diff mbox series

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

Message ID 20200214110324.70972-1-stephan@gerhold.net (mailing list archive)
State New, archived
Headers show
Series iio: magnetometer: ak8974: Fix negative raw values in sysfs | expand

Commit Message

Stephan Gerhold Feb. 14, 2020, 11:03 a.m. UTC
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 <linus.walleij@linaro.org>
Fixes: 7c94a8b2ee8c ("iio: magn: add a driver for AK8974")
Signed-off-by: Stephan Gerhold <stephan@gerhold.net>
---
 drivers/iio/magnetometer/ak8974.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Linus Walleij Feb. 16, 2020, 9:07 p.m. UTC | #1
On Fri, Feb 14, 2020 at 12:04 PM Stephan Gerhold <stephan@gerhold.net> wrote:

> 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 <linus.walleij@linaro.org>
> Fixes: 7c94a8b2ee8c ("iio: magn: add a driver for AK8974")
> Signed-off-by: Stephan Gerhold <stephan@gerhold.net>

Reviewed-by: Linus Waleij <linus.walleij@linaro.org>

It's my bug, thanks for fixing!

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
index fc7e910f8e8b..d32996702110 100644
--- a/drivers/iio/magnetometer/ak8974.c
+++ b/drivers/iio/magnetometer/ak8974.c
@@ -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;
 	}