[PATCH v3] iio: gyro: mpu3050: Fix reported temperature value

From: Dmitry Osipenko
Date: Thu Apr 22 2021 - 22:10:11 EST


The raw temperature value is a 16-bit signed integer. The sign casting
is missing in the code, which results in a wrong temperature reported
by userspace tools, fix it.

Cc: stable@xxxxxxxxxxxxxxx
Fixes: 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope")
Datasheet: https://www.cdiweb.com/datasheets/invensense/mpu-3000a.pdf
Tested-by: Maxim Schwalm <maxim.schwalm@xxxxxxxxx> # Asus TF700T
Tested-by: Svyatoslav Ryhel <clamor95@xxxxxxxxx> # Asus TF201
Reported-by: Svyatoslav Ryhel <clamor95@xxxxxxxxx>
Reviewed-by: Andy Shevchenko <Andy.Shevchenko@xxxxxxxxx>
Reviewed-by: Linus Walleij <linus.walleij@xxxxxxxxxx>
Signed-off-by: Dmitry Osipenko <digetx@xxxxxxxxx>
---

v3: - Improved comment in the code, saying that the temperature range
corresponds to the "best fit straight line" range where temperature
is reported reliably, i.e. in accordance to the linear equation.

v2: - Replaced "signed 16bit integer" wording with "16-bit signed integer",
replaced "Link" tag with the "Datasheet" and added "Fixes" tag as was
suggested by Andy Shevchenko.

- Added r-b from Andy Shevchenko and Linus Walleij.

drivers/iio/gyro/mpu3050-core.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/gyro/mpu3050-core.c b/drivers/iio/gyro/mpu3050-core.c
index ac90be03332a..f17a93519535 100644
--- a/drivers/iio/gyro/mpu3050-core.c
+++ b/drivers/iio/gyro/mpu3050-core.c
@@ -272,7 +272,16 @@ static int mpu3050_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_OFFSET:
switch (chan->type) {
case IIO_TEMP:
- /* The temperature scaling is (x+23000)/280 Celsius */
+ /*
+ * The temperature scaling is (x+23000)/280 Celsius
+ * for the "best fit straight line" temperature range
+ * of -30C..85C. The 23000 includes room temperature
+ * offset of +35C, 280 is the precision scale and x is
+ * the 16-bit signed integer reported by hardware.
+ *
+ * Temperature value itself represents temperature of
+ * the sensor die.
+ */
*val = 23000;
return IIO_VAL_INT;
default:
@@ -329,7 +338,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.30.2