Re: [PATCH 05/11] staging: iio: adc: ad7606: Add support for threaded irq

From: Popa, Stefan Serban
Date: Mon Dec 17 2018 - 05:28:31 EST


On Du, 2018-12-16 at 13:49 +0000, Jonathan Cameron wrote:
> On Thu, 13 Dec 2018 14:46:17 +0200
> Stefan Popa <stefan.popa@xxxxxxxxxx> wrote:
>
> >
> > This patch replaces the use of a polling ring buffer with a threaded
> > interrupt.
> >
> > Enabling the buffer sets the CONVST signal to high. When the rising
> > edge
> > of the CONVST is applied, BUSY signal goes logic high and transitions
> > low
> > at the end of the entire conversion process. The falling edge of the
> > BUSY
> > signal triggers the interrupt.
> >
> > ad7606_trigger_handler() is used as bottom half of the poll function.
> > It reads data from the device and stores it in the internal buffer.
> >
> > Signed-off-by: Stefan Popa <stefan.popa@xxxxxxxxxx>
> I'd like a little more info as comments in this one. See below.
> Which is another way of saying I have no idea what is going on :)
>
> Thanks,
>
> Jonathan.
>

Hi Jonathan,
Thank you for the review. It turns out that there is no reason to trigger a
conversion before disabling the buffer. I will remove it in v2.

Thank you!
-Stefan

