blob: 7ed05f0d629a80c0c0fc7d65aaab82859ed407de [file] [log] [blame]
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +02001/*
2 * clk-flexgen.c
3 *
4 * Copyright (C) ST-Microelectronics SA 2013
5 * Author: Maxime Coquelin <maxime.coquelin@st.com> for ST-Microelectronics.
6 * License terms: GNU General Public License (GPL), version 2 */
7
Stephen Boydd5f728a2015-06-19 15:00:46 -07008#include <linux/clk.h>
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +02009#include <linux/clk-provider.h>
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <linux/io.h>
13#include <linux/err.h>
14#include <linux/string.h>
15#include <linux/of.h>
16#include <linux/of_address.h>
17
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +020018struct clkgen_data {
19 unsigned long flags;
20};
21
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020022struct flexgen {
23 struct clk_hw hw;
24
25 /* Crossbar */
26 struct clk_mux mux;
27 /* Pre-divisor's gate */
28 struct clk_gate pgate;
29 /* Pre-divisor */
30 struct clk_divider pdiv;
31 /* Final divisor's gate */
32 struct clk_gate fgate;
33 /* Final divisor */
34 struct clk_divider fdiv;
35};
36
37#define to_flexgen(_hw) container_of(_hw, struct flexgen, hw)
38
39static int flexgen_enable(struct clk_hw *hw)
40{
41 struct flexgen *flexgen = to_flexgen(hw);
42 struct clk_hw *pgate_hw = &flexgen->pgate.hw;
43 struct clk_hw *fgate_hw = &flexgen->fgate.hw;
44
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010045 __clk_hw_set_clk(pgate_hw, hw);
46 __clk_hw_set_clk(fgate_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020047
48 clk_gate_ops.enable(pgate_hw);
49
50 clk_gate_ops.enable(fgate_hw);
51
Stephen Boyd836ee0f2015-08-12 11:42:23 -070052 pr_debug("%s: flexgen output enabled\n", clk_hw_get_name(hw));
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020053 return 0;
54}
55
56static void flexgen_disable(struct clk_hw *hw)
57{
58 struct flexgen *flexgen = to_flexgen(hw);
59 struct clk_hw *fgate_hw = &flexgen->fgate.hw;
60
61 /* disable only the final gate */
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010062 __clk_hw_set_clk(fgate_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020063
64 clk_gate_ops.disable(fgate_hw);
65
Stephen Boyd836ee0f2015-08-12 11:42:23 -070066 pr_debug("%s: flexgen output disabled\n", clk_hw_get_name(hw));
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020067}
68
69static int flexgen_is_enabled(struct clk_hw *hw)
70{
71 struct flexgen *flexgen = to_flexgen(hw);
72 struct clk_hw *fgate_hw = &flexgen->fgate.hw;
73
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010074 __clk_hw_set_clk(fgate_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020075
76 if (!clk_gate_ops.is_enabled(fgate_hw))
77 return 0;
78
79 return 1;
80}
81
82static u8 flexgen_get_parent(struct clk_hw *hw)
83{
84 struct flexgen *flexgen = to_flexgen(hw);
85 struct clk_hw *mux_hw = &flexgen->mux.hw;
86
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010087 __clk_hw_set_clk(mux_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020088
89 return clk_mux_ops.get_parent(mux_hw);
90}
91
92static int flexgen_set_parent(struct clk_hw *hw, u8 index)
93{
94 struct flexgen *flexgen = to_flexgen(hw);
95 struct clk_hw *mux_hw = &flexgen->mux.hw;
96
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010097 __clk_hw_set_clk(mux_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020098
99 return clk_mux_ops.set_parent(mux_hw, index);
100}
101
102static inline unsigned long
103clk_best_div(unsigned long parent_rate, unsigned long rate)
104{
105 return parent_rate / rate + ((rate > (2*(parent_rate % rate))) ? 0 : 1);
106}
107
108static long flexgen_round_rate(struct clk_hw *hw, unsigned long rate,
109 unsigned long *prate)
110{
111 unsigned long div;
112
113 /* Round div according to exact prate and wished rate */
114 div = clk_best_div(*prate, rate);
115
Stephen Boyd98d8a602015-06-29 16:56:30 -0700116 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200117 *prate = rate * div;
118 return rate;
119 }
120
121 return *prate / div;
122}
123
Stephen Boyd8e6dd772015-05-01 12:45:53 -0700124static unsigned long flexgen_recalc_rate(struct clk_hw *hw,
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200125 unsigned long parent_rate)
126{
127 struct flexgen *flexgen = to_flexgen(hw);
128 struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
129 struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
130 unsigned long mid_rate;
131
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100132 __clk_hw_set_clk(pdiv_hw, hw);
133 __clk_hw_set_clk(fdiv_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200134
135 mid_rate = clk_divider_ops.recalc_rate(pdiv_hw, parent_rate);
136
137 return clk_divider_ops.recalc_rate(fdiv_hw, mid_rate);
138}
139
140static int flexgen_set_rate(struct clk_hw *hw, unsigned long rate,
141 unsigned long parent_rate)
142{
143 struct flexgen *flexgen = to_flexgen(hw);
144 struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
145 struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
Peter Griffinedc30072015-01-20 15:32:41 +0000146 unsigned long div = 0;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200147 int ret = 0;
148
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100149 __clk_hw_set_clk(pdiv_hw, hw);
150 __clk_hw_set_clk(fdiv_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200151
Peter Griffinedc30072015-01-20 15:32:41 +0000152 div = clk_best_div(parent_rate, rate);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200153
Peter Griffinedc30072015-01-20 15:32:41 +0000154 /*
155 * pdiv is mainly targeted for low freq results, while fdiv
156 * should be used for div <= 64. The other way round can
157 * lead to 'duty cycle' issues.
158 */
159
160 if (div <= 64) {
161 clk_divider_ops.set_rate(pdiv_hw, parent_rate, parent_rate);
162 ret = clk_divider_ops.set_rate(fdiv_hw, rate, rate * div);
163 } else {
164 clk_divider_ops.set_rate(fdiv_hw, parent_rate, parent_rate);
165 ret = clk_divider_ops.set_rate(pdiv_hw, rate, rate * div);
166 }
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200167
168 return ret;
169}
170
171static const struct clk_ops flexgen_ops = {
172 .enable = flexgen_enable,
173 .disable = flexgen_disable,
174 .is_enabled = flexgen_is_enabled,
175 .get_parent = flexgen_get_parent,
176 .set_parent = flexgen_set_parent,
177 .round_rate = flexgen_round_rate,
178 .recalc_rate = flexgen_recalc_rate,
179 .set_rate = flexgen_set_rate,
180};
181
Stephen Boyd8e6dd772015-05-01 12:45:53 -0700182static struct clk *clk_register_flexgen(const char *name,
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200183 const char **parent_names, u8 num_parents,
184 void __iomem *reg, spinlock_t *lock, u32 idx,
185 unsigned long flexgen_flags) {
186 struct flexgen *fgxbar;
187 struct clk *clk;
188 struct clk_init_data init;
189 u32 xbar_shift;
190 void __iomem *xbar_reg, *fdiv_reg;
191
192 fgxbar = kzalloc(sizeof(struct flexgen), GFP_KERNEL);
193 if (!fgxbar)
194 return ERR_PTR(-ENOMEM);
195
196 init.name = name;
197 init.ops = &flexgen_ops;
Pankaj Dev18fee452015-06-23 16:09:24 +0200198 init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE | flexgen_flags;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200199 init.parent_names = parent_names;
200 init.num_parents = num_parents;
201
202 xbar_reg = reg + 0x18 + (idx & ~0x3);
203 xbar_shift = (idx % 4) * 0x8;
204 fdiv_reg = reg + 0x164 + idx * 4;
205
206 /* Crossbar element config */
207 fgxbar->mux.lock = lock;
208 fgxbar->mux.mask = BIT(6) - 1;
209 fgxbar->mux.reg = xbar_reg;
210 fgxbar->mux.shift = xbar_shift;
211 fgxbar->mux.table = NULL;
212
213
214 /* Pre-divider's gate config (in xbar register)*/
215 fgxbar->pgate.lock = lock;
216 fgxbar->pgate.reg = xbar_reg;
217 fgxbar->pgate.bit_idx = xbar_shift + 6;
218
219 /* Pre-divider config */
220 fgxbar->pdiv.lock = lock;
221 fgxbar->pdiv.reg = reg + 0x58 + idx * 4;
222 fgxbar->pdiv.width = 10;
223
224 /* Final divider's gate config */
225 fgxbar->fgate.lock = lock;
226 fgxbar->fgate.reg = fdiv_reg;
227 fgxbar->fgate.bit_idx = 6;
228
229 /* Final divider config */
230 fgxbar->fdiv.lock = lock;
231 fgxbar->fdiv.reg = fdiv_reg;
232 fgxbar->fdiv.width = 6;
233
234 fgxbar->hw.init = &init;
235
236 clk = clk_register(NULL, &fgxbar->hw);
237 if (IS_ERR(clk))
238 kfree(fgxbar);
239 else
240 pr_debug("%s: parent %s rate %u\n",
241 __clk_get_name(clk),
242 __clk_get_name(clk_get_parent(clk)),
243 (unsigned int)clk_get_rate(clk));
244 return clk;
245}
246
247static const char ** __init flexgen_get_parents(struct device_node *np,
248 int *num_parents)
249{
250 const char **parents;
Stephen Boydcaeb0572016-02-19 17:43:30 -0800251 unsigned int nparents;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200252
Geert Uytterhoeven0a652392015-05-29 11:25:46 +0200253 nparents = of_clk_get_parent_count(np);
Stephen Boydcaeb0572016-02-19 17:43:30 -0800254 if (WARN_ON(!nparents))
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200255 return NULL;
256
257 parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
258 if (!parents)
259 return NULL;
260
Dinh Nguyen0b4e7f02015-07-06 22:59:04 -0500261 *num_parents = of_clk_parent_fill(np, parents, nparents);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200262
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200263 return parents;
264}
265
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200266static const struct clkgen_data clkgen_audio = {
267 .flags = CLK_SET_RATE_PARENT,
268};
269
270static const struct of_device_id flexgen_of_match[] = {
271 {
272 .compatible = "st,flexgen-audio",
273 .data = &clkgen_audio,
274 },
275 {}
276};
277
Stephen Boyd8e6dd772015-05-01 12:45:53 -0700278static void __init st_of_flexgen_setup(struct device_node *np)
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200279{
280 struct device_node *pnode;
281 void __iomem *reg;
282 struct clk_onecell_data *clk_data;
283 const char **parents;
284 int num_parents, i;
285 spinlock_t *rlock = NULL;
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200286 const struct of_device_id *match;
287 struct clkgen_data *data = NULL;
288 unsigned long flex_flags = 0;
Andrzej Hajdaa1c22a42015-09-24 16:00:16 +0200289 int ret;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200290
291 pnode = of_get_parent(np);
292 if (!pnode)
293 return;
294
295 reg = of_iomap(pnode, 0);
296 if (!reg)
297 return;
298
299 parents = flexgen_get_parents(np, &num_parents);
300 if (!parents)
301 return;
302
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200303 match = of_match_node(flexgen_of_match, np);
304 if (match) {
305 data = (struct clkgen_data *)match->data;
306 flex_flags = data->flags;
307 }
308
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200309 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
310 if (!clk_data)
311 goto err;
312
Andrzej Hajdaa1c22a42015-09-24 16:00:16 +0200313 ret = of_property_count_strings(np, "clock-output-names");
314 if (ret <= 0) {
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200315 pr_err("%s: Failed to get number of output clocks (%d)",
316 __func__, clk_data->clk_num);
317 goto err;
318 }
Andrzej Hajdaa1c22a42015-09-24 16:00:16 +0200319 clk_data->clk_num = ret;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200320
321 clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *),
322 GFP_KERNEL);
323 if (!clk_data->clks)
324 goto err;
325
326 rlock = kzalloc(sizeof(spinlock_t), GFP_KERNEL);
327 if (!rlock)
328 goto err;
329
Giuseppe Cavallaro0f4f2af2015-06-23 16:09:23 +0200330 spin_lock_init(rlock);
331
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200332 for (i = 0; i < clk_data->clk_num; i++) {
333 struct clk *clk;
334 const char *clk_name;
335
336 if (of_property_read_string_index(np, "clock-output-names",
337 i, &clk_name)) {
338 break;
339 }
340
Lee Jonesfa6415a2016-06-07 12:19:25 +0100341 of_clk_detect_critical(np, i, &flex_flags);
342
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200343 /*
344 * If we read an empty clock name then the output is unused
345 */
346 if (*clk_name == '\0')
347 continue;
348
349 clk = clk_register_flexgen(clk_name, parents, num_parents,
350 reg, rlock, i, flex_flags);
351
352 if (IS_ERR(clk))
353 goto err;
354
355 clk_data->clks[i] = clk;
356 }
357
358 kfree(parents);
359 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
360
361 return;
362
363err:
364 if (clk_data)
365 kfree(clk_data->clks);
366 kfree(clk_data);
367 kfree(parents);
368 kfree(rlock);
369}
370CLK_OF_DECLARE(flexgen, "st,flexgen", st_of_flexgen_setup);