]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.14/thermal-generic-adc-fix-adc-to-temp-interpolation.patch
autosel patches for 4.14
[thirdparty/kernel/stable-queue.git] / queue-4.14 / thermal-generic-adc-fix-adc-to-temp-interpolation.patch
1 From 480d3771eb4a34ad8f4240aa80d2a6b1711fa53c Mon Sep 17 00:00:00 2001
2 From: Bjorn Andersson <bjorn.andersson@linaro.org>
3 Date: Sun, 23 Dec 2018 23:26:44 -0800
4 Subject: thermal: generic-adc: Fix adc to temp interpolation
5
6 [ Upstream commit 9d216211fded20fff301d0317af3238d8383634c ]
7
8 First correct the edge case to return the last element if we're
9 outside the range, rather than at the last element, so that
10 interpolation is not omitted for points between the two last entries in
11 the table.
12
13 Then correct the formula to perform linear interpolation based the two
14 points surrounding the read ADC value. The indices for temp are kept as
15 "hi" and "lo" to pair with the adc indices, but there's no requirement
16 that the temperature is provided in descendent order. mult_frac() is
17 used to prevent issues with overflowing the int.
18
19 Cc: Laxman Dewangan <ldewangan@nvidia.com>
20 Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
21 Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
22 Signed-off-by: Sasha Levin <sashal@kernel.org>
23 ---
24 drivers/thermal/thermal-generic-adc.c | 12 ++++++++----
25 1 file changed, 8 insertions(+), 4 deletions(-)
26
27 diff --git a/drivers/thermal/thermal-generic-adc.c b/drivers/thermal/thermal-generic-adc.c
28 index 73f55d6a1721..ad601e5b4175 100644
29 --- a/drivers/thermal/thermal-generic-adc.c
30 +++ b/drivers/thermal/thermal-generic-adc.c
31 @@ -26,7 +26,7 @@ struct gadc_thermal_info {
32
33 static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
34 {
35 - int temp, adc_hi, adc_lo;
36 + int temp, temp_hi, temp_lo, adc_hi, adc_lo;
37 int i;
38
39 for (i = 0; i < gti->nlookup_table; i++) {
40 @@ -36,13 +36,17 @@ static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
41
42 if (i == 0) {
43 temp = gti->lookup_table[0];
44 - } else if (i >= (gti->nlookup_table - 1)) {
45 + } else if (i >= gti->nlookup_table) {
46 temp = gti->lookup_table[2 * (gti->nlookup_table - 1)];
47 } else {
48 adc_hi = gti->lookup_table[2 * i - 1];
49 adc_lo = gti->lookup_table[2 * i + 1];
50 - temp = gti->lookup_table[2 * i];
51 - temp -= ((val - adc_lo) * 1000) / (adc_hi - adc_lo);
52 +
53 + temp_hi = gti->lookup_table[2 * i - 2];
54 + temp_lo = gti->lookup_table[2 * i];
55 +
56 + temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi,
57 + adc_lo - adc_hi);
58 }
59
60 return temp;
61 --
62 2.19.1
63