From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Lothar Rubusch <l.rubusch@gmail.com>
Cc: lars@metafoo.de, Michael.Hennerich@analog.com, jic23@kernel.org,
dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
corbet@lwn.net, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
eraretuya@gmail.com
Subject: Re: [PATCH v9 08/11] iio: accel: adxl345: add inactivity feature
Date: Thu, 12 Jun 2025 15:15:15 +0300 [thread overview]
Message-ID: <aErE0xmlm4qBHg03@smile.fi.intel.com> (raw)
In-Reply-To: <20250610215933.84795-9-l.rubusch@gmail.com>
On Tue, Jun 10, 2025 at 09:59:30PM +0000, Lothar Rubusch wrote:
> Add the inactivity feature of the sensor to the driver. 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.
>
> Inactivity detection needs a threshold to be configured and a period of
> time in seconds. After, it will transition to inactivity state, if
> measurements stay below inactivity threshold.
>
> When a ODR is configured the period for inactivity is adjusted with a
> corresponding reasonable default value, in order to have higher
> frequencies, 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 between 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.
...
> +static const struct iio_event_spec adxl345_fake_chan_events[] = {
> + {
> + /* inactivity */
> + .type = IIO_EV_TYPE_MAG,
> + .dir = IIO_EV_DIR_FALLING,
> + .mask_separate = BIT(IIO_EV_INFO_ENABLE),
> + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
> + BIT(IIO_EV_INFO_PERIOD),
Slightly better
.mask_shared_by_type =
BIT(IIO_EV_INFO_VALUE) |
BIT(IIO_EV_INFO_PERIOD),
> + },
> +};
And the same for other similar cases.
...
> +/**
> + * adxl345_set_inact_time - Configure inactivity time explicitly or by ODR.
> + * @st: The sensor state instance.
> + * @val_s: A desired time value, between 0 and 255.
> + *
> + * Inactivity time can be configured between 1 and 255 sec. If a val_s of 0
> + * is configured by a user, then a default inactivity time will be computed.
> + *
> + * In such case, 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(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];
clamp() ?
> + }
> +
> + return regmap_write(st->regmap, ADXL345_REG_TIME_INACT, val);
> +}
...
> if (type == ADXL345_ACTIVITY) {
> axis_ctrl = ADXL345_ACT_X_EN | ADXL345_ACT_Y_EN |
> ADXL345_ACT_Z_EN;
> } else {
> - axis_ctrl = 0x00;
> + axis_ctrl = ADXL345_INACT_X_EN | ADXL345_INACT_Y_EN |
> + ADXL345_INACT_Z_EN;
> }
Now this can be as simple as
axis_ctrl = ADXL345_ACT_X_EN;
if (type == ADXL345_ACTIVITY)
axis_ctrl |= ADXL345_ACT_Y_EN | ADXL345_ACT_Z_EN;
else
axis_ctrl |= ADXL345_INACT_Y_EN | ADXL345_INACT_Z_EN;
Yeah, I don't know how to make the diff better (it gets worse), but the end
result is better.
One way, which I don't like much is to previously have this conditional written as:
axis_ctrl = ADXL345_ACT_X_EN;
if (type == ADXL345_ACTIVITY)
axis_ctrl |= ADXL345_ACT_Y_EN | ADXL345_ACT_Z_EN;
else
axis_ctrl = 0;
...
> + ret = regmap_assign_bits(st->regmap, ADXL345_REG_POWER_CTL,
> + (ADXL345_POWER_CTL_AUTO_SLEEP | ADXL345_POWER_CTL_LINK),
Unneeded parentheses.
> + en);
> if (ret)
> return ret;
...
> static int adxl345_set_odr(struct adxl345_state *st, enum adxl345_odr odr)
> {
> - return regmap_update_bits(st->regmap, ADXL345_REG_BW_RATE,
> + int ret;
> +
> + ret = regmap_update_bits(st->regmap, ADXL345_REG_BW_RATE,
> ADXL345_BW_RATE_MSK,
> FIELD_PREP(ADXL345_BW_RATE_MSK, odr));
> + if (ret)
> + return ret;
> +
> + /* update inactivity time by ODR */
> + return adxl345_set_inact_time(st, 0);
Okay, in this case the initial form of
int ret;
ret = ...
if (ret)
return ret;
return 0;
will be better with the respectful comment (as Jonathan suggested) in that
change that this is not optimal as standalone change, but it will help reduce
churn in the next change(s).
> }
...
> static int adxl345_set_range(struct adxl345_state *st, enum adxl345_range range)
> {
> - return regmap_update_bits(st->regmap, ADXL345_REG_DATA_FORMAT,
Same here.
> + unsigned int act_threshold, inact_threshold;
> + unsigned int range_old;
> + unsigned int regval;
> + int ret;
> +
> + ret = regmap_read(st->regmap, ADXL345_REG_DATA_FORMAT, ®val);
> + if (ret)
> + return ret;
> + range_old = FIELD_GET(ADXL345_DATA_FORMAT_RANGE, regval);
> +
> + ret = regmap_read(st->regmap,
> + adxl345_act_thresh_reg[ADXL345_ACTIVITY],
> + &act_threshold);
> + if (ret)
> + return ret;
> +
> + ret = regmap_read(st->regmap,
> + adxl345_act_thresh_reg[ADXL345_INACTIVITY],
> + &inact_threshold);
> + if (ret)
> + return ret;
> +
> + ret = regmap_update_bits(st->regmap, ADXL345_REG_DATA_FORMAT,
> ADXL345_DATA_FORMAT_RANGE,
> FIELD_PREP(ADXL345_DATA_FORMAT_RANGE, range));
> + if (ret)
> + return ret;
> +
> + act_threshold = act_threshold * adxl345_range_factor_tbl[range_old]
> + / adxl345_range_factor_tbl[range];
> + act_threshold = min(U8_MAX, max(1, act_threshold));
> +
> + inact_threshold = inact_threshold * adxl345_range_factor_tbl[range_old]
> + / adxl345_range_factor_tbl[range];
> + inact_threshold = min(U8_MAX, max(1, inact_threshold));
> +
> + ret = regmap_write(st->regmap, adxl345_act_thresh_reg[ADXL345_ACTIVITY],
> + act_threshold);
> + if (ret)
> + return ret;
> +
> + return regmap_write(st->regmap, adxl345_act_thresh_reg[ADXL345_INACTIVITY],
> + inact_threshold);
> }
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2025-06-12 12:15 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-10 21:59 [PATCH v9 00/11] iio: accel: adxl345: add interrupt based sensor events Lothar Rubusch
2025-06-10 21:59 ` [PATCH v9 01/11] iio: accel: adxl345: apply scale factor to tap threshold Lothar Rubusch
2025-06-14 13:42 ` Jonathan Cameron
2025-06-15 22:20 ` Lothar Rubusch
2025-06-21 16:55 ` Jonathan Cameron
2025-06-21 18:14 ` Lothar Rubusch
2025-06-10 21:59 ` [PATCH v9 02/11] iio: accel: adxl345: make data struct variable irq function local Lothar Rubusch
2025-06-14 13:43 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 03/11] iio: accel: adxl345: simplify measure enable Lothar Rubusch
2025-06-14 13:45 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 04/11] iio: accel: adxl345: simplify interrupt mapping Lothar Rubusch
2025-06-11 15:20 ` Andy Shevchenko
2025-06-10 21:59 ` [PATCH v9 05/11] iio: accel: adxl345: simplify reading the FIFO Lothar Rubusch
2025-06-14 13:47 ` Jonathan Cameron
2025-06-15 22:14 ` Lothar Rubusch
2025-06-21 16:58 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 06/11] iio: accel: adxl345: replace magic numbers by unit expressions Lothar Rubusch
2025-06-14 13:49 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 07/11] iio: accel: adxl345: add activity event feature Lothar Rubusch
2025-06-12 11:51 ` Andy Shevchenko
2025-06-21 18:06 ` Lothar Rubusch
2025-06-21 19:55 ` Andy Shevchenko
2025-06-10 21:59 ` [PATCH v9 08/11] iio: accel: adxl345: add inactivity feature Lothar Rubusch
2025-06-12 12:15 ` Andy Shevchenko [this message]
2025-06-14 13:55 ` Jonathan Cameron
2025-06-14 19:02 ` Andy Shevchenko
2025-06-21 18:53 ` Lothar Rubusch
2025-06-21 19:24 ` Andy Shevchenko
2025-06-21 19:28 ` Andy Shevchenko
2025-06-14 14:00 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 09/11] iio: accel: adxl345: add coupling detection for activity/inactivity Lothar Rubusch
2025-06-14 14:04 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 10/11] iio: accel: adxl345: extend inactivity time for less than 1s Lothar Rubusch
2025-06-14 14:10 ` Jonathan Cameron
2025-06-10 21:59 ` [PATCH v9 11/11] docs: iio: add documentation for adxl345 driver Lothar Rubusch
2025-06-14 14:17 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aErE0xmlm4qBHg03@smile.fi.intel.com \
--to=andriy.shevchenko@intel.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=corbet@lwn.net \
--cc=dlechner@baylibre.com \
--cc=eraretuya@gmail.com \
--cc=jic23@kernel.org \
--cc=l.rubusch@gmail.com \
--cc=lars@metafoo.de \
--cc=linux-doc@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®