All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Linus Walleij <linus.walleij@linaro.org>,
	Jean Delvare <jdelvare@suse.com>
Cc: linux-hwmon@vger.kernel.org, Peter Rosin <peda@axentia.se>,
	Chris Lesiak <chris.lesiak@licor.com>,
	Jonathan Cameron <jic23@cam.ac.uk>,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH] hwmon: (ntc_thermistor): try reading processed
Date: Mon, 21 Dec 2020 08:15:39 -0800	[thread overview]
Message-ID: <6d339a6c-6f8c-4a5e-718b-c90a9fbb8c01@roeck-us.net> (raw)
In-Reply-To: <20201219224143.686074-1-linus.walleij@linaro.org>

On 12/19/20 2:41 PM, Linus Walleij wrote:
> Before trying the custom method of reading the sensor
> as raw and then converting assuming 1000 scaling, just
> use iio_read_channel_processed() which first tries to
> see if the ADC can provide a processed value directly,
> else reads raw and applies scaling inside of IIO
> using the scale attributes of the ADC.
> 
> The code that hardcodes scaling to 1000 and assumes
> a 12bit ADC is very dubious. I keep it around here
> but I have a strong urge to just delete it.
> 
> This gives correct readings on the AB8500 thermistor
> inputs used in the Ux500 HREFP520 platform for reading
> battery and board temperature.
> 
> Cc: Peter Rosin <peda@axentia.se>
> Cc: Chris Lesiak <chris.lesiak@licor.com>
> Cc: Jonathan Cameron <jic23@cam.ac.uk>
> Cc: linux-iio@vger.kernel.org
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>  drivers/hwmon/ntc_thermistor.c | 29 ++++++++++++++++++++---------
>  1 file changed, 20 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/hwmon/ntc_thermistor.c b/drivers/hwmon/ntc_thermistor.c
> index 3aad62a0e661..ac0d80faddf6 100644
> --- a/drivers/hwmon/ntc_thermistor.c
> +++ b/drivers/hwmon/ntc_thermistor.c
> @@ -326,18 +326,29 @@ struct ntc_data {
>  static int ntc_adc_iio_read(struct ntc_thermistor_platform_data *pdata)
>  {
>  	struct iio_channel *channel = pdata->chan;
> -	int raw, uv, ret;
> +	int uv, ret;
>  
> -	ret = iio_read_channel_raw(channel, &raw);
> +	/* A processed voltage channel will return microvolts */
> +	ret = iio_read_channel_processed(channel, &uv);
>  	if (ret < 0) {
> -		pr_err("read channel() error: %d\n", ret);
> -		return ret;
> -	}
> +		int raw;
>  
> -	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;
> +		/*
> +		 * FIXME: This fallback to using a raw read and then right
> +		 * out assume the ADC is 12 bits and hard-coding scale
> +		 * to 1000 seems a bit dangerous. Should it simply be
> +		 * deleted?
> +		 */

The hwmon ABI specifically supports unscaled values, which can then be
scaled in userspace using the sensors configuration file.
Given that we return the pseudo-scaled value to userspace today,
it seems to me that it would do more harm to change that instead of just
leaving it in place.

Either case, calling iio_convert_raw_to_processed() does not seem to add
value here. This is already done by iio_read_channel_processed().
The best we can do is to use the original fallback.

Guenter

> +		ret = iio_read_channel_raw(channel, &raw);
> +		if (ret < 0) {
> +			pr_err("read channel() error: %d\n", ret);
> +			return ret;
> +		}
> +		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 uv;
> 


  reply	other threads:[~2020-12-21 18:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-19 22:41 [PATCH] hwmon: (ntc_thermistor): try reading processed Linus Walleij
2020-12-21 16:15 ` Guenter Roeck [this message]
2020-12-23 21:08   ` Linus Walleij
2020-12-23 21:42     ` Guenter Roeck
2020-12-24  0:47       ` Linus Walleij
2020-12-23 22:39     ` Chris Lesiak
     [not found]     ` <SN6PR08MB5565DD42F17BA93D362DB95B9ADE0@SN6PR08MB5565.namprd08.prod.outlook.com>
2020-12-24  0:39       ` Linus Walleij

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6d339a6c-6f8c-4a5e-718b-c90a9fbb8c01@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=chris.lesiak@licor.com \
    --cc=jdelvare@suse.com \
    --cc=jic23@cam.ac.uk \
    --cc=linus.walleij@linaro.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=peda@axentia.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.