[PATCH 03/16] iio: frequency: ad9523: fix the M2 divider selection
From: Nuno Sá
Date: Mon Nov 30 2026 - 12:37:47 EST
For channels 4 to 9, the M2 divider is used if it looks closer to the
requested frequency than M1. However, the comparison is done on the
largest multiple of the requested frequency below each source, which
does not tell how close the resulting output is. For example, with a
3000 MHz VCO, M1 = 3 and M2 = 4, asking for 500 MHz selects M2 and gives
750 MHz, while asking for 375 MHz selects M1 and gives 500 MHz.
Compare the output frequencies each source actually gives instead, and
only use M2 if it gets closer to the requested frequency.
Fixes: cd1678f96329 ("iio: frequency: New driver for AD9523 SPI Low Jitter Clock Generator")
Signed-off-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
---
drivers/iio/frequency/ad9523.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index aecd94db28bb..e6913ef36fb0 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -415,6 +415,13 @@ static int ad9523_vco_out_map(struct iio_dev *indio_dev,
return ret;
}
+static unsigned long ad9523_chan_freq(struct ad9523_state *st, unsigned int src,
+ unsigned long freq)
+{
+ return st->vco_out_freq[src] /
+ clamp(st->vco_out_freq[src] / freq, 1, 1024);
+}
+
static int ad9523_set_clock_provider(struct iio_dev *indio_dev,
unsigned int ch, unsigned long freq)
{
@@ -433,11 +440,10 @@ static int ad9523_set_clock_provider(struct iio_dev *indio_dev,
break;
}
- tmp1 = st->vco_out_freq[AD9523_VCO1] / freq;
- tmp2 = st->vco_out_freq[AD9523_VCO2] / freq;
- tmp1 *= freq;
- tmp2 *= freq;
- use_alt_clk_src = (abs(tmp1 - freq) > abs(tmp2 - freq));
+ /* Use M2 only if the output gets closer to the requested rate */
+ tmp1 = abs_diff(ad9523_chan_freq(st, AD9523_VCO1, freq), freq);
+ tmp2 = abs_diff(ad9523_chan_freq(st, AD9523_VCO2, freq), freq);
+ use_alt_clk_src = tmp2 < tmp1;
break;
default:
/* Ch 10..14: No action required, return success */
--
2.56.0