[PATCH 07/18] iio: adc: ad4170-4: Fix overflow in sample rate fractional part
From: Janani Sunil
Date: Thu Dec 03 2026 - 10:21:06 EST
ad4170_fill_sps_tbl() computes the fractional part of each sample rate
with mult_frac(), which works in the type of its first argument. The
remainder times MICRO overflows unsigned int for remainders above 4294,
so the fractional part reported for FILTER_FS 65532 at the 16 MHz main
clock is 7.001085 instead of 7.629860. A slower main clock makes more
entries overflow.
Compute the fractional part with 64-bit arithmetic.
Fixes: a770f70b4f5b ("iio: adc: ad4170-4: Add digital filter and sample frequency config support")
Signed-off-by: Janani Sunil <janani.sunil@xxxxxxxxxx>
---
drivers/iio/adc/ad4170-4.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/ad4170-4.c b/drivers/iio/adc/ad4170-4.c
index f353b630834e..575ac8d61301 100644
--- a/drivers/iio/adc/ad4170-4.c
+++ b/drivers/iio/adc/ad4170-4.c
@@ -475,7 +475,8 @@ static void ad4170_fill_sps_tbl(struct ad4170_state *st)
for (i = 0; i < ARRAY_SIZE(ad4170_sinc3_filt_fs_tbl); i++) {
tmp0 = div_u64_rem(st->mclk_hz, 32 * ad4170_sinc3_filt_fs_tbl[i],
&tmp1);
- tmp1 = mult_frac(tmp1, MICRO, 32 * ad4170_sinc3_filt_fs_tbl[i]);
+ tmp1 = div_u64((u64)tmp1 * MICRO,
+ 32 * ad4170_sinc3_filt_fs_tbl[i]);
/* Fill sinc5+avg filter SPS table */
st->sps_tbl[AD4170_SINC5_AVG][i][0] = tmp0; /* Integer part */
st->sps_tbl[AD4170_SINC5_AVG][i][1] = tmp1; /* Fractional part */
@@ -488,7 +489,8 @@ static void ad4170_fill_sps_tbl(struct ad4170_state *st)
for (i = 0; i < ARRAY_SIZE(ad4170_sinc5_filt_fs_tbl); i++) {
tmp0 = div_u64_rem(st->mclk_hz, 32 * ad4170_sinc5_filt_fs_tbl[i],
&tmp1);
- tmp1 = mult_frac(tmp1, MICRO, 32 * ad4170_sinc5_filt_fs_tbl[i]);
+ tmp1 = div_u64((u64)tmp1 * MICRO,
+ 32 * ad4170_sinc5_filt_fs_tbl[i]);
/* Fill sinc5 filter SPS table */
st->sps_tbl[AD4170_SINC5][i][0] = tmp0; /* Integer part */
st->sps_tbl[AD4170_SINC5][i][1] = tmp1; /* Fractional part */
--
2.43.0