[PATCH 15/16] iio: frequency: ad9523: split the clock source selection

From: Nuno Sá

Date: Mon Nov 30 2026 - 12:38:54 EST


ad9523_set_clock_provider() decides which clock source a channel uses
for a given frequency and also programs it. Move the decision into
ad9523_use_alt_clk_src(), so that it can be reused without touching the
device.

No functional change.

Signed-off-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
---
drivers/iio/frequency/ad9523.c | 34 +++++++++++++++++++---------------
1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index 216b1a92ce1a..d226ab897b14 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -554,35 +554,39 @@ static unsigned long ad9523_chan_freq(struct ad9523_state *st, unsigned int src,
return st->vco_out_freq[src] / ad9523_chan_div(st, src, freq);
}

-static int ad9523_set_clock_provider(struct iio_dev *indio_dev,
- unsigned int ch, unsigned long freq)
+static bool ad9523_use_alt_clk_src(struct ad9523_state *st, unsigned int ch,
+ unsigned long freq)
{
- struct ad9523_state *st = iio_priv(indio_dev);
long tmp1, tmp2;
- bool use_alt_clk_src;

switch (ch) {
case 0 ... 3:
- use_alt_clk_src = (freq == st->vco_out_freq[AD9523_VCXO]);
- break;
+ return freq == st->vco_out_freq[AD9523_VCXO];
case 4 ... 9:
/* M2 is powered down, so it can't be used */
- if (!st->vco_out_freq[AD9523_VCO2]) {
- use_alt_clk_src = false;
- break;
- }
+ if (!st->vco_out_freq[AD9523_VCO2])
+ return false;

/* 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;
+ return tmp2 < tmp1;
default:
- /* Ch 10..14: No action required, return success */
- return 0;
+ return false;
}
+}

- return ad9523_vco_out_map(indio_dev, ch, use_alt_clk_src);
+static int ad9523_set_clock_provider(struct iio_dev *indio_dev,
+ unsigned int ch, unsigned long freq)
+{
+ struct ad9523_state *st = iio_priv(indio_dev);
+
+ /* Ch 10..13: No action required, return success */
+ if (ch > 9)
+ return 0;
+
+ return ad9523_vco_out_map(indio_dev, ch,
+ ad9523_use_alt_clk_src(st, ch, freq));
}

static int ad9523_store_eeprom(struct iio_dev *indio_dev)

--
2.56.0