1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 MediaTek Inc.
4 * Author: Owen Chen <owen.chen@mediatek.com>
8 #include <linux/clk-provider.h>
9 #include <linux/compiler_types.h>
10 #include <linux/container_of.h>
11 #include <linux/err.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/spinlock.h>
16 #include <linux/slab.h>
22 struct regmap *regmap;
23 const struct mtk_mux *data;
28 static inline struct mtk_clk_mux *to_mtk_clk_mux(struct clk_hw *hw)
30 return container_of(hw, struct mtk_clk_mux, hw);
33 static int mtk_clk_mux_enable_setclr(struct clk_hw *hw)
35 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
36 unsigned long flags = 0;
39 spin_lock_irqsave(mux->lock, flags);
43 regmap_write(mux->regmap, mux->data->clr_ofs,
44 BIT(mux->data->gate_shift));
47 * If the parent has been changed when the clock was disabled, it will
48 * not be effective yet. Set the update bit to ensure the mux gets
51 if (mux->reparent && mux->data->upd_shift >= 0) {
52 regmap_write(mux->regmap, mux->data->upd_ofs,
53 BIT(mux->data->upd_shift));
54 mux->reparent = false;
58 spin_unlock_irqrestore(mux->lock, flags);
65 static void mtk_clk_mux_disable_setclr(struct clk_hw *hw)
67 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
69 regmap_write(mux->regmap, mux->data->set_ofs,
70 BIT(mux->data->gate_shift));
73 static int mtk_clk_mux_is_enabled(struct clk_hw *hw)
75 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
78 regmap_read(mux->regmap, mux->data->mux_ofs, &val);
80 return (val & BIT(mux->data->gate_shift)) == 0;
83 static u8 mtk_clk_mux_get_parent(struct clk_hw *hw)
85 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
86 u32 mask = GENMASK(mux->data->mux_width - 1, 0);
89 regmap_read(mux->regmap, mux->data->mux_ofs, &val);
90 val = (val >> mux->data->mux_shift) & mask;
95 static int mtk_clk_mux_set_parent_setclr_lock(struct clk_hw *hw, u8 index)
97 struct mtk_clk_mux *mux = to_mtk_clk_mux(hw);
98 u32 mask = GENMASK(mux->data->mux_width - 1, 0);
100 unsigned long flags = 0;
103 spin_lock_irqsave(mux->lock, flags);
105 __acquire(mux->lock);
107 regmap_read(mux->regmap, mux->data->mux_ofs, &orig);
108 val = (orig & ~(mask << mux->data->mux_shift))
109 | (index << mux->data->mux_shift);
112 regmap_write(mux->regmap, mux->data->clr_ofs,
113 mask << mux->data->mux_shift);
114 regmap_write(mux->regmap, mux->data->set_ofs,
115 index << mux->data->mux_shift);
117 if (mux->data->upd_shift >= 0) {
118 regmap_write(mux->regmap, mux->data->upd_ofs,
119 BIT(mux->data->upd_shift));
120 mux->reparent = true;
125 spin_unlock_irqrestore(mux->lock, flags);
127 __release(mux->lock);
132 const struct clk_ops mtk_mux_clr_set_upd_ops = {
133 .get_parent = mtk_clk_mux_get_parent,
134 .set_parent = mtk_clk_mux_set_parent_setclr_lock,
136 EXPORT_SYMBOL_GPL(mtk_mux_clr_set_upd_ops);
138 const struct clk_ops mtk_mux_gate_clr_set_upd_ops = {
139 .enable = mtk_clk_mux_enable_setclr,
140 .disable = mtk_clk_mux_disable_setclr,
141 .is_enabled = mtk_clk_mux_is_enabled,
142 .get_parent = mtk_clk_mux_get_parent,
143 .set_parent = mtk_clk_mux_set_parent_setclr_lock,
145 EXPORT_SYMBOL_GPL(mtk_mux_gate_clr_set_upd_ops);
147 static struct clk_hw *mtk_clk_register_mux(const struct mtk_mux *mux,
148 struct regmap *regmap,
151 struct mtk_clk_mux *clk_mux;
152 struct clk_init_data init = {};
155 clk_mux = kzalloc(sizeof(*clk_mux), GFP_KERNEL);
157 return ERR_PTR(-ENOMEM);
159 init.name = mux->name;
160 init.flags = mux->flags | CLK_SET_RATE_PARENT;
161 init.parent_names = mux->parent_names;
162 init.num_parents = mux->num_parents;
165 clk_mux->regmap = regmap;
167 clk_mux->lock = lock;
168 clk_mux->hw.init = &init;
170 ret = clk_hw_register(NULL, &clk_mux->hw);
179 static void mtk_clk_unregister_mux(struct clk_hw *hw)
181 struct mtk_clk_mux *mux;
185 mux = to_mtk_clk_mux(hw);
187 clk_hw_unregister(hw);
191 int mtk_clk_register_muxes(const struct mtk_mux *muxes,
192 int num, struct device_node *node,
194 struct clk_hw_onecell_data *clk_data)
196 struct regmap *regmap;
200 regmap = device_node_to_regmap(node);
201 if (IS_ERR(regmap)) {
202 pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
203 return PTR_ERR(regmap);
206 for (i = 0; i < num; i++) {
207 const struct mtk_mux *mux = &muxes[i];
209 if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
210 pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
215 hw = mtk_clk_register_mux(mux, regmap, lock);
218 pr_err("Failed to register clk %s: %pe\n", mux->name,
223 clk_data->hws[mux->id] = hw;
230 const struct mtk_mux *mux = &muxes[i];
232 if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
235 mtk_clk_unregister_mux(clk_data->hws[mux->id]);
236 clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
241 EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);
243 void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
244 struct clk_hw_onecell_data *clk_data)
251 for (i = num; i > 0; i--) {
252 const struct mtk_mux *mux = &muxes[i - 1];
254 if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
257 mtk_clk_unregister_mux(clk_data->hws[mux->id]);
258 clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
261 EXPORT_SYMBOL_GPL(mtk_clk_unregister_muxes);
264 * This clock notifier is called when the frequency of the parent
265 * PLL clock is to be changed. The idea is to switch the parent to a
266 * stable clock, such as the main oscillator, while the PLL frequency
269 static int mtk_clk_mux_notifier_cb(struct notifier_block *nb,
270 unsigned long event, void *_data)
272 struct clk_notifier_data *data = _data;
273 struct clk_hw *hw = __clk_get_hw(data->clk);
274 struct mtk_mux_nb *mux_nb = to_mtk_mux_nb(nb);
278 case PRE_RATE_CHANGE:
279 mux_nb->original_index = mux_nb->ops->get_parent(hw);
280 ret = mux_nb->ops->set_parent(hw, mux_nb->bypass_index);
282 case POST_RATE_CHANGE:
283 case ABORT_RATE_CHANGE:
284 ret = mux_nb->ops->set_parent(hw, mux_nb->original_index);
288 return notifier_from_errno(ret);
291 int devm_mtk_clk_mux_notifier_register(struct device *dev, struct clk *clk,
292 struct mtk_mux_nb *mux_nb)
294 mux_nb->nb.notifier_call = mtk_clk_mux_notifier_cb;
296 return devm_clk_notifier_register(dev, clk, &mux_nb->nb);
298 EXPORT_SYMBOL_GPL(devm_mtk_clk_mux_notifier_register);
300 MODULE_LICENSE("GPL");