rk_hdmi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright (c) 2015 Google, Inc
  3. * Copyright 2014 Rockchip Inc.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <display.h>
  10. #include <dm.h>
  11. #include <edid.h>
  12. #include <regmap.h>
  13. #include <syscon.h>
  14. #include <asm/gpio.h>
  15. #include <asm/io.h>
  16. #include <asm/arch/clock.h>
  17. #include <asm/arch/grf_rk3288.h>
  18. #include <asm/arch/hdmi_rk3288.h>
  19. #include <power/regulator.h>
  20. struct tmds_n_cts {
  21. u32 tmds;
  22. u32 cts;
  23. u32 n;
  24. };
  25. struct rk_hdmi_priv {
  26. struct rk3288_hdmi *regs;
  27. struct rk3288_grf *grf;
  28. };
  29. static const struct tmds_n_cts n_cts_table[] = {
  30. {
  31. .tmds = 25175, .n = 6144, .cts = 25175,
  32. }, {
  33. .tmds = 25200, .n = 6144, .cts = 25200,
  34. }, {
  35. .tmds = 27000, .n = 6144, .cts = 27000,
  36. }, {
  37. .tmds = 27027, .n = 6144, .cts = 27027,
  38. }, {
  39. .tmds = 40000, .n = 6144, .cts = 40000,
  40. }, {
  41. .tmds = 54000, .n = 6144, .cts = 54000,
  42. }, {
  43. .tmds = 54054, .n = 6144, .cts = 54054,
  44. }, {
  45. .tmds = 65000, .n = 6144, .cts = 65000,
  46. }, {
  47. .tmds = 74176, .n = 11648, .cts = 140625,
  48. }, {
  49. .tmds = 74250, .n = 6144, .cts = 74250,
  50. }, {
  51. .tmds = 83500, .n = 6144, .cts = 83500,
  52. }, {
  53. .tmds = 106500, .n = 6144, .cts = 106500,
  54. }, {
  55. .tmds = 108000, .n = 6144, .cts = 108000,
  56. }, {
  57. .tmds = 148352, .n = 5824, .cts = 140625,
  58. }, {
  59. .tmds = 148500, .n = 6144, .cts = 148500,
  60. }, {
  61. .tmds = 297000, .n = 5120, .cts = 247500,
  62. }
  63. };
  64. struct hdmi_mpll_config {
  65. u64 mpixelclock;
  66. /* Mode of Operation and PLL Dividers Control Register */
  67. u32 cpce;
  68. /* PLL Gmp Control Register */
  69. u32 gmp;
  70. /* PLL Current COntrol Register */
  71. u32 curr;
  72. };
  73. struct hdmi_phy_config {
  74. u64 mpixelclock;
  75. u32 sym_ctr; /* clock symbol and transmitter control */
  76. u32 term; /* transmission termination value */
  77. u32 vlev_ctr; /* voltage level control */
  78. };
  79. static const struct hdmi_phy_config rockchip_phy_config[] = {
  80. {
  81. .mpixelclock = 74250000,
  82. .sym_ctr = 0x8009, .term = 0x0004, .vlev_ctr = 0x0272,
  83. }, {
  84. .mpixelclock = 148500000,
  85. .sym_ctr = 0x802b, .term = 0x0004, .vlev_ctr = 0x028d,
  86. }, {
  87. .mpixelclock = 297000000,
  88. .sym_ctr = 0x8039, .term = 0x0005, .vlev_ctr = 0x028d,
  89. }, {
  90. .mpixelclock = ~0ul,
  91. .sym_ctr = 0x0000, .term = 0x0000, .vlev_ctr = 0x0000,
  92. }
  93. };
  94. static const struct hdmi_mpll_config rockchip_mpll_cfg[] = {
  95. {
  96. .mpixelclock = 40000000,
  97. .cpce = 0x00b3, .gmp = 0x0000, .curr = 0x0018,
  98. }, {
  99. .mpixelclock = 65000000,
  100. .cpce = 0x0072, .gmp = 0x0001, .curr = 0x0028,
  101. }, {
  102. .mpixelclock = 66000000,
  103. .cpce = 0x013e, .gmp = 0x0003, .curr = 0x0038,
  104. }, {
  105. .mpixelclock = 835000000,
  106. .cpce = 0x0072, .gmp = 0x0001, .curr = 0x0028,
  107. }, {
  108. .mpixelclock = 146250000,
  109. .cpce = 0x0051, .gmp = 0x0002, .curr = 0x0038,
  110. }, {
  111. .mpixelclock = 148500000,
  112. .cpce = 0x0051, .gmp = 0x0003, .curr = 0x0000,
  113. }, {
  114. .mpixelclock = ~0ul,
  115. .cpce = 0x0051, .gmp = 0x0003, .curr = 0x0000,
  116. }
  117. };
  118. static const u32 csc_coeff_default[3][4] = {
  119. { 0x2000, 0x0000, 0x0000, 0x0000 },
  120. { 0x0000, 0x2000, 0x0000, 0x0000 },
  121. { 0x0000, 0x0000, 0x2000, 0x0000 }
  122. };
  123. static void hdmi_set_clock_regenerator(struct rk3288_hdmi *regs, u32 n, u32 cts)
  124. {
  125. uint cts3;
  126. uint n3;
  127. /* first set ncts_atomic_write (if present) */
  128. n3 = HDMI_AUD_N3_NCTS_ATOMIC_WRITE;
  129. writel(n3, &regs->aud_n3);
  130. /* set cts_manual (if present) */
  131. cts3 = HDMI_AUD_CTS3_CTS_MANUAL;
  132. cts3 |= HDMI_AUD_CTS3_N_SHIFT_1 << HDMI_AUD_CTS3_N_SHIFT_OFFSET;
  133. cts3 |= (cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK;
  134. /* write cts values; cts3 must be written first */
  135. writel(cts3, &regs->aud_cts3);
  136. writel((cts >> 8) & 0xff, &regs->aud_cts2);
  137. writel(cts & 0xff, &regs->aud_cts1);
  138. /* write n values; n1 must be written last */
  139. n3 |= (n >> 16) & HDMI_AUD_N3_AUDN19_16_MASK;
  140. writel(n3, &regs->aud_n3);
  141. writel((n >> 8) & 0xff, &regs->aud_n2);
  142. writel(n & 0xff, &regs->aud_n1);
  143. writel(HDMI_AUD_INPUTCLKFS_128, &regs->aud_inputclkfs);
  144. }
  145. static int hdmi_lookup_n_cts(u32 pixel_clk)
  146. {
  147. int i;
  148. for (i = 0; i < ARRAY_SIZE(n_cts_table); i++)
  149. if (pixel_clk <= n_cts_table[i].tmds)
  150. break;
  151. if (i >= ARRAY_SIZE(n_cts_table))
  152. return -1;
  153. return i;
  154. }
  155. static void hdmi_audio_set_samplerate(struct rk3288_hdmi *regs, u32 pixel_clk)
  156. {
  157. u32 clk_n, clk_cts;
  158. int index;
  159. index = hdmi_lookup_n_cts(pixel_clk);
  160. if (index == -1) {
  161. debug("audio not supported for pixel clk %d\n", pixel_clk);
  162. return;
  163. }
  164. clk_n = n_cts_table[index].n;
  165. clk_cts = n_cts_table[index].cts;
  166. hdmi_set_clock_regenerator(regs, clk_n, clk_cts);
  167. }
  168. /*
  169. * this submodule is responsible for the video data synchronization.
  170. * for example, for rgb 4:4:4 input, the data map is defined as
  171. * pin{47~40} <==> r[7:0]
  172. * pin{31~24} <==> g[7:0]
  173. * pin{15~8} <==> b[7:0]
  174. */
  175. static void hdmi_video_sample(struct rk3288_hdmi *regs)
  176. {
  177. u32 color_format = 0x01;
  178. uint val;
  179. val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
  180. ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
  181. HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
  182. writel(val, &regs->tx_invid0);
  183. /* enable tx stuffing: when de is inactive, fix the output data to 0 */
  184. val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
  185. HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
  186. HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
  187. writel(val, &regs->tx_instuffing);
  188. writel(0x0, &regs->tx_gydata0);
  189. writel(0x0, &regs->tx_gydata1);
  190. writel(0x0, &regs->tx_rcrdata0);
  191. writel(0x0, &regs->tx_rcrdata1);
  192. writel(0x0, &regs->tx_bcbdata0);
  193. writel(0x0, &regs->tx_bcbdata1);
  194. }
  195. static void hdmi_update_csc_coeffs(struct rk3288_hdmi *regs)
  196. {
  197. u32 i, j;
  198. u32 csc_scale = 1;
  199. /* the csc registers are sequential, alternating msb then lsb */
  200. for (i = 0; i < ARRAY_SIZE(csc_coeff_default); i++) {
  201. for (j = 0; j < ARRAY_SIZE(csc_coeff_default[0]); j++) {
  202. u32 coeff = csc_coeff_default[i][j];
  203. writel(coeff >> 8, &regs->csc_coef[i][j].msb);
  204. writel(coeff && 0xff, &regs->csc_coef[i][j].lsb);
  205. }
  206. }
  207. clrsetbits_le32(&regs->csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
  208. csc_scale);
  209. }
  210. static void hdmi_video_csc(struct rk3288_hdmi *regs)
  211. {
  212. u32 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
  213. u32 interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
  214. /* configure the csc registers */
  215. writel(interpolation, &regs->csc_cfg);
  216. clrsetbits_le32(&regs->csc_scale,
  217. HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK, color_depth);
  218. hdmi_update_csc_coeffs(regs);
  219. }
  220. static void hdmi_video_packetize(struct rk3288_hdmi *regs)
  221. {
  222. u32 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  223. u32 remap_size = HDMI_VP_REMAP_YCC422_16BIT;
  224. u32 color_depth = 0;
  225. uint val, vp_conf;
  226. /* set the packetizer registers */
  227. val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
  228. HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
  229. ((0 << HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
  230. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
  231. writel(val, &regs->vp_pr_cd);
  232. clrsetbits_le32(&regs->vp_stuff, HDMI_VP_STUFF_PR_STUFFING_MASK,
  233. HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE);
  234. /* data from pixel repeater block */
  235. vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
  236. HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
  237. clrsetbits_le32(&regs->vp_conf, HDMI_VP_CONF_PR_EN_MASK |
  238. HDMI_VP_CONF_BYPASS_SELECT_MASK, vp_conf);
  239. clrsetbits_le32(&regs->vp_stuff, HDMI_VP_STUFF_IDEFAULT_PHASE_MASK,
  240. 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET);
  241. writel(remap_size, &regs->vp_remap);
  242. vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
  243. HDMI_VP_CONF_PP_EN_DISABLE |
  244. HDMI_VP_CONF_YCC422_EN_DISABLE;
  245. clrsetbits_le32(&regs->vp_conf, HDMI_VP_CONF_BYPASS_EN_MASK |
  246. HDMI_VP_CONF_PP_EN_ENMASK | HDMI_VP_CONF_YCC422_EN_MASK,
  247. vp_conf);
  248. clrsetbits_le32(&regs->vp_stuff, HDMI_VP_STUFF_PP_STUFFING_MASK |
  249. HDMI_VP_STUFF_YCC422_STUFFING_MASK,
  250. HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
  251. HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE);
  252. clrsetbits_le32(&regs->vp_conf, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
  253. output_select);
  254. }
  255. static inline void hdmi_phy_test_clear(struct rk3288_hdmi *regs, uint bit)
  256. {
  257. clrsetbits_le32(&regs->phy_tst0, HDMI_PHY_TST0_TSTCLR_MASK,
  258. bit << HDMI_PHY_TST0_TSTCLR_OFFSET);
  259. }
  260. static int hdmi_phy_wait_i2c_done(struct rk3288_hdmi *regs, u32 msec)
  261. {
  262. ulong start;
  263. u32 val;
  264. start = get_timer(0);
  265. do {
  266. val = readl(&regs->ih_i2cmphy_stat0);
  267. if (val & 0x3) {
  268. writel(val, &regs->ih_i2cmphy_stat0);
  269. return 0;
  270. }
  271. udelay(100);
  272. } while (get_timer(start) < msec);
  273. return 1;
  274. }
  275. static void hdmi_phy_i2c_write(struct rk3288_hdmi *regs, uint data, uint addr)
  276. {
  277. writel(0xff, &regs->ih_i2cmphy_stat0);
  278. writel(addr, &regs->phy_i2cm_address_addr);
  279. writel((u8)(data >> 8), &regs->phy_i2cm_datao_1_addr);
  280. writel((u8)(data >> 0), &regs->phy_i2cm_datao_0_addr);
  281. writel(HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
  282. &regs->phy_i2cm_operation_addr);
  283. hdmi_phy_wait_i2c_done(regs, 1000);
  284. }
  285. static void hdmi_phy_enable_power(struct rk3288_hdmi *regs, uint enable)
  286. {
  287. clrsetbits_le32(&regs->phy_conf0, HDMI_PHY_CONF0_PDZ_MASK,
  288. enable << HDMI_PHY_CONF0_PDZ_OFFSET);
  289. }
  290. static void hdmi_phy_enable_tmds(struct rk3288_hdmi *regs, uint enable)
  291. {
  292. clrsetbits_le32(&regs->phy_conf0, HDMI_PHY_CONF0_ENTMDS_MASK,
  293. enable << HDMI_PHY_CONF0_ENTMDS_OFFSET);
  294. }
  295. static void hdmi_phy_enable_spare(struct rk3288_hdmi *regs, uint enable)
  296. {
  297. clrsetbits_le32(&regs->phy_conf0, HDMI_PHY_CONF0_SPARECTRL_MASK,
  298. enable << HDMI_PHY_CONF0_SPARECTRL_OFFSET);
  299. }
  300. static void hdmi_phy_gen2_pddq(struct rk3288_hdmi *regs, uint enable)
  301. {
  302. clrsetbits_le32(&regs->phy_conf0, HDMI_PHY_CONF0_GEN2_PDDQ_MASK,
  303. enable << HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET);
  304. }
  305. static void hdmi_phy_gen2_txpwron(struct rk3288_hdmi *regs, uint enable)
  306. {
  307. clrsetbits_le32(&regs->phy_conf0,
  308. HDMI_PHY_CONF0_GEN2_TXPWRON_MASK,
  309. enable << HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET);
  310. }
  311. static void hdmi_phy_sel_data_en_pol(struct rk3288_hdmi *regs, uint enable)
  312. {
  313. clrsetbits_le32(&regs->phy_conf0,
  314. HDMI_PHY_CONF0_SELDATAENPOL_MASK,
  315. enable << HDMI_PHY_CONF0_SELDATAENPOL_OFFSET);
  316. }
  317. static void hdmi_phy_sel_interface_control(struct rk3288_hdmi *regs,
  318. uint enable)
  319. {
  320. clrsetbits_le32(&regs->phy_conf0, HDMI_PHY_CONF0_SELDIPIF_MASK,
  321. enable << HDMI_PHY_CONF0_SELDIPIF_OFFSET);
  322. }
  323. static int hdmi_phy_configure(struct rk3288_hdmi *regs, u32 mpixelclock)
  324. {
  325. ulong start;
  326. uint i, val;
  327. writel(HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
  328. &regs->mc_flowctrl);
  329. /* gen2 tx power off */
  330. hdmi_phy_gen2_txpwron(regs, 0);
  331. /* gen2 pddq */
  332. hdmi_phy_gen2_pddq(regs, 1);
  333. /* phy reset */
  334. writel(HDMI_MC_PHYRSTZ_DEASSERT, &regs->mc_phyrstz);
  335. writel(HDMI_MC_PHYRSTZ_ASSERT, &regs->mc_phyrstz);
  336. writel(HDMI_MC_HEACPHY_RST_ASSERT, &regs->mc_heacphy_rst);
  337. hdmi_phy_test_clear(regs, 1);
  338. writel(HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2, &regs->phy_i2cm_slave_addr);
  339. hdmi_phy_test_clear(regs, 0);
  340. /* pll/mpll cfg - always match on final entry */
  341. for (i = 0; rockchip_mpll_cfg[i].mpixelclock != (~0ul); i++)
  342. if (mpixelclock <= rockchip_mpll_cfg[i].mpixelclock)
  343. break;
  344. hdmi_phy_i2c_write(regs, rockchip_mpll_cfg[i].cpce, PHY_OPMODE_PLLCFG);
  345. hdmi_phy_i2c_write(regs, rockchip_mpll_cfg[i].gmp, PHY_PLLGMPCTRL);
  346. hdmi_phy_i2c_write(regs, rockchip_mpll_cfg[i].curr, PHY_PLLCURRCTRL);
  347. hdmi_phy_i2c_write(regs, 0x0000, PHY_PLLPHBYCTRL);
  348. hdmi_phy_i2c_write(regs, 0x0006, PHY_PLLCLKBISTPHASE);
  349. for (i = 0; rockchip_phy_config[i].mpixelclock != (~0ul); i++)
  350. if (mpixelclock <= rockchip_phy_config[i].mpixelclock)
  351. break;
  352. /*
  353. * resistance term 133ohm cfg
  354. * preemp cgf 0.00
  355. * tx/ck lvl 10
  356. */
  357. hdmi_phy_i2c_write(regs, rockchip_phy_config[i].term, PHY_TXTERM);
  358. hdmi_phy_i2c_write(regs, rockchip_phy_config[i].sym_ctr,
  359. PHY_CKSYMTXCTRL);
  360. hdmi_phy_i2c_write(regs, rockchip_phy_config[i].vlev_ctr, PHY_VLEVCTRL);
  361. /* remove clk term */
  362. hdmi_phy_i2c_write(regs, 0x8000, PHY_CKCALCTRL);
  363. hdmi_phy_enable_power(regs, 1);
  364. /* toggle tmds enable */
  365. hdmi_phy_enable_tmds(regs, 0);
  366. hdmi_phy_enable_tmds(regs, 1);
  367. /* gen2 tx power on */
  368. hdmi_phy_gen2_txpwron(regs, 1);
  369. hdmi_phy_gen2_pddq(regs, 0);
  370. hdmi_phy_enable_spare(regs, 1);
  371. /* wait for phy pll lock */
  372. start = get_timer(0);
  373. do {
  374. val = readl(&regs->phy_stat0);
  375. if (!(val & HDMI_PHY_TX_PHY_LOCK))
  376. return 0;
  377. udelay(100);
  378. } while (get_timer(start) < 5);
  379. return -1;
  380. }
  381. static int hdmi_phy_init(struct rk3288_hdmi *regs, uint mpixelclock)
  382. {
  383. int i, ret;
  384. /* hdmi phy spec says to do the phy initialization sequence twice */
  385. for (i = 0; i < 2; i++) {
  386. hdmi_phy_sel_data_en_pol(regs, 1);
  387. hdmi_phy_sel_interface_control(regs, 0);
  388. hdmi_phy_enable_tmds(regs, 0);
  389. hdmi_phy_enable_power(regs, 0);
  390. /* enable csc */
  391. ret = hdmi_phy_configure(regs, mpixelclock);
  392. if (ret) {
  393. debug("hdmi phy config failure %d\n", ret);
  394. return ret;
  395. }
  396. }
  397. return 0;
  398. }
  399. static void hdmi_av_composer(struct rk3288_hdmi *regs,
  400. const struct display_timing *edid)
  401. {
  402. bool mdataenablepolarity = true;
  403. uint inv_val;
  404. uint hbl;
  405. uint vbl;
  406. hbl = edid->hback_porch.typ + edid->hfront_porch.typ +
  407. edid->hsync_len.typ;
  408. vbl = edid->vback_porch.typ + edid->vfront_porch.typ +
  409. edid->vsync_len.typ;
  410. /* set up hdmi_fc_invidconf */
  411. inv_val = HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE;
  412. inv_val |= (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH ?
  413. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
  414. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW);
  415. inv_val |= (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH ?
  416. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
  417. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW);
  418. inv_val |= (mdataenablepolarity ?
  419. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
  420. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
  421. /*
  422. * TODO(sjg@chromium.org>: Need to check for HDMI / DVI
  423. * inv_val |= (edid->hdmi_monitor_detected ?
  424. * HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
  425. * HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE);
  426. */
  427. inv_val |= HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE;
  428. inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
  429. inv_val |= HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
  430. writel(inv_val, &regs->fc_invidconf);
  431. /* set up horizontal active pixel width */
  432. writel(edid->hactive.typ >> 8, &regs->fc_inhactv1);
  433. writel(edid->hactive.typ, &regs->fc_inhactv0);
  434. /* set up vertical active lines */
  435. writel(edid->vactive.typ >> 8, &regs->fc_invactv1);
  436. writel(edid->vactive.typ, &regs->fc_invactv0);
  437. /* set up horizontal blanking pixel region width */
  438. writel(hbl >> 8, &regs->fc_inhblank1);
  439. writel(hbl, &regs->fc_inhblank0);
  440. /* set up vertical blanking pixel region width */
  441. writel(vbl, &regs->fc_invblank);
  442. /* set up hsync active edge delay width (in pixel clks) */
  443. writel(edid->hfront_porch.typ >> 8, &regs->fc_hsyncindelay1);
  444. writel(edid->hfront_porch.typ, &regs->fc_hsyncindelay0);
  445. /* set up vsync active edge delay (in lines) */
  446. writel(edid->vfront_porch.typ, &regs->fc_vsyncindelay);
  447. /* set up hsync active pulse width (in pixel clks) */
  448. writel(edid->hsync_len.typ >> 8, &regs->fc_hsyncinwidth1);
  449. writel(edid->hsync_len.typ, &regs->fc_hsyncinwidth0);
  450. /* set up vsync active edge delay (in lines) */
  451. writel(edid->vsync_len.typ, &regs->fc_vsyncinwidth);
  452. }
  453. /* hdmi initialization step b.4 */
  454. static void hdmi_enable_video_path(struct rk3288_hdmi *regs)
  455. {
  456. uint clkdis;
  457. /* control period minimum duration */
  458. writel(12, &regs->fc_ctrldur);
  459. writel(32, &regs->fc_exctrldur);
  460. writel(1, &regs->fc_exctrlspac);
  461. /* set to fill tmds data channels */
  462. writel(0x0b, &regs->fc_ch0pream);
  463. writel(0x16, &regs->fc_ch1pream);
  464. writel(0x21, &regs->fc_ch2pream);
  465. /* enable pixel clock and tmds data path */
  466. clkdis = 0x7f;
  467. clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
  468. writel(clkdis, &regs->mc_clkdis);
  469. clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  470. writel(clkdis, &regs->mc_clkdis);
  471. clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  472. writel(clkdis, &regs->mc_clkdis);
  473. }
  474. /* workaround to clear the overflow condition */
  475. static void hdmi_clear_overflow(struct rk3288_hdmi *regs)
  476. {
  477. uint val, count;
  478. /* tmds software reset */
  479. writel((u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, &regs->mc_swrstz);
  480. val = readl(&regs->fc_invidconf);
  481. for (count = 0; count < 4; count++)
  482. writel(val, &regs->fc_invidconf);
  483. }
  484. static void hdmi_audio_set_format(struct rk3288_hdmi *regs)
  485. {
  486. writel(HDMI_AUD_CONF0_I2S_SELECT | HDMI_AUD_CONF0_I2S_IN_EN_0,
  487. &regs->aud_conf0);
  488. writel(HDMI_AUD_CONF1_I2S_MODE_STANDARD_MODE |
  489. HDMI_AUD_CONF1_I2S_WIDTH_16BIT, &regs->aud_conf1);
  490. writel(0x00, &regs->aud_conf2);
  491. }
  492. static void hdmi_audio_fifo_reset(struct rk3288_hdmi *regs)
  493. {
  494. writel((u8)~HDMI_MC_SWRSTZ_II2SSWRST_REQ, &regs->mc_swrstz);
  495. writel(HDMI_AUD_CONF0_SW_AUDIO_FIFO_RST, &regs->aud_conf0);
  496. writel(0x00, &regs->aud_int);
  497. writel(0x00, &regs->aud_int1);
  498. }
  499. static void hdmi_init_interrupt(struct rk3288_hdmi *regs)
  500. {
  501. uint ih_mute;
  502. /*
  503. * boot up defaults are:
  504. * hdmi_ih_mute = 0x03 (disabled)
  505. * hdmi_ih_mute_* = 0x00 (enabled)
  506. *
  507. * disable top level interrupt bits in hdmi block
  508. */
  509. ih_mute = readl(&regs->ih_mute) |
  510. HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  511. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
  512. writel(ih_mute, &regs->ih_mute);
  513. /* enable i2c master done irq */
  514. writel(~0x04, &regs->i2cm_int);
  515. /* enable i2c client nack % arbitration error irq */
  516. writel(~0x44, &regs->i2cm_ctlint);
  517. /* enable phy i2cm done irq */
  518. writel(HDMI_PHY_I2CM_INT_ADDR_DONE_POL, &regs->phy_i2cm_int_addr);
  519. /* enable phy i2cm nack & arbitration error irq */
  520. writel(HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
  521. HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
  522. &regs->phy_i2cm_ctlint_addr);
  523. /* enable cable hot plug irq */
  524. writel((u8)~HDMI_PHY_HPD, &regs->phy_mask0);
  525. /* clear hotplug interrupts */
  526. writel(HDMI_IH_PHY_STAT0_HPD, &regs->ih_phy_stat0);
  527. }
  528. static int hdmi_get_plug_in_status(struct rk3288_hdmi *regs)
  529. {
  530. uint val = readl(&regs->phy_stat0) & HDMI_PHY_HPD;
  531. return !!val;
  532. }
  533. static int hdmi_wait_for_hpd(struct rk3288_hdmi *regs)
  534. {
  535. ulong start;
  536. start = get_timer(0);
  537. do {
  538. if (hdmi_get_plug_in_status(regs))
  539. return 0;
  540. udelay(100);
  541. } while (get_timer(start) < 300);
  542. return -1;
  543. }
  544. static int hdmi_ddc_wait_i2c_done(struct rk3288_hdmi *regs, int msec)
  545. {
  546. u32 val;
  547. ulong start;
  548. start = get_timer(0);
  549. do {
  550. val = readl(&regs->ih_i2cm_stat0);
  551. if (val & 0x2) {
  552. writel(val, &regs->ih_i2cm_stat0);
  553. return 0;
  554. }
  555. udelay(100);
  556. } while (get_timer(start) < msec);
  557. return 1;
  558. }
  559. static void hdmi_ddc_reset(struct rk3288_hdmi *regs)
  560. {
  561. clrbits_le32(&regs->i2cm_softrstz, HDMI_I2CM_SOFTRSTZ);
  562. }
  563. static int hdmi_read_edid(struct rk3288_hdmi *regs, int block, u8 *buff)
  564. {
  565. int shift = (block % 2) * 0x80;
  566. int edid_read_err = 0;
  567. u32 trytime = 5;
  568. u32 n, j, val;
  569. /* set ddc i2c clk which devided from ddc_clk to 100khz */
  570. writel(0x7a, &regs->i2cm_ss_scl_hcnt_0_addr);
  571. writel(0x8d, &regs->i2cm_ss_scl_lcnt_0_addr);
  572. /*
  573. * TODO(sjg@chromium.org): The above values don't work - these ones
  574. * work better, but generate lots of errors in the data.
  575. */
  576. writel(0x0d, &regs->i2cm_ss_scl_hcnt_0_addr);
  577. writel(0x0d, &regs->i2cm_ss_scl_lcnt_0_addr);
  578. clrsetbits_le32(&regs->i2cm_div, HDMI_I2CM_DIV_FAST_STD_MODE,
  579. HDMI_I2CM_DIV_STD_MODE);
  580. writel(HDMI_I2CM_SLAVE_DDC_ADDR, &regs->i2cm_slave);
  581. writel(HDMI_I2CM_SEGADDR_DDC, &regs->i2cm_segaddr);
  582. writel(block >> 1, &regs->i2cm_segptr);
  583. while (trytime--) {
  584. edid_read_err = 0;
  585. for (n = 0; n < HDMI_EDID_BLOCK_SIZE / 8; n++) {
  586. writel(shift + 8 * n, &regs->i2c_address);
  587. if (block == 0)
  588. clrsetbits_le32(&regs->i2cm_operation,
  589. HDMI_I2CM_OPT_RD8,
  590. HDMI_I2CM_OPT_RD8);
  591. else
  592. clrsetbits_le32(&regs->i2cm_operation,
  593. HDMI_I2CM_OPT_RD8_EXT,
  594. HDMI_I2CM_OPT_RD8_EXT);
  595. if (hdmi_ddc_wait_i2c_done(regs, 10)) {
  596. hdmi_ddc_reset(regs);
  597. edid_read_err = 1;
  598. break;
  599. }
  600. for (j = 0; j < 8; j++) {
  601. val = readl(&regs->i2cm_buf0 + j);
  602. buff[8 * n + j] = val;
  603. }
  604. }
  605. if (!edid_read_err)
  606. break;
  607. }
  608. return edid_read_err;
  609. }
  610. static const u8 pre_buf[] = {
  611. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  612. 0x04, 0x69, 0xfa, 0x23, 0xc8, 0x28, 0x01, 0x00,
  613. 0x10, 0x17, 0x01, 0x03, 0x80, 0x33, 0x1d, 0x78,
  614. 0x2a, 0xd9, 0x45, 0xa2, 0x55, 0x4d, 0xa0, 0x27,
  615. 0x12, 0x50, 0x54, 0xb7, 0xef, 0x00, 0x71, 0x4f,
  616. 0x81, 0x40, 0x81, 0x80, 0x95, 0x00, 0xb3, 0x00,
  617. 0xd1, 0xc0, 0x81, 0xc0, 0x81, 0x00, 0x02, 0x3a,
  618. 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
  619. 0x45, 0x00, 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e,
  620. 0x00, 0x00, 0x00, 0xff, 0x00, 0x44, 0x34, 0x4c,
  621. 0x4d, 0x54, 0x46, 0x30, 0x37, 0x35, 0x39, 0x37,
  622. 0x36, 0x0a, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x32,
  623. 0x4b, 0x18, 0x53, 0x11, 0x00, 0x0a, 0x20, 0x20,
  624. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
  625. 0x00, 0x41, 0x53, 0x55, 0x53, 0x20, 0x56, 0x53,
  626. 0x32, 0x33, 0x38, 0x0a, 0x20, 0x20, 0x01, 0xb0,
  627. 0x02, 0x03, 0x22, 0x71, 0x4f, 0x01, 0x02, 0x03,
  628. 0x11, 0x12, 0x13, 0x04, 0x14, 0x05, 0x0e, 0x0f,
  629. 0x1d, 0x1e, 0x1f, 0x10, 0x23, 0x09, 0x17, 0x07,
  630. 0x83, 0x01, 0x00, 0x00, 0x65, 0x03, 0x0c, 0x00,
  631. 0x10, 0x00, 0x8c, 0x0a, 0xd0, 0x8a, 0x20, 0xe0,
  632. 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xfd, 0x1e,
  633. 0x11, 0x00, 0x00, 0x18, 0x01, 0x1d, 0x00, 0x72,
  634. 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
  635. 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e, 0x01, 0x1d,
  636. 0x00, 0xbc, 0x52, 0xd0, 0x1e, 0x20, 0xb8, 0x28,
  637. 0x55, 0x40, 0xfd, 0x1e, 0x11, 0x00, 0x00, 0x1e,
  638. 0x8c, 0x0a, 0xd0, 0x90, 0x20, 0x40, 0x31, 0x20,
  639. 0x0c, 0x40, 0x55, 0x00, 0xfd, 0x1e, 0x11, 0x00,
  640. 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  641. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  642. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe9,
  643. };
  644. static int rk_hdmi_read_edid(struct udevice *dev, u8 *buf, int buf_size)
  645. {
  646. struct rk_hdmi_priv *priv = dev_get_priv(dev);
  647. u32 edid_size = HDMI_EDID_BLOCK_SIZE;
  648. int ret;
  649. if (0) {
  650. edid_size = sizeof(pre_buf);
  651. memcpy(buf, pre_buf, edid_size);
  652. } else {
  653. ret = hdmi_read_edid(priv->regs, 0, buf);
  654. if (ret) {
  655. debug("failed to read edid.\n");
  656. return -1;
  657. }
  658. if (buf[0x7e] != 0) {
  659. hdmi_read_edid(priv->regs, 1,
  660. buf + HDMI_EDID_BLOCK_SIZE);
  661. edid_size += HDMI_EDID_BLOCK_SIZE;
  662. }
  663. }
  664. return edid_size;
  665. }
  666. static int rk_hdmi_enable(struct udevice *dev, int panel_bpp,
  667. const struct display_timing *edid)
  668. {
  669. struct rk_hdmi_priv *priv = dev_get_priv(dev);
  670. struct rk3288_hdmi *regs = priv->regs;
  671. int ret;
  672. debug("hdmi, mode info : clock %d hdis %d vdis %d\n",
  673. edid->pixelclock.typ, edid->hactive.typ, edid->vactive.typ);
  674. hdmi_av_composer(regs, edid);
  675. ret = hdmi_phy_init(regs, edid->pixelclock.typ);
  676. if (ret)
  677. return ret;
  678. hdmi_enable_video_path(regs);
  679. hdmi_audio_fifo_reset(regs);
  680. hdmi_audio_set_format(regs);
  681. hdmi_audio_set_samplerate(regs, edid->pixelclock.typ);
  682. hdmi_video_packetize(regs);
  683. hdmi_video_csc(regs);
  684. hdmi_video_sample(regs);
  685. hdmi_clear_overflow(regs);
  686. return 0;
  687. }
  688. static int rk_hdmi_ofdata_to_platdata(struct udevice *dev)
  689. {
  690. struct rk_hdmi_priv *priv = dev_get_priv(dev);
  691. priv->regs = (struct rk3288_hdmi *)dev_get_addr(dev);
  692. priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
  693. return 0;
  694. }
  695. static int rk_hdmi_probe(struct udevice *dev)
  696. {
  697. struct display_plat *uc_plat = dev_get_uclass_platdata(dev);
  698. struct rk_hdmi_priv *priv = dev_get_priv(dev);
  699. struct udevice *reg;
  700. struct clk clk;
  701. int ret;
  702. int vop_id = uc_plat->source_id;
  703. ret = clk_get_by_index(dev, 0, &clk);
  704. if (ret >= 0) {
  705. ret = clk_set_rate(&clk, 0);
  706. clk_free(&clk);
  707. }
  708. if (ret) {
  709. debug("%s: Failed to set hdmi clock: ret=%d\n", __func__, ret);
  710. return ret;
  711. }
  712. /*
  713. * Configure the maximum clock to permit whatever resolution the
  714. * monitor wants
  715. */
  716. ret = clk_get_by_index(uc_plat->src_dev, 0, &clk);
  717. if (ret >= 0) {
  718. ret = clk_set_rate(&clk, 384000000);
  719. clk_free(&clk);
  720. }
  721. if (ret < 0) {
  722. debug("%s: Failed to set clock in source device '%s': ret=%d\n",
  723. __func__, uc_plat->src_dev->name, ret);
  724. return ret;
  725. }
  726. ret = regulator_get_by_platname("vcc50_hdmi", &reg);
  727. if (!ret)
  728. ret = regulator_set_enable(reg, true);
  729. if (ret)
  730. debug("%s: Cannot set regulator vcc50_hdmi\n", __func__);
  731. /* hdmi source select hdmi controller */
  732. rk_setreg(&priv->grf->soc_con6, 1 << 15);
  733. /* hdmi data from vop id */
  734. rk_clrsetreg(&priv->grf->soc_con6, 1 << 4,
  735. (vop_id == 1) ? (1 << 4) : 0);
  736. ret = hdmi_wait_for_hpd(priv->regs);
  737. if (ret < 0) {
  738. debug("hdmi can not get hpd signal\n");
  739. return -1;
  740. }
  741. hdmi_init_interrupt(priv->regs);
  742. return 0;
  743. }
  744. static const struct dm_display_ops rk_hdmi_ops = {
  745. .read_edid = rk_hdmi_read_edid,
  746. .enable = rk_hdmi_enable,
  747. };
  748. static const struct udevice_id rk_hdmi_ids[] = {
  749. { .compatible = "rockchip,rk3288-dw-hdmi" },
  750. { }
  751. };
  752. U_BOOT_DRIVER(hdmi_rockchip) = {
  753. .name = "hdmi_rockchip",
  754. .id = UCLASS_DISPLAY,
  755. .of_match = rk_hdmi_ids,
  756. .ops = &rk_hdmi_ops,
  757. .ofdata_to_platdata = rk_hdmi_ofdata_to_platdata,
  758. .probe = rk_hdmi_probe,
  759. .priv_auto_alloc_size = sizeof(struct rk_hdmi_priv),
  760. };