Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: gyro: mpu3050: fix sign of raw angular velocity readings
@ 2026-08-02  6:55 Cong Nguyen
  2026-08-02 15:54 ` David Lechner
  2026-08-02 15:55 ` Joshua Crofts
  0 siblings, 2 replies; 3+ messages in thread
From: Cong Nguyen @ 2026-08-02  6:55 UTC (permalink / raw)
  To: Linus Walleij, Jonathan Cameron
  Cc: David Lechner, Nuno Sá, Andy Shevchenko, linux-iio,
	linux-kernel

The MPU-3050 gyroscope output registers hold 16-bit two's complement
values; the angular velocity channels are declared with .sign = 's'.
When mpu3050_read_raw() handles IIO_CHAN_INFO_RAW it reads the register
via a big-endian regmap_bulk_read() and assigns it with:

	*val = be16_to_cpu(raw_val);

be16_to_cpu() yields an unsigned 16-bit quantity, so negative rates
(bit 15 set) are reported to userspace as large positive integers
(e.g. -1 becomes 65535) instead of the correct negative value.

Cast to s16 before the assignment, matching the temperature channel a
few lines above which already handles the sign correctly.

Fixes: 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope")
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-4
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
 drivers/iio/gyro/mpu3050-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/gyro/mpu3050-core.c b/drivers/iio/gyro/mpu3050-core.c
index d84e04e4b431..07c0f9011c63 100644
--- a/drivers/iio/gyro/mpu3050-core.c
+++ b/drivers/iio/gyro/mpu3050-core.c
@@ -356,7 +356,7 @@ static int mpu3050_read_raw(struct iio_dev *indio_dev,
 				goto out_read_raw_unlock;
 			}
 
-			*val = be16_to_cpu(raw_val);
+			*val = (s16)be16_to_cpu(raw_val);
 			ret = IIO_VAL_INT;
 
 			goto out_read_raw_unlock;
-- 
2.25.1


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

* Re: [PATCH] iio: gyro: mpu3050: fix sign of raw angular velocity readings
  2026-08-02  6:55 [PATCH] iio: gyro: mpu3050: fix sign of raw angular velocity readings Cong Nguyen
@ 2026-08-02 15:54 ` David Lechner
  2026-08-02 15:55 ` Joshua Crofts
  1 sibling, 0 replies; 3+ messages in thread
From: David Lechner @ 2026-08-02 15:54 UTC (permalink / raw)
  To: Cong Nguyen, Linus Walleij, Jonathan Cameron
  Cc: Nuno Sá, Andy Shevchenko, linux-iio, linux-kernel

On 8/2/26 1:55 AM, Cong Nguyen wrote:
> The MPU-3050 gyroscope output registers hold 16-bit two's complement
> values; the angular velocity channels are declared with .sign = 's'.
> When mpu3050_read_raw() handles IIO_CHAN_INFO_RAW it reads the register
> via a big-endian regmap_bulk_read() and assigns it with:
> 
> 	*val = be16_to_cpu(raw_val);
> 
> be16_to_cpu() yields an unsigned 16-bit quantity, so negative rates
> (bit 15 set) are reported to userspace as large positive integers
> (e.g. -1 becomes 65535) instead of the correct negative value.
> 
> Cast to s16 before the assignment, matching the temperature channel a
> few lines above which already handles the sign correctly.
> 
> Fixes: 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope")
> Cc: stable@vger.kernel.org
> Assisted-by: Claude:claude-opus-4
> Signed-off-by: Cong Nguyen <congnt264@gmail.com>
> ---
>  drivers/iio/gyro/mpu3050-core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/gyro/mpu3050-core.c b/drivers/iio/gyro/mpu3050-core.c
> index d84e04e4b431..07c0f9011c63 100644
> --- a/drivers/iio/gyro/mpu3050-core.c
> +++ b/drivers/iio/gyro/mpu3050-core.c
> @@ -356,7 +356,7 @@ static int mpu3050_read_raw(struct iio_dev *indio_dev,
>  				goto out_read_raw_unlock;
>  			}
>  
> -			*val = be16_to_cpu(raw_val);
> +			*val = (s16)be16_to_cpu(raw_val);
>  			ret = IIO_VAL_INT;
>  
>  			goto out_read_raw_unlock;

Usually, we use sign_extend32() instead of a cast for this. But this
driver already has a similar cast for the temperature channel. So to
keep it consistent, this looks fine.

Reviewed-by: David Lechner <dlechner@baylibre.com>



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

* Re: [PATCH] iio: gyro: mpu3050: fix sign of raw angular velocity readings
  2026-08-02  6:55 [PATCH] iio: gyro: mpu3050: fix sign of raw angular velocity readings Cong Nguyen
  2026-08-02 15:54 ` David Lechner
@ 2026-08-02 15:55 ` Joshua Crofts
  1 sibling, 0 replies; 3+ messages in thread
From: Joshua Crofts @ 2026-08-02 15:55 UTC (permalink / raw)
  To: Cong Nguyen
  Cc: Linus Walleij, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel

On Sun,  2 Aug 2026 13:55:40 +0700
Cong Nguyen <congnt264@gmail.com> wrote:

> The MPU-3050 gyroscope output registers hold 16-bit two's complement
> values; the angular velocity channels are declared with .sign = 's'.
> When mpu3050_read_raw() handles IIO_CHAN_INFO_RAW it reads the register
> via a big-endian regmap_bulk_read() and assigns it with:
> 
> 	*val = be16_to_cpu(raw_val);
> 
> be16_to_cpu() yields an unsigned 16-bit quantity, so negative rates
> (bit 15 set) are reported to userspace as large positive integers
> (e.g. -1 becomes 65535) instead of the correct negative value.
> 
> Cast to s16 before the assignment, matching the temperature channel a
> few lines above which already handles the sign correctly.
> 

LGTM.

Reviewed-by: Joshua Crofts <joshua.crofts1@gmail.com>

-- 
Kind regards,
Joshua Crofts

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

end of thread, other threads:[~2026-08-02 15:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-02  6:55 [PATCH] iio: gyro: mpu3050: fix sign of raw angular velocity readings Cong Nguyen
2026-08-02 15:54 ` David Lechner
2026-08-02 15:55 ` Joshua Crofts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox