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.h>
18 #include <asm/div64.h>
19 #include <linux/clk.h>
21 #include <linux/of_gpio.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/frequency/adf4350.h>
30 ADF4350_FREQ_RESOLUTION
,
34 struct adf4350_state
{
35 struct spi_device
*spi
;
36 struct regulator
*reg
;
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
,
64 .gpio_lock_detect
= -1,
67 static int adf4350_sync_config(struct adf4350_state
*st
)
69 int ret
, i
, doublebuf
= 0;
71 for (i
= ADF4350_REG5
; i
>= ADF4350_REG0
; i
--) {
72 if ((st
->regs_hw
[i
] != st
->regs
[i
]) ||
73 ((i
== ADF4350_REG0
) && doublebuf
)) {
81 st
->val
= cpu_to_be32(st
->regs
[i
] | i
);
82 ret
= spi_write(st
->spi
, &st
->val
, 4);
85 st
->regs_hw
[i
] = st
->regs
[i
];
86 dev_dbg(&st
->spi
->dev
, "[%d] 0x%X\n",
87 i
, (u32
)st
->regs
[i
] | i
);
93 static int adf4350_reg_access(struct iio_dev
*indio_dev
,
94 unsigned reg
, unsigned writeval
,
97 struct adf4350_state
*st
= iio_priv(indio_dev
);
100 if (reg
> ADF4350_REG5
)
103 mutex_lock(&indio_dev
->mlock
);
104 if (readval
== NULL
) {
105 st
->regs
[reg
] = writeval
& ~(BIT(0) | BIT(1) | BIT(2));
106 ret
= adf4350_sync_config(st
);
108 *readval
= st
->regs_hw
[reg
];
111 mutex_unlock(&indio_dev
->mlock
);
116 static int adf4350_tune_r_cnt(struct adf4350_state
*st
, unsigned short r_cnt
)
118 struct adf4350_platform_data
*pdata
= st
->pdata
;
122 st
->fpfd
= (st
->clkin
* (pdata
->ref_doubler_en
? 2 : 1)) /
123 (r_cnt
* (pdata
->ref_div2_en
? 2 : 1));
124 } while (st
->fpfd
> ADF4350_MAX_FREQ_PFD
);
129 static int adf4350_set_freq(struct adf4350_state
*st
, unsigned long long freq
)
131 struct adf4350_platform_data
*pdata
= st
->pdata
;
133 u32 div_gcd
, prescaler
, chspc
;
137 if (freq
> ADF4350_MAX_OUT_FREQ
|| freq
< st
->min_out_freq
)
140 if (freq
> ADF4350_MAX_FREQ_45_PRESC
) {
141 prescaler
= ADF4350_REG1_PRESCALER
;
148 st
->r4_rf_div_sel
= 0;
150 while (freq
< ADF4350_MIN_VCO_FREQ
) {
156 * Allow a predefined reference division factor
157 * if not set, compute our own
159 if (pdata
->ref_div_factor
)
160 r_cnt
= pdata
->ref_div_factor
- 1;
167 r_cnt
= adf4350_tune_r_cnt(st
, r_cnt
);
168 st
->r1_mod
= st
->fpfd
/ chspc
;
169 if (r_cnt
> ADF4350_MAX_R_CNT
) {
170 /* try higher spacing values */
174 } while ((st
->r1_mod
> ADF4350_MAX_MODULUS
) && r_cnt
);
175 } while (r_cnt
== 0);
177 tmp
= freq
* (u64
)st
->r1_mod
+ (st
->fpfd
>> 1);
178 do_div(tmp
, st
->fpfd
); /* Div round closest (n + d/2)/d */
179 st
->r0_fract
= do_div(tmp
, st
->r1_mod
);
181 } while (mdiv
> st
->r0_int
);
183 band_sel_div
= DIV_ROUND_UP(st
->fpfd
, ADF4350_MAX_BANDSEL_CLK
);
185 if (st
->r0_fract
&& st
->r1_mod
) {
186 div_gcd
= gcd(st
->r1_mod
, st
->r0_fract
);
187 st
->r1_mod
/= div_gcd
;
188 st
->r0_fract
/= div_gcd
;
194 dev_dbg(&st
->spi
->dev
, "VCO: %llu Hz, PFD %lu Hz\n"
195 "REF_DIV %d, R0_INT %d, R0_FRACT %d\n"
196 "R1_MOD %d, RF_DIV %d\nPRESCALER %s, BAND_SEL_DIV %d\n",
197 freq
, st
->fpfd
, r_cnt
, st
->r0_int
, st
->r0_fract
, st
->r1_mod
,
198 1 << st
->r4_rf_div_sel
, prescaler
? "8/9" : "4/5",
201 st
->regs
[ADF4350_REG0
] = ADF4350_REG0_INT(st
->r0_int
) |
202 ADF4350_REG0_FRACT(st
->r0_fract
);
204 st
->regs
[ADF4350_REG1
] = ADF4350_REG1_PHASE(1) |
205 ADF4350_REG1_MOD(st
->r1_mod
) |
208 st
->regs
[ADF4350_REG2
] =
209 ADF4350_REG2_10BIT_R_CNT(r_cnt
) |
210 ADF4350_REG2_DOUBLE_BUFF_EN
|
211 (pdata
->ref_doubler_en
? ADF4350_REG2_RMULT2_EN
: 0) |
212 (pdata
->ref_div2_en
? ADF4350_REG2_RDIV2_EN
: 0) |
213 (pdata
->r2_user_settings
& (ADF4350_REG2_PD_POLARITY_POS
|
214 ADF4350_REG2_LDP_6ns
| ADF4350_REG2_LDF_INT_N
|
215 ADF4350_REG2_CHARGE_PUMP_CURR_uA(5000) |
216 ADF4350_REG2_MUXOUT(0x7) | ADF4350_REG2_NOISE_MODE(0x3)));
218 st
->regs
[ADF4350_REG3
] = pdata
->r3_user_settings
&
219 (ADF4350_REG3_12BIT_CLKDIV(0xFFF) |
220 ADF4350_REG3_12BIT_CLKDIV_MODE(0x3) |
221 ADF4350_REG3_12BIT_CSR_EN
|
222 ADF4351_REG3_CHARGE_CANCELLATION_EN
|
223 ADF4351_REG3_ANTI_BACKLASH_3ns_EN
|
224 ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH
);
226 st
->regs
[ADF4350_REG4
] =
227 ADF4350_REG4_FEEDBACK_FUND
|
228 ADF4350_REG4_RF_DIV_SEL(st
->r4_rf_div_sel
) |
229 ADF4350_REG4_8BIT_BAND_SEL_CLKDIV(band_sel_div
) |
230 ADF4350_REG4_RF_OUT_EN
|
231 (pdata
->r4_user_settings
&
232 (ADF4350_REG4_OUTPUT_PWR(0x3) |
233 ADF4350_REG4_AUX_OUTPUT_PWR(0x3) |
234 ADF4350_REG4_AUX_OUTPUT_EN
|
235 ADF4350_REG4_AUX_OUTPUT_FUND
|
236 ADF4350_REG4_MUTE_TILL_LOCK_EN
));
238 st
->regs
[ADF4350_REG5
] = ADF4350_REG5_LD_PIN_MODE_DIGITAL
;
241 return adf4350_sync_config(st
);
244 static ssize_t
adf4350_write(struct iio_dev
*indio_dev
,
246 const struct iio_chan_spec
*chan
,
247 const char *buf
, size_t len
)
249 struct adf4350_state
*st
= iio_priv(indio_dev
);
250 unsigned long long readin
;
254 ret
= kstrtoull(buf
, 10, &readin
);
258 mutex_lock(&indio_dev
->mlock
);
259 switch ((u32
)private) {
261 ret
= adf4350_set_freq(st
, readin
);
263 case ADF4350_FREQ_REFIN
:
264 if (readin
> ADF4350_MAX_FREQ_REFIN
) {
270 tmp
= clk_round_rate(st
->clk
, readin
);
275 ret
= clk_set_rate(st
->clk
, tmp
);
280 ret
= adf4350_set_freq(st
, st
->freq_req
);
282 case ADF4350_FREQ_RESOLUTION
:
288 case ADF4350_PWRDOWN
:
290 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
292 st
->regs
[ADF4350_REG2
] &= ~ADF4350_REG2_POWER_DOWN_EN
;
294 adf4350_sync_config(st
);
299 mutex_unlock(&indio_dev
->mlock
);
301 return ret
? ret
: len
;
304 static ssize_t
adf4350_read(struct iio_dev
*indio_dev
,
306 const struct iio_chan_spec
*chan
,
309 struct adf4350_state
*st
= iio_priv(indio_dev
);
310 unsigned long long val
;
313 mutex_lock(&indio_dev
->mlock
);
314 switch ((u32
)private) {
316 val
= (u64
)((st
->r0_int
* st
->r1_mod
) + st
->r0_fract
) *
318 do_div(val
, st
->r1_mod
* (1 << st
->r4_rf_div_sel
));
319 /* PLL unlocked? return error */
320 if (gpio_is_valid(st
->pdata
->gpio_lock_detect
))
321 if (!gpio_get_value(st
->pdata
->gpio_lock_detect
)) {
322 dev_dbg(&st
->spi
->dev
, "PLL un-locked\n");
326 case ADF4350_FREQ_REFIN
:
328 st
->clkin
= clk_get_rate(st
->clk
);
332 case ADF4350_FREQ_RESOLUTION
:
335 case ADF4350_PWRDOWN
:
336 val
= !!(st
->regs
[ADF4350_REG2
] & ADF4350_REG2_POWER_DOWN_EN
);
342 mutex_unlock(&indio_dev
->mlock
);
344 return ret
< 0 ? ret
: sprintf(buf
, "%llu\n", val
);
347 #define _ADF4350_EXT_INFO(_name, _ident) { \
349 .read = adf4350_read, \
350 .write = adf4350_write, \
352 .shared = IIO_SEPARATE, \
355 static const struct iio_chan_spec_ext_info adf4350_ext_info
[] = {
356 /* Ideally we use IIO_CHAN_INFO_FREQUENCY, but there are
357 * values > 2^32 in order to support the entire frequency range
358 * in Hz. Using scale is a bit ugly.
360 _ADF4350_EXT_INFO("frequency", ADF4350_FREQ
),
361 _ADF4350_EXT_INFO("frequency_resolution", ADF4350_FREQ_RESOLUTION
),
362 _ADF4350_EXT_INFO("refin_frequency", ADF4350_FREQ_REFIN
),
363 _ADF4350_EXT_INFO("powerdown", ADF4350_PWRDOWN
),
367 static const struct iio_chan_spec adf4350_chan
= {
368 .type
= IIO_ALTVOLTAGE
,
371 .ext_info
= adf4350_ext_info
,
374 static const struct iio_info adf4350_info
= {
375 .debugfs_reg_access
= &adf4350_reg_access
,
379 static struct adf4350_platform_data
*adf4350_parse_dt(struct device
*dev
)
381 struct device_node
*np
= dev
->of_node
;
382 struct adf4350_platform_data
*pdata
;
386 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
390 snprintf(&pdata
->name
[0], SPI_NAME_SIZE
- 1, "%pOFn", np
);
393 of_property_read_u32(np
, "adi,channel-spacing", &tmp
);
394 pdata
->channel_spacing
= tmp
;
397 of_property_read_u32(np
, "adi,power-up-frequency", &tmp
);
398 pdata
->power_up_frequency
= tmp
;
401 of_property_read_u32(np
, "adi,reference-div-factor", &tmp
);
402 pdata
->ref_div_factor
= tmp
;
404 ret
= of_get_gpio(np
, 0);
406 pdata
->gpio_lock_detect
= -1;
408 pdata
->gpio_lock_detect
= ret
;
410 pdata
->ref_doubler_en
= of_property_read_bool(np
,
411 "adi,reference-doubler-enable");
412 pdata
->ref_div2_en
= of_property_read_bool(np
,
413 "adi,reference-div2-enable");
415 /* r2_user_settings */
416 pdata
->r2_user_settings
= of_property_read_bool(np
,
417 "adi,phase-detector-polarity-positive-enable") ?
418 ADF4350_REG2_PD_POLARITY_POS
: 0;
419 pdata
->r2_user_settings
|= of_property_read_bool(np
,
420 "adi,lock-detect-precision-6ns-enable") ?
421 ADF4350_REG2_LDP_6ns
: 0;
422 pdata
->r2_user_settings
|= of_property_read_bool(np
,
423 "adi,lock-detect-function-integer-n-enable") ?
424 ADF4350_REG2_LDF_INT_N
: 0;
427 of_property_read_u32(np
, "adi,charge-pump-current", &tmp
);
428 pdata
->r2_user_settings
|= ADF4350_REG2_CHARGE_PUMP_CURR_uA(tmp
);
431 of_property_read_u32(np
, "adi,muxout-select", &tmp
);
432 pdata
->r2_user_settings
|= ADF4350_REG2_MUXOUT(tmp
);
434 pdata
->r2_user_settings
|= of_property_read_bool(np
,
435 "adi,low-spur-mode-enable") ?
436 ADF4350_REG2_NOISE_MODE(0x3) : 0;
438 /* r3_user_settings */
440 pdata
->r3_user_settings
= of_property_read_bool(np
,
441 "adi,cycle-slip-reduction-enable") ?
442 ADF4350_REG3_12BIT_CSR_EN
: 0;
443 pdata
->r3_user_settings
|= of_property_read_bool(np
,
444 "adi,charge-cancellation-enable") ?
445 ADF4351_REG3_CHARGE_CANCELLATION_EN
: 0;
447 pdata
->r3_user_settings
|= of_property_read_bool(np
,
448 "adi,anti-backlash-3ns-enable") ?
449 ADF4351_REG3_ANTI_BACKLASH_3ns_EN
: 0;
450 pdata
->r3_user_settings
|= of_property_read_bool(np
,
451 "adi,band-select-clock-mode-high-enable") ?
452 ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH
: 0;
455 of_property_read_u32(np
, "adi,12bit-clk-divider", &tmp
);
456 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CLKDIV(tmp
);
459 of_property_read_u32(np
, "adi,clk-divider-mode", &tmp
);
460 pdata
->r3_user_settings
|= ADF4350_REG3_12BIT_CLKDIV_MODE(tmp
);
462 /* r4_user_settings */
464 pdata
->r4_user_settings
= of_property_read_bool(np
,
465 "adi,aux-output-enable") ?
466 ADF4350_REG4_AUX_OUTPUT_EN
: 0;
467 pdata
->r4_user_settings
|= of_property_read_bool(np
,
468 "adi,aux-output-fundamental-enable") ?
469 ADF4350_REG4_AUX_OUTPUT_FUND
: 0;
470 pdata
->r4_user_settings
|= of_property_read_bool(np
,
471 "adi,mute-till-lock-enable") ?
472 ADF4350_REG4_MUTE_TILL_LOCK_EN
: 0;
475 of_property_read_u32(np
, "adi,output-power", &tmp
);
476 pdata
->r4_user_settings
|= ADF4350_REG4_OUTPUT_PWR(tmp
);
479 of_property_read_u32(np
, "adi,aux-output-power", &tmp
);
480 pdata
->r4_user_settings
|= ADF4350_REG4_AUX_OUTPUT_PWR(tmp
);
486 struct adf4350_platform_data
*adf4350_parse_dt(struct device
*dev
)
492 static int adf4350_probe(struct spi_device
*spi
)
494 struct adf4350_platform_data
*pdata
;
495 struct iio_dev
*indio_dev
;
496 struct adf4350_state
*st
;
497 struct clk
*clk
= NULL
;
500 if (spi
->dev
.of_node
) {
501 pdata
= adf4350_parse_dt(&spi
->dev
);
505 pdata
= spi
->dev
.platform_data
;
509 dev_warn(&spi
->dev
, "no platform data? using default\n");
510 pdata
= &default_pdata
;
514 clk
= devm_clk_get(&spi
->dev
, "clkin");
516 return -EPROBE_DEFER
;
518 ret
= clk_prepare_enable(clk
);
523 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
524 if (indio_dev
== NULL
) {
526 goto error_disable_clk
;
529 st
= iio_priv(indio_dev
);
531 st
->reg
= devm_regulator_get(&spi
->dev
, "vcc");
532 if (!IS_ERR(st
->reg
)) {
533 ret
= regulator_enable(st
->reg
);
535 goto error_disable_clk
;
538 spi_set_drvdata(spi
, indio_dev
);
542 indio_dev
->dev
.parent
= &spi
->dev
;
543 indio_dev
->name
= (pdata
->name
[0] != 0) ? pdata
->name
:
544 spi_get_device_id(spi
)->name
;
546 indio_dev
->info
= &adf4350_info
;
547 indio_dev
->modes
= INDIO_DIRECT_MODE
;
548 indio_dev
->channels
= &adf4350_chan
;
549 indio_dev
->num_channels
= 1;
551 st
->chspc
= pdata
->channel_spacing
;
554 st
->clkin
= clk_get_rate(clk
);
556 st
->clkin
= pdata
->clkin
;
559 st
->min_out_freq
= spi_get_device_id(spi
)->driver_data
== 4351 ?
560 ADF4351_MIN_OUT_FREQ
: ADF4350_MIN_OUT_FREQ
;
562 memset(st
->regs_hw
, 0xFF, sizeof(st
->regs_hw
));
564 if (gpio_is_valid(pdata
->gpio_lock_detect
)) {
565 ret
= devm_gpio_request(&spi
->dev
, pdata
->gpio_lock_detect
,
568 dev_err(&spi
->dev
, "fail to request lock detect GPIO-%d",
569 pdata
->gpio_lock_detect
);
570 goto error_disable_reg
;
572 gpio_direction_input(pdata
->gpio_lock_detect
);
575 if (pdata
->power_up_frequency
) {
576 ret
= adf4350_set_freq(st
, pdata
->power_up_frequency
);
578 goto error_disable_reg
;
581 ret
= iio_device_register(indio_dev
);
583 goto error_disable_reg
;
588 if (!IS_ERR(st
->reg
))
589 regulator_disable(st
->reg
);
592 clk_disable_unprepare(clk
);
597 static int adf4350_remove(struct spi_device
*spi
)
599 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
600 struct adf4350_state
*st
= iio_priv(indio_dev
);
601 struct regulator
*reg
= st
->reg
;
603 st
->regs
[ADF4350_REG2
] |= ADF4350_REG2_POWER_DOWN_EN
;
604 adf4350_sync_config(st
);
606 iio_device_unregister(indio_dev
);
609 clk_disable_unprepare(st
->clk
);
612 regulator_disable(reg
);
617 static const struct of_device_id adf4350_of_match
[] = {
618 { .compatible
= "adi,adf4350", },
619 { .compatible
= "adi,adf4351", },
622 MODULE_DEVICE_TABLE(of
, adf4350_of_match
);
624 static const struct spi_device_id adf4350_id
[] = {
629 MODULE_DEVICE_TABLE(spi
, adf4350_id
);
631 static struct spi_driver adf4350_driver
= {
634 .of_match_table
= of_match_ptr(adf4350_of_match
),
636 .probe
= adf4350_probe
,
637 .remove
= adf4350_remove
,
638 .id_table
= adf4350_id
,
640 module_spi_driver(adf4350_driver
);
642 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
643 MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL");
644 MODULE_LICENSE("GPL v2");