dp_pll_7nm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  4. */
  5. /*
  6. * Display Port PLL driver block diagram for branch clocks
  7. *
  8. * +------------------------------+
  9. * | DP_VCO_CLK |
  10. * | |
  11. * | +-------------------+ |
  12. * | | (DP PLL/VCO) | |
  13. * | +---------+---------+ |
  14. * | v |
  15. * | +----------+-----------+ |
  16. * | | hsclk_divsel_clk_src | |
  17. * | +----------+-----------+ |
  18. * +------------------------------+
  19. * |
  20. * +------------<---------v------------>----------+
  21. * | |
  22. * +-----v------------+ |
  23. * | dp_link_clk_src | |
  24. * | divsel_ten | |
  25. * +---------+--------+ |
  26. * | |
  27. * | |
  28. * v v
  29. * Input to DISPCC block |
  30. * for link clk, crypto clk |
  31. * and interface clock |
  32. * |
  33. * |
  34. * +--------<------------+-----------------+---<---+
  35. * | | |
  36. * +-------v------+ +--------v-----+ +--------v------+
  37. * | vco_divided | | vco_divided | | vco_divided |
  38. * | _clk_src | | _clk_src | | _clk_src |
  39. * | | | | | |
  40. * |divsel_six | | divsel_two | | divsel_four |
  41. * +-------+------+ +-----+--------+ +--------+------+
  42. * | | |
  43. * v------->----------v-------------<------v
  44. * |
  45. * +----------+---------+
  46. * | vco_divided_clk |
  47. * | _src_mux |
  48. * +---------+----------+
  49. * |
  50. * v
  51. * Input to DISPCC block
  52. * for DP pixel clock
  53. *
  54. */
  55. #define pr_fmt(fmt) "%s: " fmt, __func__
  56. #include <linux/kernel.h>
  57. #include <linux/err.h>
  58. #include <linux/delay.h>
  59. #include <dt-bindings/clock/mdss-10nm-pll-clk.h>
  60. #include "pll_drv.h"
  61. #include "dp_pll.h"
  62. #include "dp_pll_7nm.h"
  63. static struct dp_pll_db_7nm dp_pdb_7nm;
  64. static struct clk_ops mux_clk_ops;
  65. static struct regmap_config dp_pll_7nm_cfg = {
  66. .reg_bits = 32,
  67. .reg_stride = 4,
  68. .val_bits = 32,
  69. .max_register = 0x910,
  70. };
  71. static struct regmap_bus dp_pixel_mux_regmap_ops = {
  72. .reg_write = dp_mux_set_parent_7nm,
  73. .reg_read = dp_mux_get_parent_7nm,
  74. };
  75. /* Op structures */
  76. static const struct clk_ops dp_7nm_vco_clk_ops = {
  77. .recalc_rate = dp_vco_recalc_rate_7nm,
  78. .set_rate = dp_vco_set_rate_7nm,
  79. .round_rate = dp_vco_round_rate_7nm,
  80. .prepare = dp_vco_prepare_7nm,
  81. .unprepare = dp_vco_unprepare_7nm,
  82. };
  83. static struct dp_pll_vco_clk dp_vco_clk = {
  84. .min_rate = DP_VCO_HSCLK_RATE_1620MHZDIV1000,
  85. .max_rate = DP_VCO_HSCLK_RATE_8100MHZDIV1000,
  86. .hw.init = &(struct clk_init_data){
  87. .name = "dp_vco_clk",
  88. .parent_names = (const char *[]){ "xo_board" },
  89. .num_parents = 1,
  90. .ops = &dp_7nm_vco_clk_ops,
  91. },
  92. };
  93. static struct clk_fixed_factor dp_phy_pll_link_clk = {
  94. .div = 10,
  95. .mult = 1,
  96. .hw.init = &(struct clk_init_data){
  97. .name = "dp_phy_pll_link_clk",
  98. .parent_names =
  99. (const char *[]){ "dp_vco_clk" },
  100. .num_parents = 1,
  101. .flags = CLK_SET_RATE_PARENT,
  102. .ops = &clk_fixed_factor_ops,
  103. },
  104. };
  105. static struct clk_fixed_factor dp_link_clk_divsel_ten = {
  106. .div = 10,
  107. .mult = 1,
  108. .hw.init = &(struct clk_init_data){
  109. .name = "dp_link_clk_divsel_ten",
  110. .parent_names =
  111. (const char *[]){ "dp_vco_clk" },
  112. .num_parents = 1,
  113. .flags = CLK_SET_RATE_PARENT,
  114. .ops = &clk_fixed_factor_ops,
  115. },
  116. };
  117. static struct clk_fixed_factor dp_vco_divsel_two_clk_src = {
  118. .div = 2,
  119. .mult = 1,
  120. .hw.init = &(struct clk_init_data){
  121. .name = "dp_vco_divsel_two_clk_src",
  122. .parent_names =
  123. (const char *[]){ "dp_vco_clk" },
  124. .num_parents = 1,
  125. .ops = &clk_fixed_factor_ops,
  126. },
  127. };
  128. static struct clk_fixed_factor dp_vco_divsel_four_clk_src = {
  129. .div = 4,
  130. .mult = 1,
  131. .hw.init = &(struct clk_init_data){
  132. .name = "dp_vco_divsel_four_clk_src",
  133. .parent_names =
  134. (const char *[]){ "dp_vco_clk" },
  135. .num_parents = 1,
  136. .ops = &clk_fixed_factor_ops,
  137. },
  138. };
  139. static struct clk_fixed_factor dp_vco_divsel_six_clk_src = {
  140. .div = 6,
  141. .mult = 1,
  142. .hw.init = &(struct clk_init_data){
  143. .name = "dp_vco_divsel_six_clk_src",
  144. .parent_names =
  145. (const char *[]){ "dp_vco_clk" },
  146. .num_parents = 1,
  147. .ops = &clk_fixed_factor_ops,
  148. },
  149. };
  150. static int clk_mux_determine_rate(struct clk_hw *hw,
  151. struct clk_rate_request *req)
  152. {
  153. int ret = 0;
  154. if (!hw || !req) {
  155. pr_err("Invalid input parameters\n");
  156. return -EINVAL;
  157. }
  158. ret = __clk_mux_determine_rate_closest(hw, req);
  159. if (ret)
  160. return ret;
  161. /* Set the new parent of mux if there is a new valid parent */
  162. if (hw->clk && req->best_parent_hw->clk)
  163. clk_set_parent(hw->clk, req->best_parent_hw->clk);
  164. return 0;
  165. }
  166. static unsigned long mux_recalc_rate(struct clk_hw *hw,
  167. unsigned long parent_rate)
  168. {
  169. struct clk *div_clk = NULL, *vco_clk = NULL;
  170. struct dp_pll_vco_clk *vco = NULL;
  171. if (!hw) {
  172. pr_err("Invalid input parameter\n");
  173. return 0;
  174. }
  175. div_clk = clk_get_parent(hw->clk);
  176. if (!div_clk)
  177. return 0;
  178. vco_clk = clk_get_parent(div_clk);
  179. if (!vco_clk)
  180. return 0;
  181. vco = to_dp_vco_hw(__clk_get_hw(vco_clk));
  182. if (!vco)
  183. return 0;
  184. if (vco->rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000)
  185. return (vco->rate / 6);
  186. else if (vco->rate == DP_VCO_HSCLK_RATE_5400MHZDIV1000)
  187. return (vco->rate / 4);
  188. else
  189. return (vco->rate / 2);
  190. }
  191. static struct clk_regmap_mux dp_phy_pll_vco_div_clk = {
  192. .reg = 0x64,
  193. .shift = 0,
  194. .width = 2,
  195. .clkr = {
  196. .hw.init = &(struct clk_init_data){
  197. .name = "dp_phy_pll_vco_div_clk",
  198. .parent_names =
  199. (const char *[]){"dp_vco_divsel_two_clk_src",
  200. "dp_vco_divsel_four_clk_src",
  201. "dp_vco_divsel_six_clk_src"},
  202. .num_parents = 3,
  203. .ops = &mux_clk_ops,
  204. .flags = CLK_SET_RATE_PARENT,
  205. },
  206. },
  207. };
  208. static struct clk_regmap_mux dp_vco_divided_clk_src_mux = {
  209. .reg = 0x64,
  210. .shift = 0,
  211. .width = 2,
  212. .clkr = {
  213. .hw.init = &(struct clk_init_data){
  214. .name = "dp_vco_divided_clk_src_mux",
  215. .parent_names =
  216. (const char *[]){"dp_vco_divsel_two_clk_src",
  217. "dp_vco_divsel_four_clk_src",
  218. "dp_vco_divsel_six_clk_src"},
  219. .num_parents = 3,
  220. .ops = &mux_clk_ops,
  221. .flags = CLK_SET_RATE_PARENT,
  222. },
  223. },
  224. };
  225. static struct clk_hw *mdss_dp_pllcc_7nm[] = {
  226. [DP_VCO_CLK] = &dp_vco_clk.hw,
  227. [DP_LINK_CLK_DIVSEL_TEN] = &dp_link_clk_divsel_ten.hw,
  228. [DP_VCO_DIVIDED_TWO_CLK_SRC] = &dp_vco_divsel_two_clk_src.hw,
  229. [DP_VCO_DIVIDED_FOUR_CLK_SRC] = &dp_vco_divsel_four_clk_src.hw,
  230. [DP_VCO_DIVIDED_SIX_CLK_SRC] = &dp_vco_divsel_six_clk_src.hw,
  231. [DP_VCO_DIVIDED_CLK_SRC_MUX] = &dp_vco_divided_clk_src_mux.clkr.hw,
  232. };
  233. int dp_pll_clock_register_7nm(struct platform_device *pdev,
  234. struct mdss_pll_resources *pll_res)
  235. {
  236. int rc = -ENOTSUPP, i = 0;
  237. struct clk_onecell_data *clk_data;
  238. struct clk *clk;
  239. struct regmap *regmap;
  240. int num_clks = ARRAY_SIZE(mdss_dp_pllcc_7nm);
  241. clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL);
  242. if (!clk_data)
  243. return -ENOMEM;
  244. clk_data->clks = devm_kcalloc(&pdev->dev, num_clks,
  245. sizeof(struct clk *), GFP_KERNEL);
  246. if (!clk_data->clks)
  247. return -ENOMEM;
  248. clk_data->clk_num = num_clks;
  249. pll_res->priv = &dp_pdb_7nm;
  250. dp_pdb_7nm.pll = pll_res;
  251. /* Set client data for vco, mux and div clocks */
  252. regmap = devm_regmap_init(&pdev->dev, &dp_pixel_mux_regmap_ops,
  253. pll_res, &dp_pll_7nm_cfg);
  254. mux_clk_ops = clk_regmap_mux_closest_ops;
  255. mux_clk_ops.determine_rate = clk_mux_determine_rate;
  256. mux_clk_ops.recalc_rate = mux_recalc_rate;
  257. dp_vco_clk.priv = pll_res;
  258. /*
  259. * Consumer for the pll clock expects, the DP_LINK_CLK_DIVSEL_TEN and
  260. * DP_VCO_DIVIDED_CLK_SRC_MUX clock names to be "dp_phy_pll_link_clk"
  261. * and "dp_phy_pll_vco_div_clk" respectively for a V2 pll interface
  262. * target.
  263. */
  264. if (pll_res->pll_interface_type == MDSS_DP_PLL_7NM_V2) {
  265. mdss_dp_pllcc_7nm[DP_LINK_CLK_DIVSEL_TEN] =
  266. &dp_phy_pll_link_clk.hw;
  267. mdss_dp_pllcc_7nm[DP_VCO_DIVIDED_CLK_SRC_MUX] =
  268. &dp_phy_pll_vco_div_clk.clkr.hw;
  269. dp_phy_pll_vco_div_clk.clkr.regmap = regmap;
  270. } else
  271. dp_vco_divided_clk_src_mux.clkr.regmap = regmap;
  272. for (i = DP_VCO_CLK; i <= DP_VCO_DIVIDED_CLK_SRC_MUX; i++) {
  273. pr_debug("reg clk: %d index: %d\n", i, pll_res->index);
  274. clk = devm_clk_register(&pdev->dev, mdss_dp_pllcc_7nm[i]);
  275. if (IS_ERR(clk)) {
  276. pr_err("clk registration failed for DP: %d\n",
  277. pll_res->index);
  278. rc = -EINVAL;
  279. goto clk_reg_fail;
  280. }
  281. clk_data->clks[i] = clk;
  282. }
  283. rc = of_clk_add_provider(pdev->dev.of_node,
  284. of_clk_src_onecell_get, clk_data);
  285. if (rc) {
  286. pr_err("Clock register failed rc=%d\n", rc);
  287. rc = -EPROBE_DEFER;
  288. goto clk_reg_fail;
  289. } else {
  290. pr_debug("SUCCESS\n");
  291. }
  292. return rc;
  293. clk_reg_fail:
  294. return rc;
  295. }