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