dp_link.c 44 KB

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