From: Jonathan Cameron <jic23@kernel.org>
To: Gustavo Silva <gustavograzs@gmail.com>
Cc: "Alex Lanzano" <lanzano.alex@gmail.com>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/3] iio: imu: bmi270: add channel for step counter
Date: Sat, 26 Apr 2025 14:40:20 +0100 [thread overview]
Message-ID: <20250426144020.2633f9cb@jic23-huawei> (raw)
In-Reply-To: <20250424-bmi270-events-v1-1-a6c722673e5f@gmail.com>
On Thu, 24 Apr 2025 21:14:50 -0300
Gustavo Silva <gustavograzs@gmail.com> wrote:
> Add a channel for enabling/disabling the step counter, reading the
> number of steps and resetting the counter.
>
> Signed-off-by: Gustavo Silva <gustavograzs@gmail.com>
Hi Gustavo,
This is tripping over the somewhat theoretical requirement for
regmap to be provided with DMA safe buffers for bulk accesses.
Jonathan
> ---
> drivers/iio/imu/bmi270/bmi270_core.c | 127 +++++++++++++++++++++++++++++++++++
> 1 file changed, 127 insertions(+)
>
> diff --git a/drivers/iio/imu/bmi270/bmi270_core.c b/drivers/iio/imu/bmi270/bmi270_core.c
> index a86be5af5ccb1f010f2282ee31c47f284c1bcc86..f09d8dead9df63df5ae8550cf473b5573374955b 100644
> --- a/drivers/iio/imu/bmi270/bmi270_core.c
> +++ b/drivers/iio/imu/bmi270/bmi270_core.c
> @@ -31,6 +31,8 @@
> /* See datasheet section 4.6.14, Temperature Sensor */
> #define BMI270_TEMP_OFFSET 11776
> #define BMI270_TEMP_SCALE 1953125
> @@ -111,6 +118,7 @@ struct bmi270_data {
> struct iio_trigger *trig;
> /* Protect device's private data from concurrent access */
> struct mutex mutex;
> + int steps_enabled;
Seems a little odd to have a thing called _enabled as an integer.
Probably better as a bool even though that will require slightly more
code to handle read / write.
>
> /*
> * Where IIO_DMA_MINALIGN may be larger than 8 bytes, align to
> @@ -282,6 +290,99 @@ static const struct bmi270_odr_item bmi270_odr_table[] = {
> },
> };
>
> +enum bmi270_feature_reg_id {
> + BMI270_SC_26_REG,
> +};
> +
> +struct bmi270_feature_reg {
> + u8 page;
> + u8 addr;
> +};
> +
> +static const struct bmi270_feature_reg bmi270_feature_regs[] = {
> + [BMI270_SC_26_REG] = {
> + .page = 6,
> + .addr = 0x32,
> + },
> +};
> +
> +static int bmi270_write_feature_reg(struct bmi270_data *data,
> + enum bmi270_feature_reg_id id,
> + u16 val)
> +{
> + const struct bmi270_feature_reg *reg = &bmi270_feature_regs[id];
> + int ret;
> +
> + ret = regmap_write(data->regmap, BMI270_FEAT_PAGE_REG, reg->page);
> + if (ret)
> + return ret;
> +
> + return regmap_bulk_write(data->regmap, reg->addr, &val, sizeof(val));
For a regmap on top of an SPI bus. I think we are still requiring DMA safe
buffers until we can get confirmation that the API guarantees that isn't
needed. (there is another thread going on where we are trying to get that
confirmation).
That is a pain here because this can run concurrently with buffered
capture and as such I think we can't just put a additional element after
data->data but instead need to mark that new element __aligned(IIO_DMA_MINALIGN)
as well (and add a comment that it can be used concurrently with data->data).
This hole thing is a mess because in reality I think the regmap core is always
bouncing data today. In theory it could sometimes be avoiding copies
and the underlying regmap_spi does require DMA safe buffers. This all relies
on an old discussion where Mark Brown said that we should not assume any
different requirements from the the underlying bus.
> +}
> +
> +static int bmi270_read_feature_reg(struct bmi270_data *data,
> + enum bmi270_feature_reg_id id,
> + u16 *val)
> +{
> + const struct bmi270_feature_reg *reg = &bmi270_feature_regs[id];
> + int ret;
> +
> + ret = regmap_write(data->regmap, BMI270_FEAT_PAGE_REG, reg->page);
> + if (ret)
> + return ret;
> +
> + return regmap_bulk_read(data->regmap, reg->addr, val, sizeof(*val));
> +}
> +
> +static int bmi270_update_feature_reg(struct bmi270_data *data,
> + enum bmi270_feature_reg_id id,
> + u16 mask, u16 val)
> +{
> + u16 reg_val;
> + int ret;
> +
> + ret = bmi270_read_feature_reg(data, id, ®_val);
> + if (ret)
> + return ret;
> +
> + set_mask_bits(®_val, mask, val);
> +
> + return bmi270_write_feature_reg(data, id, reg_val);
> +}
> +
> +static int bmi270_read_steps(struct bmi270_data *data, int *val)
> +{
> + __le16 steps_count;
> + int ret;
> +
> + ret = regmap_bulk_read(data->regmap, BMI270_SC_OUT_0_REG, &steps_count,
> + sizeof(steps_count));
> + if (ret)
> + return ret;
> +
> + *val = sign_extend32(le16_to_cpu(steps_count), 15);
> + return IIO_VAL_INT;
> +}
> +
> static int bmi270_set_scale(struct bmi270_data *data, int chan_type, int uscale)
> {
> int i;
> @@ -551,6 +652,8 @@ static int bmi270_read_raw(struct iio_dev *indio_dev,
> struct bmi270_data *data = iio_priv(indio_dev);
>
> switch (mask) {
> + case IIO_CHAN_INFO_PROCESSED:
> + return bmi270_read_steps(data, val);
> case IIO_CHAN_INFO_RAW:
> if (!iio_device_claim_direct(indio_dev))
> return -EBUSY;
> @@ -571,6 +674,10 @@ static int bmi270_read_raw(struct iio_dev *indio_dev,
> case IIO_CHAN_INFO_SAMP_FREQ:
> ret = bmi270_get_odr(data, chan->type, val, val2);
> return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
> + case IIO_CHAN_INFO_ENABLE:
> + scoped_guard(mutex, &data->mutex)
> + *val = data->steps_enabled;
What race is this protecting against? Protecting the write is fine because it
is about ensuring we don't race an enable against a clear of the counter.
A race here would I think just give the same as either the race to take the lock
being won by this or not (so not a race as such, just ordering of calls)
So I don't think you need the lock here.
> + return IIO_VAL_INT;
next prev parent reply other threads:[~2025-04-26 13:40 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-25 0:14 [PATCH 0/3] BMI270: Add support for step counter and motion events Gustavo Silva
2025-04-25 0:14 ` [PATCH 1/3] iio: imu: bmi270: add channel for step counter Gustavo Silva
2025-04-25 4:28 ` Andy Shevchenko
2025-04-26 13:40 ` Jonathan Cameron [this message]
2025-04-27 0:19 ` Gustavo Silva
2025-05-05 13:13 ` Jonathan Cameron
2025-05-07 10:35 ` kernel test robot
2025-04-25 0:14 ` [PATCH 2/3] iio: imu: bmi270: add step counter watermark event Gustavo Silva
2025-04-25 4:33 ` Andy Shevchenko
2025-04-26 23:01 ` Gustavo Silva
2025-04-26 13:47 ` Jonathan Cameron
2025-04-27 0:57 ` Gustavo Silva
2025-05-05 13:21 ` Jonathan Cameron
2025-04-25 0:14 ` [PATCH 3/3] iio: imu: bmi270: add support for motion events Gustavo Silva
2025-04-25 5:25 ` Andy Shevchenko
2025-04-26 23:06 ` Gustavo Silva
2025-04-26 14:12 ` 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=20250426144020.2633f9cb@jic23-huawei \
--to=jic23@kernel.org \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=gustavograzs@gmail.com \
--cc=lanzano.alex@gmail.com \
--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