linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/1] iio: accel: bmc150: fix endianness when reading axes
@ 2016-03-29 12:35 Irina Tirdea
  2016-04-03 10:17 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Irina Tirdea @ 2016-03-29 12:35 UTC (permalink / raw)
  To: Jonathan Cameron, linux-iio
  Cc: linux-kernel, Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald,
	Irina Tirdea

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 b6fb9b6d6552 ("iio:
accel: bmc150: optimize transfers in trigger handler").

Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
---

Changes from v1:
 - pass sizeof(raw_val) to regmap_bulk_read instead of
hardcoded value

 drivers/iio/accel/bmc150-accel-core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
index c73331f7..2072a31 100644
--- a/drivers/iio/accel/bmc150-accel-core.c
+++ b/drivers/iio/accel/bmc150-accel-core.c
@@ -547,7 +547,7 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 {
 	int ret;
 	int axis = chan->scan_index;
-	unsigned int raw_val;
+	__le16 raw_val;
 
 	mutex_lock(&data->mutex);
 	ret = bmc150_accel_set_power_state(data, true);
@@ -557,14 +557,14 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
 	}
 
 	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
-			       &raw_val, 2);
+			       &raw_val, sizeof(raw_val));
 	if (ret < 0) {
 		dev_err(data->dev, "Error reading axis %d\n", axis);
 		bmc150_accel_set_power_state(data, false);
 		mutex_unlock(&data->mutex);
 		return ret;
 	}
-	*val = sign_extend32(raw_val >> chan->scan_type.shift,
+	*val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
 			     chan->scan_type.realbits - 1);
 	ret = bmc150_accel_set_power_state(data, false);
 	mutex_unlock(&data->mutex);
@@ -988,6 +988,7 @@ static const struct iio_event_spec bmc150_accel_event = {
 		.realbits = (bits),					\
 		.storagebits = 16,					\
 		.shift = 16 - (bits),					\
+		.endianness = IIO_LE,					\
 	},								\
 	.event_spec = &bmc150_accel_event,				\
 	.num_event_specs = 1						\
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2 1/1] iio: accel: bmc150: fix endianness when reading axes
  2016-03-29 12:35 [PATCH v2 1/1] iio: accel: bmc150: fix endianness when reading axes Irina Tirdea
@ 2016-04-03 10:17 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2016-04-03 10:17 UTC (permalink / raw)
  To: Irina Tirdea, linux-iio
  Cc: linux-kernel, Hartmut Knaack, Lars-Peter Clausen, Peter Meerwald

On 29/03/16 13:35, Irina Tirdea wrote:
> 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 b6fb9b6d6552 ("iio:
> accel: bmc150: optimize transfers in trigger handler").
> 
> Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Applied to the fixes-togreg-post-rc1 branch of iio.git and marked for
stable.
> ---
> 
> Changes from v1:
>  - pass sizeof(raw_val) to regmap_bulk_read instead of
> hardcoded value
> 
>  drivers/iio/accel/bmc150-accel-core.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
> index c73331f7..2072a31 100644
> --- a/drivers/iio/accel/bmc150-accel-core.c
> +++ b/drivers/iio/accel/bmc150-accel-core.c
> @@ -547,7 +547,7 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
>  {
>  	int ret;
>  	int axis = chan->scan_index;
> -	unsigned int raw_val;
> +	__le16 raw_val;
>  
>  	mutex_lock(&data->mutex);
>  	ret = bmc150_accel_set_power_state(data, true);
> @@ -557,14 +557,14 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data,
>  	}
>  
>  	ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis),
> -			       &raw_val, 2);
> +			       &raw_val, sizeof(raw_val));
>  	if (ret < 0) {
>  		dev_err(data->dev, "Error reading axis %d\n", axis);
>  		bmc150_accel_set_power_state(data, false);
>  		mutex_unlock(&data->mutex);
>  		return ret;
>  	}
> -	*val = sign_extend32(raw_val >> chan->scan_type.shift,
> +	*val = sign_extend32(le16_to_cpu(raw_val) >> chan->scan_type.shift,
>  			     chan->scan_type.realbits - 1);
>  	ret = bmc150_accel_set_power_state(data, false);
>  	mutex_unlock(&data->mutex);
> @@ -988,6 +988,7 @@ static const struct iio_event_spec bmc150_accel_event = {
>  		.realbits = (bits),					\
>  		.storagebits = 16,					\
>  		.shift = 16 - (bits),					\
> +		.endianness = IIO_LE,					\
>  	},								\
>  	.event_spec = &bmc150_accel_event,				\
>  	.num_event_specs = 1						\
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-04-03 10:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-29 12:35 [PATCH v2 1/1] iio: accel: bmc150: fix endianness when reading axes Irina Tirdea
2016-04-03 10:17 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).