ccu_nkm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Maxime Ripard
  4. * Maxime Ripard <[email protected]>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include "ccu_gate.h"
  9. #include "ccu_nkm.h"
  10. struct _ccu_nkm {
  11. unsigned long n, min_n, max_n;
  12. unsigned long k, min_k, max_k;
  13. unsigned long m, min_m, max_m;
  14. };
  15. static void ccu_nkm_find_best(unsigned long parent, unsigned long rate,
  16. struct _ccu_nkm *nkm)
  17. {
  18. unsigned long best_rate = 0;
  19. unsigned long best_n = 0, best_k = 0, best_m = 0;
  20. unsigned long _n, _k, _m;
  21. for (_k = nkm->min_k; _k <= nkm->max_k; _k++) {
  22. for (_n = nkm->min_n; _n <= nkm->max_n; _n++) {
  23. for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
  24. unsigned long tmp_rate;
  25. tmp_rate = parent * _n * _k / _m;
  26. if (tmp_rate > rate)
  27. continue;
  28. if ((rate - tmp_rate) < (rate - best_rate)) {
  29. best_rate = tmp_rate;
  30. best_n = _n;
  31. best_k = _k;
  32. best_m = _m;
  33. }
  34. }
  35. }
  36. }
  37. nkm->n = best_n;
  38. nkm->k = best_k;
  39. nkm->m = best_m;
  40. }
  41. static void ccu_nkm_disable(struct clk_hw *hw)
  42. {
  43. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  44. return ccu_gate_helper_disable(&nkm->common, nkm->enable);
  45. }
  46. static int ccu_nkm_enable(struct clk_hw *hw)
  47. {
  48. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  49. return ccu_gate_helper_enable(&nkm->common, nkm->enable);
  50. }
  51. static int ccu_nkm_is_enabled(struct clk_hw *hw)
  52. {
  53. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  54. return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable);
  55. }
  56. static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw,
  57. unsigned long parent_rate)
  58. {
  59. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  60. unsigned long n, m, k, rate;
  61. u32 reg;
  62. reg = readl(nkm->common.base + nkm->common.reg);
  63. n = reg >> nkm->n.shift;
  64. n &= (1 << nkm->n.width) - 1;
  65. n += nkm->n.offset;
  66. if (!n)
  67. n++;
  68. k = reg >> nkm->k.shift;
  69. k &= (1 << nkm->k.width) - 1;
  70. k += nkm->k.offset;
  71. if (!k)
  72. k++;
  73. m = reg >> nkm->m.shift;
  74. m &= (1 << nkm->m.width) - 1;
  75. m += nkm->m.offset;
  76. if (!m)
  77. m++;
  78. rate = parent_rate * n * k / m;
  79. if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
  80. rate /= nkm->fixed_post_div;
  81. return rate;
  82. }
  83. static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
  84. struct clk_hw *hw,
  85. unsigned long *parent_rate,
  86. unsigned long rate,
  87. void *data)
  88. {
  89. struct ccu_nkm *nkm = data;
  90. struct _ccu_nkm _nkm;
  91. _nkm.min_n = nkm->n.min ?: 1;
  92. _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
  93. _nkm.min_k = nkm->k.min ?: 1;
  94. _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
  95. _nkm.min_m = 1;
  96. _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
  97. if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
  98. rate *= nkm->fixed_post_div;
  99. ccu_nkm_find_best(*parent_rate, rate, &_nkm);
  100. rate = *parent_rate * _nkm.n * _nkm.k / _nkm.m;
  101. if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
  102. rate /= nkm->fixed_post_div;
  103. return rate;
  104. }
  105. static int ccu_nkm_determine_rate(struct clk_hw *hw,
  106. struct clk_rate_request *req)
  107. {
  108. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  109. return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
  110. req, ccu_nkm_round_rate, nkm);
  111. }
  112. static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  116. struct _ccu_nkm _nkm;
  117. unsigned long flags;
  118. u32 reg;
  119. if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV)
  120. rate *= nkm->fixed_post_div;
  121. _nkm.min_n = nkm->n.min ?: 1;
  122. _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
  123. _nkm.min_k = nkm->k.min ?: 1;
  124. _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
  125. _nkm.min_m = 1;
  126. _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
  127. ccu_nkm_find_best(parent_rate, rate, &_nkm);
  128. spin_lock_irqsave(nkm->common.lock, flags);
  129. reg = readl(nkm->common.base + nkm->common.reg);
  130. reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
  131. reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
  132. reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
  133. reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift;
  134. reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift;
  135. reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift;
  136. writel(reg, nkm->common.base + nkm->common.reg);
  137. spin_unlock_irqrestore(nkm->common.lock, flags);
  138. ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
  139. return 0;
  140. }
  141. static u8 ccu_nkm_get_parent(struct clk_hw *hw)
  142. {
  143. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  144. return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
  145. }
  146. static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
  147. {
  148. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  149. return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
  150. }
  151. const struct clk_ops ccu_nkm_ops = {
  152. .disable = ccu_nkm_disable,
  153. .enable = ccu_nkm_enable,
  154. .is_enabled = ccu_nkm_is_enabled,
  155. .get_parent = ccu_nkm_get_parent,
  156. .set_parent = ccu_nkm_set_parent,
  157. .determine_rate = ccu_nkm_determine_rate,
  158. .recalc_rate = ccu_nkm_recalc_rate,
  159. .set_rate = ccu_nkm_set_rate,
  160. };
  161. EXPORT_SYMBOL_NS_GPL(ccu_nkm_ops, SUNXI_CCU);