iio: accel: adxl345: update period cache after successful write
Subsystems (2)
PATCH RESEND v2 1/1 iio: accel: adxl345: update period cache after successful write
The ADXL345 event-period read path returns st->inact_time_ms. The setters for inactivity, free-fall and the ODR-derived default currently update this cache before writing TIME_INACT or TIME_FF. If the regmap write fails, the sysfs write reports an error but a subsequent read returns the requested period even though it was not programmed.
Update the cached period only after the corresponding regmap write succeeds.
---
Resend: unchanged code; submitted via b4 web submission to avoid delivery-time wrapping.
v2:
- Keep Fixes: tags on a single line (Nuno)
- Use a Latin-script name in From: and Signed-off-by: (Nuno)
- Drop the long validation notes; build-tested only, not tested on hardware
The missing serialization between concurrent sysfs callers that Nuno pointed out is a separate issue and will be addressed in a follow-up patch.
drivers/iio/accel/adxl345_core.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/iio/accel/adxl345_core.c +22 -5
@@ -391,30 +391,47 @@ static int adxl345_set_default_time(struct adxl345_state *st) odr =FIELD_GET (ADXL345_BW_RATE_MSK , regval); val =clamp (max_boundary - adxl345_odr_tbl[odr][0 ], min_boundary, max_boundary); - st->inact_time_ms =MILLI * val;/* Inactivity time in s */ -return regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val); + ret =regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val); +if (ret) +return ret; + + st->inact_time_ms =MILLI * val; + +return 0 ; }static int adxl345_set_inactivity_time (struct adxl345_state *st,u32 val_int) { +int ret; + + ret =regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val_int); +if (ret) +return ret; + st->inact_time_ms =MILLI * val_int; -return regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val_int); +return 0 ; }static int adxl345_set_freefall_time (struct adxl345_state *st,u32 val_fract) { +int ret; +/* * Datasheet max. value is 255 * 5000 us = 1.275000 seconds. * * Recommended values between 100ms and 350ms (0x14 to 0x46) */ + ret =regmap_write (st->regmap,ADXL345_REG_TIME_FF , +DIV_ROUND_CLOSEST (val_fract,5 )); +if (ret) +return ret; + st->inact_time_ms =DIV_ROUND_UP (val_fract,MILLI ); -return regmap_write (st->regmap,ADXL345_REG_TIME_FF , -DIV_ROUND_CLOSEST (val_fract,5 )); +return 0 ; }/**
base-commit: 9ee8306121495d2a25aa5d1bfd519f2748786b83
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 9 lines · On Sun, Oct 18, 2026 at 09:42:00PM +0800, Wenqiang Zhao via B4 Relay wrote:
The ADXL345 event-period read path returns
st->inact_time_ms. The setters for inactivity, free-fall and the ODR-derived default currently update this cache before writingTIME_INACTorTIME_FF. If the regmap write fails, the sysfs write reports an error but a subsequent read returns the requested period even though it was not programmed.Update the cached period only after the corresponding regmap write succeeds.
...
drivers/iio/accel/adxl345_core.c static int adxl345_set_default_time(struct adxl345_state *st) · line 400
Quoted 7 lines
drivers/iio/accel/adxl345_core.c +5
-1/* Inactivity time in s */ -return regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val); + ret =regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val); +if (ret) +return ret; + + st->inact_time_ms =MILLI * val;
While at it, replace MILLI with MSEC_PER_SEC (from time.h).
Quoted 1 line
(quoted diff) +1
-0+ return 0;
...
drivers/iio/accel/adxl345_core.c static int adxl345_set_default_time(struct adxl345_state *st) · line 413
Quoted 9 lines
static int
adxl345_set_inactivity_time(struct adxl345_state *st, u32 val_int){drivers/iio/accel/adxl345_core.c +6
-0+int ret; + + ret =regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val_int); +if (ret) +return ret; + st->inact_time_ms =MILLI * val_int;
Ditto.
Quoted 3 lines
drivers/iio/accel/adxl345_core.c +1
-1-return regmap_write (st->regmap,ADXL345_REG_TIME_INACT , val_int); +return 0 ; }
...
Quoted 5 lines
static int
adxl345_set_freefall_time(struct adxl345_state *st, u32 val_fract){drivers/iio/accel/adxl345_core.c +2
-0+int ret; +/*
drivers/iio/accel/adxl345_core.c static int adxl345_set_default_time(struct adxl345_state *st) · line 432
Quoted 10 lines
drivers/iio/accel/adxl345_core.c +5
-0* Datasheet max. value is 255 * 5000 us = 1.275000 seconds. * * Recommended values between 100ms and 350ms (0x14 to 0x46) */ + ret =regmap_write (st->regmap,ADXL345_REG_TIME_FF , +DIV_ROUND_CLOSEST (val_fract,5 )); +if (ret) +return ret; + st->inact_time_ms =DIV_ROUND_UP (val_fract,MILLI );
Hmm... Okay, since it's three instances and only one you actually touched perhaps a separate patch.
Quoted 4 lines
drivers/iio/accel/adxl345_core.c +1
-2-return regmap_write (st->regmap,ADXL345_REG_TIME_FF , -DIV_ROUND_CLOSEST (val_fract,5 )); +return 0 ; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · On Sun, Oct 18, 2026 at 09:42:00PM +0800, Wenqiang Zhao via B4 Relay wrote:
The ADXL345 event-period read path returns
st->inact_time_ms. The setters for inactivity, free-fall and the ODR-derived default currently update this cache before writingTIME_INACTorTIME_FF. If the regmap write fails, the sysfs write reports an error but a subsequent read returns the requested period even though it was not programmed.Update the cached period only after the corresponding regmap write succeeds.
Assuming that my comments may lead to a separate cleanup patch later on.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Andy,
Thanks for the review and the Reviewed-by tag. I'll address the time-unit constants in a separate cleanup patch, keeping this fix focused on updating the cache after successful writes.
Best regards,
Wenqiang
Quoted 17 lines · Andy Shevchenko
Quoted 8 lines · On Sun, Oct 18, 2026 at 09:42:00PM +0800, Wenqiang Zhao via B4 Relay wrote:
The ADXL345 event-period read path returns
st->inact_time_ms. The setters for inactivity, free-fall and the ODR-derived default currently update this cache before writingTIME_INACTorTIME_FF. If the regmap write fails, the sysfs write reports an error but a subsequent read returns the requested period even though it was not programmed.Update the cached period only after the corresponding regmap write succeeds.
Reviewed-by Andy Shevchenko <[email protected]>Assuming that my comments may lead to a separate cleanup patch later on.
--
With Best Regards,
Andy Shevchenko
Quoted 6 lines · On Mon, 19 Oct 2026 07:53:28 -0500 Iron Berrington <[email protected]>…
Hi Andy,
Thanks for the review and the Reviewed-by tag. I'll address the time-unit constants in a separate cleanup patch, keeping this fix focused on updating the cache after successful writes.
Applied.
Quoted 23 lines
Best regards,
WenqiangQuoted 17 lines · Andy Shevchenko
Quoted 8 lines · On Sun, Oct 18, 2026 at 09:42:00PM +0800, Wenqiang Zhao via B4 Relay wrote:
The ADXL345 event-period read path returns
st->inact_time_ms. The setters for inactivity, free-fall and the ODR-derived default currently update this cache before writingTIME_INACTorTIME_FF. If the regmap write fails, the sysfs write reports an error but a subsequent read returns the requested period even though it was not programmed.Update the cached period only after the corresponding regmap write succeeds.
Reviewed-by Andy Shevchenko <[email protected]>Assuming that my comments may lead to a separate cleanup patch later on.
--
With Best Regards,
Andy Shevchenko