1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: James Liao <jamesjj.liao@mediatek.com>
8 #include <linux/of_address.h>
11 #include <linux/slab.h>
12 #include <linux/delay.h>
13 #include <linux/clkdev.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/device.h>
16 #include <linux/of_device.h>
21 struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
24 struct clk_onecell_data *clk_data;
26 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
30 clk_data->clks = kcalloc(clk_num, sizeof(*clk_data->clks), GFP_KERNEL);
34 clk_data->clk_num = clk_num;
36 for (i = 0; i < clk_num; i++)
37 clk_data->clks[i] = ERR_PTR(-ENOENT);
46 void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
47 int num, struct clk_onecell_data *clk_data)
52 for (i = 0; i < num; i++) {
53 const struct mtk_fixed_clk *rc = &clks[i];
55 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[rc->id]))
58 clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
62 pr_err("Failed to register clk %s: %ld\n",
63 rc->name, PTR_ERR(clk));
68 clk_data->clks[rc->id] = clk;
72 void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
73 int num, struct clk_onecell_data *clk_data)
78 for (i = 0; i < num; i++) {
79 const struct mtk_fixed_factor *ff = &clks[i];
81 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[ff->id]))
84 clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
85 CLK_SET_RATE_PARENT, ff->mult, ff->div);
88 pr_err("Failed to register clk %s: %ld\n",
89 ff->name, PTR_ERR(clk));
94 clk_data->clks[ff->id] = clk;
98 int mtk_clk_register_gates_with_dev(struct device_node *node,
99 const struct mtk_gate *clks,
100 int num, struct clk_onecell_data *clk_data,
105 struct regmap *regmap;
110 regmap = device_node_to_regmap(node);
111 if (IS_ERR(regmap)) {
112 pr_err("Cannot find regmap for %pOF: %ld\n", node,
114 return PTR_ERR(regmap);
117 for (i = 0; i < num; i++) {
118 const struct mtk_gate *gate = &clks[i];
120 if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
123 clk = mtk_clk_register_gate(gate->name, gate->parent_name,
128 gate->shift, gate->ops, gate->flags, dev);
131 pr_err("Failed to register clk %s: %ld\n",
132 gate->name, PTR_ERR(clk));
136 clk_data->clks[gate->id] = clk;
142 int mtk_clk_register_gates(struct device_node *node,
143 const struct mtk_gate *clks,
144 int num, struct clk_onecell_data *clk_data)
146 return mtk_clk_register_gates_with_dev(node,
147 clks, num, clk_data, NULL);
150 struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
151 void __iomem *base, spinlock_t *lock)
154 struct clk_mux *mux = NULL;
155 struct clk_gate *gate = NULL;
156 struct clk_divider *div = NULL;
157 struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
158 const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
159 const char * const *parent_names;
164 if (mc->mux_shift >= 0) {
165 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
167 return ERR_PTR(-ENOMEM);
169 mux->reg = base + mc->mux_reg;
170 mux->mask = BIT(mc->mux_width) - 1;
171 mux->shift = mc->mux_shift;
173 mux->flags = mc->mux_flags;
175 mux_ops = &clk_mux_ops;
177 parent_names = mc->parent_names;
178 num_parents = mc->num_parents;
181 parent_names = &parent;
185 if (mc->gate_shift >= 0) {
186 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
192 gate->reg = base + mc->gate_reg;
193 gate->bit_idx = mc->gate_shift;
194 gate->flags = CLK_GATE_SET_TO_DISABLE;
198 gate_ops = &clk_gate_ops;
201 if (mc->divider_shift >= 0) {
202 div = kzalloc(sizeof(*div), GFP_KERNEL);
208 div->reg = base + mc->divider_reg;
209 div->shift = mc->divider_shift;
210 div->width = mc->divider_width;
214 div_ops = &clk_divider_ops;
217 clk = clk_register_composite(NULL, mc->name, parent_names, num_parents,
237 void mtk_clk_register_composites(const struct mtk_composite *mcs,
238 int num, void __iomem *base, spinlock_t *lock,
239 struct clk_onecell_data *clk_data)
244 for (i = 0; i < num; i++) {
245 const struct mtk_composite *mc = &mcs[i];
247 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mc->id]))
250 clk = mtk_clk_register_composite(mc, base, lock);
253 pr_err("Failed to register clk %s: %ld\n",
254 mc->name, PTR_ERR(clk));
259 clk_data->clks[mc->id] = clk;
263 void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
264 int num, void __iomem *base, spinlock_t *lock,
265 struct clk_onecell_data *clk_data)
270 for (i = 0; i < num; i++) {
271 const struct mtk_clk_divider *mcd = &mcds[i];
273 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
276 clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
277 mcd->flags, base + mcd->div_reg, mcd->div_shift,
278 mcd->div_width, mcd->clk_divider_flags, lock);
281 pr_err("Failed to register clk %s: %ld\n",
282 mcd->name, PTR_ERR(clk));
287 clk_data->clks[mcd->id] = clk;
291 int mtk_clk_simple_probe(struct platform_device *pdev)
293 const struct mtk_clk_desc *mcd;
294 struct clk_onecell_data *clk_data;
295 struct device_node *node = pdev->dev.of_node;
298 mcd = of_device_get_match_data(&pdev->dev);
302 clk_data = mtk_alloc_clk_data(mcd->num_clks);
306 r = mtk_clk_register_gates(node, mcd->clks, mcd->num_clks, clk_data);
310 return of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);