public inbox for devicetree@vger.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Rodrigo Alencar via B4 Relay
	<devnull+rodrigo.alencar.analog.com@kernel.org>
Cc: rodrigo.alencar@analog.com, linux-iio@vger.kernel.org,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Michael Auchter <michael.auchter@ni.com>,
	linux-hardening@vger.kernel.org,
	Lars-Peter Clausen <lars@metafoo.de>,
	Michael Hennerich <Michael.Hennerich@analog.com>,
	David Lechner <dlechner@baylibre.com>,
	Andy Shevchenko <andy@kernel.org>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>, Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	Philipp Zabel <p.zabel@pengutronix.de>
Subject: Re: [PATCH 22/22] iio: dac: ad5686: add gain control support
Date: Thu, 23 Apr 2026 19:29:45 +0100	[thread overview]
Message-ID: <20260423192945.2be798a0@jic23-huawei> (raw)
In-Reply-To: <20260422-ad5313r-iio-support-v1-22-ed7dca001d1b@analog.com>

On Wed, 22 Apr 2026 15:45:56 +0100
Rodrigo Alencar via B4 Relay <devnull+rodrigo.alencar.analog.com@kernel.org> wrote:

> From: Rodrigo Alencar <rodrigo.alencar@analog.com>
> 
> Most of the supported devices rely on a GAIN pin to control a 2x
> multiplier applied to the output voltage. Other devices, e.g. the
> single-channel ones, provides a gain control through a bit field in the
> control register. Some designs might have the GAIN pin hardwired to
> VDD/VLOGIC or GND, which would still be fine for this patch, that allows
> the scale property to be configurable with two available options. In
> read_raw() and write_raw() implementations mutex guards are used to allow
> early returns.
> 
> Signed-off-by: Rodrigo Alencar <rodrigo.alencar@analog.com>
Minor stuff inline.

Thanks,

Jonathan

> ---
>  drivers/iio/dac/ad5686.c | 110 +++++++++++++++++++++++++++++++++++++++--------
>  drivers/iio/dac/ad5686.h |   8 ++++
>  2 files changed, 101 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c
> index bec951afe8d0..adbf62848697 100644
> --- a/drivers/iio/dac/ad5686.c
> +++ b/drivers/iio/dac/ad5686.c

>  static inline int ad5686_pd_mask_shift(const struct iio_chan_spec *chan)
> @@ -163,20 +167,25 @@ static int ad5686_read_raw(struct iio_dev *indio_dev,
>  	struct ad5686_state *st = iio_priv(indio_dev);
>  	int ret;
>  
> +	guard(mutex)(&st->lock);
As below. I'd break out the guard() usage as a precusor cleanup patch.

> +
>  	switch (m) {
>  	case IIO_CHAN_INFO_RAW:
> -		mutex_lock(&st->lock);
>  		ret = ad5686_read(st, chan->address);
> -		mutex_unlock(&st->lock);
>  		if (ret < 0)
>  			return ret;
>  		*val = (ret >> chan->scan_type.shift) &
>  			GENMASK(chan->scan_type.realbits - 1, 0);
>  		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_SCALE:
> -		*val = st->vref_mv;
> -		*val2 = chan->scan_type.realbits;
> -		return IIO_VAL_FRACTIONAL_LOG2;
> +		if (st->double_scale) {
> +			*val = st->scale_avail[2];
> +			*val2 = st->scale_avail[3];
> +		} else {
> +			*val = st->scale_avail[0];
> +			*val2 = st->scale_avail[1];
> +		}
> +		return IIO_VAL_INT_PLUS_NANO;
>  	}
>  	return -EINVAL;
>  }
> @@ -188,28 +197,77 @@ static int ad5686_write_raw(struct iio_dev *indio_dev,
>  			    long mask)
>  {
>  	struct ad5686_state *st = iio_priv(indio_dev);
> -	int ret;
> +
> +	guard(mutex)(&st->lock);
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_RAW:
>  		if (!in_range(val, 0, 1 << chan->scan_type.realbits))
>  			return -EINVAL;
>  
> -		mutex_lock(&st->lock);
> -		ret = ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
> -				   chan->address, val << chan->scan_type.shift);
> -		mutex_unlock(&st->lock);

As Andy pointed out, move this switch to guard() magic earlier in series.


