clk-pllv1.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. struct clk_pllv1 {
  20. struct clk_hw hw;
  21. void __iomem *base;
  22. };
  23. #define to_clk_pllv1(clk) (container_of(clk, struct clk_pllv1, clk))
  24. static unsigned long clk_pllv1_recalc_rate(struct clk_hw *hw,
  25. unsigned long parent_rate)
  26. {
  27. struct clk_pllv1 *pll = to_clk_pllv1(hw);
  28. long long ll;
  29. int mfn_abs;
  30. unsigned int mfi, mfn, mfd, pd;
  31. u32 reg;
  32. unsigned long rate;
  33. reg = readl(pll->base);
  34. /*
  35. * Get the resulting clock rate from a PLL register value and the input
  36. * frequency. PLLs with this register layout can be found on i.MX1,
  37. * i.MX21, i.MX27 and i,MX31
  38. *
  39. * mfi + mfn / (mfd + 1)
  40. * f = 2 * f_ref * --------------------
  41. * pd + 1
  42. */
  43. mfi = (reg >> 10) & 0xf;
  44. mfn = reg & 0x3ff;
  45. mfd = (reg >> 16) & 0x3ff;
  46. pd = (reg >> 26) & 0xf;
  47. mfi = mfi <= 5 ? 5 : mfi;
  48. mfn_abs = mfn;
  49. /*
  50. * On all i.MXs except i.MX1 and i.MX21 mfn is a 10bit
  51. * 2's complements number
  52. */
  53. if (!cpu_is_mx1() && !cpu_is_mx21() && mfn >= 0x200)
  54. mfn_abs = 0x400 - mfn;
  55. rate = parent_rate * 2;
  56. rate /= pd + 1;
  57. ll = (unsigned long long)rate * mfn_abs;
  58. do_div(ll, mfd + 1);
  59. if (!cpu_is_mx1() && !cpu_is_mx21() && mfn >= 0x200)
  60. ll = -ll;
  61. ll = (rate * mfi) + ll;
  62. return ll;
  63. }
  64. struct clk_ops clk_pllv1_ops = {
  65. .recalc_rate = clk_pllv1_recalc_rate,
  66. };
  67. struct clk *imx_clk_pllv1(const char *name, const char *parent,
  68. void __iomem *base)
  69. {
  70. struct clk_pllv1 *pll;
  71. struct clk *clk;
  72. struct clk_init_data init;
  73. pll = kmalloc(sizeof(*pll), GFP_KERNEL);
  74. if (!pll)
  75. return ERR_PTR(-ENOMEM);
  76. pll->base = base;
  77. init.name = name;
  78. init.ops = &clk_pllv1_ops;
  79. init.flags = 0;
  80. init.parent_names = &parent;
  81. init.num_parents = 1;
  82. pll->hw.init = &init;
  83. clk = clk_register(NULL, &pll->hw);
  84. if (IS_ERR(clk))
  85. kfree(pll);
  86. return clk;
  87. }