Linux IIO development
 help / color / mirror / Atom feed
* [PATCH v2] iio: dac: ad5592r: fix temperature channel scaling value
@ 2024-05-01 15:05 marc.ferland
  2024-05-06 14:35 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: marc.ferland @ 2024-05-01 15:05 UTC (permalink / raw)
  To: lars; +Cc: Michael.Hennerich, jic23, linux-iio, linux-kernel, Marc Ferland

From: Marc Ferland <marc.ferland@sonatest.com>

The scale value for the temperature channel is (assuming Vref=2.5 and
the datasheet):

    376.7897513

When calculating both val and val2 for the temperature scale we
use (3767897513/25) and multiply it by Vref (here I assume 2500mV) to
obtain:

  2500 * (3767897513/25) ==> 376789751300

Finally we divide with remainder by 10^9 to get:

    val = 376
    val2 = 789751300

However, we return IIO_VAL_INT_PLUS_MICRO (should have been NANO) as
the scale type. So when converting the raw temperature value to the
'processed' temperature value we will get (assuming raw=810,
offset=-753):

    processed = (raw + offset) * scale_val
              = (810 + -753) * 376
	      = 21432

    processed += div((raw + offset) * scale_val2, 10^6)
              += div((810 + -753) * 789751300, 10^6)
	      += 45015
    ==> 66447
    ==> 66.4 Celcius

instead of the expected 21.5 Celsius.

Fix this issue by changing IIO_VAL_INT_PLUS_MICRO to
IIO_VAL_INT_PLUS_NANO.

Signed-off-by: Marc Ferland <marc.ferland@sonatest.com>
---
Change in v2:
 - Improve commit message as suggested by Jonathan.

 drivers/iio/dac/ad5592r-base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/dac/ad5592r-base.c b/drivers/iio/dac/ad5592r-base.c
index 076bc9ecfb49..4763402dbcd6 100644
--- a/drivers/iio/dac/ad5592r-base.c
+++ b/drivers/iio/dac/ad5592r-base.c
@@ -415,7 +415,7 @@ static int ad5592r_read_raw(struct iio_dev *iio_dev,
 			s64 tmp = *val * (3767897513LL / 25LL);
 			*val = div_s64_rem(tmp, 1000000000LL, val2);
 
-			return IIO_VAL_INT_PLUS_MICRO;
+			return IIO_VAL_INT_PLUS_NANO;
 		}
 
 		mutex_lock(&st->lock);

base-commit: 98369dccd2f8e16bf4c6621053af7aa4821dcf8e
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] iio: dac: ad5592r: fix temperature channel scaling value
  2024-05-01 15:05 [PATCH v2] iio: dac: ad5592r: fix temperature channel scaling value marc.ferland
@ 2024-05-06 14:35 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2024-05-06 14:35 UTC (permalink / raw)
  To: marc.ferland
  Cc: lars, Michael.Hennerich, linux-iio, linux-kernel, Marc Ferland,
	Nuno Sa

On Wed,  1 May 2024 11:05:54 -0400
marc.ferland@gmail.com wrote:

> From: Marc Ferland <marc.ferland@sonatest.com>
> 
> The scale value for the temperature channel is (assuming Vref=2.5 and
> the datasheet):
> 
>     376.7897513
> 
> When calculating both val and val2 for the temperature scale we
> use (3767897513/25) and multiply it by Vref (here I assume 2500mV) to
> obtain:
> 
>   2500 * (3767897513/25) ==> 376789751300
> 
> Finally we divide with remainder by 10^9 to get:
> 
>     val = 376
>     val2 = 789751300
> 
> However, we return IIO_VAL_INT_PLUS_MICRO (should have been NANO) as
> the scale type. So when converting the raw temperature value to the
> 'processed' temperature value we will get (assuming raw=810,
> offset=-753):
> 
>     processed = (raw + offset) * scale_val
>               = (810 + -753) * 376
> 	      = 21432
> 
>     processed += div((raw + offset) * scale_val2, 10^6)
>               += div((810 + -753) * 789751300, 10^6)
> 	      += 45015
>     ==> 66447
>     ==> 66.4 Celcius  
> 
> instead of the expected 21.5 Celsius.
> 
> Fix this issue by changing IIO_VAL_INT_PLUS_MICRO to
> IIO_VAL_INT_PLUS_NANO.
> 
> Signed-off-by: Marc Ferland <marc.ferland@sonatest.com>
+CC Nuno.

Seems obviously correct to me so I'll apply it to the fixes-togreg
branch of iio.git.  I've been wrong before though so ideally would like
one of the ADI team to sanity check this.

I'll add the fixes tag as well and mark it for stable. This goes all the
way back.

Fixes: 56ca9db862bf ("iio: dac: Add support for the AD5592R/AD5593R ADCs/DACs")


> ---
> Change in v2:
>  - Improve commit message as suggested by Jonathan.
> 
>  drivers/iio/dac/ad5592r-base.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/dac/ad5592r-base.c b/drivers/iio/dac/ad5592r-base.c
> index 076bc9ecfb49..4763402dbcd6 100644
> --- a/drivers/iio/dac/ad5592r-base.c
> +++ b/drivers/iio/dac/ad5592r-base.c
> @@ -415,7 +415,7 @@ static int ad5592r_read_raw(struct iio_dev *iio_dev,
>  			s64 tmp = *val * (3767897513LL / 25LL);
>  			*val = div_s64_rem(tmp, 1000000000LL, val2);
>  
> -			return IIO_VAL_INT_PLUS_MICRO;
> +			return IIO_VAL_INT_PLUS_NANO;
>  		}
>  
>  		mutex_lock(&st->lock);
> 
> base-commit: 98369dccd2f8e16bf4c6621053af7aa4821dcf8e


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-06 14:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-01 15:05 [PATCH v2] iio: dac: ad5592r: fix temperature channel scaling value marc.ferland
2024-05-06 14:35 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox