Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
To: Erick Henrique <erick.henrique.rodrigues@usp.br>
Cc: andriy.shevchenko@intel.com, andy@kernel.org,
	dlechner@baylibre.com, nuno.sa@analog.com,
	joshua.crofts1@gmail.com, sashiko-bot@kernel.org,
	linux-iio@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v2] iio: dac: m62332: Fix regulator reference count imbalance
Date: Sat, 11 Jul 2026 23:34:25 +0100	[thread overview]
Message-ID: <20260711233425.37dbb268@jic23-huawei> (raw)
In-Reply-To: <20260703205236.201834-1-erick.henrique.rodrigues@usp.br>

On Fri,  3 Jul 2026 17:52:36 -0300
Erick Henrique <erick.henrique.rodrigues@usp.br> wrote:

> m62332_set_value() enables the Vcc regulator on every write of a
> non-zero value and disables it on every write of zero, without tracking
> the channel's current state. Because the regulator is reference counted,
> changing a channel directly from one non-zero value to another enables
> it more than once, while a later write of zero disables it only once.
> The reference count never returns to zero and the regulator is left
> enabled indefinitely.
> 
> Only enable the regulator on the transition from zero to non-zero, and
> only disable it on the transition from non-zero to zero, using the
> previously stored channel value to detect the edge. Balance the
> regulator on the I2C error path so the reference count stays consistent
> if the write fails.
> 
> Fixes: b87b0c0f81e8 ("iio: add m62332 DAC driver")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260418130322.106769-1-erick.henrique.rodrigues%40usp.br
> Cc: stable@vger.kernel.org
> Signed-off-by: Erick Henrique <erick.henrique.rodrigues@usp.br>
Applied to the fixes-togreg branch of iio.git

Thanks,

Jonathan

> ---
> v2:
> - Use local enabling/disabling booleans for the edge conditions (Jonathan)
> - Credit Sashiko directly in Reported-by with a Closes: link to its
>   report entry, per Jonathan
> v1: https://lore.kernel.org/r/20260630021309.36636-1-erick.henrique.rodrigues@usp.br
> 
>  drivers/iio/dac/m62332.c | 17 ++++++++++++-----
>  1 file changed, 12 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/iio/dac/m62332.c b/drivers/iio/dac/m62332.c
> index 3497513854d7..2c13feee8d61 100644
> --- a/drivers/iio/dac/m62332.c
> +++ b/drivers/iio/dac/m62332.c
> @@ -32,6 +32,7 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
>  {
>  	struct m62332_data *data = iio_priv(indio_dev);
>  	struct i2c_client *client = data->client;
> +	bool enabling, disabling;
>  	u8 outbuf[2];
>  	int res;
>  
> @@ -43,7 +44,10 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
>  
>  	mutex_lock(&data->mutex);
>  
> -	if (val) {
> +	enabling = val && !data->raw[channel];
> +	disabling = !val && data->raw[channel];
> +
> +	if (enabling) {
>  		res = regulator_enable(data->vcc);
>  		if (res)
>  			goto out;
> @@ -52,14 +56,17 @@ static int m62332_set_value(struct iio_dev *indio_dev, u8 val, int channel)
>  	res = i2c_master_send(client, outbuf, ARRAY_SIZE(outbuf));
>  	if (res >= 0 && res != ARRAY_SIZE(outbuf))
>  		res = -EIO;
> -	if (res < 0)
> +	if (res < 0) {
> +		if (enabling)
> +			regulator_disable(data->vcc);
>  		goto out;
> +	}
>  
> -	data->raw[channel] = val;
> -
> -	if (!val)
> +	if (disabling)
>  		regulator_disable(data->vcc);
>  
> +	data->raw[channel] = val;
> +
>  	mutex_unlock(&data->mutex);
>  
>  	return 0;


      reply	other threads:[~2026-07-11 22:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03 20:52 [PATCH v2] iio: dac: m62332: Fix regulator reference count imbalance Erick Henrique
2026-07-11 22:34 ` Jonathan Cameron [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=20260711233425.37dbb268@jic23-huawei \
    --to=jonathan.cameron@oss.qualcomm.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=erick.henrique.rodrigues@usp.br \
    --cc=joshua.crofts1@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    /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