2 * Copyright 2013 Emilio López
4 * Emilio López <emilio@elopez.com.ar>
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.
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.
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/clkdev.h>
22 #include <linux/of_address.h>
23 #include <linux/reset-controller.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/log2.h>
28 #include "clk-factors.h"
30 static DEFINE_SPINLOCK(clk_lock);
32 /* Maximum number of parents our clocks have */
33 #define SUNXI_MAX_PARENTS 5
36 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
37 * PLL1 rate is calculated as follows
38 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
39 * parent_rate is always 24Mhz
42 static void sun4i_get_pll1_factors(struct factors_request *req)
46 /* Normalize value to a 6M multiple */
47 div = req->rate / 6000000;
48 req->rate = 6000000 * div;
50 /* m is always zero for pll1 */
53 /* k is 1 only on these cases */
54 if (req->rate >= 768000000 || req->rate == 42000000 ||
55 req->rate == 54000000)
60 /* p will be 3 for divs under 10 */
64 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
65 else if (div < 20 || (div < 32 && (div & 1)))
68 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
69 * of divs between 40-62 */
70 else if (div < 40 || (div < 64 && (div & 2)))
73 /* any other entries have p = 0 */
77 /* calculate a suitable n based on k and p */
84 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
85 * PLL1 rate is calculated as follows
86 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
87 * parent_rate should always be 24MHz
89 static void sun6i_a31_get_pll1_factors(struct factors_request *req)
92 * We can operate only on MHz, this will make our life easier
95 u32 freq_mhz = req->rate / 1000000;
96 u32 parent_freq_mhz = req->parent_rate / 1000000;
99 * Round down the frequency to the closest multiple of either
102 u32 round_freq_6 = round_down(freq_mhz, 6);
103 u32 round_freq_16 = round_down(freq_mhz, 16);
105 if (round_freq_6 > round_freq_16)
106 freq_mhz = round_freq_6;
108 freq_mhz = round_freq_16;
110 req->rate = freq_mhz * 1000000;
112 /* If the frequency is a multiple of 32 MHz, k is always 3 */
113 if (!(freq_mhz % 32))
115 /* If the frequency is a multiple of 9 MHz, k is always 2 */
116 else if (!(freq_mhz % 9))
118 /* If the frequency is a multiple of 8 MHz, k is always 1 */
119 else if (!(freq_mhz % 8))
121 /* Otherwise, we don't use the k factor */
126 * If the frequency is a multiple of 2 but not a multiple of
127 * 3, m is 3. This is the first time we use 6 here, yet we
128 * will use it on several other places.
129 * We use this number because it's the lowest frequency we can
130 * generate (with n = 0, k = 0, m = 3), so every other frequency
131 * somehow relates to this frequency.
133 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
136 * If the frequency is a multiple of 6MHz, but the factor is
139 else if ((freq_mhz / 6) & 1)
141 /* Otherwise, we end up with m = 1 */
145 /* Calculate n thanks to the above factors we already got */
146 req->n = freq_mhz * (req->m + 1) / ((req->k + 1) * parent_freq_mhz)
150 * If n end up being outbound, and that we can still decrease
153 if ((req->n + 1) > 31 && (req->m + 1) > 1) {
154 req->n = (req->n + 1) / 2 - 1;
155 req->m = (req->m + 1) / 2 - 1;
160 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
161 * PLL1 rate is calculated as follows
162 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
163 * parent_rate is always 24Mhz
166 static void sun8i_a23_get_pll1_factors(struct factors_request *req)
170 /* Normalize value to a 6M multiple */
171 div = req->rate / 6000000;
172 req->rate = 6000000 * div;
174 /* m is always zero for pll1 */
177 /* k is 1 only on these cases */
178 if (req->rate >= 768000000 || req->rate == 42000000 ||
179 req->rate == 54000000)
184 /* p will be 2 for divs under 20 and odd divs under 32 */
185 if (div < 20 || (div < 32 && (div & 1)))
188 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
189 * of divs between 40-62 */
190 else if (div < 40 || (div < 64 && (div & 2)))
193 /* any other entries have p = 0 */
197 /* calculate a suitable n based on k and p */
200 req->n = div / 4 - 1;
204 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
205 * PLL5 rate is calculated as follows
206 * rate = parent_rate * n * (k + 1)
207 * parent_rate is always 24Mhz
210 static void sun4i_get_pll5_factors(struct factors_request *req)
214 /* Normalize value to a parent_rate multiple (24M) */
215 div = req->rate / req->parent_rate;
216 req->rate = req->parent_rate * div;
220 else if (div / 2 < 31)
222 else if (div / 3 < 31)
227 req->n = DIV_ROUND_UP(div, (req->k + 1));
231 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
232 * PLL6x2 rate is calculated as follows
233 * rate = parent_rate * (n + 1) * (k + 1)
234 * parent_rate is always 24Mhz
237 static void sun6i_a31_get_pll6_factors(struct factors_request *req)
241 /* Normalize value to a parent_rate multiple (24M) */
242 div = req->rate / req->parent_rate;
243 req->rate = req->parent_rate * div;
249 req->n = DIV_ROUND_UP(div, (req->k + 1)) - 1;
253 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
254 * AHB rate is calculated as follows
255 * rate = parent_rate >> p
258 static void sun5i_a13_get_ahb_factors(struct factors_request *req)
263 if (req->parent_rate < req->rate)
264 req->rate = req->parent_rate;
267 * user manual says valid speed is 8k ~ 276M, but tests show it
268 * can work at speeds up to 300M, just after reparenting to pll6
270 if (req->rate < 8000)
272 if (req->rate > 300000000)
273 req->rate = 300000000;
275 div = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
281 req->rate = req->parent_rate >> div;
286 #define SUN6I_AHB1_PARENT_PLL6 3
289 * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
290 * AHB rate is calculated as follows
291 * rate = parent_rate >> p
293 * if parent is pll6, then
294 * parent_rate = pll6 rate / (m + 1)
297 static void sun6i_get_ahb1_factors(struct factors_request *req)
299 u8 div, calcp, calcm = 1;
302 * clock can only divide, so we will never be able to achieve
303 * frequencies higher than the parent frequency
305 if (req->parent_rate && req->rate > req->parent_rate)
306 req->rate = req->parent_rate;
308 div = DIV_ROUND_UP(req->parent_rate, req->rate);
310 /* calculate pre-divider if parent is pll6 */
311 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6) {
314 else if (div / 2 < 4)
316 else if (div / 4 < 4)
321 calcm = DIV_ROUND_UP(div, 1 << calcp);
323 calcp = __roundup_pow_of_two(div);
324 calcp = calcp > 3 ? 3 : calcp;
327 req->rate = (req->parent_rate / calcm) >> calcp;
333 * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
336 static void sun6i_ahb1_recalc(struct factors_request *req)
338 req->rate = req->parent_rate;
340 /* apply pre-divider first if parent is pll6 */
341 if (req->parent_index == SUN6I_AHB1_PARENT_PLL6)
342 req->rate /= req->m + 1;
345 req->rate >>= req->p;
349 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
350 * APB1 rate is calculated as follows
351 * rate = (parent_rate >> p) / (m + 1);
354 static void sun4i_get_apb1_factors(struct factors_request *req)
359 if (req->parent_rate < req->rate)
360 req->rate = req->parent_rate;
362 div = DIV_ROUND_UP(req->parent_rate, req->rate);
377 calcm = (div >> calcp) - 1;
379 req->rate = (req->parent_rate >> calcp) / (calcm + 1);
388 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
389 * CLK_OUT rate is calculated as follows
390 * rate = (parent_rate >> p) / (m + 1);
393 static void sun7i_a20_get_out_factors(struct factors_request *req)
395 u8 div, calcm, calcp;
397 /* These clocks can only divide, so we will never be able to achieve
398 * frequencies higher than the parent frequency */
399 if (req->rate > req->parent_rate)
400 req->rate = req->parent_rate;
402 div = DIV_ROUND_UP(req->parent_rate, req->rate);
406 else if (div / 2 < 32)
408 else if (div / 4 < 32)
413 calcm = DIV_ROUND_UP(div, 1 << calcp);
415 req->rate = (req->parent_rate >> calcp) / calcm;
421 * sunxi_factors_clk_setup() - Setup function for factor clocks
424 static const struct clk_factors_config sun4i_pll1_config = {
435 static const struct clk_factors_config sun6i_a31_pll1_config = {
445 static const struct clk_factors_config sun8i_a23_pll1_config = {
457 static const struct clk_factors_config sun4i_pll5_config = {
464 static const struct clk_factors_config sun6i_a31_pll6_config = {
472 static const struct clk_factors_config sun5i_a13_ahb_config = {
477 static const struct clk_factors_config sun6i_ahb1_config = {
484 static const struct clk_factors_config sun4i_apb1_config = {
491 /* user manual says "n" but it's really "p" */
492 static const struct clk_factors_config sun7i_a20_out_config = {
499 static const struct factors_data sun4i_pll1_data __initconst = {
501 .table = &sun4i_pll1_config,
502 .getter = sun4i_get_pll1_factors,
505 static const struct factors_data sun6i_a31_pll1_data __initconst = {
507 .table = &sun6i_a31_pll1_config,
508 .getter = sun6i_a31_get_pll1_factors,
511 static const struct factors_data sun8i_a23_pll1_data __initconst = {
513 .table = &sun8i_a23_pll1_config,
514 .getter = sun8i_a23_get_pll1_factors,
517 static const struct factors_data sun7i_a20_pll4_data __initconst = {
519 .table = &sun4i_pll5_config,
520 .getter = sun4i_get_pll5_factors,
523 static const struct factors_data sun4i_pll5_data __initconst = {
525 .table = &sun4i_pll5_config,
526 .getter = sun4i_get_pll5_factors,
529 static const struct factors_data sun6i_a31_pll6_data __initconst = {
531 .table = &sun6i_a31_pll6_config,
532 .getter = sun6i_a31_get_pll6_factors,
535 static const struct factors_data sun5i_a13_ahb_data __initconst = {
537 .muxmask = BIT(1) | BIT(0),
538 .table = &sun5i_a13_ahb_config,
539 .getter = sun5i_a13_get_ahb_factors,
542 static const struct factors_data sun6i_ahb1_data __initconst = {
544 .muxmask = BIT(1) | BIT(0),
545 .table = &sun6i_ahb1_config,
546 .getter = sun6i_get_ahb1_factors,
547 .recalc = sun6i_ahb1_recalc,
550 static const struct factors_data sun4i_apb1_data __initconst = {
552 .muxmask = BIT(1) | BIT(0),
553 .table = &sun4i_apb1_config,
554 .getter = sun4i_get_apb1_factors,
557 static const struct factors_data sun7i_a20_out_data __initconst = {
560 .muxmask = BIT(1) | BIT(0),
561 .table = &sun7i_a20_out_config,
562 .getter = sun7i_a20_get_out_factors,
565 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
566 const struct factors_data *data)
570 reg = of_iomap(node, 0);
572 pr_err("Could not get registers for factors-clk: %pOFn\n",
577 return sunxi_factors_register(node, data, &clk_lock, reg);
580 static void __init sun4i_pll1_clk_setup(struct device_node *node)
582 sunxi_factors_clk_setup(node, &sun4i_pll1_data);
584 CLK_OF_DECLARE(sun4i_pll1, "allwinner,sun4i-a10-pll1-clk",
585 sun4i_pll1_clk_setup);
587 static void __init sun6i_pll1_clk_setup(struct device_node *node)
589 sunxi_factors_clk_setup(node, &sun6i_a31_pll1_data);
591 CLK_OF_DECLARE(sun6i_pll1, "allwinner,sun6i-a31-pll1-clk",
592 sun6i_pll1_clk_setup);
594 static void __init sun8i_pll1_clk_setup(struct device_node *node)
596 sunxi_factors_clk_setup(node, &sun8i_a23_pll1_data);
598 CLK_OF_DECLARE(sun8i_pll1, "allwinner,sun8i-a23-pll1-clk",
599 sun8i_pll1_clk_setup);
601 static void __init sun7i_pll4_clk_setup(struct device_node *node)
603 sunxi_factors_clk_setup(node, &sun7i_a20_pll4_data);
605 CLK_OF_DECLARE(sun7i_pll4, "allwinner,sun7i-a20-pll4-clk",
606 sun7i_pll4_clk_setup);
608 static void __init sun5i_ahb_clk_setup(struct device_node *node)
610 sunxi_factors_clk_setup(node, &sun5i_a13_ahb_data);
612 CLK_OF_DECLARE(sun5i_ahb, "allwinner,sun5i-a13-ahb-clk",
613 sun5i_ahb_clk_setup);
615 static void __init sun6i_ahb1_clk_setup(struct device_node *node)
617 sunxi_factors_clk_setup(node, &sun6i_ahb1_data);
619 CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-clk",
620 sun6i_ahb1_clk_setup);
622 static void __init sun4i_apb1_clk_setup(struct device_node *node)
624 sunxi_factors_clk_setup(node, &sun4i_apb1_data);
626 CLK_OF_DECLARE(sun4i_apb1, "allwinner,sun4i-a10-apb1-clk",
627 sun4i_apb1_clk_setup);
629 static void __init sun7i_out_clk_setup(struct device_node *node)
631 sunxi_factors_clk_setup(node, &sun7i_a20_out_data);
633 CLK_OF_DECLARE(sun7i_out, "allwinner,sun7i-a20-out-clk",
634 sun7i_out_clk_setup);
638 * sunxi_mux_clk_setup() - Setup function for muxes
641 #define SUNXI_MUX_GATE_WIDTH 2
647 static const struct mux_data sun4i_cpu_mux_data __initconst = {
651 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
655 static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
659 static struct clk * __init sunxi_mux_clk_setup(struct device_node *node,
660 const struct mux_data *data,
664 const char *clk_name = node->name;
665 const char *parents[SUNXI_MAX_PARENTS];
669 reg = of_iomap(node, 0);
671 pr_err("Could not map registers for mux-clk: %pOF\n", node);
675 i = of_clk_parent_fill(node, parents, SUNXI_MAX_PARENTS);
676 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
677 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
682 clk = clk_register_mux(NULL, clk_name, parents, i,
683 CLK_SET_RATE_PARENT | flags, reg,
684 data->shift, SUNXI_MUX_GATE_WIDTH,
688 pr_err("%s: failed to register mux clock %s: %ld\n", __func__,
689 clk_name, PTR_ERR(clk));
693 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
694 pr_err("%s: failed to add clock provider for %s\n",
696 clk_unregister_divider(clk);
706 static void __init sun4i_cpu_clk_setup(struct device_node *node)
708 /* Protect CPU clock */
709 sunxi_mux_clk_setup(node, &sun4i_cpu_mux_data, CLK_IS_CRITICAL);
711 CLK_OF_DECLARE(sun4i_cpu, "allwinner,sun4i-a10-cpu-clk",
712 sun4i_cpu_clk_setup);
714 static void __init sun6i_ahb1_mux_clk_setup(struct device_node *node)
716 sunxi_mux_clk_setup(node, &sun6i_a31_ahb1_mux_data, 0);
718 CLK_OF_DECLARE(sun6i_ahb1_mux, "allwinner,sun6i-a31-ahb1-mux-clk",
719 sun6i_ahb1_mux_clk_setup);
721 static void __init sun8i_ahb2_clk_setup(struct device_node *node)
723 sunxi_mux_clk_setup(node, &sun8i_h3_ahb2_mux_data, 0);
725 CLK_OF_DECLARE(sun8i_ahb2, "allwinner,sun8i-h3-ahb2-clk",
726 sun8i_ahb2_clk_setup);
730 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
737 const struct clk_div_table *table;
740 static const struct div_data sun4i_axi_data __initconst = {
746 static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
747 { .val = 0, .div = 1 },
748 { .val = 1, .div = 2 },
749 { .val = 2, .div = 3 },
750 { .val = 3, .div = 4 },
751 { .val = 4, .div = 4 },
752 { .val = 5, .div = 4 },
753 { .val = 6, .div = 4 },
754 { .val = 7, .div = 4 },
758 static const struct div_data sun8i_a23_axi_data __initconst = {
760 .table = sun8i_a23_axi_table,
763 static const struct div_data sun4i_ahb_data __initconst = {
769 static const struct clk_div_table sun4i_apb0_table[] __initconst = {
770 { .val = 0, .div = 2 },
771 { .val = 1, .div = 2 },
772 { .val = 2, .div = 4 },
773 { .val = 3, .div = 8 },
777 static const struct div_data sun4i_apb0_data __initconst = {
781 .table = sun4i_apb0_table,
784 static void __init sunxi_divider_clk_setup(struct device_node *node,
785 const struct div_data *data)
788 const char *clk_name = node->name;
789 const char *clk_parent;
792 reg = of_iomap(node, 0);
794 pr_err("Could not map registers for mux-clk: %pOF\n", node);
798 clk_parent = of_clk_get_parent_name(node, 0);
800 if (of_property_read_string(node, "clock-output-names", &clk_name)) {
801 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
806 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
807 reg, data->shift, data->width,
808 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
809 data->table, &clk_lock);
811 pr_err("%s: failed to register divider clock %s: %ld\n",
812 __func__, clk_name, PTR_ERR(clk));
816 if (of_clk_add_provider(node, of_clk_src_simple_get, clk)) {
817 pr_err("%s: failed to add clock provider for %s\n",
822 if (clk_register_clkdev(clk, clk_name, NULL)) {
823 of_clk_del_provider(node);
829 clk_unregister_divider(clk);
835 static void __init sun4i_ahb_clk_setup(struct device_node *node)
837 sunxi_divider_clk_setup(node, &sun4i_ahb_data);
839 CLK_OF_DECLARE(sun4i_ahb, "allwinner,sun4i-a10-ahb-clk",
840 sun4i_ahb_clk_setup);
842 static void __init sun4i_apb0_clk_setup(struct device_node *node)
844 sunxi_divider_clk_setup(node, &sun4i_apb0_data);
846 CLK_OF_DECLARE(sun4i_apb0, "allwinner,sun4i-a10-apb0-clk",
847 sun4i_apb0_clk_setup);
849 static void __init sun4i_axi_clk_setup(struct device_node *node)
851 sunxi_divider_clk_setup(node, &sun4i_axi_data);
853 CLK_OF_DECLARE(sun4i_axi, "allwinner,sun4i-a10-axi-clk",
854 sun4i_axi_clk_setup);
856 static void __init sun8i_axi_clk_setup(struct device_node *node)
858 sunxi_divider_clk_setup(node, &sun8i_a23_axi_data);
860 CLK_OF_DECLARE(sun8i_axi, "allwinner,sun8i-a23-axi-clk",
861 sun8i_axi_clk_setup);
866 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
869 #define SUNXI_GATES_MAX_SIZE 64
872 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
876 * sunxi_divs_clk_setup() helper data
879 #define SUNXI_DIVS_MAX_QTY 4
880 #define SUNXI_DIVISOR_WIDTH 2
883 const struct factors_data *factors; /* data for the factor clock */
884 int ndivs; /* number of outputs */
886 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
887 * self or base factor clock refers to the output from the pll
888 * itself. The remaining refer to fixed or configurable divider
892 u8 self; /* is it the base factor clock? (only one) */
893 u8 fixed; /* is it a fixed divisor? if not... */
894 struct clk_div_table *table; /* is it a table based divisor? */
895 u8 shift; /* otherwise it's a normal divisor with this shift */
896 u8 pow; /* is it power-of-two based? */
897 u8 gate; /* is it independently gateable? */
899 } div[SUNXI_DIVS_MAX_QTY];
902 static struct clk_div_table pll6_sata_tbl[] = {
903 { .val = 0, .div = 6, },
904 { .val = 1, .div = 12, },
905 { .val = 2, .div = 18, },
906 { .val = 3, .div = 24, },
910 static const struct divs_data pll5_divs_data __initconst = {
911 .factors = &sun4i_pll5_data,
914 /* Protect PLL5_DDR */
915 { .shift = 0, .pow = 0, .critical = true }, /* M, DDR */
916 { .shift = 16, .pow = 1, }, /* P, other */
917 /* No output for the base factor clock */
921 static const struct divs_data pll6_divs_data __initconst = {
922 .factors = &sun4i_pll5_data,
925 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
926 { .fixed = 2 }, /* P, other */
927 { .self = 1 }, /* base factor clock, 2x */
928 { .fixed = 4 }, /* pll6 / 4, used as ahb input */
932 static const struct divs_data sun6i_a31_pll6_divs_data __initconst = {
933 .factors = &sun6i_a31_pll6_data,
936 { .fixed = 2 }, /* normal output */
937 { .self = 1 }, /* base factor clock, 2x */
942 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
944 * These clocks look something like this
945 * ________________________
946 * | ___divisor 1---|----> to consumer
947 * parent >--| pll___/___divisor 2---|----> to consumer
948 * | \_______________|____> to consumer
949 * |________________________|
952 static struct clk ** __init sunxi_divs_clk_setup(struct device_node *node,
953 const struct divs_data *data)
955 struct clk_onecell_data *clk_data;
957 const char *clk_name;
958 struct clk **clks, *pclk;
959 struct clk_hw *gate_hw, *rate_hw;
960 const struct clk_ops *rate_ops;
961 struct clk_gate *gate = NULL;
962 struct clk_fixed_factor *fix_factor;
963 struct clk_divider *divider;
964 struct factors_data factors = *data->factors;
965 char *derived_name = NULL;
967 int ndivs = SUNXI_DIVS_MAX_QTY, i = 0;
970 /* if number of children known, use it */
974 /* Try to find a name for base factor clock */
975 for (i = 0; i < ndivs; i++) {
976 if (data->div[i].self) {
977 of_property_read_string_index(node, "clock-output-names",
982 /* If we don't have a .self clk use the first output-name up to '_' */
983 if (factors.name == NULL) {
986 of_property_read_string_index(node, "clock-output-names",
988 endp = strchr(clk_name, '_');
990 derived_name = kstrndup(clk_name, endp - clk_name,
992 factors.name = derived_name;
994 factors.name = clk_name;
998 /* Set up factor clock that we will be dividing */
999 pclk = sunxi_factors_clk_setup(node, &factors);
1003 parent = __clk_get_name(pclk);
1004 kfree(derived_name);
1006 reg = of_iomap(node, 0);
1008 pr_err("Could not map registers for divs-clk: %pOF\n", node);
1012 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1016 clks = kcalloc(ndivs, sizeof(*clks), GFP_KERNEL);
1020 clk_data->clks = clks;
1022 /* It's not a good idea to have automatic reparenting changing
1024 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1026 for (i = 0; i < ndivs; i++) {
1027 if (of_property_read_string_index(node, "clock-output-names",
1031 /* If this is the base factor clock, only update clks */
1032 if (data->div[i].self) {
1033 clk_data->clks[i] = pclk;
1041 /* If this leaf clock can be gated, create a gate */
1042 if (data->div[i].gate) {
1043 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1048 gate->bit_idx = data->div[i].gate;
1049 gate->lock = &clk_lock;
1051 gate_hw = &gate->hw;
1054 /* Leaves can be fixed or configurable divisors */
1055 if (data->div[i].fixed) {
1056 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1060 fix_factor->mult = 1;
1061 fix_factor->div = data->div[i].fixed;
1063 rate_hw = &fix_factor->hw;
1064 rate_ops = &clk_fixed_factor_ops;
1066 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1070 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1073 divider->shift = data->div[i].shift;
1074 divider->width = SUNXI_DIVISOR_WIDTH;
1075 divider->flags = flags;
1076 divider->lock = &clk_lock;
1077 divider->table = data->div[i].table;
1079 rate_hw = ÷r->hw;
1080 rate_ops = &clk_divider_ops;
1083 /* Wrap the (potential) gate and the divisor on a composite
1084 * clock to unify them */
1085 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1088 gate_hw, &clk_gate_ops,
1090 data->div[i].critical ?
1091 CLK_IS_CRITICAL : 0);
1093 WARN_ON(IS_ERR(clk_data->clks[i]));
1096 /* Adjust to the real max */
1097 clk_data->clk_num = i;
1099 if (of_clk_add_provider(node, of_clk_src_onecell_get, clk_data)) {
1100 pr_err("%s: failed to add clock provider for %s\n",
1101 __func__, clk_name);
1117 static void __init sun4i_pll5_clk_setup(struct device_node *node)
1119 sunxi_divs_clk_setup(node, &pll5_divs_data);
1121 CLK_OF_DECLARE(sun4i_pll5, "allwinner,sun4i-a10-pll5-clk",
1122 sun4i_pll5_clk_setup);
1124 static void __init sun4i_pll6_clk_setup(struct device_node *node)
1126 sunxi_divs_clk_setup(node, &pll6_divs_data);
1128 CLK_OF_DECLARE(sun4i_pll6, "allwinner,sun4i-a10-pll6-clk",
1129 sun4i_pll6_clk_setup);
1131 static void __init sun6i_pll6_clk_setup(struct device_node *node)
1133 sunxi_divs_clk_setup(node, &sun6i_a31_pll6_divs_data);
1135 CLK_OF_DECLARE(sun6i_pll6, "allwinner,sun6i-a31-pll6-clk",
1136 sun6i_pll6_clk_setup);
1141 * rate = parent_rate / (m + 1);
1143 static void sun6i_display_factors(struct factors_request *req)
1147 if (req->rate > req->parent_rate)
1148 req->rate = req->parent_rate;
1150 m = DIV_ROUND_UP(req->parent_rate, req->rate);
1152 req->rate = req->parent_rate / m;
1156 static const struct clk_factors_config sun6i_display_config = {
1161 static const struct factors_data sun6i_display_data __initconst = {
1164 .muxmask = BIT(2) | BIT(1) | BIT(0),
1165 .table = &sun6i_display_config,
1166 .getter = sun6i_display_factors,
1169 static void __init sun6i_display_setup(struct device_node *node)
1171 sunxi_factors_clk_setup(node, &sun6i_display_data);
1173 CLK_OF_DECLARE(sun6i_display, "allwinner,sun6i-a31-display-clk",
1174 sun6i_display_setup);