2 * drivers/sh/clk.c - SuperH clock framework
4 * Copyright (C) 2005 - 2010 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 <linux/sh_clk.h>
32 static LIST_HEAD(clock_list);
33 static DEFINE_SPINLOCK(clock_lock);
34 static DEFINE_MUTEX(clock_list_sem);
36 void clk_rate_table_build(struct clk *clk,
37 struct cpufreq_frequency_table *freq_table,
39 struct clk_div_mult_table *src_table,
40 unsigned long *bitmap)
42 unsigned long mult, div;
46 for (i = 0; i < nr_freqs; i++) {
50 if (src_table->divisors && i < src_table->nr_divisors)
51 div = src_table->divisors[i];
53 if (src_table->multipliers && i < src_table->nr_multipliers)
54 mult = src_table->multipliers[i];
56 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
57 freq = CPUFREQ_ENTRY_INVALID;
59 freq = clk->parent->rate * mult / div;
61 freq_table[i].index = i;
62 freq_table[i].frequency = freq;
65 /* Termination entry */
66 freq_table[i].index = i;
67 freq_table[i].frequency = CPUFREQ_TABLE_END;
70 long clk_rate_table_round(struct clk *clk,
71 struct cpufreq_frequency_table *freq_table,
74 unsigned long rate_error, rate_error_prev = ~0UL;
75 unsigned long rate_best_fit = rate;
76 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 (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
168 if (!(--clk->usecount)) {
169 if (likely(clk->ops && clk->ops->disable))
170 clk->ops->disable(clk);
171 if (likely(clk->parent))
172 __clk_disable(clk->parent);
176 void clk_disable(struct clk *clk)
183 spin_lock_irqsave(&clock_lock, flags);
185 spin_unlock_irqrestore(&clock_lock, flags);
187 EXPORT_SYMBOL_GPL(clk_disable);
189 static int __clk_enable(struct clk *clk)
193 if (clk->usecount++ == 0) {
195 ret = __clk_enable(clk->parent);
200 if (clk->ops && clk->ops->enable) {
201 ret = clk->ops->enable(clk);
204 __clk_disable(clk->parent);
216 int clk_enable(struct clk *clk)
224 spin_lock_irqsave(&clock_lock, flags);
225 ret = __clk_enable(clk);
226 spin_unlock_irqrestore(&clock_lock, flags);
230 EXPORT_SYMBOL_GPL(clk_enable);
232 static LIST_HEAD(root_clks);
235 * recalculate_root_clocks - recalculate and propagate all root clocks
237 * Recalculates all root clocks (clocks with no parent), which if the
238 * clock's .recalc is set correctly, should also propagate their rates.
241 void recalculate_root_clocks(void)
245 list_for_each_entry(clkp, &root_clks, sibling) {
246 if (clkp->ops && clkp->ops->recalc)
247 clkp->rate = clkp->ops->recalc(clkp);
248 propagate_rate(clkp);
252 int clk_register(struct clk *clk)
254 if (clk == NULL || IS_ERR(clk))
258 * trap out already registered clocks
260 if (clk->node.next || clk->node.prev)
263 mutex_lock(&clock_list_sem);
265 INIT_LIST_HEAD(&clk->children);
269 list_add(&clk->sibling, &clk->parent->children);
271 list_add(&clk->sibling, &root_clks);
273 list_add(&clk->node, &clock_list);
274 if (clk->ops && clk->ops->init)
276 mutex_unlock(&clock_list_sem);
280 EXPORT_SYMBOL_GPL(clk_register);
282 void clk_unregister(struct clk *clk)
284 mutex_lock(&clock_list_sem);
285 list_del(&clk->sibling);
286 list_del(&clk->node);
287 mutex_unlock(&clock_list_sem);
289 EXPORT_SYMBOL_GPL(clk_unregister);
291 void clk_enable_init_clocks(void)
295 list_for_each_entry(clkp, &clock_list, node)
296 if (clkp->flags & CLK_ENABLE_ON_INIT)
300 unsigned long clk_get_rate(struct clk *clk)
304 EXPORT_SYMBOL_GPL(clk_get_rate);
306 int clk_set_rate(struct clk *clk, unsigned long rate)
308 return clk_set_rate_ex(clk, rate, 0);
310 EXPORT_SYMBOL_GPL(clk_set_rate);
312 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
314 int ret = -EOPNOTSUPP;
317 spin_lock_irqsave(&clock_lock, flags);
319 if (likely(clk->ops && clk->ops->set_rate)) {
320 ret = clk->ops->set_rate(clk, rate, algo_id);
328 if (clk->ops && clk->ops->recalc)
329 clk->rate = clk->ops->recalc(clk);
334 spin_unlock_irqrestore(&clock_lock, flags);
338 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
340 int clk_set_parent(struct clk *clk, struct clk *parent)
347 if (clk->parent == parent)
350 spin_lock_irqsave(&clock_lock, flags);
351 if (clk->usecount == 0) {
352 if (clk->ops->set_parent)
353 ret = clk->ops->set_parent(clk, parent);
355 ret = clk_reparent(clk, parent);
358 if (clk->ops->recalc)
359 clk->rate = clk->ops->recalc(clk);
360 pr_debug("clock: set parent of %p to %p (new rate %ld)\n",
361 clk, clk->parent, clk->rate);
366 spin_unlock_irqrestore(&clock_lock, flags);
370 EXPORT_SYMBOL_GPL(clk_set_parent);
372 struct clk *clk_get_parent(struct clk *clk)
376 EXPORT_SYMBOL_GPL(clk_get_parent);
378 long clk_round_rate(struct clk *clk, unsigned long rate)
380 if (likely(clk->ops && clk->ops->round_rate)) {
381 unsigned long flags, rounded;
383 spin_lock_irqsave(&clock_lock, flags);
384 rounded = clk->ops->round_rate(clk, rate);
385 spin_unlock_irqrestore(&clock_lock, flags);
390 return clk_get_rate(clk);
392 EXPORT_SYMBOL_GPL(clk_round_rate);
395 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
397 static pm_message_t prev_state;
400 switch (state.event) {
402 /* Resumeing from hibernation */
403 if (prev_state.event != PM_EVENT_FREEZE)
406 list_for_each_entry(clkp, &clock_list, node) {
407 if (likely(clkp->ops)) {
408 unsigned long rate = clkp->rate;
410 if (likely(clkp->ops->set_parent))
411 clkp->ops->set_parent(clkp,
413 if (likely(clkp->ops->set_rate))
414 clkp->ops->set_rate(clkp,
416 else if (likely(clkp->ops->recalc))
417 clkp->rate = clkp->ops->recalc(clkp);
421 case PM_EVENT_FREEZE:
423 case PM_EVENT_SUSPEND:
431 static int clks_sysdev_resume(struct sys_device *dev)
433 return clks_sysdev_suspend(dev, PMSG_ON);
436 static struct sysdev_class clks_sysdev_class = {
440 static struct sysdev_driver clks_sysdev_driver = {
441 .suspend = clks_sysdev_suspend,
442 .resume = clks_sysdev_resume,
445 static struct sys_device clks_sysdev_dev = {
446 .cls = &clks_sysdev_class,
449 static int __init clk_sysdev_init(void)
451 sysdev_class_register(&clks_sysdev_class);
452 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
453 sysdev_register(&clks_sysdev_dev);
457 subsys_initcall(clk_sysdev_init);
461 * debugfs support to trace clock tree hierarchy and attributes
463 static struct dentry *clk_debugfs_root;
465 static int clk_debugfs_register_one(struct clk *c)
468 struct dentry *d, *child, *child_tmp;
469 struct clk *pa = c->parent;
473 p += sprintf(p, "%p", c);
474 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
479 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
484 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
489 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
498 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
499 debugfs_remove(child);
500 debugfs_remove(c->dentry);
504 static int clk_debugfs_register(struct clk *c)
507 struct clk *pa = c->parent;
509 if (pa && !pa->dentry) {
510 err = clk_debugfs_register(pa);
516 err = clk_debugfs_register_one(c);
523 static int __init clk_debugfs_init(void)
529 d = debugfs_create_dir("clock", NULL);
532 clk_debugfs_root = d;
534 list_for_each_entry(c, &clock_list, node) {
535 err = clk_debugfs_register(c);
541 debugfs_remove_recursive(clk_debugfs_root);
544 late_initcall(clk_debugfs_init);