1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
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 (core->parents[i] == parent)
1520 if (core->parents[i])
1523 /* Fallback to comparing globally unique names */
1524 if (!strcmp(parent->name, core->parent_names[i])) {
1525 core->parents[i] = parent;
1534 * Update the orphan status of @core and all its children.
1536 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1538 struct clk_core *child;
1540 core->orphan = is_orphan;
1542 hlist_for_each_entry(child, &core->children, child_node)
1543 clk_core_update_orphan_status(child, is_orphan);
1546 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1548 bool was_orphan = core->orphan;
1550 hlist_del(&core->child_node);
1553 bool becomes_orphan = new_parent->orphan;
1555 /* avoid duplicate POST_RATE_CHANGE notifications */
1556 if (new_parent->new_child == core)
1557 new_parent->new_child = NULL;
1559 hlist_add_head(&core->child_node, &new_parent->children);
1561 if (was_orphan != becomes_orphan)
1562 clk_core_update_orphan_status(core, becomes_orphan);
1564 hlist_add_head(&core->child_node, &clk_orphan_list);
1566 clk_core_update_orphan_status(core, true);
1569 core->parent = new_parent;
1572 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1573 struct clk_core *parent)
1575 unsigned long flags;
1576 struct clk_core *old_parent = core->parent;
1579 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1581 * 2. Migrate prepare state between parents and prevent race with
1584 * If the clock is not prepared, then a race with
1585 * clk_enable/disable() is impossible since we already have the
1586 * prepare lock (future calls to clk_enable() need to be preceded by
1589 * If the clock is prepared, migrate the prepared state to the new
1590 * parent and also protect against a race with clk_enable() by
1591 * forcing the clock and the new parent on. This ensures that all
1592 * future calls to clk_enable() are practically NOPs with respect to
1593 * hardware and software states.
1595 * See also: Comment for clk_set_parent() below.
1598 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1599 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1600 clk_core_prepare_enable(old_parent);
1601 clk_core_prepare_enable(parent);
1604 /* migrate prepare count if > 0 */
1605 if (core->prepare_count) {
1606 clk_core_prepare_enable(parent);
1607 clk_core_enable_lock(core);
1610 /* update the clk tree topology */
1611 flags = clk_enable_lock();
1612 clk_reparent(core, parent);
1613 clk_enable_unlock(flags);
1618 static void __clk_set_parent_after(struct clk_core *core,
1619 struct clk_core *parent,
1620 struct clk_core *old_parent)
1623 * Finish the migration of prepare state and undo the changes done
1624 * for preventing a race with clk_enable().
1626 if (core->prepare_count) {
1627 clk_core_disable_lock(core);
1628 clk_core_disable_unprepare(old_parent);
1631 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1632 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1633 clk_core_disable_unprepare(parent);
1634 clk_core_disable_unprepare(old_parent);
1638 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1641 unsigned long flags;
1643 struct clk_core *old_parent;
1645 old_parent = __clk_set_parent_before(core, parent);
1647 trace_clk_set_parent(core, parent);
1649 /* change clock input source */
1650 if (parent && core->ops->set_parent)
1651 ret = core->ops->set_parent(core->hw, p_index);
1653 trace_clk_set_parent_complete(core, parent);
1656 flags = clk_enable_lock();
1657 clk_reparent(core, old_parent);
1658 clk_enable_unlock(flags);
1659 __clk_set_parent_after(core, old_parent, parent);
1664 __clk_set_parent_after(core, parent, old_parent);
1670 * __clk_speculate_rates
1671 * @core: first clk in the subtree
1672 * @parent_rate: the "future" rate of clk's parent
1674 * Walks the subtree of clks starting with clk, speculating rates as it
1675 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1677 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1678 * pre-rate change notifications and returns early if no clks in the
1679 * subtree have subscribed to the notifications. Note that if a clk does not
1680 * implement the .recalc_rate callback then it is assumed that the clock will
1681 * take on the rate of its parent.
1683 static int __clk_speculate_rates(struct clk_core *core,
1684 unsigned long parent_rate)
1686 struct clk_core *child;
1687 unsigned long new_rate;
1688 int ret = NOTIFY_DONE;
1690 lockdep_assert_held(&prepare_lock);
1692 new_rate = clk_recalc(core, parent_rate);
1694 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1695 if (core->notifier_count)
1696 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1698 if (ret & NOTIFY_STOP_MASK) {
1699 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1700 __func__, core->name, ret);
1704 hlist_for_each_entry(child, &core->children, child_node) {
1705 ret = __clk_speculate_rates(child, new_rate);
1706 if (ret & NOTIFY_STOP_MASK)
1714 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1715 struct clk_core *new_parent, u8 p_index)
1717 struct clk_core *child;
1719 core->new_rate = new_rate;
1720 core->new_parent = new_parent;
1721 core->new_parent_index = p_index;
1722 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1723 core->new_child = NULL;
1724 if (new_parent && new_parent != core->parent)
1725 new_parent->new_child = core;
1727 hlist_for_each_entry(child, &core->children, child_node) {
1728 child->new_rate = clk_recalc(child, new_rate);
1729 clk_calc_subtree(child, child->new_rate, NULL, 0);
1734 * calculate the new rates returning the topmost clock that has to be
1737 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1740 struct clk_core *top = core;
1741 struct clk_core *old_parent, *parent;
1742 unsigned long best_parent_rate = 0;
1743 unsigned long new_rate;
1744 unsigned long min_rate;
1745 unsigned long max_rate;
1750 if (IS_ERR_OR_NULL(core))
1753 /* save parent rate, if it exists */
1754 parent = old_parent = core->parent;
1756 best_parent_rate = parent->rate;
1758 clk_core_get_boundaries(core, &min_rate, &max_rate);
1760 /* find the closest rate and parent clk/rate */
1761 if (clk_core_can_round(core)) {
1762 struct clk_rate_request req;
1765 req.min_rate = min_rate;
1766 req.max_rate = max_rate;
1768 clk_core_init_rate_req(core, &req);
1770 ret = clk_core_determine_round_nolock(core, &req);
1774 best_parent_rate = req.best_parent_rate;
1775 new_rate = req.rate;
1776 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1778 if (new_rate < min_rate || new_rate > max_rate)
1780 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1781 /* pass-through clock without adjustable parent */
1782 core->new_rate = core->rate;
1785 /* pass-through clock with adjustable parent */
1786 top = clk_calc_new_rates(parent, rate);
1787 new_rate = parent->new_rate;
1791 /* some clocks must be gated to change parent */
1792 if (parent != old_parent &&
1793 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1794 pr_debug("%s: %s not gated but wants to reparent\n",
1795 __func__, core->name);
1799 /* try finding the new parent index */
1800 if (parent && core->num_parents > 1) {
1801 p_index = clk_fetch_parent_index(core, parent);
1803 pr_debug("%s: clk %s can not be parent of clk %s\n",
1804 __func__, parent->name, core->name);
1809 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1810 best_parent_rate != parent->rate)
1811 top = clk_calc_new_rates(parent, best_parent_rate);
1814 clk_calc_subtree(core, new_rate, parent, p_index);
1820 * Notify about rate changes in a subtree. Always walk down the whole tree
1821 * so that in case of an error we can walk down the whole tree again and
1824 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1825 unsigned long event)
1827 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1828 int ret = NOTIFY_DONE;
1830 if (core->rate == core->new_rate)
1833 if (core->notifier_count) {
1834 ret = __clk_notify(core, event, core->rate, core->new_rate);
1835 if (ret & NOTIFY_STOP_MASK)
1839 hlist_for_each_entry(child, &core->children, child_node) {
1840 /* Skip children who will be reparented to another clock */
1841 if (child->new_parent && child->new_parent != core)
1843 tmp_clk = clk_propagate_rate_change(child, event);
1848 /* handle the new child who might not be in core->children yet */
1849 if (core->new_child) {
1850 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1859 * walk down a subtree and set the new rates notifying the rate
1862 static void clk_change_rate(struct clk_core *core)
1864 struct clk_core *child;
1865 struct hlist_node *tmp;
1866 unsigned long old_rate;
1867 unsigned long best_parent_rate = 0;
1868 bool skip_set_rate = false;
1869 struct clk_core *old_parent;
1870 struct clk_core *parent = NULL;
1872 old_rate = core->rate;
1874 if (core->new_parent) {
1875 parent = core->new_parent;
1876 best_parent_rate = core->new_parent->rate;
1877 } else if (core->parent) {
1878 parent = core->parent;
1879 best_parent_rate = core->parent->rate;
1882 if (clk_pm_runtime_get(core))
1885 if (core->flags & CLK_SET_RATE_UNGATE) {
1886 unsigned long flags;
1888 clk_core_prepare(core);
1889 flags = clk_enable_lock();
1890 clk_core_enable(core);
1891 clk_enable_unlock(flags);
1894 if (core->new_parent && core->new_parent != core->parent) {
1895 old_parent = __clk_set_parent_before(core, core->new_parent);
1896 trace_clk_set_parent(core, core->new_parent);
1898 if (core->ops->set_rate_and_parent) {
1899 skip_set_rate = true;
1900 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1902 core->new_parent_index);
1903 } else if (core->ops->set_parent) {
1904 core->ops->set_parent(core->hw, core->new_parent_index);
1907 trace_clk_set_parent_complete(core, core->new_parent);
1908 __clk_set_parent_after(core, core->new_parent, old_parent);
1911 if (core->flags & CLK_OPS_PARENT_ENABLE)
1912 clk_core_prepare_enable(parent);
1914 trace_clk_set_rate(core, core->new_rate);
1916 if (!skip_set_rate && core->ops->set_rate)
1917 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1919 trace_clk_set_rate_complete(core, core->new_rate);
1921 core->rate = clk_recalc(core, best_parent_rate);
1923 if (core->flags & CLK_SET_RATE_UNGATE) {
1924 unsigned long flags;
1926 flags = clk_enable_lock();
1927 clk_core_disable(core);
1928 clk_enable_unlock(flags);
1929 clk_core_unprepare(core);
1932 if (core->flags & CLK_OPS_PARENT_ENABLE)
1933 clk_core_disable_unprepare(parent);
1935 if (core->notifier_count && old_rate != core->rate)
1936 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1938 if (core->flags & CLK_RECALC_NEW_RATES)
1939 (void)clk_calc_new_rates(core, core->new_rate);
1942 * Use safe iteration, as change_rate can actually swap parents
1943 * for certain clock types.
1945 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1946 /* Skip children who will be reparented to another clock */
1947 if (child->new_parent && child->new_parent != core)
1949 clk_change_rate(child);
1952 /* handle the new child who might not be in core->children yet */
1953 if (core->new_child)
1954 clk_change_rate(core->new_child);
1956 clk_pm_runtime_put(core);
1959 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1960 unsigned long req_rate)
1963 struct clk_rate_request req;
1965 lockdep_assert_held(&prepare_lock);
1970 /* simulate what the rate would be if it could be freely set */
1971 cnt = clk_core_rate_nuke_protect(core);
1975 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1976 req.rate = req_rate;
1978 ret = clk_core_round_rate_nolock(core, &req);
1980 /* restore the protection */
1981 clk_core_rate_restore_protect(core, cnt);
1983 return ret ? 0 : req.rate;
1986 static int clk_core_set_rate_nolock(struct clk_core *core,
1987 unsigned long req_rate)
1989 struct clk_core *top, *fail_clk;
1996 rate = clk_core_req_round_rate_nolock(core, req_rate);
1998 /* bail early if nothing to do */
1999 if (rate == clk_core_get_rate_nolock(core))
2002 /* fail on a direct rate set of a protected provider */
2003 if (clk_core_rate_is_protected(core))
2006 /* calculate new rates and get the topmost changed clock */
2007 top = clk_calc_new_rates(core, req_rate);
2011 ret = clk_pm_runtime_get(core);
2015 /* notify that we are about to change rates */
2016 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2018 pr_debug("%s: failed to set %s rate\n", __func__,
2020 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2025 /* change the rates */
2026 clk_change_rate(top);
2028 core->req_rate = req_rate;
2030 clk_pm_runtime_put(core);
2036 * clk_set_rate - specify a new rate for clk
2037 * @clk: the clk whose rate is being changed
2038 * @rate: the new rate for clk
2040 * In the simplest case clk_set_rate will only adjust the rate of clk.
2042 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2043 * propagate up to clk's parent; whether or not this happens depends on the
2044 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2045 * after calling .round_rate then upstream parent propagation is ignored. If
2046 * *parent_rate comes back with a new rate for clk's parent then we propagate
2047 * up to clk's parent and set its rate. Upward propagation will continue
2048 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2049 * .round_rate stops requesting changes to clk's parent_rate.
2051 * Rate changes are accomplished via tree traversal that also recalculates the
2052 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2054 * Returns 0 on success, -EERROR otherwise.
2056 int clk_set_rate(struct clk *clk, unsigned long rate)
2063 /* prevent racing with updates to the clock topology */
2066 if (clk->exclusive_count)
2067 clk_core_rate_unprotect(clk->core);
2069 ret = clk_core_set_rate_nolock(clk->core, rate);
2071 if (clk->exclusive_count)
2072 clk_core_rate_protect(clk->core);
2074 clk_prepare_unlock();
2078 EXPORT_SYMBOL_GPL(clk_set_rate);
2081 * clk_set_rate_exclusive - specify a new rate get exclusive control
2082 * @clk: the clk whose rate is being changed
2083 * @rate: the new rate for clk
2085 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2086 * within a critical section
2088 * This can be used initially to ensure that at least 1 consumer is
2089 * statisfied when several consumers are competing for exclusivity over the
2090 * same clock provider.
2092 * The exclusivity is not applied if setting the rate failed.
2094 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2095 * clk_rate_exclusive_put().
2097 * Returns 0 on success, -EERROR otherwise.
2099 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2106 /* prevent racing with updates to the clock topology */
2110 * The temporary protection removal is not here, on purpose
2111 * This function is meant to be used instead of clk_rate_protect,
2112 * so before the consumer code path protect the clock provider
2115 ret = clk_core_set_rate_nolock(clk->core, rate);
2117 clk_core_rate_protect(clk->core);
2118 clk->exclusive_count++;
2121 clk_prepare_unlock();
2125 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2128 * clk_set_rate_range - set a rate range for a clock source
2129 * @clk: clock source
2130 * @min: desired minimum clock rate in Hz, inclusive
2131 * @max: desired maximum clock rate in Hz, inclusive
2133 * Returns success (0) or negative errno.
2135 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2138 unsigned long old_min, old_max, rate;
2144 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2145 __func__, clk->core->name, clk->dev_id, clk->con_id,
2152 if (clk->exclusive_count)
2153 clk_core_rate_unprotect(clk->core);
2155 /* Save the current values in case we need to rollback the change */
2156 old_min = clk->min_rate;
2157 old_max = clk->max_rate;
2158 clk->min_rate = min;
2159 clk->max_rate = max;
2161 rate = clk_core_get_rate_nolock(clk->core);
2162 if (rate < min || rate > max) {
2165 * We are in bit of trouble here, current rate is outside the
2166 * the requested range. We are going try to request appropriate
2167 * range boundary but there is a catch. It may fail for the
2168 * usual reason (clock broken, clock protected, etc) but also
2170 * - round_rate() was not favorable and fell on the wrong
2171 * side of the boundary
2172 * - the determine_rate() callback does not really check for
2173 * this corner case when determining the rate
2181 ret = clk_core_set_rate_nolock(clk->core, rate);
2183 /* rollback the changes */
2184 clk->min_rate = old_min;
2185 clk->max_rate = old_max;
2189 if (clk->exclusive_count)
2190 clk_core_rate_protect(clk->core);
2192 clk_prepare_unlock();
2196 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2199 * clk_set_min_rate - set a minimum clock rate for a clock source
2200 * @clk: clock source
2201 * @rate: desired minimum clock rate in Hz, inclusive
2203 * Returns success (0) or negative errno.
2205 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2210 return clk_set_rate_range(clk, rate, clk->max_rate);
2212 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2215 * clk_set_max_rate - set a maximum clock rate for a clock source
2216 * @clk: clock source
2217 * @rate: desired maximum clock rate in Hz, inclusive
2219 * Returns success (0) or negative errno.
2221 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2226 return clk_set_rate_range(clk, clk->min_rate, rate);
2228 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2231 * clk_get_parent - return the parent of a clk
2232 * @clk: the clk whose parent gets returned
2234 * Simply returns clk->parent. Returns NULL if clk is NULL.
2236 struct clk *clk_get_parent(struct clk *clk)
2244 /* TODO: Create a per-user clk and change callers to call clk_put */
2245 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2246 clk_prepare_unlock();
2250 EXPORT_SYMBOL_GPL(clk_get_parent);
2252 static struct clk_core *__clk_init_parent(struct clk_core *core)
2256 if (core->num_parents > 1 && core->ops->get_parent)
2257 index = core->ops->get_parent(core->hw);
2259 return clk_core_get_parent_by_index(core, index);
2262 static void clk_core_reparent(struct clk_core *core,
2263 struct clk_core *new_parent)
2265 clk_reparent(core, new_parent);
2266 __clk_recalc_accuracies(core);
2267 __clk_recalc_rates(core, POST_RATE_CHANGE);
2270 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2275 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2279 * clk_has_parent - check if a clock is a possible parent for another
2280 * @clk: clock source
2281 * @parent: parent clock source
2283 * This function can be used in drivers that need to check that a clock can be
2284 * the parent of another without actually changing the parent.
2286 * Returns true if @parent is a possible parent for @clk, false otherwise.
2288 bool clk_has_parent(struct clk *clk, struct clk *parent)
2290 struct clk_core *core, *parent_core;
2292 /* NULL clocks should be nops, so return success if either is NULL. */
2293 if (!clk || !parent)
2297 parent_core = parent->core;
2299 /* Optimize for the case where the parent is already the parent. */
2300 if (core->parent == parent_core)
2303 return match_string(core->parent_names, core->num_parents,
2304 parent_core->name) >= 0;
2306 EXPORT_SYMBOL_GPL(clk_has_parent);
2308 static int clk_core_set_parent_nolock(struct clk_core *core,
2309 struct clk_core *parent)
2313 unsigned long p_rate = 0;
2315 lockdep_assert_held(&prepare_lock);
2320 if (core->parent == parent)
2323 /* verify ops for for multi-parent clks */
2324 if (core->num_parents > 1 && !core->ops->set_parent)
2327 /* check that we are allowed to re-parent if the clock is in use */
2328 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2331 if (clk_core_rate_is_protected(core))
2334 /* try finding the new parent index */
2336 p_index = clk_fetch_parent_index(core, parent);
2338 pr_debug("%s: clk %s can not be parent of clk %s\n",
2339 __func__, parent->name, core->name);
2342 p_rate = parent->rate;
2345 ret = clk_pm_runtime_get(core);
2349 /* propagate PRE_RATE_CHANGE notifications */
2350 ret = __clk_speculate_rates(core, p_rate);
2352 /* abort if a driver objects */
2353 if (ret & NOTIFY_STOP_MASK)
2356 /* do the re-parent */
2357 ret = __clk_set_parent(core, parent, p_index);
2359 /* propagate rate an accuracy recalculation accordingly */
2361 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2363 __clk_recalc_rates(core, POST_RATE_CHANGE);
2364 __clk_recalc_accuracies(core);
2368 clk_pm_runtime_put(core);
2374 * clk_set_parent - switch the parent of a mux clk
2375 * @clk: the mux clk whose input we are switching
2376 * @parent: the new input to clk
2378 * Re-parent clk to use parent as its new input source. If clk is in
2379 * prepared state, the clk will get enabled for the duration of this call. If
2380 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2381 * that, the reparenting is glitchy in hardware, etc), use the
2382 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2384 * After successfully changing clk's parent clk_set_parent will update the
2385 * clk topology, sysfs topology and propagate rate recalculation via
2386 * __clk_recalc_rates.
2388 * Returns 0 on success, -EERROR otherwise.
2390 int clk_set_parent(struct clk *clk, struct clk *parent)
2399 if (clk->exclusive_count)
2400 clk_core_rate_unprotect(clk->core);
2402 ret = clk_core_set_parent_nolock(clk->core,
2403 parent ? parent->core : NULL);
2405 if (clk->exclusive_count)
2406 clk_core_rate_protect(clk->core);
2408 clk_prepare_unlock();
2412 EXPORT_SYMBOL_GPL(clk_set_parent);
2414 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2418 lockdep_assert_held(&prepare_lock);
2423 if (clk_core_rate_is_protected(core))
2426 trace_clk_set_phase(core, degrees);
2428 if (core->ops->set_phase) {
2429 ret = core->ops->set_phase(core->hw, degrees);
2431 core->phase = degrees;
2434 trace_clk_set_phase_complete(core, degrees);
2440 * clk_set_phase - adjust the phase shift of a clock signal
2441 * @clk: clock signal source
2442 * @degrees: number of degrees the signal is shifted
2444 * Shifts the phase of a clock signal by the specified
2445 * degrees. Returns 0 on success, -EERROR otherwise.
2447 * This function makes no distinction about the input or reference
2448 * signal that we adjust the clock signal phase against. For example
2449 * phase locked-loop clock signal generators we may shift phase with
2450 * respect to feedback clock signal input, but for other cases the
2451 * clock phase may be shifted with respect to some other, unspecified
2454 * Additionally the concept of phase shift does not propagate through
2455 * the clock tree hierarchy, which sets it apart from clock rates and
2456 * clock accuracy. A parent clock phase attribute does not have an
2457 * impact on the phase attribute of a child clock.
2459 int clk_set_phase(struct clk *clk, int degrees)
2466 /* sanity check degrees */
2473 if (clk->exclusive_count)
2474 clk_core_rate_unprotect(clk->core);
2476 ret = clk_core_set_phase_nolock(clk->core, degrees);
2478 if (clk->exclusive_count)
2479 clk_core_rate_protect(clk->core);
2481 clk_prepare_unlock();
2485 EXPORT_SYMBOL_GPL(clk_set_phase);
2487 static int clk_core_get_phase(struct clk_core *core)
2492 /* Always try to update cached phase if possible */
2493 if (core->ops->get_phase)
2494 core->phase = core->ops->get_phase(core->hw);
2496 clk_prepare_unlock();
2502 * clk_get_phase - return the phase shift of a clock signal
2503 * @clk: clock signal source
2505 * Returns the phase shift of a clock node in degrees, otherwise returns
2508 int clk_get_phase(struct clk *clk)
2513 return clk_core_get_phase(clk->core);
2515 EXPORT_SYMBOL_GPL(clk_get_phase);
2517 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2519 /* Assume a default value of 50% */
2524 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2526 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2528 struct clk_duty *duty = &core->duty;
2531 if (!core->ops->get_duty_cycle)
2532 return clk_core_update_duty_cycle_parent_nolock(core);
2534 ret = core->ops->get_duty_cycle(core->hw, duty);
2538 /* Don't trust the clock provider too much */
2539 if (duty->den == 0 || duty->num > duty->den) {
2547 clk_core_reset_duty_cycle_nolock(core);
2551 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2556 core->flags & CLK_DUTY_CYCLE_PARENT) {
2557 ret = clk_core_update_duty_cycle_nolock(core->parent);
2558 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2560 clk_core_reset_duty_cycle_nolock(core);
2566 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2567 struct clk_duty *duty);
2569 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2570 struct clk_duty *duty)
2574 lockdep_assert_held(&prepare_lock);
2576 if (clk_core_rate_is_protected(core))
2579 trace_clk_set_duty_cycle(core, duty);
2581 if (!core->ops->set_duty_cycle)
2582 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2584 ret = core->ops->set_duty_cycle(core->hw, duty);
2586 memcpy(&core->duty, duty, sizeof(*duty));
2588 trace_clk_set_duty_cycle_complete(core, duty);
2593 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2594 struct clk_duty *duty)
2599 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2600 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2601 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2608 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2609 * @clk: clock signal source
2610 * @num: numerator of the duty cycle ratio to be applied
2611 * @den: denominator of the duty cycle ratio to be applied
2613 * Apply the duty cycle ratio if the ratio is valid and the clock can
2614 * perform this operation
2616 * Returns (0) on success, a negative errno otherwise.
2618 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2621 struct clk_duty duty;
2626 /* sanity check the ratio */
2627 if (den == 0 || num > den)
2635 if (clk->exclusive_count)
2636 clk_core_rate_unprotect(clk->core);
2638 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2640 if (clk->exclusive_count)
2641 clk_core_rate_protect(clk->core);
2643 clk_prepare_unlock();
2647 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2649 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2652 struct clk_duty *duty = &core->duty;
2657 ret = clk_core_update_duty_cycle_nolock(core);
2659 ret = mult_frac(scale, duty->num, duty->den);
2661 clk_prepare_unlock();
2667 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2668 * @clk: clock signal source
2669 * @scale: scaling factor to be applied to represent the ratio as an integer
2671 * Returns the duty cycle ratio of a clock node multiplied by the provided
2672 * scaling factor, or negative errno on error.
2674 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2679 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2681 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2684 * clk_is_match - check if two clk's point to the same hardware clock
2685 * @p: clk compared against q
2686 * @q: clk compared against p
2688 * Returns true if the two struct clk pointers both point to the same hardware
2689 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2690 * share the same struct clk_core object.
2692 * Returns false otherwise. Note that two NULL clks are treated as matching.
2694 bool clk_is_match(const struct clk *p, const struct clk *q)
2696 /* trivial case: identical struct clk's or both NULL */
2700 /* true if clk->core pointers match. Avoid dereferencing garbage */
2701 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2702 if (p->core == q->core)
2707 EXPORT_SYMBOL_GPL(clk_is_match);
2709 /*** debugfs support ***/
2711 #ifdef CONFIG_DEBUG_FS
2712 #include <linux/debugfs.h>
2714 static struct dentry *rootdir;
2715 static int inited = 0;
2716 static DEFINE_MUTEX(clk_debug_lock);
2717 static HLIST_HEAD(clk_debug_list);
2719 static struct hlist_head *all_lists[] = {
2725 static struct hlist_head *orphan_list[] = {
2730 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2736 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
2738 30 - level * 3, c->name,
2739 c->enable_count, c->prepare_count, c->protect_count,
2740 clk_core_get_rate(c), clk_core_get_accuracy(c),
2741 clk_core_get_phase(c),
2742 clk_core_get_scaled_duty_cycle(c, 100000));
2745 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2748 struct clk_core *child;
2753 clk_summary_show_one(s, c, level);
2755 hlist_for_each_entry(child, &c->children, child_node)
2756 clk_summary_show_subtree(s, child, level + 1);
2759 static int clk_summary_show(struct seq_file *s, void *data)
2762 struct hlist_head **lists = (struct hlist_head **)s->private;
2764 seq_puts(s, " enable prepare protect duty\n");
2765 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2766 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
2770 for (; *lists; lists++)
2771 hlist_for_each_entry(c, *lists, child_node)
2772 clk_summary_show_subtree(s, c, 0);
2774 clk_prepare_unlock();
2778 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2780 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2785 /* This should be JSON format, i.e. elements separated with a comma */
2786 seq_printf(s, "\"%s\": { ", c->name);
2787 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2788 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2789 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2790 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2791 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2792 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
2793 seq_printf(s, "\"duty_cycle\": %u",
2794 clk_core_get_scaled_duty_cycle(c, 100000));
2797 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2799 struct clk_core *child;
2804 clk_dump_one(s, c, level);
2806 hlist_for_each_entry(child, &c->children, child_node) {
2808 clk_dump_subtree(s, child, level + 1);
2814 static int clk_dump_show(struct seq_file *s, void *data)
2817 bool first_node = true;
2818 struct hlist_head **lists = (struct hlist_head **)s->private;
2823 for (; *lists; lists++) {
2824 hlist_for_each_entry(c, *lists, child_node) {
2828 clk_dump_subtree(s, c, 0);
2832 clk_prepare_unlock();
2837 DEFINE_SHOW_ATTRIBUTE(clk_dump);
2839 static const struct {
2843 #define ENTRY(f) { f, #f }
2844 ENTRY(CLK_SET_RATE_GATE),
2845 ENTRY(CLK_SET_PARENT_GATE),
2846 ENTRY(CLK_SET_RATE_PARENT),
2847 ENTRY(CLK_IGNORE_UNUSED),
2848 ENTRY(CLK_IS_BASIC),
2849 ENTRY(CLK_GET_RATE_NOCACHE),
2850 ENTRY(CLK_SET_RATE_NO_REPARENT),
2851 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2852 ENTRY(CLK_RECALC_NEW_RATES),
2853 ENTRY(CLK_SET_RATE_UNGATE),
2854 ENTRY(CLK_IS_CRITICAL),
2855 ENTRY(CLK_OPS_PARENT_ENABLE),
2856 ENTRY(CLK_DUTY_CYCLE_PARENT),
2860 static int clk_flags_show(struct seq_file *s, void *data)
2862 struct clk_core *core = s->private;
2863 unsigned long flags = core->flags;
2866 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2867 if (flags & clk_flags[i].flag) {
2868 seq_printf(s, "%s\n", clk_flags[i].name);
2869 flags &= ~clk_flags[i].flag;
2874 seq_printf(s, "0x%lx\n", flags);
2879 DEFINE_SHOW_ATTRIBUTE(clk_flags);
2881 static int possible_parents_show(struct seq_file *s, void *data)
2883 struct clk_core *core = s->private;
2886 for (i = 0; i < core->num_parents - 1; i++)
2887 seq_printf(s, "%s ", core->parent_names[i]);
2889 seq_printf(s, "%s\n", core->parent_names[i]);
2893 DEFINE_SHOW_ATTRIBUTE(possible_parents);
2895 static int clk_duty_cycle_show(struct seq_file *s, void *data)
2897 struct clk_core *core = s->private;
2898 struct clk_duty *duty = &core->duty;
2900 seq_printf(s, "%u/%u\n", duty->num, duty->den);
2904 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
2906 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2908 struct dentry *root;
2910 if (!core || !pdentry)
2913 root = debugfs_create_dir(core->name, pdentry);
2914 core->dentry = root;
2916 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2917 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2918 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2919 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2920 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2921 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2922 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2923 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
2924 debugfs_create_file("clk_duty_cycle", 0444, root, core,
2925 &clk_duty_cycle_fops);
2927 if (core->num_parents > 1)
2928 debugfs_create_file("clk_possible_parents", 0444, root, core,
2929 &possible_parents_fops);
2931 if (core->ops->debug_init)
2932 core->ops->debug_init(core->hw, core->dentry);
2936 * clk_debug_register - add a clk node to the debugfs clk directory
2937 * @core: the clk being added to the debugfs clk directory
2939 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2940 * initialized. Otherwise it bails out early since the debugfs clk directory
2941 * will be created lazily by clk_debug_init as part of a late_initcall.
2943 static void clk_debug_register(struct clk_core *core)
2945 mutex_lock(&clk_debug_lock);
2946 hlist_add_head(&core->debug_node, &clk_debug_list);
2948 clk_debug_create_one(core, rootdir);
2949 mutex_unlock(&clk_debug_lock);
2953 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2954 * @core: the clk being removed from the debugfs clk directory
2956 * Dynamically removes a clk and all its child nodes from the
2957 * debugfs clk directory if clk->dentry points to debugfs created by
2958 * clk_debug_register in __clk_core_init.
2960 static void clk_debug_unregister(struct clk_core *core)
2962 mutex_lock(&clk_debug_lock);
2963 hlist_del_init(&core->debug_node);
2964 debugfs_remove_recursive(core->dentry);
2965 core->dentry = NULL;
2966 mutex_unlock(&clk_debug_lock);
2970 * clk_debug_init - lazily populate the debugfs clk directory
2972 * clks are often initialized very early during boot before memory can be
2973 * dynamically allocated and well before debugfs is setup. This function
2974 * populates the debugfs clk directory once at boot-time when we know that
2975 * debugfs is setup. It should only be called once at boot-time, all other clks
2976 * added dynamically will be done so with clk_debug_register.
2978 static int __init clk_debug_init(void)
2980 struct clk_core *core;
2982 rootdir = debugfs_create_dir("clk", NULL);
2984 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2986 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2988 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2990 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2993 mutex_lock(&clk_debug_lock);
2994 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2995 clk_debug_create_one(core, rootdir);
2998 mutex_unlock(&clk_debug_lock);
3002 late_initcall(clk_debug_init);
3004 static inline void clk_debug_register(struct clk_core *core) { }
3005 static inline void clk_debug_reparent(struct clk_core *core,
3006 struct clk_core *new_parent)
3009 static inline void clk_debug_unregister(struct clk_core *core)
3015 * __clk_core_init - initialize the data structures in a struct clk_core
3016 * @core: clk_core being initialized
3018 * Initializes the lists in struct clk_core, queries the hardware for the
3019 * parent and rate and sets them both.
3021 static int __clk_core_init(struct clk_core *core)
3024 struct clk_core *orphan;
3025 struct hlist_node *tmp2;
3033 ret = clk_pm_runtime_get(core);
3037 /* check to see if a clock with this name is already registered */
3038 if (clk_core_lookup(core->name)) {
3039 pr_debug("%s: clk %s already initialized\n",
3040 __func__, core->name);
3045 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3046 if (core->ops->set_rate &&
3047 !((core->ops->round_rate || core->ops->determine_rate) &&
3048 core->ops->recalc_rate)) {
3049 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3050 __func__, core->name);
3055 if (core->ops->set_parent && !core->ops->get_parent) {
3056 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3057 __func__, core->name);
3062 if (core->num_parents > 1 && !core->ops->get_parent) {
3063 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3064 __func__, core->name);
3069 if (core->ops->set_rate_and_parent &&
3070 !(core->ops->set_parent && core->ops->set_rate)) {
3071 pr_err("%s: %s must implement .set_parent & .set_rate\n",
3072 __func__, core->name);
3077 /* throw a WARN if any entries in parent_names are NULL */
3078 for (i = 0; i < core->num_parents; i++)
3079 WARN(!core->parent_names[i],
3080 "%s: invalid NULL in %s's .parent_names\n",
3081 __func__, core->name);
3083 core->parent = __clk_init_parent(core);
3086 * Populate core->parent if parent has already been clk_core_init'd. If
3087 * parent has not yet been clk_core_init'd then place clk in the orphan
3088 * list. If clk doesn't have any parents then place it in the root
3091 * Every time a new clk is clk_init'd then we walk the list of orphan
3092 * clocks and re-parent any that are children of the clock currently
3096 hlist_add_head(&core->child_node,
3097 &core->parent->children);
3098 core->orphan = core->parent->orphan;
3099 } else if (!core->num_parents) {
3100 hlist_add_head(&core->child_node, &clk_root_list);
3101 core->orphan = false;
3103 hlist_add_head(&core->child_node, &clk_orphan_list);
3104 core->orphan = true;
3108 * optional platform-specific magic
3110 * The .init callback is not used by any of the basic clock types, but
3111 * exists for weird hardware that must perform initialization magic.
3112 * Please consider other ways of solving initialization problems before
3113 * using this callback, as its use is discouraged.
3115 if (core->ops->init)
3116 core->ops->init(core->hw);
3119 * Set clk's accuracy. The preferred method is to use
3120 * .recalc_accuracy. For simple clocks and lazy developers the default
3121 * fallback is to use the parent's accuracy. If a clock doesn't have a
3122 * parent (or is orphaned) then accuracy is set to zero (perfect
3125 if (core->ops->recalc_accuracy)
3126 core->accuracy = core->ops->recalc_accuracy(core->hw,
3127 __clk_get_accuracy(core->parent));
3128 else if (core->parent)
3129 core->accuracy = core->parent->accuracy;
3135 * Since a phase is by definition relative to its parent, just
3136 * query the current clock phase, or just assume it's in phase.
3138 if (core->ops->get_phase)
3139 core->phase = core->ops->get_phase(core->hw);
3144 * Set clk's duty cycle.
3146 clk_core_update_duty_cycle_nolock(core);
3149 * Set clk's rate. The preferred method is to use .recalc_rate. For
3150 * simple clocks and lazy developers the default fallback is to use the
3151 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3152 * then rate is set to zero.
3154 if (core->ops->recalc_rate)
3155 rate = core->ops->recalc_rate(core->hw,
3156 clk_core_get_rate_nolock(core->parent));
3157 else if (core->parent)
3158 rate = core->parent->rate;
3161 core->rate = core->req_rate = rate;
3164 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3165 * don't get accidentally disabled when walking the orphan tree and
3166 * reparenting clocks
3168 if (core->flags & CLK_IS_CRITICAL) {
3169 unsigned long flags;
3171 clk_core_prepare(core);
3173 flags = clk_enable_lock();
3174 clk_core_enable(core);
3175 clk_enable_unlock(flags);
3179 * walk the list of orphan clocks and reparent any that newly finds a
3182 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3183 struct clk_core *parent = __clk_init_parent(orphan);
3186 * We need to use __clk_set_parent_before() and _after() to
3187 * to properly migrate any prepare/enable count of the orphan
3188 * clock. This is important for CLK_IS_CRITICAL clocks, which
3189 * are enabled during init but might not have a parent yet.
3192 /* update the clk tree topology */
3193 __clk_set_parent_before(orphan, parent);
3194 __clk_set_parent_after(orphan, parent, NULL);
3195 __clk_recalc_accuracies(orphan);
3196 __clk_recalc_rates(orphan, 0);
3200 kref_init(&core->ref);
3202 clk_pm_runtime_put(core);
3204 clk_prepare_unlock();
3207 clk_debug_register(core);
3212 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3217 /* This is to allow this function to be chained to others */
3218 if (IS_ERR_OR_NULL(hw))
3219 return ERR_CAST(hw);
3221 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3223 return ERR_PTR(-ENOMEM);
3225 clk->core = hw->core;
3226 clk->dev_id = dev_id;
3227 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3228 clk->max_rate = ULONG_MAX;
3231 hlist_add_head(&clk->clks_node, &hw->core->clks);
3232 clk_prepare_unlock();
3237 /* keep in sync with __clk_put */
3238 void __clk_free_clk(struct clk *clk)
3241 hlist_del(&clk->clks_node);
3242 clk_prepare_unlock();
3244 kfree_const(clk->con_id);
3249 * clk_register - allocate a new clock, register it and return an opaque cookie
3250 * @dev: device that is registering this clock
3251 * @hw: link to hardware-specific clock data
3253 * clk_register is the primary interface for populating the clock tree with new
3254 * clock nodes. It returns a pointer to the newly allocated struct clk which
3255 * cannot be dereferenced by driver code but may be used in conjunction with the
3256 * rest of the clock API. In the event of an error clk_register will return an
3257 * error code; drivers must test for an error code after calling clk_register.
3259 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3262 struct clk_core *core;
3264 core = kzalloc(sizeof(*core), GFP_KERNEL);
3270 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3276 if (WARN_ON(!hw->init->ops)) {
3280 core->ops = hw->init->ops;
3282 if (dev && pm_runtime_enabled(dev))
3284 if (dev && dev->driver)
3285 core->owner = dev->driver->owner;
3287 core->flags = hw->init->flags;
3288 core->num_parents = hw->init->num_parents;
3290 core->max_rate = ULONG_MAX;
3293 /* allocate local copy in case parent_names is __initdata */
3294 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
3297 if (!core->parent_names) {
3299 goto fail_parent_names;
3303 /* copy each string name in case parent_names is __initdata */
3304 for (i = 0; i < core->num_parents; i++) {
3305 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
3307 if (!core->parent_names[i]) {
3309 goto fail_parent_names_copy;
3313 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3314 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3316 if (!core->parents) {
3321 INIT_HLIST_HEAD(&core->clks);
3323 hw->clk = __clk_create_clk(hw, NULL, NULL);
3324 if (IS_ERR(hw->clk)) {
3325 ret = PTR_ERR(hw->clk);
3329 ret = __clk_core_init(core);
3333 __clk_free_clk(hw->clk);
3337 kfree(core->parents);
3338 fail_parent_names_copy:
3340 kfree_const(core->parent_names[i]);
3341 kfree(core->parent_names);
3344 kfree_const(core->name);
3348 return ERR_PTR(ret);
3350 EXPORT_SYMBOL_GPL(clk_register);
3353 * clk_hw_register - register a clk_hw and return an error code
3354 * @dev: device that is registering this clock
3355 * @hw: link to hardware-specific clock data
3357 * clk_hw_register is the primary interface for populating the clock tree with
3358 * new clock nodes. It returns an integer equal to zero indicating success or
3359 * less than zero indicating failure. Drivers must test for an error code after
3360 * calling clk_hw_register().
3362 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3364 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3366 EXPORT_SYMBOL_GPL(clk_hw_register);
3368 /* Free memory allocated for a clock. */
3369 static void __clk_release(struct kref *ref)
3371 struct clk_core *core = container_of(ref, struct clk_core, ref);
3372 int i = core->num_parents;
3374 lockdep_assert_held(&prepare_lock);
3376 kfree(core->parents);
3378 kfree_const(core->parent_names[i]);
3380 kfree(core->parent_names);
3381 kfree_const(core->name);
3386 * Empty clk_ops for unregistered clocks. These are used temporarily
3387 * after clk_unregister() was called on a clock and until last clock
3388 * consumer calls clk_put() and the struct clk object is freed.
3390 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3395 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3400 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3401 unsigned long parent_rate)
3406 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3411 static const struct clk_ops clk_nodrv_ops = {
3412 .enable = clk_nodrv_prepare_enable,
3413 .disable = clk_nodrv_disable_unprepare,
3414 .prepare = clk_nodrv_prepare_enable,
3415 .unprepare = clk_nodrv_disable_unprepare,
3416 .set_rate = clk_nodrv_set_rate,
3417 .set_parent = clk_nodrv_set_parent,
3421 * clk_unregister - unregister a currently registered clock
3422 * @clk: clock to unregister
3424 void clk_unregister(struct clk *clk)
3426 unsigned long flags;
3428 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3431 clk_debug_unregister(clk->core);
3435 if (clk->core->ops == &clk_nodrv_ops) {
3436 pr_err("%s: unregistered clock: %s\n", __func__,
3441 * Assign empty clock ops for consumers that might still hold
3442 * a reference to this clock.
3444 flags = clk_enable_lock();
3445 clk->core->ops = &clk_nodrv_ops;
3446 clk_enable_unlock(flags);
3448 if (!hlist_empty(&clk->core->children)) {
3449 struct clk_core *child;
3450 struct hlist_node *t;
3452 /* Reparent all children to the orphan list. */
3453 hlist_for_each_entry_safe(child, t, &clk->core->children,
3455 clk_core_set_parent_nolock(child, NULL);
3458 hlist_del_init(&clk->core->child_node);
3460 if (clk->core->prepare_count)
3461 pr_warn("%s: unregistering prepared clock: %s\n",
3462 __func__, clk->core->name);
3464 if (clk->core->protect_count)
3465 pr_warn("%s: unregistering protected clock: %s\n",
3466 __func__, clk->core->name);
3468 kref_put(&clk->core->ref, __clk_release);
3470 clk_prepare_unlock();
3472 EXPORT_SYMBOL_GPL(clk_unregister);
3475 * clk_hw_unregister - unregister a currently registered clk_hw
3476 * @hw: hardware-specific clock data to unregister
3478 void clk_hw_unregister(struct clk_hw *hw)
3480 clk_unregister(hw->clk);
3482 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3484 static void devm_clk_release(struct device *dev, void *res)
3486 clk_unregister(*(struct clk **)res);
3489 static void devm_clk_hw_release(struct device *dev, void *res)
3491 clk_hw_unregister(*(struct clk_hw **)res);
3495 * devm_clk_register - resource managed clk_register()
3496 * @dev: device that is registering this clock
3497 * @hw: link to hardware-specific clock data
3499 * Managed clk_register(). Clocks returned from this function are
3500 * automatically clk_unregister()ed on driver detach. See clk_register() for
3503 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3508 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3510 return ERR_PTR(-ENOMEM);
3512 clk = clk_register(dev, hw);
3515 devres_add(dev, clkp);
3522 EXPORT_SYMBOL_GPL(devm_clk_register);
3525 * devm_clk_hw_register - resource managed clk_hw_register()
3526 * @dev: device that is registering this clock
3527 * @hw: link to hardware-specific clock data
3529 * Managed clk_hw_register(). Clocks registered by this function are
3530 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3531 * for more information.
3533 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3535 struct clk_hw **hwp;
3538 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3542 ret = clk_hw_register(dev, hw);
3545 devres_add(dev, hwp);
3552 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3554 static int devm_clk_match(struct device *dev, void *res, void *data)
3556 struct clk *c = res;
3562 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3564 struct clk_hw *hw = res;
3572 * devm_clk_unregister - resource managed clk_unregister()
3573 * @clk: clock to unregister
3575 * Deallocate a clock allocated with devm_clk_register(). Normally
3576 * this function will not need to be called and the resource management
3577 * code will ensure that the resource is freed.
3579 void devm_clk_unregister(struct device *dev, struct clk *clk)
3581 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3583 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3586 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3587 * @dev: device that is unregistering the hardware-specific clock data
3588 * @hw: link to hardware-specific clock data
3590 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3591 * this function will not need to be called and the resource management
3592 * code will ensure that the resource is freed.
3594 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3596 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3599 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3604 int __clk_get(struct clk *clk)
3606 struct clk_core *core = !clk ? NULL : clk->core;
3609 if (!try_module_get(core->owner))
3612 kref_get(&core->ref);
3617 /* keep in sync with __clk_free_clk */
3618 void __clk_put(struct clk *clk)
3620 struct module *owner;
3622 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3628 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3629 * given user should be balanced with calls to clk_rate_exclusive_put()
3630 * and by that same consumer
3632 if (WARN_ON(clk->exclusive_count)) {
3633 /* We voiced our concern, let's sanitize the situation */
3634 clk->core->protect_count -= (clk->exclusive_count - 1);
3635 clk_core_rate_unprotect(clk->core);
3636 clk->exclusive_count = 0;
3639 hlist_del(&clk->clks_node);
3640 if (clk->min_rate > clk->core->req_rate ||
3641 clk->max_rate < clk->core->req_rate)
3642 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3644 owner = clk->core->owner;
3645 kref_put(&clk->core->ref, __clk_release);
3647 clk_prepare_unlock();
3651 kfree_const(clk->con_id);
3655 /*** clk rate change notifiers ***/
3658 * clk_notifier_register - add a clk rate change notifier
3659 * @clk: struct clk * to watch
3660 * @nb: struct notifier_block * with callback info
3662 * Request notification when clk's rate changes. This uses an SRCU
3663 * notifier because we want it to block and notifier unregistrations are
3664 * uncommon. The callbacks associated with the notifier must not
3665 * re-enter into the clk framework by calling any top-level clk APIs;
3666 * this will cause a nested prepare_lock mutex.
3668 * In all notification cases (pre, post and abort rate change) the original
3669 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3670 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3672 * clk_notifier_register() must be called from non-atomic context.
3673 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3674 * allocation failure; otherwise, passes along the return value of
3675 * srcu_notifier_chain_register().
3677 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3679 struct clk_notifier *cn;
3687 /* search the list of notifiers for this clk */
3688 list_for_each_entry(cn, &clk_notifier_list, node)
3692 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3693 if (cn->clk != clk) {
3694 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3699 srcu_init_notifier_head(&cn->notifier_head);
3701 list_add(&cn->node, &clk_notifier_list);
3704 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3706 clk->core->notifier_count++;
3709 clk_prepare_unlock();
3713 EXPORT_SYMBOL_GPL(clk_notifier_register);
3716 * clk_notifier_unregister - remove a clk rate change notifier
3717 * @clk: struct clk *
3718 * @nb: struct notifier_block * with callback info
3720 * Request no further notification for changes to 'clk' and frees memory
3721 * allocated in clk_notifier_register.
3723 * Returns -EINVAL if called with null arguments; otherwise, passes
3724 * along the return value of srcu_notifier_chain_unregister().
3726 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3728 struct clk_notifier *cn = NULL;
3736 list_for_each_entry(cn, &clk_notifier_list, node)
3740 if (cn->clk == clk) {
3741 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3743 clk->core->notifier_count--;
3745 /* XXX the notifier code should handle this better */
3746 if (!cn->notifier_head.head) {
3747 srcu_cleanup_notifier_head(&cn->notifier_head);
3748 list_del(&cn->node);
3756 clk_prepare_unlock();
3760 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3764 * struct of_clk_provider - Clock provider registration structure
3765 * @link: Entry in global list of clock providers
3766 * @node: Pointer to device tree node of clock provider
3767 * @get: Get clock callback. Returns NULL or a struct clk for the
3768 * given clock specifier
3769 * @data: context pointer to be passed into @get callback
3771 struct of_clk_provider {
3772 struct list_head link;
3774 struct device_node *node;
3775 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3776 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3780 static const struct of_device_id __clk_of_table_sentinel
3781 __used __section(__clk_of_table_end);
3783 static LIST_HEAD(of_clk_providers);
3784 static DEFINE_MUTEX(of_clk_mutex);
3786 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3791 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3793 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3797 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3799 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3801 struct clk_onecell_data *clk_data = data;
3802 unsigned int idx = clkspec->args[0];
3804 if (idx >= clk_data->clk_num) {
3805 pr_err("%s: invalid clock index %u\n", __func__, idx);
3806 return ERR_PTR(-EINVAL);
3809 return clk_data->clks[idx];
3811 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3814 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3816 struct clk_hw_onecell_data *hw_data = data;
3817 unsigned int idx = clkspec->args[0];
3819 if (idx >= hw_data->num) {
3820 pr_err("%s: invalid index %u\n", __func__, idx);
3821 return ERR_PTR(-EINVAL);
3824 return hw_data->hws[idx];
3826 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3829 * of_clk_add_provider() - Register a clock provider for a node
3830 * @np: Device node pointer associated with clock provider
3831 * @clk_src_get: callback for decoding clock
3832 * @data: context pointer for @clk_src_get callback.
3834 int of_clk_add_provider(struct device_node *np,
3835 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3839 struct of_clk_provider *cp;
3842 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3846 cp->node = of_node_get(np);
3848 cp->get = clk_src_get;
3850 mutex_lock(&of_clk_mutex);
3851 list_add(&cp->link, &of_clk_providers);
3852 mutex_unlock(&of_clk_mutex);
3853 pr_debug("Added clock from %pOF\n", np);
3855 ret = of_clk_set_defaults(np, true);
3857 of_clk_del_provider(np);
3861 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3864 * of_clk_add_hw_provider() - Register a clock provider for a node
3865 * @np: Device node pointer associated with clock provider
3866 * @get: callback for decoding clk_hw
3867 * @data: context pointer for @get callback.
3869 int of_clk_add_hw_provider(struct device_node *np,
3870 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3874 struct of_clk_provider *cp;
3877 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3881 cp->node = of_node_get(np);
3885 mutex_lock(&of_clk_mutex);
3886 list_add(&cp->link, &of_clk_providers);
3887 mutex_unlock(&of_clk_mutex);
3888 pr_debug("Added clk_hw provider from %pOF\n", np);
3890 ret = of_clk_set_defaults(np, true);
3892 of_clk_del_provider(np);
3896 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3898 static void devm_of_clk_release_provider(struct device *dev, void *res)
3900 of_clk_del_provider(*(struct device_node **)res);
3904 * We allow a child device to use its parent device as the clock provider node
3905 * for cases like MFD sub-devices where the child device driver wants to use
3906 * devm_*() APIs but not list the device in DT as a sub-node.
3908 static struct device_node *get_clk_provider_node(struct device *dev)
3910 struct device_node *np, *parent_np;
3913 parent_np = dev->parent ? dev->parent->of_node : NULL;
3915 if (!of_find_property(np, "#clock-cells", NULL))
3916 if (of_find_property(parent_np, "#clock-cells", NULL))
3923 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
3924 * @dev: Device acting as the clock provider (used for DT node and lifetime)
3925 * @get: callback for decoding clk_hw
3926 * @data: context pointer for @get callback
3928 * Registers clock provider for given device's node. If the device has no DT
3929 * node or if the device node lacks of clock provider information (#clock-cells)
3930 * then the parent device's node is scanned for this information. If parent node
3931 * has the #clock-cells then it is used in registration. Provider is
3932 * automatically released at device exit.
3934 * Return: 0 on success or an errno on failure.
3936 int devm_of_clk_add_hw_provider(struct device *dev,
3937 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3941 struct device_node **ptr, *np;
3944 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3949 np = get_clk_provider_node(dev);
3950 ret = of_clk_add_hw_provider(np, get, data);
3953 devres_add(dev, ptr);
3960 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3963 * of_clk_del_provider() - Remove a previously registered clock provider
3964 * @np: Device node pointer associated with clock provider
3966 void of_clk_del_provider(struct device_node *np)
3968 struct of_clk_provider *cp;
3970 mutex_lock(&of_clk_mutex);
3971 list_for_each_entry(cp, &of_clk_providers, link) {
3972 if (cp->node == np) {
3973 list_del(&cp->link);
3974 of_node_put(cp->node);
3979 mutex_unlock(&of_clk_mutex);
3981 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3983 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3985 struct device_node **np = res;
3987 if (WARN_ON(!np || !*np))
3994 * devm_of_clk_del_provider() - Remove clock provider registered using devm
3995 * @dev: Device to whose lifetime the clock provider was bound
3997 void devm_of_clk_del_provider(struct device *dev)
4000 struct device_node *np = get_clk_provider_node(dev);
4002 ret = devres_release(dev, devm_of_clk_release_provider,
4003 devm_clk_provider_match, np);
4007 EXPORT_SYMBOL(devm_of_clk_del_provider);
4009 static struct clk_hw *
4010 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4011 struct of_phandle_args *clkspec)
4015 if (provider->get_hw)
4016 return provider->get_hw(clkspec, provider->data);
4018 clk = provider->get(clkspec, provider->data);
4020 return ERR_CAST(clk);
4021 return __clk_get_hw(clk);
4024 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
4025 const char *dev_id, const char *con_id)
4027 struct of_clk_provider *provider;
4028 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
4032 return ERR_PTR(-EINVAL);
4034 /* Check if we have such a provider in our array */
4035 mutex_lock(&of_clk_mutex);
4036 list_for_each_entry(provider, &of_clk_providers, link) {
4037 if (provider->node == clkspec->np) {
4038 hw = __of_clk_get_hw_from_provider(provider, clkspec);
4039 clk = __clk_create_clk(hw, dev_id, con_id);
4043 if (!__clk_get(clk)) {
4044 __clk_free_clk(clk);
4045 clk = ERR_PTR(-ENOENT);
4051 mutex_unlock(&of_clk_mutex);
4057 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4058 * @clkspec: pointer to a clock specifier data structure
4060 * This function looks up a struct clk from the registered list of clock
4061 * providers, an input is a clock specifier data structure as returned
4062 * from the of_parse_phandle_with_args() function call.
4064 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4066 return __of_clk_get_from_provider(clkspec, NULL, __func__);
4068 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4071 * of_clk_get_parent_count() - Count the number of clocks a device node has
4072 * @np: device node to count
4074 * Returns: The number of clocks that are possible parents of this node
4076 unsigned int of_clk_get_parent_count(struct device_node *np)
4080 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4086 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4088 const char *of_clk_get_parent_name(struct device_node *np, int index)
4090 struct of_phandle_args clkspec;
4091 struct property *prop;
4092 const char *clk_name;
4099 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4104 index = clkspec.args_count ? clkspec.args[0] : 0;
4107 /* if there is an indices property, use it to transfer the index
4108 * specified into an array offset for the clock-output-names property.
4110 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4117 /* We went off the end of 'clock-indices' without finding it */
4121 if (of_property_read_string_index(clkspec.np, "clock-output-names",
4125 * Best effort to get the name if the clock has been
4126 * registered with the framework. If the clock isn't
4127 * registered, we return the node name as the name of
4128 * the clock as long as #clock-cells = 0.
4130 clk = of_clk_get_from_provider(&clkspec);
4132 if (clkspec.args_count == 0)
4133 clk_name = clkspec.np->name;
4137 clk_name = __clk_get_name(clk);
4143 of_node_put(clkspec.np);
4146 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4149 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4151 * @np: Device node pointer associated with clock provider
4152 * @parents: pointer to char array that hold the parents' names
4153 * @size: size of the @parents array
4155 * Return: number of parents for the clock node.
4157 int of_clk_parent_fill(struct device_node *np, const char **parents,
4162 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4167 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4169 struct clock_provider {
4170 void (*clk_init_cb)(struct device_node *);
4171 struct device_node *np;
4172 struct list_head node;
4176 * This function looks for a parent clock. If there is one, then it
4177 * checks that the provider for this parent clock was initialized, in
4178 * this case the parent clock will be ready.
4180 static int parent_ready(struct device_node *np)
4185 struct clk *clk = of_clk_get(np, i);
4187 /* this parent is ready we can check the next one */
4194 /* at least one parent is not ready, we exit now */
4195 if (PTR_ERR(clk) == -EPROBE_DEFER)
4199 * Here we make assumption that the device tree is
4200 * written correctly. So an error means that there is
4201 * no more parent. As we didn't exit yet, then the
4202 * previous parent are ready. If there is no clock
4203 * parent, no need to wait for them, then we can
4204 * consider their absence as being ready
4211 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4212 * @np: Device node pointer associated with clock provider
4213 * @index: clock index
4214 * @flags: pointer to top-level framework flags
4216 * Detects if the clock-critical property exists and, if so, sets the
4217 * corresponding CLK_IS_CRITICAL flag.
4219 * Do not use this function. It exists only for legacy Device Tree
4220 * bindings, such as the one-clock-per-node style that are outdated.
4221 * Those bindings typically put all clock data into .dts and the Linux
4222 * driver has no clock data, thus making it impossible to set this flag
4223 * correctly from the driver. Only those drivers may call
4224 * of_clk_detect_critical from their setup functions.
4226 * Return: error code or zero on success
4228 int of_clk_detect_critical(struct device_node *np,
4229 int index, unsigned long *flags)
4231 struct property *prop;
4238 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4240 *flags |= CLK_IS_CRITICAL;
4246 * of_clk_init() - Scan and init clock providers from the DT
4247 * @matches: array of compatible values and init functions for providers.
4249 * This function scans the device tree for matching clock providers
4250 * and calls their initialization functions. It also does it by trying
4251 * to follow the dependencies.
4253 void __init of_clk_init(const struct of_device_id *matches)
4255 const struct of_device_id *match;
4256 struct device_node *np;
4257 struct clock_provider *clk_provider, *next;
4260 LIST_HEAD(clk_provider_list);
4263 matches = &__clk_of_table;
4265 /* First prepare the list of the clocks providers */
4266 for_each_matching_node_and_match(np, matches, &match) {
4267 struct clock_provider *parent;
4269 if (!of_device_is_available(np))
4272 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4274 list_for_each_entry_safe(clk_provider, next,
4275 &clk_provider_list, node) {
4276 list_del(&clk_provider->node);
4277 of_node_put(clk_provider->np);
4278 kfree(clk_provider);
4284 parent->clk_init_cb = match->data;
4285 parent->np = of_node_get(np);
4286 list_add_tail(&parent->node, &clk_provider_list);
4289 while (!list_empty(&clk_provider_list)) {
4290 is_init_done = false;
4291 list_for_each_entry_safe(clk_provider, next,
4292 &clk_provider_list, node) {
4293 if (force || parent_ready(clk_provider->np)) {
4295 /* Don't populate platform devices */
4296 of_node_set_flag(clk_provider->np,
4299 clk_provider->clk_init_cb(clk_provider->np);
4300 of_clk_set_defaults(clk_provider->np, true);
4302 list_del(&clk_provider->node);
4303 of_node_put(clk_provider->np);
4304 kfree(clk_provider);
4305 is_init_done = true;
4310 * We didn't manage to initialize any of the
4311 * remaining providers during the last loop, so now we
4312 * initialize all the remaining ones unconditionally
4313 * in case the clock parent was not mandatory