owl-composite.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. struct owl_divider_hw *div = &comp->rate.div_hw;
  47. return divider_determine_rate(&comp->common.hw, req, div->table,
  48. div->width, div->div_flags);
  49. }
  50. static unsigned long owl_comp_div_recalc_rate(struct clk_hw *hw,
  51. unsigned long parent_rate)
  52. {
  53. struct owl_composite *comp = hw_to_owl_comp(hw);
  54. return owl_divider_helper_recalc_rate(&comp->common, &comp->rate.div_hw,
  55. parent_rate);
  56. }
  57. static int owl_comp_div_set_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long parent_rate)
  59. {
  60. struct owl_composite *comp = hw_to_owl_comp(hw);
  61. return owl_divider_helper_set_rate(&comp->common, &comp->rate.div_hw,
  62. rate, parent_rate);
  63. }
  64. static int owl_comp_fact_determine_rate(struct clk_hw *hw,
  65. struct clk_rate_request *req)
  66. {
  67. struct owl_composite *comp = hw_to_owl_comp(hw);
  68. long rate;
  69. rate = owl_factor_helper_round_rate(&comp->common,
  70. &comp->rate.factor_hw,
  71. req->rate, &req->best_parent_rate);
  72. if (rate < 0)
  73. return rate;
  74. req->rate = rate;
  75. return 0;
  76. }
  77. static unsigned long owl_comp_fact_recalc_rate(struct clk_hw *hw,
  78. unsigned long parent_rate)
  79. {
  80. struct owl_composite *comp = hw_to_owl_comp(hw);
  81. return owl_factor_helper_recalc_rate(&comp->common,
  82. &comp->rate.factor_hw,
  83. parent_rate);
  84. }
  85. static int owl_comp_fact_set_rate(struct clk_hw *hw, unsigned long rate,
  86. unsigned long parent_rate)
  87. {
  88. struct owl_composite *comp = hw_to_owl_comp(hw);
  89. return owl_factor_helper_set_rate(&comp->common,
  90. &comp->rate.factor_hw,
  91. rate, parent_rate);
  92. }
  93. static int owl_comp_fix_fact_determine_rate(struct clk_hw *hw,
  94. struct clk_rate_request *req)
  95. {
  96. struct owl_composite *comp = hw_to_owl_comp(hw);
  97. struct clk_fixed_factor *fix_fact_hw = &comp->rate.fix_fact_hw;
  98. return comp->fix_fact_ops->determine_rate(&fix_fact_hw->hw, req);
  99. }
  100. static unsigned long owl_comp_fix_fact_recalc_rate(struct clk_hw *hw,
  101. unsigned long parent_rate)
  102. {
  103. struct owl_composite *comp = hw_to_owl_comp(hw);
  104. struct clk_fixed_factor *fix_fact_hw = &comp->rate.fix_fact_hw;
  105. return comp->fix_fact_ops->recalc_rate(&fix_fact_hw->hw, parent_rate);
  106. }
  107. static int owl_comp_fix_fact_set_rate(struct clk_hw *hw, unsigned long rate,
  108. unsigned long parent_rate)
  109. {
  110. /*
  111. * We must report success but we can do so unconditionally because
  112. * owl_comp_fix_fact_round_rate returns values that ensure this call is
  113. * a nop.
  114. */
  115. return 0;
  116. }
  117. const struct clk_ops owl_comp_div_ops = {
  118. /* mux_ops */
  119. .get_parent = owl_comp_get_parent,
  120. .set_parent = owl_comp_set_parent,
  121. /* gate_ops */
  122. .disable = owl_comp_disable,
  123. .enable = owl_comp_enable,
  124. .is_enabled = owl_comp_is_enabled,
  125. /* div_ops */
  126. .determine_rate = owl_comp_div_determine_rate,
  127. .recalc_rate = owl_comp_div_recalc_rate,
  128. .set_rate = owl_comp_div_set_rate,
  129. };
  130. const struct clk_ops owl_comp_fact_ops = {
  131. /* mux_ops */
  132. .get_parent = owl_comp_get_parent,
  133. .set_parent = owl_comp_set_parent,
  134. /* gate_ops */
  135. .disable = owl_comp_disable,
  136. .enable = owl_comp_enable,
  137. .is_enabled = owl_comp_is_enabled,
  138. /* fact_ops */
  139. .determine_rate = owl_comp_fact_determine_rate,
  140. .recalc_rate = owl_comp_fact_recalc_rate,
  141. .set_rate = owl_comp_fact_set_rate,
  142. };
  143. const struct clk_ops owl_comp_fix_fact_ops = {
  144. /* gate_ops */
  145. .disable = owl_comp_disable,
  146. .enable = owl_comp_enable,
  147. .is_enabled = owl_comp_is_enabled,
  148. /* fix_fact_ops */
  149. .determine_rate = owl_comp_fix_fact_determine_rate,
  150. .recalc_rate = owl_comp_fix_fact_recalc_rate,
  151. .set_rate = owl_comp_fix_fact_set_rate,
  152. };
  153. const struct clk_ops owl_comp_pass_ops = {
  154. /* mux_ops */
  155. .determine_rate = clk_hw_determine_rate_no_reparent,
  156. .get_parent = owl_comp_get_parent,
  157. .set_parent = owl_comp_set_parent,
  158. /* gate_ops */
  159. .disable = owl_comp_disable,
  160. .enable = owl_comp_enable,
  161. .is_enabled = owl_comp_is_enabled,
  162. };