linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: imu: st_lsm6dsx: fix decimation factor estimation
@ 2019-11-28 15:55 Lorenzo Bianconi
  2019-12-01 20:56 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Lorenzo Bianconi @ 2019-11-28 15:55 UTC (permalink / raw)
  To: jic23; +Cc: lorenzo.bianconi, mario.tesi, linux-iio

Fix decimation factor and sip estimation for LSM6DSM series
(max value for decimation factor is 32).
If gyro and accel sensors are enabled at 12.5Hz and 416Hz
respectively, decimation factor lookup will fail, producing
unaligned data.
Remove unused decimator filed in st_lsm6dsx_sensor structure.

Fixes: f8710f0357bc ("iio: imu: st_lsm6dsx: express odr in mHZ")
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 --
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 25 +++++++++++++------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
index b54aefcdaad4..dc55d7dff3eb 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
@@ -320,7 +320,6 @@ enum st_lsm6dsx_fifo_mode {
  * @odr: Output data rate of the sensor [Hz].
  * @watermark: Sensor watermark level.
  * @sip: Number of samples in a given pattern.
- * @decimator: FIFO decimation factor.
  * @ts_ref: Sensor timestamp reference for hw one.
  * @ext_info: Sensor settings if it is connected to i2c controller
  */
@@ -334,7 +333,6 @@ struct st_lsm6dsx_sensor {
 
 	u16 watermark;
 	u8 sip;
-	u8 decimator;
 	s64 ts_ref;
 
 	struct {
diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
index bfd4c6306c0b..cb536b81a1c2 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
@@ -78,14 +78,20 @@ struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
 	{ 32, 0x7 },
 };
 
-static int st_lsm6dsx_get_decimator_val(u8 val)
+static int
+st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor *sensor, u32 max_odr)
 {
 	const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
+	u32 decimator =  max_odr / sensor->odr;
 	int i;
 
-	for (i = 0; i < max_size; i++)
-		if (st_lsm6dsx_decimator_table[i].decimator == val)
+	if (decimator > 1)
+		decimator = round_down(decimator, 2);
+
+	for (i = 0; i < max_size; i++) {
+		if (st_lsm6dsx_decimator_table[i].decimator == decimator)
 			break;
+	}
 
 	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
 }
@@ -111,6 +117,13 @@ static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
 	}
 }
 
