phy-qcom-edp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017, 2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021, Linaro Ltd.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_address.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/reset.h>
  21. #include <linux/slab.h>
  22. #include <dt-bindings/phy/phy.h>
  23. #include "phy-qcom-qmp.h"
  24. /* EDP_PHY registers */
  25. #define DP_PHY_CFG 0x0010
  26. #define DP_PHY_CFG_1 0x0014
  27. #define DP_PHY_PD_CTL 0x001c
  28. #define DP_PHY_MODE 0x0020
  29. #define DP_PHY_AUX_CFG0 0x0024
  30. #define DP_PHY_AUX_CFG1 0x0028
  31. #define DP_PHY_AUX_CFG2 0x002C
  32. #define DP_PHY_AUX_CFG3 0x0030
  33. #define DP_PHY_AUX_CFG4 0x0034
  34. #define DP_PHY_AUX_CFG5 0x0038
  35. #define DP_PHY_AUX_CFG6 0x003C
  36. #define DP_PHY_AUX_CFG7 0x0040
  37. #define DP_PHY_AUX_CFG8 0x0044
  38. #define DP_PHY_AUX_CFG9 0x0048
  39. #define DP_PHY_AUX_INTERRUPT_MASK 0x0058
  40. #define DP_PHY_VCO_DIV 0x0074
  41. #define DP_PHY_TX0_TX1_LANE_CTL 0x007c
  42. #define DP_PHY_TX2_TX3_LANE_CTL 0x00a0
  43. #define DP_PHY_STATUS 0x00e0
  44. /* LANE_TXn registers */
  45. #define TXn_CLKBUF_ENABLE 0x0000
  46. #define TXn_TX_EMP_POST1_LVL 0x0004
  47. #define TXn_TX_DRV_LVL 0x0014
  48. #define TXn_TX_DRV_LVL_OFFSET 0x0018
  49. #define TXn_RESET_TSYNC_EN 0x001c
  50. #define TXn_LDO_CONFIG 0x0084
  51. #define TXn_TX_BAND 0x0028
  52. #define TXn_RES_CODE_LANE_OFFSET_TX0 0x0044
  53. #define TXn_RES_CODE_LANE_OFFSET_TX1 0x0048
  54. #define TXn_TRANSCEIVER_BIAS_EN 0x0054
  55. #define TXn_HIGHZ_DRVR_EN 0x0058
  56. #define TXn_TX_POL_INV 0x005c
  57. #define TXn_LANE_MODE_1 0x0064
  58. #define TXn_TRAN_DRVR_EMP_EN 0x0078
  59. struct qcom_edp_cfg {
  60. bool is_dp;
  61. /* DP PHY swing and pre_emphasis tables */
  62. const u8 (*swing_hbr_rbr)[4][4];
  63. const u8 (*swing_hbr3_hbr2)[4][4];
  64. const u8 (*pre_emphasis_hbr_rbr)[4][4];
  65. const u8 (*pre_emphasis_hbr3_hbr2)[4][4];
  66. };
  67. struct qcom_edp {
  68. struct device *dev;
  69. const struct qcom_edp_cfg *cfg;
  70. struct phy *phy;
  71. void __iomem *edp;
  72. void __iomem *tx0;
  73. void __iomem *tx1;
  74. void __iomem *pll;
  75. struct clk_hw dp_link_hw;
  76. struct clk_hw dp_pixel_hw;
  77. struct phy_configure_opts_dp dp_opts;
  78. struct clk_bulk_data clks[2];
  79. struct regulator_bulk_data supplies[2];
  80. };
  81. static const u8 dp_swing_hbr_rbr[4][4] = {
  82. { 0x08, 0x0f, 0x16, 0x1f },
  83. { 0x11, 0x1e, 0x1f, 0xff },
  84. { 0x16, 0x1f, 0xff, 0xff },
  85. { 0x1f, 0xff, 0xff, 0xff }
  86. };
  87. static const u8 dp_pre_emp_hbr_rbr[4][4] = {
  88. { 0x00, 0x0d, 0x14, 0x1a },
  89. { 0x00, 0x0e, 0x15, 0xff },
  90. { 0x00, 0x0e, 0xff, 0xff },
  91. { 0x03, 0xff, 0xff, 0xff }
  92. };
  93. static const u8 dp_swing_hbr2_hbr3[4][4] = {
  94. { 0x02, 0x12, 0x16, 0x1a },
  95. { 0x09, 0x19, 0x1f, 0xff },
  96. { 0x10, 0x1f, 0xff, 0xff },
  97. { 0x1f, 0xff, 0xff, 0xff }
  98. };
  99. static const u8 dp_pre_emp_hbr2_hbr3[4][4] = {
  100. { 0x00, 0x0c, 0x15, 0x1b },
  101. { 0x02, 0x0e, 0x16, 0xff },
  102. { 0x02, 0x11, 0xff, 0xff },
  103. { 0x04, 0xff, 0xff, 0xff }
  104. };
  105. static const struct qcom_edp_cfg dp_phy_cfg = {
  106. .is_dp = true,
  107. .swing_hbr_rbr = &dp_swing_hbr_rbr,
  108. .swing_hbr3_hbr2 = &dp_swing_hbr2_hbr3,
  109. .pre_emphasis_hbr_rbr = &dp_pre_emp_hbr_rbr,
  110. .pre_emphasis_hbr3_hbr2 = &dp_pre_emp_hbr2_hbr3,
  111. };
  112. static const u8 edp_swing_hbr_rbr[4][4] = {
  113. { 0x07, 0x0f, 0x16, 0x1f },
  114. { 0x0d, 0x16, 0x1e, 0xff },
  115. { 0x11, 0x1b, 0xff, 0xff },
  116. { 0x16, 0xff, 0xff, 0xff }
  117. };
  118. static const u8 edp_pre_emp_hbr_rbr[4][4] = {
  119. { 0x05, 0x12, 0x17, 0x1d },
  120. { 0x05, 0x11, 0x18, 0xff },
  121. { 0x06, 0x11, 0xff, 0xff },
  122. { 0x00, 0xff, 0xff, 0xff }
  123. };
  124. static const u8 edp_swing_hbr2_hbr3[4][4] = {
  125. { 0x0b, 0x11, 0x17, 0x1c },
  126. { 0x10, 0x19, 0x1f, 0xff },
  127. { 0x19, 0x1f, 0xff, 0xff },
  128. { 0x1f, 0xff, 0xff, 0xff }
  129. };
  130. static const u8 edp_pre_emp_hbr2_hbr3[4][4] = {
  131. { 0x08, 0x11, 0x17, 0x1b },
  132. { 0x00, 0x0c, 0x13, 0xff },
  133. { 0x05, 0x10, 0xff, 0xff },
  134. { 0x00, 0xff, 0xff, 0xff }
  135. };
  136. static const struct qcom_edp_cfg edp_phy_cfg = {
  137. .is_dp = false,
  138. .swing_hbr_rbr = &edp_swing_hbr_rbr,
  139. .swing_hbr3_hbr2 = &edp_swing_hbr2_hbr3,
  140. .pre_emphasis_hbr_rbr = &edp_pre_emp_hbr_rbr,
  141. .pre_emphasis_hbr3_hbr2 = &edp_pre_emp_hbr2_hbr3,
  142. };
  143. static int qcom_edp_phy_init(struct phy *phy)
  144. {
  145. struct qcom_edp *edp = phy_get_drvdata(phy);
  146. const struct qcom_edp_cfg *cfg = edp->cfg;
  147. int ret;
  148. u8 cfg8;
  149. ret = regulator_bulk_enable(ARRAY_SIZE(edp->supplies), edp->supplies);
  150. if (ret)
  151. return ret;
  152. ret = clk_bulk_prepare_enable(ARRAY_SIZE(edp->clks), edp->clks);
  153. if (ret)
  154. goto out_disable_supplies;
  155. writel(DP_PHY_PD_CTL_PWRDN | DP_PHY_PD_CTL_AUX_PWRDN |
  156. DP_PHY_PD_CTL_PLL_PWRDN | DP_PHY_PD_CTL_DP_CLAMP_EN,
  157. edp->edp + DP_PHY_PD_CTL);
  158. /* Turn on BIAS current for PHY/PLL */
  159. writel(0x17, edp->pll + QSERDES_V4_COM_BIAS_EN_CLKBUFLR_EN);
  160. writel(DP_PHY_PD_CTL_PSR_PWRDN, edp->edp + DP_PHY_PD_CTL);
  161. msleep(20);
  162. writel(DP_PHY_PD_CTL_PWRDN | DP_PHY_PD_CTL_AUX_PWRDN |
  163. DP_PHY_PD_CTL_LANE_0_1_PWRDN | DP_PHY_PD_CTL_LANE_2_3_PWRDN |
  164. DP_PHY_PD_CTL_PLL_PWRDN | DP_PHY_PD_CTL_DP_CLAMP_EN,
  165. edp->edp + DP_PHY_PD_CTL);
  166. if (cfg && cfg->is_dp)
  167. cfg8 = 0xb7;
  168. else
  169. cfg8 = 0x37;
  170. writel(0xfc, edp->edp + DP_PHY_MODE);
  171. writel(0x00, edp->edp + DP_PHY_AUX_CFG0);
  172. writel(0x13, edp->edp + DP_PHY_AUX_CFG1);
  173. writel(0x24, edp->edp + DP_PHY_AUX_CFG2);
  174. writel(0x00, edp->edp + DP_PHY_AUX_CFG3);
  175. writel(0x0a, edp->edp + DP_PHY_AUX_CFG4);
  176. writel(0x26, edp->edp + DP_PHY_AUX_CFG5);
  177. writel(0x0a, edp->edp + DP_PHY_AUX_CFG6);
  178. writel(0x03, edp->edp + DP_PHY_AUX_CFG7);
  179. writel(cfg8, edp->edp + DP_PHY_AUX_CFG8);
  180. writel(0x03, edp->edp + DP_PHY_AUX_CFG9);
  181. writel(PHY_AUX_STOP_ERR_MASK | PHY_AUX_DEC_ERR_MASK |
  182. PHY_AUX_SYNC_ERR_MASK | PHY_AUX_ALIGN_ERR_MASK |
  183. PHY_AUX_REQ_ERR_MASK, edp->edp + DP_PHY_AUX_INTERRUPT_MASK);
  184. msleep(20);
  185. return 0;
  186. out_disable_supplies:
  187. regulator_bulk_disable(ARRAY_SIZE(edp->supplies), edp->supplies);
  188. return ret;
  189. }
  190. static int qcom_edp_set_voltages(struct qcom_edp *edp, const struct phy_configure_opts_dp *dp_opts)
  191. {
  192. const struct qcom_edp_cfg *cfg = edp->cfg;
  193. unsigned int v_level = 0;
  194. unsigned int p_level = 0;
  195. u8 ldo_config;
  196. u8 swing;
  197. u8 emph;
  198. int i;
  199. if (!cfg)
  200. return 0;
  201. for (i = 0; i < dp_opts->lanes; i++) {
  202. v_level = max(v_level, dp_opts->voltage[i]);
  203. p_level = max(p_level, dp_opts->pre[i]);
  204. }
  205. if (dp_opts->link_rate <= 2700) {
  206. swing = (*cfg->swing_hbr_rbr)[v_level][p_level];
  207. emph = (*cfg->pre_emphasis_hbr_rbr)[v_level][p_level];
  208. } else {
  209. swing = (*cfg->swing_hbr3_hbr2)[v_level][p_level];
  210. emph = (*cfg->pre_emphasis_hbr3_hbr2)[v_level][p_level];
  211. }
  212. if (swing == 0xff || emph == 0xff)
  213. return -EINVAL;
  214. ldo_config = (cfg && cfg->is_dp) ? 0x1 : 0x0;
  215. writel(ldo_config, edp->tx0 + TXn_LDO_CONFIG);
  216. writel(swing, edp->tx0 + TXn_TX_DRV_LVL);
  217. writel(emph, edp->tx0 + TXn_TX_EMP_POST1_LVL);
  218. writel(ldo_config, edp->tx1 + TXn_LDO_CONFIG);
  219. writel(swing, edp->tx1 + TXn_TX_DRV_LVL);
  220. writel(emph, edp->tx1 + TXn_TX_EMP_POST1_LVL);
  221. return 0;
  222. }
  223. static int qcom_edp_phy_configure(struct phy *phy, union phy_configure_opts *opts)
  224. {
  225. const struct phy_configure_opts_dp *dp_opts = &opts->dp;
  226. struct qcom_edp *edp = phy_get_drvdata(phy);
  227. int ret = 0;
  228. memcpy(&edp->dp_opts, dp_opts, sizeof(*dp_opts));
  229. if (dp_opts->set_voltages)
  230. ret = qcom_edp_set_voltages(edp, dp_opts);
  231. return ret;
  232. }
  233. static int qcom_edp_configure_ssc(const struct qcom_edp *edp)
  234. {
  235. const struct phy_configure_opts_dp *dp_opts = &edp->dp_opts;
  236. u32 step1;
  237. u32 step2;
  238. switch (dp_opts->link_rate) {
  239. case 1620:
  240. case 2700:
  241. case 8100:
  242. step1 = 0x45;
  243. step2 = 0x06;
  244. break;
  245. case 5400:
  246. step1 = 0x5c;
  247. step2 = 0x08;
  248. break;
  249. default:
  250. /* Other link rates aren't supported */
  251. return -EINVAL;
  252. }
  253. writel(0x01, edp->pll + QSERDES_V4_COM_SSC_EN_CENTER);
  254. writel(0x00, edp->pll + QSERDES_V4_COM_SSC_ADJ_PER1);
  255. writel(0x36, edp->pll + QSERDES_V4_COM_SSC_PER1);
  256. writel(0x01, edp->pll + QSERDES_V4_COM_SSC_PER2);
  257. writel(step1, edp->pll + QSERDES_V4_COM_SSC_STEP_SIZE1_MODE0);
  258. writel(step2, edp->pll + QSERDES_V4_COM_SSC_STEP_SIZE2_MODE0);
  259. return 0;
  260. }
  261. static int qcom_edp_configure_pll(const struct qcom_edp *edp)
  262. {
  263. const struct phy_configure_opts_dp *dp_opts = &edp->dp_opts;
  264. u32 div_frac_start2_mode0;
  265. u32 div_frac_start3_mode0;
  266. u32 dec_start_mode0;
  267. u32 lock_cmp1_mode0;
  268. u32 lock_cmp2_mode0;
  269. u32 hsclk_sel;
  270. switch (dp_opts->link_rate) {
  271. case 1620:
  272. hsclk_sel = 0x5;
  273. dec_start_mode0 = 0x69;
  274. div_frac_start2_mode0 = 0x80;
  275. div_frac_start3_mode0 = 0x07;
  276. lock_cmp1_mode0 = 0x6f;
  277. lock_cmp2_mode0 = 0x08;
  278. break;
  279. case 2700:
  280. hsclk_sel = 0x3;
  281. dec_start_mode0 = 0x69;
  282. div_frac_start2_mode0 = 0x80;
  283. div_frac_start3_mode0 = 0x07;
  284. lock_cmp1_mode0 = 0x0f;
  285. lock_cmp2_mode0 = 0x0e;
  286. break;
  287. case 5400:
  288. hsclk_sel = 0x1;
  289. dec_start_mode0 = 0x8c;
  290. div_frac_start2_mode0 = 0x00;
  291. div_frac_start3_mode0 = 0x0a;
  292. lock_cmp1_mode0 = 0x1f;
  293. lock_cmp2_mode0 = 0x1c;
  294. break;
  295. case 8100:
  296. hsclk_sel = 0x0;
  297. dec_start_mode0 = 0x69;
  298. div_frac_start2_mode0 = 0x80;
  299. div_frac_start3_mode0 = 0x07;
  300. lock_cmp1_mode0 = 0x2f;
  301. lock_cmp2_mode0 = 0x2a;
  302. break;
  303. default:
  304. /* Other link rates aren't supported */
  305. return -EINVAL;
  306. }
  307. writel(0x01, edp->pll + QSERDES_V4_COM_SVS_MODE_CLK_SEL);
  308. writel(0x0b, edp->pll + QSERDES_V4_COM_SYSCLK_EN_SEL);
  309. writel(0x02, edp->pll + QSERDES_V4_COM_SYS_CLK_CTRL);
  310. writel(0x0c, edp->pll + QSERDES_V4_COM_CLK_ENABLE1);
  311. writel(0x06, edp->pll + QSERDES_V4_COM_SYSCLK_BUF_ENABLE);
  312. writel(0x30, edp->pll + QSERDES_V4_COM_CLK_SELECT);
  313. writel(hsclk_sel, edp->pll + QSERDES_V4_COM_HSCLK_SEL);
  314. writel(0x0f, edp->pll + QSERDES_V4_COM_PLL_IVCO);
  315. writel(0x08, edp->pll + QSERDES_V4_COM_LOCK_CMP_EN);
  316. writel(0x36, edp->pll + QSERDES_V4_COM_PLL_CCTRL_MODE0);
  317. writel(0x16, edp->pll + QSERDES_V4_COM_PLL_RCTRL_MODE0);
  318. writel(0x06, edp->pll + QSERDES_V4_COM_CP_CTRL_MODE0);
  319. writel(dec_start_mode0, edp->pll + QSERDES_V4_COM_DEC_START_MODE0);
  320. writel(0x00, edp->pll + QSERDES_V4_COM_DIV_FRAC_START1_MODE0);
  321. writel(div_frac_start2_mode0, edp->pll + QSERDES_V4_COM_DIV_FRAC_START2_MODE0);
  322. writel(div_frac_start3_mode0, edp->pll + QSERDES_V4_COM_DIV_FRAC_START3_MODE0);
  323. writel(0x02, edp->pll + QSERDES_V4_COM_CMN_CONFIG);
  324. writel(0x3f, edp->pll + QSERDES_V4_COM_INTEGLOOP_GAIN0_MODE0);
  325. writel(0x00, edp->pll + QSERDES_V4_COM_INTEGLOOP_GAIN1_MODE0);
  326. writel(0x00, edp->pll + QSERDES_V4_COM_VCO_TUNE_MAP);
  327. writel(lock_cmp1_mode0, edp->pll + QSERDES_V4_COM_LOCK_CMP1_MODE0);
  328. writel(lock_cmp2_mode0, edp->pll + QSERDES_V4_COM_LOCK_CMP2_MODE0);
  329. writel(0x0a, edp->pll + QSERDES_V4_COM_BG_TIMER);
  330. writel(0x14, edp->pll + QSERDES_V4_COM_CORECLK_DIV_MODE0);
  331. writel(0x00, edp->pll + QSERDES_V4_COM_VCO_TUNE_CTRL);
  332. writel(0x17, edp->pll + QSERDES_V4_COM_BIAS_EN_CLKBUFLR_EN);
  333. writel(0x0f, edp->pll + QSERDES_V4_COM_CORE_CLK_EN);
  334. writel(0xa0, edp->pll + QSERDES_V4_COM_VCO_TUNE1_MODE0);
  335. writel(0x03, edp->pll + QSERDES_V4_COM_VCO_TUNE2_MODE0);
  336. return 0;
  337. }
  338. static int qcom_edp_set_vco_div(const struct qcom_edp *edp, unsigned long *pixel_freq)
  339. {
  340. const struct phy_configure_opts_dp *dp_opts = &edp->dp_opts;
  341. u32 vco_div;
  342. switch (dp_opts->link_rate) {
  343. case 1620:
  344. vco_div = 0x1;
  345. *pixel_freq = 1620000000UL / 2;
  346. break;
  347. case 2700:
  348. vco_div = 0x1;
  349. *pixel_freq = 2700000000UL / 2;
  350. break;
  351. case 5400:
  352. vco_div = 0x2;
  353. *pixel_freq = 5400000000UL / 4;
  354. break;
  355. case 8100:
  356. vco_div = 0x0;
  357. *pixel_freq = 8100000000UL / 6;
  358. break;
  359. default:
  360. /* Other link rates aren't supported */
  361. return -EINVAL;
  362. }
  363. writel(vco_div, edp->edp + DP_PHY_VCO_DIV);
  364. return 0;
  365. }
  366. static int qcom_edp_phy_power_on(struct phy *phy)
  367. {
  368. const struct qcom_edp *edp = phy_get_drvdata(phy);
  369. const struct qcom_edp_cfg *cfg = edp->cfg;
  370. u32 bias0_en, drvr0_en, bias1_en, drvr1_en;
  371. unsigned long pixel_freq;
  372. u8 ldo_config;
  373. int timeout;
  374. int ret;
  375. u32 val;
  376. u8 cfg1;
  377. writel(DP_PHY_PD_CTL_PWRDN | DP_PHY_PD_CTL_AUX_PWRDN |
  378. DP_PHY_PD_CTL_LANE_0_1_PWRDN | DP_PHY_PD_CTL_LANE_2_3_PWRDN |
  379. DP_PHY_PD_CTL_PLL_PWRDN | DP_PHY_PD_CTL_DP_CLAMP_EN,
  380. edp->edp + DP_PHY_PD_CTL);
  381. writel(0xfc, edp->edp + DP_PHY_MODE);
  382. timeout = readl_poll_timeout(edp->pll + QSERDES_V4_COM_CMN_STATUS,
  383. val, val & BIT(7), 5, 200);
  384. if (timeout)
  385. return timeout;
  386. ldo_config = (cfg && cfg->is_dp) ? 0x1 : 0x0;
  387. writel(ldo_config, edp->tx0 + TXn_LDO_CONFIG);
  388. writel(ldo_config, edp->tx1 + TXn_LDO_CONFIG);
  389. writel(0x00, edp->tx0 + TXn_LANE_MODE_1);
  390. writel(0x00, edp->tx1 + TXn_LANE_MODE_1);
  391. if (edp->dp_opts.ssc) {
  392. ret = qcom_edp_configure_ssc(edp);
  393. if (ret)
  394. return ret;
  395. }
  396. ret = qcom_edp_configure_pll(edp);
  397. if (ret)
  398. return ret;
  399. /* TX Lane configuration */
  400. writel(0x05, edp->edp + DP_PHY_TX0_TX1_LANE_CTL);
  401. writel(0x05, edp->edp + DP_PHY_TX2_TX3_LANE_CTL);
  402. /* TX-0 register configuration */
  403. writel(0x03, edp->tx0 + TXn_TRANSCEIVER_BIAS_EN);
  404. writel(0x0f, edp->tx0 + TXn_CLKBUF_ENABLE);
  405. writel(0x03, edp->tx0 + TXn_RESET_TSYNC_EN);
  406. writel(0x01, edp->tx0 + TXn_TRAN_DRVR_EMP_EN);
  407. writel(0x04, edp->tx0 + TXn_TX_BAND);
  408. /* TX-1 register configuration */
  409. writel(0x03, edp->tx1 + TXn_TRANSCEIVER_BIAS_EN);
  410. writel(0x0f, edp->tx1 + TXn_CLKBUF_ENABLE);
  411. writel(0x03, edp->tx1 + TXn_RESET_TSYNC_EN);
  412. writel(0x01, edp->tx1 + TXn_TRAN_DRVR_EMP_EN);
  413. writel(0x04, edp->tx1 + TXn_TX_BAND);
  414. ret = qcom_edp_set_vco_div(edp, &pixel_freq);
  415. if (ret)
  416. return ret;
  417. writel(0x01, edp->edp + DP_PHY_CFG);
  418. writel(0x05, edp->edp + DP_PHY_CFG);
  419. writel(0x01, edp->edp + DP_PHY_CFG);
  420. writel(0x09, edp->edp + DP_PHY_CFG);
  421. writel(0x20, edp->pll + QSERDES_V4_COM_RESETSM_CNTRL);
  422. timeout = readl_poll_timeout(edp->pll + QSERDES_V4_COM_C_READY_STATUS,
  423. val, val & BIT(0), 500, 10000);
  424. if (timeout)
  425. return timeout;
  426. writel(0x19, edp->edp + DP_PHY_CFG);
  427. writel(0x1f, edp->tx0 + TXn_HIGHZ_DRVR_EN);
  428. writel(0x04, edp->tx0 + TXn_HIGHZ_DRVR_EN);
  429. writel(0x00, edp->tx0 + TXn_TX_POL_INV);
  430. writel(0x1f, edp->tx1 + TXn_HIGHZ_DRVR_EN);
  431. writel(0x04, edp->tx1 + TXn_HIGHZ_DRVR_EN);
  432. writel(0x00, edp->tx1 + TXn_TX_POL_INV);
  433. writel(0x10, edp->tx0 + TXn_TX_DRV_LVL_OFFSET);
  434. writel(0x10, edp->tx1 + TXn_TX_DRV_LVL_OFFSET);
  435. writel(0x11, edp->tx0 + TXn_RES_CODE_LANE_OFFSET_TX0);
  436. writel(0x11, edp->tx0 + TXn_RES_CODE_LANE_OFFSET_TX1);
  437. writel(0x11, edp->tx1 + TXn_RES_CODE_LANE_OFFSET_TX0);
  438. writel(0x11, edp->tx1 + TXn_RES_CODE_LANE_OFFSET_TX1);
  439. writel(0x10, edp->tx0 + TXn_TX_EMP_POST1_LVL);
  440. writel(0x10, edp->tx1 + TXn_TX_EMP_POST1_LVL);
  441. writel(0x1f, edp->tx0 + TXn_TX_DRV_LVL);
  442. writel(0x1f, edp->tx1 + TXn_TX_DRV_LVL);
  443. if (edp->dp_opts.lanes == 1) {
  444. bias0_en = 0x01;
  445. bias1_en = 0x00;
  446. drvr0_en = 0x06;
  447. drvr1_en = 0x07;
  448. cfg1 = 0x1;
  449. } else if (edp->dp_opts.lanes == 2) {
  450. bias0_en = 0x03;
  451. bias1_en = 0x00;
  452. drvr0_en = 0x04;
  453. drvr1_en = 0x07;
  454. cfg1 = 0x3;
  455. } else {
  456. bias0_en = 0x03;
  457. bias1_en = 0x03;
  458. drvr0_en = 0x04;
  459. drvr1_en = 0x04;
  460. cfg1 = 0xf;
  461. }
  462. writel(drvr0_en, edp->tx0 + TXn_HIGHZ_DRVR_EN);
  463. writel(bias0_en, edp->tx0 + TXn_TRANSCEIVER_BIAS_EN);
  464. writel(drvr1_en, edp->tx1 + TXn_HIGHZ_DRVR_EN);
  465. writel(bias1_en, edp->tx1 + TXn_TRANSCEIVER_BIAS_EN);
  466. writel(cfg1, edp->edp + DP_PHY_CFG_1);
  467. writel(0x18, edp->edp + DP_PHY_CFG);
  468. usleep_range(100, 1000);
  469. writel(0x19, edp->edp + DP_PHY_CFG);
  470. ret = readl_poll_timeout(edp->edp + DP_PHY_STATUS,
  471. val, val & BIT(1), 500, 10000);
  472. if (ret)
  473. return ret;
  474. clk_set_rate(edp->dp_link_hw.clk, edp->dp_opts.link_rate * 100000);
  475. clk_set_rate(edp->dp_pixel_hw.clk, pixel_freq);
  476. return 0;
  477. }
  478. static int qcom_edp_phy_power_off(struct phy *phy)
  479. {
  480. const struct qcom_edp *edp = phy_get_drvdata(phy);
  481. writel(DP_PHY_PD_CTL_PSR_PWRDN, edp->edp + DP_PHY_PD_CTL);
  482. return 0;
  483. }
  484. static int qcom_edp_phy_exit(struct phy *phy)
  485. {
  486. struct qcom_edp *edp = phy_get_drvdata(phy);
  487. clk_bulk_disable_unprepare(ARRAY_SIZE(edp->clks), edp->clks);
  488. regulator_bulk_disable(ARRAY_SIZE(edp->supplies), edp->supplies);
  489. return 0;
  490. }
  491. static const struct phy_ops qcom_edp_ops = {
  492. .init = qcom_edp_phy_init,
  493. .configure = qcom_edp_phy_configure,
  494. .power_on = qcom_edp_phy_power_on,
  495. .power_off = qcom_edp_phy_power_off,
  496. .exit = qcom_edp_phy_exit,
  497. .owner = THIS_MODULE,
  498. };
  499. /*
  500. * Embedded Display Port PLL driver block diagram for branch clocks
  501. *
  502. * +------------------------------+
  503. * | EDP_VCO_CLK |
  504. * | |
  505. * | +-------------------+ |
  506. * | | (EDP PLL/VCO) | |
  507. * | +---------+---------+ |
  508. * | v |
  509. * | +----------+-----------+ |
  510. * | | hsclk_divsel_clk_src | |
  511. * | +----------+-----------+ |
  512. * +------------------------------+
  513. * |
  514. * +---------<---------v------------>----------+
  515. * | |
  516. * +--------v----------------+ |
  517. * | edp_phy_pll_link_clk | |
  518. * | link_clk | |
  519. * +--------+----------------+ |
  520. * | |
  521. * | |
  522. * v v
  523. * Input to DISPCC block |
  524. * for link clk, crypto clk |
  525. * and interface clock |
  526. * |
  527. * |
  528. * +--------<------------+-----------------+---<---+
  529. * | | |
  530. * +----v---------+ +--------v-----+ +--------v------+
  531. * | vco_divided | | vco_divided | | vco_divided |
  532. * | _clk_src | | _clk_src | | _clk_src |
  533. * | | | | | |
  534. * |divsel_six | | divsel_two | | divsel_four |
  535. * +-------+------+ +-----+--------+ +--------+------+
  536. * | | |
  537. * v---->----------v-------------<------v
  538. * |
  539. * +----------+-----------------+
  540. * | edp_phy_pll_vco_div_clk |
  541. * +---------+------------------+
  542. * |
  543. * v
  544. * Input to DISPCC block
  545. * for EDP pixel clock
  546. *
  547. */
  548. static int qcom_edp_dp_pixel_clk_determine_rate(struct clk_hw *hw,
  549. struct clk_rate_request *req)
  550. {
  551. switch (req->rate) {
  552. case 1620000000UL / 2:
  553. case 2700000000UL / 2:
  554. /* 5.4 and 8.1 GHz are same link rate as 2.7GHz, i.e. div 4 and div 6 */
  555. return 0;
  556. default:
  557. return -EINVAL;
  558. }
  559. }
  560. static unsigned long
  561. qcom_edp_dp_pixel_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  562. {
  563. const struct qcom_edp *edp = container_of(hw, struct qcom_edp, dp_pixel_hw);
  564. const struct phy_configure_opts_dp *dp_opts = &edp->dp_opts;
  565. switch (dp_opts->link_rate) {
  566. case 1620:
  567. return 1620000000UL / 2;
  568. case 2700:
  569. return 2700000000UL / 2;
  570. case 5400:
  571. return 5400000000UL / 4;
  572. case 8100:
  573. return 8100000000UL / 6;
  574. default:
  575. return 0;
  576. }
  577. }
  578. static const struct clk_ops qcom_edp_dp_pixel_clk_ops = {
  579. .determine_rate = qcom_edp_dp_pixel_clk_determine_rate,
  580. .recalc_rate = qcom_edp_dp_pixel_clk_recalc_rate,
  581. };
  582. static int qcom_edp_dp_link_clk_determine_rate(struct clk_hw *hw,
  583. struct clk_rate_request *req)
  584. {
  585. switch (req->rate) {
  586. case 162000000:
  587. case 270000000:
  588. case 540000000:
  589. case 810000000:
  590. return 0;
  591. default:
  592. return -EINVAL;
  593. }
  594. }
  595. static unsigned long
  596. qcom_edp_dp_link_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  597. {
  598. const struct qcom_edp *edp = container_of(hw, struct qcom_edp, dp_link_hw);
  599. const struct phy_configure_opts_dp *dp_opts = &edp->dp_opts;
  600. switch (dp_opts->link_rate) {
  601. case 1620:
  602. case 2700:
  603. case 5400:
  604. case 8100:
  605. return dp_opts->link_rate * 100000;
  606. default:
  607. return 0;
  608. }
  609. }
  610. static const struct clk_ops qcom_edp_dp_link_clk_ops = {
  611. .determine_rate = qcom_edp_dp_link_clk_determine_rate,
  612. .recalc_rate = qcom_edp_dp_link_clk_recalc_rate,
  613. };
  614. static int qcom_edp_clks_register(struct qcom_edp *edp, struct device_node *np)
  615. {
  616. struct clk_hw_onecell_data *data;
  617. struct clk_init_data init = { };
  618. char name[64];
  619. int ret;
  620. data = devm_kzalloc(edp->dev, struct_size(data, hws, 2), GFP_KERNEL);
  621. if (!data)
  622. return -ENOMEM;
  623. snprintf(name, sizeof(name), "%s::link_clk", dev_name(edp->dev));
  624. init.ops = &qcom_edp_dp_link_clk_ops;
  625. init.name = name;
  626. edp->dp_link_hw.init = &init;
  627. ret = devm_clk_hw_register(edp->dev, &edp->dp_link_hw);
  628. if (ret)
  629. return ret;
  630. snprintf(name, sizeof(name), "%s::vco_div_clk", dev_name(edp->dev));
  631. init.ops = &qcom_edp_dp_pixel_clk_ops;
  632. init.name = name;
  633. edp->dp_pixel_hw.init = &init;
  634. ret = devm_clk_hw_register(edp->dev, &edp->dp_pixel_hw);
  635. if (ret)
  636. return ret;
  637. data->hws[0] = &edp->dp_link_hw;
  638. data->hws[1] = &edp->dp_pixel_hw;
  639. data->num = 2;
  640. return devm_of_clk_add_hw_provider(edp->dev, of_clk_hw_onecell_get, data);
  641. }
  642. static int qcom_edp_phy_probe(struct platform_device *pdev)
  643. {
  644. struct phy_provider *phy_provider;
  645. struct device *dev = &pdev->dev;
  646. struct qcom_edp *edp;
  647. int ret;
  648. edp = devm_kzalloc(dev, sizeof(*edp), GFP_KERNEL);
  649. if (!edp)
  650. return -ENOMEM;
  651. edp->dev = dev;
  652. edp->cfg = of_device_get_match_data(&pdev->dev);
  653. edp->edp = devm_platform_ioremap_resource(pdev, 0);
  654. if (IS_ERR(edp->edp))
  655. return PTR_ERR(edp->edp);
  656. edp->tx0 = devm_platform_ioremap_resource(pdev, 1);
  657. if (IS_ERR(edp->tx0))
  658. return PTR_ERR(edp->tx0);
  659. edp->tx1 = devm_platform_ioremap_resource(pdev, 2);
  660. if (IS_ERR(edp->tx1))
  661. return PTR_ERR(edp->tx1);
  662. edp->pll = devm_platform_ioremap_resource(pdev, 3);
  663. if (IS_ERR(edp->pll))
  664. return PTR_ERR(edp->pll);
  665. edp->clks[0].id = "aux";
  666. edp->clks[1].id = "cfg_ahb";
  667. ret = devm_clk_bulk_get(dev, ARRAY_SIZE(edp->clks), edp->clks);
  668. if (ret)
  669. return ret;
  670. edp->supplies[0].supply = "vdda-phy";
  671. edp->supplies[1].supply = "vdda-pll";
  672. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(edp->supplies), edp->supplies);
  673. if (ret)
  674. return ret;
  675. ret = regulator_set_load(edp->supplies[0].consumer, 21800); /* 1.2 V vdda-phy */
  676. if (ret) {
  677. dev_err(dev, "failed to set load at %s\n", edp->supplies[0].supply);
  678. return ret;
  679. }
  680. ret = regulator_set_load(edp->supplies[1].consumer, 36000); /* 0.9 V vdda-pll */
  681. if (ret) {
  682. dev_err(dev, "failed to set load at %s\n", edp->supplies[1].supply);
  683. return ret;
  684. }
  685. ret = qcom_edp_clks_register(edp, pdev->dev.of_node);
  686. if (ret)
  687. return ret;
  688. edp->phy = devm_phy_create(dev, pdev->dev.of_node, &qcom_edp_ops);
  689. if (IS_ERR(edp->phy)) {
  690. dev_err(dev, "failed to register phy\n");
  691. return PTR_ERR(edp->phy);
  692. }
  693. phy_set_drvdata(edp->phy, edp);
  694. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  695. return PTR_ERR_OR_ZERO(phy_provider);
  696. }
  697. static const struct of_device_id qcom_edp_phy_match_table[] = {
  698. { .compatible = "qcom,sc7280-edp-phy" },
  699. { .compatible = "qcom,sc8180x-edp-phy" },
  700. { .compatible = "qcom,sc8280xp-dp-phy", .data = &dp_phy_cfg },
  701. { .compatible = "qcom,sc8280xp-edp-phy", .data = &edp_phy_cfg },
  702. { }
  703. };
  704. MODULE_DEVICE_TABLE(of, qcom_edp_phy_match_table);
  705. static struct platform_driver qcom_edp_phy_driver = {
  706. .probe = qcom_edp_phy_probe,
  707. .driver = {
  708. .name = "qcom-edp-phy",
  709. .of_match_table = qcom_edp_phy_match_table,
  710. },
  711. };
  712. module_platform_driver(qcom_edp_phy_driver);
  713. MODULE_AUTHOR("Bjorn Andersson <[email protected]>");
  714. MODULE_DESCRIPTION("Qualcomm eDP QMP PHY driver");
  715. MODULE_LICENSE("GPL v2");