2 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
4 * Copyright (C) 2005 - 2009 Paul Mundt
6 * This clock framework is derived from the OMAP version by:
8 * Copyright (C) 2004 - 2008 Nokia Corporation
9 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
11 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
15 * Copyright (C) 2008 Russell King.
17 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/list.h>
26 #include <linux/kobject.h>
27 #include <linux/sysdev.h>
28 #include <linux/seq_file.h>
29 #include <linux/err.h>
30 #include <linux/platform_device.h>
31 #include <linux/debugfs.h>
32 #include <asm/clock.h>
33 #include <asm/machvec.h>
35 static LIST_HEAD(clock_list);
36 static DEFINE_SPINLOCK(clock_lock);
37 static DEFINE_MUTEX(clock_list_sem);
39 /* Used for clocks that always have same value as the parent clock */
40 unsigned long followparent_recalc(struct clk *clk)
42 return clk->parent->rate;
45 int clk_reparent(struct clk *child, struct clk *parent)
47 list_del_init(&child->sibling);
49 list_add(&child->sibling, &parent->children);
50 child->parent = parent;
52 /* now do the debugfs renaming to reattach the child
53 to the proper parent */
58 /* Propagate rate to children */
59 void propagate_rate(struct clk *tclk)
63 list_for_each_entry(clkp, &tclk->children, sibling) {
64 if (clkp->ops && clkp->ops->recalc)
65 clkp->rate = clkp->ops->recalc(clkp);
66 if (clkp->ops && clkp->ops->build_rate_table)
67 clkp->ops->build_rate_table(clkp);
73 static void __clk_disable(struct clk *clk)
75 if (clk->usecount == 0) {
76 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
82 if (!(--clk->usecount)) {
83 if (likely(clk->ops && clk->ops->disable))
84 clk->ops->disable(clk);
85 if (likely(clk->parent))
86 __clk_disable(clk->parent);
90 void clk_disable(struct clk *clk)
97 spin_lock_irqsave(&clock_lock, flags);
99 spin_unlock_irqrestore(&clock_lock, flags);
101 EXPORT_SYMBOL_GPL(clk_disable);
103 static int __clk_enable(struct clk *clk)
107 if (clk->usecount++ == 0) {
109 ret = __clk_enable(clk->parent);
114 if (clk->ops && clk->ops->enable) {
115 ret = clk->ops->enable(clk);
118 __clk_disable(clk->parent);
130 int clk_enable(struct clk *clk)
138 spin_lock_irqsave(&clock_lock, flags);
139 ret = __clk_enable(clk);
140 spin_unlock_irqrestore(&clock_lock, flags);
144 EXPORT_SYMBOL_GPL(clk_enable);
146 static LIST_HEAD(root_clks);
149 * recalculate_root_clocks - recalculate and propagate all root clocks
151 * Recalculates all root clocks (clocks with no parent), which if the
152 * clock's .recalc is set correctly, should also propagate their rates.
155 void recalculate_root_clocks(void)
159 list_for_each_entry(clkp, &root_clks, sibling) {
160 if (clkp->ops && clkp->ops->recalc)
161 clkp->rate = clkp->ops->recalc(clkp);
162 propagate_rate(clkp);
166 int clk_register(struct clk *clk)
168 if (clk == NULL || IS_ERR(clk))
172 * trap out already registered clocks
174 if (clk->node.next || clk->node.prev)
177 mutex_lock(&clock_list_sem);
179 INIT_LIST_HEAD(&clk->children);
183 list_add(&clk->sibling, &clk->parent->children);
185 list_add(&clk->sibling, &root_clks);
187 list_add(&clk->node, &clock_list);
188 if (clk->ops && clk->ops->init)
190 mutex_unlock(&clock_list_sem);
194 EXPORT_SYMBOL_GPL(clk_register);
196 void clk_unregister(struct clk *clk)
198 mutex_lock(&clock_list_sem);
199 list_del(&clk->sibling);
200 list_del(&clk->node);
201 mutex_unlock(&clock_list_sem);
203 EXPORT_SYMBOL_GPL(clk_unregister);
205 static void clk_enable_init_clocks(void)
209 list_for_each_entry(clkp, &clock_list, node)
210 if (clkp->flags & CLK_ENABLE_ON_INIT)
214 unsigned long clk_get_rate(struct clk *clk)
218 EXPORT_SYMBOL_GPL(clk_get_rate);
220 int clk_set_rate(struct clk *clk, unsigned long rate)
222 return clk_set_rate_ex(clk, rate, 0);
224 EXPORT_SYMBOL_GPL(clk_set_rate);
226 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
228 int ret = -EOPNOTSUPP;
231 spin_lock_irqsave(&clock_lock, flags);
233 if (likely(clk->ops && clk->ops->set_rate)) {
234 ret = clk->ops->set_rate(clk, rate, algo_id);
242 if (clk->ops && clk->ops->recalc)
243 clk->rate = clk->ops->recalc(clk);
248 spin_unlock_irqrestore(&clock_lock, flags);
252 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
254 int clk_set_parent(struct clk *clk, struct clk *parent)
261 if (clk->parent == parent)
264 spin_lock_irqsave(&clock_lock, flags);
265 if (clk->usecount == 0) {
266 if (clk->ops->set_parent)
267 ret = clk->ops->set_parent(clk, parent);
269 ret = clk_reparent(clk, parent);
272 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
273 clk->name, clk->parent->name, clk->rate);
274 if (clk->ops->recalc)
275 clk->rate = clk->ops->recalc(clk);
280 spin_unlock_irqrestore(&clock_lock, flags);
284 EXPORT_SYMBOL_GPL(clk_set_parent);
286 struct clk *clk_get_parent(struct clk *clk)
290 EXPORT_SYMBOL_GPL(clk_get_parent);
292 long clk_round_rate(struct clk *clk, unsigned long rate)
294 if (likely(clk->ops && clk->ops->round_rate)) {
295 unsigned long flags, rounded;
297 spin_lock_irqsave(&clock_lock, flags);
298 rounded = clk->ops->round_rate(clk, rate);
299 spin_unlock_irqrestore(&clock_lock, flags);
304 return clk_get_rate(clk);
306 EXPORT_SYMBOL_GPL(clk_round_rate);
309 * Find the correct struct clk for the device and connection ID.
310 * We do slightly fuzzy matching here:
311 * An entry with a NULL ID is assumed to be a wildcard.
312 * If an entry has a device ID, it must match
313 * If an entry has a connection ID, it must match
314 * Then we take the most specific entry - with the following
315 * order of precidence: dev+con > dev only > con only.
317 static struct clk *clk_find(const char *dev_id, const char *con_id)
319 struct clk_lookup *p;
320 struct clk *clk = NULL;
323 list_for_each_entry(p, &clock_list, node) {
326 if (!dev_id || strcmp(p->dev_id, dev_id))
331 if (!con_id || strcmp(p->con_id, con_id))
346 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
350 mutex_lock(&clock_list_sem);
351 clk = clk_find(dev_id, con_id);
352 mutex_unlock(&clock_list_sem);
354 return clk ? clk : ERR_PTR(-ENOENT);
356 EXPORT_SYMBOL_GPL(clk_get_sys);
359 * Returns a clock. Note that we first try to use device id on the bus
360 * and clock name. If this fails, we try to use clock name only.
362 struct clk *clk_get(struct device *dev, const char *id)
364 const char *dev_id = dev ? dev_name(dev) : NULL;
365 struct clk *p, *clk = ERR_PTR(-ENOENT);
368 clk = clk_get_sys(dev_id, id);
369 if (clk && !IS_ERR(clk))
372 if (dev == NULL || dev->bus != &platform_bus_type)
375 idno = to_platform_device(dev)->id;
377 mutex_lock(&clock_list_sem);
378 list_for_each_entry(p, &clock_list, node) {
380 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
386 list_for_each_entry(p, &clock_list, node) {
387 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
394 mutex_unlock(&clock_list_sem);
398 EXPORT_SYMBOL_GPL(clk_get);
400 void clk_put(struct clk *clk)
402 if (clk && !IS_ERR(clk))
403 module_put(clk->owner);
405 EXPORT_SYMBOL_GPL(clk_put);
408 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
410 static pm_message_t prev_state;
413 switch (state.event) {
415 /* Resumeing from hibernation */
416 if (prev_state.event != PM_EVENT_FREEZE)
419 list_for_each_entry(clkp, &clock_list, node) {
420 if (likely(clkp->ops)) {
421 unsigned long rate = clkp->rate;
423 if (likely(clkp->ops->set_parent))
424 clkp->ops->set_parent(clkp,
426 if (likely(clkp->ops->set_rate))
427 clkp->ops->set_rate(clkp,
429 else if (likely(clkp->ops->recalc))
430 clkp->rate = clkp->ops->recalc(clkp);
434 case PM_EVENT_FREEZE:
436 case PM_EVENT_SUSPEND:
444 static int clks_sysdev_resume(struct sys_device *dev)
446 return clks_sysdev_suspend(dev, PMSG_ON);
449 static struct sysdev_class clks_sysdev_class = {
453 static struct sysdev_driver clks_sysdev_driver = {
454 .suspend = clks_sysdev_suspend,
455 .resume = clks_sysdev_resume,
458 static struct sys_device clks_sysdev_dev = {
459 .cls = &clks_sysdev_class,
462 static int __init clk_sysdev_init(void)
464 sysdev_class_register(&clks_sysdev_class);
465 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
466 sysdev_register(&clks_sysdev_dev);
470 subsys_initcall(clk_sysdev_init);
473 int __init clk_init(void)
477 ret = arch_clk_init();
479 pr_err("%s: CPU clock registration failed.\n", __func__);
483 if (sh_mv.mv_clk_init) {
484 ret = sh_mv.mv_clk_init();
486 pr_err("%s: machvec clock initialization failed.\n",
492 /* Kick the child clocks.. */
493 recalculate_root_clocks();
495 /* Enable the necessary init clocks */
496 clk_enable_init_clocks();
502 * debugfs support to trace clock tree hierarchy and attributes
504 static struct dentry *clk_debugfs_root;
506 static int clk_debugfs_register_one(struct clk *c)
509 struct dentry *d, *child;
510 struct clk *pa = c->parent;
514 p += sprintf(p, "%s", c->name);
516 sprintf(p, ":%d", c->id);
517 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
522 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
527 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
532 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
541 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
542 debugfs_remove(child);
543 debugfs_remove(c->dentry);
547 static int clk_debugfs_register(struct clk *c)
550 struct clk *pa = c->parent;
552 if (pa && !pa->dentry) {
553 err = clk_debugfs_register(pa);
559 err = clk_debugfs_register_one(c);
566 static int __init clk_debugfs_init(void)
572 d = debugfs_create_dir("clock", NULL);
575 clk_debugfs_root = d;
577 list_for_each_entry(c, &clock_list, node) {
578 err = clk_debugfs_register(c);
584 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
587 late_initcall(clk_debugfs_init);