devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Romain Gantois <romain.gantois@bootlin.com>
To: "Liam Girdwood" <lgirdwood@gmail.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Jonathan Cameron" <jic23@kernel.org>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>
Cc: Hans de Goede <hansg@kernel.org>,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	linux-iio@vger.kernel.org
Subject: Re: [PATCH v2 2/5] iio: add processed write API
Date: Wed, 01 Oct 2025 09:19:37 +0200	[thread overview]
Message-ID: <5015441.GXAFRqVoOG@fw-rgant> (raw)
In-Reply-To: <2a503edb-fe9f-4d61-92c0-c1083a028e19@baylibre.com>

[-- Attachment #1: Type: text/plain, Size: 1994 bytes --]

Hello David,

On Thursday, 25 September 2025 23:10:14 CEST David Lechner wrote:
> On 9/25/25 7:37 AM, Romain Gantois wrote:
> > Add a function to allow IIO consumers to write a processed value to a
...
> > +static int iio_convert_processed_to_raw_unlocked(struct iio_channel
> > *chan,
> > +						 int processed, int *raw,
> > +						 unsigned int scale)
> > +{
> > +	int scale_type, scale_val, scale_val2;
> > +	int offset_type, offset_val, offset_val2;
> > +	int ret;
> > +
> > +	scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
> > +				      IIO_CHAN_INFO_SCALE);
> > +	if (scale_type >= 0) {
> > +		ret = iio_divide_by_value(raw, processed, scale_type, 
scale_val,
> > scale_val2); +		if (ret < 0)
> > +			return ret;
> > +	} else {
> > +		*raw = processed;
> > +	}
> > +
> > +	if (!scale)
> > +		return -ERANGE;
> > +
> > +	*raw = div_s64(*raw, scale);
> > +
> > +	offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
> > +				       IIO_CHAN_INFO_OFFSET);
> > +	if (offset_type >= 0) {
> > +		switch (offset_type) {
> > +		case IIO_VAL_INT:
> > +		case IIO_VAL_INT_PLUS_MICRO:
> > +		case IIO_VAL_INT_PLUS_NANO:
> > +			break;
> > +		case IIO_VAL_FRACTIONAL:
> > +			offset_val /= offset_val2;
> > +			break;
> > +		case IIO_VAL_FRACTIONAL_LOG2:
> > +			offset_val >>= offset_val2;
> > +			break;
> > +		default:
> > +			return -EINVAL;
> > +		}
> > +
> > +		*raw -= offset_val;
> > +	}
> 
> There are some rounding biases in this function, but I'm not sure if
> it is worth trying to make a perfectly fair function.

I'm unfamiliar with the notion of rounding bias, does it mean that nested 
calls of this function would tend to amplify rounding errors? In this case, 
would rounding to the nearest integer instead of whatever is being done by the 
integer division here be a good solution?

Maybe I'm misunderstanding what you mean, in that case please let me know.

Thanks,

-- 
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2025-10-01  7:19 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-25 12:37 [PATCH v2 0/5] Add support for the LTM8054 voltage regulator Romain Gantois
2025-09-25 12:37 ` [PATCH v2 1/5] regulator: dt-bindings: Add Linear Technology LTM8054 regulator Romain Gantois
2025-09-25 19:27   ` Conor Dooley
2025-09-26 15:59     ` Romain Gantois
2025-09-27 22:31       ` Conor Dooley
2025-10-01  7:11         ` Romain Gantois
2025-10-01 11:18           ` David Lechner
2025-10-01 18:40             ` Conor Dooley
2025-10-02  7:11               ` David Lechner
2025-09-25 12:37 ` [PATCH v2 2/5] iio: add processed write API Romain Gantois
2025-09-25 21:10   ` David Lechner
2025-10-01  7:19     ` Romain Gantois [this message]
2025-10-01 10:43       ` David Lechner
     [not found]       ` <CAMknhBG_o=jTKtHHDyK=bq7wcHMnDM1ZHaYAfX0K2hjHfkX3Bg@mail.gmail.com>
2025-10-03 14:35         ` Romain Gantois
2025-09-28  9:07   ` Jonathan Cameron
2025-09-25 12:37 ` [PATCH v2 3/5] Add kunit tests for iio_divide_by_value() Romain Gantois
2025-09-25 20:26   ` David Lechner
2025-09-25 12:37 ` [PATCH v2 4/5] regulator: Support the LTM8054 voltage regulator Romain Gantois
2025-10-22 14:44   ` Andy Shevchenko
2025-09-25 12:37 ` [PATCH v2 5/5] regulator: ltm8054: Support output current limit control Romain Gantois
2025-10-22  8:05   ` Romain Gantois
2025-10-22 16:37     ` Andy Shevchenko
2025-10-24  7:55       ` Romain Gantois

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=5015441.GXAFRqVoOG@fw-rgant \
    --to=romain.gantois@bootlin.com \
    --cc=andy@kernel.org \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=hansg@kernel.org \
    --cc=jic23@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=thomas.petazzoni@bootlin.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;
as well as URLs for NNTP newsgroup(s).