From: Jonathan Cameron <jic23@kernel.org>
To: "Uwe Kleine-König" <u.kleine-koenig@baylibre.com>
Cc: Alexandru Tachici <alexandru.tachici@analog.com>,
Conor Dooley <conor+dt@kernel.org>,
David Lechner <dlechner@baylibre.com>,
Dumitru Ceclan <dumitru.ceclan@analog.com>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
Nuno Sa <nuno.sa@analog.com>, Rob Herring <robh@kernel.org>,
devicetree@vger.kernel.org, linux-iio@vger.kernel.org
Subject: Re: [PATCH 2/3] iio: adc: ad_sigma_delta: Add support for reading irq status using a GPIO
Date: Sun, 27 Oct 2024 12:04:39 +0000 [thread overview]
Message-ID: <20241027120439.664bc74f@jic23-huawei> (raw)
In-Reply-To: <20241024171703.201436-7-u.kleine-koenig@baylibre.com>
On Thu, 24 Oct 2024 19:17:04 +0200
Uwe Kleine-König <u.kleine-koenig@baylibre.com> wrote:
> Some of the ADCs by Analog signal their irq condition on the MISO line.
> So typically that line is connected to an SPI controller and a GPIO. The
> GPIO is used as input and the respective interrupt is enabled when the
> last SPI transfer is completed.
>
> Depending on the GPIO controller the toggling MISO line might make the
> interrupt pending even while it's masked. In that case the irq handler
> is called immediately after irq_enable() and so before the device
> actually pulls that line low which results in non-sense values being
> reported to the upper layers.
>
> The only way to find out if the line was actually pulled low is to read
> the GPIO. (There is a flag in AD7124's status register that also signals
> if an interrupt was asserted, but reading that register toggles the MISO
> line and so might trigger another spurious interrupt.)
>
> Add the possibility to specify an interrupt GPIO in the machine
> description instead of a plain interrupt. This GPIO is used as interrupt
> source and to check if the irq line is actually active in the irq
> handler.
Maybe we should just implement polling instead and never enable the interrupt
for cases like this?
I'm not sure if it is the device assumptions that are wrong wrt to the Linux
interrupt model, or the GPIO/IRQ chip you have doing something it shouldn't.
I am worried this solution is fragile.
Polling was never in the driver because the interrupt is always wired
but if we need it to get around the fact it is wired, but to a pin we can't
really use as an interrupt, then maybe it is time to add polling.
(probably just a fixed delay and check the status register).
>
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
> ---
> drivers/iio/adc/ad_sigma_delta.c | 36 +++++++++++++++++++++-----
> include/linux/iio/adc/ad_sigma_delta.h | 1 +
> 2 files changed, 31 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
> index e2bed2d648f2..d35602cfb093 100644
> --- a/drivers/iio/adc/ad_sigma_delta.c
> +++ b/drivers/iio/adc/ad_sigma_delta.c
> @@ -539,12 +539,29 @@ static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
> {
> struct ad_sigma_delta *sigma_delta = private;
>
> - complete(&sigma_delta->completion);
> - disable_irq_nosync(irq);
> - sigma_delta->irq_dis = true;
> - iio_trigger_poll(sigma_delta->trig);
> + /*
> + * AD7124 and a few others use the same physical line for interrupt
> + * reporting (nRDY) and MISO.
This is pretty common, so I'd drop the 'and a few others' bit.
> + * As MISO toggles when reading a register, this likely results in a
> + * pending interrupt. This has two consequences: a) The irq might
> + * trigger immediately after it's enabled even though the conversion
> + * isn't done yet; and b) checking the STATUS register's nRDY flag is
> + * off-limits as reading that would trigger another irq event.
> + *
> + * So read the MOSI line as GPIO (if available) and only trigger the irq
> + * if the line is active.
> + */
>
> - return IRQ_HANDLED;
> + if (!sigma_delta->irq_gpiod || gpiod_get_value(sigma_delta->irq_gpiod)) {
> + complete(&sigma_delta->completion);
> + disable_irq_nosync(irq);
> + sigma_delta->irq_dis = true;
> + iio_trigger_poll(sigma_delta->trig);
> +
> + return IRQ_HANDLED;
> + } else {
> + return IRQ_NONE;
> + }
> }
>
> /**
> @@ -676,8 +693,15 @@ int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
>
> if (info->irq_line)
> sigma_delta->irq_line = info->irq_line;
> - else
> + else if (spi->irq)
> sigma_delta->irq_line = spi->irq;
> + else {
> + sigma_delta->irq_gpiod = devm_gpiod_get(&spi->dev, "interrupt", GPIOD_IN);
> + if (IS_ERR(sigma_delta->irq_gpiod))
> + return dev_err_probe(&spi->dev, PTR_ERR(sigma_delta->irq_gpiod),
> + "Failed to find interrupt gpio\n");
> + sigma_delta->irq_line = gpiod_to_irq(sigma_delta->irq_gpiod);
As I mentioned on the binding if this is something we are going to do there should be
no assumption of the gpio and the irq being the same pin.
> + }
>
> iio_device_set_drvdata(indio_dev, sigma_delta);
>
> diff --git a/include/linux/iio/adc/ad_sigma_delta.h b/include/linux/iio/adc/ad_sigma_delta.h
> index f8c1d2505940..fc0141e0f0ef 100644
> --- a/include/linux/iio/adc/ad_sigma_delta.h
> +++ b/include/linux/iio/adc/ad_sigma_delta.h
> @@ -96,6 +96,7 @@ struct ad_sigma_delta {
> unsigned int active_slots;
> unsigned int current_slot;
> unsigned int num_slots;
> + struct gpio_desc *irq_gpiod;
> int irq_line;
> bool status_appended;
> /* map slots to channels in order to know what to expect from devices */
next prev parent reply other threads:[~2024-10-27 12:04 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-24 17:17 [PATCH 0/3] iio: adc: ad7124: Make it work on de10-nano Uwe Kleine-König
2024-10-24 17:17 ` [PATCH 1/3] dt-bindings: iio: adc: adi,ad7124: Allow specifications of a gpio for irq line Uwe Kleine-König
2024-10-27 11:54 ` Jonathan Cameron
2024-10-27 21:53 ` Uwe Kleine-König
2024-10-28 18:57 ` Jonathan Cameron
2024-10-27 20:49 ` Krzysztof Kozlowski
2024-10-27 21:26 ` Rob Herring
2024-10-28 9:51 ` Uwe Kleine-König
2024-10-24 17:17 ` [PATCH 2/3] iio: adc: ad_sigma_delta: Add support for reading irq status using a GPIO Uwe Kleine-König
2024-10-27 12:04 ` Jonathan Cameron [this message]
2024-10-24 17:17 ` [PATCH 3/3] iio: adc: ad7124: Disable all channels at probe time Uwe Kleine-König
2024-10-27 11:42 ` Jonathan Cameron
2024-10-27 21:47 ` 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=20241027120439.664bc74f@jic23-huawei \
--to=jic23@kernel.org \
--cc=Michael.Hennerich@analog.com \
--cc=alexandru.tachici@analog.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=dumitru.ceclan@analog.com \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=u.kleine-koenig@baylibre.com \
/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).