public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: light: vcnl4000: use lock guard()
@ 2026-04-16 21:16 Raffael Raiel Trindade
  2026-04-17  8:19 ` Andy Shevchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Raffael Raiel Trindade @ 2026-04-16 21:16 UTC (permalink / raw)
  To: jic23, dlechner, nuno.sa, andy
  Cc: Raffael Raiel Trindade, Kim Carvalho, linux-iio

From: Raffael Raiel Trindade <raffaelraiel@usp.br>

Use guard() for handling mutex lock instead of manually locking and
unlocking. Remove gotos in error handling logic. This prevents forgotten
locks on early exits.

Signed-off-by: Raffael Raiel Trindade <raffaelraiel@usp.br>
Co-developed-by: Kim Carvalho <kim.ca@usp.br>
Signed-off-by: Kim Carvalho <kim.ca@usp.br>
---
 drivers/iio/light/vcnl4000.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 9650dbc41f2b..0201dd1cc957 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -18,6 +18,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/i2c.h>
@@ -288,11 +289,11 @@ static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en)
 {
 	int ret;
 
-	mutex_lock(&data->vcnl4000_lock);
+	guard(mutex)(&data->vcnl4000_lock);
 
 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF);
 	if (ret < 0)
-		goto out;
+		return ret;
 
 	if (en)
 		ret &= ~VCNL4040_ALS_CONF_ALS_SHUTDOWN;
@@ -301,9 +302,6 @@ static ssize_t vcnl4000_write_als_enable(struct vcnl4000_data *data, bool en)
 
 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, ret);
 
-out:
-	mutex_unlock(&data->vcnl4000_lock);
-
 	return ret;
 }
 
@@ -311,11 +309,11 @@ static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en)
 {
 	int ret;
 
-	mutex_lock(&data->vcnl4000_lock);
+	guard(mutex)(&data->vcnl4000_lock);
 
 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
 	if (ret < 0)
-		goto out;
+		return ret;
 
 	if (en)
 		ret &= ~VCNL4040_PS_CONF1_PS_SHUTDOWN;
@@ -324,9 +322,6 @@ static ssize_t vcnl4000_write_ps_enable(struct vcnl4000_data *data, bool en)
 
 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret);
 
-out:
-	mutex_unlock(&data->vcnl4000_lock);
-
 	return ret;
 }
 
@@ -680,19 +675,17 @@ static ssize_t vcnl4040_write_ps_it(struct vcnl4000_data *data, int val)
 
 	data->vcnl4200_ps.sampling_rate = ktime_set(0, val * 60 * NSEC_PER_USEC);
 
-	mutex_lock(&data->vcnl4000_lock);
+	guard(mutex)(&data->vcnl4000_lock);
 
 	ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
 	if (ret < 0)
-		goto out;
+		return ret;
 
 	regval = (ret & ~VCNL4040_PS_CONF2_PS_IT) |
 	    FIELD_PREP(VCNL4040_PS_CONF2_PS_IT, index);
 	ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1,
 					regval);
 
-out:
-	mutex_unlock(&data->vcnl4000_lock);
 	return ret;
 }
 
@@ -1500,13 +1493,13 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
 	u16 val, mask;
 	struct vcnl4000_data *data = iio_priv(indio_dev);
 
-	mutex_lock(&data->vcnl4000_lock);
+	guard(mutex)(&data->vcnl4000_lock);
 
 	switch (chan->type) {
 	case IIO_LIGHT:
 		ret = i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF);
 		if (ret < 0)
-			goto out;
+			return ret;
 
 		mask = VCNL4040_ALS_CONF_INT_EN;
 		if (state)
@@ -1521,7 +1514,7 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
 	case IIO_PROXIMITY:
 		ret = i2c_smbus_read_word_data(data->client, VCNL4200_PS_CONF1);
 		if (ret < 0)
-			goto out;
+			return ret;
 
 		if (dir == IIO_EV_DIR_RISING)
 			mask = VCNL4040_PS_IF_AWAY;
@@ -1538,9 +1531,6 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
 		break;
 	}
 
-out:
-	mutex_unlock(&data->vcnl4000_lock);
-
 	return ret;
 }
 
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-04-17  8:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-16 21:16 [PATCH] iio: light: vcnl4000: use lock guard() Raffael Raiel Trindade
2026-04-17  8:19 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox