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
6 * Author: Yu Tu <yu.tu@amlogic.com>
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_CTRL0,
47 .reg_off = ANACTRL_FIXPLL_CTRL0,
52 .reg_off = ANACTRL_FIXPLL_CTRL0,
57 .hw.init = &(struct clk_init_data){
58 .name = "fixed_pll_dco",
59 .ops = &meson_clk_pll_ro_ops,
60 .parent_data = (const struct clk_parent_data []) {
61 { .fw_name = "xtal", }
67 static struct clk_regmap s4_fixed_pll = {
68 .data = &(struct clk_regmap_div_data){
69 .offset = ANACTRL_FIXPLL_CTRL0,
72 .flags = CLK_DIVIDER_POWER_OF_TWO,
74 .hw.init = &(struct clk_init_data){
76 .ops = &clk_regmap_divider_ro_ops,
77 .parent_hws = (const struct clk_hw *[]) {
82 * This clock won't ever change at runtime so
83 * CLK_SET_RATE_PARENT is not required
88 static struct clk_fixed_factor s4_fclk_div2_div = {
91 .hw.init = &(struct clk_init_data){
92 .name = "fclk_div2_div",
93 .ops = &clk_fixed_factor_ops,
94 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
99 static struct clk_regmap s4_fclk_div2 = {
100 .data = &(struct clk_regmap_gate_data){
101 .offset = ANACTRL_FIXPLL_CTRL1,
104 .hw.init = &(struct clk_init_data){
106 .ops = &clk_regmap_gate_ro_ops,
107 .parent_hws = (const struct clk_hw *[]) {
114 static struct clk_fixed_factor s4_fclk_div3_div = {
117 .hw.init = &(struct clk_init_data){
118 .name = "fclk_div3_div",
119 .ops = &clk_fixed_factor_ops,
120 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
125 static struct clk_regmap s4_fclk_div3 = {
126 .data = &(struct clk_regmap_gate_data){
127 .offset = ANACTRL_FIXPLL_CTRL1,
130 .hw.init = &(struct clk_init_data){
132 .ops = &clk_regmap_gate_ro_ops,
133 .parent_hws = (const struct clk_hw *[]) {
140 static struct clk_fixed_factor s4_fclk_div4_div = {
143 .hw.init = &(struct clk_init_data){
144 .name = "fclk_div4_div",
145 .ops = &clk_fixed_factor_ops,
146 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
151 static struct clk_regmap s4_fclk_div4 = {
152 .data = &(struct clk_regmap_gate_data){
153 .offset = ANACTRL_FIXPLL_CTRL1,
156 .hw.init = &(struct clk_init_data){
158 .ops = &clk_regmap_gate_ro_ops,
159 .parent_hws = (const struct clk_hw *[]) {
166 static struct clk_fixed_factor s4_fclk_div5_div = {
169 .hw.init = &(struct clk_init_data){
170 .name = "fclk_div5_div",
171 .ops = &clk_fixed_factor_ops,
172 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
177 static struct clk_regmap s4_fclk_div5 = {
178 .data = &(struct clk_regmap_gate_data){
179 .offset = ANACTRL_FIXPLL_CTRL1,
182 .hw.init = &(struct clk_init_data){
184 .ops = &clk_regmap_gate_ro_ops,
185 .parent_hws = (const struct clk_hw *[]) {
192 static struct clk_fixed_factor s4_fclk_div7_div = {
195 .hw.init = &(struct clk_init_data){
196 .name = "fclk_div7_div",
197 .ops = &clk_fixed_factor_ops,
198 .parent_hws = (const struct clk_hw *[]) { &s4_fixed_pll.hw },
203 static struct clk_regmap s4_fclk_div7 = {
204 .data = &(struct clk_regmap_gate_data){
205 .offset = ANACTRL_FIXPLL_CTRL1,
208 .hw.init = &(struct clk_init_data){
210 .ops = &clk_regmap_gate_ro_ops,
211 .parent_hws = (const struct clk_hw *[]) {
218 static struct clk_fixed_factor s4_fclk_div2p5_div = {
221 .hw.init = &(struct clk_init_data){
222 .name = "fclk_div2p5_div",
223 .ops = &clk_fixed_factor_ops,
224 .parent_hws = (const struct clk_hw *[]) {
231 static struct clk_regmap s4_fclk_div2p5 = {
232 .data = &(struct clk_regmap_gate_data){
233 .offset = ANACTRL_FIXPLL_CTRL1,
236 .hw.init = &(struct clk_init_data){
237 .name = "fclk_div2p5",
238 .ops = &clk_regmap_gate_ro_ops,
239 .parent_hws = (const struct clk_hw *[]) {
240 &s4_fclk_div2p5_div.hw
246 static const struct pll_mult_range s4_gp0_pll_mult_range = {
252 * Internal gp0 pll emulation configuration parameters
254 static const struct reg_sequence s4_gp0_init_regs[] = {
255 { .reg = ANACTRL_GP0PLL_CTRL1, .def = 0x00000000 },
256 { .reg = ANACTRL_GP0PLL_CTRL2, .def = 0x00000000 },
257 { .reg = ANACTRL_GP0PLL_CTRL3, .def = 0x48681c00 },
258 { .reg = ANACTRL_GP0PLL_CTRL4, .def = 0x88770290 },
259 { .reg = ANACTRL_GP0PLL_CTRL5, .def = 0x39272000 },
260 { .reg = ANACTRL_GP0PLL_CTRL6, .def = 0x56540000 }
263 static struct clk_regmap s4_gp0_pll_dco = {
264 .data = &(struct meson_clk_pll_data){
266 .reg_off = ANACTRL_GP0PLL_CTRL0,
271 .reg_off = ANACTRL_GP0PLL_CTRL0,
276 .reg_off = ANACTRL_GP0PLL_CTRL0,
281 .reg_off = ANACTRL_GP0PLL_CTRL0,
286 .reg_off = ANACTRL_GP0PLL_CTRL0,
290 .range = &s4_gp0_pll_mult_range,
291 .init_regs = s4_gp0_init_regs,
292 .init_count = ARRAY_SIZE(s4_gp0_init_regs),
294 .hw.init = &(struct clk_init_data){
295 .name = "gp0_pll_dco",
296 .ops = &meson_clk_pll_ops,
297 .parent_data = (const struct clk_parent_data []) {
298 { .fw_name = "xtal", }
304 static struct clk_regmap s4_gp0_pll = {
305 .data = &(struct clk_regmap_div_data){
306 .offset = ANACTRL_GP0PLL_CTRL0,
309 .flags = (CLK_DIVIDER_POWER_OF_TWO |
310 CLK_DIVIDER_ROUND_CLOSEST),
312 .hw.init = &(struct clk_init_data){
314 .ops = &clk_regmap_divider_ops,
315 .parent_hws = (const struct clk_hw *[]) {
319 .flags = CLK_SET_RATE_PARENT,
324 * Internal hifi pll emulation configuration parameters
326 static const struct reg_sequence s4_hifi_init_regs[] = {
327 { .reg = ANACTRL_HIFIPLL_CTRL1, .def = 0x00010e56 },
328 { .reg = ANACTRL_HIFIPLL_CTRL2, .def = 0x00000000 },
329 { .reg = ANACTRL_HIFIPLL_CTRL3, .def = 0x6a285c00 },
330 { .reg = ANACTRL_HIFIPLL_CTRL4, .def = 0x65771290 },
331 { .reg = ANACTRL_HIFIPLL_CTRL5, .def = 0x39272000 },
332 { .reg = ANACTRL_HIFIPLL_CTRL6, .def = 0x56540000 }
335 static struct clk_regmap s4_hifi_pll_dco = {
336 .data = &(struct meson_clk_pll_data){
338 .reg_off = ANACTRL_HIFIPLL_CTRL0,
343 .reg_off = ANACTRL_HIFIPLL_CTRL0,
348 .reg_off = ANACTRL_HIFIPLL_CTRL0,
353 .reg_off = ANACTRL_HIFIPLL_CTRL0,
358 .reg_off = ANACTRL_HIFIPLL_CTRL0,
362 .range = &s4_gp0_pll_mult_range,
363 .init_regs = s4_hifi_init_regs,
364 .init_count = ARRAY_SIZE(s4_hifi_init_regs),
365 .flags = CLK_MESON_PLL_ROUND_CLOSEST,
367 .hw.init = &(struct clk_init_data){
368 .name = "hifi_pll_dco",
369 .ops = &meson_clk_pll_ops,
370 .parent_data = (const struct clk_parent_data []) {
371 { .fw_name = "xtal", }
377 static struct clk_regmap s4_hifi_pll = {
378 .data = &(struct clk_regmap_div_data){
379 .offset = ANACTRL_HIFIPLL_CTRL0,
382 .flags = (CLK_DIVIDER_POWER_OF_TWO |
383 CLK_DIVIDER_ROUND_CLOSEST),
385 .hw.init = &(struct clk_init_data){
387 .ops = &clk_regmap_divider_ops,
388 .parent_hws = (const struct clk_hw *[]) {
392 .flags = CLK_SET_RATE_PARENT,
396 static struct clk_regmap s4_hdmi_pll_dco = {
397 .data = &(struct meson_clk_pll_data){
399 .reg_off = ANACTRL_HDMIPLL_CTRL0,
404 .reg_off = ANACTRL_HDMIPLL_CTRL0,
409 .reg_off = ANACTRL_HDMIPLL_CTRL0,
414 .reg_off = ANACTRL_HDMIPLL_CTRL0,
419 .reg_off = ANACTRL_HDMIPLL_CTRL0,
423 .range = &s4_gp0_pll_mult_range,
425 .hw.init = &(struct clk_init_data){
426 .name = "hdmi_pll_dco",
427 .ops = &meson_clk_pll_ops,
428 .parent_data = (const struct clk_parent_data []) {
429 { .fw_name = "xtal", }
435 static struct clk_regmap s4_hdmi_pll_od = {
436 .data = &(struct clk_regmap_div_data){
437 .offset = ANACTRL_HDMIPLL_CTRL0,
440 .flags = CLK_DIVIDER_POWER_OF_TWO,
442 .hw.init = &(struct clk_init_data){
443 .name = "hdmi_pll_od",
444 .ops = &clk_regmap_divider_ops,
445 .parent_hws = (const struct clk_hw *[]) {
449 .flags = CLK_SET_RATE_PARENT,
453 static struct clk_regmap s4_hdmi_pll = {
454 .data = &(struct clk_regmap_div_data){
455 .offset = ANACTRL_HDMIPLL_CTRL0,
458 .flags = CLK_DIVIDER_POWER_OF_TWO,
460 .hw.init = &(struct clk_init_data){
462 .ops = &clk_regmap_divider_ops,
463 .parent_hws = (const struct clk_hw *[]) {
467 .flags = CLK_SET_RATE_PARENT,
471 static struct clk_fixed_factor s4_mpll_50m_div = {
474 .hw.init = &(struct clk_init_data){
475 .name = "mpll_50m_div",
476 .ops = &clk_fixed_factor_ops,
477 .parent_hws = (const struct clk_hw *[]) {
484 static struct clk_regmap s4_mpll_50m = {
485 .data = &(struct clk_regmap_mux_data){
486 .offset = ANACTRL_FIXPLL_CTRL3,
490 .hw.init = &(struct clk_init_data){
492 .ops = &clk_regmap_mux_ro_ops,
493 .parent_data = (const struct clk_parent_data []) {
494 { .fw_name = "xtal", },
495 { .hw = &s4_mpll_50m_div.hw },
501 static struct clk_fixed_factor s4_mpll_prediv = {
504 .hw.init = &(struct clk_init_data){
505 .name = "mpll_prediv",
506 .ops = &clk_fixed_factor_ops,
507 .parent_hws = (const struct clk_hw *[]) {
514 static const struct reg_sequence s4_mpll0_init_regs[] = {
515 { .reg = ANACTRL_MPLL_CTRL2, .def = 0x40000033 }
518 static struct clk_regmap s4_mpll0_div = {
519 .data = &(struct meson_clk_mpll_data){
521 .reg_off = ANACTRL_MPLL_CTRL1,
526 .reg_off = ANACTRL_MPLL_CTRL1,
531 .reg_off = ANACTRL_MPLL_CTRL1,
536 .reg_off = ANACTRL_MPLL_CTRL1,
540 .lock = &meson_clk_lock,
541 .init_regs = s4_mpll0_init_regs,
542 .init_count = ARRAY_SIZE(s4_mpll0_init_regs),
544 .hw.init = &(struct clk_init_data){
546 .ops = &meson_clk_mpll_ops,
547 .parent_hws = (const struct clk_hw *[]) {
554 static struct clk_regmap s4_mpll0 = {
555 .data = &(struct clk_regmap_gate_data){
556 .offset = ANACTRL_MPLL_CTRL1,
559 .hw.init = &(struct clk_init_data){
561 .ops = &clk_regmap_gate_ops,
562 .parent_hws = (const struct clk_hw *[]) { &s4_mpll0_div.hw },
564 .flags = CLK_SET_RATE_PARENT,
568 static const struct reg_sequence s4_mpll1_init_regs[] = {
569 { .reg = ANACTRL_MPLL_CTRL4, .def = 0x40000033 }
572 static struct clk_regmap s4_mpll1_div = {
573 .data = &(struct meson_clk_mpll_data){
575 .reg_off = ANACTRL_MPLL_CTRL3,
580 .reg_off = ANACTRL_MPLL_CTRL3,
585 .reg_off = ANACTRL_MPLL_CTRL3,
590 .reg_off = ANACTRL_MPLL_CTRL3,
594 .lock = &meson_clk_lock,
595 .init_regs = s4_mpll1_init_regs,
596 .init_count = ARRAY_SIZE(s4_mpll1_init_regs),
598 .hw.init = &(struct clk_init_data){
600 .ops = &meson_clk_mpll_ops,
601 .parent_hws = (const struct clk_hw *[]) {
608 static struct clk_regmap s4_mpll1 = {
609 .data = &(struct clk_regmap_gate_data){
610 .offset = ANACTRL_MPLL_CTRL3,
613 .hw.init = &(struct clk_init_data){
615 .ops = &clk_regmap_gate_ops,
616 .parent_hws = (const struct clk_hw *[]) { &s4_mpll1_div.hw },
618 .flags = CLK_SET_RATE_PARENT,
622 static const struct reg_sequence s4_mpll2_init_regs[] = {
623 { .reg = ANACTRL_MPLL_CTRL6, .def = 0x40000033 }
626 static struct clk_regmap s4_mpll2_div = {
627 .data = &(struct meson_clk_mpll_data){
629 .reg_off = ANACTRL_MPLL_CTRL5,
634 .reg_off = ANACTRL_MPLL_CTRL5,
639 .reg_off = ANACTRL_MPLL_CTRL5,
644 .reg_off = ANACTRL_MPLL_CTRL5,
648 .lock = &meson_clk_lock,
649 .init_regs = s4_mpll2_init_regs,
650 .init_count = ARRAY_SIZE(s4_mpll2_init_regs),
652 .hw.init = &(struct clk_init_data){
654 .ops = &meson_clk_mpll_ops,
655 .parent_hws = (const struct clk_hw *[]) {
662 static struct clk_regmap s4_mpll2 = {
663 .data = &(struct clk_regmap_gate_data){
664 .offset = ANACTRL_MPLL_CTRL5,
667 .hw.init = &(struct clk_init_data){
669 .ops = &clk_regmap_gate_ops,
670 .parent_hws = (const struct clk_hw *[]) { &s4_mpll2_div.hw },
672 .flags = CLK_SET_RATE_PARENT,
676 static const struct reg_sequence s4_mpll3_init_regs[] = {
677 { .reg = ANACTRL_MPLL_CTRL8, .def = 0x40000033 }
680 static struct clk_regmap s4_mpll3_div = {
681 .data = &(struct meson_clk_mpll_data){
683 .reg_off = ANACTRL_MPLL_CTRL7,
688 .reg_off = ANACTRL_MPLL_CTRL7,
693 .reg_off = ANACTRL_MPLL_CTRL7,
698 .reg_off = ANACTRL_MPLL_CTRL7,
702 .lock = &meson_clk_lock,
703 .init_regs = s4_mpll3_init_regs,
704 .init_count = ARRAY_SIZE(s4_mpll3_init_regs),
706 .hw.init = &(struct clk_init_data){
708 .ops = &meson_clk_mpll_ops,
709 .parent_hws = (const struct clk_hw *[]) {
716 static struct clk_regmap s4_mpll3 = {
717 .data = &(struct clk_regmap_gate_data){
718 .offset = ANACTRL_MPLL_CTRL7,
721 .hw.init = &(struct clk_init_data){
723 .ops = &clk_regmap_gate_ops,
724 .parent_hws = (const struct clk_hw *[]) { &s4_mpll3_div.hw },
726 .flags = CLK_SET_RATE_PARENT,
730 /* Array of all clocks provided by this provider */
731 static struct clk_hw *s4_pll_hw_clks[] = {
732 [CLKID_FIXED_PLL_DCO] = &s4_fixed_pll_dco.hw,
733 [CLKID_FIXED_PLL] = &s4_fixed_pll.hw,
734 [CLKID_FCLK_DIV2_DIV] = &s4_fclk_div2_div.hw,
735 [CLKID_FCLK_DIV2] = &s4_fclk_div2.hw,
736 [CLKID_FCLK_DIV3_DIV] = &s4_fclk_div3_div.hw,
737 [CLKID_FCLK_DIV3] = &s4_fclk_div3.hw,
738 [CLKID_FCLK_DIV4_DIV] = &s4_fclk_div4_div.hw,
739 [CLKID_FCLK_DIV4] = &s4_fclk_div4.hw,
740 [CLKID_FCLK_DIV5_DIV] = &s4_fclk_div5_div.hw,
741 [CLKID_FCLK_DIV5] = &s4_fclk_div5.hw,
742 [CLKID_FCLK_DIV7_DIV] = &s4_fclk_div7_div.hw,
743 [CLKID_FCLK_DIV7] = &s4_fclk_div7.hw,
744 [CLKID_FCLK_DIV2P5_DIV] = &s4_fclk_div2p5_div.hw,
745 [CLKID_FCLK_DIV2P5] = &s4_fclk_div2p5.hw,
746 [CLKID_GP0_PLL_DCO] = &s4_gp0_pll_dco.hw,
747 [CLKID_GP0_PLL] = &s4_gp0_pll.hw,
748 [CLKID_HIFI_PLL_DCO] = &s4_hifi_pll_dco.hw,
749 [CLKID_HIFI_PLL] = &s4_hifi_pll.hw,
750 [CLKID_HDMI_PLL_DCO] = &s4_hdmi_pll_dco.hw,
751 [CLKID_HDMI_PLL_OD] = &s4_hdmi_pll_od.hw,
752 [CLKID_HDMI_PLL] = &s4_hdmi_pll.hw,
753 [CLKID_MPLL_50M_DIV] = &s4_mpll_50m_div.hw,
754 [CLKID_MPLL_50M] = &s4_mpll_50m.hw,
755 [CLKID_MPLL_PREDIV] = &s4_mpll_prediv.hw,
756 [CLKID_MPLL0_DIV] = &s4_mpll0_div.hw,
757 [CLKID_MPLL0] = &s4_mpll0.hw,
758 [CLKID_MPLL1_DIV] = &s4_mpll1_div.hw,
759 [CLKID_MPLL1] = &s4_mpll1.hw,
760 [CLKID_MPLL2_DIV] = &s4_mpll2_div.hw,
761 [CLKID_MPLL2] = &s4_mpll2.hw,
762 [CLKID_MPLL3_DIV] = &s4_mpll3_div.hw,
763 [CLKID_MPLL3] = &s4_mpll3.hw,
766 static struct clk_regmap *const s4_pll_clk_regmaps[] = {
793 static const struct reg_sequence s4_init_regs[] = {
794 { .reg = ANACTRL_MPLL_CTRL0, .def = 0x00000543 },
797 static struct regmap_config clkc_regmap_config = {
803 static struct meson_clk_hw_data s4_pll_clks = {
804 .hws = s4_pll_hw_clks,
805 .num = ARRAY_SIZE(s4_pll_hw_clks),
808 static int meson_s4_pll_probe(struct platform_device *pdev)
810 struct device *dev = &pdev->dev;
811 struct regmap *regmap;
815 base = devm_platform_ioremap_resource(pdev, 0);
817 return dev_err_probe(dev, PTR_ERR(base),
818 "can't ioremap resource\n");
820 regmap = devm_regmap_init_mmio(dev, base, &clkc_regmap_config);
822 return dev_err_probe(dev, PTR_ERR(regmap),
823 "can't init regmap mmio region\n");
825 ret = regmap_multi_reg_write(regmap, s4_init_regs, ARRAY_SIZE(s4_init_regs));
827 return dev_err_probe(dev, ret,
828 "Failed to init registers\n");
830 /* Populate regmap for the regmap backed clocks */
831 for (i = 0; i < ARRAY_SIZE(s4_pll_clk_regmaps); i++)
832 s4_pll_clk_regmaps[i]->map = regmap;
834 /* Register clocks */
835 for (i = 0; i < s4_pll_clks.num; i++) {
836 /* array might be sparse */
837 if (!s4_pll_clks.hws[i])
840 ret = devm_clk_hw_register(dev, s4_pll_clks.hws[i]);
842 return dev_err_probe(dev, ret,
843 "clock[%d] registration failed\n", i);
846 return devm_of_clk_add_hw_provider(dev, meson_clk_hw_get,
850 static const struct of_device_id clkc_match_table[] = {
852 .compatible = "amlogic,s4-pll-clkc",
857 static struct platform_driver s4_driver = {
858 .probe = meson_s4_pll_probe,
860 .name = "s4-pll-clkc",
861 .of_match_table = clkc_match_table,
865 module_platform_driver(s4_driver);
866 MODULE_AUTHOR("Yu Tu <yu.tu@amlogic.com>");
867 MODULE_LICENSE("GPL");