linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 5/5] iio:adxl372: Add filter bandwidth support
@ 2018-07-12 15:36 Stefan Popa
  2018-07-15 10:36 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Popa @ 2018-07-12 15:36 UTC (permalink / raw)
  To: jic23
  Cc: lars, Michael.Hennerich, knaack.h, pmeerw, linux-iio,
	linux-kernel, stefan.popa

This patch adds the option for the user to select the filter bandwidth. The
user can also read the available bandwidths which are always adjusted to be
at most half of the sampling frequency. Furthermore, the currently selected
bandwidth can be read via the read_raw function, while the write_raw sets a
new bandwidth value.

Signed-off-by: Stefan Popa <stefan.popa@analog.com>
---
 drivers/iio/accel/adxl372.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c
index 498c740..a73482e 100644
--- a/drivers/iio/accel/adxl372.c
+++ b/drivers/iio/accel/adxl372.c
@@ -199,6 +199,10 @@ static const int adxl372_samp_freq_tbl[5] = {
 	400, 800, 1600, 3200, 6400,
 };
 
+static const int adxl372_bw_freq_tbl[5] = {
+	200, 400, 800, 1600, 3200,
+};
+
 #define ADXL372_ACCEL_CHANNEL(index, reg, axis) {			\
 	.type = IIO_ACCEL,						\
 	.address = reg,							\
@@ -206,7 +210,8 @@ static const int adxl372_samp_freq_tbl[5] = {
 	.channel2 = IIO_MOD_##axis,					\
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
-				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
+				    BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
+		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),	\
 	.scan_index = index,						\
 	.scan_type = {							\
 		.sign = 's',						\
@@ -585,6 +590,9 @@ static int adxl372_read_raw(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = adxl372_samp_freq_tbl[st->odr];
 		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
+		*val = adxl372_bw_freq_tbl[st->bw];
+		return IIO_VAL_INT;
 	}
 
 	return -EINVAL;
@@ -595,7 +603,7 @@ static int adxl372_write_raw(struct iio_dev *indio_dev,
 			     int val, int val2, long info)
 {
 	struct adxl372_state *st = iio_priv(indio_dev);
-	int odr_index, ret;
+	int odr_index, bw_index, ret;
 
 	switch (info) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
@@ -613,11 +621,34 @@ static int adxl372_write_raw(struct iio_dev *indio_dev,
 			ret = adxl372_set_bandwidth(st, odr_index);
 
 		return ret;
+	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
+		bw_index = adxl372_find_closest_match(adxl372_bw_freq_tbl,
+					ARRAY_SIZE(adxl372_bw_freq_tbl),
+					val);
+		return adxl372_set_bandwidth(st, bw_index);
 	default:
 		return -EINVAL;
 	}
 }
 
+static ssize_t adxl372_show_filter_freq_avail(struct device *dev,
+					      struct device_attribute *attr,
+					      char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct adxl372_state *st = iio_priv(indio_dev);
+	int i;
+	size_t len = 0;
+
+	for (i = 0; i <= st->odr; i++)
+		len += scnprintf(buf + len, PAGE_SIZE - len,
+				 "%d ", adxl372_bw_freq_tbl[i]);
+
+	buf[len - 1] = '\n';
+
+	return len;
+}
+
 static ssize_t adxl372_get_fifo_enabled(struct device *dev,
 					  struct device_attribute *attr,
 					  char *buf)
@@ -768,9 +799,12 @@ static const struct iio_trigger_ops adxl372_trigger_ops = {
 };
 
 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("400 800 1600 3200 6400");
+static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
+		       0444, adxl372_show_filter_freq_avail, NULL, 0);
 
 static struct attribute *adxl372_attributes[] = {
 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
+	&iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
 	NULL,
 };
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 5/5] iio:adxl372: Add filter bandwidth support
  2018-07-12 15:36 [PATCH 5/5] iio:adxl372: Add filter bandwidth support Stefan Popa
@ 2018-07-15 10:36 ` Jonathan Cameron
  2018-07-15 10:44   ` Lars-Peter Clausen
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2018-07-15 10:36 UTC (permalink / raw)
  To: Stefan Popa
  Cc: lars, Michael.Hennerich, knaack.h, pmeerw, linux-iio, linux-kernel

On Thu, 12 Jul 2018 18:36:58 +0300
Stefan Popa <stefan.popa@analog.com> wrote:

> This patch adds the option for the user to select the filter bandwidth. The
> user can also read the available bandwidths which are always adjusted to be
> at most half of the sampling frequency. Furthermore, the currently selected
> bandwidth can be read via the read_raw function, while the write_raw sets a
> new bandwidth value.
> 

How will the youth of today learn about aliasing if we are so nice to them?
 :)  Good user friendly design choice btw.  If someone is doing something
crazy where they actually want the aliasing they can hack the driver.

This and the previous are fine.

Nice driver, you just ran into some 'old' problems in one or two places
that it would be good to finally solve.

