pll.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <chunyan.zhang@spreadtrum.com>
  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. for (i = 1; i < num + 1; i++)
  70. if (rate <= table[i])
  71. break;
  72. return (i == num + 1) ? num : i;
  73. }
  74. static unsigned long _sprd_pll_recalc_rate(const struct sprd_pll *pll,
  75. unsigned long parent_rate)
  76. {
  77. u32 *cfg;
  78. u32 i, mask, regs_num = pll->regs_num;
  79. unsigned long rate, nint, kint = 0;
  80. u64 refin;
  81. u16 k1, k2;
  82. cfg = kcalloc(regs_num, sizeof(*cfg), GFP_KERNEL);
  83. if (!cfg)
  84. return -ENOMEM;
  85. for (i = 0; i < regs_num; i++)
  86. cfg[i] = sprd_pll_read(pll, i);
  87. refin = pll_get_refin(pll);
  88. if (pinternal(pll, cfg, PLL_PREDIV))
  89. refin = refin * 2;
  90. if (pwidth(pll, PLL_POSTDIV) &&
  91. ((pll->fflag == 1 && pinternal(pll, cfg, PLL_POSTDIV)) ||
  92. (!pll->fflag && !pinternal(pll, cfg, PLL_POSTDIV))))
  93. refin = refin / 2;
  94. if (!pinternal(pll, cfg, PLL_DIV_S)) {
  95. rate = refin * pinternal_val(pll, cfg, PLL_N) * CLK_PLL_10M;
  96. } else {
  97. nint = pinternal_val(pll, cfg, PLL_NINT);
  98. if (pinternal(pll, cfg, PLL_SDM_EN))
  99. kint = pinternal_val(pll, cfg, PLL_KINT);
  100. mask = pmask(pll, PLL_KINT);
  101. k1 = pll->k1;
  102. k2 = pll->k2;
  103. rate = DIV_ROUND_CLOSEST_ULL(refin * kint * k1,
  104. ((mask >> __ffs(mask)) + 1)) *
  105. k2 + refin * nint * CLK_PLL_1M;
  106. }
  107. return rate;
  108. }
  109. #define SPRD_PLL_WRITE_CHECK(pll, i, mask, val) \
  110. (((sprd_pll_read(pll, i) & mask) == val) ? 0 : (-EFAULT))
  111. static int _sprd_pll_set_rate(const struct sprd_pll *pll,
  112. unsigned long rate,
  113. unsigned long parent_rate)
  114. {
  115. struct reg_cfg *cfg;
  116. int ret = 0;
  117. u32 mask, shift, width, ibias_val, index;
  118. u32 regs_num = pll->regs_num, i = 0;
  119. unsigned long kint, nint;
  120. u64 tmp, refin, fvco = rate;
  121. cfg = kcalloc(regs_num, sizeof(*cfg), GFP_KERNEL);
  122. if (!cfg)
  123. return -ENOMEM;
  124. refin = pll_get_refin(pll);
  125. mask = pmask(pll, PLL_PREDIV);
  126. index = pindex(pll, PLL_PREDIV);
  127. width = pwidth(pll, PLL_PREDIV);
  128. if (width && (sprd_pll_read(pll, index) & mask))
  129. refin = refin * 2;
  130. mask = pmask(pll, PLL_POSTDIV);
  131. index = pindex(pll, PLL_POSTDIV);
  132. width = pwidth(pll, PLL_POSTDIV);
  133. cfg[index].msk = mask;
  134. if (width && ((pll->fflag == 1 && fvco <= pll->fvco) ||
  135. (pll->fflag == 0 && fvco > pll->fvco)))
  136. cfg[index].val |= mask;
  137. if (width && fvco <= pll->fvco)
  138. fvco = fvco * 2;
  139. mask = pmask(pll, PLL_DIV_S);
  140. index = pindex(pll, PLL_DIV_S);
  141. cfg[index].val |= mask;
  142. cfg[index].msk |= mask;
  143. mask = pmask(pll, PLL_SDM_EN);
  144. index = pindex(pll, PLL_SDM_EN);
  145. cfg[index].val |= mask;
  146. cfg[index].msk |= mask;
  147. nint = do_div(fvco, refin * CLK_PLL_1M);
  148. mask = pmask(pll, PLL_NINT);
  149. index = pindex(pll, PLL_NINT);
  150. shift = pshift(pll, PLL_NINT);
  151. cfg[index].val |= (nint << shift) & mask;
  152. cfg[index].msk |= mask;
  153. mask = pmask(pll, PLL_KINT);
  154. index = pindex(pll, PLL_KINT);
  155. width = pwidth(pll, PLL_KINT);
  156. shift = pshift(pll, PLL_KINT);
  157. tmp = fvco - refin * nint * CLK_PLL_1M;
  158. tmp = do_div(tmp, 10000) * ((mask >> shift) + 1);
  159. kint = DIV_ROUND_CLOSEST_ULL(tmp, refin * 100);
  160. cfg[index].val |= (kint << shift) & mask;
  161. cfg[index].msk |= mask;
  162. ibias_val = pll_get_ibias(fvco, pll->itable);
  163. mask = pmask(pll, PLL_IBIAS);
  164. index = pindex(pll, PLL_IBIAS);
  165. shift = pshift(pll, PLL_IBIAS);
  166. cfg[index].val |= ibias_val << shift & mask;
  167. cfg[index].msk |= mask;
  168. for (i = 0; i < regs_num; i++) {
  169. if (cfg[i].msk) {
  170. sprd_pll_write(pll, i, cfg[i].msk, cfg[i].val);
  171. ret |= SPRD_PLL_WRITE_CHECK(pll, i, cfg[i].msk,
  172. cfg[i].val);
  173. }
  174. }
  175. if (!ret)
  176. udelay(pll->udelay);
  177. return ret;
  178. }
  179. static unsigned long sprd_pll_recalc_rate(struct clk_hw *hw,
  180. unsigned long parent_rate)
  181. {
  182. struct sprd_pll *pll = hw_to_sprd_pll(hw);
  183. return _sprd_pll_recalc_rate(pll, parent_rate);
  184. }
  185. static int sprd_pll_set_rate(struct clk_hw *hw,
  186. unsigned long rate,
  187. unsigned long parent_rate)
  188. {
  189. struct sprd_pll *pll = hw_to_sprd_pll(hw);
  190. return _sprd_pll_set_rate(pll, rate, parent_rate);
  191. }
  192. static int sprd_pll_clk_prepare(struct clk_hw *hw)
  193. {
  194. struct sprd_pll *pll = hw_to_sprd_pll(hw);
  195. udelay(pll->udelay);
  196. return 0;
  197. }
  198. static long sprd_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  199. unsigned long *prate)
  200. {
  201. return rate;
  202. }
  203. const struct clk_ops sprd_pll_ops = {
  204. .prepare = sprd_pll_clk_prepare,
  205. .recalc_rate = sprd_pll_recalc_rate,
  206. .round_rate = sprd_pll_round_rate,
  207. .set_rate = sprd_pll_set_rate,
  208. };
  209. EXPORT_SYMBOL_GPL(sprd_pll_ops);