|
8 | 8 | #include <linux/err.h>
|
9 | 9 | #include <linux/bug.h>
|
10 | 10 | #include <linux/export.h>
|
| 11 | +#include <linux/clk.h> |
11 | 12 | #include <linux/clk-provider.h>
|
12 | 13 | #include <linux/delay.h>
|
13 | 14 | #include <linux/rational.h>
|
14 | 15 | #include <linux/regmap.h>
|
15 | 16 | #include <linux/math64.h>
|
| 17 | +#include <linux/gcd.h> |
16 | 18 | #include <linux/minmax.h>
|
17 | 19 | #include <linux/slab.h>
|
18 | 20 |
|
|
32 | 34 |
|
33 | 35 | #define CFG_REG 0x4
|
34 | 36 | #define CFG_SRC_DIV_SHIFT 0
|
| 37 | +#define CFG_SRC_DIV_LENGTH 8 |
35 | 38 | #define CFG_SRC_SEL_SHIFT 8
|
36 | 39 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT)
|
37 | 40 | #define CFG_MODE_SHIFT 12
|
@@ -148,6 +151,17 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)
|
148 | 151 | return update_config(rcg);
|
149 | 152 | }
|
150 | 153 |
|
| 154 | +/** |
| 155 | + * convert_to_reg_val() - Convert divisor values to hardware values. |
| 156 | + * |
| 157 | + * @f: Frequency table with pure m/n/pre_div parameters. |
| 158 | + */ |
| 159 | +static void convert_to_reg_val(struct freq_tbl *f) |
| 160 | +{ |
| 161 | + f->pre_div *= 2; |
| 162 | + f->pre_div -= 1; |
| 163 | +} |
| 164 | + |
151 | 165 | /**
|
152 | 166 | * calc_rate() - Calculate rate based on m/n:d values
|
153 | 167 | *
|
@@ -402,6 +416,90 @@ static int clk_rcg2_fm_determine_rate(struct clk_hw *hw,
|
402 | 416 | return _freq_tbl_fm_determine_rate(hw, rcg->freq_multi_tbl, req);
|
403 | 417 | }
|
404 | 418 |
|
| 419 | +/** |
| 420 | + * clk_rcg2_split_div() - Split multiplier that doesn't fit in n neither in pre_div. |
| 421 | + * |
| 422 | + * @multiplier: Multiplier to split between n and pre_div. |
| 423 | + * @pre_div: Pointer to pre divisor value. |
| 424 | + * @n: Pointer to n divisor value. |
| 425 | + * @pre_div_max: Pre divisor maximum value. |
| 426 | + */ |
| 427 | +static inline void clk_rcg2_split_div(int multiplier, unsigned int *pre_div, |
| 428 | + u16 *n, unsigned int pre_div_max) |
| 429 | +{ |
| 430 | + *n = mult_frac(multiplier * *n, *pre_div, pre_div_max); |
| 431 | + *pre_div = pre_div_max; |
| 432 | +} |
| 433 | + |
| 434 | +static void clk_rcg2_calc_mnd(u64 parent_rate, u64 rate, struct freq_tbl *f, |
| 435 | + unsigned int mnd_max, unsigned int pre_div_max) |
| 436 | +{ |
| 437 | + int i = 2; |
| 438 | + unsigned int pre_div = 1; |
| 439 | + unsigned long rates_gcd, scaled_parent_rate; |
| 440 | + u16 m, n = 1, n_candidate = 1, n_max; |
| 441 | + |
| 442 | + rates_gcd = gcd(parent_rate, rate); |
| 443 | + m = div64_u64(rate, rates_gcd); |
| 444 | + scaled_parent_rate = div64_u64(parent_rate, rates_gcd); |
| 445 | + while (scaled_parent_rate > (mnd_max + m) * pre_div_max) { |
| 446 | + // we're exceeding divisor's range, trying lower scale. |
| 447 | + if (m > 1) { |
| 448 | + m--; |
| 449 | + scaled_parent_rate = mult_frac(scaled_parent_rate, m, (m + 1)); |
| 450 | + } else { |
| 451 | + // cannot lower scale, just set max divisor values. |
| 452 | + f->n = mnd_max + m; |
| 453 | + f->pre_div = pre_div_max; |
| 454 | + f->m = m; |
| 455 | + return; |
| 456 | + } |
| 457 | + } |
| 458 | + |
| 459 | + n_max = m + mnd_max; |
| 460 | + |
| 461 | + while (scaled_parent_rate > 1) { |
| 462 | + while (scaled_parent_rate % i == 0) { |
| 463 | + n_candidate *= i; |
| 464 | + if (n_candidate < n_max) |
| 465 | + n = n_candidate; |
| 466 | + else if (pre_div * i < pre_div_max) |
| 467 | + pre_div *= i; |
| 468 | + else |
| 469 | + clk_rcg2_split_div(i, &pre_div, &n, pre_div_max); |
| 470 | + |
| 471 | + scaled_parent_rate /= i; |
| 472 | + } |
| 473 | + i++; |
| 474 | + } |
| 475 | + |
| 476 | + f->m = m; |
| 477 | + f->n = n; |
| 478 | + f->pre_div = pre_div > 1 ? pre_div : 0; |
| 479 | +} |
| 480 | + |
| 481 | +static int clk_rcg2_determine_gp_rate(struct clk_hw *hw, |
| 482 | + struct clk_rate_request *req) |
| 483 | +{ |
| 484 | + struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 485 | + struct freq_tbl f_tbl = {}, *f = &f_tbl; |
| 486 | + int mnd_max = BIT(rcg->mnd_width) - 1; |
| 487 | + int hid_max = BIT(rcg->hid_width) - 1; |
| 488 | + struct clk_hw *parent; |
| 489 | + u64 parent_rate; |
| 490 | + |
| 491 | + parent = clk_hw_get_parent(hw); |
| 492 | + parent_rate = clk_get_rate(parent->clk); |
| 493 | + if (!parent_rate) |
| 494 | + return -EINVAL; |
| 495 | + |
| 496 | + clk_rcg2_calc_mnd(parent_rate, req->rate, f, mnd_max, hid_max / 2); |
| 497 | + convert_to_reg_val(f); |
| 498 | + req->rate = calc_rate(parent_rate, f->m, f->n, f->n, f->pre_div); |
| 499 | + |
| 500 | + return 0; |
| 501 | +} |
| 502 | + |
405 | 503 | static int __clk_rcg2_configure_parent(struct clk_rcg2 *rcg, u8 src, u32 *_cfg)
|
406 | 504 | {
|
407 | 505 | struct clk_hw *hw = &rcg->clkr.hw;
|
@@ -499,6 +597,26 @@ static int clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f)
|
499 | 597 | return update_config(rcg);
|
500 | 598 | }
|
501 | 599 |
|
| 600 | +static int clk_rcg2_configure_gp(struct clk_rcg2 *rcg, const struct freq_tbl *f) |
| 601 | +{ |
| 602 | + u32 cfg; |
| 603 | + int ret; |
| 604 | + |
| 605 | + ret = regmap_read(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), &cfg); |
| 606 | + if (ret) |
| 607 | + return ret; |
| 608 | + |
| 609 | + ret = __clk_rcg2_configure_mnd(rcg, f, &cfg); |
| 610 | + if (ret) |
| 611 | + return ret; |
| 612 | + |
| 613 | + ret = regmap_write(rcg->clkr.regmap, RCG_CFG_OFFSET(rcg), cfg); |
| 614 | + if (ret) |
| 615 | + return ret; |
| 616 | + |
| 617 | + return update_config(rcg); |
| 618 | +} |
| 619 | + |
502 | 620 | static int __clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
|
503 | 621 | enum freq_policy policy)
|
504 | 622 | {
|
@@ -552,6 +670,22 @@ static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,
|
552 | 670 | return __clk_rcg2_set_rate(hw, rate, CEIL);
|
553 | 671 | }
|
554 | 672 |
|
| 673 | +static int clk_rcg2_set_gp_rate(struct clk_hw *hw, unsigned long rate, |
| 674 | + unsigned long parent_rate) |
| 675 | +{ |
| 676 | + struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 677 | + int mnd_max = BIT(rcg->mnd_width) - 1; |
| 678 | + int hid_max = BIT(rcg->hid_width) - 1; |
| 679 | + struct freq_tbl f_tbl = {}, *f = &f_tbl; |
| 680 | + int ret; |
| 681 | + |
| 682 | + clk_rcg2_calc_mnd(parent_rate, rate, f, mnd_max, hid_max / 2); |
| 683 | + convert_to_reg_val(f); |
| 684 | + ret = clk_rcg2_configure_gp(rcg, f); |
| 685 | + |
| 686 | + return ret; |
| 687 | +} |
| 688 | + |
555 | 689 | static int clk_rcg2_set_floor_rate(struct clk_hw *hw, unsigned long rate,
|
556 | 690 | unsigned long parent_rate)
|
557 | 691 | {
|
@@ -679,6 +813,18 @@ const struct clk_ops clk_rcg2_ops = {
|
679 | 813 | };
|
680 | 814 | EXPORT_SYMBOL_GPL(clk_rcg2_ops);
|
681 | 815 |
|
| 816 | +const struct clk_ops clk_rcg2_gp_ops = { |
| 817 | + .is_enabled = clk_rcg2_is_enabled, |
| 818 | + .get_parent = clk_rcg2_get_parent, |
| 819 | + .set_parent = clk_rcg2_set_parent, |
| 820 | + .recalc_rate = clk_rcg2_recalc_rate, |
| 821 | + .determine_rate = clk_rcg2_determine_gp_rate, |
| 822 | + .set_rate = clk_rcg2_set_gp_rate, |
| 823 | + .get_duty_cycle = clk_rcg2_get_duty_cycle, |
| 824 | + .set_duty_cycle = clk_rcg2_set_duty_cycle, |
| 825 | +}; |
| 826 | +EXPORT_SYMBOL_GPL(clk_rcg2_gp_ops); |
| 827 | + |
682 | 828 | const struct clk_ops clk_rcg2_floor_ops = {
|
683 | 829 | .is_enabled = clk_rcg2_is_enabled,
|
684 | 830 | .get_parent = clk_rcg2_get_parent,
|
|
0 commit comments