2 * TI CDCE706 programmable 3-PLL clock synthesizer driver
4 * Copyright (c) 2014 Cadence Design Systems Inc.
6 * Reference: http://www.ti.com/lit/ds/symlink/cdce706.pdf
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
21 #include <linux/rational.h>
22 #include <linux/regmap.h>
23 #include <linux/slab.h>
25 #define CDCE706_CLKIN_CLOCK 10
26 #define CDCE706_CLKIN_SOURCE 11
27 #define CDCE706_PLL_M_LOW(pll) (1 + 3 * (pll))
28 #define CDCE706_PLL_N_LOW(pll) (2 + 3 * (pll))
29 #define CDCE706_PLL_HI(pll) (3 + 3 * (pll))
30 #define CDCE706_PLL_MUX 3
31 #define CDCE706_PLL_FVCO 6
32 #define CDCE706_DIVIDER(div) (13 + (div))
33 #define CDCE706_CLKOUT(out) (19 + (out))
35 #define CDCE706_CLKIN_CLOCK_MASK 0x10
36 #define CDCE706_CLKIN_SOURCE_SHIFT 6
37 #define CDCE706_CLKIN_SOURCE_MASK 0xc0
38 #define CDCE706_CLKIN_SOURCE_LVCMOS 0x40
40 #define CDCE706_PLL_MUX_MASK(pll) (0x80 >> (pll))
41 #define CDCE706_PLL_LOW_M_MASK 0xff
42 #define CDCE706_PLL_LOW_N_MASK 0xff
43 #define CDCE706_PLL_HI_M_MASK 0x1
44 #define CDCE706_PLL_HI_N_MASK 0x1e
45 #define CDCE706_PLL_HI_N_SHIFT 1
46 #define CDCE706_PLL_M_MAX 0x1ff
47 #define CDCE706_PLL_N_MAX 0xfff
48 #define CDCE706_PLL_FVCO_MASK(pll) (0x80 >> (pll))
49 #define CDCE706_PLL_FREQ_MIN 80000000
50 #define CDCE706_PLL_FREQ_MAX 300000000
51 #define CDCE706_PLL_FREQ_HI 180000000
53 #define CDCE706_DIVIDER_PLL(div) (9 + (div) - ((div) > 2) - ((div) > 4))
54 #define CDCE706_DIVIDER_PLL_SHIFT(div) ((div) < 2 ? 5 : 3 * ((div) & 1))
55 #define CDCE706_DIVIDER_PLL_MASK(div) (0x7 << CDCE706_DIVIDER_PLL_SHIFT(div))
56 #define CDCE706_DIVIDER_DIVIDER_MASK 0x7f
57 #define CDCE706_DIVIDER_DIVIDER_MAX 0x7f
59 #define CDCE706_CLKOUT_DIVIDER_MASK 0x7
60 #define CDCE706_CLKOUT_ENABLE_MASK 0x8
62 static const struct regmap_config cdce706_regmap_config
= {
65 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
68 #define to_hw_data(phw) (container_of((phw), struct cdce706_hw_data, hw))
70 struct cdce706_hw_data
{
71 struct cdce706_dev_data
*dev_data
;
80 struct cdce706_dev_data
{
81 struct i2c_client
*client
;
82 struct regmap
*regmap
;
83 struct clk
*clkin_clk
[2];
84 const char *clkin_name
[2];
85 struct cdce706_hw_data clkin
[1];
86 struct cdce706_hw_data pll
[3];
87 struct cdce706_hw_data divider
[6];
88 struct cdce706_hw_data clkout
[6];
91 static const char * const cdce706_source_name
[] = {
95 static const char * const cdce706_clkin_name
[] = {
99 static const char * const cdce706_pll_name
[] = {
100 "pll1", "pll2", "pll3",
103 static const char * const cdce706_divider_parent_name
[] = {
104 "clk_in", "pll1", "pll2", "pll2", "pll3",
107 static const char *cdce706_divider_name
[] = {
108 "p0", "p1", "p2", "p3", "p4", "p5",
111 static const char * const cdce706_clkout_name
[] = {
112 "clk_out0", "clk_out1", "clk_out2", "clk_out3", "clk_out4", "clk_out5",
115 static int cdce706_reg_read(struct cdce706_dev_data
*dev_data
, unsigned reg
,
118 int rc
= regmap_read(dev_data
->regmap
, reg
| 0x80, val
);
121 dev_err(&dev_data
->client
->dev
, "error reading reg %u", reg
);
125 static int cdce706_reg_write(struct cdce706_dev_data
*dev_data
, unsigned reg
,
128 int rc
= regmap_write(dev_data
->regmap
, reg
| 0x80, val
);
131 dev_err(&dev_data
->client
->dev
, "error writing reg %u", reg
);
135 static int cdce706_reg_update(struct cdce706_dev_data
*dev_data
, unsigned reg
,
136 unsigned mask
, unsigned val
)
138 int rc
= regmap_update_bits(dev_data
->regmap
, reg
| 0x80, mask
, val
);
141 dev_err(&dev_data
->client
->dev
, "error updating reg %u", reg
);
145 static int cdce706_clkin_set_parent(struct clk_hw
*hw
, u8 index
)
147 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
153 static u8
cdce706_clkin_get_parent(struct clk_hw
*hw
)
155 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
160 static const struct clk_ops cdce706_clkin_ops
= {
161 .set_parent
= cdce706_clkin_set_parent
,
162 .get_parent
= cdce706_clkin_get_parent
,
165 static unsigned long cdce706_pll_recalc_rate(struct clk_hw
*hw
,
166 unsigned long parent_rate
)
168 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
170 dev_dbg(&hwd
->dev_data
->client
->dev
,
171 "%s, pll: %d, mux: %d, mul: %u, div: %u\n",
172 __func__
, hwd
->idx
, hwd
->mux
, hwd
->mul
, hwd
->div
);
175 if (hwd
->div
&& hwd
->mul
) {
176 u64 res
= (u64
)parent_rate
* hwd
->mul
;
178 do_div(res
, hwd
->div
);
183 return parent_rate
/ hwd
->div
;
188 static long cdce706_pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
189 unsigned long *parent_rate
)
191 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
192 unsigned long mul
, div
;
195 dev_dbg(&hwd
->dev_data
->client
->dev
,
196 "%s, rate: %lu, parent_rate: %lu\n",
197 __func__
, rate
, *parent_rate
);
199 rational_best_approximation(rate
, *parent_rate
,
200 CDCE706_PLL_N_MAX
, CDCE706_PLL_M_MAX
,
205 dev_dbg(&hwd
->dev_data
->client
->dev
,
206 "%s, pll: %d, mul: %lu, div: %lu\n",
207 __func__
, hwd
->idx
, mul
, div
);
209 res
= (u64
)*parent_rate
* hwd
->mul
;
210 do_div(res
, hwd
->div
);
214 static int cdce706_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
215 unsigned long parent_rate
)
217 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
218 unsigned long mul
= hwd
->mul
, div
= hwd
->div
;
221 dev_dbg(&hwd
->dev_data
->client
->dev
,
222 "%s, pll: %d, mul: %lu, div: %lu\n",
223 __func__
, hwd
->idx
, mul
, div
);
225 err
= cdce706_reg_update(hwd
->dev_data
,
226 CDCE706_PLL_HI(hwd
->idx
),
227 CDCE706_PLL_HI_M_MASK
| CDCE706_PLL_HI_N_MASK
,
228 ((div
>> 8) & CDCE706_PLL_HI_M_MASK
) |
229 ((mul
>> (8 - CDCE706_PLL_HI_N_SHIFT
)) &
230 CDCE706_PLL_HI_N_MASK
));
234 err
= cdce706_reg_write(hwd
->dev_data
,
235 CDCE706_PLL_M_LOW(hwd
->idx
),
236 div
& CDCE706_PLL_LOW_M_MASK
);
240 err
= cdce706_reg_write(hwd
->dev_data
,
241 CDCE706_PLL_N_LOW(hwd
->idx
),
242 mul
& CDCE706_PLL_LOW_N_MASK
);
246 err
= cdce706_reg_update(hwd
->dev_data
,
248 CDCE706_PLL_FVCO_MASK(hwd
->idx
),
249 rate
> CDCE706_PLL_FREQ_HI
?
250 CDCE706_PLL_FVCO_MASK(hwd
->idx
) : 0);
254 static const struct clk_ops cdce706_pll_ops
= {
255 .recalc_rate
= cdce706_pll_recalc_rate
,
256 .round_rate
= cdce706_pll_round_rate
,
257 .set_rate
= cdce706_pll_set_rate
,
260 static int cdce706_divider_set_parent(struct clk_hw
*hw
, u8 index
)
262 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
264 if (hwd
->parent
== index
)
267 return cdce706_reg_update(hwd
->dev_data
,
268 CDCE706_DIVIDER_PLL(hwd
->idx
),
269 CDCE706_DIVIDER_PLL_MASK(hwd
->idx
),
270 index
<< CDCE706_DIVIDER_PLL_SHIFT(hwd
->idx
));
273 static u8
cdce706_divider_get_parent(struct clk_hw
*hw
)
275 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
280 static unsigned long cdce706_divider_recalc_rate(struct clk_hw
*hw
,
281 unsigned long parent_rate
)
283 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
285 dev_dbg(&hwd
->dev_data
->client
->dev
,
286 "%s, divider: %d, div: %u\n",
287 __func__
, hwd
->idx
, hwd
->div
);
289 return parent_rate
/ hwd
->div
;
293 static long cdce706_divider_round_rate(struct clk_hw
*hw
, unsigned long rate
,
294 unsigned long *parent_rate
)
296 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
297 struct cdce706_dev_data
*cdce
= hwd
->dev_data
;
298 unsigned long mul
, div
;
300 dev_dbg(&hwd
->dev_data
->client
->dev
,
301 "%s, rate: %lu, parent_rate: %lu\n",
302 __func__
, rate
, *parent_rate
);
304 rational_best_approximation(rate
, *parent_rate
,
305 1, CDCE706_DIVIDER_DIVIDER_MAX
,
308 div
= CDCE706_DIVIDER_DIVIDER_MAX
;
310 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
) {
311 unsigned long best_diff
= rate
;
312 unsigned long best_div
= 0;
313 struct clk
*gp_clk
= cdce
->clkin_clk
[cdce
->clkin
[0].parent
];
314 unsigned long gp_rate
= gp_clk
? clk_get_rate(gp_clk
) : 0;
316 for (div
= CDCE706_PLL_FREQ_MIN
/ rate
; best_diff
&&
317 div
<= CDCE706_PLL_FREQ_MAX
/ rate
; ++div
) {
320 unsigned long div_rate
;
323 if (rate
* div
< CDCE706_PLL_FREQ_MIN
)
326 rational_best_approximation(rate
* div
, gp_rate
,
330 div_rate64
= (u64
)gp_rate
* n
;
331 do_div(div_rate64
, m
);
332 do_div(div_rate64
, div
);
333 div_rate
= div_rate64
;
334 diff
= max(div_rate
, rate
) - min(div_rate
, rate
);
336 if (diff
< best_diff
) {
339 dev_dbg(&hwd
->dev_data
->client
->dev
,
340 "%s, %lu * %lu / %lu / %lu = %lu\n",
341 __func__
, gp_rate
, n
, m
, div
, div_rate
);
347 dev_dbg(&hwd
->dev_data
->client
->dev
,
348 "%s, altering parent rate: %lu -> %lu\n",
349 __func__
, *parent_rate
, rate
* div
);
350 *parent_rate
= rate
* div
;
354 dev_dbg(&hwd
->dev_data
->client
->dev
,
355 "%s, divider: %d, div: %lu\n",
356 __func__
, hwd
->idx
, div
);
358 return *parent_rate
/ div
;
361 static int cdce706_divider_set_rate(struct clk_hw
*hw
, unsigned long rate
,
362 unsigned long parent_rate
)
364 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
366 dev_dbg(&hwd
->dev_data
->client
->dev
,
367 "%s, divider: %d, div: %u\n",
368 __func__
, hwd
->idx
, hwd
->div
);
370 return cdce706_reg_update(hwd
->dev_data
,
371 CDCE706_DIVIDER(hwd
->idx
),
372 CDCE706_DIVIDER_DIVIDER_MASK
,
376 static const struct clk_ops cdce706_divider_ops
= {
377 .set_parent
= cdce706_divider_set_parent
,
378 .get_parent
= cdce706_divider_get_parent
,
379 .recalc_rate
= cdce706_divider_recalc_rate
,
380 .round_rate
= cdce706_divider_round_rate
,
381 .set_rate
= cdce706_divider_set_rate
,
384 static int cdce706_clkout_prepare(struct clk_hw
*hw
)
386 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
388 return cdce706_reg_update(hwd
->dev_data
, CDCE706_CLKOUT(hwd
->idx
),
389 CDCE706_CLKOUT_ENABLE_MASK
,
390 CDCE706_CLKOUT_ENABLE_MASK
);
393 static void cdce706_clkout_unprepare(struct clk_hw
*hw
)
395 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
397 cdce706_reg_update(hwd
->dev_data
, CDCE706_CLKOUT(hwd
->idx
),
398 CDCE706_CLKOUT_ENABLE_MASK
, 0);
401 static int cdce706_clkout_set_parent(struct clk_hw
*hw
, u8 index
)
403 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
405 if (hwd
->parent
== index
)
408 return cdce706_reg_update(hwd
->dev_data
,
409 CDCE706_CLKOUT(hwd
->idx
),
410 CDCE706_CLKOUT_ENABLE_MASK
, index
);
413 static u8
cdce706_clkout_get_parent(struct clk_hw
*hw
)
415 struct cdce706_hw_data
*hwd
= to_hw_data(hw
);
420 static unsigned long cdce706_clkout_recalc_rate(struct clk_hw
*hw
,
421 unsigned long parent_rate
)
426 static long cdce706_clkout_round_rate(struct clk_hw
*hw
, unsigned long rate
,
427 unsigned long *parent_rate
)
433 static int cdce706_clkout_set_rate(struct clk_hw
*hw
, unsigned long rate
,
434 unsigned long parent_rate
)
439 static const struct clk_ops cdce706_clkout_ops
= {
440 .prepare
= cdce706_clkout_prepare
,
441 .unprepare
= cdce706_clkout_unprepare
,
442 .set_parent
= cdce706_clkout_set_parent
,
443 .get_parent
= cdce706_clkout_get_parent
,
444 .recalc_rate
= cdce706_clkout_recalc_rate
,
445 .round_rate
= cdce706_clkout_round_rate
,
446 .set_rate
= cdce706_clkout_set_rate
,
449 static int cdce706_register_hw(struct cdce706_dev_data
*cdce
,
450 struct cdce706_hw_data
*hw
, unsigned num_hw
,
451 const char * const *clk_names
,
452 struct clk_init_data
*init
)
457 for (i
= 0; i
< num_hw
; ++i
, ++hw
) {
458 init
->name
= clk_names
[i
];
462 ret
= devm_clk_hw_register(&cdce
->client
->dev
,
465 dev_err(&cdce
->client
->dev
, "Failed to register %s\n",
473 static int cdce706_register_clkin(struct cdce706_dev_data
*cdce
)
475 struct clk_init_data init
= {
476 .ops
= &cdce706_clkin_ops
,
477 .parent_names
= cdce
->clkin_name
,
478 .num_parents
= ARRAY_SIZE(cdce
->clkin_name
),
482 unsigned clock
, source
;
484 for (i
= 0; i
< ARRAY_SIZE(cdce
->clkin_name
); ++i
) {
485 struct clk
*parent
= devm_clk_get(&cdce
->client
->dev
,
486 cdce706_source_name
[i
]);
488 if (IS_ERR(parent
)) {
489 cdce
->clkin_name
[i
] = cdce706_source_name
[i
];
491 cdce
->clkin_name
[i
] = __clk_get_name(parent
);
492 cdce
->clkin_clk
[i
] = parent
;
496 ret
= cdce706_reg_read(cdce
, CDCE706_CLKIN_SOURCE
, &source
);
499 if ((source
& CDCE706_CLKIN_SOURCE_MASK
) ==
500 CDCE706_CLKIN_SOURCE_LVCMOS
) {
501 ret
= cdce706_reg_read(cdce
, CDCE706_CLKIN_CLOCK
, &clock
);
504 cdce
->clkin
[0].parent
= !!(clock
& CDCE706_CLKIN_CLOCK_MASK
);
507 ret
= cdce706_register_hw(cdce
, cdce
->clkin
,
508 ARRAY_SIZE(cdce
->clkin
),
509 cdce706_clkin_name
, &init
);
513 static int cdce706_register_plls(struct cdce706_dev_data
*cdce
)
515 struct clk_init_data init
= {
516 .ops
= &cdce706_pll_ops
,
517 .parent_names
= cdce706_clkin_name
,
518 .num_parents
= ARRAY_SIZE(cdce706_clkin_name
),
524 ret
= cdce706_reg_read(cdce
, CDCE706_PLL_MUX
, &mux
);
528 for (i
= 0; i
< ARRAY_SIZE(cdce
->pll
); ++i
) {
531 ret
= cdce706_reg_read(cdce
, CDCE706_PLL_M_LOW(i
), &m
);
534 ret
= cdce706_reg_read(cdce
, CDCE706_PLL_N_LOW(i
), &n
);
537 ret
= cdce706_reg_read(cdce
, CDCE706_PLL_HI(i
), &v
);
540 cdce
->pll
[i
].div
= m
| ((v
& CDCE706_PLL_HI_M_MASK
) << 8);
541 cdce
->pll
[i
].mul
= n
| ((v
& CDCE706_PLL_HI_N_MASK
) <<
542 (8 - CDCE706_PLL_HI_N_SHIFT
));
543 cdce
->pll
[i
].mux
= mux
& CDCE706_PLL_MUX_MASK(i
);
544 dev_dbg(&cdce
->client
->dev
,
545 "%s: i: %u, div: %u, mul: %u, mux: %d\n", __func__
, i
,
546 cdce
->pll
[i
].div
, cdce
->pll
[i
].mul
, cdce
->pll
[i
].mux
);
549 ret
= cdce706_register_hw(cdce
, cdce
->pll
,
550 ARRAY_SIZE(cdce
->pll
),
551 cdce706_pll_name
, &init
);
555 static int cdce706_register_dividers(struct cdce706_dev_data
*cdce
)
557 struct clk_init_data init
= {
558 .ops
= &cdce706_divider_ops
,
559 .parent_names
= cdce706_divider_parent_name
,
560 .num_parents
= ARRAY_SIZE(cdce706_divider_parent_name
),
561 .flags
= CLK_SET_RATE_PARENT
,
566 for (i
= 0; i
< ARRAY_SIZE(cdce
->divider
); ++i
) {
569 ret
= cdce706_reg_read(cdce
, CDCE706_DIVIDER_PLL(i
), &val
);
572 cdce
->divider
[i
].parent
=
573 (val
& CDCE706_DIVIDER_PLL_MASK(i
)) >>
574 CDCE706_DIVIDER_PLL_SHIFT(i
);
576 ret
= cdce706_reg_read(cdce
, CDCE706_DIVIDER(i
), &val
);
579 cdce
->divider
[i
].div
= val
& CDCE706_DIVIDER_DIVIDER_MASK
;
580 dev_dbg(&cdce
->client
->dev
,
581 "%s: i: %u, parent: %u, div: %u\n", __func__
, i
,
582 cdce
->divider
[i
].parent
, cdce
->divider
[i
].div
);
585 ret
= cdce706_register_hw(cdce
, cdce
->divider
,
586 ARRAY_SIZE(cdce
->divider
),
587 cdce706_divider_name
, &init
);
591 static int cdce706_register_clkouts(struct cdce706_dev_data
*cdce
)
593 struct clk_init_data init
= {
594 .ops
= &cdce706_clkout_ops
,
595 .parent_names
= cdce706_divider_name
,
596 .num_parents
= ARRAY_SIZE(cdce706_divider_name
),
597 .flags
= CLK_SET_RATE_PARENT
,
602 for (i
= 0; i
< ARRAY_SIZE(cdce
->clkout
); ++i
) {
605 ret
= cdce706_reg_read(cdce
, CDCE706_CLKOUT(i
), &val
);
608 cdce
->clkout
[i
].parent
= val
& CDCE706_CLKOUT_DIVIDER_MASK
;
609 dev_dbg(&cdce
->client
->dev
,
610 "%s: i: %u, parent: %u\n", __func__
, i
,
611 cdce
->clkout
[i
].parent
);
614 return cdce706_register_hw(cdce
, cdce
->clkout
,
615 ARRAY_SIZE(cdce
->clkout
),
616 cdce706_clkout_name
, &init
);
619 static struct clk_hw
*
620 of_clk_cdce_get(struct of_phandle_args
*clkspec
, void *data
)
622 struct cdce706_dev_data
*cdce
= data
;
623 unsigned int idx
= clkspec
->args
[0];
625 if (idx
>= ARRAY_SIZE(cdce
->clkout
)) {
626 pr_err("%s: invalid index %u\n", __func__
, idx
);
627 return ERR_PTR(-EINVAL
);
630 return &cdce
->clkout
[idx
].hw
;
633 static int cdce706_probe(struct i2c_client
*client
,
634 const struct i2c_device_id
*id
)
636 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
637 struct cdce706_dev_data
*cdce
;
640 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
643 cdce
= devm_kzalloc(&client
->dev
, sizeof(*cdce
), GFP_KERNEL
);
647 cdce
->client
= client
;
648 cdce
->regmap
= devm_regmap_init_i2c(client
, &cdce706_regmap_config
);
649 if (IS_ERR(cdce
->regmap
)) {
650 dev_err(&client
->dev
, "Failed to initialize regmap\n");
654 i2c_set_clientdata(client
, cdce
);
656 ret
= cdce706_register_clkin(cdce
);
659 ret
= cdce706_register_plls(cdce
);
662 ret
= cdce706_register_dividers(cdce
);
665 ret
= cdce706_register_clkouts(cdce
);
668 return of_clk_add_hw_provider(client
->dev
.of_node
, of_clk_cdce_get
,
672 static int cdce706_remove(struct i2c_client
*client
)
674 of_clk_del_provider(client
->dev
.of_node
);
680 static const struct of_device_id cdce706_dt_match
[] = {
681 { .compatible
= "ti,cdce706" },
684 MODULE_DEVICE_TABLE(of
, cdce706_dt_match
);
687 static const struct i2c_device_id cdce706_id
[] = {
691 MODULE_DEVICE_TABLE(i2c
, cdce706_id
);
693 static struct i2c_driver cdce706_i2c_driver
= {
696 .of_match_table
= of_match_ptr(cdce706_dt_match
),
698 .probe
= cdce706_probe
,
699 .remove
= cdce706_remove
,
700 .id_table
= cdce706_id
,
702 module_i2c_driver(cdce706_i2c_driver
);
704 MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
705 MODULE_DESCRIPTION("TI CDCE 706 clock synthesizer driver");
706 MODULE_LICENSE("GPL");