exynos_dp.c 25 KB

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