exynos_dp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics
  3. *
  4. * Author: Donghwa Lee <dh09.lee@samsung.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <linux/compat.h>
  12. #include <linux/err.h>
  13. #include <asm/arch/clk.h>
  14. #include <asm/arch/cpu.h>
  15. #include <asm/arch/dp_info.h>
  16. #include <asm/arch/dp.h>
  17. #include <fdtdec.h>
  18. #include <libfdt.h>
  19. #include "exynos_dp_lowlevel.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static struct exynos_dp_platform_data *dp_pd;
  22. void __exynos_set_dp_phy(unsigned int onoff)
  23. {
  24. }
  25. void exynos_set_dp_phy(unsigned int onoff)
  26. __attribute__((weak, alias("__exynos_set_dp_phy")));
  27. static void exynos_dp_disp_info(struct edp_disp_info *disp_info)
  28. {
  29. disp_info->h_total = disp_info->h_res + disp_info->h_sync_width +
  30. disp_info->h_back_porch + disp_info->h_front_porch;
  31. disp_info->v_total = disp_info->v_res + disp_info->v_sync_width +
  32. disp_info->v_back_porch + disp_info->v_front_porch;
  33. return;
  34. }
  35. static int exynos_dp_init_dp(void)
  36. {
  37. int ret;
  38. exynos_dp_reset();
  39. /* SW defined function Normal operation */
  40. exynos_dp_enable_sw_func(DP_ENABLE);
  41. ret = exynos_dp_init_analog_func();
  42. if (ret != EXYNOS_DP_SUCCESS)
  43. return ret;
  44. exynos_dp_init_hpd();
  45. exynos_dp_init_aux();
  46. return ret;
  47. }
  48. static unsigned char exynos_dp_calc_edid_check_sum(unsigned char *edid_data)
  49. {
  50. int i;
  51. unsigned char sum = 0;
  52. for (i = 0; i < EDID_BLOCK_LENGTH; i++)
  53. sum = sum + edid_data[i];
  54. return sum;
  55. }
  56. static unsigned int exynos_dp_read_edid(void)
  57. {
  58. unsigned char edid[EDID_BLOCK_LENGTH * 2];
  59. unsigned int extend_block = 0;
  60. unsigned char sum;
  61. unsigned char test_vector;
  62. int retval;
  63. /*
  64. * EDID device address is 0x50.
  65. * However, if necessary, you must have set upper address
  66. * into E-EDID in I2C device, 0x30.
  67. */
  68. /* Read Extension Flag, Number of 128-byte EDID extension blocks */
  69. exynos_dp_read_byte_from_i2c(I2C_EDID_DEVICE_ADDR, EDID_EXTENSION_FLAG,
  70. &extend_block);
  71. if (extend_block > 0) {
  72. printf("DP EDID data includes a single extension!\n");
  73. /* Read EDID data */
  74. retval = exynos_dp_read_bytes_from_i2c(I2C_EDID_DEVICE_ADDR,
  75. EDID_HEADER_PATTERN,
  76. EDID_BLOCK_LENGTH,
  77. &edid[EDID_HEADER_PATTERN]);
  78. if (retval != 0) {
  79. printf("DP EDID Read failed!\n");
  80. return -1;
  81. }
  82. sum = exynos_dp_calc_edid_check_sum(edid);
  83. if (sum != 0) {
  84. printf("DP EDID bad checksum!\n");
  85. return -1;
  86. }
  87. /* Read additional EDID data */
  88. retval = exynos_dp_read_bytes_from_i2c(I2C_EDID_DEVICE_ADDR,
  89. EDID_BLOCK_LENGTH,
  90. EDID_BLOCK_LENGTH,
  91. &edid[EDID_BLOCK_LENGTH]);
  92. if (retval != 0) {
  93. printf("DP EDID Read failed!\n");
  94. return -1;
  95. }
  96. sum = exynos_dp_calc_edid_check_sum(&edid[EDID_BLOCK_LENGTH]);
  97. if (sum != 0) {
  98. printf("DP EDID bad checksum!\n");
  99. return -1;
  100. }
  101. exynos_dp_read_byte_from_dpcd(DPCD_TEST_REQUEST,
  102. &test_vector);
  103. if (test_vector & DPCD_TEST_EDID_READ) {
  104. exynos_dp_write_byte_to_dpcd(DPCD_TEST_EDID_CHECKSUM,
  105. edid[EDID_BLOCK_LENGTH + EDID_CHECKSUM]);
  106. exynos_dp_write_byte_to_dpcd(DPCD_TEST_RESPONSE,
  107. DPCD_TEST_EDID_CHECKSUM_WRITE);
  108. }
  109. } else {
  110. debug("DP EDID data does not include any extensions.\n");
  111. /* Read EDID data */
  112. retval = exynos_dp_read_bytes_from_i2c(I2C_EDID_DEVICE_ADDR,
  113. EDID_HEADER_PATTERN,
  114. EDID_BLOCK_LENGTH,
  115. &edid[EDID_HEADER_PATTERN]);
  116. if (retval != 0) {
  117. printf("DP EDID Read failed!\n");
  118. return -1;
  119. }
  120. sum = exynos_dp_calc_edid_check_sum(edid);
  121. if (sum != 0) {
  122. printf("DP EDID bad checksum!\n");
  123. return -1;
  124. }
  125. exynos_dp_read_byte_from_dpcd(DPCD_TEST_REQUEST,
  126. &test_vector);
  127. if (test_vector & DPCD_TEST_EDID_READ) {
  128. exynos_dp_write_byte_to_dpcd(DPCD_TEST_EDID_CHECKSUM,
  129. edid[EDID_CHECKSUM]);
  130. exynos_dp_write_byte_to_dpcd(DPCD_TEST_RESPONSE,
  131. DPCD_TEST_EDID_CHECKSUM_WRITE);
  132. }
  133. }
  134. debug("DP EDID Read success!\n");
  135. return 0;
  136. }
  137. static unsigned int exynos_dp_handle_edid(struct edp_device_info *edp_info)
  138. {
  139. unsigned char buf[12];
  140. unsigned int ret;
  141. unsigned char temp;
  142. unsigned char retry_cnt;
  143. unsigned char dpcd_rev[16];
  144. unsigned char lane_bw[16];
  145. unsigned char lane_cnt[16];
  146. memset(dpcd_rev, 0, 16);
  147. memset(lane_bw, 0, 16);
  148. memset(lane_cnt, 0, 16);
  149. memset(buf, 0, 12);
  150. retry_cnt = 5;
  151. while (retry_cnt) {
  152. /* Read DPCD 0x0000-0x000b */
  153. ret = exynos_dp_read_bytes_from_dpcd(DPCD_DPCD_REV, 12,
  154. buf);
  155. if (ret != EXYNOS_DP_SUCCESS) {
  156. if (retry_cnt == 0) {
  157. printf("DP read_byte_from_dpcd() failed\n");
  158. return ret;
  159. }
  160. retry_cnt--;
  161. } else
  162. break;
  163. }
  164. /* */
  165. temp = buf[DPCD_DPCD_REV];
  166. if (temp == DP_DPCD_REV_10 || temp == DP_DPCD_REV_11)
  167. edp_info->dpcd_rev = temp;
  168. else {
  169. printf("DP Wrong DPCD Rev : %x\n", temp);
  170. return -ENODEV;
  171. }
  172. temp = buf[DPCD_MAX_LINK_RATE];
  173. if (temp == DP_LANE_BW_1_62 || temp == DP_LANE_BW_2_70)
  174. edp_info->lane_bw = temp;
  175. else {
  176. printf("DP Wrong MAX LINK RATE : %x\n", temp);
  177. return -EINVAL;
  178. }
  179. /*Refer VESA Display Port Stnadard Ver1.1a Page 120 */
  180. if (edp_info->dpcd_rev == DP_DPCD_REV_11) {
  181. temp = buf[DPCD_MAX_LANE_COUNT] & 0x1f;
  182. if (buf[DPCD_MAX_LANE_COUNT] & 0x80)
  183. edp_info->dpcd_efc = 1;
  184. else
  185. edp_info->dpcd_efc = 0;
  186. } else {
  187. temp = buf[DPCD_MAX_LANE_COUNT];
  188. edp_info->dpcd_efc = 0;
  189. }
  190. if (temp == DP_LANE_CNT_1 || temp == DP_LANE_CNT_2 ||
  191. temp == DP_LANE_CNT_4) {
  192. edp_info->lane_cnt = temp;
  193. } else {
  194. printf("DP Wrong MAX LANE COUNT : %x\n", temp);
  195. return -EINVAL;
  196. }
  197. ret = exynos_dp_read_edid();
  198. if (ret != EXYNOS_DP_SUCCESS) {
  199. printf("DP exynos_dp_read_edid() failed\n");
  200. return -EINVAL;
  201. }
  202. return ret;
  203. }
  204. static void exynos_dp_init_training(void)
  205. {
  206. /*
  207. * MACRO_RST must be applied after the PLL_LOCK to avoid
  208. * the DP inter pair skew issue for at least 10 us
  209. */
  210. exynos_dp_reset_macro();
  211. /* All DP analog module power up */
  212. exynos_dp_set_analog_power_down(POWER_ALL, 0);
  213. }
  214. static unsigned int exynos_dp_link_start(struct edp_device_info *edp_info)
  215. {
  216. unsigned char buf[5];
  217. unsigned int ret = 0;
  218. debug("DP: %s was called\n", __func__);
  219. edp_info->lt_info.lt_status = DP_LT_CR;
  220. edp_info->lt_info.ep_loop = 0;
  221. edp_info->lt_info.cr_loop[0] = 0;
  222. edp_info->lt_info.cr_loop[1] = 0;
  223. edp_info->lt_info.cr_loop[2] = 0;
  224. edp_info->lt_info.cr_loop[3] = 0;
  225. /* Set sink to D0 (Sink Not Ready) mode. */
  226. ret = exynos_dp_write_byte_to_dpcd(DPCD_SINK_POWER_STATE,
  227. DPCD_SET_POWER_STATE_D0);
  228. if (ret != EXYNOS_DP_SUCCESS) {
  229. printf("DP write_dpcd_byte failed\n");
  230. return ret;
  231. }
  232. /* Set link rate and count as you want to establish*/
  233. exynos_dp_set_link_bandwidth(edp_info->lane_bw);
  234. exynos_dp_set_lane_count(edp_info->lane_cnt);
  235. /* Setup RX configuration */
  236. buf[0] = edp_info->lane_bw;
  237. buf[1] = edp_info->lane_cnt;
  238. ret = exynos_dp_write_bytes_to_dpcd(DPCD_LINK_BW_SET, 2,
  239. buf);
  240. if (ret != EXYNOS_DP_SUCCESS) {
  241. printf("DP write_dpcd_byte failed\n");
  242. return ret;
  243. }
  244. exynos_dp_set_lane_pre_emphasis(PRE_EMPHASIS_LEVEL_0,
  245. edp_info->lane_cnt);
  246. /* Set training pattern 1 */
  247. exynos_dp_set_training_pattern(TRAINING_PTN1);
  248. /* Set RX training pattern */
  249. buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_1;
  250. buf[1] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
  251. DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
  252. buf[2] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
  253. DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
  254. buf[3] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
  255. DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
  256. buf[4] = DPCD_PRE_EMPHASIS_SET_PATTERN_2_LEVEL_0 |
  257. DPCD_VOLTAGE_SWING_SET_PATTERN_1_LEVEL_0;
  258. ret = exynos_dp_write_bytes_to_dpcd(DPCD_TRAINING_PATTERN_SET,
  259. 5, buf);
  260. if (ret != EXYNOS_DP_SUCCESS) {
  261. printf("DP write_dpcd_byte failed\n");
  262. return ret;
  263. }
  264. return ret;
  265. }
  266. static unsigned int exynos_dp_training_pattern_dis(void)
  267. {
  268. unsigned int ret = EXYNOS_DP_SUCCESS;
  269. exynos_dp_set_training_pattern(DP_NONE);
  270. ret = exynos_dp_write_byte_to_dpcd(DPCD_TRAINING_PATTERN_SET,
  271. DPCD_TRAINING_PATTERN_DISABLED);
  272. if (ret != EXYNOS_DP_SUCCESS) {
  273. printf("DP requst_link_traninig_req failed\n");
  274. return -EAGAIN;
  275. }
  276. return ret;
  277. }
  278. static unsigned int exynos_dp_enable_rx_to_enhanced_mode(unsigned char enable)
  279. {
  280. unsigned char data;
  281. unsigned int ret = EXYNOS_DP_SUCCESS;
  282. ret = exynos_dp_read_byte_from_dpcd(DPCD_LANE_COUNT_SET,
  283. &data);
  284. if (ret != EXYNOS_DP_SUCCESS) {
  285. printf("DP read_from_dpcd failed\n");
  286. return -EAGAIN;
  287. }
  288. if (enable)
  289. data = DPCD_ENHANCED_FRAME_EN | DPCD_LN_COUNT_SET(data);
  290. else
  291. data = DPCD_LN_COUNT_SET(data);
  292. ret = exynos_dp_write_byte_to_dpcd(DPCD_LANE_COUNT_SET,
  293. data);
  294. if (ret != EXYNOS_DP_SUCCESS) {
  295. printf("DP write_to_dpcd failed\n");
  296. return -EAGAIN;
  297. }
  298. return ret;
  299. }
  300. static unsigned int exynos_dp_set_enhanced_mode(unsigned char enhance_mode)
  301. {
  302. unsigned int ret = EXYNOS_DP_SUCCESS;
  303. ret = exynos_dp_enable_rx_to_enhanced_mode(enhance_mode);
  304. if (ret != EXYNOS_DP_SUCCESS) {
  305. printf("DP rx_enhance_mode failed\n");
  306. return -EAGAIN;
  307. }
  308. exynos_dp_enable_enhanced_mode(enhance_mode);
  309. return ret;
  310. }
  311. static int exynos_dp_read_dpcd_lane_stat(struct edp_device_info *edp_info,
  312. unsigned char *status)
  313. {
  314. unsigned int ret, i;
  315. unsigned char buf[2];
  316. unsigned char lane_stat[DP_LANE_CNT_4] = {0,};
  317. unsigned char shift_val[DP_LANE_CNT_4] = {0,};
  318. shift_val[0] = 0;
  319. shift_val[1] = 4;
  320. shift_val[2] = 0;
  321. shift_val[3] = 4;
  322. ret = exynos_dp_read_bytes_from_dpcd(DPCD_LANE0_1_STATUS, 2, buf);
  323. if (ret != EXYNOS_DP_SUCCESS) {
  324. printf("DP read lane status failed\n");
  325. return ret;
  326. }
  327. for (i = 0; i < edp_info->lane_cnt; i++) {
  328. lane_stat[i] = (buf[(i / 2)] >> shift_val[i]) & 0x0f;
  329. if (lane_stat[0] != lane_stat[i]) {
  330. printf("Wrong lane status\n");
  331. return -EINVAL;
  332. }
  333. }
  334. *status = lane_stat[0];
  335. return ret;
  336. }
  337. static unsigned int exynos_dp_read_dpcd_adj_req(unsigned char lane_num,
  338. unsigned char *sw, unsigned char *em)
  339. {
  340. unsigned int ret = EXYNOS_DP_SUCCESS;
  341. unsigned char buf;
  342. unsigned int dpcd_addr;
  343. unsigned char shift_val[DP_LANE_CNT_4] = {0, 4, 0, 4};
  344. /*lane_num value is used as arry index, so this range 0 ~ 3 */
  345. dpcd_addr = DPCD_ADJUST_REQUEST_LANE0_1 + (lane_num / 2);
  346. ret = exynos_dp_read_byte_from_dpcd(dpcd_addr, &buf);
  347. if (ret != EXYNOS_DP_SUCCESS) {
  348. printf("DP read adjust request failed\n");
  349. return -EAGAIN;
  350. }
  351. *sw = ((buf >> shift_val[lane_num]) & 0x03);
  352. *em = ((buf >> shift_val[lane_num]) & 0x0c) >> 2;
  353. return ret;
  354. }
  355. static int exynos_dp_equalizer_err_link(struct edp_device_info *edp_info)
  356. {
  357. int ret;
  358. ret = exynos_dp_training_pattern_dis();
  359. if (ret != EXYNOS_DP_SUCCESS) {
  360. printf("DP training_patter_disable() failed\n");
  361. edp_info->lt_info.lt_status = DP_LT_FAIL;
  362. }
  363. ret = exynos_dp_set_enhanced_mode(edp_info->dpcd_efc);
  364. if (ret != EXYNOS_DP_SUCCESS) {
  365. printf("DP set_enhanced_mode() failed\n");
  366. edp_info->lt_info.lt_status = DP_LT_FAIL;
  367. }
  368. return ret;
  369. }
  370. static int exynos_dp_reduce_link_rate(struct edp_device_info *edp_info)
  371. {
  372. int ret;
  373. if (edp_info->lane_bw == DP_LANE_BW_2_70) {
  374. edp_info->lane_bw = DP_LANE_BW_1_62;
  375. printf("DP Change lane bw to 1.62Gbps\n");
  376. edp_info->lt_info.lt_status = DP_LT_START;
  377. ret = EXYNOS_DP_SUCCESS;
  378. } else {
  379. ret = exynos_dp_training_pattern_dis();
  380. if (ret != EXYNOS_DP_SUCCESS)
  381. printf("DP training_patter_disable() failed\n");
  382. ret = exynos_dp_set_enhanced_mode(edp_info->dpcd_efc);
  383. if (ret != EXYNOS_DP_SUCCESS)
  384. printf("DP set_enhanced_mode() failed\n");
  385. edp_info->lt_info.lt_status = DP_LT_FAIL;
  386. }
  387. return ret;
  388. }
  389. static unsigned int exynos_dp_process_clock_recovery(struct edp_device_info
  390. *edp_info)
  391. {
  392. unsigned int ret = EXYNOS_DP_SUCCESS;
  393. unsigned char lane_stat;
  394. unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0, };
  395. unsigned int i;
  396. unsigned char adj_req_sw;
  397. unsigned char adj_req_em;
  398. unsigned char buf[5];
  399. debug("DP: %s was called\n", __func__);
  400. mdelay(1);
  401. ret = exynos_dp_read_dpcd_lane_stat(edp_info, &lane_stat);
  402. if (ret != EXYNOS_DP_SUCCESS) {
  403. printf("DP read lane status failed\n");
  404. edp_info->lt_info.lt_status = DP_LT_FAIL;
  405. return ret;
  406. }
  407. if (lane_stat & DP_LANE_STAT_CR_DONE) {
  408. debug("DP clock Recovery training succeed\n");
  409. exynos_dp_set_training_pattern(TRAINING_PTN2);
  410. for (i = 0; i < edp_info->lane_cnt; i++) {
  411. ret = exynos_dp_read_dpcd_adj_req(i, &adj_req_sw,
  412. &adj_req_em);
  413. if (ret != EXYNOS_DP_SUCCESS) {
  414. edp_info->lt_info.lt_status = DP_LT_FAIL;
  415. return ret;
  416. }
  417. lt_ctl_val[i] = 0;
  418. lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
  419. if ((adj_req_sw == VOLTAGE_LEVEL_3)
  420. || (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
  421. lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
  422. MAX_PRE_EMPHASIS_REACH_3;
  423. }
  424. exynos_dp_set_lanex_pre_emphasis(lt_ctl_val[i], i);
  425. }
  426. buf[0] = DPCD_SCRAMBLING_DISABLED | DPCD_TRAINING_PATTERN_2;
  427. buf[1] = lt_ctl_val[0];
  428. buf[2] = lt_ctl_val[1];
  429. buf[3] = lt_ctl_val[2];
  430. buf[4] = lt_ctl_val[3];
  431. ret = exynos_dp_write_bytes_to_dpcd(
  432. DPCD_TRAINING_PATTERN_SET, 5, buf);
  433. if (ret != EXYNOS_DP_SUCCESS) {
  434. printf("DP write traning pattern1 failed\n");
  435. edp_info->lt_info.lt_status = DP_LT_FAIL;
  436. return ret;
  437. } else
  438. edp_info->lt_info.lt_status = DP_LT_ET;
  439. } else {
  440. for (i = 0; i < edp_info->lane_cnt; i++) {
  441. lt_ctl_val[i] = exynos_dp_get_lanex_pre_emphasis(i);
  442. ret = exynos_dp_read_dpcd_adj_req(i,
  443. &adj_req_sw, &adj_req_em);
  444. if (ret != EXYNOS_DP_SUCCESS) {
  445. printf("DP read adj req failed\n");
  446. edp_info->lt_info.lt_status = DP_LT_FAIL;
  447. return ret;
  448. }
  449. if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
  450. (adj_req_em == PRE_EMPHASIS_LEVEL_3))
  451. ret = exynos_dp_reduce_link_rate(edp_info);
  452. if ((DRIVE_CURRENT_SET_0_GET(lt_ctl_val[i]) ==
  453. adj_req_sw) &&
  454. (PRE_EMPHASIS_SET_0_GET(lt_ctl_val[i]) ==
  455. adj_req_em)) {
  456. edp_info->lt_info.cr_loop[i]++;
  457. if (edp_info->lt_info.cr_loop[i] == MAX_CR_LOOP)
  458. ret = exynos_dp_reduce_link_rate(
  459. edp_info);
  460. }
  461. lt_ctl_val[i] = 0;
  462. lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
  463. if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
  464. (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
  465. lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3 |
  466. MAX_PRE_EMPHASIS_REACH_3;
  467. }
  468. exynos_dp_set_lanex_pre_emphasis(lt_ctl_val[i], i);
  469. }
  470. ret = exynos_dp_write_bytes_to_dpcd(
  471. DPCD_TRAINING_LANE0_SET, 4, lt_ctl_val);
  472. if (ret != EXYNOS_DP_SUCCESS) {
  473. printf("DP write traning pattern2 failed\n");
  474. edp_info->lt_info.lt_status = DP_LT_FAIL;
  475. return ret;
  476. }
  477. }
  478. return ret;
  479. }
  480. static unsigned int exynos_dp_process_equalizer_training(struct edp_device_info
  481. *edp_info)
  482. {
  483. unsigned int ret = EXYNOS_DP_SUCCESS;
  484. unsigned char lane_stat, adj_req_sw, adj_req_em, i;
  485. unsigned char lt_ctl_val[DP_LANE_CNT_4] = {0,};
  486. unsigned char interlane_aligned = 0;
  487. unsigned char f_bw;
  488. unsigned char f_lane_cnt;
  489. unsigned char sink_stat;
  490. mdelay(1);
  491. ret = exynos_dp_read_dpcd_lane_stat(edp_info, &lane_stat);
  492. if (ret != EXYNOS_DP_SUCCESS) {
  493. printf("DP read lane status failed\n");
  494. edp_info->lt_info.lt_status = DP_LT_FAIL;
  495. return ret;
  496. }
  497. debug("DP lane stat : %x\n", lane_stat);
  498. if (lane_stat & DP_LANE_STAT_CR_DONE) {
  499. ret = exynos_dp_read_byte_from_dpcd(DPCD_LN_ALIGN_UPDATED,
  500. &sink_stat);
  501. if (ret != EXYNOS_DP_SUCCESS) {
  502. edp_info->lt_info.lt_status = DP_LT_FAIL;
  503. return ret;
  504. }
  505. interlane_aligned = (sink_stat & DPCD_INTERLANE_ALIGN_DONE);
  506. for (i = 0; i < edp_info->lane_cnt; i++) {
  507. ret = exynos_dp_read_dpcd_adj_req(i,
  508. &adj_req_sw, &adj_req_em);
  509. if (ret != EXYNOS_DP_SUCCESS) {
  510. printf("DP read adj req 1 failed\n");
  511. edp_info->lt_info.lt_status = DP_LT_FAIL;
  512. return ret;
  513. }
  514. lt_ctl_val[i] = 0;
  515. lt_ctl_val[i] = adj_req_em << 3 | adj_req_sw;
  516. if ((adj_req_sw == VOLTAGE_LEVEL_3) ||
  517. (adj_req_em == PRE_EMPHASIS_LEVEL_3)) {
  518. lt_ctl_val[i] |= MAX_DRIVE_CURRENT_REACH_3;
  519. lt_ctl_val[i] |= MAX_PRE_EMPHASIS_REACH_3;
  520. }
  521. }
  522. if (((lane_stat&DP_LANE_STAT_CE_DONE) &&
  523. (lane_stat&DP_LANE_STAT_SYM_LOCK))
  524. && (interlane_aligned == DPCD_INTERLANE_ALIGN_DONE)) {
  525. debug("DP Equalizer training succeed\n");
  526. f_bw = exynos_dp_get_link_bandwidth();
  527. f_lane_cnt = exynos_dp_get_lane_count();
  528. debug("DP final BandWidth : %x\n", f_bw);
  529. debug("DP final Lane Count : %x\n", f_lane_cnt);
  530. edp_info->lt_info.lt_status = DP_LT_FINISHED;
  531. exynos_dp_equalizer_err_link(edp_info);
  532. } else {
  533. edp_info->lt_info.ep_loop++;
  534. if (edp_info->lt_info.ep_loop > MAX_EQ_LOOP) {
  535. if (edp_info->lane_bw == DP_LANE_BW_2_70) {
  536. ret = exynos_dp_reduce_link_rate(
  537. edp_info);
  538. } else {
  539. edp_info->lt_info.lt_status =
  540. DP_LT_FAIL;
  541. exynos_dp_equalizer_err_link(edp_info);
  542. }
  543. } else {
  544. for (i = 0; i < edp_info->lane_cnt; i++)
  545. exynos_dp_set_lanex_pre_emphasis(
  546. lt_ctl_val[i], i);
  547. ret = exynos_dp_write_bytes_to_dpcd(
  548. DPCD_TRAINING_LANE0_SET,
  549. 4, lt_ctl_val);
  550. if (ret != EXYNOS_DP_SUCCESS) {
  551. printf("DP set lt pattern failed\n");
  552. edp_info->lt_info.lt_status =
  553. DP_LT_FAIL;
  554. exynos_dp_equalizer_err_link(edp_info);
  555. }
  556. }
  557. }
  558. } else if (edp_info->lane_bw == DP_LANE_BW_2_70) {
  559. ret = exynos_dp_reduce_link_rate(edp_info);
  560. } else {
  561. edp_info->lt_info.lt_status = DP_LT_FAIL;
  562. exynos_dp_equalizer_err_link(edp_info);
  563. }
  564. return ret;
  565. }
  566. static unsigned int exynos_dp_sw_link_training(struct edp_device_info *edp_info)
  567. {
  568. unsigned int ret = 0;
  569. int training_finished;
  570. /* Turn off unnecessary lane */
  571. if (edp_info->lane_cnt == 1)
  572. exynos_dp_set_analog_power_down(CH1_BLOCK, 1);
  573. training_finished = 0;
  574. edp_info->lt_info.lt_status = DP_LT_START;
  575. /* Process here */
  576. while (!training_finished) {
  577. switch (edp_info->lt_info.lt_status) {
  578. case DP_LT_START:
  579. ret = exynos_dp_link_start(edp_info);
  580. if (ret != EXYNOS_DP_SUCCESS) {
  581. printf("DP LT:link start failed\n");
  582. return ret;
  583. }
  584. break;
  585. case DP_LT_CR:
  586. ret = exynos_dp_process_clock_recovery(edp_info);
  587. if (ret != EXYNOS_DP_SUCCESS) {
  588. printf("DP LT:clock recovery failed\n");
  589. return ret;
  590. }
  591. break;
  592. case DP_LT_ET:
  593. ret = exynos_dp_process_equalizer_training(edp_info);
  594. if (ret != EXYNOS_DP_SUCCESS) {
  595. printf("DP LT:equalizer training failed\n");
  596. return ret;
  597. }
  598. break;
  599. case DP_LT_FINISHED:
  600. training_finished = 1;
  601. break;
  602. case DP_LT_FAIL:
  603. return -1;
  604. }
  605. }
  606. return ret;
  607. }
  608. static unsigned int exynos_dp_set_link_train(struct edp_device_info *edp_info)
  609. {
  610. unsigned int ret;
  611. exynos_dp_init_training();
  612. ret = exynos_dp_sw_link_training(edp_info);
  613. if (ret != EXYNOS_DP_SUCCESS)
  614. printf("DP dp_sw_link_traning() failed\n");
  615. return ret;
  616. }
  617. static void exynos_dp_enable_scramble(unsigned int enable)
  618. {
  619. unsigned char data;
  620. if (enable) {
  621. exynos_dp_enable_scrambling(DP_ENABLE);
  622. exynos_dp_read_byte_from_dpcd(DPCD_TRAINING_PATTERN_SET,
  623. &data);
  624. exynos_dp_write_byte_to_dpcd(DPCD_TRAINING_PATTERN_SET,
  625. (u8)(data & ~DPCD_SCRAMBLING_DISABLED));
  626. } else {
  627. exynos_dp_enable_scrambling(DP_DISABLE);
  628. exynos_dp_read_byte_from_dpcd(DPCD_TRAINING_PATTERN_SET,
  629. &data);
  630. exynos_dp_write_byte_to_dpcd(DPCD_TRAINING_PATTERN_SET,
  631. (u8)(data | DPCD_SCRAMBLING_DISABLED));
  632. }
  633. }
  634. static unsigned int exynos_dp_config_video(struct edp_device_info *edp_info)
  635. {
  636. unsigned int ret = 0;
  637. unsigned int retry_cnt;
  638. mdelay(1);
  639. if (edp_info->video_info.master_mode) {
  640. printf("DP does not support master mode\n");
  641. return -ENODEV;
  642. } else {
  643. /* debug slave */
  644. exynos_dp_config_video_slave_mode(&edp_info->video_info);
  645. }
  646. exynos_dp_set_video_color_format(&edp_info->video_info);
  647. if (edp_info->video_info.bist_mode) {
  648. if (exynos_dp_config_video_bist(edp_info) != 0)
  649. return -1;
  650. }
  651. ret = exynos_dp_get_pll_lock_status();
  652. if (ret != PLL_LOCKED) {
  653. printf("DP PLL is not locked yet\n");
  654. return -EIO;
  655. }
  656. if (edp_info->video_info.master_mode == 0) {
  657. retry_cnt = 10;
  658. while (retry_cnt) {
  659. ret = exynos_dp_is_slave_video_stream_clock_on();
  660. if (ret != EXYNOS_DP_SUCCESS) {
  661. if (retry_cnt == 0) {
  662. printf("DP stream_clock_on failed\n");
  663. return ret;
  664. }
  665. retry_cnt--;
  666. mdelay(1);
  667. } else
  668. break;
  669. }
  670. }
  671. /* Set to use the register calculated M/N video */
  672. exynos_dp_set_video_cr_mn(CALCULATED_M, 0, 0);
  673. /* For video bist, Video timing must be generated by register */
  674. exynos_dp_set_video_timing_mode(VIDEO_TIMING_FROM_CAPTURE);
  675. /* Enable video bist */
  676. if (edp_info->video_info.bist_pattern != COLOR_RAMP &&
  677. edp_info->video_info.bist_pattern != BALCK_WHITE_V_LINES &&
  678. edp_info->video_info.bist_pattern != COLOR_SQUARE)
  679. exynos_dp_enable_video_bist(edp_info->video_info.bist_mode);
  680. else
  681. exynos_dp_enable_video_bist(DP_DISABLE);
  682. /* Disable video mute */
  683. exynos_dp_enable_video_mute(DP_DISABLE);
  684. /* Configure video Master or Slave mode */
  685. exynos_dp_enable_video_master(edp_info->video_info.master_mode);
  686. /* Enable video */
  687. exynos_dp_start_video();
  688. if (edp_info->video_info.master_mode == 0) {
  689. retry_cnt = 100;
  690. while (retry_cnt) {
  691. ret = exynos_dp_is_video_stream_on();
  692. if (ret != EXYNOS_DP_SUCCESS) {
  693. if (retry_cnt == 0) {
  694. printf("DP Timeout of video stream\n");
  695. return ret;
  696. }
  697. retry_cnt--;
  698. mdelay(5);
  699. } else
  700. break;
  701. }
  702. }
  703. return ret;
  704. }
  705. #ifdef CONFIG_OF_CONTROL
  706. int exynos_dp_parse_dt(const void *blob, struct edp_device_info *edp_info)
  707. {
  708. unsigned int node = fdtdec_next_compatible(blob, 0,
  709. COMPAT_SAMSUNG_EXYNOS5_DP);
  710. if (node <= 0) {
  711. debug("exynos_dp: Can't get device node for dp\n");
  712. return -ENODEV;
  713. }
  714. edp_info->disp_info.h_res = fdtdec_get_int(blob, node,
  715. "samsung,h-res", 0);
  716. edp_info->disp_info.h_sync_width = fdtdec_get_int(blob, node,
  717. "samsung,h-sync-width", 0);
  718. edp_info->disp_info.h_back_porch = fdtdec_get_int(blob, node,
  719. "samsung,h-back-porch", 0);
  720. edp_info->disp_info.h_front_porch = fdtdec_get_int(blob, node,
  721. "samsung,h-front-porch", 0);
  722. edp_info->disp_info.v_res = fdtdec_get_int(blob, node,
  723. "samsung,v-res", 0);
  724. edp_info->disp_info.v_sync_width = fdtdec_get_int(blob, node,
  725. "samsung,v-sync-width", 0);
  726. edp_info->disp_info.v_back_porch = fdtdec_get_int(blob, node,
  727. "samsung,v-back-porch", 0);
  728. edp_info->disp_info.v_front_porch = fdtdec_get_int(blob, node,
  729. "samsung,v-front-porch", 0);
  730. edp_info->disp_info.v_sync_rate = fdtdec_get_int(blob, node,
  731. "samsung,v-sync-rate", 0);
  732. edp_info->lt_info.lt_status = fdtdec_get_int(blob, node,
  733. "samsung,lt-status", 0);
  734. edp_info->video_info.master_mode = fdtdec_get_int(blob, node,
  735. "samsung,master-mode", 0);
  736. edp_info->video_info.bist_mode = fdtdec_get_int(blob, node,
  737. "samsung,bist-mode", 0);
  738. edp_info->video_info.bist_pattern = fdtdec_get_int(blob, node,
  739. "samsung,bist-pattern", 0);
  740. edp_info->video_info.h_sync_polarity = fdtdec_get_int(blob, node,
  741. "samsung,h-sync-polarity", 0);
  742. edp_info->video_info.v_sync_polarity = fdtdec_get_int(blob, node,
  743. "samsung,v-sync-polarity", 0);
  744. edp_info->video_info.interlaced = fdtdec_get_int(blob, node,
  745. "samsung,interlaced", 0);
  746. edp_info->video_info.color_space = fdtdec_get_int(blob, node,
  747. "samsung,color-space", 0);
  748. edp_info->video_info.dynamic_range = fdtdec_get_int(blob, node,
  749. "samsung,dynamic-range", 0);
  750. edp_info->video_info.ycbcr_coeff = fdtdec_get_int(blob, node,
  751. "samsung,ycbcr-coeff", 0);
  752. edp_info->video_info.color_depth = fdtdec_get_int(blob, node,
  753. "samsung,color-depth", 0);
  754. return 0;
  755. }
  756. #endif
  757. unsigned int exynos_init_dp(void)
  758. {
  759. unsigned int ret;
  760. struct edp_device_info *edp_info;
  761. edp_info = kzalloc(sizeof(struct edp_device_info), GFP_KERNEL);
  762. if (!edp_info) {
  763. debug("failed to allocate edp device object.\n");
  764. return -EFAULT;
  765. }
  766. #ifdef CONFIG_OF_CONTROL
  767. if (exynos_dp_parse_dt(gd->fdt_blob, edp_info))
  768. debug("unable to parse DP DT node\n");
  769. #else
  770. edp_info = dp_pd->edp_dev_info;
  771. if (edp_info == NULL) {
  772. debug("failed to get edp_info data.\n");
  773. return -EFAULT;
  774. }
  775. #endif
  776. exynos_dp_set_base_addr();
  777. exynos_dp_disp_info(&edp_info->disp_info);
  778. exynos_set_dp_phy(1);
  779. ret = exynos_dp_init_dp();
  780. if (ret != EXYNOS_DP_SUCCESS) {
  781. printf("DP exynos_dp_init_dp() failed\n");
  782. return ret;
  783. }
  784. ret = exynos_dp_handle_edid(edp_info);
  785. if (ret != EXYNOS_DP_SUCCESS) {
  786. printf("EDP handle_edid fail\n");
  787. return ret;
  788. }
  789. ret = exynos_dp_set_link_train(edp_info);
  790. if (ret != EXYNOS_DP_SUCCESS) {
  791. printf("DP link training fail\n");
  792. return ret;
  793. }
  794. exynos_dp_enable_scramble(DP_ENABLE);
  795. exynos_dp_enable_rx_to_enhanced_mode(DP_ENABLE);
  796. exynos_dp_enable_enhanced_mode(DP_ENABLE);
  797. exynos_dp_set_link_bandwidth(edp_info->lane_bw);
  798. exynos_dp_set_lane_count(edp_info->lane_cnt);
  799. exynos_dp_init_video();
  800. ret = exynos_dp_config_video(edp_info);
  801. if (ret != EXYNOS_DP_SUCCESS) {
  802. printf("Exynos DP init failed\n");
  803. return ret;
  804. }
  805. printf("Exynos DP init done\n");
  806. return ret;
  807. }
  808. void exynos_set_dp_platform_data(struct exynos_dp_platform_data *pd)
  809. {
  810. if (pd == NULL) {
  811. debug("pd is NULL\n");
  812. return;
  813. }
  814. dp_pd = pd;
  815. }