Devicetree
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Marcelo Schmitt <marcelo.schmitt@analog.com>
Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux@analog.com, jic23@kernel.org,
	nuno.sa@analog.com, dlechner@baylibre.com, andy@kernel.org,
	Michael.Hennerich@analog.com, robh@kernel.org,
	krzk+dt@kernel.org, conor+dt@kernel.org, corbet@lwn.net,
	skhan@linuxfoundation.org, marcelo.schmitt1@gmail.com
Subject: Re: [PATCH v1 09/13] iio: adc: ad4134: Support SPI 4-wire mode
Date: Thu, 3 Sep 2026 09:39:48 +0300	[thread overview]
Message-ID: <apkWNHHKyf9e9_Q7@ashevche-desk.local> (raw)
In-Reply-To: <b7639db1c5f9b13f74d0c4f04edc8d9c11ac4727.1788368334.git.marcelo.schmitt@analog.com>

On Wed, Sep 02, 2026 at 02:24:25PM -0300, Marcelo Schmitt wrote:
> AD4134 devices can be wired in a few different ways. So far, only minimum
> I/O mode was supported. While minimum I/O mode allows interfacing with
> AD4134 with a reduced number of wires, that wiring configuration is not
> optimal for high-throughput data acquisition.
> 
> Extend AD4134 support to enable interfacing in SPI 4-wire configuration.

...

> struct ad4134_state {

I hope on each stage you run `pahole` to confirm that this is the optimal
layout.

>  	struct gpio_desc *odr_gpio;
>  	int refin_mv;
>  	bool crc_en;
> +	enum ad4134_spi_mode spi_mode;
> +	struct mux_state *mux_st[2];
>  	/*
>  	 * Synchronize access to members the of driver state, and ensure
>  	 * atomicity of consecutive register access operations.
>  	 */
>  	struct mutex lock;
> +	/*
> +	 * Ensure atomicity of access mode switch operations.
> +	 */
> +	struct mutex access_mode_lock;

>  };

...

> +static int ad4134_set_register_access(struct ad4134_state *st)
> +{
> +	int ret;
> +
> +	guard(mutex)(&st->access_mode_lock);

+ blank line.

> +	st->spi->mode = SPI_MODE_0;
> +	ret = spi_setup(st->spi);
> +	if (ret)
> +		return ret;
> +
> +	ret = mux_state_deselect(st->mux_st[AD4134_DOUT0_INPUT]);
> +	if (ret)
> +		dev_err(&st->spi->dev, "error on DOUT0 deselect: %d\n", ret);

> +	ret = mux_state_try_select(st->mux_st[AD4134_SDO_INPUT]);
> +	if (ret && ret != -EBUSY)
> +		return ret;

Reading this without a comment about EBUSY is difficult. Interpreting BUSY as
occupied mux channel, why do we return success?

> +	return 0;
> +}

...

> +static int ad4134_set_sample_access(struct ad4134_state *st)
> +{

	struct device *dev = &st->spi->dev;

> +	int ret, ret2;

I would go with

	int mux_state_ret;
	int ret;

> +	guard(mutex)(&st->access_mode_lock);
> +	ret = mux_state_deselect(st->mux_st[AD4134_SDO_INPUT]);
> +	if (ret)
> +		dev_err(&st->spi->dev, "error on SDO deselect: %d\n", ret);
> +
> +	ret = mux_state_try_select(st->mux_st[AD4134_DOUT0_INPUT]);
> +	if (ret) {
> +		dev_err(&st->spi->dev, "error on DOUT0 select: %d\n", ret);
> +		return mux_state_select(st->mux_st[AD4134_SDO_INPUT]);
> +	}
> +
> +	/*
> +	 * Data output on the DOUT lines is sampled on the falling edge
> +	 * (SPI mode 1).
> +	 */
> +	st->spi->mode = SPI_MODE_1;
> +	ret = spi_setup(st->spi);
> +	if (ret) {
> +		dev_err(&st->spi->dev, "failed to setup SPI mode 1: %d\n", ret);
> +		ret2 = mux_state_deselect(st->mux_st[AD4134_DOUT0_INPUT]);
> +		if (ret2)
> +			dev_err(&st->spi->dev, "error on DOUT0 deselect: %d\n", ret2);
> +
> +		ret2 = mux_state_select(st->mux_st[AD4134_SDO_INPUT]);
> +		if (ret2)
> +			dev_err(&st->spi->dev, "error on SDO select: %d\n", ret2);
> +
> +		return ret;
> +	}
> +
> +	return 0;
> +}

...

