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 <mach/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 #ifndef CONFIG_COMMON_CLKDEV
41 * Returns a clock. Note that we first try to use device id on the bus
42 * and clock name. If this fails, we try to use clock name only.
44 struct clk * clk_get(struct device *dev, const char *id)
46 struct clk *p, *clk = ERR_PTR(-ENOENT);
49 if (dev == NULL || dev->bus != &platform_bus_type)
52 idno = to_platform_device(dev)->id;
54 mutex_lock(&clocks_mutex);
56 list_for_each_entry(p, &clocks, node) {
57 if (p->id == idno && strcmp(id, p->name) == 0) {
63 list_for_each_entry(p, &clocks, node) {
64 if (strcmp(id, p->name) == 0) {
71 mutex_unlock(&clocks_mutex);
75 EXPORT_SYMBOL(clk_get);
78 int clk_enable(struct clk *clk)
83 if (clk == NULL || IS_ERR(clk))
86 spin_lock_irqsave(&clockfw_lock, flags);
87 if (arch_clock->clk_enable)
88 ret = arch_clock->clk_enable(clk);
89 spin_unlock_irqrestore(&clockfw_lock, flags);
93 EXPORT_SYMBOL(clk_enable);
95 void clk_disable(struct clk *clk)
99 if (clk == NULL || IS_ERR(clk))
102 spin_lock_irqsave(&clockfw_lock, flags);
103 if (clk->usecount == 0) {
104 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
110 if (arch_clock->clk_disable)
111 arch_clock->clk_disable(clk);
114 spin_unlock_irqrestore(&clockfw_lock, flags);
116 EXPORT_SYMBOL(clk_disable);
118 int clk_get_usecount(struct clk *clk)
123 if (clk == NULL || IS_ERR(clk))
126 spin_lock_irqsave(&clockfw_lock, flags);
128 spin_unlock_irqrestore(&clockfw_lock, flags);
132 EXPORT_SYMBOL(clk_get_usecount);
134 unsigned long clk_get_rate(struct clk *clk)
137 unsigned long ret = 0;
139 if (clk == NULL || IS_ERR(clk))
142 spin_lock_irqsave(&clockfw_lock, flags);
144 spin_unlock_irqrestore(&clockfw_lock, flags);
148 EXPORT_SYMBOL(clk_get_rate);
150 #ifndef CONFIG_COMMON_CLKDEV
151 void clk_put(struct clk *clk)
154 EXPORT_SYMBOL(clk_put);
157 /*-------------------------------------------------------------------------
158 * Optional clock functions defined in include/linux/clk.h
159 *-------------------------------------------------------------------------*/
161 long clk_round_rate(struct clk *clk, unsigned long rate)
166 if (clk == NULL || IS_ERR(clk))
169 spin_lock_irqsave(&clockfw_lock, flags);
170 if (arch_clock->clk_round_rate)
171 ret = arch_clock->clk_round_rate(clk, rate);
172 spin_unlock_irqrestore(&clockfw_lock, flags);
176 EXPORT_SYMBOL(clk_round_rate);
178 int clk_set_rate(struct clk *clk, unsigned long rate)
183 if (clk == NULL || IS_ERR(clk))
186 spin_lock_irqsave(&clockfw_lock, flags);
187 if (arch_clock->clk_set_rate)
188 ret = arch_clock->clk_set_rate(clk, rate);
189 if (ret == 0 && (clk->flags & RATE_PROPAGATES))
191 spin_unlock_irqrestore(&clockfw_lock, flags);
195 EXPORT_SYMBOL(clk_set_rate);
197 int clk_set_parent(struct clk *clk, struct clk *parent)
202 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
205 spin_lock_irqsave(&clockfw_lock, flags);
206 if (arch_clock->clk_set_parent)
207 ret = arch_clock->clk_set_parent(clk, parent);
208 if (ret == 0 && (clk->flags & RATE_PROPAGATES))
210 spin_unlock_irqrestore(&clockfw_lock, flags);
214 EXPORT_SYMBOL(clk_set_parent);
216 struct clk *clk_get_parent(struct clk *clk)
220 EXPORT_SYMBOL(clk_get_parent);
222 /*-------------------------------------------------------------------------
223 * OMAP specific clock functions shared between omap1 and omap2
224 *-------------------------------------------------------------------------*/
226 unsigned int __initdata mpurate;
229 * By default we use the rate set by the bootloader.
230 * You can override this with mpurate= cmdline option.
232 static int __init omap_clk_setup(char *str)
234 get_option(&str, &mpurate);
244 __setup("mpurate=", omap_clk_setup);
246 /* Used for clocks that always have same value as the parent clock */
247 void followparent_recalc(struct clk *clk)
249 if (clk == NULL || IS_ERR(clk))
252 clk->rate = clk->parent->rate;
255 /* Propagate rate to children */
256 void propagate_rate(struct clk * tclk)
260 if (tclk == NULL || IS_ERR(tclk))
263 list_for_each_entry(clkp, &clocks, node) {
264 if (likely(clkp->parent != tclk))
268 if (clkp->flags & RATE_PROPAGATES)
269 propagate_rate(clkp);
274 * recalculate_root_clocks - recalculate and propagate all root clocks
276 * Recalculates all root clocks (clocks with no parent), which if the
277 * clock's .recalc is set correctly, should also propagate their rates.
280 void recalculate_root_clocks(void)
284 list_for_each_entry(clkp, &clocks, node) {
288 if (clkp->flags & RATE_PROPAGATES)
289 propagate_rate(clkp);
294 int clk_register(struct clk *clk)
296 if (clk == NULL || IS_ERR(clk))
300 * trap out already registered clocks
302 if (clk->node.next || clk->node.prev)
305 mutex_lock(&clocks_mutex);
306 list_add(&clk->node, &clocks);
309 mutex_unlock(&clocks_mutex);
313 EXPORT_SYMBOL(clk_register);
315 void clk_unregister(struct clk *clk)
317 if (clk == NULL || IS_ERR(clk))
320 mutex_lock(&clocks_mutex);
321 list_del(&clk->node);
322 mutex_unlock(&clocks_mutex);
324 EXPORT_SYMBOL(clk_unregister);
326 void clk_enable_init_clocks(void)
330 list_for_each_entry(clkp, &clocks, node) {
331 if (clkp->flags & ENABLE_ON_INIT)
335 EXPORT_SYMBOL(clk_enable_init_clocks);
340 static int clkll_enable_null(struct clk *clk)
345 static void clkll_disable_null(struct clk *clk)
349 const struct clkops clkops_null = {
350 .enable = clkll_enable_null,
351 .disable = clkll_disable_null,
354 #ifdef CONFIG_CPU_FREQ
355 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
359 spin_lock_irqsave(&clockfw_lock, flags);
360 if (arch_clock->clk_init_cpufreq_table)
361 arch_clock->clk_init_cpufreq_table(table);
362 spin_unlock_irqrestore(&clockfw_lock, flags);
364 EXPORT_SYMBOL(clk_init_cpufreq_table);
367 /*-------------------------------------------------------------------------*/
369 #ifdef CONFIG_OMAP_RESET_CLOCKS
371 * Disable any unused clocks left on by the bootloader
373 static int __init clk_disable_unused(void)
378 list_for_each_entry(ck, &clocks, node) {
379 if (ck->ops == &clkops_null)
382 if (ck->usecount > 0 || ck->enable_reg == 0)
385 spin_lock_irqsave(&clockfw_lock, flags);
386 if (arch_clock->clk_disable_unused)
387 arch_clock->clk_disable_unused(ck);
388 spin_unlock_irqrestore(&clockfw_lock, flags);
393 late_initcall(clk_disable_unused);
396 int __init clk_init(struct clk_functions * custom_clocks)
398 if (!custom_clocks) {
399 printk(KERN_ERR "No custom clock functions registered\n");
403 arch_clock = custom_clocks;
408 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
410 * debugfs support to trace clock tree hierarchy and attributes
412 static struct dentry *clk_debugfs_root;
414 static int clk_debugfs_register_one(struct clk *c)
417 struct dentry *d, *child;
418 struct clk *pa = c->parent;
422 p += sprintf(p, "%s", c->name);
424 sprintf(p, ":%d", c->id);
425 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
430 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
435 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
440 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
449 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
450 debugfs_remove(child);
451 debugfs_remove(c->dent);
455 static int clk_debugfs_register(struct clk *c)
458 struct clk *pa = c->parent;
460 if (pa && !pa->dent) {
461 err = clk_debugfs_register(pa);
467 err = clk_debugfs_register_one(c);
474 static int __init clk_debugfs_init(void)
480 d = debugfs_create_dir("clock", NULL);
483 clk_debugfs_root = d;
485 list_for_each_entry(c, &clocks, node) {
486 err = clk_debugfs_register(c);
492 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
495 late_initcall(clk_debugfs_init);
497 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */