Re: [PATCH v3 8/8] iio: adc: Add events support to ad4052

From: Jonathan Cameron
Date: Sat Jun 14 2025 - 06:40:31 EST


On Sat, 14 Jun 2025 11:25:44 +0100
Jonathan Cameron <jic23@xxxxxxxxxx> wrote:

> On Fri, 13 Jun 2025 11:03:24 -0500
> David Lechner <dlechner@xxxxxxxxxxxx> wrote:
>
> > On 6/13/25 5:02 AM, Jorge Marques wrote:
> > > Hi David,
> > > On Thu, Jun 12, 2025 at 02:38:45PM -0500, David Lechner wrote:
> > >> On 6/10/25 2:34 AM, Jorge Marques wrote:
> > >>> The AD4052 family supports autonomous monitoring readings for threshold
> > >>> crossings. Add support for catching the GPIO interrupt and expose as an IIO
> > >>> event. The device allows to set either, rising and falling directions. Only
> > >>> either threshold crossing is implemented.
> > >>>
> > >>> Signed-off-by: Jorge Marques <jorge.marques@xxxxxxxxxx>
> > >>> ---
> > >>
> > >> ...
> > >>
> > >>> +
> > >>> +static ssize_t ad4052_events_frequency_store(struct device *dev,
> > >>> + struct device_attribute *attr,
> > >>> + const char *buf,
> > >>> + size_t len)
> > >>> +{
> > >>> + struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> > >>> + struct ad4052_state *st = iio_priv(indio_dev);
> > >>> + int ret;
> > >>> +
> > >>> + if (!iio_device_claim_direct(indio_dev))
> > >>> + return -EBUSY;
> > >>> + if (st->wait_event) {
> > >>> + ret = -EBUSY;
> > >>> + goto out_release;
> > >>> + }
> > >>
> > >> I'm wondering if we should instead have some kind of iio_device_claim_monitor_mode()
> > >> so that we don't have to implement this manually everywhere. If monitor mode was
> > >> claimed, then iio_device_claim_direct() and iio_device_claim_buffer_mode() would
> > >> both return -EBUSY. If buffer mode was claimed, iio_device_claim_monitor_mode()
> > >> would fail. If direct mode was claimed, iio_device_claim_monitor_mode() would wait.
> > >>
> > > I don't think this would scale with other vendors and devices, it is a
> >
> > Why not? I've seen lots of devices that have some sort of monitor mode
> > where they are internally continuously comparing measurements to something
> > and only signal an interrupt when some condition is met.
>
> There are lots that support such a monitor, but I think far fewer were direct
> accesses don't work at the same time. The max1363 comes to mind but in that
> case it is possible to do both monitor and direct reads it is just that the
> data format changes and I think we never bothered implementing the handling
> for that combination.
>
> I wouldn't mind such helpers if there are at least a couple of users.
>
I got this wrong. Key here is not direct access and monitor, but rather
monitor and buffering (which is the odd format case on the max1363 etc).

Anyhow, conclusion that helpers are fine is the same.

I would try to minimise what doesn't work when monitor mode is enabled though
(as commented in review of this patch).

We also have to cover the internal cases where buffer mode is claimed but
there isn't (IIRC) a call to that particular claim function as we don't
want to end up holding the lock - same will be true for monitor mode - there
is a difference between temporary fixing of state where locks are fine
and the mode running for a long period in which we don't hold the lock.

> >
> > > limitation of ADI:ADC:SPI requiring to enter configuration mode to read
> >
> > I don't see how it could be a limitiation exclusive to this combination of
> > vendor, sensor type and bus type.
> >
> > > registers. A deep dive into the other drivers that use IIO Events is
> > > needed.
> > >>> +
> >
> > ...
> >
> > >>> +
> > >>> +static int ad4052_monitor_mode_disable(struct ad4052_state *st)
> > >>> +{
> > >>> + int ret;
> > >>> +
> > >>> + pm_runtime_mark_last_busy(&st->spi->dev);
> > >>> + pm_runtime_put_autosuspend(&st->spi->dev);
> > >>> +
> > >>> + ret = ad4052_exit_command(st);
> > >>> + if (ret)
> > >>> + return ret;
> > >>> + return regmap_write(st->regmap, AD4052_REG_DEVICE_STATUS,
> > >>> + AD4052_REG_DEVICE_STATUS_MAX_FLAG |
> > >>> + AD4052_REG_DEVICE_STATUS_MIN_FLAG);
> > >>> +}
> > >>> +
> > >>
> > >> It seems like we need to make sure monitor mode is disabled when the
> > >> driver is removed. Otherwise we could end up with unbalanced calls to
> > >> the pm_runtime stuff and leave the chip running.
> > >>
> > >>
> > > When monitor mode is enabled, pm is already disabled (won't enter low
> > > power). I expect the pm to handle the clean-up properly since devm is
> > > used.
> > > The .remove() I suggest is reg access to:
> > >
> > > * Put in configuration mode, if not.
> > > * Put on low power mode, if not.
> > >
> > I was just thinking something like:
> >
> > if (st->wait_event)
> > ad4052_monitor_mode_disable(st);
> >
> > Also might need to use devm_add_action_or_reset() instead of .remove
> > to get correct ordering.
>
>