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/clkdev.h>
19 #include <linux/clk-provider.h>
21 #include <linux/of_address.h>
22 #include <linux/log2.h>
24 #include "clk-factors.h"
28 * sun9i_a80_get_pll4_factors() - calculates n, p, m factors for PLL4
29 * PLL4 rate is calculated as follows
30 * rate = (parent_rate * n >> p) / (m + 1);
31 * parent_rate is always 24MHz
33 * p and m are named div1 and div2 in Allwinner's SDK
36 static void sun9i_a80_get_pll4_factors(struct factors_request
*req
)
42 /* Normalize value to a 6 MHz multiple (24 MHz / 4) */
43 n
= DIV_ROUND_UP(req
->rate
, 6000000);
45 /* If n is too large switch to steps of 12 MHz */
51 /* If n is still too large switch to steps of 24 MHz */
57 /* n must be between 12 and 255 */
63 req
->rate
= ((24000000 * n
) >> p
) / (m
+ 1);
69 static const struct clk_factors_config sun9i_a80_pll4_config
= {
78 static const struct factors_data sun9i_a80_pll4_data __initconst
= {
80 .table
= &sun9i_a80_pll4_config
,
81 .getter
= sun9i_a80_get_pll4_factors
,
84 static DEFINE_SPINLOCK(sun9i_a80_pll4_lock
);
86 static void __init
sun9i_a80_pll4_setup(struct device_node
*node
)
90 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
92 pr_err("Could not get registers for a80-pll4-clk: %s\n",
97 sunxi_factors_register(node
, &sun9i_a80_pll4_data
,
98 &sun9i_a80_pll4_lock
, reg
);
100 CLK_OF_DECLARE(sun9i_a80_pll4
, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup
);
104 * sun9i_a80_get_gt_factors() - calculates m factor for GT
105 * GT rate is calculated as follows
106 * rate = parent_rate / (m + 1);
109 static void sun9i_a80_get_gt_factors(struct factors_request
*req
)
113 if (req
->parent_rate
< req
->rate
)
114 req
->rate
= req
->parent_rate
;
116 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
118 /* maximum divider is 4 */
122 req
->rate
= req
->parent_rate
/ div
;
126 static const struct clk_factors_config sun9i_a80_gt_config
= {
131 static const struct factors_data sun9i_a80_gt_data __initconst
= {
133 .muxmask
= BIT(1) | BIT(0),
134 .table
= &sun9i_a80_gt_config
,
135 .getter
= sun9i_a80_get_gt_factors
,
138 static DEFINE_SPINLOCK(sun9i_a80_gt_lock
);
140 static void __init
sun9i_a80_gt_setup(struct device_node
*node
)
145 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
147 pr_err("Could not get registers for a80-gt-clk: %s\n",
152 gt
= sunxi_factors_register(node
, &sun9i_a80_gt_data
,
153 &sun9i_a80_gt_lock
, reg
);
155 /* The GT bus clock needs to be always enabled */
157 clk_prepare_enable(gt
);
159 CLK_OF_DECLARE(sun9i_a80_gt
, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup
);
163 * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
164 * AHB rate is calculated as follows
165 * rate = parent_rate >> p;
168 static void sun9i_a80_get_ahb_factors(struct factors_request
*req
)
172 if (req
->parent_rate
< req
->rate
)
173 req
->rate
= req
->parent_rate
;
175 _p
= order_base_2(DIV_ROUND_UP(req
->parent_rate
, req
->rate
));
181 req
->rate
= req
->parent_rate
>> _p
;
185 static const struct clk_factors_config sun9i_a80_ahb_config
= {
190 static const struct factors_data sun9i_a80_ahb_data __initconst
= {
192 .muxmask
= BIT(1) | BIT(0),
193 .table
= &sun9i_a80_ahb_config
,
194 .getter
= sun9i_a80_get_ahb_factors
,
197 static DEFINE_SPINLOCK(sun9i_a80_ahb_lock
);
199 static void __init
sun9i_a80_ahb_setup(struct device_node
*node
)
203 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
205 pr_err("Could not get registers for a80-ahb-clk: %s\n",
210 sunxi_factors_register(node
, &sun9i_a80_ahb_data
,
211 &sun9i_a80_ahb_lock
, reg
);
213 CLK_OF_DECLARE(sun9i_a80_ahb
, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup
);
216 static const struct factors_data sun9i_a80_apb0_data __initconst
= {
219 .table
= &sun9i_a80_ahb_config
,
220 .getter
= sun9i_a80_get_ahb_factors
,
223 static DEFINE_SPINLOCK(sun9i_a80_apb0_lock
);
225 static void __init
sun9i_a80_apb0_setup(struct device_node
*node
)
229 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
231 pr_err("Could not get registers for a80-apb0-clk: %s\n",
236 sunxi_factors_register(node
, &sun9i_a80_apb0_data
,
237 &sun9i_a80_apb0_lock
, reg
);
239 CLK_OF_DECLARE(sun9i_a80_apb0
, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup
);
243 * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
244 * APB1 rate is calculated as follows
245 * rate = (parent_rate >> p) / (m + 1);
248 static void sun9i_a80_get_apb1_factors(struct factors_request
*req
)
252 if (req
->parent_rate
< req
->rate
)
253 req
->rate
= req
->parent_rate
;
255 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
257 /* Highest possible divider is 256 (p = 3, m = 31) */
261 req
->p
= order_base_2(div
);
262 req
->m
= (req
->parent_rate
>> req
->p
) - 1;
263 req
->rate
= (req
->parent_rate
>> req
->p
) / (req
->m
+ 1);
266 static const struct clk_factors_config sun9i_a80_apb1_config
= {
273 static const struct factors_data sun9i_a80_apb1_data __initconst
= {
276 .table
= &sun9i_a80_apb1_config
,
277 .getter
= sun9i_a80_get_apb1_factors
,
280 static DEFINE_SPINLOCK(sun9i_a80_apb1_lock
);
282 static void __init
sun9i_a80_apb1_setup(struct device_node
*node
)
286 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
288 pr_err("Could not get registers for a80-apb1-clk: %s\n",
293 sunxi_factors_register(node
, &sun9i_a80_apb1_data
,
294 &sun9i_a80_apb1_lock
, reg
);
296 CLK_OF_DECLARE(sun9i_a80_apb1
, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup
);