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>
23 #include "clk-factors.h"
25 static DEFINE_SPINLOCK(clk_lock);
27 /* Maximum number of parents our clocks have */
28 #define SUNXI_MAX_PARENTS 5
31 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
32 * PLL1 rate is calculated as follows
33 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
34 * parent_rate is always 24Mhz
37 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
38 u8 *n, u8 *k, u8 *m, u8 *p)
42 /* Normalize value to a 6M multiple */
43 div = *freq / 6000000;
44 *freq = 6000000 * div;
46 /* we were called to round the frequency, we can now return */
50 /* m is always zero for pll1 */
53 /* k is 1 only on these cases */
54 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
59 /* p will be 3 for divs under 10 */
63 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
64 else if (div < 20 || (div < 32 && (div & 1)))
67 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
68 * of divs between 40-62 */
69 else if (div < 40 || (div < 64 && (div & 2)))
72 /* any other entries have p = 0 */
76 /* calculate a suitable n based on k and p */
83 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
84 * PLL1 rate is calculated as follows
85 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
86 * parent_rate should always be 24MHz
88 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
89 u8 *n, u8 *k, u8 *m, u8 *p)
92 * We can operate only on MHz, this will make our life easier
95 u32 freq_mhz = *freq / 1000000;
96 u32 parent_freq_mhz = 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 *freq = freq_mhz * 1000000;
113 * If the factors pointer are null, we were just called to
114 * round down the frequency.
120 /* If the frequency is a multiple of 32 MHz, k is always 3 */
121 if (!(freq_mhz % 32))
123 /* If the frequency is a multiple of 9 MHz, k is always 2 */
124 else if (!(freq_mhz % 9))
126 /* If the frequency is a multiple of 8 MHz, k is always 1 */
127 else if (!(freq_mhz % 8))
129 /* Otherwise, we don't use the k factor */
134 * If the frequency is a multiple of 2 but not a multiple of
135 * 3, m is 3. This is the first time we use 6 here, yet we
136 * will use it on several other places.
137 * We use this number because it's the lowest frequency we can
138 * generate (with n = 0, k = 0, m = 3), so every other frequency
139 * somehow relates to this frequency.
141 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
144 * If the frequency is a multiple of 6MHz, but the factor is
147 else if ((freq_mhz / 6) & 1)
149 /* Otherwise, we end up with m = 1 */
153 /* Calculate n thanks to the above factors we already got */
154 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
157 * If n end up being outbound, and that we can still decrease
160 if ((*n + 1) > 31 && (*m + 1) > 1) {
161 *n = (*n + 1) / 2 - 1;
162 *m = (*m + 1) / 2 - 1;
167 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
168 * PLL1 rate is calculated as follows
169 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
170 * parent_rate is always 24Mhz
173 static void sun8i_a23_get_pll1_factors(u32 *freq, u32 parent_rate,
174 u8 *n, u8 *k, u8 *m, u8 *p)
178 /* Normalize value to a 6M multiple */
179 div = *freq / 6000000;
180 *freq = 6000000 * div;
182 /* we were called to round the frequency, we can now return */
186 /* m is always zero for pll1 */
189 /* k is 1 only on these cases */
190 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
195 /* p will be 2 for divs under 20 and odd divs under 32 */
196 if (div < 20 || (div < 32 && (div & 1)))
199 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
200 * of divs between 40-62 */
201 else if (div < 40 || (div < 64 && (div & 2)))
204 /* any other entries have p = 0 */
208 /* calculate a suitable n based on k and p */
215 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
216 * PLL5 rate is calculated as follows
217 * rate = parent_rate * n * (k + 1)
218 * parent_rate is always 24Mhz
221 static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
222 u8 *n, u8 *k, u8 *m, u8 *p)
226 /* Normalize value to a parent_rate multiple (24M) */
227 div = *freq / parent_rate;
228 *freq = parent_rate * div;
230 /* we were called to round the frequency, we can now return */
236 else if (div / 2 < 31)
238 else if (div / 3 < 31)
243 *n = DIV_ROUND_UP(div, (*k+1));
247 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6
248 * PLL6 rate is calculated as follows
249 * rate = parent_rate * n * (k + 1) / 2
250 * parent_rate is always 24Mhz
253 static void sun6i_a31_get_pll6_factors(u32 *freq, u32 parent_rate,
254 u8 *n, u8 *k, u8 *m, u8 *p)
259 * We always have 24MHz / 2, so we can just say that our
260 * parent clock is 12MHz.
262 parent_rate = parent_rate / 2;
264 /* Normalize value to a parent_rate multiple (24M / 2) */
265 div = *freq / parent_rate;
266 *freq = parent_rate * div;
268 /* we were called to round the frequency, we can now return */
276 *n = DIV_ROUND_UP(div, (*k+1));
280 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
281 * APB1 rate is calculated as follows
282 * rate = (parent_rate >> p) / (m + 1);
285 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
286 u8 *n, u8 *k, u8 *m, u8 *p)
290 if (parent_rate < *freq)
293 parent_rate = DIV_ROUND_UP(parent_rate, *freq);
296 if (parent_rate > 32)
299 if (parent_rate <= 4)
301 else if (parent_rate <= 8)
303 else if (parent_rate <= 16)
308 calcm = (parent_rate >> calcp) - 1;
310 *freq = (parent_rate >> calcp) / (calcm + 1);
312 /* we were called to round the frequency, we can now return */
323 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
324 * MOD0 rate is calculated as follows
325 * rate = (parent_rate >> p) / (m + 1);
328 static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
329 u8 *n, u8 *k, u8 *m, u8 *p)
331 u8 div, calcm, calcp;
333 /* These clocks can only divide, so we will never be able to achieve
334 * frequencies higher than the parent frequency */
335 if (*freq > parent_rate)
338 div = DIV_ROUND_UP(parent_rate, *freq);
342 else if (div / 2 < 16)
344 else if (div / 4 < 16)
349 calcm = DIV_ROUND_UP(div, 1 << calcp);
351 *freq = (parent_rate >> calcp) / calcm;
353 /* we were called to round the frequency, we can now return */
364 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
365 * CLK_OUT rate is calculated as follows
366 * rate = (parent_rate >> p) / (m + 1);
369 static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
370 u8 *n, u8 *k, u8 *m, u8 *p)
372 u8 div, calcm, calcp;
374 /* These clocks can only divide, so we will never be able to achieve
375 * frequencies higher than the parent frequency */
376 if (*freq > parent_rate)
379 div = DIV_ROUND_UP(parent_rate, *freq);
383 else if (div / 2 < 32)
385 else if (div / 4 < 32)
390 calcm = DIV_ROUND_UP(div, 1 << calcp);
392 *freq = (parent_rate >> calcp) / calcm;
394 /* we were called to round the frequency, we can now return */
403 * clk_sunxi_mmc_phase_control() - configures MMC clock phase control
406 void clk_sunxi_mmc_phase_control(struct clk *clk, u8 sample, u8 output)
408 #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
409 #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
411 struct clk_hw *hw = __clk_get_hw(clk);
412 struct clk_composite *composite = to_clk_composite(hw);
413 struct clk_hw *rate_hw = composite->rate_hw;
414 struct clk_factors *factors = to_clk_factors(rate_hw);
415 unsigned long flags = 0;
419 spin_lock_irqsave(factors->lock, flags);
421 reg = readl(factors->reg);
423 /* set sample clock phase control */
425 reg |= ((sample & 0x7) << 20);
427 /* set output clock phase control */
429 reg |= ((output & 0x7) << 8);
431 writel(reg, factors->reg);
434 spin_unlock_irqrestore(factors->lock, flags);
436 EXPORT_SYMBOL(clk_sunxi_mmc_phase_control);
440 * sunxi_factors_clk_setup() - Setup function for factor clocks
443 #define SUNXI_FACTORS_MUX_MASK 0x3
445 struct factors_data {
448 struct clk_factors_config *table;
449 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
453 static struct clk_factors_config sun4i_pll1_config = {
464 static struct clk_factors_config sun6i_a31_pll1_config = {
473 static struct clk_factors_config sun8i_a23_pll1_config = {
485 static struct clk_factors_config sun4i_pll5_config = {
492 static struct clk_factors_config sun6i_a31_pll6_config = {
499 static struct clk_factors_config sun4i_apb1_config = {
506 /* user manual says "n" but it's really "p" */
507 static struct clk_factors_config sun4i_mod0_config = {
514 /* user manual says "n" but it's really "p" */
515 static struct clk_factors_config sun7i_a20_out_config = {
522 static const struct factors_data sun4i_pll1_data __initconst = {
524 .table = &sun4i_pll1_config,
525 .getter = sun4i_get_pll1_factors,
528 static const struct factors_data sun6i_a31_pll1_data __initconst = {
530 .table = &sun6i_a31_pll1_config,
531 .getter = sun6i_a31_get_pll1_factors,
534 static const struct factors_data sun8i_a23_pll1_data __initconst = {
536 .table = &sun8i_a23_pll1_config,
537 .getter = sun8i_a23_get_pll1_factors,
540 static const struct factors_data sun7i_a20_pll4_data __initconst = {
542 .table = &sun4i_pll5_config,
543 .getter = sun4i_get_pll5_factors,
546 static const struct factors_data sun4i_pll5_data __initconst = {
548 .table = &sun4i_pll5_config,
549 .getter = sun4i_get_pll5_factors,
553 static const struct factors_data sun4i_pll6_data __initconst = {
555 .table = &sun4i_pll5_config,
556 .getter = sun4i_get_pll5_factors,
560 static const struct factors_data sun6i_a31_pll6_data __initconst = {
562 .table = &sun6i_a31_pll6_config,
563 .getter = sun6i_a31_get_pll6_factors,
566 static const struct factors_data sun4i_apb1_data __initconst = {
567 .table = &sun4i_apb1_config,
568 .getter = sun4i_get_apb1_factors,
571 static const struct factors_data sun4i_mod0_data __initconst = {
574 .table = &sun4i_mod0_config,
575 .getter = sun4i_get_mod0_factors,
578 static const struct factors_data sun7i_a20_out_data __initconst = {
581 .table = &sun7i_a20_out_config,
582 .getter = sun7i_a20_get_out_factors,
585 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
586 const struct factors_data *data)
589 struct clk_factors *factors;
590 struct clk_gate *gate = NULL;
591 struct clk_mux *mux = NULL;
592 struct clk_hw *gate_hw = NULL;
593 struct clk_hw *mux_hw = NULL;
594 const char *clk_name = node->name;
595 const char *parents[SUNXI_MAX_PARENTS];
599 reg = of_iomap(node, 0);
601 /* if we have a mux, we will have >1 parents */
602 while (i < SUNXI_MAX_PARENTS &&
603 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
607 * some factor clocks, such as pll5 and pll6, may have multiple
608 * outputs, and have their name designated in factors_data
611 clk_name = data->name;
613 of_property_read_string(node, "clock-output-names", &clk_name);
615 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
619 /* Add a gate if this factor clock can be gated */
621 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
627 /* set up gate properties */
629 gate->bit_idx = data->enable;
630 gate->lock = &clk_lock;
634 /* Add a mux if this factor clock can be muxed */
636 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
643 /* set up gate properties */
645 mux->shift = data->mux;
646 mux->mask = SUNXI_FACTORS_MUX_MASK;
647 mux->lock = &clk_lock;
651 /* set up factors properties */
653 factors->config = data->table;
654 factors->get_factors = data->getter;
655 factors->lock = &clk_lock;
657 clk = clk_register_composite(NULL, clk_name,
659 mux_hw, &clk_mux_ops,
660 &factors->hw, &clk_factors_ops,
661 gate_hw, &clk_gate_ops, 0);
664 of_clk_add_provider(node, of_clk_src_simple_get, clk);
665 clk_register_clkdev(clk, clk_name, NULL);
674 * sunxi_mux_clk_setup() - Setup function for muxes
677 #define SUNXI_MUX_GATE_WIDTH 2
683 static const struct mux_data sun4i_cpu_mux_data __initconst = {
687 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
691 static const struct mux_data sun4i_apb1_mux_data __initconst = {
695 static void __init sunxi_mux_clk_setup(struct device_node *node,
696 struct mux_data *data)
699 const char *clk_name = node->name;
700 const char *parents[SUNXI_MAX_PARENTS];
704 reg = of_iomap(node, 0);
706 while (i < SUNXI_MAX_PARENTS &&
707 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
710 of_property_read_string(node, "clock-output-names", &clk_name);
712 clk = clk_register_mux(NULL, clk_name, parents, i,
713 CLK_SET_RATE_NO_REPARENT, reg,
714 data->shift, SUNXI_MUX_GATE_WIDTH,
718 of_clk_add_provider(node, of_clk_src_simple_get, clk);
719 clk_register_clkdev(clk, clk_name, NULL);
726 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
733 const struct clk_div_table *table;
736 static const struct div_data sun4i_axi_data __initconst = {
742 static const struct clk_div_table sun8i_a23_axi_table[] __initconst = {
743 { .val = 0, .div = 1 },
744 { .val = 1, .div = 2 },
745 { .val = 2, .div = 3 },
746 { .val = 3, .div = 4 },
747 { .val = 4, .div = 4 },
748 { .val = 5, .div = 4 },
749 { .val = 6, .div = 4 },
750 { .val = 7, .div = 4 },
754 static const struct div_data sun8i_a23_axi_data __initconst = {
756 .table = sun8i_a23_axi_table,
759 static const struct div_data sun4i_ahb_data __initconst = {
765 static const struct div_data sun4i_apb0_data __initconst = {
771 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
777 static void __init sunxi_divider_clk_setup(struct device_node *node,
778 struct div_data *data)
781 const char *clk_name = node->name;
782 const char *clk_parent;
785 reg = of_iomap(node, 0);
787 clk_parent = of_clk_get_parent_name(node, 0);
789 of_property_read_string(node, "clock-output-names", &clk_name);
791 clk = clk_register_divider_table(NULL, clk_name, clk_parent, 0,
792 reg, data->shift, data->width,
793 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
794 data->table, &clk_lock);
796 of_clk_add_provider(node, of_clk_src_simple_get, clk);
797 clk_register_clkdev(clk, clk_name, NULL);
804 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
807 struct gates_reset_data {
810 struct reset_controller_dev rcdev;
813 static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
816 struct gates_reset_data *data = container_of(rcdev,
817 struct gates_reset_data,
822 spin_lock_irqsave(data->lock, flags);
824 reg = readl(data->reg);
825 writel(reg & ~BIT(id), data->reg);
827 spin_unlock_irqrestore(data->lock, flags);
832 static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
835 struct gates_reset_data *data = container_of(rcdev,
836 struct gates_reset_data,
841 spin_lock_irqsave(data->lock, flags);
843 reg = readl(data->reg);
844 writel(reg | BIT(id), data->reg);
846 spin_unlock_irqrestore(data->lock, flags);
851 static struct reset_control_ops sunxi_gates_reset_ops = {
852 .assert = sunxi_gates_reset_assert,
853 .deassert = sunxi_gates_reset_deassert,
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 static const struct gates_data sun4i_axi_gates_data __initconst = {
871 static const struct gates_data sun4i_ahb_gates_data __initconst = {
872 .mask = {0x7F77FFF, 0x14FB3F},
875 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
876 .mask = {0x147667e7, 0x185915},
879 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
880 .mask = {0x107067e7, 0x185111},
883 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
884 .mask = {0xEDFE7F62, 0x794F931},
887 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
888 .mask = { 0x12f77fff, 0x16ff3f },
891 static const struct gates_data sun8i_a23_ahb1_gates_data __initconst = {
892 .mask = {0x25386742, 0x2505111},
895 static const struct gates_data sun4i_apb0_gates_data __initconst = {
899 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
903 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
907 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
911 static const struct gates_data sun4i_apb1_gates_data __initconst = {
915 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
919 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
923 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
927 static const struct gates_data sun8i_a23_apb1_gates_data __initconst = {
931 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
935 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
936 .mask = { 0xff80ff },
939 static const struct gates_data sun8i_a23_apb2_gates_data __initconst = {
943 static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
948 static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
953 static const struct gates_data sun6i_a31_usb_gates_data __initconst = {
954 .mask = { BIT(18) | BIT(17) | BIT(16) | BIT(10) | BIT(9) | BIT(8) },
955 .reset_mask = BIT(2) | BIT(1) | BIT(0),
958 static void __init sunxi_gates_clk_setup(struct device_node *node,
959 struct gates_data *data)
961 struct clk_onecell_data *clk_data;
962 struct gates_reset_data *reset_data;
963 const char *clk_parent;
964 const char *clk_name;
970 reg = of_iomap(node, 0);
972 clk_parent = of_clk_get_parent_name(node, 0);
974 /* Worst-case size approximation and memory allocation */
975 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
976 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
979 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
980 if (!clk_data->clks) {
985 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
986 of_property_read_string_index(node, "clock-output-names",
989 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
991 reg + 4 * (i/32), i % 32,
993 WARN_ON(IS_ERR(clk_data->clks[i]));
994 clk_register_clkdev(clk_data->clks[i], clk_name, NULL);
999 /* Adjust to the real max */
1000 clk_data->clk_num = i;
1002 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1004 /* Register a reset controler for gates with reset bits */
1005 if (data->reset_mask == 0)
1008 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
1012 reset_data->reg = reg;
1013 reset_data->lock = &clk_lock;
1014 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
1015 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
1016 reset_data->rcdev.of_node = node;
1017 reset_controller_register(&reset_data->rcdev);
1023 * sunxi_divs_clk_setup() helper data
1026 #define SUNXI_DIVS_MAX_QTY 2
1027 #define SUNXI_DIVISOR_WIDTH 2
1030 const struct factors_data *factors; /* data for the factor clock */
1032 u8 fixed; /* is it a fixed divisor? if not... */
1033 struct clk_div_table *table; /* is it a table based divisor? */
1034 u8 shift; /* otherwise it's a normal divisor with this shift */
1035 u8 pow; /* is it power-of-two based? */
1036 u8 gate; /* is it independently gateable? */
1037 } div[SUNXI_DIVS_MAX_QTY];
1040 static struct clk_div_table pll6_sata_tbl[] = {
1041 { .val = 0, .div = 6, },
1042 { .val = 1, .div = 12, },
1043 { .val = 2, .div = 18, },
1044 { .val = 3, .div = 24, },
1048 static const struct divs_data pll5_divs_data __initconst = {
1049 .factors = &sun4i_pll5_data,
1051 { .shift = 0, .pow = 0, }, /* M, DDR */
1052 { .shift = 16, .pow = 1, }, /* P, other */
1056 static const struct divs_data pll6_divs_data __initconst = {
1057 .factors = &sun4i_pll6_data,
1059 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
1060 { .fixed = 2 }, /* P, other */
1065 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
1067 * These clocks look something like this
1068 * ________________________
1069 * | ___divisor 1---|----> to consumer
1070 * parent >--| pll___/___divisor 2---|----> to consumer
1071 * | \_______________|____> to consumer
1072 * |________________________|
1075 static void __init sunxi_divs_clk_setup(struct device_node *node,
1076 struct divs_data *data)
1078 struct clk_onecell_data *clk_data;
1080 const char *clk_name;
1081 struct clk **clks, *pclk;
1082 struct clk_hw *gate_hw, *rate_hw;
1083 const struct clk_ops *rate_ops;
1084 struct clk_gate *gate = NULL;
1085 struct clk_fixed_factor *fix_factor;
1086 struct clk_divider *divider;
1089 int flags, clkflags;
1091 /* Set up factor clock that we will be dividing */
1092 pclk = sunxi_factors_clk_setup(node, data->factors);
1093 parent = __clk_get_name(pclk);
1095 reg = of_iomap(node, 0);
1097 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
1101 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
1105 clk_data->clks = clks;
1107 /* It's not a good idea to have automatic reparenting changing
1109 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
1111 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
1112 if (of_property_read_string_index(node, "clock-output-names",
1120 /* If this leaf clock can be gated, create a gate */
1121 if (data->div[i].gate) {
1122 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1127 gate->bit_idx = data->div[i].gate;
1128 gate->lock = &clk_lock;
1130 gate_hw = &gate->hw;
1133 /* Leaves can be fixed or configurable divisors */
1134 if (data->div[i].fixed) {
1135 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1139 fix_factor->mult = 1;
1140 fix_factor->div = data->div[i].fixed;
1142 rate_hw = &fix_factor->hw;
1143 rate_ops = &clk_fixed_factor_ops;
1145 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1149 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1152 divider->shift = data->div[i].shift;
1153 divider->width = SUNXI_DIVISOR_WIDTH;
1154 divider->flags = flags;
1155 divider->lock = &clk_lock;
1156 divider->table = data->div[i].table;
1158 rate_hw = ÷r->hw;
1159 rate_ops = &clk_divider_ops;
1162 /* Wrap the (potential) gate and the divisor on a composite
1163 * clock to unify them */
1164 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1167 gate_hw, &clk_gate_ops,
1170 WARN_ON(IS_ERR(clk_data->clks[i]));
1171 clk_register_clkdev(clks[i], clk_name, NULL);
1174 /* The last clock available on the getter is the parent */
1177 /* Adjust to the real max */
1178 clk_data->clk_num = i;
1180 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1194 /* Matches for factors clocks */
1195 static const struct of_device_id clk_factors_match[] __initconst = {
1196 {.compatible = "allwinner,sun4i-a10-pll1-clk", .data = &sun4i_pll1_data,},
1197 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
1198 {.compatible = "allwinner,sun8i-a23-pll1-clk", .data = &sun8i_a23_pll1_data,},
1199 {.compatible = "allwinner,sun7i-a20-pll4-clk", .data = &sun7i_a20_pll4_data,},
1200 {.compatible = "allwinner,sun6i-a31-pll6-clk", .data = &sun6i_a31_pll6_data,},
1201 {.compatible = "allwinner,sun4i-a10-apb1-clk", .data = &sun4i_apb1_data,},
1202 {.compatible = "allwinner,sun4i-a10-mod0-clk", .data = &sun4i_mod0_data,},
1203 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
1207 /* Matches for divider clocks */
1208 static const struct of_device_id clk_div_match[] __initconst = {
1209 {.compatible = "allwinner,sun4i-a10-axi-clk", .data = &sun4i_axi_data,},
1210 {.compatible = "allwinner,sun8i-a23-axi-clk", .data = &sun8i_a23_axi_data,},
1211 {.compatible = "allwinner,sun4i-a10-ahb-clk", .data = &sun4i_ahb_data,},
1212 {.compatible = "allwinner,sun4i-a10-apb0-clk", .data = &sun4i_apb0_data,},
1213 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
1217 /* Matches for divided outputs */
1218 static const struct of_device_id clk_divs_match[] __initconst = {
1219 {.compatible = "allwinner,sun4i-a10-pll5-clk", .data = &pll5_divs_data,},
1220 {.compatible = "allwinner,sun4i-a10-pll6-clk", .data = &pll6_divs_data,},
1224 /* Matches for mux clocks */
1225 static const struct of_device_id clk_mux_match[] __initconst = {
1226 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
1227 {.compatible = "allwinner,sun4i-a10-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
1228 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
1232 /* Matches for gate clocks */
1233 static const struct of_device_id clk_gates_match[] __initconst = {
1234 {.compatible = "allwinner,sun4i-a10-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1235 {.compatible = "allwinner,sun4i-a10-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
1236 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
1237 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
1238 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
1239 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
1240 {.compatible = "allwinner,sun8i-a23-ahb1-gates-clk", .data = &sun8i_a23_ahb1_gates_data,},
1241 {.compatible = "allwinner,sun4i-a10-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
1242 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
1243 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
1244 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
1245 {.compatible = "allwinner,sun4i-a10-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
1246 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
1247 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
1248 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
1249 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
1250 {.compatible = "allwinner,sun8i-a23-apb1-gates-clk", .data = &sun8i_a23_apb1_gates_data,},
1251 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
1252 {.compatible = "allwinner,sun8i-a23-apb2-gates-clk", .data = &sun8i_a23_apb2_gates_data,},
1253 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1254 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
1255 {.compatible = "allwinner,sun6i-a31-usb-clk", .data = &sun6i_a31_usb_gates_data,},
1259 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1262 struct device_node *np;
1263 const struct div_data *data;
1264 const struct of_device_id *match;
1265 void (*setup_function)(struct device_node *, const void *) = function;
1267 for_each_matching_node_and_match(np, clk_match, &match) {
1269 setup_function(np, data);
1273 static void __init sunxi_init_clocks(const char *clocks[], int nclocks)
1277 /* Register factor clocks */
1278 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1280 /* Register divider clocks */
1281 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1283 /* Register divided output clocks */
1284 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1286 /* Register mux clocks */
1287 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
1289 /* Register gate clocks */
1290 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
1292 /* Protect the clocks that needs to stay on */
1293 for (i = 0; i < nclocks; i++) {
1294 struct clk *clk = clk_get(NULL, clocks[i]);
1297 clk_prepare_enable(clk);
1301 static const char *sun4i_a10_critical_clocks[] __initdata = {
1306 static void __init sun4i_a10_init_clocks(struct device_node *node)
1308 sunxi_init_clocks(sun4i_a10_critical_clocks,
1309 ARRAY_SIZE(sun4i_a10_critical_clocks));
1311 CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sun4i_a10_init_clocks);
1313 static const char *sun5i_critical_clocks[] __initdata = {
1319 static void __init sun5i_init_clocks(struct device_node *node)
1321 sunxi_init_clocks(sun5i_critical_clocks,
1322 ARRAY_SIZE(sun5i_critical_clocks));
1324 CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sun5i_init_clocks);
1325 CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sun5i_init_clocks);
1326 CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sun5i_init_clocks);
1328 static const char *sun6i_critical_clocks[] __initdata = {
1333 static void __init sun6i_init_clocks(struct device_node *node)
1335 sunxi_init_clocks(sun6i_critical_clocks,
1336 ARRAY_SIZE(sun6i_critical_clocks));
1338 CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
1339 CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);