dp_pll_4nm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. /*
  6. * Display Port PLL driver block diagram for branch clocks
  7. *
  8. * +------------------------+ +------------------------+
  9. * | dp_phy_pll_link_clk | | dp_phy_pll_vco_div_clk |
  10. * +------------------------+ +------------------------+
  11. * | |
  12. * | |
  13. * V V
  14. * dp_link_clk dp_pixel_clk
  15. *
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/kernel.h>
  23. #include <linux/regmap.h>
  24. #include "dp_hpd.h"
  25. #include "dp_debug.h"
  26. #include "dp_pll.h"
  27. #define DP_PHY_CFG 0x0010
  28. #define DP_PHY_CFG_1 0x0014
  29. #define DP_PHY_PD_CTL 0x0018
  30. #define DP_PHY_MODE 0x001C
  31. #define DP_PHY_AUX_CFG1 0x0024
  32. #define DP_PHY_AUX_CFG2 0x0028
  33. #define DP_PHY_TX0_TX1_LANE_CTL 0x0078
  34. #define DP_PHY_TX2_TX3_LANE_CTL 0x009C
  35. #define DP_PHY_SPARE0 0x00C8
  36. #define DP_PHY_STATUS 0x00E4
  37. /* Tx registers */
  38. #define TXn_CLKBUF_ENABLE 0x0008
  39. #define TXn_TX_EMP_POST1_LVL 0x000C
  40. #define TXn_TX_DRV_LVL 0x0014
  41. #define TXn_RESET_TSYNC_EN 0x001C
  42. #define TXn_PRE_STALL_LDO_BOOST_EN 0x0020
  43. #define TXn_TX_BAND 0x0024
  44. #define TXn_INTERFACE_SELECT 0x002C
  45. #define TXn_RES_CODE_LANE_OFFSET_TX 0x003C
  46. #define TXn_RES_CODE_LANE_OFFSET_RX 0x0040
  47. #define TXn_TRANSCEIVER_BIAS_EN 0x0054
  48. #define TXn_HIGHZ_DRVR_EN 0x0058
  49. #define TXn_TX_POL_INV 0x005C
  50. #define TXn_PARRATE_REC_DETECT_IDLE_EN 0x0060
  51. /* PLL register offset */
  52. #define QSERDES_COM_BG_TIMER 0x00BC
  53. #define QSERDES_COM_SSC_EN_CENTER 0x00C0
  54. #define QSERDES_COM_SSC_ADJ_PER1 0x00C4
  55. #define QSERDES_COM_SSC_PER1 0x00CC
  56. #define QSERDES_COM_SSC_PER2 0x00D0
  57. #define QSERDES_COM_SSC_STEP_SIZE1_MODE0 0x0060
  58. #define QSERDES_COM_SSC_STEP_SIZE2_MODE0 0X0064
  59. #define QSERDES_COM_BIAS_EN_CLKBUFLR_EN 0x00DC
  60. #define QSERDES_COM_CLK_ENABLE1 0x00E0
  61. #define QSERDES_COM_SYS_CLK_CTRL 0x00E4
  62. #define QSERDES_COM_SYSCLK_BUF_ENABLE 0x00E8
  63. #define QSERDES_COM_PLL_IVCO 0x00F4
  64. #define QSERDES_COM_CP_CTRL_MODE0 0x0070
  65. #define QSERDES_COM_PLL_RCTRL_MODE0 0x0074
  66. #define QSERDES_COM_PLL_CCTRL_MODE0 0x0078
  67. #define QSERDES_COM_SYSCLK_EN_SEL 0x0110
  68. #define QSERDES_COM_RESETSM_CNTRL 0x0118
  69. #define QSERDES_COM_LOCK_CMP_EN 0x0120
  70. #define QSERDES_COM_LOCK_CMP1_MODE0 0x0080
  71. #define QSERDES_COM_LOCK_CMP2_MODE0 0x0084
  72. #define QSERDES_COM_DEC_START_MODE0 0x0088
  73. #define QSERDES_COM_DIV_FRAC_START1_MODE0 0x0090
  74. #define QSERDES_COM_DIV_FRAC_START2_MODE0 0x0094
  75. #define QSERDES_COM_DIV_FRAC_START3_MODE0 0x0098
  76. #define QSERDES_COM_INTEGLOOP_GAIN0_MODE0 0x00A0
  77. #define QSERDES_COM_INTEGLOOP_GAIN1_MODE0 0x00A4
  78. #define QSERDES_COM_VCO_TUNE_CTRL 0x013C
  79. #define QSERDES_COM_VCO_TUNE_MAP 0x0140
  80. #define QSERDES_COM_CMN_STATUS 0x01D0
  81. #define QSERDES_COM_CLK_SEL 0x0164
  82. #define QSERDES_COM_HSCLK_SEL_1 0x003C
  83. #define QSERDES_COM_CORECLK_DIV_MODE0 0x007C
  84. #define QSERDES_COM_CORE_CLK_EN 0x0170
  85. #define QSERDES_COM_C_READY_STATUS 0x01F8
  86. #define QSERDES_COM_CMN_CONFIG_1 0x0174
  87. #define QSERDES_COM_SVS_MODE_CLK_SEL 0x017C
  88. #define QSERDES_COM_BIN_VCOCAL_CMP_CODE1_MODE0 0x0058
  89. #define QSERDES_COM_BIN_VCOCAL_CMP_CODE2_MODE0 0x005C
  90. /* Tx tran offsets */
  91. #define DP_TRAN_DRVR_EMP_EN 0x00C0
  92. #define DP_TX_INTERFACE_MODE 0x00C4
  93. /* Tx VMODE offsets */
  94. #define DP_VMODE_CTRL1 0x00C8
  95. #define DP_PHY_PLL_POLL_SLEEP_US 500
  96. #define DP_PHY_PLL_POLL_TIMEOUT_US 10000
  97. #define DP_VCO_RATE_8100MHZDIV1000 8100000UL
  98. #define DP_VCO_RATE_9720MHZDIV1000 9720000UL
  99. #define DP_VCO_RATE_10800MHZDIV1000 10800000UL
  100. #define DP_PLL_NUM_CLKS 2
  101. #define DP_4NM_C_READY BIT(0)
  102. #define DP_4NM_FREQ_DONE BIT(0)
  103. #define DP_4NM_PLL_LOCKED BIT(1)
  104. #define DP_4NM_PHY_READY BIT(1)
  105. #define DP_4NM_TSYNC_DONE BIT(0)
  106. static const struct dp_pll_params pll_params_v1[HSCLK_RATE_MAX] = {
  107. {0x05, 0x3f, 0x00, 0x04, 0x01, 0x69, 0x00, 0x80, 0x07, 0x6f, 0x08, 0x45, 0x06, 0x36, 0x01,
  108. 0xe2, 0x18, 0x0f, 0x0e, 0x0f, 0x0c, 0x0c},
  109. {0x03, 0x3f, 0x00, 0x08, 0x01, 0x69, 0x00, 0x80, 0x07, 0x0f, 0x0e, 0x45, 0x06, 0x36, 0x01,
  110. 0xe2, 0x18, 0x0f, 0x0e, 0x0f, 0x0c, 0x0c},
  111. {0x01, 0x3f, 0x00, 0x08, 0x02, 0x8c, 0x00, 0x00, 0x0a, 0x1f, 0x1c, 0x5c, 0x08, 0x36, 0x01,
  112. 0x2e, 0x21, 0x0f, 0x0e, 0x0f, 0x0c, 0x0c},
  113. {0x00, 0x3f, 0x00, 0x08, 0x00, 0x69, 0x00, 0x80, 0x07, 0x2f, 0x2a, 0x45, 0x06, 0x36, 0x01,
  114. 0xe2, 0x18, 0x0f, 0x0e, 0x0f, 0x0c, 0x0c},
  115. };
  116. static const struct dp_pll_params pll_params_v1_1[HSCLK_RATE_MAX] = {
  117. {0x05, 0x3f, 0x00, 0x04, 0x01, 0x34, 0x00, 0xc0, 0x0b, 0x37, 0x04, 0x92, 0x01, 0x6b, 0x02,
  118. 0x71, 0x0c, 0x0f, 0x0a, 0x0f, 0x0c, 0x0c},
  119. {0x03, 0x3f, 0x00, 0x08, 0x01, 0x34, 0x00, 0xc0, 0x0b, 0x07, 0x07, 0x92, 0x01, 0x6b, 0x02,
  120. 0x71, 0x0c, 0x0f, 0x0a, 0x0f, 0x0c, 0x0c},
  121. {0x01, 0x3f, 0x00, 0x08, 0x02, 0x46, 0x00, 0x00, 0x05, 0x0f, 0x0e, 0x18, 0x02, 0x6b, 0x02,
  122. 0x97, 0x10, 0x0f, 0x0a, 0x0f, 0x0c, 0x0c},
  123. {0x00, 0x3f, 0x00, 0x08, 0x00, 0x34, 0x00, 0xc0, 0x0b, 0x17, 0x15, 0x92, 0x01, 0x6b, 0x02,
  124. 0x71, 0x0c, 0x0f, 0x0a, 0x0f, 0x0c, 0x0c}
  125. };
  126. static int set_vco_div(struct dp_pll *pll, unsigned long rate)
  127. {
  128. u32 div, val;
  129. if (!pll)
  130. return -EINVAL;
  131. if (is_gdsc_disabled(pll))
  132. return -EINVAL;
  133. val = dp_pll_read(dp_phy, DP_PHY_VCO_DIV);
  134. val &= ~0x03;
  135. switch (rate) {
  136. case DP_VCO_HSCLK_RATE_1620MHZDIV1000:
  137. case DP_VCO_HSCLK_RATE_2700MHZDIV1000:
  138. div = 2;
  139. val |= 1;
  140. break;
  141. case DP_VCO_HSCLK_RATE_5400MHZDIV1000:
  142. div = 4;
  143. val |= 2;
  144. break;
  145. case DP_VCO_HSCLK_RATE_8100MHZDIV1000:
  146. div = 6;
  147. /* val = 0 for this case, so no update needed */
  148. break;
  149. default:
  150. /* No other link rates are supported */
  151. return -EINVAL;
  152. }
  153. dp_pll_write(dp_phy, DP_PHY_VCO_DIV, val);
  154. /* Make sure the PHY registers writes are done */
  155. wmb();
  156. /*
  157. * Set the rate for the link and pixel clock sources so that the
  158. * linux clock framework can appropriately compute the MND values
  159. * whenever the pixel clock rate is set.
  160. */
  161. clk_set_rate(pll->clk_data->clks[0], pll->vco_rate / 10);
  162. clk_set_rate(pll->clk_data->clks[1], pll->vco_rate / div);
  163. DP_DEBUG("val=%#x div=%x link_clk rate=%lu vco_div_clk rate=%lu\n",
  164. val, div, pll->vco_rate / 10, pll->vco_rate / div);
  165. return 0;
  166. }
  167. static int dp_vco_pll_init_db_4nm(struct dp_pll_db *pdb,
  168. unsigned long rate)
  169. {
  170. struct dp_pll *pll = pdb->pll;
  171. u32 spare_value = 0;
  172. spare_value = dp_pll_read(dp_phy, DP_PHY_SPARE0);
  173. pdb->lane_cnt = spare_value & 0x0F;
  174. pdb->orientation = (spare_value & 0xF0) >> 4;
  175. DP_DEBUG("spare_value=0x%x, ln_cnt=0x%x, orientation=0x%x\n",
  176. spare_value, pdb->lane_cnt, pdb->orientation);
  177. switch (rate) {
  178. case DP_VCO_HSCLK_RATE_1620MHZDIV1000:
  179. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_9720MHZDIV1000);
  180. pdb->rate_idx = HSCLK_RATE_1620MHZ;
  181. break;
  182. case DP_VCO_HSCLK_RATE_2700MHZDIV1000:
  183. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_10800MHZDIV1000);
  184. pdb->rate_idx = HSCLK_RATE_2700MHZ;
  185. break;
  186. case DP_VCO_HSCLK_RATE_5400MHZDIV1000:
  187. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_10800MHZDIV1000);
  188. pdb->rate_idx = HSCLK_RATE_5400MHZ;
  189. break;
  190. case DP_VCO_HSCLK_RATE_8100MHZDIV1000:
  191. DP_DEBUG("VCO rate: %ld\n", DP_VCO_RATE_8100MHZDIV1000);
  192. pdb->rate_idx = HSCLK_RATE_8100MHZ;
  193. break;
  194. default:
  195. DP_ERR("unsupported rate %ld\n", rate);
  196. return -EINVAL;
  197. }
  198. return 0;
  199. }
  200. static int dp_config_vco_rate_4nm(struct dp_pll *pll,
  201. unsigned long rate)
  202. {
  203. int rc = 0;
  204. struct dp_pll_db *pdb = (struct dp_pll_db *)pll->priv;
  205. const struct dp_pll_params *params;
  206. rc = dp_vco_pll_init_db_4nm(pdb, rate);
  207. if (rc < 0) {
  208. DP_ERR("VCO Init DB failed\n");
  209. return rc;
  210. }
  211. dp_pll_write(dp_phy, DP_PHY_CFG_1, 0x0F);
  212. if (pdb->lane_cnt != 4) {
  213. if (pdb->orientation == ORIENTATION_CC2)
  214. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x6d);
  215. else
  216. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x75);
  217. } else {
  218. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x7d);
  219. }
  220. if (pdb->rate_idx < HSCLK_RATE_MAX) {
  221. params = &pdb->pll_params[pdb->rate_idx];
  222. } else {
  223. DP_ERR("link rate not set\n");
  224. return -EINVAL;
  225. }
  226. /* Make sure the PHY register writes are done */
  227. wmb();
  228. dp_pll_write(dp_pll, QSERDES_COM_SVS_MODE_CLK_SEL, 0x15);
  229. dp_pll_write(dp_pll, QSERDES_COM_SYSCLK_EN_SEL, 0x3b);
  230. dp_pll_write(dp_pll, QSERDES_COM_SYS_CLK_CTRL, 0x02);
  231. dp_pll_write(dp_pll, QSERDES_COM_CLK_ENABLE1, 0x0c);
  232. dp_pll_write(dp_pll, QSERDES_COM_SYSCLK_BUF_ENABLE, 0x06);
  233. dp_pll_write(dp_pll, QSERDES_COM_CLK_SEL, 0x30);
  234. /* Make sure the PHY register writes are done */
  235. wmb();
  236. /* PLL Optimization */
  237. dp_pll_write(dp_pll, QSERDES_COM_PLL_IVCO, params->pll_ivco);
  238. dp_pll_write(dp_pll, QSERDES_COM_PLL_CCTRL_MODE0, 0x36);
  239. dp_pll_write(dp_pll, QSERDES_COM_PLL_RCTRL_MODE0, 0x16);
  240. dp_pll_write(dp_pll, QSERDES_COM_CP_CTRL_MODE0, 0x06);
  241. /* Make sure the PLL register writes are done */
  242. wmb();
  243. /* link rate dependent params */
  244. dp_pll_write(dp_pll, QSERDES_COM_HSCLK_SEL_1, params->hsclk_sel);
  245. dp_pll_write(dp_pll, QSERDES_COM_DEC_START_MODE0, params->dec_start_mode0);
  246. dp_pll_write(dp_pll,
  247. QSERDES_COM_DIV_FRAC_START1_MODE0, params->div_frac_start1_mode0);
  248. dp_pll_write(dp_pll,
  249. QSERDES_COM_DIV_FRAC_START2_MODE0, params->div_frac_start2_mode0);
  250. dp_pll_write(dp_pll,
  251. QSERDES_COM_DIV_FRAC_START3_MODE0, params->div_frac_start3_mode0);
  252. dp_pll_write(dp_pll, QSERDES_COM_LOCK_CMP1_MODE0, params->lock_cmp1_mode0);
  253. dp_pll_write(dp_pll, QSERDES_COM_LOCK_CMP2_MODE0, params->lock_cmp2_mode0);
  254. dp_pll_write(dp_pll, QSERDES_COM_LOCK_CMP_EN, params->lock_cmp_en);
  255. dp_pll_write(dp_phy, DP_PHY_VCO_DIV, params->phy_vco_div);
  256. /* Make sure the PLL register writes are done */
  257. wmb();
  258. dp_pll_write(dp_pll, QSERDES_COM_CMN_CONFIG_1, 0x12);
  259. dp_pll_write(dp_pll, QSERDES_COM_INTEGLOOP_GAIN0_MODE0,
  260. params->integloop_gain0_mode0);
  261. dp_pll_write(dp_pll, QSERDES_COM_INTEGLOOP_GAIN1_MODE0,
  262. params->integloop_gain1_mode0);
  263. dp_pll_write(dp_pll, QSERDES_COM_VCO_TUNE_MAP, 0x00);
  264. /* Make sure the PHY register writes are done */
  265. wmb();
  266. dp_pll_write(dp_pll, QSERDES_COM_BG_TIMER, params->bg_timer);
  267. dp_pll_write(dp_pll, QSERDES_COM_CORECLK_DIV_MODE0, 0x14);
  268. dp_pll_write(dp_pll, QSERDES_COM_VCO_TUNE_CTRL, 0x00);
  269. if (pll->bonding_en)
  270. dp_pll_write(dp_pll, QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x1f);
  271. else
  272. dp_pll_write(dp_pll, QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x17);
  273. dp_pll_write(dp_pll, QSERDES_COM_CORE_CLK_EN, params->core_clk_en);
  274. dp_pll_write(dp_pll, QSERDES_COM_BIN_VCOCAL_CMP_CODE1_MODE0,
  275. params->cmp_code1_mode0);
  276. dp_pll_write(dp_pll, QSERDES_COM_BIN_VCOCAL_CMP_CODE2_MODE0,
  277. params->cmp_code2_mode0);
  278. /* Make sure the PHY register writes are done */
  279. wmb();
  280. if (pll->ssc_en) {
  281. dp_pll_write(dp_pll, QSERDES_COM_SSC_EN_CENTER, 0x01);
  282. dp_pll_write(dp_pll, QSERDES_COM_SSC_ADJ_PER1, 0x00);
  283. dp_pll_write(dp_pll, QSERDES_COM_SSC_PER1, params->ssc_per1);
  284. dp_pll_write(dp_pll, QSERDES_COM_SSC_PER2, params->ssc_per2);
  285. dp_pll_write(dp_pll, QSERDES_COM_SSC_STEP_SIZE1_MODE0,
  286. params->ssc_step_size1_mode0);
  287. dp_pll_write(dp_pll, QSERDES_COM_SSC_STEP_SIZE2_MODE0,
  288. params->ssc_step_size2_mode0);
  289. }
  290. if (pdb->orientation == ORIENTATION_CC2)
  291. dp_pll_write(dp_phy, DP_PHY_MODE, 0x4c);
  292. else
  293. dp_pll_write(dp_phy, DP_PHY_MODE, 0x5c);
  294. dp_pll_write(dp_phy, DP_PHY_AUX_CFG1, 0x13);
  295. dp_pll_write(dp_phy, DP_PHY_AUX_CFG2, 0xA4);
  296. /* Make sure the PLL register writes are done */
  297. wmb();
  298. /* TX-0 register configuration */
  299. dp_pll_write(dp_phy, DP_PHY_TX0_TX1_LANE_CTL, 0x05);
  300. dp_pll_write(dp_ln_tx0, DP_VMODE_CTRL1, 0x40);
  301. dp_pll_write(dp_ln_tx0, TXn_PRE_STALL_LDO_BOOST_EN, 0x30);
  302. dp_pll_write(dp_ln_tx0, TXn_INTERFACE_SELECT, 0x3b);
  303. dp_pll_write(dp_ln_tx0, TXn_CLKBUF_ENABLE, 0x0f);
  304. dp_pll_write(dp_ln_tx0, TXn_RESET_TSYNC_EN, 0x03);
  305. dp_pll_write(dp_ln_tx0, DP_TRAN_DRVR_EMP_EN, 0xf);
  306. dp_pll_write(dp_ln_tx0, TXn_PARRATE_REC_DETECT_IDLE_EN, 0x00);
  307. dp_pll_write(dp_ln_tx0, DP_TX_INTERFACE_MODE, 0x00);
  308. dp_pll_write(dp_ln_tx0, TXn_RES_CODE_LANE_OFFSET_TX, params->lane_offset_tx);
  309. dp_pll_write(dp_ln_tx0, TXn_RES_CODE_LANE_OFFSET_RX, params->lane_offset_rx);
  310. dp_pll_write(dp_ln_tx0, TXn_TX_BAND, 0x04);
  311. /* Make sure the PLL register writes are done */
  312. wmb();
  313. /* TX-1 register configuration */
  314. dp_pll_write(dp_phy, DP_PHY_TX2_TX3_LANE_CTL, 0x05);
  315. dp_pll_write(dp_ln_tx1, DP_VMODE_CTRL1, 0x40);
  316. dp_pll_write(dp_ln_tx1, TXn_PRE_STALL_LDO_BOOST_EN, 0x30);
  317. dp_pll_write(dp_ln_tx1, TXn_INTERFACE_SELECT, 0x3b);
  318. dp_pll_write(dp_ln_tx1, TXn_CLKBUF_ENABLE, 0x0f);
  319. dp_pll_write(dp_ln_tx1, TXn_RESET_TSYNC_EN, 0x03);
  320. dp_pll_write(dp_ln_tx1, DP_TRAN_DRVR_EMP_EN, 0xf);
  321. dp_pll_write(dp_ln_tx1, TXn_PARRATE_REC_DETECT_IDLE_EN, 0x00);
  322. dp_pll_write(dp_ln_tx1, DP_TX_INTERFACE_MODE, 0x00);
  323. dp_pll_write(dp_ln_tx1, TXn_RES_CODE_LANE_OFFSET_TX, params->lane_offset_tx);
  324. dp_pll_write(dp_ln_tx1, TXn_RES_CODE_LANE_OFFSET_RX, params->lane_offset_rx);
  325. dp_pll_write(dp_ln_tx1, TXn_TX_BAND, 0x04);
  326. /* Make sure the PHY register writes are done */
  327. wmb();
  328. return set_vco_div(pll, rate);
  329. }
  330. enum dp_4nm_pll_status {
  331. C_READY,
  332. FREQ_DONE,
  333. PLL_LOCKED,
  334. PHY_READY,
  335. TSYNC_DONE,
  336. };
  337. char *dp_4nm_pll_get_status_name(enum dp_4nm_pll_status status)
  338. {
  339. switch (status) {
  340. case C_READY:
  341. return "C_READY";
  342. case FREQ_DONE:
  343. return "FREQ_DONE";
  344. case PLL_LOCKED:
  345. return "PLL_LOCKED";
  346. case PHY_READY:
  347. return "PHY_READY";
  348. case TSYNC_DONE:
  349. return "TSYNC_DONE";
  350. default:
  351. return "unknown";
  352. }
  353. }
  354. static bool dp_4nm_pll_get_status(struct dp_pll *pll,
  355. enum dp_4nm_pll_status status)
  356. {
  357. u32 reg, state, bit;
  358. void __iomem *base;
  359. bool success = true;
  360. switch (status) {
  361. case C_READY:
  362. base = dp_pll_get_base(dp_pll);
  363. reg = QSERDES_COM_C_READY_STATUS;
  364. bit = DP_4NM_C_READY;
  365. break;
  366. case FREQ_DONE:
  367. base = dp_pll_get_base(dp_pll);
  368. reg = QSERDES_COM_CMN_STATUS;
  369. bit = DP_4NM_FREQ_DONE;
  370. break;
  371. case PLL_LOCKED:
  372. base = dp_pll_get_base(dp_pll);
  373. reg = QSERDES_COM_CMN_STATUS;
  374. bit = DP_4NM_PLL_LOCKED;
  375. break;
  376. case PHY_READY:
  377. base = dp_pll_get_base(dp_phy);
  378. reg = DP_PHY_STATUS;
  379. bit = DP_4NM_PHY_READY;
  380. break;
  381. case TSYNC_DONE:
  382. base = dp_pll_get_base(dp_phy);
  383. reg = DP_PHY_STATUS;
  384. bit = DP_4NM_TSYNC_DONE;
  385. break;
  386. default:
  387. return false;
  388. }
  389. if (readl_poll_timeout_atomic((base + reg), state,
  390. ((state & bit) > 0),
  391. DP_PHY_PLL_POLL_SLEEP_US,
  392. DP_PHY_PLL_POLL_TIMEOUT_US)) {
  393. DP_ERR("%s failed, status=%x\n",
  394. dp_4nm_pll_get_status_name(status), state);
  395. success = false;
  396. }
  397. return success;
  398. }
  399. static int dp_pll_enable_4nm(struct dp_pll *pll)
  400. {
  401. int rc = 0;
  402. pll->aux->state &= ~DP_STATE_PLL_LOCKED;
  403. dp_pll_write(dp_phy, DP_PHY_CFG, 0x01);
  404. dp_pll_write(dp_phy, DP_PHY_CFG, 0x05);
  405. dp_pll_write(dp_phy, DP_PHY_CFG, 0x01);
  406. dp_pll_write(dp_phy, DP_PHY_CFG, 0x09);
  407. dp_pll_write(dp_pll, QSERDES_COM_RESETSM_CNTRL, 0x20);
  408. wmb(); /* Make sure the PLL register writes are done */
  409. if (!dp_4nm_pll_get_status(pll, C_READY)) {
  410. rc = -EINVAL;
  411. goto lock_err;
  412. }
  413. if (!dp_4nm_pll_get_status(pll, FREQ_DONE)) {
  414. rc = -EINVAL;
  415. goto lock_err;
  416. }
  417. if (!dp_4nm_pll_get_status(pll, PLL_LOCKED)) {
  418. rc = -EINVAL;
  419. goto lock_err;
  420. }
  421. dp_pll_write(dp_phy, DP_PHY_CFG, 0x19);
  422. /* Make sure the PHY register writes are done */
  423. wmb();
  424. if (!dp_4nm_pll_get_status(pll, TSYNC_DONE)) {
  425. rc = -EINVAL;
  426. goto lock_err;
  427. }
  428. if (!dp_4nm_pll_get_status(pll, PHY_READY)) {
  429. rc = -EINVAL;
  430. goto lock_err;
  431. }
  432. pll->aux->state |= DP_STATE_PLL_LOCKED;
  433. DP_DEBUG("PLL is locked\n");
  434. lock_err:
  435. return rc;
  436. }
  437. static void dp_pll_disable_4nm(struct dp_pll *pll)
  438. {
  439. /* Assert DP PHY power down */
  440. dp_pll_write(dp_phy, DP_PHY_PD_CTL, 0x2);
  441. /*
  442. * Make sure all the register writes to disable PLL are
  443. * completed before doing any other operation
  444. */
  445. wmb();
  446. }
  447. static int dp_vco_set_rate_4nm(struct dp_pll *pll, unsigned long rate)
  448. {
  449. int rc = 0;
  450. if (!pll) {
  451. DP_ERR("invalid input parameters\n");
  452. return -EINVAL;
  453. }
  454. DP_DEBUG("DP lane CLK rate=%ld\n", rate);
  455. rc = dp_config_vco_rate_4nm(pll, rate);
  456. if (rc < 0) {
  457. DP_ERR("Failed to set clk rate\n");
  458. return rc;
  459. }
  460. return rc;
  461. }
  462. static int dp_regulator_enable_4nm(struct dp_parser *parser,
  463. enum dp_pm_type pm_type, bool enable)
  464. {
  465. int rc = 0;
  466. struct dss_module_power mp;
  467. if (pm_type < DP_CORE_PM || pm_type >= DP_MAX_PM) {
  468. DP_ERR("invalid resource: %d %s\n", pm_type,
  469. dp_parser_pm_name(pm_type));
  470. return -EINVAL;
  471. }
  472. mp = parser->mp[pm_type];
  473. rc = msm_dss_enable_vreg(mp.vreg_config, mp.num_vreg, enable);
  474. if (rc) {
  475. DP_ERR("failed to '%s' vregs for %s\n",
  476. enable ? "enable" : "disable",
  477. dp_parser_pm_name(pm_type));
  478. return rc;
  479. }
  480. DP_DEBUG("success: '%s' vregs for %s\n", enable ? "enable" : "disable",
  481. dp_parser_pm_name(pm_type));
  482. return rc;
  483. }
  484. static int dp_pll_configure(struct dp_pll *pll, unsigned long rate)
  485. {
  486. int rc = 0;
  487. if (!pll || !rate) {
  488. DP_ERR("invalid input parameters rate = %lu\n", rate);
  489. return -EINVAL;
  490. }
  491. rate = rate * 10;
  492. if (rate <= DP_VCO_HSCLK_RATE_1620MHZDIV1000)
  493. rate = DP_VCO_HSCLK_RATE_1620MHZDIV1000;
  494. else if (rate <= DP_VCO_HSCLK_RATE_2700MHZDIV1000)
  495. rate = DP_VCO_HSCLK_RATE_2700MHZDIV1000;
  496. else if (rate <= DP_VCO_HSCLK_RATE_5400MHZDIV1000)
  497. rate = DP_VCO_HSCLK_RATE_5400MHZDIV1000;
  498. else
  499. rate = DP_VCO_HSCLK_RATE_8100MHZDIV1000;
  500. pll->vco_rate = rate;
  501. rc = dp_vco_set_rate_4nm(pll, rate);
  502. if (rc < 0) {
  503. DP_ERR("pll rate %s set failed\n", rate);
  504. pll->vco_rate = 0;
  505. return rc;
  506. }
  507. DP_DEBUG("pll rate %lu set success\n", rate);
  508. return rc;
  509. }
  510. static int dp_pll_prepare(struct dp_pll *pll)
  511. {
  512. int rc = 0;
  513. if (!pll) {
  514. DP_ERR("invalid input parameters\n");
  515. return -EINVAL;
  516. }
  517. /*
  518. * Enable DP_PM_PLL regulator if the PLL revision is 4nm-V1 and the
  519. * link rate is 8.1Gbps. This will result in voting to place Mx rail in
  520. * turbo as required for V1 hardware PLL functionality.
  521. */
  522. if (pll->revision >= DP_PLL_4NM_V1 &&
  523. pll->vco_rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000) {
  524. rc = dp_regulator_enable_4nm(pll->parser, DP_PLL_PM, true);
  525. if (rc < 0) {
  526. DP_ERR("enable pll power failed\n");
  527. return rc;
  528. }
  529. }
  530. rc = dp_pll_enable_4nm(pll);
  531. if (rc < 0)
  532. DP_ERR("ndx=%d failed to enable dp pll\n", pll->index);
  533. return rc;
  534. }
  535. static int dp_pll_unprepare(struct dp_pll *pll)
  536. {
  537. int rc = 0;
  538. if (!pll) {
  539. DP_ERR("invalid input parameter\n");
  540. return -EINVAL;
  541. }
  542. if (pll->revision >= DP_PLL_4NM_V1 &&
  543. pll->vco_rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000) {
  544. rc = dp_regulator_enable_4nm(pll->parser, DP_PLL_PM, false);
  545. if (rc < 0) {
  546. DP_ERR("disable pll power failed\n");
  547. return rc;
  548. }
  549. }
  550. dp_pll_disable_4nm(pll);
  551. pll->vco_rate = 0;
  552. return rc;
  553. }
  554. unsigned long dp_vco_recalc_rate_4nm(struct dp_pll *pll)
  555. {
  556. u32 hsclk_sel, link_clk_divsel, hsclk_div, link_clk_div = 0;
  557. unsigned long vco_rate = 0;
  558. if (!pll) {
  559. DP_ERR("invalid input parameters\n");
  560. return -EINVAL;
  561. }
  562. if (is_gdsc_disabled(pll))
  563. return 0;
  564. hsclk_sel = dp_pll_read(dp_pll, QSERDES_COM_HSCLK_SEL_1);
  565. hsclk_sel &= 0x0f;
  566. switch (hsclk_sel) {
  567. case 5:
  568. hsclk_div = 5;
  569. break;
  570. case 3:
  571. hsclk_div = 3;
  572. break;
  573. case 1:
  574. hsclk_div = 2;
  575. break;
  576. case 0:
  577. hsclk_div = 1;
  578. break;
  579. default:
  580. DP_DEBUG("unknown divider. forcing to default\n");
  581. hsclk_div = 5;
  582. break;
  583. }
  584. link_clk_divsel = dp_pll_read(dp_phy, DP_PHY_AUX_CFG2);
  585. link_clk_divsel >>= 2;
  586. link_clk_divsel &= 0x3;
  587. if (link_clk_divsel == 0)
  588. link_clk_div = 5;
  589. else if (link_clk_divsel == 1)
  590. link_clk_div = 10;
  591. else if (link_clk_divsel == 2)
  592. link_clk_div = 20;
  593. else
  594. DP_ERR("unsupported div. Phy_mode: %d\n", link_clk_divsel);
  595. if (link_clk_div == 20) {
  596. vco_rate = DP_VCO_HSCLK_RATE_2700MHZDIV1000;
  597. } else {
  598. if (hsclk_div == 5)
  599. vco_rate = DP_VCO_HSCLK_RATE_1620MHZDIV1000;
  600. else if (hsclk_div == 3)
  601. vco_rate = DP_VCO_HSCLK_RATE_2700MHZDIV1000;
  602. else if (hsclk_div == 2)
  603. vco_rate = DP_VCO_HSCLK_RATE_5400MHZDIV1000;
  604. else
  605. vco_rate = DP_VCO_HSCLK_RATE_8100MHZDIV1000;
  606. }
  607. DP_DEBUG("hsclk: sel=0x%x, div=0x%x; lclk: sel=%u, div=%u, rate=%lu\n",
  608. hsclk_sel, hsclk_div, link_clk_divsel, link_clk_div, vco_rate);
  609. return vco_rate;
  610. }
  611. static unsigned long dp_pll_link_clk_recalc_rate(struct clk_hw *hw,
  612. unsigned long parent_rate)
  613. {
  614. struct dp_pll *pll = NULL;
  615. struct dp_pll_vco_clk *pll_link = NULL;
  616. unsigned long rate = 0;
  617. if (!hw) {
  618. DP_ERR("invalid input parameters\n");
  619. return -EINVAL;
  620. }
  621. pll_link = to_dp_vco_hw(hw);
  622. pll = pll_link->priv;
  623. rate = pll->vco_rate * pll->clk_factor / 10;
  624. return rate;
  625. }
  626. static long dp_pll_link_clk_round(struct clk_hw *hw, unsigned long rate,
  627. unsigned long *parent_rate)
  628. {
  629. struct dp_pll *pll = NULL;
  630. struct dp_pll_vco_clk *pll_link = NULL;
  631. if (!hw) {
  632. DP_ERR("invalid input parameters\n");
  633. return -EINVAL;
  634. }
  635. pll_link = to_dp_vco_hw(hw);
  636. pll = pll_link->priv;
  637. rate = pll->vco_rate * pll->clk_factor / 10;
  638. return rate;
  639. }
  640. static unsigned long dp_pll_vco_div_clk_get_rate(struct dp_pll *pll)
  641. {
  642. if (pll->vco_rate == DP_VCO_HSCLK_RATE_8100MHZDIV1000)
  643. return ((pll->vco_rate / 6) * pll->clk_factor);
  644. else if (pll->vco_rate == DP_VCO_HSCLK_RATE_5400MHZDIV1000)
  645. return ((pll->vco_rate / 4) * pll->clk_factor);
  646. else
  647. return ((pll->vco_rate / 2) * pll->clk_factor);
  648. }
  649. static unsigned long dp_pll_vco_div_clk_recalc_rate(struct clk_hw *hw,
  650. unsigned long parent_rate)
  651. {
  652. struct dp_pll *pll = NULL;
  653. struct dp_pll_vco_clk *pll_link = NULL;
  654. if (!hw) {
  655. DP_ERR("invalid input parameters\n");
  656. return -EINVAL;
  657. }
  658. pll_link = to_dp_vco_hw(hw);
  659. pll = pll_link->priv;
  660. return dp_pll_vco_div_clk_get_rate(pll);
  661. }
  662. static long dp_pll_vco_div_clk_round(struct clk_hw *hw, unsigned long rate,
  663. unsigned long *parent_rate)
  664. {
  665. return dp_pll_vco_div_clk_recalc_rate(hw, *parent_rate);
  666. }
  667. static const struct clk_ops pll_link_clk_ops = {
  668. .recalc_rate = dp_pll_link_clk_recalc_rate,
  669. .round_rate = dp_pll_link_clk_round,
  670. };
  671. static const struct clk_ops pll_vco_div_clk_ops = {
  672. .recalc_rate = dp_pll_vco_div_clk_recalc_rate,
  673. .round_rate = dp_pll_vco_div_clk_round,
  674. };
  675. static struct dp_pll_vco_clk dp0_phy_pll_clks[DP_PLL_NUM_CLKS] = {
  676. {
  677. .hw.init = &(struct clk_init_data) {
  678. .name = "dp0_phy_pll_link_clk",
  679. .ops = &pll_link_clk_ops,
  680. },
  681. },
  682. {
  683. .hw.init = &(struct clk_init_data) {
  684. .name = "dp0_phy_pll_vco_div_clk",
  685. .ops = &pll_vco_div_clk_ops,
  686. },
  687. },
  688. };
  689. static struct dp_pll_vco_clk dp_phy_pll_clks[DP_PLL_NUM_CLKS] = {
  690. {
  691. .hw.init = &(struct clk_init_data) {
  692. .name = "dp_phy_pll_link_clk",
  693. .ops = &pll_link_clk_ops,
  694. },
  695. },
  696. {
  697. .hw.init = &(struct clk_init_data) {
  698. .name = "dp_phy_pll_vco_div_clk",
  699. .ops = &pll_vco_div_clk_ops,
  700. },
  701. },
  702. };
  703. static struct dp_pll_db dp_pdb;
  704. int dp_pll_clock_register_4nm(struct dp_pll *pll)
  705. {
  706. int rc = 0;
  707. struct platform_device *pdev;
  708. struct dp_pll_vco_clk *pll_clks;
  709. if (!pll) {
  710. DP_ERR("pll data not initialized\n");
  711. return -EINVAL;
  712. }
  713. pdev = pll->pdev;
  714. pll->clk_data = kzalloc(sizeof(*pll->clk_data), GFP_KERNEL);
  715. if (!pll->clk_data)
  716. return -ENOMEM;
  717. pll->clk_data->clks = kcalloc(DP_PLL_NUM_CLKS, sizeof(struct clk *),
  718. GFP_KERNEL);
  719. if (!pll->clk_data->clks) {
  720. kfree(pll->clk_data);
  721. return -ENOMEM;
  722. }
  723. pll->clk_data->clk_num = DP_PLL_NUM_CLKS;
  724. pll->priv = &dp_pdb;
  725. dp_pdb.pll = pll;
  726. if (pll->revision == DP_PLL_4NM_V1_1)
  727. dp_pdb.pll_params = pll_params_v1_1;
  728. else
  729. dp_pdb.pll_params = pll_params_v1;
  730. pll->pll_cfg = dp_pll_configure;
  731. pll->pll_prepare = dp_pll_prepare;
  732. pll->pll_unprepare = dp_pll_unprepare;
  733. if (pll->dp_core_revision >= 0x10040000)
  734. pll_clks = dp0_phy_pll_clks;
  735. else
  736. pll_clks = dp_phy_pll_clks;
  737. rc = dp_pll_clock_register_helper(pll, pll_clks, DP_PLL_NUM_CLKS);
  738. if (rc) {
  739. DP_ERR("Clock register failed rc=%d\n", rc);
  740. goto clk_reg_fail;
  741. }
  742. rc = of_clk_add_provider(pdev->dev.of_node,
  743. of_clk_src_onecell_get, pll->clk_data);
  744. if (rc) {
  745. DP_ERR("Clock add provider failed rc=%d\n", rc);
  746. goto clk_reg_fail;
  747. }
  748. DP_DEBUG("success\n");
  749. return rc;
  750. clk_reg_fail:
  751. dp_pll_clock_unregister_4nm(pll);
  752. return rc;
  753. }
  754. void dp_pll_clock_unregister_4nm(struct dp_pll *pll)
  755. {
  756. kfree(pll->clk_data->clks);
  757. kfree(pll->clk_data);
  758. }