4 * Copyright (C) 2013 Texas Instruments, Inc.
6 * Tero Kristo <t-kristo@ti.com>
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.
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.
18 #include <linux/clk.h>
19 #include <linux/clk-provider.h>
20 #include <linux/clkdev.h>
21 #include <linux/clk/ti.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>
32 #define pr_fmt(fmt) "%s: " fmt, __func__
34 struct ti_clk_ll_ops *ti_clk_ll_ops;
35 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
37 static struct ti_clk_features ti_clk_features;
40 struct regmap *regmap;
44 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
46 static void clk_memmap_writel(u32 val, void __iomem *reg)
48 struct clk_omap_reg *r = (struct clk_omap_reg *)®
49 struct clk_iomap *io = clk_memmaps[r->index];
52 regmap_write(io->regmap, r->offset, val);
54 writel_relaxed(val, io->mem + r->offset);
57 static u32 clk_memmap_readl(void __iomem *reg)
60 struct clk_omap_reg *r = (struct clk_omap_reg *)®
61 struct clk_iomap *io = clk_memmaps[r->index];
64 regmap_read(io->regmap, r->offset, &val);
66 val = readl_relaxed(io->mem + r->offset);
72 * ti_clk_setup_ll_ops - setup low level clock operations
73 * @ops: low level clock ops descriptor
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
80 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
83 pr_err("Attempt to register ll_ops multiple times.\n");
88 ops->clk_readl = clk_memmap_readl;
89 ops->clk_writel = clk_memmap_writel;
95 * ti_dt_clocks_register - register DT alias clocks during boot
96 * @oclks: list of clocks to register
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.
103 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
106 struct device_node *node;
108 struct of_phandle_args clkspec;
110 for (c = oclks; c->node_name != NULL; c++) {
111 node = of_find_node_by_name(NULL, c->node_name);
113 clk = of_clk_get_from_provider(&clkspec);
119 pr_warn("failed to lookup clock node %s\n",
125 struct clk_init_item {
126 struct device_node *node;
128 ti_of_clk_init_cb_t func;
129 struct list_head link;
132 static LIST_HEAD(retry_list);
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
140 * Adds a failed clock init to the retry list. The retry list is parsed
141 * once all the other clocks have been initialized.
143 int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
144 ti_of_clk_init_cb_t func)
146 struct clk_init_item *retry;
148 pr_debug("%s: adding to retry list...\n", node->name);
149 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
156 list_add(&retry->link, &retry_list);
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
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.
170 void __iomem *ti_clk_get_reg_addr(struct device_node *node, int index)
172 struct clk_omap_reg *reg;
177 reg = (struct clk_omap_reg *)&tmp;
179 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
180 if (clocks_node_ptr[i] == node->parent)
184 if (i == CLK_MAX_MEMMAPS) {
185 pr_err("clk-provider not found for %s!\n", node->name);
186 return IOMEM_ERR_PTR(-ENOENT);
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);
198 return (__force void __iomem *)tmp;
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
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.
215 int __init omap2_clk_provider_init(struct device_node *parent, int index,
216 struct regmap *syscon, void __iomem *mem)
218 struct device_node *clocks;
219 struct clk_iomap *io;
221 /* get clocks for this parent */
222 clocks = of_get_child_by_name(parent, "clocks");
224 pr_err("%s missing 'clocks' child node.\n", parent->name);
228 /* add clocks node info */
229 clocks_node_ptr[index] = clocks;
231 io = kzalloc(sizeof(*io), GFP_KERNEL);
238 clk_memmaps[index] = io;
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
248 * Initializes a legacy clock provider memory mapping.
250 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
252 struct clk_iomap *io;
254 io = memblock_virt_alloc(sizeof(*io), 0);
258 clk_memmaps[index] = io;
262 * ti_dt_clk_init_retry_clks - init clocks from the retry list
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.
269 void ti_dt_clk_init_retry_clks(void)
271 struct clk_init_item *retry;
272 struct clk_init_item *tmp;
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);
286 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
287 void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
290 memcpy((*patch)->patch, *patch, sizeof(**patch));
295 struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
298 struct ti_clk_fixed *fixed;
299 struct ti_clk_fixed_factor *fixed_factor;
300 struct clk_hw *clk_hw;
306 switch (setup->type) {
310 clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
313 ret = ti_clk_add_alias(NULL, clk, setup->name);
321 clk = ti_clk_register_mux(setup);
324 clk = ti_clk_register_divider(setup);
326 case TI_CLK_COMPOSITE:
327 clk = ti_clk_register_composite(setup);
329 case TI_CLK_FIXED_FACTOR:
330 fixed_factor = setup->data;
332 clk = clk_register_fixed_factor(NULL, setup->name,
333 fixed_factor->parent,
334 0, fixed_factor->mult,
337 ret = ti_clk_add_alias(NULL, clk, setup->name);
345 clk = ti_clk_register_gate(setup);
348 clk = ti_clk_register_dpll(setup);
351 pr_err("bad type for %s!\n", setup->name);
352 clk = ERR_PTR(-EINVAL);
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",
363 to_clk_hw_omap(clk_hw)->clkdm_name =
365 omap2_init_clk_clkdm(clk_hw);
373 static const struct of_device_id simple_clk_match_table[] __initconst = {
374 { .compatible = "fixed-clock" },
375 { .compatible = "fixed-factor-clock" },
379 int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
383 struct ti_clk_alias *retry_clk;
384 struct ti_clk_alias *tmp;
387 clk = ti_clk_register_clk(clks->clk);
389 if (PTR_ERR(clk) == -EAGAIN) {
390 list_add(&clks->link, &retry_list);
392 pr_err("register for %s failed: %ld\n",
393 clks->clk->name, PTR_ERR(clk));
402 while (!list_empty(&retry_list) && retry) {
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);
408 if (PTR_ERR(clk) == -EAGAIN) {
411 pr_err("register for %s failed: %ld\n",
412 retry_clk->clk->name,
418 list_del(&retry_clk->link);
428 * ti_clk_add_aliases - setup clock aliases
430 * Sets up any missing clock aliases. No return value.
432 void __init ti_clk_add_aliases(void)
434 struct device_node *np;
437 for_each_matching_node(np, simple_clk_match_table) {
438 struct of_phandle_args clkspec;
441 clk = of_clk_get_from_provider(&clkspec);
443 ti_clk_add_alias(NULL, clk, np->name);
448 * ti_clk_setup_features - setup clock features flags
449 * @features: features definition to use
451 * Initializes the clock driver features flags based on platform
452 * provided data. No return value.
454 void __init ti_clk_setup_features(struct ti_clk_features *features)
456 memcpy(&ti_clk_features, features, sizeof(*features));
460 * ti_clk_get_features - get clock driver features flags
462 * Get TI clock driver features description. Returns a pointer
463 * to the current feature setup.
465 const struct ti_clk_features *ti_clk_get_features(void)
467 return &ti_clk_features;
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
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?
480 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
482 struct clk *init_clk;
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",
490 clk_prepare_enable(init_clk);
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
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
504 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
506 struct clk_lookup *cl;
514 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
519 cl->dev_id = dev_name(dev);
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
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.
538 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
544 clk = clk_register(dev, hw);
548 ret = ti_clk_add_alias(dev, clk, con);