ccu_nkm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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;
  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. return parent_rate * n * k / m;
  82. }
  83. static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
  84. unsigned long parent_rate,
  85. unsigned long rate,
  86. void *data)
  87. {
  88. struct ccu_nkm *nkm = data;
  89. struct _ccu_nkm _nkm;
  90. _nkm.min_n = nkm->n.min ?: 1;
  91. _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
  92. _nkm.min_k = nkm->k.min ?: 1;
  93. _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
  94. _nkm.min_m = 1;
  95. _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
  96. ccu_nkm_find_best(parent_rate, rate, &_nkm);
  97. return parent_rate * _nkm.n * _nkm.k / _nkm.m;
  98. }
  99. static int ccu_nkm_determine_rate(struct clk_hw *hw,
  100. struct clk_rate_request *req)
  101. {
  102. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  103. return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
  104. req, ccu_nkm_round_rate, nkm);
  105. }
  106. static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
  107. unsigned long parent_rate)
  108. {
  109. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  110. struct _ccu_nkm _nkm;
  111. unsigned long flags;
  112. u32 reg;
  113. _nkm.min_n = nkm->n.min ?: 1;
  114. _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width;
  115. _nkm.min_k = nkm->k.min ?: 1;
  116. _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width;
  117. _nkm.min_m = 1;
  118. _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
  119. ccu_nkm_find_best(parent_rate, rate, &_nkm);
  120. spin_lock_irqsave(nkm->common.lock, flags);
  121. reg = readl(nkm->common.base + nkm->common.reg);
  122. reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
  123. reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
  124. reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
  125. reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift;
  126. reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift;
  127. reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift;
  128. writel(reg, nkm->common.base + nkm->common.reg);
  129. spin_unlock_irqrestore(nkm->common.lock, flags);
  130. ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
  131. return 0;
  132. }
  133. static u8 ccu_nkm_get_parent(struct clk_hw *hw)
  134. {
  135. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  136. return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
  137. }
  138. static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
  139. {
  140. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  141. return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
  142. }
  143. const struct clk_ops ccu_nkm_ops = {
  144. .disable = ccu_nkm_disable,
  145. .enable = ccu_nkm_enable,
  146. .is_enabled = ccu_nkm_is_enabled,
  147. .get_parent = ccu_nkm_get_parent,
  148. .set_parent = ccu_nkm_set_parent,
  149. .determine_rate = ccu_nkm_determine_rate,
  150. .recalc_rate = ccu_nkm_recalc_rate,
  151. .set_rate = ccu_nkm_set_rate,
  152. };