> Signed-off-by: Stefan Popa <stefan.popa@analog.com>
> ---
>  drivers/iio/accel/adxl372.c | 38 ++++++++++++++++++++++++++++++++++++--
>  1 file changed, 36 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/accel/adxl372.c b/drivers/iio/accel/adxl372.c
> index 498c740..a73482e 100644
> --- a/drivers/iio/accel/adxl372.c
> +++ b/drivers/iio/accel/adxl372.c
> @@ -199,6 +199,10 @@ static const int adxl372_samp_freq_tbl[5] = {
>  	400, 800, 1600, 3200, 6400,
>  };
>  
> +static const int adxl372_bw_freq_tbl[5] = {
> +	200, 400, 800, 1600, 3200,
> +};
> +
>  #define ADXL372_ACCEL_CHANNEL(index, reg, axis) {			\
>  	.type = IIO_ACCEL,						\
>  	.address = reg,							\
> @@ -206,7 +210,8 @@ static const int adxl372_samp_freq_tbl[5] = {
>  	.channel2 = IIO_MOD_##axis,					\
>  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
>  	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
> -				    BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
> +				    BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
> +		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),	\
>  	.scan_index = index,						\
>  	.scan_type = {							\
>  		.sign = 's',						\
> @@ -585,6 +590,9 @@ static int adxl372_read_raw(struct iio_dev *indio_dev,
>  	case IIO_CHAN_INFO_SAMP_FREQ:
>  		*val = adxl372_samp_freq_tbl[st->odr];
>  		return IIO_VAL_INT;
> +	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
> +		*val = adxl372_bw_freq_tbl[st->bw];
> +		return IIO_VAL_INT;
>  	}
>  
>  	return -EINVAL;
> @@ -595,7 +603,7 @@ static int adxl372_write_raw(struct iio_dev *indio_dev,
>  			     int val, int val2, long info)
>  {
>  	struct adxl372_state *st = iio_priv(indio_dev);
> -	int odr_index, ret;
> +	int odr_index, bw_index, ret;
>  
>  	switch (info) {
>  	case IIO_CHAN_INFO_SAMP_FREQ:
> @@ -613,11 +621,34 @@ static int adxl372_write_raw(struct iio_dev *indio_dev,
>  			ret = adxl372_set_bandwidth(st, odr_index);
>  
>  		return ret;
> +	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
> +		bw_index = adxl372_find_closest_match(adxl372_bw_freq_tbl,
> +					ARRAY_SIZE(adxl372_bw_freq_tbl),
> +					val);
> +		return adxl372_set_bandwidth(st, bw_index);
>  	default:
>  		return -EINVAL;
>  	}
>  }
>  
> +static ssize_t adxl372_show_filter_freq_avail(struct device *dev,
> +					      struct device_attribute *attr,
> +					      char *buf)
> +{
> +	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> +	struct adxl372_state *st = iio_priv(indio_dev);
> +	int i;
> +	size_t len = 0;
> +
> +	for (i = 0; i <= st->odr; i++)
> +		len += scnprintf(buf + len, PAGE_SIZE - len,
> +				 "%d ", adxl372_bw_freq_tbl[i]);
> +
> +	buf[len - 1] = '\n';
> +
> +	return len;
> +}
> +
>  static ssize_t adxl372_get_fifo_enabled(struct device *dev,
>  					  struct device_attribute *attr,
>  					  char *buf)
> @@ -768,9 +799,12 @@ static const struct iio_trigger_ops adxl372_trigger_ops = {
>  };
>  
>  static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("400 800 1600 3200 6400");
> +static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
> +		       0444, adxl372_show_filter_freq_avail, NULL, 0);
>  
>  static struct attribute *adxl372_attributes[] = {
>  	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
> +	&iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
>  	NULL,
>  };
>  


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 5/5] iio:adxl372: Add filter bandwidth support
  2018-07-15 10:36 ` Jonathan Cameron
@ 2018-07-15 10:44   ` Lars-Peter Clausen
  0 siblings, 0 replies; 3+ messages in thread
From: Lars-Peter Clausen @ 2018-07-15 10:44 UTC (permalink / raw)
  To: Jonathan Cameron, Stefan Popa
  Cc: Michael.Hennerich, knaack.h, pmeerw, linux-iio, linux-kernel

On 07/15/2018 12:36 PM, Jonathan Cameron wrote:
> On Thu, 12 Jul 2018 18:36:58 +0300
> Stefan Popa <stefan.popa@analog.com> wrote:
> 
>> This patch adds the option for the user to select the filter bandwidth. The
>> user can also read the available bandwidths which are always adjusted to be
>> at most half of the sampling frequency. Furthermore, the currently selected
>> bandwidth can be read via the read_raw function, while the write_raw sets a
>> new bandwidth value.
>>
> 
> How will the youth of today learn about aliasing if we are so nice to them?
>  :)  Good user friendly design choice btw.  If someone is doing something
> crazy where they actually want the aliasing they can hack the driver.

Actually they can't. The hardware itself enforces that the filter bandwidth
is below or equal to the Nyquist frequency. All the user can do is select a
bandwidth even smaller. :)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-07-15 10:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-12 15:36 [PATCH 5/5] iio:adxl372: Add filter bandwidth support Stefan Popa
2018-07-15 10:36 ` Jonathan Cameron
2018-07-15 10:44   ` Lars-Peter Clausen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).