1 // SPDX-License-Identifier: GPL-2.0-only
3 * Amlogic C3 PLL Controller Driver
5 * Copyright (c) 2023 Amlogic, inc.
9 #include <linux/clk-provider.h>
10 #include <linux/platform_device.h>
11 #include "clk-regmap.h"
13 #include "meson-clkc-utils.h"
14 #include <dt-bindings/clock/amlogic,c3-pll-clkc.h>
16 #define ANACTRL_FIXPLL_CTRL4 0x50
17 #define ANACTRL_GP0PLL_CTRL0 0x80
18 #define ANACTRL_GP0PLL_CTRL1 0x84
19 #define ANACTRL_GP0PLL_CTRL2 0x88
20 #define ANACTRL_GP0PLL_CTRL3 0x8c
21 #define ANACTRL_GP0PLL_CTRL4 0x90
22 #define ANACTRL_GP0PLL_CTRL5 0x94
23 #define ANACTRL_GP0PLL_CTRL6 0x98
24 #define ANACTRL_HIFIPLL_CTRL0 0x100
25 #define ANACTRL_HIFIPLL_CTRL1 0x104
26 #define ANACTRL_HIFIPLL_CTRL2 0x108
27 #define ANACTRL_HIFIPLL_CTRL3 0x10c
28 #define ANACTRL_HIFIPLL_CTRL4 0x110
29 #define ANACTRL_HIFIPLL_CTRL5 0x114
30 #define ANACTRL_HIFIPLL_CTRL6 0x118
31 #define ANACTRL_MPLL_CTRL0 0x180
32 #define ANACTRL_MPLL_CTRL1 0x184
33 #define ANACTRL_MPLL_CTRL2 0x188
34 #define ANACTRL_MPLL_CTRL3 0x18c
35 #define ANACTRL_MPLL_CTRL4 0x190
37 static struct clk_regmap fclk_50m_en = {
38 .data = &(struct clk_regmap_gate_data) {
39 .offset = ANACTRL_FIXPLL_CTRL4,
42 .hw.init = &(struct clk_init_data) {
43 .name = "fclk_50m_en",
44 .ops = &clk_regmap_gate_ro_ops,
45 .parent_data = &(const struct clk_parent_data) {
52 static struct clk_fixed_factor fclk_50m = {
55 .hw.init = &(struct clk_init_data) {
57 .ops = &clk_fixed_factor_ops,
58 .parent_hws = (const struct clk_hw *[]) {
65 static struct clk_fixed_factor fclk_div2_div = {
68 .hw.init = &(struct clk_init_data) {
69 .name = "fclk_div2_div",
70 .ops = &clk_fixed_factor_ops,
71 .parent_data = &(const struct clk_parent_data) {
78 static struct clk_regmap fclk_div2 = {
79 .data = &(struct clk_regmap_gate_data) {
80 .offset = ANACTRL_FIXPLL_CTRL4,
83 .hw.init = &(struct clk_init_data) {
85 .ops = &clk_regmap_gate_ro_ops,
86 .parent_hws = (const struct clk_hw *[]) {
93 static struct clk_fixed_factor fclk_div2p5_div = {
96 .hw.init = &(struct clk_init_data) {
97 .name = "fclk_div2p5_div",
98 .ops = &clk_fixed_factor_ops,
99 .parent_data = &(const struct clk_parent_data) {
106 static struct clk_regmap fclk_div2p5 = {
107 .data = &(struct clk_regmap_gate_data) {
108 .offset = ANACTRL_FIXPLL_CTRL4,
111 .hw.init = &(struct clk_init_data) {
112 .name = "fclk_div2p5",
113 .ops = &clk_regmap_gate_ro_ops,
114 .parent_hws = (const struct clk_hw *[]) {
121 static struct clk_fixed_factor fclk_div3_div = {
124 .hw.init = &(struct clk_init_data) {
125 .name = "fclk_div3_div",
126 .ops = &clk_fixed_factor_ops,
127 .parent_data = &(const struct clk_parent_data) {
134 static struct clk_regmap fclk_div3 = {
135 .data = &(struct clk_regmap_gate_data) {
136 .offset = ANACTRL_FIXPLL_CTRL4,
139 .hw.init = &(struct clk_init_data) {
141 .ops = &clk_regmap_gate_ro_ops,
142 .parent_hws = (const struct clk_hw *[]) {
149 static struct clk_fixed_factor fclk_div4_div = {
152 .hw.init = &(struct clk_init_data) {
153 .name = "fclk_div4_div",
154 .ops = &clk_fixed_factor_ops,
155 .parent_data = &(const struct clk_parent_data) {
162 static struct clk_regmap fclk_div4 = {
163 .data = &(struct clk_regmap_gate_data) {
164 .offset = ANACTRL_FIXPLL_CTRL4,
167 .hw.init = &(struct clk_init_data) {
169 .ops = &clk_regmap_gate_ro_ops,
170 .parent_hws = (const struct clk_hw *[]) {
177 static struct clk_fixed_factor fclk_div5_div = {
180 .hw.init = &(struct clk_init_data) {
181 .name = "fclk_div5_div",
182 .ops = &clk_fixed_factor_ops,
183 .parent_data = &(const struct clk_parent_data) {
190 static struct clk_regmap fclk_div5 = {
191 .data = &(struct clk_regmap_gate_data) {
192 .offset = ANACTRL_FIXPLL_CTRL4,
195 .hw.init = &(struct clk_init_data) {
197 .ops = &clk_regmap_gate_ro_ops,
198 .parent_hws = (const struct clk_hw *[]) {
205 static struct clk_fixed_factor fclk_div7_div = {
208 .hw.init = &(struct clk_init_data) {
209 .name = "fclk_div7_div",
210 .ops = &clk_fixed_factor_ops,
211 .parent_data = &(const struct clk_parent_data) {
218 static struct clk_regmap fclk_div7 = {
219 .data = &(struct clk_regmap_gate_data) {
220 .offset = ANACTRL_FIXPLL_CTRL4,
223 .hw.init = &(struct clk_init_data) {
225 .ops = &clk_regmap_gate_ro_ops,
226 .parent_hws = (const struct clk_hw *[]) {
233 static const struct reg_sequence c3_gp0_init_regs[] = {
234 { .reg = ANACTRL_GP0PLL_CTRL2, .def = 0x0 },
235 { .reg = ANACTRL_GP0PLL_CTRL3, .def = 0x48681c00 },
236 { .reg = ANACTRL_GP0PLL_CTRL4, .def = 0x88770290 },
237 { .reg = ANACTRL_GP0PLL_CTRL5, .def = 0x3927200a },
238 { .reg = ANACTRL_GP0PLL_CTRL6, .def = 0x56540000 },
241 static const struct pll_mult_range c3_gp0_pll_mult_range = {
246 static struct clk_regmap gp0_pll_dco = {
247 .data = &(struct meson_clk_pll_data) {
249 .reg_off = ANACTRL_GP0PLL_CTRL0,
254 .reg_off = ANACTRL_GP0PLL_CTRL0,
259 .reg_off = ANACTRL_GP0PLL_CTRL1,
264 .reg_off = ANACTRL_GP0PLL_CTRL0,
269 .reg_off = ANACTRL_GP0PLL_CTRL0,
274 .reg_off = ANACTRL_GP0PLL_CTRL0,
278 .range = &c3_gp0_pll_mult_range,
279 .init_regs = c3_gp0_init_regs,
280 .init_count = ARRAY_SIZE(c3_gp0_init_regs),
282 .hw.init = &(struct clk_init_data) {
283 .name = "gp0_pll_dco",
284 .ops = &meson_clk_pll_ops,
285 .parent_data = &(const struct clk_parent_data) {
292 /* The maximum frequency divider supports is 32, not 128(2^7) */
293 static const struct clk_div_table c3_gp0_pll_od_table[] = {
303 static struct clk_regmap gp0_pll = {
304 .data = &(struct clk_regmap_div_data) {
305 .offset = ANACTRL_GP0PLL_CTRL0,
308 .table = c3_gp0_pll_od_table,
310 .hw.init = &(struct clk_init_data) {
312 .ops = &clk_regmap_divider_ops,
313 .parent_hws = (const struct clk_hw *[]) {
317 .flags = CLK_SET_RATE_PARENT,
321 static const struct reg_sequence c3_hifi_init_regs[] = {
322 { .reg = ANACTRL_HIFIPLL_CTRL2, .def = 0x0 },
323 { .reg = ANACTRL_HIFIPLL_CTRL3, .def = 0x6a285c00 },
324 { .reg = ANACTRL_HIFIPLL_CTRL4, .def = 0x65771290 },
325 { .reg = ANACTRL_HIFIPLL_CTRL5, .def = 0x3927200a },
326 { .reg = ANACTRL_HIFIPLL_CTRL6, .def = 0x56540000 },
329 static struct clk_regmap hifi_pll_dco = {
330 .data = &(struct meson_clk_pll_data) {
332 .reg_off = ANACTRL_HIFIPLL_CTRL0,
337 .reg_off = ANACTRL_HIFIPLL_CTRL0,
342 .reg_off = ANACTRL_HIFIPLL_CTRL1,
347 .reg_off = ANACTRL_HIFIPLL_CTRL0,
352 .reg_off = ANACTRL_HIFIPLL_CTRL0,
357 .reg_off = ANACTRL_HIFIPLL_CTRL0,
361 .range = &c3_gp0_pll_mult_range,
362 .init_regs = c3_hifi_init_regs,
363 .init_count = ARRAY_SIZE(c3_hifi_init_regs),
366 .hw.init = &(struct clk_init_data) {
367 .name = "hifi_pll_dco",
368 .ops = &meson_clk_pll_ops,
369 .parent_data = &(const struct clk_parent_data) {
376 static struct clk_regmap hifi_pll = {
377 .data = &(struct clk_regmap_div_data) {
378 .offset = ANACTRL_HIFIPLL_CTRL0,
381 .flags = CLK_DIVIDER_POWER_OF_TWO,
383 .hw.init = &(struct clk_init_data) {
385 .ops = &clk_regmap_divider_ops,
386 .parent_hws = (const struct clk_hw *[]) {
390 .flags = CLK_SET_RATE_PARENT,
394 static const struct reg_sequence c3_mclk_init_regs[] = {
395 { .reg = ANACTRL_MPLL_CTRL1, .def = 0x1420500f },
396 { .reg = ANACTRL_MPLL_CTRL2, .def = 0x00023041 },
397 { .reg = ANACTRL_MPLL_CTRL3, .def = 0x18180000 },
398 { .reg = ANACTRL_MPLL_CTRL2, .def = 0x00023001 }
401 static const struct pll_mult_range c3_mclk_pll_mult_range = {
406 static struct clk_regmap mclk_pll_dco = {
407 .data = &(struct meson_clk_pll_data) {
409 .reg_off = ANACTRL_MPLL_CTRL0,
414 .reg_off = ANACTRL_MPLL_CTRL0,
419 .reg_off = ANACTRL_MPLL_CTRL0,
424 .reg_off = ANACTRL_MPLL_CTRL0,
429 .reg_off = ANACTRL_MPLL_CTRL0,
433 .range = &c3_mclk_pll_mult_range,
434 .init_regs = c3_mclk_init_regs,
435 .init_count = ARRAY_SIZE(c3_mclk_init_regs),
437 .hw.init = &(struct clk_init_data) {
438 .name = "mclk_pll_dco",
439 .ops = &meson_clk_pll_ops,
440 .parent_data = &(const struct clk_parent_data) {
447 static const struct clk_div_table c3_mpll_od_table[] = {
456 static struct clk_regmap mclk_pll_od = {
457 .data = &(struct clk_regmap_div_data) {
458 .offset = ANACTRL_MPLL_CTRL0,
461 .table = c3_mpll_od_table,
463 .hw.init = &(struct clk_init_data) {
464 .name = "mclk_pll_od",
465 .ops = &clk_regmap_divider_ops,
466 .parent_hws = (const struct clk_hw *[]) {
469 .flags = CLK_SET_RATE_PARENT,
473 /* both value 0 and 1 gives divide the input rate by one */
474 static struct clk_regmap mclk_pll = {
475 .data = &(struct clk_regmap_div_data) {
476 .offset = ANACTRL_MPLL_CTRL4,
479 .flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
481 .hw.init = &(struct clk_init_data) {
483 .ops = &clk_regmap_divider_ops,
484 .parent_hws = (const struct clk_hw *[]) {
488 .flags = CLK_SET_RATE_PARENT,
492 static const struct clk_parent_data mclk_parent[] = {
493 { .hw = &mclk_pll.hw },
494 { .fw_name = "mclk" },
495 { .hw = &fclk_50m.hw }
498 static struct clk_regmap mclk0_sel = {
499 .data = &(struct clk_regmap_mux_data) {
500 .offset = ANACTRL_MPLL_CTRL4,
504 .hw.init = &(struct clk_init_data) {
506 .ops = &clk_regmap_mux_ops,
507 .parent_data = mclk_parent,
508 .num_parents = ARRAY_SIZE(mclk_parent),
512 static struct clk_regmap mclk0_div_en = {
513 .data = &(struct clk_regmap_gate_data) {
514 .offset = ANACTRL_MPLL_CTRL4,
517 .hw.init = &(struct clk_init_data) {
518 .name = "mclk0_div_en",
519 .ops = &clk_regmap_gate_ops,
520 .parent_hws = (const struct clk_hw *[]) {
524 .flags = CLK_SET_RATE_PARENT,
528 static struct clk_regmap mclk0_div = {
529 .data = &(struct clk_regmap_div_data) {
530 .offset = ANACTRL_MPLL_CTRL4,
534 .hw.init = &(struct clk_init_data) {
536 .ops = &clk_regmap_divider_ops,
537 .parent_hws = (const struct clk_hw *[]) {
541 .flags = CLK_SET_RATE_PARENT,
545 static struct clk_regmap mclk0 = {
546 .data = &(struct clk_regmap_gate_data) {
547 .offset = ANACTRL_MPLL_CTRL4,
550 .hw.init = &(struct clk_init_data) {
552 .ops = &clk_regmap_gate_ops,
553 .parent_hws = (const struct clk_hw *[]) {
557 .flags = CLK_SET_RATE_PARENT,
561 static struct clk_regmap mclk1_sel = {
562 .data = &(struct clk_regmap_mux_data) {
563 .offset = ANACTRL_MPLL_CTRL4,
567 .hw.init = &(struct clk_init_data) {
569 .ops = &clk_regmap_mux_ops,
570 .parent_data = mclk_parent,
571 .num_parents = ARRAY_SIZE(mclk_parent),
575 static struct clk_regmap mclk1_div_en = {
576 .data = &(struct clk_regmap_gate_data) {
577 .offset = ANACTRL_MPLL_CTRL4,
580 .hw.init = &(struct clk_init_data) {
581 .name = "mclk1_div_en",
582 .ops = &clk_regmap_gate_ops,
583 .parent_hws = (const struct clk_hw *[]) {
587 .flags = CLK_SET_RATE_PARENT,
591 static struct clk_regmap mclk1_div = {
592 .data = &(struct clk_regmap_div_data) {
593 .offset = ANACTRL_MPLL_CTRL4,
597 .hw.init = &(struct clk_init_data) {
599 .ops = &clk_regmap_divider_ops,
600 .parent_hws = (const struct clk_hw *[]) {
604 .flags = CLK_SET_RATE_PARENT,
608 static struct clk_regmap mclk1 = {
609 .data = &(struct clk_regmap_gate_data) {
610 .offset = ANACTRL_MPLL_CTRL4,
613 .hw.init = &(struct clk_init_data) {
615 .ops = &clk_regmap_gate_ops,
616 .parent_hws = (const struct clk_hw *[]) {
620 .flags = CLK_SET_RATE_PARENT,
624 static struct clk_hw *c3_pll_hw_clks[] = {
625 [CLKID_FCLK_50M_EN] = &fclk_50m_en.hw,
626 [CLKID_FCLK_50M] = &fclk_50m.hw,
627 [CLKID_FCLK_DIV2_DIV] = &fclk_div2_div.hw,
628 [CLKID_FCLK_DIV2] = &fclk_div2.hw,
629 [CLKID_FCLK_DIV2P5_DIV] = &fclk_div2p5_div.hw,
630 [CLKID_FCLK_DIV2P5] = &fclk_div2p5.hw,
631 [CLKID_FCLK_DIV3_DIV] = &fclk_div3_div.hw,
632 [CLKID_FCLK_DIV3] = &fclk_div3.hw,
633 [CLKID_FCLK_DIV4_DIV] = &fclk_div4_div.hw,
634 [CLKID_FCLK_DIV4] = &fclk_div4.hw,
635 [CLKID_FCLK_DIV5_DIV] = &fclk_div5_div.hw,
636 [CLKID_FCLK_DIV5] = &fclk_div5.hw,
637 [CLKID_FCLK_DIV7_DIV] = &fclk_div7_div.hw,
638 [CLKID_FCLK_DIV7] = &fclk_div7.hw,
639 [CLKID_GP0_PLL_DCO] = &gp0_pll_dco.hw,
640 [CLKID_GP0_PLL] = &gp0_pll.hw,
641 [CLKID_HIFI_PLL_DCO] = &hifi_pll_dco.hw,
642 [CLKID_HIFI_PLL] = &hifi_pll.hw,
643 [CLKID_MCLK_PLL_DCO] = &mclk_pll_dco.hw,
644 [CLKID_MCLK_PLL_OD] = &mclk_pll_od.hw,
645 [CLKID_MCLK_PLL] = &mclk_pll.hw,
646 [CLKID_MCLK0_SEL] = &mclk0_sel.hw,
647 [CLKID_MCLK0_SEL_EN] = &mclk0_div_en.hw,
648 [CLKID_MCLK0_DIV] = &mclk0_div.hw,
649 [CLKID_MCLK0] = &mclk0.hw,
650 [CLKID_MCLK1_SEL] = &mclk1_sel.hw,
651 [CLKID_MCLK1_SEL_EN] = &mclk1_div_en.hw,
652 [CLKID_MCLK1_DIV] = &mclk1_div.hw,
653 [CLKID_MCLK1] = &mclk1.hw
656 /* Convenience table to populate regmap in .probe */
657 static struct clk_regmap *const c3_pll_clk_regmaps[] = {
682 static const struct regmap_config clkc_regmap_config = {
686 .max_register = ANACTRL_MPLL_CTRL4,
689 static struct meson_clk_hw_data c3_pll_clks = {
690 .hws = c3_pll_hw_clks,
691 .num = ARRAY_SIZE(c3_pll_hw_clks),
694 static int c3_pll_probe(struct platform_device *pdev)
696 struct device *dev = &pdev->dev;
697 struct regmap *regmap;
701 base = devm_platform_ioremap_resource(pdev, 0);
703 return PTR_ERR(base);
705 regmap = devm_regmap_init_mmio(dev, base, &clkc_regmap_config);
707 return PTR_ERR(regmap);
709 /* Populate regmap for the regmap backed clocks */
710 for (i = 0; i < ARRAY_SIZE(c3_pll_clk_regmaps); i++)
711 c3_pll_clk_regmaps[i]->map = regmap;
713 for (clkid = 0; clkid < c3_pll_clks.num; clkid++) {
714 /* array might be sparse */
715 if (!c3_pll_clks.hws[clkid])
718 ret = devm_clk_hw_register(dev, c3_pll_clks.hws[clkid]);
720 dev_err(dev, "Clock registration failed\n");
725 return devm_of_clk_add_hw_provider(dev, meson_clk_hw_get,
729 static const struct of_device_id c3_pll_clkc_match_table[] = {
731 .compatible = "amlogic,c3-pll-clkc",
735 MODULE_DEVICE_TABLE(of, c3_pll_clkc_match_table);
737 static struct platform_driver c3_pll_driver = {
738 .probe = c3_pll_probe,
740 .name = "c3-pll-clkc",
741 .of_match_table = c3_pll_clkc_match_table,
744 module_platform_driver(c3_pll_driver);
746 MODULE_DESCRIPTION("Amlogic C3 PLL Clock Controller driver");
748 MODULE_LICENSE("GPL");
749 MODULE_IMPORT_NS("CLK_MESON");