blob: 23a07059999307a0569fac8f1e942ededa6c2460 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tony Lindgrenb9158552005-07-10 19:58:14 +01002 * linux/arch/arm/plat-omap/clock.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +03004 * Copyright (C) 2004 - 2008 Nokia corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6 *
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00007 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000013#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000015#include <linux/init.h>
16#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/list.h>
18#include <linux/errno.h>
19#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080020#include <linux/string.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000021#include <linux/clk.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000022#include <linux/mutex.h>
Tony Lindgrenb824efa2006-04-02 17:46:20 +010023#include <linux/platform_device.h>
Russell Kingb851cb22008-05-22 16:38:50 +010024#include <linux/cpufreq.h>
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +030025#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +010027#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Juha Yrjola7df34502006-06-26 16:16:22 -070031static LIST_HEAD(clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +000032static DEFINE_MUTEX(clocks_mutex);
Juha Yrjola7df34502006-06-26 16:16:22 -070033static DEFINE_SPINLOCK(clockfw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000035static struct clk_functions *arch_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000037/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -080038 * Standard clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000039 *-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Tony Lindgrenb824efa2006-04-02 17:46:20 +010041/*
42 * Returns a clock. Note that we first try to use device id on the bus
43 * and clock name. If this fails, we try to use clock name only.
44 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000045struct clk * clk_get(struct device *dev, const char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct clk *p, *clk = ERR_PTR(-ENOENT);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010048 int idno;
49
50 if (dev == NULL || dev->bus != &platform_bus_type)
51 idno = -1;
52 else
53 idno = to_platform_device(dev)->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Arjan van de Ven00431702006-01-12 18:42:23 +000055 mutex_lock(&clocks_mutex);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010056
57 list_for_each_entry(p, &clocks, node) {
58 if (p->id == idno &&
59 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
60 clk = p;
Tony Lindgren67d4d832006-04-09 22:21:05 +010061 goto found;
Tony Lindgrenb824efa2006-04-02 17:46:20 +010062 }
63 }
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 list_for_each_entry(p, &clocks, node) {
66 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
67 clk = p;
68 break;
69 }
70 }
Tony Lindgrenb824efa2006-04-02 17:46:20 +010071
Tony Lindgren67d4d832006-04-09 22:21:05 +010072found:
Arjan van de Ven00431702006-01-12 18:42:23 +000073 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 return clk;
76}
77EXPORT_SYMBOL(clk_get);
78
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000079int clk_enable(struct clk *clk)
80{
81 unsigned long flags;
82 int ret = 0;
83
Tony Lindgrenb824efa2006-04-02 17:46:20 +010084 if (clk == NULL || IS_ERR(clk))
85 return -EINVAL;
86
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000087 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -080088 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000089 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000090 spin_unlock_irqrestore(&clockfw_lock, flags);
91
92 return ret;
93}
94EXPORT_SYMBOL(clk_enable);
95
96void clk_disable(struct clk *clk)
97{
98 unsigned long flags;
99
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100100 if (clk == NULL || IS_ERR(clk))
101 return;
102
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000103 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700104 if (clk->usecount == 0) {
105 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
106 clk->name);
107 WARN_ON(1);
108 goto out;
109 }
110
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800111 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000112 arch_clock->clk_disable(clk);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700113
114out:
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000115 spin_unlock_irqrestore(&clockfw_lock, flags);
116}
117EXPORT_SYMBOL(clk_disable);
118
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000119int clk_get_usecount(struct clk *clk)
120{
121 unsigned long flags;
122 int ret = 0;
123
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100124 if (clk == NULL || IS_ERR(clk))
125 return 0;
126
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000127 spin_lock_irqsave(&clockfw_lock, flags);
128 ret = clk->usecount;
129 spin_unlock_irqrestore(&clockfw_lock, flags);
130
131 return ret;
132}
133EXPORT_SYMBOL(clk_get_usecount);
134
135unsigned long clk_get_rate(struct clk *clk)
136{
137 unsigned long flags;
138 unsigned long ret = 0;
139
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100140 if (clk == NULL || IS_ERR(clk))
141 return 0;
142
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000143 spin_lock_irqsave(&clockfw_lock, flags);
144 ret = clk->rate;
145 spin_unlock_irqrestore(&clockfw_lock, flags);
146
147 return ret;
148}
149EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151void clk_put(struct clk *clk)
152{
153 if (clk && !IS_ERR(clk))
154 module_put(clk->owner);
155}
156EXPORT_SYMBOL(clk_put);
157
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000158/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800159 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000160 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162long clk_round_rate(struct clk *clk, unsigned long rate)
163{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000164 unsigned long flags;
165 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100167 if (clk == NULL || IS_ERR(clk))
168 return ret;
169
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000170 spin_lock_irqsave(&clockfw_lock, flags);
171 if (arch_clock->clk_round_rate)
172 ret = arch_clock->clk_round_rate(clk, rate);
173 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000175 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177EXPORT_SYMBOL(clk_round_rate);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179int clk_set_rate(struct clk *clk, unsigned long rate)
180{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000181 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100182 int ret = -EINVAL;
183
184 if (clk == NULL || IS_ERR(clk))
185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000187 spin_lock_irqsave(&clockfw_lock, flags);
188 if (arch_clock->clk_set_rate)
189 ret = arch_clock->clk_set_rate(clk, rate);
190 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return ret;
193}
194EXPORT_SYMBOL(clk_set_rate);
195
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000196int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000198 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100199 int ret = -EINVAL;
200
201 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
202 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000204 spin_lock_irqsave(&clockfw_lock, flags);
205 if (arch_clock->clk_set_parent)
206 ret = arch_clock->clk_set_parent(clk, parent);
207 spin_unlock_irqrestore(&clockfw_lock, flags);
208
209 return ret;
210}
211EXPORT_SYMBOL(clk_set_parent);
212
213struct clk *clk_get_parent(struct clk *clk)
214{
215 unsigned long flags;
216 struct clk * ret = NULL;
217
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100218 if (clk == NULL || IS_ERR(clk))
219 return ret;
220
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000221 spin_lock_irqsave(&clockfw_lock, flags);
222 if (arch_clock->clk_get_parent)
223 ret = arch_clock->clk_get_parent(clk);
224 spin_unlock_irqrestore(&clockfw_lock, flags);
225
226 return ret;
227}
228EXPORT_SYMBOL(clk_get_parent);
229
230/*-------------------------------------------------------------------------
231 * OMAP specific clock functions shared between omap1 and omap2
232 *-------------------------------------------------------------------------*/
233
234unsigned int __initdata mpurate;
235
236/*
237 * By default we use the rate set by the bootloader.
238 * You can override this with mpurate= cmdline option.
239 */
240static int __init omap_clk_setup(char *str)
241{
242 get_option(&str, &mpurate);
243
244 if (!mpurate)
245 return 1;
246
247 if (mpurate < 1000)
248 mpurate *= 1000000;
249
250 return 1;
251}
252__setup("mpurate=", omap_clk_setup);
253
254/* Used for clocks that always have same value as the parent clock */
255void followparent_recalc(struct clk *clk)
256{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100257 if (clk == NULL || IS_ERR(clk))
258 return;
259
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000260 clk->rate = clk->parent->rate;
Imre Deakb1465bf2007-03-06 03:52:01 -0800261 if (unlikely(clk->flags & RATE_PROPAGATES))
262 propagate_rate(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000263}
264
265/* Propagate rate to children */
266void propagate_rate(struct clk * tclk)
267{
268 struct clk *clkp;
269
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100270 if (tclk == NULL || IS_ERR(tclk))
271 return;
272
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000273 list_for_each_entry(clkp, &clocks, node) {
274 if (likely(clkp->parent != tclk))
275 continue;
276 if (likely((u32)clkp->recalc))
277 clkp->recalc(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200281/**
282 * recalculate_root_clocks - recalculate and propagate all root clocks
283 *
284 * Recalculates all root clocks (clocks with no parent), which if the
285 * clock's .recalc is set correctly, should also propagate their rates.
286 * Called at init.
287 */
288void recalculate_root_clocks(void)
289{
290 struct clk *clkp;
291
292 list_for_each_entry(clkp, &clocks, node) {
293 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
294 clkp->recalc(clkp);
295 }
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298int clk_register(struct clk *clk)
299{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100300 if (clk == NULL || IS_ERR(clk))
301 return -EINVAL;
302
Arjan van de Ven00431702006-01-12 18:42:23 +0000303 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 list_add(&clk->node, &clocks);
305 if (clk->init)
306 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000307 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return 0;
310}
311EXPORT_SYMBOL(clk_register);
312
313void clk_unregister(struct clk *clk)
314{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100315 if (clk == NULL || IS_ERR(clk))
316 return;
317
Arjan van de Ven00431702006-01-12 18:42:23 +0000318 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000320 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322EXPORT_SYMBOL(clk_unregister);
323
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000324void clk_deny_idle(struct clk *clk)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100325{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000326 unsigned long flags;
327
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100328 if (clk == NULL || IS_ERR(clk))
329 return;
330
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000331 spin_lock_irqsave(&clockfw_lock, flags);
332 if (arch_clock->clk_deny_idle)
333 arch_clock->clk_deny_idle(clk);
334 spin_unlock_irqrestore(&clockfw_lock, flags);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100335}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000336EXPORT_SYMBOL(clk_deny_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000338void clk_allow_idle(struct clk *clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000340 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100342 if (clk == NULL || IS_ERR(clk))
343 return;
344
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000345 spin_lock_irqsave(&clockfw_lock, flags);
346 if (arch_clock->clk_allow_idle)
347 arch_clock->clk_allow_idle(clk);
348 spin_unlock_irqrestore(&clockfw_lock, flags);
349}
350EXPORT_SYMBOL(clk_allow_idle);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100351
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200352void clk_enable_init_clocks(void)
353{
354 struct clk *clkp;
355
356 list_for_each_entry(clkp, &clocks, node) {
357 if (clkp->flags & ENABLE_ON_INIT)
358 clk_enable(clkp);
359 }
360}
361EXPORT_SYMBOL(clk_enable_init_clocks);
362
363#ifdef CONFIG_CPU_FREQ
364void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
365{
366 unsigned long flags;
367
368 spin_lock_irqsave(&clockfw_lock, flags);
369 if (arch_clock->clk_init_cpufreq_table)
370 arch_clock->clk_init_cpufreq_table(table);
371 spin_unlock_irqrestore(&clockfw_lock, flags);
372}
373EXPORT_SYMBOL(clk_init_cpufreq_table);
374#endif
375
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000376/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300378#ifdef CONFIG_OMAP_RESET_CLOCKS
379/*
380 * Disable any unused clocks left on by the bootloader
381 */
382static int __init clk_disable_unused(void)
383{
384 struct clk *ck;
385 unsigned long flags;
386
387 list_for_each_entry(ck, &clocks, node) {
388 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
389 ck->enable_reg == 0)
390 continue;
391
392 spin_lock_irqsave(&clockfw_lock, flags);
393 if (arch_clock->clk_disable_unused)
394 arch_clock->clk_disable_unused(ck);
395 spin_unlock_irqrestore(&clockfw_lock, flags);
396 }
397
398 return 0;
399}
400late_initcall(clk_disable_unused);
401#endif
402
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000403int __init clk_init(struct clk_functions * custom_clocks)
404{
405 if (!custom_clocks) {
406 printk(KERN_ERR "No custom clock functions registered\n");
407 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000410 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return 0;
413}
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200414
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300415#if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
416/*
417 * debugfs support to trace clock tree hierarchy and attributes
418 */
419static struct dentry *clk_debugfs_root;
420
421static int clk_debugfs_register_one(struct clk *c)
422{
423 int err;
424 struct dentry *d, *child;
425 struct clk *pa = c->parent;
426 char s[255];
427 char *p = s;
428
429 p += sprintf(p, "%s", c->name);
430 if (c->id != 0)
431 sprintf(p, ":%d", c->id);
432 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
433 if (IS_ERR(d))
434 return PTR_ERR(d);
435 c->dent = d;
436
437 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
438 if (IS_ERR(d)) {
439 err = PTR_ERR(d);
440 goto err_out;
441 }
442 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
443 if (IS_ERR(d)) {
444 err = PTR_ERR(d);
445 goto err_out;
446 }
447 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
448 if (IS_ERR(d)) {
449 err = PTR_ERR(d);
450 goto err_out;
451 }
452 return 0;
453
454err_out:
455 d = c->dent;
456 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
457 debugfs_remove(child);
458 debugfs_remove(c->dent);
459 return err;
460}
461
462static int clk_debugfs_register(struct clk *c)
463{
464 int err;
465 struct clk *pa = c->parent;
466
467 if (pa && !pa->dent) {
468 err = clk_debugfs_register(pa);
469 if (err)
470 return err;
471 }
472
473 if (!c->dent) {
474 err = clk_debugfs_register_one(c);
475 if (err)
476 return err;
477 }
478 return 0;
479}
480
481static int __init clk_debugfs_init(void)
482{
483 struct clk *c;
484 struct dentry *d;
485 int err;
486
487 d = debugfs_create_dir("clock", NULL);
488 if (IS_ERR(d))
489 return PTR_ERR(d);
490 clk_debugfs_root = d;
491
492 list_for_each_entry(c, &clocks, node) {
493 err = clk_debugfs_register(c);
494 if (err)
495 goto err_out;
496 }
497 return 0;
498err_out:
499 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
500 return err;
501}
502late_initcall(clk_debugfs_init);
503
504#endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */