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