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>
28 #include <asm/semaphore.h>
30 #include <asm/arch/clock.h>
32 static LIST_HEAD(clocks);
33 static DEFINE_MUTEX(clocks_mutex);
34 static DEFINE_SPINLOCK(clockfw_lock);
36 static struct clk_functions *arch_clock;
38 /*-------------------------------------------------------------------------
39 * Standard clock functions defined in include/linux/clk.h
40 *-------------------------------------------------------------------------*/
43 * Returns a clock. Note that we first try to use device id on the bus
44 * and clock name. If this fails, we try to use clock name only.
46 struct clk * clk_get(struct device *dev, const char *id)
48 struct clk *p, *clk = ERR_PTR(-ENOENT);
51 if (dev == NULL || dev->bus != &platform_bus_type)
54 idno = to_platform_device(dev)->id;
56 mutex_lock(&clocks_mutex);
58 list_for_each_entry(p, &clocks, node) {
60 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
66 list_for_each_entry(p, &clocks, node) {
67 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
74 mutex_unlock(&clocks_mutex);
78 EXPORT_SYMBOL(clk_get);
80 int clk_enable(struct clk *clk)
85 if (clk == NULL || IS_ERR(clk))
88 spin_lock_irqsave(&clockfw_lock, flags);
89 if (arch_clock->clk_enable)
90 ret = arch_clock->clk_enable(clk);
91 spin_unlock_irqrestore(&clockfw_lock, flags);
95 EXPORT_SYMBOL(clk_enable);
97 void clk_disable(struct clk *clk)
101 if (clk == NULL || IS_ERR(clk))
104 spin_lock_irqsave(&clockfw_lock, flags);
105 if (clk->usecount == 0) {
106 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
112 if (arch_clock->clk_disable)
113 arch_clock->clk_disable(clk);
116 spin_unlock_irqrestore(&clockfw_lock, flags);
118 EXPORT_SYMBOL(clk_disable);
120 int clk_get_usecount(struct clk *clk)
125 if (clk == NULL || IS_ERR(clk))
128 spin_lock_irqsave(&clockfw_lock, flags);
130 spin_unlock_irqrestore(&clockfw_lock, flags);
134 EXPORT_SYMBOL(clk_get_usecount);
136 unsigned long clk_get_rate(struct clk *clk)
139 unsigned long ret = 0;
141 if (clk == NULL || IS_ERR(clk))
144 spin_lock_irqsave(&clockfw_lock, flags);
146 spin_unlock_irqrestore(&clockfw_lock, flags);
150 EXPORT_SYMBOL(clk_get_rate);
152 void clk_put(struct clk *clk)
154 if (clk && !IS_ERR(clk))
155 module_put(clk->owner);
157 EXPORT_SYMBOL(clk_put);
159 /*-------------------------------------------------------------------------
160 * Optional clock functions defined in include/linux/clk.h
161 *-------------------------------------------------------------------------*/
163 long clk_round_rate(struct clk *clk, unsigned long rate)
168 if (clk == NULL || IS_ERR(clk))
171 spin_lock_irqsave(&clockfw_lock, flags);
172 if (arch_clock->clk_round_rate)
173 ret = arch_clock->clk_round_rate(clk, rate);
174 spin_unlock_irqrestore(&clockfw_lock, flags);
178 EXPORT_SYMBOL(clk_round_rate);
180 int clk_set_rate(struct clk *clk, unsigned long rate)
185 if (clk == NULL || IS_ERR(clk))
188 spin_lock_irqsave(&clockfw_lock, flags);
189 if (arch_clock->clk_set_rate)
190 ret = arch_clock->clk_set_rate(clk, rate);
191 spin_unlock_irqrestore(&clockfw_lock, flags);
195 EXPORT_SYMBOL(clk_set_rate);
197 int clk_set_parent(struct clk *clk, struct clk *parent)
202 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
205 spin_lock_irqsave(&clockfw_lock, flags);
206 if (arch_clock->clk_set_parent)
207 ret = arch_clock->clk_set_parent(clk, parent);
208 spin_unlock_irqrestore(&clockfw_lock, flags);
212 EXPORT_SYMBOL(clk_set_parent);
214 struct clk *clk_get_parent(struct clk *clk)
217 struct clk * ret = NULL;
219 if (clk == NULL || IS_ERR(clk))
222 spin_lock_irqsave(&clockfw_lock, flags);
223 if (arch_clock->clk_get_parent)
224 ret = arch_clock->clk_get_parent(clk);
225 spin_unlock_irqrestore(&clockfw_lock, flags);
229 EXPORT_SYMBOL(clk_get_parent);
231 /*-------------------------------------------------------------------------
232 * OMAP specific clock functions shared between omap1 and omap2
233 *-------------------------------------------------------------------------*/
235 unsigned int __initdata mpurate;
238 * By default we use the rate set by the bootloader.
239 * You can override this with mpurate= cmdline option.
241 static int __init omap_clk_setup(char *str)
243 get_option(&str, &mpurate);
253 __setup("mpurate=", omap_clk_setup);
255 /* Used for clocks that always have same value as the parent clock */
256 void followparent_recalc(struct clk *clk)
258 if (clk == NULL || IS_ERR(clk))
261 clk->rate = clk->parent->rate;
262 if (unlikely(clk->flags & RATE_PROPAGATES))
266 /* Propagate rate to children */
267 void propagate_rate(struct clk * tclk)
271 if (tclk == NULL || IS_ERR(tclk))
274 list_for_each_entry(clkp, &clocks, node) {
275 if (likely(clkp->parent != tclk))
277 if (likely((u32)clkp->recalc))
283 * recalculate_root_clocks - recalculate and propagate all root clocks
285 * Recalculates all root clocks (clocks with no parent), which if the
286 * clock's .recalc is set correctly, should also propagate their rates.
289 void recalculate_root_clocks(void)
293 list_for_each_entry(clkp, &clocks, node) {
294 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
299 int clk_register(struct clk *clk)
301 if (clk == NULL || IS_ERR(clk))
304 mutex_lock(&clocks_mutex);
305 list_add(&clk->node, &clocks);
308 mutex_unlock(&clocks_mutex);
312 EXPORT_SYMBOL(clk_register);
314 void clk_unregister(struct clk *clk)
316 if (clk == NULL || IS_ERR(clk))
319 mutex_lock(&clocks_mutex);
320 list_del(&clk->node);
321 mutex_unlock(&clocks_mutex);
323 EXPORT_SYMBOL(clk_unregister);
325 void clk_deny_idle(struct clk *clk)
329 if (clk == NULL || IS_ERR(clk))
332 spin_lock_irqsave(&clockfw_lock, flags);
333 if (arch_clock->clk_deny_idle)
334 arch_clock->clk_deny_idle(clk);
335 spin_unlock_irqrestore(&clockfw_lock, flags);
337 EXPORT_SYMBOL(clk_deny_idle);
339 void clk_allow_idle(struct clk *clk)
343 if (clk == NULL || IS_ERR(clk))
346 spin_lock_irqsave(&clockfw_lock, flags);
347 if (arch_clock->clk_allow_idle)
348 arch_clock->clk_allow_idle(clk);
349 spin_unlock_irqrestore(&clockfw_lock, flags);
351 EXPORT_SYMBOL(clk_allow_idle);
353 void clk_enable_init_clocks(void)
357 list_for_each_entry(clkp, &clocks, node) {
358 if (clkp->flags & ENABLE_ON_INIT)
362 EXPORT_SYMBOL(clk_enable_init_clocks);
364 #ifdef CONFIG_CPU_FREQ
365 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
369 spin_lock_irqsave(&clockfw_lock, flags);
370 if (arch_clock->clk_init_cpufreq_table)
371 arch_clock->clk_init_cpufreq_table(table);
372 spin_unlock_irqrestore(&clockfw_lock, flags);
374 EXPORT_SYMBOL(clk_init_cpufreq_table);
377 /*-------------------------------------------------------------------------*/
379 #ifdef CONFIG_OMAP_RESET_CLOCKS
381 * Disable any unused clocks left on by the bootloader
383 static int __init clk_disable_unused(void)
388 list_for_each_entry(ck, &clocks, node) {
389 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
393 spin_lock_irqsave(&clockfw_lock, flags);
394 if (arch_clock->clk_disable_unused)
395 arch_clock->clk_disable_unused(ck);
396 spin_unlock_irqrestore(&clockfw_lock, flags);
401 late_initcall(clk_disable_unused);
404 int __init clk_init(struct clk_functions * custom_clocks)
406 if (!custom_clocks) {
407 printk(KERN_ERR "No custom clock functions registered\n");
411 arch_clock = custom_clocks;
416 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
418 * debugfs support to trace clock tree hierarchy and attributes
420 static struct dentry *clk_debugfs_root;
422 static int clk_debugfs_register_one(struct clk *c)
425 struct dentry *d, *child;
426 struct clk *pa = c->parent;
430 p += sprintf(p, "%s", c->name);
432 sprintf(p, ":%d", c->id);
433 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
438 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
443 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
448 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
457 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
458 debugfs_remove(child);
459 debugfs_remove(c->dent);
463 static int clk_debugfs_register(struct clk *c)
466 struct clk *pa = c->parent;
468 if (pa && !pa->dent) {
469 err = clk_debugfs_register(pa);
475 err = clk_debugfs_register_one(c);
482 static int __init clk_debugfs_init(void)
488 d = debugfs_create_dir("clock", NULL);
491 clk_debugfs_root = d;
493 list_for_each_entry(c, &clocks, node) {
494 err = clk_debugfs_register(c);
500 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
503 late_initcall(clk_debugfs_init);
505 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */