[PATCH 3/3] iio: light: tcs3472: Use guard(mutex)() family over manual locking

Aldo Conte
2026-05-06T09:43:18Z

Convert tcs3472_read_event_config, tcs3472_write_event_config,
tcs3472_write_event, tcs3472_powerdown and tcs3472_resume to use
automatico cleanup with guard(mutex)() instead of the old manual
locking method.

Found by code inspection.

Tested on a Raspberry Pi 3B with a TCS34725 at 0x29 address.
The following fields were read and written without any issues:
sampling_frequency, integration_time, calibscale and the
threshold event interface.
The unload of the driver works cleanly.

Signed-off-by: Aldo Conte <aldocontelk@gmail.com>
---
drivers/iio/light/tcs3472.c | 39 ++++++++++++-------------------------
1 file changed, 12 insertions(+), 27 deletions(-)

diff --git a/drivers/iio/light/tcs3472.c b/drivers/iio/light/tcs3472.c
index de51eb61f42a..90552f47a373 100644
--- a/drivers/iio/light/tcs3472.c
+++ b/drivers/iio/light/tcs3472.c
@@ -393,7 +393,7 @@ static int tcs3472_write_event(struct iio_dev *indio_dev,
int period;
int i;

- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);
switch (info) {
case IIO_EV_INFO_VALUE:
switch (dir) {
@@ -404,18 +404,17 @@ static int tcs3472_write_event(struct iio_dev *indio_dev,
command = TCS3472_AILT;
break;
default:
- ret = -EINVAL;
- goto error;
+ return -EINVAL;
}
ret = i2c_smbus_write_word_data(data->client, command, val);
if (ret)
- goto error;
+ return ret;

if (dir == IIO_EV_DIR_RISING)
data->high_thresh = val;
else
data->low_thresh = val;
- break;
+ return 0;
case IIO_EV_INFO_PERIOD:
period = val * USEC_PER_SEC + val2;
for (i = 1; i < ARRAY_SIZE(tcs3472_intr_pers) - 1; i++) {
@@ -425,18 +424,13 @@ static int tcs3472_write_event(struct iio_dev *indio_dev,
}
ret = i2c_smbus_write_byte_data(data->client, TCS3472_PERS, i);
if (ret)
- goto error;
+ return ret;

data->apers = i;
- break;
+ return 0;
default:
- ret = -EINVAL;
- break;
+ return -EINVAL;
}
-error:
- mutex_unlock(&data->lock);
-
- return ret;
}

static int tcs3472_read_event_config(struct iio_dev *indio_dev,
@@ -444,13 +438,9 @@ static int tcs3472_read_event_config(struct iio_dev *indio_dev,
enum iio_event_direction dir)
{
struct tcs3472_data *data = iio_priv(indio_dev);
- int ret;

- mutex_lock(&data->lock);
- ret = !!(data->enable & TCS3472_ENABLE_AIEN);
- mutex_unlock(&data->lock);
-
- return ret;
+ guard(mutex)(&data->lock);
+ return !!(data->enable & TCS3472_ENABLE_AIEN);
}

static int tcs3472_write_event_config(struct iio_dev *indio_dev,
@@ -461,7 +451,7 @@ static int tcs3472_write_event_config(struct iio_dev *indio_dev,
int ret = 0;
u8 enable_old;

- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);

enable_old = data->enable;

@@ -476,7 +466,6 @@ static int tcs3472_write_event_config(struct iio_dev *indio_dev,
if (ret)
data->enable = enable_old;
}
- mutex_unlock(&data->lock);

return ret;
}
@@ -580,15 +569,13 @@ static int tcs3472_powerdown(struct tcs3472_data *data)
u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON |
TCS3472_ENABLE_WEN;

- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);

ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
data->enable & ~enable_mask);
if (!ret)
data->enable &= ~enable_mask;

- mutex_unlock(&data->lock);
-
return ret;
}

@@ -722,15 +709,13 @@ static int tcs3472_resume(struct device *dev)
u8 enable_mask = TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON |
TCS3472_ENABLE_WEN;

- mutex_lock(&data->lock);
+ guard(mutex)(&data->lock);

ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE,
data->enable | enable_mask);
if (!ret)
data->enable |= enable_mask;

- mutex_unlock(&data->lock);
-
return ret;
}

--
2.54.0