clk: sunxi: Remove calls to clk_put
[pandora-kernel.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-provider.h>
18 #include <linux/clkdev.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/reset-controller.h>
22
23 #include "clk-factors.h"
24
25 static DEFINE_SPINLOCK(clk_lock);
26
27 /* Maximum number of parents our clocks have */
28 #define SUNXI_MAX_PARENTS       5
29
30 /**
31  * sun4i_osc_clk_setup() - Setup function for gatable oscillator
32  */
33
34 #define SUNXI_OSC24M_GATE       0
35
36 static void __init sun4i_osc_clk_setup(struct device_node *node)
37 {
38         struct clk *clk;
39         struct clk_fixed_rate *fixed;
40         struct clk_gate *gate;
41         const char *clk_name = node->name;
42         u32 rate;
43
44         if (of_property_read_u32(node, "clock-frequency", &rate))
45                 return;
46
47         /* allocate fixed-rate and gate clock structs */
48         fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
49         if (!fixed)
50                 return;
51         gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
52         if (!gate)
53                 goto err_free_fixed;
54
55         of_property_read_string(node, "clock-output-names", &clk_name);
56
57         /* set up gate and fixed rate properties */
58         gate->reg = of_iomap(node, 0);
59         gate->bit_idx = SUNXI_OSC24M_GATE;
60         gate->lock = &clk_lock;
61         fixed->fixed_rate = rate;
62
63         clk = clk_register_composite(NULL, clk_name,
64                         NULL, 0,
65                         NULL, NULL,
66                         &fixed->hw, &clk_fixed_rate_ops,
67                         &gate->hw, &clk_gate_ops,
68                         CLK_IS_ROOT);
69
70         if (IS_ERR(clk))
71                 goto err_free_gate;
72
73         of_clk_add_provider(node, of_clk_src_simple_get, clk);
74         clk_register_clkdev(clk, clk_name, NULL);
75
76         return;
77
78 err_free_gate:
79         kfree(gate);
80 err_free_fixed:
81         kfree(fixed);
82 }
83 CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-a10-osc-clk", sun4i_osc_clk_setup);
84
85
86
87 /**
88  * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
89  * PLL1 rate is calculated as follows
90  * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
91  * parent_rate is always 24Mhz
92  */
93
94 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
95                                    u8 *n, u8 *k, u8 *m, u8 *p)
96 {
97         u8 div;
98
99         /* Normalize value to a 6M multiple */
100         div = *freq / 6000000;
101         *freq = 6000000 * div;
102
103         /* we were called to round the frequency, we can now return */
104         if (n == NULL)
105                 return;
106
107         /* m is always zero for pll1 */
108         *m = 0;
109
110         /* k is 1 only on these cases */
111         if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
112                 *k = 1;
113         else
114                 *k = 0;
115
116         /* p will be 3 for divs under 10 */
117         if (div < 10)
118                 *p = 3;
119
120         /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
121         else if (div < 20 || (div < 32 && (div & 1)))
122                 *p = 2;
123
124         /* p will be 1 for even divs under 32, divs under 40 and odd pairs
125          * of divs between 40-62 */
126         else if (div < 40 || (div < 64 && (div & 2)))
127                 *p = 1;
128
129         /* any other entries have p = 0 */
130         else
131                 *p = 0;
132
133         /* calculate a suitable n based on k and p */
134         div <<= *p;
135         div /= (*k + 1);
136         *n = div / 4;
137 }
138
139 /**
140  * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
141  * PLL1 rate is calculated as follows
142  * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
143  * parent_rate should always be 24MHz
144  */
145 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
146                                        u8 *n, u8 *k, u8 *m, u8 *p)
147 {
148         /*
149          * We can operate only on MHz, this will make our life easier
150          * later.
151          */
152         u32 freq_mhz = *freq / 1000000;
153         u32 parent_freq_mhz = parent_rate / 1000000;
154
155         /*
156          * Round down the frequency to the closest multiple of either
157          * 6 or 16
158          */
159         u32 round_freq_6 = round_down(freq_mhz, 6);
160         u32 round_freq_16 = round_down(freq_mhz, 16);
161
162         if (round_freq_6 > round_freq_16)
163                 freq_mhz = round_freq_6;
164         else
165                 freq_mhz = round_freq_16;
166
167         *freq = freq_mhz * 1000000;
168
169         /*
170          * If the factors pointer are null, we were just called to
171          * round down the frequency.
172          * Exit.
173          */
174         if (n == NULL)
175                 return;
176
177         /* If the frequency is a multiple of 32 MHz, k is always 3 */
178         if (!(freq_mhz % 32))
179                 *k = 3;
180         /* If the frequency is a multiple of 9 MHz, k is always 2 */
181         else if (!(freq_mhz % 9))
182                 *k = 2;
183         /* If the frequency is a multiple of 8 MHz, k is always 1 */
184         else if (!(freq_mhz % 8))
185                 *k = 1;
186         /* Otherwise, we don't use the k factor */
187         else
188                 *k = 0;
189
190         /*
191          * If the frequency is a multiple of 2 but not a multiple of
192          * 3, m is 3. This is the first time we use 6 here, yet we
193          * will use it on several other places.
194          * We use this number because it's the lowest frequency we can
195          * generate (with n = 0, k = 0, m = 3), so every other frequency
196          * somehow relates to this frequency.
197          */
198         if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
199                 *m = 2;
200         /*
201          * If the frequency is a multiple of 6MHz, but the factor is
202          * odd, m will be 3
203          */
204         else if ((freq_mhz / 6) & 1)
205                 *m = 3;
206         /* Otherwise, we end up with m = 1 */
207         else
208                 *m = 1;
209
210         /* Calculate n thanks to the above factors we already got */
211         *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
212
213         /*
214          * If n end up being outbound, and that we can still decrease
215          * m, do it.
216          */
217         if ((*n + 1) > 31 && (*m + 1) > 1) {
218                 *n = (*n + 1) / 2 - 1;
219                 *m = (*m + 1) / 2 - 1;
220         }
221 }
222
223 /**
224  * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
225  * PLL5 rate is calculated as follows
226  * rate = parent_rate * n * (k + 1)
227  * parent_rate is always 24Mhz
228  */
229
230 static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
231                                    u8 *n, u8 *k, u8 *m, u8 *p)
232 {
233         u8 div;
234
235         /* Normalize value to a parent_rate multiple (24M) */
236         div = *freq / parent_rate;
237         *freq = parent_rate * div;
238
239         /* we were called to round the frequency, we can now return */
240         if (n == NULL)
241                 return;
242
243         if (div < 31)
244                 *k = 0;
245         else if (div / 2 < 31)
246                 *k = 1;
247         else if (div / 3 < 31)
248                 *k = 2;
249         else
250                 *k = 3;
251
252         *n = DIV_ROUND_UP(div, (*k+1));
253 }
254
255 /**
256  * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
257  * PLL6 rate is calculated as follows
258  * rate = parent_rate * n * (k + 1) / 2
259  * parent_rate is always 24Mhz
260  */
261
262 static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
263                                        u8 *n, u8 *k, u8 *m, u8 *p)
264 {
265         u8 div;
266
267         /*
268          * We always have 24MHz / 2, so we can just say that our
269          * parent clock is 12MHz.
270          */
271         parent_rate = parent_rate / 2;
272
273         /* Normalize value to a parent_rate multiple (24M / 2) */
274         div = *freq / parent_rate;
275         *freq = parent_rate * div;
276
277         /* we were called to round the frequency, we can now return */
278         if (n == NULL)
279                 return;
280
281         *k = div / 32;
282         if (*k > 3)
283                 *k = 3;
284
285         *n = DIV_ROUND_UP(div, (*k+1));
286 }
287
288 /**
289  * sun4i_get_apb1_factors() - calculates m, p factors for APB1
290  * APB1 rate is calculated as follows
291  * rate = (parent_rate >> p) / (m + 1);
292  */
293
294 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
295                                    u8 *n, u8 *k, u8 *m, u8 *p)
296 {
297         u8 calcm, calcp;
298
299         if (parent_rate < *freq)
300                 *freq = parent_rate;
301
302         parent_rate = DIV_ROUND_UP(parent_rate, *freq);
303
304         /* Invalid rate! */
305         if (parent_rate > 32)
306                 return;
307
308         if (parent_rate <= 4)
309                 calcp = 0;
310         else if (parent_rate <= 8)
311                 calcp = 1;
312         else if (parent_rate <= 16)
313                 calcp = 2;
314         else
315                 calcp = 3;
316
317         calcm = (parent_rate >> calcp) - 1;
318
319         *freq = (parent_rate >> calcp) / (calcm + 1);
320
321         /* we were called to round the frequency, we can now return */
322         if (n == NULL)
323                 return;
324
325         *m = calcm;
326         *p = calcp;
327 }
328
329
330
331 /**
332  * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
333  * MOD0 rate is calculated as follows
334  * rate = (parent_rate >> p) / (m + 1);
335  */
336
337 static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
338                                    u8 *n, u8 *k, u8 *m, u8 *p)
339 {
340         u8 div, calcm, calcp;
341
342         /* These clocks can only divide, so we will never be able to achieve
343          * frequencies higher than the parent frequency */
344         if (*freq > parent_rate)
345                 *freq = parent_rate;
346
347         div = DIV_ROUND_UP(parent_rate, *freq);
348
349         if (div < 16)
350                 calcp = 0;
351         else if (div / 2 < 16)
352                 calcp = 1;
353         else if (div / 4 < 16)
354                 calcp = 2;
355         else
356                 calcp = 3;
357
358         calcm = DIV_ROUND_UP(div, 1 << calcp);
359
360         *freq = (parent_rate >> calcp) / calcm;
361
362         /* we were called to round the frequency, we can now return */
363         if (n == NULL)
364                 return;
365
366         *m = calcm - 1;
367         *p = calcp;
368 }
369
370
371
372 /**
373  * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
374  * CLK_OUT rate is calculated as follows
375  * rate = (parent_rate >> p) / (m + 1);
376  */
377
378 static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
379                                       u8 *n, u8 *k, u8 *m, u8 *p)
380 {
381         u8 div, calcm, calcp;
382
383         /* These clocks can only divide, so we will never be able to achieve
384          * frequencies higher than the parent frequency */
385         if (*freq > parent_rate)
386                 *freq = parent_rate;
387
388         div = DIV_ROUND_UP(parent_rate, *freq);
389
390         if (div < 32)
391                 calcp = 0;
392         else if (div / 2 < 32)
393                 calcp = 1;
394         else if (div / 4 < 32)
395                 calcp = 2;
396         else
397                 calcp = 3;
398
399         calcm = DIV_ROUND_UP(div, 1 << calcp);
400
401         *freq = (parent_rate >> calcp) / calcm;
402
403         /* we were called to round the frequency, we can now return */
404         if (n == NULL)
405                 return;
406
407         *m = calcm - 1;
408         *p = calcp;
409 }
410
411
412
413 /**
414  * sun7i_a20_gmac_clk_setup - Setup function for A20/A31 GMAC clock module
415  *
416  * This clock looks something like this
417  *                               ________________________
418  *  MII TX clock from PHY >-----|___________    _________|----> to GMAC core
419  *  GMAC Int. RGMII TX clk >----|___________\__/__gate---|----> to PHY
420  *  Ext. 125MHz RGMII TX clk >--|__divider__/            |
421  *                              |________________________|
422  *
423  * The external 125 MHz reference is optional, i.e. GMAC can use its
424  * internal TX clock just fine. The A31 GMAC clock module does not have
425  * the divider controls for the external reference.
426  *
427  * To keep it simple, let the GMAC use either the MII TX clock for MII mode,
428  * and its internal TX clock for GMII and RGMII modes. The GMAC driver should
429  * select the appropriate source and gate/ungate the output to the PHY.
430  *
431  * Only the GMAC should use this clock. Altering the clock so that it doesn't
432  * match the GMAC's operation parameters will result in the GMAC not being
433  * able to send traffic out. The GMAC driver should set the clock rate and
434  * enable/disable this clock to configure the required state. The clock
435  * driver then responds by auto-reparenting the clock.
436  */
437
438 #define SUN7I_A20_GMAC_GPIT     2
439 #define SUN7I_A20_GMAC_MASK     0x3
440 #define SUN7I_A20_GMAC_PARENTS  2
441
442 static void __init sun7i_a20_gmac_clk_setup(struct device_node *node)
443 {
444         struct clk *clk;
445         struct clk_mux *mux;
446         struct clk_gate *gate;
447         const char *clk_name = node->name;
448         const char *parents[SUN7I_A20_GMAC_PARENTS];
449         void *reg;
450
451         if (of_property_read_string(node, "clock-output-names", &clk_name))
452                 return;
453
454         /* allocate mux and gate clock structs */
455         mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
456         if (!mux)
457                 return;
458
459         gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
460         if (!gate)
461                 goto free_mux;
462
463         /* gmac clock requires exactly 2 parents */
464         parents[0] = of_clk_get_parent_name(node, 0);
465         parents[1] = of_clk_get_parent_name(node, 1);
466         if (!parents[0] || !parents[1])
467                 goto free_gate;
468
469         reg = of_iomap(node, 0);
470         if (!reg)
471                 goto free_gate;
472
473         /* set up gate and fixed rate properties */
474         gate->reg = reg;
475         gate->bit_idx = SUN7I_A20_GMAC_GPIT;
476         gate->lock = &clk_lock;
477         mux->reg = reg;
478         mux->mask = SUN7I_A20_GMAC_MASK;
479         mux->flags = CLK_MUX_INDEX_BIT;
480         mux->lock = &clk_lock;
481
482         clk = clk_register_composite(NULL, clk_name,
483                         parents, SUN7I_A20_GMAC_PARENTS,
484                         &mux->hw, &clk_mux_ops,
485                         NULL, NULL,
486                         &gate->hw, &clk_gate_ops,
487                         0);
488
489         if (IS_ERR(clk))
490                 goto iounmap_reg;
491
492         of_clk_add_provider(node, of_clk_src_simple_get, clk);
493         clk_register_clkdev(clk, clk_name, NULL);
494
495         return;
496
497 iounmap_reg:
498         iounmap(reg);
499 free_gate:
500         kfree(gate);
501 free_mux:
502         kfree(mux);
503 }
504 CLK_OF_DECLARE(sun7i_a20_gmac, "allwinner,sun7i-a20-gmac-clk",
505                 sun7i_a20_gmac_clk_setup);
506
507
508
509 /**
510  * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
511  */
512
513 void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
514 {
515         #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
516         #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
517
518         struct clk_hw *hw = __clk_get_hw(clk);
519         struct clk_composite *composite = to_clk_composite(hw);
520         struct clk_hw *rate_hw = composite->rate_hw;
521         struct clk_factors *factors = to_clk_factors(rate_hw);
522         unsigned long flags = 0;
523         u32 reg;
524
525         if (factors->lock)
526                 spin_lock_irqsave(factors->lock, flags);
527
528         reg = readl(factors->reg);
529
530         /* set sample clock phase control */
531         reg &= ~(0x7 << 20);
532         reg |= ((sample & 0x7) << 20);
533
534         /* set output clock phase control */
535         reg &= ~(0x7 << 8);
536         reg |= ((output & 0x7) << 8);
537
538         writel(reg, factors->reg);
539
540         if (factors->lock)
541                 spin_unlock_irqrestore(factors->lock, flags);
542 }
543 EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
544
545
546 /**
547  * sunxi_factors_clk_setup() - Setup function for factor clocks
548  */
549
550 #define SUNXI_FACTORS_MUX_MASK 0x3
551
552 struct factors_data {
553         int enable;
554         int mux;
555         struct clk_factors_config *table;
556         void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
557         const char *name;
558 };
559
560 static struct clk_factors_config sun4i_pll1_config = {
561         .nshift = 8,
562         .nwidth = 5,
563         .kshift = 4,
564         .kwidth = 2,
565         .mshift = 0,
566         .mwidth = 2,
567         .pshift = 16,
568         .pwidth = 2,
569 };
570
571 static struct clk_factors_config sun6i_a31_pll1_config = {
572         .nshift = 8,
573         .nwidth = 5,
574         .kshift = 4,
575         .kwidth = 2,
576         .mshift = 0,
577         .mwidth = 2,
578 };
579
580 static struct clk_factors_config sun4i_pll5_config = {
581         .nshift = 8,
582         .nwidth = 5,
583         .kshift = 4,
584         .kwidth = 2,
585 };
586
587 static struct clk_factors_config sun6i_a31_pll6_config = {
588         .nshift = 8,
589         .nwidth = 5,
590         .kshift = 4,
591         .kwidth = 2,
592 };
593
594 static struct clk_factors_config sun4i_apb1_config = {
595         .mshift = 0,
596         .mwidth = 5,
597         .pshift = 16,
598         .pwidth = 2,
599 };
600
601 /* user manual says "n" but it's really "p" */
602 static struct clk_factors_config sun4i_mod0_config = {
603         .mshift = 0,
604         .mwidth = 4,
605         .pshift = 16,
606         .pwidth = 2,
607 };
608
609 /* user manual says "n" but it's really "p" */
610 static struct clk_factors_config sun7i_a20_out_config = {
611         .mshift = 8,
612         .mwidth = 5,
613         .pshift = 20,
614         .pwidth = 2,
615 };
616
617 static const struct factors_data sun4i_pll1_data __initconst = {
618         .enable = 31,
619         .table = &sun4i_pll1_config,
620         .getter = sun4i_get_pll1_factors,
621 };
622
623 static const struct factors_data sun6i_a31_pll1_data __initconst = {
624         .enable = 31,
625         .table = &sun6i_a31_pll1_config,
626         .getter = sun6i_a31_get_pll1_factors,
627 };
628
629 static const struct factors_data sun7i_a20_pll4_data __initconst = {
630         .enable = 31,
631         .table = &sun4i_pll5_config,
632         .getter = sun4i_get_pll5_factors,
633 };
634
635 static const struct factors_data sun4i_pll5_data __initconst = {
636         .enable = 31,
637         .table = &sun4i_pll5_config,
638         .getter = sun4i_get_pll5_factors,
639         .name = "pll5",
640 };
641
642 static const struct factors_data sun4i_pll6_data __initconst = {
643         .enable = 31,
644         .table = &sun4i_pll5_config,
645         .getter = sun4i_get_pll5_factors,
646         .name = "pll6",
647 };
648
649 static const struct factors_data sun6i_a31_pll6_data __initconst = {
650         .enable = 31,
651         .table = &sun6i_a31_pll6_config,
652         .getter = sun6i_a31_get_pll6_factors,
653 };
654
655 static const struct factors_data sun4i_apb1_data __initconst = {
656         .table = &sun4i_apb1_config,
657         .getter = sun4i_get_apb1_factors,
658 };
659
660 static const struct factors_data sun4i_mod0_data __initconst = {
661         .enable = 31,
662         .mux = 24,
663         .table = &sun4i_mod0_config,
664         .getter = sun4i_get_mod0_factors,
665 };
666
667 static const struct factors_data sun7i_a20_out_data __initconst = {
668         .enable = 31,
669         .mux = 24,
670         .table = &sun7i_a20_out_config,
671         .getter = sun7i_a20_get_out_factors,
672 };
673
674 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
675                                                 const struct factors_data *data)
676 {
677         struct clk *clk;
678         struct clk_factors *factors;
679         struct clk_gate *gate = NULL;
680         struct clk_mux *mux = NULL;
681         struct clk_hw *gate_hw = NULL;
682         struct clk_hw *mux_hw = NULL;
683         const char *clk_name = node->name;
684         const char *parents[SUNXI_MAX_PARENTS];
685         void *reg;
686         int i = 0;
687
688         reg = of_iomap(node, 0);
689
690         /* if we have a mux, we will have >1 parents */
691         while (i < SUNXI_MAX_PARENTS &&
692                (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
693                 i++;
694
695         /*
696          * some factor clocks, such as pll5 and pll6, may have multiple
697          * outputs, and have their name designated in factors_data
698          */
699         if (data->name)
700                 clk_name = data->name;
701         else
702                 of_property_read_string(node, "clock-output-names", &clk_name);
703
704         factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
705         if (!factors)
706                 return NULL;
707
708         /* Add a gate if this factor clock can be gated */
709         if (data->enable) {
710                 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
711                 if (!gate) {
712                         kfree(factors);
713                         return NULL;
714                 }
715
716                 /* set up gate properties */
717                 gate->reg = reg;
718                 gate->bit_idx = data->enable;
719                 gate->lock = &clk_lock;
720                 gate_hw = &gate->hw;
721         }
722
723         /* Add a mux if this factor clock can be muxed */
724         if (data->mux) {
725                 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
726                 if (!mux) {
727                         kfree(factors);
728                         kfree(gate);
729                         return NULL;
730                 }
731
732                 /* set up gate properties */
733                 mux->reg = reg;
734                 mux->shift = data->mux;
735                 mux->mask = SUNXI_FACTORS_MUX_MASK;
736                 mux->lock = &clk_lock;
737                 mux_hw = &mux->hw;
738         }
739
740         /* set up factors properties */
741         factors->reg = reg;
742         factors->config = data->table;
743         factors->get_factors = data->getter;
744         factors->lock = &clk_lock;
745
746         clk = clk_register_composite(NULL, clk_name,
747                         parents, i,
748                         mux_hw, &clk_mux_ops,
749                         &factors->hw, &clk_factors_ops,
750                         gate_hw, &clk_gate_ops, 0);
751
752         if (!IS_ERR(clk)) {
753                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
754                 clk_register_clkdev(clk, clk_name, NULL);
755         }
756
757         return clk;
758 }
759
760
761
762 /**
763  * sunxi_mux_clk_setup() - Setup function for muxes
764  */
765
766 #define SUNXI_MUX_GATE_WIDTH    2
767
768 struct mux_data {
769         u8 shift;
770 };
771
772 static const struct mux_data sun4i_cpu_mux_data __initconst = {
773         .shift = 16,
774 };
775
776 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
777         .shift = 12,
778 };
779
780 static const struct mux_data sun4i_apb1_mux_data __initconst = {
781         .shift = 24,
782 };
783
784 static void __init sunxi_mux_clk_setup(struct device_node *node,
785                                        struct mux_data *data)
786 {
787         struct clk *clk;
788         const char *clk_name = node->name;
789         const char *parents[SUNXI_MAX_PARENTS];
790         void *reg;
791         int i = 0;
792
793         reg = of_iomap(node, 0);
794
795         while (i < SUNXI_MAX_PARENTS &&
796                (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
797                 i++;
798
799         of_property_read_string(node, "clock-output-names", &clk_name);
800
801         clk = clk_register_mux(NULL, clk_name, parents, i,
802                                CLK_SET_RATE_NO_REPARENT, reg,
803                                data->shift, SUNXI_MUX_GATE_WIDTH,
804                                0, &clk_lock);
805
806         if (clk) {
807                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
808                 clk_register_clkdev(clk, clk_name, NULL);
809         }
810 }
811
812
813
814 /**
815  * sunxi_divider_clk_setup() - Setup function for simple divider clocks
816  */
817
818 struct div_data {
819         u8      shift;
820         u8      pow;
821         u8      width;
822 };
823
824 static const struct div_data sun4i_axi_data __initconst = {
825         .shift  = 0,
826         .pow    = 0,
827         .width  = 2,
828 };
829
830 static const struct div_data sun4i_ahb_data __initconst = {
831         .shift  = 4,
832         .pow    = 1,
833         .width  = 2,
834 };
835
836 static const struct div_data sun4i_apb0_data __initconst = {
837         .shift  = 8,
838         .pow    = 1,
839         .width  = 2,
840 };
841
842 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
843         .shift  = 0,
844         .pow    = 0,
845         .width  = 4,
846 };
847
848 static void __init sunxi_divider_clk_setup(struct device_node *node,
849                                            struct div_data *data)
850 {
851         struct clk *clk;
852         const char *clk_name = node->name;
853         const char *clk_parent;
854         void *reg;
855
856         reg = of_iomap(node, 0);
857
858         clk_parent = of_clk_get_parent_name(node, 0);
859
860         of_property_read_string(node, "clock-output-names", &clk_name);
861
862         clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
863                                    reg, data->shift, data->width,
864                                    data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
865                                    &clk_lock);
866         if (clk) {
867                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
868                 clk_register_clkdev(clk, clk_name, NULL);
869         }
870 }
871
872
873
874 /**
875  * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
876  */
877
878 struct gates_reset_data {
879         void __iomem                    *reg;
880         spinlock_t                      *lock;
881         struct reset_controller_dev     rcdev;
882 };
883
884 static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
885                               unsigned long id)
886 {
887         struct gates_reset_data *data = container_of(rcdev,
888                                                      struct gates_reset_data,
889                                                      rcdev);
890         unsigned long flags;
891         u32 reg;
892
893         spin_lock_irqsave(data->lock, flags);
894
895         reg = readl(data->reg);
896         writel(reg & ~BIT(id), data->reg);
897
898         spin_unlock_irqrestore(data->lock, flags);
899
900         return 0;
901 }
902
903 static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
904                                 unsigned long id)
905 {
906         struct gates_reset_data *data = container_of(rcdev,
907                                                      struct gates_reset_data,
908                                                      rcdev);
909         unsigned long flags;
910         u32 reg;
911
912         spin_lock_irqsave(data->lock, flags);
913
914         reg = readl(data->reg);
915         writel(reg | BIT(id), data->reg);
916
917         spin_unlock_irqrestore(data->lock, flags);
918
919         return 0;
920 }
921
922 static struct reset_control_ops sunxi_gates_reset_ops = {
923         .assert         = sunxi_gates_reset_assert,
924         .deassert       = sunxi_gates_reset_deassert,
925 };
926
927 /**
928  * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
929  */
930
931 #define SUNXI_GATES_MAX_SIZE    64
932
933 struct gates_data {
934         DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
935         u32 reset_mask;
936 };
937
938 static const struct gates_data sun4i_axi_gates_data __initconst = {
939         .mask = {1},
940 };
941
942 static const struct gates_data sun4i_ahb_gates_data __initconst = {
943         .mask = {0x7F77FFF, 0x14FB3F},
944 };
945
946 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
947         .mask = {0x147667e7, 0x185915},
948 };
949
950 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
951         .mask = {0x107067e7, 0x185111},
952 };
953
954 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
955         .mask = {0xEDFE7F62, 0x794F931},
956 };
957
958 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
959         .mask = { 0x12f77fff, 0x16ff3f },
960 };
961
962 static const struct gates_data sun4i_apb0_gates_data __initconst = {
963         .mask = {0x4EF},
964 };
965
966 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
967         .mask = {0x469},
968 };
969
970 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
971         .mask = {0x61},
972 };
973
974 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
975         .mask = { 0x4ff },
976 };
977
978 static const struct gates_data sun4i_apb1_gates_data __initconst = {
979         .mask = {0xFF00F7},
980 };
981
982 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
983         .mask = {0xf0007},
984 };
985
986 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
987         .mask = {0xa0007},
988 };
989
990 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
991         .mask = {0x3031},
992 };
993
994 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
995         .mask = {0x3F000F},
996 };
997
998 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
999         .mask = { 0xff80ff },
1000 };
1001
1002 static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
1003         .mask = {0x1C0},
1004         .reset_mask = 0x07,
1005 };
1006
1007 static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
1008         .mask = {0x140},
1009         .reset_mask = 0x03,
1010 };
1011
1012 static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
1013         .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
1014         .reset_mask = BIT(2) | BIT(1) | BIT(0),
1015 };
1016
1017 static void __init sunxi_gates_clk_setup(struct device_node *node,
1018                                          struct gates_data *data)
1019 {
1020         struct clk_onecell_data *clk_data;
1021         struct gates_reset_data *reset_data;
1022         const char *clk_parent;
1023         const char *clk_name;
1024         void *reg;
1025         int qty;
1026         int i = 0;
1027         int j = 0;
1028         int ignore;
1029
1030         reg = of_iomap(node, 0);
1031
1032         clk_parent = of_clk_get_parent_name(node, 0);
1033
1034         /* Worst-case size approximation and memory allocation */
1035         qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
1036         clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1037         if (!clk_data)
1038                 return;
1039         clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
1040         if (!clk_data->clks) {
1041                 kfree(clk_data);
1042                 return;
1043         }
1044
1045         for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
1046                 of_property_read_string_index(node, "clock-output-names",
1047                                               j, &clk_name);
1048
1049                 /* No driver claims this clock, but it should remain gated */
1050                 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
1051
1052                 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
1053                                                       clk_parent, ignore,
1054                                                       reg + 4 * (i/32), i % 32,
1055                                                       0, &clk_lock);
1056                 WARN_ON(IS_ERR(clk_data->clks[i]));
1057
1058                 j++;
1059         }
1060
1061         /* Adjust to the real max */
1062         clk_data->clk_num = i;
1063
1064         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1065
1066         /* Register a reset controler for gates with reset bits */
1067         if (data->reset_mask == 0)
1068                 return;
1069
1070         reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
1071         if (!reset_data)
1072                 return;
1073
1074         reset_data->reg = reg;
1075         reset_data->lock = &clk_lock;
1076         reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
1077         reset_data->rcdev.ops = &sunxi_gates_reset_ops;
1078         reset_data->rcdev.of_node = node;
1079         reset_controller_register(&reset_data->rcdev);
1080 }
1081
1082
1083
1084 /**
1085  * sunxi_divs_clk_setup() helper data
1086  */
1087
1088 #define SUNXI_DIVS_MAX_QTY      2
1089 #define SUNXI_DIVISOR_WIDTH     2
1090
1091 struct divs_data {
1092         const struct factors_data *factors; /* data for the factor clock */
1093         struct {
1094                 u8 fixed; /* is it a fixed divisor? if not... */
1095                 struct clk_div_table *table; /* is it a table based divisor? */
1096                 u8 shift; /* otherwise it's a normal divisor with this shift */
1097                 u8 pow;   /* is it power-of-two based? */
1098                 u8 gate;  /* is it independently gateable? */
1099         } div[SUNXI_DIVS_MAX_QTY];
1100 };
1101
1102 static struct clk_div_table pll6_sata_tbl[] = {
1103         { .val = 0, .div = 6, },
1104         { .val = 1, .div = 12, },
1105         { .val = 2, .div = 18, },
1106         { .val = 3, .div = 24, },
1107         { } /* sentinel */
1108 };
1109
1110 static const struct divs_data pll5_divs_data __initconst = {
1111         .factors = &sun4i_pll5_data,
1112         .div = {
1113                 { .shift = 0, .pow = 0, }, /* M, DDR */
1114                 { .shift = 16, .pow = 1, }, /* P, other */
1115         }
1116 };
1117
1118 static const struct divs_data pll6_divs_data __initconst = {
1119         .factors = &sun4i_pll6_data,
1120         .div = {
1121                 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
1122                 { .fixed = 2 }, /* P, other */
1123         }
1124 };
1125
1126 /**
1127  * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
1128  *
1129  * These clocks look something like this
1130  *            ________________________
1131  *           |         ___divisor 1---|----> to consumer
1132  * parent >--|  pll___/___divisor 2---|----> to consumer
1133  *           |        \_______________|____> to consumer
1134  *           |________________________|
1135  */
1136
1137 static void __init sunxi_divs_clk_setup(struct device_node *node,
1138                                         struct divs_data *data)
1139 {
1140         struct clk_onecell_data *clk_data;
1141         const char *parent;
1142         const char *clk_name;
1143         struct clk **clks, *pclk;
1144         struct clk_hw *gate_hw, *rate_hw;
1145         const struct clk_ops *rate_ops;
1146         struct clk_gate *gate = NULL;
1147         struct clk_fixed_factor *fix_factor;
1148         struct clk_divider *divider;
1149         void *reg;
1150         int i = 0;
1151         int flags, clkflags;
1152
1153         /* Set up factor clock that we will be dividing */
1154         pclk = sunxi_factors_clk_setup(node, data->factors);
1155         parent = __clk_get_name(pclk);
1156
1157         reg = of_iomap(node, 0);
1158
1159         clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1160         if (!clk_data)
1161                 return;
1162
1163         clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
1164         if (!clks)
1165                 goto free_clkdata;
1166
1167         clk_data->clks = clks;
1168
1169         /* It's not a good idea to have automatic reparenting changing
1170          * our RAM clock! */
1171         clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1172
1173         for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1174                 if (of_property_read_string_index(node, "clock-output-names",
1175                                                   i, &clk_name) != 0)
1176                         break;
1177
1178                 gate_hw = NULL;
1179                 rate_hw = NULL;
1180                 rate_ops = NULL;
1181
1182                 /* If this leaf clock can be gated, create a gate */
1183                 if (data->div[i].gate) {
1184                         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1185                         if (!gate)
1186                                 goto free_clks;
1187
1188                         gate->reg = reg;
1189                         gate->bit_idx = data->div[i].gate;
1190                         gate->lock = &clk_lock;
1191
1192                         gate_hw = &gate->hw;
1193                 }
1194
1195                 /* Leaves can be fixed or configurable divisors */
1196                 if (data->div[i].fixed) {
1197                         fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1198                         if (!fix_factor)
1199                                 goto free_gate;
1200
1201                         fix_factor->mult = 1;
1202                         fix_factor->div = data->div[i].fixed;
1203
1204                         rate_hw = &fix_factor->hw;
1205                         rate_ops = &clk_fixed_factor_ops;
1206                 } else {
1207                         divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1208                         if (!divider)
1209                                 goto free_gate;
1210
1211                         flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1212
1213                         divider->reg = reg;
1214                         divider->shift = data->div[i].shift;
1215                         divider->width = SUNXI_DIVISOR_WIDTH;
1216                         divider->flags = flags;
1217                         divider->lock = &clk_lock;
1218                         divider->table = data->div[i].table;
1219
1220                         rate_hw = &divider->hw;
1221                         rate_ops = &clk_divider_ops;
1222                 }
1223
1224                 /* Wrap the (potential) gate and the divisor on a composite
1225                  * clock to unify them */
1226                 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1227                                                  NULL, NULL,
1228                                                  rate_hw, rate_ops,
1229                                                  gate_hw, &clk_gate_ops,
1230                                                  clkflags);
1231
1232                 WARN_ON(IS_ERR(clk_data->clks[i]));
1233                 clk_register_clkdev(clks[i], clk_name, NULL);
1234         }
1235
1236         /* The last clock available on the getter is the parent */
1237         clks[i++] = pclk;
1238
1239         /* Adjust to the real max */
1240         clk_data->clk_num = i;
1241
1242         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1243
1244         return;
1245
1246 free_gate:
1247         kfree(gate);
1248 free_clks:
1249         kfree(clks);
1250 free_clkdata:
1251         kfree(clk_data);
1252 }
1253
1254
1255
1256 /* Matches for factors clocks */
1257 static const struct of_device_id clk_factors_match[] __initconst = {
1258         {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
1259         {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
1260         {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
1261         {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
1262         {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1263         {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
1264         {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
1265         {}
1266 };
1267
1268 /* Matches for divider clocks */
1269 static const struct of_device_id clk_div_match[] __initconst = {
1270         {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
1271         {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1272         {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
1273         {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
1274         {}
1275 };
1276
1277 /* Matches for divided outputs */
1278 static const struct of_device_id clk_divs_match[] __initconst = {
1279         {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1280         {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
1281         {}
1282 };
1283
1284 /* Matches for mux clocks */
1285 static const struct of_device_id clk_mux_match[] __initconst = {
1286         {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1287         {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
1288         {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
1289         {}
1290 };
1291
1292 /* Matches for gate clocks */
1293 static const struct of_device_id clk_gates_match[] __initconst = {
1294         {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1295         {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
1296         {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
1297         {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
1298         {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
1299         {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
1300         {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
1301         {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
1302         {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
1303         {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
1304         {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
1305         {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
1306         {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
1307         {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
1308         {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
1309         {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
1310         {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1311         {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
1312         {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
1313         {}
1314 };
1315
1316 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1317                                               void *function)
1318 {
1319         struct device_node *np;
1320         const struct div_data *data;
1321         const struct of_device_id *match;
1322         void (*setup_function)(struct device_node *, const void *) = function;
1323
1324         for_each_matching_node_and_match(np, clk_match, &match) {
1325                 data = match->data;
1326                 setup_function(np, data);
1327         }
1328 }
1329
1330 /**
1331  * System clock protection
1332  *
1333  * By enabling these critical clocks, we prevent their accidental gating
1334  * by the framework
1335  */
1336 static void __init sunxi_clock_protect(void)
1337 {
1338         struct clk *clk;
1339
1340         /* memory bus clock - sun5i+ */
1341         clk = clk_get(NULL, "mbus");
1342         if (!IS_ERR(clk))
1343                 clk_prepare_enable(clk);
1344
1345         /* DDR clock - sun4i+ */
1346         clk = clk_get(NULL, "pll5_ddr");
1347         if (!IS_ERR(clk))
1348                 clk_prepare_enable(clk);
1349 }
1350
1351 static void __init sunxi_init_clocks(struct device_node *np)
1352 {
1353         /* Register factor clocks */
1354         of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1355
1356         /* Register divider clocks */
1357         of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1358
1359         /* Register divided output clocks */
1360         of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1361
1362         /* Register mux clocks */
1363         of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
1364
1365         /* Register gate clocks */
1366         of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
1367
1368         /* Enable core system clocks */
1369         sunxi_clock_protect();
1370 }
1371 CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1372 CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1373 CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1374 CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1375 CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);