4 * Copyright (C) 2013 Texas Instruments, Inc.
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>
24 #include <linux/of_address.h>
25 #include <linux/list.h>
26 #include <linux/regmap.h>
27 #include <linux/memblock.h>
28 #include <linux/device.h>
33 #define pr_fmt(fmt) "%s: " fmt, __func__
35 static LIST_HEAD(clk_hw_omap_clocks);
36 struct ti_clk_ll_ops *ti_clk_ll_ops;
37 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
39 struct ti_clk_features ti_clk_features;
42 struct regmap *regmap;
46 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
48 static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
50 struct clk_iomap *io = clk_memmaps[reg->index];
53 writel_relaxed(val, reg->ptr);
55 regmap_write(io->regmap, reg->offset, val);
57 writel_relaxed(val, io->mem + reg->offset);
60 static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
64 v = readl_relaxed(ptr);
67 writel_relaxed(v, ptr);
70 static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
72 struct clk_iomap *io = clk_memmaps[reg->index];
75 _clk_rmw(val, mask, reg->ptr);
76 } else if (io->regmap) {
77 regmap_update_bits(io->regmap, reg->offset, mask, val);
79 _clk_rmw(val, mask, io->mem + reg->offset);
83 static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
86 struct clk_iomap *io = clk_memmaps[reg->index];
89 val = readl_relaxed(reg->ptr);
91 regmap_read(io->regmap, reg->offset, &val);
93 val = readl_relaxed(io->mem + reg->offset);
99 * ti_clk_setup_ll_ops - setup low level clock operations
100 * @ops: low level clock ops descriptor
102 * Sets up low level clock operations for TI clock driver. This is used
103 * to provide various callbacks for the clock driver towards platform
104 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
105 * registered already.
107 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
110 pr_err("Attempt to register ll_ops multiple times.\n");
115 ops->clk_readl = clk_memmap_readl;
116 ops->clk_writel = clk_memmap_writel;
117 ops->clk_rmw = clk_memmap_rmw;
123 * ti_dt_clocks_register - register DT alias clocks during boot
124 * @oclks: list of clocks to register
126 * Register alias or non-standard DT clock entries during boot. By
127 * default, DT clocks are found based on their node name. If any
128 * additional con-id / dev-id -> clock mapping is required, use this
129 * function to list these.
131 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
134 struct device_node *node, *parent;
136 struct of_phandle_args clkspec;
143 static bool clkctrl_nodes_missing;
144 static bool has_clkctrl_data;
145 static bool compat_mode;
147 compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
149 for (c = oclks; c->node_name != NULL; c++) {
150 strcpy(buf, c->node_name);
152 for (i = 0; i < 2; i++)
158 pr_warn("Bad number of tags on %s\n",
162 tags[num_args++] = ptr + 1;
168 if (num_args && clkctrl_nodes_missing)
171 node = of_find_node_by_name(NULL, buf);
172 if (num_args && compat_mode) {
174 node = of_get_child_by_name(parent, "clock");
176 node = of_get_child_by_name(parent, "clk");
181 clkspec.args_count = num_args;
182 for (i = 0; i < num_args; i++) {
183 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
185 pr_warn("Bad tag in %s at %d: %s\n",
186 c->node_name, i, tags[i]);
191 clk = of_clk_get_from_provider(&clkspec);
197 if (num_args && !has_clkctrl_data) {
198 struct device_node *np;
200 np = of_find_compatible_node(NULL, NULL,
203 has_clkctrl_data = true;
206 clkctrl_nodes_missing = true;
208 pr_warn("missing clkctrl nodes, please update your dts.\n");
213 pr_warn("failed to lookup clock node %s, ret=%ld\n",
214 c->node_name, PTR_ERR(clk));
219 struct clk_init_item {
220 struct device_node *node;
222 ti_of_clk_init_cb_t func;
223 struct list_head link;
226 static LIST_HEAD(retry_list);
229 * ti_clk_retry_init - retries a failed clock init at later phase
230 * @node: device not for the clock
231 * @user: user data pointer
232 * @func: init function to be called for the clock
234 * Adds a failed clock init to the retry list. The retry list is parsed
235 * once all the other clocks have been initialized.
237 int __init ti_clk_retry_init(struct device_node *node, void *user,
238 ti_of_clk_init_cb_t func)
240 struct clk_init_item *retry;
242 pr_debug("%pOFn: adding to retry list...\n", node);
243 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
250 list_add(&retry->link, &retry_list);
256 * ti_clk_get_reg_addr - get register address for a clock register
257 * @node: device node for the clock
258 * @index: register index from the clock node
259 * @reg: pointer to target register struct
261 * Builds clock register address from device tree information, and returns
262 * the data via the provided output pointer @reg. Returns 0 on success,
263 * negative error value on failure.
265 int ti_clk_get_reg_addr(struct device_node *node, int index,
266 struct clk_omap_reg *reg)
271 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
272 if (clocks_node_ptr[i] == node->parent)
276 if (i == CLK_MAX_MEMMAPS) {
277 pr_err("clk-provider not found for %pOFn!\n", node);
283 if (of_property_read_u32_index(node, "reg", index, &val)) {
284 pr_err("%pOFn must have reg[%d]!\n", node, index);
294 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
303 ti_clk_ll_ops->clk_rmw(latch, latch, reg);
304 ti_clk_ll_ops->clk_rmw(0, latch, reg);
305 ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
309 * omap2_clk_provider_init - init master clock provider
310 * @parent: master node
311 * @index: internal index for clk_reg_ops
312 * @syscon: syscon regmap pointer for accessing clock registers
313 * @mem: iomem pointer for the clock provider memory area, only used if
314 * syscon is not provided
316 * Initializes a master clock IP block. This basically sets up the
317 * mapping from clocks node to the memory map index. All the clocks
318 * are then initialized through the common of_clk_init call, and the
319 * clocks will access their memory maps based on the node layout.
320 * Returns 0 in success.
322 int __init omap2_clk_provider_init(struct device_node *parent, int index,
323 struct regmap *syscon, void __iomem *mem)
325 struct device_node *clocks;
326 struct clk_iomap *io;
328 /* get clocks for this parent */
329 clocks = of_get_child_by_name(parent, "clocks");
331 pr_err("%pOFn missing 'clocks' child node.\n", parent);
335 /* add clocks node info */
336 clocks_node_ptr[index] = clocks;
338 io = kzalloc(sizeof(*io), GFP_KERNEL);
345 clk_memmaps[index] = io;
351 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
352 * @index: index for the clock provider
353 * @mem: iomem pointer for the clock provider memory area
355 * Initializes a legacy clock provider memory mapping.
357 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
359 struct clk_iomap *io;
361 io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
363 panic("%s: Failed to allocate %zu bytes\n", __func__,
368 clk_memmaps[index] = io;
372 * ti_dt_clk_init_retry_clks - init clocks from the retry list
374 * Initializes any clocks that have failed to initialize before,
375 * reasons being missing parent node(s) during earlier init. This
376 * typically happens only for DPLLs which need to have both of their
377 * parent clocks ready during init.
379 void ti_dt_clk_init_retry_clks(void)
381 struct clk_init_item *retry;
382 struct clk_init_item *tmp;
385 while (!list_empty(&retry_list) && retries) {
386 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
387 pr_debug("retry-init: %pOFn\n", retry->node);
388 retry->func(retry->user, retry->node);
389 list_del(&retry->link);
396 static const struct of_device_id simple_clk_match_table[] __initconst = {
397 { .compatible = "fixed-clock" },
398 { .compatible = "fixed-factor-clock" },
403 * ti_clk_add_aliases - setup clock aliases
405 * Sets up any missing clock aliases. No return value.
407 void __init ti_clk_add_aliases(void)
409 struct device_node *np;
412 for_each_matching_node(np, simple_clk_match_table) {
413 struct of_phandle_args clkspec;
416 clk = of_clk_get_from_provider(&clkspec);
418 ti_clk_add_alias(NULL, clk, np->name);
423 * ti_clk_setup_features - setup clock features flags
424 * @features: features definition to use
426 * Initializes the clock driver features flags based on platform
427 * provided data. No return value.
429 void __init ti_clk_setup_features(struct ti_clk_features *features)
431 memcpy(&ti_clk_features, features, sizeof(*features));
435 * ti_clk_get_features - get clock driver features flags
437 * Get TI clock driver features description. Returns a pointer
438 * to the current feature setup.
440 const struct ti_clk_features *ti_clk_get_features(void)
442 return &ti_clk_features;
446 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
447 * @clk_names: ptr to an array of strings of clock names to enable
448 * @num_clocks: number of clock names in @clk_names
450 * Prepare and enable a list of clocks, named by @clk_names. No
451 * return value. XXX Deprecated; only needed until these clocks are
452 * properly claimed and enabled by the drivers or core code that uses
453 * them. XXX What code disables & calls clk_put on these clocks?
455 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
457 struct clk *init_clk;
460 for (i = 0; i < num_clocks; i++) {
461 init_clk = clk_get(NULL, clk_names[i]);
462 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
465 clk_prepare_enable(init_clk);
470 * ti_clk_add_alias - add a clock alias for a TI clock
471 * @dev: device alias for this clock
472 * @clk: clock handle to create alias for
473 * @con: connection ID for this clock
475 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
476 * and assigns the data to it. Returns 0 if successful, negative error
479 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
481 struct clk_lookup *cl;
489 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
494 cl->dev_id = dev_name(dev);
504 * ti_clk_register - register a TI clock to the common clock framework
505 * @dev: device for this clock
506 * @hw: hardware clock handle
507 * @con: connection ID for this clock
509 * Registers a TI clock to the common clock framework, and adds a clock
510 * alias for it. Returns a handle to the registered clock if successful,
511 * ERR_PTR value in failure.
513 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
519 clk = clk_register(dev, hw);
523 ret = ti_clk_add_alias(dev, clk, con);
533 * ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
534 * @dev: device for this clock
535 * @hw: hardware clock handle
536 * @con: connection ID for this clock
538 * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
539 * for it, and adds the list to the available clk_hw_omap type clocks.
540 * Returns a handle to the registered clock if successful, ERR_PTR value
543 struct clk *ti_clk_register_omap_hw(struct device *dev, struct clk_hw *hw,
547 struct clk_hw_omap *oclk;
549 clk = ti_clk_register(dev, hw, con);
553 oclk = to_clk_hw_omap(hw);
555 list_add(&oclk->node, &clk_hw_omap_clocks);
561 * omap2_clk_for_each - call function for each registered clk_hw_omap
562 * @fn: pointer to a callback function
564 * Call @fn for each registered clk_hw_omap, passing @hw to each
565 * function. @fn must return 0 for success or any other value for
566 * failure. If @fn returns non-zero, the iteration across clocks
567 * will stop and the non-zero return value will be passed to the
568 * caller of omap2_clk_for_each().
570 int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
573 struct clk_hw_omap *hw;
575 list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
585 * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
586 * @hw: clk_hw to check if it is an omap clock or not
588 * Checks if the provided clk_hw is OMAP clock or not. Returns true if
589 * it is, false otherwise.
591 bool omap2_clk_is_hw_omap(struct clk_hw *hw)
593 struct clk_hw_omap *oclk;
595 list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {