[PATCH 03/15] iio: accel: adxl367: don't stay in standby on -EINVAL

From: Greg Kroah-Hartman

Date: Tue Oct 27 2026 - 12:23:13 EST


From: Josef Bacik <josef@xxxxxxxxxxxxxx>

adxl367_set_act_threshold() puts the sensor into standby, writes the
threshold and switches the measurement on again. A threshold above 8191 is
refused in between, and the sensor stays in standby: no samples and no
events until the next write that succeeds.

adxl367_set_range() has the same problem and a worse one. It rescales both
thresholds for the new range, times four from 8 g to 2 g. If one no longer
fits it returns -EINVAL with the sensor in standby, the new range in the
chip, the old one in st->range and rescaled thresholds in the driver's
state: from the next successful write on the samples are reported with the
wrong scale.

Switch the measurement on again if the threshold is refused. In
adxl367_set_range() see whether the thresholds fit before anything is
changed.

Fixes: cbab791c5e2a ("iio: accel: add ADXL367 driver")
Cc: stable <stable@xxxxxxxxxx>
Cc: Marcelo Schmitt <marcelo.schmitt@xxxxxxxxxx>
Cc: Antoniu Miclaus <antoniu.miclaus@xxxxxxxxxx>
Cc: "Nuno Sá" <nuno.sa@xxxxxxxxxx>
Cc: Michael Hennerich <Michael.Hennerich@xxxxxxxxxx>
Cc: David Lechner <dlechner@xxxxxxxxxxxx>
Cc: Andy Shevchenko <andy@xxxxxxxxxx>
Cc: Cosmin Tanislav <demonsingur@xxxxxxxxx>
Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
drivers/iio/accel/adxl367.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/accel/adxl367.c b/drivers/iio/accel/adxl367.c
index 026db73057c5..ba6e7423978f 100644
--- a/drivers/iio/accel/adxl367.c
+++ b/drivers/iio/accel/adxl367.c
@@ -349,8 +349,11 @@ static int adxl367_set_act_threshold(struct adxl367_state *st,
return ret;

ret = _adxl367_set_act_threshold(st, act, threshold);
- if (ret)
+ if (ret) {
+ /* Leave standby also if the value was refused */
+ adxl367_set_measure_en(st, true);
return ret;
+ }

return adxl367_set_measure_en(st, true);
}
@@ -483,6 +486,12 @@ static int adxl367_set_range(struct iio_dev *indio_dev,

guard(mutex)(&st->lock);

+ /* The thresholds are rescaled: see that they fit before any change */
+ if (max(st->act_threshold, st->inact_threshold) *
+ adxl367_range_scale_factor_tbl[st->range] /
+ adxl367_range_scale_factor_tbl[range] > ADXL367_THRESH_MAX)
+ return -EINVAL;
+
ret = adxl367_set_measure_en(st, false);
if (ret)
return ret;

--
2.56.0