Lines Matching +full:clk +full:- +full:phase +full:-
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
25 #include "clk.h"
81 int phase; member
95 #include <trace/events/clk.h>
97 struct clk { struct
111 if (!core->rpm_enabled) in clk_pm_runtime_get()
114 return pm_runtime_resume_and_get(core->dev); in clk_pm_runtime_get()
119 if (!core->rpm_enabled) in clk_pm_runtime_put()
122 pm_runtime_put_sync(core->dev); in clk_pm_runtime_put()
146 if (--prepare_refcnt) in clk_prepare_unlock()
186 if (--enable_refcnt) { in clk_enable_unlock()
196 return core->protect_count; in clk_core_rate_is_protected()
207 if (!core->ops->is_prepared) in clk_core_is_prepared()
208 return core->prepare_count; in clk_core_is_prepared()
211 ret = core->ops->is_prepared(core->hw); in clk_core_is_prepared()
226 if (!core->ops->is_enabled) in clk_core_is_enabled()
227 return core->enable_count; in clk_core_is_enabled()
239 if (core->rpm_enabled) { in clk_core_is_enabled()
240 pm_runtime_get_noresume(core->dev); in clk_core_is_enabled()
241 if (!pm_runtime_active(core->dev)) { in clk_core_is_enabled()
252 if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent) in clk_core_is_enabled()
253 if (!clk_core_is_enabled(core->parent)) { in clk_core_is_enabled()
258 ret = core->ops->is_enabled(core->hw); in clk_core_is_enabled()
260 if (core->rpm_enabled) in clk_core_is_enabled()
261 pm_runtime_put(core->dev); in clk_core_is_enabled()
268 const char *__clk_get_name(const struct clk *clk) in __clk_get_name() argument
270 return !clk ? NULL : clk->core->name; in __clk_get_name()
276 return hw->core->name; in clk_hw_get_name()
280 struct clk_hw *__clk_get_hw(struct clk *clk) in __clk_get_hw() argument
282 return !clk ? NULL : clk->core->hw; in __clk_get_hw()
288 return hw->core->num_parents; in clk_hw_get_num_parents()
294 return hw->core->parent ? hw->core->parent->hw : NULL; in clk_hw_get_parent()
304 if (!strcmp(core->name, name)) in __clk_lookup_subtree()
307 hlist_for_each_entry(child, &core->children, child_node) { in __clk_lookup_subtree()
324 /* search the 'proper' clk tree first */ in clk_core_lookup()
351 return -ENOENT; in of_parse_clkspec()
356 return ERR_PTR(-ENOENT); in of_clk_get_hw_from_clkspec()
361 * clk_core_get - Find the clk_core parent of a clk
362 * @core: clk to find parent of
365 * This is the preferred method for clk providers to find the parent of a
366 * clk when that parent is external to the clk controller. The parent_names
368 * node's 'clock-names' property or as the 'con_id' matching the device's
369 * dev_name() in a clk_lookup. This allows clk providers to use their own
373 * clock-controller@c001 that has a clk_init_data::parent_data array
375 * clock-controller@f00abcd without needing to get the globally unique name of
376 * the xtal clk.
378 * parent: clock-controller@f00abcd {
380 * #clock-cells = <0>;
383 * clock-controller@c001 {
386 * clock-names = "xtal";
387 * #clock-cells = <1>;
390 * Returns: -ENOENT when the provider can't be found or the clk doesn't
392 * in a clkdev lookup. NULL when the provider knows about the clk but it
394 * A valid clk_core pointer when the clk can be found in the provider.
398 const char *name = core->parents[p_index].fw_name; in clk_core_get()
399 int index = core->parents[p_index].index; in clk_core_get()
400 struct clk_hw *hw = ERR_PTR(-ENOENT); in clk_core_get()
401 struct device *dev = core->dev; in clk_core_get()
403 struct device_node *np = core->of_node; in clk_core_get()
421 return hw->core; in clk_core_get()
426 struct clk_parent_map *entry = &core->parents[index]; in clk_core_fill_parent_index()
429 if (entry->hw) { in clk_core_fill_parent_index()
430 parent = entry->hw->core; in clk_core_fill_parent_index()
433 if (PTR_ERR(parent) == -ENOENT && entry->name) in clk_core_fill_parent_index()
434 parent = clk_core_lookup(entry->name); in clk_core_fill_parent_index()
443 parent = ERR_PTR(-EPROBE_DEFER); in clk_core_fill_parent_index()
447 entry->core = parent; in clk_core_fill_parent_index()
453 if (!core || index >= core->num_parents || !core->parents) in clk_core_get_parent_by_index()
456 if (!core->parents[index].core) in clk_core_get_parent_by_index()
459 return core->parents[index].core; in clk_core_get_parent_by_index()
467 parent = clk_core_get_parent_by_index(hw->core, index); in clk_hw_get_parent_by_index()
469 return !parent ? NULL : parent->hw; in clk_hw_get_parent_by_index()
473 unsigned int __clk_get_enable_count(struct clk *clk) in __clk_get_enable_count() argument
475 return !clk ? 0 : clk->core->enable_count; in __clk_get_enable_count()
483 if (!core->num_parents || core->parent) in clk_core_get_rate_nolock()
484 return core->rate; in clk_core_get_rate_nolock()
487 * Clk must have a parent because num_parents > 0 but the parent isn't in clk_core_get_rate_nolock()
488 * known yet. Best to return 0 as the rate of this clk until we can in clk_core_get_rate_nolock()
496 return clk_core_get_rate_nolock(hw->core); in clk_hw_get_rate()
505 return core->accuracy; in clk_core_get_accuracy_no_lock()
510 return hw->core->flags; in clk_hw_get_flags()
516 return clk_core_is_prepared(hw->core); in clk_hw_is_prepared()
522 return clk_core_rate_is_protected(hw->core); in clk_hw_rate_is_protected()
528 return clk_core_is_enabled(hw->core); in clk_hw_is_enabled()
532 bool __clk_is_enabled(struct clk *clk) in __clk_is_enabled() argument
534 if (!clk) in __clk_is_enabled()
537 return clk_core_is_enabled(clk->core); in __clk_is_enabled()
545 return abs(now - rate) < abs(best - rate); in mux_is_better_rate()
563 if (core->parent == parent) in clk_core_has_parent()
566 for (i = 0; i < core->num_parents; i++) { in clk_core_has_parent()
590 if (req->min_rate < old_req->min_rate) in clk_core_forward_rate_req()
591 req->min_rate = old_req->min_rate; in clk_core_forward_rate_req()
593 if (req->max_rate > old_req->max_rate) in clk_core_forward_rate_req()
594 req->max_rate = old_req->max_rate; in clk_core_forward_rate_req()
601 struct clk_core *core = hw->core; in clk_core_determine_rate_no_reparent()
602 struct clk_core *parent = core->parent; in clk_core_determine_rate_no_reparent()
606 if (core->flags & CLK_SET_RATE_PARENT) { in clk_core_determine_rate_no_reparent()
610 req->rate = 0; in clk_core_determine_rate_no_reparent()
615 req->rate); in clk_core_determine_rate_no_reparent()
632 req->best_parent_rate = best; in clk_core_determine_rate_no_reparent()
633 req->rate = best; in clk_core_determine_rate_no_reparent()
642 struct clk_core *core = hw->core, *parent, *best_parent = NULL; in clk_mux_determine_rate_flags()
647 if (core->flags & CLK_SET_RATE_NO_REPARENT) in clk_mux_determine_rate_flags()
651 num_parents = core->num_parents; in clk_mux_determine_rate_flags()
659 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
662 clk_core_forward_rate_req(core, req, parent, &parent_req, req->rate); in clk_mux_determine_rate_flags()
677 if (mux_is_better_rate(req->rate, parent_rate, in clk_mux_determine_rate_flags()
685 return -EINVAL; in clk_mux_determine_rate_flags()
687 req->best_parent_hw = best_parent->hw; in clk_mux_determine_rate_flags()
688 req->best_parent_rate = best; in clk_mux_determine_rate_flags()
689 req->rate = best; in clk_mux_determine_rate_flags()
695 struct clk *__clk_lookup(const char *name) in __clk_lookup()
699 return !core ? NULL : core->hw->clk; in __clk_lookup()
706 struct clk *clk_user; in clk_core_get_boundaries()
710 *min_rate = core->min_rate; in clk_core_get_boundaries()
711 *max_rate = core->max_rate; in clk_core_get_boundaries()
713 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
714 *min_rate = max(*min_rate, clk_user->min_rate); in clk_core_get_boundaries()
716 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
717 *max_rate = min(*max_rate, clk_user->max_rate); in clk_core_get_boundaries()
721 * clk_hw_get_rate_range() - returns the clock rate range for a hw clk
722 * @hw: the hw clk we want to get the range from
732 clk_core_get_boundaries(hw->core, min_rate, max_rate); in clk_hw_get_rate_range()
740 struct clk *user; in clk_core_check_boundaries()
744 if (min_rate > core->max_rate || max_rate < core->min_rate) in clk_core_check_boundaries()
747 hlist_for_each_entry(user, &core->clks, clks_node) in clk_core_check_boundaries()
748 if (min_rate > user->max_rate || max_rate < user->min_rate) in clk_core_check_boundaries()
757 hw->core->min_rate = min_rate; in clk_hw_set_rate_range()
758 hw->core->max_rate = max_rate; in clk_hw_set_rate_range()
763 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
764 * @hw: mux type clk to determine rate on
771 * Returns: 0 on success, -EERROR value on error
788 …* clk_hw_determine_rate_no_reparent - clk_ops::determine_rate implementation for a clk that doesn'…
789 * @hw: mux type clk to determine rate on
797 * Returns: 0 on success, -EERROR value on error
806 /*** clk api ***/
815 if (WARN(core->protect_count == 0, in clk_core_rate_unprotect()
816 "%s already unprotected\n", core->name)) in clk_core_rate_unprotect()
819 if (--core->protect_count > 0) in clk_core_rate_unprotect()
822 clk_core_rate_unprotect(core->parent); in clk_core_rate_unprotect()
832 return -EINVAL; in clk_core_rate_nuke_protect()
834 if (core->protect_count == 0) in clk_core_rate_nuke_protect()
837 ret = core->protect_count; in clk_core_rate_nuke_protect()
838 core->protect_count = 1; in clk_core_rate_nuke_protect()
845 * clk_rate_exclusive_put - release exclusivity over clock rate control
846 * @clk: the clk over which the exclusivity is released
862 void clk_rate_exclusive_put(struct clk *clk) in clk_rate_exclusive_put() argument
864 if (!clk) in clk_rate_exclusive_put()
873 if (WARN_ON(clk->exclusive_count <= 0)) in clk_rate_exclusive_put()
876 clk_core_rate_unprotect(clk->core); in clk_rate_exclusive_put()
877 clk->exclusive_count--; in clk_rate_exclusive_put()
890 if (core->protect_count == 0) in clk_core_rate_protect()
891 clk_core_rate_protect(core->parent); in clk_core_rate_protect()
893 core->protect_count++; in clk_core_rate_protect()
907 core->protect_count = count; in clk_core_rate_restore_protect()
911 * clk_rate_exclusive_get - get exclusivity over the clk rate control
912 * @clk: the clk over which the exclusity of rate control is requested
926 * Returns 0 on success, -EERROR otherwise
928 int clk_rate_exclusive_get(struct clk *clk) in clk_rate_exclusive_get() argument
930 if (!clk) in clk_rate_exclusive_get()
934 clk_core_rate_protect(clk->core); in clk_rate_exclusive_get()
935 clk->exclusive_count++; in clk_rate_exclusive_get()
949 if (WARN(core->prepare_count == 0, in clk_core_unprepare()
950 "%s already unprepared\n", core->name)) in clk_core_unprepare()
953 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_unprepare()
954 "Unpreparing critical %s\n", core->name)) in clk_core_unprepare()
957 if (core->flags & CLK_SET_RATE_GATE) in clk_core_unprepare()
960 if (--core->prepare_count > 0) in clk_core_unprepare()
963 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name); in clk_core_unprepare()
967 if (core->ops->unprepare) in clk_core_unprepare()
968 core->ops->unprepare(core->hw); in clk_core_unprepare()
971 clk_core_unprepare(core->parent); in clk_core_unprepare()
983 * clk_unprepare - undo preparation of a clock source
984 * @clk: the clk being unprepared
987 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
988 * if the operation may sleep. One example is a clk which is accessed over
989 * I2c. In the complex case a clk gate operation may require a fast and a slow
993 void clk_unprepare(struct clk *clk) in clk_unprepare() argument
995 if (IS_ERR_OR_NULL(clk)) in clk_unprepare()
998 clk_core_unprepare_lock(clk->core); in clk_unprepare()
1011 if (core->prepare_count == 0) { in clk_core_prepare()
1016 ret = clk_core_prepare(core->parent); in clk_core_prepare()
1022 if (core->ops->prepare) in clk_core_prepare()
1023 ret = core->ops->prepare(core->hw); in clk_core_prepare()
1031 core->prepare_count++; in clk_core_prepare()
1040 if (core->flags & CLK_SET_RATE_GATE) in clk_core_prepare()
1045 clk_core_unprepare(core->parent); in clk_core_prepare()
1063 * clk_prepare - prepare a clock source
1064 * @clk: the clk being prepared
1067 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
1068 * operation may sleep. One example is a clk which is accessed over I2c. In
1069 * the complex case a clk ungate operation may require a fast and a slow part.
1072 * Returns 0 on success, -EERROR otherwise.
1074 int clk_prepare(struct clk *clk) in clk_prepare() argument
1076 if (!clk) in clk_prepare()
1079 return clk_core_prepare_lock(clk->core); in clk_prepare()
1090 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name)) in clk_core_disable()
1093 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_disable()
1094 "Disabling critical %s\n", core->name)) in clk_core_disable()
1097 if (--core->enable_count > 0) in clk_core_disable()
1102 if (core->ops->disable) in clk_core_disable()
1103 core->ops->disable(core->hw); in clk_core_disable()
1107 clk_core_disable(core->parent); in clk_core_disable()
1120 * clk_disable - gate a clock
1121 * @clk: the clk being gated
1125 * clk if the operation is fast and will never sleep. One example is a
1126 * SoC-internal clk which is controlled via simple register writes. In the
1127 * complex case a clk gate operation may require a fast and a slow part. It is
1131 void clk_disable(struct clk *clk) in clk_disable() argument
1133 if (IS_ERR_OR_NULL(clk)) in clk_disable()
1136 clk_core_disable_lock(clk->core); in clk_disable()
1149 if (WARN(core->prepare_count == 0, in clk_core_enable()
1150 "Enabling unprepared %s\n", core->name)) in clk_core_enable()
1151 return -ESHUTDOWN; in clk_core_enable()
1153 if (core->enable_count == 0) { in clk_core_enable()
1154 ret = clk_core_enable(core->parent); in clk_core_enable()
1161 if (core->ops->enable) in clk_core_enable()
1162 ret = core->ops->enable(core->hw); in clk_core_enable()
1167 clk_core_disable(core->parent); in clk_core_enable()
1172 core->enable_count++; in clk_core_enable()
1189 * clk_gate_restore_context - restore context for poweroff
1200 struct clk_core *core = hw->core; in clk_gate_restore_context()
1202 if (core->enable_count) in clk_gate_restore_context()
1203 core->ops->enable(hw); in clk_gate_restore_context()
1205 core->ops->disable(hw); in clk_gate_restore_context()
1214 hlist_for_each_entry(child, &core->children, child_node) { in clk_core_save_context()
1220 if (core->ops && core->ops->save_context) in clk_core_save_context()
1221 ret = core->ops->save_context(core->hw); in clk_core_save_context()
1230 if (core->ops && core->ops->restore_context) in clk_core_restore_context()
1231 core->ops->restore_context(core->hw); in clk_core_restore_context()
1233 hlist_for_each_entry(child, &core->children, child_node) in clk_core_restore_context()
1238 * clk_save_context - save clock context for poweroff
1246 struct clk_core *clk; in clk_save_context() local
1249 hlist_for_each_entry(clk, &clk_root_list, child_node) { in clk_save_context()
1250 ret = clk_core_save_context(clk); in clk_save_context()
1255 hlist_for_each_entry(clk, &clk_orphan_list, child_node) { in clk_save_context()
1256 ret = clk_core_save_context(clk); in clk_save_context()
1266 * clk_restore_context - restore clock context after poweroff
1284 * clk_enable - ungate a clock
1285 * @clk: the clk being ungated
1288 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1289 * if the operation will never sleep. One example is a SoC-internal clk which
1290 * is controlled via simple register writes. In the complex case a clk ungate
1293 * must be called before clk_enable. Returns 0 on success, -EERROR
1296 int clk_enable(struct clk *clk) in clk_enable() argument
1298 if (!clk) in clk_enable()
1301 return clk_core_enable_lock(clk->core); in clk_enable()
1306 * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it.
1307 * @clk: clock source
1310 * making clk_enable()/clk_disable() no-ops, false otherwise.
1320 bool clk_is_enabled_when_prepared(struct clk *clk) in clk_is_enabled_when_prepared() argument
1322 return clk && !(clk->core->ops->enable && clk->core->ops->disable); in clk_is_enabled_when_prepared()
1353 hlist_for_each_entry(child, &core->children, child_node) in clk_unprepare_unused_subtree()
1356 if (core->prepare_count) in clk_unprepare_unused_subtree()
1359 if (core->flags & CLK_IGNORE_UNUSED) in clk_unprepare_unused_subtree()
1367 if (core->ops->unprepare_unused) in clk_unprepare_unused_subtree()
1368 core->ops->unprepare_unused(core->hw); in clk_unprepare_unused_subtree()
1369 else if (core->ops->unprepare) in clk_unprepare_unused_subtree()
1370 core->ops->unprepare(core->hw); in clk_unprepare_unused_subtree()
1384 hlist_for_each_entry(child, &core->children, child_node) in clk_disable_unused_subtree()
1387 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1388 clk_core_prepare_enable(core->parent); in clk_disable_unused_subtree()
1395 if (core->enable_count) in clk_disable_unused_subtree()
1398 if (core->flags & CLK_IGNORE_UNUSED) in clk_disable_unused_subtree()
1402 * some gate clocks have special needs during the disable-unused in clk_disable_unused_subtree()
1408 if (core->ops->disable_unused) in clk_disable_unused_subtree()
1409 core->ops->disable_unused(core->hw); in clk_disable_unused_subtree()
1410 else if (core->ops->disable) in clk_disable_unused_subtree()
1411 core->ops->disable(core->hw); in clk_disable_unused_subtree()
1419 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1420 clk_core_disable_unprepare(core->parent); in clk_disable_unused_subtree()
1436 pr_warn("clk: Not disabling unused clocks\n"); in clk_disable_unused()
1440 pr_info("clk: Disabling unused clocks\n"); in clk_disable_unused()
1473 * Some clock providers hand-craft their clk_rate_requests and in clk_core_determine_round_nolock()
1480 if (!req->min_rate && !req->max_rate) in clk_core_determine_round_nolock()
1482 __func__, core->name); in clk_core_determine_round_nolock()
1484 req->rate = clamp(req->rate, req->min_rate, req->max_rate); in clk_core_determine_round_nolock()
1488 * - if the provider is not protected at all in clk_core_determine_round_nolock()
1489 * - if the calling consumer is the only one which has exclusivity in clk_core_determine_round_nolock()
1493 req->rate = core->rate; in clk_core_determine_round_nolock()
1494 } else if (core->ops->determine_rate) { in clk_core_determine_round_nolock()
1495 return core->ops->determine_rate(core->hw, req); in clk_core_determine_round_nolock()
1496 } else if (core->ops->round_rate) { in clk_core_determine_round_nolock()
1497 rate = core->ops->round_rate(core->hw, req->rate, in clk_core_determine_round_nolock()
1498 &req->best_parent_rate); in clk_core_determine_round_nolock()
1502 req->rate = rate; in clk_core_determine_round_nolock()
1504 return -EINVAL; in clk_core_determine_round_nolock()
1520 req->max_rate = ULONG_MAX; in clk_core_init_rate_req()
1525 req->core = core; in clk_core_init_rate_req()
1526 req->rate = rate; in clk_core_init_rate_req()
1527 clk_core_get_boundaries(core, &req->min_rate, &req->max_rate); in clk_core_init_rate_req()
1529 parent = core->parent; in clk_core_init_rate_req()
1531 req->best_parent_hw = parent->hw; in clk_core_init_rate_req()
1532 req->best_parent_rate = parent->rate; in clk_core_init_rate_req()
1534 req->best_parent_hw = NULL; in clk_core_init_rate_req()
1535 req->best_parent_rate = 0; in clk_core_init_rate_req()
1540 * clk_hw_init_rate_request - Initializes a clk_rate_request
1541 * @hw: the clk for which we want to submit a rate request
1555 clk_core_init_rate_req(hw->core, req, rate); in clk_hw_init_rate_request()
1560 * clk_hw_forward_rate_request - Forwards a clk_rate_request to a clock's parent
1563 * @parent: the clk we want to forward @old_req to
1579 clk_core_forward_rate_req(hw->core, old_req, in clk_hw_forward_rate_request()
1580 parent->core, req, in clk_hw_forward_rate_request()
1587 return core->ops->determine_rate || core->ops->round_rate; in clk_core_can_round()
1598 req->rate = 0; in clk_core_round_rate_nolock()
1605 if (core->flags & CLK_SET_RATE_PARENT) { in clk_core_round_rate_nolock()
1608 clk_core_forward_rate_req(core, req, core->parent, &parent_req, req->rate); in clk_core_round_rate_nolock()
1612 ret = clk_core_round_rate_nolock(core->parent, &parent_req); in clk_core_round_rate_nolock()
1618 req->best_parent_rate = parent_req.rate; in clk_core_round_rate_nolock()
1619 req->rate = parent_req.rate; in clk_core_round_rate_nolock()
1624 req->rate = core->rate; in clk_core_round_rate_nolock()
1629 * __clk_determine_rate - get the closest rate actually supported by a clock
1638 req->rate = 0; in __clk_determine_rate()
1642 return clk_core_round_rate_nolock(hw->core, req); in __clk_determine_rate()
1647 * clk_hw_round_rate() - round the given rate for a hw clk
1648 * @hw: the hw clk for which we are rounding a rate
1651 * Takes in a rate as input and rounds it to a rate that the clk can actually
1655 * For clk providers to call from within clk_ops such as .round_rate,
1658 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1666 clk_core_init_rate_req(hw->core, &req, rate); in clk_hw_round_rate()
1670 ret = clk_core_round_rate_nolock(hw->core, &req); in clk_hw_round_rate()
1681 * clk_round_rate - round the given rate for a clk
1682 * @clk: the clk for which we are rounding a rate
1685 * Takes in a rate as input and rounds it to a rate that the clk can actually
1686 * use which is then returned. If clk doesn't support round_rate operation
1689 long clk_round_rate(struct clk *clk, unsigned long rate) in clk_round_rate() argument
1694 if (!clk) in clk_round_rate()
1699 if (clk->exclusive_count) in clk_round_rate()
1700 clk_core_rate_unprotect(clk->core); in clk_round_rate()
1702 clk_core_init_rate_req(clk->core, &req, rate); in clk_round_rate()
1706 ret = clk_core_round_rate_nolock(clk->core, &req); in clk_round_rate()
1710 if (clk->exclusive_count) in clk_round_rate()
1711 clk_core_rate_protect(clk->core); in clk_round_rate()
1723 * __clk_notify - call clk notifier chain
1724 * @core: clk that is changing rate
1725 * @msg: clk notifier type (see include/linux/clk.h)
1726 * @old_rate: old clk rate
1727 * @new_rate: new clk rate
1729 * Triggers a notifier call chain on the clk rate-change notification
1730 * for 'clk'. Passes a pointer to the struct clk and the previous
1747 if (cn->clk->core == core) { in __clk_notify()
1748 cnd.clk = cn->clk; in __clk_notify()
1749 ret = srcu_notifier_call_chain(&cn->notifier_head, msg, in __clk_notify()
1761 * @core: first clk in the subtree
1763 * Walks the subtree of clks starting with clk and recalculates accuracies as
1764 * it goes. Note that if a clk does not implement the .recalc_accuracy
1775 if (core->parent) in __clk_recalc_accuracies()
1776 parent_accuracy = core->parent->accuracy; in __clk_recalc_accuracies()
1778 if (core->ops->recalc_accuracy) in __clk_recalc_accuracies()
1779 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_recalc_accuracies()
1782 core->accuracy = parent_accuracy; in __clk_recalc_accuracies()
1784 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_accuracies()
1790 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) in clk_core_get_accuracy_recalc()
1797 * clk_get_accuracy - return the accuracy of clk
1798 * @clk: the clk whose accuracy is being returned
1800 * Simply returns the cached accuracy of the clk, unless
1803 * If clk is NULL then returns 0.
1805 long clk_get_accuracy(struct clk *clk) in clk_get_accuracy() argument
1809 if (!clk) in clk_get_accuracy()
1813 accuracy = clk_core_get_accuracy_recalc(clk->core); in clk_get_accuracy()
1825 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { in clk_recalc()
1826 rate = core->ops->recalc_rate(core->hw, parent_rate); in clk_recalc()
1834 * @core: first clk in the subtree
1836 * @msg: notification type (see include/linux/clk.h)
1838 * Walks the subtree of clks starting with clk and recalculates rates as it
1839 * goes. Note that if a clk does not implement the .recalc_rate callback then
1854 old_rate = core->rate; in __clk_recalc_rates()
1856 if (core->parent) in __clk_recalc_rates()
1857 parent_rate = core->parent->rate; in __clk_recalc_rates()
1859 core->rate = clk_recalc(core, parent_rate); in __clk_recalc_rates()
1861 core->req_rate = core->rate; in __clk_recalc_rates()
1867 if (core->notifier_count && msg) in __clk_recalc_rates()
1868 __clk_notify(core, msg, old_rate, core->rate); in __clk_recalc_rates()
1870 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_rates()
1876 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) in clk_core_get_rate_recalc()
1883 * clk_get_rate - return the rate of clk
1884 * @clk: the clk whose rate is being returned
1886 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1888 * the clock enabledness. If clk is NULL, or if an error occurred, then returns
1891 unsigned long clk_get_rate(struct clk *clk) in clk_get_rate() argument
1895 if (!clk) in clk_get_rate()
1899 rate = clk_core_get_rate_recalc(clk->core); in clk_get_rate()
1912 return -EINVAL; in clk_fetch_parent_index()
1914 for (i = 0; i < core->num_parents; i++) { in clk_fetch_parent_index()
1916 if (core->parents[i].core == parent) in clk_fetch_parent_index()
1920 if (core->parents[i].core) in clk_fetch_parent_index()
1924 if (core->parents[i].hw) { in clk_fetch_parent_index()
1925 if (core->parents[i].hw == parent->hw) in clk_fetch_parent_index()
1937 if (core->parents[i].name && in clk_fetch_parent_index()
1938 !strcmp(parent->name, core->parents[i].name)) in clk_fetch_parent_index()
1942 if (i == core->num_parents) in clk_fetch_parent_index()
1943 return -EINVAL; in clk_fetch_parent_index()
1945 core->parents[i].core = parent; in clk_fetch_parent_index()
1950 * clk_hw_get_parent_index - return the index of the parent clock
1951 * @hw: clk_hw associated with the clk being consumed
1953 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1961 return -EINVAL; in clk_hw_get_parent_index()
1963 return clk_fetch_parent_index(hw->core, parent->core); in clk_hw_get_parent_index()
1974 core->orphan = is_orphan; in clk_core_update_orphan_status()
1976 hlist_for_each_entry(child, &core->children, child_node) in clk_core_update_orphan_status()
1982 bool was_orphan = core->orphan; in clk_reparent()
1984 hlist_del(&core->child_node); in clk_reparent()
1987 bool becomes_orphan = new_parent->orphan; in clk_reparent()
1990 if (new_parent->new_child == core) in clk_reparent()
1991 new_parent->new_child = NULL; in clk_reparent()
1993 hlist_add_head(&core->child_node, &new_parent->children); in clk_reparent()
1998 hlist_add_head(&core->child_node, &clk_orphan_list); in clk_reparent()
2003 core->parent = new_parent; in clk_reparent()
2010 struct clk_core *old_parent = core->parent; in __clk_set_parent_before()
2033 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_before()
2039 if (core->prepare_count) { in __clk_set_parent_before()
2044 /* update the clk tree topology */ in __clk_set_parent_before()
2060 if (core->prepare_count) { in __clk_set_parent_after()
2065 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ in __clk_set_parent_after()
2066 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_after()
2084 if (parent && core->ops->set_parent) in __clk_set_parent()
2085 ret = core->ops->set_parent(core->hw, p_index); in __clk_set_parent()
2106 * @core: first clk in the subtree
2107 * @parent_rate: the "future" rate of clk's parent
2109 * Walks the subtree of clks starting with clk, speculating rates as it
2113 * pre-rate change notifications and returns early if no clks in the
2114 * subtree have subscribed to the notifications. Note that if a clk does not
2130 if (core->notifier_count) in __clk_speculate_rates()
2131 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); in __clk_speculate_rates()
2134 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", in __clk_speculate_rates()
2135 __func__, core->name, ret); in __clk_speculate_rates()
2139 hlist_for_each_entry(child, &core->children, child_node) { in __clk_speculate_rates()
2154 core->new_rate = new_rate; in clk_calc_subtree()
2155 core->new_parent = new_parent; in clk_calc_subtree()
2156 core->new_parent_index = p_index; in clk_calc_subtree()
2157 /* include clk in new parent's PRE_RATE_CHANGE notifications */ in clk_calc_subtree()
2158 core->new_child = NULL; in clk_calc_subtree()
2159 if (new_parent && new_parent != core->parent) in clk_calc_subtree()
2160 new_parent->new_child = core; in clk_calc_subtree()
2162 hlist_for_each_entry(child, &core->children, child_node) { in clk_calc_subtree()
2163 child->new_rate = clk_recalc(child, new_rate); in clk_calc_subtree()
2164 clk_calc_subtree(child, child->new_rate, NULL, 0); in clk_calc_subtree()
2189 parent = old_parent = core->parent; in clk_calc_new_rates()
2191 best_parent_rate = parent->rate; in clk_calc_new_rates()
2195 /* find the closest rate and parent clk/rate */ in clk_calc_new_rates()
2211 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; in clk_calc_new_rates()
2215 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { in clk_calc_new_rates()
2216 /* pass-through clock without adjustable parent */ in clk_calc_new_rates()
2217 core->new_rate = core->rate; in clk_calc_new_rates()
2220 /* pass-through clock with adjustable parent */ in clk_calc_new_rates()
2222 new_rate = parent->new_rate; in clk_calc_new_rates()
2228 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { in clk_calc_new_rates()
2230 __func__, core->name); in clk_calc_new_rates()
2235 if (parent && core->num_parents > 1) { in clk_calc_new_rates()
2238 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_calc_new_rates()
2239 __func__, parent->name, core->name); in clk_calc_new_rates()
2244 if ((core->flags & CLK_SET_RATE_PARENT) && parent && in clk_calc_new_rates()
2245 best_parent_rate != parent->rate) in clk_calc_new_rates()
2265 if (core->rate == core->new_rate) in clk_propagate_rate_change()
2268 if (core->notifier_count) { in clk_propagate_rate_change()
2269 ret = __clk_notify(core, event, core->rate, core->new_rate); in clk_propagate_rate_change()
2274 hlist_for_each_entry(child, &core->children, child_node) { in clk_propagate_rate_change()
2276 if (child->new_parent && child->new_parent != core) in clk_propagate_rate_change()
2283 /* handle the new child who might not be in core->children yet */ in clk_propagate_rate_change()
2284 if (core->new_child) { in clk_propagate_rate_change()
2285 tmp_clk = clk_propagate_rate_change(core->new_child, event); in clk_propagate_rate_change()
2307 old_rate = core->rate; in clk_change_rate()
2309 if (core->new_parent) { in clk_change_rate()
2310 parent = core->new_parent; in clk_change_rate()
2311 best_parent_rate = core->new_parent->rate; in clk_change_rate()
2312 } else if (core->parent) { in clk_change_rate()
2313 parent = core->parent; in clk_change_rate()
2314 best_parent_rate = core->parent->rate; in clk_change_rate()
2320 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2325 if (core->new_parent && core->new_parent != core->parent) { in clk_change_rate()
2326 old_parent = __clk_set_parent_before(core, core->new_parent); in clk_change_rate()
2327 trace_clk_set_parent(core, core->new_parent); in clk_change_rate()
2329 if (core->ops->set_rate_and_parent) { in clk_change_rate()
2331 core->ops->set_rate_and_parent(core->hw, core->new_rate, in clk_change_rate()
2333 core->new_parent_index); in clk_change_rate()
2334 } else if (core->ops->set_parent) { in clk_change_rate()
2335 core->ops->set_parent(core->hw, core->new_parent_index); in clk_change_rate()
2338 trace_clk_set_parent_complete(core, core->new_parent); in clk_change_rate()
2339 __clk_set_parent_after(core, core->new_parent, old_parent); in clk_change_rate()
2342 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2345 trace_clk_set_rate(core, core->new_rate); in clk_change_rate()
2347 if (!skip_set_rate && core->ops->set_rate) in clk_change_rate()
2348 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); in clk_change_rate()
2350 trace_clk_set_rate_complete(core, core->new_rate); in clk_change_rate()
2352 core->rate = clk_recalc(core, best_parent_rate); in clk_change_rate()
2354 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2359 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2362 if (core->notifier_count && old_rate != core->rate) in clk_change_rate()
2363 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); in clk_change_rate()
2365 if (core->flags & CLK_RECALC_NEW_RATES) in clk_change_rate()
2366 (void)clk_calc_new_rates(core, core->new_rate); in clk_change_rate()
2372 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { in clk_change_rate()
2374 if (child->new_parent && child->new_parent != core) in clk_change_rate()
2379 /* handle the new child who might not be in core->children yet */ in clk_change_rate()
2380 if (core->new_child) in clk_change_rate()
2381 clk_change_rate(core->new_child); in clk_change_rate()
2434 return -EBUSY; in clk_core_set_rate_nolock()
2439 return -EINVAL; in clk_core_set_rate_nolock()
2449 fail_clk->name); in clk_core_set_rate_nolock()
2451 ret = -EBUSY; in clk_core_set_rate_nolock()
2458 core->req_rate = req_rate; in clk_core_set_rate_nolock()
2466 * clk_set_rate - specify a new rate for clk
2467 * @clk: the clk whose rate is being changed
2468 * @rate: the new rate for clk
2470 * In the simplest case clk_set_rate will only adjust the rate of clk.
2473 * propagate up to clk's parent; whether or not this happens depends on the
2474 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2476 * *parent_rate comes back with a new rate for clk's parent then we propagate
2477 * up to clk's parent and set its rate. Upward propagation will continue
2478 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2479 * .round_rate stops requesting changes to clk's parent_rate.
2484 * Returns 0 on success, -EERROR otherwise.
2486 int clk_set_rate(struct clk *clk, unsigned long rate) in clk_set_rate() argument
2490 if (!clk) in clk_set_rate()
2496 if (clk->exclusive_count) in clk_set_rate()
2497 clk_core_rate_unprotect(clk->core); in clk_set_rate()
2499 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate()
2501 if (clk->exclusive_count) in clk_set_rate()
2502 clk_core_rate_protect(clk->core); in clk_set_rate()
2511 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2512 * @clk: the clk whose rate is being changed
2513 * @rate: the new rate for clk
2527 * Returns 0 on success, -EERROR otherwise.
2529 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) in clk_set_rate_exclusive() argument
2533 if (!clk) in clk_set_rate_exclusive()
2545 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_exclusive()
2547 clk_core_rate_protect(clk->core); in clk_set_rate_exclusive()
2548 clk->exclusive_count++; in clk_set_rate_exclusive()
2557 static int clk_set_rate_range_nolock(struct clk *clk, in clk_set_rate_range_nolock() argument
2566 if (!clk) in clk_set_rate_range_nolock()
2569 trace_clk_set_rate_range(clk->core, min, max); in clk_set_rate_range_nolock()
2572 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", in clk_set_rate_range_nolock()
2573 __func__, clk->core->name, clk->dev_id, clk->con_id, in clk_set_rate_range_nolock()
2575 return -EINVAL; in clk_set_rate_range_nolock()
2578 if (clk->exclusive_count) in clk_set_rate_range_nolock()
2579 clk_core_rate_unprotect(clk->core); in clk_set_rate_range_nolock()
2582 old_min = clk->min_rate; in clk_set_rate_range_nolock()
2583 old_max = clk->max_rate; in clk_set_rate_range_nolock()
2584 clk->min_rate = min; in clk_set_rate_range_nolock()
2585 clk->max_rate = max; in clk_set_rate_range_nolock()
2587 if (!clk_core_check_boundaries(clk->core, min, max)) { in clk_set_rate_range_nolock()
2588 ret = -EINVAL; in clk_set_rate_range_nolock()
2592 rate = clk->core->req_rate; in clk_set_rate_range_nolock()
2593 if (clk->core->flags & CLK_GET_RATE_NOCACHE) in clk_set_rate_range_nolock()
2594 rate = clk_core_get_rate_recalc(clk->core); in clk_set_rate_range_nolock()
2608 * - round_rate() was not favorable and fell on the wrong in clk_set_rate_range_nolock()
2610 * - the determine_rate() callback does not really check for in clk_set_rate_range_nolock()
2614 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_range_nolock()
2617 clk->min_rate = old_min; in clk_set_rate_range_nolock()
2618 clk->max_rate = old_max; in clk_set_rate_range_nolock()
2622 if (clk->exclusive_count) in clk_set_rate_range_nolock()
2623 clk_core_rate_protect(clk->core); in clk_set_rate_range_nolock()
2629 * clk_set_rate_range - set a rate range for a clock source
2630 * @clk: clock source
2636 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) in clk_set_rate_range() argument
2640 if (!clk) in clk_set_rate_range()
2645 ret = clk_set_rate_range_nolock(clk, min, max); in clk_set_rate_range()
2654 * clk_set_min_rate - set a minimum clock rate for a clock source
2655 * @clk: clock source
2660 int clk_set_min_rate(struct clk *clk, unsigned long rate) in clk_set_min_rate() argument
2662 if (!clk) in clk_set_min_rate()
2665 trace_clk_set_min_rate(clk->core, rate); in clk_set_min_rate()
2667 return clk_set_rate_range(clk, rate, clk->max_rate); in clk_set_min_rate()
2672 * clk_set_max_rate - set a maximum clock rate for a clock source
2673 * @clk: clock source
2678 int clk_set_max_rate(struct clk *clk, unsigned long rate) in clk_set_max_rate() argument
2680 if (!clk) in clk_set_max_rate()
2683 trace_clk_set_max_rate(clk->core, rate); in clk_set_max_rate()
2685 return clk_set_rate_range(clk, clk->min_rate, rate); in clk_set_max_rate()
2690 * clk_get_parent - return the parent of a clk
2691 * @clk: the clk whose parent gets returned
2693 * Simply returns clk->parent. Returns NULL if clk is NULL.
2695 struct clk *clk_get_parent(struct clk *clk) in clk_get_parent() argument
2697 struct clk *parent; in clk_get_parent()
2699 if (!clk) in clk_get_parent()
2703 /* TODO: Create a per-user clk and change callers to call clk_put */ in clk_get_parent()
2704 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; in clk_get_parent()
2715 if (core->num_parents > 1 && core->ops->get_parent) in __clk_init_parent()
2716 index = core->ops->get_parent(core->hw); in __clk_init_parent()
2734 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); in clk_hw_reparent()
2738 * clk_has_parent - check if a clock is a possible parent for another
2739 * @clk: clock source
2745 * Returns true if @parent is a possible parent for @clk, false otherwise.
2747 bool clk_has_parent(const struct clk *clk, const struct clk *parent) in clk_has_parent() argument
2750 if (!clk || !parent) in clk_has_parent()
2753 return clk_core_has_parent(clk->core, parent->core); in clk_has_parent()
2769 if (core->parent == parent) in clk_core_set_parent_nolock()
2772 /* verify ops for multi-parent clks */ in clk_core_set_parent_nolock()
2773 if (core->num_parents > 1 && !core->ops->set_parent) in clk_core_set_parent_nolock()
2774 return -EPERM; in clk_core_set_parent_nolock()
2776 /* check that we are allowed to re-parent if the clock is in use */ in clk_core_set_parent_nolock()
2777 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) in clk_core_set_parent_nolock()
2778 return -EBUSY; in clk_core_set_parent_nolock()
2781 return -EBUSY; in clk_core_set_parent_nolock()
2787 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_core_set_parent_nolock()
2788 __func__, parent->name, core->name); in clk_core_set_parent_nolock()
2791 p_rate = parent->rate; in clk_core_set_parent_nolock()
2805 /* do the re-parent */ in clk_core_set_parent_nolock()
2824 return clk_core_set_parent_nolock(hw->core, parent->core); in clk_hw_set_parent()
2829 * clk_set_parent - switch the parent of a mux clk
2830 * @clk: the mux clk whose input we are switching
2831 * @parent: the new input to clk
2833 * Re-parent clk to use parent as its new input source. If clk is in
2834 * prepared state, the clk will get enabled for the duration of this call. If
2835 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2837 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2839 * After successfully changing clk's parent clk_set_parent will update the
2840 * clk topology, sysfs topology and propagate rate recalculation via
2843 * Returns 0 on success, -EERROR otherwise.
2845 int clk_set_parent(struct clk *clk, struct clk *parent) in clk_set_parent() argument
2849 if (!clk) in clk_set_parent()
2854 if (clk->exclusive_count) in clk_set_parent()
2855 clk_core_rate_unprotect(clk->core); in clk_set_parent()
2857 ret = clk_core_set_parent_nolock(clk->core, in clk_set_parent()
2858 parent ? parent->core : NULL); in clk_set_parent()
2860 if (clk->exclusive_count) in clk_set_parent()
2861 clk_core_rate_protect(clk->core); in clk_set_parent()
2871 int ret = -EINVAL; in clk_core_set_phase_nolock()
2879 return -EBUSY; in clk_core_set_phase_nolock()
2883 if (core->ops->set_phase) { in clk_core_set_phase_nolock()
2884 ret = core->ops->set_phase(core->hw, degrees); in clk_core_set_phase_nolock()
2886 core->phase = degrees; in clk_core_set_phase_nolock()
2895 * clk_set_phase - adjust the phase shift of a clock signal
2896 * @clk: clock signal source
2899 * Shifts the phase of a clock signal by the specified
2900 * degrees. Returns 0 on success, -EERROR otherwise.
2903 * signal that we adjust the clock signal phase against. For example
2904 * phase locked-loop clock signal generators we may shift phase with
2906 * clock phase may be shifted with respect to some other, unspecified
2909 * Additionally the concept of phase shift does not propagate through
2911 * clock accuracy. A parent clock phase attribute does not have an
2912 * impact on the phase attribute of a child clock.
2914 int clk_set_phase(struct clk *clk, int degrees) in clk_set_phase() argument
2918 if (!clk) in clk_set_phase()
2928 if (clk->exclusive_count) in clk_set_phase()
2929 clk_core_rate_unprotect(clk->core); in clk_set_phase()
2931 ret = clk_core_set_phase_nolock(clk->core, degrees); in clk_set_phase()
2933 if (clk->exclusive_count) in clk_set_phase()
2934 clk_core_rate_protect(clk->core); in clk_set_phase()
2947 if (!core->ops->get_phase) in clk_core_get_phase()
2950 /* Always try to update cached phase if possible */ in clk_core_get_phase()
2951 ret = core->ops->get_phase(core->hw); in clk_core_get_phase()
2953 core->phase = ret; in clk_core_get_phase()
2959 * clk_get_phase - return the phase shift of a clock signal
2960 * @clk: clock signal source
2962 * Returns the phase shift of a clock node in degrees, otherwise returns
2963 * -EERROR.
2965 int clk_get_phase(struct clk *clk) in clk_get_phase() argument
2969 if (!clk) in clk_get_phase()
2973 ret = clk_core_get_phase(clk->core); in clk_get_phase()
2983 core->duty.num = 1; in clk_core_reset_duty_cycle_nolock()
2984 core->duty.den = 2; in clk_core_reset_duty_cycle_nolock()
2991 struct clk_duty *duty = &core->duty; in clk_core_update_duty_cycle_nolock()
2994 if (!core->ops->get_duty_cycle) in clk_core_update_duty_cycle_nolock()
2997 ret = core->ops->get_duty_cycle(core->hw, duty); in clk_core_update_duty_cycle_nolock()
3002 if (duty->den == 0 || duty->num > duty->den) { in clk_core_update_duty_cycle_nolock()
3003 ret = -EINVAL; in clk_core_update_duty_cycle_nolock()
3018 if (core->parent && in clk_core_update_duty_cycle_parent_nolock()
3019 core->flags & CLK_DUTY_CYCLE_PARENT) { in clk_core_update_duty_cycle_parent_nolock()
3020 ret = clk_core_update_duty_cycle_nolock(core->parent); in clk_core_update_duty_cycle_parent_nolock()
3021 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_update_duty_cycle_parent_nolock()
3040 return -EBUSY; in clk_core_set_duty_cycle_nolock()
3044 if (!core->ops->set_duty_cycle) in clk_core_set_duty_cycle_nolock()
3047 ret = core->ops->set_duty_cycle(core->hw, duty); in clk_core_set_duty_cycle_nolock()
3049 memcpy(&core->duty, duty, sizeof(*duty)); in clk_core_set_duty_cycle_nolock()
3061 if (core->parent && in clk_core_set_duty_cycle_parent_nolock()
3062 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) { in clk_core_set_duty_cycle_parent_nolock()
3063 ret = clk_core_set_duty_cycle_nolock(core->parent, duty); in clk_core_set_duty_cycle_parent_nolock()
3064 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_set_duty_cycle_parent_nolock()
3071 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
3072 * @clk: clock signal source
3081 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den) in clk_set_duty_cycle() argument
3086 if (!clk) in clk_set_duty_cycle()
3091 return -EINVAL; in clk_set_duty_cycle()
3098 if (clk->exclusive_count) in clk_set_duty_cycle()
3099 clk_core_rate_unprotect(clk->core); in clk_set_duty_cycle()
3101 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty); in clk_set_duty_cycle()
3103 if (clk->exclusive_count) in clk_set_duty_cycle()
3104 clk_core_rate_protect(clk->core); in clk_set_duty_cycle()
3115 struct clk_duty *duty = &core->duty; in clk_core_get_scaled_duty_cycle()
3122 ret = mult_frac(scale, duty->num, duty->den); in clk_core_get_scaled_duty_cycle()
3130 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
3131 * @clk: clock signal source
3137 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale) in clk_get_scaled_duty_cycle() argument
3139 if (!clk) in clk_get_scaled_duty_cycle()
3142 return clk_core_get_scaled_duty_cycle(clk->core, scale); in clk_get_scaled_duty_cycle()
3147 * clk_is_match - check if two clk's point to the same hardware clock
3148 * @p: clk compared against q
3149 * @q: clk compared against p
3151 * Returns true if the two struct clk pointers both point to the same hardware
3152 * clock node. Put differently, returns true if struct clk *p and struct clk *q
3157 bool clk_is_match(const struct clk *p, const struct clk *q) in clk_is_match()
3159 /* trivial case: identical struct clk's or both NULL */ in clk_is_match()
3163 /* true if clk->core pointers match. Avoid dereferencing garbage */ in clk_is_match()
3165 if (p->core == q->core) in clk_is_match()
3190 int phase; in clk_summary_show_one() local
3191 struct clk *clk_user; in clk_summary_show_one()
3194 seq_printf(s, "%*s%-*s %-7d %-8d %-8d %-11lu %-10lu ", in clk_summary_show_one()
3196 35 - level * 3, c->name, in clk_summary_show_one()
3197 c->enable_count, c->prepare_count, c->protect_count, in clk_summary_show_one()
3201 phase = clk_core_get_phase(c); in clk_summary_show_one()
3202 if (phase >= 0) in clk_summary_show_one()
3203 seq_printf(s, "%-5d", phase); in clk_summary_show_one()
3205 seq_puts(s, "-----"); in clk_summary_show_one()
3207 seq_printf(s, " %-6d", clk_core_get_scaled_duty_cycle(c, 100000)); in clk_summary_show_one()
3209 if (c->ops->is_enabled) in clk_summary_show_one()
3211 else if (!c->ops->enable) in clk_summary_show_one()
3216 hlist_for_each_entry(clk_user, &c->clks, clks_node) { in clk_summary_show_one()
3217 seq_printf(s, "%*s%-*s %-25s\n", in clk_summary_show_one()
3220 clk_user->dev_id ? clk_user->dev_id : "deviceless", in clk_summary_show_one()
3221 clk_user->con_id ? clk_user->con_id : "no_connection_id"); in clk_summary_show_one()
3237 hlist_for_each_entry(child, &c->children, child_node) in clk_summary_show_subtree()
3244 struct hlist_head **lists = s->private; in clk_summary_show()
3247 …k count count count rate accuracy phase cycle enable … in clk_summary_show()
3248 …s(s, "--------------------------------------------------------------------------------------------… in clk_summary_show()
3265 int phase; in clk_dump_one() local
3271 seq_printf(s, "\"%s\": { ", c->name); in clk_dump_one()
3272 seq_printf(s, "\"enable_count\": %d,", c->enable_count); in clk_dump_one()
3273 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); in clk_dump_one()
3274 seq_printf(s, "\"protect_count\": %d,", c->protect_count); in clk_dump_one()
3279 phase = clk_core_get_phase(c); in clk_dump_one()
3280 if (phase >= 0) in clk_dump_one()
3281 seq_printf(s, "\"phase\": %d,", phase); in clk_dump_one()
3292 hlist_for_each_entry(child, &c->children, child_node) { in clk_dump_subtree()
3304 struct hlist_head **lists = s->private; in clk_dump_show()
3367 ret = clk_prepare_enable(core->hw->clk); in clk_prepare_enable_set()
3369 clk_disable_unprepare(core->hw->clk); in clk_prepare_enable_set()
3378 *val = core->enable_count && core->prepare_count; in clk_prepare_enable_get()
3410 *val = core->phase; in clk_phase_get()
3438 struct clk_core *core = s->private; in clk_flags_show()
3439 unsigned long flags = core->flags; in clk_flags_show()
3469 * 4. Fetch parent clock's clock-output-name if DT index was set in possible_parent_show()
3477 seq_puts(s, parent->name); in possible_parent_show()
3478 } else if (core->parents[i].name) { in possible_parent_show()
3479 seq_puts(s, core->parents[i].name); in possible_parent_show()
3480 } else if (core->parents[i].fw_name) { in possible_parent_show()
3481 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name); in possible_parent_show()
3483 if (core->parents[i].index >= 0) in possible_parent_show()
3484 name = of_clk_get_parent_name(core->of_node, core->parents[i].index); in possible_parent_show()
3496 struct clk_core *core = s->private; in possible_parents_show()
3499 for (i = 0; i < core->num_parents - 1; i++) in possible_parents_show()
3510 struct clk_core *core = s->private; in current_parent_show()
3512 if (core->parent) in current_parent_show()
3513 seq_printf(s, "%s\n", core->parent->name); in current_parent_show()
3523 struct seq_file *s = file->private_data; in current_parent_write()
3524 struct clk_core *core = s->private; in current_parent_write()
3535 return -ENOENT; in current_parent_write()
3557 struct clk_core *core = s->private; in clk_duty_cycle_show()
3558 struct clk_duty *duty = &core->duty; in clk_duty_cycle_show()
3560 seq_printf(s, "%u/%u\n", duty->num, duty->den); in clk_duty_cycle_show()
3568 struct clk_core *core = s->private; in clk_min_rate_show()
3582 struct clk_core *core = s->private; in clk_max_rate_show()
3601 root = debugfs_create_dir(core->name, pdentry); in clk_debug_create_one()
3602 core->dentry = root; in clk_debug_create_one()
3608 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy); in clk_debug_create_one()
3612 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count); in clk_debug_create_one()
3613 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count); in clk_debug_create_one()
3614 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count); in clk_debug_create_one()
3615 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count); in clk_debug_create_one()
3622 if (core->num_parents > 1) in clk_debug_create_one()
3627 if (core->num_parents > 0) in clk_debug_create_one()
3631 if (core->num_parents > 1) in clk_debug_create_one()
3635 if (core->ops->debug_init) in clk_debug_create_one()
3636 core->ops->debug_init(core->hw, core->dentry); in clk_debug_create_one()
3640 * clk_debug_register - add a clk node to the debugfs clk directory
3641 * @core: the clk being added to the debugfs clk directory
3643 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3644 * initialized. Otherwise it bails out early since the debugfs clk directory
3650 hlist_add_head(&core->debug_node, &clk_debug_list); in clk_debug_register()
3657 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3658 * @core: the clk being removed from the debugfs clk directory
3660 * Dynamically removes a clk and all its child nodes from the
3661 * debugfs clk directory if clk->dentry points to debugfs created by
3667 hlist_del_init(&core->debug_node); in clk_debug_unregister()
3668 debugfs_remove_recursive(core->dentry); in clk_debug_unregister()
3669 core->dentry = NULL; in clk_debug_unregister()
3674 * clk_debug_init - lazily populate the debugfs clk directory
3678 * populates the debugfs clk directory once at boot-time when we know that
3679 * debugfs is setup. It should only be called once at boot-time, all other clks
3691 pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n"); in clk_debug_init()
3693 pr_warn("** This means that this kernel is built to expose clk operations **\n"); in clk_debug_init()
3704 rootdir = debugfs_create_dir("clk", NULL); in clk_debug_init()
3751 /* update the clk tree topology */ in clk_core_reparent_orphans_nolock()
3765 * 'req_rate' is set to something non-zero so that in clk_core_reparent_orphans_nolock()
3768 orphan->req_rate = orphan->rate; in clk_core_reparent_orphans_nolock()
3774 * __clk_core_init - initialize the data structures in a struct clk_core
3785 int phase; in __clk_core_init() local
3790 * Set hw->core after grabbing the prepare_lock to synchronize with in __clk_core_init()
3791 * callers of clk_core_fill_parent_index() where we treat hw->core in __clk_core_init()
3792 * being NULL as the clk not being registered yet. This is crucial so in __clk_core_init()
3795 core->hw->core = core; in __clk_core_init()
3802 if (clk_core_lookup(core->name)) { in __clk_core_init()
3803 pr_debug("%s: clk %s already initialized\n", in __clk_core_init()
3804 __func__, core->name); in __clk_core_init()
3805 ret = -EEXIST; in __clk_core_init()
3809 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */ in __clk_core_init()
3810 if (core->ops->set_rate && in __clk_core_init()
3811 !((core->ops->round_rate || core->ops->determine_rate) && in __clk_core_init()
3812 core->ops->recalc_rate)) { in __clk_core_init()
3814 __func__, core->name); in __clk_core_init()
3815 ret = -EINVAL; in __clk_core_init()
3819 if (core->ops->set_parent && !core->ops->get_parent) { in __clk_core_init()
3821 __func__, core->name); in __clk_core_init()
3822 ret = -EINVAL; in __clk_core_init()
3826 if (core->ops->set_parent && !core->ops->determine_rate) { in __clk_core_init()
3828 __func__, core->name); in __clk_core_init()
3829 ret = -EINVAL; in __clk_core_init()
3833 if (core->num_parents > 1 && !core->ops->get_parent) { in __clk_core_init()
3835 __func__, core->name); in __clk_core_init()
3836 ret = -EINVAL; in __clk_core_init()
3840 if (core->ops->set_rate_and_parent && in __clk_core_init()
3841 !(core->ops->set_parent && core->ops->set_rate)) { in __clk_core_init()
3843 __func__, core->name); in __clk_core_init()
3844 ret = -EINVAL; in __clk_core_init()
3849 * optional platform-specific magic in __clk_core_init()
3862 if (core->ops->init) { in __clk_core_init()
3863 ret = core->ops->init(core->hw); in __clk_core_init()
3868 parent = core->parent = __clk_init_parent(core); in __clk_core_init()
3871 * Populate core->parent if parent has already been clk_core_init'd. If in __clk_core_init()
3872 * parent has not yet been clk_core_init'd then place clk in the orphan in __clk_core_init()
3873 * list. If clk doesn't have any parents then place it in the root in __clk_core_init()
3874 * clk list. in __clk_core_init()
3876 * Every time a new clk is clk_init'd then we walk the list of orphan in __clk_core_init()
3877 * clocks and re-parent any that are children of the clock currently in __clk_core_init()
3881 hlist_add_head(&core->child_node, &parent->children); in __clk_core_init()
3882 core->orphan = parent->orphan; in __clk_core_init()
3883 } else if (!core->num_parents) { in __clk_core_init()
3884 hlist_add_head(&core->child_node, &clk_root_list); in __clk_core_init()
3885 core->orphan = false; in __clk_core_init()
3887 hlist_add_head(&core->child_node, &clk_orphan_list); in __clk_core_init()
3888 core->orphan = true; in __clk_core_init()
3892 * Set clk's accuracy. The preferred method is to use in __clk_core_init()
3898 if (core->ops->recalc_accuracy) in __clk_core_init()
3899 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_core_init()
3902 core->accuracy = parent->accuracy; in __clk_core_init()
3904 core->accuracy = 0; in __clk_core_init()
3907 * Set clk's phase by clk_core_get_phase() caching the phase. in __clk_core_init()
3908 * Since a phase is by definition relative to its parent, just in __clk_core_init()
3909 * query the current clock phase, or just assume it's in phase. in __clk_core_init()
3911 phase = clk_core_get_phase(core); in __clk_core_init()
3912 if (phase < 0) { in __clk_core_init()
3913 ret = phase; in __clk_core_init()
3914 pr_warn("%s: Failed to get phase for clk '%s'\n", __func__, in __clk_core_init()
3915 core->name); in __clk_core_init()
3920 * Set clk's duty cycle. in __clk_core_init()
3925 * Set clk's rate. The preferred method is to use .recalc_rate. For in __clk_core_init()
3930 if (core->ops->recalc_rate) in __clk_core_init()
3931 rate = core->ops->recalc_rate(core->hw, in __clk_core_init()
3934 rate = parent->rate; in __clk_core_init()
3937 core->rate = core->req_rate = rate; in __clk_core_init()
3944 if (core->flags & CLK_IS_CRITICAL) { in __clk_core_init()
3947 pr_warn("%s: critical clk '%s' failed to prepare\n", in __clk_core_init()
3948 __func__, core->name); in __clk_core_init()
3954 pr_warn("%s: critical clk '%s' failed to enable\n", in __clk_core_init()
3955 __func__, core->name); in __clk_core_init()
3963 kref_init(&core->ref); in __clk_core_init()
3968 hlist_del_init(&core->child_node); in __clk_core_init()
3969 core->hw->core = NULL; in __clk_core_init()
3981 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3982 * @core: clk to add consumer to
3983 * @clk: consumer to link to a clk
3985 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk) in clk_core_link_consumer() argument
3988 hlist_add_head(&clk->clks_node, &core->clks); in clk_core_link_consumer()
3993 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3994 * @clk: consumer to unlink
3996 static void clk_core_unlink_consumer(struct clk *clk) in clk_core_unlink_consumer() argument
3999 hlist_del(&clk->clks_node); in clk_core_unlink_consumer()
4003 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
4004 * @core: clk to allocate a consumer for
4008 * Returns: clk consumer left unlinked from the consumer list
4010 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id, in alloc_clk()
4013 struct clk *clk; in alloc_clk() local
4015 clk = kzalloc(sizeof(*clk), GFP_KERNEL); in alloc_clk()
4016 if (!clk) in alloc_clk()
4017 return ERR_PTR(-ENOMEM); in alloc_clk()
4019 clk->core = core; in alloc_clk()
4020 clk->dev_id = dev_id; in alloc_clk()
4021 clk->con_id = kstrdup_const(con_id, GFP_KERNEL); in alloc_clk()
4022 clk->max_rate = ULONG_MAX; in alloc_clk()
4024 return clk; in alloc_clk()
4028 * free_clk - Free a clk consumer
4029 * @clk: clk consumer to free
4031 * Note, this assumes the clk has been unlinked from the clk_core consumer
4034 static void free_clk(struct clk *clk) in free_clk() argument
4036 kfree_const(clk->con_id); in free_clk()
4037 kfree(clk); in free_clk()
4041 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
4043 * @dev: clk consumer device
4044 * @hw: clk_hw associated with the clk being consumed
4048 * This is the main function used to create a clk pointer for use by clk
4050 * used by the framework and clk provider respectively.
4052 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw, in clk_hw_create_clk()
4055 struct clk *clk; in clk_hw_create_clk() local
4062 core = hw->core; in clk_hw_create_clk()
4063 clk = alloc_clk(core, dev_id, con_id); in clk_hw_create_clk()
4064 if (IS_ERR(clk)) in clk_hw_create_clk()
4065 return clk; in clk_hw_create_clk()
4066 clk->dev = dev; in clk_hw_create_clk()
4068 if (!try_module_get(core->owner)) { in clk_hw_create_clk()
4069 free_clk(clk); in clk_hw_create_clk()
4070 return ERR_PTR(-ENOENT); in clk_hw_create_clk()
4073 kref_get(&core->ref); in clk_hw_create_clk()
4074 clk_core_link_consumer(core, clk); in clk_hw_create_clk()
4076 return clk; in clk_hw_create_clk()
4080 * clk_hw_get_clk - get clk consumer given an clk_hw
4081 * @hw: clk_hw associated with the clk being consumed
4084 * Returns: new clk consumer
4086 * to get a consumer clk and act on the clock element
4089 struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id) in clk_hw_get_clk()
4091 struct device *dev = hw->core->dev; in clk_hw_get_clk()
4104 return -EINVAL; in clk_cpy_name()
4110 return -ENOMEM; in clk_cpy_name()
4118 u8 num_parents = init->num_parents; in clk_core_populate_parent_map()
4119 const char * const *parent_names = init->parent_names; in clk_core_populate_parent_map()
4120 const struct clk_hw **parent_hws = init->parent_hws; in clk_core_populate_parent_map()
4121 const struct clk_parent_data *parent_data = init->parent_data; in clk_core_populate_parent_map()
4129 * Avoid unnecessary string look-ups of clk_core's possible parents by in clk_core_populate_parent_map()
4133 core->parents = parents; in clk_core_populate_parent_map()
4135 return -ENOMEM; in clk_core_populate_parent_map()
4139 parent->index = -1; in clk_core_populate_parent_map()
4144 __func__, core->name); in clk_core_populate_parent_map()
4145 ret = clk_cpy_name(&parent->name, parent_names[i], in clk_core_populate_parent_map()
4148 parent->hw = parent_data[i].hw; in clk_core_populate_parent_map()
4149 parent->index = parent_data[i].index; in clk_core_populate_parent_map()
4150 ret = clk_cpy_name(&parent->fw_name, in clk_core_populate_parent_map()
4153 ret = clk_cpy_name(&parent->name, in clk_core_populate_parent_map()
4157 parent->hw = parent_hws[i]; in clk_core_populate_parent_map()
4159 ret = -EINVAL; in clk_core_populate_parent_map()
4167 } while (--i >= 0); in clk_core_populate_parent_map()
4179 int i = core->num_parents; in clk_core_free_parent_map()
4181 if (!core->num_parents) in clk_core_free_parent_map()
4184 while (--i >= 0) { in clk_core_free_parent_map()
4185 kfree_const(core->parents[i].name); in clk_core_free_parent_map()
4186 kfree_const(core->parents[i].fw_name); in clk_core_free_parent_map()
4189 kfree(core->parents); in clk_core_free_parent_map()
4192 static struct clk *
4197 const struct clk_init_data *init = hw->init; in __clk_register()
4202 * we catch use of hw->init early on in the core. in __clk_register()
4204 hw->init = NULL; in __clk_register()
4208 ret = -ENOMEM; in __clk_register()
4212 core->name = kstrdup_const(init->name, GFP_KERNEL); in __clk_register()
4213 if (!core->name) { in __clk_register()
4214 ret = -ENOMEM; in __clk_register()
4218 if (WARN_ON(!init->ops)) { in __clk_register()
4219 ret = -EINVAL; in __clk_register()
4222 core->ops = init->ops; in __clk_register()
4225 core->rpm_enabled = true; in __clk_register()
4226 core->dev = dev; in __clk_register()
4227 core->of_node = np; in __clk_register()
4228 if (dev && dev->driver) in __clk_register()
4229 core->owner = dev->driver->owner; in __clk_register()
4230 core->hw = hw; in __clk_register()
4231 core->flags = init->flags; in __clk_register()
4232 core->num_parents = init->num_parents; in __clk_register()
4233 core->min_rate = 0; in __clk_register()
4234 core->max_rate = ULONG_MAX; in __clk_register()
4240 INIT_HLIST_HEAD(&core->clks); in __clk_register()
4246 hw->clk = alloc_clk(core, NULL, NULL); in __clk_register()
4247 if (IS_ERR(hw->clk)) { in __clk_register()
4248 ret = PTR_ERR(hw->clk); in __clk_register()
4252 clk_core_link_consumer(core, hw->clk); in __clk_register()
4256 return hw->clk; in __clk_register()
4259 clk_core_unlink_consumer(hw->clk); in __clk_register()
4262 free_clk(hw->clk); in __clk_register()
4263 hw->clk = NULL; in __clk_register()
4269 kfree_const(core->name); in __clk_register()
4277 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
4281 * @dev->parent if dev doesn't have a device node, or NULL if neither
4282 * @dev or @dev->parent have a device node.
4293 np = dev_of_node(dev->parent); in dev_or_parent_of_node()
4299 * clk_register - allocate a new clock, register it and return an opaque cookie
4301 * @hw: link to hardware-specific clock data
4306 * Returns: a pointer to the newly allocated struct clk which
4311 struct clk *clk_register(struct device *dev, struct clk_hw *hw) in clk_register()
4318 * clk_hw_register - register a clk_hw and return an error code
4320 * @hw: link to hardware-specific clock data
4335 * of_clk_hw_register - register a clk_hw and return an error code
4337 * @hw: link to hardware-specific clock data
4359 kfree_const(core->name); in __clk_release()
4366 * consumer calls clk_put() and the struct clk object is freed.
4370 return -ENXIO; in clk_nodrv_prepare_enable()
4381 return -ENXIO; in clk_nodrv_set_rate()
4386 return -ENXIO; in clk_nodrv_set_parent()
4392 return -ENXIO; in clk_nodrv_determine_rate()
4411 for (i = 0; i < root->num_parents; i++) in clk_core_evict_parent_cache_subtree()
4412 if (root->parents[i].core == target) in clk_core_evict_parent_cache_subtree()
4413 root->parents[i].core = NULL; in clk_core_evict_parent_cache_subtree()
4415 hlist_for_each_entry(child, &root->children, child_node) in clk_core_evict_parent_cache_subtree()
4419 /* Remove this clk from all parent caches */
4434 * clk_unregister - unregister a currently registered clock
4435 * @clk: clock to unregister
4437 void clk_unregister(struct clk *clk) in clk_unregister() argument
4442 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in clk_unregister()
4445 clk_debug_unregister(clk->core); in clk_unregister()
4449 ops = clk->core->ops; in clk_unregister()
4452 clk->core->name); in clk_unregister()
4460 clk->core->ops = &clk_nodrv_ops; in clk_unregister()
4463 if (ops->terminate) in clk_unregister()
4464 ops->terminate(clk->core->hw); in clk_unregister()
4466 if (!hlist_empty(&clk->core->children)) { in clk_unregister()
4471 hlist_for_each_entry_safe(child, t, &clk->core->children, in clk_unregister()
4476 clk_core_evict_parent_cache(clk->core); in clk_unregister()
4478 hlist_del_init(&clk->core->child_node); in clk_unregister()
4480 if (clk->core->prepare_count) in clk_unregister()
4482 __func__, clk->core->name); in clk_unregister()
4484 if (clk->core->protect_count) in clk_unregister()
4486 __func__, clk->core->name); in clk_unregister()
4488 kref_put(&clk->core->ref, __clk_release); in clk_unregister()
4489 free_clk(clk); in clk_unregister()
4496 * clk_hw_unregister - unregister a currently registered clk_hw
4497 * @hw: hardware-specific clock data to unregister
4501 clk_unregister(hw->clk); in clk_hw_unregister()
4507 clk_unregister(*(struct clk **)res); in devm_clk_unregister_cb()
4516 * devm_clk_register - resource managed clk_register()
4518 * @hw: link to hardware-specific clock data
4525 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) in devm_clk_register()
4527 struct clk *clk; in devm_clk_register() local
4528 struct clk **clkp; in devm_clk_register()
4532 return ERR_PTR(-ENOMEM); in devm_clk_register()
4534 clk = clk_register(dev, hw); in devm_clk_register()
4535 if (!IS_ERR(clk)) { in devm_clk_register()
4536 *clkp = clk; in devm_clk_register()
4542 return clk; in devm_clk_register()
4547 * devm_clk_hw_register - resource managed clk_hw_register()
4549 * @hw: link to hardware-specific clock data
4562 return -ENOMEM; in devm_clk_hw_register()
4578 clk_put(*(struct clk **)res); in devm_clk_release()
4582 * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
4584 * @hw: clk_hw associated with the clk being consumed
4591 struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw, in devm_clk_hw_get_clk()
4594 struct clk *clk; in devm_clk_hw_get_clk() local
4595 struct clk **clkp; in devm_clk_hw_get_clk()
4601 WARN_ON_ONCE(dev != hw->core->dev); in devm_clk_hw_get_clk()
4605 return ERR_PTR(-ENOMEM); in devm_clk_hw_get_clk()
4607 clk = clk_hw_get_clk(hw, con_id); in devm_clk_hw_get_clk()
4608 if (!IS_ERR(clk)) { in devm_clk_hw_get_clk()
4609 *clkp = clk; in devm_clk_hw_get_clk()
4615 return clk; in devm_clk_hw_get_clk()
4623 void __clk_put(struct clk *clk) in __clk_put() argument
4627 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in __clk_put()
4637 if (WARN_ON(clk->exclusive_count)) { in __clk_put()
4639 clk->core->protect_count -= (clk->exclusive_count - 1); in __clk_put()
4640 clk_core_rate_unprotect(clk->core); in __clk_put()
4641 clk->exclusive_count = 0; in __clk_put()
4644 hlist_del(&clk->clks_node); in __clk_put()
4647 if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX) in __clk_put()
4648 clk_set_rate_range_nolock(clk, 0, ULONG_MAX); in __clk_put()
4650 owner = clk->core->owner; in __clk_put()
4651 kref_put(&clk->core->ref, __clk_release); in __clk_put()
4657 free_clk(clk); in __clk_put()
4660 /*** clk rate change notifiers ***/
4663 * clk_notifier_register - add a clk rate change notifier
4664 * @clk: struct clk * to watch
4667 * Request notification when clk's rate changes. This uses an SRCU
4670 * re-enter into the clk framework by calling any top-level clk APIs;
4677 * clk_notifier_register() must be called from non-atomic context.
4678 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4682 int clk_notifier_register(struct clk *clk, struct notifier_block *nb) in clk_notifier_register() argument
4685 int ret = -ENOMEM; in clk_notifier_register()
4687 if (!clk || !nb) in clk_notifier_register()
4688 return -EINVAL; in clk_notifier_register()
4692 /* search the list of notifiers for this clk */ in clk_notifier_register()
4694 if (cn->clk == clk) in clk_notifier_register()
4697 /* if clk wasn't in the notifier list, allocate new clk_notifier */ in clk_notifier_register()
4702 cn->clk = clk; in clk_notifier_register()
4703 srcu_init_notifier_head(&cn->notifier_head); in clk_notifier_register()
4705 list_add(&cn->node, &clk_notifier_list); in clk_notifier_register()
4708 ret = srcu_notifier_chain_register(&cn->notifier_head, nb); in clk_notifier_register()
4710 clk->core->notifier_count++; in clk_notifier_register()
4720 * clk_notifier_unregister - remove a clk rate change notifier
4721 * @clk: struct clk *
4724 * Request no further notification for changes to 'clk' and frees memory
4727 * Returns -EINVAL if called with null arguments; otherwise, passes
4730 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) in clk_notifier_unregister() argument
4733 int ret = -ENOENT; in clk_notifier_unregister()
4735 if (!clk || !nb) in clk_notifier_unregister()
4736 return -EINVAL; in clk_notifier_unregister()
4741 if (cn->clk == clk) { in clk_notifier_unregister()
4742 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); in clk_notifier_unregister()
4744 clk->core->notifier_count--; in clk_notifier_unregister()
4747 if (!cn->notifier_head.head) { in clk_notifier_unregister()
4748 srcu_cleanup_notifier_head(&cn->notifier_head); in clk_notifier_unregister()
4749 list_del(&cn->node); in clk_notifier_unregister()
4763 struct clk *clk; member
4771 clk_notifier_unregister(devres->clk, devres->nb); in devm_clk_notifier_release()
4774 int devm_clk_notifier_register(struct device *dev, struct clk *clk, in devm_clk_notifier_register() argument
4784 return -ENOMEM; in devm_clk_notifier_register()
4786 ret = clk_notifier_register(clk, nb); in devm_clk_notifier_register()
4788 devres->clk = clk; in devm_clk_notifier_register()
4789 devres->nb = nb; in devm_clk_notifier_register()
4808 * struct of_clk_provider - Clock provider registration structure
4811 * @get: Get clock callback. Returns NULL or a struct clk for the
4821 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4833 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, in of_clk_src_simple_get()
4846 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) in of_clk_src_onecell_get()
4849 unsigned int idx = clkspec->args[0]; in of_clk_src_onecell_get()
4851 if (idx >= clk_data->clk_num) { in of_clk_src_onecell_get()
4853 return ERR_PTR(-EINVAL); in of_clk_src_onecell_get()
4856 return clk_data->clks[idx]; in of_clk_src_onecell_get()
4864 unsigned int idx = clkspec->args[0]; in of_clk_hw_onecell_get()
4866 if (idx >= hw_data->num) { in of_clk_hw_onecell_get()
4868 return ERR_PTR(-EINVAL); in of_clk_hw_onecell_get()
4871 return hw_data->hws[idx]; in of_clk_hw_onecell_get()
4876 * of_clk_add_provider() - Register a clock provider for a node
4884 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, in of_clk_add_provider()
4896 return -ENOMEM; in of_clk_add_provider()
4898 cp->node = of_node_get(np); in of_clk_add_provider()
4899 cp->data = data; in of_clk_add_provider()
4900 cp->get = clk_src_get; in of_clk_add_provider()
4903 list_add(&cp->link, &of_clk_providers); in of_clk_add_provider()
4913 fwnode_dev_initialized(&np->fwnode, true); in of_clk_add_provider()
4920 * of_clk_add_hw_provider() - Register a clock provider for a node
4938 return -ENOMEM; in of_clk_add_hw_provider()
4940 cp->node = of_node_get(np); in of_clk_add_hw_provider()
4941 cp->data = data; in of_clk_add_hw_provider()
4942 cp->get_hw = get; in of_clk_add_hw_provider()
4945 list_add(&cp->link, &of_clk_providers); in of_clk_add_hw_provider()
4955 fwnode_dev_initialized(&np->fwnode, true); in of_clk_add_hw_provider()
4968 * for cases like MFD sub-devices where the child device driver wants to use
4969 * devm_*() APIs but not list the device in DT as a sub-node.
4975 np = dev->of_node; in get_clk_provider_node()
4976 parent_np = dev->parent ? dev->parent->of_node : NULL; in get_clk_provider_node()
4978 if (!of_property_present(np, "#clock-cells")) in get_clk_provider_node()
4979 if (of_property_present(parent_np, "#clock-cells")) in get_clk_provider_node()
4986 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4992 * node or if the device node lacks of clock provider information (#clock-cells)
4994 * has the #clock-cells then it is used in registration. Provider is
5010 return -ENOMEM; in devm_of_clk_add_hw_provider()
5026 * of_clk_del_provider() - Remove a previously registered clock provider
5038 if (cp->node == np) { in of_clk_del_provider()
5039 list_del(&cp->link); in of_clk_del_provider()
5040 fwnode_dev_initialized(&np->fwnode, false); in of_clk_del_provider()
5041 of_node_put(cp->node); in of_clk_del_provider()
5051 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
5057 * Parses a device node's "clocks" and "clock-names" properties to find the
5060 * parsing error. The @index argument is ignored if @name is non-NULL.
5064 * phandle1: clock-controller@1 {
5065 * #clock-cells = <2>;
5068 * phandle2: clock-controller@2 {
5069 * #clock-cells = <1>;
5072 * clock-consumer@3 {
5074 * clock-names = "name1", "name2";
5077 * To get a device_node for `clock-controller@2' node you may call this
5080 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
5081 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
5082 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
5084 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
5085 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
5086 * the "clock-names" property of @np.
5091 int ret = -ENOENT; in of_parse_clkspec()
5097 * "clock-names" property. If it cannot be found, then index in of_parse_clkspec()
5099 * return -EINVAL. in of_parse_clkspec()
5102 index = of_property_match_string(np, "clock-names", name); in of_parse_clkspec()
5103 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells", in of_parse_clkspec()
5112 * has a "clock-ranges" property, then we can try one of its in of_parse_clkspec()
5115 np = np->parent; in of_parse_clkspec()
5116 if (np && !of_get_property(np, "clock-ranges", NULL)) in of_parse_clkspec()
5128 struct clk *clk; in __of_clk_get_hw_from_provider() local
5130 if (provider->get_hw) in __of_clk_get_hw_from_provider()
5131 return provider->get_hw(clkspec, provider->data); in __of_clk_get_hw_from_provider()
5133 clk = provider->get(clkspec, provider->data); in __of_clk_get_hw_from_provider()
5134 if (IS_ERR(clk)) in __of_clk_get_hw_from_provider()
5135 return ERR_CAST(clk); in __of_clk_get_hw_from_provider()
5136 return __clk_get_hw(clk); in __of_clk_get_hw_from_provider()
5143 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER); in of_clk_get_hw_from_clkspec()
5146 return ERR_PTR(-EINVAL); in of_clk_get_hw_from_clkspec()
5150 if (provider->node == clkspec->np) { in of_clk_get_hw_from_clkspec()
5162 * of_clk_get_from_provider() - Lookup a clock from a clock provider
5165 * This function looks up a struct clk from the registered list of clock
5169 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) in of_clk_get_from_provider()
5194 static struct clk *__of_clk_get(struct device_node *np, in __of_clk_get()
5203 struct clk *of_clk_get(struct device_node *np, int index) in of_clk_get()
5205 return __of_clk_get(np, index, np->full_name, NULL); in of_clk_get()
5210 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
5214 * This function parses the clocks and clock-names properties,
5215 * and uses them to look up the struct clk from the registered list of clock
5218 struct clk *of_clk_get_by_name(struct device_node *np, const char *name) in of_clk_get_by_name()
5221 return ERR_PTR(-ENOENT); in of_clk_get_by_name()
5223 return __of_clk_get(np, 0, np->full_name, name); in of_clk_get_by_name()
5228 * of_clk_get_parent_count() - Count the number of clocks a device node has
5237 count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); in of_clk_get_parent_count()
5254 struct clk *clk; in of_clk_get_parent_name() local
5256 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, in of_clk_get_parent_name()
5265 * specified into an array offset for the clock-output-names property. in of_clk_get_parent_name()
5267 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { in of_clk_get_parent_name()
5274 /* We went off the end of 'clock-indices' without finding it */ in of_clk_get_parent_name()
5278 if (of_property_read_string_index(clkspec.np, "clock-output-names", in of_clk_get_parent_name()
5285 * the clock as long as #clock-cells = 0. in of_clk_get_parent_name()
5287 clk = of_clk_get_from_provider(&clkspec); in of_clk_get_parent_name()
5288 if (IS_ERR(clk)) { in of_clk_get_parent_name()
5290 clk_name = clkspec.np->name; in of_clk_get_parent_name()
5294 clk_name = __clk_get_name(clk); in of_clk_get_parent_name()
5295 clk_put(clk); in of_clk_get_parent_name()
5306 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
5342 struct clk *clk = of_clk_get(np, i); in parent_ready() local
5345 if (!IS_ERR(clk)) { in parent_ready()
5346 clk_put(clk); in parent_ready()
5352 if (PTR_ERR(clk) == -EPROBE_DEFER) in parent_ready()
5368 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
5371 * @flags: pointer to top-level framework flags
5373 * Detects if the clock-critical property exists and, if so, sets the
5377 * bindings, such as the one-clock-per-node style that are outdated.
5393 return -EINVAL; in of_clk_detect_critical()
5395 of_property_for_each_u32(np, "clock-critical", prop, cur, idx) in of_clk_detect_critical()
5403 * of_clk_init() - Scan and init clock providers from the DT
5433 list_del(&clk_provider->node); in of_clk_init()
5434 of_node_put(clk_provider->np); in of_clk_init()
5441 parent->clk_init_cb = match->data; in of_clk_init()
5442 parent->np = of_node_get(np); in of_clk_init()
5443 list_add_tail(&parent->node, &clk_provider_list); in of_clk_init()
5450 if (force || parent_ready(clk_provider->np)) { in of_clk_init()
5453 of_node_set_flag(clk_provider->np, in of_clk_init()
5456 clk_provider->clk_init_cb(clk_provider->np); in of_clk_init()
5457 of_clk_set_defaults(clk_provider->np, true); in of_clk_init()
5459 list_del(&clk_provider->node); in of_clk_init()
5460 of_node_put(clk_provider->np); in of_clk_init()