> -		break;
> -	default:
> -		ret = -EINVAL;
> -	}
> +		return ad5686_write(st, AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
> +				    chan->address, val << chan->scan_type.shift);
> +	case IIO_CHAN_INFO_SCALE:
> +		if (val == st->scale_avail[0] && val2 == st->scale_avail[1])
> +			st->double_scale = false;
> +		else if (val == st->scale_avail[2] && val2 == st->scale_avail[3])
> +			st->double_scale = true;
> +		else
> +			return -EINVAL;
>  
> -	return ret;
> +		switch (st->chip_info->regmap_type) {
> +		case AD5310_REGMAP:
> +			return ad5310_control_sync(st);
> +		case AD5683_REGMAP:
> +			return ad5683_control_sync(st);
> +		case AD5686_REGMAP:
> +			gpiod_set_value_cansleep(st->gain_gpio, st->double_scale);
> +			return 0;
> +		default:
> +			return -EINVAL;
> +		}
> +	default:
> +		return -EINVAL;
> +	}
> +}
>

  reply	other threads:[~2026-04-23 18:29 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-22 14:45 [PATCH 00/22] Extend device support for AD5686 driver Rodrigo Alencar via B4 Relay
2026-04-22 14:45 ` [PATCH 01/22] dt-bindings: iio: dac: ad5696: extend device support Rodrigo Alencar via B4 Relay
2026-04-23 17:33   ` Conor Dooley
2026-04-22 14:45 ` [PATCH 02/22] dt-bindings: iio: dac: ad5696: add reset/ldac/gain gpio support Rodrigo Alencar via B4 Relay
2026-04-23 17:34   ` Conor Dooley
2026-04-22 14:45 ` [PATCH 03/22] dt-bindings: iio: dac: ad5696: rework on power supplies Rodrigo Alencar via B4 Relay
2026-04-23 17:47   ` Jonathan Cameron
2026-04-24  7:44     ` Rodrigo Alencar
2026-04-24 17:04       ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 04/22] dt-bindings: iio: dac: ad5686: extend device support Rodrigo Alencar via B4 Relay
2026-04-23 17:33   ` Conor Dooley
2026-04-22 14:45 ` [PATCH 05/22] dt-bindings: iio: dac: ad5686: add reset/ldac/gain gpio support Rodrigo Alencar via B4 Relay
2026-04-23 17:34   ` Conor Dooley
2026-04-22 14:45 ` [PATCH 06/22] dt-bindings: iio: dac: ad5686: rework on power supplies Rodrigo Alencar via B4 Relay
2026-04-23 17:32   ` Conor Dooley
2026-04-24  7:35     ` Rodrigo Alencar
2026-04-24 17:10       ` Conor Dooley
2026-04-22 14:45 ` [PATCH 07/22] iio: dac: ad5686: refactor include headers Rodrigo Alencar via B4 Relay
2026-04-22 19:43   ` Andy Shevchenko
2026-04-22 14:45 ` [PATCH 08/22] iio: dac: ad5686: remove redundant register definition Rodrigo Alencar via B4 Relay
2026-04-22 19:47   ` Andy Shevchenko
2026-04-22 14:45 ` [PATCH 09/22] iio: dac: ad5686: drop enum id Rodrigo Alencar via B4 Relay
2026-04-22 14:45 ` [PATCH 10/22] iio: dac: ad5686: add of_match table to the spi driver Rodrigo Alencar via B4 Relay
2026-04-22 14:45 ` [PATCH 11/22] iio: dac: ad5686: fix ref bit initialization for single-channel parts Rodrigo Alencar via B4 Relay
2026-04-22 19:58   ` Andy Shevchenko
2026-04-23 17:56   ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 12/22] iio: dac: ad5686: fix powerdown control Rodrigo Alencar via B4 Relay
2026-04-22 20:25   ` Andy Shevchenko
2026-04-23 17:59   ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 13/22] iio: dac: ad5686: fix input raw value check Rodrigo Alencar via B4 Relay
2026-04-23  8:13   ` Nuno Sá
2026-04-23 18:03   ` Jonathan Cameron
2026-04-24  8:03     ` Nuno Sá
2026-04-22 14:45 ` [PATCH 14/22] iio: dac: ad5686: add support for missing power supplies Rodrigo Alencar via B4 Relay
2026-04-23 18:05   ` Jonathan Cameron
2026-04-24  8:08     ` Rodrigo Alencar
2026-04-24 17:10       ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 15/22] iio: dac: ad5686: create bus ops struct Rodrigo Alencar via B4 Relay
2026-04-23 18:09   ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 16/22] iio: dac: ad5686: extend device support with new parts Rodrigo Alencar via B4 Relay
2026-04-22 14:45 ` [PATCH 17/22] iio: dac: ad5686: update device list description Rodrigo Alencar via B4 Relay
2026-04-22 20:06   ` Andy Shevchenko
2026-04-22 20:07     ` Andy Shevchenko
2026-04-22 14:45 ` [PATCH 18/22] iio: dac: ad5686: consume optional reset signal Rodrigo Alencar via B4 Relay
2026-04-24  9:25   ` Philipp Zabel
2026-04-22 14:45 ` [PATCH 19/22] iio: dac: ad5686: add ldac gpio Rodrigo Alencar via B4 Relay
2026-04-22 14:45 ` [PATCH 20/22] iio: dac: ad5686: implement new sync() op for the spi bus Rodrigo Alencar via B4 Relay
2026-04-23 18:18   ` Jonathan Cameron
2026-04-24  8:58     ` Rodrigo Alencar
2026-04-24 17:02       ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 21/22] iio: dac: ad5686: add triggered buffer support Rodrigo Alencar via B4 Relay
2026-04-23 18:27   ` Jonathan Cameron
2026-04-24  9:20     ` Rodrigo Alencar
2026-04-24 17:11       ` Jonathan Cameron
2026-04-22 14:45 ` [PATCH 22/22] iio: dac: ad5686: add gain control support Rodrigo Alencar via B4 Relay
2026-04-23 18:29   ` Jonathan Cameron [this message]
2026-04-22 20:28 ` [PATCH 00/22] Extend device support for AD5686 driver Andy Shevchenko
2026-04-23 18:32   ` Jonathan Cameron
2026-04-23  8:44 ` Krzysztof Kozlowski

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=20260423192945.2be798a0@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Michael.Hennerich@analog.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=devnull+rodrigo.alencar.analog.com@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.auchter@ni.com \
    --cc=p.zabel@pengutronix.de \
    --cc=robh@kernel.org \
    --cc=rodrigo.alencar@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