Commit 9b1de75b authored by Adriana Reus's avatar Adriana Reus Committed by Jonathan Cameron

iio: light: us5182d: Refactor read_raw function

A bit of refactoring for better readability.
Moved and slightly reorganized all the activity necessary for reading als
and proximity into a different function. This way the switch in read raw
becomes clearer and more compact.
Signed-off-by: default avatarAdriana Reus <adriana.reus@intel.com>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent b6695254
......@@ -203,23 +203,6 @@ static const struct iio_chan_spec us5182d_channels[] = {
}
};
static int us5182d_get_als(struct us5182d_data *data)
{
int ret;
unsigned long result;
ret = i2c_smbus_read_word_data(data->client,
US5182D_REG_ADL);
if (ret < 0)
return ret;
result = ret * data->ga / US5182D_GA_RESOLUTION;
if (result > 0xffff)
result = 0xffff;
return result;
}
static int us5182d_oneshot_en(struct us5182d_data *data)
{
int ret;
......@@ -324,6 +307,39 @@ static int us5182d_px_enable(struct us5182d_data *data)
return 0;
}
static int us5182d_get_als(struct us5182d_data *data)
{
int ret;
unsigned long result;
ret = us5182d_als_enable(data);
if (ret < 0)
return ret;
ret = i2c_smbus_read_word_data(data->client,
US5182D_REG_ADL);
if (ret < 0)
return ret;
result = ret * data->ga / US5182D_GA_RESOLUTION;
if (result > 0xffff)
result = 0xffff;
return result;
}
static int us5182d_get_px(struct us5182d_data *data)
{
int ret;
ret = us5182d_px_enable(data);
if (ret < 0)
return ret;
return i2c_smbus_read_word_data(data->client,
US5182D_REG_PDL);
}
static int us5182d_shutdown_en(struct us5182d_data *data, u8 state)
{
int ret;
......@@ -370,85 +386,70 @@ static int us5182d_set_power_state(struct us5182d_data *data, bool on)
return ret;
}
static int us5182d_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
static int us5182d_read_value(struct us5182d_data *data,
struct iio_chan_spec const *chan)
{
struct us5182d_data *data = iio_priv(indio_dev);
int ret;
int ret, value;
switch (mask) {
case IIO_CHAN_INFO_RAW:
switch (chan->type) {
case IIO_LIGHT:
mutex_lock(&data->lock);
if (data->power_mode == US5182D_ONESHOT) {
ret = us5182d_oneshot_en(data);
if (ret < 0)
goto out_err;
}
ret = us5182d_set_power_state(data, true);
if (ret < 0)
goto out_err;
ret = us5182d_als_enable(data);
if (ret < 0)
goto out_poweroff;
if (chan->type == IIO_LIGHT)
ret = us5182d_get_als(data);
else
ret = us5182d_get_px(data);
if (ret < 0)
goto out_poweroff;
*val = ret;
value = ret;
ret = us5182d_set_power_state(data, false);
if (ret < 0)
goto out_err;
mutex_unlock(&data->lock);
return IIO_VAL_INT;
case IIO_PROXIMITY:
mutex_lock(&data->lock);
if (data->power_mode == US5182D_ONESHOT) {
ret = us5182d_oneshot_en(data);
if (ret < 0)
goto out_err;
}
ret = us5182d_set_power_state(data, true);
if (ret < 0)
goto out_err;
ret = us5182d_px_enable(data);
if (ret < 0)
goto out_poweroff;
ret = i2c_smbus_read_word_data(data->client,
US5182D_REG_PDL);
return value;
out_poweroff:
us5182d_set_power_state(data, false);
out_err:
mutex_unlock(&data->lock);
return ret;
}
static int us5182d_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val,
int *val2, long mask)
{
struct us5182d_data *data = iio_priv(indio_dev);
int ret;
switch (mask) {
case IIO_CHAN_INFO_RAW:
ret = us5182d_read_value(data, chan);
if (ret < 0)
goto out_poweroff;
return ret;
*val = ret;
ret = us5182d_set_power_state(data, false);
if (ret < 0)
goto out_err;
mutex_unlock(&data->lock);
return IIO_VAL_INT;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_SCALE:
ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG1);
if (ret < 0)
return ret;
*val = 0;
*val2 = us5182d_scales[ret & US5182D_AGAIN_MASK];
return IIO_VAL_INT_PLUS_MICRO;
default:
return -EINVAL;
}
return -EINVAL;
out_poweroff:
us5182d_set_power_state(data, false);
out_err:
mutex_unlock(&data->lock);
return ret;
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment