[PATCH v5 4/5] iio: accel: adxl345: Implement event scaling for ABI compliance

Taha Ed-Dafili
2026-02-24T14:04:09Z

The ADXL345 uses a fixed threshold resolution of 62.5 mg/LSB for
event-related registers. Previously, the driver reported raw
values without a scale factor.

Implement IIO_EV_INFO_SCALE for all event types to provide the
conversion factor (0.612915 m/s^2) as required by the IIO ABI.

Consequently, remove the obsolete comment in adxl345_read_event_value()
which stated that the scale factor is not applied.

Add explicit write rejection for IIO_EV_INFO_SCALE in
adxl345_write_event_value() returning -EINVAL.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Taha Ed-Dafili <0rayn.dev@gmail.com>
---
drivers/iio/accel/adxl345_core.c | 32 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/drivers/iio/accel/adxl345_core.c b/drivers/iio/accel/adxl345_core.c
index 1ac99263ad8e..722b5018757e 100644
--- a/drivers/iio/accel/adxl345_core.c
+++ b/drivers/iio/accel/adxl345_core.c
@@ -213,7 +213,8 @@ static const struct iio_event_spec adxl345_events[] = {
.dir = IIO_EV_DIR_RISING,
.mask_shared_by_type =
BIT(IIO_EV_INFO_ENABLE) |
- BIT(IIO_EV_INFO_VALUE),
+ BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_SCALE),
},
{
/* activity, ac bit set */
@@ -221,14 +222,17 @@ static const struct iio_event_spec adxl345_events[] = {
.dir = IIO_EV_DIR_RISING,
.mask_shared_by_type =
BIT(IIO_EV_INFO_ENABLE) |
- BIT(IIO_EV_INFO_VALUE),
+ BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_SCALE),
},
{
/* single tap */
.type = IIO_EV_TYPE_GESTURE,
.dir = IIO_EV_DIR_SINGLETAP,
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
- .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
+ .mask_shared_by_type =
+ BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_SCALE) |
BIT(IIO_EV_INFO_TIMEOUT),
},
{
@@ -238,6 +242,7 @@ static const struct iio_event_spec adxl345_events[] = {
.mask_shared_by_type =
BIT(IIO_EV_INFO_ENABLE) |
BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_SCALE) |
BIT(IIO_EV_INFO_RESET_TIMEOUT) |
BIT(IIO_EV_INFO_TAP2_MIN_DELAY),
},
@@ -277,6 +282,7 @@ static const struct iio_event_spec adxl345_fake_chan_events[] = {
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
.mask_shared_by_type =
BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_SCALE) |
BIT(IIO_EV_INFO_PERIOD),
},
{
@@ -286,6 +292,7 @@ static const struct iio_event_spec adxl345_fake_chan_events[] = {
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
.mask_shared_by_type =
BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_SCALE) |
BIT(IIO_EV_INFO_PERIOD),
},
};
@@ -1343,6 +1350,16 @@ static int adxl345_read_event_value(struct iio_dev *indio_dev,
unsigned int tap_threshold;
int ret;

+ /*
+ * The event threshold LSB is fixed at 62.5 mg/LSB
+ * 0.0625 * 9.80665 = 0.612915625 m/s^2
+ */
+ if (info == IIO_EV_INFO_SCALE) {
+ *val = 0;
+ *val2 = 612915;
+ return IIO_VAL_INT_PLUS_MICRO;
+ }
+
switch (type) {
case IIO_EV_TYPE_MAG:
return adxl345_read_mag_value(st, dir, info,
@@ -1357,12 +1374,6 @@ static int adxl345_read_event_value(struct iio_dev *indio_dev,
case IIO_EV_TYPE_GESTURE:
switch (info) {
case IIO_EV_INFO_VALUE:
- /*
- * The scale factor would be 62.5mg/LSB (i.e. 0xFF = 16g) but
- * not applied here. In context of this general purpose sensor,
- * what imports is rather signal intensity than the absolute
- * measured g value.
- */
ret = regmap_read(st->regmap, ADXL345_REG_THRESH_TAP,
&tap_threshold);
if (ret)
@@ -1403,6 +1414,9 @@ static int adxl345_write_event_value(struct iio_dev *indio_dev,
if (ret)
return ret;

+ if (info == IIO_EV_INFO_SCALE)
+ return -EINVAL;
+
switch (type) {
case IIO_EV_TYPE_MAG:
ret = adxl345_write_mag_value(st, dir, info,
--
2.47.3