3f9cbb760423a7176dd880ebe9369bf5af3b5229
[firefly-linux-kernel-4.4.55.git] / drivers / clk / rockchip / clk.c
1 /*
2  * Copyright (c) 2014 MundoReader S.L.
3  * Author: Heiko Stuebner <heiko@sntech.de>
4  *
5  * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
6  * Author: Xing Zheng <zhengxing@rock-chips.com>
7  *
8  * 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>
29 #include <linux/mfd/syscon.h>
30 #include <linux/regmap.h>
31 #include <linux/reboot.h>
32 #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  */
44 static struct clk *rockchip_clk_register_branch(const char *name,
45                 const char *const *parent_names, u8 num_parents, void __iomem *base,
46                 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)
76                         return ERR_PTR(-ENOMEM);
77
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)
88                         return ERR_PTR(-ENOMEM);
89
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;
96                 div_ops = (div_flags & CLK_DIVIDER_READ_ONLY)
97                                                 ? &clk_divider_ro_ops
98                                                 : &clk_divider_ops;
99         }
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;
108 }
109
110 struct rockchip_clk_frac {
111         struct notifier_block                   clk_nb;
112         struct clk_fractional_divider           div;
113         struct clk_gate                         gate;
114
115         struct clk_mux                          mux;
116         const struct clk_ops                    *mux_ops;
117         int                                     mux_frac_idx;
118
119         bool                                    rate_change_remuxed;
120         int                                     rate_change_idx;
121 };
122
123 #define to_rockchip_clk_frac_nb(nb) \
124                         container_of(nb, struct rockchip_clk_frac, clk_nb)
125
126 static int rockchip_clk_frac_notifier_cb(struct notifier_block *nb,
127                                          unsigned long event, void *data)
128 {
129         struct clk_notifier_data *ndata = data;
130         struct rockchip_clk_frac *frac = to_rockchip_clk_frac_nb(nb);
131         struct clk_mux *frac_mux = &frac->mux;
132         int ret = 0;
133
134         pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n",
135                  __func__, event, ndata->old_rate, ndata->new_rate);
136         if (event == PRE_RATE_CHANGE) {
137                 frac->rate_change_idx = frac->mux_ops->get_parent(&frac_mux->hw);
138                 if (frac->rate_change_idx != frac->mux_frac_idx) {
139                         frac->mux_ops->set_parent(&frac_mux->hw, frac->mux_frac_idx);
140                         frac->rate_change_remuxed = 1;
141                 }
142         } else if (event == POST_RATE_CHANGE) {
143                 /*
144                  * The POST_RATE_CHANGE notifier runs directly after the
145                  * divider clock is set in clk_change_rate, so we'll have
146                  * remuxed back to the original parent before clk_change_rate
147                  * reaches the mux itself.
148                  */
149                 if (frac->rate_change_remuxed) {
150                         frac->mux_ops->set_parent(&frac_mux->hw, frac->rate_change_idx);
151                         frac->rate_change_remuxed = 0;
152                 }
153         }
154
155         return notifier_from_errno(ret);
156 }
157
158 static struct clk *rockchip_clk_register_frac_branch(
159                 struct rockchip_clk_provider *ctx, const char *name,
160                 const char *const *parent_names, u8 num_parents,
161                 void __iomem *base, int muxdiv_offset, u8 div_flags,
162                 int gate_offset, u8 gate_shift, u8 gate_flags,
163                 unsigned long flags, struct rockchip_clk_branch *child,
164                 spinlock_t *lock)
165 {
166         struct rockchip_clk_frac *frac;
167         struct clk *clk;
168         struct clk_gate *gate = NULL;
169         struct clk_fractional_divider *div = NULL;
170         const struct clk_ops *div_ops = NULL, *gate_ops = NULL;
171
172         if (muxdiv_offset < 0)
173                 return ERR_PTR(-EINVAL);
174
175         if (child && child->branch_type != branch_mux) {
176                 pr_err("%s: fractional child clock for %s can only be a mux\n",
177                        __func__, name);
178                 return ERR_PTR(-EINVAL);
179         }
180
181         frac = kzalloc(sizeof(*frac), GFP_KERNEL);
182         if (!frac)
183                 return ERR_PTR(-ENOMEM);
184
185         if (gate_offset >= 0) {
186                 gate = &frac->gate;
187                 gate->flags = gate_flags;
188                 gate->reg = base + gate_offset;
189                 gate->bit_idx = gate_shift;
190                 gate->lock = lock;
191                 gate_ops = &clk_gate_ops;
192         }
193
194         div = &frac->div;
195         div->flags = div_flags;
196         div->reg = base + muxdiv_offset;
197         div->mshift = 16;
198         div->mwidth = 16;
199         div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift;
200         div->nshift = 0;
201         div->nwidth = 16;
202         div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift;
203         div->lock = lock;
204         div_ops = &clk_fractional_divider_ops;
205
206         clk = clk_register_composite(NULL, name, parent_names, num_parents,
207                                      NULL, NULL,
208                                      &div->hw, div_ops,
209                                      gate ? &gate->hw : NULL, gate_ops,
210                                      flags | CLK_SET_RATE_UNGATE);
211         if (IS_ERR(clk)) {
212                 kfree(frac);
213                 return clk;
214         }
215
216         if (child) {
217                 struct clk_mux *frac_mux = &frac->mux;
218                 struct clk_init_data init;
219                 struct clk *mux_clk;
220                 int i, ret;
221
222                 frac->mux_frac_idx = -1;
223                 for (i = 0; i < child->num_parents; i++) {
224                         if (!strcmp(name, child->parent_names[i])) {
225                                 pr_debug("%s: found fractional parent in mux at pos %d\n",
226                                          __func__, i);
227                                 frac->mux_frac_idx = i;
228                                 break;
229                         }
230                 }
231
232                 frac->mux_ops = &clk_mux_ops;
233                 frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb;
234
235                 frac_mux->reg = base + child->muxdiv_offset;
236                 frac_mux->shift = child->mux_shift;
237                 frac_mux->mask = BIT(child->mux_width) - 1;
238                 frac_mux->flags = child->mux_flags;
239                 frac_mux->lock = lock;
240                 frac_mux->hw.init = &init;
241
242                 init.name = child->name;
243                 init.flags = child->flags | CLK_SET_RATE_PARENT;
244                 init.ops = frac->mux_ops;
245                 init.parent_names = child->parent_names;
246                 init.num_parents = child->num_parents;
247
248                 mux_clk = clk_register(NULL, &frac_mux->hw);
249                 if (IS_ERR(mux_clk))
250                         return clk;
251
252                 rockchip_clk_add_lookup(ctx, mux_clk, child->id);
253
254                 /* notifier on the fraction divider to catch rate changes */
255                 if (frac->mux_frac_idx >= 0) {
256                         ret = clk_notifier_register(clk, &frac->clk_nb);
257                         if (ret)
258                                 pr_err("%s: failed to register clock notifier for %s\n",
259                                                 __func__, name);
260                 } else {
261                         pr_warn("%s: could not find %s as parent of %s, rate changes may not work\n",
262                                 __func__, name, child->name);
263                 }
264         }
265
266         return clk;
267 }
268
269 static struct clk *rockchip_clk_register_factor_branch(const char *name,
270                 const char *const *parent_names, u8 num_parents,
271                 void __iomem *base, unsigned int mult, unsigned int div,
272                 int gate_offset, u8 gate_shift, u8 gate_flags,
273                 unsigned long flags, spinlock_t *lock)
274 {
275         struct clk *clk;
276         struct clk_gate *gate = NULL;
277         struct clk_fixed_factor *fix = NULL;
278
279         /* without gate, register a simple factor clock */
280         if (gate_offset == 0) {
281                 return clk_register_fixed_factor(NULL, name,
282                                 parent_names[0], flags, mult,
283                                 div);
284         }
285
286         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
287         if (!gate)
288                 return ERR_PTR(-ENOMEM);
289
290         gate->flags = gate_flags;
291         gate->reg = base + gate_offset;
292         gate->bit_idx = gate_shift;
293         gate->lock = lock;
294
295         fix = kzalloc(sizeof(*fix), GFP_KERNEL);
296         if (!fix) {
297                 kfree(gate);
298                 return ERR_PTR(-ENOMEM);
299         }
300
301         fix->mult = mult;
302         fix->div = div;
303
304         clk = clk_register_composite(NULL, name, parent_names, num_parents,
305                                      NULL, NULL,
306                                      &fix->hw, &clk_fixed_factor_ops,
307                                      &gate->hw, &clk_gate_ops, flags);
308         if (IS_ERR(clk)) {
309                 kfree(fix);
310                 kfree(gate);
311         }
312
313         return clk;
314 }
315
316 struct rockchip_clk_provider * __init rockchip_clk_init(struct device_node *np,
317                         void __iomem *base, unsigned long nr_clks)
318 {
319         struct rockchip_clk_provider *ctx;
320         struct clk **clk_table;
321         int i;
322
323         ctx = kzalloc(sizeof(struct rockchip_clk_provider), GFP_KERNEL);
324         if (!ctx) {
325                 pr_err("%s: Could not allocate clock provider context\n",
326                         __func__);
327                 return ERR_PTR(-ENOMEM);
328         }
329
330         clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
331         if (!clk_table) {
332                 pr_err("%s: Could not allocate clock lookup table\n",
333                         __func__);
334                 goto err_free;
335         }
336
337         for (i = 0; i < nr_clks; ++i)
338                 clk_table[i] = ERR_PTR(-ENOENT);
339
340         ctx->reg_base = base;
341         ctx->clk_data.clks = clk_table;
342         ctx->clk_data.clk_num = nr_clks;
343         ctx->cru_node = np;
344         ctx->grf = ERR_PTR(-EPROBE_DEFER);
345         spin_lock_init(&ctx->lock);
346
347         return ctx;
348
349 err_free:
350         kfree(ctx);
351         return ERR_PTR(-ENOMEM);
352 }
353
354 void __init rockchip_clk_of_add_provider(struct device_node *np,
355                                 struct rockchip_clk_provider *ctx)
356 {
357         if (np) {
358                 if (of_clk_add_provider(np, of_clk_src_onecell_get,
359                                         &ctx->clk_data))
360                         pr_err("%s: could not register clk provider\n", __func__);
361         }
362 }
363
364 struct regmap *rockchip_clk_get_grf(struct rockchip_clk_provider *ctx)
365 {
366         if (IS_ERR(ctx->grf))
367                 ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node, "rockchip,grf");
368         return ctx->grf;
369 }
370
371 void rockchip_clk_add_lookup(struct rockchip_clk_provider *ctx,
372                              struct clk *clk, unsigned int id)
373 {
374         if (ctx->clk_data.clks && id)
375                 ctx->clk_data.clks[id] = clk;
376 }
377
378 void __init rockchip_clk_register_plls(struct rockchip_clk_provider *ctx,
379                                 struct rockchip_pll_clock *list,
380                                 unsigned int nr_pll, int grf_lock_offset)
381 {
382         struct clk *clk;
383         int idx;
384
385         for (idx = 0; idx < nr_pll; idx++, list++) {
386                 clk = rockchip_clk_register_pll(ctx, list->type, list->name,
387                                 list->parent_names, list->num_parents,
388                                 list->con_offset, grf_lock_offset,
389                                 list->lock_shift, list->mode_offset,
390                                 list->mode_shift, list->rate_table,
391                                 list->pll_flags);
392                 if (IS_ERR(clk)) {
393                         pr_err("%s: failed to register clock %s\n", __func__,
394                                 list->name);
395                         continue;
396                 }
397
398                 rockchip_clk_add_lookup(ctx, clk, list->id);
399         }
400 }
401
402 void __init rockchip_clk_register_branches(
403                                       struct rockchip_clk_provider *ctx,
404                                       struct rockchip_clk_branch *list,
405                                       unsigned int nr_clk)
406 {
407         struct clk *clk = NULL;
408         unsigned int idx;
409         unsigned long flags;
410
411         for (idx = 0; idx < nr_clk; idx++, list++) {
412                 flags = list->flags;
413
414                 /* catch simple muxes */
415                 switch (list->branch_type) {
416                 case branch_mux:
417                         clk = clk_register_mux(NULL, list->name,
418                                 list->parent_names, list->num_parents,
419                                 flags, ctx->reg_base + list->muxdiv_offset,
420                                 list->mux_shift, list->mux_width,
421                                 list->mux_flags, &ctx->lock);
422                         break;
423                 case branch_divider:
424                         if (list->div_table)
425                                 clk = clk_register_divider_table(NULL,
426                                         list->name, list->parent_names[0],
427                                         flags, ctx->reg_base + list->muxdiv_offset,
428                                         list->div_shift, list->div_width,
429                                         list->div_flags, list->div_table,
430                                         &ctx->lock);
431                         else
432                                 clk = clk_register_divider(NULL, list->name,
433                                         list->parent_names[0], flags,
434                                         ctx->reg_base + list->muxdiv_offset,
435                                         list->div_shift, list->div_width,
436                                         list->div_flags, &ctx->lock);
437                         break;
438                 case branch_fraction_divider:
439                         clk = rockchip_clk_register_frac_branch(ctx, list->name,
440                                 list->parent_names, list->num_parents,
441                                 ctx->reg_base, list->muxdiv_offset, list->div_flags,
442                                 list->gate_offset, list->gate_shift,
443                                 list->gate_flags, flags, list->child,
444                                 &ctx->lock);
445                         break;
446                 case branch_gate:
447                         flags |= CLK_SET_RATE_PARENT;
448
449                         clk = clk_register_gate(NULL, list->name,
450                                 list->parent_names[0], flags,
451                                 ctx->reg_base + list->gate_offset,
452                                 list->gate_shift, list->gate_flags, &ctx->lock);
453                         break;
454                 case branch_composite:
455                         clk = rockchip_clk_register_branch(list->name,
456                                 list->parent_names, list->num_parents,
457                                 ctx->reg_base, list->muxdiv_offset, list->mux_shift,
458                                 list->mux_width, list->mux_flags,
459                                 list->div_shift, list->div_width,
460                                 list->div_flags, list->div_table,
461                                 list->gate_offset, list->gate_shift,
462                                 list->gate_flags, flags, &ctx->lock);
463                         break;
464                 case branch_mmc:
465                         clk = rockchip_clk_register_mmc(
466                                 list->name,
467                                 list->parent_names, list->num_parents,
468                                 ctx->reg_base + list->muxdiv_offset,
469                                 list->div_shift
470                         );
471                         break;
472                 case branch_inverter:
473                         clk = rockchip_clk_register_inverter(
474                                 list->name, list->parent_names,
475                                 list->num_parents,
476                                 ctx->reg_base + list->muxdiv_offset,
477                                 list->div_shift, list->div_flags, &ctx->lock);
478                         break;
479                 case branch_factor:
480                         clk = rockchip_clk_register_factor_branch(
481                                 list->name, list->parent_names,
482                                 list->num_parents, ctx->reg_base,
483                                 list->div_shift, list->div_width,
484                                 list->gate_offset, list->gate_shift,
485                                 list->gate_flags, flags, &ctx->lock);
486                         break;
487                 }
488
489                 /* none of the cases above matched */
490                 if (!clk) {
491                         pr_err("%s: unknown clock type %d\n",
492                                __func__, list->branch_type);
493                         continue;
494                 }
495
496                 if (IS_ERR(clk)) {
497                         pr_err("%s: failed to register clock %s: %ld\n",
498                                __func__, list->name, PTR_ERR(clk));
499                         continue;
500                 }
501
502                 rockchip_clk_add_lookup(ctx, clk, list->id);
503         }
504 }
505
506 void __init rockchip_clk_register_armclk(struct rockchip_clk_provider *ctx,
507                         unsigned int lookup_id,
508                         const char *name, const char *const *parent_names,
509                         u8 num_parents,
510                         const struct rockchip_cpuclk_reg_data *reg_data,
511                         const struct rockchip_cpuclk_rate_table *rates,
512                         int nrates)
513 {
514         struct clk *clk;
515
516         clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents,
517                                            reg_data, rates, nrates, ctx->reg_base,
518                                            &ctx->lock);
519         if (IS_ERR(clk)) {
520                 pr_err("%s: failed to register clock %s: %ld\n",
521                        __func__, name, PTR_ERR(clk));
522                 return;
523         }
524
525         rockchip_clk_add_lookup(ctx, clk, lookup_id);
526 }
527
528 void __init rockchip_clk_protect_critical(const char *const clocks[],
529                                           int nclocks)
530 {
531         int i;
532
533         /* Protect the clocks that needs to stay on */
534         for (i = 0; i < nclocks; i++) {
535                 struct clk *clk = __clk_lookup(clocks[i]);
536
537                 if (clk)
538                         clk_prepare_enable(clk);
539         }
540 }
541
542 static void __iomem *rst_base;
543 static unsigned int reg_restart;
544 static void (*cb_restart)(void);
545 static int rockchip_restart_notify(struct notifier_block *this,
546                                    unsigned long mode, void *cmd)
547 {
548         if (cb_restart)
549                 cb_restart();
550
551         writel(0xfdb9, rst_base + reg_restart);
552         return NOTIFY_DONE;
553 }
554
555 static struct notifier_block rockchip_restart_handler = {
556         .notifier_call = rockchip_restart_notify,
557         .priority = 128,
558 };
559
560 void __init rockchip_register_restart_notifier(struct rockchip_clk_provider *ctx,
561                                                unsigned int reg, void (*cb)(void))
562 {
563         int ret;
564
565         rst_base = ctx->reg_base;
566         reg_restart = reg;
567         cb_restart = cb;
568         ret = register_restart_handler(&rockchip_restart_handler);
569         if (ret)
570                 pr_err("%s: cannot register restart handler, %d\n",
571                        __func__, ret);
572 }