1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
7 #include <linux/bitops.h>
8 #include <linux/clk-provider.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
21 static void mtk_init_clk_data(struct clk_hw_onecell_data *clk_data,
26 clk_data->num = clk_num;
28 for (i = 0; i < clk_num; i++)
29 clk_data->hws[i] = ERR_PTR(-ENOENT);
32 struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
35 struct clk_hw_onecell_data *clk_data;
37 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, clk_num),
42 mtk_init_clk_data(clk_data, clk_num);
46 EXPORT_SYMBOL_GPL(mtk_devm_alloc_clk_data);
48 struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
50 struct clk_hw_onecell_data *clk_data;
52 clk_data = kzalloc(struct_size(clk_data, hws, clk_num), GFP_KERNEL);
56 mtk_init_clk_data(clk_data, clk_num);
60 EXPORT_SYMBOL_GPL(mtk_alloc_clk_data);
62 void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data)
66 EXPORT_SYMBOL_GPL(mtk_free_clk_data);
68 int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
69 struct clk_hw_onecell_data *clk_data)
77 for (i = 0; i < num; i++) {
78 const struct mtk_fixed_clk *rc = &clks[i];
80 if (!IS_ERR_OR_NULL(clk_data->hws[rc->id])) {
81 pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
85 hw = clk_hw_register_fixed_rate(NULL, rc->name, rc->parent, 0,
89 pr_err("Failed to register clk %s: %pe\n", rc->name,
94 clk_data->hws[rc->id] = hw;
101 const struct mtk_fixed_clk *rc = &clks[i];
103 if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
106 clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
107 clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
112 EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
114 void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
115 struct clk_hw_onecell_data *clk_data)
122 for (i = num; i > 0; i--) {
123 const struct mtk_fixed_clk *rc = &clks[i - 1];
125 if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
128 clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
129 clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
132 EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
134 int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
135 struct clk_hw_onecell_data *clk_data)
143 for (i = 0; i < num; i++) {
144 const struct mtk_fixed_factor *ff = &clks[i];
146 if (!IS_ERR_OR_NULL(clk_data->hws[ff->id])) {
147 pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
151 hw = clk_hw_register_fixed_factor(NULL, ff->name, ff->parent_name,
152 ff->flags, ff->mult, ff->div);
155 pr_err("Failed to register clk %s: %pe\n", ff->name,
160 clk_data->hws[ff->id] = hw;
167 const struct mtk_fixed_factor *ff = &clks[i];
169 if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
172 clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
173 clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
178 EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
180 void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
181 struct clk_hw_onecell_data *clk_data)
188 for (i = num; i > 0; i--) {
189 const struct mtk_fixed_factor *ff = &clks[i - 1];
191 if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
194 clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
195 clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
198 EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
200 static struct clk_hw *mtk_clk_register_composite(const struct mtk_composite *mc,
201 void __iomem *base, spinlock_t *lock)
204 struct clk_mux *mux = NULL;
205 struct clk_gate *gate = NULL;
206 struct clk_divider *div = NULL;
207 struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
208 const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
209 const char * const *parent_names;
214 if (mc->mux_shift >= 0) {
215 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
217 return ERR_PTR(-ENOMEM);
219 mux->reg = base + mc->mux_reg;
220 mux->mask = BIT(mc->mux_width) - 1;
221 mux->shift = mc->mux_shift;
223 mux->flags = mc->mux_flags;
225 mux_ops = &clk_mux_ops;
227 parent_names = mc->parent_names;
228 num_parents = mc->num_parents;
231 parent_names = &parent;
235 if (mc->gate_shift >= 0) {
236 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
242 gate->reg = base + mc->gate_reg;
243 gate->bit_idx = mc->gate_shift;
244 gate->flags = CLK_GATE_SET_TO_DISABLE;
248 gate_ops = &clk_gate_ops;
251 if (mc->divider_shift >= 0) {
252 div = kzalloc(sizeof(*div), GFP_KERNEL);
258 div->reg = base + mc->divider_reg;
259 div->shift = mc->divider_shift;
260 div->width = mc->divider_width;
264 div_ops = &clk_divider_ops;
267 hw = clk_hw_register_composite(NULL, mc->name, parent_names, num_parents,
287 static void mtk_clk_unregister_composite(struct clk_hw *hw)
289 struct clk_composite *composite;
290 struct clk_mux *mux = NULL;
291 struct clk_gate *gate = NULL;
292 struct clk_divider *div = NULL;
297 composite = to_clk_composite(hw);
298 if (composite->mux_hw)
299 mux = to_clk_mux(composite->mux_hw);
300 if (composite->gate_hw)
301 gate = to_clk_gate(composite->gate_hw);
302 if (composite->rate_hw)
303 div = to_clk_divider(composite->rate_hw);
305 clk_hw_unregister_composite(hw);
311 int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
312 void __iomem *base, spinlock_t *lock,
313 struct clk_hw_onecell_data *clk_data)
321 for (i = 0; i < num; i++) {
322 const struct mtk_composite *mc = &mcs[i];
324 if (!IS_ERR_OR_NULL(clk_data->hws[mc->id])) {
325 pr_warn("Trying to register duplicate clock ID: %d\n",
330 hw = mtk_clk_register_composite(mc, base, lock);
333 pr_err("Failed to register clk %s: %pe\n", mc->name,
338 clk_data->hws[mc->id] = hw;
345 const struct mtk_composite *mc = &mcs[i];
347 if (IS_ERR_OR_NULL(clk_data->hws[mcs->id]))
350 mtk_clk_unregister_composite(clk_data->hws[mc->id]);
351 clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
356 EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
358 void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
359 struct clk_hw_onecell_data *clk_data)
366 for (i = num; i > 0; i--) {
367 const struct mtk_composite *mc = &mcs[i - 1];
369 if (IS_ERR_OR_NULL(clk_data->hws[mc->id]))
372 mtk_clk_unregister_composite(clk_data->hws[mc->id]);
373 clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
376 EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
378 int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
379 void __iomem *base, spinlock_t *lock,
380 struct clk_hw_onecell_data *clk_data)
388 for (i = 0; i < num; i++) {
389 const struct mtk_clk_divider *mcd = &mcds[i];
391 if (!IS_ERR_OR_NULL(clk_data->hws[mcd->id])) {
392 pr_warn("Trying to register duplicate clock ID: %d\n",
397 hw = clk_hw_register_divider(NULL, mcd->name, mcd->parent_name,
398 mcd->flags, base + mcd->div_reg, mcd->div_shift,
399 mcd->div_width, mcd->clk_divider_flags, lock);
402 pr_err("Failed to register clk %s: %pe\n", mcd->name,
407 clk_data->hws[mcd->id] = hw;
414 const struct mtk_clk_divider *mcd = &mcds[i];
416 if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
419 clk_hw_unregister_divider(clk_data->hws[mcd->id]);
420 clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
425 EXPORT_SYMBOL_GPL(mtk_clk_register_dividers);
427 void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
428 struct clk_hw_onecell_data *clk_data)
435 for (i = num; i > 0; i--) {
436 const struct mtk_clk_divider *mcd = &mcds[i - 1];
438 if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
441 clk_hw_unregister_divider(clk_data->hws[mcd->id]);
442 clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
445 EXPORT_SYMBOL_GPL(mtk_clk_unregister_dividers);
447 int mtk_clk_simple_probe(struct platform_device *pdev)
449 const struct mtk_clk_desc *mcd;
450 struct clk_hw_onecell_data *clk_data;
451 struct device_node *node = pdev->dev.of_node;
454 mcd = of_device_get_match_data(&pdev->dev);
458 clk_data = mtk_alloc_clk_data(mcd->num_clks);
462 r = mtk_clk_register_gates_with_dev(node, mcd->clks, mcd->num_clks,
463 clk_data, &pdev->dev);
467 r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
469 goto unregister_clks;
471 platform_set_drvdata(pdev, clk_data);
474 r = mtk_register_reset_controller_with_dev(&pdev->dev,
477 goto unregister_clks;
483 mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
485 mtk_free_clk_data(clk_data);
488 EXPORT_SYMBOL_GPL(mtk_clk_simple_probe);
490 int mtk_clk_simple_remove(struct platform_device *pdev)
492 const struct mtk_clk_desc *mcd = of_device_get_match_data(&pdev->dev);
493 struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
494 struct device_node *node = pdev->dev.of_node;
496 of_clk_del_provider(node);
497 mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
498 mtk_free_clk_data(clk_data);
502 EXPORT_SYMBOL_GPL(mtk_clk_simple_remove);
504 MODULE_LICENSE("GPL");