clk-pll.c 2.2 KB

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