ccu_nk.c 3.4 KB

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