hdmi_phy_8996.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/delay.h>
  7. #include "hdmi.h"
  8. #define HDMI_VCO_MAX_FREQ 12000000000UL
  9. #define HDMI_VCO_MIN_FREQ 8000000000UL
  10. #define HDMI_PCLK_MAX_FREQ 600000000
  11. #define HDMI_PCLK_MIN_FREQ 25000000
  12. #define HDMI_HIGH_FREQ_BIT_CLK_THRESHOLD 3400000000UL
  13. #define HDMI_DIG_FREQ_BIT_CLK_THRESHOLD 1500000000UL
  14. #define HDMI_MID_FREQ_BIT_CLK_THRESHOLD 750000000UL
  15. #define HDMI_CORECLK_DIV 5
  16. #define HDMI_DEFAULT_REF_CLOCK 19200000
  17. #define HDMI_PLL_CMP_CNT 1024
  18. #define HDMI_PLL_POLL_MAX_READS 100
  19. #define HDMI_PLL_POLL_TIMEOUT_US 150
  20. #define HDMI_NUM_TX_CHANNEL 4
  21. struct hdmi_pll_8996 {
  22. struct platform_device *pdev;
  23. struct clk_hw clk_hw;
  24. /* pll mmio base */
  25. void __iomem *mmio_qserdes_com;
  26. /* tx channel base */
  27. void __iomem *mmio_qserdes_tx[HDMI_NUM_TX_CHANNEL];
  28. };
  29. #define hw_clk_to_pll(x) container_of(x, struct hdmi_pll_8996, clk_hw)
  30. struct hdmi_8996_phy_pll_reg_cfg {
  31. u32 tx_lx_lane_mode[HDMI_NUM_TX_CHANNEL];
  32. u32 tx_lx_tx_band[HDMI_NUM_TX_CHANNEL];
  33. u32 com_svs_mode_clk_sel;
  34. u32 com_hsclk_sel;
  35. u32 com_pll_cctrl_mode0;
  36. u32 com_pll_rctrl_mode0;
  37. u32 com_cp_ctrl_mode0;
  38. u32 com_dec_start_mode0;
  39. u32 com_div_frac_start1_mode0;
  40. u32 com_div_frac_start2_mode0;
  41. u32 com_div_frac_start3_mode0;
  42. u32 com_integloop_gain0_mode0;
  43. u32 com_integloop_gain1_mode0;
  44. u32 com_lock_cmp_en;
  45. u32 com_lock_cmp1_mode0;
  46. u32 com_lock_cmp2_mode0;
  47. u32 com_lock_cmp3_mode0;
  48. u32 com_core_clk_en;
  49. u32 com_coreclk_div;
  50. u32 com_vco_tune_ctrl;
  51. u32 tx_lx_tx_drv_lvl[HDMI_NUM_TX_CHANNEL];
  52. u32 tx_lx_tx_emp_post1_lvl[HDMI_NUM_TX_CHANNEL];
  53. u32 tx_lx_vmode_ctrl1[HDMI_NUM_TX_CHANNEL];
  54. u32 tx_lx_vmode_ctrl2[HDMI_NUM_TX_CHANNEL];
  55. u32 tx_lx_res_code_lane_tx[HDMI_NUM_TX_CHANNEL];
  56. u32 tx_lx_hp_pd_enables[HDMI_NUM_TX_CHANNEL];
  57. u32 phy_mode;
  58. };
  59. struct hdmi_8996_post_divider {
  60. u64 vco_freq;
  61. int hsclk_divsel;
  62. int vco_ratio;
  63. int tx_band_sel;
  64. int half_rate_mode;
  65. };
  66. static inline struct hdmi_phy *pll_get_phy(struct hdmi_pll_8996 *pll)
  67. {
  68. return platform_get_drvdata(pll->pdev);
  69. }
  70. static inline void hdmi_pll_write(struct hdmi_pll_8996 *pll, int offset,
  71. u32 data)
  72. {
  73. writel(data, pll->mmio_qserdes_com + offset);
  74. }
  75. static inline u32 hdmi_pll_read(struct hdmi_pll_8996 *pll, int offset)
  76. {
  77. return readl(pll->mmio_qserdes_com + offset);
  78. }
  79. static inline void hdmi_tx_chan_write(struct hdmi_pll_8996 *pll, int channel,
  80. int offset, int data)
  81. {
  82. writel(data, pll->mmio_qserdes_tx[channel] + offset);
  83. }
  84. static inline u32 pll_get_cpctrl(u64 frac_start, unsigned long ref_clk,
  85. bool gen_ssc)
  86. {
  87. if ((frac_start != 0) || gen_ssc)
  88. return (11000000 / (ref_clk / 20));
  89. return 0x23;
  90. }
  91. static inline u32 pll_get_rctrl(u64 frac_start, bool gen_ssc)
  92. {
  93. if ((frac_start != 0) || gen_ssc)
  94. return 0x16;
  95. return 0x10;
  96. }
  97. static inline u32 pll_get_cctrl(u64 frac_start, bool gen_ssc)
  98. {
  99. if ((frac_start != 0) || gen_ssc)
  100. return 0x28;
  101. return 0x1;
  102. }
  103. static inline u32 pll_get_integloop_gain(u64 frac_start, u64 bclk, u32 ref_clk,
  104. bool gen_ssc)
  105. {
  106. int digclk_divsel = bclk >= HDMI_DIG_FREQ_BIT_CLK_THRESHOLD ? 1 : 2;
  107. u64 base;
  108. if ((frac_start != 0) || gen_ssc)
  109. base = (64 * ref_clk) / HDMI_DEFAULT_REF_CLOCK;
  110. else
  111. base = (1022 * ref_clk) / 100;
  112. base <<= digclk_divsel;
  113. return (base <= 2046 ? base : 2046);
  114. }
  115. static inline u32 pll_get_pll_cmp(u64 fdata, unsigned long ref_clk)
  116. {
  117. u64 dividend = HDMI_PLL_CMP_CNT * fdata;
  118. u32 divisor = ref_clk * 10;
  119. u32 rem;
  120. rem = do_div(dividend, divisor);
  121. if (rem > (divisor >> 1))
  122. dividend++;
  123. return dividend - 1;
  124. }
  125. static inline u64 pll_cmp_to_fdata(u32 pll_cmp, unsigned long ref_clk)
  126. {
  127. u64 fdata = ((u64)pll_cmp) * ref_clk * 10;
  128. do_div(fdata, HDMI_PLL_CMP_CNT);
  129. return fdata;
  130. }
  131. static int pll_get_post_div(struct hdmi_8996_post_divider *pd, u64 bclk)
  132. {
  133. int ratio[] = { 2, 3, 4, 5, 6, 9, 10, 12, 14, 15, 20, 21, 25, 28, 35 };
  134. int hs_divsel[] = { 0, 4, 8, 12, 1, 5, 2, 9, 3, 13, 10, 7, 14, 11, 15 };
  135. int tx_band_sel[] = { 0, 1, 2, 3 };
  136. u64 vco_freq[60];
  137. u64 vco, vco_optimal;
  138. int half_rate_mode = 0;
  139. int vco_optimal_index, vco_freq_index;
  140. int i, j;
  141. retry:
  142. vco_optimal = HDMI_VCO_MAX_FREQ;
  143. vco_optimal_index = -1;
  144. vco_freq_index = 0;
  145. for (i = 0; i < 15; i++) {
  146. for (j = 0; j < 4; j++) {
  147. u32 ratio_mult = ratio[i] << tx_band_sel[j];
  148. vco = bclk >> half_rate_mode;
  149. vco *= ratio_mult;
  150. vco_freq[vco_freq_index++] = vco;
  151. }
  152. }
  153. for (i = 0; i < 60; i++) {
  154. u64 vco_tmp = vco_freq[i];
  155. if ((vco_tmp >= HDMI_VCO_MIN_FREQ) &&
  156. (vco_tmp <= vco_optimal)) {
  157. vco_optimal = vco_tmp;
  158. vco_optimal_index = i;
  159. }
  160. }
  161. if (vco_optimal_index == -1) {
  162. if (!half_rate_mode) {
  163. half_rate_mode = 1;
  164. goto retry;
  165. }
  166. } else {
  167. pd->vco_freq = vco_optimal;
  168. pd->tx_band_sel = tx_band_sel[vco_optimal_index % 4];
  169. pd->vco_ratio = ratio[vco_optimal_index / 4];
  170. pd->hsclk_divsel = hs_divsel[vco_optimal_index / 4];
  171. return 0;
  172. }
  173. return -EINVAL;
  174. }
  175. static int pll_calculate(unsigned long pix_clk, unsigned long ref_clk,
  176. struct hdmi_8996_phy_pll_reg_cfg *cfg)
  177. {
  178. struct hdmi_8996_post_divider pd;
  179. u64 bclk;
  180. u64 tmds_clk;
  181. u64 dec_start;
  182. u64 frac_start;
  183. u64 fdata;
  184. u32 pll_divisor;
  185. u32 rem;
  186. u32 cpctrl;
  187. u32 rctrl;
  188. u32 cctrl;
  189. u32 integloop_gain;
  190. u32 pll_cmp;
  191. int i, ret;
  192. /* bit clk = 10 * pix_clk */
  193. bclk = ((u64)pix_clk) * 10;
  194. if (bclk > HDMI_HIGH_FREQ_BIT_CLK_THRESHOLD)
  195. tmds_clk = pix_clk >> 2;
  196. else
  197. tmds_clk = pix_clk;
  198. ret = pll_get_post_div(&pd, bclk);
  199. if (ret)
  200. return ret;
  201. dec_start = pd.vco_freq;
  202. pll_divisor = 4 * ref_clk;
  203. do_div(dec_start, pll_divisor);
  204. frac_start = pd.vco_freq * (1 << 20);
  205. rem = do_div(frac_start, pll_divisor);
  206. frac_start -= dec_start * (1 << 20);
  207. if (rem > (pll_divisor >> 1))
  208. frac_start++;
  209. cpctrl = pll_get_cpctrl(frac_start, ref_clk, false);
  210. rctrl = pll_get_rctrl(frac_start, false);
  211. cctrl = pll_get_cctrl(frac_start, false);
  212. integloop_gain = pll_get_integloop_gain(frac_start, bclk,
  213. ref_clk, false);
  214. fdata = pd.vco_freq;
  215. do_div(fdata, pd.vco_ratio);
  216. pll_cmp = pll_get_pll_cmp(fdata, ref_clk);
  217. DBG("VCO freq: %llu", pd.vco_freq);
  218. DBG("fdata: %llu", fdata);
  219. DBG("pix_clk: %lu", pix_clk);
  220. DBG("tmds clk: %llu", tmds_clk);
  221. DBG("HSCLK_SEL: %d", pd.hsclk_divsel);
  222. DBG("DEC_START: %llu", dec_start);
  223. DBG("DIV_FRAC_START: %llu", frac_start);
  224. DBG("PLL_CPCTRL: %u", cpctrl);
  225. DBG("PLL_RCTRL: %u", rctrl);
  226. DBG("PLL_CCTRL: %u", cctrl);
  227. DBG("INTEGLOOP_GAIN: %u", integloop_gain);
  228. DBG("TX_BAND: %d", pd.tx_band_sel);
  229. DBG("PLL_CMP: %u", pll_cmp);
  230. /* Convert these values to register specific values */
  231. if (bclk > HDMI_DIG_FREQ_BIT_CLK_THRESHOLD)
  232. cfg->com_svs_mode_clk_sel = 1;
  233. else
  234. cfg->com_svs_mode_clk_sel = 2;
  235. cfg->com_hsclk_sel = (0x20 | pd.hsclk_divsel);
  236. cfg->com_pll_cctrl_mode0 = cctrl;
  237. cfg->com_pll_rctrl_mode0 = rctrl;
  238. cfg->com_cp_ctrl_mode0 = cpctrl;
  239. cfg->com_dec_start_mode0 = dec_start;
  240. cfg->com_div_frac_start1_mode0 = (frac_start & 0xff);
  241. cfg->com_div_frac_start2_mode0 = ((frac_start & 0xff00) >> 8);
  242. cfg->com_div_frac_start3_mode0 = ((frac_start & 0xf0000) >> 16);
  243. cfg->com_integloop_gain0_mode0 = (integloop_gain & 0xff);
  244. cfg->com_integloop_gain1_mode0 = ((integloop_gain & 0xf00) >> 8);
  245. cfg->com_lock_cmp1_mode0 = (pll_cmp & 0xff);
  246. cfg->com_lock_cmp2_mode0 = ((pll_cmp & 0xff00) >> 8);
  247. cfg->com_lock_cmp3_mode0 = ((pll_cmp & 0x30000) >> 16);
  248. cfg->com_lock_cmp_en = 0x0;
  249. cfg->com_core_clk_en = 0x2c;
  250. cfg->com_coreclk_div = HDMI_CORECLK_DIV;
  251. cfg->phy_mode = (bclk > HDMI_HIGH_FREQ_BIT_CLK_THRESHOLD) ? 0x10 : 0x0;
  252. cfg->com_vco_tune_ctrl = 0x0;
  253. cfg->tx_lx_lane_mode[0] =
  254. cfg->tx_lx_lane_mode[2] = 0x43;
  255. cfg->tx_lx_hp_pd_enables[0] =
  256. cfg->tx_lx_hp_pd_enables[1] =
  257. cfg->tx_lx_hp_pd_enables[2] = 0x0c;
  258. cfg->tx_lx_hp_pd_enables[3] = 0x3;
  259. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++)
  260. cfg->tx_lx_tx_band[i] = pd.tx_band_sel + 4;
  261. if (bclk > HDMI_HIGH_FREQ_BIT_CLK_THRESHOLD) {
  262. cfg->tx_lx_tx_drv_lvl[0] =
  263. cfg->tx_lx_tx_drv_lvl[1] =
  264. cfg->tx_lx_tx_drv_lvl[2] = 0x25;
  265. cfg->tx_lx_tx_drv_lvl[3] = 0x22;
  266. cfg->tx_lx_tx_emp_post1_lvl[0] =
  267. cfg->tx_lx_tx_emp_post1_lvl[1] =
  268. cfg->tx_lx_tx_emp_post1_lvl[2] = 0x23;
  269. cfg->tx_lx_tx_emp_post1_lvl[3] = 0x27;
  270. cfg->tx_lx_vmode_ctrl1[0] =
  271. cfg->tx_lx_vmode_ctrl1[1] =
  272. cfg->tx_lx_vmode_ctrl1[2] =
  273. cfg->tx_lx_vmode_ctrl1[3] = 0x00;
  274. cfg->tx_lx_vmode_ctrl2[0] =
  275. cfg->tx_lx_vmode_ctrl2[1] =
  276. cfg->tx_lx_vmode_ctrl2[2] = 0x0D;
  277. cfg->tx_lx_vmode_ctrl2[3] = 0x00;
  278. } else if (bclk > HDMI_MID_FREQ_BIT_CLK_THRESHOLD) {
  279. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++) {
  280. cfg->tx_lx_tx_drv_lvl[i] = 0x25;
  281. cfg->tx_lx_tx_emp_post1_lvl[i] = 0x23;
  282. cfg->tx_lx_vmode_ctrl1[i] = 0x00;
  283. }
  284. cfg->tx_lx_vmode_ctrl2[0] =
  285. cfg->tx_lx_vmode_ctrl2[1] =
  286. cfg->tx_lx_vmode_ctrl2[2] = 0x0D;
  287. cfg->tx_lx_vmode_ctrl2[3] = 0x00;
  288. } else {
  289. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++) {
  290. cfg->tx_lx_tx_drv_lvl[i] = 0x20;
  291. cfg->tx_lx_tx_emp_post1_lvl[i] = 0x20;
  292. cfg->tx_lx_vmode_ctrl1[i] = 0x00;
  293. cfg->tx_lx_vmode_ctrl2[i] = 0x0E;
  294. }
  295. }
  296. DBG("com_svs_mode_clk_sel = 0x%x", cfg->com_svs_mode_clk_sel);
  297. DBG("com_hsclk_sel = 0x%x", cfg->com_hsclk_sel);
  298. DBG("com_lock_cmp_en = 0x%x", cfg->com_lock_cmp_en);
  299. DBG("com_pll_cctrl_mode0 = 0x%x", cfg->com_pll_cctrl_mode0);
  300. DBG("com_pll_rctrl_mode0 = 0x%x", cfg->com_pll_rctrl_mode0);
  301. DBG("com_cp_ctrl_mode0 = 0x%x", cfg->com_cp_ctrl_mode0);
  302. DBG("com_dec_start_mode0 = 0x%x", cfg->com_dec_start_mode0);
  303. DBG("com_div_frac_start1_mode0 = 0x%x", cfg->com_div_frac_start1_mode0);
  304. DBG("com_div_frac_start2_mode0 = 0x%x", cfg->com_div_frac_start2_mode0);
  305. DBG("com_div_frac_start3_mode0 = 0x%x", cfg->com_div_frac_start3_mode0);
  306. DBG("com_integloop_gain0_mode0 = 0x%x", cfg->com_integloop_gain0_mode0);
  307. DBG("com_integloop_gain1_mode0 = 0x%x", cfg->com_integloop_gain1_mode0);
  308. DBG("com_lock_cmp1_mode0 = 0x%x", cfg->com_lock_cmp1_mode0);
  309. DBG("com_lock_cmp2_mode0 = 0x%x", cfg->com_lock_cmp2_mode0);
  310. DBG("com_lock_cmp3_mode0 = 0x%x", cfg->com_lock_cmp3_mode0);
  311. DBG("com_core_clk_en = 0x%x", cfg->com_core_clk_en);
  312. DBG("com_coreclk_div = 0x%x", cfg->com_coreclk_div);
  313. DBG("phy_mode = 0x%x", cfg->phy_mode);
  314. DBG("tx_l0_lane_mode = 0x%x", cfg->tx_lx_lane_mode[0]);
  315. DBG("tx_l2_lane_mode = 0x%x", cfg->tx_lx_lane_mode[2]);
  316. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++) {
  317. DBG("tx_l%d_tx_band = 0x%x", i, cfg->tx_lx_tx_band[i]);
  318. DBG("tx_l%d_tx_drv_lvl = 0x%x", i, cfg->tx_lx_tx_drv_lvl[i]);
  319. DBG("tx_l%d_tx_emp_post1_lvl = 0x%x", i,
  320. cfg->tx_lx_tx_emp_post1_lvl[i]);
  321. DBG("tx_l%d_vmode_ctrl1 = 0x%x", i, cfg->tx_lx_vmode_ctrl1[i]);
  322. DBG("tx_l%d_vmode_ctrl2 = 0x%x", i, cfg->tx_lx_vmode_ctrl2[i]);
  323. }
  324. return 0;
  325. }
  326. static int hdmi_8996_pll_set_clk_rate(struct clk_hw *hw, unsigned long rate,
  327. unsigned long parent_rate)
  328. {
  329. struct hdmi_pll_8996 *pll = hw_clk_to_pll(hw);
  330. struct hdmi_phy *phy = pll_get_phy(pll);
  331. struct hdmi_8996_phy_pll_reg_cfg cfg;
  332. int i, ret;
  333. memset(&cfg, 0x00, sizeof(cfg));
  334. ret = pll_calculate(rate, parent_rate, &cfg);
  335. if (ret) {
  336. DRM_ERROR("PLL calculation failed\n");
  337. return ret;
  338. }
  339. /* Initially shut down PHY */
  340. DBG("Disabling PHY");
  341. hdmi_phy_write(phy, REG_HDMI_8996_PHY_PD_CTL, 0x0);
  342. udelay(500);
  343. /* Power up sequence */
  344. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_BG_CTRL, 0x04);
  345. hdmi_phy_write(phy, REG_HDMI_8996_PHY_PD_CTL, 0x1);
  346. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_RESETSM_CNTRL, 0x20);
  347. hdmi_phy_write(phy, REG_HDMI_8996_PHY_TX0_TX1_LANE_CTL, 0x0F);
  348. hdmi_phy_write(phy, REG_HDMI_8996_PHY_TX2_TX3_LANE_CTL, 0x0F);
  349. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++) {
  350. hdmi_tx_chan_write(pll, i,
  351. REG_HDMI_PHY_QSERDES_TX_LX_CLKBUF_ENABLE,
  352. 0x03);
  353. hdmi_tx_chan_write(pll, i,
  354. REG_HDMI_PHY_QSERDES_TX_LX_TX_BAND,
  355. cfg.tx_lx_tx_band[i]);
  356. hdmi_tx_chan_write(pll, i,
  357. REG_HDMI_PHY_QSERDES_TX_LX_RESET_TSYNC_EN,
  358. 0x03);
  359. }
  360. hdmi_tx_chan_write(pll, 0, REG_HDMI_PHY_QSERDES_TX_LX_LANE_MODE,
  361. cfg.tx_lx_lane_mode[0]);
  362. hdmi_tx_chan_write(pll, 2, REG_HDMI_PHY_QSERDES_TX_LX_LANE_MODE,
  363. cfg.tx_lx_lane_mode[2]);
  364. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SYSCLK_BUF_ENABLE, 0x1E);
  365. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x07);
  366. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SYSCLK_EN_SEL, 0x37);
  367. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SYS_CLK_CTRL, 0x02);
  368. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_CLK_ENABLE1, 0x0E);
  369. /* Bypass VCO calibration */
  370. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SVS_MODE_CLK_SEL,
  371. cfg.com_svs_mode_clk_sel);
  372. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_BG_TRIM, 0x0F);
  373. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_PLL_IVCO, 0x0F);
  374. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_VCO_TUNE_CTRL,
  375. cfg.com_vco_tune_ctrl);
  376. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_BG_CTRL, 0x06);
  377. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_CLK_SELECT, 0x30);
  378. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_HSCLK_SEL,
  379. cfg.com_hsclk_sel);
  380. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP_EN,
  381. cfg.com_lock_cmp_en);
  382. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_PLL_CCTRL_MODE0,
  383. cfg.com_pll_cctrl_mode0);
  384. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_PLL_RCTRL_MODE0,
  385. cfg.com_pll_rctrl_mode0);
  386. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_CP_CTRL_MODE0,
  387. cfg.com_cp_ctrl_mode0);
  388. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_DEC_START_MODE0,
  389. cfg.com_dec_start_mode0);
  390. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_DIV_FRAC_START1_MODE0,
  391. cfg.com_div_frac_start1_mode0);
  392. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_DIV_FRAC_START2_MODE0,
  393. cfg.com_div_frac_start2_mode0);
  394. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_DIV_FRAC_START3_MODE0,
  395. cfg.com_div_frac_start3_mode0);
  396. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_INTEGLOOP_GAIN0_MODE0,
  397. cfg.com_integloop_gain0_mode0);
  398. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_INTEGLOOP_GAIN1_MODE0,
  399. cfg.com_integloop_gain1_mode0);
  400. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP1_MODE0,
  401. cfg.com_lock_cmp1_mode0);
  402. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP2_MODE0,
  403. cfg.com_lock_cmp2_mode0);
  404. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP3_MODE0,
  405. cfg.com_lock_cmp3_mode0);
  406. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_VCO_TUNE_MAP, 0x00);
  407. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_CORE_CLK_EN,
  408. cfg.com_core_clk_en);
  409. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_CORECLK_DIV,
  410. cfg.com_coreclk_div);
  411. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_CMN_CONFIG, 0x02);
  412. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_RESCODE_DIV_NUM, 0x15);
  413. /* TX lanes setup (TX 0/1/2/3) */
  414. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++) {
  415. hdmi_tx_chan_write(pll, i,
  416. REG_HDMI_PHY_QSERDES_TX_LX_TX_DRV_LVL,
  417. cfg.tx_lx_tx_drv_lvl[i]);
  418. hdmi_tx_chan_write(pll, i,
  419. REG_HDMI_PHY_QSERDES_TX_LX_TX_EMP_POST1_LVL,
  420. cfg.tx_lx_tx_emp_post1_lvl[i]);
  421. hdmi_tx_chan_write(pll, i,
  422. REG_HDMI_PHY_QSERDES_TX_LX_VMODE_CTRL1,
  423. cfg.tx_lx_vmode_ctrl1[i]);
  424. hdmi_tx_chan_write(pll, i,
  425. REG_HDMI_PHY_QSERDES_TX_LX_VMODE_CTRL2,
  426. cfg.tx_lx_vmode_ctrl2[i]);
  427. hdmi_tx_chan_write(pll, i,
  428. REG_HDMI_PHY_QSERDES_TX_LX_TX_DRV_LVL_OFFSET,
  429. 0x00);
  430. hdmi_tx_chan_write(pll, i,
  431. REG_HDMI_PHY_QSERDES_TX_LX_RES_CODE_LANE_OFFSET,
  432. 0x00);
  433. hdmi_tx_chan_write(pll, i,
  434. REG_HDMI_PHY_QSERDES_TX_LX_TRAN_DRVR_EMP_EN,
  435. 0x03);
  436. hdmi_tx_chan_write(pll, i,
  437. REG_HDMI_PHY_QSERDES_TX_LX_PARRATE_REC_DETECT_IDLE_EN,
  438. 0x40);
  439. hdmi_tx_chan_write(pll, i,
  440. REG_HDMI_PHY_QSERDES_TX_LX_HP_PD_ENABLES,
  441. cfg.tx_lx_hp_pd_enables[i]);
  442. }
  443. hdmi_phy_write(phy, REG_HDMI_8996_PHY_MODE, cfg.phy_mode);
  444. hdmi_phy_write(phy, REG_HDMI_8996_PHY_PD_CTL, 0x1F);
  445. /*
  446. * Ensure that vco configuration gets flushed to hardware before
  447. * enabling the PLL
  448. */
  449. wmb();
  450. return 0;
  451. }
  452. static int hdmi_8996_phy_ready_status(struct hdmi_phy *phy)
  453. {
  454. u32 nb_tries = HDMI_PLL_POLL_MAX_READS;
  455. unsigned long timeout = HDMI_PLL_POLL_TIMEOUT_US;
  456. u32 status;
  457. int phy_ready = 0;
  458. DBG("Waiting for PHY ready");
  459. while (nb_tries--) {
  460. status = hdmi_phy_read(phy, REG_HDMI_8996_PHY_STATUS);
  461. phy_ready = status & BIT(0);
  462. if (phy_ready)
  463. break;
  464. udelay(timeout);
  465. }
  466. DBG("PHY is %sready", phy_ready ? "" : "*not* ");
  467. return phy_ready;
  468. }
  469. static int hdmi_8996_pll_lock_status(struct hdmi_pll_8996 *pll)
  470. {
  471. u32 status;
  472. int nb_tries = HDMI_PLL_POLL_MAX_READS;
  473. unsigned long timeout = HDMI_PLL_POLL_TIMEOUT_US;
  474. int pll_locked = 0;
  475. DBG("Waiting for PLL lock");
  476. while (nb_tries--) {
  477. status = hdmi_pll_read(pll,
  478. REG_HDMI_PHY_QSERDES_COM_C_READY_STATUS);
  479. pll_locked = status & BIT(0);
  480. if (pll_locked)
  481. break;
  482. udelay(timeout);
  483. }
  484. DBG("HDMI PLL is %slocked", pll_locked ? "" : "*not* ");
  485. return pll_locked;
  486. }
  487. static int hdmi_8996_pll_prepare(struct clk_hw *hw)
  488. {
  489. struct hdmi_pll_8996 *pll = hw_clk_to_pll(hw);
  490. struct hdmi_phy *phy = pll_get_phy(pll);
  491. int i, ret = 0;
  492. hdmi_phy_write(phy, REG_HDMI_8996_PHY_CFG, 0x1);
  493. udelay(100);
  494. hdmi_phy_write(phy, REG_HDMI_8996_PHY_CFG, 0x19);
  495. udelay(100);
  496. ret = hdmi_8996_pll_lock_status(pll);
  497. if (!ret)
  498. return ret;
  499. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++)
  500. hdmi_tx_chan_write(pll, i,
  501. REG_HDMI_PHY_QSERDES_TX_LX_HIGHZ_TRANSCEIVEREN_BIAS_DRVR_EN,
  502. 0x6F);
  503. /* Disable SSC */
  504. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SSC_PER1, 0x0);
  505. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SSC_PER2, 0x0);
  506. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SSC_STEP_SIZE1, 0x0);
  507. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SSC_STEP_SIZE2, 0x0);
  508. hdmi_pll_write(pll, REG_HDMI_PHY_QSERDES_COM_SSC_EN_CENTER, 0x2);
  509. ret = hdmi_8996_phy_ready_status(phy);
  510. if (!ret)
  511. return ret;
  512. /* Restart the retiming buffer */
  513. hdmi_phy_write(phy, REG_HDMI_8996_PHY_CFG, 0x18);
  514. udelay(1);
  515. hdmi_phy_write(phy, REG_HDMI_8996_PHY_CFG, 0x19);
  516. return 0;
  517. }
  518. static int hdmi_8996_pll_determine_rate(struct clk_hw *hw,
  519. struct clk_rate_request *req)
  520. {
  521. req->rate = clamp_t(unsigned long, req->rate, HDMI_PCLK_MIN_FREQ, HDMI_PCLK_MAX_FREQ);
  522. return 0;
  523. }
  524. static unsigned long hdmi_8996_pll_recalc_rate(struct clk_hw *hw,
  525. unsigned long parent_rate)
  526. {
  527. struct hdmi_pll_8996 *pll = hw_clk_to_pll(hw);
  528. u64 fdata;
  529. u32 cmp1, cmp2, cmp3, pll_cmp;
  530. cmp1 = hdmi_pll_read(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP1_MODE0);
  531. cmp2 = hdmi_pll_read(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP2_MODE0);
  532. cmp3 = hdmi_pll_read(pll, REG_HDMI_PHY_QSERDES_COM_LOCK_CMP3_MODE0);
  533. pll_cmp = cmp1 | (cmp2 << 8) | (cmp3 << 16);
  534. fdata = pll_cmp_to_fdata(pll_cmp + 1, parent_rate);
  535. do_div(fdata, 10);
  536. return fdata;
  537. }
  538. static void hdmi_8996_pll_unprepare(struct clk_hw *hw)
  539. {
  540. struct hdmi_pll_8996 *pll = hw_clk_to_pll(hw);
  541. struct hdmi_phy *phy = pll_get_phy(pll);
  542. hdmi_phy_write(phy, REG_HDMI_8996_PHY_CFG, 0x6);
  543. usleep_range(100, 150);
  544. }
  545. static int hdmi_8996_pll_is_enabled(struct clk_hw *hw)
  546. {
  547. struct hdmi_pll_8996 *pll = hw_clk_to_pll(hw);
  548. u32 status;
  549. int pll_locked;
  550. status = hdmi_pll_read(pll, REG_HDMI_PHY_QSERDES_COM_C_READY_STATUS);
  551. pll_locked = status & BIT(0);
  552. return pll_locked;
  553. }
  554. static const struct clk_ops hdmi_8996_pll_ops = {
  555. .set_rate = hdmi_8996_pll_set_clk_rate,
  556. .determine_rate = hdmi_8996_pll_determine_rate,
  557. .recalc_rate = hdmi_8996_pll_recalc_rate,
  558. .prepare = hdmi_8996_pll_prepare,
  559. .unprepare = hdmi_8996_pll_unprepare,
  560. .is_enabled = hdmi_8996_pll_is_enabled,
  561. };
  562. static const struct clk_init_data pll_init = {
  563. .name = "hdmipll",
  564. .ops = &hdmi_8996_pll_ops,
  565. .parent_data = (const struct clk_parent_data[]){
  566. { .fw_name = "xo", .name = "xo_board" },
  567. },
  568. .num_parents = 1,
  569. .flags = CLK_IGNORE_UNUSED,
  570. };
  571. int msm_hdmi_pll_8996_init(struct platform_device *pdev)
  572. {
  573. struct device *dev = &pdev->dev;
  574. struct hdmi_pll_8996 *pll;
  575. int i, ret;
  576. pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
  577. if (!pll)
  578. return -ENOMEM;
  579. pll->pdev = pdev;
  580. pll->mmio_qserdes_com = msm_ioremap(pdev, "hdmi_pll");
  581. if (IS_ERR(pll->mmio_qserdes_com)) {
  582. DRM_DEV_ERROR(dev, "failed to map pll base\n");
  583. return -ENOMEM;
  584. }
  585. for (i = 0; i < HDMI_NUM_TX_CHANNEL; i++) {
  586. char name[32];
  587. snprintf(name, sizeof(name), "hdmi_tx_l%d", i);
  588. pll->mmio_qserdes_tx[i] = msm_ioremap(pdev, name);
  589. if (IS_ERR(pll->mmio_qserdes_tx[i])) {
  590. DRM_DEV_ERROR(dev, "failed to map pll base\n");
  591. return -ENOMEM;
  592. }
  593. }
  594. pll->clk_hw.init = &pll_init;
  595. ret = devm_clk_hw_register(dev, &pll->clk_hw);
  596. if (ret) {
  597. DRM_DEV_ERROR(dev, "failed to register pll clock\n");
  598. return ret;
  599. }
  600. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, &pll->clk_hw);
  601. if (ret) {
  602. DRM_DEV_ERROR(dev, "%s: failed to register clk provider: %d\n", __func__, ret);
  603. return ret;
  604. }
  605. return 0;
  606. }
  607. static const char * const hdmi_phy_8996_reg_names[] = {
  608. "vddio",
  609. "vcca",
  610. };
  611. static const char * const hdmi_phy_8996_clk_names[] = {
  612. "iface", "ref",
  613. };
  614. const struct hdmi_phy_cfg msm_hdmi_phy_8996_cfg = {
  615. .type = MSM_HDMI_PHY_8996,
  616. .reg_names = hdmi_phy_8996_reg_names,
  617. .num_regs = ARRAY_SIZE(hdmi_phy_8996_reg_names),
  618. .clk_names = hdmi_phy_8996_clk_names,
  619. .num_clks = ARRAY_SIZE(hdmi_phy_8996_clk_names),
  620. };