From: Pedro Barletta Gennari <pedro.pbg@usp.br>
To: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
andy@kernel.org
Cc: pedro.pbg@usp.br, linux-iio@vger.kernel.org
Subject: [PATCH] iio: light: iqs621-als: use lock guards
Date: Sun, 19 Apr 2026 18:31:23 -0300 [thread overview]
Message-ID: <20260419213202.68356-1-pedro.pbg@usp.br> (raw)
Use guard(mutex)() for handling mutex lock instead of
manually locking and unlocking the mutex. This prevents forgotten
locks due to early exits and remove the need of gotos.
Signed-off-by: Pedro Barletta Gennari <pedro.pbg@usp.br>
---
drivers/iio/light/iqs621-als.c | 83 ++++++++++++----------------------
1 file changed, 28 insertions(+), 55 deletions(-)
diff --git a/drivers/iio/light/iqs621-als.c b/drivers/iio/light/iqs621-als.c
index b9f230210f07..222a42036799 100644
--- a/drivers/iio/light/iqs621-als.c
+++ b/drivers/iio/light/iqs621-als.c
@@ -15,6 +15,7 @@
#include <linux/notifier.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
+#include <linux/cleanup.h>
#define IQS621_ALS_FLAGS_LIGHT BIT(7)
#define IQS621_ALS_FLAGS_RANGE GENMASK(3, 0)
@@ -107,25 +108,22 @@ static int iqs621_als_notifier(struct notifier_block *notifier,
indio_dev = iqs621_als->indio_dev;
timestamp = iio_get_time_ns(indio_dev);
- mutex_lock(&iqs621_als->lock);
+ guard(mutex)(&iqs621_als->lock);
if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
ret = iqs621_als_init(iqs621_als);
if (ret) {
dev_err(indio_dev->dev.parent,
"Failed to re-initialize device: %d\n", ret);
- ret = NOTIFY_BAD;
+ return NOTIFY_BAD;
} else {
- ret = NOTIFY_OK;
+ return NOTIFY_OK;
}
-
- goto err_mutex;
}
if (!iqs621_als->light_en && !iqs621_als->range_en &&
!iqs621_als->prox_en) {
- ret = NOTIFY_DONE;
- goto err_mutex;
+ return NOTIFY_DONE;
}
/* IQS621 only */
@@ -181,12 +179,7 @@ static int iqs621_als_notifier(struct notifier_block *notifier,
iqs621_als->als_flags = event_data->als_flags;
iqs621_als->ir_flags = event_data->ir_flags;
- ret = NOTIFY_OK;
-
-err_mutex:
- mutex_unlock(&iqs621_als->lock);
-
- return ret;
+ return NOTIFY_OK;
}
static void iqs621_als_notifier_unregister(void *context)
@@ -241,30 +234,22 @@ static int iqs621_als_read_event_config(struct iio_dev *indio_dev,
enum iio_event_direction dir)
{
struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
- int ret;
- mutex_lock(&iqs621_als->lock);
+ guard(mutex)(&iqs621_als->lock);
switch (chan->type) {
case IIO_LIGHT:
- ret = iqs621_als->light_en;
- break;
+ return iqs621_als->light_en;
case IIO_INTENSITY:
- ret = iqs621_als->range_en;
- break;
+ return iqs621_als->range_en;
case IIO_PROXIMITY:
- ret = iqs621_als->prox_en;
- break;
+ return iqs621_als->prox_en;
default:
- ret = -EINVAL;
+ return -EINVAL;
}
-
- mutex_unlock(&iqs621_als->lock);
-
- return ret;
}
static int iqs621_als_write_event_config(struct iio_dev *indio_dev,
@@ -278,11 +263,11 @@ static int iqs621_als_write_event_config(struct iio_dev *indio_dev,
unsigned int val;
int ret;
- mutex_lock(&iqs621_als->lock);
+ guard(mutex)(&iqs621_als->lock);
ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->als_flags, &val);
if (ret)
- goto err_mutex;
+ return ret;
iqs621_als->als_flags = val;
switch (chan->type) {
@@ -293,7 +278,7 @@ static int iqs621_als_write_event_config(struct iio_dev *indio_dev,
0xFF);
if (!ret)
iqs621_als->light_en = state;
- break;
+ return ret;
case IIO_INTENSITY:
ret = regmap_update_bits(iqs62x->regmap, IQS620_GLBL_EVENT_MASK,
@@ -302,12 +287,12 @@ static int iqs621_als_write_event_config(struct iio_dev *indio_dev,
0xFF);
if (!ret)
iqs621_als->range_en = state;
- break;
+ return ret;
case IIO_PROXIMITY:
ret = regmap_read(iqs62x->regmap, IQS622_IR_FLAGS, &val);
if (ret)
- goto err_mutex;
+ return ret;
iqs621_als->ir_flags = val;
ret = regmap_update_bits(iqs62x->regmap, IQS620_GLBL_EVENT_MASK,
@@ -315,16 +300,11 @@ static int iqs621_als_write_event_config(struct iio_dev *indio_dev,
state ? 0 : 0xFF);
if (!ret)
iqs621_als->prox_en = state;
- break;
+ return ret;
default:
- ret = -EINVAL;
+ return -EINVAL;
}
-
-err_mutex:
- mutex_unlock(&iqs621_als->lock);
-
- return ret;
}
static int iqs621_als_read_event_value(struct iio_dev *indio_dev,
@@ -337,31 +317,27 @@ static int iqs621_als_read_event_value(struct iio_dev *indio_dev,
struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
int ret = IIO_VAL_INT;
- mutex_lock(&iqs621_als->lock);
+ guard(mutex)(&iqs621_als->lock);
switch (dir) {
case IIO_EV_DIR_RISING:
*val = iqs621_als->thresh_light * 16;
- break;
+ return ret;
case IIO_EV_DIR_FALLING:
*val = iqs621_als->thresh_dark * 4;
- break;
+ return ret;
case IIO_EV_DIR_EITHER:
if (iqs621_als->ir_flags_mask == IQS622_IR_FLAGS_TOUCH)
*val = iqs621_als->thresh_prox * 4;
else
*val = iqs621_als->thresh_prox;
- break;
+ return ret;
default:
- ret = -EINVAL;
+ return -EINVAL;
}
-
- mutex_unlock(&iqs621_als->lock);
-
- return ret;
}
static int iqs621_als_write_event_value(struct iio_dev *indio_dev,
@@ -377,7 +353,7 @@ static int iqs621_als_write_event_value(struct iio_dev *indio_dev,
u8 ir_flags_mask, *thresh_cache;
int ret = -EINVAL;
- mutex_lock(&iqs621_als->lock);
+ guard(mutex)(&iqs621_als->lock);
switch (dir) {
case IIO_EV_DIR_RISING:
@@ -426,29 +402,26 @@ static int iqs621_als_write_event_value(struct iio_dev *indio_dev,
break;
default:
- goto err_mutex;
+ return ret;
}
thresh_cache = &iqs621_als->thresh_prox;
break;
default:
- goto err_mutex;
+ return ret;
}
if (thresh_val > 0xFF)
- goto err_mutex;
+ return ret;
ret = regmap_write(iqs62x->regmap, thresh_reg, thresh_val);
if (ret)
- goto err_mutex;
+ return ret;
*thresh_cache = thresh_val;
iqs621_als->ir_flags_mask = ir_flags_mask;
-err_mutex:
- mutex_unlock(&iqs621_als->lock);
-
return ret;
}
--
2.51.0
next reply other threads:[~2026-04-19 21:33 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-19 21:31 Pedro Barletta Gennari [this message]
2026-04-20 8:45 ` [PATCH] iio: light: iqs621-als: use lock guards Andy Shevchenko
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=20260419213202.68356-1-pedro.pbg@usp.br \
--to=pedro.pbg@usp.br \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--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