ccu_mp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_mp.h"
  13. static void ccu_mp_find_best(unsigned long parent, unsigned long rate,
  14. unsigned int max_m, unsigned int max_p,
  15. unsigned int *m, unsigned int *p)
  16. {
  17. unsigned long best_rate = 0;
  18. unsigned int best_m = 0, best_p = 0;
  19. unsigned int _m, _p;
  20. for (_p = 1; _p <= max_p; _p <<= 1) {
  21. for (_m = 1; _m <= max_m; _m++) {
  22. unsigned long tmp_rate = parent / _p / _m;
  23. if (tmp_rate > rate)
  24. continue;
  25. if ((rate - tmp_rate) < (rate - best_rate)) {
  26. best_rate = tmp_rate;
  27. best_m = _m;
  28. best_p = _p;
  29. }
  30. }
  31. }
  32. *m = best_m;
  33. *p = best_p;
  34. }
  35. static unsigned long ccu_mp_round_rate(struct ccu_mux_internal *mux,
  36. unsigned long parent_rate,
  37. unsigned long rate,
  38. void *data)
  39. {
  40. struct ccu_mp *cmp = data;
  41. unsigned int max_m, max_p;
  42. unsigned int m, p;
  43. max_m = cmp->m.max ?: 1 << cmp->m.width;
  44. max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
  45. ccu_mp_find_best(parent_rate, rate, max_m, max_p, &m, &p);
  46. return parent_rate / p / m;
  47. }
  48. static void ccu_mp_disable(struct clk_hw *hw)
  49. {
  50. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  51. return ccu_gate_helper_disable(&cmp->common, cmp->enable);
  52. }
  53. static int ccu_mp_enable(struct clk_hw *hw)
  54. {
  55. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  56. return ccu_gate_helper_enable(&cmp->common, cmp->enable);
  57. }
  58. static int ccu_mp_is_enabled(struct clk_hw *hw)
  59. {
  60. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  61. return ccu_gate_helper_is_enabled(&cmp->common, cmp->enable);
  62. }
  63. static unsigned long ccu_mp_recalc_rate(struct clk_hw *hw,
  64. unsigned long parent_rate)
  65. {
  66. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  67. unsigned int m, p;
  68. u32 reg;
  69. /* Adjust parent_rate according to pre-dividers */
  70. ccu_mux_helper_adjust_parent_for_prediv(&cmp->common, &cmp->mux,
  71. -1, &parent_rate);
  72. reg = readl(cmp->common.base + cmp->common.reg);
  73. m = reg >> cmp->m.shift;
  74. m &= (1 << cmp->m.width) - 1;
  75. p = reg >> cmp->p.shift;
  76. p &= (1 << cmp->p.width) - 1;
  77. return (parent_rate >> p) / (m + 1);
  78. }
  79. static int ccu_mp_determine_rate(struct clk_hw *hw,
  80. struct clk_rate_request *req)
  81. {
  82. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  83. return ccu_mux_helper_determine_rate(&cmp->common, &cmp->mux,
  84. req, ccu_mp_round_rate, cmp);
  85. }
  86. static int ccu_mp_set_rate(struct clk_hw *hw, unsigned long rate,
  87. unsigned long parent_rate)
  88. {
  89. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  90. unsigned long flags;
  91. unsigned int max_m, max_p;
  92. unsigned int m, p;
  93. u32 reg;
  94. /* Adjust parent_rate according to pre-dividers */
  95. ccu_mux_helper_adjust_parent_for_prediv(&cmp->common, &cmp->mux,
  96. -1, &parent_rate);
  97. max_m = cmp->m.max ?: 1 << cmp->m.width;
  98. max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
  99. ccu_mp_find_best(parent_rate, rate, max_m, max_p, &m, &p);
  100. spin_lock_irqsave(cmp->common.lock, flags);
  101. reg = readl(cmp->common.base + cmp->common.reg);
  102. reg &= ~GENMASK(cmp->m.width + cmp->m.shift - 1, cmp->m.shift);
  103. reg &= ~GENMASK(cmp->p.width + cmp->p.shift - 1, cmp->p.shift);
  104. writel(reg | (ilog2(p) << cmp->p.shift) | ((m - 1) << cmp->m.shift),
  105. cmp->common.base + cmp->common.reg);
  106. spin_unlock_irqrestore(cmp->common.lock, flags);
  107. return 0;
  108. }
  109. static u8 ccu_mp_get_parent(struct clk_hw *hw)
  110. {
  111. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  112. return ccu_mux_helper_get_parent(&cmp->common, &cmp->mux);
  113. }
  114. static int ccu_mp_set_parent(struct clk_hw *hw, u8 index)
  115. {
  116. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  117. return ccu_mux_helper_set_parent(&cmp->common, &cmp->mux, index);
  118. }
  119. const struct clk_ops ccu_mp_ops = {
  120. .disable = ccu_mp_disable,
  121. .enable = ccu_mp_enable,
  122. .is_enabled = ccu_mp_is_enabled,
  123. .get_parent = ccu_mp_get_parent,
  124. .set_parent = ccu_mp_set_parent,
  125. .determine_rate = ccu_mp_determine_rate,
  126. .recalc_rate = ccu_mp_recalc_rate,
  127. .set_rate = ccu_mp_set_rate,
  128. };