Merge branch 'for-2.6.33' of git://linux-nfs.org/~bfields/linux
[pandora-kernel.git] / arch / arm / plat-omap / clock.c
1 /*
2  *  linux/arch/arm/plat-omap/clock.c
3  *
4  *  Copyright (C) 2004 - 2008 Nokia corporation
5  *  Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6  *
7  *  Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8  *
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  */
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>
26
27 #include <plat/clock.h>
28
29 static LIST_HEAD(clocks);
30 static DEFINE_MUTEX(clocks_mutex);
31 static DEFINE_SPINLOCK(clockfw_lock);
32
33 static struct clk_functions *arch_clock;
34
35 /*-------------------------------------------------------------------------
36  * Standard clock functions defined in include/linux/clk.h
37  *-------------------------------------------------------------------------*/
38
39 /* 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 int clk_enable(struct clk *clk)
44 {
45         unsigned long flags;
46         int ret = 0;
47
48         if (clk == NULL || IS_ERR(clk))
49                 return -EINVAL;
50
51         spin_lock_irqsave(&clockfw_lock, flags);
52         if (arch_clock->clk_enable)
53                 ret = arch_clock->clk_enable(clk);
54         spin_unlock_irqrestore(&clockfw_lock, flags);
55
56         return ret;
57 }
58 EXPORT_SYMBOL(clk_enable);
59
60 void clk_disable(struct clk *clk)
61 {
62         unsigned long flags;
63
64         if (clk == NULL || IS_ERR(clk))
65                 return;
66
67         spin_lock_irqsave(&clockfw_lock, flags);
68         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
75         if (arch_clock->clk_disable)
76                 arch_clock->clk_disable(clk);
77
78 out:
79         spin_unlock_irqrestore(&clockfw_lock, flags);
80 }
81 EXPORT_SYMBOL(clk_disable);
82
83 unsigned long clk_get_rate(struct clk *clk)
84 {
85         unsigned long flags;
86         unsigned long ret = 0;
87
88         if (clk == NULL || IS_ERR(clk))
89                 return 0;
90
91         spin_lock_irqsave(&clockfw_lock, flags);
92         ret = clk->rate;
93         spin_unlock_irqrestore(&clockfw_lock, flags);
94
95         return ret;
96 }
97 EXPORT_SYMBOL(clk_get_rate);
98
99 /*-------------------------------------------------------------------------
100  * Optional clock functions defined in include/linux/clk.h
101  *-------------------------------------------------------------------------*/
102
103 long clk_round_rate(struct clk *clk, unsigned long rate)
104 {
105         unsigned long flags;
106         long ret = 0;
107
108         if (clk == NULL || IS_ERR(clk))
109                 return ret;
110
111         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);
115
116         return ret;
117 }
118 EXPORT_SYMBOL(clk_round_rate);
119
120 int clk_set_rate(struct clk *clk, unsigned long rate)
121 {
122         unsigned long flags;
123         int ret = -EINVAL;
124
125         if (clk == NULL || IS_ERR(clk))
126                 return ret;
127
128         spin_lock_irqsave(&clockfw_lock, flags);
129         if (arch_clock->clk_set_rate)
130                 ret = arch_clock->clk_set_rate(clk, rate);
131         if (ret == 0) {
132                 if (clk->recalc)
133                         clk->rate = clk->recalc(clk);
134                 propagate_rate(clk);
135         }
136         spin_unlock_irqrestore(&clockfw_lock, flags);
137
138         return ret;
139 }
140 EXPORT_SYMBOL(clk_set_rate);
141
142 int clk_set_parent(struct clk *clk, struct clk *parent)
143 {
144         unsigned long flags;
145         int ret = -EINVAL;
146
147         if (cpu_is_omap44xx())
148         /* OMAP4 clk framework not supported yet */
149                 return 0;
150         if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
151                 return ret;
152
153         spin_lock_irqsave(&clockfw_lock, flags);
154         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;
164         spin_unlock_irqrestore(&clockfw_lock, flags);
165
166         return ret;
167 }
168 EXPORT_SYMBOL(clk_set_parent);
169
170 struct clk *clk_get_parent(struct clk *clk)
171 {
172         return clk->parent;
173 }
174 EXPORT_SYMBOL(clk_get_parent);
175
176 /*-------------------------------------------------------------------------
177  * OMAP specific clock functions shared between omap1 and omap2
178  *-------------------------------------------------------------------------*/
179
180 unsigned 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  */
186 static 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 */
201 unsigned long followparent_recalc(struct clk *clk)
202 {
203         return clk->parent->rate;
204 }
205
206 void 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
217 /* Propagate rate to children */
218 void propagate_rate(struct clk * tclk)
219 {
220         struct clk *clkp;
221
222         list_for_each_entry(clkp, &tclk->children, sibling) {
223                 if (clkp->recalc)
224                         clkp->rate = clkp->recalc(clkp);
225                 propagate_rate(clkp);
226         }
227 }
228
229 static LIST_HEAD(root_clks);
230
231 /**
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  */
238 void recalculate_root_clocks(void)
239 {
240         struct clk *clkp;
241
242         list_for_each_entry(clkp, &root_clks, sibling) {
243                 if (clkp->recalc)
244                         clkp->rate = clkp->recalc(clkp);
245                 propagate_rate(clkp);
246         }
247 }
248
249 /**
250  * clk_preinit - initialize any fields in the struct clk before clk init
251  * @clk: struct clk * to initialize
252  *
253  * Initialize any struct clk fields needed before normal clk initialization
254  * can run.  No return value.
255  */
256 void clk_preinit(struct clk *clk)
257 {
258         INIT_LIST_HEAD(&clk->children);
259 }
260
261 int clk_register(struct clk *clk)
262 {
263         if (clk == NULL || IS_ERR(clk))
264                 return -EINVAL;
265
266         /*
267          * trap out already registered clocks
268          */
269         if (clk->node.next || clk->node.prev)
270                 return 0;
271
272         mutex_lock(&clocks_mutex);
273         if (clk->parent)
274                 list_add(&clk->sibling, &clk->parent->children);
275         else
276                 list_add(&clk->sibling, &root_clks);
277
278         list_add(&clk->node, &clocks);
279         if (clk->init)
280                 clk->init(clk);
281         mutex_unlock(&clocks_mutex);
282
283         return 0;
284 }
285 EXPORT_SYMBOL(clk_register);
286
287 void clk_unregister(struct clk *clk)
288 {
289         if (clk == NULL || IS_ERR(clk))
290                 return;
291
292         mutex_lock(&clocks_mutex);
293         list_del(&clk->sibling);
294         list_del(&clk->node);
295         mutex_unlock(&clocks_mutex);
296 }
297 EXPORT_SYMBOL(clk_unregister);
298
299 void 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 }
308 EXPORT_SYMBOL(clk_enable_init_clocks);
309
310 /*
311  * Low level helpers
312  */
313 static int clkll_enable_null(struct clk *clk)
314 {
315         return 0;
316 }
317
318 static void clkll_disable_null(struct clk *clk)
319 {
320 }
321
322 const struct clkops clkops_null = {
323         .enable         = clkll_enable_null,
324         .disable        = clkll_disable_null,
325 };
326
327 #ifdef CONFIG_CPU_FREQ
328 void 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 }
337 EXPORT_SYMBOL(clk_init_cpufreq_table);
338 #endif
339
340 /*-------------------------------------------------------------------------*/
341
342 #ifdef CONFIG_OMAP_RESET_CLOCKS
343 /*
344  * Disable any unused clocks left on by the bootloader
345  */
346 static int __init clk_disable_unused(void)
347 {
348         struct clk *ck;
349         unsigned long flags;
350
351         list_for_each_entry(ck, &clocks, node) {
352                 if (ck->ops == &clkops_null)
353                         continue;
354
355                 if (ck->usecount > 0 || ck->enable_reg == 0)
356                         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 }
366 late_initcall(clk_disable_unused);
367 #endif
368
369 int __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();
374         }
375
376         arch_clock = custom_clocks;
377
378         return 0;
379 }
380
381 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
382 /*
383  *      debugfs support to trace clock tree hierarchy and attributes
384  */
385 static struct dentry *clk_debugfs_root;
386
387 static 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);
399         if (!d)
400                 return -ENOMEM;
401         c->dent = d;
402
403         d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
404         if (!d) {
405                 err = -ENOMEM;
406                 goto err_out;
407         }
408         d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
409         if (!d) {
410                 err = -ENOMEM;
411                 goto err_out;
412         }
413         d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
414         if (!d) {
415                 err = -ENOMEM;
416                 goto err_out;
417         }
418         return 0;
419
420 err_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
428 static 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
447 static 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);
454         if (!d)
455                 return -ENOMEM;
456         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;
464 err_out:
465         debugfs_remove_recursive(clk_debugfs_root);
466         return err;
467 }
468 late_initcall(clk_debugfs_init);
469
470 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */