1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADF4350/ADF4351 SPI Wideband Synthesizer driver
5 * Copyright 2012-2013 Analog Devices Inc.
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/spi/spi.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/err.h>
18 #include <linux/gcd.h>
19 #include <linux/gpio/consumer.h>
20 #include <asm/div64.h>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/frequency/adf4350.h>
31 ADF4350_FREQ_RESOLUTION
,
35 struct adf4350_state
{
36 struct spi_device
*spi
;
37 struct gpio_desc
*lock_detect_gpiod
;
38 struct adf4350_platform_data
*pdata
;
41 const char *clk_out_name
;
44 unsigned long chspc
; /* Channel Spacing */
45 unsigned long fpfd
; /* Phase Frequency Detector */
46 unsigned long min_out_freq
;
50 unsigned r4_rf_div_sel
;
51 unsigned long regs
[6];
52 unsigned long regs_hw
[6];
53 unsigned long long freq_req
;
55 * Lock to protect the state of the device from potential concurrent
56 * writes. The device is configured via a sequence of SPI writes,
57 * and this lock is meant to prevent the start of another sequence
58 * before another one has finished.
62 * DMA (thus cache coherency maintenance) may require that
63 * transfer buffers live in their own cache lines.
65 __be32 val
__aligned(IIO_DMA_MINALIGN
);
68 #define to_adf4350_state(_hw) container_of(_hw, struct adf4350_state, hw)
70 static struct adf4350_platform_data default_pdata
= {
71 .channel_spacing
= 10000,
72 .r2_user_settings
= ADF4350_REG2_PD_POLARITY_POS
|
73 ADF4350_REG2_CHARGE_PUMP_CURR_uA(2500),
74 .r3_user_settings
= ADF4350_REG3_12BIT_CLKDIV_MODE(0),
75 .r4_user_settings
= ADF4350_REG4_OUTPUT_PWR(3) |
76 ADF4350_REG4_MUTE_TILL_LOCK_EN
,
79 static int adf4350_sync_config(struct adf4350_state
*st
)
81 int ret
, i
, doublebuf
= 0;
83 for (i
= ADF4350_REG5
; i
>= ADF4350_REG0
; i
--) {
84 if ((st
->regs_hw
[i
] != st
->regs
[i
]) ||
85 ((i
== ADF4350_REG0
) && doublebuf
)) {
93 st
->val
= cpu_to_be32(st
->regs
[i
] | i
);
94 ret
= spi_write(st
->spi
, &st
->val
, 4);
97 st
->regs_hw
[i
] = st
->regs
[i
];
98 dev_dbg(&st
->spi
->dev
, "[%d] 0x%X\n",
99 i
, (u32
)st
->regs
[i
] | i
);
105 static int adf4350_reg_access(struct iio_dev
*indio_dev
,
106 unsigned reg
, unsigned writeval
,
109 struct adf4350_state
*st
= iio_priv(indio_dev
);
112 if (reg
> ADF4350_REG5
)
115 mutex_lock(&st
->lock
);
116 if (readval
== NULL
) {
117 st
->regs
[reg
] = writeval
& ~(BIT(0) | BIT(1) | BIT(2));
118 ret
= adf4350_sync_config(st
);
120 *readval
= st
->regs_hw
[reg
];
123 mutex_unlock(&st
->lock
);
128 static int adf4350_tune_r_cnt(struct adf4350_state
*st
, unsigned short r_cnt
)
130 struct adf4350_platform_data
*pdata
= st
->pdata
;
134 st
->fpfd
= (st
->clkin
* (pdata
->ref_doubler_en
? 2 : 1)) /
135 (r_cnt
* (pdata
->ref_div2_en
? 2 : 1));
136 } while (st
->fpfd
> ADF4350_MAX_FREQ_PFD
);
141 static int adf4350_set_freq(struct adf4350_state
*st
, unsigned long long freq
)
143 struct adf4350_platform_data
*pdata
= st
->pdata
;
145 u32 div_gcd
, prescaler
, chspc
;
149 if (freq
> ADF4350_MAX_OUT_FREQ
|| freq
< st
->min_out_freq
)
152 if (freq
> ADF4350_MAX_FREQ_45_PRESC
) {
153 prescaler
= ADF4350_REG1_PRESCALER
;
160 st
->r4_rf_div_sel
= 0;
162 while (freq
< ADF4350_MIN_VCO_FREQ
) {
168 * Allow a predefined reference division factor
169 * if not set, compute our own
171 if (pdata
->ref_div_factor
)
172 r_cnt
= pdata
->ref_div_factor
- 1;
179 r_cnt
= adf4350_tune_r_cnt(st
, r_cnt
);
180 st
->r1_mod
= st
->fpfd
/ chspc
;
181 if (r_cnt
> ADF4350_MAX_R_CNT
) {
182 /* try higher spacing values */
186 } while ((st
->r1_mod
> ADF4350_MAX_MODULUS
) && r_cnt
);
187 } while (r_cnt
== 0);
189 tmp
= freq
* (u64
)st
->r1_mod
+ (st
->fpfd
>> 1);
190 do_div(tmp
, st
->fpfd
); /* Div round closest (n + d/2)/d */
191 st
->r0_fract
= do_div(tmp
, st
->r1_mod
);
193 } while (mdiv
> st
->r0_int
);
195 band_sel_div
= DIV_ROUND_UP(st
->fpfd
, ADF4350_MAX_BANDSEL_CLK
);
197 if (st
->r0_fract
&& st
->r1_mod
) {
198 div_gcd
= gcd(st
->r1_mod
, st
->r0_fract
);
199 st
->r1_mod
/= div_gcd
;
200 st
->r0_fract
/= div_gcd
;
206 dev_dbg(&st
->spi
->dev
, "VCO: %llu Hz, PFD %lu Hz\n"
207 "REF_DIV %d, R0_INT %d, R0_FRACT %d\n"
208 "R1_MOD %d, RF_DIV %d\nPRESCALER %s, BAND_SEL_DIV %d\n",
209 freq
, st
->fpfd
, r_cnt
, st
->r0_int
, st
->r0_fract
, st
->r1_mod
,
210 1 << st
->r4_rf_div_sel
, prescaler
? "8/9" : "4/5",
213 st
->regs
[ADF4350_REG0
] = ADF4350_REG0_INT(st
->r0_int
) |
214 ADF4350_REG0_FRACT(st
->r0_fract
);
216 st
->regs
[ADF4350_REG1
] = ADF4350_REG1_PHASE(1) |
217 ADF4350_REG1_MOD(st
->r1_mod
) |
220 st
->regs
[ADF4350_REG2
] =
221 ADF4350_REG2_10BIT_R_CNT(r_cnt
) |
222 ADF4350_REG2_DOUBLE_BUFF_EN
|
223 (pdata
->ref_doubler_en
? ADF4350_REG2_RMULT2_EN
: 0) |
224 (pdata
->ref_div2_en
? ADF4350_REG2_RDIV2_EN
: 0) |
225 (pdata
->r2_user_settings
& (ADF4350_REG2_PD_POLARITY_POS
|
226 ADF4350_REG2_LDP_6ns
| ADF4350_REG2_LDF_INT_N
|
227 ADF4350_REG2_CHARGE_PUMP_CURR_uA(5000) |
228 ADF4350_REG2_MUXOUT(0x7) | ADF4350_REG2_NOISE_MODE(0x3)));
230 st
->regs
[ADF4350_REG3
] = pdata
->r3_user_settings
&
231 (ADF4350_REG3_12BIT_CLKDIV(0xFFF) |
232 ADF4350_REG3_12BIT_CLKDIV_MODE(0x3) |
233 ADF4350_REG3_12BIT_CSR_EN
|
234 ADF4351_REG3_CHARGE_CANCELLATION_EN
|
235 ADF4351_REG3_ANTI_BACKLASH_3ns_EN
|
236 ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH
);
238 st
->regs
[ADF4350_REG4
] =
239 ADF4350_REG4_FEEDBACK_FUND
|
240 ADF4350_REG4_RF_DIV_SEL(st
->r4_rf_div_sel
) |
241 ADF4350_REG4_8BIT_BAND_SEL_CLKDIV(band_sel_div
) |
242 ADF4350_REG4_RF_OUT_EN
|
243 (pdata
->r4_user_settings
&
244 (ADF4350_REG4_OUTPUT_PWR(0x3) |
245 ADF4350_REG4_AUX_OUTPUT_PWR(0x3) |
246 ADF4350_REG4_AUX_OUTPUT_EN
|
247 ADF4350_REG4_AUX_OUTPUT_FUND
|
248 ADF4350_REG4_MUTE_TILL_LOCK_EN
));
250 st
->regs
[ADF4350_REG5
] = ADF4350_REG5_LD_PIN_MODE_DIGITAL
;
253 return adf4350_sync_config(st
);
256 static ssize_t
adf4350_write(struct iio_dev
*indio_dev
,
258 const struct iio_chan_spec
*chan
,
259 const char *buf
, size_t len
)
261 struct adf4350_state
*st
= iio_priv(indio_dev
);
262 unsigned long long readin
;
266 ret
= kstrtoull(buf
, 10, &readin
);
270 mutex_lock(&st
->lock
);
271 switch ((u32
)private) {
273 ret
= adf4350_set_freq(st
, readin
);
275 case ADF4350_FREQ_REFIN
:
276 if (readin
> ADF4350_MAX_FREQ_REFIN
) {
282 tmp
= clk_round_rate(st
->clk
, readin
);
287 ret
= clk_set_rate(st
->clk
, tmp
);
292 ret
= adf4350_set_freq(st
, st
->freq_req
);
294 case ADF4350_FREQ_RESOLUTION
:
300 case ADF4350_PWRDOWN
:
302 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
304 st
->regs
[ADF4350_REG2
] &= ~ADF4350_REG2_POWER_DOWN_EN
;
306 adf4350_sync_config(st
);
311 mutex_unlock(&st
->lock
);
313 return ret
? ret
: len
;
316 static ssize_t
adf4350_read(struct iio_dev
*indio_dev
,
318 const struct iio_chan_spec
*chan
,
321 struct adf4350_state
*st
= iio_priv(indio_dev
);
322 unsigned long long val
;
325 mutex_lock(&st
->lock
);
326 switch ((u32
)private) {
328 val
= (u64
)((st
->r0_int
* st
->r1_mod
) + st
->r0_fract
) *
330 do_div(val
, st
->r1_mod
* (1 << st
->r4_rf_div_sel
));
331 /* PLL unlocked? return error */
332 if (st
->lock_detect_gpiod
)
333 if (!gpiod_get_value(st
->lock_detect_gpiod
)) {
334 dev_dbg(&st
->spi
->dev
, "PLL un-locked\n");
338 case ADF4350_FREQ_REFIN
:
340 st
->clkin
= clk_get_rate(st
->clk
);
344 case ADF4350_FREQ_RESOLUTION
:
347 case ADF4350_PWRDOWN
:
348 val
= !!(st
->regs
[ADF4350_REG2
] & ADF4350_REG2_POWER_DOWN_EN
);
354 mutex_unlock(&st
->lock
);
356 return ret
< 0 ? ret
: sprintf(buf
, "%llu\n", val
);
359 #define _ADF4350_EXT_INFO(_name, _ident) { \
361 .read = adf4350_read, \
362 .write = adf4350_write, \
364 .shared = IIO_SEPARATE, \
367 static const struct iio_chan_spec_ext_info adf4350_ext_info
[] = {
368 /* Ideally we use IIO_CHAN_INFO_FREQUENCY, but there are
369 * values > 2^32 in order to support the entire frequency range
370 * in Hz. Using scale is a bit ugly.
372 _ADF4350_EXT_INFO("frequency", ADF4350_FREQ
),
373 _ADF4350_EXT_INFO("frequency_resolution", ADF4350_FREQ_RESOLUTION
),
374 _ADF4350_EXT_INFO("refin_frequency", ADF4350_FREQ_REFIN
),
375 _ADF4350_EXT_INFO("powerdown", ADF4350_PWRDOWN
),
379 static const struct iio_chan_spec adf4350_chan
= {
380 .type
= IIO_ALTVOLTAGE
,
383 .ext_info
= adf4350_ext_info
,
386 static const struct iio_info adf4350_info
= {
387 .debugfs_reg_access
= &adf4350_reg_access
,
390 static void adf4350_clk_del_provider(void *data
)
392 struct adf4350_state
*st
= data
;
394 of_clk_del_provider(st
->spi
->dev
.of_node
);
397 static unsigned long adf4350_clk_recalc_rate(struct clk_hw
*hw
,
398 unsigned long parent_rate
)
400 struct adf4350_state
*st
= to_adf4350_state(hw
);
401 unsigned long long tmp
;
403 tmp
= (u64
)(st
->r0_int
* st
->r1_mod
+ st
->r0_fract
) * st
->fpfd
;
404 do_div(tmp
, st
->r1_mod
* (1 << st
->r4_rf_div_sel
));
409 static int adf4350_clk_set_rate(struct clk_hw
*hw
,
411 unsigned long parent_rate
)
413 struct adf4350_state
*st
= to_adf4350_state(hw
);
415 if (parent_rate
== 0 || parent_rate
> ADF4350_MAX_FREQ_REFIN
)
418 st
->clkin
= parent_rate
;
420 return adf4350_set_freq(st
, rate
);
423 static int adf4350_clk_prepare(struct clk_hw
*hw
)
425 struct adf4350_state
*st
= to_adf4350_state(hw
);
427 st
->regs
[ADF4350_REG2
] &= ~ADF4350_REG2_POWER_DOWN_EN
;
429 return adf4350_sync_config(st
);
432 static void adf4350_clk_unprepare(struct clk_hw
*hw
)
434 struct adf4350_state
*st
= to_adf4350_state(hw
);
436 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
438 adf4350_sync_config(st
);
441 static int adf4350_clk_is_enabled(struct clk_hw
*hw
)
443 struct adf4350_state
*st
= to_adf4350_state(hw
);
445 return (st
->regs
[ADF4350_REG2
] & ADF4350_REG2_POWER_DOWN_EN
);
448 static const struct clk_ops adf4350_clk_ops
= {
449 .recalc_rate
= adf4350_clk_recalc_rate
,
450 .set_rate
= adf4350_clk_set_rate
,
451 .prepare
= adf4350_clk_prepare
,
452 .unprepare
= adf4350_clk_unprepare
,
453 .is_enabled
= adf4350_clk_is_enabled
,
456 static int adf4350_clk_register(struct adf4350_state
*st
)
458 struct spi_device
*spi
= st
->spi
;
459 struct clk_init_data init
;
461 const char *parent_name
;
464 if (!device_property_present(&spi
->dev
, "#clock-cells"))
467 if (device_property_read_string(&spi
->dev
, "clock-output-names", &init
.name
)) {
468 init
.name
= devm_kasprintf(&spi
->dev
, GFP_KERNEL
, "%s-clk",
469 fwnode_get_name(dev_fwnode(&spi
->dev
)));
474 parent_name
= of_clk_get_parent_name(spi
->dev
.of_node
, 0);
478 init
.ops
= &adf4350_clk_ops
;
479 init
.parent_names
= &parent_name
;
480 init
.num_parents
= 1;
481 init
.flags
= CLK_SET_RATE_PARENT
;
484 clk
= devm_clk_register(&spi
->dev
, &st
->hw
);
488 ret
= of_clk_add_provider(spi
->dev
.of_node
, of_clk_src_simple_get
, clk
);
494 return devm_add_action_or_reset(&spi
->dev
, adf4350_clk_del_provider
, st
);
497 static struct adf4350_platform_data
*adf4350_parse_dt(struct device
*dev
)
499 struct adf4350_platform_data
*pdata
;
502 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
506 snprintf(pdata
->name
, sizeof(pdata
->name
), "%pfw", dev_fwnode(dev
));
509 device_property_read_u32(dev
, "adi,channel-spacing", &tmp
);
510 pdata
->channel_spacing
= tmp
;
513 device_property_read_u32(dev
, "adi,power-up-frequency", &tmp
);
514 pdata
->power_up_frequency
= tmp
;
517 device_property_read_u32(dev
, "adi,reference-div-factor", &tmp
);
518 pdata
->ref_div_factor
= tmp
;
520 pdata
->ref_doubler_en
= device_property_read_bool(dev
, "adi,reference-doubler-enable");
521 pdata
->ref_div2_en
= device_property_read_bool(dev
, "adi,reference-div2-enable");
523 /* r2_user_settings */
524 pdata
->r2_user_settings
= 0;
525 if (device_property_read_bool(dev
, "adi,phase-detector-polarity-positive-enable"))
526 pdata
->r2_user_settings
|= ADF4350_REG2_PD_POLARITY_POS
;
527 if (device_property_read_bool(dev
, "adi,lock-detect-precision-6ns-enable"))
528 pdata
->r2_user_settings
|= ADF4350_REG2_LDP_6ns
;
529 if (device_property_read_bool(dev
, "adi,lock-detect-function-integer-n-enable"))
530 pdata
->r2_user_settings
|= ADF4350_REG2_LDF_INT_N
;
533 device_property_read_u32(dev
, "adi,charge-pump-current", &tmp
);
534 pdata
->r2_user_settings
|= ADF4350_REG2_CHARGE_PUMP_CURR_uA(tmp
);
537 device_property_read_u32(dev
, "adi,muxout-select", &tmp
);
538 pdata
->r2_user_settings
|= ADF4350_REG2_MUXOUT(tmp
);
540 if (device_property_read_bool(dev
, "adi,low-spur-mode-enable"))
541 pdata
->r2_user_settings
|= ADF4350_REG2_NOISE_MODE(0x3);
543 /* r3_user_settings */
545 pdata
->r3_user_settings
= 0;
546 if (device_property_read_bool(dev
, "adi,cycle-slip-reduction-enable"))
547 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CSR_EN
;
548 if (device_property_read_bool(dev
, "adi,charge-cancellation-enable"))
549 pdata
->r3_user_settings
|= ADF4351_REG3_CHARGE_CANCELLATION_EN
;
550 if (device_property_read_bool(dev
, "adi,anti-backlash-3ns-enable"))
551 pdata
->r3_user_settings
|= ADF4351_REG3_ANTI_BACKLASH_3ns_EN
;
552 if (device_property_read_bool(dev
, "adi,band-select-clock-mode-high-enable"))
553 pdata
->r3_user_settings
|= ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH
;
556 device_property_read_u32(dev
, "adi,12bit-clk-divider", &tmp
);
557 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CLKDIV(tmp
);
560 device_property_read_u32(dev
, "adi,clk-divider-mode", &tmp
);
561 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CLKDIV_MODE(tmp
);
563 /* r4_user_settings */
565 pdata
->r4_user_settings
= 0;
566 if (device_property_read_bool(dev
, "adi,aux-output-enable"))
567 pdata
->r4_user_settings
|= ADF4350_REG4_AUX_OUTPUT_EN
;
568 if (device_property_read_bool(dev
, "adi,aux-output-fundamental-enable"))
569 pdata
->r4_user_settings
|= ADF4350_REG4_AUX_OUTPUT_FUND
;
570 if (device_property_read_bool(dev
, "adi,mute-till-lock-enable"))
571 pdata
->r4_user_settings
|= ADF4350_REG4_MUTE_TILL_LOCK_EN
;
574 device_property_read_u32(dev
, "adi,output-power", &tmp
);
575 pdata
->r4_user_settings
|= ADF4350_REG4_OUTPUT_PWR(tmp
);
578 device_property_read_u32(dev
, "adi,aux-output-power", &tmp
);
579 pdata
->r4_user_settings
|= ADF4350_REG4_AUX_OUTPUT_PWR(tmp
);
584 static void adf4350_power_down(void *data
)
586 struct iio_dev
*indio_dev
= data
;
587 struct adf4350_state
*st
= iio_priv(indio_dev
);
589 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
590 adf4350_sync_config(st
);
593 static int adf4350_probe(struct spi_device
*spi
)
595 struct adf4350_platform_data
*pdata
;
596 struct iio_dev
*indio_dev
;
597 struct adf4350_state
*st
;
598 struct clk
*clk
= NULL
;
601 if (dev_fwnode(&spi
->dev
)) {
602 pdata
= adf4350_parse_dt(&spi
->dev
);
606 pdata
= dev_get_platdata(&spi
->dev
);
610 dev_warn(&spi
->dev
, "no platform data? using default\n");
611 pdata
= &default_pdata
;
615 clk
= devm_clk_get_enabled(&spi
->dev
, "clkin");
620 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
621 if (indio_dev
== NULL
)
624 st
= iio_priv(indio_dev
);
626 ret
= devm_regulator_get_enable(&spi
->dev
, "vcc");
633 indio_dev
->name
= (pdata
->name
[0] != 0) ? pdata
->name
:
634 spi_get_device_id(spi
)->name
;
636 indio_dev
->info
= &adf4350_info
;
637 indio_dev
->modes
= INDIO_DIRECT_MODE
;
639 mutex_init(&st
->lock
);
641 st
->chspc
= pdata
->channel_spacing
;
644 st
->clkin
= clk_get_rate(clk
);
646 st
->clkin
= pdata
->clkin
;
649 st
->min_out_freq
= spi_get_device_id(spi
)->driver_data
== 4351 ?
650 ADF4351_MIN_OUT_FREQ
: ADF4350_MIN_OUT_FREQ
;
652 memset(st
->regs_hw
, 0xFF, sizeof(st
->regs_hw
));
654 st
->lock_detect_gpiod
= devm_gpiod_get_optional(&spi
->dev
, NULL
,
656 if (IS_ERR(st
->lock_detect_gpiod
))
657 return PTR_ERR(st
->lock_detect_gpiod
);
659 if (pdata
->power_up_frequency
) {
660 ret
= adf4350_set_freq(st
, pdata
->power_up_frequency
);
665 ret
= adf4350_clk_register(st
);
670 indio_dev
->channels
= &adf4350_chan
;
671 indio_dev
->num_channels
= 1;
674 ret
= devm_add_action_or_reset(&spi
->dev
, adf4350_power_down
, indio_dev
);
676 return dev_err_probe(&spi
->dev
, ret
,
677 "Failed to add action to managed power down\n");
679 return devm_iio_device_register(&spi
->dev
, indio_dev
);
682 static const struct of_device_id adf4350_of_match
[] = {
683 { .compatible
= "adi,adf4350", },
684 { .compatible
= "adi,adf4351", },
687 MODULE_DEVICE_TABLE(of
, adf4350_of_match
);
689 static const struct spi_device_id adf4350_id
[] = {
694 MODULE_DEVICE_TABLE(spi
, adf4350_id
);
696 static struct spi_driver adf4350_driver
= {
699 .of_match_table
= adf4350_of_match
,
701 .probe
= adf4350_probe
,
702 .id_table
= adf4350_id
,
704 module_spi_driver(adf4350_driver
);
706 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
707 MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL");
708 MODULE_LICENSE("GPL v2");