> >
> > ---
> > Âdrivers/staging/iio/adc/ad7606.c | 116 +++++++++++++++++++++++++++++
> > ----------
> > Âdrivers/staging/iio/adc/ad7606.h |ÂÂÂ6 +-
> > Â2 files changed, 89 insertions(+), 33 deletions(-)
> >
> > diff --git a/drivers/staging/iio/adc/ad7606.c
> > b/drivers/staging/iio/adc/ad7606.c
> > index 7191d51..13aeeec 100644
> > --- a/drivers/staging/iio/adc/ad7606.c
> > +++ b/drivers/staging/iio/adc/ad7606.c
> > @@ -21,6 +21,7 @@
> > Â#include <linux/iio/sysfs.h>
> > Â#include <linux/iio/buffer.h>
> > Â#include <linux/iio/trigger_consumer.h>
> > +#include <linux/iio/trigger.h>
> > Â#include <linux/iio/triggered_buffer.h>
> > Â
> > Â#include "ad7606.h"
> > @@ -81,36 +82,24 @@ static int ad7606_read_samples(struct ad7606_state
> > *st)
> > Âstatic irqreturn_t ad7606_trigger_handler(int irq, void *p)
> > Â{
> > Â struct iio_poll_func *pf = p;
> > - struct ad7606_state *st = iio_priv(pf->indio_dev);
> > -
> > - gpiod_set_value(st->gpio_convst, 1);
> > -
> > - return IRQ_HANDLED;
> > -}
> > -
> > -/**
> > - * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring
> > buffer
> > - * @work_s: the work struct through which this was scheduled
> > - *
> > - * Currently there is no option in this driver to disable the saving
> > of
> > - * timestamps within the ring.
> > - * I think the one copy of this at a time was to avoid problems if the
> > - * trigger was set far too high and the reads then locked up the
> > computer.
> > - **/
> > -static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
> > -{
> > - struct ad7606_state *st = container_of(work_s, struct
> > ad7606_state,
> > - poll_work);
> > - struct iio_dev *indio_dev = iio_priv_to_dev(st);
> > + struct iio_dev *indio_dev = pf->indio_dev;
> > + struct ad7606_state *st = iio_priv(indio_dev);
> > Â int ret;
> > Â
> > + mutex_lock(&st->lock);
> > +
> > Â ret = ad7606_read_samples(st);
> > Â if (ret == 0)
> > Â iio_push_to_buffers_with_timestamp(indio_dev, st-
> > >data,
> > Â ÂÂÂiio_get_time_ns(ind
> > io_dev));
> > Â
> > - gpiod_set_value(st->gpio_convst, 0);
> > Â iio_trigger_notify_done(indio_dev->trig);
> > + /* The rising edge of the CONVST signal starts a new
> > conversion. */
> > + gpiod_set_value(st->gpio_convst, 1);
> > +
> > + mutex_unlock(&st->lock);
> > +
> > + return IRQ_HANDLED;
> > Â}
> > Â
> > Âstatic int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int
> > ch)
> > @@ -378,8 +367,11 @@ static int ad7606_request_gpios(struct
> > ad7606_state *st)
> > Â return PTR_ERR_OR_ZERO(st->gpio_os);
> > Â}
> > Â
> > -/**
> > - *ÂÂInterrupt handler
> > +/*
> > + * The BUSY signal indicates when conversions are in progress, so when
> > a rising
> > + * edge of CONVST is applied, BUSY goes logic high and transitions low
> > at the
> > + * end of the entire conversion process. The falling edge of the BUSY
> > signal
> > + * triggers this interrupt.
> > Â */
> > Âstatic irqreturn_t ad7606_interrupt(int irq, void *dev_id)
> > Â{
> > @@ -387,7 +379,8 @@ static irqreturn_t ad7606_interrupt(int irq, void
> > *dev_id)
> > Â struct ad7606_state *st = iio_priv(indio_dev);
> > Â
> > Â if (iio_buffer_enabled(indio_dev)) {
> > - schedule_work(&st->poll_work);
> > + gpiod_set_value(st->gpio_convst, 0);
> > + iio_trigger_poll_chained(st->trig);
> > Â } else {
> > Â complete(&st->completion);
> > Â }
> > @@ -395,26 +388,74 @@ static irqreturn_t ad7606_interrupt(int irq, void
> > *dev_id)
> > Â return IRQ_HANDLED;
> > Â};
> > Â
> > +static int ad7606_validate_trigger(struct iio_dev *indio_dev,
> > + ÂÂÂstruct iio_trigger *trig)
> > +{
> > + struct ad7606_state *st = iio_priv(indio_dev);
> > +
> > + if (st->trig != trig)
> > + return -EINVAL;
> > +
> > + return 0;
> > +}
> > +
> > +static int ad7606_buffer_postenable(struct iio_dev *indio_dev)
> > +{
> > + struct ad7606_state *st = iio_priv(indio_dev);
> > +
> > + iio_triggered_buffer_postenable(indio_dev);
> > + gpiod_set_value(st->gpio_convst, 1);
> > +
> > + return 0;
> > +}
> > +
> > +static int ad7606_buffer_predisable(struct iio_dev *indio_dev)
> > +{
> > + struct ad7606_state *st = iio_priv(indio_dev);
> > + int ret;
> > +
> > + reinit_completion(&st->completion);
> > + gpiod_set_value(st->gpio_convst, 1);
> > + ret = wait_for_completion_timeout(&st->completion,
> > + ÂÂmsecs_to_jiffies(1000));
> Along with Dan's comment. I'd like to see a comment here on what
> is actually going on.ÂÂNot immediately obvious a conversion should
> be triggered to disable the buffer...
>
> I'll guess there is a race against the normal handler that we
> are closing with this dance, but that race needs explaining.
>
> >
> > + gpiod_set_value(st->gpio_convst, 0);
> > +
> > + return iio_triggered_buffer_predisable(indio_dev);
> > +}
> > +
> > +static const struct iio_buffer_setup_ops ad7606_buffer_ops = {
> > + .postenable = &ad7606_buffer_postenable,
> > + .predisable = &ad7606_buffer_predisable,
> > +};
> > +
> > Âstatic const struct iio_info ad7606_info_no_os_or_range = {
> > Â .read_raw = &ad7606_read_raw,
> > + .validate_trigger = &ad7606_validate_trigger,
> > Â};
> > Â
> > Âstatic const struct iio_info ad7606_info_os_and_range = {
> > Â .read_raw = &ad7606_read_raw,
> > Â .write_raw = &ad7606_write_raw,
> > Â .attrs = &ad7606_attribute_group_os_and_range,
> > + .validate_trigger = &ad7606_validate_trigger,
> > Â};
> > Â
> > Âstatic const struct iio_info ad7606_info_os = {
> > Â .read_raw = &ad7606_read_raw,
> > Â .write_raw = &ad7606_write_raw,
> > Â .attrs = &ad7606_attribute_group_os,
> > + .validate_trigger = &ad7606_validate_trigger,
> > Â};
> > Â
> > Âstatic const struct iio_info ad7606_info_range = {
> > Â .read_raw = &ad7606_read_raw,
> > Â .write_raw = &ad7606_write_raw,
> > Â .attrs = &ad7606_attribute_group_range,
> > + .validate_trigger = &ad7606_validate_trigger,
> > +};
> > +
> > +static const struct iio_trigger_ops ad7606_trigger_ops = {
> > + .validate_device = iio_trigger_validate_own_device,
> > Â};
> > Â
> > Âstatic void ad7606_regulator_disable(void *data)
> > @@ -446,7 +487,6 @@ int ad7606_probe(struct device *dev, int irq, void
> > __iomem *base_address,
> > Â /* tied to logic low, analog input range is +/- 5V */
> > Â st->range = 0;
> > Â st->oversampling = 1;
> > - INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
> > Â
> > Â st->reg = devm_regulator_get(dev, "avcc");
> > Â if (IS_ERR(st->reg))
> > @@ -491,14 +531,32 @@ int ad7606_probe(struct device *dev, int irq,
> > void __iomem *base_address,
> > Â if (ret)
> > Â dev_warn(st->dev, "failed to RESET: no RESET GPIO
> > specified\n");
> > Â
> > - ret = devm_request_irq(dev, irq, ad7606_interrupt,
> > IRQF_TRIGGER_FALLING,
> > - ÂÂÂÂÂÂÂname, indio_dev);
> > + st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
> > + ÂÂindio_dev->name, indio_dev-
> > >id);
> > + if (!st->trig)
> > + return -ENOMEM;
> > +
> > + st->trig->ops = &ad7606_trigger_ops;
> > + st->trig->dev.parent = dev;
> > + iio_trigger_set_drvdata(st->trig, indio_dev);
> > + ret = devm_iio_trigger_register(dev, st->trig);
> > + if (ret)
> > + return ret;
> > +
> > + indio_dev->trig = iio_trigger_get(st->trig);
> > +
> > + ret = devm_request_threaded_irq(dev, irq,
> > + NULL,
> > + &ad7606_interrupt,
> > + IRQF_TRIGGER_FALLING |
> > IRQF_ONESHOT,
> > + name, indio_dev);
> > Â if (ret)
> > Â return ret;
> > Â
> > Â ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> > + ÂÂÂÂÂÂ&iio_pollfunc_store_time
> > ,
> > Â ÂÂÂÂÂÂ&ad7606_trigger_handler,
> > - ÂÂÂÂÂÂNULL, NULL);
> > + ÂÂÂÂÂÂ&ad7606_buffer_ops);
> > Â if (ret)
> > Â return ret;
> > Â
> > diff --git a/drivers/staging/iio/adc/ad7606.h
> > b/drivers/staging/iio/adc/ad7606.h
> > index 70486ef..b238e9b 100644
> > --- a/drivers/staging/iio/adc/ad7606.h
> > +++ b/drivers/staging/iio/adc/ad7606.h
> > @@ -26,8 +26,6 @@ struct ad7606_chip_info {
> > Â * @dev pointer to kernel device
> > Â * @chip_info entry in the table of chips that
> > describes this device
> > Â * @reg regulator info for the the power supply of the
> > device
> > - * @poll_work work struct for continuously reading data
> > from the device
> > - * into an IIO triggered buffer
> > Â * @bops bus operations (SPI or parallel)
> > Â * @range voltage range selection, selects which scale
> > to apply
> > Â * @oversampling oversampling selection
> > @@ -42,14 +40,13 @@ struct ad7606_chip_info {
> > Â * is being read on the first channel
> > Â * @gpio_os GPIO descriptors to control oversampling on
> > the device
> > Â * @complete completion to indicate end of conversion
> > + * @trig The IIO trigger associated with the device.
> > Â * @data buffer for reading data from the device
> > Â */
> > -
> > Âstruct ad7606_state {
> > Â struct device *dev;
> > Â const struct ad7606_chip_info *chip_info;
> > Â struct regulator *reg;
> > - struct work_struct poll_work;
> > Â const struct ad7606_bus_ops *bops;
> > Â unsigned int range;
> > Â unsigned int oversampling;
> > @@ -62,6 +59,7 @@ struct ad7606_state {
> > Â struct gpio_desc *gpio_standby;
> > Â struct gpio_desc *gpio_frstdata;
> > Â struct gpio_descs *gpio_os;
> > + struct iio_trigger *trig;
> > Â struct completion completion;
> > Â
> > Â /*