Skip to content

Commit 51eedb3

Browse files
embedded-essentialsjic23
authored andcommitted
iio: proximity: rfd77402: Use kernel helper for result polling
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> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 36bff18 commit 51eedb3

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

drivers/iio/proximity/rfd77402.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <linux/delay.h>
1414
#include <linux/i2c.h>
15+
#include <linux/iopoll.h>
1516
#include <linux/module.h>
1617

1718
#include <linux/iio/iio.h>
@@ -110,10 +111,31 @@ static int rfd77402_set_state(struct i2c_client *client, u8 state, u16 check)
110111
return 0;
111112
}
112113

113-
static int rfd77402_measure(struct i2c_client *client)
114+
static int rfd77402_wait_for_result(struct rfd77402_data *data)
114115
{
116+
struct i2c_client *client = data->client;
117+
int val, ret;
118+
119+
/*
120+
* As per RFD77402 datasheet section '3.1.1 Single Measure', the
121+
* suggested timeout value for single measure is 100ms.
122+
*/
123+
ret = read_poll_timeout(i2c_smbus_read_byte_data, val,
124+
(val < 0) || (val & RFD77402_ICSR_RESULT),
125+
10 * USEC_PER_MSEC,
126+
10 * 10 * USEC_PER_MSEC,
127+
false,
128+
client, RFD77402_ICSR);
129+
if (val < 0)
130+
return val;
131+
132+
return ret;
133+
}
134+
135+
static int rfd77402_measure(struct rfd77402_data *data)
136+
{
137+
struct i2c_client *client = data->client;
115138
int ret;
116-
int tries = 10;
117139

118140
ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON,
119141
RFD77402_STATUS_MCPU_ON);
@@ -126,23 +148,9 @@ static int rfd77402_measure(struct i2c_client *client)
126148
if (ret < 0)
127149
goto err;
128150

129-
while (tries-- > 0) {
130-
ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR);
131-
if (ret < 0)
132-
goto err;
133-
if (ret & RFD77402_ICSR_RESULT)
134-
break;
135-
/*
136-
* As per RFD77402 datasheet section '3.1.1 Single Measure',
137-
* the suggested timeout value for single measure is 100ms.
138-
*/
139-
msleep(10);
140-
}
141-
142-
if (tries < 0) {
143-
ret = -ETIMEDOUT;
151+
ret = rfd77402_wait_for_result(data);
152+
if (ret < 0)
144153
goto err;
145-
}
146154

147155
ret = i2c_smbus_read_word_data(client, RFD77402_RESULT_R);
148156
if (ret < 0)
@@ -172,7 +180,7 @@ static int rfd77402_read_raw(struct iio_dev *indio_dev,
172180
switch (mask) {
173181
case IIO_CHAN_INFO_RAW:
174182
mutex_lock(&data->lock);
175-
ret = rfd77402_measure(data->client);
183+
ret = rfd77402_measure(data);
176184
mutex_unlock(&data->lock);
177185
if (ret < 0)
178186
return ret;

0 commit comments

Comments
 (0)