Lines Matching +full:parent +full:- +full:clk
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"
68 struct clk_core *parent; member
100 #include <trace/events/clk.h>
102 struct clk { struct
118 if (!core->rpm_enabled) in clk_pm_runtime_get()
121 ret = pm_runtime_get_sync(core->dev); in clk_pm_runtime_get()
123 pm_runtime_put_noidle(core->dev); in clk_pm_runtime_get()
131 if (!core->rpm_enabled) in clk_pm_runtime_put()
134 pm_runtime_put_sync(core->dev); in clk_pm_runtime_put()
138 * clk_pm_runtime_get_all() - Runtime "get" all clk provider devices
140 * Call clk_pm_runtime_get() on all runtime PM enabled clks in the clk tree so
172 pr_err("clk: Failed to runtime PM get '%s' for clk '%s'\n", in clk_pm_runtime_get_all()
173 dev_name(failed->dev), failed->name); in clk_pm_runtime_get_all()
193 * clk_pm_runtime_put_all() - Runtime "put" all clk provider devices
209 struct device *dev = core->dev; in clk_pm_runtime_init()
212 core->rpm_enabled = true; in clk_pm_runtime_init()
215 hlist_add_head(&core->rpm_node, &clk_rpm_list); in clk_pm_runtime_init()
241 if (--prepare_refcnt) in clk_prepare_unlock()
281 if (--enable_refcnt) { in clk_enable_unlock()
291 return core->protect_count; in clk_core_rate_is_protected()
302 if (!core->ops->is_prepared) in clk_core_is_prepared()
303 return core->prepare_count; in clk_core_is_prepared()
306 ret = core->ops->is_prepared(core->hw); in clk_core_is_prepared()
321 if (!core->ops->is_enabled) in clk_core_is_enabled()
322 return core->enable_count; in clk_core_is_enabled()
334 if (core->rpm_enabled) { in clk_core_is_enabled()
335 pm_runtime_get_noresume(core->dev); in clk_core_is_enabled()
336 if (!pm_runtime_active(core->dev)) { in clk_core_is_enabled()
344 * context. If the parent isn't enabled already, we can't do in clk_core_is_enabled()
347 if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent) in clk_core_is_enabled()
348 if (!clk_core_is_enabled(core->parent)) { in clk_core_is_enabled()
353 ret = core->ops->is_enabled(core->hw); in clk_core_is_enabled()
355 if (core->rpm_enabled) in clk_core_is_enabled()
356 pm_runtime_put(core->dev); in clk_core_is_enabled()
363 const char *__clk_get_name(const struct clk *clk) in __clk_get_name() argument
365 return !clk ? NULL : clk->core->name; in __clk_get_name()
371 return hw->core->name; in clk_hw_get_name()
375 struct clk_hw *__clk_get_hw(struct clk *clk) in __clk_get_hw() argument
377 return !clk ? NULL : clk->core->hw; in __clk_get_hw()
383 return hw->core->num_parents; in clk_hw_get_num_parents()
389 return hw->core->parent ? hw->core->parent->hw : NULL; in clk_hw_get_parent()
399 if (!strcmp(core->name, name)) in __clk_lookup_subtree()
402 hlist_for_each_entry(child, &core->children, child_node) { in __clk_lookup_subtree()
419 /* search the 'proper' clk tree first */ in clk_core_lookup()
446 return -ENOENT; in of_parse_clkspec()
451 return ERR_PTR(-ENOENT); in of_clk_get_hw_from_clkspec()
456 * clk_core_get - Find the clk_core parent of a clk
457 * @core: clk to find parent of
458 * @p_index: parent index to search for
460 * This is the preferred method for clk providers to find the parent of a
461 * clk when that parent is external to the clk controller. The parent_names
463 * node's 'clock-names' property or as the 'con_id' matching the device's
464 * dev_name() in a clk_lookup. This allows clk providers to use their own
465 * namespace instead of looking for a globally unique parent string.
468 * clock-controller@c001 that has a clk_init_data::parent_data array
470 * clock-controller@f00abcd without needing to get the globally unique name of
471 * the xtal clk.
473 * parent: clock-controller@f00abcd {
475 * #clock-cells = <0>;
478 * clock-controller@c001 {
480 * clocks = <&parent>;
481 * clock-names = "xtal";
482 * #clock-cells = <1>;
485 * Returns: -ENOENT when the provider can't be found or the clk doesn't
487 * in a clkdev lookup. NULL when the provider knows about the clk but it
489 * A valid clk_core pointer when the clk can be found in the provider.
493 const char *name = core->parents[p_index].fw_name; in clk_core_get()
494 int index = core->parents[p_index].index; in clk_core_get()
495 struct clk_hw *hw = ERR_PTR(-ENOENT); in clk_core_get()
496 struct device *dev = core->dev; in clk_core_get()
498 struct device_node *np = core->of_node; in clk_core_get()
519 return hw->core; in clk_core_get()
524 struct clk_parent_map *entry = &core->parents[index]; in clk_core_fill_parent_index()
525 struct clk_core *parent = ERR_PTR(-ENOENT); in clk_core_fill_parent_index() local
527 if (entry->hw) { in clk_core_fill_parent_index()
528 parent = entry->hw->core; in clk_core_fill_parent_index()
532 * when the parent is registered. in clk_core_fill_parent_index()
534 if (!parent) in clk_core_fill_parent_index()
535 parent = ERR_PTR(-EPROBE_DEFER); in clk_core_fill_parent_index()
537 parent = clk_core_get(core, index); in clk_core_fill_parent_index()
538 if (PTR_ERR(parent) == -ENOENT && entry->name) in clk_core_fill_parent_index()
539 parent = clk_core_lookup(entry->name); in clk_core_fill_parent_index()
543 if (!IS_ERR(parent)) in clk_core_fill_parent_index()
544 entry->core = parent; in clk_core_fill_parent_index()
550 if (!core || index >= core->num_parents || !core->parents) in clk_core_get_parent_by_index()
553 if (!core->parents[index].core) in clk_core_get_parent_by_index()
556 return core->parents[index].core; in clk_core_get_parent_by_index()
562 struct clk_core *parent; in clk_hw_get_parent_by_index() local
564 parent = clk_core_get_parent_by_index(hw->core, index); in clk_hw_get_parent_by_index()
566 return !parent ? NULL : parent->hw; in clk_hw_get_parent_by_index()
570 unsigned int __clk_get_enable_count(struct clk *clk) in __clk_get_enable_count() argument
572 return !clk ? 0 : clk->core->enable_count; in __clk_get_enable_count()
580 if (!core->num_parents || core->parent) in clk_core_get_rate_nolock()
581 return core->rate; in clk_core_get_rate_nolock()
584 * Clk must have a parent because num_parents > 0 but the parent isn't in clk_core_get_rate_nolock()
585 * known yet. Best to return 0 as the rate of this clk until we can in clk_core_get_rate_nolock()
586 * properly recalc the rate based on the parent's rate. in clk_core_get_rate_nolock()
593 return clk_core_get_rate_nolock(hw->core); in clk_hw_get_rate()
602 return core->accuracy; in clk_core_get_accuracy_no_lock()
607 return hw->core->flags; in clk_hw_get_flags()
613 return clk_core_is_prepared(hw->core); in clk_hw_is_prepared()
619 return clk_core_rate_is_protected(hw->core); in clk_hw_rate_is_protected()
625 return clk_core_is_enabled(hw->core); in clk_hw_is_enabled()
629 bool __clk_is_enabled(struct clk *clk) in __clk_is_enabled() argument
631 if (!clk) in __clk_is_enabled()
634 return clk_core_is_enabled(clk->core); in __clk_is_enabled()
642 return abs(now - rate) < abs(best - rate); in mux_is_better_rate()
651 struct clk_core *core = hw->core, *parent, *best_parent = NULL; in clk_mux_determine_rate_flags() local
656 /* if NO_REPARENT flag set, pass through to current parent */ in clk_mux_determine_rate_flags()
657 if (core->flags & CLK_SET_RATE_NO_REPARENT) { in clk_mux_determine_rate_flags()
658 parent = core->parent; in clk_mux_determine_rate_flags()
659 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
660 ret = __clk_determine_rate(parent ? parent->hw : NULL, in clk_mux_determine_rate_flags()
666 } else if (parent) { in clk_mux_determine_rate_flags()
667 best = clk_core_get_rate_nolock(parent); in clk_mux_determine_rate_flags()
675 /* find the parent that can provide the fastest rate <= rate */ in clk_mux_determine_rate_flags()
676 num_parents = core->num_parents; in clk_mux_determine_rate_flags()
678 parent = clk_core_get_parent_by_index(core, i); in clk_mux_determine_rate_flags()
679 if (!parent) in clk_mux_determine_rate_flags()
682 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
684 ret = __clk_determine_rate(parent->hw, &parent_req); in clk_mux_determine_rate_flags()
688 parent_req.rate = clk_core_get_rate_nolock(parent); in clk_mux_determine_rate_flags()
691 if (mux_is_better_rate(req->rate, parent_req.rate, in clk_mux_determine_rate_flags()
693 best_parent = parent; in clk_mux_determine_rate_flags()
699 return -EINVAL; in clk_mux_determine_rate_flags()
703 req->best_parent_hw = best_parent->hw; in clk_mux_determine_rate_flags()
704 req->best_parent_rate = best; in clk_mux_determine_rate_flags()
705 req->rate = best; in clk_mux_determine_rate_flags()
711 struct clk *__clk_lookup(const char *name) in __clk_lookup()
715 return !core ? NULL : core->hw->clk; in __clk_lookup()
722 struct clk *clk_user; in clk_core_get_boundaries()
726 *min_rate = core->min_rate; in clk_core_get_boundaries()
727 *max_rate = core->max_rate; in clk_core_get_boundaries()
729 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
730 *min_rate = max(*min_rate, clk_user->min_rate); in clk_core_get_boundaries()
732 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
733 *max_rate = min(*max_rate, clk_user->max_rate); in clk_core_get_boundaries()
740 struct clk *user; in clk_core_check_boundaries()
744 if (min_rate > core->max_rate || max_rate < core->min_rate) in clk_core_check_boundaries()
747 hlist_for_each_entry(user, &core->clks, clks_node) in clk_core_check_boundaries()
748 if (min_rate > user->max_rate || max_rate < user->min_rate) in clk_core_check_boundaries()
757 hw->core->min_rate = min_rate; in clk_hw_set_rate_range()
758 hw->core->max_rate = max_rate; in clk_hw_set_rate_range()
763 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
764 * @hw: mux type clk to determine rate on
765 * @req: rate request, also used to return preferred parent and frequencies
767 * Helper for finding best parent to provide a given frequency. This can be used
771 * Returns: 0 on success, -EERROR value on error
787 /*** clk api ***/
796 if (WARN(core->protect_count == 0, in clk_core_rate_unprotect()
797 "%s already unprotected\n", core->name)) in clk_core_rate_unprotect()
800 if (--core->protect_count > 0) in clk_core_rate_unprotect()
803 clk_core_rate_unprotect(core->parent); in clk_core_rate_unprotect()
813 return -EINVAL; in clk_core_rate_nuke_protect()
815 if (core->protect_count == 0) in clk_core_rate_nuke_protect()
818 ret = core->protect_count; in clk_core_rate_nuke_protect()
819 core->protect_count = 1; in clk_core_rate_nuke_protect()
826 * clk_rate_exclusive_put - release exclusivity over clock rate control
827 * @clk: the clk over which the exclusivity is released
833 * further up the parent chain of clocks. As a result, clocks up parent chain
843 void clk_rate_exclusive_put(struct clk *clk) in clk_rate_exclusive_put() argument
845 if (!clk) in clk_rate_exclusive_put()
854 if (WARN_ON(clk->exclusive_count <= 0)) in clk_rate_exclusive_put()
857 clk_core_rate_unprotect(clk->core); in clk_rate_exclusive_put()
858 clk->exclusive_count--; in clk_rate_exclusive_put()
871 if (core->protect_count == 0) in clk_core_rate_protect()
872 clk_core_rate_protect(core->parent); in clk_core_rate_protect()
874 core->protect_count++; in clk_core_rate_protect()
888 core->protect_count = count; in clk_core_rate_restore_protect()
892 * clk_rate_exclusive_get - get exclusivity over the clk rate control
893 * @clk: the clk over which the exclusity of rate control is requested
899 * further up the parent chain of clocks. As a result, clocks up parent chain
907 * Returns 0 on success, -EERROR otherwise
909 int clk_rate_exclusive_get(struct clk *clk) in clk_rate_exclusive_get() argument
911 if (!clk) in clk_rate_exclusive_get()
915 clk_core_rate_protect(clk->core); in clk_rate_exclusive_get()
916 clk->exclusive_count++; in clk_rate_exclusive_get()
930 if (WARN(core->prepare_count == 0, in clk_core_unprepare()
931 "%s already unprepared\n", core->name)) in clk_core_unprepare()
934 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_unprepare()
935 "Unpreparing critical %s\n", core->name)) in clk_core_unprepare()
938 if (core->flags & CLK_SET_RATE_GATE) in clk_core_unprepare()
941 if (--core->prepare_count > 0) in clk_core_unprepare()
944 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name); in clk_core_unprepare()
948 if (core->ops->unprepare) in clk_core_unprepare()
949 core->ops->unprepare(core->hw); in clk_core_unprepare()
952 clk_core_unprepare(core->parent); in clk_core_unprepare()
964 * clk_unprepare - undo preparation of a clock source
965 * @clk: the clk being unprepared
968 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
969 * if the operation may sleep. One example is a clk which is accessed over
970 * I2c. In the complex case a clk gate operation may require a fast and a slow
974 void clk_unprepare(struct clk *clk) in clk_unprepare() argument
976 if (IS_ERR_OR_NULL(clk)) in clk_unprepare()
979 clk_core_unprepare_lock(clk->core); in clk_unprepare()
992 if (core->prepare_count == 0) { in clk_core_prepare()
997 ret = clk_core_prepare(core->parent); in clk_core_prepare()
1003 if (core->ops->prepare) in clk_core_prepare()
1004 ret = core->ops->prepare(core->hw); in clk_core_prepare()
1012 core->prepare_count++; in clk_core_prepare()
1021 if (core->flags & CLK_SET_RATE_GATE) in clk_core_prepare()
1026 clk_core_unprepare(core->parent); in clk_core_prepare()
1044 * clk_prepare - prepare a clock source
1045 * @clk: the clk being prepared
1048 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
1049 * operation may sleep. One example is a clk which is accessed over I2c. In
1050 * the complex case a clk ungate operation may require a fast and a slow part.
1053 * Returns 0 on success, -EERROR otherwise.
1055 int clk_prepare(struct clk *clk) in clk_prepare() argument
1057 if (!clk) in clk_prepare()
1060 return clk_core_prepare_lock(clk->core); in clk_prepare()
1071 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name)) in clk_core_disable()
1074 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_disable()
1075 "Disabling critical %s\n", core->name)) in clk_core_disable()
1078 if (--core->enable_count > 0) in clk_core_disable()
1083 if (core->ops->disable) in clk_core_disable()
1084 core->ops->disable(core->hw); in clk_core_disable()
1088 clk_core_disable(core->parent); in clk_core_disable()
1101 * clk_disable - gate a clock
1102 * @clk: the clk being gated
1106 * clk if the operation is fast and will never sleep. One example is a
1107 * SoC-internal clk which is controlled via simple register writes. In the
1108 * complex case a clk gate operation may require a fast and a slow part. It is
1112 void clk_disable(struct clk *clk) in clk_disable() argument
1114 if (IS_ERR_OR_NULL(clk)) in clk_disable()
1117 clk_core_disable_lock(clk->core); in clk_disable()
1130 if (WARN(core->prepare_count == 0, in clk_core_enable()
1131 "Enabling unprepared %s\n", core->name)) in clk_core_enable()
1132 return -ESHUTDOWN; in clk_core_enable()
1134 if (core->enable_count == 0) { in clk_core_enable()
1135 ret = clk_core_enable(core->parent); in clk_core_enable()
1142 if (core->ops->enable) in clk_core_enable()
1143 ret = core->ops->enable(core->hw); in clk_core_enable()
1148 clk_core_disable(core->parent); in clk_core_enable()
1153 core->enable_count++; in clk_core_enable()
1170 * clk_gate_restore_context - restore context for poweroff
1181 struct clk_core *core = hw->core; in clk_gate_restore_context()
1183 if (core->enable_count) in clk_gate_restore_context()
1184 core->ops->enable(hw); in clk_gate_restore_context()
1186 core->ops->disable(hw); in clk_gate_restore_context()
1195 hlist_for_each_entry(child, &core->children, child_node) { in clk_core_save_context()
1201 if (core->ops && core->ops->save_context) in clk_core_save_context()
1202 ret = core->ops->save_context(core->hw); in clk_core_save_context()
1211 if (core->ops && core->ops->restore_context) in clk_core_restore_context()
1212 core->ops->restore_context(core->hw); in clk_core_restore_context()
1214 hlist_for_each_entry(child, &core->children, child_node) in clk_core_restore_context()
1219 * clk_save_context - save clock context for poweroff
1227 struct clk_core *clk; in clk_save_context() local
1230 hlist_for_each_entry(clk, &clk_root_list, child_node) { in clk_save_context()
1231 ret = clk_core_save_context(clk); in clk_save_context()
1236 hlist_for_each_entry(clk, &clk_orphan_list, child_node) { in clk_save_context()
1237 ret = clk_core_save_context(clk); in clk_save_context()
1247 * clk_restore_context - restore clock context after poweroff
1265 * clk_enable - ungate a clock
1266 * @clk: the clk being ungated
1269 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1270 * if the operation will never sleep. One example is a SoC-internal clk which
1271 * is controlled via simple register writes. In the complex case a clk ungate
1274 * must be called before clk_enable. Returns 0 on success, -EERROR
1277 int clk_enable(struct clk *clk) in clk_enable() argument
1279 if (!clk) in clk_enable()
1282 return clk_core_enable_lock(clk->core); in clk_enable()
1313 hlist_for_each_entry(child, &core->children, child_node) in clk_unprepare_unused_subtree()
1316 if (core->prepare_count) in clk_unprepare_unused_subtree()
1319 if (core->flags & CLK_IGNORE_UNUSED) in clk_unprepare_unused_subtree()
1324 if (core->ops->unprepare_unused) in clk_unprepare_unused_subtree()
1325 core->ops->unprepare_unused(core->hw); in clk_unprepare_unused_subtree()
1326 else if (core->ops->unprepare) in clk_unprepare_unused_subtree()
1327 core->ops->unprepare(core->hw); in clk_unprepare_unused_subtree()
1339 hlist_for_each_entry(child, &core->children, child_node) in clk_disable_unused_subtree()
1342 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1343 clk_core_prepare_enable(core->parent); in clk_disable_unused_subtree()
1347 if (core->enable_count) in clk_disable_unused_subtree()
1350 if (core->flags & CLK_IGNORE_UNUSED) in clk_disable_unused_subtree()
1354 * some gate clocks have special needs during the disable-unused in clk_disable_unused_subtree()
1360 if (core->ops->disable_unused) in clk_disable_unused_subtree()
1361 core->ops->disable_unused(core->hw); in clk_disable_unused_subtree()
1362 else if (core->ops->disable) in clk_disable_unused_subtree()
1363 core->ops->disable(core->hw); in clk_disable_unused_subtree()
1369 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1370 clk_core_disable_unprepare(core->parent); in clk_disable_unused_subtree()
1387 pr_warn("clk: Not disabling unused clocks\n"); in clk_disable_unused()
1391 pr_info("clk: Disabling unused clocks\n"); in clk_disable_unused()
1397 * Grab the prepare lock to keep the clk topology stable while iterating in clk_disable_unused()
1434 * - if the provider is not protected at all in clk_core_determine_round_nolock()
1435 * - if the calling consumer is the only one which has exclusivity in clk_core_determine_round_nolock()
1439 req->rate = core->rate; in clk_core_determine_round_nolock()
1440 } else if (core->ops->determine_rate) { in clk_core_determine_round_nolock()
1441 return core->ops->determine_rate(core->hw, req); in clk_core_determine_round_nolock()
1442 } else if (core->ops->round_rate) { in clk_core_determine_round_nolock()
1443 rate = core->ops->round_rate(core->hw, req->rate, in clk_core_determine_round_nolock()
1444 &req->best_parent_rate); in clk_core_determine_round_nolock()
1448 req->rate = rate; in clk_core_determine_round_nolock()
1450 return -EINVAL; in clk_core_determine_round_nolock()
1459 struct clk_core *parent; in clk_core_init_rate_req() local
1464 parent = core->parent; in clk_core_init_rate_req()
1465 if (parent) { in clk_core_init_rate_req()
1466 req->best_parent_hw = parent->hw; in clk_core_init_rate_req()
1467 req->best_parent_rate = parent->rate; in clk_core_init_rate_req()
1469 req->best_parent_hw = NULL; in clk_core_init_rate_req()
1470 req->best_parent_rate = 0; in clk_core_init_rate_req()
1476 return core->ops->determine_rate || core->ops->round_rate; in clk_core_can_round()
1485 req->rate = 0; in clk_core_round_rate_nolock()
1493 else if (core->flags & CLK_SET_RATE_PARENT) in clk_core_round_rate_nolock()
1494 return clk_core_round_rate_nolock(core->parent, req); in clk_core_round_rate_nolock()
1496 req->rate = core->rate; in clk_core_round_rate_nolock()
1501 * __clk_determine_rate - get the closest rate actually supported by a clock
1510 req->rate = 0; in __clk_determine_rate()
1514 return clk_core_round_rate_nolock(hw->core, req); in __clk_determine_rate()
1519 * clk_hw_round_rate() - round the given rate for a hw clk
1520 * @hw: the hw clk for which we are rounding a rate
1523 * Takes in a rate as input and rounds it to a rate that the clk can actually
1527 * For clk providers to call from within clk_ops such as .round_rate,
1530 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1531 * else returns the parent rate.
1538 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); in clk_hw_round_rate()
1541 ret = clk_core_round_rate_nolock(hw->core, &req); in clk_hw_round_rate()
1550 * clk_round_rate - round the given rate for a clk
1551 * @clk: the clk for which we are rounding a rate
1554 * Takes in a rate as input and rounds it to a rate that the clk can actually
1555 * use which is then returned. If clk doesn't support round_rate operation
1556 * then the parent rate is returned.
1558 long clk_round_rate(struct clk *clk, unsigned long rate) in clk_round_rate() argument
1563 if (!clk) in clk_round_rate()
1568 if (clk->exclusive_count) in clk_round_rate()
1569 clk_core_rate_unprotect(clk->core); in clk_round_rate()
1571 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); in clk_round_rate()
1574 ret = clk_core_round_rate_nolock(clk->core, &req); in clk_round_rate()
1576 if (clk->exclusive_count) in clk_round_rate()
1577 clk_core_rate_protect(clk->core); in clk_round_rate()
1589 * __clk_notify - call clk notifier chain
1590 * @core: clk that is changing rate
1591 * @msg: clk notifier type (see include/linux/clk.h)
1592 * @old_rate: old clk rate
1593 * @new_rate: new clk rate
1595 * Triggers a notifier call chain on the clk rate-change notification
1596 * for 'clk'. Passes a pointer to the struct clk and the previous
1613 if (cn->clk->core == core) { in __clk_notify()
1614 cnd.clk = cn->clk; in __clk_notify()
1615 ret = srcu_notifier_call_chain(&cn->notifier_head, msg, in __clk_notify()
1627 * @core: first clk in the subtree
1629 * Walks the subtree of clks starting with clk and recalculates accuracies as
1630 * it goes. Note that if a clk does not implement the .recalc_accuracy
1632 * parent.
1641 if (core->parent) in __clk_recalc_accuracies()
1642 parent_accuracy = core->parent->accuracy; in __clk_recalc_accuracies()
1644 if (core->ops->recalc_accuracy) in __clk_recalc_accuracies()
1645 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_recalc_accuracies()
1648 core->accuracy = parent_accuracy; in __clk_recalc_accuracies()
1650 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_accuracies()
1656 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) in clk_core_get_accuracy_recalc()
1663 * clk_get_accuracy - return the accuracy of clk
1664 * @clk: the clk whose accuracy is being returned
1666 * Simply returns the cached accuracy of the clk, unless
1669 * If clk is NULL then returns 0.
1671 long clk_get_accuracy(struct clk *clk) in clk_get_accuracy() argument
1675 if (!clk) in clk_get_accuracy()
1679 accuracy = clk_core_get_accuracy_recalc(clk->core); in clk_get_accuracy()
1691 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { in clk_recalc()
1692 rate = core->ops->recalc_rate(core->hw, parent_rate); in clk_recalc()
1700 * @core: first clk in the subtree
1701 * @msg: notification type (see include/linux/clk.h)
1703 * Walks the subtree of clks starting with clk and recalculates rates as it
1704 * goes. Note that if a clk does not implement the .recalc_rate callback then
1705 * it is assumed that the clock will take on the rate of its parent.
1718 old_rate = core->rate; in __clk_recalc_rates()
1720 if (core->parent) in __clk_recalc_rates()
1721 parent_rate = core->parent->rate; in __clk_recalc_rates()
1723 core->rate = clk_recalc(core, parent_rate); in __clk_recalc_rates()
1729 if (core->notifier_count && msg) in __clk_recalc_rates()
1730 __clk_notify(core, msg, old_rate, core->rate); in __clk_recalc_rates()
1732 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_rates()
1738 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) in clk_core_get_rate_recalc()
1745 * clk_get_rate - return the rate of clk
1746 * @clk: the clk whose rate is being returned
1748 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1750 * If clk is NULL then returns 0.
1752 unsigned long clk_get_rate(struct clk *clk) in clk_get_rate() argument
1756 if (!clk) in clk_get_rate()
1760 rate = clk_core_get_rate_recalc(clk->core); in clk_get_rate()
1768 struct clk_core *parent) in clk_fetch_parent_index() argument
1772 if (!parent) in clk_fetch_parent_index()
1773 return -EINVAL; in clk_fetch_parent_index()
1775 for (i = 0; i < core->num_parents; i++) { in clk_fetch_parent_index()
1777 if (core->parents[i].core == parent) in clk_fetch_parent_index()
1781 if (core->parents[i].core) in clk_fetch_parent_index()
1785 if (core->parents[i].hw) { in clk_fetch_parent_index()
1786 if (core->parents[i].hw == parent->hw) in clk_fetch_parent_index()
1794 if (parent == clk_core_get(core, i)) in clk_fetch_parent_index()
1798 if (core->parents[i].name && in clk_fetch_parent_index()
1799 !strcmp(parent->name, core->parents[i].name)) in clk_fetch_parent_index()
1803 if (i == core->num_parents) in clk_fetch_parent_index()
1804 return -EINVAL; in clk_fetch_parent_index()
1806 core->parents[i].core = parent; in clk_fetch_parent_index()
1811 * clk_hw_get_parent_index - return the index of the parent clock
1812 * @hw: clk_hw associated with the clk being consumed
1814 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1815 * clock does not have a current parent.
1819 struct clk_hw *parent = clk_hw_get_parent(hw); in clk_hw_get_parent_index() local
1821 if (WARN_ON(parent == NULL)) in clk_hw_get_parent_index()
1822 return -EINVAL; in clk_hw_get_parent_index()
1824 return clk_fetch_parent_index(hw->core, parent->core); in clk_hw_get_parent_index()
1835 core->orphan = is_orphan; in clk_core_update_orphan_status()
1837 hlist_for_each_entry(child, &core->children, child_node) in clk_core_update_orphan_status()
1843 bool was_orphan = core->orphan; in clk_reparent()
1845 hlist_del(&core->child_node); in clk_reparent()
1848 bool becomes_orphan = new_parent->orphan; in clk_reparent()
1851 if (new_parent->new_child == core) in clk_reparent()
1852 new_parent->new_child = NULL; in clk_reparent()
1854 hlist_add_head(&core->child_node, &new_parent->children); in clk_reparent()
1859 hlist_add_head(&core->child_node, &clk_orphan_list); in clk_reparent()
1864 core->parent = new_parent; in clk_reparent()
1868 struct clk_core *parent) in __clk_set_parent_before() argument
1871 struct clk_core *old_parent = core->parent; in __clk_set_parent_before()
1885 * parent and also protect against a race with clk_enable() by in __clk_set_parent_before()
1886 * forcing the clock and the new parent on. This ensures that all in __clk_set_parent_before()
1893 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */ in __clk_set_parent_before()
1894 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_before()
1896 clk_core_prepare_enable(parent); in __clk_set_parent_before()
1900 if (core->prepare_count) { in __clk_set_parent_before()
1901 clk_core_prepare_enable(parent); in __clk_set_parent_before()
1905 /* update the clk tree topology */ in __clk_set_parent_before()
1907 clk_reparent(core, parent); in __clk_set_parent_before()
1914 struct clk_core *parent, in __clk_set_parent_after() argument
1921 if (core->prepare_count) { in __clk_set_parent_after()
1926 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ in __clk_set_parent_after()
1927 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_after()
1928 clk_core_disable_unprepare(parent); in __clk_set_parent_after()
1933 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent, in __clk_set_parent() argument
1940 old_parent = __clk_set_parent_before(core, parent); in __clk_set_parent()
1942 trace_clk_set_parent(core, parent); in __clk_set_parent()
1945 if (parent && core->ops->set_parent) in __clk_set_parent()
1946 ret = core->ops->set_parent(core->hw, p_index); in __clk_set_parent()
1948 trace_clk_set_parent_complete(core, parent); in __clk_set_parent()
1954 __clk_set_parent_after(core, old_parent, parent); in __clk_set_parent()
1959 __clk_set_parent_after(core, parent, old_parent); in __clk_set_parent()
1966 * @core: first clk in the subtree
1967 * @parent_rate: the "future" rate of clk's parent
1969 * Walks the subtree of clks starting with clk, speculating rates as it
1973 * pre-rate change notifications and returns early if no clks in the
1974 * subtree have subscribed to the notifications. Note that if a clk does not
1976 * take on the rate of its parent.
1990 if (core->notifier_count) in __clk_speculate_rates()
1991 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); in __clk_speculate_rates()
1994 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", in __clk_speculate_rates()
1995 __func__, core->name, ret); in __clk_speculate_rates()
1999 hlist_for_each_entry(child, &core->children, child_node) { in __clk_speculate_rates()
2014 core->new_rate = new_rate; in clk_calc_subtree()
2015 core->new_parent = new_parent; in clk_calc_subtree()
2016 core->new_parent_index = p_index; in clk_calc_subtree()
2017 /* include clk in new parent's PRE_RATE_CHANGE notifications */ in clk_calc_subtree()
2018 core->new_child = NULL; in clk_calc_subtree()
2019 if (new_parent && new_parent != core->parent) in clk_calc_subtree()
2020 new_parent->new_child = core; in clk_calc_subtree()
2022 hlist_for_each_entry(child, &core->children, child_node) { in clk_calc_subtree()
2023 child->new_rate = clk_recalc(child, new_rate); in clk_calc_subtree()
2024 clk_calc_subtree(child, child->new_rate, NULL, 0); in clk_calc_subtree()
2036 struct clk_core *old_parent, *parent; in clk_calc_new_rates() local
2048 /* save parent rate, if it exists */ in clk_calc_new_rates()
2049 parent = old_parent = core->parent; in clk_calc_new_rates()
2050 if (parent) in clk_calc_new_rates()
2051 best_parent_rate = parent->rate; in clk_calc_new_rates()
2055 /* find the closest rate and parent clk/rate */ in clk_calc_new_rates()
2071 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; in clk_calc_new_rates()
2075 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { in clk_calc_new_rates()
2076 /* pass-through clock without adjustable parent */ in clk_calc_new_rates()
2077 core->new_rate = core->rate; in clk_calc_new_rates()
2080 /* pass-through clock with adjustable parent */ in clk_calc_new_rates()
2081 top = clk_calc_new_rates(parent, rate); in clk_calc_new_rates()
2082 new_rate = parent->new_rate; in clk_calc_new_rates()
2086 /* some clocks must be gated to change parent */ in clk_calc_new_rates()
2087 if (parent != old_parent && in clk_calc_new_rates()
2088 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { in clk_calc_new_rates()
2090 __func__, core->name); in clk_calc_new_rates()
2094 /* try finding the new parent index */ in clk_calc_new_rates()
2095 if (parent && core->num_parents > 1) { in clk_calc_new_rates()
2096 p_index = clk_fetch_parent_index(core, parent); in clk_calc_new_rates()
2098 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_calc_new_rates()
2099 __func__, parent->name, core->name); in clk_calc_new_rates()
2104 if ((core->flags & CLK_SET_RATE_PARENT) && parent && in clk_calc_new_rates()
2105 best_parent_rate != parent->rate) in clk_calc_new_rates()
2106 top = clk_calc_new_rates(parent, best_parent_rate); in clk_calc_new_rates()
2109 clk_calc_subtree(core, new_rate, parent, p_index); in clk_calc_new_rates()
2125 if (core->rate == core->new_rate) in clk_propagate_rate_change()
2128 if (core->notifier_count) { in clk_propagate_rate_change()
2129 ret = __clk_notify(core, event, core->rate, core->new_rate); in clk_propagate_rate_change()
2134 hlist_for_each_entry(child, &core->children, child_node) { in clk_propagate_rate_change()
2136 if (child->new_parent && child->new_parent != core) in clk_propagate_rate_change()
2143 /* handle the new child who might not be in core->children yet */ in clk_propagate_rate_change()
2144 if (core->new_child) { in clk_propagate_rate_change()
2145 tmp_clk = clk_propagate_rate_change(core->new_child, event); in clk_propagate_rate_change()
2165 struct clk_core *parent = NULL; in clk_change_rate() local
2167 old_rate = core->rate; in clk_change_rate()
2169 if (core->new_parent) { in clk_change_rate()
2170 parent = core->new_parent; in clk_change_rate()
2171 best_parent_rate = core->new_parent->rate; in clk_change_rate()
2172 } else if (core->parent) { in clk_change_rate()
2173 parent = core->parent; in clk_change_rate()
2174 best_parent_rate = core->parent->rate; in clk_change_rate()
2180 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2189 if (core->new_parent && core->new_parent != core->parent) { in clk_change_rate()
2190 old_parent = __clk_set_parent_before(core, core->new_parent); in clk_change_rate()
2191 trace_clk_set_parent(core, core->new_parent); in clk_change_rate()
2193 if (core->ops->set_rate_and_parent) { in clk_change_rate()
2195 core->ops->set_rate_and_parent(core->hw, core->new_rate, in clk_change_rate()
2197 core->new_parent_index); in clk_change_rate()
2198 } else if (core->ops->set_parent) { in clk_change_rate()
2199 core->ops->set_parent(core->hw, core->new_parent_index); in clk_change_rate()
2202 trace_clk_set_parent_complete(core, core->new_parent); in clk_change_rate()
2203 __clk_set_parent_after(core, core->new_parent, old_parent); in clk_change_rate()
2206 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2207 clk_core_prepare_enable(parent); in clk_change_rate()
2209 trace_clk_set_rate(core, core->new_rate); in clk_change_rate()
2211 if (!skip_set_rate && core->ops->set_rate) in clk_change_rate()
2212 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); in clk_change_rate()
2214 trace_clk_set_rate_complete(core, core->new_rate); in clk_change_rate()
2216 core->rate = clk_recalc(core, best_parent_rate); in clk_change_rate()
2218 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2227 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2228 clk_core_disable_unprepare(parent); in clk_change_rate()
2230 if (core->notifier_count && old_rate != core->rate) in clk_change_rate()
2231 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); in clk_change_rate()
2233 if (core->flags & CLK_RECALC_NEW_RATES) in clk_change_rate()
2234 (void)clk_calc_new_rates(core, core->new_rate); in clk_change_rate()
2240 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { in clk_change_rate()
2242 if (child->new_parent && child->new_parent != core) in clk_change_rate()
2247 /* handle the new child who might not be in core->children yet */ in clk_change_rate()
2248 if (core->new_child) in clk_change_rate()
2249 clk_change_rate(core->new_child); in clk_change_rate()
2299 return -EBUSY; in clk_core_set_rate_nolock()
2304 return -EINVAL; in clk_core_set_rate_nolock()
2314 fail_clk->name); in clk_core_set_rate_nolock()
2316 ret = -EBUSY; in clk_core_set_rate_nolock()
2323 core->req_rate = req_rate; in clk_core_set_rate_nolock()
2331 * clk_set_rate - specify a new rate for clk
2332 * @clk: the clk whose rate is being changed
2333 * @rate: the new rate for clk
2335 * In the simplest case clk_set_rate will only adjust the rate of clk.
2338 * propagate up to clk's parent; whether or not this happens depends on the
2339 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2340 * after calling .round_rate then upstream parent propagation is ignored. If
2341 * *parent_rate comes back with a new rate for clk's parent then we propagate
2342 * up to clk's parent and set its rate. Upward propagation will continue
2343 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2344 * .round_rate stops requesting changes to clk's parent_rate.
2349 * Returns 0 on success, -EERROR otherwise.
2351 int clk_set_rate(struct clk *clk, unsigned long rate) in clk_set_rate() argument
2355 if (!clk) in clk_set_rate()
2361 if (clk->exclusive_count) in clk_set_rate()
2362 clk_core_rate_unprotect(clk->core); in clk_set_rate()
2364 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate()
2366 if (clk->exclusive_count) in clk_set_rate()
2367 clk_core_rate_protect(clk->core); in clk_set_rate()
2376 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2377 * @clk: the clk whose rate is being changed
2378 * @rate: the new rate for clk
2392 * Returns 0 on success, -EERROR otherwise.
2394 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) in clk_set_rate_exclusive() argument
2398 if (!clk) in clk_set_rate_exclusive()
2410 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_exclusive()
2412 clk_core_rate_protect(clk->core); in clk_set_rate_exclusive()
2413 clk->exclusive_count++; in clk_set_rate_exclusive()
2423 * clk_set_rate_range - set a rate range for a clock source
2424 * @clk: clock source
2430 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) in clk_set_rate_range() argument
2435 if (!clk) in clk_set_rate_range()
2439 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", in clk_set_rate_range()
2440 __func__, clk->core->name, clk->dev_id, clk->con_id, in clk_set_rate_range()
2442 return -EINVAL; in clk_set_rate_range()
2447 if (clk->exclusive_count) in clk_set_rate_range()
2448 clk_core_rate_unprotect(clk->core); in clk_set_rate_range()
2451 old_min = clk->min_rate; in clk_set_rate_range()
2452 old_max = clk->max_rate; in clk_set_rate_range()
2453 clk->min_rate = min; in clk_set_rate_range()
2454 clk->max_rate = max; in clk_set_rate_range()
2456 if (!clk_core_check_boundaries(clk->core, min, max)) { in clk_set_rate_range()
2457 ret = -EINVAL; in clk_set_rate_range()
2461 rate = clk_core_get_rate_nolock(clk->core); in clk_set_rate_range()
2470 * - round_rate() was not favorable and fell on the wrong in clk_set_rate_range()
2472 * - the determine_rate() callback does not really check for in clk_set_rate_range()
2481 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_range()
2484 clk->min_rate = old_min; in clk_set_rate_range()
2485 clk->max_rate = old_max; in clk_set_rate_range()
2490 if (clk->exclusive_count) in clk_set_rate_range()
2491 clk_core_rate_protect(clk->core); in clk_set_rate_range()
2500 * clk_set_min_rate - set a minimum clock rate for a clock source
2501 * @clk: clock source
2506 int clk_set_min_rate(struct clk *clk, unsigned long rate) in clk_set_min_rate() argument
2508 if (!clk) in clk_set_min_rate()
2511 return clk_set_rate_range(clk, rate, clk->max_rate); in clk_set_min_rate()
2516 * clk_set_max_rate - set a maximum clock rate for a clock source
2517 * @clk: clock source
2522 int clk_set_max_rate(struct clk *clk, unsigned long rate) in clk_set_max_rate() argument
2524 if (!clk) in clk_set_max_rate()
2527 return clk_set_rate_range(clk, clk->min_rate, rate); in clk_set_max_rate()
2532 * clk_get_parent - return the parent of a clk
2533 * @clk: the clk whose parent gets returned
2535 * Simply returns clk->parent. Returns NULL if clk is NULL.
2537 struct clk *clk_get_parent(struct clk *clk) in clk_get_parent() argument
2539 struct clk *parent; in clk_get_parent() local
2541 if (!clk) in clk_get_parent()
2545 /* TODO: Create a per-user clk and change callers to call clk_put */ in clk_get_parent()
2546 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; in clk_get_parent()
2549 return parent; in clk_get_parent()
2557 if (core->num_parents > 1 && core->ops->get_parent) in __clk_init_parent()
2558 index = core->ops->get_parent(core->hw); in __clk_init_parent()
2576 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); in clk_hw_reparent()
2580 * clk_has_parent - check if a clock is a possible parent for another
2581 * @clk: clock source
2582 * @parent: parent clock source
2585 * the parent of another without actually changing the parent.
2587 * Returns true if @parent is a possible parent for @clk, false otherwise.
2589 bool clk_has_parent(struct clk *clk, struct clk *parent) in clk_has_parent() argument
2595 if (!clk || !parent) in clk_has_parent()
2598 core = clk->core; in clk_has_parent()
2599 parent_core = parent->core; in clk_has_parent()
2601 /* Optimize for the case where the parent is already the parent. */ in clk_has_parent()
2602 if (core->parent == parent_core) in clk_has_parent()
2605 for (i = 0; i < core->num_parents; i++) in clk_has_parent()
2606 if (!strcmp(core->parents[i].name, parent_core->name)) in clk_has_parent()
2614 struct clk_core *parent) in clk_core_set_parent_nolock() argument
2625 if (core->parent == parent) in clk_core_set_parent_nolock()
2628 /* verify ops for multi-parent clks */ in clk_core_set_parent_nolock()
2629 if (core->num_parents > 1 && !core->ops->set_parent) in clk_core_set_parent_nolock()
2630 return -EPERM; in clk_core_set_parent_nolock()
2632 /* check that we are allowed to re-parent if the clock is in use */ in clk_core_set_parent_nolock()
2633 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) in clk_core_set_parent_nolock()
2634 return -EBUSY; in clk_core_set_parent_nolock()
2637 return -EBUSY; in clk_core_set_parent_nolock()
2639 /* try finding the new parent index */ in clk_core_set_parent_nolock()
2640 if (parent) { in clk_core_set_parent_nolock()
2641 p_index = clk_fetch_parent_index(core, parent); in clk_core_set_parent_nolock()
2643 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_core_set_parent_nolock()
2644 __func__, parent->name, core->name); in clk_core_set_parent_nolock()
2647 p_rate = parent->rate; in clk_core_set_parent_nolock()
2661 /* do the re-parent */ in clk_core_set_parent_nolock()
2662 ret = __clk_set_parent(core, parent, p_index); in clk_core_set_parent_nolock()
2678 int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent) in clk_hw_set_parent() argument
2680 return clk_core_set_parent_nolock(hw->core, parent->core); in clk_hw_set_parent()
2685 * clk_set_parent - switch the parent of a mux clk
2686 * @clk: the mux clk whose input we are switching
2687 * @parent: the new input to clk
2689 * Re-parent clk to use parent as its new input source. If clk is in
2690 * prepared state, the clk will get enabled for the duration of this call. If
2691 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2693 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2695 * After successfully changing clk's parent clk_set_parent will update the
2696 * clk topology, sysfs topology and propagate rate recalculation via
2699 * Returns 0 on success, -EERROR otherwise.
2701 int clk_set_parent(struct clk *clk, struct clk *parent) in clk_set_parent() argument
2705 if (!clk) in clk_set_parent()
2710 if (clk->exclusive_count) in clk_set_parent()
2711 clk_core_rate_unprotect(clk->core); in clk_set_parent()
2713 ret = clk_core_set_parent_nolock(clk->core, in clk_set_parent()
2714 parent ? parent->core : NULL); in clk_set_parent()
2716 if (clk->exclusive_count) in clk_set_parent()
2717 clk_core_rate_protect(clk->core); in clk_set_parent()
2727 int ret = -EINVAL; in clk_core_set_phase_nolock()
2735 return -EBUSY; in clk_core_set_phase_nolock()
2739 if (core->ops->set_phase) { in clk_core_set_phase_nolock()
2740 ret = core->ops->set_phase(core->hw, degrees); in clk_core_set_phase_nolock()
2742 core->phase = degrees; in clk_core_set_phase_nolock()
2751 * clk_set_phase - adjust the phase shift of a clock signal
2752 * @clk: clock signal source
2756 * degrees. Returns 0 on success, -EERROR otherwise.
2760 * phase locked-loop clock signal generators we may shift phase with
2767 * clock accuracy. A parent clock phase attribute does not have an
2770 int clk_set_phase(struct clk *clk, int degrees) in clk_set_phase() argument
2774 if (!clk) in clk_set_phase()
2784 if (clk->exclusive_count) in clk_set_phase()
2785 clk_core_rate_unprotect(clk->core); in clk_set_phase()
2787 ret = clk_core_set_phase_nolock(clk->core, degrees); in clk_set_phase()
2789 if (clk->exclusive_count) in clk_set_phase()
2790 clk_core_rate_protect(clk->core); in clk_set_phase()
2803 if (!core->ops->get_phase) in clk_core_get_phase()
2807 ret = core->ops->get_phase(core->hw); in clk_core_get_phase()
2809 core->phase = ret; in clk_core_get_phase()
2815 * clk_get_phase - return the phase shift of a clock signal
2816 * @clk: clock signal source
2819 * -EERROR.
2821 int clk_get_phase(struct clk *clk) in clk_get_phase() argument
2825 if (!clk) in clk_get_phase()
2829 ret = clk_core_get_phase(clk->core); in clk_get_phase()
2839 core->duty.num = 1; in clk_core_reset_duty_cycle_nolock()
2840 core->duty.den = 2; in clk_core_reset_duty_cycle_nolock()
2847 struct clk_duty *duty = &core->duty; in clk_core_update_duty_cycle_nolock()
2850 if (!core->ops->get_duty_cycle) in clk_core_update_duty_cycle_nolock()
2853 ret = core->ops->get_duty_cycle(core->hw, duty); in clk_core_update_duty_cycle_nolock()
2858 if (duty->den == 0 || duty->num > duty->den) { in clk_core_update_duty_cycle_nolock()
2859 ret = -EINVAL; in clk_core_update_duty_cycle_nolock()
2874 if (core->parent && in clk_core_update_duty_cycle_parent_nolock()
2875 core->flags & CLK_DUTY_CYCLE_PARENT) { in clk_core_update_duty_cycle_parent_nolock()
2876 ret = clk_core_update_duty_cycle_nolock(core->parent); in clk_core_update_duty_cycle_parent_nolock()
2877 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_update_duty_cycle_parent_nolock()
2896 return -EBUSY; in clk_core_set_duty_cycle_nolock()
2900 if (!core->ops->set_duty_cycle) in clk_core_set_duty_cycle_nolock()
2903 ret = core->ops->set_duty_cycle(core->hw, duty); in clk_core_set_duty_cycle_nolock()
2905 memcpy(&core->duty, duty, sizeof(*duty)); in clk_core_set_duty_cycle_nolock()
2917 if (core->parent && in clk_core_set_duty_cycle_parent_nolock()
2918 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) { in clk_core_set_duty_cycle_parent_nolock()
2919 ret = clk_core_set_duty_cycle_nolock(core->parent, duty); in clk_core_set_duty_cycle_parent_nolock()
2920 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_set_duty_cycle_parent_nolock()
2927 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2928 * @clk: clock signal source
2937 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den) in clk_set_duty_cycle() argument
2942 if (!clk) in clk_set_duty_cycle()
2947 return -EINVAL; in clk_set_duty_cycle()
2954 if (clk->exclusive_count) in clk_set_duty_cycle()
2955 clk_core_rate_unprotect(clk->core); in clk_set_duty_cycle()
2957 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty); in clk_set_duty_cycle()
2959 if (clk->exclusive_count) in clk_set_duty_cycle()
2960 clk_core_rate_protect(clk->core); in clk_set_duty_cycle()
2971 struct clk_duty *duty = &core->duty; in clk_core_get_scaled_duty_cycle()
2978 ret = mult_frac(scale, duty->num, duty->den); in clk_core_get_scaled_duty_cycle()
2986 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2987 * @clk: clock signal source
2993 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale) in clk_get_scaled_duty_cycle() argument
2995 if (!clk) in clk_get_scaled_duty_cycle()
2998 return clk_core_get_scaled_duty_cycle(clk->core, scale); in clk_get_scaled_duty_cycle()
3003 * clk_is_match - check if two clk's point to the same hardware clock
3004 * @p: clk compared against q
3005 * @q: clk compared against p
3007 * Returns true if the two struct clk pointers both point to the same hardware
3008 * clock node. Put differently, returns true if struct clk *p and struct clk *q
3013 bool clk_is_match(const struct clk *p, const struct clk *q) in clk_is_match()
3015 /* trivial case: identical struct clk's or both NULL */ in clk_is_match()
3019 /* true if clk->core pointers match. Avoid dereferencing garbage */ in clk_is_match()
3021 if (p->core == q->core) in clk_is_match()
3048 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ", in clk_summary_show_one()
3050 30 - level * 3, c->name, in clk_summary_show_one()
3051 c->enable_count, c->prepare_count, c->protect_count, in clk_summary_show_one()
3059 seq_puts(s, "-----"); in clk_summary_show_one()
3071 hlist_for_each_entry(child, &c->children, child_node) in clk_summary_show_subtree()
3078 struct hlist_head **lists = (struct hlist_head **)s->private; in clk_summary_show()
3082 …seq_puts(s, "-------------------------------------------------------------------------------------… in clk_summary_show()
3104 seq_printf(s, "\"%s\": { ", c->name); in clk_dump_one()
3105 seq_printf(s, "\"enable_count\": %d,", c->enable_count); in clk_dump_one()
3106 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); in clk_dump_one()
3107 seq_printf(s, "\"protect_count\": %d,", c->protect_count); in clk_dump_one()
3125 hlist_for_each_entry(child, &c->children, child_node) { in clk_dump_subtree()
3137 struct hlist_head **lists = (struct hlist_head **)s->private; in clk_dump_show()
3185 ret = clk_prepare_enable(core->hw->clk); in clk_prepare_enable_set()
3187 clk_disable_unprepare(core->hw->clk); in clk_prepare_enable_set()
3196 *val = core->enable_count && core->prepare_count; in clk_prepare_enable_get()
3212 *val = core->rate; in clk_rate_get()
3240 struct clk_core *core = s->private; in clk_flags_show()
3241 unsigned long flags = core->flags; in clk_flags_show()
3262 struct clk_core *parent; in possible_parent_show() local
3266 * Go through the following options to fetch a parent's name. in possible_parent_show()
3268 * 1. Fetch the registered parent clock and use its name in possible_parent_show()
3271 * 4. Fetch parent clock's clock-output-name if DT index was set in possible_parent_show()
3273 * This may still fail in some cases, such as when the parent is in possible_parent_show()
3277 parent = clk_core_get_parent_by_index(core, i); in possible_parent_show()
3278 if (parent) { in possible_parent_show()
3279 seq_puts(s, parent->name); in possible_parent_show()
3280 } else if (core->parents[i].name) { in possible_parent_show()
3281 seq_puts(s, core->parents[i].name); in possible_parent_show()
3282 } else if (core->parents[i].fw_name) { in possible_parent_show()
3283 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name); in possible_parent_show()
3285 if (core->parents[i].index >= 0) in possible_parent_show()
3286 name = of_clk_get_parent_name(core->of_node, core->parents[i].index); in possible_parent_show()
3298 struct clk_core *core = s->private; in possible_parents_show()
3301 for (i = 0; i < core->num_parents - 1; i++) in possible_parents_show()
3312 struct clk_core *core = s->private; in current_parent_show()
3314 if (core->parent) in current_parent_show()
3315 seq_printf(s, "%s\n", core->parent->name); in current_parent_show()
3323 struct clk_core *core = s->private; in clk_duty_cycle_show()
3324 struct clk_duty *duty = &core->duty; in clk_duty_cycle_show()
3326 seq_printf(s, "%u/%u\n", duty->num, duty->den); in clk_duty_cycle_show()
3334 struct clk_core *core = s->private; in clk_min_rate_show()
3348 struct clk_core *core = s->private; in clk_max_rate_show()
3367 root = debugfs_create_dir(core->name, pdentry); in clk_debug_create_one()
3368 core->dentry = root; in clk_debug_create_one()
3374 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy); in clk_debug_create_one()
3375 debugfs_create_u32("clk_phase", 0444, root, &core->phase); in clk_debug_create_one()
3377 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count); in clk_debug_create_one()
3378 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count); in clk_debug_create_one()
3379 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count); in clk_debug_create_one()
3380 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count); in clk_debug_create_one()
3388 if (core->num_parents > 0) in clk_debug_create_one()
3392 if (core->num_parents > 1) in clk_debug_create_one()
3396 if (core->ops->debug_init) in clk_debug_create_one()
3397 core->ops->debug_init(core->hw, core->dentry); in clk_debug_create_one()
3401 * clk_debug_register - add a clk node to the debugfs clk directory
3402 * @core: the clk being added to the debugfs clk directory
3404 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3405 * initialized. Otherwise it bails out early since the debugfs clk directory
3411 hlist_add_head(&core->debug_node, &clk_debug_list); in clk_debug_register()
3418 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3419 * @core: the clk being removed from the debugfs clk directory
3421 * Dynamically removes a clk and all its child nodes from the
3422 * debugfs clk directory if clk->dentry points to debugfs created by
3428 hlist_del_init(&core->debug_node); in clk_debug_unregister()
3429 debugfs_remove_recursive(core->dentry); in clk_debug_unregister()
3430 core->dentry = NULL; in clk_debug_unregister()
3435 * clk_debug_init - lazily populate the debugfs clk directory
3439 * populates the debugfs clk directory once at boot-time when we know that
3440 * debugfs is setup. It should only be called once at boot-time, all other clks
3452 pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n"); in clk_debug_init()
3454 pr_warn("** This means that this kernel is built to expose clk operations **\n"); in clk_debug_init()
3455 pr_warn("** such as parent or rate setting, enabling, disabling, etc. **\n"); in clk_debug_init()
3465 rootdir = debugfs_create_dir("clk", NULL); in clk_debug_init()
3500 * parent. in clk_core_reparent_orphans_nolock()
3503 struct clk_core *parent = __clk_init_parent(orphan); in clk_core_reparent_orphans_nolock() local
3509 * are enabled during init but might not have a parent yet. in clk_core_reparent_orphans_nolock()
3511 if (parent) { in clk_core_reparent_orphans_nolock()
3512 /* update the clk tree topology */ in clk_core_reparent_orphans_nolock()
3513 __clk_set_parent_before(orphan, parent); in clk_core_reparent_orphans_nolock()
3514 __clk_set_parent_after(orphan, parent, NULL); in clk_core_reparent_orphans_nolock()
3526 * 'req_rate' is set to something non-zero so that in clk_core_reparent_orphans_nolock()
3529 orphan->req_rate = orphan->rate; in clk_core_reparent_orphans_nolock()
3535 * __clk_core_init - initialize the data structures in a struct clk_core
3539 * parent and rate and sets them both.
3544 struct clk_core *parent; in __clk_core_init() local
3549 return -EINVAL; in __clk_core_init()
3554 * Set hw->core after grabbing the prepare_lock to synchronize with in __clk_core_init()
3555 * callers of clk_core_fill_parent_index() where we treat hw->core in __clk_core_init()
3556 * being NULL as the clk not being registered yet. This is crucial so in __clk_core_init()
3557 * that clks aren't parented until their parent is fully registered. in __clk_core_init()
3559 core->hw->core = core; in __clk_core_init()
3566 if (clk_core_lookup(core->name)) { in __clk_core_init()
3567 pr_debug("%s: clk %s already initialized\n", in __clk_core_init()
3568 __func__, core->name); in __clk_core_init()
3569 ret = -EEXIST; in __clk_core_init()
3573 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */ in __clk_core_init()
3574 if (core->ops->set_rate && in __clk_core_init()
3575 !((core->ops->round_rate || core->ops->determine_rate) && in __clk_core_init()
3576 core->ops->recalc_rate)) { in __clk_core_init()
3578 __func__, core->name); in __clk_core_init()
3579 ret = -EINVAL; in __clk_core_init()
3583 if (core->ops->set_parent && !core->ops->get_parent) { in __clk_core_init()
3585 __func__, core->name); in __clk_core_init()
3586 ret = -EINVAL; in __clk_core_init()
3590 if (core->num_parents > 1 && !core->ops->get_parent) { in __clk_core_init()
3592 __func__, core->name); in __clk_core_init()
3593 ret = -EINVAL; in __clk_core_init()
3597 if (core->ops->set_rate_and_parent && in __clk_core_init()
3598 !(core->ops->set_parent && core->ops->set_rate)) { in __clk_core_init()
3600 __func__, core->name); in __clk_core_init()
3601 ret = -EINVAL; in __clk_core_init()
3606 * optional platform-specific magic in __clk_core_init()
3614 * such as rate, parent, etc ... in __clk_core_init()
3619 if (core->ops->init) { in __clk_core_init()
3620 ret = core->ops->init(core->hw); in __clk_core_init()
3625 parent = core->parent = __clk_init_parent(core); in __clk_core_init()
3628 * Populate core->parent if parent has already been clk_core_init'd. If in __clk_core_init()
3629 * parent has not yet been clk_core_init'd then place clk in the orphan in __clk_core_init()
3630 * list. If clk doesn't have any parents then place it in the root in __clk_core_init()
3631 * clk list. in __clk_core_init()
3633 * Every time a new clk is clk_init'd then we walk the list of orphan in __clk_core_init()
3634 * clocks and re-parent any that are children of the clock currently in __clk_core_init()
3637 if (parent) { in __clk_core_init()
3638 hlist_add_head(&core->child_node, &parent->children); in __clk_core_init()
3639 core->orphan = parent->orphan; in __clk_core_init()
3640 } else if (!core->num_parents) { in __clk_core_init()
3641 hlist_add_head(&core->child_node, &clk_root_list); in __clk_core_init()
3642 core->orphan = false; in __clk_core_init()
3644 hlist_add_head(&core->child_node, &clk_orphan_list); in __clk_core_init()
3645 core->orphan = true; in __clk_core_init()
3649 * Set clk's accuracy. The preferred method is to use in __clk_core_init()
3651 * fallback is to use the parent's accuracy. If a clock doesn't have a in __clk_core_init()
3652 * parent (or is orphaned) then accuracy is set to zero (perfect in __clk_core_init()
3655 if (core->ops->recalc_accuracy) in __clk_core_init()
3656 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_core_init()
3657 clk_core_get_accuracy_no_lock(parent)); in __clk_core_init()
3658 else if (parent) in __clk_core_init()
3659 core->accuracy = parent->accuracy; in __clk_core_init()
3661 core->accuracy = 0; in __clk_core_init()
3664 * Set clk's phase by clk_core_get_phase() caching the phase. in __clk_core_init()
3665 * Since a phase is by definition relative to its parent, just in __clk_core_init()
3671 pr_warn("%s: Failed to get phase for clk '%s'\n", __func__, in __clk_core_init()
3672 core->name); in __clk_core_init()
3677 * Set clk's duty cycle. in __clk_core_init()
3682 * Set clk's rate. The preferred method is to use .recalc_rate. For in __clk_core_init()
3684 * parent's rate. If a clock doesn't have a parent (or is orphaned) in __clk_core_init()
3687 if (core->ops->recalc_rate) in __clk_core_init()
3688 rate = core->ops->recalc_rate(core->hw, in __clk_core_init()
3689 clk_core_get_rate_nolock(parent)); in __clk_core_init()
3690 else if (parent) in __clk_core_init()
3691 rate = parent->rate; in __clk_core_init()
3694 core->rate = core->req_rate = rate; in __clk_core_init()
3701 if (core->flags & CLK_IS_CRITICAL) { in __clk_core_init()
3706 pr_warn("%s: critical clk '%s' failed to prepare\n", in __clk_core_init()
3707 __func__, core->name); in __clk_core_init()
3715 pr_warn("%s: critical clk '%s' failed to enable\n", in __clk_core_init()
3716 __func__, core->name); in __clk_core_init()
3727 hlist_del_init(&core->child_node); in __clk_core_init()
3728 core->hw->core = NULL; in __clk_core_init()
3740 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3741 * @core: clk to add consumer to
3742 * @clk: consumer to link to a clk
3744 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk) in clk_core_link_consumer() argument
3747 hlist_add_head(&clk->clks_node, &core->clks); in clk_core_link_consumer()
3752 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3753 * @clk: consumer to unlink
3755 static void clk_core_unlink_consumer(struct clk *clk) in clk_core_unlink_consumer() argument
3758 hlist_del(&clk->clks_node); in clk_core_unlink_consumer()
3762 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3763 * @core: clk to allocate a consumer for
3767 * Returns: clk consumer left unlinked from the consumer list
3769 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id, in alloc_clk()
3772 struct clk *clk; in alloc_clk() local
3774 clk = kzalloc(sizeof(*clk), GFP_KERNEL); in alloc_clk()
3775 if (!clk) in alloc_clk()
3776 return ERR_PTR(-ENOMEM); in alloc_clk()
3778 clk->core = core; in alloc_clk()
3779 clk->dev_id = dev_id; in alloc_clk()
3780 clk->con_id = kstrdup_const(con_id, GFP_KERNEL); in alloc_clk()
3781 clk->max_rate = ULONG_MAX; in alloc_clk()
3783 return clk; in alloc_clk()
3787 * free_clk - Free a clk consumer
3788 * @clk: clk consumer to free
3790 * Note, this assumes the clk has been unlinked from the clk_core consumer
3793 static void free_clk(struct clk *clk) in free_clk() argument
3795 kfree_const(clk->con_id); in free_clk()
3796 kfree(clk); in free_clk()
3800 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3802 * @dev: clk consumer device
3803 * @hw: clk_hw associated with the clk being consumed
3807 * This is the main function used to create a clk pointer for use by clk
3809 * used by the framework and clk provider respectively.
3811 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw, in clk_hw_create_clk()
3814 struct clk *clk; in clk_hw_create_clk() local
3821 core = hw->core; in clk_hw_create_clk()
3822 clk = alloc_clk(core, dev_id, con_id); in clk_hw_create_clk()
3823 if (IS_ERR(clk)) in clk_hw_create_clk()
3824 return clk; in clk_hw_create_clk()
3825 clk->dev = dev; in clk_hw_create_clk()
3827 if (!try_module_get(core->owner)) { in clk_hw_create_clk()
3828 free_clk(clk); in clk_hw_create_clk()
3829 return ERR_PTR(-ENOENT); in clk_hw_create_clk()
3832 kref_get(&core->ref); in clk_hw_create_clk()
3833 clk_core_link_consumer(core, clk); in clk_hw_create_clk()
3835 return clk; in clk_hw_create_clk()
3844 return -EINVAL; in clk_cpy_name()
3850 return -ENOMEM; in clk_cpy_name()
3858 u8 num_parents = init->num_parents; in clk_core_populate_parent_map()
3859 const char * const *parent_names = init->parent_names; in clk_core_populate_parent_map()
3860 const struct clk_hw **parent_hws = init->parent_hws; in clk_core_populate_parent_map()
3861 const struct clk_parent_data *parent_data = init->parent_data; in clk_core_populate_parent_map()
3863 struct clk_parent_map *parents, *parent; in clk_core_populate_parent_map() local
3869 * Avoid unnecessary string look-ups of clk_core's possible parents by in clk_core_populate_parent_map()
3873 core->parents = parents; in clk_core_populate_parent_map()
3875 return -ENOMEM; in clk_core_populate_parent_map()
3878 for (i = 0, parent = parents; i < num_parents; i++, parent++) { in clk_core_populate_parent_map()
3879 parent->index = -1; in clk_core_populate_parent_map()
3884 __func__, core->name); in clk_core_populate_parent_map()
3885 ret = clk_cpy_name(&parent->name, parent_names[i], in clk_core_populate_parent_map()
3888 parent->hw = parent_data[i].hw; in clk_core_populate_parent_map()
3889 parent->index = parent_data[i].index; in clk_core_populate_parent_map()
3890 ret = clk_cpy_name(&parent->fw_name, in clk_core_populate_parent_map()
3893 ret = clk_cpy_name(&parent->name, in clk_core_populate_parent_map()
3897 parent->hw = parent_hws[i]; in clk_core_populate_parent_map()
3899 ret = -EINVAL; in clk_core_populate_parent_map()
3907 } while (--i >= 0); in clk_core_populate_parent_map()
3919 int i = core->num_parents; in clk_core_free_parent_map()
3921 if (!core->num_parents) in clk_core_free_parent_map()
3924 while (--i >= 0) { in clk_core_free_parent_map()
3925 kfree_const(core->parents[i].name); in clk_core_free_parent_map()
3926 kfree_const(core->parents[i].fw_name); in clk_core_free_parent_map()
3929 kfree(core->parents); in clk_core_free_parent_map()
3937 if (core->rpm_enabled) { in __clk_release()
3939 hlist_del(&core->rpm_node); in __clk_release()
3944 kfree_const(core->name); in __clk_release()
3948 static struct clk *
3953 const struct clk_init_data *init = hw->init; in __clk_register()
3958 * we catch use of hw->init early on in the core. in __clk_register()
3960 hw->init = NULL; in __clk_register()
3964 ret = -ENOMEM; in __clk_register()
3968 kref_init(&core->ref); in __clk_register()
3970 core->name = kstrdup_const(init->name, GFP_KERNEL); in __clk_register()
3971 if (!core->name) { in __clk_register()
3972 ret = -ENOMEM; in __clk_register()
3976 if (WARN_ON(!init->ops)) { in __clk_register()
3977 ret = -EINVAL; in __clk_register()
3980 core->ops = init->ops; in __clk_register()
3982 core->dev = dev; in __clk_register()
3984 core->of_node = np; in __clk_register()
3985 if (dev && dev->driver) in __clk_register()
3986 core->owner = dev->driver->owner; in __clk_register()
3987 core->hw = hw; in __clk_register()
3988 core->flags = init->flags; in __clk_register()
3989 core->num_parents = init->num_parents; in __clk_register()
3990 core->min_rate = 0; in __clk_register()
3991 core->max_rate = ULONG_MAX; in __clk_register()
3997 INIT_HLIST_HEAD(&core->clks); in __clk_register()
4003 hw->clk = alloc_clk(core, NULL, NULL); in __clk_register()
4004 if (IS_ERR(hw->clk)) { in __clk_register()
4005 ret = PTR_ERR(hw->clk); in __clk_register()
4009 clk_core_link_consumer(core, hw->clk); in __clk_register()
4013 return hw->clk; in __clk_register()
4016 clk_core_unlink_consumer(hw->clk); in __clk_register()
4019 free_clk(hw->clk); in __clk_register()
4020 hw->clk = NULL; in __clk_register()
4026 kref_put(&core->ref, __clk_release); in __clk_register()
4032 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
4036 * @dev->parent if dev doesn't have a device node, or NULL if neither
4037 * @dev or @dev->parent have a device node.
4048 np = dev_of_node(dev->parent); in dev_or_parent_of_node()
4054 * clk_register - allocate a new clock, register it and return an opaque cookie
4056 * @hw: link to hardware-specific clock data
4061 * Returns: a pointer to the newly allocated struct clk which
4066 struct clk *clk_register(struct device *dev, struct clk_hw *hw) in clk_register()
4073 * clk_hw_register - register a clk_hw and return an error code
4075 * @hw: link to hardware-specific clock data
4090 * of_clk_hw_register - register a clk_hw and return an error code
4092 * @hw: link to hardware-specific clock data
4109 * consumer calls clk_put() and the struct clk object is freed.
4113 return -ENXIO; in clk_nodrv_prepare_enable()
4124 return -ENXIO; in clk_nodrv_set_rate()
4129 return -ENXIO; in clk_nodrv_set_parent()
4147 for (i = 0; i < root->num_parents; i++) in clk_core_evict_parent_cache_subtree()
4148 if (root->parents[i].core == target) in clk_core_evict_parent_cache_subtree()
4149 root->parents[i].core = NULL; in clk_core_evict_parent_cache_subtree()
4151 hlist_for_each_entry(child, &root->children, child_node) in clk_core_evict_parent_cache_subtree()
4155 /* Remove this clk from all parent caches */
4170 * clk_unregister - unregister a currently registered clock
4171 * @clk: clock to unregister
4173 void clk_unregister(struct clk *clk) in clk_unregister() argument
4178 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in clk_unregister()
4181 clk_debug_unregister(clk->core); in clk_unregister()
4185 ops = clk->core->ops; in clk_unregister()
4188 clk->core->name); in clk_unregister()
4196 clk->core->ops = &clk_nodrv_ops; in clk_unregister()
4199 if (ops->terminate) in clk_unregister()
4200 ops->terminate(clk->core->hw); in clk_unregister()
4202 if (!hlist_empty(&clk->core->children)) { in clk_unregister()
4207 hlist_for_each_entry_safe(child, t, &clk->core->children, in clk_unregister()
4212 clk_core_evict_parent_cache(clk->core); in clk_unregister()
4214 hlist_del_init(&clk->core->child_node); in clk_unregister()
4216 if (clk->core->prepare_count) in clk_unregister()
4218 __func__, clk->core->name); in clk_unregister()
4220 if (clk->core->protect_count) in clk_unregister()
4222 __func__, clk->core->name); in clk_unregister()
4224 kref_put(&clk->core->ref, __clk_release); in clk_unregister()
4225 free_clk(clk); in clk_unregister()
4232 * clk_hw_unregister - unregister a currently registered clk_hw
4233 * @hw: hardware-specific clock data to unregister
4237 clk_unregister(hw->clk); in clk_hw_unregister()
4243 clk_unregister(*(struct clk **)res); in devm_clk_release()
4252 * devm_clk_register - resource managed clk_register()
4254 * @hw: link to hardware-specific clock data
4261 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) in devm_clk_register()
4263 struct clk *clk; in devm_clk_register() local
4264 struct clk **clkp; in devm_clk_register()
4268 return ERR_PTR(-ENOMEM); in devm_clk_register()
4270 clk = clk_register(dev, hw); in devm_clk_register()
4271 if (!IS_ERR(clk)) { in devm_clk_register()
4272 *clkp = clk; in devm_clk_register()
4278 return clk; in devm_clk_register()
4283 * devm_clk_hw_register - resource managed clk_hw_register()
4285 * @hw: link to hardware-specific clock data
4298 return -ENOMEM; in devm_clk_hw_register()
4314 struct clk *c = res; in devm_clk_match()
4330 * devm_clk_unregister - resource managed clk_unregister()
4332 * @clk: clock to unregister
4338 void devm_clk_unregister(struct device *dev, struct clk *clk) in devm_clk_unregister() argument
4340 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk)); in devm_clk_unregister()
4345 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
4346 * @dev: device that is unregistering the hardware-specific clock data
4347 * @hw: link to hardware-specific clock data
4364 void __clk_put(struct clk *clk) in __clk_put() argument
4368 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in __clk_put()
4378 if (WARN_ON(clk->exclusive_count)) { in __clk_put()
4380 clk->core->protect_count -= (clk->exclusive_count - 1); in __clk_put()
4381 clk_core_rate_unprotect(clk->core); in __clk_put()
4382 clk->exclusive_count = 0; in __clk_put()
4385 hlist_del(&clk->clks_node); in __clk_put()
4386 if (clk->min_rate > clk->core->req_rate || in __clk_put()
4387 clk->max_rate < clk->core->req_rate) in __clk_put()
4388 clk_core_set_rate_nolock(clk->core, clk->core->req_rate); in __clk_put()
4390 owner = clk->core->owner; in __clk_put()
4391 kref_put(&clk->core->ref, __clk_release); in __clk_put()
4397 free_clk(clk); in __clk_put()
4400 /*** clk rate change notifiers ***/
4403 * clk_notifier_register - add a clk rate change notifier
4404 * @clk: struct clk * to watch
4407 * Request notification when clk's rate changes. This uses an SRCU
4410 * re-enter into the clk framework by calling any top-level clk APIs;
4417 * clk_notifier_register() must be called from non-atomic context.
4418 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4422 int clk_notifier_register(struct clk *clk, struct notifier_block *nb) in clk_notifier_register() argument
4425 int ret = -ENOMEM; in clk_notifier_register()
4427 if (!clk || !nb) in clk_notifier_register()
4428 return -EINVAL; in clk_notifier_register()
4432 /* search the list of notifiers for this clk */ in clk_notifier_register()
4434 if (cn->clk == clk) in clk_notifier_register()
4437 /* if clk wasn't in the notifier list, allocate new clk_notifier */ in clk_notifier_register()
4442 cn->clk = clk; in clk_notifier_register()
4443 srcu_init_notifier_head(&cn->notifier_head); in clk_notifier_register()
4445 list_add(&cn->node, &clk_notifier_list); in clk_notifier_register()
4448 ret = srcu_notifier_chain_register(&cn->notifier_head, nb); in clk_notifier_register()
4450 clk->core->notifier_count++; in clk_notifier_register()
4460 * clk_notifier_unregister - remove a clk rate change notifier
4461 * @clk: struct clk *
4464 * Request no further notification for changes to 'clk' and frees memory
4467 * Returns -EINVAL if called with null arguments; otherwise, passes
4470 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) in clk_notifier_unregister() argument
4473 int ret = -ENOENT; in clk_notifier_unregister()
4475 if (!clk || !nb) in clk_notifier_unregister()
4476 return -EINVAL; in clk_notifier_unregister()
4481 if (cn->clk == clk) { in clk_notifier_unregister()
4482 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); in clk_notifier_unregister()
4484 clk->core->notifier_count--; in clk_notifier_unregister()
4487 if (!cn->notifier_head.head) { in clk_notifier_unregister()
4488 srcu_cleanup_notifier_head(&cn->notifier_head); in clk_notifier_unregister()
4489 list_del(&cn->node); in clk_notifier_unregister()
4511 * struct of_clk_provider - Clock provider registration structure
4514 * @get: Get clock callback. Returns NULL or a struct clk for the
4524 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4536 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, in of_clk_src_simple_get()
4549 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) in of_clk_src_onecell_get()
4552 unsigned int idx = clkspec->args[0]; in of_clk_src_onecell_get()
4554 if (idx >= clk_data->clk_num) { in of_clk_src_onecell_get()
4556 return ERR_PTR(-EINVAL); in of_clk_src_onecell_get()
4559 return clk_data->clks[idx]; in of_clk_src_onecell_get()
4567 unsigned int idx = clkspec->args[0]; in of_clk_hw_onecell_get()
4569 if (idx >= hw_data->num) { in of_clk_hw_onecell_get()
4571 return ERR_PTR(-EINVAL); in of_clk_hw_onecell_get()
4574 return hw_data->hws[idx]; in of_clk_hw_onecell_get()
4579 * of_clk_add_provider() - Register a clock provider for a node
4587 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, in of_clk_add_provider()
4596 return -ENOMEM; in of_clk_add_provider()
4598 cp->node = of_node_get(np); in of_clk_add_provider()
4599 cp->data = data; in of_clk_add_provider()
4600 cp->get = clk_src_get; in of_clk_add_provider()
4603 list_add(&cp->link, &of_clk_providers); in of_clk_add_provider()
4618 * of_clk_add_hw_provider() - Register a clock provider for a node
4633 return -ENOMEM; in of_clk_add_hw_provider()
4635 cp->node = of_node_get(np); in of_clk_add_hw_provider()
4636 cp->data = data; in of_clk_add_hw_provider()
4637 cp->get_hw = get; in of_clk_add_hw_provider()
4640 list_add(&cp->link, &of_clk_providers); in of_clk_add_hw_provider()
4660 * We allow a child device to use its parent device as the clock provider node
4661 * for cases like MFD sub-devices where the child device driver wants to use
4662 * devm_*() APIs but not list the device in DT as a sub-node.
4668 np = dev->of_node; in get_clk_provider_node()
4669 parent_np = dev->parent ? dev->parent->of_node : NULL; in get_clk_provider_node()
4671 if (!of_find_property(np, "#clock-cells", NULL)) in get_clk_provider_node()
4672 if (of_find_property(parent_np, "#clock-cells", NULL)) in get_clk_provider_node()
4679 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4685 * node or if the device node lacks of clock provider information (#clock-cells)
4686 * then the parent device's node is scanned for this information. If parent node
4687 * has the #clock-cells then it is used in registration. Provider is
4703 return -ENOMEM; in devm_of_clk_add_hw_provider()
4719 * of_clk_del_provider() - Remove a previously registered clock provider
4728 if (cp->node == np) { in of_clk_del_provider()
4729 list_del(&cp->link); in of_clk_del_provider()
4730 of_node_put(cp->node); in of_clk_del_provider()
4750 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4766 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4772 * Parses a device node's "clocks" and "clock-names" properties to find the
4775 * parsing error. The @index argument is ignored if @name is non-NULL.
4779 * phandle1: clock-controller@1 {
4780 * #clock-cells = <2>;
4783 * phandle2: clock-controller@2 {
4784 * #clock-cells = <1>;
4787 * clock-consumer@3 {
4789 * clock-names = "name1", "name2";
4792 * To get a device_node for `clock-controller@2' node you may call this
4795 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4796 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4797 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4799 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4800 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4801 * the "clock-names" property of @np.
4806 int ret = -ENOENT; in of_parse_clkspec()
4812 * "clock-names" property. If it cannot be found, then index in of_parse_clkspec()
4814 * return -EINVAL. in of_parse_clkspec()
4817 index = of_property_match_string(np, "clock-names", name); in of_parse_clkspec()
4818 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells", in of_parse_clkspec()
4826 * No matching clock found on this node. If the parent node in of_parse_clkspec()
4827 * has a "clock-ranges" property, then we can try one of its in of_parse_clkspec()
4830 np = np->parent; in of_parse_clkspec()
4831 if (np && !of_get_property(np, "clock-ranges", NULL)) in of_parse_clkspec()
4843 struct clk *clk; in __of_clk_get_hw_from_provider() local
4845 if (provider->get_hw) in __of_clk_get_hw_from_provider()
4846 return provider->get_hw(clkspec, provider->data); in __of_clk_get_hw_from_provider()
4848 clk = provider->get(clkspec, provider->data); in __of_clk_get_hw_from_provider()
4849 if (IS_ERR(clk)) in __of_clk_get_hw_from_provider()
4850 return ERR_CAST(clk); in __of_clk_get_hw_from_provider()
4851 return __clk_get_hw(clk); in __of_clk_get_hw_from_provider()
4858 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER); in of_clk_get_hw_from_clkspec()
4861 return ERR_PTR(-EINVAL); in of_clk_get_hw_from_clkspec()
4865 if (provider->node == clkspec->np) { in of_clk_get_hw_from_clkspec()
4877 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4880 * This function looks up a struct clk from the registered list of clock
4884 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) in of_clk_get_from_provider()
4909 static struct clk *__of_clk_get(struct device_node *np, in __of_clk_get()
4918 struct clk *of_clk_get(struct device_node *np, int index) in of_clk_get()
4920 return __of_clk_get(np, index, np->full_name, NULL); in of_clk_get()
4925 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4929 * This function parses the clocks and clock-names properties,
4930 * and uses them to look up the struct clk from the registered list of clock
4933 struct clk *of_clk_get_by_name(struct device_node *np, const char *name) in of_clk_get_by_name()
4936 return ERR_PTR(-ENOENT); in of_clk_get_by_name()
4938 return __of_clk_get(np, 0, np->full_name, name); in of_clk_get_by_name()
4943 * of_clk_get_parent_count() - Count the number of clocks a device node has
4952 count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); in of_clk_get_parent_count()
4969 struct clk *clk; in of_clk_get_parent_name() local
4971 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, in of_clk_get_parent_name()
4980 * specified into an array offset for the clock-output-names property. in of_clk_get_parent_name()
4982 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { in of_clk_get_parent_name()
4989 /* We went off the end of 'clock-indices' without finding it */ in of_clk_get_parent_name()
4993 if (of_property_read_string_index(clkspec.np, "clock-output-names", in of_clk_get_parent_name()
5000 * the clock as long as #clock-cells = 0. in of_clk_get_parent_name()
5002 clk = of_clk_get_from_provider(&clkspec); in of_clk_get_parent_name()
5003 if (IS_ERR(clk)) { in of_clk_get_parent_name()
5005 clk_name = clkspec.np->name; in of_clk_get_parent_name()
5009 clk_name = __clk_get_name(clk); in of_clk_get_parent_name()
5010 clk_put(clk); in of_clk_get_parent_name()
5021 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
5048 * This function looks for a parent clock. If there is one, then it
5049 * checks that the provider for this parent clock was initialized, in
5050 * this case the parent clock will be ready.
5057 struct clk *clk = of_clk_get(np, i); in parent_ready() local
5059 /* this parent is ready we can check the next one */ in parent_ready()
5060 if (!IS_ERR(clk)) { in parent_ready()
5061 clk_put(clk); in parent_ready()
5066 /* at least one parent is not ready, we exit now */ in parent_ready()
5067 if (PTR_ERR(clk) == -EPROBE_DEFER) in parent_ready()
5073 * no more parent. As we didn't exit yet, then the in parent_ready()
5074 * previous parent are ready. If there is no clock in parent_ready()
5075 * parent, no need to wait for them, then we can in parent_ready()
5083 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
5086 * @flags: pointer to top-level framework flags
5088 * Detects if the clock-critical property exists and, if so, sets the
5092 * bindings, such as the one-clock-per-node style that are outdated.
5108 return -EINVAL; in of_clk_detect_critical()
5110 of_property_for_each_u32(np, "clock-critical", prop, cur, idx) in of_clk_detect_critical()
5118 * of_clk_init() - Scan and init clock providers from the DT
5139 struct clock_provider *parent; in of_clk_init() local
5144 parent = kzalloc(sizeof(*parent), GFP_KERNEL); in of_clk_init()
5145 if (!parent) { in of_clk_init()
5148 list_del(&clk_provider->node); in of_clk_init()
5149 of_node_put(clk_provider->np); in of_clk_init()
5156 parent->clk_init_cb = match->data; in of_clk_init()
5157 parent->np = of_node_get(np); in of_clk_init()
5158 list_add_tail(&parent->node, &clk_provider_list); in of_clk_init()
5165 if (force || parent_ready(clk_provider->np)) { in of_clk_init()
5168 of_node_set_flag(clk_provider->np, in of_clk_init()
5171 clk_provider->clk_init_cb(clk_provider->np); in of_clk_init()
5172 of_clk_set_defaults(clk_provider->np, true); in of_clk_init()
5174 list_del(&clk_provider->node); in of_clk_init()
5175 of_node_put(clk_provider->np); in of_clk_init()
5185 * in case the clock parent was not mandatory in of_clk_init()