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 <linux/cpufreq.h>
33 #include <asm/clock.h>
34 #include <asm/machvec.h>
36 static LIST_HEAD(clock_list);
37 static DEFINE_SPINLOCK(clock_lock);
38 static DEFINE_MUTEX(clock_list_sem);
40 void clk_rate_table_build(struct clk *clk,
41 struct cpufreq_frequency_table *freq_table,
43 struct clk_div_mult_table *src_table,
44 unsigned long *bitmap)
46 unsigned long mult, div;
50 for (i = 0; i < nr_freqs; i++) {
54 if (src_table->divisors && i < src_table->nr_divisors)
55 div = src_table->divisors[i];
57 if (src_table->multipliers && i < src_table->nr_multipliers)
58 mult = src_table->multipliers[i];
60 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
61 freq = CPUFREQ_ENTRY_INVALID;
63 freq = clk->parent->rate * mult / div;
65 freq_table[i].index = i;
66 freq_table[i].frequency = freq;
69 /* Termination entry */
70 freq_table[i].index = i;
71 freq_table[i].frequency = CPUFREQ_TABLE_END;
74 long clk_rate_table_round(struct clk *clk,
75 struct cpufreq_frequency_table *freq_table,
78 unsigned long rate_error, rate_error_prev = ~0UL;
79 unsigned long rate_best_fit = rate;
80 unsigned long highest, lowest;
85 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
86 unsigned long freq = freq_table[i].frequency;
88 if (freq == CPUFREQ_ENTRY_INVALID)
96 rate_error = abs(freq - rate);
97 if (rate_error < rate_error_prev) {
99 rate_error_prev = rate_error;
107 rate_best_fit = highest;
109 rate_best_fit = lowest;
111 return rate_best_fit;
114 /* Used for clocks that always have same value as the parent clock */
115 unsigned long followparent_recalc(struct clk *clk)
117 return clk->parent ? clk->parent->rate : 0;
120 int clk_reparent(struct clk *child, struct clk *parent)
122 list_del_init(&child->sibling);
124 list_add(&child->sibling, &parent->children);
125 child->parent = parent;
127 /* now do the debugfs renaming to reattach the child
128 to the proper parent */
133 /* Propagate rate to children */
134 void propagate_rate(struct clk *tclk)
138 list_for_each_entry(clkp, &tclk->children, sibling) {
139 if (clkp->ops && clkp->ops->recalc)
140 clkp->rate = clkp->ops->recalc(clkp);
142 propagate_rate(clkp);
146 static void __clk_disable(struct clk *clk)
148 if (clk->usecount == 0) {
149 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
155 if (!(--clk->usecount)) {
156 if (likely(clk->ops && clk->ops->disable))
157 clk->ops->disable(clk);
158 if (likely(clk->parent))
159 __clk_disable(clk->parent);
163 void clk_disable(struct clk *clk)
170 spin_lock_irqsave(&clock_lock, flags);
172 spin_unlock_irqrestore(&clock_lock, flags);
174 EXPORT_SYMBOL_GPL(clk_disable);
176 static int __clk_enable(struct clk *clk)
180 if (clk->usecount++ == 0) {
182 ret = __clk_enable(clk->parent);
187 if (clk->ops && clk->ops->enable) {
188 ret = clk->ops->enable(clk);
191 __clk_disable(clk->parent);
203 int clk_enable(struct clk *clk)
211 spin_lock_irqsave(&clock_lock, flags);
212 ret = __clk_enable(clk);
213 spin_unlock_irqrestore(&clock_lock, flags);
217 EXPORT_SYMBOL_GPL(clk_enable);
219 static LIST_HEAD(root_clks);
222 * recalculate_root_clocks - recalculate and propagate all root clocks
224 * Recalculates all root clocks (clocks with no parent), which if the
225 * clock's .recalc is set correctly, should also propagate their rates.
228 void recalculate_root_clocks(void)
232 list_for_each_entry(clkp, &root_clks, sibling) {
233 if (clkp->ops && clkp->ops->recalc)
234 clkp->rate = clkp->ops->recalc(clkp);
235 propagate_rate(clkp);
239 int clk_register(struct clk *clk)
241 if (clk == NULL || IS_ERR(clk))
245 * trap out already registered clocks
247 if (clk->node.next || clk->node.prev)
250 mutex_lock(&clock_list_sem);
252 INIT_LIST_HEAD(&clk->children);
256 list_add(&clk->sibling, &clk->parent->children);
258 list_add(&clk->sibling, &root_clks);
260 list_add(&clk->node, &clock_list);
261 if (clk->ops && clk->ops->init)
263 mutex_unlock(&clock_list_sem);
267 EXPORT_SYMBOL_GPL(clk_register);
269 void clk_unregister(struct clk *clk)
271 mutex_lock(&clock_list_sem);
272 list_del(&clk->sibling);
273 list_del(&clk->node);
274 mutex_unlock(&clock_list_sem);
276 EXPORT_SYMBOL_GPL(clk_unregister);
278 static void clk_enable_init_clocks(void)
282 list_for_each_entry(clkp, &clock_list, node)
283 if (clkp->flags & CLK_ENABLE_ON_INIT)
287 unsigned long clk_get_rate(struct clk *clk)
291 EXPORT_SYMBOL_GPL(clk_get_rate);
293 int clk_set_rate(struct clk *clk, unsigned long rate)
295 return clk_set_rate_ex(clk, rate, 0);
297 EXPORT_SYMBOL_GPL(clk_set_rate);
299 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
301 int ret = -EOPNOTSUPP;
304 spin_lock_irqsave(&clock_lock, flags);
306 if (likely(clk->ops && clk->ops->set_rate)) {
307 ret = clk->ops->set_rate(clk, rate, algo_id);
315 if (clk->ops && clk->ops->recalc)
316 clk->rate = clk->ops->recalc(clk);
321 spin_unlock_irqrestore(&clock_lock, flags);
325 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
327 int clk_set_parent(struct clk *clk, struct clk *parent)
334 if (clk->parent == parent)
337 spin_lock_irqsave(&clock_lock, flags);
338 if (clk->usecount == 0) {
339 if (clk->ops->set_parent)
340 ret = clk->ops->set_parent(clk, parent);
342 ret = clk_reparent(clk, parent);
345 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
346 clk->name, clk->parent->name, clk->rate);
347 if (clk->ops->recalc)
348 clk->rate = clk->ops->recalc(clk);
353 spin_unlock_irqrestore(&clock_lock, flags);
357 EXPORT_SYMBOL_GPL(clk_set_parent);
359 struct clk *clk_get_parent(struct clk *clk)
363 EXPORT_SYMBOL_GPL(clk_get_parent);
365 long clk_round_rate(struct clk *clk, unsigned long rate)
367 if (likely(clk->ops && clk->ops->round_rate)) {
368 unsigned long flags, rounded;
370 spin_lock_irqsave(&clock_lock, flags);
371 rounded = clk->ops->round_rate(clk, rate);
372 spin_unlock_irqrestore(&clock_lock, flags);
377 return clk_get_rate(clk);
379 EXPORT_SYMBOL_GPL(clk_round_rate);
382 * Find the correct struct clk for the device and connection ID.
383 * We do slightly fuzzy matching here:
384 * An entry with a NULL ID is assumed to be a wildcard.
385 * If an entry has a device ID, it must match
386 * If an entry has a connection ID, it must match
387 * Then we take the most specific entry - with the following
388 * order of precidence: dev+con > dev only > con only.
390 static struct clk *clk_find(const char *dev_id, const char *con_id)
392 struct clk_lookup *p;
393 struct clk *clk = NULL;
396 list_for_each_entry(p, &clock_list, node) {
399 if (!dev_id || strcmp(p->dev_id, dev_id))
404 if (!con_id || strcmp(p->con_id, con_id))
419 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
423 mutex_lock(&clock_list_sem);
424 clk = clk_find(dev_id, con_id);
425 mutex_unlock(&clock_list_sem);
427 return clk ? clk : ERR_PTR(-ENOENT);
429 EXPORT_SYMBOL_GPL(clk_get_sys);
432 * Returns a clock. Note that we first try to use device id on the bus
433 * and clock name. If this fails, we try to use clock name only.
435 struct clk *clk_get(struct device *dev, const char *id)
437 const char *dev_id = dev ? dev_name(dev) : NULL;
438 struct clk *p, *clk = ERR_PTR(-ENOENT);
441 clk = clk_get_sys(dev_id, id);
442 if (clk && !IS_ERR(clk))
445 if (dev == NULL || dev->bus != &platform_bus_type)
448 idno = to_platform_device(dev)->id;
450 mutex_lock(&clock_list_sem);
451 list_for_each_entry(p, &clock_list, node) {
453 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
459 list_for_each_entry(p, &clock_list, node) {
460 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
467 mutex_unlock(&clock_list_sem);
471 EXPORT_SYMBOL_GPL(clk_get);
473 void clk_put(struct clk *clk)
475 if (clk && !IS_ERR(clk))
476 module_put(clk->owner);
478 EXPORT_SYMBOL_GPL(clk_put);
481 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
483 static pm_message_t prev_state;
486 switch (state.event) {
488 /* Resumeing from hibernation */
489 if (prev_state.event != PM_EVENT_FREEZE)
492 list_for_each_entry(clkp, &clock_list, node) {
493 if (likely(clkp->ops)) {
494 unsigned long rate = clkp->rate;
496 if (likely(clkp->ops->set_parent))
497 clkp->ops->set_parent(clkp,
499 if (likely(clkp->ops->set_rate))
500 clkp->ops->set_rate(clkp,
502 else if (likely(clkp->ops->recalc))
503 clkp->rate = clkp->ops->recalc(clkp);
507 case PM_EVENT_FREEZE:
509 case PM_EVENT_SUSPEND:
517 static int clks_sysdev_resume(struct sys_device *dev)
519 return clks_sysdev_suspend(dev, PMSG_ON);
522 static struct sysdev_class clks_sysdev_class = {
526 static struct sysdev_driver clks_sysdev_driver = {
527 .suspend = clks_sysdev_suspend,
528 .resume = clks_sysdev_resume,
531 static struct sys_device clks_sysdev_dev = {
532 .cls = &clks_sysdev_class,
535 static int __init clk_sysdev_init(void)
537 sysdev_class_register(&clks_sysdev_class);
538 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
539 sysdev_register(&clks_sysdev_dev);
543 subsys_initcall(clk_sysdev_init);
546 int __init clk_init(void)
550 ret = arch_clk_init();
552 pr_err("%s: CPU clock registration failed.\n", __func__);
556 if (sh_mv.mv_clk_init) {
557 ret = sh_mv.mv_clk_init();
559 pr_err("%s: machvec clock initialization failed.\n",
565 /* Kick the child clocks.. */
566 recalculate_root_clocks();
568 /* Enable the necessary init clocks */
569 clk_enable_init_clocks();
575 * debugfs support to trace clock tree hierarchy and attributes
577 static struct dentry *clk_debugfs_root;
579 static int clk_debugfs_register_one(struct clk *c)
582 struct dentry *d, *child;
583 struct clk *pa = c->parent;
587 p += sprintf(p, "%s", c->name);
589 sprintf(p, ":%d", c->id);
590 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
595 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
600 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
605 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
614 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
615 debugfs_remove(child);
616 debugfs_remove(c->dentry);
620 static int clk_debugfs_register(struct clk *c)
623 struct clk *pa = c->parent;
625 if (pa && !pa->dentry) {
626 err = clk_debugfs_register(pa);
632 err = clk_debugfs_register_one(c);
639 static int __init clk_debugfs_init(void)
645 d = debugfs_create_dir("clock", NULL);
648 clk_debugfs_root = d;
650 list_for_each_entry(c, &clock_list, node) {
651 err = clk_debugfs_register(c);
657 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
660 late_initcall(clk_debugfs_init);