xref: /freebsd-src/sys/dev/drm2/drm_dp_helper.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1*592ffb21SWarner Losh /*
2*592ffb21SWarner Losh  * Copyright © 2009 Keith Packard
3*592ffb21SWarner Losh  *
4*592ffb21SWarner Losh  * Permission to use, copy, modify, distribute, and sell this software and its
5*592ffb21SWarner Losh  * documentation for any purpose is hereby granted without fee, provided that
6*592ffb21SWarner Losh  * the above copyright notice appear in all copies and that both that copyright
7*592ffb21SWarner Losh  * notice and this permission notice appear in supporting documentation, and
8*592ffb21SWarner Losh  * that the name of the copyright holders not be used in advertising or
9*592ffb21SWarner Losh  * publicity pertaining to distribution of the software without specific,
10*592ffb21SWarner Losh  * written prior permission.  The copyright holders make no representations
11*592ffb21SWarner Losh  * about the suitability of this software for any purpose.  It is provided "as
12*592ffb21SWarner Losh  * is" without express or implied warranty.
13*592ffb21SWarner Losh  *
14*592ffb21SWarner Losh  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*592ffb21SWarner Losh  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*592ffb21SWarner Losh  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*592ffb21SWarner Losh  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*592ffb21SWarner Losh  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*592ffb21SWarner Losh  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*592ffb21SWarner Losh  * OF THIS SOFTWARE.
21*592ffb21SWarner Losh  */
22*592ffb21SWarner Losh 
23*592ffb21SWarner Losh #include <sys/cdefs.h>
24*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
25*592ffb21SWarner Losh #include <dev/drm2/drm_dp_helper.h>
26*592ffb21SWarner Losh 
27*592ffb21SWarner Losh /**
28*592ffb21SWarner Losh  * DOC: dp helpers
29*592ffb21SWarner Losh  *
30*592ffb21SWarner Losh  * These functions contain some common logic and helpers at various abstraction
31*592ffb21SWarner Losh  * levels to deal with Display Port sink devices and related things like DP aux
32*592ffb21SWarner Losh  * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
33*592ffb21SWarner Losh  * blocks, ...
34*592ffb21SWarner Losh  */
35*592ffb21SWarner Losh 
36*592ffb21SWarner Losh /* Helpers for DP link training */
dp_link_status(u8 link_status[DP_LINK_STATUS_SIZE],int r)37*592ffb21SWarner Losh static u8 dp_link_status(u8 link_status[DP_LINK_STATUS_SIZE], int r)
38*592ffb21SWarner Losh {
39*592ffb21SWarner Losh 	return link_status[r - DP_LANE0_1_STATUS];
40*592ffb21SWarner Losh }
41*592ffb21SWarner Losh 
dp_get_lane_status(u8 link_status[DP_LINK_STATUS_SIZE],int lane)42*592ffb21SWarner Losh static u8 dp_get_lane_status(u8 link_status[DP_LINK_STATUS_SIZE],
43*592ffb21SWarner Losh 			     int lane)
44*592ffb21SWarner Losh {
45*592ffb21SWarner Losh 	int i = DP_LANE0_1_STATUS + (lane >> 1);
46*592ffb21SWarner Losh 	int s = (lane & 1) * 4;
47*592ffb21SWarner Losh 	u8 l = dp_link_status(link_status, i);
48*592ffb21SWarner Losh 	return (l >> s) & 0xf;
49*592ffb21SWarner Losh }
50*592ffb21SWarner Losh 
drm_dp_channel_eq_ok(u8 link_status[DP_LINK_STATUS_SIZE],int lane_count)51*592ffb21SWarner Losh bool drm_dp_channel_eq_ok(u8 link_status[DP_LINK_STATUS_SIZE],
52*592ffb21SWarner Losh 			  int lane_count)
53*592ffb21SWarner Losh {
54*592ffb21SWarner Losh 	u8 lane_align;
55*592ffb21SWarner Losh 	u8 lane_status;
56*592ffb21SWarner Losh 	int lane;
57*592ffb21SWarner Losh 
58*592ffb21SWarner Losh 	lane_align = dp_link_status(link_status,
59*592ffb21SWarner Losh 				    DP_LANE_ALIGN_STATUS_UPDATED);
60*592ffb21SWarner Losh 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
61*592ffb21SWarner Losh 		return false;
62*592ffb21SWarner Losh 	for (lane = 0; lane < lane_count; lane++) {
63*592ffb21SWarner Losh 		lane_status = dp_get_lane_status(link_status, lane);
64*592ffb21SWarner Losh 		if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
65*592ffb21SWarner Losh 			return false;
66*592ffb21SWarner Losh 	}
67*592ffb21SWarner Losh 	return true;
68*592ffb21SWarner Losh }
69*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_channel_eq_ok);
70*592ffb21SWarner Losh 
drm_dp_clock_recovery_ok(u8 link_status[DP_LINK_STATUS_SIZE],int lane_count)71*592ffb21SWarner Losh bool drm_dp_clock_recovery_ok(u8 link_status[DP_LINK_STATUS_SIZE],
72*592ffb21SWarner Losh 			      int lane_count)
73*592ffb21SWarner Losh {
74*592ffb21SWarner Losh 	int lane;
75*592ffb21SWarner Losh 	u8 lane_status;
76*592ffb21SWarner Losh 
77*592ffb21SWarner Losh 	for (lane = 0; lane < lane_count; lane++) {
78*592ffb21SWarner Losh 		lane_status = dp_get_lane_status(link_status, lane);
79*592ffb21SWarner Losh 		if ((lane_status & DP_LANE_CR_DONE) == 0)
80*592ffb21SWarner Losh 			return false;
81*592ffb21SWarner Losh 	}
82*592ffb21SWarner Losh 	return true;
83*592ffb21SWarner Losh }
84*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
85*592ffb21SWarner Losh 
drm_dp_get_adjust_request_voltage(u8 link_status[DP_LINK_STATUS_SIZE],int lane)86*592ffb21SWarner Losh u8 drm_dp_get_adjust_request_voltage(u8 link_status[DP_LINK_STATUS_SIZE],
87*592ffb21SWarner Losh 				     int lane)
88*592ffb21SWarner Losh {
89*592ffb21SWarner Losh 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
90*592ffb21SWarner Losh 	int s = ((lane & 1) ?
91*592ffb21SWarner Losh 		 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
92*592ffb21SWarner Losh 		 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
93*592ffb21SWarner Losh 	u8 l = dp_link_status(link_status, i);
94*592ffb21SWarner Losh 
95*592ffb21SWarner Losh 	return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
96*592ffb21SWarner Losh }
97*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
98*592ffb21SWarner Losh 
drm_dp_get_adjust_request_pre_emphasis(u8 link_status[DP_LINK_STATUS_SIZE],int lane)99*592ffb21SWarner Losh u8 drm_dp_get_adjust_request_pre_emphasis(u8 link_status[DP_LINK_STATUS_SIZE],
100*592ffb21SWarner Losh 					  int lane)
101*592ffb21SWarner Losh {
102*592ffb21SWarner Losh 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
103*592ffb21SWarner Losh 	int s = ((lane & 1) ?
104*592ffb21SWarner Losh 		 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
105*592ffb21SWarner Losh 		 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
106*592ffb21SWarner Losh 	u8 l = dp_link_status(link_status, i);
107*592ffb21SWarner Losh 
108*592ffb21SWarner Losh 	return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
109*592ffb21SWarner Losh }
110*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
111*592ffb21SWarner Losh 
drm_dp_link_train_clock_recovery_delay(u8 dpcd[DP_RECEIVER_CAP_SIZE])112*592ffb21SWarner Losh void drm_dp_link_train_clock_recovery_delay(u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
113*592ffb21SWarner Losh 	if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
114*592ffb21SWarner Losh 		udelay(100);
115*592ffb21SWarner Losh 	else
116*592ffb21SWarner Losh 		mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
117*592ffb21SWarner Losh }
118*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
119*592ffb21SWarner Losh 
drm_dp_link_train_channel_eq_delay(u8 dpcd[DP_RECEIVER_CAP_SIZE])120*592ffb21SWarner Losh void drm_dp_link_train_channel_eq_delay(u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
121*592ffb21SWarner Losh 	if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
122*592ffb21SWarner Losh 		udelay(400);
123*592ffb21SWarner Losh 	else
124*592ffb21SWarner Losh 		mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
125*592ffb21SWarner Losh }
126*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
127*592ffb21SWarner Losh 
drm_dp_link_rate_to_bw_code(int link_rate)128*592ffb21SWarner Losh u8 drm_dp_link_rate_to_bw_code(int link_rate)
129*592ffb21SWarner Losh {
130*592ffb21SWarner Losh 	switch (link_rate) {
131*592ffb21SWarner Losh 	case 162000:
132*592ffb21SWarner Losh 	default:
133*592ffb21SWarner Losh 		return DP_LINK_BW_1_62;
134*592ffb21SWarner Losh 	case 270000:
135*592ffb21SWarner Losh 		return DP_LINK_BW_2_7;
136*592ffb21SWarner Losh 	case 540000:
137*592ffb21SWarner Losh 		return DP_LINK_BW_5_4;
138*592ffb21SWarner Losh 	}
139*592ffb21SWarner Losh }
140*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
141*592ffb21SWarner Losh 
drm_dp_bw_code_to_link_rate(u8 link_bw)142*592ffb21SWarner Losh int drm_dp_bw_code_to_link_rate(u8 link_bw)
143*592ffb21SWarner Losh {
144*592ffb21SWarner Losh 	switch (link_bw) {
145*592ffb21SWarner Losh 	case DP_LINK_BW_1_62:
146*592ffb21SWarner Losh 	default:
147*592ffb21SWarner Losh 		return 162000;
148*592ffb21SWarner Losh 	case DP_LINK_BW_2_7:
149*592ffb21SWarner Losh 		return 270000;
150*592ffb21SWarner Losh 	case DP_LINK_BW_5_4:
151*592ffb21SWarner Losh 		return 540000;
152*592ffb21SWarner Losh 	}
153*592ffb21SWarner Losh }
154*592ffb21SWarner Losh EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
155