Skip to content

Commit 95e7ff0

Browse files
itirdeajic23
authored andcommitted
iio: gyro: bmg160: fix endianness when reading axes
For big endian platforms, reading the axes will return invalid values. The device stores each axis value in a 16 bit little endian register. The driver uses regmap_read_bulk to get the axis value, resulting in a 16 bit little endian value. This needs to be converted to cpu endianness to work on big endian platforms. Fix endianness for big endian platforms by converting the values for the axes read from little endian to cpu. This is also partially fixed in commit 82d8e5da1a33 ("iio: accel: bmg160: optimize transfers in trigger handler"). Signed-off-by: Irina Tirdea <irina.tirdea@intel.com> Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 2215f31 commit 95e7ff0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/iio/gyro/bmg160_core.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ static int bmg160_get_temp(struct bmg160_data *data, int *val)
452452
static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
453453
{
454454
int ret;
455-
unsigned int raw_val;
455+
__le16 raw_val;
456456

457457
mutex_lock(&data->mutex);
458458
ret = bmg160_set_power_state(data, true);
@@ -462,15 +462,15 @@ static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val)
462462
}
463463

464464
ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val,
465-
2);
465+
sizeof(raw_val));
466466
if (ret < 0) {
467467
dev_err(data->dev, "Error reading axis %d\n", axis);
468468
bmg160_set_power_state(data, false);
469469
mutex_unlock(&data->mutex);
470470
return ret;
471471
}
472472

473-
*val = sign_extend32(raw_val, 15);
473+
*val = sign_extend32(le16_to_cpu(raw_val), 15);
474474
ret = bmg160_set_power_state(data, false);
475475
mutex_unlock(&data->mutex);
476476
if (ret < 0)
@@ -733,6 +733,7 @@ static const struct iio_event_spec bmg160_event = {
733733
.sign = 's', \
734734
.realbits = 16, \
735735
.storagebits = 16, \
736+
.endianness = IIO_LE, \
736737
}, \
737738
.event_spec = &bmg160_event, \
738739
.num_event_specs = 1 \

0 commit comments

Comments
 (0)