All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: adc: max1363: sign-extend bipolar differential channel reads
@ 2026-08-10  6:42 Cong Nguyen
  2026-08-10  6:45 ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Cong Nguyen @ 2026-08-10  6:42 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
	linux-kernel

The MAX1363 differential channels are bipolar (scan_type.sign = 's') and
the device returns those samples in two's complement. However
max1363_read_single_chan() masks the raw value to the ADC resolution and
stores it as-is, without sign extension. A negative differential reading
is therefore reported to userspace as a large positive value (for a 12-bit
part, -1 reads back as 4095).

These channels expose only IIO_CHAN_INFO_RAW (no offset), so the raw sysfs
value is expected to be the signed result. Sign-extend the masked value
from the resolution bit for differential channels before returning it.
Single-ended channels are unipolar (sign = 'u') and are left unchanged.

Fixes: 168c9d95a940 ("iio:adc:max1363 move from staging.")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-4
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
 drivers/iio/adc/max1363.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index 4d0b79cfeb27..497f5daadcea 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -405,6 +405,14 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev,
 
 		data = rxbuf[0];
 	}
+
+	/*
+	 * Differential channels are bipolar and the device returns the sample
+	 * in two's complement, so sign-extend it from the resolution bit.
+	 */
+	if (chan->differential)
+		data = sign_extend32(data, st->chip_info->bits - 1);
+
 	*val = data;
 
 	return 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: max1363: sign-extend bipolar differential channel reads
  2026-08-10  6:42 [PATCH] iio: adc: max1363: sign-extend bipolar differential channel reads Cong Nguyen
@ 2026-08-10  6:45 ` Andy Shevchenko
  2026-08-10 16:50   ` Nguyễn Công
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-08-10  6:45 UTC (permalink / raw)
  To: Cong Nguyen
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

On Mon, Aug 10, 2026 at 9:42 AM Cong Nguyen <congnt264@gmail.com> wrote:
>
> The MAX1363 differential channels are bipolar (scan_type.sign = 's') and
> the device returns those samples in two's complement. However
> max1363_read_single_chan() masks the raw value to the ADC resolution and
> stores it as-is, without sign extension. A negative differential reading
> is therefore reported to userspace as a large positive value (for a 12-bit
> part, -1 reads back as 4095).

Try to make it more compact (remove unneeded or duplicative details,
do not blindly rely on AI).

> These channels expose only IIO_CHAN_INFO_RAW (no offset), so the raw sysfs
> value is expected to be the signed result. Sign-extend the masked value
> from the resolution bit for differential channels before returning it.
> Single-ended channels are unipolar (sign = 'u') and are left unchanged.

Same here.

> Fixes: 168c9d95a940 ("iio:adc:max1363 move from staging.")

This is a wrong reference in case the driver existed before (and that
is suggested by the commit description).

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: max1363: sign-extend bipolar differential channel reads
  2026-08-10  6:45 ` Andy Shevchenko
@ 2026-08-10 16:50   ` Nguyễn Công
  0 siblings, 0 replies; 3+ messages in thread
From: Nguyễn Công @ 2026-08-10 16:50 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

On Mon, Aug 10, 2026 at 1:45 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Mon, Aug 10, 2026 at 9:42 AM Cong Nguyen <congnt264@gmail.com> wrote:
> >
> > The MAX1363 differential channels are bipolar (scan_type.sign = 's') and
> > the device returns those samples in two's complement. However
> > max1363_read_single_chan() masks the raw value to the ADC resolution and
> > stores it as-is, without sign extension. A negative differential reading
> > is therefore reported to userspace as a large positive value (for a 12-bit
> > part, -1 reads back as 4095).
>
> Try to make it more compact (remove unneeded or duplicative details,
> do not blindly rely on AI).

Done in v2 - trimmed both paragraphs down to the bug, one example, and
the fix.

>
> > These channels expose only IIO_CHAN_INFO_RAW (no offset), so the raw sysfs
> > value is expected to be the signed result. Sign-extend the masked value
> > from the resolution bit for differential channels before returning it.
> > Single-ended channels are unipolar (sign = 'u') and are left unchanged.
>
> Same here.
>
> > Fixes: 168c9d95a940 ("iio:adc:max1363 move from staging.")
>
> This is a wrong reference in case the driver existed before (and that
> is suggested by the commit description).
>
> --
> With Best Regards,
> Andy Shevchenko

Right, the bug predates the move. The driver was added in 2010 by
d1325cf45077 ("Staging: IIO: max1363 ADC driver"), which already reads
differential channels with an unsigned mask, so v2 uses that as the
Fixes: tag.
Thanks for the review.
Cong

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-08-10 16:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-10  6:42 [PATCH] iio: adc: max1363: sign-extend bipolar differential channel reads Cong Nguyen
2026-08-10  6:45 ` Andy Shevchenko
2026-08-10 16:50   ` Nguyễn Công

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.