dp_link.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, 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. if (!(get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
  951. DP_LINK_STATUS_UPDATED) || /* link status updated */
  952. (drm_dp_clock_recovery_ok(link->link_status,
  953. link->dp_link.link_params.lane_count) &&
  954. drm_dp_channel_eq_ok(link->link_status,
  955. link->dp_link.link_params.lane_count)))
  956. return -EINVAL;
  957. DP_DEBUG("channel_eq_done = %d, clock_recovery_done = %d\n",
  958. drm_dp_channel_eq_ok(link->link_status,
  959. link->dp_link.link_params.lane_count),
  960. drm_dp_clock_recovery_ok(link->link_status,
  961. link->dp_link.link_params.lane_count));
  962. return 0;
  963. }
  964. static bool dp_link_is_ds_port_status_changed(struct dp_link_private *link)
  965. {
  966. if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
  967. DP_DOWNSTREAM_PORT_STATUS_CHANGED) /* port status changed */
  968. return true;
  969. if (link->prev_sink_count != link->dp_link.sink_count.count)
  970. return true;
  971. return false;
  972. }
  973. /**
  974. * dp_link_process_downstream_port_status_change() - process port status changes
  975. * @link: Display Port Driver data
  976. *
  977. * This function will handle downstream port updates that are initiated by
  978. * the sink. If the downstream port status has changed, the EDID is read via
  979. * AUX.
  980. *
  981. * The function will return 0 if a downstream port update has been
  982. * processed, otherwise it will return -EINVAL.
  983. */
  984. static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
  985. {
  986. if (!dp_link_is_ds_port_status_changed(link))
  987. return -EINVAL;
  988. /* reset prev_sink_count */
  989. link->prev_sink_count = link->dp_link.sink_count.count;
  990. return 0;
  991. }
  992. static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
  993. {
  994. return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
  995. && !(link->request.test_requested &
  996. DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
  997. }
  998. static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
  999. {
  1000. return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
  1001. }
  1002. /**
  1003. * dp_link_process_video_pattern_request() - process new video pattern request
  1004. * @link: Display Port link module's data
  1005. *
  1006. * This function will handle a new video pattern request that are initiated by
  1007. * the sink. This is acheieved by first sending a disconnect notification to
  1008. * the sink followed by a subsequent connect notification to the user modules,
  1009. * where it is expected that the user modules would draw the required link
  1010. * pattern.
  1011. */
  1012. static int dp_link_process_video_pattern_request(struct dp_link_private *link)
  1013. {
  1014. if (!dp_link_is_video_pattern_requested(link))
  1015. goto end;
  1016. DP_DEBUG("%s: bit depth=%d(%d bpp) pattern=%s\n",
  1017. dp_link_get_test_name(DP_TEST_LINK_VIDEO_PATTERN),
  1018. link->dp_link.test_video.test_bit_depth,
  1019. dp_link_bit_depth_to_bpp(
  1020. link->dp_link.test_video.test_bit_depth),
  1021. dp_link_video_pattern_to_string(
  1022. link->dp_link.test_video.test_video_pattern));
  1023. return 0;
  1024. end:
  1025. return -EINVAL;
  1026. }
  1027. /**
  1028. * dp_link_process_audio_pattern_request() - process new audio pattern request
  1029. * @link: Display Port link module data
  1030. *
  1031. * This function will handle a new audio pattern request that is initiated by
  1032. * the sink. This is acheieved by sending the necessary secondary data packets
  1033. * to the sink. It is expected that any simulatenous requests for video
  1034. * patterns will be handled before the audio pattern is sent to the sink.
  1035. */
  1036. static int dp_link_process_audio_pattern_request(struct dp_link_private *link)
  1037. {
  1038. if (!dp_link_is_audio_pattern_requested(link))
  1039. return -EINVAL;
  1040. DP_DEBUG("sampling_rate=%s, channel_count=%d, pattern_type=%s\n",
  1041. dp_link_get_audio_sample_rate(
  1042. link->dp_link.test_audio.test_audio_sampling_rate),
  1043. link->dp_link.test_audio.test_audio_channel_count,
  1044. dp_link_get_audio_test_pattern(
  1045. link->dp_link.test_audio.test_audio_pattern_type));
  1046. DP_DEBUG("audio_period: ch1=0x%x, ch2=0x%x, ch3=0x%x, ch4=0x%x\n",
  1047. link->dp_link.test_audio.test_audio_period_ch_1,
  1048. link->dp_link.test_audio.test_audio_period_ch_2,
  1049. link->dp_link.test_audio.test_audio_period_ch_3,
  1050. link->dp_link.test_audio.test_audio_period_ch_4);
  1051. DP_DEBUG("audio_period: ch5=0x%x, ch6=0x%x, ch7=0x%x, ch8=0x%x\n",
  1052. link->dp_link.test_audio.test_audio_period_ch_5,
  1053. link->dp_link.test_audio.test_audio_period_ch_6,
  1054. link->dp_link.test_audio.test_audio_period_ch_7,
  1055. link->dp_link.test_audio.test_audio_period_ch_8);
  1056. return 0;
  1057. }
  1058. static void dp_link_reset_data(struct dp_link_private *link)
  1059. {
  1060. link->request = (const struct dp_link_request){ 0 };
  1061. link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
  1062. link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
  1063. link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
  1064. link->dp_link.phy_params.phy_test_pattern_sel = 0;
  1065. link->dp_link.sink_request = 0;
  1066. link->dp_link.test_response = 0;
  1067. }
  1068. /**
  1069. * dp_link_process_request() - handle HPD IRQ transition to HIGH
  1070. * @link: pointer to link module data
  1071. *
  1072. * This function will handle the HPD IRQ state transitions from LOW to HIGH
  1073. * (including cases when there are back to back HPD IRQ HIGH) indicating
  1074. * the start of a new link training request or sink status update.
  1075. */
  1076. static int dp_link_process_request(struct dp_link *dp_link)
  1077. {
  1078. int ret = 0;
  1079. struct dp_link_private *link;
  1080. if (!dp_link) {
  1081. DP_ERR("invalid input\n");
  1082. return -EINVAL;
  1083. }
  1084. link = container_of(dp_link, struct dp_link_private, dp_link);
  1085. dp_link_reset_data(link);
  1086. dp_link_parse_sink_status_field(link);
  1087. if (dp_link_is_test_edid_read(link)) {
  1088. dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
  1089. goto exit;
  1090. }
  1091. ret = dp_link_process_ds_port_status_change(link);
  1092. if (!ret) {
  1093. dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
  1094. goto exit;
  1095. }
  1096. ret = dp_link_process_link_training_request(link);
  1097. if (!ret) {
  1098. dp_link->sink_request |= DP_TEST_LINK_TRAINING;
  1099. goto exit;
  1100. }
  1101. ret = dp_link_process_phy_test_pattern_request(link);
  1102. if (!ret) {
  1103. dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
  1104. goto exit;
  1105. }
  1106. ret = dp_link_process_link_status_update(link);
  1107. if (!ret) {
  1108. dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
  1109. goto exit;
  1110. }
  1111. ret = dp_link_process_video_pattern_request(link);
  1112. if (!ret) {
  1113. dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
  1114. goto exit;
  1115. }
  1116. ret = dp_link_process_audio_pattern_request(link);
  1117. if (!ret) {
  1118. dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
  1119. goto exit;
  1120. }
  1121. DP_DEBUG("no test requested\n");
  1122. return ret;
  1123. exit:
  1124. /*
  1125. * log this as it can be a use initiated action to run a DP CTS
  1126. * test or in normal cases, sink has encountered a problem and
  1127. * and want source to redo some part of initialization which can
  1128. * be helpful in debugging.
  1129. */
  1130. DP_INFO("test requested: %s\n",
  1131. dp_link_get_test_name(dp_link->sink_request));
  1132. return 0;
  1133. }
  1134. static int dp_link_get_colorimetry_config(struct dp_link *dp_link)
  1135. {
  1136. u32 cc;
  1137. enum dynamic_range dr;
  1138. struct dp_link_private *link;
  1139. if (!dp_link) {
  1140. DP_ERR("invalid input\n");
  1141. return -EINVAL;
  1142. }
  1143. link = container_of(dp_link, struct dp_link_private, dp_link);
  1144. /* unless a video pattern CTS test is ongoing, use CEA_VESA */
  1145. if (dp_link_is_video_pattern_requested(link))
  1146. dr = link->dp_link.test_video.test_dyn_range;
  1147. else
  1148. dr = DP_DYNAMIC_RANGE_RGB_VESA;
  1149. /* Only RGB_VESA nd RGB_CEA supported for now */
  1150. switch (dr) {
  1151. case DP_DYNAMIC_RANGE_RGB_CEA:
  1152. cc = BIT(2);
  1153. break;
  1154. case DP_DYNAMIC_RANGE_RGB_VESA:
  1155. default:
  1156. cc = 0;
  1157. }
  1158. return cc;
  1159. }
  1160. static int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
  1161. {
  1162. int i;
  1163. int max = 0;
  1164. u8 data;
  1165. struct dp_link_private *link;
  1166. u8 buf[8] = {0}, offset = 0;
  1167. if (!dp_link) {
  1168. DP_ERR("invalid input\n");
  1169. return -EINVAL;
  1170. }
  1171. link = container_of(dp_link, struct dp_link_private, dp_link);
  1172. /* use the max level across lanes */
  1173. for (i = 0; i < dp_link->link_params.lane_count; i++) {
  1174. data = drm_dp_get_adjust_request_voltage(link_status, i);
  1175. data >>= DP_TRAIN_VOLTAGE_SWING_SHIFT;
  1176. offset = i * 2;
  1177. if (offset < sizeof(buf))
  1178. buf[offset] = data;
  1179. if (max < data)
  1180. max = data;
  1181. }
  1182. dp_link->phy_params.v_level = max;
  1183. /* use the max level across lanes */
  1184. max = 0;
  1185. for (i = 0; i < dp_link->link_params.lane_count; i++) {
  1186. data = drm_dp_get_adjust_request_pre_emphasis(link_status, i);
  1187. data >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
  1188. offset = (i * 2) + 1;
  1189. if (offset < sizeof(buf))
  1190. buf[offset] = data;
  1191. if (max < data)
  1192. max = data;
  1193. }
  1194. dp_link->phy_params.p_level = max;
  1195. print_hex_dump(KERN_DEBUG, "[drm-dp] Req (VxPx): ",
  1196. DUMP_PREFIX_NONE, 8, 2, buf, sizeof(buf), false);
  1197. /**
  1198. * Adjust the voltage swing and pre-emphasis level combination to within
  1199. * the allowable range.
  1200. */
  1201. if (dp_link->phy_params.v_level > DP_LINK_VOLTAGE_MAX)
  1202. dp_link->phy_params.v_level = DP_LINK_VOLTAGE_MAX;
  1203. if (dp_link->phy_params.p_level > DP_LINK_PRE_EMPHASIS_MAX)
  1204. dp_link->phy_params.p_level = DP_LINK_PRE_EMPHASIS_MAX;
  1205. if ((dp_link->phy_params.p_level > DP_LINK_PRE_EMPHASIS_LEVEL_1)
  1206. && (dp_link->phy_params.v_level == DP_LINK_VOLTAGE_LEVEL_2))
  1207. dp_link->phy_params.p_level = DP_LINK_PRE_EMPHASIS_LEVEL_1;
  1208. if ((dp_link->phy_params.p_level > DP_LINK_PRE_EMPHASIS_LEVEL_2)
  1209. && (dp_link->phy_params.v_level == DP_LINK_VOLTAGE_LEVEL_1))
  1210. dp_link->phy_params.p_level = DP_LINK_PRE_EMPHASIS_LEVEL_2;
  1211. DP_DEBUG("Set (VxPx): %x%x\n",
  1212. dp_link->phy_params.v_level, dp_link->phy_params.p_level);
  1213. return 0;
  1214. }
  1215. static int dp_link_send_psm_request(struct dp_link *dp_link, bool req)
  1216. {
  1217. struct dp_link_private *link;
  1218. if (!dp_link) {
  1219. DP_ERR("invalid input\n");
  1220. return -EINVAL;
  1221. }
  1222. link = container_of(dp_link, struct dp_link_private, dp_link);
  1223. return 0;
  1224. }
  1225. static u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
  1226. {
  1227. u32 tbd;
  1228. /*
  1229. * Few simplistic rules and assumptions made here:
  1230. * 1. Test bit depth is bit depth per color component
  1231. * 2. Assume 3 color components
  1232. */
  1233. switch (bpp) {
  1234. case 18:
  1235. tbd = DP_TEST_BIT_DEPTH_6;
  1236. break;
  1237. case 24:
  1238. tbd = DP_TEST_BIT_DEPTH_8;
  1239. break;
  1240. case 30:
  1241. tbd = DP_TEST_BIT_DEPTH_10;
  1242. break;
  1243. default:
  1244. tbd = DP_TEST_BIT_DEPTH_UNKNOWN;
  1245. break;
  1246. }
  1247. if (tbd != DP_TEST_BIT_DEPTH_UNKNOWN)
  1248. tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
  1249. return tbd;
  1250. }
  1251. struct dp_link *dp_link_get(struct device *dev, struct dp_aux *aux)
  1252. {
  1253. int rc = 0;
  1254. struct dp_link_private *link;
  1255. struct dp_link *dp_link;
  1256. if (!dev || !aux) {
  1257. DP_ERR("invalid input\n");
  1258. rc = -EINVAL;
  1259. goto error;
  1260. }
  1261. link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
  1262. if (!link) {
  1263. rc = -EINVAL;
  1264. goto error;
  1265. }
  1266. link->dev = dev;
  1267. link->aux = aux;
  1268. dp_link = &link->dp_link;
  1269. dp_link->process_request = dp_link_process_request;
  1270. dp_link->get_test_bits_depth = dp_link_get_test_bits_depth;
  1271. dp_link->get_colorimetry_config = dp_link_get_colorimetry_config;
  1272. dp_link->adjust_levels = dp_link_adjust_levels;
  1273. dp_link->send_psm_request = dp_link_send_psm_request;
  1274. dp_link->send_test_response = dp_link_send_test_response;
  1275. dp_link->psm_config = dp_link_psm_config;
  1276. dp_link->send_edid_checksum = dp_link_send_edid_checksum;
  1277. return dp_link;
  1278. error:
  1279. return ERR_PTR(rc);
  1280. }
  1281. void dp_link_put(struct dp_link *dp_link)
  1282. {
  1283. struct dp_link_private *link;
  1284. if (!dp_link)
  1285. return;
  1286. link = container_of(dp_link, struct dp_link_private, dp_link);
  1287. devm_kfree(link->dev, link);
  1288. }