linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Lars-Peter Clausen <lars@metafoo.de>
Cc: Jonathan Cameron <jic23@cam.ac.uk>,
	linux-iio@vger.kernel.org, drivers@analog.com
Subject: Re: [PATCH 3/5] staging:iio:ad7298: Fix temperature scale and offset
Date: Sat, 17 Nov 2012 11:19:11 +0000	[thread overview]
Message-ID: <50A772AF.70006@kernel.org> (raw)
In-Reply-To: <1352985328-24322-3-git-send-email-lars@metafoo.de>

On 11/15/2012 01:15 PM, Lars-Peter Clausen wrote:
> The temperature scale and offset depend on the reference voltage, the current
> formula used in the driver assumes a 2.5V reference. This patch modifies the
> code to report the unprocessed value for the temperature channel "raw" property
> and to provide proper "scale" and "offset" properties which depend on the
> selected reference voltage.
added to togreg branch of iio.git. Thanks.

> 
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
> ---
>  drivers/staging/iio/adc/ad7298_core.c | 51 ++++++++++++++++-------------------
>  1 file changed, 23 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/staging/iio/adc/ad7298_core.c b/drivers/staging/iio/adc/ad7298_core.c
> index 6dd696f..b74b76f 100644
> --- a/drivers/staging/iio/adc/ad7298_core.c
> +++ b/drivers/staging/iio/adc/ad7298_core.c
> @@ -45,7 +45,8 @@ static const struct iio_chan_spec ad7298_channels[] = {
>  		.indexed = 1,
>  		.channel = 0,
>  		.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
> -		IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
> +			IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
> +			IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
>  		.address = AD7298_CH_TEMP,
>  		.scan_index = -1,
>  		.scan_type = {
> @@ -80,7 +81,7 @@ static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
>  
>  static int ad7298_scan_temp(struct ad7298_state *st, int *val)
>  {
> -	int tmp, ret;
> +	int ret;
>  	__be16 buf;
>  
>  	buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
> @@ -102,24 +103,24 @@ static int ad7298_scan_temp(struct ad7298_state *st, int *val)
>  	if (ret)
>  		return ret;
>  
> -	tmp = be16_to_cpu(buf) & RES_MASK(AD7298_BITS);
> +	*val = sign_extend32(be16_to_cpu(buf), 11);
>  
> -	/*
> -	 * One LSB of the ADC corresponds to 0.25 deg C.
> -	 * The temperature reading is in 12-bit twos complement format
> -	 */
> +	return 0;
> +}
> +
> +static int ad7298_get_ref_voltage(struct ad7298_state *st)
> +{
> +	int vref;
>  
> -	if (tmp & (1 << (AD7298_BITS - 1))) {
> -		tmp = (4096 - tmp) * 250;
> -		tmp -= (2 * tmp);
> +	if (st->ext_ref) {
> +		vref = regulator_get_voltage(st->reg);
> +		if (vref < 0)
> +			return vref;
>  
> +		return vref / 1000;
>  	} else {
> -		tmp *= 250; /* temperature in milli degrees Celsius */
> +		return AD7298_INTREF_mV;
>  	}
> -
> -	*val = tmp;
> -
> -	return 0;
>  }
>  
>  static int ad7298_read_raw(struct iio_dev *indio_dev,
> @@ -130,7 +131,6 @@ static int ad7298_read_raw(struct iio_dev *indio_dev,
>  {
>  	int ret;
>  	struct ad7298_state *st = iio_priv(indio_dev);
> -	int scale_mv;
>  
>  	switch (m) {
>  	case IIO_CHAN_INFO_RAW:
> @@ -155,24 +155,19 @@ static int ad7298_read_raw(struct iio_dev *indio_dev,
>  	case IIO_CHAN_INFO_SCALE:
>  		switch (chan->type) {
>  		case IIO_VOLTAGE:
> -			if (st->ext_ref) {
> -				scale_mv = regulator_get_voltage(st->reg);
> -				if (scale_mv < 0)
> -					return scale_mv;
> -				scale_mv /= 1000;
> -			} else {
> -				scale_mv = AD7298_INTREF_mV;
> -			}
> -			*val =  scale_mv;
> +			*val = ad7298_get_ref_voltage(st);
>  			*val2 = chan->scan_type.realbits;
>  			return IIO_VAL_FRACTIONAL_LOG2;
>  		case IIO_TEMP:
> -			*val =  1;
> -			*val2 = 0;
> -			return IIO_VAL_INT_PLUS_MICRO;
> +			*val = ad7298_get_ref_voltage(st);
> +			*val2 = 10;
> +			return IIO_VAL_FRACTIONAL;
>  		default:
>  			return -EINVAL;
>  		}
> +	case IIO_CHAN_INFO_OFFSET:
> +		*val = 1093 - 2732500 / ad7298_get_ref_voltage(st);
> +		return IIO_VAL_INT;
>  	}
>  	return -EINVAL;
>  }
> 

  reply	other threads:[~2012-11-17 11:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-15 13:15 [PATCH 1/5] staging:iio:ad7298: Do not perform endianness conversion in buffered mode Lars-Peter Clausen
2012-11-15 13:15 ` [PATCH 2/5] staging:iio:ad7298: Rework regulator handling Lars-Peter Clausen
2012-11-17 11:17   ` Jonathan Cameron
2012-11-15 13:15 ` [PATCH 3/5] staging:iio:ad7298: Fix temperature scale and offset Lars-Peter Clausen
2012-11-17 11:19   ` Jonathan Cameron [this message]
2012-11-15 13:15 ` [PATCH 4/5] staging:iio:ad7298: Squash everything into one file Lars-Peter Clausen
2012-11-17 11:20   ` Jonathan Cameron
2012-11-17 11:34     ` Jonathan Cameron
2012-11-17 17:23       ` Lars-Peter Clausen
2012-11-15 13:15 ` [PATCH 5/5] staging:iio: Move the ad7298 driver out of staging Lars-Peter Clausen
2012-11-17 11:45   ` Jonathan Cameron
2012-11-17 11:16 ` [PATCH 1/5] staging:iio:ad7298: Do not perform endianness conversion in buffered mode Jonathan Cameron

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=50A772AF.70006@kernel.org \
    --to=jic23@kernel.org \
    --cc=drivers@analog.com \
    --cc=jic23@cam.ac.uk \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).