iio: accel: adxl355: Add triggered buffer support
PATCH 1/1 iio: accel: adxl355: Add triggered buffer support
Provide a way for continuous data capture by setting up buffer support. The data ready signal exposed at the DRDY pin of the ADXL355 is exploited as a hardware interrupt which triggers to fill the buffer.
--- drivers/iio/accel/Kconfig | 4 + drivers/iio/accel/adxl355_core.c | 154 ++++++++++++++++++++++++++++++- 2 files changed, 155 insertions(+), 3 deletions(-)
drivers/iio/accel/Kconfig +4 -0
@@ -69,6 +69,8 @@ config ADXL355_I2C depends on I2C select ADXL355 select REGMAP_I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER help Say Y here if you want to build i2c support for the Analog Devices ADXL355 3-axis digital accelerometer.
@@ -82,6 +84,8 @@ config ADXL355_SPI depends on SPI select ADXL355 select REGMAP_SPI + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER help Say Y here if you want to build spi support for the Analog Devices ADXL355 3-axis digital accelerometer.
drivers/iio/accel/adxl355_core.c +151 -3
246 lines not on this page · raw message
Signature · 1 line
2.30.1
Quoted 4 lines · Puranjay Mohan
Provide a way for continuous data capture by setting up buffer support. The data ready signal exposed at the DRDY pin of the ADXL355 is exploited as a hardware interrupt which triggers to fill the buffer.
...
Quoted 4 lines
*
* Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf
*/
-
Unrelated change.
...
drivers/iio/accel/adxl355_core.c line 15
Quoted 5 lines
drivers/iio/accel/adxl355_core.c +4
-0+#include <linux/iio/buffer.h> #include <linux/iio/iio.h> +#include <linux/iio/trigger.h> +#include <linux/iio/triggered_buffer.h> +#include <linux/iio/trigger_consumer.h>
I would rather regroup this like
linux/*
asm/*
linux/iio*
...
Quoted 1 line
+#include <
linux/of_irq.h>
Okay, this is due to the absence of generic fwnode_irq_get_byname() or so.
...
Quoted 3 lines
struct
adxl355_data{+ int irq;
Depending on container_of and frequency of usage this is not a good location for this.
Strating from regmap pointer is much better (no pointer arithmetics involved).
Quoted 5 lines
struct regmap *regmap; struct device *dev; struct mutex lock; /* lock to protect op_mode */};
...
drivers/iio/accel/adxl355_core.c static int adxl355_read_avail(struct iio_dev *indio_dev, · line 565
Quoted 3 lines
drivers/iio/accel/adxl355_core.c +3
-0+ ret =regmap_bulk_read (data->regmap,ADXL355_XDATA3_REG , + &data->buffer.buf[1 ], +3 );
ARRAY_SIZE()? Or put this 3 to the previous line, it will be easier to read.
Ditto for the rest of the similar code.
Quoted 2 lines
(quoted diff) +2
-0+ if (ret) + goto out_unlock_notify;
...
drivers/iio/accel/adxl355_core.c int adxl355_core_probe(struct device *dev, struct regmap *regmap, · line 696
Quoted 2 lines
drivers/iio/accel/adxl355_core.c +2
-0+/* +* Would be good to move it to the generic version.
Something like "TODO: Would be..." ?
Quoted 4 lines
drivers/iio/accel/adxl355_core.c +4
-0+*/ + ret =of_irq_get_byname (dev->of_node,"DRDY" ); +if (ret >0 ) + data->irq = ret;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 5 lines · On Fri, 13 Aug 2021 14:04:55 +0530 Puranjay Mohan <[email protected]> wrote:
Provide a way for continuous data capture by setting up buffer support. The data ready signal exposed at the DRDY pin of the ADXL355 is exploited as a hardware interrupt which triggers to fill the buffer.
Signed-off-by Puranjay Mohan <[email protected]>
Hi.
A few comments additions to add to what Andy has highlighted. Other than these details and those Andy highlighted, this looks good to me.
...
Quoted 9 lines
drivers/iio/accel/adxl355_core.c +1
-0@@ -157,6 +162,7 @@ static const struct adxl355_chan_info adxl355_chans[] = { };struct adxl355_data { +int irq;struct regmap *regmap;struct device *dev;struct mutex lock;/* lock to protect op_mode */
drivers/iio/accel/adxl355_core.c struct adxl355_data { · line 174
Quoted 5 lines
drivers/iio/accel/adxl355_core.c +1
-1@@ -165,7 +171,14 @@ struct adxl355_data {enum adxl355_hpf_3db hpf_3db;int calibbias[3 ];int adxl355_hpf_3db_table[7 ][2 ]; -u8 transf_buf[3 ]____cacheline_aligned ; +struct iio_trigger *dready_trig;
As the rest of the structure doesn't have extra spaces for alignment of variable names, don't do it here either.
Quoted 14 lines
drivers/iio/accel/adxl355_core.c +7
-0+union { +u8 transf_buf[3 ]; +struct { +u8 buf[14 ]; +s64 ts; + } buffer; + }____cacheline_aligned ; }; static int adxl355_set_op_mode(struct adxl355_data *data,@@ -186,6 +199,23 @@ static int adxl355_set_op_mode(struct adxl355_data *data,return ret; }
...
Quoted 6 lines
drivers/iio/accel/adxl355_core.c +6
-0+static const struct iio_trigger_ops adxl355_trigger_ops = { + .set_trigger_state = &adxl355_data_rdy_trigger_set_state, + .validate_device = &iio_trigger_validate_own_device, +}; + +static irqreturn_t adxl355_trigger_handler (int irq,void *p)
drivers/iio/accel/adxl355_core.c static int adxl355_read_avail(struct iio_dev *indio_dev, · line 564
Quoted 10 lines
drivers/iio/accel/adxl355_core.c +10
-0+{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct adxl355_data *data =iio_priv (indio_dev); +int ret; + +mutex_lock (&data->lock); + + ret = regmap_bulk_read(data->regmap, ADXL355_XDATA3_REG, + &data->buffer.buf[1],
I would add some comments on the data layout here given you are writing the bottom 24bits of a 32 bit big endian variable and that may be non obvious.
There is also an issue as a result of the union used for the buffer. It is (I think) possible that earlier read has put a non 0 value into transf_buf[0]. Now C doesn't guarantee that will be visible if accessed via buffer.buf[0], but it's certainly possible. As such you may get garbage in that top byte. It's not a 'problem' from an ABI point of view, but it might well confused someone who is for example logging the data stream unprocessed.
We have drivers where the hardware puts something in the bits that will get screened out using the type (often status info and similar) but we probably don't want it to be from an entirely different read or write.
Perhaps just set buf[0] = 0 somewhere in this funciton with a comment on why.
Quoted 4 lines
drivers/iio/accel/adxl355_core.c +4
-0+ 3); +if (ret) + goto out_unlock_notify; +
...
Quoted 14 lines
drivers/iio/accel/adxl355_core.c +13
-0+static int adxl355_probe_trigger (struct iio_dev *indio_dev) +{ +struct adxl355_data *data =iio_priv (indio_dev); +int ret; + +if (!data->irq) { +dev_info (data->dev,"no irq, using polling\n" ); +return 0 ; + } + + data->dready_trig =devm_iio_trigger_alloc (data->dev,"%s-dev%d" , + indio_dev->name, + indio_dev->id);
drivers/iio/accel/adxl355_core.c static const struct iio_chan_spec adxl355_channels[] = { · line 663
Quoted 10 lines
drivers/iio/accel/adxl355_core.c +10
-0+if (!data->dready_trig) +return -ENOMEM ; + + data->dready_trig->ops = &adxl355_trigger_ops; +iio_trigger_set_drvdata (data->dready_trig, indio_dev); + + ret =devm_request_irq (data->dev, data->irq, + &iio_trigger_generic_data_rdy_poll, +IRQF_ONESHOT ,"adxl355_irq" , data->dready_trig); +if (ret <0 )
if (ret)
If you follow through what devm_request_irq calls, you will see devm_request_threaded_irq internally checks for errors with if (rc) so we should do the same here. It would also be more consistent with the rest of your code in this function.
Quoted 28 lines
drivers/iio/accel/adxl355_core.c +21
-0+return dev_err_probe (data->dev, ret,"request irq %d failed\n" , + data->irq); + + ret =devm_iio_trigger_register (data->dev, data->dready_trig); +if (ret) { +dev_err (data->dev,"iio trigger register failed\n" ); +return ret; + } + + indio_dev->trig =iio_trigger_get (data->dready_trig); + +return 0 ; +} +int adxl355_core_probe (struct device *dev,struct regmap *regmap,const char *name) {@@ -563,18 +692,37 @@ int adxl355_core_probe(struct device *dev, struct regmap *regmap, data->op_mode =ADXL355_STANDBY ;mutex_init (&data->lock); +/* +* Would be good to move it to the generic version. +*/ + ret =of_irq_get_byname (dev->of_node,"DRDY" ); +if (ret >0 ) + data->irq = ret; +
drivers/iio/accel/adxl355_core.c int adxl355_core_probe(struct device *dev, struct regmap *regmap, · line 710
Quoted 10 lines
drivers/iio/accel/adxl355_core.c +2
-1indio_dev->name = name; indio_dev->info = &adxl355_info; indio_dev->modes =INDIO_DIRECT_MODE ; indio_dev->channels = adxl355_channels; indio_dev->num_channels =ARRAY_SIZE (adxl355_channels); + indio_dev->available_scan_masks = adxl355_avail_scan_masks; ret =adxl355_setup (data); -if (ret <0 ) { +if (ret) {
Nitpick. Ideally this would have been in an earlier patch, but it's trivial here so I'm not that bothered (so don't bother moving it).
Quoted 18 lines
drivers/iio/accel/adxl355_core.c +11
-0dev_err (dev,"ADXL355 setup failed\n" );return ret; } + ret =devm_iio_triggered_buffer_setup (dev, indio_dev, + &iio_pollfunc_store_time, + &adxl355_trigger_handler,NULL ); +if (ret) +return dev_err_probe (dev, ret, +"iio triggered buffer setup failed\n" ); + + ret =adxl355_probe_trigger (indio_dev); +if (ret) +return ret; +return devm_iio_device_register (dev, indio_dev); }EXPORT_SYMBOL_GPL (adxl355_core_probe);