Linux 4.19.133
[linux/fpc-iii.git] / drivers / clk / ti / clk.c
blob0cc87c6ae91c96dc3ae57c92da9989430600825f
1 /*
2 * TI clock support
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>
22 #include <linux/of.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>
29 #include "clock.h"
31 #undef pr_fmt
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;
39 struct clk_iomap {
40 struct regmap *regmap;
41 void __iomem *mem;
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];
50 if (reg->ptr)
51 writel_relaxed(val, reg->ptr);
52 else if (io->regmap)
53 regmap_write(io->regmap, reg->offset, val);
54 else
55 writel_relaxed(val, io->mem + reg->offset);
58 static void _clk_rmw(u32 val, u32 mask, void __iomem *ptr)
60 u32 v;
62 v = readl_relaxed(ptr);
63 v &= ~mask;
64 v |= val;
65 writel_relaxed(v, ptr);
68 static void clk_memmap_rmw(u32 val, u32 mask, const struct clk_omap_reg *reg)
70 struct clk_iomap *io = clk_memmaps[reg->index];
72 if (reg->ptr) {
73 _clk_rmw(val, mask, reg->ptr);
74 } else if (io->regmap) {
75 regmap_update_bits(io->regmap, reg->offset, mask, val);
76 } else {
77 _clk_rmw(val, mask, io->mem + reg->offset);
81 static u32 clk_memmap_readl(const struct clk_omap_reg *reg)
83 u32 val;
84 struct clk_iomap *io = clk_memmaps[reg->index];
86 if (reg->ptr)
87 val = readl_relaxed(reg->ptr);
88 else if (io->regmap)
89 regmap_read(io->regmap, reg->offset, &val);
90 else
91 val = readl_relaxed(io->mem + reg->offset);
93 return val;
96 /**
97 * ti_clk_setup_ll_ops - setup low level clock operations
98 * @ops: low level clock ops descriptor
100 * Sets up low level clock operations for TI clock driver. This is used
101 * to provide various callbacks for the clock driver towards platform
102 * specific code. Returns 0 on success, -EBUSY if ll_ops have been
103 * registered already.
105 int ti_clk_setup_ll_ops(struct ti_clk_ll_ops *ops)
107 if (ti_clk_ll_ops) {
108 pr_err("Attempt to register ll_ops multiple times.\n");
109 return -EBUSY;
112 ti_clk_ll_ops = ops;
113 ops->clk_readl = clk_memmap_readl;
114 ops->clk_writel = clk_memmap_writel;
115 ops->clk_rmw = clk_memmap_rmw;
117 return 0;
121 * ti_dt_clocks_register - register DT alias clocks during boot
122 * @oclks: list of clocks to register
124 * Register alias or non-standard DT clock entries during boot. By
125 * default, DT clocks are found based on their node name. If any
126 * additional con-id / dev-id -> clock mapping is required, use this
127 * function to list these.
129 void __init ti_dt_clocks_register(struct ti_dt_clk oclks[])
131 struct ti_dt_clk *c;
132 struct device_node *node, *parent;
133 struct clk *clk;
134 struct of_phandle_args clkspec;
135 char buf[64];
136 char *ptr;
137 char *tags[2];
138 int i;
139 int num_args;
140 int ret;
141 static bool clkctrl_nodes_missing;
142 static bool has_clkctrl_data;
144 for (c = oclks; c->node_name != NULL; c++) {
145 strcpy(buf, c->node_name);
146 ptr = buf;
147 for (i = 0; i < 2; i++)
148 tags[i] = NULL;
149 num_args = 0;
150 while (*ptr) {
151 if (*ptr == ':') {
152 if (num_args >= 2) {
153 pr_warn("Bad number of tags on %s\n",
154 c->node_name);
155 return;
157 tags[num_args++] = ptr + 1;
158 *ptr = 0;
160 ptr++;
163 if (num_args && clkctrl_nodes_missing)
164 continue;
166 node = of_find_node_by_name(NULL, buf);
167 if (num_args) {
168 parent = node;
169 node = of_get_child_by_name(parent, "clk");
170 of_node_put(parent);
173 clkspec.np = node;
174 clkspec.args_count = num_args;
175 for (i = 0; i < num_args; i++) {
176 ret = kstrtoint(tags[i], i ? 10 : 16, clkspec.args + i);
177 if (ret) {
178 pr_warn("Bad tag in %s at %d: %s\n",
179 c->node_name, i, tags[i]);
180 of_node_put(node);
181 return;
184 clk = of_clk_get_from_provider(&clkspec);
185 of_node_put(node);
186 if (!IS_ERR(clk)) {
187 c->lk.clk = clk;
188 clkdev_add(&c->lk);
189 } else {
190 if (num_args && !has_clkctrl_data) {
191 struct device_node *np;
193 np = of_find_compatible_node(NULL, NULL,
194 "ti,clkctrl");
195 if (np) {
196 has_clkctrl_data = true;
197 of_node_put(np);
198 } else {
199 clkctrl_nodes_missing = true;
201 pr_warn("missing clkctrl nodes, please update your dts.\n");
202 continue;
206 pr_warn("failed to lookup clock node %s, ret=%ld\n",
207 c->node_name, PTR_ERR(clk));
212 struct clk_init_item {
213 struct device_node *node;
214 void *user;
215 ti_of_clk_init_cb_t func;
216 struct list_head link;
219 static LIST_HEAD(retry_list);
222 * ti_clk_retry_init - retries a failed clock init at later phase
223 * @node: device not for the clock
224 * @user: user data pointer
225 * @func: init function to be called for the clock
227 * Adds a failed clock init to the retry list. The retry list is parsed
228 * once all the other clocks have been initialized.
230 int __init ti_clk_retry_init(struct device_node *node, void *user,
231 ti_of_clk_init_cb_t func)
233 struct clk_init_item *retry;
235 pr_debug("%s: adding to retry list...\n", node->name);
236 retry = kzalloc(sizeof(*retry), GFP_KERNEL);
237 if (!retry)
238 return -ENOMEM;
240 retry->node = node;
241 retry->func = func;
242 retry->user = user;
243 list_add(&retry->link, &retry_list);
245 return 0;
249 * ti_clk_get_reg_addr - get register address for a clock register
250 * @node: device node for the clock
251 * @index: register index from the clock node
252 * @reg: pointer to target register struct
254 * Builds clock register address from device tree information, and returns
255 * the data via the provided output pointer @reg. Returns 0 on success,
256 * negative error value on failure.
258 int ti_clk_get_reg_addr(struct device_node *node, int index,
259 struct clk_omap_reg *reg)
261 u32 val;
262 int i;
264 for (i = 0; i < CLK_MAX_MEMMAPS; i++) {
265 if (clocks_node_ptr[i] == node->parent)
266 break;
269 if (i == CLK_MAX_MEMMAPS) {
270 pr_err("clk-provider not found for %s!\n", node->name);
271 return -ENOENT;
274 reg->index = i;
276 if (of_property_read_u32_index(node, "reg", index, &val)) {
277 pr_err("%s must have reg[%d]!\n", node->name, index);
278 return -EINVAL;
281 reg->offset = val;
282 reg->ptr = NULL;
284 return 0;
287 void ti_clk_latch(struct clk_omap_reg *reg, s8 shift)
289 u32 latch;
291 if (shift < 0)
292 return;
294 latch = 1 << shift;
296 ti_clk_ll_ops->clk_rmw(latch, latch, reg);
297 ti_clk_ll_ops->clk_rmw(0, latch, reg);
298 ti_clk_ll_ops->clk_readl(reg); /* OCP barrier */
302 * omap2_clk_provider_init - init master clock provider
303 * @parent: master node
304 * @index: internal index for clk_reg_ops
305 * @syscon: syscon regmap pointer for accessing clock registers
306 * @mem: iomem pointer for the clock provider memory area, only used if
307 * syscon is not provided
309 * Initializes a master clock IP block. This basically sets up the
310 * mapping from clocks node to the memory map index. All the clocks
311 * are then initialized through the common of_clk_init call, and the
312 * clocks will access their memory maps based on the node layout.
313 * Returns 0 in success.
315 int __init omap2_clk_provider_init(struct device_node *parent, int index,
316 struct regmap *syscon, void __iomem *mem)
318 struct device_node *clocks;
319 struct clk_iomap *io;
321 /* get clocks for this parent */
322 clocks = of_get_child_by_name(parent, "clocks");
323 if (!clocks) {
324 pr_err("%s missing 'clocks' child node.\n", parent->name);
325 return -EINVAL;
328 /* add clocks node info */
329 clocks_node_ptr[index] = clocks;
331 io = kzalloc(sizeof(*io), GFP_KERNEL);
332 if (!io)
333 return -ENOMEM;
335 io->regmap = syscon;
336 io->mem = mem;
338 clk_memmaps[index] = io;
340 return 0;
344 * omap2_clk_legacy_provider_init - initialize a legacy clock provider
345 * @index: index for the clock provider
346 * @mem: iomem pointer for the clock provider memory area
348 * Initializes a legacy clock provider memory mapping.
350 void __init omap2_clk_legacy_provider_init(int index, void __iomem *mem)
352 struct clk_iomap *io;
354 io = memblock_virt_alloc(sizeof(*io), 0);
356 io->mem = mem;
358 clk_memmaps[index] = io;
362 * ti_dt_clk_init_retry_clks - init clocks from the retry list
364 * Initializes any clocks that have failed to initialize before,
365 * reasons being missing parent node(s) during earlier init. This
366 * typically happens only for DPLLs which need to have both of their
367 * parent clocks ready during init.
369 void ti_dt_clk_init_retry_clks(void)
371 struct clk_init_item *retry;
372 struct clk_init_item *tmp;
373 int retries = 5;
375 while (!list_empty(&retry_list) && retries) {
376 list_for_each_entry_safe(retry, tmp, &retry_list, link) {
377 pr_debug("retry-init: %s\n", retry->node->name);
378 retry->func(retry->user, retry->node);
379 list_del(&retry->link);
380 kfree(retry);
382 retries--;
386 static const struct of_device_id simple_clk_match_table[] __initconst = {
387 { .compatible = "fixed-clock" },
388 { .compatible = "fixed-factor-clock" },
393 * ti_clk_add_aliases - setup clock aliases
395 * Sets up any missing clock aliases. No return value.
397 void __init ti_clk_add_aliases(void)
399 struct device_node *np;
400 struct clk *clk;
402 for_each_matching_node(np, simple_clk_match_table) {
403 struct of_phandle_args clkspec;
405 clkspec.np = np;
406 clk = of_clk_get_from_provider(&clkspec);
408 ti_clk_add_alias(NULL, clk, np->name);
413 * ti_clk_setup_features - setup clock features flags
414 * @features: features definition to use
416 * Initializes the clock driver features flags based on platform
417 * provided data. No return value.
419 void __init ti_clk_setup_features(struct ti_clk_features *features)
421 memcpy(&ti_clk_features, features, sizeof(*features));
425 * ti_clk_get_features - get clock driver features flags
427 * Get TI clock driver features description. Returns a pointer
428 * to the current feature setup.
430 const struct ti_clk_features *ti_clk_get_features(void)
432 return &ti_clk_features;
436 * omap2_clk_enable_init_clocks - prepare & enable a list of clocks
437 * @clk_names: ptr to an array of strings of clock names to enable
438 * @num_clocks: number of clock names in @clk_names
440 * Prepare and enable a list of clocks, named by @clk_names. No
441 * return value. XXX Deprecated; only needed until these clocks are
442 * properly claimed and enabled by the drivers or core code that uses
443 * them. XXX What code disables & calls clk_put on these clocks?
445 void omap2_clk_enable_init_clocks(const char **clk_names, u8 num_clocks)
447 struct clk *init_clk;
448 int i;
450 for (i = 0; i < num_clocks; i++) {
451 init_clk = clk_get(NULL, clk_names[i]);
452 if (WARN(IS_ERR(init_clk), "could not find init clock %s\n",
453 clk_names[i]))
454 continue;
455 clk_prepare_enable(init_clk);
460 * ti_clk_add_alias - add a clock alias for a TI clock
461 * @dev: device alias for this clock
462 * @clk: clock handle to create alias for
463 * @con: connection ID for this clock
465 * Creates a clock alias for a TI clock. Allocates the clock lookup entry
466 * and assigns the data to it. Returns 0 if successful, negative error
467 * value otherwise.
469 int ti_clk_add_alias(struct device *dev, struct clk *clk, const char *con)
471 struct clk_lookup *cl;
473 if (!clk)
474 return 0;
476 if (IS_ERR(clk))
477 return PTR_ERR(clk);
479 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
480 if (!cl)
481 return -ENOMEM;
483 if (dev)
484 cl->dev_id = dev_name(dev);
485 cl->con_id = con;
486 cl->clk = clk;
488 clkdev_add(cl);
490 return 0;
494 * ti_clk_register - register a TI clock to the common clock framework
495 * @dev: device for this clock
496 * @hw: hardware clock handle
497 * @con: connection ID for this clock
499 * Registers a TI clock to the common clock framework, and adds a clock
500 * alias for it. Returns a handle to the registered clock if successful,
501 * ERR_PTR value in failure.
503 struct clk *ti_clk_register(struct device *dev, struct clk_hw *hw,
504 const char *con)
506 struct clk *clk;
507 int ret;
509 clk = clk_register(dev, hw);
510 if (IS_ERR(clk))
511 return clk;
513 ret = ti_clk_add_alias(dev, clk, con);
514 if (ret) {
515 clk_unregister(clk);
516 return ERR_PTR(ret);
519 return clk;