2 * Clock and PLL control for C64x+ devices
4 * Copyright (C) 2010, 2011 Texas Instruments.
5 * Contributed by: Mark Salter <msalter@redhat.com>
7 * Copied heavily from arm/mach-davinci/clock.c, so:
9 * Copyright (C) 2006-2007 Texas Instruments.
10 * Copyright (C) 2008-2009 Deep Root Systems, LLC
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/module.h>
19 #include <linux/clkdev.h>
20 #include <linux/clk.h>
22 #include <linux/err.h>
24 #include <asm/clock.h>
27 static LIST_HEAD(clocks);
28 static DEFINE_MUTEX(clocks_mutex);
29 static DEFINE_SPINLOCK(clockfw_lock);
31 static void __clk_enable(struct clk *clk)
34 __clk_enable(clk->parent);
38 static void __clk_disable(struct clk *clk)
40 if (WARN_ON(clk->usecount == 0))
45 __clk_disable(clk->parent);
48 int clk_enable(struct clk *clk)
52 if (clk == NULL || IS_ERR(clk))
55 spin_lock_irqsave(&clockfw_lock, flags);
57 spin_unlock_irqrestore(&clockfw_lock, flags);
61 EXPORT_SYMBOL(clk_enable);
63 void clk_disable(struct clk *clk)
67 if (clk == NULL || IS_ERR(clk))
70 spin_lock_irqsave(&clockfw_lock, flags);
72 spin_unlock_irqrestore(&clockfw_lock, flags);
74 EXPORT_SYMBOL(clk_disable);
76 unsigned long clk_get_rate(struct clk *clk)
78 if (clk == NULL || IS_ERR(clk))
83 EXPORT_SYMBOL(clk_get_rate);
85 long clk_round_rate(struct clk *clk, unsigned long rate)
87 if (clk == NULL || IS_ERR(clk))
91 return clk->round_rate(clk, rate);
95 EXPORT_SYMBOL(clk_round_rate);
97 /* Propagate rate to children */
98 static void propagate_rate(struct clk *root)
102 list_for_each_entry(clk, &root->children, childnode) {
104 clk->rate = clk->recalc(clk);
109 int clk_set_rate(struct clk *clk, unsigned long rate)
114 if (clk == NULL || IS_ERR(clk))
118 ret = clk->set_rate(clk, rate);
120 spin_lock_irqsave(&clockfw_lock, flags);
123 clk->rate = clk->recalc(clk);
126 spin_unlock_irqrestore(&clockfw_lock, flags);
130 EXPORT_SYMBOL(clk_set_rate);
132 int clk_set_parent(struct clk *clk, struct clk *parent)
136 if (clk == NULL || IS_ERR(clk))
139 /* Cannot change parent on enabled clock */
140 if (WARN_ON(clk->usecount))
143 mutex_lock(&clocks_mutex);
144 clk->parent = parent;
145 list_del_init(&clk->childnode);
146 list_add(&clk->childnode, &clk->parent->children);
147 mutex_unlock(&clocks_mutex);
149 spin_lock_irqsave(&clockfw_lock, flags);
151 clk->rate = clk->recalc(clk);
153 spin_unlock_irqrestore(&clockfw_lock, flags);
157 EXPORT_SYMBOL(clk_set_parent);
159 int clk_register(struct clk *clk)
161 if (clk == NULL || IS_ERR(clk))
164 if (WARN(clk->parent && !clk->parent->rate,
165 "CLK: %s parent %s has no rate!\n",
166 clk->name, clk->parent->name))
169 mutex_lock(&clocks_mutex);
170 list_add_tail(&clk->node, &clocks);
172 list_add_tail(&clk->childnode, &clk->parent->children);
173 mutex_unlock(&clocks_mutex);
175 /* If rate is already set, use it */
179 /* Else, see if there is a way to calculate it */
181 clk->rate = clk->recalc(clk);
183 /* Otherwise, default to parent rate */
184 else if (clk->parent)
185 clk->rate = clk->parent->rate;
189 EXPORT_SYMBOL(clk_register);
191 void clk_unregister(struct clk *clk)
193 if (clk == NULL || IS_ERR(clk))
196 mutex_lock(&clocks_mutex);
197 list_del(&clk->node);
198 list_del(&clk->childnode);
199 mutex_unlock(&clocks_mutex);
201 EXPORT_SYMBOL(clk_unregister);
204 static u32 pll_read(struct pll_data *pll, int reg)
206 return soc_readl(pll->base + reg);
209 static unsigned long clk_sysclk_recalc(struct clk *clk)
212 struct pll_data *pll;
213 unsigned long rate = clk->rate;
215 if (WARN_ON(!clk->parent))
218 rate = clk->parent->rate;
220 /* the parent must be a PLL */
221 if (WARN_ON(!clk->parent->pll_data))
224 pll = clk->parent->pll_data;
226 /* If pre-PLL, source clock is before the multiplier and divider(s) */
227 if (clk->flags & PRE_PLL)
228 rate = pll->input_rate;
231 pr_debug("%s: (no divider) rate = %lu KHz\n",
232 clk->name, rate / 1000);
236 if (clk->flags & FIXED_DIV_PLL) {
238 pr_debug("%s: (fixed divide by %d) rate = %lu KHz\n",
239 clk->name, clk->div, rate / 1000);
243 v = pll_read(pll, clk->div);
245 plldiv = (v & PLLDIV_RATIO_MASK) + 1;
252 pr_debug("%s: (divide by %d) rate = %lu KHz\n",
253 clk->name, plldiv, rate / 1000);
258 static unsigned long clk_leafclk_recalc(struct clk *clk)
260 if (WARN_ON(!clk->parent))
263 pr_debug("%s: (parent %s) rate = %lu KHz\n",
264 clk->name, clk->parent->name, clk->parent->rate / 1000);
266 return clk->parent->rate;
269 static unsigned long clk_pllclk_recalc(struct clk *clk)
271 u32 ctrl, mult = 0, prediv = 0, postdiv = 0;
273 struct pll_data *pll = clk->pll_data;
274 unsigned long rate = clk->rate;
276 if (clk->flags & FIXED_RATE_PLL)
279 ctrl = pll_read(pll, PLLCTL);
280 rate = pll->input_rate = clk->parent->rate;
282 if (ctrl & PLLCTL_PLLEN)
287 if (pll->flags & PLL_HAS_MUL) {
288 mult = pll_read(pll, PLLM);
289 mult = (mult & PLLM_PLLM_MASK) + 1;
291 if (pll->flags & PLL_HAS_PRE) {
292 prediv = pll_read(pll, PLLPRE);
293 if (prediv & PLLDIV_EN)
294 prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
298 if (pll->flags & PLL_HAS_POST) {
299 postdiv = pll_read(pll, PLLPOST);
300 if (postdiv & PLLDIV_EN)
301 postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
314 pr_debug("PLL%d: input = %luMHz, pre[%d] mul[%d] post[%d] "
315 "--> %luMHz output.\n",
316 pll->num, clk->parent->rate / 1000000,
317 prediv, mult, postdiv, rate / 1000000);
319 pr_debug("PLL%d: input = %luMHz, bypass mode.\n",
320 pll->num, clk->parent->rate / 1000000);
326 static void __init __init_clk(struct clk *clk)
328 INIT_LIST_HEAD(&clk->node);
329 INIT_LIST_HEAD(&clk->children);
330 INIT_LIST_HEAD(&clk->childnode);
334 /* Check if clock is a PLL */
336 clk->recalc = clk_pllclk_recalc;
338 /* Else, if it is a PLL-derived clock */
339 else if (clk->flags & CLK_PLL)
340 clk->recalc = clk_sysclk_recalc;
342 /* Otherwise, it is a leaf clock (PSC clock) */
343 else if (clk->parent)
344 clk->recalc = clk_leafclk_recalc;
348 void __init c6x_clks_init(struct clk_lookup *clocks)
350 struct clk_lookup *c;
352 size_t num_clocks = 0;
354 for (c = clocks; c->clk; c++) {
361 /* Turn on clocks that Linux doesn't otherwise manage */
362 if (clk->flags & ALWAYS_ENABLED)
366 clkdev_add_table(clocks, num_clocks);
369 #ifdef CONFIG_DEBUG_FS
371 #include <linux/debugfs.h>
372 #include <linux/seq_file.h>
374 #define CLKNAME_MAX 10 /* longest clock name */
379 dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
382 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
386 if (parent->flags & CLK_PLL)
391 /* <nest spaces> name <pad to end> */
392 memset(buf, ' ', sizeof(buf) - 1);
393 buf[sizeof(buf) - 1] = 0;
394 i = strlen(parent->name);
395 memcpy(buf + nest, parent->name,
396 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
398 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
399 buf, parent->usecount, state, clk_get_rate(parent));
400 /* REVISIT show device associations too */
402 /* cost is now small, but not linear... */
403 list_for_each_entry(clk, &parent->children, childnode) {
404 dump_clock(s, nest + NEST_DELTA, clk);
408 static int c6x_ck_show(struct seq_file *m, void *v)
413 * Show clock tree; We trust nonzero usecounts equate to PSC enables...
415 mutex_lock(&clocks_mutex);
416 list_for_each_entry(clk, &clocks, node)
418 dump_clock(m, 0, clk);
419 mutex_unlock(&clocks_mutex);
424 static int c6x_ck_open(struct inode *inode, struct file *file)
426 return single_open(file, c6x_ck_show, NULL);
429 static const struct file_operations c6x_ck_operations = {
433 .release = single_release,
436 static int __init c6x_clk_debugfs_init(void)
438 debugfs_create_file("c6x_clocks", S_IFREG | S_IRUGO, NULL, NULL,
443 device_initcall(c6x_clk_debugfs_init);
444 #endif /* CONFIG_DEBUG_FS */