1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
6 #define pr_fmt(fmt) "[drm-dp] %s: " fmt, __func__
8 #include <drm/drm_print.h>
13 #define DP_TEST_REQUEST_MASK 0x7F
15 enum 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,
25 enum audio_pattern_type {
26 AUDIO_TEST_PATTERN_OPERATOR_DEFINED = 0x00,
27 AUDIO_TEST_PATTERN_SAWTOOTH = 0x01,
30 struct dp_link_request {
36 struct dp_link_private {
39 struct drm_dp_aux *aux;
40 struct dp_link dp_link;
42 struct dp_link_request request;
43 struct mutex psm_mutex;
44 u8 link_status[DP_LINK_STATUS_SIZE];
47 static int dp_aux_link_power_up(struct drm_dp_aux *aux,
48 struct dp_link_info *link)
53 if (link->revision < 0x11)
56 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
60 value &= ~DP_SET_POWER_MASK;
61 value |= DP_SET_POWER_D0;
63 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
67 usleep_range(1000, 2000);
72 static int dp_aux_link_power_down(struct drm_dp_aux *aux,
73 struct dp_link_info *link)
78 if (link->revision < 0x11)
81 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
85 value &= ~DP_SET_POWER_MASK;
86 value |= DP_SET_POWER_D3;
88 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
95 static int dp_link_get_period(struct dp_link_private *link, int const addr)
99 u32 const max_audio_period = 0xA;
101 /* TEST_AUDIO_PERIOD_CH_XX */
102 if (drm_dp_dpcd_readb(link->aux, addr, &data) < 0) {
103 DRM_ERROR("failed to read test_audio_period (0x%x)\n", addr);
108 /* Period - Bits 3:0 */
110 if ((int)data > max_audio_period) {
111 DRM_ERROR("invalid test_audio_period_ch_1 = 0x%x\n", data);
121 static int dp_link_parse_audio_channel_period(struct dp_link_private *link)
124 struct dp_link_test_audio *req = &link->dp_link.test_audio;
126 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
130 req->test_audio_period_ch_1 = ret;
131 DRM_DEBUG_DP("test_audio_period_ch_1 = 0x%x\n", ret);
133 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
137 req->test_audio_period_ch_2 = ret;
138 DRM_DEBUG_DP("test_audio_period_ch_2 = 0x%x\n", ret);
140 /* TEST_AUDIO_PERIOD_CH_3 (Byte 0x275) */
141 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
145 req->test_audio_period_ch_3 = ret;
146 DRM_DEBUG_DP("test_audio_period_ch_3 = 0x%x\n", ret);
148 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
152 req->test_audio_period_ch_4 = ret;
153 DRM_DEBUG_DP("test_audio_period_ch_4 = 0x%x\n", ret);
155 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
159 req->test_audio_period_ch_5 = ret;
160 DRM_DEBUG_DP("test_audio_period_ch_5 = 0x%x\n", ret);
162 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
166 req->test_audio_period_ch_6 = ret;
167 DRM_DEBUG_DP("test_audio_period_ch_6 = 0x%x\n", ret);
169 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
173 req->test_audio_period_ch_7 = ret;
174 DRM_DEBUG_DP("test_audio_period_ch_7 = 0x%x\n", ret);
176 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
180 req->test_audio_period_ch_8 = ret;
181 DRM_DEBUG_DP("test_audio_period_ch_8 = 0x%x\n", ret);
186 static int dp_link_parse_audio_pattern_type(struct dp_link_private *link)
191 int const max_audio_pattern_type = 0x1;
193 rlen = drm_dp_dpcd_readb(link->aux,
194 DP_TEST_AUDIO_PATTERN_TYPE, &data);
196 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
200 /* Audio Pattern Type - Bits 7:0 */
201 if ((int)data > max_audio_pattern_type) {
202 DRM_ERROR("invalid audio pattern type = 0x%x\n", data);
207 link->dp_link.test_audio.test_audio_pattern_type = data;
208 DRM_DEBUG_DP("audio pattern type = 0x%x\n", data);
213 static int dp_link_parse_audio_mode(struct dp_link_private *link)
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;
223 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_AUDIO_MODE, &data);
225 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
229 /* Sampling Rate - Bits 3:0 */
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);
238 /* Channel Count - Bits 7:4 */
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);
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);
255 static int dp_link_parse_audio_pattern_params(struct dp_link_private *link)
259 ret = dp_link_parse_audio_mode(link);
263 ret = dp_link_parse_audio_pattern_type(link);
267 ret = dp_link_parse_audio_channel_period(link);
273 static bool dp_link_is_video_pattern_valid(u32 pattern)
276 case DP_NO_TEST_PATTERN:
278 case DP_BLACK_AND_WHITE_VERTICAL_LINES:
279 case DP_COLOR_SQUARE:
287 * dp_link_is_bit_depth_valid() - validates the bit depth requested
288 * @tbd: bit depth requested by the sink
290 * Returns true if the requested bit depth is supported.
292 static bool dp_link_is_bit_depth_valid(u32 tbd)
294 /* DP_TEST_VIDEO_PATTERN_NONE is treated as invalid */
296 case DP_TEST_BIT_DEPTH_6:
297 case DP_TEST_BIT_DEPTH_8:
298 case DP_TEST_BIT_DEPTH_10:
305 static int dp_link_parse_timing_params1(struct dp_link_private *link,
306 int addr, int len, u32 *val)
314 /* Read the requested video link pattern (Byte 0x221). */
315 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
317 DRM_ERROR("failed to read 0x%x\n", addr);
321 *val = bp[1] | (bp[0] << 8);
326 static int dp_link_parse_timing_params2(struct dp_link_private *link,
328 u32 *val1, u32 *val2)
336 /* Read the requested video link pattern (Byte 0x221). */
337 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
339 DRM_ERROR("failed to read 0x%x\n", addr);
343 *val1 = (bp[0] & BIT(7)) >> 7;
344 *val2 = bp[1] | ((bp[0] & 0x7F) << 8);
349 static int dp_link_parse_timing_params3(struct dp_link_private *link,
356 rlen = drm_dp_dpcd_read(link->aux, addr, &bp, len);
358 DRM_ERROR("failed to read 0x%x\n", addr);
367 * dp_parse_video_pattern_params() - parses video pattern parameters from DPCD
368 * @link: Display Port Driver data
370 * Returns 0 if it successfully parses the video link pattern and the link
371 * bit depth requested by the sink and, and if the values parsed are valid.
373 static int dp_link_parse_video_pattern_params(struct dp_link_private *link)
379 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_PATTERN, &bp);
381 DRM_ERROR("failed to read link video pattern. rlen=%zd\n",
386 if (!dp_link_is_video_pattern_valid(bp)) {
387 DRM_ERROR("invalid link video pattern = 0x%x\n", bp);
392 link->dp_link.test_video.test_video_pattern = bp;
394 /* Read the requested color bit depth and dynamic range (Byte 0x232) */
395 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_MISC0, &bp);
397 DRM_ERROR("failed to read link bit depth. rlen=%zd\n", rlen);
402 link->dp_link.test_video.test_dyn_range =
403 (bp & DP_TEST_DYNAMIC_RANGE_CEA);
405 /* Color bit depth */
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);
413 link->dp_link.test_video.test_bit_depth = bp;
415 /* resolution timing params */
416 ret = dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
417 &link->dp_link.test_video.test_h_total);
419 DRM_ERROR("failed to parse test_htotal(DP_TEST_H_TOTAL_HI)\n");
423 ret = dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
424 &link->dp_link.test_video.test_v_total);
426 DRM_ERROR("failed to parse test_v_total(DP_TEST_V_TOTAL_HI)\n");
430 ret = dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
431 &link->dp_link.test_video.test_h_start);
433 DRM_ERROR("failed to parse test_h_start(DP_TEST_H_START_HI)\n");
437 ret = dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
438 &link->dp_link.test_video.test_v_start);
440 DRM_ERROR("failed to parse test_v_start(DP_TEST_V_START_HI)\n");
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);
448 DRM_ERROR("failed to parse (DP_TEST_HSYNC_HI)\n");
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);
456 DRM_ERROR("failed to parse (DP_TEST_VSYNC_HI)\n");
460 ret = dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
461 &link->dp_link.test_video.test_h_width);
463 DRM_ERROR("failed to parse test_h_width(DP_TEST_H_WIDTH_HI)\n");
467 ret = dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
468 &link->dp_link.test_video.test_v_height);
470 DRM_ERROR("failed to parse test_v_height\n");
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;
478 DRM_ERROR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
482 ret = dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
483 &link->dp_link.test_video.test_rr_n);
485 DRM_ERROR("failed to parse test_rr_n\n");
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);
522 * dp_link_parse_link_training_params() - parses link training parameters from
524 * @link: Display Port Driver data
526 * Returns 0 if it successfully parses the link rate (Byte 0x219) and lane
527 * count (Byte 0x220), and if these values parse are valid.
529 static int dp_link_parse_link_training_params(struct dp_link_private *link)
534 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LINK_RATE, &bp);
536 DRM_ERROR("failed to read link rate. rlen=%zd\n", rlen);
540 if (!is_link_rate_valid(bp)) {
541 DRM_ERROR("invalid link rate = 0x%x\n", bp);
545 link->request.test_link_rate = bp;
546 DRM_DEBUG_DP("link rate = 0x%x\n", link->request.test_link_rate);
548 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LANE_COUNT, &bp);
550 DRM_ERROR("failed to read lane count. rlen=%zd\n", rlen);
553 bp &= DP_MAX_LANE_COUNT_MASK;
555 if (!is_lane_count_valid(bp)) {
556 DRM_ERROR("invalid lane count = 0x%x\n", bp);
560 link->request.test_lane_count = bp;
561 DRM_DEBUG_DP("lane count = 0x%x\n", link->request.test_lane_count);
566 * dp_parse_phy_test_params() - parses the phy link parameters
567 * @link: Display Port Driver data
569 * Parses the DPCD (Byte 0x248) for the DP PHY link pattern that is being
572 static int dp_link_parse_phy_test_params(struct dp_link_private *link)
577 rlen = drm_dp_dpcd_readb(link->aux, DP_PHY_TEST_PATTERN,
580 DRM_ERROR("failed to read phy link pattern. rlen=%zd\n", rlen);
584 link->dp_link.phy_params.phy_test_pattern_sel = data & 0x07;
586 DRM_DEBUG_DP("phy_test_pattern_sel = 0x%x\n", 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:
603 * dp_link_is_video_audio_test_requested() - checks for audio/video link request
604 * @link: link requested by the sink
606 * Returns true if the requested link is a permitted audio/video link.
608 static bool dp_link_is_video_audio_test_requested(u32 link)
610 u8 video_audio_test = (DP_TEST_LINK_VIDEO_PATTERN |
611 DP_TEST_LINK_AUDIO_PATTERN |
612 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
614 return ((link & video_audio_test) &&
615 !(link & ~video_audio_test));
619 * dp_link_parse_request() - parses link request parameters from sink
620 * @link: Display Port Driver data
622 * Parses the DPCD to check if an automated link is requested (Byte 0x201),
623 * and what type of link automation is being requested (Byte 0x218).
625 static int dp_link_parse_request(struct dp_link_private *link)
632 * Read the device service IRQ vector (Byte 0x201) to determine
633 * whether an automated link has been requested by the sink.
635 rlen = drm_dp_dpcd_readb(link->aux,
636 DP_DEVICE_SERVICE_IRQ_VECTOR, &data);
638 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
642 DRM_DEBUG_DP("device service irq vector = 0x%x\n", data);
644 if (!(data & DP_AUTOMATED_TEST_REQUEST)) {
645 DRM_DEBUG_DP("no test requested\n");
650 * Read the link request byte (Byte 0x218) to determine what type
651 * of automated link has been requested by the sink.
653 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_REQUEST, &data);
655 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
659 if (!data || (data == DP_TEST_LINK_FAUX_PATTERN)) {
660 DRM_DEBUG_DP("link 0x%x not supported\n", data);
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);
670 ret = dp_link_parse_link_training_params(link);
675 if (link->request.test_requested == DP_TEST_LINK_TRAINING) {
676 ret = dp_link_parse_link_training_params(link);
681 if (dp_link_is_video_audio_test_requested(
682 link->request.test_requested)) {
683 ret = dp_link_parse_video_pattern_params(link);
687 ret = dp_link_parse_audio_pattern_params(link);
691 * Send a DP_TEST_ACK if all link parameters are valid, otherwise send
695 link->dp_link.test_response = DP_TEST_NAK;
697 if (link->request.test_requested != DP_TEST_LINK_EDID_READ)
698 link->dp_link.test_response = DP_TEST_ACK;
700 link->dp_link.test_response =
701 DP_TEST_EDID_CHECKSUM_WRITE;
708 * dp_link_parse_sink_count() - parses the sink count
709 * @dp_link: pointer to link module data
711 * Parses the DPCD to check if there is an update to the sink count
712 * (Byte 0x200), and whether all the sink devices connected have Content
713 * Protection enabled.
715 static int dp_link_parse_sink_count(struct dp_link *dp_link)
720 struct dp_link_private *link = container_of(dp_link,
721 struct dp_link_private, dp_link);
723 rlen = drm_dp_dpcd_readb(link->aux, DP_SINK_COUNT,
724 &link->dp_link.sink_count);
726 DRM_ERROR("sink count read failed. rlen=%zd\n", rlen);
730 cp_ready = link->dp_link.sink_count & DP_SINK_CP_READY;
732 link->dp_link.sink_count =
733 DP_GET_SINK_COUNT(link->dp_link.sink_count);
735 DRM_DEBUG_DP("sink_count = 0x%x, cp_ready = 0x%x\n",
736 link->dp_link.sink_count, cp_ready);
740 static void dp_link_parse_sink_status_field(struct dp_link_private *link)
744 link->prev_sink_count = link->dp_link.sink_count;
745 dp_link_parse_sink_count(&link->dp_link);
747 len = drm_dp_dpcd_read_link_status(link->aux,
749 if (len < DP_LINK_STATUS_SIZE)
750 DRM_ERROR("DP link status read failed\n");
751 dp_link_parse_request(link);
755 * dp_link_process_link_training_request() - processes new training requests
756 * @link: Display Port link data
758 * This function will handle new link training requests that are initiated by
759 * the sink. In particular, it will update the requested lane count and link
760 * rate, and then trigger the link retraining procedure.
762 * The function will return 0 if a link training request has been processed,
763 * otherwise it will return -EINVAL.
765 static int dp_link_process_link_training_request(struct dp_link_private *link)
767 if (link->request.test_requested != DP_TEST_LINK_TRAINING)
770 DRM_DEBUG_DP("Test:0x%x link rate = 0x%x, lane count = 0x%x\n",
771 DP_TEST_LINK_TRAINING,
772 link->request.test_link_rate,
773 link->request.test_lane_count);
775 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
776 link->dp_link.link_params.rate = link->request.test_link_rate;
781 bool dp_link_send_test_response(struct dp_link *dp_link)
783 struct dp_link_private *link = NULL;
787 DRM_ERROR("invalid input\n");
791 link = container_of(dp_link, struct dp_link_private, dp_link);
793 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_RESPONSE,
794 dp_link->test_response);
799 int dp_link_psm_config(struct dp_link *dp_link,
800 struct dp_link_info *link_info, bool enable)
802 struct dp_link_private *link = NULL;
806 DRM_ERROR("invalid params\n");
810 link = container_of(dp_link, struct dp_link_private, dp_link);
812 mutex_lock(&link->psm_mutex);
814 ret = dp_aux_link_power_down(link->aux, link_info);
816 ret = dp_aux_link_power_up(link->aux, link_info);
819 DRM_ERROR("Failed to %s low power mode\n", enable ?
822 dp_link->psm_enabled = enable;
824 mutex_unlock(&link->psm_mutex);
828 bool dp_link_send_edid_checksum(struct dp_link *dp_link, u8 checksum)
830 struct dp_link_private *link = NULL;
834 DRM_ERROR("invalid input\n");
838 link = container_of(dp_link, struct dp_link_private, dp_link);
840 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_EDID_CHECKSUM,
845 static int dp_link_parse_vx_px(struct dp_link_private *link)
849 DRM_DEBUG_DP("vx: 0=%d, 1=%d, 2=%d, 3=%d\n",
850 drm_dp_get_adjust_request_voltage(link->link_status, 0),
851 drm_dp_get_adjust_request_voltage(link->link_status, 1),
852 drm_dp_get_adjust_request_voltage(link->link_status, 2),
853 drm_dp_get_adjust_request_voltage(link->link_status, 3));
855 DRM_DEBUG_DP("px: 0=%d, 1=%d, 2=%d, 3=%d\n",
856 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0),
857 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 1),
858 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 2),
859 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 3));
862 * Update the voltage and pre-emphasis levels as per DPCD request
865 DRM_DEBUG_DP("Current: v_level = 0x%x, p_level = 0x%x\n",
866 link->dp_link.phy_params.v_level,
867 link->dp_link.phy_params.p_level);
868 link->dp_link.phy_params.v_level =
869 drm_dp_get_adjust_request_voltage(link->link_status, 0);
870 link->dp_link.phy_params.p_level =
871 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0);
872 DRM_DEBUG_DP("Requested: v_level = 0x%x, p_level = 0x%x\n",
873 link->dp_link.phy_params.v_level,
874 link->dp_link.phy_params.p_level);
880 * dp_link_process_phy_test_pattern_request() - process new phy link requests
881 * @link: Display Port Driver data
883 * This function will handle new phy link pattern requests that are initiated
884 * by the sink. The function will return 0 if a phy link pattern has been
885 * processed, otherwise it will return -EINVAL.
887 static int dp_link_process_phy_test_pattern_request(
888 struct dp_link_private *link)
892 if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
893 DRM_DEBUG_DP("no phy test\n");
897 if (!is_link_rate_valid(link->request.test_link_rate) ||
898 !is_lane_count_valid(link->request.test_lane_count)) {
899 DRM_ERROR("Invalid: link rate = 0x%x,lane count = 0x%x\n",
900 link->request.test_link_rate,
901 link->request.test_lane_count);
905 DRM_DEBUG_DP("Current: rate = 0x%x, lane count = 0x%x\n",
906 link->dp_link.link_params.rate,
907 link->dp_link.link_params.num_lanes);
909 DRM_DEBUG_DP("Requested: rate = 0x%x, lane count = 0x%x\n",
910 link->request.test_link_rate,
911 link->request.test_lane_count);
913 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
914 link->dp_link.link_params.rate = link->request.test_link_rate;
916 ret = dp_link_parse_vx_px(link);
919 DRM_ERROR("parse_vx_px failed. ret=%d\n", ret);
924 static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
926 return link_status[r - DP_LANE0_1_STATUS];
930 * dp_link_process_link_status_update() - processes link status updates
931 * @link: Display Port link module data
933 * This function will check for changes in the link status, e.g. clock
934 * recovery done on all lanes, and trigger link training if there is a
935 * failure/error on the link.
937 * The function will return 0 if the a link status update has been processed,
938 * otherwise it will return -EINVAL.
940 static int dp_link_process_link_status_update(struct dp_link_private *link)
942 if (!(get_link_status(link->link_status,
943 DP_LANE_ALIGN_STATUS_UPDATED) &
944 DP_LINK_STATUS_UPDATED) ||
945 (drm_dp_clock_recovery_ok(link->link_status,
946 link->dp_link.link_params.num_lanes) &&
947 drm_dp_channel_eq_ok(link->link_status,
948 link->dp_link.link_params.num_lanes)))
951 DRM_DEBUG_DP("channel_eq_done = %d, clock_recovery_done = %d\n",
952 drm_dp_clock_recovery_ok(link->link_status,
953 link->dp_link.link_params.num_lanes),
954 drm_dp_clock_recovery_ok(link->link_status,
955 link->dp_link.link_params.num_lanes));
961 * dp_link_process_downstream_port_status_change() - process port status changes
962 * @link: Display Port Driver data
964 * This function will handle downstream port updates that are initiated by
965 * the sink. If the downstream port status has changed, the EDID is read via
968 * The function will return 0 if a downstream port update has been
969 * processed, otherwise it will return -EINVAL.
971 static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
973 if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
974 DP_DOWNSTREAM_PORT_STATUS_CHANGED)
977 if (link->prev_sink_count == link->dp_link.sink_count)
981 /* reset prev_sink_count */
982 link->prev_sink_count = link->dp_link.sink_count;
987 static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
989 return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
990 && !(link->request.test_requested &
991 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
994 static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
996 return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
999 static void dp_link_reset_data(struct dp_link_private *link)
1001 link->request = (const struct dp_link_request){ 0 };
1002 link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
1003 link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
1004 link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
1005 link->dp_link.phy_params.phy_test_pattern_sel = 0;
1006 link->dp_link.sink_request = 0;
1007 link->dp_link.test_response = 0;
1011 * dp_link_process_request() - handle HPD IRQ transition to HIGH
1012 * @dp_link: pointer to link module data
1014 * This function will handle the HPD IRQ state transitions from LOW to HIGH
1015 * (including cases when there are back to back HPD IRQ HIGH) indicating
1016 * the start of a new link training request or sink status update.
1018 int dp_link_process_request(struct dp_link *dp_link)
1021 struct dp_link_private *link;
1024 DRM_ERROR("invalid input\n");
1028 link = container_of(dp_link, struct dp_link_private, dp_link);
1030 dp_link_reset_data(link);
1032 dp_link_parse_sink_status_field(link);
1034 if (link->request.test_requested == DP_TEST_LINK_EDID_READ) {
1035 dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
1039 ret = dp_link_process_ds_port_status_change(link);
1041 dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
1045 ret = dp_link_process_link_training_request(link);
1047 dp_link->sink_request |= DP_TEST_LINK_TRAINING;
1051 ret = dp_link_process_phy_test_pattern_request(link);
1053 dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
1057 ret = dp_link_process_link_status_update(link);
1059 dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
1063 if (dp_link_is_video_pattern_requested(link)) {
1065 dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
1068 if (dp_link_is_audio_pattern_requested(link)) {
1069 dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
1076 int dp_link_get_colorimetry_config(struct dp_link *dp_link)
1079 struct dp_link_private *link;
1082 DRM_ERROR("invalid input\n");
1086 link = container_of(dp_link, struct dp_link_private, dp_link);
1089 * Unless a video pattern CTS test is ongoing, use RGB_VESA
1090 * Only RGB_VESA and RGB_CEA supported for now
1092 if (dp_link_is_video_pattern_requested(link))
1093 cc = link->dp_link.test_video.test_dyn_range;
1095 cc = DP_TEST_DYNAMIC_RANGE_VESA;
1100 int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
1103 int v_max = 0, p_max = 0;
1106 DRM_ERROR("invalid input\n");
1110 /* use the max level across lanes */
1111 for (i = 0; i < dp_link->link_params.num_lanes; i++) {
1112 u8 data_v = drm_dp_get_adjust_request_voltage(link_status, i);
1113 u8 data_p = drm_dp_get_adjust_request_pre_emphasis(link_status,
1115 DRM_DEBUG_DP("lane=%d req_vol_swing=%d req_pre_emphasis=%d\n",
1123 dp_link->phy_params.v_level = v_max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
1124 dp_link->phy_params.p_level = p_max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
1127 * Adjust the voltage swing and pre-emphasis level combination to within
1128 * the allowable range.
1130 if (dp_link->phy_params.v_level > DP_TRAIN_VOLTAGE_SWING_MAX) {
1131 DRM_DEBUG_DP("Requested vSwingLevel=%d, change to %d\n",
1132 dp_link->phy_params.v_level,
1133 DP_TRAIN_VOLTAGE_SWING_MAX);
1134 dp_link->phy_params.v_level = DP_TRAIN_VOLTAGE_SWING_MAX;
1137 if (dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_MAX) {
1138 DRM_DEBUG_DP("Requested preEmphasisLevel=%d, change to %d\n",
1139 dp_link->phy_params.p_level,
1140 DP_TRAIN_PRE_EMPHASIS_MAX);
1141 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_MAX;
1144 if ((dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_LVL_1)
1145 && (dp_link->phy_params.v_level ==
1146 DP_TRAIN_VOLTAGE_SWING_LVL_2)) {
1147 DRM_DEBUG_DP("Requested preEmphasisLevel=%d, change to %d\n",
1148 dp_link->phy_params.p_level,
1149 DP_TRAIN_PRE_EMPHASIS_LVL_1);
1150 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_LVL_1;
1153 DRM_DEBUG_DP("adjusted: v_level=%d, p_level=%d\n",
1154 dp_link->phy_params.v_level, dp_link->phy_params.p_level);
1159 u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
1164 * Few simplistic rules and assumptions made here:
1165 * 1. Test bit depth is bit depth per color component
1166 * 2. Assume 3 color components
1170 tbd = DP_TEST_BIT_DEPTH_6;
1173 tbd = DP_TEST_BIT_DEPTH_8;
1176 tbd = DP_TEST_BIT_DEPTH_10;
1179 tbd = DP_TEST_BIT_DEPTH_UNKNOWN;
1183 if (tbd != DP_TEST_BIT_DEPTH_UNKNOWN)
1184 tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
1189 struct dp_link *dp_link_get(struct device *dev, struct drm_dp_aux *aux)
1191 struct dp_link_private *link;
1192 struct dp_link *dp_link;
1195 DRM_ERROR("invalid input\n");
1196 return ERR_PTR(-EINVAL);
1199 link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
1201 return ERR_PTR(-ENOMEM);
1206 mutex_init(&link->psm_mutex);
1207 dp_link = &link->dp_link;