ccu_nkmp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_nkmp.h"
  14. struct _ccu_nkmp {
  15. unsigned long n, max_n;
  16. unsigned long k, max_k;
  17. unsigned long m, max_m;
  18. unsigned long p, max_p;
  19. };
  20. static void ccu_nkmp_find_best(unsigned long parent, unsigned long rate,
  21. struct _ccu_nkmp *nkmp)
  22. {
  23. unsigned long best_rate = 0;
  24. unsigned long best_n = 0, best_k = 0, best_m = 0, best_p = 0;
  25. unsigned long _n, _k, _m, _p;
  26. for (_k = 1; _k <= nkmp->max_k; _k++) {
  27. for (_p = 1; _p <= nkmp->max_p; _p <<= 1) {
  28. unsigned long tmp_rate;
  29. rational_best_approximation(rate / _k, parent / _p,
  30. nkmp->max_n, nkmp->max_m,
  31. &_n, &_m);
  32. tmp_rate = parent * _n * _k / (_m * _p);
  33. if (tmp_rate > rate)
  34. continue;
  35. if ((rate - tmp_rate) < (rate - best_rate)) {
  36. best_rate = tmp_rate;
  37. best_n = _n;
  38. best_k = _k;
  39. best_m = _m;
  40. best_p = _p;
  41. }
  42. }
  43. }
  44. nkmp->n = best_n;
  45. nkmp->k = best_k;
  46. nkmp->m = best_m;
  47. nkmp->p = best_p;
  48. }
  49. static void ccu_nkmp_disable(struct clk_hw *hw)
  50. {
  51. struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw);
  52. return ccu_gate_helper_disable(&nkmp->common, nkmp->enable);
  53. }
  54. static int ccu_nkmp_enable(struct clk_hw *hw)
  55. {
  56. struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw);
  57. return ccu_gate_helper_enable(&nkmp->common, nkmp->enable);
  58. }
  59. static int ccu_nkmp_is_enabled(struct clk_hw *hw)
  60. {
  61. struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw);
  62. return ccu_gate_helper_is_enabled(&nkmp->common, nkmp->enable);
  63. }
  64. static unsigned long ccu_nkmp_recalc_rate(struct clk_hw *hw,
  65. unsigned long parent_rate)
  66. {
  67. struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw);
  68. unsigned long n, m, k, p;
  69. u32 reg;
  70. reg = readl(nkmp->common.base + nkmp->common.reg);
  71. n = reg >> nkmp->n.shift;
  72. n &= (1 << nkmp->n.width) - 1;
  73. k = reg >> nkmp->k.shift;
  74. k &= (1 << nkmp->k.width) - 1;
  75. m = reg >> nkmp->m.shift;
  76. m &= (1 << nkmp->m.width) - 1;
  77. p = reg >> nkmp->p.shift;
  78. p &= (1 << nkmp->p.width) - 1;
  79. return (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
  80. }
  81. static long ccu_nkmp_round_rate(struct clk_hw *hw, unsigned long rate,
  82. unsigned long *parent_rate)
  83. {
  84. struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw);
  85. struct _ccu_nkmp _nkmp;
  86. _nkmp.max_n = 1 << nkmp->n.width;
  87. _nkmp.max_k = 1 << nkmp->k.width;
  88. _nkmp.max_m = nkmp->m.max ?: 1 << nkmp->m.width;
  89. _nkmp.max_p = nkmp->p.max ?: 1 << ((1 << nkmp->p.width) - 1);
  90. ccu_nkmp_find_best(*parent_rate, rate, &_nkmp);
  91. return *parent_rate * _nkmp.n * _nkmp.k / (_nkmp.m * _nkmp.p);
  92. }
  93. static int ccu_nkmp_set_rate(struct clk_hw *hw, unsigned long rate,
  94. unsigned long parent_rate)
  95. {
  96. struct ccu_nkmp *nkmp = hw_to_ccu_nkmp(hw);
  97. struct _ccu_nkmp _nkmp;
  98. unsigned long flags;
  99. u32 reg;
  100. _nkmp.max_n = 1 << nkmp->n.width;
  101. _nkmp.max_k = 1 << nkmp->k.width;
  102. _nkmp.max_m = nkmp->m.max ?: 1 << nkmp->m.width;
  103. _nkmp.max_p = nkmp->p.max ?: 1 << ((1 << nkmp->p.width) - 1);
  104. ccu_nkmp_find_best(parent_rate, rate, &_nkmp);
  105. spin_lock_irqsave(nkmp->common.lock, flags);
  106. reg = readl(nkmp->common.base + nkmp->common.reg);
  107. reg &= ~GENMASK(nkmp->n.width + nkmp->n.shift - 1, nkmp->n.shift);
  108. reg &= ~GENMASK(nkmp->k.width + nkmp->k.shift - 1, nkmp->k.shift);
  109. reg &= ~GENMASK(nkmp->m.width + nkmp->m.shift - 1, nkmp->m.shift);
  110. reg &= ~GENMASK(nkmp->p.width + nkmp->p.shift - 1, nkmp->p.shift);
  111. reg |= (_nkmp.n - 1) << nkmp->n.shift;
  112. reg |= (_nkmp.k - 1) << nkmp->k.shift;
  113. reg |= (_nkmp.m - 1) << nkmp->m.shift;
  114. reg |= ilog2(_nkmp.p) << nkmp->p.shift;
  115. writel(reg, nkmp->common.base + nkmp->common.reg);
  116. spin_unlock_irqrestore(nkmp->common.lock, flags);
  117. ccu_helper_wait_for_lock(&nkmp->common, nkmp->lock);
  118. return 0;
  119. }
  120. const struct clk_ops ccu_nkmp_ops = {
  121. .disable = ccu_nkmp_disable,
  122. .enable = ccu_nkmp_enable,
  123. .is_enabled = ccu_nkmp_is_enabled,
  124. .recalc_rate = ccu_nkmp_recalc_rate,
  125. .round_rate = ccu_nkmp_round_rate,
  126. .set_rate = ccu_nkmp_set_rate,
  127. };