Lines Matching +full:clk +full:- +full:div

1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Author: Xing Zheng <zhengxing@rock-chips.com>
11 * samsung/clk.c
18 #include <linux/clk.h>
19 #include <linux/clk-provider.h>
25 #include "clk.h"
31 * src1 --|--\
32 * |M |--[GATE]-[DIV]-
33 * src2 --|--/
37 static struct clk *rockchip_clk_register_branch(const char *name, in rockchip_clk_register_branch()
50 struct clk_divider *div = NULL; in rockchip_clk_register_branch() local
58 return ERR_PTR(-ENOMEM); in rockchip_clk_register_branch()
60 mux->reg = base + muxdiv_offset; in rockchip_clk_register_branch()
61 mux->shift = mux_shift; in rockchip_clk_register_branch()
62 mux->mask = BIT(mux_width) - 1; in rockchip_clk_register_branch()
63 mux->flags = mux_flags; in rockchip_clk_register_branch()
64 mux->table = mux_table; in rockchip_clk_register_branch()
65 mux->lock = lock; in rockchip_clk_register_branch()
73 ret = -ENOMEM; in rockchip_clk_register_branch()
77 gate->flags = gate_flags; in rockchip_clk_register_branch()
78 gate->reg = base + gate_offset; in rockchip_clk_register_branch()
79 gate->bit_idx = gate_shift; in rockchip_clk_register_branch()
80 gate->lock = lock; in rockchip_clk_register_branch()
85 div = kzalloc(sizeof(*div), GFP_KERNEL); in rockchip_clk_register_branch()
86 if (!div) { in rockchip_clk_register_branch()
87 ret = -ENOMEM; in rockchip_clk_register_branch()
91 div->flags = div_flags; in rockchip_clk_register_branch()
93 div->reg = base + div_offset; in rockchip_clk_register_branch()
95 div->reg = base + muxdiv_offset; in rockchip_clk_register_branch()
96 div->shift = div_shift; in rockchip_clk_register_branch()
97 div->width = div_width; in rockchip_clk_register_branch()
98 div->lock = lock; in rockchip_clk_register_branch()
99 div->table = div_table; in rockchip_clk_register_branch()
106 mux ? &mux->hw : NULL, mux_ops, in rockchip_clk_register_branch()
107 div ? &div->hw : NULL, div_ops, in rockchip_clk_register_branch()
108 gate ? &gate->hw : NULL, gate_ops, in rockchip_clk_register_branch()
111 kfree(div); in rockchip_clk_register_branch()
116 return hw->clk; in rockchip_clk_register_branch()
126 struct clk_fractional_divider div; member
145 struct clk_mux *frac_mux = &frac->mux; in rockchip_clk_frac_notifier_cb()
149 __func__, event, ndata->old_rate, ndata->new_rate); in rockchip_clk_frac_notifier_cb()
151 frac->rate_change_idx = in rockchip_clk_frac_notifier_cb()
152 frac->mux_ops->get_parent(&frac_mux->hw); in rockchip_clk_frac_notifier_cb()
153 if (frac->rate_change_idx != frac->mux_frac_idx) { in rockchip_clk_frac_notifier_cb()
154 frac->mux_ops->set_parent(&frac_mux->hw, in rockchip_clk_frac_notifier_cb()
155 frac->mux_frac_idx); in rockchip_clk_frac_notifier_cb()
156 frac->rate_change_remuxed = 1; in rockchip_clk_frac_notifier_cb()
165 if (frac->rate_change_remuxed) { in rockchip_clk_frac_notifier_cb()
166 frac->mux_ops->set_parent(&frac_mux->hw, in rockchip_clk_frac_notifier_cb()
167 frac->rate_change_idx); in rockchip_clk_frac_notifier_cb()
168 frac->rate_change_remuxed = 0; in rockchip_clk_frac_notifier_cb()
208 } else if (!(fd->flags & CLK_FRAC_DIVIDER_NO_LIMIT)) { in rockchip_fractional_approximation()
209 pr_warn("%s p_rate(%ld) is low than rate(%ld)*20, use integer or half-div\n", in rockchip_fractional_approximation()
222 * by (scale - fd->nwidth) bits. in rockchip_fractional_approximation()
224 scale = fls_long(*parent_rate / rate - 1); in rockchip_fractional_approximation()
225 if (scale > fd->nwidth) in rockchip_fractional_approximation()
226 rate <<= scale - fd->nwidth; in rockchip_fractional_approximation()
229 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), in rockchip_fractional_approximation()
233 static struct clk *rockchip_clk_register_frac_branch( in rockchip_clk_register_frac_branch()
244 struct clk_fractional_divider *div = NULL; in rockchip_clk_register_frac_branch() local
248 return ERR_PTR(-EINVAL); in rockchip_clk_register_frac_branch()
250 if (child && child->branch_type != branch_mux) { in rockchip_clk_register_frac_branch()
253 return ERR_PTR(-EINVAL); in rockchip_clk_register_frac_branch()
258 return ERR_PTR(-ENOMEM); in rockchip_clk_register_frac_branch()
261 gate = &frac->gate; in rockchip_clk_register_frac_branch()
262 gate->flags = gate_flags; in rockchip_clk_register_frac_branch()
263 gate->reg = base + gate_offset; in rockchip_clk_register_frac_branch()
264 gate->bit_idx = gate_shift; in rockchip_clk_register_frac_branch()
265 gate->lock = lock; in rockchip_clk_register_frac_branch()
269 div = &frac->div; in rockchip_clk_register_frac_branch()
270 div->flags = div_flags; in rockchip_clk_register_frac_branch()
271 div->reg = base + muxdiv_offset; in rockchip_clk_register_frac_branch()
272 div->mshift = 16; in rockchip_clk_register_frac_branch()
273 div->mwidth = 16; in rockchip_clk_register_frac_branch()
274 div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift; in rockchip_clk_register_frac_branch()
275 div->nshift = 0; in rockchip_clk_register_frac_branch()
276 div->nwidth = 16; in rockchip_clk_register_frac_branch()
277 div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift; in rockchip_clk_register_frac_branch()
278 div->lock = lock; in rockchip_clk_register_frac_branch()
279 div->approximation = rockchip_fractional_approximation; in rockchip_clk_register_frac_branch()
284 &div->hw, div_ops, in rockchip_clk_register_frac_branch()
285 gate ? &gate->hw : NULL, gate_ops, in rockchip_clk_register_frac_branch()
293 struct clk_mux *frac_mux = &frac->mux; in rockchip_clk_register_frac_branch()
295 struct clk *mux_clk; in rockchip_clk_register_frac_branch()
298 frac->mux_frac_idx = match_string(child->parent_names, in rockchip_clk_register_frac_branch()
299 child->num_parents, name); in rockchip_clk_register_frac_branch()
300 frac->mux_ops = &clk_mux_ops; in rockchip_clk_register_frac_branch()
301 frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb; in rockchip_clk_register_frac_branch()
303 frac_mux->reg = base + child->muxdiv_offset; in rockchip_clk_register_frac_branch()
304 frac_mux->shift = child->mux_shift; in rockchip_clk_register_frac_branch()
305 frac_mux->mask = BIT(child->mux_width) - 1; in rockchip_clk_register_frac_branch()
306 frac_mux->flags = child->mux_flags; in rockchip_clk_register_frac_branch()
307 if (child->mux_table) in rockchip_clk_register_frac_branch()
308 frac_mux->table = child->mux_table; in rockchip_clk_register_frac_branch()
309 frac_mux->lock = lock; in rockchip_clk_register_frac_branch()
310 frac_mux->hw.init = &init; in rockchip_clk_register_frac_branch()
312 init.name = child->name; in rockchip_clk_register_frac_branch()
313 init.flags = child->flags | CLK_SET_RATE_PARENT; in rockchip_clk_register_frac_branch()
314 init.ops = frac->mux_ops; in rockchip_clk_register_frac_branch()
315 init.parent_names = child->parent_names; in rockchip_clk_register_frac_branch()
316 init.num_parents = child->num_parents; in rockchip_clk_register_frac_branch()
318 mux_clk = clk_register(NULL, &frac_mux->hw); in rockchip_clk_register_frac_branch()
324 rockchip_clk_add_lookup(ctx, mux_clk, child->id); in rockchip_clk_register_frac_branch()
327 if (frac->mux_frac_idx >= 0) { in rockchip_clk_register_frac_branch()
329 __func__, frac->mux_frac_idx); in rockchip_clk_register_frac_branch()
330 ret = clk_notifier_register(hw->clk, &frac->clk_nb); in rockchip_clk_register_frac_branch()
336 __func__, name, child->name); in rockchip_clk_register_frac_branch()
340 return hw->clk; in rockchip_clk_register_frac_branch()
343 static struct clk *rockchip_clk_register_factor_branch(const char *name, in rockchip_clk_register_factor_branch()
345 void __iomem *base, unsigned int mult, unsigned int div, in rockchip_clk_register_factor_branch() argument
357 div); in rockchip_clk_register_factor_branch()
362 return ERR_PTR(-ENOMEM); in rockchip_clk_register_factor_branch()
364 gate->flags = gate_flags; in rockchip_clk_register_factor_branch()
365 gate->reg = base + gate_offset; in rockchip_clk_register_factor_branch()
366 gate->bit_idx = gate_shift; in rockchip_clk_register_factor_branch()
367 gate->lock = lock; in rockchip_clk_register_factor_branch()
372 return ERR_PTR(-ENOMEM); in rockchip_clk_register_factor_branch()
375 fix->mult = mult; in rockchip_clk_register_factor_branch()
376 fix->div = div; in rockchip_clk_register_factor_branch()
380 &fix->hw, &clk_fixed_factor_ops, in rockchip_clk_register_factor_branch()
381 &gate->hw, &clk_gate_ops, flags); in rockchip_clk_register_factor_branch()
388 return hw->clk; in rockchip_clk_register_factor_branch()
396 struct clk **clk_table; in rockchip_clk_init()
401 return ERR_PTR(-ENOMEM); in rockchip_clk_init()
403 clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL); in rockchip_clk_init()
408 clk_table[i] = ERR_PTR(-ENOENT); in rockchip_clk_init()
410 ctx->reg_base = base; in rockchip_clk_init()
411 ctx->clk_data.clks = clk_table; in rockchip_clk_init()
412 ctx->clk_data.clk_num = nr_clks; in rockchip_clk_init()
413 ctx->cru_node = np; in rockchip_clk_init()
414 spin_lock_init(&ctx->lock); in rockchip_clk_init()
416 ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node, in rockchip_clk_init()
418 ctx->pmugrf = syscon_regmap_lookup_by_phandle(ctx->cru_node, in rockchip_clk_init()
425 return ERR_PTR(-ENOMEM); in rockchip_clk_init()
433 &ctx->clk_data)) in rockchip_clk_of_add_provider()
434 pr_err("%s: could not register clk provider\n", __func__); in rockchip_clk_of_add_provider()
439 struct clk *clk, unsigned int id) in rockchip_clk_add_lookup() argument
441 if (ctx->clk_data.clks && id) in rockchip_clk_add_lookup()
442 ctx->clk_data.clks[id] = clk; in rockchip_clk_add_lookup()
450 struct clk *clk; in rockchip_clk_register_plls() local
454 clk = rockchip_clk_register_pll(ctx, list->type, list->name, in rockchip_clk_register_plls()
455 list->parent_names, list->num_parents, in rockchip_clk_register_plls()
456 list->con_offset, grf_lock_offset, in rockchip_clk_register_plls()
457 list->lock_shift, list->mode_offset, in rockchip_clk_register_plls()
458 list->mode_shift, list->rate_table, in rockchip_clk_register_plls()
459 list->flags, list->pll_flags); in rockchip_clk_register_plls()
460 if (IS_ERR(clk)) { in rockchip_clk_register_plls()
462 list->name); in rockchip_clk_register_plls()
466 rockchip_clk_add_lookup(ctx, clk, list->id); in rockchip_clk_register_plls()
475 struct clk *clk = NULL; in rockchip_clk_register_branches() local
480 flags = list->flags; in rockchip_clk_register_branches()
483 switch (list->branch_type) { in rockchip_clk_register_branches()
485 if (list->mux_table) in rockchip_clk_register_branches()
486 clk = clk_register_mux_table(NULL, list->name, in rockchip_clk_register_branches()
487 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
489 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
490 list->mux_shift, in rockchip_clk_register_branches()
491 BIT(list->mux_width) - 1, in rockchip_clk_register_branches()
492 list->mux_flags, list->mux_table, in rockchip_clk_register_branches()
493 &ctx->lock); in rockchip_clk_register_branches()
495 clk = clk_register_mux(NULL, list->name, in rockchip_clk_register_branches()
496 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
498 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
499 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
500 list->mux_flags, &ctx->lock); in rockchip_clk_register_branches()
503 clk = rockchip_clk_register_muxgrf(list->name, in rockchip_clk_register_branches()
504 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
505 flags, ctx->grf, list->muxdiv_offset, in rockchip_clk_register_branches()
506 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
507 list->mux_flags); in rockchip_clk_register_branches()
510 clk = rockchip_clk_register_muxgrf(list->name, in rockchip_clk_register_branches()
511 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
512 flags, ctx->pmugrf, list->muxdiv_offset, in rockchip_clk_register_branches()
513 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
514 list->mux_flags); in rockchip_clk_register_branches()
517 if (list->div_table) in rockchip_clk_register_branches()
518 clk = clk_register_divider_table(NULL, in rockchip_clk_register_branches()
519 list->name, list->parent_names[0], in rockchip_clk_register_branches()
521 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
522 list->div_shift, list->div_width, in rockchip_clk_register_branches()
523 list->div_flags, list->div_table, in rockchip_clk_register_branches()
524 &ctx->lock); in rockchip_clk_register_branches()
526 clk = clk_register_divider(NULL, list->name, in rockchip_clk_register_branches()
527 list->parent_names[0], flags, in rockchip_clk_register_branches()
528 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
529 list->div_shift, list->div_width, in rockchip_clk_register_branches()
530 list->div_flags, &ctx->lock); in rockchip_clk_register_branches()
533 clk = rockchip_clk_register_frac_branch(ctx, list->name, in rockchip_clk_register_branches()
534 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
535 ctx->reg_base, list->muxdiv_offset, in rockchip_clk_register_branches()
536 list->div_flags, in rockchip_clk_register_branches()
537 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
538 list->gate_flags, flags, list->child, in rockchip_clk_register_branches()
539 &ctx->lock); in rockchip_clk_register_branches()
542 clk = rockchip_clk_register_halfdiv(list->name, in rockchip_clk_register_branches()
543 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
544 ctx->reg_base, list->muxdiv_offset, in rockchip_clk_register_branches()
545 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
546 list->mux_flags, list->div_offset, in rockchip_clk_register_branches()
547 list->div_shift, list->div_width, in rockchip_clk_register_branches()
548 list->div_flags, list->gate_offset, in rockchip_clk_register_branches()
549 list->gate_shift, list->gate_flags, in rockchip_clk_register_branches()
550 flags, &ctx->lock); in rockchip_clk_register_branches()
555 clk = clk_register_gate(NULL, list->name, in rockchip_clk_register_branches()
556 list->parent_names[0], flags, in rockchip_clk_register_branches()
557 ctx->reg_base + list->gate_offset, in rockchip_clk_register_branches()
558 list->gate_shift, list->gate_flags, &ctx->lock); in rockchip_clk_register_branches()
563 clk = clk_register_gate(NULL, list->name, in rockchip_clk_register_branches()
564 list->parent_names[0], flags, in rockchip_clk_register_branches()
565 ctx->reg_base + list->gate_offset, in rockchip_clk_register_branches()
566 list->gate_shift, list->gate_flags, &ctx->lock); in rockchip_clk_register_branches()
569 clk = rockchip_clk_register_branch(list->name, in rockchip_clk_register_branches()
570 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
571 ctx->reg_base, list->muxdiv_offset, in rockchip_clk_register_branches()
572 list->mux_shift, in rockchip_clk_register_branches()
573 list->mux_width, list->mux_flags, in rockchip_clk_register_branches()
574 list->mux_table, list->div_offset, in rockchip_clk_register_branches()
575 list->div_shift, list->div_width, in rockchip_clk_register_branches()
576 list->div_flags, list->div_table, in rockchip_clk_register_branches()
577 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
578 list->gate_flags, flags, &ctx->lock); in rockchip_clk_register_branches()
581 clk = rockchip_clk_register_mmc( in rockchip_clk_register_branches()
582 list->name, in rockchip_clk_register_branches()
583 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
584 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
585 list->div_shift in rockchip_clk_register_branches()
590 clk = rockchip_clk_register_inverter( in rockchip_clk_register_branches()
591 list->name, list->parent_names, in rockchip_clk_register_branches()
592 list->num_parents, in rockchip_clk_register_branches()
593 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
594 list->div_shift, list->div_flags, &ctx->lock); in rockchip_clk_register_branches()
598 clk = rockchip_clk_register_factor_branch( in rockchip_clk_register_branches()
599 list->name, list->parent_names, in rockchip_clk_register_branches()
600 list->num_parents, ctx->reg_base, in rockchip_clk_register_branches()
601 list->div_shift, list->div_width, in rockchip_clk_register_branches()
602 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
603 list->gate_flags, flags, &ctx->lock); in rockchip_clk_register_branches()
606 clk = rockchip_clk_register_ddrclk( in rockchip_clk_register_branches()
607 list->name, list->flags, in rockchip_clk_register_branches()
608 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
609 list->muxdiv_offset, list->mux_shift, in rockchip_clk_register_branches()
610 list->mux_width, list->div_shift, in rockchip_clk_register_branches()
611 list->div_width, list->div_flags, in rockchip_clk_register_branches()
612 ctx->reg_base); in rockchip_clk_register_branches()
617 if (!clk) { in rockchip_clk_register_branches()
619 __func__, list->branch_type); in rockchip_clk_register_branches()
623 if (IS_ERR(clk)) { in rockchip_clk_register_branches()
625 __func__, list->name, PTR_ERR(clk)); in rockchip_clk_register_branches()
629 rockchip_clk_add_lookup(ctx, clk, list->id); in rockchip_clk_register_branches()
638 struct clk *parent, struct clk *alt_parent, in rockchip_clk_register_armclk()
643 struct clk *clk; in rockchip_clk_register_armclk() local
645 clk = rockchip_clk_register_cpuclk(name, num_parents, in rockchip_clk_register_armclk()
648 ctx->reg_base, &ctx->lock); in rockchip_clk_register_armclk()
649 if (IS_ERR(clk)) { in rockchip_clk_register_armclk()
651 __func__, name, PTR_ERR(clk)); in rockchip_clk_register_armclk()
655 rockchip_clk_add_lookup(ctx, clk, lookup_id); in rockchip_clk_register_armclk()
664 struct clk *clk; in rockchip_clk_register_armclk_v2() local
666 clk = rockchip_clk_register_cpuclk_v2(list->name, list->parent_names, in rockchip_clk_register_armclk_v2()
667 list->num_parents, ctx->reg_base, in rockchip_clk_register_armclk_v2()
668 list->muxdiv_offset, list->mux_shift, in rockchip_clk_register_armclk_v2()
669 list->mux_width, list->mux_flags, in rockchip_clk_register_armclk_v2()
670 list->div_offset, list->div_shift, in rockchip_clk_register_armclk_v2()
671 list->div_width, list->div_flags, in rockchip_clk_register_armclk_v2()
672 list->flags, &ctx->lock, rates, nrates); in rockchip_clk_register_armclk_v2()
673 if (IS_ERR(clk)) { in rockchip_clk_register_armclk_v2()
675 __func__, list->name, PTR_ERR(clk)); in rockchip_clk_register_armclk_v2()
679 rockchip_clk_add_lookup(ctx, clk, list->id); in rockchip_clk_register_armclk_v2()
723 rst_base = ctx->reg_base; in rockchip_register_restart_notifier()
736 static struct clk **protect_clocks;
742 struct clk *clk = NULL; in rockchip_clk_protect() local
745 if (protect_clocks || !ctx || !clocks || !ctx->clk_data.clks) in rockchip_clk_protect()
750 return -ENOMEM; in rockchip_clk_protect()
753 if (clocks[i] >= ctx->clk_data.clk_num) { in rockchip_clk_protect()
757 clk = ctx->clk_data.clks[clocks[i]]; in rockchip_clk_protect()
758 if (clk) { in rockchip_clk_protect()
759 clk_prepare_enable(clk); in rockchip_clk_protect()
760 protect_clocks[i] = clk; in rockchip_clk_protect()