iio: chemical: ccs811: Factor out handling of read of IIO_INFO_RAW to simplify error paths.

Factor out the implementation of this part of read_raw() and use
guard() to allow direct returns, simplifying both error and non error
paths.

Reviewed-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
Link: https://patch.msgid.link/20250331121317.1694135-3-jic23@kernel.org
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Jonathan Cameron
2025-03-31 13:12:42 +01:00
parent 36ee4794dd
commit fef6da136e

View File

@@ -15,6 +15,7 @@
* 4. Read error register and put the information in logs
*/
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
@@ -214,6 +215,40 @@ static int ccs811_get_measurement(struct ccs811_data *data)
return ret;
}
static int ccs811_read_info_raw(struct ccs811_data *data,
struct iio_chan_spec const *chan,
int *val, int mask)
{
int ret;
guard(mutex)(&data->lock);
ret = ccs811_get_measurement(data);
if (ret < 0)
return ret;
switch (chan->type) {
case IIO_VOLTAGE:
*val = be16_to_cpu(data->buffer.raw_data) & CCS811_VOLTAGE_MASK;
return IIO_VAL_INT;
case IIO_CURRENT:
*val = be16_to_cpu(data->buffer.raw_data) >> 10;
return IIO_VAL_INT;
case IIO_CONCENTRATION:
switch (chan->channel2) {
case IIO_MOD_CO2:
*val = be16_to_cpu(data->buffer.co2);
return IIO_VAL_INT;
case IIO_MOD_VOC:
*val = be16_to_cpu(data->buffer.voc);
return IIO_VAL_INT;
default:
return -EINVAL;
}
default:
return -EINVAL;
}
}
static int ccs811_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
@@ -226,42 +261,9 @@ static int ccs811_read_raw(struct iio_dev *indio_dev,
ret = iio_device_claim_direct_mode(indio_dev);
if (ret)
return ret;
mutex_lock(&data->lock);
ret = ccs811_get_measurement(data);
if (ret < 0) {
mutex_unlock(&data->lock);
iio_device_release_direct_mode(indio_dev);
return ret;
}
switch (chan->type) {
case IIO_VOLTAGE:
*val = be16_to_cpu(data->buffer.raw_data) &
CCS811_VOLTAGE_MASK;
ret = IIO_VAL_INT;
break;
case IIO_CURRENT:
*val = be16_to_cpu(data->buffer.raw_data) >> 10;
ret = IIO_VAL_INT;
break;
case IIO_CONCENTRATION:
switch (chan->channel2) {
case IIO_MOD_CO2:
*val = be16_to_cpu(data->buffer.co2);
ret = IIO_VAL_INT;
break;
case IIO_MOD_VOC:
*val = be16_to_cpu(data->buffer.voc);
ret = IIO_VAL_INT;
break;
default:
ret = -EINVAL;
}
break;
default:
ret = -EINVAL;
}
mutex_unlock(&data->lock);
ret = ccs811_read_info_raw(data, chan, val, mask);
iio_device_release_direct_mode(indio_dev);
return ret;