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