1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/sched.h>
23 #include <linux/clkdev.h>
27 static DEFINE_SPINLOCK(enable_lock);
28 static DEFINE_MUTEX(prepare_lock);
30 static struct task_struct *prepare_owner;
31 static struct task_struct *enable_owner;
33 static int prepare_refcnt;
34 static int enable_refcnt;
36 static HLIST_HEAD(clk_root_list);
37 static HLIST_HEAD(clk_orphan_list);
38 static LIST_HEAD(clk_notifier_list);
40 /*** private data structures ***/
42 struct clk_parent_map {
43 const struct clk_hw *hw;
44 struct clk_core *core;
52 const struct clk_ops *ops;
56 struct device_node *of_node;
57 struct clk_core *parent;
58 struct clk_parent_map *parents;
62 unsigned long req_rate;
63 unsigned long new_rate;
64 struct clk_core *new_parent;
65 struct clk_core *new_child;
69 unsigned int enable_count;
70 unsigned int prepare_count;
71 unsigned int protect_count;
72 unsigned long min_rate;
73 unsigned long max_rate;
74 unsigned long accuracy;
77 struct hlist_head children;
78 struct hlist_node child_node;
79 struct hlist_head clks;
80 unsigned int notifier_count;
81 #ifdef CONFIG_DEBUG_FS
82 struct dentry *dentry;
83 struct hlist_node debug_node;
88 #define CREATE_TRACE_POINTS
89 #include <trace/events/clk.h>
92 struct clk_core *core;
96 unsigned long min_rate;
97 unsigned long max_rate;
98 unsigned int exclusive_count;
99 struct hlist_node clks_node;
103 static int clk_pm_runtime_get(struct clk_core *core)
107 if (!core->rpm_enabled)
110 ret = pm_runtime_get_sync(core->dev);
111 return ret < 0 ? ret : 0;
114 static void clk_pm_runtime_put(struct clk_core *core)
116 if (!core->rpm_enabled)
119 pm_runtime_put_sync(core->dev);
123 static void clk_prepare_lock(void)
125 if (!mutex_trylock(&prepare_lock)) {
126 if (prepare_owner == current) {
130 mutex_lock(&prepare_lock);
132 WARN_ON_ONCE(prepare_owner != NULL);
133 WARN_ON_ONCE(prepare_refcnt != 0);
134 prepare_owner = current;
138 static void clk_prepare_unlock(void)
140 WARN_ON_ONCE(prepare_owner != current);
141 WARN_ON_ONCE(prepare_refcnt == 0);
143 if (--prepare_refcnt)
145 prepare_owner = NULL;
146 mutex_unlock(&prepare_lock);
149 static unsigned long clk_enable_lock(void)
150 __acquires(enable_lock)
155 * On UP systems, spin_trylock_irqsave() always returns true, even if
156 * we already hold the lock. So, in that case, we rely only on
157 * reference counting.
159 if (!IS_ENABLED(CONFIG_SMP) ||
160 !spin_trylock_irqsave(&enable_lock, flags)) {
161 if (enable_owner == current) {
163 __acquire(enable_lock);
164 if (!IS_ENABLED(CONFIG_SMP))
165 local_save_flags(flags);
168 spin_lock_irqsave(&enable_lock, flags);
170 WARN_ON_ONCE(enable_owner != NULL);
171 WARN_ON_ONCE(enable_refcnt != 0);
172 enable_owner = current;
177 static void clk_enable_unlock(unsigned long flags)
178 __releases(enable_lock)
180 WARN_ON_ONCE(enable_owner != current);
181 WARN_ON_ONCE(enable_refcnt == 0);
183 if (--enable_refcnt) {
184 __release(enable_lock);
188 spin_unlock_irqrestore(&enable_lock, flags);
191 static bool clk_core_rate_is_protected(struct clk_core *core)
193 return core->protect_count;
196 static bool clk_core_is_prepared(struct clk_core *core)
201 * .is_prepared is optional for clocks that can prepare
202 * fall back to software usage counter if it is missing
204 if (!core->ops->is_prepared)
205 return core->prepare_count;
207 if (!clk_pm_runtime_get(core)) {
208 ret = core->ops->is_prepared(core->hw);
209 clk_pm_runtime_put(core);
215 static bool clk_core_is_enabled(struct clk_core *core)
220 * .is_enabled is only mandatory for clocks that gate
221 * fall back to software usage counter if .is_enabled is missing
223 if (!core->ops->is_enabled)
224 return core->enable_count;
227 * Check if clock controller's device is runtime active before
228 * calling .is_enabled callback. If not, assume that clock is
229 * disabled, because we might be called from atomic context, from
230 * which pm_runtime_get() is not allowed.
231 * This function is called mainly from clk_disable_unused_subtree,
232 * which ensures proper runtime pm activation of controller before
233 * taking enable spinlock, but the below check is needed if one tries
234 * to call it from other places.
236 if (core->rpm_enabled) {
237 pm_runtime_get_noresume(core->dev);
238 if (!pm_runtime_active(core->dev)) {
244 ret = core->ops->is_enabled(core->hw);
246 if (core->rpm_enabled)
247 pm_runtime_put(core->dev);
252 /*** helper functions ***/
254 const char *__clk_get_name(const struct clk *clk)
256 return !clk ? NULL : clk->core->name;
258 EXPORT_SYMBOL_GPL(__clk_get_name);
260 const char *clk_hw_get_name(const struct clk_hw *hw)
262 return hw->core->name;
264 EXPORT_SYMBOL_GPL(clk_hw_get_name);
266 struct clk_hw *__clk_get_hw(struct clk *clk)
268 return !clk ? NULL : clk->core->hw;
270 EXPORT_SYMBOL_GPL(__clk_get_hw);
272 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
274 return hw->core->num_parents;
276 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
278 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
280 return hw->core->parent ? hw->core->parent->hw : NULL;
282 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
284 static struct clk_core *__clk_lookup_subtree(const char *name,
285 struct clk_core *core)
287 struct clk_core *child;
288 struct clk_core *ret;
290 if (!strcmp(core->name, name))
293 hlist_for_each_entry(child, &core->children, child_node) {
294 ret = __clk_lookup_subtree(name, child);
302 static struct clk_core *clk_core_lookup(const char *name)
304 struct clk_core *root_clk;
305 struct clk_core *ret;
310 /* search the 'proper' clk tree first */
311 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
312 ret = __clk_lookup_subtree(name, root_clk);
317 /* if not found, then search the orphan tree */
318 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
319 ret = __clk_lookup_subtree(name, root_clk);
328 * clk_core_get - Find the clk_core parent of a clk
329 * @core: clk to find parent of
330 * @p_index: parent index to search for
332 * This is the preferred method for clk providers to find the parent of a
333 * clk when that parent is external to the clk controller. The parent_names
334 * array is indexed and treated as a local name matching a string in the device
335 * node's 'clock-names' property or as the 'con_id' matching the device's
336 * dev_name() in a clk_lookup. This allows clk providers to use their own
337 * namespace instead of looking for a globally unique parent string.
339 * For example the following DT snippet would allow a clock registered by the
340 * clock-controller@c001 that has a clk_init_data::parent_data array
341 * with 'xtal' in the 'name' member to find the clock provided by the
342 * clock-controller@f00abcd without needing to get the globally unique name of
345 * parent: clock-controller@f00abcd {
346 * reg = <0xf00abcd 0xabcd>;
347 * #clock-cells = <0>;
350 * clock-controller@c001 {
351 * reg = <0xc001 0xf00d>;
352 * clocks = <&parent>;
353 * clock-names = "xtal";
354 * #clock-cells = <1>;
357 * Returns: -ENOENT when the provider can't be found or the clk doesn't
358 * exist in the provider. -EINVAL when the name can't be found. NULL when the
359 * provider knows about the clk but it isn't provided on this system.
360 * A valid clk_core pointer when the clk can be found in the provider.
362 static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
364 const char *name = core->parents[p_index].fw_name;
365 int index = core->parents[p_index].index;
366 struct clk_hw *hw = ERR_PTR(-ENOENT);
367 struct device *dev = core->dev;
368 const char *dev_id = dev ? dev_name(dev) : NULL;
369 struct device_node *np = core->of_node;
371 if (np && (name || index >= 0))
372 hw = of_clk_get_hw(np, index, name);
375 * If the DT search above couldn't find the provider or the provider
376 * didn't know about this clk, fallback to looking up via clkdev based
379 if (PTR_ERR(hw) == -ENOENT && name)
380 hw = clk_find_hw(dev_id, name);
388 static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
390 struct clk_parent_map *entry = &core->parents[index];
391 struct clk_core *parent = ERR_PTR(-ENOENT);
394 parent = entry->hw->core;
396 * We have a direct reference but it isn't registered yet?
397 * Orphan it and let clk_reparent() update the orphan status
398 * when the parent is registered.
401 parent = ERR_PTR(-EPROBE_DEFER);
403 parent = clk_core_get(core, index);
404 if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT)
405 parent = clk_core_lookup(entry->name);
408 /* Only cache it if it's not an error */
410 entry->core = parent;
413 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
416 if (!core || index >= core->num_parents || !core->parents)
419 if (!core->parents[index].core)
420 clk_core_fill_parent_index(core, index);
422 return core->parents[index].core;
426 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
428 struct clk_core *parent;
430 parent = clk_core_get_parent_by_index(hw->core, index);
432 return !parent ? NULL : parent->hw;
434 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
436 unsigned int __clk_get_enable_count(struct clk *clk)
438 return !clk ? 0 : clk->core->enable_count;
441 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
446 if (!core->num_parents || core->parent)
450 * Clk must have a parent because num_parents > 0 but the parent isn't
451 * known yet. Best to return 0 as the rate of this clk until we can
452 * properly recalc the rate based on the parent's rate.
457 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
459 return clk_core_get_rate_nolock(hw->core);
461 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
463 static unsigned long __clk_get_accuracy(struct clk_core *core)
468 return core->accuracy;
471 unsigned long __clk_get_flags(struct clk *clk)
473 return !clk ? 0 : clk->core->flags;
475 EXPORT_SYMBOL_GPL(__clk_get_flags);
477 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
479 return hw->core->flags;
481 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
483 bool clk_hw_is_prepared(const struct clk_hw *hw)
485 return clk_core_is_prepared(hw->core);
487 EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
489 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
491 return clk_core_rate_is_protected(hw->core);
493 EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
495 bool clk_hw_is_enabled(const struct clk_hw *hw)
497 return clk_core_is_enabled(hw->core);
499 EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
501 bool __clk_is_enabled(struct clk *clk)
506 return clk_core_is_enabled(clk->core);
508 EXPORT_SYMBOL_GPL(__clk_is_enabled);
510 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
511 unsigned long best, unsigned long flags)
513 if (flags & CLK_MUX_ROUND_CLOSEST)
514 return abs(now - rate) < abs(best - rate);
516 return now <= rate && now > best;
519 int clk_mux_determine_rate_flags(struct clk_hw *hw,
520 struct clk_rate_request *req,
523 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
524 int i, num_parents, ret;
525 unsigned long best = 0;
526 struct clk_rate_request parent_req = *req;
528 /* if NO_REPARENT flag set, pass through to current parent */
529 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
530 parent = core->parent;
531 if (core->flags & CLK_SET_RATE_PARENT) {
532 ret = __clk_determine_rate(parent ? parent->hw : NULL,
537 best = parent_req.rate;
539 best = clk_core_get_rate_nolock(parent);
541 best = clk_core_get_rate_nolock(core);
547 /* find the parent that can provide the fastest rate <= rate */
548 num_parents = core->num_parents;
549 for (i = 0; i < num_parents; i++) {
550 parent = clk_core_get_parent_by_index(core, i);
554 if (core->flags & CLK_SET_RATE_PARENT) {
556 ret = __clk_determine_rate(parent->hw, &parent_req);
560 parent_req.rate = clk_core_get_rate_nolock(parent);
563 if (mux_is_better_rate(req->rate, parent_req.rate,
565 best_parent = parent;
566 best = parent_req.rate;
575 req->best_parent_hw = best_parent->hw;
576 req->best_parent_rate = best;
581 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
583 struct clk *__clk_lookup(const char *name)
585 struct clk_core *core = clk_core_lookup(name);
587 return !core ? NULL : core->hw->clk;
590 static void clk_core_get_boundaries(struct clk_core *core,
591 unsigned long *min_rate,
592 unsigned long *max_rate)
594 struct clk *clk_user;
596 lockdep_assert_held(&prepare_lock);
598 *min_rate = core->min_rate;
599 *max_rate = core->max_rate;
601 hlist_for_each_entry(clk_user, &core->clks, clks_node)
602 *min_rate = max(*min_rate, clk_user->min_rate);
604 hlist_for_each_entry(clk_user, &core->clks, clks_node)
605 *max_rate = min(*max_rate, clk_user->max_rate);
608 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
609 unsigned long max_rate)
611 hw->core->min_rate = min_rate;
612 hw->core->max_rate = max_rate;
614 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
617 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
618 * @hw: mux type clk to determine rate on
619 * @req: rate request, also used to return preferred parent and frequencies
621 * Helper for finding best parent to provide a given frequency. This can be used
622 * directly as a determine_rate callback (e.g. for a mux), or from a more
623 * complex clock that may combine a mux with other operations.
625 * Returns: 0 on success, -EERROR value on error
627 int __clk_mux_determine_rate(struct clk_hw *hw,
628 struct clk_rate_request *req)
630 return clk_mux_determine_rate_flags(hw, req, 0);
632 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
634 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
635 struct clk_rate_request *req)
637 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
639 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
643 static void clk_core_rate_unprotect(struct clk_core *core)
645 lockdep_assert_held(&prepare_lock);
650 if (WARN(core->protect_count == 0,
651 "%s already unprotected\n", core->name))
654 if (--core->protect_count > 0)
657 clk_core_rate_unprotect(core->parent);
660 static int clk_core_rate_nuke_protect(struct clk_core *core)
664 lockdep_assert_held(&prepare_lock);
669 if (core->protect_count == 0)
672 ret = core->protect_count;
673 core->protect_count = 1;
674 clk_core_rate_unprotect(core);
680 * clk_rate_exclusive_put - release exclusivity over clock rate control
681 * @clk: the clk over which the exclusivity is released
683 * clk_rate_exclusive_put() completes a critical section during which a clock
684 * consumer cannot tolerate any other consumer making any operation on the
685 * clock which could result in a rate change or rate glitch. Exclusive clocks
686 * cannot have their rate changed, either directly or indirectly due to changes
687 * further up the parent chain of clocks. As a result, clocks up parent chain
688 * also get under exclusive control of the calling consumer.
690 * If exlusivity is claimed more than once on clock, even by the same consumer,
691 * the rate effectively gets locked as exclusivity can't be preempted.
693 * Calls to clk_rate_exclusive_put() must be balanced with calls to
694 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
697 void clk_rate_exclusive_put(struct clk *clk)
705 * if there is something wrong with this consumer protect count, stop
706 * here before messing with the provider
708 if (WARN_ON(clk->exclusive_count <= 0))
711 clk_core_rate_unprotect(clk->core);
712 clk->exclusive_count--;
714 clk_prepare_unlock();
716 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
718 static void clk_core_rate_protect(struct clk_core *core)
720 lockdep_assert_held(&prepare_lock);
725 if (core->protect_count == 0)
726 clk_core_rate_protect(core->parent);
728 core->protect_count++;
731 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
733 lockdep_assert_held(&prepare_lock);
741 clk_core_rate_protect(core);
742 core->protect_count = count;
746 * clk_rate_exclusive_get - get exclusivity over the clk rate control
747 * @clk: the clk over which the exclusity of rate control is requested
749 * clk_rate_exlusive_get() begins a critical section during which a clock
750 * consumer cannot tolerate any other consumer making any operation on the
751 * clock which could result in a rate change or rate glitch. Exclusive clocks
752 * cannot have their rate changed, either directly or indirectly due to changes
753 * further up the parent chain of clocks. As a result, clocks up parent chain
754 * also get under exclusive control of the calling consumer.
756 * If exlusivity is claimed more than once on clock, even by the same consumer,
757 * the rate effectively gets locked as exclusivity can't be preempted.
759 * Calls to clk_rate_exclusive_get() should be balanced with calls to
760 * clk_rate_exclusive_put(). Calls to this function may sleep.
761 * Returns 0 on success, -EERROR otherwise
763 int clk_rate_exclusive_get(struct clk *clk)
769 clk_core_rate_protect(clk->core);
770 clk->exclusive_count++;
771 clk_prepare_unlock();
775 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
777 static void clk_core_unprepare(struct clk_core *core)
779 lockdep_assert_held(&prepare_lock);
784 if (WARN(core->prepare_count == 0,
785 "%s already unprepared\n", core->name))
788 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
789 "Unpreparing critical %s\n", core->name))
792 if (core->flags & CLK_SET_RATE_GATE)
793 clk_core_rate_unprotect(core);
795 if (--core->prepare_count > 0)
798 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
800 trace_clk_unprepare(core);
802 if (core->ops->unprepare)
803 core->ops->unprepare(core->hw);
805 clk_pm_runtime_put(core);
807 trace_clk_unprepare_complete(core);
808 clk_core_unprepare(core->parent);
811 static void clk_core_unprepare_lock(struct clk_core *core)
814 clk_core_unprepare(core);
815 clk_prepare_unlock();
819 * clk_unprepare - undo preparation of a clock source
820 * @clk: the clk being unprepared
822 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
823 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
824 * if the operation may sleep. One example is a clk which is accessed over
825 * I2c. In the complex case a clk gate operation may require a fast and a slow
826 * part. It is this reason that clk_unprepare and clk_disable are not mutually
827 * exclusive. In fact clk_disable must be called before clk_unprepare.
829 void clk_unprepare(struct clk *clk)
831 if (IS_ERR_OR_NULL(clk))
834 clk_core_unprepare_lock(clk->core);
836 EXPORT_SYMBOL_GPL(clk_unprepare);
838 static int clk_core_prepare(struct clk_core *core)
842 lockdep_assert_held(&prepare_lock);
847 if (core->prepare_count == 0) {
848 ret = clk_pm_runtime_get(core);
852 ret = clk_core_prepare(core->parent);
856 trace_clk_prepare(core);
858 if (core->ops->prepare)
859 ret = core->ops->prepare(core->hw);
861 trace_clk_prepare_complete(core);
867 core->prepare_count++;
870 * CLK_SET_RATE_GATE is a special case of clock protection
871 * Instead of a consumer claiming exclusive rate control, it is
872 * actually the provider which prevents any consumer from making any
873 * operation which could result in a rate change or rate glitch while
874 * the clock is prepared.
876 if (core->flags & CLK_SET_RATE_GATE)
877 clk_core_rate_protect(core);
881 clk_core_unprepare(core->parent);
883 clk_pm_runtime_put(core);
887 static int clk_core_prepare_lock(struct clk_core *core)
892 ret = clk_core_prepare(core);
893 clk_prepare_unlock();
899 * clk_prepare - prepare a clock source
900 * @clk: the clk being prepared
902 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
903 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
904 * operation may sleep. One example is a clk which is accessed over I2c. In
905 * the complex case a clk ungate operation may require a fast and a slow part.
906 * It is this reason that clk_prepare and clk_enable are not mutually
907 * exclusive. In fact clk_prepare must be called before clk_enable.
908 * Returns 0 on success, -EERROR otherwise.
910 int clk_prepare(struct clk *clk)
915 return clk_core_prepare_lock(clk->core);
917 EXPORT_SYMBOL_GPL(clk_prepare);
919 static void clk_core_disable(struct clk_core *core)
921 lockdep_assert_held(&enable_lock);
926 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
929 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
930 "Disabling critical %s\n", core->name))
933 if (--core->enable_count > 0)
936 trace_clk_disable_rcuidle(core);
938 if (core->ops->disable)
939 core->ops->disable(core->hw);
941 trace_clk_disable_complete_rcuidle(core);
943 clk_core_disable(core->parent);
946 static void clk_core_disable_lock(struct clk_core *core)
950 flags = clk_enable_lock();
951 clk_core_disable(core);
952 clk_enable_unlock(flags);
956 * clk_disable - gate a clock
957 * @clk: the clk being gated
959 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
960 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
961 * clk if the operation is fast and will never sleep. One example is a
962 * SoC-internal clk which is controlled via simple register writes. In the
963 * complex case a clk gate operation may require a fast and a slow part. It is
964 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
965 * In fact clk_disable must be called before clk_unprepare.
967 void clk_disable(struct clk *clk)
969 if (IS_ERR_OR_NULL(clk))
972 clk_core_disable_lock(clk->core);
974 EXPORT_SYMBOL_GPL(clk_disable);
976 static int clk_core_enable(struct clk_core *core)
980 lockdep_assert_held(&enable_lock);
985 if (WARN(core->prepare_count == 0,
986 "Enabling unprepared %s\n", core->name))
989 if (core->enable_count == 0) {
990 ret = clk_core_enable(core->parent);
995 trace_clk_enable_rcuidle(core);
997 if (core->ops->enable)
998 ret = core->ops->enable(core->hw);
1000 trace_clk_enable_complete_rcuidle(core);
1003 clk_core_disable(core->parent);
1008 core->enable_count++;
1012 static int clk_core_enable_lock(struct clk_core *core)
1014 unsigned long flags;
1017 flags = clk_enable_lock();
1018 ret = clk_core_enable(core);
1019 clk_enable_unlock(flags);
1025 * clk_gate_restore_context - restore context for poweroff
1026 * @hw: the clk_hw pointer of clock whose state is to be restored
1028 * The clock gate restore context function enables or disables
1029 * the gate clocks based on the enable_count. This is done in cases
1030 * where the clock context is lost and based on the enable_count
1031 * the clock either needs to be enabled/disabled. This
1032 * helps restore the state of gate clocks.
1034 void clk_gate_restore_context(struct clk_hw *hw)
1036 struct clk_core *core = hw->core;
1038 if (core->enable_count)
1039 core->ops->enable(hw);
1041 core->ops->disable(hw);
1043 EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1045 static int clk_core_save_context(struct clk_core *core)
1047 struct clk_core *child;
1050 hlist_for_each_entry(child, &core->children, child_node) {
1051 ret = clk_core_save_context(child);
1056 if (core->ops && core->ops->save_context)
1057 ret = core->ops->save_context(core->hw);
1062 static void clk_core_restore_context(struct clk_core *core)
1064 struct clk_core *child;
1066 if (core->ops && core->ops->restore_context)
1067 core->ops->restore_context(core->hw);
1069 hlist_for_each_entry(child, &core->children, child_node)
1070 clk_core_restore_context(child);
1074 * clk_save_context - save clock context for poweroff
1076 * Saves the context of the clock register for powerstates in which the
1077 * contents of the registers will be lost. Occurs deep within the suspend
1078 * code. Returns 0 on success.
1080 int clk_save_context(void)
1082 struct clk_core *clk;
1085 hlist_for_each_entry(clk, &clk_root_list, child_node) {
1086 ret = clk_core_save_context(clk);
1091 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
1092 ret = clk_core_save_context(clk);
1099 EXPORT_SYMBOL_GPL(clk_save_context);
1102 * clk_restore_context - restore clock context after poweroff
1104 * Restore the saved clock context upon resume.
1107 void clk_restore_context(void)
1109 struct clk_core *core;
1111 hlist_for_each_entry(core, &clk_root_list, child_node)
1112 clk_core_restore_context(core);
1114 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1115 clk_core_restore_context(core);
1117 EXPORT_SYMBOL_GPL(clk_restore_context);
1120 * clk_enable - ungate a clock
1121 * @clk: the clk being ungated
1123 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1124 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1125 * if the operation will never sleep. One example is a SoC-internal clk which
1126 * is controlled via simple register writes. In the complex case a clk ungate
1127 * operation may require a fast and a slow part. It is this reason that
1128 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1129 * must be called before clk_enable. Returns 0 on success, -EERROR
1132 int clk_enable(struct clk *clk)
1137 return clk_core_enable_lock(clk->core);
1139 EXPORT_SYMBOL_GPL(clk_enable);
1141 static int clk_core_prepare_enable(struct clk_core *core)
1145 ret = clk_core_prepare_lock(core);
1149 ret = clk_core_enable_lock(core);
1151 clk_core_unprepare_lock(core);
1156 static void clk_core_disable_unprepare(struct clk_core *core)
1158 clk_core_disable_lock(core);
1159 clk_core_unprepare_lock(core);
1162 static void clk_unprepare_unused_subtree(struct clk_core *core)
1164 struct clk_core *child;
1166 lockdep_assert_held(&prepare_lock);
1168 hlist_for_each_entry(child, &core->children, child_node)
1169 clk_unprepare_unused_subtree(child);
1171 if (core->prepare_count)
1174 if (core->flags & CLK_IGNORE_UNUSED)
1177 if (clk_pm_runtime_get(core))
1180 if (clk_core_is_prepared(core)) {
1181 trace_clk_unprepare(core);
1182 if (core->ops->unprepare_unused)
1183 core->ops->unprepare_unused(core->hw);
1184 else if (core->ops->unprepare)
1185 core->ops->unprepare(core->hw);
1186 trace_clk_unprepare_complete(core);
1189 clk_pm_runtime_put(core);
1192 static void clk_disable_unused_subtree(struct clk_core *core)
1194 struct clk_core *child;
1195 unsigned long flags;
1197 lockdep_assert_held(&prepare_lock);
1199 hlist_for_each_entry(child, &core->children, child_node)
1200 clk_disable_unused_subtree(child);
1202 if (core->flags & CLK_OPS_PARENT_ENABLE)
1203 clk_core_prepare_enable(core->parent);
1205 if (clk_pm_runtime_get(core))
1208 flags = clk_enable_lock();
1210 if (core->enable_count)
1213 if (core->flags & CLK_IGNORE_UNUSED)
1217 * some gate clocks have special needs during the disable-unused
1218 * sequence. call .disable_unused if available, otherwise fall
1221 if (clk_core_is_enabled(core)) {
1222 trace_clk_disable(core);
1223 if (core->ops->disable_unused)
1224 core->ops->disable_unused(core->hw);
1225 else if (core->ops->disable)
1226 core->ops->disable(core->hw);
1227 trace_clk_disable_complete(core);
1231 clk_enable_unlock(flags);
1232 clk_pm_runtime_put(core);
1234 if (core->flags & CLK_OPS_PARENT_ENABLE)
1235 clk_core_disable_unprepare(core->parent);
1238 static bool clk_ignore_unused;
1239 static int __init clk_ignore_unused_setup(char *__unused)
1241 clk_ignore_unused = true;
1244 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1246 static int clk_disable_unused(void)
1248 struct clk_core *core;
1250 if (clk_ignore_unused) {
1251 pr_warn("clk: Not disabling unused clocks\n");
1257 hlist_for_each_entry(core, &clk_root_list, child_node)
1258 clk_disable_unused_subtree(core);
1260 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1261 clk_disable_unused_subtree(core);
1263 hlist_for_each_entry(core, &clk_root_list, child_node)
1264 clk_unprepare_unused_subtree(core);
1266 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1267 clk_unprepare_unused_subtree(core);
1269 clk_prepare_unlock();
1273 late_initcall_sync(clk_disable_unused);
1275 static int clk_core_determine_round_nolock(struct clk_core *core,
1276 struct clk_rate_request *req)
1280 lockdep_assert_held(&prepare_lock);
1286 * At this point, core protection will be disabled if
1287 * - if the provider is not protected at all
1288 * - if the calling consumer is the only one which has exclusivity
1291 if (clk_core_rate_is_protected(core)) {
1292 req->rate = core->rate;
1293 } else if (core->ops->determine_rate) {
1294 return core->ops->determine_rate(core->hw, req);
1295 } else if (core->ops->round_rate) {
1296 rate = core->ops->round_rate(core->hw, req->rate,
1297 &req->best_parent_rate);
1309 static void clk_core_init_rate_req(struct clk_core * const core,
1310 struct clk_rate_request *req)
1312 struct clk_core *parent;
1314 if (WARN_ON(!core || !req))
1317 parent = core->parent;
1319 req->best_parent_hw = parent->hw;
1320 req->best_parent_rate = parent->rate;
1322 req->best_parent_hw = NULL;
1323 req->best_parent_rate = 0;
1327 static bool clk_core_can_round(struct clk_core * const core)
1329 return core->ops->determine_rate || core->ops->round_rate;
1332 static int clk_core_round_rate_nolock(struct clk_core *core,
1333 struct clk_rate_request *req)
1335 lockdep_assert_held(&prepare_lock);
1342 clk_core_init_rate_req(core, req);
1344 if (clk_core_can_round(core))
1345 return clk_core_determine_round_nolock(core, req);
1346 else if (core->flags & CLK_SET_RATE_PARENT)
1347 return clk_core_round_rate_nolock(core->parent, req);
1349 req->rate = core->rate;
1354 * __clk_determine_rate - get the closest rate actually supported by a clock
1355 * @hw: determine the rate of this clock
1356 * @req: target rate request
1358 * Useful for clk_ops such as .set_rate and .determine_rate.
1360 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1367 return clk_core_round_rate_nolock(hw->core, req);
1369 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1371 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1374 struct clk_rate_request req;
1376 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1379 ret = clk_core_round_rate_nolock(hw->core, &req);
1385 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1388 * clk_round_rate - round the given rate for a clk
1389 * @clk: the clk for which we are rounding a rate
1390 * @rate: the rate which is to be rounded
1392 * Takes in a rate as input and rounds it to a rate that the clk can actually
1393 * use which is then returned. If clk doesn't support round_rate operation
1394 * then the parent rate is returned.
1396 long clk_round_rate(struct clk *clk, unsigned long rate)
1398 struct clk_rate_request req;
1406 if (clk->exclusive_count)
1407 clk_core_rate_unprotect(clk->core);
1409 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1412 ret = clk_core_round_rate_nolock(clk->core, &req);
1414 if (clk->exclusive_count)
1415 clk_core_rate_protect(clk->core);
1417 clk_prepare_unlock();
1424 EXPORT_SYMBOL_GPL(clk_round_rate);
1427 * __clk_notify - call clk notifier chain
1428 * @core: clk that is changing rate
1429 * @msg: clk notifier type (see include/linux/clk.h)
1430 * @old_rate: old clk rate
1431 * @new_rate: new clk rate
1433 * Triggers a notifier call chain on the clk rate-change notification
1434 * for 'clk'. Passes a pointer to the struct clk and the previous
1435 * and current rates to the notifier callback. Intended to be called by
1436 * internal clock code only. Returns NOTIFY_DONE from the last driver
1437 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1438 * a driver returns that.
1440 static int __clk_notify(struct clk_core *core, unsigned long msg,
1441 unsigned long old_rate, unsigned long new_rate)
1443 struct clk_notifier *cn;
1444 struct clk_notifier_data cnd;
1445 int ret = NOTIFY_DONE;
1447 cnd.old_rate = old_rate;
1448 cnd.new_rate = new_rate;
1450 list_for_each_entry(cn, &clk_notifier_list, node) {
1451 if (cn->clk->core == core) {
1453 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1455 if (ret & NOTIFY_STOP_MASK)
1464 * __clk_recalc_accuracies
1465 * @core: first clk in the subtree
1467 * Walks the subtree of clks starting with clk and recalculates accuracies as
1468 * it goes. Note that if a clk does not implement the .recalc_accuracy
1469 * callback then it is assumed that the clock will take on the accuracy of its
1472 static void __clk_recalc_accuracies(struct clk_core *core)
1474 unsigned long parent_accuracy = 0;
1475 struct clk_core *child;
1477 lockdep_assert_held(&prepare_lock);
1480 parent_accuracy = core->parent->accuracy;
1482 if (core->ops->recalc_accuracy)
1483 core->accuracy = core->ops->recalc_accuracy(core->hw,
1486 core->accuracy = parent_accuracy;
1488 hlist_for_each_entry(child, &core->children, child_node)
1489 __clk_recalc_accuracies(child);
1492 static long clk_core_get_accuracy(struct clk_core *core)
1494 unsigned long accuracy;
1497 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1498 __clk_recalc_accuracies(core);
1500 accuracy = __clk_get_accuracy(core);
1501 clk_prepare_unlock();
1507 * clk_get_accuracy - return the accuracy of clk
1508 * @clk: the clk whose accuracy is being returned
1510 * Simply returns the cached accuracy of the clk, unless
1511 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1513 * If clk is NULL then returns 0.
1515 long clk_get_accuracy(struct clk *clk)
1520 return clk_core_get_accuracy(clk->core);
1522 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1524 static unsigned long clk_recalc(struct clk_core *core,
1525 unsigned long parent_rate)
1527 unsigned long rate = parent_rate;
1529 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1530 rate = core->ops->recalc_rate(core->hw, parent_rate);
1531 clk_pm_runtime_put(core);
1537 * __clk_recalc_rates
1538 * @core: first clk in the subtree
1539 * @msg: notification type (see include/linux/clk.h)
1541 * Walks the subtree of clks starting with clk and recalculates rates as it
1542 * goes. Note that if a clk does not implement the .recalc_rate callback then
1543 * it is assumed that the clock will take on the rate of its parent.
1545 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1548 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1550 unsigned long old_rate;
1551 unsigned long parent_rate = 0;
1552 struct clk_core *child;
1554 lockdep_assert_held(&prepare_lock);
1556 old_rate = core->rate;
1559 parent_rate = core->parent->rate;
1561 core->rate = clk_recalc(core, parent_rate);
1564 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1565 * & ABORT_RATE_CHANGE notifiers
1567 if (core->notifier_count && msg)
1568 __clk_notify(core, msg, old_rate, core->rate);
1570 hlist_for_each_entry(child, &core->children, child_node)
1571 __clk_recalc_rates(child, msg);
1574 static unsigned long clk_core_get_rate(struct clk_core *core)
1580 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1581 __clk_recalc_rates(core, 0);
1583 rate = clk_core_get_rate_nolock(core);
1584 clk_prepare_unlock();
1590 * clk_get_rate - return the rate of clk
1591 * @clk: the clk whose rate is being returned
1593 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1594 * is set, which means a recalc_rate will be issued.
1595 * If clk is NULL then returns 0.
1597 unsigned long clk_get_rate(struct clk *clk)
1602 return clk_core_get_rate(clk->core);
1604 EXPORT_SYMBOL_GPL(clk_get_rate);
1606 static int clk_fetch_parent_index(struct clk_core *core,
1607 struct clk_core *parent)
1614 for (i = 0; i < core->num_parents; i++) {
1615 /* Found it first try! */
1616 if (core->parents[i].core == parent)
1619 /* Something else is here, so keep looking */
1620 if (core->parents[i].core)
1623 /* Maybe core hasn't been cached but the hw is all we know? */
1624 if (core->parents[i].hw) {
1625 if (core->parents[i].hw == parent->hw)
1628 /* Didn't match, but we're expecting a clk_hw */
1632 /* Maybe it hasn't been cached (clk_set_parent() path) */
1633 if (parent == clk_core_get(core, i))
1636 /* Fallback to comparing globally unique names */
1637 if (!strcmp(parent->name, core->parents[i].name))
1641 if (i == core->num_parents)
1644 core->parents[i].core = parent;
1649 * Update the orphan status of @core and all its children.
1651 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1653 struct clk_core *child;
1655 core->orphan = is_orphan;
1657 hlist_for_each_entry(child, &core->children, child_node)
1658 clk_core_update_orphan_status(child, is_orphan);
1661 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1663 bool was_orphan = core->orphan;
1665 hlist_del(&core->child_node);
1668 bool becomes_orphan = new_parent->orphan;
1670 /* avoid duplicate POST_RATE_CHANGE notifications */
1671 if (new_parent->new_child == core)
1672 new_parent->new_child = NULL;
1674 hlist_add_head(&core->child_node, &new_parent->children);
1676 if (was_orphan != becomes_orphan)
1677 clk_core_update_orphan_status(core, becomes_orphan);
1679 hlist_add_head(&core->child_node, &clk_orphan_list);
1681 clk_core_update_orphan_status(core, true);
1684 core->parent = new_parent;
1687 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1688 struct clk_core *parent)
1690 unsigned long flags;
1691 struct clk_core *old_parent = core->parent;
1694 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1696 * 2. Migrate prepare state between parents and prevent race with
1699 * If the clock is not prepared, then a race with
1700 * clk_enable/disable() is impossible since we already have the
1701 * prepare lock (future calls to clk_enable() need to be preceded by
1704 * If the clock is prepared, migrate the prepared state to the new
1705 * parent and also protect against a race with clk_enable() by
1706 * forcing the clock and the new parent on. This ensures that all
1707 * future calls to clk_enable() are practically NOPs with respect to
1708 * hardware and software states.
1710 * See also: Comment for clk_set_parent() below.
1713 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1714 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1715 clk_core_prepare_enable(old_parent);
1716 clk_core_prepare_enable(parent);
1719 /* migrate prepare count if > 0 */
1720 if (core->prepare_count) {
1721 clk_core_prepare_enable(parent);
1722 clk_core_enable_lock(core);
1725 /* update the clk tree topology */
1726 flags = clk_enable_lock();
1727 clk_reparent(core, parent);
1728 clk_enable_unlock(flags);
1733 static void __clk_set_parent_after(struct clk_core *core,
1734 struct clk_core *parent,
1735 struct clk_core *old_parent)
1738 * Finish the migration of prepare state and undo the changes done
1739 * for preventing a race with clk_enable().
1741 if (core->prepare_count) {
1742 clk_core_disable_lock(core);
1743 clk_core_disable_unprepare(old_parent);
1746 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1747 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1748 clk_core_disable_unprepare(parent);
1749 clk_core_disable_unprepare(old_parent);
1753 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1756 unsigned long flags;
1758 struct clk_core *old_parent;
1760 old_parent = __clk_set_parent_before(core, parent);
1762 trace_clk_set_parent(core, parent);
1764 /* change clock input source */
1765 if (parent && core->ops->set_parent)
1766 ret = core->ops->set_parent(core->hw, p_index);
1768 trace_clk_set_parent_complete(core, parent);
1771 flags = clk_enable_lock();
1772 clk_reparent(core, old_parent);
1773 clk_enable_unlock(flags);
1774 __clk_set_parent_after(core, old_parent, parent);
1779 __clk_set_parent_after(core, parent, old_parent);
1785 * __clk_speculate_rates
1786 * @core: first clk in the subtree
1787 * @parent_rate: the "future" rate of clk's parent
1789 * Walks the subtree of clks starting with clk, speculating rates as it
1790 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1792 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1793 * pre-rate change notifications and returns early if no clks in the
1794 * subtree have subscribed to the notifications. Note that if a clk does not
1795 * implement the .recalc_rate callback then it is assumed that the clock will
1796 * take on the rate of its parent.
1798 static int __clk_speculate_rates(struct clk_core *core,
1799 unsigned long parent_rate)
1801 struct clk_core *child;
1802 unsigned long new_rate;
1803 int ret = NOTIFY_DONE;
1805 lockdep_assert_held(&prepare_lock);
1807 new_rate = clk_recalc(core, parent_rate);
1809 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1810 if (core->notifier_count)
1811 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1813 if (ret & NOTIFY_STOP_MASK) {
1814 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1815 __func__, core->name, ret);
1819 hlist_for_each_entry(child, &core->children, child_node) {
1820 ret = __clk_speculate_rates(child, new_rate);
1821 if (ret & NOTIFY_STOP_MASK)
1829 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1830 struct clk_core *new_parent, u8 p_index)
1832 struct clk_core *child;
1834 core->new_rate = new_rate;
1835 core->new_parent = new_parent;
1836 core->new_parent_index = p_index;
1837 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1838 core->new_child = NULL;
1839 if (new_parent && new_parent != core->parent)
1840 new_parent->new_child = core;
1842 hlist_for_each_entry(child, &core->children, child_node) {
1843 child->new_rate = clk_recalc(child, new_rate);
1844 clk_calc_subtree(child, child->new_rate, NULL, 0);
1849 * calculate the new rates returning the topmost clock that has to be
1852 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1855 struct clk_core *top = core;
1856 struct clk_core *old_parent, *parent;
1857 unsigned long best_parent_rate = 0;
1858 unsigned long new_rate;
1859 unsigned long min_rate;
1860 unsigned long max_rate;
1865 if (IS_ERR_OR_NULL(core))
1868 /* save parent rate, if it exists */
1869 parent = old_parent = core->parent;
1871 best_parent_rate = parent->rate;
1873 clk_core_get_boundaries(core, &min_rate, &max_rate);
1875 /* find the closest rate and parent clk/rate */
1876 if (clk_core_can_round(core)) {
1877 struct clk_rate_request req;
1880 req.min_rate = min_rate;
1881 req.max_rate = max_rate;
1883 clk_core_init_rate_req(core, &req);
1885 ret = clk_core_determine_round_nolock(core, &req);
1889 best_parent_rate = req.best_parent_rate;
1890 new_rate = req.rate;
1891 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1893 if (new_rate < min_rate || new_rate > max_rate)
1895 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1896 /* pass-through clock without adjustable parent */
1897 core->new_rate = core->rate;
1900 /* pass-through clock with adjustable parent */
1901 top = clk_calc_new_rates(parent, rate);
1902 new_rate = parent->new_rate;
1906 /* some clocks must be gated to change parent */
1907 if (parent != old_parent &&
1908 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1909 pr_debug("%s: %s not gated but wants to reparent\n",
1910 __func__, core->name);
1914 /* try finding the new parent index */
1915 if (parent && core->num_parents > 1) {
1916 p_index = clk_fetch_parent_index(core, parent);
1918 pr_debug("%s: clk %s can not be parent of clk %s\n",
1919 __func__, parent->name, core->name);
1924 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1925 best_parent_rate != parent->rate)
1926 top = clk_calc_new_rates(parent, best_parent_rate);
1929 clk_calc_subtree(core, new_rate, parent, p_index);
1935 * Notify about rate changes in a subtree. Always walk down the whole tree
1936 * so that in case of an error we can walk down the whole tree again and
1939 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1940 unsigned long event)
1942 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1943 int ret = NOTIFY_DONE;
1945 if (core->rate == core->new_rate)
1948 if (core->notifier_count) {
1949 ret = __clk_notify(core, event, core->rate, core->new_rate);
1950 if (ret & NOTIFY_STOP_MASK)
1954 hlist_for_each_entry(child, &core->children, child_node) {
1955 /* Skip children who will be reparented to another clock */
1956 if (child->new_parent && child->new_parent != core)
1958 tmp_clk = clk_propagate_rate_change(child, event);
1963 /* handle the new child who might not be in core->children yet */
1964 if (core->new_child) {
1965 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1974 * walk down a subtree and set the new rates notifying the rate
1977 static void clk_change_rate(struct clk_core *core)
1979 struct clk_core *child;
1980 struct hlist_node *tmp;
1981 unsigned long old_rate;
1982 unsigned long best_parent_rate = 0;
1983 bool skip_set_rate = false;
1984 struct clk_core *old_parent;
1985 struct clk_core *parent = NULL;
1987 old_rate = core->rate;
1989 if (core->new_parent) {
1990 parent = core->new_parent;
1991 best_parent_rate = core->new_parent->rate;
1992 } else if (core->parent) {
1993 parent = core->parent;
1994 best_parent_rate = core->parent->rate;
1997 if (clk_pm_runtime_get(core))
2000 if (core->flags & CLK_SET_RATE_UNGATE) {
2001 unsigned long flags;
2003 clk_core_prepare(core);
2004 flags = clk_enable_lock();
2005 clk_core_enable(core);
2006 clk_enable_unlock(flags);
2009 if (core->new_parent && core->new_parent != core->parent) {
2010 old_parent = __clk_set_parent_before(core, core->new_parent);
2011 trace_clk_set_parent(core, core->new_parent);
2013 if (core->ops->set_rate_and_parent) {
2014 skip_set_rate = true;
2015 core->ops->set_rate_and_parent(core->hw, core->new_rate,
2017 core->new_parent_index);
2018 } else if (core->ops->set_parent) {
2019 core->ops->set_parent(core->hw, core->new_parent_index);
2022 trace_clk_set_parent_complete(core, core->new_parent);
2023 __clk_set_parent_after(core, core->new_parent, old_parent);
2026 if (core->flags & CLK_OPS_PARENT_ENABLE)
2027 clk_core_prepare_enable(parent);
2029 trace_clk_set_rate(core, core->new_rate);
2031 if (!skip_set_rate && core->ops->set_rate)
2032 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
2034 trace_clk_set_rate_complete(core, core->new_rate);
2036 core->rate = clk_recalc(core, best_parent_rate);
2038 if (core->flags & CLK_SET_RATE_UNGATE) {
2039 unsigned long flags;
2041 flags = clk_enable_lock();
2042 clk_core_disable(core);
2043 clk_enable_unlock(flags);
2044 clk_core_unprepare(core);
2047 if (core->flags & CLK_OPS_PARENT_ENABLE)
2048 clk_core_disable_unprepare(parent);
2050 if (core->notifier_count && old_rate != core->rate)
2051 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
2053 if (core->flags & CLK_RECALC_NEW_RATES)
2054 (void)clk_calc_new_rates(core, core->new_rate);
2057 * Use safe iteration, as change_rate can actually swap parents
2058 * for certain clock types.
2060 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
2061 /* Skip children who will be reparented to another clock */
2062 if (child->new_parent && child->new_parent != core)
2064 clk_change_rate(child);
2067 /* handle the new child who might not be in core->children yet */
2068 if (core->new_child)
2069 clk_change_rate(core->new_child);
2071 clk_pm_runtime_put(core);
2074 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2075 unsigned long req_rate)
2078 struct clk_rate_request req;
2080 lockdep_assert_held(&prepare_lock);
2085 /* simulate what the rate would be if it could be freely set */
2086 cnt = clk_core_rate_nuke_protect(core);
2090 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2091 req.rate = req_rate;
2093 ret = clk_core_round_rate_nolock(core, &req);
2095 /* restore the protection */
2096 clk_core_rate_restore_protect(core, cnt);
2098 return ret ? 0 : req.rate;
2101 static int clk_core_set_rate_nolock(struct clk_core *core,
2102 unsigned long req_rate)
2104 struct clk_core *top, *fail_clk;
2111 rate = clk_core_req_round_rate_nolock(core, req_rate);
2113 /* bail early if nothing to do */
2114 if (rate == clk_core_get_rate_nolock(core))
2117 /* fail on a direct rate set of a protected provider */
2118 if (clk_core_rate_is_protected(core))
2121 /* calculate new rates and get the topmost changed clock */
2122 top = clk_calc_new_rates(core, req_rate);
2126 ret = clk_pm_runtime_get(core);
2130 /* notify that we are about to change rates */
2131 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2133 pr_debug("%s: failed to set %s rate\n", __func__,
2135 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2140 /* change the rates */
2141 clk_change_rate(top);
2143 core->req_rate = req_rate;
2145 clk_pm_runtime_put(core);
2151 * clk_set_rate - specify a new rate for clk
2152 * @clk: the clk whose rate is being changed
2153 * @rate: the new rate for clk
2155 * In the simplest case clk_set_rate will only adjust the rate of clk.
2157 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2158 * propagate up to clk's parent; whether or not this happens depends on the
2159 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2160 * after calling .round_rate then upstream parent propagation is ignored. If
2161 * *parent_rate comes back with a new rate for clk's parent then we propagate
2162 * up to clk's parent and set its rate. Upward propagation will continue
2163 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2164 * .round_rate stops requesting changes to clk's parent_rate.
2166 * Rate changes are accomplished via tree traversal that also recalculates the
2167 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2169 * Returns 0 on success, -EERROR otherwise.
2171 int clk_set_rate(struct clk *clk, unsigned long rate)
2178 /* prevent racing with updates to the clock topology */
2181 if (clk->exclusive_count)
2182 clk_core_rate_unprotect(clk->core);
2184 ret = clk_core_set_rate_nolock(clk->core, rate);
2186 if (clk->exclusive_count)
2187 clk_core_rate_protect(clk->core);
2189 clk_prepare_unlock();
2193 EXPORT_SYMBOL_GPL(clk_set_rate);
2196 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2197 * @clk: the clk whose rate is being changed
2198 * @rate: the new rate for clk
2200 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2201 * within a critical section
2203 * This can be used initially to ensure that at least 1 consumer is
2204 * satisfied when several consumers are competing for exclusivity over the
2205 * same clock provider.
2207 * The exclusivity is not applied if setting the rate failed.
2209 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2210 * clk_rate_exclusive_put().
2212 * Returns 0 on success, -EERROR otherwise.
2214 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2221 /* prevent racing with updates to the clock topology */
2225 * The temporary protection removal is not here, on purpose
2226 * This function is meant to be used instead of clk_rate_protect,
2227 * so before the consumer code path protect the clock provider
2230 ret = clk_core_set_rate_nolock(clk->core, rate);
2232 clk_core_rate_protect(clk->core);
2233 clk->exclusive_count++;
2236 clk_prepare_unlock();
2240 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2243 * clk_set_rate_range - set a rate range for a clock source
2244 * @clk: clock source
2245 * @min: desired minimum clock rate in Hz, inclusive
2246 * @max: desired maximum clock rate in Hz, inclusive
2248 * Returns success (0) or negative errno.
2250 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2253 unsigned long old_min, old_max, rate;
2259 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2260 __func__, clk->core->name, clk->dev_id, clk->con_id,
2267 if (clk->exclusive_count)
2268 clk_core_rate_unprotect(clk->core);
2270 /* Save the current values in case we need to rollback the change */
2271 old_min = clk->min_rate;
2272 old_max = clk->max_rate;
2273 clk->min_rate = min;
2274 clk->max_rate = max;
2276 rate = clk_core_get_rate_nolock(clk->core);
2277 if (rate < min || rate > max) {
2280 * We are in bit of trouble here, current rate is outside the
2281 * the requested range. We are going try to request appropriate
2282 * range boundary but there is a catch. It may fail for the
2283 * usual reason (clock broken, clock protected, etc) but also
2285 * - round_rate() was not favorable and fell on the wrong
2286 * side of the boundary
2287 * - the determine_rate() callback does not really check for
2288 * this corner case when determining the rate
2296 ret = clk_core_set_rate_nolock(clk->core, rate);
2298 /* rollback the changes */
2299 clk->min_rate = old_min;
2300 clk->max_rate = old_max;
2304 if (clk->exclusive_count)
2305 clk_core_rate_protect(clk->core);
2307 clk_prepare_unlock();
2311 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2314 * clk_set_min_rate - set a minimum clock rate for a clock source
2315 * @clk: clock source
2316 * @rate: desired minimum clock rate in Hz, inclusive
2318 * Returns success (0) or negative errno.
2320 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2325 return clk_set_rate_range(clk, rate, clk->max_rate);
2327 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2330 * clk_set_max_rate - set a maximum clock rate for a clock source
2331 * @clk: clock source
2332 * @rate: desired maximum clock rate in Hz, inclusive
2334 * Returns success (0) or negative errno.
2336 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2341 return clk_set_rate_range(clk, clk->min_rate, rate);
2343 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2346 * clk_get_parent - return the parent of a clk
2347 * @clk: the clk whose parent gets returned
2349 * Simply returns clk->parent. Returns NULL if clk is NULL.
2351 struct clk *clk_get_parent(struct clk *clk)
2359 /* TODO: Create a per-user clk and change callers to call clk_put */
2360 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2361 clk_prepare_unlock();
2365 EXPORT_SYMBOL_GPL(clk_get_parent);
2367 static struct clk_core *__clk_init_parent(struct clk_core *core)
2371 if (core->num_parents > 1 && core->ops->get_parent)
2372 index = core->ops->get_parent(core->hw);
2374 return clk_core_get_parent_by_index(core, index);
2377 static void clk_core_reparent(struct clk_core *core,
2378 struct clk_core *new_parent)
2380 clk_reparent(core, new_parent);
2381 __clk_recalc_accuracies(core);
2382 __clk_recalc_rates(core, POST_RATE_CHANGE);
2385 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2390 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2394 * clk_has_parent - check if a clock is a possible parent for another
2395 * @clk: clock source
2396 * @parent: parent clock source
2398 * This function can be used in drivers that need to check that a clock can be
2399 * the parent of another without actually changing the parent.
2401 * Returns true if @parent is a possible parent for @clk, false otherwise.
2403 bool clk_has_parent(struct clk *clk, struct clk *parent)
2405 struct clk_core *core, *parent_core;
2408 /* NULL clocks should be nops, so return success if either is NULL. */
2409 if (!clk || !parent)
2413 parent_core = parent->core;
2415 /* Optimize for the case where the parent is already the parent. */
2416 if (core->parent == parent_core)
2419 for (i = 0; i < core->num_parents; i++)
2420 if (!strcmp(core->parents[i].name, parent_core->name))
2425 EXPORT_SYMBOL_GPL(clk_has_parent);
2427 static int clk_core_set_parent_nolock(struct clk_core *core,
2428 struct clk_core *parent)
2432 unsigned long p_rate = 0;
2434 lockdep_assert_held(&prepare_lock);
2439 if (core->parent == parent)
2442 /* verify ops for for multi-parent clks */
2443 if (core->num_parents > 1 && !core->ops->set_parent)
2446 /* check that we are allowed to re-parent if the clock is in use */
2447 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2450 if (clk_core_rate_is_protected(core))
2453 /* try finding the new parent index */
2455 p_index = clk_fetch_parent_index(core, parent);
2457 pr_debug("%s: clk %s can not be parent of clk %s\n",
2458 __func__, parent->name, core->name);
2461 p_rate = parent->rate;
2464 ret = clk_pm_runtime_get(core);
2468 /* propagate PRE_RATE_CHANGE notifications */
2469 ret = __clk_speculate_rates(core, p_rate);
2471 /* abort if a driver objects */
2472 if (ret & NOTIFY_STOP_MASK)
2475 /* do the re-parent */
2476 ret = __clk_set_parent(core, parent, p_index);
2478 /* propagate rate an accuracy recalculation accordingly */
2480 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2482 __clk_recalc_rates(core, POST_RATE_CHANGE);
2483 __clk_recalc_accuracies(core);
2487 clk_pm_runtime_put(core);
2492 int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
2494 return clk_core_set_parent_nolock(hw->core, parent->core);
2496 EXPORT_SYMBOL_GPL(clk_hw_set_parent);
2499 * clk_set_parent - switch the parent of a mux clk
2500 * @clk: the mux clk whose input we are switching
2501 * @parent: the new input to clk
2503 * Re-parent clk to use parent as its new input source. If clk is in
2504 * prepared state, the clk will get enabled for the duration of this call. If
2505 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2506 * that, the reparenting is glitchy in hardware, etc), use the
2507 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2509 * After successfully changing clk's parent clk_set_parent will update the
2510 * clk topology, sysfs topology and propagate rate recalculation via
2511 * __clk_recalc_rates.
2513 * Returns 0 on success, -EERROR otherwise.
2515 int clk_set_parent(struct clk *clk, struct clk *parent)
2524 if (clk->exclusive_count)
2525 clk_core_rate_unprotect(clk->core);
2527 ret = clk_core_set_parent_nolock(clk->core,
2528 parent ? parent->core : NULL);
2530 if (clk->exclusive_count)
2531 clk_core_rate_protect(clk->core);
2533 clk_prepare_unlock();
2537 EXPORT_SYMBOL_GPL(clk_set_parent);
2539 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2543 lockdep_assert_held(&prepare_lock);
2548 if (clk_core_rate_is_protected(core))
2551 trace_clk_set_phase(core, degrees);
2553 if (core->ops->set_phase) {
2554 ret = core->ops->set_phase(core->hw, degrees);
2556 core->phase = degrees;
2559 trace_clk_set_phase_complete(core, degrees);
2565 * clk_set_phase - adjust the phase shift of a clock signal
2566 * @clk: clock signal source
2567 * @degrees: number of degrees the signal is shifted
2569 * Shifts the phase of a clock signal by the specified
2570 * degrees. Returns 0 on success, -EERROR otherwise.
2572 * This function makes no distinction about the input or reference
2573 * signal that we adjust the clock signal phase against. For example
2574 * phase locked-loop clock signal generators we may shift phase with
2575 * respect to feedback clock signal input, but for other cases the
2576 * clock phase may be shifted with respect to some other, unspecified
2579 * Additionally the concept of phase shift does not propagate through
2580 * the clock tree hierarchy, which sets it apart from clock rates and
2581 * clock accuracy. A parent clock phase attribute does not have an
2582 * impact on the phase attribute of a child clock.
2584 int clk_set_phase(struct clk *clk, int degrees)
2591 /* sanity check degrees */
2598 if (clk->exclusive_count)
2599 clk_core_rate_unprotect(clk->core);
2601 ret = clk_core_set_phase_nolock(clk->core, degrees);
2603 if (clk->exclusive_count)
2604 clk_core_rate_protect(clk->core);
2606 clk_prepare_unlock();
2610 EXPORT_SYMBOL_GPL(clk_set_phase);
2612 static int clk_core_get_phase(struct clk_core *core)
2617 /* Always try to update cached phase if possible */
2618 if (core->ops->get_phase)
2619 core->phase = core->ops->get_phase(core->hw);
2621 clk_prepare_unlock();
2627 * clk_get_phase - return the phase shift of a clock signal
2628 * @clk: clock signal source
2630 * Returns the phase shift of a clock node in degrees, otherwise returns
2633 int clk_get_phase(struct clk *clk)
2638 return clk_core_get_phase(clk->core);
2640 EXPORT_SYMBOL_GPL(clk_get_phase);
2642 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2644 /* Assume a default value of 50% */
2649 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2651 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2653 struct clk_duty *duty = &core->duty;
2656 if (!core->ops->get_duty_cycle)
2657 return clk_core_update_duty_cycle_parent_nolock(core);
2659 ret = core->ops->get_duty_cycle(core->hw, duty);
2663 /* Don't trust the clock provider too much */
2664 if (duty->den == 0 || duty->num > duty->den) {
2672 clk_core_reset_duty_cycle_nolock(core);
2676 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2681 core->flags & CLK_DUTY_CYCLE_PARENT) {
2682 ret = clk_core_update_duty_cycle_nolock(core->parent);
2683 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2685 clk_core_reset_duty_cycle_nolock(core);
2691 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2692 struct clk_duty *duty);
2694 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2695 struct clk_duty *duty)
2699 lockdep_assert_held(&prepare_lock);
2701 if (clk_core_rate_is_protected(core))
2704 trace_clk_set_duty_cycle(core, duty);
2706 if (!core->ops->set_duty_cycle)
2707 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2709 ret = core->ops->set_duty_cycle(core->hw, duty);
2711 memcpy(&core->duty, duty, sizeof(*duty));
2713 trace_clk_set_duty_cycle_complete(core, duty);
2718 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2719 struct clk_duty *duty)
2724 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2725 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2726 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2733 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2734 * @clk: clock signal source
2735 * @num: numerator of the duty cycle ratio to be applied
2736 * @den: denominator of the duty cycle ratio to be applied
2738 * Apply the duty cycle ratio if the ratio is valid and the clock can
2739 * perform this operation
2741 * Returns (0) on success, a negative errno otherwise.
2743 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2746 struct clk_duty duty;
2751 /* sanity check the ratio */
2752 if (den == 0 || num > den)
2760 if (clk->exclusive_count)
2761 clk_core_rate_unprotect(clk->core);
2763 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2765 if (clk->exclusive_count)
2766 clk_core_rate_protect(clk->core);
2768 clk_prepare_unlock();
2772 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2774 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2777 struct clk_duty *duty = &core->duty;
2782 ret = clk_core_update_duty_cycle_nolock(core);
2784 ret = mult_frac(scale, duty->num, duty->den);
2786 clk_prepare_unlock();
2792 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2793 * @clk: clock signal source
2794 * @scale: scaling factor to be applied to represent the ratio as an integer
2796 * Returns the duty cycle ratio of a clock node multiplied by the provided
2797 * scaling factor, or negative errno on error.
2799 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2804 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2806 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2809 * clk_is_match - check if two clk's point to the same hardware clock
2810 * @p: clk compared against q
2811 * @q: clk compared against p
2813 * Returns true if the two struct clk pointers both point to the same hardware
2814 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2815 * share the same struct clk_core object.
2817 * Returns false otherwise. Note that two NULL clks are treated as matching.
2819 bool clk_is_match(const struct clk *p, const struct clk *q)
2821 /* trivial case: identical struct clk's or both NULL */
2825 /* true if clk->core pointers match. Avoid dereferencing garbage */
2826 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2827 if (p->core == q->core)
2832 EXPORT_SYMBOL_GPL(clk_is_match);
2834 /*** debugfs support ***/
2836 #ifdef CONFIG_DEBUG_FS
2837 #include <linux/debugfs.h>
2839 static struct dentry *rootdir;
2840 static int inited = 0;
2841 static DEFINE_MUTEX(clk_debug_lock);
2842 static HLIST_HEAD(clk_debug_list);
2844 static struct hlist_head *all_lists[] = {
2850 static struct hlist_head *orphan_list[] = {
2855 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2858 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
2860 30 - level * 3, c->name,
2861 c->enable_count, c->prepare_count, c->protect_count,
2862 clk_core_get_rate(c), clk_core_get_accuracy(c),
2863 clk_core_get_phase(c),
2864 clk_core_get_scaled_duty_cycle(c, 100000));
2867 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2870 struct clk_core *child;
2872 clk_summary_show_one(s, c, level);
2874 hlist_for_each_entry(child, &c->children, child_node)
2875 clk_summary_show_subtree(s, child, level + 1);
2878 static int clk_summary_show(struct seq_file *s, void *data)
2881 struct hlist_head **lists = (struct hlist_head **)s->private;
2883 seq_puts(s, " enable prepare protect duty\n");
2884 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2885 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
2889 for (; *lists; lists++)
2890 hlist_for_each_entry(c, *lists, child_node)
2891 clk_summary_show_subtree(s, c, 0);
2893 clk_prepare_unlock();
2897 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2899 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2901 unsigned long min_rate, max_rate;
2903 clk_core_get_boundaries(c, &min_rate, &max_rate);
2905 /* This should be JSON format, i.e. elements separated with a comma */
2906 seq_printf(s, "\"%s\": { ", c->name);
2907 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2908 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2909 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2910 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2911 seq_printf(s, "\"min_rate\": %lu,", min_rate);
2912 seq_printf(s, "\"max_rate\": %lu,", max_rate);
2913 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2914 seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
2915 seq_printf(s, "\"duty_cycle\": %u",
2916 clk_core_get_scaled_duty_cycle(c, 100000));
2919 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2921 struct clk_core *child;
2923 clk_dump_one(s, c, level);
2925 hlist_for_each_entry(child, &c->children, child_node) {
2927 clk_dump_subtree(s, child, level + 1);
2933 static int clk_dump_show(struct seq_file *s, void *data)
2936 bool first_node = true;
2937 struct hlist_head **lists = (struct hlist_head **)s->private;
2942 for (; *lists; lists++) {
2943 hlist_for_each_entry(c, *lists, child_node) {
2947 clk_dump_subtree(s, c, 0);
2951 clk_prepare_unlock();
2956 DEFINE_SHOW_ATTRIBUTE(clk_dump);
2958 static const struct {
2962 #define ENTRY(f) { f, #f }
2963 ENTRY(CLK_SET_RATE_GATE),
2964 ENTRY(CLK_SET_PARENT_GATE),
2965 ENTRY(CLK_SET_RATE_PARENT),
2966 ENTRY(CLK_IGNORE_UNUSED),
2967 ENTRY(CLK_GET_RATE_NOCACHE),
2968 ENTRY(CLK_SET_RATE_NO_REPARENT),
2969 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2970 ENTRY(CLK_RECALC_NEW_RATES),
2971 ENTRY(CLK_SET_RATE_UNGATE),
2972 ENTRY(CLK_IS_CRITICAL),
2973 ENTRY(CLK_OPS_PARENT_ENABLE),
2974 ENTRY(CLK_DUTY_CYCLE_PARENT),
2978 static int clk_flags_show(struct seq_file *s, void *data)
2980 struct clk_core *core = s->private;
2981 unsigned long flags = core->flags;
2984 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2985 if (flags & clk_flags[i].flag) {
2986 seq_printf(s, "%s\n", clk_flags[i].name);
2987 flags &= ~clk_flags[i].flag;
2992 seq_printf(s, "0x%lx\n", flags);
2997 DEFINE_SHOW_ATTRIBUTE(clk_flags);
2999 static void possible_parent_show(struct seq_file *s, struct clk_core *core,
3000 unsigned int i, char terminator)
3002 struct clk_core *parent;
3005 * Go through the following options to fetch a parent's name.
3007 * 1. Fetch the registered parent clock and use its name
3008 * 2. Use the global (fallback) name if specified
3009 * 3. Use the local fw_name if provided
3010 * 4. Fetch parent clock's clock-output-name if DT index was set
3012 * This may still fail in some cases, such as when the parent is
3013 * specified directly via a struct clk_hw pointer, but it isn't
3016 parent = clk_core_get_parent_by_index(core, i);
3018 seq_puts(s, parent->name);
3019 else if (core->parents[i].name)
3020 seq_puts(s, core->parents[i].name);
3021 else if (core->parents[i].fw_name)
3022 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3023 else if (core->parents[i].index >= 0)
3025 of_clk_get_parent_name(core->of_node,
3026 core->parents[i].index));
3028 seq_puts(s, "(missing)");
3030 seq_putc(s, terminator);
3033 static int possible_parents_show(struct seq_file *s, void *data)
3035 struct clk_core *core = s->private;
3038 for (i = 0; i < core->num_parents - 1; i++)
3039 possible_parent_show(s, core, i, ' ');
3041 possible_parent_show(s, core, i, '\n');
3045 DEFINE_SHOW_ATTRIBUTE(possible_parents);
3047 static int current_parent_show(struct seq_file *s, void *data)
3049 struct clk_core *core = s->private;
3052 seq_printf(s, "%s\n", core->parent->name);
3056 DEFINE_SHOW_ATTRIBUTE(current_parent);
3058 static int clk_duty_cycle_show(struct seq_file *s, void *data)
3060 struct clk_core *core = s->private;
3061 struct clk_duty *duty = &core->duty;
3063 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3067 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3069 static int clk_min_rate_show(struct seq_file *s, void *data)
3071 struct clk_core *core = s->private;
3072 unsigned long min_rate, max_rate;
3075 clk_core_get_boundaries(core, &min_rate, &max_rate);
3076 clk_prepare_unlock();
3077 seq_printf(s, "%lu\n", min_rate);
3081 DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
3083 static int clk_max_rate_show(struct seq_file *s, void *data)
3085 struct clk_core *core = s->private;
3086 unsigned long min_rate, max_rate;
3089 clk_core_get_boundaries(core, &min_rate, &max_rate);
3090 clk_prepare_unlock();
3091 seq_printf(s, "%lu\n", max_rate);
3095 DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
3097 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
3099 struct dentry *root;
3101 if (!core || !pdentry)
3104 root = debugfs_create_dir(core->name, pdentry);
3105 core->dentry = root;
3107 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
3108 debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
3109 debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
3110 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3111 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3112 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3113 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3114 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3115 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3116 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
3117 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3118 &clk_duty_cycle_fops);
3120 if (core->num_parents > 0)
3121 debugfs_create_file("clk_parent", 0444, root, core,
3122 ¤t_parent_fops);
3124 if (core->num_parents > 1)
3125 debugfs_create_file("clk_possible_parents", 0444, root, core,
3126 &possible_parents_fops);
3128 if (core->ops->debug_init)
3129 core->ops->debug_init(core->hw, core->dentry);
3133 * clk_debug_register - add a clk node to the debugfs clk directory
3134 * @core: the clk being added to the debugfs clk directory
3136 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3137 * initialized. Otherwise it bails out early since the debugfs clk directory
3138 * will be created lazily by clk_debug_init as part of a late_initcall.
3140 static void clk_debug_register(struct clk_core *core)
3142 mutex_lock(&clk_debug_lock);
3143 hlist_add_head(&core->debug_node, &clk_debug_list);
3145 clk_debug_create_one(core, rootdir);
3146 mutex_unlock(&clk_debug_lock);
3150 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3151 * @core: the clk being removed from the debugfs clk directory
3153 * Dynamically removes a clk and all its child nodes from the
3154 * debugfs clk directory if clk->dentry points to debugfs created by
3155 * clk_debug_register in __clk_core_init.
3157 static void clk_debug_unregister(struct clk_core *core)
3159 mutex_lock(&clk_debug_lock);
3160 hlist_del_init(&core->debug_node);
3161 debugfs_remove_recursive(core->dentry);
3162 core->dentry = NULL;
3163 mutex_unlock(&clk_debug_lock);
3167 * clk_debug_init - lazily populate the debugfs clk directory
3169 * clks are often initialized very early during boot before memory can be
3170 * dynamically allocated and well before debugfs is setup. This function
3171 * populates the debugfs clk directory once at boot-time when we know that
3172 * debugfs is setup. It should only be called once at boot-time, all other clks
3173 * added dynamically will be done so with clk_debug_register.
3175 static int __init clk_debug_init(void)
3177 struct clk_core *core;
3179 rootdir = debugfs_create_dir("clk", NULL);
3181 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3183 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3185 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3187 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3190 mutex_lock(&clk_debug_lock);
3191 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3192 clk_debug_create_one(core, rootdir);
3195 mutex_unlock(&clk_debug_lock);
3199 late_initcall(clk_debug_init);
3201 static inline void clk_debug_register(struct clk_core *core) { }
3202 static inline void clk_debug_reparent(struct clk_core *core,
3203 struct clk_core *new_parent)
3206 static inline void clk_debug_unregister(struct clk_core *core)
3212 * __clk_core_init - initialize the data structures in a struct clk_core
3213 * @core: clk_core being initialized
3215 * Initializes the lists in struct clk_core, queries the hardware for the
3216 * parent and rate and sets them both.
3218 static int __clk_core_init(struct clk_core *core)
3221 struct clk_core *orphan;
3222 struct hlist_node *tmp2;
3230 ret = clk_pm_runtime_get(core);
3234 /* check to see if a clock with this name is already registered */
3235 if (clk_core_lookup(core->name)) {
3236 pr_debug("%s: clk %s already initialized\n",
3237 __func__, core->name);
3242 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3243 if (core->ops->set_rate &&
3244 !((core->ops->round_rate || core->ops->determine_rate) &&
3245 core->ops->recalc_rate)) {
3246 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3247 __func__, core->name);
3252 if (core->ops->set_parent && !core->ops->get_parent) {
3253 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3254 __func__, core->name);
3259 if (core->num_parents > 1 && !core->ops->get_parent) {
3260 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3261 __func__, core->name);
3266 if (core->ops->set_rate_and_parent &&
3267 !(core->ops->set_parent && core->ops->set_rate)) {
3268 pr_err("%s: %s must implement .set_parent & .set_rate\n",
3269 __func__, core->name);
3274 core->parent = __clk_init_parent(core);
3277 * Populate core->parent if parent has already been clk_core_init'd. If
3278 * parent has not yet been clk_core_init'd then place clk in the orphan
3279 * list. If clk doesn't have any parents then place it in the root
3282 * Every time a new clk is clk_init'd then we walk the list of orphan
3283 * clocks and re-parent any that are children of the clock currently
3287 hlist_add_head(&core->child_node,
3288 &core->parent->children);
3289 core->orphan = core->parent->orphan;
3290 } else if (!core->num_parents) {
3291 hlist_add_head(&core->child_node, &clk_root_list);
3292 core->orphan = false;
3294 hlist_add_head(&core->child_node, &clk_orphan_list);
3295 core->orphan = true;
3299 * optional platform-specific magic
3301 * The .init callback is not used by any of the basic clock types, but
3302 * exists for weird hardware that must perform initialization magic.
3303 * Please consider other ways of solving initialization problems before
3304 * using this callback, as its use is discouraged.
3306 if (core->ops->init)
3307 core->ops->init(core->hw);
3310 * Set clk's accuracy. The preferred method is to use
3311 * .recalc_accuracy. For simple clocks and lazy developers the default
3312 * fallback is to use the parent's accuracy. If a clock doesn't have a
3313 * parent (or is orphaned) then accuracy is set to zero (perfect
3316 if (core->ops->recalc_accuracy)
3317 core->accuracy = core->ops->recalc_accuracy(core->hw,
3318 __clk_get_accuracy(core->parent));
3319 else if (core->parent)
3320 core->accuracy = core->parent->accuracy;
3326 * Since a phase is by definition relative to its parent, just
3327 * query the current clock phase, or just assume it's in phase.
3329 if (core->ops->get_phase)
3330 core->phase = core->ops->get_phase(core->hw);
3335 * Set clk's duty cycle.
3337 clk_core_update_duty_cycle_nolock(core);
3340 * Set clk's rate. The preferred method is to use .recalc_rate. For
3341 * simple clocks and lazy developers the default fallback is to use the
3342 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3343 * then rate is set to zero.
3345 if (core->ops->recalc_rate)
3346 rate = core->ops->recalc_rate(core->hw,
3347 clk_core_get_rate_nolock(core->parent));
3348 else if (core->parent)
3349 rate = core->parent->rate;
3352 core->rate = core->req_rate = rate;
3355 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3356 * don't get accidentally disabled when walking the orphan tree and
3357 * reparenting clocks
3359 if (core->flags & CLK_IS_CRITICAL) {
3360 unsigned long flags;
3362 clk_core_prepare(core);
3364 flags = clk_enable_lock();
3365 clk_core_enable(core);
3366 clk_enable_unlock(flags);
3370 * walk the list of orphan clocks and reparent any that newly finds a
3373 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3374 struct clk_core *parent = __clk_init_parent(orphan);
3377 * We need to use __clk_set_parent_before() and _after() to
3378 * to properly migrate any prepare/enable count of the orphan
3379 * clock. This is important for CLK_IS_CRITICAL clocks, which
3380 * are enabled during init but might not have a parent yet.
3383 /* update the clk tree topology */
3384 __clk_set_parent_before(orphan, parent);
3385 __clk_set_parent_after(orphan, parent, NULL);
3386 __clk_recalc_accuracies(orphan);
3387 __clk_recalc_rates(orphan, 0);
3391 kref_init(&core->ref);
3393 clk_pm_runtime_put(core);
3395 clk_prepare_unlock();
3398 clk_debug_register(core);
3404 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3405 * @core: clk to add consumer to
3406 * @clk: consumer to link to a clk
3408 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3411 hlist_add_head(&clk->clks_node, &core->clks);
3412 clk_prepare_unlock();
3416 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3417 * @clk: consumer to unlink
3419 static void clk_core_unlink_consumer(struct clk *clk)
3421 lockdep_assert_held(&prepare_lock);
3422 hlist_del(&clk->clks_node);
3426 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3427 * @core: clk to allocate a consumer for
3428 * @dev_id: string describing device name
3429 * @con_id: connection ID string on device
3431 * Returns: clk consumer left unlinked from the consumer list
3433 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
3438 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3440 return ERR_PTR(-ENOMEM);
3443 clk->dev_id = dev_id;
3444 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3445 clk->max_rate = ULONG_MAX;
3451 * free_clk - Free a clk consumer
3452 * @clk: clk consumer to free
3454 * Note, this assumes the clk has been unlinked from the clk_core consumer
3457 static void free_clk(struct clk *clk)
3459 kfree_const(clk->con_id);
3464 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3466 * @dev: clk consumer device
3467 * @hw: clk_hw associated with the clk being consumed
3468 * @dev_id: string describing device name
3469 * @con_id: connection ID string on device
3471 * This is the main function used to create a clk pointer for use by clk
3472 * consumers. It connects a consumer to the clk_core and clk_hw structures
3473 * used by the framework and clk provider respectively.
3475 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
3476 const char *dev_id, const char *con_id)
3479 struct clk_core *core;
3481 /* This is to allow this function to be chained to others */
3482 if (IS_ERR_OR_NULL(hw))
3483 return ERR_CAST(hw);
3486 clk = alloc_clk(core, dev_id, con_id);
3491 if (!try_module_get(core->owner)) {
3493 return ERR_PTR(-ENOENT);
3496 kref_get(&core->ref);
3497 clk_core_link_consumer(core, clk);
3502 static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
3512 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3519 static int clk_core_populate_parent_map(struct clk_core *core)
3521 const struct clk_init_data *init = core->hw->init;
3522 u8 num_parents = init->num_parents;
3523 const char * const *parent_names = init->parent_names;
3524 const struct clk_hw **parent_hws = init->parent_hws;
3525 const struct clk_parent_data *parent_data = init->parent_data;
3527 struct clk_parent_map *parents, *parent;
3533 * Avoid unnecessary string look-ups of clk_core's possible parents by
3534 * having a cache of names/clk_hw pointers to clk_core pointers.
3536 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3537 core->parents = parents;
3541 /* Copy everything over because it might be __initdata */
3542 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
3545 /* throw a WARN if any entries are NULL */
3546 WARN(!parent_names[i],
3547 "%s: invalid NULL in %s's .parent_names\n",
3548 __func__, core->name);
3549 ret = clk_cpy_name(&parent->name, parent_names[i],
3551 } else if (parent_data) {
3552 parent->hw = parent_data[i].hw;
3553 parent->index = parent_data[i].index;
3554 ret = clk_cpy_name(&parent->fw_name,
3555 parent_data[i].fw_name, false);
3557 ret = clk_cpy_name(&parent->name,
3558 parent_data[i].name,
3560 } else if (parent_hws) {
3561 parent->hw = parent_hws[i];
3564 WARN(1, "Must specify parents if num_parents > 0\n");
3569 kfree_const(parents[i].name);
3570 kfree_const(parents[i].fw_name);
3581 static void clk_core_free_parent_map(struct clk_core *core)
3583 int i = core->num_parents;
3585 if (!core->num_parents)
3589 kfree_const(core->parents[i].name);
3590 kfree_const(core->parents[i].fw_name);
3593 kfree(core->parents);
3597 __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
3600 struct clk_core *core;
3602 core = kzalloc(sizeof(*core), GFP_KERNEL);
3608 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3614 if (WARN_ON(!hw->init->ops)) {
3618 core->ops = hw->init->ops;
3620 if (dev && pm_runtime_enabled(dev))
3621 core->rpm_enabled = true;
3624 if (dev && dev->driver)
3625 core->owner = dev->driver->owner;
3627 core->flags = hw->init->flags;
3628 core->num_parents = hw->init->num_parents;
3630 core->max_rate = ULONG_MAX;
3633 ret = clk_core_populate_parent_map(core);
3637 INIT_HLIST_HEAD(&core->clks);
3640 * Don't call clk_hw_create_clk() here because that would pin the
3641 * provider module to itself and prevent it from ever being removed.
3643 hw->clk = alloc_clk(core, NULL, NULL);
3644 if (IS_ERR(hw->clk)) {
3645 ret = PTR_ERR(hw->clk);
3646 goto fail_create_clk;
3649 clk_core_link_consumer(hw->core, hw->clk);
3651 ret = __clk_core_init(core);
3656 clk_core_unlink_consumer(hw->clk);
3657 clk_prepare_unlock();
3663 clk_core_free_parent_map(core);
3666 kfree_const(core->name);
3670 return ERR_PTR(ret);
3674 * clk_register - allocate a new clock, register it and return an opaque cookie
3675 * @dev: device that is registering this clock
3676 * @hw: link to hardware-specific clock data
3678 * clk_register is the *deprecated* interface for populating the clock tree with
3679 * new clock nodes. Use clk_hw_register() instead.
3681 * Returns: a pointer to the newly allocated struct clk which
3682 * cannot be dereferenced by driver code but may be used in conjunction with the
3683 * rest of the clock API. In the event of an error clk_register will return an
3684 * error code; drivers must test for an error code after calling clk_register.
3686 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3688 return __clk_register(dev, dev_of_node(dev), hw);
3690 EXPORT_SYMBOL_GPL(clk_register);
3693 * clk_hw_register - register a clk_hw and return an error code
3694 * @dev: device that is registering this clock
3695 * @hw: link to hardware-specific clock data
3697 * clk_hw_register is the primary interface for populating the clock tree with
3698 * new clock nodes. It returns an integer equal to zero indicating success or
3699 * less than zero indicating failure. Drivers must test for an error code after
3700 * calling clk_hw_register().
3702 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3704 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_of_node(dev), hw));
3706 EXPORT_SYMBOL_GPL(clk_hw_register);
3709 * of_clk_hw_register - register a clk_hw and return an error code
3710 * @node: device_node of device that is registering this clock
3711 * @hw: link to hardware-specific clock data
3713 * of_clk_hw_register() is the primary interface for populating the clock tree
3714 * with new clock nodes when a struct device is not available, but a struct
3715 * device_node is. It returns an integer equal to zero indicating success or
3716 * less than zero indicating failure. Drivers must test for an error code after
3717 * calling of_clk_hw_register().
3719 int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3721 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3723 EXPORT_SYMBOL_GPL(of_clk_hw_register);
3725 /* Free memory allocated for a clock. */
3726 static void __clk_release(struct kref *ref)
3728 struct clk_core *core = container_of(ref, struct clk_core, ref);
3730 lockdep_assert_held(&prepare_lock);
3732 clk_core_free_parent_map(core);
3733 kfree_const(core->name);
3738 * Empty clk_ops for unregistered clocks. These are used temporarily
3739 * after clk_unregister() was called on a clock and until last clock
3740 * consumer calls clk_put() and the struct clk object is freed.
3742 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3747 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3752 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3753 unsigned long parent_rate)
3758 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3763 static const struct clk_ops clk_nodrv_ops = {
3764 .enable = clk_nodrv_prepare_enable,
3765 .disable = clk_nodrv_disable_unprepare,
3766 .prepare = clk_nodrv_prepare_enable,
3767 .unprepare = clk_nodrv_disable_unprepare,
3768 .set_rate = clk_nodrv_set_rate,
3769 .set_parent = clk_nodrv_set_parent,
3773 * clk_unregister - unregister a currently registered clock
3774 * @clk: clock to unregister
3776 void clk_unregister(struct clk *clk)
3778 unsigned long flags;
3780 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3783 clk_debug_unregister(clk->core);
3787 if (clk->core->ops == &clk_nodrv_ops) {
3788 pr_err("%s: unregistered clock: %s\n", __func__,
3793 * Assign empty clock ops for consumers that might still hold
3794 * a reference to this clock.
3796 flags = clk_enable_lock();
3797 clk->core->ops = &clk_nodrv_ops;
3798 clk_enable_unlock(flags);
3800 if (!hlist_empty(&clk->core->children)) {
3801 struct clk_core *child;
3802 struct hlist_node *t;
3804 /* Reparent all children to the orphan list. */
3805 hlist_for_each_entry_safe(child, t, &clk->core->children,
3807 clk_core_set_parent_nolock(child, NULL);
3810 hlist_del_init(&clk->core->child_node);
3812 if (clk->core->prepare_count)
3813 pr_warn("%s: unregistering prepared clock: %s\n",
3814 __func__, clk->core->name);
3816 if (clk->core->protect_count)
3817 pr_warn("%s: unregistering protected clock: %s\n",
3818 __func__, clk->core->name);
3820 kref_put(&clk->core->ref, __clk_release);
3822 clk_prepare_unlock();
3824 EXPORT_SYMBOL_GPL(clk_unregister);
3827 * clk_hw_unregister - unregister a currently registered clk_hw
3828 * @hw: hardware-specific clock data to unregister
3830 void clk_hw_unregister(struct clk_hw *hw)
3832 clk_unregister(hw->clk);
3834 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3836 static void devm_clk_release(struct device *dev, void *res)
3838 clk_unregister(*(struct clk **)res);
3841 static void devm_clk_hw_release(struct device *dev, void *res)
3843 clk_hw_unregister(*(struct clk_hw **)res);
3847 * devm_clk_register - resource managed clk_register()
3848 * @dev: device that is registering this clock
3849 * @hw: link to hardware-specific clock data
3851 * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
3853 * Clocks returned from this function are automatically clk_unregister()ed on
3854 * driver detach. See clk_register() for more information.
3856 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3861 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3863 return ERR_PTR(-ENOMEM);
3865 clk = clk_register(dev, hw);
3868 devres_add(dev, clkp);
3875 EXPORT_SYMBOL_GPL(devm_clk_register);
3878 * devm_clk_hw_register - resource managed clk_hw_register()
3879 * @dev: device that is registering this clock
3880 * @hw: link to hardware-specific clock data
3882 * Managed clk_hw_register(). Clocks registered by this function are
3883 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3884 * for more information.
3886 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3888 struct clk_hw **hwp;
3891 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3895 ret = clk_hw_register(dev, hw);
3898 devres_add(dev, hwp);
3905 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3907 static int devm_clk_match(struct device *dev, void *res, void *data)
3909 struct clk *c = res;
3915 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3917 struct clk_hw *hw = res;
3925 * devm_clk_unregister - resource managed clk_unregister()
3926 * @clk: clock to unregister
3928 * Deallocate a clock allocated with devm_clk_register(). Normally
3929 * this function will not need to be called and the resource management
3930 * code will ensure that the resource is freed.
3932 void devm_clk_unregister(struct device *dev, struct clk *clk)
3934 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3936 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3939 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3940 * @dev: device that is unregistering the hardware-specific clock data
3941 * @hw: link to hardware-specific clock data
3943 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3944 * this function will not need to be called and the resource management
3945 * code will ensure that the resource is freed.
3947 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3949 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3952 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3958 void __clk_put(struct clk *clk)
3960 struct module *owner;
3962 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3968 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3969 * given user should be balanced with calls to clk_rate_exclusive_put()
3970 * and by that same consumer
3972 if (WARN_ON(clk->exclusive_count)) {
3973 /* We voiced our concern, let's sanitize the situation */
3974 clk->core->protect_count -= (clk->exclusive_count - 1);
3975 clk_core_rate_unprotect(clk->core);
3976 clk->exclusive_count = 0;
3979 hlist_del(&clk->clks_node);
3980 if (clk->min_rate > clk->core->req_rate ||
3981 clk->max_rate < clk->core->req_rate)
3982 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3984 owner = clk->core->owner;
3985 kref_put(&clk->core->ref, __clk_release);
3987 clk_prepare_unlock();
3994 /*** clk rate change notifiers ***/
3997 * clk_notifier_register - add a clk rate change notifier
3998 * @clk: struct clk * to watch
3999 * @nb: struct notifier_block * with callback info
4001 * Request notification when clk's rate changes. This uses an SRCU
4002 * notifier because we want it to block and notifier unregistrations are
4003 * uncommon. The callbacks associated with the notifier must not
4004 * re-enter into the clk framework by calling any top-level clk APIs;
4005 * this will cause a nested prepare_lock mutex.
4007 * In all notification cases (pre, post and abort rate change) the original
4008 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4009 * and the new frequency is passed via struct clk_notifier_data.new_rate.
4011 * clk_notifier_register() must be called from non-atomic context.
4012 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4013 * allocation failure; otherwise, passes along the return value of
4014 * srcu_notifier_chain_register().
4016 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
4018 struct clk_notifier *cn;
4026 /* search the list of notifiers for this clk */
4027 list_for_each_entry(cn, &clk_notifier_list, node)
4031 /* if clk wasn't in the notifier list, allocate new clk_notifier */
4032 if (cn->clk != clk) {
4033 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
4038 srcu_init_notifier_head(&cn->notifier_head);
4040 list_add(&cn->node, &clk_notifier_list);
4043 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4045 clk->core->notifier_count++;
4048 clk_prepare_unlock();
4052 EXPORT_SYMBOL_GPL(clk_notifier_register);
4055 * clk_notifier_unregister - remove a clk rate change notifier
4056 * @clk: struct clk *
4057 * @nb: struct notifier_block * with callback info
4059 * Request no further notification for changes to 'clk' and frees memory
4060 * allocated in clk_notifier_register.
4062 * Returns -EINVAL if called with null arguments; otherwise, passes
4063 * along the return value of srcu_notifier_chain_unregister().
4065 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4067 struct clk_notifier *cn = NULL;
4075 list_for_each_entry(cn, &clk_notifier_list, node)
4079 if (cn->clk == clk) {
4080 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4082 clk->core->notifier_count--;
4084 /* XXX the notifier code should handle this better */
4085 if (!cn->notifier_head.head) {
4086 srcu_cleanup_notifier_head(&cn->notifier_head);
4087 list_del(&cn->node);
4095 clk_prepare_unlock();
4099 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
4103 * struct of_clk_provider - Clock provider registration structure
4104 * @link: Entry in global list of clock providers
4105 * @node: Pointer to device tree node of clock provider
4106 * @get: Get clock callback. Returns NULL or a struct clk for the
4107 * given clock specifier
4108 * @data: context pointer to be passed into @get callback
4110 struct of_clk_provider {
4111 struct list_head link;
4113 struct device_node *node;
4114 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4115 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
4119 extern struct of_device_id __clk_of_table;
4120 static const struct of_device_id __clk_of_table_sentinel
4121 __used __section(__clk_of_table_end);
4123 static LIST_HEAD(of_clk_providers);
4124 static DEFINE_MUTEX(of_clk_mutex);
4126 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4131 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4133 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4137 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4139 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4141 struct clk_onecell_data *clk_data = data;
4142 unsigned int idx = clkspec->args[0];
4144 if (idx >= clk_data->clk_num) {
4145 pr_err("%s: invalid clock index %u\n", __func__, idx);
4146 return ERR_PTR(-EINVAL);
4149 return clk_data->clks[idx];
4151 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4154 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4156 struct clk_hw_onecell_data *hw_data = data;
4157 unsigned int idx = clkspec->args[0];
4159 if (idx >= hw_data->num) {
4160 pr_err("%s: invalid index %u\n", __func__, idx);
4161 return ERR_PTR(-EINVAL);
4164 return hw_data->hws[idx];
4166 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4169 * of_clk_add_provider() - Register a clock provider for a node
4170 * @np: Device node pointer associated with clock provider
4171 * @clk_src_get: callback for decoding clock
4172 * @data: context pointer for @clk_src_get callback.
4174 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
4176 int of_clk_add_provider(struct device_node *np,
4177 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4181 struct of_clk_provider *cp;
4184 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4188 cp->node = of_node_get(np);
4190 cp->get = clk_src_get;
4192 mutex_lock(&of_clk_mutex);
4193 list_add(&cp->link, &of_clk_providers);
4194 mutex_unlock(&of_clk_mutex);
4195 pr_debug("Added clock from %pOF\n", np);
4197 ret = of_clk_set_defaults(np, true);
4199 of_clk_del_provider(np);
4203 EXPORT_SYMBOL_GPL(of_clk_add_provider);
4206 * of_clk_add_hw_provider() - Register a clock provider for a node
4207 * @np: Device node pointer associated with clock provider
4208 * @get: callback for decoding clk_hw
4209 * @data: context pointer for @get callback.
4211 int of_clk_add_hw_provider(struct device_node *np,
4212 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4216 struct of_clk_provider *cp;
4219 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4223 cp->node = of_node_get(np);
4227 mutex_lock(&of_clk_mutex);
4228 list_add(&cp->link, &of_clk_providers);
4229 mutex_unlock(&of_clk_mutex);
4230 pr_debug("Added clk_hw provider from %pOF\n", np);
4232 ret = of_clk_set_defaults(np, true);
4234 of_clk_del_provider(np);
4238 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4240 static void devm_of_clk_release_provider(struct device *dev, void *res)
4242 of_clk_del_provider(*(struct device_node **)res);
4246 * We allow a child device to use its parent device as the clock provider node
4247 * for cases like MFD sub-devices where the child device driver wants to use
4248 * devm_*() APIs but not list the device in DT as a sub-node.
4250 static struct device_node *get_clk_provider_node(struct device *dev)
4252 struct device_node *np, *parent_np;
4255 parent_np = dev->parent ? dev->parent->of_node : NULL;
4257 if (!of_find_property(np, "#clock-cells", NULL))
4258 if (of_find_property(parent_np, "#clock-cells", NULL))
4265 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4266 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4267 * @get: callback for decoding clk_hw
4268 * @data: context pointer for @get callback
4270 * Registers clock provider for given device's node. If the device has no DT
4271 * node or if the device node lacks of clock provider information (#clock-cells)
4272 * then the parent device's node is scanned for this information. If parent node
4273 * has the #clock-cells then it is used in registration. Provider is
4274 * automatically released at device exit.
4276 * Return: 0 on success or an errno on failure.
4278 int devm_of_clk_add_hw_provider(struct device *dev,
4279 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4283 struct device_node **ptr, *np;
4286 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4291 np = get_clk_provider_node(dev);
4292 ret = of_clk_add_hw_provider(np, get, data);
4295 devres_add(dev, ptr);
4302 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4305 * of_clk_del_provider() - Remove a previously registered clock provider
4306 * @np: Device node pointer associated with clock provider
4308 void of_clk_del_provider(struct device_node *np)
4310 struct of_clk_provider *cp;
4312 mutex_lock(&of_clk_mutex);
4313 list_for_each_entry(cp, &of_clk_providers, link) {
4314 if (cp->node == np) {
4315 list_del(&cp->link);
4316 of_node_put(cp->node);
4321 mutex_unlock(&of_clk_mutex);
4323 EXPORT_SYMBOL_GPL(of_clk_del_provider);
4325 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4327 struct device_node **np = res;
4329 if (WARN_ON(!np || !*np))
4336 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4337 * @dev: Device to whose lifetime the clock provider was bound
4339 void devm_of_clk_del_provider(struct device *dev)
4342 struct device_node *np = get_clk_provider_node(dev);
4344 ret = devres_release(dev, devm_of_clk_release_provider,
4345 devm_clk_provider_match, np);
4349 EXPORT_SYMBOL(devm_of_clk_del_provider);
4352 * Beware the return values when np is valid, but no clock provider is found.
4353 * If name == NULL, the function returns -ENOENT.
4354 * If name != NULL, the function returns -EINVAL. This is because
4355 * of_parse_phandle_with_args() is called even if of_property_match_string()
4358 static int of_parse_clkspec(const struct device_node *np, int index,
4359 const char *name, struct of_phandle_args *out_args)
4363 /* Walk up the tree of devices looking for a clock property that matches */
4366 * For named clocks, first look up the name in the
4367 * "clock-names" property. If it cannot be found, then index
4368 * will be an error code and of_parse_phandle_with_args() will
4372 index = of_property_match_string(np, "clock-names", name);
4373 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4377 if (name && index >= 0)
4381 * No matching clock found on this node. If the parent node
4382 * has a "clock-ranges" property, then we can try one of its
4386 if (np && !of_get_property(np, "clock-ranges", NULL))
4394 static struct clk_hw *
4395 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4396 struct of_phandle_args *clkspec)
4400 if (provider->get_hw)
4401 return provider->get_hw(clkspec, provider->data);
4403 clk = provider->get(clkspec, provider->data);
4405 return ERR_CAST(clk);
4406 return __clk_get_hw(clk);
4409 static struct clk_hw *
4410 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
4412 struct of_clk_provider *provider;
4413 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
4416 return ERR_PTR(-EINVAL);
4418 mutex_lock(&of_clk_mutex);
4419 list_for_each_entry(provider, &of_clk_providers, link) {
4420 if (provider->node == clkspec->np) {
4421 hw = __of_clk_get_hw_from_provider(provider, clkspec);
4426 mutex_unlock(&of_clk_mutex);
4432 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4433 * @clkspec: pointer to a clock specifier data structure
4435 * This function looks up a struct clk from the registered list of clock
4436 * providers, an input is a clock specifier data structure as returned
4437 * from the of_parse_phandle_with_args() function call.
4439 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4441 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4443 return clk_hw_create_clk(NULL, hw, NULL, __func__);
4445 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4447 struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4452 struct of_phandle_args clkspec;
4454 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4456 return ERR_PTR(ret);
4458 hw = of_clk_get_hw_from_clkspec(&clkspec);
4459 of_node_put(clkspec.np);
4464 static struct clk *__of_clk_get(struct device_node *np,
4465 int index, const char *dev_id,
4468 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4470 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4473 struct clk *of_clk_get(struct device_node *np, int index)
4475 return __of_clk_get(np, index, np->full_name, NULL);
4477 EXPORT_SYMBOL(of_clk_get);
4480 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4481 * @np: pointer to clock consumer node
4482 * @name: name of consumer's clock input, or NULL for the first clock reference
4484 * This function parses the clocks and clock-names properties,
4485 * and uses them to look up the struct clk from the registered list of clock
4488 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4491 return ERR_PTR(-ENOENT);
4493 return __of_clk_get(np, 0, np->full_name, name);
4495 EXPORT_SYMBOL(of_clk_get_by_name);
4498 * of_clk_get_parent_count() - Count the number of clocks a device node has
4499 * @np: device node to count
4501 * Returns: The number of clocks that are possible parents of this node
4503 unsigned int of_clk_get_parent_count(struct device_node *np)
4507 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4513 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4515 const char *of_clk_get_parent_name(struct device_node *np, int index)
4517 struct of_phandle_args clkspec;
4518 struct property *prop;
4519 const char *clk_name;
4526 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4531 index = clkspec.args_count ? clkspec.args[0] : 0;
4534 /* if there is an indices property, use it to transfer the index
4535 * specified into an array offset for the clock-output-names property.
4537 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4544 /* We went off the end of 'clock-indices' without finding it */
4548 if (of_property_read_string_index(clkspec.np, "clock-output-names",
4552 * Best effort to get the name if the clock has been
4553 * registered with the framework. If the clock isn't
4554 * registered, we return the node name as the name of
4555 * the clock as long as #clock-cells = 0.
4557 clk = of_clk_get_from_provider(&clkspec);
4559 if (clkspec.args_count == 0)
4560 clk_name = clkspec.np->name;
4564 clk_name = __clk_get_name(clk);
4570 of_node_put(clkspec.np);
4573 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4576 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4578 * @np: Device node pointer associated with clock provider
4579 * @parents: pointer to char array that hold the parents' names
4580 * @size: size of the @parents array
4582 * Return: number of parents for the clock node.
4584 int of_clk_parent_fill(struct device_node *np, const char **parents,
4589 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4594 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4596 struct clock_provider {
4597 void (*clk_init_cb)(struct device_node *);
4598 struct device_node *np;
4599 struct list_head node;
4603 * This function looks for a parent clock. If there is one, then it
4604 * checks that the provider for this parent clock was initialized, in
4605 * this case the parent clock will be ready.
4607 static int parent_ready(struct device_node *np)
4612 struct clk *clk = of_clk_get(np, i);
4614 /* this parent is ready we can check the next one */
4621 /* at least one parent is not ready, we exit now */
4622 if (PTR_ERR(clk) == -EPROBE_DEFER)
4626 * Here we make assumption that the device tree is
4627 * written correctly. So an error means that there is
4628 * no more parent. As we didn't exit yet, then the
4629 * previous parent are ready. If there is no clock
4630 * parent, no need to wait for them, then we can
4631 * consider their absence as being ready
4638 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4639 * @np: Device node pointer associated with clock provider
4640 * @index: clock index
4641 * @flags: pointer to top-level framework flags
4643 * Detects if the clock-critical property exists and, if so, sets the
4644 * corresponding CLK_IS_CRITICAL flag.
4646 * Do not use this function. It exists only for legacy Device Tree
4647 * bindings, such as the one-clock-per-node style that are outdated.
4648 * Those bindings typically put all clock data into .dts and the Linux
4649 * driver has no clock data, thus making it impossible to set this flag
4650 * correctly from the driver. Only those drivers may call
4651 * of_clk_detect_critical from their setup functions.
4653 * Return: error code or zero on success
4655 int of_clk_detect_critical(struct device_node *np,
4656 int index, unsigned long *flags)
4658 struct property *prop;
4665 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4667 *flags |= CLK_IS_CRITICAL;
4673 * of_clk_init() - Scan and init clock providers from the DT
4674 * @matches: array of compatible values and init functions for providers.
4676 * This function scans the device tree for matching clock providers
4677 * and calls their initialization functions. It also does it by trying
4678 * to follow the dependencies.
4680 void __init of_clk_init(const struct of_device_id *matches)
4682 const struct of_device_id *match;
4683 struct device_node *np;
4684 struct clock_provider *clk_provider, *next;
4687 LIST_HEAD(clk_provider_list);
4690 matches = &__clk_of_table;
4692 /* First prepare the list of the clocks providers */
4693 for_each_matching_node_and_match(np, matches, &match) {
4694 struct clock_provider *parent;
4696 if (!of_device_is_available(np))
4699 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4701 list_for_each_entry_safe(clk_provider, next,
4702 &clk_provider_list, node) {
4703 list_del(&clk_provider->node);
4704 of_node_put(clk_provider->np);
4705 kfree(clk_provider);
4711 parent->clk_init_cb = match->data;
4712 parent->np = of_node_get(np);
4713 list_add_tail(&parent->node, &clk_provider_list);
4716 while (!list_empty(&clk_provider_list)) {
4717 is_init_done = false;
4718 list_for_each_entry_safe(clk_provider, next,
4719 &clk_provider_list, node) {
4720 if (force || parent_ready(clk_provider->np)) {
4722 /* Don't populate platform devices */
4723 of_node_set_flag(clk_provider->np,
4726 clk_provider->clk_init_cb(clk_provider->np);
4727 of_clk_set_defaults(clk_provider->np, true);
4729 list_del(&clk_provider->node);
4730 of_node_put(clk_provider->np);
4731 kfree(clk_provider);
4732 is_init_done = true;
4737 * We didn't manage to initialize any of the
4738 * remaining providers during the last loop, so now we
4739 * initialize all the remaining ones unconditionally
4740 * in case the clock parent was not mandatory