diff mbox series

iio: imu: st_lsm6dsx: fix decimation factor estimation

Message ID 6d08e5a80bb0c49eb96dc883ff1ed2936f142bbb.1574956387.git.lorenzo@kernel.org (mailing list archive)
State New, archived
Headers show
Series iio: imu: st_lsm6dsx: fix decimation factor estimation | expand

Commit Message

Lorenzo Bianconi Nov. 28, 2019, 3:55 p.m. UTC
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(-)

Comments

Jonathan Cameron Dec. 1, 2019, 8:56 p.m. UTC | #1
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);
diff mbox series

Patch

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