2 * linux/arch/arm/plat-omap/clock.c
4 * Copyright (C) 2004 - 2008 Nokia corporation
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
7 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/list.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/clk.h>
21 #include <linux/mutex.h>
22 #include <linux/platform_device.h>
23 #include <linux/cpufreq.h>
24 #include <linux/debugfs.h>
27 #include <mach/clock.h>
29 static LIST_HEAD(clocks);
30 static DEFINE_MUTEX(clocks_mutex);
31 static DEFINE_SPINLOCK(clockfw_lock);
33 static struct clk_functions *arch_clock;
35 /*-------------------------------------------------------------------------
36 * Standard clock functions defined in include/linux/clk.h
37 *-------------------------------------------------------------------------*/
40 * Returns a clock. Note that we first try to use device id on the bus
41 * and clock name. If this fails, we try to use clock name only.
43 struct clk * clk_get(struct device *dev, const char *id)
45 struct clk *p, *clk = ERR_PTR(-ENOENT);
48 if (dev == NULL || dev->bus != &platform_bus_type)
51 idno = to_platform_device(dev)->id;
53 mutex_lock(&clocks_mutex);
55 list_for_each_entry(p, &clocks, node) {
57 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
63 list_for_each_entry(p, &clocks, node) {
64 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
71 mutex_unlock(&clocks_mutex);
75 EXPORT_SYMBOL(clk_get);
77 int clk_enable(struct clk *clk)
82 if (clk == NULL || IS_ERR(clk))
85 spin_lock_irqsave(&clockfw_lock, flags);
86 if (arch_clock->clk_enable) {
87 ret = arch_clock->clk_enable(clk);
88 if (ret == 0 && clk->flags & RECALC_ON_ENABLE) {
90 (*clk->recalc)(clk, clk->parent->rate,
92 if (clk->flags & RATE_PROPAGATES)
93 propagate_rate(clk, CURRENT_RATE);
97 spin_unlock_irqrestore(&clockfw_lock, flags);
101 EXPORT_SYMBOL(clk_enable);
103 void clk_disable(struct clk *clk)
107 if (clk == NULL || IS_ERR(clk))
110 spin_lock_irqsave(&clockfw_lock, flags);
111 if (clk->usecount == 0) {
112 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
118 if (arch_clock->clk_disable) {
119 arch_clock->clk_disable(clk);
120 if (clk->flags & RECALC_ON_ENABLE) {
122 (*clk->recalc)(clk, clk->parent->rate,
124 if (clk->flags & RATE_PROPAGATES)
125 propagate_rate(clk, CURRENT_RATE);
130 spin_unlock_irqrestore(&clockfw_lock, flags);
132 EXPORT_SYMBOL(clk_disable);
134 int clk_get_usecount(struct clk *clk)
139 if (clk == NULL || IS_ERR(clk))
142 spin_lock_irqsave(&clockfw_lock, flags);
144 spin_unlock_irqrestore(&clockfw_lock, flags);
148 EXPORT_SYMBOL(clk_get_usecount);
150 unsigned long clk_get_rate(struct clk *clk)
153 unsigned long ret = 0;
155 if (clk == NULL || IS_ERR(clk))
158 spin_lock_irqsave(&clockfw_lock, flags);
160 spin_unlock_irqrestore(&clockfw_lock, flags);
164 EXPORT_SYMBOL(clk_get_rate);
166 void clk_put(struct clk *clk)
168 if (clk && !IS_ERR(clk))
169 module_put(clk->owner);
171 EXPORT_SYMBOL(clk_put);
173 /*-------------------------------------------------------------------------
174 * Optional clock functions defined in include/linux/clk.h
175 *-------------------------------------------------------------------------*/
177 long clk_round_rate(struct clk *clk, unsigned long rate)
182 if (clk == NULL || IS_ERR(clk))
185 spin_lock_irqsave(&clockfw_lock, flags);
186 if (arch_clock->clk_round_rate)
187 ret = arch_clock->clk_round_rate(clk, rate);
188 spin_unlock_irqrestore(&clockfw_lock, flags);
192 EXPORT_SYMBOL(clk_round_rate);
194 int clk_set_rate(struct clk *clk, unsigned long rate)
199 if (clk == NULL || IS_ERR(clk))
202 spin_lock_irqsave(&clockfw_lock, flags);
204 if (arch_clock->clk_set_rate) {
205 ret = arch_clock->clk_set_rate(clk, rate);
208 (*clk->recalc)(clk, clk->parent->rate,
210 if (clk->flags & RATE_PROPAGATES)
211 propagate_rate(clk, CURRENT_RATE);
215 spin_unlock_irqrestore(&clockfw_lock, flags);
219 EXPORT_SYMBOL(clk_set_rate);
221 int clk_set_parent(struct clk *clk, struct clk *parent)
226 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
229 spin_lock_irqsave(&clockfw_lock, flags);
231 if (arch_clock->clk_set_parent) {
232 ret = arch_clock->clk_set_parent(clk, parent);
235 (*clk->recalc)(clk, clk->parent->rate,
237 if (clk->flags & RATE_PROPAGATES)
238 propagate_rate(clk, CURRENT_RATE);
242 spin_unlock_irqrestore(&clockfw_lock, flags);
246 EXPORT_SYMBOL(clk_set_parent);
248 struct clk *clk_get_parent(struct clk *clk)
251 struct clk * ret = NULL;
253 if (clk == NULL || IS_ERR(clk))
256 spin_lock_irqsave(&clockfw_lock, flags);
257 if (arch_clock->clk_get_parent)
258 ret = arch_clock->clk_get_parent(clk);
259 spin_unlock_irqrestore(&clockfw_lock, flags);
263 EXPORT_SYMBOL(clk_get_parent);
265 /*-------------------------------------------------------------------------
266 * OMAP specific clock functions shared between omap1 and omap2
267 *-------------------------------------------------------------------------*/
269 unsigned int __initdata mpurate;
272 * By default we use the rate set by the bootloader.
273 * You can override this with mpurate= cmdline option.
275 static int __init omap_clk_setup(char *str)
277 get_option(&str, &mpurate);
287 __setup("mpurate=", omap_clk_setup);
289 /* Used for clocks that always have same value as the parent clock */
290 void followparent_recalc(struct clk *clk, unsigned long new_parent_rate,
293 if (rate_storage == CURRENT_RATE)
294 clk->rate = new_parent_rate;
295 else if (rate_storage == TEMP_RATE)
296 clk->temp_rate = new_parent_rate;
299 /* Propagate rate to children */
300 void propagate_rate(struct clk *tclk, u8 rate_storage)
303 unsigned long parent_rate = 0;
305 if (tclk == NULL || IS_ERR(tclk))
308 list_for_each_entry(clkp, &clocks, node) {
309 if (likely(clkp->parent != tclk))
312 if (rate_storage == CURRENT_RATE)
313 parent_rate = tclk->rate;
314 else if (rate_storage == TEMP_RATE)
315 parent_rate = tclk->temp_rate;
318 clkp->recalc(clkp, parent_rate, rate_storage);
319 if (clkp->flags & RATE_PROPAGATES)
320 propagate_rate(clkp, rate_storage);
325 * recalculate_root_clocks - recalculate and propagate all root clocks
327 * Recalculates all root clocks (clocks with no parent), which if the
328 * clock's .recalc is set correctly, should also propagate their rates.
331 void recalculate_root_clocks(void)
335 list_for_each_entry(clkp, &clocks, node) {
336 if (unlikely(!clkp->parent)) {
338 clkp->recalc(clkp, 0, CURRENT_RATE);
339 if (clkp->flags & RATE_PROPAGATES)
340 propagate_rate(clkp, CURRENT_RATE);
345 int clk_register(struct clk *clk)
347 if (clk == NULL || IS_ERR(clk))
350 mutex_lock(&clocks_mutex);
351 list_add(&clk->node, &clocks);
354 mutex_unlock(&clocks_mutex);
358 EXPORT_SYMBOL(clk_register);
360 void clk_unregister(struct clk *clk)
362 if (clk == NULL || IS_ERR(clk))
365 mutex_lock(&clocks_mutex);
366 list_del(&clk->node);
367 mutex_unlock(&clocks_mutex);
369 EXPORT_SYMBOL(clk_unregister);
371 void clk_deny_idle(struct clk *clk)
375 if (clk == NULL || IS_ERR(clk))
378 spin_lock_irqsave(&clockfw_lock, flags);
379 if (arch_clock->clk_deny_idle)
380 arch_clock->clk_deny_idle(clk);
381 spin_unlock_irqrestore(&clockfw_lock, flags);
383 EXPORT_SYMBOL(clk_deny_idle);
385 void clk_allow_idle(struct clk *clk)
389 if (clk == NULL || IS_ERR(clk))
392 spin_lock_irqsave(&clockfw_lock, flags);
393 if (arch_clock->clk_allow_idle)
394 arch_clock->clk_allow_idle(clk);
395 spin_unlock_irqrestore(&clockfw_lock, flags);
397 EXPORT_SYMBOL(clk_allow_idle);
399 void clk_enable_init_clocks(void)
403 list_for_each_entry(clkp, &clocks, node) {
404 if (clkp->flags & ENABLE_ON_INIT)
408 EXPORT_SYMBOL(clk_enable_init_clocks);
410 #ifdef CONFIG_CPU_FREQ
411 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
415 spin_lock_irqsave(&clockfw_lock, flags);
416 if (arch_clock->clk_init_cpufreq_table)
417 arch_clock->clk_init_cpufreq_table(table);
418 spin_unlock_irqrestore(&clockfw_lock, flags);
420 EXPORT_SYMBOL(clk_init_cpufreq_table);
423 /*-------------------------------------------------------------------------*/
425 #ifdef CONFIG_OMAP_RESET_CLOCKS
427 * Disable any unused clocks left on by the bootloader
429 static int __init clk_disable_unused(void)
434 list_for_each_entry(ck, &clocks, node) {
435 if (ck->usecount > 0 ||
436 (ck->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK)))
439 if (cpu_class_is_omap1() && ck->enable_reg == 0)
442 spin_lock_irqsave(&clockfw_lock, flags);
443 if (arch_clock->clk_disable_unused)
444 arch_clock->clk_disable_unused(ck);
445 spin_unlock_irqrestore(&clockfw_lock, flags);
450 late_initcall(clk_disable_unused);
453 int __init clk_init(struct clk_functions * custom_clocks)
455 if (!custom_clocks) {
456 printk(KERN_ERR "No custom clock functions registered\n");
460 arch_clock = custom_clocks;
465 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
467 * debugfs support to trace clock tree hierarchy and attributes
469 static struct dentry *clk_debugfs_root;
471 static int clk_debugfs_register_one(struct clk *c)
474 struct dentry *d, *child;
475 struct clk *pa = c->parent;
479 p += sprintf(p, "%s", c->name);
481 sprintf(p, ":%d", c->id);
482 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
487 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
492 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
497 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
506 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
507 debugfs_remove(child);
508 debugfs_remove(c->dent);
512 static int clk_debugfs_register(struct clk *c)
515 struct clk *pa = c->parent;
517 if (pa && !pa->dent) {
518 err = clk_debugfs_register(pa);
524 err = clk_debugfs_register_one(c);
531 static int __init clk_debugfs_init(void)
537 d = debugfs_create_dir("clock", NULL);
540 clk_debugfs_root = d;
542 list_for_each_entry(c, &clocks, node) {
543 err = clk_debugfs_register(c);
549 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
552 late_initcall(clk_debugfs_init);
554 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */