Skip to contentSkip to conversation

iio: adc: Provide mult_fract() and make use of it

PATCH In review Andy Shevchenko 3 participants 6 messages Nov 19 – Nov 21

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover iio: adc: Provide mult_fract() and make use of it

In a couple of IIO ADC driver the suggested new helper mult_fract() may be used. So, provide it and update the straightforward users.

The helper has potential treewide, hence it's provided directly in math.h.

In v2:
- updated commit message in patch 1 (Sashiko, c/o Joshua)
- used local variable to avoid double evaluation (Sashiko, c/o Joshua)
- collected tags (Marcelo)

v1: <[email protected]>

Andy Shevchenko (3):
math.h: Add mult_fract() that takes struct __Txx_fract as argument
iio: adc: ad4030: Switch to use mult_fract() helper
iio: adc: ltc2378: Switch to use mult_fract() helper

 drivers/iio/adc/ad4030.c  | 13 ++++++-------
 drivers/iio/adc/ltc2378.c |  4 +++-
 include/linux/math.h      |  7 +++++++
 3 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/3 math.h: Add mult_fract() that takes struct __Txx_fract as argument

Add mult_fract() that takes struct __Txx_fract as argument and calls mult_frac() underneath.

---
 include/linux/math.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/math.h +7 -0
index 1e8fb3efbc8c..0f584ee48753 100644
@@ -128,6 +128,13 @@ __STRUCT_FRACT(u32)
 	q * n_ + r * n_ / d_;	\
 })
 
+/* Same as above but for a struct __Txx_fract fraction */
+#define mult_fract(x, f) 				\
+({							\
+	auto f_ = (f);					\
+	mult_frac(x, f_->numerator, f_->denominator);	\
+})
+
 #define sector_div(a, b) do_div(a, b)
 
 /**
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 2/3 iio: adc: ad4030: Switch to use mult_fract() helper

In reply to Andy Shevchenko

Switch the driver to use mult_fract() helper.

---
 drivers/iio/adc/ad4030.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/iio/adc/ad4030.c +6 -7
index 877b06007396..d61950129083 100644
@@ -161,11 +161,11 @@ static const int adaq4216_hw_gains_vpv[] = {
 	20 * MILLI / 3,		/* 0.6666 */
 };
 
-static const int adaq4216_hw_gains_frac[][2] = {
-	{ 1, 3 },  /* 1/3 V/V gain */
-	{ 5, 9 },  /* 5/9 V/V gain */
-	{ 20, 9 }, /* 20/9 V/V gain */
-	{ 20, 3 }, /* 20/3 V/V gain */
+static const struct s32_fract adaq4216_hw_gains_frac[] = {
+	{ .numerator = 3, .denominator = 1 },  /* 1/3 V/V gain */
+	{ .numerator = 9, .denominator = 5 },  /* 5/9 V/V gain */
+	{ .numerator = 9, .denominator = 20 }, /* 20/9 V/V gain */
+	{ .numerator = 3, .denominator = 20 }, /* 20/3 V/V gain */
 };
 
 struct ad4030_chip_info {
@@ -467,8 +467,7 @@ static void ad4030_fill_scale_avail(struct ad4030_state *st)
 	mag_bits = st->chip->precision_bits - 1;
 
 	for (unsigned int i = 0; i < ARRAY_SIZE(adaq4216_hw_gains_frac); i++) {
-		range = mult_frac(st->vref_uv, adaq4216_hw_gains_frac[i][1],
-				  adaq4216_hw_gains_frac[i][0]);
+		range = mult_fract(st->vref_uv, &adaq4216_hw_gains_frac[i]);
 		/*
 		 * If range were in mV, we would multiply it by NANO below.
 		 * Though, range is in µV so multiply it by MICRO only so the
Signature · 1 line
2.50.1
#3 lore raw

PATCH v2 3/3 iio: adc: ltc2378: Switch to use mult_fract() helper

In reply to Andy Shevchenko

Switch the driver to use mult_fract() helper.

---
 drivers/iio/adc/ltc2378.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/ltc2378.c +3 -1
index b0ff2be1aee0..a0c5fdd5e761 100644
@@ -396,9 +396,11 @@ static int ltc2378_read_raw(struct iio_dev *indio_dev,
 	}
 	case IIO_CHAN_INFO_SCALE: {
 		struct u32_fract fract = st->info->internal_div;
+
 		*val = st->ref_uV / MILLI;
 		if (fract.numerator && fract.denominator)
-			*val = mult_frac(*val, fract.numerator, fract.denominator);
+			*val = mult_fract(*val, &fract);
+
 		/*
 		 * For all LTC2378-like devices, the amount of bits that express
 		 * voltage magnitude depend on the polarity / output code format:
Signature · 1 line
2.50.1
Reviewed-by on the v2 series
#4 lore raw
In reply to Andy Shevchenko
Quoted 17 lines · Andy Shevchenko

In a couple of IIO ADC driver the suggested new helper mult_fract() may be used. So, provide it and update the straightforward users.

The helper has potential treewide, hence it's provided directly in math.h.

In v2:
- updated commit message in patch 1 (Sashiko, c/o Joshua)
- used local variable to avoid double evaluation (Sashiko, c/o Joshua)
- collected tags (Marcelo)

v1: <[email protected]>

Andy Shevchenko (3):
math.h: Add mult_fract() that takes struct __Txx_fract as argument
iio: adc: ad4030: Switch to use mult_fract() helper
iio: adc: ltc2378: Switch to use mult_fract() helper

Quoted 5 lines
 drivers/iio/adc/ad4030.c  | 13 ++++++-------
 drivers/iio/adc/ltc2378.c |  4 +++-
 include/linux/math.h      |  7 +++++++
 3 files changed, 16 insertions(+), 8 deletions(-)

Fine now.

Signature · 2 lines
Kind regards,
Joshua Crofts
maintainer
#5 lore raw
Quoted 24 lines · On Fri, 20 Nov 2026 11:28:35 +0100 Joshua Crofts <[email protected]> wrote:
Quoted 17 lines · Andy Shevchenko

In a couple of IIO ADC driver the suggested new helper mult_fract() may be used. So, provide it and update the straightforward users.

The helper has potential treewide, hence it's provided directly in math.h.

In v2:
- updated commit message in patch 1 (Sashiko, c/o Joshua)
- used local variable to avoid double evaluation (Sashiko, c/o Joshua)
- collected tags (Marcelo)

v1: <[email protected]>

Andy Shevchenko (3):
math.h: Add mult_fract() that takes struct __Txx_fract as argument
iio: adc: ad4030: Switch to use mult_fract() helper
iio: adc: ltc2378: Switch to use mult_fract() helper

Quoted 5 lines
 drivers/iio/adc/ad4030.c  | 13 ++++++-------
 drivers/iio/adc/ltc2378.c |  4 +++-
 include/linux/math.h      |  7 +++++++
 3 files changed, 16 insertions(+), 8 deletions(-)
Quoted 4 lines

Fine now.

I briefly wondered if we should wait for some tags related to math.h but then I couldn't figure out who would be paying attention to that other than Andy. So I guess we are fine without.

Applied to the testing branch of iio.git.

Thanks,

Jonathan