0003-clk-sunxi-ng-Remove-the-use-of-rational-computations.patch 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. From ee28648cb2b4d4ab5c2eb8199ea86675fe19016b Mon Sep 17 00:00:00 2001
  2. From: Maxime Ripard <[email protected]>
  3. Date: Thu, 29 Sep 2016 22:53:12 +0200
  4. Subject: clk: sunxi-ng: Remove the use of rational computations
  5. While the rational library works great, it doesn't really allow us to add
  6. more constraints, like the minimum.
  7. Remove that in order to be able to deal with the constraints we'll need.
  8. Signed-off-by: Maxime Ripard <[email protected]>
  9. Acked-by: Chen-Yu Tsai <[email protected]>
  10. ---
  11. drivers/clk/sunxi-ng/Kconfig | 3 ---
  12. drivers/clk/sunxi-ng/ccu_nkm.c | 31 ++++++++++++-----------
  13. drivers/clk/sunxi-ng/ccu_nkmp.c | 37 ++++++++++++++--------------
  14. drivers/clk/sunxi-ng/ccu_nm.c | 54 +++++++++++++++++++++++++++++++----------
  15. 4 files changed, 74 insertions(+), 51 deletions(-)
  16. --- a/drivers/clk/sunxi-ng/Kconfig
  17. +++ b/drivers/clk/sunxi-ng/Kconfig
  18. @@ -36,17 +36,14 @@ config SUNXI_CCU_NK
  19. config SUNXI_CCU_NKM
  20. bool
  21. - select RATIONAL
  22. select SUNXI_CCU_GATE
  23. config SUNXI_CCU_NKMP
  24. bool
  25. - select RATIONAL
  26. select SUNXI_CCU_GATE
  27. config SUNXI_CCU_NM
  28. bool
  29. - select RATIONAL
  30. select SUNXI_CCU_FRAC
  31. select SUNXI_CCU_GATE
  32. --- a/drivers/clk/sunxi-ng/ccu_nkm.c
  33. +++ b/drivers/clk/sunxi-ng/ccu_nkm.c
  34. @@ -9,7 +9,6 @@
  35. */
  36. #include <linux/clk-provider.h>
  37. -#include <linux/rational.h>
  38. #include "ccu_gate.h"
  39. #include "ccu_nkm.h"
  40. @@ -28,21 +27,21 @@ static void ccu_nkm_find_best(unsigned l
  41. unsigned long _n, _k, _m;
  42. for (_k = 1; _k <= nkm->max_k; _k++) {
  43. - unsigned long tmp_rate;
  44. -
  45. - rational_best_approximation(rate / _k, parent,
  46. - nkm->max_n, nkm->max_m, &_n, &_m);
  47. -
  48. - tmp_rate = parent * _n * _k / _m;
  49. -
  50. - if (tmp_rate > rate)
  51. - continue;
  52. -
  53. - if ((rate - tmp_rate) < (rate - best_rate)) {
  54. - best_rate = tmp_rate;
  55. - best_n = _n;
  56. - best_k = _k;
  57. - best_m = _m;
  58. + for (_n = 1; _n <= nkm->max_n; _n++) {
  59. + for (_m = 1; _n <= nkm->max_m; _m++) {
  60. + unsigned long tmp_rate;
  61. +
  62. + tmp_rate = parent * _n * _k / _m;
  63. +
  64. + if (tmp_rate > rate)
  65. + continue;
  66. + if ((rate - tmp_rate) < (rate - best_rate)) {
  67. + best_rate = tmp_rate;
  68. + best_n = _n;
  69. + best_k = _k;
  70. + best_m = _m;
  71. + }
  72. + }
  73. }
  74. }
  75. --- a/drivers/clk/sunxi-ng/ccu_nkmp.c
  76. +++ b/drivers/clk/sunxi-ng/ccu_nkmp.c
  77. @@ -9,7 +9,6 @@
  78. */
  79. #include <linux/clk-provider.h>
  80. -#include <linux/rational.h>
  81. #include "ccu_gate.h"
  82. #include "ccu_nkmp.h"
  83. @@ -29,24 +28,24 @@ static void ccu_nkmp_find_best(unsigned
  84. unsigned long _n, _k, _m, _p;
  85. for (_k = 1; _k <= nkmp->max_k; _k++) {
  86. - for (_p = 1; _p <= nkmp->max_p; _p <<= 1) {
  87. - unsigned long tmp_rate;
  88. -
  89. - rational_best_approximation(rate / _k, parent / _p,
  90. - nkmp->max_n, nkmp->max_m,
  91. - &_n, &_m);
  92. -
  93. - tmp_rate = parent * _n * _k / (_m * _p);
  94. -
  95. - if (tmp_rate > rate)
  96. - continue;
  97. -
  98. - if ((rate - tmp_rate) < (rate - best_rate)) {
  99. - best_rate = tmp_rate;
  100. - best_n = _n;
  101. - best_k = _k;
  102. - best_m = _m;
  103. - best_p = _p;
  104. + for (_n = 1; _n <= nkmp->max_n; _n++) {
  105. + for (_m = 1; _n <= nkmp->max_m; _m++) {
  106. + for (_p = 1; _p <= nkmp->max_p; _p <<= 1) {
  107. + unsigned long tmp_rate;
  108. +
  109. + tmp_rate = parent * _n * _k / (_m * _p);
  110. +
  111. + if (tmp_rate > rate)
  112. + continue;
  113. +
  114. + if ((rate - tmp_rate) < (rate - best_rate)) {
  115. + best_rate = tmp_rate;
  116. + best_n = _n;
  117. + best_k = _k;
  118. + best_m = _m;
  119. + best_p = _p;
  120. + }
  121. + }
  122. }
  123. }
  124. }
  125. --- a/drivers/clk/sunxi-ng/ccu_nm.c
  126. +++ b/drivers/clk/sunxi-ng/ccu_nm.c
  127. @@ -9,12 +9,42 @@
  128. */
  129. #include <linux/clk-provider.h>
  130. -#include <linux/rational.h>
  131. #include "ccu_frac.h"
  132. #include "ccu_gate.h"
  133. #include "ccu_nm.h"
  134. +struct _ccu_nm {
  135. + unsigned long n, max_n;
  136. + unsigned long m, max_m;
  137. +};
  138. +
  139. +static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
  140. + struct _ccu_nm *nm)
  141. +{
  142. + unsigned long best_rate = 0;
  143. + unsigned long best_n = 0, best_m = 0;
  144. + unsigned long _n, _m;
  145. +
  146. + for (_n = 1; _n <= nm->max_n; _n++) {
  147. + for (_m = 1; _n <= nm->max_m; _m++) {
  148. + unsigned long tmp_rate = parent * _n / _m;
  149. +
  150. + if (tmp_rate > rate)
  151. + continue;
  152. +
  153. + if ((rate - tmp_rate) < (rate - best_rate)) {
  154. + best_rate = tmp_rate;
  155. + best_n = _n;
  156. + best_m = _m;
  157. + }
  158. + }
  159. + }
  160. +
  161. + nm->n = best_n;
  162. + nm->m = best_m;
  163. +}
  164. +
  165. static void ccu_nm_disable(struct clk_hw *hw)
  166. {
  167. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  168. @@ -61,24 +91,22 @@ static long ccu_nm_round_rate(struct clk
  169. unsigned long *parent_rate)
  170. {
  171. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  172. - unsigned long max_n, max_m;
  173. - unsigned long n, m;
  174. + struct _ccu_nm _nm;
  175. - max_n = 1 << nm->n.width;
  176. - max_m = nm->m.max ?: 1 << nm->m.width;
  177. + _nm.max_n = 1 << nm->n.width;
  178. + _nm.max_m = nm->m.max ?: 1 << nm->m.width;
  179. - rational_best_approximation(rate, *parent_rate, max_n, max_m, &n, &m);
  180. + ccu_nm_find_best(*parent_rate, rate, &_nm);
  181. - return *parent_rate * n / m;
  182. + return *parent_rate * _nm.n / _nm.m;
  183. }
  184. static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
  185. unsigned long parent_rate)
  186. {
  187. struct ccu_nm *nm = hw_to_ccu_nm(hw);
  188. + struct _ccu_nm _nm;
  189. unsigned long flags;
  190. - unsigned long max_n, max_m;
  191. - unsigned long n, m;
  192. u32 reg;
  193. if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
  194. @@ -86,10 +114,10 @@ static int ccu_nm_set_rate(struct clk_hw
  195. else
  196. ccu_frac_helper_disable(&nm->common, &nm->frac);
  197. - max_n = 1 << nm->n.width;
  198. - max_m = nm->m.max ?: 1 << nm->m.width;
  199. + _nm.max_n = 1 << nm->n.width;
  200. + _nm.max_m = nm->m.max ?: 1 << nm->m.width;
  201. - rational_best_approximation(rate, parent_rate, max_n, max_m, &n, &m);
  202. + ccu_nm_find_best(parent_rate, rate, &_nm);
  203. spin_lock_irqsave(nm->common.lock, flags);
  204. @@ -97,7 +125,7 @@ static int ccu_nm_set_rate(struct clk_hw
  205. reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
  206. reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
  207. - writel(reg | ((m - 1) << nm->m.shift) | ((n - 1) << nm->n.shift),
  208. + writel(reg | ((_nm.m - 1) << nm->m.shift) | ((_nm.n - 1) << nm->n.shift),
  209. nm->common.base + nm->common.reg);
  210. spin_unlock_irqrestore(nm->common.lock, flags);