Skip to content

Commit a53f945

Browse files
NXP-CarlosSongjic23
authored andcommitted
iio: imu: fxos8700: fix IMU data bits returned to user space
ACCEL output data registers contain the X-axis, Y-axis, and Z-axis 14-bit left-justified sample data and MAGN output data registers contain the X-axis, Y-axis, and Z-axis 16-bit sample data. The ACCEL raw register output data should be divided by 4 before sent to userspace. Apply a 2 bits signed right shift to the raw data from ACCEL output data register but keep that from MAGN sensor as the origin. Fixes: 84e5ddd ("iio: imu: Add support for the FXOS8700 IMU") Signed-off-by: Carlos Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Cc: <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 37a94d8 commit a53f945

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

drivers/iio/imu/fxos8700_core.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ static int fxos8700_get_data(struct fxos8700_data *data, int chan_type,
394394
int axis, int *val)
395395
{
396396
u8 base, reg;
397+
s16 tmp;
397398
int ret;
398399

399400
/*
@@ -421,8 +422,33 @@ static int fxos8700_get_data(struct fxos8700_data *data, int chan_type,
421422
/* Convert axis to buffer index */
422423
reg = axis - IIO_MOD_X;
423424

425+
/*
426+
* Convert to native endianness. The accel data and magn data
427+
* are signed, so a forced type conversion is needed.
428+
*/
429+
tmp = be16_to_cpu(data->buf[reg]);
430+
431+
/*
432+
* ACCEL output data registers contain the X-axis, Y-axis, and Z-axis
433+
* 14-bit left-justified sample data and MAGN output data registers
434+
* contain the X-axis, Y-axis, and Z-axis 16-bit sample data. Apply
435+
* a signed 2 bits right shift to the readback raw data from ACCEL
436+
* output data register and keep that from MAGN sensor as the origin.
437+
* Value should be extended to 32 bit.
438+
*/
439+
switch (chan_type) {
440+
case IIO_ACCEL:
441+
tmp = tmp >> 2;
442+
break;
443+
case IIO_MAGN:
444+
/* Nothing to do */
445+
break;
446+
default:
447+
return -EINVAL;
448+
}
449+
424450
/* Convert to native endianness */
425-
*val = sign_extend32(be16_to_cpu(data->buf[reg]), 15);
451+
*val = sign_extend32(tmp, 15);
426452

427453
return 0;
428454
}

0 commit comments

Comments
 (0)