Skip to contentSkip to conversation

iio: light: zopt2201: Convert mutex usage to lock guards

PATCH In review Gabriel Almeida 2 participants 7 messages Feb 17 – Feb 22 IIO

lore
Share & tools
lore
#0 lore raw

v3 cover iio: light: zopt2201: Convert mutex usage to lock guards

This series replaces mutex_lock()/mutex_unlock() pairs with the guard() and scoped_guard() helpers in the zopt2201 driver.

The first patch reorders the header includes to follow kernel style conventions. The second patch converts the remaining mutex usage to lock guards and simplifies the control flow accordingly.

Changes in v3:
- Split header reordering into a separate patch
- Simplified control flow in zopt2201_write_scale_by_idx()

Changes in v2:
- Reordered headers
- Added required <linux/cleanup.h> include
- Used lock guards in zopt2201_read()
- Used scoped_guard() in zopt2201_write_resolution()

Changes in v1:
- Initial version

Gabriel Almeida (2):
iio: light: zopt2201: Reorder header includes
iio: light: zopt2201: use lock guards

 drivers/iio/light/zopt2201.c | 44 ++++++++++++++----------------------
 1 file changed, 17 insertions(+), 27 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 iio: light: zopt2201: Reorder header includes

Reorder the header includes to follow the usual kernel ordering conventions and improve readability.

---
 drivers/iio/light/zopt2201.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/iio/light/zopt2201.c +5 -6
index 1dba1b949cc3..3192aa72a0fb 100644
@@ -10,15 +10,14 @@
  * TODO: interrupt support, ALS/UVB raw mode
  */
 
-#include <linux/module.h>
-#include <linux/i2c.h>
-#include <linux/mutex.h>
-#include <linux/err.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
-
+#include <linux/err.h>
+#include <linux/i2c.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
-
+#include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/unaligned.h>
 
 #define ZOPT2201_DRV_NAME "zopt2201"
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH v3 1/2] iio: light: zopt2201: Reorder header includes

Quoted 13 lines · On Tue, 17 Feb 2026 11:31:56 -0300 Gabriel Almeida <[email protected]>…

Reorder the header includes to follow the usual kernel ordering conventions and improve readability.

---
 drivers/iio/light/zopt2201.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/iio/light/zopt2201.c +0 -0
index 1dba1b949cc3..3192aa72a0fb 100644

drivers/iio/light/zopt2201.c line 13

Quoted 8 lines
drivers/iio/light/zopt2201.c +1 -4
@@ -10,15 +10,14 @@
  * TODO: interrupt support, ALS/UVB raw mode
  */
 
-#include <linux/module.h>
-#include <linux/i2c.h>
-#include <linux/mutex.h>
-#include <linux/err.h>
+#include <linux/cleanup.h>

I'll fix this up whilst applying to save time, but for future reference. This additional header is not used in patch 1 so it should only have come in with patch 2.

I'll move it.

Thanks,

Jonathan

Quoted 12 lines
drivers/iio/light/zopt2201.c +4 -2
 #include <linux/delay.h>
-
+#include <linux/err.h>
+#include <linux/i2c.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
-
+#include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/unaligned.h>
 
 #define ZOPT2201_DRV_NAME "zopt2201"
maintainer
#3 lore raw

Re: [PATCH v3 1/2] iio: light: zopt2201: Reorder header includes

In reply to Gabriel Almeida
Quoted 17 lines · On Tue, 17 Feb 2026 11:31:56 -0300 Gabriel Almeida <[email protected]>…

Reorder the header includes to follow the usual kernel ordering conventions and improve readability.

---
 drivers/iio/light/zopt2201.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/iio/light/zopt2201.c +0 -1
index 1dba1b949cc3..3192aa72a0fb 100644
@@ -10,15 +10,14 @@
  * TODO: interrupt support, ALS/UVB raw mode
  */
 
-#include <linux/module.h>

drivers/iio/light/zopt2201.c line 18

Quoted 10 lines
drivers/iio/light/zopt2201.c +3 -4
-#include <linux/i2c.h>
-#include <linux/mutex.h>
-#include <linux/err.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
-
+#include <linux/err.h>
+#include <linux/i2c.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>

Actually I decided to do a bit more. A common convetion is to have the IIO specific headers in an IIO drive in their own block.

drivers/iio/light/zopt2201.c line 21

Quoted 4 lines
drivers/iio/light/zopt2201.c +2 -1
-
+#include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/unaligned.h>

This ended up here just because of a global replace of asm/unaligned.h with linux/unaligned.h which should really have included moving which block it was in.

Anyhow, tweaked and applied

Thanks,

Jonathan

Quoted 2 lines
drivers/iio/light/zopt2201.c +0 -0
 #define ZOPT2201_DRV_NAME "zopt2201"
#4 lore raw

PATCH v3 2/2 iio: light: zopt2201: use lock guards

In reply to Gabriel Almeida

Use guard() and scoped_guard() to handle the mutex lock instead of manually locking and unlocking it.

---
 drivers/iio/light/zopt2201.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)
drivers/iio/light/zopt2201.c +12 -21
index 3192aa72a0fb..05a79ee1752a 100644
@@ -185,10 +185,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 +199,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[] = {
@@ -323,9 +318,9 @@ static int zopt2201_write_resolution(struct zopt2201_data *data,
 
 	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);
+			scoped_guard(mutex, &data->lock) {
+				ret = zopt2201_set_resolution(data, i);
+			}
 			return ret;
 		}
 
@@ -350,16 +345,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,
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH v3 2/2] iio: light: zopt2201: use lock guards

Quoted 4 lines · On Tue, 17 Feb 2026 11:31:57 -0300 Gabriel Almeida <[email protected]>…

Use guard() and scoped_guard() to handle the mutex lock instead of manually locking and unlocking it.

Applied with this tweak

drivers/iio/light/zopt2201.c +3 -4
index 01d01c6669ec..c2777b27bf6c 100644
@@ -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>
@@ -318,10 +319,8 @@ static int zopt2201_write_resolution(struct zopt2201_data *data,
 
        for (i = 0; i < ARRAY_SIZE(zopt2201_resolution); i++)
                if (val2 == zopt2201_resolution[i].us) {
-                       scoped_guard(mutex, &data->lock) {
-                               ret = zopt2201_set_resolution(data, i);
-                       }
-                       return ret;
+                       guard(mutex)(&data->lock);
+                       return zopt2201_set_resolution(data, i);
                }
 
        return -EINVAL;
Quoted 57 lines
---
 drivers/iio/light/zopt2201.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)
drivers/iio/light/zopt2201.c +6 -11
index 3192aa72a0fb..05a79ee1752a 100644
@@ -185,10 +185,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 +199,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[] = {

drivers/iio/light/zopt2201.c static int zopt2201_write_resolution(struct zopt2201_data *data, · line 321

Quoted 7 lines
drivers/iio/light/zopt2201.c +1 -3
@@ -323,9 +318,9 @@ static int zopt2201_write_resolution(struct zopt2201_data *data,
 
 	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);
+			scoped_guard(mutex, &data->lock) {

This is not a good use for scoped_guard() as it makes the code more confusing. We are exiting scope anyway with the return so

			guard(mutex)(&data->lock);
			return zopt2201_set_resolution(data, i);

is both easier to read and more compact.

Thanks,

Jonathan

Quoted 25 lines
drivers/iio/light/zopt2201.c +5 -7
+				ret = zopt2201_set_resolution(data, i);
+			}
 			return ret;
 		}
 
@@ -350,16 +345,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,
maintainer
#6 lore raw
In reply to Gabriel Almeida
Quoted 9 lines · On Tue, 17 Feb 2026 11:31:55 -0300 Gabriel Almeida <[email protected]>…

This series replaces mutex_lock()/mutex_unlock() pairs with the guard() and scoped_guard() helpers in the zopt2201 driver.

The first patch reorders the header includes to follow kernel style conventions. The second patch converts the remaining mutex usage to lock guards and simplifies the control flow accordingly.

Series applied with tweaks as mentioned in the replies to each patch.

Thanks,

Jonathan

Quoted 20 lines

Changes in v3:
- Split header reordering into a separate patch
- Simplified control flow in zopt2201_write_scale_by_idx()

Changes in v2:
- Reordered headers
- Added required <linux/cleanup.h> include
- Used lock guards in zopt2201_read()
- Used scoped_guard() in zopt2201_write_resolution()

Changes in v1:
- Initial version

Gabriel Almeida (2):
iio: light: zopt2201: Reorder header includes
iio: light: zopt2201: use lock guards

 drivers/iio/light/zopt2201.c | 44 ++++++++++++++----------------------
 1 file changed, 17 insertions(+), 27 deletions(-)