1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2013 Texas Instruments, Inc.
7 * Tero Kristo <t-kristo@ti.com>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/ti.h>
16 #include <linux/of_address.h>
17 #include <linux/list.h>
18 #include <linux/regmap.h>
19 #include <linux/memblock.h>
20 #include <linux/device.h>
25 #define pr_fmt(fmt) "%s: " fmt, __func__
27 static LIST_HEAD(clk_hw_omap_clocks);
28 struct ti_clk_ll_ops *ti_clk_ll_ops;
29 static struct device_node *clocks_node_ptr[CLK_MAX_MEMMAPS];
31 struct ti_clk_features ti_clk_features;
34 struct regmap *regmap;
38 static struct clk_iomap *clk_memmaps[CLK_MAX_MEMMAPS];
40 static void clk_memmap_writel(u32 val, const struct clk_omap_reg *reg)
42 struct clk_iomap *io = clk_memmaps[reg->index];
45 writel_relaxed(val, reg->ptr);
47 regmap_write(io->regmap, reg->offset, val);
49 writel_relaxed(val, io->mem + reg->offset);
52 static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
56 v = readl_relaxed(ptr);
59 writel_relaxed(v, ptr);
62 static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
64 struct clk_iomap *io = clk_memmaps[reg->index];
67 _clk_rmw(val, mask, reg->ptr);
68 } else if (io->regmap) {
69 regmap_update_bits(io->regmap, reg->offset, mask, val);
71 _clk_rmw(val, mask, io->mem + reg->offset);
75 static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
78 struct clk_iomap *io = clk_memmaps[reg->index];
81 val = readl_relaxed(reg->ptr);
83 regmap_read(io->regmap, reg->offset, &val);
85 val = readl_relaxed(io->mem + reg->offset);
91 * ti_clk_setup_ll_ops - setup low level clock operations
92 * @ops: low level clock ops descriptor
94 * Sets up low level clock operations for TI clock driver. This is used
95 * to provide various callbacks for the clock driver towards platform
96 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
99 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
102 pr_err("Attempt to register ll_ops multiple times.\n");
107 ops->clk_readl = clk_memmap_readl;
108 ops->clk_writel = clk_memmap_writel;
109 ops->clk_rmw = clk_memmap_rmw;
115 * Eventually we could standardize to using '_' for clk-*.c files to follow the
116 * TRM naming and leave out the tmp name here.
118 static struct device_node *ti_find_clock_provider(struct device_node *from,
121 struct device_node *np;
126 tmp = kstrdup(name, GFP_KERNEL);
129 strreplace(tmp, '-', '_');
131 /* Node named "clock" with "clock-output-names" */
132 for_each_of_allnodes_from(from, np) {
133 if (of_property_read_string_index(np, "clock-output-names",
137 if (!strncmp(n, tmp, strlen(tmp))) {
149 /* Fall back to using old node name base provider name */
150 return of_find_node_by_name(from, name);
154 * ti_dt_clocks_register - register DT alias clocks during boot
155 * @oclks: list of clocks to register
157 * Register alias or non-standard DT clock entries during boot. By
158 * default, DT clocks are found based on their clock-output-names
159 * property, or the clock node name for legacy cases. If any
160 * additional con-id / dev-id -> clock mapping is required, use this
161 * function to list these.
163 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
166 struct device_node *node, *parent, *child;
168 struct of_phandle_args clkspec;
175 static bool clkctrl_nodes_missing;
176 static bool has_clkctrl_data;
177 static bool compat_mode;
179 compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
181 for (c = oclks; c->node_name != NULL; c++) {
182 strcpy(buf, c->node_name);
184 for (i = 0; i < 2; i++)
190 pr_warn("Bad number of tags on %s\n",
194 tags[num_args++] = ptr + 1;
200 if (num_args && clkctrl_nodes_missing)
203 node = ti_find_clock_provider(NULL, buf);
204 if (num_args && compat_mode) {
206 child = of_get_child_by_name(parent, "clock");
208 child = of_get_child_by_name(parent, "clk");
216 clkspec.args_count = num_args;
217 for (i = 0; i < num_args; i++) {
218 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
220 pr_warn("Bad tag in %s at %d: %s\n",
221 c->node_name, i, tags[i]);
226 clk = of_clk_get_from_provider(&clkspec);
232 if (num_args && !has_clkctrl_data) {
233 struct device_node *np;
235 np = of_find_compatible_node(NULL, NULL,
238 has_clkctrl_data = true;
241 clkctrl_nodes_missing = true;
243 pr_warn("missing clkctrl nodes, please update your dts.\n");
248 pr_warn("failed to lookup clock node %s, ret=%ld\n",
249 c->node_name, PTR_ERR(clk));
254 struct clk_init_item {
255 struct device_node *node;
257 ti_of_clk_init_cb_t func;
258 struct list_head link;
261 static LIST_HEAD(retry_list);
264 * ti_clk_retry_init - retries a failed clock init at later phase
265 * @node: device not for the clock
266 * @user: user data pointer
267 * @func: init function to be called for the clock
269 * Adds a failed clock init to the retry list. The retry list is parsed
270 * once all the other clocks have been initialized.
272 int __init ti_clk_retry_init(struct device_node *node, void *user,
273 ti_of_clk_init_cb_t func)
275 struct clk_init_item *retry;
277 pr_debug("%pOFn: adding to retry list...\n", node);
278 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
285 list_add(&retry->link, &retry_list);
291 * ti_clk_get_reg_addr - get register address for a clock register
292 * @node: device node for the clock
293 * @index: register index from the clock node
294 * @reg: pointer to target register struct
296 * Builds clock register address from device tree information, and returns
297 * the data via the provided output pointer @reg. Returns 0 on success,
298 * negative error value on failure.
300 int ti_clk_get_reg_addr(struct device_node *node, int index,
301 struct clk_omap_reg *reg)
306 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
307 if (clocks_node_ptr[i] == node->parent)
309 if (clocks_node_ptr[i] == node->parent->parent)
313 if (i == CLK_MAX_MEMMAPS) {
314 pr_err("clk-provider not found for %pOFn!\n", node);
320 if (of_property_read_u32_index(node, "reg", index, &val)) {
321 if (of_property_read_u32_index(node->parent, "reg",
323 pr_err("%pOFn or parent must have reg[%d]!\n",
335 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
344 ti_clk_ll_ops->clk_rmw(latch, latch, reg);
345 ti_clk_ll_ops->clk_rmw(0, latch, reg);
346 ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
350 * omap2_clk_provider_init - init master clock provider
351 * @parent: master node
352 * @index: internal index for clk_reg_ops
353 * @syscon: syscon regmap pointer for accessing clock registers
354 * @mem: iomem pointer for the clock provider memory area, only used if
355 * syscon is not provided
357 * Initializes a master clock IP block. This basically sets up the
358 * mapping from clocks node to the memory map index. All the clocks
359 * are then initialized through the common of_clk_init call, and the
360 * clocks will access their memory maps based on the node layout.
361 * Returns 0 in success.
363 int __init omap2_clk_provider_init(struct device_node *parent, int index,
364 struct regmap *syscon, void __iomem *mem)
366 struct device_node *clocks;
367 struct clk_iomap *io;
369 /* get clocks for this parent */
370 clocks = of_get_child_by_name(parent, "clocks");
372 pr_err("%pOFn missing 'clocks' child node.\n", parent);
376 /* add clocks node info */
377 clocks_node_ptr[index] = clocks;
379 io = kzalloc(sizeof(*io), GFP_KERNEL);
386 clk_memmaps[index] = io;
392 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
393 * @index: index for the clock provider
394 * @mem: iomem pointer for the clock provider memory area
396 * Initializes a legacy clock provider memory mapping.
398 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
400 struct clk_iomap *io;
402 io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
404 panic("%s: Failed to allocate %zu bytes\n", __func__,
409 clk_memmaps[index] = io;
413 * ti_dt_clk_init_retry_clks - init clocks from the retry list
415 * Initializes any clocks that have failed to initialize before,
416 * reasons being missing parent node(s) during earlier init. This
417 * typically happens only for DPLLs which need to have both of their
418 * parent clocks ready during init.
420 void ti_dt_clk_init_retry_clks(void)
422 struct clk_init_item *retry;
423 struct clk_init_item *tmp;
426 while (!list_empty(&retry_list) && retries) {
427 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
428 pr_debug("retry-init: %pOFn\n", retry->node);
429 retry->func(retry->user, retry->node);
430 list_del(&retry->link);
437 static const struct of_device_id simple_clk_match_table[] __initconst = {
438 { .compatible = "fixed-clock" },
439 { .compatible = "fixed-factor-clock" },
444 * ti_dt_clk_name - init clock name from first output name or node name
447 * Use the first clock-output-name for the clock name if found. Fall back
448 * to legacy naming based on node name.
450 const char *ti_dt_clk_name(struct device_node *np)
454 if (!of_property_read_string_index(np, "clock-output-names", 0,
462 * ti_clk_add_aliases - setup clock aliases
464 * Sets up any missing clock aliases. No return value.
466 void __init ti_clk_add_aliases(void)
468 struct device_node *np;
471 for_each_matching_node(np, simple_clk_match_table) {
472 struct of_phandle_args clkspec;
475 clk = of_clk_get_from_provider(&clkspec);
477 ti_clk_add_alias(NULL, clk, ti_dt_clk_name(np));
482 * ti_clk_setup_features - setup clock features flags
483 * @features: features definition to use
485 * Initializes the clock driver features flags based on platform
486 * provided data. No return value.
488 void __init ti_clk_setup_features(struct ti_clk_features *features)
490 memcpy(&ti_clk_features, features, sizeof(*features));
494 * ti_clk_get_features - get clock driver features flags
496 * Get TI clock driver features description. Returns a pointer
497 * to the current feature setup.
499 const struct ti_clk_features *ti_clk_get_features(void)
501 return &ti_clk_features;
505 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
506 * @clk_names: ptr to an array of strings of clock names to enable
507 * @num_clocks: number of clock names in @clk_names
509 * Prepare and enable a list of clocks, named by @clk_names. No
510 * return value. XXX Deprecated; only needed until these clocks are
511 * properly claimed and enabled by the drivers or core code that uses
512 * them. XXX What code disables & calls clk_put on these clocks?
514 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
516 struct clk *init_clk;
519 for (i = 0; i < num_clocks; i++) {
520 init_clk = clk_get(NULL, clk_names[i]);
521 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
524 clk_prepare_enable(init_clk);
529 * ti_clk_add_alias - add a clock alias for a TI clock
530 * @dev: device alias for this clock
531 * @clk: clock handle to create alias for
532 * @con: connection ID for this clock
534 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
535 * and assigns the data to it. Returns 0 if successful, negative error
538 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
540 struct clk_lookup *cl;
548 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
553 cl->dev_id = dev_name(dev);
563 * ti_clk_register - register a TI clock to the common clock framework
564 * @dev: device for this clock
565 * @hw: hardware clock handle
566 * @con: connection ID for this clock
568 * Registers a TI clock to the common clock framework, and adds a clock
569 * alias for it. Returns a handle to the registered clock if successful,
570 * ERR_PTR value in failure.
572 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
578 clk = clk_register(dev, hw);
582 ret = ti_clk_add_alias(dev, clk, con);
592 * ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
593 * @dev: device for this clock
594 * @hw: hardware clock handle
595 * @con: connection ID for this clock
597 * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
598 * for it, and adds the list to the available clk_hw_omap type clocks.
599 * Returns a handle to the registered clock if successful, ERR_PTR value
602 struct clk *ti_clk_register_omap_hw(struct device *dev, struct clk_hw *hw,
606 struct clk_hw_omap *oclk;
608 clk = ti_clk_register(dev, hw, con);
612 oclk = to_clk_hw_omap(hw);
614 list_add(&oclk->node, &clk_hw_omap_clocks);
620 * omap2_clk_for_each - call function for each registered clk_hw_omap
621 * @fn: pointer to a callback function
623 * Call @fn for each registered clk_hw_omap, passing @hw to each
624 * function. @fn must return 0 for success or any other value for
625 * failure. If @fn returns non-zero, the iteration across clocks
626 * will stop and the non-zero return value will be passed to the
627 * caller of omap2_clk_for_each().
629 int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
632 struct clk_hw_omap *hw;
634 list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
644 * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
645 * @hw: clk_hw to check if it is an omap clock or not
647 * Checks if the provided clk_hw is OMAP clock or not. Returns true if
648 * it is, false otherwise.
650 bool omap2_clk_is_hw_omap(struct clk_hw *hw)
652 struct clk_hw_omap *oclk;
654 list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {