clk-prcmu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_rate_ops = {
  133. .is_enabled = clk_prcmu_is_enabled,
  134. .recalc_rate = clk_prcmu_recalc_rate,
  135. };
  136. static struct clk_ops clk_prcmu_opp_gate_ops = {
  137. .prepare = clk_prcmu_opp_prepare,
  138. .unprepare = clk_prcmu_opp_unprepare,
  139. .enable = clk_prcmu_enable,
  140. .disable = clk_prcmu_disable,
  141. .is_enabled = clk_prcmu_is_enabled,
  142. .recalc_rate = clk_prcmu_recalc_rate,
  143. };
  144. static struct clk *clk_reg_prcmu(const char *name,
  145. const char *parent_name,
  146. u8 cg_sel,
  147. unsigned long rate,
  148. unsigned long flags,
  149. struct clk_ops *clk_prcmu_ops)
  150. {
  151. struct clk_prcmu *clk;
  152. struct clk_init_data clk_prcmu_init;
  153. struct clk *clk_reg;
  154. if (!name) {
  155. pr_err("clk_prcmu: %s invalid arguments passed\n", __func__);
  156. return ERR_PTR(-EINVAL);
  157. }
  158. clk = kzalloc(sizeof(struct clk_prcmu), GFP_KERNEL);
  159. if (!clk) {
  160. pr_err("clk_prcmu: %s could not allocate clk\n", __func__);
  161. return ERR_PTR(-ENOMEM);
  162. }
  163. clk->cg_sel = cg_sel;
  164. clk->is_enabled = 1;
  165. /* "rate" can be used for changing the initial frequency */
  166. if (rate)
  167. prcmu_set_clock_rate(cg_sel, rate);
  168. clk_prcmu_init.name = name;
  169. clk_prcmu_init.ops = clk_prcmu_ops;
  170. clk_prcmu_init.flags = flags;
  171. clk_prcmu_init.parent_names = (parent_name ? &parent_name : NULL);
  172. clk_prcmu_init.num_parents = (parent_name ? 1 : 0);
  173. clk->hw.init = &clk_prcmu_init;
  174. clk_reg = clk_register(NULL, &clk->hw);
  175. if (IS_ERR_OR_NULL(clk_reg))
  176. goto free_clk;
  177. return clk_reg;
  178. free_clk:
  179. kfree(clk);
  180. pr_err("clk_prcmu: %s failed to register clk\n", __func__);
  181. return ERR_PTR(-ENOMEM);
  182. }
  183. struct clk *clk_reg_prcmu_scalable(const char *name,
  184. const char *parent_name,
  185. u8 cg_sel,
  186. unsigned long rate,
  187. unsigned long flags)
  188. {
  189. return clk_reg_prcmu(name, parent_name, cg_sel, rate, flags,
  190. &clk_prcmu_scalable_ops);
  191. }
  192. struct clk *clk_reg_prcmu_gate(const char *name,
  193. const char *parent_name,
  194. u8 cg_sel,
  195. unsigned long flags)
  196. {
  197. return clk_reg_prcmu(name, parent_name, cg_sel, 0, flags,
  198. &clk_prcmu_gate_ops);
  199. }
  200. struct clk *clk_reg_prcmu_rate(const char *name,
  201. const char *parent_name,
  202. u8 cg_sel,
  203. unsigned long flags)
  204. {
  205. return clk_reg_prcmu(name, parent_name, cg_sel, 0, flags,
  206. &clk_prcmu_rate_ops);
  207. }
  208. struct clk *clk_reg_prcmu_opp_gate(const char *name,
  209. const char *parent_name,
  210. u8 cg_sel,
  211. unsigned long flags)
  212. {
  213. return clk_reg_prcmu(name, parent_name, cg_sel, 0, flags,
  214. &clk_prcmu_opp_gate_ops);
  215. }