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-provider.h>
18 #include <linux/clkdev.h>
20 #include <linux/of_address.h>
21 #include <linux/reset-controller.h>
22 #include <linux/spinlock.h>
24 #include "clk-factors.h"
26 static DEFINE_SPINLOCK(clk_lock);
28 /* Maximum number of parents our clocks have */
29 #define SUNXI_MAX_PARENTS 5
32 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
33 * PLL1 rate is calculated as follows
34 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
35 * parent_rate is always 24Mhz
38 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
39 u8 *n, u8 *k, u8 *m, u8 *p)
43 /* Normalize value to a 6M multiple */
44 div = *freq / 6000000;
45 *freq = 6000000 * div;
47 /* we were called to round the frequency, we can now return */
51 /* m is always zero for pll1 */
54 /* k is 1 only on these cases */
55 if (*freq >= 768000000 || *freq == 42000000 || *freq == 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(u32 *freq, u32 parent_rate,
90 u8 *n, u8 *k, u8 *m, u8 *p)
93 * We can operate only on MHz, this will make our life easier
96 u32 freq_mhz = *freq / 1000000;
97 u32 parent_freq_mhz = parent_rate / 1000000;
100 * Round down the frequency to the closest multiple of either
103 u32 round_freq_6 = round_down(freq_mhz, 6);
104 u32 round_freq_16 = round_down(freq_mhz, 16);
106 if (round_freq_6 > round_freq_16)
107 freq_mhz = round_freq_6;
109 freq_mhz = round_freq_16;
111 *freq = freq_mhz * 1000000;
114 * If the factors pointer are null, we were just called to
115 * round down the frequency.
121 /* If the frequency is a multiple of 32 MHz, k is always 3 */
122 if (!(freq_mhz % 32))
124 /* If the frequency is a multiple of 9 MHz, k is always 2 */
125 else if (!(freq_mhz % 9))
127 /* If the frequency is a multiple of 8 MHz, k is always 1 */
128 else if (!(freq_mhz % 8))
130 /* Otherwise, we don't use the k factor */
135 * If the frequency is a multiple of 2 but not a multiple of
136 * 3, m is 3. This is the first time we use 6 here, yet we
137 * will use it on several other places.
138 * We use this number because it's the lowest frequency we can
139 * generate (with n = 0, k = 0, m = 3), so every other frequency
140 * somehow relates to this frequency.
142 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
145 * If the frequency is a multiple of 6MHz, but the factor is
148 else if ((freq_mhz / 6) & 1)
150 /* Otherwise, we end up with m = 1 */
154 /* Calculate n thanks to the above factors we already got */
155 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
158 * If n end up being outbound, and that we can still decrease
161 if ((*n + 1) > 31 && (*m + 1) > 1) {
162 *n = (*n + 1) / 2 - 1;
163 *m = (*m + 1) / 2 - 1;
168 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
169 * PLL1 rate is calculated as follows
170 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
171 * parent_rate is always 24Mhz
174 static void sun8i_a23_get_pll1_factors(u32 *freq, u32 parent_rate,
175 u8 *n, u8 *k, u8 *m, u8 *p)
179 /* Normalize value to a 6M multiple */
180 div = *freq / 6000000;
181 *freq = 6000000 * div;
183 /* we were called to round the frequency, we can now return */
187 /* m is always zero for pll1 */
190 /* k is 1 only on these cases */
191 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
196 /* p will be 2 for divs under 20 and odd divs under 32 */
197 if (div < 20 || (div < 32 && (div & 1)))
200 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
201 * of divs between 40-62 */
202 else if (div < 40 || (div < 64 && (div & 2)))
205 /* any other entries have p = 0 */
209 /* calculate a suitable n based on k and p */
216 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
217 * PLL5 rate is calculated as follows
218 * rate = parent_rate * n * (k + 1)
219 * parent_rate is always 24Mhz
222 static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
223 u8 *n, u8 *k, u8 *m, u8 *p)
227 /* Normalize value to a parent_rate multiple (24M) */
228 div = *freq / parent_rate;
229 *freq = parent_rate * div;
231 /* we were called to round the frequency, we can now return */
237 else if (div / 2 < 31)
239 else if (div / 3 < 31)
244 *n = DIV_ROUND_UP(div, (*k+1));
248 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
249 * PLL6 rate is calculated as follows
250 * rate = parent_rate * n * (k + 1) / 2
251 * parent_rate is always 24Mhz
254 static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
255 u8 *n, u8 *k, u8 *m, u8 *p)
260 * We always have 24MHz / 2, so we can just say that our
261 * parent clock is 12MHz.
263 parent_rate = parent_rate / 2;
265 /* Normalize value to a parent_rate multiple (24M / 2) */
266 div = *freq / parent_rate;
267 *freq = parent_rate * div;
269 /* we were called to round the frequency, we can now return */
277 *n = DIV_ROUND_UP(div, (*k+1));
281 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
282 * APB1 rate is calculated as follows
283 * rate = (parent_rate >> p) / (m + 1);
286 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
287 u8 *n, u8 *k, u8 *m, u8 *p)
291 if (parent_rate < *freq)
294 parent_rate = DIV_ROUND_UP(parent_rate, *freq);
297 if (parent_rate > 32)
300 if (parent_rate <= 4)
302 else if (parent_rate <= 8)
304 else if (parent_rate <= 16)
309 calcm = (parent_rate >> calcp) - 1;
311 *freq = (parent_rate >> calcp) / (calcm + 1);
313 /* we were called to round the frequency, we can now return */
324 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
325 * MOD0 rate is calculated as follows
326 * rate = (parent_rate >> p) / (m + 1);
329 static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
330 u8 *n, u8 *k, u8 *m, u8 *p)
332 u8 div, calcm, calcp;
334 /* These clocks can only divide, so we will never be able to achieve
335 * frequencies higher than the parent frequency */
336 if (*freq > parent_rate)
339 div = DIV_ROUND_UP(parent_rate, *freq);
343 else if (div / 2 < 16)
345 else if (div / 4 < 16)
350 calcm = DIV_ROUND_UP(div, 1 << calcp);
352 *freq = (parent_rate >> calcp) / calcm;
354 /* we were called to round the frequency, we can now return */
365 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
366 * CLK_OUT rate is calculated as follows
367 * rate = (parent_rate >> p) / (m + 1);
370 static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
371 u8 *n, u8 *k, u8 *m, u8 *p)
373 u8 div, calcm, calcp;
375 /* These clocks can only divide, so we will never be able to achieve
376 * frequencies higher than the parent frequency */
377 if (*freq > parent_rate)
380 div = DIV_ROUND_UP(parent_rate, *freq);
384 else if (div / 2 < 32)
386 else if (div / 4 < 32)
391 calcm = DIV_ROUND_UP(div, 1 << calcp);
393 *freq = (parent_rate >> calcp) / calcm;
395 /* we were called to round the frequency, we can now return */
404 * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
407 void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
409 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
410 #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
412 struct clk_hw *hw = __clk_get_hw(clk);
413 struct clk_composite *composite = to_clk_composite(hw);
414 struct clk_hw *rate_hw = composite->rate_hw;
415 struct clk_factors *factors = to_clk_factors(rate_hw);
416 unsigned long flags = 0;
420 spin_lock_irqsave(factors->lock, flags);
422 reg = readl(factors->reg);
424 /* set sample clock phase control */
426 reg |= ((sample & 0x7) << 20);
428 /* set output clock phase control */
430 reg |= ((output & 0x7) << 8);
432 writel(reg, factors->reg);
435 spin_unlock_irqrestore(factors->lock, flags);
437 EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
441 * sunxi_factors_clk_setup() - Setup function for factor clocks
444 static struct clk_factors_config sun4i_pll1_config = {
455 static struct clk_factors_config sun6i_a31_pll1_config = {
464 static struct clk_factors_config sun8i_a23_pll1_config = {
476 static struct clk_factors_config sun4i_pll5_config = {
483 static struct clk_factors_config sun6i_a31_pll6_config = {
490 static struct clk_factors_config sun4i_apb1_config = {
497 /* user manual says "n" but it's really "p" */
498 static struct clk_factors_config sun4i_mod0_config = {
505 /* user manual says "n" but it's really "p" */
506 static struct clk_factors_config sun7i_a20_out_config = {
513 static const struct factors_data sun4i_pll1_data __initconst = {
515 .table = &sun4i_pll1_config,
516 .getter = sun4i_get_pll1_factors,
519 static const struct factors_data sun6i_a31_pll1_data __initconst = {
521 .table = &sun6i_a31_pll1_config,
522 .getter = sun6i_a31_get_pll1_factors,
525 static const struct factors_data sun8i_a23_pll1_data __initconst = {
527 .table = &sun8i_a23_pll1_config,
528 .getter = sun8i_a23_get_pll1_factors,
531 static const struct factors_data sun7i_a20_pll4_data __initconst = {
533 .table = &sun4i_pll5_config,
534 .getter = sun4i_get_pll5_factors,
537 static const struct factors_data sun4i_pll5_data __initconst = {
539 .table = &sun4i_pll5_config,
540 .getter = sun4i_get_pll5_factors,
544 static const struct factors_data sun4i_pll6_data __initconst = {
546 .table = &sun4i_pll5_config,
547 .getter = sun4i_get_pll5_factors,
551 static const struct factors_data sun6i_a31_pll6_data __initconst = {
553 .table = &sun6i_a31_pll6_config,
554 .getter = sun6i_a31_get_pll6_factors,
557 static const struct factors_data sun4i_apb1_data __initconst = {
558 .table = &sun4i_apb1_config,
559 .getter = sun4i_get_apb1_factors,
562 static const struct factors_data sun4i_mod0_data __initconst = {
565 .table = &sun4i_mod0_config,
566 .getter = sun4i_get_mod0_factors,
569 static const struct factors_data sun7i_a20_out_data __initconst = {
572 .table = &sun7i_a20_out_config,
573 .getter = sun7i_a20_get_out_factors,
576 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
577 const struct factors_data *data)
579 return sunxi_factors_register(node, data, &clk_lock);
585 * sunxi_mux_clk_setup() - Setup function for muxes
588 #define SUNXI_MUX_GATE_WIDTH 2
594 static const struct mux_data sun4i_cpu_mux_data __initconst = {
598 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
602 static const struct mux_data sun4i_apb1_mux_data __initconst = {
606 static void __init sunxi_mux_clk_setup(struct device_node *node,
607 struct mux_data *data)
610 const char *clk_name = node->name;
611 const char *parents[SUNXI_MAX_PARENTS];
615 reg = of_iomap(node, 0);
617 while (i < SUNXI_MAX_PARENTS &&
618 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
621 of_property_read_string(node, "clock-output-names", &clk_name);
623 clk = clk_register_mux(NULL, clk_name, parents, i,
624 CLK_SET_RATE_NO_REPARENT, reg,
625 data->shift, SUNXI_MUX_GATE_WIDTH,
629 of_clk_add_provider(node, of_clk_src_simple_get, clk);
630 clk_register_clkdev(clk, clk_name, NULL);
637 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
644 const struct clk_div_table *table;
647 static const struct div_data sun4i_axi_data __initconst = {
653 static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
654 { .val = 0, .div = 1 },
655 { .val = 1, .div = 2 },
656 { .val = 2, .div = 3 },
657 { .val = 3, .div = 4 },
658 { .val = 4, .div = 4 },
659 { .val = 5, .div = 4 },
660 { .val = 6, .div = 4 },
661 { .val = 7, .div = 4 },
665 static const struct div_data sun8i_a23_axi_data __initconst = {
667 .table = sun8i_a23_axi_table,
670 static const struct div_data sun4i_ahb_data __initconst = {
676 static const struct clk_div_table sun4i_apb0_table[] __initconst = {
677 { .val = 0, .div = 2 },
678 { .val = 1, .div = 2 },
679 { .val = 2, .div = 4 },
680 { .val = 3, .div = 8 },
684 static const struct div_data sun4i_apb0_data __initconst = {
688 .table = sun4i_apb0_table,
691 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
697 static void __init sunxi_divider_clk_setup(struct device_node *node,
698 struct div_data *data)
701 const char *clk_name = node->name;
702 const char *clk_parent;
705 reg = of_iomap(node, 0);
707 clk_parent = of_clk_get_parent_name(node, 0);
709 of_property_read_string(node, "clock-output-names", &clk_name);
711 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
712 reg, data->shift, data->width,
713 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
714 data->table, &clk_lock);
716 of_clk_add_provider(node, of_clk_src_simple_get, clk);
717 clk_register_clkdev(clk, clk_name, NULL);
724 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
727 struct gates_reset_data {
730 struct reset_controller_dev rcdev;
733 static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
736 struct gates_reset_data *data = container_of(rcdev,
737 struct gates_reset_data,
742 spin_lock_irqsave(data->lock, flags);
744 reg = readl(data->reg);
745 writel(reg & ~BIT(id), data->reg);
747 spin_unlock_irqrestore(data->lock, flags);
752 static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
755 struct gates_reset_data *data = container_of(rcdev,
756 struct gates_reset_data,
761 spin_lock_irqsave(data->lock, flags);
763 reg = readl(data->reg);
764 writel(reg | BIT(id), data->reg);
766 spin_unlock_irqrestore(data->lock, flags);
771 static struct reset_control_ops sunxi_gates_reset_ops = {
772 .assert = sunxi_gates_reset_assert,
773 .deassert = sunxi_gates_reset_deassert,
777 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
780 #define SUNXI_GATES_MAX_SIZE 64
783 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
787 static const struct gates_data sun4i_axi_gates_data __initconst = {
791 static const struct gates_data sun4i_ahb_gates_data __initconst = {
792 .mask = {0x7F77FFF, 0x14FB3F},
795 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
796 .mask = {0x147667e7, 0x185915},
799 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
800 .mask = {0x107067e7, 0x185111},
803 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
804 .mask = {0xEDFE7F62, 0x794F931},
807 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
808 .mask = { 0x12f77fff, 0x16ff3f },
811 static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
812 .mask = {0x25386742, 0x2505111},
815 static const struct gates_data sun4i_apb0_gates_data __initconst = {
819 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
823 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
827 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
831 static const struct gates_data sun4i_apb1_gates_data __initconst = {
835 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
839 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
843 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
847 static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
851 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
855 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
856 .mask = { 0xff80ff },
859 static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
863 static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
868 static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
873 static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
874 .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
875 .reset_mask = BIT(2) | BIT(1) | BIT(0),
878 static void __init sunxi_gates_clk_setup(struct device_node *node,
879 struct gates_data *data)
881 struct clk_onecell_data *clk_data;
882 struct gates_reset_data *reset_data;
883 const char *clk_parent;
884 const char *clk_name;
890 reg = of_iomap(node, 0);
892 clk_parent = of_clk_get_parent_name(node, 0);
894 /* Worst-case size approximation and memory allocation */
895 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
896 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
899 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
900 if (!clk_data->clks) {
905 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
906 of_property_read_string_index(node, "clock-output-names",
909 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
911 reg + 4 * (i/32), i % 32,
913 WARN_ON(IS_ERR(clk_data->clks[i]));
914 clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
919 /* Adjust to the real max */
920 clk_data->clk_num = i;
922 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
924 /* Register a reset controler for gates with reset bits */
925 if (data->reset_mask == 0)
928 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
932 reset_data->reg = reg;
933 reset_data->lock = &clk_lock;
934 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
935 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
936 reset_data->rcdev.of_node = node;
937 reset_controller_register(&reset_data->rcdev);
943 * sunxi_divs_clk_setup() helper data
946 #define SUNXI_DIVS_MAX_QTY 2
947 #define SUNXI_DIVISOR_WIDTH 2
950 const struct factors_data *factors; /* data for the factor clock */
952 u8 fixed; /* is it a fixed divisor? if not... */
953 struct clk_div_table *table; /* is it a table based divisor? */
954 u8 shift; /* otherwise it's a normal divisor with this shift */
955 u8 pow; /* is it power-of-two based? */
956 u8 gate; /* is it independently gateable? */
957 } div[SUNXI_DIVS_MAX_QTY];
960 static struct clk_div_table pll6_sata_tbl[] = {
961 { .val = 0, .div = 6, },
962 { .val = 1, .div = 12, },
963 { .val = 2, .div = 18, },
964 { .val = 3, .div = 24, },
968 static const struct divs_data pll5_divs_data __initconst = {
969 .factors = &sun4i_pll5_data,
971 { .shift = 0, .pow = 0, }, /* M, DDR */
972 { .shift = 16, .pow = 1, }, /* P, other */
976 static const struct divs_data pll6_divs_data __initconst = {
977 .factors = &sun4i_pll6_data,
979 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
980 { .fixed = 2 }, /* P, other */
985 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
987 * These clocks look something like this
988 * ________________________
989 * | ___divisor 1---|----> to consumer
990 * parent >--| pll___/___divisor 2---|----> to consumer
991 * | \_______________|____> to consumer
992 * |________________________|
995 static void __init sunxi_divs_clk_setup(struct device_node *node,
996 struct divs_data *data)
998 struct clk_onecell_data *clk_data;
1000 const char *clk_name;
1001 struct clk **clks, *pclk;
1002 struct clk_hw *gate_hw, *rate_hw;
1003 const struct clk_ops *rate_ops;
1004 struct clk_gate *gate = NULL;
1005 struct clk_fixed_factor *fix_factor;
1006 struct clk_divider *divider;
1009 int flags, clkflags;
1011 /* Set up factor clock that we will be dividing */
1012 pclk = sunxi_factors_clk_setup(node, data->factors);
1013 parent = __clk_get_name(pclk);
1015 reg = of_iomap(node, 0);
1017 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1021 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
1025 clk_data->clks = clks;
1027 /* It's not a good idea to have automatic reparenting changing
1029 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1031 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1032 if (of_property_read_string_index(node, "clock-output-names",
1040 /* If this leaf clock can be gated, create a gate */
1041 if (data->div[i].gate) {
1042 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1047 gate->bit_idx = data->div[i].gate;
1048 gate->lock = &clk_lock;
1050 gate_hw = &gate->hw;
1053 /* Leaves can be fixed or configurable divisors */
1054 if (data->div[i].fixed) {
1055 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1059 fix_factor->mult = 1;
1060 fix_factor->div = data->div[i].fixed;
1062 rate_hw = &fix_factor->hw;
1063 rate_ops = &clk_fixed_factor_ops;
1065 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1069 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1072 divider->shift = data->div[i].shift;
1073 divider->width = SUNXI_DIVISOR_WIDTH;
1074 divider->flags = flags;
1075 divider->lock = &clk_lock;
1076 divider->table = data->div[i].table;
1078 rate_hw = ÷r->hw;
1079 rate_ops = &clk_divider_ops;
1082 /* Wrap the (potential) gate and the divisor on a composite
1083 * clock to unify them */
1084 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1087 gate_hw, &clk_gate_ops,
1090 WARN_ON(IS_ERR(clk_data->clks[i]));
1091 clk_register_clkdev(clks[i], clk_name, NULL);
1094 /* The last clock available on the getter is the parent */
1097 /* Adjust to the real max */
1098 clk_data->clk_num = i;
1100 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1114 /* Matches for factors clocks */
1115 static const struct of_device_id clk_factors_match[] __initconst = {
1116 {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
1117 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
1118 {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
1119 {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
1120 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
1121 {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1122 {.compatible = "allwinner,sun5i-a13-mbus-clk", .data = &sun4i_mod0_data,},
1123 {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
1124 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
1128 /* Matches for divider clocks */
1129 static const struct of_device_id clk_div_match[] __initconst = {
1130 {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
1131 {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
1132 {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1133 {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
1134 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
1138 /* Matches for divided outputs */
1139 static const struct of_device_id clk_divs_match[] __initconst = {
1140 {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1141 {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
1145 /* Matches for mux clocks */
1146 static const struct of_device_id clk_mux_match[] __initconst = {
1147 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1148 {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
1149 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
1153 /* Matches for gate clocks */
1154 static const struct of_device_id clk_gates_match[] __initconst = {
1155 {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1156 {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
1157 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
1158 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
1159 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
1160 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
1161 {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
1162 {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
1163 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
1164 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
1165 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
1166 {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
1167 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
1168 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
1169 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
1170 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
1171 {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
1172 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
1173 {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
1174 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1175 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
1176 {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
1180 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1183 struct device_node *np;
1184 const struct div_data *data;
1185 const struct of_device_id *match;
1186 void (*setup_function)(struct device_node *, const void *) = function;
1188 for_each_matching_node_and_match(np, clk_match, &match) {
1190 setup_function(np, data);
1194 static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
1198 /* Register factor clocks */
1199 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1201 /* Register divider clocks */
1202 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1204 /* Register divided output clocks */
1205 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1207 /* Register mux clocks */
1208 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
1210 /* Register gate clocks */
1211 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
1213 /* Protect the clocks that needs to stay on */
1214 for (i = 0; i < nclocks; i++) {
1215 struct clk *clk = clk_get(NULL, clocks[i]);
1218 clk_prepare_enable(clk);
1222 static const char *sun4i_a10_critical_clocks[] __initdata = {
1227 static void __init sun4i_a10_init_clocks(struct device_node *node)
1229 sunxi_init_clocks(sun4i_a10_critical_clocks,
1230 ARRAY_SIZE(sun4i_a10_critical_clocks));
1232 CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
1234 static const char *sun5i_critical_clocks[] __initdata = {
1240 static void __init sun5i_init_clocks(struct device_node *node)
1242 sunxi_init_clocks(sun5i_critical_clocks,
1243 ARRAY_SIZE(sun5i_critical_clocks));
1245 CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
1246 CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
1247 CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
1249 static const char *sun6i_critical_clocks[] __initdata = {
1254 static void __init sun6i_init_clocks(struct device_node *node)
1256 sunxi_init_clocks(sun6i_critical_clocks,
1257 ARRAY_SIZE(sun6i_critical_clocks));
1259 CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
1260 CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);