Linux 4.19.133
[linux/fpc-iii.git] / drivers / clk / clk.c
blob8353ab9bd31bd541ec983cbda1602838cac87642
1 /*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
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.
9 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
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>
28 #include "clk.h"
30 static DEFINE_SPINLOCK(enable_lock);
31 static DEFINE_MUTEX(prepare_lock);
33 static struct task_struct *prepare_owner;
34 static struct task_struct *enable_owner;
36 static int prepare_refcnt;
37 static int enable_refcnt;
39 static HLIST_HEAD(clk_root_list);
40 static HLIST_HEAD(clk_orphan_list);
41 static LIST_HEAD(clk_notifier_list);
43 /*** private data structures ***/
45 struct clk_core {
46 const char *name;
47 const struct clk_ops *ops;
48 struct clk_hw *hw;
49 struct module *owner;
50 struct device *dev;
51 struct clk_core *parent;
52 const char **parent_names;
53 struct clk_core **parents;
54 u8 num_parents;
55 u8 new_parent_index;
56 unsigned long rate;
57 unsigned long req_rate;
58 unsigned long new_rate;
59 struct clk_core *new_parent;
60 struct clk_core *new_child;
61 unsigned long flags;
62 bool orphan;
63 unsigned int enable_count;
64 unsigned int prepare_count;
65 unsigned int protect_count;
66 unsigned long min_rate;
67 unsigned long max_rate;
68 unsigned long accuracy;
69 int phase;
70 struct clk_duty duty;
71 struct hlist_head children;
72 struct hlist_node child_node;
73 struct hlist_head clks;
74 unsigned int notifier_count;
75 #ifdef CONFIG_DEBUG_FS
76 struct dentry *dentry;
77 struct hlist_node debug_node;
78 #endif
79 struct kref ref;
82 #define CREATE_TRACE_POINTS
83 #include <trace/events/clk.h>
85 struct clk {
86 struct clk_core *core;
87 const char *dev_id;
88 const char *con_id;
89 unsigned long min_rate;
90 unsigned long max_rate;
91 unsigned int exclusive_count;
92 struct hlist_node clks_node;
95 /*** runtime pm ***/
96 static int clk_pm_runtime_get(struct clk_core *core)
98 int ret = 0;
100 if (!core->dev)
101 return 0;
103 ret = pm_runtime_get_sync(core->dev);
104 if (ret < 0) {
105 pm_runtime_put_noidle(core->dev);
106 return ret;
108 return 0;
111 static void clk_pm_runtime_put(struct clk_core *core)
113 if (!core->dev)
114 return;
116 pm_runtime_put_sync(core->dev);
119 /*** locking ***/
120 static void clk_prepare_lock(void)
122 if (!mutex_trylock(&prepare_lock)) {
123 if (prepare_owner == current) {
124 prepare_refcnt++;
125 return;
127 mutex_lock(&prepare_lock);
129 WARN_ON_ONCE(prepare_owner != NULL);
130 WARN_ON_ONCE(prepare_refcnt != 0);
131 prepare_owner = current;
132 prepare_refcnt = 1;
135 static void clk_prepare_unlock(void)
137 WARN_ON_ONCE(prepare_owner != current);
138 WARN_ON_ONCE(prepare_refcnt == 0);
140 if (--prepare_refcnt)
141 return;
142 prepare_owner = NULL;
143 mutex_unlock(&prepare_lock);
146 static unsigned long clk_enable_lock(void)
147 __acquires(enable_lock)
149 unsigned long flags;
152 * On UP systems, spin_trylock_irqsave() always returns true, even if
153 * we already hold the lock. So, in that case, we rely only on
154 * reference counting.
156 if (!IS_ENABLED(CONFIG_SMP) ||
157 !spin_trylock_irqsave(&enable_lock, flags)) {
158 if (enable_owner == current) {
159 enable_refcnt++;
160 __acquire(enable_lock);
161 if (!IS_ENABLED(CONFIG_SMP))
162 local_save_flags(flags);
163 return flags;
165 spin_lock_irqsave(&enable_lock, flags);
167 WARN_ON_ONCE(enable_owner != NULL);
168 WARN_ON_ONCE(enable_refcnt != 0);
169 enable_owner = current;
170 enable_refcnt = 1;
171 return flags;
174 static void clk_enable_unlock(unsigned long flags)
175 __releases(enable_lock)
177 WARN_ON_ONCE(enable_owner != current);
178 WARN_ON_ONCE(enable_refcnt == 0);
180 if (--enable_refcnt) {
181 __release(enable_lock);
182 return;
184 enable_owner = NULL;
185 spin_unlock_irqrestore(&enable_lock, flags);
188 static bool clk_core_rate_is_protected(struct clk_core *core)
190 return core->protect_count;
193 static bool clk_core_is_prepared(struct clk_core *core)
195 bool ret = false;
198 * .is_prepared is optional for clocks that can prepare
199 * fall back to software usage counter if it is missing
201 if (!core->ops->is_prepared)
202 return core->prepare_count;
204 if (!clk_pm_runtime_get(core)) {
205 ret = core->ops->is_prepared(core->hw);
206 clk_pm_runtime_put(core);
209 return ret;
212 static bool clk_core_is_enabled(struct clk_core *core)
214 bool ret = false;
217 * .is_enabled is only mandatory for clocks that gate
218 * fall back to software usage counter if .is_enabled is missing
220 if (!core->ops->is_enabled)
221 return core->enable_count;
224 * Check if clock controller's device is runtime active before
225 * calling .is_enabled callback. If not, assume that clock is
226 * disabled, because we might be called from atomic context, from
227 * which pm_runtime_get() is not allowed.
228 * This function is called mainly from clk_disable_unused_subtree,
229 * which ensures proper runtime pm activation of controller before
230 * taking enable spinlock, but the below check is needed if one tries
231 * to call it from other places.
233 if (core->dev) {
234 pm_runtime_get_noresume(core->dev);
235 if (!pm_runtime_active(core->dev)) {
236 ret = false;
237 goto done;
241 ret = core->ops->is_enabled(core->hw);
242 done:
243 if (core->dev)
244 pm_runtime_put(core->dev);
246 return ret;
249 /*** helper functions ***/
251 const char *__clk_get_name(const struct clk *clk)
253 return !clk ? NULL : clk->core->name;
255 EXPORT_SYMBOL_GPL(__clk_get_name);
257 const char *clk_hw_get_name(const struct clk_hw *hw)
259 return hw->core->name;
261 EXPORT_SYMBOL_GPL(clk_hw_get_name);
263 struct clk_hw *__clk_get_hw(struct clk *clk)
265 return !clk ? NULL : clk->core->hw;
267 EXPORT_SYMBOL_GPL(__clk_get_hw);
269 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
271 return hw->core->num_parents;
273 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
275 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
277 return hw->core->parent ? hw->core->parent->hw : NULL;
279 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
281 static struct clk_core *__clk_lookup_subtree(const char *name,
282 struct clk_core *core)
284 struct clk_core *child;
285 struct clk_core *ret;
287 if (!strcmp(core->name, name))
288 return core;
290 hlist_for_each_entry(child, &core->children, child_node) {
291 ret = __clk_lookup_subtree(name, child);
292 if (ret)
293 return ret;
296 return NULL;
299 static struct clk_core *clk_core_lookup(const char *name)
301 struct clk_core *root_clk;
302 struct clk_core *ret;
304 if (!name)
305 return NULL;
307 /* search the 'proper' clk tree first */
308 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
309 ret = __clk_lookup_subtree(name, root_clk);
310 if (ret)
311 return ret;
314 /* if not found, then search the orphan tree */
315 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
316 ret = __clk_lookup_subtree(name, root_clk);
317 if (ret)
318 return ret;
321 return NULL;
324 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
325 u8 index)
327 if (!core || index >= core->num_parents)
328 return NULL;
330 if (!core->parents[index])
331 core->parents[index] =
332 clk_core_lookup(core->parent_names[index]);
334 return core->parents[index];
337 struct clk_hw *
338 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
340 struct clk_core *parent;
342 parent = clk_core_get_parent_by_index(hw->core, index);
344 return !parent ? NULL : parent->hw;
346 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
348 unsigned int __clk_get_enable_count(struct clk *clk)
350 return !clk ? 0 : clk->core->enable_count;
353 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
355 unsigned long ret;
357 if (!core) {
358 ret = 0;
359 goto out;
362 ret = core->rate;
364 if (!core->num_parents)
365 goto out;
367 if (!core->parent)
368 ret = 0;
370 out:
371 return ret;
374 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
376 return clk_core_get_rate_nolock(hw->core);
378 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
380 static unsigned long __clk_get_accuracy(struct clk_core *core)
382 if (!core)
383 return 0;
385 return core->accuracy;
388 unsigned long __clk_get_flags(struct clk *clk)
390 return !clk ? 0 : clk->core->flags;
392 EXPORT_SYMBOL_GPL(__clk_get_flags);
394 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
396 return hw->core->flags;
398 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
400 bool clk_hw_is_prepared(const struct clk_hw *hw)
402 return clk_core_is_prepared(hw->core);
405 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
407 return clk_core_rate_is_protected(hw->core);
410 bool clk_hw_is_enabled(const struct clk_hw *hw)
412 return clk_core_is_enabled(hw->core);
415 bool __clk_is_enabled(struct clk *clk)
417 if (!clk)
418 return false;
420 return clk_core_is_enabled(clk->core);
422 EXPORT_SYMBOL_GPL(__clk_is_enabled);
424 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
425 unsigned long best, unsigned long flags)
427 if (flags & CLK_MUX_ROUND_CLOSEST)
428 return abs(now - rate) < abs(best - rate);
430 return now <= rate && now > best;
433 int clk_mux_determine_rate_flags(struct clk_hw *hw,
434 struct clk_rate_request *req,
435 unsigned long flags)
437 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
438 int i, num_parents, ret;
439 unsigned long best = 0;
440 struct clk_rate_request parent_req = *req;
442 /* if NO_REPARENT flag set, pass through to current parent */
443 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
444 parent = core->parent;
445 if (core->flags & CLK_SET_RATE_PARENT) {
446 ret = __clk_determine_rate(parent ? parent->hw : NULL,
447 &parent_req);
448 if (ret)
449 return ret;
451 best = parent_req.rate;
452 } else if (parent) {
453 best = clk_core_get_rate_nolock(parent);
454 } else {
455 best = clk_core_get_rate_nolock(core);
458 goto out;
461 /* find the parent that can provide the fastest rate <= rate */
462 num_parents = core->num_parents;
463 for (i = 0; i < num_parents; i++) {
464 parent = clk_core_get_parent_by_index(core, i);
465 if (!parent)
466 continue;
468 if (core->flags & CLK_SET_RATE_PARENT) {
469 parent_req = *req;
470 ret = __clk_determine_rate(parent->hw, &parent_req);
471 if (ret)
472 continue;
473 } else {
474 parent_req.rate = clk_core_get_rate_nolock(parent);
477 if (mux_is_better_rate(req->rate, parent_req.rate,
478 best, flags)) {
479 best_parent = parent;
480 best = parent_req.rate;
484 if (!best_parent)
485 return -EINVAL;
487 out:
488 if (best_parent)
489 req->best_parent_hw = best_parent->hw;
490 req->best_parent_rate = best;
491 req->rate = best;
493 return 0;
495 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
497 struct clk *__clk_lookup(const char *name)
499 struct clk_core *core = clk_core_lookup(name);
501 return !core ? NULL : core->hw->clk;
504 static void clk_core_get_boundaries(struct clk_core *core,
505 unsigned long *min_rate,
506 unsigned long *max_rate)
508 struct clk *clk_user;
510 *min_rate = core->min_rate;
511 *max_rate = core->max_rate;
513 hlist_for_each_entry(clk_user, &core->clks, clks_node)
514 *min_rate = max(*min_rate, clk_user->min_rate);
516 hlist_for_each_entry(clk_user, &core->clks, clks_node)
517 *max_rate = min(*max_rate, clk_user->max_rate);
520 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
521 unsigned long max_rate)
523 hw->core->min_rate = min_rate;
524 hw->core->max_rate = max_rate;
526 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
529 * Helper for finding best parent to provide a given frequency. This can be used
530 * directly as a determine_rate callback (e.g. for a mux), or from a more
531 * complex clock that may combine a mux with other operations.
533 int __clk_mux_determine_rate(struct clk_hw *hw,
534 struct clk_rate_request *req)
536 return clk_mux_determine_rate_flags(hw, req, 0);
538 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
540 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
541 struct clk_rate_request *req)
543 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
545 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
547 /*** clk api ***/
549 static void clk_core_rate_unprotect(struct clk_core *core)
551 lockdep_assert_held(&prepare_lock);
553 if (!core)
554 return;
556 if (WARN(core->protect_count == 0,
557 "%s already unprotected\n", core->name))
558 return;
560 if (--core->protect_count > 0)
561 return;
563 clk_core_rate_unprotect(core->parent);
566 static int clk_core_rate_nuke_protect(struct clk_core *core)
568 int ret;
570 lockdep_assert_held(&prepare_lock);
572 if (!core)
573 return -EINVAL;
575 if (core->protect_count == 0)
576 return 0;
578 ret = core->protect_count;
579 core->protect_count = 1;
580 clk_core_rate_unprotect(core);
582 return ret;
586 * clk_rate_exclusive_put - release exclusivity over clock rate control
587 * @clk: the clk over which the exclusivity is released
589 * clk_rate_exclusive_put() completes a critical section during which a clock
590 * consumer cannot tolerate any other consumer making any operation on the
591 * clock which could result in a rate change or rate glitch. Exclusive clocks
592 * cannot have their rate changed, either directly or indirectly due to changes
593 * further up the parent chain of clocks. As a result, clocks up parent chain
594 * also get under exclusive control of the calling consumer.
596 * If exlusivity is claimed more than once on clock, even by the same consumer,
597 * the rate effectively gets locked as exclusivity can't be preempted.
599 * Calls to clk_rate_exclusive_put() must be balanced with calls to
600 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
601 * error status.
603 void clk_rate_exclusive_put(struct clk *clk)
605 if (!clk)
606 return;
608 clk_prepare_lock();
611 * if there is something wrong with this consumer protect count, stop
612 * here before messing with the provider
614 if (WARN_ON(clk->exclusive_count <= 0))
615 goto out;
617 clk_core_rate_unprotect(clk->core);
618 clk->exclusive_count--;
619 out:
620 clk_prepare_unlock();
622 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
624 static void clk_core_rate_protect(struct clk_core *core)
626 lockdep_assert_held(&prepare_lock);
628 if (!core)
629 return;
631 if (core->protect_count == 0)
632 clk_core_rate_protect(core->parent);
634 core->protect_count++;
637 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
639 lockdep_assert_held(&prepare_lock);
641 if (!core)
642 return;
644 if (count == 0)
645 return;
647 clk_core_rate_protect(core);
648 core->protect_count = count;
652 * clk_rate_exclusive_get - get exclusivity over the clk rate control
653 * @clk: the clk over which the exclusity of rate control is requested
655 * clk_rate_exlusive_get() begins a critical section during which a clock
656 * consumer cannot tolerate any other consumer making any operation on the
657 * clock which could result in a rate change or rate glitch. Exclusive clocks
658 * cannot have their rate changed, either directly or indirectly due to changes
659 * further up the parent chain of clocks. As a result, clocks up parent chain
660 * also get under exclusive control of the calling consumer.
662 * If exlusivity is claimed more than once on clock, even by the same consumer,
663 * the rate effectively gets locked as exclusivity can't be preempted.
665 * Calls to clk_rate_exclusive_get() should be balanced with calls to
666 * clk_rate_exclusive_put(). Calls to this function may sleep.
667 * Returns 0 on success, -EERROR otherwise
669 int clk_rate_exclusive_get(struct clk *clk)
671 if (!clk)
672 return 0;
674 clk_prepare_lock();
675 clk_core_rate_protect(clk->core);
676 clk->exclusive_count++;
677 clk_prepare_unlock();
679 return 0;
681 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
683 static void clk_core_unprepare(struct clk_core *core)
685 lockdep_assert_held(&prepare_lock);
687 if (!core)
688 return;
690 if (WARN(core->prepare_count == 0,
691 "%s already unprepared\n", core->name))
692 return;
694 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
695 "Unpreparing critical %s\n", core->name))
696 return;
698 if (core->flags & CLK_SET_RATE_GATE)
699 clk_core_rate_unprotect(core);
701 if (--core->prepare_count > 0)
702 return;
704 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
706 trace_clk_unprepare(core);
708 if (core->ops->unprepare)
709 core->ops->unprepare(core->hw);
711 clk_pm_runtime_put(core);
713 trace_clk_unprepare_complete(core);
714 clk_core_unprepare(core->parent);
717 static void clk_core_unprepare_lock(struct clk_core *core)
719 clk_prepare_lock();
720 clk_core_unprepare(core);
721 clk_prepare_unlock();
725 * clk_unprepare - undo preparation of a clock source
726 * @clk: the clk being unprepared
728 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
729 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
730 * if the operation may sleep. One example is a clk which is accessed over
731 * I2c. In the complex case a clk gate operation may require a fast and a slow
732 * part. It is this reason that clk_unprepare and clk_disable are not mutually
733 * exclusive. In fact clk_disable must be called before clk_unprepare.
735 void clk_unprepare(struct clk *clk)
737 if (IS_ERR_OR_NULL(clk))
738 return;
740 clk_core_unprepare_lock(clk->core);
742 EXPORT_SYMBOL_GPL(clk_unprepare);
744 static int clk_core_prepare(struct clk_core *core)
746 int ret = 0;
748 lockdep_assert_held(&prepare_lock);
750 if (!core)
751 return 0;
753 if (core->prepare_count == 0) {
754 ret = clk_pm_runtime_get(core);
755 if (ret)
756 return ret;
758 ret = clk_core_prepare(core->parent);
759 if (ret)
760 goto runtime_put;
762 trace_clk_prepare(core);
764 if (core->ops->prepare)
765 ret = core->ops->prepare(core->hw);
767 trace_clk_prepare_complete(core);
769 if (ret)
770 goto unprepare;
773 core->prepare_count++;
776 * CLK_SET_RATE_GATE is a special case of clock protection
777 * Instead of a consumer claiming exclusive rate control, it is
778 * actually the provider which prevents any consumer from making any
779 * operation which could result in a rate change or rate glitch while
780 * the clock is prepared.
782 if (core->flags & CLK_SET_RATE_GATE)
783 clk_core_rate_protect(core);
785 return 0;
786 unprepare:
787 clk_core_unprepare(core->parent);
788 runtime_put:
789 clk_pm_runtime_put(core);
790 return ret;
793 static int clk_core_prepare_lock(struct clk_core *core)
795 int ret;
797 clk_prepare_lock();
798 ret = clk_core_prepare(core);
799 clk_prepare_unlock();
801 return ret;
805 * clk_prepare - prepare a clock source
806 * @clk: the clk being prepared
808 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
809 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
810 * operation may sleep. One example is a clk which is accessed over I2c. In
811 * the complex case a clk ungate operation may require a fast and a slow part.
812 * It is this reason that clk_prepare and clk_enable are not mutually
813 * exclusive. In fact clk_prepare must be called before clk_enable.
814 * Returns 0 on success, -EERROR otherwise.
816 int clk_prepare(struct clk *clk)
818 if (!clk)
819 return 0;
821 return clk_core_prepare_lock(clk->core);
823 EXPORT_SYMBOL_GPL(clk_prepare);
825 static void clk_core_disable(struct clk_core *core)
827 lockdep_assert_held(&enable_lock);
829 if (!core)
830 return;
832 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
833 return;
835 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
836 "Disabling critical %s\n", core->name))
837 return;
839 if (--core->enable_count > 0)
840 return;
842 trace_clk_disable_rcuidle(core);
844 if (core->ops->disable)
845 core->ops->disable(core->hw);
847 trace_clk_disable_complete_rcuidle(core);
849 clk_core_disable(core->parent);
852 static void clk_core_disable_lock(struct clk_core *core)
854 unsigned long flags;
856 flags = clk_enable_lock();
857 clk_core_disable(core);
858 clk_enable_unlock(flags);
862 * clk_disable - gate a clock
863 * @clk: the clk being gated
865 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
866 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
867 * clk if the operation is fast and will never sleep. One example is a
868 * SoC-internal clk which is controlled via simple register writes. In the
869 * complex case a clk gate operation may require a fast and a slow part. It is
870 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
871 * In fact clk_disable must be called before clk_unprepare.
873 void clk_disable(struct clk *clk)
875 if (IS_ERR_OR_NULL(clk))
876 return;
878 clk_core_disable_lock(clk->core);
880 EXPORT_SYMBOL_GPL(clk_disable);
882 static int clk_core_enable(struct clk_core *core)
884 int ret = 0;
886 lockdep_assert_held(&enable_lock);
888 if (!core)
889 return 0;
891 if (WARN(core->prepare_count == 0,
892 "Enabling unprepared %s\n", core->name))
893 return -ESHUTDOWN;
895 if (core->enable_count == 0) {
896 ret = clk_core_enable(core->parent);
898 if (ret)
899 return ret;
901 trace_clk_enable_rcuidle(core);
903 if (core->ops->enable)
904 ret = core->ops->enable(core->hw);
906 trace_clk_enable_complete_rcuidle(core);
908 if (ret) {
909 clk_core_disable(core->parent);
910 return ret;
914 core->enable_count++;
915 return 0;
918 static int clk_core_enable_lock(struct clk_core *core)
920 unsigned long flags;
921 int ret;
923 flags = clk_enable_lock();
924 ret = clk_core_enable(core);
925 clk_enable_unlock(flags);
927 return ret;
931 * clk_enable - ungate a clock
932 * @clk: the clk being ungated
934 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
935 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
936 * if the operation will never sleep. One example is a SoC-internal clk which
937 * is controlled via simple register writes. In the complex case a clk ungate
938 * operation may require a fast and a slow part. It is this reason that
939 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
940 * must be called before clk_enable. Returns 0 on success, -EERROR
941 * otherwise.
943 int clk_enable(struct clk *clk)
945 if (!clk)
946 return 0;
948 return clk_core_enable_lock(clk->core);
950 EXPORT_SYMBOL_GPL(clk_enable);
952 static int clk_core_prepare_enable(struct clk_core *core)
954 int ret;
956 ret = clk_core_prepare_lock(core);
957 if (ret)
958 return ret;
960 ret = clk_core_enable_lock(core);
961 if (ret)
962 clk_core_unprepare_lock(core);
964 return ret;
967 static void clk_core_disable_unprepare(struct clk_core *core)
969 clk_core_disable_lock(core);
970 clk_core_unprepare_lock(core);
973 static void clk_unprepare_unused_subtree(struct clk_core *core)
975 struct clk_core *child;
977 lockdep_assert_held(&prepare_lock);
979 hlist_for_each_entry(child, &core->children, child_node)
980 clk_unprepare_unused_subtree(child);
982 if (core->prepare_count)
983 return;
985 if (core->flags & CLK_IGNORE_UNUSED)
986 return;
988 if (clk_pm_runtime_get(core))
989 return;
991 if (clk_core_is_prepared(core)) {
992 trace_clk_unprepare(core);
993 if (core->ops->unprepare_unused)
994 core->ops->unprepare_unused(core->hw);
995 else if (core->ops->unprepare)
996 core->ops->unprepare(core->hw);
997 trace_clk_unprepare_complete(core);
1000 clk_pm_runtime_put(core);
1003 static void clk_disable_unused_subtree(struct clk_core *core)
1005 struct clk_core *child;
1006 unsigned long flags;
1008 lockdep_assert_held(&prepare_lock);
1010 hlist_for_each_entry(child, &core->children, child_node)
1011 clk_disable_unused_subtree(child);
1013 if (core->flags & CLK_OPS_PARENT_ENABLE)
1014 clk_core_prepare_enable(core->parent);
1016 if (clk_pm_runtime_get(core))
1017 goto unprepare_out;
1019 flags = clk_enable_lock();
1021 if (core->enable_count)
1022 goto unlock_out;
1024 if (core->flags & CLK_IGNORE_UNUSED)
1025 goto unlock_out;
1028 * some gate clocks have special needs during the disable-unused
1029 * sequence. call .disable_unused if available, otherwise fall
1030 * back to .disable
1032 if (clk_core_is_enabled(core)) {
1033 trace_clk_disable(core);
1034 if (core->ops->disable_unused)
1035 core->ops->disable_unused(core->hw);
1036 else if (core->ops->disable)
1037 core->ops->disable(core->hw);
1038 trace_clk_disable_complete(core);
1041 unlock_out:
1042 clk_enable_unlock(flags);
1043 clk_pm_runtime_put(core);
1044 unprepare_out:
1045 if (core->flags & CLK_OPS_PARENT_ENABLE)
1046 clk_core_disable_unprepare(core->parent);
1049 static bool clk_ignore_unused;
1050 static int __init clk_ignore_unused_setup(char *__unused)
1052 clk_ignore_unused = true;
1053 return 1;
1055 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1057 static int clk_disable_unused(void)
1059 struct clk_core *core;
1061 if (clk_ignore_unused) {
1062 pr_warn("clk: Not disabling unused clocks\n");
1063 return 0;
1066 clk_prepare_lock();
1068 hlist_for_each_entry(core, &clk_root_list, child_node)
1069 clk_disable_unused_subtree(core);
1071 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1072 clk_disable_unused_subtree(core);
1074 hlist_for_each_entry(core, &clk_root_list, child_node)
1075 clk_unprepare_unused_subtree(core);
1077 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1078 clk_unprepare_unused_subtree(core);
1080 clk_prepare_unlock();
1082 return 0;
1084 late_initcall_sync(clk_disable_unused);
1086 static int clk_core_determine_round_nolock(struct clk_core *core,
1087 struct clk_rate_request *req)
1089 long rate;
1091 lockdep_assert_held(&prepare_lock);
1093 if (!core)
1094 return 0;
1097 * At this point, core protection will be disabled if
1098 * - if the provider is not protected at all
1099 * - if the calling consumer is the only one which has exclusivity
1100 * over the provider
1102 if (clk_core_rate_is_protected(core)) {
1103 req->rate = core->rate;
1104 } else if (core->ops->determine_rate) {
1105 return core->ops->determine_rate(core->hw, req);
1106 } else if (core->ops->round_rate) {
1107 rate = core->ops->round_rate(core->hw, req->rate,
1108 &req->best_parent_rate);
1109 if (rate < 0)
1110 return rate;
1112 req->rate = rate;
1113 } else {
1114 return -EINVAL;
1117 return 0;
1120 static void clk_core_init_rate_req(struct clk_core * const core,
1121 struct clk_rate_request *req)
1123 struct clk_core *parent;
1125 if (WARN_ON(!core || !req))
1126 return;
1128 parent = core->parent;
1129 if (parent) {
1130 req->best_parent_hw = parent->hw;
1131 req->best_parent_rate = parent->rate;
1132 } else {
1133 req->best_parent_hw = NULL;
1134 req->best_parent_rate = 0;
1138 static bool clk_core_can_round(struct clk_core * const core)
1140 if (core->ops->determine_rate || core->ops->round_rate)
1141 return true;
1143 return false;
1146 static int clk_core_round_rate_nolock(struct clk_core *core,
1147 struct clk_rate_request *req)
1149 lockdep_assert_held(&prepare_lock);
1151 if (!core) {
1152 req->rate = 0;
1153 return 0;
1156 clk_core_init_rate_req(core, req);
1158 if (clk_core_can_round(core))
1159 return clk_core_determine_round_nolock(core, req);
1160 else if (core->flags & CLK_SET_RATE_PARENT)
1161 return clk_core_round_rate_nolock(core->parent, req);
1163 req->rate = core->rate;
1164 return 0;
1168 * __clk_determine_rate - get the closest rate actually supported by a clock
1169 * @hw: determine the rate of this clock
1170 * @req: target rate request
1172 * Useful for clk_ops such as .set_rate and .determine_rate.
1174 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1176 if (!hw) {
1177 req->rate = 0;
1178 return 0;
1181 return clk_core_round_rate_nolock(hw->core, req);
1183 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1185 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1187 int ret;
1188 struct clk_rate_request req;
1190 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1191 req.rate = rate;
1193 ret = clk_core_round_rate_nolock(hw->core, &req);
1194 if (ret)
1195 return 0;
1197 return req.rate;
1199 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1202 * clk_round_rate - round the given rate for a clk
1203 * @clk: the clk for which we are rounding a rate
1204 * @rate: the rate which is to be rounded
1206 * Takes in a rate as input and rounds it to a rate that the clk can actually
1207 * use which is then returned. If clk doesn't support round_rate operation
1208 * then the parent rate is returned.
1210 long clk_round_rate(struct clk *clk, unsigned long rate)
1212 struct clk_rate_request req;
1213 int ret;
1215 if (!clk)
1216 return 0;
1218 clk_prepare_lock();
1220 if (clk->exclusive_count)
1221 clk_core_rate_unprotect(clk->core);
1223 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1224 req.rate = rate;
1226 ret = clk_core_round_rate_nolock(clk->core, &req);
1228 if (clk->exclusive_count)
1229 clk_core_rate_protect(clk->core);
1231 clk_prepare_unlock();
1233 if (ret)
1234 return ret;
1236 return req.rate;
1238 EXPORT_SYMBOL_GPL(clk_round_rate);
1241 * __clk_notify - call clk notifier chain
1242 * @core: clk that is changing rate
1243 * @msg: clk notifier type (see include/linux/clk.h)
1244 * @old_rate: old clk rate
1245 * @new_rate: new clk rate
1247 * Triggers a notifier call chain on the clk rate-change notification
1248 * for 'clk'. Passes a pointer to the struct clk and the previous
1249 * and current rates to the notifier callback. Intended to be called by
1250 * internal clock code only. Returns NOTIFY_DONE from the last driver
1251 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1252 * a driver returns that.
1254 static int __clk_notify(struct clk_core *core, unsigned long msg,
1255 unsigned long old_rate, unsigned long new_rate)
1257 struct clk_notifier *cn;
1258 struct clk_notifier_data cnd;
1259 int ret = NOTIFY_DONE;
1261 cnd.old_rate = old_rate;
1262 cnd.new_rate = new_rate;
1264 list_for_each_entry(cn, &clk_notifier_list, node) {
1265 if (cn->clk->core == core) {
1266 cnd.clk = cn->clk;
1267 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1268 &cnd);
1269 if (ret & NOTIFY_STOP_MASK)
1270 return ret;
1274 return ret;
1278 * __clk_recalc_accuracies
1279 * @core: first clk in the subtree
1281 * Walks the subtree of clks starting with clk and recalculates accuracies as
1282 * it goes. Note that if a clk does not implement the .recalc_accuracy
1283 * callback then it is assumed that the clock will take on the accuracy of its
1284 * parent.
1286 static void __clk_recalc_accuracies(struct clk_core *core)
1288 unsigned long parent_accuracy = 0;
1289 struct clk_core *child;
1291 lockdep_assert_held(&prepare_lock);
1293 if (core->parent)
1294 parent_accuracy = core->parent->accuracy;
1296 if (core->ops->recalc_accuracy)
1297 core->accuracy = core->ops->recalc_accuracy(core->hw,
1298 parent_accuracy);
1299 else
1300 core->accuracy = parent_accuracy;
1302 hlist_for_each_entry(child, &core->children, child_node)
1303 __clk_recalc_accuracies(child);
1306 static long clk_core_get_accuracy(struct clk_core *core)
1308 unsigned long accuracy;
1310 clk_prepare_lock();
1311 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1312 __clk_recalc_accuracies(core);
1314 accuracy = __clk_get_accuracy(core);
1315 clk_prepare_unlock();
1317 return accuracy;
1321 * clk_get_accuracy - return the accuracy of clk
1322 * @clk: the clk whose accuracy is being returned
1324 * Simply returns the cached accuracy of the clk, unless
1325 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1326 * issued.
1327 * If clk is NULL then returns 0.
1329 long clk_get_accuracy(struct clk *clk)
1331 if (!clk)
1332 return 0;
1334 return clk_core_get_accuracy(clk->core);
1336 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1338 static unsigned long clk_recalc(struct clk_core *core,
1339 unsigned long parent_rate)
1341 unsigned long rate = parent_rate;
1343 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1344 rate = core->ops->recalc_rate(core->hw, parent_rate);
1345 clk_pm_runtime_put(core);
1347 return rate;
1351 * __clk_recalc_rates
1352 * @core: first clk in the subtree
1353 * @msg: notification type (see include/linux/clk.h)
1355 * Walks the subtree of clks starting with clk and recalculates rates as it
1356 * goes. Note that if a clk does not implement the .recalc_rate callback then
1357 * it is assumed that the clock will take on the rate of its parent.
1359 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1360 * if necessary.
1362 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1364 unsigned long old_rate;
1365 unsigned long parent_rate = 0;
1366 struct clk_core *child;
1368 lockdep_assert_held(&prepare_lock);
1370 old_rate = core->rate;
1372 if (core->parent)
1373 parent_rate = core->parent->rate;
1375 core->rate = clk_recalc(core, parent_rate);
1378 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1379 * & ABORT_RATE_CHANGE notifiers
1381 if (core->notifier_count && msg)
1382 __clk_notify(core, msg, old_rate, core->rate);
1384 hlist_for_each_entry(child, &core->children, child_node)
1385 __clk_recalc_rates(child, msg);
1388 static unsigned long clk_core_get_rate(struct clk_core *core)
1390 unsigned long rate;
1392 clk_prepare_lock();
1394 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1395 __clk_recalc_rates(core, 0);
1397 rate = clk_core_get_rate_nolock(core);
1398 clk_prepare_unlock();
1400 return rate;
1404 * clk_get_rate - return the rate of clk
1405 * @clk: the clk whose rate is being returned
1407 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1408 * is set, which means a recalc_rate will be issued.
1409 * If clk is NULL then returns 0.
1411 unsigned long clk_get_rate(struct clk *clk)
1413 if (!clk)
1414 return 0;
1416 return clk_core_get_rate(clk->core);
1418 EXPORT_SYMBOL_GPL(clk_get_rate);
1420 static int clk_fetch_parent_index(struct clk_core *core,
1421 struct clk_core *parent)
1423 int i;
1425 if (!parent)
1426 return -EINVAL;
1428 for (i = 0; i < core->num_parents; i++)
1429 if (clk_core_get_parent_by_index(core, i) == parent)
1430 return i;
1432 return -EINVAL;
1436 * Update the orphan status of @core and all its children.
1438 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1440 struct clk_core *child;
1442 core->orphan = is_orphan;
1444 hlist_for_each_entry(child, &core->children, child_node)
1445 clk_core_update_orphan_status(child, is_orphan);
1448 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1450 bool was_orphan = core->orphan;
1452 hlist_del(&core->child_node);
1454 if (new_parent) {
1455 bool becomes_orphan = new_parent->orphan;
1457 /* avoid duplicate POST_RATE_CHANGE notifications */
1458 if (new_parent->new_child == core)
1459 new_parent->new_child = NULL;
1461 hlist_add_head(&core->child_node, &new_parent->children);
1463 if (was_orphan != becomes_orphan)
1464 clk_core_update_orphan_status(core, becomes_orphan);
1465 } else {
1466 hlist_add_head(&core->child_node, &clk_orphan_list);
1467 if (!was_orphan)
1468 clk_core_update_orphan_status(core, true);
1471 core->parent = new_parent;
1474 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1475 struct clk_core *parent)
1477 unsigned long flags;
1478 struct clk_core *old_parent = core->parent;
1481 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1483 * 2. Migrate prepare state between parents and prevent race with
1484 * clk_enable().
1486 * If the clock is not prepared, then a race with
1487 * clk_enable/disable() is impossible since we already have the
1488 * prepare lock (future calls to clk_enable() need to be preceded by
1489 * a clk_prepare()).
1491 * If the clock is prepared, migrate the prepared state to the new
1492 * parent and also protect against a race with clk_enable() by
1493 * forcing the clock and the new parent on. This ensures that all
1494 * future calls to clk_enable() are practically NOPs with respect to
1495 * hardware and software states.
1497 * See also: Comment for clk_set_parent() below.
1500 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1501 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1502 clk_core_prepare_enable(old_parent);
1503 clk_core_prepare_enable(parent);
1506 /* migrate prepare count if > 0 */
1507 if (core->prepare_count) {
1508 clk_core_prepare_enable(parent);
1509 clk_core_enable_lock(core);
1512 /* update the clk tree topology */
1513 flags = clk_enable_lock();
1514 clk_reparent(core, parent);
1515 clk_enable_unlock(flags);
1517 return old_parent;
1520 static void __clk_set_parent_after(struct clk_core *core,
1521 struct clk_core *parent,
1522 struct clk_core *old_parent)
1525 * Finish the migration of prepare state and undo the changes done
1526 * for preventing a race with clk_enable().
1528 if (core->prepare_count) {
1529 clk_core_disable_lock(core);
1530 clk_core_disable_unprepare(old_parent);
1533 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1534 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1535 clk_core_disable_unprepare(parent);
1536 clk_core_disable_unprepare(old_parent);
1540 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1541 u8 p_index)
1543 unsigned long flags;
1544 int ret = 0;
1545 struct clk_core *old_parent;
1547 old_parent = __clk_set_parent_before(core, parent);
1549 trace_clk_set_parent(core, parent);
1551 /* change clock input source */
1552 if (parent && core->ops->set_parent)
1553 ret = core->ops->set_parent(core->hw, p_index);
1555 trace_clk_set_parent_complete(core, parent);
1557 if (ret) {
1558 flags = clk_enable_lock();
1559 clk_reparent(core, old_parent);
1560 clk_enable_unlock(flags);
1561 __clk_set_parent_after(core, old_parent, parent);
1563 return ret;
1566 __clk_set_parent_after(core, parent, old_parent);
1568 return 0;
1572 * __clk_speculate_rates
1573 * @core: first clk in the subtree
1574 * @parent_rate: the "future" rate of clk's parent
1576 * Walks the subtree of clks starting with clk, speculating rates as it
1577 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1579 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1580 * pre-rate change notifications and returns early if no clks in the
1581 * subtree have subscribed to the notifications. Note that if a clk does not
1582 * implement the .recalc_rate callback then it is assumed that the clock will
1583 * take on the rate of its parent.
1585 static int __clk_speculate_rates(struct clk_core *core,
1586 unsigned long parent_rate)
1588 struct clk_core *child;
1589 unsigned long new_rate;
1590 int ret = NOTIFY_DONE;
1592 lockdep_assert_held(&prepare_lock);
1594 new_rate = clk_recalc(core, parent_rate);
1596 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1597 if (core->notifier_count)
1598 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1600 if (ret & NOTIFY_STOP_MASK) {
1601 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1602 __func__, core->name, ret);
1603 goto out;
1606 hlist_for_each_entry(child, &core->children, child_node) {
1607 ret = __clk_speculate_rates(child, new_rate);
1608 if (ret & NOTIFY_STOP_MASK)
1609 break;
1612 out:
1613 return ret;
1616 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1617 struct clk_core *new_parent, u8 p_index)
1619 struct clk_core *child;
1621 core->new_rate = new_rate;
1622 core->new_parent = new_parent;
1623 core->new_parent_index = p_index;
1624 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1625 core->new_child = NULL;
1626 if (new_parent && new_parent != core->parent)
1627 new_parent->new_child = core;
1629 hlist_for_each_entry(child, &core->children, child_node) {
1630 child->new_rate = clk_recalc(child, new_rate);
1631 clk_calc_subtree(child, child->new_rate, NULL, 0);
1636 * calculate the new rates returning the topmost clock that has to be
1637 * changed.
1639 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1640 unsigned long rate)
1642 struct clk_core *top = core;
1643 struct clk_core *old_parent, *parent;
1644 unsigned long best_parent_rate = 0;
1645 unsigned long new_rate;
1646 unsigned long min_rate;
1647 unsigned long max_rate;
1648 int p_index = 0;
1649 long ret;
1651 /* sanity */
1652 if (IS_ERR_OR_NULL(core))
1653 return NULL;
1655 /* save parent rate, if it exists */
1656 parent = old_parent = core->parent;
1657 if (parent)
1658 best_parent_rate = parent->rate;
1660 clk_core_get_boundaries(core, &min_rate, &max_rate);
1662 /* find the closest rate and parent clk/rate */
1663 if (clk_core_can_round(core)) {
1664 struct clk_rate_request req;
1666 req.rate = rate;
1667 req.min_rate = min_rate;
1668 req.max_rate = max_rate;
1670 clk_core_init_rate_req(core, &req);
1672 ret = clk_core_determine_round_nolock(core, &req);
1673 if (ret < 0)
1674 return NULL;
1676 best_parent_rate = req.best_parent_rate;
1677 new_rate = req.rate;
1678 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1680 if (new_rate < min_rate || new_rate > max_rate)
1681 return NULL;
1682 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1683 /* pass-through clock without adjustable parent */
1684 core->new_rate = core->rate;
1685 return NULL;
1686 } else {
1687 /* pass-through clock with adjustable parent */
1688 top = clk_calc_new_rates(parent, rate);
1689 new_rate = parent->new_rate;
1690 goto out;
1693 /* some clocks must be gated to change parent */
1694 if (parent != old_parent &&
1695 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1696 pr_debug("%s: %s not gated but wants to reparent\n",
1697 __func__, core->name);
1698 return NULL;
1701 /* try finding the new parent index */
1702 if (parent && core->num_parents > 1) {
1703 p_index = clk_fetch_parent_index(core, parent);
1704 if (p_index < 0) {
1705 pr_debug("%s: clk %s can not be parent of clk %s\n",
1706 __func__, parent->name, core->name);
1707 return NULL;
1711 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1712 best_parent_rate != parent->rate)
1713 top = clk_calc_new_rates(parent, best_parent_rate);
1715 out:
1716 clk_calc_subtree(core, new_rate, parent, p_index);
1718 return top;
1722 * Notify about rate changes in a subtree. Always walk down the whole tree
1723 * so that in case of an error we can walk down the whole tree again and
1724 * abort the change.
1726 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1727 unsigned long event)
1729 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1730 int ret = NOTIFY_DONE;
1732 if (core->rate == core->new_rate)
1733 return NULL;
1735 if (core->notifier_count) {
1736 ret = __clk_notify(core, event, core->rate, core->new_rate);
1737 if (ret & NOTIFY_STOP_MASK)
1738 fail_clk = core;
1741 hlist_for_each_entry(child, &core->children, child_node) {
1742 /* Skip children who will be reparented to another clock */
1743 if (child->new_parent && child->new_parent != core)
1744 continue;
1745 tmp_clk = clk_propagate_rate_change(child, event);
1746 if (tmp_clk)
1747 fail_clk = tmp_clk;
1750 /* handle the new child who might not be in core->children yet */
1751 if (core->new_child) {
1752 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1753 if (tmp_clk)
1754 fail_clk = tmp_clk;
1757 return fail_clk;
1761 * walk down a subtree and set the new rates notifying the rate
1762 * change on the way
1764 static void clk_change_rate(struct clk_core *core)
1766 struct clk_core *child;
1767 struct hlist_node *tmp;
1768 unsigned long old_rate;
1769 unsigned long best_parent_rate = 0;
1770 bool skip_set_rate = false;
1771 struct clk_core *old_parent;
1772 struct clk_core *parent = NULL;
1774 old_rate = core->rate;
1776 if (core->new_parent) {
1777 parent = core->new_parent;
1778 best_parent_rate = core->new_parent->rate;
1779 } else if (core->parent) {
1780 parent = core->parent;
1781 best_parent_rate = core->parent->rate;
1784 if (clk_pm_runtime_get(core))
1785 return;
1787 if (core->flags & CLK_SET_RATE_UNGATE) {
1788 unsigned long flags;
1790 clk_core_prepare(core);
1791 flags = clk_enable_lock();
1792 clk_core_enable(core);
1793 clk_enable_unlock(flags);
1796 if (core->new_parent && core->new_parent != core->parent) {
1797 old_parent = __clk_set_parent_before(core, core->new_parent);
1798 trace_clk_set_parent(core, core->new_parent);
1800 if (core->ops->set_rate_and_parent) {
1801 skip_set_rate = true;
1802 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1803 best_parent_rate,
1804 core->new_parent_index);
1805 } else if (core->ops->set_parent) {
1806 core->ops->set_parent(core->hw, core->new_parent_index);
1809 trace_clk_set_parent_complete(core, core->new_parent);
1810 __clk_set_parent_after(core, core->new_parent, old_parent);
1813 if (core->flags & CLK_OPS_PARENT_ENABLE)
1814 clk_core_prepare_enable(parent);
1816 trace_clk_set_rate(core, core->new_rate);
1818 if (!skip_set_rate && core->ops->set_rate)
1819 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1821 trace_clk_set_rate_complete(core, core->new_rate);
1823 core->rate = clk_recalc(core, best_parent_rate);
1825 if (core->flags & CLK_SET_RATE_UNGATE) {
1826 unsigned long flags;
1828 flags = clk_enable_lock();
1829 clk_core_disable(core);
1830 clk_enable_unlock(flags);
1831 clk_core_unprepare(core);
1834 if (core->flags & CLK_OPS_PARENT_ENABLE)
1835 clk_core_disable_unprepare(parent);
1837 if (core->notifier_count && old_rate != core->rate)
1838 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1840 if (core->flags & CLK_RECALC_NEW_RATES)
1841 (void)clk_calc_new_rates(core, core->new_rate);
1844 * Use safe iteration, as change_rate can actually swap parents
1845 * for certain clock types.
1847 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1848 /* Skip children who will be reparented to another clock */
1849 if (child->new_parent && child->new_parent != core)
1850 continue;
1851 clk_change_rate(child);
1854 /* handle the new child who might not be in core->children yet */
1855 if (core->new_child)
1856 clk_change_rate(core->new_child);
1858 clk_pm_runtime_put(core);
1861 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1862 unsigned long req_rate)
1864 int ret, cnt;
1865 struct clk_rate_request req;
1867 lockdep_assert_held(&prepare_lock);
1869 if (!core)
1870 return 0;
1872 /* simulate what the rate would be if it could be freely set */
1873 cnt = clk_core_rate_nuke_protect(core);
1874 if (cnt < 0)
1875 return cnt;
1877 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1878 req.rate = req_rate;
1880 ret = clk_core_round_rate_nolock(core, &req);
1882 /* restore the protection */
1883 clk_core_rate_restore_protect(core, cnt);
1885 return ret ? 0 : req.rate;
1888 static int clk_core_set_rate_nolock(struct clk_core *core,
1889 unsigned long req_rate)
1891 struct clk_core *top, *fail_clk;
1892 unsigned long rate;
1893 int ret = 0;
1895 if (!core)
1896 return 0;
1898 rate = clk_core_req_round_rate_nolock(core, req_rate);
1900 /* bail early if nothing to do */
1901 if (rate == clk_core_get_rate_nolock(core))
1902 return 0;
1904 /* fail on a direct rate set of a protected provider */
1905 if (clk_core_rate_is_protected(core))
1906 return -EBUSY;
1908 /* calculate new rates and get the topmost changed clock */
1909 top = clk_calc_new_rates(core, req_rate);
1910 if (!top)
1911 return -EINVAL;
1913 ret = clk_pm_runtime_get(core);
1914 if (ret)
1915 return ret;
1917 /* notify that we are about to change rates */
1918 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1919 if (fail_clk) {
1920 pr_debug("%s: failed to set %s rate\n", __func__,
1921 fail_clk->name);
1922 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1923 ret = -EBUSY;
1924 goto err;
1927 /* change the rates */
1928 clk_change_rate(top);
1930 core->req_rate = req_rate;
1931 err:
1932 clk_pm_runtime_put(core);
1934 return ret;
1938 * clk_set_rate - specify a new rate for clk
1939 * @clk: the clk whose rate is being changed
1940 * @rate: the new rate for clk
1942 * In the simplest case clk_set_rate will only adjust the rate of clk.
1944 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1945 * propagate up to clk's parent; whether or not this happens depends on the
1946 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1947 * after calling .round_rate then upstream parent propagation is ignored. If
1948 * *parent_rate comes back with a new rate for clk's parent then we propagate
1949 * up to clk's parent and set its rate. Upward propagation will continue
1950 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1951 * .round_rate stops requesting changes to clk's parent_rate.
1953 * Rate changes are accomplished via tree traversal that also recalculates the
1954 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1956 * Returns 0 on success, -EERROR otherwise.
1958 int clk_set_rate(struct clk *clk, unsigned long rate)
1960 int ret;
1962 if (!clk)
1963 return 0;
1965 /* prevent racing with updates to the clock topology */
1966 clk_prepare_lock();
1968 if (clk->exclusive_count)
1969 clk_core_rate_unprotect(clk->core);
1971 ret = clk_core_set_rate_nolock(clk->core, rate);
1973 if (clk->exclusive_count)
1974 clk_core_rate_protect(clk->core);
1976 clk_prepare_unlock();
1978 return ret;
1980 EXPORT_SYMBOL_GPL(clk_set_rate);
1983 * clk_set_rate_exclusive - specify a new rate get exclusive control
1984 * @clk: the clk whose rate is being changed
1985 * @rate: the new rate for clk
1987 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1988 * within a critical section
1990 * This can be used initially to ensure that at least 1 consumer is
1991 * statisfied when several consumers are competing for exclusivity over the
1992 * same clock provider.
1994 * The exclusivity is not applied if setting the rate failed.
1996 * Calls to clk_rate_exclusive_get() should be balanced with calls to
1997 * clk_rate_exclusive_put().
1999 * Returns 0 on success, -EERROR otherwise.
2001 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2003 int ret;
2005 if (!clk)
2006 return 0;
2008 /* prevent racing with updates to the clock topology */
2009 clk_prepare_lock();
2012 * The temporary protection removal is not here, on purpose
2013 * This function is meant to be used instead of clk_rate_protect,
2014 * so before the consumer code path protect the clock provider
2017 ret = clk_core_set_rate_nolock(clk->core, rate);
2018 if (!ret) {
2019 clk_core_rate_protect(clk->core);
2020 clk->exclusive_count++;
2023 clk_prepare_unlock();
2025 return ret;
2027 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2030 * clk_set_rate_range - set a rate range for a clock source
2031 * @clk: clock source
2032 * @min: desired minimum clock rate in Hz, inclusive
2033 * @max: desired maximum clock rate in Hz, inclusive
2035 * Returns success (0) or negative errno.
2037 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2039 int ret = 0;
2040 unsigned long old_min, old_max, rate;
2042 if (!clk)
2043 return 0;
2045 if (min > max) {
2046 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2047 __func__, clk->core->name, clk->dev_id, clk->con_id,
2048 min, max);
2049 return -EINVAL;
2052 clk_prepare_lock();
2054 if (clk->exclusive_count)
2055 clk_core_rate_unprotect(clk->core);
2057 /* Save the current values in case we need to rollback the change */
2058 old_min = clk->min_rate;
2059 old_max = clk->max_rate;
2060 clk->min_rate = min;
2061 clk->max_rate = max;
2063 rate = clk_core_get_rate_nolock(clk->core);
2064 if (rate < min || rate > max) {
2066 * FIXME:
2067 * We are in bit of trouble here, current rate is outside the
2068 * the requested range. We are going try to request appropriate
2069 * range boundary but there is a catch. It may fail for the
2070 * usual reason (clock broken, clock protected, etc) but also
2071 * because:
2072 * - round_rate() was not favorable and fell on the wrong
2073 * side of the boundary
2074 * - the determine_rate() callback does not really check for
2075 * this corner case when determining the rate
2078 if (rate < min)
2079 rate = min;
2080 else
2081 rate = max;
2083 ret = clk_core_set_rate_nolock(clk->core, rate);
2084 if (ret) {
2085 /* rollback the changes */
2086 clk->min_rate = old_min;
2087 clk->max_rate = old_max;
2091 if (clk->exclusive_count)
2092 clk_core_rate_protect(clk->core);
2094 clk_prepare_unlock();
2096 return ret;
2098 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2101 * clk_set_min_rate - set a minimum clock rate for a clock source
2102 * @clk: clock source
2103 * @rate: desired minimum clock rate in Hz, inclusive
2105 * Returns success (0) or negative errno.
2107 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2109 if (!clk)
2110 return 0;
2112 return clk_set_rate_range(clk, rate, clk->max_rate);
2114 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2117 * clk_set_max_rate - set a maximum clock rate for a clock source
2118 * @clk: clock source
2119 * @rate: desired maximum clock rate in Hz, inclusive
2121 * Returns success (0) or negative errno.
2123 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2125 if (!clk)
2126 return 0;
2128 return clk_set_rate_range(clk, clk->min_rate, rate);
2130 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2133 * clk_get_parent - return the parent of a clk
2134 * @clk: the clk whose parent gets returned
2136 * Simply returns clk->parent. Returns NULL if clk is NULL.
2138 struct clk *clk_get_parent(struct clk *clk)
2140 struct clk *parent;
2142 if (!clk)
2143 return NULL;
2145 clk_prepare_lock();
2146 /* TODO: Create a per-user clk and change callers to call clk_put */
2147 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2148 clk_prepare_unlock();
2150 return parent;
2152 EXPORT_SYMBOL_GPL(clk_get_parent);
2154 static struct clk_core *__clk_init_parent(struct clk_core *core)
2156 u8 index = 0;
2158 if (core->num_parents > 1 && core->ops->get_parent)
2159 index = core->ops->get_parent(core->hw);
2161 return clk_core_get_parent_by_index(core, index);
2164 static void clk_core_reparent(struct clk_core *core,
2165 struct clk_core *new_parent)
2167 clk_reparent(core, new_parent);
2168 __clk_recalc_accuracies(core);
2169 __clk_recalc_rates(core, POST_RATE_CHANGE);
2172 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2174 if (!hw)
2175 return;
2177 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2181 * clk_has_parent - check if a clock is a possible parent for another
2182 * @clk: clock source
2183 * @parent: parent clock source
2185 * This function can be used in drivers that need to check that a clock can be
2186 * the parent of another without actually changing the parent.
2188 * Returns true if @parent is a possible parent for @clk, false otherwise.
2190 bool clk_has_parent(struct clk *clk, struct clk *parent)
2192 struct clk_core *core, *parent_core;
2194 /* NULL clocks should be nops, so return success if either is NULL. */
2195 if (!clk || !parent)
2196 return true;
2198 core = clk->core;
2199 parent_core = parent->core;
2201 /* Optimize for the case where the parent is already the parent. */
2202 if (core->parent == parent_core)
2203 return true;
2205 return match_string(core->parent_names, core->num_parents,
2206 parent_core->name) >= 0;
2208 EXPORT_SYMBOL_GPL(clk_has_parent);
2210 static int clk_core_set_parent_nolock(struct clk_core *core,
2211 struct clk_core *parent)
2213 int ret = 0;
2214 int p_index = 0;
2215 unsigned long p_rate = 0;
2217 lockdep_assert_held(&prepare_lock);
2219 if (!core)
2220 return 0;
2222 if (core->parent == parent)
2223 return 0;
2225 /* verify ops for for multi-parent clks */
2226 if (core->num_parents > 1 && !core->ops->set_parent)
2227 return -EPERM;
2229 /* check that we are allowed to re-parent if the clock is in use */
2230 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2231 return -EBUSY;
2233 if (clk_core_rate_is_protected(core))
2234 return -EBUSY;
2236 /* try finding the new parent index */
2237 if (parent) {
2238 p_index = clk_fetch_parent_index(core, parent);
2239 if (p_index < 0) {
2240 pr_debug("%s: clk %s can not be parent of clk %s\n",
2241 __func__, parent->name, core->name);
2242 return p_index;
2244 p_rate = parent->rate;
2247 ret = clk_pm_runtime_get(core);
2248 if (ret)
2249 return ret;
2251 /* propagate PRE_RATE_CHANGE notifications */
2252 ret = __clk_speculate_rates(core, p_rate);
2254 /* abort if a driver objects */
2255 if (ret & NOTIFY_STOP_MASK)
2256 goto runtime_put;
2258 /* do the re-parent */
2259 ret = __clk_set_parent(core, parent, p_index);
2261 /* propagate rate an accuracy recalculation accordingly */
2262 if (ret) {
2263 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2264 } else {
2265 __clk_recalc_rates(core, POST_RATE_CHANGE);
2266 __clk_recalc_accuracies(core);
2269 runtime_put:
2270 clk_pm_runtime_put(core);
2272 return ret;
2276 * clk_set_parent - switch the parent of a mux clk
2277 * @clk: the mux clk whose input we are switching
2278 * @parent: the new input to clk
2280 * Re-parent clk to use parent as its new input source. If clk is in
2281 * prepared state, the clk will get enabled for the duration of this call. If
2282 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2283 * that, the reparenting is glitchy in hardware, etc), use the
2284 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2286 * After successfully changing clk's parent clk_set_parent will update the
2287 * clk topology, sysfs topology and propagate rate recalculation via
2288 * __clk_recalc_rates.
2290 * Returns 0 on success, -EERROR otherwise.
2292 int clk_set_parent(struct clk *clk, struct clk *parent)
2294 int ret;
2296 if (!clk)
2297 return 0;
2299 clk_prepare_lock();
2301 if (clk->exclusive_count)
2302 clk_core_rate_unprotect(clk->core);
2304 ret = clk_core_set_parent_nolock(clk->core,
2305 parent ? parent->core : NULL);
2307 if (clk->exclusive_count)
2308 clk_core_rate_protect(clk->core);
2310 clk_prepare_unlock();
2312 return ret;
2314 EXPORT_SYMBOL_GPL(clk_set_parent);
2316 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2318 int ret = -EINVAL;
2320 lockdep_assert_held(&prepare_lock);
2322 if (!core)
2323 return 0;
2325 if (clk_core_rate_is_protected(core))
2326 return -EBUSY;
2328 trace_clk_set_phase(core, degrees);
2330 if (core->ops->set_phase) {
2331 ret = core->ops->set_phase(core->hw, degrees);
2332 if (!ret)
2333 core->phase = degrees;
2336 trace_clk_set_phase_complete(core, degrees);
2338 return ret;
2342 * clk_set_phase - adjust the phase shift of a clock signal
2343 * @clk: clock signal source
2344 * @degrees: number of degrees the signal is shifted
2346 * Shifts the phase of a clock signal by the specified
2347 * degrees. Returns 0 on success, -EERROR otherwise.
2349 * This function makes no distinction about the input or reference
2350 * signal that we adjust the clock signal phase against. For example
2351 * phase locked-loop clock signal generators we may shift phase with
2352 * respect to feedback clock signal input, but for other cases the
2353 * clock phase may be shifted with respect to some other, unspecified
2354 * signal.
2356 * Additionally the concept of phase shift does not propagate through
2357 * the clock tree hierarchy, which sets it apart from clock rates and
2358 * clock accuracy. A parent clock phase attribute does not have an
2359 * impact on the phase attribute of a child clock.
2361 int clk_set_phase(struct clk *clk, int degrees)
2363 int ret;
2365 if (!clk)
2366 return 0;
2368 /* sanity check degrees */
2369 degrees %= 360;
2370 if (degrees < 0)
2371 degrees += 360;
2373 clk_prepare_lock();
2375 if (clk->exclusive_count)
2376 clk_core_rate_unprotect(clk->core);
2378 ret = clk_core_set_phase_nolock(clk->core, degrees);
2380 if (clk->exclusive_count)
2381 clk_core_rate_protect(clk->core);
2383 clk_prepare_unlock();
2385 return ret;
2387 EXPORT_SYMBOL_GPL(clk_set_phase);
2389 static int clk_core_get_phase(struct clk_core *core)
2391 int ret;
2393 clk_prepare_lock();
2394 /* Always try to update cached phase if possible */
2395 if (core->ops->get_phase)
2396 core->phase = core->ops->get_phase(core->hw);
2397 ret = core->phase;
2398 clk_prepare_unlock();
2400 return ret;
2404 * clk_get_phase - return the phase shift of a clock signal
2405 * @clk: clock signal source
2407 * Returns the phase shift of a clock node in degrees, otherwise returns
2408 * -EERROR.
2410 int clk_get_phase(struct clk *clk)
2412 if (!clk)
2413 return 0;
2415 return clk_core_get_phase(clk->core);
2417 EXPORT_SYMBOL_GPL(clk_get_phase);
2419 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2421 /* Assume a default value of 50% */
2422 core->duty.num = 1;
2423 core->duty.den = 2;
2426 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2428 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2430 struct clk_duty *duty = &core->duty;
2431 int ret = 0;
2433 if (!core->ops->get_duty_cycle)
2434 return clk_core_update_duty_cycle_parent_nolock(core);
2436 ret = core->ops->get_duty_cycle(core->hw, duty);
2437 if (ret)
2438 goto reset;
2440 /* Don't trust the clock provider too much */
2441 if (duty->den == 0 || duty->num > duty->den) {
2442 ret = -EINVAL;
2443 goto reset;
2446 return 0;
2448 reset:
2449 clk_core_reset_duty_cycle_nolock(core);
2450 return ret;
2453 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2455 int ret = 0;
2457 if (core->parent &&
2458 core->flags & CLK_DUTY_CYCLE_PARENT) {
2459 ret = clk_core_update_duty_cycle_nolock(core->parent);
2460 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2461 } else {
2462 clk_core_reset_duty_cycle_nolock(core);
2465 return ret;
2468 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2469 struct clk_duty *duty);
2471 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2472 struct clk_duty *duty)
2474 int ret;
2476 lockdep_assert_held(&prepare_lock);
2478 if (clk_core_rate_is_protected(core))
2479 return -EBUSY;
2481 trace_clk_set_duty_cycle(core, duty);
2483 if (!core->ops->set_duty_cycle)
2484 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2486 ret = core->ops->set_duty_cycle(core->hw, duty);
2487 if (!ret)
2488 memcpy(&core->duty, duty, sizeof(*duty));
2490 trace_clk_set_duty_cycle_complete(core, duty);
2492 return ret;
2495 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2496 struct clk_duty *duty)
2498 int ret = 0;
2500 if (core->parent &&
2501 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2502 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2503 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2506 return ret;
2510 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2511 * @clk: clock signal source
2512 * @num: numerator of the duty cycle ratio to be applied
2513 * @den: denominator of the duty cycle ratio to be applied
2515 * Apply the duty cycle ratio if the ratio is valid and the clock can
2516 * perform this operation
2518 * Returns (0) on success, a negative errno otherwise.
2520 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2522 int ret;
2523 struct clk_duty duty;
2525 if (!clk)
2526 return 0;
2528 /* sanity check the ratio */
2529 if (den == 0 || num > den)
2530 return -EINVAL;
2532 duty.num = num;
2533 duty.den = den;
2535 clk_prepare_lock();
2537 if (clk->exclusive_count)
2538 clk_core_rate_unprotect(clk->core);
2540 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2542 if (clk->exclusive_count)
2543 clk_core_rate_protect(clk->core);
2545 clk_prepare_unlock();
2547 return ret;
2549 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2551 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2552 unsigned int scale)
2554 struct clk_duty *duty = &core->duty;
2555 int ret;
2557 clk_prepare_lock();
2559 ret = clk_core_update_duty_cycle_nolock(core);
2560 if (!ret)
2561 ret = mult_frac(scale, duty->num, duty->den);
2563 clk_prepare_unlock();
2565 return ret;
2569 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2570 * @clk: clock signal source
2571 * @scale: scaling factor to be applied to represent the ratio as an integer
2573 * Returns the duty cycle ratio of a clock node multiplied by the provided
2574 * scaling factor, or negative errno on error.
2576 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2578 if (!clk)
2579 return 0;
2581 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2583 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2586 * clk_is_match - check if two clk's point to the same hardware clock
2587 * @p: clk compared against q
2588 * @q: clk compared against p
2590 * Returns true if the two struct clk pointers both point to the same hardware
2591 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2592 * share the same struct clk_core object.
2594 * Returns false otherwise. Note that two NULL clks are treated as matching.
2596 bool clk_is_match(const struct clk *p, const struct clk *q)
2598 /* trivial case: identical struct clk's or both NULL */
2599 if (p == q)
2600 return true;
2602 /* true if clk->core pointers match. Avoid dereferencing garbage */
2603 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2604 if (p->core == q->core)
2605 return true;
2607 return false;
2609 EXPORT_SYMBOL_GPL(clk_is_match);
2611 /*** debugfs support ***/
2613 #ifdef CONFIG_DEBUG_FS
2614 #include <linux/debugfs.h>
2616 static struct dentry *rootdir;
2617 static int inited = 0;
2618 static DEFINE_MUTEX(clk_debug_lock);
2619 static HLIST_HEAD(clk_debug_list);
2621 static struct hlist_head *all_lists[] = {
2622 &clk_root_list,
2623 &clk_orphan_list,
2624 NULL,
2627 static struct hlist_head *orphan_list[] = {
2628 &clk_orphan_list,
2629 NULL,
2632 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2633 int level)
2635 if (!c)
2636 return;
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));
2647 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2648 int level)
2650 struct clk_core *child;
2652 if (!c)
2653 return;
2655 clk_summary_show_one(s, c, level);
2657 hlist_for_each_entry(child, &c->children, child_node)
2658 clk_summary_show_subtree(s, child, level + 1);
2661 static int clk_summary_show(struct seq_file *s, void *data)
2663 struct clk_core *c;
2664 struct hlist_head **lists = (struct hlist_head **)s->private;
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");
2670 clk_prepare_lock();
2672 for (; *lists; lists++)
2673 hlist_for_each_entry(c, *lists, child_node)
2674 clk_summary_show_subtree(s, c, 0);
2676 clk_prepare_unlock();
2678 return 0;
2680 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2682 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2684 if (!c)
2685 return;
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));
2699 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2701 struct clk_core *child;
2703 if (!c)
2704 return;
2706 clk_dump_one(s, c, level);
2708 hlist_for_each_entry(child, &c->children, child_node) {
2709 seq_putc(s, ',');
2710 clk_dump_subtree(s, child, level + 1);
2713 seq_putc(s, '}');
2716 static int clk_dump_show(struct seq_file *s, void *data)
2718 struct clk_core *c;
2719 bool first_node = true;
2720 struct hlist_head **lists = (struct hlist_head **)s->private;
2722 seq_putc(s, '{');
2723 clk_prepare_lock();
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);
2734 clk_prepare_unlock();
2736 seq_puts(s, "}\n");
2737 return 0;
2739 DEFINE_SHOW_ATTRIBUTE(clk_dump);
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
2762 static int clk_flags_show(struct seq_file *s, void *data)
2764 struct clk_core *core = s->private;
2765 unsigned long flags = core->flags;
2766 unsigned int i;
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;
2774 if (flags) {
2775 /* Unknown flags */
2776 seq_printf(s, "0x%lx\n", flags);
2779 return 0;
2781 DEFINE_SHOW_ATTRIBUTE(clk_flags);
2783 static int possible_parents_show(struct seq_file *s, void *data)
2785 struct clk_core *core = s->private;
2786 int i;
2788 for (i = 0; i < core->num_parents - 1; i++)
2789 seq_printf(s, "%s ", core->parent_names[i]);
2791 seq_printf(s, "%s\n", core->parent_names[i]);
2793 return 0;
2795 DEFINE_SHOW_ATTRIBUTE(possible_parents);
2797 static int clk_duty_cycle_show(struct seq_file *s, void *data)
2799 struct clk_core *core = s->private;
2800 struct clk_duty *duty = &core->duty;
2802 seq_printf(s, "%u/%u\n", duty->num, duty->den);
2804 return 0;
2806 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
2808 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2810 struct dentry *root;
2812 if (!core || !pdentry)
2813 return;
2815 root = debugfs_create_dir(core->name, pdentry);
2816 core->dentry = root;
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);
2829 if (core->num_parents > 1)
2830 debugfs_create_file("clk_possible_parents", 0444, root, core,
2831 &possible_parents_fops);
2833 if (core->ops->debug_init)
2834 core->ops->debug_init(core->hw, core->dentry);
2838 * clk_debug_register - add a clk node to the debugfs clk directory
2839 * @core: the clk being added to the debugfs clk directory
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.
2845 static void clk_debug_register(struct clk_core *core)
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);
2855 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2856 * @core: the clk being removed from the debugfs clk directory
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.
2862 static void clk_debug_unregister(struct clk_core *core)
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);
2872 * clk_debug_init - lazily populate the debugfs clk directory
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.
2880 static int __init clk_debug_init(void)
2882 struct clk_core *core;
2884 rootdir = debugfs_create_dir("clk", NULL);
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);
2895 mutex_lock(&clk_debug_lock);
2896 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2897 clk_debug_create_one(core, rootdir);
2899 inited = 1;
2900 mutex_unlock(&clk_debug_lock);
2902 return 0;
2904 late_initcall(clk_debug_init);
2905 #else
2906 static inline void clk_debug_register(struct clk_core *core) { }
2907 static inline void clk_debug_reparent(struct clk_core *core,
2908 struct clk_core *new_parent)
2911 static inline void clk_debug_unregister(struct clk_core *core)
2914 #endif
2917 * __clk_core_init - initialize the data structures in a struct clk_core
2918 * @core: clk_core being initialized
2920 * Initializes the lists in struct clk_core, queries the hardware for the
2921 * parent and rate and sets them both.
2923 static int __clk_core_init(struct clk_core *core)
2925 int i, ret;
2926 struct clk_core *orphan;
2927 struct hlist_node *tmp2;
2928 unsigned long rate;
2930 if (!core)
2931 return -EINVAL;
2933 clk_prepare_lock();
2935 ret = clk_pm_runtime_get(core);
2936 if (ret)
2937 goto unlock;
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;
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;
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;
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;
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;
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);
2985 core->parent = __clk_init_parent(core);
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.
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.
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;
3010 * optional platform-specific magic
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.
3017 if (core->ops->init)
3018 core->ops->init(core->hw);
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).
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;
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.
3040 if (core->ops->get_phase)
3041 core->phase = core->ops->get_phase(core->hw);
3042 else
3043 core->phase = 0;
3046 * Set clk's duty cycle.
3048 clk_core_update_duty_cycle_nolock(core);
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.
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;
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
3070 if (core->flags & CLK_IS_CRITICAL) {
3071 unsigned long flags;
3073 ret = clk_core_prepare(core);
3074 if (ret)
3075 goto out;
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;
3087 * walk the list of orphan clocks and reparent any that newly finds a
3088 * parent.
3090 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3091 struct clk_core *parent = __clk_init_parent(orphan);
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.
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);
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);
3115 clk_prepare_unlock();
3117 if (!ret)
3118 clk_debug_register(core);
3120 return ret;
3123 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3124 const char *con_id)
3126 struct clk *clk;
3128 /* This is to allow this function to be chained to others */
3129 if (IS_ERR_OR_NULL(hw))
3130 return ERR_CAST(hw);
3132 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3133 if (!clk)
3134 return ERR_PTR(-ENOMEM);
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;
3141 clk_prepare_lock();
3142 hlist_add_head(&clk->clks_node, &hw->core->clks);
3143 clk_prepare_unlock();
3145 return clk;
3148 /* keep in sync with __clk_put */
3149 void __clk_free_clk(struct clk *clk)
3151 clk_prepare_lock();
3152 hlist_del(&clk->clks_node);
3153 clk_prepare_unlock();
3155 kfree_const(clk->con_id);
3156 kfree(clk);
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
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.
3170 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3172 int i, ret;
3173 struct clk_core *core;
3175 core = kzalloc(sizeof(*core), GFP_KERNEL);
3176 if (!core) {
3177 ret = -ENOMEM;
3178 goto fail_out;
3181 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3182 if (!core->name) {
3183 ret = -ENOMEM;
3184 goto fail_name;
3187 if (WARN_ON(!hw->init->ops)) {
3188 ret = -EINVAL;
3189 goto fail_ops;
3191 core->ops = hw->init->ops;
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;
3204 /* allocate local copy in case parent_names is __initdata */
3205 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
3206 GFP_KERNEL);
3208 if (!core->parent_names) {
3209 ret = -ENOMEM;
3210 goto fail_parent_names;
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;
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;
3232 INIT_HLIST_HEAD(&core->clks);
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;
3240 ret = __clk_core_init(core);
3241 if (!ret)
3242 return hw->clk;
3244 __clk_free_clk(hw->clk);
3245 hw->clk = NULL;
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);
3261 EXPORT_SYMBOL_GPL(clk_register);
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
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().
3273 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3275 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3277 EXPORT_SYMBOL_GPL(clk_hw_register);
3279 /* Free memory allocated for a clock. */
3280 static void __clk_release(struct kref *ref)
3282 struct clk_core *core = container_of(ref, struct clk_core, ref);
3283 int i = core->num_parents;
3285 lockdep_assert_held(&prepare_lock);
3287 kfree(core->parents);
3288 while (--i >= 0)
3289 kfree_const(core->parent_names[i]);
3291 kfree(core->parent_names);
3292 kfree_const(core->name);
3293 kfree(core);
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.
3301 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3303 return -ENXIO;
3306 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3308 WARN_ON_ONCE(1);
3311 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3312 unsigned long parent_rate)
3314 return -ENXIO;
3317 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3319 return -ENXIO;
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,
3332 * clk_unregister - unregister a currently registered clock
3333 * @clk: clock to unregister
3335 void clk_unregister(struct clk *clk)
3337 unsigned long flags;
3339 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3340 return;
3342 clk_debug_unregister(clk->core);
3344 clk_prepare_lock();
3346 if (clk->core->ops == &clk_nodrv_ops) {
3347 pr_err("%s: unregistered clock: %s\n", __func__,
3348 clk->core->name);
3349 goto unlock;
3352 * Assign empty clock ops for consumers that might still hold
3353 * a reference to this clock.
3355 flags = clk_enable_lock();
3356 clk->core->ops = &clk_nodrv_ops;
3357 clk_enable_unlock(flags);
3359 if (!hlist_empty(&clk->core->children)) {
3360 struct clk_core *child;
3361 struct hlist_node *t;
3363 /* Reparent all children to the orphan list. */
3364 hlist_for_each_entry_safe(child, t, &clk->core->children,
3365 child_node)
3366 clk_core_set_parent_nolock(child, NULL);
3369 hlist_del_init(&clk->core->child_node);
3371 if (clk->core->prepare_count)
3372 pr_warn("%s: unregistering prepared clock: %s\n",
3373 __func__, clk->core->name);
3375 if (clk->core->protect_count)
3376 pr_warn("%s: unregistering protected clock: %s\n",
3377 __func__, clk->core->name);
3379 kref_put(&clk->core->ref, __clk_release);
3380 unlock:
3381 clk_prepare_unlock();
3383 EXPORT_SYMBOL_GPL(clk_unregister);
3386 * clk_hw_unregister - unregister a currently registered clk_hw
3387 * @hw: hardware-specific clock data to unregister
3389 void clk_hw_unregister(struct clk_hw *hw)
3391 clk_unregister(hw->clk);
3393 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3395 static void devm_clk_release(struct device *dev, void *res)
3397 clk_unregister(*(struct clk **)res);
3400 static void devm_clk_hw_release(struct device *dev, void *res)
3402 clk_hw_unregister(*(struct clk_hw **)res);
3406 * devm_clk_register - resource managed clk_register()
3407 * @dev: device that is registering this clock
3408 * @hw: link to hardware-specific clock data
3410 * Managed clk_register(). Clocks returned from this function are
3411 * automatically clk_unregister()ed on driver detach. See clk_register() for
3412 * more information.
3414 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3416 struct clk *clk;
3417 struct clk **clkp;
3419 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3420 if (!clkp)
3421 return ERR_PTR(-ENOMEM);
3423 clk = clk_register(dev, hw);
3424 if (!IS_ERR(clk)) {
3425 *clkp = clk;
3426 devres_add(dev, clkp);
3427 } else {
3428 devres_free(clkp);
3431 return clk;
3433 EXPORT_SYMBOL_GPL(devm_clk_register);
3436 * devm_clk_hw_register - resource managed clk_hw_register()
3437 * @dev: device that is registering this clock
3438 * @hw: link to hardware-specific clock data
3440 * Managed clk_hw_register(). Clocks registered by this function are
3441 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3442 * for more information.
3444 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3446 struct clk_hw **hwp;
3447 int ret;
3449 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3450 if (!hwp)
3451 return -ENOMEM;
3453 ret = clk_hw_register(dev, hw);
3454 if (!ret) {
3455 *hwp = hw;
3456 devres_add(dev, hwp);
3457 } else {
3458 devres_free(hwp);
3461 return ret;
3463 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3465 static int devm_clk_match(struct device *dev, void *res, void *data)
3467 struct clk *c = res;
3468 if (WARN_ON(!c))
3469 return 0;
3470 return c == data;
3473 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3475 struct clk_hw *hw = res;
3477 if (WARN_ON(!hw))
3478 return 0;
3479 return hw == data;
3483 * devm_clk_unregister - resource managed clk_unregister()
3484 * @clk: clock to unregister
3486 * Deallocate a clock allocated with devm_clk_register(). Normally
3487 * this function will not need to be called and the resource management
3488 * code will ensure that the resource is freed.
3490 void devm_clk_unregister(struct device *dev, struct clk *clk)
3492 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3494 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3497 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3498 * @dev: device that is unregistering the hardware-specific clock data
3499 * @hw: link to hardware-specific clock data
3501 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3502 * this function will not need to be called and the resource management
3503 * code will ensure that the resource is freed.
3505 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3507 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3508 hw));
3510 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3513 * clkdev helpers
3515 int __clk_get(struct clk *clk)
3517 struct clk_core *core = !clk ? NULL : clk->core;
3519 if (core) {
3520 if (!try_module_get(core->owner))
3521 return 0;
3523 kref_get(&core->ref);
3525 return 1;
3528 /* keep in sync with __clk_free_clk */
3529 void __clk_put(struct clk *clk)
3531 struct module *owner;
3533 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3534 return;
3536 clk_prepare_lock();
3539 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3540 * given user should be balanced with calls to clk_rate_exclusive_put()
3541 * and by that same consumer
3543 if (WARN_ON(clk->exclusive_count)) {
3544 /* We voiced our concern, let's sanitize the situation */
3545 clk->core->protect_count -= (clk->exclusive_count - 1);
3546 clk_core_rate_unprotect(clk->core);
3547 clk->exclusive_count = 0;
3550 hlist_del(&clk->clks_node);
3551 if (clk->min_rate > clk->core->req_rate ||
3552 clk->max_rate < clk->core->req_rate)
3553 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3555 owner = clk->core->owner;
3556 kref_put(&clk->core->ref, __clk_release);
3558 clk_prepare_unlock();
3560 module_put(owner);
3562 kfree_const(clk->con_id);
3563 kfree(clk);
3566 /*** clk rate change notifiers ***/
3569 * clk_notifier_register - add a clk rate change notifier
3570 * @clk: struct clk * to watch
3571 * @nb: struct notifier_block * with callback info
3573 * Request notification when clk's rate changes. This uses an SRCU
3574 * notifier because we want it to block and notifier unregistrations are
3575 * uncommon. The callbacks associated with the notifier must not
3576 * re-enter into the clk framework by calling any top-level clk APIs;
3577 * this will cause a nested prepare_lock mutex.
3579 * In all notification cases (pre, post and abort rate change) the original
3580 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3581 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3583 * clk_notifier_register() must be called from non-atomic context.
3584 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3585 * allocation failure; otherwise, passes along the return value of
3586 * srcu_notifier_chain_register().
3588 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3590 struct clk_notifier *cn;
3591 int ret = -ENOMEM;
3593 if (!clk || !nb)
3594 return -EINVAL;
3596 clk_prepare_lock();
3598 /* search the list of notifiers for this clk */
3599 list_for_each_entry(cn, &clk_notifier_list, node)
3600 if (cn->clk == clk)
3601 break;
3603 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3604 if (cn->clk != clk) {
3605 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3606 if (!cn)
3607 goto out;
3609 cn->clk = clk;
3610 srcu_init_notifier_head(&cn->notifier_head);
3612 list_add(&cn->node, &clk_notifier_list);
3615 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3617 clk->core->notifier_count++;
3619 out:
3620 clk_prepare_unlock();
3622 return ret;
3624 EXPORT_SYMBOL_GPL(clk_notifier_register);
3627 * clk_notifier_unregister - remove a clk rate change notifier
3628 * @clk: struct clk *
3629 * @nb: struct notifier_block * with callback info
3631 * Request no further notification for changes to 'clk' and frees memory
3632 * allocated in clk_notifier_register.
3634 * Returns -EINVAL if called with null arguments; otherwise, passes
3635 * along the return value of srcu_notifier_chain_unregister().
3637 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3639 struct clk_notifier *cn = NULL;
3640 int ret = -EINVAL;
3642 if (!clk || !nb)
3643 return -EINVAL;
3645 clk_prepare_lock();
3647 list_for_each_entry(cn, &clk_notifier_list, node)
3648 if (cn->clk == clk)
3649 break;
3651 if (cn->clk == clk) {
3652 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3654 clk->core->notifier_count--;
3656 /* XXX the notifier code should handle this better */
3657 if (!cn->notifier_head.head) {
3658 srcu_cleanup_notifier_head(&cn->notifier_head);
3659 list_del(&cn->node);
3660 kfree(cn);
3663 } else {
3664 ret = -ENOENT;
3667 clk_prepare_unlock();
3669 return ret;
3671 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3673 #ifdef CONFIG_OF
3675 * struct of_clk_provider - Clock provider registration structure
3676 * @link: Entry in global list of clock providers
3677 * @node: Pointer to device tree node of clock provider
3678 * @get: Get clock callback. Returns NULL or a struct clk for the
3679 * given clock specifier
3680 * @data: context pointer to be passed into @get callback
3682 struct of_clk_provider {
3683 struct list_head link;
3685 struct device_node *node;
3686 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3687 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3688 void *data;
3691 static const struct of_device_id __clk_of_table_sentinel
3692 __used __section(__clk_of_table_end);
3694 static LIST_HEAD(of_clk_providers);
3695 static DEFINE_MUTEX(of_clk_mutex);
3697 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3698 void *data)
3700 return data;
3702 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3704 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3706 return data;
3708 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3710 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3712 struct clk_onecell_data *clk_data = data;
3713 unsigned int idx = clkspec->args[0];
3715 if (idx >= clk_data->clk_num) {
3716 pr_err("%s: invalid clock index %u\n", __func__, idx);
3717 return ERR_PTR(-EINVAL);
3720 return clk_data->clks[idx];
3722 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3724 struct clk_hw *
3725 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3727 struct clk_hw_onecell_data *hw_data = data;
3728 unsigned int idx = clkspec->args[0];
3730 if (idx >= hw_data->num) {
3731 pr_err("%s: invalid index %u\n", __func__, idx);
3732 return ERR_PTR(-EINVAL);
3735 return hw_data->hws[idx];
3737 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3740 * of_clk_add_provider() - Register a clock provider for a node
3741 * @np: Device node pointer associated with clock provider
3742 * @clk_src_get: callback for decoding clock
3743 * @data: context pointer for @clk_src_get callback.
3745 int of_clk_add_provider(struct device_node *np,
3746 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3747 void *data),
3748 void *data)
3750 struct of_clk_provider *cp;
3751 int ret;
3753 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3754 if (!cp)
3755 return -ENOMEM;
3757 cp->node = of_node_get(np);
3758 cp->data = data;
3759 cp->get = clk_src_get;
3761 mutex_lock(&of_clk_mutex);
3762 list_add(&cp->link, &of_clk_providers);
3763 mutex_unlock(&of_clk_mutex);
3764 pr_debug("Added clock from %pOF\n", np);
3766 ret = of_clk_set_defaults(np, true);
3767 if (ret < 0)
3768 of_clk_del_provider(np);
3770 return ret;
3772 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3775 * of_clk_add_hw_provider() - Register a clock provider for a node
3776 * @np: Device node pointer associated with clock provider
3777 * @get: callback for decoding clk_hw
3778 * @data: context pointer for @get callback.
3780 int of_clk_add_hw_provider(struct device_node *np,
3781 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3782 void *data),
3783 void *data)
3785 struct of_clk_provider *cp;
3786 int ret;
3788 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3789 if (!cp)
3790 return -ENOMEM;
3792 cp->node = of_node_get(np);
3793 cp->data = data;
3794 cp->get_hw = get;
3796 mutex_lock(&of_clk_mutex);
3797 list_add(&cp->link, &of_clk_providers);
3798 mutex_unlock(&of_clk_mutex);
3799 pr_debug("Added clk_hw provider from %pOF\n", np);
3801 ret = of_clk_set_defaults(np, true);
3802 if (ret < 0)
3803 of_clk_del_provider(np);
3805 return ret;
3807 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3809 static void devm_of_clk_release_provider(struct device *dev, void *res)
3811 of_clk_del_provider(*(struct device_node **)res);
3814 int devm_of_clk_add_hw_provider(struct device *dev,
3815 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3816 void *data),
3817 void *data)
3819 struct device_node **ptr, *np;
3820 int ret;
3822 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3823 GFP_KERNEL);
3824 if (!ptr)
3825 return -ENOMEM;
3827 np = dev->of_node;
3828 ret = of_clk_add_hw_provider(np, get, data);
3829 if (!ret) {
3830 *ptr = np;
3831 devres_add(dev, ptr);
3832 } else {
3833 devres_free(ptr);
3836 return ret;
3838 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3841 * of_clk_del_provider() - Remove a previously registered clock provider
3842 * @np: Device node pointer associated with clock provider
3844 void of_clk_del_provider(struct device_node *np)
3846 struct of_clk_provider *cp;
3848 mutex_lock(&of_clk_mutex);
3849 list_for_each_entry(cp, &of_clk_providers, link) {
3850 if (cp->node == np) {
3851 list_del(&cp->link);
3852 of_node_put(cp->node);
3853 kfree(cp);
3854 break;
3857 mutex_unlock(&of_clk_mutex);
3859 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3861 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3863 struct device_node **np = res;
3865 if (WARN_ON(!np || !*np))
3866 return 0;
3868 return *np == data;
3871 void devm_of_clk_del_provider(struct device *dev)
3873 int ret;
3875 ret = devres_release(dev, devm_of_clk_release_provider,
3876 devm_clk_provider_match, dev->of_node);
3878 WARN_ON(ret);
3880 EXPORT_SYMBOL(devm_of_clk_del_provider);
3882 static struct clk_hw *
3883 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3884 struct of_phandle_args *clkspec)
3886 struct clk *clk;
3888 if (provider->get_hw)
3889 return provider->get_hw(clkspec, provider->data);
3891 clk = provider->get(clkspec, provider->data);
3892 if (IS_ERR(clk))
3893 return ERR_CAST(clk);
3894 return __clk_get_hw(clk);
3897 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3898 const char *dev_id, const char *con_id)
3900 struct of_clk_provider *provider;
3901 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3902 struct clk_hw *hw;
3904 if (!clkspec)
3905 return ERR_PTR(-EINVAL);
3907 /* Check if we have such a provider in our array */
3908 mutex_lock(&of_clk_mutex);
3909 list_for_each_entry(provider, &of_clk_providers, link) {
3910 if (provider->node == clkspec->np) {
3911 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3912 clk = __clk_create_clk(hw, dev_id, con_id);
3915 if (!IS_ERR(clk)) {
3916 if (!__clk_get(clk)) {
3917 __clk_free_clk(clk);
3918 clk = ERR_PTR(-ENOENT);
3921 break;
3924 mutex_unlock(&of_clk_mutex);
3926 return clk;
3930 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3931 * @clkspec: pointer to a clock specifier data structure
3933 * This function looks up a struct clk from the registered list of clock
3934 * providers, an input is a clock specifier data structure as returned
3935 * from the of_parse_phandle_with_args() function call.
3937 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3939 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3941 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3944 * of_clk_get_parent_count() - Count the number of clocks a device node has
3945 * @np: device node to count
3947 * Returns: The number of clocks that are possible parents of this node
3949 unsigned int of_clk_get_parent_count(struct device_node *np)
3951 int count;
3953 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3954 if (count < 0)
3955 return 0;
3957 return count;
3959 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3961 const char *of_clk_get_parent_name(struct device_node *np, int index)
3963 struct of_phandle_args clkspec;
3964 struct property *prop;
3965 const char *clk_name;
3966 const __be32 *vp;
3967 u32 pv;
3968 int rc;
3969 int count;
3970 struct clk *clk;
3972 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3973 &clkspec);
3974 if (rc)
3975 return NULL;
3977 index = clkspec.args_count ? clkspec.args[0] : 0;
3978 count = 0;
3980 /* if there is an indices property, use it to transfer the index
3981 * specified into an array offset for the clock-output-names property.
3983 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3984 if (index == pv) {
3985 index = count;
3986 break;
3988 count++;
3990 /* We went off the end of 'clock-indices' without finding it */
3991 if (prop && !vp)
3992 return NULL;
3994 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3995 index,
3996 &clk_name) < 0) {
3998 * Best effort to get the name if the clock has been
3999 * registered with the framework. If the clock isn't
4000 * registered, we return the node name as the name of
4001 * the clock as long as #clock-cells = 0.
4003 clk = of_clk_get_from_provider(&clkspec);
4004 if (IS_ERR(clk)) {
4005 if (clkspec.args_count == 0)
4006 clk_name = clkspec.np->name;
4007 else
4008 clk_name = NULL;
4009 } else {
4010 clk_name = __clk_get_name(clk);
4011 clk_put(clk);
4016 of_node_put(clkspec.np);
4017 return clk_name;
4019 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4022 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4023 * number of parents
4024 * @np: Device node pointer associated with clock provider
4025 * @parents: pointer to char array that hold the parents' names
4026 * @size: size of the @parents array
4028 * Return: number of parents for the clock node.
4030 int of_clk_parent_fill(struct device_node *np, const char **parents,
4031 unsigned int size)
4033 unsigned int i = 0;
4035 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4036 i++;
4038 return i;
4040 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4042 struct clock_provider {
4043 void (*clk_init_cb)(struct device_node *);
4044 struct device_node *np;
4045 struct list_head node;
4049 * This function looks for a parent clock. If there is one, then it
4050 * checks that the provider for this parent clock was initialized, in
4051 * this case the parent clock will be ready.
4053 static int parent_ready(struct device_node *np)
4055 int i = 0;
4057 while (true) {
4058 struct clk *clk = of_clk_get(np, i);
4060 /* this parent is ready we can check the next one */
4061 if (!IS_ERR(clk)) {
4062 clk_put(clk);
4063 i++;
4064 continue;
4067 /* at least one parent is not ready, we exit now */
4068 if (PTR_ERR(clk) == -EPROBE_DEFER)
4069 return 0;
4072 * Here we make assumption that the device tree is
4073 * written correctly. So an error means that there is
4074 * no more parent. As we didn't exit yet, then the
4075 * previous parent are ready. If there is no clock
4076 * parent, no need to wait for them, then we can
4077 * consider their absence as being ready
4079 return 1;
4084 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4085 * @np: Device node pointer associated with clock provider
4086 * @index: clock index
4087 * @flags: pointer to top-level framework flags
4089 * Detects if the clock-critical property exists and, if so, sets the
4090 * corresponding CLK_IS_CRITICAL flag.
4092 * Do not use this function. It exists only for legacy Device Tree
4093 * bindings, such as the one-clock-per-node style that are outdated.
4094 * Those bindings typically put all clock data into .dts and the Linux
4095 * driver has no clock data, thus making it impossible to set this flag
4096 * correctly from the driver. Only those drivers may call
4097 * of_clk_detect_critical from their setup functions.
4099 * Return: error code or zero on success
4101 int of_clk_detect_critical(struct device_node *np,
4102 int index, unsigned long *flags)
4104 struct property *prop;
4105 const __be32 *cur;
4106 uint32_t idx;
4108 if (!np || !flags)
4109 return -EINVAL;
4111 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4112 if (index == idx)
4113 *flags |= CLK_IS_CRITICAL;
4115 return 0;
4119 * of_clk_init() - Scan and init clock providers from the DT
4120 * @matches: array of compatible values and init functions for providers.
4122 * This function scans the device tree for matching clock providers
4123 * and calls their initialization functions. It also does it by trying
4124 * to follow the dependencies.
4126 void __init of_clk_init(const struct of_device_id *matches)
4128 const struct of_device_id *match;
4129 struct device_node *np;
4130 struct clock_provider *clk_provider, *next;
4131 bool is_init_done;
4132 bool force = false;
4133 LIST_HEAD(clk_provider_list);
4135 if (!matches)
4136 matches = &__clk_of_table;
4138 /* First prepare the list of the clocks providers */
4139 for_each_matching_node_and_match(np, matches, &match) {
4140 struct clock_provider *parent;
4142 if (!of_device_is_available(np))
4143 continue;
4145 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4146 if (!parent) {
4147 list_for_each_entry_safe(clk_provider, next,
4148 &clk_provider_list, node) {
4149 list_del(&clk_provider->node);
4150 of_node_put(clk_provider->np);
4151 kfree(clk_provider);
4153 of_node_put(np);
4154 return;
4157 parent->clk_init_cb = match->data;
4158 parent->np = of_node_get(np);
4159 list_add_tail(&parent->node, &clk_provider_list);
4162 while (!list_empty(&clk_provider_list)) {
4163 is_init_done = false;
4164 list_for_each_entry_safe(clk_provider, next,
4165 &clk_provider_list, node) {
4166 if (force || parent_ready(clk_provider->np)) {
4168 /* Don't populate platform devices */
4169 of_node_set_flag(clk_provider->np,
4170 OF_POPULATED);
4172 clk_provider->clk_init_cb(clk_provider->np);
4173 of_clk_set_defaults(clk_provider->np, true);
4175 list_del(&clk_provider->node);
4176 of_node_put(clk_provider->np);
4177 kfree(clk_provider);
4178 is_init_done = true;
4183 * We didn't manage to initialize any of the
4184 * remaining providers during the last loop, so now we
4185 * initialize all the remaining ones unconditionally
4186 * in case the clock parent was not mandatory
4188 if (!is_init_done)
4189 force = true;
4192 #endif