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/proc_fs.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 show_clocks(char *buf, char **start, off_t off,
409 int len, int *eof, void *data)
414 list_for_each_entry_reverse(clk, &clock_list, node) {
415 unsigned long rate = clk_get_rate(clk);
417 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
418 rate / 1000000, (rate % 1000000) / 10000,
419 (clk->usecount > 0) ? "enabled" : "disabled");
426 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
428 static pm_message_t prev_state;
431 switch (state.event) {
433 /* Resumeing from hibernation */
434 if (prev_state.event != PM_EVENT_FREEZE)
437 list_for_each_entry(clkp, &clock_list, node) {
438 if (likely(clkp->ops)) {
439 unsigned long rate = clkp->rate;
441 if (likely(clkp->ops->set_parent))
442 clkp->ops->set_parent(clkp,
444 if (likely(clkp->ops->set_rate))
445 clkp->ops->set_rate(clkp,
447 else if (likely(clkp->ops->recalc))
448 clkp->rate = clkp->ops->recalc(clkp);
452 case PM_EVENT_FREEZE:
454 case PM_EVENT_SUSPEND:
462 static int clks_sysdev_resume(struct sys_device *dev)
464 return clks_sysdev_suspend(dev, PMSG_ON);
467 static struct sysdev_class clks_sysdev_class = {
471 static struct sysdev_driver clks_sysdev_driver = {
472 .suspend = clks_sysdev_suspend,
473 .resume = clks_sysdev_resume,
476 static struct sys_device clks_sysdev_dev = {
477 .cls = &clks_sysdev_class,
480 static int __init clk_sysdev_init(void)
482 sysdev_class_register(&clks_sysdev_class);
483 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
484 sysdev_register(&clks_sysdev_dev);
488 subsys_initcall(clk_sysdev_init);
491 int __init clk_init(void)
495 ret = arch_clk_init();
497 pr_err("%s: CPU clock registration failed.\n", __func__);
501 if (sh_mv.mv_clk_init) {
502 ret = sh_mv.mv_clk_init();
504 pr_err("%s: machvec clock initialization failed.\n",
510 /* Kick the child clocks.. */
511 recalculate_root_clocks();
513 /* Enable the necessary init clocks */
514 clk_enable_init_clocks();
519 static int __init clk_proc_init(void)
521 struct proc_dir_entry *p;
522 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
529 subsys_initcall(clk_proc_init);