blob: 15310f8afc98c309b3dcc5dc0019aad534b7a598 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2014 MediaTek Inc.
3 * Author: James Liao <jamesjj.liao@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/clkdev.h>
22#include <linux/mfd/syscon.h>
23
24#include "clk-mtk.h"
25#include "clk-mux.h"
26#include "clk-gate.h"
27
28struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
29{
30 int i;
31 struct clk_onecell_data *clk_data;
32
33 clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
34 if (!clk_data)
35 return NULL;
36
37 clk_data->clks = kcalloc(clk_num, sizeof(*clk_data->clks), GFP_KERNEL);
38 if (!clk_data->clks)
39 goto err_out;
40
41 clk_data->clk_num = clk_num;
42
43 for (i = 0; i < clk_num; i++)
44 clk_data->clks[i] = ERR_PTR(-ENOENT);
45
46 return clk_data;
47err_out:
48 kfree(clk_data);
49
50 return NULL;
51}
52
53void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
54 int num, struct clk_onecell_data *clk_data)
55{
56 int i;
57 struct clk *clk;
58
59 for (i = 0; i < num; i++) {
60 const struct mtk_fixed_clk *rc = &clks[i];
61
62 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[rc->id]))
63 continue;
64
65 clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
66 rc->rate);
67
68 if (IS_ERR(clk)) {
69 pr_err("Failed to register clk %s: %ld\n",
70 rc->name, PTR_ERR(clk));
71 continue;
72 }
73
74 if (clk_data)
75 clk_data->clks[rc->id] = clk;
76 }
77}
78
79void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
80 int num, struct clk_onecell_data *clk_data)
81{
82 int i;
83 struct clk *clk;
84
85 for (i = 0; i < num; i++) {
86 const struct mtk_fixed_factor *ff = &clks[i];
87
88 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[ff->id]))
89 continue;
90
91 clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
92 CLK_SET_RATE_PARENT, ff->mult, ff->div);
93
94 if (IS_ERR(clk)) {
95 pr_err("Failed to register clk %s: %ld\n",
96 ff->name, PTR_ERR(clk));
97 continue;
98 }
99
100 if (clk_data)
101 clk_data->clks[ff->id] = clk;
102 }
103}
104
105int mtk_clk_register_gates(struct device_node *node,
106 const struct mtk_gate *clks,
107 int num, struct clk_onecell_data *clk_data)
108{
109 int i;
110 struct clk *clk;
111 struct regmap *regmap;
112
113 if (!clk_data)
114 return -ENOMEM;
115
116 regmap = syscon_node_to_regmap(node);
117 if (IS_ERR(regmap)) {
118 pr_err("Cannot find regmap for %pOF: %ld\n", node,
119 PTR_ERR(regmap));
120 return PTR_ERR(regmap);
121 }
122
123 for (i = 0; i < num; i++) {
124 const struct mtk_gate *gate = &clks[i];
125
126 if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
127 continue;
128
129 clk = mtk_clk_register_gate(gate->name, gate->parent_name,
130 regmap,
131 gate->regs->set_ofs,
132 gate->regs->clr_ofs,
133 gate->regs->sta_ofs,
134 gate->shift, gate->ops,
135 gate->flags);
136
137 if (IS_ERR(clk)) {
138 pr_err("Failed to register clk %s: %ld\n",
139 gate->name, PTR_ERR(clk));
140 continue;
141 }
142
143 clk_data->clks[gate->id] = clk;
144 }
145
146 return 0;
147}
148
149int mtk_clk_register_muxes(const struct mtk_mux *muxes,
150 int num, struct device_node *node,
151 spinlock_t *lock,
152 struct clk_onecell_data *clk_data)
153{
154 struct regmap *regmap;
155 struct clk *clk;
156 int i;
157
158 if (!clk_data)
159 return -ENOMEM;
160
161 regmap = syscon_node_to_regmap(node);
162 if (IS_ERR(regmap)) {
163 pr_err("Cannot find regmap for %pOF: %ld\n", node,
164 PTR_ERR(regmap));
165 return PTR_ERR(regmap);
166 }
167
168 for (i = 0; i < num; i++) {
169 const struct mtk_mux *mux = &muxes[i];
170
171 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mux->id]))
172 continue;
173
174 clk = mtk_clk_register_mux(mux, regmap, lock);
175
176 if (IS_ERR(clk)) {
177 pr_err("Failed to register clk %s: %ld\n",
178 mux->name, PTR_ERR(clk));
179 continue;
180 }
181
182 if (clk_data)
183 clk_data->clks[mux->id] = clk;
184 }
185
186 return 0;
187}
188
189struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
190 void __iomem *base, spinlock_t *lock)
191{
192 struct clk *clk;
193 struct clk_mux *mux = NULL;
194 struct clk_gate *gate = NULL;
195 struct clk_divider *div = NULL;
196 struct clk_hw *mux_hw = NULL, *gate_hw = NULL, *div_hw = NULL;
197 const struct clk_ops *mux_ops = NULL, *gate_ops = NULL, *div_ops = NULL;
198 const char * const *parent_names;
199 const char *parent;
200 int num_parents;
201 int ret;
202
203 if (mc->mux_shift >= 0) {
204 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
205 if (!mux)
206 return ERR_PTR(-ENOMEM);
207
208 mux->reg = base + mc->mux_reg;
209 mux->mask = BIT(mc->mux_width) - 1;
210 mux->shift = mc->mux_shift;
211 mux->lock = lock;
212
213 mux_hw = &mux->hw;
214 mux_ops = &clk_mux_ops;
215
216 parent_names = mc->parent_names;
217 num_parents = mc->num_parents;
218 } else {
219 parent = mc->parent;
220 parent_names = &parent;
221 num_parents = 1;
222 }
223
224 if (mc->gate_shift >= 0) {
225 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
226 if (!gate) {
227 ret = -ENOMEM;
228 goto err_out;
229 }
230
231 gate->reg = base + mc->gate_reg;
232 gate->bit_idx = mc->gate_shift;
233 gate->flags = CLK_GATE_SET_TO_DISABLE;
234 gate->lock = lock;
235
236 gate_hw = &gate->hw;
237 gate_ops = &clk_gate_ops;
238 }
239
240 if (mc->divider_shift >= 0) {
241 div = kzalloc(sizeof(*div), GFP_KERNEL);
242 if (!div) {
243 ret = -ENOMEM;
244 goto err_out;
245 }
246
247 div->reg = base + mc->divider_reg;
248 div->shift = mc->divider_shift;
249 div->width = mc->divider_width;
250 div->lock = lock;
251
252 div_hw = &div->hw;
253 div_ops = &clk_divider_ops;
254 }
255
256 clk = clk_register_composite(NULL, mc->name, parent_names, num_parents,
257 mux_hw, mux_ops,
258 div_hw, div_ops,
259 gate_hw, gate_ops,
260 mc->flags);
261
262 if (IS_ERR(clk)) {
263 ret = PTR_ERR(clk);
264 goto err_out;
265 }
266
267 return clk;
268err_out:
269 kfree(div);
270 kfree(gate);
271 kfree(mux);
272
273 return ERR_PTR(ret);
274}
275
276void mtk_clk_register_composites(const struct mtk_composite *mcs,
277 int num, void __iomem *base, spinlock_t *lock,
278 struct clk_onecell_data *clk_data)
279{
280 struct clk *clk;
281 int i;
282
283 for (i = 0; i < num; i++) {
284 const struct mtk_composite *mc = &mcs[i];
285
286 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mc->id]))
287 continue;
288
289 clk = mtk_clk_register_composite(mc, base, lock);
290
291 if (IS_ERR(clk)) {
292 pr_err("Failed to register clk %s: %ld\n",
293 mc->name, PTR_ERR(clk));
294 continue;
295 }
296
297 if (clk_data)
298 clk_data->clks[mc->id] = clk;
299 }
300}
301
302void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
303 int num, void __iomem *base, spinlock_t *lock,
304 struct clk_onecell_data *clk_data)
305{
306 struct clk *clk;
307 int i;
308
309 for (i = 0; i < num; i++) {
310 const struct mtk_clk_divider *mcd = &mcds[i];
311
312 if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
313 continue;
314
315 clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
316 mcd->flags, base + mcd->div_reg, mcd->div_shift,
317 mcd->div_width, mcd->clk_divider_flags, lock);
318
319 if (IS_ERR(clk)) {
320 pr_err("Failed to register clk %s: %ld\n",
321 mcd->name, PTR_ERR(clk));
322 continue;
323 }
324
325 if (clk_data)
326 clk_data->clks[mcd->id] = clk;
327 }
328}