2
0

0100-clk-qcom-clk-rcg2-introduce-support-for-multiple-con.patch 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. From 032be4f49dda786fea9e1501212f6cd09a7ded96 Mon Sep 17 00:00:00 2001
  2. From: Christian Marangi <[email protected]>
  3. Date: Thu, 3 Nov 2022 14:49:43 +0100
  4. Subject: [PATCH] clk: qcom: clk-rcg2: introduce support for multiple conf for
  5. same freq
  6. Some RCG frequency can be reached by multiple configuration.
  7. We currently declare multiple configuration for the same frequency but
  8. that is not supported and always the first configuration will be taken.
  9. These multiple configuration are needed as based on the current parent
  10. configuration, it may be needed to use a different configuration to
  11. reach the same frequency.
  12. To handle this introduce 2 new macro, FM and C.
  13. - FM is used to declare an empty freq_tbl with just the frequency and an
  14. array of confs to insert all the config for the provided frequency.
  15. - C is used to declare a fre_conf where src, pre_div, m and n are
  16. provided.
  17. The driver is changed to handle this special freq_tbl and select the
  18. correct config by calculating the final rate and deciding based on the
  19. one that is less different than the requested one.
  20. Tested-by: Robert Marko <[email protected]>
  21. Signed-off-by: Christian Marangi <[email protected]>
  22. ---
  23. drivers/clk/qcom/clk-rcg.h | 14 ++++++-
  24. drivers/clk/qcom/clk-rcg2.c | 84 +++++++++++++++++++++++++++++++++----
  25. 2 files changed, 88 insertions(+), 10 deletions(-)
  26. --- a/drivers/clk/qcom/clk-rcg.h
  27. +++ b/drivers/clk/qcom/clk-rcg.h
  28. @@ -7,7 +7,17 @@
  29. #include <linux/clk-provider.h>
  30. #include "clk-regmap.h"
  31. -#define F(f, s, h, m, n) { (f), (s), (2 * (h) - 1), (m), (n) }
  32. +#define F(f, s, h, m, n) { (f), (s), (2 * (h) - 1), (m), (n), 0, NULL }
  33. +
  34. +#define FM(_f, _confs) { .freq = (_f), .confs_num = ARRAY_SIZE(_confs), .confs = (_confs) }
  35. +#define C(s, h, m, n) { (s), (2 * (h) - 1), (m), (n) }
  36. +
  37. +struct freq_conf {
  38. + u8 src;
  39. + u8 pre_div;
  40. + u16 m;
  41. + u16 n;
  42. +};
  43. struct freq_tbl {
  44. unsigned long freq;
  45. @@ -15,6 +25,8 @@ struct freq_tbl {
  46. u8 pre_div;
  47. u16 m;
  48. u16 n;
  49. + int confs_num;
  50. + const struct freq_conf *confs;
  51. };
  52. /**
  53. --- a/drivers/clk/qcom/clk-rcg2.c
  54. +++ b/drivers/clk/qcom/clk-rcg2.c
  55. @@ -209,11 +209,60 @@ clk_rcg2_recalc_rate(struct clk_hw *hw,
  56. return __clk_rcg2_recalc_rate(hw, parent_rate, cfg);
  57. }
  58. +static void
  59. +clk_rcg2_select_conf(struct clk_hw *hw, struct freq_tbl *f_tbl,
  60. + const struct freq_tbl *f, unsigned long req_rate)
  61. +{
  62. + unsigned long best_rate = 0, parent_rate, rate;
  63. + const struct freq_conf *conf, *best_conf;
  64. + struct clk_rcg2 *rcg = to_clk_rcg2(hw);
  65. + struct clk_hw *p;
  66. + int index, i;
  67. +
  68. + /* Search in each provided config the one that is near the wanted rate */
  69. + for (i = 0, conf = f->confs; i < f->confs_num; i++, conf++) {
  70. + index = qcom_find_src_index(hw, rcg->parent_map, conf->src);
  71. + if (index < 0)
  72. + continue;
  73. +
  74. + p = clk_hw_get_parent_by_index(hw, index);
  75. + if (!p)
  76. + continue;
  77. +
  78. + parent_rate = clk_hw_get_rate(p);
  79. + rate = calc_rate(parent_rate, conf->n, conf->m, conf->n, conf->pre_div);
  80. +
  81. + if (rate == req_rate) {
  82. + best_conf = conf;
  83. + break;
  84. + }
  85. +
  86. + if (abs(req_rate - rate) < abs(best_rate - rate)) {
  87. + best_rate = rate;
  88. + best_conf = conf;
  89. + }
  90. + }
  91. +
  92. + /*
  93. + * Very unlikely.
  94. + * Force the first conf if we can't find a correct config.
  95. + */
  96. + if (unlikely(i == f->confs_num))
  97. + best_conf = f->confs;
  98. +
  99. + /* Apply the config */
  100. + f_tbl->src = best_conf->src;
  101. + f_tbl->pre_div = best_conf->pre_div;
  102. + f_tbl->m = best_conf->m;
  103. + f_tbl->n = best_conf->n;
  104. +}
  105. +
  106. static int _freq_tbl_determine_rate(struct clk_hw *hw, const struct freq_tbl *f,
  107. struct clk_rate_request *req,
  108. enum freq_policy policy)
  109. {
  110. unsigned long clk_flags, rate = req->rate;
  111. + struct freq_tbl f_tbl;
  112. struct clk_hw *p;
  113. struct clk_rcg2 *rcg = to_clk_rcg2(hw);
  114. int index;
  115. @@ -232,7 +281,15 @@ static int _freq_tbl_determine_rate(stru
  116. if (!f)
  117. return -EINVAL;
  118. - index = qcom_find_src_index(hw, rcg->parent_map, f->src);
  119. + f_tbl = *f;
  120. + /*
  121. + * A single freq may be reached by multiple configuration.
  122. + * Try to find the bast one if we have this kind of freq_table.
  123. + */
  124. + if (f->confs)
  125. + clk_rcg2_select_conf(hw, &f_tbl, f, rate);
  126. +
  127. + index = qcom_find_src_index(hw, rcg->parent_map, f_tbl.src);
  128. if (index < 0)
  129. return index;
  130. @@ -242,18 +299,18 @@ static int _freq_tbl_determine_rate(stru
  131. return -EINVAL;
  132. if (clk_flags & CLK_SET_RATE_PARENT) {
  133. - rate = f->freq;
  134. - if (f->pre_div) {
  135. + rate = f_tbl.freq;
  136. + if (f_tbl.pre_div) {
  137. if (!rate)
  138. rate = req->rate;
  139. rate /= 2;
  140. - rate *= f->pre_div + 1;
  141. + rate *= f_tbl.pre_div + 1;
  142. }
  143. - if (f->n) {
  144. + if (f_tbl.n) {
  145. u64 tmp = rate;
  146. - tmp = tmp * f->n;
  147. - do_div(tmp, f->m);
  148. + tmp = tmp * f_tbl.n;
  149. + do_div(tmp, f_tbl.m);
  150. rate = tmp;
  151. }
  152. } else {
  153. @@ -261,7 +318,7 @@ static int _freq_tbl_determine_rate(stru
  154. }
  155. req->best_parent_hw = p;
  156. req->best_parent_rate = rate;
  157. - req->rate = f->freq;
  158. + req->rate = f_tbl.freq;
  159. return 0;
  160. }
  161. @@ -357,6 +414,7 @@ static int __clk_rcg2_set_rate(struct cl
  162. {
  163. struct clk_rcg2 *rcg = to_clk_rcg2(hw);
  164. const struct freq_tbl *f;
  165. + struct freq_tbl f_tbl;
  166. switch (policy) {
  167. case FLOOR:
  168. @@ -372,7 +430,15 @@ static int __clk_rcg2_set_rate(struct cl
  169. if (!f)
  170. return -EINVAL;
  171. - return clk_rcg2_configure(rcg, f);
  172. + f_tbl = *f;
  173. + /*
  174. + * A single freq may be reached by multiple configuration.
  175. + * Try to find the best one if we have this kind of freq_table.
  176. + */
  177. + if (f->confs)
  178. + clk_rcg2_select_conf(hw, &f_tbl, f, rate);
  179. +
  180. + return clk_rcg2_configure(rcg, &f_tbl);
  181. }
  182. static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate,