From: Jonathan Cameron <jic23@kernel.org>
To: Jagath Jog J <jagathjog1996@gmail.com>
Cc: andriy.shevchenko@linux.intel.com, lars@metafoo.de,
robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [RFC 2/2] iio: imu: Add driver for BMI323 IMU
Date: Sat, 30 Sep 2023 17:13:21 +0100 [thread overview]
Message-ID: <20230930171321.3afbada2@jic23-huawei> (raw)
In-Reply-To: <CAM+2EuJBxj7P-ymu84u308g8LCemSEsYi_TSHYtaK9PyrhqrfA@mail.gmail.com>
>
> > > +struct bmi323_data {
> > > + struct device *dev;
> > > + struct regmap *regmap;
> > > + struct iio_mount_matrix orientation;
> > > + enum bmi323_irq_pin irq_pin;
> > > + struct iio_trigger *trig;
> > > + bool drdy_trigger_enabled;
> > > + enum bmi323_state state;
> > > + s64 fifo_tstamp, old_fifo_tstamp;
> > > + u32 odrns[2];
> > > + u32 odrhz[2];
> > > + unsigned int feature_events;
> > > +
> > > + /*
> > > + * Lock to protect the members of device's private data from concurrent
> > > + * access and also to serialize the access of extended registers.
> > > + * See bmi323_write_ext_reg(..) for more info.
> > > + */
> > > + struct mutex mutex;
> > > + int watermark;
> > > + __le16 fifo_buff[BMI323_FIFO_FULL_IN_WORDS] __aligned(IIO_DMA_MINALIGN);
> > > + struct {
> > > + __le16 channels[6];
> > > + s64 ts __aligned(8);
> >
> > Hopefully Andy's aligned_s64 set will land soon and we can tidy this up.
> > I'm a bit unsure of this, but can you overlap some of these buffers or are
> > they used concurrently? (if they are then we have problems with DMA safety.)
> >
> > Perhaps an anonymous union is appropriate?
>
> Yes both buffers are used at the same time. In fifo_flush
> fifo_buff is used to store all fifo data, and buffer is
> used to push a single data frame to iio buffers, overlapping
> will corrupt the data, so I used separate buffers for both.
Ah. So the structure is used in 2 ways.
1. As a target for DMA, which means it should live in the cacheline we
are saving for that purpsoe.
2. As a place to build up data.
In general we should be careful with doing 2 as that could race with
DMA and end up with data corruption, however you only use it that
way in flush_fifo where both the DMA and this usage under under
the mutex. Hence I think you are fine.
>
> > > +static IIO_DEVICE_ATTR_RW(in_accel_gyro_averaging, 0);
> > > +static IIO_CONST_ATTR(in_accel_gyro_averaging_available, "2 4 8 16 32 64");
> > > +
> > > +static struct attribute *bmi323_attributes[] = {
> > > + &iio_dev_attr_in_accel_gyro_averaging.dev_attr.attr,
> > > + &iio_const_attr_in_accel_gyro_averaging_available.dev_attr.attr,
> >
> > So averaging often maps directly to oversampling. Kind of different names
> > for the same thing. Perhaps that standard ABI can be used?
> > It tends to make sampling frequency reporting need to take it into account
> > though as that drops as divided by oversampling ratio.
>
> Yes, oversampling can be used, but changing the average
> value doesn't alter the sampling frequency. The sampling
> frequency is same even with the increase in averaging value.
Ok. That's unusual so good to know.
> > > +static int bmi323_feature_engine_enable(struct bmi323_data *data, bool en)
> > > +{
> > > + unsigned int feature_status;
> > > + int ret, i;
> > > +
> > > + if (en) {
> > > + ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG,
> > > + 0x012c);
> > > + if (ret)
> > > + return ret;
> > > +
> > > + ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
> > > + BMI323_FEAT_IO_STATUS_MSK);
> > > + if (ret)
> > > + return ret;
> > > +
> > > + ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
> > > + BMI323_FEAT_ENG_EN_MSK);
> > > + if (ret)
> > > + return ret;
> > > +
> > > + i = 5;
> >
> > Why 5?
>
> No specific reason, during testing the feature engine was
> taking around 4 milliseconds, so I thought of checking
> every 2 milliseconds and max of 5 trials.
That's a good reason. Just add a comment to that say that.
>
> > > + * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
> > > + * Each SPI register read operation requires to read one dummy byte before
> > > + * the actual payload.
> > > + */
> > > +static int bmi323_regmap_spi_read(void *context, const void *reg_buf,
> > > + size_t reg_size, void *val_buf,
> > > + size_t val_size)
> > > +{
> > > + struct spi_device *spi = context;
> > > + u8 reg, *buff = NULL;
> > > + int ret;
> > > +
> > > + buff = kmalloc(val_size + BMI323_SPI_DUMMY, GFP_KERNEL);
> >
> > Hmm. Regmap has pad_bits (which can be multiple bytes) but this case
> > is unusual in that they only apply to reads.
> >
> > I wonder if we can make this cheaper though rather than having
> > to handle either some context or having dynamic allocations in here.
> >
> > How about making the write bigger? Does that have any effect?
> > Looks like don't care state in Figure 31. If that's the case,
> > send some zeros on that as it's known fixed size (2 bytes including
> > the padding) and then you can directly use the read buffer without
> > yet another memcpy.
>
> For spi with pad_bits=8 and without any custom read and
> write functions, regmap_read() works but regmap_write()
> does not. Write is also adding 8 bits of padding and
> the device is treating it as data.
> (7.2.3 SPI Protocol Figure 30)
Understood. I looked it up too before suggesting this local hack.
You'll still need a custom regmap, but at least this trick would
allow you to avoid allocating a buffer in the read function.
Jonathan
next prev parent reply other threads:[~2023-09-30 16:13 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-18 8:03 [RFC 0/2] iio: imu: Add driver and dt-bindings for BMI323 Jagath Jog J
2023-09-18 8:03 ` [RFC 1/2] dt-bindings: iio: imu: Add DT binding doc " Jagath Jog J
2023-09-18 12:25 ` Krzysztof Kozlowski
2023-09-19 16:44 ` Jagath Jog J
2023-09-24 13:31 ` Jonathan Cameron
2023-09-27 21:37 ` Jagath Jog J
2023-09-24 13:37 ` Jonathan Cameron
2023-09-27 21:37 ` Jagath Jog J
2023-09-30 16:05 ` Jonathan Cameron
2023-10-08 6:24 ` Jagath Jog J
2023-10-10 9:00 ` Jonathan Cameron
2023-10-10 9:06 ` Linus Walleij
2023-10-10 14:42 ` Jonathan Cameron
2023-10-10 19:51 ` Linus Walleij
2023-10-13 8:16 ` Jonathan Cameron
2023-10-13 16:23 ` Jagath Jog J
2023-09-18 8:03 ` [RFC 2/2] iio: imu: Add driver for BMI323 IMU Jagath Jog J
2023-09-18 10:03 ` Andy Shevchenko
2023-09-19 22:43 ` Jagath Jog J
2023-09-20 13:24 ` Andy Shevchenko
2023-10-08 6:25 ` Jagath Jog J
2023-10-10 9:02 ` Jonathan Cameron
2023-09-24 14:30 ` Jonathan Cameron
2023-09-27 19:59 ` Jagath Jog J
2023-09-27 21:25 ` Denis Benato
2023-09-29 7:59 ` Jagath Jog J
2023-09-30 16:17 ` Jonathan Cameron
2023-10-01 13:53 ` Denis Benato
2023-10-03 20:35 ` Jagath Jog J
2023-09-30 16:13 ` Jonathan Cameron [this message]
2023-09-27 9:57 ` Uwe Kleine-König
2023-09-27 12:35 ` Andy Shevchenko
2023-09-27 14:34 ` Uwe Kleine-König
2023-10-01 8:20 ` Andy Shevchenko
2023-09-28 18:19 ` Jagath Jog J
2023-09-28 20:48 ` Uwe Kleine-König
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=20230930171321.3afbada2@jic23-huawei \
--to=jic23@kernel.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=devicetree@vger.kernel.org \
--cc=jagathjog1996@gmail.com \
--cc=krzysztof.kozlowski+dt@linaro.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robh+dt@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).