Skip to contentSkip to conversation

st_lsm6dsx: add tap event detection

PATCH Applied Francesco Lavra 3 participants 20 messages Dec 1, 2025 – Dec 7, 2025

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

v4 cover st_lsm6dsx: add tap event detection

The bulk of this patch set consists of reworking the existing code for event detection (which supports IIO_EV_TYPE_THRESH events only) in order to make it generic to accommodate different event types. Actual support for tap events is implemented in the last patch.
Tested on LSMDSV16X.

Changes from v3 [3]:
- replaced double negation with single negation in st_lsm6dsx_event_setup() (Andy)

Changes from v2 [2]:
- replaced status_reg and status_mask in struct st_lsm6dsx_event_src with a struct st_lsm6dsx_reg field (Lorenzo)
- dropped new local variables in favor of directly using fields of existing struct variables (Lorenzo)
- added blank line after return statements (Lorenzo)
- added Andy's Reviewed-by tag to patches 1-6,8-9
- added Lorenzo's Acked-by tag to patches 1-3,6,9

Changes from v1 [1]:
- added Fixes tag to patch 1/9 (Andy, Lorenzo)
- added st_lsm6dsx_field_get() temporary macro, to be removed once the generic FIELD_GET() macro lands (Andy)
- refactored st_lsm6dsx_write_event_config() with the addition of two helper functions st_lsm6dsx_check_other_events() and st_lsm6dsx_events_enable() (Andy)
- replaced dynamic allocation of iio_chan_spec and iio_event_spec arrays with the addition of new static arrays (Jonathan)
- changed line wrapping to 80 characters (Lorenzo, Andy)
- changed line wrapping for commit messages to 75 characters (Jonathan)
- added comments to st_lsm6dsx_event_setup() and st_lsm6dsx_get_event_reg() (Andy)
- miscellaneous stylistic changes (Andy)

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

Francesco Lavra (9):
iio: imu: st_lsm6dsx: fix iio_chan_spec for sensors without event
detection
iio: imu: st_lsm6dsx: make event_settings more generic
iio: imu: st_lsm6dsx: move wakeup event enable mask to event_src
iio: imu: st_lsm6dsx: rework code to check for enabled events
iio: imu: st_lsm6dsx: remove event_threshold field from hw struct
iio: imu: st_lsm6dsx: make event management functions generic
iio: imu: st_lsm6dsx: add event configurability on a per axis basis
iio: imu: st_lsm6dsx: add event spec parameter to iio_chan_spec
initializer
iio: imu: st_lsm6dsx: add tap event detection

 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  56 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 538 +++++++++++++------
 2 files changed, 395 insertions(+), 199 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v4 1/9 iio: imu: st_lsm6dsx: fix iio_chan_spec for sensors without event detection

The st_lsm6dsx_acc_channels array of struct iio_chan_spec has a non-NULL event_spec field, indicating support for IIO events. However, event detection is not supported for all sensors, and if userspace tries to configure accelerometer wakeup events on a sensor device that does not support them (e.g. LSM6DS0), st_lsm6dsx_write_event() dereferences a NULL pointer when trying to write to the wakeup register.
Define an additional struct iio_chan_spec array whose members have a NULL event_spec field, and use this array instead of st_lsm6dsx_acc_channels for sensors without event detection capability.

Fixes b5969abfa8b8 ("iio: imu: st_lsm6dsx: add motion events")
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +11 -4
index 216160549b5a..a09df9d772dd 100644
@@ -103,6 +103,13 @@ static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(3),
 };
 
+static const struct iio_chan_spec st_lsm6ds0_acc_channels[] = {
+	ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
+	ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
+	ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
+};
+
 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x22, IIO_MOD_X, 0),
 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x24, IIO_MOD_Y, 1),
@@ -144,8 +151,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
 		},
 		.channels = {
 			[ST_LSM6DSX_ID_ACC] = {
-				.chan = st_lsm6dsx_acc_channels,
-				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
+				.chan = st_lsm6ds0_acc_channels,
+				.len = ARRAY_SIZE(st_lsm6ds0_acc_channels),
 			},
 			[ST_LSM6DSX_ID_GYRO] = {
 				.chan = st_lsm6ds0_gyro_channels,
@@ -1445,8 +1452,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
 		},
 		.channels = {
 			[ST_LSM6DSX_ID_ACC] = {
-				.chan = st_lsm6dsx_acc_channels,
-				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
+				.chan = st_lsm6ds0_acc_channels,
+				.len = ARRAY_SIZE(st_lsm6ds0_acc_channels),
 			},
 			[ST_LSM6DSX_ID_GYRO] = {
 				.chan = st_lsm6dsx_gyro_channels,
Signature · 1 line
2.39.5
maintainer Applied
#2 lore raw

Re: [PATCH v4 1/9] iio: imu: st_lsm6dsx: fix iio_chan_spec for sensors without event detection

Quoted 10 lines · On Mon, 1 Dec 2025 11:00:10 +0100 Francesco Lavra <[email protected]> wrote:

The st_lsm6dsx_acc_channels array of struct iio_chan_spec has a non-NULL event_spec field, indicating support for IIO events. However, event detection is not supported for all sensors, and if userspace tries to configure accelerometer wakeup events on a sensor device that does not support them (e.g. LSM6DS0), st_lsm6dsx_write_event() dereferences a NULL pointer when trying to write to the wakeup register.
Define an additional struct iio_chan_spec array whose members have a NULL event_spec field, and use this array instead of st_lsm6dsx_acc_channels for sensors without event detection capability.

Quoted 4 lines
Fixes b5969abfa8b8 ("iio: imu: st_lsm6dsx: add motion events")

Applied to the fixes-togreg branch of iio.git (which I might not push out until I can rebase on rc1) and marked for stable.

If this causes conflicts with the rest of the series that will have to wait until this is in upstream and can merge that into the main iio tree.

This feels to urgent to sit on for a whole extra cycle.

Thanks,

Jonathan

Quoted 44 lines
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +11 -4
index 216160549b5a..a09df9d772dd 100644
@@ -103,6 +103,13 @@ static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
 	IIO_CHAN_SOFT_TIMESTAMP(3),
 };
 
+static const struct iio_chan_spec st_lsm6ds0_acc_channels[] = {
+	ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
+	ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
+	ST_LSM6DSX_CHANNEL(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
+	IIO_CHAN_SOFT_TIMESTAMP(3),
+};
+
 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x22, IIO_MOD_X, 0),
 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x24, IIO_MOD_Y, 1),
@@ -144,8 +151,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
 		},
 		.channels = {
 			[ST_LSM6DSX_ID_ACC] = {
-				.chan = st_lsm6dsx_acc_channels,
-				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
+				.chan = st_lsm6ds0_acc_channels,
+				.len = ARRAY_SIZE(st_lsm6ds0_acc_channels),
 			},
 			[ST_LSM6DSX_ID_GYRO] = {
 				.chan = st_lsm6ds0_gyro_channels,
@@ -1445,8 +1452,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
 		},
 		.channels = {
 			[ST_LSM6DSX_ID_ACC] = {
-				.chan = st_lsm6dsx_acc_channels,
-				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
+				.chan = st_lsm6ds0_acc_channels,
+				.len = ARRAY_SIZE(st_lsm6ds0_acc_channels),
 			},
 			[ST_LSM6DSX_ID_GYRO] = {
 				.chan = st_lsm6dsx_gyro_channels,
#3 lore raw

PATCH v4 2/9 iio: imu: st_lsm6dsx: make event_settings more generic

In reply to Francesco Lavra

The st_lsm6dsx_event_settings structure contains fields specific for one event type (wakeup). In preparation for adding support for more event types, introduce an event id enum and a generic event source structure, and replace wakeup-specific data in struct st_lsm6dsx_event_settings with an array of event source structures.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  20 ++-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 146 ++++++++++++-------
 2 files changed, 106 insertions(+), 60 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +14 -6
index a4f558899767..80bc5686454b 100644
@@ -242,14 +242,22 @@ struct st_lsm6dsx_shub_settings {
 	u8 pause;
 };
 
+enum st_lsm6dsx_event_id {
+	ST_LSM6DSX_EVENT_WAKEUP,
+	ST_LSM6DSX_EVENT_MAX
+};
+
+struct st_lsm6dsx_event_src {
+	struct st_lsm6dsx_reg value;
+	struct st_lsm6dsx_reg status;
+	u8 status_x_mask;
+	u8 status_y_mask;
+	u8 status_z_mask;
+};
+
 struct st_lsm6dsx_event_settings {
 	struct st_lsm6dsx_reg enable_reg;
-	struct st_lsm6dsx_reg wakeup_reg;
-	u8 wakeup_src_reg;
-	u8 wakeup_src_status_mask;
-	u8 wakeup_src_z_mask;
-	u8 wakeup_src_y_mask;
-	u8 wakeup_src_x_mask;
+	struct st_lsm6dsx_event_src sources[ST_LSM6DSX_EVENT_MAX];
 };
 
 enum st_lsm6dsx_ext_sensor_id {
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +92 -54

229 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#4 lore raw

Re: [PATCH v4 2/9] iio: imu: st_lsm6dsx: make event_settings more generic

Quoted 9 lines · On Mon, 1 Dec 2025 11:00:11 +0100 Francesco Lavra <[email protected]> wrote:

The st_lsm6dsx_event_settings structure contains fields specific for one event type (wakeup). In preparation for adding support for more event types, introduce an event id enum and a generic event source structure, and replace wakeup-specific data in struct st_lsm6dsx_event_settings with an array of event source structures.

Given I've largely forgotten what this looks like but it has some good review I'm going to do the application one patch at a time.
That should also reduce what we need to handle if there is a clash with the fix going a different path.

Applied this one to my local tree. I'll rebase that on rc1 once available and push out as togreg at that time.

Thanks,

Jonathan

#5 lore raw

PATCH v4 3/9 iio: imu: st_lsm6dsx: move wakeup event enable mask to event_src

In reply to Francesco Lavra

The mask value being assigned to the irq1_func and irq2_func fields of the irq_config struct is specific to a single event source (i.e. the wakeup event), and as such it should be separate from the definition of the interrupt function registers, which cover multiple event sources. In preparation for adding support for more event types, change the irq1_func and irq2_func type from an {address, mask} pair to an address, and move the mask value to a new field of struct st_lsm6dsx_event_src. No functional changes.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  7 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 80 +++++++-------------
 2 files changed, 30 insertions(+), 57 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +4 -3
index 80bc5686454b..4200e5231950 100644
@@ -249,6 +249,7 @@ enum st_lsm6dsx_event_id {
 
 struct st_lsm6dsx_event_src {
 	struct st_lsm6dsx_reg value;
+	u8 enable_mask;
 	struct st_lsm6dsx_reg status;
 	u8 status_x_mask;
 	u8 status_y_mask;
@@ -343,8 +344,8 @@ struct st_lsm6dsx_settings {
 	struct {
 		struct st_lsm6dsx_reg irq1;
 		struct st_lsm6dsx_reg irq2;
-		struct st_lsm6dsx_reg irq1_func;
-		struct st_lsm6dsx_reg irq2_func;
+		u8 irq1_func;
+		u8 irq2_func;
 		struct st_lsm6dsx_reg lir;
 		struct st_lsm6dsx_reg clear_on_read;
 		struct st_lsm6dsx_reg hla;
@@ -443,7 +444,7 @@ struct st_lsm6dsx_hw {
 	u8 ts_sip;
 	u8 sip;
 
-	const struct st_lsm6dsx_reg *irq_routing;
+	u8 irq_routing;
 	u8 event_threshold;
 	u8 enable_event;
 
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +26 -54

176 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer Applied
#6 lore raw

Re: [PATCH v4 3/9] iio: imu: st_lsm6dsx: move wakeup event enable mask to event_src

Quoted 9 lines · On Mon, 1 Dec 2025 11:00:12 +0100 Francesco Lavra <[email protected]> wrote:

The mask value being assigned to the irq1_func and irq2_func fields of the irq_config struct is specific to a single event source (i.e. the wakeup event), and as such it should be separate from the definition of the interrupt function registers, which cover multiple event sources. In preparation for adding support for more event types, change the irq1_func and irq2_func type from an {address, mask} pair to an address, and move the mask value to a new field of struct st_lsm6dsx_event_src. No functional changes.

Quoted 3 lines

Applied

Thanks,
Jonathan

#7 lore raw

PATCH v4 4/9 iio: imu: st_lsm6dsx: rework code to check for enabled events

In reply to Francesco Lavra

The enable_event field in struct st_lsm6dsx_hw does not lend itself well to handling multiple event sources, so it will have to be modified to add support for more event sources. As a preparatory step, remove references to this field from code that does not deal with event management; rework the st_lsm6dsx_check_events() function so that it returns whether any events are currently enabled on a given sensor.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +8 -10
index ba163dd2ab82..828e495c870c 100644
@@ -1756,11 +1756,11 @@ __st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
 }
 
 static int
-st_lsm6dsx_check_events(struct st_lsm6dsx_sensor *sensor, bool enable)
+st_lsm6dsx_check_events(struct st_lsm6dsx_sensor *sensor)
 {
 	struct st_lsm6dsx_hw *hw = sensor->hw;
 
-	if (sensor->id == ST_LSM6DSX_ID_GYRO || enable)
+	if (sensor->id != ST_LSM6DSX_ID_ACC)
 		return 0;
 
 	return hw->enable_event;
@@ -1769,7 +1769,7 @@ st_lsm6dsx_check_events(struct st_lsm6dsx_sensor *sensor, bool enable)
 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
 				 bool enable)
 {
-	if (st_lsm6dsx_check_events(sensor, enable))
+	if (st_lsm6dsx_check_events(sensor))
 		return 0;
 
 	return __st_lsm6dsx_sensor_set_enable(sensor, enable);
@@ -1797,11 +1797,9 @@ static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
 	if (err < 0)
 		return err;
 
-	if (!hw->enable_event) {
-		err = st_lsm6dsx_sensor_set_enable(sensor, false);
-		if (err < 0)
-			return err;
-	}
+	err = st_lsm6dsx_sensor_set_enable(sensor, false);
+	if (err < 0)
+		return err;
 
 	*val = (s16)le16_to_cpu(data);
 
@@ -2762,7 +2760,7 @@ static int st_lsm6dsx_suspend(struct device *dev)
 			continue;
 
 		if (device_may_wakeup(dev) &&
-		    sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event) {
+		    st_lsm6dsx_check_events(sensor)) {
 			/* Enable wake from IRQ */
 			enable_irq_wake(hw->irq);
 			continue;
@@ -2793,7 +2791,7 @@ static int st_lsm6dsx_resume(struct device *dev)
 
 		sensor = iio_priv(hw->iio_devs[i]);
 		if (device_may_wakeup(dev) &&
-		    sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event)
+		    st_lsm6dsx_check_events(sensor))
 			disable_irq_wake(hw->irq);
 
 		if (!(hw->suspend_mask & BIT(sensor->id)))
Signature · 1 line
2.39.5
maintainer Applied
#8 lore raw

Re: [PATCH v4 4/9] iio: imu: st_lsm6dsx: rework code to check for enabled events

Quoted 9 lines · On Mon, 1 Dec 2025 11:00:13 +0100 Francesco Lavra <[email protected]> wrote:

The enable_event field in struct st_lsm6dsx_hw does not lend itself well to handling multiple event sources, so it will have to be modified to add support for more event sources. As a preparatory step, remove references to this field from code that does not deal with event management; rework the st_lsm6dsx_check_events() function so that it returns whether any events are currently enabled on a given sensor.

Applied.

Note that if anyone else has more comments or wants to add tags I'm fine rebasing as appropriate until sometime after rc1 when I push out as togreg and linux-next picks it up.

Thanks,

Jonathan

#9 lore raw

PATCH v4 5/9 iio: imu: st_lsm6dsx: remove event_threshold field from hw struct

In reply to Francesco Lavra

This field is used to store the wakeup event detection threshold value. When adding support for more event types, some of which may have different threshold values for different axes, storing all threshold values for all event sources would be cumbersome. Thus, remove this field altogether, and read the currently configured value from the sensor when requested by userspace.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  3 +--
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 12 +++++++++---
 2 files changed, 10 insertions(+), 5 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +1 -2
index 4200e5231950..b27a833d5107 100644
@@ -79,6 +79,7 @@ enum st_lsm6dsx_hw_id {
 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN	((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
 					 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
+#define st_lsm6dsx_field_get(mask, reg)	((reg & mask) >> __ffs(mask))
 
 #define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx)		\
 {									\
@@ -421,7 +422,6 @@ struct st_lsm6dsx_sensor {
  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
  * @buff: Device read buffer.
  * @irq_routing: pointer to interrupt routing configuration.
- * @event_threshold: wakeup event threshold.
  * @enable_event: enabled event bitmask.
  * @iio_devs: Pointers to acc/gyro iio_dev instances.
  * @settings: Pointer to the specific sensor settings in use.
@@ -445,7 +445,6 @@ struct st_lsm6dsx_hw {
 	u8 sip;
 
 	u8 irq_routing;
-	u8 event_threshold;
 	u8 enable_event;
 
 	u8 *buff;
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +9 -3
index 828e495c870c..dbdf9bb9e258 100644
@@ -1911,12 +1911,20 @@ static int st_lsm6dsx_read_event(struct iio_dev *iio_dev,
 {
 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
 	struct st_lsm6dsx_hw *hw = sensor->hw;
+	const struct st_lsm6dsx_reg *reg;
+	u8 data;
+	int err;
 
 	if (type != IIO_EV_TYPE_THRESH)
 		return -EINVAL;
 
+	reg = &hw->settings->event_settings.sources[ST_LSM6DSX_EVENT_WAKEUP].value;
+	err = st_lsm6dsx_read_locked(hw, reg->addr, &data, sizeof(data));
+	if (err < 0)
+		return err;
+
 	*val2 = 0;
-	*val = hw->event_threshold;
+	*val = st_lsm6dsx_field_get(reg->mask, data);
 
 	return IIO_VAL_INT;
 }
@@ -1948,8 +1956,6 @@ st_lsm6dsx_write_event(struct iio_dev *iio_dev,
 	if (err < 0)
 		return -EINVAL;
 
-	hw->event_threshold = val;
-
 	return 0;
 }
 
Signature · 1 line
2.39.5
maintainer Applied
#10 lore raw

Re: [PATCH v4 5/9] iio: imu: st_lsm6dsx: remove event_threshold field from hw struct

Quoted 19 lines · On Mon, 1 Dec 2025 11:00:14 +0100 Francesco Lavra <[email protected]> wrote:

This field is used to store the wakeup event detection threshold value. When adding support for more event types, some of which may have different threshold values for different axes, storing all threshold values for all event sources would be cumbersome. Thus, remove this field altogether, and read the currently configured value from the sensor when requested by userspace.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  3 +--
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 12 +++++++++---
 2 files changed, 10 insertions(+), 5 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +0 -0
index 4200e5231950..b27a833d5107 100644

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h enum st_lsm6dsx_hw_id { · line 82

Quoted 4 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +1 -0
@@ -79,6 +79,7 @@ enum st_lsm6dsx_hw_id {
 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN	((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
 					 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
+#define st_lsm6dsx_field_get(mask, reg)	((reg & mask) >> __ffs(mask))

I'm not going to fuss too much about this as expectation is that
this will be replaced soon anyway with a generic version but convention would be to (reg) & (mask) to avoid precedence of operator problems if there are any in the parameters passed. The generic version will I guess also deal with avoiding multiple evaluation of mask.

Anyhow, doesn't matter here given the simple user.

Applied.

Jonathan

#11 lore raw

Re: [PATCH v4 5/9] iio: imu: st_lsm6dsx: remove event_threshold field from hw struct

Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Mon, 1 Dec 2025 11:00:14 +0100 Francesco Lavra <[email protected]> wrote:

This field is used to store the wakeup event detection threshold value. When adding support for more event types, some of which may have different threshold values for different axes, storing all threshold values for all event sources would be cumbersome. Thus, remove this field altogether, and read the currently configured value from the sensor when requested by userspace.

...

Quoted 9 lines
Quoted 2 lines

#define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
+#define st_lsm6dsx_field_get(mask, reg) ((reg & mask) >> __ffs(mask))

I'm not going to fuss too much about this as expectation is that
this will be replaced soon anyway with a generic version but convention would be to (reg) & (mask) to avoid precedence of operator problems if there are any in the parameters passed. The generic version will I guess also deal with avoiding multiple evaluation of mask.

Quoted 3 lines

Anyhow, doesn't matter here given the simple user.

Applied.

Note, the new API is already in Linus' tree.

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

PATCH v4 6/9 iio: imu: st_lsm6dsx: make event management functions generic

In reply to Francesco Lavra

In preparation for adding support for more event types, use an array indexed by event ID instead of a scalar value to store enabled events, and refactor the functions to configure and report events so that their implementation is not specific for wakeup events. Move the logic to update the global event interrupt enable flag from st_lsm6dsx_event_setup() to its calling function, so that it can take into account also event sources different from the source being configured. While changing the signature of the st_lsm6dsx_event_setup() function, opportunistically add the currently unused axis parameter, which will be used when adding support for enabling and disabling events on a per axis basis.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |   2 +-
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 150 ++++++++++++++-----
 2 files changed, 110 insertions(+), 42 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +1 -1
index b27a833d5107..2aae56b7db0b 100644
@@ -445,7 +445,7 @@ struct st_lsm6dsx_hw {
 	u8 sip;
 
 	u8 irq_routing;
-	u8 enable_event;
+	u8 enable_event[ST_LSM6DSX_EVENT_MAX];
 
 	u8 *buff;
 
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +109 -41

299 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer Applied
#13 lore raw

Re: [PATCH v4 6/9] iio: imu: st_lsm6dsx: make event management functions generic

Quoted 11 lines · On Mon, 1 Dec 2025 11:00:15 +0100 Francesco Lavra <[email protected]> wrote:

In preparation for adding support for more event types, use an array indexed by event ID instead of a scalar value to store enabled events, and refactor the functions to configure and report events so that their implementation is not specific for wakeup events. Move the logic to update the global event interrupt enable flag from st_lsm6dsx_event_setup() to its calling function, so that it can take into account also event sources different from the source being configured. While changing the signature of the st_lsm6dsx_event_setup() function, opportunistically add the currently unused axis parameter, which will be used when adding support for enabling and disabling events on a per axis basis.

Quoted 3 lines

Applied.

#14 lore raw

PATCH v4 7/9 iio: imu: st_lsm6dsx: add event configurability on a per axis basis

In reply to Francesco Lavra

In order to be able to configure event detection on a per axis basis (for either setting an event threshold/sensitivity value, or enabling/disabling event detection), add new axis-specific fields to struct st_lsm6dsx_event_src, and modify the logic that handles event configuration to properly handle axis-specific settings when supported by a given event source.
A future commit will add actual event sources with per-axis configurability.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  7 ++
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 95 +++++++++++++++++---
 2 files changed, 88 insertions(+), 14 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +7 -0
index 2aae56b7db0b..515aadbee3a4 100644
@@ -250,7 +250,14 @@ enum st_lsm6dsx_event_id {
 
 struct st_lsm6dsx_event_src {
 	struct st_lsm6dsx_reg value;
+	struct st_lsm6dsx_reg x_value;
+	struct st_lsm6dsx_reg y_value;
+	struct st_lsm6dsx_reg z_value;
 	u8 enable_mask;
+	u8 enable_axis_reg;
+	u8 enable_x_mask;
+	u8 enable_y_mask;
+	u8 enable_z_mask;
 	struct st_lsm6dsx_reg status;
 	u8 status_x_mask;
 	u8 status_y_mask;
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +81 -14

140 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer Applied
#15 lore raw

Re: [PATCH v4 7/9] iio: imu: st_lsm6dsx: add event configurability on a per axis basis

Quoted 10 lines · On Mon, 1 Dec 2025 11:00:16 +0100 Francesco Lavra <[email protected]> wrote:

In order to be able to configure event detection on a per axis basis (for either setting an event threshold/sensitivity value, or enabling/disabling event detection), add new axis-specific fields to struct st_lsm6dsx_event_src, and modify the logic that handles event configuration to properly handle axis-specific settings when supported by a given event source.
A future commit will add actual event sources with per-axis configurability.

Applied.

Thanks,

Jonathan

#16 lore raw

PATCH v4 8/9 iio: imu: st_lsm6dsx: add event spec parameter to iio_chan_spec initializer

In reply to Francesco Lavra

In preparation for adding support for more event sources, add to the ST_LSM6DSX_CHANNEL_ACC() iio_chan_spec initializer macro an iio_event_spec array argument, so that this macro can be used with different arrays by sensors that support different event sources; change the st_lsm6dsx_event struct declaration to an array (renamed as st_lsm6dsx_ev_motion) so that it can be passed to the macro (and opportunistically move it from the header file where it does not belong to the C file where it is used). In addition, remove from this macro the channel type parameter and hard-code IIO_ACCEL in the macro definition, since all callers use IIO_ACCEL as channel type argument.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      | 15 ++++-----------
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 15 ++++++++++++---
 2 files changed, 16 insertions(+), 14 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +4 -11
index 515aadbee3a4..3edff8456a8f 100644
@@ -81,9 +81,9 @@ enum st_lsm6dsx_hw_id {
 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
 #define st_lsm6dsx_field_get(mask, reg)	((reg & mask) >> __ffs(mask))
 
-#define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx)		\
+#define ST_LSM6DSX_CHANNEL_ACC(addr, mod, scan_idx, events)		\
 {									\
-	.type = chan_type,						\
+	.type = IIO_ACCEL,						\
 	.address = addr,						\
 	.modified = 1,							\
 	.channel2 = mod,						\
@@ -97,9 +97,9 @@ enum st_lsm6dsx_hw_id {
 		.storagebits = 16,					\
 		.endianness = IIO_LE,					\
 	},								\
-	.event_spec = &st_lsm6dsx_event,				\
+	.event_spec = events,						\
+	.num_event_specs = ARRAY_SIZE(events),				\
 	.ext_info = st_lsm6dsx_ext_info,				\
-	.num_event_specs = 1,						\
 }
 
 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx)		\
@@ -468,13 +468,6 @@ struct st_lsm6dsx_hw {
 	} scan[ST_LSM6DSX_ID_MAX];
 };
 
-static __maybe_unused const struct iio_event_spec st_lsm6dsx_event = {
-	.type = IIO_EV_TYPE_THRESH,
-	.dir = IIO_EV_DIR_EITHER,
-	.mask_separate = BIT(IIO_EV_INFO_VALUE) |
-			 BIT(IIO_EV_INFO_ENABLE)
-};
-
 static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = {
 	0x7, 0x0,
 };
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +12 -3
index e6c769049981..04a3c65d4546 100644
@@ -96,10 +96,19 @@
 
 #define ST_LSM6DSX_TS_SENSITIVITY		25000UL /* 25us */
 
+static const struct iio_event_spec st_lsm6dsx_ev_motion[] = {
+	{
+		.type = IIO_EV_TYPE_THRESH,
+		.dir = IIO_EV_DIR_EITHER,
+		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
+				 BIT(IIO_EV_INFO_ENABLE),
+	},
+};
+
 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
-	ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
-	ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
-	ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
+	ST_LSM6DSX_CHANNEL_ACC(0x28, IIO_MOD_X, 0, st_lsm6dsx_ev_motion),
+	ST_LSM6DSX_CHANNEL_ACC(0x2a, IIO_MOD_Y, 1, st_lsm6dsx_ev_motion),
+	ST_LSM6DSX_CHANNEL_ACC(0x2c, IIO_MOD_Z, 2, st_lsm6dsx_ev_motion),
 	IIO_CHAN_SOFT_TIMESTAMP(3),
 };
 
Signature · 1 line
2.39.5
maintainer Applied
#17 lore raw

Re: [PATCH v4 8/9] iio: imu: st_lsm6dsx: add event spec parameter to iio_chan_spec initializer

Quoted 11 lines · On Mon, 1 Dec 2025 11:00:17 +0100 Francesco Lavra <[email protected]> wrote:

In preparation for adding support for more event sources, add to the ST_LSM6DSX_CHANNEL_ACC() iio_chan_spec initializer macro an iio_event_spec array argument, so that this macro can be used with different arrays by sensors that support different event sources; change the st_lsm6dsx_event struct declaration to an array (renamed as st_lsm6dsx_ev_motion) so that it can be passed to the macro (and opportunistically move it from the header file where it does not belong to the C file where it is used). In addition, remove from this macro the channel type parameter and hard-code IIO_ACCEL in the macro definition, since all callers use IIO_ACCEL as channel type argument.

Quoted 2 lines

Applied.

Thanks,
J

#18 lore raw

PATCH v4 9/9 iio: imu: st_lsm6dsx: add tap event detection

In reply to Francesco Lavra

In order to allow sensors to advertise tap event capability and report tap events, define a new struct iio_event_spec array that includes a tap event spec, and a new struct iio_chan_spec array that references the new iio_event_spec array; for the LSM6DSV chip family, use the new iio_chan_spec array and define an event source for tap events. Tested on LSMDSV16X.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h      |  1 +
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 57 +++++++++++++++++++-
 2 files changed, 56 insertions(+), 2 deletions(-)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +1 -0
index 3edff8456a8f..957dae73e13d 100644
@@ -245,6 +245,7 @@ struct st_lsm6dsx_shub_settings {
 
 enum st_lsm6dsx_event_id {
 	ST_LSM6DSX_EVENT_WAKEUP,
+	ST_LSM6DSX_EVENT_TAP,
 	ST_LSM6DSX_EVENT_MAX
 };
 
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +55 -2

93 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#19 lore raw

Re: [PATCH v4 9/9] iio: imu: st_lsm6dsx: add tap event detection

Quoted 10 lines · On Mon, 1 Dec 2025 11:00:18 +0100 Francesco Lavra <[email protected]> wrote:

In order to allow sensors to advertise tap event capability and report tap events, define a new struct iio_event_spec array that includes a tap event spec, and a new struct iio_chan_spec array that references the new iio_event_spec array; for the LSM6DSV chip family, use the new iio_chan_spec array and define an event source for tap events. Tested on LSMDSV16X.

Nice series and thanks to those who did all the reviews!

Applied this one as well. Seems there was never a clash with patch 1 going via the other path. Great as that makes it easier for me ;)

I have pushed this out as the testing branch of iio.git for now but that will definitely be rebased.

Jonathan