mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Giorgi Tchankvetadze <giorgitchankvetadze1997@gmail.com>
Cc: antoniu.miclaus@analog.com, lars@metafoo.de,
	Michael.Hennerich@analog.com, dlechner@baylibre.com,
	nuno.sa@analog.com, andy@kernel.org, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH RFC] iio: adc: ad4030: fix calibscale read/write unit mismatch
Date: Sat, 28 Feb 2026 16:27:07 +0000	[thread overview]
Message-ID: <20260228162638.00440b5c@jic23-huawei> (raw)
In-Reply-To: <20260225113102.84568-2-giorgitchankvetadze1997@gmail.com>

On Wed, 25 Feb 2026 15:30:39 +0400
Giorgi Tchankvetadze <giorgitchankvetadze1997@gmail.com> wrote:

> The read path returns calibscale in IIO_VAL_INT_PLUS_NANO but the write
> path treats it as MICRO. Since no write_raw_get_fmt is provided, the
> IIO core defaults to MICRO when parsing userspace input.
> 
> This means reading calibscale and writing it back results in a ~1000x
> gain error.
I'm not following this part.  What I'd expect to see something like
reading avail gets you
[0.0 0.0000030518 1.99996982]

Reading the channel gets you maybe 1.1333333333 and then when you write
back because of the lack of write_fmt it gets truncated and you
are effectively reading 1.133333 

So I'd not expect to see a gain error, just a loss of precision.

What am I missing? Please provide a numeric example to motivate the
patch.

Thanks,

Jonathan 
> 
> Change the read path and available range to use MICRO to match the
> write path.
> 
> Fixes: 0cb8b324852f ("iio: adc: ad4030: add driver for ad4030-24")
> Signed-off-by: Giorgi Tchankvetadze <giorgitchankvetadze1997@gmail.com>
> ---
>  drivers/iio/adc/ad4030.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad4030.c b/drivers/iio/adc/ad4030.c
> index def3e1d01ceb..3d823648371d 100644
> --- a/drivers/iio/adc/ad4030.c
> +++ b/drivers/iio/adc/ad4030.c
> @@ -423,10 +423,10 @@ static int ad4030_get_chan_calibscale(struct iio_dev *indio_dev,
>  
>  	/* From datasheet: multiplied output = input × gain word/0x8000 */
>  	*val = gain / AD4030_GAIN_MIDLE_POINT;
> -	*val2 = mul_u64_u32_div(gain % AD4030_GAIN_MIDLE_POINT, NANO,
> +	*val2 = mul_u64_u32_div(gain % AD4030_GAIN_MIDLE_POINT, MICRO,
>  				AD4030_GAIN_MIDLE_POINT);
>  
> -	return IIO_VAL_INT_PLUS_NANO;
> +	return IIO_VAL_INT_PLUS_MICRO;
>  }
>  
>  /* Returns the offset where 1 LSB = (VREF/2^precision_bits - 1)/gain */
> @@ -720,8 +720,8 @@ static irqreturn_t ad4030_trigger_handler(int irq, void *p)
>  
>  static const int ad4030_gain_avail[3][2] = {
>  	{ 0, 0 },
> -	{ 0, 30518 },
> -	{ 1, 999969482 },
> +	{ 0, 30 },
> +	{ 1, 999969 },
>  };
>  
>  static int ad4030_read_avail(struct iio_dev *indio_dev,
> @@ -739,7 +739,7 @@ static int ad4030_read_avail(struct iio_dev *indio_dev,
>  
>  	case IIO_CHAN_INFO_CALIBSCALE:
>  		*vals = (void *)ad4030_gain_avail;
> -		*type = IIO_VAL_INT_PLUS_NANO;
> +		*type = IIO_VAL_INT_PLUS_MICRO;
>  		return IIO_AVAIL_RANGE;
>  
>  	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:


      parent reply	other threads:[~2026-02-28 16:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-25 11:30 Giorgi Tchankvetadze
2026-02-25 11:53 ` Marcelo Schmitt
2026-02-25 12:21   ` Giorgi Tchankvetadze
2026-02-25 15:02     ` Andy Shevchenko
2026-02-25 15:20   ` David Lechner
2026-02-26  9:32     ` Nuno Sá
2026-02-28 16:28       ` Jonathan Cameron
2026-02-28 16:27 ` Jonathan Cameron [this message]

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=20260228162638.00440b5c@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Michael.Hennerich@analog.com \
    --cc=andy@kernel.org \
    --cc=antoniu.miclaus@analog.com \
    --cc=dlechner@baylibre.com \
    --cc=giorgitchankvetadze1997@gmail.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /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

all inboxes | Powered by JetHome®