1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2014 Chen-Yu Tsai
5 * Chen-Yu Tsai <wens@csie.org>
9 #include <linux/clk-provider.h>
11 #include <linux/of_address.h>
12 #include <linux/log2.h>
14 #include "clk-factors.h"
18 * sun9i_a80_get_pll4_factors() - calculates n, p, m factors for PLL4
19 * PLL4 rate is calculated as follows
20 * rate = (parent_rate * n >> p) / (m + 1);
21 * parent_rate is always 24MHz
23 * p and m are named div1 and div2 in Allwinner's SDK
26 static void sun9i_a80_get_pll4_factors(struct factors_request
*req
)
32 /* Normalize value to a 6 MHz multiple (24 MHz / 4) */
33 n
= DIV_ROUND_UP(req
->rate
, 6000000);
35 /* If n is too large switch to steps of 12 MHz */
41 /* If n is still too large switch to steps of 24 MHz */
47 /* n must be between 12 and 255 */
53 req
->rate
= ((24000000 * n
) >> p
) / (m
+ 1);
59 static const struct clk_factors_config sun9i_a80_pll4_config
= {
68 static const struct factors_data sun9i_a80_pll4_data __initconst
= {
70 .table
= &sun9i_a80_pll4_config
,
71 .getter
= sun9i_a80_get_pll4_factors
,
74 static DEFINE_SPINLOCK(sun9i_a80_pll4_lock
);
76 static void __init
sun9i_a80_pll4_setup(struct device_node
*node
)
80 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
82 pr_err("Could not get registers for a80-pll4-clk: %pOFn\n",
87 sunxi_factors_register(node
, &sun9i_a80_pll4_data
,
88 &sun9i_a80_pll4_lock
, reg
);
90 CLK_OF_DECLARE(sun9i_a80_pll4
, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup
);
94 * sun9i_a80_get_gt_factors() - calculates m factor for GT
95 * GT rate is calculated as follows
96 * rate = parent_rate / (m + 1);
99 static void sun9i_a80_get_gt_factors(struct factors_request
*req
)
103 if (req
->parent_rate
< req
->rate
)
104 req
->rate
= req
->parent_rate
;
106 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
108 /* maximum divider is 4 */
112 req
->rate
= req
->parent_rate
/ div
;
116 static const struct clk_factors_config sun9i_a80_gt_config
= {
121 static const struct factors_data sun9i_a80_gt_data __initconst
= {
123 .muxmask
= BIT(1) | BIT(0),
124 .table
= &sun9i_a80_gt_config
,
125 .getter
= sun9i_a80_get_gt_factors
,
128 static DEFINE_SPINLOCK(sun9i_a80_gt_lock
);
130 static void __init
sun9i_a80_gt_setup(struct device_node
*node
)
134 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
136 pr_err("Could not get registers for a80-gt-clk: %pOFn\n",
141 /* The GT bus clock needs to be always enabled */
142 sunxi_factors_register_critical(node
, &sun9i_a80_gt_data
,
143 &sun9i_a80_gt_lock
, reg
);
145 CLK_OF_DECLARE(sun9i_a80_gt
, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup
);
149 * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
150 * AHB rate is calculated as follows
151 * rate = parent_rate >> p;
154 static void sun9i_a80_get_ahb_factors(struct factors_request
*req
)
158 if (req
->parent_rate
< req
->rate
)
159 req
->rate
= req
->parent_rate
;
161 _p
= order_base_2(DIV_ROUND_UP(req
->parent_rate
, req
->rate
));
167 req
->rate
= req
->parent_rate
>> _p
;
171 static const struct clk_factors_config sun9i_a80_ahb_config
= {
176 static const struct factors_data sun9i_a80_ahb_data __initconst
= {
178 .muxmask
= BIT(1) | BIT(0),
179 .table
= &sun9i_a80_ahb_config
,
180 .getter
= sun9i_a80_get_ahb_factors
,
183 static DEFINE_SPINLOCK(sun9i_a80_ahb_lock
);
185 static void __init
sun9i_a80_ahb_setup(struct device_node
*node
)
189 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
191 pr_err("Could not get registers for a80-ahb-clk: %pOFn\n",
196 sunxi_factors_register(node
, &sun9i_a80_ahb_data
,
197 &sun9i_a80_ahb_lock
, reg
);
199 CLK_OF_DECLARE(sun9i_a80_ahb
, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup
);
202 static const struct factors_data sun9i_a80_apb0_data __initconst
= {
205 .table
= &sun9i_a80_ahb_config
,
206 .getter
= sun9i_a80_get_ahb_factors
,
209 static DEFINE_SPINLOCK(sun9i_a80_apb0_lock
);
211 static void __init
sun9i_a80_apb0_setup(struct device_node
*node
)
215 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
217 pr_err("Could not get registers for a80-apb0-clk: %pOFn\n",
222 sunxi_factors_register(node
, &sun9i_a80_apb0_data
,
223 &sun9i_a80_apb0_lock
, reg
);
225 CLK_OF_DECLARE(sun9i_a80_apb0
, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup
);
229 * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
230 * APB1 rate is calculated as follows
231 * rate = (parent_rate >> p) / (m + 1);
234 static void sun9i_a80_get_apb1_factors(struct factors_request
*req
)
238 if (req
->parent_rate
< req
->rate
)
239 req
->rate
= req
->parent_rate
;
241 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
243 /* Highest possible divider is 256 (p = 3, m = 31) */
247 req
->p
= order_base_2(div
);
248 req
->m
= (req
->parent_rate
>> req
->p
) - 1;
249 req
->rate
= (req
->parent_rate
>> req
->p
) / (req
->m
+ 1);
252 static const struct clk_factors_config sun9i_a80_apb1_config
= {
259 static const struct factors_data sun9i_a80_apb1_data __initconst
= {
262 .table
= &sun9i_a80_apb1_config
,
263 .getter
= sun9i_a80_get_apb1_factors
,
266 static DEFINE_SPINLOCK(sun9i_a80_apb1_lock
);
268 static void __init
sun9i_a80_apb1_setup(struct device_node
*node
)
272 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
274 pr_err("Could not get registers for a80-apb1-clk: %pOFn\n",
279 sunxi_factors_register(node
, &sun9i_a80_apb1_data
,
280 &sun9i_a80_apb1_lock
, reg
);
282 CLK_OF_DECLARE(sun9i_a80_apb1
, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup
);