1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
7 #include <linux/kernel.h>
8 #include <linux/bitops.h>
10 #include <linux/delay.h>
11 #include <linux/export.h>
12 #include <linux/clk-provider.h>
13 #include <linux/regmap.h>
15 #include "clk-branch.h"
17 static bool clk_branch_in_hwcg_mode(const struct clk_branch *br)
24 regmap_read(br->clkr.regmap, br->hwcg_reg, &val);
26 return !!(val & BIT(br->hwcg_bit));
29 static bool clk_branch_check_halt(const struct clk_branch *br, bool enabling)
31 bool invert = (br->halt_check == BRANCH_HALT_ENABLE);
34 regmap_read(br->clkr.regmap, br->halt_reg, &val);
36 val &= BIT(br->halt_bit);
40 return !!val == !enabling;
43 static bool clk_branch2_check_halt(const struct clk_branch *br, bool enabling)
47 bool invert = (br->halt_check == BRANCH_HALT_ENABLE);
49 mask = CBCR_NOC_FSM_STATUS;
52 regmap_read(br->clkr.regmap, br->halt_reg, &val);
56 return (val & CBCR_CLK_OFF) == (invert ? CBCR_CLK_OFF : 0) ||
57 FIELD_GET(CBCR_NOC_FSM_STATUS, val) == FSM_STATUS_ON;
59 return (val & CBCR_CLK_OFF) == (invert ? 0 : CBCR_CLK_OFF);
62 static int clk_branch_wait(const struct clk_branch *br, bool enabling,
63 bool (check_halt)(const struct clk_branch *, bool))
65 bool voted = br->halt_check & BRANCH_VOTED;
66 const char *name = clk_hw_get_name(&br->clkr.hw);
69 * Skip checking halt bit if we're explicitly ignoring the bit or the
70 * clock is in hardware gated mode
72 if (br->halt_check == BRANCH_HALT_SKIP || clk_branch_in_hwcg_mode(br))
75 if (br->halt_check == BRANCH_HALT_DELAY || (!enabling && voted)) {
77 } else if (br->halt_check == BRANCH_HALT_ENABLE ||
78 br->halt_check == BRANCH_HALT ||
79 (enabling && voted)) {
83 if (check_halt(br, enabling))
87 WARN(1, "%s status stuck at 'o%s'", name,
88 enabling ? "ff" : "n");
94 static int clk_branch_toggle(struct clk_hw *hw, bool en,
95 bool (check_halt)(const struct clk_branch *, bool))
97 struct clk_branch *br = to_clk_branch(hw);
101 ret = clk_enable_regmap(hw);
105 clk_disable_regmap(hw);
108 return clk_branch_wait(br, en, check_halt);
111 static int clk_branch_enable(struct clk_hw *hw)
113 return clk_branch_toggle(hw, true, clk_branch_check_halt);
116 static void clk_branch_disable(struct clk_hw *hw)
118 clk_branch_toggle(hw, false, clk_branch_check_halt);
121 const struct clk_ops clk_branch_ops = {
122 .enable = clk_branch_enable,
123 .disable = clk_branch_disable,
124 .is_enabled = clk_is_enabled_regmap,
126 EXPORT_SYMBOL_GPL(clk_branch_ops);
128 static int clk_branch2_enable(struct clk_hw *hw)
130 return clk_branch_toggle(hw, true, clk_branch2_check_halt);
133 static void clk_branch2_disable(struct clk_hw *hw)
135 clk_branch_toggle(hw, false, clk_branch2_check_halt);
138 static int clk_branch2_mem_enable(struct clk_hw *hw)
140 struct clk_mem_branch *mem_br = to_clk_mem_branch(hw);
141 struct clk_branch branch = mem_br->branch;
145 regmap_update_bits(branch.clkr.regmap, mem_br->mem_enable_reg,
146 mem_br->mem_enable_ack_mask, mem_br->mem_enable_ack_mask);
148 ret = regmap_read_poll_timeout(branch.clkr.regmap, mem_br->mem_ack_reg,
149 val, val & mem_br->mem_enable_ack_mask, 0, 200);
151 WARN(1, "%s mem enable failed\n", clk_hw_get_name(&branch.clkr.hw));
155 return clk_branch2_enable(hw);
158 static void clk_branch2_mem_disable(struct clk_hw *hw)
160 struct clk_mem_branch *mem_br = to_clk_mem_branch(hw);
162 regmap_update_bits(mem_br->branch.clkr.regmap, mem_br->mem_enable_reg,
163 mem_br->mem_enable_ack_mask, 0);
165 return clk_branch2_disable(hw);
168 const struct clk_ops clk_branch2_mem_ops = {
169 .enable = clk_branch2_mem_enable,
170 .disable = clk_branch2_mem_disable,
171 .is_enabled = clk_is_enabled_regmap,
173 EXPORT_SYMBOL_GPL(clk_branch2_mem_ops);
175 const struct clk_ops clk_branch2_ops = {
176 .enable = clk_branch2_enable,
177 .disable = clk_branch2_disable,
178 .is_enabled = clk_is_enabled_regmap,
180 EXPORT_SYMBOL_GPL(clk_branch2_ops);
182 const struct clk_ops clk_branch2_aon_ops = {
183 .enable = clk_branch2_enable,
184 .is_enabled = clk_is_enabled_regmap,
186 EXPORT_SYMBOL_GPL(clk_branch2_aon_ops);
188 const struct clk_ops clk_branch_simple_ops = {
189 .enable = clk_enable_regmap,
190 .disable = clk_disable_regmap,
191 .is_enabled = clk_is_enabled_regmap,
193 EXPORT_SYMBOL_GPL(clk_branch_simple_ops);
195 const struct clk_ops clk_branch2_prepare_ops = {
196 .prepare = clk_branch2_enable,
197 .unprepare = clk_branch2_disable,
198 .is_prepared = clk_is_enabled_regmap,
200 EXPORT_SYMBOL_GPL(clk_branch2_prepare_ops);