clk-hfpll.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. if (hd->droop_reg)
  38. regmap_write(regmap, hd->droop_reg, hd->droop_val);
  39. h->init_done = true;
  40. }
  41. static void __clk_hfpll_enable(struct clk_hw *hw)
  42. {
  43. struct clk_hfpll *h = to_clk_hfpll(hw);
  44. struct hfpll_data const *hd = h->d;
  45. struct regmap *regmap = h->clkr.regmap;
  46. u32 val;
  47. __clk_hfpll_init_once(hw);
  48. /* Disable PLL bypass mode. */
  49. regmap_update_bits(regmap, hd->mode_reg, PLL_BYPASSNL, PLL_BYPASSNL);
  50. /*
  51. * H/W requires a 5us delay between disabling the bypass and
  52. * de-asserting the reset. Delay 10us just to be safe.
  53. */
  54. udelay(10);
  55. /* De-assert active-low PLL reset. */
  56. regmap_update_bits(regmap, hd->mode_reg, PLL_RESET_N, PLL_RESET_N);
  57. /* Wait for PLL to lock. */
  58. if (hd->status_reg)
  59. /*
  60. * Busy wait. Should never timeout, we add a timeout to
  61. * prevent any sort of stall.
  62. */
  63. regmap_read_poll_timeout(regmap, hd->status_reg, val,
  64. !(val & BIT(hd->lock_bit)), 0,
  65. 100 * USEC_PER_MSEC);
  66. else
  67. udelay(60);
  68. /* Enable PLL output. */
  69. regmap_update_bits(regmap, hd->mode_reg, PLL_OUTCTRL, PLL_OUTCTRL);
  70. }
  71. /* Enable an already-configured HFPLL. */
  72. static int clk_hfpll_enable(struct clk_hw *hw)
  73. {
  74. unsigned long flags;
  75. struct clk_hfpll *h = to_clk_hfpll(hw);
  76. struct hfpll_data const *hd = h->d;
  77. struct regmap *regmap = h->clkr.regmap;
  78. u32 mode;
  79. spin_lock_irqsave(&h->lock, flags);
  80. regmap_read(regmap, hd->mode_reg, &mode);
  81. if (!(mode & (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)))
  82. __clk_hfpll_enable(hw);
  83. spin_unlock_irqrestore(&h->lock, flags);
  84. return 0;
  85. }
  86. static void __clk_hfpll_disable(struct clk_hfpll *h)
  87. {
  88. struct hfpll_data const *hd = h->d;
  89. struct regmap *regmap = h->clkr.regmap;
  90. /*
  91. * Disable the PLL output, disable test mode, enable the bypass mode,
  92. * and assert the reset.
  93. */
  94. regmap_update_bits(regmap, hd->mode_reg,
  95. PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL, 0);
  96. }
  97. static void clk_hfpll_disable(struct clk_hw *hw)
  98. {
  99. struct clk_hfpll *h = to_clk_hfpll(hw);
  100. unsigned long flags;
  101. spin_lock_irqsave(&h->lock, flags);
  102. __clk_hfpll_disable(h);
  103. spin_unlock_irqrestore(&h->lock, flags);
  104. }
  105. static long clk_hfpll_round_rate(struct clk_hw *hw, unsigned long rate,
  106. unsigned long *parent_rate)
  107. {
  108. struct clk_hfpll *h = to_clk_hfpll(hw);
  109. struct hfpll_data const *hd = h->d;
  110. unsigned long rrate;
  111. rate = clamp(rate, hd->min_rate, hd->max_rate);
  112. rrate = DIV_ROUND_UP(rate, *parent_rate) * *parent_rate;
  113. if (rrate > hd->max_rate)
  114. rrate -= *parent_rate;
  115. return rrate;
  116. }
  117. /*
  118. * For optimization reasons, assumes no downstream clocks are actively using
  119. * it.
  120. */
  121. static int clk_hfpll_set_rate(struct clk_hw *hw, unsigned long rate,
  122. unsigned long parent_rate)
  123. {
  124. struct clk_hfpll *h = to_clk_hfpll(hw);
  125. struct hfpll_data const *hd = h->d;
  126. struct regmap *regmap = h->clkr.regmap;
  127. unsigned long flags;
  128. u32 l_val, val;
  129. bool enabled;
  130. l_val = rate / parent_rate;
  131. spin_lock_irqsave(&h->lock, flags);
  132. enabled = __clk_is_enabled(hw->clk);
  133. if (enabled)
  134. __clk_hfpll_disable(h);
  135. /* Pick the right VCO. */
  136. if (hd->user_reg && hd->user_vco_mask) {
  137. regmap_read(regmap, hd->user_reg, &val);
  138. if (rate <= hd->low_vco_max_rate)
  139. val &= ~hd->user_vco_mask;
  140. else
  141. val |= hd->user_vco_mask;
  142. regmap_write(regmap, hd->user_reg, val);
  143. }
  144. regmap_write(regmap, hd->l_reg, l_val);
  145. if (enabled)
  146. __clk_hfpll_enable(hw);
  147. spin_unlock_irqrestore(&h->lock, flags);
  148. return 0;
  149. }
  150. static unsigned long clk_hfpll_recalc_rate(struct clk_hw *hw,
  151. unsigned long parent_rate)
  152. {
  153. struct clk_hfpll *h = to_clk_hfpll(hw);
  154. struct hfpll_data const *hd = h->d;
  155. struct regmap *regmap = h->clkr.regmap;
  156. u32 l_val;
  157. regmap_read(regmap, hd->l_reg, &l_val);
  158. return l_val * parent_rate;
  159. }
  160. static int clk_hfpll_init(struct clk_hw *hw)
  161. {
  162. struct clk_hfpll *h = to_clk_hfpll(hw);
  163. struct hfpll_data const *hd = h->d;
  164. struct regmap *regmap = h->clkr.regmap;
  165. u32 mode, status;
  166. regmap_read(regmap, hd->mode_reg, &mode);
  167. if (mode != (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)) {
  168. __clk_hfpll_init_once(hw);
  169. return 0;
  170. }
  171. if (hd->status_reg) {
  172. regmap_read(regmap, hd->status_reg, &status);
  173. if (!(status & BIT(hd->lock_bit))) {
  174. WARN(1, "HFPLL %s is ON, but not locked!\n",
  175. __clk_get_name(hw->clk));
  176. clk_hfpll_disable(hw);
  177. __clk_hfpll_init_once(hw);
  178. }
  179. }
  180. return 0;
  181. }
  182. static int hfpll_is_enabled(struct clk_hw *hw)
  183. {
  184. struct clk_hfpll *h = to_clk_hfpll(hw);
  185. struct hfpll_data const *hd = h->d;
  186. struct regmap *regmap = h->clkr.regmap;
  187. u32 mode;
  188. regmap_read(regmap, hd->mode_reg, &mode);
  189. mode &= 0x7;
  190. return mode == (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL);
  191. }
  192. const struct clk_ops clk_ops_hfpll = {
  193. .enable = clk_hfpll_enable,
  194. .disable = clk_hfpll_disable,
  195. .is_enabled = hfpll_is_enabled,
  196. .round_rate = clk_hfpll_round_rate,
  197. .set_rate = clk_hfpll_set_rate,
  198. .recalc_rate = clk_hfpll_recalc_rate,
  199. .init = clk_hfpll_init,
  200. };
  201. EXPORT_SYMBOL_GPL(clk_ops_hfpll);