Re: [PATCH v2 09/12] iio: imu: inv_icm42600: add buffer support in iio devices

From: Jonathan Cameron
Date: Sat Jun 06 2020 - 10:42:39 EST


On Tue, 2 Jun 2020 12:57:26 +0000
Jean-Baptiste Maneyrol <JManeyrol@xxxxxxxxxxxxxx> wrote:

> Hi Jonathan,
>
> I agree that this multiplexed watermark computation value is anything except simple and clear to understand.
> I will add more documentation about it. And it also triggered a
> kbuild robot issue, because it is using 64 bits modulo without using
> math64 macros.
>
> For buffer preenable/postenable/..., the sequence I am using
> currently is:
> - preenable -> turn chip on (pm_runtime_get)
> - update_scan_mode -> set FIFO en bits configuration (which sensor
> data is going into the fifo)
> - hwfifo_watermark -> compute and set watermark value
> - postenable -> turn FIFO on (and multiplexed with a FIFO on counter
> since used by accel & gyro)
> - predisable -> turn FIFO off (multiplexed with counter)
> - postdisable -> turn chip off (pm_runtime_put)
>
> This setting is working well. Good to note that if there is an error
> when enabling the buffer, postdisable will always get called after
> preenable. So it ensures pm_runtime reference counter to be always OK.
>
> Another way would be to only store configuration in internal state
> with update_scan_mode and hwfifo_watermark, and do everything in
> postenable/predisable. This is a possibility, but makes things a
> little more complex.

What you have here is fine. Thanks for the explanation.

>
> For hwfifo flush, this is an interesting feature when there is a need
> to have data immediately. Or when there is a need to do a clean
> change of configuration. In Android systems, Android framework is
> mainly using FIFO flush to change the sensor configuration (ODR,
> watermark) in a clean way. For our case with the FIFO interleaved
> this is a not an issue. If there are samples from the 2 sensors, it
> means the 2 buffers are enabled. And if data is coming to the iio
> buffer sooner than expected, that should not be a problem. The
> limitation I see when the 2 sensors are runnings, is that we will
> return less data than should have been possible. I limit FIFO reading
> to the provided n bytes, so we could read less than n samples of 1
> sensor.

Yes. I'm a little nervous about unexpected impacts of this last
case. As you say early data rarely matters but late data can
potentially be an issue as software will assume it didn't get the
data because it didn't yet exist.

>
> Something I have in mind, that would be really interesting to be able
> to set/change watermark value when the buffer is enabled. Otherwise,
> we are always loosing events by turning sensor off when we want to
> change the value. Is there any limitation to work this way, or should
> it be possible to implement this feature in the future ?

I can't see why it would particularly matter in general if we did this
whilst 'live'. However, potentially we have sensors where reducing the
value could result in not getting an interrupt from a hw fifo.
(unwise design perhaps but we all know those exist :)

So care would be needed - perhaps some kind of opt in?

>
> Thanks,
> JB

Please remember to wrap our email if possible.

Thanks,

Jonathan



