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/machvec.h>
35 static LIST_HEAD(clock_list);
36 static DEFINE_SPINLOCK(clock_lock);
37 static DEFINE_MUTEX(clock_list_sem);
39 /* Used for clocks that always have same value as the parent clock */
40 unsigned long followparent_recalc(struct clk *clk)
42 return clk->parent->rate;
45 int clk_reparent(struct clk *child, struct clk *parent)
47 list_del_init(&child->sibling);
49 list_add(&child->sibling, &parent->children);
50 child->parent = parent;
52 /* now do the debugfs renaming to reattach the child
53 to the proper parent */
58 /* Propagate rate to children */
59 void propagate_rate(struct clk *tclk)
63 list_for_each_entry(clkp, &tclk->children, sibling) {
64 if (clkp->ops && clkp->ops->recalc)
65 clkp->rate = clkp->ops->recalc(clkp);
70 static void __clk_disable(struct clk *clk)
72 if (clk->usecount == 0) {
73 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
79 if (!(--clk->usecount)) {
80 if (likely(clk->ops && clk->ops->disable))
81 clk->ops->disable(clk);
82 if (likely(clk->parent))
83 __clk_disable(clk->parent);
87 void clk_disable(struct clk *clk)
94 spin_lock_irqsave(&clock_lock, flags);
96 spin_unlock_irqrestore(&clock_lock, flags);
98 EXPORT_SYMBOL_GPL(clk_disable);
100 static int __clk_enable(struct clk *clk)
104 if (clk->usecount++ == 0) {
106 ret = __clk_enable(clk->parent);
111 if (clk->ops && clk->ops->enable) {
112 ret = clk->ops->enable(clk);
115 __clk_disable(clk->parent);
127 int clk_enable(struct clk *clk)
135 spin_lock_irqsave(&clock_lock, flags);
136 ret = __clk_enable(clk);
137 spin_unlock_irqrestore(&clock_lock, flags);
141 EXPORT_SYMBOL_GPL(clk_enable);
143 static LIST_HEAD(root_clks);
146 * recalculate_root_clocks - recalculate and propagate all root clocks
148 * Recalculates all root clocks (clocks with no parent), which if the
149 * clock's .recalc is set correctly, should also propagate their rates.
152 void recalculate_root_clocks(void)
156 list_for_each_entry(clkp, &root_clks, sibling) {
157 if (clkp->ops && clkp->ops->recalc)
158 clkp->rate = clkp->ops->recalc(clkp);
159 propagate_rate(clkp);
163 int clk_register(struct clk *clk)
165 if (clk == NULL || IS_ERR(clk))
169 * trap out already registered clocks
171 if (clk->node.next || clk->node.prev)
174 mutex_lock(&clock_list_sem);
176 INIT_LIST_HEAD(&clk->children);
180 list_add(&clk->sibling, &clk->parent->children);
182 list_add(&clk->sibling, &root_clks);
184 list_add(&clk->node, &clock_list);
185 if (clk->ops && clk->ops->init)
187 mutex_unlock(&clock_list_sem);
191 EXPORT_SYMBOL_GPL(clk_register);
193 void clk_unregister(struct clk *clk)
195 mutex_lock(&clock_list_sem);
196 list_del(&clk->sibling);
197 list_del(&clk->node);
198 mutex_unlock(&clock_list_sem);
200 EXPORT_SYMBOL_GPL(clk_unregister);
202 static void clk_enable_init_clocks(void)
206 list_for_each_entry(clkp, &clock_list, node)
207 if (clkp->flags & CLK_ENABLE_ON_INIT)
211 unsigned long clk_get_rate(struct clk *clk)
215 EXPORT_SYMBOL_GPL(clk_get_rate);
217 int clk_set_rate(struct clk *clk, unsigned long rate)
219 return clk_set_rate_ex(clk, rate, 0);
221 EXPORT_SYMBOL_GPL(clk_set_rate);
223 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
225 int ret = -EOPNOTSUPP;
228 spin_lock_irqsave(&clock_lock, flags);
230 if (likely(clk->ops && clk->ops->set_rate)) {
231 ret = clk->ops->set_rate(clk, rate, algo_id);
239 if (clk->ops && clk->ops->recalc)
240 clk->rate = clk->ops->recalc(clk);
245 spin_unlock_irqrestore(&clock_lock, flags);
249 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
251 int clk_set_parent(struct clk *clk, struct clk *parent)
258 if (clk->parent == parent)
261 spin_lock_irqsave(&clock_lock, flags);
262 if (clk->usecount == 0) {
263 if (clk->ops->set_parent)
264 ret = clk->ops->set_parent(clk, parent);
266 ret = clk_reparent(clk, parent);
269 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
270 clk->name, clk->parent->name, clk->rate);
271 if (clk->ops->recalc)
272 clk->rate = clk->ops->recalc(clk);
277 spin_unlock_irqrestore(&clock_lock, flags);
281 EXPORT_SYMBOL_GPL(clk_set_parent);
283 struct clk *clk_get_parent(struct clk *clk)
287 EXPORT_SYMBOL_GPL(clk_get_parent);
289 long clk_round_rate(struct clk *clk, unsigned long rate)
291 if (likely(clk->ops && clk->ops->round_rate)) {
292 unsigned long flags, rounded;
294 spin_lock_irqsave(&clock_lock, flags);
295 rounded = clk->ops->round_rate(clk, rate);
296 spin_unlock_irqrestore(&clock_lock, flags);
301 return clk_get_rate(clk);
303 EXPORT_SYMBOL_GPL(clk_round_rate);
306 * Find the correct struct clk for the device and connection ID.
307 * We do slightly fuzzy matching here:
308 * An entry with a NULL ID is assumed to be a wildcard.
309 * If an entry has a device ID, it must match
310 * If an entry has a connection ID, it must match
311 * Then we take the most specific entry - with the following
312 * order of precidence: dev+con > dev only > con only.
314 static struct clk *clk_find(const char *dev_id, const char *con_id)
316 struct clk_lookup *p;
317 struct clk *clk = NULL;
320 list_for_each_entry(p, &clock_list, node) {
323 if (!dev_id || strcmp(p->dev_id, dev_id))
328 if (!con_id || strcmp(p->con_id, con_id))
343 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
347 mutex_lock(&clock_list_sem);
348 clk = clk_find(dev_id, con_id);
349 mutex_unlock(&clock_list_sem);
351 return clk ? clk : ERR_PTR(-ENOENT);
353 EXPORT_SYMBOL_GPL(clk_get_sys);
356 * Returns a clock. Note that we first try to use device id on the bus
357 * and clock name. If this fails, we try to use clock name only.
359 struct clk *clk_get(struct device *dev, const char *id)
361 const char *dev_id = dev ? dev_name(dev) : NULL;
362 struct clk *p, *clk = ERR_PTR(-ENOENT);
365 clk = clk_get_sys(dev_id, id);
366 if (clk && !IS_ERR(clk))
369 if (dev == NULL || dev->bus != &platform_bus_type)
372 idno = to_platform_device(dev)->id;
374 mutex_lock(&clock_list_sem);
375 list_for_each_entry(p, &clock_list, node) {
377 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
383 list_for_each_entry(p, &clock_list, node) {
384 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
391 mutex_unlock(&clock_list_sem);
395 EXPORT_SYMBOL_GPL(clk_get);
397 void clk_put(struct clk *clk)
399 if (clk && !IS_ERR(clk))
400 module_put(clk->owner);
402 EXPORT_SYMBOL_GPL(clk_put);
405 static int show_clocks(char *buf, char **start, off_t off,
406 int len, int *eof, void *data)
411 list_for_each_entry_reverse(clk, &clock_list, node) {
412 unsigned long rate = clk_get_rate(clk);
414 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
415 rate / 1000000, (rate % 1000000) / 10000,
416 (clk->usecount > 0) ? "enabled" : "disabled");
423 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
425 static pm_message_t prev_state;
428 switch (state.event) {
430 /* Resumeing from hibernation */
431 if (prev_state.event != PM_EVENT_FREEZE)
434 list_for_each_entry(clkp, &clock_list, node) {
435 if (likely(clkp->ops)) {
436 unsigned long rate = clkp->rate;
438 if (likely(clkp->ops->set_parent))
439 clkp->ops->set_parent(clkp,
441 if (likely(clkp->ops->set_rate))
442 clkp->ops->set_rate(clkp,
444 else if (likely(clkp->ops->recalc))
445 clkp->rate = clkp->ops->recalc(clkp);
449 case PM_EVENT_FREEZE:
451 case PM_EVENT_SUSPEND:
459 static int clks_sysdev_resume(struct sys_device *dev)
461 return clks_sysdev_suspend(dev, PMSG_ON);
464 static struct sysdev_class clks_sysdev_class = {
468 static struct sysdev_driver clks_sysdev_driver = {
469 .suspend = clks_sysdev_suspend,
470 .resume = clks_sysdev_resume,
473 static struct sys_device clks_sysdev_dev = {
474 .cls = &clks_sysdev_class,
477 static int __init clk_sysdev_init(void)
479 sysdev_class_register(&clks_sysdev_class);
480 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
481 sysdev_register(&clks_sysdev_dev);
485 subsys_initcall(clk_sysdev_init);
488 int __init clk_init(void)
492 ret = arch_clk_init();
494 pr_err("%s: CPU clock registration failed.\n", __func__);
498 if (sh_mv.mv_clk_init) {
499 ret = sh_mv.mv_clk_init();
501 pr_err("%s: machvec clock initialization failed.\n",
507 /* Kick the child clocks.. */
508 recalculate_root_clocks();
510 /* Enable the necessary init clocks */
511 clk_enable_init_clocks();
516 static int __init clk_proc_init(void)
518 struct proc_dir_entry *p;
519 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
526 subsys_initcall(clk_proc_init);