Skip to content

Commit f6725ae

Browse files
ChrisLesiakgroeck
authored andcommitted
hwmon: (ntc_thermistor) Improve precision of resistance calculation
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 <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent 0315253 commit f6725ae

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

drivers/hwmon/ntc_thermistor.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -350,30 +350,27 @@ static inline u64 div64_u64_safe(u64 dividend, u64 divisor)
350350
static int get_ohm_of_thermistor(struct ntc_data *data, unsigned int uv)
351351
{
352352
struct ntc_thermistor_platform_data *pdata = data->pdata;
353-
u64 mv = uv / 1000;
354-
u64 pmv = pdata->pullup_uv / 1000;
353+
u32 puv = pdata->pullup_uv;
355354
u64 n, puo, pdo;
356355
puo = pdata->pullup_ohm;
357356
pdo = pdata->pulldown_ohm;
358357

359-
if (mv == 0) {
360-
if (pdata->connect == NTC_CONNECTED_POSITIVE)
361-
return INT_MAX;
362-
return 0;
363-
}
364-
if (mv >= pmv)
358+
if (uv == 0)
359+
return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
360+
INT_MAX : 0;
361+
if (uv >= puv)
365362
return (pdata->connect == NTC_CONNECTED_POSITIVE) ?
366363
0 : INT_MAX;
367364

368365
if (pdata->connect == NTC_CONNECTED_POSITIVE && puo == 0)
369-
n = div64_u64_safe(pdo * (pmv - mv), mv);
366+
n = div_u64(pdo * (puv - uv), uv);
370367
else if (pdata->connect == NTC_CONNECTED_GROUND && pdo == 0)
371-
n = div64_u64_safe(puo * mv, pmv - mv);
368+
n = div_u64(puo * uv, puv - uv);
372369
else if (pdata->connect == NTC_CONNECTED_POSITIVE)
373-
n = div64_u64_safe(pdo * puo * (pmv - mv),
374-
puo * mv - pdo * (pmv - mv));
370+
n = div64_u64_safe(pdo * puo * (puv - uv),
371+
puo * uv - pdo * (puv - uv));
375372
else
376-
n = div64_u64_safe(pdo * puo * mv, pdo * (pmv - mv) - puo * mv);
373+
n = div64_u64_safe(pdo * puo * uv, pdo * (puv - uv) - puo * uv);
377374

378375
if (n > INT_MAX)
379376
n = INT_MAX;

0 commit comments

Comments
 (0)