[PATCH] iio: adc: max1363: sign-extend bipolar differential channel reads

Cong Nguyen <[email protected]>
Newsgroups org.kernel.vger.linux-iio,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
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: [email protected]
Assisted-by: Claude:claude-opus-4
Signed-off-by: Cong Nguyen <[email protected]>
---
 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.