dp_link.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__
  6. #include <drm/drm_device.h>
  7. #include <drm/drm_of.h>
  8. #include <drm/drm_print.h>
  9. #include "dp_reg.h"
  10. #include "dp_link.h"
  11. #include "dp_panel.h"
  12. #define DP_LINK_RATE_HBR2 540000 /* kbytes */
  13. #define DP_TEST_REQUEST_MASK 0x7F
  14. enum audio_sample_rate {
  15. AUDIO_SAMPLE_RATE_32_KHZ = 0x00,
  16. AUDIO_SAMPLE_RATE_44_1_KHZ = 0x01,
  17. AUDIO_SAMPLE_RATE_48_KHZ = 0x02,
  18. AUDIO_SAMPLE_RATE_88_2_KHZ = 0x03,
  19. AUDIO_SAMPLE_RATE_96_KHZ = 0x04,
  20. AUDIO_SAMPLE_RATE_176_4_KHZ = 0x05,
  21. AUDIO_SAMPLE_RATE_192_KHZ = 0x06,
  22. };
  23. enum audio_pattern_type {
  24. AUDIO_TEST_PATTERN_OPERATOR_DEFINED = 0x00,
  25. AUDIO_TEST_PATTERN_SAWTOOTH = 0x01,
  26. };
  27. struct msm_dp_link_request {
  28. u32 test_requested;
  29. u32 test_link_rate;
  30. u32 test_lane_count;
  31. };
  32. struct msm_dp_link_private {
  33. u32 prev_sink_count;
  34. struct drm_device *drm_dev;
  35. struct drm_dp_aux *aux;
  36. struct msm_dp_link msm_dp_link;
  37. struct msm_dp_link_request request;
  38. struct mutex psm_mutex;
  39. u8 link_status[DP_LINK_STATUS_SIZE];
  40. };
  41. static int msm_dp_aux_link_power_up(struct drm_dp_aux *aux,
  42. struct msm_dp_link_info *link)
  43. {
  44. u8 value;
  45. ssize_t len;
  46. int i;
  47. if (link->revision < 0x11)
  48. return 0;
  49. len = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  50. if (len < 0)
  51. return len;
  52. value &= ~DP_SET_POWER_MASK;
  53. value |= DP_SET_POWER_D0;
  54. /* retry for 1ms to give the sink time to wake up */
  55. for (i = 0; i < 3; i++) {
  56. len = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  57. usleep_range(1000, 2000);
  58. if (len == 1)
  59. break;
  60. }
  61. return 0;
  62. }
  63. static int msm_dp_aux_link_power_down(struct drm_dp_aux *aux,
  64. struct msm_dp_link_info *link)
  65. {
  66. u8 value;
  67. int err;
  68. if (link->revision < 0x11)
  69. return 0;
  70. err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
  71. if (err < 0)
  72. return err;
  73. value &= ~DP_SET_POWER_MASK;
  74. value |= DP_SET_POWER_D3;
  75. err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
  76. if (err < 0)
  77. return err;
  78. return 0;
  79. }
  80. static int msm_dp_link_get_period(struct msm_dp_link_private *link, int const addr)
  81. {
  82. int ret = 0;
  83. u8 data;
  84. u32 const max_audio_period = 0xA;
  85. /* TEST_AUDIO_PERIOD_CH_XX */
  86. if (drm_dp_dpcd_readb(link->aux, addr, &data) < 0) {
  87. DRM_ERROR("failed to read test_audio_period (0x%x)\n", addr);
  88. ret = -EINVAL;
  89. goto exit;
  90. }
  91. /* Period - Bits 3:0 */
  92. data = data & 0xF;
  93. if ((int)data > max_audio_period) {
  94. DRM_ERROR("invalid test_audio_period_ch_1 = 0x%x\n", data);
  95. ret = -EINVAL;
  96. goto exit;
  97. }
  98. ret = data;
  99. exit:
  100. return ret;
  101. }
  102. static int msm_dp_link_parse_audio_channel_period(struct msm_dp_link_private *link)
  103. {
  104. int ret = 0;
  105. struct msm_dp_link_test_audio *req = &link->msm_dp_link.test_audio;
  106. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
  107. if (ret == -EINVAL)
  108. goto exit;
  109. req->test_audio_period_ch_1 = ret;
  110. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_1 = 0x%x\n", ret);
  111. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
  112. if (ret == -EINVAL)
  113. goto exit;
  114. req->test_audio_period_ch_2 = ret;
  115. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_2 = 0x%x\n", ret);
  116. /* TEST_AUDIO_PERIOD_CH_3 (Byte 0x275) */
  117. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
  118. if (ret == -EINVAL)
  119. goto exit;
  120. req->test_audio_period_ch_3 = ret;
  121. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_3 = 0x%x\n", ret);
  122. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
  123. if (ret == -EINVAL)
  124. goto exit;
  125. req->test_audio_period_ch_4 = ret;
  126. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_4 = 0x%x\n", ret);
  127. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
  128. if (ret == -EINVAL)
  129. goto exit;
  130. req->test_audio_period_ch_5 = ret;
  131. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_5 = 0x%x\n", ret);
  132. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
  133. if (ret == -EINVAL)
  134. goto exit;
  135. req->test_audio_period_ch_6 = ret;
  136. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_6 = 0x%x\n", ret);
  137. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
  138. if (ret == -EINVAL)
  139. goto exit;
  140. req->test_audio_period_ch_7 = ret;
  141. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_7 = 0x%x\n", ret);
  142. ret = msm_dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
  143. if (ret == -EINVAL)
  144. goto exit;
  145. req->test_audio_period_ch_8 = ret;
  146. drm_dbg_dp(link->drm_dev, "test_audio_period_ch_8 = 0x%x\n", ret);
  147. exit:
  148. return ret;
  149. }
  150. static int msm_dp_link_parse_audio_pattern_type(struct msm_dp_link_private *link)
  151. {
  152. int ret = 0;
  153. u8 data;
  154. ssize_t rlen;
  155. int const max_audio_pattern_type = 0x1;
  156. rlen = drm_dp_dpcd_readb(link->aux,
  157. DP_TEST_AUDIO_PATTERN_TYPE, &data);
  158. if (rlen < 0) {
  159. DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
  160. return rlen;
  161. }
  162. /* Audio Pattern Type - Bits 7:0 */
  163. if ((int)data > max_audio_pattern_type) {
  164. DRM_ERROR("invalid audio pattern type = 0x%x\n", data);
  165. ret = -EINVAL;
  166. goto exit;
  167. }
  168. link->msm_dp_link.test_audio.test_audio_pattern_type = data;
  169. drm_dbg_dp(link->drm_dev, "audio pattern type = 0x%x\n", data);
  170. exit:
  171. return ret;
  172. }
  173. static int msm_dp_link_parse_audio_mode(struct msm_dp_link_private *link)
  174. {
  175. int ret = 0;
  176. u8 data;
  177. ssize_t rlen;
  178. int const max_audio_sampling_rate = 0x6;
  179. int const max_audio_channel_count = 0x8;
  180. int sampling_rate = 0x0;
  181. int channel_count = 0x0;
  182. rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_AUDIO_MODE, &data);
  183. if (rlen < 0) {
  184. DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
  185. return rlen;
  186. }
  187. /* Sampling Rate - Bits 3:0 */
  188. sampling_rate = data & 0xF;
  189. if (sampling_rate > max_audio_sampling_rate) {
  190. DRM_ERROR("sampling rate (0x%x) greater than max (0x%x)\n",
  191. sampling_rate, max_audio_sampling_rate);
  192. ret = -EINVAL;
  193. goto exit;
  194. }
  195. /* Channel Count - Bits 7:4 */
  196. channel_count = ((data & 0xF0) >> 4) + 1;
  197. if (channel_count > max_audio_channel_count) {
  198. DRM_ERROR("channel_count (0x%x) greater than max (0x%x)\n",
  199. channel_count, max_audio_channel_count);
  200. ret = -EINVAL;
  201. goto exit;
  202. }
  203. link->msm_dp_link.test_audio.test_audio_sampling_rate = sampling_rate;
  204. link->msm_dp_link.test_audio.test_audio_channel_count = channel_count;
  205. drm_dbg_dp(link->drm_dev,
  206. "sampling_rate = 0x%x, channel_count = 0x%x\n",
  207. sampling_rate, channel_count);
  208. exit:
  209. return ret;
  210. }
  211. static int msm_dp_link_parse_audio_pattern_params(struct msm_dp_link_private *link)
  212. {
  213. int ret = 0;
  214. ret = msm_dp_link_parse_audio_mode(link);
  215. if (ret)
  216. goto exit;
  217. ret = msm_dp_link_parse_audio_pattern_type(link);
  218. if (ret)
  219. goto exit;
  220. ret = msm_dp_link_parse_audio_channel_period(link);
  221. exit:
  222. return ret;
  223. }
  224. static bool msm_dp_link_is_video_pattern_valid(u32 pattern)
  225. {
  226. switch (pattern) {
  227. case DP_NO_TEST_PATTERN:
  228. case DP_COLOR_RAMP:
  229. case DP_BLACK_AND_WHITE_VERTICAL_LINES:
  230. case DP_COLOR_SQUARE:
  231. return true;
  232. default:
  233. return false;
  234. }
  235. }
  236. /**
  237. * msm_dp_link_is_bit_depth_valid() - validates the bit depth requested
  238. * @tbd: bit depth requested by the sink
  239. *
  240. * Returns true if the requested bit depth is supported.
  241. */
  242. static bool msm_dp_link_is_bit_depth_valid(u32 tbd)
  243. {
  244. /* DP_TEST_VIDEO_PATTERN_NONE is treated as invalid */
  245. switch (tbd) {
  246. case DP_TEST_BIT_DEPTH_6:
  247. case DP_TEST_BIT_DEPTH_8:
  248. case DP_TEST_BIT_DEPTH_10:
  249. return true;
  250. default:
  251. return false;
  252. }
  253. }
  254. static int msm_dp_link_parse_timing_params1(struct msm_dp_link_private *link,
  255. int addr, int len, u32 *val)
  256. {
  257. u8 bp[2];
  258. int rlen;
  259. if (len != 2)
  260. return -EINVAL;
  261. /* Read the requested video link pattern (Byte 0x221). */
  262. rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
  263. if (rlen < len) {
  264. DRM_ERROR("failed to read 0x%x\n", addr);
  265. return -EINVAL;
  266. }
  267. *val = bp[1] | (bp[0] << 8);
  268. return 0;
  269. }
  270. static int msm_dp_link_parse_timing_params2(struct msm_dp_link_private *link,
  271. int addr, int len,
  272. u32 *val1, u32 *val2)
  273. {
  274. u8 bp[2];
  275. int rlen;
  276. if (len != 2)
  277. return -EINVAL;
  278. /* Read the requested video link pattern (Byte 0x221). */
  279. rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
  280. if (rlen < len) {
  281. DRM_ERROR("failed to read 0x%x\n", addr);
  282. return -EINVAL;
  283. }
  284. *val1 = (bp[0] & BIT(7)) >> 7;
  285. *val2 = bp[1] | ((bp[0] & 0x7F) << 8);
  286. return 0;
  287. }
  288. static int msm_dp_link_parse_timing_params3(struct msm_dp_link_private *link,
  289. int addr, u32 *val)
  290. {
  291. u8 bp;
  292. u32 len = 1;
  293. int rlen;
  294. rlen = drm_dp_dpcd_read(link->aux, addr, &bp, len);
  295. if (rlen < 1) {
  296. DRM_ERROR("failed to read 0x%x\n", addr);
  297. return -EINVAL;
  298. }
  299. *val = bp;
  300. return 0;
  301. }
  302. /**
  303. * msm_dp_link_parse_video_pattern_params() - parses video pattern parameters from DPCD
  304. * @link: Display Port Driver data
  305. *
  306. * Returns 0 if it successfully parses the video link pattern and the link
  307. * bit depth requested by the sink and, and if the values parsed are valid.
  308. */
  309. static int msm_dp_link_parse_video_pattern_params(struct msm_dp_link_private *link)
  310. {
  311. int ret = 0;
  312. ssize_t rlen;
  313. u8 bp;
  314. rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_PATTERN, &bp);
  315. if (rlen < 0) {
  316. DRM_ERROR("failed to read link video pattern. rlen=%zd\n",
  317. rlen);
  318. return rlen;
  319. }
  320. if (!msm_dp_link_is_video_pattern_valid(bp)) {
  321. DRM_ERROR("invalid link video pattern = 0x%x\n", bp);
  322. ret = -EINVAL;
  323. return ret;
  324. }
  325. link->msm_dp_link.test_video.test_video_pattern = bp;
  326. /* Read the requested color bit depth and dynamic range (Byte 0x232) */
  327. rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_MISC0, &bp);
  328. if (rlen < 0) {
  329. DRM_ERROR("failed to read link bit depth. rlen=%zd\n", rlen);
  330. return rlen;
  331. }
  332. /* Dynamic Range */
  333. link->msm_dp_link.test_video.test_dyn_range =
  334. (bp & DP_TEST_DYNAMIC_RANGE_CEA);
  335. /* Color bit depth */
  336. bp &= DP_TEST_BIT_DEPTH_MASK;
  337. if (!msm_dp_link_is_bit_depth_valid(bp)) {
  338. DRM_ERROR("invalid link bit depth = 0x%x\n", bp);
  339. ret = -EINVAL;
  340. return ret;
  341. }
  342. link->msm_dp_link.test_video.test_bit_depth = bp;
  343. /* resolution timing params */
  344. ret = msm_dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
  345. &link->msm_dp_link.test_video.test_h_total);
  346. if (ret) {
  347. DRM_ERROR("failed to parse test_htotal(DP_TEST_H_TOTAL_HI)\n");
  348. return ret;
  349. }
  350. ret = msm_dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
  351. &link->msm_dp_link.test_video.test_v_total);
  352. if (ret) {
  353. DRM_ERROR("failed to parse test_v_total(DP_TEST_V_TOTAL_HI)\n");
  354. return ret;
  355. }
  356. ret = msm_dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
  357. &link->msm_dp_link.test_video.test_h_start);
  358. if (ret) {
  359. DRM_ERROR("failed to parse test_h_start(DP_TEST_H_START_HI)\n");
  360. return ret;
  361. }
  362. ret = msm_dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
  363. &link->msm_dp_link.test_video.test_v_start);
  364. if (ret) {
  365. DRM_ERROR("failed to parse test_v_start(DP_TEST_V_START_HI)\n");
  366. return ret;
  367. }
  368. ret = msm_dp_link_parse_timing_params2(link, DP_TEST_HSYNC_HI, 2,
  369. &link->msm_dp_link.test_video.test_hsync_pol,
  370. &link->msm_dp_link.test_video.test_hsync_width);
  371. if (ret) {
  372. DRM_ERROR("failed to parse (DP_TEST_HSYNC_HI)\n");
  373. return ret;
  374. }
  375. ret = msm_dp_link_parse_timing_params2(link, DP_TEST_VSYNC_HI, 2,
  376. &link->msm_dp_link.test_video.test_vsync_pol,
  377. &link->msm_dp_link.test_video.test_vsync_width);
  378. if (ret) {
  379. DRM_ERROR("failed to parse (DP_TEST_VSYNC_HI)\n");
  380. return ret;
  381. }
  382. ret = msm_dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
  383. &link->msm_dp_link.test_video.test_h_width);
  384. if (ret) {
  385. DRM_ERROR("failed to parse test_h_width(DP_TEST_H_WIDTH_HI)\n");
  386. return ret;
  387. }
  388. ret = msm_dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
  389. &link->msm_dp_link.test_video.test_v_height);
  390. if (ret) {
  391. DRM_ERROR("failed to parse test_v_height\n");
  392. return ret;
  393. }
  394. ret = msm_dp_link_parse_timing_params3(link, DP_TEST_MISC1,
  395. &link->msm_dp_link.test_video.test_rr_d);
  396. link->msm_dp_link.test_video.test_rr_d &= DP_TEST_REFRESH_DENOMINATOR;
  397. if (ret) {
  398. DRM_ERROR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
  399. return ret;
  400. }
  401. ret = msm_dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
  402. &link->msm_dp_link.test_video.test_rr_n);
  403. if (ret) {
  404. DRM_ERROR("failed to parse test_rr_n\n");
  405. return ret;
  406. }
  407. drm_dbg_dp(link->drm_dev,
  408. "link video pattern = 0x%x\n"
  409. "link dynamic range = 0x%x\n"
  410. "link bit depth = 0x%x\n"
  411. "TEST_H_TOTAL = %d, TEST_V_TOTAL = %d\n"
  412. "TEST_H_START = %d, TEST_V_START = %d\n"
  413. "TEST_HSYNC_POL = %d\n"
  414. "TEST_HSYNC_WIDTH = %d\n"
  415. "TEST_VSYNC_POL = %d\n"
  416. "TEST_VSYNC_WIDTH = %d\n"
  417. "TEST_H_WIDTH = %d\n"
  418. "TEST_V_HEIGHT = %d\n"
  419. "TEST_REFRESH_DENOMINATOR = %d\n"
  420. "TEST_REFRESH_NUMERATOR = %d\n",
  421. link->msm_dp_link.test_video.test_video_pattern,
  422. link->msm_dp_link.test_video.test_dyn_range,
  423. link->msm_dp_link.test_video.test_bit_depth,
  424. link->msm_dp_link.test_video.test_h_total,
  425. link->msm_dp_link.test_video.test_v_total,
  426. link->msm_dp_link.test_video.test_h_start,
  427. link->msm_dp_link.test_video.test_v_start,
  428. link->msm_dp_link.test_video.test_hsync_pol,
  429. link->msm_dp_link.test_video.test_hsync_width,
  430. link->msm_dp_link.test_video.test_vsync_pol,
  431. link->msm_dp_link.test_video.test_vsync_width,
  432. link->msm_dp_link.test_video.test_h_width,
  433. link->msm_dp_link.test_video.test_v_height,
  434. link->msm_dp_link.test_video.test_rr_d,
  435. link->msm_dp_link.test_video.test_rr_n);
  436. return ret;
  437. }
  438. /**
  439. * msm_dp_link_parse_link_training_params() - parses link training parameters from
  440. * DPCD
  441. * @link: Display Port Driver data
  442. *
  443. * Returns 0 if it successfully parses the link rate (Byte 0x219) and lane
  444. * count (Byte 0x220), and if these values parse are valid.
  445. */
  446. static int msm_dp_link_parse_link_training_params(struct msm_dp_link_private *link)
  447. {
  448. u8 bp;
  449. ssize_t rlen;
  450. rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LINK_RATE, &bp);
  451. if (rlen < 0) {
  452. DRM_ERROR("failed to read link rate. rlen=%zd\n", rlen);
  453. return rlen;
  454. }
  455. if (!is_link_rate_valid(bp)) {
  456. DRM_ERROR("invalid link rate = 0x%x\n", bp);
  457. return -EINVAL;
  458. }
  459. link->request.test_link_rate = bp;
  460. drm_dbg_dp(link->drm_dev, "link rate = 0x%x\n",
  461. link->request.test_link_rate);
  462. rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LANE_COUNT, &bp);
  463. if (rlen < 0) {
  464. DRM_ERROR("failed to read lane count. rlen=%zd\n", rlen);
  465. return rlen;
  466. }
  467. bp &= DP_MAX_LANE_COUNT_MASK;
  468. if (!is_lane_count_valid(bp)) {
  469. DRM_ERROR("invalid lane count = 0x%x\n", bp);
  470. return -EINVAL;
  471. }
  472. link->request.test_lane_count = bp;
  473. drm_dbg_dp(link->drm_dev, "lane count = 0x%x\n",
  474. link->request.test_lane_count);
  475. return 0;
  476. }
  477. /**
  478. * msm_dp_link_parse_phy_test_params() - parses the phy link parameters
  479. * @link: Display Port Driver data
  480. *
  481. * Parses the DPCD (Byte 0x248) for the DP PHY link pattern that is being
  482. * requested.
  483. */
  484. static int msm_dp_link_parse_phy_test_params(struct msm_dp_link_private *link)
  485. {
  486. u8 data;
  487. ssize_t rlen;
  488. rlen = drm_dp_dpcd_readb(link->aux, DP_PHY_TEST_PATTERN,
  489. &data);
  490. if (rlen < 0) {
  491. DRM_ERROR("failed to read phy link pattern. rlen=%zd\n", rlen);
  492. return rlen;
  493. }
  494. link->msm_dp_link.phy_params.phy_test_pattern_sel = data & 0x07;
  495. drm_dbg_dp(link->drm_dev, "phy_test_pattern_sel = 0x%x\n", data);
  496. switch (data) {
  497. case DP_PHY_TEST_PATTERN_SEL_MASK:
  498. case DP_PHY_TEST_PATTERN_NONE:
  499. case DP_PHY_TEST_PATTERN_D10_2:
  500. case DP_PHY_TEST_PATTERN_ERROR_COUNT:
  501. case DP_PHY_TEST_PATTERN_PRBS7:
  502. case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
  503. case DP_PHY_TEST_PATTERN_CP2520:
  504. return 0;
  505. default:
  506. return -EINVAL;
  507. }
  508. }
  509. /**
  510. * msm_dp_link_is_video_audio_test_requested() - checks for audio/video link request
  511. * @link: link requested by the sink
  512. *
  513. * Returns true if the requested link is a permitted audio/video link.
  514. */
  515. static bool msm_dp_link_is_video_audio_test_requested(u32 link)
  516. {
  517. u8 video_audio_test = (DP_TEST_LINK_VIDEO_PATTERN |
  518. DP_TEST_LINK_AUDIO_PATTERN |
  519. DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
  520. return ((link & video_audio_test) &&
  521. !(link & ~video_audio_test));
  522. }
  523. /**
  524. * msm_dp_link_parse_request() - parses link request parameters from sink
  525. * @link: Display Port Driver data
  526. *
  527. * Parses the DPCD to check if an automated link is requested (Byte 0x201),
  528. * and what type of link automation is being requested (Byte 0x218).
  529. */
  530. static int msm_dp_link_parse_request(struct msm_dp_link_private *link)
  531. {
  532. int ret = 0;
  533. u8 data;
  534. ssize_t rlen;
  535. /**
  536. * Read the device service IRQ vector (Byte 0x201) to determine
  537. * whether an automated link has been requested by the sink.
  538. */
  539. rlen = drm_dp_dpcd_readb(link->aux,
  540. DP_DEVICE_SERVICE_IRQ_VECTOR, &data);
  541. if (rlen < 0) {
  542. DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
  543. return rlen;
  544. }
  545. drm_dbg_dp(link->drm_dev, "device service irq vector = 0x%x\n", data);
  546. if (!(data & DP_AUTOMATED_TEST_REQUEST)) {
  547. drm_dbg_dp(link->drm_dev, "no test requested\n");
  548. return 0;
  549. }
  550. /**
  551. * Read the link request byte (Byte 0x218) to determine what type
  552. * of automated link has been requested by the sink.
  553. */
  554. rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_REQUEST, &data);
  555. if (rlen < 0) {
  556. DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
  557. return rlen;
  558. }
  559. if (!data || (data == DP_TEST_LINK_FAUX_PATTERN)) {
  560. drm_dbg_dp(link->drm_dev, "link 0x%x not supported\n", data);
  561. goto end;
  562. }
  563. drm_dbg_dp(link->drm_dev, "Test:(0x%x) requested\n", data);
  564. link->request.test_requested = data;
  565. if (link->request.test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) {
  566. ret = msm_dp_link_parse_phy_test_params(link);
  567. if (ret)
  568. goto end;
  569. ret = msm_dp_link_parse_link_training_params(link);
  570. if (ret)
  571. goto end;
  572. }
  573. if (link->request.test_requested == DP_TEST_LINK_TRAINING) {
  574. ret = msm_dp_link_parse_link_training_params(link);
  575. if (ret)
  576. goto end;
  577. }
  578. if (msm_dp_link_is_video_audio_test_requested(
  579. link->request.test_requested)) {
  580. ret = msm_dp_link_parse_video_pattern_params(link);
  581. if (ret)
  582. goto end;
  583. ret = msm_dp_link_parse_audio_pattern_params(link);
  584. }
  585. end:
  586. /*
  587. * Send a DP_TEST_ACK if all link parameters are valid, otherwise send
  588. * a DP_TEST_NAK.
  589. */
  590. if (ret) {
  591. link->msm_dp_link.test_response = DP_TEST_NAK;
  592. } else {
  593. if (link->request.test_requested != DP_TEST_LINK_EDID_READ)
  594. link->msm_dp_link.test_response = DP_TEST_ACK;
  595. else
  596. link->msm_dp_link.test_response =
  597. DP_TEST_EDID_CHECKSUM_WRITE;
  598. }
  599. return ret;
  600. }
  601. static int msm_dp_link_parse_sink_status_field(struct msm_dp_link_private *link)
  602. {
  603. int ret;
  604. link->prev_sink_count = link->msm_dp_link.sink_count;
  605. ret = drm_dp_read_sink_count(link->aux);
  606. if (ret < 0) {
  607. DRM_ERROR("DP parse sink count failed\n");
  608. return ret;
  609. }
  610. link->msm_dp_link.sink_count = ret;
  611. ret = drm_dp_dpcd_read_link_status(link->aux,
  612. link->link_status);
  613. if (ret < 0) {
  614. DRM_ERROR("DP link status read failed\n");
  615. return ret;
  616. }
  617. return msm_dp_link_parse_request(link);
  618. }
  619. /**
  620. * msm_dp_link_process_link_training_request() - processes new training requests
  621. * @link: Display Port link data
  622. *
  623. * This function will handle new link training requests that are initiated by
  624. * the sink. In particular, it will update the requested lane count and link
  625. * rate, and then trigger the link retraining procedure.
  626. *
  627. * The function will return 0 if a link training request has been processed,
  628. * otherwise it will return -EINVAL.
  629. */
  630. static int msm_dp_link_process_link_training_request(struct msm_dp_link_private *link)
  631. {
  632. if (link->request.test_requested != DP_TEST_LINK_TRAINING)
  633. return -EINVAL;
  634. drm_dbg_dp(link->drm_dev,
  635. "Test:0x%x link rate = 0x%x, lane count = 0x%x\n",
  636. DP_TEST_LINK_TRAINING,
  637. link->request.test_link_rate,
  638. link->request.test_lane_count);
  639. link->msm_dp_link.link_params.num_lanes = link->request.test_lane_count;
  640. link->msm_dp_link.link_params.rate =
  641. drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
  642. return 0;
  643. }
  644. bool msm_dp_link_send_test_response(struct msm_dp_link *msm_dp_link)
  645. {
  646. struct msm_dp_link_private *link = NULL;
  647. int ret = 0;
  648. if (!msm_dp_link) {
  649. DRM_ERROR("invalid input\n");
  650. return false;
  651. }
  652. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  653. ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_RESPONSE,
  654. msm_dp_link->test_response);
  655. return ret == 1;
  656. }
  657. int msm_dp_link_psm_config(struct msm_dp_link *msm_dp_link,
  658. struct msm_dp_link_info *link_info, bool enable)
  659. {
  660. struct msm_dp_link_private *link = NULL;
  661. int ret = 0;
  662. if (!msm_dp_link) {
  663. DRM_ERROR("invalid params\n");
  664. return -EINVAL;
  665. }
  666. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  667. mutex_lock(&link->psm_mutex);
  668. if (enable)
  669. ret = msm_dp_aux_link_power_down(link->aux, link_info);
  670. else
  671. ret = msm_dp_aux_link_power_up(link->aux, link_info);
  672. if (ret)
  673. DRM_ERROR("Failed to %s low power mode\n", enable ?
  674. "enter" : "exit");
  675. mutex_unlock(&link->psm_mutex);
  676. return ret;
  677. }
  678. bool msm_dp_link_send_edid_checksum(struct msm_dp_link *msm_dp_link, u8 checksum)
  679. {
  680. struct msm_dp_link_private *link = NULL;
  681. int ret = 0;
  682. if (!msm_dp_link) {
  683. DRM_ERROR("invalid input\n");
  684. return false;
  685. }
  686. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  687. ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_EDID_CHECKSUM,
  688. checksum);
  689. return ret == 1;
  690. }
  691. static void msm_dp_link_parse_vx_px(struct msm_dp_link_private *link)
  692. {
  693. drm_dbg_dp(link->drm_dev, "vx: 0=%d, 1=%d, 2=%d, 3=%d\n",
  694. drm_dp_get_adjust_request_voltage(link->link_status, 0),
  695. drm_dp_get_adjust_request_voltage(link->link_status, 1),
  696. drm_dp_get_adjust_request_voltage(link->link_status, 2),
  697. drm_dp_get_adjust_request_voltage(link->link_status, 3));
  698. drm_dbg_dp(link->drm_dev, "px: 0=%d, 1=%d, 2=%d, 3=%d\n",
  699. drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0),
  700. drm_dp_get_adjust_request_pre_emphasis(link->link_status, 1),
  701. drm_dp_get_adjust_request_pre_emphasis(link->link_status, 2),
  702. drm_dp_get_adjust_request_pre_emphasis(link->link_status, 3));
  703. /**
  704. * Update the voltage and pre-emphasis levels as per DPCD request
  705. * vector.
  706. */
  707. drm_dbg_dp(link->drm_dev,
  708. "Current: v_level = 0x%x, p_level = 0x%x\n",
  709. link->msm_dp_link.phy_params.v_level,
  710. link->msm_dp_link.phy_params.p_level);
  711. link->msm_dp_link.phy_params.v_level =
  712. drm_dp_get_adjust_request_voltage(link->link_status, 0);
  713. link->msm_dp_link.phy_params.p_level =
  714. drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0);
  715. link->msm_dp_link.phy_params.p_level >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
  716. drm_dbg_dp(link->drm_dev,
  717. "Requested: v_level = 0x%x, p_level = 0x%x\n",
  718. link->msm_dp_link.phy_params.v_level,
  719. link->msm_dp_link.phy_params.p_level);
  720. }
  721. /**
  722. * msm_dp_link_process_phy_test_pattern_request() - process new phy link requests
  723. * @link: Display Port Driver data
  724. *
  725. * This function will handle new phy link pattern requests that are initiated
  726. * by the sink. The function will return 0 if a phy link pattern has been
  727. * processed, otherwise it will return -EINVAL.
  728. */
  729. static int msm_dp_link_process_phy_test_pattern_request(
  730. struct msm_dp_link_private *link)
  731. {
  732. if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
  733. drm_dbg_dp(link->drm_dev, "no phy test\n");
  734. return -EINVAL;
  735. }
  736. if (!is_link_rate_valid(link->request.test_link_rate) ||
  737. !is_lane_count_valid(link->request.test_lane_count)) {
  738. DRM_ERROR("Invalid: link rate = 0x%x,lane count = 0x%x\n",
  739. link->request.test_link_rate,
  740. link->request.test_lane_count);
  741. return -EINVAL;
  742. }
  743. drm_dbg_dp(link->drm_dev,
  744. "Current: rate = 0x%x, lane count = 0x%x\n",
  745. link->msm_dp_link.link_params.rate,
  746. link->msm_dp_link.link_params.num_lanes);
  747. drm_dbg_dp(link->drm_dev,
  748. "Requested: rate = 0x%x, lane count = 0x%x\n",
  749. link->request.test_link_rate,
  750. link->request.test_lane_count);
  751. link->msm_dp_link.link_params.num_lanes = link->request.test_lane_count;
  752. link->msm_dp_link.link_params.rate =
  753. drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
  754. msm_dp_link_parse_vx_px(link);
  755. return 0;
  756. }
  757. static bool msm_dp_link_read_psr_error_status(struct msm_dp_link_private *link)
  758. {
  759. u8 status;
  760. drm_dp_dpcd_read(link->aux, DP_PSR_ERROR_STATUS, &status, 1);
  761. if (status & DP_PSR_LINK_CRC_ERROR)
  762. DRM_ERROR("PSR LINK CRC ERROR\n");
  763. else if (status & DP_PSR_RFB_STORAGE_ERROR)
  764. DRM_ERROR("PSR RFB STORAGE ERROR\n");
  765. else if (status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
  766. DRM_ERROR("PSR VSC SDP UNCORRECTABLE ERROR\n");
  767. else
  768. return false;
  769. return true;
  770. }
  771. static bool msm_dp_link_psr_capability_changed(struct msm_dp_link_private *link)
  772. {
  773. u8 status;
  774. drm_dp_dpcd_read(link->aux, DP_PSR_ESI, &status, 1);
  775. if (status & DP_PSR_CAPS_CHANGE) {
  776. drm_dbg_dp(link->drm_dev, "PSR Capability Change\n");
  777. return true;
  778. }
  779. return false;
  780. }
  781. static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
  782. {
  783. return link_status[r - DP_LANE0_1_STATUS];
  784. }
  785. /**
  786. * msm_dp_link_process_link_status_update() - processes link status updates
  787. * @link: Display Port link module data
  788. *
  789. * This function will check for changes in the link status, e.g. clock
  790. * recovery done on all lanes, and trigger link training if there is a
  791. * failure/error on the link.
  792. *
  793. * The function will return 0 if the a link status update has been processed,
  794. * otherwise it will return -EINVAL.
  795. */
  796. static int msm_dp_link_process_link_status_update(struct msm_dp_link_private *link)
  797. {
  798. bool channel_eq_done = drm_dp_channel_eq_ok(link->link_status,
  799. link->msm_dp_link.link_params.num_lanes);
  800. bool clock_recovery_done = drm_dp_clock_recovery_ok(link->link_status,
  801. link->msm_dp_link.link_params.num_lanes);
  802. drm_dbg_dp(link->drm_dev,
  803. "channel_eq_done = %d, clock_recovery_done = %d\n",
  804. channel_eq_done, clock_recovery_done);
  805. if (channel_eq_done && clock_recovery_done)
  806. return -EINVAL;
  807. return 0;
  808. }
  809. /**
  810. * msm_dp_link_process_ds_port_status_change() - process port status changes
  811. * @link: Display Port Driver data
  812. *
  813. * This function will handle downstream port updates that are initiated by
  814. * the sink. If the downstream port status has changed, the EDID is read via
  815. * AUX.
  816. *
  817. * The function will return 0 if a downstream port update has been
  818. * processed, otherwise it will return -EINVAL.
  819. */
  820. static int msm_dp_link_process_ds_port_status_change(struct msm_dp_link_private *link)
  821. {
  822. if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
  823. DP_DOWNSTREAM_PORT_STATUS_CHANGED)
  824. goto reset;
  825. if (link->prev_sink_count == link->msm_dp_link.sink_count)
  826. return -EINVAL;
  827. reset:
  828. /* reset prev_sink_count */
  829. link->prev_sink_count = link->msm_dp_link.sink_count;
  830. return 0;
  831. }
  832. static bool msm_dp_link_is_video_pattern_requested(struct msm_dp_link_private *link)
  833. {
  834. return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
  835. && !(link->request.test_requested &
  836. DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
  837. }
  838. static bool msm_dp_link_is_audio_pattern_requested(struct msm_dp_link_private *link)
  839. {
  840. return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
  841. }
  842. static void msm_dp_link_reset_data(struct msm_dp_link_private *link)
  843. {
  844. link->request = (const struct msm_dp_link_request){ 0 };
  845. link->msm_dp_link.test_video = (const struct msm_dp_link_test_video){ 0 };
  846. link->msm_dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
  847. link->msm_dp_link.test_audio = (const struct msm_dp_link_test_audio){ 0 };
  848. link->msm_dp_link.phy_params.phy_test_pattern_sel = 0;
  849. link->msm_dp_link.sink_request = 0;
  850. link->msm_dp_link.test_response = 0;
  851. }
  852. /**
  853. * msm_dp_link_process_request() - handle HPD IRQ transition to HIGH
  854. * @msm_dp_link: pointer to link module data
  855. *
  856. * This function will handle the HPD IRQ state transitions from LOW to HIGH
  857. * (including cases when there are back to back HPD IRQ HIGH) indicating
  858. * the start of a new link training request or sink status update.
  859. */
  860. int msm_dp_link_process_request(struct msm_dp_link *msm_dp_link)
  861. {
  862. int ret = 0;
  863. struct msm_dp_link_private *link;
  864. if (!msm_dp_link) {
  865. DRM_ERROR("invalid input\n");
  866. return -EINVAL;
  867. }
  868. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  869. msm_dp_link_reset_data(link);
  870. ret = msm_dp_link_parse_sink_status_field(link);
  871. if (ret)
  872. return ret;
  873. if (link->request.test_requested == DP_TEST_LINK_EDID_READ) {
  874. msm_dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
  875. } else if (!msm_dp_link_process_ds_port_status_change(link)) {
  876. msm_dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
  877. } else if (!msm_dp_link_process_link_training_request(link)) {
  878. msm_dp_link->sink_request |= DP_TEST_LINK_TRAINING;
  879. } else if (!msm_dp_link_process_phy_test_pattern_request(link)) {
  880. msm_dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
  881. } else if (msm_dp_link_read_psr_error_status(link)) {
  882. DRM_ERROR("PSR IRQ_HPD received\n");
  883. } else if (msm_dp_link_psr_capability_changed(link)) {
  884. drm_dbg_dp(link->drm_dev, "PSR Capability changed\n");
  885. } else {
  886. ret = msm_dp_link_process_link_status_update(link);
  887. if (!ret) {
  888. msm_dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
  889. } else {
  890. if (msm_dp_link_is_video_pattern_requested(link)) {
  891. ret = 0;
  892. msm_dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
  893. }
  894. if (msm_dp_link_is_audio_pattern_requested(link)) {
  895. msm_dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
  896. ret = -EINVAL;
  897. }
  898. }
  899. }
  900. drm_dbg_dp(link->drm_dev, "sink request=%#x\n",
  901. msm_dp_link->sink_request);
  902. return ret;
  903. }
  904. int msm_dp_link_get_colorimetry_config(struct msm_dp_link *msm_dp_link)
  905. {
  906. u32 cc = DP_MISC0_COLORIMERY_CFG_LEGACY_RGB;
  907. struct msm_dp_link_private *link;
  908. if (!msm_dp_link) {
  909. DRM_ERROR("invalid input\n");
  910. return -EINVAL;
  911. }
  912. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  913. /*
  914. * Unless a video pattern CTS test is ongoing, use RGB_VESA
  915. * Only RGB_VESA and RGB_CEA supported for now
  916. */
  917. if (msm_dp_link_is_video_pattern_requested(link)) {
  918. if (link->msm_dp_link.test_video.test_dyn_range &
  919. DP_TEST_DYNAMIC_RANGE_CEA)
  920. cc = DP_MISC0_COLORIMERY_CFG_CEA_RGB;
  921. }
  922. return cc;
  923. }
  924. int msm_dp_link_adjust_levels(struct msm_dp_link *msm_dp_link, u8 *link_status)
  925. {
  926. int i;
  927. u8 max_p_level;
  928. int v_max = 0, p_max = 0;
  929. struct msm_dp_link_private *link;
  930. if (!msm_dp_link) {
  931. DRM_ERROR("invalid input\n");
  932. return -EINVAL;
  933. }
  934. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  935. /* use the max level across lanes */
  936. for (i = 0; i < msm_dp_link->link_params.num_lanes; i++) {
  937. u8 data_v = drm_dp_get_adjust_request_voltage(link_status, i);
  938. u8 data_p = drm_dp_get_adjust_request_pre_emphasis(link_status,
  939. i);
  940. drm_dbg_dp(link->drm_dev,
  941. "lane=%d req_vol_swing=%d req_pre_emphasis=%d\n",
  942. i, data_v, data_p);
  943. if (v_max < data_v)
  944. v_max = data_v;
  945. if (p_max < data_p)
  946. p_max = data_p;
  947. }
  948. msm_dp_link->phy_params.v_level = v_max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
  949. msm_dp_link->phy_params.p_level = p_max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
  950. /**
  951. * Adjust the voltage swing and pre-emphasis level combination to within
  952. * the allowable range.
  953. */
  954. if (msm_dp_link->phy_params.v_level > DP_TRAIN_LEVEL_MAX) {
  955. drm_dbg_dp(link->drm_dev,
  956. "Requested vSwingLevel=%d, change to %d\n",
  957. msm_dp_link->phy_params.v_level,
  958. DP_TRAIN_LEVEL_MAX);
  959. msm_dp_link->phy_params.v_level = DP_TRAIN_LEVEL_MAX;
  960. }
  961. if (msm_dp_link->phy_params.p_level > DP_TRAIN_LEVEL_MAX) {
  962. drm_dbg_dp(link->drm_dev,
  963. "Requested preEmphasisLevel=%d, change to %d\n",
  964. msm_dp_link->phy_params.p_level,
  965. DP_TRAIN_LEVEL_MAX);
  966. msm_dp_link->phy_params.p_level = DP_TRAIN_LEVEL_MAX;
  967. }
  968. max_p_level = DP_TRAIN_LEVEL_MAX - msm_dp_link->phy_params.v_level;
  969. if (msm_dp_link->phy_params.p_level > max_p_level) {
  970. drm_dbg_dp(link->drm_dev,
  971. "Requested preEmphasisLevel=%d, change to %d\n",
  972. msm_dp_link->phy_params.p_level,
  973. max_p_level);
  974. msm_dp_link->phy_params.p_level = max_p_level;
  975. }
  976. drm_dbg_dp(link->drm_dev, "adjusted: v_level=%d, p_level=%d\n",
  977. msm_dp_link->phy_params.v_level, msm_dp_link->phy_params.p_level);
  978. return 0;
  979. }
  980. void msm_dp_link_reset_phy_params_vx_px(struct msm_dp_link *msm_dp_link)
  981. {
  982. msm_dp_link->phy_params.v_level = 0;
  983. msm_dp_link->phy_params.p_level = 0;
  984. }
  985. u32 msm_dp_link_get_test_bits_depth(struct msm_dp_link *msm_dp_link, u32 bpp)
  986. {
  987. u32 tbd;
  988. struct msm_dp_link_private *link;
  989. link = container_of(msm_dp_link, struct msm_dp_link_private, msm_dp_link);
  990. /*
  991. * Few simplistic rules and assumptions made here:
  992. * 1. Test bit depth is bit depth per color component
  993. * 2. Assume 3 color components
  994. */
  995. switch (bpp) {
  996. case 18:
  997. tbd = DP_TEST_BIT_DEPTH_6;
  998. break;
  999. case 24:
  1000. tbd = DP_TEST_BIT_DEPTH_8;
  1001. break;
  1002. case 30:
  1003. tbd = DP_TEST_BIT_DEPTH_10;
  1004. break;
  1005. default:
  1006. drm_dbg_dp(link->drm_dev, "bpp=%d not supported, use bpc=8\n",
  1007. bpp);
  1008. tbd = DP_TEST_BIT_DEPTH_8;
  1009. break;
  1010. }
  1011. tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
  1012. return tbd;
  1013. }
  1014. static u32 msm_dp_link_link_frequencies(struct device_node *of_node)
  1015. {
  1016. struct device_node *endpoint;
  1017. u64 frequency = 0;
  1018. int cnt;
  1019. endpoint = of_graph_get_endpoint_by_regs(of_node, 1, 0); /* port@1 */
  1020. if (!endpoint)
  1021. return 0;
  1022. cnt = of_property_count_u64_elems(endpoint, "link-frequencies");
  1023. if (cnt > 0)
  1024. of_property_read_u64_index(endpoint, "link-frequencies",
  1025. cnt - 1, &frequency);
  1026. of_node_put(endpoint);
  1027. do_div(frequency,
  1028. 10 * /* from symbol rate to link rate */
  1029. 1000); /* kbytes */
  1030. return frequency;
  1031. }
  1032. /*
  1033. * Always populate msm_dp_link->lane_map with 4 lanes.
  1034. * - Use DTS "data-lanes" if present; otherwise fall back to default mapping.
  1035. * - For partial definitions, fill remaining entries with unused lanes in
  1036. * ascending order.
  1037. */
  1038. static int msm_dp_link_lane_map(struct device *dev, struct msm_dp_link *msm_dp_link)
  1039. {
  1040. struct device_node *of_node = dev->of_node;
  1041. struct device_node *endpoint;
  1042. int cnt = msm_dp_link->max_dp_lanes;
  1043. u32 tmp[DP_MAX_NUM_DP_LANES];
  1044. u32 map[DP_MAX_NUM_DP_LANES] = {0, 1, 2, 3}; /* default 1:1 mapping */
  1045. bool used[DP_MAX_NUM_DP_LANES] = {false};
  1046. int i, j = 0, ret = -EINVAL;
  1047. endpoint = of_graph_get_endpoint_by_regs(of_node, 1, -1);
  1048. if (endpoint) {
  1049. ret = of_property_read_u32_array(endpoint, "data-lanes", tmp, cnt);
  1050. if (ret)
  1051. dev_dbg(dev, "endpoint data-lanes read failed (ret=%d)\n", ret);
  1052. }
  1053. if (ret) {
  1054. ret = of_property_read_u32_array(of_node, "data-lanes", tmp, cnt);
  1055. if (ret) {
  1056. dev_info(dev, "data-lanes not defined, set to default\n");
  1057. goto out;
  1058. }
  1059. }
  1060. for (i = 0; i < cnt; i++) {
  1061. if (tmp[i] >= DP_MAX_NUM_DP_LANES) {
  1062. dev_err(dev, "data-lanes[%d]=%u out of range\n", i, tmp[i]);
  1063. return -EINVAL;
  1064. }
  1065. used[tmp[i]] = true;
  1066. map[i] = tmp[i];
  1067. }
  1068. /* Fill the remaining entries with unused physical lanes (ascending) */
  1069. for (i = cnt; i < DP_MAX_NUM_DP_LANES && j < DP_MAX_NUM_DP_LANES; j++) {
  1070. if (!used[j])
  1071. map[i++] = j;
  1072. }
  1073. out:
  1074. if (endpoint)
  1075. of_node_put(endpoint);
  1076. dev_dbg(dev, "data-lanes count %d <%d %d %d %d>\n", cnt, map[0], map[1], map[2], map[3]);
  1077. memcpy(msm_dp_link->lane_map, map, sizeof(map));
  1078. return 0;
  1079. }
  1080. static int msm_dp_link_parse_dt(struct device *dev, struct msm_dp_link *msm_dp_link)
  1081. {
  1082. struct device_node *of_node = dev->of_node;
  1083. int cnt;
  1084. /*
  1085. * data-lanes is the property of msm_dp_out endpoint
  1086. */
  1087. cnt = drm_of_get_data_lanes_count_ep(of_node, 1, 0, 1, DP_MAX_NUM_DP_LANES);
  1088. if (cnt < 0) {
  1089. /* legacy code, data-lanes is the property of mdss_dp node */
  1090. cnt = drm_of_get_data_lanes_count(of_node, 1, DP_MAX_NUM_DP_LANES);
  1091. }
  1092. if (cnt > 0)
  1093. msm_dp_link->max_dp_lanes = cnt;
  1094. else
  1095. msm_dp_link->max_dp_lanes = DP_MAX_NUM_DP_LANES; /* 4 lanes */
  1096. if (msm_dp_link_lane_map(dev, msm_dp_link)) {
  1097. dev_err(dev, "failed to parse data-lanes\n");
  1098. return -EINVAL;
  1099. }
  1100. msm_dp_link->max_dp_link_rate = msm_dp_link_link_frequencies(of_node);
  1101. if (!msm_dp_link->max_dp_link_rate)
  1102. msm_dp_link->max_dp_link_rate = DP_LINK_RATE_HBR2;
  1103. return 0;
  1104. }
  1105. struct msm_dp_link *msm_dp_link_get(struct device *dev, struct drm_dp_aux *aux)
  1106. {
  1107. struct msm_dp_link_private *link;
  1108. struct msm_dp_link *msm_dp_link;
  1109. int ret;
  1110. if (!dev || !aux) {
  1111. DRM_ERROR("invalid input\n");
  1112. return ERR_PTR(-EINVAL);
  1113. }
  1114. link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
  1115. if (!link)
  1116. return ERR_PTR(-ENOMEM);
  1117. link->aux = aux;
  1118. mutex_init(&link->psm_mutex);
  1119. msm_dp_link = &link->msm_dp_link;
  1120. ret = msm_dp_link_parse_dt(dev, msm_dp_link);
  1121. if (ret)
  1122. return ERR_PTR(ret);
  1123. return msm_dp_link;
  1124. }