Skip to contentSkip to conversation

iio: gyro: mpu3050: fix sign of raw angular velocity readings

PATCH Applied Cong Nguyen 5 participants 5 messages Aug 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: gyro: mpu3050: fix sign of raw angular velocity readings

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")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/gyro/mpu3050-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/gyro/mpu3050-core.c +1 -1
index d84e04e4b431..07c0f9011c63 100644
@@ -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;
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Cong Nguyen

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")
Assisted-by Claude:claude-opus-4
---
 drivers/iio/gyro/mpu3050-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/gyro/mpu3050-core.c +0 -0
index d84e04e4b431..07c0f9011c63 100644

drivers/iio/gyro/mpu3050-core.c static int mpu3050_read_raw(struct iio_dev *indio_dev, · line 362

Quoted 8 lines
drivers/iio/gyro/mpu3050-core.c +1 -1
@@ -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 on v1 1/1
#2 lore raw
In reply to Cong Nguyen
Quoted 5 lines · On Sun, 2 Aug 2026 13:55:40 +0700 Cong Nguyen <[email protected]> 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:

Quoted 9 lines
	*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.

Signature · 2 lines
Kind regards,
Joshua Crofts
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Cong Nguyen
Quoted 11 lines · Cong Nguyen

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.

Quoted 7 lines

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")
Assisted-by Claude:claude-opus-4

Yours,
Linus Walleij

maintainer Applied
#4 lore raw
Quoted 16 lines · On Sun, 2 Aug 2026 23:30:31 +0200 Linus Walleij <[email protected]> wrote:
Quoted 14 lines · Cong Nguyen

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.

Quoted 6 lines
Quoted 4 lines
Fixes 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope")
Assisted-by Claude:claude-opus-4

Applied to the fixes-togreg branch of iio.git

Note this will probably not go upstream until the merge window.

Thanks,

Jonathan

Quoted 3 lines

Yours,
Linus Walleij