]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/clk/ti/clk.c
clk: ti: use automatic clock alias generation framework
[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 #include <linux/device.h>
28
29 #include "clock.h"
30
31 #undef pr_fmt
32 #define pr_fmt(fmt) "%s: " fmt, __func__
33
34 struct ti_clk_ll_ops *ti_clk_ll_ops;
35 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
36
37 static struct ti_clk_features ti_clk_features;
38
39 struct clk_iomap {
40         struct regmap *regmap;
41         void __iomem *mem;
42 };
43
44 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
45
46 static void clk_memmap_writel(u32 val, void __iomem *reg)
47 {
48         struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
49         struct clk_iomap *io = clk_memmaps[r->index];
50
51         if (io->regmap)
52                 regmap_write(io->regmap, r->offset, val);
53         else
54                 writel_relaxed(val, io->mem + r->offset);
55 }
56
57 static u32 clk_memmap_readl(void __iomem *reg)
58 {
59         u32 val;
60         struct clk_omap_reg *r = (struct clk_omap_reg *)&reg;
61         struct clk_iomap *io = clk_memmaps[r->index];
62
63         if (io->regmap)
64                 regmap_read(io->regmap, r->offset, &val);
65         else
66                 val = readl_relaxed(io->mem + r->offset);
67
68         return val;
69 }
70
71 /**
72  * ti_clk_setup_ll_ops - setup low level clock operations
73  * @ops: low level clock ops descriptor
74  *
75  * Sets up low level clock operations for TI clock driver. This is used
76  * to provide various callbacks for the clock driver towards platform
77  * specific code. Returns 0 on success, -EBUSY if ll_ops have been
78  * registered already.
79  */
80 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
81 {
82         if (ti_clk_ll_ops) {
83                 pr_err("Attempt to register ll_ops multiple times.\n");
84                 return -EBUSY;
85         }
86
87         ti_clk_ll_ops = ops;
88         ops->clk_readl = clk_memmap_readl;
89         ops->clk_writel = clk_memmap_writel;
90
91         return 0;
92 }
93
94 /**
95  * ti_dt_clocks_register - register DT alias clocks during boot
96  * @oclks: list of clocks to register
97  *
98  * Register alias or non-standard DT clock entries during boot. By
99  * default, DT clocks are found based on their node name. If any
100  * additional con-id / dev-id -> clock mapping is required, use this
101  * function to list these.
102  */
103 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
104 {
105         struct ti_dt_clk *c;
106         struct device_node *node;
107         struct clk *clk;
108         struct of_phandle_args clkspec;
109
110         for (c = oclks; c->node_name != NULL; c++) {
111                 node = of_find_node_by_name(NULL, c->node_name);
112                 clkspec.np = node;
113                 clk = of_clk_get_from_provider(&clkspec);
114
115                 if (!IS_ERR(clk)) {
116                         c->lk.clk = clk;
117                         clkdev_add(&c->lk);
118                 } else {
119                         pr_warn("failed to lookup clock node %s\n",
120                                 c->node_name);
121                 }
122         }
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         int ret;
302
303         if (setup->clk)
304                 return setup->clk;
305
306         switch (setup->type) {
307         case TI_CLK_FIXED:
308                 fixed = setup->data;
309
310                 clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
311                                               fixed->frequency);
312                 if (!IS_ERR(clk)) {
313                         ret = ti_clk_add_alias(NULL, clk, setup->name);
314                         if (ret) {
315                                 clk_unregister(clk);
316                                 clk = ERR_PTR(ret);
317                         }
318                 }
319                 break;
320         case TI_CLK_MUX:
321                 clk = ti_clk_register_mux(setup);
322                 break;
323         case TI_CLK_DIVIDER:
324                 clk = ti_clk_register_divider(setup);
325                 break;
326         case TI_CLK_COMPOSITE:
327                 clk = ti_clk_register_composite(setup);
328                 break;
329         case TI_CLK_FIXED_FACTOR:
330                 fixed_factor = setup->data;
331
332                 clk = clk_register_fixed_factor(NULL, setup->name,
333                                                 fixed_factor->parent,
334                                                 0, fixed_factor->mult,
335                                                 fixed_factor->div);
336                 if (!IS_ERR(clk)) {
337                         ret = ti_clk_add_alias(NULL, clk, setup->name);
338                         if (ret) {
339                                 clk_unregister(clk);
340                                 clk = ERR_PTR(ret);
341                         }
342                 }
343                 break;
344         case TI_CLK_GATE:
345                 clk = ti_clk_register_gate(setup);
346                 break;
347         case TI_CLK_DPLL:
348                 clk = ti_clk_register_dpll(setup);
349                 break;
350         default:
351                 pr_err("bad type for %s!\n", setup->name);
352                 clk = ERR_PTR(-EINVAL);
353         }
354
355         if (!IS_ERR(clk)) {
356                 setup->clk = clk;
357                 if (setup->clkdm_name) {
358                         clk_hw = __clk_get_hw(clk);
359                         if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
360                                 pr_warn("can't setup clkdm for basic clk %s\n",
361                                         setup->name);
362                         } else {
363                                 to_clk_hw_omap(clk_hw)->clkdm_name =
364                                         setup->clkdm_name;
365                                 omap2_init_clk_clkdm(clk_hw);
366                         }
367                 }
368         }
369
370         return clk;
371 }
372
373 static const struct of_device_id simple_clk_match_table[] __initconst = {
374         { .compatible = "fixed-clock" },
375         { .compatible = "fixed-factor-clock" },
376         { }
377 };
378
379 int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
380 {
381         struct clk *clk;
382         bool retry;
383         struct ti_clk_alias *retry_clk;
384         struct ti_clk_alias *tmp;
385
386         while (clks->clk) {
387                 clk = ti_clk_register_clk(clks->clk);
388                 if (IS_ERR(clk)) {
389                         if (PTR_ERR(clk) == -EAGAIN) {
390                                 list_add(&clks->link, &retry_list);
391                         } else {
392                                 pr_err("register for %s failed: %ld\n",
393                                        clks->clk->name, PTR_ERR(clk));
394                                 return PTR_ERR(clk);
395                         }
396                 }
397                 clks++;
398         }
399
400         retry = true;
401
402         while (!list_empty(&retry_list) && retry) {
403                 retry = false;
404                 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
405                         pr_debug("retry-init: %s\n", retry_clk->clk->name);
406                         clk = ti_clk_register_clk(retry_clk->clk);
407                         if (IS_ERR(clk)) {
408                                 if (PTR_ERR(clk) == -EAGAIN) {
409                                         continue;
410                                 } else {
411                                         pr_err("register for %s failed: %ld\n",
412                                                retry_clk->clk->name,
413                                                PTR_ERR(clk));
414                                         return PTR_ERR(clk);
415                                 }
416                         } else {
417                                 retry = true;
418                                 list_del(&retry_clk->link);
419                         }
420                 }
421         }
422
423         return 0;
424 }
425 #endif
426
427 /**
428  * ti_clk_add_aliases - setup clock aliases
429  *
430  * Sets up any missing clock aliases. No return value.
431  */
432 void __init ti_clk_add_aliases(void)
433 {
434         struct device_node *np;
435         struct clk *clk;
436
437         for_each_matching_node(np, simple_clk_match_table) {
438                 struct of_phandle_args clkspec;
439
440                 clkspec.np = np;
441                 clk = of_clk_get_from_provider(&clkspec);
442
443                 ti_clk_add_alias(NULL, clk, np->name);
444         }
445 }
446
447 /**
448  * ti_clk_setup_features - setup clock features flags
449  * @features: features definition to use
450  *
451  * Initializes the clock driver features flags based on platform
452  * provided data. No return value.
453  */
454 void __init ti_clk_setup_features(struct ti_clk_features *features)
455 {
456         memcpy(&ti_clk_features, features, sizeof(*features));
457 }
458
459 /**
460  * ti_clk_get_features - get clock driver features flags
461  *
462  * Get TI clock driver features description. Returns a pointer
463  * to the current feature setup.
464  */
465 const struct ti_clk_features *ti_clk_get_features(void)
466 {
467         return &ti_clk_features;
468 }
469
470 /**
471  * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
472  * @clk_names: ptr to an array of strings of clock names to enable
473  * @num_clocks: number of clock names in @clk_names
474  *
475  * Prepare and enable a list of clocks, named by @clk_names.  No
476  * return value. XXX Deprecated; only needed until these clocks are
477  * properly claimed and enabled by the drivers or core code that uses
478  * them.  XXX What code disables & calls clk_put on these clocks?
479  */
480 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
481 {
482         struct clk *init_clk;
483         int i;
484
485         for (i = 0; i < num_clocks; i++) {
486                 init_clk = clk_get(NULL, clk_names[i]);
487                 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
488                          clk_names[i]))
489                         continue;
490                 clk_prepare_enable(init_clk);
491         }
492 }
493
494 /**
495  * ti_clk_add_alias - add a clock alias for a TI clock
496  * @dev: device alias for this clock
497  * @clk: clock handle to create alias for
498  * @con: connection ID for this clock
499  *
500  * Creates a clock alias for a TI clock. Allocates the clock lookup entry
501  * and assigns the data to it. Returns 0 if successful, negative error
502  * value otherwise.
503  */
504 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
505 {
506         struct clk_lookup *cl;
507
508         if (!clk)
509                 return 0;
510
511         if (IS_ERR(clk))
512                 return PTR_ERR(clk);
513
514         cl = kzalloc(sizeof(*cl), GFP_KERNEL);
515         if (!cl)
516                 return -ENOMEM;
517
518         if (dev)
519                 cl->dev_id = dev_name(dev);
520         cl->con_id = con;
521         cl->clk = clk;
522
523         clkdev_add(cl);
524
525         return 0;
526 }
527
528 /**
529  * ti_clk_register - register a TI clock to the common clock framework
530  * @dev: device for this clock
531  * @hw: hardware clock handle
532  * @con: connection ID for this clock
533  *
534  * Registers a TI clock to the common clock framework, and adds a clock
535  * alias for it. Returns a handle to the registered clock if successful,
536  * ERR_PTR value in failure.
537  */
538 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
539                             const char *con)
540 {
541         struct clk *clk;
542         int ret;
543
544         clk = clk_register(dev, hw);
545         if (IS_ERR(clk))
546                 return clk;
547
548         ret = ti_clk_add_alias(dev, clk, con);
549         if (ret) {
550                 clk_unregister(clk);
551                 return ERR_PTR(ret);
552         }
553
554         return clk;
555 }