clk-pll.c 8.0 KB

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