2 * clk-si5351.c: Silicon Laboratories Si5351A/B/C I2C Clock Generator
4 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
5 * Rabeeh Khoury <rabeeh@solid-run.com>
8 * [1] "Si5351A/B/C Data Sheet"
9 * http://www.silabs.com/Support%20Documents/TechnicalDocs/Si5351.pdf
10 * [2] "Manually Generating an Si5351 Register Map"
11 * http://www.silabs.com/Support%20Documents/TechnicalDocs/AN619.pdf
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/errno.h>
26 #include <linux/rational.h>
27 #include <linux/i2c.h>
28 #include <linux/of_platform.h>
29 #include <linux/platform_data/si5351.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <asm/div64.h>
35 #include "clk-si5351.h"
37 struct si5351_driver_data
;
39 struct si5351_parameters
{
46 struct si5351_hw_data
{
48 struct si5351_driver_data
*drvdata
;
49 struct si5351_parameters params
;
53 struct si5351_driver_data
{
54 enum si5351_variant variant
;
55 struct i2c_client
*client
;
56 struct regmap
*regmap
;
59 const char *pxtal_name
;
62 const char *pclkin_name
;
65 struct si5351_hw_data pll
[2];
66 struct si5351_hw_data
*msynth
;
67 struct si5351_hw_data
*clkout
;
71 static const char * const si5351_input_names
[] = {
74 static const char * const si5351_pll_names
[] = {
75 "si5351_plla", "si5351_pllb", "si5351_vxco"
77 static const char * const si5351_msynth_names
[] = {
78 "ms0", "ms1", "ms2", "ms3", "ms4", "ms5", "ms6", "ms7"
80 static const char * const si5351_clkout_names
[] = {
81 "clk0", "clk1", "clk2", "clk3", "clk4", "clk5", "clk6", "clk7"
87 static inline u8
si5351_reg_read(struct si5351_driver_data
*drvdata
, u8 reg
)
92 ret
= regmap_read(drvdata
->regmap
, reg
, &val
);
94 dev_err(&drvdata
->client
->dev
,
95 "unable to read from reg%02x\n", reg
);
102 static inline int si5351_bulk_read(struct si5351_driver_data
*drvdata
,
103 u8 reg
, u8 count
, u8
*buf
)
105 return regmap_bulk_read(drvdata
->regmap
, reg
, buf
, count
);
108 static inline int si5351_reg_write(struct si5351_driver_data
*drvdata
,
111 return regmap_write(drvdata
->regmap
, reg
, val
);
114 static inline int si5351_bulk_write(struct si5351_driver_data
*drvdata
,
115 u8 reg
, u8 count
, const u8
*buf
)
117 return regmap_raw_write(drvdata
->regmap
, reg
, buf
, count
);
120 static inline int si5351_set_bits(struct si5351_driver_data
*drvdata
,
121 u8 reg
, u8 mask
, u8 val
)
123 return regmap_update_bits(drvdata
->regmap
, reg
, mask
, val
);
126 static inline u8
si5351_msynth_params_address(int num
)
129 return SI5351_CLK6_PARAMETERS
+ (num
- 6);
130 return SI5351_CLK0_PARAMETERS
+ (SI5351_PARAMETERS_LENGTH
* num
);
133 static void si5351_read_parameters(struct si5351_driver_data
*drvdata
,
134 u8 reg
, struct si5351_parameters
*params
)
136 u8 buf
[SI5351_PARAMETERS_LENGTH
];
139 case SI5351_CLK6_PARAMETERS
:
140 case SI5351_CLK7_PARAMETERS
:
141 buf
[0] = si5351_reg_read(drvdata
, reg
);
147 si5351_bulk_read(drvdata
, reg
, SI5351_PARAMETERS_LENGTH
, buf
);
148 params
->p1
= ((buf
[2] & 0x03) << 16) | (buf
[3] << 8) | buf
[4];
149 params
->p2
= ((buf
[5] & 0x0f) << 16) | (buf
[6] << 8) | buf
[7];
150 params
->p3
= ((buf
[5] & 0xf0) << 12) | (buf
[0] << 8) | buf
[1];
155 static void si5351_write_parameters(struct si5351_driver_data
*drvdata
,
156 u8 reg
, struct si5351_parameters
*params
)
158 u8 buf
[SI5351_PARAMETERS_LENGTH
];
161 case SI5351_CLK6_PARAMETERS
:
162 case SI5351_CLK7_PARAMETERS
:
163 buf
[0] = params
->p1
& 0xff;
164 si5351_reg_write(drvdata
, reg
, buf
[0]);
167 buf
[0] = ((params
->p3
& 0x0ff00) >> 8) & 0xff;
168 buf
[1] = params
->p3
& 0xff;
169 /* save rdiv and divby4 */
170 buf
[2] = si5351_reg_read(drvdata
, reg
+ 2) & ~0x03;
171 buf
[2] |= ((params
->p1
& 0x30000) >> 16) & 0x03;
172 buf
[3] = ((params
->p1
& 0x0ff00) >> 8) & 0xff;
173 buf
[4] = params
->p1
& 0xff;
174 buf
[5] = ((params
->p3
& 0xf0000) >> 12) |
175 ((params
->p2
& 0xf0000) >> 16);
176 buf
[6] = ((params
->p2
& 0x0ff00) >> 8) & 0xff;
177 buf
[7] = params
->p2
& 0xff;
178 si5351_bulk_write(drvdata
, reg
, SI5351_PARAMETERS_LENGTH
, buf
);
182 static bool si5351_regmap_is_volatile(struct device
*dev
, unsigned int reg
)
185 case SI5351_DEVICE_STATUS
:
186 case SI5351_INTERRUPT_STATUS
:
187 case SI5351_PLL_RESET
:
193 static bool si5351_regmap_is_writeable(struct device
*dev
, unsigned int reg
)
195 /* reserved registers */
196 if (reg
>= 4 && reg
<= 8)
198 if (reg
>= 10 && reg
<= 14)
200 if (reg
>= 173 && reg
<= 176)
202 if (reg
>= 178 && reg
<= 182)
205 if (reg
== SI5351_DEVICE_STATUS
)
210 static const struct regmap_config si5351_regmap_config
= {
213 .cache_type
= REGCACHE_RBTREE
,
215 .writeable_reg
= si5351_regmap_is_writeable
,
216 .volatile_reg
= si5351_regmap_is_volatile
,
220 * Si5351 xtal clock input
222 static int si5351_xtal_prepare(struct clk_hw
*hw
)
224 struct si5351_driver_data
*drvdata
=
225 container_of(hw
, struct si5351_driver_data
, xtal
);
226 si5351_set_bits(drvdata
, SI5351_FANOUT_ENABLE
,
227 SI5351_XTAL_ENABLE
, SI5351_XTAL_ENABLE
);
231 static void si5351_xtal_unprepare(struct clk_hw
*hw
)
233 struct si5351_driver_data
*drvdata
=
234 container_of(hw
, struct si5351_driver_data
, xtal
);
235 si5351_set_bits(drvdata
, SI5351_FANOUT_ENABLE
,
236 SI5351_XTAL_ENABLE
, 0);
239 static const struct clk_ops si5351_xtal_ops
= {
240 .prepare
= si5351_xtal_prepare
,
241 .unprepare
= si5351_xtal_unprepare
,
245 * Si5351 clkin clock input (Si5351C only)
247 static int si5351_clkin_prepare(struct clk_hw
*hw
)
249 struct si5351_driver_data
*drvdata
=
250 container_of(hw
, struct si5351_driver_data
, clkin
);
251 si5351_set_bits(drvdata
, SI5351_FANOUT_ENABLE
,
252 SI5351_CLKIN_ENABLE
, SI5351_CLKIN_ENABLE
);
256 static void si5351_clkin_unprepare(struct clk_hw
*hw
)
258 struct si5351_driver_data
*drvdata
=
259 container_of(hw
, struct si5351_driver_data
, clkin
);
260 si5351_set_bits(drvdata
, SI5351_FANOUT_ENABLE
,
261 SI5351_CLKIN_ENABLE
, 0);
265 * CMOS clock source constraints:
266 * The input frequency range of the PLL is 10Mhz to 40MHz.
267 * If CLKIN is >40MHz, the input divider must be used.
269 static unsigned long si5351_clkin_recalc_rate(struct clk_hw
*hw
,
270 unsigned long parent_rate
)
272 struct si5351_driver_data
*drvdata
=
273 container_of(hw
, struct si5351_driver_data
, clkin
);
278 if (parent_rate
> 160000000) {
279 idiv
= SI5351_CLKIN_DIV_8
;
281 } else if (parent_rate
> 80000000) {
282 idiv
= SI5351_CLKIN_DIV_4
;
284 } else if (parent_rate
> 40000000) {
285 idiv
= SI5351_CLKIN_DIV_2
;
288 idiv
= SI5351_CLKIN_DIV_1
;
291 si5351_set_bits(drvdata
, SI5351_PLL_INPUT_SOURCE
,
292 SI5351_CLKIN_DIV_MASK
, idiv
);
294 dev_dbg(&drvdata
->client
->dev
, "%s - clkin div = %d, rate = %lu\n",
295 __func__
, (1 << (idiv
>> 6)), rate
);
300 static const struct clk_ops si5351_clkin_ops
= {
301 .prepare
= si5351_clkin_prepare
,
302 .unprepare
= si5351_clkin_unprepare
,
303 .recalc_rate
= si5351_clkin_recalc_rate
,
307 * Si5351 vxco clock input (Si5351B only)
310 static int si5351_vxco_prepare(struct clk_hw
*hw
)
312 struct si5351_hw_data
*hwdata
=
313 container_of(hw
, struct si5351_hw_data
, hw
);
315 dev_warn(&hwdata
->drvdata
->client
->dev
, "VXCO currently unsupported\n");
320 static void si5351_vxco_unprepare(struct clk_hw
*hw
)
324 static unsigned long si5351_vxco_recalc_rate(struct clk_hw
*hw
,
325 unsigned long parent_rate
)
330 static int si5351_vxco_set_rate(struct clk_hw
*hw
, unsigned long rate
,
331 unsigned long parent
)
336 static const struct clk_ops si5351_vxco_ops
= {
337 .prepare
= si5351_vxco_prepare
,
338 .unprepare
= si5351_vxco_unprepare
,
339 .recalc_rate
= si5351_vxco_recalc_rate
,
340 .set_rate
= si5351_vxco_set_rate
,
346 * Feedback Multisynth Divider Equations [2]
348 * fVCO = fIN * (a + b/c)
350 * with 15 + 0/1048575 <= (a + b/c) <= 90 + 0/1048575 and
351 * fIN = fXTAL or fIN = fCLKIN/CLKIN_DIV
353 * Feedback Multisynth Register Equations
355 * (1) MSNx_P1[17:0] = 128 * a + floor(128 * b/c) - 512
356 * (2) MSNx_P2[19:0] = 128 * b - c * floor(128 * b/c) = (128*b) mod c
357 * (3) MSNx_P3[19:0] = c
359 * Transposing (2) yields: (4) floor(128 * b/c) = (128 * b / MSNx_P2)/c
361 * Using (4) on (1) yields:
362 * MSNx_P1 = 128 * a + (128 * b/MSNx_P2)/c - 512
363 * MSNx_P1 + 512 + MSNx_P2/c = 128 * a + 128 * b/c
365 * a + b/c = (MSNx_P1 + MSNx_P2/MSNx_P3 + 512)/128
366 * = (MSNx_P1*MSNx_P3 + MSNx_P2 + 512*MSNx_P3)/(128*MSNx_P3)
369 static int _si5351_pll_reparent(struct si5351_driver_data
*drvdata
,
370 int num
, enum si5351_pll_src parent
)
372 u8 mask
= (num
== 0) ? SI5351_PLLA_SOURCE
: SI5351_PLLB_SOURCE
;
374 if (parent
== SI5351_PLL_SRC_DEFAULT
)
380 if (drvdata
->variant
!= SI5351_VARIANT_C
&&
381 parent
!= SI5351_PLL_SRC_XTAL
)
384 si5351_set_bits(drvdata
, SI5351_PLL_INPUT_SOURCE
, mask
,
385 (parent
== SI5351_PLL_SRC_XTAL
) ? 0 : mask
);
389 static unsigned char si5351_pll_get_parent(struct clk_hw
*hw
)
391 struct si5351_hw_data
*hwdata
=
392 container_of(hw
, struct si5351_hw_data
, hw
);
393 u8 mask
= (hwdata
->num
== 0) ? SI5351_PLLA_SOURCE
: SI5351_PLLB_SOURCE
;
396 val
= si5351_reg_read(hwdata
->drvdata
, SI5351_PLL_INPUT_SOURCE
);
398 return (val
& mask
) ? 1 : 0;
401 static int si5351_pll_set_parent(struct clk_hw
*hw
, u8 index
)
403 struct si5351_hw_data
*hwdata
=
404 container_of(hw
, struct si5351_hw_data
, hw
);
406 if (hwdata
->drvdata
->variant
!= SI5351_VARIANT_C
&&
413 return _si5351_pll_reparent(hwdata
->drvdata
, hwdata
->num
,
414 (index
== 0) ? SI5351_PLL_SRC_XTAL
:
415 SI5351_PLL_SRC_CLKIN
);
418 static unsigned long si5351_pll_recalc_rate(struct clk_hw
*hw
,
419 unsigned long parent_rate
)
421 struct si5351_hw_data
*hwdata
=
422 container_of(hw
, struct si5351_hw_data
, hw
);
423 u8 reg
= (hwdata
->num
== 0) ? SI5351_PLLA_PARAMETERS
:
424 SI5351_PLLB_PARAMETERS
;
425 unsigned long long rate
;
427 if (!hwdata
->params
.valid
)
428 si5351_read_parameters(hwdata
->drvdata
, reg
, &hwdata
->params
);
430 if (hwdata
->params
.p3
== 0)
433 /* fVCO = fIN * (P1*P3 + 512*P3 + P2)/(128*P3) */
434 rate
= hwdata
->params
.p1
* hwdata
->params
.p3
;
435 rate
+= 512 * hwdata
->params
.p3
;
436 rate
+= hwdata
->params
.p2
;
438 do_div(rate
, 128 * hwdata
->params
.p3
);
440 dev_dbg(&hwdata
->drvdata
->client
->dev
,
441 "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, parent_rate = %lu, rate = %lu\n",
442 __func__
, clk_hw_get_name(hw
),
443 hwdata
->params
.p1
, hwdata
->params
.p2
, hwdata
->params
.p3
,
444 parent_rate
, (unsigned long)rate
);
446 return (unsigned long)rate
;
449 static long si5351_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
450 unsigned long *parent_rate
)
452 struct si5351_hw_data
*hwdata
=
453 container_of(hw
, struct si5351_hw_data
, hw
);
454 unsigned long rfrac
, denom
, a
, b
, c
;
455 unsigned long long lltmp
;
457 if (rate
< SI5351_PLL_VCO_MIN
)
458 rate
= SI5351_PLL_VCO_MIN
;
459 if (rate
> SI5351_PLL_VCO_MAX
)
460 rate
= SI5351_PLL_VCO_MAX
;
462 /* determine integer part of feedback equation */
463 a
= rate
/ *parent_rate
;
465 if (a
< SI5351_PLL_A_MIN
)
466 rate
= *parent_rate
* SI5351_PLL_A_MIN
;
467 if (a
> SI5351_PLL_A_MAX
)
468 rate
= *parent_rate
* SI5351_PLL_A_MAX
;
470 /* find best approximation for b/c = fVCO mod fIN */
472 lltmp
= rate
% (*parent_rate
);
474 do_div(lltmp
, *parent_rate
);
475 rfrac
= (unsigned long)lltmp
;
480 rational_best_approximation(rfrac
, denom
,
481 SI5351_PLL_B_MAX
, SI5351_PLL_C_MAX
, &b
, &c
);
483 /* calculate parameters */
484 hwdata
->params
.p3
= c
;
485 hwdata
->params
.p2
= (128 * b
) % c
;
486 hwdata
->params
.p1
= 128 * a
;
487 hwdata
->params
.p1
+= (128 * b
/ c
);
488 hwdata
->params
.p1
-= 512;
490 /* recalculate rate by fIN * (a + b/c) */
491 lltmp
= *parent_rate
;
495 rate
= (unsigned long)lltmp
;
496 rate
+= *parent_rate
* a
;
498 dev_dbg(&hwdata
->drvdata
->client
->dev
,
499 "%s - %s: a = %lu, b = %lu, c = %lu, parent_rate = %lu, rate = %lu\n",
500 __func__
, clk_hw_get_name(hw
), a
, b
, c
,
506 static int si5351_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
507 unsigned long parent_rate
)
509 struct si5351_hw_data
*hwdata
=
510 container_of(hw
, struct si5351_hw_data
, hw
);
511 u8 reg
= (hwdata
->num
== 0) ? SI5351_PLLA_PARAMETERS
:
512 SI5351_PLLB_PARAMETERS
;
514 /* write multisynth parameters */
515 si5351_write_parameters(hwdata
->drvdata
, reg
, &hwdata
->params
);
517 /* plla/pllb ctrl is in clk6/clk7 ctrl registers */
518 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK6_CTRL
+ hwdata
->num
,
519 SI5351_CLK_INTEGER_MODE
,
520 (hwdata
->params
.p2
== 0) ? SI5351_CLK_INTEGER_MODE
: 0);
522 /* Do a pll soft reset on the affected pll */
523 si5351_reg_write(hwdata
->drvdata
, SI5351_PLL_RESET
,
524 hwdata
->num
== 0 ? SI5351_PLL_RESET_A
:
527 dev_dbg(&hwdata
->drvdata
->client
->dev
,
528 "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, parent_rate = %lu, rate = %lu\n",
529 __func__
, clk_hw_get_name(hw
),
530 hwdata
->params
.p1
, hwdata
->params
.p2
, hwdata
->params
.p3
,
536 static const struct clk_ops si5351_pll_ops
= {
537 .set_parent
= si5351_pll_set_parent
,
538 .get_parent
= si5351_pll_get_parent
,
539 .recalc_rate
= si5351_pll_recalc_rate
,
540 .round_rate
= si5351_pll_round_rate
,
541 .set_rate
= si5351_pll_set_rate
,
545 * Si5351 multisync divider
547 * for fOUT <= 150 MHz:
549 * fOUT = (fIN * (a + b/c)) / CLKOUTDIV
551 * with 6 + 0/1048575 <= (a + b/c) <= 1800 + 0/1048575 and
554 * Output Clock Multisynth Register Equations
556 * MSx_P1[17:0] = 128 * a + floor(128 * b/c) - 512
557 * MSx_P2[19:0] = 128 * b - c * floor(128 * b/c) = (128*b) mod c
560 * MS[6,7] are integer (P1) divide only, P1 = divide value,
561 * P2 and P3 are not applicable
563 * for 150MHz < fOUT <= 160MHz:
565 * MSx_P1 = 0, MSx_P2 = 0, MSx_P3 = 1, MSx_INT = 1, MSx_DIVBY4 = 11b
567 static int _si5351_msynth_reparent(struct si5351_driver_data
*drvdata
,
568 int num
, enum si5351_multisynth_src parent
)
570 if (parent
== SI5351_MULTISYNTH_SRC_DEFAULT
)
576 si5351_set_bits(drvdata
, SI5351_CLK0_CTRL
+ num
, SI5351_CLK_PLL_SELECT
,
577 (parent
== SI5351_MULTISYNTH_SRC_VCO0
) ? 0 :
578 SI5351_CLK_PLL_SELECT
);
582 static unsigned char si5351_msynth_get_parent(struct clk_hw
*hw
)
584 struct si5351_hw_data
*hwdata
=
585 container_of(hw
, struct si5351_hw_data
, hw
);
588 val
= si5351_reg_read(hwdata
->drvdata
, SI5351_CLK0_CTRL
+ hwdata
->num
);
590 return (val
& SI5351_CLK_PLL_SELECT
) ? 1 : 0;
593 static int si5351_msynth_set_parent(struct clk_hw
*hw
, u8 index
)
595 struct si5351_hw_data
*hwdata
=
596 container_of(hw
, struct si5351_hw_data
, hw
);
598 return _si5351_msynth_reparent(hwdata
->drvdata
, hwdata
->num
,
599 (index
== 0) ? SI5351_MULTISYNTH_SRC_VCO0
:
600 SI5351_MULTISYNTH_SRC_VCO1
);
603 static unsigned long si5351_msynth_recalc_rate(struct clk_hw
*hw
,
604 unsigned long parent_rate
)
606 struct si5351_hw_data
*hwdata
=
607 container_of(hw
, struct si5351_hw_data
, hw
);
608 u8 reg
= si5351_msynth_params_address(hwdata
->num
);
609 unsigned long long rate
;
612 if (!hwdata
->params
.valid
)
613 si5351_read_parameters(hwdata
->drvdata
, reg
, &hwdata
->params
);
616 * multisync0-5: fOUT = (128 * P3 * fIN) / (P1*P3 + P2 + 512*P3)
617 * multisync6-7: fOUT = fIN / P1
620 if (hwdata
->num
> 5) {
621 m
= hwdata
->params
.p1
;
622 } else if (hwdata
->params
.p3
== 0) {
624 } else if ((si5351_reg_read(hwdata
->drvdata
, reg
+ 2) &
625 SI5351_OUTPUT_CLK_DIVBY4
) == SI5351_OUTPUT_CLK_DIVBY4
) {
628 rate
*= 128 * hwdata
->params
.p3
;
629 m
= hwdata
->params
.p1
* hwdata
->params
.p3
;
630 m
+= hwdata
->params
.p2
;
631 m
+= 512 * hwdata
->params
.p3
;
638 dev_dbg(&hwdata
->drvdata
->client
->dev
,
639 "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, m = %lu, parent_rate = %lu, rate = %lu\n",
640 __func__
, clk_hw_get_name(hw
),
641 hwdata
->params
.p1
, hwdata
->params
.p2
, hwdata
->params
.p3
,
642 m
, parent_rate
, (unsigned long)rate
);
644 return (unsigned long)rate
;
647 static long si5351_msynth_round_rate(struct clk_hw
*hw
, unsigned long rate
,
648 unsigned long *parent_rate
)
650 struct si5351_hw_data
*hwdata
=
651 container_of(hw
, struct si5351_hw_data
, hw
);
652 unsigned long long lltmp
;
653 unsigned long a
, b
, c
;
656 /* multisync6-7 can only handle freqencies < 150MHz */
657 if (hwdata
->num
>= 6 && rate
> SI5351_MULTISYNTH67_MAX_FREQ
)
658 rate
= SI5351_MULTISYNTH67_MAX_FREQ
;
660 /* multisync frequency is 1MHz .. 160MHz */
661 if (rate
> SI5351_MULTISYNTH_MAX_FREQ
)
662 rate
= SI5351_MULTISYNTH_MAX_FREQ
;
663 if (rate
< SI5351_MULTISYNTH_MIN_FREQ
)
664 rate
= SI5351_MULTISYNTH_MIN_FREQ
;
667 if (rate
> SI5351_MULTISYNTH_DIVBY4_FREQ
)
670 /* multisync can set pll */
671 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
) {
673 * find largest integer divider for max
674 * vco frequency and given target rate
677 lltmp
= SI5351_PLL_VCO_MAX
;
679 a
= (unsigned long)lltmp
;
686 *parent_rate
= a
* rate
;
687 } else if (hwdata
->num
>= 6) {
688 /* determine the closest integer divider */
689 a
= DIV_ROUND_CLOSEST(*parent_rate
, rate
);
690 if (a
< SI5351_MULTISYNTH_A_MIN
)
691 a
= SI5351_MULTISYNTH_A_MIN
;
692 if (a
> SI5351_MULTISYNTH67_A_MAX
)
693 a
= SI5351_MULTISYNTH67_A_MAX
;
698 unsigned long rfrac
, denom
;
702 rate
= SI5351_MULTISYNTH_DIVBY4_FREQ
;
706 /* determine integer part of divider equation */
707 a
= *parent_rate
/ rate
;
708 if (a
< SI5351_MULTISYNTH_A_MIN
)
709 a
= SI5351_MULTISYNTH_A_MIN
;
710 if (a
> SI5351_MULTISYNTH_A_MAX
)
711 a
= SI5351_MULTISYNTH_A_MAX
;
713 /* find best approximation for b/c = fVCO mod fOUT */
715 lltmp
= (*parent_rate
) % rate
;
718 rfrac
= (unsigned long)lltmp
;
723 rational_best_approximation(rfrac
, denom
,
724 SI5351_MULTISYNTH_B_MAX
, SI5351_MULTISYNTH_C_MAX
,
728 /* recalculate rate by fOUT = fIN / (a + b/c) */
729 lltmp
= *parent_rate
;
731 do_div(lltmp
, a
* c
+ b
);
732 rate
= (unsigned long)lltmp
;
734 /* calculate parameters */
736 hwdata
->params
.p3
= 1;
737 hwdata
->params
.p2
= 0;
738 hwdata
->params
.p1
= 0;
739 } else if (hwdata
->num
>= 6) {
740 hwdata
->params
.p3
= 0;
741 hwdata
->params
.p2
= 0;
742 hwdata
->params
.p1
= a
;
744 hwdata
->params
.p3
= c
;
745 hwdata
->params
.p2
= (128 * b
) % c
;
746 hwdata
->params
.p1
= 128 * a
;
747 hwdata
->params
.p1
+= (128 * b
/ c
);
748 hwdata
->params
.p1
-= 512;
751 dev_dbg(&hwdata
->drvdata
->client
->dev
,
752 "%s - %s: a = %lu, b = %lu, c = %lu, divby4 = %d, parent_rate = %lu, rate = %lu\n",
753 __func__
, clk_hw_get_name(hw
), a
, b
, c
, divby4
,
759 static int si5351_msynth_set_rate(struct clk_hw
*hw
, unsigned long rate
,
760 unsigned long parent_rate
)
762 struct si5351_hw_data
*hwdata
=
763 container_of(hw
, struct si5351_hw_data
, hw
);
764 u8 reg
= si5351_msynth_params_address(hwdata
->num
);
767 /* write multisynth parameters */
768 si5351_write_parameters(hwdata
->drvdata
, reg
, &hwdata
->params
);
770 if (rate
> SI5351_MULTISYNTH_DIVBY4_FREQ
)
773 /* enable/disable integer mode and divby4 on multisynth0-5 */
774 if (hwdata
->num
< 6) {
775 si5351_set_bits(hwdata
->drvdata
, reg
+ 2,
776 SI5351_OUTPUT_CLK_DIVBY4
,
777 (divby4
) ? SI5351_OUTPUT_CLK_DIVBY4
: 0);
778 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK0_CTRL
+ hwdata
->num
,
779 SI5351_CLK_INTEGER_MODE
,
780 (hwdata
->params
.p2
== 0) ? SI5351_CLK_INTEGER_MODE
: 0);
783 dev_dbg(&hwdata
->drvdata
->client
->dev
,
784 "%s - %s: p1 = %lu, p2 = %lu, p3 = %lu, divby4 = %d, parent_rate = %lu, rate = %lu\n",
785 __func__
, clk_hw_get_name(hw
),
786 hwdata
->params
.p1
, hwdata
->params
.p2
, hwdata
->params
.p3
,
787 divby4
, parent_rate
, rate
);
792 static const struct clk_ops si5351_msynth_ops
= {
793 .set_parent
= si5351_msynth_set_parent
,
794 .get_parent
= si5351_msynth_get_parent
,
795 .recalc_rate
= si5351_msynth_recalc_rate
,
796 .round_rate
= si5351_msynth_round_rate
,
797 .set_rate
= si5351_msynth_set_rate
,
801 * Si5351 clkout divider
803 static int _si5351_clkout_reparent(struct si5351_driver_data
*drvdata
,
804 int num
, enum si5351_clkout_src parent
)
812 case SI5351_CLKOUT_SRC_MSYNTH_N
:
813 val
= SI5351_CLK_INPUT_MULTISYNTH_N
;
815 case SI5351_CLKOUT_SRC_MSYNTH_0_4
:
816 /* clk0/clk4 can only connect to its own multisync */
817 if (num
== 0 || num
== 4)
818 val
= SI5351_CLK_INPUT_MULTISYNTH_N
;
820 val
= SI5351_CLK_INPUT_MULTISYNTH_0_4
;
822 case SI5351_CLKOUT_SRC_XTAL
:
823 val
= SI5351_CLK_INPUT_XTAL
;
825 case SI5351_CLKOUT_SRC_CLKIN
:
826 if (drvdata
->variant
!= SI5351_VARIANT_C
)
829 val
= SI5351_CLK_INPUT_CLKIN
;
835 si5351_set_bits(drvdata
, SI5351_CLK0_CTRL
+ num
,
836 SI5351_CLK_INPUT_MASK
, val
);
840 static int _si5351_clkout_set_drive_strength(
841 struct si5351_driver_data
*drvdata
, int num
,
842 enum si5351_drive_strength drive
)
850 case SI5351_DRIVE_2MA
:
851 mask
= SI5351_CLK_DRIVE_STRENGTH_2MA
;
853 case SI5351_DRIVE_4MA
:
854 mask
= SI5351_CLK_DRIVE_STRENGTH_4MA
;
856 case SI5351_DRIVE_6MA
:
857 mask
= SI5351_CLK_DRIVE_STRENGTH_6MA
;
859 case SI5351_DRIVE_8MA
:
860 mask
= SI5351_CLK_DRIVE_STRENGTH_8MA
;
866 si5351_set_bits(drvdata
, SI5351_CLK0_CTRL
+ num
,
867 SI5351_CLK_DRIVE_STRENGTH_MASK
, mask
);
871 static int _si5351_clkout_set_disable_state(
872 struct si5351_driver_data
*drvdata
, int num
,
873 enum si5351_disable_state state
)
875 u8 reg
= (num
< 4) ? SI5351_CLK3_0_DISABLE_STATE
:
876 SI5351_CLK7_4_DISABLE_STATE
;
877 u8 shift
= (num
< 4) ? (2 * num
) : (2 * (num
-4));
878 u8 mask
= SI5351_CLK_DISABLE_STATE_MASK
<< shift
;
885 case SI5351_DISABLE_LOW
:
886 val
= SI5351_CLK_DISABLE_STATE_LOW
;
888 case SI5351_DISABLE_HIGH
:
889 val
= SI5351_CLK_DISABLE_STATE_HIGH
;
891 case SI5351_DISABLE_FLOATING
:
892 val
= SI5351_CLK_DISABLE_STATE_FLOAT
;
894 case SI5351_DISABLE_NEVER
:
895 val
= SI5351_CLK_DISABLE_STATE_NEVER
;
901 si5351_set_bits(drvdata
, reg
, mask
, val
<< shift
);
906 static void _si5351_clkout_reset_pll(struct si5351_driver_data
*drvdata
, int num
)
908 u8 val
= si5351_reg_read(drvdata
, SI5351_CLK0_CTRL
+ num
);
910 switch (val
& SI5351_CLK_INPUT_MASK
) {
911 case SI5351_CLK_INPUT_XTAL
:
912 case SI5351_CLK_INPUT_CLKIN
:
913 return; /* pll not used, no need to reset */
916 si5351_reg_write(drvdata
, SI5351_PLL_RESET
,
917 val
& SI5351_CLK_PLL_SELECT
? SI5351_PLL_RESET_B
:
920 dev_dbg(&drvdata
->client
->dev
, "%s - %s: pll = %d\n",
921 __func__
, clk_hw_get_name(&drvdata
->clkout
[num
].hw
),
922 (val
& SI5351_CLK_PLL_SELECT
) ? 1 : 0);
925 static int si5351_clkout_prepare(struct clk_hw
*hw
)
927 struct si5351_hw_data
*hwdata
=
928 container_of(hw
, struct si5351_hw_data
, hw
);
929 struct si5351_platform_data
*pdata
=
930 hwdata
->drvdata
->client
->dev
.platform_data
;
932 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK0_CTRL
+ hwdata
->num
,
933 SI5351_CLK_POWERDOWN
, 0);
936 * Do a pll soft reset on the parent pll -- needed to get a
937 * deterministic phase relationship between the output clocks.
939 if (pdata
->clkout
[hwdata
->num
].pll_reset
)
940 _si5351_clkout_reset_pll(hwdata
->drvdata
, hwdata
->num
);
942 si5351_set_bits(hwdata
->drvdata
, SI5351_OUTPUT_ENABLE_CTRL
,
943 (1 << hwdata
->num
), 0);
947 static void si5351_clkout_unprepare(struct clk_hw
*hw
)
949 struct si5351_hw_data
*hwdata
=
950 container_of(hw
, struct si5351_hw_data
, hw
);
952 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK0_CTRL
+ hwdata
->num
,
953 SI5351_CLK_POWERDOWN
, SI5351_CLK_POWERDOWN
);
954 si5351_set_bits(hwdata
->drvdata
, SI5351_OUTPUT_ENABLE_CTRL
,
955 (1 << hwdata
->num
), (1 << hwdata
->num
));
958 static u8
si5351_clkout_get_parent(struct clk_hw
*hw
)
960 struct si5351_hw_data
*hwdata
=
961 container_of(hw
, struct si5351_hw_data
, hw
);
965 val
= si5351_reg_read(hwdata
->drvdata
, SI5351_CLK0_CTRL
+ hwdata
->num
);
966 switch (val
& SI5351_CLK_INPUT_MASK
) {
967 case SI5351_CLK_INPUT_MULTISYNTH_N
:
970 case SI5351_CLK_INPUT_MULTISYNTH_0_4
:
973 case SI5351_CLK_INPUT_XTAL
:
976 case SI5351_CLK_INPUT_CLKIN
:
984 static int si5351_clkout_set_parent(struct clk_hw
*hw
, u8 index
)
986 struct si5351_hw_data
*hwdata
=
987 container_of(hw
, struct si5351_hw_data
, hw
);
988 enum si5351_clkout_src parent
= SI5351_CLKOUT_SRC_DEFAULT
;
992 parent
= SI5351_CLKOUT_SRC_MSYNTH_N
;
995 parent
= SI5351_CLKOUT_SRC_MSYNTH_0_4
;
998 parent
= SI5351_CLKOUT_SRC_XTAL
;
1001 parent
= SI5351_CLKOUT_SRC_CLKIN
;
1005 return _si5351_clkout_reparent(hwdata
->drvdata
, hwdata
->num
, parent
);
1008 static unsigned long si5351_clkout_recalc_rate(struct clk_hw
*hw
,
1009 unsigned long parent_rate
)
1011 struct si5351_hw_data
*hwdata
=
1012 container_of(hw
, struct si5351_hw_data
, hw
);
1016 if (hwdata
->num
<= 5)
1017 reg
= si5351_msynth_params_address(hwdata
->num
) + 2;
1019 reg
= SI5351_CLK6_7_OUTPUT_DIVIDER
;
1021 rdiv
= si5351_reg_read(hwdata
->drvdata
, reg
);
1022 if (hwdata
->num
== 6) {
1023 rdiv
&= SI5351_OUTPUT_CLK6_DIV_MASK
;
1025 rdiv
&= SI5351_OUTPUT_CLK_DIV_MASK
;
1026 rdiv
>>= SI5351_OUTPUT_CLK_DIV_SHIFT
;
1029 return parent_rate
>> rdiv
;
1032 static long si5351_clkout_round_rate(struct clk_hw
*hw
, unsigned long rate
,
1033 unsigned long *parent_rate
)
1035 struct si5351_hw_data
*hwdata
=
1036 container_of(hw
, struct si5351_hw_data
, hw
);
1039 /* clkout6/7 can only handle output freqencies < 150MHz */
1040 if (hwdata
->num
>= 6 && rate
> SI5351_CLKOUT67_MAX_FREQ
)
1041 rate
= SI5351_CLKOUT67_MAX_FREQ
;
1043 /* clkout freqency is 8kHz - 160MHz */
1044 if (rate
> SI5351_CLKOUT_MAX_FREQ
)
1045 rate
= SI5351_CLKOUT_MAX_FREQ
;
1046 if (rate
< SI5351_CLKOUT_MIN_FREQ
)
1047 rate
= SI5351_CLKOUT_MIN_FREQ
;
1049 /* request frequency if multisync master */
1050 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
) {
1051 /* use r divider for frequencies below 1MHz */
1052 rdiv
= SI5351_OUTPUT_CLK_DIV_1
;
1053 while (rate
< SI5351_MULTISYNTH_MIN_FREQ
&&
1054 rdiv
< SI5351_OUTPUT_CLK_DIV_128
) {
1058 *parent_rate
= rate
;
1060 unsigned long new_rate
, new_err
, err
;
1062 /* round to closed rdiv */
1063 rdiv
= SI5351_OUTPUT_CLK_DIV_1
;
1064 new_rate
= *parent_rate
;
1065 err
= abs(new_rate
- rate
);
1068 new_err
= abs(new_rate
- rate
);
1069 if (new_err
> err
|| rdiv
== SI5351_OUTPUT_CLK_DIV_128
)
1075 rate
= *parent_rate
>> rdiv
;
1077 dev_dbg(&hwdata
->drvdata
->client
->dev
,
1078 "%s - %s: rdiv = %u, parent_rate = %lu, rate = %lu\n",
1079 __func__
, clk_hw_get_name(hw
), (1 << rdiv
),
1080 *parent_rate
, rate
);
1085 static int si5351_clkout_set_rate(struct clk_hw
*hw
, unsigned long rate
,
1086 unsigned long parent_rate
)
1088 struct si5351_hw_data
*hwdata
=
1089 container_of(hw
, struct si5351_hw_data
, hw
);
1090 unsigned long new_rate
, new_err
, err
;
1093 /* round to closed rdiv */
1094 rdiv
= SI5351_OUTPUT_CLK_DIV_1
;
1095 new_rate
= parent_rate
;
1096 err
= abs(new_rate
- rate
);
1099 new_err
= abs(new_rate
- rate
);
1100 if (new_err
> err
|| rdiv
== SI5351_OUTPUT_CLK_DIV_128
)
1106 /* write output divider */
1107 switch (hwdata
->num
) {
1109 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK6_7_OUTPUT_DIVIDER
,
1110 SI5351_OUTPUT_CLK6_DIV_MASK
, rdiv
);
1113 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK6_7_OUTPUT_DIVIDER
,
1114 SI5351_OUTPUT_CLK_DIV_MASK
,
1115 rdiv
<< SI5351_OUTPUT_CLK_DIV_SHIFT
);
1118 si5351_set_bits(hwdata
->drvdata
,
1119 si5351_msynth_params_address(hwdata
->num
) + 2,
1120 SI5351_OUTPUT_CLK_DIV_MASK
,
1121 rdiv
<< SI5351_OUTPUT_CLK_DIV_SHIFT
);
1124 /* powerup clkout */
1125 si5351_set_bits(hwdata
->drvdata
, SI5351_CLK0_CTRL
+ hwdata
->num
,
1126 SI5351_CLK_POWERDOWN
, 0);
1128 dev_dbg(&hwdata
->drvdata
->client
->dev
,
1129 "%s - %s: rdiv = %u, parent_rate = %lu, rate = %lu\n",
1130 __func__
, clk_hw_get_name(hw
), (1 << rdiv
),
1136 static const struct clk_ops si5351_clkout_ops
= {
1137 .prepare
= si5351_clkout_prepare
,
1138 .unprepare
= si5351_clkout_unprepare
,
1139 .set_parent
= si5351_clkout_set_parent
,
1140 .get_parent
= si5351_clkout_get_parent
,
1141 .recalc_rate
= si5351_clkout_recalc_rate
,
1142 .round_rate
= si5351_clkout_round_rate
,
1143 .set_rate
= si5351_clkout_set_rate
,
1147 * Si5351 i2c probe and DT
1150 static const struct of_device_id si5351_dt_ids
[] = {
1151 { .compatible
= "silabs,si5351a", .data
= (void *)SI5351_VARIANT_A
, },
1152 { .compatible
= "silabs,si5351a-msop",
1153 .data
= (void *)SI5351_VARIANT_A3
, },
1154 { .compatible
= "silabs,si5351b", .data
= (void *)SI5351_VARIANT_B
, },
1155 { .compatible
= "silabs,si5351c", .data
= (void *)SI5351_VARIANT_C
, },
1158 MODULE_DEVICE_TABLE(of
, si5351_dt_ids
);
1160 static int si5351_dt_parse(struct i2c_client
*client
,
1161 enum si5351_variant variant
)
1163 struct device_node
*child
, *np
= client
->dev
.of_node
;
1164 struct si5351_platform_data
*pdata
;
1165 struct property
*prop
;
1173 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1178 * property silabs,pll-source : <num src>, [<..>]
1179 * allow to selectively set pll source
1181 of_property_for_each_u32(np
, "silabs,pll-source", prop
, p
, num
) {
1183 dev_err(&client
->dev
,
1184 "invalid pll %d on pll-source prop\n", num
);
1188 p
= of_prop_next_u32(prop
, p
, &val
);
1190 dev_err(&client
->dev
,
1191 "missing pll-source for pll %d\n", num
);
1197 pdata
->pll_src
[num
] = SI5351_PLL_SRC_XTAL
;
1200 if (variant
!= SI5351_VARIANT_C
) {
1201 dev_err(&client
->dev
,
1202 "invalid parent %d for pll %d\n",
1206 pdata
->pll_src
[num
] = SI5351_PLL_SRC_CLKIN
;
1209 dev_err(&client
->dev
,
1210 "invalid parent %d for pll %d\n", val
, num
);
1215 /* per clkout properties */
1216 for_each_child_of_node(np
, child
) {
1217 if (of_property_read_u32(child
, "reg", &num
)) {
1218 dev_err(&client
->dev
, "missing reg property of %s\n",
1224 (variant
== SI5351_VARIANT_A3
&& num
>= 3)) {
1225 dev_err(&client
->dev
, "invalid clkout %d\n", num
);
1229 if (!of_property_read_u32(child
, "silabs,multisynth-source",
1233 pdata
->clkout
[num
].multisynth_src
=
1234 SI5351_MULTISYNTH_SRC_VCO0
;
1237 pdata
->clkout
[num
].multisynth_src
=
1238 SI5351_MULTISYNTH_SRC_VCO1
;
1241 dev_err(&client
->dev
,
1242 "invalid parent %d for multisynth %d\n",
1248 if (!of_property_read_u32(child
, "silabs,clock-source", &val
)) {
1251 pdata
->clkout
[num
].clkout_src
=
1252 SI5351_CLKOUT_SRC_MSYNTH_N
;
1255 pdata
->clkout
[num
].clkout_src
=
1256 SI5351_CLKOUT_SRC_MSYNTH_0_4
;
1259 pdata
->clkout
[num
].clkout_src
=
1260 SI5351_CLKOUT_SRC_XTAL
;
1263 if (variant
!= SI5351_VARIANT_C
) {
1264 dev_err(&client
->dev
,
1265 "invalid parent %d for clkout %d\n",
1269 pdata
->clkout
[num
].clkout_src
=
1270 SI5351_CLKOUT_SRC_CLKIN
;
1273 dev_err(&client
->dev
,
1274 "invalid parent %d for clkout %d\n",
1280 if (!of_property_read_u32(child
, "silabs,drive-strength",
1283 case SI5351_DRIVE_2MA
:
1284 case SI5351_DRIVE_4MA
:
1285 case SI5351_DRIVE_6MA
:
1286 case SI5351_DRIVE_8MA
:
1287 pdata
->clkout
[num
].drive
= val
;
1290 dev_err(&client
->dev
,
1291 "invalid drive strength %d for clkout %d\n",
1297 if (!of_property_read_u32(child
, "silabs,disable-state",
1301 pdata
->clkout
[num
].disable_state
=
1305 pdata
->clkout
[num
].disable_state
=
1306 SI5351_DISABLE_HIGH
;
1309 pdata
->clkout
[num
].disable_state
=
1310 SI5351_DISABLE_FLOATING
;
1313 pdata
->clkout
[num
].disable_state
=
1314 SI5351_DISABLE_NEVER
;
1317 dev_err(&client
->dev
,
1318 "invalid disable state %d for clkout %d\n",
1324 if (!of_property_read_u32(child
, "clock-frequency", &val
))
1325 pdata
->clkout
[num
].rate
= val
;
1327 pdata
->clkout
[num
].pll_master
=
1328 of_property_read_bool(child
, "silabs,pll-master");
1330 pdata
->clkout
[num
].pll_reset
=
1331 of_property_read_bool(child
, "silabs,pll-reset");
1333 client
->dev
.platform_data
= pdata
;
1341 static struct clk_hw
*
1342 si53351_of_clk_get(struct of_phandle_args
*clkspec
, void *data
)
1344 struct si5351_driver_data
*drvdata
= data
;
1345 unsigned int idx
= clkspec
->args
[0];
1347 if (idx
>= drvdata
->num_clkout
) {
1348 pr_err("%s: invalid index %u\n", __func__
, idx
);
1349 return ERR_PTR(-EINVAL
);
1352 return &drvdata
->clkout
[idx
].hw
;
1355 static int si5351_dt_parse(struct i2c_client
*client
, enum si5351_variant variant
)
1360 static struct clk_hw
*
1361 si53351_of_clk_get(struct of_phandle_args
*clkspec
, void *data
)
1365 #endif /* CONFIG_OF */
1367 static int si5351_i2c_probe(struct i2c_client
*client
,
1368 const struct i2c_device_id
*id
)
1370 enum si5351_variant variant
= (enum si5351_variant
)id
->driver_data
;
1371 struct si5351_platform_data
*pdata
;
1372 struct si5351_driver_data
*drvdata
;
1373 struct clk_init_data init
;
1374 const char *parent_names
[4];
1375 u8 num_parents
, num_clocks
;
1378 ret
= si5351_dt_parse(client
, variant
);
1382 pdata
= client
->dev
.platform_data
;
1386 drvdata
= devm_kzalloc(&client
->dev
, sizeof(*drvdata
), GFP_KERNEL
);
1390 i2c_set_clientdata(client
, drvdata
);
1391 drvdata
->client
= client
;
1392 drvdata
->variant
= variant
;
1393 drvdata
->pxtal
= devm_clk_get(&client
->dev
, "xtal");
1394 drvdata
->pclkin
= devm_clk_get(&client
->dev
, "clkin");
1396 if (PTR_ERR(drvdata
->pxtal
) == -EPROBE_DEFER
||
1397 PTR_ERR(drvdata
->pclkin
) == -EPROBE_DEFER
)
1398 return -EPROBE_DEFER
;
1401 * Check for valid parent clock: VARIANT_A and VARIANT_B need XTAL,
1402 * VARIANT_C can have CLKIN instead.
1404 if (IS_ERR(drvdata
->pxtal
) &&
1405 (drvdata
->variant
!= SI5351_VARIANT_C
|| IS_ERR(drvdata
->pclkin
))) {
1406 dev_err(&client
->dev
, "missing parent clock\n");
1410 drvdata
->regmap
= devm_regmap_init_i2c(client
, &si5351_regmap_config
);
1411 if (IS_ERR(drvdata
->regmap
)) {
1412 dev_err(&client
->dev
, "failed to allocate register map\n");
1413 return PTR_ERR(drvdata
->regmap
);
1416 /* Disable interrupts */
1417 si5351_reg_write(drvdata
, SI5351_INTERRUPT_MASK
, 0xf0);
1418 /* Ensure pll select is on XTAL for Si5351A/B */
1419 if (drvdata
->variant
!= SI5351_VARIANT_C
)
1420 si5351_set_bits(drvdata
, SI5351_PLL_INPUT_SOURCE
,
1421 SI5351_PLLA_SOURCE
| SI5351_PLLB_SOURCE
, 0);
1423 /* setup clock configuration */
1424 for (n
= 0; n
< 2; n
++) {
1425 ret
= _si5351_pll_reparent(drvdata
, n
, pdata
->pll_src
[n
]);
1427 dev_err(&client
->dev
,
1428 "failed to reparent pll %d to %d\n",
1429 n
, pdata
->pll_src
[n
]);
1434 for (n
= 0; n
< 8; n
++) {
1435 ret
= _si5351_msynth_reparent(drvdata
, n
,
1436 pdata
->clkout
[n
].multisynth_src
);
1438 dev_err(&client
->dev
,
1439 "failed to reparent multisynth %d to %d\n",
1440 n
, pdata
->clkout
[n
].multisynth_src
);
1444 ret
= _si5351_clkout_reparent(drvdata
, n
,
1445 pdata
->clkout
[n
].clkout_src
);
1447 dev_err(&client
->dev
,
1448 "failed to reparent clkout %d to %d\n",
1449 n
, pdata
->clkout
[n
].clkout_src
);
1453 ret
= _si5351_clkout_set_drive_strength(drvdata
, n
,
1454 pdata
->clkout
[n
].drive
);
1456 dev_err(&client
->dev
,
1457 "failed set drive strength of clkout%d to %d\n",
1458 n
, pdata
->clkout
[n
].drive
);
1462 ret
= _si5351_clkout_set_disable_state(drvdata
, n
,
1463 pdata
->clkout
[n
].disable_state
);
1465 dev_err(&client
->dev
,
1466 "failed set disable state of clkout%d to %d\n",
1467 n
, pdata
->clkout
[n
].disable_state
);
1472 /* register xtal input clock gate */
1473 memset(&init
, 0, sizeof(init
));
1474 init
.name
= si5351_input_names
[0];
1475 init
.ops
= &si5351_xtal_ops
;
1477 if (!IS_ERR(drvdata
->pxtal
)) {
1478 drvdata
->pxtal_name
= __clk_get_name(drvdata
->pxtal
);
1479 init
.parent_names
= &drvdata
->pxtal_name
;
1480 init
.num_parents
= 1;
1482 drvdata
->xtal
.init
= &init
;
1483 ret
= devm_clk_hw_register(&client
->dev
, &drvdata
->xtal
);
1485 dev_err(&client
->dev
, "unable to register %s\n", init
.name
);
1489 /* register clkin input clock gate */
1490 if (drvdata
->variant
== SI5351_VARIANT_C
) {
1491 memset(&init
, 0, sizeof(init
));
1492 init
.name
= si5351_input_names
[1];
1493 init
.ops
= &si5351_clkin_ops
;
1494 if (!IS_ERR(drvdata
->pclkin
)) {
1495 drvdata
->pclkin_name
= __clk_get_name(drvdata
->pclkin
);
1496 init
.parent_names
= &drvdata
->pclkin_name
;
1497 init
.num_parents
= 1;
1499 drvdata
->clkin
.init
= &init
;
1500 ret
= devm_clk_hw_register(&client
->dev
, &drvdata
->clkin
);
1502 dev_err(&client
->dev
, "unable to register %s\n",
1508 /* Si5351C allows to mux either xtal or clkin to PLL input */
1509 num_parents
= (drvdata
->variant
== SI5351_VARIANT_C
) ? 2 : 1;
1510 parent_names
[0] = si5351_input_names
[0];
1511 parent_names
[1] = si5351_input_names
[1];
1514 drvdata
->pll
[0].num
= 0;
1515 drvdata
->pll
[0].drvdata
= drvdata
;
1516 drvdata
->pll
[0].hw
.init
= &init
;
1517 memset(&init
, 0, sizeof(init
));
1518 init
.name
= si5351_pll_names
[0];
1519 init
.ops
= &si5351_pll_ops
;
1521 init
.parent_names
= parent_names
;
1522 init
.num_parents
= num_parents
;
1523 ret
= devm_clk_hw_register(&client
->dev
, &drvdata
->pll
[0].hw
);
1525 dev_err(&client
->dev
, "unable to register %s\n", init
.name
);
1529 /* register PLLB or VXCO (Si5351B) */
1530 drvdata
->pll
[1].num
= 1;
1531 drvdata
->pll
[1].drvdata
= drvdata
;
1532 drvdata
->pll
[1].hw
.init
= &init
;
1533 memset(&init
, 0, sizeof(init
));
1534 if (drvdata
->variant
== SI5351_VARIANT_B
) {
1535 init
.name
= si5351_pll_names
[2];
1536 init
.ops
= &si5351_vxco_ops
;
1538 init
.parent_names
= NULL
;
1539 init
.num_parents
= 0;
1541 init
.name
= si5351_pll_names
[1];
1542 init
.ops
= &si5351_pll_ops
;
1544 init
.parent_names
= parent_names
;
1545 init
.num_parents
= num_parents
;
1547 ret
= devm_clk_hw_register(&client
->dev
, &drvdata
->pll
[1].hw
);
1549 dev_err(&client
->dev
, "unable to register %s\n", init
.name
);
1553 /* register clk multisync and clk out divider */
1554 num_clocks
= (drvdata
->variant
== SI5351_VARIANT_A3
) ? 3 : 8;
1555 parent_names
[0] = si5351_pll_names
[0];
1556 if (drvdata
->variant
== SI5351_VARIANT_B
)
1557 parent_names
[1] = si5351_pll_names
[2];
1559 parent_names
[1] = si5351_pll_names
[1];
1561 drvdata
->msynth
= devm_kcalloc(&client
->dev
, num_clocks
,
1562 sizeof(*drvdata
->msynth
), GFP_KERNEL
);
1563 drvdata
->clkout
= devm_kcalloc(&client
->dev
, num_clocks
,
1564 sizeof(*drvdata
->clkout
), GFP_KERNEL
);
1565 drvdata
->num_clkout
= num_clocks
;
1567 if (WARN_ON(!drvdata
->msynth
|| !drvdata
->clkout
)) {
1572 for (n
= 0; n
< num_clocks
; n
++) {
1573 drvdata
->msynth
[n
].num
= n
;
1574 drvdata
->msynth
[n
].drvdata
= drvdata
;
1575 drvdata
->msynth
[n
].hw
.init
= &init
;
1576 memset(&init
, 0, sizeof(init
));
1577 init
.name
= si5351_msynth_names
[n
];
1578 init
.ops
= &si5351_msynth_ops
;
1580 if (pdata
->clkout
[n
].pll_master
)
1581 init
.flags
|= CLK_SET_RATE_PARENT
;
1582 init
.parent_names
= parent_names
;
1583 init
.num_parents
= 2;
1584 ret
= devm_clk_hw_register(&client
->dev
,
1585 &drvdata
->msynth
[n
].hw
);
1587 dev_err(&client
->dev
, "unable to register %s\n",
1593 num_parents
= (drvdata
->variant
== SI5351_VARIANT_C
) ? 4 : 3;
1594 parent_names
[2] = si5351_input_names
[0];
1595 parent_names
[3] = si5351_input_names
[1];
1596 for (n
= 0; n
< num_clocks
; n
++) {
1597 parent_names
[0] = si5351_msynth_names
[n
];
1598 parent_names
[1] = (n
< 4) ? si5351_msynth_names
[0] :
1599 si5351_msynth_names
[4];
1601 drvdata
->clkout
[n
].num
= n
;
1602 drvdata
->clkout
[n
].drvdata
= drvdata
;
1603 drvdata
->clkout
[n
].hw
.init
= &init
;
1604 memset(&init
, 0, sizeof(init
));
1605 init
.name
= si5351_clkout_names
[n
];
1606 init
.ops
= &si5351_clkout_ops
;
1608 if (pdata
->clkout
[n
].clkout_src
== SI5351_CLKOUT_SRC_MSYNTH_N
)
1609 init
.flags
|= CLK_SET_RATE_PARENT
;
1610 init
.parent_names
= parent_names
;
1611 init
.num_parents
= num_parents
;
1612 ret
= devm_clk_hw_register(&client
->dev
,
1613 &drvdata
->clkout
[n
].hw
);
1615 dev_err(&client
->dev
, "unable to register %s\n",
1620 /* set initial clkout rate */
1621 if (pdata
->clkout
[n
].rate
!= 0) {
1623 ret
= clk_set_rate(drvdata
->clkout
[n
].hw
.clk
,
1624 pdata
->clkout
[n
].rate
);
1626 dev_err(&client
->dev
, "Cannot set rate : %d\n",
1632 ret
= of_clk_add_hw_provider(client
->dev
.of_node
, si53351_of_clk_get
,
1635 dev_err(&client
->dev
, "unable to add clk provider\n");
1642 static int si5351_i2c_remove(struct i2c_client
*client
)
1644 of_clk_del_provider(client
->dev
.of_node
);
1649 static const struct i2c_device_id si5351_i2c_ids
[] = {
1650 { "si5351a", SI5351_VARIANT_A
},
1651 { "si5351a-msop", SI5351_VARIANT_A3
},
1652 { "si5351b", SI5351_VARIANT_B
},
1653 { "si5351c", SI5351_VARIANT_C
},
1656 MODULE_DEVICE_TABLE(i2c
, si5351_i2c_ids
);
1658 static struct i2c_driver si5351_driver
= {
1661 .of_match_table
= of_match_ptr(si5351_dt_ids
),
1663 .probe
= si5351_i2c_probe
,
1664 .remove
= si5351_i2c_remove
,
1665 .id_table
= si5351_i2c_ids
,
1667 module_i2c_driver(si5351_driver
);
1669 MODULE_AUTHOR("Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com");
1670 MODULE_DESCRIPTION("Silicon Labs Si5351A/B/C clock generator driver");
1671 MODULE_LICENSE("GPL");