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>
31 #define pr_fmt(fmt) "%s: " fmt, __func__
33 struct ti_clk_ll_ops *ti_clk_ll_ops;
34 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
36 static struct ti_clk_features ti_clk_features;
39 struct regmap *regmap;
43 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
45 static void clk_memmap_writel(u32 val, void __iomem *reg)
47 struct clk_omap_reg *r = (struct clk_omap_reg *)®
48 struct clk_iomap *io = clk_memmaps[r->index];
51 regmap_write(io->regmap, r->offset, val);
53 writel_relaxed(val, io->mem + r->offset);
56 static u32 clk_memmap_readl(void __iomem *reg)
59 struct clk_omap_reg *r = (struct clk_omap_reg *)®
60 struct clk_iomap *io = clk_memmaps[r->index];
63 regmap_read(io->regmap, r->offset, &val);
65 val = readl_relaxed(io->mem + r->offset);
71 * ti_clk_setup_ll_ops - setup low level clock operations
72 * @ops: low level clock ops descriptor
74 * Sets up low level clock operations for TI clock driver. This is used
75 * to provide various callbacks for the clock driver towards platform
76 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
79 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
82 pr_err("Attempt to register ll_ops multiple times.\n");
87 ops->clk_readl = clk_memmap_readl;
88 ops->clk_writel = clk_memmap_writel;
94 * ti_dt_clocks_register - register DT alias clocks during boot
95 * @oclks: list of clocks to register
97 * Register alias or non-standard DT clock entries during boot. By
98 * default, DT clocks are found based on their node name. If any
99 * additional con-id / dev-id -> clock mapping is required, use this
100 * function to list these.
102 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
105 struct device_node *node;
107 struct of_phandle_args clkspec;
109 for (c = oclks; c->node_name != NULL; c++) {
110 node = of_find_node_by_name(NULL, c->node_name);
112 clk = of_clk_get_from_provider(&clkspec);
118 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;
305 switch (setup->type) {
309 clk = clk_register_fixed_rate(NULL, setup->name, NULL,
310 CLK_IS_ROOT, fixed->frequency);
313 clk = ti_clk_register_mux(setup);
316 clk = ti_clk_register_divider(setup);
318 case TI_CLK_COMPOSITE:
319 clk = ti_clk_register_composite(setup);
321 case TI_CLK_FIXED_FACTOR:
322 fixed_factor = setup->data;
324 clk = clk_register_fixed_factor(NULL, setup->name,
325 fixed_factor->parent,
326 0, fixed_factor->mult,
330 clk = ti_clk_register_gate(setup);
333 clk = ti_clk_register_dpll(setup);
336 pr_err("bad type for %s!\n", setup->name);
337 clk = ERR_PTR(-EINVAL);
342 if (setup->clkdm_name) {
343 clk_hw = __clk_get_hw(clk);
344 if (clk_hw_get_flags(clk_hw) & CLK_IS_BASIC) {
345 pr_warn("can't setup clkdm for basic clk %s\n",
348 to_clk_hw_omap(clk_hw)->clkdm_name =
350 omap2_init_clk_clkdm(clk_hw);
358 int __init ti_clk_register_legacy_clks(struct ti_clk_alias *clks)
362 struct ti_clk_alias *retry_clk;
363 struct ti_clk_alias *tmp;
366 clk = ti_clk_register_clk(clks->clk);
368 if (PTR_ERR(clk) == -EAGAIN) {
369 list_add(&clks->link, &retry_list);
371 pr_err("register for %s failed: %ld\n",
372 clks->clk->name, PTR_ERR(clk));
377 clkdev_add(&clks->lk);
384 while (!list_empty(&retry_list) && retry) {
386 list_for_each_entry_safe(retry_clk, tmp, &retry_list, link) {
387 pr_debug("retry-init: %s\n", retry_clk->clk->name);
388 clk = ti_clk_register_clk(retry_clk->clk);
390 if (PTR_ERR(clk) == -EAGAIN) {
393 pr_err("register for %s failed: %ld\n",
394 retry_clk->clk->name,
400 retry_clk->lk.clk = clk;
401 clkdev_add(&retry_clk->lk);
402 list_del(&retry_clk->link);
412 * ti_clk_setup_features - setup clock features flags
413 * @features: features definition to use
415 * Initializes the clock driver features flags based on platform
416 * provided data. No return value.
418 void __init ti_clk_setup_features(struct ti_clk_features *features)
420 memcpy(&ti_clk_features, features, sizeof(*features));
424 * ti_clk_get_features - get clock driver features flags
426 * Get TI clock driver features description. Returns a pointer
427 * to the current feature setup.
429 const struct ti_clk_features *ti_clk_get_features(void)
431 return &ti_clk_features;
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
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?
444 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
446 struct clk *init_clk;
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",
454 clk_prepare_enable(init_clk);