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/regmap.h>
14 #include <linux/math64.h>
15 #include <linux/slab.h>
17 #include <asm/div64.h>
23 #define CMD_UPDATE BIT(0)
24 #define CMD_ROOT_EN BIT(1)
25 #define CMD_DIRTY_CFG BIT(4)
26 #define CMD_DIRTY_N BIT(5)
27 #define CMD_DIRTY_M BIT(6)
28 #define CMD_DIRTY_D BIT(7)
29 #define CMD_ROOT_OFF BIT(31)
32 #define CFG_SRC_DIV_SHIFT 0
33 #define CFG_SRC_SEL_SHIFT 8
34 #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT)
35 #define CFG_MODE_SHIFT 12
36 #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT)
37 #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT)
38 #define CFG_HW_CLK_CTRL_MASK BIT(20)
44 #define RCG_CFG_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + CFG_REG)
45 #define RCG_M_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + M_REG)
46 #define RCG_N_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + N_REG)
47 #define RCG_D_OFFSET(rcg) ((rcg)->cmd_rcgr + (rcg)->cfg_off + D_REG)
49 /* Dynamic Frequency Scaling */
50 #define MAX_PERF_LEVEL 8
51 #define SE_CMD_DFSR_OFFSET 0x14
52 #define SE_CMD_DFS_EN BIT(0)
53 #define SE_PERF_DFSR(level) (0x1c + 0x4 * (level))
54 #define SE_PERF_M_DFSR(level) (0x5c + 0x4 * (level))
55 #define SE_PERF_N_DFSR(level) (0x9c + 0x4 * (level))
62 static int clk_rcg2_is_enabled(struct clk_hw *hw)
64 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
68 ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd);
72 return (cmd & CMD_ROOT_OFF) == 0;
75 static u8 clk_rcg2_get_parent(struct clk_hw *hw)
77 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
78 int num_parents = clk_hw_get_num_parents(hw);
82 ret = regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg);
86 cfg &= CFG_SRC_SEL_MASK;
87 cfg >>= CFG_SRC_SEL_SHIFT;
89 for (i = 0; i < num_parents; i++)
90 if (cfg == rcg->parent_map[i].cfg)
94 pr_debug("%s: Clock %s has invalid parent, using default.\n",
95 __func__, clk_hw_get_name(hw));
99 static int update_config(struct clk_rcg2 *rcg)
103 struct clk_hw *hw = &rcg->clkr.hw;
104 const char *name = clk_hw_get_name(hw);
106 ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG,
107 CMD_UPDATE, CMD_UPDATE);
111 /* Wait for update to take effect */
112 for (count = 500; count > 0; count--) {
113 ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd);
116 if (!(cmd & CMD_UPDATE))
121 WARN(1, "%s: rcg didn't update its configuration.", name);
125 static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)
127 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
129 u32 cfg = rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
131 ret = regmap_update_bits(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg),
132 CFG_SRC_SEL_MASK, cfg);
136 return update_config(rcg);
140 * Calculate m/n:d rate
143 * rate = ----------- x ---
147 calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div)
165 clk_rcg2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
167 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
168 u32 cfg, hid_div, m = 0, n = 0, mode = 0, mask;
170 regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg);
172 if (rcg->mnd_width) {
173 mask = BIT(rcg->mnd_width) - 1;
174 regmap_read(rcg->clkr.regmap, RCG_M_OFFSET(rcg), &m);
176 regmap_read(rcg->clkr.regmap, RCG_N_OFFSET(rcg), &n);
180 mode = cfg & CFG_MODE_MASK;
181 mode >>= CFG_MODE_SHIFT;
184 mask = BIT(rcg->hid_width) - 1;
185 hid_div = cfg >> CFG_SRC_DIV_SHIFT;
188 return calc_rate(parent_rate, m, n, mode, hid_div);
191 static int _freq_tbl_determine_rate(struct clk_hw *hw, const struct freq_tbl *f,
192 struct clk_rate_request *req,
193 enum freq_policy policy)
195 unsigned long clk_flags, rate = req->rate;
197 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
202 f = qcom_find_freq_floor(f, rate);
205 f = qcom_find_freq(f, rate);
214 index = qcom_find_src_index(hw, rcg->parent_map, f->src);
218 clk_flags = clk_hw_get_flags(hw);
219 p = clk_hw_get_parent_by_index(hw, index);
220 if (clk_flags & CLK_SET_RATE_PARENT) {
224 rate *= f->pre_div + 1;
234 rate = clk_hw_get_rate(p);
236 req->best_parent_hw = p;
237 req->best_parent_rate = rate;
243 static int clk_rcg2_determine_rate(struct clk_hw *hw,
244 struct clk_rate_request *req)
246 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
248 return _freq_tbl_determine_rate(hw, rcg->freq_tbl, req, CEIL);
251 static int clk_rcg2_determine_floor_rate(struct clk_hw *hw,
252 struct clk_rate_request *req)
254 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
256 return _freq_tbl_determine_rate(hw, rcg->freq_tbl, req, FLOOR);
259 static int __clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f)
262 struct clk_hw *hw = &rcg->clkr.hw;
263 int ret, index = qcom_find_src_index(hw, rcg->parent_map, f->src);
268 if (rcg->mnd_width && f->n) {
269 mask = BIT(rcg->mnd_width) - 1;
270 ret = regmap_update_bits(rcg->clkr.regmap,
271 RCG_M_OFFSET(rcg), mask, f->m);
275 ret = regmap_update_bits(rcg->clkr.regmap,
276 RCG_N_OFFSET(rcg), mask, ~(f->n - f->m));
280 ret = regmap_update_bits(rcg->clkr.regmap,
281 RCG_D_OFFSET(rcg), mask, ~f->n);
286 mask = BIT(rcg->hid_width) - 1;
287 mask |= CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK;
288 cfg = f->pre_div << CFG_SRC_DIV_SHIFT;
289 cfg |= rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
290 if (rcg->mnd_width && f->n && (f->m != f->n))
291 cfg |= CFG_MODE_DUAL_EDGE;
292 return regmap_update_bits(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg),
296 static int clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f)
300 ret = __clk_rcg2_configure(rcg, f);
304 return update_config(rcg);
307 static int __clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
308 enum freq_policy policy)
310 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
311 const struct freq_tbl *f;
315 f = qcom_find_freq_floor(rcg->freq_tbl, rate);
318 f = qcom_find_freq(rcg->freq_tbl, rate);
327 return clk_rcg2_configure(rcg, f);
330 static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
331 unsigned long parent_rate)
333 return __clk_rcg2_set_rate(hw, rate, CEIL);
336 static int clk_rcg2_set_floor_rate(struct clk_hw *hw, unsigned long rate,
337 unsigned long parent_rate)
339 return __clk_rcg2_set_rate(hw, rate, FLOOR);
342 static int clk_rcg2_set_rate_and_parent(struct clk_hw *hw,
343 unsigned long rate, unsigned long parent_rate, u8 index)
345 return __clk_rcg2_set_rate(hw, rate, CEIL);
348 static int clk_rcg2_set_floor_rate_and_parent(struct clk_hw *hw,
349 unsigned long rate, unsigned long parent_rate, u8 index)
351 return __clk_rcg2_set_rate(hw, rate, FLOOR);
354 const struct clk_ops clk_rcg2_ops = {
355 .is_enabled = clk_rcg2_is_enabled,
356 .get_parent = clk_rcg2_get_parent,
357 .set_parent = clk_rcg2_set_parent,
358 .recalc_rate = clk_rcg2_recalc_rate,
359 .determine_rate = clk_rcg2_determine_rate,
360 .set_rate = clk_rcg2_set_rate,
361 .set_rate_and_parent = clk_rcg2_set_rate_and_parent,
363 EXPORT_SYMBOL_GPL(clk_rcg2_ops);
365 const struct clk_ops clk_rcg2_floor_ops = {
366 .is_enabled = clk_rcg2_is_enabled,
367 .get_parent = clk_rcg2_get_parent,
368 .set_parent = clk_rcg2_set_parent,
369 .recalc_rate = clk_rcg2_recalc_rate,
370 .determine_rate = clk_rcg2_determine_floor_rate,
371 .set_rate = clk_rcg2_set_floor_rate,
372 .set_rate_and_parent = clk_rcg2_set_floor_rate_and_parent,
374 EXPORT_SYMBOL_GPL(clk_rcg2_floor_ops);
381 static const struct frac_entry frac_table_675m[] = { /* link rate of 270M */
382 { 52, 295 }, /* 119 M */
383 { 11, 57 }, /* 130.25 M */
384 { 63, 307 }, /* 138.50 M */
385 { 11, 50 }, /* 148.50 M */
386 { 47, 206 }, /* 154 M */
387 { 31, 100 }, /* 205.25 M */
388 { 107, 269 }, /* 268.50 M */
392 static struct frac_entry frac_table_810m[] = { /* Link rate of 162M */
393 { 31, 211 }, /* 119 M */
394 { 32, 199 }, /* 130.25 M */
395 { 63, 307 }, /* 138.50 M */
396 { 11, 60 }, /* 148.50 M */
397 { 50, 263 }, /* 154 M */
398 { 31, 120 }, /* 205.25 M */
399 { 119, 359 }, /* 268.50 M */
403 static int clk_edp_pixel_set_rate(struct clk_hw *hw, unsigned long rate,
404 unsigned long parent_rate)
406 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
407 struct freq_tbl f = *rcg->freq_tbl;
408 const struct frac_entry *frac;
410 s64 src_rate = parent_rate;
412 u32 mask = BIT(rcg->hid_width) - 1;
415 if (src_rate == 810000000)
416 frac = frac_table_810m;
418 frac = frac_table_675m;
420 for (; frac->num; frac++) {
422 request *= frac->den;
423 request = div_s64(request, frac->num);
424 if ((src_rate < (request - delta)) ||
425 (src_rate > (request + delta)))
428 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
431 f.pre_div >>= CFG_SRC_DIV_SHIFT;
436 return clk_rcg2_configure(rcg, &f);
442 static int clk_edp_pixel_set_rate_and_parent(struct clk_hw *hw,
443 unsigned long rate, unsigned long parent_rate, u8 index)
445 /* Parent index is set statically in frequency table */
446 return clk_edp_pixel_set_rate(hw, rate, parent_rate);
449 static int clk_edp_pixel_determine_rate(struct clk_hw *hw,
450 struct clk_rate_request *req)
452 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
453 const struct freq_tbl *f = rcg->freq_tbl;
454 const struct frac_entry *frac;
457 u32 mask = BIT(rcg->hid_width) - 1;
459 int index = qcom_find_src_index(hw, rcg->parent_map, f->src);
461 /* Force the correct parent */
462 req->best_parent_hw = clk_hw_get_parent_by_index(hw, index);
463 req->best_parent_rate = clk_hw_get_rate(req->best_parent_hw);
465 if (req->best_parent_rate == 810000000)
466 frac = frac_table_810m;
468 frac = frac_table_675m;
470 for (; frac->num; frac++) {
472 request *= frac->den;
473 request = div_s64(request, frac->num);
474 if ((req->best_parent_rate < (request - delta)) ||
475 (req->best_parent_rate > (request + delta)))
478 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
480 hid_div >>= CFG_SRC_DIV_SHIFT;
483 req->rate = calc_rate(req->best_parent_rate,
484 frac->num, frac->den,
485 !!frac->den, hid_div);
492 const struct clk_ops clk_edp_pixel_ops = {
493 .is_enabled = clk_rcg2_is_enabled,
494 .get_parent = clk_rcg2_get_parent,
495 .set_parent = clk_rcg2_set_parent,
496 .recalc_rate = clk_rcg2_recalc_rate,
497 .set_rate = clk_edp_pixel_set_rate,
498 .set_rate_and_parent = clk_edp_pixel_set_rate_and_parent,
499 .determine_rate = clk_edp_pixel_determine_rate,
501 EXPORT_SYMBOL_GPL(clk_edp_pixel_ops);
503 static int clk_byte_determine_rate(struct clk_hw *hw,
504 struct clk_rate_request *req)
506 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
507 const struct freq_tbl *f = rcg->freq_tbl;
508 int index = qcom_find_src_index(hw, rcg->parent_map, f->src);
509 unsigned long parent_rate, div;
510 u32 mask = BIT(rcg->hid_width) - 1;
516 req->best_parent_hw = p = clk_hw_get_parent_by_index(hw, index);
517 req->best_parent_rate = parent_rate = clk_hw_round_rate(p, req->rate);
519 div = DIV_ROUND_UP((2 * parent_rate), req->rate) - 1;
520 div = min_t(u32, div, mask);
522 req->rate = calc_rate(parent_rate, 0, 0, 0, div);
527 static int clk_byte_set_rate(struct clk_hw *hw, unsigned long rate,
528 unsigned long parent_rate)
530 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
531 struct freq_tbl f = *rcg->freq_tbl;
533 u32 mask = BIT(rcg->hid_width) - 1;
535 div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
536 div = min_t(u32, div, mask);
540 return clk_rcg2_configure(rcg, &f);
543 static int clk_byte_set_rate_and_parent(struct clk_hw *hw,
544 unsigned long rate, unsigned long parent_rate, u8 index)
546 /* Parent index is set statically in frequency table */
547 return clk_byte_set_rate(hw, rate, parent_rate);
550 const struct clk_ops clk_byte_ops = {
551 .is_enabled = clk_rcg2_is_enabled,
552 .get_parent = clk_rcg2_get_parent,
553 .set_parent = clk_rcg2_set_parent,
554 .recalc_rate = clk_rcg2_recalc_rate,
555 .set_rate = clk_byte_set_rate,
556 .set_rate_and_parent = clk_byte_set_rate_and_parent,
557 .determine_rate = clk_byte_determine_rate,
559 EXPORT_SYMBOL_GPL(clk_byte_ops);
561 static int clk_byte2_determine_rate(struct clk_hw *hw,
562 struct clk_rate_request *req)
564 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
565 unsigned long parent_rate, div;
566 u32 mask = BIT(rcg->hid_width) - 1;
568 unsigned long rate = req->rate;
573 p = req->best_parent_hw;
574 req->best_parent_rate = parent_rate = clk_hw_round_rate(p, rate);
576 div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
577 div = min_t(u32, div, mask);
579 req->rate = calc_rate(parent_rate, 0, 0, 0, div);
584 static int clk_byte2_set_rate(struct clk_hw *hw, unsigned long rate,
585 unsigned long parent_rate)
587 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
588 struct freq_tbl f = { 0 };
590 int i, num_parents = clk_hw_get_num_parents(hw);
591 u32 mask = BIT(rcg->hid_width) - 1;
594 div = DIV_ROUND_UP((2 * parent_rate), rate) - 1;
595 div = min_t(u32, div, mask);
599 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg);
600 cfg &= CFG_SRC_SEL_MASK;
601 cfg >>= CFG_SRC_SEL_SHIFT;
603 for (i = 0; i < num_parents; i++) {
604 if (cfg == rcg->parent_map[i].cfg) {
605 f.src = rcg->parent_map[i].src;
606 return clk_rcg2_configure(rcg, &f);
613 static int clk_byte2_set_rate_and_parent(struct clk_hw *hw,
614 unsigned long rate, unsigned long parent_rate, u8 index)
616 /* Read the hardware to determine parent during set_rate */
617 return clk_byte2_set_rate(hw, rate, parent_rate);
620 const struct clk_ops clk_byte2_ops = {
621 .is_enabled = clk_rcg2_is_enabled,
622 .get_parent = clk_rcg2_get_parent,
623 .set_parent = clk_rcg2_set_parent,
624 .recalc_rate = clk_rcg2_recalc_rate,
625 .set_rate = clk_byte2_set_rate,
626 .set_rate_and_parent = clk_byte2_set_rate_and_parent,
627 .determine_rate = clk_byte2_determine_rate,
629 EXPORT_SYMBOL_GPL(clk_byte2_ops);
631 static const struct frac_entry frac_table_pixel[] = {
639 static int clk_pixel_determine_rate(struct clk_hw *hw,
640 struct clk_rate_request *req)
642 unsigned long request, src_rate;
644 const struct frac_entry *frac = frac_table_pixel;
646 for (; frac->num; frac++) {
647 request = (req->rate * frac->den) / frac->num;
649 src_rate = clk_hw_round_rate(req->best_parent_hw, request);
650 if ((src_rate < (request - delta)) ||
651 (src_rate > (request + delta)))
654 req->best_parent_rate = src_rate;
655 req->rate = (src_rate * frac->num) / frac->den;
662 static int clk_pixel_set_rate(struct clk_hw *hw, unsigned long rate,
663 unsigned long parent_rate)
665 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
666 struct freq_tbl f = { 0 };
667 const struct frac_entry *frac = frac_table_pixel;
668 unsigned long request;
670 u32 mask = BIT(rcg->hid_width) - 1;
672 int i, num_parents = clk_hw_get_num_parents(hw);
674 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg);
675 cfg &= CFG_SRC_SEL_MASK;
676 cfg >>= CFG_SRC_SEL_SHIFT;
678 for (i = 0; i < num_parents; i++)
679 if (cfg == rcg->parent_map[i].cfg) {
680 f.src = rcg->parent_map[i].src;
684 for (; frac->num; frac++) {
685 request = (rate * frac->den) / frac->num;
687 if ((parent_rate < (request - delta)) ||
688 (parent_rate > (request + delta)))
691 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
694 f.pre_div >>= CFG_SRC_DIV_SHIFT;
699 return clk_rcg2_configure(rcg, &f);
704 static int clk_pixel_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
705 unsigned long parent_rate, u8 index)
707 return clk_pixel_set_rate(hw, rate, parent_rate);
710 const struct clk_ops clk_pixel_ops = {
711 .is_enabled = clk_rcg2_is_enabled,
712 .get_parent = clk_rcg2_get_parent,
713 .set_parent = clk_rcg2_set_parent,
714 .recalc_rate = clk_rcg2_recalc_rate,
715 .set_rate = clk_pixel_set_rate,
716 .set_rate_and_parent = clk_pixel_set_rate_and_parent,
717 .determine_rate = clk_pixel_determine_rate,
719 EXPORT_SYMBOL_GPL(clk_pixel_ops);
721 static int clk_gfx3d_determine_rate(struct clk_hw *hw,
722 struct clk_rate_request *req)
724 struct clk_rate_request parent_req = { };
725 struct clk_hw *p2, *p8, *p9, *xo;
726 unsigned long p9_rate;
729 xo = clk_hw_get_parent_by_index(hw, 0);
730 if (req->rate == clk_hw_get_rate(xo)) {
731 req->best_parent_hw = xo;
735 p9 = clk_hw_get_parent_by_index(hw, 2);
736 p2 = clk_hw_get_parent_by_index(hw, 3);
737 p8 = clk_hw_get_parent_by_index(hw, 4);
739 /* PLL9 is a fixed rate PLL */
740 p9_rate = clk_hw_get_rate(p9);
742 parent_req.rate = req->rate = min(req->rate, p9_rate);
743 if (req->rate == p9_rate) {
744 req->rate = req->best_parent_rate = p9_rate;
745 req->best_parent_hw = p9;
749 if (req->best_parent_hw == p9) {
750 /* Are we going back to a previously used rate? */
751 if (clk_hw_get_rate(p8) == req->rate)
752 req->best_parent_hw = p8;
754 req->best_parent_hw = p2;
755 } else if (req->best_parent_hw == p8) {
756 req->best_parent_hw = p2;
758 req->best_parent_hw = p8;
761 ret = __clk_determine_rate(req->best_parent_hw, &parent_req);
765 req->rate = req->best_parent_rate = parent_req.rate;
770 static int clk_gfx3d_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
771 unsigned long parent_rate, u8 index)
773 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
777 /* Just mux it, we don't use the division or m/n hardware */
778 cfg = rcg->parent_map[index].cfg << CFG_SRC_SEL_SHIFT;
779 ret = regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, cfg);
783 return update_config(rcg);
786 static int clk_gfx3d_set_rate(struct clk_hw *hw, unsigned long rate,
787 unsigned long parent_rate)
790 * We should never get here; clk_gfx3d_determine_rate() should always
791 * make us use a different parent than what we're currently using, so
792 * clk_gfx3d_set_rate_and_parent() should always be called.
797 const struct clk_ops clk_gfx3d_ops = {
798 .is_enabled = clk_rcg2_is_enabled,
799 .get_parent = clk_rcg2_get_parent,
800 .set_parent = clk_rcg2_set_parent,
801 .recalc_rate = clk_rcg2_recalc_rate,
802 .set_rate = clk_gfx3d_set_rate,
803 .set_rate_and_parent = clk_gfx3d_set_rate_and_parent,
804 .determine_rate = clk_gfx3d_determine_rate,
806 EXPORT_SYMBOL_GPL(clk_gfx3d_ops);
808 static int clk_rcg2_set_force_enable(struct clk_hw *hw)
810 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
811 const char *name = clk_hw_get_name(hw);
814 ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG,
815 CMD_ROOT_EN, CMD_ROOT_EN);
819 /* wait for RCG to turn ON */
820 for (count = 500; count > 0; count--) {
821 if (clk_rcg2_is_enabled(hw))
827 pr_err("%s: RCG did not turn on\n", name);
831 static int clk_rcg2_clear_force_enable(struct clk_hw *hw)
833 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
835 return regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG,
840 clk_rcg2_shared_force_enable_clear(struct clk_hw *hw, const struct freq_tbl *f)
842 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
845 ret = clk_rcg2_set_force_enable(hw);
849 ret = clk_rcg2_configure(rcg, f);
853 return clk_rcg2_clear_force_enable(hw);
856 static int clk_rcg2_shared_set_rate(struct clk_hw *hw, unsigned long rate,
857 unsigned long parent_rate)
859 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
860 const struct freq_tbl *f;
862 f = qcom_find_freq(rcg->freq_tbl, rate);
867 * In case clock is disabled, update the CFG, M, N and D registers
868 * and don't hit the update bit of CMD register.
870 if (!__clk_is_enabled(hw->clk))
871 return __clk_rcg2_configure(rcg, f);
873 return clk_rcg2_shared_force_enable_clear(hw, f);
876 static int clk_rcg2_shared_set_rate_and_parent(struct clk_hw *hw,
877 unsigned long rate, unsigned long parent_rate, u8 index)
879 return clk_rcg2_shared_set_rate(hw, rate, parent_rate);
882 static int clk_rcg2_shared_enable(struct clk_hw *hw)
884 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
888 * Set the update bit because required configuration has already
889 * been written in clk_rcg2_shared_set_rate()
891 ret = clk_rcg2_set_force_enable(hw);
895 ret = update_config(rcg);
899 return clk_rcg2_clear_force_enable(hw);
902 static void clk_rcg2_shared_disable(struct clk_hw *hw)
904 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
908 * Store current configuration as switching to safe source would clear
909 * the SRC and DIV of CFG register
911 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg);
914 * Park the RCG at a safe configuration - sourced off of safe source.
915 * Force enable and disable the RCG while configuring it to safeguard
916 * against any update signal coming from the downstream clock.
917 * The current parent is still prepared and enabled at this point, and
918 * the safe source is always on while application processor subsystem
919 * is online. Therefore, the RCG can safely switch its parent.
921 clk_rcg2_set_force_enable(hw);
923 regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG,
924 rcg->safe_src_index << CFG_SRC_SEL_SHIFT);
928 clk_rcg2_clear_force_enable(hw);
930 /* Write back the stored configuration corresponding to current rate */
931 regmap_write(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, cfg);
934 const struct clk_ops clk_rcg2_shared_ops = {
935 .enable = clk_rcg2_shared_enable,
936 .disable = clk_rcg2_shared_disable,
937 .get_parent = clk_rcg2_get_parent,
938 .set_parent = clk_rcg2_set_parent,
939 .recalc_rate = clk_rcg2_recalc_rate,
940 .determine_rate = clk_rcg2_determine_rate,
941 .set_rate = clk_rcg2_shared_set_rate,
942 .set_rate_and_parent = clk_rcg2_shared_set_rate_and_parent,
944 EXPORT_SYMBOL_GPL(clk_rcg2_shared_ops);
946 /* Common APIs to be used for DFS based RCGR */
947 static void clk_rcg2_dfs_populate_freq(struct clk_hw *hw, unsigned int l,
950 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
952 unsigned long prate = 0;
953 u32 val, mask, cfg, mode;
956 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_DFSR(l), &cfg);
958 mask = BIT(rcg->hid_width) - 1;
961 f->pre_div = cfg & mask;
963 cfg &= CFG_SRC_SEL_MASK;
964 cfg >>= CFG_SRC_SEL_SHIFT;
966 num_parents = clk_hw_get_num_parents(hw);
967 for (i = 0; i < num_parents; i++) {
968 if (cfg == rcg->parent_map[i].cfg) {
969 f->src = rcg->parent_map[i].src;
970 p = clk_hw_get_parent_by_index(&rcg->clkr.hw, i);
971 prate = clk_hw_get_rate(p);
975 mode = cfg & CFG_MODE_MASK;
976 mode >>= CFG_MODE_SHIFT;
978 mask = BIT(rcg->mnd_width) - 1;
979 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_M_DFSR(l),
984 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_N_DFSR(l),
992 f->freq = calc_rate(prate, f->m, f->n, mode, f->pre_div);
995 static int clk_rcg2_dfs_populate_freq_table(struct clk_rcg2 *rcg)
997 struct freq_tbl *freq_tbl;
1000 /* Allocate space for 1 extra since table is NULL terminated */
1001 freq_tbl = kcalloc(MAX_PERF_LEVEL + 1, sizeof(*freq_tbl), GFP_KERNEL);
1004 rcg->freq_tbl = freq_tbl;
1006 for (i = 0; i < MAX_PERF_LEVEL; i++)
1007 clk_rcg2_dfs_populate_freq(&rcg->clkr.hw, i, freq_tbl + i);
1012 static int clk_rcg2_dfs_determine_rate(struct clk_hw *hw,
1013 struct clk_rate_request *req)
1015 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1018 if (!rcg->freq_tbl) {
1019 ret = clk_rcg2_dfs_populate_freq_table(rcg);
1021 pr_err("Failed to update DFS tables for %s\n",
1022 clk_hw_get_name(hw));
1027 return clk_rcg2_determine_rate(hw, req);
1030 static unsigned long
1031 clk_rcg2_dfs_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
1033 struct clk_rcg2 *rcg = to_clk_rcg2(hw);
1034 u32 level, mask, cfg, m = 0, n = 0, mode, pre_div;
1036 regmap_read(rcg->clkr.regmap,
1037 rcg->cmd_rcgr + SE_CMD_DFSR_OFFSET, &level);
1038 level &= GENMASK(4, 1);
1042 return rcg->freq_tbl[level].freq;
1045 * Assume that parent_rate is actually the parent because
1046 * we can't do any better at figuring it out when the table
1047 * hasn't been populated yet. We only populate the table
1048 * in determine_rate because we can't guarantee the parents
1049 * will be registered with the framework until then.
1051 regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + SE_PERF_DFSR(level),
1054 mask = BIT(rcg->hid_width) - 1;
1057 pre_div = cfg & mask;
1059 mode = cfg & CFG_MODE_MASK;
1060 mode >>= CFG_MODE_SHIFT;
1062 mask = BIT(rcg->mnd_width) - 1;
1063 regmap_read(rcg->clkr.regmap,
1064 rcg->cmd_rcgr + SE_PERF_M_DFSR(level), &m);
1067 regmap_read(rcg->clkr.regmap,
1068 rcg->cmd_rcgr + SE_PERF_N_DFSR(level), &n);
1074 return calc_rate(parent_rate, m, n, mode, pre_div);
1077 static const struct clk_ops clk_rcg2_dfs_ops = {
1078 .is_enabled = clk_rcg2_is_enabled,
1079 .get_parent = clk_rcg2_get_parent,
1080 .determine_rate = clk_rcg2_dfs_determine_rate,
1081 .recalc_rate = clk_rcg2_dfs_recalc_rate,
1084 static int clk_rcg2_enable_dfs(const struct clk_rcg_dfs_data *data,
1085 struct regmap *regmap)
1087 struct clk_rcg2 *rcg = data->rcg;
1088 struct clk_init_data *init = data->init;
1092 ret = regmap_read(regmap, rcg->cmd_rcgr + SE_CMD_DFSR_OFFSET, &val);
1096 if (!(val & SE_CMD_DFS_EN))
1100 * Rate changes with consumer writing a register in
1101 * their own I/O region
1103 init->flags |= CLK_GET_RATE_NOCACHE;
1104 init->ops = &clk_rcg2_dfs_ops;
1106 rcg->freq_tbl = NULL;
1111 int qcom_cc_register_rcg_dfs(struct regmap *regmap,
1112 const struct clk_rcg_dfs_data *rcgs, size_t len)
1116 for (i = 0; i < len; i++) {
1117 ret = clk_rcg2_enable_dfs(&rcgs[i], regmap);
1124 EXPORT_SYMBOL_GPL(qcom_cc_register_rcg_dfs);