Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
b9158556 | 2 | * linux/arch/arm/plat-omap/clock.c |
1da177e4 | 3 | * |
137b3ee2 | 4 | * Copyright (C) 2004 - 2008 Nokia corporation |
1da177e4 LT |
5 | * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com> |
6 | * | |
1a8bfa1e TL |
7 | * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com> |
8 | * | |
1da177e4 LT |
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 | */ | |
1da177e4 | 13 | #include <linux/kernel.h> |
1a8bfa1e | 14 | #include <linux/init.h> |
1da177e4 LT |
15 | #include <linux/list.h> |
16 | #include <linux/errno.h> | |
17 | #include <linux/err.h> | |
4e57b681 | 18 | #include <linux/string.h> |
f8ce2547 | 19 | #include <linux/clk.h> |
00431707 | 20 | #include <linux/mutex.h> |
b851cb28 | 21 | #include <linux/cpufreq.h> |
137b3ee2 | 22 | #include <linux/debugfs.h> |
fced80c7 | 23 | #include <linux/io.h> |
1da177e4 | 24 | |
ce491cf8 | 25 | #include <plat/clock.h> |
1da177e4 | 26 | |
7df3450e | 27 | static LIST_HEAD(clocks); |
00431707 | 28 | static DEFINE_MUTEX(clocks_mutex); |
7df3450e | 29 | static DEFINE_SPINLOCK(clockfw_lock); |
1da177e4 | 30 | |
1a8bfa1e | 31 | static struct clk_functions *arch_clock; |
1da177e4 | 32 | |
3587aeb0 | 33 | /* |
f07adc59 | 34 | * Standard clock functions defined in include/linux/clk.h |
3587aeb0 | 35 | */ |
1da177e4 | 36 | |
1da177e4 LT |
37 | int clk_enable(struct clk *clk) |
38 | { | |
39 | unsigned long flags; | |
1a8bfa1e | 40 | int ret = 0; |
1da177e4 | 41 | |
b824efae TL |
42 | if (clk == NULL || IS_ERR(clk)) |
43 | return -EINVAL; | |
44 | ||
1da177e4 | 45 | spin_lock_irqsave(&clockfw_lock, flags); |
f07adc59 | 46 | if (arch_clock->clk_enable) |
1a8bfa1e | 47 | ret = arch_clock->clk_enable(clk); |
1da177e4 | 48 | spin_unlock_irqrestore(&clockfw_lock, flags); |
1a8bfa1e | 49 | |
1da177e4 LT |
50 | return ret; |
51 | } | |
52 | EXPORT_SYMBOL(clk_enable); | |
53 | ||
1da177e4 LT |
54 | void clk_disable(struct clk *clk) |
55 | { | |
56 | unsigned long flags; | |
57 | ||
b824efae TL |
58 | if (clk == NULL || IS_ERR(clk)) |
59 | return; | |
60 | ||
1da177e4 | 61 | spin_lock_irqsave(&clockfw_lock, flags); |
7cf95774 TL |
62 | if (clk->usecount == 0) { |
63 | printk(KERN_ERR "Trying disable clock %s with 0 usecount\n", | |
64 | clk->name); | |
65 | WARN_ON(1); | |
66 | goto out; | |
67 | } | |
68 | ||
f07adc59 | 69 | if (arch_clock->clk_disable) |
1a8bfa1e | 70 | arch_clock->clk_disable(clk); |
7cf95774 TL |
71 | |
72 | out: | |
1da177e4 LT |
73 | spin_unlock_irqrestore(&clockfw_lock, flags); |
74 | } | |
75 | EXPORT_SYMBOL(clk_disable); | |
76 | ||
1a8bfa1e | 77 | unsigned long clk_get_rate(struct clk *clk) |
1da177e4 | 78 | { |
1a8bfa1e TL |
79 | unsigned long flags; |
80 | unsigned long ret = 0; | |
1da177e4 | 81 | |
b824efae TL |
82 | if (clk == NULL || IS_ERR(clk)) |
83 | return 0; | |
84 | ||
1a8bfa1e TL |
85 | spin_lock_irqsave(&clockfw_lock, flags); |
86 | ret = clk->rate; | |
87 | spin_unlock_irqrestore(&clockfw_lock, flags); | |
1da177e4 | 88 | |
1a8bfa1e | 89 | return ret; |
1da177e4 | 90 | } |
1a8bfa1e | 91 | EXPORT_SYMBOL(clk_get_rate); |
1da177e4 | 92 | |
3587aeb0 | 93 | /* |
f07adc59 | 94 | * Optional clock functions defined in include/linux/clk.h |
3587aeb0 | 95 | */ |
bb13b5fd | 96 | |
1da177e4 LT |
97 | long clk_round_rate(struct clk *clk, unsigned long rate) |
98 | { | |
1a8bfa1e TL |
99 | unsigned long flags; |
100 | long ret = 0; | |
1da177e4 | 101 | |
b824efae TL |
102 | if (clk == NULL || IS_ERR(clk)) |
103 | return ret; | |
104 | ||
1a8bfa1e TL |
105 | spin_lock_irqsave(&clockfw_lock, flags); |
106 | if (arch_clock->clk_round_rate) | |
107 | ret = arch_clock->clk_round_rate(clk, rate); | |
108 | spin_unlock_irqrestore(&clockfw_lock, flags); | |
1da177e4 | 109 | |
1a8bfa1e | 110 | return ret; |
1da177e4 LT |
111 | } |
112 | EXPORT_SYMBOL(clk_round_rate); | |
113 | ||
1a8bfa1e | 114 | int clk_set_rate(struct clk *clk, unsigned long rate) |
1da177e4 | 115 | { |
1a8bfa1e | 116 | unsigned long flags; |
b824efae TL |
117 | int ret = -EINVAL; |
118 | ||
119 | if (clk == NULL || IS_ERR(clk)) | |
120 | return ret; | |
bb13b5fd | 121 | |
1a8bfa1e TL |
122 | spin_lock_irqsave(&clockfw_lock, flags); |
123 | if (arch_clock->clk_set_rate) | |
124 | ret = arch_clock->clk_set_rate(clk, rate); | |
b5088c0d RK |
125 | if (ret == 0) { |
126 | if (clk->recalc) | |
8b9dbc16 | 127 | clk->rate = clk->recalc(clk); |
3f0a820c | 128 | propagate_rate(clk); |
b5088c0d | 129 | } |
1a8bfa1e | 130 | spin_unlock_irqrestore(&clockfw_lock, flags); |
1da177e4 | 131 | |
1a8bfa1e | 132 | return ret; |
1da177e4 | 133 | } |
1a8bfa1e | 134 | EXPORT_SYMBOL(clk_set_rate); |
1da177e4 | 135 | |
1a8bfa1e | 136 | int clk_set_parent(struct clk *clk, struct clk *parent) |
1da177e4 | 137 | { |
1a8bfa1e | 138 | unsigned long flags; |
b824efae TL |
139 | int ret = -EINVAL; |
140 | ||
141 | if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent)) | |
142 | return ret; | |
1da177e4 | 143 | |
1a8bfa1e | 144 | spin_lock_irqsave(&clockfw_lock, flags); |
4da37821 RK |
145 | if (clk->usecount == 0) { |
146 | if (arch_clock->clk_set_parent) | |
147 | ret = arch_clock->clk_set_parent(clk, parent); | |
148 | if (ret == 0) { | |
149 | if (clk->recalc) | |
150 | clk->rate = clk->recalc(clk); | |
151 | propagate_rate(clk); | |
152 | } | |
153 | } else | |
154 | ret = -EBUSY; | |
1a8bfa1e | 155 | spin_unlock_irqrestore(&clockfw_lock, flags); |
1da177e4 | 156 | |
1a8bfa1e | 157 | return ret; |
1da177e4 | 158 | } |
1a8bfa1e | 159 | EXPORT_SYMBOL(clk_set_parent); |
1da177e4 | 160 | |
1a8bfa1e | 161 | struct clk *clk_get_parent(struct clk *clk) |
1da177e4 | 162 | { |
2e777bf1 | 163 | return clk->parent; |
1da177e4 | 164 | } |
1a8bfa1e | 165 | EXPORT_SYMBOL(clk_get_parent); |
1da177e4 | 166 | |
3587aeb0 | 167 | /* |
1a8bfa1e | 168 | * OMAP specific clock functions shared between omap1 and omap2 |
3587aeb0 | 169 | */ |
1da177e4 | 170 | |
d3730192 | 171 | int __initdata mpurate; |
1da177e4 | 172 | |
1a8bfa1e TL |
173 | /* |
174 | * By default we use the rate set by the bootloader. | |
175 | * You can override this with mpurate= cmdline option. | |
176 | */ | |
177 | static int __init omap_clk_setup(char *str) | |
1da177e4 | 178 | { |
1a8bfa1e | 179 | get_option(&str, &mpurate); |
1da177e4 | 180 | |
1a8bfa1e TL |
181 | if (!mpurate) |
182 | return 1; | |
1da177e4 | 183 | |
1a8bfa1e TL |
184 | if (mpurate < 1000) |
185 | mpurate *= 1000000; | |
1da177e4 | 186 | |
1a8bfa1e | 187 | return 1; |
1da177e4 | 188 | } |
1a8bfa1e | 189 | __setup("mpurate=", omap_clk_setup); |
1da177e4 | 190 | |
1a8bfa1e | 191 | /* Used for clocks that always have same value as the parent clock */ |
8b9dbc16 | 192 | unsigned long followparent_recalc(struct clk *clk) |
1da177e4 | 193 | { |
8b9dbc16 | 194 | return clk->parent->rate; |
1da177e4 LT |
195 | } |
196 | ||
e9b98f60 PW |
197 | /* |
198 | * Used for clocks that have the same value as the parent clock, | |
199 | * divided by some factor | |
200 | */ | |
201 | unsigned long omap_fixed_divisor_recalc(struct clk *clk) | |
202 | { | |
203 | WARN_ON(!clk->fixed_div); | |
204 | ||
205 | return clk->parent->rate / clk->fixed_div; | |
206 | } | |
207 | ||
3f0a820c RK |
208 | void clk_reparent(struct clk *child, struct clk *parent) |
209 | { | |
210 | list_del_init(&child->sibling); | |
211 | if (parent) | |
212 | list_add(&child->sibling, &parent->children); | |
213 | child->parent = parent; | |
214 | ||
215 | /* now do the debugfs renaming to reattach the child | |
216 | to the proper parent */ | |
217 | } | |
218 | ||
1a8bfa1e | 219 | /* Propagate rate to children */ |
3587aeb0 | 220 | void propagate_rate(struct clk *tclk) |
1da177e4 | 221 | { |
1a8bfa1e | 222 | struct clk *clkp; |
1da177e4 | 223 | |
3f0a820c | 224 | list_for_each_entry(clkp, &tclk->children, sibling) { |
9a5fedac | 225 | if (clkp->recalc) |
8b9dbc16 | 226 | clkp->rate = clkp->recalc(clkp); |
3f0a820c | 227 | propagate_rate(clkp); |
1a8bfa1e | 228 | } |
1da177e4 LT |
229 | } |
230 | ||
3f0a820c RK |
231 | static LIST_HEAD(root_clks); |
232 | ||
6b8858a9 PW |
233 | /** |
234 | * recalculate_root_clocks - recalculate and propagate all root clocks | |
235 | * | |
236 | * Recalculates all root clocks (clocks with no parent), which if the | |
237 | * clock's .recalc is set correctly, should also propagate their rates. | |
238 | * Called at init. | |
239 | */ | |
240 | void recalculate_root_clocks(void) | |
241 | { | |
242 | struct clk *clkp; | |
243 | ||
3f0a820c RK |
244 | list_for_each_entry(clkp, &root_clks, sibling) { |
245 | if (clkp->recalc) | |
8b9dbc16 | 246 | clkp->rate = clkp->recalc(clkp); |
3f0a820c | 247 | propagate_rate(clkp); |
6b8858a9 PW |
248 | } |
249 | } | |
250 | ||
c8088112 | 251 | /** |
79716870 | 252 | * clk_preinit - initialize any fields in the struct clk before clk init |
c8088112 PW |
253 | * @clk: struct clk * to initialize |
254 | * | |
255 | * Initialize any struct clk fields needed before normal clk initialization | |
256 | * can run. No return value. | |
257 | */ | |
79716870 | 258 | void clk_preinit(struct clk *clk) |
3f0a820c RK |
259 | { |
260 | INIT_LIST_HEAD(&clk->children); | |
261 | } | |
262 | ||
1da177e4 LT |
263 | int clk_register(struct clk *clk) |
264 | { | |
b824efae TL |
265 | if (clk == NULL || IS_ERR(clk)) |
266 | return -EINVAL; | |
267 | ||
dbb674d5 RK |
268 | /* |
269 | * trap out already registered clocks | |
270 | */ | |
271 | if (clk->node.next || clk->node.prev) | |
272 | return 0; | |
273 | ||
00431707 | 274 | mutex_lock(&clocks_mutex); |
3f0a820c RK |
275 | if (clk->parent) |
276 | list_add(&clk->sibling, &clk->parent->children); | |
277 | else | |
278 | list_add(&clk->sibling, &root_clks); | |
279 | ||
1da177e4 LT |
280 | list_add(&clk->node, &clocks); |
281 | if (clk->init) | |
282 | clk->init(clk); | |
00431707 | 283 | mutex_unlock(&clocks_mutex); |
1a8bfa1e | 284 | |
1da177e4 LT |
285 | return 0; |
286 | } | |
287 | EXPORT_SYMBOL(clk_register); | |
288 | ||
289 | void clk_unregister(struct clk *clk) | |
290 | { | |
b824efae TL |
291 | if (clk == NULL || IS_ERR(clk)) |
292 | return; | |
293 | ||
00431707 | 294 | mutex_lock(&clocks_mutex); |
3f0a820c | 295 | list_del(&clk->sibling); |
1da177e4 | 296 | list_del(&clk->node); |
00431707 | 297 | mutex_unlock(&clocks_mutex); |
1da177e4 LT |
298 | } |
299 | EXPORT_SYMBOL(clk_unregister); | |
300 | ||
6b8858a9 PW |
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 | } | |
6b8858a9 | 310 | |
74be8427 PW |
311 | /** |
312 | * omap_clk_get_by_name - locate OMAP struct clk by its name | |
313 | * @name: name of the struct clk to locate | |
314 | * | |
315 | * Locate an OMAP struct clk by its name. Assumes that struct clk | |
316 | * names are unique. Returns NULL if not found or a pointer to the | |
317 | * struct clk if found. | |
318 | */ | |
319 | struct clk *omap_clk_get_by_name(const char *name) | |
320 | { | |
321 | struct clk *c; | |
322 | struct clk *ret = NULL; | |
323 | ||
324 | mutex_lock(&clocks_mutex); | |
325 | ||
326 | list_for_each_entry(c, &clocks, node) { | |
327 | if (!strcmp(c->name, name)) { | |
328 | ret = c; | |
329 | break; | |
330 | } | |
331 | } | |
332 | ||
333 | mutex_unlock(&clocks_mutex); | |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
897dcded RK |
338 | /* |
339 | * Low level helpers | |
340 | */ | |
341 | static int clkll_enable_null(struct clk *clk) | |
342 | { | |
343 | return 0; | |
344 | } | |
345 | ||
346 | static void clkll_disable_null(struct clk *clk) | |
347 | { | |
348 | } | |
349 | ||
350 | const struct clkops clkops_null = { | |
351 | .enable = clkll_enable_null, | |
352 | .disable = clkll_disable_null, | |
353 | }; | |
354 | ||
7c43d547 SS |
355 | /* |
356 | * Dummy clock | |
357 | * | |
358 | * Used for clock aliases that are needed on some OMAPs, but not others | |
359 | */ | |
360 | struct clk dummy_ck = { | |
361 | .name = "dummy", | |
362 | .ops = &clkops_null, | |
363 | }; | |
364 | ||
6b8858a9 PW |
365 | #ifdef CONFIG_CPU_FREQ |
366 | void clk_init_cpufreq_table(struct cpufreq_frequency_table **table) | |
367 | { | |
368 | unsigned long flags; | |
369 | ||
370 | spin_lock_irqsave(&clockfw_lock, flags); | |
371 | if (arch_clock->clk_init_cpufreq_table) | |
372 | arch_clock->clk_init_cpufreq_table(table); | |
373 | spin_unlock_irqrestore(&clockfw_lock, flags); | |
374 | } | |
4e37c10d PW |
375 | |
376 | void clk_exit_cpufreq_table(struct cpufreq_frequency_table **table) | |
377 | { | |
378 | unsigned long flags; | |
379 | ||
380 | spin_lock_irqsave(&clockfw_lock, flags); | |
381 | if (arch_clock->clk_exit_cpufreq_table) | |
382 | arch_clock->clk_exit_cpufreq_table(table); | |
383 | spin_unlock_irqrestore(&clockfw_lock, flags); | |
384 | } | |
6b8858a9 PW |
385 | #endif |
386 | ||
3587aeb0 PW |
387 | /* |
388 | * | |
389 | */ | |
bb13b5fd | 390 | |
90afd5cb TL |
391 | #ifdef CONFIG_OMAP_RESET_CLOCKS |
392 | /* | |
393 | * Disable any unused clocks left on by the bootloader | |
394 | */ | |
395 | static int __init clk_disable_unused(void) | |
396 | { | |
397 | struct clk *ck; | |
398 | unsigned long flags; | |
399 | ||
400 | list_for_each_entry(ck, &clocks, node) { | |
897dcded RK |
401 | if (ck->ops == &clkops_null) |
402 | continue; | |
403 | ||
3587aeb0 | 404 | if (ck->usecount > 0 || !ck->enable_reg) |
90afd5cb TL |
405 | continue; |
406 | ||
407 | spin_lock_irqsave(&clockfw_lock, flags); | |
408 | if (arch_clock->clk_disable_unused) | |
409 | arch_clock->clk_disable_unused(ck); | |
410 | spin_unlock_irqrestore(&clockfw_lock, flags); | |
411 | } | |
412 | ||
413 | return 0; | |
414 | } | |
415 | late_initcall(clk_disable_unused); | |
416 | #endif | |
417 | ||
1a8bfa1e | 418 | int __init clk_init(struct clk_functions * custom_clocks) |
bb13b5fd | 419 | { |
1a8bfa1e TL |
420 | if (!custom_clocks) { |
421 | printk(KERN_ERR "No custom clock functions registered\n"); | |
422 | BUG(); | |
bb13b5fd TL |
423 | } |
424 | ||
1a8bfa1e TL |
425 | arch_clock = custom_clocks; |
426 | ||
bb13b5fd TL |
427 | return 0; |
428 | } | |
6b8858a9 | 429 | |
137b3ee2 HD |
430 | #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) |
431 | /* | |
432 | * debugfs support to trace clock tree hierarchy and attributes | |
433 | */ | |
434 | static struct dentry *clk_debugfs_root; | |
435 | ||
436 | static int clk_debugfs_register_one(struct clk *c) | |
437 | { | |
438 | int err; | |
0825cc8a | 439 | struct dentry *d, *child, *child_tmp; |
137b3ee2 HD |
440 | struct clk *pa = c->parent; |
441 | char s[255]; | |
442 | char *p = s; | |
443 | ||
444 | p += sprintf(p, "%s", c->name); | |
137b3ee2 | 445 | d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root); |
e621f266 Z |
446 | if (!d) |
447 | return -ENOMEM; | |
137b3ee2 HD |
448 | c->dent = d; |
449 | ||
450 | d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount); | |
e621f266 Z |
451 | if (!d) { |
452 | err = -ENOMEM; | |
137b3ee2 HD |
453 | goto err_out; |
454 | } | |
455 | d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate); | |
e621f266 Z |
456 | if (!d) { |
457 | err = -ENOMEM; | |
137b3ee2 HD |
458 | goto err_out; |
459 | } | |
460 | d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags); | |
e621f266 Z |
461 | if (!d) { |
462 | err = -ENOMEM; | |
137b3ee2 HD |
463 | goto err_out; |
464 | } | |
465 | return 0; | |
466 | ||
467 | err_out: | |
468 | d = c->dent; | |
0825cc8a | 469 | list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child) |
137b3ee2 HD |
470 | debugfs_remove(child); |
471 | debugfs_remove(c->dent); | |
472 | return err; | |
473 | } | |
474 | ||
475 | static int clk_debugfs_register(struct clk *c) | |
476 | { | |
477 | int err; | |
478 | struct clk *pa = c->parent; | |
479 | ||
480 | if (pa && !pa->dent) { | |
481 | err = clk_debugfs_register(pa); | |
482 | if (err) | |
483 | return err; | |
484 | } | |
485 | ||
486 | if (!c->dent) { | |
487 | err = clk_debugfs_register_one(c); | |
488 | if (err) | |
489 | return err; | |
490 | } | |
491 | return 0; | |
492 | } | |
493 | ||
494 | static int __init clk_debugfs_init(void) | |
495 | { | |
496 | struct clk *c; | |
497 | struct dentry *d; | |
498 | int err; | |
499 | ||
500 | d = debugfs_create_dir("clock", NULL); | |
e621f266 Z |
501 | if (!d) |
502 | return -ENOMEM; | |
137b3ee2 HD |
503 | clk_debugfs_root = d; |
504 | ||
505 | list_for_each_entry(c, &clocks, node) { | |
506 | err = clk_debugfs_register(c); | |
507 | if (err) | |
508 | goto err_out; | |
509 | } | |
510 | return 0; | |
511 | err_out: | |
ca4caa4e | 512 | debugfs_remove_recursive(clk_debugfs_root); |
137b3ee2 HD |
513 | return err; |
514 | } | |
515 | late_initcall(clk_debugfs_init); | |
516 | ||
517 | #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */ |