Skip to contentSkip to conversation

iio: magnetometer: ak8974: Fix negative raw values in sysfs

PATCH In review Stephan Gerhold 2 participants 2 messages Feb 14, 2020 – Feb 16, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: magnetometer: ak8974: Fix negative raw values in sysfs

At the moment, reading from in_magn_*_raw in sysfs tends to return large values around 65000, even though the output of ak8974 is actually limited to ±32768. This happens because the value is never converted to the signed 16-bit integer variant.

Add an explicit cast to s16 to fix this.

Cc Linus Walleij <[email protected]>
Fixes 7c94a8b2ee8c ("iio: magn: add a driver for AK8974")
---
 drivers/iio/magnetometer/ak8974.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/magnetometer/ak8974.c +1 -1
index fc7e910f8e8b..d32996702110 100644
@@ -564,7 +564,7 @@ static int ak8974_read_raw(struct iio_dev *indio_dev,
 		 * We read all axes and discard all but one, for optimized
 		 * reading, use the triggered buffer.
 		 */
-		*val = le16_to_cpu(hw_values[chan->address]);
+		*val = (s16)le16_to_cpu(hw_values[chan->address]);
 
 		ret = IIO_VAL_INT;
 	}
Signature · 1 line
2.25.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Stephan Gerhold

At the moment, reading from in_magn_*_raw in sysfs tends to return large values around 65000, even though the output of ak8974 is actually limited to ±32768. This happens because the value is never converted to the signed 16-bit integer variant.

Add an explicit cast to s16 to fix this.

Cc Linus Walleij <[email protected]>
Fixes 7c94a8b2ee8c ("iio: magn: add a driver for AK8974")

It's my bug, thanks for fixing!

Yours,
Linus Walleij