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>
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/list.h>
22 #include <linux/kobject.h>
23 #include <linux/sysdev.h>
24 #include <linux/seq_file.h>
25 #include <linux/err.h>
26 #include <linux/platform_device.h>
27 #include <linux/debugfs.h>
28 #include <linux/cpufreq.h>
29 #include <linux/clk.h>
30 #include <asm/clock.h>
31 #include <asm/machvec.h>
33 static LIST_HEAD(clock_list);
34 static DEFINE_SPINLOCK(clock_lock);
35 static DEFINE_MUTEX(clock_list_sem);
37 void clk_rate_table_build(struct clk *clk,
38 struct cpufreq_frequency_table *freq_table,
40 struct clk_div_mult_table *src_table,
41 unsigned long *bitmap)
43 unsigned long mult, div;
47 for (i = 0; i < nr_freqs; i++) {
51 if (src_table->divisors && i < src_table->nr_divisors)
52 div = src_table->divisors[i];
54 if (src_table->multipliers && i < src_table->nr_multipliers)
55 mult = src_table->multipliers[i];
57 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
58 freq = CPUFREQ_ENTRY_INVALID;
60 freq = clk->parent->rate * mult / div;
62 freq_table[i].index = i;
63 freq_table[i].frequency = freq;
66 /* Termination entry */
67 freq_table[i].index = i;
68 freq_table[i].frequency = CPUFREQ_TABLE_END;
71 long clk_rate_table_round(struct clk *clk,
72 struct cpufreq_frequency_table *freq_table,
75 unsigned long rate_error, rate_error_prev = ~0UL;
76 unsigned long rate_best_fit = rate;
77 unsigned long highest, lowest;
82 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
83 unsigned long freq = freq_table[i].frequency;
85 if (freq == CPUFREQ_ENTRY_INVALID)
93 rate_error = abs(freq - rate);
94 if (rate_error < rate_error_prev) {
96 rate_error_prev = rate_error;
104 rate_best_fit = highest;
106 rate_best_fit = lowest;
108 return rate_best_fit;
111 int clk_rate_table_find(struct clk *clk,
112 struct cpufreq_frequency_table *freq_table,
117 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
118 unsigned long freq = freq_table[i].frequency;
120 if (freq == CPUFREQ_ENTRY_INVALID)
130 /* Used for clocks that always have same value as the parent clock */
131 unsigned long followparent_recalc(struct clk *clk)
133 return clk->parent ? clk->parent->rate : 0;
136 int clk_reparent(struct clk *child, struct clk *parent)
138 list_del_init(&child->sibling);
140 list_add(&child->sibling, &parent->children);
141 child->parent = parent;
143 /* now do the debugfs renaming to reattach the child
144 to the proper parent */
149 /* Propagate rate to children */
150 void propagate_rate(struct clk *tclk)
154 list_for_each_entry(clkp, &tclk->children, sibling) {
155 if (clkp->ops && clkp->ops->recalc)
156 clkp->rate = clkp->ops->recalc(clkp);
158 propagate_rate(clkp);
162 static void __clk_disable(struct clk *clk)
164 if (clk->usecount == 0) {
165 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
171 if (!(--clk->usecount)) {
172 if (likely(clk->ops && clk->ops->disable))
173 clk->ops->disable(clk);
174 if (likely(clk->parent))
175 __clk_disable(clk->parent);
179 void clk_disable(struct clk *clk)
186 spin_lock_irqsave(&clock_lock, flags);
188 spin_unlock_irqrestore(&clock_lock, flags);
190 EXPORT_SYMBOL_GPL(clk_disable);
192 static int __clk_enable(struct clk *clk)
196 if (clk->usecount++ == 0) {
198 ret = __clk_enable(clk->parent);
203 if (clk->ops && clk->ops->enable) {
204 ret = clk->ops->enable(clk);
207 __clk_disable(clk->parent);
219 int clk_enable(struct clk *clk)
227 spin_lock_irqsave(&clock_lock, flags);
228 ret = __clk_enable(clk);
229 spin_unlock_irqrestore(&clock_lock, flags);
233 EXPORT_SYMBOL_GPL(clk_enable);
235 static LIST_HEAD(root_clks);
238 * recalculate_root_clocks - recalculate and propagate all root clocks
240 * Recalculates all root clocks (clocks with no parent), which if the
241 * clock's .recalc is set correctly, should also propagate their rates.
244 void recalculate_root_clocks(void)
248 list_for_each_entry(clkp, &root_clks, sibling) {
249 if (clkp->ops && clkp->ops->recalc)
250 clkp->rate = clkp->ops->recalc(clkp);
251 propagate_rate(clkp);
255 int clk_register(struct clk *clk)
257 if (clk == NULL || IS_ERR(clk))
261 * trap out already registered clocks
263 if (clk->node.next || clk->node.prev)
266 mutex_lock(&clock_list_sem);
268 INIT_LIST_HEAD(&clk->children);
272 list_add(&clk->sibling, &clk->parent->children);
274 list_add(&clk->sibling, &root_clks);
276 list_add(&clk->node, &clock_list);
277 if (clk->ops && clk->ops->init)
279 mutex_unlock(&clock_list_sem);
283 EXPORT_SYMBOL_GPL(clk_register);
285 void clk_unregister(struct clk *clk)
287 mutex_lock(&clock_list_sem);
288 list_del(&clk->sibling);
289 list_del(&clk->node);
290 mutex_unlock(&clock_list_sem);
292 EXPORT_SYMBOL_GPL(clk_unregister);
294 static void clk_enable_init_clocks(void)
298 list_for_each_entry(clkp, &clock_list, node)
299 if (clkp->flags & CLK_ENABLE_ON_INIT)
303 unsigned long clk_get_rate(struct clk *clk)
307 EXPORT_SYMBOL_GPL(clk_get_rate);
309 int clk_set_rate(struct clk *clk, unsigned long rate)
311 return clk_set_rate_ex(clk, rate, 0);
313 EXPORT_SYMBOL_GPL(clk_set_rate);
315 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
317 int ret = -EOPNOTSUPP;
320 spin_lock_irqsave(&clock_lock, flags);
322 if (likely(clk->ops && clk->ops->set_rate)) {
323 ret = clk->ops->set_rate(clk, rate, algo_id);
331 if (clk->ops && clk->ops->recalc)
332 clk->rate = clk->ops->recalc(clk);
337 spin_unlock_irqrestore(&clock_lock, flags);
341 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
343 int clk_set_parent(struct clk *clk, struct clk *parent)
350 if (clk->parent == parent)
353 spin_lock_irqsave(&clock_lock, flags);
354 if (clk->usecount == 0) {
355 if (clk->ops->set_parent)
356 ret = clk->ops->set_parent(clk, parent);
358 ret = clk_reparent(clk, parent);
361 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
362 clk->name, clk->parent->name, clk->rate);
363 if (clk->ops->recalc)
364 clk->rate = clk->ops->recalc(clk);
369 spin_unlock_irqrestore(&clock_lock, flags);
373 EXPORT_SYMBOL_GPL(clk_set_parent);
375 struct clk *clk_get_parent(struct clk *clk)
379 EXPORT_SYMBOL_GPL(clk_get_parent);
381 long clk_round_rate(struct clk *clk, unsigned long rate)
383 if (likely(clk->ops && clk->ops->round_rate)) {
384 unsigned long flags, rounded;
386 spin_lock_irqsave(&clock_lock, flags);
387 rounded = clk->ops->round_rate(clk, rate);
388 spin_unlock_irqrestore(&clock_lock, flags);
393 return clk_get_rate(clk);
395 EXPORT_SYMBOL_GPL(clk_round_rate);
398 * Returns a clock. Note that we first try to use device id on the bus
399 * and clock name. If this fails, we try to use clock name only.
401 struct clk *clk_get(struct device *dev, const char *id)
403 const char *dev_id = dev ? dev_name(dev) : NULL;
404 struct clk *p, *clk = ERR_PTR(-ENOENT);
407 clk = clk_get_sys(dev_id, id);
408 if (clk && !IS_ERR(clk))
411 if (dev == NULL || dev->bus != &platform_bus_type)
414 idno = to_platform_device(dev)->id;
416 mutex_lock(&clock_list_sem);
417 list_for_each_entry(p, &clock_list, node) {
418 if (p->name && p->id == idno &&
419 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
425 list_for_each_entry(p, &clock_list, node) {
427 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
434 mutex_unlock(&clock_list_sem);
438 EXPORT_SYMBOL_GPL(clk_get);
440 void clk_put(struct clk *clk)
442 if (clk && !IS_ERR(clk))
443 module_put(clk->owner);
445 EXPORT_SYMBOL_GPL(clk_put);
448 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
450 static pm_message_t prev_state;
453 switch (state.event) {
455 /* Resumeing from hibernation */
456 if (prev_state.event != PM_EVENT_FREEZE)
459 list_for_each_entry(clkp, &clock_list, node) {
460 if (likely(clkp->ops)) {
461 unsigned long rate = clkp->rate;
463 if (likely(clkp->ops->set_parent))
464 clkp->ops->set_parent(clkp,
466 if (likely(clkp->ops->set_rate))
467 clkp->ops->set_rate(clkp,
469 else if (likely(clkp->ops->recalc))
470 clkp->rate = clkp->ops->recalc(clkp);
474 case PM_EVENT_FREEZE:
476 case PM_EVENT_SUSPEND:
484 static int clks_sysdev_resume(struct sys_device *dev)
486 return clks_sysdev_suspend(dev, PMSG_ON);
489 static struct sysdev_class clks_sysdev_class = {
493 static struct sysdev_driver clks_sysdev_driver = {
494 .suspend = clks_sysdev_suspend,
495 .resume = clks_sysdev_resume,
498 static struct sys_device clks_sysdev_dev = {
499 .cls = &clks_sysdev_class,
502 static int __init clk_sysdev_init(void)
504 sysdev_class_register(&clks_sysdev_class);
505 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
506 sysdev_register(&clks_sysdev_dev);
510 subsys_initcall(clk_sysdev_init);
513 int __init clk_init(void)
517 ret = arch_clk_init();
519 pr_err("%s: CPU clock registration failed.\n", __func__);
523 if (sh_mv.mv_clk_init) {
524 ret = sh_mv.mv_clk_init();
526 pr_err("%s: machvec clock initialization failed.\n",
532 /* Kick the child clocks.. */
533 recalculate_root_clocks();
535 /* Enable the necessary init clocks */
536 clk_enable_init_clocks();
542 * debugfs support to trace clock tree hierarchy and attributes
544 static struct dentry *clk_debugfs_root;
546 static int clk_debugfs_register_one(struct clk *c)
549 struct dentry *d, *child, *child_tmp;
550 struct clk *pa = c->parent;
554 p += sprintf(p, "%s", c->name);
556 sprintf(p, ":%d", c->id);
557 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
562 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
567 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
572 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
581 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
582 debugfs_remove(child);
583 debugfs_remove(c->dentry);
587 static int clk_debugfs_register(struct clk *c)
590 struct clk *pa = c->parent;
592 if (pa && !pa->dentry) {
593 err = clk_debugfs_register(pa);
598 if (!c->dentry && c->name) {
599 err = clk_debugfs_register_one(c);
606 static int __init clk_debugfs_init(void)
612 d = debugfs_create_dir("clock", NULL);
615 clk_debugfs_root = d;
617 list_for_each_entry(c, &clock_list, node) {
618 err = clk_debugfs_register(c);
624 debugfs_remove_recursive(clk_debugfs_root);
627 late_initcall(clk_debugfs_init);