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_device *drm_dev;
40 struct drm_dp_aux *aux;
41 struct dp_link dp_link;
43 struct dp_link_request request;
44 struct mutex psm_mutex;
45 u8 link_status[DP_LINK_STATUS_SIZE];
48 static int dp_aux_link_power_up(struct drm_dp_aux *aux,
49 struct dp_link_info *link)
54 if (link->revision < 0x11)
57 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
61 value &= ~DP_SET_POWER_MASK;
62 value |= DP_SET_POWER_D0;
64 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
68 usleep_range(1000, 2000);
73 static int dp_aux_link_power_down(struct drm_dp_aux *aux,
74 struct dp_link_info *link)
79 if (link->revision < 0x11)
82 err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
86 value &= ~DP_SET_POWER_MASK;
87 value |= DP_SET_POWER_D3;
89 err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
96 static int dp_link_get_period(struct dp_link_private *link, int const addr)
100 u32 const max_audio_period = 0xA;
102 /* TEST_AUDIO_PERIOD_CH_XX */
103 if (drm_dp_dpcd_readb(link->aux, addr, &data) < 0) {
104 DRM_ERROR("failed to read test_audio_period (0x%x)\n", addr);
109 /* Period - Bits 3:0 */
111 if ((int)data > max_audio_period) {
112 DRM_ERROR("invalid test_audio_period_ch_1 = 0x%x\n", data);
122 static int dp_link_parse_audio_channel_period(struct dp_link_private *link)
125 struct dp_link_test_audio *req = &link->dp_link.test_audio;
127 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH1);
131 req->test_audio_period_ch_1 = ret;
132 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_1 = 0x%x\n", ret);
134 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH2);
138 req->test_audio_period_ch_2 = ret;
139 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_2 = 0x%x\n", ret);
141 /* TEST_AUDIO_PERIOD_CH_3 (Byte 0x275) */
142 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH3);
146 req->test_audio_period_ch_3 = ret;
147 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_3 = 0x%x\n", ret);
149 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH4);
153 req->test_audio_period_ch_4 = ret;
154 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_4 = 0x%x\n", ret);
156 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH5);
160 req->test_audio_period_ch_5 = ret;
161 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_5 = 0x%x\n", ret);
163 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH6);
167 req->test_audio_period_ch_6 = ret;
168 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_6 = 0x%x\n", ret);
170 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH7);
174 req->test_audio_period_ch_7 = ret;
175 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_7 = 0x%x\n", ret);
177 ret = dp_link_get_period(link, DP_TEST_AUDIO_PERIOD_CH8);
181 req->test_audio_period_ch_8 = ret;
182 drm_dbg_dp(link->drm_dev, "test_audio_period_ch_8 = 0x%x\n", ret);
187 static int dp_link_parse_audio_pattern_type(struct dp_link_private *link)
192 int const max_audio_pattern_type = 0x1;
194 rlen = drm_dp_dpcd_readb(link->aux,
195 DP_TEST_AUDIO_PATTERN_TYPE, &data);
197 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
201 /* Audio Pattern Type - Bits 7:0 */
202 if ((int)data > max_audio_pattern_type) {
203 DRM_ERROR("invalid audio pattern type = 0x%x\n", data);
208 link->dp_link.test_audio.test_audio_pattern_type = data;
209 drm_dbg_dp(link->drm_dev, "audio pattern type = 0x%x\n", data);
214 static int dp_link_parse_audio_mode(struct dp_link_private *link)
219 int const max_audio_sampling_rate = 0x6;
220 int const max_audio_channel_count = 0x8;
221 int sampling_rate = 0x0;
222 int channel_count = 0x0;
224 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_AUDIO_MODE, &data);
226 DRM_ERROR("failed to read link audio mode. rlen=%zd\n", rlen);
230 /* Sampling Rate - Bits 3:0 */
231 sampling_rate = data & 0xF;
232 if (sampling_rate > max_audio_sampling_rate) {
233 DRM_ERROR("sampling rate (0x%x) greater than max (0x%x)\n",
234 sampling_rate, max_audio_sampling_rate);
239 /* Channel Count - Bits 7:4 */
240 channel_count = ((data & 0xF0) >> 4) + 1;
241 if (channel_count > max_audio_channel_count) {
242 DRM_ERROR("channel_count (0x%x) greater than max (0x%x)\n",
243 channel_count, max_audio_channel_count);
248 link->dp_link.test_audio.test_audio_sampling_rate = sampling_rate;
249 link->dp_link.test_audio.test_audio_channel_count = channel_count;
250 drm_dbg_dp(link->drm_dev,
251 "sampling_rate = 0x%x, channel_count = 0x%x\n",
252 sampling_rate, channel_count);
257 static int dp_link_parse_audio_pattern_params(struct dp_link_private *link)
261 ret = dp_link_parse_audio_mode(link);
265 ret = dp_link_parse_audio_pattern_type(link);
269 ret = dp_link_parse_audio_channel_period(link);
275 static bool dp_link_is_video_pattern_valid(u32 pattern)
278 case DP_NO_TEST_PATTERN:
280 case DP_BLACK_AND_WHITE_VERTICAL_LINES:
281 case DP_COLOR_SQUARE:
289 * dp_link_is_bit_depth_valid() - validates the bit depth requested
290 * @tbd: bit depth requested by the sink
292 * Returns true if the requested bit depth is supported.
294 static bool dp_link_is_bit_depth_valid(u32 tbd)
296 /* DP_TEST_VIDEO_PATTERN_NONE is treated as invalid */
298 case DP_TEST_BIT_DEPTH_6:
299 case DP_TEST_BIT_DEPTH_8:
300 case DP_TEST_BIT_DEPTH_10:
307 static int dp_link_parse_timing_params1(struct dp_link_private *link,
308 int addr, int len, u32 *val)
316 /* Read the requested video link pattern (Byte 0x221). */
317 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
319 DRM_ERROR("failed to read 0x%x\n", addr);
323 *val = bp[1] | (bp[0] << 8);
328 static int dp_link_parse_timing_params2(struct dp_link_private *link,
330 u32 *val1, u32 *val2)
338 /* Read the requested video link pattern (Byte 0x221). */
339 rlen = drm_dp_dpcd_read(link->aux, addr, bp, len);
341 DRM_ERROR("failed to read 0x%x\n", addr);
345 *val1 = (bp[0] & BIT(7)) >> 7;
346 *val2 = bp[1] | ((bp[0] & 0x7F) << 8);
351 static int dp_link_parse_timing_params3(struct dp_link_private *link,
358 rlen = drm_dp_dpcd_read(link->aux, addr, &bp, len);
360 DRM_ERROR("failed to read 0x%x\n", addr);
369 * dp_link_parse_video_pattern_params() - parses video pattern parameters from DPCD
370 * @link: Display Port Driver data
372 * Returns 0 if it successfully parses the video link pattern and the link
373 * bit depth requested by the sink and, and if the values parsed are valid.
375 static int dp_link_parse_video_pattern_params(struct dp_link_private *link)
381 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_PATTERN, &bp);
383 DRM_ERROR("failed to read link video pattern. rlen=%zd\n",
388 if (!dp_link_is_video_pattern_valid(bp)) {
389 DRM_ERROR("invalid link video pattern = 0x%x\n", bp);
394 link->dp_link.test_video.test_video_pattern = bp;
396 /* Read the requested color bit depth and dynamic range (Byte 0x232) */
397 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_MISC0, &bp);
399 DRM_ERROR("failed to read link bit depth. rlen=%zd\n", rlen);
404 link->dp_link.test_video.test_dyn_range =
405 (bp & DP_TEST_DYNAMIC_RANGE_CEA);
407 /* Color bit depth */
408 bp &= DP_TEST_BIT_DEPTH_MASK;
409 if (!dp_link_is_bit_depth_valid(bp)) {
410 DRM_ERROR("invalid link bit depth = 0x%x\n", bp);
415 link->dp_link.test_video.test_bit_depth = bp;
417 /* resolution timing params */
418 ret = dp_link_parse_timing_params1(link, DP_TEST_H_TOTAL_HI, 2,
419 &link->dp_link.test_video.test_h_total);
421 DRM_ERROR("failed to parse test_htotal(DP_TEST_H_TOTAL_HI)\n");
425 ret = dp_link_parse_timing_params1(link, DP_TEST_V_TOTAL_HI, 2,
426 &link->dp_link.test_video.test_v_total);
428 DRM_ERROR("failed to parse test_v_total(DP_TEST_V_TOTAL_HI)\n");
432 ret = dp_link_parse_timing_params1(link, DP_TEST_H_START_HI, 2,
433 &link->dp_link.test_video.test_h_start);
435 DRM_ERROR("failed to parse test_h_start(DP_TEST_H_START_HI)\n");
439 ret = dp_link_parse_timing_params1(link, DP_TEST_V_START_HI, 2,
440 &link->dp_link.test_video.test_v_start);
442 DRM_ERROR("failed to parse test_v_start(DP_TEST_V_START_HI)\n");
446 ret = dp_link_parse_timing_params2(link, DP_TEST_HSYNC_HI, 2,
447 &link->dp_link.test_video.test_hsync_pol,
448 &link->dp_link.test_video.test_hsync_width);
450 DRM_ERROR("failed to parse (DP_TEST_HSYNC_HI)\n");
454 ret = dp_link_parse_timing_params2(link, DP_TEST_VSYNC_HI, 2,
455 &link->dp_link.test_video.test_vsync_pol,
456 &link->dp_link.test_video.test_vsync_width);
458 DRM_ERROR("failed to parse (DP_TEST_VSYNC_HI)\n");
462 ret = dp_link_parse_timing_params1(link, DP_TEST_H_WIDTH_HI, 2,
463 &link->dp_link.test_video.test_h_width);
465 DRM_ERROR("failed to parse test_h_width(DP_TEST_H_WIDTH_HI)\n");
469 ret = dp_link_parse_timing_params1(link, DP_TEST_V_HEIGHT_HI, 2,
470 &link->dp_link.test_video.test_v_height);
472 DRM_ERROR("failed to parse test_v_height\n");
476 ret = dp_link_parse_timing_params3(link, DP_TEST_MISC1,
477 &link->dp_link.test_video.test_rr_d);
478 link->dp_link.test_video.test_rr_d &= DP_TEST_REFRESH_DENOMINATOR;
480 DRM_ERROR("failed to parse test_rr_d (DP_TEST_MISC1)\n");
484 ret = dp_link_parse_timing_params3(link, DP_TEST_REFRESH_RATE_NUMERATOR,
485 &link->dp_link.test_video.test_rr_n);
487 DRM_ERROR("failed to parse test_rr_n\n");
491 drm_dbg_dp(link->drm_dev,
492 "link video pattern = 0x%x\n"
493 "link dynamic range = 0x%x\n"
494 "link bit depth = 0x%x\n"
495 "TEST_H_TOTAL = %d, TEST_V_TOTAL = %d\n"
496 "TEST_H_START = %d, TEST_V_START = %d\n"
497 "TEST_HSYNC_POL = %d\n"
498 "TEST_HSYNC_WIDTH = %d\n"
499 "TEST_VSYNC_POL = %d\n"
500 "TEST_VSYNC_WIDTH = %d\n"
501 "TEST_H_WIDTH = %d\n"
502 "TEST_V_HEIGHT = %d\n"
503 "TEST_REFRESH_DENOMINATOR = %d\n"
504 "TEST_REFRESH_NUMERATOR = %d\n",
505 link->dp_link.test_video.test_video_pattern,
506 link->dp_link.test_video.test_dyn_range,
507 link->dp_link.test_video.test_bit_depth,
508 link->dp_link.test_video.test_h_total,
509 link->dp_link.test_video.test_v_total,
510 link->dp_link.test_video.test_h_start,
511 link->dp_link.test_video.test_v_start,
512 link->dp_link.test_video.test_hsync_pol,
513 link->dp_link.test_video.test_hsync_width,
514 link->dp_link.test_video.test_vsync_pol,
515 link->dp_link.test_video.test_vsync_width,
516 link->dp_link.test_video.test_h_width,
517 link->dp_link.test_video.test_v_height,
518 link->dp_link.test_video.test_rr_d,
519 link->dp_link.test_video.test_rr_n);
525 * dp_link_parse_link_training_params() - parses link training parameters from
527 * @link: Display Port Driver data
529 * Returns 0 if it successfully parses the link rate (Byte 0x219) and lane
530 * count (Byte 0x220), and if these values parse are valid.
532 static int dp_link_parse_link_training_params(struct dp_link_private *link)
537 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LINK_RATE, &bp);
539 DRM_ERROR("failed to read link rate. rlen=%zd\n", rlen);
543 if (!is_link_rate_valid(bp)) {
544 DRM_ERROR("invalid link rate = 0x%x\n", bp);
548 link->request.test_link_rate = bp;
549 drm_dbg_dp(link->drm_dev, "link rate = 0x%x\n",
550 link->request.test_link_rate);
552 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_LANE_COUNT, &bp);
554 DRM_ERROR("failed to read lane count. rlen=%zd\n", rlen);
557 bp &= DP_MAX_LANE_COUNT_MASK;
559 if (!is_lane_count_valid(bp)) {
560 DRM_ERROR("invalid lane count = 0x%x\n", bp);
564 link->request.test_lane_count = bp;
565 drm_dbg_dp(link->drm_dev, "lane count = 0x%x\n",
566 link->request.test_lane_count);
571 * dp_link_parse_phy_test_params() - parses the phy link parameters
572 * @link: Display Port Driver data
574 * Parses the DPCD (Byte 0x248) for the DP PHY link pattern that is being
577 static int dp_link_parse_phy_test_params(struct dp_link_private *link)
582 rlen = drm_dp_dpcd_readb(link->aux, DP_PHY_TEST_PATTERN,
585 DRM_ERROR("failed to read phy link pattern. rlen=%zd\n", rlen);
589 link->dp_link.phy_params.phy_test_pattern_sel = data & 0x07;
591 drm_dbg_dp(link->drm_dev, "phy_test_pattern_sel = 0x%x\n", data);
594 case DP_PHY_TEST_PATTERN_SEL_MASK:
595 case DP_PHY_TEST_PATTERN_NONE:
596 case DP_PHY_TEST_PATTERN_D10_2:
597 case DP_PHY_TEST_PATTERN_ERROR_COUNT:
598 case DP_PHY_TEST_PATTERN_PRBS7:
599 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
600 case DP_PHY_TEST_PATTERN_CP2520:
608 * dp_link_is_video_audio_test_requested() - checks for audio/video link request
609 * @link: link requested by the sink
611 * Returns true if the requested link is a permitted audio/video link.
613 static bool dp_link_is_video_audio_test_requested(u32 link)
615 u8 video_audio_test = (DP_TEST_LINK_VIDEO_PATTERN |
616 DP_TEST_LINK_AUDIO_PATTERN |
617 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
619 return ((link & video_audio_test) &&
620 !(link & ~video_audio_test));
624 * dp_link_parse_request() - parses link request parameters from sink
625 * @link: Display Port Driver data
627 * Parses the DPCD to check if an automated link is requested (Byte 0x201),
628 * and what type of link automation is being requested (Byte 0x218).
630 static int dp_link_parse_request(struct dp_link_private *link)
637 * Read the device service IRQ vector (Byte 0x201) to determine
638 * whether an automated link has been requested by the sink.
640 rlen = drm_dp_dpcd_readb(link->aux,
641 DP_DEVICE_SERVICE_IRQ_VECTOR, &data);
643 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
647 drm_dbg_dp(link->drm_dev, "device service irq vector = 0x%x\n", data);
649 if (!(data & DP_AUTOMATED_TEST_REQUEST)) {
650 drm_dbg_dp(link->drm_dev, "no test requested\n");
655 * Read the link request byte (Byte 0x218) to determine what type
656 * of automated link has been requested by the sink.
658 rlen = drm_dp_dpcd_readb(link->aux, DP_TEST_REQUEST, &data);
660 DRM_ERROR("aux read failed. rlen=%zd\n", rlen);
664 if (!data || (data == DP_TEST_LINK_FAUX_PATTERN)) {
665 drm_dbg_dp(link->drm_dev, "link 0x%x not supported\n", data);
669 drm_dbg_dp(link->drm_dev, "Test:(0x%x) requested\n", data);
670 link->request.test_requested = data;
671 if (link->request.test_requested == DP_TEST_LINK_PHY_TEST_PATTERN) {
672 ret = dp_link_parse_phy_test_params(link);
675 ret = dp_link_parse_link_training_params(link);
680 if (link->request.test_requested == DP_TEST_LINK_TRAINING) {
681 ret = dp_link_parse_link_training_params(link);
686 if (dp_link_is_video_audio_test_requested(
687 link->request.test_requested)) {
688 ret = dp_link_parse_video_pattern_params(link);
692 ret = dp_link_parse_audio_pattern_params(link);
696 * Send a DP_TEST_ACK if all link parameters are valid, otherwise send
700 link->dp_link.test_response = DP_TEST_NAK;
702 if (link->request.test_requested != DP_TEST_LINK_EDID_READ)
703 link->dp_link.test_response = DP_TEST_ACK;
705 link->dp_link.test_response =
706 DP_TEST_EDID_CHECKSUM_WRITE;
713 * dp_link_parse_sink_count() - parses the sink count
714 * @dp_link: pointer to link module data
716 * Parses the DPCD to check if there is an update to the sink count
717 * (Byte 0x200), and whether all the sink devices connected have Content
718 * Protection enabled.
720 static int dp_link_parse_sink_count(struct dp_link *dp_link)
725 struct dp_link_private *link = container_of(dp_link,
726 struct dp_link_private, dp_link);
728 rlen = drm_dp_dpcd_readb(link->aux, DP_SINK_COUNT,
729 &link->dp_link.sink_count);
731 DRM_ERROR("sink count read failed. rlen=%zd\n", rlen);
735 cp_ready = link->dp_link.sink_count & DP_SINK_CP_READY;
737 link->dp_link.sink_count =
738 DP_GET_SINK_COUNT(link->dp_link.sink_count);
740 drm_dbg_dp(link->drm_dev, "sink_count = 0x%x, cp_ready = 0x%x\n",
741 link->dp_link.sink_count, cp_ready);
745 static int dp_link_parse_sink_status_field(struct dp_link_private *link)
749 link->prev_sink_count = link->dp_link.sink_count;
750 len = dp_link_parse_sink_count(&link->dp_link);
752 DRM_ERROR("DP parse sink count failed\n");
756 len = drm_dp_dpcd_read_link_status(link->aux,
758 if (len < DP_LINK_STATUS_SIZE) {
759 DRM_ERROR("DP link status read failed\n");
763 return dp_link_parse_request(link);
767 * dp_link_process_link_training_request() - processes new training requests
768 * @link: Display Port link data
770 * This function will handle new link training requests that are initiated by
771 * the sink. In particular, it will update the requested lane count and link
772 * rate, and then trigger the link retraining procedure.
774 * The function will return 0 if a link training request has been processed,
775 * otherwise it will return -EINVAL.
777 static int dp_link_process_link_training_request(struct dp_link_private *link)
779 if (link->request.test_requested != DP_TEST_LINK_TRAINING)
782 drm_dbg_dp(link->drm_dev,
783 "Test:0x%x link rate = 0x%x, lane count = 0x%x\n",
784 DP_TEST_LINK_TRAINING,
785 link->request.test_link_rate,
786 link->request.test_lane_count);
788 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
789 link->dp_link.link_params.rate =
790 drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
795 bool dp_link_send_test_response(struct dp_link *dp_link)
797 struct dp_link_private *link = NULL;
801 DRM_ERROR("invalid input\n");
805 link = container_of(dp_link, struct dp_link_private, dp_link);
807 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_RESPONSE,
808 dp_link->test_response);
813 int dp_link_psm_config(struct dp_link *dp_link,
814 struct dp_link_info *link_info, bool enable)
816 struct dp_link_private *link = NULL;
820 DRM_ERROR("invalid params\n");
824 link = container_of(dp_link, struct dp_link_private, dp_link);
826 mutex_lock(&link->psm_mutex);
828 ret = dp_aux_link_power_down(link->aux, link_info);
830 ret = dp_aux_link_power_up(link->aux, link_info);
833 DRM_ERROR("Failed to %s low power mode\n", enable ?
836 dp_link->psm_enabled = enable;
838 mutex_unlock(&link->psm_mutex);
842 bool dp_link_send_edid_checksum(struct dp_link *dp_link, u8 checksum)
844 struct dp_link_private *link = NULL;
848 DRM_ERROR("invalid input\n");
852 link = container_of(dp_link, struct dp_link_private, dp_link);
854 ret = drm_dp_dpcd_writeb(link->aux, DP_TEST_EDID_CHECKSUM,
859 static void dp_link_parse_vx_px(struct dp_link_private *link)
861 drm_dbg_dp(link->drm_dev, "vx: 0=%d, 1=%d, 2=%d, 3=%d\n",
862 drm_dp_get_adjust_request_voltage(link->link_status, 0),
863 drm_dp_get_adjust_request_voltage(link->link_status, 1),
864 drm_dp_get_adjust_request_voltage(link->link_status, 2),
865 drm_dp_get_adjust_request_voltage(link->link_status, 3));
867 drm_dbg_dp(link->drm_dev, "px: 0=%d, 1=%d, 2=%d, 3=%d\n",
868 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0),
869 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 1),
870 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 2),
871 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 3));
874 * Update the voltage and pre-emphasis levels as per DPCD request
877 drm_dbg_dp(link->drm_dev,
878 "Current: v_level = 0x%x, p_level = 0x%x\n",
879 link->dp_link.phy_params.v_level,
880 link->dp_link.phy_params.p_level);
881 link->dp_link.phy_params.v_level =
882 drm_dp_get_adjust_request_voltage(link->link_status, 0);
883 link->dp_link.phy_params.p_level =
884 drm_dp_get_adjust_request_pre_emphasis(link->link_status, 0);
886 link->dp_link.phy_params.p_level >>= DP_TRAIN_PRE_EMPHASIS_SHIFT;
888 drm_dbg_dp(link->drm_dev,
889 "Requested: v_level = 0x%x, p_level = 0x%x\n",
890 link->dp_link.phy_params.v_level,
891 link->dp_link.phy_params.p_level);
895 * dp_link_process_phy_test_pattern_request() - process new phy link requests
896 * @link: Display Port Driver data
898 * This function will handle new phy link pattern requests that are initiated
899 * by the sink. The function will return 0 if a phy link pattern has been
900 * processed, otherwise it will return -EINVAL.
902 static int dp_link_process_phy_test_pattern_request(
903 struct dp_link_private *link)
905 if (!(link->request.test_requested & DP_TEST_LINK_PHY_TEST_PATTERN)) {
906 drm_dbg_dp(link->drm_dev, "no phy test\n");
910 if (!is_link_rate_valid(link->request.test_link_rate) ||
911 !is_lane_count_valid(link->request.test_lane_count)) {
912 DRM_ERROR("Invalid: link rate = 0x%x,lane count = 0x%x\n",
913 link->request.test_link_rate,
914 link->request.test_lane_count);
918 drm_dbg_dp(link->drm_dev,
919 "Current: rate = 0x%x, lane count = 0x%x\n",
920 link->dp_link.link_params.rate,
921 link->dp_link.link_params.num_lanes);
923 drm_dbg_dp(link->drm_dev,
924 "Requested: rate = 0x%x, lane count = 0x%x\n",
925 link->request.test_link_rate,
926 link->request.test_lane_count);
928 link->dp_link.link_params.num_lanes = link->request.test_lane_count;
929 link->dp_link.link_params.rate =
930 drm_dp_bw_code_to_link_rate(link->request.test_link_rate);
932 dp_link_parse_vx_px(link);
937 static u8 get_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
939 return link_status[r - DP_LANE0_1_STATUS];
943 * dp_link_process_link_status_update() - processes link status updates
944 * @link: Display Port link module data
946 * This function will check for changes in the link status, e.g. clock
947 * recovery done on all lanes, and trigger link training if there is a
948 * failure/error on the link.
950 * The function will return 0 if the a link status update has been processed,
951 * otherwise it will return -EINVAL.
953 static int dp_link_process_link_status_update(struct dp_link_private *link)
955 bool channel_eq_done = drm_dp_channel_eq_ok(link->link_status,
956 link->dp_link.link_params.num_lanes);
958 bool clock_recovery_done = drm_dp_clock_recovery_ok(link->link_status,
959 link->dp_link.link_params.num_lanes);
961 drm_dbg_dp(link->drm_dev,
962 "channel_eq_done = %d, clock_recovery_done = %d\n",
963 channel_eq_done, clock_recovery_done);
965 if (channel_eq_done && clock_recovery_done)
973 * dp_link_process_ds_port_status_change() - process port status changes
974 * @link: Display Port Driver data
976 * This function will handle downstream port updates that are initiated by
977 * the sink. If the downstream port status has changed, the EDID is read via
980 * The function will return 0 if a downstream port update has been
981 * processed, otherwise it will return -EINVAL.
983 static int dp_link_process_ds_port_status_change(struct dp_link_private *link)
985 if (get_link_status(link->link_status, DP_LANE_ALIGN_STATUS_UPDATED) &
986 DP_DOWNSTREAM_PORT_STATUS_CHANGED)
989 if (link->prev_sink_count == link->dp_link.sink_count)
993 /* reset prev_sink_count */
994 link->prev_sink_count = link->dp_link.sink_count;
999 static bool dp_link_is_video_pattern_requested(struct dp_link_private *link)
1001 return (link->request.test_requested & DP_TEST_LINK_VIDEO_PATTERN)
1002 && !(link->request.test_requested &
1003 DP_TEST_LINK_AUDIO_DISABLED_VIDEO);
1006 static bool dp_link_is_audio_pattern_requested(struct dp_link_private *link)
1008 return (link->request.test_requested & DP_TEST_LINK_AUDIO_PATTERN);
1011 static void dp_link_reset_data(struct dp_link_private *link)
1013 link->request = (const struct dp_link_request){ 0 };
1014 link->dp_link.test_video = (const struct dp_link_test_video){ 0 };
1015 link->dp_link.test_video.test_bit_depth = DP_TEST_BIT_DEPTH_UNKNOWN;
1016 link->dp_link.test_audio = (const struct dp_link_test_audio){ 0 };
1017 link->dp_link.phy_params.phy_test_pattern_sel = 0;
1018 link->dp_link.sink_request = 0;
1019 link->dp_link.test_response = 0;
1023 * dp_link_process_request() - handle HPD IRQ transition to HIGH
1024 * @dp_link: pointer to link module data
1026 * This function will handle the HPD IRQ state transitions from LOW to HIGH
1027 * (including cases when there are back to back HPD IRQ HIGH) indicating
1028 * the start of a new link training request or sink status update.
1030 int dp_link_process_request(struct dp_link *dp_link)
1033 struct dp_link_private *link;
1036 DRM_ERROR("invalid input\n");
1040 link = container_of(dp_link, struct dp_link_private, dp_link);
1042 dp_link_reset_data(link);
1044 ret = dp_link_parse_sink_status_field(link);
1048 if (link->request.test_requested == DP_TEST_LINK_EDID_READ) {
1049 dp_link->sink_request |= DP_TEST_LINK_EDID_READ;
1050 } else if (!dp_link_process_ds_port_status_change(link)) {
1051 dp_link->sink_request |= DS_PORT_STATUS_CHANGED;
1052 } else if (!dp_link_process_link_training_request(link)) {
1053 dp_link->sink_request |= DP_TEST_LINK_TRAINING;
1054 } else if (!dp_link_process_phy_test_pattern_request(link)) {
1055 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;
1061 if (dp_link_is_video_pattern_requested(link)) {
1063 dp_link->sink_request |= DP_TEST_LINK_VIDEO_PATTERN;
1065 if (dp_link_is_audio_pattern_requested(link)) {
1066 dp_link->sink_request |= DP_TEST_LINK_AUDIO_PATTERN;
1072 drm_dbg_dp(link->drm_dev, "sink request=%#x",
1073 dp_link->sink_request);
1077 int dp_link_get_colorimetry_config(struct dp_link *dp_link)
1080 struct dp_link_private *link;
1083 DRM_ERROR("invalid input\n");
1087 link = container_of(dp_link, struct dp_link_private, dp_link);
1090 * Unless a video pattern CTS test is ongoing, use RGB_VESA
1091 * Only RGB_VESA and RGB_CEA supported for now
1093 if (dp_link_is_video_pattern_requested(link))
1094 cc = link->dp_link.test_video.test_dyn_range;
1096 cc = DP_TEST_DYNAMIC_RANGE_VESA;
1101 int dp_link_adjust_levels(struct dp_link *dp_link, u8 *link_status)
1104 int v_max = 0, p_max = 0;
1105 struct dp_link_private *link;
1108 DRM_ERROR("invalid input\n");
1112 link = container_of(dp_link, struct dp_link_private, dp_link);
1114 /* use the max level across lanes */
1115 for (i = 0; i < dp_link->link_params.num_lanes; i++) {
1116 u8 data_v = drm_dp_get_adjust_request_voltage(link_status, i);
1117 u8 data_p = drm_dp_get_adjust_request_pre_emphasis(link_status,
1119 drm_dbg_dp(link->drm_dev,
1120 "lane=%d req_vol_swing=%d req_pre_emphasis=%d\n",
1128 dp_link->phy_params.v_level = v_max >> DP_TRAIN_VOLTAGE_SWING_SHIFT;
1129 dp_link->phy_params.p_level = p_max >> DP_TRAIN_PRE_EMPHASIS_SHIFT;
1132 * Adjust the voltage swing and pre-emphasis level combination to within
1133 * the allowable range.
1135 if (dp_link->phy_params.v_level > DP_TRAIN_VOLTAGE_SWING_MAX) {
1136 drm_dbg_dp(link->drm_dev,
1137 "Requested vSwingLevel=%d, change to %d\n",
1138 dp_link->phy_params.v_level,
1139 DP_TRAIN_VOLTAGE_SWING_MAX);
1140 dp_link->phy_params.v_level = DP_TRAIN_VOLTAGE_SWING_MAX;
1143 if (dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_MAX) {
1144 drm_dbg_dp(link->drm_dev,
1145 "Requested preEmphasisLevel=%d, change to %d\n",
1146 dp_link->phy_params.p_level,
1147 DP_TRAIN_PRE_EMPHASIS_MAX);
1148 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_MAX;
1151 if ((dp_link->phy_params.p_level > DP_TRAIN_PRE_EMPHASIS_LVL_1)
1152 && (dp_link->phy_params.v_level ==
1153 DP_TRAIN_VOLTAGE_SWING_LVL_2)) {
1154 drm_dbg_dp(link->drm_dev,
1155 "Requested preEmphasisLevel=%d, change to %d\n",
1156 dp_link->phy_params.p_level,
1157 DP_TRAIN_PRE_EMPHASIS_LVL_1);
1158 dp_link->phy_params.p_level = DP_TRAIN_PRE_EMPHASIS_LVL_1;
1161 drm_dbg_dp(link->drm_dev, "adjusted: v_level=%d, p_level=%d\n",
1162 dp_link->phy_params.v_level, dp_link->phy_params.p_level);
1167 void dp_link_reset_phy_params_vx_px(struct dp_link *dp_link)
1169 dp_link->phy_params.v_level = 0;
1170 dp_link->phy_params.p_level = 0;
1173 u32 dp_link_get_test_bits_depth(struct dp_link *dp_link, u32 bpp)
1178 * Few simplistic rules and assumptions made here:
1179 * 1. Test bit depth is bit depth per color component
1180 * 2. Assume 3 color components
1184 tbd = DP_TEST_BIT_DEPTH_6;
1187 tbd = DP_TEST_BIT_DEPTH_8;
1190 tbd = DP_TEST_BIT_DEPTH_10;
1193 tbd = DP_TEST_BIT_DEPTH_UNKNOWN;
1197 if (tbd != DP_TEST_BIT_DEPTH_UNKNOWN)
1198 tbd = (tbd >> DP_TEST_BIT_DEPTH_SHIFT);
1203 struct dp_link *dp_link_get(struct device *dev, struct drm_dp_aux *aux)
1205 struct dp_link_private *link;
1206 struct dp_link *dp_link;
1209 DRM_ERROR("invalid input\n");
1210 return ERR_PTR(-EINVAL);
1213 link = devm_kzalloc(dev, sizeof(*link), GFP_KERNEL);
1215 return ERR_PTR(-ENOMEM);
1220 mutex_init(&link->psm_mutex);
1221 dp_link = &link->dp_link;