mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/2] iio: adc: Use guard(mutex) to manage locks
@ 2026-11-26 11:44 Vaibhav Chahar
  2026-11-26 11:44 ` [PATCH v4 1/2] iio: adc: mcp3564: Use guard() to manage mutex Vaibhav Chahar
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Vaibhav Chahar @ 2026-11-26 11:44 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Marius Cristea, David Lechner, Nuno Sá,
	Andy Shevchenko, Joshua Crofts, linux-iio, linux-kernel,
	Vaibhav Chahar

Convert mcp3564 and palmas_gpadc to use guard(mutex) instead of manual
mutex_lock() and mutex_unlock() calls to simplify error handling paths.

Changes in v4:
- mcp3564: Drop blank lines before `if (ret)` checks and add blank lines
  after `guard(mutex)` in `mcp3564_read_raw()` to improve readibility.
- palmas_gpadc: Move `palmas_gpadc_read_done()` inside
  `palmas_gpadc_read_raw_locked()` and drop unused `ret` from
  `palmas_gpadc_read_raw()`, moved conditional check inside
  `case IIO_EV_INFO_VALUE:`.

Changes in v3:
- mcp3564: Use of guard(mutex) across all paths. (Dropping __func__
  from dev_dbg() calls will follow in a separate patch.)
- palmas_gpadc: Make helper function palmas_gpadc_read_raw_locked() so
  palmas_gpadc_read_done() always runs before unlocking safely.

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() to manage mutex
  iio: adc: palmas_gpadc: Use guard(mutex) to manage locks

 drivers/iio/adc/mcp3564.c      | 103 ++++++++++++++-------------
 drivers/iio/adc/palmas_gpadc.c | 126 ++++++++++++++-------------------
 2 files changed, 107 insertions(+), 122 deletions(-)

--
2.56.0


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

* [PATCH v4 1/2] iio: adc: mcp3564: Use guard() to manage mutex
  2026-11-26 11:44 [PATCH v4 0/2] iio: adc: Use guard(mutex) to manage locks Vaibhav Chahar
@ 2026-11-26 11:44 ` Vaibhav Chahar
  2026-11-26 11:44 ` [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
  2026-11-26 13:26 ` [PATCH v4 0/2] iio: adc: " Joshua Crofts
  2 siblings, 0 replies; 6+ messages in thread
From: Vaibhav Chahar @ 2026-11-26 11:44 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Marius Cristea, David Lechner, Nuno Sá,
	Andy Shevchenko, Joshua Crofts, linux-iio, linux-kernel,
	Vaibhav Chahar

Use guard(mutex) 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>
---
 drivers/iio/adc/mcp3564.c | 103 ++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 49 deletions(-)

diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
index 259ac4ebd..3b363eb82 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,18 +499,17 @@ 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));
-
-	if (ret)
+	if (ret) {
 		dev_err(&indio_dev->dev, "Failed to configure CONFIG2 register\n");
-	else
-		adc->current_boost_mode = mode;
+		return ret;
+	}
+	adc->current_boost_mode = mode;
 
-	mutex_unlock(&adc->lock);
-
-	return ret;
+	return 0;
 }
 
 static int mcp3564_get_current_boost_mode(struct iio_dev *indio_dev,
@@ -561,18 +561,17 @@ 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));
-
-	if (ret)
+	if (ret) {
 		dev_err(&indio_dev->dev, "Failed to update CONFIG2 register\n");
-	else
-		adc->auto_zeroing_mux = auto_zero;
+		return ret;
+	}
+	adc->auto_zeroing_mux = auto_zero;
 
-	mutex_unlock(&adc->lock);
-
-	return ret ? ret : len;
+	return len;
 }
 
 static ssize_t mcp3564_auto_zeroing_ref_show(struct device *dev,
@@ -598,18 +597,17 @@ 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));
-
-	if (ret)
+	if (ret) {
 		dev_err(&indio_dev->dev, "Failed to update CONFIG2 register\n");
-	else
-		adc->auto_zeroing_ref = auto_zero;
+		return ret;
+	}
+	adc->auto_zeroing_ref = auto_zero;
 
-	mutex_unlock(&adc->lock);
-
-	return ret ? ret : len;
+	return len;
 }
 
 static const struct iio_chan_spec mcp3564_channel_template = {
@@ -828,10 +826,10 @@ 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;
 		}
 
@@ -839,12 +837,13 @@ static int mcp3564_read_raw(struct iio_dev *indio_dev,
 		if (ret)
 			return -EINVAL;
 		return IIO_VAL_INT;
-	case IIO_CHAN_INFO_SCALE:
-		mutex_lock(&adc->lock);
+	case IIO_CHAN_INFO_SCALE: {
+		guard(mutex)(&adc->lock);
+
 		*val = adc->scale_tbls[adc->hwgain][0];
 		*val2 = adc->scale_tbls[adc->hwgain][1];
-		mutex_unlock(&adc->lock);
 		return IIO_VAL_INT_PLUS_NANO;
+	}
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
 		*val = mcp3564_oversampling_avail[adc->oversampling];
 		return IIO_VAL_INT;
@@ -888,7 +887,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,41 +902,45 @@ 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));
-
-		if (ret)
+		if (ret) {
 			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:
+			return ret;
+		}
+		adc->burnout_mode = burnout;
+
+		return 0;
+	}
+	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 +950,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 +972,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] 6+ messages in thread

* [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
  2026-11-26 11:44 [PATCH v4 0/2] iio: adc: Use guard(mutex) to manage locks Vaibhav Chahar
  2026-11-26 11:44 ` [PATCH v4 1/2] iio: adc: mcp3564: Use guard() to manage mutex Vaibhav Chahar
@ 2026-11-26 11:44 ` Vaibhav Chahar
  2026-11-26 13:26   ` Joshua Crofts
  2026-11-26 13:26 ` [PATCH v4 0/2] iio: adc: " Joshua Crofts
  2 siblings, 1 reply; 6+ messages in thread
From: Vaibhav Chahar @ 2026-11-26 11:44 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: Marius Cristea, David Lechner, Nuno Sá,
	Andy Shevchenko, Joshua Crofts, 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.

Made a new palmas_gpadc_read_raw_locked() helper function which
calls palmas_gpadc_read_done() on all exit paths before
returning to palmas_gpadc_read_raw() where the mutex is unlocked,
allowing the removal of unlock goto labels safely.

Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
---
 drivers/iio/adc/palmas_gpadc.c | 126 ++++++++++++++-------------------
 1 file changed, 53 insertions(+), 73 deletions(-)

diff --git a/drivers/iio/adc/palmas_gpadc.c b/drivers/iio/adc/palmas_gpadc.c
index f777986a6..fa1526e31 100644
--- a/drivers/iio/adc/palmas_gpadc.c
+++ b/drivers/iio/adc/palmas_gpadc.c
@@ -16,6 +16,7 @@
 #include <linux/delay.h>
 #include <linux/pm.h>
 #include <linux/mfd/palmas.h>
+#include <linux/cleanup.h>
 #include <linux/completion.h>
 #include <linux/of.h>
 #include <linux/iio/events.h>
@@ -538,50 +539,50 @@ static int palmas_gpadc_get_low_threshold_raw(struct palmas_gpadc *adc,
 						     min_offset_error);
 }
 
+static int palmas_gpadc_read_raw_locked(struct palmas_gpadc *adc, int adc_chan,
+					int *val, long mask)
+{
+	int ret;
+
+	ret = palmas_gpadc_read_prepare(adc, adc_chan);
+	if (ret < 0)
+		goto done;
+
+	ret = palmas_gpadc_start_conversion(adc, adc_chan);
+	if (ret < 0) {
+		dev_err(adc->dev, "ADC start conversion failed\n");
+		goto done;
+	}
+
+	if (mask == IIO_CHAN_INFO_PROCESSED)
+		ret = palmas_gpadc_get_calibrated_code(adc, adc_chan, ret);
+
+	*val = ret;
+	ret = IIO_VAL_INT;
+
+done:
+	palmas_gpadc_read_done(adc, adc_chan);
+	return ret;
+}
+
 static int palmas_gpadc_read_raw(struct iio_dev *indio_dev,
 	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
 {
 	struct  palmas_gpadc *adc = iio_priv(indio_dev);
 	int adc_chan = chan->channel;
-	int ret = 0;
 
 	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;
-
-		ret = palmas_gpadc_start_conversion(adc, adc_chan);
-		if (ret < 0) {
-			dev_err(adc->dev,
-			"ADC start conversion failed\n");
-			goto out;
-		}
-
-		if (mask == IIO_CHAN_INFO_PROCESSED)
-			ret = palmas_gpadc_get_calibrated_code(
-							adc, adc_chan, ret);
-
-		*val = ret;
-
-		ret = IIO_VAL_INT;
-		goto out;
+		return palmas_gpadc_read_raw_locked(adc, adc_chan, val, mask);
 	}
 
-	mutex_unlock(&adc->lock);
-	return ret;
-
-out:
-	palmas_gpadc_read_done(adc, adc_chan);
-	mutex_unlock(&adc->lock);
-
-	return ret;
+	return 0;
 }
 
 static int palmas_gpadc_read_event_config(struct iio_dev *indio_dev,
@@ -591,19 +592,16 @@ static int palmas_gpadc_read_event_config(struct iio_dev *indio_dev,
 {
 	struct palmas_gpadc *adc = iio_priv(indio_dev);
 	int adc_chan = chan->channel;
-	int ret = 0;
 
 	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;
+		return 1;
 
-	mutex_unlock(&adc->lock);
-
-	return ret;
+	return 0;
 }
 
 static int palmas_adc_configure_events(struct palmas_gpadc *adc);
@@ -680,21 +678,16 @@ static int palmas_gpadc_write_event_config(struct iio_dev *indio_dev,
 {
 	struct palmas_gpadc *adc = iio_priv(indio_dev);
 	int adc_chan = chan->channel;
-	int ret;
 
 	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);
+		return palmas_gpadc_enable_event_config(adc, chan, dir);
 
-	mutex_unlock(&adc->lock);
-
-	return ret;
+	return palmas_gpadc_disable_event_config(adc, chan, dir);
 }
 
 static int palmas_gpadc_read_event_value(struct iio_dev *indio_dev,
@@ -706,28 +699,21 @@ static int palmas_gpadc_read_event_value(struct iio_dev *indio_dev,
 {
 	struct palmas_gpadc *adc = iio_priv(indio_dev);
 	int adc_chan = chan->channel;
-	int ret;
 
 	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:
 		*val = (dir == IIO_EV_DIR_RISING) ?
 			adc->thresholds[adc_chan].high :
 			adc->thresholds[adc_chan].low;
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-	mutex_unlock(&adc->lock);
-
-	return ret;
 }
 
 static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
@@ -739,19 +725,18 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
 {
 	struct palmas_gpadc *adc = iio_priv(indio_dev);
 	int adc_chan = chan->channel;
-	int old;
-	int ret;
 
 	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;
-		}
+	case IIO_EV_INFO_VALUE: {
+		int old;
+
+		if (val < 0 || val > 0xFFF)
+			return -EINVAL;
 		if (dir == IIO_EV_DIR_RISING) {
 			old = adc->thresholds[adc_chan].high;
 			adc->thresholds[adc_chan].high = val;
@@ -759,20 +744,15 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
 			old = adc->thresholds[adc_chan].low;
 			adc->thresholds[adc_chan].low = val;
 		}
-		ret = 0;
-		break;
+
+		if (val != old && palmas_gpadc_get_event(adc, adc_chan, dir))
+			return palmas_gpadc_reconfigure_event_channels(adc);
+
+		return 0;
+	}
 	default:
-		ret = -EINVAL;
-		goto out_unlock;
+		return -EINVAL;
 	}
-
-	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;
 }
 
 static const struct iio_info palmas_gpadc_iio_info = {
-- 
2.56.0


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

* Re: [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
  2026-11-26 11:44 ` [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
@ 2026-11-26 13:26   ` Joshua Crofts
  2026-11-28  4:02     ` Jonathan Cameron
  0 siblings, 1 reply; 6+ messages in thread
From: Joshua Crofts @ 2026-11-26 13:26 UTC (permalink / raw)
  To: Vaibhav Chahar, Jonathan Cameron
  Cc: Marius Cristea, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel

On 26-Nov-26 12:44, Vaibhav Chahar wrote:
> Use guard(mutex) instead of manual mutex_lock() and mutex_unlock()
> calls to modernize the driver and simplify error handling paths.
> 
> Made a new palmas_gpadc_read_raw_locked() helper function which
> calls palmas_gpadc_read_done() on all exit paths before
> returning to palmas_gpadc_read_raw() where the mutex is unlocked,
> allowing the removal of unlock goto labels safely.
> 
> Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
> ---
>  drivers/iio/adc/palmas_gpadc.c | 126 ++++++++++++++-------------------
>  1 file changed, 53 insertions(+), 73 deletions(-)
> 
> diff --git a/drivers/iio/adc/palmas_gpadc.c b/drivers/iio/adc/palmas_gpadc.c
> index f777986a6..fa1526e31 100644
> --- a/drivers/iio/adc/palmas_gpadc.c
> +++ b/drivers/iio/adc/palmas_gpadc.c
> @@ -16,6 +16,7 @@
>  #include <linux/delay.h>
>  #include <linux/pm.h>
>  #include <linux/mfd/palmas.h>
> +#include <linux/cleanup.h>
>  #include <linux/completion.h>
>  #include <linux/of.h>
>  #include <linux/iio/events.h>
> @@ -538,50 +539,50 @@ static int palmas_gpadc_get_low_threshold_raw(struct palmas_gpadc *adc,
>  						     min_offset_error);
>  }
>  
> +static int palmas_gpadc_read_raw_locked(struct palmas_gpadc *adc, int adc_chan,
> +					int *val, long mask)
> +{
> +	int ret;
> +
> +	ret = palmas_gpadc_read_prepare(adc, adc_chan);
> +	if (ret < 0)
> +		goto done;
> +
> +	ret = palmas_gpadc_start_conversion(adc, adc_chan);
> +	if (ret < 0) {
> +		dev_err(adc->dev, "ADC start conversion failed\n");
> +		goto done;
> +	}
> +
> +	if (mask == IIO_CHAN_INFO_PROCESSED)
> +		ret = palmas_gpadc_get_calibrated_code(adc, adc_chan, ret);
> +
> +	*val = ret;
> +	ret = IIO_VAL_INT;
> +
> +done:
> +	palmas_gpadc_read_done(adc, adc_chan);
> +	return ret;
> +}
> +
>  static int palmas_gpadc_read_raw(struct iio_dev *indio_dev,
>  	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
>  {
>  	struct  palmas_gpadc *adc = iio_priv(indio_dev);
>  	int adc_chan = chan->channel;
> -	int ret = 0;
>  
>  	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;
> -
> -		ret = palmas_gpadc_start_conversion(adc, adc_chan);
> -		if (ret < 0) {
> -			dev_err(adc->dev,
> -			"ADC start conversion failed\n");
> -			goto out;
> -		}
> -
> -		if (mask == IIO_CHAN_INFO_PROCESSED)
> -			ret = palmas_gpadc_get_calibrated_code(
> -							adc, adc_chan, ret);
> -
> -		*val = ret;
> -
> -		ret = IIO_VAL_INT;
> -		goto out;
> +		return palmas_gpadc_read_raw_locked(adc, adc_chan, val, mask);
>  	}
>  
> -	mutex_unlock(&adc->lock);
> -	return ret;
> -
> -out:
> -	palmas_gpadc_read_done(adc, adc_chan);
> -	mutex_unlock(&adc->lock);
> -
> -	return ret;
> +	return 0;
>  }
>  
>  static int palmas_gpadc_read_event_config(struct iio_dev *indio_dev,
> @@ -591,19 +592,16 @@ static int palmas_gpadc_read_event_config(struct iio_dev *indio_dev,
>  {
>  	struct palmas_gpadc *adc = iio_priv(indio_dev);
>  	int adc_chan = chan->channel;
> -	int ret = 0;
>  
>  	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;
> +		return 1;
>  
> -	mutex_unlock(&adc->lock);
> -
> -	return ret;
> +	return 0;
>  }
>  
>  static int palmas_adc_configure_events(struct palmas_gpadc *adc);
> @@ -680,21 +678,16 @@ static int palmas_gpadc_write_event_config(struct iio_dev *indio_dev,
>  {
>  	struct palmas_gpadc *adc = iio_priv(indio_dev);
>  	int adc_chan = chan->channel;
> -	int ret;
>  
>  	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);
> +		return palmas_gpadc_enable_event_config(adc, chan, dir);
>  
> -	mutex_unlock(&adc->lock);
> -
> -	return ret;
> +	return palmas_gpadc_disable_event_config(adc, chan, dir);
>  }
>  
>  static int palmas_gpadc_read_event_value(struct iio_dev *indio_dev,
> @@ -706,28 +699,21 @@ static int palmas_gpadc_read_event_value(struct iio_dev *indio_dev,
>  {
>  	struct palmas_gpadc *adc = iio_priv(indio_dev);
>  	int adc_chan = chan->channel;
> -	int ret;
>  
>  	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:
>  		*val = (dir == IIO_EV_DIR_RISING) ?
>  			adc->thresholds[adc_chan].high :
>  			adc->thresholds[adc_chan].low;
> -		ret = IIO_VAL_INT;
> -		break;
> +		return IIO_VAL_INT;
>  	default:
> -		ret = -EINVAL;
> -		break;
> +		return -EINVAL;
>  	}
> -
> -	mutex_unlock(&adc->lock);
> -
> -	return ret;
>  }
>  
>  static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
> @@ -739,19 +725,18 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
>  {
>  	struct palmas_gpadc *adc = iio_priv(indio_dev);
>  	int adc_chan = chan->channel;
> -	int old;
> -	int ret;
>  
>  	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;
> -		}
> +	case IIO_EV_INFO_VALUE: {
> +		int old;
> +
> +		if (val < 0 || val > 0xFFF)
> +			return -EINVAL;

Not introduced by this patch (I sound like Sashiko :) ), but -ERANGE is preferred.

This could be tweaked whilst applying though.

-- 
Kind regards,
Joshua Crofts

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

* Re: [PATCH v4 0/2] iio: adc: Use guard(mutex) to manage locks
  2026-11-26 11:44 [PATCH v4 0/2] iio: adc: Use guard(mutex) to manage locks Vaibhav Chahar
  2026-11-26 11:44 ` [PATCH v4 1/2] iio: adc: mcp3564: Use guard() to manage mutex Vaibhav Chahar
  2026-11-26 11:44 ` [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
@ 2026-11-26 13:26 ` Joshua Crofts
  2 siblings, 0 replies; 6+ messages in thread
From: Joshua Crofts @ 2026-11-26 13:26 UTC (permalink / raw)
  To: Vaibhav Chahar, Jonathan Cameron
  Cc: Marius Cristea, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel

On 26-Nov-26 12:44, Vaibhav Chahar wrote:
> Convert mcp3564 and palmas_gpadc to use guard(mutex) instead of manual
> mutex_lock() and mutex_unlock() calls to simplify error handling paths.
> 
> Changes in v4:
> - mcp3564: Drop blank lines before `if (ret)` checks and add blank lines
>   after `guard(mutex)` in `mcp3564_read_raw()` to improve readibility.
> - palmas_gpadc: Move `palmas_gpadc_read_done()` inside
>   `palmas_gpadc_read_raw_locked()` and drop unused `ret` from
>   `palmas_gpadc_read_raw()`, moved conditional check inside
>   `case IIO_EV_INFO_VALUE:`.
> 
> Changes in v3:
> - mcp3564: Use of guard(mutex) across all paths. (Dropping __func__
>   from dev_dbg() calls will follow in a separate patch.)
> - palmas_gpadc: Make helper function palmas_gpadc_read_raw_locked() so
>   palmas_gpadc_read_done() always runs before unlocking safely.
> 
> 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() to manage mutex
>   iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
> 

For the series,

Reviewed-by: Joshua Crofts <crofts@kernel.org>

-- 
Kind regards,
Joshua Crofts

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

* Re: [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
  2026-11-26 13:26   ` Joshua Crofts
@ 2026-11-28  4:02     ` Jonathan Cameron
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Cameron @ 2026-11-28  4:02 UTC (permalink / raw)
  To: Joshua Crofts
  Cc: Vaibhav Chahar, Marius Cristea, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel

On Thu, 26 Nov 2026 14:26:21 +0100

> >  static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
> > @@ -739,19 +725,18 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
> >  {
> >  	struct palmas_gpadc *adc = iio_priv(indio_dev);
> >  	int adc_chan = chan->channel;
> > -	int old;
> > -	int ret;
> >  
> >  	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;
> > -		}
> > +	case IIO_EV_INFO_VALUE: {
> > +		int old;
> > +
> > +		if (val < 0 || val > 0xFFF)
> > +			return -EINVAL;  
> 
> Not introduced by this patch (I sound like Sashiko :) ), but -ERANGE is preferred.
> 
> This could be tweaked whilst applying though.
Backlog too large for this sort of thing today.

So will leave it for now. 

Series applied to the testing branch of iio.git.

Thanks,

Jonathan

> 


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

end of thread, other threads:[~2026-11-28  4:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 11:44 [PATCH v4 0/2] iio: adc: Use guard(mutex) to manage locks Vaibhav Chahar
2026-11-26 11:44 ` [PATCH v4 1/2] iio: adc: mcp3564: Use guard() to manage mutex Vaibhav Chahar
2026-11-26 11:44 ` [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Vaibhav Chahar
2026-11-26 13:26   ` Joshua Crofts
2026-11-28  4:02     ` Jonathan Cameron
2026-11-26 13:26 ` [PATCH v4 0/2] iio: adc: " Joshua Crofts

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®