Skip to contentSkip to conversation

iio: adc: max1363: sign-extend bipolar differential channel reads

PATCH Superseded Cong Nguyen 3 participants 4 messages Aug 10 – Aug 17 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: max1363: sign-extend bipolar differential channel reads

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.")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/adc/max1363.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iio/adc/max1363.c +8 -0
index 4d0b79cfeb27..497f5daadcea 100644
@@ -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;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Cong Nguyen

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).

Quoted 4 lines

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.

Quoted 1 line
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).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Cong Nguyen

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).

Quoted 2 lines

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.

Quoted 8 lines
Quoted 5 lines

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.

Quoted 8 lines
Quoted 1 line
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

maintainer
#3 lore raw
Quoted 31 lines · On Mon, 10 Aug 2026 23:50:53 +0700 Nguyễn Công <[email protected]> wrote:
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Cong Nguyen

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).

Quoted 2 lines

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.

Quoted 13 lines
Quoted 5 lines

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.

Quoted 1 line
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).

Quoted 8 lines
Quoted 3 lines

--
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.

Trivial thing to watch out for - don't put anything a thread that smells like a tag. b4 tends to scrape them and if I'm not paying attention (or like in this case my git hooks catch it) we end up with bonus tags.

Quoted 2 lines

Thanks for the review.
Cong