Linux IIO development
 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>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	 linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	 linux-iio@vger.kernel.org,
	Romain Gantois <romain.gantois@bootlin.com>
Subject: [PATCH v4 3/6] iio: add processed write API
Date: Mon, 24 Nov 2025 15:48:07 +0100	[thread overview]
Message-ID: <20251124-ltm8054-driver-v4-3-107a8a814abe@bootlin.com> (raw)
In-Reply-To: <20251124-ltm8054-driver-v4-0-107a8a814abe@bootlin.com>

Add a function to allow IIO consumers to write a processed value to a
channel.

Signed-off-by: Romain Gantois <romain.gantois@bootlin.com>
---
 drivers/iio/inkern.c         | 127 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/iio/consumer.h |  36 ++++++++++++
 2 files changed, 163 insertions(+)

diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 70b6f589f37a..6667e8e7648b 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -635,6 +635,54 @@ int iio_multiply_value(int *result, s64 multiplier,
 }
 EXPORT_SYMBOL_NS_GPL(iio_multiply_value, "IIO_UNIT_TEST");
 
+int iio_divide_by_value(int *result, s64 numerator,
+			unsigned int type, int val, int val2)
+{
+	s64 tmp_num, tmp_den;
+
+	switch (type) {
+	case IIO_VAL_INT:
+		tmp_num = numerator;
+		tmp_den = val;
+		break;
+	case IIO_VAL_INT_PLUS_MICRO:
+		tmp_num = numerator * MICRO;
+		/* Cast inside abs() to avoid undefined behavior if val* == -INT_MIN. */
+		tmp_den = abs((s64)val) * MICRO + abs((s64)val2);
+
+		if (val < 0 || val2 < 0)
+			tmp_num *= -1;
+
+		break;
+	case IIO_VAL_INT_PLUS_NANO:
+		tmp_num = numerator * NANO;
+		tmp_den = abs((s64)val) * NANO + abs((s64)val2);
+
+		if (val < 0 || val2 < 0)
+			tmp_num *= -1;
+
+		break;
+	case IIO_VAL_FRACTIONAL:
+		tmp_num = (s64)numerator * (s64)val2;
+		tmp_den = val;
+		break;
+	case IIO_VAL_FRACTIONAL_LOG2:
+		tmp_num = (s64)numerator << val2;
+		tmp_den = val;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (!tmp_den)
+		return -EDOM;
+
+	*result = div64_s64(tmp_num, tmp_den);
+
+	return IIO_VAL_INT;
+}
+EXPORT_SYMBOL_NS_GPL(iio_divide_by_value, "IIO_UNIT_TEST");
+
 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
 						 int raw, int *processed,
 						 unsigned int scale)
@@ -703,6 +751,66 @@ int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
 }
 EXPORT_SYMBOL_NS_GPL(iio_convert_raw_to_processed, "IIO_CONSUMER");
 
+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, half_step = 0;
+
+	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 -EDOM;
+
+	*raw = div_s64(*raw, scale);
+
+	offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
+				       IIO_CHAN_INFO_OFFSET);
+
+	switch (offset_type) {
+	case IIO_VAL_INT:
+	case IIO_VAL_INT_PLUS_MICRO:
+		half_step = MICRO / 2;
+		break;
+	case IIO_VAL_INT_PLUS_NANO:
+		half_step = NANO / 2;
+		break;
+	case IIO_VAL_FRACTIONAL:
+		offset_val = DIV_ROUND_CLOSEST(offset_val, offset_val2);
+		break;
+	case IIO_VAL_FRACTIONAL_LOG2:
+		offset_val >>= offset_val2;
+		break;
+	default:
+		if (offset_type >= 0)
+			return -EINVAL;
+
+		offset_val = 0;
+	}
+
+	/* Round fractional part to closest to reduce rounding bias. */
+	if (half_step) {
+		if (offset_val2 >= half_step)
+			*raw -= 1;
+		else if (offset_val2 <= -half_step)
+			*raw += 1;
+	}
+
+	*raw -= offset_val;
+
+	return 0;
+}
+
 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
 			       enum iio_chan_info_enum attribute)
 {
@@ -1039,3 +1147,22 @@ ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
 	return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
 }
 EXPORT_SYMBOL_NS_GPL(iio_read_channel_label, "IIO_CONSUMER");
+
+int iio_write_channel_processed_scale(struct iio_channel *chan, int val,
+				      unsigned int scale)
+{
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
+	int ret, processed;
+
+	guard(mutex)(&iio_dev_opaque->info_exist_lock);
+
+	if (!chan->indio_dev->info)
+		return -ENODEV;
+
+	ret = iio_convert_processed_to_raw_unlocked(chan, val, &processed, scale);
+	if (ret)
+		return ret;
+
+	return iio_channel_write(chan, processed, 0, IIO_CHAN_INFO_RAW);
+}
+EXPORT_SYMBOL_NS_GPL(iio_write_channel_processed_scale, "IIO_CONSUMER");
diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
index a38b277c2c02..f80ab1b80234 100644
--- a/include/linux/iio/consumer.h
+++ b/include/linux/iio/consumer.h
@@ -399,6 +399,24 @@ int iio_read_channel_scale(struct iio_channel *chan, int *val,
 int iio_multiply_value(int *result, s64 multiplier,
 		       unsigned int type, int val, int val2);
 
+/**
+ * iio_divide_by_value() - Divide by an IIO value
+ * @result:	Destination pointer for the division result
+ * @numerator:	Numerator.
+ * @type:	One of the %IIO_VAL_* constants. This decides how the @val
+ *		and @val2 parameters are interpreted.
+ * @val:	Denominator.
+ * @val2:	Denominator. @val2 use depends on type.
+ *
+ * Divide @numerator by an IIO value, storing the result as
+ * %IIO_VAL_INT. This is typically used for scaling.
+ *
+ * Returns:
+ * %IIO_VAL_INT on success or a negative error-number on failure.
+ */
+int iio_divide_by_value(int *result, s64 numerator,
+			unsigned int type, int val, int val2);
+
 /**
  * iio_convert_raw_to_processed() - Converts a raw value to a processed value
  * @chan:		The channel being queried
@@ -469,4 +487,22 @@ ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
  */
 ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf);
 
+/**
+ * iio_write_channel_processed_scale() - scale and write processed value to a given channel
+ * @chan:		The channel being queried.
+ * @val:		Value to write.
+ * @scale:		Processed value is divided by this scale factor during the conversion.
+ *
+ * This function writes a processed value to a channel. A processed value means
+ * that this value will have the correct unit and not some device internal
+ * representation. If the device does not support writing a processed value, the
+ * function will query the channel's scale and offset and write an appropriately
+ * transformed raw value.
+ *
+ * Returns:
+ * 0 or a negative error-number on failure.
+ */
+int iio_write_channel_processed_scale(struct iio_channel *chan, int val,
+				      unsigned int scale);
+
 #endif

-- 
2.51.2


  parent reply	other threads:[~2025-11-24 14:48 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-24 14:48 [PATCH v4 0/6] Add support for the LTM8054 voltage regulator Romain Gantois
2025-11-24 14:48 ` [PATCH v4 1/6] regulator: dt-bindings: Add Linear Technology LTM8054 regulator Romain Gantois
2025-11-24 14:48 ` [PATCH v4 2/6] iio: inkern: Use namespaced exports Romain Gantois
2025-11-24 14:48 ` Romain Gantois [this message]
2025-12-07 18:52   ` [PATCH v4 3/6] iio: add processed write API Jonathan Cameron
2025-12-08  8:44     ` Romain Gantois
2025-11-24 14:48 ` [PATCH v4 4/6] iio: test: Add kunit tests for iio_divide_by_value() Romain Gantois
2025-11-24 14:48 ` [PATCH v4 5/6] regulator: Support the LTM8054 voltage regulator Romain Gantois
2025-11-24 14:48 ` [PATCH v4 6/6] regulator: ltm8054: Support output current limit control Romain Gantois
2025-11-24 14:57 ` [PATCH v4 0/6] Add support for the LTM8054 voltage regulator Guenter Roeck
2025-11-24 15:07   ` Andy Shevchenko
2025-11-24 15:13   ` Romain Gantois
2025-11-24 15:35     ` H. Nikolaus Schaller
2025-11-24 15:57       ` Romain Gantois
2025-11-24 16:19         ` H. Nikolaus Schaller
2025-11-25  8:41           ` Romain Gantois
2025-11-25 10:25             ` H. Nikolaus Schaller
2025-11-25 16:37               ` Guenter Roeck
2025-12-07 18:48                 ` Jonathan Cameron
2025-12-08  8:57                   ` Romain Gantois
2025-11-27 15:06               ` Romain Gantois
2025-11-25  8:49           ` Romain Gantois
2025-11-24 15:40     ` Guenter Roeck
2025-11-24 16:02       ` 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=20251124-ltm8054-driver-v4-3-107a8a814abe@bootlin.com \
    --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=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