Lines Matching +full:clk +full:- +full:phase +full:-
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
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
25 #include "clk.h"
81 int phase; member
95 #include <trace/events/clk.h>
97 struct clk { struct
113 if (!core->rpm_enabled) in clk_pm_runtime_get()
116 ret = pm_runtime_get_sync(core->dev); in clk_pm_runtime_get()
118 pm_runtime_put_noidle(core->dev); in clk_pm_runtime_get()
126 if (!core->rpm_enabled) in clk_pm_runtime_put()
129 pm_runtime_put_sync(core->dev); in clk_pm_runtime_put()
153 if (--prepare_refcnt) in clk_prepare_unlock()
193 if (--enable_refcnt) { in clk_enable_unlock()
203 return core->protect_count; in clk_core_rate_is_protected()
214 if (!core->ops->is_prepared) in clk_core_is_prepared()
215 return core->prepare_count; in clk_core_is_prepared()
218 ret = core->ops->is_prepared(core->hw); in clk_core_is_prepared()
233 if (!core->ops->is_enabled) in clk_core_is_enabled()
234 return core->enable_count; in clk_core_is_enabled()
246 if (core->rpm_enabled) { in clk_core_is_enabled()
247 pm_runtime_get_noresume(core->dev); in clk_core_is_enabled()
248 if (!pm_runtime_active(core->dev)) { in clk_core_is_enabled()
259 if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent) in clk_core_is_enabled()
260 if (!clk_core_is_enabled(core->parent)) { in clk_core_is_enabled()
265 ret = core->ops->is_enabled(core->hw); in clk_core_is_enabled()
267 if (core->rpm_enabled) in clk_core_is_enabled()
268 pm_runtime_put(core->dev); in clk_core_is_enabled()
275 const char *__clk_get_name(const struct clk *clk) in __clk_get_name() argument
277 return !clk ? NULL : clk->core->name; in __clk_get_name()
283 return hw->core->name; in clk_hw_get_name()
287 struct clk_hw *__clk_get_hw(struct clk *clk) in __clk_get_hw() argument
289 return !clk ? NULL : clk->core->hw; in __clk_get_hw()
295 return hw->core->num_parents; in clk_hw_get_num_parents()
301 return hw->core->parent ? hw->core->parent->hw : NULL; in clk_hw_get_parent()
311 if (!strcmp(core->name, name)) in __clk_lookup_subtree()
314 hlist_for_each_entry(child, &core->children, child_node) { in __clk_lookup_subtree()
331 /* search the 'proper' clk tree first */ in clk_core_lookup()
358 return -ENOENT; in of_parse_clkspec()
363 return ERR_PTR(-ENOENT); in of_clk_get_hw_from_clkspec()
368 * clk_core_get - Find the clk_core parent of a clk
369 * @core: clk to find parent of
372 * This is the preferred method for clk providers to find the parent of a
373 * clk when that parent is external to the clk controller. The parent_names
375 * node's 'clock-names' property or as the 'con_id' matching the device's
376 * dev_name() in a clk_lookup. This allows clk providers to use their own
380 * clock-controller@c001 that has a clk_init_data::parent_data array
382 * clock-controller@f00abcd without needing to get the globally unique name of
383 * the xtal clk.
385 * parent: clock-controller@f00abcd {
387 * #clock-cells = <0>;
390 * clock-controller@c001 {
393 * clock-names = "xtal";
394 * #clock-cells = <1>;
397 * Returns: -ENOENT when the provider can't be found or the clk doesn't
399 * in a clkdev lookup. NULL when the provider knows about the clk but it
401 * A valid clk_core pointer when the clk can be found in the provider.
405 const char *name = core->parents[p_index].fw_name; in clk_core_get()
406 int index = core->parents[p_index].index; in clk_core_get()
407 struct clk_hw *hw = ERR_PTR(-ENOENT); in clk_core_get()
408 struct device *dev = core->dev; in clk_core_get()
410 struct device_node *np = core->of_node; in clk_core_get()
428 return hw->core; in clk_core_get()
433 struct clk_parent_map *entry = &core->parents[index]; in clk_core_fill_parent_index()
434 struct clk_core *parent = ERR_PTR(-ENOENT); in clk_core_fill_parent_index()
436 if (entry->hw) { in clk_core_fill_parent_index()
437 parent = entry->hw->core; in clk_core_fill_parent_index()
444 parent = ERR_PTR(-EPROBE_DEFER); in clk_core_fill_parent_index()
447 if (PTR_ERR(parent) == -ENOENT && entry->name) in clk_core_fill_parent_index()
448 parent = clk_core_lookup(entry->name); in clk_core_fill_parent_index()
453 entry->core = parent; in clk_core_fill_parent_index()
459 if (!core || index >= core->num_parents || !core->parents) in clk_core_get_parent_by_index()
462 if (!core->parents[index].core) in clk_core_get_parent_by_index()
465 return core->parents[index].core; in clk_core_get_parent_by_index()
473 parent = clk_core_get_parent_by_index(hw->core, index); in clk_hw_get_parent_by_index()
475 return !parent ? NULL : parent->hw; in clk_hw_get_parent_by_index()
479 unsigned int __clk_get_enable_count(struct clk *clk) in __clk_get_enable_count() argument
481 return !clk ? 0 : clk->core->enable_count; in __clk_get_enable_count()
489 if (!core->num_parents || core->parent) in clk_core_get_rate_nolock()
490 return core->rate; in clk_core_get_rate_nolock()
493 * Clk must have a parent because num_parents > 0 but the parent isn't in clk_core_get_rate_nolock()
494 * known yet. Best to return 0 as the rate of this clk until we can in clk_core_get_rate_nolock()
502 return clk_core_get_rate_nolock(hw->core); in clk_hw_get_rate()
511 return core->accuracy; in clk_core_get_accuracy_no_lock()
516 return hw->core->flags; in clk_hw_get_flags()
522 return clk_core_is_prepared(hw->core); in clk_hw_is_prepared()
528 return clk_core_rate_is_protected(hw->core); in clk_hw_rate_is_protected()
534 return clk_core_is_enabled(hw->core); in clk_hw_is_enabled()
538 bool __clk_is_enabled(struct clk *clk) in __clk_is_enabled() argument
540 if (!clk) in __clk_is_enabled()
543 return clk_core_is_enabled(clk->core); in __clk_is_enabled()
551 return abs(now - rate) < abs(best - rate); in mux_is_better_rate()
560 struct clk_core *core = hw->core, *parent, *best_parent = NULL; in clk_mux_determine_rate_flags()
566 if (core->flags & CLK_SET_RATE_NO_REPARENT) { in clk_mux_determine_rate_flags()
567 parent = core->parent; in clk_mux_determine_rate_flags()
568 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
569 ret = __clk_determine_rate(parent ? parent->hw : NULL, in clk_mux_determine_rate_flags()
585 num_parents = core->num_parents; in clk_mux_determine_rate_flags()
591 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
593 ret = __clk_determine_rate(parent->hw, &parent_req); in clk_mux_determine_rate_flags()
600 if (mux_is_better_rate(req->rate, parent_req.rate, in clk_mux_determine_rate_flags()
608 return -EINVAL; in clk_mux_determine_rate_flags()
612 req->best_parent_hw = best_parent->hw; in clk_mux_determine_rate_flags()
613 req->best_parent_rate = best; in clk_mux_determine_rate_flags()
614 req->rate = best; in clk_mux_determine_rate_flags()
620 struct clk *__clk_lookup(const char *name) in __clk_lookup()
624 return !core ? NULL : core->hw->clk; in __clk_lookup()
631 struct clk *clk_user; in clk_core_get_boundaries()
635 *min_rate = core->min_rate; in clk_core_get_boundaries()
636 *max_rate = core->max_rate; in clk_core_get_boundaries()
638 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
639 *min_rate = max(*min_rate, clk_user->min_rate); in clk_core_get_boundaries()
641 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
642 *max_rate = min(*max_rate, clk_user->max_rate); in clk_core_get_boundaries()
649 struct clk *user; in clk_core_check_boundaries()
653 if (min_rate > core->max_rate || max_rate < core->min_rate) in clk_core_check_boundaries()
656 hlist_for_each_entry(user, &core->clks, clks_node) in clk_core_check_boundaries()
657 if (min_rate > user->max_rate || max_rate < user->min_rate) in clk_core_check_boundaries()
666 hw->core->min_rate = min_rate; in clk_hw_set_rate_range()
667 hw->core->max_rate = max_rate; in clk_hw_set_rate_range()
672 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
673 * @hw: mux type clk to determine rate on
680 * Returns: 0 on success, -EERROR value on error
696 /*** clk api ***/
705 if (WARN(core->protect_count == 0, in clk_core_rate_unprotect()
706 "%s already unprotected\n", core->name)) in clk_core_rate_unprotect()
709 if (--core->protect_count > 0) in clk_core_rate_unprotect()
712 clk_core_rate_unprotect(core->parent); in clk_core_rate_unprotect()
722 return -EINVAL; in clk_core_rate_nuke_protect()
724 if (core->protect_count == 0) in clk_core_rate_nuke_protect()
727 ret = core->protect_count; in clk_core_rate_nuke_protect()
728 core->protect_count = 1; in clk_core_rate_nuke_protect()
735 * clk_rate_exclusive_put - release exclusivity over clock rate control
736 * @clk: the clk over which the exclusivity is released
752 void clk_rate_exclusive_put(struct clk *clk) in clk_rate_exclusive_put() argument
754 if (!clk) in clk_rate_exclusive_put()
763 if (WARN_ON(clk->exclusive_count <= 0)) in clk_rate_exclusive_put()
766 clk_core_rate_unprotect(clk->core); in clk_rate_exclusive_put()
767 clk->exclusive_count--; in clk_rate_exclusive_put()
780 if (core->protect_count == 0) in clk_core_rate_protect()
781 clk_core_rate_protect(core->parent); in clk_core_rate_protect()
783 core->protect_count++; in clk_core_rate_protect()
797 core->protect_count = count; in clk_core_rate_restore_protect()
801 * clk_rate_exclusive_get - get exclusivity over the clk rate control
802 * @clk: the clk over which the exclusity of rate control is requested
816 * Returns 0 on success, -EERROR otherwise
818 int clk_rate_exclusive_get(struct clk *clk) in clk_rate_exclusive_get() argument
820 if (!clk) in clk_rate_exclusive_get()
824 clk_core_rate_protect(clk->core); in clk_rate_exclusive_get()
825 clk->exclusive_count++; in clk_rate_exclusive_get()
839 if (WARN(core->prepare_count == 0, in clk_core_unprepare()
840 "%s already unprepared\n", core->name)) in clk_core_unprepare()
843 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_unprepare()
844 "Unpreparing critical %s\n", core->name)) in clk_core_unprepare()
847 if (core->flags & CLK_SET_RATE_GATE) in clk_core_unprepare()
850 if (--core->prepare_count > 0) in clk_core_unprepare()
853 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name); in clk_core_unprepare()
857 if (core->ops->unprepare) in clk_core_unprepare()
858 core->ops->unprepare(core->hw); in clk_core_unprepare()
861 clk_core_unprepare(core->parent); in clk_core_unprepare()
873 * clk_unprepare - undo preparation of a clock source
874 * @clk: the clk being unprepared
877 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
878 * if the operation may sleep. One example is a clk which is accessed over
879 * I2c. In the complex case a clk gate operation may require a fast and a slow
883 void clk_unprepare(struct clk *clk) in clk_unprepare() argument
885 if (IS_ERR_OR_NULL(clk)) in clk_unprepare()
888 clk_core_unprepare_lock(clk->core); in clk_unprepare()
901 if (core->prepare_count == 0) { in clk_core_prepare()
906 ret = clk_core_prepare(core->parent); in clk_core_prepare()
912 if (core->ops->prepare) in clk_core_prepare()
913 ret = core->ops->prepare(core->hw); in clk_core_prepare()
921 core->prepare_count++; in clk_core_prepare()
930 if (core->flags & CLK_SET_RATE_GATE) in clk_core_prepare()
935 clk_core_unprepare(core->parent); in clk_core_prepare()
953 * clk_prepare - prepare a clock source
954 * @clk: the clk being prepared
957 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
958 * operation may sleep. One example is a clk which is accessed over I2c. In
959 * the complex case a clk ungate operation may require a fast and a slow part.
962 * Returns 0 on success, -EERROR otherwise.
964 int clk_prepare(struct clk *clk) in clk_prepare() argument
966 if (!clk) in clk_prepare()
969 return clk_core_prepare_lock(clk->core); in clk_prepare()
980 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name)) in clk_core_disable()
983 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_disable()
984 "Disabling critical %s\n", core->name)) in clk_core_disable()
987 if (--core->enable_count > 0) in clk_core_disable()
992 if (core->ops->disable) in clk_core_disable()
993 core->ops->disable(core->hw); in clk_core_disable()
997 clk_core_disable(core->parent); in clk_core_disable()
1010 * clk_disable - gate a clock
1011 * @clk: the clk being gated
1015 * clk if the operation is fast and will never sleep. One example is a
1016 * SoC-internal clk which is controlled via simple register writes. In the
1017 * complex case a clk gate operation may require a fast and a slow part. It is
1021 void clk_disable(struct clk *clk) in clk_disable() argument
1023 if (IS_ERR_OR_NULL(clk)) in clk_disable()
1026 clk_core_disable_lock(clk->core); in clk_disable()
1039 if (WARN(core->prepare_count == 0, in clk_core_enable()
1040 "Enabling unprepared %s\n", core->name)) in clk_core_enable()
1041 return -ESHUTDOWN; in clk_core_enable()
1043 if (core->enable_count == 0) { in clk_core_enable()
1044 ret = clk_core_enable(core->parent); in clk_core_enable()
1051 if (core->ops->enable) in clk_core_enable()
1052 ret = core->ops->enable(core->hw); in clk_core_enable()
1057 clk_core_disable(core->parent); in clk_core_enable()
1062 core->enable_count++; in clk_core_enable()
1079 * clk_gate_restore_context - restore context for poweroff
1090 struct clk_core *core = hw->core; in clk_gate_restore_context()
1092 if (core->enable_count) in clk_gate_restore_context()
1093 core->ops->enable(hw); in clk_gate_restore_context()
1095 core->ops->disable(hw); in clk_gate_restore_context()
1104 hlist_for_each_entry(child, &core->children, child_node) { in clk_core_save_context()
1110 if (core->ops && core->ops->save_context) in clk_core_save_context()
1111 ret = core->ops->save_context(core->hw); in clk_core_save_context()
1120 if (core->ops && core->ops->restore_context) in clk_core_restore_context()
1121 core->ops->restore_context(core->hw); in clk_core_restore_context()
1123 hlist_for_each_entry(child, &core->children, child_node) in clk_core_restore_context()
1128 * clk_save_context - save clock context for poweroff
1136 struct clk_core *clk; in clk_save_context() local
1139 hlist_for_each_entry(clk, &clk_root_list, child_node) { in clk_save_context()
1140 ret = clk_core_save_context(clk); in clk_save_context()
1145 hlist_for_each_entry(clk, &clk_orphan_list, child_node) { in clk_save_context()
1146 ret = clk_core_save_context(clk); in clk_save_context()
1156 * clk_restore_context - restore clock context after poweroff
1174 * clk_enable - ungate a clock
1175 * @clk: the clk being ungated
1178 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1179 * if the operation will never sleep. One example is a SoC-internal clk which
1180 * is controlled via simple register writes. In the complex case a clk ungate
1183 * must be called before clk_enable. Returns 0 on success, -EERROR
1186 int clk_enable(struct clk *clk) in clk_enable() argument
1188 if (!clk) in clk_enable()
1191 return clk_core_enable_lock(clk->core); in clk_enable()
1222 hlist_for_each_entry(child, &core->children, child_node) in clk_unprepare_unused_subtree()
1225 if (core->prepare_count) in clk_unprepare_unused_subtree()
1228 if (core->flags & CLK_IGNORE_UNUSED) in clk_unprepare_unused_subtree()
1236 if (core->ops->unprepare_unused) in clk_unprepare_unused_subtree()
1237 core->ops->unprepare_unused(core->hw); in clk_unprepare_unused_subtree()
1238 else if (core->ops->unprepare) in clk_unprepare_unused_subtree()
1239 core->ops->unprepare(core->hw); in clk_unprepare_unused_subtree()
1253 hlist_for_each_entry(child, &core->children, child_node) in clk_disable_unused_subtree()
1256 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1257 clk_core_prepare_enable(core->parent); in clk_disable_unused_subtree()
1264 if (core->enable_count) in clk_disable_unused_subtree()
1267 if (core->flags & CLK_IGNORE_UNUSED) in clk_disable_unused_subtree()
1271 * some gate clocks have special needs during the disable-unused in clk_disable_unused_subtree()
1277 if (core->ops->disable_unused) in clk_disable_unused_subtree()
1278 core->ops->disable_unused(core->hw); in clk_disable_unused_subtree()
1279 else if (core->ops->disable) in clk_disable_unused_subtree()
1280 core->ops->disable(core->hw); in clk_disable_unused_subtree()
1288 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1289 clk_core_disable_unprepare(core->parent); in clk_disable_unused_subtree()
1305 pr_warn("clk: Not disabling unused clocks\n"); in clk_disable_unused()
1341 * - if the provider is not protected at all in clk_core_determine_round_nolock()
1342 * - if the calling consumer is the only one which has exclusivity in clk_core_determine_round_nolock()
1346 req->rate = core->rate; in clk_core_determine_round_nolock()
1347 } else if (core->ops->determine_rate) { in clk_core_determine_round_nolock()
1348 return core->ops->determine_rate(core->hw, req); in clk_core_determine_round_nolock()
1349 } else if (core->ops->round_rate) { in clk_core_determine_round_nolock()
1350 rate = core->ops->round_rate(core->hw, req->rate, in clk_core_determine_round_nolock()
1351 &req->best_parent_rate); in clk_core_determine_round_nolock()
1355 req->rate = rate; in clk_core_determine_round_nolock()
1357 return -EINVAL; in clk_core_determine_round_nolock()
1371 parent = core->parent; in clk_core_init_rate_req()
1373 req->best_parent_hw = parent->hw; in clk_core_init_rate_req()
1374 req->best_parent_rate = parent->rate; in clk_core_init_rate_req()
1376 req->best_parent_hw = NULL; in clk_core_init_rate_req()
1377 req->best_parent_rate = 0; in clk_core_init_rate_req()
1383 return core->ops->determine_rate || core->ops->round_rate; in clk_core_can_round()
1392 req->rate = 0; in clk_core_round_rate_nolock()
1400 else if (core->flags & CLK_SET_RATE_PARENT) in clk_core_round_rate_nolock()
1401 return clk_core_round_rate_nolock(core->parent, req); in clk_core_round_rate_nolock()
1403 req->rate = core->rate; in clk_core_round_rate_nolock()
1408 * __clk_determine_rate - get the closest rate actually supported by a clock
1417 req->rate = 0; in __clk_determine_rate()
1421 return clk_core_round_rate_nolock(hw->core, req); in __clk_determine_rate()
1426 * clk_hw_round_rate() - round the given rate for a hw clk
1427 * @hw: the hw clk for which we are rounding a rate
1430 * Takes in a rate as input and rounds it to a rate that the clk can actually
1434 * For clk providers to call from within clk_ops such as .round_rate,
1437 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1445 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); in clk_hw_round_rate()
1448 ret = clk_core_round_rate_nolock(hw->core, &req); in clk_hw_round_rate()
1457 * clk_round_rate - round the given rate for a clk
1458 * @clk: the clk for which we are rounding a rate
1461 * Takes in a rate as input and rounds it to a rate that the clk can actually
1462 * use which is then returned. If clk doesn't support round_rate operation
1465 long clk_round_rate(struct clk *clk, unsigned long rate) in clk_round_rate() argument
1470 if (!clk) in clk_round_rate()
1475 if (clk->exclusive_count) in clk_round_rate()
1476 clk_core_rate_unprotect(clk->core); in clk_round_rate()
1478 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); in clk_round_rate()
1481 ret = clk_core_round_rate_nolock(clk->core, &req); in clk_round_rate()
1483 if (clk->exclusive_count) in clk_round_rate()
1484 clk_core_rate_protect(clk->core); in clk_round_rate()
1496 * __clk_notify - call clk notifier chain
1497 * @core: clk that is changing rate
1498 * @msg: clk notifier type (see include/linux/clk.h)
1499 * @old_rate: old clk rate
1500 * @new_rate: new clk rate
1502 * Triggers a notifier call chain on the clk rate-change notification
1503 * for 'clk'. Passes a pointer to the struct clk and the previous
1520 if (cn->clk->core == core) { in __clk_notify()
1521 cnd.clk = cn->clk; in __clk_notify()
1522 ret = srcu_notifier_call_chain(&cn->notifier_head, msg, in __clk_notify()
1534 * @core: first clk in the subtree
1536 * Walks the subtree of clks starting with clk and recalculates accuracies as
1537 * it goes. Note that if a clk does not implement the .recalc_accuracy
1548 if (core->parent) in __clk_recalc_accuracies()
1549 parent_accuracy = core->parent->accuracy; in __clk_recalc_accuracies()
1551 if (core->ops->recalc_accuracy) in __clk_recalc_accuracies()
1552 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_recalc_accuracies()
1555 core->accuracy = parent_accuracy; in __clk_recalc_accuracies()
1557 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_accuracies()
1563 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) in clk_core_get_accuracy_recalc()
1570 * clk_get_accuracy - return the accuracy of clk
1571 * @clk: the clk whose accuracy is being returned
1573 * Simply returns the cached accuracy of the clk, unless
1576 * If clk is NULL then returns 0.
1578 long clk_get_accuracy(struct clk *clk) in clk_get_accuracy() argument
1582 if (!clk) in clk_get_accuracy()
1586 accuracy = clk_core_get_accuracy_recalc(clk->core); in clk_get_accuracy()
1598 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { in clk_recalc()
1599 rate = core->ops->recalc_rate(core->hw, parent_rate); in clk_recalc()
1607 * @core: first clk in the subtree
1608 * @msg: notification type (see include/linux/clk.h)
1610 * Walks the subtree of clks starting with clk and recalculates rates as it
1611 * goes. Note that if a clk does not implement the .recalc_rate callback then
1625 old_rate = core->rate; in __clk_recalc_rates()
1627 if (core->parent) in __clk_recalc_rates()
1628 parent_rate = core->parent->rate; in __clk_recalc_rates()
1630 core->rate = clk_recalc(core, parent_rate); in __clk_recalc_rates()
1636 if (core->notifier_count && msg) in __clk_recalc_rates()
1637 __clk_notify(core, msg, old_rate, core->rate); in __clk_recalc_rates()
1639 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_rates()
1645 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) in clk_core_get_rate_recalc()
1652 * clk_get_rate - return the rate of clk
1653 * @clk: the clk whose rate is being returned
1655 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1657 * If clk is NULL then returns 0.
1659 unsigned long clk_get_rate(struct clk *clk) in clk_get_rate() argument
1663 if (!clk) in clk_get_rate()
1667 rate = clk_core_get_rate_recalc(clk->core); in clk_get_rate()
1680 return -EINVAL; in clk_fetch_parent_index()
1682 for (i = 0; i < core->num_parents; i++) { in clk_fetch_parent_index()
1684 if (core->parents[i].core == parent) in clk_fetch_parent_index()
1688 if (core->parents[i].core) in clk_fetch_parent_index()
1692 if (core->parents[i].hw) { in clk_fetch_parent_index()
1693 if (core->parents[i].hw == parent->hw) in clk_fetch_parent_index()
1705 if (core->parents[i].name && in clk_fetch_parent_index()
1706 !strcmp(parent->name, core->parents[i].name)) in clk_fetch_parent_index()
1710 if (i == core->num_parents) in clk_fetch_parent_index()
1711 return -EINVAL; in clk_fetch_parent_index()
1713 core->parents[i].core = parent; in clk_fetch_parent_index()
1718 * clk_hw_get_parent_index - return the index of the parent clock
1719 * @hw: clk_hw associated with the clk being consumed
1721 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1729 return -EINVAL; in clk_hw_get_parent_index()
1731 return clk_fetch_parent_index(hw->core, parent->core); in clk_hw_get_parent_index()
1742 core->orphan = is_orphan; in clk_core_update_orphan_status()
1744 hlist_for_each_entry(child, &core->children, child_node) in clk_core_update_orphan_status()
1750 bool was_orphan = core->orphan; in clk_reparent()
1752 hlist_del(&core->child_node); in clk_reparent()
1755 bool becomes_orphan = new_parent->orphan; in clk_reparent()
1758 if (new_parent->new_child == core) in clk_reparent()
1759 new_parent->new_child = NULL; in clk_reparent()
1761 hlist_add_head(&core->child_node, &new_parent->children); in clk_reparent()
1766 hlist_add_head(&core->child_node, &clk_orphan_list); in clk_reparent()
1771 core->parent = new_parent; in clk_reparent()
1778 struct clk_core *old_parent = core->parent; in __clk_set_parent_before()
1801 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_before()
1807 if (core->prepare_count) { in __clk_set_parent_before()
1812 /* update the clk tree topology */ in __clk_set_parent_before()
1828 if (core->prepare_count) { in __clk_set_parent_after()
1833 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ in __clk_set_parent_after()
1834 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_after()
1852 if (parent && core->ops->set_parent) in __clk_set_parent()
1853 ret = core->ops->set_parent(core->hw, p_index); in __clk_set_parent()
1873 * @core: first clk in the subtree
1874 * @parent_rate: the "future" rate of clk's parent
1876 * Walks the subtree of clks starting with clk, speculating rates as it
1880 * pre-rate change notifications and returns early if no clks in the
1881 * subtree have subscribed to the notifications. Note that if a clk does not
1897 if (core->notifier_count) in __clk_speculate_rates()
1898 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); in __clk_speculate_rates()
1901 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", in __clk_speculate_rates()
1902 __func__, core->name, ret); in __clk_speculate_rates()
1906 hlist_for_each_entry(child, &core->children, child_node) { in __clk_speculate_rates()
1921 core->new_rate = new_rate; in clk_calc_subtree()
1922 core->new_parent = new_parent; in clk_calc_subtree()
1923 core->new_parent_index = p_index; in clk_calc_subtree()
1924 /* include clk in new parent's PRE_RATE_CHANGE notifications */ in clk_calc_subtree()
1925 core->new_child = NULL; in clk_calc_subtree()
1926 if (new_parent && new_parent != core->parent) in clk_calc_subtree()
1927 new_parent->new_child = core; in clk_calc_subtree()
1929 hlist_for_each_entry(child, &core->children, child_node) { in clk_calc_subtree()
1930 child->new_rate = clk_recalc(child, new_rate); in clk_calc_subtree()
1931 clk_calc_subtree(child, child->new_rate, NULL, 0); in clk_calc_subtree()
1956 parent = old_parent = core->parent; in clk_calc_new_rates()
1958 best_parent_rate = parent->rate; in clk_calc_new_rates()
1962 /* find the closest rate and parent clk/rate */ in clk_calc_new_rates()
1978 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; in clk_calc_new_rates()
1982 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { in clk_calc_new_rates()
1983 /* pass-through clock without adjustable parent */ in clk_calc_new_rates()
1984 core->new_rate = core->rate; in clk_calc_new_rates()
1987 /* pass-through clock with adjustable parent */ in clk_calc_new_rates()
1989 new_rate = parent->new_rate; in clk_calc_new_rates()
1995 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { in clk_calc_new_rates()
1997 __func__, core->name); in clk_calc_new_rates()
2002 if (parent && core->num_parents > 1) { in clk_calc_new_rates()
2005 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_calc_new_rates()
2006 __func__, parent->name, core->name); in clk_calc_new_rates()
2011 if ((core->flags & CLK_SET_RATE_PARENT) && parent && in clk_calc_new_rates()
2012 best_parent_rate != parent->rate) in clk_calc_new_rates()
2032 if (core->rate == core->new_rate) in clk_propagate_rate_change()
2035 if (core->notifier_count) { in clk_propagate_rate_change()
2036 ret = __clk_notify(core, event, core->rate, core->new_rate); in clk_propagate_rate_change()
2041 hlist_for_each_entry(child, &core->children, child_node) { in clk_propagate_rate_change()
2043 if (child->new_parent && child->new_parent != core) in clk_propagate_rate_change()
2050 /* handle the new child who might not be in core->children yet */ in clk_propagate_rate_change()
2051 if (core->new_child) { in clk_propagate_rate_change()
2052 tmp_clk = clk_propagate_rate_change(core->new_child, event); in clk_propagate_rate_change()
2074 old_rate = core->rate; in clk_change_rate()
2076 if (core->new_parent) { in clk_change_rate()
2077 parent = core->new_parent; in clk_change_rate()
2078 best_parent_rate = core->new_parent->rate; in clk_change_rate()
2079 } else if (core->parent) { in clk_change_rate()
2080 parent = core->parent; in clk_change_rate()
2081 best_parent_rate = core->parent->rate; in clk_change_rate()
2087 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2096 if (core->new_parent && core->new_parent != core->parent) { in clk_change_rate()
2097 old_parent = __clk_set_parent_before(core, core->new_parent); in clk_change_rate()
2098 trace_clk_set_parent(core, core->new_parent); in clk_change_rate()
2100 if (core->ops->set_rate_and_parent) { in clk_change_rate()
2102 core->ops->set_rate_and_parent(core->hw, core->new_rate, in clk_change_rate()
2104 core->new_parent_index); in clk_change_rate()
2105 } else if (core->ops->set_parent) { in clk_change_rate()
2106 core->ops->set_parent(core->hw, core->new_parent_index); in clk_change_rate()
2109 trace_clk_set_parent_complete(core, core->new_parent); in clk_change_rate()
2110 __clk_set_parent_after(core, core->new_parent, old_parent); in clk_change_rate()
2113 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2116 trace_clk_set_rate(core, core->new_rate); in clk_change_rate()
2118 if (!skip_set_rate && core->ops->set_rate) in clk_change_rate()
2119 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); in clk_change_rate()
2121 trace_clk_set_rate_complete(core, core->new_rate); in clk_change_rate()
2123 core->rate = clk_recalc(core, best_parent_rate); in clk_change_rate()
2125 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2134 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2137 if (core->notifier_count && old_rate != core->rate) in clk_change_rate()
2138 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); in clk_change_rate()
2140 if (core->flags & CLK_RECALC_NEW_RATES) in clk_change_rate()
2141 (void)clk_calc_new_rates(core, core->new_rate); in clk_change_rate()
2147 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { in clk_change_rate()
2149 if (child->new_parent && child->new_parent != core) in clk_change_rate()
2154 /* handle the new child who might not be in core->children yet */ in clk_change_rate()
2155 if (core->new_child) in clk_change_rate()
2156 clk_change_rate(core->new_child); in clk_change_rate()
2206 return -EBUSY; in clk_core_set_rate_nolock()
2211 return -EINVAL; in clk_core_set_rate_nolock()
2221 fail_clk->name); in clk_core_set_rate_nolock()
2223 ret = -EBUSY; in clk_core_set_rate_nolock()
2230 core->req_rate = req_rate; in clk_core_set_rate_nolock()
2238 * clk_set_rate - specify a new rate for clk
2239 * @clk: the clk whose rate is being changed
2240 * @rate: the new rate for clk
2242 * In the simplest case clk_set_rate will only adjust the rate of clk.
2245 * propagate up to clk's parent; whether or not this happens depends on the
2246 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2248 * *parent_rate comes back with a new rate for clk's parent then we propagate
2249 * up to clk's parent and set its rate. Upward propagation will continue
2250 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2251 * .round_rate stops requesting changes to clk's parent_rate.
2256 * Returns 0 on success, -EERROR otherwise.
2258 int clk_set_rate(struct clk *clk, unsigned long rate) in clk_set_rate() argument
2262 if (!clk) in clk_set_rate()
2268 if (clk->exclusive_count) in clk_set_rate()
2269 clk_core_rate_unprotect(clk->core); in clk_set_rate()
2271 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate()
2273 if (clk->exclusive_count) in clk_set_rate()
2274 clk_core_rate_protect(clk->core); in clk_set_rate()
2283 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2284 * @clk: the clk whose rate is being changed
2285 * @rate: the new rate for clk
2299 * Returns 0 on success, -EERROR otherwise.
2301 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) in clk_set_rate_exclusive() argument
2305 if (!clk) in clk_set_rate_exclusive()
2317 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_exclusive()
2319 clk_core_rate_protect(clk->core); in clk_set_rate_exclusive()
2320 clk->exclusive_count++; in clk_set_rate_exclusive()
2330 * clk_set_rate_range - set a rate range for a clock source
2331 * @clk: clock source
2337 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) in clk_set_rate_range() argument
2342 if (!clk) in clk_set_rate_range()
2346 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", in clk_set_rate_range()
2347 __func__, clk->core->name, clk->dev_id, clk->con_id, in clk_set_rate_range()
2349 return -EINVAL; in clk_set_rate_range()
2354 if (clk->exclusive_count) in clk_set_rate_range()
2355 clk_core_rate_unprotect(clk->core); in clk_set_rate_range()
2358 old_min = clk->min_rate; in clk_set_rate_range()
2359 old_max = clk->max_rate; in clk_set_rate_range()
2360 clk->min_rate = min; in clk_set_rate_range()
2361 clk->max_rate = max; in clk_set_rate_range()
2363 if (!clk_core_check_boundaries(clk->core, min, max)) { in clk_set_rate_range()
2364 ret = -EINVAL; in clk_set_rate_range()
2368 rate = clk_core_get_rate_nolock(clk->core); in clk_set_rate_range()
2377 * - round_rate() was not favorable and fell on the wrong in clk_set_rate_range()
2379 * - the determine_rate() callback does not really check for in clk_set_rate_range()
2388 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_range()
2391 clk->min_rate = old_min; in clk_set_rate_range()
2392 clk->max_rate = old_max; in clk_set_rate_range()
2397 if (clk->exclusive_count) in clk_set_rate_range()
2398 clk_core_rate_protect(clk->core); in clk_set_rate_range()
2407 * clk_set_min_rate - set a minimum clock rate for a clock source
2408 * @clk: clock source
2413 int clk_set_min_rate(struct clk *clk, unsigned long rate) in clk_set_min_rate() argument
2415 if (!clk) in clk_set_min_rate()
2418 return clk_set_rate_range(clk, rate, clk->max_rate); in clk_set_min_rate()
2423 * clk_set_max_rate - set a maximum clock rate for a clock source
2424 * @clk: clock source
2429 int clk_set_max_rate(struct clk *clk, unsigned long rate) in clk_set_max_rate() argument
2431 if (!clk) in clk_set_max_rate()
2434 return clk_set_rate_range(clk, clk->min_rate, rate); in clk_set_max_rate()
2439 * clk_get_parent - return the parent of a clk
2440 * @clk: the clk whose parent gets returned
2442 * Simply returns clk->parent. Returns NULL if clk is NULL.
2444 struct clk *clk_get_parent(struct clk *clk) in clk_get_parent() argument
2446 struct clk *parent; in clk_get_parent()
2448 if (!clk) in clk_get_parent()
2452 /* TODO: Create a per-user clk and change callers to call clk_put */ in clk_get_parent()
2453 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; in clk_get_parent()
2464 if (core->num_parents > 1 && core->ops->get_parent) in __clk_init_parent()
2465 index = core->ops->get_parent(core->hw); in __clk_init_parent()
2483 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); in clk_hw_reparent()
2487 * clk_has_parent - check if a clock is a possible parent for another
2488 * @clk: clock source
2494 * Returns true if @parent is a possible parent for @clk, false otherwise.
2496 bool clk_has_parent(struct clk *clk, struct clk *parent) in clk_has_parent() argument
2502 if (!clk || !parent) in clk_has_parent()
2505 core = clk->core; in clk_has_parent()
2506 parent_core = parent->core; in clk_has_parent()
2509 if (core->parent == parent_core) in clk_has_parent()
2512 for (i = 0; i < core->num_parents; i++) in clk_has_parent()
2513 if (!strcmp(core->parents[i].name, parent_core->name)) in clk_has_parent()
2532 if (core->parent == parent) in clk_core_set_parent_nolock()
2535 /* verify ops for multi-parent clks */ in clk_core_set_parent_nolock()
2536 if (core->num_parents > 1 && !core->ops->set_parent) in clk_core_set_parent_nolock()
2537 return -EPERM; in clk_core_set_parent_nolock()
2539 /* check that we are allowed to re-parent if the clock is in use */ in clk_core_set_parent_nolock()
2540 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) in clk_core_set_parent_nolock()
2541 return -EBUSY; in clk_core_set_parent_nolock()
2544 return -EBUSY; in clk_core_set_parent_nolock()
2550 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_core_set_parent_nolock()
2551 __func__, parent->name, core->name); in clk_core_set_parent_nolock()
2554 p_rate = parent->rate; in clk_core_set_parent_nolock()
2568 /* do the re-parent */ in clk_core_set_parent_nolock()
2587 return clk_core_set_parent_nolock(hw->core, parent->core); in clk_hw_set_parent()
2592 * clk_set_parent - switch the parent of a mux clk
2593 * @clk: the mux clk whose input we are switching
2594 * @parent: the new input to clk
2596 * Re-parent clk to use parent as its new input source. If clk is in
2597 * prepared state, the clk will get enabled for the duration of this call. If
2598 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2600 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2602 * After successfully changing clk's parent clk_set_parent will update the
2603 * clk topology, sysfs topology and propagate rate recalculation via
2606 * Returns 0 on success, -EERROR otherwise.
2608 int clk_set_parent(struct clk *clk, struct clk *parent) in clk_set_parent() argument
2612 if (!clk) in clk_set_parent()
2617 if (clk->exclusive_count) in clk_set_parent()
2618 clk_core_rate_unprotect(clk->core); in clk_set_parent()
2620 ret = clk_core_set_parent_nolock(clk->core, in clk_set_parent()
2621 parent ? parent->core : NULL); in clk_set_parent()
2623 if (clk->exclusive_count) in clk_set_parent()
2624 clk_core_rate_protect(clk->core); in clk_set_parent()
2634 int ret = -EINVAL; in clk_core_set_phase_nolock()
2642 return -EBUSY; in clk_core_set_phase_nolock()
2646 if (core->ops->set_phase) { in clk_core_set_phase_nolock()
2647 ret = core->ops->set_phase(core->hw, degrees); in clk_core_set_phase_nolock()
2649 core->phase = degrees; in clk_core_set_phase_nolock()
2658 * clk_set_phase - adjust the phase shift of a clock signal
2659 * @clk: clock signal source
2662 * Shifts the phase of a clock signal by the specified
2663 * degrees. Returns 0 on success, -EERROR otherwise.
2666 * signal that we adjust the clock signal phase against. For example
2667 * phase locked-loop clock signal generators we may shift phase with
2669 * clock phase may be shifted with respect to some other, unspecified
2672 * Additionally the concept of phase shift does not propagate through
2674 * clock accuracy. A parent clock phase attribute does not have an
2675 * impact on the phase attribute of a child clock.
2677 int clk_set_phase(struct clk *clk, int degrees) in clk_set_phase() argument
2681 if (!clk) in clk_set_phase()
2691 if (clk->exclusive_count) in clk_set_phase()
2692 clk_core_rate_unprotect(clk->core); in clk_set_phase()
2694 ret = clk_core_set_phase_nolock(clk->core, degrees); in clk_set_phase()
2696 if (clk->exclusive_count) in clk_set_phase()
2697 clk_core_rate_protect(clk->core); in clk_set_phase()
2710 if (!core->ops->get_phase) in clk_core_get_phase()
2713 /* Always try to update cached phase if possible */ in clk_core_get_phase()
2714 ret = core->ops->get_phase(core->hw); in clk_core_get_phase()
2716 core->phase = ret; in clk_core_get_phase()
2722 * clk_get_phase - return the phase shift of a clock signal
2723 * @clk: clock signal source
2725 * Returns the phase shift of a clock node in degrees, otherwise returns
2726 * -EERROR.
2728 int clk_get_phase(struct clk *clk) in clk_get_phase() argument
2732 if (!clk) in clk_get_phase()
2736 ret = clk_core_get_phase(clk->core); in clk_get_phase()
2746 core->duty.num = 1; in clk_core_reset_duty_cycle_nolock()
2747 core->duty.den = 2; in clk_core_reset_duty_cycle_nolock()
2754 struct clk_duty *duty = &core->duty; in clk_core_update_duty_cycle_nolock()
2757 if (!core->ops->get_duty_cycle) in clk_core_update_duty_cycle_nolock()
2760 ret = core->ops->get_duty_cycle(core->hw, duty); in clk_core_update_duty_cycle_nolock()
2765 if (duty->den == 0 || duty->num > duty->den) { in clk_core_update_duty_cycle_nolock()
2766 ret = -EINVAL; in clk_core_update_duty_cycle_nolock()
2781 if (core->parent && in clk_core_update_duty_cycle_parent_nolock()
2782 core->flags & CLK_DUTY_CYCLE_PARENT) { in clk_core_update_duty_cycle_parent_nolock()
2783 ret = clk_core_update_duty_cycle_nolock(core->parent); in clk_core_update_duty_cycle_parent_nolock()
2784 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_update_duty_cycle_parent_nolock()
2803 return -EBUSY; in clk_core_set_duty_cycle_nolock()
2807 if (!core->ops->set_duty_cycle) in clk_core_set_duty_cycle_nolock()
2810 ret = core->ops->set_duty_cycle(core->hw, duty); in clk_core_set_duty_cycle_nolock()
2812 memcpy(&core->duty, duty, sizeof(*duty)); in clk_core_set_duty_cycle_nolock()
2824 if (core->parent && in clk_core_set_duty_cycle_parent_nolock()
2825 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) { in clk_core_set_duty_cycle_parent_nolock()
2826 ret = clk_core_set_duty_cycle_nolock(core->parent, duty); in clk_core_set_duty_cycle_parent_nolock()
2827 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_set_duty_cycle_parent_nolock()
2834 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2835 * @clk: clock signal source
2844 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den) in clk_set_duty_cycle() argument
2849 if (!clk) in clk_set_duty_cycle()
2854 return -EINVAL; in clk_set_duty_cycle()
2861 if (clk->exclusive_count) in clk_set_duty_cycle()
2862 clk_core_rate_unprotect(clk->core); in clk_set_duty_cycle()
2864 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty); in clk_set_duty_cycle()
2866 if (clk->exclusive_count) in clk_set_duty_cycle()
2867 clk_core_rate_protect(clk->core); in clk_set_duty_cycle()
2878 struct clk_duty *duty = &core->duty; in clk_core_get_scaled_duty_cycle()
2885 ret = mult_frac(scale, duty->num, duty->den); in clk_core_get_scaled_duty_cycle()
2893 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2894 * @clk: clock signal source
2900 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale) in clk_get_scaled_duty_cycle() argument
2902 if (!clk) in clk_get_scaled_duty_cycle()
2905 return clk_core_get_scaled_duty_cycle(clk->core, scale); in clk_get_scaled_duty_cycle()
2910 * clk_is_match - check if two clk's point to the same hardware clock
2911 * @p: clk compared against q
2912 * @q: clk compared against p
2914 * Returns true if the two struct clk pointers both point to the same hardware
2915 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2920 bool clk_is_match(const struct clk *p, const struct clk *q) in clk_is_match()
2922 /* trivial case: identical struct clk's or both NULL */ in clk_is_match()
2926 /* true if clk->core pointers match. Avoid dereferencing garbage */ in clk_is_match()
2928 if (p->core == q->core) in clk_is_match()
2953 int phase; in clk_summary_show_one() local
2955 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ", in clk_summary_show_one()
2957 30 - level * 3, c->name, in clk_summary_show_one()
2958 c->enable_count, c->prepare_count, c->protect_count, in clk_summary_show_one()
2962 phase = clk_core_get_phase(c); in clk_summary_show_one()
2963 if (phase >= 0) in clk_summary_show_one()
2964 seq_printf(s, "%5d", phase); in clk_summary_show_one()
2966 seq_puts(s, "-----"); in clk_summary_show_one()
2978 hlist_for_each_entry(child, &c->children, child_node) in clk_summary_show_subtree()
2985 struct hlist_head **lists = (struct hlist_head **)s->private; in clk_summary_show()
2988 …" clock count count count rate accuracy phase cycle\n"); in clk_summary_show()
2989 …seq_puts(s, "-------------------------------------------------------------------------------------… in clk_summary_show()
3005 int phase; in clk_dump_one() local
3011 seq_printf(s, "\"%s\": { ", c->name); in clk_dump_one()
3012 seq_printf(s, "\"enable_count\": %d,", c->enable_count); in clk_dump_one()
3013 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); in clk_dump_one()
3014 seq_printf(s, "\"protect_count\": %d,", c->protect_count); in clk_dump_one()
3019 phase = clk_core_get_phase(c); in clk_dump_one()
3020 if (phase >= 0) in clk_dump_one()
3021 seq_printf(s, "\"phase\": %d,", phase); in clk_dump_one()
3032 hlist_for_each_entry(child, &c->children, child_node) { in clk_dump_subtree()
3044 struct hlist_head **lists = (struct hlist_head **)s->private; in clk_dump_show()
3092 ret = clk_prepare_enable(core->hw->clk); in clk_prepare_enable_set()
3094 clk_disable_unprepare(core->hw->clk); in clk_prepare_enable_set()
3103 *val = core->enable_count && core->prepare_count; in clk_prepare_enable_get()
3119 *val = core->rate; in clk_rate_get()
3147 struct clk_core *core = s->private; in clk_flags_show()
3148 unsigned long flags = core->flags; in clk_flags_show()
3177 * 4. Fetch parent clock's clock-output-name if DT index was set in possible_parent_show()
3185 seq_puts(s, parent->name); in possible_parent_show()
3186 else if (core->parents[i].name) in possible_parent_show()
3187 seq_puts(s, core->parents[i].name); in possible_parent_show()
3188 else if (core->parents[i].fw_name) in possible_parent_show()
3189 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name); in possible_parent_show()
3190 else if (core->parents[i].index >= 0) in possible_parent_show()
3192 of_clk_get_parent_name(core->of_node, in possible_parent_show()
3193 core->parents[i].index)); in possible_parent_show()
3202 struct clk_core *core = s->private; in possible_parents_show()
3205 for (i = 0; i < core->num_parents - 1; i++) in possible_parents_show()
3216 struct clk_core *core = s->private; in current_parent_show()
3218 if (core->parent) in current_parent_show()
3219 seq_printf(s, "%s\n", core->parent->name); in current_parent_show()
3227 struct clk_core *core = s->private; in clk_duty_cycle_show()
3228 struct clk_duty *duty = &core->duty; in clk_duty_cycle_show()
3230 seq_printf(s, "%u/%u\n", duty->num, duty->den); in clk_duty_cycle_show()
3238 struct clk_core *core = s->private; in clk_min_rate_show()
3252 struct clk_core *core = s->private; in clk_max_rate_show()
3271 root = debugfs_create_dir(core->name, pdentry); in clk_debug_create_one()
3272 core->dentry = root; in clk_debug_create_one()
3278 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy); in clk_debug_create_one()
3279 debugfs_create_u32("clk_phase", 0444, root, &core->phase); in clk_debug_create_one()
3281 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count); in clk_debug_create_one()
3282 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count); in clk_debug_create_one()
3283 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count); in clk_debug_create_one()
3284 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count); in clk_debug_create_one()
3292 if (core->num_parents > 0) in clk_debug_create_one()
3296 if (core->num_parents > 1) in clk_debug_create_one()
3300 if (core->ops->debug_init) in clk_debug_create_one()
3301 core->ops->debug_init(core->hw, core->dentry); in clk_debug_create_one()
3305 * clk_debug_register - add a clk node to the debugfs clk directory
3306 * @core: the clk being added to the debugfs clk directory
3308 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3309 * initialized. Otherwise it bails out early since the debugfs clk directory
3315 hlist_add_head(&core->debug_node, &clk_debug_list); in clk_debug_register()
3322 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3323 * @core: the clk being removed from the debugfs clk directory
3325 * Dynamically removes a clk and all its child nodes from the
3326 * debugfs clk directory if clk->dentry points to debugfs created by
3332 hlist_del_init(&core->debug_node); in clk_debug_unregister()
3333 debugfs_remove_recursive(core->dentry); in clk_debug_unregister()
3334 core->dentry = NULL; in clk_debug_unregister()
3339 * clk_debug_init - lazily populate the debugfs clk directory
3343 * populates the debugfs clk directory once at boot-time when we know that
3344 * debugfs is setup. It should only be called once at boot-time, all other clks
3356 pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n"); in clk_debug_init()
3358 pr_warn("** This means that this kernel is built to expose clk operations **\n"); in clk_debug_init()
3369 rootdir = debugfs_create_dir("clk", NULL); in clk_debug_init()
3416 /* update the clk tree topology */ in clk_core_reparent_orphans_nolock()
3430 * 'req_rate' is set to something non-zero so that in clk_core_reparent_orphans_nolock()
3433 orphan->req_rate = orphan->rate; in clk_core_reparent_orphans_nolock()
3439 * __clk_core_init - initialize the data structures in a struct clk_core
3450 int phase; in __clk_core_init() local
3453 return -EINVAL; in __clk_core_init()
3458 * Set hw->core after grabbing the prepare_lock to synchronize with in __clk_core_init()
3459 * callers of clk_core_fill_parent_index() where we treat hw->core in __clk_core_init()
3460 * being NULL as the clk not being registered yet. This is crucial so in __clk_core_init()
3463 core->hw->core = core; in __clk_core_init()
3470 if (clk_core_lookup(core->name)) { in __clk_core_init()
3471 pr_debug("%s: clk %s already initialized\n", in __clk_core_init()
3472 __func__, core->name); in __clk_core_init()
3473 ret = -EEXIST; in __clk_core_init()
3477 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */ in __clk_core_init()
3478 if (core->ops->set_rate && in __clk_core_init()
3479 !((core->ops->round_rate || core->ops->determine_rate) && in __clk_core_init()
3480 core->ops->recalc_rate)) { in __clk_core_init()
3482 __func__, core->name); in __clk_core_init()
3483 ret = -EINVAL; in __clk_core_init()
3487 if (core->ops->set_parent && !core->ops->get_parent) { in __clk_core_init()
3489 __func__, core->name); in __clk_core_init()
3490 ret = -EINVAL; in __clk_core_init()
3494 if (core->num_parents > 1 && !core->ops->get_parent) { in __clk_core_init()
3496 __func__, core->name); in __clk_core_init()
3497 ret = -EINVAL; in __clk_core_init()
3501 if (core->ops->set_rate_and_parent && in __clk_core_init()
3502 !(core->ops->set_parent && core->ops->set_rate)) { in __clk_core_init()
3504 __func__, core->name); in __clk_core_init()
3505 ret = -EINVAL; in __clk_core_init()
3510 * optional platform-specific magic in __clk_core_init()
3523 if (core->ops->init) { in __clk_core_init()
3524 ret = core->ops->init(core->hw); in __clk_core_init()
3529 parent = core->parent = __clk_init_parent(core); in __clk_core_init()
3532 * Populate core->parent if parent has already been clk_core_init'd. If in __clk_core_init()
3533 * parent has not yet been clk_core_init'd then place clk in the orphan in __clk_core_init()
3534 * list. If clk doesn't have any parents then place it in the root in __clk_core_init()
3535 * clk list. in __clk_core_init()
3537 * Every time a new clk is clk_init'd then we walk the list of orphan in __clk_core_init()
3538 * clocks and re-parent any that are children of the clock currently in __clk_core_init()
3542 hlist_add_head(&core->child_node, &parent->children); in __clk_core_init()
3543 core->orphan = parent->orphan; in __clk_core_init()
3544 } else if (!core->num_parents) { in __clk_core_init()
3545 hlist_add_head(&core->child_node, &clk_root_list); in __clk_core_init()
3546 core->orphan = false; in __clk_core_init()
3548 hlist_add_head(&core->child_node, &clk_orphan_list); in __clk_core_init()
3549 core->orphan = true; in __clk_core_init()
3553 * Set clk's accuracy. The preferred method is to use in __clk_core_init()
3559 if (core->ops->recalc_accuracy) in __clk_core_init()
3560 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_core_init()
3563 core->accuracy = parent->accuracy; in __clk_core_init()
3565 core->accuracy = 0; in __clk_core_init()
3568 * Set clk's phase by clk_core_get_phase() caching the phase. in __clk_core_init()
3569 * Since a phase is by definition relative to its parent, just in __clk_core_init()
3570 * query the current clock phase, or just assume it's in phase. in __clk_core_init()
3572 phase = clk_core_get_phase(core); in __clk_core_init()
3573 if (phase < 0) { in __clk_core_init()
3574 ret = phase; in __clk_core_init()
3575 pr_warn("%s: Failed to get phase for clk '%s'\n", __func__, in __clk_core_init()
3576 core->name); in __clk_core_init()
3581 * Set clk's duty cycle. in __clk_core_init()
3586 * Set clk's rate. The preferred method is to use .recalc_rate. For in __clk_core_init()
3591 if (core->ops->recalc_rate) in __clk_core_init()
3592 rate = core->ops->recalc_rate(core->hw, in __clk_core_init()
3595 rate = parent->rate; in __clk_core_init()
3598 core->rate = core->req_rate = rate; in __clk_core_init()
3605 if (core->flags & CLK_IS_CRITICAL) { in __clk_core_init()
3610 pr_warn("%s: critical clk '%s' failed to prepare\n", in __clk_core_init()
3611 __func__, core->name); in __clk_core_init()
3619 pr_warn("%s: critical clk '%s' failed to enable\n", in __clk_core_init()
3620 __func__, core->name); in __clk_core_init()
3629 kref_init(&core->ref); in __clk_core_init()
3634 hlist_del_init(&core->child_node); in __clk_core_init()
3635 core->hw->core = NULL; in __clk_core_init()
3647 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3648 * @core: clk to add consumer to
3649 * @clk: consumer to link to a clk
3651 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk) in clk_core_link_consumer() argument
3654 hlist_add_head(&clk->clks_node, &core->clks); in clk_core_link_consumer()
3659 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3660 * @clk: consumer to unlink
3662 static void clk_core_unlink_consumer(struct clk *clk) in clk_core_unlink_consumer() argument
3665 hlist_del(&clk->clks_node); in clk_core_unlink_consumer()
3669 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3670 * @core: clk to allocate a consumer for
3674 * Returns: clk consumer left unlinked from the consumer list
3676 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id, in alloc_clk()
3679 struct clk *clk; in alloc_clk() local
3681 clk = kzalloc(sizeof(*clk), GFP_KERNEL); in alloc_clk()
3682 if (!clk) in alloc_clk()
3683 return ERR_PTR(-ENOMEM); in alloc_clk()
3685 clk->core = core; in alloc_clk()
3686 clk->dev_id = dev_id; in alloc_clk()
3687 clk->con_id = kstrdup_const(con_id, GFP_KERNEL); in alloc_clk()
3688 clk->max_rate = ULONG_MAX; in alloc_clk()
3690 return clk; in alloc_clk()
3694 * free_clk - Free a clk consumer
3695 * @clk: clk consumer to free
3697 * Note, this assumes the clk has been unlinked from the clk_core consumer
3700 static void free_clk(struct clk *clk) in free_clk() argument
3702 kfree_const(clk->con_id); in free_clk()
3703 kfree(clk); in free_clk()
3707 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3709 * @dev: clk consumer device
3710 * @hw: clk_hw associated with the clk being consumed
3714 * This is the main function used to create a clk pointer for use by clk
3716 * used by the framework and clk provider respectively.
3718 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw, in clk_hw_create_clk()
3721 struct clk *clk; in clk_hw_create_clk() local
3728 core = hw->core; in clk_hw_create_clk()
3729 clk = alloc_clk(core, dev_id, con_id); in clk_hw_create_clk()
3730 if (IS_ERR(clk)) in clk_hw_create_clk()
3731 return clk; in clk_hw_create_clk()
3732 clk->dev = dev; in clk_hw_create_clk()
3734 if (!try_module_get(core->owner)) { in clk_hw_create_clk()
3735 free_clk(clk); in clk_hw_create_clk()
3736 return ERR_PTR(-ENOENT); in clk_hw_create_clk()
3739 kref_get(&core->ref); in clk_hw_create_clk()
3740 clk_core_link_consumer(core, clk); in clk_hw_create_clk()
3742 return clk; in clk_hw_create_clk()
3751 return -EINVAL; in clk_cpy_name()
3757 return -ENOMEM; in clk_cpy_name()
3765 u8 num_parents = init->num_parents; in clk_core_populate_parent_map()
3766 const char * const *parent_names = init->parent_names; in clk_core_populate_parent_map()
3767 const struct clk_hw **parent_hws = init->parent_hws; in clk_core_populate_parent_map()
3768 const struct clk_parent_data *parent_data = init->parent_data; in clk_core_populate_parent_map()
3776 * Avoid unnecessary string look-ups of clk_core's possible parents by in clk_core_populate_parent_map()
3780 core->parents = parents; in clk_core_populate_parent_map()
3782 return -ENOMEM; in clk_core_populate_parent_map()
3786 parent->index = -1; in clk_core_populate_parent_map()
3791 __func__, core->name); in clk_core_populate_parent_map()
3792 ret = clk_cpy_name(&parent->name, parent_names[i], in clk_core_populate_parent_map()
3795 parent->hw = parent_data[i].hw; in clk_core_populate_parent_map()
3796 parent->index = parent_data[i].index; in clk_core_populate_parent_map()
3797 ret = clk_cpy_name(&parent->fw_name, in clk_core_populate_parent_map()
3800 ret = clk_cpy_name(&parent->name, in clk_core_populate_parent_map()
3804 parent->hw = parent_hws[i]; in clk_core_populate_parent_map()
3806 ret = -EINVAL; in clk_core_populate_parent_map()
3814 } while (--i >= 0); in clk_core_populate_parent_map()
3826 int i = core->num_parents; in clk_core_free_parent_map()
3828 if (!core->num_parents) in clk_core_free_parent_map()
3831 while (--i >= 0) { in clk_core_free_parent_map()
3832 kfree_const(core->parents[i].name); in clk_core_free_parent_map()
3833 kfree_const(core->parents[i].fw_name); in clk_core_free_parent_map()
3836 kfree(core->parents); in clk_core_free_parent_map()
3839 static struct clk *
3844 const struct clk_init_data *init = hw->init; in __clk_register()
3849 * we catch use of hw->init early on in the core. in __clk_register()
3851 hw->init = NULL; in __clk_register()
3855 ret = -ENOMEM; in __clk_register()
3859 core->name = kstrdup_const(init->name, GFP_KERNEL); in __clk_register()
3860 if (!core->name) { in __clk_register()
3861 ret = -ENOMEM; in __clk_register()
3865 if (WARN_ON(!init->ops)) { in __clk_register()
3866 ret = -EINVAL; in __clk_register()
3869 core->ops = init->ops; in __clk_register()
3872 core->rpm_enabled = true; in __clk_register()
3873 core->dev = dev; in __clk_register()
3874 core->of_node = np; in __clk_register()
3875 if (dev && dev->driver) in __clk_register()
3876 core->owner = dev->driver->owner; in __clk_register()
3877 core->hw = hw; in __clk_register()
3878 core->flags = init->flags; in __clk_register()
3879 core->num_parents = init->num_parents; in __clk_register()
3880 core->min_rate = 0; in __clk_register()
3881 core->max_rate = ULONG_MAX; in __clk_register()
3887 INIT_HLIST_HEAD(&core->clks); in __clk_register()
3893 hw->clk = alloc_clk(core, NULL, NULL); in __clk_register()
3894 if (IS_ERR(hw->clk)) { in __clk_register()
3895 ret = PTR_ERR(hw->clk); in __clk_register()
3899 clk_core_link_consumer(core, hw->clk); in __clk_register()
3903 return hw->clk; in __clk_register()
3906 clk_core_unlink_consumer(hw->clk); in __clk_register()
3909 free_clk(hw->clk); in __clk_register()
3910 hw->clk = NULL; in __clk_register()
3916 kfree_const(core->name); in __clk_register()
3924 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
3928 * @dev->parent if dev doesn't have a device node, or NULL if neither
3929 * @dev or @dev->parent have a device node.
3940 np = dev_of_node(dev->parent); in dev_or_parent_of_node()
3946 * clk_register - allocate a new clock, register it and return an opaque cookie
3948 * @hw: link to hardware-specific clock data
3953 * Returns: a pointer to the newly allocated struct clk which
3958 struct clk *clk_register(struct device *dev, struct clk_hw *hw) in clk_register()
3965 * clk_hw_register - register a clk_hw and return an error code
3967 * @hw: link to hardware-specific clock data
3982 * of_clk_hw_register - register a clk_hw and return an error code
3984 * @hw: link to hardware-specific clock data
4006 kfree_const(core->name); in __clk_release()
4013 * consumer calls clk_put() and the struct clk object is freed.
4017 return -ENXIO; in clk_nodrv_prepare_enable()
4028 return -ENXIO; in clk_nodrv_set_rate()
4033 return -ENXIO; in clk_nodrv_set_parent()
4051 for (i = 0; i < root->num_parents; i++) in clk_core_evict_parent_cache_subtree()
4052 if (root->parents[i].core == target) in clk_core_evict_parent_cache_subtree()
4053 root->parents[i].core = NULL; in clk_core_evict_parent_cache_subtree()
4055 hlist_for_each_entry(child, &root->children, child_node) in clk_core_evict_parent_cache_subtree()
4059 /* Remove this clk from all parent caches */
4074 * clk_unregister - unregister a currently registered clock
4075 * @clk: clock to unregister
4077 void clk_unregister(struct clk *clk) in clk_unregister() argument
4082 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in clk_unregister()
4085 clk_debug_unregister(clk->core); in clk_unregister()
4089 ops = clk->core->ops; in clk_unregister()
4092 clk->core->name); in clk_unregister()
4100 clk->core->ops = &clk_nodrv_ops; in clk_unregister()
4103 if (ops->terminate) in clk_unregister()
4104 ops->terminate(clk->core->hw); in clk_unregister()
4106 if (!hlist_empty(&clk->core->children)) { in clk_unregister()
4111 hlist_for_each_entry_safe(child, t, &clk->core->children, in clk_unregister()
4116 clk_core_evict_parent_cache(clk->core); in clk_unregister()
4118 hlist_del_init(&clk->core->child_node); in clk_unregister()
4120 if (clk->core->prepare_count) in clk_unregister()
4122 __func__, clk->core->name); in clk_unregister()
4124 if (clk->core->protect_count) in clk_unregister()
4126 __func__, clk->core->name); in clk_unregister()
4128 kref_put(&clk->core->ref, __clk_release); in clk_unregister()
4129 free_clk(clk); in clk_unregister()
4136 * clk_hw_unregister - unregister a currently registered clk_hw
4137 * @hw: hardware-specific clock data to unregister
4141 clk_unregister(hw->clk); in clk_hw_unregister()
4147 clk_unregister(*(struct clk **)res); in devm_clk_release()
4156 * devm_clk_register - resource managed clk_register()
4158 * @hw: link to hardware-specific clock data
4165 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) in devm_clk_register()
4167 struct clk *clk; in devm_clk_register() local
4168 struct clk **clkp; in devm_clk_register()
4172 return ERR_PTR(-ENOMEM); in devm_clk_register()
4174 clk = clk_register(dev, hw); in devm_clk_register()
4175 if (!IS_ERR(clk)) { in devm_clk_register()
4176 *clkp = clk; in devm_clk_register()
4182 return clk; in devm_clk_register()
4187 * devm_clk_hw_register - resource managed clk_hw_register()
4189 * @hw: link to hardware-specific clock data
4202 return -ENOMEM; in devm_clk_hw_register()
4218 struct clk *c = res; in devm_clk_match()
4234 * devm_clk_unregister - resource managed clk_unregister()
4236 * @clk: clock to unregister
4242 void devm_clk_unregister(struct device *dev, struct clk *clk) in devm_clk_unregister() argument
4244 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); in devm_clk_unregister()
4249 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
4250 * @dev: device that is unregistering the hardware-specific clock data
4251 * @hw: link to hardware-specific clock data
4268 void __clk_put(struct clk *clk) in __clk_put() argument
4272 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in __clk_put()
4282 if (WARN_ON(clk->exclusive_count)) { in __clk_put()
4284 clk->core->protect_count -= (clk->exclusive_count - 1); in __clk_put()
4285 clk_core_rate_unprotect(clk->core); in __clk_put()
4286 clk->exclusive_count = 0; in __clk_put()
4289 hlist_del(&clk->clks_node); in __clk_put()
4290 if (clk->min_rate > clk->core->req_rate || in __clk_put()
4291 clk->max_rate < clk->core->req_rate) in __clk_put()
4292 clk_core_set_rate_nolock(clk->core, clk->core->req_rate); in __clk_put()
4294 owner = clk->core->owner; in __clk_put()
4295 kref_put(&clk->core->ref, __clk_release); in __clk_put()
4301 free_clk(clk); in __clk_put()
4304 /*** clk rate change notifiers ***/
4307 * clk_notifier_register - add a clk rate change notifier
4308 * @clk: struct clk * to watch
4311 * Request notification when clk's rate changes. This uses an SRCU
4314 * re-enter into the clk framework by calling any top-level clk APIs;
4321 * clk_notifier_register() must be called from non-atomic context.
4322 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4326 int clk_notifier_register(struct clk *clk, struct notifier_block *nb) in clk_notifier_register() argument
4329 int ret = -ENOMEM; in clk_notifier_register()
4331 if (!clk || !nb) in clk_notifier_register()
4332 return -EINVAL; in clk_notifier_register()
4336 /* search the list of notifiers for this clk */ in clk_notifier_register()
4338 if (cn->clk == clk) in clk_notifier_register()
4341 /* if clk wasn't in the notifier list, allocate new clk_notifier */ in clk_notifier_register()
4346 cn->clk = clk; in clk_notifier_register()
4347 srcu_init_notifier_head(&cn->notifier_head); in clk_notifier_register()
4349 list_add(&cn->node, &clk_notifier_list); in clk_notifier_register()
4352 ret = srcu_notifier_chain_register(&cn->notifier_head, nb); in clk_notifier_register()
4354 clk->core->notifier_count++; in clk_notifier_register()
4364 * clk_notifier_unregister - remove a clk rate change notifier
4365 * @clk: struct clk *
4368 * Request no further notification for changes to 'clk' and frees memory
4371 * Returns -EINVAL if called with null arguments; otherwise, passes
4374 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) in clk_notifier_unregister() argument
4377 int ret = -ENOENT; in clk_notifier_unregister()
4379 if (!clk || !nb) in clk_notifier_unregister()
4380 return -EINVAL; in clk_notifier_unregister()
4385 if (cn->clk == clk) { in clk_notifier_unregister()
4386 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); in clk_notifier_unregister()
4388 clk->core->notifier_count--; in clk_notifier_unregister()
4391 if (!cn->notifier_head.head) { in clk_notifier_unregister()
4392 srcu_cleanup_notifier_head(&cn->notifier_head); in clk_notifier_unregister()
4393 list_del(&cn->node); in clk_notifier_unregister()
4415 * struct of_clk_provider - Clock provider registration structure
4418 * @get: Get clock callback. Returns NULL or a struct clk for the
4428 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4440 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, in of_clk_src_simple_get()
4453 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) in of_clk_src_onecell_get()
4456 unsigned int idx = clkspec->args[0]; in of_clk_src_onecell_get()
4458 if (idx >= clk_data->clk_num) { in of_clk_src_onecell_get()
4460 return ERR_PTR(-EINVAL); in of_clk_src_onecell_get()
4463 return clk_data->clks[idx]; in of_clk_src_onecell_get()
4471 unsigned int idx = clkspec->args[0]; in of_clk_hw_onecell_get()
4473 if (idx >= hw_data->num) { in of_clk_hw_onecell_get()
4475 return ERR_PTR(-EINVAL); in of_clk_hw_onecell_get()
4478 return hw_data->hws[idx]; in of_clk_hw_onecell_get()
4483 * of_clk_add_provider() - Register a clock provider for a node
4491 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, in of_clk_add_provider()
4500 return -ENOMEM; in of_clk_add_provider()
4502 cp->node = of_node_get(np); in of_clk_add_provider()
4503 cp->data = data; in of_clk_add_provider()
4504 cp->get = clk_src_get; in of_clk_add_provider()
4507 list_add(&cp->link, &of_clk_providers); in of_clk_add_provider()
4522 * of_clk_add_hw_provider() - Register a clock provider for a node
4537 return -ENOMEM; in of_clk_add_hw_provider()
4539 cp->node = of_node_get(np); in of_clk_add_hw_provider()
4540 cp->data = data; in of_clk_add_hw_provider()
4541 cp->get_hw = get; in of_clk_add_hw_provider()
4544 list_add(&cp->link, &of_clk_providers); in of_clk_add_hw_provider()
4565 * for cases like MFD sub-devices where the child device driver wants to use
4566 * devm_*() APIs but not list the device in DT as a sub-node.
4572 np = dev->of_node; in get_clk_provider_node()
4573 parent_np = dev->parent ? dev->parent->of_node : NULL; in get_clk_provider_node()
4575 if (!of_find_property(np, "#clock-cells", NULL)) in get_clk_provider_node()
4576 if (of_find_property(parent_np, "#clock-cells", NULL)) in get_clk_provider_node()
4583 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4589 * node or if the device node lacks of clock provider information (#clock-cells)
4591 * has the #clock-cells then it is used in registration. Provider is
4607 return -ENOMEM; in devm_of_clk_add_hw_provider()
4623 * of_clk_del_provider() - Remove a previously registered clock provider
4632 if (cp->node == np) { in of_clk_del_provider()
4633 list_del(&cp->link); in of_clk_del_provider()
4634 of_node_put(cp->node); in of_clk_del_provider()
4654 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4670 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4676 * Parses a device node's "clocks" and "clock-names" properties to find the
4679 * parsing error. The @index argument is ignored if @name is non-NULL.
4683 * phandle1: clock-controller@1 {
4684 * #clock-cells = <2>;
4687 * phandle2: clock-controller@2 {
4688 * #clock-cells = <1>;
4691 * clock-consumer@3 {
4693 * clock-names = "name1", "name2";
4696 * To get a device_node for `clock-controller@2' node you may call this
4699 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4700 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4701 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4703 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4704 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4705 * the "clock-names" property of @np.
4710 int ret = -ENOENT; in of_parse_clkspec()
4716 * "clock-names" property. If it cannot be found, then index in of_parse_clkspec()
4718 * return -EINVAL. in of_parse_clkspec()
4721 index = of_property_match_string(np, "clock-names", name); in of_parse_clkspec()
4722 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells", in of_parse_clkspec()
4731 * has a "clock-ranges" property, then we can try one of its in of_parse_clkspec()
4734 np = np->parent; in of_parse_clkspec()
4735 if (np && !of_get_property(np, "clock-ranges", NULL)) in of_parse_clkspec()
4747 struct clk *clk; in __of_clk_get_hw_from_provider() local
4749 if (provider->get_hw) in __of_clk_get_hw_from_provider()
4750 return provider->get_hw(clkspec, provider->data); in __of_clk_get_hw_from_provider()
4752 clk = provider->get(clkspec, provider->data); in __of_clk_get_hw_from_provider()
4753 if (IS_ERR(clk)) in __of_clk_get_hw_from_provider()
4754 return ERR_CAST(clk); in __of_clk_get_hw_from_provider()
4755 return __clk_get_hw(clk); in __of_clk_get_hw_from_provider()
4762 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER); in of_clk_get_hw_from_clkspec()
4765 return ERR_PTR(-EINVAL); in of_clk_get_hw_from_clkspec()
4769 if (provider->node == clkspec->np) { in of_clk_get_hw_from_clkspec()
4781 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4784 * This function looks up a struct clk from the registered list of clock
4788 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) in of_clk_get_from_provider()
4813 static struct clk *__of_clk_get(struct device_node *np, in __of_clk_get()
4822 struct clk *of_clk_get(struct device_node *np, int index) in of_clk_get()
4824 return __of_clk_get(np, index, np->full_name, NULL); in of_clk_get()
4829 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4833 * This function parses the clocks and clock-names properties,
4834 * and uses them to look up the struct clk from the registered list of clock
4837 struct clk *of_clk_get_by_name(struct device_node *np, const char *name) in of_clk_get_by_name()
4840 return ERR_PTR(-ENOENT); in of_clk_get_by_name()
4842 return __of_clk_get(np, 0, np->full_name, name); in of_clk_get_by_name()
4847 * of_clk_get_parent_count() - Count the number of clocks a device node has
4856 count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); in of_clk_get_parent_count()
4873 struct clk *clk; in of_clk_get_parent_name() local
4875 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, in of_clk_get_parent_name()
4884 * specified into an array offset for the clock-output-names property. in of_clk_get_parent_name()
4886 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { in of_clk_get_parent_name()
4893 /* We went off the end of 'clock-indices' without finding it */ in of_clk_get_parent_name()
4897 if (of_property_read_string_index(clkspec.np, "clock-output-names", in of_clk_get_parent_name()
4904 * the clock as long as #clock-cells = 0. in of_clk_get_parent_name()
4906 clk = of_clk_get_from_provider(&clkspec); in of_clk_get_parent_name()
4907 if (IS_ERR(clk)) { in of_clk_get_parent_name()
4909 clk_name = clkspec.np->name; in of_clk_get_parent_name()
4913 clk_name = __clk_get_name(clk); in of_clk_get_parent_name()
4914 clk_put(clk); in of_clk_get_parent_name()
4925 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4961 struct clk *clk = of_clk_get(np, i); in parent_ready() local
4964 if (!IS_ERR(clk)) { in parent_ready()
4965 clk_put(clk); in parent_ready()
4971 if (PTR_ERR(clk) == -EPROBE_DEFER) in parent_ready()
4987 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4990 * @flags: pointer to top-level framework flags
4992 * Detects if the clock-critical property exists and, if so, sets the
4996 * bindings, such as the one-clock-per-node style that are outdated.
5012 return -EINVAL; in of_clk_detect_critical()
5014 of_property_for_each_u32(np, "clock-critical", prop, cur, idx) in of_clk_detect_critical()
5022 * of_clk_init() - Scan and init clock providers from the DT
5052 list_del(&clk_provider->node); in of_clk_init()
5053 of_node_put(clk_provider->np); in of_clk_init()
5060 parent->clk_init_cb = match->data; in of_clk_init()
5061 parent->np = of_node_get(np); in of_clk_init()
5062 list_add_tail(&parent->node, &clk_provider_list); in of_clk_init()
5069 if (force || parent_ready(clk_provider->np)) { in of_clk_init()
5072 of_node_set_flag(clk_provider->np, in of_clk_init()
5075 clk_provider->clk_init_cb(clk_provider->np); in of_clk_init()
5076 of_clk_set_defaults(clk_provider->np, true); in of_clk_init()
5078 list_del(&clk_provider->node); in of_clk_init()
5079 of_node_put(clk_provider->np); in of_clk_init()