From: Joshua Crofts <crofts@kernel.org>
To: Vaibhav Chahar <vaibhav.chahar26@gmail.com>,
Jonathan Cameron <jic23@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>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
Date: Thu, 26 Nov 2026 14:26:21 +0100 [thread overview]
Message-ID: <0a01f11c-86d3-437d-977f-dd5978542b86@kernel.org> (raw)
In-Reply-To: <20261126114433.20674-3-vaibhav.chahar26@gmail.com>
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
next prev parent reply other threads:[~2026-11-26 13:26 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 11:44 [PATCH v4 0/2] iio: adc: " 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 [this message]
2026-11-26 13:26 ` [PATCH v4 0/2] iio: adc: " Joshua Crofts
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=0a01f11c-86d3-437d-977f-dd5978542b86@kernel.org \
--to=crofts@kernel.org \
--cc=andy@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 \
--cc=vaibhav.chahar26@gmail.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®