[PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks

From: Vaibhav Chahar

Date: Mon Nov 23 2026 - 12:29:23 EST


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@xxxxxxxxx>
---
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