clk-pll.c 2.5 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.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include "clk.h"
  18. /**
  19. * struct clk_pll - mxs pll clock
  20. * @hw: clk_hw for the pll
  21. * @base: base address of the pll
  22. * @power: the shift of power bit
  23. * @rate: the clock rate of the pll
  24. *
  25. * The mxs pll is a fixed rate clock with power and gate control,
  26. * and the shift of gate bit is always 31.
  27. */
  28. struct clk_pll {
  29. struct clk_hw hw;
  30. void __iomem *base;
  31. u8 power;
  32. unsigned long rate;
  33. };
  34. #define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
  35. static int clk_pll_prepare(struct clk_hw *hw)
  36. {
  37. struct clk_pll *pll = to_clk_pll(hw);
  38. writel_relaxed(1 << pll->power, pll->base + SET);
  39. udelay(10);
  40. return 0;
  41. }
  42. static void clk_pll_unprepare(struct clk_hw *hw)
  43. {
  44. struct clk_pll *pll = to_clk_pll(hw);
  45. writel_relaxed(1 << pll->power, pll->base + CLR);
  46. }
  47. static int clk_pll_enable(struct clk_hw *hw)
  48. {
  49. struct clk_pll *pll = to_clk_pll(hw);
  50. writel_relaxed(1 << 31, pll->base + CLR);
  51. return 0;
  52. }
  53. static void clk_pll_disable(struct clk_hw *hw)
  54. {
  55. struct clk_pll *pll = to_clk_pll(hw);
  56. writel_relaxed(1 << 31, pll->base + SET);
  57. }
  58. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  59. unsigned long parent_rate)
  60. {
  61. struct clk_pll *pll = to_clk_pll(hw);
  62. return pll->rate;
  63. }
  64. static const struct clk_ops clk_pll_ops = {
  65. .prepare = clk_pll_prepare,
  66. .unprepare = clk_pll_unprepare,
  67. .enable = clk_pll_enable,
  68. .disable = clk_pll_disable,
  69. .recalc_rate = clk_pll_recalc_rate,
  70. };
  71. struct clk *mxs_clk_pll(const char *name, const char *parent_name,
  72. void __iomem *base, u8 power, unsigned long rate)
  73. {
  74. struct clk_pll *pll;
  75. struct clk *clk;
  76. struct clk_init_data init;
  77. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  78. if (!pll)
  79. return ERR_PTR(-ENOMEM);
  80. init.name = name;
  81. init.ops = &clk_pll_ops;
  82. init.flags = 0;
  83. init.parent_names = (parent_name ? &parent_name: NULL);
  84. init.num_parents = (parent_name ? 1 : 0);
  85. pll->base = base;
  86. pll->rate = rate;
  87. pll->power = power;
  88. pll->hw.init = &init;
  89. clk = clk_register(NULL, &pll->hw);
  90. if (IS_ERR(clk))
  91. kfree(pll);
  92. return clk;
  93. }