[PATCH v2 1/4] iio: vcnl4000: Factorize data reading and writing.

From: Mathieu Othacehe
Date: Mon Apr 06 2020 - 10:54:09 EST


Factorize data reading in vcnl4000_measure into a vcnl4000_read_block_data
function. Use it to provide a vcnl4000_read_data function that is able to
read sensor data under lock. Also add a vcnl4000_write_data function.

Signed-off-by: Mathieu Othacehe <m.othacehe@xxxxxxxxx>
---
drivers/iio/light/vcnl4000.c | 54 +++++++++++++++++++++++++++++++++---
1 file changed, 50 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index e5b00a6611ac..c95b89740dfa 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -189,11 +189,59 @@ static int vcnl4200_init(struct vcnl4000_data *data)
return 0;
};

+static int vcnl4000_read_block_data(struct vcnl4000_data *data, u8 data_reg,
+ int *val)
+{
+ __be16 buf;
+ int ret;
+
+ ret = i2c_smbus_read_i2c_block_data(data->client,
+ data_reg, sizeof(buf), (u8 *) &buf);
+ if (ret < 0)
+ goto end;
+
+ *val = be16_to_cpu(buf);
+end:
+ return ret;
+}
+
+static int vcnl4000_read_data(struct vcnl4000_data *data, u8 data_reg,
+ int *val)
+{
+ int ret;
+
+ mutex_lock(&data->vcnl4000_lock);
+ ret = vcnl4000_read_block_data(data, data_reg, val);
+ mutex_unlock(&data->vcnl4000_lock);
+
+ return ret;
+}
+
+static int vcnl4000_write_data(struct vcnl4000_data *data, u8 data_reg,
+ u16 val)
+{
+ int ret;
+
+ if (val > U16_MAX)
+ return -ERANGE;
+
+ mutex_lock(&data->vcnl4000_lock);
+
+ ret = i2c_smbus_write_byte_data(data->client, data_reg, val >> 8);
+ if (ret < 0)
+ goto end;
+
+ ret = i2c_smbus_write_byte_data(data->client, data_reg + 1, val);
+end:
+ mutex_unlock(&data->vcnl4000_lock);
+
+ return ret;
+}
+
static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
u8 rdy_mask, u8 data_reg, int *val)
{
int tries = 20;
- __be16 buf;
int ret;

mutex_lock(&data->vcnl4000_lock);
@@ -220,13 +268,11 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
goto fail;
}

- ret = i2c_smbus_read_i2c_block_data(data->client,
- data_reg, sizeof(buf), (u8 *) &buf);
+ ret = vcnl4000_read_block_data(data, data_reg, val);
if (ret < 0)
goto fail;

mutex_unlock(&data->vcnl4000_lock);
- *val = be16_to_cpu(buf);

return 0;

--
2.26.0