b857d117a45a7f686b5ca83448f3649531839af1
[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 /* Dummy hooks only for OMAP4.For rest OMAPs, common clkdev is used */
44 #if defined(CONFIG_ARCH_OMAP4)
45 struct clk *clk_get(struct device *dev, const char *id)
46 {
47         return NULL;
48 }
49 EXPORT_SYMBOL(clk_get);
50
51 void clk_put(struct clk *clk)
52 {
53 }
54 EXPORT_SYMBOL(clk_put);
55
56 void omap2_clk_prepare_for_reboot(void)
57 {
58 }
59 EXPORT_SYMBOL(omap2_clk_prepare_for_reboot);
60 #endif
61 int clk_enable(struct clk *clk)
62 {
63         unsigned long flags;
64         int ret = 0;
65         if (cpu_is_omap44xx())
66                 /* OMAP4 clk framework not supported yet */
67                 return 0;
68
69         if (clk == NULL || IS_ERR(clk))
70                 return -EINVAL;
71
72         spin_lock_irqsave(&clockfw_lock, flags);
73         if (arch_clock->clk_enable)
74                 ret = arch_clock->clk_enable(clk);
75         spin_unlock_irqrestore(&clockfw_lock, flags);
76
77         return ret;
78 }
79 EXPORT_SYMBOL(clk_enable);
80
81 void clk_disable(struct clk *clk)
82 {
83         unsigned long flags;
84
85         if (clk == NULL || IS_ERR(clk))
86                 return;
87
88         spin_lock_irqsave(&clockfw_lock, flags);
89         if (clk->usecount == 0) {
90                 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
91                        clk->name);
92                 WARN_ON(1);
93                 goto out;
94         }
95
96         if (arch_clock->clk_disable)
97                 arch_clock->clk_disable(clk);
98
99 out:
100         spin_unlock_irqrestore(&clockfw_lock, flags);
101 }
102 EXPORT_SYMBOL(clk_disable);
103
104 unsigned long clk_get_rate(struct clk *clk)
105 {
106         unsigned long flags;
107         unsigned long ret = 0;
108
109         if (clk == NULL || IS_ERR(clk))
110                 return 0;
111
112         spin_lock_irqsave(&clockfw_lock, flags);
113         ret = clk->rate;
114         spin_unlock_irqrestore(&clockfw_lock, flags);
115
116         return ret;
117 }
118 EXPORT_SYMBOL(clk_get_rate);
119
120 /*-------------------------------------------------------------------------
121  * Optional clock functions defined in include/linux/clk.h
122  *-------------------------------------------------------------------------*/
123
124 long clk_round_rate(struct clk *clk, unsigned long rate)
125 {
126         unsigned long flags;
127         long ret = 0;
128
129         if (clk == NULL || IS_ERR(clk))
130                 return ret;
131
132         spin_lock_irqsave(&clockfw_lock, flags);
133         if (arch_clock->clk_round_rate)
134                 ret = arch_clock->clk_round_rate(clk, rate);
135         spin_unlock_irqrestore(&clockfw_lock, flags);
136
137         return ret;
138 }
139 EXPORT_SYMBOL(clk_round_rate);
140
141 int clk_set_rate(struct clk *clk, unsigned long rate)
142 {
143         unsigned long flags;
144         int ret = -EINVAL;
145
146         if (clk == NULL || IS_ERR(clk))
147                 return ret;
148
149         spin_lock_irqsave(&clockfw_lock, flags);
150         if (arch_clock->clk_set_rate)
151                 ret = arch_clock->clk_set_rate(clk, rate);
152         if (ret == 0) {
153                 if (clk->recalc)
154                         clk->rate = clk->recalc(clk);
155                 propagate_rate(clk);
156         }
157         spin_unlock_irqrestore(&clockfw_lock, flags);
158
159         return ret;
160 }
161 EXPORT_SYMBOL(clk_set_rate);
162
163 int clk_set_parent(struct clk *clk, struct clk *parent)
164 {
165         unsigned long flags;
166         int ret = -EINVAL;
167
168         if (cpu_is_omap44xx())
169         /* OMAP4 clk framework not supported yet */
170                 return 0;
171         if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
172                 return ret;
173
174         spin_lock_irqsave(&clockfw_lock, flags);
175         if (clk->usecount == 0) {
176                 if (arch_clock->clk_set_parent)
177                         ret = arch_clock->clk_set_parent(clk, parent);
178                 if (ret == 0) {
179                         if (clk->recalc)
180                                 clk->rate = clk->recalc(clk);
181                         propagate_rate(clk);
182                 }
183         } else
184                 ret = -EBUSY;
185         spin_unlock_irqrestore(&clockfw_lock, flags);
186
187         return ret;
188 }
189 EXPORT_SYMBOL(clk_set_parent);
190
191 struct clk *clk_get_parent(struct clk *clk)
192 {
193         return clk->parent;
194 }
195 EXPORT_SYMBOL(clk_get_parent);
196
197 /*-------------------------------------------------------------------------
198  * OMAP specific clock functions shared between omap1 and omap2
199  *-------------------------------------------------------------------------*/
200
201 unsigned int __initdata mpurate;
202
203 /*
204  * By default we use the rate set by the bootloader.
205  * You can override this with mpurate= cmdline option.
206  */
207 static int __init omap_clk_setup(char *str)
208 {
209         get_option(&str, &mpurate);
210
211         if (!mpurate)
212                 return 1;
213
214         if (mpurate < 1000)
215                 mpurate *= 1000000;
216
217         return 1;
218 }
219 __setup("mpurate=", omap_clk_setup);
220
221 /* Used for clocks that always have same value as the parent clock */
222 unsigned long followparent_recalc(struct clk *clk)
223 {
224         return clk->parent->rate;
225 }
226
227 void clk_reparent(struct clk *child, struct clk *parent)
228 {
229         list_del_init(&child->sibling);
230         if (parent)
231                 list_add(&child->sibling, &parent->children);
232         child->parent = parent;
233
234         /* now do the debugfs renaming to reattach the child
235            to the proper parent */
236 }
237
238 /* Propagate rate to children */
239 void propagate_rate(struct clk * tclk)
240 {
241         struct clk *clkp;
242
243         list_for_each_entry(clkp, &tclk->children, sibling) {
244                 if (clkp->recalc)
245                         clkp->rate = clkp->recalc(clkp);
246                 propagate_rate(clkp);
247         }
248 }
249
250 static LIST_HEAD(root_clks);
251
252 /**
253  * recalculate_root_clocks - recalculate and propagate all root clocks
254  *
255  * Recalculates all root clocks (clocks with no parent), which if the
256  * clock's .recalc is set correctly, should also propagate their rates.
257  * Called at init.
258  */
259 void recalculate_root_clocks(void)
260 {
261         struct clk *clkp;
262
263         list_for_each_entry(clkp, &root_clks, sibling) {
264                 if (clkp->recalc)
265                         clkp->rate = clkp->recalc(clkp);
266                 propagate_rate(clkp);
267         }
268 }
269
270 /**
271  * clk_preinit - initialize any fields in the struct clk before clk init
272  * @clk: struct clk * to initialize
273  *
274  * Initialize any struct clk fields needed before normal clk initialization
275  * can run.  No return value.
276  */
277 void clk_preinit(struct clk *clk)
278 {
279         INIT_LIST_HEAD(&clk->children);
280 }
281
282 int clk_register(struct clk *clk)
283 {
284         if (clk == NULL || IS_ERR(clk))
285                 return -EINVAL;
286
287         /*
288          * trap out already registered clocks
289          */
290         if (clk->node.next || clk->node.prev)
291                 return 0;
292
293         mutex_lock(&clocks_mutex);
294         if (clk->parent)
295                 list_add(&clk->sibling, &clk->parent->children);
296         else
297                 list_add(&clk->sibling, &root_clks);
298
299         list_add(&clk->node, &clocks);
300         if (clk->init)
301                 clk->init(clk);
302         mutex_unlock(&clocks_mutex);
303
304         return 0;
305 }
306 EXPORT_SYMBOL(clk_register);
307
308 void clk_unregister(struct clk *clk)
309 {
310         if (clk == NULL || IS_ERR(clk))
311                 return;
312
313         mutex_lock(&clocks_mutex);
314         list_del(&clk->sibling);
315         list_del(&clk->node);
316         mutex_unlock(&clocks_mutex);
317 }
318 EXPORT_SYMBOL(clk_unregister);
319
320 void clk_enable_init_clocks(void)
321 {
322         struct clk *clkp;
323
324         list_for_each_entry(clkp, &clocks, node) {
325                 if (clkp->flags & ENABLE_ON_INIT)
326                         clk_enable(clkp);
327         }
328 }
329 EXPORT_SYMBOL(clk_enable_init_clocks);
330
331 /*
332  * Low level helpers
333  */
334 static int clkll_enable_null(struct clk *clk)
335 {
336         return 0;
337 }
338
339 static void clkll_disable_null(struct clk *clk)
340 {
341 }
342
343 const struct clkops clkops_null = {
344         .enable         = clkll_enable_null,
345         .disable        = clkll_disable_null,
346 };
347
348 #ifdef CONFIG_CPU_FREQ
349 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
350 {
351         unsigned long flags;
352
353         spin_lock_irqsave(&clockfw_lock, flags);
354         if (arch_clock->clk_init_cpufreq_table)
355                 arch_clock->clk_init_cpufreq_table(table);
356         spin_unlock_irqrestore(&clockfw_lock, flags);
357 }
358 EXPORT_SYMBOL(clk_init_cpufreq_table);
359 #endif
360
361 /*-------------------------------------------------------------------------*/
362
363 #ifdef CONFIG_OMAP_RESET_CLOCKS
364 /*
365  * Disable any unused clocks left on by the bootloader
366  */
367 static int __init clk_disable_unused(void)
368 {
369         struct clk *ck;
370         unsigned long flags;
371
372         list_for_each_entry(ck, &clocks, node) {
373                 if (ck->ops == &clkops_null)
374                         continue;
375
376                 if (ck->usecount > 0 || ck->enable_reg == 0)
377                         continue;
378
379                 spin_lock_irqsave(&clockfw_lock, flags);
380                 if (arch_clock->clk_disable_unused)
381                         arch_clock->clk_disable_unused(ck);
382                 spin_unlock_irqrestore(&clockfw_lock, flags);
383         }
384
385         return 0;
386 }
387 late_initcall(clk_disable_unused);
388 #endif
389
390 int __init clk_init(struct clk_functions * custom_clocks)
391 {
392         if (!custom_clocks) {
393                 printk(KERN_ERR "No custom clock functions registered\n");
394                 BUG();
395         }
396
397         arch_clock = custom_clocks;
398
399         return 0;
400 }
401
402 #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
403 /*
404  *      debugfs support to trace clock tree hierarchy and attributes
405  */
406 static struct dentry *clk_debugfs_root;
407
408 static int clk_debugfs_register_one(struct clk *c)
409 {
410         int err;
411         struct dentry *d, *child;
412         struct clk *pa = c->parent;
413         char s[255];
414         char *p = s;
415
416         p += sprintf(p, "%s", c->name);
417         if (c->id != 0)
418                 sprintf(p, ":%d", c->id);
419         d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
420         if (!d)
421                 return -ENOMEM;
422         c->dent = d;
423
424         d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
425         if (!d) {
426                 err = -ENOMEM;
427                 goto err_out;
428         }
429         d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
430         if (!d) {
431                 err = -ENOMEM;
432                 goto err_out;
433         }
434         d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
435         if (!d) {
436                 err = -ENOMEM;
437                 goto err_out;
438         }
439         return 0;
440
441 err_out:
442         d = c->dent;
443         list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
444                 debugfs_remove(child);
445         debugfs_remove(c->dent);
446         return err;
447 }
448
449 static int clk_debugfs_register(struct clk *c)
450 {
451         int err;
452         struct clk *pa = c->parent;
453
454         if (pa && !pa->dent) {
455                 err = clk_debugfs_register(pa);
456                 if (err)
457                         return err;
458         }
459
460         if (!c->dent) {
461                 err = clk_debugfs_register_one(c);
462                 if (err)
463                         return err;
464         }
465         return 0;
466 }
467
468 static int __init clk_debugfs_init(void)
469 {
470         struct clk *c;
471         struct dentry *d;
472         int err;
473
474         d = debugfs_create_dir("clock", NULL);
475         if (!d)
476                 return -ENOMEM;
477         clk_debugfs_root = d;
478
479         list_for_each_entry(c, &clocks, node) {
480                 err = clk_debugfs_register(c);
481                 if (err)
482                         goto err_out;
483         }
484         return 0;
485 err_out:
486         debugfs_remove_recursive(clk_debugfs_root);
487         return err;
488 }
489 late_initcall(clk_debugfs_init);
490
491 #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */