Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <peda@axentia.se>
Cc: Chris Lesiak <chris.lesiak@licor.com>
Cc: linux-iio@vger.kernel.org
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20210704222014.12058-1-linus.walleij@linaro.org
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Linus Walleij and committed by
Guenter Roeck
bd56c1e9 02c9dce4

+11 -9
+11 -9
drivers/hwmon/ntc_thermistor.c
··· 13 13 #include <linux/err.h> 14 14 #include <linux/of.h> 15 15 #include <linux/of_device.h> 16 + #include <linux/fixp-arith.h> 16 17 17 18 #include <linux/platform_data/ntc_thermistor.h> 18 19 ··· 550 549 int temp; 551 550 552 551 lookup_comp(data, ohm, &low, &high); 553 - if (low == high) { 554 - /* Unable to use linear approximation */ 555 - temp = data->comp[low].temp_c * 1000; 556 - } else { 557 - temp = data->comp[low].temp_c * 1000 + 558 - ((data->comp[high].temp_c - data->comp[low].temp_c) * 559 - 1000 * ((int)ohm - (int)data->comp[low].ohm)) / 560 - ((int)data->comp[high].ohm - (int)data->comp[low].ohm); 561 - } 552 + /* 553 + * First multiplying the table temperatures with 1000 to get to 554 + * millicentigrades (which is what we want) and then interpolating 555 + * will give the best precision. 556 + */ 557 + temp = fixp_linear_interpolate(data->comp[low].ohm, 558 + data->comp[low].temp_c * 1000, 559 + data->comp[high].ohm, 560 + data->comp[high].temp_c * 1000, 561 + ohm); 562 562 return temp; 563 563 } 564 564