1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Linaro Ltd.
4 * Author: Pi-Cheng Chen <pi-cheng.chen@linaro.org>
7 #include <linux/clk-provider.h>
8 #include <linux/container_of.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/slab.h>
16 #include "clk-cpumux.h"
18 struct mtk_clk_cpumux {
20 struct regmap *regmap;
26 static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
28 return container_of(_hw, struct mtk_clk_cpumux, hw);
31 static u8 clk_cpumux_get_parent(struct clk_hw *hw)
33 struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
36 regmap_read(mux->regmap, mux->reg, &val);
44 static int clk_cpumux_set_parent(struct clk_hw *hw, u8 index)
46 struct mtk_clk_cpumux *mux = to_mtk_clk_cpumux(hw);
49 val = index << mux->shift;
50 mask = mux->mask << mux->shift;
52 return regmap_update_bits(mux->regmap, mux->reg, mask, val);
55 static const struct clk_ops clk_cpumux_ops = {
56 .get_parent = clk_cpumux_get_parent,
57 .set_parent = clk_cpumux_set_parent,
60 static struct clk_hw *
61 mtk_clk_register_cpumux(const struct mtk_composite *mux,
62 struct regmap *regmap)
64 struct mtk_clk_cpumux *cpumux;
66 struct clk_init_data init;
68 cpumux = kzalloc(sizeof(*cpumux), GFP_KERNEL);
70 return ERR_PTR(-ENOMEM);
72 init.name = mux->name;
73 init.ops = &clk_cpumux_ops;
74 init.parent_names = mux->parent_names;
75 init.num_parents = mux->num_parents;
76 init.flags = mux->flags;
78 cpumux->reg = mux->mux_reg;
79 cpumux->shift = mux->mux_shift;
80 cpumux->mask = BIT(mux->mux_width) - 1;
81 cpumux->regmap = regmap;
82 cpumux->hw.init = &init;
84 ret = clk_hw_register(NULL, &cpumux->hw);
93 static void mtk_clk_unregister_cpumux(struct clk_hw *hw)
95 struct mtk_clk_cpumux *cpumux;
99 cpumux = to_mtk_clk_cpumux(hw);
101 clk_hw_unregister(hw);
105 int mtk_clk_register_cpumuxes(struct device_node *node,
106 const struct mtk_composite *clks, int num,
107 struct clk_hw_onecell_data *clk_data)
111 struct regmap *regmap;
113 regmap = device_node_to_regmap(node);
114 if (IS_ERR(regmap)) {
115 pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
116 return PTR_ERR(regmap);
119 for (i = 0; i < num; i++) {
120 const struct mtk_composite *mux = &clks[i];
122 if (!IS_ERR_OR_NULL(clk_data->hws[mux->id])) {
123 pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
128 hw = mtk_clk_register_cpumux(mux, regmap);
130 pr_err("Failed to register clk %s: %pe\n", mux->name,
135 clk_data->hws[mux->id] = hw;
142 const struct mtk_composite *mux = &clks[i];
144 if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
147 mtk_clk_unregister_cpumux(clk_data->hws[mux->id]);
148 clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
153 EXPORT_SYMBOL_GPL(mtk_clk_register_cpumuxes);
155 void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
156 struct clk_hw_onecell_data *clk_data)
160 for (i = num; i > 0; i--) {
161 const struct mtk_composite *mux = &clks[i - 1];
163 if (IS_ERR_OR_NULL(clk_data->hws[mux->id]))
166 mtk_clk_unregister_cpumux(clk_data->hws[mux->id]);
167 clk_data->hws[mux->id] = ERR_PTR(-ENOENT);
170 EXPORT_SYMBOL_GPL(mtk_clk_unregister_cpumuxes);
172 MODULE_LICENSE("GPL");