blob: 2524d6f41b3b681a6c3d0bb6d937d28fc5b1752e [file] [log] [blame]
Emilio Lópeze874a662013-02-25 11:44:26 -03001/*
2 * Copyright 2013 Emilio López
3 *
4 * Emilio López <emilio@elopez.com.ar>
5 *
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.
10 *
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.
15 */
16
Stephen Boyd9dfefe82015-06-19 15:00:46 -070017#include <linux/clk.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030018#include <linux/clk-provider.h>
19#include <linux/clkdev.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030020#include <linux/of.h>
21#include <linux/of_address.h>
Hans de Goedecfb00862014-02-07 16:21:49 +010022#include <linux/reset-controller.h>
Stephen Boyd9dfefe82015-06-19 15:00:46 -070023#include <linux/slab.h>
Maxime Ripard601da9d2014-07-04 22:24:52 +020024#include <linux/spinlock.h>
Chen-Yu Tsai7954dfa2014-11-26 15:16:52 +080025#include <linux/log2.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030026
27#include "clk-factors.h"
28
29static DEFINE_SPINLOCK(clk_lock);
30
Emilio López40a5dcb2013-12-23 00:32:32 -030031/* Maximum number of parents our clocks have */
32#define SUNXI_MAX_PARENTS 5
33
Emilio Lópeze874a662013-02-25 11:44:26 -030034/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +020035 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
Emilio Lópeze874a662013-02-25 11:44:26 -030036 * PLL1 rate is calculated as follows
37 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
38 * parent_rate is always 24Mhz
39 */
40
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080041static void sun4i_get_pll1_factors(struct factors_request *req)
Emilio Lópeze874a662013-02-25 11:44:26 -030042{
43 u8 div;
44
45 /* Normalize value to a 6M multiple */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080046 div = req->rate / 6000000;
47 req->rate = 6000000 * div;
Emilio Lópeze874a662013-02-25 11:44:26 -030048
49 /* m is always zero for pll1 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080050 req->m = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030051
52 /* k is 1 only on these cases */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080053 if (req->rate >= 768000000 || req->rate == 42000000 ||
54 req->rate == 54000000)
55 req->k = 1;
Emilio Lópeze874a662013-02-25 11:44:26 -030056 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080057 req->k = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030058
59 /* p will be 3 for divs under 10 */
60 if (div < 10)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080061 req->p = 3;
Emilio Lópeze874a662013-02-25 11:44:26 -030062
63 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
64 else if (div < 20 || (div < 32 && (div & 1)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080065 req->p = 2;
Emilio Lópeze874a662013-02-25 11:44:26 -030066
67 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
68 * of divs between 40-62 */
69 else if (div < 40 || (div < 64 && (div & 2)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080070 req->p = 1;
Emilio Lópeze874a662013-02-25 11:44:26 -030071
72 /* any other entries have p = 0 */
73 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080074 req->p = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -030075
76 /* calculate a suitable n based on k and p */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080077 div <<= req->p;
78 div /= (req->k + 1);
79 req->n = div / 4;
Emilio Lópeze874a662013-02-25 11:44:26 -030080}
81
Maxime Ripard6a721db2013-07-23 23:34:10 +020082/**
83 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
84 * PLL1 rate is calculated as follows
85 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
86 * parent_rate should always be 24MHz
87 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080088static void sun6i_a31_get_pll1_factors(struct factors_request *req)
Maxime Ripard6a721db2013-07-23 23:34:10 +020089{
90 /*
91 * We can operate only on MHz, this will make our life easier
92 * later.
93 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +080094 u32 freq_mhz = req->rate / 1000000;
95 u32 parent_freq_mhz = req->parent_rate / 1000000;
Emilio Lópeze874a662013-02-25 11:44:26 -030096
Maxime Ripard6a721db2013-07-23 23:34:10 +020097 /*
98 * Round down the frequency to the closest multiple of either
99 * 6 or 16
100 */
101 u32 round_freq_6 = round_down(freq_mhz, 6);
102 u32 round_freq_16 = round_down(freq_mhz, 16);
103
104 if (round_freq_6 > round_freq_16)
105 freq_mhz = round_freq_6;
106 else
107 freq_mhz = round_freq_16;
108
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800109 req->rate = freq_mhz * 1000000;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200110
111 /* If the frequency is a multiple of 32 MHz, k is always 3 */
112 if (!(freq_mhz % 32))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800113 req->k = 3;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200114 /* If the frequency is a multiple of 9 MHz, k is always 2 */
115 else if (!(freq_mhz % 9))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800116 req->k = 2;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200117 /* If the frequency is a multiple of 8 MHz, k is always 1 */
118 else if (!(freq_mhz % 8))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800119 req->k = 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200120 /* Otherwise, we don't use the k factor */
121 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800122 req->k = 0;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200123
124 /*
125 * If the frequency is a multiple of 2 but not a multiple of
126 * 3, m is 3. This is the first time we use 6 here, yet we
127 * will use it on several other places.
128 * We use this number because it's the lowest frequency we can
129 * generate (with n = 0, k = 0, m = 3), so every other frequency
130 * somehow relates to this frequency.
131 */
132 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800133 req->m = 2;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200134 /*
135 * If the frequency is a multiple of 6MHz, but the factor is
136 * odd, m will be 3
137 */
138 else if ((freq_mhz / 6) & 1)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800139 req->m = 3;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200140 /* Otherwise, we end up with m = 1 */
141 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800142 req->m = 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200143
144 /* Calculate n thanks to the above factors we already got */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800145 req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
146 - 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200147
148 /*
149 * If n end up being outbound, and that we can still decrease
150 * m, do it.
151 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800152 if ((req->n + 1) > 31 && (req->m + 1) > 1) {
153 req->n = (req->n + 1) / 2 - 1;
154 req->m = (req->m + 1) / 2 - 1;
Maxime Ripard6a721db2013-07-23 23:34:10 +0200155 }
156}
Emilio Lópeze874a662013-02-25 11:44:26 -0300157
158/**
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800159 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
160 * PLL1 rate is calculated as follows
161 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
162 * parent_rate is always 24Mhz
163 */
164
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800165static void sun8i_a23_get_pll1_factors(struct factors_request *req)
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800166{
167 u8 div;
168
169 /* Normalize value to a 6M multiple */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800170 div = req->rate / 6000000;
171 req->rate = 6000000 * div;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800172
173 /* m is always zero for pll1 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800174 req->m = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800175
176 /* k is 1 only on these cases */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800177 if (req->rate >= 768000000 || req->rate == 42000000 ||
178 req->rate == 54000000)
179 req->k = 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800180 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800181 req->k = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800182
183 /* p will be 2 for divs under 20 and odd divs under 32 */
184 if (div < 20 || (div < 32 && (div & 1)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800185 req->p = 2;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800186
187 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
188 * of divs between 40-62 */
189 else if (div < 40 || (div < 64 && (div & 2)))
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800190 req->p = 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800191
192 /* any other entries have p = 0 */
193 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800194 req->p = 0;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800195
196 /* calculate a suitable n based on k and p */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800197 div <<= req->p;
198 div /= (req->k + 1);
199 req->n = div / 4 - 1;
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800200}
201
202/**
Emilio Lópezd584c132013-12-23 00:32:37 -0300203 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
204 * PLL5 rate is calculated as follows
205 * rate = parent_rate * n * (k + 1)
206 * parent_rate is always 24Mhz
207 */
208
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800209static void sun4i_get_pll5_factors(struct factors_request *req)
Emilio Lópezd584c132013-12-23 00:32:37 -0300210{
211 u8 div;
212
213 /* Normalize value to a parent_rate multiple (24M) */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800214 div = req->rate / req->parent_rate;
215 req->rate = req->parent_rate * div;
Emilio Lópezd584c132013-12-23 00:32:37 -0300216
217 if (div < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800218 req->k = 0;
Emilio Lópezd584c132013-12-23 00:32:37 -0300219 else if (div / 2 < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800220 req->k = 1;
Emilio Lópezd584c132013-12-23 00:32:37 -0300221 else if (div / 3 < 31)
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800222 req->k = 2;
Emilio Lópezd584c132013-12-23 00:32:37 -0300223 else
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800224 req->k = 3;
Emilio Lópezd584c132013-12-23 00:32:37 -0300225
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800226 req->n = DIV_ROUND_UP(div, (req->k + 1));
Emilio Lópezd584c132013-12-23 00:32:37 -0300227}
228
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100229/**
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800230 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
231 * PLL6x2 rate is calculated as follows
232 * rate = parent_rate * (n + 1) * (k + 1)
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100233 * parent_rate is always 24Mhz
234 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300235
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800236static void sun6i_a31_get_pll6_factors(struct factors_request *req)
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100237{
238 u8 div;
239
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800240 /* Normalize value to a parent_rate multiple (24M) */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800241 div = req->rate / req->parent_rate;
242 req->rate = req->parent_rate * div;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100243
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800244 req->k = div / 32;
245 if (req->k > 3)
246 req->k = 3;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100247
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800248 req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100249}
Emilio Lópezd584c132013-12-23 00:32:37 -0300250
251/**
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800252 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
253 * AHB rate is calculated as follows
254 * rate = parent_rate >> p
255 */
256
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800257static void sun5i_a13_get_ahb_factors(struct factors_request *req)
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800258{
259 u32 div;
260
261 /* divide only */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800262 if (req->parent_rate < req->rate)
263 req->rate = req->parent_rate;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800264
265 /*
266 * user manual says valid speed is 8k ~ 276M, but tests show it
267 * can work at speeds up to 300M, just after reparenting to pll6
268 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800269 if (req->rate < 8000)
270 req->rate = 8000;
271 if (req->rate > 300000000)
272 req->rate = 300000000;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800273
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800274 div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800275
276 /* p = 0 ~ 3 */
277 if (div > 3)
278 div = 3;
279
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800280 req->rate = req->parent_rate >> div;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800281
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800282 req->p = div;
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800283}
284
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800285#define SUN6I_AHB1_PARENT_PLL6 3
286
287/**
288 * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
289 * AHB rate is calculated as follows
290 * rate = parent_rate >> p
291 *
292 * if parent is pll6, then
293 * parent_rate = pll6 rate / (m + 1)
294 */
295
296static void sun6i_get_ahb1_factors(struct factors_request *req)
297{
298 u8 div, calcp, calcm = 1;
299
300 /*
301 * clock can only divide, so we will never be able to achieve
302 * frequencies higher than the parent frequency
303 */
304 if (req->parent_rate && req->rate > req->parent_rate)
305 req->rate = req->parent_rate;
306
307 div = DIV_ROUND_UP(req->parent_rate, req->rate);
308
309 /* calculate pre-divider if parent is pll6 */
310 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
311 if (div < 4)
312 calcp = 0;
313 else if (div / 2 < 4)
314 calcp = 1;
315 else if (div / 4 < 4)
316 calcp = 2;
317 else
318 calcp = 3;
319
320 calcm = DIV_ROUND_UP(div, 1 << calcp);
321 } else {
322 calcp = __roundup_pow_of_two(div);
323 calcp = calcp > 3 ? 3 : calcp;
324 }
325
326 req->rate = (req->parent_rate / calcm) >> calcp;
327 req->p = calcp;
328 req->m = calcm - 1;
329}
330
331/**
332 * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
333 * parent index
334 */
335static void sun6i_ahb1_recalc(struct factors_request *req)
336{
337 req->rate = req->parent_rate;
338
339 /* apply pre-divider first if parent is pll6 */
340 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
341 req->rate /= req->m + 1;
342
343 /* clk divider */
344 req->rate >>= req->p;
345}
346
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800347/**
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200348 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
Emilio Lópeze874a662013-02-25 11:44:26 -0300349 * APB1 rate is calculated as follows
350 * rate = (parent_rate >> p) / (m + 1);
351 */
352
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800353static void sun4i_get_apb1_factors(struct factors_request *req)
Emilio Lópeze874a662013-02-25 11:44:26 -0300354{
355 u8 calcm, calcp;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800356 int div;
Emilio Lópeze874a662013-02-25 11:44:26 -0300357
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800358 if (req->parent_rate < req->rate)
359 req->rate = req->parent_rate;
Emilio Lópeze874a662013-02-25 11:44:26 -0300360
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800361 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Emilio Lópeze874a662013-02-25 11:44:26 -0300362
363 /* Invalid rate! */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800364 if (div > 32)
Emilio Lópeze874a662013-02-25 11:44:26 -0300365 return;
366
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800367 if (div <= 4)
Emilio Lópeze874a662013-02-25 11:44:26 -0300368 calcp = 0;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800369 else if (div <= 8)
Emilio Lópeze874a662013-02-25 11:44:26 -0300370 calcp = 1;
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800371 else if (div <= 16)
Emilio Lópeze874a662013-02-25 11:44:26 -0300372 calcp = 2;
373 else
374 calcp = 3;
375
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800376 calcm = (req->parent_rate >> calcp) - 1;
Emilio Lópeze874a662013-02-25 11:44:26 -0300377
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800378 req->rate = (req->parent_rate >> calcp) / (calcm + 1);
379 req->m = calcm;
380 req->p = calcp;
Emilio Lópeze874a662013-02-25 11:44:26 -0300381}
382
383
384
Emilio López75517692013-12-23 00:32:39 -0300385
386/**
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800387 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
388 * CLK_OUT rate is calculated as follows
389 * rate = (parent_rate >> p) / (m + 1);
390 */
391
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800392static void sun7i_a20_get_out_factors(struct factors_request *req)
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800393{
394 u8 div, calcm, calcp;
395
396 /* These clocks can only divide, so we will never be able to achieve
397 * frequencies higher than the parent frequency */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800398 if (req->rate > req->parent_rate)
399 req->rate = req->parent_rate;
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800400
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800401 div = DIV_ROUND_UP(req->parent_rate, req->rate);
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800402
403 if (div < 32)
404 calcp = 0;
405 else if (div / 2 < 32)
406 calcp = 1;
407 else if (div / 4 < 32)
408 calcp = 2;
409 else
410 calcp = 3;
411
412 calcm = DIV_ROUND_UP(div, 1 << calcp);
413
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800414 req->rate = (req->parent_rate >> calcp) / calcm;
415 req->m = calcm - 1;
416 req->p = calcp;
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800417}
418
Chen-Yu Tsaie4c6d6c2014-02-10 18:35:47 +0800419/**
Emilio Lópeze874a662013-02-25 11:44:26 -0300420 * sunxi_factors_clk_setup() - Setup function for factor clocks
421 */
422
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800423static const struct clk_factors_config sun4i_pll1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300424 .nshift = 8,
425 .nwidth = 5,
426 .kshift = 4,
427 .kwidth = 2,
428 .mshift = 0,
429 .mwidth = 2,
430 .pshift = 16,
431 .pwidth = 2,
432};
433
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800434static const struct clk_factors_config sun6i_a31_pll1_config = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200435 .nshift = 8,
436 .nwidth = 5,
437 .kshift = 4,
438 .kwidth = 2,
439 .mshift = 0,
440 .mwidth = 2,
Hans de Goede76820fc2015-01-24 12:56:32 +0100441 .n_start = 1,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200442};
443
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800444static const struct clk_factors_config sun8i_a23_pll1_config = {
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800445 .nshift = 8,
446 .nwidth = 5,
447 .kshift = 4,
448 .kwidth = 2,
449 .mshift = 0,
450 .mwidth = 2,
451 .pshift = 16,
452 .pwidth = 2,
453 .n_start = 1,
454};
455
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800456static const struct clk_factors_config sun4i_pll5_config = {
Emilio Lópezd584c132013-12-23 00:32:37 -0300457 .nshift = 8,
458 .nwidth = 5,
459 .kshift = 4,
460 .kwidth = 2,
461};
462
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800463static const struct clk_factors_config sun6i_a31_pll6_config = {
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100464 .nshift = 8,
465 .nwidth = 5,
466 .kshift = 4,
467 .kwidth = 2,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800468 .n_start = 1,
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100469};
470
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800471static const struct clk_factors_config sun5i_a13_ahb_config = {
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800472 .pshift = 4,
473 .pwidth = 2,
474};
475
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800476static const struct clk_factors_config sun6i_ahb1_config = {
477 .mshift = 6,
478 .mwidth = 2,
479 .pshift = 4,
480 .pwidth = 2,
481};
482
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800483static const struct clk_factors_config sun4i_apb1_config = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300484 .mshift = 0,
485 .mwidth = 5,
486 .pshift = 16,
487 .pwidth = 2,
488};
489
Emilio López75517692013-12-23 00:32:39 -0300490/* user manual says "n" but it's really "p" */
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800491static const struct clk_factors_config sun7i_a20_out_config = {
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800492 .mshift = 8,
493 .mwidth = 5,
494 .pshift = 20,
495 .pwidth = 2,
496};
497
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530498static const struct factors_data sun4i_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300499 .enable = 31,
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200500 .table = &sun4i_pll1_config,
501 .getter = sun4i_get_pll1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300502};
503
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530504static const struct factors_data sun6i_a31_pll1_data __initconst = {
Emilio Lópezd838ff32013-12-23 00:32:34 -0300505 .enable = 31,
Maxime Ripard6a721db2013-07-23 23:34:10 +0200506 .table = &sun6i_a31_pll1_config,
507 .getter = sun6i_a31_get_pll1_factors,
508};
509
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800510static const struct factors_data sun8i_a23_pll1_data __initconst = {
511 .enable = 31,
512 .table = &sun8i_a23_pll1_config,
513 .getter = sun8i_a23_get_pll1_factors,
514};
515
Emilio López5a8ddf22014-03-19 15:19:30 -0300516static const struct factors_data sun7i_a20_pll4_data __initconst = {
517 .enable = 31,
518 .table = &sun4i_pll5_config,
519 .getter = sun4i_get_pll5_factors,
520};
521
Emilio Lópezd584c132013-12-23 00:32:37 -0300522static const struct factors_data sun4i_pll5_data __initconst = {
523 .enable = 31,
524 .table = &sun4i_pll5_config,
525 .getter = sun4i_get_pll5_factors,
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800526 .name = "pll5",
527};
528
529static const struct factors_data sun4i_pll6_data __initconst = {
530 .enable = 31,
531 .table = &sun4i_pll5_config,
532 .getter = sun4i_get_pll5_factors,
533 .name = "pll6",
Emilio Lópezd584c132013-12-23 00:32:37 -0300534};
535
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100536static const struct factors_data sun6i_a31_pll6_data __initconst = {
537 .enable = 31,
538 .table = &sun6i_a31_pll6_config,
539 .getter = sun6i_a31_get_pll6_factors,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800540 .name = "pll6x2",
Maxime Ripard92ef67c2014-02-05 14:05:03 +0100541};
542
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800543static const struct factors_data sun5i_a13_ahb_data __initconst = {
544 .mux = 6,
545 .muxmask = BIT(1) | BIT(0),
546 .table = &sun5i_a13_ahb_config,
547 .getter = sun5i_a13_get_ahb_factors,
548};
549
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800550static const struct factors_data sun6i_ahb1_data __initconst = {
551 .mux = 12,
552 .muxmask = BIT(1) | BIT(0),
553 .table = &sun6i_ahb1_config,
554 .getter = sun6i_get_ahb1_factors,
555 .recalc = sun6i_ahb1_recalc,
556};
557
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530558static const struct factors_data sun4i_apb1_data __initconst = {
Emilio López93746e72014-11-06 11:40:29 +0800559 .mux = 24,
560 .muxmask = BIT(1) | BIT(0),
Maxime Ripard81ba6c52013-07-22 18:21:32 +0200561 .table = &sun4i_apb1_config,
562 .getter = sun4i_get_apb1_factors,
Emilio Lópeze874a662013-02-25 11:44:26 -0300563};
564
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800565static const struct factors_data sun7i_a20_out_data __initconst = {
566 .enable = 31,
567 .mux = 24,
Chen-Yu Tsaie94f8cb32014-10-20 22:10:26 +0800568 .muxmask = BIT(1) | BIT(0),
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800569 .table = &sun7i_a20_out_config,
570 .getter = sun7i_a20_get_out_factors,
571};
572
Emilio López5f4e0be2013-12-23 00:32:36 -0300573static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
Maxime Ripard601da9d2014-07-04 22:24:52 +0200574 const struct factors_data *data)
Emilio Lópeze874a662013-02-25 11:44:26 -0300575{
Hans de Goede7c74c222014-11-23 14:38:07 +0100576 void __iomem *reg;
577
578 reg = of_iomap(node, 0);
579 if (!reg) {
580 pr_err("Could not get registers for factors-clk: %s\n",
581 node->name);
582 return NULL;
583 }
584
585 return sunxi_factors_register(node, data, &clk_lock, reg);
Emilio Lópeze874a662013-02-25 11:44:26 -0300586}
587
Chen-Yu Tsaia78bb352016-01-25 21:15:45 +0800588static void __init sun6i_ahb1_clk_setup(struct device_node *node)
589{
590 sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
591}
592CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
593 sun6i_ahb1_clk_setup);
Emilio Lópeze874a662013-02-25 11:44:26 -0300594
595
596/**
597 * sunxi_mux_clk_setup() - Setup function for muxes
598 */
599
600#define SUNXI_MUX_GATE_WIDTH 2
601
602struct mux_data {
603 u8 shift;
604};
605
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530606static const struct mux_data sun4i_cpu_mux_data __initconst = {
Emilio Lópeze874a662013-02-25 11:44:26 -0300607 .shift = 16,
608};
609
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530610static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
Maxime Ripard6a721db2013-07-23 23:34:10 +0200611 .shift = 12,
612};
613
Jens Kuskeab6e23a2015-12-04 22:24:40 +0100614static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
615 .shift = 0,
616};
617
Emilio Lópeze874a662013-02-25 11:44:26 -0300618static void __init sunxi_mux_clk_setup(struct device_node *node,
619 struct mux_data *data)
620{
621 struct clk *clk;
622 const char *clk_name = node->name;
Emilio Lópezedaf3fb2013-12-23 00:32:33 -0300623 const char *parents[SUNXI_MAX_PARENTS];
Emilio López89a94562014-07-28 00:49:42 -0300624 void __iomem *reg;
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500625 int i;
Emilio Lópeze874a662013-02-25 11:44:26 -0300626
627 reg = of_iomap(node, 0);
628
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500629 i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
Andre Przywarad221b7a2016-02-01 17:39:27 +0000630 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
631 pr_warn("%s: could not read clock-output-names for \"%s\"\n",
632 __func__, clk_name);
633 goto out_unmap;
634 }
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800635
James Hogan819c1de2013-07-29 12:25:01 +0100636 clk = clk_register_mux(NULL, clk_name, parents, i,
Chen-Yu Tsai3ec72fa2015-01-06 10:35:12 +0800637 CLK_SET_RATE_PARENT, reg,
Emilio Lópeze874a662013-02-25 11:44:26 -0300638 data->shift, SUNXI_MUX_GATE_WIDTH,
639 0, &clk_lock);
640
Andre Przywarad221b7a2016-02-01 17:39:27 +0000641 if (IS_ERR(clk)) {
642 pr_warn("%s: failed to register mux clock %s: %ld\n", __func__,
643 clk_name, PTR_ERR(clk));
644 goto out_unmap;
Emilio Lópeze874a662013-02-25 11:44:26 -0300645 }
Andre Przywarad221b7a2016-02-01 17:39:27 +0000646
647 of_clk_add_provider(node, of_clk_src_simple_get, clk);
648 clk_register_clkdev(clk, clk_name, NULL);
649 return;
650
651out_unmap:
652 iounmap(reg);
Emilio Lópeze874a662013-02-25 11:44:26 -0300653}
654
655
656
657/**
658 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
659 */
660
Emilio Lópeze874a662013-02-25 11:44:26 -0300661struct div_data {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200662 u8 shift;
663 u8 pow;
664 u8 width;
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800665 const struct clk_div_table *table;
Emilio Lópeze874a662013-02-25 11:44:26 -0300666};
667
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530668static const struct div_data sun4i_axi_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200669 .shift = 0,
670 .pow = 0,
671 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300672};
673
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800674static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
675 { .val = 0, .div = 1 },
676 { .val = 1, .div = 2 },
677 { .val = 2, .div = 3 },
678 { .val = 3, .div = 4 },
679 { .val = 4, .div = 4 },
680 { .val = 5, .div = 4 },
681 { .val = 6, .div = 4 },
682 { .val = 7, .div = 4 },
683 { } /* sentinel */
684};
685
686static const struct div_data sun8i_a23_axi_data __initconst = {
687 .width = 3,
688 .table = sun8i_a23_axi_table,
689};
690
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530691static const struct div_data sun4i_ahb_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200692 .shift = 4,
693 .pow = 1,
694 .width = 2,
Emilio Lópeze874a662013-02-25 11:44:26 -0300695};
696
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800697static const struct clk_div_table sun4i_apb0_table[] __initconst = {
698 { .val = 0, .div = 2 },
699 { .val = 1, .div = 2 },
700 { .val = 2, .div = 4 },
701 { .val = 3, .div = 8 },
702 { } /* sentinel */
703};
704
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530705static const struct div_data sun4i_apb0_data __initconst = {
Maxime Ripard70855bb2013-07-23 09:25:56 +0200706 .shift = 8,
707 .pow = 1,
708 .width = 2,
Chen-Yu Tsaicfe4c932014-09-06 14:45:10 +0800709 .table = sun4i_apb0_table,
Emilio Lópeze874a662013-02-25 11:44:26 -0300710};
711
712static void __init sunxi_divider_clk_setup(struct device_node *node,
713 struct div_data *data)
714{
715 struct clk *clk;
716 const char *clk_name = node->name;
717 const char *clk_parent;
Emilio López89a94562014-07-28 00:49:42 -0300718 void __iomem *reg;
Emilio Lópeze874a662013-02-25 11:44:26 -0300719
720 reg = of_iomap(node, 0);
721
722 clk_parent = of_clk_get_parent_name(node, 0);
723
Chen-Yu Tsaif64111e2014-02-03 09:51:37 +0800724 of_property_read_string(node, "clock-output-names", &clk_name);
725
Chen-Yu Tsaiea5671b2014-06-26 23:55:42 +0800726 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
727 reg, data->shift, data->width,
728 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
729 data->table, &clk_lock);
Emilio Lópeze874a662013-02-25 11:44:26 -0300730 if (clk) {
731 of_clk_add_provider(node, of_clk_src_simple_get, clk);
732 clk_register_clkdev(clk, clk_name, NULL);
733 }
734}
735
736
Emilio López13569a72013-03-27 18:20:37 -0300737
738/**
739 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
740 */
741
742#define SUNXI_GATES_MAX_SIZE 64
743
744struct gates_data {
745 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
746};
747
Emilio Lópezd584c132013-12-23 00:32:37 -0300748/**
749 * sunxi_divs_clk_setup() helper data
750 */
751
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800752#define SUNXI_DIVS_MAX_QTY 4
Emilio Lópezd584c132013-12-23 00:32:37 -0300753#define SUNXI_DIVISOR_WIDTH 2
754
755struct divs_data {
756 const struct factors_data *factors; /* data for the factor clock */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800757 int ndivs; /* number of outputs */
758 /*
759 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
760 * self or base factor clock refers to the output from the pll
761 * itself. The remaining refer to fixed or configurable divider
762 * outputs.
763 */
Emilio Lópezd584c132013-12-23 00:32:37 -0300764 struct {
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800765 u8 self; /* is it the base factor clock? (only one) */
Emilio Lópezd584c132013-12-23 00:32:37 -0300766 u8 fixed; /* is it a fixed divisor? if not... */
767 struct clk_div_table *table; /* is it a table based divisor? */
768 u8 shift; /* otherwise it's a normal divisor with this shift */
769 u8 pow; /* is it power-of-two based? */
770 u8 gate; /* is it independently gateable? */
771 } div[SUNXI_DIVS_MAX_QTY];
772};
773
774static struct clk_div_table pll6_sata_tbl[] = {
775 { .val = 0, .div = 6, },
776 { .val = 1, .div = 12, },
777 { .val = 2, .div = 18, },
778 { .val = 3, .div = 24, },
779 { } /* sentinel */
780};
781
782static const struct divs_data pll5_divs_data __initconst = {
783 .factors = &sun4i_pll5_data,
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800784 .ndivs = 2,
Emilio Lópezd584c132013-12-23 00:32:37 -0300785 .div = {
786 { .shift = 0, .pow = 0, }, /* M, DDR */
787 { .shift = 16, .pow = 1, }, /* P, other */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800788 /* No output for the base factor clock */
Emilio Lópezd584c132013-12-23 00:32:37 -0300789 }
790};
791
792static const struct divs_data pll6_divs_data __initconst = {
Chen-Yu Tsai667f5422014-02-03 09:51:39 +0800793 .factors = &sun4i_pll6_data,
Chen-Yu Tsaif1017962015-03-25 01:22:08 +0800794 .ndivs = 4,
Emilio Lópezd584c132013-12-23 00:32:37 -0300795 .div = {
796 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
797 { .fixed = 2 }, /* P, other */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800798 { .self = 1 }, /* base factor clock, 2x */
Chen-Yu Tsaif1017962015-03-25 01:22:08 +0800799 { .fixed = 4 }, /* pll6 / 4, used as ahb input */
Emilio Lópezd584c132013-12-23 00:32:37 -0300800 }
801};
802
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800803static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
804 .factors = &sun6i_a31_pll6_data,
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800805 .ndivs = 2,
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800806 .div = {
807 { .fixed = 2 }, /* normal output */
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800808 { .self = 1 }, /* base factor clock, 2x */
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800809 }
810};
811
Emilio Lópezd584c132013-12-23 00:32:37 -0300812/**
813 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
814 *
815 * These clocks look something like this
816 * ________________________
817 * | ___divisor 1---|----> to consumer
818 * parent >--| pll___/___divisor 2---|----> to consumer
819 * | \_______________|____> to consumer
820 * |________________________|
821 */
822
823static void __init sunxi_divs_clk_setup(struct device_node *node,
824 struct divs_data *data)
825{
826 struct clk_onecell_data *clk_data;
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800827 const char *parent;
Emilio Lópezd584c132013-12-23 00:32:37 -0300828 const char *clk_name;
829 struct clk **clks, *pclk;
830 struct clk_hw *gate_hw, *rate_hw;
831 const struct clk_ops *rate_ops;
832 struct clk_gate *gate = NULL;
833 struct clk_fixed_factor *fix_factor;
834 struct clk_divider *divider;
Emilio López89a94562014-07-28 00:49:42 -0300835 void __iomem *reg;
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800836 int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
Emilio Lópezd584c132013-12-23 00:32:37 -0300837 int flags, clkflags;
838
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800839 /* if number of children known, use it */
840 if (data->ndivs)
841 ndivs = data->ndivs;
842
Emilio Lópezd584c132013-12-23 00:32:37 -0300843 /* Set up factor clock that we will be dividing */
844 pclk = sunxi_factors_clk_setup(node, data->factors);
Chen-Yu Tsai97e36b32014-02-03 09:51:40 +0800845 parent = __clk_get_name(pclk);
Emilio Lópezd584c132013-12-23 00:32:37 -0300846
847 reg = of_iomap(node, 0);
848
849 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
850 if (!clk_data)
851 return;
852
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800853 clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
Emilio Lópezd584c132013-12-23 00:32:37 -0300854 if (!clks)
855 goto free_clkdata;
856
857 clk_data->clks = clks;
858
859 /* It's not a good idea to have automatic reparenting changing
860 * our RAM clock! */
861 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
862
Chen-Yu Tsai13d52f62014-11-13 02:08:30 +0800863 for (i = 0; i < ndivs; i++) {
Emilio Lópezd584c132013-12-23 00:32:37 -0300864 if (of_property_read_string_index(node, "clock-output-names",
865 i, &clk_name) != 0)
866 break;
867
Chen-Yu Tsai934fe5f2015-03-25 01:22:07 +0800868 /* If this is the base factor clock, only update clks */
869 if (data->div[i].self) {
870 clk_data->clks[i] = pclk;
871 continue;
872 }
873
Emilio Lópezd584c132013-12-23 00:32:37 -0300874 gate_hw = NULL;
875 rate_hw = NULL;
876 rate_ops = NULL;
877
878 /* If this leaf clock can be gated, create a gate */
879 if (data->div[i].gate) {
880 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
881 if (!gate)
882 goto free_clks;
883
884 gate->reg = reg;
885 gate->bit_idx = data->div[i].gate;
886 gate->lock = &clk_lock;
887
888 gate_hw = &gate->hw;
889 }
890
891 /* Leaves can be fixed or configurable divisors */
892 if (data->div[i].fixed) {
893 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
894 if (!fix_factor)
895 goto free_gate;
896
897 fix_factor->mult = 1;
898 fix_factor->div = data->div[i].fixed;
899
900 rate_hw = &fix_factor->hw;
901 rate_ops = &clk_fixed_factor_ops;
902 } else {
903 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
904 if (!divider)
905 goto free_gate;
906
907 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
908
909 divider->reg = reg;
910 divider->shift = data->div[i].shift;
911 divider->width = SUNXI_DIVISOR_WIDTH;
912 divider->flags = flags;
913 divider->lock = &clk_lock;
914 divider->table = data->div[i].table;
915
916 rate_hw = &divider->hw;
917 rate_ops = &clk_divider_ops;
918 }
919
920 /* Wrap the (potential) gate and the divisor on a composite
921 * clock to unify them */
922 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
923 NULL, NULL,
924 rate_hw, rate_ops,
925 gate_hw, &clk_gate_ops,
926 clkflags);
927
928 WARN_ON(IS_ERR(clk_data->clks[i]));
929 clk_register_clkdev(clks[i], clk_name, NULL);
930 }
931
Emilio Lópezd584c132013-12-23 00:32:37 -0300932 /* Adjust to the real max */
933 clk_data->clk_num = i;
934
935 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
936
937 return;
938
939free_gate:
940 kfree(gate);
941free_clks:
942 kfree(clks);
943free_clkdata:
944 kfree(clk_data);
945}
946
947
948
Emilio Lópeze874a662013-02-25 11:44:26 -0300949/* Matches for factors clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530950static const struct of_device_id clk_factors_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +0100951 {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200952 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800953 {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
Emilio López5a8ddf22014-03-19 15:19:30 -0300954 {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
Chen-Yu Tsai9f243092015-03-20 01:19:03 +0800955 {.compatible = "allwinner,sun5i-a13-ahb-clk", .data = &sun5i_a13_ahb_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +0100956 {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
Chen-Yu Tsai6f863412013-12-24 21:26:17 +0800957 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300958 {}
959};
960
961/* Matches for divider clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530962static const struct of_device_id clk_div_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +0100963 {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +0800964 {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
Maxime Ripardfd1b22f2014-02-06 09:55:57 +0100965 {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
966 {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300967 {}
968};
969
Emilio Lópezd584c132013-12-23 00:32:37 -0300970/* Matches for divided outputs */
971static const struct of_device_id clk_divs_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +0100972 {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
973 {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
Chen-Yu Tsai95e94c12014-11-13 02:08:31 +0800974 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_divs_data,},
Emilio Lópezd584c132013-12-23 00:32:37 -0300975 {}
976};
977
Emilio Lópeze874a662013-02-25 11:44:26 -0300978/* Matches for mux clocks */
Sachin Kamat52be7cc2013-08-12 14:44:06 +0530979static const struct of_device_id clk_mux_match[] __initconst = {
Maxime Ripardfd1b22f2014-02-06 09:55:57 +0100980 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
Maxime Ripard6a721db2013-07-23 23:34:10 +0200981 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
Jens Kuskeab6e23a2015-12-04 22:24:40 +0100982 {.compatible = "allwinner,sun8i-h3-ahb2-clk", .data = &sun8i_h3_ahb2_mux_data,},
Emilio Lópeze874a662013-02-25 11:44:26 -0300983 {}
984};
985
Emilio López13569a72013-03-27 18:20:37 -0300986
Emilio Lópeze874a662013-02-25 11:44:26 -0300987static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
988 void *function)
989{
990 struct device_node *np;
991 const struct div_data *data;
992 const struct of_device_id *match;
993 void (*setup_function)(struct device_node *, const void *) = function;
994
Rob Herringcb7d5f42014-05-12 11:24:31 -0500995 for_each_matching_node_and_match(np, clk_match, &match) {
Emilio Lópeze874a662013-02-25 11:44:26 -0300996 data = match->data;
997 setup_function(np, data);
998 }
999}
1000
Maxime Ripard134a6692014-05-09 22:33:39 -05001001static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
Emilio López8e6a4c42013-09-20 22:03:12 -03001002{
Maxime Ripard134a6692014-05-09 22:33:39 -05001003 unsigned int i;
Emilio López8e6a4c42013-09-20 22:03:12 -03001004
Chen-Yu Tsaib712a622015-03-20 01:19:05 +08001005 /* Register divided output clocks */
1006 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1007
Emilio Lópeze874a662013-02-25 11:44:26 -03001008 /* Register factor clocks */
1009 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1010
1011 /* Register divider clocks */
1012 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1013
1014 /* Register mux clocks */
1015 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
Emilio López13569a72013-03-27 18:20:37 -03001016
Maxime Ripard134a6692014-05-09 22:33:39 -05001017 /* Protect the clocks that needs to stay on */
1018 for (i = 0; i < nclocks; i++) {
1019 struct clk *clk = clk_get(NULL, clocks[i]);
1020
1021 if (!IS_ERR(clk))
1022 clk_prepare_enable(clk);
1023 }
Emilio Lópeze874a662013-02-25 11:44:26 -03001024}
Maxime Ripard134a6692014-05-09 22:33:39 -05001025
1026static const char *sun4i_a10_critical_clocks[] __initdata = {
1027 "pll5_ddr",
1028};
1029
1030static void __init sun4i_a10_init_clocks(struct device_node *node)
1031{
1032 sunxi_init_clocks(sun4i_a10_critical_clocks,
1033 ARRAY_SIZE(sun4i_a10_critical_clocks));
1034}
1035CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
1036
1037static const char *sun5i_critical_clocks[] __initdata = {
Chen-Yu Tsai946fd402015-03-20 01:19:04 +08001038 "cpu",
Maxime Ripard134a6692014-05-09 22:33:39 -05001039 "pll5_ddr",
1040};
1041
1042static void __init sun5i_init_clocks(struct device_node *node)
1043{
1044 sunxi_init_clocks(sun5i_critical_clocks,
1045 ARRAY_SIZE(sun5i_critical_clocks));
1046}
1047CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
1048CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
Maxime Ripardbef62292015-06-09 19:38:04 +02001049CLK_OF_DECLARE(sun5i_r8_clk_init, "allwinner,sun5i-r8", sun5i_init_clocks);
Maxime Ripard134a6692014-05-09 22:33:39 -05001050CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
1051
1052static const char *sun6i_critical_clocks[] __initdata = {
Maxime Ripard2df73f42014-05-09 22:33:40 -05001053 "cpu",
Maxime Ripard134a6692014-05-09 22:33:39 -05001054};
1055
1056static void __init sun6i_init_clocks(struct device_node *node)
1057{
1058 sunxi_init_clocks(sun6i_critical_clocks,
1059 ARRAY_SIZE(sun6i_critical_clocks));
1060}
1061CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
Hans de Goedeb0f2faa2014-12-17 18:18:14 +01001062CLK_OF_DECLARE(sun6i_a31s_clk_init, "allwinner,sun6i-a31s", sun6i_init_clocks);
Chen-Yu Tsai515c1a42014-06-26 23:55:43 +08001063CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);
Vishnu Patekar159870d2015-05-30 16:55:01 +02001064CLK_OF_DECLARE(sun8i_a33_clk_init, "allwinner,sun8i-a33", sun6i_init_clocks);
Jens Kuskeab6e23a2015-12-04 22:24:40 +01001065CLK_OF_DECLARE(sun8i_h3_clk_init, "allwinner,sun8i-h3", sun6i_init_clocks);
Chen-Yu Tsai0b0f0802014-10-20 22:10:28 +08001066
1067static void __init sun9i_init_clocks(struct device_node *node)
1068{
1069 sunxi_init_clocks(NULL, 0);
1070}
1071CLK_OF_DECLARE(sun9i_a80_clk_init, "allwinner,sun9i-a80", sun9i_init_clocks);