ccu_nkm.c 5.0 KB

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