Commit | Line | Data |
---|---|---|
b2476490 MT |
1 | /* |
2 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> | |
3 | * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Standard functionality for the common clock API. See Documentation/clk.txt | |
10 | */ | |
11 | ||
12 | #include <linux/clk-private.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/mutex.h> | |
15 | #include <linux/spinlock.h> | |
16 | #include <linux/err.h> | |
17 | #include <linux/list.h> | |
18 | #include <linux/slab.h> | |
766e6a4e | 19 | #include <linux/of.h> |
46c8773a | 20 | #include <linux/device.h> |
f2f6c255 | 21 | #include <linux/init.h> |
533ddeb1 | 22 | #include <linux/sched.h> |
b2476490 MT |
23 | |
24 | static DEFINE_SPINLOCK(enable_lock); | |
25 | static DEFINE_MUTEX(prepare_lock); | |
26 | ||
533ddeb1 MT |
27 | static struct task_struct *prepare_owner; |
28 | static struct task_struct *enable_owner; | |
29 | ||
30 | static int prepare_refcnt; | |
31 | static int enable_refcnt; | |
32 | ||
b2476490 MT |
33 | static HLIST_HEAD(clk_root_list); |
34 | static HLIST_HEAD(clk_orphan_list); | |
35 | static LIST_HEAD(clk_notifier_list); | |
36 | ||
eab89f69 MT |
37 | /*** locking ***/ |
38 | static void clk_prepare_lock(void) | |
39 | { | |
533ddeb1 MT |
40 | if (!mutex_trylock(&prepare_lock)) { |
41 | if (prepare_owner == current) { | |
42 | prepare_refcnt++; | |
43 | return; | |
44 | } | |
45 | mutex_lock(&prepare_lock); | |
46 | } | |
47 | WARN_ON_ONCE(prepare_owner != NULL); | |
48 | WARN_ON_ONCE(prepare_refcnt != 0); | |
49 | prepare_owner = current; | |
50 | prepare_refcnt = 1; | |
eab89f69 MT |
51 | } |
52 | ||
53 | static void clk_prepare_unlock(void) | |
54 | { | |
533ddeb1 MT |
55 | WARN_ON_ONCE(prepare_owner != current); |
56 | WARN_ON_ONCE(prepare_refcnt == 0); | |
57 | ||
58 | if (--prepare_refcnt) | |
59 | return; | |
60 | prepare_owner = NULL; | |
eab89f69 MT |
61 | mutex_unlock(&prepare_lock); |
62 | } | |
63 | ||
64 | static unsigned long clk_enable_lock(void) | |
65 | { | |
66 | unsigned long flags; | |
533ddeb1 MT |
67 | |
68 | if (!spin_trylock_irqsave(&enable_lock, flags)) { | |
69 | if (enable_owner == current) { | |
70 | enable_refcnt++; | |
71 | return flags; | |
72 | } | |
73 | spin_lock_irqsave(&enable_lock, flags); | |
74 | } | |
75 | WARN_ON_ONCE(enable_owner != NULL); | |
76 | WARN_ON_ONCE(enable_refcnt != 0); | |
77 | enable_owner = current; | |
78 | enable_refcnt = 1; | |
eab89f69 MT |
79 | return flags; |
80 | } | |
81 | ||
82 | static void clk_enable_unlock(unsigned long flags) | |
83 | { | |
533ddeb1 MT |
84 | WARN_ON_ONCE(enable_owner != current); |
85 | WARN_ON_ONCE(enable_refcnt == 0); | |
86 | ||
87 | if (--enable_refcnt) | |
88 | return; | |
89 | enable_owner = NULL; | |
eab89f69 MT |
90 | spin_unlock_irqrestore(&enable_lock, flags); |
91 | } | |
92 | ||
b2476490 MT |
93 | /*** debugfs support ***/ |
94 | ||
95 | #ifdef CONFIG_COMMON_CLK_DEBUG | |
96 | #include <linux/debugfs.h> | |
97 | ||
98 | static struct dentry *rootdir; | |
99 | static struct dentry *orphandir; | |
100 | static int inited = 0; | |
101 | ||
1af599df PG |
102 | static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level) |
103 | { | |
104 | if (!c) | |
105 | return; | |
106 | ||
107 | seq_printf(s, "%*s%-*s %-11d %-12d %-10lu", | |
108 | level * 3 + 1, "", | |
109 | 30 - level * 3, c->name, | |
110 | c->enable_count, c->prepare_count, c->rate); | |
111 | seq_printf(s, "\n"); | |
112 | } | |
113 | ||
114 | static void clk_summary_show_subtree(struct seq_file *s, struct clk *c, | |
115 | int level) | |
116 | { | |
117 | struct clk *child; | |
1af599df PG |
118 | |
119 | if (!c) | |
120 | return; | |
121 | ||
122 | clk_summary_show_one(s, c, level); | |
123 | ||
b67bfe0d | 124 | hlist_for_each_entry(child, &c->children, child_node) |
1af599df PG |
125 | clk_summary_show_subtree(s, child, level + 1); |
126 | } | |
127 | ||
128 | static int clk_summary_show(struct seq_file *s, void *data) | |
129 | { | |
130 | struct clk *c; | |
1af599df PG |
131 | |
132 | seq_printf(s, " clock enable_cnt prepare_cnt rate\n"); | |
133 | seq_printf(s, "---------------------------------------------------------------------\n"); | |
134 | ||
eab89f69 | 135 | clk_prepare_lock(); |
1af599df | 136 | |
b67bfe0d | 137 | hlist_for_each_entry(c, &clk_root_list, child_node) |
1af599df PG |
138 | clk_summary_show_subtree(s, c, 0); |
139 | ||
b67bfe0d | 140 | hlist_for_each_entry(c, &clk_orphan_list, child_node) |
1af599df PG |
141 | clk_summary_show_subtree(s, c, 0); |
142 | ||
eab89f69 | 143 | clk_prepare_unlock(); |
1af599df PG |
144 | |
145 | return 0; | |
146 | } | |
147 | ||
148 | ||
149 | static int clk_summary_open(struct inode *inode, struct file *file) | |
150 | { | |
151 | return single_open(file, clk_summary_show, inode->i_private); | |
152 | } | |
153 | ||
154 | static const struct file_operations clk_summary_fops = { | |
155 | .open = clk_summary_open, | |
156 | .read = seq_read, | |
157 | .llseek = seq_lseek, | |
158 | .release = single_release, | |
159 | }; | |
160 | ||
bddca894 PG |
161 | static void clk_dump_one(struct seq_file *s, struct clk *c, int level) |
162 | { | |
163 | if (!c) | |
164 | return; | |
165 | ||
166 | seq_printf(s, "\"%s\": { ", c->name); | |
167 | seq_printf(s, "\"enable_count\": %d,", c->enable_count); | |
168 | seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); | |
169 | seq_printf(s, "\"rate\": %lu", c->rate); | |
170 | } | |
171 | ||
172 | static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level) | |
173 | { | |
174 | struct clk *child; | |
bddca894 PG |
175 | |
176 | if (!c) | |
177 | return; | |
178 | ||
179 | clk_dump_one(s, c, level); | |
180 | ||
b67bfe0d | 181 | hlist_for_each_entry(child, &c->children, child_node) { |
bddca894 PG |
182 | seq_printf(s, ","); |
183 | clk_dump_subtree(s, child, level + 1); | |
184 | } | |
185 | ||
186 | seq_printf(s, "}"); | |
187 | } | |
188 | ||
189 | static int clk_dump(struct seq_file *s, void *data) | |
190 | { | |
191 | struct clk *c; | |
bddca894 PG |
192 | bool first_node = true; |
193 | ||
194 | seq_printf(s, "{"); | |
195 | ||
eab89f69 | 196 | clk_prepare_lock(); |
bddca894 | 197 | |
b67bfe0d | 198 | hlist_for_each_entry(c, &clk_root_list, child_node) { |
bddca894 PG |
199 | if (!first_node) |
200 | seq_printf(s, ","); | |
201 | first_node = false; | |
202 | clk_dump_subtree(s, c, 0); | |
203 | } | |
204 | ||
b67bfe0d | 205 | hlist_for_each_entry(c, &clk_orphan_list, child_node) { |
bddca894 PG |
206 | seq_printf(s, ","); |
207 | clk_dump_subtree(s, c, 0); | |
208 | } | |
209 | ||
eab89f69 | 210 | clk_prepare_unlock(); |
bddca894 PG |
211 | |
212 | seq_printf(s, "}"); | |
213 | return 0; | |
214 | } | |
215 | ||
216 | ||
217 | static int clk_dump_open(struct inode *inode, struct file *file) | |
218 | { | |
219 | return single_open(file, clk_dump, inode->i_private); | |
220 | } | |
221 | ||
222 | static const struct file_operations clk_dump_fops = { | |
223 | .open = clk_dump_open, | |
224 | .read = seq_read, | |
225 | .llseek = seq_lseek, | |
226 | .release = single_release, | |
227 | }; | |
228 | ||
b2476490 MT |
229 | /* caller must hold prepare_lock */ |
230 | static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry) | |
231 | { | |
232 | struct dentry *d; | |
233 | int ret = -ENOMEM; | |
234 | ||
235 | if (!clk || !pdentry) { | |
236 | ret = -EINVAL; | |
237 | goto out; | |
238 | } | |
239 | ||
240 | d = debugfs_create_dir(clk->name, pdentry); | |
241 | if (!d) | |
242 | goto out; | |
243 | ||
244 | clk->dentry = d; | |
245 | ||
246 | d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry, | |
247 | (u32 *)&clk->rate); | |
248 | if (!d) | |
249 | goto err_out; | |
250 | ||
251 | d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry, | |
252 | (u32 *)&clk->flags); | |
253 | if (!d) | |
254 | goto err_out; | |
255 | ||
256 | d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry, | |
257 | (u32 *)&clk->prepare_count); | |
258 | if (!d) | |
259 | goto err_out; | |
260 | ||
261 | d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry, | |
262 | (u32 *)&clk->enable_count); | |
263 | if (!d) | |
264 | goto err_out; | |
265 | ||
266 | d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry, | |
267 | (u32 *)&clk->notifier_count); | |
268 | if (!d) | |
269 | goto err_out; | |
270 | ||
271 | ret = 0; | |
272 | goto out; | |
273 | ||
274 | err_out: | |
275 | debugfs_remove(clk->dentry); | |
276 | out: | |
277 | return ret; | |
278 | } | |
279 | ||
280 | /* caller must hold prepare_lock */ | |
281 | static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry) | |
282 | { | |
283 | struct clk *child; | |
b2476490 MT |
284 | int ret = -EINVAL;; |
285 | ||
286 | if (!clk || !pdentry) | |
287 | goto out; | |
288 | ||
289 | ret = clk_debug_create_one(clk, pdentry); | |
290 | ||
291 | if (ret) | |
292 | goto out; | |
293 | ||
b67bfe0d | 294 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
295 | clk_debug_create_subtree(child, clk->dentry); |
296 | ||
297 | ret = 0; | |
298 | out: | |
299 | return ret; | |
300 | } | |
301 | ||
302 | /** | |
303 | * clk_debug_register - add a clk node to the debugfs clk tree | |
304 | * @clk: the clk being added to the debugfs clk tree | |
305 | * | |
306 | * Dynamically adds a clk to the debugfs clk tree if debugfs has been | |
307 | * initialized. Otherwise it bails out early since the debugfs clk tree | |
308 | * will be created lazily by clk_debug_init as part of a late_initcall. | |
309 | * | |
310 | * Caller must hold prepare_lock. Only clk_init calls this function (so | |
311 | * far) so this is taken care. | |
312 | */ | |
313 | static int clk_debug_register(struct clk *clk) | |
314 | { | |
315 | struct clk *parent; | |
316 | struct dentry *pdentry; | |
317 | int ret = 0; | |
318 | ||
319 | if (!inited) | |
320 | goto out; | |
321 | ||
322 | parent = clk->parent; | |
323 | ||
324 | /* | |
325 | * Check to see if a clk is a root clk. Also check that it is | |
326 | * safe to add this clk to debugfs | |
327 | */ | |
328 | if (!parent) | |
329 | if (clk->flags & CLK_IS_ROOT) | |
330 | pdentry = rootdir; | |
331 | else | |
332 | pdentry = orphandir; | |
333 | else | |
334 | if (parent->dentry) | |
335 | pdentry = parent->dentry; | |
336 | else | |
337 | goto out; | |
338 | ||
339 | ret = clk_debug_create_subtree(clk, pdentry); | |
340 | ||
341 | out: | |
342 | return ret; | |
343 | } | |
344 | ||
345 | /** | |
346 | * clk_debug_init - lazily create the debugfs clk tree visualization | |
347 | * | |
348 | * clks are often initialized very early during boot before memory can | |
349 | * be dynamically allocated and well before debugfs is setup. | |
350 | * clk_debug_init walks the clk tree hierarchy while holding | |
351 | * prepare_lock and creates the topology as part of a late_initcall, | |
352 | * thus insuring that clks initialized very early will still be | |
353 | * represented in the debugfs clk tree. This function should only be | |
354 | * called once at boot-time, and all other clks added dynamically will | |
355 | * be done so with clk_debug_register. | |
356 | */ | |
357 | static int __init clk_debug_init(void) | |
358 | { | |
359 | struct clk *clk; | |
1af599df | 360 | struct dentry *d; |
b2476490 MT |
361 | |
362 | rootdir = debugfs_create_dir("clk", NULL); | |
363 | ||
364 | if (!rootdir) | |
365 | return -ENOMEM; | |
366 | ||
1af599df PG |
367 | d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL, |
368 | &clk_summary_fops); | |
369 | if (!d) | |
370 | return -ENOMEM; | |
371 | ||
bddca894 PG |
372 | d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL, |
373 | &clk_dump_fops); | |
374 | if (!d) | |
375 | return -ENOMEM; | |
376 | ||
b2476490 MT |
377 | orphandir = debugfs_create_dir("orphans", rootdir); |
378 | ||
379 | if (!orphandir) | |
380 | return -ENOMEM; | |
381 | ||
eab89f69 | 382 | clk_prepare_lock(); |
b2476490 | 383 | |
b67bfe0d | 384 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
b2476490 MT |
385 | clk_debug_create_subtree(clk, rootdir); |
386 | ||
b67bfe0d | 387 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
b2476490 MT |
388 | clk_debug_create_subtree(clk, orphandir); |
389 | ||
390 | inited = 1; | |
391 | ||
eab89f69 | 392 | clk_prepare_unlock(); |
b2476490 MT |
393 | |
394 | return 0; | |
395 | } | |
396 | late_initcall(clk_debug_init); | |
397 | #else | |
398 | static inline int clk_debug_register(struct clk *clk) { return 0; } | |
70d347e6 | 399 | #endif |
b2476490 | 400 | |
1c155b3d UH |
401 | /* caller must hold prepare_lock */ |
402 | static void clk_unprepare_unused_subtree(struct clk *clk) | |
403 | { | |
404 | struct clk *child; | |
405 | ||
406 | if (!clk) | |
407 | return; | |
408 | ||
409 | hlist_for_each_entry(child, &clk->children, child_node) | |
410 | clk_unprepare_unused_subtree(child); | |
411 | ||
412 | if (clk->prepare_count) | |
413 | return; | |
414 | ||
415 | if (clk->flags & CLK_IGNORE_UNUSED) | |
416 | return; | |
417 | ||
3cc8247f UH |
418 | if (__clk_is_prepared(clk)) { |
419 | if (clk->ops->unprepare_unused) | |
420 | clk->ops->unprepare_unused(clk->hw); | |
421 | else if (clk->ops->unprepare) | |
1c155b3d | 422 | clk->ops->unprepare(clk->hw); |
3cc8247f | 423 | } |
1c155b3d UH |
424 | } |
425 | ||
b2476490 MT |
426 | /* caller must hold prepare_lock */ |
427 | static void clk_disable_unused_subtree(struct clk *clk) | |
428 | { | |
429 | struct clk *child; | |
b2476490 MT |
430 | unsigned long flags; |
431 | ||
432 | if (!clk) | |
433 | goto out; | |
434 | ||
b67bfe0d | 435 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
436 | clk_disable_unused_subtree(child); |
437 | ||
eab89f69 | 438 | flags = clk_enable_lock(); |
b2476490 MT |
439 | |
440 | if (clk->enable_count) | |
441 | goto unlock_out; | |
442 | ||
443 | if (clk->flags & CLK_IGNORE_UNUSED) | |
444 | goto unlock_out; | |
445 | ||
7c045a55 MT |
446 | /* |
447 | * some gate clocks have special needs during the disable-unused | |
448 | * sequence. call .disable_unused if available, otherwise fall | |
449 | * back to .disable | |
450 | */ | |
451 | if (__clk_is_enabled(clk)) { | |
452 | if (clk->ops->disable_unused) | |
453 | clk->ops->disable_unused(clk->hw); | |
454 | else if (clk->ops->disable) | |
455 | clk->ops->disable(clk->hw); | |
456 | } | |
b2476490 MT |
457 | |
458 | unlock_out: | |
eab89f69 | 459 | clk_enable_unlock(flags); |
b2476490 MT |
460 | |
461 | out: | |
462 | return; | |
463 | } | |
464 | ||
465 | static int clk_disable_unused(void) | |
466 | { | |
467 | struct clk *clk; | |
b2476490 | 468 | |
eab89f69 | 469 | clk_prepare_lock(); |
b2476490 | 470 | |
b67bfe0d | 471 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
b2476490 MT |
472 | clk_disable_unused_subtree(clk); |
473 | ||
b67bfe0d | 474 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
b2476490 MT |
475 | clk_disable_unused_subtree(clk); |
476 | ||
1c155b3d UH |
477 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
478 | clk_unprepare_unused_subtree(clk); | |
479 | ||
480 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) | |
481 | clk_unprepare_unused_subtree(clk); | |
482 | ||
eab89f69 | 483 | clk_prepare_unlock(); |
b2476490 MT |
484 | |
485 | return 0; | |
486 | } | |
487 | late_initcall(clk_disable_unused); | |
b2476490 MT |
488 | |
489 | /*** helper functions ***/ | |
490 | ||
65800b2c | 491 | const char *__clk_get_name(struct clk *clk) |
b2476490 MT |
492 | { |
493 | return !clk ? NULL : clk->name; | |
494 | } | |
4895084c | 495 | EXPORT_SYMBOL_GPL(__clk_get_name); |
b2476490 | 496 | |
65800b2c | 497 | struct clk_hw *__clk_get_hw(struct clk *clk) |
b2476490 MT |
498 | { |
499 | return !clk ? NULL : clk->hw; | |
500 | } | |
501 | ||
65800b2c | 502 | u8 __clk_get_num_parents(struct clk *clk) |
b2476490 | 503 | { |
2ac6b1f5 | 504 | return !clk ? 0 : clk->num_parents; |
b2476490 MT |
505 | } |
506 | ||
65800b2c | 507 | struct clk *__clk_get_parent(struct clk *clk) |
b2476490 MT |
508 | { |
509 | return !clk ? NULL : clk->parent; | |
510 | } | |
511 | ||
65800b2c | 512 | unsigned int __clk_get_enable_count(struct clk *clk) |
b2476490 | 513 | { |
2ac6b1f5 | 514 | return !clk ? 0 : clk->enable_count; |
b2476490 MT |
515 | } |
516 | ||
65800b2c | 517 | unsigned int __clk_get_prepare_count(struct clk *clk) |
b2476490 | 518 | { |
2ac6b1f5 | 519 | return !clk ? 0 : clk->prepare_count; |
b2476490 MT |
520 | } |
521 | ||
522 | unsigned long __clk_get_rate(struct clk *clk) | |
523 | { | |
524 | unsigned long ret; | |
525 | ||
526 | if (!clk) { | |
34e44fe8 | 527 | ret = 0; |
b2476490 MT |
528 | goto out; |
529 | } | |
530 | ||
531 | ret = clk->rate; | |
532 | ||
533 | if (clk->flags & CLK_IS_ROOT) | |
534 | goto out; | |
535 | ||
536 | if (!clk->parent) | |
34e44fe8 | 537 | ret = 0; |
b2476490 MT |
538 | |
539 | out: | |
540 | return ret; | |
541 | } | |
542 | ||
65800b2c | 543 | unsigned long __clk_get_flags(struct clk *clk) |
b2476490 | 544 | { |
2ac6b1f5 | 545 | return !clk ? 0 : clk->flags; |
b2476490 MT |
546 | } |
547 | ||
3d6ee287 UH |
548 | bool __clk_is_prepared(struct clk *clk) |
549 | { | |
550 | int ret; | |
551 | ||
552 | if (!clk) | |
553 | return false; | |
554 | ||
555 | /* | |
556 | * .is_prepared is optional for clocks that can prepare | |
557 | * fall back to software usage counter if it is missing | |
558 | */ | |
559 | if (!clk->ops->is_prepared) { | |
560 | ret = clk->prepare_count ? 1 : 0; | |
561 | goto out; | |
562 | } | |
563 | ||
564 | ret = clk->ops->is_prepared(clk->hw); | |
565 | out: | |
566 | return !!ret; | |
567 | } | |
568 | ||
2ac6b1f5 | 569 | bool __clk_is_enabled(struct clk *clk) |
b2476490 MT |
570 | { |
571 | int ret; | |
572 | ||
573 | if (!clk) | |
2ac6b1f5 | 574 | return false; |
b2476490 MT |
575 | |
576 | /* | |
577 | * .is_enabled is only mandatory for clocks that gate | |
578 | * fall back to software usage counter if .is_enabled is missing | |
579 | */ | |
580 | if (!clk->ops->is_enabled) { | |
581 | ret = clk->enable_count ? 1 : 0; | |
582 | goto out; | |
583 | } | |
584 | ||
585 | ret = clk->ops->is_enabled(clk->hw); | |
586 | out: | |
2ac6b1f5 | 587 | return !!ret; |
b2476490 MT |
588 | } |
589 | ||
590 | static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk) | |
591 | { | |
592 | struct clk *child; | |
593 | struct clk *ret; | |
b2476490 MT |
594 | |
595 | if (!strcmp(clk->name, name)) | |
596 | return clk; | |
597 | ||
b67bfe0d | 598 | hlist_for_each_entry(child, &clk->children, child_node) { |
b2476490 MT |
599 | ret = __clk_lookup_subtree(name, child); |
600 | if (ret) | |
601 | return ret; | |
602 | } | |
603 | ||
604 | return NULL; | |
605 | } | |
606 | ||
607 | struct clk *__clk_lookup(const char *name) | |
608 | { | |
609 | struct clk *root_clk; | |
610 | struct clk *ret; | |
b2476490 MT |
611 | |
612 | if (!name) | |
613 | return NULL; | |
614 | ||
615 | /* search the 'proper' clk tree first */ | |
b67bfe0d | 616 | hlist_for_each_entry(root_clk, &clk_root_list, child_node) { |
b2476490 MT |
617 | ret = __clk_lookup_subtree(name, root_clk); |
618 | if (ret) | |
619 | return ret; | |
620 | } | |
621 | ||
622 | /* if not found, then search the orphan tree */ | |
b67bfe0d | 623 | hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { |
b2476490 MT |
624 | ret = __clk_lookup_subtree(name, root_clk); |
625 | if (ret) | |
626 | return ret; | |
627 | } | |
628 | ||
629 | return NULL; | |
630 | } | |
631 | ||
632 | /*** clk api ***/ | |
633 | ||
634 | void __clk_unprepare(struct clk *clk) | |
635 | { | |
636 | if (!clk) | |
637 | return; | |
638 | ||
639 | if (WARN_ON(clk->prepare_count == 0)) | |
640 | return; | |
641 | ||
642 | if (--clk->prepare_count > 0) | |
643 | return; | |
644 | ||
645 | WARN_ON(clk->enable_count > 0); | |
646 | ||
647 | if (clk->ops->unprepare) | |
648 | clk->ops->unprepare(clk->hw); | |
649 | ||
650 | __clk_unprepare(clk->parent); | |
651 | } | |
652 | ||
653 | /** | |
654 | * clk_unprepare - undo preparation of a clock source | |
655 | * @clk: the clk being unprepare | |
656 | * | |
657 | * clk_unprepare may sleep, which differentiates it from clk_disable. In a | |
658 | * simple case, clk_unprepare can be used instead of clk_disable to gate a clk | |
659 | * if the operation may sleep. One example is a clk which is accessed over | |
660 | * I2c. In the complex case a clk gate operation may require a fast and a slow | |
661 | * part. It is this reason that clk_unprepare and clk_disable are not mutually | |
662 | * exclusive. In fact clk_disable must be called before clk_unprepare. | |
663 | */ | |
664 | void clk_unprepare(struct clk *clk) | |
665 | { | |
eab89f69 | 666 | clk_prepare_lock(); |
b2476490 | 667 | __clk_unprepare(clk); |
eab89f69 | 668 | clk_prepare_unlock(); |
b2476490 MT |
669 | } |
670 | EXPORT_SYMBOL_GPL(clk_unprepare); | |
671 | ||
672 | int __clk_prepare(struct clk *clk) | |
673 | { | |
674 | int ret = 0; | |
675 | ||
676 | if (!clk) | |
677 | return 0; | |
678 | ||
679 | if (clk->prepare_count == 0) { | |
680 | ret = __clk_prepare(clk->parent); | |
681 | if (ret) | |
682 | return ret; | |
683 | ||
684 | if (clk->ops->prepare) { | |
685 | ret = clk->ops->prepare(clk->hw); | |
686 | if (ret) { | |
687 | __clk_unprepare(clk->parent); | |
688 | return ret; | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
693 | clk->prepare_count++; | |
694 | ||
695 | return 0; | |
696 | } | |
697 | ||
698 | /** | |
699 | * clk_prepare - prepare a clock source | |
700 | * @clk: the clk being prepared | |
701 | * | |
702 | * clk_prepare may sleep, which differentiates it from clk_enable. In a simple | |
703 | * case, clk_prepare can be used instead of clk_enable to ungate a clk if the | |
704 | * operation may sleep. One example is a clk which is accessed over I2c. In | |
705 | * the complex case a clk ungate operation may require a fast and a slow part. | |
706 | * It is this reason that clk_prepare and clk_enable are not mutually | |
707 | * exclusive. In fact clk_prepare must be called before clk_enable. | |
708 | * Returns 0 on success, -EERROR otherwise. | |
709 | */ | |
710 | int clk_prepare(struct clk *clk) | |
711 | { | |
712 | int ret; | |
713 | ||
eab89f69 | 714 | clk_prepare_lock(); |
b2476490 | 715 | ret = __clk_prepare(clk); |
eab89f69 | 716 | clk_prepare_unlock(); |
b2476490 MT |
717 | |
718 | return ret; | |
719 | } | |
720 | EXPORT_SYMBOL_GPL(clk_prepare); | |
721 | ||
722 | static void __clk_disable(struct clk *clk) | |
723 | { | |
724 | if (!clk) | |
725 | return; | |
726 | ||
e47c6a34 FW |
727 | if (WARN_ON(IS_ERR(clk))) |
728 | return; | |
729 | ||
b2476490 MT |
730 | if (WARN_ON(clk->enable_count == 0)) |
731 | return; | |
732 | ||
733 | if (--clk->enable_count > 0) | |
734 | return; | |
735 | ||
736 | if (clk->ops->disable) | |
737 | clk->ops->disable(clk->hw); | |
738 | ||
739 | __clk_disable(clk->parent); | |
740 | } | |
741 | ||
742 | /** | |
743 | * clk_disable - gate a clock | |
744 | * @clk: the clk being gated | |
745 | * | |
746 | * clk_disable must not sleep, which differentiates it from clk_unprepare. In | |
747 | * a simple case, clk_disable can be used instead of clk_unprepare to gate a | |
748 | * clk if the operation is fast and will never sleep. One example is a | |
749 | * SoC-internal clk which is controlled via simple register writes. In the | |
750 | * complex case a clk gate operation may require a fast and a slow part. It is | |
751 | * this reason that clk_unprepare and clk_disable are not mutually exclusive. | |
752 | * In fact clk_disable must be called before clk_unprepare. | |
753 | */ | |
754 | void clk_disable(struct clk *clk) | |
755 | { | |
756 | unsigned long flags; | |
757 | ||
eab89f69 | 758 | flags = clk_enable_lock(); |
b2476490 | 759 | __clk_disable(clk); |
eab89f69 | 760 | clk_enable_unlock(flags); |
b2476490 MT |
761 | } |
762 | EXPORT_SYMBOL_GPL(clk_disable); | |
763 | ||
764 | static int __clk_enable(struct clk *clk) | |
765 | { | |
766 | int ret = 0; | |
767 | ||
768 | if (!clk) | |
769 | return 0; | |
770 | ||
771 | if (WARN_ON(clk->prepare_count == 0)) | |
772 | return -ESHUTDOWN; | |
773 | ||
774 | if (clk->enable_count == 0) { | |
775 | ret = __clk_enable(clk->parent); | |
776 | ||
777 | if (ret) | |
778 | return ret; | |
779 | ||
780 | if (clk->ops->enable) { | |
781 | ret = clk->ops->enable(clk->hw); | |
782 | if (ret) { | |
783 | __clk_disable(clk->parent); | |
784 | return ret; | |
785 | } | |
786 | } | |
787 | } | |
788 | ||
789 | clk->enable_count++; | |
790 | return 0; | |
791 | } | |
792 | ||
793 | /** | |
794 | * clk_enable - ungate a clock | |
795 | * @clk: the clk being ungated | |
796 | * | |
797 | * clk_enable must not sleep, which differentiates it from clk_prepare. In a | |
798 | * simple case, clk_enable can be used instead of clk_prepare to ungate a clk | |
799 | * if the operation will never sleep. One example is a SoC-internal clk which | |
800 | * is controlled via simple register writes. In the complex case a clk ungate | |
801 | * operation may require a fast and a slow part. It is this reason that | |
802 | * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare | |
803 | * must be called before clk_enable. Returns 0 on success, -EERROR | |
804 | * otherwise. | |
805 | */ | |
806 | int clk_enable(struct clk *clk) | |
807 | { | |
808 | unsigned long flags; | |
809 | int ret; | |
810 | ||
eab89f69 | 811 | flags = clk_enable_lock(); |
b2476490 | 812 | ret = __clk_enable(clk); |
eab89f69 | 813 | clk_enable_unlock(flags); |
b2476490 MT |
814 | |
815 | return ret; | |
816 | } | |
817 | EXPORT_SYMBOL_GPL(clk_enable); | |
818 | ||
b2476490 MT |
819 | /** |
820 | * __clk_round_rate - round the given rate for a clk | |
821 | * @clk: round the rate of this clock | |
822 | * | |
823 | * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate | |
824 | */ | |
825 | unsigned long __clk_round_rate(struct clk *clk, unsigned long rate) | |
826 | { | |
81536e07 | 827 | unsigned long parent_rate = 0; |
b2476490 MT |
828 | |
829 | if (!clk) | |
2ac6b1f5 | 830 | return 0; |
b2476490 | 831 | |
f4d8af2e SG |
832 | if (!clk->ops->round_rate) { |
833 | if (clk->flags & CLK_SET_RATE_PARENT) | |
834 | return __clk_round_rate(clk->parent, rate); | |
835 | else | |
836 | return clk->rate; | |
837 | } | |
b2476490 | 838 | |
81536e07 SG |
839 | if (clk->parent) |
840 | parent_rate = clk->parent->rate; | |
841 | ||
842 | return clk->ops->round_rate(clk->hw, rate, &parent_rate); | |
b2476490 MT |
843 | } |
844 | ||
845 | /** | |
846 | * clk_round_rate - round the given rate for a clk | |
847 | * @clk: the clk for which we are rounding a rate | |
848 | * @rate: the rate which is to be rounded | |
849 | * | |
850 | * Takes in a rate as input and rounds it to a rate that the clk can actually | |
851 | * use which is then returned. If clk doesn't support round_rate operation | |
852 | * then the parent rate is returned. | |
853 | */ | |
854 | long clk_round_rate(struct clk *clk, unsigned long rate) | |
855 | { | |
856 | unsigned long ret; | |
857 | ||
eab89f69 | 858 | clk_prepare_lock(); |
b2476490 | 859 | ret = __clk_round_rate(clk, rate); |
eab89f69 | 860 | clk_prepare_unlock(); |
b2476490 MT |
861 | |
862 | return ret; | |
863 | } | |
864 | EXPORT_SYMBOL_GPL(clk_round_rate); | |
865 | ||
866 | /** | |
867 | * __clk_notify - call clk notifier chain | |
868 | * @clk: struct clk * that is changing rate | |
869 | * @msg: clk notifier type (see include/linux/clk.h) | |
870 | * @old_rate: old clk rate | |
871 | * @new_rate: new clk rate | |
872 | * | |
873 | * Triggers a notifier call chain on the clk rate-change notification | |
874 | * for 'clk'. Passes a pointer to the struct clk and the previous | |
875 | * and current rates to the notifier callback. Intended to be called by | |
876 | * internal clock code only. Returns NOTIFY_DONE from the last driver | |
877 | * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if | |
878 | * a driver returns that. | |
879 | */ | |
880 | static int __clk_notify(struct clk *clk, unsigned long msg, | |
881 | unsigned long old_rate, unsigned long new_rate) | |
882 | { | |
883 | struct clk_notifier *cn; | |
884 | struct clk_notifier_data cnd; | |
885 | int ret = NOTIFY_DONE; | |
886 | ||
887 | cnd.clk = clk; | |
888 | cnd.old_rate = old_rate; | |
889 | cnd.new_rate = new_rate; | |
890 | ||
891 | list_for_each_entry(cn, &clk_notifier_list, node) { | |
892 | if (cn->clk == clk) { | |
893 | ret = srcu_notifier_call_chain(&cn->notifier_head, msg, | |
894 | &cnd); | |
895 | break; | |
896 | } | |
897 | } | |
898 | ||
899 | return ret; | |
900 | } | |
901 | ||
902 | /** | |
903 | * __clk_recalc_rates | |
904 | * @clk: first clk in the subtree | |
905 | * @msg: notification type (see include/linux/clk.h) | |
906 | * | |
907 | * Walks the subtree of clks starting with clk and recalculates rates as it | |
908 | * goes. Note that if a clk does not implement the .recalc_rate callback then | |
909 | * it is assumed that the clock will take on the rate of it's parent. | |
910 | * | |
911 | * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, | |
912 | * if necessary. | |
913 | * | |
914 | * Caller must hold prepare_lock. | |
915 | */ | |
916 | static void __clk_recalc_rates(struct clk *clk, unsigned long msg) | |
917 | { | |
918 | unsigned long old_rate; | |
919 | unsigned long parent_rate = 0; | |
b2476490 MT |
920 | struct clk *child; |
921 | ||
922 | old_rate = clk->rate; | |
923 | ||
924 | if (clk->parent) | |
925 | parent_rate = clk->parent->rate; | |
926 | ||
927 | if (clk->ops->recalc_rate) | |
928 | clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate); | |
929 | else | |
930 | clk->rate = parent_rate; | |
931 | ||
932 | /* | |
933 | * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE | |
934 | * & ABORT_RATE_CHANGE notifiers | |
935 | */ | |
936 | if (clk->notifier_count && msg) | |
937 | __clk_notify(clk, msg, old_rate, clk->rate); | |
938 | ||
b67bfe0d | 939 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
940 | __clk_recalc_rates(child, msg); |
941 | } | |
942 | ||
a093bde2 UH |
943 | /** |
944 | * clk_get_rate - return the rate of clk | |
945 | * @clk: the clk whose rate is being returned | |
946 | * | |
947 | * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag | |
948 | * is set, which means a recalc_rate will be issued. | |
949 | * If clk is NULL then returns 0. | |
950 | */ | |
951 | unsigned long clk_get_rate(struct clk *clk) | |
952 | { | |
953 | unsigned long rate; | |
954 | ||
eab89f69 | 955 | clk_prepare_lock(); |
a093bde2 UH |
956 | |
957 | if (clk && (clk->flags & CLK_GET_RATE_NOCACHE)) | |
958 | __clk_recalc_rates(clk, 0); | |
959 | ||
960 | rate = __clk_get_rate(clk); | |
eab89f69 | 961 | clk_prepare_unlock(); |
a093bde2 UH |
962 | |
963 | return rate; | |
964 | } | |
965 | EXPORT_SYMBOL_GPL(clk_get_rate); | |
966 | ||
b2476490 MT |
967 | /** |
968 | * __clk_speculate_rates | |
969 | * @clk: first clk in the subtree | |
970 | * @parent_rate: the "future" rate of clk's parent | |
971 | * | |
972 | * Walks the subtree of clks starting with clk, speculating rates as it | |
973 | * goes and firing off PRE_RATE_CHANGE notifications as necessary. | |
974 | * | |
975 | * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending | |
976 | * pre-rate change notifications and returns early if no clks in the | |
977 | * subtree have subscribed to the notifications. Note that if a clk does not | |
978 | * implement the .recalc_rate callback then it is assumed that the clock will | |
979 | * take on the rate of it's parent. | |
980 | * | |
981 | * Caller must hold prepare_lock. | |
982 | */ | |
983 | static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate) | |
984 | { | |
b2476490 MT |
985 | struct clk *child; |
986 | unsigned long new_rate; | |
987 | int ret = NOTIFY_DONE; | |
988 | ||
989 | if (clk->ops->recalc_rate) | |
990 | new_rate = clk->ops->recalc_rate(clk->hw, parent_rate); | |
991 | else | |
992 | new_rate = parent_rate; | |
993 | ||
994 | /* abort the rate change if a driver returns NOTIFY_BAD */ | |
995 | if (clk->notifier_count) | |
996 | ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate); | |
997 | ||
998 | if (ret == NOTIFY_BAD) | |
999 | goto out; | |
1000 | ||
b67bfe0d | 1001 | hlist_for_each_entry(child, &clk->children, child_node) { |
b2476490 MT |
1002 | ret = __clk_speculate_rates(child, new_rate); |
1003 | if (ret == NOTIFY_BAD) | |
1004 | break; | |
1005 | } | |
1006 | ||
1007 | out: | |
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | static void clk_calc_subtree(struct clk *clk, unsigned long new_rate) | |
1012 | { | |
1013 | struct clk *child; | |
b2476490 MT |
1014 | |
1015 | clk->new_rate = new_rate; | |
1016 | ||
b67bfe0d | 1017 | hlist_for_each_entry(child, &clk->children, child_node) { |
b2476490 MT |
1018 | if (child->ops->recalc_rate) |
1019 | child->new_rate = child->ops->recalc_rate(child->hw, new_rate); | |
1020 | else | |
1021 | child->new_rate = new_rate; | |
1022 | clk_calc_subtree(child, child->new_rate); | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * calculate the new rates returning the topmost clock that has to be | |
1028 | * changed. | |
1029 | */ | |
1030 | static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate) | |
1031 | { | |
1032 | struct clk *top = clk; | |
81536e07 | 1033 | unsigned long best_parent_rate = 0; |
b2476490 MT |
1034 | unsigned long new_rate; |
1035 | ||
7452b219 MT |
1036 | /* sanity */ |
1037 | if (IS_ERR_OR_NULL(clk)) | |
1038 | return NULL; | |
1039 | ||
63f5c3b2 MT |
1040 | /* save parent rate, if it exists */ |
1041 | if (clk->parent) | |
1042 | best_parent_rate = clk->parent->rate; | |
1043 | ||
7452b219 MT |
1044 | /* never propagate up to the parent */ |
1045 | if (!(clk->flags & CLK_SET_RATE_PARENT)) { | |
1046 | if (!clk->ops->round_rate) { | |
1047 | clk->new_rate = clk->rate; | |
1048 | return NULL; | |
7452b219 | 1049 | } |
63f5c3b2 MT |
1050 | new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate); |
1051 | goto out; | |
7452b219 MT |
1052 | } |
1053 | ||
1054 | /* need clk->parent from here on out */ | |
1055 | if (!clk->parent) { | |
1056 | pr_debug("%s: %s has NULL parent\n", __func__, clk->name); | |
b2476490 MT |
1057 | return NULL; |
1058 | } | |
1059 | ||
7452b219 | 1060 | if (!clk->ops->round_rate) { |
b2476490 | 1061 | top = clk_calc_new_rates(clk->parent, rate); |
1b2f9903 | 1062 | new_rate = clk->parent->new_rate; |
b2476490 MT |
1063 | |
1064 | goto out; | |
1065 | } | |
1066 | ||
7452b219 | 1067 | new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate); |
b2476490 MT |
1068 | |
1069 | if (best_parent_rate != clk->parent->rate) { | |
1070 | top = clk_calc_new_rates(clk->parent, best_parent_rate); | |
1071 | ||
1072 | goto out; | |
1073 | } | |
1074 | ||
1075 | out: | |
1076 | clk_calc_subtree(clk, new_rate); | |
1077 | ||
1078 | return top; | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * Notify about rate changes in a subtree. Always walk down the whole tree | |
1083 | * so that in case of an error we can walk down the whole tree again and | |
1084 | * abort the change. | |
1085 | */ | |
1086 | static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event) | |
1087 | { | |
b2476490 MT |
1088 | struct clk *child, *fail_clk = NULL; |
1089 | int ret = NOTIFY_DONE; | |
1090 | ||
1091 | if (clk->rate == clk->new_rate) | |
5fda6858 | 1092 | return NULL; |
b2476490 MT |
1093 | |
1094 | if (clk->notifier_count) { | |
1095 | ret = __clk_notify(clk, event, clk->rate, clk->new_rate); | |
1096 | if (ret == NOTIFY_BAD) | |
1097 | fail_clk = clk; | |
1098 | } | |
1099 | ||
b67bfe0d | 1100 | hlist_for_each_entry(child, &clk->children, child_node) { |
b2476490 MT |
1101 | clk = clk_propagate_rate_change(child, event); |
1102 | if (clk) | |
1103 | fail_clk = clk; | |
1104 | } | |
1105 | ||
1106 | return fail_clk; | |
1107 | } | |
1108 | ||
1109 | /* | |
1110 | * walk down a subtree and set the new rates notifying the rate | |
1111 | * change on the way | |
1112 | */ | |
1113 | static void clk_change_rate(struct clk *clk) | |
1114 | { | |
1115 | struct clk *child; | |
1116 | unsigned long old_rate; | |
bf47b4fd | 1117 | unsigned long best_parent_rate = 0; |
b2476490 MT |
1118 | |
1119 | old_rate = clk->rate; | |
1120 | ||
bf47b4fd PM |
1121 | if (clk->parent) |
1122 | best_parent_rate = clk->parent->rate; | |
1123 | ||
b2476490 | 1124 | if (clk->ops->set_rate) |
bf47b4fd | 1125 | clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate); |
b2476490 MT |
1126 | |
1127 | if (clk->ops->recalc_rate) | |
bf47b4fd | 1128 | clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate); |
b2476490 | 1129 | else |
bf47b4fd | 1130 | clk->rate = best_parent_rate; |
b2476490 MT |
1131 | |
1132 | if (clk->notifier_count && old_rate != clk->rate) | |
1133 | __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate); | |
1134 | ||
b67bfe0d | 1135 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
1136 | clk_change_rate(child); |
1137 | } | |
1138 | ||
1139 | /** | |
1140 | * clk_set_rate - specify a new rate for clk | |
1141 | * @clk: the clk whose rate is being changed | |
1142 | * @rate: the new rate for clk | |
1143 | * | |
5654dc94 | 1144 | * In the simplest case clk_set_rate will only adjust the rate of clk. |
b2476490 | 1145 | * |
5654dc94 MT |
1146 | * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to |
1147 | * propagate up to clk's parent; whether or not this happens depends on the | |
1148 | * outcome of clk's .round_rate implementation. If *parent_rate is unchanged | |
1149 | * after calling .round_rate then upstream parent propagation is ignored. If | |
1150 | * *parent_rate comes back with a new rate for clk's parent then we propagate | |
1151 | * up to clk's parent and set it's rate. Upward propagation will continue | |
1152 | * until either a clk does not support the CLK_SET_RATE_PARENT flag or | |
1153 | * .round_rate stops requesting changes to clk's parent_rate. | |
b2476490 | 1154 | * |
5654dc94 MT |
1155 | * Rate changes are accomplished via tree traversal that also recalculates the |
1156 | * rates for the clocks and fires off POST_RATE_CHANGE notifiers. | |
b2476490 MT |
1157 | * |
1158 | * Returns 0 on success, -EERROR otherwise. | |
1159 | */ | |
1160 | int clk_set_rate(struct clk *clk, unsigned long rate) | |
1161 | { | |
1162 | struct clk *top, *fail_clk; | |
1163 | int ret = 0; | |
1164 | ||
1165 | /* prevent racing with updates to the clock topology */ | |
eab89f69 | 1166 | clk_prepare_lock(); |
b2476490 MT |
1167 | |
1168 | /* bail early if nothing to do */ | |
1169 | if (rate == clk->rate) | |
1170 | goto out; | |
1171 | ||
7e0fa1b5 | 1172 | if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) { |
0e1c0301 VK |
1173 | ret = -EBUSY; |
1174 | goto out; | |
1175 | } | |
1176 | ||
b2476490 MT |
1177 | /* calculate new rates and get the topmost changed clock */ |
1178 | top = clk_calc_new_rates(clk, rate); | |
1179 | if (!top) { | |
1180 | ret = -EINVAL; | |
1181 | goto out; | |
1182 | } | |
1183 | ||
1184 | /* notify that we are about to change rates */ | |
1185 | fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); | |
1186 | if (fail_clk) { | |
1187 | pr_warn("%s: failed to set %s rate\n", __func__, | |
1188 | fail_clk->name); | |
1189 | clk_propagate_rate_change(top, ABORT_RATE_CHANGE); | |
1190 | ret = -EBUSY; | |
1191 | goto out; | |
1192 | } | |
1193 | ||
1194 | /* change the rates */ | |
1195 | clk_change_rate(top); | |
1196 | ||
b2476490 | 1197 | out: |
eab89f69 | 1198 | clk_prepare_unlock(); |
b2476490 MT |
1199 | |
1200 | return ret; | |
1201 | } | |
1202 | EXPORT_SYMBOL_GPL(clk_set_rate); | |
1203 | ||
1204 | /** | |
1205 | * clk_get_parent - return the parent of a clk | |
1206 | * @clk: the clk whose parent gets returned | |
1207 | * | |
1208 | * Simply returns clk->parent. Returns NULL if clk is NULL. | |
1209 | */ | |
1210 | struct clk *clk_get_parent(struct clk *clk) | |
1211 | { | |
1212 | struct clk *parent; | |
1213 | ||
eab89f69 | 1214 | clk_prepare_lock(); |
b2476490 | 1215 | parent = __clk_get_parent(clk); |
eab89f69 | 1216 | clk_prepare_unlock(); |
b2476490 MT |
1217 | |
1218 | return parent; | |
1219 | } | |
1220 | EXPORT_SYMBOL_GPL(clk_get_parent); | |
1221 | ||
1222 | /* | |
1223 | * .get_parent is mandatory for clocks with multiple possible parents. It is | |
1224 | * optional for single-parent clocks. Always call .get_parent if it is | |
1225 | * available and WARN if it is missing for multi-parent clocks. | |
1226 | * | |
1227 | * For single-parent clocks without .get_parent, first check to see if the | |
1228 | * .parents array exists, and if so use it to avoid an expensive tree | |
1229 | * traversal. If .parents does not exist then walk the tree with __clk_lookup. | |
1230 | */ | |
1231 | static struct clk *__clk_init_parent(struct clk *clk) | |
1232 | { | |
1233 | struct clk *ret = NULL; | |
1234 | u8 index; | |
1235 | ||
1236 | /* handle the trivial cases */ | |
1237 | ||
1238 | if (!clk->num_parents) | |
1239 | goto out; | |
1240 | ||
1241 | if (clk->num_parents == 1) { | |
1242 | if (IS_ERR_OR_NULL(clk->parent)) | |
1243 | ret = clk->parent = __clk_lookup(clk->parent_names[0]); | |
1244 | ret = clk->parent; | |
1245 | goto out; | |
1246 | } | |
1247 | ||
1248 | if (!clk->ops->get_parent) { | |
1249 | WARN(!clk->ops->get_parent, | |
1250 | "%s: multi-parent clocks must implement .get_parent\n", | |
1251 | __func__); | |
1252 | goto out; | |
1253 | }; | |
1254 | ||
1255 | /* | |
1256 | * Do our best to cache parent clocks in clk->parents. This prevents | |
1257 | * unnecessary and expensive calls to __clk_lookup. We don't set | |
1258 | * clk->parent here; that is done by the calling function | |
1259 | */ | |
1260 | ||
1261 | index = clk->ops->get_parent(clk->hw); | |
1262 | ||
1263 | if (!clk->parents) | |
1264 | clk->parents = | |
7975059d | 1265 | kzalloc((sizeof(struct clk*) * clk->num_parents), |
b2476490 MT |
1266 | GFP_KERNEL); |
1267 | ||
1268 | if (!clk->parents) | |
1269 | ret = __clk_lookup(clk->parent_names[index]); | |
1270 | else if (!clk->parents[index]) | |
1271 | ret = clk->parents[index] = | |
1272 | __clk_lookup(clk->parent_names[index]); | |
1273 | else | |
1274 | ret = clk->parents[index]; | |
1275 | ||
1276 | out: | |
1277 | return ret; | |
1278 | } | |
1279 | ||
1280 | void __clk_reparent(struct clk *clk, struct clk *new_parent) | |
1281 | { | |
1282 | #ifdef CONFIG_COMMON_CLK_DEBUG | |
1283 | struct dentry *d; | |
1284 | struct dentry *new_parent_d; | |
1285 | #endif | |
1286 | ||
1287 | if (!clk || !new_parent) | |
1288 | return; | |
1289 | ||
1290 | hlist_del(&clk->child_node); | |
1291 | ||
1292 | if (new_parent) | |
1293 | hlist_add_head(&clk->child_node, &new_parent->children); | |
1294 | else | |
1295 | hlist_add_head(&clk->child_node, &clk_orphan_list); | |
1296 | ||
1297 | #ifdef CONFIG_COMMON_CLK_DEBUG | |
1298 | if (!inited) | |
1299 | goto out; | |
1300 | ||
1301 | if (new_parent) | |
1302 | new_parent_d = new_parent->dentry; | |
1303 | else | |
1304 | new_parent_d = orphandir; | |
1305 | ||
1306 | d = debugfs_rename(clk->dentry->d_parent, clk->dentry, | |
1307 | new_parent_d, clk->name); | |
1308 | if (d) | |
1309 | clk->dentry = d; | |
1310 | else | |
1311 | pr_debug("%s: failed to rename debugfs entry for %s\n", | |
1312 | __func__, clk->name); | |
1313 | out: | |
1314 | #endif | |
1315 | ||
1316 | clk->parent = new_parent; | |
1317 | ||
1318 | __clk_recalc_rates(clk, POST_RATE_CHANGE); | |
1319 | } | |
1320 | ||
1321 | static int __clk_set_parent(struct clk *clk, struct clk *parent) | |
1322 | { | |
1323 | struct clk *old_parent; | |
1324 | unsigned long flags; | |
1325 | int ret = -EINVAL; | |
1326 | u8 i; | |
1327 | ||
1328 | old_parent = clk->parent; | |
1329 | ||
863b1327 | 1330 | if (!clk->parents) |
7975059d RN |
1331 | clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents), |
1332 | GFP_KERNEL); | |
b2476490 MT |
1333 | |
1334 | /* | |
863b1327 RN |
1335 | * find index of new parent clock using cached parent ptrs, |
1336 | * or if not yet cached, use string name comparison and cache | |
1337 | * them now to avoid future calls to __clk_lookup. | |
b2476490 | 1338 | */ |
863b1327 RN |
1339 | for (i = 0; i < clk->num_parents; i++) { |
1340 | if (clk->parents && clk->parents[i] == parent) | |
1341 | break; | |
1342 | else if (!strcmp(clk->parent_names[i], parent->name)) { | |
1343 | if (clk->parents) | |
1344 | clk->parents[i] = __clk_lookup(parent->name); | |
1345 | break; | |
1346 | } | |
1347 | } | |
b2476490 MT |
1348 | |
1349 | if (i == clk->num_parents) { | |
1350 | pr_debug("%s: clock %s is not a possible parent of clock %s\n", | |
1351 | __func__, parent->name, clk->name); | |
1352 | goto out; | |
1353 | } | |
1354 | ||
1355 | /* migrate prepare and enable */ | |
1356 | if (clk->prepare_count) | |
1357 | __clk_prepare(parent); | |
1358 | ||
1359 | /* FIXME replace with clk_is_enabled(clk) someday */ | |
eab89f69 | 1360 | flags = clk_enable_lock(); |
b2476490 MT |
1361 | if (clk->enable_count) |
1362 | __clk_enable(parent); | |
eab89f69 | 1363 | clk_enable_unlock(flags); |
b2476490 MT |
1364 | |
1365 | /* change clock input source */ | |
1366 | ret = clk->ops->set_parent(clk->hw, i); | |
1367 | ||
1368 | /* clean up old prepare and enable */ | |
eab89f69 | 1369 | flags = clk_enable_lock(); |
b2476490 MT |
1370 | if (clk->enable_count) |
1371 | __clk_disable(old_parent); | |
eab89f69 | 1372 | clk_enable_unlock(flags); |
b2476490 MT |
1373 | |
1374 | if (clk->prepare_count) | |
1375 | __clk_unprepare(old_parent); | |
1376 | ||
1377 | out: | |
1378 | return ret; | |
1379 | } | |
1380 | ||
1381 | /** | |
1382 | * clk_set_parent - switch the parent of a mux clk | |
1383 | * @clk: the mux clk whose input we are switching | |
1384 | * @parent: the new input to clk | |
1385 | * | |
1386 | * Re-parent clk to use parent as it's new input source. If clk has the | |
1387 | * CLK_SET_PARENT_GATE flag set then clk must be gated for this | |
1388 | * operation to succeed. After successfully changing clk's parent | |
1389 | * clk_set_parent will update the clk topology, sysfs topology and | |
1390 | * propagate rate recalculation via __clk_recalc_rates. Returns 0 on | |
1391 | * success, -EERROR otherwise. | |
1392 | */ | |
1393 | int clk_set_parent(struct clk *clk, struct clk *parent) | |
1394 | { | |
1395 | int ret = 0; | |
1396 | ||
1397 | if (!clk || !clk->ops) | |
1398 | return -EINVAL; | |
1399 | ||
1400 | if (!clk->ops->set_parent) | |
1401 | return -ENOSYS; | |
1402 | ||
1403 | /* prevent racing with updates to the clock topology */ | |
eab89f69 | 1404 | clk_prepare_lock(); |
b2476490 MT |
1405 | |
1406 | if (clk->parent == parent) | |
1407 | goto out; | |
1408 | ||
1409 | /* propagate PRE_RATE_CHANGE notifications */ | |
1410 | if (clk->notifier_count) | |
1411 | ret = __clk_speculate_rates(clk, parent->rate); | |
1412 | ||
1413 | /* abort if a driver objects */ | |
1414 | if (ret == NOTIFY_STOP) | |
1415 | goto out; | |
1416 | ||
1417 | /* only re-parent if the clock is not in use */ | |
1418 | if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) | |
1419 | ret = -EBUSY; | |
1420 | else | |
1421 | ret = __clk_set_parent(clk, parent); | |
1422 | ||
1423 | /* propagate ABORT_RATE_CHANGE if .set_parent failed */ | |
1424 | if (ret) { | |
1425 | __clk_recalc_rates(clk, ABORT_RATE_CHANGE); | |
1426 | goto out; | |
1427 | } | |
1428 | ||
1429 | /* propagate rate recalculation downstream */ | |
1430 | __clk_reparent(clk, parent); | |
1431 | ||
1432 | out: | |
eab89f69 | 1433 | clk_prepare_unlock(); |
b2476490 MT |
1434 | |
1435 | return ret; | |
1436 | } | |
1437 | EXPORT_SYMBOL_GPL(clk_set_parent); | |
1438 | ||
1439 | /** | |
1440 | * __clk_init - initialize the data structures in a struct clk | |
1441 | * @dev: device initializing this clk, placeholder for now | |
1442 | * @clk: clk being initialized | |
1443 | * | |
1444 | * Initializes the lists in struct clk, queries the hardware for the | |
1445 | * parent and rate and sets them both. | |
b2476490 | 1446 | */ |
d1302a36 | 1447 | int __clk_init(struct device *dev, struct clk *clk) |
b2476490 | 1448 | { |
d1302a36 | 1449 | int i, ret = 0; |
b2476490 | 1450 | struct clk *orphan; |
b67bfe0d | 1451 | struct hlist_node *tmp2; |
b2476490 MT |
1452 | |
1453 | if (!clk) | |
d1302a36 | 1454 | return -EINVAL; |
b2476490 | 1455 | |
eab89f69 | 1456 | clk_prepare_lock(); |
b2476490 MT |
1457 | |
1458 | /* check to see if a clock with this name is already registered */ | |
d1302a36 MT |
1459 | if (__clk_lookup(clk->name)) { |
1460 | pr_debug("%s: clk %s already initialized\n", | |
1461 | __func__, clk->name); | |
1462 | ret = -EEXIST; | |
b2476490 | 1463 | goto out; |
d1302a36 | 1464 | } |
b2476490 | 1465 | |
d4d7e3dd MT |
1466 | /* check that clk_ops are sane. See Documentation/clk.txt */ |
1467 | if (clk->ops->set_rate && | |
1468 | !(clk->ops->round_rate && clk->ops->recalc_rate)) { | |
1469 | pr_warning("%s: %s must implement .round_rate & .recalc_rate\n", | |
1470 | __func__, clk->name); | |
d1302a36 | 1471 | ret = -EINVAL; |
d4d7e3dd MT |
1472 | goto out; |
1473 | } | |
1474 | ||
1475 | if (clk->ops->set_parent && !clk->ops->get_parent) { | |
1476 | pr_warning("%s: %s must implement .get_parent & .set_parent\n", | |
1477 | __func__, clk->name); | |
d1302a36 | 1478 | ret = -EINVAL; |
d4d7e3dd MT |
1479 | goto out; |
1480 | } | |
1481 | ||
b2476490 MT |
1482 | /* throw a WARN if any entries in parent_names are NULL */ |
1483 | for (i = 0; i < clk->num_parents; i++) | |
1484 | WARN(!clk->parent_names[i], | |
1485 | "%s: invalid NULL in %s's .parent_names\n", | |
1486 | __func__, clk->name); | |
1487 | ||
1488 | /* | |
1489 | * Allocate an array of struct clk *'s to avoid unnecessary string | |
1490 | * look-ups of clk's possible parents. This can fail for clocks passed | |
1491 | * in to clk_init during early boot; thus any access to clk->parents[] | |
1492 | * must always check for a NULL pointer and try to populate it if | |
1493 | * necessary. | |
1494 | * | |
1495 | * If clk->parents is not NULL we skip this entire block. This allows | |
1496 | * for clock drivers to statically initialize clk->parents. | |
1497 | */ | |
9ca1c5a4 RN |
1498 | if (clk->num_parents > 1 && !clk->parents) { |
1499 | clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents), | |
b2476490 MT |
1500 | GFP_KERNEL); |
1501 | /* | |
1502 | * __clk_lookup returns NULL for parents that have not been | |
1503 | * clk_init'd; thus any access to clk->parents[] must check | |
1504 | * for a NULL pointer. We can always perform lazy lookups for | |
1505 | * missing parents later on. | |
1506 | */ | |
1507 | if (clk->parents) | |
1508 | for (i = 0; i < clk->num_parents; i++) | |
1509 | clk->parents[i] = | |
1510 | __clk_lookup(clk->parent_names[i]); | |
1511 | } | |
1512 | ||
1513 | clk->parent = __clk_init_parent(clk); | |
1514 | ||
1515 | /* | |
1516 | * Populate clk->parent if parent has already been __clk_init'd. If | |
1517 | * parent has not yet been __clk_init'd then place clk in the orphan | |
1518 | * list. If clk has set the CLK_IS_ROOT flag then place it in the root | |
1519 | * clk list. | |
1520 | * | |
1521 | * Every time a new clk is clk_init'd then we walk the list of orphan | |
1522 | * clocks and re-parent any that are children of the clock currently | |
1523 | * being clk_init'd. | |
1524 | */ | |
1525 | if (clk->parent) | |
1526 | hlist_add_head(&clk->child_node, | |
1527 | &clk->parent->children); | |
1528 | else if (clk->flags & CLK_IS_ROOT) | |
1529 | hlist_add_head(&clk->child_node, &clk_root_list); | |
1530 | else | |
1531 | hlist_add_head(&clk->child_node, &clk_orphan_list); | |
1532 | ||
1533 | /* | |
1534 | * Set clk's rate. The preferred method is to use .recalc_rate. For | |
1535 | * simple clocks and lazy developers the default fallback is to use the | |
1536 | * parent's rate. If a clock doesn't have a parent (or is orphaned) | |
1537 | * then rate is set to zero. | |
1538 | */ | |
1539 | if (clk->ops->recalc_rate) | |
1540 | clk->rate = clk->ops->recalc_rate(clk->hw, | |
1541 | __clk_get_rate(clk->parent)); | |
1542 | else if (clk->parent) | |
1543 | clk->rate = clk->parent->rate; | |
1544 | else | |
1545 | clk->rate = 0; | |
1546 | ||
1547 | /* | |
1548 | * walk the list of orphan clocks and reparent any that are children of | |
1549 | * this clock | |
1550 | */ | |
b67bfe0d | 1551 | hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { |
1f61e5f1 MF |
1552 | if (orphan->ops->get_parent) { |
1553 | i = orphan->ops->get_parent(orphan->hw); | |
1554 | if (!strcmp(clk->name, orphan->parent_names[i])) | |
1555 | __clk_reparent(orphan, clk); | |
1556 | continue; | |
1557 | } | |
1558 | ||
b2476490 MT |
1559 | for (i = 0; i < orphan->num_parents; i++) |
1560 | if (!strcmp(clk->name, orphan->parent_names[i])) { | |
1561 | __clk_reparent(orphan, clk); | |
1562 | break; | |
1563 | } | |
1f61e5f1 | 1564 | } |
b2476490 MT |
1565 | |
1566 | /* | |
1567 | * optional platform-specific magic | |
1568 | * | |
1569 | * The .init callback is not used by any of the basic clock types, but | |
1570 | * exists for weird hardware that must perform initialization magic. | |
1571 | * Please consider other ways of solving initialization problems before | |
1572 | * using this callback, as it's use is discouraged. | |
1573 | */ | |
1574 | if (clk->ops->init) | |
1575 | clk->ops->init(clk->hw); | |
1576 | ||
1577 | clk_debug_register(clk); | |
1578 | ||
1579 | out: | |
eab89f69 | 1580 | clk_prepare_unlock(); |
b2476490 | 1581 | |
d1302a36 | 1582 | return ret; |
b2476490 MT |
1583 | } |
1584 | ||
0197b3ea SK |
1585 | /** |
1586 | * __clk_register - register a clock and return a cookie. | |
1587 | * | |
1588 | * Same as clk_register, except that the .clk field inside hw shall point to a | |
1589 | * preallocated (generally statically allocated) struct clk. None of the fields | |
1590 | * of the struct clk need to be initialized. | |
1591 | * | |
1592 | * The data pointed to by .init and .clk field shall NOT be marked as init | |
1593 | * data. | |
1594 | * | |
1595 | * __clk_register is only exposed via clk-private.h and is intended for use with | |
1596 | * very large numbers of clocks that need to be statically initialized. It is | |
1597 | * a layering violation to include clk-private.h from any code which implements | |
1598 | * a clock's .ops; as such any statically initialized clock data MUST be in a | |
1599 | * separate C file from the logic that implements it's operations. Returns 0 | |
1600 | * on success, otherwise an error code. | |
1601 | */ | |
1602 | struct clk *__clk_register(struct device *dev, struct clk_hw *hw) | |
1603 | { | |
1604 | int ret; | |
1605 | struct clk *clk; | |
1606 | ||
1607 | clk = hw->clk; | |
1608 | clk->name = hw->init->name; | |
1609 | clk->ops = hw->init->ops; | |
1610 | clk->hw = hw; | |
1611 | clk->flags = hw->init->flags; | |
1612 | clk->parent_names = hw->init->parent_names; | |
1613 | clk->num_parents = hw->init->num_parents; | |
1614 | ||
1615 | ret = __clk_init(dev, clk); | |
1616 | if (ret) | |
1617 | return ERR_PTR(ret); | |
1618 | ||
1619 | return clk; | |
1620 | } | |
1621 | EXPORT_SYMBOL_GPL(__clk_register); | |
1622 | ||
46c8773a | 1623 | static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk) |
b2476490 | 1624 | { |
d1302a36 | 1625 | int i, ret; |
b2476490 | 1626 | |
0197b3ea SK |
1627 | clk->name = kstrdup(hw->init->name, GFP_KERNEL); |
1628 | if (!clk->name) { | |
1629 | pr_err("%s: could not allocate clk->name\n", __func__); | |
1630 | ret = -ENOMEM; | |
1631 | goto fail_name; | |
1632 | } | |
1633 | clk->ops = hw->init->ops; | |
b2476490 | 1634 | clk->hw = hw; |
0197b3ea SK |
1635 | clk->flags = hw->init->flags; |
1636 | clk->num_parents = hw->init->num_parents; | |
b2476490 MT |
1637 | hw->clk = clk; |
1638 | ||
d1302a36 | 1639 | /* allocate local copy in case parent_names is __initdata */ |
0197b3ea | 1640 | clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents), |
d1302a36 MT |
1641 | GFP_KERNEL); |
1642 | ||
1643 | if (!clk->parent_names) { | |
1644 | pr_err("%s: could not allocate clk->parent_names\n", __func__); | |
1645 | ret = -ENOMEM; | |
1646 | goto fail_parent_names; | |
1647 | } | |
1648 | ||
1649 | ||
1650 | /* copy each string name in case parent_names is __initdata */ | |
0197b3ea SK |
1651 | for (i = 0; i < clk->num_parents; i++) { |
1652 | clk->parent_names[i] = kstrdup(hw->init->parent_names[i], | |
1653 | GFP_KERNEL); | |
d1302a36 MT |
1654 | if (!clk->parent_names[i]) { |
1655 | pr_err("%s: could not copy parent_names\n", __func__); | |
1656 | ret = -ENOMEM; | |
1657 | goto fail_parent_names_copy; | |
1658 | } | |
1659 | } | |
1660 | ||
1661 | ret = __clk_init(dev, clk); | |
1662 | if (!ret) | |
46c8773a | 1663 | return 0; |
b2476490 | 1664 | |
d1302a36 MT |
1665 | fail_parent_names_copy: |
1666 | while (--i >= 0) | |
1667 | kfree(clk->parent_names[i]); | |
1668 | kfree(clk->parent_names); | |
1669 | fail_parent_names: | |
0197b3ea SK |
1670 | kfree(clk->name); |
1671 | fail_name: | |
46c8773a SB |
1672 | return ret; |
1673 | } | |
1674 | ||
1675 | /** | |
1676 | * clk_register - allocate a new clock, register it and return an opaque cookie | |
1677 | * @dev: device that is registering this clock | |
1678 | * @hw: link to hardware-specific clock data | |
1679 | * | |
1680 | * clk_register is the primary interface for populating the clock tree with new | |
1681 | * clock nodes. It returns a pointer to the newly allocated struct clk which | |
1682 | * cannot be dereferenced by driver code but may be used in conjuction with the | |
1683 | * rest of the clock API. In the event of an error clk_register will return an | |
1684 | * error code; drivers must test for an error code after calling clk_register. | |
1685 | */ | |
1686 | struct clk *clk_register(struct device *dev, struct clk_hw *hw) | |
1687 | { | |
1688 | int ret; | |
1689 | struct clk *clk; | |
1690 | ||
1691 | clk = kzalloc(sizeof(*clk), GFP_KERNEL); | |
1692 | if (!clk) { | |
1693 | pr_err("%s: could not allocate clk\n", __func__); | |
1694 | ret = -ENOMEM; | |
1695 | goto fail_out; | |
1696 | } | |
1697 | ||
1698 | ret = _clk_register(dev, hw, clk); | |
1699 | if (!ret) | |
1700 | return clk; | |
1701 | ||
d1302a36 MT |
1702 | kfree(clk); |
1703 | fail_out: | |
1704 | return ERR_PTR(ret); | |
b2476490 MT |
1705 | } |
1706 | EXPORT_SYMBOL_GPL(clk_register); | |
1707 | ||
1df5c939 MB |
1708 | /** |
1709 | * clk_unregister - unregister a currently registered clock | |
1710 | * @clk: clock to unregister | |
1711 | * | |
1712 | * Currently unimplemented. | |
1713 | */ | |
1714 | void clk_unregister(struct clk *clk) {} | |
1715 | EXPORT_SYMBOL_GPL(clk_unregister); | |
1716 | ||
46c8773a SB |
1717 | static void devm_clk_release(struct device *dev, void *res) |
1718 | { | |
1719 | clk_unregister(res); | |
1720 | } | |
1721 | ||
1722 | /** | |
1723 | * devm_clk_register - resource managed clk_register() | |
1724 | * @dev: device that is registering this clock | |
1725 | * @hw: link to hardware-specific clock data | |
1726 | * | |
1727 | * Managed clk_register(). Clocks returned from this function are | |
1728 | * automatically clk_unregister()ed on driver detach. See clk_register() for | |
1729 | * more information. | |
1730 | */ | |
1731 | struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) | |
1732 | { | |
1733 | struct clk *clk; | |
1734 | int ret; | |
1735 | ||
1736 | clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL); | |
1737 | if (!clk) | |
1738 | return ERR_PTR(-ENOMEM); | |
1739 | ||
1740 | ret = _clk_register(dev, hw, clk); | |
1741 | if (!ret) { | |
1742 | devres_add(dev, clk); | |
1743 | } else { | |
1744 | devres_free(clk); | |
1745 | clk = ERR_PTR(ret); | |
1746 | } | |
1747 | ||
1748 | return clk; | |
1749 | } | |
1750 | EXPORT_SYMBOL_GPL(devm_clk_register); | |
1751 | ||
1752 | static int devm_clk_match(struct device *dev, void *res, void *data) | |
1753 | { | |
1754 | struct clk *c = res; | |
1755 | if (WARN_ON(!c)) | |
1756 | return 0; | |
1757 | return c == data; | |
1758 | } | |
1759 | ||
1760 | /** | |
1761 | * devm_clk_unregister - resource managed clk_unregister() | |
1762 | * @clk: clock to unregister | |
1763 | * | |
1764 | * Deallocate a clock allocated with devm_clk_register(). Normally | |
1765 | * this function will not need to be called and the resource management | |
1766 | * code will ensure that the resource is freed. | |
1767 | */ | |
1768 | void devm_clk_unregister(struct device *dev, struct clk *clk) | |
1769 | { | |
1770 | WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); | |
1771 | } | |
1772 | EXPORT_SYMBOL_GPL(devm_clk_unregister); | |
1773 | ||
b2476490 MT |
1774 | /*** clk rate change notifiers ***/ |
1775 | ||
1776 | /** | |
1777 | * clk_notifier_register - add a clk rate change notifier | |
1778 | * @clk: struct clk * to watch | |
1779 | * @nb: struct notifier_block * with callback info | |
1780 | * | |
1781 | * Request notification when clk's rate changes. This uses an SRCU | |
1782 | * notifier because we want it to block and notifier unregistrations are | |
1783 | * uncommon. The callbacks associated with the notifier must not | |
1784 | * re-enter into the clk framework by calling any top-level clk APIs; | |
1785 | * this will cause a nested prepare_lock mutex. | |
1786 | * | |
1787 | * Pre-change notifier callbacks will be passed the current, pre-change | |
1788 | * rate of the clk via struct clk_notifier_data.old_rate. The new, | |
1789 | * post-change rate of the clk is passed via struct | |
1790 | * clk_notifier_data.new_rate. | |
1791 | * | |
1792 | * Post-change notifiers will pass the now-current, post-change rate of | |
1793 | * the clk in both struct clk_notifier_data.old_rate and struct | |
1794 | * clk_notifier_data.new_rate. | |
1795 | * | |
1796 | * Abort-change notifiers are effectively the opposite of pre-change | |
1797 | * notifiers: the original pre-change clk rate is passed in via struct | |
1798 | * clk_notifier_data.new_rate and the failed post-change rate is passed | |
1799 | * in via struct clk_notifier_data.old_rate. | |
1800 | * | |
1801 | * clk_notifier_register() must be called from non-atomic context. | |
1802 | * Returns -EINVAL if called with null arguments, -ENOMEM upon | |
1803 | * allocation failure; otherwise, passes along the return value of | |
1804 | * srcu_notifier_chain_register(). | |
1805 | */ | |
1806 | int clk_notifier_register(struct clk *clk, struct notifier_block *nb) | |
1807 | { | |
1808 | struct clk_notifier *cn; | |
1809 | int ret = -ENOMEM; | |
1810 | ||
1811 | if (!clk || !nb) | |
1812 | return -EINVAL; | |
1813 | ||
eab89f69 | 1814 | clk_prepare_lock(); |
b2476490 MT |
1815 | |
1816 | /* search the list of notifiers for this clk */ | |
1817 | list_for_each_entry(cn, &clk_notifier_list, node) | |
1818 | if (cn->clk == clk) | |
1819 | break; | |
1820 | ||
1821 | /* if clk wasn't in the notifier list, allocate new clk_notifier */ | |
1822 | if (cn->clk != clk) { | |
1823 | cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL); | |
1824 | if (!cn) | |
1825 | goto out; | |
1826 | ||
1827 | cn->clk = clk; | |
1828 | srcu_init_notifier_head(&cn->notifier_head); | |
1829 | ||
1830 | list_add(&cn->node, &clk_notifier_list); | |
1831 | } | |
1832 | ||
1833 | ret = srcu_notifier_chain_register(&cn->notifier_head, nb); | |
1834 | ||
1835 | clk->notifier_count++; | |
1836 | ||
1837 | out: | |
eab89f69 | 1838 | clk_prepare_unlock(); |
b2476490 MT |
1839 | |
1840 | return ret; | |
1841 | } | |
1842 | EXPORT_SYMBOL_GPL(clk_notifier_register); | |
1843 | ||
1844 | /** | |
1845 | * clk_notifier_unregister - remove a clk rate change notifier | |
1846 | * @clk: struct clk * | |
1847 | * @nb: struct notifier_block * with callback info | |
1848 | * | |
1849 | * Request no further notification for changes to 'clk' and frees memory | |
1850 | * allocated in clk_notifier_register. | |
1851 | * | |
1852 | * Returns -EINVAL if called with null arguments; otherwise, passes | |
1853 | * along the return value of srcu_notifier_chain_unregister(). | |
1854 | */ | |
1855 | int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) | |
1856 | { | |
1857 | struct clk_notifier *cn = NULL; | |
1858 | int ret = -EINVAL; | |
1859 | ||
1860 | if (!clk || !nb) | |
1861 | return -EINVAL; | |
1862 | ||
eab89f69 | 1863 | clk_prepare_lock(); |
b2476490 MT |
1864 | |
1865 | list_for_each_entry(cn, &clk_notifier_list, node) | |
1866 | if (cn->clk == clk) | |
1867 | break; | |
1868 | ||
1869 | if (cn->clk == clk) { | |
1870 | ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); | |
1871 | ||
1872 | clk->notifier_count--; | |
1873 | ||
1874 | /* XXX the notifier code should handle this better */ | |
1875 | if (!cn->notifier_head.head) { | |
1876 | srcu_cleanup_notifier_head(&cn->notifier_head); | |
1877 | kfree(cn); | |
1878 | } | |
1879 | ||
1880 | } else { | |
1881 | ret = -ENOENT; | |
1882 | } | |
1883 | ||
eab89f69 | 1884 | clk_prepare_unlock(); |
b2476490 MT |
1885 | |
1886 | return ret; | |
1887 | } | |
1888 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); | |
766e6a4e GL |
1889 | |
1890 | #ifdef CONFIG_OF | |
1891 | /** | |
1892 | * struct of_clk_provider - Clock provider registration structure | |
1893 | * @link: Entry in global list of clock providers | |
1894 | * @node: Pointer to device tree node of clock provider | |
1895 | * @get: Get clock callback. Returns NULL or a struct clk for the | |
1896 | * given clock specifier | |
1897 | * @data: context pointer to be passed into @get callback | |
1898 | */ | |
1899 | struct of_clk_provider { | |
1900 | struct list_head link; | |
1901 | ||
1902 | struct device_node *node; | |
1903 | struct clk *(*get)(struct of_phandle_args *clkspec, void *data); | |
1904 | void *data; | |
1905 | }; | |
1906 | ||
f2f6c255 PG |
1907 | extern struct of_device_id __clk_of_table[]; |
1908 | ||
1909 | static const struct of_device_id __clk_of_table_sentinel | |
1910 | __used __section(__clk_of_table_end); | |
1911 | ||
766e6a4e GL |
1912 | static LIST_HEAD(of_clk_providers); |
1913 | static DEFINE_MUTEX(of_clk_lock); | |
1914 | ||
1915 | struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, | |
1916 | void *data) | |
1917 | { | |
1918 | return data; | |
1919 | } | |
1920 | EXPORT_SYMBOL_GPL(of_clk_src_simple_get); | |
1921 | ||
494bfec9 SG |
1922 | struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) |
1923 | { | |
1924 | struct clk_onecell_data *clk_data = data; | |
1925 | unsigned int idx = clkspec->args[0]; | |
1926 | ||
1927 | if (idx >= clk_data->clk_num) { | |
1928 | pr_err("%s: invalid clock index %d\n", __func__, idx); | |
1929 | return ERR_PTR(-EINVAL); | |
1930 | } | |
1931 | ||
1932 | return clk_data->clks[idx]; | |
1933 | } | |
1934 | EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); | |
1935 | ||
766e6a4e GL |
1936 | /** |
1937 | * of_clk_add_provider() - Register a clock provider for a node | |
1938 | * @np: Device node pointer associated with clock provider | |
1939 | * @clk_src_get: callback for decoding clock | |
1940 | * @data: context pointer for @clk_src_get callback. | |
1941 | */ | |
1942 | int of_clk_add_provider(struct device_node *np, | |
1943 | struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, | |
1944 | void *data), | |
1945 | void *data) | |
1946 | { | |
1947 | struct of_clk_provider *cp; | |
1948 | ||
1949 | cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); | |
1950 | if (!cp) | |
1951 | return -ENOMEM; | |
1952 | ||
1953 | cp->node = of_node_get(np); | |
1954 | cp->data = data; | |
1955 | cp->get = clk_src_get; | |
1956 | ||
1957 | mutex_lock(&of_clk_lock); | |
1958 | list_add(&cp->link, &of_clk_providers); | |
1959 | mutex_unlock(&of_clk_lock); | |
1960 | pr_debug("Added clock from %s\n", np->full_name); | |
1961 | ||
1962 | return 0; | |
1963 | } | |
1964 | EXPORT_SYMBOL_GPL(of_clk_add_provider); | |
1965 | ||
1966 | /** | |
1967 | * of_clk_del_provider() - Remove a previously registered clock provider | |
1968 | * @np: Device node pointer associated with clock provider | |
1969 | */ | |
1970 | void of_clk_del_provider(struct device_node *np) | |
1971 | { | |
1972 | struct of_clk_provider *cp; | |
1973 | ||
1974 | mutex_lock(&of_clk_lock); | |
1975 | list_for_each_entry(cp, &of_clk_providers, link) { | |
1976 | if (cp->node == np) { | |
1977 | list_del(&cp->link); | |
1978 | of_node_put(cp->node); | |
1979 | kfree(cp); | |
1980 | break; | |
1981 | } | |
1982 | } | |
1983 | mutex_unlock(&of_clk_lock); | |
1984 | } | |
1985 | EXPORT_SYMBOL_GPL(of_clk_del_provider); | |
1986 | ||
1987 | struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) | |
1988 | { | |
1989 | struct of_clk_provider *provider; | |
1990 | struct clk *clk = ERR_PTR(-ENOENT); | |
1991 | ||
1992 | /* Check if we have such a provider in our array */ | |
1993 | mutex_lock(&of_clk_lock); | |
1994 | list_for_each_entry(provider, &of_clk_providers, link) { | |
1995 | if (provider->node == clkspec->np) | |
1996 | clk = provider->get(clkspec, provider->data); | |
1997 | if (!IS_ERR(clk)) | |
1998 | break; | |
1999 | } | |
2000 | mutex_unlock(&of_clk_lock); | |
2001 | ||
2002 | return clk; | |
2003 | } | |
2004 | ||
2005 | const char *of_clk_get_parent_name(struct device_node *np, int index) | |
2006 | { | |
2007 | struct of_phandle_args clkspec; | |
2008 | const char *clk_name; | |
2009 | int rc; | |
2010 | ||
2011 | if (index < 0) | |
2012 | return NULL; | |
2013 | ||
2014 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, | |
2015 | &clkspec); | |
2016 | if (rc) | |
2017 | return NULL; | |
2018 | ||
2019 | if (of_property_read_string_index(clkspec.np, "clock-output-names", | |
2020 | clkspec.args_count ? clkspec.args[0] : 0, | |
2021 | &clk_name) < 0) | |
2022 | clk_name = clkspec.np->name; | |
2023 | ||
2024 | of_node_put(clkspec.np); | |
2025 | return clk_name; | |
2026 | } | |
2027 | EXPORT_SYMBOL_GPL(of_clk_get_parent_name); | |
2028 | ||
2029 | /** | |
2030 | * of_clk_init() - Scan and init clock providers from the DT | |
2031 | * @matches: array of compatible values and init functions for providers. | |
2032 | * | |
2033 | * This function scans the device tree for matching clock providers and | |
2034 | * calls their initialization functions | |
2035 | */ | |
2036 | void __init of_clk_init(const struct of_device_id *matches) | |
2037 | { | |
2038 | struct device_node *np; | |
2039 | ||
f2f6c255 PG |
2040 | if (!matches) |
2041 | matches = __clk_of_table; | |
2042 | ||
766e6a4e GL |
2043 | for_each_matching_node(np, matches) { |
2044 | const struct of_device_id *match = of_match_node(matches, np); | |
2045 | of_clk_init_cb_t clk_init_cb = match->data; | |
2046 | clk_init_cb(np); | |
2047 | } | |
2048 | } | |
2049 | #endif |