ccu_mux.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/delay.h>
  13. #include "ccu_gate.h"
  14. #include "ccu_mux.h"
  15. void ccu_mux_helper_adjust_parent_for_prediv(struct ccu_common *common,
  16. struct ccu_mux_internal *cm,
  17. int parent_index,
  18. unsigned long *parent_rate)
  19. {
  20. u16 prediv = 1;
  21. u32 reg;
  22. int i;
  23. if (!((common->features & CCU_FEATURE_FIXED_PREDIV) ||
  24. (common->features & CCU_FEATURE_VARIABLE_PREDIV)))
  25. return;
  26. reg = readl(common->base + common->reg);
  27. if (parent_index < 0) {
  28. parent_index = reg >> cm->shift;
  29. parent_index &= (1 << cm->width) - 1;
  30. }
  31. if (common->features & CCU_FEATURE_FIXED_PREDIV)
  32. for (i = 0; i < cm->n_predivs; i++)
  33. if (parent_index == cm->fixed_predivs[i].index)
  34. prediv = cm->fixed_predivs[i].div;
  35. if (common->features & CCU_FEATURE_VARIABLE_PREDIV)
  36. if (parent_index == cm->variable_prediv.index) {
  37. u8 div;
  38. div = reg >> cm->variable_prediv.shift;
  39. div &= (1 << cm->variable_prediv.width) - 1;
  40. prediv = div + 1;
  41. }
  42. *parent_rate = *parent_rate / prediv;
  43. }
  44. int ccu_mux_helper_determine_rate(struct ccu_common *common,
  45. struct ccu_mux_internal *cm,
  46. struct clk_rate_request *req,
  47. unsigned long (*round)(struct ccu_mux_internal *,
  48. unsigned long,
  49. unsigned long,
  50. void *),
  51. void *data)
  52. {
  53. unsigned long best_parent_rate = 0, best_rate = 0;
  54. struct clk_hw *best_parent, *hw = &common->hw;
  55. unsigned int i;
  56. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  57. unsigned long tmp_rate, parent_rate;
  58. struct clk_hw *parent;
  59. parent = clk_hw_get_parent_by_index(hw, i);
  60. if (!parent)
  61. continue;
  62. parent_rate = clk_hw_get_rate(parent);
  63. ccu_mux_helper_adjust_parent_for_prediv(common, cm, i,
  64. &parent_rate);
  65. tmp_rate = round(cm, clk_hw_get_rate(parent), req->rate, data);
  66. if (tmp_rate == req->rate) {
  67. best_parent = parent;
  68. best_parent_rate = parent_rate;
  69. best_rate = tmp_rate;
  70. goto out;
  71. }
  72. if ((req->rate - tmp_rate) < (req->rate - best_rate)) {
  73. best_rate = tmp_rate;
  74. best_parent_rate = parent_rate;
  75. best_parent = parent;
  76. }
  77. }
  78. if (best_rate == 0)
  79. return -EINVAL;
  80. out:
  81. req->best_parent_hw = best_parent;
  82. req->best_parent_rate = best_parent_rate;
  83. req->rate = best_rate;
  84. return 0;
  85. }
  86. u8 ccu_mux_helper_get_parent(struct ccu_common *common,
  87. struct ccu_mux_internal *cm)
  88. {
  89. u32 reg;
  90. u8 parent;
  91. reg = readl(common->base + common->reg);
  92. parent = reg >> cm->shift;
  93. parent &= (1 << cm->width) - 1;
  94. if (cm->table) {
  95. int num_parents = clk_hw_get_num_parents(&common->hw);
  96. int i;
  97. for (i = 0; i < num_parents; i++)
  98. if (cm->table[i] == parent)
  99. return i;
  100. }
  101. return parent;
  102. }
  103. int ccu_mux_helper_set_parent(struct ccu_common *common,
  104. struct ccu_mux_internal *cm,
  105. u8 index)
  106. {
  107. unsigned long flags;
  108. u32 reg;
  109. if (cm->table)
  110. index = cm->table[index];
  111. spin_lock_irqsave(common->lock, flags);
  112. reg = readl(common->base + common->reg);
  113. reg &= ~GENMASK(cm->width + cm->shift - 1, cm->shift);
  114. writel(reg | (index << cm->shift), common->base + common->reg);
  115. spin_unlock_irqrestore(common->lock, flags);
  116. return 0;
  117. }
  118. static void ccu_mux_disable(struct clk_hw *hw)
  119. {
  120. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  121. return ccu_gate_helper_disable(&cm->common, cm->enable);
  122. }
  123. static int ccu_mux_enable(struct clk_hw *hw)
  124. {
  125. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  126. return ccu_gate_helper_enable(&cm->common, cm->enable);
  127. }
  128. static int ccu_mux_is_enabled(struct clk_hw *hw)
  129. {
  130. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  131. return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
  132. }
  133. static u8 ccu_mux_get_parent(struct clk_hw *hw)
  134. {
  135. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  136. return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
  137. }
  138. static int ccu_mux_set_parent(struct clk_hw *hw, u8 index)
  139. {
  140. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  141. return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
  142. }
  143. static unsigned long ccu_mux_recalc_rate(struct clk_hw *hw,
  144. unsigned long parent_rate)
  145. {
  146. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  147. ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
  148. &parent_rate);
  149. return parent_rate;
  150. }
  151. const struct clk_ops ccu_mux_ops = {
  152. .disable = ccu_mux_disable,
  153. .enable = ccu_mux_enable,
  154. .is_enabled = ccu_mux_is_enabled,
  155. .get_parent = ccu_mux_get_parent,
  156. .set_parent = ccu_mux_set_parent,
  157. .determine_rate = __clk_mux_determine_rate,
  158. .recalc_rate = ccu_mux_recalc_rate,
  159. };
  160. /*
  161. * This clock notifier is called when the frequency of the of the parent
  162. * PLL clock is to be changed. The idea is to switch the parent to a
  163. * stable clock, such as the main oscillator, while the PLL frequency
  164. * stabilizes.
  165. */
  166. static int ccu_mux_notifier_cb(struct notifier_block *nb,
  167. unsigned long event, void *data)
  168. {
  169. struct ccu_mux_nb *mux = to_ccu_mux_nb(nb);
  170. int ret = 0;
  171. if (event == PRE_RATE_CHANGE) {
  172. mux->original_index = ccu_mux_helper_get_parent(mux->common,
  173. mux->cm);
  174. ret = ccu_mux_helper_set_parent(mux->common, mux->cm,
  175. mux->bypass_index);
  176. } else if (event == POST_RATE_CHANGE) {
  177. ret = ccu_mux_helper_set_parent(mux->common, mux->cm,
  178. mux->original_index);
  179. }
  180. udelay(mux->delay_us);
  181. return notifier_from_errno(ret);
  182. }
  183. int ccu_mux_notifier_register(struct clk *clk, struct ccu_mux_nb *mux_nb)
  184. {
  185. mux_nb->clk_nb.notifier_call = ccu_mux_notifier_cb;
  186. return clk_notifier_register(clk, &mux_nb->clk_nb);
  187. }