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