GNU Linux-libre 5.10.217-gnu1
[releases.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  * ti_dt_clocks_register - register DT alias clocks during boot
124  * @oclks: list of clocks to register
125  *
126  * Register alias or non-standard DT clock entries during boot. By
127  * default, DT clocks are found based on their node name. If any
128  * additional con-id / dev-id -> clock mapping is required, use this
129  * function to list these.
130  */
131 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
132 {
133         struct ti_dt_clk *c;
134         struct device_node *node, *parent, *child;
135         struct clk *clk;
136         struct of_phandle_args clkspec;
137         char buf[64];
138         char *ptr;
139         char *tags[2];
140         int i;
141         int num_args;
142         int ret;
143         static bool clkctrl_nodes_missing;
144         static bool has_clkctrl_data;
145         static bool compat_mode;
146
147         compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
148
149         for (c = oclks; c->node_name != NULL; c++) {
150                 strcpy(buf, c->node_name);
151                 ptr = buf;
152                 for (i = 0; i < 2; i++)
153                         tags[i] = NULL;
154                 num_args = 0;
155                 while (*ptr) {
156                         if (*ptr == ':') {
157                                 if (num_args >= 2) {
158                                         pr_warn("Bad number of tags on %s\n",
159                                                 c->node_name);
160                                         return;
161                                 }
162                                 tags[num_args++] = ptr + 1;
163                                 *ptr = 0;
164                         }
165                         ptr++;
166                 }
167
168                 if (num_args && clkctrl_nodes_missing)
169                         continue;
170
171                 node = of_find_node_by_name(NULL, buf);
172                 if (num_args && compat_mode) {
173                         parent = node;
174                         child = of_get_child_by_name(parent, "clock");
175                         if (!child)
176                                 child = of_get_child_by_name(parent, "clk");
177                         if (child) {
178                                 of_node_put(parent);
179                                 node = child;
180                         }
181                 }
182
183                 clkspec.np = node;
184                 clkspec.args_count = num_args;
185                 for (i = 0; i < num_args; i++) {
186                         ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
187                         if (ret) {
188                                 pr_warn("Bad tag in %s at %d: %s\n",
189                                         c->node_name, i, tags[i]);
190                                 of_node_put(node);
191                                 return;
192                         }
193                 }
194                 clk = of_clk_get_from_provider(&clkspec);
195                 of_node_put(node);
196                 if (!IS_ERR(clk)) {
197                         c->lk.clk = clk;
198                         clkdev_add(&c->lk);
199                 } else {
200                         if (num_args && !has_clkctrl_data) {
201                                 struct device_node *np;
202
203                                 np = of_find_compatible_node(NULL, NULL,
204                                                              "ti,clkctrl");
205                                 if (np) {
206                                         has_clkctrl_data = true;
207                                         of_node_put(np);
208                                 } else {
209                                         clkctrl_nodes_missing = true;
210
211                                         pr_warn("missing clkctrl nodes, please update your dts.\n");
212                                         continue;
213                                 }
214                         }
215
216                         pr_warn("failed to lookup clock node %s, ret=%ld\n",
217                                 c->node_name, PTR_ERR(clk));
218                 }
219         }
220 }
221
222 struct clk_init_item {
223         struct device_node *node;
224         void *user;
225         ti_of_clk_init_cb_t func;
226         struct list_head link;
227 };
228
229 static LIST_HEAD(retry_list);
230
231 /**
232  * ti_clk_retry_init - retries a failed clock init at later phase
233  * @node: device not for the clock
234  * @user: user data pointer
235  * @func: init function to be called for the clock
236  *
237  * Adds a failed clock init to the retry list. The retry list is parsed
238  * once all the other clocks have been initialized.
239  */
240 int __init ti_clk_retry_init(struct device_node *node, void *user,
241                              ti_of_clk_init_cb_t func)
242 {
243         struct clk_init_item *retry;
244
245         pr_debug("%pOFn: adding to retry list...\n", node);
246         retry = kzalloc(sizeof(*retry), GFP_KERNEL);
247         if (!retry)
248                 return -ENOMEM;
249
250         retry->node = node;
251         retry->func = func;
252         retry->user = user;
253         list_add(&retry->link, &retry_list);
254
255         return 0;
256 }
257
258 /**
259  * ti_clk_get_reg_addr - get register address for a clock register
260  * @node: device node for the clock
261  * @index: register index from the clock node
262  * @reg: pointer to target register struct
263  *
264  * Builds clock register address from device tree information, and returns
265  * the data via the provided output pointer @reg. Returns 0 on success,
266  * negative error value on failure.
267  */
268 int ti_clk_get_reg_addr(struct device_node *node, int index,
269                         struct clk_omap_reg *reg)
270 {
271         u32 val;
272         int i;
273
274         for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
275                 if (clocks_node_ptr[i] == node->parent)
276                         break;
277         }
278
279         if (i == CLK_MAX_MEMMAPS) {
280                 pr_err("clk-provider not found for %pOFn!\n", node);
281                 return -ENOENT;
282         }
283
284         reg->index = i;
285
286         if (of_property_read_u32_index(node, "reg", index, &val)) {
287                 pr_err("%pOFn must have reg[%d]!\n", node, index);
288                 return -EINVAL;
289         }
290
291         reg->offset = val;
292         reg->ptr = NULL;
293
294         return 0;
295 }
296
297 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
298 {
299         u32 latch;
300
301         if (shift < 0)
302                 return;
303
304         latch = 1 << shift;
305
306         ti_clk_ll_ops->clk_rmw(latch, latch, reg);
307         ti_clk_ll_ops->clk_rmw(0, latch, reg);
308         ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
309 }
310
311 /**
312  * omap2_clk_provider_init - init master clock provider
313  * @parent: master node
314  * @index: internal index for clk_reg_ops
315  * @syscon: syscon regmap pointer for accessing clock registers
316  * @mem: iomem pointer for the clock provider memory area, only used if
317  *       syscon is not provided
318  *
319  * Initializes a master clock IP block. This basically sets up the
320  * mapping from clocks node to the memory map index. All the clocks
321  * are then initialized through the common of_clk_init call, and the
322  * clocks will access their memory maps based on the node layout.
323  * Returns 0 in success.
324  */
325 int __init omap2_clk_provider_init(struct device_node *parent, int index,
326                                    struct regmap *syscon, void __iomem *mem)
327 {
328         struct device_node *clocks;
329         struct clk_iomap *io;
330
331         /* get clocks for this parent */
332         clocks = of_get_child_by_name(parent, "clocks");
333         if (!clocks) {
334                 pr_err("%pOFn missing 'clocks' child node.\n", parent);
335                 return -EINVAL;
336         }
337
338         /* add clocks node info */
339         clocks_node_ptr[index] = clocks;
340
341         io = kzalloc(sizeof(*io), GFP_KERNEL);
342         if (!io)
343                 return -ENOMEM;
344
345         io->regmap = syscon;
346         io->mem = mem;
347
348         clk_memmaps[index] = io;
349
350         return 0;
351 }
352
353 /**
354  * omap2_clk_legacy_provider_init - initialize a legacy clock provider
355  * @index: index for the clock provider
356  * @mem: iomem pointer for the clock provider memory area
357  *
358  * Initializes a legacy clock provider memory mapping.
359  */
360 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
361 {
362         struct clk_iomap *io;
363
364         io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
365         if (!io)
366                 panic("%s: Failed to allocate %zu bytes\n", __func__,
367                       sizeof(*io));
368
369         io->mem = mem;
370
371         clk_memmaps[index] = io;
372 }
373
374 /**
375  * ti_dt_clk_init_retry_clks - init clocks from the retry list
376  *
377  * Initializes any clocks that have failed to initialize before,
378  * reasons being missing parent node(s) during earlier init. This
379  * typically happens only for DPLLs which need to have both of their
380  * parent clocks ready during init.
381  */
382 void ti_dt_clk_init_retry_clks(void)
383 {
384         struct clk_init_item *retry;
385         struct clk_init_item *tmp;
386         int retries = 5;
387
388         while (!list_empty(&retry_list) && retries) {
389                 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
390                         pr_debug("retry-init: %pOFn\n", retry->node);
391                         retry->func(retry->user, retry->node);
392                         list_del(&retry->link);
393                         kfree(retry);
394                 }
395                 retries--;
396         }
397 }
398
399 static const struct of_device_id simple_clk_match_table[] __initconst = {
400         { .compatible = "fixed-clock" },
401         { .compatible = "fixed-factor-clock" },
402         { }
403 };
404
405 /**
406  * ti_dt_clk_name - init clock name from first output name or node name
407  * @np: device node
408  *
409  * Use the first clock-output-name for the clock name if found. Fall back
410  * to legacy naming based on node name.
411  */
412 const char *ti_dt_clk_name(struct device_node *np)
413 {
414         const char *name;
415
416         if (!of_property_read_string_index(np, "clock-output-names", 0,
417                                            &name))
418                 return name;
419
420         return np->name;
421 }
422
423 /**
424  * ti_clk_add_aliases - setup clock aliases
425  *
426  * Sets up any missing clock aliases. No return value.
427  */
428 void __init ti_clk_add_aliases(void)
429 {
430         struct device_node *np;
431         struct clk *clk;
432
433         for_each_matching_node(np, simple_clk_match_table) {
434                 struct of_phandle_args clkspec;
435
436                 clkspec.np = np;
437                 clk = of_clk_get_from_provider(&clkspec);
438
439                 ti_clk_add_alias(clk, ti_dt_clk_name(np));
440         }
441 }
442
443 /**
444  * ti_clk_setup_features - setup clock features flags
445  * @features: features definition to use
446  *
447  * Initializes the clock driver features flags based on platform
448  * provided data. No return value.
449  */
450 void __init ti_clk_setup_features(struct ti_clk_features *features)
451 {
452         memcpy(&ti_clk_features, features, sizeof(*features));
453 }
454
455 /**
456  * ti_clk_get_features - get clock driver features flags
457  *
458  * Get TI clock driver features description. Returns a pointer
459  * to the current feature setup.
460  */
461 const struct ti_clk_features *ti_clk_get_features(void)
462 {
463         return &ti_clk_features;
464 }
465
466 /**
467  * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
468  * @clk_names: ptr to an array of strings of clock names to enable
469  * @num_clocks: number of clock names in @clk_names
470  *
471  * Prepare and enable a list of clocks, named by @clk_names.  No
472  * return value. XXX Deprecated; only needed until these clocks are
473  * properly claimed and enabled by the drivers or core code that uses
474  * them.  XXX What code disables & calls clk_put on these clocks?
475  */
476 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
477 {
478         struct clk *init_clk;
479         int i;
480
481         for (i = 0; i < num_clocks; i++) {
482                 init_clk = clk_get(NULL, clk_names[i]);
483                 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
484                          clk_names[i]))
485                         continue;
486                 clk_prepare_enable(init_clk);
487         }
488 }
489
490 /**
491  * ti_clk_add_alias - add a clock alias for a TI clock
492  * @clk: clock handle to create alias for
493  * @con: connection ID for this clock
494  *
495  * Creates a clock alias for a TI clock. Allocates the clock lookup entry
496  * and assigns the data to it. Returns 0 if successful, negative error
497  * value otherwise.
498  */
499 int ti_clk_add_alias(struct clk *clk, const char *con)
500 {
501         struct clk_lookup *cl;
502
503         if (!clk)
504                 return 0;
505
506         if (IS_ERR(clk))
507                 return PTR_ERR(clk);
508
509         cl = kzalloc(sizeof(*cl), GFP_KERNEL);
510         if (!cl)
511                 return -ENOMEM;
512
513         cl->con_id = con;
514         cl->clk = clk;
515
516         clkdev_add(cl);
517
518         return 0;
519 }
520
521 /**
522  * of_ti_clk_register - register a TI clock to the common clock framework
523  * @node: device node for this clock
524  * @hw: hardware clock handle
525  * @con: connection ID for this clock
526  *
527  * Registers a TI clock to the common clock framework, and adds a clock
528  * alias for it. Returns a handle to the registered clock if successful,
529  * ERR_PTR value in failure.
530  */
531 struct clk *of_ti_clk_register(struct device_node *node, struct clk_hw *hw,
532                                const char *con)
533 {
534         struct clk *clk;
535         int ret;
536
537         ret = of_clk_hw_register(node, hw);
538         if (ret)
539                 return ERR_PTR(ret);
540
541         clk = hw->clk;
542         ret = ti_clk_add_alias(clk, con);
543         if (ret) {
544                 clk_unregister(clk);
545                 return ERR_PTR(ret);
546         }
547
548         return clk;
549 }
550
551 /**
552  * of_ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
553  * @node: device node for this clock
554  * @hw: hardware clock handle
555  * @con: connection ID for this clock
556  *
557  * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
558  * for it, and adds the list to the available clk_hw_omap type clocks.
559  * Returns a handle to the registered clock if successful, ERR_PTR value
560  * in failure.
561  */
562 struct clk *of_ti_clk_register_omap_hw(struct device_node *node,
563                                        struct clk_hw *hw, const char *con)
564 {
565         struct clk *clk;
566         struct clk_hw_omap *oclk;
567
568         clk = of_ti_clk_register(node, hw, con);
569         if (IS_ERR(clk))
570                 return clk;
571
572         oclk = to_clk_hw_omap(hw);
573
574         list_add(&oclk->node, &clk_hw_omap_clocks);
575
576         return clk;
577 }
578
579 /**
580  * omap2_clk_for_each - call function for each registered clk_hw_omap
581  * @fn: pointer to a callback function
582  *
583  * Call @fn for each registered clk_hw_omap, passing @hw to each
584  * function.  @fn must return 0 for success or any other value for
585  * failure.  If @fn returns non-zero, the iteration across clocks
586  * will stop and the non-zero return value will be passed to the
587  * caller of omap2_clk_for_each().
588  */
589 int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
590 {
591         int ret;
592         struct clk_hw_omap *hw;
593
594         list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
595                 ret = (*fn)(hw);
596                 if (ret)
597                         break;
598         }
599
600         return ret;
601 }
602
603 /**
604  * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
605  * @hw: clk_hw to check if it is an omap clock or not
606  *
607  * Checks if the provided clk_hw is OMAP clock or not. Returns true if
608  * it is, false otherwise.
609  */
610 bool omap2_clk_is_hw_omap(struct clk_hw *hw)
611 {
612         struct clk_hw_omap *oclk;
613
614         list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {
615                 if (&oclk->hw == hw)
616                         return true;
617         }
618
619         return false;
620 }