blob: 277f9270bf728f4bdc804fea58d037d3627ccedd [file] [log] [blame]
Heiko Stübnera245fec2014-07-03 01:58:39 +02001/*
2 * Copyright (c) 2014 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
4 *
Xing Zhengef1d9fe2016-03-09 10:37:04 +08005 * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
6 * Author: Xing Zheng <zhengxing@rock-chips.com>
7 *
Heiko Stübnera245fec2014-07-03 01:58:39 +02008 * based on
9 *
10 * samsung/clk.c
11 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
12 * Copyright (c) 2013 Linaro Ltd.
13 * Author: Thomas Abraham <thomas.ab@samsung.com>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 */
25
26#include <linux/slab.h>
27#include <linux/clk.h>
28#include <linux/clk-provider.h>
Heiko Stübner90c59022014-07-03 01:59:10 +020029#include <linux/mfd/syscon.h>
30#include <linux/regmap.h>
Heiko Stübner6f1294b2014-08-19 17:45:38 -070031#include <linux/reboot.h>
Heiko Stübnera245fec2014-07-03 01:58:39 +020032#include "clk.h"
33
34/**
35 * Register a clock branch.
36 * Most clock branches have a form like
37 *
38 * src1 --|--\
39 * |M |--[GATE]-[DIV]-
40 * src2 --|--/
41 *
42 * sometimes without one of those components.
43 */
Heiko Stübner1a4b1812014-08-27 00:54:56 +020044static struct clk *rockchip_clk_register_branch(const char *name,
Uwe Kleine-König4a1caed2015-05-28 10:45:51 +020045 const char *const *parent_names, u8 num_parents, void __iomem *base,
Heiko Stübnera245fec2014-07-03 01:58:39 +020046 int muxdiv_offset, u8 mux_shift, u8 mux_width, u8 mux_flags,
47 u8 div_shift, u8 div_width, u8 div_flags,
48 struct clk_div_table *div_table, int gate_offset,
49 u8 gate_shift, u8 gate_flags, unsigned long flags,
50 spinlock_t *lock)
51{
52 struct clk *clk;
53 struct clk_mux *mux = NULL;
54 struct clk_gate *gate = NULL;
55 struct clk_divider *div = NULL;
56 const struct clk_ops *mux_ops = NULL, *div_ops = NULL,
57 *gate_ops = NULL;
58
59 if (num_parents > 1) {
60 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
61 if (!mux)
62 return ERR_PTR(-ENOMEM);
63
64 mux->reg = base + muxdiv_offset;
65 mux->shift = mux_shift;
66 mux->mask = BIT(mux_width) - 1;
67 mux->flags = mux_flags;
68 mux->lock = lock;
69 mux_ops = (mux_flags & CLK_MUX_READ_ONLY) ? &clk_mux_ro_ops
70 : &clk_mux_ops;
71 }
72
73 if (gate_offset >= 0) {
74 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
75 if (!gate)
Shawn Lin2467b672016-02-02 11:37:50 +080076 goto err_gate;
Heiko Stübnera245fec2014-07-03 01:58:39 +020077
78 gate->flags = gate_flags;
79 gate->reg = base + gate_offset;
80 gate->bit_idx = gate_shift;
81 gate->lock = lock;
82 gate_ops = &clk_gate_ops;
83 }
84
85 if (div_width > 0) {
86 div = kzalloc(sizeof(*div), GFP_KERNEL);
87 if (!div)
Shawn Lin2467b672016-02-02 11:37:50 +080088 goto err_div;
Heiko Stübnera245fec2014-07-03 01:58:39 +020089
90 div->flags = div_flags;
91 div->reg = base + muxdiv_offset;
92 div->shift = div_shift;
93 div->width = div_width;
94 div->lock = lock;
95 div->table = div_table;
Heiko Stuebner50359812016-01-21 21:53:09 +010096 div_ops = (div_flags & CLK_DIVIDER_READ_ONLY)
97 ? &clk_divider_ro_ops
98 : &clk_divider_ops;
Heiko Stübnera245fec2014-07-03 01:58:39 +020099 }
100
101 clk = clk_register_composite(NULL, name, parent_names, num_parents,
102 mux ? &mux->hw : NULL, mux_ops,
103 div ? &div->hw : NULL, div_ops,
104 gate ? &gate->hw : NULL, gate_ops,
105 flags);
106
107 return clk;
Shawn Lin2467b672016-02-02 11:37:50 +0800108err_div:
109 kfree(gate);
110err_gate:
111 kfree(mux);
112 return ERR_PTR(-ENOMEM);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200113}
114
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100115struct rockchip_clk_frac {
116 struct notifier_block clk_nb;
117 struct clk_fractional_divider div;
118 struct clk_gate gate;
119
120 struct clk_mux mux;
121 const struct clk_ops *mux_ops;
122 int mux_frac_idx;
123
124 bool rate_change_remuxed;
125 int rate_change_idx;
126};
127
128#define to_rockchip_clk_frac_nb(nb) \
129 container_of(nb, struct rockchip_clk_frac, clk_nb)
130
131static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb,
132 unsigned long event, void *data)
133{
134 struct clk_notifier_data *ndata = data;
135 struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb);
136 struct clk_mux *frac_mux = &frac->mux;
137 int ret = 0;
138
139 pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
140 __func__, event, ndata->old_rate, ndata->new_rate);
141 if (event == PRE_RATE_CHANGE) {
142 frac->rate_change_idx = frac->mux_ops->get_parent(&frac_mux->hw);
143 if (frac->rate_change_idx != frac->mux_frac_idx) {
144 frac->mux_ops->set_parent(&frac_mux->hw, frac->mux_frac_idx);
145 frac->rate_change_remuxed = 1;
146 }
147 } else if (event == POST_RATE_CHANGE) {
148 /*
149 * The POST_RATE_CHANGE notifier runs directly after the
150 * divider clock is set in clk_change_rate, so we'll have
151 * remuxed back to the original parent before clk_change_rate
152 * reaches the mux itself.
153 */
154 if (frac->rate_change_remuxed) {
155 frac->mux_ops->set_parent(&frac_mux->hw, frac->rate_change_idx);
156 frac->rate_change_remuxed = 0;
157 }
158 }
159
160 return notifier_from_errno(ret);
161}
162
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800163static struct clk *rockchip_clk_register_frac_branch(
164 struct rockchip_clk_provider *ctx, const char *name,
Uwe Kleine-König4a1caed2015-05-28 10:45:51 +0200165 const char *const *parent_names, u8 num_parents,
166 void __iomem *base, int muxdiv_offset, u8 div_flags,
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200167 int gate_offset, u8 gate_shift, u8 gate_flags,
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100168 unsigned long flags, struct rockchip_clk_branch *child,
169 spinlock_t *lock)
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200170{
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100171 struct rockchip_clk_frac *frac;
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200172 struct clk *clk;
173 struct clk_gate *gate = NULL;
174 struct clk_fractional_divider *div = NULL;
175 const struct clk_ops *div_ops = NULL, *gate_ops = NULL;
176
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100177 if (muxdiv_offset < 0)
178 return ERR_PTR(-EINVAL);
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200179
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100180 if (child && child->branch_type != branch_mux) {
181 pr_err("%s: fractional child clock for %s can only be a mux\n",
182 __func__, name);
183 return ERR_PTR(-EINVAL);
184 }
185
186 frac = kzalloc(sizeof(*frac), GFP_KERNEL);
187 if (!frac)
188 return ERR_PTR(-ENOMEM);
189
190 if (gate_offset >= 0) {
191 gate = &frac->gate;
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200192 gate->flags = gate_flags;
193 gate->reg = base + gate_offset;
194 gate->bit_idx = gate_shift;
195 gate->lock = lock;
196 gate_ops = &clk_gate_ops;
197 }
198
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100199 div = &frac->div;
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200200 div->flags = div_flags;
201 div->reg = base + muxdiv_offset;
202 div->mshift = 16;
Andy Shevchenko5d49a6e2015-09-22 18:54:10 +0300203 div->mwidth = 16;
204 div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift;
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200205 div->nshift = 0;
Andy Shevchenko5d49a6e2015-09-22 18:54:10 +0300206 div->nwidth = 16;
207 div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift;
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200208 div->lock = lock;
209 div_ops = &clk_fractional_divider_ops;
210
211 clk = clk_register_composite(NULL, name, parent_names, num_parents,
212 NULL, NULL,
213 &div->hw, div_ops,
214 gate ? &gate->hw : NULL, gate_ops,
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100215 flags | CLK_SET_RATE_UNGATE);
216 if (IS_ERR(clk)) {
217 kfree(frac);
218 return clk;
219 }
220
221 if (child) {
222 struct clk_mux *frac_mux = &frac->mux;
223 struct clk_init_data init;
224 struct clk *mux_clk;
225 int i, ret;
226
227 frac->mux_frac_idx = -1;
228 for (i = 0; i < child->num_parents; i++) {
229 if (!strcmp(name, child->parent_names[i])) {
230 pr_debug("%s: found fractional parent in mux at pos %d\n",
231 __func__, i);
232 frac->mux_frac_idx = i;
233 break;
234 }
235 }
236
237 frac->mux_ops = &clk_mux_ops;
238 frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb;
239
240 frac_mux->reg = base + child->muxdiv_offset;
241 frac_mux->shift = child->mux_shift;
242 frac_mux->mask = BIT(child->mux_width) - 1;
243 frac_mux->flags = child->mux_flags;
244 frac_mux->lock = lock;
245 frac_mux->hw.init = &init;
246
247 init.name = child->name;
248 init.flags = child->flags | CLK_SET_RATE_PARENT;
249 init.ops = frac->mux_ops;
250 init.parent_names = child->parent_names;
251 init.num_parents = child->num_parents;
252
253 mux_clk = clk_register(NULL, &frac_mux->hw);
254 if (IS_ERR(mux_clk))
255 return clk;
256
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800257 rockchip_clk_add_lookup(ctx, mux_clk, child->id);
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100258
259 /* notifier on the fraction divider to catch rate changes */
260 if (frac->mux_frac_idx >= 0) {
261 ret = clk_notifier_register(clk, &frac->clk_nb);
262 if (ret)
263 pr_err("%s: failed to register clock notifier for %s\n",
264 __func__, name);
265 } else {
266 pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n",
267 __func__, name, child->name);
268 }
269 }
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200270
271 return clk;
272}
273
Heiko Stuebner29a30c22015-06-20 13:08:57 +0200274static struct clk *rockchip_clk_register_factor_branch(const char *name,
275 const char *const *parent_names, u8 num_parents,
276 void __iomem *base, unsigned int mult, unsigned int div,
277 int gate_offset, u8 gate_shift, u8 gate_flags,
278 unsigned long flags, spinlock_t *lock)
279{
280 struct clk *clk;
281 struct clk_gate *gate = NULL;
282 struct clk_fixed_factor *fix = NULL;
283
284 /* without gate, register a simple factor clock */
285 if (gate_offset == 0) {
286 return clk_register_fixed_factor(NULL, name,
287 parent_names[0], flags, mult,
288 div);
289 }
290
291 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
292 if (!gate)
293 return ERR_PTR(-ENOMEM);
294
295 gate->flags = gate_flags;
296 gate->reg = base + gate_offset;
297 gate->bit_idx = gate_shift;
298 gate->lock = lock;
299
300 fix = kzalloc(sizeof(*fix), GFP_KERNEL);
301 if (!fix) {
302 kfree(gate);
303 return ERR_PTR(-ENOMEM);
304 }
305
306 fix->mult = mult;
307 fix->div = div;
308
309 clk = clk_register_composite(NULL, name, parent_names, num_parents,
310 NULL, NULL,
311 &fix->hw, &clk_fixed_factor_ops,
312 &gate->hw, &clk_gate_ops, flags);
313 if (IS_ERR(clk)) {
314 kfree(fix);
315 kfree(gate);
316 }
317
318 return clk;
319}
320
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800321struct rockchip_clk_provider * __init rockchip_clk_init(struct device_node *np,
322 void __iomem *base, unsigned long nr_clks)
Heiko Stübnera245fec2014-07-03 01:58:39 +0200323{
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800324 struct rockchip_clk_provider *ctx;
325 struct clk **clk_table;
326 int i;
327
328 ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL);
329 if (!ctx) {
330 pr_err("%s: Could not allocate clock provider context\n",
331 __func__);
332 return ERR_PTR(-ENOMEM);
333 }
Heiko Stübnera245fec2014-07-03 01:58:39 +0200334
335 clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800336 if (!clk_table) {
337 pr_err("%s: Could not allocate clock lookup table\n",
338 __func__);
339 goto err_free;
340 }
Heiko Stübnera245fec2014-07-03 01:58:39 +0200341
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800342 for (i = 0; i < nr_clks; ++i)
343 clk_table[i] = ERR_PTR(-ENOENT);
344
345 ctx->reg_base = base;
346 ctx->clk_data.clks = clk_table;
347 ctx->clk_data.clk_num = nr_clks;
348 ctx->cru_node = np;
349 ctx->grf = ERR_PTR(-EPROBE_DEFER);
350 spin_lock_init(&ctx->lock);
351
352 return ctx;
353
354err_free:
355 kfree(ctx);
356 return ERR_PTR(-ENOMEM);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200357}
358
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800359void __init rockchip_clk_of_add_provider(struct device_node *np,
360 struct rockchip_clk_provider *ctx)
Heiko Stübner90c59022014-07-03 01:59:10 +0200361{
Shawn Linff1ae202016-03-13 00:25:53 +0800362 if (of_clk_add_provider(np, of_clk_src_onecell_get,
363 &ctx->clk_data))
364 pr_err("%s: could not register clk provider\n", __func__);
Heiko Stübner90c59022014-07-03 01:59:10 +0200365}
366
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800367struct regmap *rockchip_clk_get_grf(struct rockchip_clk_provider *ctx)
Heiko Stübnera245fec2014-07-03 01:58:39 +0200368{
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800369 if (IS_ERR(ctx->grf))
370 ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node, "rockchip,grf");
371 return ctx->grf;
Heiko Stübnera245fec2014-07-03 01:58:39 +0200372}
373
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800374void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
375 struct clk *clk, unsigned int id)
376{
377 if (ctx->clk_data.clks && id)
378 ctx->clk_data.clks[id] = clk;
379}
380
381void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
382 struct rockchip_pll_clock *list,
Heiko Stübner90c59022014-07-03 01:59:10 +0200383 unsigned int nr_pll, int grf_lock_offset)
384{
385 struct clk *clk;
386 int idx;
387
388 for (idx = 0; idx < nr_pll; idx++, list++) {
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800389 clk = rockchip_clk_register_pll(ctx, list->type, list->name,
Heiko Stübner90c59022014-07-03 01:59:10 +0200390 list->parent_names, list->num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800391 list->con_offset, grf_lock_offset,
Heiko Stübner90c59022014-07-03 01:59:10 +0200392 list->lock_shift, list->mode_offset,
Heiko Stuebner4f8a7c52014-11-20 20:38:50 +0100393 list->mode_shift, list->rate_table,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800394 list->pll_flags);
Heiko Stübner90c59022014-07-03 01:59:10 +0200395 if (IS_ERR(clk)) {
396 pr_err("%s: failed to register clock %s\n", __func__,
397 list->name);
398 continue;
399 }
400
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800401 rockchip_clk_add_lookup(ctx, clk, list->id);
Heiko Stübner90c59022014-07-03 01:59:10 +0200402 }
403}
404
Heiko Stübnera245fec2014-07-03 01:58:39 +0200405void __init rockchip_clk_register_branches(
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800406 struct rockchip_clk_provider *ctx,
Heiko Stübnera245fec2014-07-03 01:58:39 +0200407 struct rockchip_clk_branch *list,
408 unsigned int nr_clk)
409{
410 struct clk *clk = NULL;
411 unsigned int idx;
412 unsigned long flags;
413
414 for (idx = 0; idx < nr_clk; idx++, list++) {
415 flags = list->flags;
416
417 /* catch simple muxes */
418 switch (list->branch_type) {
419 case branch_mux:
420 clk = clk_register_mux(NULL, list->name,
421 list->parent_names, list->num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800422 flags, ctx->reg_base + list->muxdiv_offset,
Heiko Stübnera245fec2014-07-03 01:58:39 +0200423 list->mux_shift, list->mux_width,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800424 list->mux_flags, &ctx->lock);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200425 break;
426 case branch_divider:
427 if (list->div_table)
428 clk = clk_register_divider_table(NULL,
429 list->name, list->parent_names[0],
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800430 flags, ctx->reg_base + list->muxdiv_offset,
Heiko Stübnera245fec2014-07-03 01:58:39 +0200431 list->div_shift, list->div_width,
432 list->div_flags, list->div_table,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800433 &ctx->lock);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200434 else
435 clk = clk_register_divider(NULL, list->name,
436 list->parent_names[0], flags,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800437 ctx->reg_base + list->muxdiv_offset,
Heiko Stübnera245fec2014-07-03 01:58:39 +0200438 list->div_shift, list->div_width,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800439 list->div_flags, &ctx->lock);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200440 break;
441 case branch_fraction_divider:
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800442 clk = rockchip_clk_register_frac_branch(ctx, list->name,
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200443 list->parent_names, list->num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800444 ctx->reg_base, list->muxdiv_offset, list->div_flags,
Heiko Stübnerb2155a712014-08-27 00:54:21 +0200445 list->gate_offset, list->gate_shift,
Heiko Stuebner8ca1ca82015-12-22 22:27:59 +0100446 list->gate_flags, flags, list->child,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800447 &ctx->lock);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200448 break;
449 case branch_gate:
450 flags |= CLK_SET_RATE_PARENT;
451
Heiko Stübnera245fec2014-07-03 01:58:39 +0200452 clk = clk_register_gate(NULL, list->name,
453 list->parent_names[0], flags,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800454 ctx->reg_base + list->gate_offset,
455 list->gate_shift, list->gate_flags, &ctx->lock);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200456 break;
457 case branch_composite:
Heiko Stübnera245fec2014-07-03 01:58:39 +0200458 clk = rockchip_clk_register_branch(list->name,
459 list->parent_names, list->num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800460 ctx->reg_base, list->muxdiv_offset, list->mux_shift,
Heiko Stübnera245fec2014-07-03 01:58:39 +0200461 list->mux_width, list->mux_flags,
462 list->div_shift, list->div_width,
463 list->div_flags, list->div_table,
464 list->gate_offset, list->gate_shift,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800465 list->gate_flags, flags, &ctx->lock);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200466 break;
Alexandru M Stan89bf26c2014-11-26 17:30:27 -0800467 case branch_mmc:
468 clk = rockchip_clk_register_mmc(
469 list->name,
470 list->parent_names, list->num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800471 ctx->reg_base + list->muxdiv_offset,
Alexandru M Stan89bf26c2014-11-26 17:30:27 -0800472 list->div_shift
473 );
474 break;
Heiko Stuebner8a76f442015-07-05 11:00:14 +0200475 case branch_inverter:
476 clk = rockchip_clk_register_inverter(
477 list->name, list->parent_names,
478 list->num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800479 ctx->reg_base + list->muxdiv_offset,
480 list->div_shift, list->div_flags, &ctx->lock);
Heiko Stuebner8a76f442015-07-05 11:00:14 +0200481 break;
Heiko Stuebner29a30c22015-06-20 13:08:57 +0200482 case branch_factor:
483 clk = rockchip_clk_register_factor_branch(
484 list->name, list->parent_names,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800485 list->num_parents, ctx->reg_base,
Heiko Stuebner29a30c22015-06-20 13:08:57 +0200486 list->div_shift, list->div_width,
487 list->gate_offset, list->gate_shift,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800488 list->gate_flags, flags, &ctx->lock);
Heiko Stuebner29a30c22015-06-20 13:08:57 +0200489 break;
Heiko Stübnera245fec2014-07-03 01:58:39 +0200490 }
491
492 /* none of the cases above matched */
493 if (!clk) {
494 pr_err("%s: unknown clock type %d\n",
495 __func__, list->branch_type);
496 continue;
497 }
498
499 if (IS_ERR(clk)) {
500 pr_err("%s: failed to register clock %s: %ld\n",
501 __func__, list->name, PTR_ERR(clk));
502 continue;
503 }
504
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800505 rockchip_clk_add_lookup(ctx, clk, list->id);
Heiko Stübnera245fec2014-07-03 01:58:39 +0200506 }
507}
Heiko Stübnerfe94f972014-08-14 23:00:26 +0200508
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800509void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
510 unsigned int lookup_id,
Uwe Kleine-König4a1caed2015-05-28 10:45:51 +0200511 const char *name, const char *const *parent_names,
Heiko Stuebnerf6fba5f2014-09-04 22:10:43 +0200512 u8 num_parents,
513 const struct rockchip_cpuclk_reg_data *reg_data,
514 const struct rockchip_cpuclk_rate_table *rates,
515 int nrates)
516{
517 struct clk *clk;
518
519 clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800520 reg_data, rates, nrates, ctx->reg_base,
521 &ctx->lock);
Heiko Stuebnerf6fba5f2014-09-04 22:10:43 +0200522 if (IS_ERR(clk)) {
523 pr_err("%s: failed to register clock %s: %ld\n",
524 __func__, name, PTR_ERR(clk));
525 return;
526 }
527
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800528 rockchip_clk_add_lookup(ctx, clk, lookup_id);
Heiko Stuebnerf6fba5f2014-09-04 22:10:43 +0200529}
530
Uwe Kleine-König692d8322015-02-18 10:59:45 +0100531void __init rockchip_clk_protect_critical(const char *const clocks[],
532 int nclocks)
Heiko Stübnerfe94f972014-08-14 23:00:26 +0200533{
534 int i;
535
536 /* Protect the clocks that needs to stay on */
537 for (i = 0; i < nclocks; i++) {
538 struct clk *clk = __clk_lookup(clocks[i]);
539
540 if (clk)
541 clk_prepare_enable(clk);
542 }
543}
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700544
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800545static void __iomem *rst_base;
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700546static unsigned int reg_restart;
Heiko Stuebnerdfff24b2015-12-18 17:51:55 +0100547static void (*cb_restart)(void);
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700548static int rockchip_restart_notify(struct notifier_block *this,
549 unsigned long mode, void *cmd)
550{
Heiko Stuebnerdfff24b2015-12-18 17:51:55 +0100551 if (cb_restart)
552 cb_restart();
553
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800554 writel(0xfdb9, rst_base + reg_restart);
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700555 return NOTIFY_DONE;
556}
557
558static struct notifier_block rockchip_restart_handler = {
559 .notifier_call = rockchip_restart_notify,
560 .priority = 128,
561};
562
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800563void __init rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
564 unsigned int reg, void (*cb)(void))
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700565{
566 int ret;
567
Xing Zhengef1d9fe2016-03-09 10:37:04 +0800568 rst_base = ctx->reg_base;
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700569 reg_restart = reg;
Heiko Stuebnerdfff24b2015-12-18 17:51:55 +0100570 cb_restart = cb;
Heiko Stübner6f1294b2014-08-19 17:45:38 -0700571 ret = register_restart_handler(&rockchip_restart_handler);
572 if (ret)
573 pr_err("%s: cannot register restart handler, %d\n",
574 __func__, ret);
575}