1 // SPDX-License-Identifier: (GPL-2.0-only OR MIT)
3 * Amlogic S4 PLL Clock Controller Driver
5 * Copyright (c) 2022-2023 Amlogic, inc. All rights reserved
9 #include <linux/clk-provider.h>
10 #include <linux/of_device.h>
11 #include <linux/platform_device.h>
15 #include "clk-regmap.h"
17 #include "meson-clkc-utils.h"
18 #include <dt-bindings/clock/amlogic,s4-pll-clkc.h>
20 static DEFINE_SPINLOCK(meson_clk_lock);
23 * These clock are a fixed value (fixed_pll is 2GHz) that is initialized by ROMcode.
24 * The chip was changed fixed pll for security reasons. Fixed PLL registers are not writable
25 * in the kernel phase. Write of fixed PLL-related register will cause the system to crash.
26 * Meanwhile, these clock won't ever change at runtime.
27 * For the above reasons, we can only use ro_ops for fixed PLL related clocks.
29 static struct clk_regmap s4_fixed_pll_dco = {
30 .data = &(struct meson_clk_pll_data){
32 .reg_off = ANACTRL_FIXPLL_CTRL0,
37 .reg_off = ANACTRL_FIXPLL_CTRL0,
42 .reg_off = ANACTRL_FIXPLL_CTRL1,
47 .reg_off = ANACTRL_FIXPLL_CTRL0,
52 .reg_off = ANACTRL_FIXPLL_CTRL0,
57 .reg_off = ANACTRL_FIXPLL_CTRL0,
62 .hw.init = &(struct clk_init_data){
63 .name = "fixed_pll_dco",
64 .ops = &meson_clk_pll_ro_ops,
65 .parent_data = (const struct clk_parent_data []) {
66 { .fw_name = "xtal", }
72 static struct clk_regmap s4_fixed_pll = {
73 .data = &(struct clk_regmap_div_data){
74 .offset = ANACTRL_FIXPLL_CTRL0,
77 .flags = CLK_DIVIDER_POWER_OF_TWO,
79 .hw.init = &(struct clk_init_data){
81 .ops = &clk_regmap_divider_ro_ops,
82 .parent_hws = (const struct clk_hw *[]) {
87 * This clock won't ever change at runtime so
88 * CLK_SET_RATE_PARENT is not required
93 static struct clk_fixed_factor s4_fclk_div2_div = {
96 .hw.init = &(struct clk_init_data){
97 .name = "fclk_div2_div",
98 .ops = &clk_fixed_factor_ops,
99 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
104 static struct clk_regmap s4_fclk_div2 = {
105 .data = &(struct clk_regmap_gate_data){
106 .offset = ANACTRL_FIXPLL_CTRL1,
109 .hw.init = &(struct clk_init_data){
111 .ops = &clk_regmap_gate_ro_ops,
112 .parent_hws = (const struct clk_hw *[]) {
119 static struct clk_fixed_factor s4_fclk_div3_div = {
122 .hw.init = &(struct clk_init_data){
123 .name = "fclk_div3_div",
124 .ops = &clk_fixed_factor_ops,
125 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
130 static struct clk_regmap s4_fclk_div3 = {
131 .data = &(struct clk_regmap_gate_data){
132 .offset = ANACTRL_FIXPLL_CTRL1,
135 .hw.init = &(struct clk_init_data){
137 .ops = &clk_regmap_gate_ro_ops,
138 .parent_hws = (const struct clk_hw *[]) {
145 static struct clk_fixed_factor s4_fclk_div4_div = {
148 .hw.init = &(struct clk_init_data){
149 .name = "fclk_div4_div",
150 .ops = &clk_fixed_factor_ops,
151 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
156 static struct clk_regmap s4_fclk_div4 = {
157 .data = &(struct clk_regmap_gate_data){
158 .offset = ANACTRL_FIXPLL_CTRL1,
161 .hw.init = &(struct clk_init_data){
163 .ops = &clk_regmap_gate_ro_ops,
164 .parent_hws = (const struct clk_hw *[]) {
171 static struct clk_fixed_factor s4_fclk_div5_div = {
174 .hw.init = &(struct clk_init_data){
175 .name = "fclk_div5_div",
176 .ops = &clk_fixed_factor_ops,
177 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
182 static struct clk_regmap s4_fclk_div5 = {
183 .data = &(struct clk_regmap_gate_data){
184 .offset = ANACTRL_FIXPLL_CTRL1,
187 .hw.init = &(struct clk_init_data){
189 .ops = &clk_regmap_gate_ro_ops,
190 .parent_hws = (const struct clk_hw *[]) {
197 static struct clk_fixed_factor s4_fclk_div7_div = {
200 .hw.init = &(struct clk_init_data){
201 .name = "fclk_div7_div",
202 .ops = &clk_fixed_factor_ops,
203 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
208 static struct clk_regmap s4_fclk_div7 = {
209 .data = &(struct clk_regmap_gate_data){
210 .offset = ANACTRL_FIXPLL_CTRL1,
213 .hw.init = &(struct clk_init_data){
215 .ops = &clk_regmap_gate_ro_ops,
216 .parent_hws = (const struct clk_hw *[]) {
223 static struct clk_fixed_factor s4_fclk_div2p5_div = {
226 .hw.init = &(struct clk_init_data){
227 .name = "fclk_div2p5_div",
228 .ops = &clk_fixed_factor_ops,
229 .parent_hws = (const struct clk_hw *[]) {
236 static struct clk_regmap s4_fclk_div2p5 = {
237 .data = &(struct clk_regmap_gate_data){
238 .offset = ANACTRL_FIXPLL_CTRL1,
241 .hw.init = &(struct clk_init_data){
242 .name = "fclk_div2p5",
243 .ops = &clk_regmap_gate_ro_ops,
244 .parent_hws = (const struct clk_hw *[]) {
245 &s4_fclk_div2p5_div.hw
251 static const struct pll_mult_range s4_gp0_pll_mult_range = {
257 * Internal gp0 pll emulation configuration parameters
259 static const struct reg_sequence s4_gp0_init_regs[] = {
260 { .reg = ANACTRL_GP0PLL_CTRL1, .def = 0x00000000 },
261 { .reg = ANACTRL_GP0PLL_CTRL2, .def = 0x00000000 },
262 { .reg = ANACTRL_GP0PLL_CTRL3, .def = 0x48681c00 },
263 { .reg = ANACTRL_GP0PLL_CTRL4, .def = 0x88770290 },
264 { .reg = ANACTRL_GP0PLL_CTRL5, .def = 0x39272000 },
265 { .reg = ANACTRL_GP0PLL_CTRL6, .def = 0x56540000 }
268 static struct clk_regmap s4_gp0_pll_dco = {
269 .data = &(struct meson_clk_pll_data){
271 .reg_off = ANACTRL_GP0PLL_CTRL0,
276 .reg_off = ANACTRL_GP0PLL_CTRL0,
281 .reg_off = ANACTRL_GP0PLL_CTRL0,
286 .reg_off = ANACTRL_GP0PLL_CTRL0,
291 .reg_off = ANACTRL_GP0PLL_CTRL0,
295 .range = &s4_gp0_pll_mult_range,
296 .init_regs = s4_gp0_init_regs,
297 .init_count = ARRAY_SIZE(s4_gp0_init_regs),
299 .hw.init = &(struct clk_init_data){
300 .name = "gp0_pll_dco",
301 .ops = &meson_clk_pll_ops,
302 .parent_data = (const struct clk_parent_data []) {
303 { .fw_name = "xtal", }
309 static struct clk_regmap s4_gp0_pll = {
310 .data = &(struct clk_regmap_div_data){
311 .offset = ANACTRL_GP0PLL_CTRL0,
314 .flags = (CLK_DIVIDER_POWER_OF_TWO |
315 CLK_DIVIDER_ROUND_CLOSEST),
317 .hw.init = &(struct clk_init_data){
319 .ops = &clk_regmap_divider_ops,
320 .parent_hws = (const struct clk_hw *[]) {
324 .flags = CLK_SET_RATE_PARENT,
329 * Internal hifi pll emulation configuration parameters
331 static const struct reg_sequence s4_hifi_init_regs[] = {
332 { .reg = ANACTRL_HIFIPLL_CTRL1, .def = 0x00010e56 },
333 { .reg = ANACTRL_HIFIPLL_CTRL2, .def = 0x00000000 },
334 { .reg = ANACTRL_HIFIPLL_CTRL3, .def = 0x6a285c00 },
335 { .reg = ANACTRL_HIFIPLL_CTRL4, .def = 0x65771290 },
336 { .reg = ANACTRL_HIFIPLL_CTRL5, .def = 0x39272000 },
337 { .reg = ANACTRL_HIFIPLL_CTRL6, .def = 0x56540000 }
340 static struct clk_regmap s4_hifi_pll_dco = {
341 .data = &(struct meson_clk_pll_data){
343 .reg_off = ANACTRL_HIFIPLL_CTRL0,
348 .reg_off = ANACTRL_HIFIPLL_CTRL0,
353 .reg_off = ANACTRL_HIFIPLL_CTRL0,
358 .reg_off = ANACTRL_HIFIPLL_CTRL0,
363 .reg_off = ANACTRL_HIFIPLL_CTRL0,
367 .range = &s4_gp0_pll_mult_range,
368 .init_regs = s4_hifi_init_regs,
369 .init_count = ARRAY_SIZE(s4_hifi_init_regs),
370 .flags = CLK_MESON_PLL_ROUND_CLOSEST,
372 .hw.init = &(struct clk_init_data){
373 .name = "hifi_pll_dco",
374 .ops = &meson_clk_pll_ops,
375 .parent_data = (const struct clk_parent_data []) {
376 { .fw_name = "xtal", }
382 static struct clk_regmap s4_hifi_pll = {
383 .data = &(struct clk_regmap_div_data){
384 .offset = ANACTRL_HIFIPLL_CTRL0,
387 .flags = (CLK_DIVIDER_POWER_OF_TWO |
388 CLK_DIVIDER_ROUND_CLOSEST),
390 .hw.init = &(struct clk_init_data){
392 .ops = &clk_regmap_divider_ops,
393 .parent_hws = (const struct clk_hw *[]) {
397 .flags = CLK_SET_RATE_PARENT,
401 static struct clk_regmap s4_hdmi_pll_dco = {
402 .data = &(struct meson_clk_pll_data){
404 .reg_off = ANACTRL_HDMIPLL_CTRL0,
409 .reg_off = ANACTRL_HDMIPLL_CTRL0,
414 .reg_off = ANACTRL_HDMIPLL_CTRL0,
419 .reg_off = ANACTRL_HDMIPLL_CTRL0,
424 .reg_off = ANACTRL_HDMIPLL_CTRL0,
428 .range = &s4_gp0_pll_mult_range,
430 .hw.init = &(struct clk_init_data){
431 .name = "hdmi_pll_dco",
432 .ops = &meson_clk_pll_ops,
433 .parent_data = (const struct clk_parent_data []) {
434 { .fw_name = "xtal", }
440 static struct clk_regmap s4_hdmi_pll_od = {
441 .data = &(struct clk_regmap_div_data){
442 .offset = ANACTRL_HDMIPLL_CTRL0,
445 .flags = CLK_DIVIDER_POWER_OF_TWO,
447 .hw.init = &(struct clk_init_data){
448 .name = "hdmi_pll_od",
449 .ops = &clk_regmap_divider_ops,
450 .parent_hws = (const struct clk_hw *[]) {
454 .flags = CLK_SET_RATE_PARENT,
458 static struct clk_regmap s4_hdmi_pll = {
459 .data = &(struct clk_regmap_div_data){
460 .offset = ANACTRL_HDMIPLL_CTRL0,
463 .flags = CLK_DIVIDER_POWER_OF_TWO,
465 .hw.init = &(struct clk_init_data){
467 .ops = &clk_regmap_divider_ops,
468 .parent_hws = (const struct clk_hw *[]) {
472 .flags = CLK_SET_RATE_PARENT,
476 static struct clk_fixed_factor s4_mpll_50m_div = {
479 .hw.init = &(struct clk_init_data){
480 .name = "mpll_50m_div",
481 .ops = &clk_fixed_factor_ops,
482 .parent_hws = (const struct clk_hw *[]) {
489 static struct clk_regmap s4_mpll_50m = {
490 .data = &(struct clk_regmap_mux_data){
491 .offset = ANACTRL_FIXPLL_CTRL3,
495 .hw.init = &(struct clk_init_data){
497 .ops = &clk_regmap_mux_ro_ops,
498 .parent_data = (const struct clk_parent_data []) {
499 { .fw_name = "xtal", },
500 { .hw = &s4_mpll_50m_div.hw },
506 static struct clk_fixed_factor s4_mpll_prediv = {
509 .hw.init = &(struct clk_init_data){
510 .name = "mpll_prediv",
511 .ops = &clk_fixed_factor_ops,
512 .parent_hws = (const struct clk_hw *[]) {
519 static const struct reg_sequence s4_mpll0_init_regs[] = {
520 { .reg = ANACTRL_MPLL_CTRL2, .def = 0x40000033 }
523 static struct clk_regmap s4_mpll0_div = {
524 .data = &(struct meson_clk_mpll_data){
526 .reg_off = ANACTRL_MPLL_CTRL1,
531 .reg_off = ANACTRL_MPLL_CTRL1,
536 .reg_off = ANACTRL_MPLL_CTRL1,
541 .reg_off = ANACTRL_MPLL_CTRL1,
545 .lock = &meson_clk_lock,
546 .init_regs = s4_mpll0_init_regs,
547 .init_count = ARRAY_SIZE(s4_mpll0_init_regs),
549 .hw.init = &(struct clk_init_data){
551 .ops = &meson_clk_mpll_ops,
552 .parent_hws = (const struct clk_hw *[]) {
559 static struct clk_regmap s4_mpll0 = {
560 .data = &(struct clk_regmap_gate_data){
561 .offset = ANACTRL_MPLL_CTRL1,
564 .hw.init = &(struct clk_init_data){
566 .ops = &clk_regmap_gate_ops,
567 .parent_hws = (const struct clk_hw *[]) { &s4_mpll0_div.hw },
569 .flags = CLK_SET_RATE_PARENT,
573 static const struct reg_sequence s4_mpll1_init_regs[] = {
574 { .reg = ANACTRL_MPLL_CTRL4, .def = 0x40000033 }
577 static struct clk_regmap s4_mpll1_div = {
578 .data = &(struct meson_clk_mpll_data){
580 .reg_off = ANACTRL_MPLL_CTRL3,
585 .reg_off = ANACTRL_MPLL_CTRL3,
590 .reg_off = ANACTRL_MPLL_CTRL3,
595 .reg_off = ANACTRL_MPLL_CTRL3,
599 .lock = &meson_clk_lock,
600 .init_regs = s4_mpll1_init_regs,
601 .init_count = ARRAY_SIZE(s4_mpll1_init_regs),
603 .hw.init = &(struct clk_init_data){
605 .ops = &meson_clk_mpll_ops,
606 .parent_hws = (const struct clk_hw *[]) {
613 static struct clk_regmap s4_mpll1 = {
614 .data = &(struct clk_regmap_gate_data){
615 .offset = ANACTRL_MPLL_CTRL3,
618 .hw.init = &(struct clk_init_data){
620 .ops = &clk_regmap_gate_ops,
621 .parent_hws = (const struct clk_hw *[]) { &s4_mpll1_div.hw },
623 .flags = CLK_SET_RATE_PARENT,
627 static const struct reg_sequence s4_mpll2_init_regs[] = {
628 { .reg = ANACTRL_MPLL_CTRL6, .def = 0x40000033 }
631 static struct clk_regmap s4_mpll2_div = {
632 .data = &(struct meson_clk_mpll_data){
634 .reg_off = ANACTRL_MPLL_CTRL5,
639 .reg_off = ANACTRL_MPLL_CTRL5,
644 .reg_off = ANACTRL_MPLL_CTRL5,
649 .reg_off = ANACTRL_MPLL_CTRL5,
653 .lock = &meson_clk_lock,
654 .init_regs = s4_mpll2_init_regs,
655 .init_count = ARRAY_SIZE(s4_mpll2_init_regs),
657 .hw.init = &(struct clk_init_data){
659 .ops = &meson_clk_mpll_ops,
660 .parent_hws = (const struct clk_hw *[]) {
667 static struct clk_regmap s4_mpll2 = {
668 .data = &(struct clk_regmap_gate_data){
669 .offset = ANACTRL_MPLL_CTRL5,
672 .hw.init = &(struct clk_init_data){
674 .ops = &clk_regmap_gate_ops,
675 .parent_hws = (const struct clk_hw *[]) { &s4_mpll2_div.hw },
677 .flags = CLK_SET_RATE_PARENT,
681 static const struct reg_sequence s4_mpll3_init_regs[] = {
682 { .reg = ANACTRL_MPLL_CTRL8, .def = 0x40000033 }
685 static struct clk_regmap s4_mpll3_div = {
686 .data = &(struct meson_clk_mpll_data){
688 .reg_off = ANACTRL_MPLL_CTRL7,
693 .reg_off = ANACTRL_MPLL_CTRL7,
698 .reg_off = ANACTRL_MPLL_CTRL7,
703 .reg_off = ANACTRL_MPLL_CTRL7,
707 .lock = &meson_clk_lock,
708 .init_regs = s4_mpll3_init_regs,
709 .init_count = ARRAY_SIZE(s4_mpll3_init_regs),
711 .hw.init = &(struct clk_init_data){
713 .ops = &meson_clk_mpll_ops,
714 .parent_hws = (const struct clk_hw *[]) {
721 static struct clk_regmap s4_mpll3 = {
722 .data = &(struct clk_regmap_gate_data){
723 .offset = ANACTRL_MPLL_CTRL7,
726 .hw.init = &(struct clk_init_data){
728 .ops = &clk_regmap_gate_ops,
729 .parent_hws = (const struct clk_hw *[]) { &s4_mpll3_div.hw },
731 .flags = CLK_SET_RATE_PARENT,
735 /* Array of all clocks provided by this provider */
736 static struct clk_hw *s4_pll_hw_clks[] = {
737 [CLKID_FIXED_PLL_DCO] = &s4_fixed_pll_dco.hw,
738 [CLKID_FIXED_PLL] = &s4_fixed_pll.hw,
739 [CLKID_FCLK_DIV2_DIV] = &s4_fclk_div2_div.hw,
740 [CLKID_FCLK_DIV2] = &s4_fclk_div2.hw,
741 [CLKID_FCLK_DIV3_DIV] = &s4_fclk_div3_div.hw,
742 [CLKID_FCLK_DIV3] = &s4_fclk_div3.hw,
743 [CLKID_FCLK_DIV4_DIV] = &s4_fclk_div4_div.hw,
744 [CLKID_FCLK_DIV4] = &s4_fclk_div4.hw,
745 [CLKID_FCLK_DIV5_DIV] = &s4_fclk_div5_div.hw,
746 [CLKID_FCLK_DIV5] = &s4_fclk_div5.hw,
747 [CLKID_FCLK_DIV7_DIV] = &s4_fclk_div7_div.hw,
748 [CLKID_FCLK_DIV7] = &s4_fclk_div7.hw,
749 [CLKID_FCLK_DIV2P5_DIV] = &s4_fclk_div2p5_div.hw,
750 [CLKID_FCLK_DIV2P5] = &s4_fclk_div2p5.hw,
751 [CLKID_GP0_PLL_DCO] = &s4_gp0_pll_dco.hw,
752 [CLKID_GP0_PLL] = &s4_gp0_pll.hw,
753 [CLKID_HIFI_PLL_DCO] = &s4_hifi_pll_dco.hw,
754 [CLKID_HIFI_PLL] = &s4_hifi_pll.hw,
755 [CLKID_HDMI_PLL_DCO] = &s4_hdmi_pll_dco.hw,
756 [CLKID_HDMI_PLL_OD] = &s4_hdmi_pll_od.hw,
757 [CLKID_HDMI_PLL] = &s4_hdmi_pll.hw,
758 [CLKID_MPLL_50M_DIV] = &s4_mpll_50m_div.hw,
759 [CLKID_MPLL_50M] = &s4_mpll_50m.hw,
760 [CLKID_MPLL_PREDIV] = &s4_mpll_prediv.hw,
761 [CLKID_MPLL0_DIV] = &s4_mpll0_div.hw,
762 [CLKID_MPLL0] = &s4_mpll0.hw,
763 [CLKID_MPLL1_DIV] = &s4_mpll1_div.hw,
764 [CLKID_MPLL1] = &s4_mpll1.hw,
765 [CLKID_MPLL2_DIV] = &s4_mpll2_div.hw,
766 [CLKID_MPLL2] = &s4_mpll2.hw,
767 [CLKID_MPLL3_DIV] = &s4_mpll3_div.hw,
768 [CLKID_MPLL3] = &s4_mpll3.hw,
771 static struct clk_regmap *const s4_pll_clk_regmaps[] = {
798 static const struct reg_sequence s4_init_regs[] = {
799 { .reg = ANACTRL_MPLL_CTRL0, .def = 0x00000543 },
802 static struct regmap_config clkc_regmap_config = {
806 .max_register = ANACTRL_HDMIPLL_CTRL0,
809 static struct meson_clk_hw_data s4_pll_clks = {
810 .hws = s4_pll_hw_clks,
811 .num = ARRAY_SIZE(s4_pll_hw_clks),
814 static int meson_s4_pll_probe(struct platform_device *pdev)
816 struct device *dev = &pdev->dev;
817 struct regmap *regmap;
821 base = devm_platform_ioremap_resource(pdev, 0);
823 return dev_err_probe(dev, PTR_ERR(base),
824 "can't ioremap resource\n");
826 regmap = devm_regmap_init_mmio(dev, base, &clkc_regmap_config);
828 return dev_err_probe(dev, PTR_ERR(regmap),
829 "can't init regmap mmio region\n");
831 ret = regmap_multi_reg_write(regmap, s4_init_regs, ARRAY_SIZE(s4_init_regs));
833 return dev_err_probe(dev, ret,
834 "Failed to init registers\n");
836 /* Populate regmap for the regmap backed clocks */
837 for (i = 0; i < ARRAY_SIZE(s4_pll_clk_regmaps); i++)
838 s4_pll_clk_regmaps[i]->map = regmap;
840 /* Register clocks */
841 for (i = 0; i < s4_pll_clks.num; i++) {
842 /* array might be sparse */
843 if (!s4_pll_clks.hws[i])
846 ret = devm_clk_hw_register(dev, s4_pll_clks.hws[i]);
848 return dev_err_probe(dev, ret,
849 "clock[%d] registration failed\n", i);
852 return devm_of_clk_add_hw_provider(dev, meson_clk_hw_get,
856 static const struct of_device_id clkc_match_table[] = {
858 .compatible = "amlogic,s4-pll-clkc",
862 MODULE_DEVICE_TABLE(of, clkc_match_table);
864 static struct platform_driver s4_driver = {
865 .probe = meson_s4_pll_probe,
867 .name = "s4-pll-clkc",
868 .of_match_table = clkc_match_table,
871 module_platform_driver(s4_driver);
873 MODULE_DESCRIPTION("Amlogic S4 PLL Clock Controller driver");
875 MODULE_LICENSE("GPL");