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>
14 #define DP_TEST_REQUEST_MASK 0x7F
16 enum audio_sample_rate {
17 AUDIO_SAMPLE_RATE_32_KHZ = 0x00,
18 AUDIO_SAMPLE_RATE_44_1_KHZ = 0x01,
19 AUDIO_SAMPLE_RATE_48_KHZ = 0x02,
20 AUDIO_SAMPLE_RATE_88_2_KHZ = 0x03,
21 AUDIO_SAMPLE_RATE_96_KHZ = 0x04,
22 AUDIO_SAMPLE_RATE_176_4_KHZ = 0x05,
23 AUDIO_SAMPLE_RATE_192_KHZ = 0x06,
26 enum audio_pattern_type {
27 AUDIO_TEST_PATTERN_OPERATOR_DEFINED = 0x00,
28 AUDIO_TEST_PATTERN_SAWTOOTH = 0x01,
31 struct dp_link_request {
37 struct dp_link_private {
40 struct drm_device *drm_dev;
41 struct drm_dp_aux *aux;
42 struct dp_link dp_link;
44 struct dp_link_request request;
45 struct mutex psm_mutex;
46 u8 link_status[DP_LINK_STATUS_SIZE];
49 static int dp_aux_link_power_up(struct drm_dp_aux *aux,
50 struct dp_link_info *link)
56 if (link->revision < 0x11)
59 len = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
63 value &= ~DP_SET_POWER_MASK;
64 value |= DP_SET_POWER_D0;
66 /* retry for 1ms to give the sink time to wake up */
67 for (i = 0; i < 3; i++) {
68 len = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
69 usleep_range(1000, 2000);
77 static int dp_aux_link_power_down(struct drm_dp_aux *aux,
78 struct dp_link_info *link)
83 if (link->revision < 0x11)
86 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
90 value &= ~DP_SET_POWER_MASK;
91 value |= DP_SET_POWER_D3;
93 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
100 static int dp_link_get_period(struct dp_link_private *link, int const addr)
104 u32 const max_audio_period = 0xA;
106 /* TEST_AUDIO_PERIOD_CH_XX */
107 if (drm_dp_dpcd_readb(link->aux, addr, &data) < 0) {
108 DRM_ERROR("failed to read test_audio_period (0x%x)\n", addr);
113 /* Period - Bits 3:0 */
115 if ((int)data > max_audio_period) {
116 DRM_ERROR("invalid test_audio_period_ch_1 = 0x%x\n", data);
126 static int dp_link_parse_audio_channel_period(struct dp_link_private *link)
129 struct dp_link_test_audio *req = &link->dp_link.test_audio;
131 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
135 req->test_audio_period_ch_1 = ret;
136 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_1 = 0x%x\n", ret);
138 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
142 req->test_audio_period_ch_2 = ret;
143 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_2 = 0x%x\n", ret);
145 /* TEST_AUDIO_PERIOD_CH_3 (Byte 0x275) */
146 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
150 req->test_audio_period_ch_3 = ret;
151 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_3 = 0x%x\n", ret);
153 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
157 req->test_audio_period_ch_4 = ret;
158 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_4 = 0x%x\n", ret);
160 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
164 req->test_audio_period_ch_5 = ret;
165 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_5 = 0x%x\n", ret);
167 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
171 req->test_audio_period_ch_6 = ret;
172 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_6 = 0x%x\n", ret);
174 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
178 req->test_audio_period_ch_7 = ret;
179 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_7 = 0x%x\n", ret);
181 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
185 req->test_audio_period_ch_8 = ret;
186 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_8 = 0x%x\n", ret);
191 static int dp_link_parse_audio_pattern_type(struct dp_link_private *link)
196 int const max_audio_pattern_type = 0x1;
198 rlen = drm_dp_dpcd_readb(link->aux,
199 DP_TEST_AUDIO_PATTERN_TYPE, &data);
201 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
205 /* Audio Pattern Type - Bits 7:0 */
206 if ((int)data > max_audio_pattern_type) {
207 DRM_ERROR("invalid audio pattern type = 0x%x\n", data);
212 link->dp_link.test_audio.test_audio_pattern_type = data;
213 drm_dbg_dp(link->drm_dev, "audio pattern type = 0x%x\n", data);
218 static int dp_link_parse_audio_mode(struct dp_link_private *link)
223 int const max_audio_sampling_rate = 0x6;
224 int const max_audio_channel_count = 0x8;
225 int sampling_rate = 0x0;
226 int channel_count = 0x0;
228 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_AUDIO_MODE, &data);
230 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
234 /* Sampling Rate - Bits 3:0 */
235 sampling_rate = data & 0xF;
236 if (sampling_rate > max_audio_sampling_rate) {
237 DRM_ERROR("sampling rate (0x%x) greater than max (0x%x)\n",
238 sampling_rate, max_audio_sampling_rate);
243 /* Channel Count - Bits 7:4 */
244 channel_count = ((data & 0xF0) >> 4) + 1;
245 if (channel_count > max_audio_channel_count) {
246 DRM_ERROR("channel_count (0x%x) greater than max (0x%x)\n",
247 channel_count, max_audio_channel_count);
252 link->dp_link.test_audio.test_audio_sampling_rate = sampling_rate;
253 link->dp_link.test_audio.test_audio_channel_count = channel_count;
254 drm_dbg_dp(link->drm_dev,
255 "sampling_rate = 0x%x, channel_count = 0x%x\n",
256 sampling_rate, channel_count);
261 static int dp_link_parse_audio_pattern_params(struct dp_link_private *link)
265 ret = dp_link_parse_audio_mode(link);
269 ret = dp_link_parse_audio_pattern_type(link);
273 ret = dp_link_parse_audio_channel_period(link);
279 static bool dp_link_is_video_pattern_valid(u32 pattern)
282 case DP_NO_TEST_PATTERN:
284 case DP_BLACK_AND_WHITE_VERTICAL_LINES:
285 case DP_COLOR_SQUARE:
293 * dp_link_is_bit_depth_valid() - validates the bit depth requested
294 * @tbd: bit depth requested by the sink
296 * Returns true if the requested bit depth is supported.
298 static bool dp_link_is_bit_depth_valid(u32 tbd)
300 /* DP_TEST_VIDEO_PATTERN_NONE is treated as invalid */
302 case DP_TEST_BIT_DEPTH_6:
303 case DP_TEST_BIT_DEPTH_8:
304 case DP_TEST_BIT_DEPTH_10:
311 static int dp_link_parse_timing_params1(struct dp_link_private *link,
312 int addr, int len, u32 *val)
320 /* Read the requested video link pattern (Byte 0x221). */
321 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
323 DRM_ERROR("failed to read 0x%x\n", addr);
327 *val = bp[1] | (bp[0] << 8);
332 static int dp_link_parse_timing_params2(struct dp_link_private *link,
334 u32 *val1, u32 *val2)
342 /* Read the requested video link pattern (Byte 0x221). */
343 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
345 DRM_ERROR("failed to read 0x%x\n", addr);
349 *val1 = (bp[0] & BIT(7)) >> 7;
350 *val2 = bp[1] | ((bp[0] & 0x7F) << 8);
355 static int dp_link_parse_timing_params3(struct dp_link_private *link,
362 rlen = drm_dp_dpcd_read(link->aux, addr, &bp, len);
364 DRM_ERROR("failed to read 0x%x\n", addr);
373 * dp_link_parse_video_pattern_params() - parses video pattern parameters from DPCD
374 * @link: Display Port Driver data
376 * Returns 0 if it successfully parses the video link pattern and the link
377 * bit depth requested by the sink and, and if the values parsed are valid.
379 static int dp_link_parse_video_pattern_params(struct dp_link_private *link)
385 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_PATTERN, &bp);
387 DRM_ERROR("failed to read link video pattern. rlen=%zd\n",
392 if (!dp_link_is_video_pattern_valid(bp)) {
393 DRM_ERROR("invalid link video pattern = 0x%x\n", bp);
398 link->dp_link.test_video.test_video_pattern = bp;
400 /* Read the requested color bit depth and dynamic range (Byte 0x232) */
401 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_MISC0, &bp);
403 DRM_ERROR("failed to read link bit depth. rlen=%zd\n", rlen);
408 link->dp_link.test_video.test_dyn_range =
409 (bp & DP_TEST_DYNAMIC_RANGE_CEA);
411 /* Color bit depth */
412 bp &= DP_TEST_BIT_DEPTH_MASK;
413 if (!dp_link_is_bit_depth_valid(bp)) {
414 DRM_ERROR("invalid link bit depth = 0x%x\n", bp);
419 link->dp_link.test_video.test_bit_depth = bp;
421 /* resolution timing params */
422 ret = dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
423 &link->dp_link.test_video.test_h_total);
425 DRM_ERROR("failed to parse test_htotal(DP_TEST_H_TOTAL_HI)\n");
429 ret = dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
430 &link->dp_link.test_video.test_v_total);
432 DRM_ERROR("failed to parse test_v_total(DP_TEST_V_TOTAL_HI)\n");
436 ret = dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
437 &link->dp_link.test_video.test_h_start);
439 DRM_ERROR("failed to parse test_h_start(DP_TEST_H_START_HI)\n");
443 ret = dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
444 &link->dp_link.test_video.test_v_start);
446 DRM_ERROR("failed to parse test_v_start(DP_TEST_V_START_HI)\n");
450 ret = dp_link_parse_timing_params2(link, DP_TEST_HSYNC_HI, 2,
451 &link->dp_link.test_video.test_hsync_pol,
452 &link->dp_link.test_video.test_hsync_width);
454 DRM_ERROR("failed to parse (DP_TEST_HSYNC_HI)\n");
458 ret = dp_link_parse_timing_params2(link, DP_TEST_VSYNC_HI, 2,
459 &link->dp_link.test_video.test_vsync_pol,
460 &link->dp_link.test_video.test_vsync_width);
462 DRM_ERROR("failed to parse (DP_TEST_VSYNC_HI)\n");
466 ret = dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
467 &link->dp_link.test_video.test_h_width);
469 DRM_ERROR("failed to parse test_h_width(DP_TEST_H_WIDTH_HI)\n");
473 ret = dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
474 &link->dp_link.test_video.test_v_height);
476 DRM_ERROR("failed to parse test_v_height\n");
480 ret = dp_link_parse_timing_params3(link, DP_TEST_MISC1,
481 &link->dp_link.test_video.test_rr_d);
482 link->dp_link.test_video.test_rr_d &= DP_TEST_REFRESH_DENOMINATOR;
484 DRM_ERROR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
488 ret = dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
489 &link->dp_link.test_video.test_rr_n);
491 DRM_ERROR("failed to parse test_rr_n\n");
495 drm_dbg_dp(link->drm_dev,
496 "link video pattern = 0x%x\n"
497 "link dynamic range = 0x%x\n"
498 "link bit depth = 0x%x\n"
499 "TEST_H_TOTAL = %d, TEST_V_TOTAL = %d\n"
500 "TEST_H_START = %d, TEST_V_START = %d\n"
501 "TEST_HSYNC_POL = %d\n"
502 "TEST_HSYNC_WIDTH = %d\n"
503 "TEST_VSYNC_POL = %d\n"
504 "TEST_VSYNC_WIDTH = %d\n"
505 "TEST_H_WIDTH = %d\n"
506 "TEST_V_HEIGHT = %d\n"
507 "TEST_REFRESH_DENOMINATOR = %d\n"
508 "TEST_REFRESH_NUMERATOR = %d\n",
509 link->dp_link.test_video.test_video_pattern,
510 link->dp_link.test_video.test_dyn_range,
511 link->dp_link.test_video.test_bit_depth,
512 link->dp_link.test_video.test_h_total,
513 link->dp_link.test_video.test_v_total,
514 link->dp_link.test_video.test_h_start,
515 link->dp_link.test_video.test_v_start,
516 link->dp_link.test_video.test_hsync_pol,
517 link->dp_link.test_video.test_hsync_width,
518 link->dp_link.test_video.test_vsync_pol,
519 link->dp_link.test_video.test_vsync_width,
520 link->dp_link.test_video.test_h_width,
521 link->dp_link.test_video.test_v_height,
522 link->dp_link.test_video.test_rr_d,
523 link->dp_link.test_video.test_rr_n);
529 * dp_link_parse_link_training_params() - parses link training parameters from
531 * @link: Display Port Driver data
533 * Returns 0 if it successfully parses the link rate (Byte 0x219) and lane
534 * count (Byte 0x220), and if these values parse are valid.
536 static int dp_link_parse_link_training_params(struct dp_link_private *link)
541 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LINK_RATE, &bp);
543 DRM_ERROR("failed to read link rate. rlen=%zd\n", rlen);
547 if (!is_link_rate_valid(bp)) {
548 DRM_ERROR("invalid link rate = 0x%x\n", bp);
552 link->request.test_link_rate = bp;
553 drm_dbg_dp(link->drm_dev, "link rate = 0x%x\n",
554 link->request.test_link_rate);
556 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LANE_COUNT, &bp);
558 DRM_ERROR("failed to read lane count. rlen=%zd\n", rlen);
561 bp &= DP_MAX_LANE_COUNT_MASK;
563 if (!is_lane_count_valid(bp)) {
564 DRM_ERROR("invalid lane count = 0x%x\n", bp);
568 link->request.test_lane_count = bp;
569 drm_dbg_dp(link->drm_dev, "lane count = 0x%x\n",
570 link->request.test_lane_count);
575 * dp_link_parse_phy_test_params() - parses the phy link parameters
576 * @link: Display Port Driver data
578 * Parses the DPCD (Byte 0x248) for the DP PHY link pattern that is being
581 static int dp_link_parse_phy_test_params(struct dp_link_private *link)
586 rlen = drm_dp_dpcd_readb(link->aux, DP_PHY_TEST_PATTERN,
589 DRM_ERROR("failed to read phy link pattern. rlen=%zd\n", rlen);
593 link->dp_link.phy_params.phy_test_pattern_sel = data & 0x07;
595 drm_dbg_dp(link->drm_dev, "phy_test_pattern_sel = 0x%x\n", data);
598 case DP_PHY_TEST_PATTERN_SEL_MASK:
599 case DP_PHY_TEST_PATTERN_NONE:
600 case DP_PHY_TEST_PATTERN_D10_2:
601 case DP_PHY_TEST_PATTERN_ERROR_COUNT:
602 case DP_PHY_TEST_PATTERN_PRBS7:
603 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
604 case DP_PHY_TEST_PATTERN_CP2520:
612 * dp_link_is_video_audio_test_requested() - checks for audio/video link request
613 * @link: link requested by the sink
615 * Returns true if the requested link is a permitted audio/video link.
617 static bool dp_link_is_video_audio_test_requested(u32 link)
619 u8 video_audio_test = (DP_TEST_LINK_VIDEO_PATTERN |
620 DP_TEST_LINK_AUDIO_PATTERN |
621 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
623 return ((link & video_audio_test) &&
624 !(link & ~video_audio_test));
628 * dp_link_parse_request() - parses link request parameters from sink
629 * @link: Display Port Driver data
631 * Parses the DPCD to check if an automated link is requested (Byte 0x201),
632 * and what type of link automation is being requested (Byte 0x218).
634 static int dp_link_parse_request(struct dp_link_private *link)
641 * Read the device service IRQ vector (Byte 0x201) to determine
642 * whether an automated link has been requested by the sink.
644 rlen = drm_dp_dpcd_readb(link->aux,
645 DP_DEVICE_SERVICE_IRQ_VECTOR, &data);
647 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
651 drm_dbg_dp(link->drm_dev, "device service irq vector = 0x%x\n", data);
653 if (!(data & DP_AUTOMATED_TEST_REQUEST)) {
654 drm_dbg_dp(link->drm_dev, "no test requested\n");
659 * Read the link request byte (Byte 0x218) to determine what type
660 * of automated link has been requested by the sink.
662 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_REQUEST, &data);
664 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
668 if (!data || (data == DP_TEST_LINK_FAUX_PATTERN)) {
669 drm_dbg_dp(link->drm_dev, "link 0x%x not supported\n", data);
673 drm_dbg_dp(link->drm_dev, "Test:(0x%x) requested\n", data);
674 link->request.test_requested = data;
675 if (link->request.test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) {
676 ret = dp_link_parse_phy_test_params(link);
679 ret = dp_link_parse_link_training_params(link);
684 if (link->request.test_requested == DP_TEST_LINK_TRAINING) {
685 ret = dp_link_parse_link_training_params(link);
690 if (dp_link_is_video_audio_test_requested(
691 link->request.test_requested)) {
692 ret = dp_link_parse_video_pattern_params(link);
696 ret = dp_link_parse_audio_pattern_params(link);
700 * Send a DP_TEST_ACK if all link parameters are valid, otherwise send
704 link->dp_link.test_response = DP_TEST_NAK;
706 if (link->request.test_requested != DP_TEST_LINK_EDID_READ)
707 link->dp_link.test_response = DP_TEST_ACK;
709 link->dp_link.test_response =
710 DP_TEST_EDID_CHECKSUM_WRITE;
717 * dp_link_parse_sink_count() - parses the sink count
718 * @dp_link: pointer to link module data
720 * Parses the DPCD to check if there is an update to the sink count
721 * (Byte 0x200), and whether all the sink devices connected have Content
722 * Protection enabled.
724 static int dp_link_parse_sink_count(struct dp_link *dp_link)
729 struct dp_link_private *link = container_of(dp_link,
730 struct dp_link_private, dp_link);
732 rlen = drm_dp_dpcd_readb(link->aux, DP_SINK_COUNT,
733 &link->dp_link.sink_count);
735 DRM_ERROR("sink count read failed. rlen=%zd\n", rlen);
739 cp_ready = link->dp_link.sink_count & DP_SINK_CP_READY;
741 link->dp_link.sink_count =
742 DP_GET_SINK_COUNT(link->dp_link.sink_count);
744 drm_dbg_dp(link->drm_dev, "sink_count = 0x%x, cp_ready = 0x%x\n",
745 link->dp_link.sink_count, cp_ready);
749 static int dp_link_parse_sink_status_field(struct dp_link_private *link)
753 link->prev_sink_count = link->dp_link.sink_count;
754 len = dp_link_parse_sink_count(&link->dp_link);
756 DRM_ERROR("DP parse sink count failed\n");
760 len = drm_dp_dpcd_read_link_status(link->aux,
762 if (len < DP_LINK_STATUS_SIZE) {
763 DRM_ERROR("DP link status read failed\n");
767 return dp_link_parse_request(link);
771 * dp_link_process_link_training_request() - processes new training requests
772 * @link: Display Port link data
774 * This function will handle new link training requests that are initiated by
775 * the sink. In particular, it will update the requested lane count and link
776 * rate, and then trigger the link retraining procedure.
778 * The function will return 0 if a link training request has been processed,
779 * otherwise it will return -EINVAL.
781 static int dp_link_process_link_training_request(struct dp_link_private *link)
783 if (link->request.test_requested != DP_TEST_LINK_TRAINING)
786 drm_dbg_dp(link->drm_dev,
787 "Test:0x%x link rate = 0x%x, lane count = 0x%x\n",
788 DP_TEST_LINK_TRAINING,
789 link->request.test_link_rate,
790 link->request.test_lane_count);
792 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
793 link->dp_link.link_params.rate =
794 drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
799 bool dp_link_send_test_response(struct dp_link *dp_link)
801 struct dp_link_private *link = NULL;
805 DRM_ERROR("invalid input\n");
809 link = container_of(dp_link, struct dp_link_private, dp_link);
811 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_RESPONSE,
812 dp_link->test_response);
817 int dp_link_psm_config(struct dp_link *dp_link,
818 struct dp_link_info *link_info, bool enable)
820 struct dp_link_private *link = NULL;
824 DRM_ERROR("invalid params\n");
828 link = container_of(dp_link, struct dp_link_private, dp_link);
830 mutex_lock(&link->psm_mutex);
832 ret = dp_aux_link_power_down(link->aux, link_info);
834 ret = dp_aux_link_power_up(link->aux, link_info);
837 DRM_ERROR("Failed to %s low power mode\n", enable ?
840 dp_link->psm_enabled = enable;
842 mutex_unlock(&link->psm_mutex);
846 bool dp_link_send_edid_checksum(struct dp_link *dp_link, u8 checksum)
848 struct dp_link_private *link = NULL;
852 DRM_ERROR("invalid input\n");
856 link = container_of(dp_link, struct dp_link_private, dp_link);
858 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_EDID_CHECKSUM,
863 static void dp_link_parse_vx_px(struct dp_link_private *link)
865 drm_dbg_dp(link->drm_dev, "vx: 0=%d, 1=%d, 2=%d, 3=%d\n",
866 drm_dp_get_adjust_request_voltage(link->link_status, 0),
867 drm_dp_get_adjust_request_voltage(link->link_status, 1),
868 drm_dp_get_adjust_request_voltage(link->link_status, 2),
869 drm_dp_get_adjust_request_voltage(link->link_status, 3));
871 drm_dbg_dp(link->drm_dev, "px: 0=%d, 1=%d, 2=%d, 3=%d\n",
872 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0),
873 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 1),
874 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 2),
875 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 3));
878 * Update the voltage and pre-emphasis levels as per DPCD request
881 drm_dbg_dp(link->drm_dev,
882 "Current: v_level = 0x%x, p_level = 0x%x\n",
883 link->dp_link.phy_params.v_level,
884 link->dp_link.phy_params.p_level);
885 link->dp_link.phy_params.v_level =
886 drm_dp_get_adjust_request_voltage(link->link_status, 0);
887 link->dp_link.phy_params.p_level =
888 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0);
890 link->dp_link.phy_params.p_level >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
892 drm_dbg_dp(link->drm_dev,
893 "Requested: v_level = 0x%x, p_level = 0x%x\n",
894 link->dp_link.phy_params.v_level,
895 link->dp_link.phy_params.p_level);
899 * dp_link_process_phy_test_pattern_request() - process new phy link requests
900 * @link: Display Port Driver data
902 * This function will handle new phy link pattern requests that are initiated
903 * by the sink. The function will return 0 if a phy link pattern has been
904 * processed, otherwise it will return -EINVAL.
906 static int dp_link_process_phy_test_pattern_request(
907 struct dp_link_private *link)
909 if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
910 drm_dbg_dp(link->drm_dev, "no phy test\n");
914 if (!is_link_rate_valid(link->request.test_link_rate) ||
915 !is_lane_count_valid(link->request.test_lane_count)) {
916 DRM_ERROR("Invalid: link rate = 0x%x,lane count = 0x%x\n",
917 link->request.test_link_rate,
918 link->request.test_lane_count);
922 drm_dbg_dp(link->drm_dev,
923 "Current: rate = 0x%x, lane count = 0x%x\n",
924 link->dp_link.link_params.rate,
925 link->dp_link.link_params.num_lanes);
927 drm_dbg_dp(link->drm_dev,
928 "Requested: rate = 0x%x, lane count = 0x%x\n",
929 link->request.test_link_rate,
930 link->request.test_lane_count);
932 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
933 link->dp_link.link_params.rate =
934 drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
936 dp_link_parse_vx_px(link);
941 static bool dp_link_read_psr_error_status(struct dp_link_private *link)
945 drm_dp_dpcd_read(link->aux, DP_PSR_ERROR_STATUS, &status, 1);
947 if (status & DP_PSR_LINK_CRC_ERROR)
948 DRM_ERROR("PSR LINK CRC ERROR\n");
949 else if (status & DP_PSR_RFB_STORAGE_ERROR)
950 DRM_ERROR("PSR RFB STORAGE ERROR\n");
951 else if (status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
952 DRM_ERROR("PSR VSC SDP UNCORRECTABLE ERROR\n");
959 static bool dp_link_psr_capability_changed(struct dp_link_private *link)
963 drm_dp_dpcd_read(link->aux, DP_PSR_ESI, &status, 1);
965 if (status & DP_PSR_CAPS_CHANGE) {
966 drm_dbg_dp(link->drm_dev, "PSR Capability Change\n");
973 static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
975 return link_status[r - DP_LANE0_1_STATUS];
979 * dp_link_process_link_status_update() - processes link status updates
980 * @link: Display Port link module data
982 * This function will check for changes in the link status, e.g. clock
983 * recovery done on all lanes, and trigger link training if there is a
984 * failure/error on the link.
986 * The function will return 0 if the a link status update has been processed,
987 * otherwise it will return -EINVAL.
989 static int dp_link_process_link_status_update(struct dp_link_private *link)
991 bool channel_eq_done = drm_dp_channel_eq_ok(link->link_status,
992 link->dp_link.link_params.num_lanes);
994 bool clock_recovery_done = drm_dp_clock_recovery_ok(link->link_status,
995 link->dp_link.link_params.num_lanes);
997 drm_dbg_dp(link->drm_dev,
998 "channel_eq_done = %d, clock_recovery_done = %d\n",
999 channel_eq_done, clock_recovery_done);
1001 if (channel_eq_done && clock_recovery_done)
1008 * dp_link_process_ds_port_status_change() - process port status changes
1009 * @link: Display Port Driver data
1011 * This function will handle downstream port updates that are initiated by
1012 * the sink. If the downstream port status has changed, the EDID is read via
1015 * The function will return 0 if a downstream port update has been
1016 * processed, otherwise it will return -EINVAL.
1018 static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
1020 if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
1021 DP_DOWNSTREAM_PORT_STATUS_CHANGED)
1024 if (link->prev_sink_count == link->dp_link.sink_count)
1028 /* reset prev_sink_count */
1029 link->prev_sink_count = link->dp_link.sink_count;
1034 static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
1036 return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
1037 && !(link->request.test_requested &
1038 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
1041 static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
1043 return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
1046 static void dp_link_reset_data(struct dp_link_private *link)
1048 link->request = (const struct dp_link_request){ 0 };
1049 link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
1050 link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
1051 link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
1052 link->dp_link.phy_params.phy_test_pattern_sel = 0;
1053 link->dp_link.sink_request = 0;
1054 link->dp_link.test_response = 0;
1058 * dp_link_process_request() - handle HPD IRQ transition to HIGH
1059 * @dp_link: pointer to link module data
1061 * This function will handle the HPD IRQ state transitions from LOW to HIGH
1062 * (including cases when there are back to back HPD IRQ HIGH) indicating
1063 * the start of a new link training request or sink status update.
1065 int dp_link_process_request(struct dp_link *dp_link)
1068 struct dp_link_private *link;
1071 DRM_ERROR("invalid input\n");
1075 link = container_of(dp_link, struct dp_link_private, dp_link);
1077 dp_link_reset_data(link);
1079 ret = dp_link_parse_sink_status_field(link);
1083 if (link->request.test_requested == DP_TEST_LINK_EDID_READ) {
1084 dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
1085 } else if (!dp_link_process_ds_port_status_change(link)) {
1086 dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
1087 } else if (!dp_link_process_link_training_request(link)) {
1088 dp_link->sink_request |= DP_TEST_LINK_TRAINING;
1089 } else if (!dp_link_process_phy_test_pattern_request(link)) {
1090 dp_link->sink_request |= DP_TEST_LINK_PHY_TEST_PATTERN;
1091 } else if (dp_link_read_psr_error_status(link)) {
1092 DRM_ERROR("PSR IRQ_HPD received\n");
1093 } else if (dp_link_psr_capability_changed(link)) {
1094 drm_dbg_dp(link->drm_dev, "PSR Capability changed\n");
1096 ret = dp_link_process_link_status_update(link);
1098 dp_link->sink_request |= DP_LINK_STATUS_UPDATED;
1100 if (dp_link_is_video_pattern_requested(link)) {
1102 dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
1104 if (dp_link_is_audio_pattern_requested(link)) {
1105 dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
1111 drm_dbg_dp(link->drm_dev, "sink request=%#x\n",
1112 dp_link->sink_request);
1116 int dp_link_get_colorimetry_config(struct dp_link *dp_link)
1118 u32 cc = DP_MISC0_COLORIMERY_CFG_LEGACY_RGB;
1119 struct dp_link_private *link;
1122 DRM_ERROR("invalid input\n");
1126 link = container_of(dp_link, struct dp_link_private, dp_link);
1129 * Unless a video pattern CTS test is ongoing, use RGB_VESA
1130 * Only RGB_VESA and RGB_CEA supported for now
1132 if (dp_link_is_video_pattern_requested(link)) {
1133 if (link->dp_link.test_video.test_dyn_range &
1134 DP_TEST_DYNAMIC_RANGE_CEA)
1135 cc = DP_MISC0_COLORIMERY_CFG_CEA_RGB;
1141 int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
1144 int v_max = 0, p_max = 0;
1145 struct dp_link_private *link;
1148 DRM_ERROR("invalid input\n");
1152 link = container_of(dp_link, struct dp_link_private, dp_link);
1154 /* use the max level across lanes */
1155 for (i = 0; i < dp_link->link_params.num_lanes; i++) {
1156 u8 data_v = drm_dp_get_adjust_request_voltage(link_status, i);
1157 u8 data_p = drm_dp_get_adjust_request_pre_emphasis(link_status,
1159 drm_dbg_dp(link->drm_dev,
1160 "lane=%d req_vol_swing=%d req_pre_emphasis=%d\n",
1168 dp_link->phy_params.v_level = v_max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
1169 dp_link->phy_params.p_level = p_max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
1172 * Adjust the voltage swing and pre-emphasis level combination to within
1173 * the allowable range.
1175 if (dp_link->phy_params.v_level > DP_TRAIN_VOLTAGE_SWING_MAX) {
1176 drm_dbg_dp(link->drm_dev,
1177 "Requested vSwingLevel=%d, change to %d\n",
1178 dp_link->phy_params.v_level,
1179 DP_TRAIN_VOLTAGE_SWING_MAX);
1180 dp_link->phy_params.v_level = DP_TRAIN_VOLTAGE_SWING_MAX;
1183 if (dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_MAX) {
1184 drm_dbg_dp(link->drm_dev,
1185 "Requested preEmphasisLevel=%d, change to %d\n",
1186 dp_link->phy_params.p_level,
1187 DP_TRAIN_PRE_EMPHASIS_MAX);
1188 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_MAX;
1191 if ((dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_LVL_1)
1192 && (dp_link->phy_params.v_level ==
1193 DP_TRAIN_VOLTAGE_SWING_LVL_2)) {
1194 drm_dbg_dp(link->drm_dev,
1195 "Requested preEmphasisLevel=%d, change to %d\n",
1196 dp_link->phy_params.p_level,
1197 DP_TRAIN_PRE_EMPHASIS_LVL_1);
1198 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_LVL_1;
1201 drm_dbg_dp(link->drm_dev, "adjusted: v_level=%d, p_level=%d\n",
1202 dp_link->phy_params.v_level, dp_link->phy_params.p_level);
1207 void dp_link_reset_phy_params_vx_px(struct dp_link *dp_link)
1209 dp_link->phy_params.v_level = 0;
1210 dp_link->phy_params.p_level = 0;
1213 u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
1216 struct dp_link_private *link;
1218 link = container_of(dp_link, struct dp_link_private, dp_link);
1221 * Few simplistic rules and assumptions made here:
1222 * 1. Test bit depth is bit depth per color component
1223 * 2. Assume 3 color components
1227 tbd = DP_TEST_BIT_DEPTH_6;
1230 tbd = DP_TEST_BIT_DEPTH_8;
1233 tbd = DP_TEST_BIT_DEPTH_10;
1236 drm_dbg_dp(link->drm_dev, "bpp=%d not supported, use bpc=8\n",
1238 tbd = DP_TEST_BIT_DEPTH_8;
1242 tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
1247 struct dp_link *dp_link_get(struct device *dev, struct drm_dp_aux *aux)
1249 struct dp_link_private *link;
1250 struct dp_link *dp_link;
1253 DRM_ERROR("invalid input\n");
1254 return ERR_PTR(-EINVAL);
1257 link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
1259 return ERR_PTR(-ENOMEM);
1264 mutex_init(&link->psm_mutex);
1265 dp_link = &link->dp_link;