clk-hfpll.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018, The Linux Foundation. All rights reserved.
  3. #include <linux/kernel.h>
  4. #include <linux/export.h>
  5. #include <linux/regmap.h>
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/spinlock.h>
  10. #include "clk-regmap.h"
  11. #include "clk-hfpll.h"
  12. #define PLL_OUTCTRL BIT(0)
  13. #define PLL_BYPASSNL BIT(1)
  14. #define PLL_RESET_N BIT(2)
  15. /* Initialize a HFPLL at a given rate and enable it. */
  16. static void __clk_hfpll_init_once(struct clk_hw *hw)
  17. {
  18. struct clk_hfpll *h = to_clk_hfpll(hw);
  19. struct hfpll_data const *hd = h->d;
  20. struct regmap *regmap = h->clkr.regmap;
  21. if (likely(h->init_done))
  22. return;
  23. /* Configure PLL parameters for integer mode. */
  24. if (hd->config_val)
  25. regmap_write(regmap, hd->config_reg, hd->config_val);
  26. regmap_write(regmap, hd->m_reg, 0);
  27. regmap_write(regmap, hd->n_reg, 1);
  28. if (hd->user_reg) {
  29. u32 regval = hd->user_val;
  30. unsigned long rate;
  31. rate = clk_hw_get_rate(hw);
  32. /* Pick the right VCO. */
  33. if (hd->user_vco_mask && rate > hd->low_vco_max_rate)
  34. regval |= hd->user_vco_mask;
  35. regmap_write(regmap, hd->user_reg, regval);
  36. }
  37. /* Write L_VAL from conf if it exist */
  38. if (hd->l_val)
  39. regmap_write(regmap, hd->l_reg, hd->l_val);
  40. if (hd->droop_reg)
  41. regmap_write(regmap, hd->droop_reg, hd->droop_val);
  42. h->init_done = true;
  43. }
  44. static void __clk_hfpll_enable(struct clk_hw *hw)
  45. {
  46. struct clk_hfpll *h = to_clk_hfpll(hw);
  47. struct hfpll_data const *hd = h->d;
  48. struct regmap *regmap = h->clkr.regmap;
  49. u32 val;
  50. __clk_hfpll_init_once(hw);
  51. /* Disable PLL bypass mode. */
  52. regmap_update_bits(regmap, hd->mode_reg, PLL_BYPASSNL, PLL_BYPASSNL);
  53. /*
  54. * H/W requires a 5us delay between disabling the bypass and
  55. * de-asserting the reset. Delay 10us just to be safe.
  56. */
  57. udelay(10);
  58. /* De-assert active-low PLL reset. */
  59. regmap_update_bits(regmap, hd->mode_reg, PLL_RESET_N, PLL_RESET_N);
  60. /* Wait for PLL to lock. */
  61. if (hd->status_reg)
  62. /*
  63. * Busy wait. Should never timeout, we add a timeout to
  64. * prevent any sort of stall.
  65. */
  66. regmap_read_poll_timeout(regmap, hd->status_reg, val,
  67. !(val & BIT(hd->lock_bit)), 0,
  68. 100 * USEC_PER_MSEC);
  69. else
  70. udelay(60);
  71. /* Enable PLL output. */
  72. regmap_update_bits(regmap, hd->mode_reg, PLL_OUTCTRL, PLL_OUTCTRL);
  73. }
  74. /* Enable an already-configured HFPLL. */
  75. static int clk_hfpll_enable(struct clk_hw *hw)
  76. {
  77. unsigned long flags;
  78. struct clk_hfpll *h = to_clk_hfpll(hw);
  79. struct hfpll_data const *hd = h->d;
  80. struct regmap *regmap = h->clkr.regmap;
  81. u32 mode;
  82. spin_lock_irqsave(&h->lock, flags);
  83. regmap_read(regmap, hd->mode_reg, &mode);
  84. if (!(mode & (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)))
  85. __clk_hfpll_enable(hw);
  86. spin_unlock_irqrestore(&h->lock, flags);
  87. return 0;
  88. }
  89. static void __clk_hfpll_disable(struct clk_hfpll *h)
  90. {
  91. struct hfpll_data const *hd = h->d;
  92. struct regmap *regmap = h->clkr.regmap;
  93. /*
  94. * Disable the PLL output, disable test mode, enable the bypass mode,
  95. * and assert the reset.
  96. */
  97. regmap_update_bits(regmap, hd->mode_reg,
  98. PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL, 0);
  99. }
  100. static void clk_hfpll_disable(struct clk_hw *hw)
  101. {
  102. struct clk_hfpll *h = to_clk_hfpll(hw);
  103. unsigned long flags;
  104. spin_lock_irqsave(&h->lock, flags);
  105. __clk_hfpll_disable(h);
  106. spin_unlock_irqrestore(&h->lock, flags);
  107. }
  108. static int clk_hfpll_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  109. {
  110. struct clk_hfpll *h = to_clk_hfpll(hw);
  111. struct hfpll_data const *hd = h->d;
  112. unsigned long rrate;
  113. req->rate = clamp(req->rate, hd->min_rate, hd->max_rate);
  114. rrate = DIV_ROUND_UP(req->rate, req->best_parent_rate) * req->best_parent_rate;
  115. if (rrate > hd->max_rate)
  116. rrate -= req->best_parent_rate;
  117. req->rate = rrate;
  118. return 0;
  119. }
  120. /*
  121. * For optimization reasons, assumes no downstream clocks are actively using
  122. * it.
  123. */
  124. static int clk_hfpll_set_rate(struct clk_hw *hw, unsigned long rate,
  125. unsigned long parent_rate)
  126. {
  127. struct clk_hfpll *h = to_clk_hfpll(hw);
  128. struct hfpll_data const *hd = h->d;
  129. struct regmap *regmap = h->clkr.regmap;
  130. unsigned long flags;
  131. u32 l_val, val;
  132. bool enabled;
  133. l_val = rate / parent_rate;
  134. spin_lock_irqsave(&h->lock, flags);
  135. enabled = __clk_is_enabled(hw->clk);
  136. if (enabled)
  137. __clk_hfpll_disable(h);
  138. /* Pick the right VCO. */
  139. if (hd->user_reg && hd->user_vco_mask) {
  140. regmap_read(regmap, hd->user_reg, &val);
  141. if (rate <= hd->low_vco_max_rate)
  142. val &= ~hd->user_vco_mask;
  143. else
  144. val |= hd->user_vco_mask;
  145. regmap_write(regmap, hd->user_reg, val);
  146. }
  147. regmap_write(regmap, hd->l_reg, l_val);
  148. if (enabled)
  149. __clk_hfpll_enable(hw);
  150. spin_unlock_irqrestore(&h->lock, flags);
  151. return 0;
  152. }
  153. static unsigned long clk_hfpll_recalc_rate(struct clk_hw *hw,
  154. unsigned long parent_rate)
  155. {
  156. struct clk_hfpll *h = to_clk_hfpll(hw);
  157. struct hfpll_data const *hd = h->d;
  158. struct regmap *regmap = h->clkr.regmap;
  159. u32 l_val;
  160. regmap_read(regmap, hd->l_reg, &l_val);
  161. return l_val * parent_rate;
  162. }
  163. static int clk_hfpll_init(struct clk_hw *hw)
  164. {
  165. struct clk_hfpll *h = to_clk_hfpll(hw);
  166. struct hfpll_data const *hd = h->d;
  167. struct regmap *regmap = h->clkr.regmap;
  168. u32 mode, status;
  169. regmap_read(regmap, hd->mode_reg, &mode);
  170. if (mode != (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)) {
  171. __clk_hfpll_init_once(hw);
  172. return 0;
  173. }
  174. if (hd->status_reg) {
  175. regmap_read(regmap, hd->status_reg, &status);
  176. if (!(status & BIT(hd->lock_bit))) {
  177. WARN(1, "HFPLL %s is ON, but not locked!\n",
  178. __clk_get_name(hw->clk));
  179. clk_hfpll_disable(hw);
  180. __clk_hfpll_init_once(hw);
  181. }
  182. }
  183. return 0;
  184. }
  185. static int hfpll_is_enabled(struct clk_hw *hw)
  186. {
  187. struct clk_hfpll *h = to_clk_hfpll(hw);
  188. struct hfpll_data const *hd = h->d;
  189. struct regmap *regmap = h->clkr.regmap;
  190. u32 mode;
  191. regmap_read(regmap, hd->mode_reg, &mode);
  192. mode &= 0x7;
  193. return mode == (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL);
  194. }
  195. const struct clk_ops clk_ops_hfpll = {
  196. .enable = clk_hfpll_enable,
  197. .disable = clk_hfpll_disable,
  198. .is_enabled = hfpll_is_enabled,
  199. .determine_rate = clk_hfpll_determine_rate,
  200. .set_rate = clk_hfpll_set_rate,
  201. .recalc_rate = clk_hfpll_recalc_rate,
  202. .init = clk_hfpll_init,
  203. };
  204. EXPORT_SYMBOL_GPL(clk_ops_hfpll);