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