2 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
4 * Copyright (C) 2005, 2006, 2007 Paul Mundt
6 * This clock framework is derived from the OMAP version by:
8 * Copyright (C) 2004 - 2005 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/proc_fs.h>
28 #include <asm/clock.h>
30 static LIST_HEAD(clock_list);
31 static DEFINE_SPINLOCK(clock_lock);
32 static DEFINE_MUTEX(clock_list_sem);
35 * Each subtype is expected to define the init routines for these clocks,
36 * as each subtype (or processor family) will have these clocks at the
37 * very least. These are all provided through the CPG, which even some of
38 * the more quirky parts (such as ST40, SH4-202, etc.) still have.
40 * The processor-specific code is expected to register any additional
41 * clock sources that are of interest.
43 static struct clk master_clk = {
45 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
46 .rate = CONFIG_SH_PCLK_FREQ,
49 static struct clk module_clk = {
51 .parent = &master_clk,
52 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
55 static struct clk bus_clk = {
57 .parent = &master_clk,
58 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
61 static struct clk cpu_clk = {
63 .parent = &master_clk,
64 .flags = CLK_ALWAYS_ENABLED,
68 * The ordering of these clocks matters, do not change it.
70 static struct clk *onchip_clocks[] = {
77 static void propagate_rate(struct clk *clk)
81 list_for_each_entry(clkp, &clock_list, node) {
82 if (likely(clkp->parent != clk))
84 if (likely(clkp->ops && clkp->ops->recalc))
85 clkp->ops->recalc(clkp);
86 if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
91 static void __clk_init(struct clk *clk)
94 * See if this is the first time we're enabling the clock, some
95 * clocks that are always enabled still require "special"
96 * initialization. This is especially true if the clock mode
97 * changes and the clock needs to hunt for the proper set of
98 * divisors to use before it can effectively recalc.
101 if (clk->flags & CLK_NEEDS_INIT) {
102 if (clk->ops && clk->ops->init)
105 clk->flags &= ~CLK_NEEDS_INIT;
109 static int __clk_enable(struct clk *clk)
116 /* nothing to do if always enabled */
117 if (clk->flags & CLK_ALWAYS_ENABLED)
120 if (clk->usecount == 1) {
123 __clk_enable(clk->parent);
125 if (clk->ops && clk->ops->enable)
126 clk->ops->enable(clk);
132 int clk_enable(struct clk *clk)
137 spin_lock_irqsave(&clock_lock, flags);
138 ret = __clk_enable(clk);
139 spin_unlock_irqrestore(&clock_lock, flags);
143 EXPORT_SYMBOL_GPL(clk_enable);
145 static void __clk_disable(struct clk *clk)
152 WARN_ON(clk->usecount < 0);
154 if (clk->flags & CLK_ALWAYS_ENABLED)
157 if (clk->usecount == 0) {
158 if (likely(clk->ops && clk->ops->disable))
159 clk->ops->disable(clk);
161 __clk_disable(clk->parent);
165 void clk_disable(struct clk *clk)
169 spin_lock_irqsave(&clock_lock, flags);
171 spin_unlock_irqrestore(&clock_lock, flags);
173 EXPORT_SYMBOL_GPL(clk_disable);
175 int clk_register(struct clk *clk)
177 mutex_lock(&clock_list_sem);
179 list_add(&clk->node, &clock_list);
181 clk->flags |= CLK_NEEDS_INIT;
183 mutex_unlock(&clock_list_sem);
185 if (clk->flags & CLK_ALWAYS_ENABLED) {
187 pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name);
188 if (clk->ops && clk->ops->enable)
189 clk->ops->enable(clk);
190 pr_debug( "Enabled.");
195 EXPORT_SYMBOL_GPL(clk_register);
197 void clk_unregister(struct clk *clk)
199 mutex_lock(&clock_list_sem);
200 list_del(&clk->node);
201 mutex_unlock(&clock_list_sem);
203 EXPORT_SYMBOL_GPL(clk_unregister);
205 unsigned long clk_get_rate(struct clk *clk)
209 EXPORT_SYMBOL_GPL(clk_get_rate);
211 int clk_set_rate(struct clk *clk, unsigned long rate)
213 return clk_set_rate_ex(clk, rate, 0);
215 EXPORT_SYMBOL_GPL(clk_set_rate);
217 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
219 int ret = -EOPNOTSUPP;
221 if (likely(clk->ops && clk->ops->set_rate)) {
224 spin_lock_irqsave(&clock_lock, flags);
225 ret = clk->ops->set_rate(clk, rate, algo_id);
226 spin_unlock_irqrestore(&clock_lock, flags);
229 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
234 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
236 void clk_recalc_rate(struct clk *clk)
238 if (likely(clk->ops && clk->ops->recalc)) {
241 spin_lock_irqsave(&clock_lock, flags);
242 clk->ops->recalc(clk);
243 spin_unlock_irqrestore(&clock_lock, flags);
246 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
249 EXPORT_SYMBOL_GPL(clk_recalc_rate);
251 int clk_set_parent(struct clk *clk, struct clk *parent)
260 if (likely(clk->ops && clk->ops->set_parent)) {
262 spin_lock_irqsave(&clock_lock, flags);
263 ret = clk->ops->set_parent(clk, parent);
264 spin_unlock_irqrestore(&clock_lock, flags);
265 clk->parent = (ret ? old : parent);
268 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
272 EXPORT_SYMBOL_GPL(clk_set_parent);
274 struct clk *clk_get_parent(struct clk *clk)
278 EXPORT_SYMBOL_GPL(clk_get_parent);
280 long clk_round_rate(struct clk *clk, unsigned long rate)
282 if (likely(clk->ops && clk->ops->round_rate)) {
283 unsigned long flags, rounded;
285 spin_lock_irqsave(&clock_lock, flags);
286 rounded = clk->ops->round_rate(clk, rate);
287 spin_unlock_irqrestore(&clock_lock, flags);
292 return clk_get_rate(clk);
294 EXPORT_SYMBOL_GPL(clk_round_rate);
297 * Returns a clock. Note that we first try to use device id on the bus
298 * and clock name. If this fails, we try to use clock name only.
300 struct clk *clk_get(struct device *dev, const char *id)
302 struct clk *p, *clk = ERR_PTR(-ENOENT);
305 if (dev == NULL || dev->bus != &platform_bus_type)
308 idno = to_platform_device(dev)->id;
310 mutex_lock(&clock_list_sem);
311 list_for_each_entry(p, &clock_list, node) {
313 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
319 list_for_each_entry(p, &clock_list, node) {
320 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
327 mutex_unlock(&clock_list_sem);
331 EXPORT_SYMBOL_GPL(clk_get);
333 void clk_put(struct clk *clk)
335 if (clk && !IS_ERR(clk))
336 module_put(clk->owner);
338 EXPORT_SYMBOL_GPL(clk_put);
340 void __init __attribute__ ((weak))
341 arch_init_clk_ops(struct clk_ops **ops, int type)
345 int __init __attribute__ ((weak))
351 static int show_clocks(char *buf, char **start, off_t off,
352 int len, int *eof, void *data)
357 list_for_each_entry_reverse(clk, &clock_list, node) {
358 unsigned long rate = clk_get_rate(clk);
360 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
361 rate / 1000000, (rate % 1000000) / 10000,
362 ((clk->flags & CLK_ALWAYS_ENABLED) ||
364 "enabled" : "disabled");
371 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
373 static pm_message_t prev_state;
376 switch (state.event) {
378 /* Resumeing from hibernation */
379 if (prev_state.event == PM_EVENT_FREEZE) {
380 list_for_each_entry(clkp, &clock_list, node)
381 if (likely(clkp->ops)) {
382 unsigned long rate = clkp->rate;
384 if (likely(clkp->ops->set_parent))
385 clkp->ops->set_parent(clkp,
387 if (likely(clkp->ops->set_rate))
388 clkp->ops->set_rate(clkp,
390 else if (likely(clkp->ops->recalc))
391 clkp->ops->recalc(clkp);
395 case PM_EVENT_FREEZE:
397 case PM_EVENT_SUSPEND:
405 static int clks_sysdev_resume(struct sys_device *dev)
407 return clks_sysdev_suspend(dev, PMSG_ON);
410 static struct sysdev_class clks_sysdev_class = {
414 static struct sysdev_driver clks_sysdev_driver = {
415 .suspend = clks_sysdev_suspend,
416 .resume = clks_sysdev_resume,
419 static struct sys_device clks_sysdev_dev = {
420 .cls = &clks_sysdev_class,
423 static int __init clk_sysdev_init(void)
425 sysdev_class_register(&clks_sysdev_class);
426 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
427 sysdev_register(&clks_sysdev_dev);
431 subsys_initcall(clk_sysdev_init);
434 int __init clk_init(void)
438 BUG_ON(!master_clk.rate);
440 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
441 struct clk *clk = onchip_clocks[i];
443 arch_init_clk_ops(&clk->ops, i);
444 ret |= clk_register(clk);
447 ret |= arch_clk_init();
449 /* Kick the child clocks.. */
450 propagate_rate(&master_clk);
451 propagate_rate(&bus_clk);
456 static int __init clk_proc_init(void)
458 struct proc_dir_entry *p;
459 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
466 subsys_initcall(clk_proc_init);