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/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/gcd.h>
23 /* Each chip has different number of PLLs and outputs, for example:
24 * The CECE925 has 2 PLLs which can be routed through dividers to 5 outputs.
25 * Model this as 2 PLL clocks which are parents to the outputs.
28 struct clk_cdce925_chip_info {
33 #define MAX_NUMBER_OF_PLLS 4
34 #define MAX_NUMBER_OF_OUTPUTS 9
36 #define CDCE925_REG_GLOBAL1 0x01
37 #define CDCE925_REG_Y1SPIPDIVH 0x02
38 #define CDCE925_REG_PDIVL 0x03
39 #define CDCE925_REG_XCSEL 0x05
40 /* PLL parameters start at 0x10, steps of 0x10 */
41 #define CDCE925_OFFSET_PLL 0x10
42 /* Add CDCE925_OFFSET_PLL * (pll) to these registers before sending */
43 #define CDCE925_PLL_MUX_OUTPUTS 0x14
44 #define CDCE925_PLL_MULDIV 0x18
46 #define CDCE925_PLL_FREQUENCY_MIN 80000000ul
47 #define CDCE925_PLL_FREQUENCY_MAX 230000000ul
48 struct clk_cdce925_chip;
50 struct clk_cdce925_output {
52 struct clk_cdce925_chip *chip;
54 u16 pdiv; /* 1..127 for Y2-Y9; 1..1023 for Y1 */
56 #define to_clk_cdce925_output(_hw) \
57 container_of(_hw, struct clk_cdce925_output, hw)
59 struct clk_cdce925_pll {
61 struct clk_cdce925_chip *chip;
66 #define to_clk_cdce925_pll(_hw) container_of(_hw, struct clk_cdce925_pll, hw)
68 struct clk_cdce925_chip {
69 struct regmap *regmap;
70 struct i2c_client *i2c_client;
71 const struct clk_cdce925_chip_info *chip_info;
72 struct clk_cdce925_pll pll[MAX_NUMBER_OF_PLLS];
73 struct clk_cdce925_output clk[MAX_NUMBER_OF_OUTPUTS];
76 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
78 static unsigned long cdce925_pll_calculate_rate(unsigned long parent_rate,
81 if ((!m || !n) || (m == n))
82 return parent_rate; /* In bypass mode runs at same frequency */
83 return mult_frac(parent_rate, (unsigned long)n, (unsigned long)m);
86 static unsigned long cdce925_pll_recalc_rate(struct clk_hw *hw,
87 unsigned long parent_rate)
89 /* Output frequency of PLL is Fout = (Fin/Pdiv)*(N/M) */
90 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
92 return cdce925_pll_calculate_rate(parent_rate, data->n, data->m);
95 static void cdce925_pll_find_rate(unsigned long rate,
96 unsigned long parent_rate, u16 *n, u16 *m)
102 if (rate <= parent_rate) {
103 /* Can always deliver parent_rate in bypass mode */
107 /* In PLL mode, need to apply min/max range */
108 if (rate < CDCE925_PLL_FREQUENCY_MIN)
109 rate = CDCE925_PLL_FREQUENCY_MIN;
110 else if (rate > CDCE925_PLL_FREQUENCY_MAX)
111 rate = CDCE925_PLL_FREQUENCY_MAX;
113 g = gcd(rate, parent_rate);
114 um = parent_rate / g;
116 /* When outside hw range, reduce to fit (rounding errors) */
117 while ((un > 4095) || (um > 511)) {
131 static long cdce925_pll_round_rate(struct clk_hw *hw, unsigned long rate,
132 unsigned long *parent_rate)
136 cdce925_pll_find_rate(rate, *parent_rate, &n, &m);
137 return (long)cdce925_pll_calculate_rate(*parent_rate, n, m);
140 static int cdce925_pll_set_rate(struct clk_hw *hw, unsigned long rate,
141 unsigned long parent_rate)
143 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
145 if (!rate || (rate == parent_rate)) {
146 data->m = 0; /* Bypass mode */
151 if ((rate < CDCE925_PLL_FREQUENCY_MIN) ||
152 (rate > CDCE925_PLL_FREQUENCY_MAX)) {
153 pr_debug("%s: rate %lu outside PLL range.\n", __func__, rate);
157 if (rate < parent_rate) {
158 pr_debug("%s: rate %lu less than parent rate %lu.\n", __func__,
163 cdce925_pll_find_rate(rate, parent_rate, &data->n, &data->m);
168 /* calculate p = max(0, 4 - int(log2 (n/m))) */
169 static u8 cdce925_pll_calc_p(u16 n, u16 m)
184 /* Returns VCO range bits for VCO1_0_RANGE */
185 static u8 cdce925_pll_calc_range_bits(struct clk_hw *hw, u16 n, u16 m)
187 struct clk *parent = clk_get_parent(hw->clk);
188 unsigned long rate = clk_get_rate(parent);
190 rate = mult_frac(rate, (unsigned long)n, (unsigned long)m);
191 if (rate >= 175000000)
193 if (rate >= 150000000)
195 if (rate >= 125000000)
200 /* I2C clock, hence everything must happen in (un)prepare because this
202 static int cdce925_pll_prepare(struct clk_hw *hw)
204 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
211 u8 pll[4]; /* Bits are spread out over 4 byte registers */
212 u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
215 if ((!m || !n) || (m == n)) {
216 /* Set PLL mux to bypass mode, leave the rest as is */
217 regmap_update_bits(data->chip->regmap,
218 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x80);
220 /* According to data sheet: */
221 /* p = max(0, 4 - int(log2 (n/m))) */
222 p = cdce925_pll_calc_p(n, m);
227 if ((q < 16) || (q > 63)) {
228 pr_debug("%s invalid q=%d\n", __func__, q);
233 pr_debug("%s invalid r=%d\n", __func__, r);
236 pr_debug("%s n=%d m=%d p=%d q=%d r=%d\n", __func__,
238 /* encode into register bits */
240 pll[1] = ((n & 0x0F) << 4) | ((r >> 5) & 0x0F);
241 pll[2] = ((r & 0x1F) << 3) | ((q >> 3) & 0x07);
242 pll[3] = ((q & 0x07) << 5) | (p << 2) |
243 cdce925_pll_calc_range_bits(hw, n, m);
244 /* Write to registers */
245 for (i = 0; i < ARRAY_SIZE(pll); ++i)
246 regmap_write(data->chip->regmap,
247 reg_ofs + CDCE925_PLL_MULDIV + i, pll[i]);
249 regmap_update_bits(data->chip->regmap,
250 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x00);
256 static void cdce925_pll_unprepare(struct clk_hw *hw)
258 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
259 u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
261 regmap_update_bits(data->chip->regmap,
262 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x80);
265 static const struct clk_ops cdce925_pll_ops = {
266 .prepare = cdce925_pll_prepare,
267 .unprepare = cdce925_pll_unprepare,
268 .recalc_rate = cdce925_pll_recalc_rate,
269 .round_rate = cdce925_pll_round_rate,
270 .set_rate = cdce925_pll_set_rate,
274 static void cdce925_clk_set_pdiv(struct clk_cdce925_output *data, u16 pdiv)
276 switch (data->index) {
278 regmap_update_bits(data->chip->regmap,
279 CDCE925_REG_Y1SPIPDIVH,
280 0x03, (pdiv >> 8) & 0x03);
281 regmap_write(data->chip->regmap, 0x03, pdiv & 0xFF);
284 regmap_update_bits(data->chip->regmap, 0x16, 0x7F, pdiv);
287 regmap_update_bits(data->chip->regmap, 0x17, 0x7F, pdiv);
290 regmap_update_bits(data->chip->regmap, 0x26, 0x7F, pdiv);
293 regmap_update_bits(data->chip->regmap, 0x27, 0x7F, pdiv);
296 regmap_update_bits(data->chip->regmap, 0x36, 0x7F, pdiv);
299 regmap_update_bits(data->chip->regmap, 0x37, 0x7F, pdiv);
302 regmap_update_bits(data->chip->regmap, 0x46, 0x7F, pdiv);
305 regmap_update_bits(data->chip->regmap, 0x47, 0x7F, pdiv);
310 static void cdce925_clk_activate(struct clk_cdce925_output *data)
312 switch (data->index) {
314 regmap_update_bits(data->chip->regmap,
315 CDCE925_REG_Y1SPIPDIVH, 0x0c, 0x0c);
319 regmap_update_bits(data->chip->regmap, 0x14, 0x03, 0x03);
323 regmap_update_bits(data->chip->regmap, 0x24, 0x03, 0x03);
327 regmap_update_bits(data->chip->regmap, 0x34, 0x03, 0x03);
331 regmap_update_bits(data->chip->regmap, 0x44, 0x03, 0x03);
336 static int cdce925_clk_prepare(struct clk_hw *hw)
338 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
340 cdce925_clk_set_pdiv(data, data->pdiv);
341 cdce925_clk_activate(data);
345 static void cdce925_clk_unprepare(struct clk_hw *hw)
347 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
349 /* Disable clock by setting divider to "0" */
350 cdce925_clk_set_pdiv(data, 0);
353 static unsigned long cdce925_clk_recalc_rate(struct clk_hw *hw,
354 unsigned long parent_rate)
356 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
359 return parent_rate / data->pdiv;
363 static u16 cdce925_calc_divider(unsigned long rate,
364 unsigned long parent_rate)
366 unsigned long divider;
370 if (rate >= parent_rate)
373 divider = DIV_ROUND_CLOSEST(parent_rate, rate);
380 static unsigned long cdce925_clk_best_parent_rate(
381 struct clk_hw *hw, unsigned long rate)
383 struct clk *pll = clk_get_parent(hw->clk);
384 struct clk *root = clk_get_parent(pll);
385 unsigned long root_rate = clk_get_rate(root);
386 unsigned long best_rate_error = rate;
392 if (root_rate % rate == 0)
393 return root_rate; /* Don't need the PLL, use bypass */
395 pdiv_min = (u16)max(1ul, DIV_ROUND_UP(CDCE925_PLL_FREQUENCY_MIN, rate));
396 pdiv_max = (u16)min(127ul, CDCE925_PLL_FREQUENCY_MAX / rate);
398 if (pdiv_min > pdiv_max)
399 return 0; /* No can do? */
401 pdiv_best = pdiv_min;
402 for (pdiv_now = pdiv_min; pdiv_now < pdiv_max; ++pdiv_now) {
403 unsigned long target_rate = rate * pdiv_now;
404 long pll_rate = clk_round_rate(pll, target_rate);
405 unsigned long actual_rate;
406 unsigned long rate_error;
410 actual_rate = pll_rate / pdiv_now;
411 rate_error = abs((long)actual_rate - (long)rate);
412 if (rate_error < best_rate_error) {
413 pdiv_best = pdiv_now;
414 best_rate_error = rate_error;
416 /* TODO: Consider PLL frequency based on smaller n/m values
417 * and pick the better one if the error is equal */
420 return rate * pdiv_best;
423 static long cdce925_clk_round_rate(struct clk_hw *hw, unsigned long rate,
424 unsigned long *parent_rate)
426 unsigned long l_parent_rate = *parent_rate;
427 u16 divider = cdce925_calc_divider(rate, l_parent_rate);
429 if (l_parent_rate / divider != rate) {
430 l_parent_rate = cdce925_clk_best_parent_rate(hw, rate);
431 divider = cdce925_calc_divider(rate, l_parent_rate);
432 *parent_rate = l_parent_rate;
436 return (long)(l_parent_rate / divider);
440 static int cdce925_clk_set_rate(struct clk_hw *hw, unsigned long rate,
441 unsigned long parent_rate)
443 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
445 data->pdiv = cdce925_calc_divider(rate, parent_rate);
450 static const struct clk_ops cdce925_clk_ops = {
451 .prepare = cdce925_clk_prepare,
452 .unprepare = cdce925_clk_unprepare,
453 .recalc_rate = cdce925_clk_recalc_rate,
454 .round_rate = cdce925_clk_round_rate,
455 .set_rate = cdce925_clk_set_rate,
459 static u16 cdce925_y1_calc_divider(unsigned long rate,
460 unsigned long parent_rate)
462 unsigned long divider;
466 if (rate >= parent_rate)
469 divider = DIV_ROUND_CLOSEST(parent_rate, rate);
470 if (divider > 0x3FF) /* Y1 has 10-bit divider */
476 static long cdce925_clk_y1_round_rate(struct clk_hw *hw, unsigned long rate,
477 unsigned long *parent_rate)
479 unsigned long l_parent_rate = *parent_rate;
480 u16 divider = cdce925_y1_calc_divider(rate, l_parent_rate);
483 return (long)(l_parent_rate / divider);
487 static int cdce925_clk_y1_set_rate(struct clk_hw *hw, unsigned long rate,
488 unsigned long parent_rate)
490 struct clk_cdce925_output *data = to_clk_cdce925_output(hw);
492 data->pdiv = cdce925_y1_calc_divider(rate, parent_rate);
497 static const struct clk_ops cdce925_clk_y1_ops = {
498 .prepare = cdce925_clk_prepare,
499 .unprepare = cdce925_clk_unprepare,
500 .recalc_rate = cdce925_clk_recalc_rate,
501 .round_rate = cdce925_clk_y1_round_rate,
502 .set_rate = cdce925_clk_y1_set_rate,
505 #define CDCE925_I2C_COMMAND_BLOCK_TRANSFER 0x00
506 #define CDCE925_I2C_COMMAND_BYTE_TRANSFER 0x80
508 static int cdce925_regmap_i2c_write(
509 void *context, const void *data, size_t count)
511 struct device *dev = context;
512 struct i2c_client *i2c = to_i2c_client(dev);
519 /* First byte is command code */
520 reg_data[0] = CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)data)[0];
521 reg_data[1] = ((u8 *)data)[1];
523 dev_dbg(&i2c->dev, "%s(%zu) %#x %#x\n", __func__, count,
524 reg_data[0], reg_data[1]);
526 ret = i2c_master_send(i2c, reg_data, count);
527 if (likely(ret == count))
535 static int cdce925_regmap_i2c_read(void *context,
536 const void *reg, size_t reg_size, void *val, size_t val_size)
538 struct device *dev = context;
539 struct i2c_client *i2c = to_i2c_client(dev);
540 struct i2c_msg xfer[2];
547 xfer[0].addr = i2c->addr;
549 xfer[0].buf = reg_data;
552 CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)reg)[0];
556 CDCE925_I2C_COMMAND_BLOCK_TRANSFER | ((u8 *)reg)[0];
557 reg_data[1] = val_size;
561 xfer[1].addr = i2c->addr;
562 xfer[1].flags = I2C_M_RD;
563 xfer[1].len = val_size;
566 ret = i2c_transfer(i2c->adapter, xfer, 2);
567 if (likely(ret == 2)) {
568 dev_dbg(&i2c->dev, "%s(%zu, %zu) %#x %#x\n", __func__,
569 reg_size, val_size, reg_data[0], *((u8 *)val));
577 static struct clk_hw *
578 of_clk_cdce925_get(struct of_phandle_args *clkspec, void *_data)
580 struct clk_cdce925_chip *data = _data;
581 unsigned int idx = clkspec->args[0];
583 if (idx >= ARRAY_SIZE(data->clk)) {
584 pr_err("%s: invalid index %u\n", __func__, idx);
585 return ERR_PTR(-EINVAL);
588 return &data->clk[idx].hw;
591 static int cdce925_regulator_enable(struct device *dev, const char *name)
595 err = devm_regulator_get_enable(dev, name);
597 dev_err_probe(dev, err, "Failed to enable %s:\n", name);
602 /* The CDCE925 uses a funky way to read/write registers. Bulk mode is
603 * just weird, so just use the single byte mode exclusively. */
604 static const struct regmap_bus regmap_cdce925_bus = {
605 .write = cdce925_regmap_i2c_write,
606 .read = cdce925_regmap_i2c_read,
609 static int cdce925_probe(struct i2c_client *client)
611 struct clk_cdce925_chip *data;
612 struct device_node *node = client->dev.of_node;
613 const char *parent_name;
614 const char *pll_clk_name[MAX_NUMBER_OF_PLLS] = {NULL,};
615 struct clk_init_data init;
619 struct device_node *np_output;
621 struct regmap_config config = {
622 .name = "configuration0",
625 .cache_type = REGCACHE_MAPLE,
628 dev_dbg(&client->dev, "%s\n", __func__);
630 err = cdce925_regulator_enable(&client->dev, "vdd");
634 err = cdce925_regulator_enable(&client->dev, "vddout");
638 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
642 data->i2c_client = client;
643 data->chip_info = i2c_get_match_data(client);
644 config.max_register = CDCE925_OFFSET_PLL +
645 data->chip_info->num_plls * 0x10 - 1;
646 data->regmap = devm_regmap_init(&client->dev, ®map_cdce925_bus,
647 &client->dev, &config);
648 if (IS_ERR(data->regmap)) {
649 dev_err(&client->dev, "failed to allocate register map\n");
650 return PTR_ERR(data->regmap);
652 i2c_set_clientdata(client, data);
654 parent_name = of_clk_get_parent_name(node, 0);
656 dev_err(&client->dev, "missing parent clock\n");
659 dev_dbg(&client->dev, "parent is: %s\n", parent_name);
661 if (of_property_read_u32(node, "xtal-load-pf", &value) == 0)
662 regmap_write(data->regmap,
663 CDCE925_REG_XCSEL, (value << 3) & 0xF8);
665 regmap_update_bits(data->regmap, CDCE925_REG_GLOBAL1, BIT(4), 0);
667 /* Set input source for Y1 to be the XTAL */
668 regmap_update_bits(data->regmap, 0x02, BIT(7), 0);
670 init.ops = &cdce925_pll_ops;
672 init.parent_names = &parent_name;
673 init.num_parents = 1;
675 /* Register PLL clocks */
676 for (i = 0; i < data->chip_info->num_plls; ++i) {
677 pll_clk_name[i] = kasprintf(GFP_KERNEL, "%pOFn.pll%d",
678 client->dev.of_node, i);
679 if (!pll_clk_name[i]) {
683 init.name = pll_clk_name[i];
684 data->pll[i].chip = data;
685 data->pll[i].hw.init = &init;
686 data->pll[i].index = i;
687 err = devm_clk_hw_register(&client->dev, &data->pll[i].hw);
689 dev_err(&client->dev, "Failed register PLL %d\n", i);
692 sprintf(child_name, "PLL%d", i+1);
693 np_output = of_get_child_by_name(node, child_name);
696 if (!of_property_read_u32(np_output,
697 "clock-frequency", &value)) {
698 err = clk_set_rate(data->pll[i].hw.clk, value);
700 dev_err(&client->dev,
701 "unable to set PLL frequency %ud\n",
704 if (!of_property_read_u32(np_output,
705 "spread-spectrum", &value)) {
706 u8 flag = of_property_read_bool(np_output,
707 "spread-spectrum-center") ? 0x80 : 0x00;
708 regmap_update_bits(data->regmap,
709 0x16 + (i*CDCE925_OFFSET_PLL),
711 regmap_update_bits(data->regmap,
712 0x12 + (i*CDCE925_OFFSET_PLL),
715 of_node_put(np_output);
718 /* Register output clock Y1 */
719 init.ops = &cdce925_clk_y1_ops;
721 init.num_parents = 1;
722 init.parent_names = &parent_name; /* Mux Y1 to input */
723 init.name = kasprintf(GFP_KERNEL, "%pOFn.Y1", client->dev.of_node);
728 data->clk[0].chip = data;
729 data->clk[0].hw.init = &init;
730 data->clk[0].index = 0;
731 data->clk[0].pdiv = 1;
732 err = devm_clk_hw_register(&client->dev, &data->clk[0].hw);
733 kfree(init.name); /* clock framework made a copy of the name */
735 dev_err(&client->dev, "clock registration Y1 failed\n");
739 /* Register output clocks Y2 .. Y5*/
740 init.ops = &cdce925_clk_ops;
741 init.flags = CLK_SET_RATE_PARENT;
742 init.num_parents = 1;
743 for (i = 1; i < data->chip_info->num_outputs; ++i) {
744 init.name = kasprintf(GFP_KERNEL, "%pOFn.Y%d",
745 client->dev.of_node, i+1);
750 data->clk[i].chip = data;
751 data->clk[i].hw.init = &init;
752 data->clk[i].index = i;
753 data->clk[i].pdiv = 1;
757 /* Mux Y2/3 to PLL1 */
758 init.parent_names = &pll_clk_name[0];
762 /* Mux Y4/5 to PLL2 */
763 init.parent_names = &pll_clk_name[1];
767 /* Mux Y6/7 to PLL3 */
768 init.parent_names = &pll_clk_name[2];
772 /* Mux Y8/9 to PLL4 */
773 init.parent_names = &pll_clk_name[3];
776 err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
777 kfree(init.name); /* clock framework made a copy of the name */
779 dev_err(&client->dev, "clock registration failed\n");
784 /* Register the output clocks */
785 err = of_clk_add_hw_provider(client->dev.of_node, of_clk_cdce925_get,
788 dev_err(&client->dev, "unable to add OF clock provider\n");
793 for (i = 0; i < data->chip_info->num_plls; ++i)
794 /* clock framework made a copy of the name */
795 kfree(pll_clk_name[i]);
800 static const struct clk_cdce925_chip_info clk_cdce913_info = {
805 static const struct clk_cdce925_chip_info clk_cdce925_info = {
810 static const struct clk_cdce925_chip_info clk_cdce937_info = {
815 static const struct clk_cdce925_chip_info clk_cdce949_info = {
820 static const struct i2c_device_id cdce925_id[] = {
821 { "cdce913", (kernel_ulong_t)&clk_cdce913_info },
822 { "cdce925", (kernel_ulong_t)&clk_cdce925_info },
823 { "cdce937", (kernel_ulong_t)&clk_cdce937_info },
824 { "cdce949", (kernel_ulong_t)&clk_cdce949_info },
827 MODULE_DEVICE_TABLE(i2c, cdce925_id);
829 static const struct of_device_id clk_cdce925_of_match[] = {
830 { .compatible = "ti,cdce913", .data = &clk_cdce913_info },
831 { .compatible = "ti,cdce925", .data = &clk_cdce925_info },
832 { .compatible = "ti,cdce937", .data = &clk_cdce937_info },
833 { .compatible = "ti,cdce949", .data = &clk_cdce949_info },
836 MODULE_DEVICE_TABLE(of, clk_cdce925_of_match);
838 static struct i2c_driver cdce925_driver = {
841 .of_match_table = clk_cdce925_of_match,
843 .probe = cdce925_probe,
844 .id_table = cdce925_id,
846 module_i2c_driver(cdce925_driver);
849 MODULE_DESCRIPTION("TI CDCE913/925/937/949 driver");
850 MODULE_LICENSE("GPL");