blob: 918ba3164da94c9dcf02927b17940f7e76165551 [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;
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +020020 bool mode;
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +020021};
22
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020023struct flexgen {
24 struct clk_hw hw;
25
26 /* Crossbar */
27 struct clk_mux mux;
28 /* Pre-divisor's gate */
29 struct clk_gate pgate;
30 /* Pre-divisor */
31 struct clk_divider pdiv;
32 /* Final divisor's gate */
33 struct clk_gate fgate;
34 /* Final divisor */
35 struct clk_divider fdiv;
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +020036 /* Asynchronous mode control */
37 struct clk_gate sync;
38 /* hw control flags */
39 bool control_mode;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020040};
41
42#define to_flexgen(_hw) container_of(_hw, struct flexgen, hw)
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +020043#define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020044
45static int flexgen_enable(struct clk_hw *hw)
46{
47 struct flexgen *flexgen = to_flexgen(hw);
48 struct clk_hw *pgate_hw = &flexgen->pgate.hw;
49 struct clk_hw *fgate_hw = &flexgen->fgate.hw;
50
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010051 __clk_hw_set_clk(pgate_hw, hw);
52 __clk_hw_set_clk(fgate_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020053
54 clk_gate_ops.enable(pgate_hw);
55
56 clk_gate_ops.enable(fgate_hw);
57
Stephen Boyd836ee0f2015-08-12 11:42:23 -070058 pr_debug("%s: flexgen output enabled\n", clk_hw_get_name(hw));
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020059 return 0;
60}
61
62static void flexgen_disable(struct clk_hw *hw)
63{
64 struct flexgen *flexgen = to_flexgen(hw);
65 struct clk_hw *fgate_hw = &flexgen->fgate.hw;
66
67 /* disable only the final gate */
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010068 __clk_hw_set_clk(fgate_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020069
70 clk_gate_ops.disable(fgate_hw);
71
Stephen Boyd836ee0f2015-08-12 11:42:23 -070072 pr_debug("%s: flexgen output disabled\n", clk_hw_get_name(hw));
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020073}
74
75static int flexgen_is_enabled(struct clk_hw *hw)
76{
77 struct flexgen *flexgen = to_flexgen(hw);
78 struct clk_hw *fgate_hw = &flexgen->fgate.hw;
79
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010080 __clk_hw_set_clk(fgate_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020081
82 if (!clk_gate_ops.is_enabled(fgate_hw))
83 return 0;
84
85 return 1;
86}
87
88static u8 flexgen_get_parent(struct clk_hw *hw)
89{
90 struct flexgen *flexgen = to_flexgen(hw);
91 struct clk_hw *mux_hw = &flexgen->mux.hw;
92
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010093 __clk_hw_set_clk(mux_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +020094
95 return clk_mux_ops.get_parent(mux_hw);
96}
97
98static int flexgen_set_parent(struct clk_hw *hw, u8 index)
99{
100 struct flexgen *flexgen = to_flexgen(hw);
101 struct clk_hw *mux_hw = &flexgen->mux.hw;
102
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100103 __clk_hw_set_clk(mux_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200104
105 return clk_mux_ops.set_parent(mux_hw, index);
106}
107
108static inline unsigned long
109clk_best_div(unsigned long parent_rate, unsigned long rate)
110{
111 return parent_rate / rate + ((rate > (2*(parent_rate % rate))) ? 0 : 1);
112}
113
114static long flexgen_round_rate(struct clk_hw *hw, unsigned long rate,
115 unsigned long *prate)
116{
117 unsigned long div;
118
119 /* Round div according to exact prate and wished rate */
120 div = clk_best_div(*prate, rate);
121
Stephen Boyd98d8a602015-06-29 16:56:30 -0700122 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200123 *prate = rate * div;
124 return rate;
125 }
126
127 return *prate / div;
128}
129
Stephen Boyd8e6dd772015-05-01 12:45:53 -0700130static unsigned long flexgen_recalc_rate(struct clk_hw *hw,
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200131 unsigned long parent_rate)
132{
133 struct flexgen *flexgen = to_flexgen(hw);
134 struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
135 struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
136 unsigned long mid_rate;
137
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100138 __clk_hw_set_clk(pdiv_hw, hw);
139 __clk_hw_set_clk(fdiv_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200140
141 mid_rate = clk_divider_ops.recalc_rate(pdiv_hw, parent_rate);
142
143 return clk_divider_ops.recalc_rate(fdiv_hw, mid_rate);
144}
145
146static int flexgen_set_rate(struct clk_hw *hw, unsigned long rate,
147 unsigned long parent_rate)
148{
149 struct flexgen *flexgen = to_flexgen(hw);
150 struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
151 struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200152 struct clk_hw *sync_hw = &flexgen->sync.hw;
153 struct clk_gate *config = to_clk_gate(sync_hw);
Peter Griffinedc30072015-01-20 15:32:41 +0000154 unsigned long div = 0;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200155 int ret = 0;
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200156 u32 reg;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200157
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100158 __clk_hw_set_clk(pdiv_hw, hw);
159 __clk_hw_set_clk(fdiv_hw, hw);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200160
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200161 if (flexgen->control_mode) {
162 reg = readl(config->reg);
163 reg &= ~BIT(config->bit_idx);
164 writel(reg, config->reg);
165 }
166
Peter Griffinedc30072015-01-20 15:32:41 +0000167 div = clk_best_div(parent_rate, rate);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200168
Peter Griffinedc30072015-01-20 15:32:41 +0000169 /*
170 * pdiv is mainly targeted for low freq results, while fdiv
171 * should be used for div <= 64. The other way round can
172 * lead to 'duty cycle' issues.
173 */
174
175 if (div <= 64) {
176 clk_divider_ops.set_rate(pdiv_hw, parent_rate, parent_rate);
177 ret = clk_divider_ops.set_rate(fdiv_hw, rate, rate * div);
178 } else {
179 clk_divider_ops.set_rate(fdiv_hw, parent_rate, parent_rate);
180 ret = clk_divider_ops.set_rate(pdiv_hw, rate, rate * div);
181 }
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200182
183 return ret;
184}
185
186static const struct clk_ops flexgen_ops = {
187 .enable = flexgen_enable,
188 .disable = flexgen_disable,
189 .is_enabled = flexgen_is_enabled,
190 .get_parent = flexgen_get_parent,
191 .set_parent = flexgen_set_parent,
192 .round_rate = flexgen_round_rate,
193 .recalc_rate = flexgen_recalc_rate,
194 .set_rate = flexgen_set_rate,
195};
196
Stephen Boyd8e6dd772015-05-01 12:45:53 -0700197static struct clk *clk_register_flexgen(const char *name,
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200198 const char **parent_names, u8 num_parents,
199 void __iomem *reg, spinlock_t *lock, u32 idx,
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200200 unsigned long flexgen_flags, bool mode) {
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200201 struct flexgen *fgxbar;
202 struct clk *clk;
203 struct clk_init_data init;
204 u32 xbar_shift;
205 void __iomem *xbar_reg, *fdiv_reg;
206
207 fgxbar = kzalloc(sizeof(struct flexgen), GFP_KERNEL);
208 if (!fgxbar)
209 return ERR_PTR(-ENOMEM);
210
211 init.name = name;
212 init.ops = &flexgen_ops;
Pankaj Dev18fee452015-06-23 16:09:24 +0200213 init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE | flexgen_flags;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200214 init.parent_names = parent_names;
215 init.num_parents = num_parents;
216
217 xbar_reg = reg + 0x18 + (idx & ~0x3);
218 xbar_shift = (idx % 4) * 0x8;
219 fdiv_reg = reg + 0x164 + idx * 4;
220
221 /* Crossbar element config */
222 fgxbar->mux.lock = lock;
223 fgxbar->mux.mask = BIT(6) - 1;
224 fgxbar->mux.reg = xbar_reg;
225 fgxbar->mux.shift = xbar_shift;
226 fgxbar->mux.table = NULL;
227
228
229 /* Pre-divider's gate config (in xbar register)*/
230 fgxbar->pgate.lock = lock;
231 fgxbar->pgate.reg = xbar_reg;
232 fgxbar->pgate.bit_idx = xbar_shift + 6;
233
234 /* Pre-divider config */
235 fgxbar->pdiv.lock = lock;
236 fgxbar->pdiv.reg = reg + 0x58 + idx * 4;
237 fgxbar->pdiv.width = 10;
238
239 /* Final divider's gate config */
240 fgxbar->fgate.lock = lock;
241 fgxbar->fgate.reg = fdiv_reg;
242 fgxbar->fgate.bit_idx = 6;
243
244 /* Final divider config */
245 fgxbar->fdiv.lock = lock;
246 fgxbar->fdiv.reg = fdiv_reg;
247 fgxbar->fdiv.width = 6;
248
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200249 /* Final divider sync config */
250 fgxbar->sync.lock = lock;
251 fgxbar->sync.reg = fdiv_reg;
252 fgxbar->sync.bit_idx = 7;
253
254 fgxbar->control_mode = mode;
255
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200256 fgxbar->hw.init = &init;
257
258 clk = clk_register(NULL, &fgxbar->hw);
259 if (IS_ERR(clk))
260 kfree(fgxbar);
261 else
262 pr_debug("%s: parent %s rate %u\n",
263 __clk_get_name(clk),
264 __clk_get_name(clk_get_parent(clk)),
265 (unsigned int)clk_get_rate(clk));
266 return clk;
267}
268
269static const char ** __init flexgen_get_parents(struct device_node *np,
270 int *num_parents)
271{
272 const char **parents;
Stephen Boydcaeb0572016-02-19 17:43:30 -0800273 unsigned int nparents;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200274
Geert Uytterhoeven0a652392015-05-29 11:25:46 +0200275 nparents = of_clk_get_parent_count(np);
Stephen Boydcaeb0572016-02-19 17:43:30 -0800276 if (WARN_ON(!nparents))
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200277 return NULL;
278
279 parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
280 if (!parents)
281 return NULL;
282
Dinh Nguyen0b4e7f02015-07-06 22:59:04 -0500283 *num_parents = of_clk_parent_fill(np, parents, nparents);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200284
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200285 return parents;
286}
287
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200288static const struct clkgen_data clkgen_audio = {
289 .flags = CLK_SET_RATE_PARENT,
290};
291
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200292static const struct clkgen_data clkgen_video = {
293 .flags = CLK_SET_RATE_PARENT,
294 .mode = 1,
295};
296
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200297static const struct of_device_id flexgen_of_match[] = {
298 {
299 .compatible = "st,flexgen-audio",
300 .data = &clkgen_audio,
301 },
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200302 {
303 .compatible = "st,flexgen-video",
304 .data = &clkgen_video,
305 },
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200306 {}
307};
308
Stephen Boyd8e6dd772015-05-01 12:45:53 -0700309static void __init st_of_flexgen_setup(struct device_node *np)
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200310{
311 struct device_node *pnode;
312 void __iomem *reg;
313 struct clk_onecell_data *clk_data;
314 const char **parents;
315 int num_parents, i;
316 spinlock_t *rlock = NULL;
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200317 const struct of_device_id *match;
318 struct clkgen_data *data = NULL;
319 unsigned long flex_flags = 0;
Andrzej Hajdaa1c22a42015-09-24 16:00:16 +0200320 int ret;
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200321 bool clk_mode = 0;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200322
323 pnode = of_get_parent(np);
324 if (!pnode)
325 return;
326
327 reg = of_iomap(pnode, 0);
328 if (!reg)
329 return;
330
331 parents = flexgen_get_parents(np, &num_parents);
Arvind Yadav16cd7762016-09-19 13:51:24 +0530332 if (!parents) {
333 iounmap(reg);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200334 return;
Arvind Yadav16cd7762016-09-19 13:51:24 +0530335 }
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200336
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200337 match = of_match_node(flexgen_of_match, np);
338 if (match) {
339 data = (struct clkgen_data *)match->data;
340 flex_flags = data->flags;
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200341 clk_mode = data->mode;
Gabriel Fernandez26bd0a52016-08-29 14:26:57 +0200342 }
343
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200344 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
345 if (!clk_data)
346 goto err;
347
Andrzej Hajdaa1c22a42015-09-24 16:00:16 +0200348 ret = of_property_count_strings(np, "clock-output-names");
349 if (ret <= 0) {
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200350 pr_err("%s: Failed to get number of output clocks (%d)",
351 __func__, clk_data->clk_num);
352 goto err;
353 }
Andrzej Hajdaa1c22a42015-09-24 16:00:16 +0200354 clk_data->clk_num = ret;
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200355
356 clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *),
357 GFP_KERNEL);
358 if (!clk_data->clks)
359 goto err;
360
361 rlock = kzalloc(sizeof(spinlock_t), GFP_KERNEL);
362 if (!rlock)
363 goto err;
364
Giuseppe Cavallaro0f4f2af2015-06-23 16:09:23 +0200365 spin_lock_init(rlock);
366
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200367 for (i = 0; i < clk_data->clk_num; i++) {
368 struct clk *clk;
369 const char *clk_name;
370
371 if (of_property_read_string_index(np, "clock-output-names",
372 i, &clk_name)) {
373 break;
374 }
375
Lee Jonesfa6415a2016-06-07 12:19:25 +0100376 of_clk_detect_critical(np, i, &flex_flags);
377
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200378 /*
379 * If we read an empty clock name then the output is unused
380 */
381 if (*clk_name == '\0')
382 continue;
383
384 clk = clk_register_flexgen(clk_name, parents, num_parents,
Gabriel Fernandezcb80ec72016-08-29 14:26:58 +0200385 reg, rlock, i, flex_flags, clk_mode);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200386
387 if (IS_ERR(clk))
388 goto err;
389
390 clk_data->clks[i] = clk;
391 }
392
393 kfree(parents);
394 of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
395
396 return;
397
398err:
Arvind Yadav16cd7762016-09-19 13:51:24 +0530399 iounmap(reg);
Gabriel FERNANDEZb1165172014-07-15 17:20:22 +0200400 if (clk_data)
401 kfree(clk_data->clks);
402 kfree(clk_data);
403 kfree(parents);
404 kfree(rlock);
405}
406CLK_OF_DECLARE(flexgen, "st,flexgen", st_of_flexgen_setup);