From: Linus Walleij Date: Sun, 4 Jul 2021 22:20:14 +0000 (+0200) Subject: hwmon: (ntc_thermistor) Use library interpolation X-Git-Tag: v5.15~434^2~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bd56c1e9603a4c645fcafdef8df9b2b04125d406;p=platform%2Fkernel%2Flinux-starfive.git hwmon: (ntc_thermistor) Use library interpolation The kernel has a helper function for linear interpolation so use it. It incidentally makes the code easier to read as well. Tested on the ST-Ericsson HREFv60plus hardware reference design with two thermistors forming a thermal zone. Cc: Peter Rosin Cc: Chris Lesiak Cc: linux-iio@vger.kernel.org Signed-off-by: Linus Walleij Link: https://lore.kernel.org/r/20210704222014.12058-1-linus.walleij@linaro.org Signed-off-by: Guenter Roeck --- diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c index 18fd6f12ca16..cf26c44f2b88 100644 --- a/drivers/hwmon/ntc_thermistor.c +++ b/drivers/hwmon/ntc_thermistor.c @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -549,15 +550,16 @@ static int get_temp_mc(struct ntc_data *data, unsigned int ohm) int temp; lookup_comp(data, ohm, &low, &high); - if (low == high) { - /* Unable to use linear approximation */ - temp = data->comp[low].temp_c * 1000; - } else { - temp = data->comp[low].temp_c * 1000 + - ((data->comp[high].temp_c - data->comp[low].temp_c) * - 1000 * ((int)ohm - (int)data->comp[low].ohm)) / - ((int)data->comp[high].ohm - (int)data->comp[low].ohm); - } + /* + * First multiplying the table temperatures with 1000 to get to + * millicentigrades (which is what we want) and then interpolating + * will give the best precision. + */ + temp = fixp_linear_interpolate(data->comp[low].ohm, + data->comp[low].temp_c * 1000, + data->comp[high].ohm, + data->comp[high].temp_c * 1000, + ohm); return temp; }