Re: [PATCH v2] staging: iio: adc: ad7816: add timeout to busy-wait loop
From: Andy Shevchenko
Date: Thu Feb 26 2026 - 04:18:45 EST
On Thu, Feb 26, 2026 at 08:25:47AM +0000, Ruslan Valiyev wrote:
> The ad7816_spi_read() function polls the busy GPIO pin in a tight
> loop without any timeout. If the hardware fails to deassert the
> busy signal, the kernel hangs indefinitely in an unbounded
> busy-wait.
>
> Replace the open-coded while/cpu_relax() loop with
> read_poll_timeout_atomic() which polls every 5 us and returns
> -ETIMEDOUT after 1 ms. Per the AD7816 datasheet the maximum
> conversion time is 27 us (temperature channel), so 1 ms provides
> generous margin.
>
> Use the atomic variant to preserve the existing busy-wait semantics
> of the original loop.
>
> Also handle the case where gpiod_get_value() returns a negative
> error code, which the original loop would treat as busy and keep
> spinning on indefinitely.
First of all, start a new email thread for a new version of the patch.
Second, give at least 24h between the versions to have a chance being
reviewed by more people.
> if (chip->id == ID_AD7816 || chip->id == ID_AD7817) {
> + int val;
> +
> + ret = read_poll_timeout_atomic(gpiod_get_value, val, val <= 0,
While val <= 0 is technically correct, semantically it's harder to read,
it's better to use val < 0 || val == 0.
> + 5, 1000, false, chip->busy_pin);
This has an indentation issue now.
> + if (val < 0)
> + return val;
> + if (ret)
> + return ret;
--
With Best Regards,
Andy Shevchenko