clk-prcmu.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * PRCMU clock implementation for ux500 platform.
  3. *
  4. * Copyright (C) 2012 ST-Ericsson SA
  5. * Author: Ulf Hansson <ulf.hansson@linaro.org>
  6. *
  7. * License terms: GNU General Public License (GPL) version 2
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/clk-private.h>
  11. #include <linux/mfd/dbx500-prcmu.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include "clk.h"
  16. #define to_clk_prcmu(_hw) container_of(_hw, struct clk_prcmu, hw)
  17. struct clk_prcmu {
  18. struct clk_hw hw;
  19. u8 cg_sel;
  20. int is_enabled;
  21. };
  22. /* PRCMU clock operations. */
  23. static int clk_prcmu_prepare(struct clk_hw *hw)
  24. {
  25. struct clk_prcmu *clk = to_clk_prcmu(hw);
  26. return prcmu_request_clock(clk->cg_sel, true);
  27. }
  28. static void clk_prcmu_unprepare(struct clk_hw *hw)
  29. {
  30. struct clk_prcmu *clk = to_clk_prcmu(hw);
  31. if (prcmu_request_clock(clk->cg_sel, false))
  32. pr_err("clk_prcmu: %s failed to disable %s.\n", __func__,
  33. hw->init->name);
  34. }
  35. static int clk_prcmu_enable(struct clk_hw *hw)
  36. {
  37. struct clk_prcmu *clk = to_clk_prcmu(hw);
  38. clk->is_enabled = 1;
  39. return 0;
  40. }
  41. static void clk_prcmu_disable(struct clk_hw *hw)
  42. {
  43. struct clk_prcmu *clk = to_clk_prcmu(hw);
  44. clk->is_enabled = 0;
  45. }
  46. static int clk_prcmu_is_enabled(struct clk_hw *hw)
  47. {
  48. struct clk_prcmu *clk = to_clk_prcmu(hw);
  49. return clk->is_enabled;
  50. }
  51. static unsigned long clk_prcmu_recalc_rate(struct clk_hw *hw,
  52. unsigned long parent_rate)
  53. {
  54. struct clk_prcmu *clk = to_clk_prcmu(hw);
  55. return prcmu_clock_rate(clk->cg_sel);
  56. }
  57. static long clk_prcmu_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *parent_rate)
  59. {
  60. struct clk_prcmu *clk = to_clk_prcmu(hw);
  61. return prcmu_round_clock_rate(clk->cg_sel, rate);
  62. }
  63. static int clk_prcmu_set_rate(struct clk_hw *hw, unsigned long rate,
  64. unsigned long parent_rate)
  65. {
  66. struct clk_prcmu *clk = to_clk_prcmu(hw);
  67. return prcmu_set_clock_rate(clk->cg_sel, rate);
  68. }
  69. static int request_ape_opp100(bool enable)
  70. {
  71. static int reqs;
  72. int err = 0;
  73. if (enable) {
  74. if (!reqs)
  75. err = prcmu_qos_add_requirement(PRCMU_QOS_APE_OPP,
  76. "clock", 100);
  77. if (!err)
  78. reqs++;
  79. } else {
  80. reqs--;
  81. if (!reqs)
  82. prcmu_qos_remove_requirement(PRCMU_QOS_APE_OPP,
  83. "clock");
  84. }
  85. return err;
  86. }
  87. static int clk_prcmu_opp_prepare(struct clk_hw *hw)
  88. {
  89. int err;
  90. struct clk_prcmu *clk = to_clk_prcmu(hw);
  91. err = request_ape_opp100(true);
  92. if (err) {
  93. pr_err("clk_prcmu: %s failed to request APE OPP100 for %s.\n",
  94. __func__, hw->init->name);
  95. return err;
  96. }
  97. err = prcmu_request_clock(clk->cg_sel, true);
  98. if (err)
  99. request_ape_opp100(false);
  100. return err;
  101. }
  102. static void clk_prcmu_opp_unprepare(struct clk_hw *hw)
  103. {
  104. struct clk_prcmu *clk = to_clk_prcmu(hw);
  105. if (prcmu_request_clock(clk->cg_sel, false))
  106. goto out_error;
  107. if (request_ape_opp100(false))
  108. goto out_error;
  109. return;
  110. out_error:
  111. pr_err("clk_prcmu: %s failed to disable %s.\n", __func__,
  112. hw->init->name);
  113. }
  114. static struct clk_ops clk_prcmu_scalable_ops = {
  115. .prepare = clk_prcmu_prepare,
  116. .unprepare = clk_prcmu_unprepare,
  117. .enable = clk_prcmu_enable,
  118. .disable = clk_prcmu_disable,
  119. .is_enabled = clk_prcmu_is_enabled,
  120. .recalc_rate = clk_prcmu_recalc_rate,
  121. .round_rate = clk_prcmu_round_rate,
  122. .set_rate = clk_prcmu_set_rate,
  123. };
  124. static struct clk_ops clk_prcmu_gate_ops = {
  125. .prepare = clk_prcmu_prepare,
  126. .unprepare = clk_prcmu_unprepare,
  127. .enable = clk_prcmu_enable,
  128. .disable = clk_prcmu_disable,
  129. .is_enabled = clk_prcmu_is_enabled,
  130. .recalc_rate = clk_prcmu_recalc_rate,
  131. };
  132. static struct clk_ops clk_prcmu_opp_gate_ops = {
  133. .prepare = clk_prcmu_opp_prepare,
  134. .unprepare = clk_prcmu_opp_unprepare,
  135. .enable = clk_prcmu_enable,
  136. .disable = clk_prcmu_disable,
  137. .is_enabled = clk_prcmu_is_enabled,
  138. .recalc_rate = clk_prcmu_recalc_rate,
  139. };
  140. static struct clk *clk_reg_prcmu(const char *name,
  141. const char *parent_name,
  142. u8 cg_sel,
  143. unsigned long rate,
  144. unsigned long flags,
  145. struct clk_ops *clk_prcmu_ops)
  146. {
  147. struct clk_prcmu *clk;
  148. struct clk_init_data clk_prcmu_init;
  149. struct clk *clk_reg;
  150. if (!name) {
  151. pr_err("clk_prcmu: %s invalid arguments passed\n", __func__);
  152. return ERR_PTR(-EINVAL);
  153. }
  154. clk = kzalloc(sizeof(struct clk_prcmu), GFP_KERNEL);
  155. if (!clk) {
  156. pr_err("clk_prcmu: %s could not allocate clk\n", __func__);
  157. return ERR_PTR(-ENOMEM);
  158. }
  159. clk->cg_sel = cg_sel;
  160. clk->is_enabled = 1;
  161. /* "rate" can be used for changing the initial frequency */
  162. if (rate)
  163. prcmu_set_clock_rate(cg_sel, rate);
  164. clk_prcmu_init.name = name;
  165. clk_prcmu_init.ops = clk_prcmu_ops;
  166. clk_prcmu_init.flags = flags;
  167. clk_prcmu_init.parent_names = (parent_name ? &parent_name : NULL);
  168. clk_prcmu_init.num_parents = (parent_name ? 1 : 0);
  169. clk->hw.init = &clk_prcmu_init;
  170. clk_reg = clk_register(NULL, &clk->hw);
  171. if (IS_ERR_OR_NULL(clk_reg))
  172. goto free_clk;
  173. return clk_reg;
  174. free_clk:
  175. kfree(clk);
  176. pr_err("clk_prcmu: %s failed to register clk\n", __func__);
  177. return ERR_PTR(-ENOMEM);
  178. }
  179. struct clk *clk_reg_prcmu_scalable(const char *name,
  180. const char *parent_name,
  181. u8 cg_sel,
  182. unsigned long rate,
  183. unsigned long flags)
  184. {
  185. return clk_reg_prcmu(name, parent_name, cg_sel, rate, flags,
  186. &clk_prcmu_scalable_ops);
  187. }
  188. struct clk *clk_reg_prcmu_gate(const char *name,
  189. const char *parent_name,
  190. u8 cg_sel,
  191. unsigned long flags)
  192. {
  193. return clk_reg_prcmu(name, parent_name, cg_sel, 0, flags,
  194. &clk_prcmu_gate_ops);
  195. }
  196. struct clk *clk_reg_prcmu_opp_gate(const char *name,
  197. const char *parent_name,
  198. u8 cg_sel,
  199. unsigned long flags)
  200. {
  201. return clk_reg_prcmu(name, parent_name, cg_sel, 0, flags,
  202. &clk_prcmu_opp_gate_ops);
  203. }