1 // SPDX-License-Identifier: GPL-2.0
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/sched.h>
23 #include <linux/clkdev.h>
27 static DEFINE_SPINLOCK(enable_lock);
28 static DEFINE_MUTEX(prepare_lock);
30 static struct task_struct *prepare_owner;
31 static struct task_struct *enable_owner;
33 static int prepare_refcnt;
34 static int enable_refcnt;
36 static HLIST_HEAD(clk_root_list);
37 static HLIST_HEAD(clk_orphan_list);
38 static LIST_HEAD(clk_notifier_list);
40 /*** private data structures ***/
44 const struct clk_ops *ops;
48 struct clk_core *parent;
49 const char **parent_names;
50 struct clk_core **parents;
54 unsigned long req_rate;
55 unsigned long new_rate;
56 struct clk_core *new_parent;
57 struct clk_core *new_child;
60 unsigned int enable_count;
61 unsigned int prepare_count;
62 unsigned int protect_count;
63 unsigned long min_rate;
64 unsigned long max_rate;
65 unsigned long accuracy;
68 struct hlist_head children;
69 struct hlist_node child_node;
70 struct hlist_head clks;
71 unsigned int notifier_count;
72 #ifdef CONFIG_DEBUG_FS
73 struct dentry *dentry;
74 struct hlist_node debug_node;
79 #define CREATE_TRACE_POINTS
80 #include <trace/events/clk.h>
83 struct clk_core *core;
86 unsigned long min_rate;
87 unsigned long max_rate;
88 unsigned int exclusive_count;
89 struct hlist_node clks_node;
93 static int clk_pm_runtime_get(struct clk_core *core)
100 ret = pm_runtime_get_sync(core->dev);
101 return ret < 0 ? ret : 0;
104 static void clk_pm_runtime_put(struct clk_core *core)
109 pm_runtime_put_sync(core->dev);
113 static void clk_prepare_lock(void)
115 if (!mutex_trylock(&prepare_lock)) {
116 if (prepare_owner == current) {
120 mutex_lock(&prepare_lock);
122 WARN_ON_ONCE(prepare_owner != NULL);
123 WARN_ON_ONCE(prepare_refcnt != 0);
124 prepare_owner = current;
128 static void clk_prepare_unlock(void)
130 WARN_ON_ONCE(prepare_owner != current);
131 WARN_ON_ONCE(prepare_refcnt == 0);
133 if (--prepare_refcnt)
135 prepare_owner = NULL;
136 mutex_unlock(&prepare_lock);
139 static unsigned long clk_enable_lock(void)
140 __acquires(enable_lock)
145 * On UP systems, spin_trylock_irqsave() always returns true, even if
146 * we already hold the lock. So, in that case, we rely only on
147 * reference counting.
149 if (!IS_ENABLED(CONFIG_SMP) ||
150 !spin_trylock_irqsave(&enable_lock, flags)) {
151 if (enable_owner == current) {
153 __acquire(enable_lock);
154 if (!IS_ENABLED(CONFIG_SMP))
155 local_save_flags(flags);
158 spin_lock_irqsave(&enable_lock, flags);
160 WARN_ON_ONCE(enable_owner != NULL);
161 WARN_ON_ONCE(enable_refcnt != 0);
162 enable_owner = current;
167 static void clk_enable_unlock(unsigned long flags)
168 __releases(enable_lock)
170 WARN_ON_ONCE(enable_owner != current);
171 WARN_ON_ONCE(enable_refcnt == 0);
173 if (--enable_refcnt) {
174 __release(enable_lock);
178 spin_unlock_irqrestore(&enable_lock, flags);
181 static bool clk_core_rate_is_protected(struct clk_core *core)
183 return core->protect_count;
186 static bool clk_core_is_prepared(struct clk_core *core)
191 * .is_prepared is optional for clocks that can prepare
192 * fall back to software usage counter if it is missing
194 if (!core->ops->is_prepared)
195 return core->prepare_count;
197 if (!clk_pm_runtime_get(core)) {
198 ret = core->ops->is_prepared(core->hw);
199 clk_pm_runtime_put(core);
205 static bool clk_core_is_enabled(struct clk_core *core)
210 * .is_enabled is only mandatory for clocks that gate
211 * fall back to software usage counter if .is_enabled is missing
213 if (!core->ops->is_enabled)
214 return core->enable_count;
217 * Check if clock controller's device is runtime active before
218 * calling .is_enabled callback. If not, assume that clock is
219 * disabled, because we might be called from atomic context, from
220 * which pm_runtime_get() is not allowed.
221 * This function is called mainly from clk_disable_unused_subtree,
222 * which ensures proper runtime pm activation of controller before
223 * taking enable spinlock, but the below check is needed if one tries
224 * to call it from other places.
227 pm_runtime_get_noresume(core->dev);
228 if (!pm_runtime_active(core->dev)) {
234 ret = core->ops->is_enabled(core->hw);
237 pm_runtime_put(core->dev);
242 /*** helper functions ***/
244 const char *__clk_get_name(const struct clk *clk)
246 return !clk ? NULL : clk->core->name;
248 EXPORT_SYMBOL_GPL(__clk_get_name);
250 const char *clk_hw_get_name(const struct clk_hw *hw)
252 return hw->core->name;
254 EXPORT_SYMBOL_GPL(clk_hw_get_name);
256 struct clk_hw *__clk_get_hw(struct clk *clk)
258 return !clk ? NULL : clk->core->hw;
260 EXPORT_SYMBOL_GPL(__clk_get_hw);
262 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
264 return hw->core->num_parents;
266 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
268 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
270 return hw->core->parent ? hw->core->parent->hw : NULL;
272 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
274 static struct clk_core *__clk_lookup_subtree(const char *name,
275 struct clk_core *core)
277 struct clk_core *child;
278 struct clk_core *ret;
280 if (!strcmp(core->name, name))
283 hlist_for_each_entry(child, &core->children, child_node) {
284 ret = __clk_lookup_subtree(name, child);
292 static struct clk_core *clk_core_lookup(const char *name)
294 struct clk_core *root_clk;
295 struct clk_core *ret;
300 /* search the 'proper' clk tree first */
301 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
302 ret = __clk_lookup_subtree(name, root_clk);
307 /* if not found, then search the orphan tree */
308 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
309 ret = __clk_lookup_subtree(name, root_clk);
317 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
320 if (!core || index >= core->num_parents)
323 if (!core->parents[index])
324 core->parents[index] =
325 clk_core_lookup(core->parent_names[index]);
327 return core->parents[index];
331 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
333 struct clk_core *parent;
335 parent = clk_core_get_parent_by_index(hw->core, index);
337 return !parent ? NULL : parent->hw;
339 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
341 unsigned int __clk_get_enable_count(struct clk *clk)
343 return !clk ? 0 : clk->core->enable_count;
346 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
357 if (!core->num_parents)
367 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
369 return clk_core_get_rate_nolock(hw->core);
371 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
373 static unsigned long __clk_get_accuracy(struct clk_core *core)
378 return core->accuracy;
381 unsigned long __clk_get_flags(struct clk *clk)
383 return !clk ? 0 : clk->core->flags;
385 EXPORT_SYMBOL_GPL(__clk_get_flags);
387 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
389 return hw->core->flags;
391 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
393 bool clk_hw_is_prepared(const struct clk_hw *hw)
395 return clk_core_is_prepared(hw->core);
398 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
400 return clk_core_rate_is_protected(hw->core);
403 bool clk_hw_is_enabled(const struct clk_hw *hw)
405 return clk_core_is_enabled(hw->core);
408 bool __clk_is_enabled(struct clk *clk)
413 return clk_core_is_enabled(clk->core);
415 EXPORT_SYMBOL_GPL(__clk_is_enabled);
417 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
418 unsigned long best, unsigned long flags)
420 if (flags & CLK_MUX_ROUND_CLOSEST)
421 return abs(now - rate) < abs(best - rate);
423 return now <= rate && now > best;
426 int clk_mux_determine_rate_flags(struct clk_hw *hw,
427 struct clk_rate_request *req,
430 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
431 int i, num_parents, ret;
432 unsigned long best = 0;
433 struct clk_rate_request parent_req = *req;
435 /* if NO_REPARENT flag set, pass through to current parent */
436 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
437 parent = core->parent;
438 if (core->flags & CLK_SET_RATE_PARENT) {
439 ret = __clk_determine_rate(parent ? parent->hw : NULL,
444 best = parent_req.rate;
446 best = clk_core_get_rate_nolock(parent);
448 best = clk_core_get_rate_nolock(core);
454 /* find the parent that can provide the fastest rate <= rate */
455 num_parents = core->num_parents;
456 for (i = 0; i < num_parents; i++) {
457 parent = clk_core_get_parent_by_index(core, i);
461 if (core->flags & CLK_SET_RATE_PARENT) {
463 ret = __clk_determine_rate(parent->hw, &parent_req);
467 parent_req.rate = clk_core_get_rate_nolock(parent);
470 if (mux_is_better_rate(req->rate, parent_req.rate,
472 best_parent = parent;
473 best = parent_req.rate;
482 req->best_parent_hw = best_parent->hw;
483 req->best_parent_rate = best;
488 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
490 struct clk *__clk_lookup(const char *name)
492 struct clk_core *core = clk_core_lookup(name);
494 return !core ? NULL : core->hw->clk;
497 static void clk_core_get_boundaries(struct clk_core *core,
498 unsigned long *min_rate,
499 unsigned long *max_rate)
501 struct clk *clk_user;
503 *min_rate = core->min_rate;
504 *max_rate = core->max_rate;
506 hlist_for_each_entry(clk_user, &core->clks, clks_node)
507 *min_rate = max(*min_rate, clk_user->min_rate);
509 hlist_for_each_entry(clk_user, &core->clks, clks_node)
510 *max_rate = min(*max_rate, clk_user->max_rate);
513 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
514 unsigned long max_rate)
516 hw->core->min_rate = min_rate;
517 hw->core->max_rate = max_rate;
519 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
522 * Helper for finding best parent to provide a given frequency. This can be used
523 * directly as a determine_rate callback (e.g. for a mux), or from a more
524 * complex clock that may combine a mux with other operations.
526 int __clk_mux_determine_rate(struct clk_hw *hw,
527 struct clk_rate_request *req)
529 return clk_mux_determine_rate_flags(hw, req, 0);
531 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
533 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
534 struct clk_rate_request *req)
536 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
538 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
542 static void clk_core_rate_unprotect(struct clk_core *core)
544 lockdep_assert_held(&prepare_lock);
549 if (WARN(core->protect_count == 0,
550 "%s already unprotected\n", core->name))
553 if (--core->protect_count > 0)
556 clk_core_rate_unprotect(core->parent);
559 static int clk_core_rate_nuke_protect(struct clk_core *core)
563 lockdep_assert_held(&prepare_lock);
568 if (core->protect_count == 0)
571 ret = core->protect_count;
572 core->protect_count = 1;
573 clk_core_rate_unprotect(core);
579 * clk_rate_exclusive_put - release exclusivity over clock rate control
580 * @clk: the clk over which the exclusivity is released
582 * clk_rate_exclusive_put() completes a critical section during which a clock
583 * consumer cannot tolerate any other consumer making any operation on the
584 * clock which could result in a rate change or rate glitch. Exclusive clocks
585 * cannot have their rate changed, either directly or indirectly due to changes
586 * further up the parent chain of clocks. As a result, clocks up parent chain
587 * also get under exclusive control of the calling consumer.
589 * If exlusivity is claimed more than once on clock, even by the same consumer,
590 * the rate effectively gets locked as exclusivity can't be preempted.
592 * Calls to clk_rate_exclusive_put() must be balanced with calls to
593 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
596 void clk_rate_exclusive_put(struct clk *clk)
604 * if there is something wrong with this consumer protect count, stop
605 * here before messing with the provider
607 if (WARN_ON(clk->exclusive_count <= 0))
610 clk_core_rate_unprotect(clk->core);
611 clk->exclusive_count--;
613 clk_prepare_unlock();
615 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
617 static void clk_core_rate_protect(struct clk_core *core)
619 lockdep_assert_held(&prepare_lock);
624 if (core->protect_count == 0)
625 clk_core_rate_protect(core->parent);
627 core->protect_count++;
630 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
632 lockdep_assert_held(&prepare_lock);
640 clk_core_rate_protect(core);
641 core->protect_count = count;
645 * clk_rate_exclusive_get - get exclusivity over the clk rate control
646 * @clk: the clk over which the exclusity of rate control is requested
648 * clk_rate_exlusive_get() begins a critical section during which a clock
649 * consumer cannot tolerate any other consumer making any operation on the
650 * clock which could result in a rate change or rate glitch. Exclusive clocks
651 * cannot have their rate changed, either directly or indirectly due to changes
652 * further up the parent chain of clocks. As a result, clocks up parent chain
653 * also get under exclusive control of the calling consumer.
655 * If exlusivity is claimed more than once on clock, even by the same consumer,
656 * the rate effectively gets locked as exclusivity can't be preempted.
658 * Calls to clk_rate_exclusive_get() should be balanced with calls to
659 * clk_rate_exclusive_put(). Calls to this function may sleep.
660 * Returns 0 on success, -EERROR otherwise
662 int clk_rate_exclusive_get(struct clk *clk)
668 clk_core_rate_protect(clk->core);
669 clk->exclusive_count++;
670 clk_prepare_unlock();
674 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
676 static void clk_core_unprepare(struct clk_core *core)
678 lockdep_assert_held(&prepare_lock);
683 if (WARN(core->prepare_count == 0,
684 "%s already unprepared\n", core->name))
687 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
688 "Unpreparing critical %s\n", core->name))
691 if (core->flags & CLK_SET_RATE_GATE)
692 clk_core_rate_unprotect(core);
694 if (--core->prepare_count > 0)
697 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
699 trace_clk_unprepare(core);
701 if (core->ops->unprepare)
702 core->ops->unprepare(core->hw);
704 clk_pm_runtime_put(core);
706 trace_clk_unprepare_complete(core);
707 clk_core_unprepare(core->parent);
710 static void clk_core_unprepare_lock(struct clk_core *core)
713 clk_core_unprepare(core);
714 clk_prepare_unlock();
718 * clk_unprepare - undo preparation of a clock source
719 * @clk: the clk being unprepared
721 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
722 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
723 * if the operation may sleep. One example is a clk which is accessed over
724 * I2c. In the complex case a clk gate operation may require a fast and a slow
725 * part. It is this reason that clk_unprepare and clk_disable are not mutually
726 * exclusive. In fact clk_disable must be called before clk_unprepare.
728 void clk_unprepare(struct clk *clk)
730 if (IS_ERR_OR_NULL(clk))
733 clk_core_unprepare_lock(clk->core);
735 EXPORT_SYMBOL_GPL(clk_unprepare);
737 static int clk_core_prepare(struct clk_core *core)
741 lockdep_assert_held(&prepare_lock);
746 if (core->prepare_count == 0) {
747 ret = clk_pm_runtime_get(core);
751 ret = clk_core_prepare(core->parent);
755 trace_clk_prepare(core);
757 if (core->ops->prepare)
758 ret = core->ops->prepare(core->hw);
760 trace_clk_prepare_complete(core);
766 core->prepare_count++;
769 * CLK_SET_RATE_GATE is a special case of clock protection
770 * Instead of a consumer claiming exclusive rate control, it is
771 * actually the provider which prevents any consumer from making any
772 * operation which could result in a rate change or rate glitch while
773 * the clock is prepared.
775 if (core->flags & CLK_SET_RATE_GATE)
776 clk_core_rate_protect(core);
780 clk_core_unprepare(core->parent);
782 clk_pm_runtime_put(core);
786 static int clk_core_prepare_lock(struct clk_core *core)
791 ret = clk_core_prepare(core);
792 clk_prepare_unlock();
798 * clk_prepare - prepare a clock source
799 * @clk: the clk being prepared
801 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
802 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
803 * operation may sleep. One example is a clk which is accessed over I2c. In
804 * the complex case a clk ungate operation may require a fast and a slow part.
805 * It is this reason that clk_prepare and clk_enable are not mutually
806 * exclusive. In fact clk_prepare must be called before clk_enable.
807 * Returns 0 on success, -EERROR otherwise.
809 int clk_prepare(struct clk *clk)
814 return clk_core_prepare_lock(clk->core);
816 EXPORT_SYMBOL_GPL(clk_prepare);
818 static void clk_core_disable(struct clk_core *core)
820 lockdep_assert_held(&enable_lock);
825 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
828 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
829 "Disabling critical %s\n", core->name))
832 if (--core->enable_count > 0)
835 trace_clk_disable_rcuidle(core);
837 if (core->ops->disable)
838 core->ops->disable(core->hw);
840 trace_clk_disable_complete_rcuidle(core);
842 clk_core_disable(core->parent);
845 static void clk_core_disable_lock(struct clk_core *core)
849 flags = clk_enable_lock();
850 clk_core_disable(core);
851 clk_enable_unlock(flags);
855 * clk_disable - gate a clock
856 * @clk: the clk being gated
858 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
859 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
860 * clk if the operation is fast and will never sleep. One example is a
861 * SoC-internal clk which is controlled via simple register writes. In the
862 * complex case a clk gate operation may require a fast and a slow part. It is
863 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
864 * In fact clk_disable must be called before clk_unprepare.
866 void clk_disable(struct clk *clk)
868 if (IS_ERR_OR_NULL(clk))
871 clk_core_disable_lock(clk->core);
873 EXPORT_SYMBOL_GPL(clk_disable);
875 static int clk_core_enable(struct clk_core *core)
879 lockdep_assert_held(&enable_lock);
884 if (WARN(core->prepare_count == 0,
885 "Enabling unprepared %s\n", core->name))
888 if (core->enable_count == 0) {
889 ret = clk_core_enable(core->parent);
894 trace_clk_enable_rcuidle(core);
896 if (core->ops->enable)
897 ret = core->ops->enable(core->hw);
899 trace_clk_enable_complete_rcuidle(core);
902 clk_core_disable(core->parent);
907 core->enable_count++;
911 static int clk_core_enable_lock(struct clk_core *core)
916 flags = clk_enable_lock();
917 ret = clk_core_enable(core);
918 clk_enable_unlock(flags);
924 * clk_gate_restore_context - restore context for poweroff
925 * @hw: the clk_hw pointer of clock whose state is to be restored
927 * The clock gate restore context function enables or disables
928 * the gate clocks based on the enable_count. This is done in cases
929 * where the clock context is lost and based on the enable_count
930 * the clock either needs to be enabled/disabled. This
931 * helps restore the state of gate clocks.
933 void clk_gate_restore_context(struct clk_hw *hw)
935 struct clk_core *core = hw->core;
937 if (core->enable_count)
938 core->ops->enable(hw);
940 core->ops->disable(hw);
942 EXPORT_SYMBOL_GPL(clk_gate_restore_context);
944 static int clk_core_save_context(struct clk_core *core)
946 struct clk_core *child;
949 hlist_for_each_entry(child, &core->children, child_node) {
950 ret = clk_core_save_context(child);
955 if (core->ops && core->ops->save_context)
956 ret = core->ops->save_context(core->hw);
961 static void clk_core_restore_context(struct clk_core *core)
963 struct clk_core *child;
965 if (core->ops && core->ops->restore_context)
966 core->ops->restore_context(core->hw);
968 hlist_for_each_entry(child, &core->children, child_node)
969 clk_core_restore_context(child);
973 * clk_save_context - save clock context for poweroff
975 * Saves the context of the clock register for powerstates in which the
976 * contents of the registers will be lost. Occurs deep within the suspend
977 * code. Returns 0 on success.
979 int clk_save_context(void)
981 struct clk_core *clk;
984 hlist_for_each_entry(clk, &clk_root_list, child_node) {
985 ret = clk_core_save_context(clk);
990 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
991 ret = clk_core_save_context(clk);
998 EXPORT_SYMBOL_GPL(clk_save_context);
1001 * clk_restore_context - restore clock context after poweroff
1003 * Restore the saved clock context upon resume.
1006 void clk_restore_context(void)
1008 struct clk_core *core;
1010 hlist_for_each_entry(core, &clk_root_list, child_node)
1011 clk_core_restore_context(core);
1013 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1014 clk_core_restore_context(core);
1016 EXPORT_SYMBOL_GPL(clk_restore_context);
1019 * clk_enable - ungate a clock
1020 * @clk: the clk being ungated
1022 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1023 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1024 * if the operation will never sleep. One example is a SoC-internal clk which
1025 * is controlled via simple register writes. In the complex case a clk ungate
1026 * operation may require a fast and a slow part. It is this reason that
1027 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1028 * must be called before clk_enable. Returns 0 on success, -EERROR
1031 int clk_enable(struct clk *clk)
1036 return clk_core_enable_lock(clk->core);
1038 EXPORT_SYMBOL_GPL(clk_enable);
1040 static int clk_core_prepare_enable(struct clk_core *core)
1044 ret = clk_core_prepare_lock(core);
1048 ret = clk_core_enable_lock(core);
1050 clk_core_unprepare_lock(core);
1055 static void clk_core_disable_unprepare(struct clk_core *core)
1057 clk_core_disable_lock(core);
1058 clk_core_unprepare_lock(core);
1061 static void clk_unprepare_unused_subtree(struct clk_core *core)
1063 struct clk_core *child;
1065 lockdep_assert_held(&prepare_lock);
1067 hlist_for_each_entry(child, &core->children, child_node)
1068 clk_unprepare_unused_subtree(child);
1070 if (core->prepare_count)
1073 if (core->flags & CLK_IGNORE_UNUSED)
1076 if (clk_pm_runtime_get(core))
1079 if (clk_core_is_prepared(core)) {
1080 trace_clk_unprepare(core);
1081 if (core->ops->unprepare_unused)
1082 core->ops->unprepare_unused(core->hw);
1083 else if (core->ops->unprepare)
1084 core->ops->unprepare(core->hw);
1085 trace_clk_unprepare_complete(core);
1088 clk_pm_runtime_put(core);
1091 static void clk_disable_unused_subtree(struct clk_core *core)
1093 struct clk_core *child;
1094 unsigned long flags;
1096 lockdep_assert_held(&prepare_lock);
1098 hlist_for_each_entry(child, &core->children, child_node)
1099 clk_disable_unused_subtree(child);
1101 if (core->flags & CLK_OPS_PARENT_ENABLE)
1102 clk_core_prepare_enable(core->parent);
1104 if (clk_pm_runtime_get(core))
1107 flags = clk_enable_lock();
1109 if (core->enable_count)
1112 if (core->flags & CLK_IGNORE_UNUSED)
1116 * some gate clocks have special needs during the disable-unused
1117 * sequence. call .disable_unused if available, otherwise fall
1120 if (clk_core_is_enabled(core)) {
1121 trace_clk_disable(core);
1122 if (core->ops->disable_unused)
1123 core->ops->disable_unused(core->hw);
1124 else if (core->ops->disable)
1125 core->ops->disable(core->hw);
1126 trace_clk_disable_complete(core);
1130 clk_enable_unlock(flags);
1131 clk_pm_runtime_put(core);
1133 if (core->flags & CLK_OPS_PARENT_ENABLE)
1134 clk_core_disable_unprepare(core->parent);
1137 static bool clk_ignore_unused;
1138 static int __init clk_ignore_unused_setup(char *__unused)
1140 clk_ignore_unused = true;
1143 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1145 static int clk_disable_unused(void)
1147 struct clk_core *core;
1149 if (clk_ignore_unused) {
1150 pr_warn("clk: Not disabling unused clocks\n");
1156 hlist_for_each_entry(core, &clk_root_list, child_node)
1157 clk_disable_unused_subtree(core);
1159 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1160 clk_disable_unused_subtree(core);
1162 hlist_for_each_entry(core, &clk_root_list, child_node)
1163 clk_unprepare_unused_subtree(core);
1165 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1166 clk_unprepare_unused_subtree(core);
1168 clk_prepare_unlock();
1172 late_initcall_sync(clk_disable_unused);
1174 static int clk_core_determine_round_nolock(struct clk_core *core,
1175 struct clk_rate_request *req)
1179 lockdep_assert_held(&prepare_lock);
1185 * At this point, core protection will be disabled if
1186 * - if the provider is not protected at all
1187 * - if the calling consumer is the only one which has exclusivity
1190 if (clk_core_rate_is_protected(core)) {
1191 req->rate = core->rate;
1192 } else if (core->ops->determine_rate) {
1193 return core->ops->determine_rate(core->hw, req);
1194 } else if (core->ops->round_rate) {
1195 rate = core->ops->round_rate(core->hw, req->rate,
1196 &req->best_parent_rate);
1208 static void clk_core_init_rate_req(struct clk_core * const core,
1209 struct clk_rate_request *req)
1211 struct clk_core *parent;
1213 if (WARN_ON(!core || !req))
1216 parent = core->parent;
1218 req->best_parent_hw = parent->hw;
1219 req->best_parent_rate = parent->rate;
1221 req->best_parent_hw = NULL;
1222 req->best_parent_rate = 0;
1226 static bool clk_core_can_round(struct clk_core * const core)
1228 if (core->ops->determine_rate || core->ops->round_rate)
1234 static int clk_core_round_rate_nolock(struct clk_core *core,
1235 struct clk_rate_request *req)
1237 lockdep_assert_held(&prepare_lock);
1244 clk_core_init_rate_req(core, req);
1246 if (clk_core_can_round(core))
1247 return clk_core_determine_round_nolock(core, req);
1248 else if (core->flags & CLK_SET_RATE_PARENT)
1249 return clk_core_round_rate_nolock(core->parent, req);
1251 req->rate = core->rate;
1256 * __clk_determine_rate - get the closest rate actually supported by a clock
1257 * @hw: determine the rate of this clock
1258 * @req: target rate request
1260 * Useful for clk_ops such as .set_rate and .determine_rate.
1262 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1269 return clk_core_round_rate_nolock(hw->core, req);
1271 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1273 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1276 struct clk_rate_request req;
1278 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1281 ret = clk_core_round_rate_nolock(hw->core, &req);
1287 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1290 * clk_round_rate - round the given rate for a clk
1291 * @clk: the clk for which we are rounding a rate
1292 * @rate: the rate which is to be rounded
1294 * Takes in a rate as input and rounds it to a rate that the clk can actually
1295 * use which is then returned. If clk doesn't support round_rate operation
1296 * then the parent rate is returned.
1298 long clk_round_rate(struct clk *clk, unsigned long rate)
1300 struct clk_rate_request req;
1308 if (clk->exclusive_count)
1309 clk_core_rate_unprotect(clk->core);
1311 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1314 ret = clk_core_round_rate_nolock(clk->core, &req);
1316 if (clk->exclusive_count)
1317 clk_core_rate_protect(clk->core);
1319 clk_prepare_unlock();
1326 EXPORT_SYMBOL_GPL(clk_round_rate);
1329 * __clk_notify - call clk notifier chain
1330 * @core: clk that is changing rate
1331 * @msg: clk notifier type (see include/linux/clk.h)
1332 * @old_rate: old clk rate
1333 * @new_rate: new clk rate
1335 * Triggers a notifier call chain on the clk rate-change notification
1336 * for 'clk'. Passes a pointer to the struct clk and the previous
1337 * and current rates to the notifier callback. Intended to be called by
1338 * internal clock code only. Returns NOTIFY_DONE from the last driver
1339 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1340 * a driver returns that.
1342 static int __clk_notify(struct clk_core *core, unsigned long msg,
1343 unsigned long old_rate, unsigned long new_rate)
1345 struct clk_notifier *cn;
1346 struct clk_notifier_data cnd;
1347 int ret = NOTIFY_DONE;
1349 cnd.old_rate = old_rate;
1350 cnd.new_rate = new_rate;
1352 list_for_each_entry(cn, &clk_notifier_list, node) {
1353 if (cn->clk->core == core) {
1355 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1357 if (ret & NOTIFY_STOP_MASK)
1366 * __clk_recalc_accuracies
1367 * @core: first clk in the subtree
1369 * Walks the subtree of clks starting with clk and recalculates accuracies as
1370 * it goes. Note that if a clk does not implement the .recalc_accuracy
1371 * callback then it is assumed that the clock will take on the accuracy of its
1374 static void __clk_recalc_accuracies(struct clk_core *core)
1376 unsigned long parent_accuracy = 0;
1377 struct clk_core *child;
1379 lockdep_assert_held(&prepare_lock);
1382 parent_accuracy = core->parent->accuracy;
1384 if (core->ops->recalc_accuracy)
1385 core->accuracy = core->ops->recalc_accuracy(core->hw,
1388 core->accuracy = parent_accuracy;
1390 hlist_for_each_entry(child, &core->children, child_node)
1391 __clk_recalc_accuracies(child);
1394 static long clk_core_get_accuracy(struct clk_core *core)
1396 unsigned long accuracy;
1399 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1400 __clk_recalc_accuracies(core);
1402 accuracy = __clk_get_accuracy(core);
1403 clk_prepare_unlock();
1409 * clk_get_accuracy - return the accuracy of clk
1410 * @clk: the clk whose accuracy is being returned
1412 * Simply returns the cached accuracy of the clk, unless
1413 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1415 * If clk is NULL then returns 0.
1417 long clk_get_accuracy(struct clk *clk)
1422 return clk_core_get_accuracy(clk->core);
1424 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1426 static unsigned long clk_recalc(struct clk_core *core,
1427 unsigned long parent_rate)
1429 unsigned long rate = parent_rate;
1431 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1432 rate = core->ops->recalc_rate(core->hw, parent_rate);
1433 clk_pm_runtime_put(core);
1439 * __clk_recalc_rates
1440 * @core: first clk in the subtree
1441 * @msg: notification type (see include/linux/clk.h)
1443 * Walks the subtree of clks starting with clk and recalculates rates as it
1444 * goes. Note that if a clk does not implement the .recalc_rate callback then
1445 * it is assumed that the clock will take on the rate of its parent.
1447 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1450 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1452 unsigned long old_rate;
1453 unsigned long parent_rate = 0;
1454 struct clk_core *child;
1456 lockdep_assert_held(&prepare_lock);
1458 old_rate = core->rate;
1461 parent_rate = core->parent->rate;
1463 core->rate = clk_recalc(core, parent_rate);
1466 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1467 * & ABORT_RATE_CHANGE notifiers
1469 if (core->notifier_count && msg)
1470 __clk_notify(core, msg, old_rate, core->rate);
1472 hlist_for_each_entry(child, &core->children, child_node)
1473 __clk_recalc_rates(child, msg);
1476 static unsigned long clk_core_get_rate(struct clk_core *core)
1482 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1483 __clk_recalc_rates(core, 0);
1485 rate = clk_core_get_rate_nolock(core);
1486 clk_prepare_unlock();
1492 * clk_get_rate - return the rate of clk
1493 * @clk: the clk whose rate is being returned
1495 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1496 * is set, which means a recalc_rate will be issued.
1497 * If clk is NULL then returns 0.
1499 unsigned long clk_get_rate(struct clk *clk)
1504 return clk_core_get_rate(clk->core);
1506 EXPORT_SYMBOL_GPL(clk_get_rate);
1508 static int clk_fetch_parent_index(struct clk_core *core,
1509 struct clk_core *parent)
1516 for (i = 0; i < core->num_parents; i++)
1517 if (clk_core_get_parent_by_index(core, i) == parent)
1524 * Update the orphan status of @core and all its children.
1526 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1528 struct clk_core *child;
1530 core->orphan = is_orphan;
1532 hlist_for_each_entry(child, &core->children, child_node)
1533 clk_core_update_orphan_status(child, is_orphan);
1536 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1538 bool was_orphan = core->orphan;
1540 hlist_del(&core->child_node);
1543 bool becomes_orphan = new_parent->orphan;
1545 /* avoid duplicate POST_RATE_CHANGE notifications */
1546 if (new_parent->new_child == core)
1547 new_parent->new_child = NULL;
1549 hlist_add_head(&core->child_node, &new_parent->children);
1551 if (was_orphan != becomes_orphan)
1552 clk_core_update_orphan_status(core, becomes_orphan);
1554 hlist_add_head(&core->child_node, &clk_orphan_list);
1556 clk_core_update_orphan_status(core, true);
1559 core->parent = new_parent;
1562 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1563 struct clk_core *parent)
1565 unsigned long flags;
1566 struct clk_core *old_parent = core->parent;
1569 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1571 * 2. Migrate prepare state between parents and prevent race with
1574 * If the clock is not prepared, then a race with
1575 * clk_enable/disable() is impossible since we already have the
1576 * prepare lock (future calls to clk_enable() need to be preceded by
1579 * If the clock is prepared, migrate the prepared state to the new
1580 * parent and also protect against a race with clk_enable() by
1581 * forcing the clock and the new parent on. This ensures that all
1582 * future calls to clk_enable() are practically NOPs with respect to
1583 * hardware and software states.
1585 * See also: Comment for clk_set_parent() below.
1588 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1589 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1590 clk_core_prepare_enable(old_parent);
1591 clk_core_prepare_enable(parent);
1594 /* migrate prepare count if > 0 */
1595 if (core->prepare_count) {
1596 clk_core_prepare_enable(parent);
1597 clk_core_enable_lock(core);
1600 /* update the clk tree topology */
1601 flags = clk_enable_lock();
1602 clk_reparent(core, parent);
1603 clk_enable_unlock(flags);
1608 static void __clk_set_parent_after(struct clk_core *core,
1609 struct clk_core *parent,
1610 struct clk_core *old_parent)
1613 * Finish the migration of prepare state and undo the changes done
1614 * for preventing a race with clk_enable().
1616 if (core->prepare_count) {
1617 clk_core_disable_lock(core);
1618 clk_core_disable_unprepare(old_parent);
1621 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1622 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1623 clk_core_disable_unprepare(parent);
1624 clk_core_disable_unprepare(old_parent);
1628 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1631 unsigned long flags;
1633 struct clk_core *old_parent;
1635 old_parent = __clk_set_parent_before(core, parent);
1637 trace_clk_set_parent(core, parent);
1639 /* change clock input source */
1640 if (parent && core->ops->set_parent)
1641 ret = core->ops->set_parent(core->hw, p_index);
1643 trace_clk_set_parent_complete(core, parent);
1646 flags = clk_enable_lock();
1647 clk_reparent(core, old_parent);
1648 clk_enable_unlock(flags);
1649 __clk_set_parent_after(core, old_parent, parent);
1654 __clk_set_parent_after(core, parent, old_parent);
1660 * __clk_speculate_rates
1661 * @core: first clk in the subtree
1662 * @parent_rate: the "future" rate of clk's parent
1664 * Walks the subtree of clks starting with clk, speculating rates as it
1665 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1667 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1668 * pre-rate change notifications and returns early if no clks in the
1669 * subtree have subscribed to the notifications. Note that if a clk does not
1670 * implement the .recalc_rate callback then it is assumed that the clock will
1671 * take on the rate of its parent.
1673 static int __clk_speculate_rates(struct clk_core *core,
1674 unsigned long parent_rate)
1676 struct clk_core *child;
1677 unsigned long new_rate;
1678 int ret = NOTIFY_DONE;
1680 lockdep_assert_held(&prepare_lock);
1682 new_rate = clk_recalc(core, parent_rate);
1684 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1685 if (core->notifier_count)
1686 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1688 if (ret & NOTIFY_STOP_MASK) {
1689 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1690 __func__, core->name, ret);
1694 hlist_for_each_entry(child, &core->children, child_node) {
1695 ret = __clk_speculate_rates(child, new_rate);
1696 if (ret & NOTIFY_STOP_MASK)
1704 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1705 struct clk_core *new_parent, u8 p_index)
1707 struct clk_core *child;
1709 core->new_rate = new_rate;
1710 core->new_parent = new_parent;
1711 core->new_parent_index = p_index;
1712 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1713 core->new_child = NULL;
1714 if (new_parent && new_parent != core->parent)
1715 new_parent->new_child = core;
1717 hlist_for_each_entry(child, &core->children, child_node) {
1718 child->new_rate = clk_recalc(child, new_rate);
1719 clk_calc_subtree(child, child->new_rate, NULL, 0);
1724 * calculate the new rates returning the topmost clock that has to be
1727 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1730 struct clk_core *top = core;
1731 struct clk_core *old_parent, *parent;
1732 unsigned long best_parent_rate = 0;
1733 unsigned long new_rate;
1734 unsigned long min_rate;
1735 unsigned long max_rate;
1740 if (IS_ERR_OR_NULL(core))
1743 /* save parent rate, if it exists */
1744 parent = old_parent = core->parent;
1746 best_parent_rate = parent->rate;
1748 clk_core_get_boundaries(core, &min_rate, &max_rate);
1750 /* find the closest rate and parent clk/rate */
1751 if (clk_core_can_round(core)) {
1752 struct clk_rate_request req;
1755 req.min_rate = min_rate;
1756 req.max_rate = max_rate;
1758 clk_core_init_rate_req(core, &req);
1760 ret = clk_core_determine_round_nolock(core, &req);
1764 best_parent_rate = req.best_parent_rate;
1765 new_rate = req.rate;
1766 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1768 if (new_rate < min_rate || new_rate > max_rate)
1770 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1771 /* pass-through clock without adjustable parent */
1772 core->new_rate = core->rate;
1775 /* pass-through clock with adjustable parent */
1776 top = clk_calc_new_rates(parent, rate);
1777 new_rate = parent->new_rate;
1781 /* some clocks must be gated to change parent */
1782 if (parent != old_parent &&
1783 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1784 pr_debug("%s: %s not gated but wants to reparent\n",
1785 __func__, core->name);
1789 /* try finding the new parent index */
1790 if (parent && core->num_parents > 1) {
1791 p_index = clk_fetch_parent_index(core, parent);
1793 pr_debug("%s: clk %s can not be parent of clk %s\n",
1794 __func__, parent->name, core->name);
1799 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1800 best_parent_rate != parent->rate)
1801 top = clk_calc_new_rates(parent, best_parent_rate);
1804 clk_calc_subtree(core, new_rate, parent, p_index);
1810 * Notify about rate changes in a subtree. Always walk down the whole tree
1811 * so that in case of an error we can walk down the whole tree again and
1814 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1815 unsigned long event)
1817 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1818 int ret = NOTIFY_DONE;
1820 if (core->rate == core->new_rate)
1823 if (core->notifier_count) {
1824 ret = __clk_notify(core, event, core->rate, core->new_rate);
1825 if (ret & NOTIFY_STOP_MASK)
1829 hlist_for_each_entry(child, &core->children, child_node) {
1830 /* Skip children who will be reparented to another clock */
1831 if (child->new_parent && child->new_parent != core)
1833 tmp_clk = clk_propagate_rate_change(child, event);
1838 /* handle the new child who might not be in core->children yet */
1839 if (core->new_child) {
1840 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1849 * walk down a subtree and set the new rates notifying the rate
1852 static void clk_change_rate(struct clk_core *core)
1854 struct clk_core *child;
1855 struct hlist_node *tmp;
1856 unsigned long old_rate;
1857 unsigned long best_parent_rate = 0;
1858 bool skip_set_rate = false;
1859 struct clk_core *old_parent;
1860 struct clk_core *parent = NULL;
1862 old_rate = core->rate;
1864 if (core->new_parent) {
1865 parent = core->new_parent;
1866 best_parent_rate = core->new_parent->rate;
1867 } else if (core->parent) {
1868 parent = core->parent;
1869 best_parent_rate = core->parent->rate;
1872 if (clk_pm_runtime_get(core))
1875 if (core->flags & CLK_SET_RATE_UNGATE) {
1876 unsigned long flags;
1878 clk_core_prepare(core);
1879 flags = clk_enable_lock();
1880 clk_core_enable(core);
1881 clk_enable_unlock(flags);
1884 if (core->new_parent && core->new_parent != core->parent) {
1885 old_parent = __clk_set_parent_before(core, core->new_parent);
1886 trace_clk_set_parent(core, core->new_parent);
1888 if (core->ops->set_rate_and_parent) {
1889 skip_set_rate = true;
1890 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1892 core->new_parent_index);
1893 } else if (core->ops->set_parent) {
1894 core->ops->set_parent(core->hw, core->new_parent_index);
1897 trace_clk_set_parent_complete(core, core->new_parent);
1898 __clk_set_parent_after(core, core->new_parent, old_parent);
1901 if (core->flags & CLK_OPS_PARENT_ENABLE)
1902 clk_core_prepare_enable(parent);
1904 trace_clk_set_rate(core, core->new_rate);
1906 if (!skip_set_rate && core->ops->set_rate)
1907 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1909 trace_clk_set_rate_complete(core, core->new_rate);
1911 core->rate = clk_recalc(core, best_parent_rate);
1913 if (core->flags & CLK_SET_RATE_UNGATE) {
1914 unsigned long flags;
1916 flags = clk_enable_lock();
1917 clk_core_disable(core);
1918 clk_enable_unlock(flags);
1919 clk_core_unprepare(core);
1922 if (core->flags & CLK_OPS_PARENT_ENABLE)
1923 clk_core_disable_unprepare(parent);
1925 if (core->notifier_count && old_rate != core->rate)
1926 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1928 if (core->flags & CLK_RECALC_NEW_RATES)
1929 (void)clk_calc_new_rates(core, core->new_rate);
1932 * Use safe iteration, as change_rate can actually swap parents
1933 * for certain clock types.
1935 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1936 /* Skip children who will be reparented to another clock */
1937 if (child->new_parent && child->new_parent != core)
1939 clk_change_rate(child);
1942 /* handle the new child who might not be in core->children yet */
1943 if (core->new_child)
1944 clk_change_rate(core->new_child);
1946 clk_pm_runtime_put(core);
1949 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1950 unsigned long req_rate)
1953 struct clk_rate_request req;
1955 lockdep_assert_held(&prepare_lock);
1960 /* simulate what the rate would be if it could be freely set */
1961 cnt = clk_core_rate_nuke_protect(core);
1965 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1966 req.rate = req_rate;
1968 ret = clk_core_round_rate_nolock(core, &req);
1970 /* restore the protection */
1971 clk_core_rate_restore_protect(core, cnt);
1973 return ret ? 0 : req.rate;
1976 static int clk_core_set_rate_nolock(struct clk_core *core,
1977 unsigned long req_rate)
1979 struct clk_core *top, *fail_clk;
1986 rate = clk_core_req_round_rate_nolock(core, req_rate);
1988 /* bail early if nothing to do */
1989 if (rate == clk_core_get_rate_nolock(core))
1992 /* fail on a direct rate set of a protected provider */
1993 if (clk_core_rate_is_protected(core))
1996 /* calculate new rates and get the topmost changed clock */
1997 top = clk_calc_new_rates(core, req_rate);
2001 ret = clk_pm_runtime_get(core);
2005 /* notify that we are about to change rates */
2006 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2008 pr_debug("%s: failed to set %s rate\n", __func__,
2010 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2015 /* change the rates */
2016 clk_change_rate(top);
2018 core->req_rate = req_rate;
2020 clk_pm_runtime_put(core);
2026 * clk_set_rate - specify a new rate for clk
2027 * @clk: the clk whose rate is being changed
2028 * @rate: the new rate for clk
2030 * In the simplest case clk_set_rate will only adjust the rate of clk.
2032 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2033 * propagate up to clk's parent; whether or not this happens depends on the
2034 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2035 * after calling .round_rate then upstream parent propagation is ignored. If
2036 * *parent_rate comes back with a new rate for clk's parent then we propagate
2037 * up to clk's parent and set its rate. Upward propagation will continue
2038 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2039 * .round_rate stops requesting changes to clk's parent_rate.
2041 * Rate changes are accomplished via tree traversal that also recalculates the
2042 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2044 * Returns 0 on success, -EERROR otherwise.
2046 int clk_set_rate(struct clk *clk, unsigned long rate)
2053 /* prevent racing with updates to the clock topology */
2056 if (clk->exclusive_count)
2057 clk_core_rate_unprotect(clk->core);
2059 ret = clk_core_set_rate_nolock(clk->core, rate);
2061 if (clk->exclusive_count)
2062 clk_core_rate_protect(clk->core);
2064 clk_prepare_unlock();
2068 EXPORT_SYMBOL_GPL(clk_set_rate);
2071 * clk_set_rate_exclusive - specify a new rate get exclusive control
2072 * @clk: the clk whose rate is being changed
2073 * @rate: the new rate for clk
2075 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2076 * within a critical section
2078 * This can be used initially to ensure that at least 1 consumer is
2079 * statisfied when several consumers are competing for exclusivity over the
2080 * same clock provider.
2082 * The exclusivity is not applied if setting the rate failed.
2084 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2085 * clk_rate_exclusive_put().
2087 * Returns 0 on success, -EERROR otherwise.
2089 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2096 /* prevent racing with updates to the clock topology */
2100 * The temporary protection removal is not here, on purpose
2101 * This function is meant to be used instead of clk_rate_protect,
2102 * so before the consumer code path protect the clock provider
2105 ret = clk_core_set_rate_nolock(clk->core, rate);
2107 clk_core_rate_protect(clk->core);
2108 clk->exclusive_count++;
2111 clk_prepare_unlock();
2115 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2118 * clk_set_rate_range - set a rate range for a clock source
2119 * @clk: clock source
2120 * @min: desired minimum clock rate in Hz, inclusive
2121 * @max: desired maximum clock rate in Hz, inclusive
2123 * Returns success (0) or negative errno.
2125 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2128 unsigned long old_min, old_max, rate;
2134 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2135 __func__, clk->core->name, clk->dev_id, clk->con_id,
2142 if (clk->exclusive_count)
2143 clk_core_rate_unprotect(clk->core);
2145 /* Save the current values in case we need to rollback the change */
2146 old_min = clk->min_rate;
2147 old_max = clk->max_rate;
2148 clk->min_rate = min;
2149 clk->max_rate = max;
2151 rate = clk_core_get_rate_nolock(clk->core);
2152 if (rate < min || rate > max) {
2155 * We are in bit of trouble here, current rate is outside the
2156 * the requested range. We are going try to request appropriate
2157 * range boundary but there is a catch. It may fail for the
2158 * usual reason (clock broken, clock protected, etc) but also
2160 * - round_rate() was not favorable and fell on the wrong
2161 * side of the boundary
2162 * - the determine_rate() callback does not really check for
2163 * this corner case when determining the rate
2171 ret = clk_core_set_rate_nolock(clk->core, rate);
2173 /* rollback the changes */
2174 clk->min_rate = old_min;
2175 clk->max_rate = old_max;
2179 if (clk->exclusive_count)
2180 clk_core_rate_protect(clk->core);
2182 clk_prepare_unlock();
2186 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2189 * clk_set_min_rate - set a minimum clock rate for a clock source
2190 * @clk: clock source
2191 * @rate: desired minimum clock rate in Hz, inclusive
2193 * Returns success (0) or negative errno.
2195 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2200 return clk_set_rate_range(clk, rate, clk->max_rate);
2202 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2205 * clk_set_max_rate - set a maximum clock rate for a clock source
2206 * @clk: clock source
2207 * @rate: desired maximum clock rate in Hz, inclusive
2209 * Returns success (0) or negative errno.
2211 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2216 return clk_set_rate_range(clk, clk->min_rate, rate);
2218 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2221 * clk_get_parent - return the parent of a clk
2222 * @clk: the clk whose parent gets returned
2224 * Simply returns clk->parent. Returns NULL if clk is NULL.
2226 struct clk *clk_get_parent(struct clk *clk)
2234 /* TODO: Create a per-user clk and change callers to call clk_put */
2235 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2236 clk_prepare_unlock();
2240 EXPORT_SYMBOL_GPL(clk_get_parent);
2242 static struct clk_core *__clk_init_parent(struct clk_core *core)
2246 if (core->num_parents > 1 && core->ops->get_parent)
2247 index = core->ops->get_parent(core->hw);
2249 return clk_core_get_parent_by_index(core, index);
2252 static void clk_core_reparent(struct clk_core *core,
2253 struct clk_core *new_parent)
2255 clk_reparent(core, new_parent);
2256 __clk_recalc_accuracies(core);
2257 __clk_recalc_rates(core, POST_RATE_CHANGE);
2260 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2265 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2269 * clk_has_parent - check if a clock is a possible parent for another
2270 * @clk: clock source
2271 * @parent: parent clock source
2273 * This function can be used in drivers that need to check that a clock can be
2274 * the parent of another without actually changing the parent.
2276 * Returns true if @parent is a possible parent for @clk, false otherwise.
2278 bool clk_has_parent(struct clk *clk, struct clk *parent)
2280 struct clk_core *core, *parent_core;
2282 /* NULL clocks should be nops, so return success if either is NULL. */
2283 if (!clk || !parent)
2287 parent_core = parent->core;
2289 /* Optimize for the case where the parent is already the parent. */
2290 if (core->parent == parent_core)
2293 return match_string(core->parent_names, core->num_parents,
2294 parent_core->name) >= 0;
2296 EXPORT_SYMBOL_GPL(clk_has_parent);
2298 static int clk_core_set_parent_nolock(struct clk_core *core,
2299 struct clk_core *parent)
2303 unsigned long p_rate = 0;
2305 lockdep_assert_held(&prepare_lock);
2310 if (core->parent == parent)
2313 /* verify ops for for multi-parent clks */
2314 if (core->num_parents > 1 && !core->ops->set_parent)
2317 /* check that we are allowed to re-parent if the clock is in use */
2318 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2321 if (clk_core_rate_is_protected(core))
2324 /* try finding the new parent index */
2326 p_index = clk_fetch_parent_index(core, parent);
2328 pr_debug("%s: clk %s can not be parent of clk %s\n",
2329 __func__, parent->name, core->name);
2332 p_rate = parent->rate;
2335 ret = clk_pm_runtime_get(core);
2339 /* propagate PRE_RATE_CHANGE notifications */
2340 ret = __clk_speculate_rates(core, p_rate);
2342 /* abort if a driver objects */
2343 if (ret & NOTIFY_STOP_MASK)
2346 /* do the re-parent */
2347 ret = __clk_set_parent(core, parent, p_index);
2349 /* propagate rate an accuracy recalculation accordingly */
2351 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2353 __clk_recalc_rates(core, POST_RATE_CHANGE);
2354 __clk_recalc_accuracies(core);
2358 clk_pm_runtime_put(core);
2364 * clk_set_parent - switch the parent of a mux clk
2365 * @clk: the mux clk whose input we are switching
2366 * @parent: the new input to clk
2368 * Re-parent clk to use parent as its new input source. If clk is in
2369 * prepared state, the clk will get enabled for the duration of this call. If
2370 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2371 * that, the reparenting is glitchy in hardware, etc), use the
2372 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2374 * After successfully changing clk's parent clk_set_parent will update the
2375 * clk topology, sysfs topology and propagate rate recalculation via
2376 * __clk_recalc_rates.
2378 * Returns 0 on success, -EERROR otherwise.
2380 int clk_set_parent(struct clk *clk, struct clk *parent)
2389 if (clk->exclusive_count)
2390 clk_core_rate_unprotect(clk->core);
2392 ret = clk_core_set_parent_nolock(clk->core,
2393 parent ? parent->core : NULL);
2395 if (clk->exclusive_count)
2396 clk_core_rate_protect(clk->core);
2398 clk_prepare_unlock();
2402 EXPORT_SYMBOL_GPL(clk_set_parent);
2404 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2408 lockdep_assert_held(&prepare_lock);
2413 if (clk_core_rate_is_protected(core))
2416 trace_clk_set_phase(core, degrees);
2418 if (core->ops->set_phase) {
2419 ret = core->ops->set_phase(core->hw, degrees);
2421 core->phase = degrees;
2424 trace_clk_set_phase_complete(core, degrees);
2430 * clk_set_phase - adjust the phase shift of a clock signal
2431 * @clk: clock signal source
2432 * @degrees: number of degrees the signal is shifted
2434 * Shifts the phase of a clock signal by the specified
2435 * degrees. Returns 0 on success, -EERROR otherwise.
2437 * This function makes no distinction about the input or reference
2438 * signal that we adjust the clock signal phase against. For example
2439 * phase locked-loop clock signal generators we may shift phase with
2440 * respect to feedback clock signal input, but for other cases the
2441 * clock phase may be shifted with respect to some other, unspecified
2444 * Additionally the concept of phase shift does not propagate through
2445 * the clock tree hierarchy, which sets it apart from clock rates and
2446 * clock accuracy. A parent clock phase attribute does not have an
2447 * impact on the phase attribute of a child clock.
2449 int clk_set_phase(struct clk *clk, int degrees)
2456 /* sanity check degrees */
2463 if (clk->exclusive_count)
2464 clk_core_rate_unprotect(clk->core);
2466 ret = clk_core_set_phase_nolock(clk->core, degrees);
2468 if (clk->exclusive_count)
2469 clk_core_rate_protect(clk->core);
2471 clk_prepare_unlock();
2475 EXPORT_SYMBOL_GPL(clk_set_phase);
2477 static int clk_core_get_phase(struct clk_core *core)
2482 /* Always try to update cached phase if possible */
2483 if (core->ops->get_phase)
2484 core->phase = core->ops->get_phase(core->hw);
2486 clk_prepare_unlock();
2492 * clk_get_phase - return the phase shift of a clock signal
2493 * @clk: clock signal source
2495 * Returns the phase shift of a clock node in degrees, otherwise returns
2498 int clk_get_phase(struct clk *clk)
2503 return clk_core_get_phase(clk->core);
2505 EXPORT_SYMBOL_GPL(clk_get_phase);
2507 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2509 /* Assume a default value of 50% */
2514 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2516 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2518 struct clk_duty *duty = &core->duty;
2521 if (!core->ops->get_duty_cycle)
2522 return clk_core_update_duty_cycle_parent_nolock(core);
2524 ret = core->ops->get_duty_cycle(core->hw, duty);
2528 /* Don't trust the clock provider too much */
2529 if (duty->den == 0 || duty->num > duty->den) {
2537 clk_core_reset_duty_cycle_nolock(core);
2541 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2546 core->flags & CLK_DUTY_CYCLE_PARENT) {
2547 ret = clk_core_update_duty_cycle_nolock(core->parent);
2548 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2550 clk_core_reset_duty_cycle_nolock(core);
2556 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2557 struct clk_duty *duty);
2559 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2560 struct clk_duty *duty)
2564 lockdep_assert_held(&prepare_lock);
2566 if (clk_core_rate_is_protected(core))
2569 trace_clk_set_duty_cycle(core, duty);
2571 if (!core->ops->set_duty_cycle)
2572 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2574 ret = core->ops->set_duty_cycle(core->hw, duty);
2576 memcpy(&core->duty, duty, sizeof(*duty));
2578 trace_clk_set_duty_cycle_complete(core, duty);
2583 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2584 struct clk_duty *duty)
2589 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2590 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2591 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2598 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2599 * @clk: clock signal source
2600 * @num: numerator of the duty cycle ratio to be applied
2601 * @den: denominator of the duty cycle ratio to be applied
2603 * Apply the duty cycle ratio if the ratio is valid and the clock can
2604 * perform this operation
2606 * Returns (0) on success, a negative errno otherwise.
2608 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2611 struct clk_duty duty;
2616 /* sanity check the ratio */
2617 if (den == 0 || num > den)
2625 if (clk->exclusive_count)
2626 clk_core_rate_unprotect(clk->core);
2628 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2630 if (clk->exclusive_count)
2631 clk_core_rate_protect(clk->core);
2633 clk_prepare_unlock();
2637 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2639 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2642 struct clk_duty *duty = &core->duty;
2647 ret = clk_core_update_duty_cycle_nolock(core);
2649 ret = mult_frac(scale, duty->num, duty->den);
2651 clk_prepare_unlock();
2657 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2658 * @clk: clock signal source
2659 * @scale: scaling factor to be applied to represent the ratio as an integer
2661 * Returns the duty cycle ratio of a clock node multiplied by the provided
2662 * scaling factor, or negative errno on error.
2664 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2669 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2671 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2674 * clk_is_match - check if two clk's point to the same hardware clock
2675 * @p: clk compared against q
2676 * @q: clk compared against p
2678 * Returns true if the two struct clk pointers both point to the same hardware
2679 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2680 * share the same struct clk_core object.
2682 * Returns false otherwise. Note that two NULL clks are treated as matching.
2684 bool clk_is_match(const struct clk *p, const struct clk *q)
2686 /* trivial case: identical struct clk's or both NULL */
2690 /* true if clk->core pointers match. Avoid dereferencing garbage */
2691 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2692 if (p->core == q->core)
2697 EXPORT_SYMBOL_GPL(clk_is_match);
2699 /*** debugfs support ***/
2701 #ifdef CONFIG_DEBUG_FS
2702 #include <linux/debugfs.h>
2704 static struct dentry *rootdir;
2705 static int inited = 0;
2706 static DEFINE_MUTEX(clk_debug_lock);
2707 static HLIST_HEAD(clk_debug_list);
2709 static struct hlist_head *all_lists[] = {
2715 static struct hlist_head *orphan_list[] = {
2720 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2726 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
2728 30 - level * 3, c->name,
2729 c->enable_count, c->prepare_count, c->protect_count,
2730 clk_core_get_rate(c), clk_core_get_accuracy(c),
2731 clk_core_get_phase(c),
2732 clk_core_get_scaled_duty_cycle(c, 100000));
2735 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2738 struct clk_core *child;
2743 clk_summary_show_one(s, c, level);
2745 hlist_for_each_entry(child, &c->children, child_node)
2746 clk_summary_show_subtree(s, child, level + 1);
2749 static int clk_summary_show(struct seq_file *s, void *data)
2752 struct hlist_head **lists = (struct hlist_head **)s->private;
2754 seq_puts(s, " enable prepare protect duty\n");
2755 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2756 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
2760 for (; *lists; lists++)
2761 hlist_for_each_entry(c, *lists, child_node)
2762 clk_summary_show_subtree(s, c, 0);
2764 clk_prepare_unlock();
2768 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2770 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2775 /* This should be JSON format, i.e. elements separated with a comma */
2776 seq_printf(s, "\"%s\": { ", c->name);
2777 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2778 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2779 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2780 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2781 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2782 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2783 seq_printf(s, "\"duty_cycle\": %u",
2784 clk_core_get_scaled_duty_cycle(c, 100000));
2787 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2789 struct clk_core *child;
2794 clk_dump_one(s, c, level);
2796 hlist_for_each_entry(child, &c->children, child_node) {
2798 clk_dump_subtree(s, child, level + 1);
2804 static int clk_dump_show(struct seq_file *s, void *data)
2807 bool first_node = true;
2808 struct hlist_head **lists = (struct hlist_head **)s->private;
2813 for (; *lists; lists++) {
2814 hlist_for_each_entry(c, *lists, child_node) {
2818 clk_dump_subtree(s, c, 0);
2822 clk_prepare_unlock();
2827 DEFINE_SHOW_ATTRIBUTE(clk_dump);
2829 static const struct {
2833 #define ENTRY(f) { f, #f }
2834 ENTRY(CLK_SET_RATE_GATE),
2835 ENTRY(CLK_SET_PARENT_GATE),
2836 ENTRY(CLK_SET_RATE_PARENT),
2837 ENTRY(CLK_IGNORE_UNUSED),
2838 ENTRY(CLK_IS_BASIC),
2839 ENTRY(CLK_GET_RATE_NOCACHE),
2840 ENTRY(CLK_SET_RATE_NO_REPARENT),
2841 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2842 ENTRY(CLK_RECALC_NEW_RATES),
2843 ENTRY(CLK_SET_RATE_UNGATE),
2844 ENTRY(CLK_IS_CRITICAL),
2845 ENTRY(CLK_OPS_PARENT_ENABLE),
2846 ENTRY(CLK_DUTY_CYCLE_PARENT),
2850 static int clk_flags_show(struct seq_file *s, void *data)
2852 struct clk_core *core = s->private;
2853 unsigned long flags = core->flags;
2856 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2857 if (flags & clk_flags[i].flag) {
2858 seq_printf(s, "%s\n", clk_flags[i].name);
2859 flags &= ~clk_flags[i].flag;
2864 seq_printf(s, "0x%lx\n", flags);
2869 DEFINE_SHOW_ATTRIBUTE(clk_flags);
2871 static int possible_parents_show(struct seq_file *s, void *data)
2873 struct clk_core *core = s->private;
2876 for (i = 0; i < core->num_parents - 1; i++)
2877 seq_printf(s, "%s ", core->parent_names[i]);
2879 seq_printf(s, "%s\n", core->parent_names[i]);
2883 DEFINE_SHOW_ATTRIBUTE(possible_parents);
2885 static int clk_duty_cycle_show(struct seq_file *s, void *data)
2887 struct clk_core *core = s->private;
2888 struct clk_duty *duty = &core->duty;
2890 seq_printf(s, "%u/%u\n", duty->num, duty->den);
2894 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
2896 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2898 struct dentry *root;
2900 if (!core || !pdentry)
2903 root = debugfs_create_dir(core->name, pdentry);
2904 core->dentry = root;
2906 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2907 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2908 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2909 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2910 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2911 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2912 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2913 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
2914 debugfs_create_file("clk_duty_cycle", 0444, root, core,
2915 &clk_duty_cycle_fops);
2917 if (core->num_parents > 1)
2918 debugfs_create_file("clk_possible_parents", 0444, root, core,
2919 &possible_parents_fops);
2921 if (core->ops->debug_init)
2922 core->ops->debug_init(core->hw, core->dentry);
2926 * clk_debug_register - add a clk node to the debugfs clk directory
2927 * @core: the clk being added to the debugfs clk directory
2929 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2930 * initialized. Otherwise it bails out early since the debugfs clk directory
2931 * will be created lazily by clk_debug_init as part of a late_initcall.
2933 static void clk_debug_register(struct clk_core *core)
2935 mutex_lock(&clk_debug_lock);
2936 hlist_add_head(&core->debug_node, &clk_debug_list);
2938 clk_debug_create_one(core, rootdir);
2939 mutex_unlock(&clk_debug_lock);
2943 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2944 * @core: the clk being removed from the debugfs clk directory
2946 * Dynamically removes a clk and all its child nodes from the
2947 * debugfs clk directory if clk->dentry points to debugfs created by
2948 * clk_debug_register in __clk_core_init.
2950 static void clk_debug_unregister(struct clk_core *core)
2952 mutex_lock(&clk_debug_lock);
2953 hlist_del_init(&core->debug_node);
2954 debugfs_remove_recursive(core->dentry);
2955 core->dentry = NULL;
2956 mutex_unlock(&clk_debug_lock);
2960 * clk_debug_init - lazily populate the debugfs clk directory
2962 * clks are often initialized very early during boot before memory can be
2963 * dynamically allocated and well before debugfs is setup. This function
2964 * populates the debugfs clk directory once at boot-time when we know that
2965 * debugfs is setup. It should only be called once at boot-time, all other clks
2966 * added dynamically will be done so with clk_debug_register.
2968 static int __init clk_debug_init(void)
2970 struct clk_core *core;
2972 rootdir = debugfs_create_dir("clk", NULL);
2974 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2976 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2978 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2980 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2983 mutex_lock(&clk_debug_lock);
2984 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2985 clk_debug_create_one(core, rootdir);
2988 mutex_unlock(&clk_debug_lock);
2992 late_initcall(clk_debug_init);
2994 static inline void clk_debug_register(struct clk_core *core) { }
2995 static inline void clk_debug_reparent(struct clk_core *core,
2996 struct clk_core *new_parent)
2999 static inline void clk_debug_unregister(struct clk_core *core)
3005 * __clk_core_init - initialize the data structures in a struct clk_core
3006 * @core: clk_core being initialized
3008 * Initializes the lists in struct clk_core, queries the hardware for the
3009 * parent and rate and sets them both.
3011 static int __clk_core_init(struct clk_core *core)
3014 struct clk_core *orphan;
3015 struct hlist_node *tmp2;
3023 ret = clk_pm_runtime_get(core);
3027 /* check to see if a clock with this name is already registered */
3028 if (clk_core_lookup(core->name)) {
3029 pr_debug("%s: clk %s already initialized\n",
3030 __func__, core->name);
3035 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3036 if (core->ops->set_rate &&
3037 !((core->ops->round_rate || core->ops->determine_rate) &&
3038 core->ops->recalc_rate)) {
3039 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3040 __func__, core->name);
3045 if (core->ops->set_parent && !core->ops->get_parent) {
3046 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3047 __func__, core->name);
3052 if (core->num_parents > 1 && !core->ops->get_parent) {
3053 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3054 __func__, core->name);
3059 if (core->ops->set_rate_and_parent &&
3060 !(core->ops->set_parent && core->ops->set_rate)) {
3061 pr_err("%s: %s must implement .set_parent & .set_rate\n",
3062 __func__, core->name);
3067 /* throw a WARN if any entries in parent_names are NULL */
3068 for (i = 0; i < core->num_parents; i++)
3069 WARN(!core->parent_names[i],
3070 "%s: invalid NULL in %s's .parent_names\n",
3071 __func__, core->name);
3073 core->parent = __clk_init_parent(core);
3076 * Populate core->parent if parent has already been clk_core_init'd. If
3077 * parent has not yet been clk_core_init'd then place clk in the orphan
3078 * list. If clk doesn't have any parents then place it in the root
3081 * Every time a new clk is clk_init'd then we walk the list of orphan
3082 * clocks and re-parent any that are children of the clock currently
3086 hlist_add_head(&core->child_node,
3087 &core->parent->children);
3088 core->orphan = core->parent->orphan;
3089 } else if (!core->num_parents) {
3090 hlist_add_head(&core->child_node, &clk_root_list);
3091 core->orphan = false;
3093 hlist_add_head(&core->child_node, &clk_orphan_list);
3094 core->orphan = true;
3098 * optional platform-specific magic
3100 * The .init callback is not used by any of the basic clock types, but
3101 * exists for weird hardware that must perform initialization magic.
3102 * Please consider other ways of solving initialization problems before
3103 * using this callback, as its use is discouraged.
3105 if (core->ops->init)
3106 core->ops->init(core->hw);
3109 * Set clk's accuracy. The preferred method is to use
3110 * .recalc_accuracy. For simple clocks and lazy developers the default
3111 * fallback is to use the parent's accuracy. If a clock doesn't have a
3112 * parent (or is orphaned) then accuracy is set to zero (perfect
3115 if (core->ops->recalc_accuracy)
3116 core->accuracy = core->ops->recalc_accuracy(core->hw,
3117 __clk_get_accuracy(core->parent));
3118 else if (core->parent)
3119 core->accuracy = core->parent->accuracy;
3125 * Since a phase is by definition relative to its parent, just
3126 * query the current clock phase, or just assume it's in phase.
3128 if (core->ops->get_phase)
3129 core->phase = core->ops->get_phase(core->hw);
3134 * Set clk's duty cycle.
3136 clk_core_update_duty_cycle_nolock(core);
3139 * Set clk's rate. The preferred method is to use .recalc_rate. For
3140 * simple clocks and lazy developers the default fallback is to use the
3141 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3142 * then rate is set to zero.
3144 if (core->ops->recalc_rate)
3145 rate = core->ops->recalc_rate(core->hw,
3146 clk_core_get_rate_nolock(core->parent));
3147 else if (core->parent)
3148 rate = core->parent->rate;
3151 core->rate = core->req_rate = rate;
3154 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3155 * don't get accidentally disabled when walking the orphan tree and
3156 * reparenting clocks
3158 if (core->flags & CLK_IS_CRITICAL) {
3159 unsigned long flags;
3161 clk_core_prepare(core);
3163 flags = clk_enable_lock();
3164 clk_core_enable(core);
3165 clk_enable_unlock(flags);
3169 * walk the list of orphan clocks and reparent any that newly finds a
3172 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3173 struct clk_core *parent = __clk_init_parent(orphan);
3176 * We need to use __clk_set_parent_before() and _after() to
3177 * to properly migrate any prepare/enable count of the orphan
3178 * clock. This is important for CLK_IS_CRITICAL clocks, which
3179 * are enabled during init but might not have a parent yet.
3182 /* update the clk tree topology */
3183 __clk_set_parent_before(orphan, parent);
3184 __clk_set_parent_after(orphan, parent, NULL);
3185 __clk_recalc_accuracies(orphan);
3186 __clk_recalc_rates(orphan, 0);
3190 kref_init(&core->ref);
3192 clk_pm_runtime_put(core);
3194 clk_prepare_unlock();
3197 clk_debug_register(core);
3202 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3207 /* This is to allow this function to be chained to others */
3208 if (IS_ERR_OR_NULL(hw))
3209 return ERR_CAST(hw);
3211 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3213 return ERR_PTR(-ENOMEM);
3215 clk->core = hw->core;
3216 clk->dev_id = dev_id;
3217 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3218 clk->max_rate = ULONG_MAX;
3221 hlist_add_head(&clk->clks_node, &hw->core->clks);
3222 clk_prepare_unlock();
3227 /* keep in sync with __clk_put */
3228 void __clk_free_clk(struct clk *clk)
3231 hlist_del(&clk->clks_node);
3232 clk_prepare_unlock();
3234 kfree_const(clk->con_id);
3239 * clk_register - allocate a new clock, register it and return an opaque cookie
3240 * @dev: device that is registering this clock
3241 * @hw: link to hardware-specific clock data
3243 * clk_register is the primary interface for populating the clock tree with new
3244 * clock nodes. It returns a pointer to the newly allocated struct clk which
3245 * cannot be dereferenced by driver code but may be used in conjunction with the
3246 * rest of the clock API. In the event of an error clk_register will return an
3247 * error code; drivers must test for an error code after calling clk_register.
3249 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3252 struct clk_core *core;
3254 core = kzalloc(sizeof(*core), GFP_KERNEL);
3260 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3266 if (WARN_ON(!hw->init->ops)) {
3270 core->ops = hw->init->ops;
3272 if (dev && pm_runtime_enabled(dev))
3274 if (dev && dev->driver)
3275 core->owner = dev->driver->owner;
3277 core->flags = hw->init->flags;
3278 core->num_parents = hw->init->num_parents;
3280 core->max_rate = ULONG_MAX;
3283 /* allocate local copy in case parent_names is __initdata */
3284 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
3287 if (!core->parent_names) {
3289 goto fail_parent_names;
3293 /* copy each string name in case parent_names is __initdata */
3294 for (i = 0; i < core->num_parents; i++) {
3295 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
3297 if (!core->parent_names[i]) {
3299 goto fail_parent_names_copy;
3303 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3304 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3306 if (!core->parents) {
3311 INIT_HLIST_HEAD(&core->clks);
3313 hw->clk = __clk_create_clk(hw, NULL, NULL);
3314 if (IS_ERR(hw->clk)) {
3315 ret = PTR_ERR(hw->clk);
3319 ret = __clk_core_init(core);
3323 __clk_free_clk(hw->clk);
3327 kfree(core->parents);
3328 fail_parent_names_copy:
3330 kfree_const(core->parent_names[i]);
3331 kfree(core->parent_names);
3334 kfree_const(core->name);
3338 return ERR_PTR(ret);
3340 EXPORT_SYMBOL_GPL(clk_register);
3343 * clk_hw_register - register a clk_hw and return an error code
3344 * @dev: device that is registering this clock
3345 * @hw: link to hardware-specific clock data
3347 * clk_hw_register is the primary interface for populating the clock tree with
3348 * new clock nodes. It returns an integer equal to zero indicating success or
3349 * less than zero indicating failure. Drivers must test for an error code after
3350 * calling clk_hw_register().
3352 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3354 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3356 EXPORT_SYMBOL_GPL(clk_hw_register);
3358 /* Free memory allocated for a clock. */
3359 static void __clk_release(struct kref *ref)
3361 struct clk_core *core = container_of(ref, struct clk_core, ref);
3362 int i = core->num_parents;
3364 lockdep_assert_held(&prepare_lock);
3366 kfree(core->parents);
3368 kfree_const(core->parent_names[i]);
3370 kfree(core->parent_names);
3371 kfree_const(core->name);
3376 * Empty clk_ops for unregistered clocks. These are used temporarily
3377 * after clk_unregister() was called on a clock and until last clock
3378 * consumer calls clk_put() and the struct clk object is freed.
3380 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3385 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3390 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3391 unsigned long parent_rate)
3396 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3401 static const struct clk_ops clk_nodrv_ops = {
3402 .enable = clk_nodrv_prepare_enable,
3403 .disable = clk_nodrv_disable_unprepare,
3404 .prepare = clk_nodrv_prepare_enable,
3405 .unprepare = clk_nodrv_disable_unprepare,
3406 .set_rate = clk_nodrv_set_rate,
3407 .set_parent = clk_nodrv_set_parent,
3411 * clk_unregister - unregister a currently registered clock
3412 * @clk: clock to unregister
3414 void clk_unregister(struct clk *clk)
3416 unsigned long flags;
3418 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3421 clk_debug_unregister(clk->core);
3425 if (clk->core->ops == &clk_nodrv_ops) {
3426 pr_err("%s: unregistered clock: %s\n", __func__,
3431 * Assign empty clock ops for consumers that might still hold
3432 * a reference to this clock.
3434 flags = clk_enable_lock();
3435 clk->core->ops = &clk_nodrv_ops;
3436 clk_enable_unlock(flags);
3438 if (!hlist_empty(&clk->core->children)) {
3439 struct clk_core *child;
3440 struct hlist_node *t;
3442 /* Reparent all children to the orphan list. */
3443 hlist_for_each_entry_safe(child, t, &clk->core->children,
3445 clk_core_set_parent_nolock(child, NULL);
3448 hlist_del_init(&clk->core->child_node);
3450 if (clk->core->prepare_count)
3451 pr_warn("%s: unregistering prepared clock: %s\n",
3452 __func__, clk->core->name);
3454 if (clk->core->protect_count)
3455 pr_warn("%s: unregistering protected clock: %s\n",
3456 __func__, clk->core->name);
3458 kref_put(&clk->core->ref, __clk_release);
3460 clk_prepare_unlock();
3462 EXPORT_SYMBOL_GPL(clk_unregister);
3465 * clk_hw_unregister - unregister a currently registered clk_hw
3466 * @hw: hardware-specific clock data to unregister
3468 void clk_hw_unregister(struct clk_hw *hw)
3470 clk_unregister(hw->clk);
3472 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3474 static void devm_clk_release(struct device *dev, void *res)
3476 clk_unregister(*(struct clk **)res);
3479 static void devm_clk_hw_release(struct device *dev, void *res)
3481 clk_hw_unregister(*(struct clk_hw **)res);
3485 * devm_clk_register - resource managed clk_register()
3486 * @dev: device that is registering this clock
3487 * @hw: link to hardware-specific clock data
3489 * Managed clk_register(). Clocks returned from this function are
3490 * automatically clk_unregister()ed on driver detach. See clk_register() for
3493 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3498 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3500 return ERR_PTR(-ENOMEM);
3502 clk = clk_register(dev, hw);
3505 devres_add(dev, clkp);
3512 EXPORT_SYMBOL_GPL(devm_clk_register);
3515 * devm_clk_hw_register - resource managed clk_hw_register()
3516 * @dev: device that is registering this clock
3517 * @hw: link to hardware-specific clock data
3519 * Managed clk_hw_register(). Clocks registered by this function are
3520 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3521 * for more information.
3523 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3525 struct clk_hw **hwp;
3528 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3532 ret = clk_hw_register(dev, hw);
3535 devres_add(dev, hwp);
3542 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3544 static int devm_clk_match(struct device *dev, void *res, void *data)
3546 struct clk *c = res;
3552 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3554 struct clk_hw *hw = res;
3562 * devm_clk_unregister - resource managed clk_unregister()
3563 * @clk: clock to unregister
3565 * Deallocate a clock allocated with devm_clk_register(). Normally
3566 * this function will not need to be called and the resource management
3567 * code will ensure that the resource is freed.
3569 void devm_clk_unregister(struct device *dev, struct clk *clk)
3571 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3573 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3576 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3577 * @dev: device that is unregistering the hardware-specific clock data
3578 * @hw: link to hardware-specific clock data
3580 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3581 * this function will not need to be called and the resource management
3582 * code will ensure that the resource is freed.
3584 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3586 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3589 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3594 int __clk_get(struct clk *clk)
3596 struct clk_core *core = !clk ? NULL : clk->core;
3599 if (!try_module_get(core->owner))
3602 kref_get(&core->ref);
3607 /* keep in sync with __clk_free_clk */
3608 void __clk_put(struct clk *clk)
3610 struct module *owner;
3612 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3618 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3619 * given user should be balanced with calls to clk_rate_exclusive_put()
3620 * and by that same consumer
3622 if (WARN_ON(clk->exclusive_count)) {
3623 /* We voiced our concern, let's sanitize the situation */
3624 clk->core->protect_count -= (clk->exclusive_count - 1);
3625 clk_core_rate_unprotect(clk->core);
3626 clk->exclusive_count = 0;
3629 hlist_del(&clk->clks_node);
3630 if (clk->min_rate > clk->core->req_rate ||
3631 clk->max_rate < clk->core->req_rate)
3632 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3634 owner = clk->core->owner;
3635 kref_put(&clk->core->ref, __clk_release);
3637 clk_prepare_unlock();
3641 kfree_const(clk->con_id);
3645 /*** clk rate change notifiers ***/
3648 * clk_notifier_register - add a clk rate change notifier
3649 * @clk: struct clk * to watch
3650 * @nb: struct notifier_block * with callback info
3652 * Request notification when clk's rate changes. This uses an SRCU
3653 * notifier because we want it to block and notifier unregistrations are
3654 * uncommon. The callbacks associated with the notifier must not
3655 * re-enter into the clk framework by calling any top-level clk APIs;
3656 * this will cause a nested prepare_lock mutex.
3658 * In all notification cases (pre, post and abort rate change) the original
3659 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3660 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3662 * clk_notifier_register() must be called from non-atomic context.
3663 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3664 * allocation failure; otherwise, passes along the return value of
3665 * srcu_notifier_chain_register().
3667 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3669 struct clk_notifier *cn;
3677 /* search the list of notifiers for this clk */
3678 list_for_each_entry(cn, &clk_notifier_list, node)
3682 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3683 if (cn->clk != clk) {
3684 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3689 srcu_init_notifier_head(&cn->notifier_head);
3691 list_add(&cn->node, &clk_notifier_list);
3694 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3696 clk->core->notifier_count++;
3699 clk_prepare_unlock();
3703 EXPORT_SYMBOL_GPL(clk_notifier_register);
3706 * clk_notifier_unregister - remove a clk rate change notifier
3707 * @clk: struct clk *
3708 * @nb: struct notifier_block * with callback info
3710 * Request no further notification for changes to 'clk' and frees memory
3711 * allocated in clk_notifier_register.
3713 * Returns -EINVAL if called with null arguments; otherwise, passes
3714 * along the return value of srcu_notifier_chain_unregister().
3716 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3718 struct clk_notifier *cn = NULL;
3726 list_for_each_entry(cn, &clk_notifier_list, node)
3730 if (cn->clk == clk) {
3731 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3733 clk->core->notifier_count--;
3735 /* XXX the notifier code should handle this better */
3736 if (!cn->notifier_head.head) {
3737 srcu_cleanup_notifier_head(&cn->notifier_head);
3738 list_del(&cn->node);
3746 clk_prepare_unlock();
3750 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3754 * struct of_clk_provider - Clock provider registration structure
3755 * @link: Entry in global list of clock providers
3756 * @node: Pointer to device tree node of clock provider
3757 * @get: Get clock callback. Returns NULL or a struct clk for the
3758 * given clock specifier
3759 * @data: context pointer to be passed into @get callback
3761 struct of_clk_provider {
3762 struct list_head link;
3764 struct device_node *node;
3765 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3766 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3770 static const struct of_device_id __clk_of_table_sentinel
3771 __used __section(__clk_of_table_end);
3773 static LIST_HEAD(of_clk_providers);
3774 static DEFINE_MUTEX(of_clk_mutex);
3776 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3781 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3783 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3787 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3789 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3791 struct clk_onecell_data *clk_data = data;
3792 unsigned int idx = clkspec->args[0];
3794 if (idx >= clk_data->clk_num) {
3795 pr_err("%s: invalid clock index %u\n", __func__, idx);
3796 return ERR_PTR(-EINVAL);
3799 return clk_data->clks[idx];
3801 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3804 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3806 struct clk_hw_onecell_data *hw_data = data;
3807 unsigned int idx = clkspec->args[0];
3809 if (idx >= hw_data->num) {
3810 pr_err("%s: invalid index %u\n", __func__, idx);
3811 return ERR_PTR(-EINVAL);
3814 return hw_data->hws[idx];
3816 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3819 * of_clk_add_provider() - Register a clock provider for a node
3820 * @np: Device node pointer associated with clock provider
3821 * @clk_src_get: callback for decoding clock
3822 * @data: context pointer for @clk_src_get callback.
3824 int of_clk_add_provider(struct device_node *np,
3825 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3829 struct of_clk_provider *cp;
3832 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3836 cp->node = of_node_get(np);
3838 cp->get = clk_src_get;
3840 mutex_lock(&of_clk_mutex);
3841 list_add(&cp->link, &of_clk_providers);
3842 mutex_unlock(&of_clk_mutex);
3843 pr_debug("Added clock from %pOF\n", np);
3845 ret = of_clk_set_defaults(np, true);
3847 of_clk_del_provider(np);
3851 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3854 * of_clk_add_hw_provider() - Register a clock provider for a node
3855 * @np: Device node pointer associated with clock provider
3856 * @get: callback for decoding clk_hw
3857 * @data: context pointer for @get callback.
3859 int of_clk_add_hw_provider(struct device_node *np,
3860 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3864 struct of_clk_provider *cp;
3867 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3871 cp->node = of_node_get(np);
3875 mutex_lock(&of_clk_mutex);
3876 list_add(&cp->link, &of_clk_providers);
3877 mutex_unlock(&of_clk_mutex);
3878 pr_debug("Added clk_hw provider from %pOF\n", np);
3880 ret = of_clk_set_defaults(np, true);
3882 of_clk_del_provider(np);
3886 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3888 static void devm_of_clk_release_provider(struct device *dev, void *res)
3890 of_clk_del_provider(*(struct device_node **)res);
3894 * We allow a child device to use its parent device as the clock provider node
3895 * for cases like MFD sub-devices where the child device driver wants to use
3896 * devm_*() APIs but not list the device in DT as a sub-node.
3898 static struct device_node *get_clk_provider_node(struct device *dev)
3900 struct device_node *np, *parent_np;
3903 parent_np = dev->parent ? dev->parent->of_node : NULL;
3905 if (!of_find_property(np, "#clock-cells", NULL))
3906 if (of_find_property(parent_np, "#clock-cells", NULL))
3913 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
3914 * @dev: Device acting as the clock provider (used for DT node and lifetime)
3915 * @get: callback for decoding clk_hw
3916 * @data: context pointer for @get callback
3918 * Registers clock provider for given device's node. If the device has no DT
3919 * node or if the device node lacks of clock provider information (#clock-cells)
3920 * then the parent device's node is scanned for this information. If parent node
3921 * has the #clock-cells then it is used in registration. Provider is
3922 * automatically released at device exit.
3924 * Return: 0 on success or an errno on failure.
3926 int devm_of_clk_add_hw_provider(struct device *dev,
3927 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3931 struct device_node **ptr, *np;
3934 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3939 np = get_clk_provider_node(dev);
3940 ret = of_clk_add_hw_provider(np, get, data);
3943 devres_add(dev, ptr);
3950 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3953 * of_clk_del_provider() - Remove a previously registered clock provider
3954 * @np: Device node pointer associated with clock provider
3956 void of_clk_del_provider(struct device_node *np)
3958 struct of_clk_provider *cp;
3960 mutex_lock(&of_clk_mutex);
3961 list_for_each_entry(cp, &of_clk_providers, link) {
3962 if (cp->node == np) {
3963 list_del(&cp->link);
3964 of_node_put(cp->node);
3969 mutex_unlock(&of_clk_mutex);
3971 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3973 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3975 struct device_node **np = res;
3977 if (WARN_ON(!np || !*np))
3984 * devm_of_clk_del_provider() - Remove clock provider registered using devm
3985 * @dev: Device to whose lifetime the clock provider was bound
3987 void devm_of_clk_del_provider(struct device *dev)
3990 struct device_node *np = get_clk_provider_node(dev);
3992 ret = devres_release(dev, devm_of_clk_release_provider,
3993 devm_clk_provider_match, np);
3997 EXPORT_SYMBOL(devm_of_clk_del_provider);
3999 static struct clk_hw *
4000 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4001 struct of_phandle_args *clkspec)
4005 if (provider->get_hw)
4006 return provider->get_hw(clkspec, provider->data);
4008 clk = provider->get(clkspec, provider->data);
4010 return ERR_CAST(clk);
4011 return __clk_get_hw(clk);
4014 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
4015 const char *dev_id, const char *con_id)
4017 struct of_clk_provider *provider;
4018 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
4022 return ERR_PTR(-EINVAL);
4024 /* Check if we have such a provider in our array */
4025 mutex_lock(&of_clk_mutex);
4026 list_for_each_entry(provider, &of_clk_providers, link) {
4027 if (provider->node == clkspec->np) {
4028 hw = __of_clk_get_hw_from_provider(provider, clkspec);
4029 clk = __clk_create_clk(hw, dev_id, con_id);
4033 if (!__clk_get(clk)) {
4034 __clk_free_clk(clk);
4035 clk = ERR_PTR(-ENOENT);
4041 mutex_unlock(&of_clk_mutex);
4047 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4048 * @clkspec: pointer to a clock specifier data structure
4050 * This function looks up a struct clk from the registered list of clock
4051 * providers, an input is a clock specifier data structure as returned
4052 * from the of_parse_phandle_with_args() function call.
4054 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4056 return __of_clk_get_from_provider(clkspec, NULL, __func__);
4058 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4061 * of_clk_get_parent_count() - Count the number of clocks a device node has
4062 * @np: device node to count
4064 * Returns: The number of clocks that are possible parents of this node
4066 unsigned int of_clk_get_parent_count(struct device_node *np)
4070 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4076 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4078 const char *of_clk_get_parent_name(struct device_node *np, int index)
4080 struct of_phandle_args clkspec;
4081 struct property *prop;
4082 const char *clk_name;
4089 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4094 index = clkspec.args_count ? clkspec.args[0] : 0;
4097 /* if there is an indices property, use it to transfer the index
4098 * specified into an array offset for the clock-output-names property.
4100 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4107 /* We went off the end of 'clock-indices' without finding it */
4111 if (of_property_read_string_index(clkspec.np, "clock-output-names",
4115 * Best effort to get the name if the clock has been
4116 * registered with the framework. If the clock isn't
4117 * registered, we return the node name as the name of
4118 * the clock as long as #clock-cells = 0.
4120 clk = of_clk_get_from_provider(&clkspec);
4122 if (clkspec.args_count == 0)
4123 clk_name = clkspec.np->name;
4127 clk_name = __clk_get_name(clk);
4133 of_node_put(clkspec.np);
4136 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4139 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4141 * @np: Device node pointer associated with clock provider
4142 * @parents: pointer to char array that hold the parents' names
4143 * @size: size of the @parents array
4145 * Return: number of parents for the clock node.
4147 int of_clk_parent_fill(struct device_node *np, const char **parents,
4152 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4157 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4159 struct clock_provider {
4160 void (*clk_init_cb)(struct device_node *);
4161 struct device_node *np;
4162 struct list_head node;
4166 * This function looks for a parent clock. If there is one, then it
4167 * checks that the provider for this parent clock was initialized, in
4168 * this case the parent clock will be ready.
4170 static int parent_ready(struct device_node *np)
4175 struct clk *clk = of_clk_get(np, i);
4177 /* this parent is ready we can check the next one */
4184 /* at least one parent is not ready, we exit now */
4185 if (PTR_ERR(clk) == -EPROBE_DEFER)
4189 * Here we make assumption that the device tree is
4190 * written correctly. So an error means that there is
4191 * no more parent. As we didn't exit yet, then the
4192 * previous parent are ready. If there is no clock
4193 * parent, no need to wait for them, then we can
4194 * consider their absence as being ready
4201 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4202 * @np: Device node pointer associated with clock provider
4203 * @index: clock index
4204 * @flags: pointer to top-level framework flags
4206 * Detects if the clock-critical property exists and, if so, sets the
4207 * corresponding CLK_IS_CRITICAL flag.
4209 * Do not use this function. It exists only for legacy Device Tree
4210 * bindings, such as the one-clock-per-node style that are outdated.
4211 * Those bindings typically put all clock data into .dts and the Linux
4212 * driver has no clock data, thus making it impossible to set this flag
4213 * correctly from the driver. Only those drivers may call
4214 * of_clk_detect_critical from their setup functions.
4216 * Return: error code or zero on success
4218 int of_clk_detect_critical(struct device_node *np,
4219 int index, unsigned long *flags)
4221 struct property *prop;
4228 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4230 *flags |= CLK_IS_CRITICAL;
4236 * of_clk_init() - Scan and init clock providers from the DT
4237 * @matches: array of compatible values and init functions for providers.
4239 * This function scans the device tree for matching clock providers
4240 * and calls their initialization functions. It also does it by trying
4241 * to follow the dependencies.
4243 void __init of_clk_init(const struct of_device_id *matches)
4245 const struct of_device_id *match;
4246 struct device_node *np;
4247 struct clock_provider *clk_provider, *next;
4250 LIST_HEAD(clk_provider_list);
4253 matches = &__clk_of_table;
4255 /* First prepare the list of the clocks providers */
4256 for_each_matching_node_and_match(np, matches, &match) {
4257 struct clock_provider *parent;
4259 if (!of_device_is_available(np))
4262 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4264 list_for_each_entry_safe(clk_provider, next,
4265 &clk_provider_list, node) {
4266 list_del(&clk_provider->node);
4267 of_node_put(clk_provider->np);
4268 kfree(clk_provider);
4274 parent->clk_init_cb = match->data;
4275 parent->np = of_node_get(np);
4276 list_add_tail(&parent->node, &clk_provider_list);
4279 while (!list_empty(&clk_provider_list)) {
4280 is_init_done = false;
4281 list_for_each_entry_safe(clk_provider, next,
4282 &clk_provider_list, node) {
4283 if (force || parent_ready(clk_provider->np)) {
4285 /* Don't populate platform devices */
4286 of_node_set_flag(clk_provider->np,
4289 clk_provider->clk_init_cb(clk_provider->np);
4290 of_clk_set_defaults(clk_provider->np, true);
4292 list_del(&clk_provider->node);
4293 of_node_put(clk_provider->np);
4294 kfree(clk_provider);
4295 is_init_done = true;
4300 * We didn't manage to initialize any of the
4301 * remaining providers during the last loop, so now we
4302 * initialize all the remaining ones unconditionally
4303 * in case the clock parent was not mandatory