>  static int ad4134_reg_read(void *context, unsigned int reg, unsigned int *val)
>  {
>  	struct ad4134_state *st = context;
> +	int ret, ret2;
>  
> -	if (reg >= AD4134_CH_VREG(0))
> -		return ad4134_data_read(st, reg, val);
> +	if (reg >= AD4134_CH_VREG(0)) {
> +		if (st->spi_mode == AD4134_SPI_MODE_4_WIRE) {
> +			ret = ad4134_set_sample_access(st);
> +			if (ret)
> +				return ret;
> +		}
> +
> +		ret = ad4134_data_read(st, reg, val);
> +
> +		if (st->spi_mode == AD4134_SPI_MODE_4_WIRE) {
> +			ret2 = ad4134_set_register_access(st);
> +			if (ret2)
> +				dev_err(&st->spi->dev, "access mode error: %d\n", ret2);
> +		}

I would go with duplication of _data_read() call but better flow

		if (...) {
			_set_sample_()
			ret = _data_read();
			_set_register_()
		} else {
			ret = _data_read();
		}

> +		return ret;
> +	}

...

> +	ret = device_property_match_property_string(dev, "adi,spi-mode",
> +						    ad4134_spi_modes,
> +						    ARRAY_SIZE(ad4134_spi_modes));
> +	/* Default to "no-cs" mode if adi,spi-mode is not specified */
> +	if (ret == -EINVAL)

No, use device_property_present() instead.

> +		st->spi_mode = AD4134_SPI_MODE_NO_CS;
> +	else if (ret < 0)
>  		return dev_err_probe(dev, ret,
> -				     "failed to setup minimum I/O mode\n");
> +				     "getting adi,spi-mode property failed\n");
> +	else
> +		st->spi_mode = ret;

-- 
With Best Regards,
Andy Shevchenko



  parent reply	other threads:[~2026-09-03  6:41 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-02 17:21 [PATCH v1 00/13] iio: adc: ad4134: Enable greater sample rate data capture Marcelo Schmitt
2026-09-02 17:21 ` [PATCH v1 01/13] iio: Fix typo in vendor name Marcelo Schmitt
2026-09-03  6:22   ` Andy Shevchenko
2026-09-02 17:21 ` [PATCH v1 02/13] iio: adc: ad4134: Drop import to empty name space Marcelo Schmitt
2026-09-02 17:22 ` [PATCH v1 03/13] iio: adc: ad4134: Update include list to comply with IWYU principles Marcelo Schmitt
2026-09-02 17:40   ` sashiko-bot
2026-09-03  6:26   ` Andy Shevchenko
2026-09-02 17:22 ` [PATCH v1 04/13] iio: adc: ad4134: Serialize single-read operations Marcelo Schmitt
2026-09-03  6:27   ` Andy Shevchenko
2026-09-02 17:23 ` [PATCH v1 05/13] iio: adc: ad4134: Run shorter transfers when CRC is disabled Marcelo Schmitt
2026-09-02 17:42   ` sashiko-bot
2026-09-02 17:23 ` [PATCH v1 06/13] iio: adc: ad4134: Add support for digital filter type selection Marcelo Schmitt
2026-09-03  6:31   ` Andy Shevchenko
2026-09-02 17:23 ` [PATCH v1 07/13] iio: adc: ad4134: Support buffered data read Marcelo Schmitt
2026-09-02 17:38   ` sashiko-bot
2026-09-02 17:24 ` [PATCH v1 08/13] dt-bindings: iio: adc: adi,ad4134: Document SPI connection mode Marcelo Schmitt
2026-09-02 17:46   ` sashiko-bot
2026-09-03 18:14   ` Conor Dooley
2026-09-02 17:24 ` [PATCH v1 09/13] iio: adc: ad4134: Support SPI 4-wire mode Marcelo Schmitt
2026-09-02 17:46   ` sashiko-bot
2026-09-03  6:39   ` Andy Shevchenko [this message]
2026-09-02 17:24 ` [PATCH v1 10/13] dt-bindings: iio: adc: adi,ad4134: Document PWM usage Marcelo Schmitt
2026-09-02 17:25 ` [PATCH v1 11/13] dt-bindings: iio: adc: adi,ad4134: Add high data throughput example Marcelo Schmitt
2026-09-02 17:39   ` sashiko-bot
2026-09-02 17:25 ` [PATCH v1 12/13] iio: adc: ad4134: Support high-speed data capture Marcelo Schmitt
2026-09-02 17:49   ` sashiko-bot
2026-09-03  7:00   ` Andy Shevchenko
2026-09-02 17:25 ` [PATCH v1 13/13] Docs: iio: Add AD4134 Marcelo Schmitt

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=apkWNHHKyf9e9_Q7@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=Michael.Hennerich@analog.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@analog.com \
    --cc=marcelo.schmitt1@gmail.com \
    --cc=marcelo.schmitt@analog.com \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=skhan@linuxfoundation.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