blob: 89cafc9372496d2d33989be36f2ea38b657d0949 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000014#include <linux/init.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/list.h>
17#include <linux/errno.h>
18#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/string.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000021#include <linux/mutex.h>
Tony Lindgrenb824efa2006-04-02 17:46:20 +010022#include <linux/platform_device.h>
Russell Kingb851cb22008-05-22 16:38:50 +010023#include <linux/cpufreq.h>
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +030024#include <linux/debugfs.h>
Russell Kingfced80c2008-09-06 12:10:45 +010025#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Tony Lindgrence491cf2009-10-20 09:40:47 -070027#include <plat/clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Juha Yrjola7df34502006-06-26 16:16:22 -070029static LIST_HEAD(clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +000030static DEFINE_MUTEX(clocks_mutex);
Juha Yrjola7df34502006-06-26 16:16:22 -070031static DEFINE_SPINLOCK(clockfw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000033static struct clk_functions *arch_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000035/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -080036 * Standard clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000037 *-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Santosh Shilimkar74830382009-05-28 14:16:04 -070039/* 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 */
42
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000043int clk_enable(struct clk *clk)
44{
45 unsigned long flags;
46 int ret = 0;
47
Tony Lindgrenb824efa2006-04-02 17:46:20 +010048 if (clk == NULL || IS_ERR(clk))
49 return -EINVAL;
50
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000051 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -080052 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000053 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000054 spin_unlock_irqrestore(&clockfw_lock, flags);
55
56 return ret;
57}
58EXPORT_SYMBOL(clk_enable);
59
60void clk_disable(struct clk *clk)
61{
62 unsigned long flags;
63
Tony Lindgrenb824efa2006-04-02 17:46:20 +010064 if (clk == NULL || IS_ERR(clk))
65 return;
66
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000067 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgren7cf95772007-08-07 05:20:00 -070068 if (clk->usecount == 0) {
69 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
70 clk->name);
71 WARN_ON(1);
72 goto out;
73 }
74
Tony Lindgrenf07adc52006-01-17 15:27:09 -080075 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000076 arch_clock->clk_disable(clk);
Tony Lindgren7cf95772007-08-07 05:20:00 -070077
78out:
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000079 spin_unlock_irqrestore(&clockfw_lock, flags);
80}
81EXPORT_SYMBOL(clk_disable);
82
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000083unsigned long clk_get_rate(struct clk *clk)
84{
85 unsigned long flags;
86 unsigned long ret = 0;
87
Tony Lindgrenb824efa2006-04-02 17:46:20 +010088 if (clk == NULL || IS_ERR(clk))
89 return 0;
90
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000091 spin_lock_irqsave(&clockfw_lock, flags);
92 ret = clk->rate;
93 spin_unlock_irqrestore(&clockfw_lock, flags);
94
95 return ret;
96}
97EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000099/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800100 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000101 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103long clk_round_rate(struct clk *clk, unsigned long rate)
104{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000105 unsigned long flags;
106 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100108 if (clk == NULL || IS_ERR(clk))
109 return ret;
110
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000111 spin_lock_irqsave(&clockfw_lock, flags);
112 if (arch_clock->clk_round_rate)
113 ret = arch_clock->clk_round_rate(clk, rate);
114 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118EXPORT_SYMBOL(clk_round_rate);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120int clk_set_rate(struct clk *clk, unsigned long rate)
121{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000122 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100123 int ret = -EINVAL;
124
125 if (clk == NULL || IS_ERR(clk))
126 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000128 spin_lock_irqsave(&clockfw_lock, flags);
129 if (arch_clock->clk_set_rate)
130 ret = arch_clock->clk_set_rate(clk, rate);
Russell Kingb5088c02009-01-29 19:33:19 +0000131 if (ret == 0) {
132 if (clk->recalc)
Russell King8b9dbc12009-02-12 10:12:59 +0000133 clk->rate = clk->recalc(clk);
Russell King3f0a8202009-01-31 10:05:51 +0000134 propagate_rate(clk);
Russell Kingb5088c02009-01-29 19:33:19 +0000135 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000136 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return ret;
139}
140EXPORT_SYMBOL(clk_set_rate);
141
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000142int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000144 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100145 int ret = -EINVAL;
146
Santosh Shilimkar74830382009-05-28 14:16:04 -0700147 if (cpu_is_omap44xx())
148 /* OMAP4 clk framework not supported yet */
149 return 0;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100150 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000153 spin_lock_irqsave(&clockfw_lock, flags);
Russell King4da37822009-02-24 12:46:31 +0000154 if (clk->usecount == 0) {
155 if (arch_clock->clk_set_parent)
156 ret = arch_clock->clk_set_parent(clk, parent);
157 if (ret == 0) {
158 if (clk->recalc)
159 clk->rate = clk->recalc(clk);
160 propagate_rate(clk);
161 }
162 } else
163 ret = -EBUSY;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000164 spin_unlock_irqrestore(&clockfw_lock, flags);
165
166 return ret;
167}
168EXPORT_SYMBOL(clk_set_parent);
169
170struct clk *clk_get_parent(struct clk *clk)
171{
Russell King2e777bf2009-02-08 17:49:22 +0000172 return clk->parent;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000173}
174EXPORT_SYMBOL(clk_get_parent);
175
176/*-------------------------------------------------------------------------
177 * OMAP specific clock functions shared between omap1 and omap2
178 *-------------------------------------------------------------------------*/
179
180unsigned int __initdata mpurate;
181
182/*
183 * By default we use the rate set by the bootloader.
184 * You can override this with mpurate= cmdline option.
185 */
186static int __init omap_clk_setup(char *str)
187{
188 get_option(&str, &mpurate);
189
190 if (!mpurate)
191 return 1;
192
193 if (mpurate < 1000)
194 mpurate *= 1000000;
195
196 return 1;
197}
198__setup("mpurate=", omap_clk_setup);
199
200/* Used for clocks that always have same value as the parent clock */
Russell King8b9dbc12009-02-12 10:12:59 +0000201unsigned long followparent_recalc(struct clk *clk)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000202{
Russell King8b9dbc12009-02-12 10:12:59 +0000203 return clk->parent->rate;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000204}
205
Russell King3f0a8202009-01-31 10:05:51 +0000206void clk_reparent(struct clk *child, struct clk *parent)
207{
208 list_del_init(&child->sibling);
209 if (parent)
210 list_add(&child->sibling, &parent->children);
211 child->parent = parent;
212
213 /* now do the debugfs renaming to reattach the child
214 to the proper parent */
215}
216
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000217/* Propagate rate to children */
218void propagate_rate(struct clk * tclk)
219{
220 struct clk *clkp;
221
Russell King3f0a8202009-01-31 10:05:51 +0000222 list_for_each_entry(clkp, &tclk->children, sibling) {
Russell King9a5feda2008-11-13 13:44:15 +0000223 if (clkp->recalc)
Russell King8b9dbc12009-02-12 10:12:59 +0000224 clkp->rate = clkp->recalc(clkp);
Russell King3f0a8202009-01-31 10:05:51 +0000225 propagate_rate(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Russell King3f0a8202009-01-31 10:05:51 +0000229static LIST_HEAD(root_clks);
230
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200231/**
232 * recalculate_root_clocks - recalculate and propagate all root clocks
233 *
234 * Recalculates all root clocks (clocks with no parent), which if the
235 * clock's .recalc is set correctly, should also propagate their rates.
236 * Called at init.
237 */
238void recalculate_root_clocks(void)
239{
240 struct clk *clkp;
241
Russell King3f0a8202009-01-31 10:05:51 +0000242 list_for_each_entry(clkp, &root_clks, sibling) {
243 if (clkp->recalc)
Russell King8b9dbc12009-02-12 10:12:59 +0000244 clkp->rate = clkp->recalc(clkp);
Russell King3f0a8202009-01-31 10:05:51 +0000245 propagate_rate(clkp);
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200246 }
247}
248
Paul Walmsleyc8088112009-04-22 19:48:53 -0600249/**
Paul Walmsley79716872009-05-12 17:50:30 -0600250 * clk_preinit - initialize any fields in the struct clk before clk init
Paul Walmsleyc8088112009-04-22 19:48:53 -0600251 * @clk: struct clk * to initialize
252 *
253 * Initialize any struct clk fields needed before normal clk initialization
254 * can run. No return value.
255 */
Paul Walmsley79716872009-05-12 17:50:30 -0600256void clk_preinit(struct clk *clk)
Russell King3f0a8202009-01-31 10:05:51 +0000257{
258 INIT_LIST_HEAD(&clk->children);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261int clk_register(struct clk *clk)
262{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100263 if (clk == NULL || IS_ERR(clk))
264 return -EINVAL;
265
Russell Kingdbb674d2009-01-22 16:08:04 +0000266 /*
267 * trap out already registered clocks
268 */
269 if (clk->node.next || clk->node.prev)
270 return 0;
271
Arjan van de Ven00431702006-01-12 18:42:23 +0000272 mutex_lock(&clocks_mutex);
Russell King3f0a8202009-01-31 10:05:51 +0000273 if (clk->parent)
274 list_add(&clk->sibling, &clk->parent->children);
275 else
276 list_add(&clk->sibling, &root_clks);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 list_add(&clk->node, &clocks);
279 if (clk->init)
280 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000281 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285EXPORT_SYMBOL(clk_register);
286
287void clk_unregister(struct clk *clk)
288{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100289 if (clk == NULL || IS_ERR(clk))
290 return;
291
Arjan van de Ven00431702006-01-12 18:42:23 +0000292 mutex_lock(&clocks_mutex);
Russell King3f0a8202009-01-31 10:05:51 +0000293 list_del(&clk->sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000295 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297EXPORT_SYMBOL(clk_unregister);
298
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200299void clk_enable_init_clocks(void)
300{
301 struct clk *clkp;
302
303 list_for_each_entry(clkp, &clocks, node) {
304 if (clkp->flags & ENABLE_ON_INIT)
305 clk_enable(clkp);
306 }
307}
308EXPORT_SYMBOL(clk_enable_init_clocks);
309
Russell King897dcde2008-11-04 16:35:03 +0000310/*
311 * Low level helpers
312 */
313static int clkll_enable_null(struct clk *clk)
314{
315 return 0;
316}
317
318static void clkll_disable_null(struct clk *clk)
319{
320}
321
322const struct clkops clkops_null = {
323 .enable = clkll_enable_null,
324 .disable = clkll_disable_null,
325};
326
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200327#ifdef CONFIG_CPU_FREQ
328void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
329{
330 unsigned long flags;
331
332 spin_lock_irqsave(&clockfw_lock, flags);
333 if (arch_clock->clk_init_cpufreq_table)
334 arch_clock->clk_init_cpufreq_table(table);
335 spin_unlock_irqrestore(&clockfw_lock, flags);
336}
337EXPORT_SYMBOL(clk_init_cpufreq_table);
338#endif
339
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000340/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300342#ifdef CONFIG_OMAP_RESET_CLOCKS
343/*
344 * Disable any unused clocks left on by the bootloader
345 */
346static int __init clk_disable_unused(void)
347{
348 struct clk *ck;
349 unsigned long flags;
350
351 list_for_each_entry(ck, &clocks, node) {
Russell King897dcde2008-11-04 16:35:03 +0000352 if (ck->ops == &clkops_null)
353 continue;
354
355 if (ck->usecount > 0 || ck->enable_reg == 0)
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300356 continue;
357
358 spin_lock_irqsave(&clockfw_lock, flags);
359 if (arch_clock->clk_disable_unused)
360 arch_clock->clk_disable_unused(ck);
361 spin_unlock_irqrestore(&clockfw_lock, flags);
362 }
363
364 return 0;
365}
366late_initcall(clk_disable_unused);
367#endif
368
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000369int __init clk_init(struct clk_functions * custom_clocks)
370{
371 if (!custom_clocks) {
372 printk(KERN_ERR "No custom clock functions registered\n");
373 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000376 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 return 0;
379}
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200380
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300381#if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
382/*
383 * debugfs support to trace clock tree hierarchy and attributes
384 */
385static struct dentry *clk_debugfs_root;
386
387static int clk_debugfs_register_one(struct clk *c)
388{
389 int err;
390 struct dentry *d, *child;
391 struct clk *pa = c->parent;
392 char s[255];
393 char *p = s;
394
395 p += sprintf(p, "%s", c->name);
396 if (c->id != 0)
397 sprintf(p, ":%d", c->id);
398 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
Zhaoleie621f262008-11-04 13:35:07 -0800399 if (!d)
400 return -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300401 c->dent = d;
402
403 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
Zhaoleie621f262008-11-04 13:35:07 -0800404 if (!d) {
405 err = -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300406 goto err_out;
407 }
408 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
Zhaoleie621f262008-11-04 13:35:07 -0800409 if (!d) {
410 err = -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300411 goto err_out;
412 }
413 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
Zhaoleie621f262008-11-04 13:35:07 -0800414 if (!d) {
415 err = -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300416 goto err_out;
417 }
418 return 0;
419
420err_out:
421 d = c->dent;
422 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
423 debugfs_remove(child);
424 debugfs_remove(c->dent);
425 return err;
426}
427
428static int clk_debugfs_register(struct clk *c)
429{
430 int err;
431 struct clk *pa = c->parent;
432
433 if (pa && !pa->dent) {
434 err = clk_debugfs_register(pa);
435 if (err)
436 return err;
437 }
438
439 if (!c->dent) {
440 err = clk_debugfs_register_one(c);
441 if (err)
442 return err;
443 }
444 return 0;
445}
446
447static int __init clk_debugfs_init(void)
448{
449 struct clk *c;
450 struct dentry *d;
451 int err;
452
453 d = debugfs_create_dir("clock", NULL);
Zhaoleie621f262008-11-04 13:35:07 -0800454 if (!d)
455 return -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300456 clk_debugfs_root = d;
457
458 list_for_each_entry(c, &clocks, node) {
459 err = clk_debugfs_register(c);
460 if (err)
461 goto err_out;
462 }
463 return 0;
464err_out:
Hiroshi DOYUca4caa42009-09-03 20:14:06 +0300465 debugfs_remove_recursive(clk_debugfs_root);
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300466 return err;
467}
468late_initcall(clk_debugfs_init);
469
470#endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */