Devicetree
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
Cc: Marcelo Schmitt <marcelo.schmitt@analog.com>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, nuno.sa@analog.com,
	Michael.Hennerich@analog.com, dlechner@baylibre.com,
	andy@kernel.org, robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, pop.ioan-daniel@analog.com
Subject: Re: [PATCH v2 2/4] iio: adc: ltc2378: Add support for LTC2378-20 and similar ADCs
Date: Fri, 29 May 2026 18:31:41 +0100	[thread overview]
Message-ID: <20260529183141.4a8e236b@jic23-huawei> (raw)
In-Reply-To: <ahmIbL8sIwXaysff@debian-BULLSEYE-live-builder-AMD64>

On Fri, 29 May 2026 09:37:00 -0300
Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote:

> On 05/29, Jonathan Cameron wrote:
> > On Thu, 28 May 2026 12:03:52 -0300
> > Marcelo Schmitt <marcelo.schmitt@analog.com> wrote:
> >   
> > > Support for LTC2378-20 and similar analog-to-digital converters.
> > > 
> > > Signed-off-by: Marcelo Schmitt <marcelo.schmitt@analog.com>  
> >   
> ...
> > >  
> > > +config LTC2378
> > > +	tristate "Analog Devices LTC2378 ADC driver"
> > > +	depends on SPI
> > > +	depends on GPIOLIB || PWM  
> > 
> > Triggered sashiko:
> > https://sashiko.dev/#/patchset/cover.1779976379.git.marcelo.schmitt%40analog.com
> > This dependency is odd enough that I think I'd add a comment on why.
> > Also bring in the dependency on PWM in the patch that needs it not this one.
> > 
> > Will it build without gpiolib? If so I'd prefer that to be a || COMPILE_TEST
> > just to get a little more coverage.
> >   
> The intent was to allow using the ADC with offload PWM triggered setup
> even if the GPIO is not provided/enabled. We would be able to do the buffered
> reads, but the single-shot read would be debatable since we would very likely
> trigger many conversions in between a pwm_enable()/pwm_disable(). In the doubt,
> I left the single-read procedure untouched specially because there's also the
> case where the GPIO is there and so the single-read would work normally.
> 
>      +-------------+         +-------------+
>      |         CNV |<-----+--| GPIO        |
>      |             |      +--| PWM0        |
>      |             |         |             |
>      |             |      +--| PWM1        |
>      |             |      |  +-------------+
>      |             |      +->| TRIGGER     |
>      |             |         |             |
>      |     ADC     |         |    SPI      |
>      |             |         | controller  |
>      |             |         |             |
>      |         SDI |<--------| SDO         |
>      |         SDO |-------->| SDI         |
>      |        SCLK |<--------| SCLK        |
>      +-------------+         +-------------+
> 
> Maybe I'm going too much flexible trying to allow all those combinations?
> Simplest way to avoid what sashiko points as garbage read is to go straight
> 'depends on GPIOLIB' and check the GPIO must always be there.

Yes. I'd do that.  Almost no one will build without that anyway.

Does the code remove the single read if the gpio isn't available?
Sounds like it probably should.

> 
> >   
> ...
> > > +static int ltc2378_convert_and_acquire(struct ltc2378_state *st)
> > > +{
> > > +	int ret;
> > > +
> > > +	/* Cause a rising edge of CNV to initiate a new ADC conversion */
> > > +	gpiod_set_value_cansleep(st->cnv_gpio, 1);  
> > 
> > Should we check for errors on setting the gpio?
> >   
> > > +	fsleep(4);
> > > +	ret = spi_sync_transfer(st->spi, &st->xfer, 1);
> > > +	gpiod_set_value_cansleep(st->cnv_gpio, 0);
> > > +
> > > +	return ret;
> > > +}
> > > +
> > > +static int ltc2378_channel_single_read(const struct iio_chan_spec *chan,
> > > +				       struct ltc2378_state *st, int *val)
> > > +{
> > > +	const struct iio_scan_type *scan_type = &chan->scan_type;
> > > +	u32 sample;
> > > +	int ret;
> > > +
> > > +	ret = ltc2378_convert_and_acquire(st);
> > > +	if (ret)
> > > +		return ret;
> > > +
> > > +	if (scan_type->realbits > 16)
> > > +		sample = st->scan.data.sample_buf32;  
> > 
> > Sashikio has an interesting theory here that you need a shift. I can't immediately
> > spot why it is wrong.  Given we are clocking in 32bits I assume the first
> > res_bits are valid then we get a bunch of garbage after that. Given it's MSB first
> > the SPI controller should put those in the top bits of the resulting u32 with the
> > garbage at the bottom. So shouldn't we shift them down by 32 - resolution or something
> > like that? + in buffered case provide the relevant shift value (in later patches)
> >   
> The only way I'm seing to get sample_buf32 with ADC data in the MSB is if the
> CPU endianness is BE. Otherwise (LE), the data bits would be put into the lower
> addresses when SPI puts the word in CPU endianness.

We aren't talking addreses here we are talking about handling of a 4 byte s32
(which is also what the sign extend is operating on).

> But, if we do unconditional
> shift, won't that break sign_extend32() for LE CPUs? Well, I'll anyway double
> check the data for all sample_buf16/sample_buf32 buffer/non-buffer use cases.

It's operating on an s32 so say we extend 0xfabc from bit 15

be is 

[0x00] [0x00] [0xfa] [0xbc] =>  [0xff] [0xff] [0xfa] [0xbc]

le

[0xbc] [0xfa] [0x00] [0x00] =>  [0xbc] [0xfa] [0xff] [0xff]

Which are both 0xfffffabc as an s32 (in hex obviously)

Arguement in this case is the value is actually in the top few
bytes so the ones that are 0 in the example above.


> 
> > > +	else
> > > +		sample = st->scan.data.sample_buf16;
> > > +
> > > +	if (scan_type->format == IIO_SCAN_FORMAT_SIGNED_INT)
> > > +		*val = sign_extend32(sample, scan_type->realbits - 1);
> > > +	else
> > > +		*val = sample;
> > > +
> > > +	return 0;
> > > +}
> > > +  
> ...
> 
> > case IIO_CHAN_INFO_RAW: {
> >   
> > > +		IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
> > > +		if (IIO_DEV_ACQUIRE_FAILED(claim))
> > > +			return -EBUSY;
> > > +
> > > +		ret = ltc2378_channel_single_read(chan, st, val);
> > > +		if (ret)
> > > +			return ret;
> > > +
> > > +		return IIO_VAL_INT;
> > > +  
> > }
> > 
> > As that IIO_DEV_ACQUIRE_DIRECT_MODE() is declaring a local variable
> > so there is something go out of scope and result in cleanup.
> > LLVM moans about this, GCC just does the wrong thing which can in theory
> > at least result in an underflow.
> > You'll get build bot warnings on this - bug sashiko caught it.
> >   
> 
> Oops I guess I started mis-squashing commits after a certain number of rebases XD

Happens to us all.  Always do a last sanity check with an LLVM build
(it finds way more than GCC currently!) one patch at a time.

> Will fix this and all other suggestions.
> 
> Thanks


  reply	other threads:[~2026-05-29 17:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-28 15:02 [PATCH v2 0/4] iio: adc: Add support for LTC2378 and similar ADCs Marcelo Schmitt
2026-05-28 15:03 ` [PATCH v2 1/4] dt-bindings: iio: adc: Add ltc2378 Marcelo Schmitt
2026-05-28 16:55   ` Conor Dooley
2026-05-28 15:03 ` [PATCH v2 2/4] iio: adc: ltc2378: Add support for LTC2378-20 and similar ADCs Marcelo Schmitt
2026-05-28 15:51   ` sashiko-bot
2026-05-29 10:02   ` Jonathan Cameron
2026-05-29 12:37     ` Marcelo Schmitt
2026-05-29 17:31       ` Jonathan Cameron [this message]
2026-05-28 15:04 ` [PATCH v2 3/4] iio: adc: ltc2378: Enable high-speed data capture Marcelo Schmitt
2026-05-28 16:39   ` sashiko-bot
2026-05-29 10:29   ` Jonathan Cameron
2026-05-28 15:04 ` [PATCH v2 4/4] iio: adc: ltc2378: Enable triggered buffer " Marcelo Schmitt
2026-05-28 17:21   ` sashiko-bot
2026-05-29 10:30   ` Jonathan Cameron
2026-06-03  7:59 ` [PATCH v2 0/4] iio: adc: Add support for LTC2378 and similar ADCs Andy Shevchenko
2026-06-03 14:11   ` 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=20260529183141.4a8e236b@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=dlechner@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo.schmitt1@gmail.com \
    --cc=marcelo.schmitt@analog.com \
    --cc=nuno.sa@analog.com \
    --cc=pop.ioan-daniel@analog.com \
    --cc=robh@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