blob: bf880e966d3b03bd55b73f84656359deea4dc637 [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
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/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
43/* Dummy hooks only for OMAP4.For rest OMAPs, common clkdev is used */
44#if defined(CONFIG_ARCH_OMAP4)
45struct clk *clk_get(struct device *dev, const char *id)
46{
47 return NULL;
48}
49EXPORT_SYMBOL(clk_get);
50
51void clk_put(struct clk *clk)
52{
53}
54EXPORT_SYMBOL(clk_put);
55
56void omap2_clk_prepare_for_reboot(void)
57{
58}
59EXPORT_SYMBOL(omap2_clk_prepare_for_reboot);
60
61void omap_prcm_arch_reset(char mode)
62{
63}
64EXPORT_SYMBOL(omap_prcm_arch_reset);
65#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000066int clk_enable(struct clk *clk)
67{
68 unsigned long flags;
69 int ret = 0;
Santosh Shilimkar74830382009-05-28 14:16:04 -070070 if (cpu_is_omap44xx())
71 /* OMAP4 clk framework not supported yet */
72 return 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000073
Tony Lindgrenb824efa2006-04-02 17:46:20 +010074 if (clk == NULL || IS_ERR(clk))
75 return -EINVAL;
76
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000077 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -080078 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000079 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000080 spin_unlock_irqrestore(&clockfw_lock, flags);
81
82 return ret;
83}
84EXPORT_SYMBOL(clk_enable);
85
86void clk_disable(struct clk *clk)
87{
88 unsigned long flags;
89
Tony Lindgrenb824efa2006-04-02 17:46:20 +010090 if (clk == NULL || IS_ERR(clk))
91 return;
92
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000093 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgren7cf95772007-08-07 05:20:00 -070094 if (clk->usecount == 0) {
95 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
96 clk->name);
97 WARN_ON(1);
98 goto out;
99 }
100
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800101 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000102 arch_clock->clk_disable(clk);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700103
104out:
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000105 spin_unlock_irqrestore(&clockfw_lock, flags);
106}
107EXPORT_SYMBOL(clk_disable);
108
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000109unsigned long clk_get_rate(struct clk *clk)
110{
111 unsigned long flags;
112 unsigned long ret = 0;
113
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100114 if (clk == NULL || IS_ERR(clk))
115 return 0;
116
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000117 spin_lock_irqsave(&clockfw_lock, flags);
118 ret = clk->rate;
119 spin_unlock_irqrestore(&clockfw_lock, flags);
120
121 return ret;
122}
123EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000125/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800126 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000127 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129long clk_round_rate(struct clk *clk, unsigned long rate)
130{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000131 unsigned long flags;
132 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100134 if (clk == NULL || IS_ERR(clk))
135 return ret;
136
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000137 spin_lock_irqsave(&clockfw_lock, flags);
138 if (arch_clock->clk_round_rate)
139 ret = arch_clock->clk_round_rate(clk, rate);
140 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144EXPORT_SYMBOL(clk_round_rate);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146int clk_set_rate(struct clk *clk, unsigned long rate)
147{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000148 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100149 int ret = -EINVAL;
150
151 if (clk == NULL || IS_ERR(clk))
152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000154 spin_lock_irqsave(&clockfw_lock, flags);
155 if (arch_clock->clk_set_rate)
156 ret = arch_clock->clk_set_rate(clk, rate);
Russell Kingb5088c02009-01-29 19:33:19 +0000157 if (ret == 0) {
158 if (clk->recalc)
Russell King8b9dbc12009-02-12 10:12:59 +0000159 clk->rate = clk->recalc(clk);
Russell King3f0a8202009-01-31 10:05:51 +0000160 propagate_rate(clk);
Russell Kingb5088c02009-01-29 19:33:19 +0000161 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000162 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return ret;
165}
166EXPORT_SYMBOL(clk_set_rate);
167
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000168int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000170 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100171 int ret = -EINVAL;
172
Santosh Shilimkar74830382009-05-28 14:16:04 -0700173 if (cpu_is_omap44xx())
174 /* OMAP4 clk framework not supported yet */
175 return 0;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100176 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000179 spin_lock_irqsave(&clockfw_lock, flags);
Russell King4da37822009-02-24 12:46:31 +0000180 if (clk->usecount == 0) {
181 if (arch_clock->clk_set_parent)
182 ret = arch_clock->clk_set_parent(clk, parent);
183 if (ret == 0) {
184 if (clk->recalc)
185 clk->rate = clk->recalc(clk);
186 propagate_rate(clk);
187 }
188 } else
189 ret = -EBUSY;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000190 spin_unlock_irqrestore(&clockfw_lock, flags);
191
192 return ret;
193}
194EXPORT_SYMBOL(clk_set_parent);
195
196struct clk *clk_get_parent(struct clk *clk)
197{
Russell King2e777bf2009-02-08 17:49:22 +0000198 return clk->parent;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000199}
200EXPORT_SYMBOL(clk_get_parent);
201
202/*-------------------------------------------------------------------------
203 * OMAP specific clock functions shared between omap1 and omap2
204 *-------------------------------------------------------------------------*/
205
206unsigned int __initdata mpurate;
207
208/*
209 * By default we use the rate set by the bootloader.
210 * You can override this with mpurate= cmdline option.
211 */
212static int __init omap_clk_setup(char *str)
213{
214 get_option(&str, &mpurate);
215
216 if (!mpurate)
217 return 1;
218
219 if (mpurate < 1000)
220 mpurate *= 1000000;
221
222 return 1;
223}
224__setup("mpurate=", omap_clk_setup);
225
226/* Used for clocks that always have same value as the parent clock */
Russell King8b9dbc12009-02-12 10:12:59 +0000227unsigned long followparent_recalc(struct clk *clk)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000228{
Russell King8b9dbc12009-02-12 10:12:59 +0000229 return clk->parent->rate;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000230}
231
Russell King3f0a8202009-01-31 10:05:51 +0000232void clk_reparent(struct clk *child, struct clk *parent)
233{
234 list_del_init(&child->sibling);
235 if (parent)
236 list_add(&child->sibling, &parent->children);
237 child->parent = parent;
238
239 /* now do the debugfs renaming to reattach the child
240 to the proper parent */
241}
242
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000243/* Propagate rate to children */
244void propagate_rate(struct clk * tclk)
245{
246 struct clk *clkp;
247
Russell King3f0a8202009-01-31 10:05:51 +0000248 list_for_each_entry(clkp, &tclk->children, sibling) {
Russell King9a5feda2008-11-13 13:44:15 +0000249 if (clkp->recalc)
Russell King8b9dbc12009-02-12 10:12:59 +0000250 clkp->rate = clkp->recalc(clkp);
Russell King3f0a8202009-01-31 10:05:51 +0000251 propagate_rate(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Russell King3f0a8202009-01-31 10:05:51 +0000255static LIST_HEAD(root_clks);
256
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200257/**
258 * recalculate_root_clocks - recalculate and propagate all root clocks
259 *
260 * Recalculates all root clocks (clocks with no parent), which if the
261 * clock's .recalc is set correctly, should also propagate their rates.
262 * Called at init.
263 */
264void recalculate_root_clocks(void)
265{
266 struct clk *clkp;
267
Russell King3f0a8202009-01-31 10:05:51 +0000268 list_for_each_entry(clkp, &root_clks, sibling) {
269 if (clkp->recalc)
Russell King8b9dbc12009-02-12 10:12:59 +0000270 clkp->rate = clkp->recalc(clkp);
Russell King3f0a8202009-01-31 10:05:51 +0000271 propagate_rate(clkp);
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200272 }
273}
274
Paul Walmsleyc8088112009-04-22 19:48:53 -0600275/**
Paul Walmsley79716872009-05-12 17:50:30 -0600276 * clk_preinit - initialize any fields in the struct clk before clk init
Paul Walmsleyc8088112009-04-22 19:48:53 -0600277 * @clk: struct clk * to initialize
278 *
279 * Initialize any struct clk fields needed before normal clk initialization
280 * can run. No return value.
281 */
Paul Walmsley79716872009-05-12 17:50:30 -0600282void clk_preinit(struct clk *clk)
Russell King3f0a8202009-01-31 10:05:51 +0000283{
284 INIT_LIST_HEAD(&clk->children);
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287int clk_register(struct clk *clk)
288{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100289 if (clk == NULL || IS_ERR(clk))
290 return -EINVAL;
291
Russell Kingdbb674d2009-01-22 16:08:04 +0000292 /*
293 * trap out already registered clocks
294 */
295 if (clk->node.next || clk->node.prev)
296 return 0;
297
Arjan van de Ven00431702006-01-12 18:42:23 +0000298 mutex_lock(&clocks_mutex);
Russell King3f0a8202009-01-31 10:05:51 +0000299 if (clk->parent)
300 list_add(&clk->sibling, &clk->parent->children);
301 else
302 list_add(&clk->sibling, &root_clks);
303
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);
Russell King3f0a8202009-01-31 10:05:51 +0000319 list_del(&clk->sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000321 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323EXPORT_SYMBOL(clk_unregister);
324
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200325void clk_enable_init_clocks(void)
326{
327 struct clk *clkp;
328
329 list_for_each_entry(clkp, &clocks, node) {
330 if (clkp->flags & ENABLE_ON_INIT)
331 clk_enable(clkp);
332 }
333}
334EXPORT_SYMBOL(clk_enable_init_clocks);
335
Russell King897dcde2008-11-04 16:35:03 +0000336/*
337 * Low level helpers
338 */
339static int clkll_enable_null(struct clk *clk)
340{
341 return 0;
342}
343
344static void clkll_disable_null(struct clk *clk)
345{
346}
347
348const struct clkops clkops_null = {
349 .enable = clkll_enable_null,
350 .disable = clkll_disable_null,
351};
352
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200353#ifdef CONFIG_CPU_FREQ
354void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
355{
356 unsigned long flags;
357
358 spin_lock_irqsave(&clockfw_lock, flags);
359 if (arch_clock->clk_init_cpufreq_table)
360 arch_clock->clk_init_cpufreq_table(table);
361 spin_unlock_irqrestore(&clockfw_lock, flags);
362}
363EXPORT_SYMBOL(clk_init_cpufreq_table);
364#endif
365
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000366/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300368#ifdef CONFIG_OMAP_RESET_CLOCKS
369/*
370 * Disable any unused clocks left on by the bootloader
371 */
372static int __init clk_disable_unused(void)
373{
374 struct clk *ck;
375 unsigned long flags;
376
377 list_for_each_entry(ck, &clocks, node) {
Russell King897dcde2008-11-04 16:35:03 +0000378 if (ck->ops == &clkops_null)
379 continue;
380
381 if (ck->usecount > 0 || ck->enable_reg == 0)
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300382 continue;
383
384 spin_lock_irqsave(&clockfw_lock, flags);
385 if (arch_clock->clk_disable_unused)
386 arch_clock->clk_disable_unused(ck);
387 spin_unlock_irqrestore(&clockfw_lock, flags);
388 }
389
390 return 0;
391}
392late_initcall(clk_disable_unused);
393#endif
394
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000395int __init clk_init(struct clk_functions * custom_clocks)
396{
397 if (!custom_clocks) {
398 printk(KERN_ERR "No custom clock functions registered\n");
399 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000402 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return 0;
405}
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200406
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300407#if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
408/*
409 * debugfs support to trace clock tree hierarchy and attributes
410 */
411static struct dentry *clk_debugfs_root;
412
413static int clk_debugfs_register_one(struct clk *c)
414{
415 int err;
416 struct dentry *d, *child;
417 struct clk *pa = c->parent;
418 char s[255];
419 char *p = s;
420
421 p += sprintf(p, "%s", c->name);
422 if (c->id != 0)
423 sprintf(p, ":%d", c->id);
424 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
Zhaoleie621f262008-11-04 13:35:07 -0800425 if (!d)
426 return -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300427 c->dent = d;
428
429 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
Zhaoleie621f262008-11-04 13:35:07 -0800430 if (!d) {
431 err = -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300432 goto err_out;
433 }
434 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
Zhaoleie621f262008-11-04 13:35:07 -0800435 if (!d) {
436 err = -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300437 goto err_out;
438 }
439 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
Zhaoleie621f262008-11-04 13:35:07 -0800440 if (!d) {
441 err = -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300442 goto err_out;
443 }
444 return 0;
445
446err_out:
447 d = c->dent;
448 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
449 debugfs_remove(child);
450 debugfs_remove(c->dent);
451 return err;
452}
453
454static int clk_debugfs_register(struct clk *c)
455{
456 int err;
457 struct clk *pa = c->parent;
458
459 if (pa && !pa->dent) {
460 err = clk_debugfs_register(pa);
461 if (err)
462 return err;
463 }
464
465 if (!c->dent) {
466 err = clk_debugfs_register_one(c);
467 if (err)
468 return err;
469 }
470 return 0;
471}
472
473static int __init clk_debugfs_init(void)
474{
475 struct clk *c;
476 struct dentry *d;
477 int err;
478
479 d = debugfs_create_dir("clock", NULL);
Zhaoleie621f262008-11-04 13:35:07 -0800480 if (!d)
481 return -ENOMEM;
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300482 clk_debugfs_root = d;
483
484 list_for_each_entry(c, &clocks, node) {
485 err = clk_debugfs_register(c);
486 if (err)
487 goto err_out;
488 }
489 return 0;
490err_out:
Hiroshi DOYUca4caa42009-09-03 20:14:06 +0300491 debugfs_remove_recursive(clk_debugfs_root);
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300492 return err;
493}
494late_initcall(clk_debugfs_init);
495
496#endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */