1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2013 Emilio López
5 * Emilio López <emilio@elopez.com.ar>
9 #include <linux/clk-provider.h>
10 #include <linux/clkdev.h>
13 #include <linux/of_address.h>
14 #include <linux/reset-controller.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/log2.h>
19 #include "clk-factors.h"
21 static DEFINE_SPINLOCK(clk_lock
);
23 /* Maximum number of parents our clocks have */
24 #define SUNXI_MAX_PARENTS 5
27 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
28 * PLL1 rate is calculated as follows
29 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
30 * parent_rate is always 24Mhz
33 static void sun4i_get_pll1_factors(struct factors_request
*req
)
37 /* Normalize value to a 6M multiple */
38 div
= req
->rate
/ 6000000;
39 req
->rate
= 6000000 * div
;
41 /* m is always zero for pll1 */
44 /* k is 1 only on these cases */
45 if (req
->rate
>= 768000000 || req
->rate
== 42000000 ||
46 req
->rate
== 54000000)
51 /* p will be 3 for divs under 10 */
55 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
56 else if (div
< 20 || (div
< 32 && (div
& 1)))
59 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
60 * of divs between 40-62 */
61 else if (div
< 40 || (div
< 64 && (div
& 2)))
64 /* any other entries have p = 0 */
68 /* calculate a suitable n based on k and p */
75 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
76 * PLL1 rate is calculated as follows
77 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
78 * parent_rate should always be 24MHz
80 static void sun6i_a31_get_pll1_factors(struct factors_request
*req
)
83 * We can operate only on MHz, this will make our life easier
86 u32 freq_mhz
= req
->rate
/ 1000000;
87 u32 parent_freq_mhz
= req
->parent_rate
/ 1000000;
90 * Round down the frequency to the closest multiple of either
93 u32 round_freq_6
= rounddown(freq_mhz
, 6);
94 u32 round_freq_16
= round_down(freq_mhz
, 16);
96 if (round_freq_6
> round_freq_16
)
97 freq_mhz
= round_freq_6
;
99 freq_mhz
= round_freq_16
;
101 req
->rate
= freq_mhz
* 1000000;
103 /* If the frequency is a multiple of 32 MHz, k is always 3 */
104 if (!(freq_mhz
% 32))
106 /* If the frequency is a multiple of 9 MHz, k is always 2 */
107 else if (!(freq_mhz
% 9))
109 /* If the frequency is a multiple of 8 MHz, k is always 1 */
110 else if (!(freq_mhz
% 8))
112 /* Otherwise, we don't use the k factor */
117 * If the frequency is a multiple of 2 but not a multiple of
118 * 3, m is 3. This is the first time we use 6 here, yet we
119 * will use it on several other places.
120 * We use this number because it's the lowest frequency we can
121 * generate (with n = 0, k = 0, m = 3), so every other frequency
122 * somehow relates to this frequency.
124 if ((freq_mhz
% 6) == 2 || (freq_mhz
% 6) == 4)
127 * If the frequency is a multiple of 6MHz, but the factor is
130 else if ((freq_mhz
/ 6) & 1)
132 /* Otherwise, we end up with m = 1 */
136 /* Calculate n thanks to the above factors we already got */
137 req
->n
= freq_mhz
* (req
->m
+ 1) / ((req
->k
+ 1) * parent_freq_mhz
)
141 * If n end up being outbound, and that we can still decrease
144 if ((req
->n
+ 1) > 31 && (req
->m
+ 1) > 1) {
145 req
->n
= (req
->n
+ 1) / 2 - 1;
146 req
->m
= (req
->m
+ 1) / 2 - 1;
151 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
152 * PLL1 rate is calculated as follows
153 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
154 * parent_rate is always 24Mhz
157 static void sun8i_a23_get_pll1_factors(struct factors_request
*req
)
161 /* Normalize value to a 6M multiple */
162 div
= req
->rate
/ 6000000;
163 req
->rate
= 6000000 * div
;
165 /* m is always zero for pll1 */
168 /* k is 1 only on these cases */
169 if (req
->rate
>= 768000000 || req
->rate
== 42000000 ||
170 req
->rate
== 54000000)
175 /* p will be 2 for divs under 20 and odd divs under 32 */
176 if (div
< 20 || (div
< 32 && (div
& 1)))
179 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
180 * of divs between 40-62 */
181 else if (div
< 40 || (div
< 64 && (div
& 2)))
184 /* any other entries have p = 0 */
188 /* calculate a suitable n based on k and p */
191 req
->n
= div
/ 4 - 1;
195 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
196 * PLL5 rate is calculated as follows
197 * rate = parent_rate * n * (k + 1)
198 * parent_rate is always 24Mhz
201 static void sun4i_get_pll5_factors(struct factors_request
*req
)
205 /* Normalize value to a parent_rate multiple (24M) */
206 div
= req
->rate
/ req
->parent_rate
;
207 req
->rate
= req
->parent_rate
* div
;
211 else if (div
/ 2 < 31)
213 else if (div
/ 3 < 31)
218 req
->n
= DIV_ROUND_UP(div
, (req
->k
+ 1));
222 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
223 * PLL6x2 rate is calculated as follows
224 * rate = parent_rate * (n + 1) * (k + 1)
225 * parent_rate is always 24Mhz
228 static void sun6i_a31_get_pll6_factors(struct factors_request
*req
)
232 /* Normalize value to a parent_rate multiple (24M) */
233 div
= req
->rate
/ req
->parent_rate
;
234 req
->rate
= req
->parent_rate
* div
;
240 req
->n
= DIV_ROUND_UP(div
, (req
->k
+ 1)) - 1;
244 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
245 * AHB rate is calculated as follows
246 * rate = parent_rate >> p
249 static void sun5i_a13_get_ahb_factors(struct factors_request
*req
)
254 if (req
->parent_rate
< req
->rate
)
255 req
->rate
= req
->parent_rate
;
258 * user manual says valid speed is 8k ~ 276M, but tests show it
259 * can work at speeds up to 300M, just after reparenting to pll6
261 if (req
->rate
< 8000)
263 if (req
->rate
> 300000000)
264 req
->rate
= 300000000;
266 div
= order_base_2(DIV_ROUND_UP(req
->parent_rate
, req
->rate
));
272 req
->rate
= req
->parent_rate
>> div
;
277 #define SUN6I_AHB1_PARENT_PLL6 3
280 * sun6i_a31_get_ahb_factors() - calculates m, p factors for AHB
281 * AHB rate is calculated as follows
282 * rate = parent_rate >> p
284 * if parent is pll6, then
285 * parent_rate = pll6 rate / (m + 1)
288 static void sun6i_get_ahb1_factors(struct factors_request
*req
)
290 u8 div
, calcp
, calcm
= 1;
293 * clock can only divide, so we will never be able to achieve
294 * frequencies higher than the parent frequency
296 if (req
->parent_rate
&& req
->rate
> req
->parent_rate
)
297 req
->rate
= req
->parent_rate
;
299 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
301 /* calculate pre-divider if parent is pll6 */
302 if (req
->parent_index
== SUN6I_AHB1_PARENT_PLL6
) {
305 else if (div
/ 2 < 4)
307 else if (div
/ 4 < 4)
312 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
314 calcp
= __roundup_pow_of_two(div
);
315 calcp
= calcp
> 3 ? 3 : calcp
;
318 req
->rate
= (req
->parent_rate
/ calcm
) >> calcp
;
324 * sun6i_ahb1_recalc() - calculates AHB clock rate from m, p factors and
327 static void sun6i_ahb1_recalc(struct factors_request
*req
)
329 req
->rate
= req
->parent_rate
;
331 /* apply pre-divider first if parent is pll6 */
332 if (req
->parent_index
== SUN6I_AHB1_PARENT_PLL6
)
333 req
->rate
/= req
->m
+ 1;
336 req
->rate
>>= req
->p
;
340 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
341 * APB1 rate is calculated as follows
342 * rate = (parent_rate >> p) / (m + 1);
345 static void sun4i_get_apb1_factors(struct factors_request
*req
)
350 if (req
->parent_rate
< req
->rate
)
351 req
->rate
= req
->parent_rate
;
353 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
368 calcm
= (div
>> calcp
) - 1;
370 req
->rate
= (req
->parent_rate
>> calcp
) / (calcm
+ 1);
379 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
380 * CLK_OUT rate is calculated as follows
381 * rate = (parent_rate >> p) / (m + 1);
384 static void sun7i_a20_get_out_factors(struct factors_request
*req
)
386 u8 div
, calcm
, calcp
;
388 /* These clocks can only divide, so we will never be able to achieve
389 * frequencies higher than the parent frequency */
390 if (req
->rate
> req
->parent_rate
)
391 req
->rate
= req
->parent_rate
;
393 div
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
397 else if (div
/ 2 < 32)
399 else if (div
/ 4 < 32)
404 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
406 req
->rate
= (req
->parent_rate
>> calcp
) / calcm
;
412 * sunxi_factors_clk_setup() - Setup function for factor clocks
415 static const struct clk_factors_config sun4i_pll1_config
= {
426 static const struct clk_factors_config sun6i_a31_pll1_config
= {
436 static const struct clk_factors_config sun8i_a23_pll1_config
= {
448 static const struct clk_factors_config sun4i_pll5_config
= {
455 static const struct clk_factors_config sun6i_a31_pll6_config
= {
463 static const struct clk_factors_config sun5i_a13_ahb_config
= {
468 static const struct clk_factors_config sun6i_ahb1_config
= {
475 static const struct clk_factors_config sun4i_apb1_config
= {
482 /* user manual says "n" but it's really "p" */
483 static const struct clk_factors_config sun7i_a20_out_config
= {
490 static const struct factors_data sun4i_pll1_data __initconst
= {
492 .table
= &sun4i_pll1_config
,
493 .getter
= sun4i_get_pll1_factors
,
496 static const struct factors_data sun6i_a31_pll1_data __initconst
= {
498 .table
= &sun6i_a31_pll1_config
,
499 .getter
= sun6i_a31_get_pll1_factors
,
502 static const struct factors_data sun8i_a23_pll1_data __initconst
= {
504 .table
= &sun8i_a23_pll1_config
,
505 .getter
= sun8i_a23_get_pll1_factors
,
508 static const struct factors_data sun7i_a20_pll4_data __initconst
= {
510 .table
= &sun4i_pll5_config
,
511 .getter
= sun4i_get_pll5_factors
,
514 static const struct factors_data sun4i_pll5_data __initconst
= {
516 .table
= &sun4i_pll5_config
,
517 .getter
= sun4i_get_pll5_factors
,
520 static const struct factors_data sun6i_a31_pll6_data __initconst
= {
522 .table
= &sun6i_a31_pll6_config
,
523 .getter
= sun6i_a31_get_pll6_factors
,
526 static const struct factors_data sun5i_a13_ahb_data __initconst
= {
528 .muxmask
= BIT(1) | BIT(0),
529 .table
= &sun5i_a13_ahb_config
,
530 .getter
= sun5i_a13_get_ahb_factors
,
533 static const struct factors_data sun6i_ahb1_data __initconst
= {
535 .muxmask
= BIT(1) | BIT(0),
536 .table
= &sun6i_ahb1_config
,
537 .getter
= sun6i_get_ahb1_factors
,
538 .recalc
= sun6i_ahb1_recalc
,
541 static const struct factors_data sun4i_apb1_data __initconst
= {
543 .muxmask
= BIT(1) | BIT(0),
544 .table
= &sun4i_apb1_config
,
545 .getter
= sun4i_get_apb1_factors
,
548 static const struct factors_data sun7i_a20_out_data __initconst
= {
551 .muxmask
= BIT(1) | BIT(0),
552 .table
= &sun7i_a20_out_config
,
553 .getter
= sun7i_a20_get_out_factors
,
556 static struct clk
* __init
sunxi_factors_clk_setup(struct device_node
*node
,
557 const struct factors_data
*data
)
561 reg
= of_iomap(node
, 0);
563 pr_err("Could not get registers for factors-clk: %pOFn\n",
568 return sunxi_factors_register(node
, data
, &clk_lock
, reg
);
571 static void __init
sun4i_pll1_clk_setup(struct device_node
*node
)
573 sunxi_factors_clk_setup(node
, &sun4i_pll1_data
);
575 CLK_OF_DECLARE(sun4i_pll1
, "allwinner,sun4i-a10-pll1-clk",
576 sun4i_pll1_clk_setup
);
578 static void __init
sun6i_pll1_clk_setup(struct device_node
*node
)
580 sunxi_factors_clk_setup(node
, &sun6i_a31_pll1_data
);
582 CLK_OF_DECLARE(sun6i_pll1
, "allwinner,sun6i-a31-pll1-clk",
583 sun6i_pll1_clk_setup
);
585 static void __init
sun8i_pll1_clk_setup(struct device_node
*node
)
587 sunxi_factors_clk_setup(node
, &sun8i_a23_pll1_data
);
589 CLK_OF_DECLARE(sun8i_pll1
, "allwinner,sun8i-a23-pll1-clk",
590 sun8i_pll1_clk_setup
);
592 static void __init
sun7i_pll4_clk_setup(struct device_node
*node
)
594 sunxi_factors_clk_setup(node
, &sun7i_a20_pll4_data
);
596 CLK_OF_DECLARE(sun7i_pll4
, "allwinner,sun7i-a20-pll4-clk",
597 sun7i_pll4_clk_setup
);
599 static void __init
sun5i_ahb_clk_setup(struct device_node
*node
)
601 sunxi_factors_clk_setup(node
, &sun5i_a13_ahb_data
);
603 CLK_OF_DECLARE(sun5i_ahb
, "allwinner,sun5i-a13-ahb-clk",
604 sun5i_ahb_clk_setup
);
606 static void __init
sun6i_ahb1_clk_setup(struct device_node
*node
)
608 sunxi_factors_clk_setup(node
, &sun6i_ahb1_data
);
610 CLK_OF_DECLARE(sun6i_a31_ahb1
, "allwinner,sun6i-a31-ahb1-clk",
611 sun6i_ahb1_clk_setup
);
613 static void __init
sun4i_apb1_clk_setup(struct device_node
*node
)
615 sunxi_factors_clk_setup(node
, &sun4i_apb1_data
);
617 CLK_OF_DECLARE(sun4i_apb1
, "allwinner,sun4i-a10-apb1-clk",
618 sun4i_apb1_clk_setup
);
620 static void __init
sun7i_out_clk_setup(struct device_node
*node
)
622 sunxi_factors_clk_setup(node
, &sun7i_a20_out_data
);
624 CLK_OF_DECLARE(sun7i_out
, "allwinner,sun7i-a20-out-clk",
625 sun7i_out_clk_setup
);
629 * sunxi_mux_clk_setup() - Setup function for muxes
632 #define SUNXI_MUX_GATE_WIDTH 2
638 static const struct mux_data sun4i_cpu_mux_data __initconst
= {
642 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst
= {
646 static const struct mux_data sun8i_h3_ahb2_mux_data __initconst
= {
650 static struct clk
* __init
sunxi_mux_clk_setup(struct device_node
*node
,
651 const struct mux_data
*data
,
655 const char *clk_name
= node
->name
;
656 const char *parents
[SUNXI_MAX_PARENTS
];
660 reg
= of_iomap(node
, 0);
662 pr_err("Could not map registers for mux-clk: %pOF\n", node
);
666 i
= of_clk_parent_fill(node
, parents
, SUNXI_MAX_PARENTS
);
667 if (of_property_read_string(node
, "clock-output-names", &clk_name
)) {
668 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
673 clk
= clk_register_mux(NULL
, clk_name
, parents
, i
,
674 CLK_SET_RATE_PARENT
| flags
, reg
,
675 data
->shift
, SUNXI_MUX_GATE_WIDTH
,
679 pr_err("%s: failed to register mux clock %s: %ld\n", __func__
,
680 clk_name
, PTR_ERR(clk
));
684 if (of_clk_add_provider(node
, of_clk_src_simple_get
, clk
)) {
685 pr_err("%s: failed to add clock provider for %s\n",
687 clk_unregister_divider(clk
);
697 static void __init
sun4i_cpu_clk_setup(struct device_node
*node
)
699 /* Protect CPU clock */
700 sunxi_mux_clk_setup(node
, &sun4i_cpu_mux_data
, CLK_IS_CRITICAL
);
702 CLK_OF_DECLARE(sun4i_cpu
, "allwinner,sun4i-a10-cpu-clk",
703 sun4i_cpu_clk_setup
);
705 static void __init
sun6i_ahb1_mux_clk_setup(struct device_node
*node
)
707 sunxi_mux_clk_setup(node
, &sun6i_a31_ahb1_mux_data
, 0);
709 CLK_OF_DECLARE(sun6i_ahb1_mux
, "allwinner,sun6i-a31-ahb1-mux-clk",
710 sun6i_ahb1_mux_clk_setup
);
712 static void __init
sun8i_ahb2_clk_setup(struct device_node
*node
)
714 sunxi_mux_clk_setup(node
, &sun8i_h3_ahb2_mux_data
, 0);
716 CLK_OF_DECLARE(sun8i_ahb2
, "allwinner,sun8i-h3-ahb2-clk",
717 sun8i_ahb2_clk_setup
);
721 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
728 const struct clk_div_table
*table
;
731 static const struct div_data sun4i_axi_data __initconst
= {
737 static const struct clk_div_table sun8i_a23_axi_table
[] __initconst
= {
738 { .val
= 0, .div
= 1 },
739 { .val
= 1, .div
= 2 },
740 { .val
= 2, .div
= 3 },
741 { .val
= 3, .div
= 4 },
742 { .val
= 4, .div
= 4 },
743 { .val
= 5, .div
= 4 },
744 { .val
= 6, .div
= 4 },
745 { .val
= 7, .div
= 4 },
749 static const struct div_data sun8i_a23_axi_data __initconst
= {
751 .table
= sun8i_a23_axi_table
,
754 static const struct div_data sun4i_ahb_data __initconst
= {
760 static const struct clk_div_table sun4i_apb0_table
[] __initconst
= {
761 { .val
= 0, .div
= 2 },
762 { .val
= 1, .div
= 2 },
763 { .val
= 2, .div
= 4 },
764 { .val
= 3, .div
= 8 },
768 static const struct div_data sun4i_apb0_data __initconst
= {
772 .table
= sun4i_apb0_table
,
775 static void __init
sunxi_divider_clk_setup(struct device_node
*node
,
776 const struct div_data
*data
)
779 const char *clk_name
= node
->name
;
780 const char *clk_parent
;
783 reg
= of_iomap(node
, 0);
785 pr_err("Could not map registers for mux-clk: %pOF\n", node
);
789 clk_parent
= of_clk_get_parent_name(node
, 0);
791 if (of_property_read_string(node
, "clock-output-names", &clk_name
)) {
792 pr_err("%s: could not read clock-output-names from \"%pOF\"\n",
797 clk
= clk_register_divider_table(NULL
, clk_name
, clk_parent
, 0,
798 reg
, data
->shift
, data
->width
,
799 data
->pow
? CLK_DIVIDER_POWER_OF_TWO
: 0,
800 data
->table
, &clk_lock
);
802 pr_err("%s: failed to register divider clock %s: %ld\n",
803 __func__
, clk_name
, PTR_ERR(clk
));
807 if (of_clk_add_provider(node
, of_clk_src_simple_get
, clk
)) {
808 pr_err("%s: failed to add clock provider for %s\n",
813 if (clk_register_clkdev(clk
, clk_name
, NULL
)) {
814 of_clk_del_provider(node
);
820 clk_unregister_divider(clk
);
826 static void __init
sun4i_ahb_clk_setup(struct device_node
*node
)
828 sunxi_divider_clk_setup(node
, &sun4i_ahb_data
);
830 CLK_OF_DECLARE(sun4i_ahb
, "allwinner,sun4i-a10-ahb-clk",
831 sun4i_ahb_clk_setup
);
833 static void __init
sun4i_apb0_clk_setup(struct device_node
*node
)
835 sunxi_divider_clk_setup(node
, &sun4i_apb0_data
);
837 CLK_OF_DECLARE(sun4i_apb0
, "allwinner,sun4i-a10-apb0-clk",
838 sun4i_apb0_clk_setup
);
840 static void __init
sun4i_axi_clk_setup(struct device_node
*node
)
842 sunxi_divider_clk_setup(node
, &sun4i_axi_data
);
844 CLK_OF_DECLARE(sun4i_axi
, "allwinner,sun4i-a10-axi-clk",
845 sun4i_axi_clk_setup
);
847 static void __init
sun8i_axi_clk_setup(struct device_node
*node
)
849 sunxi_divider_clk_setup(node
, &sun8i_a23_axi_data
);
851 CLK_OF_DECLARE(sun8i_axi
, "allwinner,sun8i-a23-axi-clk",
852 sun8i_axi_clk_setup
);
857 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
860 #define SUNXI_GATES_MAX_SIZE 64
863 DECLARE_BITMAP(mask
, SUNXI_GATES_MAX_SIZE
);
867 * sunxi_divs_clk_setup() helper data
870 #define SUNXI_DIVS_MAX_QTY 4
871 #define SUNXI_DIVISOR_WIDTH 2
874 const struct factors_data
*factors
; /* data for the factor clock */
875 int ndivs
; /* number of outputs */
877 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
878 * self or base factor clock refers to the output from the pll
879 * itself. The remaining refer to fixed or configurable divider
883 u8 self
; /* is it the base factor clock? (only one) */
884 u8 fixed
; /* is it a fixed divisor? if not... */
885 struct clk_div_table
*table
; /* is it a table based divisor? */
886 u8 shift
; /* otherwise it's a normal divisor with this shift */
887 u8 pow
; /* is it power-of-two based? */
888 u8 gate
; /* is it independently gateable? */
890 } div
[SUNXI_DIVS_MAX_QTY
];
893 static struct clk_div_table pll6_sata_tbl
[] = {
894 { .val
= 0, .div
= 6, },
895 { .val
= 1, .div
= 12, },
896 { .val
= 2, .div
= 18, },
897 { .val
= 3, .div
= 24, },
901 static const struct divs_data pll5_divs_data __initconst
= {
902 .factors
= &sun4i_pll5_data
,
905 /* Protect PLL5_DDR */
906 { .shift
= 0, .pow
= 0, .critical
= true }, /* M, DDR */
907 { .shift
= 16, .pow
= 1, }, /* P, other */
908 /* No output for the base factor clock */
912 static const struct divs_data pll6_divs_data __initconst
= {
913 .factors
= &sun4i_pll5_data
,
916 { .shift
= 0, .table
= pll6_sata_tbl
, .gate
= 14 }, /* M, SATA */
917 { .fixed
= 2 }, /* P, other */
918 { .self
= 1 }, /* base factor clock, 2x */
919 { .fixed
= 4 }, /* pll6 / 4, used as ahb input */
923 static const struct divs_data sun6i_a31_pll6_divs_data __initconst
= {
924 .factors
= &sun6i_a31_pll6_data
,
927 { .fixed
= 2 }, /* normal output */
928 { .self
= 1 }, /* base factor clock, 2x */
933 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
935 * These clocks look something like this
936 * ________________________
937 * | ___divisor 1---|----> to consumer
938 * parent >--| pll___/___divisor 2---|----> to consumer
939 * | \_______________|____> to consumer
940 * |________________________|
943 static struct clk
** __init
sunxi_divs_clk_setup(struct device_node
*node
,
944 const struct divs_data
*data
)
946 struct clk_onecell_data
*clk_data
;
948 const char *clk_name
;
949 struct clk
**clks
, *pclk
;
950 struct clk_hw
*gate_hw
, *rate_hw
;
951 const struct clk_ops
*rate_ops
;
952 struct clk_gate
*gate
= NULL
;
953 struct clk_fixed_factor
*fix_factor
;
954 struct clk_divider
*divider
;
955 struct factors_data factors
= *data
->factors
;
956 char *derived_name
= NULL
;
958 int ndivs
= SUNXI_DIVS_MAX_QTY
, i
= 0;
961 /* if number of children known, use it */
965 /* Try to find a name for base factor clock */
966 for (i
= 0; i
< ndivs
; i
++) {
967 if (data
->div
[i
].self
) {
968 of_property_read_string_index(node
, "clock-output-names",
973 /* If we don't have a .self clk use the first output-name up to '_' */
974 if (factors
.name
== NULL
) {
977 of_property_read_string_index(node
, "clock-output-names",
979 endp
= strchr(clk_name
, '_');
981 derived_name
= kstrndup(clk_name
, endp
- clk_name
,
985 factors
.name
= derived_name
;
987 factors
.name
= clk_name
;
991 /* Set up factor clock that we will be dividing */
992 pclk
= sunxi_factors_clk_setup(node
, &factors
);
996 parent
= __clk_get_name(pclk
);
999 reg
= of_iomap(node
, 0);
1001 pr_err("Could not map registers for divs-clk: %pOF\n", node
);
1005 clk_data
= kmalloc(sizeof(struct clk_onecell_data
), GFP_KERNEL
);
1009 clks
= kcalloc(ndivs
, sizeof(*clks
), GFP_KERNEL
);
1013 clk_data
->clks
= clks
;
1015 /* It's not a good idea to have automatic reparenting changing
1017 clkflags
= !strcmp("pll5", parent
) ? 0 : CLK_SET_RATE_PARENT
;
1019 for (i
= 0; i
< ndivs
; i
++) {
1020 if (of_property_read_string_index(node
, "clock-output-names",
1024 /* If this is the base factor clock, only update clks */
1025 if (data
->div
[i
].self
) {
1026 clk_data
->clks
[i
] = pclk
;
1034 /* If this leaf clock can be gated, create a gate */
1035 if (data
->div
[i
].gate
) {
1036 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
1041 gate
->bit_idx
= data
->div
[i
].gate
;
1042 gate
->lock
= &clk_lock
;
1044 gate_hw
= &gate
->hw
;
1047 /* Leaves can be fixed or configurable divisors */
1048 if (data
->div
[i
].fixed
) {
1049 fix_factor
= kzalloc(sizeof(*fix_factor
), GFP_KERNEL
);
1053 fix_factor
->mult
= 1;
1054 fix_factor
->div
= data
->div
[i
].fixed
;
1056 rate_hw
= &fix_factor
->hw
;
1057 rate_ops
= &clk_fixed_factor_ops
;
1059 divider
= kzalloc(sizeof(*divider
), GFP_KERNEL
);
1063 flags
= data
->div
[i
].pow
? CLK_DIVIDER_POWER_OF_TWO
: 0;
1066 divider
->shift
= data
->div
[i
].shift
;
1067 divider
->width
= SUNXI_DIVISOR_WIDTH
;
1068 divider
->flags
= flags
;
1069 divider
->lock
= &clk_lock
;
1070 divider
->table
= data
->div
[i
].table
;
1072 rate_hw
= ÷r
->hw
;
1073 rate_ops
= &clk_divider_ops
;
1076 /* Wrap the (potential) gate and the divisor on a composite
1077 * clock to unify them */
1078 clks
[i
] = clk_register_composite(NULL
, clk_name
, &parent
, 1,
1081 gate_hw
, &clk_gate_ops
,
1083 (data
->div
[i
].critical
?
1084 CLK_IS_CRITICAL
: 0));
1086 WARN_ON(IS_ERR(clk_data
->clks
[i
]));
1089 /* Adjust to the real max */
1090 clk_data
->clk_num
= i
;
1092 if (of_clk_add_provider(node
, of_clk_src_onecell_get
, clk_data
)) {
1093 pr_err("%s: failed to add clock provider for %s\n",
1094 __func__
, clk_name
);
1110 static void __init
sun4i_pll5_clk_setup(struct device_node
*node
)
1112 sunxi_divs_clk_setup(node
, &pll5_divs_data
);
1114 CLK_OF_DECLARE(sun4i_pll5
, "allwinner,sun4i-a10-pll5-clk",
1115 sun4i_pll5_clk_setup
);
1117 static void __init
sun4i_pll6_clk_setup(struct device_node
*node
)
1119 sunxi_divs_clk_setup(node
, &pll6_divs_data
);
1121 CLK_OF_DECLARE(sun4i_pll6
, "allwinner,sun4i-a10-pll6-clk",
1122 sun4i_pll6_clk_setup
);
1124 static void __init
sun6i_pll6_clk_setup(struct device_node
*node
)
1126 sunxi_divs_clk_setup(node
, &sun6i_a31_pll6_divs_data
);
1128 CLK_OF_DECLARE(sun6i_pll6
, "allwinner,sun6i-a31-pll6-clk",
1129 sun6i_pll6_clk_setup
);
1134 * rate = parent_rate / (m + 1);
1136 static void sun6i_display_factors(struct factors_request
*req
)
1140 if (req
->rate
> req
->parent_rate
)
1141 req
->rate
= req
->parent_rate
;
1143 m
= DIV_ROUND_UP(req
->parent_rate
, req
->rate
);
1145 req
->rate
= req
->parent_rate
/ m
;
1149 static const struct clk_factors_config sun6i_display_config
= {
1154 static const struct factors_data sun6i_display_data __initconst
= {
1157 .muxmask
= BIT(2) | BIT(1) | BIT(0),
1158 .table
= &sun6i_display_config
,
1159 .getter
= sun6i_display_factors
,
1162 static void __init
sun6i_display_setup(struct device_node
*node
)
1164 sunxi_factors_clk_setup(node
, &sun6i_display_data
);
1166 CLK_OF_DECLARE(sun6i_display
, "allwinner,sun6i-a31-display-clk",
1167 sun6i_display_setup
);