iio: pressure: bmp280: Fix BMP580 temperature reading
authorAdam Rizkalla <ajarizzo@gmail.com>
Thu, 25 Apr 2024 06:22:49 +0000 (01:22 -0500)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 27 May 2024 08:49:20 +0000 (09:49 +0100)
Fix overflow issue when storing BMP580 temperature reading and
properly preserve sign of 24-bit data.

Signed-off-by: Adam Rizkalla <ajarizzo@gmail.com>
Tested-By: Vasileios Amoiridis <vassilisamir@gmail.com>
Acked-by: Angel Iglesias <ang.iglesiasg@gmail.com>
Link: https://lore.kernel.org/r/Zin2udkXRD0+GrML@adam-asahi.lan
Cc: <Stable@vger.kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/pressure/bmp280-core.c

index 09f53d987c7df5b9b551f4568a418efaf0c4c5b5..221fa2c552ae2cdc7eee3f69e802ee59748bff36 100644 (file)
@@ -1394,12 +1394,12 @@ static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2)
 
        /*
         * Temperature is returned in Celsius degrees in fractional
-        * form down 2^16. We rescale by x1000 to return milli Celsius
-        * to respect IIO ABI.
+        * form down 2^16. We rescale by x1000 to return millidegrees
+        * Celsius to respect IIO ABI.
         */
-       *val = raw_temp * 1000;
-       *val2 = 16;
-       return IIO_VAL_FRACTIONAL_LOG2;
+       raw_temp = sign_extend32(raw_temp, 23);
+       *val = ((s64)raw_temp * 1000) / (1 << 16);
+       return IIO_VAL_INT;
 }
 
 static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2)