From: Raffael Raiel Trindade <raffaelraiel@usp.br>
To: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
andy@kernel.org
Cc: Raffael Raiel Trindade <raffaelraiel@usp.br>,
Kim Carvalho <kim.ca@usp.br>,
linux-iio@vger.kernel.org
Subject: [PATCH v3] iio: light: vcnl4000: use lock guard()
Date: Mon, 20 Apr 2026 16:59:33 -0300 [thread overview]
Message-ID: <20260420200047.102159-1-raffaelraiel@usp.br> (raw)
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>
---
v3:
- fix indentation problems on line breaks
v2:
- remove unnecessary ret attributions on non-error logic
- remove breaks on switch-case after return
drivers/iio/light/vcnl4000.c | 55 +++++++++++-------------------------
1 file changed, 16 insertions(+), 39 deletions(-)
diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index c08927e34b4e..023176ff14c7 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>
@@ -268,46 +269,36 @@ 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;
else
ret |= VCNL4040_ALS_CONF_ALS_SHUTDOWN;
- ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, ret);
-
-out:
- mutex_unlock(&data->vcnl4000_lock);
-
- return ret;
+ return i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, ret);
}
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;
else
ret |= VCNL4040_PS_CONF1_PS_SHUTDOWN;
- ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret);
-
-out:
- mutex_unlock(&data->vcnl4000_lock);
-
- return ret;
+ return i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, ret);
}
static int vcnl4200_set_power_state(struct vcnl4000_data *data, bool on)
@@ -660,20 +651,15 @@ 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;
+ return i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, regval);
}
static ssize_t vcnl4040_read_als_period(struct vcnl4000_data *data, int *val, int *val2)
@@ -1480,13 +1466,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)
@@ -1495,13 +1481,11 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
val = (ret & ~mask);
data->als_int = FIELD_GET(VCNL4040_ALS_CONF_INT_EN, val);
- ret = i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF,
- val);
- break;
+ return i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, val);
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;
@@ -1511,17 +1495,10 @@ static int vcnl4040_write_event_config(struct iio_dev *indio_dev,
val = state ? (ret | mask) : (ret & ~mask);
data->ps_int = FIELD_GET(VCNL4040_PS_CONF2_PS_INT, val);
- ret = i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1,
- val);
- break;
+ return i2c_smbus_write_word_data(data->client, VCNL4200_PS_CONF1, val);
default:
- break;
+ return ret;
}
-
-out:
- mutex_unlock(&data->vcnl4000_lock);
-
- return ret;
}
static irqreturn_t vcnl4040_irq_thread(int irq, void *p)
--
2.51.0
next reply other threads:[~2026-04-20 20:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-20 19:59 Raffael Raiel Trindade [this message]
2026-04-20 20:19 ` [PATCH v3] iio: light: vcnl4000: use lock guard() Andy Shevchenko
2026-04-20 20:26 ` Andy Shevchenko
2026-04-20 20:40 ` Raffael Raiel Trindade
2026-04-21 14:49 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260420200047.102159-1-raffaelraiel@usp.br \
--to=raffaelraiel@usp.br \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kim.ca@usp.br \
--cc=linux-iio@vger.kernel.org \
--cc=nuno.sa@analog.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox