Skip to contentSkip to conversation

iio: imu: st_lsm6dsx: Decouple sensor ODR from FIFO batch data rate

PATCH In review Francesco Lavra 2 participants 4 messages Oct 17, 2025 – Oct 18, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover iio: imu: st_lsm6dsx: Decouple sensor ODR from FIFO batch data rate

The rate at which accelerometer or gyroscope sensor samples are fed to the hardware FIFO (batch data rate, or BDR) does not have to coincide with the sensor sampling frequency (output data rate, or ODR); the only requirement is for the BDR to not be greater than the ODR. Having a BDR lower than the ODR is useful in cases where an application requires a high sampling rate for accurate detection of motion events (e.g. wakeup events), but wants to read sensor sample values from the device buffer at a lower data rate (e.g. to minimize the amount of I2C or SPI traffic and the rate of periodic interrupts).
This change set amends the st_lsm6dsx IIO driver to support the above use case. The first commit is a trivial fix to the source code comment that indicates what measurement unit is used for the odr field of struct st_lsm6ds_sensor, while the second commit introduces a new hwfifo_odr_mHz field in the same struct to implement the new functionality.

Changes from v2 [2]:
- fixed Fixes tag in patch 1/2
- removed local variable from st_lsm6dsx_hwfifo_odr_show() (Lorenzo)
- replaced multi-assignments with on assignment per line (Lorenzo)
- added Acked-by tags to patches 1/2 and 2/2 (Lorenzo)

Changes from v1 [1]:
- added Fixes tag to patch 1/2 (Lorenzo)
- in sysfs store handler, moved iio_device_claim_direct() call to beginning of function (Lorenzo)
- changed new struct st_lsm6dsx_sensor field name from bdr to hwfifo_odr_mHz (Lorenzo, David)
- replaced min_t() and max_t() calls with min() and max() (Andy)
- added setting of buffer frequency equal to main frequency whenever the latter is updated via sysfs (Jonathan)
- miscellaneous stylistic modifications (Lorenzo, Andy, Jonathan)

[1] https://lore.kernel.org/linux-iio/[email protected]/T/#u
[2] https://lore.kernel.org/linux-iio/[email protected]/T/#u

Francesco Lavra (2):
iio: imu: st_lsm6dsx: Fix measurement unit for odr struct member
iio: imu: st_lsm6dsx: Decouple sensor ODR from FIFO batch data rate

 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  4 +-
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 71 ++++++++++++++++---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  |  7 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c  |  2 +
 4 files changed, 73 insertions(+), 11 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v3 1/2 iio: imu: st_lsm6dsx: Fix measurement unit for odr struct member

The odr field in struct st_lsm6dsx_sensor contains a data rate value expressed in mHz, not in Hz.

Fixes f8710f0357bc3 ("iio: imu: st_lsm6dsx: express odr in mHZ")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +1 -1
index c225b246c8a5..bd366c6e282a 100644
@@ -365,7 +365,7 @@ enum st_lsm6dsx_fifo_mode {
  * @id: Sensor identifier.
  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
  * @gain: Configured sensor sensitivity.
- * @odr: Output data rate of the sensor [Hz].
+ * @odr: Output data rate of the sensor [mHz].
  * @samples_to_discard: Number of samples to discard for filters settling time.
  * @watermark: Sensor watermark level.
  * @decimator: Sensor decimation factor.
Signature · 1 line
2.39.5
#2 lore raw

PATCH v3 2/2 iio: imu: st_lsm6dsx: Decouple sensor ODR from FIFO batch data rate

In reply to Francesco Lavra

The rate at which accelerometer or gyroscope sensor samples are fed to the hardware FIFO (batch data rate, or BDR) does not have to coincide with the sensor sampling frequency (output data rate, or ODR); the only requirement is for the BDR to not be greater than the ODR. Having a BDR lower than the ODR is useful in cases where an application requires a high sampling rate for accurate detection of motion events (e.g. wakeup events), but wants to read sensor sample values from the hardware FIFO at a lower data rate (e.g. to minimize the amount of I2C or SPI traffic and the rate of periodic interrupts).
To support the above use case, add a sampling_frequency sysfs attribute to the buffer directory of st_lsm6dsx IIO devices, which controls the BDR for a given sensor independently from the "main" sampling_frequency attribute (which controls the ODR); introduce a new hwfifo_odr_mHz field in struct st_lsm6dsx_sensor to keep track of the current BDR value, and use this field instead of the odr field in the code that deals with the FIFO data rate. In the sensor hub driver, make the hwfifo_odr_mHz value always mirror the odr value, since there is no separate configuration setting to control the BDR for data produced by the sensor hub functionality. For backwards compatibility, set the buffer frequency equal to the main frequency whenever the latter is updated via sysfs; if userspace wants a different buffer frequency, it has to write to the relevant sysfs attribute after any writes to the main frequency attribute.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  2 +
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 71 ++++++++++++++++---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  |  7 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c  |  2 +
 4 files changed, 72 insertions(+), 10 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +2 -0
index bd366c6e282a..3cd520bdec46 100644
@@ -366,6 +366,7 @@ enum st_lsm6dsx_fifo_mode {
  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
  * @gain: Configured sensor sensitivity.
  * @odr: Output data rate of the sensor [mHz].
+ * hwfifo_odr_mHz: Batch data rate for hardware FIFO [mHz]
  * @samples_to_discard: Number of samples to discard for filters settling time.
  * @watermark: Sensor watermark level.
  * @decimator: Sensor decimation factor.
@@ -380,6 +381,7 @@ struct st_lsm6dsx_sensor {
 
 	u32 gain;
 	u32 odr;
+	u32 hwfifo_odr_mHz;
 
 	u16 samples_to_discard;
 	u16 watermark;
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +63 -8

118 lines not on this page · raw message

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +5 -2
index c65ad49829e7..216160549b5a 100644
@@ -1847,10 +1847,12 @@ static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
 
 		val = val * 1000 + val2 / 1000;
 		val = st_lsm6dsx_check_odr(sensor, val, &data);
-		if (val < 0)
+		if (val < 0) {
 			err = val;
-		else
+		} else {
 			sensor->odr = val;
+			sensor->hwfifo_odr_mHz = val;
+		}
 		break;
 	}
 	default:
@@ -2384,6 +2386,7 @@ static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
 	sensor->id = id;
 	sensor->hw = hw;
 	sensor->odr = hw->settings->odr_table[id].odr_avl[0].milli_hz;
+	sensor->hwfifo_odr_mHz = sensor->odr;
 	sensor->gain = hw->settings->fs_table[id].fs_avl[0].gain;
 	sensor->watermark = 1;
 
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +2 -0
index 3c5e65dc0f97..d6a1eeb151ca 100644
@@ -640,6 +640,7 @@ __st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
 
 		sensor->ext_info.slv_odr = val;
 		sensor->odr = odr;
+		sensor->hwfifo_odr_mHz = odr;
 		return 0;
 	}
 	case IIO_CHAN_INFO_SCALE:
@@ -746,6 +747,7 @@ st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw,
 	sensor->id = id;
 	sensor->hw = hw;
 	sensor->odr = hw->settings->odr_table[ref_id].odr_avl[0].milli_hz;
+	sensor->hwfifo_odr_mHz = sensor->odr;
 	sensor->ext_info.slv_odr = info->odr_table.odr_avl[0].milli_hz;
 	sensor->gain = info->fs_table.fs_avl[0].gain;
 	sensor->ext_info.settings = info;
Signature · 1 line
2.39.5
maintainer
#3 lore raw
In reply to Francesco Lavra
Quoted 7 lines · On Fri, 17 Oct 2025 18:42:53 +0200 Francesco Lavra <[email protected]> wrote:

The rate at which accelerometer or gyroscope sensor samples are fed to the hardware FIFO (batch data rate, or BDR) does not have to coincide with the sensor sampling frequency (output data rate, or ODR); the only requirement is for the BDR to not be greater than the ODR. Having a BDR lower than the ODR is useful in cases where an application requires a high sampling rate for accurate detection of motion events (e.g. wakeup events), but wants to read sensor

Quoted 9 lines

sample values from the device buffer at a lower data rate (e.g. to minimize the amount of I2C or SPI traffic and the rate of periodic interrupts).
This change set amends the st_lsm6dsx IIO driver to support the above use case. The first commit is a trivial fix to the source code comment that indicates what measurement unit is used for the odr field of struct st_lsm6ds_sensor, while the second commit introduces a new hwfifo_odr_mHz field in the same struct to implement the new functionality.

Applied to the togreg branch of iio.git which is initially pushed out as testing for the autobuilders to poke at it.

Thanks

Jonathan