clk-pllfh.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022 MediaTek Inc.
  4. * Author: Edward-JW Yang <edward-jw.yang@mediatek.com>
  5. */
  6. #include <linux/of.h>
  7. #include <linux/of_address.h>
  8. #include <linux/io.h>
  9. #include <linux/slab.h>
  10. #include <linux/clkdev.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include "clk-mtk.h"
  14. #include "clk-pllfh.h"
  15. #include "clk-fhctl.h"
  16. static DEFINE_SPINLOCK(pllfh_lock);
  17. inline struct mtk_fh *to_mtk_fh(struct clk_hw *hw)
  18. {
  19. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  20. return container_of(pll, struct mtk_fh, clk_pll);
  21. }
  22. static int mtk_fhctl_set_rate(struct clk_hw *hw, unsigned long rate,
  23. unsigned long parent_rate)
  24. {
  25. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  26. struct mtk_fh *fh = to_mtk_fh(hw);
  27. u32 pcw = 0;
  28. u32 postdiv;
  29. mtk_pll_calc_values(pll, &pcw, &postdiv, rate, parent_rate);
  30. return fh->ops->hopping(fh, pcw, postdiv);
  31. }
  32. static const struct clk_ops mtk_pllfh_ops = {
  33. .is_prepared = mtk_pll_is_prepared,
  34. .prepare = mtk_pll_prepare,
  35. .unprepare = mtk_pll_unprepare,
  36. .recalc_rate = mtk_pll_recalc_rate,
  37. .determine_rate = mtk_pll_determine_rate,
  38. .set_rate = mtk_fhctl_set_rate,
  39. };
  40. static struct mtk_pllfh_data *get_pllfh_by_id(struct mtk_pllfh_data *pllfhs,
  41. int num_fhs, int pll_id)
  42. {
  43. int i;
  44. for (i = 0; i < num_fhs; i++)
  45. if (pllfhs[i].data.pll_id == pll_id)
  46. return &pllfhs[i];
  47. return NULL;
  48. }
  49. void fhctl_parse_dt(const u8 *compatible_node, struct mtk_pllfh_data *pllfhs,
  50. int num_fhs)
  51. {
  52. void __iomem *base;
  53. struct device_node *node;
  54. u32 num_clocks, pll_id, ssc_rate;
  55. int offset, i;
  56. node = of_find_compatible_node(NULL, NULL, compatible_node);
  57. if (!node) {
  58. pr_warn("cannot find \"%s\"\n", compatible_node);
  59. return;
  60. }
  61. base = of_iomap(node, 0);
  62. if (!base) {
  63. pr_err("%s(): ioremap failed\n", __func__);
  64. goto out_node_put;
  65. }
  66. num_clocks = of_clk_get_parent_count(node);
  67. if (!num_clocks) {
  68. pr_err("%s(): failed to get clocks property\n", __func__);
  69. goto err;
  70. }
  71. for (i = 0; i < num_clocks; i++) {
  72. struct mtk_pllfh_data *pllfh;
  73. offset = i * 2;
  74. of_property_read_u32_index(node, "clocks", offset + 1, &pll_id);
  75. of_property_read_u32_index(node,
  76. "mediatek,hopping-ssc-percent",
  77. i, &ssc_rate);
  78. pllfh = get_pllfh_by_id(pllfhs, num_fhs, pll_id);
  79. if (!pllfh)
  80. continue;
  81. pllfh->state.fh_enable = 1;
  82. pllfh->state.ssc_rate = ssc_rate;
  83. pllfh->state.base = base;
  84. }
  85. out_node_put:
  86. of_node_put(node);
  87. return;
  88. err:
  89. iounmap(base);
  90. goto out_node_put;
  91. }
  92. EXPORT_SYMBOL_GPL(fhctl_parse_dt);
  93. static int pllfh_init(struct mtk_fh *fh, struct mtk_pllfh_data *pllfh_data)
  94. {
  95. struct fh_pll_regs *regs = &fh->regs;
  96. const struct fhctl_offset *offset;
  97. void __iomem *base = pllfh_data->state.base;
  98. void __iomem *fhx_base = base + pllfh_data->data.fhx_offset;
  99. offset = fhctl_get_offset_table(pllfh_data->data.fh_ver);
  100. if (IS_ERR(offset))
  101. return PTR_ERR(offset);
  102. regs->reg_hp_en = base + offset->offset_hp_en;
  103. regs->reg_clk_con = base + offset->offset_clk_con;
  104. regs->reg_rst_con = base + offset->offset_rst_con;
  105. regs->reg_slope0 = base + offset->offset_slope0;
  106. regs->reg_slope1 = base + offset->offset_slope1;
  107. regs->reg_cfg = fhx_base + offset->offset_cfg;
  108. regs->reg_updnlmt = fhx_base + offset->offset_updnlmt;
  109. regs->reg_dds = fhx_base + offset->offset_dds;
  110. regs->reg_dvfs = fhx_base + offset->offset_dvfs;
  111. regs->reg_mon = fhx_base + offset->offset_mon;
  112. fh->pllfh_data = pllfh_data;
  113. fh->lock = &pllfh_lock;
  114. fh->ops = fhctl_get_ops();
  115. return 0;
  116. }
  117. static bool fhctl_is_supported_and_enabled(const struct mtk_pllfh_data *pllfh)
  118. {
  119. return pllfh && (pllfh->state.fh_enable == 1);
  120. }
  121. static struct clk_hw *
  122. mtk_clk_register_pllfh(struct device *dev, const struct mtk_pll_data *pll_data,
  123. struct mtk_pllfh_data *pllfh_data, void __iomem *base)
  124. {
  125. struct clk_hw *hw;
  126. struct mtk_fh *fh;
  127. int ret;
  128. fh = kzalloc_obj(*fh);
  129. if (!fh)
  130. return ERR_PTR(-ENOMEM);
  131. ret = pllfh_init(fh, pllfh_data);
  132. if (ret) {
  133. hw = ERR_PTR(ret);
  134. goto out;
  135. }
  136. fh->clk_pll.dev = dev;
  137. hw = mtk_clk_register_pll_ops(&fh->clk_pll, pll_data, base,
  138. &mtk_pllfh_ops);
  139. if (IS_ERR(hw))
  140. goto out;
  141. fhctl_hw_init(fh);
  142. out:
  143. if (IS_ERR(hw))
  144. kfree(fh);
  145. return hw;
  146. }
  147. static void mtk_clk_unregister_pllfh(struct clk_hw *hw)
  148. {
  149. struct mtk_fh *fh;
  150. if (!hw)
  151. return;
  152. fh = to_mtk_fh(hw);
  153. clk_hw_unregister(hw);
  154. kfree(fh);
  155. }
  156. int mtk_clk_register_pllfhs(struct device *dev,
  157. const struct mtk_pll_data *plls, int num_plls,
  158. struct mtk_pllfh_data *pllfhs, int num_fhs,
  159. struct clk_hw_onecell_data *clk_data)
  160. {
  161. void __iomem *base;
  162. int i;
  163. struct clk_hw *hw;
  164. base = of_iomap(dev->of_node, 0);
  165. if (!base) {
  166. pr_err("%s(): ioremap failed\n", __func__);
  167. return -EINVAL;
  168. }
  169. for (i = 0; i < num_plls; i++) {
  170. const struct mtk_pll_data *pll = &plls[i];
  171. struct mtk_pllfh_data *pllfh;
  172. bool use_fhctl;
  173. pllfh = get_pllfh_by_id(pllfhs, num_fhs, pll->id);
  174. use_fhctl = fhctl_is_supported_and_enabled(pllfh);
  175. if (use_fhctl)
  176. hw = mtk_clk_register_pllfh(dev, pll, pllfh, base);
  177. else
  178. hw = mtk_clk_register_pll(dev, pll, base);
  179. if (IS_ERR(hw)) {
  180. pr_err("Failed to register %s clk %s: %ld\n",
  181. use_fhctl ? "fhpll" : "pll", pll->name,
  182. PTR_ERR(hw));
  183. goto err;
  184. }
  185. clk_data->hws[pll->id] = hw;
  186. }
  187. return 0;
  188. err:
  189. while (--i >= 0) {
  190. const struct mtk_pll_data *pll = &plls[i];
  191. struct mtk_pllfh_data *pllfh;
  192. bool use_fhctl;
  193. pllfh = get_pllfh_by_id(pllfhs, num_fhs, pll->id);
  194. use_fhctl = fhctl_is_supported_and_enabled(pllfh);
  195. if (use_fhctl)
  196. mtk_clk_unregister_pllfh(clk_data->hws[pll->id]);
  197. else
  198. mtk_clk_unregister_pll(clk_data->hws[pll->id]);
  199. clk_data->hws[pll->id] = ERR_PTR(-ENOENT);
  200. }
  201. iounmap(base);
  202. return PTR_ERR(hw);
  203. }
  204. EXPORT_SYMBOL_GPL(mtk_clk_register_pllfhs);
  205. void mtk_clk_unregister_pllfhs(const struct mtk_pll_data *plls, int num_plls,
  206. struct mtk_pllfh_data *pllfhs, int num_fhs,
  207. struct clk_hw_onecell_data *clk_data)
  208. {
  209. void __iomem *base = NULL, *fhctl_base = NULL;
  210. int i;
  211. if (!clk_data)
  212. return;
  213. for (i = num_plls; i > 0; i--) {
  214. const struct mtk_pll_data *pll = &plls[i - 1];
  215. struct mtk_pllfh_data *pllfh;
  216. bool use_fhctl;
  217. if (IS_ERR_OR_NULL(clk_data->hws[pll->id]))
  218. continue;
  219. pllfh = get_pllfh_by_id(pllfhs, num_fhs, pll->id);
  220. use_fhctl = fhctl_is_supported_and_enabled(pllfh);
  221. if (use_fhctl) {
  222. fhctl_base = pllfh->state.base;
  223. mtk_clk_unregister_pllfh(clk_data->hws[pll->id]);
  224. } else {
  225. base = mtk_clk_pll_get_base(clk_data->hws[pll->id],
  226. pll);
  227. mtk_clk_unregister_pll(clk_data->hws[pll->id]);
  228. }
  229. clk_data->hws[pll->id] = ERR_PTR(-ENOENT);
  230. }
  231. if (fhctl_base)
  232. iounmap(fhctl_base);
  233. iounmap(base);
  234. }
  235. EXPORT_SYMBOL_GPL(mtk_clk_unregister_pllfhs);