[PATCH v2 3/4] iio: proximity: rfd77402: Move polling logic into helper

Shrikant Raskar posted 4 patches 1 day, 7 hours ago
[PATCH v2 3/4] iio: proximity: rfd77402: Move polling logic into helper
Posted by Shrikant Raskar 1 day, 6 hours ago
This change extracts the polling logic into a dedicated helper,
rfd77402_result_polling(), which improves readability and keeps
rfd77402_measure() focused on the high-level measurement flow.

This refactoring is also a necessary preparation step for adding
interrupt-based result handling in a follow-up patch.

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

diff --git a/drivers/iio/proximity/rfd77402.c b/drivers/iio/proximity/rfd77402.c
index 3262af6f6882..2152509816ca 100644
--- a/drivers/iio/proximity/rfd77402.c
+++ b/drivers/iio/proximity/rfd77402.c
@@ -110,11 +110,28 @@ 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_result_polling(struct i2c_client *client)
 {
 	int ret;
 	int tries = 10;
 
+	while (tries-- > 0) {
+		ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR);
+		if (ret < 0)
+			return ret;
+
+		if (ret & RFD77402_ICSR_RESULT)
+			return 0;
+
+		msleep(20);
+	}
+
+	return -ETIMEDOUT;
+}
+
+static int rfd77402_measure(struct i2c_client *client)
+{
+	int ret;
 	ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON,
 				 RFD77402_STATUS_MCPU_ON);
 	if (ret < 0)
@@ -125,20 +142,10 @@ static int rfd77402_measure(struct i2c_client *client)
 					RFD77402_CMD_VALID);
 	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;
+
+	ret = rfd77402_result_polling(client);
+	if (ret < 0)
 		goto err;
-	}
 
 	ret = i2c_smbus_read_word_data(client, RFD77402_RESULT_R);
 	if (ret < 0)
-- 
2.43.0
Re: [PATCH v2 3/4] iio: proximity: rfd77402: Move polling logic into helper
Posted by Andy Shevchenko 1 day, 5 hours ago
On Sun, Nov 30, 2025 at 5:37 PM Shrikant Raskar
<raskar.shree97@gmail.com> wrote:
>
> This change extracts the polling logic into a dedicated helper,

Imperative voice.

> rfd77402_result_polling(), which improves readability and keeps
> rfd77402_measure() focused on the high-level measurement flow.
>
> This refactoring is also a necessary preparation step for adding
> interrupt-based result handling in a follow-up patch.

...

> +       while (tries-- > 0) {
> +               ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR);
> +               if (ret < 0)
> +                       return ret;
> +
> +               if (ret & RFD77402_ICSR_RESULT)
> +                       return 0;
> +
> +               msleep(20);
> +       }
> +
> +       return -ETIMEDOUT;

Reimplementation of one macro from iopoll.h. Include that and use a
single call, will be something like

  return read_poll_timeout(...);

...

> +       ret = rfd77402_result_polling(client);

Ah, even better, you don't need a function for that at all. Just use
that macro here inline.

> +       if (ret < 0)
>                 goto err;

-- 
With Best Regards,
Andy Shevchenko