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))) {
148 /* Fall back to using old node name base provider name */
149 return of_find_node_by_name(from, name);
153 * ti_dt_clocks_register - register DT alias clocks during boot
154 * @oclks: list of clocks to register
156 * Register alias or non-standard DT clock entries during boot. By
157 * default, DT clocks are found based on their clock-output-names
158 * property, or the clock node name for legacy cases. If any
159 * additional con-id / dev-id -> clock mapping is required, use this
160 * function to list these.
162 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
165 struct device_node *node, *parent, *child;
167 struct of_phandle_args clkspec;
174 static bool clkctrl_nodes_missing;
175 static bool has_clkctrl_data;
176 static bool compat_mode;
178 compat_mode = ti_clk_get_features()->flags & TI_CLK_CLKCTRL_COMPAT;
180 for (c = oclks; c->node_name != NULL; c++) {
181 strcpy(buf, c->node_name);
183 for (i = 0; i < 2; i++)
189 pr_warn("Bad number of tags on %s\n",
193 tags[num_args++] = ptr + 1;
199 if (num_args && clkctrl_nodes_missing)
202 node = ti_find_clock_provider(NULL, buf);
203 if (num_args && compat_mode) {
205 child = of_get_child_by_name(parent, "clock");
207 child = of_get_child_by_name(parent, "clk");
215 clkspec.args_count = num_args;
216 for (i = 0; i < num_args; i++) {
217 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
219 pr_warn("Bad tag in %s at %d: %s\n",
220 c->node_name, i, tags[i]);
225 clk = of_clk_get_from_provider(&clkspec);
231 if (num_args && !has_clkctrl_data) {
232 struct device_node *np;
234 np = of_find_compatible_node(NULL, NULL,
237 has_clkctrl_data = true;
240 clkctrl_nodes_missing = true;
242 pr_warn("missing clkctrl nodes, please update your dts.\n");
247 pr_warn("failed to lookup clock node %s, ret=%ld\n",
248 c->node_name, PTR_ERR(clk));
253 struct clk_init_item {
254 struct device_node *node;
256 ti_of_clk_init_cb_t func;
257 struct list_head link;
260 static LIST_HEAD(retry_list);
263 * ti_clk_retry_init - retries a failed clock init at later phase
264 * @node: device not for the clock
265 * @user: user data pointer
266 * @func: init function to be called for the clock
268 * Adds a failed clock init to the retry list. The retry list is parsed
269 * once all the other clocks have been initialized.
271 int __init ti_clk_retry_init(struct device_node *node, void *user,
272 ti_of_clk_init_cb_t func)
274 struct clk_init_item *retry;
276 pr_debug("%pOFn: adding to retry list...\n", node);
277 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
284 list_add(&retry->link, &retry_list);
290 * ti_clk_get_reg_addr - get register address for a clock register
291 * @node: device node for the clock
292 * @index: register index from the clock node
293 * @reg: pointer to target register struct
295 * Builds clock register address from device tree information, and returns
296 * the data via the provided output pointer @reg. Returns 0 on success,
297 * negative error value on failure.
299 int ti_clk_get_reg_addr(struct device_node *node, int index,
300 struct clk_omap_reg *reg)
305 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
306 if (clocks_node_ptr[i] == node->parent)
308 if (clocks_node_ptr[i] == node->parent->parent)
312 if (i == CLK_MAX_MEMMAPS) {
313 pr_err("clk-provider not found for %pOFn!\n", node);
319 if (of_property_read_u32_index(node, "reg", index, &val)) {
320 if (of_property_read_u32_index(node->parent, "reg",
322 pr_err("%pOFn or parent must have reg[%d]!\n",
334 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
343 ti_clk_ll_ops->clk_rmw(latch, latch, reg);
344 ti_clk_ll_ops->clk_rmw(0, latch, reg);
345 ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
349 * omap2_clk_provider_init - init master clock provider
350 * @parent: master node
351 * @index: internal index for clk_reg_ops
352 * @syscon: syscon regmap pointer for accessing clock registers
353 * @mem: iomem pointer for the clock provider memory area, only used if
354 * syscon is not provided
356 * Initializes a master clock IP block. This basically sets up the
357 * mapping from clocks node to the memory map index. All the clocks
358 * are then initialized through the common of_clk_init call, and the
359 * clocks will access their memory maps based on the node layout.
360 * Returns 0 in success.
362 int __init omap2_clk_provider_init(struct device_node *parent, int index,
363 struct regmap *syscon, void __iomem *mem)
365 struct device_node *clocks;
366 struct clk_iomap *io;
368 /* get clocks for this parent */
369 clocks = of_get_child_by_name(parent, "clocks");
371 pr_err("%pOFn missing 'clocks' child node.\n", parent);
375 /* add clocks node info */
376 clocks_node_ptr[index] = clocks;
378 io = kzalloc(sizeof(*io), GFP_KERNEL);
385 clk_memmaps[index] = io;
391 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
392 * @index: index for the clock provider
393 * @mem: iomem pointer for the clock provider memory area
395 * Initializes a legacy clock provider memory mapping.
397 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
399 struct clk_iomap *io;
401 io = memblock_alloc(sizeof(*io), SMP_CACHE_BYTES);
403 panic("%s: Failed to allocate %zu bytes\n", __func__,
408 clk_memmaps[index] = io;
412 * ti_dt_clk_init_retry_clks - init clocks from the retry list
414 * Initializes any clocks that have failed to initialize before,
415 * reasons being missing parent node(s) during earlier init. This
416 * typically happens only for DPLLs which need to have both of their
417 * parent clocks ready during init.
419 void ti_dt_clk_init_retry_clks(void)
421 struct clk_init_item *retry;
422 struct clk_init_item *tmp;
425 while (!list_empty(&retry_list) && retries) {
426 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
427 pr_debug("retry-init: %pOFn\n", retry->node);
428 retry->func(retry->user, retry->node);
429 list_del(&retry->link);
436 static const struct of_device_id simple_clk_match_table[] __initconst = {
437 { .compatible = "fixed-clock" },
438 { .compatible = "fixed-factor-clock" },
443 * ti_dt_clk_name - init clock name from first output name or node name
446 * Use the first clock-output-name for the clock name if found. Fall back
447 * to legacy naming based on node name.
449 const char *ti_dt_clk_name(struct device_node *np)
453 if (!of_property_read_string_index(np, "clock-output-names", 0,
461 * ti_clk_add_aliases - setup clock aliases
463 * Sets up any missing clock aliases. No return value.
465 void __init ti_clk_add_aliases(void)
467 struct device_node *np;
470 for_each_matching_node(np, simple_clk_match_table) {
471 struct of_phandle_args clkspec;
474 clk = of_clk_get_from_provider(&clkspec);
476 ti_clk_add_alias(NULL, clk, ti_dt_clk_name(np));
481 * ti_clk_setup_features - setup clock features flags
482 * @features: features definition to use
484 * Initializes the clock driver features flags based on platform
485 * provided data. No return value.
487 void __init ti_clk_setup_features(struct ti_clk_features *features)
489 memcpy(&ti_clk_features, features, sizeof(*features));
493 * ti_clk_get_features - get clock driver features flags
495 * Get TI clock driver features description. Returns a pointer
496 * to the current feature setup.
498 const struct ti_clk_features *ti_clk_get_features(void)
500 return &ti_clk_features;
504 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
505 * @clk_names: ptr to an array of strings of clock names to enable
506 * @num_clocks: number of clock names in @clk_names
508 * Prepare and enable a list of clocks, named by @clk_names. No
509 * return value. XXX Deprecated; only needed until these clocks are
510 * properly claimed and enabled by the drivers or core code that uses
511 * them. XXX What code disables & calls clk_put on these clocks?
513 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
515 struct clk *init_clk;
518 for (i = 0; i < num_clocks; i++) {
519 init_clk = clk_get(NULL, clk_names[i]);
520 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
523 clk_prepare_enable(init_clk);
528 * ti_clk_add_alias - add a clock alias for a TI clock
529 * @dev: device alias for this clock
530 * @clk: clock handle to create alias for
531 * @con: connection ID for this clock
533 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
534 * and assigns the data to it. Returns 0 if successful, negative error
537 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
539 struct clk_lookup *cl;
547 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
552 cl->dev_id = dev_name(dev);
562 * ti_clk_register - register a TI clock to the common clock framework
563 * @dev: device for this clock
564 * @hw: hardware clock handle
565 * @con: connection ID for this clock
567 * Registers a TI clock to the common clock framework, and adds a clock
568 * alias for it. Returns a handle to the registered clock if successful,
569 * ERR_PTR value in failure.
571 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
577 clk = clk_register(dev, hw);
581 ret = ti_clk_add_alias(dev, clk, con);
591 * ti_clk_register_omap_hw - register a clk_hw_omap to the clock framework
592 * @dev: device for this clock
593 * @hw: hardware clock handle
594 * @con: connection ID for this clock
596 * Registers a clk_hw_omap clock to the clock framewor, adds a clock alias
597 * for it, and adds the list to the available clk_hw_omap type clocks.
598 * Returns a handle to the registered clock if successful, ERR_PTR value
601 struct clk *ti_clk_register_omap_hw(struct device *dev, struct clk_hw *hw,
605 struct clk_hw_omap *oclk;
607 clk = ti_clk_register(dev, hw, con);
611 oclk = to_clk_hw_omap(hw);
613 list_add(&oclk->node, &clk_hw_omap_clocks);
619 * omap2_clk_for_each - call function for each registered clk_hw_omap
620 * @fn: pointer to a callback function
622 * Call @fn for each registered clk_hw_omap, passing @hw to each
623 * function. @fn must return 0 for success or any other value for
624 * failure. If @fn returns non-zero, the iteration across clocks
625 * will stop and the non-zero return value will be passed to the
626 * caller of omap2_clk_for_each().
628 int omap2_clk_for_each(int (*fn)(struct clk_hw_omap *hw))
631 struct clk_hw_omap *hw;
633 list_for_each_entry(hw, &clk_hw_omap_clocks, node) {
643 * omap2_clk_is_hw_omap - check if the provided clk_hw is OMAP clock
644 * @hw: clk_hw to check if it is an omap clock or not
646 * Checks if the provided clk_hw is OMAP clock or not. Returns true if
647 * it is, false otherwise.
649 bool omap2_clk_is_hw_omap(struct clk_hw *hw)
651 struct clk_hw_omap *oclk;
653 list_for_each_entry(oclk, &clk_hw_omap_clocks, node) {