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>
26 #include <linux/bootmem.h>
27 #include <linux/slab.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;
38 * omap_clk_for_each_child - call callback on each child clock of clk
39 * @clk: struct clk * to use as the "parent"
40 * @parent_rate: rate of the parent of @clk to pass along
41 * @rate_storage: flag indicating whether current or temporary rates are used
42 * @cb: pointer to a callback function
44 * For each child clock of @clk, call the callback function @cb, passing
45 * along the contents of @parent_rate and @rate_storage. If the callback
46 * function returns non-zero, terminate the function and pass along the
49 static int omap_clk_for_each_child(struct clk *clk, unsigned long parent_rate,
51 int (*cb)(struct clk *clk,
52 unsigned long parent_rate,
55 struct clk_child *child;
58 list_for_each_entry(child, &clk->children, node) {
59 ret = (*cb)(child->clk, parent_rate, rate_storage);
68 * omap_clk_has_children - does clk @clk have any child clocks?
69 * @clk: struct clk * to test for child clocks
71 * If clock @clk has any child clocks, return 1; otherwise, return 0.
73 static int omap_clk_has_children(struct clk *clk)
75 return (list_empty(&clk->children)) ? 0 : 1;
79 * _do_propagate_rate - callback function for rate propagation
80 * @clk: struct clk * to recalc and propagate from
81 * @parent_rate: rate of the parent of @clk, to use in recalculation
82 * @rate_storage: flag indicating whether current or temporary rates are used
84 * If @clk has a recalc function, call it. If @clk has any children,
85 * propagate @clk's rate. Returns 0.
87 static int _do_propagate_rate(struct clk *clk, unsigned long parent_rate,
91 clk->recalc(clk, parent_rate, rate_storage);
92 if (omap_clk_has_children(clk))
93 propagate_rate(clk, rate_storage);
98 * omap_clk_add_child - add a child clock @clk2 to @clk
99 * @clk: parent struct clk *
100 * @clk2: new child struct clk *
102 * Add a child clock @clk2 to the list of children of parent clock
103 * @clk. Will potentially allocate memory from bootmem or, if
104 * available, from slab. Must only be called with the clock framework
105 * spinlock held. No return value.
107 void omap_clk_add_child(struct clk *clk, struct clk *clk2)
109 struct clk_child *child;
112 if (!clk->children.next)
113 INIT_LIST_HEAD(&clk->children);
115 list_for_each_entry(child, &clk->children, node) {
116 if (child->flags & CLK_CHILD_DELETED) {
118 child->flags &= ~CLK_CHILD_DELETED;
124 if (slab_is_available())
125 child = kmalloc(sizeof(struct clk_child), GFP_ATOMIC);
127 child = alloc_bootmem(sizeof(struct clk_child));
134 memset(child, 0, sizeof(struct clk_child));
136 if (slab_is_available())
137 child->flags |= CLK_CHILD_SLAB_ALLOC;
142 list_add_tail(&child->node, &clk->children);
146 * omap_clk_del_child - add a child clock @clk2 to @clk
147 * @clk: parent struct clk *
148 * @clk2: former child struct clk *
150 * Remove a child clock @clk2 from the list of children of parent
151 * clock @clk. Must only be called with the clock framework spinlock
152 * held. No return value.
154 void omap_clk_del_child(struct clk *clk, struct clk *clk2)
156 struct clk_child *child, *tmp;
158 /* Loop over all existing clk_childs, when found, deallocate */
159 list_for_each_entry_safe(child, tmp, &clk->children, node) {
160 if (child->clk == clk2) {
161 list_del(&child->node);
162 if (child->flags & CLK_CHILD_SLAB_ALLOC) {
166 child->flags |= CLK_CHILD_DELETED;
173 /*-------------------------------------------------------------------------
174 * Standard clock functions defined in include/linux/clk.h
175 *-------------------------------------------------------------------------*/
178 * Returns a clock. Note that we first try to use device id on the bus
179 * and clock name. If this fails, we try to use clock name only.
181 struct clk * clk_get(struct device *dev, const char *id)
183 struct clk *p, *clk = ERR_PTR(-ENOENT);
186 if (dev == NULL || dev->bus != &platform_bus_type)
189 idno = to_platform_device(dev)->id;
191 mutex_lock(&clocks_mutex);
193 list_for_each_entry(p, &clocks, node) {
194 if (p->id == idno && strcmp(id, p->name) == 0) {
200 list_for_each_entry(p, &clocks, node) {
201 if (strcmp(id, p->name) == 0) {
208 mutex_unlock(&clocks_mutex);
212 EXPORT_SYMBOL(clk_get);
214 int clk_enable(struct clk *clk)
219 if (clk == NULL || IS_ERR(clk))
222 spin_lock_irqsave(&clockfw_lock, flags);
223 if (arch_clock->clk_enable) {
224 ret = arch_clock->clk_enable(clk);
225 if (ret == 0 && clk->flags & RECALC_ON_ENABLE)
226 _do_propagate_rate(clk, clk->parent->rate,
230 spin_unlock_irqrestore(&clockfw_lock, flags);
234 EXPORT_SYMBOL(clk_enable);
236 void clk_disable(struct clk *clk)
240 if (clk == NULL || IS_ERR(clk))
243 spin_lock_irqsave(&clockfw_lock, flags);
244 if (clk->usecount == 0) {
245 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
251 if (arch_clock->clk_disable) {
252 arch_clock->clk_disable(clk);
253 if (clk->flags & RECALC_ON_ENABLE)
254 _do_propagate_rate(clk, clk->parent->rate,
259 spin_unlock_irqrestore(&clockfw_lock, flags);
261 EXPORT_SYMBOL(clk_disable);
263 unsigned long clk_get_rate(struct clk *clk)
266 unsigned long ret = 0;
268 if (clk == NULL || IS_ERR(clk))
271 spin_lock_irqsave(&clockfw_lock, flags);
273 spin_unlock_irqrestore(&clockfw_lock, flags);
277 EXPORT_SYMBOL(clk_get_rate);
279 void clk_put(struct clk *clk)
282 EXPORT_SYMBOL(clk_put);
284 /*-------------------------------------------------------------------------
285 * Optional clock functions defined in include/linux/clk.h
286 *-------------------------------------------------------------------------*/
288 long clk_round_rate(struct clk *clk, unsigned long rate)
293 if (clk == NULL || IS_ERR(clk))
296 spin_lock_irqsave(&clockfw_lock, flags);
297 if (arch_clock->clk_round_rate)
298 ret = arch_clock->clk_round_rate(clk, rate);
299 spin_unlock_irqrestore(&clockfw_lock, flags);
303 EXPORT_SYMBOL(clk_round_rate);
305 int clk_set_rate(struct clk *clk, unsigned long rate)
310 if (clk == NULL || IS_ERR(clk))
313 spin_lock_irqsave(&clockfw_lock, flags);
315 if (arch_clock->clk_set_rate) {
316 ret = arch_clock->clk_set_rate(clk, rate);
318 _do_propagate_rate(clk, clk->parent->rate,
322 spin_unlock_irqrestore(&clockfw_lock, flags);
326 EXPORT_SYMBOL(clk_set_rate);
328 int clk_set_parent(struct clk *clk, struct clk *parent)
331 struct clk *prev_parent;
334 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
337 spin_lock_irqsave(&clockfw_lock, flags);
339 if (arch_clock->clk_set_parent) {
340 prev_parent = clk->parent;
341 ret = arch_clock->clk_set_parent(clk, parent);
343 omap_clk_del_child(prev_parent, clk);
344 omap_clk_add_child(parent, clk);
345 _do_propagate_rate(clk, clk->parent->rate,
350 spin_unlock_irqrestore(&clockfw_lock, flags);
354 EXPORT_SYMBOL(clk_set_parent);
356 struct clk *clk_get_parent(struct clk *clk)
359 struct clk * ret = NULL;
361 if (clk == NULL || IS_ERR(clk))
364 spin_lock_irqsave(&clockfw_lock, flags);
365 if (arch_clock->clk_get_parent)
366 ret = arch_clock->clk_get_parent(clk);
367 spin_unlock_irqrestore(&clockfw_lock, flags);
371 EXPORT_SYMBOL(clk_get_parent);
373 /*-------------------------------------------------------------------------
374 * OMAP specific clock functions shared between omap1 and omap2
375 *-------------------------------------------------------------------------*/
377 unsigned int __initdata mpurate;
380 * By default we use the rate set by the bootloader.
381 * You can override this with mpurate= cmdline option.
383 static int __init omap_clk_setup(char *str)
385 get_option(&str, &mpurate);
395 __setup("mpurate=", omap_clk_setup);
397 /* Used for clocks that always have same value as the parent clock */
398 void followparent_recalc(struct clk *clk, unsigned long new_parent_rate,
401 if (rate_storage == CURRENT_RATE)
402 clk->rate = new_parent_rate;
403 else if (rate_storage == TEMP_RATE)
404 clk->temp_rate = new_parent_rate;
407 /* Propagate rate to children */
408 void propagate_rate(struct clk *tclk, u8 rate_storage)
410 unsigned long parent_rate = 0;
412 if (tclk == NULL || IS_ERR(tclk))
415 if (rate_storage == CURRENT_RATE)
416 parent_rate = tclk->rate;
417 else if (rate_storage == TEMP_RATE)
418 parent_rate = tclk->temp_rate;
420 omap_clk_for_each_child(tclk, parent_rate, rate_storage,
425 * recalculate_root_clocks - recalculate and propagate all root clocks
427 * Recalculates all root clocks (clocks with no parent), which if the
428 * clock's .recalc is set correctly, should also propagate their rates.
431 void recalculate_root_clocks(void)
435 list_for_each_entry(clkp, &clocks, node)
436 if (unlikely(!clkp->parent))
437 _do_propagate_rate(clkp, 0, CURRENT_RATE);
440 int clk_register(struct clk *clk)
444 if (clk == NULL || IS_ERR(clk))
447 mutex_lock(&clocks_mutex);
448 if (arch_clock->clk_register) {
449 ret = arch_clock->clk_register(clk);
453 list_add(&clk->node, &clocks);
454 if (!clk->children.next)
455 INIT_LIST_HEAD(&clk->children);
457 omap_clk_add_child(clk->parent, clk);
462 mutex_unlock(&clocks_mutex);
466 EXPORT_SYMBOL(clk_register);
468 void clk_unregister(struct clk *clk)
470 struct clk_child *child, *tmp;
472 if (clk == NULL || IS_ERR(clk))
475 mutex_lock(&clocks_mutex);
476 list_del(&clk->node);
478 omap_clk_del_child(clk->parent, clk);
479 list_for_each_entry_safe(child, tmp, &clk->children, node)
480 if (child->flags & CLK_CHILD_SLAB_ALLOC)
482 mutex_unlock(&clocks_mutex);
484 EXPORT_SYMBOL(clk_unregister);
486 void clk_deny_idle(struct clk *clk)
490 if (clk == NULL || IS_ERR(clk))
493 spin_lock_irqsave(&clockfw_lock, flags);
494 if (arch_clock->clk_deny_idle)
495 arch_clock->clk_deny_idle(clk);
496 spin_unlock_irqrestore(&clockfw_lock, flags);
498 EXPORT_SYMBOL(clk_deny_idle);
500 void clk_allow_idle(struct clk *clk)
504 if (clk == NULL || IS_ERR(clk))
507 spin_lock_irqsave(&clockfw_lock, flags);
508 if (arch_clock->clk_allow_idle)
509 arch_clock->clk_allow_idle(clk);
510 spin_unlock_irqrestore(&clockfw_lock, flags);
512 EXPORT_SYMBOL(clk_allow_idle);
514 void clk_enable_init_clocks(void)
518 list_for_each_entry(clkp, &clocks, node) {
519 if (clkp->flags & ENABLE_ON_INIT)
523 EXPORT_SYMBOL(clk_enable_init_clocks);
525 #ifdef CONFIG_CPU_FREQ
526 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
530 spin_lock_irqsave(&clockfw_lock, flags);
531 if (arch_clock->clk_init_cpufreq_table)
532 arch_clock->clk_init_cpufreq_table(table);
533 spin_unlock_irqrestore(&clockfw_lock, flags);
535 EXPORT_SYMBOL(clk_init_cpufreq_table);
538 /*-------------------------------------------------------------------------*/
540 #ifdef CONFIG_OMAP_RESET_CLOCKS
542 * Disable any unused clocks left on by the bootloader
544 static int __init clk_disable_unused(void)
549 list_for_each_entry(ck, &clocks, node) {
550 if (ck->usecount > 0 ||
551 (ck->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK)))
554 if (cpu_class_is_omap1() && ck->enable_reg == 0)
557 spin_lock_irqsave(&clockfw_lock, flags);
558 if (arch_clock->clk_disable_unused)
559 arch_clock->clk_disable_unused(ck);
560 spin_unlock_irqrestore(&clockfw_lock, flags);
565 late_initcall(clk_disable_unused);
568 int __init clk_init(struct clk_functions * custom_clocks)
570 if (!custom_clocks) {
571 printk(KERN_ERR "No custom clock functions registered\n");
575 arch_clock = custom_clocks;
580 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
582 * debugfs support to trace clock tree hierarchy and attributes
584 static struct dentry *clk_debugfs_root;
586 static int clk_debugfs_register_one(struct clk *c)
589 struct dentry *d, *child;
590 struct clk *pa = c->parent;
594 p += sprintf(p, "%s", c->name);
596 sprintf(p, ":%d", c->id);
597 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
602 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
607 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
612 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
621 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
622 debugfs_remove(child);
623 debugfs_remove(c->dent);
627 static int clk_debugfs_register(struct clk *c)
630 struct clk *pa = c->parent;
632 if (pa && !pa->dent) {
633 err = clk_debugfs_register(pa);
639 err = clk_debugfs_register_one(c);
646 static int __init clk_debugfs_init(void)
652 d = debugfs_create_dir("clock", NULL);
655 clk_debugfs_root = d;
657 list_for_each_entry(c, &clocks, node) {
658 err = clk_debugfs_register(c);
664 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
667 late_initcall(clk_debugfs_init);
669 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */