Lines Matching +full:rate +full:- +full:a

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/clk-conf.h>
12 #include <linux/clk-provider.h>
78 unsigned long rate; member
123 if (!core->rpm_enabled) in clk_pm_runtime_get()
126 return pm_runtime_resume_and_get(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
141 * that disabling unused clks avoids a deadlock where a device is runtime PM
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()
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()
365 return !clk ? NULL : clk->core->name; in __clk_get_name()
371 return hw->core->name; in clk_hw_get_name()
377 return hw->core->dev; in clk_hw_get_dev()
383 return hw->core->of_node; in clk_hw_get_of_node()
389 return !clk ? NULL : clk->core->hw; in __clk_get_hw()
395 return hw->core->num_parents; in clk_hw_get_num_parents()
401 return hw->core->parent ? hw->core->parent->hw : NULL; in clk_hw_get_parent()
417 if (!strcmp(core->name, name)) in clk_core_lookup()
433 return -ENOENT; in of_parse_clkspec()
438 return ERR_PTR(-ENOENT); in of_clk_get_hw_from_clkspec()
443 * clk_core_get - Find the clk_core parent of a clk
447 * This is the preferred method for clk providers to find the parent of a
449 * array is indexed and treated as a local name matching a string in the device
450 * node's 'clock-names' property or as the 'con_id' matching the device's
451 * dev_name() in a clk_lookup. This allows clk providers to use their own
452 * namespace instead of looking for a globally unique parent string.
454 * For example the following DT snippet would allow a clock registered by the
455 * clock-controller@c001 that has a clk_init_data::parent_data array
457 * clock-controller@f00abcd without needing to get the globally unique name of
460 * parent: clock-controller@f00abcd {
462 * #clock-cells = <0>;
465 * clock-controller@c001 {
468 * clock-names = "xtal";
469 * #clock-cells = <1>;
472 * Returns: -ENOENT when the provider can't be found or the clk doesn't
474 * in a clkdev lookup. NULL when the provider knows about the clk but it
476 * A valid clk_core pointer when the clk can be found in the provider.
480 const char *name = core->parents[p_index].fw_name; in clk_core_get()
481 int index = core->parents[p_index].index; in clk_core_get()
482 struct clk_hw *hw = ERR_PTR(-ENOENT); in clk_core_get()
483 struct device *dev = core->dev; in clk_core_get()
485 struct device_node *np = core->of_node; in clk_core_get()
506 return hw->core; in clk_core_get()
511 struct clk_parent_map *entry = &core->parents[index]; in clk_core_fill_parent_index()
514 if (entry->hw) { in clk_core_fill_parent_index()
515 parent = entry->hw->core; in clk_core_fill_parent_index()
518 if (PTR_ERR(parent) == -ENOENT && entry->name) in clk_core_fill_parent_index()
519 parent = clk_core_lookup(entry->name); in clk_core_fill_parent_index()
523 * We have a direct reference but it isn't registered yet? in clk_core_fill_parent_index()
528 parent = ERR_PTR(-EPROBE_DEFER); in clk_core_fill_parent_index()
532 entry->core = parent; in clk_core_fill_parent_index()
538 if (!core || index >= core->num_parents || !core->parents) in clk_core_get_parent_by_index()
541 if (!core->parents[index].core) in clk_core_get_parent_by_index()
544 return core->parents[index].core; in clk_core_get_parent_by_index()
552 parent = clk_core_get_parent_by_index(hw->core, index); in clk_hw_get_parent_by_index()
554 return !parent ? NULL : parent->hw; in clk_hw_get_parent_by_index()
560 return !clk ? 0 : clk->core->enable_count; in __clk_get_enable_count()
568 if (!core->num_parents || core->parent) in clk_core_get_rate_nolock()
569 return core->rate; in clk_core_get_rate_nolock()
572 * Clk must have a parent because num_parents > 0 but the parent isn't in clk_core_get_rate_nolock()
573 * known yet. Best to return 0 as the rate of this clk until we can in clk_core_get_rate_nolock()
574 * properly recalc the rate based on the parent's rate. in clk_core_get_rate_nolock()
581 return clk_core_get_rate_nolock(hw->core); in clk_hw_get_rate()
590 return core->accuracy; in clk_core_get_accuracy_no_lock()
595 return hw->core->flags; in clk_hw_get_flags()
601 return clk_core_is_prepared(hw->core); in clk_hw_is_prepared()
607 return clk_core_is_enabled(hw->core); in clk_hw_is_enabled()
616 return clk_core_is_enabled(clk->core); in __clk_is_enabled()
620 static bool mux_is_better_rate(unsigned long rate, unsigned long now, in mux_is_better_rate() argument
624 return abs(now - rate) < abs(best - rate); in mux_is_better_rate()
626 return now <= rate && now > best; in mux_is_better_rate()
631 unsigned long rate);
642 if (core->parent == parent) in clk_core_has_parent()
645 for (i = 0; i < core->num_parents; i++) { in clk_core_has_parent()
669 if (req->min_rate < old_req->min_rate) in clk_core_forward_rate_req()
670 req->min_rate = old_req->min_rate; in clk_core_forward_rate_req()
672 if (req->max_rate > old_req->max_rate) in clk_core_forward_rate_req()
673 req->max_rate = old_req->max_rate; in clk_core_forward_rate_req()
680 struct clk_core *core = hw->core; in clk_core_determine_rate_no_reparent()
681 struct clk_core *parent = core->parent; in clk_core_determine_rate_no_reparent()
685 if (core->flags & CLK_SET_RATE_PARENT) { in clk_core_determine_rate_no_reparent()
689 req->rate = 0; in clk_core_determine_rate_no_reparent()
694 req->rate); in clk_core_determine_rate_no_reparent()
704 best = parent_req.rate; in clk_core_determine_rate_no_reparent()
711 req->best_parent_rate = best; in clk_core_determine_rate_no_reparent()
712 req->rate = best; in clk_core_determine_rate_no_reparent()
721 struct clk_core *core = hw->core, *parent, *best_parent = NULL; in clk_mux_determine_rate_flags()
726 if (core->flags & CLK_SET_RATE_NO_REPARENT) in clk_mux_determine_rate_flags()
729 /* find the parent that can provide the fastest rate <= rate */ in clk_mux_determine_rate_flags()
730 num_parents = core->num_parents; in clk_mux_determine_rate_flags()
738 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
741 clk_core_forward_rate_req(core, req, parent, &parent_req, req->rate); in clk_mux_determine_rate_flags()
751 parent_rate = parent_req.rate; in clk_mux_determine_rate_flags()
756 if (mux_is_better_rate(req->rate, parent_rate, in clk_mux_determine_rate_flags()
764 return -EINVAL; in clk_mux_determine_rate_flags()
766 req->best_parent_hw = best_parent->hw; in clk_mux_determine_rate_flags()
767 req->best_parent_rate = best; in clk_mux_determine_rate_flags()
768 req->rate = best; in clk_mux_determine_rate_flags()
778 return !core ? NULL : core->hw->clk; in __clk_lookup()
789 *min_rate = core->min_rate; in clk_core_get_boundaries()
790 *max_rate = core->max_rate; in clk_core_get_boundaries()
792 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
793 *min_rate = max(*min_rate, clk_user->min_rate); in clk_core_get_boundaries()
795 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
796 *max_rate = min(*max_rate, clk_user->max_rate); in clk_core_get_boundaries()
800 * clk_hw_get_rate_range() - returns the clock rate range for a hw clk
811 clk_core_get_boundaries(hw->core, min_rate, max_rate); in clk_hw_get_rate_range()
823 if (min_rate > core->max_rate || max_rate < core->min_rate) in clk_core_check_boundaries()
826 hlist_for_each_entry(user, &core->clks, clks_node) in clk_core_check_boundaries()
827 if (min_rate > user->max_rate || max_rate < user->min_rate) in clk_core_check_boundaries()
836 hw->core->min_rate = min_rate; in clk_hw_set_rate_range()
837 hw->core->max_rate = max_rate; in clk_hw_set_rate_range()
842 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
843 * @hw: mux type clk to determine rate on
844 * @req: rate request, also used to return preferred parent and frequencies
846 * Helper for finding best parent to provide a given frequency. This can be used
847 * directly as a determine_rate callback (e.g. for a mux), or from a more
848 * complex clock that may combine a mux with other operations.
850 * Returns: 0 on success, -EERROR value on error
867 …* clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn'…
868 * @hw: mux type clk to determine rate on
869 * @req: rate request, also used to return preferred frequency
871 * Helper for finding best parent rate to provide a given frequency.
872 * This can be used directly as a determine_rate callback (e.g. for a
873 * mux), or from a more complex clock that may combine a mux with other
876 * Returns: 0 on success, -EERROR value on error
894 if (WARN(core->protect_count == 0, in clk_core_rate_unprotect()
895 "%s already unprotected\n", core->name)) in clk_core_rate_unprotect()
898 if (--core->protect_count > 0) in clk_core_rate_unprotect()
901 clk_core_rate_unprotect(core->parent); in clk_core_rate_unprotect()
911 return -EINVAL; in clk_core_rate_nuke_protect()
913 if (core->protect_count == 0) in clk_core_rate_nuke_protect()
916 ret = core->protect_count; in clk_core_rate_nuke_protect()
917 core->protect_count = 1; in clk_core_rate_nuke_protect()
924 * clk_rate_exclusive_put - release exclusivity over clock rate control
927 * clk_rate_exclusive_put() completes a critical section during which a clock
929 * clock which could result in a rate change or rate glitch. Exclusive clocks
930 * cannot have their rate changed, either directly or indirectly due to changes
931 * further up the parent chain of clocks. As a result, clocks up parent chain
935 * the rate effectively gets locked as exclusivity can't be preempted.
952 if (WARN_ON(clk->exclusive_count <= 0)) in clk_rate_exclusive_put()
955 clk_core_rate_unprotect(clk->core); in clk_rate_exclusive_put()
956 clk->exclusive_count--; in clk_rate_exclusive_put()
969 if (core->protect_count == 0) in clk_core_rate_protect()
970 clk_core_rate_protect(core->parent); in clk_core_rate_protect()
972 core->protect_count++; in clk_core_rate_protect()
986 core->protect_count = count; in clk_core_rate_restore_protect()
990 * clk_rate_exclusive_get - get exclusivity over the clk rate control
991 * @clk: the clk over which the exclusity of rate control is requested
993 * clk_rate_exclusive_get() begins a critical section during which a clock
995 * clock which could result in a rate change or rate glitch. Exclusive clocks
996 * cannot have their rate changed, either directly or indirectly due to changes
997 * further up the parent chain of clocks. As a result, clocks up parent chain
1001 * the rate effectively gets locked as exclusivity can't be preempted.
1005 * Returns 0 on success, -EERROR otherwise
1013 clk_core_rate_protect(clk->core); in clk_rate_exclusive_get()
1014 clk->exclusive_count++; in clk_rate_exclusive_get()
1047 if (WARN(core->prepare_count == 0, in clk_core_unprepare()
1048 "%s already unprepared\n", core->name)) in clk_core_unprepare()
1051 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_unprepare()
1052 "Unpreparing critical %s\n", core->name)) in clk_core_unprepare()
1055 if (core->flags & CLK_SET_RATE_GATE) in clk_core_unprepare()
1058 if (--core->prepare_count > 0) in clk_core_unprepare()
1061 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name); in clk_core_unprepare()
1065 if (core->ops->unprepare) in clk_core_unprepare()
1066 core->ops->unprepare(core->hw); in clk_core_unprepare()
1069 clk_core_unprepare(core->parent); in clk_core_unprepare()
1081 * clk_unprepare - undo preparation of a clock source
1084 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
1085 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
1086 * if the operation may sleep. One example is a clk which is accessed over
1087 * I2c. In the complex case a clk gate operation may require a fast and a slow
1096 clk_core_unprepare_lock(clk->core); in clk_unprepare()
1109 if (core->prepare_count == 0) { in clk_core_prepare()
1114 ret = clk_core_prepare(core->parent); in clk_core_prepare()
1120 if (core->ops->prepare) in clk_core_prepare()
1121 ret = core->ops->prepare(core->hw); in clk_core_prepare()
1129 core->prepare_count++; in clk_core_prepare()
1132 * CLK_SET_RATE_GATE is a special case of clock protection in clk_core_prepare()
1133 * Instead of a consumer claiming exclusive rate control, it is in clk_core_prepare()
1135 * operation which could result in a rate change or rate glitch while in clk_core_prepare()
1138 if (core->flags & CLK_SET_RATE_GATE) in clk_core_prepare()
1143 clk_core_unprepare(core->parent); in clk_core_prepare()
1161 * clk_prepare - prepare a clock source
1164 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
1165 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
1166 * operation may sleep. One example is a clk which is accessed over I2c. In
1167 * the complex case a clk ungate operation may require a fast and a slow part.
1170 * Returns 0 on success, -EERROR otherwise.
1177 return clk_core_prepare_lock(clk->core); in clk_prepare()
1188 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name)) in clk_core_disable()
1191 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_disable()
1192 "Disabling critical %s\n", core->name)) in clk_core_disable()
1195 if (--core->enable_count > 0) in clk_core_disable()
1200 if (core->ops->disable) in clk_core_disable()
1201 core->ops->disable(core->hw); in clk_core_disable()
1205 clk_core_disable(core->parent); in clk_core_disable()
1218 * clk_disable - gate a clock
1222 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1223 * clk if the operation is fast and will never sleep. One example is a
1224 * SoC-internal clk which is controlled via simple register writes. In the
1225 * complex case a clk gate operation may require a fast and a slow part. It is
1234 clk_core_disable_lock(clk->core); in clk_disable()
1247 if (WARN(core->prepare_count == 0, in clk_core_enable()
1248 "Enabling unprepared %s\n", core->name)) in clk_core_enable()
1249 return -ESHUTDOWN; in clk_core_enable()
1251 if (core->enable_count == 0) { in clk_core_enable()
1252 ret = clk_core_enable(core->parent); in clk_core_enable()
1259 if (core->ops->enable) in clk_core_enable()
1260 ret = core->ops->enable(core->hw); in clk_core_enable()
1265 clk_core_disable(core->parent); in clk_core_enable()
1270 core->enable_count++; in clk_core_enable()
1287 * clk_gate_restore_context - restore context for poweroff
1298 struct clk_core *core = hw->core; in clk_gate_restore_context()
1300 if (core->enable_count) in clk_gate_restore_context()
1301 core->ops->enable(hw); in clk_gate_restore_context()
1303 core->ops->disable(hw); in clk_gate_restore_context()
1312 hlist_for_each_entry(child, &core->children, child_node) { in clk_core_save_context()
1318 if (core->ops && core->ops->save_context) in clk_core_save_context()
1319 ret = core->ops->save_context(core->hw); in clk_core_save_context()
1328 if (core->ops && core->ops->restore_context) in clk_core_restore_context()
1329 core->ops->restore_context(core->hw); in clk_core_restore_context()
1331 hlist_for_each_entry(child, &core->children, child_node) in clk_core_restore_context()
1336 * clk_save_context - save clock context for poweroff
1364 * clk_restore_context - restore clock context after poweroff
1382 * clk_enable - ungate a clock
1385 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1386 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1387 * if the operation will never sleep. One example is a SoC-internal clk which
1388 * is controlled via simple register writes. In the complex case a clk ungate
1389 * operation may require a fast and a slow part. It is this reason that
1391 * must be called before clk_enable. Returns 0 on success, -EERROR
1399 return clk_core_enable_lock(clk->core); in clk_enable()
1404 * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it.
1408 * making clk_enable()/clk_disable() no-ops, false otherwise.
1420 return clk && !(clk->core->ops->enable && clk->core->ops->disable); in clk_is_enabled_when_prepared()
1451 hlist_for_each_entry(child, &core->children, child_node) in clk_unprepare_unused_subtree()
1454 if (core->prepare_count) in clk_unprepare_unused_subtree()
1457 if (core->flags & CLK_IGNORE_UNUSED) in clk_unprepare_unused_subtree()
1462 if (core->ops->unprepare_unused) in clk_unprepare_unused_subtree()
1463 core->ops->unprepare_unused(core->hw); in clk_unprepare_unused_subtree()
1464 else if (core->ops->unprepare) in clk_unprepare_unused_subtree()
1465 core->ops->unprepare(core->hw); in clk_unprepare_unused_subtree()
1477 hlist_for_each_entry(child, &core->children, child_node) in clk_disable_unused_subtree()
1480 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1481 clk_core_prepare_enable(core->parent); in clk_disable_unused_subtree()
1485 if (core->enable_count) in clk_disable_unused_subtree()
1488 if (core->flags & CLK_IGNORE_UNUSED) in clk_disable_unused_subtree()
1492 * some gate clocks have special needs during the disable-unused in clk_disable_unused_subtree()
1498 if (core->ops->disable_unused) in clk_disable_unused_subtree()
1499 core->ops->disable_unused(core->hw); in clk_disable_unused_subtree()
1500 else if (core->ops->disable) in clk_disable_unused_subtree()
1501 core->ops->disable(core->hw); in clk_disable_unused_subtree()
1507 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1508 clk_core_disable_unprepare(core->parent); in clk_disable_unused_subtree()
1563 long rate; in clk_core_determine_round_nolock() local
1571 * Some clock providers hand-craft their clk_rate_requests and in clk_core_determine_round_nolock()
1574 * If it's the case, clamping the rate is equivalent to setting in clk_core_determine_round_nolock()
1575 * the rate to 0 which is bad. Skip the clamping but complain so in clk_core_determine_round_nolock()
1578 if (!req->min_rate && !req->max_rate) in clk_core_determine_round_nolock()
1579 pr_warn("%s: %s: clk_rate_request has initialized min or max rate.\n", in clk_core_determine_round_nolock()
1580 __func__, core->name); in clk_core_determine_round_nolock()
1582 req->rate = clamp(req->rate, req->min_rate, req->max_rate); in clk_core_determine_round_nolock()
1586 * - if the provider is not protected at all in clk_core_determine_round_nolock()
1587 * - if the calling consumer is the only one which has exclusivity in clk_core_determine_round_nolock()
1591 req->rate = core->rate; in clk_core_determine_round_nolock()
1592 } else if (core->ops->determine_rate) { in clk_core_determine_round_nolock()
1593 return core->ops->determine_rate(core->hw, req); in clk_core_determine_round_nolock()
1594 } else if (core->ops->round_rate) { in clk_core_determine_round_nolock()
1595 rate = core->ops->round_rate(core->hw, req->rate, in clk_core_determine_round_nolock()
1596 &req->best_parent_rate); in clk_core_determine_round_nolock()
1597 if (rate < 0) in clk_core_determine_round_nolock()
1598 return rate; in clk_core_determine_round_nolock()
1600 req->rate = rate; in clk_core_determine_round_nolock()
1602 return -EINVAL; in clk_core_determine_round_nolock()
1610 unsigned long rate) in clk_core_init_rate_req() argument
1618 req->max_rate = ULONG_MAX; in clk_core_init_rate_req()
1623 req->core = core; in clk_core_init_rate_req()
1624 req->rate = rate; in clk_core_init_rate_req()
1625 clk_core_get_boundaries(core, &req->min_rate, &req->max_rate); in clk_core_init_rate_req()
1627 parent = core->parent; in clk_core_init_rate_req()
1629 req->best_parent_hw = parent->hw; in clk_core_init_rate_req()
1630 req->best_parent_rate = parent->rate; in clk_core_init_rate_req()
1632 req->best_parent_hw = NULL; in clk_core_init_rate_req()
1633 req->best_parent_rate = 0; in clk_core_init_rate_req()
1638 * clk_hw_init_rate_request - Initializes a clk_rate_request
1639 * @hw: the clk for which we want to submit a rate request
1641 * @rate: the rate which is to be requested
1643 * Initializes a clk_rate_request structure to submit to
1648 unsigned long rate) in clk_hw_init_rate_request() argument
1653 clk_core_init_rate_req(hw->core, req, rate); in clk_hw_init_rate_request()
1658 * clk_hw_forward_rate_request - Forwards a clk_rate_request to a clock's parent
1659 * @hw: the original clock that got the rate request
1663 * @parent_rate: The rate which is to be requested to @parent
1665 * Initializes a clk_rate_request structure to submit to a clock parent
1677 clk_core_forward_rate_req(hw->core, old_req, in clk_hw_forward_rate_request()
1678 parent->core, req, in clk_hw_forward_rate_request()
1685 return core->ops->determine_rate || core->ops->round_rate; in clk_core_can_round()
1696 req->rate = 0; in clk_core_round_rate_nolock()
1703 if (core->flags & CLK_SET_RATE_PARENT) { in clk_core_round_rate_nolock()
1706 clk_core_forward_rate_req(core, req, core->parent, &parent_req, req->rate); in clk_core_round_rate_nolock()
1710 ret = clk_core_round_rate_nolock(core->parent, &parent_req); in clk_core_round_rate_nolock()
1716 req->best_parent_rate = parent_req.rate; in clk_core_round_rate_nolock()
1717 req->rate = parent_req.rate; in clk_core_round_rate_nolock()
1722 req->rate = core->rate; in clk_core_round_rate_nolock()
1727 * __clk_determine_rate - get the closest rate actually supported by a clock
1728 * @hw: determine the rate of this clock
1729 * @req: target rate request
1736 req->rate = 0; in __clk_determine_rate()
1740 return clk_core_round_rate_nolock(hw->core, req); in __clk_determine_rate()
1745 * clk_hw_round_rate() - round the given rate for a hw clk
1746 * @hw: the hw clk for which we are rounding a rate
1747 * @rate: the rate which is to be rounded
1749 * Takes in a rate as input and rounds it to a rate that the clk can actually
1756 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1757 * else returns the parent rate.
1759 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate) in clk_hw_round_rate() argument
1764 clk_core_init_rate_req(hw->core, &req, rate); in clk_hw_round_rate()
1768 ret = clk_core_round_rate_nolock(hw->core, &req); in clk_hw_round_rate()
1774 return req.rate; in clk_hw_round_rate()
1779 * clk_round_rate - round the given rate for a clk
1780 * @clk: the clk for which we are rounding a rate
1781 * @rate: the rate which is to be rounded
1783 * Takes in a rate as input and rounds it to a rate that the clk can actually
1785 * then the parent rate is returned.
1787 long clk_round_rate(struct clk *clk, unsigned long rate) in clk_round_rate() argument
1797 if (clk->exclusive_count) in clk_round_rate()
1798 clk_core_rate_unprotect(clk->core); in clk_round_rate()
1800 clk_core_init_rate_req(clk->core, &req, rate); in clk_round_rate()
1804 ret = clk_core_round_rate_nolock(clk->core, &req); in clk_round_rate()
1808 if (clk->exclusive_count) in clk_round_rate()
1809 clk_core_rate_protect(clk->core); in clk_round_rate()
1816 return req.rate; in clk_round_rate()
1821 * __clk_notify - call clk notifier chain
1822 * @core: clk that is changing rate
1824 * @old_rate: old clk rate
1825 * @new_rate: new clk rate
1827 * Triggers a notifier call chain on the clk rate-change notification
1828 * for 'clk'. Passes a pointer to the struct clk and the previous
1832 * a driver returns that.
1845 if (cn->clk->core == core) { in __clk_notify()
1846 cnd.clk = cn->clk; in __clk_notify()
1847 ret = srcu_notifier_call_chain(&cn->notifier_head, msg, in __clk_notify()
1862 * it goes. Note that if a clk does not implement the .recalc_accuracy
1873 if (core->parent) in __clk_recalc_accuracies()
1874 parent_accuracy = core->parent->accuracy; in __clk_recalc_accuracies()
1876 if (core->ops->recalc_accuracy) in __clk_recalc_accuracies()
1877 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_recalc_accuracies()
1880 core->accuracy = parent_accuracy; in __clk_recalc_accuracies()
1882 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_accuracies()
1888 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) in clk_core_get_accuracy_recalc()
1895 * clk_get_accuracy - return the accuracy of clk
1899 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1911 accuracy = clk_core_get_accuracy_recalc(clk->core); in clk_get_accuracy()
1921 unsigned long rate = parent_rate; in clk_recalc() local
1923 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { in clk_recalc()
1924 rate = core->ops->recalc_rate(core->hw, parent_rate); in clk_recalc()
1927 return rate; in clk_recalc()
1933 * @update_req: Whether req_rate should be updated with the new rate
1937 * goes. Note that if a clk does not implement the .recalc_rate callback then
1938 * it is assumed that the clock will take on the rate of its parent.
1952 old_rate = core->rate; in __clk_recalc_rates()
1954 if (core->parent) in __clk_recalc_rates()
1955 parent_rate = core->parent->rate; in __clk_recalc_rates()
1957 core->rate = clk_recalc(core, parent_rate); in __clk_recalc_rates()
1959 core->req_rate = core->rate; in __clk_recalc_rates()
1965 if (core->notifier_count && msg) in __clk_recalc_rates()
1966 __clk_notify(core, msg, old_rate, core->rate); in __clk_recalc_rates()
1968 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_rates()
1974 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) in clk_core_get_rate_recalc()
1981 * clk_get_rate - return the rate of clk
1982 * @clk: the clk whose rate is being returned
1984 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1985 * is set, which means a recalc_rate will be issued. Can be called regardless of
1991 unsigned long rate; in clk_get_rate() local
1997 rate = clk_core_get_rate_recalc(clk->core); in clk_get_rate()
2000 return rate; in clk_get_rate()
2010 return -EINVAL; in clk_fetch_parent_index()
2012 for (i = 0; i < core->num_parents; i++) { in clk_fetch_parent_index()
2014 if (core->parents[i].core == parent) in clk_fetch_parent_index()
2018 if (core->parents[i].core) in clk_fetch_parent_index()
2022 if (core->parents[i].hw) { in clk_fetch_parent_index()
2023 if (core->parents[i].hw == parent->hw) in clk_fetch_parent_index()
2026 /* Didn't match, but we're expecting a clk_hw */ in clk_fetch_parent_index()
2035 if (core->parents[i].name && in clk_fetch_parent_index()
2036 !strcmp(parent->name, core->parents[i].name)) in clk_fetch_parent_index()
2040 if (i == core->num_parents) in clk_fetch_parent_index()
2041 return -EINVAL; in clk_fetch_parent_index()
2043 core->parents[i].core = parent; in clk_fetch_parent_index()
2048 * clk_hw_get_parent_index - return the index of the parent clock
2051 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
2052 * clock does not have a current parent.
2059 return -EINVAL; in clk_hw_get_parent_index()
2061 return clk_fetch_parent_index(hw->core, parent->core); in clk_hw_get_parent_index()
2072 core->orphan = is_orphan; in clk_core_update_orphan_status()
2074 hlist_for_each_entry(child, &core->children, child_node) in clk_core_update_orphan_status()
2080 bool was_orphan = core->orphan; in clk_reparent()
2082 hlist_del(&core->child_node); in clk_reparent()
2085 bool becomes_orphan = new_parent->orphan; in clk_reparent()
2088 if (new_parent->new_child == core) in clk_reparent()
2089 new_parent->new_child = NULL; in clk_reparent()
2091 hlist_add_head(&core->child_node, &new_parent->children); in clk_reparent()
2096 hlist_add_head(&core->child_node, &clk_orphan_list); in clk_reparent()
2101 core->parent = new_parent; in clk_reparent()
2108 struct clk_core *old_parent = core->parent; in __clk_set_parent_before()
2116 * If the clock is not prepared, then a race with in __clk_set_parent_before()
2119 * a clk_prepare()). in __clk_set_parent_before()
2122 * parent and also protect against a race with clk_enable() by in __clk_set_parent_before()
2131 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_before()
2137 if (core->prepare_count) { in __clk_set_parent_before()
2156 * for preventing a race with clk_enable(). in __clk_set_parent_after()
2158 if (core->prepare_count) { in __clk_set_parent_after()
2163 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ in __clk_set_parent_after()
2164 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_after()
2182 if (parent && core->ops->set_parent) in __clk_set_parent()
2183 ret = core->ops->set_parent(core->hw, p_index); in __clk_set_parent()
2205 * @parent_rate: the "future" rate of clk's parent
2211 * pre-rate change notifications and returns early if no clks in the
2212 * subtree have subscribed to the notifications. Note that if a clk does not
2214 * take on the rate of its parent.
2227 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ in __clk_speculate_rates()
2228 if (core->notifier_count) in __clk_speculate_rates()
2229 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); in __clk_speculate_rates()
2233 __func__, core->name, ret); in __clk_speculate_rates()
2237 hlist_for_each_entry(child, &core->children, child_node) { in __clk_speculate_rates()
2252 core->new_rate = new_rate; in clk_calc_subtree()
2253 core->new_parent = new_parent; in clk_calc_subtree()
2254 core->new_parent_index = p_index; in clk_calc_subtree()
2256 core->new_child = NULL; in clk_calc_subtree()
2257 if (new_parent && new_parent != core->parent) in clk_calc_subtree()
2258 new_parent->new_child = core; in clk_calc_subtree()
2260 hlist_for_each_entry(child, &core->children, child_node) { in clk_calc_subtree()
2261 child->new_rate = clk_recalc(child, new_rate); in clk_calc_subtree()
2262 clk_calc_subtree(child, child->new_rate, NULL, 0); in clk_calc_subtree()
2271 unsigned long rate) in clk_calc_new_rates() argument
2286 /* save parent rate, if it exists */ in clk_calc_new_rates()
2287 parent = old_parent = core->parent; in clk_calc_new_rates()
2289 best_parent_rate = parent->rate; in clk_calc_new_rates()
2293 /* find the closest rate and parent clk/rate */ in clk_calc_new_rates()
2297 clk_core_init_rate_req(core, &req, rate); in clk_calc_new_rates()
2308 new_rate = req.rate; in clk_calc_new_rates()
2309 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; in clk_calc_new_rates()
2313 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { in clk_calc_new_rates()
2314 /* pass-through clock without adjustable parent */ in clk_calc_new_rates()
2315 core->new_rate = core->rate; in clk_calc_new_rates()
2318 /* pass-through clock with adjustable parent */ in clk_calc_new_rates()
2319 top = clk_calc_new_rates(parent, rate); in clk_calc_new_rates()
2320 new_rate = parent->new_rate; in clk_calc_new_rates()
2326 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { in clk_calc_new_rates()
2328 __func__, core->name); in clk_calc_new_rates()
2333 if (parent && core->num_parents > 1) { in clk_calc_new_rates()
2337 __func__, parent->name, core->name); in clk_calc_new_rates()
2342 if ((core->flags & CLK_SET_RATE_PARENT) && parent && in clk_calc_new_rates()
2343 best_parent_rate != parent->rate) in clk_calc_new_rates()
2353 * Notify about rate changes in a subtree. Always walk down the whole tree
2363 if (core->rate == core->new_rate) in clk_propagate_rate_change()
2366 if (core->notifier_count) { in clk_propagate_rate_change()
2367 ret = __clk_notify(core, event, core->rate, core->new_rate); in clk_propagate_rate_change()
2372 hlist_for_each_entry(child, &core->children, child_node) { in clk_propagate_rate_change()
2374 if (child->new_parent && child->new_parent != core) in clk_propagate_rate_change()
2381 /* handle the new child who might not be in core->children yet */ in clk_propagate_rate_change()
2382 if (core->new_child) { in clk_propagate_rate_change()
2383 tmp_clk = clk_propagate_rate_change(core->new_child, event); in clk_propagate_rate_change()
2392 * walk down a subtree and set the new rates notifying the rate
2405 old_rate = core->rate; in clk_change_rate()
2407 if (core->new_parent) { in clk_change_rate()
2408 parent = core->new_parent; in clk_change_rate()
2409 best_parent_rate = core->new_parent->rate; in clk_change_rate()
2410 } else if (core->parent) { in clk_change_rate()
2411 parent = core->parent; in clk_change_rate()
2412 best_parent_rate = core->parent->rate; in clk_change_rate()
2418 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2423 if (core->new_parent && core->new_parent != core->parent) { in clk_change_rate()
2424 old_parent = __clk_set_parent_before(core, core->new_parent); in clk_change_rate()
2425 trace_clk_set_parent(core, core->new_parent); in clk_change_rate()
2427 if (core->ops->set_rate_and_parent) { in clk_change_rate()
2429 core->ops->set_rate_and_parent(core->hw, core->new_rate, in clk_change_rate()
2431 core->new_parent_index); in clk_change_rate()
2432 } else if (core->ops->set_parent) { in clk_change_rate()
2433 core->ops->set_parent(core->hw, core->new_parent_index); in clk_change_rate()
2436 trace_clk_set_parent_complete(core, core->new_parent); in clk_change_rate()
2437 __clk_set_parent_after(core, core->new_parent, old_parent); in clk_change_rate()
2440 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2443 trace_clk_set_rate(core, core->new_rate); in clk_change_rate()
2445 if (!skip_set_rate && core->ops->set_rate) in clk_change_rate()
2446 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); in clk_change_rate()
2448 trace_clk_set_rate_complete(core, core->new_rate); in clk_change_rate()
2450 core->rate = clk_recalc(core, best_parent_rate); in clk_change_rate()
2452 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2457 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2460 if (core->notifier_count && old_rate != core->rate) in clk_change_rate()
2461 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); in clk_change_rate()
2463 if (core->flags & CLK_RECALC_NEW_RATES) in clk_change_rate()
2464 (void)clk_calc_new_rates(core, core->new_rate); in clk_change_rate()
2470 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { in clk_change_rate()
2472 if (child->new_parent && child->new_parent != core) in clk_change_rate()
2477 /* handle the new child who might not be in core->children yet */ in clk_change_rate()
2478 if (core->new_child) in clk_change_rate()
2479 clk_change_rate(core->new_child); in clk_change_rate()
2495 /* simulate what the rate would be if it could be freely set */ in clk_core_req_round_rate_nolock()
2511 return ret ? 0 : req.rate; in clk_core_req_round_rate_nolock()
2518 unsigned long rate; in clk_core_set_rate_nolock() local
2524 rate = clk_core_req_round_rate_nolock(core, req_rate); in clk_core_set_rate_nolock()
2527 if (rate == clk_core_get_rate_nolock(core)) in clk_core_set_rate_nolock()
2530 /* fail on a direct rate set of a protected provider */ in clk_core_set_rate_nolock()
2532 return -EBUSY; in clk_core_set_rate_nolock()
2537 return -EINVAL; in clk_core_set_rate_nolock()
2546 pr_debug("%s: failed to set %s rate\n", __func__, in clk_core_set_rate_nolock()
2547 fail_clk->name); in clk_core_set_rate_nolock()
2549 ret = -EBUSY; in clk_core_set_rate_nolock()
2556 core->req_rate = req_rate; in clk_core_set_rate_nolock()
2564 * clk_set_rate - specify a new rate for clk
2565 * @clk: the clk whose rate is being changed
2566 * @rate: the new rate for clk
2568 * In the simplest case clk_set_rate will only adjust the rate of clk.
2570 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2574 * *parent_rate comes back with a new rate for clk's parent then we propagate
2575 * up to clk's parent and set its rate. Upward propagation will continue
2576 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2579 * Rate changes are accomplished via tree traversal that also recalculates the
2582 * Returns 0 on success, -EERROR otherwise.
2584 int clk_set_rate(struct clk *clk, unsigned long rate) in clk_set_rate() argument
2594 if (clk->exclusive_count) in clk_set_rate()
2595 clk_core_rate_unprotect(clk->core); in clk_set_rate()
2597 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate()
2599 if (clk->exclusive_count) in clk_set_rate()
2600 clk_core_rate_protect(clk->core); in clk_set_rate()
2609 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2610 * @clk: the clk whose rate is being changed
2611 * @rate: the new rate for clk
2613 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2614 * within a critical section
2620 * The exclusivity is not applied if setting the rate failed.
2625 * Returns 0 on success, -EERROR otherwise.
2627 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) in clk_set_rate_exclusive() argument
2643 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_exclusive()
2645 clk_core_rate_protect(clk->core); in clk_set_rate_exclusive()
2646 clk->exclusive_count++; in clk_set_rate_exclusive()
2660 unsigned long old_min, old_max, rate; in clk_set_rate_range_nolock() local
2667 trace_clk_set_rate_range(clk->core, min, max); in clk_set_rate_range_nolock()
2671 __func__, clk->core->name, clk->dev_id, clk->con_id, in clk_set_rate_range_nolock()
2673 return -EINVAL; in clk_set_rate_range_nolock()
2676 if (clk->exclusive_count) in clk_set_rate_range_nolock()
2677 clk_core_rate_unprotect(clk->core); in clk_set_rate_range_nolock()
2680 old_min = clk->min_rate; in clk_set_rate_range_nolock()
2681 old_max = clk->max_rate; in clk_set_rate_range_nolock()
2682 clk->min_rate = min; in clk_set_rate_range_nolock()
2683 clk->max_rate = max; in clk_set_rate_range_nolock()
2685 if (!clk_core_check_boundaries(clk->core, min, max)) { in clk_set_rate_range_nolock()
2686 ret = -EINVAL; in clk_set_rate_range_nolock()
2690 rate = clk->core->req_rate; in clk_set_rate_range_nolock()
2691 if (clk->core->flags & CLK_GET_RATE_NOCACHE) in clk_set_rate_range_nolock()
2692 rate = clk_core_get_rate_recalc(clk->core); in clk_set_rate_range_nolock()
2696 * opportunity to the provider to adjust the clock rate based on in clk_set_rate_range_nolock()
2700 * outside of the boundaries. Clamping the last requested rate in clk_set_rate_range_nolock()
2704 * There is a catch. It may fail for the usual reason (clock in clk_set_rate_range_nolock()
2706 * - round_rate() was not favorable and fell on the wrong in clk_set_rate_range_nolock()
2708 * - the determine_rate() callback does not really check for in clk_set_rate_range_nolock()
2709 * this corner case when determining the rate in clk_set_rate_range_nolock()
2711 rate = clamp(rate, min, max); in clk_set_rate_range_nolock()
2712 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_range_nolock()
2715 clk->min_rate = old_min; in clk_set_rate_range_nolock()
2716 clk->max_rate = old_max; in clk_set_rate_range_nolock()
2720 if (clk->exclusive_count) in clk_set_rate_range_nolock()
2721 clk_core_rate_protect(clk->core); in clk_set_rate_range_nolock()
2727 * clk_set_rate_range - set a rate range for a clock source
2729 * @min: desired minimum clock rate in Hz, inclusive
2730 * @max: desired maximum clock rate in Hz, inclusive
2752 * clk_set_min_rate - set a minimum clock rate for a clock source
2754 * @rate: desired minimum clock rate in Hz, inclusive
2758 int clk_set_min_rate(struct clk *clk, unsigned long rate) in clk_set_min_rate() argument
2763 trace_clk_set_min_rate(clk->core, rate); in clk_set_min_rate()
2765 return clk_set_rate_range(clk, rate, clk->max_rate); in clk_set_min_rate()
2770 * clk_set_max_rate - set a maximum clock rate for a clock source
2772 * @rate: desired maximum clock rate in Hz, inclusive
2776 int clk_set_max_rate(struct clk *clk, unsigned long rate) in clk_set_max_rate() argument
2781 trace_clk_set_max_rate(clk->core, rate); in clk_set_max_rate()
2783 return clk_set_rate_range(clk, clk->min_rate, rate); in clk_set_max_rate()
2788 * clk_get_parent - return the parent of a clk
2791 * Simply returns clk->parent. Returns NULL if clk is NULL.
2801 /* TODO: Create a per-user clk and change callers to call clk_put */ in clk_get_parent()
2802 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; in clk_get_parent()
2813 if (core->num_parents > 1 && core->ops->get_parent) in __clk_init_parent()
2814 index = core->ops->get_parent(core->hw); in __clk_init_parent()
2832 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); in clk_hw_reparent()
2836 * clk_has_parent - check if a clock is a possible parent for another
2840 * This function can be used in drivers that need to check that a clock can be
2843 * Returns true if @parent is a possible parent for @clk, false otherwise.
2851 return clk_core_has_parent(clk->core, parent->core); in clk_has_parent()
2867 if (core->parent == parent) in clk_core_set_parent_nolock()
2870 /* verify ops for multi-parent clks */ in clk_core_set_parent_nolock()
2871 if (core->num_parents > 1 && !core->ops->set_parent) in clk_core_set_parent_nolock()
2872 return -EPERM; in clk_core_set_parent_nolock()
2874 /* check that we are allowed to re-parent if the clock is in use */ in clk_core_set_parent_nolock()
2875 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) in clk_core_set_parent_nolock()
2876 return -EBUSY; in clk_core_set_parent_nolock()
2879 return -EBUSY; in clk_core_set_parent_nolock()
2886 __func__, parent->name, core->name); in clk_core_set_parent_nolock()
2889 p_rate = parent->rate; in clk_core_set_parent_nolock()
2899 /* abort if a driver objects */ in clk_core_set_parent_nolock()
2903 /* do the re-parent */ in clk_core_set_parent_nolock()
2906 /* propagate rate an accuracy recalculation accordingly */ in clk_core_set_parent_nolock()
2922 return clk_core_set_parent_nolock(hw->core, parent->core); in clk_hw_set_parent()
2927 * clk_set_parent - switch the parent of a mux clk
2931 * Re-parent clk to use parent as its new input source. If clk is in
2933 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2938 * clk topology, sysfs topology and propagate rate recalculation via
2941 * Returns 0 on success, -EERROR otherwise.
2952 if (clk->exclusive_count) in clk_set_parent()
2953 clk_core_rate_unprotect(clk->core); in clk_set_parent()
2955 ret = clk_core_set_parent_nolock(clk->core, in clk_set_parent()
2956 parent ? parent->core : NULL); in clk_set_parent()
2958 if (clk->exclusive_count) in clk_set_parent()
2959 clk_core_rate_protect(clk->core); in clk_set_parent()
2969 int ret = -EINVAL; in clk_core_set_phase_nolock()
2977 return -EBUSY; in clk_core_set_phase_nolock()
2981 if (core->ops->set_phase) { in clk_core_set_phase_nolock()
2982 ret = core->ops->set_phase(core->hw, degrees); in clk_core_set_phase_nolock()
2984 core->phase = degrees; in clk_core_set_phase_nolock()
2993 * clk_set_phase - adjust the phase shift of a clock signal
2997 * Shifts the phase of a clock signal by the specified
2998 * degrees. Returns 0 on success, -EERROR otherwise.
3002 * phase locked-loop clock signal generators we may shift phase with
3009 * clock accuracy. A parent clock phase attribute does not have an
3010 * impact on the phase attribute of a child clock.
3026 if (clk->exclusive_count) in clk_set_phase()
3027 clk_core_rate_unprotect(clk->core); in clk_set_phase()
3029 ret = clk_core_set_phase_nolock(clk->core, degrees); in clk_set_phase()
3031 if (clk->exclusive_count) in clk_set_phase()
3032 clk_core_rate_protect(clk->core); in clk_set_phase()
3045 if (!core->ops->get_phase) in clk_core_get_phase()
3049 ret = core->ops->get_phase(core->hw); in clk_core_get_phase()
3051 core->phase = ret; in clk_core_get_phase()
3057 * clk_get_phase - return the phase shift of a clock signal
3060 * Returns the phase shift of a clock node in degrees, otherwise returns
3061 * -EERROR.
3071 ret = clk_core_get_phase(clk->core); in clk_get_phase()
3080 /* Assume a default value of 50% */ in clk_core_reset_duty_cycle_nolock()
3081 core->duty.num = 1; in clk_core_reset_duty_cycle_nolock()
3082 core->duty.den = 2; in clk_core_reset_duty_cycle_nolock()
3089 struct clk_duty *duty = &core->duty; in clk_core_update_duty_cycle_nolock()
3092 if (!core->ops->get_duty_cycle) in clk_core_update_duty_cycle_nolock()
3095 ret = core->ops->get_duty_cycle(core->hw, duty); in clk_core_update_duty_cycle_nolock()
3100 if (duty->den == 0 || duty->num > duty->den) { in clk_core_update_duty_cycle_nolock()
3101 ret = -EINVAL; in clk_core_update_duty_cycle_nolock()
3116 if (core->parent && in clk_core_update_duty_cycle_parent_nolock()
3117 core->flags & CLK_DUTY_CYCLE_PARENT) { in clk_core_update_duty_cycle_parent_nolock()
3118 ret = clk_core_update_duty_cycle_nolock(core->parent); in clk_core_update_duty_cycle_parent_nolock()
3119 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_update_duty_cycle_parent_nolock()
3138 return -EBUSY; in clk_core_set_duty_cycle_nolock()
3142 if (!core->ops->set_duty_cycle) in clk_core_set_duty_cycle_nolock()
3145 ret = core->ops->set_duty_cycle(core->hw, duty); in clk_core_set_duty_cycle_nolock()
3147 memcpy(&core->duty, duty, sizeof(*duty)); in clk_core_set_duty_cycle_nolock()
3159 if (core->parent && in clk_core_set_duty_cycle_parent_nolock()
3160 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) { in clk_core_set_duty_cycle_parent_nolock()
3161 ret = clk_core_set_duty_cycle_nolock(core->parent, duty); in clk_core_set_duty_cycle_parent_nolock()
3162 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_set_duty_cycle_parent_nolock()
3169 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
3177 * Returns (0) on success, a negative errno otherwise.
3189 return -EINVAL; in clk_set_duty_cycle()
3196 if (clk->exclusive_count) in clk_set_duty_cycle()
3197 clk_core_rate_unprotect(clk->core); in clk_set_duty_cycle()
3199 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty); in clk_set_duty_cycle()
3201 if (clk->exclusive_count) in clk_set_duty_cycle()
3202 clk_core_rate_protect(clk->core); in clk_set_duty_cycle()
3213 struct clk_duty *duty = &core->duty; in clk_core_get_scaled_duty_cycle()
3220 ret = mult_frac(scale, duty->num, duty->den); in clk_core_get_scaled_duty_cycle()
3228 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
3232 * Returns the duty cycle ratio of a clock node multiplied by the provided
3240 return clk_core_get_scaled_duty_cycle(clk->core, scale); in clk_get_scaled_duty_cycle()
3245 * clk_is_match - check if two clk's point to the same hardware clock
3261 /* true if clk->core pointers match. Avoid dereferencing garbage */ in clk_is_match()
3263 if (p->core == q->core) in clk_is_match()
3292 seq_printf(s, "%*s%-*s %-7d %-8d %-8d %-11lu %-10lu ", in clk_summary_show_one()
3294 35 - level * 3, c->name, in clk_summary_show_one()
3295 c->enable_count, c->prepare_count, c->protect_count, in clk_summary_show_one()
3301 seq_printf(s, "%-5d", phase); in clk_summary_show_one()
3303 seq_puts(s, "-----"); in clk_summary_show_one()
3305 seq_printf(s, " %-6d", clk_core_get_scaled_duty_cycle(c, 100000)); in clk_summary_show_one()
3307 if (c->ops->is_enabled) in clk_summary_show_one()
3309 else if (!c->ops->enable) in clk_summary_show_one()
3314 hlist_for_each_entry(clk_user, &c->clks, clks_node) { in clk_summary_show_one()
3315 seq_printf(s, "%*s%-*s %-25s\n", in clk_summary_show_one()
3318 clk_user->dev_id ? clk_user->dev_id : "deviceless", in clk_summary_show_one()
3319 clk_user->con_id ? clk_user->con_id : "no_connection_id"); in clk_summary_show_one()
3333 hlist_for_each_entry(child, &c->children, child_node) in clk_summary_show_subtree()
3340 struct hlist_head **lists = s->private; in clk_summary_show()
3344 …seq_puts(s, " clock count count count rate accuracy phas… in clk_summary_show()
3345 …s(s, "--------------------------------------------------------------------------------------------… in clk_summary_show()
3371 /* This should be JSON format, i.e. elements separated with a comma */ in clk_dump_one()
3372 seq_printf(s, "\"%s\": { ", c->name); in clk_dump_one()
3373 seq_printf(s, "\"enable_count\": %d,", c->enable_count); in clk_dump_one()
3374 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); in clk_dump_one()
3375 seq_printf(s, "\"protect_count\": %d,", c->protect_count); in clk_dump_one()
3376 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate_recalc(c)); in clk_dump_one()
3393 hlist_for_each_entry(child, &c->children, child_node) { in clk_dump_subtree()
3405 struct hlist_head **lists = s->private; in clk_dump_show()
3475 ret = clk_prepare_enable(core->hw->clk); in clk_prepare_enable_set()
3477 clk_disable_unprepare(core->hw->clk); in clk_prepare_enable_set()
3486 *val = core->enable_count && core->prepare_count; in clk_prepare_enable_get()
3518 *val = core->phase; in clk_phase_get()
3546 struct clk_core *core = s->private; in clk_flags_show()
3547 unsigned long flags = core->flags; in clk_flags_show()
3572 * Go through the following options to fetch a parent's name. in possible_parent_show()
3577 * 4. Fetch parent clock's clock-output-name if DT index was set in possible_parent_show()
3580 * specified directly via a struct clk_hw pointer, but it isn't in possible_parent_show()
3585 seq_puts(s, parent->name); in possible_parent_show()
3586 } else if (core->parents[i].name) { in possible_parent_show()
3587 seq_puts(s, core->parents[i].name); in possible_parent_show()
3588 } else if (core->parents[i].fw_name) { in possible_parent_show()
3589 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name); in possible_parent_show()
3591 if (core->parents[i].index >= 0) in possible_parent_show()
3592 name = of_clk_get_parent_name(core->of_node, core->parents[i].index); in possible_parent_show()
3604 struct clk_core *core = s->private; in possible_parents_show()
3607 for (i = 0; i < core->num_parents - 1; i++) in possible_parents_show()
3618 struct clk_core *core = s->private; in current_parent_show()
3620 if (core->parent) in current_parent_show()
3621 seq_printf(s, "%s\n", core->parent->name); in current_parent_show()
3631 struct seq_file *s = file->private_data; in current_parent_write()
3632 struct clk_core *core = s->private; in current_parent_write()
3643 return -ENOENT; in current_parent_write()
3665 struct clk_core *core = s->private; in clk_duty_cycle_show()
3666 struct clk_duty *duty = &core->duty; in clk_duty_cycle_show()
3668 seq_printf(s, "%u/%u\n", duty->num, duty->den); in clk_duty_cycle_show()
3676 struct clk_core *core = s->private; in clk_min_rate_show()
3690 struct clk_core *core = s->private; in clk_max_rate_show()
3709 root = debugfs_create_dir(core->name, pdentry); in clk_debug_create_one()
3710 core->dentry = root; in clk_debug_create_one()
3716 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy); in clk_debug_create_one()
3720 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count); in clk_debug_create_one()
3721 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count); in clk_debug_create_one()
3722 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count); in clk_debug_create_one()
3723 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count); in clk_debug_create_one()
3730 if (core->num_parents > 1) in clk_debug_create_one()
3735 if (core->num_parents > 0) in clk_debug_create_one()
3739 if (core->num_parents > 1) in clk_debug_create_one()
3743 if (core->ops->debug_init) in clk_debug_create_one()
3744 core->ops->debug_init(core->hw, core->dentry); in clk_debug_create_one()
3748 * clk_debug_register - add a clk node to the debugfs clk directory
3751 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3753 * will be created lazily by clk_debug_init as part of a late_initcall.
3758 hlist_add_head(&core->debug_node, &clk_debug_list); in clk_debug_register()
3765 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3768 * Dynamically removes a clk and all its child nodes from the
3769 * debugfs clk directory if clk->dentry points to debugfs created by
3775 hlist_del_init(&core->debug_node); in clk_debug_unregister()
3776 debugfs_remove_recursive(core->dentry); in clk_debug_unregister()
3777 core->dentry = NULL; in clk_debug_unregister()
3782 * clk_debug_init - lazily populate the debugfs clk directory
3786 * populates the debugfs clk directory once at boot-time when we know that
3787 * debugfs is setup. It should only be called once at boot-time, all other clks
3802 pr_warn("** such as parent or rate setting, enabling, disabling, etc. **\n"); in clk_debug_init()
3846 * walk the list of orphan clocks and reparent any that newly finds a in clk_core_reparent_orphans_nolock()
3856 * are enabled during init but might not have a parent yet. in clk_core_reparent_orphans_nolock()
3871 * clk_put() to trigger a clk_set_rate() call whenever in clk_core_reparent_orphans_nolock()
3873 * 'req_rate' is set to something non-zero so that in clk_core_reparent_orphans_nolock()
3876 orphan->req_rate = orphan->rate; in clk_core_reparent_orphans_nolock()
3882 * __clk_core_init - initialize the data structures in a struct clk_core
3886 * parent and rate and sets them both.
3892 unsigned long rate; in __clk_core_init() local
3898 * Set hw->core after grabbing the prepare_lock to synchronize with in __clk_core_init()
3899 * callers of clk_core_fill_parent_index() where we treat hw->core in __clk_core_init()
3903 core->hw->core = core; in __clk_core_init()
3909 /* check to see if a clock with this name is already registered */ in __clk_core_init()
3910 if (clk_core_lookup(core->name)) { in __clk_core_init()
3912 __func__, core->name); in __clk_core_init()
3913 ret = -EEXIST; in __clk_core_init()
3917 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */ in __clk_core_init()
3918 if (core->ops->set_rate && in __clk_core_init()
3919 !((core->ops->round_rate || core->ops->determine_rate) && in __clk_core_init()
3920 core->ops->recalc_rate)) { in __clk_core_init()
3922 __func__, core->name); in __clk_core_init()
3923 ret = -EINVAL; in __clk_core_init()
3927 if (core->ops->set_parent && !core->ops->get_parent) { in __clk_core_init()
3929 __func__, core->name); in __clk_core_init()
3930 ret = -EINVAL; in __clk_core_init()
3934 if (core->ops->set_parent && !core->ops->determine_rate) { in __clk_core_init()
3936 __func__, core->name); in __clk_core_init()
3937 ret = -EINVAL; in __clk_core_init()
3941 if (core->num_parents > 1 && !core->ops->get_parent) { in __clk_core_init()
3943 __func__, core->name); in __clk_core_init()
3944 ret = -EINVAL; in __clk_core_init()
3948 if (core->ops->set_rate_and_parent && in __clk_core_init()
3949 !(core->ops->set_parent && core->ops->set_rate)) { in __clk_core_init()
3951 __func__, core->name); in __clk_core_init()
3952 ret = -EINVAL; in __clk_core_init()
3957 * optional platform-specific magic in __clk_core_init()
3965 * such as rate, parent, etc ... in __clk_core_init()
3970 if (core->ops->init) { in __clk_core_init()
3971 ret = core->ops->init(core->hw); in __clk_core_init()
3976 parent = core->parent = __clk_init_parent(core); in __clk_core_init()
3979 * Populate core->parent if parent has already been clk_core_init'd. If in __clk_core_init()
3984 * Every time a new clk is clk_init'd then we walk the list of orphan in __clk_core_init()
3985 * clocks and re-parent any that are children of the clock currently in __clk_core_init()
3989 hlist_add_head(&core->child_node, &parent->children); in __clk_core_init()
3990 core->orphan = parent->orphan; in __clk_core_init()
3991 } else if (!core->num_parents) { in __clk_core_init()
3992 hlist_add_head(&core->child_node, &clk_root_list); in __clk_core_init()
3993 core->orphan = false; in __clk_core_init()
3995 hlist_add_head(&core->child_node, &clk_orphan_list); in __clk_core_init()
3996 core->orphan = true; in __clk_core_init()
3998 hash_add(clk_hashtable, &core->hashtable_node, in __clk_core_init()
3999 full_name_hash(NULL, core->name, strlen(core->name))); in __clk_core_init()
4004 * fallback is to use the parent's accuracy. If a clock doesn't have a in __clk_core_init()
4008 if (core->ops->recalc_accuracy) in __clk_core_init()
4009 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_core_init()
4012 core->accuracy = parent->accuracy; in __clk_core_init()
4014 core->accuracy = 0; in __clk_core_init()
4018 * Since a phase is by definition relative to its parent, just in __clk_core_init()
4025 core->name); in __clk_core_init()
4035 * Set clk's rate. The preferred method is to use .recalc_rate. For in __clk_core_init()
4037 * parent's rate. If a clock doesn't have a parent (or is orphaned) in __clk_core_init()
4038 * then rate is set to zero. in __clk_core_init()
4040 if (core->ops->recalc_rate) in __clk_core_init()
4041 rate = core->ops->recalc_rate(core->hw, in __clk_core_init()
4044 rate = parent->rate; in __clk_core_init()
4046 rate = 0; in __clk_core_init()
4047 core->rate = core->req_rate = rate; in __clk_core_init()
4054 if (core->flags & CLK_IS_CRITICAL) { in __clk_core_init()
4058 __func__, core->name); in __clk_core_init()
4065 __func__, core->name); in __clk_core_init()
4076 hash_del(&core->hashtable_node); in __clk_core_init()
4077 hlist_del_init(&core->child_node); in __clk_core_init()
4078 core->hw->core = NULL; in __clk_core_init()
4090 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
4092 * @clk: consumer to link to a clk
4097 hlist_add_head(&clk->clks_node, &core->clks); in clk_core_link_consumer()
4102 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
4108 hlist_del(&clk->clks_node); in clk_core_unlink_consumer()
4112 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
4113 * @core: clk to allocate a consumer for
4126 return ERR_PTR(-ENOMEM); in alloc_clk()
4128 clk->core = core; in alloc_clk()
4129 clk->dev_id = dev_id; in alloc_clk()
4130 clk->con_id = kstrdup_const(con_id, GFP_KERNEL); in alloc_clk()
4131 clk->max_rate = ULONG_MAX; in alloc_clk()
4137 * free_clk - Free a clk consumer
4145 kfree_const(clk->con_id); in free_clk()
4150 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
4151 * a clk_hw
4157 * This is the main function used to create a clk pointer for use by clk
4158 * consumers. It connects a consumer to the clk_core and clk_hw structures
4171 core = hw->core; in clk_hw_create_clk()
4175 clk->dev = dev; in clk_hw_create_clk()
4177 if (!try_module_get(core->owner)) { in clk_hw_create_clk()
4179 return ERR_PTR(-ENOENT); in clk_hw_create_clk()
4182 kref_get(&core->ref); in clk_hw_create_clk()
4189 * clk_hw_get_clk - get clk consumer given an clk_hw
4195 * to get a consumer clk and act on the clock element
4200 struct device *dev = hw->core->dev; in clk_hw_get_clk()
4213 return -EINVAL; in clk_cpy_name()
4219 return -ENOMEM; in clk_cpy_name()
4227 u8 num_parents = init->num_parents; in clk_core_populate_parent_map()
4228 const char * const *parent_names = init->parent_names; in clk_core_populate_parent_map()
4229 const struct clk_hw **parent_hws = init->parent_hws; in clk_core_populate_parent_map()
4230 const struct clk_parent_data *parent_data = init->parent_data; in clk_core_populate_parent_map()
4238 * Avoid unnecessary string look-ups of clk_core's possible parents by in clk_core_populate_parent_map()
4239 * having a cache of names/clk_hw pointers to clk_core pointers. in clk_core_populate_parent_map()
4242 core->parents = parents; in clk_core_populate_parent_map()
4244 return -ENOMEM; in clk_core_populate_parent_map()
4248 parent->index = -1; in clk_core_populate_parent_map()
4250 /* throw a WARN if any entries are NULL */ in clk_core_populate_parent_map()
4253 __func__, core->name); in clk_core_populate_parent_map()
4254 ret = clk_cpy_name(&parent->name, parent_names[i], in clk_core_populate_parent_map()
4257 parent->hw = parent_data[i].hw; in clk_core_populate_parent_map()
4258 parent->index = parent_data[i].index; in clk_core_populate_parent_map()
4259 ret = clk_cpy_name(&parent->fw_name, in clk_core_populate_parent_map()
4262 ret = clk_cpy_name(&parent->name, in clk_core_populate_parent_map()
4266 parent->hw = parent_hws[i]; in clk_core_populate_parent_map()
4268 ret = -EINVAL; in clk_core_populate_parent_map()
4276 } while (--i >= 0); in clk_core_populate_parent_map()
4288 int i = core->num_parents; in clk_core_free_parent_map()
4290 if (!core->num_parents) in clk_core_free_parent_map()
4293 while (--i >= 0) { in clk_core_free_parent_map()
4294 kfree_const(core->parents[i].name); in clk_core_free_parent_map()
4295 kfree_const(core->parents[i].fw_name); in clk_core_free_parent_map()
4298 kfree(core->parents); in clk_core_free_parent_map()
4301 /* Free memory allocated for a struct clk_core */
4306 if (core->rpm_enabled) { in __clk_release()
4308 hlist_del(&core->rpm_node); in __clk_release()
4313 kfree_const(core->name); in __clk_release()
4322 const struct clk_init_data *init = hw->init; in __clk_register()
4327 * we catch use of hw->init early on in the core. in __clk_register()
4329 hw->init = NULL; in __clk_register()
4333 ret = -ENOMEM; in __clk_register()
4337 kref_init(&core->ref); in __clk_register()
4339 core->name = kstrdup_const(init->name, GFP_KERNEL); in __clk_register()
4340 if (!core->name) { in __clk_register()
4341 ret = -ENOMEM; in __clk_register()
4345 if (WARN_ON(!init->ops)) { in __clk_register()
4346 ret = -EINVAL; in __clk_register()
4349 core->ops = init->ops; in __clk_register()
4351 core->dev = dev; in __clk_register()
4353 core->of_node = np; in __clk_register()
4354 if (dev && dev->driver) in __clk_register()
4355 core->owner = dev->driver->owner; in __clk_register()
4356 core->hw = hw; in __clk_register()
4357 core->flags = init->flags; in __clk_register()
4358 core->num_parents = init->num_parents; in __clk_register()
4359 core->min_rate = 0; in __clk_register()
4360 core->max_rate = ULONG_MAX; in __clk_register()
4366 INIT_HLIST_HEAD(&core->clks); in __clk_register()
4372 hw->clk = alloc_clk(core, NULL, NULL); in __clk_register()
4373 if (IS_ERR(hw->clk)) { in __clk_register()
4374 ret = PTR_ERR(hw->clk); in __clk_register()
4378 clk_core_link_consumer(core, hw->clk); in __clk_register()
4382 return hw->clk; in __clk_register()
4385 clk_core_unlink_consumer(hw->clk); in __clk_register()
4388 free_clk(hw->clk); in __clk_register()
4389 hw->clk = NULL; in __clk_register()
4395 kref_put(&core->ref, __clk_release); in __clk_register()
4399 init->name, hw); in __clk_register()
4402 np, ERR_PTR(ret), init->name, hw); in __clk_register()
4408 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
4412 * @dev->parent if dev doesn't have a device node, or NULL if neither
4413 * @dev or @dev->parent have a device node.
4424 np = dev_of_node(dev->parent); in dev_or_parent_of_node()
4430 * clk_register - allocate a new clock, register it and return an opaque cookie
4432 * @hw: link to hardware-specific clock data
4437 * Returns: a pointer to the newly allocated struct clk which
4449 * clk_hw_register - register a clk_hw and return an error code
4451 * @hw: link to hardware-specific clock data
4466 * of_clk_hw_register - register a clk_hw and return an error code
4468 * @hw: link to hardware-specific clock data
4471 * with new clock nodes when a struct device is not available, but a struct
4484 * after clk_unregister() was called on a clock and until last clock
4489 return -ENXIO; in clk_nodrv_prepare_enable()
4497 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate, in clk_nodrv_set_rate() argument
4500 return -ENXIO; in clk_nodrv_set_rate()
4505 return -ENXIO; in clk_nodrv_set_parent()
4511 return -ENXIO; in clk_nodrv_determine_rate()
4530 for (i = 0; i < root->num_parents; i++) in clk_core_evict_parent_cache_subtree()
4531 if (root->parents[i].core == target) in clk_core_evict_parent_cache_subtree()
4532 root->parents[i].core = NULL; in clk_core_evict_parent_cache_subtree()
4534 hlist_for_each_entry(child, &root->children, child_node) in clk_core_evict_parent_cache_subtree()
4553 * clk_unregister - unregister a currently registered clock
4564 clk_debug_unregister(clk->core); in clk_unregister()
4568 ops = clk->core->ops; in clk_unregister()
4571 clk->core->name); in clk_unregister()
4577 * a reference to this clock. in clk_unregister()
4580 clk->core->ops = &clk_nodrv_ops; in clk_unregister()
4583 if (ops->terminate) in clk_unregister()
4584 ops->terminate(clk->core->hw); in clk_unregister()
4586 if (!hlist_empty(&clk->core->children)) { in clk_unregister()
4591 hlist_for_each_entry_safe(child, t, &clk->core->children, in clk_unregister()
4596 clk_core_evict_parent_cache(clk->core); in clk_unregister()
4598 hash_del(&clk->core->hashtable_node); in clk_unregister()
4599 hlist_del_init(&clk->core->child_node); in clk_unregister()
4601 if (clk->core->prepare_count) in clk_unregister()
4603 __func__, clk->core->name); in clk_unregister()
4605 if (clk->core->protect_count) in clk_unregister()
4607 __func__, clk->core->name); in clk_unregister()
4610 kref_put(&clk->core->ref, __clk_release); in clk_unregister()
4616 * clk_hw_unregister - unregister a currently registered clk_hw
4617 * @hw: hardware-specific clock data to unregister
4621 clk_unregister(hw->clk); in clk_hw_unregister()
4636 * devm_clk_register - resource managed clk_register()
4638 * @hw: link to hardware-specific clock data
4652 return ERR_PTR(-ENOMEM); in devm_clk_register()
4667 * devm_clk_hw_register - resource managed clk_hw_register()
4669 * @hw: link to hardware-specific clock data
4682 return -ENOMEM; in devm_clk_hw_register()
4702 * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
4721 WARN_ON_ONCE(dev != hw->core->dev); in devm_clk_hw_get_clk()
4725 return ERR_PTR(-ENOMEM); in devm_clk_hw_get_clk()
4753 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a in __clk_put()
4757 if (WARN_ON(clk->exclusive_count)) { in __clk_put()
4759 clk->core->protect_count -= (clk->exclusive_count - 1); in __clk_put()
4760 clk_core_rate_unprotect(clk->core); in __clk_put()
4761 clk->exclusive_count = 0; in __clk_put()
4767 if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX) in __clk_put()
4772 owner = clk->core->owner; in __clk_put()
4773 kref_put(&clk->core->ref, __clk_release); in __clk_put()
4778 /*** clk rate change notifiers ***/
4781 * clk_notifier_register - add a clk rate change notifier
4785 * Request notification when clk's rate changes. This uses an SRCU
4788 * re-enter into the clk framework by calling any top-level clk APIs;
4789 * this will cause a nested prepare_lock mutex.
4791 * In all notification cases (pre, post and abort rate change) the original
4792 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4795 * clk_notifier_register() must be called from non-atomic context.
4796 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4803 int ret = -ENOMEM; in clk_notifier_register()
4806 return -EINVAL; in clk_notifier_register()
4812 if (cn->clk == clk) in clk_notifier_register()
4820 cn->clk = clk; in clk_notifier_register()
4821 srcu_init_notifier_head(&cn->notifier_head); in clk_notifier_register()
4823 list_add(&cn->node, &clk_notifier_list); in clk_notifier_register()
4826 ret = srcu_notifier_chain_register(&cn->notifier_head, nb); in clk_notifier_register()
4828 clk->core->notifier_count++; in clk_notifier_register()
4838 * clk_notifier_unregister - remove a clk rate change notifier
4845 * Returns -EINVAL if called with null arguments; otherwise, passes
4851 int ret = -ENOENT; in clk_notifier_unregister()
4854 return -EINVAL; in clk_notifier_unregister()
4859 if (cn->clk == clk) { in clk_notifier_unregister()
4860 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); in clk_notifier_unregister()
4862 clk->core->notifier_count--; in clk_notifier_unregister()
4865 if (!cn->notifier_head.head) { in clk_notifier_unregister()
4866 srcu_cleanup_notifier_head(&cn->notifier_head); in clk_notifier_unregister()
4867 list_del(&cn->node); in clk_notifier_unregister()
4889 clk_notifier_unregister(devres->clk, devres->nb); in devm_clk_notifier_release()
4902 return -ENOMEM; in devm_clk_notifier_register()
4906 devres->clk = clk; in devm_clk_notifier_register()
4907 devres->nb = nb; in devm_clk_notifier_register()
4926 * struct of_clk_provider - Clock provider registration structure
4929 * @get: Get clock callback. Returns NULL or a struct clk for the
4931 * @get_hw: Get clk_hw callback. Returns NULL, ERR_PTR or a
4967 unsigned int idx = clkspec->args[0]; in of_clk_src_onecell_get()
4969 if (idx >= clk_data->clk_num) { in of_clk_src_onecell_get()
4971 return ERR_PTR(-EINVAL); in of_clk_src_onecell_get()
4974 return clk_data->clks[idx]; in of_clk_src_onecell_get()
4982 unsigned int idx = clkspec->args[0]; in of_clk_hw_onecell_get()
4984 if (idx >= hw_data->num) { in of_clk_hw_onecell_get()
4986 return ERR_PTR(-EINVAL); in of_clk_hw_onecell_get()
4989 return hw_data->hws[idx]; in of_clk_hw_onecell_get()
4994 * of_clk_add_provider() - Register a clock provider for a node
5014 return -ENOMEM; in of_clk_add_provider()
5016 cp->node = of_node_get(np); in of_clk_add_provider()
5017 cp->data = data; in of_clk_add_provider()
5018 cp->get = clk_src_get; in of_clk_add_provider()
5021 list_add(&cp->link, &of_clk_providers); in of_clk_add_provider()
5031 fwnode_dev_initialized(&np->fwnode, true); in of_clk_add_provider()
5038 * of_clk_add_hw_provider() - Register a clock provider for a node
5056 return -ENOMEM; in of_clk_add_hw_provider()
5058 cp->node = of_node_get(np); in of_clk_add_hw_provider()
5059 cp->data = data; in of_clk_add_hw_provider()
5060 cp->get_hw = get; in of_clk_add_hw_provider()
5063 list_add(&cp->link, &of_clk_providers); in of_clk_add_hw_provider()
5073 fwnode_dev_initialized(&np->fwnode, true); in of_clk_add_hw_provider()
5085 * We allow a child device to use its parent device as the clock provider node
5086 * for cases like MFD sub-devices where the child device driver wants to use
5087 * devm_*() APIs but not list the device in DT as a sub-node.
5093 np = dev->of_node; in get_clk_provider_node()
5094 parent_np = dev->parent ? dev->parent->of_node : NULL; in get_clk_provider_node()
5096 if (!of_property_present(np, "#clock-cells")) in get_clk_provider_node()
5097 if (of_property_present(parent_np, "#clock-cells")) in get_clk_provider_node()
5104 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
5110 * node or if the device node lacks of clock provider information (#clock-cells)
5112 * has the #clock-cells then it is used in registration. Provider is
5128 return -ENOMEM; in devm_of_clk_add_hw_provider()
5144 * of_clk_del_provider() - Remove a previously registered clock provider
5156 if (cp->node == np) { in of_clk_del_provider()
5157 list_del(&cp->link); in of_clk_del_provider()
5158 fwnode_dev_initialized(&np->fwnode, false); in of_clk_del_provider()
5159 of_node_put(cp->node); in of_clk_del_provider()
5169 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
5175 * Parses a device node's "clocks" and "clock-names" properties to find the
5177 * specifier is placed into @out_args, or an errno is returned when there's a
5178 * parsing error. The @index argument is ignored if @name is non-NULL.
5182 * phandle1: clock-controller@1 {
5183 * #clock-cells = <2>;
5186 * phandle2: clock-controller@2 {
5187 * #clock-cells = <1>;
5190 * clock-consumer@3 {
5192 * clock-names = "name1", "name2";
5195 * To get a device_node for `clock-controller@2' node you may call this
5196 * function a few different ways:
5198 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
5199 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
5200 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
5202 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
5203 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
5204 * the "clock-names" property of @np.
5209 int ret = -ENOENT; in of_parse_clkspec()
5211 /* Walk up the tree of devices looking for a clock property that matches */ in of_parse_clkspec()
5215 * "clock-names" property. If it cannot be found, then index in of_parse_clkspec()
5217 * return -EINVAL. in of_parse_clkspec()
5220 index = of_property_match_string(np, "clock-names", name); in of_parse_clkspec()
5221 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells", in of_parse_clkspec()
5230 * has a "clock-ranges" property, then we can try one of its in of_parse_clkspec()
5233 np = np->parent; in of_parse_clkspec()
5234 if (np && !of_property_present(np, "clock-ranges")) in of_parse_clkspec()
5248 if (provider->get_hw) in __of_clk_get_hw_from_provider()
5249 return provider->get_hw(clkspec, provider->data); in __of_clk_get_hw_from_provider()
5251 clk = provider->get(clkspec, provider->data); in __of_clk_get_hw_from_provider()
5261 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER); in of_clk_get_hw_from_clkspec()
5264 return ERR_PTR(-EINVAL); in of_clk_get_hw_from_clkspec()
5267 if (!of_device_is_available(clkspec->np)) in of_clk_get_hw_from_clkspec()
5268 return ERR_PTR(-ENOENT); in of_clk_get_hw_from_clkspec()
5272 if (provider->node == clkspec->np) { in of_clk_get_hw_from_clkspec()
5284 * of_clk_get_from_provider() - Lookup a clock from a clock provider
5285 * @clkspec: pointer to a clock specifier data structure
5287 * This function looks up a struct clk from the registered list of clock
5288 * providers, an input is a clock specifier data structure as returned
5327 return __of_clk_get(np, index, np->full_name, NULL); in of_clk_get()
5332 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
5336 * This function parses the clocks and clock-names properties,
5343 return ERR_PTR(-ENOENT); in of_clk_get_by_name()
5345 return __of_clk_get(np, 0, np->full_name, name); in of_clk_get_by_name()
5350 * of_clk_get_parent_count() - Count the number of clocks a device node has
5359 count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); in of_clk_get_parent_count()
5377 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, in of_clk_get_parent_name()
5386 * specified into an array offset for the clock-output-names property. in of_clk_get_parent_name()
5388 of_property_for_each_u32(clkspec.np, "clock-indices", pv) { in of_clk_get_parent_name()
5396 /* We went off the end of 'clock-indices' without finding it */ in of_clk_get_parent_name()
5397 if (of_property_present(clkspec.np, "clock-indices") && !found) { in of_clk_get_parent_name()
5402 if (of_property_read_string_index(clkspec.np, "clock-output-names", in of_clk_get_parent_name()
5409 * the clock as long as #clock-cells = 0. in of_clk_get_parent_name()
5414 clk_name = clkspec.np->name; in of_clk_get_parent_name()
5430 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
5457 * This function looks for a parent clock. If there is one, then it
5476 if (PTR_ERR(clk) == -EPROBE_DEFER) in parent_ready()
5492 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
5495 * @flags: pointer to top-level framework flags
5497 * Detects if the clock-critical property exists and, if so, sets the
5501 * bindings, such as the one-clock-per-node style that are outdated.
5515 return -EINVAL; in of_clk_detect_critical()
5517 of_property_for_each_u32(np, "clock-critical", idx) in of_clk_detect_critical()
5525 * of_clk_init() - Scan and init clock providers from the DT
5555 list_del(&clk_provider->node); in of_clk_init()
5556 of_node_put(clk_provider->np); in of_clk_init()
5563 parent->clk_init_cb = match->data; in of_clk_init()
5564 parent->np = of_node_get(np); in of_clk_init()
5565 list_add_tail(&parent->node, &clk_provider_list); in of_clk_init()
5572 if (force || parent_ready(clk_provider->np)) { in of_clk_init()
5575 of_node_set_flag(clk_provider->np, in of_clk_init()
5578 clk_provider->clk_init_cb(clk_provider->np); in of_clk_init()
5579 of_clk_set_defaults(clk_provider->np, true); in of_clk_init()
5581 list_del(&clk_provider->node); in of_clk_init()
5582 of_node_put(clk_provider->np); in of_clk_init()