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/version.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/list.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/clk.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/cpufreq.h>
25 #include <linux/debugfs.h>
29 #include <mach/clock.h>
31 static LIST_HEAD(clocks);
32 static DEFINE_MUTEX(clocks_mutex);
33 static DEFINE_SPINLOCK(clockfw_lock);
35 static struct clk_functions *arch_clock;
37 /*-------------------------------------------------------------------------
38 * Standard clock functions defined in include/linux/clk.h
39 *-------------------------------------------------------------------------*/
42 * Returns a clock. Note that we first try to use device id on the bus
43 * and clock name. If this fails, we try to use clock name only.
45 struct clk * clk_get(struct device *dev, const char *id)
47 struct clk *p, *clk = ERR_PTR(-ENOENT);
50 if (dev == NULL || dev->bus != &platform_bus_type)
53 idno = to_platform_device(dev)->id;
55 mutex_lock(&clocks_mutex);
57 list_for_each_entry(p, &clocks, node) {
59 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
65 list_for_each_entry(p, &clocks, node) {
66 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
73 mutex_unlock(&clocks_mutex);
77 EXPORT_SYMBOL(clk_get);
79 int clk_enable(struct clk *clk)
84 if (clk == NULL || IS_ERR(clk))
87 spin_lock_irqsave(&clockfw_lock, flags);
88 if (arch_clock->clk_enable)
89 ret = arch_clock->clk_enable(clk);
90 spin_unlock_irqrestore(&clockfw_lock, flags);
94 EXPORT_SYMBOL(clk_enable);
96 void clk_disable(struct clk *clk)
100 if (clk == NULL || IS_ERR(clk))
103 spin_lock_irqsave(&clockfw_lock, flags);
104 if (clk->usecount == 0) {
105 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
111 if (arch_clock->clk_disable)
112 arch_clock->clk_disable(clk);
115 spin_unlock_irqrestore(&clockfw_lock, flags);
117 EXPORT_SYMBOL(clk_disable);
119 int clk_get_usecount(struct clk *clk)
124 if (clk == NULL || IS_ERR(clk))
127 spin_lock_irqsave(&clockfw_lock, flags);
129 spin_unlock_irqrestore(&clockfw_lock, flags);
133 EXPORT_SYMBOL(clk_get_usecount);
135 unsigned long clk_get_rate(struct clk *clk)
138 unsigned long ret = 0;
140 if (clk == NULL || IS_ERR(clk))
143 spin_lock_irqsave(&clockfw_lock, flags);
145 spin_unlock_irqrestore(&clockfw_lock, flags);
149 EXPORT_SYMBOL(clk_get_rate);
151 void clk_put(struct clk *clk)
153 if (clk && !IS_ERR(clk))
154 module_put(clk->owner);
156 EXPORT_SYMBOL(clk_put);
158 /*-------------------------------------------------------------------------
159 * Optional clock functions defined in include/linux/clk.h
160 *-------------------------------------------------------------------------*/
162 long clk_round_rate(struct clk *clk, unsigned long rate)
167 if (clk == NULL || IS_ERR(clk))
170 spin_lock_irqsave(&clockfw_lock, flags);
171 if (arch_clock->clk_round_rate)
172 ret = arch_clock->clk_round_rate(clk, rate);
173 spin_unlock_irqrestore(&clockfw_lock, flags);
177 EXPORT_SYMBOL(clk_round_rate);
179 int clk_set_rate(struct clk *clk, unsigned long rate)
184 if (clk == NULL || IS_ERR(clk))
187 spin_lock_irqsave(&clockfw_lock, flags);
188 if (arch_clock->clk_set_rate)
189 ret = arch_clock->clk_set_rate(clk, rate);
190 spin_unlock_irqrestore(&clockfw_lock, flags);
194 EXPORT_SYMBOL(clk_set_rate);
196 int clk_set_parent(struct clk *clk, struct clk *parent)
201 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
204 spin_lock_irqsave(&clockfw_lock, flags);
205 if (arch_clock->clk_set_parent)
206 ret = arch_clock->clk_set_parent(clk, parent);
207 spin_unlock_irqrestore(&clockfw_lock, flags);
211 EXPORT_SYMBOL(clk_set_parent);
213 struct clk *clk_get_parent(struct clk *clk)
216 struct clk * ret = NULL;
218 if (clk == NULL || IS_ERR(clk))
221 spin_lock_irqsave(&clockfw_lock, flags);
222 if (arch_clock->clk_get_parent)
223 ret = arch_clock->clk_get_parent(clk);
224 spin_unlock_irqrestore(&clockfw_lock, flags);
228 EXPORT_SYMBOL(clk_get_parent);
230 /*-------------------------------------------------------------------------
231 * OMAP specific clock functions shared between omap1 and omap2
232 *-------------------------------------------------------------------------*/
234 unsigned int __initdata mpurate;
237 * By default we use the rate set by the bootloader.
238 * You can override this with mpurate= cmdline option.
240 static int __init omap_clk_setup(char *str)
242 get_option(&str, &mpurate);
252 __setup("mpurate=", omap_clk_setup);
254 /* Used for clocks that always have same value as the parent clock */
255 void followparent_recalc(struct clk *clk)
257 if (clk == NULL || IS_ERR(clk))
260 clk->rate = clk->parent->rate;
261 if (unlikely(clk->flags & RATE_PROPAGATES))
265 /* Propagate rate to children */
266 void propagate_rate(struct clk * tclk)
270 if (tclk == NULL || IS_ERR(tclk))
273 list_for_each_entry(clkp, &clocks, node) {
274 if (likely(clkp->parent != tclk))
276 if (likely((u32)clkp->recalc))
282 * recalculate_root_clocks - recalculate and propagate all root clocks
284 * Recalculates all root clocks (clocks with no parent), which if the
285 * clock's .recalc is set correctly, should also propagate their rates.
288 void recalculate_root_clocks(void)
292 list_for_each_entry(clkp, &clocks, node) {
293 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
298 int clk_register(struct clk *clk)
300 if (clk == NULL || IS_ERR(clk))
303 mutex_lock(&clocks_mutex);
304 list_add(&clk->node, &clocks);
307 mutex_unlock(&clocks_mutex);
311 EXPORT_SYMBOL(clk_register);
313 void clk_unregister(struct clk *clk)
315 if (clk == NULL || IS_ERR(clk))
318 mutex_lock(&clocks_mutex);
319 list_del(&clk->node);
320 mutex_unlock(&clocks_mutex);
322 EXPORT_SYMBOL(clk_unregister);
324 void clk_deny_idle(struct clk *clk)
328 if (clk == NULL || IS_ERR(clk))
331 spin_lock_irqsave(&clockfw_lock, flags);
332 if (arch_clock->clk_deny_idle)
333 arch_clock->clk_deny_idle(clk);
334 spin_unlock_irqrestore(&clockfw_lock, flags);
336 EXPORT_SYMBOL(clk_deny_idle);
338 void clk_allow_idle(struct clk *clk)
342 if (clk == NULL || IS_ERR(clk))
345 spin_lock_irqsave(&clockfw_lock, flags);
346 if (arch_clock->clk_allow_idle)
347 arch_clock->clk_allow_idle(clk);
348 spin_unlock_irqrestore(&clockfw_lock, flags);
350 EXPORT_SYMBOL(clk_allow_idle);
352 void clk_enable_init_clocks(void)
356 list_for_each_entry(clkp, &clocks, node) {
357 if (clkp->flags & ENABLE_ON_INIT)
361 EXPORT_SYMBOL(clk_enable_init_clocks);
363 #ifdef CONFIG_CPU_FREQ
364 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
368 spin_lock_irqsave(&clockfw_lock, flags);
369 if (arch_clock->clk_init_cpufreq_table)
370 arch_clock->clk_init_cpufreq_table(table);
371 spin_unlock_irqrestore(&clockfw_lock, flags);
373 EXPORT_SYMBOL(clk_init_cpufreq_table);
376 /*-------------------------------------------------------------------------*/
378 #ifdef CONFIG_OMAP_RESET_CLOCKS
380 * Disable any unused clocks left on by the bootloader
382 static int __init clk_disable_unused(void)
387 list_for_each_entry(ck, &clocks, node) {
388 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
392 spin_lock_irqsave(&clockfw_lock, flags);
393 if (arch_clock->clk_disable_unused)
394 arch_clock->clk_disable_unused(ck);
395 spin_unlock_irqrestore(&clockfw_lock, flags);
400 late_initcall(clk_disable_unused);
403 int __init clk_init(struct clk_functions * custom_clocks)
405 if (!custom_clocks) {
406 printk(KERN_ERR "No custom clock functions registered\n");
410 arch_clock = custom_clocks;
415 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
417 * debugfs support to trace clock tree hierarchy and attributes
419 static struct dentry *clk_debugfs_root;
421 static int clk_debugfs_register_one(struct clk *c)
424 struct dentry *d, *child;
425 struct clk *pa = c->parent;
429 p += sprintf(p, "%s", c->name);
431 sprintf(p, ":%d", c->id);
432 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
437 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
442 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
447 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
456 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
457 debugfs_remove(child);
458 debugfs_remove(c->dent);
462 static int clk_debugfs_register(struct clk *c)
465 struct clk *pa = c->parent;
467 if (pa && !pa->dent) {
468 err = clk_debugfs_register(pa);
474 err = clk_debugfs_register_one(c);
481 static int __init clk_debugfs_init(void)
487 d = debugfs_create_dir("clock", NULL);
490 clk_debugfs_root = d;
492 list_for_each_entry(c, &clocks, node) {
493 err = clk_debugfs_register(c);
499 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
502 late_initcall(clk_debugfs_init);
504 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */