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/timer.h>
35 static LIST_HEAD(clock_list);
36 static DEFINE_SPINLOCK(clock_lock);
37 static DEFINE_MUTEX(clock_list_sem);
40 * Each subtype is expected to define the init routines for these clocks,
41 * as each subtype (or processor family) will have these clocks at the
42 * very least. These are all provided through the CPG, which even some of
43 * the more quirky parts (such as ST40, SH4-202, etc.) still have.
45 * The processor-specific code is expected to register any additional
46 * clock sources that are of interest.
48 static struct clk master_clk = {
50 .flags = CLK_ENABLE_ON_INIT,
51 .rate = CONFIG_SH_PCLK_FREQ,
54 static struct clk module_clk = {
56 .parent = &master_clk,
57 .flags = CLK_ENABLE_ON_INIT,
60 static struct clk bus_clk = {
62 .parent = &master_clk,
63 .flags = CLK_ENABLE_ON_INIT,
66 static struct clk cpu_clk = {
68 .parent = &master_clk,
69 .flags = CLK_ENABLE_ON_INIT,
73 * The ordering of these clocks matters, do not change it.
75 static struct clk *onchip_clocks[] = {
82 /* Used for clocks that always have same value as the parent clock */
83 unsigned long followparent_recalc(struct clk *clk)
85 return clk->parent->rate;
88 int clk_reparent(struct clk *child, struct clk *parent)
90 list_del_init(&child->sibling);
92 list_add(&child->sibling, &parent->children);
93 child->parent = parent;
95 /* now do the debugfs renaming to reattach the child
96 to the proper parent */
101 /* Propagate rate to children */
102 void propagate_rate(struct clk *tclk)
106 list_for_each_entry(clkp, &tclk->children, sibling) {
107 if (clkp->ops->recalc)
108 clkp->rate = clkp->ops->recalc(clkp);
109 propagate_rate(clkp);
113 static void __clk_disable(struct clk *clk)
115 if (clk->usecount == 0) {
116 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
122 if (!(--clk->usecount)) {
123 if (likely(clk->ops && clk->ops->disable))
124 clk->ops->disable(clk);
125 if (likely(clk->parent))
126 __clk_disable(clk->parent);
130 void clk_disable(struct clk *clk)
137 spin_lock_irqsave(&clock_lock, flags);
139 spin_unlock_irqrestore(&clock_lock, flags);
141 EXPORT_SYMBOL_GPL(clk_disable);
143 static int __clk_enable(struct clk *clk)
147 if (clk->usecount++ == 0) {
149 ret = __clk_enable(clk->parent);
154 if (clk->ops && clk->ops->enable) {
155 ret = clk->ops->enable(clk);
158 __clk_disable(clk->parent);
170 int clk_enable(struct clk *clk)
178 spin_lock_irqsave(&clock_lock, flags);
179 ret = __clk_enable(clk);
180 spin_unlock_irqrestore(&clock_lock, flags);
184 EXPORT_SYMBOL_GPL(clk_enable);
186 static LIST_HEAD(root_clks);
189 * recalculate_root_clocks - recalculate and propagate all root clocks
191 * Recalculates all root clocks (clocks with no parent), which if the
192 * clock's .recalc is set correctly, should also propagate their rates.
195 void recalculate_root_clocks(void)
199 list_for_each_entry(clkp, &root_clks, sibling) {
200 if (clkp->ops->recalc)
201 clkp->rate = clkp->ops->recalc(clkp);
202 propagate_rate(clkp);
206 int clk_register(struct clk *clk)
208 if (clk == NULL || IS_ERR(clk))
212 * trap out already registered clocks
214 if (clk->node.next || clk->node.prev)
217 mutex_lock(&clock_list_sem);
219 INIT_LIST_HEAD(&clk->children);
223 list_add(&clk->sibling, &clk->parent->children);
225 list_add(&clk->sibling, &root_clks);
227 list_add(&clk->node, &clock_list);
230 mutex_unlock(&clock_list_sem);
234 EXPORT_SYMBOL_GPL(clk_register);
236 void clk_unregister(struct clk *clk)
238 mutex_lock(&clock_list_sem);
239 list_del(&clk->sibling);
240 list_del(&clk->node);
241 mutex_unlock(&clock_list_sem);
243 EXPORT_SYMBOL_GPL(clk_unregister);
245 static void clk_enable_init_clocks(void)
249 list_for_each_entry(clkp, &clock_list, node)
250 if (clkp->flags & CLK_ENABLE_ON_INIT)
254 unsigned long clk_get_rate(struct clk *clk)
258 EXPORT_SYMBOL_GPL(clk_get_rate);
260 int clk_set_rate(struct clk *clk, unsigned long rate)
262 return clk_set_rate_ex(clk, rate, 0);
264 EXPORT_SYMBOL_GPL(clk_set_rate);
266 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
268 int ret = -EOPNOTSUPP;
270 if (likely(clk->ops && clk->ops->set_rate)) {
273 spin_lock_irqsave(&clock_lock, flags);
274 ret = clk->ops->set_rate(clk, rate, algo_id);
276 if (clk->ops->recalc)
277 clk->rate = clk->ops->recalc(clk);
280 spin_unlock_irqrestore(&clock_lock, flags);
285 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
287 int clk_set_parent(struct clk *clk, struct clk *parent)
294 if (clk->parent == parent)
297 spin_lock_irqsave(&clock_lock, flags);
298 if (clk->usecount == 0) {
299 if (clk->ops->set_parent)
300 ret = clk->ops->set_parent(clk, parent);
302 ret = clk_reparent(clk, parent);
305 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
306 clk->name, clk->parent->name, clk->rate);
307 if (clk->ops->recalc)
308 clk->rate = clk->ops->recalc(clk);
313 spin_unlock_irqrestore(&clock_lock, flags);
317 EXPORT_SYMBOL_GPL(clk_set_parent);
319 struct clk *clk_get_parent(struct clk *clk)
323 EXPORT_SYMBOL_GPL(clk_get_parent);
325 long clk_round_rate(struct clk *clk, unsigned long rate)
327 if (likely(clk->ops && clk->ops->round_rate)) {
328 unsigned long flags, rounded;
330 spin_lock_irqsave(&clock_lock, flags);
331 rounded = clk->ops->round_rate(clk, rate);
332 spin_unlock_irqrestore(&clock_lock, flags);
337 return clk_get_rate(clk);
339 EXPORT_SYMBOL_GPL(clk_round_rate);
342 * Find the correct struct clk for the device and connection ID.
343 * We do slightly fuzzy matching here:
344 * An entry with a NULL ID is assumed to be a wildcard.
345 * If an entry has a device ID, it must match
346 * If an entry has a connection ID, it must match
347 * Then we take the most specific entry - with the following
348 * order of precidence: dev+con > dev only > con only.
350 static struct clk *clk_find(const char *dev_id, const char *con_id)
352 struct clk_lookup *p;
353 struct clk *clk = NULL;
356 list_for_each_entry(p, &clock_list, node) {
359 if (!dev_id || strcmp(p->dev_id, dev_id))
364 if (!con_id || strcmp(p->con_id, con_id))
379 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
383 mutex_lock(&clock_list_sem);
384 clk = clk_find(dev_id, con_id);
385 mutex_unlock(&clock_list_sem);
387 return clk ? clk : ERR_PTR(-ENOENT);
389 EXPORT_SYMBOL_GPL(clk_get_sys);
392 * Returns a clock. Note that we first try to use device id on the bus
393 * and clock name. If this fails, we try to use clock name only.
395 struct clk *clk_get(struct device *dev, const char *id)
397 const char *dev_id = dev ? dev_name(dev) : NULL;
398 struct clk *p, *clk = ERR_PTR(-ENOENT);
401 clk = clk_get_sys(dev_id, id);
402 if (clk && !IS_ERR(clk))
405 if (dev == NULL || dev->bus != &platform_bus_type)
408 idno = to_platform_device(dev)->id;
410 mutex_lock(&clock_list_sem);
411 list_for_each_entry(p, &clock_list, node) {
413 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
419 list_for_each_entry(p, &clock_list, node) {
420 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
427 mutex_unlock(&clock_list_sem);
431 EXPORT_SYMBOL_GPL(clk_get);
433 void clk_put(struct clk *clk)
435 if (clk && !IS_ERR(clk))
436 module_put(clk->owner);
438 EXPORT_SYMBOL_GPL(clk_put);
440 void __init __attribute__ ((weak))
441 arch_init_clk_ops(struct clk_ops **ops, int type)
445 int __init __attribute__ ((weak))
451 static int show_clocks(char *buf, char **start, off_t off,
452 int len, int *eof, void *data)
457 list_for_each_entry_reverse(clk, &clock_list, node) {
458 unsigned long rate = clk_get_rate(clk);
460 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
461 rate / 1000000, (rate % 1000000) / 10000,
462 (clk->usecount > 0) ? "enabled" : "disabled");
469 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
471 static pm_message_t prev_state;
474 switch (state.event) {
476 /* Resumeing from hibernation */
477 if (prev_state.event != PM_EVENT_FREEZE)
480 list_for_each_entry(clkp, &clock_list, node) {
481 if (likely(clkp->ops)) {
482 unsigned long rate = clkp->rate;
484 if (likely(clkp->ops->set_parent))
485 clkp->ops->set_parent(clkp,
487 if (likely(clkp->ops->set_rate))
488 clkp->ops->set_rate(clkp,
490 else if (likely(clkp->ops->recalc))
491 clkp->rate = clkp->ops->recalc(clkp);
495 case PM_EVENT_FREEZE:
497 case PM_EVENT_SUSPEND:
505 static int clks_sysdev_resume(struct sys_device *dev)
507 return clks_sysdev_suspend(dev, PMSG_ON);
510 static struct sysdev_class clks_sysdev_class = {
514 static struct sysdev_driver clks_sysdev_driver = {
515 .suspend = clks_sysdev_suspend,
516 .resume = clks_sysdev_resume,
519 static struct sys_device clks_sysdev_dev = {
520 .cls = &clks_sysdev_class,
523 static int __init clk_sysdev_init(void)
525 sysdev_class_register(&clks_sysdev_class);
526 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
527 sysdev_register(&clks_sysdev_dev);
531 subsys_initcall(clk_sysdev_init);
534 int __init clk_init(void)
538 BUG_ON(!master_clk.rate);
540 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
541 struct clk *clk = onchip_clocks[i];
543 arch_init_clk_ops(&clk->ops, i);
544 ret |= clk_register(clk);
547 ret |= arch_clk_init();
549 /* Kick the child clocks.. */
550 recalculate_root_clocks();
552 /* Enable the necessary init clocks */
553 clk_enable_init_clocks();
558 static int __init clk_proc_init(void)
560 struct proc_dir_entry *p;
561 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
568 subsys_initcall(clk_proc_init);