pll.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Spreadtrum pll clock driver
  4. //
  5. // Copyright (C) 2015~2017 Spreadtrum, Inc.
  6. // Author: Chunyan Zhang <[email protected]>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/regmap.h>
  10. #include <linux/slab.h>
  11. #include "pll.h"
  12. #define CLK_PLL_1M 1000000
  13. #define CLK_PLL_10M (CLK_PLL_1M * 10)
  14. #define pindex(pll, member) \
  15. (pll->factors[member].shift / (8 * sizeof(pll->regs_num)))
  16. #define pshift(pll, member) \
  17. (pll->factors[member].shift % (8 * sizeof(pll->regs_num)))
  18. #define pwidth(pll, member) \
  19. pll->factors[member].width
  20. #define pmask(pll, member) \
  21. ((pwidth(pll, member)) ? \
  22. GENMASK(pwidth(pll, member) + pshift(pll, member) - 1, \
  23. pshift(pll, member)) : 0)
  24. #define pinternal(pll, cfg, member) \
  25. (cfg[pindex(pll, member)] & pmask(pll, member))
  26. #define pinternal_val(pll, cfg, member) \
  27. (pinternal(pll, cfg, member) >> pshift(pll, member))
  28. static inline unsigned int
  29. sprd_pll_read(const struct sprd_pll *pll, u8 index)
  30. {
  31. const struct sprd_clk_common *common = &pll->common;
  32. unsigned int val = 0;
  33. if (WARN_ON(index >= pll->regs_num))
  34. return 0;
  35. regmap_read(common->regmap, common->reg + index * 4, &val);
  36. return val;
  37. }
  38. static inline void
  39. sprd_pll_write(const struct sprd_pll *pll, u8 index,
  40. u32 msk, u32 val)
  41. {
  42. const struct sprd_clk_common *common = &pll->common;
  43. unsigned int offset, reg;
  44. int ret = 0;
  45. if (WARN_ON(index >= pll->regs_num))
  46. return;
  47. offset = common->reg + index * 4;
  48. ret = regmap_read(common->regmap, offset, &reg);
  49. if (!ret)
  50. regmap_write(common->regmap, offset, (reg & ~msk) | val);
  51. }
  52. static unsigned long pll_get_refin(const struct sprd_pll *pll)
  53. {
  54. u32 shift, mask, index, refin_id = 3;
  55. const unsigned long refin[4] = { 2, 4, 13, 26 };
  56. if (pwidth(pll, PLL_REFIN)) {
  57. index = pindex(pll, PLL_REFIN);
  58. shift = pshift(pll, PLL_REFIN);
  59. mask = pmask(pll, PLL_REFIN);
  60. refin_id = (sprd_pll_read(pll, index) & mask) >> shift;
  61. if (refin_id > 3)
  62. refin_id = 3;
  63. }
  64. return refin[refin_id];
  65. }
  66. static u32 pll_get_ibias(u64 rate, const u64 *table)
  67. {
  68. u32 i, num = table[0];
  69. /* table[0] indicates the number of items in this table */
  70. for (i = 0; i < num; i++)
  71. if (rate <= table[i + 1])
  72. break;
  73. return i == num ? num - 1 : i;
  74. }
  75. static unsigned long _sprd_pll_recalc_rate(const struct sprd_pll *pll,
  76. unsigned long parent_rate)
  77. {
  78. u32 *cfg;
  79. u32 i, mask, regs_num = pll->regs_num;
  80. unsigned long rate, nint, kint = 0;
  81. u64 refin;
  82. u16 k1, k2;
  83. cfg = kcalloc(regs_num, sizeof(*cfg), GFP_KERNEL);
  84. if (!cfg)
  85. return parent_rate;
  86. for (i = 0; i < regs_num; i++)
  87. cfg[i] = sprd_pll_read(pll, i);
  88. refin = pll_get_refin(pll);
  89. if (pinternal(pll, cfg, PLL_PREDIV))
  90. refin = refin * 2;
  91. if (pwidth(pll, PLL_POSTDIV) &&
  92. ((pll->fflag == 1 && pinternal(pll, cfg, PLL_POSTDIV)) ||
  93. (!pll->fflag && !pinternal(pll, cfg, PLL_POSTDIV))))
  94. refin = refin / 2;
  95. if (!pinternal(pll, cfg, PLL_DIV_S)) {
  96. rate = refin * pinternal_val(pll, cfg, PLL_N) * CLK_PLL_10M;
  97. } else {
  98. nint = pinternal_val(pll, cfg, PLL_NINT);
  99. if (pinternal(pll, cfg, PLL_SDM_EN))
  100. kint = pinternal_val(pll, cfg, PLL_KINT);
  101. mask = pmask(pll, PLL_KINT);
  102. k1 = pll->k1;
  103. k2 = pll->k2;
  104. rate = DIV_ROUND_CLOSEST_ULL(refin * kint * k1,
  105. ((mask >> __ffs(mask)) + 1)) *
  106. k2 + refin * nint * CLK_PLL_1M;
  107. }
  108. kfree(cfg);
  109. return rate;
  110. }
  111. #define SPRD_PLL_WRITE_CHECK(pll, i, mask, val) \
  112. (((sprd_pll_read(pll, i) & mask) == val) ? 0 : (-EFAULT))
  113. static int _sprd_pll_set_rate(const struct sprd_pll *pll,
  114. unsigned long rate,
  115. unsigned long parent_rate)
  116. {
  117. struct reg_cfg *cfg;
  118. int ret = 0;
  119. u32 mask, shift, width, ibias_val, index;
  120. u32 regs_num = pll->regs_num, i = 0;
  121. unsigned long kint, nint;
  122. u64 tmp, refin, fvco = rate;
  123. cfg = kcalloc(regs_num, sizeof(*cfg), GFP_KERNEL);
  124. if (!cfg)
  125. return -ENOMEM;
  126. refin = pll_get_refin(pll);
  127. mask = pmask(pll, PLL_PREDIV);
  128. index = pindex(pll, PLL_PREDIV);
  129. width = pwidth(pll, PLL_PREDIV);
  130. if (width && (sprd_pll_read(pll, index) & mask))
  131. refin = refin * 2;
  132. mask = pmask(pll, PLL_POSTDIV);
  133. index = pindex(pll, PLL_POSTDIV);
  134. width = pwidth(pll, PLL_POSTDIV);
  135. cfg[index].msk = mask;
  136. if (width && ((pll->fflag == 1 && fvco <= pll->fvco) ||
  137. (pll->fflag == 0 && fvco > pll->fvco)))
  138. cfg[index].val |= mask;
  139. if (width && fvco <= pll->fvco)
  140. fvco = fvco * 2;
  141. mask = pmask(pll, PLL_DIV_S);
  142. index = pindex(pll, PLL_DIV_S);
  143. cfg[index].val |= mask;
  144. cfg[index].msk |= mask;
  145. mask = pmask(pll, PLL_SDM_EN);
  146. index = pindex(pll, PLL_SDM_EN);
  147. cfg[index].val |= mask;
  148. cfg[index].msk |= mask;
  149. nint = do_div(fvco, refin * CLK_PLL_1M);
  150. mask = pmask(pll, PLL_NINT);
  151. index = pindex(pll, PLL_NINT);
  152. shift = pshift(pll, PLL_NINT);
  153. cfg[index].val |= (nint << shift) & mask;
  154. cfg[index].msk |= mask;
  155. mask = pmask(pll, PLL_KINT);
  156. index = pindex(pll, PLL_KINT);
  157. width = pwidth(pll, PLL_KINT);
  158. shift = pshift(pll, PLL_KINT);
  159. tmp = fvco - refin * nint * CLK_PLL_1M;
  160. tmp = do_div(tmp, 10000) * ((mask >> shift) + 1);
  161. kint = DIV_ROUND_CLOSEST_ULL(tmp, refin * 100);
  162. cfg[index].val |= (kint << shift) & mask;
  163. cfg[index].msk |= mask;
  164. ibias_val = pll_get_ibias(fvco, pll->itable);
  165. mask = pmask(pll, PLL_IBIAS);
  166. index = pindex(pll, PLL_IBIAS);
  167. shift = pshift(pll, PLL_IBIAS);
  168. cfg[index].val |= ibias_val << shift & mask;
  169. cfg[index].msk |= mask;
  170. for (i = 0; i < regs_num; i++) {
  171. if (cfg[i].msk) {
  172. sprd_pll_write(pll, i, cfg[i].msk, cfg[i].val);
  173. ret |= SPRD_PLL_WRITE_CHECK(pll, i, cfg[i].msk,
  174. cfg[i].val);
  175. }
  176. }
  177. if (!ret)
  178. udelay(pll->udelay);
  179. kfree(cfg);
  180. return ret;
  181. }
  182. static unsigned long sprd_pll_recalc_rate(struct clk_hw *hw,
  183. unsigned long parent_rate)
  184. {
  185. struct sprd_pll *pll = hw_to_sprd_pll(hw);
  186. return _sprd_pll_recalc_rate(pll, parent_rate);
  187. }
  188. static int sprd_pll_set_rate(struct clk_hw *hw,
  189. unsigned long rate,
  190. unsigned long parent_rate)
  191. {
  192. struct sprd_pll *pll = hw_to_sprd_pll(hw);
  193. return _sprd_pll_set_rate(pll, rate, parent_rate);
  194. }
  195. static int sprd_pll_clk_prepare(struct clk_hw *hw)
  196. {
  197. struct sprd_pll *pll = hw_to_sprd_pll(hw);
  198. udelay(pll->udelay);
  199. return 0;
  200. }
  201. static long sprd_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  202. unsigned long *prate)
  203. {
  204. return rate;
  205. }
  206. const struct clk_ops sprd_pll_ops = {
  207. .prepare = sprd_pll_clk_prepare,
  208. .recalc_rate = sprd_pll_recalc_rate,
  209. .round_rate = sprd_pll_round_rate,
  210. .set_rate = sprd_pll_set_rate,
  211. };
  212. EXPORT_SYMBOL_GPL(sprd_pll_ops);