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()
49 struct clk_divider *div = NULL; in rockchip_clk_register_branch() local
57 return ERR_PTR(-ENOMEM); in rockchip_clk_register_branch()
59 mux->reg = base + muxdiv_offset; in rockchip_clk_register_branch()
60 mux->shift = mux_shift; in rockchip_clk_register_branch()
61 mux->mask = BIT(mux_width) - 1; in rockchip_clk_register_branch()
62 mux->flags = mux_flags; in rockchip_clk_register_branch()
63 mux->lock = lock; in rockchip_clk_register_branch()
71 ret = -ENOMEM; in rockchip_clk_register_branch()
75 gate->flags = gate_flags; in rockchip_clk_register_branch()
76 gate->reg = base + gate_offset; in rockchip_clk_register_branch()
77 gate->bit_idx = gate_shift; in rockchip_clk_register_branch()
78 gate->lock = lock; in rockchip_clk_register_branch()
83 div = kzalloc(sizeof(*div), GFP_KERNEL); in rockchip_clk_register_branch()
84 if (!div) { in rockchip_clk_register_branch()
85 ret = -ENOMEM; in rockchip_clk_register_branch()
89 div->flags = div_flags; in rockchip_clk_register_branch()
91 div->reg = base + div_offset; in rockchip_clk_register_branch()
93 div->reg = base + muxdiv_offset; in rockchip_clk_register_branch()
94 div->shift = div_shift; in rockchip_clk_register_branch()
95 div->width = div_width; in rockchip_clk_register_branch()
96 div->lock = lock; in rockchip_clk_register_branch()
97 div->table = div_table; in rockchip_clk_register_branch()
104 mux ? &mux->hw : NULL, mux_ops, in rockchip_clk_register_branch()
105 div ? &div->hw : NULL, div_ops, in rockchip_clk_register_branch()
106 gate ? &gate->hw : NULL, gate_ops, in rockchip_clk_register_branch()
109 kfree(div); in rockchip_clk_register_branch()
114 return hw->clk; in rockchip_clk_register_branch()
124 struct clk_fractional_divider div; member
143 struct clk_mux *frac_mux = &frac->mux; in rockchip_clk_frac_notifier_cb()
147 __func__, event, ndata->old_rate, ndata->new_rate); in rockchip_clk_frac_notifier_cb()
149 frac->rate_change_idx = in rockchip_clk_frac_notifier_cb()
150 frac->mux_ops->get_parent(&frac_mux->hw); in rockchip_clk_frac_notifier_cb()
151 if (frac->rate_change_idx != frac->mux_frac_idx) { in rockchip_clk_frac_notifier_cb()
152 frac->mux_ops->set_parent(&frac_mux->hw, in rockchip_clk_frac_notifier_cb()
153 frac->mux_frac_idx); in rockchip_clk_frac_notifier_cb()
154 frac->rate_change_remuxed = 1; in rockchip_clk_frac_notifier_cb()
163 if (frac->rate_change_remuxed) { in rockchip_clk_frac_notifier_cb()
164 frac->mux_ops->set_parent(&frac_mux->hw, in rockchip_clk_frac_notifier_cb()
165 frac->rate_change_idx); in rockchip_clk_frac_notifier_cb()
166 frac->rate_change_remuxed = 0; in rockchip_clk_frac_notifier_cb()
196 * by (scale - fd->nwidth) bits. in rockchip_fractional_approximation()
198 scale = fls_long(*parent_rate / rate - 1); in rockchip_fractional_approximation()
199 if (scale > fd->nwidth) in rockchip_fractional_approximation()
200 rate <<= scale - fd->nwidth; in rockchip_fractional_approximation()
203 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), in rockchip_fractional_approximation()
207 static struct clk *rockchip_clk_register_frac_branch( in rockchip_clk_register_frac_branch()
218 struct clk_fractional_divider *div = NULL; in rockchip_clk_register_frac_branch() local
222 return ERR_PTR(-EINVAL); in rockchip_clk_register_frac_branch()
224 if (child && child->branch_type != branch_mux) { in rockchip_clk_register_frac_branch()
227 return ERR_PTR(-EINVAL); in rockchip_clk_register_frac_branch()
232 return ERR_PTR(-ENOMEM); in rockchip_clk_register_frac_branch()
235 gate = &frac->gate; in rockchip_clk_register_frac_branch()
236 gate->flags = gate_flags; in rockchip_clk_register_frac_branch()
237 gate->reg = base + gate_offset; in rockchip_clk_register_frac_branch()
238 gate->bit_idx = gate_shift; in rockchip_clk_register_frac_branch()
239 gate->lock = lock; in rockchip_clk_register_frac_branch()
243 div = &frac->div; in rockchip_clk_register_frac_branch()
244 div->flags = div_flags; in rockchip_clk_register_frac_branch()
245 div->reg = base + muxdiv_offset; in rockchip_clk_register_frac_branch()
246 div->mshift = 16; in rockchip_clk_register_frac_branch()
247 div->mwidth = 16; in rockchip_clk_register_frac_branch()
248 div->mmask = GENMASK(div->mwidth - 1, 0) << div->mshift; in rockchip_clk_register_frac_branch()
249 div->nshift = 0; in rockchip_clk_register_frac_branch()
250 div->nwidth = 16; in rockchip_clk_register_frac_branch()
251 div->nmask = GENMASK(div->nwidth - 1, 0) << div->nshift; in rockchip_clk_register_frac_branch()
252 div->lock = lock; in rockchip_clk_register_frac_branch()
253 div->approximation = rockchip_fractional_approximation; in rockchip_clk_register_frac_branch()
258 &div->hw, div_ops, in rockchip_clk_register_frac_branch()
259 gate ? &gate->hw : NULL, gate_ops, in rockchip_clk_register_frac_branch()
267 struct clk_mux *frac_mux = &frac->mux; in rockchip_clk_register_frac_branch()
269 struct clk *mux_clk; in rockchip_clk_register_frac_branch()
272 frac->mux_frac_idx = match_string(child->parent_names, in rockchip_clk_register_frac_branch()
273 child->num_parents, name); in rockchip_clk_register_frac_branch()
274 frac->mux_ops = &clk_mux_ops; in rockchip_clk_register_frac_branch()
275 frac->clk_nb.notifier_call = rockchip_clk_frac_notifier_cb; in rockchip_clk_register_frac_branch()
277 frac_mux->reg = base + child->muxdiv_offset; in rockchip_clk_register_frac_branch()
278 frac_mux->shift = child->mux_shift; in rockchip_clk_register_frac_branch()
279 frac_mux->mask = BIT(child->mux_width) - 1; in rockchip_clk_register_frac_branch()
280 frac_mux->flags = child->mux_flags; in rockchip_clk_register_frac_branch()
281 frac_mux->lock = lock; in rockchip_clk_register_frac_branch()
282 frac_mux->hw.init = &init; in rockchip_clk_register_frac_branch()
284 init.name = child->name; in rockchip_clk_register_frac_branch()
285 init.flags = child->flags | CLK_SET_RATE_PARENT; in rockchip_clk_register_frac_branch()
286 init.ops = frac->mux_ops; in rockchip_clk_register_frac_branch()
287 init.parent_names = child->parent_names; in rockchip_clk_register_frac_branch()
288 init.num_parents = child->num_parents; in rockchip_clk_register_frac_branch()
290 mux_clk = clk_register(NULL, &frac_mux->hw); in rockchip_clk_register_frac_branch()
296 rockchip_clk_add_lookup(ctx, mux_clk, child->id); in rockchip_clk_register_frac_branch()
299 if (frac->mux_frac_idx >= 0) { in rockchip_clk_register_frac_branch()
301 __func__, frac->mux_frac_idx); in rockchip_clk_register_frac_branch()
302 ret = clk_notifier_register(hw->clk, &frac->clk_nb); in rockchip_clk_register_frac_branch()
308 __func__, name, child->name); in rockchip_clk_register_frac_branch()
312 return hw->clk; in rockchip_clk_register_frac_branch()
315 static struct clk *rockchip_clk_register_factor_branch(const char *name, in rockchip_clk_register_factor_branch()
317 void __iomem *base, unsigned int mult, unsigned int div, in rockchip_clk_register_factor_branch() argument
329 div); in rockchip_clk_register_factor_branch()
334 return ERR_PTR(-ENOMEM); in rockchip_clk_register_factor_branch()
336 gate->flags = gate_flags; in rockchip_clk_register_factor_branch()
337 gate->reg = base + gate_offset; in rockchip_clk_register_factor_branch()
338 gate->bit_idx = gate_shift; in rockchip_clk_register_factor_branch()
339 gate->lock = lock; in rockchip_clk_register_factor_branch()
344 return ERR_PTR(-ENOMEM); in rockchip_clk_register_factor_branch()
347 fix->mult = mult; in rockchip_clk_register_factor_branch()
348 fix->div = div; in rockchip_clk_register_factor_branch()
352 &fix->hw, &clk_fixed_factor_ops, in rockchip_clk_register_factor_branch()
353 &gate->hw, &clk_gate_ops, flags); in rockchip_clk_register_factor_branch()
360 return hw->clk; in rockchip_clk_register_factor_branch()
368 struct clk **clk_table; in rockchip_clk_init()
373 return ERR_PTR(-ENOMEM); in rockchip_clk_init()
375 clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL); in rockchip_clk_init()
380 clk_table[i] = ERR_PTR(-ENOENT); in rockchip_clk_init()
382 ctx->reg_base = base; in rockchip_clk_init()
383 ctx->clk_data.clks = clk_table; in rockchip_clk_init()
384 ctx->clk_data.clk_num = nr_clks; in rockchip_clk_init()
385 ctx->cru_node = np; in rockchip_clk_init()
386 spin_lock_init(&ctx->lock); in rockchip_clk_init()
388 ctx->grf = syscon_regmap_lookup_by_phandle(ctx->cru_node, in rockchip_clk_init()
395 return ERR_PTR(-ENOMEM); in rockchip_clk_init()
403 &ctx->clk_data)) in rockchip_clk_of_add_provider()
404 pr_err("%s: could not register clk provider\n", __func__); in rockchip_clk_of_add_provider()
409 struct clk *clk, unsigned int id) in rockchip_clk_add_lookup() argument
411 if (ctx->clk_data.clks && id) in rockchip_clk_add_lookup()
412 ctx->clk_data.clks[id] = clk; in rockchip_clk_add_lookup()
420 struct clk *clk; in rockchip_clk_register_plls() local
424 clk = rockchip_clk_register_pll(ctx, list->type, list->name, in rockchip_clk_register_plls()
425 list->parent_names, list->num_parents, in rockchip_clk_register_plls()
426 list->con_offset, grf_lock_offset, in rockchip_clk_register_plls()
427 list->lock_shift, list->mode_offset, in rockchip_clk_register_plls()
428 list->mode_shift, list->rate_table, in rockchip_clk_register_plls()
429 list->flags, list->pll_flags); in rockchip_clk_register_plls()
430 if (IS_ERR(clk)) { in rockchip_clk_register_plls()
432 list->name); in rockchip_clk_register_plls()
436 rockchip_clk_add_lookup(ctx, clk, list->id); in rockchip_clk_register_plls()
445 struct clk *clk = NULL; in rockchip_clk_register_branches() local
450 flags = list->flags; in rockchip_clk_register_branches()
453 switch (list->branch_type) { in rockchip_clk_register_branches()
455 clk = clk_register_mux(NULL, list->name, in rockchip_clk_register_branches()
456 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
457 flags, ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
458 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
459 list->mux_flags, &ctx->lock); in rockchip_clk_register_branches()
462 clk = rockchip_clk_register_muxgrf(list->name, in rockchip_clk_register_branches()
463 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
464 flags, ctx->grf, list->muxdiv_offset, in rockchip_clk_register_branches()
465 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
466 list->mux_flags); in rockchip_clk_register_branches()
469 if (list->div_table) in rockchip_clk_register_branches()
470 clk = clk_register_divider_table(NULL, in rockchip_clk_register_branches()
471 list->name, list->parent_names[0], in rockchip_clk_register_branches()
473 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
474 list->div_shift, list->div_width, in rockchip_clk_register_branches()
475 list->div_flags, list->div_table, in rockchip_clk_register_branches()
476 &ctx->lock); in rockchip_clk_register_branches()
478 clk = clk_register_divider(NULL, list->name, in rockchip_clk_register_branches()
479 list->parent_names[0], flags, in rockchip_clk_register_branches()
480 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
481 list->div_shift, list->div_width, in rockchip_clk_register_branches()
482 list->div_flags, &ctx->lock); in rockchip_clk_register_branches()
485 clk = rockchip_clk_register_frac_branch(ctx, list->name, in rockchip_clk_register_branches()
486 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
487 ctx->reg_base, list->muxdiv_offset, in rockchip_clk_register_branches()
488 list->div_flags, in rockchip_clk_register_branches()
489 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
490 list->gate_flags, flags, list->child, in rockchip_clk_register_branches()
491 &ctx->lock); in rockchip_clk_register_branches()
494 clk = rockchip_clk_register_halfdiv(list->name, in rockchip_clk_register_branches()
495 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
496 ctx->reg_base, list->muxdiv_offset, in rockchip_clk_register_branches()
497 list->mux_shift, list->mux_width, in rockchip_clk_register_branches()
498 list->mux_flags, list->div_shift, in rockchip_clk_register_branches()
499 list->div_width, list->div_flags, in rockchip_clk_register_branches()
500 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
501 list->gate_flags, flags, &ctx->lock); in rockchip_clk_register_branches()
506 clk = clk_register_gate(NULL, list->name, in rockchip_clk_register_branches()
507 list->parent_names[0], flags, in rockchip_clk_register_branches()
508 ctx->reg_base + list->gate_offset, in rockchip_clk_register_branches()
509 list->gate_shift, list->gate_flags, &ctx->lock); in rockchip_clk_register_branches()
512 clk = rockchip_clk_register_branch(list->name, in rockchip_clk_register_branches()
513 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
514 ctx->reg_base, list->muxdiv_offset, in rockchip_clk_register_branches()
515 list->mux_shift, in rockchip_clk_register_branches()
516 list->mux_width, list->mux_flags, in rockchip_clk_register_branches()
517 list->div_offset, list->div_shift, list->div_width, in rockchip_clk_register_branches()
518 list->div_flags, list->div_table, in rockchip_clk_register_branches()
519 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
520 list->gate_flags, flags, &ctx->lock); in rockchip_clk_register_branches()
523 clk = rockchip_clk_register_mmc( in rockchip_clk_register_branches()
524 list->name, in rockchip_clk_register_branches()
525 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
526 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
527 list->div_shift in rockchip_clk_register_branches()
531 clk = rockchip_clk_register_inverter( in rockchip_clk_register_branches()
532 list->name, list->parent_names, in rockchip_clk_register_branches()
533 list->num_parents, in rockchip_clk_register_branches()
534 ctx->reg_base + list->muxdiv_offset, in rockchip_clk_register_branches()
535 list->div_shift, list->div_flags, &ctx->lock); in rockchip_clk_register_branches()
538 clk = rockchip_clk_register_factor_branch( in rockchip_clk_register_branches()
539 list->name, list->parent_names, in rockchip_clk_register_branches()
540 list->num_parents, ctx->reg_base, in rockchip_clk_register_branches()
541 list->div_shift, list->div_width, in rockchip_clk_register_branches()
542 list->gate_offset, list->gate_shift, in rockchip_clk_register_branches()
543 list->gate_flags, flags, &ctx->lock); in rockchip_clk_register_branches()
546 clk = rockchip_clk_register_ddrclk( in rockchip_clk_register_branches()
547 list->name, list->flags, in rockchip_clk_register_branches()
548 list->parent_names, list->num_parents, in rockchip_clk_register_branches()
549 list->muxdiv_offset, list->mux_shift, in rockchip_clk_register_branches()
550 list->mux_width, list->div_shift, in rockchip_clk_register_branches()
551 list->div_width, list->div_flags, in rockchip_clk_register_branches()
552 ctx->reg_base, &ctx->lock); in rockchip_clk_register_branches()
557 if (!clk) { in rockchip_clk_register_branches()
559 __func__, list->branch_type); in rockchip_clk_register_branches()
563 if (IS_ERR(clk)) { in rockchip_clk_register_branches()
565 __func__, list->name, PTR_ERR(clk)); in rockchip_clk_register_branches()
569 rockchip_clk_add_lookup(ctx, clk, list->id); in rockchip_clk_register_branches()
582 struct clk *clk; in rockchip_clk_register_armclk() local
584 clk = rockchip_clk_register_cpuclk(name, parent_names, num_parents, in rockchip_clk_register_armclk()
586 ctx->reg_base, &ctx->lock); in rockchip_clk_register_armclk()
587 if (IS_ERR(clk)) { in rockchip_clk_register_armclk()
589 __func__, name, PTR_ERR(clk)); in rockchip_clk_register_armclk()
593 rockchip_clk_add_lookup(ctx, clk, lookup_id); in rockchip_clk_register_armclk()
604 struct clk *clk = __clk_lookup(clocks[i]); in rockchip_clk_protect_critical() local
606 if (clk) in rockchip_clk_protect_critical()
607 clk_prepare_enable(clk); in rockchip_clk_protect_critical()
637 rst_base = ctx->reg_base; in rockchip_register_restart_notifier()