ccu_nk.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_nk.h"
  13. struct _ccu_nk {
  14. unsigned long n, min_n, max_n;
  15. unsigned long k, min_k, max_k;
  16. };
  17. static void ccu_nk_find_best(unsigned long parent, unsigned long rate,
  18. struct _ccu_nk *nk)
  19. {
  20. unsigned long best_rate = 0;
  21. unsigned int best_k = 0, best_n = 0;
  22. unsigned int _k, _n;
  23. for (_k = nk->min_k; _k <= nk->max_k; _k++) {
  24. for (_n = nk->min_n; _n <= nk->max_n; _n++) {
  25. unsigned long tmp_rate = parent * _n * _k;
  26. if (tmp_rate > rate)
  27. continue;
  28. if ((rate - tmp_rate) < (rate - best_rate)) {
  29. best_rate = tmp_rate;
  30. best_k = _k;
  31. best_n = _n;
  32. }
  33. }
  34. }
  35. nk->k = best_k;
  36. nk->n = best_n;
  37. }
  38. static void ccu_nk_disable(struct clk_hw *hw)
  39. {
  40. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  41. return ccu_gate_helper_disable(&nk->common, nk->enable);
  42. }
  43. static int ccu_nk_enable(struct clk_hw *hw)
  44. {
  45. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  46. return ccu_gate_helper_enable(&nk->common, nk->enable);
  47. }
  48. static int ccu_nk_is_enabled(struct clk_hw *hw)
  49. {
  50. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  51. return ccu_gate_helper_is_enabled(&nk->common, nk->enable);
  52. }
  53. static unsigned long ccu_nk_recalc_rate(struct clk_hw *hw,
  54. unsigned long parent_rate)
  55. {
  56. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  57. unsigned long rate, n, k;
  58. u32 reg;
  59. reg = readl(nk->common.base + nk->common.reg);
  60. n = reg >> nk->n.shift;
  61. n &= (1 << nk->n.width) - 1;
  62. n += nk->n.offset;
  63. if (!n)
  64. n++;
  65. k = reg >> nk->k.shift;
  66. k &= (1 << nk->k.width) - 1;
  67. k += nk->k.offset;
  68. if (!k)
  69. k++;
  70. rate = parent_rate * n * k;
  71. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  72. rate /= nk->fixed_post_div;
  73. return rate;
  74. }
  75. static long ccu_nk_round_rate(struct clk_hw *hw, unsigned long rate,
  76. unsigned long *parent_rate)
  77. {
  78. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  79. struct _ccu_nk _nk;
  80. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  81. rate *= nk->fixed_post_div;
  82. _nk.min_n = nk->n.min ?: 1;
  83. _nk.max_n = nk->n.max ?: 1 << nk->n.width;
  84. _nk.min_k = nk->k.min ?: 1;
  85. _nk.max_k = nk->k.max ?: 1 << nk->k.width;
  86. ccu_nk_find_best(*parent_rate, rate, &_nk);
  87. rate = *parent_rate * _nk.n * _nk.k;
  88. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  89. rate = rate / nk->fixed_post_div;
  90. return rate;
  91. }
  92. static int ccu_nk_set_rate(struct clk_hw *hw, unsigned long rate,
  93. unsigned long parent_rate)
  94. {
  95. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  96. unsigned long flags;
  97. struct _ccu_nk _nk;
  98. u32 reg;
  99. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  100. rate = rate * nk->fixed_post_div;
  101. _nk.min_n = nk->n.min ?: 1;
  102. _nk.max_n = nk->n.max ?: 1 << nk->n.width;
  103. _nk.min_k = nk->k.min ?: 1;
  104. _nk.max_k = nk->k.max ?: 1 << nk->k.width;
  105. ccu_nk_find_best(parent_rate, rate, &_nk);
  106. spin_lock_irqsave(nk->common.lock, flags);
  107. reg = readl(nk->common.base + nk->common.reg);
  108. reg &= ~GENMASK(nk->n.width + nk->n.shift - 1, nk->n.shift);
  109. reg &= ~GENMASK(nk->k.width + nk->k.shift - 1, nk->k.shift);
  110. reg |= (_nk.k - nk->k.offset) << nk->k.shift;
  111. reg |= (_nk.n - nk->n.offset) << nk->n.shift;
  112. writel(reg, nk->common.base + nk->common.reg);
  113. spin_unlock_irqrestore(nk->common.lock, flags);
  114. ccu_helper_wait_for_lock(&nk->common, nk->lock);
  115. return 0;
  116. }
  117. const struct clk_ops ccu_nk_ops = {
  118. .disable = ccu_nk_disable,
  119. .enable = ccu_nk_enable,
  120. .is_enabled = ccu_nk_is_enabled,
  121. .recalc_rate = ccu_nk_recalc_rate,
  122. .round_rate = ccu_nk_round_rate,
  123. .set_rate = ccu_nk_set_rate,
  124. };