From: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>, linux-iio@vger.kernel.org
Cc: "Marius Cristea" <marius.cristea@microchip.com>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Joshua Crofts" <crofts@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH v3 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
Date: Wed, 25 Nov 2026 02:10:54 +0530 [thread overview]
Message-ID: <20261124204054.85634-3-vaibhav.chahar26@gmail.com> (raw)
In-Reply-To: <20261124204054.85634-1-vaibhav.chahar26@gmail.com>
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@gmail.com>
---
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
prev parent reply other threads:[~2026-11-24 20:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 20:40 [PATCH v3 0/2] iio: adc: " Vaibhav Chahar
2026-11-24 20:40 ` [PATCH v3 1/2] iio: adc: mcp3564: Use guard() " Vaibhav Chahar
2026-11-24 20:40 ` Vaibhav Chahar [this message]
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=20261124204054.85634-3-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®