1
2
3
4
5
6#define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__
7
8#include <drm/drm_print.h>
9
10#include "dp_link.h"
11#include "dp_panel.h"
12
13#define DP_TEST_REQUEST_MASK 0x7F
14
15enum audio_sample_rate {
16 AUDIO_SAMPLE_RATE_32_KHZ = 0x00,
17 AUDIO_SAMPLE_RATE_44_1_KHZ = 0x01,
18 AUDIO_SAMPLE_RATE_48_KHZ = 0x02,
19 AUDIO_SAMPLE_RATE_88_2_KHZ = 0x03,
20 AUDIO_SAMPLE_RATE_96_KHZ = 0x04,
21 AUDIO_SAMPLE_RATE_176_4_KHZ = 0x05,
22 AUDIO_SAMPLE_RATE_192_KHZ = 0x06,
23};
24
25enum audio_pattern_type {
26 AUDIO_TEST_PATTERN_OPERATOR_DEFINED = 0x00,
27 AUDIO_TEST_PATTERN_SAWTOOTH = 0x01,
28};
29
30struct dp_link_request {
31 u32 test_requested;
32 u32 test_link_rate;
33 u32 test_lane_count;
34};
35
36struct dp_link_private {
37 u32 prev_sink_count;
38 struct device *dev;
39 struct drm_dp_aux *aux;
40 struct dp_link dp_link;
41
42 struct dp_link_request request;
43 struct mutex psm_mutex;
44 u8 link_status[DP_LINK_STATUS_SIZE];
45};
46
47static int dp_aux_link_power_up(struct drm_dp_aux *aux,
48 struct dp_link_info *link)
49{
50 u8 value;
51 int err;
52
53 if (link->revision < 0x11)
54 return 0;
55
56 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
57 if (err < 0)
58 return err;
59
60 value &= ~DP_SET_POWER_MASK;
61 value |= DP_SET_POWER_D0;
62
63 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
64 if (err < 0)
65 return err;
66
67 usleep_range(1000, 2000);
68
69 return 0;
70}
71
72static int dp_aux_link_power_down(struct drm_dp_aux *aux,
73 struct dp_link_info *link)
74{
75 u8 value;
76 int err;
77
78 if (link->revision < 0x11)
79 return 0;
80
81 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
82 if (err < 0)
83 return err;
84
85 value &= ~DP_SET_POWER_MASK;
86 value |= DP_SET_POWER_D3;
87
88 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
89 if (err < 0)
90 return err;
91
92 return 0;
93}
94
95static int dp_link_get_period(struct dp_link_private *link, int const addr)
96{
97 int ret = 0;
98 u8 data;
99 u32 const max_audio_period = 0xA;
100
101
102 if (drm_dp_dpcd_readb(link->aux, addr, &data) < 0) {
103 DRM_ERROR("failed to read test_audio_period (0x%x)\n", addr);
104 ret = -EINVAL;
105 goto exit;
106 }
107
108
109 data = data & 0xF;
110 if ((int)data > max_audio_period) {
111 DRM_ERROR("invalid test_audio_period_ch_1 = 0x%x\n", data);
112 ret = -EINVAL;
113 goto exit;
114 }
115
116 ret = data;
117exit:
118 return ret;
119}
120
121static int dp_link_parse_audio_channel_period(struct dp_link_private *link)
122{
123 int ret = 0;
124 struct dp_link_test_audio *req = &link->dp_link.test_audio;
125
126 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
127 if (ret == -EINVAL)
128 goto exit;
129
130 req->test_audio_period_ch_1 = ret;
131 DRM_DEBUG_DP("test_audio_period_ch_1 = 0x%x\n", ret);
132
133 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
134 if (ret == -EINVAL)
135 goto exit;
136
137 req->test_audio_period_ch_2 = ret;
138 DRM_DEBUG_DP("test_audio_period_ch_2 = 0x%x\n", ret);
139
140
141 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
142 if (ret == -EINVAL)
143 goto exit;
144
145 req->test_audio_period_ch_3 = ret;
146 DRM_DEBUG_DP("test_audio_period_ch_3 = 0x%x\n", ret);
147
148 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
149 if (ret == -EINVAL)
150 goto exit;
151
152 req->test_audio_period_ch_4 = ret;
153 DRM_DEBUG_DP("test_audio_period_ch_4 = 0x%x\n", ret);
154
155 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
156 if (ret == -EINVAL)
157 goto exit;
158
159 req->test_audio_period_ch_5 = ret;
160 DRM_DEBUG_DP("test_audio_period_ch_5 = 0x%x\n", ret);
161
162 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
163 if (ret == -EINVAL)
164 goto exit;
165
166 req->test_audio_period_ch_6 = ret;
167 DRM_DEBUG_DP("test_audio_period_ch_6 = 0x%x\n", ret);
168
169 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
170 if (ret == -EINVAL)
171 goto exit;
172
173 req->test_audio_period_ch_7 = ret;
174 DRM_DEBUG_DP("test_audio_period_ch_7 = 0x%x\n", ret);
175
176 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
177 if (ret == -EINVAL)
178 goto exit;
179
180 req->test_audio_period_ch_8 = ret;
181 DRM_DEBUG_DP("test_audio_period_ch_8 = 0x%x\n", ret);
182exit:
183 return ret;
184}
185
186static int dp_link_parse_audio_pattern_type(struct dp_link_private *link)
187{
188 int ret = 0;
189 u8 data;
190 ssize_t rlen;
191 int const max_audio_pattern_type = 0x1;
192
193 rlen = drm_dp_dpcd_readb(link->aux,
194 DP_TEST_AUDIO_PATTERN_TYPE, &data);
195 if (rlen < 0) {
196 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
197 return rlen;
198 }
199
200
201 if ((int)data > max_audio_pattern_type) {
202 DRM_ERROR("invalid audio pattern type = 0x%x\n", data);
203 ret = -EINVAL;
204 goto exit;
205 }
206
207 link->dp_link.test_audio.test_audio_pattern_type = data;
208 DRM_DEBUG_DP("audio pattern type = 0x%x\n", data);
209exit:
210 return ret;
211}
212
213static int dp_link_parse_audio_mode(struct dp_link_private *link)
214{
215 int ret = 0;
216 u8 data;
217 ssize_t rlen;
218 int const max_audio_sampling_rate = 0x6;
219 int const max_audio_channel_count = 0x8;
220 int sampling_rate = 0x0;
221 int channel_count = 0x0;
222
223 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_AUDIO_MODE, &data);
224 if (rlen < 0) {
225 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
226 return rlen;
227 }
228
229
230 sampling_rate = data & 0xF;
231 if (sampling_rate > max_audio_sampling_rate) {
232 DRM_ERROR("sampling rate (0x%x) greater than max (0x%x)\n",
233 sampling_rate, max_audio_sampling_rate);
234 ret = -EINVAL;
235 goto exit;
236 }
237
238
239 channel_count = ((data & 0xF0) >> 4) + 1;
240 if (channel_count > max_audio_channel_count) {
241 DRM_ERROR("channel_count (0x%x) greater than max (0x%x)\n",
242 channel_count, max_audio_channel_count);
243 ret = -EINVAL;
244 goto exit;
245 }
246
247 link->dp_link.test_audio.test_audio_sampling_rate = sampling_rate;
248 link->dp_link.test_audio.test_audio_channel_count = channel_count;
249 DRM_DEBUG_DP("sampling_rate = 0x%x, channel_count = 0x%x\n",
250 sampling_rate, channel_count);
251exit:
252 return ret;
253}
254
255static int dp_link_parse_audio_pattern_params(struct dp_link_private *link)
256{
257 int ret = 0;
258
259 ret = dp_link_parse_audio_mode(link);
260 if (ret)
261 goto exit;
262
263 ret = dp_link_parse_audio_pattern_type(link);
264 if (ret)
265 goto exit;
266
267 ret = dp_link_parse_audio_channel_period(link);
268
269exit:
270 return ret;
271}
272
273static bool dp_link_is_video_pattern_valid(u32 pattern)
274{
275 switch (pattern) {
276 case DP_NO_TEST_PATTERN:
277 case DP_COLOR_RAMP:
278 case DP_BLACK_AND_WHITE_VERTICAL_LINES:
279 case DP_COLOR_SQUARE:
280 return true;
281 default:
282 return false;
283 }
284}
285
286
287
288
289
290
291
292static bool dp_link_is_bit_depth_valid(u32 tbd)
293{
294
295 switch (tbd) {
296 case DP_TEST_BIT_DEPTH_6:
297 case DP_TEST_BIT_DEPTH_8:
298 case DP_TEST_BIT_DEPTH_10:
299 return true;
300 default:
301 return false;
302 }
303}
304
305static int dp_link_parse_timing_params1(struct dp_link_private *link,
306 int addr, int len, u32 *val)
307{
308 u8 bp[2];
309 int rlen;
310
311 if (len != 2)
312 return -EINVAL;
313
314
315 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
316 if (rlen < len) {
317 DRM_ERROR("failed to read 0x%x\n", addr);
318 return -EINVAL;
319 }
320
321 *val = bp[1] | (bp[0] << 8);
322
323 return 0;
324}
325
326static int dp_link_parse_timing_params2(struct dp_link_private *link,
327 int addr, int len,
328 u32 *val1, u32 *val2)
329{
330 u8 bp[2];
331 int rlen;
332
333 if (len != 2)
334 return -EINVAL;
335
336
337 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
338 if (rlen < len) {
339 DRM_ERROR("failed to read 0x%x\n", addr);
340 return -EINVAL;
341 }
342
343 *val1 = (bp[0] & BIT(7)) >> 7;
344 *val2 = bp[1] | ((bp[0] & 0x7F) << 8);
345
346 return 0;
347}
348
349static int dp_link_parse_timing_params3(struct dp_link_private *link,
350 int addr, u32 *val)
351{
352 u8 bp;
353 u32 len = 1;
354 int rlen;
355
356 rlen = drm_dp_dpcd_read(link->aux, addr, &bp, len);
357 if (rlen < 1) {
358 DRM_ERROR("failed to read 0x%x\n", addr);
359 return -EINVAL;
360 }
361 *val = bp;
362
363 return 0;
364}
365
366
367
368
369
370
371
372
373static int dp_link_parse_video_pattern_params(struct dp_link_private *link)
374{
375 int ret = 0;
376 ssize_t rlen;
377 u8 bp;
378
379 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_PATTERN, &bp);
380 if (rlen < 0) {
381 DRM_ERROR("failed to read link video pattern. rlen=%zd\n",
382 rlen);
383 return rlen;
384 }
385
386 if (!dp_link_is_video_pattern_valid(bp)) {
387 DRM_ERROR("invalid link video pattern = 0x%x\n", bp);
388 ret = -EINVAL;
389 return ret;
390 }
391
392 link->dp_link.test_video.test_video_pattern = bp;
393
394
395 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_MISC0, &bp);
396 if (rlen < 0) {
397 DRM_ERROR("failed to read link bit depth. rlen=%zd\n", rlen);
398 return rlen;
399 }
400
401
402 link->dp_link.test_video.test_dyn_range =
403 (bp & DP_TEST_DYNAMIC_RANGE_CEA);
404
405
406 bp &= DP_TEST_BIT_DEPTH_MASK;
407 if (!dp_link_is_bit_depth_valid(bp)) {
408 DRM_ERROR("invalid link bit depth = 0x%x\n", bp);
409 ret = -EINVAL;
410 return ret;
411 }
412
413 link->dp_link.test_video.test_bit_depth = bp;
414
415
416 ret = dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
417 &link->dp_link.test_video.test_h_total);
418 if (ret) {
419 DRM_ERROR("failed to parse test_htotal(DP_TEST_H_TOTAL_HI)\n");
420 return ret;
421 }
422
423 ret = dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
424 &link->dp_link.test_video.test_v_total);
425 if (ret) {
426 DRM_ERROR("failed to parse test_v_total(DP_TEST_V_TOTAL_HI)\n");
427 return ret;
428 }
429
430 ret = dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
431 &link->dp_link.test_video.test_h_start);
432 if (ret) {
433 DRM_ERROR("failed to parse test_h_start(DP_TEST_H_START_HI)\n");
434 return ret;
435 }
436
437 ret = dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
438 &link->dp_link.test_video.test_v_start);
439 if (ret) {
440 DRM_ERROR("failed to parse test_v_start(DP_TEST_V_START_HI)\n");
441 return ret;
442 }
443
444 ret = dp_link_parse_timing_params2(link, DP_TEST_HSYNC_HI, 2,
445 &link->dp_link.test_video.test_hsync_pol,
446 &link->dp_link.test_video.test_hsync_width);
447 if (ret) {
448 DRM_ERROR("failed to parse (DP_TEST_HSYNC_HI)\n");
449 return ret;
450 }
451
452 ret = dp_link_parse_timing_params2(link, DP_TEST_VSYNC_HI, 2,
453 &link->dp_link.test_video.test_vsync_pol,
454 &link->dp_link.test_video.test_vsync_width);
455 if (ret) {
456 DRM_ERROR("failed to parse (DP_TEST_VSYNC_HI)\n");
457 return ret;
458 }
459
460 ret = dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
461 &link->dp_link.test_video.test_h_width);
462 if (ret) {
463 DRM_ERROR("failed to parse test_h_width(DP_TEST_H_WIDTH_HI)\n");
464 return ret;
465 }
466
467 ret = dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
468 &link->dp_link.test_video.test_v_height);
469 if (ret) {
470 DRM_ERROR("failed to parse test_v_height\n");
471 return ret;
472 }
473
474 ret = dp_link_parse_timing_params3(link, DP_TEST_MISC1,
475 &link->dp_link.test_video.test_rr_d);
476 link->dp_link.test_video.test_rr_d &= DP_TEST_REFRESH_DENOMINATOR;
477 if (ret) {
478 DRM_ERROR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
479 return ret;
480 }
481
482 ret = dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
483 &link->dp_link.test_video.test_rr_n);
484 if (ret) {
485 DRM_ERROR("failed to parse test_rr_n\n");
486 return ret;
487 }
488
489 DRM_DEBUG_DP("link video pattern = 0x%x\n"
490 "link dynamic range = 0x%x\n"
491 "link bit depth = 0x%x\n"
492 "TEST_H_TOTAL = %d, TEST_V_TOTAL = %d\n"
493 "TEST_H_START = %d, TEST_V_START = %d\n"
494 "TEST_HSYNC_POL = %d\n"
495 "TEST_HSYNC_WIDTH = %d\n"
496 "TEST_VSYNC_POL = %d\n"
497 "TEST_VSYNC_WIDTH = %d\n"
498 "TEST_H_WIDTH = %d\n"
499 "TEST_V_HEIGHT = %d\n"
500 "TEST_REFRESH_DENOMINATOR = %d\n"
501 "TEST_REFRESH_NUMERATOR = %d\n",
502 link->dp_link.test_video.test_video_pattern,
503 link->dp_link.test_video.test_dyn_range,
504 link->dp_link.test_video.test_bit_depth,
505 link->dp_link.test_video.test_h_total,
506 link->dp_link.test_video.test_v_total,
507 link->dp_link.test_video.test_h_start,
508 link->dp_link.test_video.test_v_start,
509 link->dp_link.test_video.test_hsync_pol,
510 link->dp_link.test_video.test_hsync_width,
511 link->dp_link.test_video.test_vsync_pol,
512 link->dp_link.test_video.test_vsync_width,
513 link->dp_link.test_video.test_h_width,
514 link->dp_link.test_video.test_v_height,
515 link->dp_link.test_video.test_rr_d,
516 link->dp_link.test_video.test_rr_n);
517
518 return ret;
519}
520
521
522
523
524
525
526
527
528
529static int dp_link_parse_link_training_params(struct dp_link_private *link)
530{
531 u8 bp;
532 ssize_t rlen;
533
534 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LINK_RATE, &bp);
535 if (rlen < 0) {
536 DRM_ERROR("failed to read link rate. rlen=%zd\n", rlen);
537 return rlen;
538 }
539
540 if (!is_link_rate_valid(bp)) {
541 DRM_ERROR("invalid link rate = 0x%x\n", bp);
542 return -EINVAL;
543 }
544
545 link->request.test_link_rate = bp;
546 DRM_DEBUG_DP("link rate = 0x%x\n", link->request.test_link_rate);
547
548 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LANE_COUNT, &bp);
549 if (rlen < 0) {
550 DRM_ERROR("failed to read lane count. rlen=%zd\n", rlen);
551 return rlen;
552 }
553 bp &= DP_MAX_LANE_COUNT_MASK;
554
555 if (!is_lane_count_valid(bp)) {
556 DRM_ERROR("invalid lane count = 0x%x\n", bp);
557 return -EINVAL;
558 }
559
560 link->request.test_lane_count = bp;
561 DRM_DEBUG_DP("lane count = 0x%x\n", link->request.test_lane_count);
562 return 0;
563}
564
565
566
567
568
569
570
571
572static int dp_link_parse_phy_test_params(struct dp_link_private *link)
573{
574 u8 data;
575 ssize_t rlen;
576
577 rlen = drm_dp_dpcd_readb(link->aux, DP_PHY_TEST_PATTERN,
578 &data);
579 if (rlen < 0) {
580 DRM_ERROR("failed to read phy link pattern. rlen=%zd\n", rlen);
581 return rlen;
582 }
583
584 link->dp_link.phy_params.phy_test_pattern_sel = data & 0x07;
585
586 DRM_DEBUG_DP("phy_test_pattern_sel = 0x%x\n", data);
587
588 switch (data) {
589 case DP_PHY_TEST_PATTERN_SEL_MASK:
590 case DP_PHY_TEST_PATTERN_NONE:
591 case DP_PHY_TEST_PATTERN_D10_2:
592 case DP_PHY_TEST_PATTERN_ERROR_COUNT:
593 case DP_PHY_TEST_PATTERN_PRBS7:
594 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
595 case DP_PHY_TEST_PATTERN_CP2520:
596 return 0;
597 default:
598 return -EINVAL;
599 }
600}
601
602
603
604
605
606
607
608static bool dp_link_is_video_audio_test_requested(u32 link)
609{
610 u8 video_audio_test = (DP_TEST_LINK_VIDEO_PATTERN |
611 DP_TEST_LINK_AUDIO_PATTERN |
612 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
613
614 return ((link & video_audio_test) &&
615 !(link & ~video_audio_test));
616}
617
618
619
620
621
622
623
624
625static int dp_link_parse_request(struct dp_link_private *link)
626{
627 int ret = 0;
628 u8 data;
629 ssize_t rlen;
630
631
632
633
634
635 rlen = drm_dp_dpcd_readb(link->aux,
636 DP_DEVICE_SERVICE_IRQ_VECTOR, &data);
637 if (rlen < 0) {
638 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
639 return rlen;
640 }
641
642 DRM_DEBUG_DP("device service irq vector = 0x%x\n", data);
643
644 if (!(data & DP_AUTOMATED_TEST_REQUEST)) {
645 DRM_DEBUG_DP("no test requested\n");
646 return 0;
647 }
648
649
650
651
652
653 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_REQUEST, &data);
654 if (rlen < 0) {
655 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
656 return rlen;
657 }
658
659 if (!data || (data == DP_TEST_LINK_FAUX_PATTERN)) {
660 DRM_DEBUG_DP("link 0x%x not supported\n", data);
661 goto end;
662 }
663
664 DRM_DEBUG_DP("Test:(0x%x) requested\n", data);
665 link->request.test_requested = data;
666 if (link->request.test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) {
667 ret = dp_link_parse_phy_test_params(link);
668 if (ret)
669 goto end;
670 ret = dp_link_parse_link_training_params(link);
671 if (ret)
672 goto end;
673 }
674
675 if (link->request.test_requested == DP_TEST_LINK_TRAINING) {
676 ret = dp_link_parse_link_training_params(link);
677 if (ret)
678 goto end;
679 }
680
681 if (dp_link_is_video_audio_test_requested(
682 link->request.test_requested)) {
683 ret = dp_link_parse_video_pattern_params(link);
684 if (ret)
685 goto end;
686
687 ret = dp_link_parse_audio_pattern_params(link);
688 }
689end:
690
691
692
693
694 if (ret) {
695 link->dp_link.test_response = DP_TEST_NAK;
696 } else {
697 if (link->request.test_requested != DP_TEST_LINK_EDID_READ)
698 link->dp_link.test_response = DP_TEST_ACK;
699 else
700 link->dp_link.test_response =
701 DP_TEST_EDID_CHECKSUM_WRITE;
702 }
703
704 return ret;
705}
706
707
708
709
710
711
712
713
714
715static int dp_link_parse_sink_count(struct dp_link *dp_link)
716{
717 ssize_t rlen;
718 bool cp_ready;
719
720 struct dp_link_private *link = container_of(dp_link,
721 struct dp_link_private, dp_link);
722
723 rlen = drm_dp_dpcd_readb(link->aux, DP_SINK_COUNT,
724 &link->dp_link.sink_count);
725 if (rlen < 0) {
726 DRM_ERROR("sink count read failed. rlen=%zd\n", rlen);
727 return rlen;
728 }
729
730 cp_ready = link->dp_link.sink_count & DP_SINK_CP_READY;
731
732 link->dp_link.sink_count =
733 DP_GET_SINK_COUNT(link->dp_link.sink_count);
734
735 DRM_DEBUG_DP("sink_count = 0x%x, cp_ready = 0x%x\n",
736 link->dp_link.sink_count, cp_ready);
737 return 0;
738}
739
740static int dp_link_parse_sink_status_field(struct dp_link_private *link)
741{
742 int len = 0;
743
744 link->prev_sink_count = link->dp_link.sink_count;
745 len = dp_link_parse_sink_count(&link->dp_link);
746 if (len < 0) {
747 DRM_ERROR("DP parse sink count failed\n");
748 return len;
749 }
750
751 len = drm_dp_dpcd_read_link_status(link->aux,
752 link->link_status);
753 if (len < DP_LINK_STATUS_SIZE) {
754 DRM_ERROR("DP link status read failed\n");
755 return len;
756 }
757
758 return dp_link_parse_request(link);
759}
760
761
762
763
764
765
766
767
768
769
770
771
772static int dp_link_process_link_training_request(struct dp_link_private *link)
773{
774 if (link->request.test_requested != DP_TEST_LINK_TRAINING)
775 return -EINVAL;
776
777 DRM_DEBUG_DP("Test:0x%x link rate = 0x%x, lane count = 0x%x\n",
778 DP_TEST_LINK_TRAINING,
779 link->request.test_link_rate,
780 link->request.test_lane_count);
781
782 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
783 link->dp_link.link_params.rate =
784 drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
785
786 return 0;
787}
788
789bool dp_link_send_test_response(struct dp_link *dp_link)
790{
791 struct dp_link_private *link = NULL;
792 int ret = 0;
793
794 if (!dp_link) {
795 DRM_ERROR("invalid input\n");
796 return false;
797 }
798
799 link = container_of(dp_link, struct dp_link_private, dp_link);
800
801 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_RESPONSE,
802 dp_link->test_response);
803
804 return ret == 1;
805}
806
807int dp_link_psm_config(struct dp_link *dp_link,
808 struct dp_link_info *link_info, bool enable)
809{
810 struct dp_link_private *link = NULL;
811 int ret = 0;
812
813 if (!dp_link) {
814 DRM_ERROR("invalid params\n");
815 return -EINVAL;
816 }
817
818 link = container_of(dp_link, struct dp_link_private, dp_link);
819
820 mutex_lock(&link->psm_mutex);
821 if (enable)
822 ret = dp_aux_link_power_down(link->aux, link_info);
823 else
824 ret = dp_aux_link_power_up(link->aux, link_info);
825
826 if (ret)
827 DRM_ERROR("Failed to %s low power mode\n", enable ?
828 "enter" : "exit");
829 else
830 dp_link->psm_enabled = enable;
831
832 mutex_unlock(&link->psm_mutex);
833 return ret;
834}
835
836bool dp_link_send_edid_checksum(struct dp_link *dp_link, u8 checksum)
837{
838 struct dp_link_private *link = NULL;
839 int ret = 0;
840
841 if (!dp_link) {
842 DRM_ERROR("invalid input\n");
843 return false;
844 }
845
846 link = container_of(dp_link, struct dp_link_private, dp_link);
847
848 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_EDID_CHECKSUM,
849 checksum);
850 return ret == 1;
851}
852
853static void dp_link_parse_vx_px(struct dp_link_private *link)
854{
855 DRM_DEBUG_DP("vx: 0=%d, 1=%d, 2=%d, 3=%d\n",
856 drm_dp_get_adjust_request_voltage(link->link_status, 0),
857 drm_dp_get_adjust_request_voltage(link->link_status, 1),
858 drm_dp_get_adjust_request_voltage(link->link_status, 2),
859 drm_dp_get_adjust_request_voltage(link->link_status, 3));
860
861 DRM_DEBUG_DP("px: 0=%d, 1=%d, 2=%d, 3=%d\n",
862 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0),
863 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 1),
864 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 2),
865 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 3));
866
867
868
869
870
871 DRM_DEBUG_DP("Current: v_level = 0x%x, p_level = 0x%x\n",
872 link->dp_link.phy_params.v_level,
873 link->dp_link.phy_params.p_level);
874 link->dp_link.phy_params.v_level =
875 drm_dp_get_adjust_request_voltage(link->link_status, 0);
876 link->dp_link.phy_params.p_level =
877 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0);
878
879 link->dp_link.phy_params.p_level >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
880
881 DRM_DEBUG_DP("Requested: v_level = 0x%x, p_level = 0x%x\n",
882 link->dp_link.phy_params.v_level,
883 link->dp_link.phy_params.p_level);
884}
885
886
887
888
889
890
891
892
893
894static int dp_link_process_phy_test_pattern_request(
895 struct dp_link_private *link)
896{
897 if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
898 DRM_DEBUG_DP("no phy test\n");
899 return -EINVAL;
900 }
901
902 if (!is_link_rate_valid(link->request.test_link_rate) ||
903 !is_lane_count_valid(link->request.test_lane_count)) {
904 DRM_ERROR("Invalid: link rate = 0x%x,lane count = 0x%x\n",
905 link->request.test_link_rate,
906 link->request.test_lane_count);
907 return -EINVAL;
908 }
909
910 DRM_DEBUG_DP("Current: rate = 0x%x, lane count = 0x%x\n",
911 link->dp_link.link_params.rate,
912 link->dp_link.link_params.num_lanes);
913
914 DRM_DEBUG_DP("Requested: rate = 0x%x, lane count = 0x%x\n",
915 link->request.test_link_rate,
916 link->request.test_lane_count);
917
918 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
919 link->dp_link.link_params.rate =
920 drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
921
922 dp_link_parse_vx_px(link);
923
924 return 0;
925}
926
927static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
928{
929 return link_status[r - DP_LANE0_1_STATUS];
930}
931
932
933
934
935
936
937
938
939
940
941
942
943static int dp_link_process_link_status_update(struct dp_link_private *link)
944{
945 bool channel_eq_done = drm_dp_channel_eq_ok(link->link_status,
946 link->dp_link.link_params.num_lanes);
947
948 bool clock_recovery_done = drm_dp_clock_recovery_ok(link->link_status,
949 link->dp_link.link_params.num_lanes);
950
951 DRM_DEBUG_DP("channel_eq_done = %d, clock_recovery_done = %d\n",
952 channel_eq_done, clock_recovery_done);
953
954 if (channel_eq_done && clock_recovery_done)
955 return -EINVAL;
956
957
958 return 0;
959}
960
961
962
963
964
965
966
967
968
969
970
971
972static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
973{
974 if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
975 DP_DOWNSTREAM_PORT_STATUS_CHANGED)
976 goto reset;
977
978 if (link->prev_sink_count == link->dp_link.sink_count)
979 return -EINVAL;
980
981reset:
982
983 link->prev_sink_count = link->dp_link.sink_count;
984
985 return 0;
986}
987
988static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
989{
990 return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
991 && !(link->request.test_requested &
992 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
993}
994
995static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
996{
997 return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
998}
999
1000static void dp_link_reset_data(struct dp_link_private *link)
1001{
1002 link->request = (const struct dp_link_request){ 0 };
1003 link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
1004 link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
1005 link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
1006 link->dp_link.phy_params.phy_test_pattern_sel = 0;
1007 link->dp_link.sink_request = 0;
1008 link->dp_link.test_response = 0;
1009}
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019int dp_link_process_request(struct dp_link *dp_link)
1020{
1021 int ret = 0;
1022 struct dp_link_private *link;
1023
1024 if (!dp_link) {
1025 DRM_ERROR("invalid input\n");
1026 return -EINVAL;
1027 }
1028
1029 link = container_of(dp_link, struct dp_link_private, dp_link);
1030
1031 dp_link_reset_data(link);
1032
1033 ret = dp_link_parse_sink_status_field(link);
1034 if (ret)
1035 return ret;
1036
1037 if (link->request.test_requested == DP_TEST_LINK_EDID_READ) {
1038 dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
1039 } else if (!dp_link_process_ds_port_status_change(link)) {
1040 dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
1041 } else if (!dp_link_process_link_training_request(link)) {
1042 dp_link->sink_request |= DP_TEST_LINK_TRAINING;
1043 } else if (!dp_link_process_phy_test_pattern_request(link)) {
1044 dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
1045 } else {
1046 ret = dp_link_process_link_status_update(link);
1047 if (!ret) {
1048 dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
1049 } else {
1050 if (dp_link_is_video_pattern_requested(link)) {
1051 ret = 0;
1052 dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
1053 }
1054 if (dp_link_is_audio_pattern_requested(link)) {
1055 dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
1056 ret = -EINVAL;
1057 }
1058 }
1059 }
1060
1061 DRM_DEBUG_DP("sink request=%#x", dp_link->sink_request);
1062 return ret;
1063}
1064
1065int dp_link_get_colorimetry_config(struct dp_link *dp_link)
1066{
1067 u32 cc;
1068 struct dp_link_private *link;
1069
1070 if (!dp_link) {
1071 DRM_ERROR("invalid input\n");
1072 return -EINVAL;
1073 }
1074
1075 link = container_of(dp_link, struct dp_link_private, dp_link);
1076
1077
1078
1079
1080
1081 if (dp_link_is_video_pattern_requested(link))
1082 cc = link->dp_link.test_video.test_dyn_range;
1083 else
1084 cc = DP_TEST_DYNAMIC_RANGE_VESA;
1085
1086 return cc;
1087}
1088
1089int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
1090{
1091 int i;
1092 int v_max = 0, p_max = 0;
1093
1094 if (!dp_link) {
1095 DRM_ERROR("invalid input\n");
1096 return -EINVAL;
1097 }
1098
1099
1100 for (i = 0; i < dp_link->link_params.num_lanes; i++) {
1101 u8 data_v = drm_dp_get_adjust_request_voltage(link_status, i);
1102 u8 data_p = drm_dp_get_adjust_request_pre_emphasis(link_status,
1103 i);
1104 DRM_DEBUG_DP("lane=%d req_vol_swing=%d req_pre_emphasis=%d\n",
1105 i, data_v, data_p);
1106 if (v_max < data_v)
1107 v_max = data_v;
1108 if (p_max < data_p)
1109 p_max = data_p;
1110 }
1111
1112 dp_link->phy_params.v_level = v_max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
1113 dp_link->phy_params.p_level = p_max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
1114
1115
1116
1117
1118
1119 if (dp_link->phy_params.v_level > DP_TRAIN_VOLTAGE_SWING_MAX) {
1120 DRM_DEBUG_DP("Requested vSwingLevel=%d, change to %d\n",
1121 dp_link->phy_params.v_level,
1122 DP_TRAIN_VOLTAGE_SWING_MAX);
1123 dp_link->phy_params.v_level = DP_TRAIN_VOLTAGE_SWING_MAX;
1124 }
1125
1126 if (dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_MAX) {
1127 DRM_DEBUG_DP("Requested preEmphasisLevel=%d, change to %d\n",
1128 dp_link->phy_params.p_level,
1129 DP_TRAIN_PRE_EMPHASIS_MAX);
1130 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_MAX;
1131 }
1132
1133 if ((dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_LVL_1)
1134 && (dp_link->phy_params.v_level ==
1135 DP_TRAIN_VOLTAGE_SWING_LVL_2)) {
1136 DRM_DEBUG_DP("Requested preEmphasisLevel=%d, change to %d\n",
1137 dp_link->phy_params.p_level,
1138 DP_TRAIN_PRE_EMPHASIS_LVL_1);
1139 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_LVL_1;
1140 }
1141
1142 DRM_DEBUG_DP("adjusted: v_level=%d, p_level=%d\n",
1143 dp_link->phy_params.v_level, dp_link->phy_params.p_level);
1144
1145 return 0;
1146}
1147
1148void dp_link_reset_phy_params_vx_px(struct dp_link *dp_link)
1149{
1150 dp_link->phy_params.v_level = 0;
1151 dp_link->phy_params.p_level = 0;
1152}
1153
1154u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
1155{
1156 u32 tbd;
1157
1158
1159
1160
1161
1162
1163 switch (bpp) {
1164 case 18:
1165 tbd = DP_TEST_BIT_DEPTH_6;
1166 break;
1167 case 24:
1168 tbd = DP_TEST_BIT_DEPTH_8;
1169 break;
1170 case 30:
1171 tbd = DP_TEST_BIT_DEPTH_10;
1172 break;
1173 default:
1174 tbd = DP_TEST_BIT_DEPTH_UNKNOWN;
1175 break;
1176 }
1177
1178 if (tbd != DP_TEST_BIT_DEPTH_UNKNOWN)
1179 tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
1180
1181 return tbd;
1182}
1183
1184struct dp_link *dp_link_get(struct device *dev, struct drm_dp_aux *aux)
1185{
1186 struct dp_link_private *link;
1187 struct dp_link *dp_link;
1188
1189 if (!dev || !aux) {
1190 DRM_ERROR("invalid input\n");
1191 return ERR_PTR(-EINVAL);
1192 }
1193
1194 link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
1195 if (!link)
1196 return ERR_PTR(-ENOMEM);
1197
1198 link->dev = dev;
1199 link->aux = aux;
1200
1201 mutex_init(&link->psm_mutex);
1202 dp_link = &link->dp_link;
1203
1204 return dp_link;
1205}
1206