[ARM] omap: allow double-registering of clocks
[ipaq214.git] / arch / arm / plat-omap / clock.c
blob6b3ef2a0b04eafb53de7fc2cf925384f301acf61
1 /*
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>
25 #include <linux/io.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 *-------------------------------------------------------------------------*/
40 * Returns a clock. Note that we first try to use device id on the bus
41 * and clock name. If this fails, we try to use clock name only.
43 struct clk * clk_get(struct device *dev, const char *id)
45 struct clk *p, *clk = ERR_PTR(-ENOENT);
46 int idno;
48 if (dev == NULL || dev->bus != &platform_bus_type)
49 idno = -1;
50 else
51 idno = to_platform_device(dev)->id;
53 mutex_lock(&clocks_mutex);
55 list_for_each_entry(p, &clocks, node) {
56 if (p->id == idno && strcmp(id, p->name) == 0) {
57 clk = p;
58 goto found;
62 list_for_each_entry(p, &clocks, node) {
63 if (strcmp(id, p->name) == 0) {
64 clk = p;
65 break;
69 found:
70 mutex_unlock(&clocks_mutex);
72 return clk;
74 EXPORT_SYMBOL(clk_get);
76 int clk_enable(struct clk *clk)
78 unsigned long flags;
79 int ret = 0;
81 if (clk == NULL || IS_ERR(clk))
82 return -EINVAL;
84 spin_lock_irqsave(&clockfw_lock, flags);
85 if (arch_clock->clk_enable)
86 ret = arch_clock->clk_enable(clk);
87 spin_unlock_irqrestore(&clockfw_lock, flags);
89 return ret;
91 EXPORT_SYMBOL(clk_enable);
93 void clk_disable(struct clk *clk)
95 unsigned long flags;
97 if (clk == NULL || IS_ERR(clk))
98 return;
100 spin_lock_irqsave(&clockfw_lock, flags);
101 if (clk->usecount == 0) {
102 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
103 clk->name);
104 WARN_ON(1);
105 goto out;
108 if (arch_clock->clk_disable)
109 arch_clock->clk_disable(clk);
111 out:
112 spin_unlock_irqrestore(&clockfw_lock, flags);
114 EXPORT_SYMBOL(clk_disable);
116 int clk_get_usecount(struct clk *clk)
118 unsigned long flags;
119 int ret = 0;
121 if (clk == NULL || IS_ERR(clk))
122 return 0;
124 spin_lock_irqsave(&clockfw_lock, flags);
125 ret = clk->usecount;
126 spin_unlock_irqrestore(&clockfw_lock, flags);
128 return ret;
130 EXPORT_SYMBOL(clk_get_usecount);
132 unsigned long clk_get_rate(struct clk *clk)
134 unsigned long flags;
135 unsigned long ret = 0;
137 if (clk == NULL || IS_ERR(clk))
138 return 0;
140 spin_lock_irqsave(&clockfw_lock, flags);
141 ret = clk->rate;
142 spin_unlock_irqrestore(&clockfw_lock, flags);
144 return ret;
146 EXPORT_SYMBOL(clk_get_rate);
148 void clk_put(struct clk *clk)
151 EXPORT_SYMBOL(clk_put);
153 /*-------------------------------------------------------------------------
154 * Optional clock functions defined in include/linux/clk.h
155 *-------------------------------------------------------------------------*/
157 long clk_round_rate(struct clk *clk, unsigned long rate)
159 unsigned long flags;
160 long ret = 0;
162 if (clk == NULL || IS_ERR(clk))
163 return ret;
165 spin_lock_irqsave(&clockfw_lock, flags);
166 if (arch_clock->clk_round_rate)
167 ret = arch_clock->clk_round_rate(clk, rate);
168 spin_unlock_irqrestore(&clockfw_lock, flags);
170 return ret;
172 EXPORT_SYMBOL(clk_round_rate);
174 int clk_set_rate(struct clk *clk, unsigned long rate)
176 unsigned long flags;
177 int ret = -EINVAL;
179 if (clk == NULL || IS_ERR(clk))
180 return ret;
182 spin_lock_irqsave(&clockfw_lock, flags);
183 if (arch_clock->clk_set_rate)
184 ret = arch_clock->clk_set_rate(clk, rate);
185 if (ret == 0 && (clk->flags & RATE_PROPAGATES))
186 propagate_rate(clk);
187 spin_unlock_irqrestore(&clockfw_lock, flags);
189 return ret;
191 EXPORT_SYMBOL(clk_set_rate);
193 int clk_set_parent(struct clk *clk, struct clk *parent)
195 unsigned long flags;
196 int ret = -EINVAL;
198 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
199 return ret;
201 spin_lock_irqsave(&clockfw_lock, flags);
202 if (arch_clock->clk_set_parent)
203 ret = arch_clock->clk_set_parent(clk, parent);
204 if (ret == 0 && (clk->flags & RATE_PROPAGATES))
205 propagate_rate(clk);
206 spin_unlock_irqrestore(&clockfw_lock, flags);
208 return ret;
210 EXPORT_SYMBOL(clk_set_parent);
212 struct clk *clk_get_parent(struct clk *clk)
214 return clk->parent;
216 EXPORT_SYMBOL(clk_get_parent);
218 /*-------------------------------------------------------------------------
219 * OMAP specific clock functions shared between omap1 and omap2
220 *-------------------------------------------------------------------------*/
222 unsigned int __initdata mpurate;
225 * By default we use the rate set by the bootloader.
226 * You can override this with mpurate= cmdline option.
228 static int __init omap_clk_setup(char *str)
230 get_option(&str, &mpurate);
232 if (!mpurate)
233 return 1;
235 if (mpurate < 1000)
236 mpurate *= 1000000;
238 return 1;
240 __setup("mpurate=", omap_clk_setup);
242 /* Used for clocks that always have same value as the parent clock */
243 void followparent_recalc(struct clk *clk)
245 if (clk == NULL || IS_ERR(clk))
246 return;
248 clk->rate = clk->parent->rate;
251 /* Propagate rate to children */
252 void propagate_rate(struct clk * tclk)
254 struct clk *clkp;
256 if (tclk == NULL || IS_ERR(tclk))
257 return;
259 list_for_each_entry(clkp, &clocks, node) {
260 if (likely(clkp->parent != tclk))
261 continue;
262 if (clkp->recalc)
263 clkp->recalc(clkp);
264 if (clkp->flags & RATE_PROPAGATES)
265 propagate_rate(clkp);
270 * recalculate_root_clocks - recalculate and propagate all root clocks
272 * Recalculates all root clocks (clocks with no parent), which if the
273 * clock's .recalc is set correctly, should also propagate their rates.
274 * Called at init.
276 void recalculate_root_clocks(void)
278 struct clk *clkp;
280 list_for_each_entry(clkp, &clocks, node) {
281 if (!clkp->parent) {
282 if (clkp->recalc)
283 clkp->recalc(clkp);
284 if (clkp->flags & RATE_PROPAGATES)
285 propagate_rate(clkp);
290 int clk_register(struct clk *clk)
292 if (clk == NULL || IS_ERR(clk))
293 return -EINVAL;
296 * trap out already registered clocks
298 if (clk->node.next || clk->node.prev)
299 return 0;
301 mutex_lock(&clocks_mutex);
302 list_add(&clk->node, &clocks);
303 if (clk->init)
304 clk->init(clk);
305 mutex_unlock(&clocks_mutex);
307 return 0;
309 EXPORT_SYMBOL(clk_register);
311 void clk_unregister(struct clk *clk)
313 if (clk == NULL || IS_ERR(clk))
314 return;
316 mutex_lock(&clocks_mutex);
317 list_del(&clk->node);
318 mutex_unlock(&clocks_mutex);
320 EXPORT_SYMBOL(clk_unregister);
322 void clk_enable_init_clocks(void)
324 struct clk *clkp;
326 list_for_each_entry(clkp, &clocks, node) {
327 if (clkp->flags & ENABLE_ON_INIT)
328 clk_enable(clkp);
331 EXPORT_SYMBOL(clk_enable_init_clocks);
334 * Low level helpers
336 static int clkll_enable_null(struct clk *clk)
338 return 0;
341 static void clkll_disable_null(struct clk *clk)
345 const struct clkops clkops_null = {
346 .enable = clkll_enable_null,
347 .disable = clkll_disable_null,
350 #ifdef CONFIG_CPU_FREQ
351 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
353 unsigned long flags;
355 spin_lock_irqsave(&clockfw_lock, flags);
356 if (arch_clock->clk_init_cpufreq_table)
357 arch_clock->clk_init_cpufreq_table(table);
358 spin_unlock_irqrestore(&clockfw_lock, flags);
360 EXPORT_SYMBOL(clk_init_cpufreq_table);
361 #endif
363 /*-------------------------------------------------------------------------*/
365 #ifdef CONFIG_OMAP_RESET_CLOCKS
367 * Disable any unused clocks left on by the bootloader
369 static int __init clk_disable_unused(void)
371 struct clk *ck;
372 unsigned long flags;
374 list_for_each_entry(ck, &clocks, node) {
375 if (ck->ops == &clkops_null)
376 continue;
378 if (ck->usecount > 0 || ck->enable_reg == 0)
379 continue;
381 spin_lock_irqsave(&clockfw_lock, flags);
382 if (arch_clock->clk_disable_unused)
383 arch_clock->clk_disable_unused(ck);
384 spin_unlock_irqrestore(&clockfw_lock, flags);
387 return 0;
389 late_initcall(clk_disable_unused);
390 #endif
392 int __init clk_init(struct clk_functions * custom_clocks)
394 if (!custom_clocks) {
395 printk(KERN_ERR "No custom clock functions registered\n");
396 BUG();
399 arch_clock = custom_clocks;
401 return 0;
404 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
406 * debugfs support to trace clock tree hierarchy and attributes
408 static struct dentry *clk_debugfs_root;
410 static int clk_debugfs_register_one(struct clk *c)
412 int err;
413 struct dentry *d, *child;
414 struct clk *pa = c->parent;
415 char s[255];
416 char *p = s;
418 p += sprintf(p, "%s", c->name);
419 if (c->id != 0)
420 sprintf(p, ":%d", c->id);
421 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
422 if (!d)
423 return -ENOMEM;
424 c->dent = d;
426 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
427 if (!d) {
428 err = -ENOMEM;
429 goto err_out;
431 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
432 if (!d) {
433 err = -ENOMEM;
434 goto err_out;
436 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
437 if (!d) {
438 err = -ENOMEM;
439 goto err_out;
441 return 0;
443 err_out:
444 d = c->dent;
445 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
446 debugfs_remove(child);
447 debugfs_remove(c->dent);
448 return err;
451 static int clk_debugfs_register(struct clk *c)
453 int err;
454 struct clk *pa = c->parent;
456 if (pa && !pa->dent) {
457 err = clk_debugfs_register(pa);
458 if (err)
459 return err;
462 if (!c->dent) {
463 err = clk_debugfs_register_one(c);
464 if (err)
465 return err;
467 return 0;
470 static int __init clk_debugfs_init(void)
472 struct clk *c;
473 struct dentry *d;
474 int err;
476 d = debugfs_create_dir("clock", NULL);
477 if (!d)
478 return -ENOMEM;
479 clk_debugfs_root = d;
481 list_for_each_entry(c, &clocks, node) {
482 err = clk_debugfs_register(c);
483 if (err)
484 goto err_out;
486 return 0;
487 err_out:
488 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
489 return err;
491 late_initcall(clk_debugfs_init);
493 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */