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_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
22 const struct mtk_gate_regs cg_regs_dummy = { 0, 0, 0 };
23 EXPORT_SYMBOL_GPL(cg_regs_dummy);
25 static int mtk_clk_dummy_enable(struct clk_hw *hw)
30 static void mtk_clk_dummy_disable(struct clk_hw *hw) { }
32 const struct clk_ops mtk_clk_dummy_ops = {
33 .enable = mtk_clk_dummy_enable,
34 .disable = mtk_clk_dummy_disable,
36 EXPORT_SYMBOL_GPL(mtk_clk_dummy_ops);
38 static void mtk_init_clk_data(struct clk_hw_onecell_data *clk_data,
43 clk_data->num = clk_num;
45 for (i = 0; i < clk_num; i++)
46 clk_data->hws[i] = ERR_PTR(-ENOENT);
49 struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
52 struct clk_hw_onecell_data *clk_data;
54 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, clk_num),
59 mtk_init_clk_data(clk_data, clk_num);
63 EXPORT_SYMBOL_GPL(mtk_devm_alloc_clk_data);
65 struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
67 struct clk_hw_onecell_data *clk_data;
69 clk_data = kzalloc(struct_size(clk_data, hws, clk_num), GFP_KERNEL);
73 mtk_init_clk_data(clk_data, clk_num);
77 EXPORT_SYMBOL_GPL(mtk_alloc_clk_data);
79 void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data)
83 EXPORT_SYMBOL_GPL(mtk_free_clk_data);
85 int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
86 struct clk_hw_onecell_data *clk_data)
94 for (i = 0; i < num; i++) {
95 const struct mtk_fixed_clk *rc = &clks[i];
97 if (!IS_ERR_OR_NULL(clk_data->hws[rc->id])) {
98 pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
102 hw = clk_hw_register_fixed_rate(NULL, rc->name, rc->parent, 0,
106 pr_err("Failed to register clk %s: %pe\n", rc->name,
111 clk_data->hws[rc->id] = hw;
118 const struct mtk_fixed_clk *rc = &clks[i];
120 if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
123 clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
124 clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
129 EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
131 void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
132 struct clk_hw_onecell_data *clk_data)
139 for (i = num; i > 0; i--) {
140 const struct mtk_fixed_clk *rc = &clks[i - 1];
142 if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
145 clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
146 clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
149 EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
151 int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
152 struct clk_hw_onecell_data *clk_data)
160 for (i = 0; i < num; i++) {
161 const struct mtk_fixed_factor *ff = &clks[i];
163 if (!IS_ERR_OR_NULL(clk_data->hws[ff->id])) {
164 pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
168 hw = clk_hw_register_fixed_factor(NULL, ff->name, ff->parent_name,
169 ff->flags, ff->mult, ff->div);
172 pr_err("Failed to register clk %s: %pe\n", ff->name,
177 clk_data->hws[ff->id] = hw;
184 const struct mtk_fixed_factor *ff = &clks[i];
186 if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
189 clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
190 clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
195 EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
197 void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
198 struct clk_hw_onecell_data *clk_data)
205 for (i = num; i > 0; i--) {
206 const struct mtk_fixed_factor *ff = &clks[i - 1];
208 if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
211 clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
212 clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
215 EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
217 static struct clk_hw *mtk_clk_register_composite(struct device *dev,
218 const struct mtk_composite *mc, void __iomem *base, spinlock_t *lock)
221 struct clk_mux *mux = NULL;
222 struct clk_gate *gate = NULL;
223 struct clk_divider *div = NULL;
224 struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
225 const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
226 const char * const *parent_names;
231 if (mc->mux_shift >= 0) {
232 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
234 return ERR_PTR(-ENOMEM);
236 mux->reg = base + mc->mux_reg;
237 mux->mask = BIT(mc->mux_width) - 1;
238 mux->shift = mc->mux_shift;
240 mux->flags = mc->mux_flags;
242 mux_ops = &clk_mux_ops;
244 parent_names = mc->parent_names;
245 num_parents = mc->num_parents;
248 parent_names = &parent;
252 if (mc->gate_shift >= 0) {
253 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
259 gate->reg = base + mc->gate_reg;
260 gate->bit_idx = mc->gate_shift;
261 gate->flags = CLK_GATE_SET_TO_DISABLE;
265 gate_ops = &clk_gate_ops;
268 if (mc->divider_shift >= 0) {
269 div = kzalloc(sizeof(*div), GFP_KERNEL);
275 div->reg = base + mc->divider_reg;
276 div->shift = mc->divider_shift;
277 div->width = mc->divider_width;
281 div_ops = &clk_divider_ops;
284 hw = clk_hw_register_composite(dev, mc->name, parent_names, num_parents,
304 static void mtk_clk_unregister_composite(struct clk_hw *hw)
306 struct clk_composite *composite;
307 struct clk_mux *mux = NULL;
308 struct clk_gate *gate = NULL;
309 struct clk_divider *div = NULL;
314 composite = to_clk_composite(hw);
315 if (composite->mux_hw)
316 mux = to_clk_mux(composite->mux_hw);
317 if (composite->gate_hw)
318 gate = to_clk_gate(composite->gate_hw);
319 if (composite->rate_hw)
320 div = to_clk_divider(composite->rate_hw);
322 clk_hw_unregister_composite(hw);
328 int mtk_clk_register_composites(struct device *dev,
329 const struct mtk_composite *mcs, int num,
330 void __iomem *base, spinlock_t *lock,
331 struct clk_hw_onecell_data *clk_data)
339 for (i = 0; i < num; i++) {
340 const struct mtk_composite *mc = &mcs[i];
342 if (!IS_ERR_OR_NULL(clk_data->hws[mc->id])) {
343 pr_warn("Trying to register duplicate clock ID: %d\n",
348 hw = mtk_clk_register_composite(dev, mc, base, lock);
351 pr_err("Failed to register clk %s: %pe\n", mc->name,
356 clk_data->hws[mc->id] = hw;
363 const struct mtk_composite *mc = &mcs[i];
365 if (IS_ERR_OR_NULL(clk_data->hws[mcs->id]))
368 mtk_clk_unregister_composite(clk_data->hws[mc->id]);
369 clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
374 EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
376 void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
377 struct clk_hw_onecell_data *clk_data)
384 for (i = num; i > 0; i--) {
385 const struct mtk_composite *mc = &mcs[i - 1];
387 if (IS_ERR_OR_NULL(clk_data->hws[mc->id]))
390 mtk_clk_unregister_composite(clk_data->hws[mc->id]);
391 clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
394 EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
396 int mtk_clk_register_dividers(struct device *dev,
397 const struct mtk_clk_divider *mcds, int num,
398 void __iomem *base, spinlock_t *lock,
399 struct clk_hw_onecell_data *clk_data)
407 for (i = 0; i < num; i++) {
408 const struct mtk_clk_divider *mcd = &mcds[i];
410 if (!IS_ERR_OR_NULL(clk_data->hws[mcd->id])) {
411 pr_warn("Trying to register duplicate clock ID: %d\n",
416 hw = clk_hw_register_divider(dev, mcd->name, mcd->parent_name,
417 mcd->flags, base + mcd->div_reg, mcd->div_shift,
418 mcd->div_width, mcd->clk_divider_flags, lock);
421 pr_err("Failed to register clk %s: %pe\n", mcd->name,
426 clk_data->hws[mcd->id] = hw;
433 const struct mtk_clk_divider *mcd = &mcds[i];
435 if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
438 clk_hw_unregister_divider(clk_data->hws[mcd->id]);
439 clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
444 EXPORT_SYMBOL_GPL(mtk_clk_register_dividers);
446 void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
447 struct clk_hw_onecell_data *clk_data)
454 for (i = num; i > 0; i--) {
455 const struct mtk_clk_divider *mcd = &mcds[i - 1];
457 if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
460 clk_hw_unregister_divider(clk_data->hws[mcd->id]);
461 clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
464 EXPORT_SYMBOL_GPL(mtk_clk_unregister_dividers);
466 static int __mtk_clk_simple_probe(struct platform_device *pdev,
467 struct device_node *node)
469 const struct platform_device_id *id;
470 const struct mtk_clk_desc *mcd;
471 struct clk_hw_onecell_data *clk_data;
472 void __iomem *base = NULL;
475 mcd = device_get_match_data(&pdev->dev);
477 /* Clock driver wasn't registered from devicetree */
478 id = platform_get_device_id(pdev);
480 mcd = (const struct mtk_clk_desc *)id->driver_data;
486 /* Composite and divider clocks needs us to pass iomem pointer */
487 if (mcd->composite_clks || mcd->divider_clks) {
489 base = devm_platform_ioremap_resource(pdev, 0);
491 base = of_iomap(node, 0);
493 if (IS_ERR_OR_NULL(base))
494 return IS_ERR(base) ? PTR_ERR(base) : -ENOMEM;
497 /* Calculate how many clk_hw_onecell_data entries to allocate */
498 num_clks = mcd->num_clks + mcd->num_composite_clks;
499 num_clks += mcd->num_fixed_clks + mcd->num_factor_clks;
500 num_clks += mcd->num_mux_clks + mcd->num_divider_clks;
502 clk_data = mtk_alloc_clk_data(num_clks);
508 if (mcd->fixed_clks) {
509 r = mtk_clk_register_fixed_clks(mcd->fixed_clks,
510 mcd->num_fixed_clks, clk_data);
515 if (mcd->factor_clks) {
516 r = mtk_clk_register_factors(mcd->factor_clks,
517 mcd->num_factor_clks, clk_data);
519 goto unregister_fixed_clks;
523 r = mtk_clk_register_muxes(&pdev->dev, mcd->mux_clks,
524 mcd->num_mux_clks, node,
525 mcd->clk_lock, clk_data);
527 goto unregister_factors;
530 if (mcd->composite_clks) {
531 /* We don't check composite_lock because it's optional */
532 r = mtk_clk_register_composites(&pdev->dev,
534 mcd->num_composite_clks,
535 base, mcd->clk_lock, clk_data);
537 goto unregister_muxes;
540 if (mcd->divider_clks) {
541 r = mtk_clk_register_dividers(&pdev->dev,
543 mcd->num_divider_clks,
544 base, mcd->clk_lock, clk_data);
546 goto unregister_composites;
550 r = mtk_clk_register_gates(&pdev->dev, node, mcd->clks,
551 mcd->num_clks, clk_data);
553 goto unregister_dividers;
556 if (mcd->clk_notifier_func) {
557 struct clk *mfg_mux = clk_data->hws[mcd->mfg_clk_idx]->clk;
559 r = mcd->clk_notifier_func(&pdev->dev, mfg_mux);
561 goto unregister_clks;
564 r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
566 goto unregister_clks;
568 platform_set_drvdata(pdev, clk_data);
571 r = mtk_register_reset_controller_with_dev(&pdev->dev,
574 goto unregister_clks;
581 mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
583 if (mcd->divider_clks)
584 mtk_clk_unregister_dividers(mcd->divider_clks,
585 mcd->num_divider_clks, clk_data);
586 unregister_composites:
587 if (mcd->composite_clks)
588 mtk_clk_unregister_composites(mcd->composite_clks,
589 mcd->num_composite_clks, clk_data);
592 mtk_clk_unregister_muxes(mcd->mux_clks,
593 mcd->num_mux_clks, clk_data);
595 if (mcd->factor_clks)
596 mtk_clk_unregister_factors(mcd->factor_clks,
597 mcd->num_factor_clks, clk_data);
598 unregister_fixed_clks:
600 mtk_clk_unregister_fixed_clks(mcd->fixed_clks,
601 mcd->num_fixed_clks, clk_data);
603 mtk_free_clk_data(clk_data);
605 if (mcd->shared_io && base)
610 static void __mtk_clk_simple_remove(struct platform_device *pdev,
611 struct device_node *node)
613 struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
614 const struct mtk_clk_desc *mcd = device_get_match_data(&pdev->dev);
616 of_clk_del_provider(node);
618 mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
619 if (mcd->divider_clks)
620 mtk_clk_unregister_dividers(mcd->divider_clks,
621 mcd->num_divider_clks, clk_data);
622 if (mcd->composite_clks)
623 mtk_clk_unregister_composites(mcd->composite_clks,
624 mcd->num_composite_clks, clk_data);
626 mtk_clk_unregister_muxes(mcd->mux_clks,
627 mcd->num_mux_clks, clk_data);
628 if (mcd->factor_clks)
629 mtk_clk_unregister_factors(mcd->factor_clks,
630 mcd->num_factor_clks, clk_data);
632 mtk_clk_unregister_fixed_clks(mcd->fixed_clks,
633 mcd->num_fixed_clks, clk_data);
634 mtk_free_clk_data(clk_data);
637 int mtk_clk_pdev_probe(struct platform_device *pdev)
639 struct device *dev = &pdev->dev;
640 struct device_node *node = dev->parent->of_node;
642 return __mtk_clk_simple_probe(pdev, node);
644 EXPORT_SYMBOL_GPL(mtk_clk_pdev_probe);
646 int mtk_clk_simple_probe(struct platform_device *pdev)
648 struct device_node *node = pdev->dev.of_node;
650 return __mtk_clk_simple_probe(pdev, node);
652 EXPORT_SYMBOL_GPL(mtk_clk_simple_probe);
654 void mtk_clk_pdev_remove(struct platform_device *pdev)
656 struct device *dev = &pdev->dev;
657 struct device_node *node = dev->parent->of_node;
659 __mtk_clk_simple_remove(pdev, node);
661 EXPORT_SYMBOL_GPL(mtk_clk_pdev_remove);
663 void mtk_clk_simple_remove(struct platform_device *pdev)
665 __mtk_clk_simple_remove(pdev, pdev->dev.of_node);
667 EXPORT_SYMBOL_GPL(mtk_clk_simple_remove);
669 MODULE_LICENSE("GPL");