xref: /openbmc/linux/drivers/clk/sunxi/clk-sunxi.c (revision 5f32c314)
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 
22 #include "clk-factors.h"
23 
24 static DEFINE_SPINLOCK(clk_lock);
25 
26 /* Maximum number of parents our clocks have */
27 #define SUNXI_MAX_PARENTS	5
28 
29 /**
30  * sun4i_osc_clk_setup() - Setup function for gatable oscillator
31  */
32 
33 #define SUNXI_OSC24M_GATE	0
34 
35 static void __init sun4i_osc_clk_setup(struct device_node *node)
36 {
37 	struct clk *clk;
38 	struct clk_fixed_rate *fixed;
39 	struct clk_gate *gate;
40 	const char *clk_name = node->name;
41 	u32 rate;
42 
43 	if (of_property_read_u32(node, "clock-frequency", &rate))
44 		return;
45 
46 	/* allocate fixed-rate and gate clock structs */
47 	fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
48 	if (!fixed)
49 		return;
50 	gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
51 	if (!gate)
52 		goto err_free_fixed;
53 
54 	/* set up gate and fixed rate properties */
55 	gate->reg = of_iomap(node, 0);
56 	gate->bit_idx = SUNXI_OSC24M_GATE;
57 	gate->lock = &clk_lock;
58 	fixed->fixed_rate = rate;
59 
60 	clk = clk_register_composite(NULL, clk_name,
61 			NULL, 0,
62 			NULL, NULL,
63 			&fixed->hw, &clk_fixed_rate_ops,
64 			&gate->hw, &clk_gate_ops,
65 			CLK_IS_ROOT);
66 
67 	if (IS_ERR(clk))
68 		goto err_free_gate;
69 
70 	of_clk_add_provider(node, of_clk_src_simple_get, clk);
71 	clk_register_clkdev(clk, clk_name, NULL);
72 
73 	return;
74 
75 err_free_gate:
76 	kfree(gate);
77 err_free_fixed:
78 	kfree(fixed);
79 }
80 CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
81 
82 
83 
84 /**
85  * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
86  * PLL1 rate is calculated as follows
87  * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
88  * parent_rate is always 24Mhz
89  */
90 
91 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
92 				   u8 *n, u8 *k, u8 *m, u8 *p)
93 {
94 	u8 div;
95 
96 	/* Normalize value to a 6M multiple */
97 	div = *freq / 6000000;
98 	*freq = 6000000 * div;
99 
100 	/* we were called to round the frequency, we can now return */
101 	if (n == NULL)
102 		return;
103 
104 	/* m is always zero for pll1 */
105 	*m = 0;
106 
107 	/* k is 1 only on these cases */
108 	if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
109 		*k = 1;
110 	else
111 		*k = 0;
112 
113 	/* p will be 3 for divs under 10 */
114 	if (div < 10)
115 		*p = 3;
116 
117 	/* p will be 2 for divs between 10 - 20 and odd divs under 32 */
118 	else if (div < 20 || (div < 32 && (div & 1)))
119 		*p = 2;
120 
121 	/* p will be 1 for even divs under 32, divs under 40 and odd pairs
122 	 * of divs between 40-62 */
123 	else if (div < 40 || (div < 64 && (div & 2)))
124 		*p = 1;
125 
126 	/* any other entries have p = 0 */
127 	else
128 		*p = 0;
129 
130 	/* calculate a suitable n based on k and p */
131 	div <<= *p;
132 	div /= (*k + 1);
133 	*n = div / 4;
134 }
135 
136 /**
137  * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
138  * PLL1 rate is calculated as follows
139  * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
140  * parent_rate should always be 24MHz
141  */
142 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
143 				       u8 *n, u8 *k, u8 *m, u8 *p)
144 {
145 	/*
146 	 * We can operate only on MHz, this will make our life easier
147 	 * later.
148 	 */
149 	u32 freq_mhz = *freq / 1000000;
150 	u32 parent_freq_mhz = parent_rate / 1000000;
151 
152 	/*
153 	 * Round down the frequency to the closest multiple of either
154 	 * 6 or 16
155 	 */
156 	u32 round_freq_6 = round_down(freq_mhz, 6);
157 	u32 round_freq_16 = round_down(freq_mhz, 16);
158 
159 	if (round_freq_6 > round_freq_16)
160 		freq_mhz = round_freq_6;
161 	else
162 		freq_mhz = round_freq_16;
163 
164 	*freq = freq_mhz * 1000000;
165 
166 	/*
167 	 * If the factors pointer are null, we were just called to
168 	 * round down the frequency.
169 	 * Exit.
170 	 */
171 	if (n == NULL)
172 		return;
173 
174 	/* If the frequency is a multiple of 32 MHz, k is always 3 */
175 	if (!(freq_mhz % 32))
176 		*k = 3;
177 	/* If the frequency is a multiple of 9 MHz, k is always 2 */
178 	else if (!(freq_mhz % 9))
179 		*k = 2;
180 	/* If the frequency is a multiple of 8 MHz, k is always 1 */
181 	else if (!(freq_mhz % 8))
182 		*k = 1;
183 	/* Otherwise, we don't use the k factor */
184 	else
185 		*k = 0;
186 
187 	/*
188 	 * If the frequency is a multiple of 2 but not a multiple of
189 	 * 3, m is 3. This is the first time we use 6 here, yet we
190 	 * will use it on several other places.
191 	 * We use this number because it's the lowest frequency we can
192 	 * generate (with n = 0, k = 0, m = 3), so every other frequency
193 	 * somehow relates to this frequency.
194 	 */
195 	if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
196 		*m = 2;
197 	/*
198 	 * If the frequency is a multiple of 6MHz, but the factor is
199 	 * odd, m will be 3
200 	 */
201 	else if ((freq_mhz / 6) & 1)
202 		*m = 3;
203 	/* Otherwise, we end up with m = 1 */
204 	else
205 		*m = 1;
206 
207 	/* Calculate n thanks to the above factors we already got */
208 	*n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
209 
210 	/*
211 	 * If n end up being outbound, and that we can still decrease
212 	 * m, do it.
213 	 */
214 	if ((*n + 1) > 31 && (*m + 1) > 1) {
215 		*n = (*n + 1) / 2 - 1;
216 		*m = (*m + 1) / 2 - 1;
217 	}
218 }
219 
220 /**
221  * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
222  * PLL5 rate is calculated as follows
223  * rate = parent_rate * n * (k + 1)
224  * parent_rate is always 24Mhz
225  */
226 
227 static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
228 				   u8 *n, u8 *k, u8 *m, u8 *p)
229 {
230 	u8 div;
231 
232 	/* Normalize value to a parent_rate multiple (24M) */
233 	div = *freq / parent_rate;
234 	*freq = parent_rate * div;
235 
236 	/* we were called to round the frequency, we can now return */
237 	if (n == NULL)
238 		return;
239 
240 	if (div < 31)
241 		*k = 0;
242 	else if (div / 2 < 31)
243 		*k = 1;
244 	else if (div / 3 < 31)
245 		*k = 2;
246 	else
247 		*k = 3;
248 
249 	*n = DIV_ROUND_UP(div, (*k+1));
250 }
251 
252 
253 
254 /**
255  * sun4i_get_apb1_factors() - calculates m, p factors for APB1
256  * APB1 rate is calculated as follows
257  * rate = (parent_rate >> p) / (m + 1);
258  */
259 
260 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
261 				   u8 *n, u8 *k, u8 *m, u8 *p)
262 {
263 	u8 calcm, calcp;
264 
265 	if (parent_rate < *freq)
266 		*freq = parent_rate;
267 
268 	parent_rate = (parent_rate + (*freq - 1)) / *freq;
269 
270 	/* Invalid rate! */
271 	if (parent_rate > 32)
272 		return;
273 
274 	if (parent_rate <= 4)
275 		calcp = 0;
276 	else if (parent_rate <= 8)
277 		calcp = 1;
278 	else if (parent_rate <= 16)
279 		calcp = 2;
280 	else
281 		calcp = 3;
282 
283 	calcm = (parent_rate >> calcp) - 1;
284 
285 	*freq = (parent_rate >> calcp) / (calcm + 1);
286 
287 	/* we were called to round the frequency, we can now return */
288 	if (n == NULL)
289 		return;
290 
291 	*m = calcm;
292 	*p = calcp;
293 }
294 
295 
296 
297 /**
298  * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
299  * MMC rate is calculated as follows
300  * rate = (parent_rate >> p) / (m + 1);
301  */
302 
303 static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
304 				   u8 *n, u8 *k, u8 *m, u8 *p)
305 {
306 	u8 div, calcm, calcp;
307 
308 	/* These clocks can only divide, so we will never be able to achieve
309 	 * frequencies higher than the parent frequency */
310 	if (*freq > parent_rate)
311 		*freq = parent_rate;
312 
313 	div = parent_rate / *freq;
314 
315 	if (div < 16)
316 		calcp = 0;
317 	else if (div / 2 < 16)
318 		calcp = 1;
319 	else if (div / 4 < 16)
320 		calcp = 2;
321 	else
322 		calcp = 3;
323 
324 	calcm = DIV_ROUND_UP(div, 1 << calcp);
325 
326 	*freq = (parent_rate >> calcp) / calcm;
327 
328 	/* we were called to round the frequency, we can now return */
329 	if (n == NULL)
330 		return;
331 
332 	*m = calcm - 1;
333 	*p = calcp;
334 }
335 
336 
337 
338 /**
339  * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
340  * CLK_OUT rate is calculated as follows
341  * rate = (parent_rate >> p) / (m + 1);
342  */
343 
344 static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
345 				      u8 *n, u8 *k, u8 *m, u8 *p)
346 {
347 	u8 div, calcm, calcp;
348 
349 	/* These clocks can only divide, so we will never be able to achieve
350 	 * frequencies higher than the parent frequency */
351 	if (*freq > parent_rate)
352 		*freq = parent_rate;
353 
354 	div = parent_rate / *freq;
355 
356 	if (div < 32)
357 		calcp = 0;
358 	else if (div / 2 < 32)
359 		calcp = 1;
360 	else if (div / 4 < 32)
361 		calcp = 2;
362 	else
363 		calcp = 3;
364 
365 	calcm = DIV_ROUND_UP(div, 1 << calcp);
366 
367 	*freq = (parent_rate >> calcp) / calcm;
368 
369 	/* we were called to round the frequency, we can now return */
370 	if (n == NULL)
371 		return;
372 
373 	*m = calcm - 1;
374 	*p = calcp;
375 }
376 
377 
378 
379 /**
380  * sunxi_factors_clk_setup() - Setup function for factor clocks
381  */
382 
383 #define SUNXI_FACTORS_MUX_MASK 0x3
384 
385 struct factors_data {
386 	int enable;
387 	int mux;
388 	struct clk_factors_config *table;
389 	void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
390 };
391 
392 static struct clk_factors_config sun4i_pll1_config = {
393 	.nshift = 8,
394 	.nwidth = 5,
395 	.kshift = 4,
396 	.kwidth = 2,
397 	.mshift = 0,
398 	.mwidth = 2,
399 	.pshift = 16,
400 	.pwidth = 2,
401 };
402 
403 static struct clk_factors_config sun6i_a31_pll1_config = {
404 	.nshift	= 8,
405 	.nwidth = 5,
406 	.kshift = 4,
407 	.kwidth = 2,
408 	.mshift = 0,
409 	.mwidth = 2,
410 };
411 
412 static struct clk_factors_config sun4i_pll5_config = {
413 	.nshift = 8,
414 	.nwidth = 5,
415 	.kshift = 4,
416 	.kwidth = 2,
417 };
418 
419 static struct clk_factors_config sun4i_apb1_config = {
420 	.mshift = 0,
421 	.mwidth = 5,
422 	.pshift = 16,
423 	.pwidth = 2,
424 };
425 
426 /* user manual says "n" but it's really "p" */
427 static struct clk_factors_config sun4i_mod0_config = {
428 	.mshift = 0,
429 	.mwidth = 4,
430 	.pshift = 16,
431 	.pwidth = 2,
432 };
433 
434 /* user manual says "n" but it's really "p" */
435 static struct clk_factors_config sun7i_a20_out_config = {
436 	.mshift = 8,
437 	.mwidth = 5,
438 	.pshift = 20,
439 	.pwidth = 2,
440 };
441 
442 static const struct factors_data sun4i_pll1_data __initconst = {
443 	.enable = 31,
444 	.table = &sun4i_pll1_config,
445 	.getter = sun4i_get_pll1_factors,
446 };
447 
448 static const struct factors_data sun6i_a31_pll1_data __initconst = {
449 	.enable = 31,
450 	.table = &sun6i_a31_pll1_config,
451 	.getter = sun6i_a31_get_pll1_factors,
452 };
453 
454 static const struct factors_data sun4i_pll5_data __initconst = {
455 	.enable = 31,
456 	.table = &sun4i_pll5_config,
457 	.getter = sun4i_get_pll5_factors,
458 };
459 
460 static const struct factors_data sun4i_apb1_data __initconst = {
461 	.table = &sun4i_apb1_config,
462 	.getter = sun4i_get_apb1_factors,
463 };
464 
465 static const struct factors_data sun4i_mod0_data __initconst = {
466 	.enable = 31,
467 	.mux = 24,
468 	.table = &sun4i_mod0_config,
469 	.getter = sun4i_get_mod0_factors,
470 };
471 
472 static const struct factors_data sun7i_a20_out_data __initconst = {
473 	.enable = 31,
474 	.mux = 24,
475 	.table = &sun7i_a20_out_config,
476 	.getter = sun7i_a20_get_out_factors,
477 };
478 
479 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
480 						const struct factors_data *data)
481 {
482 	struct clk *clk;
483 	struct clk_factors *factors;
484 	struct clk_gate *gate = NULL;
485 	struct clk_mux *mux = NULL;
486 	struct clk_hw *gate_hw = NULL;
487 	struct clk_hw *mux_hw = NULL;
488 	const char *clk_name = node->name;
489 	const char *parents[SUNXI_MAX_PARENTS];
490 	void *reg;
491 	int i = 0;
492 
493 	reg = of_iomap(node, 0);
494 
495 	/* if we have a mux, we will have >1 parents */
496 	while (i < SUNXI_MAX_PARENTS &&
497 	       (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
498 		i++;
499 
500 	/* Nodes should be providing the name via clock-output-names
501 	 * but originally our dts didn't, and so we used node->name.
502 	 * The new, better nodes look like clk@deadbeef, so we pull the
503 	 * name just in this case */
504 	if (!strcmp("clk", clk_name)) {
505 		of_property_read_string_index(node, "clock-output-names",
506 					      0, &clk_name);
507 	}
508 
509 	factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
510 	if (!factors)
511 		return NULL;
512 
513 	/* Add a gate if this factor clock can be gated */
514 	if (data->enable) {
515 		gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
516 		if (!gate) {
517 			kfree(factors);
518 			return NULL;
519 		}
520 
521 		/* set up gate properties */
522 		gate->reg = reg;
523 		gate->bit_idx = data->enable;
524 		gate->lock = &clk_lock;
525 		gate_hw = &gate->hw;
526 	}
527 
528 	/* Add a mux if this factor clock can be muxed */
529 	if (data->mux) {
530 		mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
531 		if (!mux) {
532 			kfree(factors);
533 			kfree(gate);
534 			return NULL;
535 		}
536 
537 		/* set up gate properties */
538 		mux->reg = reg;
539 		mux->shift = data->mux;
540 		mux->mask = SUNXI_FACTORS_MUX_MASK;
541 		mux->lock = &clk_lock;
542 		mux_hw = &mux->hw;
543 	}
544 
545 	/* set up factors properties */
546 	factors->reg = reg;
547 	factors->config = data->table;
548 	factors->get_factors = data->getter;
549 	factors->lock = &clk_lock;
550 
551 	clk = clk_register_composite(NULL, clk_name,
552 			parents, i,
553 			mux_hw, &clk_mux_ops,
554 			&factors->hw, &clk_factors_ops,
555 			gate_hw, &clk_gate_ops, 0);
556 
557 	if (!IS_ERR(clk)) {
558 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
559 		clk_register_clkdev(clk, clk_name, NULL);
560 	}
561 
562 	return clk;
563 }
564 
565 
566 
567 /**
568  * sunxi_mux_clk_setup() - Setup function for muxes
569  */
570 
571 #define SUNXI_MUX_GATE_WIDTH	2
572 
573 struct mux_data {
574 	u8 shift;
575 };
576 
577 static const struct mux_data sun4i_cpu_mux_data __initconst = {
578 	.shift = 16,
579 };
580 
581 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
582 	.shift = 12,
583 };
584 
585 static const struct mux_data sun4i_apb1_mux_data __initconst = {
586 	.shift = 24,
587 };
588 
589 static void __init sunxi_mux_clk_setup(struct device_node *node,
590 				       struct mux_data *data)
591 {
592 	struct clk *clk;
593 	const char *clk_name = node->name;
594 	const char *parents[SUNXI_MAX_PARENTS];
595 	void *reg;
596 	int i = 0;
597 
598 	reg = of_iomap(node, 0);
599 
600 	while (i < SUNXI_MAX_PARENTS &&
601 	       (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
602 		i++;
603 
604 	clk = clk_register_mux(NULL, clk_name, parents, i,
605 			       CLK_SET_RATE_NO_REPARENT, reg,
606 			       data->shift, SUNXI_MUX_GATE_WIDTH,
607 			       0, &clk_lock);
608 
609 	if (clk) {
610 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
611 		clk_register_clkdev(clk, clk_name, NULL);
612 	}
613 }
614 
615 
616 
617 /**
618  * sunxi_divider_clk_setup() - Setup function for simple divider clocks
619  */
620 
621 struct div_data {
622 	u8	shift;
623 	u8	pow;
624 	u8	width;
625 };
626 
627 static const struct div_data sun4i_axi_data __initconst = {
628 	.shift	= 0,
629 	.pow	= 0,
630 	.width	= 2,
631 };
632 
633 static const struct div_data sun4i_ahb_data __initconst = {
634 	.shift	= 4,
635 	.pow	= 1,
636 	.width	= 2,
637 };
638 
639 static const struct div_data sun4i_apb0_data __initconst = {
640 	.shift	= 8,
641 	.pow	= 1,
642 	.width	= 2,
643 };
644 
645 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
646 	.shift	= 0,
647 	.pow	= 0,
648 	.width	= 4,
649 };
650 
651 static void __init sunxi_divider_clk_setup(struct device_node *node,
652 					   struct div_data *data)
653 {
654 	struct clk *clk;
655 	const char *clk_name = node->name;
656 	const char *clk_parent;
657 	void *reg;
658 
659 	reg = of_iomap(node, 0);
660 
661 	clk_parent = of_clk_get_parent_name(node, 0);
662 
663 	clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
664 				   reg, data->shift, data->width,
665 				   data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
666 				   &clk_lock);
667 	if (clk) {
668 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
669 		clk_register_clkdev(clk, clk_name, NULL);
670 	}
671 }
672 
673 
674 
675 /**
676  * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
677  */
678 
679 #define SUNXI_GATES_MAX_SIZE	64
680 
681 struct gates_data {
682 	DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
683 };
684 
685 static const struct gates_data sun4i_axi_gates_data __initconst = {
686 	.mask = {1},
687 };
688 
689 static const struct gates_data sun4i_ahb_gates_data __initconst = {
690 	.mask = {0x7F77FFF, 0x14FB3F},
691 };
692 
693 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
694 	.mask = {0x147667e7, 0x185915},
695 };
696 
697 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
698 	.mask = {0x107067e7, 0x185111},
699 };
700 
701 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
702 	.mask = {0xEDFE7F62, 0x794F931},
703 };
704 
705 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
706 	.mask = { 0x12f77fff, 0x16ff3f },
707 };
708 
709 static const struct gates_data sun4i_apb0_gates_data __initconst = {
710 	.mask = {0x4EF},
711 };
712 
713 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
714 	.mask = {0x469},
715 };
716 
717 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
718 	.mask = {0x61},
719 };
720 
721 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
722 	.mask = { 0x4ff },
723 };
724 
725 static const struct gates_data sun4i_apb1_gates_data __initconst = {
726 	.mask = {0xFF00F7},
727 };
728 
729 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
730 	.mask = {0xf0007},
731 };
732 
733 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
734 	.mask = {0xa0007},
735 };
736 
737 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
738 	.mask = {0x3031},
739 };
740 
741 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
742 	.mask = {0x3F000F},
743 };
744 
745 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
746 	.mask = { 0xff80ff },
747 };
748 
749 static void __init sunxi_gates_clk_setup(struct device_node *node,
750 					 struct gates_data *data)
751 {
752 	struct clk_onecell_data *clk_data;
753 	const char *clk_parent;
754 	const char *clk_name;
755 	void *reg;
756 	int qty;
757 	int i = 0;
758 	int j = 0;
759 	int ignore;
760 
761 	reg = of_iomap(node, 0);
762 
763 	clk_parent = of_clk_get_parent_name(node, 0);
764 
765 	/* Worst-case size approximation and memory allocation */
766 	qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
767 	clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
768 	if (!clk_data)
769 		return;
770 	clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
771 	if (!clk_data->clks) {
772 		kfree(clk_data);
773 		return;
774 	}
775 
776 	for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
777 		of_property_read_string_index(node, "clock-output-names",
778 					      j, &clk_name);
779 
780 		/* No driver claims this clock, but it should remain gated */
781 		ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
782 
783 		clk_data->clks[i] = clk_register_gate(NULL, clk_name,
784 						      clk_parent, ignore,
785 						      reg + 4 * (i/32), i % 32,
786 						      0, &clk_lock);
787 		WARN_ON(IS_ERR(clk_data->clks[i]));
788 
789 		j++;
790 	}
791 
792 	/* Adjust to the real max */
793 	clk_data->clk_num = i;
794 
795 	of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
796 }
797 
798 
799 
800 /**
801  * sunxi_divs_clk_setup() helper data
802  */
803 
804 #define SUNXI_DIVS_MAX_QTY	2
805 #define SUNXI_DIVISOR_WIDTH	2
806 
807 struct divs_data {
808 	const struct factors_data *factors; /* data for the factor clock */
809 	struct {
810 		u8 fixed; /* is it a fixed divisor? if not... */
811 		struct clk_div_table *table; /* is it a table based divisor? */
812 		u8 shift; /* otherwise it's a normal divisor with this shift */
813 		u8 pow;   /* is it power-of-two based? */
814 		u8 gate;  /* is it independently gateable? */
815 	} div[SUNXI_DIVS_MAX_QTY];
816 };
817 
818 static struct clk_div_table pll6_sata_tbl[] = {
819 	{ .val = 0, .div = 6, },
820 	{ .val = 1, .div = 12, },
821 	{ .val = 2, .div = 18, },
822 	{ .val = 3, .div = 24, },
823 	{ } /* sentinel */
824 };
825 
826 static const struct divs_data pll5_divs_data __initconst = {
827 	.factors = &sun4i_pll5_data,
828 	.div = {
829 		{ .shift = 0, .pow = 0, }, /* M, DDR */
830 		{ .shift = 16, .pow = 1, }, /* P, other */
831 	}
832 };
833 
834 static const struct divs_data pll6_divs_data __initconst = {
835 	.factors = &sun4i_pll5_data,
836 	.div = {
837 		{ .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
838 		{ .fixed = 2 }, /* P, other */
839 	}
840 };
841 
842 /**
843  * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
844  *
845  * These clocks look something like this
846  *            ________________________
847  *           |         ___divisor 1---|----> to consumer
848  * parent >--|  pll___/___divisor 2---|----> to consumer
849  *           |        \_______________|____> to consumer
850  *           |________________________|
851  */
852 
853 static void __init sunxi_divs_clk_setup(struct device_node *node,
854 					struct divs_data *data)
855 {
856 	struct clk_onecell_data *clk_data;
857 	const char *parent  = node->name;
858 	const char *clk_name;
859 	struct clk **clks, *pclk;
860 	struct clk_hw *gate_hw, *rate_hw;
861 	const struct clk_ops *rate_ops;
862 	struct clk_gate *gate = NULL;
863 	struct clk_fixed_factor *fix_factor;
864 	struct clk_divider *divider;
865 	void *reg;
866 	int i = 0;
867 	int flags, clkflags;
868 
869 	/* Set up factor clock that we will be dividing */
870 	pclk = sunxi_factors_clk_setup(node, data->factors);
871 
872 	reg = of_iomap(node, 0);
873 
874 	clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
875 	if (!clk_data)
876 		return;
877 
878 	clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
879 	if (!clks)
880 		goto free_clkdata;
881 
882 	clk_data->clks = clks;
883 
884 	/* It's not a good idea to have automatic reparenting changing
885 	 * our RAM clock! */
886 	clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
887 
888 	for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
889 		if (of_property_read_string_index(node, "clock-output-names",
890 						  i, &clk_name) != 0)
891 			break;
892 
893 		gate_hw = NULL;
894 		rate_hw = NULL;
895 		rate_ops = NULL;
896 
897 		/* If this leaf clock can be gated, create a gate */
898 		if (data->div[i].gate) {
899 			gate = kzalloc(sizeof(*gate), GFP_KERNEL);
900 			if (!gate)
901 				goto free_clks;
902 
903 			gate->reg = reg;
904 			gate->bit_idx = data->div[i].gate;
905 			gate->lock = &clk_lock;
906 
907 			gate_hw = &gate->hw;
908 		}
909 
910 		/* Leaves can be fixed or configurable divisors */
911 		if (data->div[i].fixed) {
912 			fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
913 			if (!fix_factor)
914 				goto free_gate;
915 
916 			fix_factor->mult = 1;
917 			fix_factor->div = data->div[i].fixed;
918 
919 			rate_hw = &fix_factor->hw;
920 			rate_ops = &clk_fixed_factor_ops;
921 		} else {
922 			divider = kzalloc(sizeof(*divider), GFP_KERNEL);
923 			if (!divider)
924 				goto free_gate;
925 
926 			flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
927 
928 			divider->reg = reg;
929 			divider->shift = data->div[i].shift;
930 			divider->width = SUNXI_DIVISOR_WIDTH;
931 			divider->flags = flags;
932 			divider->lock = &clk_lock;
933 			divider->table = data->div[i].table;
934 
935 			rate_hw = &divider->hw;
936 			rate_ops = &clk_divider_ops;
937 		}
938 
939 		/* Wrap the (potential) gate and the divisor on a composite
940 		 * clock to unify them */
941 		clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
942 						 NULL, NULL,
943 						 rate_hw, rate_ops,
944 						 gate_hw, &clk_gate_ops,
945 						 clkflags);
946 
947 		WARN_ON(IS_ERR(clk_data->clks[i]));
948 		clk_register_clkdev(clks[i], clk_name, NULL);
949 	}
950 
951 	/* The last clock available on the getter is the parent */
952 	clks[i++] = pclk;
953 
954 	/* Adjust to the real max */
955 	clk_data->clk_num = i;
956 
957 	of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
958 
959 	return;
960 
961 free_gate:
962 	kfree(gate);
963 free_clks:
964 	kfree(clks);
965 free_clkdata:
966 	kfree(clk_data);
967 }
968 
969 
970 
971 /* Matches for factors clocks */
972 static const struct of_device_id clk_factors_match[] __initconst = {
973 	{.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
974 	{.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
975 	{.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
976 	{.compatible = "allwinner,sun4i-mod0-clk", .data = &sun4i_mod0_data,},
977 	{.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
978 	{}
979 };
980 
981 /* Matches for divider clocks */
982 static const struct of_device_id clk_div_match[] __initconst = {
983 	{.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
984 	{.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
985 	{.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
986 	{.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
987 	{}
988 };
989 
990 /* Matches for divided outputs */
991 static const struct of_device_id clk_divs_match[] __initconst = {
992 	{.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
993 	{.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
994 	{}
995 };
996 
997 /* Matches for mux clocks */
998 static const struct of_device_id clk_mux_match[] __initconst = {
999 	{.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
1000 	{.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
1001 	{.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
1002 	{}
1003 };
1004 
1005 /* Matches for gate clocks */
1006 static const struct of_device_id clk_gates_match[] __initconst = {
1007 	{.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1008 	{.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
1009 	{.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
1010 	{.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
1011 	{.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
1012 	{.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
1013 	{.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
1014 	{.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
1015 	{.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
1016 	{.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
1017 	{.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
1018 	{.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
1019 	{.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
1020 	{.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
1021 	{.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
1022 	{.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
1023 	{}
1024 };
1025 
1026 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1027 					      void *function)
1028 {
1029 	struct device_node *np;
1030 	const struct div_data *data;
1031 	const struct of_device_id *match;
1032 	void (*setup_function)(struct device_node *, const void *) = function;
1033 
1034 	for_each_matching_node(np, clk_match) {
1035 		match = of_match_node(clk_match, np);
1036 		data = match->data;
1037 		setup_function(np, data);
1038 	}
1039 }
1040 
1041 /**
1042  * System clock protection
1043  *
1044  * By enabling these critical clocks, we prevent their accidental gating
1045  * by the framework
1046  */
1047 static void __init sunxi_clock_protect(void)
1048 {
1049 	struct clk *clk;
1050 
1051 	/* memory bus clock - sun5i+ */
1052 	clk = clk_get(NULL, "mbus");
1053 	if (!IS_ERR(clk)) {
1054 		clk_prepare_enable(clk);
1055 		clk_put(clk);
1056 	}
1057 
1058 	/* DDR clock - sun4i+ */
1059 	clk = clk_get(NULL, "pll5_ddr");
1060 	if (!IS_ERR(clk)) {
1061 		clk_prepare_enable(clk);
1062 		clk_put(clk);
1063 	}
1064 }
1065 
1066 static void __init sunxi_init_clocks(void)
1067 {
1068 	/* Register factor clocks */
1069 	of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1070 
1071 	/* Register divider clocks */
1072 	of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1073 
1074 	/* Register divided output clocks */
1075 	of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1076 
1077 	/* Register mux clocks */
1078 	of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
1079 
1080 	/* Register gate clocks */
1081 	of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
1082 
1083 	/* Enable core system clocks */
1084 	sunxi_clock_protect();
1085 }
1086 CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1087 CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1088 CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1089 CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1090 CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);
1091