Skip to content

Commit 0315253

Browse files
ChrisLesiakgroeck
authored andcommitted
hwmon: (ntc_thermistor) fix iio raw to microvolts conversion
The function ntc_adc_iio_read was assuming both a 12 bit ADC and that pullup_uv is the same as the ADC reference voltage. If either assumption is false, then the result is incorrect. Attempt to use iio_convert_raw_to_processed to convert the raw value to microvolts. It will fail for iio channels that don't support support IIO_CHAN_INFO_SCALE; in that case fall back to the assumptions. Signed-off-by: Chris Lesiak <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent e892b75 commit 0315253

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

drivers/hwmon/ntc_thermistor.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,21 @@ struct ntc_data {
228228
static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
229229
{
230230
struct iio_channel *channel = pdata->chan;
231-
s64 result;
232-
int val, ret;
231+
int raw, uv, ret;
233232

234-
ret = iio_read_channel_raw(channel, &val);
233+
ret = iio_read_channel_raw(channel, &raw);
235234
if (ret < 0) {
236235
pr_err("read channel() error: %d\n", ret);
237236
return ret;
238237
}
239238

240-
/* unit: mV */
241-
result = pdata->pullup_uv * (s64) val;
242-
result >>= 12;
239+
ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
240+
if (ret < 0) {
241+
/* Assume 12 bit ADC with vref at pullup_uv */
242+
uv = (pdata->pullup_uv * (s64)raw) >> 12;
243+
}
243244

244-
return (int)result;
245+
return uv;
245246
}
246247

247248
static const struct of_device_id ntc_match[] = {

0 commit comments

Comments
 (0)