Re: [PATCH v5 10/11] iio: accel: adxl345: add coupling detection for activity/inactivity
From: Jonathan Cameron
Date: Mon Mar 31 2025 - 06:52:17 EST
On Tue, 18 Mar 2025 23:08:42 +0000
Lothar Rubusch <l.rubusch@xxxxxxxxx> wrote:
> Add coupling activity/inactivity detection by the AC/DC bit. This is an
> addititional enhancement for the detection of activity states and
> completes the activity / inactivity feature of the ADXL345.
>
> Signed-off-by: Lothar Rubusch <l.rubusch@xxxxxxxxx>
> ---
> @@ -300,6 +319,69 @@ static int adxl345_set_measure_en(struct adxl345_state *st, bool en)
>
> /* act/inact */
>
> +static int adxl345_is_act_inact_ac(struct adxl345_state *st,
> + enum adxl345_activity_type type, bool *ac)
> +{
> + unsigned int regval;
> + int ret;
> +
> + ret = regmap_read(st->regmap, ADXL345_REG_ACT_INACT_CTRL, ®val);
> + if (ret)
> + return ret;
> +
> + if (type == ADXL345_ACTIVITY)
> + *ac = (FIELD_GET(ADXL345_REG_ACT_ACDC_MSK, regval) > 0);
> + else
> + *ac = (FIELD_GET(ADXL345_REG_INACT_ACDC_MSK, regval) > 0);
The > 0 doesn't add anything so I'd drop those.
Get used to non 0 integer == true for booleans takes some time but
is commonly assumed in kernel code (it is true in C in general but
some coding styles don't assume it!)
> +
> + return 0;
> +}
> static int adxl345_read_avail(struct iio_dev *indio_dev,
> @@ -919,6 +1050,8 @@ static int adxl345_read_event_config(struct iio_dev *indio_dev,
> {
> struct adxl345_state *st = iio_priv(indio_dev);
> bool int_en;
> + bool act_ac;
> + bool inact_ac;
Could stick those 3 bools on one line without loosing readability I think.
Save a tiny bit of scrolling ;)
or push them down to context where they are used.
> int ret;
>
> switch (type) {
> @@ -963,6 +1096,21 @@ static int adxl345_read_event_config(struct iio_dev *indio_dev,
> if (ret)
> return ret;
> return int_en;
> + case IIO_EV_TYPE_MAG_REFERENCED:
> + switch (dir) {
> + case IIO_EV_DIR_RISING:
> + ret = adxl345_is_act_inact_ac(st, ADXL345_ACTIVITY, &act_ac);
> + if (ret)
> + return ret;
> + return act_ac;
> + case IIO_EV_DIR_FALLING:
> + ret = adxl345_is_act_inact_ac(st, ADXL345_INACTIVITY, &inact_ac);
> + if (ret)
> + return ret;
> + return inact_ac;
> + default:
> + return -EINVAL;
> + }
> default:
> return -EINVAL;
> }
> @@ -999,6 +1147,16 @@ static int adxl345_write_event_config(struct iio_dev *indio_dev,
> }
> case IIO_EV_TYPE_MAG:
> return adxl345_set_ff_en(st, state);
> + case IIO_EV_TYPE_MAG_REFERENCED:
> + switch (dir) {
> + case IIO_EV_DIR_RISING:
> + return adxl345_set_act_inact_ac(st, ADXL345_ACTIVITY, state);
> + case IIO_EV_DIR_FALLING:
> + return adxl345_set_act_inact_ac(st, ADXL345_INACTIVITY, state);
> + default:
> + return -EINVAL;
> + }
> +
> default:
> return -EINVAL;
> }