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

Cong Nguyen posted 1 patch 1 month, 2 weeks ago
drivers/iio/adc/max1363.c | 8 ++++++++
1 file changed, 8 insertions(+)
[PATCH v2] iio: adc: max1363: sign-extend bipolar differential channel reads
Posted by Cong Nguyen 1 month, 2 weeks ago
MAX1363 differential channels are bipolar (scan_type.sign = 's'), 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: d1325cf45077 ("Staging: IIO: max1363 ADC driver")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-4
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
Changes in v2:
- Condense the commit message (Andy Shevchenko).
- Fix the Fixes: tag to the original driver commit d1325cf45077 rather than
  the staging->iio move; the bug has been present since the driver was added
  in staging (Andy Shevchenko).

 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
Re: [PATCH v2] iio: adc: max1363: sign-extend bipolar differential channel reads
Posted by Andy Shevchenko 1 month, 2 weeks ago
On Mon, Aug 10, 2026 at 11:44:46PM +0700, Cong Nguyen wrote:
> MAX1363 differential channels are bipolar (scan_type.sign = 's'), but

The (...) can also be dropped, but I leave that to Jonathan, no need to resend.

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

Now looks good to me,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] iio: adc: max1363: sign-extend bipolar differential channel reads
Posted by Jonathan Cameron 1 month, 1 week ago
On Mon, 10 Aug 2026 20:04:23 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Mon, Aug 10, 2026 at 11:44:46PM +0700, Cong Nguyen wrote:
> > MAX1363 differential channels are bipolar (scan_type.sign = 's'), but  
> 
> The (...) can also be dropped, but I leave that to Jonathan, no need to resend.
> 
> > 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.  
> 
> Now looks good to me,
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>

Applied.  Amazing that's sat there for so long given it is
a reasonably common part.  Oops - that younger me wrote just
as many bugs as the current version ;)

On the fixes-togreg branch but unlikely I'll do a pull request
until I've rebased that on 7.3-rc1 in a few weeks time.

Jonathan

>