summaryrefslogtreecommitdiffstats
path: root/drivers/clk/mediatek/clk-mtk.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/clk/mediatek/clk-mtk.c')
-rw-r--r--drivers/clk/mediatek/clk-mtk.c304
1 files changed, 223 insertions, 81 deletions
diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 8d5791b3f460..b4063261cf56 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -4,17 +4,16 @@
* Author: James Liao <jamesjj.liao@mediatek.com>
*/
-#include <linux/of.h>
-#include <linux/of_address.h>
+#include <linux/bitops.h>
+#include <linux/clk-provider.h>
#include <linux/err.h>
#include <linux/io.h>
-#include <linux/slab.h>
-#include <linux/delay.h>
-#include <linux/clkdev.h>
-#include <linux/module.h>
#include <linux/mfd/syscon.h>
-#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
#include "clk-mtk.h"
#include "clk-gate.h"
@@ -54,112 +53,135 @@ void mtk_free_clk_data(struct clk_onecell_data *clk_data)
kfree(clk_data);
}
-void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
- int num, struct clk_onecell_data *clk_data)
+int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data)
{
int i;
struct clk *clk;
+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_fixed_clk *rc = &clks[i];
- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[rc->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[rc->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
continue;
+ }
clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
rc->rate);
if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- rc->name, PTR_ERR(clk));
- continue;
+ pr_err("Failed to register clk %s: %pe\n", rc->name, clk);
+ goto err;
}
- if (clk_data)
- clk_data->clks[rc->id] = clk;
+ clk_data->clks[rc->id] = clk;
+ }
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_fixed_clk *rc = &clks[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[rc->id]))
+ continue;
+
+ clk_unregister_fixed_rate(clk_data->clks[rc->id]);
+ clk_data->clks[rc->id] = ERR_PTR(-ENOENT);
}
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
-void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
- int num, struct clk_onecell_data *clk_data)
+void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data)
{
int i;
- struct clk *clk;
- for (i = 0; i < num; i++) {
- const struct mtk_fixed_factor *ff = &clks[i];
-
- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[ff->id]))
- continue;
+ if (!clk_data)
+ return;
- clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
- CLK_SET_RATE_PARENT, ff->mult, ff->div);
+ for (i = num; i > 0; i--) {
+ const struct mtk_fixed_clk *rc = &clks[i - 1];
- if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- ff->name, PTR_ERR(clk));
+ if (IS_ERR_OR_NULL(clk_data->clks[rc->id]))
continue;
- }
- if (clk_data)
- clk_data->clks[ff->id] = clk;
+ clk_unregister_fixed_rate(clk_data->clks[rc->id]);
+ clk_data->clks[rc->id] = ERR_PTR(-ENOENT);
}
}
-EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
-int mtk_clk_register_gates_with_dev(struct device_node *node,
- const struct mtk_gate *clks,
- int num, struct clk_onecell_data *clk_data,
- struct device *dev)
+int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data)
{
int i;
struct clk *clk;
- struct regmap *regmap;
if (!clk_data)
return -ENOMEM;
- regmap = device_node_to_regmap(node);
- if (IS_ERR(regmap)) {
- pr_err("Cannot find regmap for %pOF: %ld\n", node,
- PTR_ERR(regmap));
- return PTR_ERR(regmap);
- }
-
for (i = 0; i < num; i++) {
- const struct mtk_gate *gate = &clks[i];
+ const struct mtk_fixed_factor *ff = &clks[i];
- if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[ff->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
continue;
+ }
- clk = mtk_clk_register_gate(gate->name, gate->parent_name,
- regmap,
- gate->regs->set_ofs,
- gate->regs->clr_ofs,
- gate->regs->sta_ofs,
- gate->shift, gate->ops, gate->flags, dev);
+ clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
+ CLK_SET_RATE_PARENT, ff->mult, ff->div);
if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- gate->name, PTR_ERR(clk));
- continue;
+ pr_err("Failed to register clk %s: %pe\n", ff->name, clk);
+ goto err;
}
- clk_data->clks[gate->id] = clk;
+ clk_data->clks[ff->id] = clk;
}
return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_fixed_factor *ff = &clks[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[ff->id]))
+ continue;
+
+ clk_unregister_fixed_factor(clk_data->clks[ff->id]);
+ clk_data->clks[ff->id] = ERR_PTR(-ENOENT);
+ }
+
+ return PTR_ERR(clk);
}
+EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
-int mtk_clk_register_gates(struct device_node *node,
- const struct mtk_gate *clks,
- int num, struct clk_onecell_data *clk_data)
+void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data)
{
- return mtk_clk_register_gates_with_dev(node,
- clks, num, clk_data, NULL);
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_fixed_factor *ff = &clks[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[ff->id]))
+ continue;
+
+ clk_unregister_fixed_factor(clk_data->clks[ff->id]);
+ clk_data->clks[ff->id] = ERR_PTR(-ENOENT);
+ }
}
-EXPORT_SYMBOL_GPL(mtk_clk_register_gates);
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
void __iomem *base, spinlock_t *lock)
@@ -248,58 +270,161 @@ err_out:
return ERR_PTR(ret);
}
-void mtk_clk_register_composites(const struct mtk_composite *mcs,
- int num, void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data)
+static void mtk_clk_unregister_composite(struct clk *clk)
+{
+ struct clk_hw *hw;
+ struct clk_composite *composite;
+ struct clk_mux *mux = NULL;
+ struct clk_gate *gate = NULL;
+ struct clk_divider *div = NULL;
+
+ hw = __clk_get_hw(clk);
+ if (!hw)
+ return;
+
+ composite = to_clk_composite(hw);
+ if (composite->mux_hw)
+ mux = to_clk_mux(composite->mux_hw);
+ if (composite->gate_hw)
+ gate = to_clk_gate(composite->gate_hw);
+ if (composite->rate_hw)
+ div = to_clk_divider(composite->rate_hw);
+
+ clk_unregister_composite(clk);
+ kfree(div);
+ kfree(gate);
+ kfree(mux);
+}
+
+int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data)
{
struct clk *clk;
int i;
+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_composite *mc = &mcs[i];
- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mc->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[mc->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n",
+ mc->id);
continue;
+ }
clk = mtk_clk_register_composite(mc, base, lock);
if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- mc->name, PTR_ERR(clk));
- continue;
+ pr_err("Failed to register clk %s: %pe\n", mc->name, clk);
+ goto err;
}
- if (clk_data)
- clk_data->clks[mc->id] = clk;
+ clk_data->clks[mc->id] = clk;
+ }
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_composite *mc = &mcs[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mcs->id]))
+ continue;
+
+ mtk_clk_unregister_composite(clk_data->clks[mc->id]);
+ clk_data->clks[mc->id] = ERR_PTR(-ENOENT);
}
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
-void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
- int num, void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data)
+void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_composite *mc = &mcs[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mc->id]))
+ continue;
+
+ mtk_clk_unregister_composite(clk_data->clks[mc->id]);
+ clk_data->clks[mc->id] = ERR_PTR(-ENOENT);
+ }
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
+
+int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data)
{
struct clk *clk;
int i;
+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_clk_divider *mcd = &mcds[i];
- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n",
+ mcd->id);
continue;
+ }
clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
mcd->flags, base + mcd->div_reg, mcd->div_shift,
mcd->div_width, mcd->clk_divider_flags, lock);
if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- mcd->name, PTR_ERR(clk));
- continue;
+ pr_err("Failed to register clk %s: %pe\n", mcd->name, clk);
+ goto err;
}
- if (clk_data)
- clk_data->clks[mcd->id] = clk;
+ clk_data->clks[mcd->id] = clk;
+ }
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_clk_divider *mcd = &mcds[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ continue;
+
+ mtk_clk_unregister_composite(clk_data->clks[mcd->id]);
+ clk_data->clks[mcd->id] = ERR_PTR(-ENOENT);
+ }
+
+ return PTR_ERR(clk);
+}
+
+void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_clk_divider *mcd = &mcds[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ continue;
+
+ clk_unregister_divider(clk_data->clks[mcd->id]);
+ clk_data->clks[mcd->id] = ERR_PTR(-ENOENT);
}
}
@@ -324,13 +449,30 @@ int mtk_clk_simple_probe(struct platform_device *pdev)
r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
if (r)
- goto free_data;
+ goto unregister_clks;
+
+ platform_set_drvdata(pdev, clk_data);
return r;
+unregister_clks:
+ mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
free_data:
mtk_free_clk_data(clk_data);
return r;
}
+int mtk_clk_simple_remove(struct platform_device *pdev)
+{
+ const struct mtk_clk_desc *mcd = of_device_get_match_data(&pdev->dev);
+ struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
+ struct device_node *node = pdev->dev.of_node;
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
+ mtk_free_clk_data(clk_data);
+
+ return 0;
+}
+
MODULE_LICENSE("GPL");