[PATCH 16/16] iio: frequency: ad9523: add clock provider support

From: Nuno Sá

Date: Mon Nov 30 2026 - 12:39:00 EST


The AD9523-1 is a clock generator, and the devices it clocks can only
get their clocks through the clock framework.

If #clock-cells is present, register a clock for each output channel
described in firmware. Like other IIO frequency drivers, the channels
are then controlled only through the clock framework and are not
exposed as IIO channels. The IIO device stays around for the PLL status
and the other device attributes.

As clock providers need the common clock framework, make the driver
depend on COMMON_CLK.

Signed-off-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
---
drivers/iio/frequency/Kconfig | 2 +-
drivers/iio/frequency/ad9523.c | 150 ++++++++++++++++++++++++++++++++++++++++-
2 files changed, 150 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/frequency/Kconfig b/drivers/iio/frequency/Kconfig
index 994d199080eb..65d7740183bd 100644
--- a/drivers/iio/frequency/Kconfig
+++ b/drivers/iio/frequency/Kconfig
@@ -13,7 +13,7 @@ menu "Clock Generator/Distribution"

config AD9523
tristate "Analog Devices AD9523 Low Jitter Clock Generator"
- depends on SPI
+ depends on SPI && COMMON_CLK
help
Say yes here to build support for Analog Devices AD9523 Low Jitter
Clock Generator. The driver provides direct access via sysfs.
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index d226ab897b14..7968d8b60e58 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -7,6 +7,7 @@

#include <linux/bitops.h>
#include <linux/clk.h>
+#include <linux/clk-provider.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/math64.h>
@@ -277,6 +278,7 @@ enum outp_drv_mode {
* @divider_output_invert_en: Invert the polarity of the output clock.
* @driver_mode: Output driver mode (logic level family).
* @label: Optional channel label.
+ * @always_on: Keep the output running without consumers (clock provider).
*/

struct ad9523_channel_spec {
@@ -284,6 +286,7 @@ struct ad9523_channel_spec {
bool divider_output_invert_en;
enum outp_drv_mode driver_mode;
const char *label;
+ bool always_on;
};

enum rpole2_resistor {
@@ -388,10 +391,19 @@ enum {
AD9523_NUM_CLK_SRC,
};

+struct ad9523_clk {
+ struct clk_hw hw;
+ struct iio_dev *indio_dev;
+ const struct iio_chan_spec *chan;
+};
+
+#define to_ad9523_clk(_hw) container_of(_hw, struct ad9523_clk, hw)
+
struct ad9523_state {
struct spi_device *spi;
struct ad9523_config cfg;
struct iio_chan_spec ad9523_channels[AD9523_NUM_CHAN];
+ struct ad9523_clk clks[AD9523_NUM_CHAN];
struct gpio_desc *pwrdown_gpio;
struct gpio_desc *reset_gpio;
struct gpio_desc *sync_gpio;
@@ -892,6 +904,127 @@ static const struct iio_info ad9523_info = {
.attrs = &ad9523_attribute_group,
};

+static int ad9523_clk_prepare(struct clk_hw *hw)
+{
+ struct ad9523_clk *clk = to_ad9523_clk(hw);
+
+ return ad9523_write_raw(clk->indio_dev, clk->chan, 1, 0,
+ IIO_CHAN_INFO_RAW);
+}
+
+static void ad9523_clk_unprepare(struct clk_hw *hw)
+{
+ struct ad9523_clk *clk = to_ad9523_clk(hw);
+
+ ad9523_write_raw(clk->indio_dev, clk->chan, 0, 0, IIO_CHAN_INFO_RAW);
+}
+
+static unsigned long ad9523_clk_recalc_rate(struct clk_hw *hw,
+ unsigned long parent_rate)
+{
+ struct ad9523_clk *clk = to_ad9523_clk(hw);
+ int ret, val;
+
+ ret = ad9523_read_raw(clk->indio_dev, clk->chan, &val, NULL,
+ IIO_CHAN_INFO_FREQUENCY);
+ if (ret < 0)
+ return 0;
+
+ return val;
+}
+
+static int ad9523_clk_determine_rate(struct clk_hw *hw,
+ struct clk_rate_request *req)
+{
+ struct ad9523_clk *clk = to_ad9523_clk(hw);
+ struct ad9523_state *st = iio_priv(clk->indio_dev);
+ unsigned int ch = clk->chan->channel;
+ unsigned int src;
+
+ if (!req->rate)
+ return -EINVAL;
+
+ src = AD9523_VCO1;
+ if (ad9523_use_alt_clk_src(st, ch, req->rate))
+ src = ch < 4 ? AD9523_VCXO : AD9523_VCO2;
+
+ req->rate = ad9523_chan_freq(st, src, req->rate);
+
+ return 0;
+}
+
+static int ad9523_clk_set_rate(struct clk_hw *hw, unsigned long rate,
+ unsigned long parent_rate)
+{
+ struct ad9523_clk *clk = to_ad9523_clk(hw);
+
+ return ad9523_write_raw(clk->indio_dev, clk->chan, rate, 0,
+ IIO_CHAN_INFO_FREQUENCY);
+}
+
+static const struct clk_ops ad9523_clk_ops = {
+ .prepare = ad9523_clk_prepare,
+ .unprepare = ad9523_clk_unprepare,
+ .recalc_rate = ad9523_clk_recalc_rate,
+ .determine_rate = ad9523_clk_determine_rate,
+ .set_rate = ad9523_clk_set_rate,
+};
+
+static int ad9523_clk_register(struct iio_dev *indio_dev)
+{
+ struct ad9523_state *st = iio_priv(indio_dev);
+ const char *names[AD9523_NUM_CHAN] = { };
+ struct clk_hw_onecell_data *clk_data;
+ struct ad9523_config *cfg = &st->cfg;
+ struct device *dev = &st->spi->dev;
+ int ret;
+
+ clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, AD9523_NUM_CHAN),
+ GFP_KERNEL);
+ if (!clk_data)
+ return -ENOMEM;
+
+ clk_data->num = AD9523_NUM_CHAN;
+ for (unsigned int i = 0; i < AD9523_NUM_CHAN; i++)
+ clk_data->hws[i] = ERR_PTR(-ENOENT);
+
+ device_property_read_string_array(dev, "clock-output-names", names,
+ ARRAY_SIZE(names));
+
+ for (unsigned int i = 0; i < cfg->num_channels; i++) {
+ struct ad9523_channel_spec *chan = &cfg->channels[i];
+ unsigned int num = chan->channel_num;
+ struct ad9523_clk *clk = &st->clks[num];
+ struct clk_init_data init = { };
+
+ init.name = names[num];
+ if (!init.name) {
+ init.name = devm_kasprintf(dev, GFP_KERNEL, "%s_out%u",
+ dev_name(dev), num);
+ if (!init.name)
+ return -ENOMEM;
+ }
+
+ init.ops = &ad9523_clk_ops;
+ if (chan->always_on)
+ init.flags = CLK_IS_CRITICAL;
+
+ clk->indio_dev = indio_dev;
+ clk->chan = &st->ad9523_channels[i];
+ clk->hw.init = &init;
+
+ ret = devm_clk_hw_register(dev, &clk->hw);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to register output %u clock\n",
+ num);
+
+ clk_data->hws[num] = &clk->hw;
+ }
+
+ return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+}
+
static int ad9523_setup(struct iio_dev *indio_dev)
{
struct ad9523_state *st = iio_priv(indio_dev);
@@ -1296,6 +1429,7 @@ static int ad9523_parse_channels(struct device *dev, struct ad9523_config *cfg)
chan->divider_output_invert_en = true;

fwnode_property_read_string(child, "label", &chan->label);
+ chan->always_on = fwnode_property_read_bool(child, "always-on");
}

return 0;
@@ -1362,6 +1496,7 @@ static int ad9523_probe(struct spi_device *spi)
struct device *dev = &spi->dev;
struct iio_dev *indio_dev;
struct ad9523_state *st;
+ bool clk_provider;
int ret;

indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
@@ -1377,6 +1512,8 @@ static int ad9523_probe(struct spi_device *spi)
if (ret)
return dev_err_probe(dev, ret, "Failed to enable supplies\n");

+ clk_provider = device_property_present(dev, "#clock-cells");
+
ret = ad9523_parse_fw(dev, st);
if (ret)
return ret;
@@ -1407,12 +1544,23 @@ static int ad9523_probe(struct spi_device *spi)
indio_dev->info = &ad9523_info;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->channels = st->ad9523_channels;
- indio_dev->num_channels = st->cfg.num_channels;
+ /*
+ * When used as a clock provider, the outputs are controlled through
+ * the clock framework only.
+ */
+ if (!clk_provider)
+ indio_dev->num_channels = st->cfg.num_channels;

ret = ad9523_setup(indio_dev);
if (ret < 0)
return ret;

+ if (clk_provider) {
+ ret = ad9523_clk_register(indio_dev);
+ if (ret)
+ return ret;
+ }
+
ret = ad9523_sync(indio_dev);
if (ret < 0)
return ret;

--
2.56.0