Merge branch 'linus' into x86/i8259
[sfrench/cifs-2.6.git] / arch / arm / plat-omap / clock.c
1 /*
2  *  linux/arch/arm/plat-omap/clock.c
3  *
4  *  Copyright (C) 2004 - 2005 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/version.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/list.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/clk.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/cpufreq.h>
25
26 #include <asm/io.h>
27
28 #include <asm/arch/clock.h>
29
30 static LIST_HEAD(clocks);
31 static DEFINE_MUTEX(clocks_mutex);
32 static DEFINE_SPINLOCK(clockfw_lock);
33
34 static struct clk_functions *arch_clock;
35
36 #ifdef CONFIG_PM_DEBUG
37
38 static void print_parents(struct clk *clk)
39 {
40         struct clk *p;
41         int printed = 0;
42
43         list_for_each_entry(p, &clocks, node) {
44                 if (p->parent == clk && p->usecount) {
45                         if (!clk->usecount && !printed) {
46                                 printk("MISMATCH: %s\n", clk->name);
47                                 printed = 1;
48                         }
49                         printk("\t%-15s\n", p->name);
50                 }
51         }
52 }
53
54 void clk_print_usecounts(void)
55 {
56         unsigned long flags;
57         struct clk *p;
58
59         spin_lock_irqsave(&clockfw_lock, flags);
60         list_for_each_entry(p, &clocks, node) {
61                 if (p->usecount)
62                         printk("%-15s: %d\n", p->name, p->usecount);
63                 print_parents(p);
64
65         }
66         spin_unlock_irqrestore(&clockfw_lock, flags);
67 }
68
69 #endif
70
71 /*-------------------------------------------------------------------------
72  * Standard clock functions defined in include/linux/clk.h
73  *-------------------------------------------------------------------------*/
74
75 /*
76  * Returns a clock. Note that we first try to use device id on the bus
77  * and clock name. If this fails, we try to use clock name only.
78  */
79 struct clk * clk_get(struct device *dev, const char *id)
80 {
81         struct clk *p, *clk = ERR_PTR(-ENOENT);
82         int idno;
83
84         if (dev == NULL || dev->bus != &platform_bus_type)
85                 idno = -1;
86         else
87                 idno = to_platform_device(dev)->id;
88
89         mutex_lock(&clocks_mutex);
90
91         list_for_each_entry(p, &clocks, node) {
92                 if (p->id == idno &&
93                     strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
94                         clk = p;
95                         goto found;
96                 }
97         }
98
99         list_for_each_entry(p, &clocks, node) {
100                 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
101                         clk = p;
102                         break;
103                 }
104         }
105
106 found:
107         mutex_unlock(&clocks_mutex);
108
109         return clk;
110 }
111 EXPORT_SYMBOL(clk_get);
112
113 int clk_enable(struct clk *clk)
114 {
115         unsigned long flags;
116         int ret = 0;
117
118         if (clk == NULL || IS_ERR(clk))
119                 return -EINVAL;
120
121         spin_lock_irqsave(&clockfw_lock, flags);
122         if (arch_clock->clk_enable)
123                 ret = arch_clock->clk_enable(clk);
124         spin_unlock_irqrestore(&clockfw_lock, flags);
125
126         return ret;
127 }
128 EXPORT_SYMBOL(clk_enable);
129
130 void clk_disable(struct clk *clk)
131 {
132         unsigned long flags;
133
134         if (clk == NULL || IS_ERR(clk))
135                 return;
136
137         spin_lock_irqsave(&clockfw_lock, flags);
138         if (clk->usecount == 0) {
139                 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
140                        clk->name);
141                 WARN_ON(1);
142                 goto out;
143         }
144
145         if (arch_clock->clk_disable)
146                 arch_clock->clk_disable(clk);
147
148 out:
149         spin_unlock_irqrestore(&clockfw_lock, flags);
150 }
151 EXPORT_SYMBOL(clk_disable);
152
153 int clk_get_usecount(struct clk *clk)
154 {
155         unsigned long flags;
156         int ret = 0;
157
158         if (clk == NULL || IS_ERR(clk))
159                 return 0;
160
161         spin_lock_irqsave(&clockfw_lock, flags);
162         ret = clk->usecount;
163         spin_unlock_irqrestore(&clockfw_lock, flags);
164
165         return ret;
166 }
167 EXPORT_SYMBOL(clk_get_usecount);
168
169 unsigned long clk_get_rate(struct clk *clk)
170 {
171         unsigned long flags;
172         unsigned long ret = 0;
173
174         if (clk == NULL || IS_ERR(clk))
175                 return 0;
176
177         spin_lock_irqsave(&clockfw_lock, flags);
178         ret = clk->rate;
179         spin_unlock_irqrestore(&clockfw_lock, flags);
180
181         return ret;
182 }
183 EXPORT_SYMBOL(clk_get_rate);
184
185 void clk_put(struct clk *clk)
186 {
187         if (clk && !IS_ERR(clk))
188                 module_put(clk->owner);
189 }
190 EXPORT_SYMBOL(clk_put);
191
192 /*-------------------------------------------------------------------------
193  * Optional clock functions defined in include/linux/clk.h
194  *-------------------------------------------------------------------------*/
195
196 long clk_round_rate(struct clk *clk, unsigned long rate)
197 {
198         unsigned long flags;
199         long ret = 0;
200
201         if (clk == NULL || IS_ERR(clk))
202                 return ret;
203
204         spin_lock_irqsave(&clockfw_lock, flags);
205         if (arch_clock->clk_round_rate)
206                 ret = arch_clock->clk_round_rate(clk, rate);
207         spin_unlock_irqrestore(&clockfw_lock, flags);
208
209         return ret;
210 }
211 EXPORT_SYMBOL(clk_round_rate);
212
213 int clk_set_rate(struct clk *clk, unsigned long rate)
214 {
215         unsigned long flags;
216         int ret = -EINVAL;
217
218         if (clk == NULL || IS_ERR(clk))
219                 return ret;
220
221         spin_lock_irqsave(&clockfw_lock, flags);
222         if (arch_clock->clk_set_rate)
223                 ret = arch_clock->clk_set_rate(clk, rate);
224         spin_unlock_irqrestore(&clockfw_lock, flags);
225
226         return ret;
227 }
228 EXPORT_SYMBOL(clk_set_rate);
229
230 int clk_set_parent(struct clk *clk, struct clk *parent)
231 {
232         unsigned long flags;
233         int ret = -EINVAL;
234
235         if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
236                 return ret;
237
238         spin_lock_irqsave(&clockfw_lock, flags);
239         if (arch_clock->clk_set_parent)
240                 ret =  arch_clock->clk_set_parent(clk, parent);
241         spin_unlock_irqrestore(&clockfw_lock, flags);
242
243         return ret;
244 }
245 EXPORT_SYMBOL(clk_set_parent);
246
247 struct clk *clk_get_parent(struct clk *clk)
248 {
249         unsigned long flags;
250         struct clk * ret = NULL;
251
252         if (clk == NULL || IS_ERR(clk))
253                 return ret;
254
255         spin_lock_irqsave(&clockfw_lock, flags);
256         if (arch_clock->clk_get_parent)
257                 ret = arch_clock->clk_get_parent(clk);
258         spin_unlock_irqrestore(&clockfw_lock, flags);
259
260         return ret;
261 }
262 EXPORT_SYMBOL(clk_get_parent);
263
264 /*-------------------------------------------------------------------------
265  * OMAP specific clock functions shared between omap1 and omap2
266  *-------------------------------------------------------------------------*/
267
268 unsigned int __initdata mpurate;
269
270 /*
271  * By default we use the rate set by the bootloader.
272  * You can override this with mpurate= cmdline option.
273  */
274 static int __init omap_clk_setup(char *str)
275 {
276         get_option(&str, &mpurate);
277
278         if (!mpurate)
279                 return 1;
280
281         if (mpurate < 1000)
282                 mpurate *= 1000000;
283
284         return 1;
285 }
286 __setup("mpurate=", omap_clk_setup);
287
288 /* Used for clocks that always have same value as the parent clock */
289 void followparent_recalc(struct clk *clk)
290 {
291         if (clk == NULL || IS_ERR(clk))
292                 return;
293
294         clk->rate = clk->parent->rate;
295         if (unlikely(clk->flags & RATE_PROPAGATES))
296                 propagate_rate(clk);
297 }
298
299 /* Propagate rate to children */
300 void propagate_rate(struct clk * tclk)
301 {
302         struct clk *clkp;
303
304         if (tclk == NULL || IS_ERR(tclk))
305                 return;
306
307         list_for_each_entry(clkp, &clocks, node) {
308                 if (likely(clkp->parent != tclk))
309                         continue;
310                 if (likely((u32)clkp->recalc))
311                         clkp->recalc(clkp);
312         }
313 }
314
315 /**
316  * recalculate_root_clocks - recalculate and propagate all root clocks
317  *
318  * Recalculates all root clocks (clocks with no parent), which if the
319  * clock's .recalc is set correctly, should also propagate their rates.
320  * Called at init.
321  */
322 void recalculate_root_clocks(void)
323 {
324         struct clk *clkp;
325
326         list_for_each_entry(clkp, &clocks, node) {
327                 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
328                         clkp->recalc(clkp);
329         }
330 }
331
332 int clk_register(struct clk *clk)
333 {
334         if (clk == NULL || IS_ERR(clk))
335                 return -EINVAL;
336
337         mutex_lock(&clocks_mutex);
338         list_add(&clk->node, &clocks);
339         if (clk->init)
340                 clk->init(clk);
341         mutex_unlock(&clocks_mutex);
342
343         return 0;
344 }
345 EXPORT_SYMBOL(clk_register);
346
347 void clk_unregister(struct clk *clk)
348 {
349         if (clk == NULL || IS_ERR(clk))
350                 return;
351
352         mutex_lock(&clocks_mutex);
353         list_del(&clk->node);
354         mutex_unlock(&clocks_mutex);
355 }
356 EXPORT_SYMBOL(clk_unregister);
357
358 void clk_deny_idle(struct clk *clk)
359 {
360         unsigned long flags;
361
362         if (clk == NULL || IS_ERR(clk))
363                 return;
364
365         spin_lock_irqsave(&clockfw_lock, flags);
366         if (arch_clock->clk_deny_idle)
367                 arch_clock->clk_deny_idle(clk);
368         spin_unlock_irqrestore(&clockfw_lock, flags);
369 }
370 EXPORT_SYMBOL(clk_deny_idle);
371
372 void clk_allow_idle(struct clk *clk)
373 {
374         unsigned long flags;
375
376         if (clk == NULL || IS_ERR(clk))
377                 return;
378
379         spin_lock_irqsave(&clockfw_lock, flags);
380         if (arch_clock->clk_allow_idle)
381                 arch_clock->clk_allow_idle(clk);
382         spin_unlock_irqrestore(&clockfw_lock, flags);
383 }
384 EXPORT_SYMBOL(clk_allow_idle);
385
386 void clk_enable_init_clocks(void)
387 {
388         struct clk *clkp;
389
390         list_for_each_entry(clkp, &clocks, node) {
391                 if (clkp->flags & ENABLE_ON_INIT)
392                         clk_enable(clkp);
393         }
394 }
395 EXPORT_SYMBOL(clk_enable_init_clocks);
396
397 #ifdef CONFIG_CPU_FREQ
398 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
399 {
400         unsigned long flags;
401
402         spin_lock_irqsave(&clockfw_lock, flags);
403         if (arch_clock->clk_init_cpufreq_table)
404                 arch_clock->clk_init_cpufreq_table(table);
405         spin_unlock_irqrestore(&clockfw_lock, flags);
406 }
407 EXPORT_SYMBOL(clk_init_cpufreq_table);
408 #endif
409
410 /*-------------------------------------------------------------------------*/
411
412 #ifdef CONFIG_OMAP_RESET_CLOCKS
413 /*
414  * Disable any unused clocks left on by the bootloader
415  */
416 static int __init clk_disable_unused(void)
417 {
418         struct clk *ck;
419         unsigned long flags;
420
421         list_for_each_entry(ck, &clocks, node) {
422                 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
423                         ck->enable_reg == 0)
424                         continue;
425
426                 spin_lock_irqsave(&clockfw_lock, flags);
427                 if (arch_clock->clk_disable_unused)
428                         arch_clock->clk_disable_unused(ck);
429                 spin_unlock_irqrestore(&clockfw_lock, flags);
430         }
431
432         return 0;
433 }
434 late_initcall(clk_disable_unused);
435 #endif
436
437 int __init clk_init(struct clk_functions * custom_clocks)
438 {
439         if (!custom_clocks) {
440                 printk(KERN_ERR "No custom clock functions registered\n");
441                 BUG();
442         }
443
444         arch_clock = custom_clocks;
445
446         return 0;
447 }
448