Skip to content

Commit 432121a

Browse files
idlethreadEduardo Valentin
authored andcommitted
thermal: tsens: Fix negative temperature reporting
The current code will always return 0xffffffff in case of negative temperatures due to a bug in how the binary sign extension is being done. Use sign_extend32() instead. Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org> Reviewed-by: Matthias Kaehlcke <mka@chromium.org> Reviewed-by: Bjorn Andersson <bjorn.andersson@linaro.org> Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
1 parent faa590b commit 432121a

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

drivers/thermal/qcom/tsens-v2.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
*/
66

77
#include <linux/regmap.h>
8+
#include <linux/bitops.h>
89
#include "tsens.h"
910

1011
#define STATUS_OFFSET 0xa0
1112
#define LAST_TEMP_MASK 0xfff
1213
#define STATUS_VALID_BIT BIT(21)
13-
#define CODE_SIGN_BIT BIT(11)
1414

1515
static int get_temp_tsens_v2(struct tsens_device *tmdev, int id, int *temp)
1616
{
1717
struct tsens_sensor *s = &tmdev->sensor[id];
1818
u32 code;
1919
unsigned int status_reg;
20-
int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
20+
u32 last_temp = 0, last_temp2 = 0, last_temp3 = 0;
21+
int ret;
2122

2223
status_reg = tmdev->tm_offset + STATUS_OFFSET + s->hw_id * 4;
2324
ret = regmap_read(tmdev->map, status_reg, &code);
@@ -54,12 +55,8 @@ static int get_temp_tsens_v2(struct tsens_device *tmdev, int id, int *temp)
5455
else if (last_temp2 == last_temp3)
5556
last_temp = last_temp3;
5657
done:
57-
/* Code sign bit is the sign extension for a negative value */
58-
if (last_temp & CODE_SIGN_BIT)
59-
last_temp |= ~CODE_SIGN_BIT;
60-
61-
/* Temperatures are in deciCelicius */
62-
*temp = last_temp * 100;
58+
/* Convert temperature from deciCelsius to milliCelsius */
59+
*temp = sign_extend32(last_temp, fls(LAST_TEMP_MASK) - 1) * 100;
6360

6461
return 0;
6562
}

0 commit comments

Comments
 (0)