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/debugfs.h>
27 #include <asm/semaphore.h>
29 #include <asm/arch/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 #ifdef CONFIG_PM_DEBUG
39 static void print_parents(struct clk *clk)
44 list_for_each_entry(p, &clocks, node) {
45 if (p->parent == clk && p->usecount) {
46 if (!clk->usecount && !printed) {
47 printk("MISMATCH: %s\n", clk->name);
50 printk("\t%-15s\n", p->name);
55 void clk_print_usecounts(void)
60 spin_lock_irqsave(&clockfw_lock, flags);
61 list_for_each_entry(p, &clocks, node) {
63 printk("%-15s: %d\n", p->name, p->usecount);
67 spin_unlock_irqrestore(&clockfw_lock, flags);
72 /*-------------------------------------------------------------------------
73 * Standard clock functions defined in include/linux/clk.h
74 *-------------------------------------------------------------------------*/
77 * Returns a clock. Note that we first try to use device id on the bus
78 * and clock name. If this fails, we try to use clock name only.
80 struct clk * clk_get(struct device *dev, const char *id)
82 struct clk *p, *clk = ERR_PTR(-ENOENT);
85 if (dev == NULL || dev->bus != &platform_bus_type)
88 idno = to_platform_device(dev)->id;
90 mutex_lock(&clocks_mutex);
92 list_for_each_entry(p, &clocks, node) {
94 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
100 list_for_each_entry(p, &clocks, node) {
101 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
108 mutex_unlock(&clocks_mutex);
112 EXPORT_SYMBOL(clk_get);
114 int clk_enable(struct clk *clk)
119 if (clk == NULL || IS_ERR(clk))
122 spin_lock_irqsave(&clockfw_lock, flags);
123 if (arch_clock->clk_enable)
124 ret = arch_clock->clk_enable(clk);
125 spin_unlock_irqrestore(&clockfw_lock, flags);
129 EXPORT_SYMBOL(clk_enable);
131 void clk_disable(struct clk *clk)
135 if (clk == NULL || IS_ERR(clk))
138 spin_lock_irqsave(&clockfw_lock, flags);
139 if (clk->usecount == 0) {
140 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
146 if (arch_clock->clk_disable)
147 arch_clock->clk_disable(clk);
150 spin_unlock_irqrestore(&clockfw_lock, flags);
152 EXPORT_SYMBOL(clk_disable);
154 int clk_get_usecount(struct clk *clk)
159 if (clk == NULL || IS_ERR(clk))
162 spin_lock_irqsave(&clockfw_lock, flags);
164 spin_unlock_irqrestore(&clockfw_lock, flags);
168 EXPORT_SYMBOL(clk_get_usecount);
170 unsigned long clk_get_rate(struct clk *clk)
173 unsigned long ret = 0;
175 if (clk == NULL || IS_ERR(clk))
178 spin_lock_irqsave(&clockfw_lock, flags);
180 spin_unlock_irqrestore(&clockfw_lock, flags);
184 EXPORT_SYMBOL(clk_get_rate);
186 void clk_put(struct clk *clk)
188 if (clk && !IS_ERR(clk))
189 module_put(clk->owner);
191 EXPORT_SYMBOL(clk_put);
193 /*-------------------------------------------------------------------------
194 * Optional clock functions defined in include/linux/clk.h
195 *-------------------------------------------------------------------------*/
197 long clk_round_rate(struct clk *clk, unsigned long rate)
202 if (clk == NULL || IS_ERR(clk))
205 spin_lock_irqsave(&clockfw_lock, flags);
206 if (arch_clock->clk_round_rate)
207 ret = arch_clock->clk_round_rate(clk, rate);
208 spin_unlock_irqrestore(&clockfw_lock, flags);
212 EXPORT_SYMBOL(clk_round_rate);
214 int clk_set_rate(struct clk *clk, unsigned long rate)
219 if (clk == NULL || IS_ERR(clk))
222 spin_lock_irqsave(&clockfw_lock, flags);
223 if (arch_clock->clk_set_rate)
224 ret = arch_clock->clk_set_rate(clk, rate);
225 spin_unlock_irqrestore(&clockfw_lock, flags);
229 EXPORT_SYMBOL(clk_set_rate);
231 int clk_set_parent(struct clk *clk, struct clk *parent)
236 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
239 spin_lock_irqsave(&clockfw_lock, flags);
240 if (arch_clock->clk_set_parent)
241 ret = arch_clock->clk_set_parent(clk, parent);
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)
292 if (clk == NULL || IS_ERR(clk))
295 clk->rate = clk->parent->rate;
296 if (unlikely(clk->flags & RATE_PROPAGATES))
300 /* Propagate rate to children */
301 void propagate_rate(struct clk * tclk)
305 if (tclk == NULL || IS_ERR(tclk))
308 list_for_each_entry(clkp, &clocks, node) {
309 if (likely(clkp->parent != tclk))
311 if (likely((u32)clkp->recalc))
317 * recalculate_root_clocks - recalculate and propagate all root clocks
319 * Recalculates all root clocks (clocks with no parent), which if the
320 * clock's .recalc is set correctly, should also propagate their rates.
323 void recalculate_root_clocks(void)
327 list_for_each_entry(clkp, &clocks, node) {
328 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
333 int clk_register(struct clk *clk)
335 if (clk == NULL || IS_ERR(clk))
338 mutex_lock(&clocks_mutex);
339 list_add(&clk->node, &clocks);
342 mutex_unlock(&clocks_mutex);
346 EXPORT_SYMBOL(clk_register);
348 void clk_unregister(struct clk *clk)
350 if (clk == NULL || IS_ERR(clk))
353 mutex_lock(&clocks_mutex);
354 list_del(&clk->node);
355 mutex_unlock(&clocks_mutex);
357 EXPORT_SYMBOL(clk_unregister);
359 void clk_deny_idle(struct clk *clk)
363 if (clk == NULL || IS_ERR(clk))
366 spin_lock_irqsave(&clockfw_lock, flags);
367 if (arch_clock->clk_deny_idle)
368 arch_clock->clk_deny_idle(clk);
369 spin_unlock_irqrestore(&clockfw_lock, flags);
371 EXPORT_SYMBOL(clk_deny_idle);
373 void clk_allow_idle(struct clk *clk)
377 if (clk == NULL || IS_ERR(clk))
380 spin_lock_irqsave(&clockfw_lock, flags);
381 if (arch_clock->clk_allow_idle)
382 arch_clock->clk_allow_idle(clk);
383 spin_unlock_irqrestore(&clockfw_lock, flags);
385 EXPORT_SYMBOL(clk_allow_idle);
387 void clk_enable_init_clocks(void)
391 list_for_each_entry(clkp, &clocks, node) {
392 if (clkp->flags & ENABLE_ON_INIT)
396 EXPORT_SYMBOL(clk_enable_init_clocks);
398 #ifdef CONFIG_CPU_FREQ
399 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
403 spin_lock_irqsave(&clockfw_lock, flags);
404 if (arch_clock->clk_init_cpufreq_table)
405 arch_clock->clk_init_cpufreq_table(table);
406 spin_unlock_irqrestore(&clockfw_lock, flags);
408 EXPORT_SYMBOL(clk_init_cpufreq_table);
411 /*-------------------------------------------------------------------------*/
413 #ifdef CONFIG_OMAP_RESET_CLOCKS
415 * Disable any unused clocks left on by the bootloader
417 static int __init clk_disable_unused(void)
422 list_for_each_entry(ck, &clocks, node) {
423 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
427 spin_lock_irqsave(&clockfw_lock, flags);
428 if (arch_clock->clk_disable_unused)
429 arch_clock->clk_disable_unused(ck);
430 spin_unlock_irqrestore(&clockfw_lock, flags);
435 late_initcall(clk_disable_unused);
438 int __init clk_init(struct clk_functions * custom_clocks)
440 if (!custom_clocks) {
441 printk(KERN_ERR "No custom clock functions registered\n");
445 arch_clock = custom_clocks;
450 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_PROC_FS)
451 #include <linux/proc_fs.h>
452 #include <linux/seq_file.h>
454 static void *omap_ck_start(struct seq_file *m, loff_t *pos)
456 return *pos < 1 ? (void *)1 : NULL;
459 static void *omap_ck_next(struct seq_file *m, void *v, loff_t *pos)
465 static void omap_ck_stop(struct seq_file *m, void *v)
469 int omap_ck_show(struct seq_file *m, void *v)
473 list_for_each_entry(cp, &clocks, node)
474 seq_printf(m,"%s %ld %d\n", cp->name, cp->rate, cp->usecount);
479 static struct seq_operations omap_ck_op = {
480 .start = omap_ck_start,
481 .next = omap_ck_next,
482 .stop = omap_ck_stop,
486 static int omap_ck_open(struct inode *inode, struct file *file)
488 return seq_open(file, &omap_ck_op);
491 static struct file_operations proc_omap_ck_operations = {
492 .open = omap_ck_open,
495 .release = seq_release,
498 int __init omap_ck_init(void)
500 struct proc_dir_entry *entry;
502 entry = create_proc_entry("omap_clocks", 0, NULL);
504 entry->proc_fops = &proc_omap_ck_operations;
508 __initcall(omap_ck_init);
511 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
513 * debugfs support to trace clock tree hierarchy and attributes
515 static struct dentry *clk_debugfs_root;
517 static int clk_debugfs_register_one(struct clk *c)
520 struct dentry *d, *child;
521 struct clk *pa = c->parent;
525 p += sprintf(p, "%s", c->name);
527 sprintf(p, ":%d", c->id);
528 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
533 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
538 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
543 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
552 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
553 debugfs_remove(child);
554 debugfs_remove(c->dent);
558 static int clk_debugfs_register(struct clk *c)
561 struct clk *pa = c->parent;
563 if (pa && !pa->dent) {
564 err = clk_debugfs_register(pa);
570 err = clk_debugfs_register_one(c);
577 static int __init clk_debugfs_init(void)
583 d = debugfs_create_dir("clock", NULL);
586 clk_debugfs_root = d;
588 list_for_each_entry(c, &clocks, node) {
589 err = clk_debugfs_register(c);
595 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
598 late_initcall(clk_debugfs_init);
600 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */