1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 MediaTek Inc.
7 #include <linux/clk-provider.h>
8 #include <linux/compiler_types.h>
9 #include <linux/container_of.h>
10 #include <linux/err.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/spinlock.h>
15 #include <linux/slab.h>
21 struct regmap *regmap;
22 const struct mtk_mux *data;
27 static inline struct mtk_clk_mux *to_mtk_clk_mux(struct clk_hw *hw)
29 return container_of(hw, struct mtk_clk_mux, hw);
32 static int mtk_clk_mux_enable_setclr(struct clk_hw *hw)
34 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
35 unsigned long flags = 0;
38 spin_lock_irqsave(mux->lock, flags);
42 regmap_write(mux->regmap, mux->data->clr_ofs,
43 BIT(mux->data->gate_shift));
46 * If the parent has been changed when the clock was disabled, it will
47 * not be effective yet. Set the update bit to ensure the mux gets
50 if (mux->reparent && mux->data->upd_shift >= 0) {
51 regmap_write(mux->regmap, mux->data->upd_ofs,
52 BIT(mux->data->upd_shift));
53 mux->reparent = false;
57 spin_unlock_irqrestore(mux->lock, flags);
64 static void mtk_clk_mux_disable_setclr(struct clk_hw *hw)
66 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
68 regmap_write(mux->regmap, mux->data->set_ofs,
69 BIT(mux->data->gate_shift));
72 static int mtk_clk_mux_is_enabled(struct clk_hw *hw)
74 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
77 regmap_read(mux->regmap, mux->data->mux_ofs, &val);
79 return (val & BIT(mux->data->gate_shift)) == 0;
82 static u8 mtk_clk_mux_get_parent(struct clk_hw *hw)
84 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
85 u32 mask = GENMASK(mux->data->mux_width - 1, 0);
88 regmap_read(mux->regmap, mux->data->mux_ofs, &val);
89 val = (val >> mux->data->mux_shift) & mask;
94 static int mtk_clk_mux_set_parent_setclr_lock(struct clk_hw *hw, u8 index)
96 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
97 u32 mask = GENMASK(mux->data->mux_width - 1, 0);
99 unsigned long flags = 0;
102 spin_lock_irqsave(mux->lock, flags);
104 __acquire(mux->lock);
106 regmap_read(mux->regmap, mux->data->mux_ofs, &orig);
107 val = (orig & ~(mask << mux->data->mux_shift))
108 | (index << mux->data->mux_shift);
111 regmap_write(mux->regmap, mux->data->clr_ofs,
112 mask << mux->data->mux_shift);
113 regmap_write(mux->regmap, mux->data->set_ofs,
114 index << mux->data->mux_shift);
116 if (mux->data->upd_shift >= 0) {
117 regmap_write(mux->regmap, mux->data->upd_ofs,
118 BIT(mux->data->upd_shift));
119 mux->reparent = true;
124 spin_unlock_irqrestore(mux->lock, flags);
126 __release(mux->lock);
131 const struct clk_ops mtk_mux_clr_set_upd_ops = {
132 .get_parent = mtk_clk_mux_get_parent,
133 .set_parent = mtk_clk_mux_set_parent_setclr_lock,
135 EXPORT_SYMBOL_GPL(mtk_mux_clr_set_upd_ops);
137 const struct clk_ops mtk_mux_gate_clr_set_upd_ops = {
138 .enable = mtk_clk_mux_enable_setclr,
139 .disable = mtk_clk_mux_disable_setclr,
140 .is_enabled = mtk_clk_mux_is_enabled,
141 .get_parent = mtk_clk_mux_get_parent,
142 .set_parent = mtk_clk_mux_set_parent_setclr_lock,
144 EXPORT_SYMBOL_GPL(mtk_mux_gate_clr_set_upd_ops);
146 static struct clk_hw *mtk_clk_register_mux(const struct mtk_mux *mux,
147 struct regmap *regmap,
150 struct mtk_clk_mux *clk_mux;
151 struct clk_init_data init = {};
154 clk_mux = kzalloc(sizeof(*clk_mux), GFP_KERNEL);
156 return ERR_PTR(-ENOMEM);
158 init.name = mux->name;
159 init.flags = mux->flags | CLK_SET_RATE_PARENT;
160 init.parent_names = mux->parent_names;
161 init.num_parents = mux->num_parents;
164 clk_mux->regmap = regmap;
166 clk_mux->lock = lock;
167 clk_mux->hw.init = &init;
169 ret = clk_hw_register(NULL, &clk_mux->hw);
178 static void mtk_clk_unregister_mux(struct clk_hw *hw)
180 struct mtk_clk_mux *mux;
184 mux = to_mtk_clk_mux(hw);
186 clk_hw_unregister(hw);
190 int mtk_clk_register_muxes(const struct mtk_mux *muxes,
191 int num, struct device_node *node,
193 struct clk_hw_onecell_data *clk_data)
195 struct regmap *regmap;
199 regmap = device_node_to_regmap(node);
200 if (IS_ERR(regmap)) {
201 pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
202 return PTR_ERR(regmap);
205 for (i = 0; i < num; i++) {
206 const struct mtk_mux *mux = &muxes[i];
208 if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
209 pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
214 hw = mtk_clk_register_mux(mux, regmap, lock);
217 pr_err("Failed to register clk %s: %pe\n", mux->name,
222 clk_data->hws[mux->id] = hw;
229 const struct mtk_mux *mux = &muxes[i];
231 if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
234 mtk_clk_unregister_mux(clk_data->hws[mux->id]);
235 clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
240 EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);
242 void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
243 struct clk_hw_onecell_data *clk_data)
250 for (i = num; i > 0; i--) {
251 const struct mtk_mux *mux = &muxes[i - 1];
253 if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
256 mtk_clk_unregister_mux(clk_data->hws[mux->id]);
257 clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
260 EXPORT_SYMBOL_GPL(mtk_clk_unregister_muxes);
262 MODULE_LICENSE("GPL");