clk-pllv1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <linux/clk-provider.h>
  2. #include <linux/io.h>
  3. #include <linux/slab.h>
  4. #include <linux/kernel.h>
  5. #include <linux/err.h>
  6. #include "clk.h"
  7. /**
  8. * pll v1
  9. *
  10. * @clk_hw clock source
  11. * @parent the parent clock name
  12. * @base base address of pll registers
  13. *
  14. * PLL clock version 1, found on i.MX1/21/25/27/31/35
  15. */
  16. #define MFN_BITS (10)
  17. #define MFN_SIGN (BIT(MFN_BITS - 1))
  18. #define MFN_MASK (MFN_SIGN - 1)
  19. struct clk_pllv1 {
  20. struct clk_hw hw;
  21. void __iomem *base;
  22. enum imx_pllv1_type type;
  23. };
  24. #define to_clk_pllv1(clk) (container_of(clk, struct clk_pllv1, clk))
  25. static inline bool is_imx1_pllv1(struct clk_pllv1 *pll)
  26. {
  27. return pll->type == IMX_PLLV1_IMX1;
  28. }
  29. static inline bool is_imx21_pllv1(struct clk_pllv1 *pll)
  30. {
  31. return pll->type == IMX_PLLV1_IMX21;
  32. }
  33. static inline bool is_imx27_pllv1(struct clk_pllv1 *pll)
  34. {
  35. return pll->type == IMX_PLLV1_IMX27;
  36. }
  37. static inline bool mfn_is_negative(struct clk_pllv1 *pll, unsigned int mfn)
  38. {
  39. return !is_imx1_pllv1(pll) && !is_imx21_pllv1(pll) && (mfn & MFN_SIGN);
  40. }
  41. static unsigned long clk_pllv1_recalc_rate(struct clk_hw *hw,
  42. unsigned long parent_rate)
  43. {
  44. struct clk_pllv1 *pll = to_clk_pllv1(hw);
  45. unsigned long long ull;
  46. int mfn_abs;
  47. unsigned int mfi, mfn, mfd, pd;
  48. u32 reg;
  49. unsigned long rate;
  50. reg = readl(pll->base);
  51. /*
  52. * Get the resulting clock rate from a PLL register value and the input
  53. * frequency. PLLs with this register layout can be found on i.MX1,
  54. * i.MX21, i.MX27 and i,MX31
  55. *
  56. * mfi + mfn / (mfd + 1)
  57. * f = 2 * f_ref * --------------------
  58. * pd + 1
  59. */
  60. mfi = (reg >> 10) & 0xf;
  61. mfn = reg & 0x3ff;
  62. mfd = (reg >> 16) & 0x3ff;
  63. pd = (reg >> 26) & 0xf;
  64. mfi = mfi <= 5 ? 5 : mfi;
  65. mfn_abs = mfn;
  66. /*
  67. * On all i.MXs except i.MX1 and i.MX21 mfn is a 10bit
  68. * 2's complements number.
  69. * On i.MX27 the bit 9 is the sign bit.
  70. */
  71. if (mfn_is_negative(pll, mfn)) {
  72. if (is_imx27_pllv1(pll))
  73. mfn_abs = mfn & MFN_MASK;
  74. else
  75. mfn_abs = BIT(MFN_BITS) - mfn;
  76. }
  77. rate = parent_rate * 2;
  78. rate /= pd + 1;
  79. ull = (unsigned long long)rate * mfn_abs;
  80. do_div(ull, mfd + 1);
  81. if (mfn_is_negative(pll, mfn))
  82. ull = (rate * mfi) - ull;
  83. else
  84. ull = (rate * mfi) + ull;
  85. return ull;
  86. }
  87. static struct clk_ops clk_pllv1_ops = {
  88. .recalc_rate = clk_pllv1_recalc_rate,
  89. };
  90. struct clk *imx_clk_pllv1(enum imx_pllv1_type type, const char *name,
  91. const char *parent, void __iomem *base)
  92. {
  93. struct clk_pllv1 *pll;
  94. struct clk *clk;
  95. struct clk_init_data init;
  96. pll = kmalloc(sizeof(*pll), GFP_KERNEL);
  97. if (!pll)
  98. return ERR_PTR(-ENOMEM);
  99. pll->base = base;
  100. pll->type = type;
  101. init.name = name;
  102. init.ops = &clk_pllv1_ops;
  103. init.flags = 0;
  104. init.parent_names = &parent;
  105. init.num_parents = 1;
  106. pll->hw.init = &init;
  107. clk = clk_register(NULL, &pll->hw);
  108. if (IS_ERR(clk))
  109. kfree(pll);
  110. return clk;
  111. }