Merge branches 'clk-range', 'clk-uniphier', 'clk-apple' and 'clk-qcom' into clk-next
[linux-2.6-microblaze.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/io.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/list.h>
26 #include <linux/regmap.h>
27 #include <linux/memblock.h>
28 #include <linux/device.h>
29
30 #include "clock.h"
31
32 #undef pr_fmt
33 #define pr_fmt(fmt) "%s: " fmt, __func__
34
35 static LIST_HEAD(clk_hw_omap_clocks);
36 struct ti_clk_ll_ops *ti_clk_ll_ops;
37 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
38
39 struct ti_clk_features ti_clk_features;
40
41 struct clk_iomap {
42         struct regmap *regmap;
43         void __iomem *mem;
44 };
45
46 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
47
48 static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
49 {
50         struct clk_iomap *io = clk_memmaps[reg->index];
51
52         if (reg->ptr)
53                 writel_relaxed(val, reg->ptr);
54         else if (io->regmap)
55                 regmap_write(io->regmap, reg->offset, val);
56         else
57                 writel_relaxed(val, io->mem + reg->offset);
58 }
59
60 static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
61 {
62         u32 v;
63
64         v = readl_relaxed(ptr);
65         v &= ~mask;
66         v |= val;
67         writel_relaxed(v, ptr);
68 }
69
70 static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
71 {
72         struct clk_iomap *io = clk_memmaps[reg->index];
73
74         if (reg->ptr) {
75                 _clk_rmw(val, mask, reg->ptr);
76         } else if (io->regmap) {
77                 regmap_update_bits(io->regmap, reg->offset, mask, val);
78         } else {
79                 _clk_rmw(val, mask, io->mem + reg->offset);
80         }
81 }
82
83 static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
84 {
85         u32 val;
86         struct clk_iomap *io = clk_memmaps[reg->index];
87
88         if (reg->ptr)
89                 val = readl_relaxed(reg->ptr);
90         else if (io->regmap)
91                 regmap_read(io->regmap, reg->offset, &val);
92         else
93                 val = readl_relaxed(io->mem + reg->offset);
94
95         return val;
96 }
97
98 /**
99  * ti_clk_setup_ll_ops - setup low level clock operations
100  * @ops: low level clock ops descriptor
101  *
102  * Sets up low level clock operations for TI clock driver. This is used
103  * to provide various callbacks for the clock driver towards platform
104  * specific code. Returns 0 on success, -EBUSY if ll_ops have been
105  * registered already.
106  */
107 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
108 {
109         if (ti_clk_ll_ops) {
110                 pr_err("Attempt to register ll_ops multiple times.\n");
111                 return -EBUSY;
112         }
113
114         ti_clk_ll_ops = ops;
115         ops->clk_readl = clk_memmap_readl;
116         ops->clk_writel = clk_memmap_writel;
117         ops->clk_rmw = clk_memmap_rmw;
118
119         return 0;
120 }
121
122 /*
123  * Eventually we could standardize to using '_' for clk-*.c files to follow the
124  * TRM naming and leave out the tmp name here.
125  */
126 static struct device_node *ti_find_clock_provider(struct device_node *from,
127                                                   const char *name)
128 {
129         struct device_node *np;
130         bool found = false;
131         const char *n;
132         char *tmp;
133
134         tmp = kstrdup(name, GFP_KERNEL);
135         if (!tmp)
136                 return NULL;
137         strreplace(tmp, '-', '_');
138
139         /* Node named "clock" with "clock-output-names" */
140         for_each_of_allnodes_from(from, np) {
141                 if (of_property_read_string_index(np, "clock-output-names",
142                                                   0, &n))
143                         continue;
144
145                 if (!strncmp(n, tmp, strlen(tmp))) {
146                         found = true;
147                         break;
148                 }
149         }
150         of_node_put(from);
151         kfree(tmp);
152
153         if (found)
154                 return np;
155
156         /* Fall back to using old node name base provider name */
157         return of_find_node_by_name(from, name);
158 }
159
160 /**
161  * ti_dt_clocks_register - register DT alias clocks during boot
162  * @oclks: list of clocks to register
163  *
164  * Register alias or non-standard DT clock entries during boot. By
165  * default, DT clocks are found based on their clock-output-names
166  * property, or the clock node name for legacy cases. If any
167  * additional con-id / dev-id -> clock mapping is required, use this
168  * function to list these.
169  */
170 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
171 {
172         struct ti_dt_clk *c;
173         struct device_node *node, *parent, *child;
174         struct clk *clk;
175         struct of_phandle_args clkspec;
176         char buf[64];
177         char *ptr;
178         char *tags[2];
179         int i;
180         int num_args;
181         int ret;
182         static bool clkctrl_nodes_missing;
183         static bool has_clkctrl_data;
184         static bool compat_mode;
185
186         compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
187
188         for (c = oclks; c->node_name != NULL; c++) {
189                 strcpy(buf, c->node_name);
190                 ptr = buf;
191                 for (i = 0; i < 2; i++)
192                         tags[i] = NULL;
193                 num_args = 0;
194                 while (*ptr) {
195                         if (*ptr == ':') {
196                                 if (num_args >= 2) {
197                                         pr_warn("Bad number of tags on %s\n",
198                                                 c->node_name);
199                                         return;
200                                 }
201                                 tags[num_args++] = ptr + 1;
202                                 *ptr = 0;
203                         }
204                         ptr++;
205                 }
206
207                 if (num_args && clkctrl_nodes_missing)
208                         continue;
209
210                 node = ti_find_clock_provider(NULL, buf);
211                 if (num_args && compat_mode) {
212                         parent = node;
213                         child = of_get_child_by_name(parent, "clock");
214                         if (!child)
215                                 child = of_get_child_by_name(parent, "clk");
216                         if (child) {
217                                 of_node_put(parent);
218                                 node = child;
219                         }
220                 }
221
222                 clkspec.np = node;
223                 clkspec.args_count = num_args;
224                 for (i = 0; i < num_args; i++) {
225                         ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
226                         if (ret) {
227                                 pr_warn("Bad tag in %s at %d: %s\n",
228                                         c->node_name, i, tags[i]);
229                                 of_node_put(node);
230                                 return;
231                         }
232                 }
233                 clk = of_clk_get_from_provider(&clkspec);
234                 of_node_put(node);
235                 if (!IS_ERR(clk)) {
236                         c->lk.clk = clk;
237                         clkdev_add(&c->lk);
238                 } else {
239                         if (num_args && !has_clkctrl_data) {
240                                 struct device_node *np;
241
242                                 np = of_find_compatible_node(NULL, NULL,
243                                                              "ti,clkctrl");
244                                 if (np) {
245                                         has_clkctrl_data = true;
246                                         of_node_put(np);
247                                 } else {
248                                         clkctrl_nodes_missing = true;
249
250                                         pr_warn("missing clkctrl nodes, please update your dts.\n");
251                                         continue;
252                                 }
253                         }
254
255                         pr_warn("failed to lookup clock node %s, ret=%ld\n",
256                                 c->node_name, PTR_ERR(clk));
257                 }
258         }
259 }
260
261 struct clk_init_item {
262         struct device_node *node;
263         void *user;
264         ti_of_clk_init_cb_t func;
265         struct list_head link;
266 };
267
268 static LIST_HEAD(retry_list);
269
270 /**
271  * ti_clk_retry_init - retries a failed clock init at later phase
272  * @node: device not for the clock
273  * @user: user data pointer
274  * @func: init function to be called for the clock
275  *
276  * Adds a failed clock init to the retry list. The retry list is parsed
277  * once all the other clocks have been initialized.
278  */
279 int __init ti_clk_retry_init(struct device_node *node, void *user,
280                              ti_of_clk_init_cb_t func)
281 {
282         struct clk_init_item *retry;
283
284         pr_debug("%pOFn: adding to retry list...\n", node);
285         retry = kzalloc(sizeof(*retry), GFP_KERNEL);
286         if (!retry)
287                 return -ENOMEM;
288
289         retry->node = node;
290         retry->func = func;
291         retry->user = user;
292         list_add(&retry->link, &retry_list);
293
294         return 0;
295 }
296
297 /**
298  * ti_clk_get_reg_addr - get register address for a clock register
299  * @node: device node for the clock
300  * @index: register index from the clock node
301  * @reg: pointer to target register struct
302  *
303  * Builds clock register address from device tree information, and returns
304  * the data via the provided output pointer @reg. Returns 0 on success,
305  * negative error value on failure.
306  */
307 int ti_clk_get_reg_addr(struct device_node *node, int index,
308                         struct clk_omap_reg *reg)
309 {
310         u32 val;
311         int i;
312
313         for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
314                 if (clocks_node_ptr[i] == node->parent)
315                         break;
316                 if (clocks_node_ptr[i] == node->parent->parent)
317                         break;
318         }
319
320         if (i == CLK_MAX_MEMMAPS) {
321                 pr_err("clk-provider not found for %pOFn!\n", node);
322                 return -ENOENT;
323         }
324
325         reg->index = i;
326
327         if (of_property_read_u32_index(node, "reg", index, &val)) {
328                 if (of_property_read_u32_index(node->parent, "reg",
329                                                index, &val)) {
330                         pr_err("%pOFn or parent must have reg[%d]!\n",
331                                node, index);
332                         return -EINVAL;
333                 }
334         }
335
336         reg->offset = val;
337         reg->ptr = NULL;
338
339         return 0;
340 }
341
342 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
343 {
344         u32 latch;
345
346         if (shift < 0)
347                 return;
348
349         latch = 1 << shift;
350
351         ti_clk_ll_ops->clk_rmw(latch, latch, reg);
352         ti_clk_ll_ops->clk_rmw(0, latch, reg);
353         ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
354 }
355
356 /**
357  * omap2_clk_provider_init - init master clock provider
358  * @parent: master node
359  * @index: internal index for clk_reg_ops
360  * @syscon: syscon regmap pointer for accessing clock registers
361  * @mem: iomem pointer for the clock provider memory area, only used if
362  *       syscon is not provided
363  *
364  * Initializes a master clock IP block. This basically sets up the
365  * mapping from clocks node to the memory map index. All the clocks
366  * are then initialized through the common of_clk_init call, and the
367  * clocks will access their memory maps based on the node layout.
368  * Returns 0 in success.
369  */
370 int __init omap2_clk_provider_init(struct device_node *parent, int index,
371                                    struct regmap *syscon, void __iomem *mem)
372 {
373         struct device_node *clocks;
374         struct clk_iomap *io;
375
376         /* get clocks for this parent */
377         clocks = of_get_child_by_name(parent, "clocks");
378         if (!clocks) {
379                 pr_err("%pOFn missing 'clocks' child node.\n", parent);
380                 return -EINVAL;
381         }
382
383         /* add clocks node info */
384         clocks_node_ptr[index] = clocks;
385
386         io = kzalloc(sizeof(*io), GFP_KERNEL);
387         if (!io)
388                 return -ENOMEM;
389
390         io->regmap = syscon;
391         io->mem = mem;
392
393         clk_memmaps[index] = io;
394
395         return 0;
396 }
397
398 /**
399  * omap2_clk_legacy_provider_init - initialize a legacy clock provider
400  * @index: index for the clock provider
401  * @mem: iomem pointer for the clock provider memory area
402  *
403  * Initializes a legacy clock provider memory mapping.
404  */
405 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
406 {
407         struct clk_iomap *io;
408
409         io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
410         if (!io)
411                 panic("%s: Failed to allocate %zu bytes\n", __func__,
412                       sizeof(*io));
413
414         io->mem = mem;
415
416         clk_memmaps[index] = io;
417 }
418
419 /**
420  * ti_dt_clk_init_retry_clks - init clocks from the retry list
421  *
422  * Initializes any clocks that have failed to initialize before,
423  * reasons being missing parent node(s) during earlier init. This
424  * typically happens only for DPLLs which need to have both of their
425  * parent clocks ready during init.
426  */
427 void ti_dt_clk_init_retry_clks(void)
428 {
429         struct clk_init_item *retry;
430         struct clk_init_item *tmp;
431         int retries = 5;
432
433         while (!list_empty(&retry_list) && retries) {
434                 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
435                         pr_debug("retry-init: %pOFn\n", retry->node);
436                         retry->func(retry->user, retry->node);
437                         list_del(&retry->link);
438                         kfree(retry);
439                 }
440                 retries--;
441         }
442 }
443
444 static const struct of_device_id simple_clk_match_table[] __initconst = {
445         { .compatible = "fixed-clock" },
446         { .compatible = "fixed-factor-clock" },
447         { }
448 };
449
450 /**
451  * ti_dt_clk_name - init clock name from first output name or node name
452  * @np: device node
453  *
454  * Use the first clock-output-name for the clock name if found. Fall back
455  * to legacy naming based on node name.
456  */
457 const char *ti_dt_clk_name(struct device_node *np)
458 {
459         const char *name;
460
461         if (!of_property_read_string_index(np, "clock-output-names", 0,
462                                            &name))
463                 return name;
464
465         return np->name;
466 }
467
468 /**
469  * ti_clk_add_aliases - setup clock aliases
470  *
471  * Sets up any missing clock aliases. No return value.
472  */
473 void __init ti_clk_add_aliases(void)
474 {
475         struct device_node *np;
476         struct clk *clk;
477
478         for_each_matching_node(np, simple_clk_match_table) {
479                 struct of_phandle_args clkspec;
480
481                 clkspec.np = np;
482                 clk = of_clk_get_from_provider(&clkspec);
483
484                 ti_clk_add_alias(NULL, clk, ti_dt_clk_name(np));
485         }
486 }
487
488 /**
489  * ti_clk_setup_features - setup clock features flags
490  * @features: features definition to use
491  *
492  * Initializes the clock driver features flags based on platform
493  * provided data. No return value.
494  */
495 void __init ti_clk_setup_features(struct ti_clk_features *features)
496 {
497         memcpy(&ti_clk_features, features, sizeof(*features));
498 }
499
500 /**
501  * ti_clk_get_features - get clock driver features flags
502  *
503  * Get TI clock driver features description. Returns a pointer
504  * to the current feature setup.
505  */
506 const struct ti_clk_features *ti_clk_get_features(void)
507 {
508         return &ti_clk_features;
509 }
510
511 /**
512  * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
513  * @clk_names: ptr to an array of strings of clock names to enable
514  * @num_clocks: number of clock names in @clk_names
515  *
516  * Prepare and enable a list of clocks, named by @clk_names.  No
517  * return value. XXX Deprecated; only needed until these clocks are
518  * properly claimed and enabled by the drivers or core code that uses
519  * them.  XXX What code disables & calls clk_put on these clocks?
520  */
521 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
522 {
523         struct clk *init_clk;
524         int i;
525
526         for (i = 0; i < num_clocks; i++) {
527                 init_clk = clk_get(NULL, clk_names[i]);
528                 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
529                          clk_names[i]))
530                         continue;
531                 clk_prepare_enable(init_clk);
532         }
533 }
534
535 /**
536  * ti_clk_add_alias - add a clock alias for a TI clock
537  * @dev: device alias for this clock
538  * @clk: clock handle to create alias for
539  * @con: connection ID for this clock
540  *
541  * Creates a clock alias for a TI clock. Allocates the clock lookup entry
542  * and assigns the data to it. Returns 0 if successful, negative error
543  * value otherwise.
544  */
545 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
546 {
547         struct clk_lookup *cl;
548
549         if (!clk)
550                 return 0;
551
552         if (IS_ERR(clk))
553                 return PTR_ERR(clk);
554
555         cl = kzalloc(sizeof(*cl), GFP_KERNEL);
556         if (!cl)
557                 return -ENOMEM;
558
559         if (dev)
560                 cl->dev_id = dev_name(dev);
561         cl->con_id = con;
562         cl->clk = clk;
563
564         clkdev_add(cl);
565
566         return 0;
567 }
568
569 /**
570  * ti_clk_register - register a TI clock to the common clock framework
571  * @dev: device for this clock
572  * @hw: hardware clock handle
573  * @con: connection ID for this clock
574  *
575  * Registers a TI clock to the common clock framework, and adds a clock
576  * alias for it. Returns a handle to the registered clock if successful,
577  * ERR_PTR value in failure.
578  */
579 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
580                             const char *con)
581 {
582         struct clk *clk;
583         int ret;
584
585         clk = clk_register(dev, hw);
586         if (IS_ERR(clk))
587                 return clk;
588
589         ret = ti_clk_add_alias(dev, clk, con);
590         if (ret) {
591                 clk_unregister(clk);
592                 return ERR_PTR(ret);
593         }
594
595         return clk;
596 }
597
598 /**
599  * ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
600  * @dev: device for this clock
601  * @hw: hardware clock handle
602  * @con: connection ID for this clock
603  *
604  * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
605  * for it, and adds the list to the available clk_hw_omap type clocks.
606  * Returns a handle to the registered clock if successful, ERR_PTR value
607  * in failure.
608  */
609 struct clk *ti_clk_register_omap_hw(struct device *dev, struct clk_hw *hw,
610                                     const char *con)
611 {
612         struct clk *clk;
613         struct clk_hw_omap *oclk;
614
615         clk = ti_clk_register(dev, hw, con);
616         if (IS_ERR(clk))
617                 return clk;
618
619         oclk = to_clk_hw_omap(hw);
620
621         list_add(&oclk->node, &clk_hw_omap_clocks);
622
623         return clk;
624 }
625
626 /**
627  * omap2_clk_for_each - call function for each registered clk_hw_omap
628  * @fn: pointer to a callback function
629  *
630  * Call @fn for each registered clk_hw_omap, passing @hw to each
631  * function.  @fn must return 0 for success or any other value for
632  * failure.  If @fn returns non-zero, the iteration across clocks
633  * will stop and the non-zero return value will be passed to the
634  * caller of omap2_clk_for_each().
635  */
636 int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
637 {
638         int ret;
639         struct clk_hw_omap *hw;
640
641         list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
642                 ret = (*fn)(hw);
643                 if (ret)
644                         break;
645         }
646
647         return ret;
648 }
649
650 /**
651  * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
652  * @hw: clk_hw to check if it is an omap clock or not
653  *
654  * Checks if the provided clk_hw is OMAP clock or not. Returns true if
655  * it is, false otherwise.
656  */
657 bool omap2_clk_is_hw_omap(struct clk_hw *hw)
658 {
659         struct clk_hw_omap *oclk;
660
661         list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
662                 if (&oclk->hw == hw)
663                         return true;
664         }
665
666         return false;
667 }