Linux IIO development
 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 v3 2/3] iio: light: ltr501: use automatic cleanup of locks
Date: Wed, 22 Apr 2026 20:12:56 -0300	[thread overview]
Message-ID: <20260422231305.677778-3-rafasales@usp.br> (raw)
In-Reply-To: <20260422231305.677778-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>

---
Changes in v3:
- Replaces `scoped_guard()` with {} + `guard()` in some cases
- Simplifies some return statements
- Includes cleanup.h header

Changes in v2:
- Maintains original locking boundaries
- Simplifies some return statements
---
 drivers/iio/light/ltr501.c | 131 +++++++++++++++++--------------------
 1 file changed, 59 insertions(+), 72 deletions(-)

diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 277c497bd6d8..81fb35cbd56a 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -14,6 +14,7 @@
 
 #include <linux/array_size.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/dev_printk.h>
 #include <linux/device.h>
@@ -250,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),
@@ -259,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),
@@ -278,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)
@@ -504,9 +499,10 @@ 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;
 
@@ -524,9 +520,10 @@ 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;
 
@@ -667,23 +664,23 @@ static int ltr501_read_info_raw(struct ltr501_data *data,
 	int ret;
 
 	switch (chan->type) {
-	case IIO_INTENSITY:
-		mutex_lock(&data->lock_als);
+	case IIO_INTENSITY: {
+		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);
+        }
+	case IIO_PROXIMITY: {
+		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;
 		return IIO_VAL_INT;
+	}
 	default:
 		return -EINVAL;
 	}
@@ -704,9 +701,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;
@@ -815,14 +811,13 @@ static int __ltr501_write_raw(struct iio_dev *indio_dev,
 
 	case IIO_CHAN_INFO_INT_TIME:
 		switch (chan->type) {
-		case IIO_INTENSITY:
+		case IIO_INTENSITY: {
 			if (val != 0)
 				return -EINVAL;
 
-			mutex_lock(&data->lock_als);
-			ret = ltr501_set_it_time(data, val2);
-			mutex_unlock(&data->lock_als);
-			return ret;
+			guard(mutex)(&data->lock_als);
+			return ltr501_set_it_time(data, val2);
+		}
 		default:
 			return -EINVAL;
 		}
@@ -959,7 +954,6 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
 			       int val, int val2)
 {
 	struct ltr501_data *data = iio_priv(indio_dev);
-	int ret;
 
 	if (val < 0)
 		return -EINVAL;
@@ -969,20 +963,18 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
 		if (val > LTR501_ALS_THRESH_MASK)
 			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);
-			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);
-			return ret;
+		case IIO_EV_DIR_RISING: {
+			guard(mutex)(&data->lock_als);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_ALS_THRESH_UP,
+						 &val, 2);
+			}
+		case IIO_EV_DIR_FALLING: {
+			guard(mutex)(&data->lock_als);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_ALS_THRESH_LOW,
+						 &val, 2);
+			}
 		default:
 			return -EINVAL;
 		}
@@ -990,20 +982,18 @@ static int ltr501_write_thresh(struct iio_dev *indio_dev,
 		if (val > LTR501_PS_THRESH_MASK)
 			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);
-			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);
-			return ret;
+		case IIO_EV_DIR_RISING: {
+			guard(mutex)(&data->lock_ps);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_PS_THRESH_UP,
+						 &val, 2);
+			}
+		case IIO_EV_DIR_FALLING: {
+			guard(mutex)(&data->lock_ps);
+			return regmap_bulk_write(data->regmap,
+						 LTR501_PS_THRESH_LOW,
+						 &val, 2);
+			}
 		default:
 			return -EINVAL;
 		}
@@ -1095,19 +1085,16 @@ static int ltr501_write_event_config(struct iio_dev *indio_dev,
 				     enum iio_event_direction dir, bool state)
 {
 	struct ltr501_data *data = iio_priv(indio_dev);
-	int ret;
 
 	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);
-		return ret;
-	case IIO_PROXIMITY:
-		mutex_lock(&data->lock_ps);
-		ret = regmap_field_write(data->reg_ps_intr, state);
-		mutex_unlock(&data->lock_ps);
-		return ret;
+	case IIO_INTENSITY: {
+		guard(mutex)(&data->lock_als);
+		return regmap_field_write(data->reg_als_intr, state);
+		}
+	case IIO_PROXIMITY: {
+		guard(mutex)(&data->lock_ps);
+		return regmap_field_write(data->reg_ps_intr, state);
+		}
 	default:
 		return -EINVAL;
 	}
-- 
2.53.0


  parent reply	other threads:[~2026-04-22 23:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-22 23:12 [PATCH v3 0/3] iio: light: ltr501: cleanups and modernizations rafasales
2026-04-22 23:12 ` [PATCH v3 1/3] iio: light: ltr501: update header inclusions rafasales
2026-04-24  9:27   ` Andy Shevchenko
2026-04-22 23:12 ` rafasales [this message]
2026-04-24  9:33   ` [PATCH v3 2/3] iio: light: ltr501: use automatic cleanup of locks Andy Shevchenko
2026-04-24 11:32   ` Jonathan Cameron
2026-04-22 23:12 ` [PATCH v3 3/3] iio: light: ltr501: use `sysfs_emit_at()` for showing scales rafasales
2026-04-24  9:34   ` 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=20260422231305.677778-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