ccu_mux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Maxime Ripard
  4. * Maxime Ripard <[email protected]>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include "ccu_gate.h"
  11. #include "ccu_mux.h"
  12. #define CCU_MUX_KEY_VALUE 0x16aa0000
  13. static u16 ccu_mux_get_prediv(struct ccu_common *common,
  14. struct ccu_mux_internal *cm,
  15. int parent_index)
  16. {
  17. u16 prediv = 1;
  18. u32 reg;
  19. if (!((common->features & CCU_FEATURE_FIXED_PREDIV) ||
  20. (common->features & CCU_FEATURE_VARIABLE_PREDIV) ||
  21. (common->features & CCU_FEATURE_ALL_PREDIV)))
  22. return 1;
  23. if (common->features & CCU_FEATURE_ALL_PREDIV)
  24. return common->prediv;
  25. reg = readl(common->base + common->reg);
  26. if (parent_index < 0) {
  27. parent_index = reg >> cm->shift;
  28. parent_index &= (1 << cm->width) - 1;
  29. }
  30. if (common->features & CCU_FEATURE_FIXED_PREDIV) {
  31. int i;
  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. }
  36. if (common->features & CCU_FEATURE_VARIABLE_PREDIV) {
  37. int i;
  38. for (i = 0; i < cm->n_var_predivs; i++)
  39. if (parent_index == cm->var_predivs[i].index) {
  40. u8 div;
  41. div = reg >> cm->var_predivs[i].shift;
  42. div &= (1 << cm->var_predivs[i].width) - 1;
  43. prediv = div + 1;
  44. }
  45. }
  46. return prediv;
  47. }
  48. unsigned long ccu_mux_helper_apply_prediv(struct ccu_common *common,
  49. struct ccu_mux_internal *cm,
  50. int parent_index,
  51. unsigned long parent_rate)
  52. {
  53. return parent_rate / ccu_mux_get_prediv(common, cm, parent_index);
  54. }
  55. EXPORT_SYMBOL_NS_GPL(ccu_mux_helper_apply_prediv, SUNXI_CCU);
  56. static unsigned long ccu_mux_helper_unapply_prediv(struct ccu_common *common,
  57. struct ccu_mux_internal *cm,
  58. int parent_index,
  59. unsigned long parent_rate)
  60. {
  61. return parent_rate * ccu_mux_get_prediv(common, cm, parent_index);
  62. }
  63. int ccu_mux_helper_determine_rate(struct ccu_common *common,
  64. struct ccu_mux_internal *cm,
  65. struct clk_rate_request *req,
  66. unsigned long (*round)(struct ccu_mux_internal *,
  67. struct clk_hw *,
  68. unsigned long *,
  69. unsigned long,
  70. void *),
  71. void *data)
  72. {
  73. unsigned long best_parent_rate = 0, best_rate = 0;
  74. struct clk_hw *best_parent, *hw = &common->hw;
  75. unsigned int i;
  76. if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
  77. unsigned long adj_parent_rate;
  78. best_parent = clk_hw_get_parent(hw);
  79. best_parent_rate = clk_hw_get_rate(best_parent);
  80. adj_parent_rate = ccu_mux_helper_apply_prediv(common, cm, -1,
  81. best_parent_rate);
  82. best_rate = round(cm, best_parent, &adj_parent_rate,
  83. req->rate, data);
  84. /*
  85. * adj_parent_rate might have been modified by our clock.
  86. * Unapply the pre-divider if there's one, and give
  87. * the actual frequency the parent needs to run at.
  88. */
  89. best_parent_rate = ccu_mux_helper_unapply_prediv(common, cm, -1,
  90. adj_parent_rate);
  91. goto out;
  92. }
  93. for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  94. unsigned long tmp_rate, parent_rate;
  95. struct clk_hw *parent;
  96. parent = clk_hw_get_parent_by_index(hw, i);
  97. if (!parent)
  98. continue;
  99. parent_rate = ccu_mux_helper_apply_prediv(common, cm, i,
  100. clk_hw_get_rate(parent));
  101. tmp_rate = round(cm, parent, &parent_rate, req->rate, data);
  102. /*
  103. * parent_rate might have been modified by our clock.
  104. * Unapply the pre-divider if there's one, and give
  105. * the actual frequency the parent needs to run at.
  106. */
  107. parent_rate = ccu_mux_helper_unapply_prediv(common, cm, i,
  108. parent_rate);
  109. if (tmp_rate == req->rate) {
  110. best_parent = parent;
  111. best_parent_rate = parent_rate;
  112. best_rate = tmp_rate;
  113. goto out;
  114. }
  115. if ((req->rate - tmp_rate) < (req->rate - best_rate)) {
  116. best_rate = tmp_rate;
  117. best_parent_rate = parent_rate;
  118. best_parent = parent;
  119. }
  120. }
  121. if (best_rate == 0)
  122. return -EINVAL;
  123. out:
  124. req->best_parent_hw = best_parent;
  125. req->best_parent_rate = best_parent_rate;
  126. req->rate = best_rate;
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_NS_GPL(ccu_mux_helper_determine_rate, SUNXI_CCU);
  130. u8 ccu_mux_helper_get_parent(struct ccu_common *common,
  131. struct ccu_mux_internal *cm)
  132. {
  133. u32 reg;
  134. u8 parent;
  135. reg = readl(common->base + common->reg);
  136. parent = reg >> cm->shift;
  137. parent &= (1 << cm->width) - 1;
  138. if (cm->table) {
  139. int num_parents = clk_hw_get_num_parents(&common->hw);
  140. int i;
  141. for (i = 0; i < num_parents; i++)
  142. if (cm->table[i] == parent)
  143. return i;
  144. }
  145. return parent;
  146. }
  147. EXPORT_SYMBOL_NS_GPL(ccu_mux_helper_get_parent, SUNXI_CCU);
  148. int ccu_mux_helper_set_parent(struct ccu_common *common,
  149. struct ccu_mux_internal *cm,
  150. u8 index)
  151. {
  152. unsigned long flags;
  153. u32 reg;
  154. if (cm->table)
  155. index = cm->table[index];
  156. spin_lock_irqsave(common->lock, flags);
  157. reg = readl(common->base + common->reg);
  158. /* The key field always reads as zero. */
  159. if (common->features & CCU_FEATURE_KEY_FIELD)
  160. reg |= CCU_MUX_KEY_VALUE;
  161. reg &= ~GENMASK(cm->width + cm->shift - 1, cm->shift);
  162. writel(reg | (index << cm->shift), common->base + common->reg);
  163. spin_unlock_irqrestore(common->lock, flags);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL_NS_GPL(ccu_mux_helper_set_parent, SUNXI_CCU);
  167. static void ccu_mux_disable(struct clk_hw *hw)
  168. {
  169. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  170. return ccu_gate_helper_disable(&cm->common, cm->enable);
  171. }
  172. static int ccu_mux_enable(struct clk_hw *hw)
  173. {
  174. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  175. return ccu_gate_helper_enable(&cm->common, cm->enable);
  176. }
  177. static int ccu_mux_is_enabled(struct clk_hw *hw)
  178. {
  179. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  180. return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
  181. }
  182. static u8 ccu_mux_get_parent(struct clk_hw *hw)
  183. {
  184. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  185. return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
  186. }
  187. static int ccu_mux_set_parent(struct clk_hw *hw, u8 index)
  188. {
  189. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  190. return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
  191. }
  192. static unsigned long ccu_mux_recalc_rate(struct clk_hw *hw,
  193. unsigned long parent_rate)
  194. {
  195. struct ccu_mux *cm = hw_to_ccu_mux(hw);
  196. return ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1,
  197. parent_rate);
  198. }
  199. const struct clk_ops ccu_mux_ops = {
  200. .disable = ccu_mux_disable,
  201. .enable = ccu_mux_enable,
  202. .is_enabled = ccu_mux_is_enabled,
  203. .get_parent = ccu_mux_get_parent,
  204. .set_parent = ccu_mux_set_parent,
  205. .determine_rate = __clk_mux_determine_rate,
  206. .recalc_rate = ccu_mux_recalc_rate,
  207. };
  208. EXPORT_SYMBOL_NS_GPL(ccu_mux_ops, SUNXI_CCU);
  209. /*
  210. * This clock notifier is called when the frequency of the of the parent
  211. * PLL clock is to be changed. The idea is to switch the parent to a
  212. * stable clock, such as the main oscillator, while the PLL frequency
  213. * stabilizes.
  214. */
  215. static int ccu_mux_notifier_cb(struct notifier_block *nb,
  216. unsigned long event, void *data)
  217. {
  218. struct ccu_mux_nb *mux = to_ccu_mux_nb(nb);
  219. int ret = 0;
  220. if (event == PRE_RATE_CHANGE) {
  221. mux->original_index = ccu_mux_helper_get_parent(mux->common,
  222. mux->cm);
  223. ret = ccu_mux_helper_set_parent(mux->common, mux->cm,
  224. mux->bypass_index);
  225. } else if (event == POST_RATE_CHANGE) {
  226. ret = ccu_mux_helper_set_parent(mux->common, mux->cm,
  227. mux->original_index);
  228. }
  229. udelay(mux->delay_us);
  230. return notifier_from_errno(ret);
  231. }
  232. int ccu_mux_notifier_register(struct clk *clk, struct ccu_mux_nb *mux_nb)
  233. {
  234. mux_nb->clk_nb.notifier_call = ccu_mux_notifier_cb;
  235. return clk_notifier_register(clk, &mux_nb->clk_nb);
  236. }
  237. EXPORT_SYMBOL_NS_GPL(ccu_mux_notifier_register, SUNXI_CCU);