oxnas: delete linux 4.1 support
[openwrt/openwrt.git] / target / linux / sunxi / patches-4.1 / 161-clk-sunxi-add-pll2-for-sun457i.patch
1 From ea6871c5b3a934d0bfe08082e95c3b952f93ef39 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= <emilio@elopez.com.ar>
3 Date: Fri, 18 Jul 2014 15:48:35 -0300
4 Subject: [PATCH] clk: sunxi: PLL2 support for sun4i, sun5i and sun7i
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This patch adds support for PLL2 and derivates on A10 revision B and
10 higher, as well as on sun5i and sun7i SoCs. As this PLL is only used for
11 audio and requires good accuracy, we only support two known good rates.
12
13 Signed-off-by: Emilio López <emilio@elopez.com.ar>
14 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
15 ---
16 drivers/clk/sunxi/Makefile | 1 +
17 drivers/clk/sunxi/clk-a10-pll2.c | 249 +++++++++++++++++++++++++++++++++++++++
18 2 files changed, 250 insertions(+)
19 create mode 100644 drivers/clk/sunxi/clk-a10-pll2.c
20
21 --- a/drivers/clk/sunxi/Makefile
22 +++ b/drivers/clk/sunxi/Makefile
23 @@ -4,6 +4,7 @@
24
25 obj-y += clk-sunxi.o clk-factors.o
26 obj-y += clk-a10-hosc.o
27 +obj-y += clk-a10-pll2.o
28 obj-y += clk-a20-gmac.o
29 obj-y += clk-mod0.o
30 obj-y += clk-sun8i-mbus.o
31 --- /dev/null
32 +++ b/drivers/clk/sunxi/clk-a10-pll2.c
33 @@ -0,0 +1,249 @@
34 +/*
35 + * Copyright 2013 Emilio López
36 + *
37 + * Emilio López <emilio@elopez.com.ar>
38 + *
39 + * This program is free software; you can redistribute it and/or modify
40 + * it under the terms of the GNU General Public License as published by
41 + * the Free Software Foundation; either version 2 of the License, or
42 + * (at your option) any later version.
43 + *
44 + * This program is distributed in the hope that it will be useful,
45 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 + * GNU General Public License for more details.
48 + */
49 +
50 +#include <linux/clk-provider.h>
51 +#include <linux/of.h>
52 +#include <linux/of_address.h>
53 +#include <linux/slab.h>
54 +
55 +#define SUN4I_PLL2_ENABLE 31
56 +#define SUN4I_PLL2_POST_DIV 26
57 +#define SUN4I_PLL2_POST_DIV_MASK 0xF
58 +#define SUN4I_PLL2_N 8
59 +#define SUN4I_PLL2_N_MASK 0x7F
60 +#define SUN4I_PLL2_PRE_DIV 0
61 +#define SUN4I_PLL2_PRE_DIV_MASK 0x1F
62 +
63 +#define SUN4I_PLL2_OUTPUTS 4
64 +
65 +struct sun4i_pll2_clk {
66 + struct clk_hw hw;
67 + void __iomem *reg;
68 +};
69 +
70 +static inline struct sun4i_pll2_clk *to_sun4i_pll2_clk(struct clk_hw *hw)
71 +{
72 + return container_of(hw, struct sun4i_pll2_clk, hw);
73 +}
74 +
75 +static unsigned long sun4i_pll2_1x_recalc_rate(struct clk_hw *hw,
76 + unsigned long parent_rate)
77 +{
78 + struct sun4i_pll2_clk *clk = to_sun4i_pll2_clk(hw);
79 + int n, prediv, postdiv;
80 +
81 + u32 val = readl(clk->reg);
82 + n = (val >> SUN4I_PLL2_N) & SUN4I_PLL2_N_MASK;
83 + prediv = (val >> SUN4I_PLL2_PRE_DIV) & SUN4I_PLL2_PRE_DIV_MASK;
84 + postdiv = (val >> SUN4I_PLL2_POST_DIV) & SUN4I_PLL2_POST_DIV_MASK;
85 +
86 + /* 0 is a special case and means 1 */
87 + if (n == 0)
88 + n = 1;
89 + if (prediv == 0)
90 + prediv = 1;
91 + if (postdiv == 0)
92 + postdiv = 1;
93 +
94 + return ((parent_rate * n) / prediv) / postdiv;
95 +}
96 +
97 +static unsigned long sun4i_pll2_8x_recalc_rate(struct clk_hw *hw,
98 + unsigned long parent_rate)
99 +{
100 + struct sun4i_pll2_clk *clk = to_sun4i_pll2_clk(hw);
101 + int n, prediv;
102 +
103 + u32 val = readl(clk->reg);
104 + n = (val >> SUN4I_PLL2_N) & SUN4I_PLL2_N_MASK;
105 + prediv = (val >> SUN4I_PLL2_PRE_DIV) & SUN4I_PLL2_PRE_DIV_MASK;
106 +
107 + /* 0 is a special case and means 1 */
108 + if (n == 0)
109 + n = 1;
110 + if (prediv == 0)
111 + prediv = 1;
112 +
113 + return ((parent_rate * 2 * n) / prediv);
114 +}
115 +
116 +static unsigned long sun4i_pll2_4x_recalc_rate(struct clk_hw *hw,
117 + unsigned long parent_rate)
118 +{
119 + return sun4i_pll2_8x_recalc_rate(hw, parent_rate / 2);
120 +}
121 +
122 +static unsigned long sun4i_pll2_2x_recalc_rate(struct clk_hw *hw,
123 + unsigned long parent_rate)
124 +{
125 + return sun4i_pll2_8x_recalc_rate(hw, parent_rate / 4);
126 +}
127 +
128 +static long sun4i_pll2_1x_round_rate(struct clk_hw *hw, unsigned long rate,
129 + unsigned long *parent_rate)
130 +{
131 + /*
132 + * There is only two interesting rates for the audio PLL, the
133 + * rest isn't really usable due to accuracy concerns. Therefore,
134 + * we specifically round to those rates here
135 + */
136 + if (rate < 22579200)
137 + return -EINVAL;
138 +
139 + if (rate >= 22579200 && rate < 24576000)
140 + return 22579200;
141 +
142 + return 24576000;
143 +}
144 +
145 +static long sun4i_pll2_8x_round_rate(struct clk_hw *hw, unsigned long rate,
146 + unsigned long *parent_rate)
147 +{
148 + /*
149 + * We should account for the postdiv that we're undoing on PLL2x8,
150 + * which is always 4 in the usable configurations. The division
151 + * by two is done because PLL2x8 also doubles the rate
152 + */
153 + *parent_rate = (rate * 4) / 2;
154 +
155 + return rate;
156 +}
157 +
158 +static long sun4i_pll2_4x_round_rate(struct clk_hw *hw, unsigned long rate,
159 + unsigned long *parent_rate)
160 +{
161 + /* PLL2x4 * 2 = PLL2x8 */
162 + return sun4i_pll2_8x_round_rate(hw, rate * 2, parent_rate);
163 +}
164 +
165 +static long sun4i_pll2_2x_round_rate(struct clk_hw *hw, unsigned long rate,
166 + unsigned long *parent_rate)
167 +{
168 + /* PLL2x2 * 4 = PLL2x8 */
169 + return sun4i_pll2_8x_round_rate(hw, rate * 4, parent_rate);
170 +}
171 +
172 +static int sun4i_pll2_set_rate(struct clk_hw *hw, unsigned long rate,
173 + unsigned long parent_rate)
174 +{
175 + struct sun4i_pll2_clk *clk = to_sun4i_pll2_clk(hw);
176 + u32 val = readl(clk->reg);
177 +
178 + val &= ~(SUN4I_PLL2_N_MASK << SUN4I_PLL2_N);
179 + val &= ~(SUN4I_PLL2_PRE_DIV_MASK << SUN4I_PLL2_PRE_DIV);
180 + val &= ~(SUN4I_PLL2_POST_DIV_MASK << SUN4I_PLL2_POST_DIV);
181 +
182 + val |= (21 << SUN4I_PLL2_PRE_DIV) | (4 << SUN4I_PLL2_POST_DIV);
183 +
184 + if (rate == 22579200)
185 + val |= (79 << SUN4I_PLL2_N);
186 + else if (rate == 24576000)
187 + val |= (86 << SUN4I_PLL2_N);
188 + else
189 + return -EINVAL;
190 +
191 + writel(val, clk->reg);
192 +
193 + return 0;
194 +}
195 +
196 +static struct clk_ops sun4i_pll2_ops_1x = {
197 + .recalc_rate = sun4i_pll2_1x_recalc_rate,
198 + .round_rate = sun4i_pll2_1x_round_rate,
199 + .set_rate = sun4i_pll2_set_rate,
200 +};
201 +
202 +static struct clk_ops sun4i_pll2_ops_2x = {
203 + .recalc_rate = sun4i_pll2_2x_recalc_rate,
204 + .round_rate = sun4i_pll2_2x_round_rate,
205 +};
206 +
207 +static struct clk_ops sun4i_pll2_ops_4x = {
208 + .recalc_rate = sun4i_pll2_4x_recalc_rate,
209 + .round_rate = sun4i_pll2_4x_round_rate,
210 +};
211 +
212 +static struct clk_ops sun4i_pll2_ops_8x = {
213 + .recalc_rate = sun4i_pll2_8x_recalc_rate,
214 + .round_rate = sun4i_pll2_8x_round_rate,
215 +};
216 +
217 +static void __init sun4i_pll2_setup(struct device_node *np)
218 +{
219 + const char *clk_name = np->name, *parent;
220 + struct clk_onecell_data *clk_data;
221 + struct sun4i_pll2_clk *pll2;
222 + struct clk_gate *gate;
223 + struct clk **clks;
224 + void __iomem *reg;
225 +
226 + pll2 = kzalloc(sizeof(*pll2), GFP_KERNEL);
227 + gate = kzalloc(sizeof(*gate), GFP_KERNEL);
228 + clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
229 + clks = kcalloc(SUN4I_PLL2_OUTPUTS, sizeof(struct clk *), GFP_KERNEL);
230 + if (!pll2 || !gate || !clk_data || !clks)
231 + goto free_mem;
232 +
233 + reg = of_iomap(np, 0);
234 + parent = of_clk_get_parent_name(np, 0);
235 + of_property_read_string_index(np, "clock-output-names", 0, &clk_name);
236 +
237 + pll2->reg = reg;
238 + gate->reg = reg;
239 + gate->bit_idx = SUN4I_PLL2_ENABLE;
240 +
241 + /* PLL2, also known as PLL2x1 */
242 + of_property_read_string_index(np, "clock-output-names", 0, &clk_name);
243 + clks[0] = clk_register_composite(NULL, clk_name, &parent, 1, NULL, NULL,
244 + &pll2->hw, &sun4i_pll2_ops_1x,
245 + &gate->hw, &clk_gate_ops, 0);
246 + WARN_ON(IS_ERR(clks[0]));
247 + parent = clk_name;
248 +
249 + /* PLL2x2, 1/4 the rate of PLL2x8 */
250 + of_property_read_string_index(np, "clock-output-names", 1, &clk_name);
251 + clks[1] = clk_register_composite(NULL, clk_name, &parent, 1, NULL, NULL,
252 + &pll2->hw, &sun4i_pll2_ops_2x,
253 + NULL, NULL, CLK_SET_RATE_PARENT);
254 + WARN_ON(IS_ERR(clks[1]));
255 +
256 + /* PLL2x4, 1/2 the rate of PLL2x8 */
257 + of_property_read_string_index(np, "clock-output-names", 2, &clk_name);
258 + clks[2] = clk_register_composite(NULL, clk_name, &parent, 1, NULL, NULL,
259 + &pll2->hw, &sun4i_pll2_ops_4x,
260 + NULL, NULL, CLK_SET_RATE_PARENT);
261 + WARN_ON(IS_ERR(clks[2]));
262 +
263 + /* PLL2x8, double of PLL2 without the post divisor */
264 + of_property_read_string_index(np, "clock-output-names", 3, &clk_name);
265 + clks[3] = clk_register_composite(NULL, clk_name, &parent, 1, NULL, NULL,
266 + &pll2->hw, &sun4i_pll2_ops_8x,
267 + NULL, NULL, CLK_SET_RATE_PARENT);
268 + WARN_ON(IS_ERR(clks[3]));
269 +
270 + clk_data->clks = clks;
271 + clk_data->clk_num = SUN4I_PLL2_OUTPUTS;
272 + of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
273 +
274 + return;
275 +
276 +free_mem:
277 + kfree(pll2);
278 + kfree(gate);
279 + kfree(clk_data);
280 + kfree(clks);
281 +}
282 +CLK_OF_DECLARE(sun4i_pll2, "allwinner,sun4i-a10-b-pll2-clk", sun4i_pll2_setup);