[PATCH v8 3/6] iio: proximity: rfd77402: Use kernel helper for result polling

Shrikant Raskar via B4 Relay posted 6 patches 1 week, 4 days ago
[PATCH v8 3/6] iio: proximity: rfd77402: Use kernel helper for result polling
Posted by Shrikant Raskar via B4 Relay 1 week, 4 days 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.

Move the polling logic into a dedicated helper function, as it will
be reused by future updates.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Shrikant Raskar <raskar.shree97@gmail.com>
---
 drivers/iio/proximity/rfd77402.c | 42 ++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/drivers/iio/proximity/rfd77402.c b/drivers/iio/proximity/rfd77402.c
index 8f7debf0e61f..6b23fb3bfe1d 100644
--- a/drivers/iio/proximity/rfd77402.c
+++ b/drivers/iio/proximity/rfd77402.c
@@ -12,6 +12,7 @@
 
 #include <linux/delay.h>
 #include <linux/i2c.h>
+#include <linux/iopoll.h>
 #include <linux/module.h>
 
 #include <linux/iio/iio.h>
@@ -110,10 +111,27 @@ static int rfd77402_set_state(struct i2c_client *client, u8 state, u16 check)
 	return 0;
 }
 
-static int rfd77402_measure(struct i2c_client *client)
+static int rfd77402_wait_for_result(struct rfd77402_data *data)
 {
+	struct i2c_client *client = data->client;
+	int val, ret;
+
+	ret = read_poll_timeout(i2c_smbus_read_byte_data, val,
+				 (val < 0) || (val & RFD77402_ICSR_RESULT),
+				 10 * USEC_PER_MSEC,
+				 10 * 10 * USEC_PER_MSEC,
+				 false,
+				 client, RFD77402_ICSR);
+	if (val < 0)
+		return val;
+
+	return ret;
+}
+
+static int rfd77402_measure(struct rfd77402_data *data)
+{
+	struct i2c_client *client = data->client;
 	int ret;
-	int tries = 10;
 
 	ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON,
 				 RFD77402_STATUS_MCPU_ON);
@@ -126,23 +144,9 @@ 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;
-		/*
-		 * As per RFD77402 datasheet section '3.1.1 Single Measure',
-		 * the suggested timeout value for single measure is 100ms.
-		 */
-		msleep(10);
-	}
-
-	if (tries < 0) {
-		ret = -ETIMEDOUT;
+	ret = rfd77402_wait_for_result(data);
+	if (ret < 0)
 		goto err;
-	}
 
 	ret = i2c_smbus_read_word_data(client, RFD77402_RESULT_R);
 	if (ret < 0)
@@ -172,7 +176,7 @@ static int rfd77402_read_raw(struct iio_dev *indio_dev,
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		mutex_lock(&data->lock);
-		ret = rfd77402_measure(data->client);
+		ret = rfd77402_measure(data);
 		mutex_unlock(&data->lock);
 		if (ret < 0)
 			return ret;

-- 
2.43.0
Re: [PATCH v8 3/6] iio: proximity: rfd77402: Use kernel helper for result polling
Posted by Andy Shevchenko 1 week, 4 days ago
On Wed, Jan 28, 2026 at 11:21:50PM +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.
> 
> Move the polling logic into a dedicated helper function, as it will
> be reused by future updates.

...

> -static int rfd77402_measure(struct i2c_client *client)
> +static int rfd77402_wait_for_result(struct rfd77402_data *data)
>  {
> +	struct i2c_client *client = data->client;
> +	int val, ret;


Where is the newly introduced comment?

> +	ret = read_poll_timeout(i2c_smbus_read_byte_data, val,
> +				 (val < 0) || (val & RFD77402_ICSR_RESULT),
> +				 10 * USEC_PER_MSEC,
> +				 10 * 10 * USEC_PER_MSEC,
> +				 false,
> +				 client, RFD77402_ICSR);
> +	if (val < 0)
> +		return val;
> +
> +	return ret;
> +}

...

> -		/*
> -		 * As per RFD77402 datasheet section '3.1.1 Single Measure',
> -		 * the suggested timeout value for single measure is 100ms.
> -		 */

???

See above.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v8 3/6] iio: proximity: rfd77402: Use kernel helper for result polling
Posted by Jonathan Cameron 1 week, 3 days ago
On Thu, 29 Jan 2026 00:23:01 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Wed, Jan 28, 2026 at 11:21:50PM +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.
> > 
> > Move the polling logic into a dedicated helper function, as it will
> > be reused by future updates.  
> 
> ...
> 
> > -static int rfd77402_measure(struct i2c_client *client)
> > +static int rfd77402_wait_for_result(struct rfd77402_data *data)
> >  {
> > +	struct i2c_client *client = data->client;
> > +	int val, ret;  
> 
> 
> Where is the newly introduced comment?
I put it here whilst applying

J
> 
> > +	ret = read_poll_timeout(i2c_smbus_read_byte_data, val,
> > +				 (val < 0) || (val & RFD77402_ICSR_RESULT),
> > +				 10 * USEC_PER_MSEC,
> > +				 10 * 10 * USEC_PER_MSEC,
> > +				 false,
> > +				 client, RFD77402_ICSR);
> > +	if (val < 0)
> > +		return val;
> > +
> > +	return ret;
> > +}  
> 
> ...
> 
> > -		/*
> > -		 * As per RFD77402 datasheet section '3.1.1 Single Measure',
> > -		 * the suggested timeout value for single measure is 100ms.
> > -		 */  
> 
> ???
> 
> See above.
>