Lines Matching +full:protected +full:- +full:clocks
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <[email protected]>
4 * Copyright (C) 2011-2012 Linaro Ltd <[email protected]>
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
116 if (!core->rpm_enabled)
119 return pm_runtime_resume_and_get(core->dev);
124 if (!core->rpm_enabled)
127 pm_runtime_put_sync(core->dev);
131 * clk_pm_runtime_get_all() - Runtime "get" all clk provider devices
166 dev_name(failed->dev), failed->name);
186 * clk_pm_runtime_put_all() - Runtime "put" all clk provider devices
202 struct device *dev = core->dev;
205 core->rpm_enabled = true;
208 hlist_add_head(&core->rpm_node, &clk_rpm_list);
234 if (--prepare_refcnt)
274 if (--enable_refcnt) {
284 return core->protect_count;
292 * .is_prepared is optional for clocks that can prepare
295 if (!core->ops->is_prepared)
296 return core->prepare_count;
299 ret = core->ops->is_prepared(core->hw);
311 * .is_enabled is only mandatory for clocks that gate
314 if (!core->ops->is_enabled)
315 return core->enable_count;
327 if (core->rpm_enabled) {
328 pm_runtime_get_noresume(core->dev);
329 if (!pm_runtime_active(core->dev)) {
340 if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent)
341 if (!clk_core_is_enabled(core->parent)) {
346 ret = core->ops->is_enabled(core->hw);
348 if (core->rpm_enabled)
349 pm_runtime_put(core->dev);
358 return !clk ? NULL : clk->core->name;
364 return hw->core->name;
370 return !clk ? NULL : clk->core->hw;
376 return hw->core->num_parents;
382 return hw->core->parent ? hw->core->parent->hw : NULL;
392 if (!strcmp(core->name, name))
395 hlist_for_each_entry(child, &core->children, child_node) {
439 return -ENOENT;
444 return ERR_PTR(-ENOENT);
449 * clk_core_get - Find the clk_core parent of a clk
456 * node's 'clock-names' property or as the 'con_id' matching the device's
461 * clock-controller@c001 that has a clk_init_data::parent_data array
463 * clock-controller@f00abcd without needing to get the globally unique name of
466 * parent: clock-controller@f00abcd {
468 * #clock-cells = <0>;
471 * clock-controller@c001 {
473 * clocks = <&parent>;
474 * clock-names = "xtal";
475 * #clock-cells = <1>;
478 * Returns: -ENOENT when the provider can't be found or the clk doesn't
486 const char *name = core->parents[p_index].fw_name;
487 int index = core->parents[p_index].index;
488 struct clk_hw *hw = ERR_PTR(-ENOENT);
489 struct device *dev = core->dev;
491 struct device_node *np = core->of_node;
512 return hw->core;
517 struct clk_parent_map *entry = &core->parents[index];
520 if (entry->hw) {
521 parent = entry->hw->core;
524 if (PTR_ERR(parent) == -ENOENT && entry->name)
525 parent = clk_core_lookup(entry->name);
534 parent = ERR_PTR(-EPROBE_DEFER);
538 entry->core = parent;
544 if (!core || index >= core->num_parents || !core->parents)
547 if (!core->parents[index].core)
550 return core->parents[index].core;
558 parent = clk_core_get_parent_by_index(hw->core, index);
560 return !parent ? NULL : parent->hw;
566 return !clk ? 0 : clk->core->enable_count;
574 if (!core->num_parents || core->parent)
575 return core->rate;
587 return clk_core_get_rate_nolock(hw->core);
596 return core->accuracy;
601 return hw->core->flags;
607 return clk_core_is_prepared(hw->core);
613 return clk_core_is_enabled(hw->core);
622 return clk_core_is_enabled(clk->core);
630 return abs(now - rate) < abs(best - rate);
648 if (core->parent == parent)
651 for (i = 0; i < core->num_parents; i++) {
675 if (req->min_rate < old_req->min_rate)
676 req->min_rate = old_req->min_rate;
678 if (req->max_rate > old_req->max_rate)
679 req->max_rate = old_req->max_rate;
686 struct clk_core *core = hw->core;
687 struct clk_core *parent = core->parent;
691 if (core->flags & CLK_SET_RATE_PARENT) {
695 req->rate = 0;
700 req->rate);
717 req->best_parent_rate = best;
718 req->rate = best;
727 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
732 if (core->flags & CLK_SET_RATE_NO_REPARENT)
736 num_parents = core->num_parents;
744 if (core->flags & CLK_SET_RATE_PARENT) {
747 clk_core_forward_rate_req(core, req, parent, &parent_req, req->rate);
762 if (mux_is_better_rate(req->rate, parent_rate,
770 return -EINVAL;
772 req->best_parent_hw = best_parent->hw;
773 req->best_parent_rate = best;
774 req->rate = best;
784 return !core ? NULL : core->hw->clk;
795 *min_rate = core->min_rate;
796 *max_rate = core->max_rate;
798 hlist_for_each_entry(clk_user, &core->clks, clks_node)
799 *min_rate = max(*min_rate, clk_user->min_rate);
801 hlist_for_each_entry(clk_user, &core->clks, clks_node)
802 *max_rate = min(*max_rate, clk_user->max_rate);
806 * clk_hw_get_rate_range() - returns the clock rate range for a hw clk
817 clk_core_get_boundaries(hw->core, min_rate, max_rate);
829 if (min_rate > core->max_rate || max_rate < core->min_rate)
832 hlist_for_each_entry(user, &core->clks, clks_node)
833 if (min_rate > user->max_rate || max_rate < user->min_rate)
842 hw->core->min_rate = min_rate;
843 hw->core->max_rate = max_rate;
848 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
856 * Returns: 0 on success, -EERROR value on error
873 * clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn't reparent
882 * Returns: 0 on success, -EERROR value on error
900 if (WARN(core->protect_count == 0,
901 "%s already unprotected\n", core->name))
904 if (--core->protect_count > 0)
907 clk_core_rate_unprotect(core->parent);
917 return -EINVAL;
919 if (core->protect_count == 0)
922 ret = core->protect_count;
923 core->protect_count = 1;
930 * clk_rate_exclusive_put - release exclusivity over clock rate control
935 * clock which could result in a rate change or rate glitch. Exclusive clocks
937 * further up the parent chain of clocks. As a result, clocks up parent chain
958 if (WARN_ON(clk->exclusive_count <= 0))
961 clk_core_rate_unprotect(clk->core);
962 clk->exclusive_count--;
975 if (core->protect_count == 0)
976 clk_core_rate_protect(core->parent);
978 core->protect_count++;
992 core->protect_count = count;
996 * clk_rate_exclusive_get - get exclusivity over the clk rate control
1001 * clock which could result in a rate change or rate glitch. Exclusive clocks
1003 * further up the parent chain of clocks. As a result, clocks up parent chain
1011 * Returns 0 on success, -EERROR otherwise
1019 clk_core_rate_protect(clk->core);
1020 clk->exclusive_count++;
1053 if (WARN(core->prepare_count == 0,
1054 "%s already unprepared\n", core->name))
1057 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
1058 "Unpreparing critical %s\n", core->name))
1061 if (core->flags & CLK_SET_RATE_GATE)
1064 if (--core->prepare_count > 0)
1067 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
1071 if (core->ops->unprepare)
1072 core->ops->unprepare(core->hw);
1075 clk_core_unprepare(core->parent);
1087 * clk_unprepare - undo preparation of a clock source
1102 clk_core_unprepare_lock(clk->core);
1115 if (core->prepare_count == 0) {
1120 ret = clk_core_prepare(core->parent);
1126 if (core->ops->prepare)
1127 ret = core->ops->prepare(core->hw);
1135 core->prepare_count++;
1144 if (core->flags & CLK_SET_RATE_GATE)
1149 clk_core_unprepare(core->parent);
1167 * clk_prepare - prepare a clock source
1176 * Returns 0 on success, -EERROR otherwise.
1183 return clk_core_prepare_lock(clk->core);
1194 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
1197 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
1198 "Disabling critical %s\n", core->name))
1201 if (--core->enable_count > 0)
1206 if (core->ops->disable)
1207 core->ops->disable(core->hw);
1211 clk_core_disable(core->parent);
1224 * clk_disable - gate a clock
1230 * SoC-internal clk which is controlled via simple register writes. In the
1240 clk_core_disable_lock(clk->core);
1253 if (WARN(core->prepare_count == 0,
1254 "Enabling unprepared %s\n", core->name))
1255 return -ESHUTDOWN;
1257 if (core->enable_count == 0) {
1258 ret = clk_core_enable(core->parent);
1265 if (core->ops->enable)
1266 ret = core->ops->enable(core->hw);
1271 clk_core_disable(core->parent);
1276 core->enable_count++;
1293 * clk_gate_restore_context - restore context for poweroff
1297 * the gate clocks based on the enable_count. This is done in cases
1300 * helps restore the state of gate clocks.
1304 struct clk_core *core = hw->core;
1306 if (core->enable_count)
1307 core->ops->enable(hw);
1309 core->ops->disable(hw);
1318 hlist_for_each_entry(child, &core->children, child_node) {
1324 if (core->ops && core->ops->save_context)
1325 ret = core->ops->save_context(core->hw);
1334 if (core->ops && core->ops->restore_context)
1335 core->ops->restore_context(core->hw);
1337 hlist_for_each_entry(child, &core->children, child_node)
1342 * clk_save_context - save clock context for poweroff
1370 * clk_restore_context - restore clock context after poweroff
1388 * clk_enable - ungate a clock
1393 * if the operation will never sleep. One example is a SoC-internal clk which
1397 * must be called before clk_enable. Returns 0 on success, -EERROR
1405 return clk_core_enable_lock(clk->core);
1410 * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it.
1414 * making clk_enable()/clk_disable() no-ops, false otherwise.
1426 return clk && !(clk->core->ops->enable && clk->core->ops->disable);
1457 hlist_for_each_entry(child, &core->children, child_node)
1460 if (core->prepare_count)
1463 if (core->flags & CLK_IGNORE_UNUSED)
1468 if (core->ops->unprepare_unused)
1469 core->ops->unprepare_unused(core->hw);
1470 else if (core->ops->unprepare)
1471 core->ops->unprepare(core->hw);
1483 hlist_for_each_entry(child, &core->children, child_node)
1486 if (core->flags & CLK_OPS_PARENT_ENABLE)
1487 clk_core_prepare_enable(core->parent);
1491 if (core->enable_count)
1494 if (core->flags & CLK_IGNORE_UNUSED)
1498 * some gate clocks have special needs during the disable-unused
1504 if (core->ops->disable_unused)
1505 core->ops->disable_unused(core->hw);
1506 else if (core->ops->disable)
1507 core->ops->disable(core->hw);
1513 if (core->flags & CLK_OPS_PARENT_ENABLE)
1514 clk_core_disable_unprepare(core->parent);
1531 pr_warn("clk: Not disabling unused clocks\n");
1535 pr_info("clk: Disabling unused clocks\n");
1577 * Some clock providers hand-craft their clk_rate_requests and
1584 if (!req->min_rate && !req->max_rate)
1586 __func__, core->name);
1588 req->rate = clamp(req->rate, req->min_rate, req->max_rate);
1592 * - if the provider is not protected at all
1593 * - if the calling consumer is the only one which has exclusivity
1597 req->rate = core->rate;
1598 } else if (core->ops->determine_rate) {
1599 return core->ops->determine_rate(core->hw, req);
1600 } else if (core->ops->round_rate) {
1601 rate = core->ops->round_rate(core->hw, req->rate,
1602 &req->best_parent_rate);
1606 req->rate = rate;
1608 return -EINVAL;
1624 req->max_rate = ULONG_MAX;
1629 req->core = core;
1630 req->rate = rate;
1631 clk_core_get_boundaries(core, &req->min_rate, &req->max_rate);
1633 parent = core->parent;
1635 req->best_parent_hw = parent->hw;
1636 req->best_parent_rate = parent->rate;
1638 req->best_parent_hw = NULL;
1639 req->best_parent_rate = 0;
1644 * clk_hw_init_rate_request - Initializes a clk_rate_request
1659 clk_core_init_rate_req(hw->core, req, rate);
1664 * clk_hw_forward_rate_request - Forwards a clk_rate_request to a clock's parent
1683 clk_core_forward_rate_req(hw->core, old_req,
1684 parent->core, req,
1691 return core->ops->determine_rate || core->ops->round_rate;
1702 req->rate = 0;
1709 if (core->flags & CLK_SET_RATE_PARENT) {
1712 clk_core_forward_rate_req(core, req, core->parent, &parent_req, req->rate);
1716 ret = clk_core_round_rate_nolock(core->parent, &parent_req);
1722 req->best_parent_rate = parent_req.rate;
1723 req->rate = parent_req.rate;
1728 req->rate = core->rate;
1733 * __clk_determine_rate - get the closest rate actually supported by a clock
1742 req->rate = 0;
1746 return clk_core_round_rate_nolock(hw->core, req);
1751 * clk_hw_round_rate() - round the given rate for a hw clk
1770 clk_core_init_rate_req(hw->core, &req, rate);
1774 ret = clk_core_round_rate_nolock(hw->core, &req);
1785 * clk_round_rate - round the given rate for a clk
1803 if (clk->exclusive_count)
1804 clk_core_rate_unprotect(clk->core);
1806 clk_core_init_rate_req(clk->core, &req, rate);
1810 ret = clk_core_round_rate_nolock(clk->core, &req);
1814 if (clk->exclusive_count)
1815 clk_core_rate_protect(clk->core);
1827 * __clk_notify - call clk notifier chain
1833 * Triggers a notifier call chain on the clk rate-change notification
1851 if (cn->clk->core == core) {
1852 cnd.clk = cn->clk;
1853 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1879 if (core->parent)
1880 parent_accuracy = core->parent->accuracy;
1882 if (core->ops->recalc_accuracy)
1883 core->accuracy = core->ops->recalc_accuracy(core->hw,
1886 core->accuracy = parent_accuracy;
1888 hlist_for_each_entry(child, &core->children, child_node)
1894 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1901 * clk_get_accuracy - return the accuracy of clk
1917 accuracy = clk_core_get_accuracy_recalc(clk->core);
1929 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1930 rate = core->ops->recalc_rate(core->hw, parent_rate);
1958 old_rate = core->rate;
1960 if (core->parent)
1961 parent_rate = core->parent->rate;
1963 core->rate = clk_recalc(core, parent_rate);
1965 core->req_rate = core->rate;
1971 if (core->notifier_count && msg)
1972 __clk_notify(core, msg, old_rate, core->rate);
1974 hlist_for_each_entry(child, &core->children, child_node)
1980 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1987 * clk_get_rate - return the rate of clk
2003 rate = clk_core_get_rate_recalc(clk->core);
2016 return -EINVAL;
2018 for (i = 0; i < core->num_parents; i++) {
2020 if (core->parents[i].core == parent)
2024 if (core->parents[i].core)
2028 if (core->parents[i].hw) {
2029 if (core->parents[i].hw == parent->hw)
2041 if (core->parents[i].name &&
2042 !strcmp(parent->name, core->parents[i].name))
2046 if (i == core->num_parents)
2047 return -EINVAL;
2049 core->parents[i].core = parent;
2054 * clk_hw_get_parent_index - return the index of the parent clock
2057 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
2065 return -EINVAL;
2067 return clk_fetch_parent_index(hw->core, parent->core);
2078 core->orphan = is_orphan;
2080 hlist_for_each_entry(child, &core->children, child_node)
2086 bool was_orphan = core->orphan;
2088 hlist_del(&core->child_node);
2091 bool becomes_orphan = new_parent->orphan;
2094 if (new_parent->new_child == core)
2095 new_parent->new_child = NULL;
2097 hlist_add_head(&core->child_node, &new_parent->children);
2102 hlist_add_head(&core->child_node, &clk_orphan_list);
2107 core->parent = new_parent;
2114 struct clk_core *old_parent = core->parent;
2137 if (core->flags & CLK_OPS_PARENT_ENABLE) {
2143 if (core->prepare_count) {
2164 if (core->prepare_count) {
2169 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
2170 if (core->flags & CLK_OPS_PARENT_ENABLE) {
2188 if (parent && core->ops->set_parent)
2189 ret = core->ops->set_parent(core->hw, p_index);
2217 * pre-rate change notifications and returns early if no clks in the
2234 if (core->notifier_count)
2235 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
2239 __func__, core->name, ret);
2243 hlist_for_each_entry(child, &core->children, child_node) {
2258 core->new_rate = new_rate;
2259 core->new_parent = new_parent;
2260 core->new_parent_index = p_index;
2262 core->new_child = NULL;
2263 if (new_parent && new_parent != core->parent)
2264 new_parent->new_child = core;
2266 hlist_for_each_entry(child, &core->children, child_node) {
2267 child->new_rate = clk_recalc(child, new_rate);
2268 clk_calc_subtree(child, child->new_rate, NULL, 0);
2293 parent = old_parent = core->parent;
2295 best_parent_rate = parent->rate;
2315 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
2319 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
2320 /* pass-through clock without adjustable parent */
2321 core->new_rate = core->rate;
2324 /* pass-through clock with adjustable parent */
2326 new_rate = parent->new_rate;
2330 /* some clocks must be gated to change parent */
2332 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
2334 __func__, core->name);
2339 if (parent && core->num_parents > 1) {
2343 __func__, parent->name, core->name);
2348 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
2349 best_parent_rate != parent->rate)
2369 if (core->rate == core->new_rate)
2372 if (core->notifier_count) {
2373 ret = __clk_notify(core, event, core->rate, core->new_rate);
2378 hlist_for_each_entry(child, &core->children, child_node) {
2380 if (child->new_parent && child->new_parent != core)
2387 /* handle the new child who might not be in core->children yet */
2388 if (core->new_child) {
2389 tmp_clk = clk_propagate_rate_change(core->new_child, event);
2411 old_rate = core->rate;
2413 if (core->new_parent) {
2414 parent = core->new_parent;
2415 best_parent_rate = core->new_parent->rate;
2416 } else if (core->parent) {
2417 parent = core->parent;
2418 best_parent_rate = core->parent->rate;
2424 if (core->flags & CLK_SET_RATE_UNGATE) {
2429 if (core->new_parent && core->new_parent != core->parent) {
2430 old_parent = __clk_set_parent_before(core, core->new_parent);
2431 trace_clk_set_parent(core, core->new_parent);
2433 if (core->ops->set_rate_and_parent) {
2435 core->ops->set_rate_and_parent(core->hw, core->new_rate,
2437 core->new_parent_index);
2438 } else if (core->ops->set_parent) {
2439 core->ops->set_parent(core->hw, core->new_parent_index);
2442 trace_clk_set_parent_complete(core, core->new_parent);
2443 __clk_set_parent_after(core, core->new_parent, old_parent);
2446 if (core->flags & CLK_OPS_PARENT_ENABLE)
2449 trace_clk_set_rate(core, core->new_rate);
2451 if (!skip_set_rate && core->ops->set_rate)
2452 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
2454 trace_clk_set_rate_complete(core, core->new_rate);
2456 core->rate = clk_recalc(core, best_parent_rate);
2458 if (core->flags & CLK_SET_RATE_UNGATE) {
2463 if (core->flags & CLK_OPS_PARENT_ENABLE)
2466 if (core->notifier_count && old_rate != core->rate)
2467 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
2469 if (core->flags & CLK_RECALC_NEW_RATES)
2470 (void)clk_calc_new_rates(core, core->new_rate);
2476 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
2478 if (child->new_parent && child->new_parent != core)
2483 /* handle the new child who might not be in core->children yet */
2484 if (core->new_child)
2485 clk_change_rate(core->new_child);
2536 /* fail on a direct rate set of a protected provider */
2538 return -EBUSY;
2543 return -EINVAL;
2553 fail_clk->name);
2555 ret = -EBUSY;
2562 core->req_rate = req_rate;
2570 * clk_set_rate - specify a new rate for clk
2586 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2588 * Returns 0 on success, -EERROR otherwise.
2600 if (clk->exclusive_count)
2601 clk_core_rate_unprotect(clk->core);
2603 ret = clk_core_set_rate_nolock(clk->core, rate);
2605 if (clk->exclusive_count)
2606 clk_core_rate_protect(clk->core);
2615 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2631 * Returns 0 on success, -EERROR otherwise.
2649 ret = clk_core_set_rate_nolock(clk->core, rate);
2651 clk_core_rate_protect(clk->core);
2652 clk->exclusive_count++;
2673 trace_clk_set_rate_range(clk->core, min, max);
2677 __func__, clk->core->name, clk->dev_id, clk->con_id,
2679 return -EINVAL;
2682 if (clk->exclusive_count)
2683 clk_core_rate_unprotect(clk->core);
2686 old_min = clk->min_rate;
2687 old_max = clk->max_rate;
2688 clk->min_rate = min;
2689 clk->max_rate = max;
2691 if (!clk_core_check_boundaries(clk->core, min, max)) {
2692 ret = -EINVAL;
2696 rate = clk->core->req_rate;
2697 if (clk->core->flags & CLK_GET_RATE_NOCACHE)
2698 rate = clk_core_get_rate_recalc(clk->core);
2711 * broken, clock protected, etc) but also because:
2712 * - round_rate() was not favorable and fell on the wrong
2714 * - the determine_rate() callback does not really check for
2718 ret = clk_core_set_rate_nolock(clk->core, rate);
2721 clk->min_rate = old_min;
2722 clk->max_rate = old_max;
2726 if (clk->exclusive_count)
2727 clk_core_rate_protect(clk->core);
2733 * clk_set_rate_range - set a rate range for a clock source
2758 * clk_set_min_rate - set a minimum clock rate for a clock source
2769 trace_clk_set_min_rate(clk->core, rate);
2771 return clk_set_rate_range(clk, rate, clk->max_rate);
2776 * clk_set_max_rate - set a maximum clock rate for a clock source
2787 trace_clk_set_max_rate(clk->core, rate);
2789 return clk_set_rate_range(clk, clk->min_rate, rate);
2794 * clk_get_parent - return the parent of a clk
2797 * Simply returns clk->parent. Returns NULL if clk is NULL.
2807 /* TODO: Create a per-user clk and change callers to call clk_put */
2808 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2819 if (core->num_parents > 1 && core->ops->get_parent)
2820 index = core->ops->get_parent(core->hw);
2838 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2842 * clk_has_parent - check if a clock is a possible parent for another
2853 /* NULL clocks should be nops, so return success if either is NULL. */
2857 return clk_core_has_parent(clk->core, parent->core);
2873 if (core->parent == parent)
2876 /* verify ops for multi-parent clks */
2877 if (core->num_parents > 1 && !core->ops->set_parent)
2878 return -EPERM;
2880 /* check that we are allowed to re-parent if the clock is in use */
2881 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2882 return -EBUSY;
2885 return -EBUSY;
2892 __func__, parent->name, core->name);
2895 p_rate = parent->rate;
2909 /* do the re-parent */
2928 return clk_core_set_parent_nolock(hw->core, parent->core);
2933 * clk_set_parent - switch the parent of a mux clk
2937 * Re-parent clk to use parent as its new input source. If clk is in
2947 * Returns 0 on success, -EERROR otherwise.
2958 if (clk->exclusive_count)
2959 clk_core_rate_unprotect(clk->core);
2961 ret = clk_core_set_parent_nolock(clk->core,
2962 parent ? parent->core : NULL);
2964 if (clk->exclusive_count)
2965 clk_core_rate_protect(clk->core);
2975 int ret = -EINVAL;
2983 return -EBUSY;
2987 if (core->ops->set_phase) {
2988 ret = core->ops->set_phase(core->hw, degrees);
2990 core->phase = degrees;
2999 * clk_set_phase - adjust the phase shift of a clock signal
3004 * degrees. Returns 0 on success, -EERROR otherwise.
3008 * phase locked-loop clock signal generators we may shift phase with
3032 if (clk->exclusive_count)
3033 clk_core_rate_unprotect(clk->core);
3035 ret = clk_core_set_phase_nolock(clk->core, degrees);
3037 if (clk->exclusive_count)
3038 clk_core_rate_protect(clk->core);
3051 if (!core->ops->get_phase)
3055 ret = core->ops->get_phase(core->hw);
3057 core->phase = ret;
3063 * clk_get_phase - return the phase shift of a clock signal
3067 * -EERROR.
3077 ret = clk_core_get_phase(clk->core);
3087 core->duty.num = 1;
3088 core->duty.den = 2;
3095 struct clk_duty *duty = &core->duty;
3098 if (!core->ops->get_duty_cycle)
3101 ret = core->ops->get_duty_cycle(core->hw, duty);
3106 if (duty->den == 0 || duty->num > duty->den) {
3107 ret = -EINVAL;
3122 if (core->parent &&
3123 core->flags & CLK_DUTY_CYCLE_PARENT) {
3124 ret = clk_core_update_duty_cycle_nolock(core->parent);
3125 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
3144 return -EBUSY;
3148 if (!core->ops->set_duty_cycle)
3151 ret = core->ops->set_duty_cycle(core->hw, duty);
3153 memcpy(&core->duty, duty, sizeof(*duty));
3165 if (core->parent &&
3166 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
3167 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
3168 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
3175 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
3195 return -EINVAL;
3202 if (clk->exclusive_count)
3203 clk_core_rate_unprotect(clk->core);
3205 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
3207 if (clk->exclusive_count)
3208 clk_core_rate_protect(clk->core);
3219 struct clk_duty *duty = &core->duty;
3226 ret = mult_frac(scale, duty->num, duty->den);
3234 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
3246 return clk_core_get_scaled_duty_cycle(clk->core, scale);
3251 * clk_is_match - check if two clk's point to the same hardware clock
3267 /* true if clk->core pointers match. Avoid dereferencing garbage */
3269 if (p->core == q->core)
3298 seq_printf(s, "%*s%-*s %-7d %-8d %-8d %-11lu %-10lu ",
3300 35 - level * 3, c->name,
3301 c->enable_count, c->prepare_count, c->protect_count,
3307 seq_printf(s, "%-5d", phase);
3309 seq_puts(s, "-----");
3311 seq_printf(s, " %-6d", clk_core_get_scaled_duty_cycle(c, 100000));
3313 if (c->ops->is_enabled)
3315 else if (!c->ops->enable)
3320 hlist_for_each_entry(clk_user, &c->clks, clks_node) {
3321 seq_printf(s, "%*s%-*s %-25s\n",
3324 clk_user->dev_id ? clk_user->dev_id : "deviceless",
3325 clk_user->con_id ? clk_user->con_id : "no_connection_id");
3339 hlist_for_each_entry(child, &c->children, child_node)
3346 struct hlist_head **lists = s->private;
3351 seq_puts(s, "---------------------------------------------------------------------------------------------------------------------------------------------\n");
3378 seq_printf(s, "\"%s\": { ", c->name);
3379 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
3380 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
3381 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
3399 hlist_for_each_entry(child, &c->children, child_node) {
3411 struct hlist_head **lists = s->private;
3481 ret = clk_prepare_enable(core->hw->clk);
3483 clk_disable_unprepare(core->hw->clk);
3492 *val = core->enable_count && core->prepare_count;
3524 *val = core->phase;
3552 struct clk_core *core = s->private;
3553 unsigned long flags = core->flags;
3583 * 4. Fetch parent clock's clock-output-name if DT index was set
3591 seq_puts(s, parent->name);
3592 } else if (core->parents[i].name) {
3593 seq_puts(s, core->parents[i].name);
3594 } else if (core->parents[i].fw_name) {
3595 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3597 if (core->parents[i].index >= 0)
3598 name = of_clk_get_parent_name(core->of_node, core->parents[i].index);
3610 struct clk_core *core = s->private;
3613 for (i = 0; i < core->num_parents - 1; i++)
3624 struct clk_core *core = s->private;
3626 if (core->parent)
3627 seq_printf(s, "%s\n", core->parent->name);
3637 struct seq_file *s = file->private_data;
3638 struct clk_core *core = s->private;
3649 return -ENOENT;
3671 struct clk_core *core = s->private;
3672 struct clk_duty *duty = &core->duty;
3674 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3682 struct clk_core *core = s->private;
3696 struct clk_core *core = s->private;
3715 root = debugfs_create_dir(core->name, pdentry);
3716 core->dentry = root;
3722 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3726 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3727 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3728 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3729 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
3736 if (core->num_parents > 1)
3741 if (core->num_parents > 0)
3745 if (core->num_parents > 1)
3749 if (core->ops->debug_init)
3750 core->ops->debug_init(core->hw, core->dentry);
3754 * clk_debug_register - add a clk node to the debugfs clk directory
3764 hlist_add_head(&core->debug_node, &clk_debug_list);
3771 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3775 * debugfs clk directory if clk->dentry points to debugfs created by
3781 hlist_del_init(&core->debug_node);
3782 debugfs_remove_recursive(core->dentry);
3783 core->dentry = NULL;
3788 * clk_debug_init - lazily populate the debugfs clk directory
3792 * populates the debugfs clk directory once at boot-time when we know that
3793 * debugfs is setup. It should only be called once at boot-time, all other clks
3852 * walk the list of orphan clocks and reparent any that newly finds a
3861 * clock. This is important for CLK_IS_CRITICAL clocks, which
3879 * 'req_rate' is set to something non-zero so that
3882 orphan->req_rate = orphan->rate;
3888 * __clk_core_init - initialize the data structures in a struct clk_core
3904 * Set hw->core after grabbing the prepare_lock to synchronize with
3905 * callers of clk_core_fill_parent_index() where we treat hw->core
3909 core->hw->core = core;
3916 if (clk_core_lookup(core->name)) {
3918 __func__, core->name);
3919 ret = -EEXIST;
3923 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3924 if (core->ops->set_rate &&
3925 !((core->ops->round_rate || core->ops->determine_rate) &&
3926 core->ops->recalc_rate)) {
3928 __func__, core->name);
3929 ret = -EINVAL;
3933 if (core->ops->set_parent && !core->ops->get_parent) {
3935 __func__, core->name);
3936 ret = -EINVAL;
3940 if (core->ops->set_parent && !core->ops->determine_rate) {
3942 __func__, core->name);
3943 ret = -EINVAL;
3947 if (core->num_parents > 1 && !core->ops->get_parent) {
3949 __func__, core->name);
3950 ret = -EINVAL;
3954 if (core->ops->set_rate_and_parent &&
3955 !(core->ops->set_parent && core->ops->set_rate)) {
3957 __func__, core->name);
3958 ret = -EINVAL;
3963 * optional platform-specific magic
3976 if (core->ops->init) {
3977 ret = core->ops->init(core->hw);
3982 parent = core->parent = __clk_init_parent(core);
3985 * Populate core->parent if parent has already been clk_core_init'd. If
3991 * clocks and re-parent any that are children of the clock currently
3995 hlist_add_head(&core->child_node, &parent->children);
3996 core->orphan = parent->orphan;
3997 } else if (!core->num_parents) {
3998 hlist_add_head(&core->child_node, &clk_root_list);
3999 core->orphan = false;
4001 hlist_add_head(&core->child_node, &clk_orphan_list);
4002 core->orphan = true;
4007 * .recalc_accuracy. For simple clocks and lazy developers the default
4012 if (core->ops->recalc_accuracy)
4013 core->accuracy = core->ops->recalc_accuracy(core->hw,
4016 core->accuracy = parent->accuracy;
4018 core->accuracy = 0;
4029 core->name);
4040 * simple clocks and lazy developers the default fallback is to use the
4044 if (core->ops->recalc_rate)
4045 rate = core->ops->recalc_rate(core->hw,
4048 rate = parent->rate;
4051 core->rate = core->req_rate = rate;
4054 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
4056 * reparenting clocks
4058 if (core->flags & CLK_IS_CRITICAL) {
4062 __func__, core->name);
4069 __func__, core->name);
4080 hlist_del_init(&core->child_node);
4081 core->hw->core = NULL;
4093 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
4100 hlist_add_head(&clk->clks_node, &core->clks);
4105 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
4111 hlist_del(&clk->clks_node);
4115 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
4129 return ERR_PTR(-ENOMEM);
4131 clk->core = core;
4132 clk->dev_id = dev_id;
4133 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
4134 clk->max_rate = ULONG_MAX;
4140 * free_clk - Free a clk consumer
4148 kfree_const(clk->con_id);
4174 core = hw->core;
4178 clk->dev = dev;
4180 if (!try_module_get(core->owner)) {
4182 return ERR_PTR(-ENOENT);
4185 kref_get(&core->ref);
4192 * clk_hw_get_clk - get clk consumer given an clk_hw
4203 struct device *dev = hw->core->dev;
4216 return -EINVAL;
4222 return -ENOMEM;
4230 u8 num_parents = init->num_parents;
4231 const char * const *parent_names = init->parent_names;
4232 const struct clk_hw **parent_hws = init->parent_hws;
4233 const struct clk_parent_data *parent_data = init->parent_data;
4241 * Avoid unnecessary string look-ups of clk_core's possible parents by
4245 core->parents = parents;
4247 return -ENOMEM;
4251 parent->index = -1;
4256 __func__, core->name);
4257 ret = clk_cpy_name(&parent->name, parent_names[i],
4260 parent->hw = parent_data[i].hw;
4261 parent->index = parent_data[i].index;
4262 ret = clk_cpy_name(&parent->fw_name,
4265 ret = clk_cpy_name(&parent->name,
4269 parent->hw = parent_hws[i];
4271 ret = -EINVAL;
4279 } while (--i >= 0);
4291 int i = core->num_parents;
4293 if (!core->num_parents)
4296 while (--i >= 0) {
4297 kfree_const(core->parents[i].name);
4298 kfree_const(core->parents[i].fw_name);
4301 kfree(core->parents);
4309 if (core->rpm_enabled) {
4311 hlist_del(&core->rpm_node);
4316 kfree_const(core->name);
4325 const struct clk_init_data *init = hw->init;
4330 * we catch use of hw->init early on in the core.
4332 hw->init = NULL;
4336 ret = -ENOMEM;
4340 kref_init(&core->ref);
4342 core->name = kstrdup_const(init->name, GFP_KERNEL);
4343 if (!core->name) {
4344 ret = -ENOMEM;
4348 if (WARN_ON(!init->ops)) {
4349 ret = -EINVAL;
4352 core->ops = init->ops;
4354 core->dev = dev;
4356 core->of_node = np;
4357 if (dev && dev->driver)
4358 core->owner = dev->driver->owner;
4359 core->hw = hw;
4360 core->flags = init->flags;
4361 core->num_parents = init->num_parents;
4362 core->min_rate = 0;
4363 core->max_rate = ULONG_MAX;
4369 INIT_HLIST_HEAD(&core->clks);
4375 hw->clk = alloc_clk(core, NULL, NULL);
4376 if (IS_ERR(hw->clk)) {
4377 ret = PTR_ERR(hw->clk);
4381 clk_core_link_consumer(core, hw->clk);
4385 return hw->clk;
4388 clk_core_unlink_consumer(hw->clk);
4391 free_clk(hw->clk);
4392 hw->clk = NULL;
4398 kref_put(&core->ref, __clk_release);
4404 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
4408 * @dev->parent if dev doesn't have a device node, or NULL if neither
4409 * @dev or @dev->parent have a device node.
4420 np = dev_of_node(dev->parent);
4426 * clk_register - allocate a new clock, register it and return an opaque cookie
4428 * @hw: link to hardware-specific clock data
4445 * clk_hw_register - register a clk_hw and return an error code
4447 * @hw: link to hardware-specific clock data
4462 * of_clk_hw_register - register a clk_hw and return an error code
4464 * @hw: link to hardware-specific clock data
4479 * Empty clk_ops for unregistered clocks. These are used temporarily
4485 return -ENXIO;
4496 return -ENXIO;
4501 return -ENXIO;
4507 return -ENXIO;
4526 for (i = 0; i < root->num_parents; i++)
4527 if (root->parents[i].core == target)
4528 root->parents[i].core = NULL;
4530 hlist_for_each_entry(child, &root->children, child_node)
4549 * clk_unregister - unregister a currently registered clock
4560 clk_debug_unregister(clk->core);
4564 ops = clk->core->ops;
4567 clk->core->name);
4576 clk->core->ops = &clk_nodrv_ops;
4579 if (ops->terminate)
4580 ops->terminate(clk->core->hw);
4582 if (!hlist_empty(&clk->core->children)) {
4587 hlist_for_each_entry_safe(child, t, &clk->core->children,
4592 clk_core_evict_parent_cache(clk->core);
4594 hlist_del_init(&clk->core->child_node);
4596 if (clk->core->prepare_count)
4598 __func__, clk->core->name);
4600 if (clk->core->protect_count)
4601 pr_warn("%s: unregistering protected clock: %s\n",
4602 __func__, clk->core->name);
4605 kref_put(&clk->core->ref, __clk_release);
4611 * clk_hw_unregister - unregister a currently registered clk_hw
4612 * @hw: hardware-specific clock data to unregister
4616 clk_unregister(hw->clk);
4631 * devm_clk_register - resource managed clk_register()
4633 * @hw: link to hardware-specific clock data
4637 * Clocks returned from this function are automatically clk_unregister()ed on
4647 return ERR_PTR(-ENOMEM);
4662 * devm_clk_hw_register - resource managed clk_hw_register()
4664 * @hw: link to hardware-specific clock data
4666 * Managed clk_hw_register(). Clocks registered by this function are
4677 return -ENOMEM;
4697 * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
4702 * Managed clk_hw_get_clk(). Clocks got with this function are
4716 WARN_ON_ONCE(dev != hw->core->dev);
4720 return ERR_PTR(-ENOMEM);
4752 if (WARN_ON(clk->exclusive_count)) {
4754 clk->core->protect_count -= (clk->exclusive_count - 1);
4755 clk_core_rate_unprotect(clk->core);
4756 clk->exclusive_count = 0;
4762 if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX)
4767 owner = clk->core->owner;
4768 kref_put(&clk->core->ref, __clk_release);
4776 * clk_notifier_register - add a clk rate change notifier
4783 * re-enter into the clk framework by calling any top-level clk APIs;
4790 * clk_notifier_register() must be called from non-atomic context.
4791 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4798 int ret = -ENOMEM;
4801 return -EINVAL;
4807 if (cn->clk == clk)
4815 cn->clk = clk;
4816 srcu_init_notifier_head(&cn->notifier_head);
4818 list_add(&cn->node, &clk_notifier_list);
4821 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4823 clk->core->notifier_count++;
4833 * clk_notifier_unregister - remove a clk rate change notifier
4840 * Returns -EINVAL if called with null arguments; otherwise, passes
4846 int ret = -ENOENT;
4849 return -EINVAL;
4854 if (cn->clk == clk) {
4855 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4857 clk->core->notifier_count--;
4860 if (!cn->notifier_head.head) {
4861 srcu_cleanup_notifier_head(&cn->notifier_head);
4862 list_del(&cn->node);
4884 clk_notifier_unregister(devres->clk, devres->nb);
4897 return -ENOMEM;
4901 devres->clk = clk;
4902 devres->nb = nb;
4921 * struct of_clk_provider - Clock provider registration structure
4962 unsigned int idx = clkspec->args[0];
4964 if (idx >= clk_data->clk_num) {
4966 return ERR_PTR(-EINVAL);
4969 return clk_data->clks[idx];
4977 unsigned int idx = clkspec->args[0];
4979 if (idx >= hw_data->num) {
4981 return ERR_PTR(-EINVAL);
4984 return hw_data->hws[idx];
4989 * of_clk_add_provider() - Register a clock provider for a node
5009 return -ENOMEM;
5011 cp->node = of_node_get(np);
5012 cp->data = data;
5013 cp->get = clk_src_get;
5016 list_add(&cp->link, &of_clk_providers);
5026 fwnode_dev_initialized(&np->fwnode, true);
5033 * of_clk_add_hw_provider() - Register a clock provider for a node
5051 return -ENOMEM;
5053 cp->node = of_node_get(np);
5054 cp->data = data;
5055 cp->get_hw = get;
5058 list_add(&cp->link, &of_clk_providers);
5068 fwnode_dev_initialized(&np->fwnode, true);
5081 * for cases like MFD sub-devices where the child device driver wants to use
5082 * devm_*() APIs but not list the device in DT as a sub-node.
5088 np = dev->of_node;
5089 parent_np = dev->parent ? dev->parent->of_node : NULL;
5091 if (!of_property_present(np, "#clock-cells"))
5092 if (of_property_present(parent_np, "#clock-cells"))
5099 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
5105 * node or if the device node lacks of clock provider information (#clock-cells)
5107 * has the #clock-cells then it is used in registration. Provider is
5123 return -ENOMEM;
5139 * of_clk_del_provider() - Remove a previously registered clock provider
5151 if (cp->node == np) {
5152 list_del(&cp->link);
5153 fwnode_dev_initialized(&np->fwnode, false);
5154 of_node_put(cp->node);
5164 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
5170 * Parses a device node's "clocks" and "clock-names" properties to find the
5173 * parsing error. The @index argument is ignored if @name is non-NULL.
5177 * phandle1: clock-controller@1 {
5178 * #clock-cells = <2>;
5181 * phandle2: clock-controller@2 {
5182 * #clock-cells = <1>;
5185 * clock-consumer@3 {
5186 * clocks = <&phandle1 1 2 &phandle2 3>;
5187 * clock-names = "name1", "name2";
5190 * To get a device_node for `clock-controller@2' node you may call this
5193 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
5194 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
5195 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
5197 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
5198 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
5199 * the "clock-names" property of @np.
5204 int ret = -ENOENT;
5209 * For named clocks, first look up the name in the
5210 * "clock-names" property. If it cannot be found, then index
5212 * return -EINVAL.
5215 index = of_property_match_string(np, "clock-names", name);
5216 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
5225 * has a "clock-ranges" property, then we can try one of its
5226 * clocks.
5228 np = np->parent;
5229 if (np && !of_property_present(np, "clock-ranges"))
5243 if (provider->get_hw)
5244 return provider->get_hw(clkspec, provider->data);
5246 clk = provider->get(clkspec, provider->data);
5256 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
5259 return ERR_PTR(-EINVAL);
5263 if (provider->node == clkspec->np) {
5275 * of_clk_get_from_provider() - Lookup a clock from a clock provider
5318 return __of_clk_get(np, index, np->full_name, NULL);
5323 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
5327 * This function parses the clocks and clock-names properties,
5334 return ERR_PTR(-ENOENT);
5336 return __of_clk_get(np, 0, np->full_name, name);
5341 * of_clk_get_parent_count() - Count the number of clocks a device node has
5344 * Returns: The number of clocks that are possible parents of this node
5350 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
5368 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
5377 * specified into an array offset for the clock-output-names property.
5379 of_property_for_each_u32(clkspec.np, "clock-indices", pv) {
5387 /* We went off the end of 'clock-indices' without finding it */
5388 if (of_property_present(clkspec.np, "clock-indices") && !found) {
5393 if (of_property_read_string_index(clkspec.np, "clock-output-names",
5400 * the clock as long as #clock-cells = 0.
5405 clk_name = clkspec.np->name;
5421 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
5467 if (PTR_ERR(clk) == -EPROBE_DEFER)
5483 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
5486 * @flags: pointer to top-level framework flags
5488 * Detects if the clock-critical property exists and, if so, sets the
5492 * bindings, such as the one-clock-per-node style that are outdated.
5506 return -EINVAL;
5508 of_property_for_each_u32(np, "clock-critical", idx)
5516 * of_clk_init() - Scan and init clock providers from the DT
5535 /* First prepare the list of the clocks providers */
5546 list_del(&clk_provider->node);
5547 of_node_put(clk_provider->np);
5554 parent->clk_init_cb = match->data;
5555 parent->np = of_node_get(np);
5556 list_add_tail(&parent->node, &clk_provider_list);
5563 if (force || parent_ready(clk_provider->np)) {
5566 of_node_set_flag(clk_provider->np,
5569 clk_provider->clk_init_cb(clk_provider->np);
5570 of_clk_set_defaults(clk_provider->np, true);
5572 list_del(&clk_provider->node);
5573 of_node_put(clk_provider->np);