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/slab.h>
11 #include <linux/sysfs.h>
12 #include <linux/spi/spi.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/gcd.h>
17 #include <linux/gpio/consumer.h>
18 #include <asm/div64.h>
19 #include <linux/clk.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/frequency/adf4350.h>
29 ADF4350_FREQ_RESOLUTION
,
33 struct adf4350_state
{
34 struct spi_device
*spi
;
35 struct regulator
*reg
;
36 struct gpio_desc
*lock_detect_gpiod
;
37 struct adf4350_platform_data
*pdata
;
40 unsigned long chspc
; /* Channel Spacing */
41 unsigned long fpfd
; /* Phase Frequency Detector */
42 unsigned long min_out_freq
;
46 unsigned r4_rf_div_sel
;
47 unsigned long regs
[6];
48 unsigned long regs_hw
[6];
49 unsigned long long freq_req
;
51 * DMA (thus cache coherency maintenance) requires the
52 * transfer buffers to live in their own cache lines.
54 __be32 val ____cacheline_aligned
;
57 static struct adf4350_platform_data default_pdata
= {
58 .channel_spacing
= 10000,
59 .r2_user_settings
= ADF4350_REG2_PD_POLARITY_POS
|
60 ADF4350_REG2_CHARGE_PUMP_CURR_uA(2500),
61 .r3_user_settings
= ADF4350_REG3_12BIT_CLKDIV_MODE(0),
62 .r4_user_settings
= ADF4350_REG4_OUTPUT_PWR(3) |
63 ADF4350_REG4_MUTE_TILL_LOCK_EN
,
66 static int adf4350_sync_config(struct adf4350_state
*st
)
68 int ret
, i
, doublebuf
= 0;
70 for (i
= ADF4350_REG5
; i
>= ADF4350_REG0
; i
--) {
71 if ((st
->regs_hw
[i
] != st
->regs
[i
]) ||
72 ((i
== ADF4350_REG0
) && doublebuf
)) {
80 st
->val
= cpu_to_be32(st
->regs
[i
] | i
);
81 ret
= spi_write(st
->spi
, &st
->val
, 4);
84 st
->regs_hw
[i
] = st
->regs
[i
];
85 dev_dbg(&st
->spi
->dev
, "[%d] 0x%X\n",
86 i
, (u32
)st
->regs
[i
] | i
);
92 static int adf4350_reg_access(struct iio_dev
*indio_dev
,
93 unsigned reg
, unsigned writeval
,
96 struct adf4350_state
*st
= iio_priv(indio_dev
);
99 if (reg
> ADF4350_REG5
)
102 mutex_lock(&indio_dev
->mlock
);
103 if (readval
== NULL
) {
104 st
->regs
[reg
] = writeval
& ~(BIT(0) | BIT(1) | BIT(2));
105 ret
= adf4350_sync_config(st
);
107 *readval
= st
->regs_hw
[reg
];
110 mutex_unlock(&indio_dev
->mlock
);
115 static int adf4350_tune_r_cnt(struct adf4350_state
*st
, unsigned short r_cnt
)
117 struct adf4350_platform_data
*pdata
= st
->pdata
;
121 st
->fpfd
= (st
->clkin
* (pdata
->ref_doubler_en
? 2 : 1)) /
122 (r_cnt
* (pdata
->ref_div2_en
? 2 : 1));
123 } while (st
->fpfd
> ADF4350_MAX_FREQ_PFD
);
128 static int adf4350_set_freq(struct adf4350_state
*st
, unsigned long long freq
)
130 struct adf4350_platform_data
*pdata
= st
->pdata
;
132 u32 div_gcd
, prescaler
, chspc
;
136 if (freq
> ADF4350_MAX_OUT_FREQ
|| freq
< st
->min_out_freq
)
139 if (freq
> ADF4350_MAX_FREQ_45_PRESC
) {
140 prescaler
= ADF4350_REG1_PRESCALER
;
147 st
->r4_rf_div_sel
= 0;
149 while (freq
< ADF4350_MIN_VCO_FREQ
) {
155 * Allow a predefined reference division factor
156 * if not set, compute our own
158 if (pdata
->ref_div_factor
)
159 r_cnt
= pdata
->ref_div_factor
- 1;
166 r_cnt
= adf4350_tune_r_cnt(st
, r_cnt
);
167 st
->r1_mod
= st
->fpfd
/ chspc
;
168 if (r_cnt
> ADF4350_MAX_R_CNT
) {
169 /* try higher spacing values */
173 } while ((st
->r1_mod
> ADF4350_MAX_MODULUS
) && r_cnt
);
174 } while (r_cnt
== 0);
176 tmp
= freq
* (u64
)st
->r1_mod
+ (st
->fpfd
>> 1);
177 do_div(tmp
, st
->fpfd
); /* Div round closest (n + d/2)/d */
178 st
->r0_fract
= do_div(tmp
, st
->r1_mod
);
180 } while (mdiv
> st
->r0_int
);
182 band_sel_div
= DIV_ROUND_UP(st
->fpfd
, ADF4350_MAX_BANDSEL_CLK
);
184 if (st
->r0_fract
&& st
->r1_mod
) {
185 div_gcd
= gcd(st
->r1_mod
, st
->r0_fract
);
186 st
->r1_mod
/= div_gcd
;
187 st
->r0_fract
/= div_gcd
;
193 dev_dbg(&st
->spi
->dev
, "VCO: %llu Hz, PFD %lu Hz\n"
194 "REF_DIV %d, R0_INT %d, R0_FRACT %d\n"
195 "R1_MOD %d, RF_DIV %d\nPRESCALER %s, BAND_SEL_DIV %d\n",
196 freq
, st
->fpfd
, r_cnt
, st
->r0_int
, st
->r0_fract
, st
->r1_mod
,
197 1 << st
->r4_rf_div_sel
, prescaler
? "8/9" : "4/5",
200 st
->regs
[ADF4350_REG0
] = ADF4350_REG0_INT(st
->r0_int
) |
201 ADF4350_REG0_FRACT(st
->r0_fract
);
203 st
->regs
[ADF4350_REG1
] = ADF4350_REG1_PHASE(1) |
204 ADF4350_REG1_MOD(st
->r1_mod
) |
207 st
->regs
[ADF4350_REG2
] =
208 ADF4350_REG2_10BIT_R_CNT(r_cnt
) |
209 ADF4350_REG2_DOUBLE_BUFF_EN
|
210 (pdata
->ref_doubler_en
? ADF4350_REG2_RMULT2_EN
: 0) |
211 (pdata
->ref_div2_en
? ADF4350_REG2_RDIV2_EN
: 0) |
212 (pdata
->r2_user_settings
& (ADF4350_REG2_PD_POLARITY_POS
|
213 ADF4350_REG2_LDP_6ns
| ADF4350_REG2_LDF_INT_N
|
214 ADF4350_REG2_CHARGE_PUMP_CURR_uA(5000) |
215 ADF4350_REG2_MUXOUT(0x7) | ADF4350_REG2_NOISE_MODE(0x3)));
217 st
->regs
[ADF4350_REG3
] = pdata
->r3_user_settings
&
218 (ADF4350_REG3_12BIT_CLKDIV(0xFFF) |
219 ADF4350_REG3_12BIT_CLKDIV_MODE(0x3) |
220 ADF4350_REG3_12BIT_CSR_EN
|
221 ADF4351_REG3_CHARGE_CANCELLATION_EN
|
222 ADF4351_REG3_ANTI_BACKLASH_3ns_EN
|
223 ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH
);
225 st
->regs
[ADF4350_REG4
] =
226 ADF4350_REG4_FEEDBACK_FUND
|
227 ADF4350_REG4_RF_DIV_SEL(st
->r4_rf_div_sel
) |
228 ADF4350_REG4_8BIT_BAND_SEL_CLKDIV(band_sel_div
) |
229 ADF4350_REG4_RF_OUT_EN
|
230 (pdata
->r4_user_settings
&
231 (ADF4350_REG4_OUTPUT_PWR(0x3) |
232 ADF4350_REG4_AUX_OUTPUT_PWR(0x3) |
233 ADF4350_REG4_AUX_OUTPUT_EN
|
234 ADF4350_REG4_AUX_OUTPUT_FUND
|
235 ADF4350_REG4_MUTE_TILL_LOCK_EN
));
237 st
->regs
[ADF4350_REG5
] = ADF4350_REG5_LD_PIN_MODE_DIGITAL
;
240 return adf4350_sync_config(st
);
243 static ssize_t
adf4350_write(struct iio_dev
*indio_dev
,
245 const struct iio_chan_spec
*chan
,
246 const char *buf
, size_t len
)
248 struct adf4350_state
*st
= iio_priv(indio_dev
);
249 unsigned long long readin
;
253 ret
= kstrtoull(buf
, 10, &readin
);
257 mutex_lock(&indio_dev
->mlock
);
258 switch ((u32
)private) {
260 ret
= adf4350_set_freq(st
, readin
);
262 case ADF4350_FREQ_REFIN
:
263 if (readin
> ADF4350_MAX_FREQ_REFIN
) {
269 tmp
= clk_round_rate(st
->clk
, readin
);
274 ret
= clk_set_rate(st
->clk
, tmp
);
279 ret
= adf4350_set_freq(st
, st
->freq_req
);
281 case ADF4350_FREQ_RESOLUTION
:
287 case ADF4350_PWRDOWN
:
289 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
291 st
->regs
[ADF4350_REG2
] &= ~ADF4350_REG2_POWER_DOWN_EN
;
293 adf4350_sync_config(st
);
298 mutex_unlock(&indio_dev
->mlock
);
300 return ret
? ret
: len
;
303 static ssize_t
adf4350_read(struct iio_dev
*indio_dev
,
305 const struct iio_chan_spec
*chan
,
308 struct adf4350_state
*st
= iio_priv(indio_dev
);
309 unsigned long long val
;
312 mutex_lock(&indio_dev
->mlock
);
313 switch ((u32
)private) {
315 val
= (u64
)((st
->r0_int
* st
->r1_mod
) + st
->r0_fract
) *
317 do_div(val
, st
->r1_mod
* (1 << st
->r4_rf_div_sel
));
318 /* PLL unlocked? return error */
319 if (st
->lock_detect_gpiod
)
320 if (!gpiod_get_value(st
->lock_detect_gpiod
)) {
321 dev_dbg(&st
->spi
->dev
, "PLL un-locked\n");
325 case ADF4350_FREQ_REFIN
:
327 st
->clkin
= clk_get_rate(st
->clk
);
331 case ADF4350_FREQ_RESOLUTION
:
334 case ADF4350_PWRDOWN
:
335 val
= !!(st
->regs
[ADF4350_REG2
] & ADF4350_REG2_POWER_DOWN_EN
);
341 mutex_unlock(&indio_dev
->mlock
);
343 return ret
< 0 ? ret
: sprintf(buf
, "%llu\n", val
);
346 #define _ADF4350_EXT_INFO(_name, _ident) { \
348 .read = adf4350_read, \
349 .write = adf4350_write, \
351 .shared = IIO_SEPARATE, \
354 static const struct iio_chan_spec_ext_info adf4350_ext_info
[] = {
355 /* Ideally we use IIO_CHAN_INFO_FREQUENCY, but there are
356 * values > 2^32 in order to support the entire frequency range
357 * in Hz. Using scale is a bit ugly.
359 _ADF4350_EXT_INFO("frequency", ADF4350_FREQ
),
360 _ADF4350_EXT_INFO("frequency_resolution", ADF4350_FREQ_RESOLUTION
),
361 _ADF4350_EXT_INFO("refin_frequency", ADF4350_FREQ_REFIN
),
362 _ADF4350_EXT_INFO("powerdown", ADF4350_PWRDOWN
),
366 static const struct iio_chan_spec adf4350_chan
= {
367 .type
= IIO_ALTVOLTAGE
,
370 .ext_info
= adf4350_ext_info
,
373 static const struct iio_info adf4350_info
= {
374 .debugfs_reg_access
= &adf4350_reg_access
,
378 static struct adf4350_platform_data
*adf4350_parse_dt(struct device
*dev
)
380 struct device_node
*np
= dev
->of_node
;
381 struct adf4350_platform_data
*pdata
;
384 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
388 snprintf(&pdata
->name
[0], SPI_NAME_SIZE
- 1, "%pOFn", np
);
391 of_property_read_u32(np
, "adi,channel-spacing", &tmp
);
392 pdata
->channel_spacing
= tmp
;
395 of_property_read_u32(np
, "adi,power-up-frequency", &tmp
);
396 pdata
->power_up_frequency
= tmp
;
399 of_property_read_u32(np
, "adi,reference-div-factor", &tmp
);
400 pdata
->ref_div_factor
= tmp
;
402 pdata
->ref_doubler_en
= of_property_read_bool(np
,
403 "adi,reference-doubler-enable");
404 pdata
->ref_div2_en
= of_property_read_bool(np
,
405 "adi,reference-div2-enable");
407 /* r2_user_settings */
408 pdata
->r2_user_settings
= of_property_read_bool(np
,
409 "adi,phase-detector-polarity-positive-enable") ?
410 ADF4350_REG2_PD_POLARITY_POS
: 0;
411 pdata
->r2_user_settings
|= of_property_read_bool(np
,
412 "adi,lock-detect-precision-6ns-enable") ?
413 ADF4350_REG2_LDP_6ns
: 0;
414 pdata
->r2_user_settings
|= of_property_read_bool(np
,
415 "adi,lock-detect-function-integer-n-enable") ?
416 ADF4350_REG2_LDF_INT_N
: 0;
419 of_property_read_u32(np
, "adi,charge-pump-current", &tmp
);
420 pdata
->r2_user_settings
|= ADF4350_REG2_CHARGE_PUMP_CURR_uA(tmp
);
423 of_property_read_u32(np
, "adi,muxout-select", &tmp
);
424 pdata
->r2_user_settings
|= ADF4350_REG2_MUXOUT(tmp
);
426 pdata
->r2_user_settings
|= of_property_read_bool(np
,
427 "adi,low-spur-mode-enable") ?
428 ADF4350_REG2_NOISE_MODE(0x3) : 0;
430 /* r3_user_settings */
432 pdata
->r3_user_settings
= of_property_read_bool(np
,
433 "adi,cycle-slip-reduction-enable") ?
434 ADF4350_REG3_12BIT_CSR_EN
: 0;
435 pdata
->r3_user_settings
|= of_property_read_bool(np
,
436 "adi,charge-cancellation-enable") ?
437 ADF4351_REG3_CHARGE_CANCELLATION_EN
: 0;
439 pdata
->r3_user_settings
|= of_property_read_bool(np
,
440 "adi,anti-backlash-3ns-enable") ?
441 ADF4351_REG3_ANTI_BACKLASH_3ns_EN
: 0;
442 pdata
->r3_user_settings
|= of_property_read_bool(np
,
443 "adi,band-select-clock-mode-high-enable") ?
444 ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH
: 0;
447 of_property_read_u32(np
, "adi,12bit-clk-divider", &tmp
);
448 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CLKDIV(tmp
);
451 of_property_read_u32(np
, "adi,clk-divider-mode", &tmp
);
452 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CLKDIV_MODE(tmp
);
454 /* r4_user_settings */
456 pdata
->r4_user_settings
= of_property_read_bool(np
,
457 "adi,aux-output-enable") ?
458 ADF4350_REG4_AUX_OUTPUT_EN
: 0;
459 pdata
->r4_user_settings
|= of_property_read_bool(np
,
460 "adi,aux-output-fundamental-enable") ?
461 ADF4350_REG4_AUX_OUTPUT_FUND
: 0;
462 pdata
->r4_user_settings
|= of_property_read_bool(np
,
463 "adi,mute-till-lock-enable") ?
464 ADF4350_REG4_MUTE_TILL_LOCK_EN
: 0;
467 of_property_read_u32(np
, "adi,output-power", &tmp
);
468 pdata
->r4_user_settings
|= ADF4350_REG4_OUTPUT_PWR(tmp
);
471 of_property_read_u32(np
, "adi,aux-output-power", &tmp
);
472 pdata
->r4_user_settings
|= ADF4350_REG4_AUX_OUTPUT_PWR(tmp
);
478 struct adf4350_platform_data
*adf4350_parse_dt(struct device
*dev
)
484 static int adf4350_probe(struct spi_device
*spi
)
486 struct adf4350_platform_data
*pdata
;
487 struct iio_dev
*indio_dev
;
488 struct adf4350_state
*st
;
489 struct clk
*clk
= NULL
;
492 if (spi
->dev
.of_node
) {
493 pdata
= adf4350_parse_dt(&spi
->dev
);
497 pdata
= spi
->dev
.platform_data
;
501 dev_warn(&spi
->dev
, "no platform data? using default\n");
502 pdata
= &default_pdata
;
506 clk
= devm_clk_get(&spi
->dev
, "clkin");
508 return -EPROBE_DEFER
;
510 ret
= clk_prepare_enable(clk
);
515 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
516 if (indio_dev
== NULL
) {
518 goto error_disable_clk
;
521 st
= iio_priv(indio_dev
);
523 st
->reg
= devm_regulator_get(&spi
->dev
, "vcc");
524 if (!IS_ERR(st
->reg
)) {
525 ret
= regulator_enable(st
->reg
);
527 goto error_disable_clk
;
530 spi_set_drvdata(spi
, indio_dev
);
534 indio_dev
->dev
.parent
= &spi
->dev
;
535 indio_dev
->name
= (pdata
->name
[0] != 0) ? pdata
->name
:
536 spi_get_device_id(spi
)->name
;
538 indio_dev
->info
= &adf4350_info
;
539 indio_dev
->modes
= INDIO_DIRECT_MODE
;
540 indio_dev
->channels
= &adf4350_chan
;
541 indio_dev
->num_channels
= 1;
543 st
->chspc
= pdata
->channel_spacing
;
546 st
->clkin
= clk_get_rate(clk
);
548 st
->clkin
= pdata
->clkin
;
551 st
->min_out_freq
= spi_get_device_id(spi
)->driver_data
== 4351 ?
552 ADF4351_MIN_OUT_FREQ
: ADF4350_MIN_OUT_FREQ
;
554 memset(st
->regs_hw
, 0xFF, sizeof(st
->regs_hw
));
556 st
->lock_detect_gpiod
= devm_gpiod_get_optional(&spi
->dev
, NULL
,
558 if (IS_ERR(st
->lock_detect_gpiod
))
559 return PTR_ERR(st
->lock_detect_gpiod
);
561 if (pdata
->power_up_frequency
) {
562 ret
= adf4350_set_freq(st
, pdata
->power_up_frequency
);
564 goto error_disable_reg
;
567 ret
= iio_device_register(indio_dev
);
569 goto error_disable_reg
;
574 if (!IS_ERR(st
->reg
))
575 regulator_disable(st
->reg
);
578 clk_disable_unprepare(clk
);
583 static int adf4350_remove(struct spi_device
*spi
)
585 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
586 struct adf4350_state
*st
= iio_priv(indio_dev
);
587 struct regulator
*reg
= st
->reg
;
589 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
590 adf4350_sync_config(st
);
592 iio_device_unregister(indio_dev
);
595 clk_disable_unprepare(st
->clk
);
598 regulator_disable(reg
);
603 static const struct of_device_id adf4350_of_match
[] = {
604 { .compatible
= "adi,adf4350", },
605 { .compatible
= "adi,adf4351", },
608 MODULE_DEVICE_TABLE(of
, adf4350_of_match
);
610 static const struct spi_device_id adf4350_id
[] = {
615 MODULE_DEVICE_TABLE(spi
, adf4350_id
);
617 static struct spi_driver adf4350_driver
= {
620 .of_match_table
= of_match_ptr(adf4350_of_match
),
622 .probe
= adf4350_probe
,
623 .remove
= adf4350_remove
,
624 .id_table
= adf4350_id
,
626 module_spi_driver(adf4350_driver
);
628 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
629 MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL");
630 MODULE_LICENSE("GPL v2");