2 * linux/arch/arm/plat-omap/clock.c
4 * Copyright (C) 2004 - 2008 Nokia corporation
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
7 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/list.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/clk.h>
21 #include <linux/mutex.h>
22 #include <linux/platform_device.h>
23 #include <linux/cpufreq.h>
24 #include <linux/debugfs.h>
27 #include <plat/clock.h>
29 static LIST_HEAD(clocks);
30 static DEFINE_MUTEX(clocks_mutex);
31 static DEFINE_SPINLOCK(clockfw_lock);
33 static struct clk_functions *arch_clock;
35 /*-------------------------------------------------------------------------
36 * Standard clock functions defined in include/linux/clk.h
37 *-------------------------------------------------------------------------*/
39 /* This functions is moved to arch/arm/common/clkdev.c. For OMAP4 since
40 * clock framework is not up , it is defined here to avoid rework in
41 * every driver. Also dummy prcm reset function is added */
43 /* Dummy hooks only for OMAP4.For rest OMAPs, common clkdev is used */
44 #if defined(CONFIG_ARCH_OMAP4)
45 struct clk *clk_get(struct device *dev, const char *id)
49 EXPORT_SYMBOL(clk_get);
51 void clk_put(struct clk *clk)
54 EXPORT_SYMBOL(clk_put);
56 void omap2_clk_prepare_for_reboot(void)
59 EXPORT_SYMBOL(omap2_clk_prepare_for_reboot);
61 int clk_enable(struct clk *clk)
65 if (cpu_is_omap44xx())
66 /* OMAP4 clk framework not supported yet */
69 if (clk == NULL || IS_ERR(clk))
72 spin_lock_irqsave(&clockfw_lock, flags);
73 if (arch_clock->clk_enable)
74 ret = arch_clock->clk_enable(clk);
75 spin_unlock_irqrestore(&clockfw_lock, flags);
79 EXPORT_SYMBOL(clk_enable);
81 void clk_disable(struct clk *clk)
85 if (clk == NULL || IS_ERR(clk))
88 spin_lock_irqsave(&clockfw_lock, flags);
89 if (clk->usecount == 0) {
90 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
96 if (arch_clock->clk_disable)
97 arch_clock->clk_disable(clk);
100 spin_unlock_irqrestore(&clockfw_lock, flags);
102 EXPORT_SYMBOL(clk_disable);
104 unsigned long clk_get_rate(struct clk *clk)
107 unsigned long ret = 0;
109 if (clk == NULL || IS_ERR(clk))
112 spin_lock_irqsave(&clockfw_lock, flags);
114 spin_unlock_irqrestore(&clockfw_lock, flags);
118 EXPORT_SYMBOL(clk_get_rate);
120 /*-------------------------------------------------------------------------
121 * Optional clock functions defined in include/linux/clk.h
122 *-------------------------------------------------------------------------*/
124 long clk_round_rate(struct clk *clk, unsigned long rate)
129 if (clk == NULL || IS_ERR(clk))
132 spin_lock_irqsave(&clockfw_lock, flags);
133 if (arch_clock->clk_round_rate)
134 ret = arch_clock->clk_round_rate(clk, rate);
135 spin_unlock_irqrestore(&clockfw_lock, flags);
139 EXPORT_SYMBOL(clk_round_rate);
141 int clk_set_rate(struct clk *clk, unsigned long rate)
146 if (clk == NULL || IS_ERR(clk))
149 spin_lock_irqsave(&clockfw_lock, flags);
150 if (arch_clock->clk_set_rate)
151 ret = arch_clock->clk_set_rate(clk, rate);
154 clk->rate = clk->recalc(clk);
157 spin_unlock_irqrestore(&clockfw_lock, flags);
161 EXPORT_SYMBOL(clk_set_rate);
163 int clk_set_parent(struct clk *clk, struct clk *parent)
168 if (cpu_is_omap44xx())
169 /* OMAP4 clk framework not supported yet */
171 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
174 spin_lock_irqsave(&clockfw_lock, flags);
175 if (clk->usecount == 0) {
176 if (arch_clock->clk_set_parent)
177 ret = arch_clock->clk_set_parent(clk, parent);
180 clk->rate = clk->recalc(clk);
185 spin_unlock_irqrestore(&clockfw_lock, flags);
189 EXPORT_SYMBOL(clk_set_parent);
191 struct clk *clk_get_parent(struct clk *clk)
195 EXPORT_SYMBOL(clk_get_parent);
197 /*-------------------------------------------------------------------------
198 * OMAP specific clock functions shared between omap1 and omap2
199 *-------------------------------------------------------------------------*/
201 unsigned int __initdata mpurate;
204 * By default we use the rate set by the bootloader.
205 * You can override this with mpurate= cmdline option.
207 static int __init omap_clk_setup(char *str)
209 get_option(&str, &mpurate);
219 __setup("mpurate=", omap_clk_setup);
221 /* Used for clocks that always have same value as the parent clock */
222 unsigned long followparent_recalc(struct clk *clk)
224 return clk->parent->rate;
227 void clk_reparent(struct clk *child, struct clk *parent)
229 list_del_init(&child->sibling);
231 list_add(&child->sibling, &parent->children);
232 child->parent = parent;
234 /* now do the debugfs renaming to reattach the child
235 to the proper parent */
238 /* Propagate rate to children */
239 void propagate_rate(struct clk * tclk)
243 list_for_each_entry(clkp, &tclk->children, sibling) {
245 clkp->rate = clkp->recalc(clkp);
246 propagate_rate(clkp);
250 static LIST_HEAD(root_clks);
253 * recalculate_root_clocks - recalculate and propagate all root clocks
255 * Recalculates all root clocks (clocks with no parent), which if the
256 * clock's .recalc is set correctly, should also propagate their rates.
259 void recalculate_root_clocks(void)
263 list_for_each_entry(clkp, &root_clks, sibling) {
265 clkp->rate = clkp->recalc(clkp);
266 propagate_rate(clkp);
271 * clk_preinit - initialize any fields in the struct clk before clk init
272 * @clk: struct clk * to initialize
274 * Initialize any struct clk fields needed before normal clk initialization
275 * can run. No return value.
277 void clk_preinit(struct clk *clk)
279 INIT_LIST_HEAD(&clk->children);
282 int clk_register(struct clk *clk)
284 if (clk == NULL || IS_ERR(clk))
288 * trap out already registered clocks
290 if (clk->node.next || clk->node.prev)
293 mutex_lock(&clocks_mutex);
295 list_add(&clk->sibling, &clk->parent->children);
297 list_add(&clk->sibling, &root_clks);
299 list_add(&clk->node, &clocks);
302 mutex_unlock(&clocks_mutex);
306 EXPORT_SYMBOL(clk_register);
308 void clk_unregister(struct clk *clk)
310 if (clk == NULL || IS_ERR(clk))
313 mutex_lock(&clocks_mutex);
314 list_del(&clk->sibling);
315 list_del(&clk->node);
316 mutex_unlock(&clocks_mutex);
318 EXPORT_SYMBOL(clk_unregister);
320 void clk_enable_init_clocks(void)
324 list_for_each_entry(clkp, &clocks, node) {
325 if (clkp->flags & ENABLE_ON_INIT)
329 EXPORT_SYMBOL(clk_enable_init_clocks);
334 static int clkll_enable_null(struct clk *clk)
339 static void clkll_disable_null(struct clk *clk)
343 const struct clkops clkops_null = {
344 .enable = clkll_enable_null,
345 .disable = clkll_disable_null,
348 #ifdef CONFIG_CPU_FREQ
349 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
353 spin_lock_irqsave(&clockfw_lock, flags);
354 if (arch_clock->clk_init_cpufreq_table)
355 arch_clock->clk_init_cpufreq_table(table);
356 spin_unlock_irqrestore(&clockfw_lock, flags);
358 EXPORT_SYMBOL(clk_init_cpufreq_table);
361 /*-------------------------------------------------------------------------*/
363 #ifdef CONFIG_OMAP_RESET_CLOCKS
365 * Disable any unused clocks left on by the bootloader
367 static int __init clk_disable_unused(void)
372 list_for_each_entry(ck, &clocks, node) {
373 if (ck->ops == &clkops_null)
376 if (ck->usecount > 0 || ck->enable_reg == 0)
379 spin_lock_irqsave(&clockfw_lock, flags);
380 if (arch_clock->clk_disable_unused)
381 arch_clock->clk_disable_unused(ck);
382 spin_unlock_irqrestore(&clockfw_lock, flags);
387 late_initcall(clk_disable_unused);
390 int __init clk_init(struct clk_functions * custom_clocks)
392 if (!custom_clocks) {
393 printk(KERN_ERR "No custom clock functions registered\n");
397 arch_clock = custom_clocks;
402 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
404 * debugfs support to trace clock tree hierarchy and attributes
406 static struct dentry *clk_debugfs_root;
408 static int clk_debugfs_register_one(struct clk *c)
411 struct dentry *d, *child;
412 struct clk *pa = c->parent;
416 p += sprintf(p, "%s", c->name);
418 sprintf(p, ":%d", c->id);
419 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
424 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
429 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
434 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
443 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
444 debugfs_remove(child);
445 debugfs_remove(c->dent);
449 static int clk_debugfs_register(struct clk *c)
452 struct clk *pa = c->parent;
454 if (pa && !pa->dent) {
455 err = clk_debugfs_register(pa);
461 err = clk_debugfs_register_one(c);
468 static int __init clk_debugfs_init(void)
474 d = debugfs_create_dir("clock", NULL);
477 clk_debugfs_root = d;
479 list_for_each_entry(c, &clocks, node) {
480 err = clk_debugfs_register(c);
486 debugfs_remove_recursive(clk_debugfs_root);
489 late_initcall(clk_debugfs_init);
491 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */