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