clk-pllv1.c 2.8 KB

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