2 * Copyright 2014 Chen-Yu Tsai
4 * Chen-Yu Tsai <wens@csie.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
20 #include <linux/of_address.h>
21 #include <linux/log2.h>
23 #include "clk-factors.h"
27 * sun9i_a80_get_pll4_factors() - calculates n, p, m factors for PLL4
28 * PLL4 rate is calculated as follows
29 * rate = (parent_rate * n >> p) / (m + 1);
30 * parent_rate is always 24MHz
32 * p and m are named div1 and div2 in Allwinner's SDK
35 static void sun9i_a80_get_pll4_factors(struct factors_request
*req
)
41 /* Normalize value to a 6 MHz multiple (24 MHz / 4) */
42 n
= DIV_ROUND_UP(req
->rate
, 6000000);
44 /* If n is too large switch to steps of 12 MHz */
50 /* If n is still too large switch to steps of 24 MHz */
56 /* n must be between 12 and 255 */
62 req
->rate
= ((24000000 * n
) >> p
) / (m
+ 1);
68 static const struct clk_factors_config sun9i_a80_pll4_config
= {
77 static const struct factors_data sun9i_a80_pll4_data __initconst
= {
79 .table
= &sun9i_a80_pll4_config
,
80 .getter
= sun9i_a80_get_pll4_factors
,
83 static DEFINE_SPINLOCK(sun9i_a80_pll4_lock
);
85 static void __init
sun9i_a80_pll4_setup(struct device_node
*node
)
89 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
91 pr_err("Could not get registers for a80-pll4-clk: %s\n",
96 sunxi_factors_register(node
, &sun9i_a80_pll4_data
,
97 &sun9i_a80_pll4_lock
, reg
);
99 CLK_OF_DECLARE(sun9i_a80_pll4
, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup
);
103 * sun9i_a80_get_gt_factors() - calculates m factor for GT
104 * GT rate is calculated as follows
105 * rate = parent_rate / (m + 1);
108 static void sun9i_a80_get_gt_factors(struct factors_request
*req
)
112 if (req
->parent_rate
< req
->rate
)
113 req
->rate
= req
->parent_rate
;
115 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
117 /* maximum divider is 4 */
121 req
->rate
= req
->parent_rate
/ div
;
125 static const struct clk_factors_config sun9i_a80_gt_config
= {
130 static const struct factors_data sun9i_a80_gt_data __initconst
= {
132 .muxmask
= BIT(1) | BIT(0),
133 .table
= &sun9i_a80_gt_config
,
134 .getter
= sun9i_a80_get_gt_factors
,
137 static DEFINE_SPINLOCK(sun9i_a80_gt_lock
);
139 static void __init
sun9i_a80_gt_setup(struct device_node
*node
)
143 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
145 pr_err("Could not get registers for a80-gt-clk: %s\n",
150 /* The GT bus clock needs to be always enabled */
151 sunxi_factors_register_critical(node
, &sun9i_a80_gt_data
,
152 &sun9i_a80_gt_lock
, reg
);
154 CLK_OF_DECLARE(sun9i_a80_gt
, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup
);
158 * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
159 * AHB rate is calculated as follows
160 * rate = parent_rate >> p;
163 static void sun9i_a80_get_ahb_factors(struct factors_request
*req
)
167 if (req
->parent_rate
< req
->rate
)
168 req
->rate
= req
->parent_rate
;
170 _p
= order_base_2(DIV_ROUND_UP(req
->parent_rate
, req
->rate
));
176 req
->rate
= req
->parent_rate
>> _p
;
180 static const struct clk_factors_config sun9i_a80_ahb_config
= {
185 static const struct factors_data sun9i_a80_ahb_data __initconst
= {
187 .muxmask
= BIT(1) | BIT(0),
188 .table
= &sun9i_a80_ahb_config
,
189 .getter
= sun9i_a80_get_ahb_factors
,
192 static DEFINE_SPINLOCK(sun9i_a80_ahb_lock
);
194 static void __init
sun9i_a80_ahb_setup(struct device_node
*node
)
198 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
200 pr_err("Could not get registers for a80-ahb-clk: %s\n",
205 sunxi_factors_register(node
, &sun9i_a80_ahb_data
,
206 &sun9i_a80_ahb_lock
, reg
);
208 CLK_OF_DECLARE(sun9i_a80_ahb
, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup
);
211 static const struct factors_data sun9i_a80_apb0_data __initconst
= {
214 .table
= &sun9i_a80_ahb_config
,
215 .getter
= sun9i_a80_get_ahb_factors
,
218 static DEFINE_SPINLOCK(sun9i_a80_apb0_lock
);
220 static void __init
sun9i_a80_apb0_setup(struct device_node
*node
)
224 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
226 pr_err("Could not get registers for a80-apb0-clk: %s\n",
231 sunxi_factors_register(node
, &sun9i_a80_apb0_data
,
232 &sun9i_a80_apb0_lock
, reg
);
234 CLK_OF_DECLARE(sun9i_a80_apb0
, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup
);
238 * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
239 * APB1 rate is calculated as follows
240 * rate = (parent_rate >> p) / (m + 1);
243 static void sun9i_a80_get_apb1_factors(struct factors_request
*req
)
247 if (req
->parent_rate
< req
->rate
)
248 req
->rate
= req
->parent_rate
;
250 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
252 /* Highest possible divider is 256 (p = 3, m = 31) */
256 req
->p
= order_base_2(div
);
257 req
->m
= (req
->parent_rate
>> req
->p
) - 1;
258 req
->rate
= (req
->parent_rate
>> req
->p
) / (req
->m
+ 1);
261 static const struct clk_factors_config sun9i_a80_apb1_config
= {
268 static const struct factors_data sun9i_a80_apb1_data __initconst
= {
271 .table
= &sun9i_a80_apb1_config
,
272 .getter
= sun9i_a80_get_apb1_factors
,
275 static DEFINE_SPINLOCK(sun9i_a80_apb1_lock
);
277 static void __init
sun9i_a80_apb1_setup(struct device_node
*node
)
281 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
283 pr_err("Could not get registers for a80-apb1-clk: %s\n",
288 sunxi_factors_register(node
, &sun9i_a80_apb1_data
,
289 &sun9i_a80_apb1_lock
, reg
);
291 CLK_OF_DECLARE(sun9i_a80_apb1
, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup
);