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 | 23 | |
d6782c26 SN |
24 | #include "clk.h" |
25 | ||
b2476490 MT |
26 | static DEFINE_SPINLOCK(enable_lock); |
27 | static DEFINE_MUTEX(prepare_lock); | |
28 | ||
533ddeb1 MT |
29 | static struct task_struct *prepare_owner; |
30 | static struct task_struct *enable_owner; | |
31 | ||
32 | static int prepare_refcnt; | |
33 | static int enable_refcnt; | |
34 | ||
b2476490 MT |
35 | static HLIST_HEAD(clk_root_list); |
36 | static HLIST_HEAD(clk_orphan_list); | |
37 | static LIST_HEAD(clk_notifier_list); | |
38 | ||
eab89f69 MT |
39 | /*** locking ***/ |
40 | static void clk_prepare_lock(void) | |
41 | { | |
533ddeb1 MT |
42 | if (!mutex_trylock(&prepare_lock)) { |
43 | if (prepare_owner == current) { | |
44 | prepare_refcnt++; | |
45 | return; | |
46 | } | |
47 | mutex_lock(&prepare_lock); | |
48 | } | |
49 | WARN_ON_ONCE(prepare_owner != NULL); | |
50 | WARN_ON_ONCE(prepare_refcnt != 0); | |
51 | prepare_owner = current; | |
52 | prepare_refcnt = 1; | |
eab89f69 MT |
53 | } |
54 | ||
55 | static void clk_prepare_unlock(void) | |
56 | { | |
533ddeb1 MT |
57 | WARN_ON_ONCE(prepare_owner != current); |
58 | WARN_ON_ONCE(prepare_refcnt == 0); | |
59 | ||
60 | if (--prepare_refcnt) | |
61 | return; | |
62 | prepare_owner = NULL; | |
eab89f69 MT |
63 | mutex_unlock(&prepare_lock); |
64 | } | |
65 | ||
66 | static unsigned long clk_enable_lock(void) | |
67 | { | |
68 | unsigned long flags; | |
533ddeb1 MT |
69 | |
70 | if (!spin_trylock_irqsave(&enable_lock, flags)) { | |
71 | if (enable_owner == current) { | |
72 | enable_refcnt++; | |
73 | return flags; | |
74 | } | |
75 | spin_lock_irqsave(&enable_lock, flags); | |
76 | } | |
77 | WARN_ON_ONCE(enable_owner != NULL); | |
78 | WARN_ON_ONCE(enable_refcnt != 0); | |
79 | enable_owner = current; | |
80 | enable_refcnt = 1; | |
eab89f69 MT |
81 | return flags; |
82 | } | |
83 | ||
84 | static void clk_enable_unlock(unsigned long flags) | |
85 | { | |
533ddeb1 MT |
86 | WARN_ON_ONCE(enable_owner != current); |
87 | WARN_ON_ONCE(enable_refcnt == 0); | |
88 | ||
89 | if (--enable_refcnt) | |
90 | return; | |
91 | enable_owner = NULL; | |
eab89f69 MT |
92 | spin_unlock_irqrestore(&enable_lock, flags); |
93 | } | |
94 | ||
b2476490 MT |
95 | /*** debugfs support ***/ |
96 | ||
ea72dc2c | 97 | #ifdef CONFIG_DEBUG_FS |
b2476490 MT |
98 | #include <linux/debugfs.h> |
99 | ||
100 | static struct dentry *rootdir; | |
101 | static struct dentry *orphandir; | |
102 | static int inited = 0; | |
103 | ||
1af599df PG |
104 | static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level) |
105 | { | |
106 | if (!c) | |
107 | return; | |
108 | ||
fb8abb7a | 109 | seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu\n", |
1af599df PG |
110 | level * 3 + 1, "", |
111 | 30 - level * 3, c->name, | |
5279fc40 BB |
112 | c->enable_count, c->prepare_count, clk_get_rate(c), |
113 | clk_get_accuracy(c)); | |
1af599df PG |
114 | } |
115 | ||
116 | static void clk_summary_show_subtree(struct seq_file *s, struct clk *c, | |
117 | int level) | |
118 | { | |
119 | struct clk *child; | |
1af599df PG |
120 | |
121 | if (!c) | |
122 | return; | |
123 | ||
124 | clk_summary_show_one(s, c, level); | |
125 | ||
b67bfe0d | 126 | hlist_for_each_entry(child, &c->children, child_node) |
1af599df PG |
127 | clk_summary_show_subtree(s, child, level + 1); |
128 | } | |
129 | ||
130 | static int clk_summary_show(struct seq_file *s, void *data) | |
131 | { | |
132 | struct clk *c; | |
1af599df | 133 | |
fb8abb7a GU |
134 | seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy\n"); |
135 | seq_puts(s, "--------------------------------------------------------------------------------\n"); | |
1af599df | 136 | |
eab89f69 | 137 | clk_prepare_lock(); |
1af599df | 138 | |
b67bfe0d | 139 | hlist_for_each_entry(c, &clk_root_list, child_node) |
1af599df PG |
140 | clk_summary_show_subtree(s, c, 0); |
141 | ||
b67bfe0d | 142 | hlist_for_each_entry(c, &clk_orphan_list, child_node) |
1af599df PG |
143 | clk_summary_show_subtree(s, c, 0); |
144 | ||
eab89f69 | 145 | clk_prepare_unlock(); |
1af599df PG |
146 | |
147 | return 0; | |
148 | } | |
149 | ||
150 | ||
151 | static int clk_summary_open(struct inode *inode, struct file *file) | |
152 | { | |
153 | return single_open(file, clk_summary_show, inode->i_private); | |
154 | } | |
155 | ||
156 | static const struct file_operations clk_summary_fops = { | |
157 | .open = clk_summary_open, | |
158 | .read = seq_read, | |
159 | .llseek = seq_lseek, | |
160 | .release = single_release, | |
161 | }; | |
162 | ||
bddca894 PG |
163 | static void clk_dump_one(struct seq_file *s, struct clk *c, int level) |
164 | { | |
165 | if (!c) | |
166 | return; | |
167 | ||
168 | seq_printf(s, "\"%s\": { ", c->name); | |
169 | seq_printf(s, "\"enable_count\": %d,", c->enable_count); | |
170 | seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); | |
670decdd | 171 | seq_printf(s, "\"rate\": %lu", clk_get_rate(c)); |
5279fc40 | 172 | seq_printf(s, "\"accuracy\": %lu", clk_get_accuracy(c)); |
bddca894 PG |
173 | } |
174 | ||
175 | static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level) | |
176 | { | |
177 | struct clk *child; | |
bddca894 PG |
178 | |
179 | if (!c) | |
180 | return; | |
181 | ||
182 | clk_dump_one(s, c, level); | |
183 | ||
b67bfe0d | 184 | hlist_for_each_entry(child, &c->children, child_node) { |
bddca894 PG |
185 | seq_printf(s, ","); |
186 | clk_dump_subtree(s, child, level + 1); | |
187 | } | |
188 | ||
189 | seq_printf(s, "}"); | |
190 | } | |
191 | ||
192 | static int clk_dump(struct seq_file *s, void *data) | |
193 | { | |
194 | struct clk *c; | |
bddca894 PG |
195 | bool first_node = true; |
196 | ||
197 | seq_printf(s, "{"); | |
198 | ||
eab89f69 | 199 | clk_prepare_lock(); |
bddca894 | 200 | |
b67bfe0d | 201 | hlist_for_each_entry(c, &clk_root_list, child_node) { |
bddca894 PG |
202 | if (!first_node) |
203 | seq_printf(s, ","); | |
204 | first_node = false; | |
205 | clk_dump_subtree(s, c, 0); | |
206 | } | |
207 | ||
b67bfe0d | 208 | hlist_for_each_entry(c, &clk_orphan_list, child_node) { |
bddca894 PG |
209 | seq_printf(s, ","); |
210 | clk_dump_subtree(s, c, 0); | |
211 | } | |
212 | ||
eab89f69 | 213 | clk_prepare_unlock(); |
bddca894 PG |
214 | |
215 | seq_printf(s, "}"); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | ||
220 | static int clk_dump_open(struct inode *inode, struct file *file) | |
221 | { | |
222 | return single_open(file, clk_dump, inode->i_private); | |
223 | } | |
224 | ||
225 | static const struct file_operations clk_dump_fops = { | |
226 | .open = clk_dump_open, | |
227 | .read = seq_read, | |
228 | .llseek = seq_lseek, | |
229 | .release = single_release, | |
230 | }; | |
231 | ||
b2476490 MT |
232 | /* caller must hold prepare_lock */ |
233 | static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry) | |
234 | { | |
235 | struct dentry *d; | |
236 | int ret = -ENOMEM; | |
237 | ||
238 | if (!clk || !pdentry) { | |
239 | ret = -EINVAL; | |
240 | goto out; | |
241 | } | |
242 | ||
243 | d = debugfs_create_dir(clk->name, pdentry); | |
244 | if (!d) | |
245 | goto out; | |
246 | ||
247 | clk->dentry = d; | |
248 | ||
249 | d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry, | |
250 | (u32 *)&clk->rate); | |
251 | if (!d) | |
252 | goto err_out; | |
253 | ||
5279fc40 BB |
254 | d = debugfs_create_u32("clk_accuracy", S_IRUGO, clk->dentry, |
255 | (u32 *)&clk->accuracy); | |
256 | if (!d) | |
257 | goto err_out; | |
258 | ||
b2476490 MT |
259 | d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry, |
260 | (u32 *)&clk->flags); | |
261 | if (!d) | |
262 | goto err_out; | |
263 | ||
264 | d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry, | |
265 | (u32 *)&clk->prepare_count); | |
266 | if (!d) | |
267 | goto err_out; | |
268 | ||
269 | d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry, | |
270 | (u32 *)&clk->enable_count); | |
271 | if (!d) | |
272 | goto err_out; | |
273 | ||
274 | d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry, | |
275 | (u32 *)&clk->notifier_count); | |
276 | if (!d) | |
277 | goto err_out; | |
278 | ||
c646cbf1 AE |
279 | if (clk->ops->debug_init) |
280 | if (clk->ops->debug_init(clk->hw, clk->dentry)) | |
281 | goto err_out; | |
282 | ||
b2476490 MT |
283 | ret = 0; |
284 | goto out; | |
285 | ||
286 | err_out: | |
b5f98e65 AE |
287 | debugfs_remove_recursive(clk->dentry); |
288 | clk->dentry = NULL; | |
b2476490 MT |
289 | out: |
290 | return ret; | |
291 | } | |
292 | ||
293 | /* caller must hold prepare_lock */ | |
294 | static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry) | |
295 | { | |
296 | struct clk *child; | |
b2476490 MT |
297 | int ret = -EINVAL;; |
298 | ||
299 | if (!clk || !pdentry) | |
300 | goto out; | |
301 | ||
302 | ret = clk_debug_create_one(clk, pdentry); | |
303 | ||
304 | if (ret) | |
305 | goto out; | |
306 | ||
b67bfe0d | 307 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
308 | clk_debug_create_subtree(child, clk->dentry); |
309 | ||
310 | ret = 0; | |
311 | out: | |
312 | return ret; | |
313 | } | |
314 | ||
315 | /** | |
316 | * clk_debug_register - add a clk node to the debugfs clk tree | |
317 | * @clk: the clk being added to the debugfs clk tree | |
318 | * | |
319 | * Dynamically adds a clk to the debugfs clk tree if debugfs has been | |
320 | * initialized. Otherwise it bails out early since the debugfs clk tree | |
321 | * will be created lazily by clk_debug_init as part of a late_initcall. | |
322 | * | |
323 | * Caller must hold prepare_lock. Only clk_init calls this function (so | |
324 | * far) so this is taken care. | |
325 | */ | |
326 | static int clk_debug_register(struct clk *clk) | |
327 | { | |
328 | struct clk *parent; | |
329 | struct dentry *pdentry; | |
330 | int ret = 0; | |
331 | ||
332 | if (!inited) | |
333 | goto out; | |
334 | ||
335 | parent = clk->parent; | |
336 | ||
337 | /* | |
338 | * Check to see if a clk is a root clk. Also check that it is | |
339 | * safe to add this clk to debugfs | |
340 | */ | |
341 | if (!parent) | |
342 | if (clk->flags & CLK_IS_ROOT) | |
343 | pdentry = rootdir; | |
344 | else | |
345 | pdentry = orphandir; | |
346 | else | |
347 | if (parent->dentry) | |
348 | pdentry = parent->dentry; | |
349 | else | |
350 | goto out; | |
351 | ||
352 | ret = clk_debug_create_subtree(clk, pdentry); | |
353 | ||
354 | out: | |
355 | return ret; | |
356 | } | |
357 | ||
fcb0ee6a SN |
358 | /** |
359 | * clk_debug_unregister - remove a clk node from the debugfs clk tree | |
360 | * @clk: the clk being removed from the debugfs clk tree | |
361 | * | |
362 | * Dynamically removes a clk and all it's children clk nodes from the | |
363 | * debugfs clk tree if clk->dentry points to debugfs created by | |
364 | * clk_debug_register in __clk_init. | |
365 | * | |
366 | * Caller must hold prepare_lock. | |
367 | */ | |
368 | static void clk_debug_unregister(struct clk *clk) | |
369 | { | |
370 | debugfs_remove_recursive(clk->dentry); | |
371 | } | |
372 | ||
b33d212f UH |
373 | /** |
374 | * clk_debug_reparent - reparent clk node in the debugfs clk tree | |
375 | * @clk: the clk being reparented | |
376 | * @new_parent: the new clk parent, may be NULL | |
377 | * | |
378 | * Rename clk entry in the debugfs clk tree if debugfs has been | |
379 | * initialized. Otherwise it bails out early since the debugfs clk tree | |
380 | * will be created lazily by clk_debug_init as part of a late_initcall. | |
381 | * | |
382 | * Caller must hold prepare_lock. | |
383 | */ | |
384 | static void clk_debug_reparent(struct clk *clk, struct clk *new_parent) | |
385 | { | |
386 | struct dentry *d; | |
387 | struct dentry *new_parent_d; | |
388 | ||
389 | if (!inited) | |
390 | return; | |
391 | ||
392 | if (new_parent) | |
393 | new_parent_d = new_parent->dentry; | |
394 | else | |
395 | new_parent_d = orphandir; | |
396 | ||
397 | d = debugfs_rename(clk->dentry->d_parent, clk->dentry, | |
398 | new_parent_d, clk->name); | |
399 | if (d) | |
400 | clk->dentry = d; | |
401 | else | |
402 | pr_debug("%s: failed to rename debugfs entry for %s\n", | |
403 | __func__, clk->name); | |
404 | } | |
405 | ||
b2476490 MT |
406 | /** |
407 | * clk_debug_init - lazily create the debugfs clk tree visualization | |
408 | * | |
409 | * clks are often initialized very early during boot before memory can | |
410 | * be dynamically allocated and well before debugfs is setup. | |
411 | * clk_debug_init walks the clk tree hierarchy while holding | |
412 | * prepare_lock and creates the topology as part of a late_initcall, | |
413 | * thus insuring that clks initialized very early will still be | |
414 | * represented in the debugfs clk tree. This function should only be | |
415 | * called once at boot-time, and all other clks added dynamically will | |
416 | * be done so with clk_debug_register. | |
417 | */ | |
418 | static int __init clk_debug_init(void) | |
419 | { | |
420 | struct clk *clk; | |
1af599df | 421 | struct dentry *d; |
b2476490 MT |
422 | |
423 | rootdir = debugfs_create_dir("clk", NULL); | |
424 | ||
425 | if (!rootdir) | |
426 | return -ENOMEM; | |
427 | ||
1af599df PG |
428 | d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL, |
429 | &clk_summary_fops); | |
430 | if (!d) | |
431 | return -ENOMEM; | |
432 | ||
bddca894 PG |
433 | d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL, |
434 | &clk_dump_fops); | |
435 | if (!d) | |
436 | return -ENOMEM; | |
437 | ||
b2476490 MT |
438 | orphandir = debugfs_create_dir("orphans", rootdir); |
439 | ||
440 | if (!orphandir) | |
441 | return -ENOMEM; | |
442 | ||
eab89f69 | 443 | clk_prepare_lock(); |
b2476490 | 444 | |
b67bfe0d | 445 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
b2476490 MT |
446 | clk_debug_create_subtree(clk, rootdir); |
447 | ||
b67bfe0d | 448 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
b2476490 MT |
449 | clk_debug_create_subtree(clk, orphandir); |
450 | ||
451 | inited = 1; | |
452 | ||
eab89f69 | 453 | clk_prepare_unlock(); |
b2476490 MT |
454 | |
455 | return 0; | |
456 | } | |
457 | late_initcall(clk_debug_init); | |
458 | #else | |
459 | static inline int clk_debug_register(struct clk *clk) { return 0; } | |
b33d212f UH |
460 | static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent) |
461 | { | |
462 | } | |
fcb0ee6a SN |
463 | static inline void clk_debug_unregister(struct clk *clk) |
464 | { | |
465 | } | |
70d347e6 | 466 | #endif |
b2476490 | 467 | |
1c155b3d UH |
468 | /* caller must hold prepare_lock */ |
469 | static void clk_unprepare_unused_subtree(struct clk *clk) | |
470 | { | |
471 | struct clk *child; | |
472 | ||
473 | if (!clk) | |
474 | return; | |
475 | ||
476 | hlist_for_each_entry(child, &clk->children, child_node) | |
477 | clk_unprepare_unused_subtree(child); | |
478 | ||
479 | if (clk->prepare_count) | |
480 | return; | |
481 | ||
482 | if (clk->flags & CLK_IGNORE_UNUSED) | |
483 | return; | |
484 | ||
3cc8247f UH |
485 | if (__clk_is_prepared(clk)) { |
486 | if (clk->ops->unprepare_unused) | |
487 | clk->ops->unprepare_unused(clk->hw); | |
488 | else if (clk->ops->unprepare) | |
1c155b3d | 489 | clk->ops->unprepare(clk->hw); |
3cc8247f | 490 | } |
1c155b3d UH |
491 | } |
492 | ||
b2476490 MT |
493 | /* caller must hold prepare_lock */ |
494 | static void clk_disable_unused_subtree(struct clk *clk) | |
495 | { | |
496 | struct clk *child; | |
b2476490 MT |
497 | unsigned long flags; |
498 | ||
499 | if (!clk) | |
500 | goto out; | |
501 | ||
b67bfe0d | 502 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
503 | clk_disable_unused_subtree(child); |
504 | ||
eab89f69 | 505 | flags = clk_enable_lock(); |
b2476490 MT |
506 | |
507 | if (clk->enable_count) | |
508 | goto unlock_out; | |
509 | ||
510 | if (clk->flags & CLK_IGNORE_UNUSED) | |
511 | goto unlock_out; | |
512 | ||
7c045a55 MT |
513 | /* |
514 | * some gate clocks have special needs during the disable-unused | |
515 | * sequence. call .disable_unused if available, otherwise fall | |
516 | * back to .disable | |
517 | */ | |
518 | if (__clk_is_enabled(clk)) { | |
519 | if (clk->ops->disable_unused) | |
520 | clk->ops->disable_unused(clk->hw); | |
521 | else if (clk->ops->disable) | |
522 | clk->ops->disable(clk->hw); | |
523 | } | |
b2476490 MT |
524 | |
525 | unlock_out: | |
eab89f69 | 526 | clk_enable_unlock(flags); |
b2476490 MT |
527 | |
528 | out: | |
529 | return; | |
530 | } | |
531 | ||
1e435256 OJ |
532 | static bool clk_ignore_unused; |
533 | static int __init clk_ignore_unused_setup(char *__unused) | |
534 | { | |
535 | clk_ignore_unused = true; | |
536 | return 1; | |
537 | } | |
538 | __setup("clk_ignore_unused", clk_ignore_unused_setup); | |
539 | ||
b2476490 MT |
540 | static int clk_disable_unused(void) |
541 | { | |
542 | struct clk *clk; | |
b2476490 | 543 | |
1e435256 OJ |
544 | if (clk_ignore_unused) { |
545 | pr_warn("clk: Not disabling unused clocks\n"); | |
546 | return 0; | |
547 | } | |
548 | ||
eab89f69 | 549 | clk_prepare_lock(); |
b2476490 | 550 | |
b67bfe0d | 551 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
b2476490 MT |
552 | clk_disable_unused_subtree(clk); |
553 | ||
b67bfe0d | 554 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) |
b2476490 MT |
555 | clk_disable_unused_subtree(clk); |
556 | ||
1c155b3d UH |
557 | hlist_for_each_entry(clk, &clk_root_list, child_node) |
558 | clk_unprepare_unused_subtree(clk); | |
559 | ||
560 | hlist_for_each_entry(clk, &clk_orphan_list, child_node) | |
561 | clk_unprepare_unused_subtree(clk); | |
562 | ||
eab89f69 | 563 | clk_prepare_unlock(); |
b2476490 MT |
564 | |
565 | return 0; | |
566 | } | |
d41d5805 | 567 | late_initcall_sync(clk_disable_unused); |
b2476490 MT |
568 | |
569 | /*** helper functions ***/ | |
570 | ||
65800b2c | 571 | const char *__clk_get_name(struct clk *clk) |
b2476490 MT |
572 | { |
573 | return !clk ? NULL : clk->name; | |
574 | } | |
4895084c | 575 | EXPORT_SYMBOL_GPL(__clk_get_name); |
b2476490 | 576 | |
65800b2c | 577 | struct clk_hw *__clk_get_hw(struct clk *clk) |
b2476490 MT |
578 | { |
579 | return !clk ? NULL : clk->hw; | |
580 | } | |
0b7f04b8 | 581 | EXPORT_SYMBOL_GPL(__clk_get_hw); |
b2476490 | 582 | |
65800b2c | 583 | u8 __clk_get_num_parents(struct clk *clk) |
b2476490 | 584 | { |
2ac6b1f5 | 585 | return !clk ? 0 : clk->num_parents; |
b2476490 | 586 | } |
0b7f04b8 | 587 | EXPORT_SYMBOL_GPL(__clk_get_num_parents); |
b2476490 | 588 | |
65800b2c | 589 | struct clk *__clk_get_parent(struct clk *clk) |
b2476490 MT |
590 | { |
591 | return !clk ? NULL : clk->parent; | |
592 | } | |
0b7f04b8 | 593 | EXPORT_SYMBOL_GPL(__clk_get_parent); |
b2476490 | 594 | |
7ef3dcc8 JH |
595 | struct clk *clk_get_parent_by_index(struct clk *clk, u8 index) |
596 | { | |
597 | if (!clk || index >= clk->num_parents) | |
598 | return NULL; | |
599 | else if (!clk->parents) | |
600 | return __clk_lookup(clk->parent_names[index]); | |
601 | else if (!clk->parents[index]) | |
602 | return clk->parents[index] = | |
603 | __clk_lookup(clk->parent_names[index]); | |
604 | else | |
605 | return clk->parents[index]; | |
606 | } | |
0b7f04b8 | 607 | EXPORT_SYMBOL_GPL(clk_get_parent_by_index); |
7ef3dcc8 | 608 | |
65800b2c | 609 | unsigned int __clk_get_enable_count(struct clk *clk) |
b2476490 | 610 | { |
2ac6b1f5 | 611 | return !clk ? 0 : clk->enable_count; |
b2476490 MT |
612 | } |
613 | ||
65800b2c | 614 | unsigned int __clk_get_prepare_count(struct clk *clk) |
b2476490 | 615 | { |
2ac6b1f5 | 616 | return !clk ? 0 : clk->prepare_count; |
b2476490 MT |
617 | } |
618 | ||
619 | unsigned long __clk_get_rate(struct clk *clk) | |
620 | { | |
621 | unsigned long ret; | |
622 | ||
623 | if (!clk) { | |
34e44fe8 | 624 | ret = 0; |
b2476490 MT |
625 | goto out; |
626 | } | |
627 | ||
628 | ret = clk->rate; | |
629 | ||
630 | if (clk->flags & CLK_IS_ROOT) | |
631 | goto out; | |
632 | ||
633 | if (!clk->parent) | |
34e44fe8 | 634 | ret = 0; |
b2476490 MT |
635 | |
636 | out: | |
637 | return ret; | |
638 | } | |
0b7f04b8 | 639 | EXPORT_SYMBOL_GPL(__clk_get_rate); |
b2476490 | 640 | |
5279fc40 BB |
641 | unsigned long __clk_get_accuracy(struct clk *clk) |
642 | { | |
643 | if (!clk) | |
644 | return 0; | |
645 | ||
646 | return clk->accuracy; | |
647 | } | |
648 | ||
65800b2c | 649 | unsigned long __clk_get_flags(struct clk *clk) |
b2476490 | 650 | { |
2ac6b1f5 | 651 | return !clk ? 0 : clk->flags; |
b2476490 | 652 | } |
b05c6836 | 653 | EXPORT_SYMBOL_GPL(__clk_get_flags); |
b2476490 | 654 | |
3d6ee287 UH |
655 | bool __clk_is_prepared(struct clk *clk) |
656 | { | |
657 | int ret; | |
658 | ||
659 | if (!clk) | |
660 | return false; | |
661 | ||
662 | /* | |
663 | * .is_prepared is optional for clocks that can prepare | |
664 | * fall back to software usage counter if it is missing | |
665 | */ | |
666 | if (!clk->ops->is_prepared) { | |
667 | ret = clk->prepare_count ? 1 : 0; | |
668 | goto out; | |
669 | } | |
670 | ||
671 | ret = clk->ops->is_prepared(clk->hw); | |
672 | out: | |
673 | return !!ret; | |
674 | } | |
675 | ||
2ac6b1f5 | 676 | bool __clk_is_enabled(struct clk *clk) |
b2476490 MT |
677 | { |
678 | int ret; | |
679 | ||
680 | if (!clk) | |
2ac6b1f5 | 681 | return false; |
b2476490 MT |
682 | |
683 | /* | |
684 | * .is_enabled is only mandatory for clocks that gate | |
685 | * fall back to software usage counter if .is_enabled is missing | |
686 | */ | |
687 | if (!clk->ops->is_enabled) { | |
688 | ret = clk->enable_count ? 1 : 0; | |
689 | goto out; | |
690 | } | |
691 | ||
692 | ret = clk->ops->is_enabled(clk->hw); | |
693 | out: | |
2ac6b1f5 | 694 | return !!ret; |
b2476490 | 695 | } |
0b7f04b8 | 696 | EXPORT_SYMBOL_GPL(__clk_is_enabled); |
b2476490 MT |
697 | |
698 | static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk) | |
699 | { | |
700 | struct clk *child; | |
701 | struct clk *ret; | |
b2476490 MT |
702 | |
703 | if (!strcmp(clk->name, name)) | |
704 | return clk; | |
705 | ||
b67bfe0d | 706 | hlist_for_each_entry(child, &clk->children, child_node) { |
b2476490 MT |
707 | ret = __clk_lookup_subtree(name, child); |
708 | if (ret) | |
709 | return ret; | |
710 | } | |
711 | ||
712 | return NULL; | |
713 | } | |
714 | ||
715 | struct clk *__clk_lookup(const char *name) | |
716 | { | |
717 | struct clk *root_clk; | |
718 | struct clk *ret; | |
b2476490 MT |
719 | |
720 | if (!name) | |
721 | return NULL; | |
722 | ||
723 | /* search the 'proper' clk tree first */ | |
b67bfe0d | 724 | hlist_for_each_entry(root_clk, &clk_root_list, child_node) { |
b2476490 MT |
725 | ret = __clk_lookup_subtree(name, root_clk); |
726 | if (ret) | |
727 | return ret; | |
728 | } | |
729 | ||
730 | /* if not found, then search the orphan tree */ | |
b67bfe0d | 731 | hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { |
b2476490 MT |
732 | ret = __clk_lookup_subtree(name, root_clk); |
733 | if (ret) | |
734 | return ret; | |
735 | } | |
736 | ||
737 | return NULL; | |
738 | } | |
739 | ||
e366fdd7 JH |
740 | /* |
741 | * Helper for finding best parent to provide a given frequency. This can be used | |
742 | * directly as a determine_rate callback (e.g. for a mux), or from a more | |
743 | * complex clock that may combine a mux with other operations. | |
744 | */ | |
745 | long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate, | |
746 | unsigned long *best_parent_rate, | |
747 | struct clk **best_parent_p) | |
748 | { | |
749 | struct clk *clk = hw->clk, *parent, *best_parent = NULL; | |
750 | int i, num_parents; | |
751 | unsigned long parent_rate, best = 0; | |
752 | ||
753 | /* if NO_REPARENT flag set, pass through to current parent */ | |
754 | if (clk->flags & CLK_SET_RATE_NO_REPARENT) { | |
755 | parent = clk->parent; | |
756 | if (clk->flags & CLK_SET_RATE_PARENT) | |
757 | best = __clk_round_rate(parent, rate); | |
758 | else if (parent) | |
759 | best = __clk_get_rate(parent); | |
760 | else | |
761 | best = __clk_get_rate(clk); | |
762 | goto out; | |
763 | } | |
764 | ||
765 | /* find the parent that can provide the fastest rate <= rate */ | |
766 | num_parents = clk->num_parents; | |
767 | for (i = 0; i < num_parents; i++) { | |
768 | parent = clk_get_parent_by_index(clk, i); | |
769 | if (!parent) | |
770 | continue; | |
771 | if (clk->flags & CLK_SET_RATE_PARENT) | |
772 | parent_rate = __clk_round_rate(parent, rate); | |
773 | else | |
774 | parent_rate = __clk_get_rate(parent); | |
775 | if (parent_rate <= rate && parent_rate > best) { | |
776 | best_parent = parent; | |
777 | best = parent_rate; | |
778 | } | |
779 | } | |
780 | ||
781 | out: | |
782 | if (best_parent) | |
783 | *best_parent_p = best_parent; | |
784 | *best_parent_rate = best; | |
785 | ||
786 | return best; | |
787 | } | |
0b7f04b8 | 788 | EXPORT_SYMBOL_GPL(__clk_mux_determine_rate); |
e366fdd7 | 789 | |
b2476490 MT |
790 | /*** clk api ***/ |
791 | ||
792 | void __clk_unprepare(struct clk *clk) | |
793 | { | |
794 | if (!clk) | |
795 | return; | |
796 | ||
797 | if (WARN_ON(clk->prepare_count == 0)) | |
798 | return; | |
799 | ||
800 | if (--clk->prepare_count > 0) | |
801 | return; | |
802 | ||
803 | WARN_ON(clk->enable_count > 0); | |
804 | ||
805 | if (clk->ops->unprepare) | |
806 | clk->ops->unprepare(clk->hw); | |
807 | ||
808 | __clk_unprepare(clk->parent); | |
809 | } | |
810 | ||
811 | /** | |
812 | * clk_unprepare - undo preparation of a clock source | |
24ee1a08 | 813 | * @clk: the clk being unprepared |
b2476490 MT |
814 | * |
815 | * clk_unprepare may sleep, which differentiates it from clk_disable. In a | |
816 | * simple case, clk_unprepare can be used instead of clk_disable to gate a clk | |
817 | * if the operation may sleep. One example is a clk which is accessed over | |
818 | * I2c. In the complex case a clk gate operation may require a fast and a slow | |
819 | * part. It is this reason that clk_unprepare and clk_disable are not mutually | |
820 | * exclusive. In fact clk_disable must be called before clk_unprepare. | |
821 | */ | |
822 | void clk_unprepare(struct clk *clk) | |
823 | { | |
63589e92 SB |
824 | if (IS_ERR_OR_NULL(clk)) |
825 | return; | |
826 | ||
eab89f69 | 827 | clk_prepare_lock(); |
b2476490 | 828 | __clk_unprepare(clk); |
eab89f69 | 829 | clk_prepare_unlock(); |
b2476490 MT |
830 | } |
831 | EXPORT_SYMBOL_GPL(clk_unprepare); | |
832 | ||
833 | int __clk_prepare(struct clk *clk) | |
834 | { | |
835 | int ret = 0; | |
836 | ||
837 | if (!clk) | |
838 | return 0; | |
839 | ||
840 | if (clk->prepare_count == 0) { | |
841 | ret = __clk_prepare(clk->parent); | |
842 | if (ret) | |
843 | return ret; | |
844 | ||
845 | if (clk->ops->prepare) { | |
846 | ret = clk->ops->prepare(clk->hw); | |
847 | if (ret) { | |
848 | __clk_unprepare(clk->parent); | |
849 | return ret; | |
850 | } | |
851 | } | |
852 | } | |
853 | ||
854 | clk->prepare_count++; | |
855 | ||
856 | return 0; | |
857 | } | |
858 | ||
859 | /** | |
860 | * clk_prepare - prepare a clock source | |
861 | * @clk: the clk being prepared | |
862 | * | |
863 | * clk_prepare may sleep, which differentiates it from clk_enable. In a simple | |
864 | * case, clk_prepare can be used instead of clk_enable to ungate a clk if the | |
865 | * operation may sleep. One example is a clk which is accessed over I2c. In | |
866 | * the complex case a clk ungate operation may require a fast and a slow part. | |
867 | * It is this reason that clk_prepare and clk_enable are not mutually | |
868 | * exclusive. In fact clk_prepare must be called before clk_enable. | |
869 | * Returns 0 on success, -EERROR otherwise. | |
870 | */ | |
871 | int clk_prepare(struct clk *clk) | |
872 | { | |
873 | int ret; | |
874 | ||
eab89f69 | 875 | clk_prepare_lock(); |
b2476490 | 876 | ret = __clk_prepare(clk); |
eab89f69 | 877 | clk_prepare_unlock(); |
b2476490 MT |
878 | |
879 | return ret; | |
880 | } | |
881 | EXPORT_SYMBOL_GPL(clk_prepare); | |
882 | ||
883 | static void __clk_disable(struct clk *clk) | |
884 | { | |
885 | if (!clk) | |
886 | return; | |
887 | ||
888 | if (WARN_ON(clk->enable_count == 0)) | |
889 | return; | |
890 | ||
891 | if (--clk->enable_count > 0) | |
892 | return; | |
893 | ||
894 | if (clk->ops->disable) | |
895 | clk->ops->disable(clk->hw); | |
896 | ||
897 | __clk_disable(clk->parent); | |
898 | } | |
899 | ||
900 | /** | |
901 | * clk_disable - gate a clock | |
902 | * @clk: the clk being gated | |
903 | * | |
904 | * clk_disable must not sleep, which differentiates it from clk_unprepare. In | |
905 | * a simple case, clk_disable can be used instead of clk_unprepare to gate a | |
906 | * clk if the operation is fast and will never sleep. One example is a | |
907 | * SoC-internal clk which is controlled via simple register writes. In the | |
908 | * complex case a clk gate operation may require a fast and a slow part. It is | |
909 | * this reason that clk_unprepare and clk_disable are not mutually exclusive. | |
910 | * In fact clk_disable must be called before clk_unprepare. | |
911 | */ | |
912 | void clk_disable(struct clk *clk) | |
913 | { | |
914 | unsigned long flags; | |
915 | ||
63589e92 SB |
916 | if (IS_ERR_OR_NULL(clk)) |
917 | return; | |
918 | ||
eab89f69 | 919 | flags = clk_enable_lock(); |
b2476490 | 920 | __clk_disable(clk); |
eab89f69 | 921 | clk_enable_unlock(flags); |
b2476490 MT |
922 | } |
923 | EXPORT_SYMBOL_GPL(clk_disable); | |
924 | ||
925 | static int __clk_enable(struct clk *clk) | |
926 | { | |
927 | int ret = 0; | |
928 | ||
929 | if (!clk) | |
930 | return 0; | |
931 | ||
932 | if (WARN_ON(clk->prepare_count == 0)) | |
933 | return -ESHUTDOWN; | |
934 | ||
935 | if (clk->enable_count == 0) { | |
936 | ret = __clk_enable(clk->parent); | |
937 | ||
938 | if (ret) | |
939 | return ret; | |
940 | ||
941 | if (clk->ops->enable) { | |
942 | ret = clk->ops->enable(clk->hw); | |
943 | if (ret) { | |
944 | __clk_disable(clk->parent); | |
945 | return ret; | |
946 | } | |
947 | } | |
948 | } | |
949 | ||
950 | clk->enable_count++; | |
951 | return 0; | |
952 | } | |
953 | ||
954 | /** | |
955 | * clk_enable - ungate a clock | |
956 | * @clk: the clk being ungated | |
957 | * | |
958 | * clk_enable must not sleep, which differentiates it from clk_prepare. In a | |
959 | * simple case, clk_enable can be used instead of clk_prepare to ungate a clk | |
960 | * if the operation will never sleep. One example is a SoC-internal clk which | |
961 | * is controlled via simple register writes. In the complex case a clk ungate | |
962 | * operation may require a fast and a slow part. It is this reason that | |
963 | * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare | |
964 | * must be called before clk_enable. Returns 0 on success, -EERROR | |
965 | * otherwise. | |
966 | */ | |
967 | int clk_enable(struct clk *clk) | |
968 | { | |
969 | unsigned long flags; | |
970 | int ret; | |
971 | ||
eab89f69 | 972 | flags = clk_enable_lock(); |
b2476490 | 973 | ret = __clk_enable(clk); |
eab89f69 | 974 | clk_enable_unlock(flags); |
b2476490 MT |
975 | |
976 | return ret; | |
977 | } | |
978 | EXPORT_SYMBOL_GPL(clk_enable); | |
979 | ||
b2476490 MT |
980 | /** |
981 | * __clk_round_rate - round the given rate for a clk | |
982 | * @clk: round the rate of this clock | |
24ee1a08 | 983 | * @rate: the rate which is to be rounded |
b2476490 MT |
984 | * |
985 | * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate | |
986 | */ | |
987 | unsigned long __clk_round_rate(struct clk *clk, unsigned long rate) | |
988 | { | |
81536e07 | 989 | unsigned long parent_rate = 0; |
71472c0c | 990 | struct clk *parent; |
b2476490 MT |
991 | |
992 | if (!clk) | |
2ac6b1f5 | 993 | return 0; |
b2476490 | 994 | |
71472c0c JH |
995 | parent = clk->parent; |
996 | if (parent) | |
997 | parent_rate = parent->rate; | |
998 | ||
999 | if (clk->ops->determine_rate) | |
1000 | return clk->ops->determine_rate(clk->hw, rate, &parent_rate, | |
1001 | &parent); | |
1002 | else if (clk->ops->round_rate) | |
1003 | return clk->ops->round_rate(clk->hw, rate, &parent_rate); | |
1004 | else if (clk->flags & CLK_SET_RATE_PARENT) | |
1005 | return __clk_round_rate(clk->parent, rate); | |
1006 | else | |
1007 | return clk->rate; | |
b2476490 MT |
1008 | } |
1009 | ||
1010 | /** | |
1011 | * clk_round_rate - round the given rate for a clk | |
1012 | * @clk: the clk for which we are rounding a rate | |
1013 | * @rate: the rate which is to be rounded | |
1014 | * | |
1015 | * Takes in a rate as input and rounds it to a rate that the clk can actually | |
1016 | * use which is then returned. If clk doesn't support round_rate operation | |
1017 | * then the parent rate is returned. | |
1018 | */ | |
1019 | long clk_round_rate(struct clk *clk, unsigned long rate) | |
1020 | { | |
1021 | unsigned long ret; | |
1022 | ||
eab89f69 | 1023 | clk_prepare_lock(); |
b2476490 | 1024 | ret = __clk_round_rate(clk, rate); |
eab89f69 | 1025 | clk_prepare_unlock(); |
b2476490 MT |
1026 | |
1027 | return ret; | |
1028 | } | |
1029 | EXPORT_SYMBOL_GPL(clk_round_rate); | |
1030 | ||
1031 | /** | |
1032 | * __clk_notify - call clk notifier chain | |
1033 | * @clk: struct clk * that is changing rate | |
1034 | * @msg: clk notifier type (see include/linux/clk.h) | |
1035 | * @old_rate: old clk rate | |
1036 | * @new_rate: new clk rate | |
1037 | * | |
1038 | * Triggers a notifier call chain on the clk rate-change notification | |
1039 | * for 'clk'. Passes a pointer to the struct clk and the previous | |
1040 | * and current rates to the notifier callback. Intended to be called by | |
1041 | * internal clock code only. Returns NOTIFY_DONE from the last driver | |
1042 | * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if | |
1043 | * a driver returns that. | |
1044 | */ | |
1045 | static int __clk_notify(struct clk *clk, unsigned long msg, | |
1046 | unsigned long old_rate, unsigned long new_rate) | |
1047 | { | |
1048 | struct clk_notifier *cn; | |
1049 | struct clk_notifier_data cnd; | |
1050 | int ret = NOTIFY_DONE; | |
1051 | ||
1052 | cnd.clk = clk; | |
1053 | cnd.old_rate = old_rate; | |
1054 | cnd.new_rate = new_rate; | |
1055 | ||
1056 | list_for_each_entry(cn, &clk_notifier_list, node) { | |
1057 | if (cn->clk == clk) { | |
1058 | ret = srcu_notifier_call_chain(&cn->notifier_head, msg, | |
1059 | &cnd); | |
1060 | break; | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | return ret; | |
1065 | } | |
1066 | ||
5279fc40 BB |
1067 | /** |
1068 | * __clk_recalc_accuracies | |
1069 | * @clk: first clk in the subtree | |
1070 | * | |
1071 | * Walks the subtree of clks starting with clk and recalculates accuracies as | |
1072 | * it goes. Note that if a clk does not implement the .recalc_accuracy | |
1073 | * callback then it is assumed that the clock will take on the accuracy of it's | |
1074 | * parent. | |
1075 | * | |
1076 | * Caller must hold prepare_lock. | |
1077 | */ | |
1078 | static void __clk_recalc_accuracies(struct clk *clk) | |
1079 | { | |
1080 | unsigned long parent_accuracy = 0; | |
1081 | struct clk *child; | |
1082 | ||
1083 | if (clk->parent) | |
1084 | parent_accuracy = clk->parent->accuracy; | |
1085 | ||
1086 | if (clk->ops->recalc_accuracy) | |
1087 | clk->accuracy = clk->ops->recalc_accuracy(clk->hw, | |
1088 | parent_accuracy); | |
1089 | else | |
1090 | clk->accuracy = parent_accuracy; | |
1091 | ||
1092 | hlist_for_each_entry(child, &clk->children, child_node) | |
1093 | __clk_recalc_accuracies(child); | |
1094 | } | |
1095 | ||
1096 | /** | |
1097 | * clk_get_accuracy - return the accuracy of clk | |
1098 | * @clk: the clk whose accuracy is being returned | |
1099 | * | |
1100 | * Simply returns the cached accuracy of the clk, unless | |
1101 | * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be | |
1102 | * issued. | |
1103 | * If clk is NULL then returns 0. | |
1104 | */ | |
1105 | long clk_get_accuracy(struct clk *clk) | |
1106 | { | |
1107 | unsigned long accuracy; | |
1108 | ||
1109 | clk_prepare_lock(); | |
1110 | if (clk && (clk->flags & CLK_GET_ACCURACY_NOCACHE)) | |
1111 | __clk_recalc_accuracies(clk); | |
1112 | ||
1113 | accuracy = __clk_get_accuracy(clk); | |
1114 | clk_prepare_unlock(); | |
1115 | ||
1116 | return accuracy; | |
1117 | } | |
1118 | EXPORT_SYMBOL_GPL(clk_get_accuracy); | |
1119 | ||
8f2c2db1 SB |
1120 | static unsigned long clk_recalc(struct clk *clk, unsigned long parent_rate) |
1121 | { | |
1122 | if (clk->ops->recalc_rate) | |
1123 | return clk->ops->recalc_rate(clk->hw, parent_rate); | |
1124 | return parent_rate; | |
1125 | } | |
1126 | ||
b2476490 MT |
1127 | /** |
1128 | * __clk_recalc_rates | |
1129 | * @clk: first clk in the subtree | |
1130 | * @msg: notification type (see include/linux/clk.h) | |
1131 | * | |
1132 | * Walks the subtree of clks starting with clk and recalculates rates as it | |
1133 | * goes. Note that if a clk does not implement the .recalc_rate callback then | |
24ee1a08 | 1134 | * it is assumed that the clock will take on the rate of its parent. |
b2476490 MT |
1135 | * |
1136 | * clk_recalc_rates also propagates the POST_RATE_CHANGE notification, | |
1137 | * if necessary. | |
1138 | * | |
1139 | * Caller must hold prepare_lock. | |
1140 | */ | |
1141 | static void __clk_recalc_rates(struct clk *clk, unsigned long msg) | |
1142 | { | |
1143 | unsigned long old_rate; | |
1144 | unsigned long parent_rate = 0; | |
b2476490 MT |
1145 | struct clk *child; |
1146 | ||
1147 | old_rate = clk->rate; | |
1148 | ||
1149 | if (clk->parent) | |
1150 | parent_rate = clk->parent->rate; | |
1151 | ||
8f2c2db1 | 1152 | clk->rate = clk_recalc(clk, parent_rate); |
b2476490 MT |
1153 | |
1154 | /* | |
1155 | * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE | |
1156 | * & ABORT_RATE_CHANGE notifiers | |
1157 | */ | |
1158 | if (clk->notifier_count && msg) | |
1159 | __clk_notify(clk, msg, old_rate, clk->rate); | |
1160 | ||
b67bfe0d | 1161 | hlist_for_each_entry(child, &clk->children, child_node) |
b2476490 MT |
1162 | __clk_recalc_rates(child, msg); |
1163 | } | |
1164 | ||
a093bde2 UH |
1165 | /** |
1166 | * clk_get_rate - return the rate of clk | |
1167 | * @clk: the clk whose rate is being returned | |
1168 | * | |
1169 | * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag | |
1170 | * is set, which means a recalc_rate will be issued. | |
1171 | * If clk is NULL then returns 0. | |
1172 | */ | |
1173 | unsigned long clk_get_rate(struct clk *clk) | |
1174 | { | |
1175 | unsigned long rate; | |
1176 | ||
eab89f69 | 1177 | clk_prepare_lock(); |
a093bde2 UH |
1178 | |
1179 | if (clk && (clk->flags & CLK_GET_RATE_NOCACHE)) | |
1180 | __clk_recalc_rates(clk, 0); | |
1181 | ||
1182 | rate = __clk_get_rate(clk); | |
eab89f69 | 1183 | clk_prepare_unlock(); |
a093bde2 UH |
1184 | |
1185 | return rate; | |
1186 | } | |
1187 | EXPORT_SYMBOL_GPL(clk_get_rate); | |
1188 | ||
f1c8b2ed | 1189 | static int clk_fetch_parent_index(struct clk *clk, struct clk *parent) |
4935b22c | 1190 | { |
f1c8b2ed | 1191 | int i; |
4935b22c | 1192 | |
f1c8b2ed | 1193 | if (!clk->parents) { |
96a7ed90 TF |
1194 | clk->parents = kcalloc(clk->num_parents, |
1195 | sizeof(struct clk *), GFP_KERNEL); | |
f1c8b2ed TF |
1196 | if (!clk->parents) |
1197 | return -ENOMEM; | |
1198 | } | |
4935b22c JH |
1199 | |
1200 | /* | |
1201 | * find index of new parent clock using cached parent ptrs, | |
1202 | * or if not yet cached, use string name comparison and cache | |
1203 | * them now to avoid future calls to __clk_lookup. | |
1204 | */ | |
1205 | for (i = 0; i < clk->num_parents; i++) { | |
da0f0b2c | 1206 | if (clk->parents[i] == parent) |
f1c8b2ed | 1207 | return i; |
da0f0b2c TF |
1208 | |
1209 | if (clk->parents[i]) | |
1210 | continue; | |
1211 | ||
1212 | if (!strcmp(clk->parent_names[i], parent->name)) { | |
1213 | clk->parents[i] = __clk_lookup(parent->name); | |
f1c8b2ed | 1214 | return i; |
4935b22c JH |
1215 | } |
1216 | } | |
1217 | ||
f1c8b2ed | 1218 | return -EINVAL; |
4935b22c JH |
1219 | } |
1220 | ||
1221 | static void clk_reparent(struct clk *clk, struct clk *new_parent) | |
1222 | { | |
1223 | hlist_del(&clk->child_node); | |
1224 | ||
903efc55 JH |
1225 | if (new_parent) { |
1226 | /* avoid duplicate POST_RATE_CHANGE notifications */ | |
1227 | if (new_parent->new_child == clk) | |
1228 | new_parent->new_child = NULL; | |
1229 | ||
4935b22c | 1230 | hlist_add_head(&clk->child_node, &new_parent->children); |
903efc55 | 1231 | } else { |
4935b22c | 1232 | hlist_add_head(&clk->child_node, &clk_orphan_list); |
903efc55 | 1233 | } |
4935b22c JH |
1234 | |
1235 | clk->parent = new_parent; | |
1236 | } | |
1237 | ||
3fa2252b | 1238 | static struct clk *__clk_set_parent_before(struct clk *clk, struct clk *parent) |
4935b22c JH |
1239 | { |
1240 | unsigned long flags; | |
4935b22c JH |
1241 | struct clk *old_parent = clk->parent; |
1242 | ||
1243 | /* | |
1244 | * Migrate prepare state between parents and prevent race with | |
1245 | * clk_enable(). | |
1246 | * | |
1247 | * If the clock is not prepared, then a race with | |
1248 | * clk_enable/disable() is impossible since we already have the | |
1249 | * prepare lock (future calls to clk_enable() need to be preceded by | |
1250 | * a clk_prepare()). | |
1251 | * | |
1252 | * If the clock is prepared, migrate the prepared state to the new | |
1253 | * parent and also protect against a race with clk_enable() by | |
1254 | * forcing the clock and the new parent on. This ensures that all | |
1255 | * future calls to clk_enable() are practically NOPs with respect to | |
1256 | * hardware and software states. | |
1257 | * | |
1258 | * See also: Comment for clk_set_parent() below. | |
1259 | */ | |
1260 | if (clk->prepare_count) { | |
1261 | __clk_prepare(parent); | |
1262 | clk_enable(parent); | |
1263 | clk_enable(clk); | |
1264 | } | |
1265 | ||
1266 | /* update the clk tree topology */ | |
1267 | flags = clk_enable_lock(); | |
1268 | clk_reparent(clk, parent); | |
1269 | clk_enable_unlock(flags); | |
1270 | ||
3fa2252b SB |
1271 | return old_parent; |
1272 | } | |
1273 | ||
1274 | static void __clk_set_parent_after(struct clk *clk, struct clk *parent, | |
1275 | struct clk *old_parent) | |
1276 | { | |
1277 | /* | |
1278 | * Finish the migration of prepare state and undo the changes done | |
1279 | * for preventing a race with clk_enable(). | |
1280 | */ | |
1281 | if (clk->prepare_count) { | |
1282 | clk_disable(clk); | |
1283 | clk_disable(old_parent); | |
1284 | __clk_unprepare(old_parent); | |
1285 | } | |
1286 | ||
1287 | /* update debugfs with new clk tree topology */ | |
1288 | clk_debug_reparent(clk, parent); | |
1289 | } | |
1290 | ||
1291 | static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index) | |
1292 | { | |
1293 | unsigned long flags; | |
1294 | int ret = 0; | |
1295 | struct clk *old_parent; | |
1296 | ||
1297 | old_parent = __clk_set_parent_before(clk, parent); | |
1298 | ||
4935b22c JH |
1299 | /* change clock input source */ |
1300 | if (parent && clk->ops->set_parent) | |
1301 | ret = clk->ops->set_parent(clk->hw, p_index); | |
1302 | ||
1303 | if (ret) { | |
1304 | flags = clk_enable_lock(); | |
1305 | clk_reparent(clk, old_parent); | |
1306 | clk_enable_unlock(flags); | |
1307 | ||
1308 | if (clk->prepare_count) { | |
1309 | clk_disable(clk); | |
1310 | clk_disable(parent); | |
1311 | __clk_unprepare(parent); | |
1312 | } | |
1313 | return ret; | |
1314 | } | |
1315 | ||
3fa2252b | 1316 | __clk_set_parent_after(clk, parent, old_parent); |
4935b22c | 1317 | |
4935b22c JH |
1318 | return 0; |
1319 | } | |
1320 | ||
b2476490 MT |
1321 | /** |
1322 | * __clk_speculate_rates | |
1323 | * @clk: first clk in the subtree | |
1324 | * @parent_rate: the "future" rate of clk's parent | |
1325 | * | |
1326 | * Walks the subtree of clks starting with clk, speculating rates as it | |
1327 | * goes and firing off PRE_RATE_CHANGE notifications as necessary. | |
1328 | * | |
1329 | * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending | |
1330 | * pre-rate change notifications and returns early if no clks in the | |
1331 | * subtree have subscribed to the notifications. Note that if a clk does not | |
1332 | * implement the .recalc_rate callback then it is assumed that the clock will | |
24ee1a08 | 1333 | * take on the rate of its parent. |
b2476490 MT |
1334 | * |
1335 | * Caller must hold prepare_lock. | |
1336 | */ | |
1337 | static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate) | |
1338 | { | |
b2476490 MT |
1339 | struct clk *child; |
1340 | unsigned long new_rate; | |
1341 | int ret = NOTIFY_DONE; | |
1342 | ||
8f2c2db1 | 1343 | new_rate = clk_recalc(clk, parent_rate); |
b2476490 | 1344 | |
fb72a059 | 1345 | /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ |
b2476490 MT |
1346 | if (clk->notifier_count) |
1347 | ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate); | |
1348 | ||
86bcfa2e MT |
1349 | if (ret & NOTIFY_STOP_MASK) { |
1350 | pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", | |
1351 | __func__, clk->name, ret); | |
b2476490 | 1352 | goto out; |
86bcfa2e | 1353 | } |
b2476490 | 1354 | |
b67bfe0d | 1355 | hlist_for_each_entry(child, &clk->children, child_node) { |
b2476490 | 1356 | ret = __clk_speculate_rates(child, new_rate); |
fb72a059 | 1357 | if (ret & NOTIFY_STOP_MASK) |
b2476490 MT |
1358 | break; |
1359 | } | |
1360 | ||
1361 | out: | |
1362 | return ret; | |
1363 | } | |
1364 | ||
71472c0c JH |
1365 | static void clk_calc_subtree(struct clk *clk, unsigned long new_rate, |
1366 | struct clk *new_parent, u8 p_index) | |
b2476490 MT |
1367 | { |
1368 | struct clk *child; | |
b2476490 MT |
1369 | |
1370 | clk->new_rate = new_rate; | |
71472c0c JH |
1371 | clk->new_parent = new_parent; |
1372 | clk->new_parent_index = p_index; | |
1373 | /* include clk in new parent's PRE_RATE_CHANGE notifications */ | |
1374 | clk->new_child = NULL; | |
1375 | if (new_parent && new_parent != clk->parent) | |
1376 | new_parent->new_child = clk; | |
b2476490 | 1377 | |
b67bfe0d | 1378 | hlist_for_each_entry(child, &clk->children, child_node) { |
8f2c2db1 | 1379 | child->new_rate = clk_recalc(child, new_rate); |
71472c0c | 1380 | clk_calc_subtree(child, child->new_rate, NULL, 0); |
b2476490 MT |
1381 | } |
1382 | } | |
1383 | ||
1384 | /* | |
1385 | * calculate the new rates returning the topmost clock that has to be | |
1386 | * changed. | |
1387 | */ | |
1388 | static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate) | |
1389 | { | |
1390 | struct clk *top = clk; | |
71472c0c | 1391 | struct clk *old_parent, *parent; |
81536e07 | 1392 | unsigned long best_parent_rate = 0; |
b2476490 | 1393 | unsigned long new_rate; |
f1c8b2ed | 1394 | int p_index = 0; |
b2476490 | 1395 | |
7452b219 MT |
1396 | /* sanity */ |
1397 | if (IS_ERR_OR_NULL(clk)) | |
1398 | return NULL; | |
1399 | ||
63f5c3b2 | 1400 | /* save parent rate, if it exists */ |
71472c0c JH |
1401 | parent = old_parent = clk->parent; |
1402 | if (parent) | |
1403 | best_parent_rate = parent->rate; | |
1404 | ||
1405 | /* find the closest rate and parent clk/rate */ | |
1406 | if (clk->ops->determine_rate) { | |
1407 | new_rate = clk->ops->determine_rate(clk->hw, rate, | |
1408 | &best_parent_rate, | |
1409 | &parent); | |
1410 | } else if (clk->ops->round_rate) { | |
1411 | new_rate = clk->ops->round_rate(clk->hw, rate, | |
1412 | &best_parent_rate); | |
1413 | } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) { | |
1414 | /* pass-through clock without adjustable parent */ | |
1415 | clk->new_rate = clk->rate; | |
1416 | return NULL; | |
1417 | } else { | |
1418 | /* pass-through clock with adjustable parent */ | |
1419 | top = clk_calc_new_rates(parent, rate); | |
1420 | new_rate = parent->new_rate; | |
63f5c3b2 | 1421 | goto out; |
7452b219 MT |
1422 | } |
1423 | ||
71472c0c JH |
1424 | /* some clocks must be gated to change parent */ |
1425 | if (parent != old_parent && | |
1426 | (clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) { | |
1427 | pr_debug("%s: %s not gated but wants to reparent\n", | |
1428 | __func__, clk->name); | |
b2476490 MT |
1429 | return NULL; |
1430 | } | |
1431 | ||
71472c0c JH |
1432 | /* try finding the new parent index */ |
1433 | if (parent) { | |
1434 | p_index = clk_fetch_parent_index(clk, parent); | |
f1c8b2ed | 1435 | if (p_index < 0) { |
71472c0c JH |
1436 | pr_debug("%s: clk %s can not be parent of clk %s\n", |
1437 | __func__, parent->name, clk->name); | |
1438 | return NULL; | |
1439 | } | |
b2476490 MT |
1440 | } |
1441 | ||
71472c0c JH |
1442 | if ((clk->flags & CLK_SET_RATE_PARENT) && parent && |
1443 | best_parent_rate != parent->rate) | |
1444 | top = clk_calc_new_rates(parent, best_parent_rate); | |
b2476490 MT |
1445 | |
1446 | out: | |
71472c0c | 1447 | clk_calc_subtree(clk, new_rate, parent, p_index); |
b2476490 MT |
1448 | |
1449 | return top; | |
1450 | } | |
1451 | ||
1452 | /* | |
1453 | * Notify about rate changes in a subtree. Always walk down the whole tree | |
1454 | * so that in case of an error we can walk down the whole tree again and | |
1455 | * abort the change. | |
1456 | */ | |
1457 | static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event) | |
1458 | { | |
71472c0c | 1459 | struct clk *child, *tmp_clk, *fail_clk = NULL; |
b2476490 MT |
1460 | int ret = NOTIFY_DONE; |
1461 | ||
1462 | if (clk->rate == clk->new_rate) | |
5fda6858 | 1463 | return NULL; |
b2476490 MT |
1464 | |
1465 | if (clk->notifier_count) { | |
1466 | ret = __clk_notify(clk, event, clk->rate, clk->new_rate); | |
fb72a059 | 1467 | if (ret & NOTIFY_STOP_MASK) |
b2476490 MT |
1468 | fail_clk = clk; |
1469 | } | |
1470 | ||
b67bfe0d | 1471 | hlist_for_each_entry(child, &clk->children, child_node) { |
71472c0c JH |
1472 | /* Skip children who will be reparented to another clock */ |
1473 | if (child->new_parent && child->new_parent != clk) | |
1474 | continue; | |
1475 | tmp_clk = clk_propagate_rate_change(child, event); | |
1476 | if (tmp_clk) | |
1477 | fail_clk = tmp_clk; | |
1478 | } | |
1479 | ||
1480 | /* handle the new child who might not be in clk->children yet */ | |
1481 | if (clk->new_child) { | |
1482 | tmp_clk = clk_propagate_rate_change(clk->new_child, event); | |
1483 | if (tmp_clk) | |
1484 | fail_clk = tmp_clk; | |
b2476490 MT |
1485 | } |
1486 | ||
1487 | return fail_clk; | |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * walk down a subtree and set the new rates notifying the rate | |
1492 | * change on the way | |
1493 | */ | |
1494 | static void clk_change_rate(struct clk *clk) | |
1495 | { | |
1496 | struct clk *child; | |
1497 | unsigned long old_rate; | |
bf47b4fd | 1498 | unsigned long best_parent_rate = 0; |
3fa2252b SB |
1499 | bool skip_set_rate = false; |
1500 | struct clk *old_parent; | |
b2476490 MT |
1501 | |
1502 | old_rate = clk->rate; | |
1503 | ||
3fa2252b SB |
1504 | if (clk->new_parent) |
1505 | best_parent_rate = clk->new_parent->rate; | |
1506 | else if (clk->parent) | |
bf47b4fd PM |
1507 | best_parent_rate = clk->parent->rate; |
1508 | ||
3fa2252b SB |
1509 | if (clk->new_parent && clk->new_parent != clk->parent) { |
1510 | old_parent = __clk_set_parent_before(clk, clk->new_parent); | |
1511 | ||
1512 | if (clk->ops->set_rate_and_parent) { | |
1513 | skip_set_rate = true; | |
1514 | clk->ops->set_rate_and_parent(clk->hw, clk->new_rate, | |
1515 | best_parent_rate, | |
1516 | clk->new_parent_index); | |
1517 | } else if (clk->ops->set_parent) { | |
1518 | clk->ops->set_parent(clk->hw, clk->new_parent_index); | |
1519 | } | |
1520 | ||
1521 | __clk_set_parent_after(clk, clk->new_parent, old_parent); | |
1522 | } | |
1523 | ||
1524 | if (!skip_set_rate && clk->ops->set_rate) | |
bf47b4fd | 1525 | clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate); |
b2476490 | 1526 | |
8f2c2db1 | 1527 | clk->rate = clk_recalc(clk, best_parent_rate); |
b2476490 MT |
1528 | |
1529 | if (clk->notifier_count && old_rate != clk->rate) | |
1530 | __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate); | |
1531 | ||
71472c0c JH |
1532 | hlist_for_each_entry(child, &clk->children, child_node) { |
1533 | /* Skip children who will be reparented to another clock */ | |
1534 | if (child->new_parent && child->new_parent != clk) | |
1535 | continue; | |
b2476490 | 1536 | clk_change_rate(child); |
71472c0c JH |
1537 | } |
1538 | ||
1539 | /* handle the new child who might not be in clk->children yet */ | |
1540 | if (clk->new_child) | |
1541 | clk_change_rate(clk->new_child); | |
b2476490 MT |
1542 | } |
1543 | ||
1544 | /** | |
1545 | * clk_set_rate - specify a new rate for clk | |
1546 | * @clk: the clk whose rate is being changed | |
1547 | * @rate: the new rate for clk | |
1548 | * | |
5654dc94 | 1549 | * In the simplest case clk_set_rate will only adjust the rate of clk. |
b2476490 | 1550 | * |
5654dc94 MT |
1551 | * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to |
1552 | * propagate up to clk's parent; whether or not this happens depends on the | |
1553 | * outcome of clk's .round_rate implementation. If *parent_rate is unchanged | |
1554 | * after calling .round_rate then upstream parent propagation is ignored. If | |
1555 | * *parent_rate comes back with a new rate for clk's parent then we propagate | |
24ee1a08 | 1556 | * up to clk's parent and set its rate. Upward propagation will continue |
5654dc94 MT |
1557 | * until either a clk does not support the CLK_SET_RATE_PARENT flag or |
1558 | * .round_rate stops requesting changes to clk's parent_rate. | |
b2476490 | 1559 | * |
5654dc94 MT |
1560 | * Rate changes are accomplished via tree traversal that also recalculates the |
1561 | * rates for the clocks and fires off POST_RATE_CHANGE notifiers. | |
b2476490 MT |
1562 | * |
1563 | * Returns 0 on success, -EERROR otherwise. | |
1564 | */ | |
1565 | int clk_set_rate(struct clk *clk, unsigned long rate) | |
1566 | { | |
1567 | struct clk *top, *fail_clk; | |
1568 | int ret = 0; | |
1569 | ||
89ac8d7a MT |
1570 | if (!clk) |
1571 | return 0; | |
1572 | ||
b2476490 | 1573 | /* prevent racing with updates to the clock topology */ |
eab89f69 | 1574 | clk_prepare_lock(); |
b2476490 MT |
1575 | |
1576 | /* bail early if nothing to do */ | |
34e452a1 | 1577 | if (rate == clk_get_rate(clk)) |
b2476490 MT |
1578 | goto out; |
1579 | ||
7e0fa1b5 | 1580 | if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) { |
0e1c0301 VK |
1581 | ret = -EBUSY; |
1582 | goto out; | |
1583 | } | |
1584 | ||
b2476490 MT |
1585 | /* calculate new rates and get the topmost changed clock */ |
1586 | top = clk_calc_new_rates(clk, rate); | |
1587 | if (!top) { | |
1588 | ret = -EINVAL; | |
1589 | goto out; | |
1590 | } | |
1591 | ||
1592 | /* notify that we are about to change rates */ | |
1593 | fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); | |
1594 | if (fail_clk) { | |
f7363861 | 1595 | pr_debug("%s: failed to set %s rate\n", __func__, |
b2476490 MT |
1596 | fail_clk->name); |
1597 | clk_propagate_rate_change(top, ABORT_RATE_CHANGE); | |
1598 | ret = -EBUSY; | |
1599 | goto out; | |
1600 | } | |
1601 | ||
1602 | /* change the rates */ | |
1603 | clk_change_rate(top); | |
1604 | ||
b2476490 | 1605 | out: |
eab89f69 | 1606 | clk_prepare_unlock(); |
b2476490 MT |
1607 | |
1608 | return ret; | |
1609 | } | |
1610 | EXPORT_SYMBOL_GPL(clk_set_rate); | |
1611 | ||
1612 | /** | |
1613 | * clk_get_parent - return the parent of a clk | |
1614 | * @clk: the clk whose parent gets returned | |
1615 | * | |
1616 | * Simply returns clk->parent. Returns NULL if clk is NULL. | |
1617 | */ | |
1618 | struct clk *clk_get_parent(struct clk *clk) | |
1619 | { | |
1620 | struct clk *parent; | |
1621 | ||
eab89f69 | 1622 | clk_prepare_lock(); |
b2476490 | 1623 | parent = __clk_get_parent(clk); |
eab89f69 | 1624 | clk_prepare_unlock(); |
b2476490 MT |
1625 | |
1626 | return parent; | |
1627 | } | |
1628 | EXPORT_SYMBOL_GPL(clk_get_parent); | |
1629 | ||
1630 | /* | |
1631 | * .get_parent is mandatory for clocks with multiple possible parents. It is | |
1632 | * optional for single-parent clocks. Always call .get_parent if it is | |
1633 | * available and WARN if it is missing for multi-parent clocks. | |
1634 | * | |
1635 | * For single-parent clocks without .get_parent, first check to see if the | |
1636 | * .parents array exists, and if so use it to avoid an expensive tree | |
1637 | * traversal. If .parents does not exist then walk the tree with __clk_lookup. | |
1638 | */ | |
1639 | static struct clk *__clk_init_parent(struct clk *clk) | |
1640 | { | |
1641 | struct clk *ret = NULL; | |
1642 | u8 index; | |
1643 | ||
1644 | /* handle the trivial cases */ | |
1645 | ||
1646 | if (!clk->num_parents) | |
1647 | goto out; | |
1648 | ||
1649 | if (clk->num_parents == 1) { | |
1650 | if (IS_ERR_OR_NULL(clk->parent)) | |
1651 | ret = clk->parent = __clk_lookup(clk->parent_names[0]); | |
1652 | ret = clk->parent; | |
1653 | goto out; | |
1654 | } | |
1655 | ||
1656 | if (!clk->ops->get_parent) { | |
1657 | WARN(!clk->ops->get_parent, | |
1658 | "%s: multi-parent clocks must implement .get_parent\n", | |
1659 | __func__); | |
1660 | goto out; | |
1661 | }; | |
1662 | ||
1663 | /* | |
1664 | * Do our best to cache parent clocks in clk->parents. This prevents | |
1665 | * unnecessary and expensive calls to __clk_lookup. We don't set | |
1666 | * clk->parent here; that is done by the calling function | |
1667 | */ | |
1668 | ||
1669 | index = clk->ops->get_parent(clk->hw); | |
1670 | ||
1671 | if (!clk->parents) | |
1672 | clk->parents = | |
96a7ed90 | 1673 | kcalloc(clk->num_parents, sizeof(struct clk *), |
b2476490 MT |
1674 | GFP_KERNEL); |
1675 | ||
7ef3dcc8 | 1676 | ret = clk_get_parent_by_index(clk, index); |
b2476490 MT |
1677 | |
1678 | out: | |
1679 | return ret; | |
1680 | } | |
1681 | ||
b33d212f UH |
1682 | void __clk_reparent(struct clk *clk, struct clk *new_parent) |
1683 | { | |
1684 | clk_reparent(clk, new_parent); | |
1685 | clk_debug_reparent(clk, new_parent); | |
5279fc40 | 1686 | __clk_recalc_accuracies(clk); |
b2476490 MT |
1687 | __clk_recalc_rates(clk, POST_RATE_CHANGE); |
1688 | } | |
1689 | ||
b2476490 MT |
1690 | /** |
1691 | * clk_set_parent - switch the parent of a mux clk | |
1692 | * @clk: the mux clk whose input we are switching | |
1693 | * @parent: the new input to clk | |
1694 | * | |
f8aa0bd5 SK |
1695 | * Re-parent clk to use parent as its new input source. If clk is in |
1696 | * prepared state, the clk will get enabled for the duration of this call. If | |
1697 | * that's not acceptable for a specific clk (Eg: the consumer can't handle | |
1698 | * that, the reparenting is glitchy in hardware, etc), use the | |
1699 | * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared. | |
1700 | * | |
1701 | * After successfully changing clk's parent clk_set_parent will update the | |
1702 | * clk topology, sysfs topology and propagate rate recalculation via | |
1703 | * __clk_recalc_rates. | |
1704 | * | |
1705 | * Returns 0 on success, -EERROR otherwise. | |
b2476490 MT |
1706 | */ |
1707 | int clk_set_parent(struct clk *clk, struct clk *parent) | |
1708 | { | |
1709 | int ret = 0; | |
f1c8b2ed | 1710 | int p_index = 0; |
031dcc9b | 1711 | unsigned long p_rate = 0; |
b2476490 | 1712 | |
89ac8d7a MT |
1713 | if (!clk) |
1714 | return 0; | |
1715 | ||
031dcc9b UH |
1716 | /* verify ops for for multi-parent clks */ |
1717 | if ((clk->num_parents > 1) && (!clk->ops->set_parent)) | |
b2476490 MT |
1718 | return -ENOSYS; |
1719 | ||
1720 | /* prevent racing with updates to the clock topology */ | |
eab89f69 | 1721 | clk_prepare_lock(); |
b2476490 MT |
1722 | |
1723 | if (clk->parent == parent) | |
1724 | goto out; | |
1725 | ||
031dcc9b UH |
1726 | /* check that we are allowed to re-parent if the clock is in use */ |
1727 | if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count) { | |
1728 | ret = -EBUSY; | |
1729 | goto out; | |
1730 | } | |
1731 | ||
1732 | /* try finding the new parent index */ | |
1733 | if (parent) { | |
1734 | p_index = clk_fetch_parent_index(clk, parent); | |
1735 | p_rate = parent->rate; | |
f1c8b2ed | 1736 | if (p_index < 0) { |
031dcc9b UH |
1737 | pr_debug("%s: clk %s can not be parent of clk %s\n", |
1738 | __func__, parent->name, clk->name); | |
f1c8b2ed | 1739 | ret = p_index; |
031dcc9b UH |
1740 | goto out; |
1741 | } | |
1742 | } | |
1743 | ||
b2476490 | 1744 | /* propagate PRE_RATE_CHANGE notifications */ |
f3aab5d6 | 1745 | ret = __clk_speculate_rates(clk, p_rate); |
b2476490 MT |
1746 | |
1747 | /* abort if a driver objects */ | |
fb72a059 | 1748 | if (ret & NOTIFY_STOP_MASK) |
b2476490 MT |
1749 | goto out; |
1750 | ||
031dcc9b UH |
1751 | /* do the re-parent */ |
1752 | ret = __clk_set_parent(clk, parent, p_index); | |
b2476490 | 1753 | |
5279fc40 BB |
1754 | /* propagate rate an accuracy recalculation accordingly */ |
1755 | if (ret) { | |
b2476490 | 1756 | __clk_recalc_rates(clk, ABORT_RATE_CHANGE); |
5279fc40 | 1757 | } else { |
a68de8e4 | 1758 | __clk_recalc_rates(clk, POST_RATE_CHANGE); |
5279fc40 BB |
1759 | __clk_recalc_accuracies(clk); |
1760 | } | |
b2476490 MT |
1761 | |
1762 | out: | |
eab89f69 | 1763 | clk_prepare_unlock(); |
b2476490 MT |
1764 | |
1765 | return ret; | |
1766 | } | |
1767 | EXPORT_SYMBOL_GPL(clk_set_parent); | |
1768 | ||
1769 | /** | |
1770 | * __clk_init - initialize the data structures in a struct clk | |
1771 | * @dev: device initializing this clk, placeholder for now | |
1772 | * @clk: clk being initialized | |
1773 | * | |
1774 | * Initializes the lists in struct clk, queries the hardware for the | |
1775 | * parent and rate and sets them both. | |
b2476490 | 1776 | */ |
d1302a36 | 1777 | int __clk_init(struct device *dev, struct clk *clk) |
b2476490 | 1778 | { |
d1302a36 | 1779 | int i, ret = 0; |
b2476490 | 1780 | struct clk *orphan; |
b67bfe0d | 1781 | struct hlist_node *tmp2; |
b2476490 MT |
1782 | |
1783 | if (!clk) | |
d1302a36 | 1784 | return -EINVAL; |
b2476490 | 1785 | |
eab89f69 | 1786 | clk_prepare_lock(); |
b2476490 MT |
1787 | |
1788 | /* check to see if a clock with this name is already registered */ | |
d1302a36 MT |
1789 | if (__clk_lookup(clk->name)) { |
1790 | pr_debug("%s: clk %s already initialized\n", | |
1791 | __func__, clk->name); | |
1792 | ret = -EEXIST; | |
b2476490 | 1793 | goto out; |
d1302a36 | 1794 | } |
b2476490 | 1795 | |
d4d7e3dd MT |
1796 | /* check that clk_ops are sane. See Documentation/clk.txt */ |
1797 | if (clk->ops->set_rate && | |
71472c0c JH |
1798 | !((clk->ops->round_rate || clk->ops->determine_rate) && |
1799 | clk->ops->recalc_rate)) { | |
1800 | pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n", | |
d4d7e3dd | 1801 | __func__, clk->name); |
d1302a36 | 1802 | ret = -EINVAL; |
d4d7e3dd MT |
1803 | goto out; |
1804 | } | |
1805 | ||
1806 | if (clk->ops->set_parent && !clk->ops->get_parent) { | |
1807 | pr_warning("%s: %s must implement .get_parent & .set_parent\n", | |
1808 | __func__, clk->name); | |
d1302a36 | 1809 | ret = -EINVAL; |
d4d7e3dd MT |
1810 | goto out; |
1811 | } | |
1812 | ||
3fa2252b SB |
1813 | if (clk->ops->set_rate_and_parent && |
1814 | !(clk->ops->set_parent && clk->ops->set_rate)) { | |
1815 | pr_warn("%s: %s must implement .set_parent & .set_rate\n", | |
1816 | __func__, clk->name); | |
1817 | ret = -EINVAL; | |
1818 | goto out; | |
1819 | } | |
1820 | ||
b2476490 MT |
1821 | /* throw a WARN if any entries in parent_names are NULL */ |
1822 | for (i = 0; i < clk->num_parents; i++) | |
1823 | WARN(!clk->parent_names[i], | |
1824 | "%s: invalid NULL in %s's .parent_names\n", | |
1825 | __func__, clk->name); | |
1826 | ||
1827 | /* | |
1828 | * Allocate an array of struct clk *'s to avoid unnecessary string | |
1829 | * look-ups of clk's possible parents. This can fail for clocks passed | |
1830 | * in to clk_init during early boot; thus any access to clk->parents[] | |
1831 | * must always check for a NULL pointer and try to populate it if | |
1832 | * necessary. | |
1833 | * | |
1834 | * If clk->parents is not NULL we skip this entire block. This allows | |
1835 | * for clock drivers to statically initialize clk->parents. | |
1836 | */ | |
9ca1c5a4 | 1837 | if (clk->num_parents > 1 && !clk->parents) { |
96a7ed90 TF |
1838 | clk->parents = kcalloc(clk->num_parents, sizeof(struct clk *), |
1839 | GFP_KERNEL); | |
b2476490 MT |
1840 | /* |
1841 | * __clk_lookup returns NULL for parents that have not been | |
1842 | * clk_init'd; thus any access to clk->parents[] must check | |
1843 | * for a NULL pointer. We can always perform lazy lookups for | |
1844 | * missing parents later on. | |
1845 | */ | |
1846 | if (clk->parents) | |
1847 | for (i = 0; i < clk->num_parents; i++) | |
1848 | clk->parents[i] = | |
1849 | __clk_lookup(clk->parent_names[i]); | |
1850 | } | |
1851 | ||
1852 | clk->parent = __clk_init_parent(clk); | |
1853 | ||
1854 | /* | |
1855 | * Populate clk->parent if parent has already been __clk_init'd. If | |
1856 | * parent has not yet been __clk_init'd then place clk in the orphan | |
1857 | * list. If clk has set the CLK_IS_ROOT flag then place it in the root | |
1858 | * clk list. | |
1859 | * | |
1860 | * Every time a new clk is clk_init'd then we walk the list of orphan | |
1861 | * clocks and re-parent any that are children of the clock currently | |
1862 | * being clk_init'd. | |
1863 | */ | |
1864 | if (clk->parent) | |
1865 | hlist_add_head(&clk->child_node, | |
1866 | &clk->parent->children); | |
1867 | else if (clk->flags & CLK_IS_ROOT) | |
1868 | hlist_add_head(&clk->child_node, &clk_root_list); | |
1869 | else | |
1870 | hlist_add_head(&clk->child_node, &clk_orphan_list); | |
1871 | ||
5279fc40 BB |
1872 | /* |
1873 | * Set clk's accuracy. The preferred method is to use | |
1874 | * .recalc_accuracy. For simple clocks and lazy developers the default | |
1875 | * fallback is to use the parent's accuracy. If a clock doesn't have a | |
1876 | * parent (or is orphaned) then accuracy is set to zero (perfect | |
1877 | * clock). | |
1878 | */ | |
1879 | if (clk->ops->recalc_accuracy) | |
1880 | clk->accuracy = clk->ops->recalc_accuracy(clk->hw, | |
1881 | __clk_get_accuracy(clk->parent)); | |
1882 | else if (clk->parent) | |
1883 | clk->accuracy = clk->parent->accuracy; | |
1884 | else | |
1885 | clk->accuracy = 0; | |
1886 | ||
b2476490 MT |
1887 | /* |
1888 | * Set clk's rate. The preferred method is to use .recalc_rate. For | |
1889 | * simple clocks and lazy developers the default fallback is to use the | |
1890 | * parent's rate. If a clock doesn't have a parent (or is orphaned) | |
1891 | * then rate is set to zero. | |
1892 | */ | |
1893 | if (clk->ops->recalc_rate) | |
1894 | clk->rate = clk->ops->recalc_rate(clk->hw, | |
1895 | __clk_get_rate(clk->parent)); | |
1896 | else if (clk->parent) | |
1897 | clk->rate = clk->parent->rate; | |
1898 | else | |
1899 | clk->rate = 0; | |
1900 | ||
3a5aec24 | 1901 | clk_debug_register(clk); |
b2476490 MT |
1902 | /* |
1903 | * walk the list of orphan clocks and reparent any that are children of | |
1904 | * this clock | |
1905 | */ | |
b67bfe0d | 1906 | hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) { |
12d29886 | 1907 | if (orphan->num_parents && orphan->ops->get_parent) { |
1f61e5f1 MF |
1908 | i = orphan->ops->get_parent(orphan->hw); |
1909 | if (!strcmp(clk->name, orphan->parent_names[i])) | |
1910 | __clk_reparent(orphan, clk); | |
1911 | continue; | |
1912 | } | |
1913 | ||
b2476490 MT |
1914 | for (i = 0; i < orphan->num_parents; i++) |
1915 | if (!strcmp(clk->name, orphan->parent_names[i])) { | |
1916 | __clk_reparent(orphan, clk); | |
1917 | break; | |
1918 | } | |
1f61e5f1 | 1919 | } |
b2476490 MT |
1920 | |
1921 | /* | |
1922 | * optional platform-specific magic | |
1923 | * | |
1924 | * The .init callback is not used by any of the basic clock types, but | |
1925 | * exists for weird hardware that must perform initialization magic. | |
1926 | * Please consider other ways of solving initialization problems before | |
24ee1a08 | 1927 | * using this callback, as its use is discouraged. |
b2476490 MT |
1928 | */ |
1929 | if (clk->ops->init) | |
1930 | clk->ops->init(clk->hw); | |
1931 | ||
fcb0ee6a | 1932 | kref_init(&clk->ref); |
b2476490 | 1933 | out: |
eab89f69 | 1934 | clk_prepare_unlock(); |
b2476490 | 1935 | |
d1302a36 | 1936 | return ret; |
b2476490 MT |
1937 | } |
1938 | ||
0197b3ea SK |
1939 | /** |
1940 | * __clk_register - register a clock and return a cookie. | |
1941 | * | |
1942 | * Same as clk_register, except that the .clk field inside hw shall point to a | |
1943 | * preallocated (generally statically allocated) struct clk. None of the fields | |
1944 | * of the struct clk need to be initialized. | |
1945 | * | |
1946 | * The data pointed to by .init and .clk field shall NOT be marked as init | |
1947 | * data. | |
1948 | * | |
1949 | * __clk_register is only exposed via clk-private.h and is intended for use with | |
1950 | * very large numbers of clocks that need to be statically initialized. It is | |
1951 | * a layering violation to include clk-private.h from any code which implements | |
1952 | * a clock's .ops; as such any statically initialized clock data MUST be in a | |
24ee1a08 | 1953 | * separate C file from the logic that implements its operations. Returns 0 |
0197b3ea SK |
1954 | * on success, otherwise an error code. |
1955 | */ | |
1956 | struct clk *__clk_register(struct device *dev, struct clk_hw *hw) | |
1957 | { | |
1958 | int ret; | |
1959 | struct clk *clk; | |
1960 | ||
1961 | clk = hw->clk; | |
1962 | clk->name = hw->init->name; | |
1963 | clk->ops = hw->init->ops; | |
1964 | clk->hw = hw; | |
1965 | clk->flags = hw->init->flags; | |
1966 | clk->parent_names = hw->init->parent_names; | |
1967 | clk->num_parents = hw->init->num_parents; | |
ac2df527 SN |
1968 | if (dev && dev->driver) |
1969 | clk->owner = dev->driver->owner; | |
1970 | else | |
1971 | clk->owner = NULL; | |
0197b3ea SK |
1972 | |
1973 | ret = __clk_init(dev, clk); | |
1974 | if (ret) | |
1975 | return ERR_PTR(ret); | |
1976 | ||
1977 | return clk; | |
1978 | } | |
1979 | EXPORT_SYMBOL_GPL(__clk_register); | |
1980 | ||
293ba3b4 SB |
1981 | /** |
1982 | * clk_register - allocate a new clock, register it and return an opaque cookie | |
1983 | * @dev: device that is registering this clock | |
1984 | * @hw: link to hardware-specific clock data | |
1985 | * | |
1986 | * clk_register is the primary interface for populating the clock tree with new | |
1987 | * clock nodes. It returns a pointer to the newly allocated struct clk which | |
1988 | * cannot be dereferenced by driver code but may be used in conjuction with the | |
1989 | * rest of the clock API. In the event of an error clk_register will return an | |
1990 | * error code; drivers must test for an error code after calling clk_register. | |
1991 | */ | |
1992 | struct clk *clk_register(struct device *dev, struct clk_hw *hw) | |
b2476490 | 1993 | { |
d1302a36 | 1994 | int i, ret; |
293ba3b4 SB |
1995 | struct clk *clk; |
1996 | ||
1997 | clk = kzalloc(sizeof(*clk), GFP_KERNEL); | |
1998 | if (!clk) { | |
1999 | pr_err("%s: could not allocate clk\n", __func__); | |
2000 | ret = -ENOMEM; | |
2001 | goto fail_out; | |
2002 | } | |
b2476490 | 2003 | |
0197b3ea SK |
2004 | clk->name = kstrdup(hw->init->name, GFP_KERNEL); |
2005 | if (!clk->name) { | |
2006 | pr_err("%s: could not allocate clk->name\n", __func__); | |
2007 | ret = -ENOMEM; | |
2008 | goto fail_name; | |
2009 | } | |
2010 | clk->ops = hw->init->ops; | |
ac2df527 SN |
2011 | if (dev && dev->driver) |
2012 | clk->owner = dev->driver->owner; | |
b2476490 | 2013 | clk->hw = hw; |
0197b3ea SK |
2014 | clk->flags = hw->init->flags; |
2015 | clk->num_parents = hw->init->num_parents; | |
b2476490 MT |
2016 | hw->clk = clk; |
2017 | ||
d1302a36 | 2018 | /* allocate local copy in case parent_names is __initdata */ |
96a7ed90 TF |
2019 | clk->parent_names = kcalloc(clk->num_parents, sizeof(char *), |
2020 | GFP_KERNEL); | |
d1302a36 MT |
2021 | |
2022 | if (!clk->parent_names) { | |
2023 | pr_err("%s: could not allocate clk->parent_names\n", __func__); | |
2024 | ret = -ENOMEM; | |
2025 | goto fail_parent_names; | |
2026 | } | |
2027 | ||
2028 | ||
2029 | /* copy each string name in case parent_names is __initdata */ | |
0197b3ea SK |
2030 | for (i = 0; i < clk->num_parents; i++) { |
2031 | clk->parent_names[i] = kstrdup(hw->init->parent_names[i], | |
2032 | GFP_KERNEL); | |
d1302a36 MT |
2033 | if (!clk->parent_names[i]) { |
2034 | pr_err("%s: could not copy parent_names\n", __func__); | |
2035 | ret = -ENOMEM; | |
2036 | goto fail_parent_names_copy; | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | ret = __clk_init(dev, clk); | |
2041 | if (!ret) | |
293ba3b4 | 2042 | return clk; |
b2476490 | 2043 | |
d1302a36 MT |
2044 | fail_parent_names_copy: |
2045 | while (--i >= 0) | |
2046 | kfree(clk->parent_names[i]); | |
2047 | kfree(clk->parent_names); | |
2048 | fail_parent_names: | |
0197b3ea SK |
2049 | kfree(clk->name); |
2050 | fail_name: | |
d1302a36 MT |
2051 | kfree(clk); |
2052 | fail_out: | |
2053 | return ERR_PTR(ret); | |
b2476490 MT |
2054 | } |
2055 | EXPORT_SYMBOL_GPL(clk_register); | |
2056 | ||
fcb0ee6a SN |
2057 | /* |
2058 | * Free memory allocated for a clock. | |
2059 | * Caller must hold prepare_lock. | |
2060 | */ | |
2061 | static void __clk_release(struct kref *ref) | |
2062 | { | |
2063 | struct clk *clk = container_of(ref, struct clk, ref); | |
2064 | int i = clk->num_parents; | |
2065 | ||
2066 | kfree(clk->parents); | |
2067 | while (--i >= 0) | |
2068 | kfree(clk->parent_names[i]); | |
2069 | ||
2070 | kfree(clk->parent_names); | |
2071 | kfree(clk->name); | |
2072 | kfree(clk); | |
2073 | } | |
2074 | ||
2075 | /* | |
2076 | * Empty clk_ops for unregistered clocks. These are used temporarily | |
2077 | * after clk_unregister() was called on a clock and until last clock | |
2078 | * consumer calls clk_put() and the struct clk object is freed. | |
2079 | */ | |
2080 | static int clk_nodrv_prepare_enable(struct clk_hw *hw) | |
2081 | { | |
2082 | return -ENXIO; | |
2083 | } | |
2084 | ||
2085 | static void clk_nodrv_disable_unprepare(struct clk_hw *hw) | |
2086 | { | |
2087 | WARN_ON_ONCE(1); | |
2088 | } | |
2089 | ||
2090 | static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate, | |
2091 | unsigned long parent_rate) | |
2092 | { | |
2093 | return -ENXIO; | |
2094 | } | |
2095 | ||
2096 | static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index) | |
2097 | { | |
2098 | return -ENXIO; | |
2099 | } | |
2100 | ||
2101 | static const struct clk_ops clk_nodrv_ops = { | |
2102 | .enable = clk_nodrv_prepare_enable, | |
2103 | .disable = clk_nodrv_disable_unprepare, | |
2104 | .prepare = clk_nodrv_prepare_enable, | |
2105 | .unprepare = clk_nodrv_disable_unprepare, | |
2106 | .set_rate = clk_nodrv_set_rate, | |
2107 | .set_parent = clk_nodrv_set_parent, | |
2108 | }; | |
2109 | ||
1df5c939 MB |
2110 | /** |
2111 | * clk_unregister - unregister a currently registered clock | |
2112 | * @clk: clock to unregister | |
1df5c939 | 2113 | */ |
fcb0ee6a SN |
2114 | void clk_unregister(struct clk *clk) |
2115 | { | |
2116 | unsigned long flags; | |
2117 | ||
2118 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) | |
2119 | return; | |
2120 | ||
2121 | clk_prepare_lock(); | |
2122 | ||
2123 | if (clk->ops == &clk_nodrv_ops) { | |
2124 | pr_err("%s: unregistered clock: %s\n", __func__, clk->name); | |
2125 | goto out; | |
2126 | } | |
2127 | /* | |
2128 | * Assign empty clock ops for consumers that might still hold | |
2129 | * a reference to this clock. | |
2130 | */ | |
2131 | flags = clk_enable_lock(); | |
2132 | clk->ops = &clk_nodrv_ops; | |
2133 | clk_enable_unlock(flags); | |
2134 | ||
2135 | if (!hlist_empty(&clk->children)) { | |
2136 | struct clk *child; | |
874f224c | 2137 | struct hlist_node *t; |
fcb0ee6a SN |
2138 | |
2139 | /* Reparent all children to the orphan list. */ | |
874f224c | 2140 | hlist_for_each_entry_safe(child, t, &clk->children, child_node) |
fcb0ee6a SN |
2141 | clk_set_parent(child, NULL); |
2142 | } | |
2143 | ||
2144 | clk_debug_unregister(clk); | |
2145 | ||
2146 | hlist_del_init(&clk->child_node); | |
2147 | ||
2148 | if (clk->prepare_count) | |
2149 | pr_warn("%s: unregistering prepared clock: %s\n", | |
2150 | __func__, clk->name); | |
2151 | ||
2152 | kref_put(&clk->ref, __clk_release); | |
2153 | out: | |
2154 | clk_prepare_unlock(); | |
2155 | } | |
1df5c939 MB |
2156 | EXPORT_SYMBOL_GPL(clk_unregister); |
2157 | ||
46c8773a SB |
2158 | static void devm_clk_release(struct device *dev, void *res) |
2159 | { | |
293ba3b4 | 2160 | clk_unregister(*(struct clk **)res); |
46c8773a SB |
2161 | } |
2162 | ||
2163 | /** | |
2164 | * devm_clk_register - resource managed clk_register() | |
2165 | * @dev: device that is registering this clock | |
2166 | * @hw: link to hardware-specific clock data | |
2167 | * | |
2168 | * Managed clk_register(). Clocks returned from this function are | |
2169 | * automatically clk_unregister()ed on driver detach. See clk_register() for | |
2170 | * more information. | |
2171 | */ | |
2172 | struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) | |
2173 | { | |
2174 | struct clk *clk; | |
293ba3b4 | 2175 | struct clk **clkp; |
46c8773a | 2176 | |
293ba3b4 SB |
2177 | clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL); |
2178 | if (!clkp) | |
46c8773a SB |
2179 | return ERR_PTR(-ENOMEM); |
2180 | ||
293ba3b4 SB |
2181 | clk = clk_register(dev, hw); |
2182 | if (!IS_ERR(clk)) { | |
2183 | *clkp = clk; | |
2184 | devres_add(dev, clkp); | |
46c8773a | 2185 | } else { |
293ba3b4 | 2186 | devres_free(clkp); |
46c8773a SB |
2187 | } |
2188 | ||
2189 | return clk; | |
2190 | } | |
2191 | EXPORT_SYMBOL_GPL(devm_clk_register); | |
2192 | ||
2193 | static int devm_clk_match(struct device *dev, void *res, void *data) | |
2194 | { | |
2195 | struct clk *c = res; | |
2196 | if (WARN_ON(!c)) | |
2197 | return 0; | |
2198 | return c == data; | |
2199 | } | |
2200 | ||
2201 | /** | |
2202 | * devm_clk_unregister - resource managed clk_unregister() | |
2203 | * @clk: clock to unregister | |
2204 | * | |
2205 | * Deallocate a clock allocated with devm_clk_register(). Normally | |
2206 | * this function will not need to be called and the resource management | |
2207 | * code will ensure that the resource is freed. | |
2208 | */ | |
2209 | void devm_clk_unregister(struct device *dev, struct clk *clk) | |
2210 | { | |
2211 | WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); | |
2212 | } | |
2213 | EXPORT_SYMBOL_GPL(devm_clk_unregister); | |
2214 | ||
ac2df527 SN |
2215 | /* |
2216 | * clkdev helpers | |
2217 | */ | |
2218 | int __clk_get(struct clk *clk) | |
2219 | { | |
00efcb1c SN |
2220 | if (clk) { |
2221 | if (!try_module_get(clk->owner)) | |
2222 | return 0; | |
ac2df527 | 2223 | |
00efcb1c SN |
2224 | kref_get(&clk->ref); |
2225 | } | |
ac2df527 SN |
2226 | return 1; |
2227 | } | |
2228 | ||
2229 | void __clk_put(struct clk *clk) | |
2230 | { | |
00efcb1c | 2231 | if (!clk || WARN_ON_ONCE(IS_ERR(clk))) |
ac2df527 SN |
2232 | return; |
2233 | ||
fcb0ee6a SN |
2234 | clk_prepare_lock(); |
2235 | kref_put(&clk->ref, __clk_release); | |
2236 | clk_prepare_unlock(); | |
2237 | ||
00efcb1c | 2238 | module_put(clk->owner); |
ac2df527 SN |
2239 | } |
2240 | ||
b2476490 MT |
2241 | /*** clk rate change notifiers ***/ |
2242 | ||
2243 | /** | |
2244 | * clk_notifier_register - add a clk rate change notifier | |
2245 | * @clk: struct clk * to watch | |
2246 | * @nb: struct notifier_block * with callback info | |
2247 | * | |
2248 | * Request notification when clk's rate changes. This uses an SRCU | |
2249 | * notifier because we want it to block and notifier unregistrations are | |
2250 | * uncommon. The callbacks associated with the notifier must not | |
2251 | * re-enter into the clk framework by calling any top-level clk APIs; | |
2252 | * this will cause a nested prepare_lock mutex. | |
2253 | * | |
5324fda7 SB |
2254 | * In all notification cases cases (pre, post and abort rate change) the |
2255 | * original clock rate is passed to the callback via struct | |
2256 | * clk_notifier_data.old_rate and the new frequency is passed via struct | |
b2476490 MT |
2257 | * clk_notifier_data.new_rate. |
2258 | * | |
b2476490 MT |
2259 | * clk_notifier_register() must be called from non-atomic context. |
2260 | * Returns -EINVAL if called with null arguments, -ENOMEM upon | |
2261 | * allocation failure; otherwise, passes along the return value of | |
2262 | * srcu_notifier_chain_register(). | |
2263 | */ | |
2264 | int clk_notifier_register(struct clk *clk, struct notifier_block *nb) | |
2265 | { | |
2266 | struct clk_notifier *cn; | |
2267 | int ret = -ENOMEM; | |
2268 | ||
2269 | if (!clk || !nb) | |
2270 | return -EINVAL; | |
2271 | ||
eab89f69 | 2272 | clk_prepare_lock(); |
b2476490 MT |
2273 | |
2274 | /* search the list of notifiers for this clk */ | |
2275 | list_for_each_entry(cn, &clk_notifier_list, node) | |
2276 | if (cn->clk == clk) | |
2277 | break; | |
2278 | ||
2279 | /* if clk wasn't in the notifier list, allocate new clk_notifier */ | |
2280 | if (cn->clk != clk) { | |
2281 | cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL); | |
2282 | if (!cn) | |
2283 | goto out; | |
2284 | ||
2285 | cn->clk = clk; | |
2286 | srcu_init_notifier_head(&cn->notifier_head); | |
2287 | ||
2288 | list_add(&cn->node, &clk_notifier_list); | |
2289 | } | |
2290 | ||
2291 | ret = srcu_notifier_chain_register(&cn->notifier_head, nb); | |
2292 | ||
2293 | clk->notifier_count++; | |
2294 | ||
2295 | out: | |
eab89f69 | 2296 | clk_prepare_unlock(); |
b2476490 MT |
2297 | |
2298 | return ret; | |
2299 | } | |
2300 | EXPORT_SYMBOL_GPL(clk_notifier_register); | |
2301 | ||
2302 | /** | |
2303 | * clk_notifier_unregister - remove a clk rate change notifier | |
2304 | * @clk: struct clk * | |
2305 | * @nb: struct notifier_block * with callback info | |
2306 | * | |
2307 | * Request no further notification for changes to 'clk' and frees memory | |
2308 | * allocated in clk_notifier_register. | |
2309 | * | |
2310 | * Returns -EINVAL if called with null arguments; otherwise, passes | |
2311 | * along the return value of srcu_notifier_chain_unregister(). | |
2312 | */ | |
2313 | int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) | |
2314 | { | |
2315 | struct clk_notifier *cn = NULL; | |
2316 | int ret = -EINVAL; | |
2317 | ||
2318 | if (!clk || !nb) | |
2319 | return -EINVAL; | |
2320 | ||
eab89f69 | 2321 | clk_prepare_lock(); |
b2476490 MT |
2322 | |
2323 | list_for_each_entry(cn, &clk_notifier_list, node) | |
2324 | if (cn->clk == clk) | |
2325 | break; | |
2326 | ||
2327 | if (cn->clk == clk) { | |
2328 | ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); | |
2329 | ||
2330 | clk->notifier_count--; | |
2331 | ||
2332 | /* XXX the notifier code should handle this better */ | |
2333 | if (!cn->notifier_head.head) { | |
2334 | srcu_cleanup_notifier_head(&cn->notifier_head); | |
72b5322f | 2335 | list_del(&cn->node); |
b2476490 MT |
2336 | kfree(cn); |
2337 | } | |
2338 | ||
2339 | } else { | |
2340 | ret = -ENOENT; | |
2341 | } | |
2342 | ||
eab89f69 | 2343 | clk_prepare_unlock(); |
b2476490 MT |
2344 | |
2345 | return ret; | |
2346 | } | |
2347 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); | |
766e6a4e GL |
2348 | |
2349 | #ifdef CONFIG_OF | |
2350 | /** | |
2351 | * struct of_clk_provider - Clock provider registration structure | |
2352 | * @link: Entry in global list of clock providers | |
2353 | * @node: Pointer to device tree node of clock provider | |
2354 | * @get: Get clock callback. Returns NULL or a struct clk for the | |
2355 | * given clock specifier | |
2356 | * @data: context pointer to be passed into @get callback | |
2357 | */ | |
2358 | struct of_clk_provider { | |
2359 | struct list_head link; | |
2360 | ||
2361 | struct device_node *node; | |
2362 | struct clk *(*get)(struct of_phandle_args *clkspec, void *data); | |
2363 | void *data; | |
2364 | }; | |
2365 | ||
f2f6c255 PG |
2366 | static const struct of_device_id __clk_of_table_sentinel |
2367 | __used __section(__clk_of_table_end); | |
2368 | ||
766e6a4e | 2369 | static LIST_HEAD(of_clk_providers); |
d6782c26 SN |
2370 | static DEFINE_MUTEX(of_clk_mutex); |
2371 | ||
2372 | /* of_clk_provider list locking helpers */ | |
2373 | void of_clk_lock(void) | |
2374 | { | |
2375 | mutex_lock(&of_clk_mutex); | |
2376 | } | |
2377 | ||
2378 | void of_clk_unlock(void) | |
2379 | { | |
2380 | mutex_unlock(&of_clk_mutex); | |
2381 | } | |
766e6a4e GL |
2382 | |
2383 | struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, | |
2384 | void *data) | |
2385 | { | |
2386 | return data; | |
2387 | } | |
2388 | EXPORT_SYMBOL_GPL(of_clk_src_simple_get); | |
2389 | ||
494bfec9 SG |
2390 | struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) |
2391 | { | |
2392 | struct clk_onecell_data *clk_data = data; | |
2393 | unsigned int idx = clkspec->args[0]; | |
2394 | ||
2395 | if (idx >= clk_data->clk_num) { | |
2396 | pr_err("%s: invalid clock index %d\n", __func__, idx); | |
2397 | return ERR_PTR(-EINVAL); | |
2398 | } | |
2399 | ||
2400 | return clk_data->clks[idx]; | |
2401 | } | |
2402 | EXPORT_SYMBOL_GPL(of_clk_src_onecell_get); | |
2403 | ||
766e6a4e GL |
2404 | /** |
2405 | * of_clk_add_provider() - Register a clock provider for a node | |
2406 | * @np: Device node pointer associated with clock provider | |
2407 | * @clk_src_get: callback for decoding clock | |
2408 | * @data: context pointer for @clk_src_get callback. | |
2409 | */ | |
2410 | int of_clk_add_provider(struct device_node *np, | |
2411 | struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, | |
2412 | void *data), | |
2413 | void *data) | |
2414 | { | |
2415 | struct of_clk_provider *cp; | |
2416 | ||
2417 | cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); | |
2418 | if (!cp) | |
2419 | return -ENOMEM; | |
2420 | ||
2421 | cp->node = of_node_get(np); | |
2422 | cp->data = data; | |
2423 | cp->get = clk_src_get; | |
2424 | ||
d6782c26 | 2425 | mutex_lock(&of_clk_mutex); |
766e6a4e | 2426 | list_add(&cp->link, &of_clk_providers); |
d6782c26 | 2427 | mutex_unlock(&of_clk_mutex); |
766e6a4e GL |
2428 | pr_debug("Added clock from %s\n", np->full_name); |
2429 | ||
2430 | return 0; | |
2431 | } | |
2432 | EXPORT_SYMBOL_GPL(of_clk_add_provider); | |
2433 | ||
2434 | /** | |
2435 | * of_clk_del_provider() - Remove a previously registered clock provider | |
2436 | * @np: Device node pointer associated with clock provider | |
2437 | */ | |
2438 | void of_clk_del_provider(struct device_node *np) | |
2439 | { | |
2440 | struct of_clk_provider *cp; | |
2441 | ||
d6782c26 | 2442 | mutex_lock(&of_clk_mutex); |
766e6a4e GL |
2443 | list_for_each_entry(cp, &of_clk_providers, link) { |
2444 | if (cp->node == np) { | |
2445 | list_del(&cp->link); | |
2446 | of_node_put(cp->node); | |
2447 | kfree(cp); | |
2448 | break; | |
2449 | } | |
2450 | } | |
d6782c26 | 2451 | mutex_unlock(&of_clk_mutex); |
766e6a4e GL |
2452 | } |
2453 | EXPORT_SYMBOL_GPL(of_clk_del_provider); | |
2454 | ||
d6782c26 | 2455 | struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec) |
766e6a4e GL |
2456 | { |
2457 | struct of_clk_provider *provider; | |
a34cd466 | 2458 | struct clk *clk = ERR_PTR(-EPROBE_DEFER); |
766e6a4e GL |
2459 | |
2460 | /* Check if we have such a provider in our array */ | |
766e6a4e GL |
2461 | list_for_each_entry(provider, &of_clk_providers, link) { |
2462 | if (provider->node == clkspec->np) | |
2463 | clk = provider->get(clkspec, provider->data); | |
2464 | if (!IS_ERR(clk)) | |
2465 | break; | |
2466 | } | |
d6782c26 SN |
2467 | |
2468 | return clk; | |
2469 | } | |
2470 | ||
2471 | struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) | |
2472 | { | |
2473 | struct clk *clk; | |
2474 | ||
2475 | mutex_lock(&of_clk_mutex); | |
2476 | clk = __of_clk_get_from_provider(clkspec); | |
2477 | mutex_unlock(&of_clk_mutex); | |
766e6a4e GL |
2478 | |
2479 | return clk; | |
2480 | } | |
2481 | ||
f6102742 MT |
2482 | int of_clk_get_parent_count(struct device_node *np) |
2483 | { | |
2484 | return of_count_phandle_with_args(np, "clocks", "#clock-cells"); | |
2485 | } | |
2486 | EXPORT_SYMBOL_GPL(of_clk_get_parent_count); | |
2487 | ||
766e6a4e GL |
2488 | const char *of_clk_get_parent_name(struct device_node *np, int index) |
2489 | { | |
2490 | struct of_phandle_args clkspec; | |
7a0fc1a3 | 2491 | struct property *prop; |
766e6a4e | 2492 | const char *clk_name; |
7a0fc1a3 BD |
2493 | const __be32 *vp; |
2494 | u32 pv; | |
766e6a4e | 2495 | int rc; |
7a0fc1a3 | 2496 | int count; |
766e6a4e GL |
2497 | |
2498 | if (index < 0) | |
2499 | return NULL; | |
2500 | ||
2501 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, | |
2502 | &clkspec); | |
2503 | if (rc) | |
2504 | return NULL; | |
2505 | ||
7a0fc1a3 BD |
2506 | index = clkspec.args_count ? clkspec.args[0] : 0; |
2507 | count = 0; | |
2508 | ||
2509 | /* if there is an indices property, use it to transfer the index | |
2510 | * specified into an array offset for the clock-output-names property. | |
2511 | */ | |
2512 | of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { | |
2513 | if (index == pv) { | |
2514 | index = count; | |
2515 | break; | |
2516 | } | |
2517 | count++; | |
2518 | } | |
2519 | ||
766e6a4e | 2520 | if (of_property_read_string_index(clkspec.np, "clock-output-names", |
7a0fc1a3 | 2521 | index, |
766e6a4e GL |
2522 | &clk_name) < 0) |
2523 | clk_name = clkspec.np->name; | |
2524 | ||
2525 | of_node_put(clkspec.np); | |
2526 | return clk_name; | |
2527 | } | |
2528 | EXPORT_SYMBOL_GPL(of_clk_get_parent_name); | |
2529 | ||
1771b10d GC |
2530 | struct clock_provider { |
2531 | of_clk_init_cb_t clk_init_cb; | |
2532 | struct device_node *np; | |
2533 | struct list_head node; | |
2534 | }; | |
2535 | ||
2536 | static LIST_HEAD(clk_provider_list); | |
2537 | ||
2538 | /* | |
2539 | * This function looks for a parent clock. If there is one, then it | |
2540 | * checks that the provider for this parent clock was initialized, in | |
2541 | * this case the parent clock will be ready. | |
2542 | */ | |
2543 | static int parent_ready(struct device_node *np) | |
2544 | { | |
2545 | int i = 0; | |
2546 | ||
2547 | while (true) { | |
2548 | struct clk *clk = of_clk_get(np, i); | |
2549 | ||
2550 | /* this parent is ready we can check the next one */ | |
2551 | if (!IS_ERR(clk)) { | |
2552 | clk_put(clk); | |
2553 | i++; | |
2554 | continue; | |
2555 | } | |
2556 | ||
2557 | /* at least one parent is not ready, we exit now */ | |
2558 | if (PTR_ERR(clk) == -EPROBE_DEFER) | |
2559 | return 0; | |
2560 | ||
2561 | /* | |
2562 | * Here we make assumption that the device tree is | |
2563 | * written correctly. So an error means that there is | |
2564 | * no more parent. As we didn't exit yet, then the | |
2565 | * previous parent are ready. If there is no clock | |
2566 | * parent, no need to wait for them, then we can | |
2567 | * consider their absence as being ready | |
2568 | */ | |
2569 | return 1; | |
2570 | } | |
2571 | } | |
2572 | ||
766e6a4e GL |
2573 | /** |
2574 | * of_clk_init() - Scan and init clock providers from the DT | |
2575 | * @matches: array of compatible values and init functions for providers. | |
2576 | * | |
1771b10d | 2577 | * This function scans the device tree for matching clock providers |
e5ca8fb4 | 2578 | * and calls their initialization functions. It also does it by trying |
1771b10d | 2579 | * to follow the dependencies. |
766e6a4e GL |
2580 | */ |
2581 | void __init of_clk_init(const struct of_device_id *matches) | |
2582 | { | |
7f7ed584 | 2583 | const struct of_device_id *match; |
766e6a4e | 2584 | struct device_node *np; |
1771b10d GC |
2585 | struct clock_provider *clk_provider, *next; |
2586 | bool is_init_done; | |
2587 | bool force = false; | |
766e6a4e | 2588 | |
f2f6c255 | 2589 | if (!matches) |
819b4861 | 2590 | matches = &__clk_of_table; |
f2f6c255 | 2591 | |
1771b10d | 2592 | /* First prepare the list of the clocks providers */ |
7f7ed584 | 2593 | for_each_matching_node_and_match(np, matches, &match) { |
1771b10d GC |
2594 | struct clock_provider *parent = |
2595 | kzalloc(sizeof(struct clock_provider), GFP_KERNEL); | |
2596 | ||
2597 | parent->clk_init_cb = match->data; | |
2598 | parent->np = np; | |
3f6d439f | 2599 | list_add_tail(&parent->node, &clk_provider_list); |
1771b10d GC |
2600 | } |
2601 | ||
2602 | while (!list_empty(&clk_provider_list)) { | |
2603 | is_init_done = false; | |
2604 | list_for_each_entry_safe(clk_provider, next, | |
2605 | &clk_provider_list, node) { | |
2606 | if (force || parent_ready(clk_provider->np)) { | |
2607 | clk_provider->clk_init_cb(clk_provider->np); | |
2608 | list_del(&clk_provider->node); | |
2609 | kfree(clk_provider); | |
2610 | is_init_done = true; | |
2611 | } | |
2612 | } | |
2613 | ||
2614 | /* | |
e5ca8fb4 | 2615 | * We didn't manage to initialize any of the |
1771b10d GC |
2616 | * remaining providers during the last loop, so now we |
2617 | * initialize all the remaining ones unconditionally | |
2618 | * in case the clock parent was not mandatory | |
2619 | */ | |
2620 | if (!is_init_done) | |
2621 | force = true; | |
2622 | ||
766e6a4e GL |
2623 | } |
2624 | } | |
2625 | #endif |