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, const struct clk_omap_reg *reg)
48 struct clk_iomap *io = clk_memmaps[reg->index];
51 writel_relaxed(val, reg->ptr);
53 regmap_write(io->regmap, reg->offset, val);
55 writel_relaxed(val, io->mem + reg->offset);
58 static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
61 struct clk_iomap *io = clk_memmaps[reg->index];
64 val = readl_relaxed(reg->ptr);
66 regmap_read(io->regmap, reg->offset, &val);
68 val = readl_relaxed(io->mem + reg->offset);
74 * ti_clk_setup_ll_ops - setup low level clock operations
75 * @ops: low level clock ops descriptor
77 * Sets up low level clock operations for TI clock driver. This is used
78 * to provide various callbacks for the clock driver towards platform
79 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
82 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
85 pr_err("Attempt to register ll_ops multiple times.\n");
90 ops->clk_readl = clk_memmap_readl;
91 ops->clk_writel = clk_memmap_writel;
97 * ti_dt_clocks_register - register DT alias clocks during boot
98 * @oclks: list of clocks to register
100 * Register alias or non-standard DT clock entries during boot. By
101 * default, DT clocks are found based on their node name. If any
102 * additional con-id / dev-id -> clock mapping is required, use this
103 * function to list these.
105 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
108 struct device_node *node;
110 struct of_phandle_args clkspec;
112 for (c = oclks; c->node_name != NULL; c++) {
113 node = of_find_node_by_name(NULL, c->node_name);
115 clk = of_clk_get_from_provider(&clkspec);
121 pr_warn("failed to lookup clock node %s\n",
127 struct clk_init_item {
128 struct device_node *node;
130 ti_of_clk_init_cb_t func;
131 struct list_head link;
134 static LIST_HEAD(retry_list);
137 * ti_clk_retry_init - retries a failed clock init at later phase
138 * @node: device not for the clock
139 * @hw: partially initialized clk_hw struct for the clock
140 * @func: init function to be called for the clock
142 * Adds a failed clock init to the retry list. The retry list is parsed
143 * once all the other clocks have been initialized.
145 int __init ti_clk_retry_init(struct device_node *node, struct clk_hw *hw,
146 ti_of_clk_init_cb_t func)
148 struct clk_init_item *retry;
150 pr_debug("%s: adding to retry list...\n", node->name);
151 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
158 list_add(&retry->link, &retry_list);
164 * ti_clk_get_reg_addr - get register address for a clock register
165 * @node: device node for the clock
166 * @index: register index from the clock node
167 * @reg: pointer to target register struct
169 * Builds clock register address from device tree information, and returns
170 * the data via the provided output pointer @reg. Returns 0 on success,
171 * negative error value on failure.
173 int ti_clk_get_reg_addr(struct device_node *node, int index,
174 struct clk_omap_reg *reg)
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);
191 if (of_property_read_u32_index(node, "reg", index, &val)) {
192 pr_err("%s must have reg[%d]!\n", node->name, index);
203 * omap2_clk_provider_init - init master clock provider
204 * @parent: master node
205 * @index: internal index for clk_reg_ops
206 * @syscon: syscon regmap pointer for accessing clock registers
207 * @mem: iomem pointer for the clock provider memory area, only used if
208 * syscon is not provided
210 * Initializes a master clock IP block. This basically sets up the
211 * mapping from clocks node to the memory map index. All the clocks
212 * are then initialized through the common of_clk_init call, and the
213 * clocks will access their memory maps based on the node layout.
214 * Returns 0 in success.
216 int __init omap2_clk_provider_init(struct device_node *parent, int index,
217 struct regmap *syscon, void __iomem *mem)
219 struct device_node *clocks;
220 struct clk_iomap *io;
222 /* get clocks for this parent */
223 clocks = of_get_child_by_name(parent, "clocks");
225 pr_err("%s missing 'clocks' child node.\n", parent->name);
229 /* add clocks node info */
230 clocks_node_ptr[index] = clocks;
232 io = kzalloc(sizeof(*io), GFP_KERNEL);
239 clk_memmaps[index] = io;
245 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
246 * @index: index for the clock provider
247 * @mem: iomem pointer for the clock provider memory area
249 * Initializes a legacy clock provider memory mapping.
251 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
253 struct clk_iomap *io;
255 io = memblock_virt_alloc(sizeof(*io), 0);
259 clk_memmaps[index] = io;
263 * ti_dt_clk_init_retry_clks - init clocks from the retry list
265 * Initializes any clocks that have failed to initialize before,
266 * reasons being missing parent node(s) during earlier init. This
267 * typically happens only for DPLLs which need to have both of their
268 * parent clocks ready during init.
270 void ti_dt_clk_init_retry_clks(void)
272 struct clk_init_item *retry;
273 struct clk_init_item *tmp;
276 while (!list_empty(&retry_list) && retries) {
277 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
278 pr_debug("retry-init: %s\n", retry->node->name);
279 retry->func(retry->hw, retry->node);
280 list_del(&retry->link);
287 #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_ATAGS)
288 void __init ti_clk_patch_legacy_clks(struct ti_clk **patch)
291 memcpy((*patch)->patch, *patch, sizeof(**patch));
296 struct clk __init *ti_clk_register_clk(struct ti_clk *setup)
299 struct ti_clk_fixed *fixed;
300 struct ti_clk_fixed_factor *fixed_factor;
301 struct clk_hw *clk_hw;
307 switch (setup->type) {
311 clk = clk_register_fixed_rate(NULL, setup->name, NULL, 0,
314 ret = ti_clk_add_alias(NULL, clk, setup->name);
322 clk = ti_clk_register_mux(setup);
325 clk = ti_clk_register_divider(setup);
327 case TI_CLK_COMPOSITE:
328 clk = ti_clk_register_composite(setup);
330 case TI_CLK_FIXED_FACTOR:
331 fixed_factor = setup->data;
333 clk = clk_register_fixed_factor(NULL, setup->name,
334 fixed_factor->parent,
335 0, fixed_factor->mult,
338 ret = ti_clk_add_alias(NULL, clk, setup->name);
346 clk = ti_clk_register_gate(setup);
349 clk = ti_clk_register_dpll(setup);
352 pr_err("bad type for %s!\n", setup->name);
353 clk = ERR_PTR(-EINVAL);
358 if (setup->clkdm_name) {
359 clk_hw = __clk_get_hw(clk);
360 if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
361 pr_warn("can't setup clkdm for basic clk %s\n",
364 to_clk_hw_omap(clk_hw)->clkdm_name =
366 omap2_init_clk_clkdm(clk_hw);
374 int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
378 struct ti_clk_alias *retry_clk;
379 struct ti_clk_alias *tmp;
382 clk = ti_clk_register_clk(clks->clk);
384 if (PTR_ERR(clk) == -EAGAIN) {
385 list_add(&clks->link, &retry_list);
387 pr_err("register for %s failed: %ld\n",
388 clks->clk->name, PTR_ERR(clk));
397 while (!list_empty(&retry_list) && retry) {
399 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
400 pr_debug("retry-init: %s\n", retry_clk->clk->name);
401 clk = ti_clk_register_clk(retry_clk->clk);
403 if (PTR_ERR(clk) == -EAGAIN) {
406 pr_err("register for %s failed: %ld\n",
407 retry_clk->clk->name,
413 list_del(&retry_clk->link);
422 static const struct of_device_id simple_clk_match_table[] __initconst = {
423 { .compatible = "fixed-clock" },
424 { .compatible = "fixed-factor-clock" },
429 * ti_clk_add_aliases - setup clock aliases
431 * Sets up any missing clock aliases. No return value.
433 void __init ti_clk_add_aliases(void)
435 struct device_node *np;
438 for_each_matching_node(np, simple_clk_match_table) {
439 struct of_phandle_args clkspec;
442 clk = of_clk_get_from_provider(&clkspec);
444 ti_clk_add_alias(NULL, clk, np->name);
449 * ti_clk_setup_features - setup clock features flags
450 * @features: features definition to use
452 * Initializes the clock driver features flags based on platform
453 * provided data. No return value.
455 void __init ti_clk_setup_features(struct ti_clk_features *features)
457 memcpy(&ti_clk_features, features, sizeof(*features));
461 * ti_clk_get_features - get clock driver features flags
463 * Get TI clock driver features description. Returns a pointer
464 * to the current feature setup.
466 const struct ti_clk_features *ti_clk_get_features(void)
468 return &ti_clk_features;
472 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
473 * @clk_names: ptr to an array of strings of clock names to enable
474 * @num_clocks: number of clock names in @clk_names
476 * Prepare and enable a list of clocks, named by @clk_names. No
477 * return value. XXX Deprecated; only needed until these clocks are
478 * properly claimed and enabled by the drivers or core code that uses
479 * them. XXX What code disables & calls clk_put on these clocks?
481 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
483 struct clk *init_clk;
486 for (i = 0; i < num_clocks; i++) {
487 init_clk = clk_get(NULL, clk_names[i]);
488 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
491 clk_prepare_enable(init_clk);
496 * ti_clk_add_alias - add a clock alias for a TI clock
497 * @dev: device alias for this clock
498 * @clk: clock handle to create alias for
499 * @con: connection ID for this clock
501 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
502 * and assigns the data to it. Returns 0 if successful, negative error
505 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
507 struct clk_lookup *cl;
515 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
520 cl->dev_id = dev_name(dev);
530 * ti_clk_register - register a TI clock to the common clock framework
531 * @dev: device for this clock
532 * @hw: hardware clock handle
533 * @con: connection ID for this clock
535 * Registers a TI clock to the common clock framework, and adds a clock
536 * alias for it. Returns a handle to the registered clock if successful,
537 * ERR_PTR value in failure.
539 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
545 clk = clk_register(dev, hw);
549 ret = ti_clk_add_alias(dev, clk, con);