Skip to content

Commit 9d21621

Browse files
anderssonEduardo Valentin
authored andcommitted
thermal: generic-adc: Fix adc to temp interpolation
First correct the edge case to return the last element if we're outside the range, rather than at the last element, so that interpolation is not omitted for points between the two last entries in the table. Then correct the formula to perform linear interpolation based the two points surrounding the read ADC value. The indices for temp are kept as "hi" and "lo" to pair with the adc indices, but there's no requirement that the temperature is provided in descendent order. mult_frac() is used to prevent issues with overflowing the int. Cc: Laxman Dewangan <ldewangan@nvidia.com> Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org> Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
1 parent e36e130 commit 9d21621

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

drivers/thermal/thermal-generic-adc.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct gadc_thermal_info {
2626

2727
static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
2828
{
29-
int temp, adc_hi, adc_lo;
29+
int temp, temp_hi, temp_lo, adc_hi, adc_lo;
3030
int i;
3131

3232
for (i = 0; i < gti->nlookup_table; i++) {
@@ -36,13 +36,17 @@ static int gadc_thermal_adc_to_temp(struct gadc_thermal_info *gti, int val)
3636

3737
if (i == 0) {
3838
temp = gti->lookup_table[0];
39-
} else if (i >= (gti->nlookup_table - 1)) {
39+
} else if (i >= gti->nlookup_table) {
4040
temp = gti->lookup_table[2 * (gti->nlookup_table - 1)];
4141
} else {
4242
adc_hi = gti->lookup_table[2 * i - 1];
4343
adc_lo = gti->lookup_table[2 * i + 1];
44-
temp = gti->lookup_table[2 * i];
45-
temp -= ((val - adc_lo) * 1000) / (adc_hi - adc_lo);
44+
45+
temp_hi = gti->lookup_table[2 * i - 2];
46+
temp_lo = gti->lookup_table[2 * i];
47+
48+
temp = temp_hi + mult_frac(temp_lo - temp_hi, val - adc_hi,
49+
adc_lo - adc_hi);
4650
}
4751

4852
return temp;

0 commit comments

Comments
 (0)