]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
iio: light: zopt2201: use lock guards
authorGabriel Almeida <gabrielsousa230@gmail.com>
Tue, 17 Feb 2026 14:31:57 +0000 (11:31 -0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 23 Feb 2026 08:24:24 +0000 (08:24 +0000)
Use guard() and scoped_guard() to handle the mutex lock instead of
manually locking and unlocking it.

Signed-off-by: Gabriel Almeida <gabrielsousa230@gmail.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/light/zopt2201.c

index b7dd20fc20d796422e33453b7b179190a07c9102..0990e4d266ebe706ad606b3c5288d1f6794cadc5 100644 (file)
@@ -10,6 +10,7 @@
  * TODO: interrupt support, ALS/UVB raw mode
  */
 
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/i2c.h>
@@ -185,10 +186,10 @@ static int zopt2201_read(struct zopt2201_data *data, u8 reg)
        u8 buf[3];
        int ret;
 
-       mutex_lock(&data->lock);
+       guard(mutex)(&data->lock);
        ret = zopt2201_enable_mode(data, reg == ZOPT2201_UVB_DATA);
        if (ret < 0)
-               goto fail;
+               return ret;
 
        while (tries--) {
                unsigned long t = zopt2201_resolution[data->res].us;
@@ -199,30 +200,25 @@ static int zopt2201_read(struct zopt2201_data *data, u8 reg)
                        msleep(t / 1000);
                ret = i2c_smbus_read_byte_data(client, ZOPT2201_MAIN_STATUS);
                if (ret < 0)
-                       goto fail;
+                       return ret;
                if (ret & ZOPT2201_MAIN_STATUS_DRDY)
                        break;
        }
 
        if (tries < 0) {
                ret = -ETIMEDOUT;
-               goto fail;
+               return ret;
        }
 
        ret = i2c_smbus_read_i2c_block_data(client, reg, sizeof(buf), buf);
        if (ret < 0)
-               goto fail;
+               return ret;
 
        ret = i2c_smbus_write_byte_data(client, ZOPT2201_MAIN_CTRL, 0x00);
        if (ret < 0)
-               goto fail;
-       mutex_unlock(&data->lock);
+               return ret;
 
        return get_unaligned_le24(&buf[0]);
-
-fail:
-       mutex_unlock(&data->lock);
-       return ret;
 }
 
 static const struct iio_chan_spec zopt2201_channels[] = {
@@ -316,17 +312,15 @@ static int zopt2201_set_resolution(struct zopt2201_data *data, u8 res)
 static int zopt2201_write_resolution(struct zopt2201_data *data,
                                     int val, int val2)
 {
-       int i, ret;
+       int i;
 
        if (val != 0)
                return -EINVAL;
 
        for (i = 0; i < ARRAY_SIZE(zopt2201_resolution); i++)
                if (val2 == zopt2201_resolution[i].us) {
-                       mutex_lock(&data->lock);
-                       ret = zopt2201_set_resolution(data, i);
-                       mutex_unlock(&data->lock);
-                       return ret;
+                       guard(mutex)(&data->lock);
+                       return zopt2201_set_resolution(data, i);
                }
 
        return -EINVAL;
@@ -350,16 +344,12 @@ static int zopt2201_write_scale_by_idx(struct zopt2201_data *data, int idx,
 {
        int ret;
 
-       mutex_lock(&data->lock);
+       guard(mutex)(&data->lock);
        ret = zopt2201_set_resolution(data, zopt2201_scale_array[idx].res);
        if (ret < 0)
-               goto unlock;
-
-       ret = zopt2201_set_gain(data, zopt2201_scale_array[idx].gain);
+               return ret;
 
-unlock:
-       mutex_unlock(&data->lock);
-       return ret;
+       return zopt2201_set_gain(data, zopt2201_scale_array[idx].gain);
 }
 
 static int zopt2201_write_scale_als(struct zopt2201_data *data,