All of lore.kernel.org
 help / color / mirror / Atom feed
* [lm-sensors] [PATCH v3] hwmon: ntc: fix iio raw to microvolts conversion
@ 2015-06-01 16:27 Chris Lesiak
  2015-06-01 19:54 ` Guenter Roeck
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Chris Lesiak @ 2015-06-01 16:27 UTC (permalink / raw)
  To: lm-sensors

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 <chris.lesiak@licor.com>
---
 drivers/hwmon/ntc_thermistor.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

This patch was previously titled:
"hwmon: ntc: use iio_read_channel_processed if possible"
but that doesn't describe it anymore.

Changes in v3
======
- Don't use iio_read_channel_processed because it does not return results in
  microvolts.

- Do use iio_convert_raw_to_processed with a scale factor of 1000 to get
  microvolts instead of the iio normal unit of millivolts.

- Do it all in the context of the original ntc_adc_iio_read function instead
  of having separate read functions for the raw and processed cases.

diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
index 6880011..e08ed47 100644
--- a/drivers/hwmon/ntc_thermistor.c
+++ b/drivers/hwmon/ntc_thermistor.c
@@ -190,20 +190,21 @@ struct ntc_data {
 static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
 {
 	struct iio_channel *channel = pdata->chan;
-	s64 result;
-	int val, ret;
+	int raw, uv, ret;
 
-	ret = iio_read_channel_raw(channel, &val);
+	ret = iio_read_channel_raw(channel, &raw);
 	if (ret < 0) {
 		pr_err("read channel() error: %d\n", ret);
 		return ret;
 	}
 
-	/* unit: mV */
-	result = pdata->pullup_uv * (s64) val;
-	result >>= 12;
+	ret = iio_convert_raw_to_processed(channel, raw, &uv, 1000);
+	if (ret < 0) {
+		/* Assume 12 bit ADC with vref at pullup_uv*/
+		uv = (pdata->pullup_uv * (s64)raw) >> 12;
+	}
 
-	return (int)result;
+	return uv;
 }
 
 static const struct of_device_id ntc_match[] = {
-- 
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] 6+ messages in thread

end of thread, other threads:[~2015-06-01 23:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-01 16:27 [lm-sensors] [PATCH v3] hwmon: ntc: fix iio raw to microvolts conversion Chris Lesiak
2015-06-01 19:54 ` Guenter Roeck
2015-06-01 21:21 ` Chris Lesiak
2015-06-01 21:44 ` Guenter Roeck
2015-06-01 22:06 ` Chris Lesiak
2015-06-01 23:48 ` 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.