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