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(u32
*freq
, u32 parent_rate
,
36 u8
*n_ret
, u8
*k
, u8
*m_ret
, u8
*p_ret
)
42 /* Normalize value to a 6 MHz multiple (24 MHz / 4) */
43 n
= DIV_ROUND_UP(*freq
, 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 *freq
= ((24000000 * n
) >> p
) / (m
+ 1);
65 /* we were called to round the frequency, we can now return */
74 static struct clk_factors_config sun9i_a80_pll4_config
= {
83 static const struct factors_data sun9i_a80_pll4_data __initconst
= {
85 .table
= &sun9i_a80_pll4_config
,
86 .getter
= sun9i_a80_get_pll4_factors
,
89 static DEFINE_SPINLOCK(sun9i_a80_pll4_lock
);
91 static void __init
sun9i_a80_pll4_setup(struct device_node
*node
)
95 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
97 pr_err("Could not get registers for a80-pll4-clk: %s\n",
102 sunxi_factors_register(node
, &sun9i_a80_pll4_data
,
103 &sun9i_a80_pll4_lock
, reg
);
105 CLK_OF_DECLARE(sun9i_a80_pll4
, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup
);
109 * sun9i_a80_get_gt_factors() - calculates m factor for GT
110 * GT rate is calculated as follows
111 * rate = parent_rate / (m + 1);
114 static void sun9i_a80_get_gt_factors(u32
*freq
, u32 parent_rate
,
115 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
119 if (parent_rate
< *freq
)
122 div
= DIV_ROUND_UP(parent_rate
, *freq
);
124 /* maximum divider is 4 */
128 *freq
= parent_rate
/ div
;
130 /* we were called to round the frequency, we can now return */
137 static struct clk_factors_config sun9i_a80_gt_config
= {
142 static const struct factors_data sun9i_a80_gt_data __initconst
= {
144 .muxmask
= BIT(1) | BIT(0),
145 .table
= &sun9i_a80_gt_config
,
146 .getter
= sun9i_a80_get_gt_factors
,
149 static DEFINE_SPINLOCK(sun9i_a80_gt_lock
);
151 static void __init
sun9i_a80_gt_setup(struct device_node
*node
)
156 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
158 pr_err("Could not get registers for a80-gt-clk: %s\n",
163 gt
= sunxi_factors_register(node
, &sun9i_a80_gt_data
,
164 &sun9i_a80_gt_lock
, reg
);
166 /* The GT bus clock needs to be always enabled */
168 clk_prepare_enable(gt
);
170 CLK_OF_DECLARE(sun9i_a80_gt
, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup
);
174 * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
175 * AHB rate is calculated as follows
176 * rate = parent_rate >> p;
179 static void sun9i_a80_get_ahb_factors(u32
*freq
, u32 parent_rate
,
180 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
184 if (parent_rate
< *freq
)
187 _p
= order_base_2(DIV_ROUND_UP(parent_rate
, *freq
));
193 *freq
= parent_rate
>> _p
;
195 /* we were called to round the frequency, we can now return */
202 static struct clk_factors_config sun9i_a80_ahb_config
= {
207 static const struct factors_data sun9i_a80_ahb_data __initconst
= {
209 .muxmask
= BIT(1) | BIT(0),
210 .table
= &sun9i_a80_ahb_config
,
211 .getter
= sun9i_a80_get_ahb_factors
,
214 static DEFINE_SPINLOCK(sun9i_a80_ahb_lock
);
216 static void __init
sun9i_a80_ahb_setup(struct device_node
*node
)
220 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
222 pr_err("Could not get registers for a80-ahb-clk: %s\n",
227 sunxi_factors_register(node
, &sun9i_a80_ahb_data
,
228 &sun9i_a80_ahb_lock
, reg
);
230 CLK_OF_DECLARE(sun9i_a80_ahb
, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup
);
233 static const struct factors_data sun9i_a80_apb0_data __initconst
= {
236 .table
= &sun9i_a80_ahb_config
,
237 .getter
= sun9i_a80_get_ahb_factors
,
240 static DEFINE_SPINLOCK(sun9i_a80_apb0_lock
);
242 static void __init
sun9i_a80_apb0_setup(struct device_node
*node
)
246 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
248 pr_err("Could not get registers for a80-apb0-clk: %s\n",
253 sunxi_factors_register(node
, &sun9i_a80_apb0_data
,
254 &sun9i_a80_apb0_lock
, reg
);
256 CLK_OF_DECLARE(sun9i_a80_apb0
, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup
);
260 * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
261 * APB1 rate is calculated as follows
262 * rate = (parent_rate >> p) / (m + 1);
265 static void sun9i_a80_get_apb1_factors(u32
*freq
, u32 parent_rate
,
266 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
271 if (parent_rate
< *freq
)
274 div
= DIV_ROUND_UP(parent_rate
, *freq
);
276 /* Highest possible divider is 256 (p = 3, m = 31) */
280 calcp
= order_base_2(div
);
281 calcm
= (parent_rate
>> calcp
) - 1;
282 *freq
= (parent_rate
>> calcp
) / (calcm
+ 1);
284 /* we were called to round the frequency, we can now return */
292 static struct clk_factors_config sun9i_a80_apb1_config
= {
299 static const struct factors_data sun9i_a80_apb1_data __initconst
= {
302 .table
= &sun9i_a80_apb1_config
,
303 .getter
= sun9i_a80_get_apb1_factors
,
306 static DEFINE_SPINLOCK(sun9i_a80_apb1_lock
);
308 static void __init
sun9i_a80_apb1_setup(struct device_node
*node
)
312 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
314 pr_err("Could not get registers for a80-apb1-clk: %s\n",
319 sunxi_factors_register(node
, &sun9i_a80_apb1_data
,
320 &sun9i_a80_apb1_lock
, reg
);
322 CLK_OF_DECLARE(sun9i_a80_apb1
, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup
);