blob: 024123f421d6f59e66eec834f302b9caaff364b5 [file] [log] [blame]
Tero Kristoa8acecc2013-07-18 11:52:33 +03001/*
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
Stephen Boyd1b29e602015-06-19 15:00:46 -070018#include <linux/clk.h>
Tero Kristoa8acecc2013-07-18 11:52:33 +030019#include <linux/clk-provider.h>
20#include <linux/clkdev.h>
21#include <linux/clk/ti.h>
22#include <linux/of.h>
Tero Kristo819b4862013-10-22 11:39:36 +030023#include <linux/of_address.h>
24#include <linux/list.h>
Tero Kristo989feaf2015-04-27 22:23:06 +030025#include <linux/regmap.h>
26#include <linux/bootmem.h>
Tero Kristo21f0bf22016-09-29 12:00:57 +030027#include <linux/device.h>
Tero Kristoa8acecc2013-07-18 11:52:33 +030028
Tero Kristoc82f8952014-12-16 18:20:46 +020029#include "clock.h"
30
Tero Kristoa8acecc2013-07-18 11:52:33 +030031#undef pr_fmt
32#define pr_fmt(fmt) "%s: " fmt, __func__
33
Tero Kristo819b4862013-10-22 11:39:36 +030034struct ti_clk_ll_ops *ti_clk_ll_ops;
Tero Kristoc08ee142014-09-12 15:01:57 +030035static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
Tero Kristo819b4862013-10-22 11:39:36 +030036
Stephen Boyd3fe6d692015-07-15 12:03:52 -070037static struct ti_clk_features ti_clk_features;
Tero Kristof3b19aa2015-02-27 17:54:14 +020038
Tero Kristo989feaf2015-04-27 22:23:06 +030039struct clk_iomap {
40 struct regmap *regmap;
41 void __iomem *mem;
42};
43
44static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
45
46static 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
57static 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
Tero Kristoa8acecc2013-07-18 11:52:33 +030071/**
Tero Kristoe9e63082015-04-27 21:55:42 +030072 * 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 */
80int 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;
Tero Kristo989feaf2015-04-27 22:23:06 +030088 ops->clk_readl = clk_memmap_readl;
89 ops->clk_writel = clk_memmap_writel;
Tero Kristoe9e63082015-04-27 21:55:42 +030090
91 return 0;
92}
93
Tero Kristoa8acecc2013-07-18 11:52:33 +030094/**
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 */
103void __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}
Tero Kristo819b4862013-10-22 11:39:36 +0300124
125struct 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
132static 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 */
143int __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
Tero Kristoc807dbe2015-02-23 21:06:08 +0200167 * is a struct of type clk_omap_reg. Returns a pointer to the register
168 * address, or a pointer error value in failure.
Tero Kristo819b4862013-10-22 11:39:36 +0300169 */
170void __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;
Tero Kristoc08ee142014-09-12 15:01:57 +0300175 int i;
Tero Kristo819b4862013-10-22 11:39:36 +0300176
177 reg = (struct clk_omap_reg *)&tmp;
Tero Kristoc08ee142014-09-12 15:01:57 +0300178
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);
Stephen Boyd412d6b42015-05-01 12:59:32 -0700186 return IOMEM_ERR_PTR(-ENOENT);
Tero Kristoc08ee142014-09-12 15:01:57 +0300187 }
188
189 reg->index = i;
Tero Kristo819b4862013-10-22 11:39:36 +0300190
191 if (of_property_read_u32_index(node, "reg", index, &val)) {
192 pr_err("%s must have reg[%d]!\n", node->name, index);
Stephen Boyd412d6b42015-05-01 12:59:32 -0700193 return IOMEM_ERR_PTR(-EINVAL);
Tero Kristo819b4862013-10-22 11:39:36 +0300194 }
195
196 reg->offset = val;
197
Stephen Boyd14cc4e92015-07-15 12:58:22 -0700198 return (__force void __iomem *)tmp;
Tero Kristo819b4862013-10-22 11:39:36 +0300199}
200
201/**
Tero Kristo989feaf2015-04-27 22:23:06 +0300202 * omap2_clk_provider_init - init master clock provider
Tero Kristo819b4862013-10-22 11:39:36 +0300203 * @parent: master node
204 * @index: internal index for clk_reg_ops
Tero Kristo989feaf2015-04-27 22:23:06 +0300205 * @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
Tero Kristo819b4862013-10-22 11:39:36 +0300208 *
Tero Kristoc08ee142014-09-12 15:01:57 +0300209 * 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.
Tero Kristo989feaf2015-04-27 22:23:06 +0300213 * Returns 0 in success.
Tero Kristo819b4862013-10-22 11:39:36 +0300214 */
Tero Kristo989feaf2015-04-27 22:23:06 +0300215int __init omap2_clk_provider_init(struct device_node *parent, int index,
216 struct regmap *syscon, void __iomem *mem)
Tero Kristo819b4862013-10-22 11:39:36 +0300217{
Tero Kristo819b4862013-10-22 11:39:36 +0300218 struct device_node *clocks;
Tero Kristo989feaf2015-04-27 22:23:06 +0300219 struct clk_iomap *io;
Tero Kristo819b4862013-10-22 11:39:36 +0300220
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);
Tero Kristo989feaf2015-04-27 22:23:06 +0300225 return -EINVAL;
Tero Kristo819b4862013-10-22 11:39:36 +0300226 }
227
Tero Kristoc08ee142014-09-12 15:01:57 +0300228 /* add clocks node info */
229 clocks_node_ptr[index] = clocks;
Tero Kristo989feaf2015-04-27 22:23:06 +0300230
231 io = kzalloc(sizeof(*io), GFP_KERNEL);
Stephen Boydf645f72d2015-07-15 11:55:42 -0700232 if (!io)
233 return -ENOMEM;
Tero Kristo989feaf2015-04-27 22:23:06 +0300234
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 */
250void __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;
Tero Kristoc08ee142014-09-12 15:01:57 +0300259}
Tero Kristo819b4862013-10-22 11:39:36 +0300260
Tero Kristoc08ee142014-09-12 15:01:57 +0300261/**
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 */
269void 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--;
Tero Kristo819b4862013-10-22 11:39:36 +0300283 }
284}
Tero Kristoc82f8952014-12-16 18:20:46 +0200285
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100286#if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
Tero Kristoc82f8952014-12-16 18:20:46 +0200287void __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
295struct 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;
Tero Kristo1ae79c42016-09-29 12:06:40 +0300301 int ret;
Tero Kristoc82f8952014-12-16 18:20:46 +0200302
303 if (setup->clk)
304 return setup->clk;
305
306 switch (setup->type) {
307 case TI_CLK_FIXED:
308 fixed = setup->data;
309
Stephen Boyd45e21152016-03-01 11:00:03 -0800310 clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
311 fixed->frequency);
Tero Kristo1ae79c42016-09-29 12:06:40 +0300312 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 }
Tero Kristoc82f8952014-12-16 18:20:46 +0200319 break;
Tero Kristo7c18a652014-12-16 18:20:47 +0200320 case TI_CLK_MUX:
321 clk = ti_clk_register_mux(setup);
322 break;
Tero Kristod96f7742014-12-16 18:20:50 +0200323 case TI_CLK_DIVIDER:
324 clk = ti_clk_register_divider(setup);
325 break;
Tero Kristob26bcf92014-12-16 18:20:52 +0200326 case TI_CLK_COMPOSITE:
327 clk = ti_clk_register_composite(setup);
328 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200329 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);
Tero Kristo1ae79c42016-09-29 12:06:40 +0300336 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 }
Tero Kristoc82f8952014-12-16 18:20:46 +0200343 break;
Tero Kristof1876162014-12-16 18:20:48 +0200344 case TI_CLK_GATE:
345 clk = ti_clk_register_gate(setup);
346 break;
Tero Kristoed405a22015-01-29 22:24:28 +0200347 case TI_CLK_DPLL:
348 clk = ti_clk_register_dpll(setup);
349 break;
Tero Kristoc82f8952014-12-16 18:20:46 +0200350 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) {
Stephen Boyd98d8a602015-06-29 16:56:30 -0700358 clk_hw = __clk_get_hw(clk);
359 if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
Tero Kristoc82f8952014-12-16 18:20:46 +0200360 pr_warn("can't setup clkdm for basic clk %s\n",
361 setup->name);
362 } else {
Tero Kristoc82f8952014-12-16 18:20:46 +0200363 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
Tero Kristoc17435c2016-09-29 12:05:00 +0300373static const struct of_device_id simple_clk_match_table[] __initconst = {
374 { .compatible = "fixed-clock" },
375 { .compatible = "fixed-factor-clock" },
376 { }
377};
378
Tero Kristoc82f8952014-12-16 18:20:46 +0200379int __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 }
Tero Kristoc82f8952014-12-16 18:20:46 +0200396 }
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;
Tero Kristoc82f8952014-12-16 18:20:46 +0200418 list_del(&retry_clk->link);
419 }
420 }
421 }
422
423 return 0;
424}
Arnd Bergmann6793a30a2015-02-03 17:59:32 +0100425#endif
Tero Kristof3b19aa2015-02-27 17:54:14 +0200426
427/**
Tero Kristoc17435c2016-09-29 12:05:00 +0300428 * ti_clk_add_aliases - setup clock aliases
429 *
430 * Sets up any missing clock aliases. No return value.
431 */
432void __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/**
Tero Kristof3b19aa2015-02-27 17:54:14 +0200448 * 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 */
454void __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 */
465const struct ti_clk_features *ti_clk_get_features(void)
466{
467 return &ti_clk_features;
468}
Tero Kristoa5aa8a62015-03-03 10:51:01 +0200469
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 */
480void 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}
Tero Kristo21f0bf22016-09-29 12:00:57 +0300493
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 */
504int 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 */
538struct 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}