Skip to content

Commit 1e15e6b

Browse files
M-VaittinenWen Zhiwei
authored andcommitted
iio: accel: kx022a: Fix raw read format
stable inclusion from stable-v6.6.64 commit 8e5e63d6ca95d234b70d5f3980f0efab1023af6e category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/IBL4B6 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=8e5e63d6ca95d234b70d5f3980f0efab1023af6e -------------------------------- commit b7d2bc99b3bdc03fff9b416dd830632346d83530 upstream. The KX022A provides the accelerometer data in two subsequent registers. The registers are laid out so that the value obtained via bulk-read of these registers can be interpreted as signed 16-bit little endian value. The read value is converted to cpu_endianes and stored into 32bit integer. The le16_to_cpu() casts value to unsigned 16-bit value, and when this is assigned to 32-bit integer the resulting value will always be positive. This has not been a problem to users (at least not all users) of the sysfs interface, who know the data format based on the scan info and who have converted the read value back to 16-bit signed value. This isn't compliant with the ABI however. This, however, will be a problem for those who use the in-kernel interfaces, especially the iio_read_channel_processed_scale(). The iio_read_channel_processed_scale() performs multiplications to the returned (always positive) raw value, which will cause strange results when the data from the sensor has been negative. Fix the read_raw format by casting the result of the le_to_cpu() to signed 16-bit value before assigning it to the integer. This will make the negative readings to be correctly reported as negative. This fix will be visible to users by changing values returned via sysfs to appear in correct (negative) format. Reported-by: Kalle Niemi <[email protected]> Fixes: 7c1d167 ("iio: accel: Support Kionix/ROHM KX022A accelerometer") Signed-off-by: Matti Vaittinen <[email protected]> Tested-by: Kalle Niemi <[email protected]> Cc: <[email protected]> Link: https://patch.msgid.link/ZyIxm_zamZfIGrnB@mva-rohm Signed-off-by: Jonathan Cameron <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Wen Zhiwei <[email protected]>
1 parent 88f76b0 commit 1e15e6b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

drivers/iio/accel/kionix-kx022a.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static int kx022a_get_axis(struct kx022a_data *data,
475475
if (ret)
476476
return ret;
477477

478-
*val = le16_to_cpu(data->buffer[0]);
478+
*val = (s16)le16_to_cpu(data->buffer[0]);
479479

480480
return IIO_VAL_INT;
481481
}

0 commit comments

Comments
 (0)