[PATCH v4 3/4] iio: proximity: rfd77402: Use kernel helper for result polling

Shrikant Raskar via B4 Relay posted 4 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH v4 3/4] iio: proximity: rfd77402: Use kernel helper for result polling
Posted by Shrikant Raskar via B4 Relay 1 month, 1 week ago
From: Shrikant Raskar <raskar.shree97@gmail.com>

Replace the manually written polling loop with read_poll_timeout(),
the kernel's standard helper for waiting on hardware status.
This makes the code easier to read and avoids repeating the same
polling code in the driver.

Signed-off-by: Shrikant Raskar <raskar.shree97@gmail.com>
---
 drivers/iio/proximity/rfd77402.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/drivers/iio/proximity/rfd77402.c b/drivers/iio/proximity/rfd77402.c
index 3262af6f6882..496c1412ebf8 100644
--- a/drivers/iio/proximity/rfd77402.c
+++ b/drivers/iio/proximity/rfd77402.c
@@ -13,6 +13,7 @@
 #include <linux/module.h>
 #include <linux/i2c.h>
 #include <linux/delay.h>
+#include <linux/iopoll.h>
 
 #include <linux/iio/iio.h>
 
@@ -113,7 +114,6 @@ static int rfd77402_set_state(struct i2c_client *client, u8 state, u16 check)
 static int rfd77402_measure(struct i2c_client *client)
 {
 	int ret;
-	int tries = 10;
 
 	ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON,
 				 RFD77402_STATUS_MCPU_ON);
@@ -126,19 +126,15 @@ static int rfd77402_measure(struct i2c_client *client)
 	if (ret < 0)
 		goto err;
 
-	while (tries-- > 0) {
-		ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR);
-		if (ret < 0)
-			goto err;
-		if (ret & RFD77402_ICSR_RESULT)
-			break;
-		msleep(20);
-	}
-
-	if (tries < 0) {
-		ret = -ETIMEDOUT;
+	/* Poll ICSR until RESULT bit is set */
+	ret = read_poll_timeout(i2c_smbus_read_byte_data, ret,
+				ret & RFD77402_ICSR_RESULT,
+				10000,    /* sleep: 10ms */
+				100000,   /* timeout: 100ms */
+				false,
+				client, RFD77402_ICSR);
+	if (ret < 0)
 		goto err;
-	}
 
 	ret = i2c_smbus_read_word_data(client, RFD77402_RESULT_R);
 	if (ret < 0)

-- 
2.43.0
Re: [PATCH v4 3/4] iio: proximity: rfd77402: Use kernel helper for result polling
Posted by Andy Shevchenko 1 month, 1 week ago
On Thu, Jan 01, 2026 at 09:47:40PM +0530, Shrikant Raskar via B4 Relay wrote:

> Replace the manually written polling loop with read_poll_timeout(),
> the kernel's standard helper for waiting on hardware status.
> This makes the code easier to read and avoids repeating the same
> polling code in the driver.

...

> index 3262af6f6882..496c1412ebf8 100644
> --- a/drivers/iio/proximity/rfd77402.c
> +++ b/drivers/iio/proximity/rfd77402.c
> @@ -13,6 +13,7 @@
>  #include <linux/module.h>
>  #include <linux/i2c.h>
>  #include <linux/delay.h>
> +#include <linux/iopoll.h>

Even if the original is unsorted, try to place the new inclusion into the
longest sorted (but maybe be sparse) chain. From the given context it might
be somewhere before module.h or less likely after i2c.h.

>  #include <linux/iio/iio.h>

...

> +	/* Poll ICSR until RESULT bit is set */
> +	ret = read_poll_timeout(i2c_smbus_read_byte_data, ret,
> +				ret & RFD77402_ICSR_RESULT,
> +				10000,    /* sleep: 10ms */
> +				100000,   /* timeout: 100ms */

Instead of the noisy comments use proper multipliers

 10 * USEC_PER_MSEC
 10 * 10 * USEC_PER_MSEC

(yes, 10 * 10 is to distinguish between one iteration delay and the whole op
 timeout.)

> +				false,
> +				client, RFD77402_ICSR);
> +	if (ret < 0)
>  		goto err;

-- 
With Best Regards,
Andy Shevchenko