diff mbox series

[v2] hwmon: (ntc_thermistor): Use library interpolation

Message ID 20210704222014.12058-1-linus.walleij@linaro.org (mailing list archive)
State Accepted
Headers show
Series [v2] hwmon: (ntc_thermistor): Use library interpolation | expand

Commit Message

Linus Walleij July 4, 2021, 10:20 p.m. UTC
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>
---
ChangeLog v1->v2:
- Drop the check for low == high as the linear interpolation
  library function does this for us.
- Multiply the temperatures in the table with 1000 *before*
  interpolating instead of *after* so we increase precision
  in the interpolation.
---
 drivers/hwmon/ntc_thermistor.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

Comments

Guenter Roeck July 5, 2021, 5:27 p.m. UTC | #1
On Mon, Jul 05, 2021 at 12:20:14AM +0200, Linus Walleij wrote:
> 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>

Applied.

Thanks,
Guenter

> ---
> ChangeLog v1->v2:
> - Drop the check for low == high as the linear interpolation
>   library function does this for us.
> - Multiply the temperatures in the table with 1000 *before*
>   interpolating instead of *after* so we increase precision
>   in the interpolation.
> ---
>  drivers/hwmon/ntc_thermistor.c | 20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
> index 8587189c7f15..4c26db6738f9 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>
>  
> @@ -553,15 +554,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;
>  }
>
diff mbox series

Patch

diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
index 8587189c7f15..4c26db6738f9 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>
 
@@ -553,15 +554,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;
 }