clk: mediatek: using CLK_MUX_ROUND_CLOSEST for the clock of dpi1_sel
[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/of.h>
23 #include <linux/of_address.h>
24 #include <linux/list.h>
25 #include <linux/regmap.h>
26 #include <linux/memblock.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 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, const struct clk_omap_reg *reg)
47 {
48         struct clk_iomap *io = clk_memmaps[reg->index];
49
50         if (reg->ptr)
51                 writel_relaxed(val, reg->ptr);
52         else if (io->regmap)
53                 regmap_write(io->regmap, reg->offset, val);
54         else
55                 writel_relaxed(val, io->mem + reg->offset);
56 }
57
58 static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
59 {
60         u32 v;
61
62         v = readl_relaxed(ptr);
63         v &= ~mask;
64         v |= val;
65         writel_relaxed(v, ptr);
66 }
67
68 static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
69 {
70         struct clk_iomap *io = clk_memmaps[reg->index];
71
72         if (reg->ptr) {
73                 _clk_rmw(val, mask, reg->ptr);
74         } else if (io->regmap) {
75                 regmap_update_bits(io->regmap, reg->offset, mask, val);
76         } else {
77                 _clk_rmw(val, mask, io->mem + reg->offset);
78         }
79 }
80
81 static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
82 {
83         u32 val;
84         struct clk_iomap *io = clk_memmaps[reg->index];
85
86         if (reg->ptr)
87                 val = readl_relaxed(reg->ptr);
88         else if (io->regmap)
89                 regmap_read(io->regmap, reg->offset, &val);
90         else
91                 val = readl_relaxed(io->mem + reg->offset);
92
93         return val;
94 }
95
96 /**
97  * ti_clk_setup_ll_ops - setup low level clock operations
98  * @ops: low level clock ops descriptor
99  *
100  * Sets up low level clock operations for TI clock driver. This is used
101  * to provide various callbacks for the clock driver towards platform
102  * specific code. Returns 0 on success, -EBUSY if ll_ops have been
103  * registered already.
104  */
105 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
106 {
107         if (ti_clk_ll_ops) {
108                 pr_err("Attempt to register ll_ops multiple times.\n");
109                 return -EBUSY;
110         }
111
112         ti_clk_ll_ops = ops;
113         ops->clk_readl = clk_memmap_readl;
114         ops->clk_writel = clk_memmap_writel;
115         ops->clk_rmw = clk_memmap_rmw;
116
117         return 0;
118 }
119
120 /**
121  * ti_dt_clocks_register - register DT alias clocks during boot
122  * @oclks: list of clocks to register
123  *
124  * Register alias or non-standard DT clock entries during boot. By
125  * default, DT clocks are found based on their node name. If any
126  * additional con-id / dev-id -> clock mapping is required, use this
127  * function to list these.
128  */
129 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
130 {
131         struct ti_dt_clk *c;
132         struct device_node *node, *parent;
133         struct clk *clk;
134         struct of_phandle_args clkspec;
135         char buf[64];
136         char *ptr;
137         char *tags[2];
138         int i;
139         int num_args;
140         int ret;
141         static bool clkctrl_nodes_missing;
142         static bool has_clkctrl_data;
143         static bool compat_mode;
144
145         compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
146
147         for (c = oclks; c->node_name != NULL; c++) {
148                 strcpy(buf, c->node_name);
149                 ptr = buf;
150                 for (i = 0; i < 2; i++)
151                         tags[i] = NULL;
152                 num_args = 0;
153                 while (*ptr) {
154                         if (*ptr == ':') {
155                                 if (num_args >= 2) {
156                                         pr_warn("Bad number of tags on %s\n",
157                                                 c->node_name);
158                                         return;
159                                 }
160                                 tags[num_args++] = ptr + 1;
161                                 *ptr = 0;
162                         }
163                         ptr++;
164                 }
165
166                 if (num_args && clkctrl_nodes_missing)
167                         continue;
168
169                 node = of_find_node_by_name(NULL, buf);
170                 if (num_args && compat_mode) {
171                         parent = node;
172                         node = of_get_child_by_name(parent, "clk");
173                         of_node_put(parent);
174                 }
175
176                 clkspec.np = node;
177                 clkspec.args_count = num_args;
178                 for (i = 0; i < num_args; i++) {
179                         ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
180                         if (ret) {
181                                 pr_warn("Bad tag in %s at %d: %s\n",
182                                         c->node_name, i, tags[i]);
183                                 of_node_put(node);
184                                 return;
185                         }
186                 }
187                 clk = of_clk_get_from_provider(&clkspec);
188                 of_node_put(node);
189                 if (!IS_ERR(clk)) {
190                         c->lk.clk = clk;
191                         clkdev_add(&c->lk);
192                 } else {
193                         if (num_args && !has_clkctrl_data) {
194                                 if (of_find_compatible_node(NULL, NULL,
195                                                             "ti,clkctrl")) {
196                                         has_clkctrl_data = true;
197                                 } else {
198                                         clkctrl_nodes_missing = true;
199
200                                         pr_warn("missing clkctrl nodes, please update your dts.\n");
201                                         continue;
202                                 }
203                         }
204
205                         pr_warn("failed to lookup clock node %s, ret=%ld\n",
206                                 c->node_name, PTR_ERR(clk));
207                 }
208         }
209 }
210
211 struct clk_init_item {
212         struct device_node *node;
213         void *user;
214         ti_of_clk_init_cb_t func;
215         struct list_head link;
216 };
217
218 static LIST_HEAD(retry_list);
219
220 /**
221  * ti_clk_retry_init - retries a failed clock init at later phase
222  * @node: device not for the clock
223  * @user: user data pointer
224  * @func: init function to be called for the clock
225  *
226  * Adds a failed clock init to the retry list. The retry list is parsed
227  * once all the other clocks have been initialized.
228  */
229 int __init ti_clk_retry_init(struct device_node *node, void *user,
230                              ti_of_clk_init_cb_t func)
231 {
232         struct clk_init_item *retry;
233
234         pr_debug("%pOFn: adding to retry list...\n", node);
235         retry = kzalloc(sizeof(*retry), GFP_KERNEL);
236         if (!retry)
237                 return -ENOMEM;
238
239         retry->node = node;
240         retry->func = func;
241         retry->user = user;
242         list_add(&retry->link, &retry_list);
243
244         return 0;
245 }
246
247 /**
248  * ti_clk_get_reg_addr - get register address for a clock register
249  * @node: device node for the clock
250  * @index: register index from the clock node
251  * @reg: pointer to target register struct
252  *
253  * Builds clock register address from device tree information, and returns
254  * the data via the provided output pointer @reg. Returns 0 on success,
255  * negative error value on failure.
256  */
257 int ti_clk_get_reg_addr(struct device_node *node, int index,
258                         struct clk_omap_reg *reg)
259 {
260         u32 val;
261         int i;
262
263         for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
264                 if (clocks_node_ptr[i] == node->parent)
265                         break;
266         }
267
268         if (i == CLK_MAX_MEMMAPS) {
269                 pr_err("clk-provider not found for %pOFn!\n", node);
270                 return -ENOENT;
271         }
272
273         reg->index = i;
274
275         if (of_property_read_u32_index(node, "reg", index, &val)) {
276                 pr_err("%pOFn must have reg[%d]!\n", node, index);
277                 return -EINVAL;
278         }
279
280         reg->offset = val;
281         reg->ptr = NULL;
282
283         return 0;
284 }
285
286 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
287 {
288         u32 latch;
289
290         if (shift < 0)
291                 return;
292
293         latch = 1 << shift;
294
295         ti_clk_ll_ops->clk_rmw(latch, latch, reg);
296         ti_clk_ll_ops->clk_rmw(0, latch, reg);
297         ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
298 }
299
300 /**
301  * omap2_clk_provider_init - init master clock provider
302  * @parent: master node
303  * @index: internal index for clk_reg_ops
304  * @syscon: syscon regmap pointer for accessing clock registers
305  * @mem: iomem pointer for the clock provider memory area, only used if
306  *       syscon is not provided
307  *
308  * Initializes a master clock IP block. This basically sets up the
309  * mapping from clocks node to the memory map index. All the clocks
310  * are then initialized through the common of_clk_init call, and the
311  * clocks will access their memory maps based on the node layout.
312  * Returns 0 in success.
313  */
314 int __init omap2_clk_provider_init(struct device_node *parent, int index,
315                                    struct regmap *syscon, void __iomem *mem)
316 {
317         struct device_node *clocks;
318         struct clk_iomap *io;
319
320         /* get clocks for this parent */
321         clocks = of_get_child_by_name(parent, "clocks");
322         if (!clocks) {
323                 pr_err("%pOFn missing 'clocks' child node.\n", parent);
324                 return -EINVAL;
325         }
326
327         /* add clocks node info */
328         clocks_node_ptr[index] = clocks;
329
330         io = kzalloc(sizeof(*io), GFP_KERNEL);
331         if (!io)
332                 return -ENOMEM;
333
334         io->regmap = syscon;
335         io->mem = mem;
336
337         clk_memmaps[index] = io;
338
339         return 0;
340 }
341
342 /**
343  * omap2_clk_legacy_provider_init - initialize a legacy clock provider
344  * @index: index for the clock provider
345  * @mem: iomem pointer for the clock provider memory area
346  *
347  * Initializes a legacy clock provider memory mapping.
348  */
349 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
350 {
351         struct clk_iomap *io;
352
353         io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
354
355         io->mem = mem;
356
357         clk_memmaps[index] = io;
358 }
359
360 /**
361  * ti_dt_clk_init_retry_clks - init clocks from the retry list
362  *
363  * Initializes any clocks that have failed to initialize before,
364  * reasons being missing parent node(s) during earlier init. This
365  * typically happens only for DPLLs which need to have both of their
366  * parent clocks ready during init.
367  */
368 void ti_dt_clk_init_retry_clks(void)
369 {
370         struct clk_init_item *retry;
371         struct clk_init_item *tmp;
372         int retries = 5;
373
374         while (!list_empty(&retry_list) && retries) {
375                 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
376                         pr_debug("retry-init: %pOFn\n", retry->node);
377                         retry->func(retry->user, retry->node);
378                         list_del(&retry->link);
379                         kfree(retry);
380                 }
381                 retries--;
382         }
383 }
384
385 static const struct of_device_id simple_clk_match_table[] __initconst = {
386         { .compatible = "fixed-clock" },
387         { .compatible = "fixed-factor-clock" },
388         { }
389 };
390
391 /**
392  * ti_clk_add_aliases - setup clock aliases
393  *
394  * Sets up any missing clock aliases. No return value.
395  */
396 void __init ti_clk_add_aliases(void)
397 {
398         struct device_node *np;
399         struct clk *clk;
400
401         for_each_matching_node(np, simple_clk_match_table) {
402                 struct of_phandle_args clkspec;
403
404                 clkspec.np = np;
405                 clk = of_clk_get_from_provider(&clkspec);
406
407                 ti_clk_add_alias(NULL, clk, np->name);
408         }
409 }
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 }
457
458 /**
459  * ti_clk_add_alias - add a clock alias for a TI clock
460  * @dev: device alias for this clock
461  * @clk: clock handle to create alias for
462  * @con: connection ID for this clock
463  *
464  * Creates a clock alias for a TI clock. Allocates the clock lookup entry
465  * and assigns the data to it. Returns 0 if successful, negative error
466  * value otherwise.
467  */
468 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
469 {
470         struct clk_lookup *cl;
471
472         if (!clk)
473                 return 0;
474
475         if (IS_ERR(clk))
476                 return PTR_ERR(clk);
477
478         cl = kzalloc(sizeof(*cl), GFP_KERNEL);
479         if (!cl)
480                 return -ENOMEM;
481
482         if (dev)
483                 cl->dev_id = dev_name(dev);
484         cl->con_id = con;
485         cl->clk = clk;
486
487         clkdev_add(cl);
488
489         return 0;
490 }
491
492 /**
493  * ti_clk_register - register a TI clock to the common clock framework
494  * @dev: device for this clock
495  * @hw: hardware clock handle
496  * @con: connection ID for this clock
497  *
498  * Registers a TI clock to the common clock framework, and adds a clock
499  * alias for it. Returns a handle to the registered clock if successful,
500  * ERR_PTR value in failure.
501  */
502 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
503                             const char *con)
504 {
505         struct clk *clk;
506         int ret;
507
508         clk = clk_register(dev, hw);
509         if (IS_ERR(clk))
510                 return clk;
511
512         ret = ti_clk_add_alias(dev, clk, con);
513         if (ret) {
514                 clk_unregister(clk);
515                 return ERR_PTR(ret);
516         }
517
518         return clk;
519 }