1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: James Liao <jamesjj.liao@mediatek.com>
7 #include <linux/clk-provider.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/printk.h>
11 #include <linux/regmap.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
19 struct regmap *regmap;
26 static inline struct mtk_clk_gate *to_mtk_clk_gate(struct clk_hw *hw)
28 return container_of(hw, struct mtk_clk_gate, hw);
31 static u32 mtk_get_clockgating(struct clk_hw *hw)
33 struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
36 regmap_read(cg->regmap, cg->sta_ofs, &val);
38 return val & BIT(cg->bit);
41 static int mtk_cg_bit_is_cleared(struct clk_hw *hw)
43 return mtk_get_clockgating(hw) == 0;
46 static int mtk_cg_bit_is_set(struct clk_hw *hw)
48 return mtk_get_clockgating(hw) != 0;
51 static void mtk_cg_set_bit(struct clk_hw *hw)
53 struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
55 regmap_write(cg->regmap, cg->set_ofs, BIT(cg->bit));
58 static void mtk_cg_clr_bit(struct clk_hw *hw)
60 struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
62 regmap_write(cg->regmap, cg->clr_ofs, BIT(cg->bit));
65 static void mtk_cg_set_bit_no_setclr(struct clk_hw *hw)
67 struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
69 regmap_set_bits(cg->regmap, cg->sta_ofs, BIT(cg->bit));
72 static void mtk_cg_clr_bit_no_setclr(struct clk_hw *hw)
74 struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
76 regmap_clear_bits(cg->regmap, cg->sta_ofs, BIT(cg->bit));
79 static int mtk_cg_enable(struct clk_hw *hw)
86 static void mtk_cg_disable(struct clk_hw *hw)
91 static int mtk_cg_enable_inv(struct clk_hw *hw)
98 static void mtk_cg_disable_inv(struct clk_hw *hw)
103 static int mtk_cg_enable_no_setclr(struct clk_hw *hw)
105 mtk_cg_clr_bit_no_setclr(hw);
110 static void mtk_cg_disable_no_setclr(struct clk_hw *hw)
112 mtk_cg_set_bit_no_setclr(hw);
115 static int mtk_cg_enable_inv_no_setclr(struct clk_hw *hw)
117 mtk_cg_set_bit_no_setclr(hw);
122 static void mtk_cg_disable_inv_no_setclr(struct clk_hw *hw)
124 mtk_cg_clr_bit_no_setclr(hw);
127 const struct clk_ops mtk_clk_gate_ops_setclr = {
128 .is_enabled = mtk_cg_bit_is_cleared,
129 .enable = mtk_cg_enable,
130 .disable = mtk_cg_disable,
132 EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_setclr);
134 const struct clk_ops mtk_clk_gate_ops_setclr_inv = {
135 .is_enabled = mtk_cg_bit_is_set,
136 .enable = mtk_cg_enable_inv,
137 .disable = mtk_cg_disable_inv,
139 EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_setclr_inv);
141 const struct clk_ops mtk_clk_gate_ops_no_setclr = {
142 .is_enabled = mtk_cg_bit_is_cleared,
143 .enable = mtk_cg_enable_no_setclr,
144 .disable = mtk_cg_disable_no_setclr,
146 EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_no_setclr);
148 const struct clk_ops mtk_clk_gate_ops_no_setclr_inv = {
149 .is_enabled = mtk_cg_bit_is_set,
150 .enable = mtk_cg_enable_inv_no_setclr,
151 .disable = mtk_cg_disable_inv_no_setclr,
153 EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_no_setclr_inv);
155 static struct clk_hw *mtk_clk_register_gate(const char *name,
156 const char *parent_name,
157 struct regmap *regmap, int set_ofs,
158 int clr_ofs, int sta_ofs, u8 bit,
159 const struct clk_ops *ops,
160 unsigned long flags, struct device *dev)
162 struct mtk_clk_gate *cg;
164 struct clk_init_data init = {};
166 cg = kzalloc(sizeof(*cg), GFP_KERNEL);
168 return ERR_PTR(-ENOMEM);
171 init.flags = flags | CLK_SET_RATE_PARENT;
172 init.parent_names = parent_name ? &parent_name : NULL;
173 init.num_parents = parent_name ? 1 : 0;
177 cg->set_ofs = set_ofs;
178 cg->clr_ofs = clr_ofs;
179 cg->sta_ofs = sta_ofs;
184 ret = clk_hw_register(dev, &cg->hw);
193 static void mtk_clk_unregister_gate(struct clk_hw *hw)
195 struct mtk_clk_gate *cg;
199 cg = to_mtk_clk_gate(hw);
201 clk_hw_unregister(hw);
205 int mtk_clk_register_gates_with_dev(struct device_node *node,
206 const struct mtk_gate *clks, int num,
207 struct clk_hw_onecell_data *clk_data,
212 struct regmap *regmap;
217 regmap = device_node_to_regmap(node);
218 if (IS_ERR(regmap)) {
219 pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
220 return PTR_ERR(regmap);
223 for (i = 0; i < num; i++) {
224 const struct mtk_gate *gate = &clks[i];
226 if (!IS_ERR_OR_NULL(clk_data->hws[gate->id])) {
227 pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
232 hw = mtk_clk_register_gate(gate->name, gate->parent_name,
237 gate->shift, gate->ops,
241 pr_err("Failed to register clk %s: %pe\n", gate->name,
246 clk_data->hws[gate->id] = hw;
253 const struct mtk_gate *gate = &clks[i];
255 if (IS_ERR_OR_NULL(clk_data->hws[gate->id]))
258 mtk_clk_unregister_gate(clk_data->hws[gate->id]);
259 clk_data->hws[gate->id] = ERR_PTR(-ENOENT);
264 EXPORT_SYMBOL_GPL(mtk_clk_register_gates_with_dev);
266 int mtk_clk_register_gates(struct device_node *node,
267 const struct mtk_gate *clks, int num,
268 struct clk_hw_onecell_data *clk_data)
270 return mtk_clk_register_gates_with_dev(node, clks, num, clk_data, NULL);
272 EXPORT_SYMBOL_GPL(mtk_clk_register_gates);
274 void mtk_clk_unregister_gates(const struct mtk_gate *clks, int num,
275 struct clk_hw_onecell_data *clk_data)
282 for (i = num; i > 0; i--) {
283 const struct mtk_gate *gate = &clks[i - 1];
285 if (IS_ERR_OR_NULL(clk_data->hws[gate->id]))
288 mtk_clk_unregister_gate(clk_data->hws[gate->id]);
289 clk_data->hws[gate->id] = ERR_PTR(-ENOENT);
292 EXPORT_SYMBOL_GPL(mtk_clk_unregister_gates);
294 MODULE_LICENSE("GPL");