]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/clk/sunxi/clk-sunxi.c
Merge remote-tracking branch 'regmap/for-next'
[karo-tx-linux.git] / drivers / clk / sunxi / clk-sunxi.c
1 /*
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
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/clkdev.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/reset-controller.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/log2.h>
26
27 #include "clk-factors.h"
28
29 static DEFINE_SPINLOCK(clk_lock);
30
31 /* Maximum number of parents our clocks have */
32 #define SUNXI_MAX_PARENTS       5
33
34 /**
35  * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
36  * PLL1 rate is calculated as follows
37  * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
38  * parent_rate is always 24Mhz
39  */
40
41 static void sun4i_get_pll1_factors(struct factors_request *req)
42 {
43         u8 div;
44
45         /* Normalize value to a 6M multiple */
46         div = req->rate / 6000000;
47         req->rate = 6000000 * div;
48
49         /* m is always zero for pll1 */
50         req->m = 0;
51
52         /* k is 1 only on these cases */
53         if (req->rate >= 768000000 || req->rate == 42000000 ||
54                         req->rate == 54000000)
55                 req->k = 1;
56         else
57                 req->k = 0;
58
59         /* p will be 3 for divs under 10 */
60         if (div < 10)
61                 req->p = 3;
62
63         /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
64         else if (div < 20 || (div < 32 && (div & 1)))
65                 req->p = 2;
66
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)))
70                 req->p = 1;
71
72         /* any other entries have p = 0 */
73         else
74                 req->p = 0;
75
76         /* calculate a suitable n based on k and p */
77         div <<= req->p;
78         div /= (req->k + 1);
79         req->n = div / 4;
80 }
81
82 /**
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  */
88 static void sun6i_a31_get_pll1_factors(struct factors_request *req)
89 {
90         /*
91          * We can operate only on MHz, this will make our life easier
92          * later.
93          */
94         u32 freq_mhz = req->rate / 1000000;
95         u32 parent_freq_mhz = req->parent_rate / 1000000;
96
97         /*
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
109         req->rate = freq_mhz * 1000000;
110
111         /* If the frequency is a multiple of 32 MHz, k is always 3 */
112         if (!(freq_mhz % 32))
113                 req->k = 3;
114         /* If the frequency is a multiple of 9 MHz, k is always 2 */
115         else if (!(freq_mhz % 9))
116                 req->k = 2;
117         /* If the frequency is a multiple of 8 MHz, k is always 1 */
118         else if (!(freq_mhz % 8))
119                 req->k = 1;
120         /* Otherwise, we don't use the k factor */
121         else
122                 req->k = 0;
123
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)
133                 req->m = 2;
134         /*
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)
139                 req->m = 3;
140         /* Otherwise, we end up with m = 1 */
141         else
142                 req->m = 1;
143
144         /* Calculate n thanks to the above factors we already got */
145         req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
146                  - 1;
147
148         /*
149          * If n end up being outbound, and that we can still decrease
150          * m, do it.
151          */
152         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;
155         }
156 }
157
158 /**
159  * 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
165 static void sun8i_a23_get_pll1_factors(struct factors_request *req)
166 {
167         u8 div;
168
169         /* Normalize value to a 6M multiple */
170         div = req->rate / 6000000;
171         req->rate = 6000000 * div;
172
173         /* m is always zero for pll1 */
174         req->m = 0;
175
176         /* k is 1 only on these cases */
177         if (req->rate >= 768000000 || req->rate == 42000000 ||
178                         req->rate == 54000000)
179                 req->k = 1;
180         else
181                 req->k = 0;
182
183         /* p will be 2 for divs under 20 and odd divs under 32 */
184         if (div < 20 || (div < 32 && (div & 1)))
185                 req->p = 2;
186
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)))
190                 req->p = 1;
191
192         /* any other entries have p = 0 */
193         else
194                 req->p = 0;
195
196         /* calculate a suitable n based on k and p */
197         div <<= req->p;
198         div /= (req->k + 1);
199         req->n = div / 4 - 1;
200 }
201
202 /**
203  * 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
209 static void sun4i_get_pll5_factors(struct factors_request *req)
210 {
211         u8 div;
212
213         /* Normalize value to a parent_rate multiple (24M) */
214         div = req->rate / req->parent_rate;
215         req->rate = req->parent_rate * div;
216
217         if (div < 31)
218                 req->k = 0;
219         else if (div / 2 < 31)
220                 req->k = 1;
221         else if (div / 3 < 31)
222                 req->k = 2;
223         else
224                 req->k = 3;
225
226         req->n = DIV_ROUND_UP(div, (req->k + 1));
227 }
228
229 /**
230  * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
231  * PLL6 rate is calculated as follows
232  * rate = parent_rate * (n + 1) * (k + 1) / 2
233  * parent_rate is always 24Mhz
234  */
235
236 static void sun6i_a31_get_pll6_factors(struct factors_request *req)
237 {
238         u8 div;
239
240         /* Normalize value to a parent_rate multiple (24M) */
241         div = req->rate / (req->parent_rate / 2);
242         req->rate = (req->parent_rate / 2) * div;
243
244         req->k = div / 32;
245         if (req->k > 3)
246                 req->k = 3;
247
248         req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
249 }
250
251 static void sun6i_a31_pll6_recalc(struct factors_request *req)
252 {
253         req->rate = req->parent_rate;
254
255         req->rate *= req->n + 1;
256         req->rate *= req->k + 1;
257         req->rate /= 2;
258 }
259
260 /**
261  * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
262  * AHB rate is calculated as follows
263  * rate = parent_rate >> p
264  */
265
266 static void sun5i_a13_get_ahb_factors(struct factors_request *req)
267 {
268         u32 div;
269
270         /* divide only */
271         if (req->parent_rate < req->rate)
272                 req->rate = req->parent_rate;
273
274         /*
275          * user manual says valid speed is 8k ~ 276M, but tests show it
276          * can work at speeds up to 300M, just after reparenting to pll6
277          */
278         if (req->rate < 8000)
279                 req->rate = 8000;
280         if (req->rate > 300000000)
281                 req->rate = 300000000;
282
283         div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
284
285         /* p = 0 ~ 3 */
286         if (div > 3)
287                 div = 3;
288
289         req->rate = req->parent_rate >> div;
290
291         req->p = div;
292 }
293
294 #define SUN6I_AHB1_PARENT_PLL6  3
295
296 /**
297  * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
298  * AHB rate is calculated as follows
299  * rate = parent_rate >> p
300  *
301  * if parent is pll6, then
302  * parent_rate = pll6 rate / (m + 1)
303  */
304
305 static void sun6i_get_ahb1_factors(struct factors_request *req)
306 {
307         u8 div, calcp, calcm = 1;
308
309         /*
310          * clock can only divide, so we will never be able to achieve
311          * frequencies higher than the parent frequency
312          */
313         if (req->parent_rate && req->rate > req->parent_rate)
314                 req->rate = req->parent_rate;
315
316         div = DIV_ROUND_UP(req->parent_rate, req->rate);
317
318         /* calculate pre-divider if parent is pll6 */
319         if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
320                 if (div < 4)
321                         calcp = 0;
322                 else if (div / 2 < 4)
323                         calcp = 1;
324                 else if (div / 4 < 4)
325                         calcp = 2;
326                 else
327                         calcp = 3;
328
329                 calcm = DIV_ROUND_UP(div, 1 << calcp);
330         } else {
331                 calcp = __roundup_pow_of_two(div);
332                 calcp = calcp > 3 ? 3 : calcp;
333         }
334
335         req->rate = (req->parent_rate / calcm) >> calcp;
336         req->p = calcp;
337         req->m = calcm - 1;
338 }
339
340 /**
341  * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
342  *                       parent index
343  */
344 static void sun6i_ahb1_recalc(struct factors_request *req)
345 {
346         req->rate = req->parent_rate;
347
348         /* apply pre-divider first if parent is pll6 */
349         if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
350                 req->rate /= req->m + 1;
351
352         /* clk divider */
353         req->rate >>= req->p;
354 }
355
356 /**
357  * sun4i_get_apb1_factors() - calculates m, p factors for APB1
358  * APB1 rate is calculated as follows
359  * rate = (parent_rate >> p) / (m + 1);
360  */
361
362 static void sun4i_get_apb1_factors(struct factors_request *req)
363 {
364         u8 calcm, calcp;
365         int div;
366
367         if (req->parent_rate < req->rate)
368                 req->rate = req->parent_rate;
369
370         div = DIV_ROUND_UP(req->parent_rate, req->rate);
371
372         /* Invalid rate! */
373         if (div > 32)
374                 return;
375
376         if (div <= 4)
377                 calcp = 0;
378         else if (div <= 8)
379                 calcp = 1;
380         else if (div <= 16)
381                 calcp = 2;
382         else
383                 calcp = 3;
384
385         calcm = (req->parent_rate >> calcp) - 1;
386
387         req->rate = (req->parent_rate >> calcp) / (calcm + 1);
388         req->m = calcm;
389         req->p = calcp;
390 }
391
392
393
394
395 /**
396  * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
397  * CLK_OUT rate is calculated as follows
398  * rate = (parent_rate >> p) / (m + 1);
399  */
400
401 static void sun7i_a20_get_out_factors(struct factors_request *req)
402 {
403         u8 div, calcm, calcp;
404
405         /* These clocks can only divide, so we will never be able to achieve
406          * frequencies higher than the parent frequency */
407         if (req->rate > req->parent_rate)
408                 req->rate = req->parent_rate;
409
410         div = DIV_ROUND_UP(req->parent_rate, req->rate);
411
412         if (div < 32)
413                 calcp = 0;
414         else if (div / 2 < 32)
415                 calcp = 1;
416         else if (div / 4 < 32)
417                 calcp = 2;
418         else
419                 calcp = 3;
420
421         calcm = DIV_ROUND_UP(div, 1 << calcp);
422
423         req->rate = (req->parent_rate >> calcp) / calcm;
424         req->m = calcm - 1;
425         req->p = calcp;
426 }
427
428 /**
429  * sunxi_factors_clk_setup() - Setup function for factor clocks
430  */
431
432 static const struct clk_factors_config sun4i_pll1_config = {
433         .nshift = 8,
434         .nwidth = 5,
435         .kshift = 4,
436         .kwidth = 2,
437         .mshift = 0,
438         .mwidth = 2,
439         .pshift = 16,
440         .pwidth = 2,
441 };
442
443 static const struct clk_factors_config sun6i_a31_pll1_config = {
444         .nshift = 8,
445         .nwidth = 5,
446         .kshift = 4,
447         .kwidth = 2,
448         .mshift = 0,
449         .mwidth = 2,
450         .n_start = 1,
451 };
452
453 static const struct clk_factors_config sun8i_a23_pll1_config = {
454         .nshift = 8,
455         .nwidth = 5,
456         .kshift = 4,
457         .kwidth = 2,
458         .mshift = 0,
459         .mwidth = 2,
460         .pshift = 16,
461         .pwidth = 2,
462         .n_start = 1,
463 };
464
465 static const struct clk_factors_config sun4i_pll5_config = {
466         .nshift = 8,
467         .nwidth = 5,
468         .kshift = 4,
469         .kwidth = 2,
470 };
471
472 static const struct clk_factors_config sun6i_a31_pll6_config = {
473         .nshift = 8,
474         .nwidth = 5,
475         .kshift = 4,
476         .kwidth = 2,
477         .n_start = 1,
478 };
479
480 static const struct clk_factors_config sun5i_a13_ahb_config = {
481         .pshift = 4,
482         .pwidth = 2,
483 };
484
485 static const struct clk_factors_config sun6i_ahb1_config = {
486         .mshift = 6,
487         .mwidth = 2,
488         .pshift = 4,
489         .pwidth = 2,
490 };
491
492 static const struct clk_factors_config sun4i_apb1_config = {
493         .mshift = 0,
494         .mwidth = 5,
495         .pshift = 16,
496         .pwidth = 2,
497 };
498
499 /* user manual says "n" but it's really "p" */
500 static const struct clk_factors_config sun7i_a20_out_config = {
501         .mshift = 8,
502         .mwidth = 5,
503         .pshift = 20,
504         .pwidth = 2,
505 };
506
507 static const struct factors_data sun4i_pll1_data __initconst = {
508         .enable = 31,
509         .table = &sun4i_pll1_config,
510         .getter = sun4i_get_pll1_factors,
511 };
512
513 static const struct factors_data sun6i_a31_pll1_data __initconst = {
514         .enable = 31,
515         .table = &sun6i_a31_pll1_config,
516         .getter = sun6i_a31_get_pll1_factors,
517 };
518
519 static const struct factors_data sun8i_a23_pll1_data __initconst = {
520         .enable = 31,
521         .table = &sun8i_a23_pll1_config,
522         .getter = sun8i_a23_get_pll1_factors,
523 };
524
525 static const struct factors_data sun7i_a20_pll4_data __initconst = {
526         .enable = 31,
527         .table = &sun4i_pll5_config,
528         .getter = sun4i_get_pll5_factors,
529 };
530
531 static const struct factors_data sun4i_pll5_data __initconst = {
532         .enable = 31,
533         .table = &sun4i_pll5_config,
534         .getter = sun4i_get_pll5_factors,
535         .name = "pll5",
536 };
537
538 static const struct factors_data sun4i_pll6_data __initconst = {
539         .enable = 31,
540         .table = &sun4i_pll5_config,
541         .getter = sun4i_get_pll5_factors,
542         .name = "pll6",
543 };
544
545 static const struct factors_data sun6i_a31_pll6_data __initconst = {
546         .enable = 31,
547         .table = &sun6i_a31_pll6_config,
548         .getter = sun6i_a31_get_pll6_factors,
549         .recalc = sun6i_a31_pll6_recalc,
550 };
551
552 static const struct factors_data sun5i_a13_ahb_data __initconst = {
553         .mux = 6,
554         .muxmask = BIT(1) | BIT(0),
555         .table = &sun5i_a13_ahb_config,
556         .getter = sun5i_a13_get_ahb_factors,
557 };
558
559 static const struct factors_data sun6i_ahb1_data __initconst = {
560         .mux = 12,
561         .muxmask = BIT(1) | BIT(0),
562         .table = &sun6i_ahb1_config,
563         .getter = sun6i_get_ahb1_factors,
564         .recalc = sun6i_ahb1_recalc,
565 };
566
567 static const struct factors_data sun4i_apb1_data __initconst = {
568         .mux = 24,
569         .muxmask = BIT(1) | BIT(0),
570         .table = &sun4i_apb1_config,
571         .getter = sun4i_get_apb1_factors,
572 };
573
574 static const struct factors_data sun7i_a20_out_data __initconst = {
575         .enable = 31,
576         .mux = 24,
577         .muxmask = BIT(1) | BIT(0),
578         .table = &sun7i_a20_out_config,
579         .getter = sun7i_a20_get_out_factors,
580 };
581
582 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
583                                                    const struct factors_data *data)
584 {
585         void __iomem *reg;
586
587         reg = of_iomap(node, 0);
588         if (!reg) {
589                 pr_err("Could not get registers for factors-clk: %s\n",
590                        node->name);
591                 return NULL;
592         }
593
594         return sunxi_factors_register(node, data, &clk_lock, reg);
595 }
596
597 static void __init sun4i_pll1_clk_setup(struct device_node *node)
598 {
599         sunxi_factors_clk_setup(node, &sun4i_pll1_data);
600 }
601 CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk",
602                sun4i_pll1_clk_setup);
603
604 static void __init sun6i_pll1_clk_setup(struct device_node *node)
605 {
606         sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data);
607 }
608 CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk",
609                sun6i_pll1_clk_setup);
610
611 static void __init sun8i_pll1_clk_setup(struct device_node *node)
612 {
613         sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data);
614 }
615 CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk",
616                sun8i_pll1_clk_setup);
617
618 static void __init sun7i_pll4_clk_setup(struct device_node *node)
619 {
620         sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data);
621 }
622 CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk",
623                sun7i_pll4_clk_setup);
624
625 static void __init sun6i_pll6_clk_setup(struct device_node *node)
626 {
627         sunxi_factors_clk_setup(node, &sun6i_a31_pll6_data);
628 }
629 CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
630                sun6i_pll6_clk_setup);
631
632 static void __init sun5i_ahb_clk_setup(struct device_node *node)
633 {
634         sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data);
635 }
636 CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk",
637                sun5i_ahb_clk_setup);
638
639 static void __init sun6i_ahb1_clk_setup(struct device_node *node)
640 {
641         sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
642 }
643 CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
644                sun6i_ahb1_clk_setup);
645
646 static void __init sun4i_apb1_clk_setup(struct device_node *node)
647 {
648         sunxi_factors_clk_setup(node, &sun4i_apb1_data);
649 }
650 CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk",
651                sun4i_apb1_clk_setup);
652
653 static void __init sun7i_out_clk_setup(struct device_node *node)
654 {
655         sunxi_factors_clk_setup(node, &sun7i_a20_out_data);
656 }
657 CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk",
658                sun7i_out_clk_setup);
659
660
661 /**
662  * sunxi_mux_clk_setup() - Setup function for muxes
663  */
664
665 #define SUNXI_MUX_GATE_WIDTH    2
666
667 struct mux_data {
668         u8 shift;
669 };
670
671 static const struct mux_data sun4i_cpu_mux_data __initconst = {
672         .shift = 16,
673 };
674
675 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
676         .shift = 12,
677 };
678
679 static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
680         .shift = 0,
681 };
682
683 static struct clk * __init sunxi_mux_clk_setup(struct device_node *node,
684                                                const struct mux_data *data)
685 {
686         struct clk *clk;
687         const char *clk_name = node->name;
688         const char *parents[SUNXI_MAX_PARENTS];
689         void __iomem *reg;
690         int i;
691
692         reg = of_iomap(node, 0);
693
694         i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
695         if (of_property_read_string(node, "clock-output-names", &clk_name)) {
696                 pr_warn("%s: could not read clock-output-names for \"%s\"\n",
697                         __func__, clk_name);
698                 goto out_unmap;
699         }
700
701         clk = clk_register_mux(NULL, clk_name, parents, i,
702                                CLK_SET_RATE_PARENT, reg,
703                                data->shift, SUNXI_MUX_GATE_WIDTH,
704                                0, &clk_lock);
705
706         if (IS_ERR(clk)) {
707                 pr_warn("%s: failed to register mux clock %s: %ld\n", __func__,
708                         clk_name, PTR_ERR(clk));
709                 goto out_unmap;
710         }
711
712         of_clk_add_provider(node, of_clk_src_simple_get, clk);
713
714         return clk;
715
716 out_unmap:
717         iounmap(reg);
718         return NULL;
719 }
720
721 static void __init sun4i_cpu_clk_setup(struct device_node *node)
722 {
723         struct clk *clk;
724
725         clk = sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data);
726         if (!clk)
727                 return;
728
729         /* Protect CPU clock */
730         __clk_get(clk);
731         clk_prepare_enable(clk);
732 }
733 CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
734                sun4i_cpu_clk_setup);
735
736 static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
737 {
738         sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data);
739 }
740 CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
741                sun6i_ahb1_mux_clk_setup);
742
743 static void __init sun8i_ahb2_clk_setup(struct device_node *node)
744 {
745         sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data);
746 }
747 CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
748                sun8i_ahb2_clk_setup);
749
750
751 /**
752  * sunxi_divider_clk_setup() - Setup function for simple divider clocks
753  */
754
755 struct div_data {
756         u8      shift;
757         u8      pow;
758         u8      width;
759         const struct clk_div_table *table;
760 };
761
762 static const struct div_data sun4i_axi_data __initconst = {
763         .shift  = 0,
764         .pow    = 0,
765         .width  = 2,
766 };
767
768 static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
769         { .val = 0, .div = 1 },
770         { .val = 1, .div = 2 },
771         { .val = 2, .div = 3 },
772         { .val = 3, .div = 4 },
773         { .val = 4, .div = 4 },
774         { .val = 5, .div = 4 },
775         { .val = 6, .div = 4 },
776         { .val = 7, .div = 4 },
777         { } /* sentinel */
778 };
779
780 static const struct div_data sun8i_a23_axi_data __initconst = {
781         .width  = 3,
782         .table  = sun8i_a23_axi_table,
783 };
784
785 static const struct div_data sun4i_ahb_data __initconst = {
786         .shift  = 4,
787         .pow    = 1,
788         .width  = 2,
789 };
790
791 static const struct clk_div_table sun4i_apb0_table[] __initconst = {
792         { .val = 0, .div = 2 },
793         { .val = 1, .div = 2 },
794         { .val = 2, .div = 4 },
795         { .val = 3, .div = 8 },
796         { } /* sentinel */
797 };
798
799 static const struct div_data sun4i_apb0_data __initconst = {
800         .shift  = 8,
801         .pow    = 1,
802         .width  = 2,
803         .table  = sun4i_apb0_table,
804 };
805
806 static void __init sunxi_divider_clk_setup(struct device_node *node,
807                                            const struct div_data *data)
808 {
809         struct clk *clk;
810         const char *clk_name = node->name;
811         const char *clk_parent;
812         void __iomem *reg;
813
814         reg = of_iomap(node, 0);
815
816         clk_parent = of_clk_get_parent_name(node, 0);
817
818         of_property_read_string(node, "clock-output-names", &clk_name);
819
820         clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
821                                          reg, data->shift, data->width,
822                                          data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
823                                          data->table, &clk_lock);
824         if (clk)
825                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
826 }
827
828 static void __init sun4i_ahb_clk_setup(struct device_node *node)
829 {
830         sunxi_divider_clk_setup(node, &sun4i_ahb_data);
831 }
832 CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
833                sun4i_ahb_clk_setup);
834
835 static void __init sun4i_apb0_clk_setup(struct device_node *node)
836 {
837         sunxi_divider_clk_setup(node, &sun4i_apb0_data);
838 }
839 CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
840                sun4i_apb0_clk_setup);
841
842 static void __init sun4i_axi_clk_setup(struct device_node *node)
843 {
844         sunxi_divider_clk_setup(node, &sun4i_axi_data);
845 }
846 CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
847                sun4i_axi_clk_setup);
848
849 static void __init sun8i_axi_clk_setup(struct device_node *node)
850 {
851         sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
852 }
853 CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
854                sun8i_axi_clk_setup);
855
856
857
858 /**
859  * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
860  */
861
862 #define SUNXI_GATES_MAX_SIZE    64
863
864 struct gates_data {
865         DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
866 };
867
868 /**
869  * sunxi_divs_clk_setup() helper data
870  */
871
872 #define SUNXI_DIVS_MAX_QTY      4
873 #define SUNXI_DIVISOR_WIDTH     2
874
875 struct divs_data {
876         const struct factors_data *factors; /* data for the factor clock */
877         int ndivs; /* number of outputs */
878         /*
879          * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
880          * self or base factor clock refers to the output from the pll
881          * itself. The remaining refer to fixed or configurable divider
882          * outputs.
883          */
884         struct {
885                 u8 self; /* is it the base factor clock? (only one) */
886                 u8 fixed; /* is it a fixed divisor? if not... */
887                 struct clk_div_table *table; /* is it a table based divisor? */
888                 u8 shift; /* otherwise it's a normal divisor with this shift */
889                 u8 pow;   /* is it power-of-two based? */
890                 u8 gate;  /* is it independently gateable? */
891         } div[SUNXI_DIVS_MAX_QTY];
892 };
893
894 static struct clk_div_table pll6_sata_tbl[] = {
895         { .val = 0, .div = 6, },
896         { .val = 1, .div = 12, },
897         { .val = 2, .div = 18, },
898         { .val = 3, .div = 24, },
899         { } /* sentinel */
900 };
901
902 static const struct divs_data pll5_divs_data __initconst = {
903         .factors = &sun4i_pll5_data,
904         .ndivs = 2,
905         .div = {
906                 { .shift = 0, .pow = 0, }, /* M, DDR */
907                 { .shift = 16, .pow = 1, }, /* P, other */
908                 /* No output for the base factor clock */
909         }
910 };
911
912 static const struct divs_data pll6_divs_data __initconst = {
913         .factors = &sun4i_pll6_data,
914         .ndivs = 4,
915         .div = {
916                 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
917                 { .fixed = 2 }, /* P, other */
918                 { .self = 1 }, /* base factor clock, 2x */
919                 { .fixed = 4 }, /* pll6 / 4, used as ahb input */
920         }
921 };
922
923 /**
924  * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
925  *
926  * These clocks look something like this
927  *            ________________________
928  *           |         ___divisor 1---|----> to consumer
929  * parent >--|  pll___/___divisor 2---|----> to consumer
930  *           |        \_______________|____> to consumer
931  *           |________________________|
932  */
933
934 static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
935                                                  const struct divs_data *data)
936 {
937         struct clk_onecell_data *clk_data;
938         const char *parent;
939         const char *clk_name;
940         struct clk **clks, *pclk;
941         struct clk_hw *gate_hw, *rate_hw;
942         const struct clk_ops *rate_ops;
943         struct clk_gate *gate = NULL;
944         struct clk_fixed_factor *fix_factor;
945         struct clk_divider *divider;
946         void __iomem *reg;
947         int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
948         int flags, clkflags;
949
950         /* if number of children known, use it */
951         if (data->ndivs)
952                 ndivs = data->ndivs;
953
954         /* Set up factor clock that we will be dividing */
955         pclk = sunxi_factors_clk_setup(node, data->factors);
956         parent = __clk_get_name(pclk);
957
958         reg = of_iomap(node, 0);
959
960         clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
961         if (!clk_data)
962                 return NULL;
963
964         clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
965         if (!clks)
966                 goto free_clkdata;
967
968         clk_data->clks = clks;
969
970         /* It's not a good idea to have automatic reparenting changing
971          * our RAM clock! */
972         clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
973
974         for (i = 0; i < ndivs; i++) {
975                 if (of_property_read_string_index(node, "clock-output-names",
976                                                   i, &clk_name) != 0)
977                         break;
978
979                 /* If this is the base factor clock, only update clks */
980                 if (data->div[i].self) {
981                         clk_data->clks[i] = pclk;
982                         continue;
983                 }
984
985                 gate_hw = NULL;
986                 rate_hw = NULL;
987                 rate_ops = NULL;
988
989                 /* If this leaf clock can be gated, create a gate */
990                 if (data->div[i].gate) {
991                         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
992                         if (!gate)
993                                 goto free_clks;
994
995                         gate->reg = reg;
996                         gate->bit_idx = data->div[i].gate;
997                         gate->lock = &clk_lock;
998
999                         gate_hw = &gate->hw;
1000                 }
1001
1002                 /* Leaves can be fixed or configurable divisors */
1003                 if (data->div[i].fixed) {
1004                         fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1005                         if (!fix_factor)
1006                                 goto free_gate;
1007
1008                         fix_factor->mult = 1;
1009                         fix_factor->div = data->div[i].fixed;
1010
1011                         rate_hw = &fix_factor->hw;
1012                         rate_ops = &clk_fixed_factor_ops;
1013                 } else {
1014                         divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1015                         if (!divider)
1016                                 goto free_gate;
1017
1018                         flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1019
1020                         divider->reg = reg;
1021                         divider->shift = data->div[i].shift;
1022                         divider->width = SUNXI_DIVISOR_WIDTH;
1023                         divider->flags = flags;
1024                         divider->lock = &clk_lock;
1025                         divider->table = data->div[i].table;
1026
1027                         rate_hw = &divider->hw;
1028                         rate_ops = &clk_divider_ops;
1029                 }
1030
1031                 /* Wrap the (potential) gate and the divisor on a composite
1032                  * clock to unify them */
1033                 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1034                                                  NULL, NULL,
1035                                                  rate_hw, rate_ops,
1036                                                  gate_hw, &clk_gate_ops,
1037                                                  clkflags);
1038
1039                 WARN_ON(IS_ERR(clk_data->clks[i]));
1040         }
1041
1042         /* Adjust to the real max */
1043         clk_data->clk_num = i;
1044
1045         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1046
1047         return clks;
1048
1049 free_gate:
1050         kfree(gate);
1051 free_clks:
1052         kfree(clks);
1053 free_clkdata:
1054         kfree(clk_data);
1055         return NULL;
1056 }
1057
1058 static void __init sun4i_pll5_clk_setup(struct device_node *node)
1059 {
1060         struct clk **clks;
1061
1062         clks = sunxi_divs_clk_setup(node, &pll5_divs_data);
1063         if (!clks)
1064                 return;
1065
1066         /* Protect PLL5_DDR */
1067         __clk_get(clks[0]);
1068         clk_prepare_enable(clks[0]);
1069 }
1070 CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
1071                sun4i_pll5_clk_setup);
1072
1073 static void __init sun4i_pll6_clk_setup(struct device_node *node)
1074 {
1075         sunxi_divs_clk_setup(node, &pll6_divs_data);
1076 }
1077 CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
1078                sun4i_pll6_clk_setup);
1079