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

MAX1363 differential channels are bipolar, but max1363_read_single_chan()
masks the raw value to the ADC resolution without sign-extending it.
Negative differential readings are therefore reported to userspace as
large positive values (e.g. -1 as 4095 on a 12-bit part).

Sign-extend the masked value from the resolution bit for differential
channels. Single-ended channels are unipolar and are left unchanged.

Fixes: d1325cf450 ("Staging: IIO: max1363 ADC driver")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-4
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
This commit is contained in:
Cong Nguyen 2026-08-10 23:44:46 +07:00 committed by Jonathan Cameron
parent fafefa925e
commit 91908c0a78

View File

@ -404,6 +404,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;