mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
@ 2026-11-23  7:08 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
  0 siblings, 2 replies; 7+ messages in thread
From: Vaibhav Chahar @ 2026-11-23  7:08 UTC (permalink / raw)
  To: marius.cristea, jic23
  Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel, Vaibhav Chahar

Adopting cleanup.h helper: Replaced mutex_lock() and mutex_unlock() calls with guard() and scoped_guard(). Ensures locks are automatically released, simplifying the code.

scoped_guard(){} was handled in mcp3564_read_raw switch statement (case: IIO_CHAN_INFO_SCALE)- giving it proper
scope.

No goto statements were present inside older mutex blocks - so it was a
1 to 1 substitution.

Tested:
- compiled with `make W=1` with zero warnings.
- Verified using Sparse (`make C=1`)
- Verified styling via 'scripts/checkpatch.pl --strict' (0 errors, 0 warnings)

Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
---
Q: If i have similar cleanups for other drivers/files in this directory. 
 Should I combine them into a single patch or submit a patch series?

 drivers/iio/adc/mcp3564.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
index 259ac4ebd..252c3886e 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,7 @@ 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 +508,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 +561,7 @@ 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 +570,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 +597,7 @@ 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 +606,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,10 +826,9 @@ 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;
 		}
 
@@ -840,10 +837,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];
@@ -903,7 +900,7 @@ 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,17 +909,17 @@ 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:
 		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:
 		if (val < mcp3564_calib_scale[0] || val > mcp3564_calib_scale[2])
@@ -931,11 +928,10 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 		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:
 		if (val < 0)
@@ -947,14 +943,13 @@ 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:
 		for (hwgain = 0; hwgain < MCP3564_MAX_PGA; hwgain++)
@@ -968,14 +963,13 @@ 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
  2026-11-23  7:08 [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex 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
  1 sibling, 0 replies; 7+ messages in thread
From: Joshua Crofts @ 2026-11-23 15:22 UTC (permalink / raw)
  To: Vaibhav Chahar, marius.cristea, jic23
  Cc: dlechner, nuno.sa, andy, linux-iio, linux-kernel

On 23-Nov-26 8:08, Vaibhav Chahar wrote:
> Adopting cleanup.h helper: Replaced mutex_lock() and mutex_unlock() calls with guard() and scoped_guard(). Ensures locks are automatically released, simplifying the code.
> 
> scoped_guard(){} was handled in mcp3564_read_raw switch statement (case: IIO_CHAN_INFO_SCALE)- giving it proper
> scope.

I'd reword the commit message; something along the lines of "Use guard(mutex)
instead of manual mutex_lock()/mutex_unlock() calls to modernize driver and improve
mutex handling."

> No goto statements were present inside older mutex blocks - so it was a
> 1 to 1 substitution.
> 
> Tested:
> - compiled with `make W=1` with zero warnings.
> - Verified using Sparse (`make C=1`)
> - Verified styling via 'scripts/checkpatch.pl --strict' (0 errors, 0 warnings)

Move the above paragraphs under the ---, you should mention testing only if
done on the affected hardware (one would expect a patch to be at least compile
tested before being sent to the mailing list).

> Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
> ---
> Q: If i have similar cleanups for other drivers/files in this directory. 
>  Should I combine them into a single patch or submit a patch series?

I'd do a series. One patch can be harder to review.

>  drivers/iio/adc/mcp3564.c | 38 ++++++++++++++++----------------------
>  1 file changed, 16 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
> index 259ac4ebd..252c3886e 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,7 @@ 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);

Blank lines around guard() please.

...

> @@ -840,10 +837,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];
> @@ -903,7 +900,7 @@ 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);

If you're adding guard() in a switch case, make sure to wrap the case
with brackets, i.e.

	case IIO_CHAN_INFO_RAW: {
		foo();
	}
	...

The cleanup.h magic works thanks to local variables, which need some
sort of scope.

