clk-hfpll.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. do {
  60. regmap_read(regmap, hd->status_reg, &val);
  61. } while (!(val & BIT(hd->lock_bit)));
  62. } else {
  63. udelay(60);
  64. }
  65. /* Enable PLL output. */
  66. regmap_update_bits(regmap, hd->mode_reg, PLL_OUTCTRL, PLL_OUTCTRL);
  67. }
  68. /* Enable an already-configured HFPLL. */
  69. static int clk_hfpll_enable(struct clk_hw *hw)
  70. {
  71. unsigned long flags;
  72. struct clk_hfpll *h = to_clk_hfpll(hw);
  73. struct hfpll_data const *hd = h->d;
  74. struct regmap *regmap = h->clkr.regmap;
  75. u32 mode;
  76. spin_lock_irqsave(&h->lock, flags);
  77. regmap_read(regmap, hd->mode_reg, &mode);
  78. if (!(mode & (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)))
  79. __clk_hfpll_enable(hw);
  80. spin_unlock_irqrestore(&h->lock, flags);
  81. return 0;
  82. }
  83. static void __clk_hfpll_disable(struct clk_hfpll *h)
  84. {
  85. struct hfpll_data const *hd = h->d;
  86. struct regmap *regmap = h->clkr.regmap;
  87. /*
  88. * Disable the PLL output, disable test mode, enable the bypass mode,
  89. * and assert the reset.
  90. */
  91. regmap_update_bits(regmap, hd->mode_reg,
  92. PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL, 0);
  93. }
  94. static void clk_hfpll_disable(struct clk_hw *hw)
  95. {
  96. struct clk_hfpll *h = to_clk_hfpll(hw);
  97. unsigned long flags;
  98. spin_lock_irqsave(&h->lock, flags);
  99. __clk_hfpll_disable(h);
  100. spin_unlock_irqrestore(&h->lock, flags);
  101. }
  102. static long clk_hfpll_round_rate(struct clk_hw *hw, unsigned long rate,
  103. unsigned long *parent_rate)
  104. {
  105. struct clk_hfpll *h = to_clk_hfpll(hw);
  106. struct hfpll_data const *hd = h->d;
  107. unsigned long rrate;
  108. rate = clamp(rate, hd->min_rate, hd->max_rate);
  109. rrate = DIV_ROUND_UP(rate, *parent_rate) * *parent_rate;
  110. if (rrate > hd->max_rate)
  111. rrate -= *parent_rate;
  112. return rrate;
  113. }
  114. /*
  115. * For optimization reasons, assumes no downstream clocks are actively using
  116. * it.
  117. */
  118. static int clk_hfpll_set_rate(struct clk_hw *hw, unsigned long rate,
  119. unsigned long parent_rate)
  120. {
  121. struct clk_hfpll *h = to_clk_hfpll(hw);
  122. struct hfpll_data const *hd = h->d;
  123. struct regmap *regmap = h->clkr.regmap;
  124. unsigned long flags;
  125. u32 l_val, val;
  126. bool enabled;
  127. l_val = rate / parent_rate;
  128. spin_lock_irqsave(&h->lock, flags);
  129. enabled = __clk_is_enabled(hw->clk);
  130. if (enabled)
  131. __clk_hfpll_disable(h);
  132. /* Pick the right VCO. */
  133. if (hd->user_reg && hd->user_vco_mask) {
  134. regmap_read(regmap, hd->user_reg, &val);
  135. if (rate <= hd->low_vco_max_rate)
  136. val &= ~hd->user_vco_mask;
  137. else
  138. val |= hd->user_vco_mask;
  139. regmap_write(regmap, hd->user_reg, val);
  140. }
  141. regmap_write(regmap, hd->l_reg, l_val);
  142. if (enabled)
  143. __clk_hfpll_enable(hw);
  144. spin_unlock_irqrestore(&h->lock, flags);
  145. return 0;
  146. }
  147. static unsigned long clk_hfpll_recalc_rate(struct clk_hw *hw,
  148. unsigned long parent_rate)
  149. {
  150. struct clk_hfpll *h = to_clk_hfpll(hw);
  151. struct hfpll_data const *hd = h->d;
  152. struct regmap *regmap = h->clkr.regmap;
  153. u32 l_val;
  154. regmap_read(regmap, hd->l_reg, &l_val);
  155. return l_val * parent_rate;
  156. }
  157. static void clk_hfpll_init(struct clk_hw *hw)
  158. {
  159. struct clk_hfpll *h = to_clk_hfpll(hw);
  160. struct hfpll_data const *hd = h->d;
  161. struct regmap *regmap = h->clkr.regmap;
  162. u32 mode, status;
  163. regmap_read(regmap, hd->mode_reg, &mode);
  164. if (mode != (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL)) {
  165. __clk_hfpll_init_once(hw);
  166. return;
  167. }
  168. if (hd->status_reg) {
  169. regmap_read(regmap, hd->status_reg, &status);
  170. if (!(status & BIT(hd->lock_bit))) {
  171. WARN(1, "HFPLL %s is ON, but not locked!\n",
  172. __clk_get_name(hw->clk));
  173. clk_hfpll_disable(hw);
  174. __clk_hfpll_init_once(hw);
  175. }
  176. }
  177. }
  178. static int hfpll_is_enabled(struct clk_hw *hw)
  179. {
  180. struct clk_hfpll *h = to_clk_hfpll(hw);
  181. struct hfpll_data const *hd = h->d;
  182. struct regmap *regmap = h->clkr.regmap;
  183. u32 mode;
  184. regmap_read(regmap, hd->mode_reg, &mode);
  185. mode &= 0x7;
  186. return mode == (PLL_BYPASSNL | PLL_RESET_N | PLL_OUTCTRL);
  187. }
  188. const struct clk_ops clk_ops_hfpll = {
  189. .enable = clk_hfpll_enable,
  190. .disable = clk_hfpll_disable,
  191. .is_enabled = hfpll_is_enabled,
  192. .round_rate = clk_hfpll_round_rate,
  193. .set_rate = clk_hfpll_set_rate,
  194. .recalc_rate = clk_hfpll_recalc_rate,
  195. .init = clk_hfpll_init,
  196. };
  197. EXPORT_SYMBOL_GPL(clk_ops_hfpll);