2 * Driver for TI Dual PLL CDCE925 clock synthesizer
4 * This driver always connects the Y1 to the input clock, Y2/Y3 to PLL1
5 * and Y4/Y5 to PLL2. PLL frequency is set on a first-come-first-serve
6 * basis. Clients can directly request any frequency that the chip can
7 * deliver using the standard clk framework. In addition, the device can
8 * be configured and activated via the devicetree.
10 * Copyright (C) 2014, Topic Embedded Products
13 #include <linux/clk-provider.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/regmap.h>
18 #include <linux/slab.h>
19 #include <linux/gcd.h>
21 /* The chip has 2 PLLs which can be routed through dividers to 5 outputs.
22 * Model this as 2 PLL clocks which are parents to the outputs.
24 #define NUMBER_OF_PLLS 2
25 #define NUMBER_OF_OUTPUTS 5
27 #define CDCE925_REG_GLOBAL1 0x01
28 #define CDCE925_REG_Y1SPIPDIVH 0x02
29 #define CDCE925_REG_PDIVL 0x03
30 #define CDCE925_REG_XCSEL 0x05
31 /* PLL parameters start at 0x10, steps of 0x10 */
32 #define CDCE925_OFFSET_PLL 0x10
33 /* Add CDCE925_OFFSET_PLL * (pll) to these registers before sending */
34 #define CDCE925_PLL_MUX_OUTPUTS 0x14
35 #define CDCE925_PLL_MULDIV 0x18
37 #define CDCE925_PLL_FREQUENCY_MIN 80000000ul
38 #define CDCE925_PLL_FREQUENCY_MAX 230000000ul
39 struct clk_cdce925_chip
;
41 struct clk_cdce925_output
{
43 struct clk_cdce925_chip
*chip
;
45 u16 pdiv
; /* 1..127 for Y2-Y5; 1..1023 for Y1 */
47 #define to_clk_cdce925_output(_hw) \
48 container_of(_hw, struct clk_cdce925_output, hw)
50 struct clk_cdce925_pll
{
52 struct clk_cdce925_chip
*chip
;
57 #define to_clk_cdce925_pll(_hw) container_of(_hw, struct clk_cdce925_pll, hw)
59 struct clk_cdce925_chip
{
60 struct regmap
*regmap
;
61 struct i2c_client
*i2c_client
;
62 struct clk_cdce925_pll pll
[NUMBER_OF_PLLS
];
63 struct clk_cdce925_output clk
[NUMBER_OF_OUTPUTS
];
64 struct clk
*dt_clk
[NUMBER_OF_OUTPUTS
];
65 struct clk_onecell_data onecell
;
68 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
70 static unsigned long cdce925_pll_calculate_rate(unsigned long parent_rate
,
73 if ((!m
|| !n
) || (m
== n
))
74 return parent_rate
; /* In bypass mode runs at same frequency */
75 return mult_frac(parent_rate
, (unsigned long)n
, (unsigned long)m
);
78 static unsigned long cdce925_pll_recalc_rate(struct clk_hw
*hw
,
79 unsigned long parent_rate
)
81 /* Output frequency of PLL is Fout = (Fin/Pdiv)*(N/M) */
82 struct clk_cdce925_pll
*data
= to_clk_cdce925_pll(hw
);
84 return cdce925_pll_calculate_rate(parent_rate
, data
->n
, data
->m
);
87 static void cdce925_pll_find_rate(unsigned long rate
,
88 unsigned long parent_rate
, u16
*n
, u16
*m
)
94 if (rate
<= parent_rate
) {
95 /* Can always deliver parent_rate in bypass mode */
100 /* In PLL mode, need to apply min/max range */
101 if (rate
< CDCE925_PLL_FREQUENCY_MIN
)
102 rate
= CDCE925_PLL_FREQUENCY_MIN
;
103 else if (rate
> CDCE925_PLL_FREQUENCY_MAX
)
104 rate
= CDCE925_PLL_FREQUENCY_MAX
;
106 g
= gcd(rate
, parent_rate
);
107 um
= parent_rate
/ g
;
109 /* When outside hw range, reduce to fit (rounding errors) */
110 while ((un
> 4095) || (um
> 511)) {
124 static long cdce925_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
125 unsigned long *parent_rate
)
129 cdce925_pll_find_rate(rate
, *parent_rate
, &n
, &m
);
130 return (long)cdce925_pll_calculate_rate(*parent_rate
, n
, m
);
133 static int cdce925_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
134 unsigned long parent_rate
)
136 struct clk_cdce925_pll
*data
= to_clk_cdce925_pll(hw
);
138 if (!rate
|| (rate
== parent_rate
)) {
139 data
->m
= 0; /* Bypass mode */
144 if ((rate
< CDCE925_PLL_FREQUENCY_MIN
) ||
145 (rate
> CDCE925_PLL_FREQUENCY_MAX
)) {
146 pr_debug("%s: rate %lu outside PLL range.\n", __func__
, rate
);
150 if (rate
< parent_rate
) {
151 pr_debug("%s: rate %lu less than parent rate %lu.\n", __func__
,
156 cdce925_pll_find_rate(rate
, parent_rate
, &data
->n
, &data
->m
);
161 /* calculate p = max(0, 4 - int(log2 (n/m))) */
162 static u8
cdce925_pll_calc_p(u16 n
, u16 m
)
177 /* Returns VCO range bits for VCO1_0_RANGE */
178 static u8
cdce925_pll_calc_range_bits(struct clk_hw
*hw
, u16 n
, u16 m
)
180 struct clk
*parent
= clk_get_parent(hw
->clk
);
181 unsigned long rate
= clk_get_rate(parent
);
183 rate
= mult_frac(rate
, (unsigned long)n
, (unsigned long)m
);
184 if (rate
>= 175000000)
186 if (rate
>= 150000000)
188 if (rate
>= 125000000)
193 /* I2C clock, hence everything must happen in (un)prepare because this
195 static int cdce925_pll_prepare(struct clk_hw
*hw
)
197 struct clk_cdce925_pll
*data
= to_clk_cdce925_pll(hw
);
204 u8 pll
[4]; /* Bits are spread out over 4 byte registers */
205 u8 reg_ofs
= data
->index
* CDCE925_OFFSET_PLL
;
208 if ((!m
|| !n
) || (m
== n
)) {
209 /* Set PLL mux to bypass mode, leave the rest as is */
210 regmap_update_bits(data
->chip
->regmap
,
211 reg_ofs
+ CDCE925_PLL_MUX_OUTPUTS
, 0x80, 0x80);
213 /* According to data sheet: */
214 /* p = max(0, 4 - int(log2 (n/m))) */
215 p
= cdce925_pll_calc_p(n
, m
);
220 if ((q
< 16) || (1 > 64)) {
221 pr_debug("%s invalid q=%d\n", __func__
, q
);
226 pr_debug("%s invalid r=%d\n", __func__
, r
);
229 pr_debug("%s n=%d m=%d p=%d q=%d r=%d\n", __func__
,
231 /* encode into register bits */
233 pll
[1] = ((n
& 0x0F) << 4) | ((r
>> 5) & 0x0F);
234 pll
[2] = ((r
& 0x1F) << 3) | ((q
>> 3) & 0x07);
235 pll
[3] = ((q
& 0x07) << 5) | (p
<< 2) |
236 cdce925_pll_calc_range_bits(hw
, n
, m
);
237 /* Write to registers */
238 for (i
= 0; i
< ARRAY_SIZE(pll
); ++i
)
239 regmap_write(data
->chip
->regmap
,
240 reg_ofs
+ CDCE925_PLL_MULDIV
+ i
, pll
[i
]);
242 regmap_update_bits(data
->chip
->regmap
,
243 reg_ofs
+ CDCE925_PLL_MUX_OUTPUTS
, 0x80, 0x00);
249 static void cdce925_pll_unprepare(struct clk_hw
*hw
)
251 struct clk_cdce925_pll
*data
= to_clk_cdce925_pll(hw
);
252 u8 reg_ofs
= data
->index
* CDCE925_OFFSET_PLL
;
254 regmap_update_bits(data
->chip
->regmap
,
255 reg_ofs
+ CDCE925_PLL_MUX_OUTPUTS
, 0x80, 0x80);
258 static const struct clk_ops cdce925_pll_ops
= {
259 .prepare
= cdce925_pll_prepare
,
260 .unprepare
= cdce925_pll_unprepare
,
261 .recalc_rate
= cdce925_pll_recalc_rate
,
262 .round_rate
= cdce925_pll_round_rate
,
263 .set_rate
= cdce925_pll_set_rate
,
267 static void cdce925_clk_set_pdiv(struct clk_cdce925_output
*data
, u16 pdiv
)
269 switch (data
->index
) {
271 regmap_update_bits(data
->chip
->regmap
,
272 CDCE925_REG_Y1SPIPDIVH
,
273 0x03, (pdiv
>> 8) & 0x03);
274 regmap_write(data
->chip
->regmap
, 0x03, pdiv
& 0xFF);
277 regmap_update_bits(data
->chip
->regmap
, 0x16, 0x7F, pdiv
);
280 regmap_update_bits(data
->chip
->regmap
, 0x17, 0x7F, pdiv
);
283 regmap_update_bits(data
->chip
->regmap
, 0x26, 0x7F, pdiv
);
286 regmap_update_bits(data
->chip
->regmap
, 0x27, 0x7F, pdiv
);
291 static void cdce925_clk_activate(struct clk_cdce925_output
*data
)
293 switch (data
->index
) {
295 regmap_update_bits(data
->chip
->regmap
,
296 CDCE925_REG_Y1SPIPDIVH
, 0x0c, 0x0c);
300 regmap_update_bits(data
->chip
->regmap
, 0x14, 0x03, 0x03);
304 regmap_update_bits(data
->chip
->regmap
, 0x24, 0x03, 0x03);
309 static int cdce925_clk_prepare(struct clk_hw
*hw
)
311 struct clk_cdce925_output
*data
= to_clk_cdce925_output(hw
);
313 cdce925_clk_set_pdiv(data
, data
->pdiv
);
314 cdce925_clk_activate(data
);
318 static void cdce925_clk_unprepare(struct clk_hw
*hw
)
320 struct clk_cdce925_output
*data
= to_clk_cdce925_output(hw
);
322 /* Disable clock by setting divider to "0" */
323 cdce925_clk_set_pdiv(data
, 0);
326 static unsigned long cdce925_clk_recalc_rate(struct clk_hw
*hw
,
327 unsigned long parent_rate
)
329 struct clk_cdce925_output
*data
= to_clk_cdce925_output(hw
);
332 return parent_rate
/ data
->pdiv
;
336 static u16
cdce925_calc_divider(unsigned long rate
,
337 unsigned long parent_rate
)
339 unsigned long divider
;
343 if (rate
>= parent_rate
)
346 divider
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
353 static unsigned long cdce925_clk_best_parent_rate(
354 struct clk_hw
*hw
, unsigned long rate
)
356 struct clk
*pll
= clk_get_parent(hw
->clk
);
357 struct clk
*root
= clk_get_parent(pll
);
358 unsigned long root_rate
= clk_get_rate(root
);
359 unsigned long best_rate_error
= rate
;
365 if (root_rate
% rate
== 0)
366 return root_rate
; /* Don't need the PLL, use bypass */
368 pdiv_min
= (u16
)max(1ul, DIV_ROUND_UP(CDCE925_PLL_FREQUENCY_MIN
, rate
));
369 pdiv_max
= (u16
)min(127ul, CDCE925_PLL_FREQUENCY_MAX
/ rate
);
371 if (pdiv_min
> pdiv_max
)
372 return 0; /* No can do? */
374 pdiv_best
= pdiv_min
;
375 for (pdiv_now
= pdiv_min
; pdiv_now
< pdiv_max
; ++pdiv_now
) {
376 unsigned long target_rate
= rate
* pdiv_now
;
377 long pll_rate
= clk_round_rate(pll
, target_rate
);
378 unsigned long actual_rate
;
379 unsigned long rate_error
;
383 actual_rate
= pll_rate
/ pdiv_now
;
384 rate_error
= abs((long)actual_rate
- (long)rate
);
385 if (rate_error
< best_rate_error
) {
386 pdiv_best
= pdiv_now
;
387 best_rate_error
= rate_error
;
389 /* TODO: Consider PLL frequency based on smaller n/m values
390 * and pick the better one if the error is equal */
393 return rate
* pdiv_best
;
396 static long cdce925_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
397 unsigned long *parent_rate
)
399 unsigned long l_parent_rate
= *parent_rate
;
400 u16 divider
= cdce925_calc_divider(rate
, l_parent_rate
);
402 if (l_parent_rate
/ divider
!= rate
) {
403 l_parent_rate
= cdce925_clk_best_parent_rate(hw
, rate
);
404 divider
= cdce925_calc_divider(rate
, l_parent_rate
);
405 *parent_rate
= l_parent_rate
;
409 return (long)(l_parent_rate
/ divider
);
413 static int cdce925_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
414 unsigned long parent_rate
)
416 struct clk_cdce925_output
*data
= to_clk_cdce925_output(hw
);
418 data
->pdiv
= cdce925_calc_divider(rate
, parent_rate
);
423 static const struct clk_ops cdce925_clk_ops
= {
424 .prepare
= cdce925_clk_prepare
,
425 .unprepare
= cdce925_clk_unprepare
,
426 .recalc_rate
= cdce925_clk_recalc_rate
,
427 .round_rate
= cdce925_clk_round_rate
,
428 .set_rate
= cdce925_clk_set_rate
,
432 static u16
cdce925_y1_calc_divider(unsigned long rate
,
433 unsigned long parent_rate
)
435 unsigned long divider
;
439 if (rate
>= parent_rate
)
442 divider
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
443 if (divider
> 0x3FF) /* Y1 has 10-bit divider */
449 static long cdce925_clk_y1_round_rate(struct clk_hw
*hw
, unsigned long rate
,
450 unsigned long *parent_rate
)
452 unsigned long l_parent_rate
= *parent_rate
;
453 u16 divider
= cdce925_y1_calc_divider(rate
, l_parent_rate
);
456 return (long)(l_parent_rate
/ divider
);
460 static int cdce925_clk_y1_set_rate(struct clk_hw
*hw
, unsigned long rate
,
461 unsigned long parent_rate
)
463 struct clk_cdce925_output
*data
= to_clk_cdce925_output(hw
);
465 data
->pdiv
= cdce925_y1_calc_divider(rate
, parent_rate
);
470 static const struct clk_ops cdce925_clk_y1_ops
= {
471 .prepare
= cdce925_clk_prepare
,
472 .unprepare
= cdce925_clk_unprepare
,
473 .recalc_rate
= cdce925_clk_recalc_rate
,
474 .round_rate
= cdce925_clk_y1_round_rate
,
475 .set_rate
= cdce925_clk_y1_set_rate
,
479 static struct regmap_config cdce925_regmap_config
= {
480 .name
= "configuration0",
483 .cache_type
= REGCACHE_RBTREE
,
484 .max_register
= 0x2F,
487 #define CDCE925_I2C_COMMAND_BLOCK_TRANSFER 0x00
488 #define CDCE925_I2C_COMMAND_BYTE_TRANSFER 0x80
490 static int cdce925_regmap_i2c_write(
491 void *context
, const void *data
, size_t count
)
493 struct device
*dev
= context
;
494 struct i2c_client
*i2c
= to_i2c_client(dev
);
501 /* First byte is command code */
502 reg_data
[0] = CDCE925_I2C_COMMAND_BYTE_TRANSFER
| ((u8
*)data
)[0];
503 reg_data
[1] = ((u8
*)data
)[1];
505 dev_dbg(&i2c
->dev
, "%s(%zu) %#x %#x\n", __func__
, count
,
506 reg_data
[0], reg_data
[1]);
508 ret
= i2c_master_send(i2c
, reg_data
, count
);
509 if (likely(ret
== count
))
517 static int cdce925_regmap_i2c_read(void *context
,
518 const void *reg
, size_t reg_size
, void *val
, size_t val_size
)
520 struct device
*dev
= context
;
521 struct i2c_client
*i2c
= to_i2c_client(dev
);
522 struct i2c_msg xfer
[2];
529 xfer
[0].addr
= i2c
->addr
;
531 xfer
[0].buf
= reg_data
;
534 CDCE925_I2C_COMMAND_BYTE_TRANSFER
| ((u8
*)reg
)[0];
538 CDCE925_I2C_COMMAND_BLOCK_TRANSFER
| ((u8
*)reg
)[0];
539 reg_data
[1] = val_size
;
543 xfer
[1].addr
= i2c
->addr
;
544 xfer
[1].flags
= I2C_M_RD
;
545 xfer
[1].len
= val_size
;
548 ret
= i2c_transfer(i2c
->adapter
, xfer
, 2);
549 if (likely(ret
== 2)) {
550 dev_dbg(&i2c
->dev
, "%s(%zu, %zu) %#x %#x\n", __func__
,
551 reg_size
, val_size
, reg_data
[0], *((u8
*)val
));
559 /* The CDCE925 uses a funky way to read/write registers. Bulk mode is
560 * just weird, so just use the single byte mode exclusively. */
561 static struct regmap_bus regmap_cdce925_bus
= {
562 .write
= cdce925_regmap_i2c_write
,
563 .read
= cdce925_regmap_i2c_read
,
566 static int cdce925_probe(struct i2c_client
*client
,
567 const struct i2c_device_id
*id
)
569 struct clk_cdce925_chip
*data
;
570 struct device_node
*node
= client
->dev
.of_node
;
571 const char *parent_name
;
572 const char *pll_clk_name
[NUMBER_OF_PLLS
] = {NULL
,};
573 struct clk_init_data init
;
578 struct device_node
*np_output
;
581 dev_dbg(&client
->dev
, "%s\n", __func__
);
582 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
586 data
->i2c_client
= client
;
587 data
->regmap
= devm_regmap_init(&client
->dev
, ®map_cdce925_bus
,
588 &client
->dev
, &cdce925_regmap_config
);
589 if (IS_ERR(data
->regmap
)) {
590 dev_err(&client
->dev
, "failed to allocate register map\n");
591 return PTR_ERR(data
->regmap
);
593 i2c_set_clientdata(client
, data
);
595 parent_name
= of_clk_get_parent_name(node
, 0);
597 dev_err(&client
->dev
, "missing parent clock\n");
600 dev_dbg(&client
->dev
, "parent is: %s\n", parent_name
);
602 if (of_property_read_u32(node
, "xtal-load-pf", &value
) == 0)
603 regmap_write(data
->regmap
,
604 CDCE925_REG_XCSEL
, (value
<< 3) & 0xF8);
606 regmap_update_bits(data
->regmap
, CDCE925_REG_GLOBAL1
, BIT(4), 0);
608 /* Set input source for Y1 to be the XTAL */
609 regmap_update_bits(data
->regmap
, 0x02, BIT(7), 0);
611 init
.ops
= &cdce925_pll_ops
;
613 init
.parent_names
= &parent_name
;
614 init
.num_parents
= parent_name
? 1 : 0;
616 /* Register PLL clocks */
617 for (i
= 0; i
< NUMBER_OF_PLLS
; ++i
) {
618 pll_clk_name
[i
] = kasprintf(GFP_KERNEL
, "%s.pll%d",
619 client
->dev
.of_node
->name
, i
);
620 init
.name
= pll_clk_name
[i
];
621 data
->pll
[i
].chip
= data
;
622 data
->pll
[i
].hw
.init
= &init
;
623 data
->pll
[i
].index
= i
;
624 clk
= devm_clk_register(&client
->dev
, &data
->pll
[i
].hw
);
626 dev_err(&client
->dev
, "Failed register PLL %d\n", i
);
630 sprintf(child_name
, "PLL%d", i
+1);
631 np_output
= of_get_child_by_name(node
, child_name
);
634 if (!of_property_read_u32(np_output
,
635 "clock-frequency", &value
)) {
636 err
= clk_set_rate(clk
, value
);
638 dev_err(&client
->dev
,
639 "unable to set PLL frequency %ud\n",
642 if (!of_property_read_u32(np_output
,
643 "spread-spectrum", &value
)) {
644 u8 flag
= of_property_read_bool(np_output
,
645 "spread-spectrum-center") ? 0x80 : 0x00;
646 regmap_update_bits(data
->regmap
,
647 0x16 + (i
*CDCE925_OFFSET_PLL
),
649 regmap_update_bits(data
->regmap
,
650 0x12 + (i
*CDCE925_OFFSET_PLL
),
655 /* Register output clock Y1 */
656 init
.ops
= &cdce925_clk_y1_ops
;
658 init
.num_parents
= 1;
659 init
.parent_names
= &parent_name
; /* Mux Y1 to input */
660 init
.name
= kasprintf(GFP_KERNEL
, "%s.Y1", client
->dev
.of_node
->name
);
661 data
->clk
[0].chip
= data
;
662 data
->clk
[0].hw
.init
= &init
;
663 data
->clk
[0].index
= 0;
664 data
->clk
[0].pdiv
= 1;
665 clk
= devm_clk_register(&client
->dev
, &data
->clk
[0].hw
);
666 kfree(init
.name
); /* clock framework made a copy of the name */
668 dev_err(&client
->dev
, "clock registration Y1 failed\n");
672 data
->dt_clk
[0] = clk
;
674 /* Register output clocks Y2 .. Y5*/
675 init
.ops
= &cdce925_clk_ops
;
676 init
.flags
= CLK_SET_RATE_PARENT
;
677 init
.num_parents
= 1;
678 for (i
= 1; i
< NUMBER_OF_OUTPUTS
; ++i
) {
679 init
.name
= kasprintf(GFP_KERNEL
, "%s.Y%d",
680 client
->dev
.of_node
->name
, i
+1);
681 data
->clk
[i
].chip
= data
;
682 data
->clk
[i
].hw
.init
= &init
;
683 data
->clk
[i
].index
= i
;
684 data
->clk
[i
].pdiv
= 1;
688 /* Mux Y2/3 to PLL1 */
689 init
.parent_names
= &pll_clk_name
[0];
693 /* Mux Y4/5 to PLL2 */
694 init
.parent_names
= &pll_clk_name
[1];
697 clk
= devm_clk_register(&client
->dev
, &data
->clk
[i
].hw
);
698 kfree(init
.name
); /* clock framework made a copy of the name */
700 dev_err(&client
->dev
, "clock registration failed\n");
704 data
->dt_clk
[i
] = clk
;
707 /* Register the output clocks */
708 data
->onecell
.clk_num
= NUMBER_OF_OUTPUTS
;
709 data
->onecell
.clks
= data
->dt_clk
;
710 err
= of_clk_add_provider(client
->dev
.of_node
, of_clk_src_onecell_get
,
713 dev_err(&client
->dev
, "unable to add OF clock provider\n");
718 for (i
= 0; i
< NUMBER_OF_PLLS
; ++i
)
719 /* clock framework made a copy of the name */
720 kfree(pll_clk_name
[i
]);
725 static const struct i2c_device_id cdce925_id
[] = {
729 MODULE_DEVICE_TABLE(i2c
, cdce925_id
);
731 static const struct of_device_id clk_cdce925_of_match
[] = {
732 { .compatible
= "ti,cdce925" },
735 MODULE_DEVICE_TABLE(of
, clk_cdce925_of_match
);
737 static struct i2c_driver cdce925_driver
= {
740 .of_match_table
= of_match_ptr(clk_cdce925_of_match
),
742 .probe
= cdce925_probe
,
743 .id_table
= cdce925_id
,
745 module_i2c_driver(cdce925_driver
);
747 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
748 MODULE_DESCRIPTION("cdce925 driver");
749 MODULE_LICENSE("GPL");