[PATCH v4 02/12] iio: frequency: ad9910: round up FM gain scale
From: Rodrigo Alencar via B4 Relay
Date: Mon Nov 16 2026 - 11:40:20 EST
From: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
The parallel port frequency scale is set through the CFR2 FM gain field,
which selects a power of two multiplier. The requested scale is converted
into a ratio against the frequency tuning word LSB and then rounded up to
a power of two, so that the resulting scale is the smallest one that is
not below the requested value.
However, the ratio itself was computed with closest rounding, so when its
fractional part is below one half, it gets rounded down before
roundup_pow_of_two() is applied. For instance, a ratio of 1.3 becomes 1,
selecting a gain of 0 instead of 1, and the applied scale ends up smaller
than the requested one.
Use mul_u64_u64_div_u64_roundup() so that the ratio is rounded up as well.
Fixes: 3647f9fe3488 ("iio: frequency: ad9910: add basic parallel port support")
Signed-off-by: Rodrigo Alencar <rodrigo.alencar@xxxxxxxxxx>
---
drivers/iio/frequency/ad9910.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/frequency/ad9910.c b/drivers/iio/frequency/ad9910.c
index 8bc24d0b7442..680415b37b0d 100644
--- a/drivers/iio/frequency/ad9910.c
+++ b/drivers/iio/frequency/ad9910.c
@@ -1626,8 +1626,8 @@ static int ad9910_write_raw(struct iio_dev *indio_dev,
if (val < 0 || val2 < 0)
return -EINVAL;
- tmp64 = ad9910_rational_scale((u64)val * NANO + val2, BIT_ULL(32),
- (u64)st->data.sysclk_freq_hz * NANO);
+ tmp64 = mul_u64_u64_div_u64_roundup((u64)val * NANO + val2, BIT_ULL(32),
+ (u64)st->data.sysclk_freq_hz * NANO);
tmp64 = roundup_pow_of_two(clamp(tmp64, 1ULL, BIT_ULL(15)));
tmp32 = FIELD_PREP(AD9910_CFR2_FM_GAIN_MSK, ilog2(tmp64));
return ad9910_reg32_update(st, AD9910_REG_CFR2,
--
2.43.0