]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
iio: chemical: ccs811: Factor out handling of read of IIO_INFO_RAW to simplify error...
authorJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 31 Mar 2025 12:12:42 +0000 (13:12 +0100)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Tue, 22 Apr 2025 18:09:58 +0000 (19:09 +0100)
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>
drivers/iio/chemical/ccs811.c

index 451fb65dbe605610a8df94a5a228f668b5ea1e49..75b0cb05dd8686318523382daabd7266db94e11c 100644 (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;