1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Clock and PLL control for C64x+ devices
5 * Copyright (C) 2010, 2011 Texas Instruments.
8 * Copied heavily from arm/mach-davinci/clock.c, so:
10 * Copyright (C) 2006-2007 Texas Instruments.
11 * Copyright (C) 2008-2009 Deep Root Systems, LLC
14 #include <linux/module.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk.h>
18 #include <linux/err.h>
20 #include <asm/clock.h>
23 static LIST_HEAD(clocks);
24 static DEFINE_MUTEX(clocks_mutex);
25 static DEFINE_SPINLOCK(clockfw_lock);
27 static void __clk_enable(struct clk *clk)
30 __clk_enable(clk->parent);
34 static void __clk_disable(struct clk *clk)
36 if (WARN_ON(clk->usecount == 0))
41 __clk_disable(clk->parent);
44 int clk_enable(struct clk *clk)
48 if (clk == NULL || IS_ERR(clk))
51 spin_lock_irqsave(&clockfw_lock, flags);
53 spin_unlock_irqrestore(&clockfw_lock, flags);
57 EXPORT_SYMBOL(clk_enable);
59 void clk_disable(struct clk *clk)
63 if (clk == NULL || IS_ERR(clk))
66 spin_lock_irqsave(&clockfw_lock, flags);
68 spin_unlock_irqrestore(&clockfw_lock, flags);
70 EXPORT_SYMBOL(clk_disable);
72 unsigned long clk_get_rate(struct clk *clk)
74 if (clk == NULL || IS_ERR(clk))
79 EXPORT_SYMBOL(clk_get_rate);
81 long clk_round_rate(struct clk *clk, unsigned long rate)
83 if (clk == NULL || IS_ERR(clk))
87 return clk->round_rate(clk, rate);
91 EXPORT_SYMBOL(clk_round_rate);
93 /* Propagate rate to children */
94 static void propagate_rate(struct clk *root)
98 list_for_each_entry(clk, &root->children, childnode) {
100 clk->rate = clk->recalc(clk);
105 int clk_set_rate(struct clk *clk, unsigned long rate)
110 if (clk == NULL || IS_ERR(clk))
114 ret = clk->set_rate(clk, rate);
116 spin_lock_irqsave(&clockfw_lock, flags);
119 clk->rate = clk->recalc(clk);
122 spin_unlock_irqrestore(&clockfw_lock, flags);
126 EXPORT_SYMBOL(clk_set_rate);
128 int clk_set_parent(struct clk *clk, struct clk *parent)
132 if (clk == NULL || IS_ERR(clk))
135 /* Cannot change parent on enabled clock */
136 if (WARN_ON(clk->usecount))
139 mutex_lock(&clocks_mutex);
140 clk->parent = parent;
141 list_del_init(&clk->childnode);
142 list_add(&clk->childnode, &clk->parent->children);
143 mutex_unlock(&clocks_mutex);
145 spin_lock_irqsave(&clockfw_lock, flags);
147 clk->rate = clk->recalc(clk);
149 spin_unlock_irqrestore(&clockfw_lock, flags);
153 EXPORT_SYMBOL(clk_set_parent);
155 int clk_register(struct clk *clk)
157 if (clk == NULL || IS_ERR(clk))
160 if (WARN(clk->parent && !clk->parent->rate,
161 "CLK: %s parent %s has no rate!\n",
162 clk->name, clk->parent->name))
165 mutex_lock(&clocks_mutex);
166 list_add_tail(&clk->node, &clocks);
168 list_add_tail(&clk->childnode, &clk->parent->children);
169 mutex_unlock(&clocks_mutex);
171 /* If rate is already set, use it */
175 /* Else, see if there is a way to calculate it */
177 clk->rate = clk->recalc(clk);
179 /* Otherwise, default to parent rate */
180 else if (clk->parent)
181 clk->rate = clk->parent->rate;
185 EXPORT_SYMBOL(clk_register);
187 void clk_unregister(struct clk *clk)
189 if (clk == NULL || IS_ERR(clk))
192 mutex_lock(&clocks_mutex);
193 list_del(&clk->node);
194 list_del(&clk->childnode);
195 mutex_unlock(&clocks_mutex);
197 EXPORT_SYMBOL(clk_unregister);
200 static u32 pll_read(struct pll_data *pll, int reg)
202 return soc_readl(pll->base + reg);
205 static unsigned long clk_sysclk_recalc(struct clk *clk)
208 struct pll_data *pll;
209 unsigned long rate = clk->rate;
211 if (WARN_ON(!clk->parent))
214 rate = clk->parent->rate;
216 /* the parent must be a PLL */
217 if (WARN_ON(!clk->parent->pll_data))
220 pll = clk->parent->pll_data;
222 /* If pre-PLL, source clock is before the multiplier and divider(s) */
223 if (clk->flags & PRE_PLL)
224 rate = pll->input_rate;
227 pr_debug("%s: (no divider) rate = %lu KHz\n",
228 clk->name, rate / 1000);
232 if (clk->flags & FIXED_DIV_PLL) {
234 pr_debug("%s: (fixed divide by %d) rate = %lu KHz\n",
235 clk->name, clk->div, rate / 1000);
239 v = pll_read(pll, clk->div);
241 plldiv = (v & PLLDIV_RATIO_MASK) + 1;
248 pr_debug("%s: (divide by %d) rate = %lu KHz\n",
249 clk->name, plldiv, rate / 1000);
254 static unsigned long clk_leafclk_recalc(struct clk *clk)
256 if (WARN_ON(!clk->parent))
259 pr_debug("%s: (parent %s) rate = %lu KHz\n",
260 clk->name, clk->parent->name, clk->parent->rate / 1000);
262 return clk->parent->rate;
265 static unsigned long clk_pllclk_recalc(struct clk *clk)
267 u32 ctrl, mult = 0, prediv = 0, postdiv = 0;
269 struct pll_data *pll = clk->pll_data;
270 unsigned long rate = clk->rate;
272 if (clk->flags & FIXED_RATE_PLL)
275 ctrl = pll_read(pll, PLLCTL);
276 rate = pll->input_rate = clk->parent->rate;
278 if (ctrl & PLLCTL_PLLEN)
283 if (pll->flags & PLL_HAS_MUL) {
284 mult = pll_read(pll, PLLM);
285 mult = (mult & PLLM_PLLM_MASK) + 1;
287 if (pll->flags & PLL_HAS_PRE) {
288 prediv = pll_read(pll, PLLPRE);
289 if (prediv & PLLDIV_EN)
290 prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
294 if (pll->flags & PLL_HAS_POST) {
295 postdiv = pll_read(pll, PLLPOST);
296 if (postdiv & PLLDIV_EN)
297 postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
310 pr_debug("PLL%d: input = %luMHz, pre[%d] mul[%d] post[%d] "
311 "--> %luMHz output.\n",
312 pll->num, clk->parent->rate / 1000000,
313 prediv, mult, postdiv, rate / 1000000);
315 pr_debug("PLL%d: input = %luMHz, bypass mode.\n",
316 pll->num, clk->parent->rate / 1000000);
322 static void __init __init_clk(struct clk *clk)
324 INIT_LIST_HEAD(&clk->node);
325 INIT_LIST_HEAD(&clk->children);
326 INIT_LIST_HEAD(&clk->childnode);
330 /* Check if clock is a PLL */
332 clk->recalc = clk_pllclk_recalc;
334 /* Else, if it is a PLL-derived clock */
335 else if (clk->flags & CLK_PLL)
336 clk->recalc = clk_sysclk_recalc;
338 /* Otherwise, it is a leaf clock (PSC clock) */
339 else if (clk->parent)
340 clk->recalc = clk_leafclk_recalc;
344 void __init c6x_clks_init(struct clk_lookup *clocks)
346 struct clk_lookup *c;
348 size_t num_clocks = 0;
350 for (c = clocks; c->clk; c++) {
357 /* Turn on clocks that Linux doesn't otherwise manage */
358 if (clk->flags & ALWAYS_ENABLED)
362 clkdev_add_table(clocks, num_clocks);
365 #ifdef CONFIG_DEBUG_FS
367 #include <linux/debugfs.h>
368 #include <linux/seq_file.h>
370 #define CLKNAME_MAX 10 /* longest clock name */
375 dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
378 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
382 if (parent->flags & CLK_PLL)
387 /* <nest spaces> name <pad to end> */
388 memset(buf, ' ', sizeof(buf) - 1);
389 buf[sizeof(buf) - 1] = 0;
390 i = strlen(parent->name);
391 memcpy(buf + nest, parent->name,
392 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
394 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
395 buf, parent->usecount, state, clk_get_rate(parent));
396 /* REVISIT show device associations too */
398 /* cost is now small, but not linear... */
399 list_for_each_entry(clk, &parent->children, childnode) {
400 dump_clock(s, nest + NEST_DELTA, clk);
404 static int c6x_ck_show(struct seq_file *m, void *v)
409 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
411 mutex_lock(&clocks_mutex);
412 list_for_each_entry(clk, &clocks, node)
414 dump_clock(m, 0, clk);
415 mutex_unlock(&clocks_mutex);
420 static int c6x_ck_open(struct inode *inode, struct file *file)
422 return single_open(file, c6x_ck_show, NULL);
425 static const struct file_operations c6x_ck_operations = {
429 .release = single_release,
432 static int __init c6x_clk_debugfs_init(void)
434 debugfs_create_file("c6x_clocks", S_IFREG | S_IRUGO, NULL, NULL,
439 device_initcall(c6x_clk_debugfs_init);
440 #endif /* CONFIG_DEBUG_FS */