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>
28 #include <mach/clock.h>
30 static LIST_HEAD(clocks);
31 static DEFINE_MUTEX(clocks_mutex);
32 static DEFINE_SPINLOCK(clockfw_lock);
34 static struct clk_functions *arch_clock;
36 /*-------------------------------------------------------------------------
37 * Standard clock functions defined in include/linux/clk.h
38 *-------------------------------------------------------------------------*/
41 * Returns a clock. Note that we first try to use device id on the bus
42 * and clock name. If this fails, we try to use clock name only.
44 struct clk * clk_get(struct device *dev, const char *id)
46 struct clk *p, *clk = ERR_PTR(-ENOENT);
49 if (dev == NULL || dev->bus != &platform_bus_type)
52 idno = to_platform_device(dev)->id;
54 mutex_lock(&clocks_mutex);
56 list_for_each_entry(p, &clocks, node) {
58 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
64 list_for_each_entry(p, &clocks, node) {
65 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
72 mutex_unlock(&clocks_mutex);
76 EXPORT_SYMBOL(clk_get);
78 int clk_enable(struct clk *clk)
83 if (clk == NULL || IS_ERR(clk))
86 spin_lock_irqsave(&clockfw_lock, flags);
87 if (arch_clock->clk_enable)
88 ret = arch_clock->clk_enable(clk);
89 spin_unlock_irqrestore(&clockfw_lock, flags);
93 EXPORT_SYMBOL(clk_enable);
95 void clk_disable(struct clk *clk)
99 if (clk == NULL || IS_ERR(clk))
102 spin_lock_irqsave(&clockfw_lock, flags);
103 if (clk->usecount == 0) {
104 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
110 if (arch_clock->clk_disable)
111 arch_clock->clk_disable(clk);
114 spin_unlock_irqrestore(&clockfw_lock, flags);
116 EXPORT_SYMBOL(clk_disable);
118 int clk_get_usecount(struct clk *clk)
123 if (clk == NULL || IS_ERR(clk))
126 spin_lock_irqsave(&clockfw_lock, flags);
128 spin_unlock_irqrestore(&clockfw_lock, flags);
132 EXPORT_SYMBOL(clk_get_usecount);
134 unsigned long clk_get_rate(struct clk *clk)
137 unsigned long ret = 0;
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_rate);
150 void clk_put(struct clk *clk)
152 if (clk && !IS_ERR(clk))
153 module_put(clk->owner);
155 EXPORT_SYMBOL(clk_put);
157 /*-------------------------------------------------------------------------
158 * Optional clock functions defined in include/linux/clk.h
159 *-------------------------------------------------------------------------*/
161 long clk_round_rate(struct clk *clk, unsigned long rate)
166 if (clk == NULL || IS_ERR(clk))
169 spin_lock_irqsave(&clockfw_lock, flags);
170 if (arch_clock->clk_round_rate)
171 ret = arch_clock->clk_round_rate(clk, rate);
172 spin_unlock_irqrestore(&clockfw_lock, flags);
176 EXPORT_SYMBOL(clk_round_rate);
178 int clk_set_rate(struct clk *clk, unsigned long rate)
183 if (clk == NULL || IS_ERR(clk))
186 spin_lock_irqsave(&clockfw_lock, flags);
187 if (arch_clock->clk_set_rate)
188 ret = arch_clock->clk_set_rate(clk, rate);
189 spin_unlock_irqrestore(&clockfw_lock, flags);
193 EXPORT_SYMBOL(clk_set_rate);
195 int clk_set_parent(struct clk *clk, struct clk *parent)
200 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
203 spin_lock_irqsave(&clockfw_lock, flags);
204 if (arch_clock->clk_set_parent)
205 ret = arch_clock->clk_set_parent(clk, parent);
206 spin_unlock_irqrestore(&clockfw_lock, flags);
210 EXPORT_SYMBOL(clk_set_parent);
212 struct clk *clk_get_parent(struct clk *clk)
215 struct clk * ret = NULL;
217 if (clk == NULL || IS_ERR(clk))
220 spin_lock_irqsave(&clockfw_lock, flags);
221 if (arch_clock->clk_get_parent)
222 ret = arch_clock->clk_get_parent(clk);
223 spin_unlock_irqrestore(&clockfw_lock, flags);
227 EXPORT_SYMBOL(clk_get_parent);
229 /*-------------------------------------------------------------------------
230 * OMAP specific clock functions shared between omap1 and omap2
231 *-------------------------------------------------------------------------*/
233 unsigned int __initdata mpurate;
236 * By default we use the rate set by the bootloader.
237 * You can override this with mpurate= cmdline option.
239 static int __init omap_clk_setup(char *str)
241 get_option(&str, &mpurate);
251 __setup("mpurate=", omap_clk_setup);
253 /* Used for clocks that always have same value as the parent clock */
254 void followparent_recalc(struct clk *clk)
256 if (clk == NULL || IS_ERR(clk))
259 clk->rate = clk->parent->rate;
260 if (unlikely(clk->flags & RATE_PROPAGATES))
264 /* Propagate rate to children */
265 void propagate_rate(struct clk * tclk)
269 if (tclk == NULL || IS_ERR(tclk))
272 list_for_each_entry(clkp, &clocks, node) {
273 if (likely(clkp->parent != tclk))
275 if (likely((u32)clkp->recalc))
281 * recalculate_root_clocks - recalculate and propagate all root clocks
283 * Recalculates all root clocks (clocks with no parent), which if the
284 * clock's .recalc is set correctly, should also propagate their rates.
287 void recalculate_root_clocks(void)
291 list_for_each_entry(clkp, &clocks, node) {
292 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
297 int clk_register(struct clk *clk)
299 if (clk == NULL || IS_ERR(clk))
302 mutex_lock(&clocks_mutex);
303 list_add(&clk->node, &clocks);
306 mutex_unlock(&clocks_mutex);
310 EXPORT_SYMBOL(clk_register);
312 void clk_unregister(struct clk *clk)
314 if (clk == NULL || IS_ERR(clk))
317 mutex_lock(&clocks_mutex);
318 list_del(&clk->node);
319 mutex_unlock(&clocks_mutex);
321 EXPORT_SYMBOL(clk_unregister);
323 void clk_deny_idle(struct clk *clk)
327 if (clk == NULL || IS_ERR(clk))
330 spin_lock_irqsave(&clockfw_lock, flags);
331 if (arch_clock->clk_deny_idle)
332 arch_clock->clk_deny_idle(clk);
333 spin_unlock_irqrestore(&clockfw_lock, flags);
335 EXPORT_SYMBOL(clk_deny_idle);
337 void clk_allow_idle(struct clk *clk)
341 if (clk == NULL || IS_ERR(clk))
344 spin_lock_irqsave(&clockfw_lock, flags);
345 if (arch_clock->clk_allow_idle)
346 arch_clock->clk_allow_idle(clk);
347 spin_unlock_irqrestore(&clockfw_lock, flags);
349 EXPORT_SYMBOL(clk_allow_idle);
351 void clk_enable_init_clocks(void)
355 list_for_each_entry(clkp, &clocks, node) {
356 if (clkp->flags & ENABLE_ON_INIT)
360 EXPORT_SYMBOL(clk_enable_init_clocks);
362 #ifdef CONFIG_CPU_FREQ
363 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
367 spin_lock_irqsave(&clockfw_lock, flags);
368 if (arch_clock->clk_init_cpufreq_table)
369 arch_clock->clk_init_cpufreq_table(table);
370 spin_unlock_irqrestore(&clockfw_lock, flags);
372 EXPORT_SYMBOL(clk_init_cpufreq_table);
375 /*-------------------------------------------------------------------------*/
377 #ifdef CONFIG_OMAP_RESET_CLOCKS
379 * Disable any unused clocks left on by the bootloader
381 static int __init clk_disable_unused(void)
386 list_for_each_entry(ck, &clocks, node) {
387 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
391 spin_lock_irqsave(&clockfw_lock, flags);
392 if (arch_clock->clk_disable_unused)
393 arch_clock->clk_disable_unused(ck);
394 spin_unlock_irqrestore(&clockfw_lock, flags);
399 late_initcall(clk_disable_unused);
402 int __init clk_init(struct clk_functions * custom_clocks)
404 if (!custom_clocks) {
405 printk(KERN_ERR "No custom clock functions registered\n");
409 arch_clock = custom_clocks;
414 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
416 * debugfs support to trace clock tree hierarchy and attributes
418 static struct dentry *clk_debugfs_root;
420 static int clk_debugfs_register_one(struct clk *c)
423 struct dentry *d, *child;
424 struct clk *pa = c->parent;
428 p += sprintf(p, "%s", c->name);
430 sprintf(p, ":%d", c->id);
431 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
436 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
441 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
446 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
455 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
456 debugfs_remove(child);
457 debugfs_remove(c->dent);
461 static int clk_debugfs_register(struct clk *c)
464 struct clk *pa = c->parent;
466 if (pa && !pa->dent) {
467 err = clk_debugfs_register(pa);
473 err = clk_debugfs_register_one(c);
480 static int __init clk_debugfs_init(void)
486 d = debugfs_create_dir("clock", NULL);
489 clk_debugfs_root = d;
491 list_for_each_entry(c, &clocks, node) {
492 err = clk_debugfs_register(c);
498 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
501 late_initcall(clk_debugfs_init);
503 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */