module: Convert symbol namespace to string literal
[linux.git] / drivers / clk / clk-cdce925.c
blobc51818c1af982619265306bbd418929a948a4c72
1 /*
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
11 * Licenced under GPL
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 {
29 int num_plls;
30 int num_outputs;
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 {
51 struct clk_hw hw;
52 struct clk_cdce925_chip *chip;
53 u8 index;
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 {
60 struct clk_hw hw;
61 struct clk_cdce925_chip *chip;
62 u8 index;
63 u16 m; /* 1..511 */
64 u16 n; /* 1..4095 */
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,
79 u16 n, u16 m)
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)
98 unsigned long un;
99 unsigned long um;
100 unsigned long g;
102 if (rate <= parent_rate) {
103 /* Can always deliver parent_rate in bypass mode */
104 *n = 0;
105 *m = 0;
106 } else {
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;
115 un = rate / g;
116 /* When outside hw range, reduce to fit (rounding errors) */
117 while ((un > 4095) || (um > 511)) {
118 un >>= 1;
119 um >>= 1;
121 if (un == 0)
122 un = 1;
123 if (um == 0)
124 um = 1;
126 *n = un;
127 *m = um;
131 static long cdce925_pll_round_rate(struct clk_hw *hw, unsigned long rate,
132 unsigned long *parent_rate)
134 u16 n, m;
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 */
147 data->n = 0;
148 return 0;
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);
154 return -EINVAL;
157 if (rate < parent_rate) {
158 pr_debug("%s: rate %lu less than parent rate %lu.\n", __func__,
159 rate, parent_rate);
160 return -EINVAL;
163 cdce925_pll_find_rate(rate, parent_rate, &data->n, &data->m);
164 return 0;
168 /* calculate p = max(0, 4 - int(log2 (n/m))) */
169 static u8 cdce925_pll_calc_p(u16 n, u16 m)
171 u8 p;
172 u16 r = n / m;
174 if (r >= 16)
175 return 0;
176 p = 4;
177 while (r > 1) {
178 r >>= 1;
179 --p;
181 return p;
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)
192 return 0x3;
193 if (rate >= 150000000)
194 return 0x02;
195 if (rate >= 125000000)
196 return 0x01;
197 return 0x00;
200 /* I2C clock, hence everything must happen in (un)prepare because this
201 * may sleep */
202 static int cdce925_pll_prepare(struct clk_hw *hw)
204 struct clk_cdce925_pll *data = to_clk_cdce925_pll(hw);
205 u16 n = data->n;
206 u16 m = data->m;
207 u16 r;
208 u8 q;
209 u8 p;
210 u16 nn;
211 u8 pll[4]; /* Bits are spread out over 4 byte registers */
212 u8 reg_ofs = data->index * CDCE925_OFFSET_PLL;
213 unsigned i;
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);
219 } else {
220 /* According to data sheet: */
221 /* p = max(0, 4 - int(log2 (n/m))) */
222 p = cdce925_pll_calc_p(n, m);
223 /* nn = n * 2^p */
224 nn = n * BIT(p);
225 /* q = int(nn/m) */
226 q = nn / m;
227 if ((q < 16) || (q > 63)) {
228 pr_debug("%s invalid q=%d\n", __func__, q);
229 return -EINVAL;
231 r = nn - (m*q);
232 if (r > 511) {
233 pr_debug("%s invalid r=%d\n", __func__, r);
234 return -EINVAL;
236 pr_debug("%s n=%d m=%d p=%d q=%d r=%d\n", __func__,
237 n, m, p, q, r);
238 /* encode into register bits */
239 pll[0] = n >> 4;
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]);
248 /* Enable PLL */
249 regmap_update_bits(data->chip->regmap,
250 reg_ofs + CDCE925_PLL_MUX_OUTPUTS, 0x80, 0x00);
253 return 0;
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) {
277 case 0:
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);
282 break;
283 case 1:
284 regmap_update_bits(data->chip->regmap, 0x16, 0x7F, pdiv);
285 break;
286 case 2:
287 regmap_update_bits(data->chip->regmap, 0x17, 0x7F, pdiv);
288 break;
289 case 3:
290 regmap_update_bits(data->chip->regmap, 0x26, 0x7F, pdiv);
291 break;
292 case 4:
293 regmap_update_bits(data->chip->regmap, 0x27, 0x7F, pdiv);
294 break;
295 case 5:
296 regmap_update_bits(data->chip->regmap, 0x36, 0x7F, pdiv);
297 break;
298 case 6:
299 regmap_update_bits(data->chip->regmap, 0x37, 0x7F, pdiv);
300 break;
301 case 7:
302 regmap_update_bits(data->chip->regmap, 0x46, 0x7F, pdiv);
303 break;
304 case 8:
305 regmap_update_bits(data->chip->regmap, 0x47, 0x7F, pdiv);
306 break;
310 static void cdce925_clk_activate(struct clk_cdce925_output *data)
312 switch (data->index) {
313 case 0:
314 regmap_update_bits(data->chip->regmap,
315 CDCE925_REG_Y1SPIPDIVH, 0x0c, 0x0c);
316 break;
317 case 1:
318 case 2:
319 regmap_update_bits(data->chip->regmap, 0x14, 0x03, 0x03);
320 break;
321 case 3:
322 case 4:
323 regmap_update_bits(data->chip->regmap, 0x24, 0x03, 0x03);
324 break;
325 case 5:
326 case 6:
327 regmap_update_bits(data->chip->regmap, 0x34, 0x03, 0x03);
328 break;
329 case 7:
330 case 8:
331 regmap_update_bits(data->chip->regmap, 0x44, 0x03, 0x03);
332 break;
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);
342 return 0;
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);
358 if (data->pdiv)
359 return parent_rate / data->pdiv;
360 return 0;
363 static u16 cdce925_calc_divider(unsigned long rate,
364 unsigned long parent_rate)
366 unsigned long divider;
368 if (!rate)
369 return 0;
370 if (rate >= parent_rate)
371 return 1;
373 divider = DIV_ROUND_CLOSEST(parent_rate, rate);
374 if (divider > 0x7F)
375 divider = 0x7F;
377 return (u16)divider;
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;
387 u16 pdiv_min;
388 u16 pdiv_max;
389 u16 pdiv_best;
390 u16 pdiv_now;
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;
408 if (pll_rate <= 0)
409 continue;
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;
435 if (divider)
436 return (long)(l_parent_rate / divider);
437 return 0;
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);
447 return 0;
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;
464 if (!rate)
465 return 0;
466 if (rate >= parent_rate)
467 return 1;
469 divider = DIV_ROUND_CLOSEST(parent_rate, rate);
470 if (divider > 0x3FF) /* Y1 has 10-bit divider */
471 divider = 0x3FF;
473 return (u16)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);
482 if (divider)
483 return (long)(l_parent_rate / divider);
484 return 0;
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);
494 return 0;
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);
513 int ret;
514 u8 reg_data[2];
516 if (count != 2)
517 return -ENOTSUPP;
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))
528 return 0;
529 else if (ret < 0)
530 return ret;
531 else
532 return -EIO;
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];
541 int ret;
542 u8 reg_data[2];
544 if (reg_size != 1)
545 return -ENOTSUPP;
547 xfer[0].addr = i2c->addr;
548 xfer[0].flags = 0;
549 xfer[0].buf = reg_data;
550 if (val_size == 1) {
551 reg_data[0] =
552 CDCE925_I2C_COMMAND_BYTE_TRANSFER | ((u8 *)reg)[0];
553 xfer[0].len = 1;
554 } else {
555 reg_data[0] =
556 CDCE925_I2C_COMMAND_BLOCK_TRANSFER | ((u8 *)reg)[0];
557 reg_data[1] = val_size;
558 xfer[0].len = 2;
561 xfer[1].addr = i2c->addr;
562 xfer[1].flags = I2C_M_RD;
563 xfer[1].len = val_size;
564 xfer[1].buf = val;
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));
570 return 0;
571 } else if (ret < 0)
572 return ret;
573 else
574 return -EIO;
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)
593 int err;
595 err = devm_regulator_get_enable(dev, name);
596 if (err)
597 dev_err_probe(dev, err, "Failed to enable %s:\n", name);
599 return err;
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;
616 u32 value;
617 int i;
618 int err;
619 struct device_node *np_output;
620 char child_name[6];
621 struct regmap_config config = {
622 .name = "configuration0",
623 .reg_bits = 8,
624 .val_bits = 8,
625 .cache_type = REGCACHE_MAPLE,
628 dev_dbg(&client->dev, "%s\n", __func__);
630 err = cdce925_regulator_enable(&client->dev, "vdd");
631 if (err)
632 return err;
634 err = cdce925_regulator_enable(&client->dev, "vddout");
635 if (err)
636 return err;
638 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
639 if (!data)
640 return -ENOMEM;
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, &regmap_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);
655 if (!parent_name) {
656 dev_err(&client->dev, "missing parent clock\n");
657 return -ENODEV;
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);
664 /* PWDN bit */
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;
671 init.flags = 0;
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]) {
680 err = -ENOMEM;
681 goto error;
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);
688 if (err) {
689 dev_err(&client->dev, "Failed register PLL %d\n", i);
690 goto error;
692 sprintf(child_name, "PLL%d", i+1);
693 np_output = of_get_child_by_name(node, child_name);
694 if (!np_output)
695 continue;
696 if (!of_property_read_u32(np_output,
697 "clock-frequency", &value)) {
698 err = clk_set_rate(data->pll[i].hw.clk, value);
699 if (err)
700 dev_err(&client->dev,
701 "unable to set PLL frequency %ud\n",
702 value);
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),
710 0x80, flag);
711 regmap_update_bits(data->regmap,
712 0x12 + (i*CDCE925_OFFSET_PLL),
713 0x07, value & 0x07);
715 of_node_put(np_output);
718 /* Register output clock Y1 */
719 init.ops = &cdce925_clk_y1_ops;
720 init.flags = 0;
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);
724 if (!init.name) {
725 err = -ENOMEM;
726 goto error;
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 */
734 if (err) {
735 dev_err(&client->dev, "clock registration Y1 failed\n");
736 goto error;
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);
746 if (!init.name) {
747 err = -ENOMEM;
748 goto error;
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;
754 switch (i) {
755 case 1:
756 case 2:
757 /* Mux Y2/3 to PLL1 */
758 init.parent_names = &pll_clk_name[0];
759 break;
760 case 3:
761 case 4:
762 /* Mux Y4/5 to PLL2 */
763 init.parent_names = &pll_clk_name[1];
764 break;
765 case 5:
766 case 6:
767 /* Mux Y6/7 to PLL3 */
768 init.parent_names = &pll_clk_name[2];
769 break;
770 case 7:
771 case 8:
772 /* Mux Y8/9 to PLL4 */
773 init.parent_names = &pll_clk_name[3];
774 break;
776 err = devm_clk_hw_register(&client->dev, &data->clk[i].hw);
777 kfree(init.name); /* clock framework made a copy of the name */
778 if (err) {
779 dev_err(&client->dev, "clock registration failed\n");
780 goto error;
784 /* Register the output clocks */
785 err = of_clk_add_hw_provider(client->dev.of_node, of_clk_cdce925_get,
786 data);
787 if (err)
788 dev_err(&client->dev, "unable to add OF clock provider\n");
790 err = 0;
792 error:
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]);
797 return err;
800 static const struct clk_cdce925_chip_info clk_cdce913_info = {
801 .num_plls = 1,
802 .num_outputs = 3,
805 static const struct clk_cdce925_chip_info clk_cdce925_info = {
806 .num_plls = 2,
807 .num_outputs = 5,
810 static const struct clk_cdce925_chip_info clk_cdce937_info = {
811 .num_plls = 3,
812 .num_outputs = 7,
815 static const struct clk_cdce925_chip_info clk_cdce949_info = {
816 .num_plls = 4,
817 .num_outputs = 9,
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 = {
839 .driver = {
840 .name = "cdce925",
841 .of_match_table = clk_cdce925_of_match,
843 .probe = cdce925_probe,
844 .id_table = cdce925_id,
846 module_i2c_driver(cdce925_driver);
848 MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
849 MODULE_DESCRIPTION("TI CDCE913/925/937/949 driver");
850 MODULE_LICENSE("GPL");