clk-pll.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/bitops.h>
  7. #include <linux/err.h>
  8. #include <linux/bug.h>
  9. #include <linux/delay.h>
  10. #include <linux/export.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/regmap.h>
  13. #include <asm/div64.h>
  14. #include "clk-pll.h"
  15. #include "common.h"
  16. #define PLL_OUTCTRL BIT(0)
  17. #define PLL_BYPASSNL BIT(1)
  18. #define PLL_RESET_N BIT(2)
  19. static int clk_pll_enable(struct clk_hw *hw)
  20. {
  21. struct clk_pll *pll = to_clk_pll(hw);
  22. int ret;
  23. u32 mask, val;
  24. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  25. ret = regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  26. if (ret)
  27. return ret;
  28. /* Skip if already enabled or in FSM mode */
  29. if ((val & mask) == mask || val & PLL_VOTE_FSM_ENA)
  30. return 0;
  31. /* Disable PLL bypass mode. */
  32. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_BYPASSNL,
  33. PLL_BYPASSNL);
  34. if (ret)
  35. return ret;
  36. /*
  37. * H/W requires a 5us delay between disabling the bypass and
  38. * de-asserting the reset. Delay 10us just to be safe.
  39. */
  40. udelay(10);
  41. /* De-assert active-low PLL reset. */
  42. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_RESET_N,
  43. PLL_RESET_N);
  44. if (ret)
  45. return ret;
  46. /* Wait until PLL is locked. */
  47. udelay(50);
  48. /* Enable PLL output. */
  49. return regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_OUTCTRL,
  50. PLL_OUTCTRL);
  51. }
  52. static void clk_pll_disable(struct clk_hw *hw)
  53. {
  54. struct clk_pll *pll = to_clk_pll(hw);
  55. u32 mask;
  56. u32 val;
  57. regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  58. /* Skip if in FSM mode */
  59. if (val & PLL_VOTE_FSM_ENA)
  60. return;
  61. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  62. regmap_update_bits(pll->clkr.regmap, pll->mode_reg, mask, 0);
  63. }
  64. static unsigned long
  65. clk_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  66. {
  67. struct clk_pll *pll = to_clk_pll(hw);
  68. u32 l, m, n, config;
  69. unsigned long rate;
  70. u64 tmp;
  71. regmap_read(pll->clkr.regmap, pll->l_reg, &l);
  72. regmap_read(pll->clkr.regmap, pll->m_reg, &m);
  73. regmap_read(pll->clkr.regmap, pll->n_reg, &n);
  74. l &= 0x3ff;
  75. m &= 0x7ffff;
  76. n &= 0x7ffff;
  77. rate = parent_rate * l;
  78. if (n) {
  79. tmp = parent_rate;
  80. tmp *= m;
  81. do_div(tmp, n);
  82. rate += tmp;
  83. }
  84. if (pll->post_div_width) {
  85. regmap_read(pll->clkr.regmap, pll->config_reg, &config);
  86. config >>= pll->post_div_shift;
  87. config &= BIT(pll->post_div_width) - 1;
  88. rate /= config + 1;
  89. }
  90. return rate;
  91. }
  92. static const
  93. struct pll_freq_tbl *find_freq(const struct pll_freq_tbl *f, unsigned long rate)
  94. {
  95. if (!f)
  96. return NULL;
  97. for (; f->freq; f++)
  98. if (rate <= f->freq)
  99. return f;
  100. return NULL;
  101. }
  102. static int
  103. clk_pll_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  104. {
  105. struct clk_pll *pll = to_clk_pll(hw);
  106. const struct pll_freq_tbl *f;
  107. f = find_freq(pll->freq_tbl, req->rate);
  108. if (!f)
  109. req->rate = clk_pll_recalc_rate(hw, req->best_parent_rate);
  110. else
  111. req->rate = f->freq;
  112. return 0;
  113. }
  114. static int
  115. clk_pll_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long p_rate)
  116. {
  117. struct clk_pll *pll = to_clk_pll(hw);
  118. const struct pll_freq_tbl *f;
  119. bool enabled;
  120. u32 mode;
  121. u32 enable_mask = PLL_OUTCTRL | PLL_BYPASSNL | PLL_RESET_N;
  122. f = find_freq(pll->freq_tbl, rate);
  123. if (!f)
  124. return -EINVAL;
  125. regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  126. enabled = (mode & enable_mask) == enable_mask;
  127. if (enabled)
  128. clk_pll_disable(hw);
  129. regmap_update_bits(pll->clkr.regmap, pll->l_reg, 0x3ff, f->l);
  130. regmap_update_bits(pll->clkr.regmap, pll->m_reg, 0x7ffff, f->m);
  131. regmap_update_bits(pll->clkr.regmap, pll->n_reg, 0x7ffff, f->n);
  132. regmap_write(pll->clkr.regmap, pll->config_reg, f->ibits);
  133. if (enabled)
  134. clk_pll_enable(hw);
  135. return 0;
  136. }
  137. const struct clk_ops clk_pll_ops = {
  138. .enable = clk_pll_enable,
  139. .disable = clk_pll_disable,
  140. .recalc_rate = clk_pll_recalc_rate,
  141. .determine_rate = clk_pll_determine_rate,
  142. .set_rate = clk_pll_set_rate,
  143. };
  144. EXPORT_SYMBOL_GPL(clk_pll_ops);
  145. static int wait_for_pll(struct clk_pll *pll)
  146. {
  147. u32 val;
  148. int count;
  149. int ret;
  150. const char *name = clk_hw_get_name(&pll->clkr.hw);
  151. /* Wait for pll to enable. */
  152. for (count = 200; count > 0; count--) {
  153. ret = regmap_read(pll->clkr.regmap, pll->status_reg, &val);
  154. if (ret)
  155. return ret;
  156. if (val & BIT(pll->status_bit))
  157. return 0;
  158. udelay(1);
  159. }
  160. WARN(1, "%s didn't enable after voting for it!\n", name);
  161. return -ETIMEDOUT;
  162. }
  163. static int clk_pll_vote_enable(struct clk_hw *hw)
  164. {
  165. int ret;
  166. struct clk_pll *p = to_clk_pll(clk_hw_get_parent(hw));
  167. ret = clk_enable_regmap(hw);
  168. if (ret)
  169. return ret;
  170. return wait_for_pll(p);
  171. }
  172. const struct clk_ops clk_pll_vote_ops = {
  173. .enable = clk_pll_vote_enable,
  174. .disable = clk_disable_regmap,
  175. };
  176. EXPORT_SYMBOL_GPL(clk_pll_vote_ops);
  177. static void clk_pll_configure(struct clk_pll *pll, struct regmap *regmap,
  178. const struct pll_config *config)
  179. {
  180. u32 val;
  181. u32 mask;
  182. regmap_write(regmap, pll->l_reg, config->l);
  183. regmap_write(regmap, pll->m_reg, config->m);
  184. regmap_write(regmap, pll->n_reg, config->n);
  185. val = config->vco_val;
  186. val |= config->pre_div_val;
  187. val |= config->post_div_val;
  188. val |= config->mn_ena_mask;
  189. val |= config->main_output_mask;
  190. val |= config->aux_output_mask;
  191. mask = config->vco_mask;
  192. mask |= config->pre_div_mask;
  193. mask |= config->post_div_mask;
  194. mask |= config->mn_ena_mask;
  195. mask |= config->main_output_mask;
  196. mask |= config->aux_output_mask;
  197. regmap_update_bits(regmap, pll->config_reg, mask, val);
  198. }
  199. void clk_pll_configure_sr(struct clk_pll *pll, struct regmap *regmap,
  200. const struct pll_config *config, bool fsm_mode)
  201. {
  202. clk_pll_configure(pll, regmap, config);
  203. if (fsm_mode)
  204. qcom_pll_set_fsm_mode(regmap, pll->mode_reg, 1, 8);
  205. }
  206. EXPORT_SYMBOL_GPL(clk_pll_configure_sr);
  207. void clk_pll_configure_sr_hpm_lp(struct clk_pll *pll, struct regmap *regmap,
  208. const struct pll_config *config, bool fsm_mode)
  209. {
  210. clk_pll_configure(pll, regmap, config);
  211. if (fsm_mode)
  212. qcom_pll_set_fsm_mode(regmap, pll->mode_reg, 1, 0);
  213. }
  214. EXPORT_SYMBOL_GPL(clk_pll_configure_sr_hpm_lp);
  215. static int clk_pll_sr2_enable(struct clk_hw *hw)
  216. {
  217. struct clk_pll *pll = to_clk_pll(hw);
  218. int ret;
  219. u32 mode;
  220. ret = regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  221. if (ret)
  222. return ret;
  223. /* Disable PLL bypass mode. */
  224. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_BYPASSNL,
  225. PLL_BYPASSNL);
  226. if (ret)
  227. return ret;
  228. /*
  229. * H/W requires a 5us delay between disabling the bypass and
  230. * de-asserting the reset. Delay 10us just to be safe.
  231. */
  232. udelay(10);
  233. /* De-assert active-low PLL reset. */
  234. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_RESET_N,
  235. PLL_RESET_N);
  236. if (ret)
  237. return ret;
  238. ret = wait_for_pll(pll);
  239. if (ret)
  240. return ret;
  241. /* Enable PLL output. */
  242. return regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_OUTCTRL,
  243. PLL_OUTCTRL);
  244. }
  245. static int
  246. clk_pll_sr2_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long prate)
  247. {
  248. struct clk_pll *pll = to_clk_pll(hw);
  249. const struct pll_freq_tbl *f;
  250. bool enabled;
  251. u32 mode;
  252. u32 enable_mask = PLL_OUTCTRL | PLL_BYPASSNL | PLL_RESET_N;
  253. f = find_freq(pll->freq_tbl, rate);
  254. if (!f)
  255. return -EINVAL;
  256. regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  257. enabled = (mode & enable_mask) == enable_mask;
  258. if (enabled)
  259. clk_pll_disable(hw);
  260. regmap_update_bits(pll->clkr.regmap, pll->l_reg, 0x3ff, f->l);
  261. regmap_update_bits(pll->clkr.regmap, pll->m_reg, 0x7ffff, f->m);
  262. regmap_update_bits(pll->clkr.regmap, pll->n_reg, 0x7ffff, f->n);
  263. if (enabled)
  264. clk_pll_sr2_enable(hw);
  265. return 0;
  266. }
  267. const struct clk_ops clk_pll_sr2_ops = {
  268. .enable = clk_pll_sr2_enable,
  269. .disable = clk_pll_disable,
  270. .set_rate = clk_pll_sr2_set_rate,
  271. .recalc_rate = clk_pll_recalc_rate,
  272. .determine_rate = clk_pll_determine_rate,
  273. };
  274. EXPORT_SYMBOL_GPL(clk_pll_sr2_ops);