dp_link.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include "dp_link.h"
  6. #include "dp_panel.h"
  7. #include "dp_debug.h"
  8. enum dynamic_range {
  9. DP_DYNAMIC_RANGE_RGB_VESA = 0x00,
  10. DP_DYNAMIC_RANGE_RGB_CEA = 0x01,
  11. DP_DYNAMIC_RANGE_UNKNOWN = 0xFFFFFFFF,
  12. };
  13. enum audio_sample_rate {
  14. AUDIO_SAMPLE_RATE_32_KHZ = 0x00,
  15. AUDIO_SAMPLE_RATE_44_1_KHZ = 0x01,
  16. AUDIO_SAMPLE_RATE_48_KHZ = 0x02,
  17. AUDIO_SAMPLE_RATE_88_2_KHZ = 0x03,
  18. AUDIO_SAMPLE_RATE_96_KHZ = 0x04,
  19. AUDIO_SAMPLE_RATE_176_4_KHZ = 0x05,
  20. AUDIO_SAMPLE_RATE_192_KHZ = 0x06,
  21. };
  22. enum audio_pattern_type {
  23. AUDIO_TEST_PATTERN_OPERATOR_DEFINED = 0x00,
  24. AUDIO_TEST_PATTERN_SAWTOOTH = 0x01,
  25. };
  26. struct dp_link_request {
  27. u32 test_requested;
  28. u32 test_link_rate;
  29. u32 test_lane_count;
  30. };
  31. struct dp_link_private {
  32. u32 prev_sink_count;
  33. struct device *dev;
  34. struct dp_aux *aux;
  35. struct dp_link dp_link;
  36. struct dp_link_request request;
  37. u8 link_status[DP_LINK_STATUS_SIZE];
  38. };
  39. static char *dp_link_get_audio_test_pattern(u32 pattern)
  40. {
  41. switch (pattern) {
  42. case AUDIO_TEST_PATTERN_OPERATOR_DEFINED:
  43. return DP_LINK_ENUM_STR(AUDIO_TEST_PATTERN_OPERATOR_DEFINED);
  44. case AUDIO_TEST_PATTERN_SAWTOOTH:
  45. return DP_LINK_ENUM_STR(AUDIO_TEST_PATTERN_SAWTOOTH);
  46. default:
  47. return "unknown";
  48. }
  49. }
  50. static char *dp_link_get_audio_sample_rate(u32 rate)
  51. {
  52. switch (rate) {
  53. case AUDIO_SAMPLE_RATE_32_KHZ:
  54. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_32_KHZ);
  55. case AUDIO_SAMPLE_RATE_44_1_KHZ:
  56. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_44_1_KHZ);
  57. case AUDIO_SAMPLE_RATE_48_KHZ:
  58. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_48_KHZ);
  59. case AUDIO_SAMPLE_RATE_88_2_KHZ:
  60. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_88_2_KHZ);
  61. case AUDIO_SAMPLE_RATE_96_KHZ:
  62. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_96_KHZ);
  63. case AUDIO_SAMPLE_RATE_176_4_KHZ:
  64. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_176_4_KHZ);
  65. case AUDIO_SAMPLE_RATE_192_KHZ:
  66. return DP_LINK_ENUM_STR(AUDIO_SAMPLE_RATE_192_KHZ);
  67. default:
  68. return "unknown";
  69. }
  70. }
  71. static int dp_link_get_period(struct dp_link_private *link, int const addr)
  72. {
  73. int ret = 0;
  74. u8 bp;
  75. u8 data;
  76. u32 const param_len = 0x1;
  77. u32 const max_audio_period = 0xA;
  78. /* TEST_AUDIO_PERIOD_CH_XX */
  79. if (drm_dp_dpcd_read(link->aux->drm_aux, addr, &bp,
  80. param_len) < param_len) {
  81. DP_ERR("failed to read test_audio_period (0x%x)\n", addr);
  82. ret = -EINVAL;
  83. goto exit;
  84. }
  85. data = bp;
  86. /* Period - Bits 3:0 */
  87. data = data & 0xF;
  88. if ((int)data > max_audio_period) {
  89. DP_ERR("invalid test_audio_period_ch_1 = 0x%x\n", data);
  90. ret = -EINVAL;
  91. goto exit;
  92. }
  93. ret = data;
  94. exit:
  95. return ret;
  96. }
  97. static int dp_link_parse_audio_channel_period(struct dp_link_private *link)
  98. {
  99. int ret = 0;
  100. struct dp_link_test_audio *req = &link->dp_link.test_audio;
  101. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
  102. if (ret == -EINVAL)
  103. goto exit;
  104. req->test_audio_period_ch_1 = ret;
  105. DP_DEBUG("test_audio_period_ch_1 = 0x%x\n", ret);
  106. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
  107. if (ret == -EINVAL)
  108. goto exit;
  109. req->test_audio_period_ch_2 = ret;
  110. DP_DEBUG("test_audio_period_ch_2 = 0x%x\n", ret);
  111. /* TEST_AUDIO_PERIOD_CH_3 (Byte 0x275) */
  112. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
  113. if (ret == -EINVAL)
  114. goto exit;
  115. req->test_audio_period_ch_3 = ret;
  116. DP_DEBUG("test_audio_period_ch_3 = 0x%x\n", ret);
  117. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
  118. if (ret == -EINVAL)
  119. goto exit;
  120. req->test_audio_period_ch_4 = ret;
  121. DP_DEBUG("test_audio_period_ch_4 = 0x%x\n", ret);
  122. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
  123. if (ret == -EINVAL)
  124. goto exit;
  125. req->test_audio_period_ch_5 = ret;
  126. DP_DEBUG("test_audio_period_ch_5 = 0x%x\n", ret);
  127. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
  128. if (ret == -EINVAL)
  129. goto exit;
  130. req->test_audio_period_ch_6 = ret;
  131. DP_DEBUG("test_audio_period_ch_6 = 0x%x\n", ret);
  132. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
  133. if (ret == -EINVAL)
  134. goto exit;
  135. req->test_audio_period_ch_7 = ret;
  136. DP_DEBUG("test_audio_period_ch_7 = 0x%x\n", ret);
  137. ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
  138. if (ret == -EINVAL)
  139. goto exit;
  140. req->test_audio_period_ch_8 = ret;
  141. DP_DEBUG("test_audio_period_ch_8 = 0x%x\n", ret);
  142. exit:
  143. return ret;
  144. }
  145. static int dp_link_parse_audio_pattern_type(struct dp_link_private *link)
  146. {
  147. int ret = 0;
  148. u8 bp;
  149. u8 data;
  150. int rlen;
  151. int const param_len = 0x1;
  152. int const max_audio_pattern_type = 0x1;
  153. rlen = drm_dp_dpcd_read(link->aux->drm_aux,
  154. DP_TEST_AUDIO_PATTERN_TYPE, &bp, param_len);
  155. if (rlen < param_len) {
  156. DP_ERR("failed to read link audio mode data\n");
  157. ret = -EINVAL;
  158. goto exit;
  159. }
  160. data = bp;
  161. /* Audio Pattern Type - Bits 7:0 */
  162. if ((int)data > max_audio_pattern_type) {
  163. DP_ERR("invalid audio pattern type = 0x%x\n", data);
  164. ret = -EINVAL;
  165. goto exit;
  166. }
  167. link->dp_link.test_audio.test_audio_pattern_type = data;
  168. DP_DEBUG("audio pattern type = %s\n",
  169. dp_link_get_audio_test_pattern(data));
  170. exit:
  171. return ret;
  172. }
  173. static int dp_link_parse_audio_mode(struct dp_link_private *link)
  174. {
  175. int ret = 0;
  176. u8 bp;
  177. u8 data;
  178. int rlen;
  179. int const param_len = 0x1;
  180. int const max_audio_sampling_rate = 0x6;
  181. int const max_audio_channel_count = 0x8;
  182. int sampling_rate = 0x0;
  183. int channel_count = 0x0;
  184. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_AUDIO_MODE,
  185. &bp, param_len);
  186. if (rlen < param_len) {
  187. DP_ERR("failed to read link audio mode data\n");
  188. ret = -EINVAL;
  189. goto exit;
  190. }
  191. data = bp;
  192. /* Sampling Rate - Bits 3:0 */
  193. sampling_rate = data & 0xF;
  194. if (sampling_rate > max_audio_sampling_rate) {
  195. DP_ERR("sampling rate (0x%x) greater than max (0x%x)\n",
  196. sampling_rate, max_audio_sampling_rate);
  197. ret = -EINVAL;
  198. goto exit;
  199. }
  200. /* Channel Count - Bits 7:4 */
  201. channel_count = ((data & 0xF0) >> 4) + 1;
  202. if (channel_count > max_audio_channel_count) {
  203. DP_ERR("channel_count (0x%x) greater than max (0x%x)\n",
  204. channel_count, max_audio_channel_count);
  205. ret = -EINVAL;
  206. goto exit;
  207. }
  208. link->dp_link.test_audio.test_audio_sampling_rate = sampling_rate;
  209. link->dp_link.test_audio.test_audio_channel_count = channel_count;
  210. DP_DEBUG("sampling_rate = %s, channel_count = 0x%x\n",
  211. dp_link_get_audio_sample_rate(sampling_rate), channel_count);
  212. exit:
  213. return ret;
  214. }
  215. /**
  216. * dp_parse_audio_pattern_params() - parses audio pattern parameters from DPCD
  217. * @link: Display Port Driver data
  218. *
  219. * Returns 0 if it successfully parses the audio link pattern parameters.
  220. */
  221. static int dp_link_parse_audio_pattern_params(struct dp_link_private *link)
  222. {
  223. int ret = 0;
  224. ret = dp_link_parse_audio_mode(link);
  225. if (ret)
  226. goto exit;
  227. ret = dp_link_parse_audio_pattern_type(link);
  228. if (ret)
  229. goto exit;
  230. ret = dp_link_parse_audio_channel_period(link);
  231. exit:
  232. return ret;
  233. }
  234. /**
  235. * dp_link_is_video_pattern_valid() - validates the video pattern
  236. * @pattern: video pattern requested by the sink
  237. *
  238. * Returns true if the requested video pattern is supported.
  239. */
  240. static bool dp_link_is_video_pattern_valid(u32 pattern)
  241. {
  242. switch (pattern) {
  243. case DP_NO_TEST_PATTERN:
  244. case DP_COLOR_RAMP:
  245. case DP_BLACK_AND_WHITE_VERTICAL_LINES:
  246. case DP_COLOR_SQUARE:
  247. return true;
  248. default:
  249. return false;
  250. }
  251. }
  252. static char *dp_link_video_pattern_to_string(u32 test_video_pattern)
  253. {
  254. switch (test_video_pattern) {
  255. case DP_NO_TEST_PATTERN:
  256. return DP_LINK_ENUM_STR(DP_NO_TEST_PATTERN);
  257. case DP_COLOR_RAMP:
  258. return DP_LINK_ENUM_STR(DP_COLOR_RAMP);
  259. case DP_BLACK_AND_WHITE_VERTICAL_LINES:
  260. return DP_LINK_ENUM_STR(DP_BLACK_AND_WHITE_VERTICAL_LINES);
  261. case DP_COLOR_SQUARE:
  262. return DP_LINK_ENUM_STR(DP_COLOR_SQUARE);
  263. default:
  264. return "unknown";
  265. }
  266. }
  267. /**
  268. * dp_link_is_dynamic_range_valid() - validates the dynamic range
  269. * @bit_depth: the dynamic range value to be checked
  270. *
  271. * Returns true if the dynamic range value is supported.
  272. */
  273. static bool dp_link_is_dynamic_range_valid(u32 dr)
  274. {
  275. switch (dr) {
  276. case DP_DYNAMIC_RANGE_RGB_VESA:
  277. case DP_DYNAMIC_RANGE_RGB_CEA:
  278. return true;
  279. default:
  280. return false;
  281. }
  282. }
  283. static char *dp_link_dynamic_range_to_string(u32 dr)
  284. {
  285. switch (dr) {
  286. case DP_DYNAMIC_RANGE_RGB_VESA:
  287. return DP_LINK_ENUM_STR(DP_DYNAMIC_RANGE_RGB_VESA);
  288. case DP_DYNAMIC_RANGE_RGB_CEA:
  289. return DP_LINK_ENUM_STR(DP_DYNAMIC_RANGE_RGB_CEA);
  290. case DP_DYNAMIC_RANGE_UNKNOWN:
  291. default:
  292. return "unknown";
  293. }
  294. }
  295. /**
  296. * dp_link_is_bit_depth_valid() - validates the bit depth requested
  297. * @bit_depth: bit depth requested by the sink
  298. *
  299. * Returns true if the requested bit depth is supported.
  300. */
  301. static bool dp_link_is_bit_depth_valid(u32 tbd)
  302. {
  303. /* DP_TEST_VIDEO_PATTERN_NONE is treated as invalid */
  304. switch (tbd) {
  305. case DP_TEST_BIT_DEPTH_6:
  306. case DP_TEST_BIT_DEPTH_8:
  307. case DP_TEST_BIT_DEPTH_10:
  308. return true;
  309. default:
  310. return false;
  311. }
  312. }
  313. static char *dp_link_bit_depth_to_string(u32 tbd)
  314. {
  315. switch (tbd) {
  316. case DP_TEST_BIT_DEPTH_6:
  317. return DP_LINK_ENUM_STR(DP_TEST_BIT_DEPTH_6);
  318. case DP_TEST_BIT_DEPTH_8:
  319. return DP_LINK_ENUM_STR(DP_TEST_BIT_DEPTH_8);
  320. case DP_TEST_BIT_DEPTH_10:
  321. return DP_LINK_ENUM_STR(DP_TEST_BIT_DEPTH_10);
  322. case DP_TEST_BIT_DEPTH_UNKNOWN:
  323. default:
  324. return "unknown";
  325. }
  326. }
  327. static int dp_link_parse_timing_params1(struct dp_link_private *link,
  328. int const addr, int const len, u32 *val)
  329. {
  330. u8 bp[2];
  331. int rlen;
  332. if (len < 2)
  333. return -EINVAL;
  334. /* Read the requested video link pattern (Byte 0x221). */
  335. rlen = drm_dp_dpcd_read(link->aux->drm_aux, addr, bp, len);
  336. if (rlen < len) {
  337. DP_ERR("failed to read 0x%x\n", addr);
  338. return -EINVAL;
  339. }
  340. *val = bp[1] | (bp[0] << 8);
  341. return 0;
  342. }
  343. static int dp_link_parse_timing_params2(struct dp_link_private *link,
  344. int const addr, int const len, u32 *val1, u32 *val2)
  345. {
  346. u8 bp[2];
  347. int rlen;
  348. if (len < 2)
  349. return -EINVAL;
  350. /* Read the requested video link pattern (Byte 0x221). */
  351. rlen = drm_dp_dpcd_read(link->aux->drm_aux, addr, bp, len);
  352. if (rlen < len) {
  353. DP_ERR("failed to read 0x%x\n", addr);
  354. return -EINVAL;
  355. }
  356. *val1 = (bp[0] & BIT(7)) >> 7;
  357. *val2 = bp[1] | ((bp[0] & 0x7F) << 8);
  358. return 0;
  359. }
  360. static int dp_link_parse_timing_params3(struct dp_link_private *link,
  361. int const addr, u32 *val)
  362. {
  363. u8 bp;
  364. u32 len = 1;
  365. int rlen;
  366. rlen = drm_dp_dpcd_read(link->aux->drm_aux, addr, &bp, len);
  367. if (rlen < 1) {
  368. DP_ERR("failed to read 0x%x\n", addr);
  369. return -EINVAL;
  370. }
  371. *val = bp;
  372. return 0;
  373. }
  374. /**
  375. * dp_parse_video_pattern_params() - parses video pattern parameters from DPCD
  376. * @link: Display Port Driver data
  377. *
  378. * Returns 0 if it successfully parses the video link pattern and the link
  379. * bit depth requested by the sink and, and if the values parsed are valid.
  380. */
  381. static int dp_link_parse_video_pattern_params(struct dp_link_private *link)
  382. {
  383. int ret = 0;
  384. int rlen;
  385. u8 bp;
  386. u8 data;
  387. u32 dyn_range;
  388. int const param_len = 0x1;
  389. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_PATTERN,
  390. &bp, param_len);
  391. if (rlen < param_len) {
  392. DP_ERR("failed to read link video pattern\n");
  393. ret = -EINVAL;
  394. goto exit;
  395. }
  396. data = bp;
  397. if (!dp_link_is_video_pattern_valid(data)) {
  398. DP_ERR("invalid link video pattern = 0x%x\n", data);
  399. ret = -EINVAL;
  400. goto exit;
  401. }
  402. link->dp_link.test_video.test_video_pattern = data;
  403. DP_DEBUG("link video pattern = 0x%x (%s)\n",
  404. link->dp_link.test_video.test_video_pattern,
  405. dp_link_video_pattern_to_string(
  406. link->dp_link.test_video.test_video_pattern));
  407. /* Read the requested color bit depth and dynamic range (Byte 0x232) */
  408. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_MISC0,
  409. &bp, param_len);
  410. if (rlen < param_len) {
  411. DP_ERR("failed to read link bit depth\n");
  412. ret = -EINVAL;
  413. goto exit;
  414. }
  415. data = bp;
  416. /* Dynamic Range */
  417. dyn_range = (data & DP_TEST_DYNAMIC_RANGE_CEA) >> 3;
  418. if (!dp_link_is_dynamic_range_valid(dyn_range)) {
  419. DP_ERR("invalid link dynamic range = 0x%x\n", dyn_range);
  420. ret = -EINVAL;
  421. goto exit;
  422. }
  423. link->dp_link.test_video.test_dyn_range = dyn_range;
  424. DP_DEBUG("link dynamic range = 0x%x (%s)\n",
  425. link->dp_link.test_video.test_dyn_range,
  426. dp_link_dynamic_range_to_string(
  427. link->dp_link.test_video.test_dyn_range));
  428. /* Color bit depth */
  429. data &= DP_TEST_BIT_DEPTH_MASK;
  430. if (!dp_link_is_bit_depth_valid(data)) {
  431. DP_ERR("invalid link bit depth = 0x%x\n", data);
  432. ret = -EINVAL;
  433. goto exit;
  434. }
  435. link->dp_link.test_video.test_bit_depth = data;
  436. DP_DEBUG("link bit depth = 0x%x (%s)\n",
  437. link->dp_link.test_video.test_bit_depth,
  438. dp_link_bit_depth_to_string(
  439. link->dp_link.test_video.test_bit_depth));
  440. /* resolution timing params */
  441. ret = dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
  442. &link->dp_link.test_video.test_h_total);
  443. if (ret) {
  444. DP_ERR("failed to parse test_h_total (DP_TEST_H_TOTAL_HI)\n");
  445. goto exit;
  446. }
  447. DP_DEBUG("TEST_H_TOTAL = %d\n", link->dp_link.test_video.test_h_total);
  448. ret = dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
  449. &link->dp_link.test_video.test_v_total);
  450. if (ret) {
  451. DP_ERR("failed to parse test_v_total (DP_TEST_V_TOTAL_HI)\n");
  452. goto exit;
  453. }
  454. DP_DEBUG("TEST_V_TOTAL = %d\n", link->dp_link.test_video.test_v_total);
  455. ret = dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
  456. &link->dp_link.test_video.test_h_start);
  457. if (ret) {
  458. DP_ERR("failed to parse test_h_start (DP_TEST_H_START_HI)\n");
  459. goto exit;
  460. }
  461. DP_DEBUG("TEST_H_START = %d\n", link->dp_link.test_video.test_h_start);
  462. ret = dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
  463. &link->dp_link.test_video.test_v_start);
  464. if (ret) {
  465. DP_ERR("failed to parse test_v_start (DP_TEST_V_START_HI)\n");
  466. goto exit;
  467. }
  468. DP_DEBUG("TEST_V_START = %d\n", link->dp_link.test_video.test_v_start);
  469. ret = dp_link_parse_timing_params2(link, DP_TEST_HSYNC_HI, 2,
  470. &link->dp_link.test_video.test_hsync_pol,
  471. &link->dp_link.test_video.test_hsync_width);
  472. if (ret) {
  473. DP_ERR("failed to parse (DP_TEST_HSYNC_HI)\n");
  474. goto exit;
  475. }
  476. DP_DEBUG("TEST_HSYNC_POL = %d\n",
  477. link->dp_link.test_video.test_hsync_pol);
  478. DP_DEBUG("TEST_HSYNC_WIDTH = %d\n",
  479. link->dp_link.test_video.test_hsync_width);
  480. ret = dp_link_parse_timing_params2(link, DP_TEST_VSYNC_HI, 2,
  481. &link->dp_link.test_video.test_vsync_pol,
  482. &link->dp_link.test_video.test_vsync_width);
  483. if (ret) {
  484. DP_ERR("failed to parse (DP_TEST_VSYNC_HI)\n");
  485. goto exit;
  486. }
  487. DP_DEBUG("TEST_VSYNC_POL = %d\n",
  488. link->dp_link.test_video.test_vsync_pol);
  489. DP_DEBUG("TEST_VSYNC_WIDTH = %d\n",
  490. link->dp_link.test_video.test_vsync_width);
  491. ret = dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
  492. &link->dp_link.test_video.test_h_width);
  493. if (ret) {
  494. DP_ERR("failed to parse test_h_width (DP_TEST_H_WIDTH_HI)\n");
  495. goto exit;
  496. }
  497. DP_DEBUG("TEST_H_WIDTH = %d\n", link->dp_link.test_video.test_h_width);
  498. ret = dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
  499. &link->dp_link.test_video.test_v_height);
  500. if (ret) {
  501. DP_ERR("failed to parse test_v_height (DP_TEST_V_HEIGHT_HI)\n");
  502. goto exit;
  503. }
  504. DP_DEBUG("TEST_V_HEIGHT = %d\n",
  505. link->dp_link.test_video.test_v_height);
  506. ret = dp_link_parse_timing_params3(link, DP_TEST_MISC1,
  507. &link->dp_link.test_video.test_rr_d);
  508. link->dp_link.test_video.test_rr_d &= DP_TEST_REFRESH_DENOMINATOR;
  509. if (ret) {
  510. DP_ERR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
  511. goto exit;
  512. }
  513. DP_DEBUG("TEST_REFRESH_DENOMINATOR = %d\n",
  514. link->dp_link.test_video.test_rr_d);
  515. ret = dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
  516. &link->dp_link.test_video.test_rr_n);
  517. if (ret) {
  518. DP_ERR("failed to parse test_rr_n (DP_TEST_REFRESH_RATE_NUMERATOR)\n");
  519. goto exit;
  520. }
  521. DP_DEBUG("TEST_REFRESH_NUMERATOR = %d\n",
  522. link->dp_link.test_video.test_rr_n);
  523. exit:
  524. return ret;
  525. }
  526. /**
  527. * dp_link_parse_link_training_params() - parses link training parameters from
  528. * DPCD
  529. * @link: Display Port Driver data
  530. *
  531. * Returns 0 if it successfully parses the link rate (Byte 0x219) and lane
  532. * count (Byte 0x220), and if these values parse are valid.
  533. */
  534. static int dp_link_parse_link_training_params(struct dp_link_private *link)
  535. {
  536. u8 bp;
  537. u8 data;
  538. int ret = 0;
  539. int rlen;
  540. int const param_len = 0x1;
  541. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_LINK_RATE,
  542. &bp, param_len);
  543. if (rlen < param_len) {
  544. DP_ERR("failed to read link rate\n");
  545. ret = -EINVAL;
  546. goto exit;
  547. }
  548. data = bp;
  549. if (!is_link_rate_valid(data)) {
  550. DP_ERR("invalid link rate = 0x%x\n", data);
  551. ret = -EINVAL;
  552. goto exit;
  553. }
  554. link->request.test_link_rate = data;
  555. DP_DEBUG("link rate = 0x%x\n", link->request.test_link_rate);
  556. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_LANE_COUNT,
  557. &bp, param_len);
  558. if (rlen < param_len) {
  559. DP_ERR("failed to read lane count\n");
  560. ret = -EINVAL;
  561. goto exit;
  562. }
  563. data = bp;
  564. data &= 0x1F;
  565. if (!is_lane_count_valid(data)) {
  566. DP_ERR("invalid lane count = 0x%x\n", data);
  567. ret = -EINVAL;
  568. goto exit;
  569. }
  570. link->request.test_lane_count = data;
  571. DP_DEBUG("lane count = 0x%x\n", link->request.test_lane_count);
  572. exit:
  573. return ret;
  574. }
  575. static bool dp_link_is_phy_test_pattern_supported(u32 phy_test_pattern_sel)
  576. {
  577. switch (phy_test_pattern_sel) {
  578. case DP_TEST_PHY_PATTERN_NONE:
  579. case DP_TEST_PHY_PATTERN_D10_2_NO_SCRAMBLING:
  580. case DP_TEST_PHY_PATTERN_SYMBOL_ERR_MEASUREMENT_CNT:
  581. case DP_TEST_PHY_PATTERN_PRBS7:
  582. case DP_TEST_PHY_PATTERN_80_BIT_CUSTOM_PATTERN:
  583. case DP_TEST_PHY_PATTERN_CP2520_PATTERN_1:
  584. case DP_TEST_PHY_PATTERN_CP2520_PATTERN_3:
  585. return true;
  586. default:
  587. return false;
  588. }
  589. }
  590. /**
  591. * dp_parse_phy_test_params() - parses the phy link parameters
  592. * @link: Display Port Driver data
  593. *
  594. * Parses the DPCD (Byte 0x248) for the DP PHY link pattern that is being
  595. * requested.
  596. */
  597. static int dp_link_parse_phy_test_params(struct dp_link_private *link)
  598. {
  599. u8 bp;
  600. u8 data;
  601. int rlen;
  602. int const param_len = 0x1;
  603. int ret = 0;
  604. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_PHY_PATTERN,
  605. &bp, param_len);
  606. if (rlen < param_len) {
  607. DP_ERR("failed to read phy link pattern\n");
  608. ret = -EINVAL;
  609. goto end;
  610. }
  611. data = bp;
  612. link->dp_link.phy_params.phy_test_pattern_sel = data;
  613. DP_DEBUG("phy_test_pattern_sel = %s\n",
  614. dp_link_get_phy_test_pattern(data));
  615. if (!dp_link_is_phy_test_pattern_supported(data))
  616. ret = -EINVAL;
  617. end:
  618. return ret;
  619. }
  620. /**
  621. * dp_link_is_video_audio_test_requested() - checks for audio/video link request
  622. * @link: link requested by the sink
  623. *
  624. * Returns true if the requested link is a permitted audio/video link.
  625. */
  626. static bool dp_link_is_video_audio_test_requested(u32 link)
  627. {
  628. return (link == DP_TEST_LINK_VIDEO_PATTERN) ||
  629. (link == (DP_TEST_LINK_AUDIO_PATTERN |
  630. DP_TEST_LINK_VIDEO_PATTERN)) ||
  631. (link == DP_TEST_LINK_AUDIO_PATTERN) ||
  632. (link == (DP_TEST_LINK_AUDIO_PATTERN |
  633. DP_TEST_LINK_AUDIO_DISABLED_VIDEO));
  634. }
  635. /**
  636. * dp_link_supported() - checks if link requested by sink is supported
  637. * @test_requested: link requested by the sink
  638. *
  639. * Returns true if the requested link is supported.
  640. */
  641. static bool dp_link_is_test_supported(u32 test_requested)
  642. {
  643. return (test_requested == DP_TEST_LINK_TRAINING) ||
  644. (test_requested == DP_TEST_LINK_EDID_READ) ||
  645. (test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) ||
  646. dp_link_is_video_audio_test_requested(test_requested);
  647. }
  648. static bool dp_link_is_test_edid_read(struct dp_link_private *link)
  649. {
  650. return (link->request.test_requested == DP_TEST_LINK_EDID_READ);
  651. }
  652. /**
  653. * dp_sink_parse_test_request() - parses link request parameters from sink
  654. * @link: Display Port Driver data
  655. *
  656. * Parses the DPCD to check if an automated link is requested (Byte 0x201),
  657. * and what type of link automation is being requested (Byte 0x218).
  658. */
  659. static int dp_link_parse_request(struct dp_link_private *link)
  660. {
  661. int ret = 0;
  662. u8 bp;
  663. u8 data;
  664. int rlen;
  665. u32 const param_len = 0x1;
  666. /**
  667. * Read the device service IRQ vector (Byte 0x201) to determine
  668. * whether an automated link has been requested by the sink.
  669. */
  670. rlen = drm_dp_dpcd_read(link->aux->drm_aux,
  671. DP_DEVICE_SERVICE_IRQ_VECTOR, &bp, param_len);
  672. if (rlen < param_len) {
  673. DP_ERR("aux read failed\n");
  674. ret = -EINVAL;
  675. goto end;
  676. }
  677. data = bp;
  678. if (!(data & DP_AUTOMATED_TEST_REQUEST))
  679. return 0;
  680. /**
  681. * Read the link request byte (Byte 0x218) to determine what type
  682. * of automated link has been requested by the sink.
  683. */
  684. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_TEST_REQUEST,
  685. &bp, param_len);
  686. if (rlen < param_len) {
  687. DP_ERR("aux read failed\n");
  688. ret = -EINVAL;
  689. goto end;
  690. }
  691. data = bp;
  692. if (!dp_link_is_test_supported(data)) {
  693. DP_DEBUG("link 0x%x not supported\n", data);
  694. goto end;
  695. }
  696. link->request.test_requested = data;
  697. if (link->request.test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) {
  698. ret = dp_link_parse_phy_test_params(link);
  699. if (ret)
  700. goto end;
  701. ret = dp_link_parse_link_training_params(link);
  702. }
  703. if (link->request.test_requested == DP_TEST_LINK_TRAINING)
  704. ret = dp_link_parse_link_training_params(link);
  705. if (dp_link_is_video_audio_test_requested(
  706. link->request.test_requested)) {
  707. ret = dp_link_parse_video_pattern_params(link);
  708. if (ret)
  709. goto end;
  710. ret = dp_link_parse_audio_pattern_params(link);
  711. }
  712. end:
  713. /**
  714. * Send a DP_TEST_ACK if all link parameters are valid, otherwise send
  715. * a DP_TEST_NAK.
  716. */
  717. if (ret) {
  718. link->dp_link.test_response = DP_TEST_NAK;
  719. } else {
  720. if (!dp_link_is_test_edid_read(link))
  721. link->dp_link.test_response = DP_TEST_ACK;
  722. else
  723. link->dp_link.test_response =
  724. DP_TEST_EDID_CHECKSUM_WRITE;
  725. }
  726. return ret;
  727. }
  728. /**
  729. * dp_link_parse_sink_count() - parses the sink count
  730. *
  731. * Parses the DPCD to check if there is an update to the sink count
  732. * (Byte 0x200), and whether all the sink devices connected have Content
  733. * Protection enabled.
  734. */
  735. static int dp_link_parse_sink_count(struct dp_link *dp_link)
  736. {
  737. int rlen;
  738. int const param_len = 0x1;
  739. struct dp_link_private *link = container_of(dp_link,
  740. struct dp_link_private, dp_link);
  741. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_SINK_COUNT,
  742. &link->dp_link.sink_count.count, param_len);
  743. if (rlen < param_len) {
  744. DP_ERR("failed to read sink count\n");
  745. return -EINVAL;
  746. }
  747. link->dp_link.sink_count.cp_ready =
  748. link->dp_link.sink_count.count & DP_SINK_CP_READY;
  749. /* BIT 7, BIT 5:0 */
  750. link->dp_link.sink_count.count =
  751. DP_GET_SINK_COUNT(link->dp_link.sink_count.count);
  752. DP_DEBUG("sink_count = 0x%x, cp_ready = 0x%x\n",
  753. link->dp_link.sink_count.count,
  754. link->dp_link.sink_count.cp_ready);
  755. return 0;
  756. }
  757. static void dp_link_parse_sink_status_field(struct dp_link_private *link)
  758. {
  759. int len = 0;
  760. link->prev_sink_count = link->dp_link.sink_count.count;
  761. dp_link_parse_sink_count(&link->dp_link);
  762. len = drm_dp_dpcd_read_link_status(link->aux->drm_aux,
  763. link->link_status);
  764. if (len < DP_LINK_STATUS_SIZE)
  765. DP_ERR("DP link status read failed\n");
  766. dp_link_parse_request(link);
  767. }
  768. static bool dp_link_is_link_training_requested(struct dp_link_private *link)
  769. {
  770. return (link->request.test_requested == DP_TEST_LINK_TRAINING);
  771. }
  772. /**
  773. * dp_link_process_link_training_request() - processes new training requests
  774. * @link: Display Port link data
  775. *
  776. * This function will handle new link training requests that are initiated by
  777. * the sink. In particular, it will update the requested lane count and link
  778. * link rate, and then trigger the link retraining procedure.
  779. *
  780. * The function will return 0 if a link training request has been processed,
  781. * otherwise it will return -EINVAL.
  782. */
  783. static int dp_link_process_link_training_request(struct dp_link_private *link)
  784. {
  785. if (!dp_link_is_link_training_requested(link))
  786. return -EINVAL;
  787. DP_DEBUG("%s link rate = 0x%x, lane count = 0x%x\n",
  788. dp_link_get_test_name(DP_TEST_LINK_TRAINING),
  789. link->request.test_link_rate,
  790. link->request.test_lane_count);
  791. link->dp_link.link_params.lane_count = link->request.test_lane_count;
  792. link->dp_link.link_params.bw_code = link->request.test_link_rate;
  793. return 0;
  794. }
  795. static void dp_link_send_test_response(struct dp_link *dp_link)
  796. {
  797. struct dp_link_private *link = NULL;
  798. u32 const response_len = 0x1;
  799. if (!dp_link) {
  800. DP_ERR("invalid input\n");
  801. return;
  802. }
  803. link = container_of(dp_link, struct dp_link_private, dp_link);
  804. drm_dp_dpcd_write(link->aux->drm_aux, DP_TEST_RESPONSE,
  805. &dp_link->test_response, response_len);
  806. }
  807. static int dp_link_psm_config(struct dp_link *dp_link,
  808. struct drm_dp_link *link_info, bool enable)
  809. {
  810. struct dp_link_private *link = NULL;
  811. int ret = 0;
  812. if (!dp_link) {
  813. DP_ERR("invalid params\n");
  814. return -EINVAL;
  815. }
  816. link = container_of(dp_link, struct dp_link_private, dp_link);
  817. if (enable)
  818. ret = drm_dp_link_power_down(link->aux->drm_aux, link_info);
  819. else
  820. ret = drm_dp_link_power_up(link->aux->drm_aux, link_info);
  821. if (ret)
  822. DP_ERR("Failed to %s low power mode\n",
  823. (enable ? "enter" : "exit"));
  824. return ret;
  825. }
  826. static void dp_link_send_edid_checksum(struct dp_link *dp_link, u8 checksum)
  827. {
  828. struct dp_link_private *link = NULL;
  829. u32 const response_len = 0x1;
  830. if (!dp_link) {
  831. DP_ERR("invalid input\n");
  832. return;
  833. }
  834. link = container_of(dp_link, struct dp_link_private, dp_link);
  835. drm_dp_dpcd_write(link->aux->drm_aux, DP_TEST_EDID_CHECKSUM,
  836. &checksum, response_len);
  837. }
  838. static int dp_link_parse_vx_px(struct dp_link_private *link)
  839. {
  840. u8 bp;
  841. u8 data;
  842. int const param_len = 0x1;
  843. int ret = 0;
  844. u32 v0, p0, v1, p1, v2, p2, v3, p3;
  845. int rlen;
  846. DP_DEBUG("\n");
  847. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_ADJUST_REQUEST_LANE0_1,
  848. &bp, param_len);
  849. if (rlen < param_len) {
  850. DP_ERR("failed reading lanes 0/1\n");
  851. ret = -EINVAL;
  852. goto end;
  853. }
  854. data = bp;
  855. DP_DEBUG("lanes 0/1 (Byte 0x206): 0x%x\n", data);
  856. v0 = data & 0x3;
  857. data = data >> 2;
  858. p0 = data & 0x3;
  859. data = data >> 2;
  860. v1 = data & 0x3;
  861. data = data >> 2;
  862. p1 = data & 0x3;
  863. data = data >> 2;
  864. rlen = drm_dp_dpcd_read(link->aux->drm_aux, DP_ADJUST_REQUEST_LANE2_3,
  865. &bp, param_len);
  866. if (rlen < param_len) {
  867. DP_ERR("failed reading lanes 2/3\n");
  868. ret = -EINVAL;
  869. goto end;
  870. }
  871. data = bp;
  872. DP_DEBUG("lanes 2/3 (Byte 0x207): 0x%x\n", data);
  873. v2 = data & 0x3;
  874. data = data >> 2;
  875. p2 = data & 0x3;
  876. data = data >> 2;
  877. v3 = data & 0x3;
  878. data = data >> 2;
  879. p3 = data & 0x3;
  880. data = data >> 2;
  881. DP_DEBUG("vx: 0=%d, 1=%d, 2=%d, 3=%d\n", v0, v1, v2, v3);
  882. DP_DEBUG("px: 0=%d, 1=%d, 2=%d, 3=%d\n", p0, p1, p2, p3);
  883. /**
  884. * Update the voltage and pre-emphasis levels as per DPCD request
  885. * vector.
  886. */
  887. DP_DEBUG("Current: v_level = 0x%x, p_level = 0x%x\n",
  888. link->dp_link.phy_params.v_level,
  889. link->dp_link.phy_params.p_level);
  890. DP_DEBUG("Requested: v_level = 0x%x, p_level = 0x%x\n", v0, p0);
  891. link->dp_link.phy_params.v_level = v0;
  892. link->dp_link.phy_params.p_level = p0;
  893. DP_DEBUG("Success\n");
  894. end:
  895. return ret;
  896. }
  897. /**
  898. * dp_link_process_phy_test_pattern_request() - process new phy link requests
  899. * @link: Display Port Driver data
  900. *
  901. * This function will handle new phy link pattern requests that are initiated
  902. * by the sink. The function will return 0 if a phy link pattern has been
  903. * processed, otherwise it will return -EINVAL.
  904. */
  905. static int dp_link_process_phy_test_pattern_request(
  906. struct dp_link_private *link)
  907. {
  908. u32 test_link_rate = 0, test_lane_count = 0;
  909. if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
  910. DP_DEBUG("no phy test\n");
  911. return -EINVAL;
  912. }
  913. test_link_rate = link->request.test_link_rate;
  914. test_lane_count = link->request.test_lane_count;
  915. if (!is_link_rate_valid(test_link_rate) ||
  916. !is_lane_count_valid(test_lane_count)) {
  917. DP_ERR("Invalid params: link rate = 0x%x, lane count = 0x%x\n",
  918. test_link_rate, test_lane_count);
  919. return -EINVAL;
  920. }
  921. DP_DEBUG("start\n");
  922. DP_INFO("Current: bw_code = 0x%x, lane count = 0x%x\n",
  923. link->dp_link.link_params.bw_code,
  924. link->dp_link.link_params.lane_count);
  925. DP_INFO("Requested: bw_code = 0x%x, lane count = 0x%x\n",
  926. test_link_rate, test_lane_count);
  927. link->dp_link.link_params.lane_count = link->request.test_lane_count;
  928. link->dp_link.link_params.bw_code = link->request.test_link_rate;
  929. dp_link_parse_vx_px(link);
  930. DP_DEBUG("end\n");
  931. return 0;
  932. }
  933. static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
  934. {
  935. return link_status[r - DP_LANE0_1_STATUS];
  936. }
  937. /**
  938. * dp_link_process_link_status_update() - processes link status updates
  939. * @link: Display Port link module data
  940. *
  941. * This function will check for changes in the link status, e.g. clock
  942. * recovery done on all lanes, and trigger link training if there is a
  943. * failure/error on the link.
  944. *
  945. * The function will return 0 if the a link status update has been processed,
  946. * otherwise it will return -EINVAL.
  947. */
  948. static int dp_link_process_link_status_update(struct dp_link_private *link)
  949. {
  950. bool channel_eq_done = drm_dp_channel_eq_ok(link->link_status,
  951. link->dp_link.link_params.lane_count);
  952. bool clock_recovery_done = drm_dp_clock_recovery_ok(link->link_status,
  953. link->dp_link.link_params.lane_count);
  954. DP_DEBUG("channel_eq_done = %d, clock_recovery_done = %d\n",
  955. channel_eq_done, clock_recovery_done);
  956. if (channel_eq_done && clock_recovery_done)
  957. return -EINVAL;
  958. return 0;
  959. }
  960. static bool dp_link_is_ds_port_status_changed(struct dp_link_private *link)
  961. {
  962. if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
  963. DP_DOWNSTREAM_PORT_STATUS_CHANGED) /* port status changed */
  964. return true;
  965. if (link->prev_sink_count != link->dp_link.sink_count.count)
  966. return true;
  967. return false;
  968. }
  969. /**
  970. * dp_link_process_downstream_port_status_change() - process port status changes
  971. * @link: Display Port Driver data
  972. *
  973. * This function will handle downstream port updates that are initiated by
  974. * the sink. If the downstream port status has changed, the EDID is read via
  975. * AUX.
  976. *
  977. * The function will return 0 if a downstream port update has been
  978. * processed, otherwise it will return -EINVAL.
  979. */
  980. static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
  981. {
  982. if (!dp_link_is_ds_port_status_changed(link))
  983. return -EINVAL;
  984. /* reset prev_sink_count */
  985. link->prev_sink_count = link->dp_link.sink_count.count;
  986. return 0;
  987. }
  988. static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
  989. {
  990. return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
  991. && !(link->request.test_requested &
  992. DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
  993. }
  994. static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
  995. {
  996. return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
  997. }
  998. /**
  999. * dp_link_process_video_pattern_request() - process new video pattern request
  1000. * @link: Display Port link module's data
  1001. *
  1002. * This function will handle a new video pattern request that are initiated by
  1003. * the sink. This is acheieved by first sending a disconnect notification to
  1004. * the sink followed by a subsequent connect notification to the user modules,
  1005. * where it is expected that the user modules would draw the required link
  1006. * pattern.
  1007. */
  1008. static int dp_link_process_video_pattern_request(struct dp_link_private *link)
  1009. {
  1010. if (!dp_link_is_video_pattern_requested(link))
  1011. goto end;
  1012. DP_DEBUG("%s: bit depth=%d(%d bpp) pattern=%s\n",
  1013. dp_link_get_test_name(DP_TEST_LINK_VIDEO_PATTERN),
  1014. link->dp_link.test_video.test_bit_depth,
  1015. dp_link_bit_depth_to_bpp(
  1016. link->dp_link.test_video.test_bit_depth),
  1017. dp_link_video_pattern_to_string(
  1018. link->dp_link.test_video.test_video_pattern));
  1019. return 0;
  1020. end:
  1021. return -EINVAL;
  1022. }
  1023. /**
  1024. * dp_link_process_audio_pattern_request() - process new audio pattern request
  1025. * @link: Display Port link module data
  1026. *
  1027. * This function will handle a new audio pattern request that is initiated by
  1028. * the sink. This is acheieved by sending the necessary secondary data packets
  1029. * to the sink. It is expected that any simulatenous requests for video
  1030. * patterns will be handled before the audio pattern is sent to the sink.
  1031. */
  1032. static int dp_link_process_audio_pattern_request(struct dp_link_private *link)
  1033. {
  1034. if (!dp_link_is_audio_pattern_requested(link))
  1035. return -EINVAL;
  1036. DP_DEBUG("sampling_rate=%s, channel_count=%d, pattern_type=%s\n",
  1037. dp_link_get_audio_sample_rate(
  1038. link->dp_link.test_audio.test_audio_sampling_rate),
  1039. link->dp_link.test_audio.test_audio_channel_count,
  1040. dp_link_get_audio_test_pattern(
  1041. link->dp_link.test_audio.test_audio_pattern_type));
  1042. DP_DEBUG("audio_period: ch1=0x%x, ch2=0x%x, ch3=0x%x, ch4=0x%x\n",
  1043. link->dp_link.test_audio.test_audio_period_ch_1,
  1044. link->dp_link.test_audio.test_audio_period_ch_2,
  1045. link->dp_link.test_audio.test_audio_period_ch_3,
  1046. link->dp_link.test_audio.test_audio_period_ch_4);
  1047. DP_DEBUG("audio_period: ch5=0x%x, ch6=0x%x, ch7=0x%x, ch8=0x%x\n",
  1048. link->dp_link.test_audio.test_audio_period_ch_5,
  1049. link->dp_link.test_audio.test_audio_period_ch_6,
  1050. link->dp_link.test_audio.test_audio_period_ch_7,
  1051. link->dp_link.test_audio.test_audio_period_ch_8);
  1052. return 0;
  1053. }
  1054. static void dp_link_reset_data(struct dp_link_private *link)
  1055. {
  1056. link->request = (const struct dp_link_request){ 0 };
  1057. link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
  1058. link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
  1059. link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
  1060. link->dp_link.phy_params.phy_test_pattern_sel = 0;
  1061. link->dp_link.sink_request = 0;
  1062. link->dp_link.test_response = 0;
  1063. }
  1064. /**
  1065. * dp_link_process_request() - handle HPD IRQ transition to HIGH
  1066. * @link: pointer to link module data
  1067. *
  1068. * This function will handle the HPD IRQ state transitions from LOW to HIGH
  1069. * (including cases when there are back to back HPD IRQ HIGH) indicating
  1070. * the start of a new link training request or sink status update.
  1071. */
  1072. static int dp_link_process_request(struct dp_link *dp_link)
  1073. {
  1074. int ret = 0;
  1075. struct dp_link_private *link;
  1076. if (!dp_link) {
  1077. DP_ERR("invalid input\n");
  1078. return -EINVAL;
  1079. }
  1080. link = container_of(dp_link, struct dp_link_private, dp_link);
  1081. dp_link_reset_data(link);
  1082. dp_link_parse_sink_status_field(link);
  1083. if (dp_link_is_test_edid_read(link)) {
  1084. dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
  1085. goto exit;
  1086. }
  1087. ret = dp_link_process_ds_port_status_change(link);
  1088. if (!ret) {
  1089. dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
  1090. goto exit;
  1091. }
  1092. ret = dp_link_process_link_training_request(link);
  1093. if (!ret) {
  1094. dp_link->sink_request |= DP_TEST_LINK_TRAINING;
  1095. goto exit;
  1096. }
  1097. ret = dp_link_process_phy_test_pattern_request(link);
  1098. if (!ret) {
  1099. dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
  1100. goto exit;
  1101. }
  1102. ret = dp_link_process_link_status_update(link);
  1103. if (!ret) {
  1104. dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
  1105. goto exit;
  1106. }
  1107. ret = dp_link_process_video_pattern_request(link);
  1108. if (!ret) {
  1109. dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
  1110. goto exit;
  1111. }
  1112. ret = dp_link_process_audio_pattern_request(link);
  1113. if (!ret) {
  1114. dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
  1115. goto exit;
  1116. }
  1117. DP_DEBUG("no test requested\n");
  1118. return ret;
  1119. exit:
  1120. /*
  1121. * log this as it can be a use initiated action to run a DP CTS
  1122. * test or in normal cases, sink has encountered a problem and
  1123. * and want source to redo some part of initialization which can
  1124. * be helpful in debugging.
  1125. */
  1126. DP_INFO("event: %s\n",
  1127. dp_link_get_test_name(dp_link->sink_request));
  1128. return 0;
  1129. }
  1130. static int dp_link_get_colorimetry_config(struct dp_link *dp_link)
  1131. {
  1132. u32 cc;
  1133. enum dynamic_range dr;
  1134. struct dp_link_private *link;
  1135. if (!dp_link) {
  1136. DP_ERR("invalid input\n");
  1137. return -EINVAL;
  1138. }
  1139. link = container_of(dp_link, struct dp_link_private, dp_link);
  1140. /* unless a video pattern CTS test is ongoing, use CEA_VESA */
  1141. if (dp_link_is_video_pattern_requested(link))
  1142. dr = link->dp_link.test_video.test_dyn_range;
  1143. else
  1144. dr = DP_DYNAMIC_RANGE_RGB_VESA;
  1145. /* Only RGB_VESA nd RGB_CEA supported for now */
  1146. switch (dr) {
  1147. case DP_DYNAMIC_RANGE_RGB_CEA:
  1148. cc = BIT(2);
  1149. break;
  1150. case DP_DYNAMIC_RANGE_RGB_VESA:
  1151. default:
  1152. cc = 0;
  1153. }
  1154. return cc;
  1155. }
  1156. static int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
  1157. {
  1158. int i;
  1159. int max = 0;
  1160. u8 data;
  1161. struct dp_link_private *link;
  1162. u8 buf[8] = {0}, offset = 0;
  1163. if (!dp_link) {
  1164. DP_ERR("invalid input\n");
  1165. return -EINVAL;
  1166. }
  1167. link = container_of(dp_link, struct dp_link_private, dp_link);
  1168. /* use the max level across lanes */
  1169. for (i = 0; i < dp_link->link_params.lane_count; i++) {
  1170. data = drm_dp_get_adjust_request_voltage(link_status, i);
  1171. data >>= DP_TRAIN_VOLTAGE_SWING_SHIFT;
  1172. offset = i * 2;
  1173. if (offset < sizeof(buf))
  1174. buf[offset] = data;
  1175. if (max < data)
  1176. max = data;
  1177. }
  1178. dp_link->phy_params.v_level = max;
  1179. /* use the max level across lanes */
  1180. max = 0;
  1181. for (i = 0; i < dp_link->link_params.lane_count; i++) {
  1182. data = drm_dp_get_adjust_request_pre_emphasis(link_status, i);
  1183. data >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
  1184. offset = (i * 2) + 1;
  1185. if (offset < sizeof(buf))
  1186. buf[offset] = data;
  1187. if (max < data)
  1188. max = data;
  1189. }
  1190. dp_link->phy_params.p_level = max;
  1191. print_hex_dump_debug("[drm-dp] Req (VxPx): ",
  1192. DUMP_PREFIX_NONE, 8, 2, buf, sizeof(buf), false);
  1193. /**
  1194. * Adjust the voltage swing and pre-emphasis level combination to within
  1195. * the allowable range.
  1196. */
  1197. if (dp_link->phy_params.v_level > DP_LINK_VOLTAGE_MAX)
  1198. dp_link->phy_params.v_level = DP_LINK_VOLTAGE_MAX;
  1199. if (dp_link->phy_params.p_level > DP_LINK_PRE_EMPHASIS_MAX)
  1200. dp_link->phy_params.p_level = DP_LINK_PRE_EMPHASIS_MAX;
  1201. if ((dp_link->phy_params.p_level > DP_LINK_PRE_EMPHASIS_LEVEL_1)
  1202. && (dp_link->phy_params.v_level == DP_LINK_VOLTAGE_LEVEL_2))
  1203. dp_link->phy_params.p_level = DP_LINK_PRE_EMPHASIS_LEVEL_1;
  1204. if ((dp_link->phy_params.p_level > DP_LINK_PRE_EMPHASIS_LEVEL_2)
  1205. && (dp_link->phy_params.v_level == DP_LINK_VOLTAGE_LEVEL_1))
  1206. dp_link->phy_params.p_level = DP_LINK_PRE_EMPHASIS_LEVEL_2;
  1207. DP_DEBUG("Set (VxPx): %x%x\n",
  1208. dp_link->phy_params.v_level, dp_link->phy_params.p_level);
  1209. return 0;
  1210. }
  1211. static int dp_link_send_psm_request(struct dp_link *dp_link, bool req)
  1212. {
  1213. struct dp_link_private *link;
  1214. if (!dp_link) {
  1215. DP_ERR("invalid input\n");
  1216. return -EINVAL;
  1217. }
  1218. link = container_of(dp_link, struct dp_link_private, dp_link);
  1219. return 0;
  1220. }
  1221. static u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
  1222. {
  1223. u32 tbd;
  1224. /*
  1225. * Few simplistic rules and assumptions made here:
  1226. * 1. Test bit depth is bit depth per color component
  1227. * 2. Assume 3 color components
  1228. */
  1229. switch (bpp) {
  1230. case 18:
  1231. tbd = DP_TEST_BIT_DEPTH_6;
  1232. break;
  1233. case 24:
  1234. tbd = DP_TEST_BIT_DEPTH_8;
  1235. break;
  1236. case 30:
  1237. tbd = DP_TEST_BIT_DEPTH_10;
  1238. break;
  1239. default:
  1240. tbd = DP_TEST_BIT_DEPTH_UNKNOWN;
  1241. break;
  1242. }
  1243. if (tbd != DP_TEST_BIT_DEPTH_UNKNOWN)
  1244. tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
  1245. return tbd;
  1246. }
  1247. struct dp_link *dp_link_get(struct device *dev, struct dp_aux *aux)
  1248. {
  1249. int rc = 0;
  1250. struct dp_link_private *link;
  1251. struct dp_link *dp_link;
  1252. if (!dev || !aux) {
  1253. DP_ERR("invalid input\n");
  1254. rc = -EINVAL;
  1255. goto error;
  1256. }
  1257. link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
  1258. if (!link) {
  1259. rc = -EINVAL;
  1260. goto error;
  1261. }
  1262. link->dev = dev;
  1263. link->aux = aux;
  1264. dp_link = &link->dp_link;
  1265. dp_link->process_request = dp_link_process_request;
  1266. dp_link->get_test_bits_depth = dp_link_get_test_bits_depth;
  1267. dp_link->get_colorimetry_config = dp_link_get_colorimetry_config;
  1268. dp_link->adjust_levels = dp_link_adjust_levels;
  1269. dp_link->send_psm_request = dp_link_send_psm_request;
  1270. dp_link->send_test_response = dp_link_send_test_response;
  1271. dp_link->psm_config = dp_link_psm_config;
  1272. dp_link->send_edid_checksum = dp_link_send_edid_checksum;
  1273. return dp_link;
  1274. error:
  1275. return ERR_PTR(rc);
  1276. }
  1277. void dp_link_put(struct dp_link *dp_link)
  1278. {
  1279. struct dp_link_private *link;
  1280. if (!dp_link)
  1281. return;
  1282. link = container_of(dp_link, struct dp_link_private, dp_link);
  1283. devm_kfree(link->dev, link);
  1284. }