[PATCH v3 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
From: Vaibhav Chahar
Date: Tue Nov 24 2026 - 15:41:51 EST
Use guard(mutex) instead of manual mutex_lock() and mutex_unlock()
calls to modernize the driver and simplify error handling paths.
Made a new helper function palmas_gpadc_read_raw_locked() and called
palmas_gpadc_read_done() after it so it can still run before the mutex
is unlocked, allowing the goto and labels to be removed safely.
Signed-off-by: Vaibhav Chahar <vaibhav.chahar26@xxxxxxxxx>
---
drivers/iio/adc/palmas_gpadc.c | 110 +++++++++++++--------------------
1 file changed, 44 insertions(+), 66 deletions(-)
diff --git a/drivers/iio/adc/palmas_gpadc.c b/drivers/iio/adc/palmas_gpadc.c
index f777986a6..00541e58a 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,6 +539,29 @@ 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)
+ return ret;
+
+ ret = palmas_gpadc_start_conversion(adc, adc_chan);
+ if (ret < 0) {
+ dev_err(adc->dev, "ADC start conversion failed\n");
+ return ret;
+ }
+
+ if (mask == IIO_CHAN_INFO_PROCESSED)
+ ret = palmas_gpadc_get_calibrated_code(adc, adc_chan, ret);
+
+ *val = ret;
+
+ return IIO_VAL_INT;
+}
+
static int palmas_gpadc_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{
@@ -548,39 +572,16 @@ 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;
-
- 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;
+ ret = palmas_gpadc_read_raw_locked(adc, adc_chan, val, mask);
+ 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;
}
@@ -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,
@@ -740,18 +726,16 @@ 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;
- }
+ 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 +743,14 @@ 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;
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;
+ return palmas_gpadc_reconfigure_event_channels(adc);
+ return 0;
}
static const struct iio_info palmas_gpadc_iio_info = {
--
2.56.0