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>
34 static LIST_HEAD(clock_list);
35 static DEFINE_SPINLOCK(clock_lock);
36 static DEFINE_MUTEX(clock_list_sem);
39 * Each subtype is expected to define the init routines for these clocks,
40 * as each subtype (or processor family) will have these clocks at the
41 * very least. These are all provided through the CPG, which even some of
42 * the more quirky parts (such as ST40, SH4-202, etc.) still have.
44 * The processor-specific code is expected to register any additional
45 * clock sources that are of interest.
47 static struct clk master_clk = {
49 .flags = CLK_ENABLE_ON_INIT,
50 .rate = CONFIG_SH_PCLK_FREQ,
53 static struct clk peripheral_clk = {
54 .name = "peripheral_clk",
55 .parent = &master_clk,
56 .flags = CLK_ENABLE_ON_INIT,
59 static struct clk bus_clk = {
61 .parent = &master_clk,
62 .flags = CLK_ENABLE_ON_INIT,
65 static struct clk cpu_clk = {
67 .parent = &master_clk,
68 .flags = CLK_ENABLE_ON_INIT,
72 * The ordering of these clocks matters, do not change it.
74 static struct clk *onchip_clocks[] = {
81 /* Used for clocks that always have same value as the parent clock */
82 unsigned long followparent_recalc(struct clk *clk)
84 return clk->parent->rate;
87 int clk_reparent(struct clk *child, struct clk *parent)
89 list_del_init(&child->sibling);
91 list_add(&child->sibling, &parent->children);
92 child->parent = parent;
94 /* now do the debugfs renaming to reattach the child
95 to the proper parent */
100 /* Propagate rate to children */
101 void propagate_rate(struct clk *tclk)
105 list_for_each_entry(clkp, &tclk->children, sibling) {
106 if (clkp->ops && clkp->ops->recalc)
107 clkp->rate = clkp->ops->recalc(clkp);
108 propagate_rate(clkp);
112 static void __clk_disable(struct clk *clk)
114 if (clk->usecount == 0) {
115 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
121 if (!(--clk->usecount)) {
122 if (likely(clk->ops && clk->ops->disable))
123 clk->ops->disable(clk);
124 if (likely(clk->parent))
125 __clk_disable(clk->parent);
129 void clk_disable(struct clk *clk)
136 spin_lock_irqsave(&clock_lock, flags);
138 spin_unlock_irqrestore(&clock_lock, flags);
140 EXPORT_SYMBOL_GPL(clk_disable);
142 static int __clk_enable(struct clk *clk)
146 if (clk->usecount++ == 0) {
148 ret = __clk_enable(clk->parent);
153 if (clk->ops && clk->ops->enable) {
154 ret = clk->ops->enable(clk);
157 __clk_disable(clk->parent);
169 int clk_enable(struct clk *clk)
177 spin_lock_irqsave(&clock_lock, flags);
178 ret = __clk_enable(clk);
179 spin_unlock_irqrestore(&clock_lock, flags);
183 EXPORT_SYMBOL_GPL(clk_enable);
185 static LIST_HEAD(root_clks);
188 * recalculate_root_clocks - recalculate and propagate all root clocks
190 * Recalculates all root clocks (clocks with no parent), which if the
191 * clock's .recalc is set correctly, should also propagate their rates.
194 void recalculate_root_clocks(void)
198 list_for_each_entry(clkp, &root_clks, sibling) {
199 if (clkp->ops && clkp->ops->recalc)
200 clkp->rate = clkp->ops->recalc(clkp);
201 propagate_rate(clkp);
205 int clk_register(struct clk *clk)
207 if (clk == NULL || IS_ERR(clk))
211 * trap out already registered clocks
213 if (clk->node.next || clk->node.prev)
216 mutex_lock(&clock_list_sem);
218 INIT_LIST_HEAD(&clk->children);
222 list_add(&clk->sibling, &clk->parent->children);
224 list_add(&clk->sibling, &root_clks);
226 list_add(&clk->node, &clock_list);
227 if (clk->ops && clk->ops->init)
229 mutex_unlock(&clock_list_sem);
233 EXPORT_SYMBOL_GPL(clk_register);
235 void clk_unregister(struct clk *clk)
237 mutex_lock(&clock_list_sem);
238 list_del(&clk->sibling);
239 list_del(&clk->node);
240 mutex_unlock(&clock_list_sem);
242 EXPORT_SYMBOL_GPL(clk_unregister);
244 static void clk_enable_init_clocks(void)
248 list_for_each_entry(clkp, &clock_list, node)
249 if (clkp->flags & CLK_ENABLE_ON_INIT)
253 unsigned long clk_get_rate(struct clk *clk)
257 EXPORT_SYMBOL_GPL(clk_get_rate);
259 int clk_set_rate(struct clk *clk, unsigned long rate)
261 return clk_set_rate_ex(clk, rate, 0);
263 EXPORT_SYMBOL_GPL(clk_set_rate);
265 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
267 int ret = -EOPNOTSUPP;
269 if (likely(clk->ops && clk->ops->set_rate)) {
272 spin_lock_irqsave(&clock_lock, flags);
273 ret = clk->ops->set_rate(clk, rate, algo_id);
275 if (clk->ops->recalc)
276 clk->rate = clk->ops->recalc(clk);
279 spin_unlock_irqrestore(&clock_lock, flags);
284 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
286 int clk_set_parent(struct clk *clk, struct clk *parent)
293 if (clk->parent == parent)
296 spin_lock_irqsave(&clock_lock, flags);
297 if (clk->usecount == 0) {
298 if (clk->ops->set_parent)
299 ret = clk->ops->set_parent(clk, parent);
301 ret = clk_reparent(clk, parent);
304 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
305 clk->name, clk->parent->name, clk->rate);
306 if (clk->ops->recalc)
307 clk->rate = clk->ops->recalc(clk);
312 spin_unlock_irqrestore(&clock_lock, flags);
316 EXPORT_SYMBOL_GPL(clk_set_parent);
318 struct clk *clk_get_parent(struct clk *clk)
322 EXPORT_SYMBOL_GPL(clk_get_parent);
324 long clk_round_rate(struct clk *clk, unsigned long rate)
326 if (likely(clk->ops && clk->ops->round_rate)) {
327 unsigned long flags, rounded;
329 spin_lock_irqsave(&clock_lock, flags);
330 rounded = clk->ops->round_rate(clk, rate);
331 spin_unlock_irqrestore(&clock_lock, flags);
336 return clk_get_rate(clk);
338 EXPORT_SYMBOL_GPL(clk_round_rate);
341 * Find the correct struct clk for the device and connection ID.
342 * We do slightly fuzzy matching here:
343 * An entry with a NULL ID is assumed to be a wildcard.
344 * If an entry has a device ID, it must match
345 * If an entry has a connection ID, it must match
346 * Then we take the most specific entry - with the following
347 * order of precidence: dev+con > dev only > con only.
349 static struct clk *clk_find(const char *dev_id, const char *con_id)
351 struct clk_lookup *p;
352 struct clk *clk = NULL;
355 list_for_each_entry(p, &clock_list, node) {
358 if (!dev_id || strcmp(p->dev_id, dev_id))
363 if (!con_id || strcmp(p->con_id, con_id))
378 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
382 mutex_lock(&clock_list_sem);
383 clk = clk_find(dev_id, con_id);
384 mutex_unlock(&clock_list_sem);
386 return clk ? clk : ERR_PTR(-ENOENT);
388 EXPORT_SYMBOL_GPL(clk_get_sys);
391 * Returns a clock. Note that we first try to use device id on the bus
392 * and clock name. If this fails, we try to use clock name only.
394 struct clk *clk_get(struct device *dev, const char *id)
396 const char *dev_id = dev ? dev_name(dev) : NULL;
397 struct clk *p, *clk = ERR_PTR(-ENOENT);
400 clk = clk_get_sys(dev_id, id);
401 if (clk && !IS_ERR(clk))
404 if (dev == NULL || dev->bus != &platform_bus_type)
407 idno = to_platform_device(dev)->id;
409 mutex_lock(&clock_list_sem);
410 list_for_each_entry(p, &clock_list, node) {
412 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
418 list_for_each_entry(p, &clock_list, node) {
419 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
426 mutex_unlock(&clock_list_sem);
430 EXPORT_SYMBOL_GPL(clk_get);
432 void clk_put(struct clk *clk)
434 if (clk && !IS_ERR(clk))
435 module_put(clk->owner);
437 EXPORT_SYMBOL_GPL(clk_put);
439 int __init __weak arch_clk_init(void)
444 static int show_clocks(char *buf, char **start, off_t off,
445 int len, int *eof, void *data)
450 list_for_each_entry_reverse(clk, &clock_list, node) {
451 unsigned long rate = clk_get_rate(clk);
453 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
454 rate / 1000000, (rate % 1000000) / 10000,
455 (clk->usecount > 0) ? "enabled" : "disabled");
462 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
464 static pm_message_t prev_state;
467 switch (state.event) {
469 /* Resumeing from hibernation */
470 if (prev_state.event != PM_EVENT_FREEZE)
473 list_for_each_entry(clkp, &clock_list, node) {
474 if (likely(clkp->ops)) {
475 unsigned long rate = clkp->rate;
477 if (likely(clkp->ops->set_parent))
478 clkp->ops->set_parent(clkp,
480 if (likely(clkp->ops->set_rate))
481 clkp->ops->set_rate(clkp,
483 else if (likely(clkp->ops->recalc))
484 clkp->rate = clkp->ops->recalc(clkp);
488 case PM_EVENT_FREEZE:
490 case PM_EVENT_SUSPEND:
498 static int clks_sysdev_resume(struct sys_device *dev)
500 return clks_sysdev_suspend(dev, PMSG_ON);
503 static struct sysdev_class clks_sysdev_class = {
507 static struct sysdev_driver clks_sysdev_driver = {
508 .suspend = clks_sysdev_suspend,
509 .resume = clks_sysdev_resume,
512 static struct sys_device clks_sysdev_dev = {
513 .cls = &clks_sysdev_class,
516 static int __init clk_sysdev_init(void)
518 sysdev_class_register(&clks_sysdev_class);
519 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
520 sysdev_register(&clks_sysdev_dev);
524 subsys_initcall(clk_sysdev_init);
527 int __init clk_init(void)
531 BUG_ON(!master_clk.rate);
533 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
534 struct clk *clk = onchip_clocks[i];
536 arch_init_clk_ops(&clk->ops, i);
537 ret |= clk_register(clk);
540 ret |= arch_clk_init();
542 /* Kick the child clocks.. */
543 recalculate_root_clocks();
545 /* Enable the necessary init clocks */
546 clk_enable_init_clocks();
551 static int __init clk_proc_init(void)
553 struct proc_dir_entry *p;
554 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
561 subsys_initcall(clk_proc_init);