On Thu, 20 Feb 2025 10:42:32 +0000 Lothar Rubusch <l.rubusch@xxxxxxxxx> wrote: > Add the inactivity feature of the sensor. When activity and inactivity > are enabled, a link bit will be set linking activity and inactivity > handling. Additionally, the auto-sleep mode will be enabled. Due to the > link bit the sensor is going to auto-sleep when inactivity was > detected. This to me looks like the justification that comment in the previous patch should mention for why we enable /disable the two together. > > Inactivity detection needs a threshold to be configured, and a time > after which it will go into inactivity state if measurements under > threshold. > > When a ODR is configured this time for inactivity is adjusted with a > corresponding reasonable default value, in order to have higher > frequencies and lower inactivity times, and lower sample frequency but > give more time until inactivity. Both with reasonable upper and lower > boundaries, since many of the sensor's features (e.g. auto-sleep) will > need to operate beween 12.5 Hz and 400 Hz. This is a default setting > when actively changing sample frequency, explicitly setting the time > until inactivity will overwrite the default. > > Similarly, setting the g-range will provide a default value for the > activity and inactivity thresholds. Both are implicit defaults, but > equally can be overwritten to be explicitly configured. > > Signed-off-by: Lothar Rubusch <l.rubusch@xxxxxxxxx> A few things inline. > @@ -307,6 +322,17 @@ static int adxl345_write_act_axis(struct adxl345_state *st, > st->act_axis_ctrl); > if (ret) > return ret; > + Probably drop this blank line. > + } else { > + st->inact_axis_ctrl = en > + ? st->inact_axis_ctrl | ADXL345_REG_INACT_AXIS_MSK > + : st->inact_axis_ctrl & ~ADXL345_REG_INACT_AXIS_MSK; > + > + ret = regmap_update_bits(st->regmap, ADXL345_REG_ACT_INACT_CTRL, > + adxl345_act_axis_msk[type], > + st->inact_axis_ctrl); > + if (ret) > + return ret; Maybe it is worth returning in both legs of the if / else? For this one can just return regmap_update_bits()... > } > return 0; > } > + > +/** > + * adxl345_set_inact_time_s - Configure inactivity time explicitly or by ODR. > + * @st: The sensor state instance. > + * @val_s: A desired time value, between 0 and 255. > + * > + * If val_s is 0, a default inactivity time will be computed. It should take > + * power consumption into consideration. Thus it shall be shorter for higher > + * frequencies and longer for lower frequencies. Hence, frequencies above 255 Hz > + * shall default to 10 s and frequencies below 10 Hz shall result in 255 s to > + * detect inactivity. > + * > + * The approach simply subtracts the pre-decimal figure of the configured > + * sample frequency from 255 s to compute inactivity time [s]. Sub-Hz are thus > + * ignored in this estimation. The recommended ODRs for various features > + * (activity/inactivity, sleep modes, free fall, etc.) lie between 12.5 Hz and > + * 400 Hz, thus higher or lower frequencies will result in the boundary > + * defaults or need to be explicitly specified via val_s. > + * > + * Return: 0 or error value. > + */ > +static int adxl345_set_inact_time_s(struct adxl345_state *st, u32 val_s) > +{ > + unsigned int max_boundary = 255; > + unsigned int min_boundary = 10; > + unsigned int val = min(val_s, max_boundary); > + enum adxl345_odr odr; > + unsigned int regval; > + int ret; > + > + if (val == 0) { > + ret = regmap_read(st->regmap, ADXL345_REG_BW_RATE, ®val); > + if (ret) > + return ret; > + odr = FIELD_GET(ADXL345_BW_RATE_MSK, regval); > + > + val = (adxl345_odr_tbl[odr][0] > max_boundary) > + ? min_boundary : max_boundary - adxl345_odr_tbl[odr][0]; > + } > + > + ret = regmap_write(st->regmap, ADXL345_REG_TIME_INACT, val); > + if (ret) > + return ret; return regmap_write() > + > + return 0; > }