[ARM] omap: remove clk_deny_idle and clk_allow_idle
[linux-2.6-block.git] / arch / arm / plat-omap / clock.c
CommitLineData
1da177e4 1/*
b9158556 2 * linux/arch/arm/plat-omap/clock.c
1da177e4 3 *
137b3ee2 4 * Copyright (C) 2004 - 2008 Nokia corporation
1da177e4
LT
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6 *
1a8bfa1e
TL
7 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8 *
1da177e4
LT
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.
12 */
1da177e4 13#include <linux/kernel.h>
1a8bfa1e
TL
14#include <linux/init.h>
15#include <linux/module.h>
1da177e4
LT
16#include <linux/list.h>
17#include <linux/errno.h>
18#include <linux/err.h>
4e57b681 19#include <linux/string.h>
f8ce2547 20#include <linux/clk.h>
00431707 21#include <linux/mutex.h>
b824efae 22#include <linux/platform_device.h>
b851cb28 23#include <linux/cpufreq.h>
137b3ee2 24#include <linux/debugfs.h>
fced80c7 25#include <linux/io.h>
1da177e4 26
a09e64fb 27#include <mach/clock.h>
1da177e4 28
7df3450e 29static LIST_HEAD(clocks);
00431707 30static DEFINE_MUTEX(clocks_mutex);
7df3450e 31static DEFINE_SPINLOCK(clockfw_lock);
1da177e4 32
1a8bfa1e 33static struct clk_functions *arch_clock;
1da177e4 34
1a8bfa1e 35/*-------------------------------------------------------------------------
f07adc59 36 * Standard clock functions defined in include/linux/clk.h
1a8bfa1e 37 *-------------------------------------------------------------------------*/
1da177e4 38
b824efae
TL
39/*
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.
42 */
1a8bfa1e 43struct clk * clk_get(struct device *dev, const char *id)
1da177e4
LT
44{
45 struct clk *p, *clk = ERR_PTR(-ENOENT);
b824efae
TL
46 int idno;
47
48 if (dev == NULL || dev->bus != &platform_bus_type)
49 idno = -1;
50 else
51 idno = to_platform_device(dev)->id;
1da177e4 52
00431707 53 mutex_lock(&clocks_mutex);
b824efae
TL
54
55 list_for_each_entry(p, &clocks, node) {
eee5b191 56 if (p->id == idno && strcmp(id, p->name) == 0) {
b824efae 57 clk = p;
67d4d835 58 goto found;
b824efae
TL
59 }
60 }
61
1da177e4 62 list_for_each_entry(p, &clocks, node) {
eee5b191 63 if (strcmp(id, p->name) == 0) {
1da177e4
LT
64 clk = p;
65 break;
66 }
67 }
b824efae 68
67d4d835 69found:
00431707 70 mutex_unlock(&clocks_mutex);
1da177e4
LT
71
72 return clk;
73}
74EXPORT_SYMBOL(clk_get);
75
1da177e4
LT
76int clk_enable(struct clk *clk)
77{
78 unsigned long flags;
1a8bfa1e 79 int ret = 0;
1da177e4 80
b824efae
TL
81 if (clk == NULL || IS_ERR(clk))
82 return -EINVAL;
83
1da177e4 84 spin_lock_irqsave(&clockfw_lock, flags);
f07adc59 85 if (arch_clock->clk_enable)
1a8bfa1e 86 ret = arch_clock->clk_enable(clk);
1da177e4 87 spin_unlock_irqrestore(&clockfw_lock, flags);
1a8bfa1e 88
1da177e4
LT
89 return ret;
90}
91EXPORT_SYMBOL(clk_enable);
92
1da177e4
LT
93void clk_disable(struct clk *clk)
94{
95 unsigned long flags;
96
b824efae
TL
97 if (clk == NULL || IS_ERR(clk))
98 return;
99
1da177e4 100 spin_lock_irqsave(&clockfw_lock, flags);
7cf95774
TL
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;
106 }
107
f07adc59 108 if (arch_clock->clk_disable)
1a8bfa1e 109 arch_clock->clk_disable(clk);
7cf95774
TL
110
111out:
1da177e4
LT
112 spin_unlock_irqrestore(&clockfw_lock, flags);
113}
114EXPORT_SYMBOL(clk_disable);
115
1da177e4
LT
116int clk_get_usecount(struct clk *clk)
117{
1a8bfa1e
TL
118 unsigned long flags;
119 int ret = 0;
1da177e4 120
b824efae
TL
121 if (clk == NULL || IS_ERR(clk))
122 return 0;
123
1a8bfa1e
TL
124 spin_lock_irqsave(&clockfw_lock, flags);
125 ret = clk->usecount;
126 spin_unlock_irqrestore(&clockfw_lock, flags);
1da177e4 127
1a8bfa1e 128 return ret;
1da177e4 129}
1a8bfa1e 130EXPORT_SYMBOL(clk_get_usecount);
1da177e4 131
1a8bfa1e 132unsigned long clk_get_rate(struct clk *clk)
1da177e4 133{
1a8bfa1e
TL
134 unsigned long flags;
135 unsigned long ret = 0;
1da177e4 136
b824efae
TL
137 if (clk == NULL || IS_ERR(clk))
138 return 0;
139
1a8bfa1e
TL
140 spin_lock_irqsave(&clockfw_lock, flags);
141 ret = clk->rate;
142 spin_unlock_irqrestore(&clockfw_lock, flags);
1da177e4 143
1a8bfa1e 144 return ret;
1da177e4 145}
1a8bfa1e 146EXPORT_SYMBOL(clk_get_rate);
1da177e4 147
1a8bfa1e 148void clk_put(struct clk *clk)
bb13b5fd 149{
bb13b5fd 150}
1a8bfa1e 151EXPORT_SYMBOL(clk_put);
bb13b5fd 152
1a8bfa1e 153/*-------------------------------------------------------------------------
f07adc59 154 * Optional clock functions defined in include/linux/clk.h
1a8bfa1e 155 *-------------------------------------------------------------------------*/
bb13b5fd 156
1da177e4
LT
157long clk_round_rate(struct clk *clk, unsigned long rate)
158{
1a8bfa1e
TL
159 unsigned long flags;
160 long ret = 0;
1da177e4 161
b824efae
TL
162 if (clk == NULL || IS_ERR(clk))
163 return ret;
164
1a8bfa1e
TL
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);
1da177e4 169
1a8bfa1e 170 return ret;
1da177e4
LT
171}
172EXPORT_SYMBOL(clk_round_rate);
173
1a8bfa1e 174int clk_set_rate(struct clk *clk, unsigned long rate)
1da177e4 175{
1a8bfa1e 176 unsigned long flags;
b824efae
TL
177 int ret = -EINVAL;
178
179 if (clk == NULL || IS_ERR(clk))
180 return ret;
bb13b5fd 181
1a8bfa1e
TL
182 spin_lock_irqsave(&clockfw_lock, flags);
183 if (arch_clock->clk_set_rate)
184 ret = arch_clock->clk_set_rate(clk, rate);
185 spin_unlock_irqrestore(&clockfw_lock, flags);
1da177e4 186
1a8bfa1e 187 return ret;
1da177e4 188}
1a8bfa1e 189EXPORT_SYMBOL(clk_set_rate);
1da177e4 190
1a8bfa1e 191int clk_set_parent(struct clk *clk, struct clk *parent)
1da177e4 192{
1a8bfa1e 193 unsigned long flags;
b824efae
TL
194 int ret = -EINVAL;
195
196 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
197 return ret;
1da177e4 198
1a8bfa1e
TL
199 spin_lock_irqsave(&clockfw_lock, flags);
200 if (arch_clock->clk_set_parent)
201 ret = arch_clock->clk_set_parent(clk, parent);
202 spin_unlock_irqrestore(&clockfw_lock, flags);
1da177e4 203
1a8bfa1e 204 return ret;
1da177e4 205}
1a8bfa1e 206EXPORT_SYMBOL(clk_set_parent);
1da177e4 207
1a8bfa1e 208struct clk *clk_get_parent(struct clk *clk)
1da177e4 209{
1a8bfa1e
TL
210 unsigned long flags;
211 struct clk * ret = NULL;
1da177e4 212
b824efae
TL
213 if (clk == NULL || IS_ERR(clk))
214 return ret;
215
1a8bfa1e
TL
216 spin_lock_irqsave(&clockfw_lock, flags);
217 if (arch_clock->clk_get_parent)
218 ret = arch_clock->clk_get_parent(clk);
219 spin_unlock_irqrestore(&clockfw_lock, flags);
1da177e4
LT
220
221 return ret;
222}
1a8bfa1e 223EXPORT_SYMBOL(clk_get_parent);
1da177e4 224
1a8bfa1e
TL
225/*-------------------------------------------------------------------------
226 * OMAP specific clock functions shared between omap1 and omap2
227 *-------------------------------------------------------------------------*/
1da177e4 228
1a8bfa1e 229unsigned int __initdata mpurate;
1da177e4 230
1a8bfa1e
TL
231/*
232 * By default we use the rate set by the bootloader.
233 * You can override this with mpurate= cmdline option.
234 */
235static int __init omap_clk_setup(char *str)
1da177e4 236{
1a8bfa1e 237 get_option(&str, &mpurate);
1da177e4 238
1a8bfa1e
TL
239 if (!mpurate)
240 return 1;
1da177e4 241
1a8bfa1e
TL
242 if (mpurate < 1000)
243 mpurate *= 1000000;
1da177e4 244
1a8bfa1e 245 return 1;
1da177e4 246}
1a8bfa1e 247__setup("mpurate=", omap_clk_setup);
1da177e4 248
1a8bfa1e
TL
249/* Used for clocks that always have same value as the parent clock */
250void followparent_recalc(struct clk *clk)
1da177e4 251{
b824efae
TL
252 if (clk == NULL || IS_ERR(clk))
253 return;
254
1a8bfa1e 255 clk->rate = clk->parent->rate;
b1465bf7
ID
256 if (unlikely(clk->flags & RATE_PROPAGATES))
257 propagate_rate(clk);
1da177e4
LT
258}
259
1a8bfa1e
TL
260/* Propagate rate to children */
261void propagate_rate(struct clk * tclk)
1da177e4 262{
1a8bfa1e 263 struct clk *clkp;
1da177e4 264
b824efae
TL
265 if (tclk == NULL || IS_ERR(tclk))
266 return;
267
1a8bfa1e
TL
268 list_for_each_entry(clkp, &clocks, node) {
269 if (likely(clkp->parent != tclk))
270 continue;
271 if (likely((u32)clkp->recalc))
272 clkp->recalc(clkp);
273 }
1da177e4
LT
274}
275
6b8858a9
PW
276/**
277 * recalculate_root_clocks - recalculate and propagate all root clocks
278 *
279 * Recalculates all root clocks (clocks with no parent), which if the
280 * clock's .recalc is set correctly, should also propagate their rates.
281 * Called at init.
282 */
283void recalculate_root_clocks(void)
284{
285 struct clk *clkp;
286
287 list_for_each_entry(clkp, &clocks, node) {
288 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
289 clkp->recalc(clkp);
290 }
291}
292
1da177e4
LT
293int clk_register(struct clk *clk)
294{
b824efae
TL
295 if (clk == NULL || IS_ERR(clk))
296 return -EINVAL;
297
00431707 298 mutex_lock(&clocks_mutex);
1da177e4
LT
299 list_add(&clk->node, &clocks);
300 if (clk->init)
301 clk->init(clk);
00431707 302 mutex_unlock(&clocks_mutex);
1a8bfa1e 303
1da177e4
LT
304 return 0;
305}
306EXPORT_SYMBOL(clk_register);
307
308void clk_unregister(struct clk *clk)
309{
b824efae
TL
310 if (clk == NULL || IS_ERR(clk))
311 return;
312
00431707 313 mutex_lock(&clocks_mutex);
1da177e4 314 list_del(&clk->node);
00431707 315 mutex_unlock(&clocks_mutex);
1da177e4
LT
316}
317EXPORT_SYMBOL(clk_unregister);
318
6b8858a9
PW
319void clk_enable_init_clocks(void)
320{
321 struct clk *clkp;
322
323 list_for_each_entry(clkp, &clocks, node) {
324 if (clkp->flags & ENABLE_ON_INIT)
325 clk_enable(clkp);
326 }
327}
328EXPORT_SYMBOL(clk_enable_init_clocks);
329
897dcded
RK
330/*
331 * Low level helpers
332 */
333static int clkll_enable_null(struct clk *clk)
334{
335 return 0;
336}
337
338static void clkll_disable_null(struct clk *clk)
339{
340}
341
342const struct clkops clkops_null = {
343 .enable = clkll_enable_null,
344 .disable = clkll_disable_null,
345};
346
6b8858a9
PW
347#ifdef CONFIG_CPU_FREQ
348void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
349{
350 unsigned long flags;
351
352 spin_lock_irqsave(&clockfw_lock, flags);
353 if (arch_clock->clk_init_cpufreq_table)
354 arch_clock->clk_init_cpufreq_table(table);
355 spin_unlock_irqrestore(&clockfw_lock, flags);
356}
357EXPORT_SYMBOL(clk_init_cpufreq_table);
358#endif
359
1a8bfa1e 360/*-------------------------------------------------------------------------*/
bb13b5fd 361
90afd5cb
TL
362#ifdef CONFIG_OMAP_RESET_CLOCKS
363/*
364 * Disable any unused clocks left on by the bootloader
365 */
366static int __init clk_disable_unused(void)
367{
368 struct clk *ck;
369 unsigned long flags;
370
371 list_for_each_entry(ck, &clocks, node) {
897dcded
RK
372 if (ck->ops == &clkops_null)
373 continue;
374
375 if (ck->usecount > 0 || ck->enable_reg == 0)
90afd5cb
TL
376 continue;
377
378 spin_lock_irqsave(&clockfw_lock, flags);
379 if (arch_clock->clk_disable_unused)
380 arch_clock->clk_disable_unused(ck);
381 spin_unlock_irqrestore(&clockfw_lock, flags);
382 }
383
384 return 0;
385}
386late_initcall(clk_disable_unused);
387#endif
388
1a8bfa1e 389int __init clk_init(struct clk_functions * custom_clocks)
bb13b5fd 390{
1a8bfa1e
TL
391 if (!custom_clocks) {
392 printk(KERN_ERR "No custom clock functions registered\n");
393 BUG();
bb13b5fd
TL
394 }
395
1a8bfa1e
TL
396 arch_clock = custom_clocks;
397
bb13b5fd
TL
398 return 0;
399}
6b8858a9 400
137b3ee2
HD
401#if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
402/*
403 * debugfs support to trace clock tree hierarchy and attributes
404 */
405static struct dentry *clk_debugfs_root;
406
407static int clk_debugfs_register_one(struct clk *c)
408{
409 int err;
410 struct dentry *d, *child;
411 struct clk *pa = c->parent;
412 char s[255];
413 char *p = s;
414
415 p += sprintf(p, "%s", c->name);
416 if (c->id != 0)
417 sprintf(p, ":%d", c->id);
418 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
e621f266
Z
419 if (!d)
420 return -ENOMEM;
137b3ee2
HD
421 c->dent = d;
422
423 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
e621f266
Z
424 if (!d) {
425 err = -ENOMEM;
137b3ee2
HD
426 goto err_out;
427 }
428 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
e621f266
Z
429 if (!d) {
430 err = -ENOMEM;
137b3ee2
HD
431 goto err_out;
432 }
433 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
e621f266
Z
434 if (!d) {
435 err = -ENOMEM;
137b3ee2
HD
436 goto err_out;
437 }
438 return 0;
439
440err_out:
441 d = c->dent;
442 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
443 debugfs_remove(child);
444 debugfs_remove(c->dent);
445 return err;
446}
447
448static int clk_debugfs_register(struct clk *c)
449{
450 int err;
451 struct clk *pa = c->parent;
452
453 if (pa && !pa->dent) {
454 err = clk_debugfs_register(pa);
455 if (err)
456 return err;
457 }
458
459 if (!c->dent) {
460 err = clk_debugfs_register_one(c);
461 if (err)
462 return err;
463 }
464 return 0;
465}
466
467static int __init clk_debugfs_init(void)
468{
469 struct clk *c;
470 struct dentry *d;
471 int err;
472
473 d = debugfs_create_dir("clock", NULL);
e621f266
Z
474 if (!d)
475 return -ENOMEM;
137b3ee2
HD
476 clk_debugfs_root = d;
477
478 list_for_each_entry(c, &clocks, node) {
479 err = clk_debugfs_register(c);
480 if (err)
481 goto err_out;
482 }
483 return 0;
484err_out:
485 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
486 return err;
487}
488late_initcall(clk_debugfs_init);
489
490#endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */