clk-pllv1.c 2.4 KB

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