drivers: clk: sunxi: Fix memory leakage in clk-sunxi.c
[cascardo/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-provider.h>
18 #include <linux/clkdev.h>
19 #include <linux/clk/sunxi.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22
23 #include "clk-factors.h"
24
25 static DEFINE_SPINLOCK(clk_lock);
26
27 /**
28  * sun4i_osc_clk_setup() - Setup function for gatable oscillator
29  */
30
31 #define SUNXI_OSC24M_GATE       0
32
33 static void __init sun4i_osc_clk_setup(struct device_node *node)
34 {
35         struct clk *clk;
36         struct clk_fixed_rate *fixed;
37         struct clk_gate *gate;
38         const char *clk_name = node->name;
39         u32 rate;
40
41         if (of_property_read_u32(node, "clock-frequency", &rate))
42                 return;
43
44         /* allocate fixed-rate and gate clock structs */
45         fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
46         if (!fixed)
47                 return;
48         gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
49         if (!gate)
50                 goto err_free_fixed;
51
52         /* set up gate and fixed rate properties */
53         gate->reg = of_iomap(node, 0);
54         gate->bit_idx = SUNXI_OSC24M_GATE;
55         gate->lock = &clk_lock;
56         fixed->fixed_rate = rate;
57
58         clk = clk_register_composite(NULL, clk_name,
59                         NULL, 0,
60                         NULL, NULL,
61                         &fixed->hw, &clk_fixed_rate_ops,
62                         &gate->hw, &clk_gate_ops,
63                         CLK_IS_ROOT);
64
65         if (IS_ERR(clk))
66                 goto err_free_gate;
67
68         of_clk_add_provider(node, of_clk_src_simple_get, clk);
69         clk_register_clkdev(clk, clk_name, NULL);
70
71         return;
72
73 err_free_gate:
74         kfree(gate);
75 err_free_fixed:
76         kfree(fixed);
77 }
78 CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
79
80
81
82 /**
83  * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
84  * PLL1 rate is calculated as follows
85  * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
86  * parent_rate is always 24Mhz
87  */
88
89 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
90                                    u8 *n, u8 *k, u8 *m, u8 *p)
91 {
92         u8 div;
93
94         /* Normalize value to a 6M multiple */
95         div = *freq / 6000000;
96         *freq = 6000000 * div;
97
98         /* we were called to round the frequency, we can now return */
99         if (n == NULL)
100                 return;
101
102         /* m is always zero for pll1 */
103         *m = 0;
104
105         /* k is 1 only on these cases */
106         if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
107                 *k = 1;
108         else
109                 *k = 0;
110
111         /* p will be 3 for divs under 10 */
112         if (div < 10)
113                 *p = 3;
114
115         /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
116         else if (div < 20 || (div < 32 && (div & 1)))
117                 *p = 2;
118
119         /* p will be 1 for even divs under 32, divs under 40 and odd pairs
120          * of divs between 40-62 */
121         else if (div < 40 || (div < 64 && (div & 2)))
122                 *p = 1;
123
124         /* any other entries have p = 0 */
125         else
126                 *p = 0;
127
128         /* calculate a suitable n based on k and p */
129         div <<= *p;
130         div /= (*k + 1);
131         *n = div / 4;
132 }
133
134 /**
135  * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
136  * PLL1 rate is calculated as follows
137  * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
138  * parent_rate should always be 24MHz
139  */
140 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
141                                        u8 *n, u8 *k, u8 *m, u8 *p)
142 {
143         /*
144          * We can operate only on MHz, this will make our life easier
145          * later.
146          */
147         u32 freq_mhz = *freq / 1000000;
148         u32 parent_freq_mhz = parent_rate / 1000000;
149
150         /*
151          * Round down the frequency to the closest multiple of either
152          * 6 or 16
153          */
154         u32 round_freq_6 = round_down(freq_mhz, 6);
155         u32 round_freq_16 = round_down(freq_mhz, 16);
156
157         if (round_freq_6 > round_freq_16)
158                 freq_mhz = round_freq_6;
159         else
160                 freq_mhz = round_freq_16;
161
162         *freq = freq_mhz * 1000000;
163
164         /*
165          * If the factors pointer are null, we were just called to
166          * round down the frequency.
167          * Exit.
168          */
169         if (n == NULL)
170                 return;
171
172         /* If the frequency is a multiple of 32 MHz, k is always 3 */
173         if (!(freq_mhz % 32))
174                 *k = 3;
175         /* If the frequency is a multiple of 9 MHz, k is always 2 */
176         else if (!(freq_mhz % 9))
177                 *k = 2;
178         /* If the frequency is a multiple of 8 MHz, k is always 1 */
179         else if (!(freq_mhz % 8))
180                 *k = 1;
181         /* Otherwise, we don't use the k factor */
182         else
183                 *k = 0;
184
185         /*
186          * If the frequency is a multiple of 2 but not a multiple of
187          * 3, m is 3. This is the first time we use 6 here, yet we
188          * will use it on several other places.
189          * We use this number because it's the lowest frequency we can
190          * generate (with n = 0, k = 0, m = 3), so every other frequency
191          * somehow relates to this frequency.
192          */
193         if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
194                 *m = 2;
195         /*
196          * If the frequency is a multiple of 6MHz, but the factor is
197          * odd, m will be 3
198          */
199         else if ((freq_mhz / 6) & 1)
200                 *m = 3;
201         /* Otherwise, we end up with m = 1 */
202         else
203                 *m = 1;
204
205         /* Calculate n thanks to the above factors we already got */
206         *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
207
208         /*
209          * If n end up being outbound, and that we can still decrease
210          * m, do it.
211          */
212         if ((*n + 1) > 31 && (*m + 1) > 1) {
213                 *n = (*n + 1) / 2 - 1;
214                 *m = (*m + 1) / 2 - 1;
215         }
216 }
217
218 /**
219  * sun4i_get_apb1_factors() - calculates m, p factors for APB1
220  * APB1 rate is calculated as follows
221  * rate = (parent_rate >> p) / (m + 1);
222  */
223
224 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
225                                    u8 *n, u8 *k, u8 *m, u8 *p)
226 {
227         u8 calcm, calcp;
228
229         if (parent_rate < *freq)
230                 *freq = parent_rate;
231
232         parent_rate = (parent_rate + (*freq - 1)) / *freq;
233
234         /* Invalid rate! */
235         if (parent_rate > 32)
236                 return;
237
238         if (parent_rate <= 4)
239                 calcp = 0;
240         else if (parent_rate <= 8)
241                 calcp = 1;
242         else if (parent_rate <= 16)
243                 calcp = 2;
244         else
245                 calcp = 3;
246
247         calcm = (parent_rate >> calcp) - 1;
248
249         *freq = (parent_rate >> calcp) / (calcm + 1);
250
251         /* we were called to round the frequency, we can now return */
252         if (n == NULL)
253                 return;
254
255         *m = calcm;
256         *p = calcp;
257 }
258
259
260
261 /**
262  * sunxi_factors_clk_setup() - Setup function for factor clocks
263  */
264
265 struct factors_data {
266         struct clk_factors_config *table;
267         void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
268 };
269
270 static struct clk_factors_config sun4i_pll1_config = {
271         .nshift = 8,
272         .nwidth = 5,
273         .kshift = 4,
274         .kwidth = 2,
275         .mshift = 0,
276         .mwidth = 2,
277         .pshift = 16,
278         .pwidth = 2,
279 };
280
281 static struct clk_factors_config sun6i_a31_pll1_config = {
282         .nshift = 8,
283         .nwidth = 5,
284         .kshift = 4,
285         .kwidth = 2,
286         .mshift = 0,
287         .mwidth = 2,
288 };
289
290 static struct clk_factors_config sun4i_apb1_config = {
291         .mshift = 0,
292         .mwidth = 5,
293         .pshift = 16,
294         .pwidth = 2,
295 };
296
297 static const struct factors_data sun4i_pll1_data __initconst = {
298         .table = &sun4i_pll1_config,
299         .getter = sun4i_get_pll1_factors,
300 };
301
302 static const struct factors_data sun6i_a31_pll1_data __initconst = {
303         .table = &sun6i_a31_pll1_config,
304         .getter = sun6i_a31_get_pll1_factors,
305 };
306
307 static const struct factors_data sun4i_apb1_data __initconst = {
308         .table = &sun4i_apb1_config,
309         .getter = sun4i_get_apb1_factors,
310 };
311
312 static void __init sunxi_factors_clk_setup(struct device_node *node,
313                                            struct factors_data *data)
314 {
315         struct clk *clk;
316         const char *clk_name = node->name;
317         const char *parent;
318         void *reg;
319
320         reg = of_iomap(node, 0);
321
322         parent = of_clk_get_parent_name(node, 0);
323
324         clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
325                                    data->table, data->getter, &clk_lock);
326
327         if (!IS_ERR(clk)) {
328                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
329                 clk_register_clkdev(clk, clk_name, NULL);
330         }
331 }
332
333
334
335 /**
336  * sunxi_mux_clk_setup() - Setup function for muxes
337  */
338
339 #define SUNXI_MUX_GATE_WIDTH    2
340
341 struct mux_data {
342         u8 shift;
343 };
344
345 static const struct mux_data sun4i_cpu_mux_data __initconst = {
346         .shift = 16,
347 };
348
349 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
350         .shift = 12,
351 };
352
353 static const struct mux_data sun4i_apb1_mux_data __initconst = {
354         .shift = 24,
355 };
356
357 static void __init sunxi_mux_clk_setup(struct device_node *node,
358                                        struct mux_data *data)
359 {
360         struct clk *clk;
361         const char *clk_name = node->name;
362         const char *parents[5];
363         void *reg;
364         int i = 0;
365
366         reg = of_iomap(node, 0);
367
368         while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
369                 i++;
370
371         clk = clk_register_mux(NULL, clk_name, parents, i,
372                                CLK_SET_RATE_NO_REPARENT, reg,
373                                data->shift, SUNXI_MUX_GATE_WIDTH,
374                                0, &clk_lock);
375
376         if (clk) {
377                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
378                 clk_register_clkdev(clk, clk_name, NULL);
379         }
380 }
381
382
383
384 /**
385  * sunxi_divider_clk_setup() - Setup function for simple divider clocks
386  */
387
388 struct div_data {
389         u8      shift;
390         u8      pow;
391         u8      width;
392 };
393
394 static const struct div_data sun4i_axi_data __initconst = {
395         .shift  = 0,
396         .pow    = 0,
397         .width  = 2,
398 };
399
400 static const struct div_data sun4i_ahb_data __initconst = {
401         .shift  = 4,
402         .pow    = 1,
403         .width  = 2,
404 };
405
406 static const struct div_data sun4i_apb0_data __initconst = {
407         .shift  = 8,
408         .pow    = 1,
409         .width  = 2,
410 };
411
412 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
413         .shift  = 0,
414         .pow    = 0,
415         .width  = 4,
416 };
417
418 static void __init sunxi_divider_clk_setup(struct device_node *node,
419                                            struct div_data *data)
420 {
421         struct clk *clk;
422         const char *clk_name = node->name;
423         const char *clk_parent;
424         void *reg;
425
426         reg = of_iomap(node, 0);
427
428         clk_parent = of_clk_get_parent_name(node, 0);
429
430         clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
431                                    reg, data->shift, data->width,
432                                    data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
433                                    &clk_lock);
434         if (clk) {
435                 of_clk_add_provider(node, of_clk_src_simple_get, clk);
436                 clk_register_clkdev(clk, clk_name, NULL);
437         }
438 }
439
440
441
442 /**
443  * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
444  */
445
446 #define SUNXI_GATES_MAX_SIZE    64
447
448 struct gates_data {
449         DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
450 };
451
452 static const struct gates_data sun4i_axi_gates_data __initconst = {
453         .mask = {1},
454 };
455
456 static const struct gates_data sun4i_ahb_gates_data __initconst = {
457         .mask = {0x7F77FFF, 0x14FB3F},
458 };
459
460 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
461         .mask = {0x147667e7, 0x185915},
462 };
463
464 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
465         .mask = {0x107067e7, 0x185111},
466 };
467
468 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
469         .mask = {0xEDFE7F62, 0x794F931},
470 };
471
472 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
473         .mask = { 0x12f77fff, 0x16ff3f },
474 };
475
476 static const struct gates_data sun4i_apb0_gates_data __initconst = {
477         .mask = {0x4EF},
478 };
479
480 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
481         .mask = {0x469},
482 };
483
484 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
485         .mask = {0x61},
486 };
487
488 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
489         .mask = { 0x4ff },
490 };
491
492 static const struct gates_data sun4i_apb1_gates_data __initconst = {
493         .mask = {0xFF00F7},
494 };
495
496 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
497         .mask = {0xf0007},
498 };
499
500 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
501         .mask = {0xa0007},
502 };
503
504 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
505         .mask = {0x3031},
506 };
507
508 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
509         .mask = {0x3F000F},
510 };
511
512 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
513         .mask = { 0xff80ff },
514 };
515
516 static void __init sunxi_gates_clk_setup(struct device_node *node,
517                                          struct gates_data *data)
518 {
519         struct clk_onecell_data *clk_data;
520         const char *clk_parent;
521         const char *clk_name;
522         void *reg;
523         int qty;
524         int i = 0;
525         int j = 0;
526         int ignore;
527
528         reg = of_iomap(node, 0);
529
530         clk_parent = of_clk_get_parent_name(node, 0);
531
532         /* Worst-case size approximation and memory allocation */
533         qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
534         clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
535         if (!clk_data)
536                 return;
537         clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
538         if (!clk_data->clks) {
539                 kfree(clk_data);
540                 return;
541         }
542
543         for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
544                 of_property_read_string_index(node, "clock-output-names",
545                                               j, &clk_name);
546
547                 /* No driver claims this clock, but it should remain gated */
548                 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
549
550                 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
551                                                       clk_parent, ignore,
552                                                       reg + 4 * (i/32), i % 32,
553                                                       0, &clk_lock);
554                 WARN_ON(IS_ERR(clk_data->clks[i]));
555
556                 j++;
557         }
558
559         /* Adjust to the real max */
560         clk_data->clk_num = i;
561
562         of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
563 }
564
565 /* Matches for factors clocks */
566 static const struct of_device_id clk_factors_match[] __initconst = {
567         {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
568         {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
569         {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
570         {}
571 };
572
573 /* Matches for divider clocks */
574 static const struct of_device_id clk_div_match[] __initconst = {
575         {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
576         {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
577         {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
578         {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
579         {}
580 };
581
582 /* Matches for mux clocks */
583 static const struct of_device_id clk_mux_match[] __initconst = {
584         {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
585         {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
586         {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
587         {}
588 };
589
590 /* Matches for gate clocks */
591 static const struct of_device_id clk_gates_match[] __initconst = {
592         {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
593         {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
594         {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
595         {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
596         {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
597         {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
598         {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
599         {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
600         {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
601         {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
602         {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
603         {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
604         {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
605         {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
606         {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
607         {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
608         {}
609 };
610
611 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
612                                               void *function)
613 {
614         struct device_node *np;
615         const struct div_data *data;
616         const struct of_device_id *match;
617         void (*setup_function)(struct device_node *, const void *) = function;
618
619         for_each_matching_node(np, clk_match) {
620                 match = of_match_node(clk_match, np);
621                 data = match->data;
622                 setup_function(np, data);
623         }
624 }
625
626 /**
627  * System clock protection
628  *
629  * By enabling these critical clocks, we prevent their accidental gating
630  * by the framework
631  */
632 static void __init sunxi_clock_protect(void)
633 {
634         struct clk *clk;
635
636         /* memory bus clock - sun5i+ */
637         clk = clk_get(NULL, "mbus");
638         if (!IS_ERR(clk)) {
639                 clk_prepare_enable(clk);
640                 clk_put(clk);
641         }
642
643         /* DDR clock - sun4i+ */
644         clk = clk_get(NULL, "pll5_ddr");
645         if (!IS_ERR(clk)) {
646                 clk_prepare_enable(clk);
647                 clk_put(clk);
648         }
649 }
650
651 void __init sunxi_init_clocks(void)
652 {
653         /* Register all the simple and basic clocks on DT */
654         of_clk_init(NULL);
655
656         /* Register factor clocks */
657         of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
658
659         /* Register divider clocks */
660         of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
661
662         /* Register mux clocks */
663         of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
664
665         /* Register gate clocks */
666         of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
667
668         /* Enable core system clocks */
669         sunxi_clock_protect();
670 }