All of lore.kernel.org
 help / color / mirror / Atom feed
* [lm-sensors] [PATCH] hwmon: ntc: improve precision of resistance calculation
@ 2015-06-02 18:48 Chris Lesiak
  2015-06-02 20:19 ` Guenter Roeck
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Chris Lesiak @ 2015-06-02 18:48 UTC (permalink / raw)
  To: lm-sensors

The function get_ohm_of_thermistor has both the measured voltage and the
pullup voltage available in microvolts.  But it was promptly converting
both to millivolts before using them to calculate the thermistor
resistance.  That conversion unnecessarily hurt the precision of the
calculation.

For example, take the ncpXXwb473 connected to 5000 mV and pulled down
through a 47000 ohm resistor.  At 25 C, the resistance of the thermistor
is 47000 ohms.  The measured voltage will be 2500 mV.  If we measure
instead 2501 mV, then the calculated resistance will be 46962 ohms --
a difference of 38 ohms.  So the precision of the resistance estimate
could be increased by 38X by doing the calculations in microvolts.

Signed-off-by: Chris Lesiak <chris.lesiak@licor.com>
---
 drivers/hwmon/ntc_thermistor.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
index e08ed47..e112814 100644
--- a/drivers/hwmon/ntc_thermistor.c
+++ b/drivers/hwmon/ntc_thermistor.c
@@ -310,30 +310,27 @@ static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
 {
 	struct ntc_thermistor_platform_data *pdata = data->pdata;
-	u64 mv = uv / 1000;
-	u64 pmv = pdata->pullup_uv / 1000;
+	u32 puv = pdata->pullup_uv;
 	u64 n, puo, pdo;
 	puo = pdata->pullup_ohm;
 	pdo = pdata->pulldown_ohm;
 
-	if (mv = 0) {
-		if (pdata->connect = NTC_CONNECTED_POSITIVE)
-			return INT_MAX;
-		return 0;
-	}
-	if (mv >= pmv)
+	if (uv <= 0)
+		return (pdata->connect = NTC_CONNECTED_POSITIVE) ?
+			INT_MAX : 0;
+	if (uv >= puv)
 		return (pdata->connect = NTC_CONNECTED_POSITIVE) ?
 			0 : INT_MAX;
 
 	if (pdata->connect = NTC_CONNECTED_POSITIVE && puo = 0)
-		n = div64_u64_safe(pdo * (pmv - mv), mv);
+		n = div_u64(pdo * (puv - uv), uv);
 	else if (pdata->connect = NTC_CONNECTED_GROUND && pdo = 0)
-		n = div64_u64_safe(puo * mv, pmv - mv);
+		n = div_u64(puo * uv, puv - uv);
 	else if (pdata->connect = NTC_CONNECTED_POSITIVE)
-		n = div64_u64_safe(pdo * puo * (pmv - mv),
-				puo * mv - pdo * (pmv - mv));
+		n = div64_u64_safe(pdo * puo * (puv - uv),
+				puo * uv - pdo * (puv - uv));
 	else
-		n = div64_u64_safe(pdo * puo * mv, pdo * (pmv - mv) - puo * mv);
+		n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
 
 	if (n > INT_MAX)
 		n = INT_MAX;
-- 
1.9.3


_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

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

* Re: [lm-sensors] [PATCH] hwmon: ntc: improve precision of resistance calculation
  2015-06-02 18:48 [lm-sensors] [PATCH] hwmon: ntc: improve precision of resistance calculation Chris Lesiak
@ 2015-06-02 20:19 ` Guenter Roeck
  2015-06-02 20:57 ` [lm-sensors] [PATCH] hwmon: ntc: Improve " Chris Lesiak
  2015-06-03 22:27 ` Guenter Roeck
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2015-06-02 20:19 UTC (permalink / raw)
  To: lm-sensors

On Tue, Jun 02, 2015 at 01:48:27PM -0500, Chris Lesiak wrote:
> The function get_ohm_of_thermistor has both the measured voltage and the
> pullup voltage available in microvolts.  But it was promptly converting
> both to millivolts before using them to calculate the thermistor
> resistance.  That conversion unnecessarily hurt the precision of the
> calculation.
> 
> For example, take the ncpXXwb473 connected to 5000 mV and pulled down
> through a 47000 ohm resistor.  At 25 C, the resistance of the thermistor
> is 47000 ohms.  The measured voltage will be 2500 mV.  If we measure
> instead 2501 mV, then the calculated resistance will be 46962 ohms --
> a difference of 38 ohms.  So the precision of the resistance estimate
> could be increased by 38X by doing the calculations in microvolts.
> 
> Signed-off-by: Chris Lesiak <chris.lesiak@licor.com>
> ---
>  drivers/hwmon/ntc_thermistor.c | 23 ++++++++++-------------
>  1 file changed, 10 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
> index e08ed47..e112814 100644
> --- a/drivers/hwmon/ntc_thermistor.c
> +++ b/drivers/hwmon/ntc_thermistor.c
> @@ -310,30 +310,27 @@ static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
>  static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
>  {
>  	struct ntc_thermistor_platform_data *pdata = data->pdata;
> -	u64 mv = uv / 1000;
> -	u64 pmv = pdata->pullup_uv / 1000;
> +	u32 puv = pdata->pullup_uv;
>  	u64 n, puo, pdo;
>  	puo = pdata->pullup_ohm;
>  	pdo = pdata->pulldown_ohm;
>  
> -	if (mv = 0) {
> -		if (pdata->connect = NTC_CONNECTED_POSITIVE)
> -			return INT_MAX;
> -		return 0;
> -	}
> -	if (mv >= pmv)
> +	if (uv <= 0)

Hi Chris,

nitpick, but like mv, uv is unsigned and can not be < 0.

Thanks,
Guenter

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

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

* [lm-sensors] [PATCH] hwmon: ntc: Improve precision of resistance calculation
  2015-06-02 18:48 [lm-sensors] [PATCH] hwmon: ntc: improve precision of resistance calculation Chris Lesiak
  2015-06-02 20:19 ` Guenter Roeck
@ 2015-06-02 20:57 ` Chris Lesiak
  2015-06-03 22:27 ` Guenter Roeck
  2 siblings, 0 replies; 4+ messages in thread
From: Chris Lesiak @ 2015-06-02 20:57 UTC (permalink / raw)
  To: lm-sensors

The function get_ohm_of_thermistor has both the measured voltage and the
pullup voltage available in microvolts.  But it was promptly converting
both to millivolts before using them to calculate the thermistor
resistance.  That conversion unnecessarily hurt the precision of the
calculation.

For example, take the ncpXXwb473 connected to 5000 mV and pulled down
through a 47000 ohm resistor.  At 25 C, the resistance of the thermistor
is 47000 ohms.  The measured voltage will be 2500 mV.  If we measure
instead 2501 mV, then the calculated resistance will be 46962 ohms --
a difference of 38 ohms.  So the precision of the resistance estimate
could be increased by 38X by doing the calculations in microvolts.

Signed-off-by: Chris Lesiak <chris.lesiak@licor.com>
---

Changes in v2
======Change comparison "uv <= 0" to "uv = 0" because uv is unsigned.

Guenter, thanks for the review.

 drivers/hwmon/ntc_thermistor.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
index e08ed47..25a310f 100644
--- a/drivers/hwmon/ntc_thermistor.c
+++ b/drivers/hwmon/ntc_thermistor.c
@@ -310,30 +310,27 @@ static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
 static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
 {
 	struct ntc_thermistor_platform_data *pdata = data->pdata;
-	u64 mv = uv / 1000;
-	u64 pmv = pdata->pullup_uv / 1000;
+	u32 puv = pdata->pullup_uv;
 	u64 n, puo, pdo;
 	puo = pdata->pullup_ohm;
 	pdo = pdata->pulldown_ohm;
 
-	if (mv = 0) {
-		if (pdata->connect = NTC_CONNECTED_POSITIVE)
-			return INT_MAX;
-		return 0;
-	}
-	if (mv >= pmv)
+	if (uv = 0)
+		return (pdata->connect = NTC_CONNECTED_POSITIVE) ?
+			INT_MAX : 0;
+	if (uv >= puv)
 		return (pdata->connect = NTC_CONNECTED_POSITIVE) ?
 			0 : INT_MAX;
 
 	if (pdata->connect = NTC_CONNECTED_POSITIVE && puo = 0)
-		n = div64_u64_safe(pdo * (pmv - mv), mv);
+		n = div_u64(pdo * (puv - uv), uv);
 	else if (pdata->connect = NTC_CONNECTED_GROUND && pdo = 0)
-		n = div64_u64_safe(puo * mv, pmv - mv);
+		n = div_u64(puo * uv, puv - uv);
 	else if (pdata->connect = NTC_CONNECTED_POSITIVE)
-		n = div64_u64_safe(pdo * puo * (pmv - mv),
-				puo * mv - pdo * (pmv - mv));
+		n = div64_u64_safe(pdo * puo * (puv - uv),
+				puo * uv - pdo * (puv - uv));
 	else
-		n = div64_u64_safe(pdo * puo * mv, pdo * (pmv - mv) - puo * mv);
+		n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
 
 	if (n > INT_MAX)
 		n = INT_MAX;
-- 
1.9.3


_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

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

* Re: [lm-sensors] [PATCH] hwmon: ntc: Improve precision of resistance calculation
  2015-06-02 18:48 [lm-sensors] [PATCH] hwmon: ntc: improve precision of resistance calculation Chris Lesiak
  2015-06-02 20:19 ` Guenter Roeck
  2015-06-02 20:57 ` [lm-sensors] [PATCH] hwmon: ntc: Improve " Chris Lesiak
@ 2015-06-03 22:27 ` Guenter Roeck
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2015-06-03 22:27 UTC (permalink / raw)
  To: lm-sensors

On 06/02/2015 01:57 PM, Chris Lesiak wrote:
> The function get_ohm_of_thermistor has both the measured voltage and the
> pullup voltage available in microvolts.  But it was promptly converting
> both to millivolts before using them to calculate the thermistor
> resistance.  That conversion unnecessarily hurt the precision of the
> calculation.
>
> For example, take the ncpXXwb473 connected to 5000 mV and pulled down
> through a 47000 ohm resistor.  At 25 C, the resistance of the thermistor
> is 47000 ohms.  The measured voltage will be 2500 mV.  If we measure
> instead 2501 mV, then the calculated resistance will be 46962 ohms --
> a difference of 38 ohms.  So the precision of the resistance estimate
> could be increased by 38X by doing the calculations in microvolts.
>
> Signed-off-by: Chris Lesiak <chris.lesiak@licor.com>

Applied to -next.

Thanks,
Guenter


_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

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

end of thread, other threads:[~2015-06-03 22:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-02 18:48 [lm-sensors] [PATCH] hwmon: ntc: improve precision of resistance calculation Chris Lesiak
2015-06-02 20:19 ` Guenter Roeck
2015-06-02 20:57 ` [lm-sensors] [PATCH] hwmon: ntc: Improve " Chris Lesiak
2015-06-03 22:27 ` 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.