+static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
+{
+	u8 sip = sensor->odr / min_odr;
+
+	return sip > 1 ? round_down(sip, 2) : sip;
+}
+
 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
 {
 	const struct st_lsm6dsx_reg *ts_dec_reg;
@@ -131,12 +144,10 @@ static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
 		sensor = iio_priv(hw->iio_devs[i]);
 		/* update fifo decimators and sample in pattern */
 		if (hw->enable_mask & BIT(sensor->id)) {
-			sensor->sip = sensor->odr / min_odr;
-			sensor->decimator = max_odr / sensor->odr;
-			data = st_lsm6dsx_get_decimator_val(sensor->decimator);
+			sensor->sip = st_lsm6dsx_get_sip(sensor, min_odr);
+			data = st_lsm6dsx_get_decimator_val(sensor, max_odr);
 		} else {
 			sensor->sip = 0;
-			sensor->decimator = 0;
 			data = 0;
 		}
 		ts_sip = max_t(u16, ts_sip, sensor->sip);
-- 
2.21.0


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

* Re: [PATCH] iio: imu: st_lsm6dsx: fix decimation factor estimation
  2019-11-28 15:55 [PATCH] iio: imu: st_lsm6dsx: fix decimation factor estimation Lorenzo Bianconi
@ 2019-12-01 20:56 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2019-12-01 20:56 UTC (permalink / raw)
  To: Lorenzo Bianconi; +Cc: lorenzo.bianconi, mario.tesi, linux-iio

On Thu, 28 Nov 2019 17:55:18 +0200
Lorenzo Bianconi <lorenzo@kernel.org> wrote:

> Fix decimation factor and sip estimation for LSM6DSM series
> (max value for decimation factor is 32).
> If gyro and accel sensors are enabled at 12.5Hz and 416Hz
> respectively, decimation factor lookup will fail, producing
> unaligned data.

I'll confess I don't really follow how those values make sense
(as opposed to 13Hz) but I'm sure they do :)

> Remove unused decimator filed in st_lsm6dsx_sensor structure.
> 
> Fixes: f8710f0357bc ("iio: imu: st_lsm6dsx: express odr in mHZ")
> Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Applied to the fixes-togreg branch of iio.git.

Thanks,

Jonathan

> ---
>  drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 --
>  .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 25 +++++++++++++------
>  2 files changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
> index b54aefcdaad4..dc55d7dff3eb 100644
> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
> @@ -320,7 +320,6 @@ enum st_lsm6dsx_fifo_mode {
>   * @odr: Output data rate of the sensor [Hz].
>   * @watermark: Sensor watermark level.
>   * @sip: Number of samples in a given pattern.
> - * @decimator: FIFO decimation factor.
>   * @ts_ref: Sensor timestamp reference for hw one.
>   * @ext_info: Sensor settings if it is connected to i2c controller
>   */
> @@ -334,7 +333,6 @@ struct st_lsm6dsx_sensor {
>  
>  	u16 watermark;
>  	u8 sip;
> -	u8 decimator;
>  	s64 ts_ref;
>  
>  	struct {
> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
> index bfd4c6306c0b..cb536b81a1c2 100644
> --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
> @@ -78,14 +78,20 @@ struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
>  	{ 32, 0x7 },
>  };
>  
> -static int st_lsm6dsx_get_decimator_val(u8 val)
> +static int
> +st_lsm6dsx_get_decimator_val(struct st_lsm6dsx_sensor *sensor, u32 max_odr)
>  {
>  	const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
> +	u32 decimator =  max_odr / sensor->odr;
>  	int i;
>  
> -	for (i = 0; i < max_size; i++)
> -		if (st_lsm6dsx_decimator_table[i].decimator == val)
> +	if (decimator > 1)
> +		decimator = round_down(decimator, 2);
> +
> +	for (i = 0; i < max_size; i++) {
> +		if (st_lsm6dsx_decimator_table[i].decimator == decimator)
>  			break;
> +	}
>  
>  	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
>  }
> @@ -111,6 +117,13 @@ static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
>  	}
>  }
>  
> +static u8 st_lsm6dsx_get_sip(struct st_lsm6dsx_sensor *sensor, u32 min_odr)
> +{
> +	u8 sip = sensor->odr / min_odr;
> +
> +	return sip > 1 ? round_down(sip, 2) : sip;
> +}
> +
>  static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
>  {
>  	const struct st_lsm6dsx_reg *ts_dec_reg;
> @@ -131,12 +144,10 @@ static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
>  		sensor = iio_priv(hw->iio_devs[i]);
>  		/* update fifo decimators and sample in pattern */
>  		if (hw->enable_mask & BIT(sensor->id)) {
> -			sensor->sip = sensor->odr / min_odr;
> -			sensor->decimator = max_odr / sensor->odr;
> -			data = st_lsm6dsx_get_decimator_val(sensor->decimator);
> +			sensor->sip = st_lsm6dsx_get_sip(sensor, min_odr);
> +			data = st_lsm6dsx_get_decimator_val(sensor, max_odr);
>  		} else {
>  			sensor->sip = 0;
> -			sensor->decimator = 0;
>  			data = 0;
>  		}
>  		ts_sip = max_t(u16, ts_sip, sensor->sip);


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

end of thread, other threads:[~2019-12-01 20:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-28 15:55 [PATCH] iio: imu: st_lsm6dsx: fix decimation factor estimation Lorenzo Bianconi
2019-12-01 20:56 ` Jonathan Cameron

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).