public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
From: rafasales@usp.br
To: andy@kernel.org, dlechner@baylibre.com, jic23@kernel.org,
	nuno.sa@analog.com
Cc: "Rafael B. Sales" <rafasales@usp.br>,
	"Gustavo C. Arakaki" <gustavo.arakaki@usp.br>,
	linux-iio@vger.kernel.org
Subject: [PATCH v2 2/2] iio: light: ltr501: use automatic cleanup of locks
Date: Mon, 20 Apr 2026 23:10:01 -0300	[thread overview]
Message-ID: <20260421021023.563290-3-rafasales@usp.br> (raw)
In-Reply-To: <20260421021023.563290-1-rafasales@usp.br>

From: "Rafael B. Sales" <rafasales@usp.br>

Replace `mutex_lock()` and `mutex_unlock()` calls with guards
to reduce boilerplate and allow for simpler 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 | 94 +++++++++++++++-----------------------
 1 file changed, 38 insertions(+), 56 deletions(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 75a49fd9bce0..d84b8f23c5a2 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -251,7 +251,7 @@ static int ltr501_ps_read_samp_freq(const struct ltr501_data *data,
 static int ltr501_als_write_samp_freq(struct ltr501_data *data,
 				      int val, int val2)
 {
-	int i, ret;
+	int i;
 
 	i = ltr501_match_samp_freq(ltr501_als_samp_table,
 				   ARRAY_SIZE(ltr501_als_samp_table),
@@ -260,17 +260,14 @@ static int ltr501_als_write_samp_freq(struct ltr501_data *data,
 	if (i < 0)
 		return i;
 
-	mutex_lock(&data->lock_als);
-	ret = regmap_field_write(data->reg_als_rate, i);
-	mutex_unlock(&data->lock_als);
-
-	return ret;
+	guard(mutex)(&data->lock_als);
+	return regmap_field_write(data->reg_als_rate, i);
 }
 
 static int ltr501_ps_write_samp_freq(struct ltr501_data *data,
 				     int val, int val2)
 {
-	int i, ret;
+	int i;
 
 	i = ltr501_match_samp_freq(ltr501_ps_samp_table,
 				   ARRAY_SIZE(ltr501_ps_samp_table),
@@ -279,11 +276,8 @@ static int ltr501_ps_write_samp_freq(struct ltr501_data *data,
 	if (i < 0)
 		return i;
 
-	mutex_lock(&data->lock_ps);
-	ret = regmap_field_write(data->reg_ps_rate, i);
-	mutex_unlock(&data->lock_ps);
-
-	return ret;
+	guard(mutex)(&data->lock_ps);
+	return regmap_field_write(data->reg_ps_rate, i);
 }
 
 static int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val)
@@ -505,9 +499,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);
-		ret = regmap_field_write(data->reg_als_prst, new_val);
-		mutex_unlock(&data->lock_als);
+		scoped_guard(mutex, &data->lock_als)
+			ret = regmap_field_write(data->reg_als_prst, new_val);
 		if (ret >= 0)
 			data->als_period = period;
 
@@ -525,9 +518,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);
-		ret = regmap_field_write(data->reg_ps_prst, new_val);
-		mutex_unlock(&data->lock_ps);
+		scoped_guard(mutex, &data->lock_ps)
+			ret = regmap_field_write(data->reg_ps_prst, new_val);
 		if (ret >= 0)
 			data->ps_period = period;
 
@@ -669,18 +661,16 @@ static int ltr501_read_info_raw(struct ltr501_data *data,
 
 	switch (chan->type) {
 	case IIO_INTENSITY:
-		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);
 		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);
-		ret = ltr501_read_ps(data);
-		mutex_unlock(&data->lock_ps);
+		scoped_guard(mutex, &data->lock_ps)
+			ret = ltr501_read_ps(data);
 		if (ret < 0)
 			return ret;
 		*val = ret & LTR501_PS_DATA_MASK;
@@ -705,9 +695,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;
@@ -820,9 +809,8 @@ static int __ltr501_write_raw(struct iio_dev *indio_dev,
 			if (val != 0)
 				return -EINVAL;
 
-			mutex_lock(&data->lock_als);
-			ret = ltr501_set_it_time(data, val2);
-			mutex_unlock(&data->lock_als);
+			scoped_guard(mutex, &data->lock_als)
+				ret = ltr501_set_it_time(data, val2);
 			return ret;
 		default:
 			return -EINVAL;
@@ -971,18 +959,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);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_ALS_THRESH_UP,
-						&val, 2);
-			mutex_unlock(&data->lock_als);
+			scoped_guard(mutex, &data->lock_als)
+				ret = regmap_bulk_write(data->regmap,
+							 LTR501_ALS_THRESH_UP,
+							 &val, 2);
 			return ret;
 		case IIO_EV_DIR_FALLING:
-			mutex_lock(&data->lock_als);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_ALS_THRESH_LOW,
-						&val, 2);
-			mutex_unlock(&data->lock_als);
+			scoped_guard(mutex, &data->lock_als)
+				ret = regmap_bulk_write(data->regmap,
+							 LTR501_ALS_THRESH_LOW,
+							 &val, 2);
 			return ret;
 		default:
 			return -EINVAL;
@@ -992,18 +978,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);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_PS_THRESH_UP,
-						&val, 2);
-			mutex_unlock(&data->lock_ps);
+			scoped_guard(mutex, &data->lock_ps)
+				ret = regmap_bulk_write(data->regmap,
+							 LTR501_PS_THRESH_UP,
+							 &val, 2);
 			return ret;
 		case IIO_EV_DIR_FALLING:
-			mutex_lock(&data->lock_ps);
-			ret = regmap_bulk_write(data->regmap,
-						LTR501_PS_THRESH_LOW,
-						&val, 2);
-			mutex_unlock(&data->lock_ps);
+			scoped_guard(mutex, &data->lock_ps)
+				ret = regmap_bulk_write(data->regmap,
+							 LTR501_PS_THRESH_LOW,
+							 &val, 2);
 			return ret;
 		default:
 			return -EINVAL;
@@ -1100,14 +1084,12 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,
 
 	switch (chan->type) {
 	case IIO_INTENSITY:
-		mutex_lock(&data->lock_als);
-		ret = regmap_field_write(data->reg_als_intr, state);
-		mutex_unlock(&data->lock_als);
+		scoped_guard(mutex, &data->lock_als)
+			ret = regmap_field_write(data->reg_als_intr, state);
 		return ret;
 	case IIO_PROXIMITY:
-		mutex_lock(&data->lock_ps);
-		ret = regmap_field_write(data->reg_ps_intr, state);
-		mutex_unlock(&data->lock_ps);
+		scoped_guard(mutex, &data->lock_ps)
+			ret = regmap_field_write(data->reg_ps_intr, state);
 		return ret;
 	default:
 		return -EINVAL;
-- 
2.53.0


  parent reply	other threads:[~2026-04-21  2:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-21  2:09 [PATCH v2 0/2] iio: light: ltr501: cleanup locking and headers rafasales
2026-04-21  2:10 ` [PATCH v2 1/2] iio: light: ltr501: update header inclusions rafasales
2026-04-21 10:19   ` Jonathan Cameron
2026-04-21 12:49     ` Andy Shevchenko
2026-04-21 12:52       ` Andy Shevchenko
2026-04-21  2:10 ` rafasales [this message]
2026-04-21 10:29   ` [PATCH v2 2/2] iio: light: ltr501: use automatic cleanup of locks 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=20260421021023.563290-3-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