>
>
> From: Jonathan Cameron <jic23@xxxxxxxxxx>
> Sent: Sunday, May 31, 2020 14:56
> To: Jean-Baptiste Maneyrol <JManeyrol@xxxxxxxxxxxxxx>
> Cc: robh+dt@xxxxxxxxxx <robh+dt@xxxxxxxxxx>; robh@xxxxxxxxxx <robh@xxxxxxxxxx>; mchehab+huawei@xxxxxxxxxx <mchehab+huawei@xxxxxxxxxx>; davem@xxxxxxxxxxxxx <davem@xxxxxxxxxxxxx>; gregkh@xxxxxxxxxxxxxxxxxxx <gregkh@xxxxxxxxxxxxxxxxxxx>; linux-iio@xxxxxxxxxxxxxxx <linux-iio@xxxxxxxxxxxxxxx>; devicetree@xxxxxxxxxxxxxxx <devicetree@xxxxxxxxxxxxxxx>; linux-kernel@xxxxxxxxxxxxxxx <linux-kernel@xxxxxxxxxxxxxxx>
> Subject: Re: [PATCH v2 09/12] iio: imu: inv_icm42600: add buffer support in iio devices
> Â
> ÂCAUTION: This email originated from outside of the organization. Please make sure the sender is who they say they are and do not click links or open attachments unless you recognize the sender and know the content is safe.
>
> On Wed, 27 May 2020 20:57:08 +0200
> Jean-Baptiste Maneyrol <jmaneyrol@xxxxxxxxxxxxxx> wrote:
>
> > Add all FIFO parsing and reading functions. Add accel and gyro
> > kfifo buffer and FIFO data parsing. Use device interrupt for
> > reading data FIFO and launching accel and gyro parsing.
> >
> > Support hwfifo watermark by multiplexing gyro and accel settings.
> > Support hwfifo flush.
>
> Both of these are complex given the interactions of the two sensors
> types and to be honest I couldn't figure out exactly what the intent was.
> Needs more docs!
>
> Thanks,
>
> Jonathan
>
> >
> > Signed-off-by: Jean-Baptiste Maneyrol <jmaneyrol@xxxxxxxxxxxxxx>
> > ---
> >Â drivers/iio/imu/inv_icm42600/KconfigÂÂÂÂÂÂÂÂÂ |ÂÂ 1 +
> >Â drivers/iio/imu/inv_icm42600/MakefileÂÂÂÂÂÂÂÂ |ÂÂ 1 +
> >Â drivers/iio/imu/inv_icm42600/inv_icm42600.hÂÂ |ÂÂ 8 +
> >Â .../iio/imu/inv_icm42600/inv_icm42600_accel.c | 160 ++++-
> >Â .../imu/inv_icm42600/inv_icm42600_buffer.cÂÂÂ | 555 ++++++++++++++++++
> >Â .../imu/inv_icm42600/inv_icm42600_buffer.hÂÂÂ |Â 98 ++++
> > .../iio/imu/inv_icm42600/inv_icm42600_core.c | 30 +
> > .../iio/imu/inv_icm42600/inv_icm42600_gyro.c | 160 ++++-
> >Â 8 files changed, 1011 insertions(+), 2 deletions(-)
> >Â create mode 100644 drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
> >Â create mode 100644 drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.h
> >
> > diff --git a/drivers/iio/imu/inv_icm42600/Kconfig b/drivers/iio/imu/inv_icm42600/Kconfig
> > index 22390a72f0a3..50cbcfcb6cf1 100644
> > --- a/drivers/iio/imu/inv_icm42600/Kconfig
> > +++ b/drivers/iio/imu/inv_icm42600/Kconfig
> > @@ -2,6 +2,7 @@
>
> >Â config INV_ICM42600
> >ÂÂÂÂÂÂÂ tristate
> > +ÂÂÂÂ select IIO_BUFFER
>
> >Â config INV_ICM42600_I2C
> >ÂÂÂÂÂÂÂ tristate "InvenSense ICM-426xx I2C driver"
> > diff --git a/drivers/iio/imu/inv_icm42600/Makefile b/drivers/iio/imu/inv_icm42600/Makefile
> > index 48965824f00c..0f49f6df3647 100644
> > --- a/drivers/iio/imu/inv_icm42600/Makefile
> > +++ b/drivers/iio/imu/inv_icm42600/Makefile
> > @@ -5,6 +5,7 @@ inv-icm42600-y += inv_icm42600_core.o
> >Â inv-icm42600-y += inv_icm42600_gyro.o
> >Â inv-icm42600-y += inv_icm42600_accel.o
> >Â inv-icm42600-y += inv_icm42600_temp.o
> > +inv-icm42600-y += inv_icm42600_buffer.o
>
> >Â obj-$(CONFIG_INV_ICM42600_I2C) += inv-icm42600-i2c.o
> >Â inv-icm42600-i2c-y += inv_icm42600_i2c.o
> > diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600.h b/drivers/iio/imu/inv_icm42600/inv_icm42600.h
> > index 43749f56426c..4d5811562a61 100644
> > --- a/drivers/iio/imu/inv_icm42600/inv_icm42600.h
> > +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600.h
> > @@ -14,6 +14,8 @@
> >Â #include <linux/pm.h>
> >Â #include <linux/iio/iio.h>
>
> > +#include "inv_icm42600_buffer.h"
> > +
> >Â enum inv_icm42600_chip {
> >ÂÂÂÂÂÂÂ INV_CHIP_ICM42600,
> >ÂÂÂÂÂÂÂ INV_CHIP_ICM42602,
> > @@ -123,6 +125,7 @@ struct inv_icm42600_suspended {
> >ÂÂ *Â @indio_gyro:ÂÂÂÂ gyroscope IIO device.
> >ÂÂ *Â @indio_accel:ÂÂÂ accelerometer IIO device.
> >ÂÂ *Â @buffer:ÂÂÂÂÂÂÂÂ data transfer buffer aligned for DMA.
> > + *Â @fifo:ÂÂÂÂÂÂÂÂÂÂ FIFO management structure.
> >ÂÂ */
> >Â struct inv_icm42600_state {
> >ÂÂÂÂÂÂÂ struct mutex lock;
> > @@ -137,6 +140,7 @@ struct inv_icm42600_state {
> >ÂÂÂÂÂÂÂ struct iio_dev *indio_gyro;
> >ÂÂÂÂÂÂÂ struct iio_dev *indio_accel;
> >ÂÂÂÂÂÂÂ uint8_t buffer[2] ____cacheline_aligned;
> > +ÂÂÂÂ struct inv_icm42600_fifo fifo;
> >Â };
>
> >Â /* Virtual register addresses: @bank on MSB (4 upper bits), @address on LSB */
> > @@ -377,6 +381,10 @@ int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
>
> >Â int inv_icm42600_gyro_init(struct inv_icm42600_state *st);
>
> > +int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev);
> > +
> >Â int inv_icm42600_accel_init(struct inv_icm42600_state *st);
>
> > +int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev);
> > +
> >Â #endif
> > diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c
> > index 6a615d7ffb24..c73ce204efc6 100644
> > --- a/drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c
> > +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_accel.c
> > @@ -11,9 +11,12 @@
> >Â #include <linux/delay.h>
> >Â #include <linux/math64.h>
> >Â #include <linux/iio/iio.h>
> > +#include <linux/iio/buffer.h>
> > +#include <linux/iio/kfifo_buf.h>
>
> >Â #include "inv_icm42600.h"
> >Â #include "inv_icm42600_temp.h"
> > +#include "inv_icm42600_buffer.h"
>
> >Â #define INV_ICM42600_ACCEL_CHAN(_modifier, _index, _ext_info)ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> >ÂÂÂÂÂÂÂ {ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > @@ -64,6 +67,76 @@ static const struct iio_chan_spec inv_icm42600_accel_channels[] = {
> >ÂÂÂÂÂÂÂ INV_ICM42600_TEMP_CHAN(INV_ICM42600_ACCEL_SCAN_TEMP),
> >Â };
>
> > +/* IIO buffer data: 8 bytes */
> > +struct inv_icm42600_accel_buffer {
> > +ÂÂÂÂ struct inv_icm42600_fifo_sensor_data accel;
> > +ÂÂÂÂ int8_t temp;
> > +ÂÂÂÂ uint8_t padding;
> > +};
> > +
> > +#define INV_ICM42600_SCAN_MASK_ACCEL_3AXISÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > +ÂÂÂÂ (BIT(INV_ICM42600_ACCEL_SCAN_X) |ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > +ÂÂÂÂ BIT(INV_ICM42600_ACCEL_SCAN_Y) |ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > +ÂÂÂÂ BIT(INV_ICM42600_ACCEL_SCAN_Z))
> > +
> > +#define INV_ICM42600_SCAN_MASK_TEMPÂ BIT(INV_ICM42600_ACCEL_SCAN_TEMP)
> > +
> > +static const unsigned long inv_icm42600_accel_scan_masks[] = {
> > +ÂÂÂÂ /* 3-axis accel + temperature */
> > +ÂÂÂÂ INV_ICM42600_SCAN_MASK_ACCEL_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
> > +ÂÂÂÂ 0,
> > +};
> > +
> > +/* enable accelerometer sensor and FIFO write */
> > +static int inv_icm42600_accel_update_scan_mode(struct iio_dev *indio_dev,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const unsigned long *scan_mask)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
> > +ÂÂÂÂ unsigned int fifo_en = 0;
> > +ÂÂÂÂ unsigned int sleep_temp = 0;
> > +ÂÂÂÂ unsigned int sleep_accel = 0;
> > +ÂÂÂÂ unsigned int sleep;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* enable temp sensor */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ fifo_en |= INV_ICM42600_SENSOR_TEMP;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ if (*scan_mask & INV_ICM42600_SCAN_MASK_ACCEL_3AXIS) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* enable accel sensor */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_accel_conf(st, &conf, &sleep_accel);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ fifo_en |= INV_ICM42600_SENSOR_ACCEL;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* update data FIFO write */
> > +ÂÂÂÂ ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ ret = inv_icm42600_buffer_update_watermark(st);
> > +
> > +out_unlock:
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +ÂÂÂÂ /* sleep maximum required time */
> > +ÂÂÂÂ if (sleep_accel > sleep_temp)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sleep = sleep_accel;
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sleep = sleep_temp;
> > +ÂÂÂÂ if (sleep)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ msleep(sleep);
> > +ÂÂÂÂ return ret;
> > +}
> > +
> >Â static int inv_icm42600_accel_read_sensor(struct inv_icm42600_state *st,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct iio_chan_spec const *chan,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int16_t *val)
> > @@ -248,7 +321,12 @@ static int inv_icm42600_accel_write_odr(struct inv_icm42600_state *st,
> >ÂÂÂÂÂÂÂ mutex_lock(&st->lock);
>
> >ÂÂÂÂÂÂÂ ret = inv_icm42600_set_accel_conf(st, &conf, NULL);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂ inv_icm42600_buffer_update_fifo_period(st);
> > +ÂÂÂÂ inv_icm42600_buffer_update_watermark(st);
>
> > +out_unlock:
> >ÂÂÂÂÂÂÂ mutex_unlock(&st->lock);
> >ÂÂÂÂÂÂÂ pm_runtime_mark_last_busy(dev);
> >ÂÂÂÂÂÂÂ pm_runtime_put_autosuspend(dev);
> > @@ -563,12 +641,51 @@ static int inv_icm42600_accel_write_raw_get_fmt(struct iio_dev *indio_dev,
> >ÂÂÂÂÂÂÂ }
> >Â }
>
> > +static int inv_icm42600_accel_hwfifo_set_watermark(struct iio_dev *indio_dev,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int val)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ st->fifo.watermark.accel = val;
> > +ÂÂÂÂ ret = inv_icm42600_buffer_update_watermark(st);
> > +
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +
> > +ÂÂÂÂ return ret;
> > +}
> > +
> > +static int inv_icm42600_accel_hwfifo_flush(struct iio_dev *indio_dev,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int count)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ if (count == 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ ret = inv_icm42600_buffer_hwfifo_flush(st, count);
> > +ÂÂÂÂ if (!ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = st->fifo.nb.accel;
> > +
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +
> > +ÂÂÂÂ return ret;
> > +}
> > +
> >Â static const struct iio_info inv_icm42600_accel_info = {
> >ÂÂÂÂÂÂÂ .read_raw = inv_icm42600_accel_read_raw,
> >ÂÂÂÂÂÂÂ .read_avail = inv_icm42600_accel_read_avail,
> >ÂÂÂÂÂÂÂ .write_raw = inv_icm42600_accel_write_raw,
> >ÂÂÂÂÂÂÂ .write_raw_get_fmt = inv_icm42600_accel_write_raw_get_fmt,
> >ÂÂÂÂÂÂÂ .debugfs_reg_access = inv_icm42600_debugfs_reg,
> > +ÂÂÂÂ .update_scan_mode = inv_icm42600_accel_update_scan_mode,
> > +ÂÂÂÂ .hwfifo_set_watermark = inv_icm42600_accel_hwfifo_set_watermark,
> > +ÂÂÂÂ .hwfifo_flush_to_buffer = inv_icm42600_accel_hwfifo_flush,
> >Â };
>
> >Â int inv_icm42600_accel_init(struct inv_icm42600_state *st)
> > @@ -576,6 +693,7 @@ int inv_icm42600_accel_init(struct inv_icm42600_state *st)
> >ÂÂÂÂÂÂÂ struct device *dev = regmap_get_device(st->map);
> >ÂÂÂÂÂÂÂ const char *name;
> >ÂÂÂÂÂÂÂ struct iio_dev *indio_dev;
> > +ÂÂÂÂ struct iio_buffer *buffer;
>
> >ÂÂÂÂÂÂÂ name = devm_kasprintf(dev, GFP_KERNEL, "%s-accel", st->name);
> >ÂÂÂÂÂÂÂ if (!name)
> > @@ -585,14 +703,54 @@ int inv_icm42600_accel_init(struct inv_icm42600_state *st)
> >ÂÂÂÂÂÂÂ if (!indio_dev)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
>
> > +ÂÂÂÂ buffer = devm_iio_kfifo_allocate(dev);
> > +ÂÂÂÂ if (!buffer)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
> > +
> >ÂÂÂÂÂÂÂ iio_device_set_drvdata(indio_dev, st);
> >ÂÂÂÂÂÂÂ indio_dev->dev.parent = dev;
> >ÂÂÂÂÂÂÂ indio_dev->name = name;
> >ÂÂÂÂÂÂÂ indio_dev->info = &inv_icm42600_accel_info;
> > -ÂÂÂÂ indio_dev->modes = INDIO_DIRECT_MODE;
> > +ÂÂÂÂ indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
> >ÂÂÂÂÂÂÂ indio_dev->channels = inv_icm42600_accel_channels;
> >ÂÂÂÂÂÂÂ indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_accel_channels);
> > +ÂÂÂÂ indio_dev->available_scan_masks = inv_icm42600_accel_scan_masks;
> > +ÂÂÂÂ indio_dev->setup_ops = &inv_icm42600_buffer_ops;
> > +
> > +ÂÂÂÂ iio_device_attach_buffer(indio_dev, buffer);
>
> >ÂÂÂÂÂÂÂ st->indio_accel = indio_dev;
> >ÂÂÂÂÂÂÂ return devm_iio_device_register(dev, st->indio_accel);
> >Â }
> > +
> > +int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ ssize_t i, size;
> > +ÂÂÂÂ const void *accel, *gyro, *timestamp;
> > +ÂÂÂÂ const int8_t *temp;
> > +ÂÂÂÂ unsigned int odr;
> > +ÂÂÂÂ struct inv_icm42600_accel_buffer buffer = {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ .padding = 0,
> > +ÂÂÂÂ };
> > +
> > +ÂÂÂÂ /* parse all fifo packets */
> > +ÂÂÂÂ for (i = 0; i < st->fifo.count; i += size) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &accel, &gyro, &temp, &timestamp, &odr);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* quit if error or FIFO is empty */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (size <= 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return size;
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* skip packet if no accel data or data is invalid */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (accel == NULL || !inv_icm42600_fifo_is_data_valid(accel))
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* fill and push data buffer */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ memcpy(&buffer.accel, accel, sizeof(buffer.accel));
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ buffer.temp = temp ? *temp : 0;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ iio_push_to_buffers(indio_dev, &buffer);
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
> > new file mode 100644
> > index 000000000000..c91075f62231
> > --- /dev/null
> > +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.c
> > @@ -0,0 +1,555 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*
> > + * Copyright (C) 2020 Invensense, Inc.
> > + */
> > +
> > +#include <linux/kernel.h>
> > +#include <linux/device.h>
> > +#include <linux/mutex.h>
> > +#include <linux/pm_runtime.h>
> > +#include <linux/regmap.h>
> > +#include <linux/delay.h>
> > +#include <linux/math64.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/iio/buffer.h>
> > +
> > +#include "inv_icm42600.h"
> > +#include "inv_icm42600_buffer.h"
> > +
> > +/* FIFO header: 1 byte */
> > +#define INV_ICM42600_FIFO_HEADER_MSGÂÂÂÂÂÂÂÂ BIT(7)
> > +#define INV_ICM42600_FIFO_HEADER_ACCELÂÂÂÂÂÂÂÂÂÂÂÂÂÂ BIT(6)
> > +#define INV_ICM42600_FIFO_HEADER_GYROÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ BIT(5)
> > +#define INV_ICM42600_FIFO_HEADER_TMST_FSYNCÂ GENMASK(3, 2)
> > +#define INV_ICM42600_FIFO_HEADER_ODR_ACCELÂÂ BIT(1)
> > +#define INV_ICM42600_FIFO_HEADER_ODR_GYROÂÂÂ BIT(0)
> > +
> > +struct inv_icm42600_fifo_1sensor_packet {
> > +ÂÂÂÂ uint8_t header;
> > +ÂÂÂÂ struct inv_icm42600_fifo_sensor_data data;
> > +ÂÂÂÂ int8_t temp;
> > +} __packed;
> > +#define INV_ICM42600_FIFO_1SENSOR_PACKET_SIZEÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 8
> > +
> > +struct inv_icm42600_fifo_2sensors_packet {
> > +ÂÂÂÂ uint8_t header;
> > +ÂÂÂÂ struct inv_icm42600_fifo_sensor_data accel;
> > +ÂÂÂÂ struct inv_icm42600_fifo_sensor_data gyro;
> > +ÂÂÂÂ int8_t temp;
> > +ÂÂÂÂ __be16 timestamp;
> > +} __packed;
> > +#define INV_ICM42600_FIFO_2SENSORS_PACKET_SIZEÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 16
> > +
> > +ssize_t inv_icm42600_fifo_decode_packet(const void *packet, const void **accel,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const void **gyro, const int8_t **temp,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const void **timestamp, unsigned int *odr)
> > +{
> > +ÂÂÂÂ const struct inv_icm42600_fifo_1sensor_packet *pack1 = packet;
> > +ÂÂÂÂ const struct inv_icm42600_fifo_2sensors_packet *pack2 = packet;
> > +ÂÂÂÂ uint8_t header = *((const uint8_t *)packet);
> > +
> > +ÂÂÂÂ /* FIFO empty */
> > +ÂÂÂÂ if (header & INV_ICM42600_FIFO_HEADER_MSG) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *accel = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *gyro = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *temp = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *timestamp = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *odr = 0;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* handle odr flags */
> > +ÂÂÂÂ *odr = 0;
> > +ÂÂÂÂ if (header & INV_ICM42600_FIFO_HEADER_ODR_GYRO)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *odr |= INV_ICM42600_SENSOR_GYRO;
> > +ÂÂÂÂ if (header & INV_ICM42600_FIFO_HEADER_ODR_ACCEL)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *odr |= INV_ICM42600_SENSOR_ACCEL;
> > +
> > +ÂÂÂÂ /* accel + gyro */
> > +ÂÂÂÂ if ((header & INV_ICM42600_FIFO_HEADER_ACCEL) &&
> > +ÂÂÂÂÂÂÂÂ (header & INV_ICM42600_FIFO_HEADER_GYRO)) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *accel = &pack2->accel;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *gyro = &pack2->gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *temp = &pack2->temp;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *timestamp = &pack2->timestamp;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return INV_ICM42600_FIFO_2SENSORS_PACKET_SIZE;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* accel only */
> > +ÂÂÂÂ if (header & INV_ICM42600_FIFO_HEADER_ACCEL) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *accel = &pack1->data;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *gyro = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *temp = &pack1->temp;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *timestamp = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* gyro only */
> > +ÂÂÂÂ if (header & INV_ICM42600_FIFO_HEADER_GYRO) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *accel = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *gyro = &pack1->data;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *temp = &pack1->temp;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ *timestamp = NULL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* invalid packet if here */
> > +ÂÂÂÂ return -EINVAL;
> > +}
> > +
> > +void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st)
> > +{
> > +ÂÂÂÂ uint32_t period_gyro, period_accel, period;
> > +
> > +ÂÂÂÂ if (st->fifo.en & INV_ICM42600_SENSOR_GYRO)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ period_gyro = inv_icm42600_odr_to_period(st->conf.gyro.odr);
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ period_gyro = U32_MAX;
> > +
> > +ÂÂÂÂ if (st->fifo.en & INV_ICM42600_SENSOR_ACCEL)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ period_accel = inv_icm42600_odr_to_period(st->conf.accel.odr);
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ period_accel = U32_MAX;
> > +
> > +ÂÂÂÂ if (period_gyro <= period_accel)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ period = period_gyro;
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ period = period_accel;
> > +
> > +ÂÂÂÂ st->fifo.period = period;
> > +}
> > +
> > +int inv_icm42600_buffer_set_fifo_en(struct inv_icm42600_state *st,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int fifo_en)
> > +{
> > +ÂÂÂÂ unsigned int mask, val;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ /* update only FIFO EN bits */
> > +ÂÂÂÂ mask = INV_ICM42600_FIFO_CONFIG1_TMST_FSYNC_EN |
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG1_TEMP_EN |
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG1_GYRO_EN |
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG1_ACCEL_EN;
> > +
> > +ÂÂÂÂ val = 0;
> > +ÂÂÂÂ if (fifo_en & INV_ICM42600_SENSOR_GYRO)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ val |= INV_ICM42600_FIFO_CONFIG1_GYRO_EN;
> > +ÂÂÂÂ if (fifo_en & INV_ICM42600_SENSOR_ACCEL)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ val |= INV_ICM42600_FIFO_CONFIG1_ACCEL_EN;
> > +ÂÂÂÂ if (fifo_en & INV_ICM42600_SENSOR_TEMP)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ val |= INV_ICM42600_FIFO_CONFIG1_TEMP_EN;
> > +
> > +ÂÂÂÂ ret = regmap_update_bits(st->map, INV_ICM42600_REG_FIFO_CONFIG1,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ mask, val);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +
> > +ÂÂÂÂ st->fifo.en = fifo_en;
> > +ÂÂÂÂ inv_icm42600_buffer_update_fifo_period(st);
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > +
> > +static size_t inv_icm42600_get_packet_size(unsigned int fifo_en)
> > +{
> > +ÂÂÂÂ size_t packet_size;
> > +
> > +ÂÂÂÂ if ((fifo_en & INV_ICM42600_SENSOR_GYRO) &&
> > +ÂÂÂÂÂÂÂÂ (fifo_en & INV_ICM42600_SENSOR_ACCEL))
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ packet_size = INV_ICM42600_FIFO_2SENSORS_PACKET_SIZE;
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ packet_size = INV_ICM42600_FIFO_1SENSOR_PACKET_SIZE;
> > +
> > +ÂÂÂÂ return packet_size;
> > +}
> > +
> > +static unsigned int inv_icm42600_wm_truncate(unsigned int watermark,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ size_t packet_size)
> > +{
> > +ÂÂÂÂ size_t wm_size;
> > +ÂÂÂÂ unsigned int wm;
> > +
> > +ÂÂÂÂ wm_size = watermark * packet_size;
> > +ÂÂÂÂ if (wm_size > INV_ICM42600_FIFO_WATERMARK_MAX)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ wm_size = INV_ICM42600_FIFO_WATERMARK_MAX;
> > +
> > +ÂÂÂÂ wm = wm_size / packet_size;
> > +
> > +ÂÂÂÂ return wm;
> > +}
> > +
>
> I think some overview docs on how this is working would be good.
> Set out the aims for the watermark selected and how it is achieved.
>
> > +int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st)
> > +{
> > +ÂÂÂÂ size_t packet_size, wm_size;
> > +ÂÂÂÂ unsigned int wm_gyro, wm_accel, watermark;
> > +ÂÂÂÂ uint32_t period_gyro, period_accel, period;
> > +ÂÂÂÂ int64_t latency_gyro, latency_accel, latency;
> > +ÂÂÂÂ bool restore;
> > +ÂÂÂÂ __le16 raw_wm;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ packet_size = inv_icm42600_get_packet_size(st->fifo.en);
> > +
> > +ÂÂÂÂ /* get minimal latency, depending on sensor watermark and odr */
> > +ÂÂÂÂ wm_gyro = inv_icm42600_wm_truncate(st->fifo.watermark.gyro,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ packet_size);
> > +ÂÂÂÂ wm_accel = inv_icm42600_wm_truncate(st->fifo.watermark.accel,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ packet_size);
> > +ÂÂÂÂ period_gyro = inv_icm42600_odr_to_period(st->conf.gyro.odr);
> > +ÂÂÂÂ period_accel = inv_icm42600_odr_to_period(st->conf.accel.odr);
> > +ÂÂÂÂ latency_gyro = (int64_t)period_gyro * (int64_t)wm_gyro;
> > +ÂÂÂÂ latency_accel = (int64_t)period_accel * (int64_t)wm_accel;
> > +ÂÂÂÂ if (latency_gyro == 0) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ latency = latency_accel;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ watermark = wm_accel;
> > +ÂÂÂÂ } else if (latency_accel == 0) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ latency = latency_gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ watermark = wm_gyro;
> > +ÂÂÂÂ } else {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* compute the smallest latency that is a multiple of both */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (latency_gyro <= latency_accel) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ latency = latency_gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ latency -= latency_accel % latency_gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ } else {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ latency = latency_accel;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ latency -= latency_gyro % latency_accel;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ }
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* use the shortest period */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (period_gyro <= period_accel)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ period = period_gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ period = period_accel;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* all this works because periods are multiple of each others */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ watermark = div_s64(latency, period);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (watermark < 1)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ watermark = 1;
> > +ÂÂÂÂ }
> > +ÂÂÂÂ wm_size = watermark * packet_size;
> > +
> > +ÂÂÂÂ /* changing FIFO watermark requires to turn off watermark interrupt */
> > +ÂÂÂÂ ret = regmap_update_bits_check(st->map, INV_ICM42600_REG_INT_SOURCE0,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 0, &restore);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +
> > +ÂÂÂÂ raw_wm = INV_ICM42600_FIFO_WATERMARK_VAL(wm_size);
> > +ÂÂÂÂ memcpy(st->buffer, &raw_wm, sizeof(raw_wm));
> > +ÂÂÂÂ ret = regmap_bulk_write(st->map, INV_ICM42600_REG_FIFO_WATERMARK,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ st->buffer, sizeof(raw_wm));
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +
> > +ÂÂÂÂ /* restore watermark interrupt */
> > +ÂÂÂÂ if (restore) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = regmap_update_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > +
> > +static int inv_icm42600_buffer_preenable(struct iio_dev *indio_dev)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ struct device *dev = regmap_get_device(st->map);
> > +
> > +ÂÂÂÂ pm_runtime_get_sync(dev);
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > +
> > +/*
> > + * update_scan_mode callback is turning sensors on and setting data FIFO enable
> > + * bits.
> > + */
> > +static int inv_icm42600_buffer_postenable(struct iio_dev *indio_dev)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ /* exit if FIFO is already on */
> > +ÂÂÂÂ if (st->fifo.on) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = 0;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_on;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* set FIFO threshold interrupt */
> > +ÂÂÂÂ ret = regmap_update_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ /* flush FIFO data */
> > +ÂÂÂÂ ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ /* set FIFO in streaming mode */
> > +ÂÂÂÂ ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG_STREAM);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ /* workaround: first read of FIFO count after reset is always 0 */
> > +ÂÂÂÂ ret = regmap_bulk_read(st->map, INV_ICM42600_REG_FIFO_COUNT, st->buffer, 2);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +out_on:
> > +ÂÂÂÂ /* increase FIFO on counter */
> > +ÂÂÂÂ st->fifo.on++;
> > +out_unlock:
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +ÂÂÂÂ return ret;
> > +}
> > +
> > +static int inv_icm42600_buffer_predisable(struct iio_dev *indio_dev)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ /* exit if there are several sensors using the FIFO */
> > +ÂÂÂÂ if (st->fifo.on > 1) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = 0;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_off;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* set FIFO in bypass mode */
> > +ÂÂÂÂ ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG_BYPASS);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ /* flush FIFO data */
> > +ÂÂÂÂ ret = regmap_write(st->map, INV_ICM42600_REG_SIGNAL_PATH_RESET,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ /* disable FIFO threshold interrupt */
> > +ÂÂÂÂ ret = regmap_update_bits(st->map, INV_ICM42600_REG_INT_SOURCE0,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN, 0);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +out_off:
> > +ÂÂÂÂ /* decrease FIFO on counter */
> > +ÂÂÂÂ st->fifo.on--;
> > +out_unlock:
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +ÂÂÂÂ return ret;
> > +}
> > +
> > +static int inv_icm42600_buffer_postdisable(struct iio_dev *indio_dev)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ struct device *dev = regmap_get_device(st->map);
> > +ÂÂÂÂ unsigned int sensor;
> > +ÂÂÂÂ unsigned int *watermark;
> > +ÂÂÂÂ struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
> > +ÂÂÂÂ unsigned int sleep_temp = 0;
> > +ÂÂÂÂ unsigned int sleep_sensor = 0;
> > +ÂÂÂÂ unsigned int sleep;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ if (indio_dev == st->indio_gyro) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sensor = INV_ICM42600_SENSOR_GYRO;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ watermark = &st->fifo.watermark.gyro;
> > +ÂÂÂÂ } else if (indio_dev == st->indio_accel) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sensor = INV_ICM42600_SENSOR_ACCEL;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ watermark = &st->fifo.watermark.accel;
> > +ÂÂÂÂ } else {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return -EINVAL;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ ret = inv_icm42600_buffer_set_fifo_en(st, st->fifo.en & ~sensor);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ *watermark = 0;
> > +ÂÂÂÂ ret = inv_icm42600_buffer_update_watermark(st);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ conf.mode = INV_ICM42600_SENSOR_MODE_OFF;
> > +ÂÂÂÂ if (sensor == INV_ICM42600_SENSOR_GYRO)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_sensor);
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_accel_conf(st, &conf, &sleep_sensor);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ /* if FIFO is off, turn temperature off */
> > +ÂÂÂÂ if (!st->fifo.on)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_temp_conf(st, false, &sleep_temp);
> > +
> > +out_unlock:
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +
> > +ÂÂÂÂ /* sleep maximum required time */
> > +ÂÂÂÂ if (sleep_sensor > sleep_temp)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sleep = sleep_sensor;
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sleep = sleep_temp;
> > +ÂÂÂÂ if (sleep)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ msleep(sleep);
> > +
> > +ÂÂÂÂ pm_runtime_mark_last_busy(dev);
> > +ÂÂÂÂ pm_runtime_put_autosuspend(dev);
> > +
> > +ÂÂÂÂ return ret;
> > +}
> > +
> > +const struct iio_buffer_setup_ops inv_icm42600_buffer_ops = {
> > +ÂÂÂÂ .preenable = inv_icm42600_buffer_preenable,
> > +ÂÂÂÂ .postenable = inv_icm42600_buffer_postenable,
>
> We've been slowly eroding the difference between preenable and posteenable.
> Would be good to understand why you need to define both?
>
> > +ÂÂÂÂ .predisable = inv_icm42600_buffer_predisable,
> > +ÂÂÂÂ .postdisable = inv_icm42600_buffer_postdisable,
> > +};
> > +
> > +int inv_icm42600_buffer_fifo_read(struct inv_icm42600_state *st,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int max)
> > +{
> > +ÂÂÂÂ size_t max_count;
> > +ÂÂÂÂ __be16 *raw_fifo_count;
> > +ÂÂÂÂ ssize_t i, size;
> > +ÂÂÂÂ const void *accel, *gyro, *timestamp;
> > +ÂÂÂÂ const int8_t *temp;
> > +ÂÂÂÂ unsigned int odr;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ /* reset all samples counters */
> > +ÂÂÂÂ st->fifo.count = 0;
> > +ÂÂÂÂ st->fifo.nb.gyro = 0;
> > +ÂÂÂÂ st->fifo.nb.accel = 0;
> > +ÂÂÂÂ st->fifo.nb.total = 0;
> > +
> > +ÂÂÂÂ /* compute maximum FIFO read size */
> > +ÂÂÂÂ if (max == 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ max_count = sizeof(st->fifo.data);
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ max_count = max * inv_icm42600_get_packet_size(st->fifo.en);
> > +
> > +ÂÂÂÂ /* read FIFO count value */
> > +ÂÂÂÂ raw_fifo_count = (__be16 *)st->buffer;
> > +ÂÂÂÂ ret = regmap_bulk_read(st->map, INV_ICM42600_REG_FIFO_COUNT,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ raw_fifo_count, sizeof(*raw_fifo_count));
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +ÂÂÂÂ st->fifo.count = be16_to_cpup(raw_fifo_count);
> > +
> > +ÂÂÂÂ /* check and clamp FIFO count value */
> > +ÂÂÂÂ if (st->fifo.count == 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
> > +ÂÂÂÂ if (st->fifo.count > max_count)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ st->fifo.count = max_count;
> > +
> > +ÂÂÂÂ /* read all FIFO data in internal buffer */
> > +ÂÂÂÂ ret = regmap_noinc_read(st->map, INV_ICM42600_REG_FIFO_DATA,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ st->fifo.data, st->fifo.count);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +
> > +ÂÂÂÂ /* compute number of samples for each sensor */
> > +ÂÂÂÂ for (i = 0; i < st->fifo.count; i += size) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &accel, &gyro, &temp, &timestamp, &odr);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (size <= 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (gyro != NULL && inv_icm42600_fifo_is_data_valid(gyro))
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ st->fifo.nb.gyro++;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (accel != NULL && inv_icm42600_fifo_is_data_valid(accel))
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ st->fifo.nb.accel++;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ st->fifo.nb.total++;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > +
> > +int inv_icm42600_buffer_fifo_parse(struct inv_icm42600_state *st)
> > +{
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ if (st->fifo.nb.total == 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
> > +
> > +ÂÂÂÂ if (st->fifo.nb.gyro > 0) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_gyro_parse_fifo(st->indio_gyro);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ if (st->fifo.nb.accel > 0) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_accel_parse_fifo(st->indio_accel);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > +
> > +int inv_icm42600_buffer_hwfifo_flush(struct inv_icm42600_state *st,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int count)
> > +{
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ ret = inv_icm42600_buffer_fifo_read(st, count);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> Definitely searching my memory for how this works in the core, so
> I may have it wrong.
>
> This is a bit unusual (I think). The intent of the flush
> is to read up to 'n' bytes because someone just did a read on the buffer
> or select, and there was data in the hwfifo capable of satisfying the read
> even though we haven't yet reached the watermark.
>
> Given both sensor types are coming from one buffer, do we have a potential
> issue here or under serving even though data is available?
>
> The case I worry may be served late is when an poll / select
> is waiting for sufficient data.
>
> So what should we be doing? We want to guarantee to provide data
> for each sensor type if it's in the hwfifo. As such we could keep reading
> until we have enough, but that could cause some issues if the two data rates
> are very different (overflow on the other kfifo)
>
> Maybe what you have here is the best we can do.
>
> I'm assuming the watermark level has a similar problem. One value represents
> the sum of the two types of data.
>
> > +
> > +ÂÂÂÂ if (st->fifo.nb.total == 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
> > +
> > +ÂÂÂÂ if (st->fifo.nb.gyro > 0) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_gyro_parse_fifo(st->indio_gyro);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ if (st->fifo.nb.accel > 0) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_accel_parse_fifo(st->indio_accel);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ return 0;
> > +}
> > +
> > +int inv_icm42600_buffer_init(struct inv_icm42600_state *st)
> > +{
> > +ÂÂÂÂ unsigned int val;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ /*
> > +ÂÂÂÂÂ * Default FIFO configuration (bits 7 to 5)
> > +ÂÂÂÂÂ * - use invalid value
> > +ÂÂÂÂÂ * - FIFO count in bytes
> > +ÂÂÂÂÂ * - FIFO count in big endian
> > +ÂÂÂÂÂ */
> > +ÂÂÂÂ val = INV_ICM42600_INTF_CONFIG0_FIFO_COUNT_ENDIAN;
> > +ÂÂÂÂ ret = regmap_update_bits(st->map, INV_ICM42600_REG_INTF_CONFIG0,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ GENMASK(7, 5), val);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +
> > +ÂÂÂÂ /*
> > +ÂÂÂÂÂ * Enable FIFO partial read and continuous watermark interrupt.
> > +ÂÂÂÂÂ * Disable all FIFO EN bits.
> > +ÂÂÂÂÂ */
> > +ÂÂÂÂ val = INV_ICM42600_FIFO_CONFIG1_RESUME_PARTIAL_RD |
> > +ÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG1_WM_GT_TH;
> > +ÂÂÂÂ return regmap_update_bits(st->map, INV_ICM42600_REG_FIFO_CONFIG1,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ GENMASK(6, 5) | GENMASK(3, 0), val);
> > +}
> > diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.h b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.h
> > new file mode 100644
> > index 000000000000..de2a3949dcc7
> > --- /dev/null
> > +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_buffer.h
> > @@ -0,0 +1,98 @@
> > +/* SPDX-License-Identifier: GPL-2.0-or-later */
> > +/*
> > + * Copyright (C) 2020 Invensense, Inc.
> > + */
> > +
> > +#ifndef INV_ICM42600_BUFFER_H_
> > +#define INV_ICM42600_BUFFER_H_
> > +
> > +#include <linux/kernel.h>
> > +#include <linux/bits.h>
> > +
> > +struct inv_icm42600_state;
> > +
> > +#define INV_ICM42600_SENSOR_GYROÂÂÂÂ BIT(0)
> > +#define INV_ICM42600_SENSOR_ACCELÂÂÂ BIT(1)
> > +#define INV_ICM42600_SENSOR_TEMPÂÂÂÂ BIT(2)
> > +
> > +/**
> > + * struct inv_icm42600_fifo - FIFO state variables
> > + * @on:ÂÂÂÂÂÂÂÂÂÂÂÂÂ reference counter for FIFO on.
> > + * @en:ÂÂÂÂÂÂÂÂÂÂÂÂÂ bits field of INV_ICM42600_SENSOR_* for FIFO EN bits.
> > + * @period:Â FIFO internal period.
> > + * @watermark:ÂÂÂÂÂÂ watermark configuration values for accel and gyro.
> > + * @count:ÂÂ number of bytes in the FIFO data buffer.
> > + * @nb:ÂÂÂÂÂÂÂÂÂÂÂÂÂ gyro, accel and total samples in the FIFO data buffer.
> > + * @data:ÂÂÂ FIFO data buffer aligned for DMA (2kB + 32 bytes of read cache).
> > + */
> > +struct inv_icm42600_fifo {
> > +ÂÂÂÂ unsigned int on;
> > +ÂÂÂÂ unsigned int en;
> > +ÂÂÂÂ uint32_t period;
> > +ÂÂÂÂ struct {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int accel;
> > +ÂÂÂÂ } watermark;
> > +ÂÂÂÂ size_t count;
> > +ÂÂÂÂ struct {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ size_t gyro;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ size_t accel;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ size_t total;
> > +ÂÂÂÂ } nb;
> > +ÂÂÂÂ uint8_t data[2080] ____cacheline_aligned;
> > +};
> > +
> > +/* FIFO data packet */
> > +struct inv_icm42600_fifo_sensor_data {
> > +ÂÂÂÂ __be16 x;
> > +ÂÂÂÂ __be16 y;
> > +ÂÂÂÂ __be16 z;
> > +} __packed;
>
> Why packed? Should be anyway I think.
>
> > +#define INV_ICM42600_FIFO_DATA_INVALIDÂÂÂÂÂÂÂÂÂÂÂÂÂÂ -32768
> > +
> > +static inline int16_t inv_icm42600_fifo_get_sensor_data(__be16 d)
> > +{
> > +ÂÂÂÂ return be16_to_cpu(d);
> > +}
> > +
> > +static inline bool
> > +inv_icm42600_fifo_is_data_valid(const struct inv_icm42600_fifo_sensor_data *s)
> > +{
> > +ÂÂÂÂ int16_t x, y, z;
> > +
> > +ÂÂÂÂ x = inv_icm42600_fifo_get_sensor_data(s->x);
> > +ÂÂÂÂ y = inv_icm42600_fifo_get_sensor_data(s->y);
> > +ÂÂÂÂ z = inv_icm42600_fifo_get_sensor_data(s->z);
> > +
> > +ÂÂÂÂ if (x == INV_ICM42600_FIFO_DATA_INVALID &&
> > +ÂÂÂÂÂÂÂÂ y == INV_ICM42600_FIFO_DATA_INVALID &&
> > +ÂÂÂÂÂÂÂÂ z == INV_ICM42600_FIFO_DATA_INVALID)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return false;
> > +
> > +ÂÂÂÂ return true;
> > +}
> > +
> > +ssize_t inv_icm42600_fifo_decode_packet(const void *packet, const void **accel,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const void **gyro, const int8_t **temp,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const void **timestamp, unsigned int *odr);
> > +
> > +extern const struct iio_buffer_setup_ops inv_icm42600_buffer_ops;
> > +
> > +int inv_icm42600_buffer_init(struct inv_icm42600_state *st);
> > +
> > +void inv_icm42600_buffer_update_fifo_period(struct inv_icm42600_state *st);
> > +
> > +int inv_icm42600_buffer_set_fifo_en(struct inv_icm42600_state *st,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int fifo_en);
> > +
> > +int inv_icm42600_buffer_update_watermark(struct inv_icm42600_state *st);
> > +
> > +int inv_icm42600_buffer_fifo_read(struct inv_icm42600_state *st,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int max);
> > +
> > +int inv_icm42600_buffer_fifo_parse(struct inv_icm42600_state *st);
> > +
> > +int inv_icm42600_buffer_hwfifo_flush(struct inv_icm42600_state *st,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int count);
> > +
> > +#endif
> > diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_core.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_core.c
> > index 246c1eb52231..6f1c1eb83953 100644
> > --- a/drivers/iio/imu/inv_icm42600/inv_icm42600_core.c
> > +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_core.c
> > @@ -18,6 +18,7 @@
> >Â #include <linux/iio/iio.h>
>
> >Â #include "inv_icm42600.h"
> > +#include "inv_icm42600_buffer.h"
>
> >Â static const struct regmap_range_cfg inv_icm42600_regmap_ranges[] = {
> >ÂÂÂÂÂÂÂ {
> > @@ -429,6 +430,18 @@ static irqreturn_t inv_icm42600_irq_handler(int irq, void *_data)
> >ÂÂÂÂÂÂÂ if (status & INV_ICM42600_INT_STATUS_FIFO_FULL)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ dev_warn(dev, "FIFO full data lost!\n");
>
> > +ÂÂÂÂ /* FIFO threshold reached */
> > +ÂÂÂÂ if (status & INV_ICM42600_INT_STATUS_FIFO_THS) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_buffer_fifo_read(st, 0);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ dev_err(dev, "FIFO read error %d\n", ret);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ }
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_buffer_fifo_parse(st);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ dev_err(dev, "FIFO parsing error %d\n", ret);
> > +ÂÂÂÂ }
> > +
> >Â out_unlock:
> >ÂÂÂÂÂÂÂ mutex_unlock(&st->lock);
> >ÂÂÂÂÂÂÂ return IRQ_HANDLED;
> > @@ -600,6 +613,10 @@ int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
> >ÂÂÂÂÂÂÂ if (ret)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
>
> > +ÂÂÂÂ ret = inv_icm42600_buffer_init(st);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > +
> >ÂÂÂÂÂÂÂ ret = inv_icm42600_gyro_init(st);
> >ÂÂÂÂÂÂÂ if (ret)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
> > @@ -645,6 +662,14 @@ static int __maybe_unused inv_icm42600_suspend(struct device *dev)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> >ÂÂÂÂÂÂÂ }
>
> > +ÂÂÂÂ /* disable FIFO data streaming */
> > +ÂÂÂÂ if (st->fifo.on) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG_BYPASS);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂ }
> > +
> >ÂÂÂÂÂÂÂ ret = inv_icm42600_set_pwr_mgmt0(st, INV_ICM42600_SENSOR_MODE_OFF,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_SENSOR_MODE_OFF, false,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ NULL);
> > @@ -684,6 +709,11 @@ static int __maybe_unused inv_icm42600_resume(struct device *dev)
> >ÂÂÂÂÂÂÂ if (ret)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
>
> > +ÂÂÂÂ /* restore FIFO data streaming */
> > +ÂÂÂÂ if (st->fifo.on)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = regmap_write(st->map, INV_ICM42600_REG_FIFO_CONFIG,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ INV_ICM42600_FIFO_CONFIG_STREAM);
> > +
> >Â out_unlock:
> >ÂÂÂÂÂÂÂ mutex_unlock(&st->lock);
> >ÂÂÂÂÂÂÂ return ret;
> > diff --git a/drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c b/drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c
> > index 38654e0d217b..b05c33876b8d 100644
> > --- a/drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c
> > +++ b/drivers/iio/imu/inv_icm42600/inv_icm42600_gyro.c
> > @@ -11,9 +11,12 @@
> >Â #include <linux/delay.h>
> >Â #include <linux/math64.h>
> >Â #include <linux/iio/iio.h>
> > +#include <linux/iio/buffer.h>
> > +#include <linux/iio/kfifo_buf.h>
>
> >Â #include "inv_icm42600.h"
> >Â #include "inv_icm42600_temp.h"
> > +#include "inv_icm42600_buffer.h"
>
> >Â #define INV_ICM42600_GYRO_CHAN(_modifier, _index, _ext_info)ÂÂÂÂÂÂÂÂ \
> >ÂÂÂÂÂÂÂ {ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > @@ -64,6 +67,76 @@ static const struct iio_chan_spec inv_icm42600_gyro_channels[] = {
> >ÂÂÂÂÂÂÂ INV_ICM42600_TEMP_CHAN(INV_ICM42600_GYRO_SCAN_TEMP),
> >Â };
>
> > +/* IIO buffer data: 8 bytes */
> > +struct inv_icm42600_gyro_buffer {
> > +ÂÂÂÂ struct inv_icm42600_fifo_sensor_data gyro;
> > +ÂÂÂÂ int8_t temp;
> > +ÂÂÂÂ uint8_t padding;
> > +};
> > +
> > +#define INV_ICM42600_SCAN_MASK_GYRO_3AXISÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > +ÂÂÂÂ (BIT(INV_ICM42600_GYRO_SCAN_X) |ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > +ÂÂÂÂ BIT(INV_ICM42600_GYRO_SCAN_Y) |ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ \
> > +ÂÂÂÂ BIT(INV_ICM42600_GYRO_SCAN_Z))
> > +
> > +#define INV_ICM42600_SCAN_MASK_TEMPÂ BIT(INV_ICM42600_GYRO_SCAN_TEMP)
> > +
> > +static const unsigned long inv_icm42600_gyro_scan_masks[] = {
> > +ÂÂÂÂ /* 3-axis gyro + temperature */
> > +ÂÂÂÂ INV_ICM42600_SCAN_MASK_GYRO_3AXIS | INV_ICM42600_SCAN_MASK_TEMP,
> > +ÂÂÂÂ 0,
> > +};
> > +
> > +/* enable gyroscope sensor and FIFO write */
> > +static int inv_icm42600_gyro_update_scan_mode(struct iio_dev *indio_dev,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ const unsigned long *scan_mask)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ struct inv_icm42600_sensor_conf conf = INV_ICM42600_SENSOR_CONF_INIT;
> > +ÂÂÂÂ unsigned int fifo_en = 0;
> > +ÂÂÂÂ unsigned int sleep_gyro = 0;
> > +ÂÂÂÂ unsigned int sleep_temp = 0;
> > +ÂÂÂÂ unsigned int sleep;
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ if (*scan_mask & INV_ICM42600_SCAN_MASK_TEMP) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* enable temp sensor */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_temp_conf(st, true, &sleep_temp);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ fifo_en |= INV_ICM42600_SENSOR_TEMP;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ if (*scan_mask & INV_ICM42600_SCAN_MASK_GYRO_3AXIS) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* enable gyro sensor */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ conf.mode = INV_ICM42600_SENSOR_MODE_LOW_NOISE;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = inv_icm42600_set_gyro_conf(st, &conf, &sleep_gyro);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ fifo_en |= INV_ICM42600_SENSOR_GYRO;
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ /* update data FIFO write */
> > +ÂÂÂÂ ret = inv_icm42600_buffer_set_fifo_en(st, fifo_en | st->fifo.en);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +
> > +ÂÂÂÂ ret = inv_icm42600_buffer_update_watermark(st);
> > +
> > +out_unlock:
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +ÂÂÂÂ /* sleep maximum required time */
> > +ÂÂÂÂ if (sleep_gyro > sleep_temp)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sleep = sleep_gyro;
> > +ÂÂÂÂ else
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ sleep = sleep_temp;
> > +ÂÂÂÂ if (sleep)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ msleep(sleep);
> > +ÂÂÂÂ return ret;
> > +}
> > +
> >Â static int inv_icm42600_gyro_read_sensor(struct inv_icm42600_state *st,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct iio_chan_spec const *chan,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int16_t *val)
> > @@ -260,7 +333,12 @@ static int inv_icm42600_gyro_write_odr(struct inv_icm42600_state *st,
> >ÂÂÂÂÂÂÂ mutex_lock(&st->lock);
>
> >ÂÂÂÂÂÂÂ ret = inv_icm42600_set_gyro_conf(st, &conf, NULL);
> > +ÂÂÂÂ if (ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ goto out_unlock;
> > +ÂÂÂÂ inv_icm42600_buffer_update_fifo_period(st);
> > +ÂÂÂÂ inv_icm42600_buffer_update_watermark(st);
>
> > +out_unlock:
> >ÂÂÂÂÂÂÂ mutex_unlock(&st->lock);
> >ÂÂÂÂÂÂÂ pm_runtime_mark_last_busy(dev);
> >ÂÂÂÂÂÂÂ pm_runtime_put_autosuspend(dev);
> > @@ -574,12 +652,51 @@ static int inv_icm42600_gyro_write_raw_get_fmt(struct iio_dev *indio_dev,
> >ÂÂÂÂÂÂÂ }
> >Â }
>
> > +static int inv_icm42600_gyro_hwfifo_set_watermark(struct iio_dev *indio_dev,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int val)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ st->fifo.watermark.gyro = val;
> > +ÂÂÂÂ ret = inv_icm42600_buffer_update_watermark(st);
> > +
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +
> > +ÂÂÂÂ return ret;
> > +}
> > +
> > +static int inv_icm42600_gyro_hwfifo_flush(struct iio_dev *indio_dev,
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned int count)
> > +{
>
> Nothing to do with this patch, but I realised reading this that we have
> some 'unusual' use of the word flush here. It's a straight forward
> read function so not sure why we called it flush.
>
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ int ret;
> > +
> > +ÂÂÂÂ if (count == 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
> > +
> > +ÂÂÂÂ mutex_lock(&st->lock);
> > +
> > +ÂÂÂÂ ret = inv_icm42600_buffer_hwfifo_flush(st, count);
> > +ÂÂÂÂ if (!ret)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ ret = st->fifo.nb.gyro;
> > +
> > +ÂÂÂÂ mutex_unlock(&st->lock);
> > +
> > +ÂÂÂÂ return ret;
> > +}
> > +
> >Â static const struct iio_info inv_icm42600_gyro_info = {
> >ÂÂÂÂÂÂÂ .read_raw = inv_icm42600_gyro_read_raw,
> >ÂÂÂÂÂÂÂ .read_avail = inv_icm42600_gyro_read_avail,
> >ÂÂÂÂÂÂÂ .write_raw = inv_icm42600_gyro_write_raw,
> >ÂÂÂÂÂÂÂ .write_raw_get_fmt = inv_icm42600_gyro_write_raw_get_fmt,
> >ÂÂÂÂÂÂÂ .debugfs_reg_access = inv_icm42600_debugfs_reg,
> > +ÂÂÂÂ .update_scan_mode = inv_icm42600_gyro_update_scan_mode,
> > +ÂÂÂÂ .hwfifo_set_watermark = inv_icm42600_gyro_hwfifo_set_watermark,
> > +ÂÂÂÂ .hwfifo_flush_to_buffer = inv_icm42600_gyro_hwfifo_flush,
> >Â };
>
> >Â int inv_icm42600_gyro_init(struct inv_icm42600_state *st)
> > @@ -587,6 +704,7 @@ int inv_icm42600_gyro_init(struct inv_icm42600_state *st)
> >ÂÂÂÂÂÂÂ struct device *dev = regmap_get_device(st->map);
> >ÂÂÂÂÂÂÂ const char *name;
> >ÂÂÂÂÂÂÂ struct iio_dev *indio_dev;
> > +ÂÂÂÂ struct iio_buffer *buffer;
>
> >ÂÂÂÂÂÂÂ name = devm_kasprintf(dev, GFP_KERNEL, "%s-gyro", st->name);
> >ÂÂÂÂÂÂÂ if (!name)
> > @@ -596,14 +714,54 @@ int inv_icm42600_gyro_init(struct inv_icm42600_state *st)
> >ÂÂÂÂÂÂÂ if (!indio_dev)
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
>
> > +ÂÂÂÂ buffer = devm_iio_kfifo_allocate(dev);
> > +ÂÂÂÂ if (!buffer)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ return -ENOMEM;
> > +
> >ÂÂÂÂÂÂÂ iio_device_set_drvdata(indio_dev, st);
> >ÂÂÂÂÂÂÂ indio_dev->dev.parent = dev;
> >ÂÂÂÂÂÂÂ indio_dev->name = name;
> >ÂÂÂÂÂÂÂ indio_dev->info = &inv_icm42600_gyro_info;
> > -ÂÂÂÂ indio_dev->modes = INDIO_DIRECT_MODE;
> > +ÂÂÂÂ indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
> >ÂÂÂÂÂÂÂ indio_dev->channels = inv_icm42600_gyro_channels;
> >ÂÂÂÂÂÂÂ indio_dev->num_channels = ARRAY_SIZE(inv_icm42600_gyro_channels);
> > +ÂÂÂÂ indio_dev->available_scan_masks = inv_icm42600_gyro_scan_masks;
> > +ÂÂÂÂ indio_dev->setup_ops = &inv_icm42600_buffer_ops;
> > +
> > +ÂÂÂÂ iio_device_attach_buffer(indio_dev, buffer);
>
> >ÂÂÂÂÂÂÂ st->indio_gyro = indio_dev;
> >ÂÂÂÂÂÂÂ return devm_iio_device_register(dev, st->indio_gyro);
> >Â }
> > +
> > +int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev)
> > +{
> > +ÂÂÂÂ struct inv_icm42600_state *st = iio_device_get_drvdata(indio_dev);
> > +ÂÂÂÂ ssize_t i, size;
> > +ÂÂÂÂ const void *accel, *gyro, *timestamp;
> > +ÂÂÂÂ const int8_t *temp;
> > +ÂÂÂÂ unsigned int odr;
> > +ÂÂÂÂ struct inv_icm42600_gyro_buffer buffer = {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ .padding = 0,
>
> Might be worth a comment here or where the structure is defined
> on why we make padding explicit.
>
> > +ÂÂÂÂ };
> > +
> > +ÂÂÂÂ /* parse all fifo packets */
> > +ÂÂÂÂ for (i = 0; i < st->fifo.count; i += size) {
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ size = inv_icm42600_fifo_decode_packet(&st->fifo.data[i],
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &accel, &gyro, &temp, &timestamp, &odr);
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* quit if error or FIFO is empty */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (size <= 0)
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return size;
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* skip packet if no gyro data or data is invalid */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ if (gyro == NULL || !inv_icm42600_fifo_is_data_valid(gyro))
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
> > +
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ /* fill and push data buffer */
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ memcpy(&buffer.gyro, gyro, sizeof(buffer.gyro));
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ buffer.temp = temp ? *temp : 0;
> > +ÂÂÂÂÂÂÂÂÂÂÂÂ iio_push_to_buffers(indio_dev, &buffer);
> > +ÂÂÂÂ }
> > +
> > +ÂÂÂÂ return 0;
> > +}