-- 
Kind regards,
Joshua Crofts


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 0/2] iio: adc: Use cleanup.h guards for mutex locking
  2026-11-23  7:08 [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
  2026-11-23 15:22 ` Joshua Crofts
@ 2026-11-23 17:28 ` Vaibhav Chahar
  2026-11-23 17:28   ` [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
  2026-11-23 17:28   ` [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
  1 sibling, 2 replies; 7+ messages in thread
From: Vaibhav Chahar @ 2026-11-23 17:28 UTC (permalink / raw)
  To: jic23, marius.cristea
  Cc: crofts, dlechner, nuno.sa, andy, linux-iio, linux-kernel, Vaibhav Chahar

Modernize mutex locking in the mcp3564 and palmas_gpadc ADC drivers by
replacing manual mutex_lock() and mutex_unlock() calls with cleanup.h
guard(mutex) and scoped_guard() helpers.

Changes in v2:
- Patch 1 (mcp3564): Wrap switch cases containing guard() in braces,
  add blank lines around guard() calls, and clean up the commit message.
- Patch 2 (palmas_gpadc): New patch converting palmas_gpadc calls to 
  guard(mutex).

Vaibhav Chahar (2):
  iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
  iio: adc: palmas_gpadc: Use guard(mutex) to manage locks

 drivers/iio/adc/mcp3564.c      | 63 +++++++++++++++++++---------------
 drivers/iio/adc/palmas_gpadc.c | 46 +++++++++----------------
 2 files changed, 53 insertions(+), 56 deletions(-)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
  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
  2026-11-23 19:05     ` Andy Shevchenko
  2026-11-23 17:28   ` [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
  1 sibling, 1 reply; 7+ messages in thread
From: Vaibhav Chahar @ 2026-11-23 17:28 UTC (permalink / raw)
  To: jic23, marius.cristea
  Cc: crofts, dlechner, nuno.sa, andy, linux-iio, linux-kernel, Vaibhav Chahar

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
  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   ` [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
@ 2026-11-23 17:28   ` Vaibhav Chahar
  2026-11-23 19:10     ` Andy Shevchenko
  1 sibling, 1 reply; 7+ messages in thread
From: Vaibhav Chahar @ 2026-11-23 17:28 UTC (permalink / raw)
  To: jic23, marius.cristea
  Cc: crofts, dlechner, nuno.sa, andy, linux-iio, linux-kernel, Vaibhav Chahar

Use guard(mutex) instead of manual mutex_lock() and mutex_unlock()
calls to modernize the driver and simplify error handling paths.

In palmas_gpadc_read_raw(), moved palmas_gpadc_read_done() into the exit
paths inside the switch block so the out: label can be removed while
preserving hardware teardown before unlocking. In
palmas_gpadc_write_event_value(), use direct returns instead of jumping
to out_unlock: label.

Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
---
New patch in v2.

 drivers/iio/adc/palmas_gpadc.c | 46 +++++++++++++---------------------
 1 file changed, 17 insertions(+), 29 deletions(-)

diff --git a/drivers/iio/adc/palmas_gpadc.c b/drivers/iio/adc/palmas_gpadc.c
index f777986a6..fc528fc0b 100644
--- a/drivers/iio/adc/palmas_gpadc.c
+++ b/drivers/iio/adc/palmas_gpadc.c
@@ -17,6 +17,7 @@
 #include <linux/pm.h>
 #include <linux/mfd/palmas.h>
 #include <linux/completion.h>
+#include <linux/cleanup.h>
 #include <linux/of.h>
 #include <linux/iio/events.h>
 #include <linux/iio/iio.h>
@@ -548,20 +549,23 @@ static int palmas_gpadc_read_raw(struct iio_dev *indio_dev,
 	if (adc_chan >= PALMAS_ADC_CH_MAX)
 		return -EINVAL;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 	case IIO_CHAN_INFO_PROCESSED:
 		ret = palmas_gpadc_read_prepare(adc, adc_chan);
-		if (ret < 0)
-			goto out;
+		if (ret < 0) {
+			palmas_gpadc_read_done(adc, adc_chan);
+			return ret;
+		}
 
 		ret = palmas_gpadc_start_conversion(adc, adc_chan);
 		if (ret < 0) {
 			dev_err(adc->dev,
 			"ADC start conversion failed\n");
-			goto out;
+			palmas_gpadc_read_done(adc, adc_chan);
+			return ret;
 		}
 
 		if (mask == IIO_CHAN_INFO_PROCESSED)
@@ -571,19 +575,12 @@ static int palmas_gpadc_read_raw(struct iio_dev *indio_dev,
 		*val = ret;
 
 		ret = IIO_VAL_INT;
-		goto out;
+		palmas_gpadc_read_done(adc, adc_chan);
+		return ret;
 	}
 
-	mutex_unlock(&adc->lock);
-	return ret;
-
-out:
-	palmas_gpadc_read_done(adc, adc_chan);
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
-
 static int palmas_gpadc_read_event_config(struct iio_dev *indio_dev,
 					  const struct iio_chan_spec *chan,
 					  enum iio_event_type type,
@@ -596,13 +593,11 @@ static int palmas_gpadc_read_event_config(struct iio_dev *indio_dev,
 	if (adc_chan >= PALMAS_ADC_CH_MAX || type != IIO_EV_TYPE_THRESH)
 		return -EINVAL;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
 
 	if (palmas_gpadc_get_event(adc, adc_chan, dir))
 		ret = 1;
 
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
@@ -685,15 +680,13 @@ static int palmas_gpadc_write_event_config(struct iio_dev *indio_dev,
 	if (adc_chan >= PALMAS_ADC_CH_MAX || type != IIO_EV_TYPE_THRESH)
 		return -EINVAL;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
 
 	if (state)
 		ret = palmas_gpadc_enable_event_config(adc, chan, dir);
 	else
 		ret = palmas_gpadc_disable_event_config(adc, chan, dir);
 
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
@@ -711,7 +704,7 @@ static int palmas_gpadc_read_event_value(struct iio_dev *indio_dev,
 	if (adc_chan >= PALMAS_ADC_CH_MAX || type != IIO_EV_TYPE_THRESH)
 		return -EINVAL;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
 
 	switch (info) {
 	case IIO_EV_INFO_VALUE:
@@ -725,8 +718,6 @@ static int palmas_gpadc_read_event_value(struct iio_dev *indio_dev,
 		break;
 	}
 
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
@@ -745,12 +736,13 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
 	if (adc_chan >= PALMAS_ADC_CH_MAX || type != IIO_EV_TYPE_THRESH)
 		return -EINVAL;
 
-	mutex_lock(&adc->lock);
+	guard(mutex)(&adc->lock);
+
 	switch (info) {
 	case IIO_EV_INFO_VALUE:
 		if (val < 0 || val > 0xFFF) {
 			ret = -EINVAL;
-			goto out_unlock;
+			return ret;
 		}
 		if (dir == IIO_EV_DIR_RISING) {
 			old = adc->thresholds[adc_chan].high;
@@ -763,15 +755,11 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
 		break;
 	default:
 		ret = -EINVAL;
-		goto out_unlock;
+		return ret;
 	}
 
 	if (val != old && palmas_gpadc_get_event(adc, adc_chan, dir))
 		ret = palmas_gpadc_reconfigure_event_channels(adc);
-
-out_unlock:
-	mutex_unlock(&adc->lock);
-
 	return ret;
 }
 
-- 
2.56.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex
  2026-11-23 17:28   ` [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
@ 2026-11-23 19:05     ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-23 19:05 UTC (permalink / raw)
  To: Vaibhav Chahar
  Cc: jic23, marius.cristea, crofts, dlechner, nuno.sa, andy,
	linux-iio, linux-kernel

On Mon, Nov 23, 2026 at 10:58:35PM +0530, Vaibhav Chahar wrote:
> Use guard(mutex) and scoped_guard() instead of manual mutex_lock() and
> mutex_unlock() calls to modernize the driver and improve mutex handling.

...

>  	dev_dbg(&indio_dev->dev, "%s: %d\n", __func__, mode);

Consider the patch to drop __func__ in dev_dbg() calls. If required with
Dynamic Debug it may be switched at run-rime.

...

> static int mcp3564_set_current_boost_mode(struct iio_dev *indio_dev,

>  	else
>  		adc->current_boost_mode = mode;
>  
> -	mutex_unlock(&adc->lock);
>  
>  	return ret;

Now you will have double blank line(s) here and elsewhere.

...

> 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;

Consider going further and unwind this to return as soon as possible.
As you understand the whole patch has to be revisited for all of the comments.

...

>  		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;
> +

Stray change.

>  		}

...

>  	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];
> +			}

Indentation issues.

>  		return IIO_VAL_INT_PLUS_NANO;

...

I stopped here. You must read what you send. Do not blindly copy'n'paste
AI slops!

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
  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
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-11-23 19:10 UTC (permalink / raw)
  To: Vaibhav Chahar
  Cc: jic23, marius.cristea, crofts, dlechner, nuno.sa, andy,
	linux-iio, linux-kernel

On Mon, Nov 23, 2026 at 10:58:36PM +0530, Vaibhav Chahar wrote:
> Use guard(mutex) instead of manual mutex_lock() and mutex_unlock()
> calls to modernize the driver and simplify error handling paths.
> 
> In palmas_gpadc_read_raw(), moved palmas_gpadc_read_done() into the exit
> paths inside the switch block so the out: label can be removed while
> preserving hardware teardown before unlocking. In
> palmas_gpadc_write_event_value(), use direct returns instead of jumping
> to out_unlock: label.

A few similar issues as in previous patch, AR: study my review and act
accordingly.

...

>  #include <linux/completion.h>
> +#include <linux/cleanup.h>

l is before o.

*yes, I see that the order is a mess.

>  #include <linux/of.h>
>  #include <linux/iio/events.h>
>  #include <linux/iio/iio.h>

...

>  		ret = palmas_gpadc_read_prepare(adc, adc_chan);
> -		if (ret < 0)
> -			goto out;
> +		if (ret < 0) {
> +			palmas_gpadc_read_done(adc, adc_chan);
> +			return ret;
> +		}
>  
>  		ret = palmas_gpadc_start_conversion(adc, adc_chan);
>  		if (ret < 0) {
>  			dev_err(adc->dev,
>  			"ADC start conversion failed\n");
> -			goto out;
> +			palmas_gpadc_read_done(adc, adc_chan);
> +			return ret;
>  		}

Nope. In this case split out an inner part into a low-level helper
function and call it _locked() or _unlocked() (I don't remember by
heart what the correct one is in IIO for this case).

...

>  	default:
>  		ret = -EINVAL;
> -		goto out_unlock;
> +		return ret;

So, what's next obvious step?

>  	}

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-23 19:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  7:08 [PATCH] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex 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   ` [PATCH v2 1/2] iio: adc: mcp3564: Use guard() and scoped_guard() to manage mutex Vaibhav Chahar
2026-11-23 19:05     ` 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

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®