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