Skip to contentSkip to conversation

imu: st_lsm6dsx: Add support for rotation sensor

PATCH Superseded Francesco Lavra 4 participants 12 messages Jan 19 – Jan 20

Subsystems (2)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover imu: st_lsm6dsx: Add support for rotation sensor

This series adds support for the rotation sensor functionality present in some chips from the ST LSM6DSX IMU family.
The second commit is a fix to a previous commit of mine [*] which made it to 6.19-rcX; the fix technically changes userspace, but this should be OK as long as it goes in during this release cycle.
Tested on LSM6DSV16X.

Changes from v2 [2]:
- amended description of patch 2 to point out that there are no supported gyro events (Jonathan)
- removed superfluous parentheses in st_lsm6dsx_fifo_setup (Lorenzo)
- added Lorenzo's acked-by tag to patch 3
- added missing checks of st_lsm6dsx_sf_set_page() return value (Jonathan)
- added comment in st_lsm6dsx_sf_write_raw (Jonathan)

Changes from v1 [1]:
- swapped patches 1 and 2 (Jonathan)
- miscellaneous stylistic changes (Andy)
- fixed usage of MICRO and MILLI constants in st_lsm6dsx_sf_read_raw and st_lsm6dsx_sf_write_raw (Andy)
- replaced scnprintf() with sysfs_emit_at() in st_lsm6dsx_sf_sampling_freq_avail (Andy)
- replaced scnprintf() with snprintf() in st_lsm6dsx_sf_probe (Andy)
- clarified in a comment in st_lsm6dsx_set_fifo_odr() that only internal sensors have a FIFO ODR configuration register (Jonathan)
- modified patch 3 description to explain justification for the extra IIO device (Jonathan)
- moved page lock from st_lsm6dsx_sf_set_page() to the callers (Jonathan)
- s/magnetometer/gyroscope/ in patch 2 description

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

Francesco Lavra (3):
iio: imu: st_lsm6dsx: set FIFO ODR for accelerometer and gyroscope
only
iio: imu: st_lsm6dsx: set buffer sampling frequency for accelerometer
only
iio: imu: st_lsm6dsx: add support for rotation sensor

 drivers/iio/imu/st_lsm6dsx/Makefile           |   2 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  26 ++-
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    |  29 ++-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  |  58 +++++
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c    | 220 ++++++++++++++++++
 5 files changed, 328 insertions(+), 7 deletions(-)
 create mode 100644 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c
Signature · 1 line
2.39.5
#1 lore raw

PATCH v3 1/3 iio: imu: st_lsm6dsx: set FIFO ODR for accelerometer and gyroscope only

The st_lsm6dsx_set_fifo_odr() function, which is called when enabling and disabling the hardware FIFO, checks the contents of the hw->settings->batch array at index sensor->id, and then sets the current ODR value in sensor registers that depend on whether the register address is set in the above array element. This logic is valid for internal sensors only, i.e. the accelerometer and gyroscope; however, since commit c91c1c844ebd ("iio: imu:
st_lsm6dsx: add i2c embedded controller support"), this function is called also when configuring the hardware FIFO for external sensors (i.e. sensors accessed through the sensor hub functionality), which can result in unrelated device registers being written.

Add a check to the beginning of st_lsm6dsx_set_fifo_odr() so that it does not touch any registers unless it is called for internal sensors.

Fixes c91c1c844ebd ("iio: imu: st_lsm6dsx: add i2c embedded controller support")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +4 -0
index 55d877745575..1ee2fc5f5f1f 100644
@@ -225,6 +225,10 @@ static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
 	const struct st_lsm6dsx_reg *batch_reg;
 	u8 data;
 
+	/* Only internal sensors have a FIFO ODR configuration register. */
+	if (sensor->id >= ARRAY_SIZE(hw->settings->batch))
+		return 0;
+
 	batch_reg = &hw->settings->batch[sensor->id];
 	if (batch_reg->addr) {
 		int val;
Signature · 1 line
2.39.5
#2 lore raw

PATCH v3 2/3 iio: imu: st_lsm6dsx: set buffer sampling frequency for accelerometer only

In reply to Francesco Lavra

The st_lsm6dsx_hwfifo_odr_store() function, which is called when userspace writes the buffer sampling frequency sysfs attribute, calls st_lsm6dsx_check_odr(), which accesses the odr_table array at index sensor->id; since this array is only 2 entries long, an access for any sensor type other than accelerometer or gyroscope is an out-of-bounds access.

The motivation for being able to set a buffer frequency different from the sensor sampling frequency is to support use cases that need accurate event detection (which requires a high sampling frequency) while retrieving sensor data at low frequency. Since all the supported event types are generated from acceleration data only, do not create the buffer sampling frequency attribute for sensor types other than the accelerometer.

Fixes 6b648a36c200 ("iio: imu: st_lsm6dsx: Decouple sensor ODR from FIFO batch data rate")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +10 -1
index 1ee2fc5f5f1f..5b28a3ffcc3d 100644
@@ -862,12 +862,21 @@ int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
 	int i, ret;
 
 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
+		const struct iio_dev_attr **attrs;
+
 		if (!hw->iio_devs[i])
 			continue;
 
+		/*
+		 * For the accelerometer, allow setting FIFO sampling frequency
+		 * values different from the sensor sampling frequency, which
+		 * may be needed to keep FIFO data rate low while sampling
+		 * acceleration data at high rates for accurate event detection.
+		 */
+		attrs = i == ST_LSM6DSX_ID_ACC ? st_lsm6dsx_buffer_attrs : NULL;
 		ret = devm_iio_kfifo_buffer_setup_ext(hw->dev, hw->iio_devs[i],
 						      &st_lsm6dsx_buffer_ops,
-						      st_lsm6dsx_buffer_attrs);
+						      attrs);
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.39.5
#3 lore raw

PATCH v3 3/3 iio: imu: st_lsm6dsx: add support for rotation sensor

In reply to Francesco Lavra

Some IMU chips in the LSM6DSX family have sensor fusion features that combine data from the accelerometer and gyroscope. One of these features generates rotation vector data and makes it available in the hardware FIFO as a quaternion (more specifically, the X, Y and Z components of the quaternion vector, expressed as 16-bit half-precision floating-point numbers).

Add support for a new sensor instance that allows receiving sensor fusion data, by defining a new struct st_lsm6dsx_sf_settings (which contains chip-specific details for the sensor fusion functionality), and adding this struct as a new field in struct st_lsm6dsx_settings. In st_lsm6dsx_core.c, populate this new struct for the LSM6DSV and LSM6DSV16X chips, and add the logic to initialize an additional IIO device if this struct is populated for the hardware type being probed.
Note: a new IIO device is being defined (as opposed to adding channels to an existing device) because the rate at which sensor fusion data is generated may not match the data rate from any of the existing devices.

Tested on LSMDSV16X.

---
 drivers/iio/imu/st_lsm6dsx/Makefile           |   2 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  26 ++-
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    |  16 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c  |  58 +++++
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c    | 220 ++++++++++++++++++
 5 files changed, 315 insertions(+), 7 deletions(-)
 create mode 100644 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c
drivers/iio/imu/st_lsm6dsx/Makefile +1 -1
index 57cbcd67d64f..19a488254de3 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 st_lsm6dsx-y := st_lsm6dsx_core.o st_lsm6dsx_buffer.o \
-		st_lsm6dsx_shub.o
+		st_lsm6dsx_shub.o st_lsm6dsx_fusion.o
 
 obj-$(CONFIG_IIO_ST_LSM6DSX) += st_lsm6dsx.o
 obj-$(CONFIG_IIO_ST_LSM6DSX_I2C) += st_lsm6dsx_i2c.o
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +21 -5
index 07b1773c87bd..4173f670f7af 100644
@@ -294,6 +294,7 @@ enum st_lsm6dsx_sensor_id {
 	ST_LSM6DSX_ID_EXT0,
 	ST_LSM6DSX_ID_EXT1,
 	ST_LSM6DSX_ID_EXT2,
+	ST_LSM6DSX_ID_SF,
 	ST_LSM6DSX_ID_MAX
 };
 
@@ -301,6 +302,15 @@ enum st_lsm6dsx_ext_sensor_id {
 	ST_LSM6DSX_ID_MAGN,
 };
 
+struct st_lsm6dsx_sf_settings {
+	const struct iio_chan_spec *chan;
+	int chan_len;
+	struct st_lsm6dsx_odr_table_entry odr_table;
+	struct st_lsm6dsx_reg fifo_enable;
+	struct st_lsm6dsx_reg page_mux;
+	struct st_lsm6dsx_reg enable;
+};
+
 /**
  * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
  * @i2c_addr: I2c slave address list.
@@ -388,6 +398,7 @@ struct st_lsm6dsx_settings {
 	struct st_lsm6dsx_hw_ts_settings ts_settings;
 	struct st_lsm6dsx_shub_settings shub_settings;
 	struct st_lsm6dsx_event_settings event_settings;
+	struct st_lsm6dsx_sf_settings sf_settings;
 };
 
 enum st_lsm6dsx_fifo_mode {
@@ -510,6 +521,9 @@ int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val);
 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
 int st_lsm6dsx_shub_read_output(struct st_lsm6dsx_hw *hw, u8 *data, int len);
+int st_lsm6dsx_sf_probe(struct st_lsm6dsx_hw *hw, const char *name);
+int st_lsm6dsx_sf_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
+int st_lsm6dsx_sf_set_odr(struct st_lsm6dsx_sensor *sensor, bool enable);
 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
 
 static inline int
@@ -564,12 +578,14 @@ st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio_dev,
 static inline int
 st_lsm6dsx_device_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable)
 {
-	if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
-	    sensor->id == ST_LSM6DSX_ID_EXT1 ||
-	    sensor->id == ST_LSM6DSX_ID_EXT2)
+	switch (sensor->id) {
+	case ST_LSM6DSX_ID_EXT0 ... ST_LSM6DSX_ID_EXT2:
 		return st_lsm6dsx_shub_set_enable(sensor, enable);
-
-	return st_lsm6dsx_sensor_set_enable(sensor, enable);
+	case ST_LSM6DSX_ID_SF:
+		return st_lsm6dsx_sf_set_enable(sensor, enable);
+	default:
+		return st_lsm6dsx_sensor_set_enable(sensor, enable);
+	}
 }
 
 static const
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +15 -1
index 5b28a3ffcc3d..9a259e04755f 100644
@@ -88,6 +88,7 @@ enum st_lsm6dsx_fifo_tag {
 	ST_LSM6DSX_EXT0_TAG = 0x0f,
 	ST_LSM6DSX_EXT1_TAG = 0x10,
 	ST_LSM6DSX_EXT2_TAG = 0x11,
+	ST_LSM6DSX_ROT_TAG = 0x13,
 };
 
 static const
@@ -226,8 +227,11 @@ static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
 	u8 data;
 
 	/* Only internal sensors have a FIFO ODR configuration register. */
-	if (sensor->id >= ARRAY_SIZE(hw->settings->batch))
+	if (sensor->id >= ARRAY_SIZE(hw->settings->batch)) {
+		if (sensor->id == ST_LSM6DSX_ID_SF)
+			return st_lsm6dsx_sf_set_odr(sensor, enable);
 		return 0;
+	}
 
 	batch_reg = &hw->settings->batch[sensor->id];
 	if (batch_reg->addr) {
@@ -580,6 +584,16 @@ st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
 	case ST_LSM6DSX_EXT2_TAG:
 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
 		break;
+	case ST_LSM6DSX_ROT_TAG:
+		/*
+		 * The sensor reports only the {X, Y, Z} elements of the
+		 * quaternion vector; set the W value to 0 (it can be derived
+		 * from the {X, Y, Z} values due to the property that the vector
+		 * is normalized).
+		 */
+		*(u16 *)(data + ST_LSM6DSX_SAMPLE_SIZE) = 0;
+		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_SF];
+		break;
 	default:
 		return -EINVAL;
 	}
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +58 -0

82 lines not on this page · raw message

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +220 -0

220 lines not on this page · raw message

Signature · 1 line
2.39.5
#4 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 19 lines · Francesco Lavra

Some IMU chips in the LSM6DSX family have sensor fusion features that combine data from the accelerometer and gyroscope. One of these features generates rotation vector data and makes it available in the hardware FIFO as a quaternion (more specifically, the X, Y and Z components of the quaternion vector, expressed as 16-bit half-precision floating-point numbers).

Add support for a new sensor instance that allows receiving sensor fusion data, by defining a new struct st_lsm6dsx_sf_settings (which contains chip-specific details for the sensor fusion functionality), and adding this struct as a new field in struct st_lsm6dsx_settings. In st_lsm6dsx_core.c, populate this new struct for the LSM6DSV and LSM6DSV16X chips, and add the logic to initialize an additional IIO device if this struct is populated for the hardware type being probed.
Note: a new IIO device is being defined (as opposed to adding channels to an existing device) because the rate at which sensor fusion data is generated may not match the data rate from any of the existing devices.

Tested on LSMDSV16X.

...

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag, · line 594

Quoted 13 lines

st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +8 -0
 	case ST_LSM6DSX_EXT2_TAG:
 		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2];
 		break;
+	case ST_LSM6DSX_ROT_TAG:
+		/*
+		 * The sensor reports only the {X, Y, Z} elements of the
+		 * quaternion vector; set the W value to 0 (it can be derived
+		 * from the {X, Y, Z} values due to the property that the vector
+		 * is normalized).
+		 */
+		*(u16 *)(data + ST_LSM6DSX_SAMPLE_SIZE) = 0;

This looks confusing taking into account

	s16 val = le16_to_cpu(*(__le16 *)data);

(which actually should use le16_to_cpup() instead of the direct conversion).

Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
+		iio_dev = hw->iio_devs[ST_LSM6DSX_ID_SF];
+		break;
 	default:
 		return -EINVAL;
 	}

...

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 58

Quoted 11 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +11 -0
+int st_lsm6dsx_sf_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable)
+{
+	struct st_lsm6dsx_hw *hw = sensor->hw;
+	const struct st_lsm6dsx_reg *en_reg;
+	int err;
+
+	guard(mutex)(&hw->page_lock);
+
+	en_reg = &hw->settings->sf_settings.enable;
+	err = st_lsm6dsx_sf_set_page(hw, true);
+	if (err < 0)

Do you need ' < 0' here? Why isn't it required at the end of the function?

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 62

Quoted 4 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +4 -0
+		return err;
+
+	err = regmap_assign_bits(hw->regmap, en_reg->addr, en_reg->mask, enable);
+	if (err < 0) {

' < 0' is not needed.

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 68

Quoted 6 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +6 -0
+		st_lsm6dsx_sf_set_page(hw, false);
+		return err;
+	}
+
+	return st_lsm6dsx_sf_set_page(hw, false);
+}

And IIRC I replied that these _sf_set_page() seems to be used only with the explicit boolean value, which means a good hint that it needs to be split just to two functions doing for true and false. It will increase the readability of the code in both places (in the caller and callee).

...

Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +7 -0
+int st_lsm6dsx_sf_set_odr(struct st_lsm6dsx_sensor *sensor, bool enable)
+{
+	struct st_lsm6dsx_hw *hw = sensor->hw;
+	const struct st_lsm6dsx_sf_settings *settings;
+	u8 data;
+	int err;
+

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 86

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +10 -0
+	guard(mutex)(&hw->page_lock);
+
+	err = st_lsm6dsx_sf_set_page(hw, true);
+	if (err < 0)
+		return err;
+
+	settings = &hw->settings->sf_settings;
+	if (enable) {
+		u8 odr_val;
+		const struct st_lsm6dsx_reg *reg = &settings->odr_table.reg;

Can we use reversed xmas tree ordering?

Quoted 21 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +21 -0
+		st_lsm6dsx_sf_get_odr_val(settings, sensor->hwfifo_odr_mHz,
+					  &odr_val);
+		data = ST_LSM6DSX_SHIFT_VAL(odr_val, reg->mask);
+		err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
+					 data);
+		if (err < 0)
+			goto out;
+	}
+
+	err = regmap_assign_bits(hw->regmap, settings->fifo_enable.addr,
+				 settings->fifo_enable.mask, enable);
+	if (err < 0)
+		goto out;
+
+	return st_lsm6dsx_sf_set_page(hw, false);
+
+out:
+	st_lsm6dsx_sf_set_page(hw, false);
+
+	return err;
+}

...

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 137

Quoted 12 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +12 -0
+static int st_lsm6dsx_sf_write_raw(struct iio_dev *iio_dev,
+				   struct iio_chan_spec const *chan,
+				   int val, int val2, long mask)
+{
+	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
+	const struct st_lsm6dsx_sf_settings *settings;
+	int err;
+
+	settings = &sensor->hw->settings->sf_settings;
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ: {
+		u32 odr_mHz = val * MILLI + val2 * MILLI / MICRO;

Probably "(MILLI / MICRO)" (with parentheses) to avoid potential overflows,

Quoted 14 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +14 -0
+		u8 odr_val;
+
+		/* check that the requested frequency is supported */
+		err = st_lsm6dsx_sf_get_odr_val(settings, odr_mHz, &odr_val);
+		if (err)
+			return err;
+
+		sensor->hwfifo_odr_mHz = odr_mHz;
+		return 0;
+	}
+	default:
+		return -EINVAL;
+	}
+}

...

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 210

Quoted 1 line
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +1 -0
+	snprintf(sensor->name, sizeof(sensor->name), "%s_sf", name);

Does GCC complain on this (make W=1 build)?
Since this can cut the string and we don't check the return value, the Q is:
is this okay to have a reduced string?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 48 lines · Andy Shevchenko
Quoted 27 lines · Francesco Lavra

Some IMU chips in the LSM6DSX family have sensor fusion features that combine data from the accelerometer and gyroscope. One of these features
generates rotation vector data and makes it available in the hardware FIFO as a quaternion (more specifically, the X, Y and Z components of the
quaternion vector, expressed as 16-bit half-precision floating-point numbers).

Add support for a new sensor instance that allows receiving sensor fusion
data, by defining a new struct st_lsm6dsx_sf_settings (which contains chip-specific details for the sensor fusion functionality), and adding this
struct as a new field in struct st_lsm6dsx_settings. In st_lsm6dsx_core.c,
populate this new struct for the LSM6DSV and LSM6DSV16X chips, and add the
logic to initialize an additional IIO device if this struct is populated
for the hardware type being probed.
Note: a new IIO device is being defined (as opposed to adding channels to
an existing device) because the rate at which sensor fusion data is generated may not match the data rate from any of the existing devices.

Tested on LSMDSV16X.

...

Quoted 16 lines

st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,

        case ST_LSM6DSX_EXT2_TAG:
                iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2]; break;
+       case ST_LSM6DSX_ROT_TAG:
+               /*
+                * The sensor reports only the {X, Y, Z} elements of the
+                * quaternion vector; set the W value to 0 (it can be derived
+                * from the {X, Y, Z} values due to the property that the vector
+                * is normalized).
+                */
+               *(u16 *)(data + ST_LSM6DSX_SAMPLE_SIZE) = 0;

Quoted 6 lines

This looks confusing taking into account

        s16 val = le16_to_cpu(*(__le16 *)data);

(which actually should use le16_to_cpup() instead of the direct conversion).

The above val variable is used to do a (val >= ST_LSM6DSX_INVALID_SAMPLE) check, which actually should be done only if (tag == ST_LSM6DSX_ACC_TAG || tag == ST_LSM6DSX_GYRO_TAG).
I will add a patch to fix this (it will fix commit 960506ed2c69 "iio: imu:
st_lsm6dsx: enable drdy-mask if available")

Quoted 8 lines
Quoted 5 lines

+               iio_dev = hw->iio_devs[ST_LSM6DSX_ID_SF]; +               break;
        default:
                return -EINVAL;
        }

...

Quoted 15 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 58

Quoted 12 lines

+int st_lsm6dsx_sf_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable)

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +10 -0
+{
+       struct st_lsm6dsx_hw *hw = sensor->hw;
+       const struct st_lsm6dsx_reg *en_reg;
+       int err;
+
+       guard(mutex)(&hw->page_lock);
+
+       en_reg = &hw->settings->sf_settings.enable;
+       err = st_lsm6dsx_sf_set_page(hw, true);
+       if (err < 0)

Do you need ' < 0' here? Why isn't it required at the end of the function?

Will drop ' < 0'

Quoted 8 lines
Quoted 5 lines

+               return err;
+
+       err = regmap_assign_bits(hw->regmap, en_reg->addr, en_reg-

Quoted 1 line

mask, enable);

+       if (err < 0) {

is not needed.

Quoted 11 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c line 68

Quoted 6 lines

+               st_lsm6dsx_sf_set_page(hw, false); +               return err;
+       }

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c new file +3 -0
+
+       return st_lsm6dsx_sf_set_page(hw, false);
+}

And IIRC I replied that these _sf_set_page() seems to be used only with the explicit boolean value, which means a good hint that it needs to be split just to two functions doing for true and false. It will increase the readability of the code in both places (in the caller and callee).

OK

Quoted 8 lines

...

Quoted 1 line

+       snprintf(sensor->name, sizeof(sensor->name), "%s_sf", name);

Does GCC complain on this (make W=1 build)?
Since this can cut the string and we don't check the return value, the Q is:
is this okay to have a reduced string?

gcc does not complain with W=1. sensor->name is appropriately sized to accommodate the longest possible name; if it wasn't, the string would be cut in the accel and gyro IIO devices too (which use a longer suffix than "_sf").

#6 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 2 lines · Francesco Lavra
Quoted 1 line · Andy Shevchenko

On Mon, Jan 19, 2026 at 11:04:49AM +0100, Francesco Lavra wrote:

...

Quoted 11 lines
Quoted 6 lines
Quoted 1 line

+       snprintf(sensor->name, sizeof(sensor->name), "%s_sf", name);

Does GCC complain on this (make W=1 build)?
Since this can cut the string and we don't check the return value, the Q is:
is this okay to have a reduced string?

gcc does not complain with W=1. sensor->name is appropriately sized to accommodate the longest possible name; if it wasn't, the string would be cut in the accel and gyro IIO devices too (which use a longer suffix than "_sf").

Right, the question is if compiler can prove that or not.

We have several patches in input subsystem to hide the warning by switching to scnprintf(), which I consider not the best approach, but still it depends if we care about cut or not. If we do, we should check for overflow.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 22 lines · Andy Shevchenko
Quoted 2 lines · Francesco Lavra
Quoted 1 line · Andy Shevchenko

On Mon, Jan 19, 2026 at 11:04:49AM +0100, Francesco Lavra wrote:

...

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

+       snprintf(sensor->name, sizeof(sensor->name), "%s_sf", name);

Does GCC complain on this (make W=1 build)?
Since this can cut the string and we don't check the return value, the Q
is:
is this okay to have a reduced string?

Quoted 6 lines

gcc does not complain with W=1. sensor->name is appropriately sized to accommodate the longest possible name; if it wasn't, the string would be
cut in the accel and gyro IIO devices too (which use a longer suffix than
"_sf").

Quoted 7 lines

Right, the question is if compiler can prove that or not.

We have several patches in input subsystem to hide the warning by switching
to scnprintf(), which I consider not the best approach, but still it depends
if we care about cut or not. If we do, we should check for overflow.

A truncated string would result in a corrupted value in /sys/bus/iio/devices/iio:deviceX/name, so I would say we do care if it's truncated. But I think the most appropriate check we could add in the code is a BUG_ON(); would that be acceptable?

#8 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 4 lines · Francesco Lavra
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Francesco Lavra
Quoted 1 line · Andy Shevchenko

On Mon, Jan 19, 2026 at 11:04:49AM +0100, Francesco Lavra wrote:

...

Quoted 24 lines
Quoted 16 lines
Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

+ snprintf(sensor->name, sizeof(sensor->name), "%s_sf",
name);

Does GCC complain on this (make W=1 build)?
Since this can cut the string and we don't check the return value, the Q
is:
is this okay to have a reduced string?

Quoted 6 lines

gcc does not complain with W=1. sensor->name is appropriately sized to accommodate the longest possible name; if it wasn't, the string would be
cut in the accel and gyro IIO devices too (which use a longer suffix than
"_sf").

Quoted 7 lines

Right, the question is if compiler can prove that or not.

We have several patches in input subsystem to hide the warning by switching
to scnprintf(), which I consider not the best approach, but still it depends
if we care about cut or not. If we do, we should check for overflow.

Quoted 4 lines

A truncated string would result in a corrupted value in /sys/bus/iio/devices/iio:deviceX/name, so I would say we do care if it's truncated. But I think the most appropriate check we could add in the code is a BUG_ON(); would that be acceptable?

No. BUG_ON() == panic == reboot (with "reboot_on_panic" or so). Mere drivers like this one should (almost) never use BUG() nor WARN(). Just add an appropriate check.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 44 lines · Andy Shevchenko
Quoted 4 lines · Francesco Lavra
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Francesco Lavra
Quoted 1 line · Andy Shevchenko

On Mon, Jan 19, 2026 at 11:04:49AM +0100, Francesco Lavra wrote:

...

Quoted 28 lines
Quoted 20 lines
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines

+       snprintf(sensor->name, sizeof(sensor->name), "%s_sf", name);

Does GCC complain on this (make W=1 build)? Since this can cut the string and we don't check the return value,
the Q
is:
is this okay to have a reduced string?

Quoted 9 lines

gcc does not complain with W=1. sensor->name is appropriately sized to
accommodate the longest possible name; if it wasn't, the string would
be
cut in the accel and gyro IIO devices too (which use a longer suffix
than
"_sf").

Quoted 7 lines

Right, the question is if compiler can prove that or not.

We have several patches in input subsystem to hide the warning by switching
to scnprintf(), which I consider not the best approach, but still it depends
if we care about cut or not. If we do, we should check for overflow.

Quoted 6 lines

A truncated string would result in a corrupted value in /sys/bus/iio/devices/iio:deviceX/name, so I would say we do care if it's
truncated. But I think the most appropriate check we could add in the code
is a BUG_ON(); would that be acceptable?

Quoted 3 lines

No. BUG_ON() == panic == reboot (with "reboot_on_panic" or so). Mere drivers like this one should (almost) never use BUG() nor WARN(). Just add an appropriate check.

I'm struggling to find a suitable error code to be returned in case of failure. What do you suggest? -ENOSPC? -EOVERFLOW?

#10 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

Quoted 7 lines · Francesco Lavra
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Francesco Lavra
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Francesco Lavra
Quoted 1 line · Andy Shevchenko

On Mon, Jan 19, 2026 at 11:04:49AM +0100, Francesco Lavra wrote:

...

Quoted 35 lines
Quoted 35 lines
Quoted 34 lines
Quoted 20 lines
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines

+ snprintf(sensor->name, sizeof(sensor->name), "%s_sf",
name);

Does GCC complain on this (make W=1 build)? Since this can cut the string and we don't check the return value,
the Q
is:
is this okay to have a reduced string?

Quoted 9 lines

gcc does not complain with W=1. sensor->name is appropriately sized to
accommodate the longest possible name; if it wasn't, the string would
be
cut in the accel and gyro IIO devices too (which use a longer suffix
than
"_sf").

Quoted 7 lines

Right, the question is if compiler can prove that or not.

We have several patches in input subsystem to hide the warning by switching
to scnprintf(), which I consider not the best approach, but still it depends
if we care about cut or not. If we do, we should check for overflow.

A truncated string would result in a corrupted value in /sys/bus/iio/devices/iio:deviceX/name, so I would say we do care if it's
truncated. But I think the most appropriate check we could add in the code
is a BUG_ON(); would that be acceptable?

Quoted 6 lines
Quoted 3 lines

No. BUG_ON() == panic == reboot (with "reboot_on_panic" or so). Mere drivers like this one should (almost) never use BUG() nor WARN(). Just add an appropriate check.

I'm struggling to find a suitable error code to be returned in case of failure. What do you suggest? -ENOSPC? -EOVERFLOW?

Same as strscpy() returns, IIRC -E2BIG.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
build bot
#11 lore raw

Re: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

In reply to Francesco Lavra
BCC: [email protected] CC: [email protected] In-Reply-To: <[email protected]> References: <[email protected]> TO: Francesco Lavra…

BCC: [email protected]
CC: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: Francesco Lavra <[email protected]>
TO: Lorenzo Bianconi <[email protected]>
TO: Jonathan Cameron <[email protected]>
TO: David Lechner <[email protected]>
TO: "Nuno Sá" <[email protected]>
TO: Andy Shevchenko <[email protected]>
TO: [email protected]
TO: [email protected]

Hi Francesco,

kernel test robot noticed the following build warnings:

[auto build test WARNING on jic23-iio/togreg]
[cannot apply to linus/master v6.19-rc6 next-20260116]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Francesco-Lavra/iio-imu-st_lsm6dsx-set-FIFO-ODR-for-accelerometer-and-gyroscope-only/20260119-181615
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
patch link:    https://lore.kernel.org/r/20260119100449.1559624-4-flavra%40baylibre.com
patch subject: [PATCH v3 3/3] iio: imu: st_lsm6dsx: add support for rotation sensor

:::::: branch date: 10 hours ago
:::::: commit date: 10 hours ago
config: sparc-randconfig-r071-20260119 (https://download.01.org/0day-ci/archive/20260120/[email protected]/config)
compiler: sparc-linux-gcc (GCC) 15.2.0
smatch version: v0.5.0-8985-g2614ff1a

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c:90 st_lsm6dsx_sf_set_odr() error: uninitialized symbol 'odr_val'.

vim +/odr_val +90 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c

aa03865dcb5ed7 Francesco Lavra 2026-01-19   69  
aa03865dcb5ed7 Francesco Lavra 2026-01-19   70  int st_lsm6dsx_sf_set_odr(struct st_lsm6dsx_sensor *sensor, bool enable)
aa03865dcb5ed7 Francesco Lavra 2026-01-19   71  {
aa03865dcb5ed7 Francesco Lavra 2026-01-19   72  	struct st_lsm6dsx_hw *hw = sensor->hw;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   73  	const struct st_lsm6dsx_sf_settings *settings;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   74  	u8 data;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   75  	int err;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   76  
aa03865dcb5ed7 Francesco Lavra 2026-01-19   77  	guard(mutex)(&hw->page_lock);
aa03865dcb5ed7 Francesco Lavra 2026-01-19   78  
aa03865dcb5ed7 Francesco Lavra 2026-01-19   79  	err = st_lsm6dsx_sf_set_page(hw, true);
aa03865dcb5ed7 Francesco Lavra 2026-01-19   80  	if (err < 0)
aa03865dcb5ed7 Francesco Lavra 2026-01-19   81  		return err;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   82  
aa03865dcb5ed7 Francesco Lavra 2026-01-19   83  	settings = &hw->settings->sf_settings;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   84  	if (enable) {
aa03865dcb5ed7 Francesco Lavra 2026-01-19   85  		u8 odr_val;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   86  		const struct st_lsm6dsx_reg *reg = &settings->odr_table.reg;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   87  
aa03865dcb5ed7 Francesco Lavra 2026-01-19   88  		st_lsm6dsx_sf_get_odr_val(settings, sensor->hwfifo_odr_mHz,
aa03865dcb5ed7 Francesco Lavra 2026-01-19   89  					  &odr_val);
aa03865dcb5ed7 Francesco Lavra 2026-01-19  @90  		data = ST_LSM6DSX_SHIFT_VAL(odr_val, reg->mask);
aa03865dcb5ed7 Francesco Lavra 2026-01-19   91  		err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
aa03865dcb5ed7 Francesco Lavra 2026-01-19   92  					 data);
aa03865dcb5ed7 Francesco Lavra 2026-01-19   93  		if (err < 0)
aa03865dcb5ed7 Francesco Lavra 2026-01-19   94  			goto out;
aa03865dcb5ed7 Francesco Lavra 2026-01-19   95  	}
aa03865dcb5ed7 Francesco Lavra 2026-01-19   96  
aa03865dcb5ed7 Francesco Lavra 2026-01-19   97  	err = regmap_assign_bits(hw->regmap, settings->fifo_enable.addr,
aa03865dcb5ed7 Francesco Lavra 2026-01-19   98  				 settings->fifo_enable.mask, enable);
aa03865dcb5ed7 Francesco Lavra 2026-01-19   99  	if (err < 0)
aa03865dcb5ed7 Francesco Lavra 2026-01-19  100  		goto out;
aa03865dcb5ed7 Francesco Lavra 2026-01-19  101  
aa03865dcb5ed7 Francesco Lavra 2026-01-19  102  	return st_lsm6dsx_sf_set_page(hw, false);
aa03865dcb5ed7 Francesco Lavra 2026-01-19  103  
aa03865dcb5ed7 Francesco Lavra 2026-01-19  104  out:
aa03865dcb5ed7 Francesco Lavra 2026-01-19  105  	st_lsm6dsx_sf_set_page(hw, false);
aa03865dcb5ed7 Francesco Lavra 2026-01-19  106  
aa03865dcb5ed7 Francesco Lavra 2026-01-19  107  	return err;
aa03865dcb5ed7 Francesco Lavra 2026-01-19  108  }
aa03865dcb5ed7 Francesco Lavra 2026-01-19  109  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki