ccu_mux.c 7.1 KB

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