]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/clk/ti/clk.c
Merge branch 'karo-tx6-mainline' into stable
[karo-tx-linux.git] / drivers / clk / ti / clk.c
1 /*
2  * TI clock support
3  *
4  * Copyright (C) 2013 Texas Instruments, Inc.
5  *
6  * Tero Kristo <t-kristo@ti.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13  * kind, whether express or implied; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/clk.h>
19 #include <linux/clk-provider.h>
20 #include <linux/clkdev.h>
21 #include <linux/clk/ti.h>
22 #include <linux/of.h>
23 #include <linux/of_address.h>
24 #include <linux/list.h>
25 #include <linux/regmap.h>
26 #include <linux/bootmem.h>
27
28 #include "clock.h"
29
30 #undef pr_fmt
31 #define pr_fmt(fmt) "%s: " fmt, __func__
32
33 struct ti_clk_ll_ops *ti_clk_ll_ops;
34 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
35
36 static struct ti_clk_features ti_clk_features;
37
38 struct clk_iomap {
39         struct regmap *regmap;
40         void __iomem *mem;
41 };
42
43 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
44
45 static void clk_memmap_writel(u32 val, void __iomem *reg)
46 {
47         struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
48         struct clk_iomap *io = clk_memmaps[r->index];
49
50         if (io->regmap)
51                 regmap_write(io->regmap, r->offset, val);
52         else
53                 writel_relaxed(val, io->mem + r->offset);
54 }
55
56 static u32 clk_memmap_readl(void __iomem *reg)
57 {
58         u32 val;
59         struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
60         struct clk_iomap *io = clk_memmaps[r->index];
61
62         if (io->regmap)
63                 regmap_read(io->regmap, r->offset, &val);
64         else
65                 val = readl_relaxed(io->mem + r->offset);
66
67         return val;
68 }
69
70 /**
71  * ti_clk_setup_ll_ops - setup low level clock operations
72  * @ops: low level clock ops descriptor
73  *
74  * Sets up low level clock operations for TI clock driver. This is used
75  * to provide various callbacks for the clock driver towards platform
76  * specific code. Returns 0 on success, -EBUSY if ll_ops have been
77  * registered already.
78  */
79 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
80 {
81         if (ti_clk_ll_ops) {
82                 pr_err("Attempt to register ll_ops multiple times.\n");
83                 return -EBUSY;
84         }
85
86         ti_clk_ll_ops = ops;
87         ops->clk_readl = clk_memmap_readl;
88         ops->clk_writel = clk_memmap_writel;
89
90         return 0;
91 }
92
93 /**
94  * ti_dt_clocks_register - register DT alias clocks during boot
95  * @oclks: list of clocks to register
96  *
97  * Register alias or non-standard DT clock entries during boot. By
98  * default, DT clocks are found based on their node name. If any
99  * additional con-id / dev-id -> clock mapping is required, use this
100  * function to list these.
101  */
102 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
103 {
104         struct ti_dt_clk *c;
105         struct device_node *node;
106         struct clk *clk;
107         struct of_phandle_args clkspec;
108
109         for (c = oclks; c->node_name != NULL; c++) {
110                 node = of_find_node_by_name(NULL, c->node_name);
111                 clkspec.np = node;
112                 clk = of_clk_get_from_provider(&clkspec);
113
114                 if (!IS_ERR(clk)) {
115                         c->lk.clk = clk;
116                         clkdev_add(&c->lk);
117                 } else {
118                         pr_warn("failed to lookup clock node %s\n",
119                                 c->node_name);
120                 }
121         }
122         of_clk_init(NULL);
123 }
124
125 struct clk_init_item {
126         struct device_node *node;
127         struct clk_hw *hw;
128         ti_of_clk_init_cb_t func;
129         struct list_head link;
130 };
131
132 static LIST_HEAD(retry_list);
133
134 /**
135  * ti_clk_retry_init - retries a failed clock init at later phase
136  * @node: device not for the clock
137  * @hw: partially initialized clk_hw struct for the clock
138  * @func: init function to be called for the clock
139  *
140  * Adds a failed clock init to the retry list. The retry list is parsed
141  * once all the other clocks have been initialized.
142  */
143 int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
144                               ti_of_clk_init_cb_t func)
145 {
146         struct clk_init_item *retry;
147
148         pr_debug("%s: adding to retry list...\n", node->name);
149         retry = kzalloc(sizeof(*retry), GFP_KERNEL);
150         if (!retry)
151                 return -ENOMEM;
152
153         retry->node = node;
154         retry->func = func;
155         retry->hw = hw;
156         list_add(&retry->link, &retry_list);
157
158         return 0;
159 }
160
161 /**
162  * ti_clk_get_reg_addr - get register address for a clock register
163  * @node: device node for the clock
164  * @index: register index from the clock node
165  *
166  * Builds clock register address from device tree information. This
167  * is a struct of type clk_omap_reg. Returns a pointer to the register
168  * address, or a pointer error value in failure.
169  */
170 void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
171 {
172         struct clk_omap_reg *reg;
173         u32 val;
174         u32 tmp;
175         int i;
176
177         reg = (struct clk_omap_reg *)&tmp;
178
179         for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
180                 if (clocks_node_ptr[i] == node->parent)
181                         break;
182         }
183
184         if (i == CLK_MAX_MEMMAPS) {
185                 pr_err("clk-provider not found for %s!\n", node->name);
186                 return IOMEM_ERR_PTR(-ENOENT);
187         }
188
189         reg->index = i;
190
191         if (of_property_read_u32_index(node, "reg", index, &val)) {
192                 pr_err("%s must have reg[%d]!\n", node->name, index);
193                 return IOMEM_ERR_PTR(-EINVAL);
194         }
195
196         reg->offset = val;
197
198         return (__force void __iomem *)tmp;
199 }
200
201 /**
202  * omap2_clk_provider_init - init master clock provider
203  * @parent: master node
204  * @index: internal index for clk_reg_ops
205  * @syscon: syscon regmap pointer for accessing clock registers
206  * @mem: iomem pointer for the clock provider memory area, only used if
207  *       syscon is not provided
208  *
209  * Initializes a master clock IP block. This basically sets up the
210  * mapping from clocks node to the memory map index. All the clocks
211  * are then initialized through the common of_clk_init call, and the
212  * clocks will access their memory maps based on the node layout.
213  * Returns 0 in success.
214  */
215 int __init omap2_clk_provider_init(struct device_node *parent, int index,
216                                    struct regmap *syscon, void __iomem *mem)
217 {
218         struct device_node *clocks;
219         struct clk_iomap *io;
220
221         /* get clocks for this parent */
222         clocks = of_get_child_by_name(parent, "clocks");
223         if (!clocks) {
224                 pr_err("%s missing 'clocks' child node.\n", parent->name);
225                 return -EINVAL;
226         }
227
228         /* add clocks node info */
229         clocks_node_ptr[index] = clocks;
230
231         io = kzalloc(sizeof(*io), GFP_KERNEL);
232         if (!io)
233                 return -ENOMEM;
234
235         io->regmap = syscon;
236         io->mem = mem;
237
238         clk_memmaps[index] = io;
239
240         return 0;
241 }
242
243 /**
244  * omap2_clk_legacy_provider_init - initialize a legacy clock provider
245  * @index: index for the clock provider
246  * @mem: iomem pointer for the clock provider memory area
247  *
248  * Initializes a legacy clock provider memory mapping.
249  */
250 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
251 {
252         struct clk_iomap *io;
253
254         io = memblock_virt_alloc(sizeof(*io), 0);
255
256         io->mem = mem;
257
258         clk_memmaps[index] = io;
259 }
260
261 /**
262  * ti_dt_clk_init_retry_clks - init clocks from the retry list
263  *
264  * Initializes any clocks that have failed to initialize before,
265  * reasons being missing parent node(s) during earlier init. This
266  * typically happens only for DPLLs which need to have both of their
267  * parent clocks ready during init.
268  */
269 void ti_dt_clk_init_retry_clks(void)
270 {
271         struct clk_init_item *retry;
272         struct clk_init_item *tmp;
273         int retries = 5;
274
275         while (!list_empty(&retry_list) && retries) {
276                 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
277                         pr_debug("retry-init: %s\n", retry->node->name);
278                         retry->func(retry->hw, retry->node);
279                         list_del(&retry->link);
280                         kfree(retry);
281                 }
282                 retries--;
283         }
284 }
285
286 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
287 void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
288 {
289         while (*patch) {
290                 memcpy((*patch)->patch, *patch, sizeof(**patch));
291                 patch++;
292         }
293 }
294
295 struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
296 {
297         struct clk *clk;
298         struct ti_clk_fixed *fixed;
299         struct ti_clk_fixed_factor *fixed_factor;
300         struct clk_hw *clk_hw;
301
302         if (setup->clk)
303                 return setup->clk;
304
305         switch (setup->type) {
306         case TI_CLK_FIXED:
307                 fixed = setup->data;
308
309                 clk = clk_register_fixed_rate(NULL, setup->name, NULL,
310                                               CLK_IS_ROOT, fixed->frequency);
311                 break;
312         case TI_CLK_MUX:
313                 clk = ti_clk_register_mux(setup);
314                 break;
315         case TI_CLK_DIVIDER:
316                 clk = ti_clk_register_divider(setup);
317                 break;
318         case TI_CLK_COMPOSITE:
319                 clk = ti_clk_register_composite(setup);
320                 break;
321         case TI_CLK_FIXED_FACTOR:
322                 fixed_factor = setup->data;
323
324                 clk = clk_register_fixed_factor(NULL, setup->name,
325                                                 fixed_factor->parent,
326                                                 0, fixed_factor->mult,
327                                                 fixed_factor->div);
328                 break;
329         case TI_CLK_GATE:
330                 clk = ti_clk_register_gate(setup);
331                 break;
332         case TI_CLK_DPLL:
333                 clk = ti_clk_register_dpll(setup);
334                 break;
335         default:
336                 pr_err("bad type for %s!\n", setup->name);
337                 clk = ERR_PTR(-EINVAL);
338         }
339
340         if (!IS_ERR(clk)) {
341                 setup->clk = clk;
342                 if (setup->clkdm_name) {
343                         clk_hw = __clk_get_hw(clk);
344                         if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
345                                 pr_warn("can't setup clkdm for basic clk %s\n",
346                                         setup->name);
347                         } else {
348                                 to_clk_hw_omap(clk_hw)->clkdm_name =
349                                         setup->clkdm_name;
350                                 omap2_init_clk_clkdm(clk_hw);
351                         }
352                 }
353         }
354
355         return clk;
356 }
357
358 int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
359 {
360         struct clk *clk;
361         bool retry;
362         struct ti_clk_alias *retry_clk;
363         struct ti_clk_alias *tmp;
364
365         while (clks->clk) {
366                 clk = ti_clk_register_clk(clks->clk);
367                 if (IS_ERR(clk)) {
368                         if (PTR_ERR(clk) == -EAGAIN) {
369                                 list_add(&clks->link, &retry_list);
370                         } else {
371                                 pr_err("register for %s failed: %ld\n",
372                                        clks->clk->name, PTR_ERR(clk));
373                                 return PTR_ERR(clk);
374                         }
375                 } else {
376                         clks->lk.clk = clk;
377                         clkdev_add(&clks->lk);
378                 }
379                 clks++;
380         }
381
382         retry = true;
383
384         while (!list_empty(&retry_list) && retry) {
385                 retry = false;
386                 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
387                         pr_debug("retry-init: %s\n", retry_clk->clk->name);
388                         clk = ti_clk_register_clk(retry_clk->clk);
389                         if (IS_ERR(clk)) {
390                                 if (PTR_ERR(clk) == -EAGAIN) {
391                                         continue;
392                                 } else {
393                                         pr_err("register for %s failed: %ld\n",
394                                                retry_clk->clk->name,
395                                                PTR_ERR(clk));
396                                         return PTR_ERR(clk);
397                                 }
398                         } else {
399                                 retry = true;
400                                 retry_clk->lk.clk = clk;
401                                 clkdev_add(&retry_clk->lk);
402                                 list_del(&retry_clk->link);
403                         }
404                 }
405         }
406
407         return 0;
408 }
409 #endif
410
411 /**
412  * ti_clk_setup_features - setup clock features flags
413  * @features: features definition to use
414  *
415  * Initializes the clock driver features flags based on platform
416  * provided data. No return value.
417  */
418 void __init ti_clk_setup_features(struct ti_clk_features *features)
419 {
420         memcpy(&ti_clk_features, features, sizeof(*features));
421 }
422
423 /**
424  * ti_clk_get_features - get clock driver features flags
425  *
426  * Get TI clock driver features description. Returns a pointer
427  * to the current feature setup.
428  */
429 const struct ti_clk_features *ti_clk_get_features(void)
430 {
431         return &ti_clk_features;
432 }
433
434 /**
435  * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
436  * @clk_names: ptr to an array of strings of clock names to enable
437  * @num_clocks: number of clock names in @clk_names
438  *
439  * Prepare and enable a list of clocks, named by @clk_names.  No
440  * return value. XXX Deprecated; only needed until these clocks are
441  * properly claimed and enabled by the drivers or core code that uses
442  * them.  XXX What code disables & calls clk_put on these clocks?
443  */
444 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
445 {
446         struct clk *init_clk;
447         int i;
448
449         for (i = 0; i < num_clocks; i++) {
450                 init_clk = clk_get(NULL, clk_names[i]);
451                 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
452                          clk_names[i]))
453                         continue;
454                 clk_prepare_enable(init_clk);
455         }
456 }