5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/clk/clk-conf.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/spinlock.h>
18 #include <linux/err.h>
19 #include <linux/list.h>
20 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/sched.h>
26 #include <linux/clkdev.h>
30 static DEFINE_SPINLOCK(enable_lock);
31 static DEFINE_MUTEX(prepare_lock);
33 static struct task_struct *prepare_owner;
34 static struct task_struct *enable_owner;
36 static int prepare_refcnt;
37 static int enable_refcnt;
39 static HLIST_HEAD(clk_root_list);
40 static HLIST_HEAD(clk_orphan_list);
41 static LIST_HEAD(clk_notifier_list);
43 /*** private data structures ***/
47 const struct clk_ops *ops;
51 struct clk_core *parent;
52 const char **parent_names;
53 struct clk_core **parents;
57 unsigned long req_rate;
58 unsigned long new_rate;
59 struct clk_core *new_parent;
60 struct clk_core *new_child;
63 unsigned int enable_count;
64 unsigned int prepare_count;
65 unsigned int protect_count;
66 unsigned long min_rate;
67 unsigned long max_rate;
68 unsigned long accuracy;
71 struct hlist_head children;
72 struct hlist_node child_node;
73 struct hlist_head clks;
74 unsigned int notifier_count;
75 #ifdef CONFIG_DEBUG_FS
76 struct dentry *dentry;
77 struct hlist_node debug_node;
82 #define CREATE_TRACE_POINTS
83 #include <trace/events/clk.h>
86 struct clk_core *core;
89 unsigned long min_rate;
90 unsigned long max_rate;
91 unsigned int exclusive_count;
92 struct hlist_node clks_node;
96 static int clk_pm_runtime_get(struct clk_core *core)
103 ret = pm_runtime_get_sync(core->dev);
104 return ret < 0 ? ret : 0;
107 static void clk_pm_runtime_put(struct clk_core *core)
112 pm_runtime_put_sync(core->dev);
116 static void clk_prepare_lock(void)
118 if (!mutex_trylock(&prepare_lock)) {
119 if (prepare_owner == current) {
123 mutex_lock(&prepare_lock);
125 WARN_ON_ONCE(prepare_owner != NULL);
126 WARN_ON_ONCE(prepare_refcnt != 0);
127 prepare_owner = current;
131 static void clk_prepare_unlock(void)
133 WARN_ON_ONCE(prepare_owner != current);
134 WARN_ON_ONCE(prepare_refcnt == 0);
136 if (--prepare_refcnt)
138 prepare_owner = NULL;
139 mutex_unlock(&prepare_lock);
142 static unsigned long clk_enable_lock(void)
143 __acquires(enable_lock)
148 * On UP systems, spin_trylock_irqsave() always returns true, even if
149 * we already hold the lock. So, in that case, we rely only on
150 * reference counting.
152 if (!IS_ENABLED(CONFIG_SMP) ||
153 !spin_trylock_irqsave(&enable_lock, flags)) {
154 if (enable_owner == current) {
156 __acquire(enable_lock);
157 if (!IS_ENABLED(CONFIG_SMP))
158 local_save_flags(flags);
161 spin_lock_irqsave(&enable_lock, flags);
163 WARN_ON_ONCE(enable_owner != NULL);
164 WARN_ON_ONCE(enable_refcnt != 0);
165 enable_owner = current;
170 static void clk_enable_unlock(unsigned long flags)
171 __releases(enable_lock)
173 WARN_ON_ONCE(enable_owner != current);
174 WARN_ON_ONCE(enable_refcnt == 0);
176 if (--enable_refcnt) {
177 __release(enable_lock);
181 spin_unlock_irqrestore(&enable_lock, flags);
184 static bool clk_core_rate_is_protected(struct clk_core *core)
186 return core->protect_count;
189 static bool clk_core_is_prepared(struct clk_core *core)
194 * .is_prepared is optional for clocks that can prepare
195 * fall back to software usage counter if it is missing
197 if (!core->ops->is_prepared)
198 return core->prepare_count;
200 if (!clk_pm_runtime_get(core)) {
201 ret = core->ops->is_prepared(core->hw);
202 clk_pm_runtime_put(core);
208 static bool clk_core_is_enabled(struct clk_core *core)
213 * .is_enabled is only mandatory for clocks that gate
214 * fall back to software usage counter if .is_enabled is missing
216 if (!core->ops->is_enabled)
217 return core->enable_count;
220 * Check if clock controller's device is runtime active before
221 * calling .is_enabled callback. If not, assume that clock is
222 * disabled, because we might be called from atomic context, from
223 * which pm_runtime_get() is not allowed.
224 * This function is called mainly from clk_disable_unused_subtree,
225 * which ensures proper runtime pm activation of controller before
226 * taking enable spinlock, but the below check is needed if one tries
227 * to call it from other places.
230 pm_runtime_get_noresume(core->dev);
231 if (!pm_runtime_active(core->dev)) {
237 ret = core->ops->is_enabled(core->hw);
240 pm_runtime_put(core->dev);
245 /*** helper functions ***/
247 const char *__clk_get_name(const struct clk *clk)
249 return !clk ? NULL : clk->core->name;
251 EXPORT_SYMBOL_GPL(__clk_get_name);
253 const char *clk_hw_get_name(const struct clk_hw *hw)
255 return hw->core->name;
257 EXPORT_SYMBOL_GPL(clk_hw_get_name);
259 struct clk_hw *__clk_get_hw(struct clk *clk)
261 return !clk ? NULL : clk->core->hw;
263 EXPORT_SYMBOL_GPL(__clk_get_hw);
265 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
267 return hw->core->num_parents;
269 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
271 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
273 return hw->core->parent ? hw->core->parent->hw : NULL;
275 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
277 static struct clk_core *__clk_lookup_subtree(const char *name,
278 struct clk_core *core)
280 struct clk_core *child;
281 struct clk_core *ret;
283 if (!strcmp(core->name, name))
286 hlist_for_each_entry(child, &core->children, child_node) {
287 ret = __clk_lookup_subtree(name, child);
295 static struct clk_core *clk_core_lookup(const char *name)
297 struct clk_core *root_clk;
298 struct clk_core *ret;
303 /* search the 'proper' clk tree first */
304 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
305 ret = __clk_lookup_subtree(name, root_clk);
310 /* if not found, then search the orphan tree */
311 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
312 ret = __clk_lookup_subtree(name, root_clk);
320 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
323 if (!core || index >= core->num_parents)
326 if (!core->parents[index])
327 core->parents[index] =
328 clk_core_lookup(core->parent_names[index]);
330 return core->parents[index];
334 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
336 struct clk_core *parent;
338 parent = clk_core_get_parent_by_index(hw->core, index);
340 return !parent ? NULL : parent->hw;
342 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
344 unsigned int __clk_get_enable_count(struct clk *clk)
346 return !clk ? 0 : clk->core->enable_count;
349 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
360 if (!core->num_parents)
370 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
372 return clk_core_get_rate_nolock(hw->core);
374 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
376 static unsigned long __clk_get_accuracy(struct clk_core *core)
381 return core->accuracy;
384 unsigned long __clk_get_flags(struct clk *clk)
386 return !clk ? 0 : clk->core->flags;
388 EXPORT_SYMBOL_GPL(__clk_get_flags);
390 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
392 return hw->core->flags;
394 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
396 bool clk_hw_is_prepared(const struct clk_hw *hw)
398 return clk_core_is_prepared(hw->core);
401 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
403 return clk_core_rate_is_protected(hw->core);
406 bool clk_hw_is_enabled(const struct clk_hw *hw)
408 return clk_core_is_enabled(hw->core);
411 bool __clk_is_enabled(struct clk *clk)
416 return clk_core_is_enabled(clk->core);
418 EXPORT_SYMBOL_GPL(__clk_is_enabled);
420 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
421 unsigned long best, unsigned long flags)
423 if (flags & CLK_MUX_ROUND_CLOSEST)
424 return abs(now - rate) < abs(best - rate);
426 return now <= rate && now > best;
429 int clk_mux_determine_rate_flags(struct clk_hw *hw,
430 struct clk_rate_request *req,
433 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
434 int i, num_parents, ret;
435 unsigned long best = 0;
436 struct clk_rate_request parent_req = *req;
438 /* if NO_REPARENT flag set, pass through to current parent */
439 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
440 parent = core->parent;
441 if (core->flags & CLK_SET_RATE_PARENT) {
442 ret = __clk_determine_rate(parent ? parent->hw : NULL,
447 best = parent_req.rate;
449 best = clk_core_get_rate_nolock(parent);
451 best = clk_core_get_rate_nolock(core);
457 /* find the parent that can provide the fastest rate <= rate */
458 num_parents = core->num_parents;
459 for (i = 0; i < num_parents; i++) {
460 parent = clk_core_get_parent_by_index(core, i);
464 if (core->flags & CLK_SET_RATE_PARENT) {
466 ret = __clk_determine_rate(parent->hw, &parent_req);
470 parent_req.rate = clk_core_get_rate_nolock(parent);
473 if (mux_is_better_rate(req->rate, parent_req.rate,
475 best_parent = parent;
476 best = parent_req.rate;
485 req->best_parent_hw = best_parent->hw;
486 req->best_parent_rate = best;
491 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
493 struct clk *__clk_lookup(const char *name)
495 struct clk_core *core = clk_core_lookup(name);
497 return !core ? NULL : core->hw->clk;
500 static void clk_core_get_boundaries(struct clk_core *core,
501 unsigned long *min_rate,
502 unsigned long *max_rate)
504 struct clk *clk_user;
506 *min_rate = core->min_rate;
507 *max_rate = core->max_rate;
509 hlist_for_each_entry(clk_user, &core->clks, clks_node)
510 *min_rate = max(*min_rate, clk_user->min_rate);
512 hlist_for_each_entry(clk_user, &core->clks, clks_node)
513 *max_rate = min(*max_rate, clk_user->max_rate);
516 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
517 unsigned long max_rate)
519 hw->core->min_rate = min_rate;
520 hw->core->max_rate = max_rate;
522 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
525 * Helper for finding best parent to provide a given frequency. This can be used
526 * directly as a determine_rate callback (e.g. for a mux), or from a more
527 * complex clock that may combine a mux with other operations.
529 int __clk_mux_determine_rate(struct clk_hw *hw,
530 struct clk_rate_request *req)
532 return clk_mux_determine_rate_flags(hw, req, 0);
534 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
536 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
537 struct clk_rate_request *req)
539 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
541 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
545 static void clk_core_rate_unprotect(struct clk_core *core)
547 lockdep_assert_held(&prepare_lock);
552 if (WARN(core->protect_count == 0,
553 "%s already unprotected\n", core->name))
556 if (--core->protect_count > 0)
559 clk_core_rate_unprotect(core->parent);
562 static int clk_core_rate_nuke_protect(struct clk_core *core)
566 lockdep_assert_held(&prepare_lock);
571 if (core->protect_count == 0)
574 ret = core->protect_count;
575 core->protect_count = 1;
576 clk_core_rate_unprotect(core);
582 * clk_rate_exclusive_put - release exclusivity over clock rate control
583 * @clk: the clk over which the exclusivity is released
585 * clk_rate_exclusive_put() completes a critical section during which a clock
586 * consumer cannot tolerate any other consumer making any operation on the
587 * clock which could result in a rate change or rate glitch. Exclusive clocks
588 * cannot have their rate changed, either directly or indirectly due to changes
589 * further up the parent chain of clocks. As a result, clocks up parent chain
590 * also get under exclusive control of the calling consumer.
592 * If exlusivity is claimed more than once on clock, even by the same consumer,
593 * the rate effectively gets locked as exclusivity can't be preempted.
595 * Calls to clk_rate_exclusive_put() must be balanced with calls to
596 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
599 void clk_rate_exclusive_put(struct clk *clk)
607 * if there is something wrong with this consumer protect count, stop
608 * here before messing with the provider
610 if (WARN_ON(clk->exclusive_count <= 0))
613 clk_core_rate_unprotect(clk->core);
614 clk->exclusive_count--;
616 clk_prepare_unlock();
618 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
620 static void clk_core_rate_protect(struct clk_core *core)
622 lockdep_assert_held(&prepare_lock);
627 if (core->protect_count == 0)
628 clk_core_rate_protect(core->parent);
630 core->protect_count++;
633 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
635 lockdep_assert_held(&prepare_lock);
643 clk_core_rate_protect(core);
644 core->protect_count = count;
648 * clk_rate_exclusive_get - get exclusivity over the clk rate control
649 * @clk: the clk over which the exclusity of rate control is requested
651 * clk_rate_exlusive_get() begins a critical section during which a clock
652 * consumer cannot tolerate any other consumer making any operation on the
653 * clock which could result in a rate change or rate glitch. Exclusive clocks
654 * cannot have their rate changed, either directly or indirectly due to changes
655 * further up the parent chain of clocks. As a result, clocks up parent chain
656 * also get under exclusive control of the calling consumer.
658 * If exlusivity is claimed more than once on clock, even by the same consumer,
659 * the rate effectively gets locked as exclusivity can't be preempted.
661 * Calls to clk_rate_exclusive_get() should be balanced with calls to
662 * clk_rate_exclusive_put(). Calls to this function may sleep.
663 * Returns 0 on success, -EERROR otherwise
665 int clk_rate_exclusive_get(struct clk *clk)
671 clk_core_rate_protect(clk->core);
672 clk->exclusive_count++;
673 clk_prepare_unlock();
677 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
679 static void clk_core_unprepare(struct clk_core *core)
681 lockdep_assert_held(&prepare_lock);
686 if (WARN(core->prepare_count == 0,
687 "%s already unprepared\n", core->name))
690 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
691 "Unpreparing critical %s\n", core->name))
694 if (core->flags & CLK_SET_RATE_GATE)
695 clk_core_rate_unprotect(core);
697 if (--core->prepare_count > 0)
700 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
702 trace_clk_unprepare(core);
704 if (core->ops->unprepare)
705 core->ops->unprepare(core->hw);
707 clk_pm_runtime_put(core);
709 trace_clk_unprepare_complete(core);
710 clk_core_unprepare(core->parent);
713 static void clk_core_unprepare_lock(struct clk_core *core)
716 clk_core_unprepare(core);
717 clk_prepare_unlock();
721 * clk_unprepare - undo preparation of a clock source
722 * @clk: the clk being unprepared
724 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
725 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
726 * if the operation may sleep. One example is a clk which is accessed over
727 * I2c. In the complex case a clk gate operation may require a fast and a slow
728 * part. It is this reason that clk_unprepare and clk_disable are not mutually
729 * exclusive. In fact clk_disable must be called before clk_unprepare.
731 void clk_unprepare(struct clk *clk)
733 if (IS_ERR_OR_NULL(clk))
736 clk_core_unprepare_lock(clk->core);
738 EXPORT_SYMBOL_GPL(clk_unprepare);
740 static int clk_core_prepare(struct clk_core *core)
744 lockdep_assert_held(&prepare_lock);
749 if (core->prepare_count == 0) {
750 ret = clk_pm_runtime_get(core);
754 ret = clk_core_prepare(core->parent);
758 trace_clk_prepare(core);
760 if (core->ops->prepare)
761 ret = core->ops->prepare(core->hw);
763 trace_clk_prepare_complete(core);
769 core->prepare_count++;
772 * CLK_SET_RATE_GATE is a special case of clock protection
773 * Instead of a consumer claiming exclusive rate control, it is
774 * actually the provider which prevents any consumer from making any
775 * operation which could result in a rate change or rate glitch while
776 * the clock is prepared.
778 if (core->flags & CLK_SET_RATE_GATE)
779 clk_core_rate_protect(core);
783 clk_core_unprepare(core->parent);
785 clk_pm_runtime_put(core);
789 static int clk_core_prepare_lock(struct clk_core *core)
794 ret = clk_core_prepare(core);
795 clk_prepare_unlock();
801 * clk_prepare - prepare a clock source
802 * @clk: the clk being prepared
804 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
805 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
806 * operation may sleep. One example is a clk which is accessed over I2c. In
807 * the complex case a clk ungate operation may require a fast and a slow part.
808 * It is this reason that clk_prepare and clk_enable are not mutually
809 * exclusive. In fact clk_prepare must be called before clk_enable.
810 * Returns 0 on success, -EERROR otherwise.
812 int clk_prepare(struct clk *clk)
817 return clk_core_prepare_lock(clk->core);
819 EXPORT_SYMBOL_GPL(clk_prepare);
821 static void clk_core_disable(struct clk_core *core)
823 lockdep_assert_held(&enable_lock);
828 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
831 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
832 "Disabling critical %s\n", core->name))
835 if (--core->enable_count > 0)
838 trace_clk_disable_rcuidle(core);
840 if (core->ops->disable)
841 core->ops->disable(core->hw);
843 trace_clk_disable_complete_rcuidle(core);
845 clk_core_disable(core->parent);
848 static void clk_core_disable_lock(struct clk_core *core)
852 flags = clk_enable_lock();
853 clk_core_disable(core);
854 clk_enable_unlock(flags);
858 * clk_disable - gate a clock
859 * @clk: the clk being gated
861 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
862 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
863 * clk if the operation is fast and will never sleep. One example is a
864 * SoC-internal clk which is controlled via simple register writes. In the
865 * complex case a clk gate operation may require a fast and a slow part. It is
866 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
867 * In fact clk_disable must be called before clk_unprepare.
869 void clk_disable(struct clk *clk)
871 if (IS_ERR_OR_NULL(clk))
874 clk_core_disable_lock(clk->core);
876 EXPORT_SYMBOL_GPL(clk_disable);
878 static int clk_core_enable(struct clk_core *core)
882 lockdep_assert_held(&enable_lock);
887 if (WARN(core->prepare_count == 0,
888 "Enabling unprepared %s\n", core->name))
891 if (core->enable_count == 0) {
892 ret = clk_core_enable(core->parent);
897 trace_clk_enable_rcuidle(core);
899 if (core->ops->enable)
900 ret = core->ops->enable(core->hw);
902 trace_clk_enable_complete_rcuidle(core);
905 clk_core_disable(core->parent);
910 core->enable_count++;
914 static int clk_core_enable_lock(struct clk_core *core)
919 flags = clk_enable_lock();
920 ret = clk_core_enable(core);
921 clk_enable_unlock(flags);
927 * clk_gate_restore_context - restore context for poweroff
928 * @hw: the clk_hw pointer of clock whose state is to be restored
930 * The clock gate restore context function enables or disables
931 * the gate clocks based on the enable_count. This is done in cases
932 * where the clock context is lost and based on the enable_count
933 * the clock either needs to be enabled/disabled. This
934 * helps restore the state of gate clocks.
936 void clk_gate_restore_context(struct clk_hw *hw)
938 struct clk_core *core = hw->core;
940 if (core->enable_count)
941 core->ops->enable(hw);
943 core->ops->disable(hw);
945 EXPORT_SYMBOL_GPL(clk_gate_restore_context);
947 static int clk_core_save_context(struct clk_core *core)
949 struct clk_core *child;
952 hlist_for_each_entry(child, &core->children, child_node) {
953 ret = clk_core_save_context(child);
958 if (core->ops && core->ops->save_context)
959 ret = core->ops->save_context(core->hw);
964 static void clk_core_restore_context(struct clk_core *core)
966 struct clk_core *child;
968 if (core->ops && core->ops->restore_context)
969 core->ops->restore_context(core->hw);
971 hlist_for_each_entry(child, &core->children, child_node)
972 clk_core_restore_context(child);
976 * clk_save_context - save clock context for poweroff
978 * Saves the context of the clock register for powerstates in which the
979 * contents of the registers will be lost. Occurs deep within the suspend
980 * code. Returns 0 on success.
982 int clk_save_context(void)
984 struct clk_core *clk;
987 hlist_for_each_entry(clk, &clk_root_list, child_node) {
988 ret = clk_core_save_context(clk);
993 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
994 ret = clk_core_save_context(clk);
1001 EXPORT_SYMBOL_GPL(clk_save_context);
1004 * clk_restore_context - restore clock context after poweroff
1006 * Restore the saved clock context upon resume.
1009 void clk_restore_context(void)
1011 struct clk_core *core;
1013 hlist_for_each_entry(core, &clk_root_list, child_node)
1014 clk_core_restore_context(core);
1016 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1017 clk_core_restore_context(core);
1019 EXPORT_SYMBOL_GPL(clk_restore_context);
1022 * clk_enable - ungate a clock
1023 * @clk: the clk being ungated
1025 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1026 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1027 * if the operation will never sleep. One example is a SoC-internal clk which
1028 * is controlled via simple register writes. In the complex case a clk ungate
1029 * operation may require a fast and a slow part. It is this reason that
1030 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1031 * must be called before clk_enable. Returns 0 on success, -EERROR
1034 int clk_enable(struct clk *clk)
1039 return clk_core_enable_lock(clk->core);
1041 EXPORT_SYMBOL_GPL(clk_enable);
1043 static int clk_core_prepare_enable(struct clk_core *core)
1047 ret = clk_core_prepare_lock(core);
1051 ret = clk_core_enable_lock(core);
1053 clk_core_unprepare_lock(core);
1058 static void clk_core_disable_unprepare(struct clk_core *core)
1060 clk_core_disable_lock(core);
1061 clk_core_unprepare_lock(core);
1064 static void clk_unprepare_unused_subtree(struct clk_core *core)
1066 struct clk_core *child;
1068 lockdep_assert_held(&prepare_lock);
1070 hlist_for_each_entry(child, &core->children, child_node)
1071 clk_unprepare_unused_subtree(child);
1073 if (core->prepare_count)
1076 if (core->flags & CLK_IGNORE_UNUSED)
1079 if (clk_pm_runtime_get(core))
1082 if (clk_core_is_prepared(core)) {
1083 trace_clk_unprepare(core);
1084 if (core->ops->unprepare_unused)
1085 core->ops->unprepare_unused(core->hw);
1086 else if (core->ops->unprepare)
1087 core->ops->unprepare(core->hw);
1088 trace_clk_unprepare_complete(core);
1091 clk_pm_runtime_put(core);
1094 static void clk_disable_unused_subtree(struct clk_core *core)
1096 struct clk_core *child;
1097 unsigned long flags;
1099 lockdep_assert_held(&prepare_lock);
1101 hlist_for_each_entry(child, &core->children, child_node)
1102 clk_disable_unused_subtree(child);
1104 if (core->flags & CLK_OPS_PARENT_ENABLE)
1105 clk_core_prepare_enable(core->parent);
1107 if (clk_pm_runtime_get(core))
1110 flags = clk_enable_lock();
1112 if (core->enable_count)
1115 if (core->flags & CLK_IGNORE_UNUSED)
1119 * some gate clocks have special needs during the disable-unused
1120 * sequence. call .disable_unused if available, otherwise fall
1123 if (clk_core_is_enabled(core)) {
1124 trace_clk_disable(core);
1125 if (core->ops->disable_unused)
1126 core->ops->disable_unused(core->hw);
1127 else if (core->ops->disable)
1128 core->ops->disable(core->hw);
1129 trace_clk_disable_complete(core);
1133 clk_enable_unlock(flags);
1134 clk_pm_runtime_put(core);
1136 if (core->flags & CLK_OPS_PARENT_ENABLE)
1137 clk_core_disable_unprepare(core->parent);
1140 static bool clk_ignore_unused;
1141 static int __init clk_ignore_unused_setup(char *__unused)
1143 clk_ignore_unused = true;
1146 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1148 static int clk_disable_unused(void)
1150 struct clk_core *core;
1152 if (clk_ignore_unused) {
1153 pr_warn("clk: Not disabling unused clocks\n");
1159 hlist_for_each_entry(core, &clk_root_list, child_node)
1160 clk_disable_unused_subtree(core);
1162 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1163 clk_disable_unused_subtree(core);
1165 hlist_for_each_entry(core, &clk_root_list, child_node)
1166 clk_unprepare_unused_subtree(core);
1168 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1169 clk_unprepare_unused_subtree(core);
1171 clk_prepare_unlock();
1175 late_initcall_sync(clk_disable_unused);
1177 static int clk_core_determine_round_nolock(struct clk_core *core,
1178 struct clk_rate_request *req)
1182 lockdep_assert_held(&prepare_lock);
1188 * At this point, core protection will be disabled if
1189 * - if the provider is not protected at all
1190 * - if the calling consumer is the only one which has exclusivity
1193 if (clk_core_rate_is_protected(core)) {
1194 req->rate = core->rate;
1195 } else if (core->ops->determine_rate) {
1196 return core->ops->determine_rate(core->hw, req);
1197 } else if (core->ops->round_rate) {
1198 rate = core->ops->round_rate(core->hw, req->rate,
1199 &req->best_parent_rate);
1211 static void clk_core_init_rate_req(struct clk_core * const core,
1212 struct clk_rate_request *req)
1214 struct clk_core *parent;
1216 if (WARN_ON(!core || !req))
1219 parent = core->parent;
1221 req->best_parent_hw = parent->hw;
1222 req->best_parent_rate = parent->rate;
1224 req->best_parent_hw = NULL;
1225 req->best_parent_rate = 0;
1229 static bool clk_core_can_round(struct clk_core * const core)
1231 if (core->ops->determine_rate || core->ops->round_rate)
1237 static int clk_core_round_rate_nolock(struct clk_core *core,
1238 struct clk_rate_request *req)
1240 lockdep_assert_held(&prepare_lock);
1247 clk_core_init_rate_req(core, req);
1249 if (clk_core_can_round(core))
1250 return clk_core_determine_round_nolock(core, req);
1251 else if (core->flags & CLK_SET_RATE_PARENT)
1252 return clk_core_round_rate_nolock(core->parent, req);
1254 req->rate = core->rate;
1259 * __clk_determine_rate - get the closest rate actually supported by a clock
1260 * @hw: determine the rate of this clock
1261 * @req: target rate request
1263 * Useful for clk_ops such as .set_rate and .determine_rate.
1265 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1272 return clk_core_round_rate_nolock(hw->core, req);
1274 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1276 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1279 struct clk_rate_request req;
1281 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1284 ret = clk_core_round_rate_nolock(hw->core, &req);
1290 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1293 * clk_round_rate - round the given rate for a clk
1294 * @clk: the clk for which we are rounding a rate
1295 * @rate: the rate which is to be rounded
1297 * Takes in a rate as input and rounds it to a rate that the clk can actually
1298 * use which is then returned. If clk doesn't support round_rate operation
1299 * then the parent rate is returned.
1301 long clk_round_rate(struct clk *clk, unsigned long rate)
1303 struct clk_rate_request req;
1311 if (clk->exclusive_count)
1312 clk_core_rate_unprotect(clk->core);
1314 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1317 ret = clk_core_round_rate_nolock(clk->core, &req);
1319 if (clk->exclusive_count)
1320 clk_core_rate_protect(clk->core);
1322 clk_prepare_unlock();
1329 EXPORT_SYMBOL_GPL(clk_round_rate);
1332 * __clk_notify - call clk notifier chain
1333 * @core: clk that is changing rate
1334 * @msg: clk notifier type (see include/linux/clk.h)
1335 * @old_rate: old clk rate
1336 * @new_rate: new clk rate
1338 * Triggers a notifier call chain on the clk rate-change notification
1339 * for 'clk'. Passes a pointer to the struct clk and the previous
1340 * and current rates to the notifier callback. Intended to be called by
1341 * internal clock code only. Returns NOTIFY_DONE from the last driver
1342 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1343 * a driver returns that.
1345 static int __clk_notify(struct clk_core *core, unsigned long msg,
1346 unsigned long old_rate, unsigned long new_rate)
1348 struct clk_notifier *cn;
1349 struct clk_notifier_data cnd;
1350 int ret = NOTIFY_DONE;
1352 cnd.old_rate = old_rate;
1353 cnd.new_rate = new_rate;
1355 list_for_each_entry(cn, &clk_notifier_list, node) {
1356 if (cn->clk->core == core) {
1358 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1360 if (ret & NOTIFY_STOP_MASK)
1369 * __clk_recalc_accuracies
1370 * @core: first clk in the subtree
1372 * Walks the subtree of clks starting with clk and recalculates accuracies as
1373 * it goes. Note that if a clk does not implement the .recalc_accuracy
1374 * callback then it is assumed that the clock will take on the accuracy of its
1377 static void __clk_recalc_accuracies(struct clk_core *core)
1379 unsigned long parent_accuracy = 0;
1380 struct clk_core *child;
1382 lockdep_assert_held(&prepare_lock);
1385 parent_accuracy = core->parent->accuracy;
1387 if (core->ops->recalc_accuracy)
1388 core->accuracy = core->ops->recalc_accuracy(core->hw,
1391 core->accuracy = parent_accuracy;
1393 hlist_for_each_entry(child, &core->children, child_node)
1394 __clk_recalc_accuracies(child);
1397 static long clk_core_get_accuracy(struct clk_core *core)
1399 unsigned long accuracy;
1402 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1403 __clk_recalc_accuracies(core);
1405 accuracy = __clk_get_accuracy(core);
1406 clk_prepare_unlock();
1412 * clk_get_accuracy - return the accuracy of clk
1413 * @clk: the clk whose accuracy is being returned
1415 * Simply returns the cached accuracy of the clk, unless
1416 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1418 * If clk is NULL then returns 0.
1420 long clk_get_accuracy(struct clk *clk)
1425 return clk_core_get_accuracy(clk->core);
1427 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1429 static unsigned long clk_recalc(struct clk_core *core,
1430 unsigned long parent_rate)
1432 unsigned long rate = parent_rate;
1434 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1435 rate = core->ops->recalc_rate(core->hw, parent_rate);
1436 clk_pm_runtime_put(core);
1442 * __clk_recalc_rates
1443 * @core: first clk in the subtree
1444 * @msg: notification type (see include/linux/clk.h)
1446 * Walks the subtree of clks starting with clk and recalculates rates as it
1447 * goes. Note that if a clk does not implement the .recalc_rate callback then
1448 * it is assumed that the clock will take on the rate of its parent.
1450 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1453 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1455 unsigned long old_rate;
1456 unsigned long parent_rate = 0;
1457 struct clk_core *child;
1459 lockdep_assert_held(&prepare_lock);
1461 old_rate = core->rate;
1464 parent_rate = core->parent->rate;
1466 core->rate = clk_recalc(core, parent_rate);
1469 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1470 * & ABORT_RATE_CHANGE notifiers
1472 if (core->notifier_count && msg)
1473 __clk_notify(core, msg, old_rate, core->rate);
1475 hlist_for_each_entry(child, &core->children, child_node)
1476 __clk_recalc_rates(child, msg);
1479 static unsigned long clk_core_get_rate(struct clk_core *core)
1485 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1486 __clk_recalc_rates(core, 0);
1488 rate = clk_core_get_rate_nolock(core);
1489 clk_prepare_unlock();
1495 * clk_get_rate - return the rate of clk
1496 * @clk: the clk whose rate is being returned
1498 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1499 * is set, which means a recalc_rate will be issued.
1500 * If clk is NULL then returns 0.
1502 unsigned long clk_get_rate(struct clk *clk)
1507 return clk_core_get_rate(clk->core);
1509 EXPORT_SYMBOL_GPL(clk_get_rate);
1511 static int clk_fetch_parent_index(struct clk_core *core,
1512 struct clk_core *parent)
1519 for (i = 0; i < core->num_parents; i++)
1520 if (clk_core_get_parent_by_index(core, i) == parent)
1527 * Update the orphan status of @core and all its children.
1529 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1531 struct clk_core *child;
1533 core->orphan = is_orphan;
1535 hlist_for_each_entry(child, &core->children, child_node)
1536 clk_core_update_orphan_status(child, is_orphan);
1539 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1541 bool was_orphan = core->orphan;
1543 hlist_del(&core->child_node);
1546 bool becomes_orphan = new_parent->orphan;
1548 /* avoid duplicate POST_RATE_CHANGE notifications */
1549 if (new_parent->new_child == core)
1550 new_parent->new_child = NULL;
1552 hlist_add_head(&core->child_node, &new_parent->children);
1554 if (was_orphan != becomes_orphan)
1555 clk_core_update_orphan_status(core, becomes_orphan);
1557 hlist_add_head(&core->child_node, &clk_orphan_list);
1559 clk_core_update_orphan_status(core, true);
1562 core->parent = new_parent;
1565 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1566 struct clk_core *parent)
1568 unsigned long flags;
1569 struct clk_core *old_parent = core->parent;
1572 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1574 * 2. Migrate prepare state between parents and prevent race with
1577 * If the clock is not prepared, then a race with
1578 * clk_enable/disable() is impossible since we already have the
1579 * prepare lock (future calls to clk_enable() need to be preceded by
1582 * If the clock is prepared, migrate the prepared state to the new
1583 * parent and also protect against a race with clk_enable() by
1584 * forcing the clock and the new parent on. This ensures that all
1585 * future calls to clk_enable() are practically NOPs with respect to
1586 * hardware and software states.
1588 * See also: Comment for clk_set_parent() below.
1591 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1592 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1593 clk_core_prepare_enable(old_parent);
1594 clk_core_prepare_enable(parent);
1597 /* migrate prepare count if > 0 */
1598 if (core->prepare_count) {
1599 clk_core_prepare_enable(parent);
1600 clk_core_enable_lock(core);
1603 /* update the clk tree topology */
1604 flags = clk_enable_lock();
1605 clk_reparent(core, parent);
1606 clk_enable_unlock(flags);
1611 static void __clk_set_parent_after(struct clk_core *core,
1612 struct clk_core *parent,
1613 struct clk_core *old_parent)
1616 * Finish the migration of prepare state and undo the changes done
1617 * for preventing a race with clk_enable().
1619 if (core->prepare_count) {
1620 clk_core_disable_lock(core);
1621 clk_core_disable_unprepare(old_parent);
1624 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1625 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1626 clk_core_disable_unprepare(parent);
1627 clk_core_disable_unprepare(old_parent);
1631 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1634 unsigned long flags;
1636 struct clk_core *old_parent;
1638 old_parent = __clk_set_parent_before(core, parent);
1640 trace_clk_set_parent(core, parent);
1642 /* change clock input source */
1643 if (parent && core->ops->set_parent)
1644 ret = core->ops->set_parent(core->hw, p_index);
1646 trace_clk_set_parent_complete(core, parent);
1649 flags = clk_enable_lock();
1650 clk_reparent(core, old_parent);
1651 clk_enable_unlock(flags);
1652 __clk_set_parent_after(core, old_parent, parent);
1657 __clk_set_parent_after(core, parent, old_parent);
1663 * __clk_speculate_rates
1664 * @core: first clk in the subtree
1665 * @parent_rate: the "future" rate of clk's parent
1667 * Walks the subtree of clks starting with clk, speculating rates as it
1668 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1670 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1671 * pre-rate change notifications and returns early if no clks in the
1672 * subtree have subscribed to the notifications. Note that if a clk does not
1673 * implement the .recalc_rate callback then it is assumed that the clock will
1674 * take on the rate of its parent.
1676 static int __clk_speculate_rates(struct clk_core *core,
1677 unsigned long parent_rate)
1679 struct clk_core *child;
1680 unsigned long new_rate;
1681 int ret = NOTIFY_DONE;
1683 lockdep_assert_held(&prepare_lock);
1685 new_rate = clk_recalc(core, parent_rate);
1687 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1688 if (core->notifier_count)
1689 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1691 if (ret & NOTIFY_STOP_MASK) {
1692 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1693 __func__, core->name, ret);
1697 hlist_for_each_entry(child, &core->children, child_node) {
1698 ret = __clk_speculate_rates(child, new_rate);
1699 if (ret & NOTIFY_STOP_MASK)
1707 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1708 struct clk_core *new_parent, u8 p_index)
1710 struct clk_core *child;
1712 core->new_rate = new_rate;
1713 core->new_parent = new_parent;
1714 core->new_parent_index = p_index;
1715 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1716 core->new_child = NULL;
1717 if (new_parent && new_parent != core->parent)
1718 new_parent->new_child = core;
1720 hlist_for_each_entry(child, &core->children, child_node) {
1721 child->new_rate = clk_recalc(child, new_rate);
1722 clk_calc_subtree(child, child->new_rate, NULL, 0);
1727 * calculate the new rates returning the topmost clock that has to be
1730 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1733 struct clk_core *top = core;
1734 struct clk_core *old_parent, *parent;
1735 unsigned long best_parent_rate = 0;
1736 unsigned long new_rate;
1737 unsigned long min_rate;
1738 unsigned long max_rate;
1743 if (IS_ERR_OR_NULL(core))
1746 /* save parent rate, if it exists */
1747 parent = old_parent = core->parent;
1749 best_parent_rate = parent->rate;
1751 clk_core_get_boundaries(core, &min_rate, &max_rate);
1753 /* find the closest rate and parent clk/rate */
1754 if (clk_core_can_round(core)) {
1755 struct clk_rate_request req;
1758 req.min_rate = min_rate;
1759 req.max_rate = max_rate;
1761 clk_core_init_rate_req(core, &req);
1763 ret = clk_core_determine_round_nolock(core, &req);
1767 best_parent_rate = req.best_parent_rate;
1768 new_rate = req.rate;
1769 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1771 if (new_rate < min_rate || new_rate > max_rate)
1773 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1774 /* pass-through clock without adjustable parent */
1775 core->new_rate = core->rate;
1778 /* pass-through clock with adjustable parent */
1779 top = clk_calc_new_rates(parent, rate);
1780 new_rate = parent->new_rate;
1784 /* some clocks must be gated to change parent */
1785 if (parent != old_parent &&
1786 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1787 pr_debug("%s: %s not gated but wants to reparent\n",
1788 __func__, core->name);
1792 /* try finding the new parent index */
1793 if (parent && core->num_parents > 1) {
1794 p_index = clk_fetch_parent_index(core, parent);
1796 pr_debug("%s: clk %s can not be parent of clk %s\n",
1797 __func__, parent->name, core->name);
1802 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1803 best_parent_rate != parent->rate)
1804 top = clk_calc_new_rates(parent, best_parent_rate);
1807 clk_calc_subtree(core, new_rate, parent, p_index);
1813 * Notify about rate changes in a subtree. Always walk down the whole tree
1814 * so that in case of an error we can walk down the whole tree again and
1817 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1818 unsigned long event)
1820 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1821 int ret = NOTIFY_DONE;
1823 if (core->rate == core->new_rate)
1826 if (core->notifier_count) {
1827 ret = __clk_notify(core, event, core->rate, core->new_rate);
1828 if (ret & NOTIFY_STOP_MASK)
1832 hlist_for_each_entry(child, &core->children, child_node) {
1833 /* Skip children who will be reparented to another clock */
1834 if (child->new_parent && child->new_parent != core)
1836 tmp_clk = clk_propagate_rate_change(child, event);
1841 /* handle the new child who might not be in core->children yet */
1842 if (core->new_child) {
1843 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1852 * walk down a subtree and set the new rates notifying the rate
1855 static void clk_change_rate(struct clk_core *core)
1857 struct clk_core *child;
1858 struct hlist_node *tmp;
1859 unsigned long old_rate;
1860 unsigned long best_parent_rate = 0;
1861 bool skip_set_rate = false;
1862 struct clk_core *old_parent;
1863 struct clk_core *parent = NULL;
1865 old_rate = core->rate;
1867 if (core->new_parent) {
1868 parent = core->new_parent;
1869 best_parent_rate = core->new_parent->rate;
1870 } else if (core->parent) {
1871 parent = core->parent;
1872 best_parent_rate = core->parent->rate;
1875 if (clk_pm_runtime_get(core))
1878 if (core->flags & CLK_SET_RATE_UNGATE) {
1879 unsigned long flags;
1881 clk_core_prepare(core);
1882 flags = clk_enable_lock();
1883 clk_core_enable(core);
1884 clk_enable_unlock(flags);
1887 if (core->new_parent && core->new_parent != core->parent) {
1888 old_parent = __clk_set_parent_before(core, core->new_parent);
1889 trace_clk_set_parent(core, core->new_parent);
1891 if (core->ops->set_rate_and_parent) {
1892 skip_set_rate = true;
1893 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1895 core->new_parent_index);
1896 } else if (core->ops->set_parent) {
1897 core->ops->set_parent(core->hw, core->new_parent_index);
1900 trace_clk_set_parent_complete(core, core->new_parent);
1901 __clk_set_parent_after(core, core->new_parent, old_parent);
1904 if (core->flags & CLK_OPS_PARENT_ENABLE)
1905 clk_core_prepare_enable(parent);
1907 trace_clk_set_rate(core, core->new_rate);
1909 if (!skip_set_rate && core->ops->set_rate)
1910 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1912 trace_clk_set_rate_complete(core, core->new_rate);
1914 core->rate = clk_recalc(core, best_parent_rate);
1916 if (core->flags & CLK_SET_RATE_UNGATE) {
1917 unsigned long flags;
1919 flags = clk_enable_lock();
1920 clk_core_disable(core);
1921 clk_enable_unlock(flags);
1922 clk_core_unprepare(core);
1925 if (core->flags & CLK_OPS_PARENT_ENABLE)
1926 clk_core_disable_unprepare(parent);
1928 if (core->notifier_count && old_rate != core->rate)
1929 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1931 if (core->flags & CLK_RECALC_NEW_RATES)
1932 (void)clk_calc_new_rates(core, core->new_rate);
1935 * Use safe iteration, as change_rate can actually swap parents
1936 * for certain clock types.
1938 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1939 /* Skip children who will be reparented to another clock */
1940 if (child->new_parent && child->new_parent != core)
1942 clk_change_rate(child);
1945 /* handle the new child who might not be in core->children yet */
1946 if (core->new_child)
1947 clk_change_rate(core->new_child);
1949 clk_pm_runtime_put(core);
1952 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1953 unsigned long req_rate)
1956 struct clk_rate_request req;
1958 lockdep_assert_held(&prepare_lock);
1963 /* simulate what the rate would be if it could be freely set */
1964 cnt = clk_core_rate_nuke_protect(core);
1968 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1969 req.rate = req_rate;
1971 ret = clk_core_round_rate_nolock(core, &req);
1973 /* restore the protection */
1974 clk_core_rate_restore_protect(core, cnt);
1976 return ret ? 0 : req.rate;
1979 static int clk_core_set_rate_nolock(struct clk_core *core,
1980 unsigned long req_rate)
1982 struct clk_core *top, *fail_clk;
1989 rate = clk_core_req_round_rate_nolock(core, req_rate);
1991 /* bail early if nothing to do */
1992 if (rate == clk_core_get_rate_nolock(core))
1995 /* fail on a direct rate set of a protected provider */
1996 if (clk_core_rate_is_protected(core))
1999 /* calculate new rates and get the topmost changed clock */
2000 top = clk_calc_new_rates(core, req_rate);
2004 ret = clk_pm_runtime_get(core);
2008 /* notify that we are about to change rates */
2009 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2011 pr_debug("%s: failed to set %s rate\n", __func__,
2013 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2018 /* change the rates */
2019 clk_change_rate(top);
2021 core->req_rate = req_rate;
2023 clk_pm_runtime_put(core);
2029 * clk_set_rate - specify a new rate for clk
2030 * @clk: the clk whose rate is being changed
2031 * @rate: the new rate for clk
2033 * In the simplest case clk_set_rate will only adjust the rate of clk.
2035 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2036 * propagate up to clk's parent; whether or not this happens depends on the
2037 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2038 * after calling .round_rate then upstream parent propagation is ignored. If
2039 * *parent_rate comes back with a new rate for clk's parent then we propagate
2040 * up to clk's parent and set its rate. Upward propagation will continue
2041 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2042 * .round_rate stops requesting changes to clk's parent_rate.
2044 * Rate changes are accomplished via tree traversal that also recalculates the
2045 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2047 * Returns 0 on success, -EERROR otherwise.
2049 int clk_set_rate(struct clk *clk, unsigned long rate)
2056 /* prevent racing with updates to the clock topology */
2059 if (clk->exclusive_count)
2060 clk_core_rate_unprotect(clk->core);
2062 ret = clk_core_set_rate_nolock(clk->core, rate);
2064 if (clk->exclusive_count)
2065 clk_core_rate_protect(clk->core);
2067 clk_prepare_unlock();
2071 EXPORT_SYMBOL_GPL(clk_set_rate);
2074 * clk_set_rate_exclusive - specify a new rate get exclusive control
2075 * @clk: the clk whose rate is being changed
2076 * @rate: the new rate for clk
2078 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2079 * within a critical section
2081 * This can be used initially to ensure that at least 1 consumer is
2082 * statisfied when several consumers are competing for exclusivity over the
2083 * same clock provider.
2085 * The exclusivity is not applied if setting the rate failed.
2087 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2088 * clk_rate_exclusive_put().
2090 * Returns 0 on success, -EERROR otherwise.
2092 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2099 /* prevent racing with updates to the clock topology */
2103 * The temporary protection removal is not here, on purpose
2104 * This function is meant to be used instead of clk_rate_protect,
2105 * so before the consumer code path protect the clock provider
2108 ret = clk_core_set_rate_nolock(clk->core, rate);
2110 clk_core_rate_protect(clk->core);
2111 clk->exclusive_count++;
2114 clk_prepare_unlock();
2118 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2121 * clk_set_rate_range - set a rate range for a clock source
2122 * @clk: clock source
2123 * @min: desired minimum clock rate in Hz, inclusive
2124 * @max: desired maximum clock rate in Hz, inclusive
2126 * Returns success (0) or negative errno.
2128 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2131 unsigned long old_min, old_max, rate;
2137 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2138 __func__, clk->core->name, clk->dev_id, clk->con_id,
2145 if (clk->exclusive_count)
2146 clk_core_rate_unprotect(clk->core);
2148 /* Save the current values in case we need to rollback the change */
2149 old_min = clk->min_rate;
2150 old_max = clk->max_rate;
2151 clk->min_rate = min;
2152 clk->max_rate = max;
2154 rate = clk_core_get_rate_nolock(clk->core);
2155 if (rate < min || rate > max) {
2158 * We are in bit of trouble here, current rate is outside the
2159 * the requested range. We are going try to request appropriate
2160 * range boundary but there is a catch. It may fail for the
2161 * usual reason (clock broken, clock protected, etc) but also
2163 * - round_rate() was not favorable and fell on the wrong
2164 * side of the boundary
2165 * - the determine_rate() callback does not really check for
2166 * this corner case when determining the rate
2174 ret = clk_core_set_rate_nolock(clk->core, rate);
2176 /* rollback the changes */
2177 clk->min_rate = old_min;
2178 clk->max_rate = old_max;
2182 if (clk->exclusive_count)
2183 clk_core_rate_protect(clk->core);
2185 clk_prepare_unlock();
2189 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2192 * clk_set_min_rate - set a minimum clock rate for a clock source
2193 * @clk: clock source
2194 * @rate: desired minimum clock rate in Hz, inclusive
2196 * Returns success (0) or negative errno.
2198 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2203 return clk_set_rate_range(clk, rate, clk->max_rate);
2205 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2208 * clk_set_max_rate - set a maximum clock rate for a clock source
2209 * @clk: clock source
2210 * @rate: desired maximum clock rate in Hz, inclusive
2212 * Returns success (0) or negative errno.
2214 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2219 return clk_set_rate_range(clk, clk->min_rate, rate);
2221 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2224 * clk_get_parent - return the parent of a clk
2225 * @clk: the clk whose parent gets returned
2227 * Simply returns clk->parent. Returns NULL if clk is NULL.
2229 struct clk *clk_get_parent(struct clk *clk)
2237 /* TODO: Create a per-user clk and change callers to call clk_put */
2238 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2239 clk_prepare_unlock();
2243 EXPORT_SYMBOL_GPL(clk_get_parent);
2245 static struct clk_core *__clk_init_parent(struct clk_core *core)
2249 if (core->num_parents > 1 && core->ops->get_parent)
2250 index = core->ops->get_parent(core->hw);
2252 return clk_core_get_parent_by_index(core, index);
2255 static void clk_core_reparent(struct clk_core *core,
2256 struct clk_core *new_parent)
2258 clk_reparent(core, new_parent);
2259 __clk_recalc_accuracies(core);
2260 __clk_recalc_rates(core, POST_RATE_CHANGE);
2263 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2268 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2272 * clk_has_parent - check if a clock is a possible parent for another
2273 * @clk: clock source
2274 * @parent: parent clock source
2276 * This function can be used in drivers that need to check that a clock can be
2277 * the parent of another without actually changing the parent.
2279 * Returns true if @parent is a possible parent for @clk, false otherwise.
2281 bool clk_has_parent(struct clk *clk, struct clk *parent)
2283 struct clk_core *core, *parent_core;
2285 /* NULL clocks should be nops, so return success if either is NULL. */
2286 if (!clk || !parent)
2290 parent_core = parent->core;
2292 /* Optimize for the case where the parent is already the parent. */
2293 if (core->parent == parent_core)
2296 return match_string(core->parent_names, core->num_parents,
2297 parent_core->name) >= 0;
2299 EXPORT_SYMBOL_GPL(clk_has_parent);
2301 static int clk_core_set_parent_nolock(struct clk_core *core,
2302 struct clk_core *parent)
2306 unsigned long p_rate = 0;
2308 lockdep_assert_held(&prepare_lock);
2313 if (core->parent == parent)
2316 /* verify ops for for multi-parent clks */
2317 if (core->num_parents > 1 && !core->ops->set_parent)
2320 /* check that we are allowed to re-parent if the clock is in use */
2321 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2324 if (clk_core_rate_is_protected(core))
2327 /* try finding the new parent index */
2329 p_index = clk_fetch_parent_index(core, parent);
2331 pr_debug("%s: clk %s can not be parent of clk %s\n",
2332 __func__, parent->name, core->name);
2335 p_rate = parent->rate;
2338 ret = clk_pm_runtime_get(core);
2342 /* propagate PRE_RATE_CHANGE notifications */
2343 ret = __clk_speculate_rates(core, p_rate);
2345 /* abort if a driver objects */
2346 if (ret & NOTIFY_STOP_MASK)
2349 /* do the re-parent */
2350 ret = __clk_set_parent(core, parent, p_index);
2352 /* propagate rate an accuracy recalculation accordingly */
2354 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2356 __clk_recalc_rates(core, POST_RATE_CHANGE);
2357 __clk_recalc_accuracies(core);
2361 clk_pm_runtime_put(core);
2367 * clk_set_parent - switch the parent of a mux clk
2368 * @clk: the mux clk whose input we are switching
2369 * @parent: the new input to clk
2371 * Re-parent clk to use parent as its new input source. If clk is in
2372 * prepared state, the clk will get enabled for the duration of this call. If
2373 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2374 * that, the reparenting is glitchy in hardware, etc), use the
2375 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2377 * After successfully changing clk's parent clk_set_parent will update the
2378 * clk topology, sysfs topology and propagate rate recalculation via
2379 * __clk_recalc_rates.
2381 * Returns 0 on success, -EERROR otherwise.
2383 int clk_set_parent(struct clk *clk, struct clk *parent)
2392 if (clk->exclusive_count)
2393 clk_core_rate_unprotect(clk->core);
2395 ret = clk_core_set_parent_nolock(clk->core,
2396 parent ? parent->core : NULL);
2398 if (clk->exclusive_count)
2399 clk_core_rate_protect(clk->core);
2401 clk_prepare_unlock();
2405 EXPORT_SYMBOL_GPL(clk_set_parent);
2407 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2411 lockdep_assert_held(&prepare_lock);
2416 if (clk_core_rate_is_protected(core))
2419 trace_clk_set_phase(core, degrees);
2421 if (core->ops->set_phase) {
2422 ret = core->ops->set_phase(core->hw, degrees);
2424 core->phase = degrees;
2427 trace_clk_set_phase_complete(core, degrees);
2433 * clk_set_phase - adjust the phase shift of a clock signal
2434 * @clk: clock signal source
2435 * @degrees: number of degrees the signal is shifted
2437 * Shifts the phase of a clock signal by the specified
2438 * degrees. Returns 0 on success, -EERROR otherwise.
2440 * This function makes no distinction about the input or reference
2441 * signal that we adjust the clock signal phase against. For example
2442 * phase locked-loop clock signal generators we may shift phase with
2443 * respect to feedback clock signal input, but for other cases the
2444 * clock phase may be shifted with respect to some other, unspecified
2447 * Additionally the concept of phase shift does not propagate through
2448 * the clock tree hierarchy, which sets it apart from clock rates and
2449 * clock accuracy. A parent clock phase attribute does not have an
2450 * impact on the phase attribute of a child clock.
2452 int clk_set_phase(struct clk *clk, int degrees)
2459 /* sanity check degrees */
2466 if (clk->exclusive_count)
2467 clk_core_rate_unprotect(clk->core);
2469 ret = clk_core_set_phase_nolock(clk->core, degrees);
2471 if (clk->exclusive_count)
2472 clk_core_rate_protect(clk->core);
2474 clk_prepare_unlock();
2478 EXPORT_SYMBOL_GPL(clk_set_phase);
2480 static int clk_core_get_phase(struct clk_core *core)
2485 /* Always try to update cached phase if possible */
2486 if (core->ops->get_phase)
2487 core->phase = core->ops->get_phase(core->hw);
2489 clk_prepare_unlock();
2495 * clk_get_phase - return the phase shift of a clock signal
2496 * @clk: clock signal source
2498 * Returns the phase shift of a clock node in degrees, otherwise returns
2501 int clk_get_phase(struct clk *clk)
2506 return clk_core_get_phase(clk->core);
2508 EXPORT_SYMBOL_GPL(clk_get_phase);
2510 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2512 /* Assume a default value of 50% */
2517 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2519 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2521 struct clk_duty *duty = &core->duty;
2524 if (!core->ops->get_duty_cycle)
2525 return clk_core_update_duty_cycle_parent_nolock(core);
2527 ret = core->ops->get_duty_cycle(core->hw, duty);
2531 /* Don't trust the clock provider too much */
2532 if (duty->den == 0 || duty->num > duty->den) {
2540 clk_core_reset_duty_cycle_nolock(core);
2544 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2549 core->flags & CLK_DUTY_CYCLE_PARENT) {
2550 ret = clk_core_update_duty_cycle_nolock(core->parent);
2551 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2553 clk_core_reset_duty_cycle_nolock(core);
2559 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2560 struct clk_duty *duty);
2562 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2563 struct clk_duty *duty)
2567 lockdep_assert_held(&prepare_lock);
2569 if (clk_core_rate_is_protected(core))
2572 trace_clk_set_duty_cycle(core, duty);
2574 if (!core->ops->set_duty_cycle)
2575 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2577 ret = core->ops->set_duty_cycle(core->hw, duty);
2579 memcpy(&core->duty, duty, sizeof(*duty));
2581 trace_clk_set_duty_cycle_complete(core, duty);
2586 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2587 struct clk_duty *duty)
2592 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2593 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2594 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2601 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2602 * @clk: clock signal source
2603 * @num: numerator of the duty cycle ratio to be applied
2604 * @den: denominator of the duty cycle ratio to be applied
2606 * Apply the duty cycle ratio if the ratio is valid and the clock can
2607 * perform this operation
2609 * Returns (0) on success, a negative errno otherwise.
2611 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2614 struct clk_duty duty;
2619 /* sanity check the ratio */
2620 if (den == 0 || num > den)
2628 if (clk->exclusive_count)
2629 clk_core_rate_unprotect(clk->core);
2631 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2633 if (clk->exclusive_count)
2634 clk_core_rate_protect(clk->core);
2636 clk_prepare_unlock();
2640 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2642 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2645 struct clk_duty *duty = &core->duty;
2650 ret = clk_core_update_duty_cycle_nolock(core);
2652 ret = mult_frac(scale, duty->num, duty->den);
2654 clk_prepare_unlock();
2660 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2661 * @clk: clock signal source
2662 * @scale: scaling factor to be applied to represent the ratio as an integer
2664 * Returns the duty cycle ratio of a clock node multiplied by the provided
2665 * scaling factor, or negative errno on error.
2667 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2672 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2674 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2677 * clk_is_match - check if two clk's point to the same hardware clock
2678 * @p: clk compared against q
2679 * @q: clk compared against p
2681 * Returns true if the two struct clk pointers both point to the same hardware
2682 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2683 * share the same struct clk_core object.
2685 * Returns false otherwise. Note that two NULL clks are treated as matching.
2687 bool clk_is_match(const struct clk *p, const struct clk *q)
2689 /* trivial case: identical struct clk's or both NULL */
2693 /* true if clk->core pointers match. Avoid dereferencing garbage */
2694 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2695 if (p->core == q->core)
2700 EXPORT_SYMBOL_GPL(clk_is_match);
2702 /*** debugfs support ***/
2704 #ifdef CONFIG_DEBUG_FS
2705 #include <linux/debugfs.h>
2707 static struct dentry *rootdir;
2708 static int inited = 0;
2709 static DEFINE_MUTEX(clk_debug_lock);
2710 static HLIST_HEAD(clk_debug_list);
2712 static struct hlist_head *all_lists[] = {
2718 static struct hlist_head *orphan_list[] = {
2723 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2729 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
2731 30 - level * 3, c->name,
2732 c->enable_count, c->prepare_count, c->protect_count,
2733 clk_core_get_rate(c), clk_core_get_accuracy(c),
2734 clk_core_get_phase(c),
2735 clk_core_get_scaled_duty_cycle(c, 100000));
2738 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2741 struct clk_core *child;
2746 clk_summary_show_one(s, c, level);
2748 hlist_for_each_entry(child, &c->children, child_node)
2749 clk_summary_show_subtree(s, child, level + 1);
2752 static int clk_summary_show(struct seq_file *s, void *data)
2755 struct hlist_head **lists = (struct hlist_head **)s->private;
2757 seq_puts(s, " enable prepare protect duty\n");
2758 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2759 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
2763 for (; *lists; lists++)
2764 hlist_for_each_entry(c, *lists, child_node)
2765 clk_summary_show_subtree(s, c, 0);
2767 clk_prepare_unlock();
2771 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2773 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2778 /* This should be JSON format, i.e. elements separated with a comma */
2779 seq_printf(s, "\"%s\": { ", c->name);
2780 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2781 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2782 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2783 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2784 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2785 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2786 seq_printf(s, "\"duty_cycle\": %u",
2787 clk_core_get_scaled_duty_cycle(c, 100000));
2790 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2792 struct clk_core *child;
2797 clk_dump_one(s, c, level);
2799 hlist_for_each_entry(child, &c->children, child_node) {
2801 clk_dump_subtree(s, child, level + 1);
2807 static int clk_dump_show(struct seq_file *s, void *data)
2810 bool first_node = true;
2811 struct hlist_head **lists = (struct hlist_head **)s->private;
2816 for (; *lists; lists++) {
2817 hlist_for_each_entry(c, *lists, child_node) {
2821 clk_dump_subtree(s, c, 0);
2825 clk_prepare_unlock();
2830 DEFINE_SHOW_ATTRIBUTE(clk_dump);
2832 static const struct {
2836 #define ENTRY(f) { f, #f }
2837 ENTRY(CLK_SET_RATE_GATE),
2838 ENTRY(CLK_SET_PARENT_GATE),
2839 ENTRY(CLK_SET_RATE_PARENT),
2840 ENTRY(CLK_IGNORE_UNUSED),
2841 ENTRY(CLK_IS_BASIC),
2842 ENTRY(CLK_GET_RATE_NOCACHE),
2843 ENTRY(CLK_SET_RATE_NO_REPARENT),
2844 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2845 ENTRY(CLK_RECALC_NEW_RATES),
2846 ENTRY(CLK_SET_RATE_UNGATE),
2847 ENTRY(CLK_IS_CRITICAL),
2848 ENTRY(CLK_OPS_PARENT_ENABLE),
2849 ENTRY(CLK_DUTY_CYCLE_PARENT),
2853 static int clk_flags_show(struct seq_file *s, void *data)
2855 struct clk_core *core = s->private;
2856 unsigned long flags = core->flags;
2859 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2860 if (flags & clk_flags[i].flag) {
2861 seq_printf(s, "%s\n", clk_flags[i].name);
2862 flags &= ~clk_flags[i].flag;
2867 seq_printf(s, "0x%lx\n", flags);
2872 DEFINE_SHOW_ATTRIBUTE(clk_flags);
2874 static int possible_parents_show(struct seq_file *s, void *data)
2876 struct clk_core *core = s->private;
2879 for (i = 0; i < core->num_parents - 1; i++)
2880 seq_printf(s, "%s ", core->parent_names[i]);
2882 seq_printf(s, "%s\n", core->parent_names[i]);
2886 DEFINE_SHOW_ATTRIBUTE(possible_parents);
2888 static int clk_duty_cycle_show(struct seq_file *s, void *data)
2890 struct clk_core *core = s->private;
2891 struct clk_duty *duty = &core->duty;
2893 seq_printf(s, "%u/%u\n", duty->num, duty->den);
2897 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
2899 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2901 struct dentry *root;
2903 if (!core || !pdentry)
2906 root = debugfs_create_dir(core->name, pdentry);
2907 core->dentry = root;
2909 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
2910 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
2911 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
2912 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
2913 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
2914 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
2915 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
2916 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
2917 debugfs_create_file("clk_duty_cycle", 0444, root, core,
2918 &clk_duty_cycle_fops);
2920 if (core->num_parents > 1)
2921 debugfs_create_file("clk_possible_parents", 0444, root, core,
2922 &possible_parents_fops);
2924 if (core->ops->debug_init)
2925 core->ops->debug_init(core->hw, core->dentry);
2929 * clk_debug_register - add a clk node to the debugfs clk directory
2930 * @core: the clk being added to the debugfs clk directory
2932 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2933 * initialized. Otherwise it bails out early since the debugfs clk directory
2934 * will be created lazily by clk_debug_init as part of a late_initcall.
2936 static void clk_debug_register(struct clk_core *core)
2938 mutex_lock(&clk_debug_lock);
2939 hlist_add_head(&core->debug_node, &clk_debug_list);
2941 clk_debug_create_one(core, rootdir);
2942 mutex_unlock(&clk_debug_lock);
2946 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2947 * @core: the clk being removed from the debugfs clk directory
2949 * Dynamically removes a clk and all its child nodes from the
2950 * debugfs clk directory if clk->dentry points to debugfs created by
2951 * clk_debug_register in __clk_core_init.
2953 static void clk_debug_unregister(struct clk_core *core)
2955 mutex_lock(&clk_debug_lock);
2956 hlist_del_init(&core->debug_node);
2957 debugfs_remove_recursive(core->dentry);
2958 core->dentry = NULL;
2959 mutex_unlock(&clk_debug_lock);
2963 * clk_debug_init - lazily populate the debugfs clk directory
2965 * clks are often initialized very early during boot before memory can be
2966 * dynamically allocated and well before debugfs is setup. This function
2967 * populates the debugfs clk directory once at boot-time when we know that
2968 * debugfs is setup. It should only be called once at boot-time, all other clks
2969 * added dynamically will be done so with clk_debug_register.
2971 static int __init clk_debug_init(void)
2973 struct clk_core *core;
2975 rootdir = debugfs_create_dir("clk", NULL);
2977 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2979 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2981 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
2983 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
2986 mutex_lock(&clk_debug_lock);
2987 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2988 clk_debug_create_one(core, rootdir);
2991 mutex_unlock(&clk_debug_lock);
2995 late_initcall(clk_debug_init);
2997 static inline void clk_debug_register(struct clk_core *core) { }
2998 static inline void clk_debug_reparent(struct clk_core *core,
2999 struct clk_core *new_parent)
3002 static inline void clk_debug_unregister(struct clk_core *core)
3008 * __clk_core_init - initialize the data structures in a struct clk_core
3009 * @core: clk_core being initialized
3011 * Initializes the lists in struct clk_core, queries the hardware for the
3012 * parent and rate and sets them both.
3014 static int __clk_core_init(struct clk_core *core)
3017 struct clk_core *orphan;
3018 struct hlist_node *tmp2;
3026 ret = clk_pm_runtime_get(core);
3030 /* check to see if a clock with this name is already registered */
3031 if (clk_core_lookup(core->name)) {
3032 pr_debug("%s: clk %s already initialized\n",
3033 __func__, core->name);
3038 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3039 if (core->ops->set_rate &&
3040 !((core->ops->round_rate || core->ops->determine_rate) &&
3041 core->ops->recalc_rate)) {
3042 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3043 __func__, core->name);
3048 if (core->ops->set_parent && !core->ops->get_parent) {
3049 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3050 __func__, core->name);
3055 if (core->num_parents > 1 && !core->ops->get_parent) {
3056 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3057 __func__, core->name);
3062 if (core->ops->set_rate_and_parent &&
3063 !(core->ops->set_parent && core->ops->set_rate)) {
3064 pr_err("%s: %s must implement .set_parent & .set_rate\n",
3065 __func__, core->name);
3070 /* throw a WARN if any entries in parent_names are NULL */
3071 for (i = 0; i < core->num_parents; i++)
3072 WARN(!core->parent_names[i],
3073 "%s: invalid NULL in %s's .parent_names\n",
3074 __func__, core->name);
3076 core->parent = __clk_init_parent(core);
3079 * Populate core->parent if parent has already been clk_core_init'd. If
3080 * parent has not yet been clk_core_init'd then place clk in the orphan
3081 * list. If clk doesn't have any parents then place it in the root
3084 * Every time a new clk is clk_init'd then we walk the list of orphan
3085 * clocks and re-parent any that are children of the clock currently
3089 hlist_add_head(&core->child_node,
3090 &core->parent->children);
3091 core->orphan = core->parent->orphan;
3092 } else if (!core->num_parents) {
3093 hlist_add_head(&core->child_node, &clk_root_list);
3094 core->orphan = false;
3096 hlist_add_head(&core->child_node, &clk_orphan_list);
3097 core->orphan = true;
3101 * optional platform-specific magic
3103 * The .init callback is not used by any of the basic clock types, but
3104 * exists for weird hardware that must perform initialization magic.
3105 * Please consider other ways of solving initialization problems before
3106 * using this callback, as its use is discouraged.
3108 if (core->ops->init)
3109 core->ops->init(core->hw);
3112 * Set clk's accuracy. The preferred method is to use
3113 * .recalc_accuracy. For simple clocks and lazy developers the default
3114 * fallback is to use the parent's accuracy. If a clock doesn't have a
3115 * parent (or is orphaned) then accuracy is set to zero (perfect
3118 if (core->ops->recalc_accuracy)
3119 core->accuracy = core->ops->recalc_accuracy(core->hw,
3120 __clk_get_accuracy(core->parent));
3121 else if (core->parent)
3122 core->accuracy = core->parent->accuracy;
3128 * Since a phase is by definition relative to its parent, just
3129 * query the current clock phase, or just assume it's in phase.
3131 if (core->ops->get_phase)
3132 core->phase = core->ops->get_phase(core->hw);
3137 * Set clk's duty cycle.
3139 clk_core_update_duty_cycle_nolock(core);
3142 * Set clk's rate. The preferred method is to use .recalc_rate. For
3143 * simple clocks and lazy developers the default fallback is to use the
3144 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3145 * then rate is set to zero.
3147 if (core->ops->recalc_rate)
3148 rate = core->ops->recalc_rate(core->hw,
3149 clk_core_get_rate_nolock(core->parent));
3150 else if (core->parent)
3151 rate = core->parent->rate;
3154 core->rate = core->req_rate = rate;
3157 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3158 * don't get accidentally disabled when walking the orphan tree and
3159 * reparenting clocks
3161 if (core->flags & CLK_IS_CRITICAL) {
3162 unsigned long flags;
3164 clk_core_prepare(core);
3166 flags = clk_enable_lock();
3167 clk_core_enable(core);
3168 clk_enable_unlock(flags);
3172 * walk the list of orphan clocks and reparent any that newly finds a
3175 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3176 struct clk_core *parent = __clk_init_parent(orphan);
3179 * We need to use __clk_set_parent_before() and _after() to
3180 * to properly migrate any prepare/enable count of the orphan
3181 * clock. This is important for CLK_IS_CRITICAL clocks, which
3182 * are enabled during init but might not have a parent yet.
3185 /* update the clk tree topology */
3186 __clk_set_parent_before(orphan, parent);
3187 __clk_set_parent_after(orphan, parent, NULL);
3188 __clk_recalc_accuracies(orphan);
3189 __clk_recalc_rates(orphan, 0);
3193 kref_init(&core->ref);
3195 clk_pm_runtime_put(core);
3197 clk_prepare_unlock();
3200 clk_debug_register(core);
3205 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3210 /* This is to allow this function to be chained to others */
3211 if (IS_ERR_OR_NULL(hw))
3212 return ERR_CAST(hw);
3214 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3216 return ERR_PTR(-ENOMEM);
3218 clk->core = hw->core;
3219 clk->dev_id = dev_id;
3220 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3221 clk->max_rate = ULONG_MAX;
3224 hlist_add_head(&clk->clks_node, &hw->core->clks);
3225 clk_prepare_unlock();
3230 /* keep in sync with __clk_put */
3231 void __clk_free_clk(struct clk *clk)
3234 hlist_del(&clk->clks_node);
3235 clk_prepare_unlock();
3237 kfree_const(clk->con_id);
3242 * clk_register - allocate a new clock, register it and return an opaque cookie
3243 * @dev: device that is registering this clock
3244 * @hw: link to hardware-specific clock data
3246 * clk_register is the primary interface for populating the clock tree with new
3247 * clock nodes. It returns a pointer to the newly allocated struct clk which
3248 * cannot be dereferenced by driver code but may be used in conjunction with the
3249 * rest of the clock API. In the event of an error clk_register will return an
3250 * error code; drivers must test for an error code after calling clk_register.
3252 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3255 struct clk_core *core;
3257 core = kzalloc(sizeof(*core), GFP_KERNEL);
3263 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3269 if (WARN_ON(!hw->init->ops)) {
3273 core->ops = hw->init->ops;
3275 if (dev && pm_runtime_enabled(dev))
3277 if (dev && dev->driver)
3278 core->owner = dev->driver->owner;
3280 core->flags = hw->init->flags;
3281 core->num_parents = hw->init->num_parents;
3283 core->max_rate = ULONG_MAX;
3286 /* allocate local copy in case parent_names is __initdata */
3287 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
3290 if (!core->parent_names) {
3292 goto fail_parent_names;
3296 /* copy each string name in case parent_names is __initdata */
3297 for (i = 0; i < core->num_parents; i++) {
3298 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
3300 if (!core->parent_names[i]) {
3302 goto fail_parent_names_copy;
3306 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3307 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3309 if (!core->parents) {
3314 INIT_HLIST_HEAD(&core->clks);
3316 hw->clk = __clk_create_clk(hw, NULL, NULL);
3317 if (IS_ERR(hw->clk)) {
3318 ret = PTR_ERR(hw->clk);
3322 ret = __clk_core_init(core);
3326 __clk_free_clk(hw->clk);
3330 kfree(core->parents);
3331 fail_parent_names_copy:
3333 kfree_const(core->parent_names[i]);
3334 kfree(core->parent_names);
3337 kfree_const(core->name);
3341 return ERR_PTR(ret);
3343 EXPORT_SYMBOL_GPL(clk_register);
3346 * clk_hw_register - register a clk_hw and return an error code
3347 * @dev: device that is registering this clock
3348 * @hw: link to hardware-specific clock data
3350 * clk_hw_register is the primary interface for populating the clock tree with
3351 * new clock nodes. It returns an integer equal to zero indicating success or
3352 * less than zero indicating failure. Drivers must test for an error code after
3353 * calling clk_hw_register().
3355 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3357 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3359 EXPORT_SYMBOL_GPL(clk_hw_register);
3361 /* Free memory allocated for a clock. */
3362 static void __clk_release(struct kref *ref)
3364 struct clk_core *core = container_of(ref, struct clk_core, ref);
3365 int i = core->num_parents;
3367 lockdep_assert_held(&prepare_lock);
3369 kfree(core->parents);
3371 kfree_const(core->parent_names[i]);
3373 kfree(core->parent_names);
3374 kfree_const(core->name);
3379 * Empty clk_ops for unregistered clocks. These are used temporarily
3380 * after clk_unregister() was called on a clock and until last clock
3381 * consumer calls clk_put() and the struct clk object is freed.
3383 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3388 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3393 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3394 unsigned long parent_rate)
3399 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3404 static const struct clk_ops clk_nodrv_ops = {
3405 .enable = clk_nodrv_prepare_enable,
3406 .disable = clk_nodrv_disable_unprepare,
3407 .prepare = clk_nodrv_prepare_enable,
3408 .unprepare = clk_nodrv_disable_unprepare,
3409 .set_rate = clk_nodrv_set_rate,
3410 .set_parent = clk_nodrv_set_parent,
3414 * clk_unregister - unregister a currently registered clock
3415 * @clk: clock to unregister
3417 void clk_unregister(struct clk *clk)
3419 unsigned long flags;
3421 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3424 clk_debug_unregister(clk->core);
3428 if (clk->core->ops == &clk_nodrv_ops) {
3429 pr_err("%s: unregistered clock: %s\n", __func__,
3434 * Assign empty clock ops for consumers that might still hold
3435 * a reference to this clock.
3437 flags = clk_enable_lock();
3438 clk->core->ops = &clk_nodrv_ops;
3439 clk_enable_unlock(flags);
3441 if (!hlist_empty(&clk->core->children)) {
3442 struct clk_core *child;
3443 struct hlist_node *t;
3445 /* Reparent all children to the orphan list. */
3446 hlist_for_each_entry_safe(child, t, &clk->core->children,
3448 clk_core_set_parent_nolock(child, NULL);
3451 hlist_del_init(&clk->core->child_node);
3453 if (clk->core->prepare_count)
3454 pr_warn("%s: unregistering prepared clock: %s\n",
3455 __func__, clk->core->name);
3457 if (clk->core->protect_count)
3458 pr_warn("%s: unregistering protected clock: %s\n",
3459 __func__, clk->core->name);
3461 kref_put(&clk->core->ref, __clk_release);
3463 clk_prepare_unlock();
3465 EXPORT_SYMBOL_GPL(clk_unregister);
3468 * clk_hw_unregister - unregister a currently registered clk_hw
3469 * @hw: hardware-specific clock data to unregister
3471 void clk_hw_unregister(struct clk_hw *hw)
3473 clk_unregister(hw->clk);
3475 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3477 static void devm_clk_release(struct device *dev, void *res)
3479 clk_unregister(*(struct clk **)res);
3482 static void devm_clk_hw_release(struct device *dev, void *res)
3484 clk_hw_unregister(*(struct clk_hw **)res);
3488 * devm_clk_register - resource managed clk_register()
3489 * @dev: device that is registering this clock
3490 * @hw: link to hardware-specific clock data
3492 * Managed clk_register(). Clocks returned from this function are
3493 * automatically clk_unregister()ed on driver detach. See clk_register() for
3496 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3501 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3503 return ERR_PTR(-ENOMEM);
3505 clk = clk_register(dev, hw);
3508 devres_add(dev, clkp);
3515 EXPORT_SYMBOL_GPL(devm_clk_register);
3518 * devm_clk_hw_register - resource managed clk_hw_register()
3519 * @dev: device that is registering this clock
3520 * @hw: link to hardware-specific clock data
3522 * Managed clk_hw_register(). Clocks registered by this function are
3523 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3524 * for more information.
3526 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3528 struct clk_hw **hwp;
3531 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3535 ret = clk_hw_register(dev, hw);
3538 devres_add(dev, hwp);
3545 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3547 static int devm_clk_match(struct device *dev, void *res, void *data)
3549 struct clk *c = res;
3555 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3557 struct clk_hw *hw = res;
3565 * devm_clk_unregister - resource managed clk_unregister()
3566 * @clk: clock to unregister
3568 * Deallocate a clock allocated with devm_clk_register(). Normally
3569 * this function will not need to be called and the resource management
3570 * code will ensure that the resource is freed.
3572 void devm_clk_unregister(struct device *dev, struct clk *clk)
3574 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3576 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3579 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3580 * @dev: device that is unregistering the hardware-specific clock data
3581 * @hw: link to hardware-specific clock data
3583 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3584 * this function will not need to be called and the resource management
3585 * code will ensure that the resource is freed.
3587 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3589 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3592 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3597 int __clk_get(struct clk *clk)
3599 struct clk_core *core = !clk ? NULL : clk->core;
3602 if (!try_module_get(core->owner))
3605 kref_get(&core->ref);
3610 /* keep in sync with __clk_free_clk */
3611 void __clk_put(struct clk *clk)
3613 struct module *owner;
3615 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3621 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3622 * given user should be balanced with calls to clk_rate_exclusive_put()
3623 * and by that same consumer
3625 if (WARN_ON(clk->exclusive_count)) {
3626 /* We voiced our concern, let's sanitize the situation */
3627 clk->core->protect_count -= (clk->exclusive_count - 1);
3628 clk_core_rate_unprotect(clk->core);
3629 clk->exclusive_count = 0;
3632 hlist_del(&clk->clks_node);
3633 if (clk->min_rate > clk->core->req_rate ||
3634 clk->max_rate < clk->core->req_rate)
3635 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3637 owner = clk->core->owner;
3638 kref_put(&clk->core->ref, __clk_release);
3640 clk_prepare_unlock();
3644 kfree_const(clk->con_id);
3648 /*** clk rate change notifiers ***/
3651 * clk_notifier_register - add a clk rate change notifier
3652 * @clk: struct clk * to watch
3653 * @nb: struct notifier_block * with callback info
3655 * Request notification when clk's rate changes. This uses an SRCU
3656 * notifier because we want it to block and notifier unregistrations are
3657 * uncommon. The callbacks associated with the notifier must not
3658 * re-enter into the clk framework by calling any top-level clk APIs;
3659 * this will cause a nested prepare_lock mutex.
3661 * In all notification cases (pre, post and abort rate change) the original
3662 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3663 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3665 * clk_notifier_register() must be called from non-atomic context.
3666 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3667 * allocation failure; otherwise, passes along the return value of
3668 * srcu_notifier_chain_register().
3670 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3672 struct clk_notifier *cn;
3680 /* search the list of notifiers for this clk */
3681 list_for_each_entry(cn, &clk_notifier_list, node)
3685 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3686 if (cn->clk != clk) {
3687 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3692 srcu_init_notifier_head(&cn->notifier_head);
3694 list_add(&cn->node, &clk_notifier_list);
3697 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3699 clk->core->notifier_count++;
3702 clk_prepare_unlock();
3706 EXPORT_SYMBOL_GPL(clk_notifier_register);
3709 * clk_notifier_unregister - remove a clk rate change notifier
3710 * @clk: struct clk *
3711 * @nb: struct notifier_block * with callback info
3713 * Request no further notification for changes to 'clk' and frees memory
3714 * allocated in clk_notifier_register.
3716 * Returns -EINVAL if called with null arguments; otherwise, passes
3717 * along the return value of srcu_notifier_chain_unregister().
3719 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3721 struct clk_notifier *cn = NULL;
3729 list_for_each_entry(cn, &clk_notifier_list, node)
3733 if (cn->clk == clk) {
3734 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3736 clk->core->notifier_count--;
3738 /* XXX the notifier code should handle this better */
3739 if (!cn->notifier_head.head) {
3740 srcu_cleanup_notifier_head(&cn->notifier_head);
3741 list_del(&cn->node);
3749 clk_prepare_unlock();
3753 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3757 * struct of_clk_provider - Clock provider registration structure
3758 * @link: Entry in global list of clock providers
3759 * @node: Pointer to device tree node of clock provider
3760 * @get: Get clock callback. Returns NULL or a struct clk for the
3761 * given clock specifier
3762 * @data: context pointer to be passed into @get callback
3764 struct of_clk_provider {
3765 struct list_head link;
3767 struct device_node *node;
3768 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3769 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3773 static const struct of_device_id __clk_of_table_sentinel
3774 __used __section(__clk_of_table_end);
3776 static LIST_HEAD(of_clk_providers);
3777 static DEFINE_MUTEX(of_clk_mutex);
3779 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3784 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3786 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3790 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3792 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3794 struct clk_onecell_data *clk_data = data;
3795 unsigned int idx = clkspec->args[0];
3797 if (idx >= clk_data->clk_num) {
3798 pr_err("%s: invalid clock index %u\n", __func__, idx);
3799 return ERR_PTR(-EINVAL);
3802 return clk_data->clks[idx];
3804 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3807 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3809 struct clk_hw_onecell_data *hw_data = data;
3810 unsigned int idx = clkspec->args[0];
3812 if (idx >= hw_data->num) {
3813 pr_err("%s: invalid index %u\n", __func__, idx);
3814 return ERR_PTR(-EINVAL);
3817 return hw_data->hws[idx];
3819 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3822 * of_clk_add_provider() - Register a clock provider for a node
3823 * @np: Device node pointer associated with clock provider
3824 * @clk_src_get: callback for decoding clock
3825 * @data: context pointer for @clk_src_get callback.
3827 int of_clk_add_provider(struct device_node *np,
3828 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3832 struct of_clk_provider *cp;
3835 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3839 cp->node = of_node_get(np);
3841 cp->get = clk_src_get;
3843 mutex_lock(&of_clk_mutex);
3844 list_add(&cp->link, &of_clk_providers);
3845 mutex_unlock(&of_clk_mutex);
3846 pr_debug("Added clock from %pOF\n", np);
3848 ret = of_clk_set_defaults(np, true);
3850 of_clk_del_provider(np);
3854 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3857 * of_clk_add_hw_provider() - Register a clock provider for a node
3858 * @np: Device node pointer associated with clock provider
3859 * @get: callback for decoding clk_hw
3860 * @data: context pointer for @get callback.
3862 int of_clk_add_hw_provider(struct device_node *np,
3863 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3867 struct of_clk_provider *cp;
3870 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3874 cp->node = of_node_get(np);
3878 mutex_lock(&of_clk_mutex);
3879 list_add(&cp->link, &of_clk_providers);
3880 mutex_unlock(&of_clk_mutex);
3881 pr_debug("Added clk_hw provider from %pOF\n", np);
3883 ret = of_clk_set_defaults(np, true);
3885 of_clk_del_provider(np);
3889 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3891 static void devm_of_clk_release_provider(struct device *dev, void *res)
3893 of_clk_del_provider(*(struct device_node **)res);
3896 int devm_of_clk_add_hw_provider(struct device *dev,
3897 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3901 struct device_node **ptr, *np;
3904 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3910 ret = of_clk_add_hw_provider(np, get, data);
3913 devres_add(dev, ptr);
3920 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3923 * of_clk_del_provider() - Remove a previously registered clock provider
3924 * @np: Device node pointer associated with clock provider
3926 void of_clk_del_provider(struct device_node *np)
3928 struct of_clk_provider *cp;
3930 mutex_lock(&of_clk_mutex);
3931 list_for_each_entry(cp, &of_clk_providers, link) {
3932 if (cp->node == np) {
3933 list_del(&cp->link);
3934 of_node_put(cp->node);
3939 mutex_unlock(&of_clk_mutex);
3941 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3943 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3945 struct device_node **np = res;
3947 if (WARN_ON(!np || !*np))
3953 void devm_of_clk_del_provider(struct device *dev)
3957 ret = devres_release(dev, devm_of_clk_release_provider,
3958 devm_clk_provider_match, dev->of_node);
3962 EXPORT_SYMBOL(devm_of_clk_del_provider);
3964 static struct clk_hw *
3965 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3966 struct of_phandle_args *clkspec)
3970 if (provider->get_hw)
3971 return provider->get_hw(clkspec, provider->data);
3973 clk = provider->get(clkspec, provider->data);
3975 return ERR_CAST(clk);
3976 return __clk_get_hw(clk);
3979 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3980 const char *dev_id, const char *con_id)
3982 struct of_clk_provider *provider;
3983 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3987 return ERR_PTR(-EINVAL);
3989 /* Check if we have such a provider in our array */
3990 mutex_lock(&of_clk_mutex);
3991 list_for_each_entry(provider, &of_clk_providers, link) {
3992 if (provider->node == clkspec->np) {
3993 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3994 clk = __clk_create_clk(hw, dev_id, con_id);
3998 if (!__clk_get(clk)) {
3999 __clk_free_clk(clk);
4000 clk = ERR_PTR(-ENOENT);
4006 mutex_unlock(&of_clk_mutex);
4012 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4013 * @clkspec: pointer to a clock specifier data structure
4015 * This function looks up a struct clk from the registered list of clock
4016 * providers, an input is a clock specifier data structure as returned
4017 * from the of_parse_phandle_with_args() function call.
4019 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4021 return __of_clk_get_from_provider(clkspec, NULL, __func__);
4023 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4026 * of_clk_get_parent_count() - Count the number of clocks a device node has
4027 * @np: device node to count
4029 * Returns: The number of clocks that are possible parents of this node
4031 unsigned int of_clk_get_parent_count(struct device_node *np)
4035 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4041 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4043 const char *of_clk_get_parent_name(struct device_node *np, int index)
4045 struct of_phandle_args clkspec;
4046 struct property *prop;
4047 const char *clk_name;
4054 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4059 index = clkspec.args_count ? clkspec.args[0] : 0;
4062 /* if there is an indices property, use it to transfer the index
4063 * specified into an array offset for the clock-output-names property.
4065 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4072 /* We went off the end of 'clock-indices' without finding it */
4076 if (of_property_read_string_index(clkspec.np, "clock-output-names",
4080 * Best effort to get the name if the clock has been
4081 * registered with the framework. If the clock isn't
4082 * registered, we return the node name as the name of
4083 * the clock as long as #clock-cells = 0.
4085 clk = of_clk_get_from_provider(&clkspec);
4087 if (clkspec.args_count == 0)
4088 clk_name = clkspec.np->name;
4092 clk_name = __clk_get_name(clk);
4098 of_node_put(clkspec.np);
4101 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4104 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4106 * @np: Device node pointer associated with clock provider
4107 * @parents: pointer to char array that hold the parents' names
4108 * @size: size of the @parents array
4110 * Return: number of parents for the clock node.
4112 int of_clk_parent_fill(struct device_node *np, const char **parents,
4117 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4122 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4124 struct clock_provider {
4125 void (*clk_init_cb)(struct device_node *);
4126 struct device_node *np;
4127 struct list_head node;
4131 * This function looks for a parent clock. If there is one, then it
4132 * checks that the provider for this parent clock was initialized, in
4133 * this case the parent clock will be ready.
4135 static int parent_ready(struct device_node *np)
4140 struct clk *clk = of_clk_get(np, i);
4142 /* this parent is ready we can check the next one */
4149 /* at least one parent is not ready, we exit now */
4150 if (PTR_ERR(clk) == -EPROBE_DEFER)
4154 * Here we make assumption that the device tree is
4155 * written correctly. So an error means that there is
4156 * no more parent. As we didn't exit yet, then the
4157 * previous parent are ready. If there is no clock
4158 * parent, no need to wait for them, then we can
4159 * consider their absence as being ready
4166 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4167 * @np: Device node pointer associated with clock provider
4168 * @index: clock index
4169 * @flags: pointer to top-level framework flags
4171 * Detects if the clock-critical property exists and, if so, sets the
4172 * corresponding CLK_IS_CRITICAL flag.
4174 * Do not use this function. It exists only for legacy Device Tree
4175 * bindings, such as the one-clock-per-node style that are outdated.
4176 * Those bindings typically put all clock data into .dts and the Linux
4177 * driver has no clock data, thus making it impossible to set this flag
4178 * correctly from the driver. Only those drivers may call
4179 * of_clk_detect_critical from their setup functions.
4181 * Return: error code or zero on success
4183 int of_clk_detect_critical(struct device_node *np,
4184 int index, unsigned long *flags)
4186 struct property *prop;
4193 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4195 *flags |= CLK_IS_CRITICAL;
4201 * of_clk_init() - Scan and init clock providers from the DT
4202 * @matches: array of compatible values and init functions for providers.
4204 * This function scans the device tree for matching clock providers
4205 * and calls their initialization functions. It also does it by trying
4206 * to follow the dependencies.
4208 void __init of_clk_init(const struct of_device_id *matches)
4210 const struct of_device_id *match;
4211 struct device_node *np;
4212 struct clock_provider *clk_provider, *next;
4215 LIST_HEAD(clk_provider_list);
4218 matches = &__clk_of_table;
4220 /* First prepare the list of the clocks providers */
4221 for_each_matching_node_and_match(np, matches, &match) {
4222 struct clock_provider *parent;
4224 if (!of_device_is_available(np))
4227 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4229 list_for_each_entry_safe(clk_provider, next,
4230 &clk_provider_list, node) {
4231 list_del(&clk_provider->node);
4232 of_node_put(clk_provider->np);
4233 kfree(clk_provider);
4239 parent->clk_init_cb = match->data;
4240 parent->np = of_node_get(np);
4241 list_add_tail(&parent->node, &clk_provider_list);
4244 while (!list_empty(&clk_provider_list)) {
4245 is_init_done = false;
4246 list_for_each_entry_safe(clk_provider, next,
4247 &clk_provider_list, node) {
4248 if (force || parent_ready(clk_provider->np)) {
4250 /* Don't populate platform devices */
4251 of_node_set_flag(clk_provider->np,
4254 clk_provider->clk_init_cb(clk_provider->np);
4255 of_clk_set_defaults(clk_provider->np, true);
4257 list_del(&clk_provider->node);
4258 of_node_put(clk_provider->np);
4259 kfree(clk_provider);
4260 is_init_done = true;
4265 * We didn't manage to initialize any of the
4266 * remaining providers during the last loop, so now we
4267 * initialize all the remaining ones unconditionally
4268 * in case the clock parent was not mandatory