From: rafasales@usp.br
To: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
andy@kernel.org
Cc: "Rafael B. Sales" <rafasales@usp.br>,
"Gustavo C. Arakaki" <gustavo.arakaki@usp.br>,
linux-iio@vger.kernel.org
Subject: [PATCH] iio: light: ltr501: Use automatic cleanup of locks
Date: Wed, 15 Apr 2026 17:33:30 -0300 [thread overview]
Message-ID: <20260415203345.195557-1-rafasales@usp.br> (raw)
From: "Rafael B. Sales" <rafasales@usp.br>
Replace `mutex_lock` and `mutex_unlock` with `guard(mutex)` and
scoped_guard(mutex)` to reduce boilerplate and allow for
easy-to-read code blocks.
Signed-off-by: Rafael B. Sales <rafasales@usp.br>
Co-developed-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
Signed-off-by: Gustavo C. Arakaki <gustavo.arakaki@usp.br>
---
drivers/iio/light/ltr501.c | 44 +++++++++++++-------------------------
1 file changed, 15 insertions(+), 29 deletions(-)
diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 4d99ae336f61..b9aae948ad3b 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -242,9 +242,8 @@ static int ltr501_als_write_samp_freq(struct ltr501_data *data,
if (i < 0)
return i;
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = regmap_field_write(data->reg_als_rate, i);
- mutex_unlock(&data->lock_als);
return ret;
}
@@ -261,9 +260,8 @@ static int ltr501_ps_write_samp_freq(struct ltr501_data *data,
if (i < 0)
return i;
- mutex_lock(&data->lock_ps);
+ guard(mutex)(&data->lock_ps);
ret = regmap_field_write(data->reg_ps_rate, i);
- mutex_unlock(&data->lock_ps);
return ret;
}
@@ -487,9 +485,8 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
if (new_val < 0 || new_val > 0x0f)
return -EINVAL;
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = regmap_field_write(data->reg_als_prst, new_val);
- mutex_unlock(&data->lock_als);
if (ret >= 0)
data->als_period = period;
@@ -507,9 +504,8 @@ static int ltr501_write_intr_prst(struct ltr501_data *data,
if (new_val < 0 || new_val > 0x0f)
return -EINVAL;
- mutex_lock(&data->lock_ps);
+ guard(mutex)(&data->lock_ps);
ret = regmap_field_write(data->reg_ps_prst, new_val);
- mutex_unlock(&data->lock_ps);
if (ret >= 0)
data->ps_period = period;
@@ -651,18 +647,16 @@ static int ltr501_read_info_raw(struct ltr501_data *data,
switch (chan->type) {
case IIO_INTENSITY:
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = ltr501_read_als(data, buf);
- mutex_unlock(&data->lock_als);
if (ret < 0)
return ret;
*val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ?
buf[0] : buf[1]);
return IIO_VAL_INT;
case IIO_PROXIMITY:
- mutex_lock(&data->lock_ps);
+ guard(mutex)(&data->lock_ps);
ret = ltr501_read_ps(data);
- mutex_unlock(&data->lock_ps);
if (ret < 0)
return ret;
*val = ret & LTR501_PS_DATA_MASK;
@@ -687,9 +681,8 @@ static int ltr501_read_raw(struct iio_dev *indio_dev,
if (!iio_device_claim_direct(indio_dev))
return -EBUSY;
- mutex_lock(&data->lock_als);
- ret = ltr501_read_als(data, buf);
- mutex_unlock(&data->lock_als);
+ scoped_guard(mutex, &data->lock_als)
+ ret = ltr501_read_als(data, buf);
iio_device_release_direct(indio_dev);
if (ret < 0)
return ret;
@@ -802,9 +795,8 @@ static int __ltr501_write_raw(struct iio_dev *indio_dev,
if (val != 0)
return -EINVAL;
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = ltr501_set_it_time(data, val2);
- mutex_unlock(&data->lock_als);
return ret;
default:
return -EINVAL;
@@ -953,18 +945,16 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
return -EINVAL;
switch (dir) {
case IIO_EV_DIR_RISING:
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = regmap_bulk_write(data->regmap,
LTR501_ALS_THRESH_UP,
&val, 2);
- mutex_unlock(&data->lock_als);
return ret;
case IIO_EV_DIR_FALLING:
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = regmap_bulk_write(data->regmap,
LTR501_ALS_THRESH_LOW,
&val, 2);
- mutex_unlock(&data->lock_als);
return ret;
default:
return -EINVAL;
@@ -974,18 +964,16 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
return -EINVAL;
switch (dir) {
case IIO_EV_DIR_RISING:
- mutex_lock(&data->lock_ps);
+ guard(mutex)(&data->lock_ps);
ret = regmap_bulk_write(data->regmap,
LTR501_PS_THRESH_UP,
&val, 2);
- mutex_unlock(&data->lock_ps);
return ret;
case IIO_EV_DIR_FALLING:
- mutex_lock(&data->lock_ps);
+ guard(mutex)(&data->lock_ps);
ret = regmap_bulk_write(data->regmap,
LTR501_PS_THRESH_LOW,
&val, 2);
- mutex_unlock(&data->lock_ps);
return ret;
default:
return -EINVAL;
@@ -1082,14 +1070,12 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,
switch (chan->type) {
case IIO_INTENSITY:
- mutex_lock(&data->lock_als);
+ guard(mutex)(&data->lock_als);
ret = regmap_field_write(data->reg_als_intr, state);
- mutex_unlock(&data->lock_als);
return ret;
case IIO_PROXIMITY:
- mutex_lock(&data->lock_ps);
+ guard(mutex)(&data->lock_ps);
ret = regmap_field_write(data->reg_ps_intr, state);
- mutex_unlock(&data->lock_ps);
return ret;
default:
return -EINVAL;
--
2.53.0
next reply other threads:[~2026-04-15 20:33 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-15 20:33 rafasales [this message]
2026-04-17 8:11 ` [PATCH] iio: light: ltr501: Use automatic cleanup of locks 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=20260415203345.195557-1-rafasales@usp.br \
--to=rafasales@usp.br \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=gustavo.arakaki@usp.br \
--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