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/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/pm_runtime.h>
17 #include <linux/slab.h>
23 const struct mtk_gate_regs cg_regs_dummy = { 0, 0, 0 };
24 EXPORT_SYMBOL_GPL(cg_regs_dummy);
26 static int mtk_clk_dummy_enable(struct clk_hw *hw)
31 static void mtk_clk_dummy_disable(struct clk_hw *hw) { }
33 const struct clk_ops mtk_clk_dummy_ops = {
34 .enable = mtk_clk_dummy_enable,
35 .disable = mtk_clk_dummy_disable,
37 EXPORT_SYMBOL_GPL(mtk_clk_dummy_ops);
39 static void mtk_init_clk_data(struct clk_hw_onecell_data *clk_data,
44 clk_data->num = clk_num;
46 for (i = 0; i < clk_num; i++)
47 clk_data->hws[i] = ERR_PTR(-ENOENT);
50 struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
53 struct clk_hw_onecell_data *clk_data;
55 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, clk_num),
60 mtk_init_clk_data(clk_data, clk_num);
64 EXPORT_SYMBOL_GPL(mtk_devm_alloc_clk_data);
66 struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
68 struct clk_hw_onecell_data *clk_data;
70 clk_data = kzalloc(struct_size(clk_data, hws, clk_num), GFP_KERNEL);
74 mtk_init_clk_data(clk_data, clk_num);
78 EXPORT_SYMBOL_GPL(mtk_alloc_clk_data);
80 void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data)
84 EXPORT_SYMBOL_GPL(mtk_free_clk_data);
86 int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
87 struct clk_hw_onecell_data *clk_data)
95 for (i = 0; i < num; i++) {
96 const struct mtk_fixed_clk *rc = &clks[i];
98 if (!IS_ERR_OR_NULL(clk_data->hws[rc->id])) {
99 pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
103 hw = clk_hw_register_fixed_rate(NULL, rc->name, rc->parent, 0,
107 pr_err("Failed to register clk %s: %pe\n", rc->name,
112 clk_data->hws[rc->id] = hw;
119 const struct mtk_fixed_clk *rc = &clks[i];
121 if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
124 clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
125 clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
130 EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
132 void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
133 struct clk_hw_onecell_data *clk_data)
140 for (i = num; i > 0; i--) {
141 const struct mtk_fixed_clk *rc = &clks[i - 1];
143 if (IS_ERR_OR_NULL(clk_data->hws[rc->id]))
146 clk_hw_unregister_fixed_rate(clk_data->hws[rc->id]);
147 clk_data->hws[rc->id] = ERR_PTR(-ENOENT);
150 EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
152 int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
153 struct clk_hw_onecell_data *clk_data)
161 for (i = 0; i < num; i++) {
162 const struct mtk_fixed_factor *ff = &clks[i];
164 if (!IS_ERR_OR_NULL(clk_data->hws[ff->id])) {
165 pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
169 hw = clk_hw_register_fixed_factor(NULL, ff->name, ff->parent_name,
170 ff->flags, ff->mult, ff->div);
173 pr_err("Failed to register clk %s: %pe\n", ff->name,
178 clk_data->hws[ff->id] = hw;
185 const struct mtk_fixed_factor *ff = &clks[i];
187 if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
190 clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
191 clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
196 EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
198 void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
199 struct clk_hw_onecell_data *clk_data)
206 for (i = num; i > 0; i--) {
207 const struct mtk_fixed_factor *ff = &clks[i - 1];
209 if (IS_ERR_OR_NULL(clk_data->hws[ff->id]))
212 clk_hw_unregister_fixed_factor(clk_data->hws[ff->id]);
213 clk_data->hws[ff->id] = ERR_PTR(-ENOENT);
216 EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
218 static struct clk_hw *mtk_clk_register_composite(struct device *dev,
219 const struct mtk_composite *mc, void __iomem *base, spinlock_t *lock)
222 struct clk_mux *mux = NULL;
223 struct clk_gate *gate = NULL;
224 struct clk_divider *div = NULL;
225 struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
226 const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
227 const char * const *parent_names;
232 if (mc->mux_shift >= 0) {
233 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
235 return ERR_PTR(-ENOMEM);
237 mux->reg = base + mc->mux_reg;
238 mux->mask = BIT(mc->mux_width) - 1;
239 mux->shift = mc->mux_shift;
241 mux->flags = mc->mux_flags;
243 mux_ops = &clk_mux_ops;
245 parent_names = mc->parent_names;
246 num_parents = mc->num_parents;
249 parent_names = &parent;
253 if (mc->gate_shift >= 0) {
254 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
260 gate->reg = base + mc->gate_reg;
261 gate->bit_idx = mc->gate_shift;
262 gate->flags = CLK_GATE_SET_TO_DISABLE;
266 gate_ops = &clk_gate_ops;
269 if (mc->divider_shift >= 0) {
270 div = kzalloc(sizeof(*div), GFP_KERNEL);
276 div->reg = base + mc->divider_reg;
277 div->shift = mc->divider_shift;
278 div->width = mc->divider_width;
282 div_ops = &clk_divider_ops;
285 hw = clk_hw_register_composite(dev, mc->name, parent_names, num_parents,
305 static void mtk_clk_unregister_composite(struct clk_hw *hw)
307 struct clk_composite *composite;
308 struct clk_mux *mux = NULL;
309 struct clk_gate *gate = NULL;
310 struct clk_divider *div = NULL;
315 composite = to_clk_composite(hw);
316 if (composite->mux_hw)
317 mux = to_clk_mux(composite->mux_hw);
318 if (composite->gate_hw)
319 gate = to_clk_gate(composite->gate_hw);
320 if (composite->rate_hw)
321 div = to_clk_divider(composite->rate_hw);
323 clk_hw_unregister_composite(hw);
329 int mtk_clk_register_composites(struct device *dev,
330 const struct mtk_composite *mcs, int num,
331 void __iomem *base, spinlock_t *lock,
332 struct clk_hw_onecell_data *clk_data)
340 for (i = 0; i < num; i++) {
341 const struct mtk_composite *mc = &mcs[i];
343 if (!IS_ERR_OR_NULL(clk_data->hws[mc->id])) {
344 pr_warn("Trying to register duplicate clock ID: %d\n",
349 hw = mtk_clk_register_composite(dev, mc, base, lock);
352 pr_err("Failed to register clk %s: %pe\n", mc->name,
357 clk_data->hws[mc->id] = hw;
364 const struct mtk_composite *mc = &mcs[i];
366 if (IS_ERR_OR_NULL(clk_data->hws[mcs->id]))
369 mtk_clk_unregister_composite(clk_data->hws[mc->id]);
370 clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
375 EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
377 void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
378 struct clk_hw_onecell_data *clk_data)
385 for (i = num; i > 0; i--) {
386 const struct mtk_composite *mc = &mcs[i - 1];
388 if (IS_ERR_OR_NULL(clk_data->hws[mc->id]))
391 mtk_clk_unregister_composite(clk_data->hws[mc->id]);
392 clk_data->hws[mc->id] = ERR_PTR(-ENOENT);
395 EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
397 int mtk_clk_register_dividers(struct device *dev,
398 const struct mtk_clk_divider *mcds, int num,
399 void __iomem *base, spinlock_t *lock,
400 struct clk_hw_onecell_data *clk_data)
408 for (i = 0; i < num; i++) {
409 const struct mtk_clk_divider *mcd = &mcds[i];
411 if (!IS_ERR_OR_NULL(clk_data->hws[mcd->id])) {
412 pr_warn("Trying to register duplicate clock ID: %d\n",
417 hw = clk_hw_register_divider(dev, mcd->name, mcd->parent_name,
418 mcd->flags, base + mcd->div_reg, mcd->div_shift,
419 mcd->div_width, mcd->clk_divider_flags, lock);
422 pr_err("Failed to register clk %s: %pe\n", mcd->name,
427 clk_data->hws[mcd->id] = hw;
434 const struct mtk_clk_divider *mcd = &mcds[i];
436 if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
439 clk_hw_unregister_divider(clk_data->hws[mcd->id]);
440 clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
445 EXPORT_SYMBOL_GPL(mtk_clk_register_dividers);
447 void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
448 struct clk_hw_onecell_data *clk_data)
455 for (i = num; i > 0; i--) {
456 const struct mtk_clk_divider *mcd = &mcds[i - 1];
458 if (IS_ERR_OR_NULL(clk_data->hws[mcd->id]))
461 clk_hw_unregister_divider(clk_data->hws[mcd->id]);
462 clk_data->hws[mcd->id] = ERR_PTR(-ENOENT);
465 EXPORT_SYMBOL_GPL(mtk_clk_unregister_dividers);
467 static int __mtk_clk_simple_probe(struct platform_device *pdev,
468 struct device_node *node)
470 const struct platform_device_id *id;
471 const struct mtk_clk_desc *mcd;
472 struct clk_hw_onecell_data *clk_data;
473 void __iomem *base = NULL;
476 mcd = device_get_match_data(&pdev->dev);
478 /* Clock driver wasn't registered from devicetree */
479 id = platform_get_device_id(pdev);
481 mcd = (const struct mtk_clk_desc *)id->driver_data;
487 /* Composite and divider clocks needs us to pass iomem pointer */
488 if (mcd->composite_clks || mcd->divider_clks) {
490 base = devm_platform_ioremap_resource(pdev, 0);
492 base = of_iomap(node, 0);
494 if (IS_ERR_OR_NULL(base))
495 return IS_ERR(base) ? PTR_ERR(base) : -ENOMEM;
499 devm_pm_runtime_enable(&pdev->dev);
501 * Do a pm_runtime_resume_and_get() to workaround a possible
502 * deadlock between clk_register() and the genpd framework.
504 r = pm_runtime_resume_and_get(&pdev->dev);
508 /* Calculate how many clk_hw_onecell_data entries to allocate */
509 num_clks = mcd->num_clks + mcd->num_composite_clks;
510 num_clks += mcd->num_fixed_clks + mcd->num_factor_clks;
511 num_clks += mcd->num_mux_clks + mcd->num_divider_clks;
513 clk_data = mtk_alloc_clk_data(num_clks);
519 if (mcd->fixed_clks) {
520 r = mtk_clk_register_fixed_clks(mcd->fixed_clks,
521 mcd->num_fixed_clks, clk_data);
526 if (mcd->factor_clks) {
527 r = mtk_clk_register_factors(mcd->factor_clks,
528 mcd->num_factor_clks, clk_data);
530 goto unregister_fixed_clks;
534 r = mtk_clk_register_muxes(&pdev->dev, mcd->mux_clks,
535 mcd->num_mux_clks, node,
536 mcd->clk_lock, clk_data);
538 goto unregister_factors;
541 if (mcd->composite_clks) {
542 /* We don't check composite_lock because it's optional */
543 r = mtk_clk_register_composites(&pdev->dev,
545 mcd->num_composite_clks,
546 base, mcd->clk_lock, clk_data);
548 goto unregister_muxes;
551 if (mcd->divider_clks) {
552 r = mtk_clk_register_dividers(&pdev->dev,
554 mcd->num_divider_clks,
555 base, mcd->clk_lock, clk_data);
557 goto unregister_composites;
561 r = mtk_clk_register_gates(&pdev->dev, node, mcd->clks,
562 mcd->num_clks, clk_data);
564 goto unregister_dividers;
567 if (mcd->clk_notifier_func) {
568 struct clk *mfg_mux = clk_data->hws[mcd->mfg_clk_idx]->clk;
570 r = mcd->clk_notifier_func(&pdev->dev, mfg_mux);
572 goto unregister_clks;
575 r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
577 goto unregister_clks;
579 platform_set_drvdata(pdev, clk_data);
582 r = mtk_register_reset_controller_with_dev(&pdev->dev,
585 goto unregister_clks;
588 pm_runtime_put(&pdev->dev);
594 mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
596 if (mcd->divider_clks)
597 mtk_clk_unregister_dividers(mcd->divider_clks,
598 mcd->num_divider_clks, clk_data);
599 unregister_composites:
600 if (mcd->composite_clks)
601 mtk_clk_unregister_composites(mcd->composite_clks,
602 mcd->num_composite_clks, clk_data);
605 mtk_clk_unregister_muxes(mcd->mux_clks,
606 mcd->num_mux_clks, clk_data);
608 if (mcd->factor_clks)
609 mtk_clk_unregister_factors(mcd->factor_clks,
610 mcd->num_factor_clks, clk_data);
611 unregister_fixed_clks:
613 mtk_clk_unregister_fixed_clks(mcd->fixed_clks,
614 mcd->num_fixed_clks, clk_data);
616 mtk_free_clk_data(clk_data);
618 if (mcd->shared_io && base)
621 pm_runtime_put(&pdev->dev);
625 static void __mtk_clk_simple_remove(struct platform_device *pdev,
626 struct device_node *node)
628 struct clk_hw_onecell_data *clk_data = platform_get_drvdata(pdev);
629 const struct mtk_clk_desc *mcd = device_get_match_data(&pdev->dev);
631 of_clk_del_provider(node);
633 mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
634 if (mcd->divider_clks)
635 mtk_clk_unregister_dividers(mcd->divider_clks,
636 mcd->num_divider_clks, clk_data);
637 if (mcd->composite_clks)
638 mtk_clk_unregister_composites(mcd->composite_clks,
639 mcd->num_composite_clks, clk_data);
641 mtk_clk_unregister_muxes(mcd->mux_clks,
642 mcd->num_mux_clks, clk_data);
643 if (mcd->factor_clks)
644 mtk_clk_unregister_factors(mcd->factor_clks,
645 mcd->num_factor_clks, clk_data);
647 mtk_clk_unregister_fixed_clks(mcd->fixed_clks,
648 mcd->num_fixed_clks, clk_data);
649 mtk_free_clk_data(clk_data);
652 int mtk_clk_pdev_probe(struct platform_device *pdev)
654 struct device *dev = &pdev->dev;
655 struct device_node *node = dev->parent->of_node;
657 return __mtk_clk_simple_probe(pdev, node);
659 EXPORT_SYMBOL_GPL(mtk_clk_pdev_probe);
661 int mtk_clk_simple_probe(struct platform_device *pdev)
663 struct device_node *node = pdev->dev.of_node;
665 return __mtk_clk_simple_probe(pdev, node);
667 EXPORT_SYMBOL_GPL(mtk_clk_simple_probe);
669 void mtk_clk_pdev_remove(struct platform_device *pdev)
671 struct device *dev = &pdev->dev;
672 struct device_node *node = dev->parent->of_node;
674 __mtk_clk_simple_remove(pdev, node);
676 EXPORT_SYMBOL_GPL(mtk_clk_pdev_remove);
678 void mtk_clk_simple_remove(struct platform_device *pdev)
680 __mtk_clk_simple_remove(pdev, pdev->dev.of_node);
682 EXPORT_SYMBOL_GPL(mtk_clk_simple_remove);
684 MODULE_LICENSE("GPL");