public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Erick Henrique <erick.henrique.rodrigues@usp.br>
Cc: dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	andriy.shevchenko@intel.com, linux-iio@vger.kernel.org
Subject: Re: [PATCH v3 2/2] iio: dac: m62332: Use guard(mutex) for locking
Date: Sun, 19 Apr 2026 14:49:58 +0100	[thread overview]
Message-ID: <20260419144958.03394ed5@jic23-huawei> (raw)
In-Reply-To: <20260418130322.106769-3-erick.henrique.rodrigues@usp.br>

On Sat, 18 Apr 2026 10:03:22 -0300
Erick Henrique <erick.henrique.rodrigues@usp.br> wrote:

> Replace mutex_lock()/mutex_unlock() calls with guard(mutex)() to
> simplify locking and make cleanup automatic when the lock goes out
> of scope. Also simplify the i2c_master_send() error handling by
> using sequential early returns instead of a combined condition.
> 
> Signed-off-by: Erick Henrique <erick.henrique.rodrigues@usp.br>
> ---
>  drivers/iio/dac/m62332.c | 18 ++++++------------
>  1 file changed, 6 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/iio/dac/m62332.c b/drivers/iio/dac/m62332.c
> index 4b139904e818..a9bb6c307431 100644
> --- a/drivers/iio/dac/m62332.c
> +++ b/drivers/iio/dac/m62332.c
> @@ -10,6 +10,7 @@
>  
>  #include <linux/array_size.h>
>  #include <linux/bits.h>
> +#include <linux/cleanup.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
>  #include <linux/i2c.h>
> @@ -46,33 +47,26 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
>  	outbuf[0] = channel;
>  	outbuf[1] = val;
>  
> -	mutex_lock(&data->mutex);
> +	guard(mutex)(&data->mutex);
>  
>  	if (val) {
>  		res = regulator_enable(data->vcc);
>  		if (res)
> -			goto out;
> +			return res;
>  	}
>  
>  	res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
> -	if (res >= 0 && res != ARRAY_SIZE(outbuf))
> -		res = -EIO;
>  	if (res < 0)
> -		goto out;
> +		return res;
> +	if (res != ARRAY_SIZE(outbuf))
> +		return -EIO;
>  
>  	data->raw[channel] = val;
>  
>  	if (!val)
>  		regulator_disable(data->vcc);
Not related to this patch, but sashiko is raising what looks to me
to be a valid point about this regulator handling.
https://sashiko.dev/#/patchset/20260418130322.106769-1-erick.henrique.rodrigues%40usp.br
(note some of the other 'bugs' reported' aren't bugs, but this one looks like it is
real to me)

The point is that a transition from val = 1, to val = 2 will enable
the regulator twice and then a transition to val = 0 will disable it only once
leaving the regulator (which is reference counted) turned on

J
>  
> -	mutex_unlock(&data->mutex);
> -
>  	return 0;
> -
> -out:
> -	mutex_unlock(&data->mutex);
> -
> -	return res;
>  }
>  
>  static int m62332_read_raw(struct iio_dev *indio_dev,


  parent reply	other threads:[~2026-04-19 13:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-14 23:39 [PATCH] iio: dac: m62332: Use guard(mutex) for locking Erick Henrique
2026-04-15 10:20 ` Andy Shevchenko
2026-04-15 23:01 ` [PATCH v2] " Erick Henrique
2026-04-17  7:58   ` Andy Shevchenko
2026-04-17  7:59     ` Andy Shevchenko
2026-04-18 13:03 ` [PATCH v3 0/2] " Erick Henrique
2026-04-18 13:03   ` [PATCH v3 1/2] iio: dac: m62332: Clean up header includes Erick Henrique
2026-04-19 12:16     ` Jonathan Cameron
2026-04-18 13:03   ` [PATCH v3 2/2] iio: dac: m62332: Use guard(mutex) for locking Erick Henrique
2026-04-19 12:17     ` Jonathan Cameron
2026-04-19 13:49     ` Jonathan Cameron [this message]
2026-04-19 12:11   ` [PATCH v3 0/2] " Jonathan Cameron

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=20260419144958.03394ed5@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=erick.henrique.rodrigues@usp.br \
    --cc=linux-iio@vger.kernel.org \
    --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