2 * Driver for TI Multi PLL CDCE913/925/937/949 clock synthesizer
4 * This driver always connects the Y1 to the input clock, Y2/Y3 to PLL1,
5 * Y4/Y5 to PLL2, and so on. 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.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <linux/gcd.h>
22 /* Each chip has different number of PLLs and outputs, for example:
23 * The CECE925 has 2 PLLs which can be routed through dividers to 5 outputs.
24 * Model this as 2 PLL clocks which are parents to the outputs.
34 struct clk_cdce925_chip_info {
39 static const struct clk_cdce925_chip_info clk_cdce925_chip_info_tbl[] = {
40 [CDCE913] = { .num_plls = 1, .num_outputs = 3 },
41 [CDCE925] = { .num_plls = 2, .num_outputs = 5 },
42 [CDCE937] = { .num_plls = 3, .num_outputs = 7 },
43 [CDCE949] = { .num_plls = 4, .num_outputs = 9 },
46 #define MAX_NUMBER_OF_PLLS 4
47 #define MAX_NUMBER_OF_OUTPUTS 9
49 #define CDCE925_REG_GLOBAL1 0x01
50 #define CDCE925_REG_Y1SPIPDIVH 0x02
51 #define CDCE925_REG_PDIVL 0x03
52 #define CDCE925_REG_XCSEL 0x05
53 /* PLL parameters start at 0x10, steps of 0x10 */
54 #define CDCE925_OFFSET_PLL 0x10
55 /* Add CDCE925_OFFSET_PLL * (pll) to these registers before sending */
56 #define CDCE925_PLL_MUX_OUTPUTS 0x14
57 #define CDCE925_PLL_MULDIV 0x18
59 #define CDCE925_PLL_FREQUENCY_MIN 80000000ul
60 #define CDCE925_PLL_FREQUENCY_MAX 230000000ul
61 struct clk_cdce925_chip;
63 struct clk_cdce925_output {
65 struct clk_cdce925_chip *chip;
67 u16 pdiv; /* 1..127 for Y2-Y9; 1..1023 for Y1 */
69 #define to_clk_cdce925_output(_hw) \
70 container_of(_hw, struct clk_cdce925_output, hw)
72 struct clk_cdce925_pll {
74 struct clk_cdce925_chip *chip;
79 #define to_clk_cdce925_pll(_hw) container_of(_hw, struct clk_cdce925_pll, hw)
81 struct clk_cdce925_chip {
82 struct regmap *regmap;
83 struct i2c_client *i2c_client;
84 const struct clk_cdce925_chip_info *chip_info;
85 struct clk_cdce925_pll pll[MAX_NUMBER_OF_PLLS];
86 struct clk_cdce925_output clk[MAX_NUMBER_OF_OUTPUTS];
89 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
91 static unsigned long cdce925_pll_calculate_rate(unsigned long parent_rate,
94 if ((!m || !n) || (m == n))
95 return parent_rate; /* In bypass mode runs at same frequency */
96 return mult_frac(parent_rate, (unsigned long)n, (unsigned long)m);
99 static unsigned long cdce925_pll_recalc_rate(struct clk_hw *hw,
100 unsigned long parent_rate)
102 /* Output frequency of PLL is Fout = (Fin/Pdiv)*(N/M) */
103 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
105 return cdce925_pll_calculate_rate(parent_rate, data->n, data->m);
108 static void cdce925_pll_find_rate(unsigned long rate,
109 unsigned long parent_rate, u16 *n, u16 *m)
115 if (rate <= parent_rate) {
116 /* Can always deliver parent_rate in bypass mode */
121 /* In PLL mode, need to apply min/max range */
122 if (rate < CDCE925_PLL_FREQUENCY_MIN)
123 rate = CDCE925_PLL_FREQUENCY_MIN;
124 else if (rate > CDCE925_PLL_FREQUENCY_MAX)
125 rate = CDCE925_PLL_FREQUENCY_MAX;
127 g = gcd(rate, parent_rate);
128 um = parent_rate / g;
130 /* When outside hw range, reduce to fit (rounding errors) */
131 while ((un > 4095) || (um > 511)) {
145 static long cdce925_pll_round_rate(struct clk_hw *hw, unsigned long rate,
146 unsigned long *parent_rate)
150 cdce925_pll_find_rate(rate, *parent_rate, &n, &m);
151 return (long)cdce925_pll_calculate_rate(*parent_rate, n, m);
154 static int cdce925_pll_set_rate(struct clk_hw *hw, unsigned long rate,
155 unsigned long parent_rate)
157 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
159 if (!rate || (rate == parent_rate)) {
160 data->m = 0; /* Bypass mode */
165 if ((rate < CDCE925_PLL_FREQUENCY_MIN) ||
166 (rate > CDCE925_PLL_FREQUENCY_MAX)) {
167 pr_debug("%s: rate %lu outside PLL range.\n", __func__, rate);
171 if (rate < parent_rate) {
172 pr_debug("%s: rate %lu less than parent rate %lu.\n", __func__,
177 cdce925_pll_find_rate(rate, parent_rate, &data->n, &data->m);
182 /* calculate p = max(0, 4 - int(log2 (n/m))) */
183 static u8 cdce925_pll_calc_p(u16 n, u16 m)
198 /* Returns VCO range bits for VCO1_0_RANGE */
199 static u8 cdce925_pll_calc_range_bits(struct clk_hw *hw, u16 n, u16 m)
201 struct clk *parent = clk_get_parent(hw->clk);
202 unsigned long rate = clk_get_rate(parent);
204 rate = mult_frac(rate, (unsigned long)n, (unsigned long)m);
205 if (rate >= 175000000)
207 if (rate >= 150000000)
209 if (rate >= 125000000)
214 /* I2C clock, hence everything must happen in (un)prepare because this
216 static int cdce925_pll_prepare(struct clk_hw *hw)
218 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
225 u8 pll[4]; /* Bits are spread out over 4 byte registers */
226 u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
229 if ((!m || !n) || (m == n)) {
230 /* Set PLL mux to bypass mode, leave the rest as is */
231 regmap_update_bits(data->chip->regmap,
232 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x80);
234 /* According to data sheet: */
235 /* p = max(0, 4 - int(log2 (n/m))) */
236 p = cdce925_pll_calc_p(n, m);
241 if ((q < 16) || (q > 63)) {
242 pr_debug("%s invalid q=%d\n", __func__, q);
247 pr_debug("%s invalid r=%d\n", __func__, r);
250 pr_debug("%s n=%d m=%d p=%d q=%d r=%d\n", __func__,
252 /* encode into register bits */
254 pll[1] = ((n & 0x0F) << 4) | ((r >> 5) & 0x0F);
255 pll[2] = ((r & 0x1F) << 3) | ((q >> 3) & 0x07);
256 pll[3] = ((q & 0x07) << 5) | (p << 2) |
257 cdce925_pll_calc_range_bits(hw, n, m);
258 /* Write to registers */
259 for (i = 0; i < ARRAY_SIZE(pll); ++i)
260 regmap_write(data->chip->regmap,
261 reg_ofs + CDCE925_PLL_MULDIV + i, pll[i]);
263 regmap_update_bits(data->chip->regmap,
264 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x00);
270 static void cdce925_pll_unprepare(struct clk_hw *hw)
272 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
273 u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
275 regmap_update_bits(data->chip->regmap,
276 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x80);
279 static const struct clk_ops cdce925_pll_ops = {
280 .prepare = cdce925_pll_prepare,
281 .unprepare = cdce925_pll_unprepare,
282 .recalc_rate = cdce925_pll_recalc_rate,
283 .round_rate = cdce925_pll_round_rate,
284 .set_rate = cdce925_pll_set_rate,
288 static void cdce925_clk_set_pdiv(struct clk_cdce925_output *data, u16 pdiv)
290 switch (data->index) {
292 regmap_update_bits(data->chip->regmap,
293 CDCE925_REG_Y1SPIPDIVH,
294 0x03, (pdiv >> 8) & 0x03);
295 regmap_write(data->chip->regmap, 0x03, pdiv & 0xFF);
298 regmap_update_bits(data->chip->regmap, 0x16, 0x7F, pdiv);
301 regmap_update_bits(data->chip->regmap, 0x17, 0x7F, pdiv);
304 regmap_update_bits(data->chip->regmap, 0x26, 0x7F, pdiv);
307 regmap_update_bits(data->chip->regmap, 0x27, 0x7F, pdiv);
310 regmap_update_bits(data->chip->regmap, 0x36, 0x7F, pdiv);
313 regmap_update_bits(data->chip->regmap, 0x37, 0x7F, pdiv);
316 regmap_update_bits(data->chip->regmap, 0x46, 0x7F, pdiv);
319 regmap_update_bits(data->chip->regmap, 0x47, 0x7F, pdiv);
324 static void cdce925_clk_activate(struct clk_cdce925_output *data)
326 switch (data->index) {
328 regmap_update_bits(data->chip->regmap,
329 CDCE925_REG_Y1SPIPDIVH, 0x0c, 0x0c);
333 regmap_update_bits(data->chip->regmap, 0x14, 0x03, 0x03);
337 regmap_update_bits(data->chip->regmap, 0x24, 0x03, 0x03);
341 regmap_update_bits(data->chip->regmap, 0x34, 0x03, 0x03);
345 regmap_update_bits(data->chip->regmap, 0x44, 0x03, 0x03);
350 static int cdce925_clk_prepare(struct clk_hw *hw)
352 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
354 cdce925_clk_set_pdiv(data, data->pdiv);
355 cdce925_clk_activate(data);
359 static void cdce925_clk_unprepare(struct clk_hw *hw)
361 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
363 /* Disable clock by setting divider to "0" */
364 cdce925_clk_set_pdiv(data, 0);
367 static unsigned long cdce925_clk_recalc_rate(struct clk_hw *hw,
368 unsigned long parent_rate)
370 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
373 return parent_rate / data->pdiv;
377 static u16 cdce925_calc_divider(unsigned long rate,
378 unsigned long parent_rate)
380 unsigned long divider;
384 if (rate >= parent_rate)
387 divider = DIV_ROUND_CLOSEST(parent_rate, rate);
394 static unsigned long cdce925_clk_best_parent_rate(
395 struct clk_hw *hw, unsigned long rate)
397 struct clk *pll = clk_get_parent(hw->clk);
398 struct clk *root = clk_get_parent(pll);
399 unsigned long root_rate = clk_get_rate(root);
400 unsigned long best_rate_error = rate;
406 if (root_rate % rate == 0)
407 return root_rate; /* Don't need the PLL, use bypass */
409 pdiv_min = (u16)max(1ul, DIV_ROUND_UP(CDCE925_PLL_FREQUENCY_MIN, rate));
410 pdiv_max = (u16)min(127ul, CDCE925_PLL_FREQUENCY_MAX / rate);
412 if (pdiv_min > pdiv_max)
413 return 0; /* No can do? */
415 pdiv_best = pdiv_min;
416 for (pdiv_now = pdiv_min; pdiv_now < pdiv_max; ++pdiv_now) {
417 unsigned long target_rate = rate * pdiv_now;
418 long pll_rate = clk_round_rate(pll, target_rate);
419 unsigned long actual_rate;
420 unsigned long rate_error;
424 actual_rate = pll_rate / pdiv_now;
425 rate_error = abs((long)actual_rate - (long)rate);
426 if (rate_error < best_rate_error) {
427 pdiv_best = pdiv_now;
428 best_rate_error = rate_error;
430 /* TODO: Consider PLL frequency based on smaller n/m values
431 * and pick the better one if the error is equal */
434 return rate * pdiv_best;
437 static long cdce925_clk_round_rate(struct clk_hw *hw, unsigned long rate,
438 unsigned long *parent_rate)
440 unsigned long l_parent_rate = *parent_rate;
441 u16 divider = cdce925_calc_divider(rate, l_parent_rate);
443 if (l_parent_rate / divider != rate) {
444 l_parent_rate = cdce925_clk_best_parent_rate(hw, rate);
445 divider = cdce925_calc_divider(rate, l_parent_rate);
446 *parent_rate = l_parent_rate;
450 return (long)(l_parent_rate / divider);
454 static int cdce925_clk_set_rate(struct clk_hw *hw, unsigned long rate,
455 unsigned long parent_rate)
457 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
459 data->pdiv = cdce925_calc_divider(rate, parent_rate);
464 static const struct clk_ops cdce925_clk_ops = {
465 .prepare = cdce925_clk_prepare,
466 .unprepare = cdce925_clk_unprepare,
467 .recalc_rate = cdce925_clk_recalc_rate,
468 .round_rate = cdce925_clk_round_rate,
469 .set_rate = cdce925_clk_set_rate,
473 static u16 cdce925_y1_calc_divider(unsigned long rate,
474 unsigned long parent_rate)
476 unsigned long divider;
480 if (rate >= parent_rate)
483 divider = DIV_ROUND_CLOSEST(parent_rate, rate);
484 if (divider > 0x3FF) /* Y1 has 10-bit divider */
490 static long cdce925_clk_y1_round_rate(struct clk_hw *hw, unsigned long rate,
491 unsigned long *parent_rate)
493 unsigned long l_parent_rate = *parent_rate;
494 u16 divider = cdce925_y1_calc_divider(rate, l_parent_rate);
497 return (long)(l_parent_rate / divider);
501 static int cdce925_clk_y1_set_rate(struct clk_hw *hw, unsigned long rate,
502 unsigned long parent_rate)
504 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
506 data->pdiv = cdce925_y1_calc_divider(rate, parent_rate);
511 static const struct clk_ops cdce925_clk_y1_ops = {
512 .prepare = cdce925_clk_prepare,
513 .unprepare = cdce925_clk_unprepare,
514 .recalc_rate = cdce925_clk_recalc_rate,
515 .round_rate = cdce925_clk_y1_round_rate,
516 .set_rate = cdce925_clk_y1_set_rate,
519 #define CDCE925_I2C_COMMAND_BLOCK_TRANSFER 0x00
520 #define CDCE925_I2C_COMMAND_BYTE_TRANSFER 0x80
522 static int cdce925_regmap_i2c_write(
523 void *context, const void *data, size_t count)
525 struct device *dev = context;
526 struct i2c_client *i2c = to_i2c_client(dev);
533 /* First byte is command code */
534 reg_data[0] = CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)data)[0];
535 reg_data[1] = ((u8 *)data)[1];
537 dev_dbg(&i2c->dev, "%s(%zu) %#x %#x\n", __func__, count,
538 reg_data[0], reg_data[1]);
540 ret = i2c_master_send(i2c, reg_data, count);
541 if (likely(ret == count))
549 static int cdce925_regmap_i2c_read(void *context,
550 const void *reg, size_t reg_size, void *val, size_t val_size)
552 struct device *dev = context;
553 struct i2c_client *i2c = to_i2c_client(dev);
554 struct i2c_msg xfer[2];
561 xfer[0].addr = i2c->addr;
563 xfer[0].buf = reg_data;
566 CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)reg)[0];
570 CDCE925_I2C_COMMAND_BLOCK_TRANSFER | ((u8 *)reg)[0];
571 reg_data[1] = val_size;
575 xfer[1].addr = i2c->addr;
576 xfer[1].flags = I2C_M_RD;
577 xfer[1].len = val_size;
580 ret = i2c_transfer(i2c->adapter, xfer, 2);
581 if (likely(ret == 2)) {
582 dev_dbg(&i2c->dev, "%s(%zu, %zu) %#x %#x\n", __func__,
583 reg_size, val_size, reg_data[0], *((u8 *)val));
591 static struct clk_hw *
592 of_clk_cdce925_get(struct of_phandle_args *clkspec, void *_data)
594 struct clk_cdce925_chip *data = _data;
595 unsigned int idx = clkspec->args[0];
597 if (idx >= ARRAY_SIZE(data->clk)) {
598 pr_err("%s: invalid index %u\n", __func__, idx);
599 return ERR_PTR(-EINVAL);
602 return &data->clk[idx].hw;
605 /* The CDCE925 uses a funky way to read/write registers. Bulk mode is
606 * just weird, so just use the single byte mode exclusively. */
607 static struct regmap_bus regmap_cdce925_bus = {
608 .write = cdce925_regmap_i2c_write,
609 .read = cdce925_regmap_i2c_read,
612 static int cdce925_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
615 struct clk_cdce925_chip *data;
616 struct device_node *node = client->dev.of_node;
617 const char *parent_name;
618 const char *pll_clk_name[MAX_NUMBER_OF_PLLS] = {NULL,};
619 struct clk_init_data init;
623 struct device_node *np_output;
625 struct regmap_config config = {
626 .name = "configuration0",
629 .cache_type = REGCACHE_RBTREE,
632 dev_dbg(&client->dev, "%s\n", __func__);
633 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
637 data->i2c_client = client;
638 data->chip_info = &clk_cdce925_chip_info_tbl[id->driver_data];
639 config.max_register = CDCE925_OFFSET_PLL +
640 data->chip_info->num_plls * 0x10 - 1;
641 data->regmap = devm_regmap_init(&client->dev, ®map_cdce925_bus,
642 &client->dev, &config);
643 if (IS_ERR(data->regmap)) {
644 dev_err(&client->dev, "failed to allocate register map\n");
645 return PTR_ERR(data->regmap);
647 i2c_set_clientdata(client, data);
649 parent_name = of_clk_get_parent_name(node, 0);
651 dev_err(&client->dev, "missing parent clock\n");
654 dev_dbg(&client->dev, "parent is: %s\n", parent_name);
656 if (of_property_read_u32(node, "xtal-load-pf", &value) == 0)
657 regmap_write(data->regmap,
658 CDCE925_REG_XCSEL, (value << 3) & 0xF8);
660 regmap_update_bits(data->regmap, CDCE925_REG_GLOBAL1, BIT(4), 0);
662 /* Set input source for Y1 to be the XTAL */
663 regmap_update_bits(data->regmap, 0x02, BIT(7), 0);
665 init.ops = &cdce925_pll_ops;
667 init.parent_names = &parent_name;
668 init.num_parents = 1;
670 /* Register PLL clocks */
671 for (i = 0; i < data->chip_info->num_plls; ++i) {
672 pll_clk_name[i] = kasprintf(GFP_KERNEL, "%pOFn.pll%d",
673 client->dev.of_node, i);
674 init.name = pll_clk_name[i];
675 data->pll[i].chip = data;
676 data->pll[i].hw.init = &init;
677 data->pll[i].index = i;
678 err = devm_clk_hw_register(&client->dev, &data->pll[i].hw);
680 dev_err(&client->dev, "Failed register PLL %d\n", i);
683 sprintf(child_name, "PLL%d", i+1);
684 np_output = of_get_child_by_name(node, child_name);
687 if (!of_property_read_u32(np_output,
688 "clock-frequency", &value)) {
689 err = clk_set_rate(data->pll[i].hw.clk, value);
691 dev_err(&client->dev,
692 "unable to set PLL frequency %ud\n",
695 if (!of_property_read_u32(np_output,
696 "spread-spectrum", &value)) {
697 u8 flag = of_property_read_bool(np_output,
698 "spread-spectrum-center") ? 0x80 : 0x00;
699 regmap_update_bits(data->regmap,
700 0x16 + (i*CDCE925_OFFSET_PLL),
702 regmap_update_bits(data->regmap,
703 0x12 + (i*CDCE925_OFFSET_PLL),
706 of_node_put(np_output);
709 /* Register output clock Y1 */
710 init.ops = &cdce925_clk_y1_ops;
712 init.num_parents = 1;
713 init.parent_names = &parent_name; /* Mux Y1 to input */
714 init.name = kasprintf(GFP_KERNEL, "%pOFn.Y1", client->dev.of_node);
715 data->clk[0].chip = data;
716 data->clk[0].hw.init = &init;
717 data->clk[0].index = 0;
718 data->clk[0].pdiv = 1;
719 err = devm_clk_hw_register(&client->dev, &data->clk[0].hw);
720 kfree(init.name); /* clock framework made a copy of the name */
722 dev_err(&client->dev, "clock registration Y1 failed\n");
726 /* Register output clocks Y2 .. Y5*/
727 init.ops = &cdce925_clk_ops;
728 init.flags = CLK_SET_RATE_PARENT;
729 init.num_parents = 1;
730 for (i = 1; i < data->chip_info->num_outputs; ++i) {
731 init.name = kasprintf(GFP_KERNEL, "%pOFn.Y%d",
732 client->dev.of_node, i+1);
733 data->clk[i].chip = data;
734 data->clk[i].hw.init = &init;
735 data->clk[i].index = i;
736 data->clk[i].pdiv = 1;
740 /* Mux Y2/3 to PLL1 */
741 init.parent_names = &pll_clk_name[0];
745 /* Mux Y4/5 to PLL2 */
746 init.parent_names = &pll_clk_name[1];
750 /* Mux Y6/7 to PLL3 */
751 init.parent_names = &pll_clk_name[2];
755 /* Mux Y8/9 to PLL4 */
756 init.parent_names = &pll_clk_name[3];
759 err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
760 kfree(init.name); /* clock framework made a copy of the name */
762 dev_err(&client->dev, "clock registration failed\n");
767 /* Register the output clocks */
768 err = of_clk_add_hw_provider(client->dev.of_node, of_clk_cdce925_get,
771 dev_err(&client->dev, "unable to add OF clock provider\n");
776 for (i = 0; i < data->chip_info->num_plls; ++i)
777 /* clock framework made a copy of the name */
778 kfree(pll_clk_name[i]);
783 static const struct i2c_device_id cdce925_id[] = {
784 { "cdce913", CDCE913 },
785 { "cdce925", CDCE925 },
786 { "cdce937", CDCE937 },
787 { "cdce949", CDCE949 },
790 MODULE_DEVICE_TABLE(i2c, cdce925_id);
792 static const struct of_device_id clk_cdce925_of_match[] = {
793 { .compatible = "ti,cdce913" },
794 { .compatible = "ti,cdce925" },
795 { .compatible = "ti,cdce937" },
796 { .compatible = "ti,cdce949" },
799 MODULE_DEVICE_TABLE(of, clk_cdce925_of_match);
801 static struct i2c_driver cdce925_driver = {
804 .of_match_table = of_match_ptr(clk_cdce925_of_match),
806 .probe = cdce925_probe,
807 .id_table = cdce925_id,
809 module_i2c_driver(cdce925_driver);
812 MODULE_DESCRIPTION("TI CDCE913/925/937/949 driver");
813 MODULE_LICENSE("GPL");