1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2013 Emilio López
4*4882a593Smuzhiyun * Emilio López <emilio@elopez.com.ar>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright 2015 Maxime Ripard
7*4882a593Smuzhiyun * Maxime Ripard <maxime.ripard@free-electrons.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/clk-provider.h>
11*4882a593Smuzhiyun #include <linux/io.h>
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/of_address.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <dt-bindings/clock/sun4i-a10-pll2.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define SUN4I_PLL2_ENABLE 31
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define SUN4I_PLL2_PRE_DIV_SHIFT 0
21*4882a593Smuzhiyun #define SUN4I_PLL2_PRE_DIV_WIDTH 5
22*4882a593Smuzhiyun #define SUN4I_PLL2_PRE_DIV_MASK GENMASK(SUN4I_PLL2_PRE_DIV_WIDTH - 1, 0)
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define SUN4I_PLL2_N_SHIFT 8
25*4882a593Smuzhiyun #define SUN4I_PLL2_N_WIDTH 7
26*4882a593Smuzhiyun #define SUN4I_PLL2_N_MASK GENMASK(SUN4I_PLL2_N_WIDTH - 1, 0)
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define SUN4I_PLL2_POST_DIV_SHIFT 26
29*4882a593Smuzhiyun #define SUN4I_PLL2_POST_DIV_WIDTH 4
30*4882a593Smuzhiyun #define SUN4I_PLL2_POST_DIV_MASK GENMASK(SUN4I_PLL2_POST_DIV_WIDTH - 1, 0)
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define SUN4I_PLL2_POST_DIV_VALUE 4
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define SUN4I_PLL2_OUTPUTS 4
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun static DEFINE_SPINLOCK(sun4i_a10_pll2_lock);
37*4882a593Smuzhiyun
sun4i_pll2_setup(struct device_node * node,int post_div_offset)38*4882a593Smuzhiyun static void __init sun4i_pll2_setup(struct device_node *node,
39*4882a593Smuzhiyun int post_div_offset)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun const char *clk_name = node->name, *parent;
42*4882a593Smuzhiyun struct clk **clks, *base_clk, *prediv_clk;
43*4882a593Smuzhiyun struct clk_onecell_data *clk_data;
44*4882a593Smuzhiyun struct clk_multiplier *mult;
45*4882a593Smuzhiyun struct clk_gate *gate;
46*4882a593Smuzhiyun void __iomem *reg;
47*4882a593Smuzhiyun u32 val;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun reg = of_io_request_and_map(node, 0, of_node_full_name(node));
50*4882a593Smuzhiyun if (IS_ERR(reg))
51*4882a593Smuzhiyun return;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
54*4882a593Smuzhiyun if (!clk_data)
55*4882a593Smuzhiyun goto err_unmap;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun clks = kcalloc(SUN4I_PLL2_OUTPUTS, sizeof(struct clk *), GFP_KERNEL);
58*4882a593Smuzhiyun if (!clks)
59*4882a593Smuzhiyun goto err_free_data;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun parent = of_clk_get_parent_name(node, 0);
62*4882a593Smuzhiyun prediv_clk = clk_register_divider(NULL, "pll2-prediv",
63*4882a593Smuzhiyun parent, 0, reg,
64*4882a593Smuzhiyun SUN4I_PLL2_PRE_DIV_SHIFT,
65*4882a593Smuzhiyun SUN4I_PLL2_PRE_DIV_WIDTH,
66*4882a593Smuzhiyun CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO,
67*4882a593Smuzhiyun &sun4i_a10_pll2_lock);
68*4882a593Smuzhiyun if (IS_ERR(prediv_clk)) {
69*4882a593Smuzhiyun pr_err("Couldn't register the prediv clock\n");
70*4882a593Smuzhiyun goto err_free_array;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Setup the gate part of the PLL2 */
74*4882a593Smuzhiyun gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
75*4882a593Smuzhiyun if (!gate)
76*4882a593Smuzhiyun goto err_unregister_prediv;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun gate->reg = reg;
79*4882a593Smuzhiyun gate->bit_idx = SUN4I_PLL2_ENABLE;
80*4882a593Smuzhiyun gate->lock = &sun4i_a10_pll2_lock;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* Setup the multiplier part of the PLL2 */
83*4882a593Smuzhiyun mult = kzalloc(sizeof(struct clk_multiplier), GFP_KERNEL);
84*4882a593Smuzhiyun if (!mult)
85*4882a593Smuzhiyun goto err_free_gate;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun mult->reg = reg;
88*4882a593Smuzhiyun mult->shift = SUN4I_PLL2_N_SHIFT;
89*4882a593Smuzhiyun mult->width = 7;
90*4882a593Smuzhiyun mult->flags = CLK_MULTIPLIER_ZERO_BYPASS |
91*4882a593Smuzhiyun CLK_MULTIPLIER_ROUND_CLOSEST;
92*4882a593Smuzhiyun mult->lock = &sun4i_a10_pll2_lock;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun parent = __clk_get_name(prediv_clk);
95*4882a593Smuzhiyun base_clk = clk_register_composite(NULL, "pll2-base",
96*4882a593Smuzhiyun &parent, 1,
97*4882a593Smuzhiyun NULL, NULL,
98*4882a593Smuzhiyun &mult->hw, &clk_multiplier_ops,
99*4882a593Smuzhiyun &gate->hw, &clk_gate_ops,
100*4882a593Smuzhiyun CLK_SET_RATE_PARENT);
101*4882a593Smuzhiyun if (IS_ERR(base_clk)) {
102*4882a593Smuzhiyun pr_err("Couldn't register the base multiplier clock\n");
103*4882a593Smuzhiyun goto err_free_multiplier;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun parent = __clk_get_name(base_clk);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * PLL2-1x
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * This is supposed to have a post divider, but we won't need
112*4882a593Smuzhiyun * to use it, we just need to initialise it to 4, and use a
113*4882a593Smuzhiyun * fixed divider.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun val = readl(reg);
116*4882a593Smuzhiyun val &= ~(SUN4I_PLL2_POST_DIV_MASK << SUN4I_PLL2_POST_DIV_SHIFT);
117*4882a593Smuzhiyun val |= (SUN4I_PLL2_POST_DIV_VALUE - post_div_offset) << SUN4I_PLL2_POST_DIV_SHIFT;
118*4882a593Smuzhiyun writel(val, reg);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun of_property_read_string_index(node, "clock-output-names",
121*4882a593Smuzhiyun SUN4I_A10_PLL2_1X, &clk_name);
122*4882a593Smuzhiyun clks[SUN4I_A10_PLL2_1X] = clk_register_fixed_factor(NULL, clk_name,
123*4882a593Smuzhiyun parent,
124*4882a593Smuzhiyun CLK_SET_RATE_PARENT,
125*4882a593Smuzhiyun 1,
126*4882a593Smuzhiyun SUN4I_PLL2_POST_DIV_VALUE);
127*4882a593Smuzhiyun WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_1X]));
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun * PLL2-2x
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * This clock doesn't use the post divider, and really is just
133*4882a593Smuzhiyun * a fixed divider from the PLL2 base clock.
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun of_property_read_string_index(node, "clock-output-names",
136*4882a593Smuzhiyun SUN4I_A10_PLL2_2X, &clk_name);
137*4882a593Smuzhiyun clks[SUN4I_A10_PLL2_2X] = clk_register_fixed_factor(NULL, clk_name,
138*4882a593Smuzhiyun parent,
139*4882a593Smuzhiyun CLK_SET_RATE_PARENT,
140*4882a593Smuzhiyun 1, 2);
141*4882a593Smuzhiyun WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_2X]));
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* PLL2-4x */
144*4882a593Smuzhiyun of_property_read_string_index(node, "clock-output-names",
145*4882a593Smuzhiyun SUN4I_A10_PLL2_4X, &clk_name);
146*4882a593Smuzhiyun clks[SUN4I_A10_PLL2_4X] = clk_register_fixed_factor(NULL, clk_name,
147*4882a593Smuzhiyun parent,
148*4882a593Smuzhiyun CLK_SET_RATE_PARENT,
149*4882a593Smuzhiyun 1, 1);
150*4882a593Smuzhiyun WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_4X]));
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* PLL2-8x */
153*4882a593Smuzhiyun of_property_read_string_index(node, "clock-output-names",
154*4882a593Smuzhiyun SUN4I_A10_PLL2_8X, &clk_name);
155*4882a593Smuzhiyun clks[SUN4I_A10_PLL2_8X] = clk_register_fixed_factor(NULL, clk_name,
156*4882a593Smuzhiyun parent,
157*4882a593Smuzhiyun CLK_SET_RATE_PARENT,
158*4882a593Smuzhiyun 2, 1);
159*4882a593Smuzhiyun WARN_ON(IS_ERR(clks[SUN4I_A10_PLL2_8X]));
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun clk_data->clks = clks;
162*4882a593Smuzhiyun clk_data->clk_num = SUN4I_PLL2_OUTPUTS;
163*4882a593Smuzhiyun of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun return;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun err_free_multiplier:
168*4882a593Smuzhiyun kfree(mult);
169*4882a593Smuzhiyun err_free_gate:
170*4882a593Smuzhiyun kfree(gate);
171*4882a593Smuzhiyun err_unregister_prediv:
172*4882a593Smuzhiyun clk_unregister_divider(prediv_clk);
173*4882a593Smuzhiyun err_free_array:
174*4882a593Smuzhiyun kfree(clks);
175*4882a593Smuzhiyun err_free_data:
176*4882a593Smuzhiyun kfree(clk_data);
177*4882a593Smuzhiyun err_unmap:
178*4882a593Smuzhiyun iounmap(reg);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
sun4i_a10_pll2_setup(struct device_node * node)181*4882a593Smuzhiyun static void __init sun4i_a10_pll2_setup(struct device_node *node)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun sun4i_pll2_setup(node, 0);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun CLK_OF_DECLARE(sun4i_a10_pll2, "allwinner,sun4i-a10-pll2-clk",
187*4882a593Smuzhiyun sun4i_a10_pll2_setup);
188*4882a593Smuzhiyun
sun5i_a13_pll2_setup(struct device_node * node)189*4882a593Smuzhiyun static void __init sun5i_a13_pll2_setup(struct device_node *node)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun sun4i_pll2_setup(node, 1);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun CLK_OF_DECLARE(sun5i_a13_pll2, "allwinner,sun5i-a13-pll2-clk",
195*4882a593Smuzhiyun sun5i_a13_pll2_setup);
196