clk-composite.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2013 NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
  21. static u8 clk_composite_get_parent(struct clk_hw *hw)
  22. {
  23. struct clk_composite *composite = to_clk_composite(hw);
  24. const struct clk_ops *mux_ops = composite->mux_ops;
  25. struct clk_hw *mux_hw = composite->mux_hw;
  26. mux_hw->clk = hw->clk;
  27. return mux_ops->get_parent(mux_hw);
  28. }
  29. static int clk_composite_set_parent(struct clk_hw *hw, u8 index)
  30. {
  31. struct clk_composite *composite = to_clk_composite(hw);
  32. const struct clk_ops *mux_ops = composite->mux_ops;
  33. struct clk_hw *mux_hw = composite->mux_hw;
  34. mux_hw->clk = hw->clk;
  35. return mux_ops->set_parent(mux_hw, index);
  36. }
  37. static unsigned long clk_composite_recalc_rate(struct clk_hw *hw,
  38. unsigned long parent_rate)
  39. {
  40. struct clk_composite *composite = to_clk_composite(hw);
  41. const struct clk_ops *rate_ops = composite->rate_ops;
  42. struct clk_hw *rate_hw = composite->rate_hw;
  43. rate_hw->clk = hw->clk;
  44. return rate_ops->recalc_rate(rate_hw, parent_rate);
  45. }
  46. static long clk_composite_round_rate(struct clk_hw *hw, unsigned long rate,
  47. unsigned long *prate)
  48. {
  49. struct clk_composite *composite = to_clk_composite(hw);
  50. const struct clk_ops *rate_ops = composite->rate_ops;
  51. struct clk_hw *rate_hw = composite->rate_hw;
  52. rate_hw->clk = hw->clk;
  53. return rate_ops->round_rate(rate_hw, rate, prate);
  54. }
  55. static int clk_composite_set_rate(struct clk_hw *hw, unsigned long rate,
  56. unsigned long parent_rate)
  57. {
  58. struct clk_composite *composite = to_clk_composite(hw);
  59. const struct clk_ops *rate_ops = composite->rate_ops;
  60. struct clk_hw *rate_hw = composite->rate_hw;
  61. rate_hw->clk = hw->clk;
  62. return rate_ops->set_rate(rate_hw, rate, parent_rate);
  63. }
  64. static int clk_composite_is_enabled(struct clk_hw *hw)
  65. {
  66. struct clk_composite *composite = to_clk_composite(hw);
  67. const struct clk_ops *gate_ops = composite->gate_ops;
  68. struct clk_hw *gate_hw = composite->gate_hw;
  69. gate_hw->clk = hw->clk;
  70. return gate_ops->is_enabled(gate_hw);
  71. }
  72. static int clk_composite_enable(struct clk_hw *hw)
  73. {
  74. struct clk_composite *composite = to_clk_composite(hw);
  75. const struct clk_ops *gate_ops = composite->gate_ops;
  76. struct clk_hw *gate_hw = composite->gate_hw;
  77. gate_hw->clk = hw->clk;
  78. return gate_ops->enable(gate_hw);
  79. }
  80. static void clk_composite_disable(struct clk_hw *hw)
  81. {
  82. struct clk_composite *composite = to_clk_composite(hw);
  83. const struct clk_ops *gate_ops = composite->gate_ops;
  84. struct clk_hw *gate_hw = composite->gate_hw;
  85. gate_hw->clk = hw->clk;
  86. gate_ops->disable(gate_hw);
  87. }
  88. struct clk *clk_register_composite(struct device *dev, const char *name,
  89. const char **parent_names, int num_parents,
  90. struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
  91. struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
  92. struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
  93. unsigned long flags)
  94. {
  95. struct clk *clk;
  96. struct clk_init_data init;
  97. struct clk_composite *composite;
  98. struct clk_ops *clk_composite_ops;
  99. composite = kzalloc(sizeof(*composite), GFP_KERNEL);
  100. if (!composite) {
  101. pr_err("%s: could not allocate composite clk\n", __func__);
  102. return ERR_PTR(-ENOMEM);
  103. }
  104. init.name = name;
  105. init.flags = flags | CLK_IS_BASIC;
  106. init.parent_names = parent_names;
  107. init.num_parents = num_parents;
  108. clk_composite_ops = &composite->ops;
  109. if (mux_hw && mux_ops) {
  110. if (!mux_ops->get_parent || !mux_ops->set_parent) {
  111. clk = ERR_PTR(-EINVAL);
  112. goto err;
  113. }
  114. composite->mux_hw = mux_hw;
  115. composite->mux_ops = mux_ops;
  116. clk_composite_ops->get_parent = clk_composite_get_parent;
  117. clk_composite_ops->set_parent = clk_composite_set_parent;
  118. }
  119. if (rate_hw && rate_ops) {
  120. if (!rate_ops->recalc_rate) {
  121. clk = ERR_PTR(-EINVAL);
  122. goto err;
  123. }
  124. /* .round_rate is a prerequisite for .set_rate */
  125. if (rate_ops->round_rate) {
  126. clk_composite_ops->round_rate = clk_composite_round_rate;
  127. if (rate_ops->set_rate) {
  128. clk_composite_ops->set_rate = clk_composite_set_rate;
  129. }
  130. } else {
  131. WARN(rate_ops->set_rate,
  132. "%s: missing round_rate op is required\n",
  133. __func__);
  134. }
  135. composite->rate_hw = rate_hw;
  136. composite->rate_ops = rate_ops;
  137. clk_composite_ops->recalc_rate = clk_composite_recalc_rate;
  138. }
  139. if (gate_hw && gate_ops) {
  140. if (!gate_ops->is_enabled || !gate_ops->enable ||
  141. !gate_ops->disable) {
  142. clk = ERR_PTR(-EINVAL);
  143. goto err;
  144. }
  145. composite->gate_hw = gate_hw;
  146. composite->gate_ops = gate_ops;
  147. clk_composite_ops->is_enabled = clk_composite_is_enabled;
  148. clk_composite_ops->enable = clk_composite_enable;
  149. clk_composite_ops->disable = clk_composite_disable;
  150. }
  151. init.ops = clk_composite_ops;
  152. composite->hw.init = &init;
  153. clk = clk_register(dev, &composite->hw);
  154. if (IS_ERR(clk))
  155. goto err;
  156. if (composite->mux_hw)
  157. composite->mux_hw->clk = clk;
  158. if (composite->rate_hw)
  159. composite->rate_hw->clk = clk;
  160. if (composite->gate_hw)
  161. composite->gate_hw->clk = clk;
  162. return clk;
  163. err:
  164. kfree(composite);
  165. return clk;
  166. }