mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
To: jic23@kernel.org, marius.cristea@microchip.com
Cc: crofts@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
	andy@kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Vaibhav Chahar <vaibhav.chahar26@gmail.com>
Subject: [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
Date: Mon, 23 Nov 2026 22:58:35 +0530	[thread overview]
Message-ID: <20261123172836.130440-2-vaibhav.chahar26@gmail.com> (raw)
In-Reply-To: <20261123172836.130440-1-vaibhav.chahar26@gmail.com>

Use guard(mutex) and scoped_guard() instead of manual mutex_lock() and
mutex_unlock() calls to modernize the driver and improve mutex handling.

Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
---
Changes in v2:
- Wrap switch cases containing guard() in braces in mcp3564_write_raw().
- Add blank lines around guard() calls.
- Change commit message and drop compile-test notes from commit log.

 drivers/iio/adc/mcp3564.c | 63 ++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 27 deletions(-)

diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
index 259ac4ebd..04bb4da4b 100644
--- a/drivers/iio/adc/mcp3564.c
+++ b/drivers/iio/adc/mcp3564.c
@@ -17,6 +17,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/iopoll.h>
 #include <linux/regulator/consumer.h>
 #include <linux/spi/spi.h>
@@ -498,7 +499,8 @@ static int mcp3564_set_current_boost_mode(struct iio_dev *indio_dev,
 
 	dev_dbg(&indio_dev->dev, "%s: %d\n", __func__, mode);
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
+
 	ret = mcp3564_update_8bits(adc, MCP3564_CONFIG2_REG, MCP3564_CONFIG2_BOOST_CURRENT_MASK,
 				   FIELD_PREP(MCP3564_CONFIG2_BOOST_CURRENT_MASK, mode));
 
@@ -507,7 +509,6 @@ static int mcp3564_set_current_boost_mode(struct iio_dev *indio_dev,
 	else
 		adc->current_boost_mode = mode;
 
-	mutex_unlock(&adc->lock);
 
 	return ret;
 }
@@ -561,7 +562,8 @@ static ssize_t mcp3564_auto_zeroing_mux_store(struct device *dev,
 	if (ret)
 		return ret;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
+
 	ret = mcp3564_update_8bits(adc, MCP3564_CONFIG2_REG, MCP3564_CONFIG2_AZ_MUX_MASK,
 				   FIELD_PREP(MCP3564_CONFIG2_AZ_MUX_MASK, auto_zero));
 
@@ -570,7 +572,6 @@ static ssize_t mcp3564_auto_zeroing_mux_store(struct device *dev,
 	else
 		adc->auto_zeroing_mux = auto_zero;
 
-	mutex_unlock(&adc->lock);
 
 	return ret ? ret : len;
 }
@@ -598,7 +599,8 @@ static ssize_t mcp3564_auto_zeroing_ref_store(struct device *dev,
 	if (ret)
 		return ret;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
+
 	ret = mcp3564_update_8bits(adc, MCP3564_CONFIG2_REG, MCP3564_CONFIG2_AZ_REF_MASK,
 				   FIELD_PREP(MCP3564_CONFIG2_AZ_REF_MASK, auto_zero));
 
@@ -607,7 +609,6 @@ static ssize_t mcp3564_auto_zeroing_ref_store(struct device *dev,
 	else
 		adc->auto_zeroing_ref = auto_zero;
 
-	mutex_unlock(&adc->lock);
 
 	return ret ? ret : len;
 }
@@ -828,11 +829,12 @@ static int mcp3564_read_raw(struct iio_dev *indio_dev,
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (channel->output) {
-			mutex_lock(&adc->lock);
+			guard(mutex)(&adc->lock);
+
 			*val = mcp3564_burnout_avail[adc->burnout_mode][0];
 			*val2 = mcp3564_burnout_avail[adc->burnout_mode][1];
-			mutex_unlock(&adc->lock);
 			return IIO_VAL_INT_PLUS_MICRO;
+
 		}
 
 		ret = mcp3564_read_single_value(indio_dev, channel, val);
@@ -840,10 +842,10 @@ static int mcp3564_read_raw(struct iio_dev *indio_dev,
 			return -EINVAL;
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		mutex_lock(&adc->lock);
-		*val = adc->scale_tbls[adc->hwgain][0];
-		*val2 = adc->scale_tbls[adc->hwgain][1];
-		mutex_unlock(&adc->lock);
+			scoped_guard(mutex, &adc->lock) {
+				*val = adc->scale_tbls[adc->hwgain][0];
+				*val2 = adc->scale_tbls[adc->hwgain][1];
+			}
 		return IIO_VAL_INT_PLUS_NANO;
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
 		*val = mcp3564_oversampling_avail[adc->oversampling];
@@ -888,7 +890,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 	int ret = 0;
 
 	switch (mask) {
-	case IIO_CHAN_INFO_RAW:
+	case IIO_CHAN_INFO_RAW: {
 		if (!channel->output)
 			return -EINVAL;
 
@@ -903,7 +905,8 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 		if (burnout == adc->burnout_mode)
 			return ret;
 
-		mutex_lock(&adc->lock);
+		guard(mutex)(&adc->lock);
+
 		ret = mcp3564_update_8bits(adc, MCP3564_CONFIG0_REG,
 					   MCP3564_CONFIG0_CS_SEL_MASK,
 					   FIELD_PREP(MCP3564_CONFIG0_CS_SEL_MASK, burnout));
@@ -912,32 +915,36 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 			dev_err(&indio_dev->dev, "Failed to configure burnout current\n");
 		else
 			adc->burnout_mode = burnout;
-		mutex_unlock(&adc->lock);
+
 		return ret;
-	case IIO_CHAN_INFO_CALIBBIAS:
+		}
+	case IIO_CHAN_INFO_CALIBBIAS: {
 		if (val < mcp3564_calib_bias[0] || val > mcp3564_calib_bias[2])
 			return -EINVAL;
 
-		mutex_lock(&adc->lock);
+		guard(mutex)(&adc->lock);
+
 		ret = mcp3564_write_24bits(adc, MCP3564_OFFSETCAL_REG, val);
 		if (!ret)
 			adc->calib_bias = val;
-		mutex_unlock(&adc->lock);
+
 		return ret;
-	case IIO_CHAN_INFO_CALIBSCALE:
+		}
+	case IIO_CHAN_INFO_CALIBSCALE: {
 		if (val < mcp3564_calib_scale[0] || val > mcp3564_calib_scale[2])
 			return -EINVAL;
 
 		if (adc->calib_scale == val)
 			return ret;
 
-		mutex_lock(&adc->lock);
+		guard(mutex)(&adc->lock);
+
 		ret = mcp3564_write_24bits(adc, MCP3564_GAINCAL_REG, val);
 		if (!ret)
 			adc->calib_scale = val;
-		mutex_unlock(&adc->lock);
 		return ret;
-	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+		}
+	case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
 		if (val < 0)
 			return -EINVAL;
 
@@ -947,16 +954,17 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 		if (adc->oversampling == tmp)
 			return ret;
 
-		mutex_lock(&adc->lock);
+		guard(mutex)(&adc->lock);
+
 		ret = mcp3564_update_8bits(adc, MCP3564_CONFIG1_REG,
 					   MCP3564_CONFIG1_OVERSPL_RATIO_MASK,
 					   FIELD_PREP(MCP3564_CONFIG1_OVERSPL_RATIO_MASK,
 						      adc->oversampling));
 		if (!ret)
 			adc->oversampling = tmp;
-		mutex_unlock(&adc->lock);
 		return ret;
-	case IIO_CHAN_INFO_SCALE:
+		}
+	case IIO_CHAN_INFO_SCALE: {
 		for (hwgain = 0; hwgain < MCP3564_MAX_PGA; hwgain++)
 			if (val == adc->scale_tbls[hwgain][0] &&
 			    val2 == adc->scale_tbls[hwgain][1])
@@ -968,15 +976,16 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 		if (hwgain == adc->hwgain)
 			return ret;
 
-		mutex_lock(&adc->lock);
+		guard(mutex)(&adc->lock);
+
 		ret = mcp3564_update_8bits(adc, MCP3564_CONFIG2_REG,
 					   MCP3564_CONFIG2_HARDWARE_GAIN_MASK,
 					   FIELD_PREP(MCP3564_CONFIG2_HARDWARE_GAIN_MASK, hwgain));
 		if (!ret)
 			adc->hwgain = hwgain;
 
-		mutex_unlock(&adc->lock);
 		return ret;
+		}
 	default:
 		return -EINVAL;
 	}
-- 
2.56.0


  reply	other threads:[~2026-11-23 17:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  7:08 [PATCH] " Vaibhav Chahar
2026-11-23 15:22 ` Joshua Crofts
2026-11-23 17:28 ` [PATCH v2 0/2] iio: adc: Use cleanup.h guards for mutex locking Vaibhav Chahar
2026-11-23 17:28   ` Vaibhav Chahar [this message]
2026-11-23 19:05     ` [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Andy Shevchenko
2026-11-23 17:28   ` [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
2026-11-23 19:10     ` 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=20261123172836.130440-2-vaibhav.chahar26@gmail.com \
    --to=vaibhav.chahar26@gmail.com \
    --cc=andy@kernel.org \
    --cc=crofts@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marius.cristea@microchip.com \
    --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

all inboxes | Powered by JetHome®