clk-pll.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #define PLL_OUTCTRL BIT(0)
  24. #define PLL_BYPASSNL BIT(1)
  25. #define PLL_RESET_N BIT(2)
  26. #define PLL_LOCK_COUNT_SHIFT 8
  27. #define PLL_LOCK_COUNT_MASK 0x3f
  28. #define PLL_BIAS_COUNT_SHIFT 14
  29. #define PLL_BIAS_COUNT_MASK 0x3f
  30. #define PLL_VOTE_FSM_ENA BIT(20)
  31. #define PLL_VOTE_FSM_RESET BIT(21)
  32. static int clk_pll_enable(struct clk_hw *hw)
  33. {
  34. struct clk_pll *pll = to_clk_pll(hw);
  35. int ret;
  36. u32 mask, val;
  37. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  38. ret = regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  39. if (ret)
  40. return ret;
  41. /* Skip if already enabled or in FSM mode */
  42. if ((val & mask) == mask || val & PLL_VOTE_FSM_ENA)
  43. return 0;
  44. /* Disable PLL bypass mode. */
  45. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_BYPASSNL,
  46. PLL_BYPASSNL);
  47. if (ret)
  48. return ret;
  49. /*
  50. * H/W requires a 5us delay between disabling the bypass and
  51. * de-asserting the reset. Delay 10us just to be safe.
  52. */
  53. udelay(10);
  54. /* De-assert active-low PLL reset. */
  55. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_RESET_N,
  56. PLL_RESET_N);
  57. if (ret)
  58. return ret;
  59. /* Wait until PLL is locked. */
  60. udelay(50);
  61. /* Enable PLL output. */
  62. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_OUTCTRL,
  63. PLL_OUTCTRL);
  64. if (ret)
  65. return ret;
  66. return 0;
  67. }
  68. static void clk_pll_disable(struct clk_hw *hw)
  69. {
  70. struct clk_pll *pll = to_clk_pll(hw);
  71. u32 mask;
  72. u32 val;
  73. regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  74. /* Skip if in FSM mode */
  75. if (val & PLL_VOTE_FSM_ENA)
  76. return;
  77. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  78. regmap_update_bits(pll->clkr.regmap, pll->mode_reg, mask, 0);
  79. }
  80. static unsigned long
  81. clk_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  82. {
  83. struct clk_pll *pll = to_clk_pll(hw);
  84. u32 l, m, n;
  85. unsigned long rate;
  86. u64 tmp;
  87. regmap_read(pll->clkr.regmap, pll->l_reg, &l);
  88. regmap_read(pll->clkr.regmap, pll->m_reg, &m);
  89. regmap_read(pll->clkr.regmap, pll->n_reg, &n);
  90. l &= 0x3ff;
  91. m &= 0x7ffff;
  92. n &= 0x7ffff;
  93. rate = parent_rate * l;
  94. if (n) {
  95. tmp = parent_rate;
  96. tmp *= m;
  97. do_div(tmp, n);
  98. rate += tmp;
  99. }
  100. return rate;
  101. }
  102. const struct clk_ops clk_pll_ops = {
  103. .enable = clk_pll_enable,
  104. .disable = clk_pll_disable,
  105. .recalc_rate = clk_pll_recalc_rate,
  106. };
  107. EXPORT_SYMBOL_GPL(clk_pll_ops);
  108. static int wait_for_pll(struct clk_pll *pll)
  109. {
  110. u32 val;
  111. int count;
  112. int ret;
  113. const char *name = __clk_get_name(pll->clkr.hw.clk);
  114. /* Wait for pll to enable. */
  115. for (count = 200; count > 0; count--) {
  116. ret = regmap_read(pll->clkr.regmap, pll->status_reg, &val);
  117. if (ret)
  118. return ret;
  119. if (val & BIT(pll->status_bit))
  120. return 0;
  121. udelay(1);
  122. }
  123. WARN(1, "%s didn't enable after voting for it!\n", name);
  124. return -ETIMEDOUT;
  125. }
  126. static int clk_pll_vote_enable(struct clk_hw *hw)
  127. {
  128. int ret;
  129. struct clk_pll *p = to_clk_pll(__clk_get_hw(__clk_get_parent(hw->clk)));
  130. ret = clk_enable_regmap(hw);
  131. if (ret)
  132. return ret;
  133. return wait_for_pll(p);
  134. }
  135. const struct clk_ops clk_pll_vote_ops = {
  136. .enable = clk_pll_vote_enable,
  137. .disable = clk_disable_regmap,
  138. };
  139. EXPORT_SYMBOL_GPL(clk_pll_vote_ops);
  140. static void
  141. clk_pll_set_fsm_mode(struct clk_pll *pll, struct regmap *regmap)
  142. {
  143. u32 val;
  144. u32 mask;
  145. /* De-assert reset to FSM */
  146. regmap_update_bits(regmap, pll->mode_reg, PLL_VOTE_FSM_RESET, 0);
  147. /* Program bias count and lock count */
  148. val = 1 << PLL_BIAS_COUNT_SHIFT;
  149. mask = PLL_BIAS_COUNT_MASK << PLL_BIAS_COUNT_SHIFT;
  150. mask |= PLL_LOCK_COUNT_MASK << PLL_LOCK_COUNT_SHIFT;
  151. regmap_update_bits(regmap, pll->mode_reg, mask, val);
  152. /* Enable PLL FSM voting */
  153. regmap_update_bits(regmap, pll->mode_reg, PLL_VOTE_FSM_ENA,
  154. PLL_VOTE_FSM_ENA);
  155. }
  156. static void clk_pll_configure(struct clk_pll *pll, struct regmap *regmap,
  157. const struct pll_config *config)
  158. {
  159. u32 val;
  160. u32 mask;
  161. regmap_write(regmap, pll->l_reg, config->l);
  162. regmap_write(regmap, pll->m_reg, config->m);
  163. regmap_write(regmap, pll->n_reg, config->n);
  164. val = config->vco_val;
  165. val |= config->pre_div_val;
  166. val |= config->post_div_val;
  167. val |= config->mn_ena_mask;
  168. val |= config->main_output_mask;
  169. val |= config->aux_output_mask;
  170. mask = config->vco_mask;
  171. mask |= config->pre_div_mask;
  172. mask |= config->post_div_mask;
  173. mask |= config->mn_ena_mask;
  174. mask |= config->main_output_mask;
  175. mask |= config->aux_output_mask;
  176. regmap_update_bits(regmap, pll->config_reg, mask, val);
  177. }
  178. void clk_pll_configure_sr_hpm_lp(struct clk_pll *pll, struct regmap *regmap,
  179. const struct pll_config *config, bool fsm_mode)
  180. {
  181. clk_pll_configure(pll, regmap, config);
  182. if (fsm_mode)
  183. clk_pll_set_fsm_mode(pll, regmap);
  184. }
  185. EXPORT_SYMBOL_GPL(clk_pll_configure_sr_hpm_lp);