1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013, 2018, The Linux Foundation. All rights reserved.
6 #include <linux/kernel.h>
7 #include <linux/bitops.h>
10 #include <linux/export.h>
11 #include <linux/clk-provider.h>
12 #include <linux/delay.h>
13 #include <linux/rational.h>
14 #include <linux/regmap.h>
15 #include <linux/math64.h>
16 #include <linux/minmax.h>
17 #include <linux/slab.h>
19 #include <asm/div64.h>
25 #define CMD_UPDATE BIT(0)
26 #define CMD_ROOT_EN BIT(1)
27 #define CMD_DIRTY_CFG BIT(4)
28 #define CMD_DIRTY_N BIT(5)
29 #define CMD_DIRTY_M BIT(6)
30 #define CMD_DIRTY_D BIT(7)
31 #define CMD_ROOT_OFF BIT(31)
34 #define CFG_SRC_DIV_SHIFT 0
35 #define CFG_SRC_SEL_SHIFT 8
36 #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT)
37 #define CFG_MODE_SHIFT 12
38 #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT)
39 #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT)
40 #define CFG_HW_CLK_CTRL_MASK BIT(20)
46 #define RCG_CFG_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + CFG_REG)
47 #define RCG_M_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + M_REG)
48 #define RCG_N_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + N_REG)
49 #define RCG_D_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + D_REG)
51 /* Dynamic Frequency Scaling */
52 #define MAX_PERF_LEVEL 8
53 #define SE_CMD_DFSR_OFFSET 0x14
54 #define SE_CMD_DFS_EN BIT(0)
55 #define SE_PERF_DFSR(level) (0x1c + 0x4 * (level))
56 #define SE_PERF_M_DFSR(level) (0x5c + 0x4 * (level))
57 #define SE_PERF_N_DFSR(level) (0x9c + 0x4 * (level))
64 static int clk_rcg2_is_enabled(struct clk_hw *hw)
66 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
70 ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd);
74 return (cmd & CMD_ROOT_OFF) == 0;
77 static u8 __clk_rcg2_get_parent(struct clk_hw *hw, u32 cfg)
79 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
80 int num_parents = clk_hw_get_num_parents(hw);
83 cfg &= CFG_SRC_SEL_MASK;
84 cfg >>= CFG_SRC_SEL_SHIFT;
86 for (i = 0; i < num_parents; i++)
87 if (cfg == rcg->parent_map[i].cfg)
90 pr_debug("%s: Clock %s has invalid parent, using default.\n",
91 __func__, clk_hw_get_name(hw));
95 static u8 clk_rcg2_get_parent(struct clk_hw *hw)
97 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
101 ret = regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg);
103 pr_debug("%s: Unable to read CFG register for %s\n",
104 __func__, clk_hw_get_name(hw));
108 return __clk_rcg2_get_parent(hw, cfg);
111 static int update_config(struct clk_rcg2 *rcg)
115 struct clk_hw *hw = &rcg->clkr.hw;
116 const char *name = clk_hw_get_name(hw);
118 ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG,
119 CMD_UPDATE, CMD_UPDATE);
123 /* Wait for update to take effect */
124 for (count = 500; count > 0; count--) {
125 ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd);
128 if (!(cmd & CMD_UPDATE))
133 WARN(1, "%s: rcg didn't update its configuration.", name);
137 static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)
139 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
141 u32 cfg = rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
143 ret = regmap_update_bits(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg),
144 CFG_SRC_SEL_MASK, cfg);
148 return update_config(rcg);
152 * Calculate m/n:d rate
155 * rate = ----------- x ---
159 calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div)
177 __clk_rcg2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, u32 cfg)
179 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
180 u32 hid_div, m = 0, n = 0, mode = 0, mask;
182 if (rcg->mnd_width) {
183 mask = BIT(rcg->mnd_width) - 1;
184 regmap_read(rcg->clkr.regmap, RCG_M_OFFSET(rcg), &m);
186 regmap_read(rcg->clkr.regmap, RCG_N_OFFSET(rcg), &n);
190 mode = cfg & CFG_MODE_MASK;
191 mode >>= CFG_MODE_SHIFT;
194 mask = BIT(rcg->hid_width) - 1;
195 hid_div = cfg >> CFG_SRC_DIV_SHIFT;
198 return calc_rate(parent_rate, m, n, mode, hid_div);
202 clk_rcg2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
204 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
207 regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg);
209 return __clk_rcg2_recalc_rate(hw, parent_rate, cfg);
212 static int _freq_tbl_determine_rate(struct clk_hw *hw, const struct freq_tbl *f,
213 struct clk_rate_request *req,
214 enum freq_policy policy)
216 unsigned long clk_flags, rate = req->rate;
218 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
223 f = qcom_find_freq_floor(f, rate);
226 f = qcom_find_freq(f, rate);
235 index = qcom_find_src_index(hw, rcg->parent_map, f->src);
239 clk_flags = clk_hw_get_flags(hw);
240 p = clk_hw_get_parent_by_index(hw, index);
244 if (clk_flags & CLK_SET_RATE_PARENT) {
250 rate *= f->pre_div + 1;
260 rate = clk_hw_get_rate(p);
262 req->best_parent_hw = p;
263 req->best_parent_rate = rate;
269 static int clk_rcg2_determine_rate(struct clk_hw *hw,
270 struct clk_rate_request *req)
272 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
274 return _freq_tbl_determine_rate(hw, rcg->freq_tbl, req, CEIL);
277 static int clk_rcg2_determine_floor_rate(struct clk_hw *hw,
278 struct clk_rate_request *req)
280 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
282 return _freq_tbl_determine_rate(hw, rcg->freq_tbl, req, FLOOR);
285 static int __clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f,
288 u32 cfg, mask, d_val, not2d_val, n_minus_m;
289 struct clk_hw *hw = &rcg->clkr.hw;
290 int ret, index = qcom_find_src_index(hw, rcg->parent_map, f->src);
295 if (rcg->mnd_width && f->n) {
296 mask = BIT(rcg->mnd_width) - 1;
297 ret = regmap_update_bits(rcg->clkr.regmap,
298 RCG_M_OFFSET(rcg), mask, f->m);
302 ret = regmap_update_bits(rcg->clkr.regmap,
303 RCG_N_OFFSET(rcg), mask, ~(f->n - f->m));
307 /* Calculate 2d value */
310 n_minus_m = f->n - f->m;
313 d_val = clamp_t(u32, d_val, f->m, n_minus_m);
314 not2d_val = ~d_val & mask;
316 ret = regmap_update_bits(rcg->clkr.regmap,
317 RCG_D_OFFSET(rcg), mask, not2d_val);
322 mask = BIT(rcg->hid_width) - 1;
323 mask |= CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK;
324 cfg = f->pre_div << CFG_SRC_DIV_SHIFT;
325 cfg |= rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
326 if (rcg->mnd_width && f->n && (f->m != f->n))
327 cfg |= CFG_MODE_DUAL_EDGE;
335 static int clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f)
340 ret = regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg);
344 ret = __clk_rcg2_configure(rcg, f, &cfg);
348 ret = regmap_write(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), cfg);
352 return update_config(rcg);
355 static int __clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
356 enum freq_policy policy)
358 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
359 const struct freq_tbl *f;
363 f = qcom_find_freq_floor(rcg->freq_tbl, rate);
366 f = qcom_find_freq(rcg->freq_tbl, rate);
375 return clk_rcg2_configure(rcg, f);
378 static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
379 unsigned long parent_rate)
381 return __clk_rcg2_set_rate(hw, rate, CEIL);
384 static int clk_rcg2_set_floor_rate(struct clk_hw *hw, unsigned long rate,
385 unsigned long parent_rate)
387 return __clk_rcg2_set_rate(hw, rate, FLOOR);
390 static int clk_rcg2_set_rate_and_parent(struct clk_hw *hw,
391 unsigned long rate, unsigned long parent_rate, u8 index)
393 return __clk_rcg2_set_rate(hw, rate, CEIL);
396 static int clk_rcg2_set_floor_rate_and_parent(struct clk_hw *hw,
397 unsigned long rate, unsigned long parent_rate, u8 index)
399 return __clk_rcg2_set_rate(hw, rate, FLOOR);
402 static int clk_rcg2_get_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
404 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
405 u32 notn_m, n, m, d, not2d, mask;
407 if (!rcg->mnd_width) {
408 /* 50 % duty-cycle for Non-MND RCGs */
414 regmap_read(rcg->clkr.regmap, RCG_D_OFFSET(rcg), ¬2d);
415 regmap_read(rcg->clkr.regmap, RCG_M_OFFSET(rcg), &m);
416 regmap_read(rcg->clkr.regmap, RCG_N_OFFSET(rcg), ¬n_m);
418 if (!not2d && !m && !notn_m) {
419 /* 50 % duty-cycle always */
425 mask = BIT(rcg->mnd_width) - 1;
428 d = DIV_ROUND_CLOSEST(d, 2);
430 n = (~(notn_m) + m) & mask;
438 static int clk_rcg2_set_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
440 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
441 u32 notn_m, n, m, d, not2d, mask, duty_per, cfg;
444 /* Duty-cycle cannot be modified for non-MND RCGs */
448 mask = BIT(rcg->mnd_width) - 1;
450 regmap_read(rcg->clkr.regmap, RCG_N_OFFSET(rcg), ¬n_m);
451 regmap_read(rcg->clkr.regmap, RCG_M_OFFSET(rcg), &m);
452 regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg);
454 /* Duty-cycle cannot be modified if MND divider is in bypass mode. */
455 if (!(cfg & CFG_MODE_MASK))
458 n = (~(notn_m) + m) & mask;
460 duty_per = (duty->num * 100) / duty->den;
462 /* Calculate 2d value */
463 d = DIV_ROUND_CLOSEST(n * duty_per * 2, 100);
466 * Check bit widths of 2d. If D is too big reduce duty cycle.
467 * Also make sure it is never zero.
469 d = clamp_val(d, 1, mask);
471 if ((d / 2) > (n - m))
473 else if ((d / 2) < (m / 2))
478 ret = regmap_update_bits(rcg->clkr.regmap, RCG_D_OFFSET(rcg), mask,
483 return update_config(rcg);
486 const struct clk_ops clk_rcg2_ops = {
487 .is_enabled = clk_rcg2_is_enabled,
488 .get_parent = clk_rcg2_get_parent,
489 .set_parent = clk_rcg2_set_parent,
490 .recalc_rate = clk_rcg2_recalc_rate,
491 .determine_rate = clk_rcg2_determine_rate,
492 .set_rate = clk_rcg2_set_rate,
493 .set_rate_and_parent = clk_rcg2_set_rate_and_parent,
494 .get_duty_cycle = clk_rcg2_get_duty_cycle,
495 .set_duty_cycle = clk_rcg2_set_duty_cycle,
497 EXPORT_SYMBOL_GPL(clk_rcg2_ops);
499 const struct clk_ops clk_rcg2_floor_ops = {
500 .is_enabled = clk_rcg2_is_enabled,
501 .get_parent = clk_rcg2_get_parent,
502 .set_parent = clk_rcg2_set_parent,
503 .recalc_rate = clk_rcg2_recalc_rate,
504 .determine_rate = clk_rcg2_determine_floor_rate,
505 .set_rate = clk_rcg2_set_floor_rate,
506 .set_rate_and_parent = clk_rcg2_set_floor_rate_and_parent,
507 .get_duty_cycle = clk_rcg2_get_duty_cycle,
508 .set_duty_cycle = clk_rcg2_set_duty_cycle,
510 EXPORT_SYMBOL_GPL(clk_rcg2_floor_ops);
517 static const struct frac_entry frac_table_675m[] = { /* link rate of 270M */
518 { 52, 295 }, /* 119 M */
519 { 11, 57 }, /* 130.25 M */
520 { 63, 307 }, /* 138.50 M */
521 { 11, 50 }, /* 148.50 M */
522 { 47, 206 }, /* 154 M */
523 { 31, 100 }, /* 205.25 M */
524 { 107, 269 }, /* 268.50 M */
528 static struct frac_entry frac_table_810m[] = { /* Link rate of 162M */
529 { 31, 211 }, /* 119 M */
530 { 32, 199 }, /* 130.25 M */
531 { 63, 307 }, /* 138.50 M */
532 { 11, 60 }, /* 148.50 M */
533 { 50, 263 }, /* 154 M */
534 { 31, 120 }, /* 205.25 M */
535 { 119, 359 }, /* 268.50 M */
539 static int clk_edp_pixel_set_rate(struct clk_hw *hw, unsigned long rate,
540 unsigned long parent_rate)
542 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
543 struct freq_tbl f = *rcg->freq_tbl;
544 const struct frac_entry *frac;
546 s64 src_rate = parent_rate;
548 u32 mask = BIT(rcg->hid_width) - 1;
551 if (src_rate == 810000000)
552 frac = frac_table_810m;
554 frac = frac_table_675m;
556 for (; frac->num; frac++) {
558 request *= frac->den;
559 request = div_s64(request, frac->num);
560 if ((src_rate < (request - delta)) ||
561 (src_rate > (request + delta)))
564 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
567 f.pre_div >>= CFG_SRC_DIV_SHIFT;
572 return clk_rcg2_configure(rcg, &f);
578 static int clk_edp_pixel_set_rate_and_parent(struct clk_hw *hw,
579 unsigned long rate, unsigned long parent_rate, u8 index)
581 /* Parent index is set statically in frequency table */
582 return clk_edp_pixel_set_rate(hw, rate, parent_rate);
585 static int clk_edp_pixel_determine_rate(struct clk_hw *hw,
586 struct clk_rate_request *req)
588 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
589 const struct freq_tbl *f = rcg->freq_tbl;
590 const struct frac_entry *frac;
593 u32 mask = BIT(rcg->hid_width) - 1;
595 int index = qcom_find_src_index(hw, rcg->parent_map, f->src);
597 /* Force the correct parent */
598 req->best_parent_hw = clk_hw_get_parent_by_index(hw, index);
599 req->best_parent_rate = clk_hw_get_rate(req->best_parent_hw);
601 if (req->best_parent_rate == 810000000)
602 frac = frac_table_810m;
604 frac = frac_table_675m;
606 for (; frac->num; frac++) {
608 request *= frac->den;
609 request = div_s64(request, frac->num);
610 if ((req->best_parent_rate < (request - delta)) ||
611 (req->best_parent_rate > (request + delta)))
614 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
616 hid_div >>= CFG_SRC_DIV_SHIFT;
619 req->rate = calc_rate(req->best_parent_rate,
620 frac->num, frac->den,
621 !!frac->den, hid_div);
628 const struct clk_ops clk_edp_pixel_ops = {
629 .is_enabled = clk_rcg2_is_enabled,
630 .get_parent = clk_rcg2_get_parent,
631 .set_parent = clk_rcg2_set_parent,
632 .recalc_rate = clk_rcg2_recalc_rate,
633 .set_rate = clk_edp_pixel_set_rate,
634 .set_rate_and_parent = clk_edp_pixel_set_rate_and_parent,
635 .determine_rate = clk_edp_pixel_determine_rate,
637 EXPORT_SYMBOL_GPL(clk_edp_pixel_ops);
639 static int clk_byte_determine_rate(struct clk_hw *hw,
640 struct clk_rate_request *req)
642 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
643 const struct freq_tbl *f = rcg->freq_tbl;
644 int index = qcom_find_src_index(hw, rcg->parent_map, f->src);
645 unsigned long parent_rate, div;
646 u32 mask = BIT(rcg->hid_width) - 1;
652 req->best_parent_hw = p = clk_hw_get_parent_by_index(hw, index);
653 req->best_parent_rate = parent_rate = clk_hw_round_rate(p, req->rate);
655 div = DIV_ROUND_UP((2 * parent_rate), req->rate) - 1;
656 div = min_t(u32, div, mask);
658 req->rate = calc_rate(parent_rate, 0, 0, 0, div);
663 static int clk_byte_set_rate(struct clk_hw *hw, unsigned long rate,
664 unsigned long parent_rate)
666 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
667 struct freq_tbl f = *rcg->freq_tbl;
669 u32 mask = BIT(rcg->hid_width) - 1;
671 div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
672 div = min_t(u32, div, mask);
676 return clk_rcg2_configure(rcg, &f);
679 static int clk_byte_set_rate_and_parent(struct clk_hw *hw,
680 unsigned long rate, unsigned long parent_rate, u8 index)
682 /* Parent index is set statically in frequency table */
683 return clk_byte_set_rate(hw, rate, parent_rate);
686 const struct clk_ops clk_byte_ops = {
687 .is_enabled = clk_rcg2_is_enabled,
688 .get_parent = clk_rcg2_get_parent,
689 .set_parent = clk_rcg2_set_parent,
690 .recalc_rate = clk_rcg2_recalc_rate,
691 .set_rate = clk_byte_set_rate,
692 .set_rate_and_parent = clk_byte_set_rate_and_parent,
693 .determine_rate = clk_byte_determine_rate,
695 EXPORT_SYMBOL_GPL(clk_byte_ops);
697 static int clk_byte2_determine_rate(struct clk_hw *hw,
698 struct clk_rate_request *req)
700 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
701 unsigned long parent_rate, div;
702 u32 mask = BIT(rcg->hid_width) - 1;
704 unsigned long rate = req->rate;
709 p = req->best_parent_hw;
710 req->best_parent_rate = parent_rate = clk_hw_round_rate(p, rate);
712 div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
713 div = min_t(u32, div, mask);
715 req->rate = calc_rate(parent_rate, 0, 0, 0, div);
720 static int clk_byte2_set_rate(struct clk_hw *hw, unsigned long rate,
721 unsigned long parent_rate)
723 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
724 struct freq_tbl f = { 0 };
726 int i, num_parents = clk_hw_get_num_parents(hw);
727 u32 mask = BIT(rcg->hid_width) - 1;
730 div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
731 div = min_t(u32, div, mask);
735 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg);
736 cfg &= CFG_SRC_SEL_MASK;
737 cfg >>= CFG_SRC_SEL_SHIFT;
739 for (i = 0; i < num_parents; i++) {
740 if (cfg == rcg->parent_map[i].cfg) {
741 f.src = rcg->parent_map[i].src;
742 return clk_rcg2_configure(rcg, &f);
749 static int clk_byte2_set_rate_and_parent(struct clk_hw *hw,
750 unsigned long rate, unsigned long parent_rate, u8 index)
752 /* Read the hardware to determine parent during set_rate */
753 return clk_byte2_set_rate(hw, rate, parent_rate);
756 const struct clk_ops clk_byte2_ops = {
757 .is_enabled = clk_rcg2_is_enabled,
758 .get_parent = clk_rcg2_get_parent,
759 .set_parent = clk_rcg2_set_parent,
760 .recalc_rate = clk_rcg2_recalc_rate,
761 .set_rate = clk_byte2_set_rate,
762 .set_rate_and_parent = clk_byte2_set_rate_and_parent,
763 .determine_rate = clk_byte2_determine_rate,
765 EXPORT_SYMBOL_GPL(clk_byte2_ops);
767 static const struct frac_entry frac_table_pixel[] = {
776 static int clk_pixel_determine_rate(struct clk_hw *hw,
777 struct clk_rate_request *req)
779 unsigned long request, src_rate;
781 const struct frac_entry *frac = frac_table_pixel;
783 for (; frac->num; frac++) {
784 request = (req->rate * frac->den) / frac->num;
786 src_rate = clk_hw_round_rate(req->best_parent_hw, request);
787 if ((src_rate < (request - delta)) ||
788 (src_rate > (request + delta)))
791 req->best_parent_rate = src_rate;
792 req->rate = (src_rate * frac->num) / frac->den;
799 static int clk_pixel_set_rate(struct clk_hw *hw, unsigned long rate,
800 unsigned long parent_rate)
802 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
803 struct freq_tbl f = { 0 };
804 const struct frac_entry *frac = frac_table_pixel;
805 unsigned long request;
807 u32 mask = BIT(rcg->hid_width) - 1;
809 int i, num_parents = clk_hw_get_num_parents(hw);
811 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg);
812 cfg &= CFG_SRC_SEL_MASK;
813 cfg >>= CFG_SRC_SEL_SHIFT;
815 for (i = 0; i < num_parents; i++)
816 if (cfg == rcg->parent_map[i].cfg) {
817 f.src = rcg->parent_map[i].src;
821 for (; frac->num; frac++) {
822 request = (rate * frac->den) / frac->num;
824 if ((parent_rate < (request - delta)) ||
825 (parent_rate > (request + delta)))
828 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
831 f.pre_div >>= CFG_SRC_DIV_SHIFT;
836 return clk_rcg2_configure(rcg, &f);
841 static int clk_pixel_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
842 unsigned long parent_rate, u8 index)
844 return clk_pixel_set_rate(hw, rate, parent_rate);
847 const struct clk_ops clk_pixel_ops = {
848 .is_enabled = clk_rcg2_is_enabled,
849 .get_parent = clk_rcg2_get_parent,
850 .set_parent = clk_rcg2_set_parent,
851 .recalc_rate = clk_rcg2_recalc_rate,
852 .set_rate = clk_pixel_set_rate,
853 .set_rate_and_parent = clk_pixel_set_rate_and_parent,
854 .determine_rate = clk_pixel_determine_rate,
856 EXPORT_SYMBOL_GPL(clk_pixel_ops);
858 static int clk_gfx3d_determine_rate(struct clk_hw *hw,
859 struct clk_rate_request *req)
861 struct clk_rate_request parent_req = { .min_rate = 0, .max_rate = ULONG_MAX };
862 struct clk_rcg2_gfx3d *cgfx = to_clk_rcg2_gfx3d(hw);
863 struct clk_hw *xo, *p0, *p1, *p2;
864 unsigned long p0_rate;
865 u8 mux_div = cgfx->div;
872 * This function does ping-pong the RCG between PLLs: if we don't
873 * have at least one fixed PLL and two variable ones,
874 * then it's not going to work correctly.
876 if (WARN_ON(!p0 || !p1 || !p2))
879 xo = clk_hw_get_parent_by_index(hw, 0);
880 if (req->rate == clk_hw_get_rate(xo)) {
881 req->best_parent_hw = xo;
888 parent_req.rate = req->rate * mux_div;
890 /* This has to be a fixed rate PLL */
891 p0_rate = clk_hw_get_rate(p0);
893 if (parent_req.rate == p0_rate) {
894 req->rate = req->best_parent_rate = p0_rate;
895 req->best_parent_hw = p0;
899 if (req->best_parent_hw == p0) {
900 /* Are we going back to a previously used rate? */
901 if (clk_hw_get_rate(p2) == parent_req.rate)
902 req->best_parent_hw = p2;
904 req->best_parent_hw = p1;
905 } else if (req->best_parent_hw == p2) {
906 req->best_parent_hw = p1;
908 req->best_parent_hw = p2;
911 ret = __clk_determine_rate(req->best_parent_hw, &parent_req);
915 req->rate = req->best_parent_rate = parent_req.rate;
916 req->rate /= mux_div;
921 static int clk_gfx3d_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
922 unsigned long parent_rate, u8 index)
924 struct clk_rcg2_gfx3d *cgfx = to_clk_rcg2_gfx3d(hw);
925 struct clk_rcg2 *rcg = &cgfx->rcg;
929 cfg = rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
930 /* On some targets, the GFX3D RCG may need to divide PLL frequency */
932 cfg |= ((2 * cgfx->div) - 1) << CFG_SRC_DIV_SHIFT;
934 ret = regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, cfg);
938 return update_config(rcg);
941 static int clk_gfx3d_set_rate(struct clk_hw *hw, unsigned long rate,
942 unsigned long parent_rate)
945 * We should never get here; clk_gfx3d_determine_rate() should always
946 * make us use a different parent than what we're currently using, so
947 * clk_gfx3d_set_rate_and_parent() should always be called.
952 const struct clk_ops clk_gfx3d_ops = {
953 .is_enabled = clk_rcg2_is_enabled,
954 .get_parent = clk_rcg2_get_parent,
955 .set_parent = clk_rcg2_set_parent,
956 .recalc_rate = clk_rcg2_recalc_rate,
957 .set_rate = clk_gfx3d_set_rate,
958 .set_rate_and_parent = clk_gfx3d_set_rate_and_parent,
959 .determine_rate = clk_gfx3d_determine_rate,
961 EXPORT_SYMBOL_GPL(clk_gfx3d_ops);
963 static int clk_rcg2_set_force_enable(struct clk_hw *hw)
965 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
966 const char *name = clk_hw_get_name(hw);
969 ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG,
970 CMD_ROOT_EN, CMD_ROOT_EN);
974 /* wait for RCG to turn ON */
975 for (count = 500; count > 0; count--) {
976 if (clk_rcg2_is_enabled(hw))
982 pr_err("%s: RCG did not turn on\n", name);
986 static int clk_rcg2_clear_force_enable(struct clk_hw *hw)
988 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
990 return regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG,
995 clk_rcg2_shared_force_enable_clear(struct clk_hw *hw, const struct freq_tbl *f)
997 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1000 ret = clk_rcg2_set_force_enable(hw);
1004 ret = clk_rcg2_configure(rcg, f);
1008 return clk_rcg2_clear_force_enable(hw);
1011 static int clk_rcg2_shared_set_rate(struct clk_hw *hw, unsigned long rate,
1012 unsigned long parent_rate)
1014 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1015 const struct freq_tbl *f;
1017 f = qcom_find_freq(rcg->freq_tbl, rate);
1022 * In case clock is disabled, update the M, N and D registers, cache
1023 * the CFG value in parked_cfg and don't hit the update bit of CMD
1026 if (!clk_hw_is_enabled(hw))
1027 return __clk_rcg2_configure(rcg, f, &rcg->parked_cfg);
1029 return clk_rcg2_shared_force_enable_clear(hw, f);
1032 static int clk_rcg2_shared_set_rate_and_parent(struct clk_hw *hw,
1033 unsigned long rate, unsigned long parent_rate, u8 index)
1035 return clk_rcg2_shared_set_rate(hw, rate, parent_rate);
1038 static int clk_rcg2_shared_enable(struct clk_hw *hw)
1040 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1044 * Set the update bit because required configuration has already
1045 * been written in clk_rcg2_shared_set_rate()
1047 ret = clk_rcg2_set_force_enable(hw);
1051 /* Write back the stored configuration corresponding to current rate */
1052 ret = regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, rcg->parked_cfg);
1056 ret = update_config(rcg);
1060 return clk_rcg2_clear_force_enable(hw);
1063 static void clk_rcg2_shared_disable(struct clk_hw *hw)
1065 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1068 * Store current configuration as switching to safe source would clear
1069 * the SRC and DIV of CFG register
1071 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &rcg->parked_cfg);
1074 * Park the RCG at a safe configuration - sourced off of safe source.
1075 * Force enable and disable the RCG while configuring it to safeguard
1076 * against any update signal coming from the downstream clock.
1077 * The current parent is still prepared and enabled at this point, and
1078 * the safe source is always on while application processor subsystem
1079 * is online. Therefore, the RCG can safely switch its parent.
1081 clk_rcg2_set_force_enable(hw);
1083 regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
1084 rcg->safe_src_index << CFG_SRC_SEL_SHIFT);
1088 clk_rcg2_clear_force_enable(hw);
1091 static u8 clk_rcg2_shared_get_parent(struct clk_hw *hw)
1093 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1095 /* If the shared rcg is parked use the cached cfg instead */
1096 if (!clk_hw_is_enabled(hw))
1097 return __clk_rcg2_get_parent(hw, rcg->parked_cfg);
1099 return clk_rcg2_get_parent(hw);
1102 static int clk_rcg2_shared_set_parent(struct clk_hw *hw, u8 index)
1104 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1106 /* If the shared rcg is parked only update the cached cfg */
1107 if (!clk_hw_is_enabled(hw)) {
1108 rcg->parked_cfg &= ~CFG_SRC_SEL_MASK;
1109 rcg->parked_cfg |= rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
1114 return clk_rcg2_set_parent(hw, index);
1117 static unsigned long
1118 clk_rcg2_shared_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
1120 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1122 /* If the shared rcg is parked use the cached cfg instead */
1123 if (!clk_hw_is_enabled(hw))
1124 return __clk_rcg2_recalc_rate(hw, parent_rate, rcg->parked_cfg);
1126 return clk_rcg2_recalc_rate(hw, parent_rate);
1129 const struct clk_ops clk_rcg2_shared_ops = {
1130 .enable = clk_rcg2_shared_enable,
1131 .disable = clk_rcg2_shared_disable,
1132 .get_parent = clk_rcg2_shared_get_parent,
1133 .set_parent = clk_rcg2_shared_set_parent,
1134 .recalc_rate = clk_rcg2_shared_recalc_rate,
1135 .determine_rate = clk_rcg2_determine_rate,
1136 .set_rate = clk_rcg2_shared_set_rate,
1137 .set_rate_and_parent = clk_rcg2_shared_set_rate_and_parent,
1139 EXPORT_SYMBOL_GPL(clk_rcg2_shared_ops);
1141 /* Common APIs to be used for DFS based RCGR */
1142 static void clk_rcg2_dfs_populate_freq(struct clk_hw *hw, unsigned int l,
1145 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1147 unsigned long prate = 0;
1148 u32 val, mask, cfg, mode, src;
1151 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_DFSR(l), &cfg);
1153 mask = BIT(rcg->hid_width) - 1;
1156 f->pre_div = cfg & mask;
1158 src = cfg & CFG_SRC_SEL_MASK;
1159 src >>= CFG_SRC_SEL_SHIFT;
1161 num_parents = clk_hw_get_num_parents(hw);
1162 for (i = 0; i < num_parents; i++) {
1163 if (src == rcg->parent_map[i].cfg) {
1164 f->src = rcg->parent_map[i].src;
1165 p = clk_hw_get_parent_by_index(&rcg->clkr.hw, i);
1166 prate = clk_hw_get_rate(p);
1170 mode = cfg & CFG_MODE_MASK;
1171 mode >>= CFG_MODE_SHIFT;
1173 mask = BIT(rcg->mnd_width) - 1;
1174 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_M_DFSR(l),
1179 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_N_DFSR(l),
1187 f->freq = calc_rate(prate, f->m, f->n, mode, f->pre_div);
1190 static int clk_rcg2_dfs_populate_freq_table(struct clk_rcg2 *rcg)
1192 struct freq_tbl *freq_tbl;
1195 /* Allocate space for 1 extra since table is NULL terminated */
1196 freq_tbl = kcalloc(MAX_PERF_LEVEL + 1, sizeof(*freq_tbl), GFP_KERNEL);
1199 rcg->freq_tbl = freq_tbl;
1201 for (i = 0; i < MAX_PERF_LEVEL; i++)
1202 clk_rcg2_dfs_populate_freq(&rcg->clkr.hw, i, freq_tbl + i);
1207 static int clk_rcg2_dfs_determine_rate(struct clk_hw *hw,
1208 struct clk_rate_request *req)
1210 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1213 if (!rcg->freq_tbl) {
1214 ret = clk_rcg2_dfs_populate_freq_table(rcg);
1216 pr_err("Failed to update DFS tables for %s\n",
1217 clk_hw_get_name(hw));
1222 return clk_rcg2_determine_rate(hw, req);
1225 static unsigned long
1226 clk_rcg2_dfs_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
1228 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1229 u32 level, mask, cfg, m = 0, n = 0, mode, pre_div;
1231 regmap_read(rcg->clkr.regmap,
1232 rcg->cmd_rcgr + SE_CMD_DFSR_OFFSET, &level);
1233 level &= GENMASK(4, 1);
1237 return rcg->freq_tbl[level].freq;
1240 * Assume that parent_rate is actually the parent because
1241 * we can't do any better at figuring it out when the table
1242 * hasn't been populated yet. We only populate the table
1243 * in determine_rate because we can't guarantee the parents
1244 * will be registered with the framework until then.
1246 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_DFSR(level),
1249 mask = BIT(rcg->hid_width) - 1;
1252 pre_div = cfg & mask;
1254 mode = cfg & CFG_MODE_MASK;
1255 mode >>= CFG_MODE_SHIFT;
1257 mask = BIT(rcg->mnd_width) - 1;
1258 regmap_read(rcg->clkr.regmap,
1259 rcg->cmd_rcgr + SE_PERF_M_DFSR(level), &m);
1262 regmap_read(rcg->clkr.regmap,
1263 rcg->cmd_rcgr + SE_PERF_N_DFSR(level), &n);
1269 return calc_rate(parent_rate, m, n, mode, pre_div);
1272 static const struct clk_ops clk_rcg2_dfs_ops = {
1273 .is_enabled = clk_rcg2_is_enabled,
1274 .get_parent = clk_rcg2_get_parent,
1275 .determine_rate = clk_rcg2_dfs_determine_rate,
1276 .recalc_rate = clk_rcg2_dfs_recalc_rate,
1279 static int clk_rcg2_enable_dfs(const struct clk_rcg_dfs_data *data,
1280 struct regmap *regmap)
1282 struct clk_rcg2 *rcg = data->rcg;
1283 struct clk_init_data *init = data->init;
1287 ret = regmap_read(regmap, rcg->cmd_rcgr + SE_CMD_DFSR_OFFSET, &val);
1291 if (!(val & SE_CMD_DFS_EN))
1295 * Rate changes with consumer writing a register in
1296 * their own I/O region
1298 init->flags |= CLK_GET_RATE_NOCACHE;
1299 init->ops = &clk_rcg2_dfs_ops;
1301 rcg->freq_tbl = NULL;
1306 int qcom_cc_register_rcg_dfs(struct regmap *regmap,
1307 const struct clk_rcg_dfs_data *rcgs, size_t len)
1311 for (i = 0; i < len; i++) {
1312 ret = clk_rcg2_enable_dfs(&rcgs[i], regmap);
1319 EXPORT_SYMBOL_GPL(qcom_cc_register_rcg_dfs);
1321 static int clk_rcg2_dp_set_rate(struct clk_hw *hw, unsigned long rate,
1322 unsigned long parent_rate)
1324 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1325 struct freq_tbl f = { 0 };
1326 u32 mask = BIT(rcg->hid_width) - 1;
1328 int i, num_parents = clk_hw_get_num_parents(hw);
1329 unsigned long num, den;
1331 rational_best_approximation(parent_rate, rate,
1332 GENMASK(rcg->mnd_width - 1, 0),
1333 GENMASK(rcg->mnd_width - 1, 0), &den, &num);
1338 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg);
1340 cfg &= CFG_SRC_SEL_MASK;
1341 cfg >>= CFG_SRC_SEL_SHIFT;
1343 for (i = 0; i < num_parents; i++) {
1344 if (cfg == rcg->parent_map[i].cfg) {
1345 f.src = rcg->parent_map[i].src;
1350 f.pre_div = hid_div;
1351 f.pre_div >>= CFG_SRC_DIV_SHIFT;
1362 return clk_rcg2_configure(rcg, &f);
1365 static int clk_rcg2_dp_set_rate_and_parent(struct clk_hw *hw,
1366 unsigned long rate, unsigned long parent_rate, u8 index)
1368 return clk_rcg2_dp_set_rate(hw, rate, parent_rate);
1371 static int clk_rcg2_dp_determine_rate(struct clk_hw *hw,
1372 struct clk_rate_request *req)
1374 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1375 unsigned long num, den;
1378 /* Parent rate is a fixed phy link rate */
1379 rational_best_approximation(req->best_parent_rate, req->rate,
1380 GENMASK(rcg->mnd_width - 1, 0),
1381 GENMASK(rcg->mnd_width - 1, 0), &den, &num);
1386 tmp = req->best_parent_rate * num;
1393 const struct clk_ops clk_dp_ops = {
1394 .is_enabled = clk_rcg2_is_enabled,
1395 .get_parent = clk_rcg2_get_parent,
1396 .set_parent = clk_rcg2_set_parent,
1397 .recalc_rate = clk_rcg2_recalc_rate,
1398 .set_rate = clk_rcg2_dp_set_rate,
1399 .set_rate_and_parent = clk_rcg2_dp_set_rate_and_parent,
1400 .determine_rate = clk_rcg2_dp_determine_rate,
1402 EXPORT_SYMBOL_GPL(clk_dp_ops);