2 * Clock and PLL control for DaVinci devices
4 * Copyright (C) 2006-2007 Texas Instruments.
5 * Copyright (C) 2008-2009 Deep Root Systems, LLC
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/platform_device.h>
23 #include <mach/hardware.h>
26 #include <mach/cputype.h>
29 static LIST_HEAD(clocks);
30 static DEFINE_MUTEX(clocks_mutex);
31 static DEFINE_SPINLOCK(clockfw_lock);
33 static unsigned psc_domain(struct clk *clk)
35 return (clk->flags & PSC_DSP)
36 ? DAVINCI_GPSC_DSPDOMAIN
37 : DAVINCI_GPSC_ARMDOMAIN;
40 static void __clk_enable(struct clk *clk)
43 __clk_enable(clk->parent);
44 if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
45 davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
49 static void __clk_disable(struct clk *clk)
51 if (WARN_ON(clk->usecount == 0))
53 if (--clk->usecount == 0 && !(clk->flags & CLK_PLL))
54 davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
57 __clk_disable(clk->parent);
60 int clk_enable(struct clk *clk)
64 if (clk == NULL || IS_ERR(clk))
67 spin_lock_irqsave(&clockfw_lock, flags);
69 spin_unlock_irqrestore(&clockfw_lock, flags);
73 EXPORT_SYMBOL(clk_enable);
75 void clk_disable(struct clk *clk)
79 if (clk == NULL || IS_ERR(clk))
82 spin_lock_irqsave(&clockfw_lock, flags);
84 spin_unlock_irqrestore(&clockfw_lock, flags);
86 EXPORT_SYMBOL(clk_disable);
88 unsigned long clk_get_rate(struct clk *clk)
90 if (clk == NULL || IS_ERR(clk))
95 EXPORT_SYMBOL(clk_get_rate);
97 long clk_round_rate(struct clk *clk, unsigned long rate)
99 if (clk == NULL || IS_ERR(clk))
104 EXPORT_SYMBOL(clk_round_rate);
106 int clk_set_rate(struct clk *clk, unsigned long rate)
108 if (clk == NULL || IS_ERR(clk))
111 /* changing the clk rate is not supported */
114 EXPORT_SYMBOL(clk_set_rate);
116 int clk_register(struct clk *clk)
118 if (clk == NULL || IS_ERR(clk))
121 if (WARN(clk->parent && !clk->parent->rate,
122 "CLK: %s parent %s has no rate!\n",
123 clk->name, clk->parent->name))
126 mutex_lock(&clocks_mutex);
127 list_add_tail(&clk->node, &clocks);
128 mutex_unlock(&clocks_mutex);
130 /* If rate is already set, use it */
134 /* Otherwise, default to parent rate */
136 clk->rate = clk->parent->rate;
140 EXPORT_SYMBOL(clk_register);
142 void clk_unregister(struct clk *clk)
144 if (clk == NULL || IS_ERR(clk))
147 mutex_lock(&clocks_mutex);
148 list_del(&clk->node);
149 mutex_unlock(&clocks_mutex);
151 EXPORT_SYMBOL(clk_unregister);
153 #ifdef CONFIG_DAVINCI_RESET_CLOCKS
155 * Disable any unused clocks left on by the bootloader
157 static int __init clk_disable_unused(void)
161 spin_lock_irq(&clockfw_lock);
162 list_for_each_entry(ck, &clocks, node) {
163 if (ck->usecount > 0)
165 if (!(ck->flags & CLK_PSC))
168 /* ignore if in Disabled or SwRstDisable states */
169 if (!davinci_psc_is_clk_active(ck->psc_ctlr, ck->lpsc))
172 pr_info("Clocks: disable unused %s\n", ck->name);
173 davinci_psc_config(psc_domain(ck), ck->psc_ctlr, ck->lpsc, 0);
175 spin_unlock_irq(&clockfw_lock);
179 late_initcall(clk_disable_unused);
182 static void clk_sysclk_recalc(struct clk *clk)
185 struct pll_data *pll;
187 /* If this is the PLL base clock, no more calculations needed */
191 if (WARN_ON(!clk->parent))
194 clk->rate = clk->parent->rate;
196 /* Otherwise, the parent must be a PLL */
197 if (WARN_ON(!clk->parent->pll_data))
200 pll = clk->parent->pll_data;
202 /* If pre-PLL, source clock is before the multiplier and divider(s) */
203 if (clk->flags & PRE_PLL)
204 clk->rate = pll->input_rate;
209 v = __raw_readl(pll->base + clk->div_reg);
211 plldiv = (v & PLLDIV_RATIO_MASK) + 1;
217 static void __init clk_pll_init(struct clk *clk)
219 u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
221 struct pll_data *pll = clk->pll_data;
223 pll->base = IO_ADDRESS(pll->phys_base);
224 ctrl = __raw_readl(pll->base + PLLCTL);
225 clk->rate = pll->input_rate = clk->parent->rate;
227 if (ctrl & PLLCTL_PLLEN) {
229 mult = __raw_readl(pll->base + PLLM);
230 mult = (mult & PLLM_PLLM_MASK) + 1;
234 if (pll->flags & PLL_HAS_PREDIV) {
235 prediv = __raw_readl(pll->base + PREDIV);
236 if (prediv & PLLDIV_EN)
237 prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
242 /* pre-divider is fixed, but (some?) chips won't report that */
243 if (cpu_is_davinci_dm355() && pll->num == 1)
246 if (pll->flags & PLL_HAS_POSTDIV) {
247 postdiv = __raw_readl(pll->base + POSTDIV);
248 if (postdiv & PLLDIV_EN)
249 postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
257 clk->rate /= postdiv;
260 pr_debug("PLL%d: input = %lu MHz [ ",
261 pll->num, clk->parent->rate / 1000000);
265 pr_debug("/ %d ", prediv);
267 pr_debug("* %d ", mult);
269 pr_debug("/ %d ", postdiv);
270 pr_debug("] --> %lu MHz output.\n", clk->rate / 1000000);
273 int __init davinci_clk_init(struct davinci_clk *clocks)
275 struct davinci_clk *c;
278 for (c = clocks; c->lk.clk; c++) {
284 /* Calculate rates for PLL-derived clocks */
285 else if (clk->flags & CLK_PLL)
286 clk_sysclk_recalc(clk);
289 clk->flags |= CLK_PSC;
294 /* Turn on clocks that Linux doesn't otherwise manage */
295 if (clk->flags & ALWAYS_ENABLED)
302 #ifdef CONFIG_PROC_FS
303 #include <linux/proc_fs.h>
304 #include <linux/seq_file.h>
306 static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
308 return *pos < 1 ? (void *)1 : NULL;
311 static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
317 static void davinci_ck_stop(struct seq_file *m, void *v)
321 #define CLKNAME_MAX 10 /* longest clock name */
326 dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
329 char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
333 if (parent->flags & CLK_PLL)
335 else if (parent->flags & CLK_PSC)
340 /* <nest spaces> name <pad to end> */
341 memset(buf, ' ', sizeof(buf) - 1);
342 buf[sizeof(buf) - 1] = 0;
343 i = strlen(parent->name);
344 memcpy(buf + nest, parent->name,
345 min(i, (unsigned)(sizeof(buf) - 1 - nest)));
347 seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
348 buf, parent->usecount, state, clk_get_rate(parent));
349 /* REVISIT show device associations too */
351 /* cost is now small, but not linear... */
352 list_for_each_entry(clk, &clocks, node) {
353 if (clk->parent == parent)
354 dump_clock(s, nest + NEST_DELTA, clk);
358 static int davinci_ck_show(struct seq_file *m, void *v)
360 /* Show clock tree; we know the main oscillator is first.
361 * We trust nonzero usecounts equate to PSC enables...
363 mutex_lock(&clocks_mutex);
364 if (!list_empty(&clocks))
365 dump_clock(m, 0, list_first_entry(&clocks, struct clk, node));
366 mutex_unlock(&clocks_mutex);
371 static const struct seq_operations davinci_ck_op = {
372 .start = davinci_ck_start,
373 .next = davinci_ck_next,
374 .stop = davinci_ck_stop,
375 .show = davinci_ck_show
378 static int davinci_ck_open(struct inode *inode, struct file *file)
380 return seq_open(file, &davinci_ck_op);
383 static const struct file_operations proc_davinci_ck_operations = {
384 .open = davinci_ck_open,
387 .release = seq_release,
390 static int __init davinci_ck_proc_init(void)
392 proc_create("davinci_clocks", 0, NULL, &proc_davinci_ck_operations);
396 __initcall(davinci_ck_proc_init);
397 #endif /* CONFIG_DEBUG_PROC_FS */