clk-cpu.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2014 Lucas Stach <l.stach@pengutronix.de>, Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  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/slab.h>
  14. struct clk_cpu {
  15. struct clk_hw hw;
  16. struct clk *div;
  17. struct clk *mux;
  18. struct clk *pll;
  19. struct clk *step;
  20. };
  21. static inline struct clk_cpu *to_clk_cpu(struct clk_hw *hw)
  22. {
  23. return container_of(hw, struct clk_cpu, hw);
  24. }
  25. static unsigned long clk_cpu_recalc_rate(struct clk_hw *hw,
  26. unsigned long parent_rate)
  27. {
  28. struct clk_cpu *cpu = to_clk_cpu(hw);
  29. return clk_get_rate(cpu->div);
  30. }
  31. static long clk_cpu_round_rate(struct clk_hw *hw, unsigned long rate,
  32. unsigned long *prate)
  33. {
  34. struct clk_cpu *cpu = to_clk_cpu(hw);
  35. return clk_round_rate(cpu->pll, rate);
  36. }
  37. static int clk_cpu_set_rate(struct clk_hw *hw, unsigned long rate,
  38. unsigned long parent_rate)
  39. {
  40. struct clk_cpu *cpu = to_clk_cpu(hw);
  41. int ret;
  42. /* switch to PLL bypass clock */
  43. ret = clk_set_parent(cpu->mux, cpu->step);
  44. if (ret)
  45. return ret;
  46. /* reprogram PLL */
  47. ret = clk_set_rate(cpu->pll, rate);
  48. if (ret) {
  49. clk_set_parent(cpu->mux, cpu->pll);
  50. return ret;
  51. }
  52. /* switch back to PLL clock */
  53. clk_set_parent(cpu->mux, cpu->pll);
  54. /* Ensure the divider is what we expect */
  55. clk_set_rate(cpu->div, rate);
  56. return 0;
  57. }
  58. static const struct clk_ops clk_cpu_ops = {
  59. .recalc_rate = clk_cpu_recalc_rate,
  60. .round_rate = clk_cpu_round_rate,
  61. .set_rate = clk_cpu_set_rate,
  62. };
  63. struct clk *imx_clk_cpu(const char *name, const char *parent_name,
  64. struct clk *div, struct clk *mux, struct clk *pll,
  65. struct clk *step)
  66. {
  67. struct clk_cpu *cpu;
  68. struct clk *clk;
  69. struct clk_init_data init;
  70. cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
  71. if (!cpu)
  72. return ERR_PTR(-ENOMEM);
  73. cpu->div = div;
  74. cpu->mux = mux;
  75. cpu->pll = pll;
  76. cpu->step = step;
  77. init.name = name;
  78. init.ops = &clk_cpu_ops;
  79. init.flags = 0;
  80. init.parent_names = &parent_name;
  81. init.num_parents = 1;
  82. cpu->hw.init = &init;
  83. clk = clk_register(NULL, &cpu->hw);
  84. if (IS_ERR(clk))
  85. kfree(cpu);
  86. return clk;
  87. }