ccu_nkm.c 4.4 KB

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