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