xref: /openbmc/linux/drivers/clk/clk.c (revision fca3aa16)
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.h>
13 #include <linux/clk-provider.h>
14 #include <linux/clk/clk-conf.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/spinlock.h>
18 #include <linux/err.h>
19 #include <linux/list.h>
20 #include <linux/slab.h>
21 #include <linux/of.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/sched.h>
26 #include <linux/clkdev.h>
27 #include <linux/stringify.h>
28 
29 #include "clk.h"
30 
31 static DEFINE_SPINLOCK(enable_lock);
32 static DEFINE_MUTEX(prepare_lock);
33 
34 static struct task_struct *prepare_owner;
35 static struct task_struct *enable_owner;
36 
37 static int prepare_refcnt;
38 static int enable_refcnt;
39 
40 static HLIST_HEAD(clk_root_list);
41 static HLIST_HEAD(clk_orphan_list);
42 static LIST_HEAD(clk_notifier_list);
43 
44 /***    private data structures    ***/
45 
46 struct clk_core {
47 	const char		*name;
48 	const struct clk_ops	*ops;
49 	struct clk_hw		*hw;
50 	struct module		*owner;
51 	struct device		*dev;
52 	struct clk_core		*parent;
53 	const char		**parent_names;
54 	struct clk_core		**parents;
55 	u8			num_parents;
56 	u8			new_parent_index;
57 	unsigned long		rate;
58 	unsigned long		req_rate;
59 	unsigned long		new_rate;
60 	struct clk_core		*new_parent;
61 	struct clk_core		*new_child;
62 	unsigned long		flags;
63 	bool			orphan;
64 	unsigned int		enable_count;
65 	unsigned int		prepare_count;
66 	unsigned int		protect_count;
67 	unsigned long		min_rate;
68 	unsigned long		max_rate;
69 	unsigned long		accuracy;
70 	int			phase;
71 	struct hlist_head	children;
72 	struct hlist_node	child_node;
73 	struct hlist_head	clks;
74 	unsigned int		notifier_count;
75 #ifdef CONFIG_DEBUG_FS
76 	struct dentry		*dentry;
77 	struct hlist_node	debug_node;
78 #endif
79 	struct kref		ref;
80 };
81 
82 #define CREATE_TRACE_POINTS
83 #include <trace/events/clk.h>
84 
85 struct clk {
86 	struct clk_core	*core;
87 	const char *dev_id;
88 	const char *con_id;
89 	unsigned long min_rate;
90 	unsigned long max_rate;
91 	unsigned int exclusive_count;
92 	struct hlist_node clks_node;
93 };
94 
95 /***           runtime pm          ***/
96 static int clk_pm_runtime_get(struct clk_core *core)
97 {
98 	int ret = 0;
99 
100 	if (!core->dev)
101 		return 0;
102 
103 	ret = pm_runtime_get_sync(core->dev);
104 	return ret < 0 ? ret : 0;
105 }
106 
107 static void clk_pm_runtime_put(struct clk_core *core)
108 {
109 	if (!core->dev)
110 		return;
111 
112 	pm_runtime_put_sync(core->dev);
113 }
114 
115 /***           locking             ***/
116 static void clk_prepare_lock(void)
117 {
118 	if (!mutex_trylock(&prepare_lock)) {
119 		if (prepare_owner == current) {
120 			prepare_refcnt++;
121 			return;
122 		}
123 		mutex_lock(&prepare_lock);
124 	}
125 	WARN_ON_ONCE(prepare_owner != NULL);
126 	WARN_ON_ONCE(prepare_refcnt != 0);
127 	prepare_owner = current;
128 	prepare_refcnt = 1;
129 }
130 
131 static void clk_prepare_unlock(void)
132 {
133 	WARN_ON_ONCE(prepare_owner != current);
134 	WARN_ON_ONCE(prepare_refcnt == 0);
135 
136 	if (--prepare_refcnt)
137 		return;
138 	prepare_owner = NULL;
139 	mutex_unlock(&prepare_lock);
140 }
141 
142 static unsigned long clk_enable_lock(void)
143 	__acquires(enable_lock)
144 {
145 	unsigned long flags;
146 
147 	/*
148 	 * On UP systems, spin_trylock_irqsave() always returns true, even if
149 	 * we already hold the lock. So, in that case, we rely only on
150 	 * reference counting.
151 	 */
152 	if (!IS_ENABLED(CONFIG_SMP) ||
153 	    !spin_trylock_irqsave(&enable_lock, flags)) {
154 		if (enable_owner == current) {
155 			enable_refcnt++;
156 			__acquire(enable_lock);
157 			if (!IS_ENABLED(CONFIG_SMP))
158 				local_save_flags(flags);
159 			return flags;
160 		}
161 		spin_lock_irqsave(&enable_lock, flags);
162 	}
163 	WARN_ON_ONCE(enable_owner != NULL);
164 	WARN_ON_ONCE(enable_refcnt != 0);
165 	enable_owner = current;
166 	enable_refcnt = 1;
167 	return flags;
168 }
169 
170 static void clk_enable_unlock(unsigned long flags)
171 	__releases(enable_lock)
172 {
173 	WARN_ON_ONCE(enable_owner != current);
174 	WARN_ON_ONCE(enable_refcnt == 0);
175 
176 	if (--enable_refcnt) {
177 		__release(enable_lock);
178 		return;
179 	}
180 	enable_owner = NULL;
181 	spin_unlock_irqrestore(&enable_lock, flags);
182 }
183 
184 static bool clk_core_rate_is_protected(struct clk_core *core)
185 {
186 	return core->protect_count;
187 }
188 
189 static bool clk_core_is_prepared(struct clk_core *core)
190 {
191 	bool ret = false;
192 
193 	/*
194 	 * .is_prepared is optional for clocks that can prepare
195 	 * fall back to software usage counter if it is missing
196 	 */
197 	if (!core->ops->is_prepared)
198 		return core->prepare_count;
199 
200 	if (!clk_pm_runtime_get(core)) {
201 		ret = core->ops->is_prepared(core->hw);
202 		clk_pm_runtime_put(core);
203 	}
204 
205 	return ret;
206 }
207 
208 static bool clk_core_is_enabled(struct clk_core *core)
209 {
210 	bool ret = false;
211 
212 	/*
213 	 * .is_enabled is only mandatory for clocks that gate
214 	 * fall back to software usage counter if .is_enabled is missing
215 	 */
216 	if (!core->ops->is_enabled)
217 		return core->enable_count;
218 
219 	/*
220 	 * Check if clock controller's device is runtime active before
221 	 * calling .is_enabled callback. If not, assume that clock is
222 	 * disabled, because we might be called from atomic context, from
223 	 * which pm_runtime_get() is not allowed.
224 	 * This function is called mainly from clk_disable_unused_subtree,
225 	 * which ensures proper runtime pm activation of controller before
226 	 * taking enable spinlock, but the below check is needed if one tries
227 	 * to call it from other places.
228 	 */
229 	if (core->dev) {
230 		pm_runtime_get_noresume(core->dev);
231 		if (!pm_runtime_active(core->dev)) {
232 			ret = false;
233 			goto done;
234 		}
235 	}
236 
237 	ret = core->ops->is_enabled(core->hw);
238 done:
239 	if (core->dev)
240 		pm_runtime_put(core->dev);
241 
242 	return ret;
243 }
244 
245 /***    helper functions   ***/
246 
247 const char *__clk_get_name(const struct clk *clk)
248 {
249 	return !clk ? NULL : clk->core->name;
250 }
251 EXPORT_SYMBOL_GPL(__clk_get_name);
252 
253 const char *clk_hw_get_name(const struct clk_hw *hw)
254 {
255 	return hw->core->name;
256 }
257 EXPORT_SYMBOL_GPL(clk_hw_get_name);
258 
259 struct clk_hw *__clk_get_hw(struct clk *clk)
260 {
261 	return !clk ? NULL : clk->core->hw;
262 }
263 EXPORT_SYMBOL_GPL(__clk_get_hw);
264 
265 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
266 {
267 	return hw->core->num_parents;
268 }
269 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
270 
271 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
272 {
273 	return hw->core->parent ? hw->core->parent->hw : NULL;
274 }
275 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
276 
277 static struct clk_core *__clk_lookup_subtree(const char *name,
278 					     struct clk_core *core)
279 {
280 	struct clk_core *child;
281 	struct clk_core *ret;
282 
283 	if (!strcmp(core->name, name))
284 		return core;
285 
286 	hlist_for_each_entry(child, &core->children, child_node) {
287 		ret = __clk_lookup_subtree(name, child);
288 		if (ret)
289 			return ret;
290 	}
291 
292 	return NULL;
293 }
294 
295 static struct clk_core *clk_core_lookup(const char *name)
296 {
297 	struct clk_core *root_clk;
298 	struct clk_core *ret;
299 
300 	if (!name)
301 		return NULL;
302 
303 	/* search the 'proper' clk tree first */
304 	hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
305 		ret = __clk_lookup_subtree(name, root_clk);
306 		if (ret)
307 			return ret;
308 	}
309 
310 	/* if not found, then search the orphan tree */
311 	hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
312 		ret = __clk_lookup_subtree(name, root_clk);
313 		if (ret)
314 			return ret;
315 	}
316 
317 	return NULL;
318 }
319 
320 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
321 							 u8 index)
322 {
323 	if (!core || index >= core->num_parents)
324 		return NULL;
325 
326 	if (!core->parents[index])
327 		core->parents[index] =
328 				clk_core_lookup(core->parent_names[index]);
329 
330 	return core->parents[index];
331 }
332 
333 struct clk_hw *
334 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
335 {
336 	struct clk_core *parent;
337 
338 	parent = clk_core_get_parent_by_index(hw->core, index);
339 
340 	return !parent ? NULL : parent->hw;
341 }
342 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
343 
344 unsigned int __clk_get_enable_count(struct clk *clk)
345 {
346 	return !clk ? 0 : clk->core->enable_count;
347 }
348 
349 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
350 {
351 	unsigned long ret;
352 
353 	if (!core) {
354 		ret = 0;
355 		goto out;
356 	}
357 
358 	ret = core->rate;
359 
360 	if (!core->num_parents)
361 		goto out;
362 
363 	if (!core->parent)
364 		ret = 0;
365 
366 out:
367 	return ret;
368 }
369 
370 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
371 {
372 	return clk_core_get_rate_nolock(hw->core);
373 }
374 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
375 
376 static unsigned long __clk_get_accuracy(struct clk_core *core)
377 {
378 	if (!core)
379 		return 0;
380 
381 	return core->accuracy;
382 }
383 
384 unsigned long __clk_get_flags(struct clk *clk)
385 {
386 	return !clk ? 0 : clk->core->flags;
387 }
388 EXPORT_SYMBOL_GPL(__clk_get_flags);
389 
390 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
391 {
392 	return hw->core->flags;
393 }
394 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
395 
396 bool clk_hw_is_prepared(const struct clk_hw *hw)
397 {
398 	return clk_core_is_prepared(hw->core);
399 }
400 
401 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
402 {
403 	return clk_core_rate_is_protected(hw->core);
404 }
405 
406 bool clk_hw_is_enabled(const struct clk_hw *hw)
407 {
408 	return clk_core_is_enabled(hw->core);
409 }
410 
411 bool __clk_is_enabled(struct clk *clk)
412 {
413 	if (!clk)
414 		return false;
415 
416 	return clk_core_is_enabled(clk->core);
417 }
418 EXPORT_SYMBOL_GPL(__clk_is_enabled);
419 
420 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
421 			   unsigned long best, unsigned long flags)
422 {
423 	if (flags & CLK_MUX_ROUND_CLOSEST)
424 		return abs(now - rate) < abs(best - rate);
425 
426 	return now <= rate && now > best;
427 }
428 
429 static int
430 clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
431 			     unsigned long flags)
432 {
433 	struct clk_core *core = hw->core, *parent, *best_parent = NULL;
434 	int i, num_parents, ret;
435 	unsigned long best = 0;
436 	struct clk_rate_request parent_req = *req;
437 
438 	/* if NO_REPARENT flag set, pass through to current parent */
439 	if (core->flags & CLK_SET_RATE_NO_REPARENT) {
440 		parent = core->parent;
441 		if (core->flags & CLK_SET_RATE_PARENT) {
442 			ret = __clk_determine_rate(parent ? parent->hw : NULL,
443 						   &parent_req);
444 			if (ret)
445 				return ret;
446 
447 			best = parent_req.rate;
448 		} else if (parent) {
449 			best = clk_core_get_rate_nolock(parent);
450 		} else {
451 			best = clk_core_get_rate_nolock(core);
452 		}
453 
454 		goto out;
455 	}
456 
457 	/* find the parent that can provide the fastest rate <= rate */
458 	num_parents = core->num_parents;
459 	for (i = 0; i < num_parents; i++) {
460 		parent = clk_core_get_parent_by_index(core, i);
461 		if (!parent)
462 			continue;
463 
464 		if (core->flags & CLK_SET_RATE_PARENT) {
465 			parent_req = *req;
466 			ret = __clk_determine_rate(parent->hw, &parent_req);
467 			if (ret)
468 				continue;
469 		} else {
470 			parent_req.rate = clk_core_get_rate_nolock(parent);
471 		}
472 
473 		if (mux_is_better_rate(req->rate, parent_req.rate,
474 				       best, flags)) {
475 			best_parent = parent;
476 			best = parent_req.rate;
477 		}
478 	}
479 
480 	if (!best_parent)
481 		return -EINVAL;
482 
483 out:
484 	if (best_parent)
485 		req->best_parent_hw = best_parent->hw;
486 	req->best_parent_rate = best;
487 	req->rate = best;
488 
489 	return 0;
490 }
491 
492 struct clk *__clk_lookup(const char *name)
493 {
494 	struct clk_core *core = clk_core_lookup(name);
495 
496 	return !core ? NULL : core->hw->clk;
497 }
498 
499 static void clk_core_get_boundaries(struct clk_core *core,
500 				    unsigned long *min_rate,
501 				    unsigned long *max_rate)
502 {
503 	struct clk *clk_user;
504 
505 	*min_rate = core->min_rate;
506 	*max_rate = core->max_rate;
507 
508 	hlist_for_each_entry(clk_user, &core->clks, clks_node)
509 		*min_rate = max(*min_rate, clk_user->min_rate);
510 
511 	hlist_for_each_entry(clk_user, &core->clks, clks_node)
512 		*max_rate = min(*max_rate, clk_user->max_rate);
513 }
514 
515 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
516 			   unsigned long max_rate)
517 {
518 	hw->core->min_rate = min_rate;
519 	hw->core->max_rate = max_rate;
520 }
521 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
522 
523 /*
524  * Helper for finding best parent to provide a given frequency. This can be used
525  * directly as a determine_rate callback (e.g. for a mux), or from a more
526  * complex clock that may combine a mux with other operations.
527  */
528 int __clk_mux_determine_rate(struct clk_hw *hw,
529 			     struct clk_rate_request *req)
530 {
531 	return clk_mux_determine_rate_flags(hw, req, 0);
532 }
533 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
534 
535 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
536 				     struct clk_rate_request *req)
537 {
538 	return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
539 }
540 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
541 
542 /***        clk api        ***/
543 
544 static void clk_core_rate_unprotect(struct clk_core *core)
545 {
546 	lockdep_assert_held(&prepare_lock);
547 
548 	if (!core)
549 		return;
550 
551 	if (WARN_ON(core->protect_count == 0))
552 		return;
553 
554 	if (--core->protect_count > 0)
555 		return;
556 
557 	clk_core_rate_unprotect(core->parent);
558 }
559 
560 static int clk_core_rate_nuke_protect(struct clk_core *core)
561 {
562 	int ret;
563 
564 	lockdep_assert_held(&prepare_lock);
565 
566 	if (!core)
567 		return -EINVAL;
568 
569 	if (core->protect_count == 0)
570 		return 0;
571 
572 	ret = core->protect_count;
573 	core->protect_count = 1;
574 	clk_core_rate_unprotect(core);
575 
576 	return ret;
577 }
578 
579 /**
580  * clk_rate_exclusive_put - release exclusivity over clock rate control
581  * @clk: the clk over which the exclusivity is released
582  *
583  * clk_rate_exclusive_put() completes a critical section during which a clock
584  * consumer cannot tolerate any other consumer making any operation on the
585  * clock which could result in a rate change or rate glitch. Exclusive clocks
586  * cannot have their rate changed, either directly or indirectly due to changes
587  * further up the parent chain of clocks. As a result, clocks up parent chain
588  * also get under exclusive control of the calling consumer.
589  *
590  * If exlusivity is claimed more than once on clock, even by the same consumer,
591  * the rate effectively gets locked as exclusivity can't be preempted.
592  *
593  * Calls to clk_rate_exclusive_put() must be balanced with calls to
594  * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
595  * error status.
596  */
597 void clk_rate_exclusive_put(struct clk *clk)
598 {
599 	if (!clk)
600 		return;
601 
602 	clk_prepare_lock();
603 
604 	/*
605 	 * if there is something wrong with this consumer protect count, stop
606 	 * here before messing with the provider
607 	 */
608 	if (WARN_ON(clk->exclusive_count <= 0))
609 		goto out;
610 
611 	clk_core_rate_unprotect(clk->core);
612 	clk->exclusive_count--;
613 out:
614 	clk_prepare_unlock();
615 }
616 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
617 
618 static void clk_core_rate_protect(struct clk_core *core)
619 {
620 	lockdep_assert_held(&prepare_lock);
621 
622 	if (!core)
623 		return;
624 
625 	if (core->protect_count == 0)
626 		clk_core_rate_protect(core->parent);
627 
628 	core->protect_count++;
629 }
630 
631 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
632 {
633 	lockdep_assert_held(&prepare_lock);
634 
635 	if (!core)
636 		return;
637 
638 	if (count == 0)
639 		return;
640 
641 	clk_core_rate_protect(core);
642 	core->protect_count = count;
643 }
644 
645 /**
646  * clk_rate_exclusive_get - get exclusivity over the clk rate control
647  * @clk: the clk over which the exclusity of rate control is requested
648  *
649  * clk_rate_exlusive_get() begins a critical section during which a clock
650  * consumer cannot tolerate any other consumer making any operation on the
651  * clock which could result in a rate change or rate glitch. Exclusive clocks
652  * cannot have their rate changed, either directly or indirectly due to changes
653  * further up the parent chain of clocks. As a result, clocks up parent chain
654  * also get under exclusive control of the calling consumer.
655  *
656  * If exlusivity is claimed more than once on clock, even by the same consumer,
657  * the rate effectively gets locked as exclusivity can't be preempted.
658  *
659  * Calls to clk_rate_exclusive_get() should be balanced with calls to
660  * clk_rate_exclusive_put(). Calls to this function may sleep.
661  * Returns 0 on success, -EERROR otherwise
662  */
663 int clk_rate_exclusive_get(struct clk *clk)
664 {
665 	if (!clk)
666 		return 0;
667 
668 	clk_prepare_lock();
669 	clk_core_rate_protect(clk->core);
670 	clk->exclusive_count++;
671 	clk_prepare_unlock();
672 
673 	return 0;
674 }
675 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
676 
677 static void clk_core_unprepare(struct clk_core *core)
678 {
679 	lockdep_assert_held(&prepare_lock);
680 
681 	if (!core)
682 		return;
683 
684 	if (WARN_ON(core->prepare_count == 0))
685 		return;
686 
687 	if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
688 		return;
689 
690 	if (--core->prepare_count > 0)
691 		return;
692 
693 	WARN_ON(core->enable_count > 0);
694 
695 	trace_clk_unprepare(core);
696 
697 	if (core->ops->unprepare)
698 		core->ops->unprepare(core->hw);
699 
700 	clk_pm_runtime_put(core);
701 
702 	trace_clk_unprepare_complete(core);
703 	clk_core_unprepare(core->parent);
704 }
705 
706 static void clk_core_unprepare_lock(struct clk_core *core)
707 {
708 	clk_prepare_lock();
709 	clk_core_unprepare(core);
710 	clk_prepare_unlock();
711 }
712 
713 /**
714  * clk_unprepare - undo preparation of a clock source
715  * @clk: the clk being unprepared
716  *
717  * clk_unprepare may sleep, which differentiates it from clk_disable.  In a
718  * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
719  * if the operation may sleep.  One example is a clk which is accessed over
720  * I2c.  In the complex case a clk gate operation may require a fast and a slow
721  * part.  It is this reason that clk_unprepare and clk_disable are not mutually
722  * exclusive.  In fact clk_disable must be called before clk_unprepare.
723  */
724 void clk_unprepare(struct clk *clk)
725 {
726 	if (IS_ERR_OR_NULL(clk))
727 		return;
728 
729 	clk_core_unprepare_lock(clk->core);
730 }
731 EXPORT_SYMBOL_GPL(clk_unprepare);
732 
733 static int clk_core_prepare(struct clk_core *core)
734 {
735 	int ret = 0;
736 
737 	lockdep_assert_held(&prepare_lock);
738 
739 	if (!core)
740 		return 0;
741 
742 	if (core->prepare_count == 0) {
743 		ret = clk_pm_runtime_get(core);
744 		if (ret)
745 			return ret;
746 
747 		ret = clk_core_prepare(core->parent);
748 		if (ret)
749 			goto runtime_put;
750 
751 		trace_clk_prepare(core);
752 
753 		if (core->ops->prepare)
754 			ret = core->ops->prepare(core->hw);
755 
756 		trace_clk_prepare_complete(core);
757 
758 		if (ret)
759 			goto unprepare;
760 	}
761 
762 	core->prepare_count++;
763 
764 	return 0;
765 unprepare:
766 	clk_core_unprepare(core->parent);
767 runtime_put:
768 	clk_pm_runtime_put(core);
769 	return ret;
770 }
771 
772 static int clk_core_prepare_lock(struct clk_core *core)
773 {
774 	int ret;
775 
776 	clk_prepare_lock();
777 	ret = clk_core_prepare(core);
778 	clk_prepare_unlock();
779 
780 	return ret;
781 }
782 
783 /**
784  * clk_prepare - prepare a clock source
785  * @clk: the clk being prepared
786  *
787  * clk_prepare may sleep, which differentiates it from clk_enable.  In a simple
788  * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
789  * operation may sleep.  One example is a clk which is accessed over I2c.  In
790  * the complex case a clk ungate operation may require a fast and a slow part.
791  * It is this reason that clk_prepare and clk_enable are not mutually
792  * exclusive.  In fact clk_prepare must be called before clk_enable.
793  * Returns 0 on success, -EERROR otherwise.
794  */
795 int clk_prepare(struct clk *clk)
796 {
797 	if (!clk)
798 		return 0;
799 
800 	return clk_core_prepare_lock(clk->core);
801 }
802 EXPORT_SYMBOL_GPL(clk_prepare);
803 
804 static void clk_core_disable(struct clk_core *core)
805 {
806 	lockdep_assert_held(&enable_lock);
807 
808 	if (!core)
809 		return;
810 
811 	if (WARN_ON(core->enable_count == 0))
812 		return;
813 
814 	if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
815 		return;
816 
817 	if (--core->enable_count > 0)
818 		return;
819 
820 	trace_clk_disable_rcuidle(core);
821 
822 	if (core->ops->disable)
823 		core->ops->disable(core->hw);
824 
825 	trace_clk_disable_complete_rcuidle(core);
826 
827 	clk_core_disable(core->parent);
828 }
829 
830 static void clk_core_disable_lock(struct clk_core *core)
831 {
832 	unsigned long flags;
833 
834 	flags = clk_enable_lock();
835 	clk_core_disable(core);
836 	clk_enable_unlock(flags);
837 }
838 
839 /**
840  * clk_disable - gate a clock
841  * @clk: the clk being gated
842  *
843  * clk_disable must not sleep, which differentiates it from clk_unprepare.  In
844  * a simple case, clk_disable can be used instead of clk_unprepare to gate a
845  * clk if the operation is fast and will never sleep.  One example is a
846  * SoC-internal clk which is controlled via simple register writes.  In the
847  * complex case a clk gate operation may require a fast and a slow part.  It is
848  * this reason that clk_unprepare and clk_disable are not mutually exclusive.
849  * In fact clk_disable must be called before clk_unprepare.
850  */
851 void clk_disable(struct clk *clk)
852 {
853 	if (IS_ERR_OR_NULL(clk))
854 		return;
855 
856 	clk_core_disable_lock(clk->core);
857 }
858 EXPORT_SYMBOL_GPL(clk_disable);
859 
860 static int clk_core_enable(struct clk_core *core)
861 {
862 	int ret = 0;
863 
864 	lockdep_assert_held(&enable_lock);
865 
866 	if (!core)
867 		return 0;
868 
869 	if (WARN_ON(core->prepare_count == 0))
870 		return -ESHUTDOWN;
871 
872 	if (core->enable_count == 0) {
873 		ret = clk_core_enable(core->parent);
874 
875 		if (ret)
876 			return ret;
877 
878 		trace_clk_enable_rcuidle(core);
879 
880 		if (core->ops->enable)
881 			ret = core->ops->enable(core->hw);
882 
883 		trace_clk_enable_complete_rcuidle(core);
884 
885 		if (ret) {
886 			clk_core_disable(core->parent);
887 			return ret;
888 		}
889 	}
890 
891 	core->enable_count++;
892 	return 0;
893 }
894 
895 static int clk_core_enable_lock(struct clk_core *core)
896 {
897 	unsigned long flags;
898 	int ret;
899 
900 	flags = clk_enable_lock();
901 	ret = clk_core_enable(core);
902 	clk_enable_unlock(flags);
903 
904 	return ret;
905 }
906 
907 /**
908  * clk_enable - ungate a clock
909  * @clk: the clk being ungated
910  *
911  * clk_enable must not sleep, which differentiates it from clk_prepare.  In a
912  * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
913  * if the operation will never sleep.  One example is a SoC-internal clk which
914  * is controlled via simple register writes.  In the complex case a clk ungate
915  * operation may require a fast and a slow part.  It is this reason that
916  * clk_enable and clk_prepare are not mutually exclusive.  In fact clk_prepare
917  * must be called before clk_enable.  Returns 0 on success, -EERROR
918  * otherwise.
919  */
920 int clk_enable(struct clk *clk)
921 {
922 	if (!clk)
923 		return 0;
924 
925 	return clk_core_enable_lock(clk->core);
926 }
927 EXPORT_SYMBOL_GPL(clk_enable);
928 
929 static int clk_core_prepare_enable(struct clk_core *core)
930 {
931 	int ret;
932 
933 	ret = clk_core_prepare_lock(core);
934 	if (ret)
935 		return ret;
936 
937 	ret = clk_core_enable_lock(core);
938 	if (ret)
939 		clk_core_unprepare_lock(core);
940 
941 	return ret;
942 }
943 
944 static void clk_core_disable_unprepare(struct clk_core *core)
945 {
946 	clk_core_disable_lock(core);
947 	clk_core_unprepare_lock(core);
948 }
949 
950 static void clk_unprepare_unused_subtree(struct clk_core *core)
951 {
952 	struct clk_core *child;
953 
954 	lockdep_assert_held(&prepare_lock);
955 
956 	hlist_for_each_entry(child, &core->children, child_node)
957 		clk_unprepare_unused_subtree(child);
958 
959 	if (core->prepare_count)
960 		return;
961 
962 	if (core->flags & CLK_IGNORE_UNUSED)
963 		return;
964 
965 	if (clk_pm_runtime_get(core))
966 		return;
967 
968 	if (clk_core_is_prepared(core)) {
969 		trace_clk_unprepare(core);
970 		if (core->ops->unprepare_unused)
971 			core->ops->unprepare_unused(core->hw);
972 		else if (core->ops->unprepare)
973 			core->ops->unprepare(core->hw);
974 		trace_clk_unprepare_complete(core);
975 	}
976 
977 	clk_pm_runtime_put(core);
978 }
979 
980 static void clk_disable_unused_subtree(struct clk_core *core)
981 {
982 	struct clk_core *child;
983 	unsigned long flags;
984 
985 	lockdep_assert_held(&prepare_lock);
986 
987 	hlist_for_each_entry(child, &core->children, child_node)
988 		clk_disable_unused_subtree(child);
989 
990 	if (core->flags & CLK_OPS_PARENT_ENABLE)
991 		clk_core_prepare_enable(core->parent);
992 
993 	if (clk_pm_runtime_get(core))
994 		goto unprepare_out;
995 
996 	flags = clk_enable_lock();
997 
998 	if (core->enable_count)
999 		goto unlock_out;
1000 
1001 	if (core->flags & CLK_IGNORE_UNUSED)
1002 		goto unlock_out;
1003 
1004 	/*
1005 	 * some gate clocks have special needs during the disable-unused
1006 	 * sequence.  call .disable_unused if available, otherwise fall
1007 	 * back to .disable
1008 	 */
1009 	if (clk_core_is_enabled(core)) {
1010 		trace_clk_disable(core);
1011 		if (core->ops->disable_unused)
1012 			core->ops->disable_unused(core->hw);
1013 		else if (core->ops->disable)
1014 			core->ops->disable(core->hw);
1015 		trace_clk_disable_complete(core);
1016 	}
1017 
1018 unlock_out:
1019 	clk_enable_unlock(flags);
1020 	clk_pm_runtime_put(core);
1021 unprepare_out:
1022 	if (core->flags & CLK_OPS_PARENT_ENABLE)
1023 		clk_core_disable_unprepare(core->parent);
1024 }
1025 
1026 static bool clk_ignore_unused;
1027 static int __init clk_ignore_unused_setup(char *__unused)
1028 {
1029 	clk_ignore_unused = true;
1030 	return 1;
1031 }
1032 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1033 
1034 static int clk_disable_unused(void)
1035 {
1036 	struct clk_core *core;
1037 
1038 	if (clk_ignore_unused) {
1039 		pr_warn("clk: Not disabling unused clocks\n");
1040 		return 0;
1041 	}
1042 
1043 	clk_prepare_lock();
1044 
1045 	hlist_for_each_entry(core, &clk_root_list, child_node)
1046 		clk_disable_unused_subtree(core);
1047 
1048 	hlist_for_each_entry(core, &clk_orphan_list, child_node)
1049 		clk_disable_unused_subtree(core);
1050 
1051 	hlist_for_each_entry(core, &clk_root_list, child_node)
1052 		clk_unprepare_unused_subtree(core);
1053 
1054 	hlist_for_each_entry(core, &clk_orphan_list, child_node)
1055 		clk_unprepare_unused_subtree(core);
1056 
1057 	clk_prepare_unlock();
1058 
1059 	return 0;
1060 }
1061 late_initcall_sync(clk_disable_unused);
1062 
1063 static int clk_core_determine_round_nolock(struct clk_core *core,
1064 					   struct clk_rate_request *req)
1065 {
1066 	long rate;
1067 
1068 	lockdep_assert_held(&prepare_lock);
1069 
1070 	if (!core)
1071 		return 0;
1072 
1073 	/*
1074 	 * At this point, core protection will be disabled if
1075 	 * - if the provider is not protected at all
1076 	 * - if the calling consumer is the only one which has exclusivity
1077 	 *   over the provider
1078 	 */
1079 	if (clk_core_rate_is_protected(core)) {
1080 		req->rate = core->rate;
1081 	} else if (core->ops->determine_rate) {
1082 		return core->ops->determine_rate(core->hw, req);
1083 	} else if (core->ops->round_rate) {
1084 		rate = core->ops->round_rate(core->hw, req->rate,
1085 					     &req->best_parent_rate);
1086 		if (rate < 0)
1087 			return rate;
1088 
1089 		req->rate = rate;
1090 	} else {
1091 		return -EINVAL;
1092 	}
1093 
1094 	return 0;
1095 }
1096 
1097 static void clk_core_init_rate_req(struct clk_core * const core,
1098 				   struct clk_rate_request *req)
1099 {
1100 	struct clk_core *parent;
1101 
1102 	if (WARN_ON(!core || !req))
1103 		return;
1104 
1105 	parent = core->parent;
1106 	if (parent) {
1107 		req->best_parent_hw = parent->hw;
1108 		req->best_parent_rate = parent->rate;
1109 	} else {
1110 		req->best_parent_hw = NULL;
1111 		req->best_parent_rate = 0;
1112 	}
1113 }
1114 
1115 static bool clk_core_can_round(struct clk_core * const core)
1116 {
1117 	if (core->ops->determine_rate || core->ops->round_rate)
1118 		return true;
1119 
1120 	return false;
1121 }
1122 
1123 static int clk_core_round_rate_nolock(struct clk_core *core,
1124 				      struct clk_rate_request *req)
1125 {
1126 	lockdep_assert_held(&prepare_lock);
1127 
1128 	if (!core) {
1129 		req->rate = 0;
1130 		return 0;
1131 	}
1132 
1133 	clk_core_init_rate_req(core, req);
1134 
1135 	if (clk_core_can_round(core))
1136 		return clk_core_determine_round_nolock(core, req);
1137 	else if (core->flags & CLK_SET_RATE_PARENT)
1138 		return clk_core_round_rate_nolock(core->parent, req);
1139 
1140 	req->rate = core->rate;
1141 	return 0;
1142 }
1143 
1144 /**
1145  * __clk_determine_rate - get the closest rate actually supported by a clock
1146  * @hw: determine the rate of this clock
1147  * @req: target rate request
1148  *
1149  * Useful for clk_ops such as .set_rate and .determine_rate.
1150  */
1151 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1152 {
1153 	if (!hw) {
1154 		req->rate = 0;
1155 		return 0;
1156 	}
1157 
1158 	return clk_core_round_rate_nolock(hw->core, req);
1159 }
1160 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1161 
1162 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1163 {
1164 	int ret;
1165 	struct clk_rate_request req;
1166 
1167 	clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1168 	req.rate = rate;
1169 
1170 	ret = clk_core_round_rate_nolock(hw->core, &req);
1171 	if (ret)
1172 		return 0;
1173 
1174 	return req.rate;
1175 }
1176 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1177 
1178 /**
1179  * clk_round_rate - round the given rate for a clk
1180  * @clk: the clk for which we are rounding a rate
1181  * @rate: the rate which is to be rounded
1182  *
1183  * Takes in a rate as input and rounds it to a rate that the clk can actually
1184  * use which is then returned.  If clk doesn't support round_rate operation
1185  * then the parent rate is returned.
1186  */
1187 long clk_round_rate(struct clk *clk, unsigned long rate)
1188 {
1189 	struct clk_rate_request req;
1190 	int ret;
1191 
1192 	if (!clk)
1193 		return 0;
1194 
1195 	clk_prepare_lock();
1196 
1197 	if (clk->exclusive_count)
1198 		clk_core_rate_unprotect(clk->core);
1199 
1200 	clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1201 	req.rate = rate;
1202 
1203 	ret = clk_core_round_rate_nolock(clk->core, &req);
1204 
1205 	if (clk->exclusive_count)
1206 		clk_core_rate_protect(clk->core);
1207 
1208 	clk_prepare_unlock();
1209 
1210 	if (ret)
1211 		return ret;
1212 
1213 	return req.rate;
1214 }
1215 EXPORT_SYMBOL_GPL(clk_round_rate);
1216 
1217 /**
1218  * __clk_notify - call clk notifier chain
1219  * @core: clk that is changing rate
1220  * @msg: clk notifier type (see include/linux/clk.h)
1221  * @old_rate: old clk rate
1222  * @new_rate: new clk rate
1223  *
1224  * Triggers a notifier call chain on the clk rate-change notification
1225  * for 'clk'.  Passes a pointer to the struct clk and the previous
1226  * and current rates to the notifier callback.  Intended to be called by
1227  * internal clock code only.  Returns NOTIFY_DONE from the last driver
1228  * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1229  * a driver returns that.
1230  */
1231 static int __clk_notify(struct clk_core *core, unsigned long msg,
1232 		unsigned long old_rate, unsigned long new_rate)
1233 {
1234 	struct clk_notifier *cn;
1235 	struct clk_notifier_data cnd;
1236 	int ret = NOTIFY_DONE;
1237 
1238 	cnd.old_rate = old_rate;
1239 	cnd.new_rate = new_rate;
1240 
1241 	list_for_each_entry(cn, &clk_notifier_list, node) {
1242 		if (cn->clk->core == core) {
1243 			cnd.clk = cn->clk;
1244 			ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1245 					&cnd);
1246 			if (ret & NOTIFY_STOP_MASK)
1247 				return ret;
1248 		}
1249 	}
1250 
1251 	return ret;
1252 }
1253 
1254 /**
1255  * __clk_recalc_accuracies
1256  * @core: first clk in the subtree
1257  *
1258  * Walks the subtree of clks starting with clk and recalculates accuracies as
1259  * it goes.  Note that if a clk does not implement the .recalc_accuracy
1260  * callback then it is assumed that the clock will take on the accuracy of its
1261  * parent.
1262  */
1263 static void __clk_recalc_accuracies(struct clk_core *core)
1264 {
1265 	unsigned long parent_accuracy = 0;
1266 	struct clk_core *child;
1267 
1268 	lockdep_assert_held(&prepare_lock);
1269 
1270 	if (core->parent)
1271 		parent_accuracy = core->parent->accuracy;
1272 
1273 	if (core->ops->recalc_accuracy)
1274 		core->accuracy = core->ops->recalc_accuracy(core->hw,
1275 							  parent_accuracy);
1276 	else
1277 		core->accuracy = parent_accuracy;
1278 
1279 	hlist_for_each_entry(child, &core->children, child_node)
1280 		__clk_recalc_accuracies(child);
1281 }
1282 
1283 static long clk_core_get_accuracy(struct clk_core *core)
1284 {
1285 	unsigned long accuracy;
1286 
1287 	clk_prepare_lock();
1288 	if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1289 		__clk_recalc_accuracies(core);
1290 
1291 	accuracy = __clk_get_accuracy(core);
1292 	clk_prepare_unlock();
1293 
1294 	return accuracy;
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  */
1306 long clk_get_accuracy(struct clk *clk)
1307 {
1308 	if (!clk)
1309 		return 0;
1310 
1311 	return clk_core_get_accuracy(clk->core);
1312 }
1313 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1314 
1315 static unsigned long clk_recalc(struct clk_core *core,
1316 				unsigned long parent_rate)
1317 {
1318 	unsigned long rate = parent_rate;
1319 
1320 	if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1321 		rate = core->ops->recalc_rate(core->hw, parent_rate);
1322 		clk_pm_runtime_put(core);
1323 	}
1324 	return rate;
1325 }
1326 
1327 /**
1328  * __clk_recalc_rates
1329  * @core: first clk in the subtree
1330  * @msg: notification type (see include/linux/clk.h)
1331  *
1332  * Walks the subtree of clks starting with clk and recalculates rates as it
1333  * goes.  Note that if a clk does not implement the .recalc_rate callback then
1334  * it is assumed that the clock will take on the rate of its parent.
1335  *
1336  * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1337  * if necessary.
1338  */
1339 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1340 {
1341 	unsigned long old_rate;
1342 	unsigned long parent_rate = 0;
1343 	struct clk_core *child;
1344 
1345 	lockdep_assert_held(&prepare_lock);
1346 
1347 	old_rate = core->rate;
1348 
1349 	if (core->parent)
1350 		parent_rate = core->parent->rate;
1351 
1352 	core->rate = clk_recalc(core, parent_rate);
1353 
1354 	/*
1355 	 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1356 	 * & ABORT_RATE_CHANGE notifiers
1357 	 */
1358 	if (core->notifier_count && msg)
1359 		__clk_notify(core, msg, old_rate, core->rate);
1360 
1361 	hlist_for_each_entry(child, &core->children, child_node)
1362 		__clk_recalc_rates(child, msg);
1363 }
1364 
1365 static unsigned long clk_core_get_rate(struct clk_core *core)
1366 {
1367 	unsigned long rate;
1368 
1369 	clk_prepare_lock();
1370 
1371 	if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1372 		__clk_recalc_rates(core, 0);
1373 
1374 	rate = clk_core_get_rate_nolock(core);
1375 	clk_prepare_unlock();
1376 
1377 	return rate;
1378 }
1379 
1380 /**
1381  * clk_get_rate - return the rate of clk
1382  * @clk: the clk whose rate is being returned
1383  *
1384  * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1385  * is set, which means a recalc_rate will be issued.
1386  * If clk is NULL then returns 0.
1387  */
1388 unsigned long clk_get_rate(struct clk *clk)
1389 {
1390 	if (!clk)
1391 		return 0;
1392 
1393 	return clk_core_get_rate(clk->core);
1394 }
1395 EXPORT_SYMBOL_GPL(clk_get_rate);
1396 
1397 static int clk_fetch_parent_index(struct clk_core *core,
1398 				  struct clk_core *parent)
1399 {
1400 	int i;
1401 
1402 	if (!parent)
1403 		return -EINVAL;
1404 
1405 	for (i = 0; i < core->num_parents; i++)
1406 		if (clk_core_get_parent_by_index(core, i) == parent)
1407 			return i;
1408 
1409 	return -EINVAL;
1410 }
1411 
1412 /*
1413  * Update the orphan status of @core and all its children.
1414  */
1415 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1416 {
1417 	struct clk_core *child;
1418 
1419 	core->orphan = is_orphan;
1420 
1421 	hlist_for_each_entry(child, &core->children, child_node)
1422 		clk_core_update_orphan_status(child, is_orphan);
1423 }
1424 
1425 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1426 {
1427 	bool was_orphan = core->orphan;
1428 
1429 	hlist_del(&core->child_node);
1430 
1431 	if (new_parent) {
1432 		bool becomes_orphan = new_parent->orphan;
1433 
1434 		/* avoid duplicate POST_RATE_CHANGE notifications */
1435 		if (new_parent->new_child == core)
1436 			new_parent->new_child = NULL;
1437 
1438 		hlist_add_head(&core->child_node, &new_parent->children);
1439 
1440 		if (was_orphan != becomes_orphan)
1441 			clk_core_update_orphan_status(core, becomes_orphan);
1442 	} else {
1443 		hlist_add_head(&core->child_node, &clk_orphan_list);
1444 		if (!was_orphan)
1445 			clk_core_update_orphan_status(core, true);
1446 	}
1447 
1448 	core->parent = new_parent;
1449 }
1450 
1451 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1452 					   struct clk_core *parent)
1453 {
1454 	unsigned long flags;
1455 	struct clk_core *old_parent = core->parent;
1456 
1457 	/*
1458 	 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1459 	 *
1460 	 * 2. Migrate prepare state between parents and prevent race with
1461 	 * clk_enable().
1462 	 *
1463 	 * If the clock is not prepared, then a race with
1464 	 * clk_enable/disable() is impossible since we already have the
1465 	 * prepare lock (future calls to clk_enable() need to be preceded by
1466 	 * a clk_prepare()).
1467 	 *
1468 	 * If the clock is prepared, migrate the prepared state to the new
1469 	 * parent and also protect against a race with clk_enable() by
1470 	 * forcing the clock and the new parent on.  This ensures that all
1471 	 * future calls to clk_enable() are practically NOPs with respect to
1472 	 * hardware and software states.
1473 	 *
1474 	 * See also: Comment for clk_set_parent() below.
1475 	 */
1476 
1477 	/* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1478 	if (core->flags & CLK_OPS_PARENT_ENABLE) {
1479 		clk_core_prepare_enable(old_parent);
1480 		clk_core_prepare_enable(parent);
1481 	}
1482 
1483 	/* migrate prepare count if > 0 */
1484 	if (core->prepare_count) {
1485 		clk_core_prepare_enable(parent);
1486 		clk_core_enable_lock(core);
1487 	}
1488 
1489 	/* update the clk tree topology */
1490 	flags = clk_enable_lock();
1491 	clk_reparent(core, parent);
1492 	clk_enable_unlock(flags);
1493 
1494 	return old_parent;
1495 }
1496 
1497 static void __clk_set_parent_after(struct clk_core *core,
1498 				   struct clk_core *parent,
1499 				   struct clk_core *old_parent)
1500 {
1501 	/*
1502 	 * Finish the migration of prepare state and undo the changes done
1503 	 * for preventing a race with clk_enable().
1504 	 */
1505 	if (core->prepare_count) {
1506 		clk_core_disable_lock(core);
1507 		clk_core_disable_unprepare(old_parent);
1508 	}
1509 
1510 	/* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1511 	if (core->flags & CLK_OPS_PARENT_ENABLE) {
1512 		clk_core_disable_unprepare(parent);
1513 		clk_core_disable_unprepare(old_parent);
1514 	}
1515 }
1516 
1517 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1518 			    u8 p_index)
1519 {
1520 	unsigned long flags;
1521 	int ret = 0;
1522 	struct clk_core *old_parent;
1523 
1524 	old_parent = __clk_set_parent_before(core, parent);
1525 
1526 	trace_clk_set_parent(core, parent);
1527 
1528 	/* change clock input source */
1529 	if (parent && core->ops->set_parent)
1530 		ret = core->ops->set_parent(core->hw, p_index);
1531 
1532 	trace_clk_set_parent_complete(core, parent);
1533 
1534 	if (ret) {
1535 		flags = clk_enable_lock();
1536 		clk_reparent(core, old_parent);
1537 		clk_enable_unlock(flags);
1538 		__clk_set_parent_after(core, old_parent, parent);
1539 
1540 		return ret;
1541 	}
1542 
1543 	__clk_set_parent_after(core, parent, old_parent);
1544 
1545 	return 0;
1546 }
1547 
1548 /**
1549  * __clk_speculate_rates
1550  * @core: first clk in the subtree
1551  * @parent_rate: the "future" rate of clk's parent
1552  *
1553  * Walks the subtree of clks starting with clk, speculating rates as it
1554  * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1555  *
1556  * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1557  * pre-rate change notifications and returns early if no clks in the
1558  * subtree have subscribed to the notifications.  Note that if a clk does not
1559  * implement the .recalc_rate callback then it is assumed that the clock will
1560  * take on the rate of its parent.
1561  */
1562 static int __clk_speculate_rates(struct clk_core *core,
1563 				 unsigned long parent_rate)
1564 {
1565 	struct clk_core *child;
1566 	unsigned long new_rate;
1567 	int ret = NOTIFY_DONE;
1568 
1569 	lockdep_assert_held(&prepare_lock);
1570 
1571 	new_rate = clk_recalc(core, parent_rate);
1572 
1573 	/* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1574 	if (core->notifier_count)
1575 		ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1576 
1577 	if (ret & NOTIFY_STOP_MASK) {
1578 		pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1579 				__func__, core->name, ret);
1580 		goto out;
1581 	}
1582 
1583 	hlist_for_each_entry(child, &core->children, child_node) {
1584 		ret = __clk_speculate_rates(child, new_rate);
1585 		if (ret & NOTIFY_STOP_MASK)
1586 			break;
1587 	}
1588 
1589 out:
1590 	return ret;
1591 }
1592 
1593 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1594 			     struct clk_core *new_parent, u8 p_index)
1595 {
1596 	struct clk_core *child;
1597 
1598 	core->new_rate = new_rate;
1599 	core->new_parent = new_parent;
1600 	core->new_parent_index = p_index;
1601 	/* include clk in new parent's PRE_RATE_CHANGE notifications */
1602 	core->new_child = NULL;
1603 	if (new_parent && new_parent != core->parent)
1604 		new_parent->new_child = core;
1605 
1606 	hlist_for_each_entry(child, &core->children, child_node) {
1607 		child->new_rate = clk_recalc(child, new_rate);
1608 		clk_calc_subtree(child, child->new_rate, NULL, 0);
1609 	}
1610 }
1611 
1612 /*
1613  * calculate the new rates returning the topmost clock that has to be
1614  * changed.
1615  */
1616 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1617 					   unsigned long rate)
1618 {
1619 	struct clk_core *top = core;
1620 	struct clk_core *old_parent, *parent;
1621 	unsigned long best_parent_rate = 0;
1622 	unsigned long new_rate;
1623 	unsigned long min_rate;
1624 	unsigned long max_rate;
1625 	int p_index = 0;
1626 	long ret;
1627 
1628 	/* sanity */
1629 	if (IS_ERR_OR_NULL(core))
1630 		return NULL;
1631 
1632 	/* save parent rate, if it exists */
1633 	parent = old_parent = core->parent;
1634 	if (parent)
1635 		best_parent_rate = parent->rate;
1636 
1637 	clk_core_get_boundaries(core, &min_rate, &max_rate);
1638 
1639 	/* find the closest rate and parent clk/rate */
1640 	if (clk_core_can_round(core)) {
1641 		struct clk_rate_request req;
1642 
1643 		req.rate = rate;
1644 		req.min_rate = min_rate;
1645 		req.max_rate = max_rate;
1646 
1647 		clk_core_init_rate_req(core, &req);
1648 
1649 		ret = clk_core_determine_round_nolock(core, &req);
1650 		if (ret < 0)
1651 			return NULL;
1652 
1653 		best_parent_rate = req.best_parent_rate;
1654 		new_rate = req.rate;
1655 		parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1656 
1657 		if (new_rate < min_rate || new_rate > max_rate)
1658 			return NULL;
1659 	} else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1660 		/* pass-through clock without adjustable parent */
1661 		core->new_rate = core->rate;
1662 		return NULL;
1663 	} else {
1664 		/* pass-through clock with adjustable parent */
1665 		top = clk_calc_new_rates(parent, rate);
1666 		new_rate = parent->new_rate;
1667 		goto out;
1668 	}
1669 
1670 	/* some clocks must be gated to change parent */
1671 	if (parent != old_parent &&
1672 	    (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1673 		pr_debug("%s: %s not gated but wants to reparent\n",
1674 			 __func__, core->name);
1675 		return NULL;
1676 	}
1677 
1678 	/* try finding the new parent index */
1679 	if (parent && core->num_parents > 1) {
1680 		p_index = clk_fetch_parent_index(core, parent);
1681 		if (p_index < 0) {
1682 			pr_debug("%s: clk %s can not be parent of clk %s\n",
1683 				 __func__, parent->name, core->name);
1684 			return NULL;
1685 		}
1686 	}
1687 
1688 	if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1689 	    best_parent_rate != parent->rate)
1690 		top = clk_calc_new_rates(parent, best_parent_rate);
1691 
1692 out:
1693 	clk_calc_subtree(core, new_rate, parent, p_index);
1694 
1695 	return top;
1696 }
1697 
1698 /*
1699  * Notify about rate changes in a subtree. Always walk down the whole tree
1700  * so that in case of an error we can walk down the whole tree again and
1701  * abort the change.
1702  */
1703 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1704 						  unsigned long event)
1705 {
1706 	struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1707 	int ret = NOTIFY_DONE;
1708 
1709 	if (core->rate == core->new_rate)
1710 		return NULL;
1711 
1712 	if (core->notifier_count) {
1713 		ret = __clk_notify(core, event, core->rate, core->new_rate);
1714 		if (ret & NOTIFY_STOP_MASK)
1715 			fail_clk = core;
1716 	}
1717 
1718 	hlist_for_each_entry(child, &core->children, child_node) {
1719 		/* Skip children who will be reparented to another clock */
1720 		if (child->new_parent && child->new_parent != core)
1721 			continue;
1722 		tmp_clk = clk_propagate_rate_change(child, event);
1723 		if (tmp_clk)
1724 			fail_clk = tmp_clk;
1725 	}
1726 
1727 	/* handle the new child who might not be in core->children yet */
1728 	if (core->new_child) {
1729 		tmp_clk = clk_propagate_rate_change(core->new_child, event);
1730 		if (tmp_clk)
1731 			fail_clk = tmp_clk;
1732 	}
1733 
1734 	return fail_clk;
1735 }
1736 
1737 /*
1738  * walk down a subtree and set the new rates notifying the rate
1739  * change on the way
1740  */
1741 static void clk_change_rate(struct clk_core *core)
1742 {
1743 	struct clk_core *child;
1744 	struct hlist_node *tmp;
1745 	unsigned long old_rate;
1746 	unsigned long best_parent_rate = 0;
1747 	bool skip_set_rate = false;
1748 	struct clk_core *old_parent;
1749 	struct clk_core *parent = NULL;
1750 
1751 	old_rate = core->rate;
1752 
1753 	if (core->new_parent) {
1754 		parent = core->new_parent;
1755 		best_parent_rate = core->new_parent->rate;
1756 	} else if (core->parent) {
1757 		parent = core->parent;
1758 		best_parent_rate = core->parent->rate;
1759 	}
1760 
1761 	if (clk_pm_runtime_get(core))
1762 		return;
1763 
1764 	if (core->flags & CLK_SET_RATE_UNGATE) {
1765 		unsigned long flags;
1766 
1767 		clk_core_prepare(core);
1768 		flags = clk_enable_lock();
1769 		clk_core_enable(core);
1770 		clk_enable_unlock(flags);
1771 	}
1772 
1773 	if (core->new_parent && core->new_parent != core->parent) {
1774 		old_parent = __clk_set_parent_before(core, core->new_parent);
1775 		trace_clk_set_parent(core, core->new_parent);
1776 
1777 		if (core->ops->set_rate_and_parent) {
1778 			skip_set_rate = true;
1779 			core->ops->set_rate_and_parent(core->hw, core->new_rate,
1780 					best_parent_rate,
1781 					core->new_parent_index);
1782 		} else if (core->ops->set_parent) {
1783 			core->ops->set_parent(core->hw, core->new_parent_index);
1784 		}
1785 
1786 		trace_clk_set_parent_complete(core, core->new_parent);
1787 		__clk_set_parent_after(core, core->new_parent, old_parent);
1788 	}
1789 
1790 	if (core->flags & CLK_OPS_PARENT_ENABLE)
1791 		clk_core_prepare_enable(parent);
1792 
1793 	trace_clk_set_rate(core, core->new_rate);
1794 
1795 	if (!skip_set_rate && core->ops->set_rate)
1796 		core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1797 
1798 	trace_clk_set_rate_complete(core, core->new_rate);
1799 
1800 	core->rate = clk_recalc(core, best_parent_rate);
1801 
1802 	if (core->flags & CLK_SET_RATE_UNGATE) {
1803 		unsigned long flags;
1804 
1805 		flags = clk_enable_lock();
1806 		clk_core_disable(core);
1807 		clk_enable_unlock(flags);
1808 		clk_core_unprepare(core);
1809 	}
1810 
1811 	if (core->flags & CLK_OPS_PARENT_ENABLE)
1812 		clk_core_disable_unprepare(parent);
1813 
1814 	if (core->notifier_count && old_rate != core->rate)
1815 		__clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1816 
1817 	if (core->flags & CLK_RECALC_NEW_RATES)
1818 		(void)clk_calc_new_rates(core, core->new_rate);
1819 
1820 	/*
1821 	 * Use safe iteration, as change_rate can actually swap parents
1822 	 * for certain clock types.
1823 	 */
1824 	hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1825 		/* Skip children who will be reparented to another clock */
1826 		if (child->new_parent && child->new_parent != core)
1827 			continue;
1828 		clk_change_rate(child);
1829 	}
1830 
1831 	/* handle the new child who might not be in core->children yet */
1832 	if (core->new_child)
1833 		clk_change_rate(core->new_child);
1834 
1835 	clk_pm_runtime_put(core);
1836 }
1837 
1838 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1839 						     unsigned long req_rate)
1840 {
1841 	int ret, cnt;
1842 	struct clk_rate_request req;
1843 
1844 	lockdep_assert_held(&prepare_lock);
1845 
1846 	if (!core)
1847 		return 0;
1848 
1849 	/* simulate what the rate would be if it could be freely set */
1850 	cnt = clk_core_rate_nuke_protect(core);
1851 	if (cnt < 0)
1852 		return cnt;
1853 
1854 	clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1855 	req.rate = req_rate;
1856 
1857 	ret = clk_core_round_rate_nolock(core, &req);
1858 
1859 	/* restore the protection */
1860 	clk_core_rate_restore_protect(core, cnt);
1861 
1862 	return ret ? 0 : req.rate;
1863 }
1864 
1865 static int clk_core_set_rate_nolock(struct clk_core *core,
1866 				    unsigned long req_rate)
1867 {
1868 	struct clk_core *top, *fail_clk;
1869 	unsigned long rate;
1870 	int ret = 0;
1871 
1872 	if (!core)
1873 		return 0;
1874 
1875 	rate = clk_core_req_round_rate_nolock(core, req_rate);
1876 
1877 	/* bail early if nothing to do */
1878 	if (rate == clk_core_get_rate_nolock(core))
1879 		return 0;
1880 
1881 	/* fail on a direct rate set of a protected provider */
1882 	if (clk_core_rate_is_protected(core))
1883 		return -EBUSY;
1884 
1885 	if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1886 		return -EBUSY;
1887 
1888 	/* calculate new rates and get the topmost changed clock */
1889 	top = clk_calc_new_rates(core, req_rate);
1890 	if (!top)
1891 		return -EINVAL;
1892 
1893 	ret = clk_pm_runtime_get(core);
1894 	if (ret)
1895 		return ret;
1896 
1897 	/* notify that we are about to change rates */
1898 	fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1899 	if (fail_clk) {
1900 		pr_debug("%s: failed to set %s rate\n", __func__,
1901 				fail_clk->name);
1902 		clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1903 		ret = -EBUSY;
1904 		goto err;
1905 	}
1906 
1907 	/* change the rates */
1908 	clk_change_rate(top);
1909 
1910 	core->req_rate = req_rate;
1911 err:
1912 	clk_pm_runtime_put(core);
1913 
1914 	return ret;
1915 }
1916 
1917 /**
1918  * clk_set_rate - specify a new rate for clk
1919  * @clk: the clk whose rate is being changed
1920  * @rate: the new rate for clk
1921  *
1922  * In the simplest case clk_set_rate will only adjust the rate of clk.
1923  *
1924  * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1925  * propagate up to clk's parent; whether or not this happens depends on the
1926  * outcome of clk's .round_rate implementation.  If *parent_rate is unchanged
1927  * after calling .round_rate then upstream parent propagation is ignored.  If
1928  * *parent_rate comes back with a new rate for clk's parent then we propagate
1929  * up to clk's parent and set its rate.  Upward propagation will continue
1930  * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1931  * .round_rate stops requesting changes to clk's parent_rate.
1932  *
1933  * Rate changes are accomplished via tree traversal that also recalculates the
1934  * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1935  *
1936  * Returns 0 on success, -EERROR otherwise.
1937  */
1938 int clk_set_rate(struct clk *clk, unsigned long rate)
1939 {
1940 	int ret;
1941 
1942 	if (!clk)
1943 		return 0;
1944 
1945 	/* prevent racing with updates to the clock topology */
1946 	clk_prepare_lock();
1947 
1948 	if (clk->exclusive_count)
1949 		clk_core_rate_unprotect(clk->core);
1950 
1951 	ret = clk_core_set_rate_nolock(clk->core, rate);
1952 
1953 	if (clk->exclusive_count)
1954 		clk_core_rate_protect(clk->core);
1955 
1956 	clk_prepare_unlock();
1957 
1958 	return ret;
1959 }
1960 EXPORT_SYMBOL_GPL(clk_set_rate);
1961 
1962 /**
1963  * clk_set_rate_exclusive - specify a new rate get exclusive control
1964  * @clk: the clk whose rate is being changed
1965  * @rate: the new rate for clk
1966  *
1967  * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1968  * within a critical section
1969  *
1970  * This can be used initially to ensure that at least 1 consumer is
1971  * statisfied when several consumers are competing for exclusivity over the
1972  * same clock provider.
1973  *
1974  * The exclusivity is not applied if setting the rate failed.
1975  *
1976  * Calls to clk_rate_exclusive_get() should be balanced with calls to
1977  * clk_rate_exclusive_put().
1978  *
1979  * Returns 0 on success, -EERROR otherwise.
1980  */
1981 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1982 {
1983 	int ret;
1984 
1985 	if (!clk)
1986 		return 0;
1987 
1988 	/* prevent racing with updates to the clock topology */
1989 	clk_prepare_lock();
1990 
1991 	/*
1992 	 * The temporary protection removal is not here, on purpose
1993 	 * This function is meant to be used instead of clk_rate_protect,
1994 	 * so before the consumer code path protect the clock provider
1995 	 */
1996 
1997 	ret = clk_core_set_rate_nolock(clk->core, rate);
1998 	if (!ret) {
1999 		clk_core_rate_protect(clk->core);
2000 		clk->exclusive_count++;
2001 	}
2002 
2003 	clk_prepare_unlock();
2004 
2005 	return ret;
2006 }
2007 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2008 
2009 /**
2010  * clk_set_rate_range - set a rate range for a clock source
2011  * @clk: clock source
2012  * @min: desired minimum clock rate in Hz, inclusive
2013  * @max: desired maximum clock rate in Hz, inclusive
2014  *
2015  * Returns success (0) or negative errno.
2016  */
2017 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2018 {
2019 	int ret = 0;
2020 	unsigned long old_min, old_max, rate;
2021 
2022 	if (!clk)
2023 		return 0;
2024 
2025 	if (min > max) {
2026 		pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2027 		       __func__, clk->core->name, clk->dev_id, clk->con_id,
2028 		       min, max);
2029 		return -EINVAL;
2030 	}
2031 
2032 	clk_prepare_lock();
2033 
2034 	if (clk->exclusive_count)
2035 		clk_core_rate_unprotect(clk->core);
2036 
2037 	/* Save the current values in case we need to rollback the change */
2038 	old_min = clk->min_rate;
2039 	old_max = clk->max_rate;
2040 	clk->min_rate = min;
2041 	clk->max_rate = max;
2042 
2043 	rate = clk_core_get_rate_nolock(clk->core);
2044 	if (rate < min || rate > max) {
2045 		/*
2046 		 * FIXME:
2047 		 * We are in bit of trouble here, current rate is outside the
2048 		 * the requested range. We are going try to request appropriate
2049 		 * range boundary but there is a catch. It may fail for the
2050 		 * usual reason (clock broken, clock protected, etc) but also
2051 		 * because:
2052 		 * - round_rate() was not favorable and fell on the wrong
2053 		 *   side of the boundary
2054 		 * - the determine_rate() callback does not really check for
2055 		 *   this corner case when determining the rate
2056 		 */
2057 
2058 		if (rate < min)
2059 			rate = min;
2060 		else
2061 			rate = max;
2062 
2063 		ret = clk_core_set_rate_nolock(clk->core, rate);
2064 		if (ret) {
2065 			/* rollback the changes */
2066 			clk->min_rate = old_min;
2067 			clk->max_rate = old_max;
2068 		}
2069 	}
2070 
2071 	if (clk->exclusive_count)
2072 		clk_core_rate_protect(clk->core);
2073 
2074 	clk_prepare_unlock();
2075 
2076 	return ret;
2077 }
2078 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2079 
2080 /**
2081  * clk_set_min_rate - set a minimum clock rate for a clock source
2082  * @clk: clock source
2083  * @rate: desired minimum clock rate in Hz, inclusive
2084  *
2085  * Returns success (0) or negative errno.
2086  */
2087 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2088 {
2089 	if (!clk)
2090 		return 0;
2091 
2092 	return clk_set_rate_range(clk, rate, clk->max_rate);
2093 }
2094 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2095 
2096 /**
2097  * clk_set_max_rate - set a maximum clock rate for a clock source
2098  * @clk: clock source
2099  * @rate: desired maximum clock rate in Hz, inclusive
2100  *
2101  * Returns success (0) or negative errno.
2102  */
2103 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2104 {
2105 	if (!clk)
2106 		return 0;
2107 
2108 	return clk_set_rate_range(clk, clk->min_rate, rate);
2109 }
2110 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2111 
2112 /**
2113  * clk_get_parent - return the parent of a clk
2114  * @clk: the clk whose parent gets returned
2115  *
2116  * Simply returns clk->parent.  Returns NULL if clk is NULL.
2117  */
2118 struct clk *clk_get_parent(struct clk *clk)
2119 {
2120 	struct clk *parent;
2121 
2122 	if (!clk)
2123 		return NULL;
2124 
2125 	clk_prepare_lock();
2126 	/* TODO: Create a per-user clk and change callers to call clk_put */
2127 	parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2128 	clk_prepare_unlock();
2129 
2130 	return parent;
2131 }
2132 EXPORT_SYMBOL_GPL(clk_get_parent);
2133 
2134 static struct clk_core *__clk_init_parent(struct clk_core *core)
2135 {
2136 	u8 index = 0;
2137 
2138 	if (core->num_parents > 1 && core->ops->get_parent)
2139 		index = core->ops->get_parent(core->hw);
2140 
2141 	return clk_core_get_parent_by_index(core, index);
2142 }
2143 
2144 static void clk_core_reparent(struct clk_core *core,
2145 				  struct clk_core *new_parent)
2146 {
2147 	clk_reparent(core, new_parent);
2148 	__clk_recalc_accuracies(core);
2149 	__clk_recalc_rates(core, POST_RATE_CHANGE);
2150 }
2151 
2152 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2153 {
2154 	if (!hw)
2155 		return;
2156 
2157 	clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2158 }
2159 
2160 /**
2161  * clk_has_parent - check if a clock is a possible parent for another
2162  * @clk: clock source
2163  * @parent: parent clock source
2164  *
2165  * This function can be used in drivers that need to check that a clock can be
2166  * the parent of another without actually changing the parent.
2167  *
2168  * Returns true if @parent is a possible parent for @clk, false otherwise.
2169  */
2170 bool clk_has_parent(struct clk *clk, struct clk *parent)
2171 {
2172 	struct clk_core *core, *parent_core;
2173 	unsigned int i;
2174 
2175 	/* NULL clocks should be nops, so return success if either is NULL. */
2176 	if (!clk || !parent)
2177 		return true;
2178 
2179 	core = clk->core;
2180 	parent_core = parent->core;
2181 
2182 	/* Optimize for the case where the parent is already the parent. */
2183 	if (core->parent == parent_core)
2184 		return true;
2185 
2186 	for (i = 0; i < core->num_parents; i++)
2187 		if (strcmp(core->parent_names[i], parent_core->name) == 0)
2188 			return true;
2189 
2190 	return false;
2191 }
2192 EXPORT_SYMBOL_GPL(clk_has_parent);
2193 
2194 static int clk_core_set_parent_nolock(struct clk_core *core,
2195 				      struct clk_core *parent)
2196 {
2197 	int ret = 0;
2198 	int p_index = 0;
2199 	unsigned long p_rate = 0;
2200 
2201 	lockdep_assert_held(&prepare_lock);
2202 
2203 	if (!core)
2204 		return 0;
2205 
2206 	if (core->parent == parent)
2207 		return 0;
2208 
2209 	/* verify ops for for multi-parent clks */
2210 	if (core->num_parents > 1 && !core->ops->set_parent)
2211 		return -EPERM;
2212 
2213 	/* check that we are allowed to re-parent if the clock is in use */
2214 	if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2215 		return -EBUSY;
2216 
2217 	if (clk_core_rate_is_protected(core))
2218 		return -EBUSY;
2219 
2220 	/* try finding the new parent index */
2221 	if (parent) {
2222 		p_index = clk_fetch_parent_index(core, parent);
2223 		if (p_index < 0) {
2224 			pr_debug("%s: clk %s can not be parent of clk %s\n",
2225 					__func__, parent->name, core->name);
2226 			return p_index;
2227 		}
2228 		p_rate = parent->rate;
2229 	}
2230 
2231 	ret = clk_pm_runtime_get(core);
2232 	if (ret)
2233 		return ret;
2234 
2235 	/* propagate PRE_RATE_CHANGE notifications */
2236 	ret = __clk_speculate_rates(core, p_rate);
2237 
2238 	/* abort if a driver objects */
2239 	if (ret & NOTIFY_STOP_MASK)
2240 		goto runtime_put;
2241 
2242 	/* do the re-parent */
2243 	ret = __clk_set_parent(core, parent, p_index);
2244 
2245 	/* propagate rate an accuracy recalculation accordingly */
2246 	if (ret) {
2247 		__clk_recalc_rates(core, ABORT_RATE_CHANGE);
2248 	} else {
2249 		__clk_recalc_rates(core, POST_RATE_CHANGE);
2250 		__clk_recalc_accuracies(core);
2251 	}
2252 
2253 runtime_put:
2254 	clk_pm_runtime_put(core);
2255 
2256 	return ret;
2257 }
2258 
2259 /**
2260  * clk_set_parent - switch the parent of a mux clk
2261  * @clk: the mux clk whose input we are switching
2262  * @parent: the new input to clk
2263  *
2264  * Re-parent clk to use parent as its new input source.  If clk is in
2265  * prepared state, the clk will get enabled for the duration of this call. If
2266  * that's not acceptable for a specific clk (Eg: the consumer can't handle
2267  * that, the reparenting is glitchy in hardware, etc), use the
2268  * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2269  *
2270  * After successfully changing clk's parent clk_set_parent will update the
2271  * clk topology, sysfs topology and propagate rate recalculation via
2272  * __clk_recalc_rates.
2273  *
2274  * Returns 0 on success, -EERROR otherwise.
2275  */
2276 int clk_set_parent(struct clk *clk, struct clk *parent)
2277 {
2278 	int ret;
2279 
2280 	if (!clk)
2281 		return 0;
2282 
2283 	clk_prepare_lock();
2284 
2285 	if (clk->exclusive_count)
2286 		clk_core_rate_unprotect(clk->core);
2287 
2288 	ret = clk_core_set_parent_nolock(clk->core,
2289 					 parent ? parent->core : NULL);
2290 
2291 	if (clk->exclusive_count)
2292 		clk_core_rate_protect(clk->core);
2293 
2294 	clk_prepare_unlock();
2295 
2296 	return ret;
2297 }
2298 EXPORT_SYMBOL_GPL(clk_set_parent);
2299 
2300 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2301 {
2302 	int ret = -EINVAL;
2303 
2304 	lockdep_assert_held(&prepare_lock);
2305 
2306 	if (!core)
2307 		return 0;
2308 
2309 	if (clk_core_rate_is_protected(core))
2310 		return -EBUSY;
2311 
2312 	trace_clk_set_phase(core, degrees);
2313 
2314 	if (core->ops->set_phase) {
2315 		ret = core->ops->set_phase(core->hw, degrees);
2316 		if (!ret)
2317 			core->phase = degrees;
2318 	}
2319 
2320 	trace_clk_set_phase_complete(core, degrees);
2321 
2322 	return ret;
2323 }
2324 
2325 /**
2326  * clk_set_phase - adjust the phase shift of a clock signal
2327  * @clk: clock signal source
2328  * @degrees: number of degrees the signal is shifted
2329  *
2330  * Shifts the phase of a clock signal by the specified
2331  * degrees. Returns 0 on success, -EERROR otherwise.
2332  *
2333  * This function makes no distinction about the input or reference
2334  * signal that we adjust the clock signal phase against. For example
2335  * phase locked-loop clock signal generators we may shift phase with
2336  * respect to feedback clock signal input, but for other cases the
2337  * clock phase may be shifted with respect to some other, unspecified
2338  * signal.
2339  *
2340  * Additionally the concept of phase shift does not propagate through
2341  * the clock tree hierarchy, which sets it apart from clock rates and
2342  * clock accuracy. A parent clock phase attribute does not have an
2343  * impact on the phase attribute of a child clock.
2344  */
2345 int clk_set_phase(struct clk *clk, int degrees)
2346 {
2347 	int ret;
2348 
2349 	if (!clk)
2350 		return 0;
2351 
2352 	/* sanity check degrees */
2353 	degrees %= 360;
2354 	if (degrees < 0)
2355 		degrees += 360;
2356 
2357 	clk_prepare_lock();
2358 
2359 	if (clk->exclusive_count)
2360 		clk_core_rate_unprotect(clk->core);
2361 
2362 	ret = clk_core_set_phase_nolock(clk->core, degrees);
2363 
2364 	if (clk->exclusive_count)
2365 		clk_core_rate_protect(clk->core);
2366 
2367 	clk_prepare_unlock();
2368 
2369 	return ret;
2370 }
2371 EXPORT_SYMBOL_GPL(clk_set_phase);
2372 
2373 static int clk_core_get_phase(struct clk_core *core)
2374 {
2375 	int ret;
2376 
2377 	clk_prepare_lock();
2378 	/* Always try to update cached phase if possible */
2379 	if (core->ops->get_phase)
2380 		core->phase = core->ops->get_phase(core->hw);
2381 	ret = core->phase;
2382 	clk_prepare_unlock();
2383 
2384 	return ret;
2385 }
2386 
2387 /**
2388  * clk_get_phase - return the phase shift of a clock signal
2389  * @clk: clock signal source
2390  *
2391  * Returns the phase shift of a clock node in degrees, otherwise returns
2392  * -EERROR.
2393  */
2394 int clk_get_phase(struct clk *clk)
2395 {
2396 	if (!clk)
2397 		return 0;
2398 
2399 	return clk_core_get_phase(clk->core);
2400 }
2401 EXPORT_SYMBOL_GPL(clk_get_phase);
2402 
2403 /**
2404  * clk_is_match - check if two clk's point to the same hardware clock
2405  * @p: clk compared against q
2406  * @q: clk compared against p
2407  *
2408  * Returns true if the two struct clk pointers both point to the same hardware
2409  * clock node. Put differently, returns true if struct clk *p and struct clk *q
2410  * share the same struct clk_core object.
2411  *
2412  * Returns false otherwise. Note that two NULL clks are treated as matching.
2413  */
2414 bool clk_is_match(const struct clk *p, const struct clk *q)
2415 {
2416 	/* trivial case: identical struct clk's or both NULL */
2417 	if (p == q)
2418 		return true;
2419 
2420 	/* true if clk->core pointers match. Avoid dereferencing garbage */
2421 	if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2422 		if (p->core == q->core)
2423 			return true;
2424 
2425 	return false;
2426 }
2427 EXPORT_SYMBOL_GPL(clk_is_match);
2428 
2429 /***        debugfs support        ***/
2430 
2431 #ifdef CONFIG_DEBUG_FS
2432 #include <linux/debugfs.h>
2433 
2434 static struct dentry *rootdir;
2435 static int inited = 0;
2436 static DEFINE_MUTEX(clk_debug_lock);
2437 static HLIST_HEAD(clk_debug_list);
2438 
2439 static struct hlist_head *all_lists[] = {
2440 	&clk_root_list,
2441 	&clk_orphan_list,
2442 	NULL,
2443 };
2444 
2445 static struct hlist_head *orphan_list[] = {
2446 	&clk_orphan_list,
2447 	NULL,
2448 };
2449 
2450 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2451 				 int level)
2452 {
2453 	if (!c)
2454 		return;
2455 
2456 	seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
2457 		   level * 3 + 1, "",
2458 		   30 - level * 3, c->name,
2459 		   c->enable_count, c->prepare_count, c->protect_count,
2460 		   clk_core_get_rate(c), clk_core_get_accuracy(c),
2461 		   clk_core_get_phase(c));
2462 }
2463 
2464 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2465 				     int level)
2466 {
2467 	struct clk_core *child;
2468 
2469 	if (!c)
2470 		return;
2471 
2472 	clk_summary_show_one(s, c, level);
2473 
2474 	hlist_for_each_entry(child, &c->children, child_node)
2475 		clk_summary_show_subtree(s, child, level + 1);
2476 }
2477 
2478 static int clk_summary_show(struct seq_file *s, void *data)
2479 {
2480 	struct clk_core *c;
2481 	struct hlist_head **lists = (struct hlist_head **)s->private;
2482 
2483 	seq_puts(s, "                                 enable  prepare  protect                               \n");
2484 	seq_puts(s, "   clock                          count    count    count        rate   accuracy   phase\n");
2485 	seq_puts(s, "----------------------------------------------------------------------------------------\n");
2486 
2487 	clk_prepare_lock();
2488 
2489 	for (; *lists; lists++)
2490 		hlist_for_each_entry(c, *lists, child_node)
2491 			clk_summary_show_subtree(s, c, 0);
2492 
2493 	clk_prepare_unlock();
2494 
2495 	return 0;
2496 }
2497 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2498 
2499 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2500 {
2501 	if (!c)
2502 		return;
2503 
2504 	/* This should be JSON format, i.e. elements separated with a comma */
2505 	seq_printf(s, "\"%s\": { ", c->name);
2506 	seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2507 	seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2508 	seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2509 	seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2510 	seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2511 	seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2512 }
2513 
2514 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2515 {
2516 	struct clk_core *child;
2517 
2518 	if (!c)
2519 		return;
2520 
2521 	clk_dump_one(s, c, level);
2522 
2523 	hlist_for_each_entry(child, &c->children, child_node) {
2524 		seq_putc(s, ',');
2525 		clk_dump_subtree(s, child, level + 1);
2526 	}
2527 
2528 	seq_putc(s, '}');
2529 }
2530 
2531 static int clk_dump_show(struct seq_file *s, void *data)
2532 {
2533 	struct clk_core *c;
2534 	bool first_node = true;
2535 	struct hlist_head **lists = (struct hlist_head **)s->private;
2536 
2537 	seq_putc(s, '{');
2538 	clk_prepare_lock();
2539 
2540 	for (; *lists; lists++) {
2541 		hlist_for_each_entry(c, *lists, child_node) {
2542 			if (!first_node)
2543 				seq_putc(s, ',');
2544 			first_node = false;
2545 			clk_dump_subtree(s, c, 0);
2546 		}
2547 	}
2548 
2549 	clk_prepare_unlock();
2550 
2551 	seq_puts(s, "}\n");
2552 	return 0;
2553 }
2554 DEFINE_SHOW_ATTRIBUTE(clk_dump);
2555 
2556 static const struct {
2557 	unsigned long flag;
2558 	const char *name;
2559 } clk_flags[] = {
2560 #define ENTRY(f) { f, __stringify(f) }
2561 	ENTRY(CLK_SET_RATE_GATE),
2562 	ENTRY(CLK_SET_PARENT_GATE),
2563 	ENTRY(CLK_SET_RATE_PARENT),
2564 	ENTRY(CLK_IGNORE_UNUSED),
2565 	ENTRY(CLK_IS_BASIC),
2566 	ENTRY(CLK_GET_RATE_NOCACHE),
2567 	ENTRY(CLK_SET_RATE_NO_REPARENT),
2568 	ENTRY(CLK_GET_ACCURACY_NOCACHE),
2569 	ENTRY(CLK_RECALC_NEW_RATES),
2570 	ENTRY(CLK_SET_RATE_UNGATE),
2571 	ENTRY(CLK_IS_CRITICAL),
2572 	ENTRY(CLK_OPS_PARENT_ENABLE),
2573 #undef ENTRY
2574 };
2575 
2576 static int clk_flags_show(struct seq_file *s, void *data)
2577 {
2578 	struct clk_core *core = s->private;
2579 	unsigned long flags = core->flags;
2580 	unsigned int i;
2581 
2582 	for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2583 		if (flags & clk_flags[i].flag) {
2584 			seq_printf(s, "%s\n", clk_flags[i].name);
2585 			flags &= ~clk_flags[i].flag;
2586 		}
2587 	}
2588 	if (flags) {
2589 		/* Unknown flags */
2590 		seq_printf(s, "0x%lx\n", flags);
2591 	}
2592 
2593 	return 0;
2594 }
2595 DEFINE_SHOW_ATTRIBUTE(clk_flags);
2596 
2597 static int possible_parents_show(struct seq_file *s, void *data)
2598 {
2599 	struct clk_core *core = s->private;
2600 	int i;
2601 
2602 	for (i = 0; i < core->num_parents - 1; i++)
2603 		seq_printf(s, "%s ", core->parent_names[i]);
2604 
2605 	seq_printf(s, "%s\n", core->parent_names[i]);
2606 
2607 	return 0;
2608 }
2609 DEFINE_SHOW_ATTRIBUTE(possible_parents);
2610 
2611 static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2612 {
2613 	struct dentry *d;
2614 	int ret = -ENOMEM;
2615 
2616 	if (!core || !pdentry) {
2617 		ret = -EINVAL;
2618 		goto out;
2619 	}
2620 
2621 	d = debugfs_create_dir(core->name, pdentry);
2622 	if (!d)
2623 		goto out;
2624 
2625 	core->dentry = d;
2626 
2627 	d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate);
2628 	if (!d)
2629 		goto err_out;
2630 
2631 	d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry,
2632 				 &core->accuracy);
2633 	if (!d)
2634 		goto err_out;
2635 
2636 	d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase);
2637 	if (!d)
2638 		goto err_out;
2639 
2640 	d = debugfs_create_file("clk_flags", 0444, core->dentry, core,
2641 				&clk_flags_fops);
2642 	if (!d)
2643 		goto err_out;
2644 
2645 	d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
2646 			       &core->prepare_count);
2647 	if (!d)
2648 		goto err_out;
2649 
2650 	d = debugfs_create_u32("clk_enable_count", 0444, core->dentry,
2651 			       &core->enable_count);
2652 	if (!d)
2653 		goto err_out;
2654 
2655 	d = debugfs_create_u32("clk_protect_count", 0444, core->dentry,
2656 			       &core->protect_count);
2657 	if (!d)
2658 		goto err_out;
2659 
2660 	d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
2661 			       &core->notifier_count);
2662 	if (!d)
2663 		goto err_out;
2664 
2665 	if (core->num_parents > 1) {
2666 		d = debugfs_create_file("clk_possible_parents", 0444,
2667 				core->dentry, core, &possible_parents_fops);
2668 		if (!d)
2669 			goto err_out;
2670 	}
2671 
2672 	if (core->ops->debug_init) {
2673 		ret = core->ops->debug_init(core->hw, core->dentry);
2674 		if (ret)
2675 			goto err_out;
2676 	}
2677 
2678 	ret = 0;
2679 	goto out;
2680 
2681 err_out:
2682 	debugfs_remove_recursive(core->dentry);
2683 	core->dentry = NULL;
2684 out:
2685 	return ret;
2686 }
2687 
2688 /**
2689  * clk_debug_register - add a clk node to the debugfs clk directory
2690  * @core: the clk being added to the debugfs clk directory
2691  *
2692  * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2693  * initialized.  Otherwise it bails out early since the debugfs clk directory
2694  * will be created lazily by clk_debug_init as part of a late_initcall.
2695  */
2696 static int clk_debug_register(struct clk_core *core)
2697 {
2698 	int ret = 0;
2699 
2700 	mutex_lock(&clk_debug_lock);
2701 	hlist_add_head(&core->debug_node, &clk_debug_list);
2702 	if (inited)
2703 		ret = clk_debug_create_one(core, rootdir);
2704 	mutex_unlock(&clk_debug_lock);
2705 
2706 	return ret;
2707 }
2708 
2709  /**
2710  * clk_debug_unregister - remove a clk node from the debugfs clk directory
2711  * @core: the clk being removed from the debugfs clk directory
2712  *
2713  * Dynamically removes a clk and all its child nodes from the
2714  * debugfs clk directory if clk->dentry points to debugfs created by
2715  * clk_debug_register in __clk_core_init.
2716  */
2717 static void clk_debug_unregister(struct clk_core *core)
2718 {
2719 	mutex_lock(&clk_debug_lock);
2720 	hlist_del_init(&core->debug_node);
2721 	debugfs_remove_recursive(core->dentry);
2722 	core->dentry = NULL;
2723 	mutex_unlock(&clk_debug_lock);
2724 }
2725 
2726 struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2727 				void *data, const struct file_operations *fops)
2728 {
2729 	struct dentry *d = NULL;
2730 
2731 	if (hw->core->dentry)
2732 		d = debugfs_create_file(name, mode, hw->core->dentry, data,
2733 					fops);
2734 
2735 	return d;
2736 }
2737 EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2738 
2739 /**
2740  * clk_debug_init - lazily populate the debugfs clk directory
2741  *
2742  * clks are often initialized very early during boot before memory can be
2743  * dynamically allocated and well before debugfs is setup. This function
2744  * populates the debugfs clk directory once at boot-time when we know that
2745  * debugfs is setup. It should only be called once at boot-time, all other clks
2746  * added dynamically will be done so with clk_debug_register.
2747  */
2748 static int __init clk_debug_init(void)
2749 {
2750 	struct clk_core *core;
2751 	struct dentry *d;
2752 
2753 	rootdir = debugfs_create_dir("clk", NULL);
2754 
2755 	if (!rootdir)
2756 		return -ENOMEM;
2757 
2758 	d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2759 				&clk_summary_fops);
2760 	if (!d)
2761 		return -ENOMEM;
2762 
2763 	d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2764 				&clk_dump_fops);
2765 	if (!d)
2766 		return -ENOMEM;
2767 
2768 	d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
2769 				&orphan_list, &clk_summary_fops);
2770 	if (!d)
2771 		return -ENOMEM;
2772 
2773 	d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
2774 				&orphan_list, &clk_dump_fops);
2775 	if (!d)
2776 		return -ENOMEM;
2777 
2778 	mutex_lock(&clk_debug_lock);
2779 	hlist_for_each_entry(core, &clk_debug_list, debug_node)
2780 		clk_debug_create_one(core, rootdir);
2781 
2782 	inited = 1;
2783 	mutex_unlock(&clk_debug_lock);
2784 
2785 	return 0;
2786 }
2787 late_initcall(clk_debug_init);
2788 #else
2789 static inline int clk_debug_register(struct clk_core *core) { return 0; }
2790 static inline void clk_debug_reparent(struct clk_core *core,
2791 				      struct clk_core *new_parent)
2792 {
2793 }
2794 static inline void clk_debug_unregister(struct clk_core *core)
2795 {
2796 }
2797 #endif
2798 
2799 /**
2800  * __clk_core_init - initialize the data structures in a struct clk_core
2801  * @core:	clk_core being initialized
2802  *
2803  * Initializes the lists in struct clk_core, queries the hardware for the
2804  * parent and rate and sets them both.
2805  */
2806 static int __clk_core_init(struct clk_core *core)
2807 {
2808 	int i, ret;
2809 	struct clk_core *orphan;
2810 	struct hlist_node *tmp2;
2811 	unsigned long rate;
2812 
2813 	if (!core)
2814 		return -EINVAL;
2815 
2816 	clk_prepare_lock();
2817 
2818 	ret = clk_pm_runtime_get(core);
2819 	if (ret)
2820 		goto unlock;
2821 
2822 	/* check to see if a clock with this name is already registered */
2823 	if (clk_core_lookup(core->name)) {
2824 		pr_debug("%s: clk %s already initialized\n",
2825 				__func__, core->name);
2826 		ret = -EEXIST;
2827 		goto out;
2828 	}
2829 
2830 	/* check that clk_ops are sane.  See Documentation/clk.txt */
2831 	if (core->ops->set_rate &&
2832 	    !((core->ops->round_rate || core->ops->determine_rate) &&
2833 	      core->ops->recalc_rate)) {
2834 		pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2835 		       __func__, core->name);
2836 		ret = -EINVAL;
2837 		goto out;
2838 	}
2839 
2840 	if (core->ops->set_parent && !core->ops->get_parent) {
2841 		pr_err("%s: %s must implement .get_parent & .set_parent\n",
2842 		       __func__, core->name);
2843 		ret = -EINVAL;
2844 		goto out;
2845 	}
2846 
2847 	if (core->num_parents > 1 && !core->ops->get_parent) {
2848 		pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2849 		       __func__, core->name);
2850 		ret = -EINVAL;
2851 		goto out;
2852 	}
2853 
2854 	if (core->ops->set_rate_and_parent &&
2855 			!(core->ops->set_parent && core->ops->set_rate)) {
2856 		pr_err("%s: %s must implement .set_parent & .set_rate\n",
2857 				__func__, core->name);
2858 		ret = -EINVAL;
2859 		goto out;
2860 	}
2861 
2862 	/* throw a WARN if any entries in parent_names are NULL */
2863 	for (i = 0; i < core->num_parents; i++)
2864 		WARN(!core->parent_names[i],
2865 				"%s: invalid NULL in %s's .parent_names\n",
2866 				__func__, core->name);
2867 
2868 	core->parent = __clk_init_parent(core);
2869 
2870 	/*
2871 	 * Populate core->parent if parent has already been clk_core_init'd. If
2872 	 * parent has not yet been clk_core_init'd then place clk in the orphan
2873 	 * list.  If clk doesn't have any parents then place it in the root
2874 	 * clk list.
2875 	 *
2876 	 * Every time a new clk is clk_init'd then we walk the list of orphan
2877 	 * clocks and re-parent any that are children of the clock currently
2878 	 * being clk_init'd.
2879 	 */
2880 	if (core->parent) {
2881 		hlist_add_head(&core->child_node,
2882 				&core->parent->children);
2883 		core->orphan = core->parent->orphan;
2884 	} else if (!core->num_parents) {
2885 		hlist_add_head(&core->child_node, &clk_root_list);
2886 		core->orphan = false;
2887 	} else {
2888 		hlist_add_head(&core->child_node, &clk_orphan_list);
2889 		core->orphan = true;
2890 	}
2891 
2892 	/*
2893 	 * optional platform-specific magic
2894 	 *
2895 	 * The .init callback is not used by any of the basic clock types, but
2896 	 * exists for weird hardware that must perform initialization magic.
2897 	 * Please consider other ways of solving initialization problems before
2898 	 * using this callback, as its use is discouraged.
2899 	 */
2900 	if (core->ops->init)
2901 		core->ops->init(core->hw);
2902 
2903 	/*
2904 	 * Set clk's accuracy.  The preferred method is to use
2905 	 * .recalc_accuracy. For simple clocks and lazy developers the default
2906 	 * fallback is to use the parent's accuracy.  If a clock doesn't have a
2907 	 * parent (or is orphaned) then accuracy is set to zero (perfect
2908 	 * clock).
2909 	 */
2910 	if (core->ops->recalc_accuracy)
2911 		core->accuracy = core->ops->recalc_accuracy(core->hw,
2912 					__clk_get_accuracy(core->parent));
2913 	else if (core->parent)
2914 		core->accuracy = core->parent->accuracy;
2915 	else
2916 		core->accuracy = 0;
2917 
2918 	/*
2919 	 * Set clk's phase.
2920 	 * Since a phase is by definition relative to its parent, just
2921 	 * query the current clock phase, or just assume it's in phase.
2922 	 */
2923 	if (core->ops->get_phase)
2924 		core->phase = core->ops->get_phase(core->hw);
2925 	else
2926 		core->phase = 0;
2927 
2928 	/*
2929 	 * Set clk's rate.  The preferred method is to use .recalc_rate.  For
2930 	 * simple clocks and lazy developers the default fallback is to use the
2931 	 * parent's rate.  If a clock doesn't have a parent (or is orphaned)
2932 	 * then rate is set to zero.
2933 	 */
2934 	if (core->ops->recalc_rate)
2935 		rate = core->ops->recalc_rate(core->hw,
2936 				clk_core_get_rate_nolock(core->parent));
2937 	else if (core->parent)
2938 		rate = core->parent->rate;
2939 	else
2940 		rate = 0;
2941 	core->rate = core->req_rate = rate;
2942 
2943 	/*
2944 	 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
2945 	 * don't get accidentally disabled when walking the orphan tree and
2946 	 * reparenting clocks
2947 	 */
2948 	if (core->flags & CLK_IS_CRITICAL) {
2949 		unsigned long flags;
2950 
2951 		clk_core_prepare(core);
2952 
2953 		flags = clk_enable_lock();
2954 		clk_core_enable(core);
2955 		clk_enable_unlock(flags);
2956 	}
2957 
2958 	/*
2959 	 * walk the list of orphan clocks and reparent any that newly finds a
2960 	 * parent.
2961 	 */
2962 	hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2963 		struct clk_core *parent = __clk_init_parent(orphan);
2964 
2965 		/*
2966 		 * We need to use __clk_set_parent_before() and _after() to
2967 		 * to properly migrate any prepare/enable count of the orphan
2968 		 * clock. This is important for CLK_IS_CRITICAL clocks, which
2969 		 * are enabled during init but might not have a parent yet.
2970 		 */
2971 		if (parent) {
2972 			/* update the clk tree topology */
2973 			__clk_set_parent_before(orphan, parent);
2974 			__clk_set_parent_after(orphan, parent, NULL);
2975 			__clk_recalc_accuracies(orphan);
2976 			__clk_recalc_rates(orphan, 0);
2977 		}
2978 	}
2979 
2980 	kref_init(&core->ref);
2981 out:
2982 	clk_pm_runtime_put(core);
2983 unlock:
2984 	clk_prepare_unlock();
2985 
2986 	if (!ret)
2987 		clk_debug_register(core);
2988 
2989 	return ret;
2990 }
2991 
2992 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2993 			     const char *con_id)
2994 {
2995 	struct clk *clk;
2996 
2997 	/* This is to allow this function to be chained to others */
2998 	if (IS_ERR_OR_NULL(hw))
2999 		return ERR_CAST(hw);
3000 
3001 	clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3002 	if (!clk)
3003 		return ERR_PTR(-ENOMEM);
3004 
3005 	clk->core = hw->core;
3006 	clk->dev_id = dev_id;
3007 	clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3008 	clk->max_rate = ULONG_MAX;
3009 
3010 	clk_prepare_lock();
3011 	hlist_add_head(&clk->clks_node, &hw->core->clks);
3012 	clk_prepare_unlock();
3013 
3014 	return clk;
3015 }
3016 
3017 void __clk_free_clk(struct clk *clk)
3018 {
3019 	clk_prepare_lock();
3020 	hlist_del(&clk->clks_node);
3021 	clk_prepare_unlock();
3022 
3023 	kfree_const(clk->con_id);
3024 	kfree(clk);
3025 }
3026 
3027 /**
3028  * clk_register - allocate a new clock, register it and return an opaque cookie
3029  * @dev: device that is registering this clock
3030  * @hw: link to hardware-specific clock data
3031  *
3032  * clk_register is the primary interface for populating the clock tree with new
3033  * clock nodes.  It returns a pointer to the newly allocated struct clk which
3034  * cannot be dereferenced by driver code but may be used in conjunction with the
3035  * rest of the clock API.  In the event of an error clk_register will return an
3036  * error code; drivers must test for an error code after calling clk_register.
3037  */
3038 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3039 {
3040 	int i, ret;
3041 	struct clk_core *core;
3042 
3043 	core = kzalloc(sizeof(*core), GFP_KERNEL);
3044 	if (!core) {
3045 		ret = -ENOMEM;
3046 		goto fail_out;
3047 	}
3048 
3049 	core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3050 	if (!core->name) {
3051 		ret = -ENOMEM;
3052 		goto fail_name;
3053 	}
3054 
3055 	if (WARN_ON(!hw->init->ops)) {
3056 		ret = -EINVAL;
3057 		goto fail_ops;
3058 	}
3059 	core->ops = hw->init->ops;
3060 
3061 	if (dev && pm_runtime_enabled(dev))
3062 		core->dev = dev;
3063 	if (dev && dev->driver)
3064 		core->owner = dev->driver->owner;
3065 	core->hw = hw;
3066 	core->flags = hw->init->flags;
3067 	core->num_parents = hw->init->num_parents;
3068 	core->min_rate = 0;
3069 	core->max_rate = ULONG_MAX;
3070 	hw->core = core;
3071 
3072 	/* allocate local copy in case parent_names is __initdata */
3073 	core->parent_names = kcalloc(core->num_parents, sizeof(char *),
3074 					GFP_KERNEL);
3075 
3076 	if (!core->parent_names) {
3077 		ret = -ENOMEM;
3078 		goto fail_parent_names;
3079 	}
3080 
3081 
3082 	/* copy each string name in case parent_names is __initdata */
3083 	for (i = 0; i < core->num_parents; i++) {
3084 		core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
3085 						GFP_KERNEL);
3086 		if (!core->parent_names[i]) {
3087 			ret = -ENOMEM;
3088 			goto fail_parent_names_copy;
3089 		}
3090 	}
3091 
3092 	/* avoid unnecessary string look-ups of clk_core's possible parents. */
3093 	core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3094 				GFP_KERNEL);
3095 	if (!core->parents) {
3096 		ret = -ENOMEM;
3097 		goto fail_parents;
3098 	};
3099 
3100 	INIT_HLIST_HEAD(&core->clks);
3101 
3102 	hw->clk = __clk_create_clk(hw, NULL, NULL);
3103 	if (IS_ERR(hw->clk)) {
3104 		ret = PTR_ERR(hw->clk);
3105 		goto fail_parents;
3106 	}
3107 
3108 	ret = __clk_core_init(core);
3109 	if (!ret)
3110 		return hw->clk;
3111 
3112 	__clk_free_clk(hw->clk);
3113 	hw->clk = NULL;
3114 
3115 fail_parents:
3116 	kfree(core->parents);
3117 fail_parent_names_copy:
3118 	while (--i >= 0)
3119 		kfree_const(core->parent_names[i]);
3120 	kfree(core->parent_names);
3121 fail_parent_names:
3122 fail_ops:
3123 	kfree_const(core->name);
3124 fail_name:
3125 	kfree(core);
3126 fail_out:
3127 	return ERR_PTR(ret);
3128 }
3129 EXPORT_SYMBOL_GPL(clk_register);
3130 
3131 /**
3132  * clk_hw_register - register a clk_hw and return an error code
3133  * @dev: device that is registering this clock
3134  * @hw: link to hardware-specific clock data
3135  *
3136  * clk_hw_register is the primary interface for populating the clock tree with
3137  * new clock nodes. It returns an integer equal to zero indicating success or
3138  * less than zero indicating failure. Drivers must test for an error code after
3139  * calling clk_hw_register().
3140  */
3141 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3142 {
3143 	return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3144 }
3145 EXPORT_SYMBOL_GPL(clk_hw_register);
3146 
3147 /* Free memory allocated for a clock. */
3148 static void __clk_release(struct kref *ref)
3149 {
3150 	struct clk_core *core = container_of(ref, struct clk_core, ref);
3151 	int i = core->num_parents;
3152 
3153 	lockdep_assert_held(&prepare_lock);
3154 
3155 	kfree(core->parents);
3156 	while (--i >= 0)
3157 		kfree_const(core->parent_names[i]);
3158 
3159 	kfree(core->parent_names);
3160 	kfree_const(core->name);
3161 	kfree(core);
3162 }
3163 
3164 /*
3165  * Empty clk_ops for unregistered clocks. These are used temporarily
3166  * after clk_unregister() was called on a clock and until last clock
3167  * consumer calls clk_put() and the struct clk object is freed.
3168  */
3169 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3170 {
3171 	return -ENXIO;
3172 }
3173 
3174 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3175 {
3176 	WARN_ON_ONCE(1);
3177 }
3178 
3179 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3180 					unsigned long parent_rate)
3181 {
3182 	return -ENXIO;
3183 }
3184 
3185 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3186 {
3187 	return -ENXIO;
3188 }
3189 
3190 static const struct clk_ops clk_nodrv_ops = {
3191 	.enable		= clk_nodrv_prepare_enable,
3192 	.disable	= clk_nodrv_disable_unprepare,
3193 	.prepare	= clk_nodrv_prepare_enable,
3194 	.unprepare	= clk_nodrv_disable_unprepare,
3195 	.set_rate	= clk_nodrv_set_rate,
3196 	.set_parent	= clk_nodrv_set_parent,
3197 };
3198 
3199 /**
3200  * clk_unregister - unregister a currently registered clock
3201  * @clk: clock to unregister
3202  */
3203 void clk_unregister(struct clk *clk)
3204 {
3205 	unsigned long flags;
3206 
3207 	if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3208 		return;
3209 
3210 	clk_debug_unregister(clk->core);
3211 
3212 	clk_prepare_lock();
3213 
3214 	if (clk->core->ops == &clk_nodrv_ops) {
3215 		pr_err("%s: unregistered clock: %s\n", __func__,
3216 		       clk->core->name);
3217 		goto unlock;
3218 	}
3219 	/*
3220 	 * Assign empty clock ops for consumers that might still hold
3221 	 * a reference to this clock.
3222 	 */
3223 	flags = clk_enable_lock();
3224 	clk->core->ops = &clk_nodrv_ops;
3225 	clk_enable_unlock(flags);
3226 
3227 	if (!hlist_empty(&clk->core->children)) {
3228 		struct clk_core *child;
3229 		struct hlist_node *t;
3230 
3231 		/* Reparent all children to the orphan list. */
3232 		hlist_for_each_entry_safe(child, t, &clk->core->children,
3233 					  child_node)
3234 			clk_core_set_parent_nolock(child, NULL);
3235 	}
3236 
3237 	hlist_del_init(&clk->core->child_node);
3238 
3239 	if (clk->core->prepare_count)
3240 		pr_warn("%s: unregistering prepared clock: %s\n",
3241 					__func__, clk->core->name);
3242 
3243 	if (clk->core->protect_count)
3244 		pr_warn("%s: unregistering protected clock: %s\n",
3245 					__func__, clk->core->name);
3246 
3247 	kref_put(&clk->core->ref, __clk_release);
3248 unlock:
3249 	clk_prepare_unlock();
3250 }
3251 EXPORT_SYMBOL_GPL(clk_unregister);
3252 
3253 /**
3254  * clk_hw_unregister - unregister a currently registered clk_hw
3255  * @hw: hardware-specific clock data to unregister
3256  */
3257 void clk_hw_unregister(struct clk_hw *hw)
3258 {
3259 	clk_unregister(hw->clk);
3260 }
3261 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3262 
3263 static void devm_clk_release(struct device *dev, void *res)
3264 {
3265 	clk_unregister(*(struct clk **)res);
3266 }
3267 
3268 static void devm_clk_hw_release(struct device *dev, void *res)
3269 {
3270 	clk_hw_unregister(*(struct clk_hw **)res);
3271 }
3272 
3273 /**
3274  * devm_clk_register - resource managed clk_register()
3275  * @dev: device that is registering this clock
3276  * @hw: link to hardware-specific clock data
3277  *
3278  * Managed clk_register(). Clocks returned from this function are
3279  * automatically clk_unregister()ed on driver detach. See clk_register() for
3280  * more information.
3281  */
3282 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3283 {
3284 	struct clk *clk;
3285 	struct clk **clkp;
3286 
3287 	clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3288 	if (!clkp)
3289 		return ERR_PTR(-ENOMEM);
3290 
3291 	clk = clk_register(dev, hw);
3292 	if (!IS_ERR(clk)) {
3293 		*clkp = clk;
3294 		devres_add(dev, clkp);
3295 	} else {
3296 		devres_free(clkp);
3297 	}
3298 
3299 	return clk;
3300 }
3301 EXPORT_SYMBOL_GPL(devm_clk_register);
3302 
3303 /**
3304  * devm_clk_hw_register - resource managed clk_hw_register()
3305  * @dev: device that is registering this clock
3306  * @hw: link to hardware-specific clock data
3307  *
3308  * Managed clk_hw_register(). Clocks registered by this function are
3309  * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3310  * for more information.
3311  */
3312 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3313 {
3314 	struct clk_hw **hwp;
3315 	int ret;
3316 
3317 	hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3318 	if (!hwp)
3319 		return -ENOMEM;
3320 
3321 	ret = clk_hw_register(dev, hw);
3322 	if (!ret) {
3323 		*hwp = hw;
3324 		devres_add(dev, hwp);
3325 	} else {
3326 		devres_free(hwp);
3327 	}
3328 
3329 	return ret;
3330 }
3331 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3332 
3333 static int devm_clk_match(struct device *dev, void *res, void *data)
3334 {
3335 	struct clk *c = res;
3336 	if (WARN_ON(!c))
3337 		return 0;
3338 	return c == data;
3339 }
3340 
3341 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3342 {
3343 	struct clk_hw *hw = res;
3344 
3345 	if (WARN_ON(!hw))
3346 		return 0;
3347 	return hw == data;
3348 }
3349 
3350 /**
3351  * devm_clk_unregister - resource managed clk_unregister()
3352  * @clk: clock to unregister
3353  *
3354  * Deallocate a clock allocated with devm_clk_register(). Normally
3355  * this function will not need to be called and the resource management
3356  * code will ensure that the resource is freed.
3357  */
3358 void devm_clk_unregister(struct device *dev, struct clk *clk)
3359 {
3360 	WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3361 }
3362 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3363 
3364 /**
3365  * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3366  * @dev: device that is unregistering the hardware-specific clock data
3367  * @hw: link to hardware-specific clock data
3368  *
3369  * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3370  * this function will not need to be called and the resource management
3371  * code will ensure that the resource is freed.
3372  */
3373 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3374 {
3375 	WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3376 				hw));
3377 }
3378 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3379 
3380 /*
3381  * clkdev helpers
3382  */
3383 int __clk_get(struct clk *clk)
3384 {
3385 	struct clk_core *core = !clk ? NULL : clk->core;
3386 
3387 	if (core) {
3388 		if (!try_module_get(core->owner))
3389 			return 0;
3390 
3391 		kref_get(&core->ref);
3392 	}
3393 	return 1;
3394 }
3395 
3396 void __clk_put(struct clk *clk)
3397 {
3398 	struct module *owner;
3399 
3400 	if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3401 		return;
3402 
3403 	clk_prepare_lock();
3404 
3405 	/*
3406 	 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3407 	 * given user should be balanced with calls to clk_rate_exclusive_put()
3408 	 * and by that same consumer
3409 	 */
3410 	if (WARN_ON(clk->exclusive_count)) {
3411 		/* We voiced our concern, let's sanitize the situation */
3412 		clk->core->protect_count -= (clk->exclusive_count - 1);
3413 		clk_core_rate_unprotect(clk->core);
3414 		clk->exclusive_count = 0;
3415 	}
3416 
3417 	hlist_del(&clk->clks_node);
3418 	if (clk->min_rate > clk->core->req_rate ||
3419 	    clk->max_rate < clk->core->req_rate)
3420 		clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3421 
3422 	owner = clk->core->owner;
3423 	kref_put(&clk->core->ref, __clk_release);
3424 
3425 	clk_prepare_unlock();
3426 
3427 	module_put(owner);
3428 
3429 	kfree(clk);
3430 }
3431 
3432 /***        clk rate change notifiers        ***/
3433 
3434 /**
3435  * clk_notifier_register - add a clk rate change notifier
3436  * @clk: struct clk * to watch
3437  * @nb: struct notifier_block * with callback info
3438  *
3439  * Request notification when clk's rate changes.  This uses an SRCU
3440  * notifier because we want it to block and notifier unregistrations are
3441  * uncommon.  The callbacks associated with the notifier must not
3442  * re-enter into the clk framework by calling any top-level clk APIs;
3443  * this will cause a nested prepare_lock mutex.
3444  *
3445  * In all notification cases (pre, post and abort rate change) the original
3446  * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3447  * and the new frequency is passed via struct clk_notifier_data.new_rate.
3448  *
3449  * clk_notifier_register() must be called from non-atomic context.
3450  * Returns -EINVAL if called with null arguments, -ENOMEM upon
3451  * allocation failure; otherwise, passes along the return value of
3452  * srcu_notifier_chain_register().
3453  */
3454 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3455 {
3456 	struct clk_notifier *cn;
3457 	int ret = -ENOMEM;
3458 
3459 	if (!clk || !nb)
3460 		return -EINVAL;
3461 
3462 	clk_prepare_lock();
3463 
3464 	/* search the list of notifiers for this clk */
3465 	list_for_each_entry(cn, &clk_notifier_list, node)
3466 		if (cn->clk == clk)
3467 			break;
3468 
3469 	/* if clk wasn't in the notifier list, allocate new clk_notifier */
3470 	if (cn->clk != clk) {
3471 		cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3472 		if (!cn)
3473 			goto out;
3474 
3475 		cn->clk = clk;
3476 		srcu_init_notifier_head(&cn->notifier_head);
3477 
3478 		list_add(&cn->node, &clk_notifier_list);
3479 	}
3480 
3481 	ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3482 
3483 	clk->core->notifier_count++;
3484 
3485 out:
3486 	clk_prepare_unlock();
3487 
3488 	return ret;
3489 }
3490 EXPORT_SYMBOL_GPL(clk_notifier_register);
3491 
3492 /**
3493  * clk_notifier_unregister - remove a clk rate change notifier
3494  * @clk: struct clk *
3495  * @nb: struct notifier_block * with callback info
3496  *
3497  * Request no further notification for changes to 'clk' and frees memory
3498  * allocated in clk_notifier_register.
3499  *
3500  * Returns -EINVAL if called with null arguments; otherwise, passes
3501  * along the return value of srcu_notifier_chain_unregister().
3502  */
3503 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3504 {
3505 	struct clk_notifier *cn = NULL;
3506 	int ret = -EINVAL;
3507 
3508 	if (!clk || !nb)
3509 		return -EINVAL;
3510 
3511 	clk_prepare_lock();
3512 
3513 	list_for_each_entry(cn, &clk_notifier_list, node)
3514 		if (cn->clk == clk)
3515 			break;
3516 
3517 	if (cn->clk == clk) {
3518 		ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3519 
3520 		clk->core->notifier_count--;
3521 
3522 		/* XXX the notifier code should handle this better */
3523 		if (!cn->notifier_head.head) {
3524 			srcu_cleanup_notifier_head(&cn->notifier_head);
3525 			list_del(&cn->node);
3526 			kfree(cn);
3527 		}
3528 
3529 	} else {
3530 		ret = -ENOENT;
3531 	}
3532 
3533 	clk_prepare_unlock();
3534 
3535 	return ret;
3536 }
3537 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3538 
3539 #ifdef CONFIG_OF
3540 /**
3541  * struct of_clk_provider - Clock provider registration structure
3542  * @link: Entry in global list of clock providers
3543  * @node: Pointer to device tree node of clock provider
3544  * @get: Get clock callback.  Returns NULL or a struct clk for the
3545  *       given clock specifier
3546  * @data: context pointer to be passed into @get callback
3547  */
3548 struct of_clk_provider {
3549 	struct list_head link;
3550 
3551 	struct device_node *node;
3552 	struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3553 	struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3554 	void *data;
3555 };
3556 
3557 static const struct of_device_id __clk_of_table_sentinel
3558 	__used __section(__clk_of_table_end);
3559 
3560 static LIST_HEAD(of_clk_providers);
3561 static DEFINE_MUTEX(of_clk_mutex);
3562 
3563 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3564 				     void *data)
3565 {
3566 	return data;
3567 }
3568 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3569 
3570 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3571 {
3572 	return data;
3573 }
3574 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3575 
3576 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3577 {
3578 	struct clk_onecell_data *clk_data = data;
3579 	unsigned int idx = clkspec->args[0];
3580 
3581 	if (idx >= clk_data->clk_num) {
3582 		pr_err("%s: invalid clock index %u\n", __func__, idx);
3583 		return ERR_PTR(-EINVAL);
3584 	}
3585 
3586 	return clk_data->clks[idx];
3587 }
3588 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3589 
3590 struct clk_hw *
3591 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3592 {
3593 	struct clk_hw_onecell_data *hw_data = data;
3594 	unsigned int idx = clkspec->args[0];
3595 
3596 	if (idx >= hw_data->num) {
3597 		pr_err("%s: invalid index %u\n", __func__, idx);
3598 		return ERR_PTR(-EINVAL);
3599 	}
3600 
3601 	return hw_data->hws[idx];
3602 }
3603 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3604 
3605 /**
3606  * of_clk_add_provider() - Register a clock provider for a node
3607  * @np: Device node pointer associated with clock provider
3608  * @clk_src_get: callback for decoding clock
3609  * @data: context pointer for @clk_src_get callback.
3610  */
3611 int of_clk_add_provider(struct device_node *np,
3612 			struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3613 						   void *data),
3614 			void *data)
3615 {
3616 	struct of_clk_provider *cp;
3617 	int ret;
3618 
3619 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3620 	if (!cp)
3621 		return -ENOMEM;
3622 
3623 	cp->node = of_node_get(np);
3624 	cp->data = data;
3625 	cp->get = clk_src_get;
3626 
3627 	mutex_lock(&of_clk_mutex);
3628 	list_add(&cp->link, &of_clk_providers);
3629 	mutex_unlock(&of_clk_mutex);
3630 	pr_debug("Added clock from %pOF\n", np);
3631 
3632 	ret = of_clk_set_defaults(np, true);
3633 	if (ret < 0)
3634 		of_clk_del_provider(np);
3635 
3636 	return ret;
3637 }
3638 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3639 
3640 /**
3641  * of_clk_add_hw_provider() - Register a clock provider for a node
3642  * @np: Device node pointer associated with clock provider
3643  * @get: callback for decoding clk_hw
3644  * @data: context pointer for @get callback.
3645  */
3646 int of_clk_add_hw_provider(struct device_node *np,
3647 			   struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3648 						 void *data),
3649 			   void *data)
3650 {
3651 	struct of_clk_provider *cp;
3652 	int ret;
3653 
3654 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3655 	if (!cp)
3656 		return -ENOMEM;
3657 
3658 	cp->node = of_node_get(np);
3659 	cp->data = data;
3660 	cp->get_hw = get;
3661 
3662 	mutex_lock(&of_clk_mutex);
3663 	list_add(&cp->link, &of_clk_providers);
3664 	mutex_unlock(&of_clk_mutex);
3665 	pr_debug("Added clk_hw provider from %pOF\n", np);
3666 
3667 	ret = of_clk_set_defaults(np, true);
3668 	if (ret < 0)
3669 		of_clk_del_provider(np);
3670 
3671 	return ret;
3672 }
3673 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3674 
3675 static void devm_of_clk_release_provider(struct device *dev, void *res)
3676 {
3677 	of_clk_del_provider(*(struct device_node **)res);
3678 }
3679 
3680 int devm_of_clk_add_hw_provider(struct device *dev,
3681 			struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3682 					      void *data),
3683 			void *data)
3684 {
3685 	struct device_node **ptr, *np;
3686 	int ret;
3687 
3688 	ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3689 			   GFP_KERNEL);
3690 	if (!ptr)
3691 		return -ENOMEM;
3692 
3693 	np = dev->of_node;
3694 	ret = of_clk_add_hw_provider(np, get, data);
3695 	if (!ret) {
3696 		*ptr = np;
3697 		devres_add(dev, ptr);
3698 	} else {
3699 		devres_free(ptr);
3700 	}
3701 
3702 	return ret;
3703 }
3704 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3705 
3706 /**
3707  * of_clk_del_provider() - Remove a previously registered clock provider
3708  * @np: Device node pointer associated with clock provider
3709  */
3710 void of_clk_del_provider(struct device_node *np)
3711 {
3712 	struct of_clk_provider *cp;
3713 
3714 	mutex_lock(&of_clk_mutex);
3715 	list_for_each_entry(cp, &of_clk_providers, link) {
3716 		if (cp->node == np) {
3717 			list_del(&cp->link);
3718 			of_node_put(cp->node);
3719 			kfree(cp);
3720 			break;
3721 		}
3722 	}
3723 	mutex_unlock(&of_clk_mutex);
3724 }
3725 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3726 
3727 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3728 {
3729 	struct device_node **np = res;
3730 
3731 	if (WARN_ON(!np || !*np))
3732 		return 0;
3733 
3734 	return *np == data;
3735 }
3736 
3737 void devm_of_clk_del_provider(struct device *dev)
3738 {
3739 	int ret;
3740 
3741 	ret = devres_release(dev, devm_of_clk_release_provider,
3742 			     devm_clk_provider_match, dev->of_node);
3743 
3744 	WARN_ON(ret);
3745 }
3746 EXPORT_SYMBOL(devm_of_clk_del_provider);
3747 
3748 static struct clk_hw *
3749 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3750 			      struct of_phandle_args *clkspec)
3751 {
3752 	struct clk *clk;
3753 
3754 	if (provider->get_hw)
3755 		return provider->get_hw(clkspec, provider->data);
3756 
3757 	clk = provider->get(clkspec, provider->data);
3758 	if (IS_ERR(clk))
3759 		return ERR_CAST(clk);
3760 	return __clk_get_hw(clk);
3761 }
3762 
3763 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3764 				       const char *dev_id, const char *con_id)
3765 {
3766 	struct of_clk_provider *provider;
3767 	struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3768 	struct clk_hw *hw;
3769 
3770 	if (!clkspec)
3771 		return ERR_PTR(-EINVAL);
3772 
3773 	/* Check if we have such a provider in our array */
3774 	mutex_lock(&of_clk_mutex);
3775 	list_for_each_entry(provider, &of_clk_providers, link) {
3776 		if (provider->node == clkspec->np) {
3777 			hw = __of_clk_get_hw_from_provider(provider, clkspec);
3778 			clk = __clk_create_clk(hw, dev_id, con_id);
3779 		}
3780 
3781 		if (!IS_ERR(clk)) {
3782 			if (!__clk_get(clk)) {
3783 				__clk_free_clk(clk);
3784 				clk = ERR_PTR(-ENOENT);
3785 			}
3786 
3787 			break;
3788 		}
3789 	}
3790 	mutex_unlock(&of_clk_mutex);
3791 
3792 	return clk;
3793 }
3794 
3795 /**
3796  * of_clk_get_from_provider() - Lookup a clock from a clock provider
3797  * @clkspec: pointer to a clock specifier data structure
3798  *
3799  * This function looks up a struct clk from the registered list of clock
3800  * providers, an input is a clock specifier data structure as returned
3801  * from the of_parse_phandle_with_args() function call.
3802  */
3803 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3804 {
3805 	return __of_clk_get_from_provider(clkspec, NULL, __func__);
3806 }
3807 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3808 
3809 /**
3810  * of_clk_get_parent_count() - Count the number of clocks a device node has
3811  * @np: device node to count
3812  *
3813  * Returns: The number of clocks that are possible parents of this node
3814  */
3815 unsigned int of_clk_get_parent_count(struct device_node *np)
3816 {
3817 	int count;
3818 
3819 	count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3820 	if (count < 0)
3821 		return 0;
3822 
3823 	return count;
3824 }
3825 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3826 
3827 const char *of_clk_get_parent_name(struct device_node *np, int index)
3828 {
3829 	struct of_phandle_args clkspec;
3830 	struct property *prop;
3831 	const char *clk_name;
3832 	const __be32 *vp;
3833 	u32 pv;
3834 	int rc;
3835 	int count;
3836 	struct clk *clk;
3837 
3838 	rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3839 					&clkspec);
3840 	if (rc)
3841 		return NULL;
3842 
3843 	index = clkspec.args_count ? clkspec.args[0] : 0;
3844 	count = 0;
3845 
3846 	/* if there is an indices property, use it to transfer the index
3847 	 * specified into an array offset for the clock-output-names property.
3848 	 */
3849 	of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3850 		if (index == pv) {
3851 			index = count;
3852 			break;
3853 		}
3854 		count++;
3855 	}
3856 	/* We went off the end of 'clock-indices' without finding it */
3857 	if (prop && !vp)
3858 		return NULL;
3859 
3860 	if (of_property_read_string_index(clkspec.np, "clock-output-names",
3861 					  index,
3862 					  &clk_name) < 0) {
3863 		/*
3864 		 * Best effort to get the name if the clock has been
3865 		 * registered with the framework. If the clock isn't
3866 		 * registered, we return the node name as the name of
3867 		 * the clock as long as #clock-cells = 0.
3868 		 */
3869 		clk = of_clk_get_from_provider(&clkspec);
3870 		if (IS_ERR(clk)) {
3871 			if (clkspec.args_count == 0)
3872 				clk_name = clkspec.np->name;
3873 			else
3874 				clk_name = NULL;
3875 		} else {
3876 			clk_name = __clk_get_name(clk);
3877 			clk_put(clk);
3878 		}
3879 	}
3880 
3881 
3882 	of_node_put(clkspec.np);
3883 	return clk_name;
3884 }
3885 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3886 
3887 /**
3888  * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3889  * number of parents
3890  * @np: Device node pointer associated with clock provider
3891  * @parents: pointer to char array that hold the parents' names
3892  * @size: size of the @parents array
3893  *
3894  * Return: number of parents for the clock node.
3895  */
3896 int of_clk_parent_fill(struct device_node *np, const char **parents,
3897 		       unsigned int size)
3898 {
3899 	unsigned int i = 0;
3900 
3901 	while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3902 		i++;
3903 
3904 	return i;
3905 }
3906 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3907 
3908 struct clock_provider {
3909 	of_clk_init_cb_t clk_init_cb;
3910 	struct device_node *np;
3911 	struct list_head node;
3912 };
3913 
3914 /*
3915  * This function looks for a parent clock. If there is one, then it
3916  * checks that the provider for this parent clock was initialized, in
3917  * this case the parent clock will be ready.
3918  */
3919 static int parent_ready(struct device_node *np)
3920 {
3921 	int i = 0;
3922 
3923 	while (true) {
3924 		struct clk *clk = of_clk_get(np, i);
3925 
3926 		/* this parent is ready we can check the next one */
3927 		if (!IS_ERR(clk)) {
3928 			clk_put(clk);
3929 			i++;
3930 			continue;
3931 		}
3932 
3933 		/* at least one parent is not ready, we exit now */
3934 		if (PTR_ERR(clk) == -EPROBE_DEFER)
3935 			return 0;
3936 
3937 		/*
3938 		 * Here we make assumption that the device tree is
3939 		 * written correctly. So an error means that there is
3940 		 * no more parent. As we didn't exit yet, then the
3941 		 * previous parent are ready. If there is no clock
3942 		 * parent, no need to wait for them, then we can
3943 		 * consider their absence as being ready
3944 		 */
3945 		return 1;
3946 	}
3947 }
3948 
3949 /**
3950  * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3951  * @np: Device node pointer associated with clock provider
3952  * @index: clock index
3953  * @flags: pointer to top-level framework flags
3954  *
3955  * Detects if the clock-critical property exists and, if so, sets the
3956  * corresponding CLK_IS_CRITICAL flag.
3957  *
3958  * Do not use this function. It exists only for legacy Device Tree
3959  * bindings, such as the one-clock-per-node style that are outdated.
3960  * Those bindings typically put all clock data into .dts and the Linux
3961  * driver has no clock data, thus making it impossible to set this flag
3962  * correctly from the driver. Only those drivers may call
3963  * of_clk_detect_critical from their setup functions.
3964  *
3965  * Return: error code or zero on success
3966  */
3967 int of_clk_detect_critical(struct device_node *np,
3968 					  int index, unsigned long *flags)
3969 {
3970 	struct property *prop;
3971 	const __be32 *cur;
3972 	uint32_t idx;
3973 
3974 	if (!np || !flags)
3975 		return -EINVAL;
3976 
3977 	of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3978 		if (index == idx)
3979 			*flags |= CLK_IS_CRITICAL;
3980 
3981 	return 0;
3982 }
3983 
3984 /**
3985  * of_clk_init() - Scan and init clock providers from the DT
3986  * @matches: array of compatible values and init functions for providers.
3987  *
3988  * This function scans the device tree for matching clock providers
3989  * and calls their initialization functions. It also does it by trying
3990  * to follow the dependencies.
3991  */
3992 void __init of_clk_init(const struct of_device_id *matches)
3993 {
3994 	const struct of_device_id *match;
3995 	struct device_node *np;
3996 	struct clock_provider *clk_provider, *next;
3997 	bool is_init_done;
3998 	bool force = false;
3999 	LIST_HEAD(clk_provider_list);
4000 
4001 	if (!matches)
4002 		matches = &__clk_of_table;
4003 
4004 	/* First prepare the list of the clocks providers */
4005 	for_each_matching_node_and_match(np, matches, &match) {
4006 		struct clock_provider *parent;
4007 
4008 		if (!of_device_is_available(np))
4009 			continue;
4010 
4011 		parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4012 		if (!parent) {
4013 			list_for_each_entry_safe(clk_provider, next,
4014 						 &clk_provider_list, node) {
4015 				list_del(&clk_provider->node);
4016 				of_node_put(clk_provider->np);
4017 				kfree(clk_provider);
4018 			}
4019 			of_node_put(np);
4020 			return;
4021 		}
4022 
4023 		parent->clk_init_cb = match->data;
4024 		parent->np = of_node_get(np);
4025 		list_add_tail(&parent->node, &clk_provider_list);
4026 	}
4027 
4028 	while (!list_empty(&clk_provider_list)) {
4029 		is_init_done = false;
4030 		list_for_each_entry_safe(clk_provider, next,
4031 					&clk_provider_list, node) {
4032 			if (force || parent_ready(clk_provider->np)) {
4033 
4034 				/* Don't populate platform devices */
4035 				of_node_set_flag(clk_provider->np,
4036 						 OF_POPULATED);
4037 
4038 				clk_provider->clk_init_cb(clk_provider->np);
4039 				of_clk_set_defaults(clk_provider->np, true);
4040 
4041 				list_del(&clk_provider->node);
4042 				of_node_put(clk_provider->np);
4043 				kfree(clk_provider);
4044 				is_init_done = true;
4045 			}
4046 		}
4047 
4048 		/*
4049 		 * We didn't manage to initialize any of the
4050 		 * remaining providers during the last loop, so now we
4051 		 * initialize all the remaining ones unconditionally
4052 		 * in case the clock parent was not mandatory
4053 		 */
4054 		if (!is_init_done)
4055 			force = true;
4056 	}
4057 }
4058 #endif
4059