Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio/adc: (max1363) Fix data conversion problems

For chips with more than 8 bit ADC resolution, received data was always
masked against 0xfff, ie with a 12 bit mask. This can result in bad data
for chips with 10 bit resolution if those chips have higher bits set
(seen with MAX1139).

The receive buffer was defined as char array. This could result in
unintentional sign extensions if the upper bit in a received byte
was set. Since the chip is configured for unipolar mode, we never
have to handle negative values, and sign extensions are never needed.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Guenter Roeck and committed by
Jonathan Cameron
482bb4e6 dcb9ca0a

+3 -2
+3 -2
drivers/iio/adc/max1363.c
··· 335 335 { 336 336 int ret = 0; 337 337 s32 data; 338 - char rxbuf[2]; 338 + u8 rxbuf[2]; 339 339 struct max1363_state *st = iio_priv(indio_dev); 340 340 struct i2c_client *client = st->client; 341 341 ··· 367 367 ret = data; 368 368 goto error_ret; 369 369 } 370 - data = (s32)(rxbuf[1]) | ((s32)(rxbuf[0] & 0x0F)) << 8; 370 + data = (rxbuf[1] | rxbuf[0] << 8) & 371 + ((1 << st->chip_info->bits) - 1); 371 372 } else { 372 373 /* Get reading */ 373 374 data = i2c_master_recv(client, rxbuf, 1);