ccu_nkm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <linux/rational.h>
  12. #include "ccu_gate.h"
  13. #include "ccu_nkm.h"
  14. struct _ccu_nkm {
  15. unsigned long n, max_n;
  16. unsigned long k, max_k;
  17. unsigned long m, max_m;
  18. };
  19. static void ccu_nkm_find_best(unsigned long parent, unsigned long rate,
  20. struct _ccu_nkm *nkm)
  21. {
  22. unsigned long best_rate = 0;
  23. unsigned long best_n = 0, best_k = 0, best_m = 0;
  24. unsigned long _n, _k, _m;
  25. for (_k = 1; _k <= nkm->max_k; _k++) {
  26. unsigned long tmp_rate;
  27. rational_best_approximation(rate / _k, parent,
  28. nkm->max_n, nkm->max_m, &_n, &_m);
  29. tmp_rate = parent * _n * _k / _m;
  30. if (tmp_rate > rate)
  31. continue;
  32. if ((rate - tmp_rate) < (rate - best_rate)) {
  33. best_rate = tmp_rate;
  34. best_n = _n;
  35. best_k = _k;
  36. best_m = _m;
  37. }
  38. }
  39. nkm->n = best_n;
  40. nkm->k = best_k;
  41. nkm->m = best_m;
  42. }
  43. static void ccu_nkm_disable(struct clk_hw *hw)
  44. {
  45. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  46. return ccu_gate_helper_disable(&nkm->common, nkm->enable);
  47. }
  48. static int ccu_nkm_enable(struct clk_hw *hw)
  49. {
  50. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  51. return ccu_gate_helper_enable(&nkm->common, nkm->enable);
  52. }
  53. static int ccu_nkm_is_enabled(struct clk_hw *hw)
  54. {
  55. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  56. return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable);
  57. }
  58. static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw,
  59. unsigned long parent_rate)
  60. {
  61. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  62. unsigned long n, m, k;
  63. u32 reg;
  64. reg = readl(nkm->common.base + nkm->common.reg);
  65. n = reg >> nkm->n.shift;
  66. n &= (1 << nkm->n.width) - 1;
  67. k = reg >> nkm->k.shift;
  68. k &= (1 << nkm->k.width) - 1;
  69. m = reg >> nkm->m.shift;
  70. m &= (1 << nkm->m.width) - 1;
  71. return parent_rate * (n + 1) * (k + 1) / (m + 1);
  72. }
  73. static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux,
  74. unsigned long parent_rate,
  75. unsigned long rate,
  76. void *data)
  77. {
  78. struct ccu_nkm *nkm = data;
  79. struct _ccu_nkm _nkm;
  80. _nkm.max_n = 1 << nkm->n.width;
  81. _nkm.max_k = 1 << nkm->k.width;
  82. _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
  83. ccu_nkm_find_best(parent_rate, rate, &_nkm);
  84. return parent_rate * _nkm.n * _nkm.k / _nkm.m;
  85. }
  86. static int ccu_nkm_determine_rate(struct clk_hw *hw,
  87. struct clk_rate_request *req)
  88. {
  89. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  90. return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux,
  91. req, ccu_nkm_round_rate, nkm);
  92. }
  93. static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate,
  94. unsigned long parent_rate)
  95. {
  96. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  97. struct _ccu_nkm _nkm;
  98. unsigned long flags;
  99. u32 reg;
  100. _nkm.max_n = 1 << nkm->n.width;
  101. _nkm.max_k = 1 << nkm->k.width;
  102. _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width;
  103. ccu_nkm_find_best(parent_rate, rate, &_nkm);
  104. spin_lock_irqsave(nkm->common.lock, flags);
  105. reg = readl(nkm->common.base + nkm->common.reg);
  106. reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift);
  107. reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift);
  108. reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift);
  109. reg |= (_nkm.n - 1) << nkm->n.shift;
  110. reg |= (_nkm.k - 1) << nkm->k.shift;
  111. reg |= (_nkm.m - 1) << nkm->m.shift;
  112. writel(reg, nkm->common.base + nkm->common.reg);
  113. spin_unlock_irqrestore(nkm->common.lock, flags);
  114. ccu_helper_wait_for_lock(&nkm->common, nkm->lock);
  115. return 0;
  116. }
  117. static u8 ccu_nkm_get_parent(struct clk_hw *hw)
  118. {
  119. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  120. return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux);
  121. }
  122. static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index)
  123. {
  124. struct ccu_nkm *nkm = hw_to_ccu_nkm(hw);
  125. return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index);
  126. }
  127. const struct clk_ops ccu_nkm_ops = {
  128. .disable = ccu_nkm_disable,
  129. .enable = ccu_nkm_enable,
  130. .is_enabled = ccu_nkm_is_enabled,
  131. .get_parent = ccu_nkm_get_parent,
  132. .set_parent = ccu_nkm_set_parent,
  133. .determine_rate = ccu_nkm_determine_rate,
  134. .recalc_rate = ccu_nkm_recalc_rate,
  135. .set_rate = ccu_nkm_set_rate,
  136. };