[PATCH] staging: iio: adc: ad7816: add timeout to busy-wait loop

From: Ruslan Valiyev

Date: Thu Feb 26 2026 - 00:27:02 EST


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() 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.

Also handle the case where gpiod_get_value() returns a negative
error code, which the original loop silently treated as not-busy.

Fixes: 7924425db04a ("staging: iio: adc: new driver for AD7816 devices")
Signed-off-by: Ruslan Valiyev <linuxoid@xxxxxxxxx>
---
drivers/staging/iio/adc/ad7816.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7816.c
index 172acf135..7b8a78e4d 100644
--- a/drivers/staging/iio/adc/ad7816.c
+++ b/drivers/staging/iio/adc/ad7816.c
@@ -8,6 +8,7 @@
#include <linux/interrupt.h>
#include <linux/gpio/consumer.h>
#include <linux/device.h>
+#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
@@ -85,8 +86,14 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
}

if (chip->id == ID_AD7816 || chip->id == ID_AD7817) {
- while (gpiod_get_value(chip->busy_pin))
- cpu_relax();
+ int val;
+
+ ret = read_poll_timeout(gpiod_get_value, val, val <= 0,
+ 5, 1000, false, chip->busy_pin);
+ if (val < 0)
+ return val;
+ if (ret)
+ return ret;
}

gpiod_set_value(chip->rdwr_pin, 0);
--
2.43.0