clk-pll.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include "clk.h"
  17. /**
  18. * struct clk_pll - mxs pll clock
  19. * @hw: clk_hw for the pll
  20. * @base: base address of the pll
  21. * @power: the shift of power bit
  22. * @rate: the clock rate of the pll
  23. *
  24. * The mxs pll is a fixed rate clock with power and gate control,
  25. * and the shift of gate bit is always 31.
  26. */
  27. struct clk_pll {
  28. struct clk_hw hw;
  29. void __iomem *base;
  30. u8 power;
  31. unsigned long rate;
  32. };
  33. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  34. static int clk_pll_prepare(struct clk_hw *hw)
  35. {
  36. struct clk_pll *pll = to_clk_pll(hw);
  37. writel_relaxed(1 << pll->power, pll->base + SET);
  38. udelay(10);
  39. return 0;
  40. }
  41. static void clk_pll_unprepare(struct clk_hw *hw)
  42. {
  43. struct clk_pll *pll = to_clk_pll(hw);
  44. writel_relaxed(1 << pll->power, pll->base + CLR);
  45. }
  46. static int clk_pll_enable(struct clk_hw *hw)
  47. {
  48. struct clk_pll *pll = to_clk_pll(hw);
  49. writel_relaxed(1 << 31, pll->base + CLR);
  50. return 0;
  51. }
  52. static void clk_pll_disable(struct clk_hw *hw)
  53. {
  54. struct clk_pll *pll = to_clk_pll(hw);
  55. writel_relaxed(1 << 31, pll->base + SET);
  56. }
  57. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  58. unsigned long parent_rate)
  59. {
  60. struct clk_pll *pll = to_clk_pll(hw);
  61. return pll->rate;
  62. }
  63. static const struct clk_ops clk_pll_ops = {
  64. .prepare = clk_pll_prepare,
  65. .unprepare = clk_pll_unprepare,
  66. .enable = clk_pll_enable,
  67. .disable = clk_pll_disable,
  68. .recalc_rate = clk_pll_recalc_rate,
  69. };
  70. struct clk *mxs_clk_pll(const char *name, const char *parent_name,
  71. void __iomem *base, u8 power, unsigned long rate)
  72. {
  73. struct clk_pll *pll;
  74. struct clk *clk;
  75. struct clk_init_data init;
  76. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  77. if (!pll)
  78. return ERR_PTR(-ENOMEM);
  79. init.name = name;
  80. init.ops = &clk_pll_ops;
  81. init.flags = 0;
  82. init.parent_names = (parent_name ? &parent_name: NULL);
  83. init.num_parents = (parent_name ? 1 : 0);
  84. pll->base = base;
  85. pll->rate = rate;
  86. pll->power = power;
  87. pll->hw.init = &init;
  88. clk = clk_register(NULL, &pll->hw);
  89. if (IS_ERR(clk))
  90. kfree(pll);
  91. return clk;
  92. }