owl-composite.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // OWL composite clock driver
  4. //
  5. // Copyright (c) 2014 Actions Semi Inc.
  6. // Author: David Liu <liuwei@actions-semi.com>
  7. //
  8. // Copyright (c) 2018 Linaro Ltd.
  9. // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  10. #include <linux/clk-provider.h>
  11. #include <linux/regmap.h>
  12. #include "owl-composite.h"
  13. static u8 owl_comp_get_parent(struct clk_hw *hw)
  14. {
  15. struct owl_composite *comp = hw_to_owl_comp(hw);
  16. return owl_mux_helper_get_parent(&comp->common, &comp->mux_hw);
  17. }
  18. static int owl_comp_set_parent(struct clk_hw *hw, u8 index)
  19. {
  20. struct owl_composite *comp = hw_to_owl_comp(hw);
  21. return owl_mux_helper_set_parent(&comp->common, &comp->mux_hw, index);
  22. }
  23. static void owl_comp_disable(struct clk_hw *hw)
  24. {
  25. struct owl_composite *comp = hw_to_owl_comp(hw);
  26. struct owl_clk_common *common = &comp->common;
  27. owl_gate_set(common, &comp->gate_hw, false);
  28. }
  29. static int owl_comp_enable(struct clk_hw *hw)
  30. {
  31. struct owl_composite *comp = hw_to_owl_comp(hw);
  32. struct owl_clk_common *common = &comp->common;
  33. owl_gate_set(common, &comp->gate_hw, true);
  34. return 0;
  35. }
  36. static int owl_comp_is_enabled(struct clk_hw *hw)
  37. {
  38. struct owl_composite *comp = hw_to_owl_comp(hw);
  39. struct owl_clk_common *common = &comp->common;
  40. return owl_gate_clk_is_enabled(common, &comp->gate_hw);
  41. }
  42. static int owl_comp_div_determine_rate(struct clk_hw *hw,
  43. struct clk_rate_request *req)
  44. {
  45. struct owl_composite *comp = hw_to_owl_comp(hw);
  46. long rate;
  47. rate = owl_divider_helper_round_rate(&comp->common, &comp->rate.div_hw,
  48. req->rate, &req->best_parent_rate);
  49. if (rate < 0)
  50. return rate;
  51. req->rate = rate;
  52. return 0;
  53. }
  54. static unsigned long owl_comp_div_recalc_rate(struct clk_hw *hw,
  55. unsigned long parent_rate)
  56. {
  57. struct owl_composite *comp = hw_to_owl_comp(hw);
  58. return owl_divider_helper_recalc_rate(&comp->common, &comp->rate.div_hw,
  59. parent_rate);
  60. }
  61. static int owl_comp_div_set_rate(struct clk_hw *hw, unsigned long rate,
  62. unsigned long parent_rate)
  63. {
  64. struct owl_composite *comp = hw_to_owl_comp(hw);
  65. return owl_divider_helper_set_rate(&comp->common, &comp->rate.div_hw,
  66. rate, parent_rate);
  67. }
  68. static int owl_comp_fact_determine_rate(struct clk_hw *hw,
  69. struct clk_rate_request *req)
  70. {
  71. struct owl_composite *comp = hw_to_owl_comp(hw);
  72. long rate;
  73. rate = owl_factor_helper_round_rate(&comp->common,
  74. &comp->rate.factor_hw,
  75. req->rate, &req->best_parent_rate);
  76. if (rate < 0)
  77. return rate;
  78. req->rate = rate;
  79. return 0;
  80. }
  81. static unsigned long owl_comp_fact_recalc_rate(struct clk_hw *hw,
  82. unsigned long parent_rate)
  83. {
  84. struct owl_composite *comp = hw_to_owl_comp(hw);
  85. return owl_factor_helper_recalc_rate(&comp->common,
  86. &comp->rate.factor_hw,
  87. parent_rate);
  88. }
  89. static int owl_comp_fact_set_rate(struct clk_hw *hw, unsigned long rate,
  90. unsigned long parent_rate)
  91. {
  92. struct owl_composite *comp = hw_to_owl_comp(hw);
  93. return owl_factor_helper_set_rate(&comp->common,
  94. &comp->rate.factor_hw,
  95. rate, parent_rate);
  96. }
  97. static long owl_comp_fix_fact_round_rate(struct clk_hw *hw, unsigned long rate,
  98. unsigned long *parent_rate)
  99. {
  100. struct owl_composite *comp = hw_to_owl_comp(hw);
  101. struct clk_fixed_factor *fix_fact_hw = &comp->rate.fix_fact_hw;
  102. return comp->fix_fact_ops->round_rate(&fix_fact_hw->hw, rate, parent_rate);
  103. }
  104. static unsigned long owl_comp_fix_fact_recalc_rate(struct clk_hw *hw,
  105. unsigned long parent_rate)
  106. {
  107. struct owl_composite *comp = hw_to_owl_comp(hw);
  108. struct clk_fixed_factor *fix_fact_hw = &comp->rate.fix_fact_hw;
  109. return comp->fix_fact_ops->recalc_rate(&fix_fact_hw->hw, parent_rate);
  110. }
  111. static int owl_comp_fix_fact_set_rate(struct clk_hw *hw, unsigned long rate,
  112. unsigned long parent_rate)
  113. {
  114. /*
  115. * We must report success but we can do so unconditionally because
  116. * owl_comp_fix_fact_round_rate returns values that ensure this call is
  117. * a nop.
  118. */
  119. return 0;
  120. }
  121. const struct clk_ops owl_comp_div_ops = {
  122. /* mux_ops */
  123. .get_parent = owl_comp_get_parent,
  124. .set_parent = owl_comp_set_parent,
  125. /* gate_ops */
  126. .disable = owl_comp_disable,
  127. .enable = owl_comp_enable,
  128. .is_enabled = owl_comp_is_enabled,
  129. /* div_ops */
  130. .determine_rate = owl_comp_div_determine_rate,
  131. .recalc_rate = owl_comp_div_recalc_rate,
  132. .set_rate = owl_comp_div_set_rate,
  133. };
  134. const struct clk_ops owl_comp_fact_ops = {
  135. /* mux_ops */
  136. .get_parent = owl_comp_get_parent,
  137. .set_parent = owl_comp_set_parent,
  138. /* gate_ops */
  139. .disable = owl_comp_disable,
  140. .enable = owl_comp_enable,
  141. .is_enabled = owl_comp_is_enabled,
  142. /* fact_ops */
  143. .determine_rate = owl_comp_fact_determine_rate,
  144. .recalc_rate = owl_comp_fact_recalc_rate,
  145. .set_rate = owl_comp_fact_set_rate,
  146. };
  147. const struct clk_ops owl_comp_fix_fact_ops = {
  148. /* gate_ops */
  149. .disable = owl_comp_disable,
  150. .enable = owl_comp_enable,
  151. .is_enabled = owl_comp_is_enabled,
  152. /* fix_fact_ops */
  153. .round_rate = owl_comp_fix_fact_round_rate,
  154. .recalc_rate = owl_comp_fix_fact_recalc_rate,
  155. .set_rate = owl_comp_fix_fact_set_rate,
  156. };
  157. const struct clk_ops owl_comp_pass_ops = {
  158. /* mux_ops */
  159. .determine_rate = clk_hw_determine_rate_no_reparent,
  160. .get_parent = owl_comp_get_parent,
  161. .set_parent = owl_comp_set_parent,
  162. /* gate_ops */
  163. .disable = owl_comp_disable,
  164. .enable = owl_comp_enable,
  165. .is_enabled = owl_comp_is_enabled,
  166. };