ccu_nk.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. k = reg >> nk->k.shift;
  63. k &= (1 << nk->k.width) - 1;
  64. rate = parent_rate * (n + 1) * (k + 1);
  65. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  66. rate /= nk->fixed_post_div;
  67. return rate;
  68. }
  69. static long ccu_nk_round_rate(struct clk_hw *hw, unsigned long rate,
  70. unsigned long *parent_rate)
  71. {
  72. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  73. struct _ccu_nk _nk;
  74. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  75. rate *= nk->fixed_post_div;
  76. _nk.min_n = nk->n.min;
  77. _nk.max_n = 1 << nk->n.width;
  78. _nk.min_k = nk->k.min;
  79. _nk.max_k = 1 << nk->k.width;
  80. ccu_nk_find_best(*parent_rate, rate, &_nk);
  81. rate = *parent_rate * _nk.n * _nk.k;
  82. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  83. rate = rate / nk->fixed_post_div;
  84. return rate;
  85. }
  86. static int ccu_nk_set_rate(struct clk_hw *hw, unsigned long rate,
  87. unsigned long parent_rate)
  88. {
  89. struct ccu_nk *nk = hw_to_ccu_nk(hw);
  90. unsigned long flags;
  91. struct _ccu_nk _nk;
  92. u32 reg;
  93. if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
  94. rate = rate * nk->fixed_post_div;
  95. _nk.min_n = nk->n.min;
  96. _nk.max_n = 1 << nk->n.width;
  97. _nk.min_k = nk->k.min;
  98. _nk.max_k = 1 << nk->k.width;
  99. ccu_nk_find_best(parent_rate, rate, &_nk);
  100. spin_lock_irqsave(nk->common.lock, flags);
  101. reg = readl(nk->common.base + nk->common.reg);
  102. reg &= ~GENMASK(nk->n.width + nk->n.shift - 1, nk->n.shift);
  103. reg &= ~GENMASK(nk->k.width + nk->k.shift - 1, nk->k.shift);
  104. writel(reg | ((_nk.k - 1) << nk->k.shift) | ((_nk.n - 1) << nk->n.shift),
  105. nk->common.base + nk->common.reg);
  106. spin_unlock_irqrestore(nk->common.lock, flags);
  107. ccu_helper_wait_for_lock(&nk->common, nk->lock);
  108. return 0;
  109. }
  110. const struct clk_ops ccu_nk_ops = {
  111. .disable = ccu_nk_disable,
  112. .enable = ccu_nk_enable,
  113. .is_enabled = ccu_nk_is_enabled,
  114. .recalc_rate = ccu_nk_recalc_rate,
  115. .round_rate = ccu_nk_round_rate,
  116. .set_rate = ccu_nk_set_rate,
  117. };