clk-gate.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author: James Liao <jamesjj.liao@mediatek.com>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/dev_printk.h>
  8. #include <linux/mfd/syscon.h>
  9. #include <linux/module.h>
  10. #include <linux/printk.h>
  11. #include <linux/regmap.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include "clk-mtk.h"
  15. #include "clk-gate.h"
  16. struct mtk_clk_gate {
  17. struct clk_hw hw;
  18. struct regmap *regmap;
  19. struct regmap *regmap_hwv;
  20. const struct mtk_gate *gate;
  21. };
  22. static inline struct mtk_clk_gate *to_mtk_clk_gate(struct clk_hw *hw)
  23. {
  24. return container_of(hw, struct mtk_clk_gate, hw);
  25. }
  26. static u32 mtk_get_clockgating(struct clk_hw *hw)
  27. {
  28. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  29. u32 val;
  30. regmap_read(cg->regmap, cg->gate->regs->sta_ofs, &val);
  31. return val & BIT(cg->gate->shift);
  32. }
  33. static int mtk_cg_bit_is_cleared(struct clk_hw *hw)
  34. {
  35. return mtk_get_clockgating(hw) == 0;
  36. }
  37. static int mtk_cg_bit_is_set(struct clk_hw *hw)
  38. {
  39. return mtk_get_clockgating(hw) != 0;
  40. }
  41. static void mtk_cg_set_bit(struct clk_hw *hw)
  42. {
  43. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  44. regmap_write(cg->regmap, cg->gate->regs->set_ofs, BIT(cg->gate->shift));
  45. }
  46. static void mtk_cg_clr_bit(struct clk_hw *hw)
  47. {
  48. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  49. regmap_write(cg->regmap, cg->gate->regs->clr_ofs, BIT(cg->gate->shift));
  50. }
  51. static void mtk_cg_set_bit_no_setclr(struct clk_hw *hw)
  52. {
  53. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  54. regmap_set_bits(cg->regmap, cg->gate->regs->sta_ofs,
  55. BIT(cg->gate->shift));
  56. }
  57. static void mtk_cg_clr_bit_no_setclr(struct clk_hw *hw)
  58. {
  59. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  60. regmap_clear_bits(cg->regmap, cg->gate->regs->sta_ofs,
  61. BIT(cg->gate->shift));
  62. }
  63. static int mtk_cg_enable(struct clk_hw *hw)
  64. {
  65. mtk_cg_clr_bit(hw);
  66. return 0;
  67. }
  68. static void mtk_cg_disable(struct clk_hw *hw)
  69. {
  70. mtk_cg_set_bit(hw);
  71. }
  72. static int mtk_cg_enable_inv(struct clk_hw *hw)
  73. {
  74. mtk_cg_set_bit(hw);
  75. return 0;
  76. }
  77. static void mtk_cg_disable_inv(struct clk_hw *hw)
  78. {
  79. mtk_cg_clr_bit(hw);
  80. }
  81. static int mtk_cg_hwv_set_en(struct clk_hw *hw, bool enable)
  82. {
  83. struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
  84. u32 val;
  85. regmap_write(cg->regmap_hwv,
  86. enable ? cg->gate->hwv_regs->set_ofs :
  87. cg->gate->hwv_regs->clr_ofs,
  88. BIT(cg->gate->shift));
  89. return regmap_read_poll_timeout_atomic(cg->regmap_hwv,
  90. cg->gate->hwv_regs->sta_ofs, val,
  91. val & BIT(cg->gate->shift), 0,
  92. MTK_WAIT_HWV_DONE_US);
  93. }
  94. static int mtk_cg_hwv_enable(struct clk_hw *hw)
  95. {
  96. return mtk_cg_hwv_set_en(hw, true);
  97. }
  98. static void mtk_cg_hwv_disable(struct clk_hw *hw)
  99. {
  100. mtk_cg_hwv_set_en(hw, false);
  101. }
  102. static int mtk_cg_enable_no_setclr(struct clk_hw *hw)
  103. {
  104. mtk_cg_clr_bit_no_setclr(hw);
  105. return 0;
  106. }
  107. static void mtk_cg_disable_no_setclr(struct clk_hw *hw)
  108. {
  109. mtk_cg_set_bit_no_setclr(hw);
  110. }
  111. static int mtk_cg_enable_inv_no_setclr(struct clk_hw *hw)
  112. {
  113. mtk_cg_set_bit_no_setclr(hw);
  114. return 0;
  115. }
  116. static void mtk_cg_disable_inv_no_setclr(struct clk_hw *hw)
  117. {
  118. mtk_cg_clr_bit_no_setclr(hw);
  119. }
  120. static bool mtk_cg_uses_hwv(const struct clk_ops *ops)
  121. {
  122. if (ops == &mtk_clk_gate_hwv_ops_setclr ||
  123. ops == &mtk_clk_gate_hwv_ops_setclr_inv)
  124. return true;
  125. return false;
  126. }
  127. const struct clk_ops mtk_clk_gate_ops_setclr = {
  128. .is_enabled = mtk_cg_bit_is_cleared,
  129. .enable = mtk_cg_enable,
  130. .disable = mtk_cg_disable,
  131. };
  132. EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_setclr);
  133. const struct clk_ops mtk_clk_gate_ops_setclr_inv = {
  134. .is_enabled = mtk_cg_bit_is_set,
  135. .enable = mtk_cg_enable_inv,
  136. .disable = mtk_cg_disable_inv,
  137. };
  138. EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_setclr_inv);
  139. const struct clk_ops mtk_clk_gate_hwv_ops_setclr = {
  140. .is_enabled = mtk_cg_bit_is_cleared,
  141. .enable = mtk_cg_hwv_enable,
  142. .disable = mtk_cg_hwv_disable,
  143. };
  144. EXPORT_SYMBOL_GPL(mtk_clk_gate_hwv_ops_setclr);
  145. const struct clk_ops mtk_clk_gate_hwv_ops_setclr_inv = {
  146. .is_enabled = mtk_cg_bit_is_set,
  147. .enable = mtk_cg_hwv_enable,
  148. .disable = mtk_cg_hwv_disable,
  149. };
  150. EXPORT_SYMBOL_GPL(mtk_clk_gate_hwv_ops_setclr_inv);
  151. const struct clk_ops mtk_clk_gate_ops_no_setclr = {
  152. .is_enabled = mtk_cg_bit_is_cleared,
  153. .enable = mtk_cg_enable_no_setclr,
  154. .disable = mtk_cg_disable_no_setclr,
  155. };
  156. EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_no_setclr);
  157. const struct clk_ops mtk_clk_gate_ops_no_setclr_inv = {
  158. .is_enabled = mtk_cg_bit_is_set,
  159. .enable = mtk_cg_enable_inv_no_setclr,
  160. .disable = mtk_cg_disable_inv_no_setclr,
  161. };
  162. EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_no_setclr_inv);
  163. static struct clk_hw *mtk_clk_register_gate(struct device *dev,
  164. const struct mtk_gate *gate,
  165. struct regmap *regmap,
  166. struct regmap *regmap_hwv)
  167. {
  168. struct mtk_clk_gate *cg;
  169. int ret;
  170. struct clk_init_data init = {};
  171. cg = kzalloc_obj(*cg);
  172. if (!cg)
  173. return ERR_PTR(-ENOMEM);
  174. init.name = gate->name;
  175. init.flags = gate->flags | CLK_SET_RATE_PARENT;
  176. init.parent_names = gate->parent_name ? &gate->parent_name : NULL;
  177. init.num_parents = gate->parent_name ? 1 : 0;
  178. init.ops = gate->ops;
  179. if (mtk_cg_uses_hwv(init.ops) && !regmap_hwv)
  180. return dev_err_ptr_probe(
  181. dev, -ENXIO,
  182. "regmap not found for hardware voter clocks\n");
  183. cg->regmap = regmap;
  184. cg->regmap_hwv = regmap_hwv;
  185. cg->gate = gate;
  186. cg->hw.init = &init;
  187. ret = clk_hw_register(dev, &cg->hw);
  188. if (ret) {
  189. kfree(cg);
  190. return ERR_PTR(ret);
  191. }
  192. return &cg->hw;
  193. }
  194. static void mtk_clk_unregister_gate(struct clk_hw *hw)
  195. {
  196. struct mtk_clk_gate *cg;
  197. if (!hw)
  198. return;
  199. cg = to_mtk_clk_gate(hw);
  200. clk_hw_unregister(hw);
  201. kfree(cg);
  202. }
  203. int mtk_clk_register_gates(struct device *dev, struct device_node *node,
  204. const struct mtk_gate *clks, int num,
  205. struct clk_hw_onecell_data *clk_data)
  206. {
  207. int i;
  208. struct clk_hw *hw;
  209. struct regmap *regmap;
  210. struct regmap *regmap_hwv;
  211. if (!clk_data)
  212. return -ENOMEM;
  213. regmap = device_node_to_regmap(node);
  214. if (IS_ERR(regmap)) {
  215. pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
  216. return PTR_ERR(regmap);
  217. }
  218. regmap_hwv = mtk_clk_get_hwv_regmap(node);
  219. if (IS_ERR(regmap_hwv))
  220. return dev_err_probe(
  221. dev, PTR_ERR(regmap_hwv),
  222. "Cannot find hardware voter regmap for %pOF\n", node);
  223. for (i = 0; i < num; i++) {
  224. const struct mtk_gate *gate = &clks[i];
  225. if (!IS_ERR_OR_NULL(clk_data->hws[gate->id])) {
  226. pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
  227. node, gate->id);
  228. continue;
  229. }
  230. hw = mtk_clk_register_gate(dev, gate, regmap, regmap_hwv);
  231. if (IS_ERR(hw)) {
  232. pr_err("Failed to register clk %s: %pe\n", gate->name,
  233. hw);
  234. goto err;
  235. }
  236. clk_data->hws[gate->id] = hw;
  237. }
  238. return 0;
  239. err:
  240. while (--i >= 0) {
  241. const struct mtk_gate *gate = &clks[i];
  242. if (IS_ERR_OR_NULL(clk_data->hws[gate->id]))
  243. continue;
  244. mtk_clk_unregister_gate(clk_data->hws[gate->id]);
  245. clk_data->hws[gate->id] = ERR_PTR(-ENOENT);
  246. }
  247. return PTR_ERR(hw);
  248. }
  249. EXPORT_SYMBOL_GPL(mtk_clk_register_gates);
  250. void mtk_clk_unregister_gates(const struct mtk_gate *clks, int num,
  251. struct clk_hw_onecell_data *clk_data)
  252. {
  253. int i;
  254. if (!clk_data)
  255. return;
  256. for (i = num; i > 0; i--) {
  257. const struct mtk_gate *gate = &clks[i - 1];
  258. if (IS_ERR_OR_NULL(clk_data->hws[gate->id]))
  259. continue;
  260. mtk_clk_unregister_gate(clk_data->hws[gate->id]);
  261. clk_data->hws[gate->id] = ERR_PTR(-ENOENT);
  262. }
  263. }
  264. EXPORT_SYMBOL_GPL(mtk_clk_unregister_gates);
  265. MODULE_LICENSE("GPL");