All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hwmon: (ntc_thermistor): Use library interpolation
@ 2021-07-03 18:01 Linus Walleij
  2021-07-04 16:14 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Linus Walleij @ 2021-07-03 18:01 UTC (permalink / raw)
  To: Jean Delvare, Guenter Roeck
  Cc: linux-hwmon, Linus Walleij, Peter Rosin, Chris Lesiak, linux-iio

The kernel has a helper function for linear interpolation so
use it. It incidentally makes the code easier to read as well.

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>
---
 drivers/hwmon/ntc_thermistor.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
index 8587189c7f15..61bd0e074ec9 100644
--- a/drivers/hwmon/ntc_thermistor.c
+++ b/drivers/hwmon/ntc_thermistor.c
@@ -14,6 +14,7 @@
 #include <linux/err.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/fixp-arith.h>
 
 #include <linux/platform_data/ntc_thermistor.h>
 
@@ -557,10 +558,12 @@ static int get_temp_mc(struct ntc_data *data, unsigned int ohm)
 		/* 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);
+		temp = fixp_linear_interpolate(data->comp[low].ohm,
+					       data->comp[low].temp_c,
+					       data->comp[high].ohm,
+					       data->comp[high].temp_c,
+					       ohm);
+		temp *= 1000;
 	}
 	return temp;
 }
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-04 17:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-03 18:01 [PATCH] hwmon: (ntc_thermistor): Use library interpolation Linus Walleij
2021-07-04 16:14 ` Jonathan Cameron
2021-07-04 17:50   ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.