From: Vaibhav Chahar <vaibhav.chahar26@gmail.com>
To: jic23@kernel.org, marius.cristea@microchip.com
Cc: crofts@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
andy@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org,
Vaibhav Chahar <vaibhav.chahar26@gmail.com>
Subject: [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
Date: Mon, 23 Nov 2026 22:58:36 +0530 [thread overview]
Message-ID: <20261123172836.130440-3-vaibhav.chahar26@gmail.com> (raw)
In-Reply-To: <20261123172836.130440-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.
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
next prev parent reply other threads:[~2026-11-23 17:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Vaibhav Chahar [this message]
2026-11-23 19:10 ` [PATCH v2 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks Andy Shevchenko
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=20261123172836.130440-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®