[PATCH 13/16] iio: frequency: ad9523: add firmware support and drop platform data
From: Nuno Sá
Date: Mon Nov 30 2026 - 12:38:37 EST
The driver refuses to probe without platform data, and there are no
in-tree users of it. That means the driver can not be used at all on a
modern system.
Get the configuration from firmware properties instead, following the
new binding, and drop the platform data support:
- The OSC_IN (VCXO) frequency comes from the oscin clock.
- The PLL2 dividers are computed from the M1 (and optional M2) output
frequency. The VCO frequency follows from the M1 frequency, and the
feedback divider from the VCO frequency and the PFD frequency, given
by the reference divider and doubler.
- The charge pump current and the loop filter components are given in
physical units.
- Each output channel has a child node with its driver mode, polarity
and label.
While at it, get the supplies as named in the datasheet instead of the
single vcc supply. The option to override the IIO device name is gone,
the standard label property can be used instead.
Signed-off-by: Nuno Sá <nuno.sa@xxxxxxxxxx>
---
drivers/iio/frequency/ad9523.c | 341 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 321 insertions(+), 20 deletions(-)
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index dbf29797130b..f0caaae4d39a 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -5,8 +5,12 @@
* Copyright 2012 Analog Devices Inc.
*/
+#include <linux/bitops.h>
+#include <linux/clk.h>
#include <linux/device.h>
#include <linux/kernel.h>
+#include <linux/math64.h>
+#include <linux/property.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/spi/spi.h>
@@ -15,6 +19,7 @@
#include <linux/err.h>
#include <linux/module.h>
#include <linux/delay.h>
+#include <linux/units.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
@@ -237,6 +242,12 @@
#define AD9523_NUM_CHAN 14
+#define AD9523_VCO_FREQ_MIN (2940 * HZ_PER_MHZ)
+#define AD9523_VCO_FREQ_MAX (3100 * HZ_PER_MHZ)
+#define AD9523_PLL2_PFD_FREQ_MAX (259 * HZ_PER_MHZ)
+#define AD9523_PLL2_CHARGE_PUMP_NA_MIN 3500
+#define AD9523_PLL2_CHARGE_PUMP_NA_MAX (255 * 3500)
+
/* Helpers to avoid excess line breaks */
#define AD_IFE(_pde, _a, _b) ((cfg->_pde) ? _a : _b)
#define AD_IF(_pde, _a) AD_IFE(_pde, _a, 0)
@@ -265,14 +276,14 @@ enum outp_drv_mode {
* @channel_num: Output channel number.
* @divider_output_invert_en: Invert the polarity of the output clock.
* @driver_mode: Output driver mode (logic level family).
- * @extended_name: Optional descriptive channel name.
+ * @label: Optional channel label.
*/
struct ad9523_channel_spec {
unsigned int channel_num;
bool divider_output_invert_en;
enum outp_drv_mode driver_mode;
- char extended_name[16];
+ const char *label;
};
enum rpole2_resistor {
@@ -323,7 +334,6 @@ enum cpole1_capacitor {
* @rzero_bypass_en: PLL2 loop filter Rzero bypass enable.
* @num_channels: Array size of struct ad9523_channel_spec.
* @channels: Pointer to channel array.
- * @name: Optional alternative iio device name.
*/
struct ad9523_config {
@@ -356,8 +366,6 @@ struct ad9523_config {
/* Output Channel Configuration */
int num_channels;
struct ad9523_channel_spec *channels;
-
- char name[SPI_NAME_SIZE];
};
enum {
@@ -382,7 +390,7 @@ enum {
struct ad9523_state {
struct spi_device *spi;
- struct ad9523_config *cfg;
+ struct ad9523_config cfg;
struct iio_chan_spec ad9523_channels[AD9523_NUM_CHAN];
struct gpio_desc *pwrdown_gpio;
struct gpio_desc *reset_gpio;
@@ -883,7 +891,7 @@ static const struct iio_info ad9523_info = {
static int ad9523_setup(struct iio_dev *indio_dev)
{
struct ad9523_state *st = iio_priv(indio_dev);
- struct ad9523_config *cfg = st->cfg;
+ struct ad9523_config *cfg = &st->cfg;
struct ad9523_channel_spec *chan;
unsigned long active_mask = 0;
int ret, i;
@@ -1022,9 +1030,7 @@ static int ad9523_setup(struct iio_dev *indio_dev)
st->ad9523_channels[i].output = 1;
st->ad9523_channels[i].indexed = 1;
st->ad9523_channels[i].channel = chan->channel_num;
- if (chan->extended_name[0])
- st->labels[chan->channel_num] =
- chan->extended_name;
+ st->labels[chan->channel_num] = chan->label;
st->ad9523_channels[i].info_mask_separate =
BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_PHASE) |
@@ -1057,18 +1063,303 @@ static int ad9523_setup(struct iio_dev *indio_dev)
return 0;
}
+static bool ad9523_pll2_n_div_valid(u64 n)
+{
+ /* N = 4 * B + A, with the A and B counter constraints from Table 45 */
+ switch (n) {
+ case 12:
+ case 16 ... 17:
+ case 20 ... 22:
+ case 24 ... 26:
+ case 28 ... 255:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static int ad9523_pll2_calc_dividers(struct device *dev,
+ struct ad9523_config *cfg)
+{
+ u32 m1_freq, m2_freq = 0, r2 = 1;
+ u64 vco_freq, ref_freq, n, rem;
+ unsigned int m;
+ int ret;
+
+ ret = device_property_read_u32(dev, "adi,pll2-m1-frequency-hz", &m1_freq);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to read adi,pll2-m1-frequency-hz\n");
+
+ /*
+ * The M1 divider is 3, 4 or 5 and the resulting VCO frequency ranges
+ * do not overlap, so both are given by the M1 frequency.
+ */
+ for (m = 3; m <= 5; m++) {
+ vco_freq = (u64)m1_freq * m;
+ if (vco_freq >= AD9523_VCO_FREQ_MIN && vco_freq <= AD9523_VCO_FREQ_MAX)
+ break;
+ }
+
+ if (m > 5)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid M1 frequency: %u Hz\n", m1_freq);
+
+ cfg->pll2_vco_div_m1 = m;
+
+ /* If not given, M2 stays at 0, which powers it down */
+ device_property_read_u32(dev, "adi,pll2-m2-frequency-hz", &m2_freq);
+ if (m2_freq) {
+ n = div64_u64_rem(vco_freq, m2_freq, &rem);
+ if (rem || n < 3 || n > 5)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid M2 frequency: %u Hz\n",
+ m2_freq);
+
+ cfg->pll2_vco_div_m2 = n;
+ }
+
+ ref_freq = cfg->vcxo_freq;
+ if (device_property_read_bool(dev, "adi,pll2-reference-doubler-enable")) {
+ ref_freq *= 2;
+ /* The doubler output must not exceed the maximum PFD frequency */
+ if (ref_freq > AD9523_PLL2_PFD_FREQ_MAX)
+ return dev_err_probe(dev, -EINVAL,
+ "OSC_IN too fast for the doubler\n");
+
+ cfg->pll2_freq_doubler_en = true;
+ }
+
+ device_property_read_u32(dev, "adi,pll2-reference-div-factor", &r2);
+ if (!r2 || r2 > 31)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid PLL2 reference divider: %u\n", r2);
+
+ /* f_pfd = f_ref / R2 */
+ if (ref_freq > (u64)AD9523_PLL2_PFD_FREQ_MAX * r2)
+ return dev_err_probe(dev, -EINVAL,
+ "PLL2 PFD frequency too high\n");
+
+ /* PLL2 is integer-N: f_vco = f_pfd * N, so N = f_vco * R2 / f_ref */
+ n = div64_u64_rem(vco_freq * r2, ref_freq, &rem);
+ if (rem || !ad9523_pll2_n_div_valid(n))
+ return dev_err_probe(dev, -EINVAL,
+ "No valid PLL2 feedback divider for a %llu Hz VCO\n",
+ vco_freq);
+
+ cfg->pll2_r2_div = r2;
+ cfg->pll2_ndiv_a_cnt = n % 4;
+ cfg->pll2_ndiv_b_cnt = n / 4;
+
+ return 0;
+}
+
+static const u16 ad9523_rpole2_ohms[] = {
+ [RPOLE2_900_OHM] = 900,
+ [RPOLE2_450_OHM] = 450,
+ [RPOLE2_300_OHM] = 300,
+ [RPOLE2_225_OHM] = 225,
+};
+
+static const u16 ad9523_rzero_ohms[] = {
+ [RZERO_3250_OHM] = 3250,
+ [RZERO_2750_OHM] = 2750,
+ [RZERO_2250_OHM] = 2250,
+ [RZERO_2100_OHM] = 2100,
+ [RZERO_3000_OHM] = 3000,
+ [RZERO_2500_OHM] = 2500,
+ [RZERO_2000_OHM] = 2000,
+ [RZERO_1850_OHM] = 1850,
+};
+
+static const u16 ad9523_cpole1_pf[] = {
+ [CPOLE1_0_PF] = 0,
+ [CPOLE1_8_PF] = 8,
+ [CPOLE1_16_PF] = 16,
+ [CPOLE1_24_PF] = 24,
+ [_CPOLE1_24_PF] = 24,
+ [CPOLE1_32_PF] = 32,
+ [CPOLE1_40_PF] = 40,
+ [CPOLE1_48_PF] = 48,
+};
+
+static int ad9523_lookup_loop_filter(struct device *dev, const char *prop,
+ const u16 *table, unsigned int size,
+ u32 val)
+{
+ for (unsigned int i = 0; i < size; i++) {
+ if (table[i] == val)
+ return i;
+ }
+
+ return dev_err_probe(dev, -EINVAL, "Invalid %s: %u\n", prop, val);
+}
+
+static int ad9523_parse_loop_filter(struct device *dev,
+ struct ad9523_config *cfg)
+{
+ u32 rzero = 3250, rpole2 = 900, cpole1 = 0;
+ int ret;
+
+ device_property_read_u32(dev, "adi,pll2-loop-filter-rzero-ohms", &rzero);
+ device_property_read_u32(dev, "adi,pll2-loop-filter-rpole2-ohms", &rpole2);
+ device_property_read_u32(dev, "adi,pll2-loop-filter-cpole1-picofarads",
+ &cpole1);
+
+ /* 0 ohms means the internal zero resistor is bypassed */
+ if (!rzero) {
+ cfg->rzero_bypass_en = true;
+ } else {
+ ret = ad9523_lookup_loop_filter(dev, "PLL2 loop filter rzero",
+ ad9523_rzero_ohms,
+ ARRAY_SIZE(ad9523_rzero_ohms),
+ rzero);
+ if (ret < 0)
+ return ret;
+
+ cfg->rzero = ret;
+ }
+
+ ret = ad9523_lookup_loop_filter(dev, "PLL2 loop filter rpole2",
+ ad9523_rpole2_ohms,
+ ARRAY_SIZE(ad9523_rpole2_ohms), rpole2);
+ if (ret < 0)
+ return ret;
+
+ cfg->rpole2 = ret;
+
+ ret = ad9523_lookup_loop_filter(dev, "PLL2 loop filter cpole1",
+ ad9523_cpole1_pf,
+ ARRAY_SIZE(ad9523_cpole1_pf), cpole1);
+ if (ret < 0)
+ return ret;
+
+ cfg->cpole1 = ret;
+
+ return 0;
+}
+
+static int ad9523_parse_channels(struct device *dev, struct ad9523_config *cfg)
+{
+ /* In enum outp_drv_mode order, starting at LVPECL_8mA */
+ static const char * const drv_modes[] = {
+ "lvpecl-8ma", "lvds-3.5ma", "lvds-7ma", "hstl0-16ma", "hstl1-8ma",
+ "cmos-true-true", "cmos-true-complement", "cmos-true-hiz",
+ "cmos-hiz-true", "cmos-hiz-hiz", "cmos-complement-complement",
+ "cmos-complement-true", "cmos-complement-hiz",
+ "cmos-hiz-complement",
+ };
+ struct ad9523_channel_spec *chan;
+ unsigned long mask = 0;
+ unsigned int i = 0;
+ int ret;
+
+ cfg->num_channels = device_get_child_node_count(dev);
+ if (!cfg->num_channels)
+ return dev_err_probe(dev, -EINVAL, "No output channels\n");
+
+ cfg->channels = devm_kcalloc(dev, cfg->num_channels,
+ sizeof(*cfg->channels), GFP_KERNEL);
+ if (!cfg->channels)
+ return -ENOMEM;
+
+ device_for_each_child_node_scoped(dev, child) {
+ chan = &cfg->channels[i++];
+
+ ret = fwnode_property_read_u32(child, "reg", &chan->channel_num);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to read channel reg\n");
+
+ if (chan->channel_num >= AD9523_NUM_CHAN ||
+ __test_and_set_bit(chan->channel_num, &mask))
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid or duplicated channel %u\n",
+ chan->channel_num);
+
+ ret = fwnode_property_match_property_string(child,
+ "adi,driver-mode",
+ drv_modes,
+ ARRAY_SIZE(drv_modes));
+ if (ret < 0)
+ return dev_err_probe(dev, ret,
+ "Invalid or missing channel %u driver mode\n",
+ chan->channel_num);
+
+ chan->driver_mode = LVPECL_8mA + ret;
+
+ if (fwnode_property_read_bool(child, "adi,divider-output-invert-enable"))
+ chan->divider_output_invert_en = true;
+
+ fwnode_property_read_string(child, "label", &chan->label);
+ }
+
+ return 0;
+}
+
+static int ad9523_parse_fw(struct device *dev, struct ad9523_state *st)
+{
+ static const char * const osc_in_modes[] = {
+ "single-ended-positive", "single-ended-negative", "differential",
+ };
+ struct ad9523_config *cfg = &st->cfg;
+ struct clk *clk;
+ u32 val;
+ int ret;
+
+ clk = devm_clk_get_enabled(dev, "oscin");
+ if (IS_ERR(clk))
+ return dev_err_probe(dev, PTR_ERR(clk),
+ "Failed to get the oscin clock\n");
+
+ cfg->vcxo_freq = clk_get_rate(clk);
+ if (!cfg->vcxo_freq)
+ return dev_err_probe(dev, -EINVAL, "Invalid oscin frequency\n");
+
+ if (device_property_present(dev, "adi,oscin-mode")) {
+ ret = device_property_match_property_string(dev, "adi,oscin-mode",
+ osc_in_modes,
+ ARRAY_SIZE(osc_in_modes));
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Invalid adi,oscin-mode\n");
+
+ cfg->osc_in_cmos_neg_inp_en = ret == 1;
+ cfg->osc_in_diff_en = ret == 2;
+ }
+
+ ret = device_property_read_u32(dev, "adi,pll2-charge-pump-current-nanoamp",
+ &val);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to read adi,pll2-charge-pump-current-nanoamp\n");
+
+ if (val < AD9523_PLL2_CHARGE_PUMP_NA_MIN || val > AD9523_PLL2_CHARGE_PUMP_NA_MAX)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid PLL2 charge pump current: %u nA\n", val);
+
+ cfg->pll2_charge_pump_current_nA = val;
+
+ ret = ad9523_pll2_calc_dividers(dev, cfg);
+ if (ret)
+ return ret;
+
+ ret = ad9523_parse_loop_filter(dev, cfg);
+ if (ret)
+ return ret;
+
+ return ad9523_parse_channels(dev, cfg);
+}
+
static int ad9523_probe(struct spi_device *spi)
{
+ static const char * const supplies[] = {
+ "vdd3-pll", "vdd3-vco", "vdd3-ref", "vdd3-out", "vdd18-out",
+ };
struct device *dev = &spi->dev;
- struct ad9523_config *cfg;
struct iio_dev *indio_dev;
struct ad9523_state *st;
int ret;
- cfg = dev_get_platdata(dev);
- if (!cfg)
- return dev_err_probe(dev, -EINVAL, "no platform data?\n");
-
indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
if (indio_dev == NULL)
return -ENOMEM;
@@ -1077,7 +1368,12 @@ static int ad9523_probe(struct spi_device *spi)
mutex_init(&st->lock);
- ret = devm_regulator_get_enable(dev, "vcc");
+ ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(supplies),
+ supplies);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to enable supplies\n");
+
+ ret = ad9523_parse_fw(dev, st);
if (ret)
return ret;
@@ -1102,14 +1398,12 @@ static int ad9523_probe(struct spi_device *spi)
return PTR_ERR(st->sync_gpio);
st->spi = spi;
- st->cfg = cfg;
- indio_dev->name = (cfg->name[0] != 0) ? cfg->name :
- spi_get_device_id(spi)->name;
+ indio_dev->name = spi_get_device_id(spi)->name;
indio_dev->info = &ad9523_info;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->channels = st->ad9523_channels;
- indio_dev->num_channels = cfg->num_channels;
+ indio_dev->num_channels = st->cfg.num_channels;
ret = ad9523_setup(indio_dev);
if (ret < 0)
@@ -1124,9 +1418,16 @@ static const struct spi_device_id ad9523_id[] = {
};
MODULE_DEVICE_TABLE(spi, ad9523_id);
+static const struct of_device_id ad9523_of_match[] = {
+ { .compatible = "adi,ad9523-1" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, ad9523_of_match);
+
static struct spi_driver ad9523_driver = {
.driver = {
.name = "ad9523",
+ .of_match_table = ad9523_of_match,
},
.probe = ad9523_probe,
.id_table = ad9523_id,
--
2.56.0