xref: /linux/drivers/gpu/drm/display/drm_dp_helper.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 /*
2  * Copyright © 2009 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <linux/backlight.h>
24 #include <linux/delay.h>
25 #include <linux/dynamic_debug.h>
26 #include <linux/errno.h>
27 #include <linux/export.h>
28 #include <linux/i2c.h>
29 #include <linux/init.h>
30 #include <linux/iopoll.h>
31 #include <linux/kernel.h>
32 #include <linux/minmax.h>
33 #include <linux/module.h>
34 #include <linux/sched.h>
35 #include <linux/seq_file.h>
36 #include <linux/string_helpers.h>
37 
38 #include <drm/display/drm_dp_helper.h>
39 #include <drm/display/drm_dp_mst_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_fixed.h>
42 #include <drm/drm_print.h>
43 #include <drm/drm_vblank.h>
44 #include <drm/drm_panel.h>
45 
46 #include "drm_dp_helper_internal.h"
47 
48 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
49 			"DRM_UT_CORE",
50 			"DRM_UT_DRIVER",
51 			"DRM_UT_KMS",
52 			"DRM_UT_PRIME",
53 			"DRM_UT_ATOMIC",
54 			"DRM_UT_VBL",
55 			"DRM_UT_STATE",
56 			"DRM_UT_LEASE",
57 			"DRM_UT_DP",
58 			"DRM_UT_DRMRES");
59 
60 struct dp_aux_backlight {
61 	struct backlight_device *base;
62 	struct drm_dp_aux *aux;
63 	struct drm_edp_backlight_info info;
64 	bool enabled;
65 };
66 
67 /**
68  * DOC: dp helpers
69  *
70  * These functions contain some common logic and helpers at various abstraction
71  * levels to deal with Display Port sink devices and related things like DP aux
72  * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
73  * blocks, ...
74  */
75 
76 /* Helpers for DP link training */
77 static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
78 {
79 	return link_status[r - DP_LANE0_1_STATUS];
80 }
81 
82 static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
83 			     int lane)
84 {
85 	int i = DP_LANE0_1_STATUS + (lane >> 1);
86 	int s = (lane & 1) * 4;
87 	u8 l = dp_link_status(link_status, i);
88 
89 	return (l >> s) & 0xf;
90 }
91 
92 bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
93 			  int lane_count)
94 {
95 	u8 lane_align;
96 	u8 lane_status;
97 	int lane;
98 
99 	lane_align = dp_link_status(link_status,
100 				    DP_LANE_ALIGN_STATUS_UPDATED);
101 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
102 		return false;
103 	for (lane = 0; lane < lane_count; lane++) {
104 		lane_status = dp_get_lane_status(link_status, lane);
105 		if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
106 			return false;
107 	}
108 	return true;
109 }
110 EXPORT_SYMBOL(drm_dp_channel_eq_ok);
111 
112 bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
113 			      int lane_count)
114 {
115 	int lane;
116 	u8 lane_status;
117 
118 	for (lane = 0; lane < lane_count; lane++) {
119 		lane_status = dp_get_lane_status(link_status, lane);
120 		if ((lane_status & DP_LANE_CR_DONE) == 0)
121 			return false;
122 	}
123 	return true;
124 }
125 EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
126 
127 bool drm_dp_post_lt_adj_req_in_progress(const u8 link_status[DP_LINK_STATUS_SIZE])
128 {
129 	u8 lane_align = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
130 
131 	return lane_align & DP_POST_LT_ADJ_REQ_IN_PROGRESS;
132 }
133 EXPORT_SYMBOL(drm_dp_post_lt_adj_req_in_progress);
134 
135 u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
136 				     int lane)
137 {
138 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
139 	int s = ((lane & 1) ?
140 		 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
141 		 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
142 	u8 l = dp_link_status(link_status, i);
143 
144 	return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
145 }
146 EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
147 
148 u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
149 					  int lane)
150 {
151 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
152 	int s = ((lane & 1) ?
153 		 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
154 		 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
155 	u8 l = dp_link_status(link_status, i);
156 
157 	return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
158 }
159 EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
160 
161 /* DP 2.0 128b/132b */
162 u8 drm_dp_get_adjust_tx_ffe_preset(const u8 link_status[DP_LINK_STATUS_SIZE],
163 				   int lane)
164 {
165 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
166 	int s = ((lane & 1) ?
167 		 DP_ADJUST_TX_FFE_PRESET_LANE1_SHIFT :
168 		 DP_ADJUST_TX_FFE_PRESET_LANE0_SHIFT);
169 	u8 l = dp_link_status(link_status, i);
170 
171 	return (l >> s) & 0xf;
172 }
173 EXPORT_SYMBOL(drm_dp_get_adjust_tx_ffe_preset);
174 
175 /* DP 2.0 errata for 128b/132b */
176 bool drm_dp_128b132b_lane_channel_eq_done(const u8 link_status[DP_LINK_STATUS_SIZE],
177 					  int lane_count)
178 {
179 	u8 lane_align, lane_status;
180 	int lane;
181 
182 	lane_align = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
183 	if (!(lane_align & DP_INTERLANE_ALIGN_DONE))
184 		return false;
185 
186 	for (lane = 0; lane < lane_count; lane++) {
187 		lane_status = dp_get_lane_status(link_status, lane);
188 		if (!(lane_status & DP_LANE_CHANNEL_EQ_DONE))
189 			return false;
190 	}
191 	return true;
192 }
193 EXPORT_SYMBOL(drm_dp_128b132b_lane_channel_eq_done);
194 
195 /* DP 2.0 errata for 128b/132b */
196 bool drm_dp_128b132b_lane_symbol_locked(const u8 link_status[DP_LINK_STATUS_SIZE],
197 					int lane_count)
198 {
199 	u8 lane_status;
200 	int lane;
201 
202 	for (lane = 0; lane < lane_count; lane++) {
203 		lane_status = dp_get_lane_status(link_status, lane);
204 		if (!(lane_status & DP_LANE_SYMBOL_LOCKED))
205 			return false;
206 	}
207 	return true;
208 }
209 EXPORT_SYMBOL(drm_dp_128b132b_lane_symbol_locked);
210 
211 /* DP 2.0 errata for 128b/132b */
212 bool drm_dp_128b132b_eq_interlane_align_done(const u8 link_status[DP_LINK_STATUS_SIZE])
213 {
214 	u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
215 
216 	return status & DP_128B132B_DPRX_EQ_INTERLANE_ALIGN_DONE;
217 }
218 EXPORT_SYMBOL(drm_dp_128b132b_eq_interlane_align_done);
219 
220 /* DP 2.0 errata for 128b/132b */
221 bool drm_dp_128b132b_cds_interlane_align_done(const u8 link_status[DP_LINK_STATUS_SIZE])
222 {
223 	u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
224 
225 	return status & DP_128B132B_DPRX_CDS_INTERLANE_ALIGN_DONE;
226 }
227 EXPORT_SYMBOL(drm_dp_128b132b_cds_interlane_align_done);
228 
229 /* DP 2.0 errata for 128b/132b */
230 bool drm_dp_128b132b_link_training_failed(const u8 link_status[DP_LINK_STATUS_SIZE])
231 {
232 	u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);
233 
234 	return status & DP_128B132B_LT_FAILED;
235 }
236 EXPORT_SYMBOL(drm_dp_128b132b_link_training_failed);
237 
238 static int __8b10b_clock_recovery_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
239 {
240 	if (rd_interval > 4)
241 		drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",
242 			    aux->name, rd_interval);
243 
244 	if (rd_interval == 0)
245 		return 100;
246 
247 	return rd_interval * 4 * USEC_PER_MSEC;
248 }
249 
250 static int __8b10b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
251 {
252 	if (rd_interval > 4)
253 		drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",
254 			    aux->name, rd_interval);
255 
256 	if (rd_interval == 0)
257 		return 400;
258 
259 	return rd_interval * 4 * USEC_PER_MSEC;
260 }
261 
262 static int __128b132b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
263 {
264 	switch (rd_interval) {
265 	default:
266 		drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x\n",
267 			    aux->name, rd_interval);
268 		fallthrough;
269 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_400_US:
270 		return 400;
271 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_4_MS:
272 		return 4000;
273 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_8_MS:
274 		return 8000;
275 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_12_MS:
276 		return 12000;
277 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_16_MS:
278 		return 16000;
279 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_32_MS:
280 		return 32000;
281 	case DP_128B132B_TRAINING_AUX_RD_INTERVAL_64_MS:
282 		return 64000;
283 	}
284 }
285 
286 /*
287  * The link training delays are different for:
288  *
289  *  - Clock recovery vs. channel equalization
290  *  - DPRX vs. LTTPR
291  *  - 128b/132b vs. 8b/10b
292  *  - DPCD rev 1.3 vs. later
293  *
294  * Get the correct delay in us, reading DPCD if necessary.
295  */
296 static int __read_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
297 			enum drm_dp_phy dp_phy, bool uhbr, bool cr)
298 {
299 	int (*parse)(const struct drm_dp_aux *aux, u8 rd_interval);
300 	unsigned int offset;
301 	u8 rd_interval, mask;
302 
303 	if (dp_phy == DP_PHY_DPRX) {
304 		if (uhbr) {
305 			if (cr)
306 				return 100;
307 
308 			offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL;
309 			mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
310 			parse = __128b132b_channel_eq_delay_us;
311 		} else {
312 			if (cr && dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
313 				return 100;
314 
315 			offset = DP_TRAINING_AUX_RD_INTERVAL;
316 			mask = DP_TRAINING_AUX_RD_MASK;
317 			if (cr)
318 				parse = __8b10b_clock_recovery_delay_us;
319 			else
320 				parse = __8b10b_channel_eq_delay_us;
321 		}
322 	} else {
323 		if (uhbr) {
324 			offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);
325 			mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
326 			parse = __128b132b_channel_eq_delay_us;
327 		} else {
328 			if (cr)
329 				return 100;
330 
331 			offset = DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);
332 			mask = DP_TRAINING_AUX_RD_MASK;
333 			parse = __8b10b_channel_eq_delay_us;
334 		}
335 	}
336 
337 	if (offset < DP_RECEIVER_CAP_SIZE) {
338 		rd_interval = dpcd[offset];
339 	} else {
340 		if (drm_dp_dpcd_read_byte(aux, offset, &rd_interval) < 0) {
341 			drm_dbg_kms(aux->drm_dev, "%s: failed rd interval read\n",
342 				    aux->name);
343 			/* arbitrary default delay */
344 			return 400;
345 		}
346 	}
347 
348 	return parse(aux, rd_interval & mask);
349 }
350 
351 int drm_dp_read_clock_recovery_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
352 				     enum drm_dp_phy dp_phy, bool uhbr)
353 {
354 	return __read_delay(aux, dpcd, dp_phy, uhbr, true);
355 }
356 EXPORT_SYMBOL(drm_dp_read_clock_recovery_delay);
357 
358 int drm_dp_read_channel_eq_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
359 				 enum drm_dp_phy dp_phy, bool uhbr)
360 {
361 	return __read_delay(aux, dpcd, dp_phy, uhbr, false);
362 }
363 EXPORT_SYMBOL(drm_dp_read_channel_eq_delay);
364 
365 /* Per DP 2.0 Errata */
366 int drm_dp_128b132b_read_aux_rd_interval(struct drm_dp_aux *aux)
367 {
368 	int unit;
369 	u8 val;
370 
371 	if (drm_dp_dpcd_read_byte(aux, DP_128B132B_TRAINING_AUX_RD_INTERVAL, &val) < 0) {
372 		drm_err(aux->drm_dev, "%s: failed rd interval read\n",
373 			aux->name);
374 		/* default to max */
375 		val = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
376 	}
377 
378 	unit = (val & DP_128B132B_TRAINING_AUX_RD_INTERVAL_1MS_UNIT) ? 1 : 2;
379 	val &= DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
380 
381 	return (val + 1) * unit * 1000;
382 }
383 EXPORT_SYMBOL(drm_dp_128b132b_read_aux_rd_interval);
384 
385 void drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux *aux,
386 					    const u8 dpcd[DP_RECEIVER_CAP_SIZE])
387 {
388 	u8 rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
389 		DP_TRAINING_AUX_RD_MASK;
390 	int delay_us;
391 
392 	if (dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
393 		delay_us = 100;
394 	else
395 		delay_us = __8b10b_clock_recovery_delay_us(aux, rd_interval);
396 
397 	usleep_range(delay_us, delay_us * 2);
398 }
399 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
400 
401 static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
402 						 u8 rd_interval)
403 {
404 	int delay_us = __8b10b_channel_eq_delay_us(aux, rd_interval);
405 
406 	usleep_range(delay_us, delay_us * 2);
407 }
408 
409 void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
410 					const u8 dpcd[DP_RECEIVER_CAP_SIZE])
411 {
412 	__drm_dp_link_train_channel_eq_delay(aux,
413 					     dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
414 					     DP_TRAINING_AUX_RD_MASK);
415 }
416 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
417 
418 /**
419  * drm_dp_phy_name() - Get the name of the given DP PHY
420  * @dp_phy: The DP PHY identifier
421  *
422  * Given the @dp_phy, get a user friendly name of the DP PHY, either "DPRX" or
423  * "LTTPR <N>", or "<INVALID DP PHY>" on errors. The returned string is always
424  * non-NULL and valid.
425  *
426  * Returns: Name of the DP PHY.
427  */
428 const char *drm_dp_phy_name(enum drm_dp_phy dp_phy)
429 {
430 	static const char * const phy_names[] = {
431 		[DP_PHY_DPRX] = "DPRX",
432 		[DP_PHY_LTTPR1] = "LTTPR 1",
433 		[DP_PHY_LTTPR2] = "LTTPR 2",
434 		[DP_PHY_LTTPR3] = "LTTPR 3",
435 		[DP_PHY_LTTPR4] = "LTTPR 4",
436 		[DP_PHY_LTTPR5] = "LTTPR 5",
437 		[DP_PHY_LTTPR6] = "LTTPR 6",
438 		[DP_PHY_LTTPR7] = "LTTPR 7",
439 		[DP_PHY_LTTPR8] = "LTTPR 8",
440 	};
441 
442 	if (dp_phy < 0 || dp_phy >= ARRAY_SIZE(phy_names) ||
443 	    WARN_ON(!phy_names[dp_phy]))
444 		return "<INVALID DP PHY>";
445 
446 	return phy_names[dp_phy];
447 }
448 EXPORT_SYMBOL(drm_dp_phy_name);
449 
450 void drm_dp_lttpr_link_train_clock_recovery_delay(void)
451 {
452 	usleep_range(100, 200);
453 }
454 EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
455 
456 static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
457 {
458 	return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
459 }
460 
461 void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
462 					      const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
463 {
464 	u8 interval = dp_lttpr_phy_cap(phy_cap,
465 				       DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
466 		      DP_TRAINING_AUX_RD_MASK;
467 
468 	__drm_dp_link_train_channel_eq_delay(aux, interval);
469 }
470 EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
471 
472 /**
473  * drm_dp_lttpr_wake_timeout_setup() - Grant extended time for sink to wake up
474  * @aux: The DP AUX channel to use
475  * @transparent_mode: This is true if lttpr is in transparent mode
476  *
477  * This function checks if the sink needs any extended wake time, if it does
478  * it grants this request. Post this setup the source device can keep trying
479  * the Aux transaction till the granted wake timeout.
480  * If this function is not called all Aux transactions are expected to take
481  * a default of 1ms before they throw an error.
482  */
483 void drm_dp_lttpr_wake_timeout_setup(struct drm_dp_aux *aux, bool transparent_mode)
484 {
485 	u8 val = 1;
486 	int ret;
487 
488 	if (transparent_mode) {
489 		static const u8 timeout_mapping[] = {
490 			[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_1_MS] = 1,
491 			[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_20_MS] = 20,
492 			[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_40_MS] = 40,
493 			[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_60_MS] = 60,
494 			[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_80_MS] = 80,
495 			[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_100_MS] = 100,
496 		};
497 
498 		ret = drm_dp_dpcd_readb(aux, DP_EXTENDED_DPRX_SLEEP_WAKE_TIMEOUT_REQUEST, &val);
499 		if (ret != 1) {
500 			drm_dbg_kms(aux->drm_dev,
501 				    "Failed to read Extended sleep wake timeout request\n");
502 			return;
503 		}
504 
505 		val = (val < sizeof(timeout_mapping) && timeout_mapping[val]) ?
506 			timeout_mapping[val] : 1;
507 
508 		if (val > 1)
509 			drm_dp_dpcd_writeb(aux,
510 					   DP_EXTENDED_DPRX_SLEEP_WAKE_TIMEOUT_GRANT,
511 					   DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_GRANTED);
512 	} else {
513 		ret = drm_dp_dpcd_readb(aux, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT, &val);
514 		if (ret != 1) {
515 			drm_dbg_kms(aux->drm_dev,
516 				    "Failed to read Extended sleep wake timeout request\n");
517 			return;
518 		}
519 
520 		val = (val & DP_EXTENDED_WAKE_TIMEOUT_REQUEST_MASK) ?
521 			(val & DP_EXTENDED_WAKE_TIMEOUT_REQUEST_MASK) * 10 : 1;
522 
523 		if (val > 1)
524 			drm_dp_dpcd_writeb(aux, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT,
525 					   DP_EXTENDED_WAKE_TIMEOUT_GRANT);
526 	}
527 }
528 EXPORT_SYMBOL(drm_dp_lttpr_wake_timeout_setup);
529 
530 u8 drm_dp_link_rate_to_bw_code(int link_rate)
531 {
532 	switch (link_rate) {
533 	case 1000000:
534 		return DP_LINK_BW_10;
535 	case 1350000:
536 		return DP_LINK_BW_13_5;
537 	case 2000000:
538 		return DP_LINK_BW_20;
539 	default:
540 		/* Spec says link_bw = link_rate / 0.27Gbps */
541 		return link_rate / 27000;
542 	}
543 }
544 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
545 
546 int drm_dp_bw_code_to_link_rate(u8 link_bw)
547 {
548 	switch (link_bw) {
549 	case DP_LINK_BW_10:
550 		return 1000000;
551 	case DP_LINK_BW_13_5:
552 		return 1350000;
553 	case DP_LINK_BW_20:
554 		return 2000000;
555 	default:
556 		/* Spec says link_rate = link_bw * 0.27Gbps */
557 		return link_bw * 27000;
558 	}
559 }
560 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
561 
562 #define AUX_RETRY_INTERVAL 500 /* us */
563 
564 static inline void
565 drm_dp_dump_access(const struct drm_dp_aux *aux,
566 		   u8 request, uint offset, void *buffer, int ret)
567 {
568 	const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
569 
570 	if (ret > 0)
571 		drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
572 			   aux->name, offset, arrow, ret, min(ret, 20), buffer);
573 	else
574 		drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",
575 			   aux->name, offset, arrow, ret);
576 }
577 
578 /**
579  * DOC: dp helpers
580  *
581  * The DisplayPort AUX channel is an abstraction to allow generic, driver-
582  * independent access to AUX functionality. Drivers can take advantage of
583  * this by filling in the fields of the drm_dp_aux structure.
584  *
585  * Transactions are described using a hardware-independent drm_dp_aux_msg
586  * structure, which is passed into a driver's .transfer() implementation.
587  * Both native and I2C-over-AUX transactions are supported.
588  */
589 
590 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
591 			      unsigned int offset, void *buffer, size_t size)
592 {
593 	struct drm_dp_aux_msg msg;
594 	unsigned int retry, native_reply;
595 	int err = 0, ret = 0;
596 
597 	memset(&msg, 0, sizeof(msg));
598 	msg.address = offset;
599 	msg.request = request;
600 	msg.buffer = buffer;
601 	msg.size = size;
602 
603 	mutex_lock(&aux->hw_mutex);
604 
605 	/*
606 	 * If the device attached to the aux bus is powered down then there's
607 	 * no reason to attempt a transfer. Error out immediately.
608 	 */
609 	if (aux->powered_down) {
610 		ret = -EBUSY;
611 		goto unlock;
612 	}
613 
614 	/*
615 	 * The specification doesn't give any recommendation on how often to
616 	 * retry native transactions. We used to retry 7 times like for
617 	 * aux i2c transactions but real world devices this wasn't
618 	 * sufficient, bump to 32 which makes Dell 4k monitors happier.
619 	 */
620 	for (retry = 0; retry < 32; retry++) {
621 		if (ret != 0 && ret != -ETIMEDOUT) {
622 			usleep_range(AUX_RETRY_INTERVAL,
623 				     AUX_RETRY_INTERVAL + 100);
624 		}
625 
626 		ret = aux->transfer(aux, &msg);
627 		if (ret >= 0) {
628 			native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
629 			if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
630 				if (ret == size)
631 					goto unlock;
632 
633 				ret = -EPROTO;
634 			} else
635 				ret = -EIO;
636 		}
637 
638 		/*
639 		 * We want the error we return to be the error we received on
640 		 * the first transaction, since we may get a different error the
641 		 * next time we retry
642 		 */
643 		if (!err)
644 			err = ret;
645 	}
646 
647 	drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",
648 		    aux->name, err);
649 	ret = err;
650 
651 unlock:
652 	mutex_unlock(&aux->hw_mutex);
653 	return ret;
654 }
655 
656 /**
657  * drm_dp_dpcd_probe() - probe a given DPCD address with a 1-byte read access
658  * @aux: DisplayPort AUX channel (SST)
659  * @offset: address of the register to probe
660  *
661  * Probe the provided DPCD address by reading 1 byte from it. The function can
662  * be used to trigger some side-effect the read access has, like waking up the
663  * sink, without the need for the read-out value.
664  *
665  * Returns 0 if the read access suceeded, or a negative error code on failure.
666  */
667 int drm_dp_dpcd_probe(struct drm_dp_aux *aux, unsigned int offset)
668 {
669 	u8 buffer;
670 	int ret;
671 
672 	ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, &buffer, 1);
673 	WARN_ON(ret == 0);
674 
675 	drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, &buffer, ret);
676 
677 	return ret < 0 ? ret : 0;
678 }
679 EXPORT_SYMBOL(drm_dp_dpcd_probe);
680 
681 /**
682  * drm_dp_dpcd_set_powered() - Set whether the DP device is powered
683  * @aux: DisplayPort AUX channel; for convenience it's OK to pass NULL here
684  *       and the function will be a no-op.
685  * @powered: true if powered; false if not
686  *
687  * If the endpoint device on the DP AUX bus is known to be powered down
688  * then this function can be called to make future transfers fail immediately
689  * instead of needing to time out.
690  *
691  * If this function is never called then a device defaults to being powered.
692  */
693 void drm_dp_dpcd_set_powered(struct drm_dp_aux *aux, bool powered)
694 {
695 	if (!aux)
696 		return;
697 
698 	mutex_lock(&aux->hw_mutex);
699 	aux->powered_down = !powered;
700 	mutex_unlock(&aux->hw_mutex);
701 }
702 EXPORT_SYMBOL(drm_dp_dpcd_set_powered);
703 
704 /**
705  * drm_dp_dpcd_set_probe() - Set whether a probing before DPCD access is done
706  * @aux: DisplayPort AUX channel
707  * @enable: Enable the probing if required
708  */
709 void drm_dp_dpcd_set_probe(struct drm_dp_aux *aux, bool enable)
710 {
711 	WRITE_ONCE(aux->dpcd_probe_disabled, !enable);
712 }
713 EXPORT_SYMBOL(drm_dp_dpcd_set_probe);
714 
715 static bool dpcd_access_needs_probe(struct drm_dp_aux *aux)
716 {
717 	/*
718 	 * HP ZR24w corrupts the first DPCD access after entering power save
719 	 * mode. Eg. on a read, the entire buffer will be filled with the same
720 	 * byte. Do a throw away read to avoid corrupting anything we care
721 	 * about. Afterwards things will work correctly until the monitor
722 	 * gets woken up and subsequently re-enters power save mode.
723 	 *
724 	 * The user pressing any button on the monitor is enough to wake it
725 	 * up, so there is no particularly good place to do the workaround.
726 	 * We just have to do it before any DPCD access and hope that the
727 	 * monitor doesn't power down exactly after the throw away read.
728 	 */
729 	return !aux->is_remote && !READ_ONCE(aux->dpcd_probe_disabled);
730 }
731 
732 /**
733  * drm_dp_dpcd_read() - read a series of bytes from the DPCD
734  * @aux: DisplayPort AUX channel (SST or MST)
735  * @offset: address of the (first) register to read
736  * @buffer: buffer to store the register values
737  * @size: number of bytes in @buffer
738  *
739  * Returns the number of bytes transferred on success, or a negative error
740  * code on failure. -EIO is returned if the request was NAKed by the sink or
741  * if the retry count was exceeded. If not all bytes were transferred, this
742  * function returns -EPROTO. Errors from the underlying AUX channel transfer
743  * function, with the exception of -EBUSY (which causes the transaction to
744  * be retried), are propagated to the caller.
745  *
746  * In most of the cases you want to use drm_dp_dpcd_read_data() instead.
747  */
748 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
749 			 void *buffer, size_t size)
750 {
751 	int ret;
752 
753 	if (dpcd_access_needs_probe(aux)) {
754 		ret = drm_dp_dpcd_probe(aux, DP_TRAINING_PATTERN_SET);
755 		if (ret < 0)
756 			return ret;
757 	}
758 
759 	if (aux->is_remote)
760 		ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
761 	else
762 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
763 					 buffer, size);
764 
765 	drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
766 	return ret;
767 }
768 EXPORT_SYMBOL(drm_dp_dpcd_read);
769 
770 /**
771  * drm_dp_dpcd_write() - write a series of bytes to the DPCD
772  * @aux: DisplayPort AUX channel (SST or MST)
773  * @offset: address of the (first) register to write
774  * @buffer: buffer containing the values to write
775  * @size: number of bytes in @buffer
776  *
777  * Returns the number of bytes transferred on success, or a negative error
778  * code on failure. -EIO is returned if the request was NAKed by the sink or
779  * if the retry count was exceeded. If not all bytes were transferred, this
780  * function returns -EPROTO. Errors from the underlying AUX channel transfer
781  * function, with the exception of -EBUSY (which causes the transaction to
782  * be retried), are propagated to the caller.
783  *
784  * In most of the cases you want to use drm_dp_dpcd_write_data() instead.
785  */
786 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
787 			  void *buffer, size_t size)
788 {
789 	int ret;
790 
791 	if (aux->is_remote)
792 		ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
793 	else
794 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
795 					 buffer, size);
796 
797 	drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
798 	return ret;
799 }
800 EXPORT_SYMBOL(drm_dp_dpcd_write);
801 
802 /**
803  * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
804  * @aux: DisplayPort AUX channel
805  * @status: buffer to store the link status in (must be at least 6 bytes)
806  *
807  * Returns a negative error code on failure or 0 on success.
808  */
809 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
810 				 u8 status[DP_LINK_STATUS_SIZE])
811 {
812 	return drm_dp_dpcd_read_data(aux, DP_LANE0_1_STATUS, status,
813 				     DP_LINK_STATUS_SIZE);
814 }
815 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
816 
817 /**
818  * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
819  * @aux: DisplayPort AUX channel
820  * @dp_phy: the DP PHY to get the link status for
821  * @link_status: buffer to return the status in
822  *
823  * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
824  * layout of the returned @link_status matches the DPCD register layout of the
825  * DPRX PHY link status.
826  *
827  * Returns 0 if the information was read successfully or a negative error code
828  * on failure.
829  */
830 int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
831 				     enum drm_dp_phy dp_phy,
832 				     u8 link_status[DP_LINK_STATUS_SIZE])
833 {
834 	int ret;
835 
836 	if (dp_phy == DP_PHY_DPRX)
837 		return drm_dp_dpcd_read_data(aux,
838 					     DP_LANE0_1_STATUS,
839 					     link_status,
840 					     DP_LINK_STATUS_SIZE);
841 
842 	ret = drm_dp_dpcd_read_data(aux,
843 				    DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
844 				    link_status,
845 				    DP_LINK_STATUS_SIZE - 1);
846 
847 	if (ret < 0)
848 		return ret;
849 
850 	/* Convert the LTTPR to the sink PHY link status layout */
851 	memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
852 		&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
853 		DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
854 	link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
855 
856 	return 0;
857 }
858 EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
859 
860 /**
861  * drm_dp_link_power_up() - power up a DisplayPort link
862  * @aux: DisplayPort AUX channel
863  * @revision: DPCD revision supported on the link
864  *
865  * Returns 0 on success or a negative error code on failure.
866  */
867 int drm_dp_link_power_up(struct drm_dp_aux *aux, unsigned char revision)
868 {
869 	u8 value;
870 	int err;
871 
872 	/* DP_SET_POWER register is only available on DPCD v1.1 and later */
873 	if (revision < DP_DPCD_REV_11)
874 		return 0;
875 
876 	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
877 	if (err < 0)
878 		return err;
879 
880 	value &= ~DP_SET_POWER_MASK;
881 	value |= DP_SET_POWER_D0;
882 
883 	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
884 	if (err < 0)
885 		return err;
886 
887 	/*
888 	 * According to the DP 1.1 specification, a "Sink Device must exit the
889 	 * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
890 	 * Control Field" (register 0x600).
891 	 */
892 	usleep_range(1000, 2000);
893 
894 	return 0;
895 }
896 EXPORT_SYMBOL(drm_dp_link_power_up);
897 
898 /**
899  * drm_dp_link_power_down() - power down a DisplayPort link
900  * @aux: DisplayPort AUX channel
901  * @revision: DPCD revision supported on the link
902  *
903  * Returns 0 on success or a negative error code on failure.
904  */
905 int drm_dp_link_power_down(struct drm_dp_aux *aux, unsigned char revision)
906 {
907 	u8 value;
908 	int err;
909 
910 	/* DP_SET_POWER register is only available on DPCD v1.1 and later */
911 	if (revision < DP_DPCD_REV_11)
912 		return 0;
913 
914 	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
915 	if (err < 0)
916 		return err;
917 
918 	value &= ~DP_SET_POWER_MASK;
919 	value |= DP_SET_POWER_D3;
920 
921 	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
922 	if (err < 0)
923 		return err;
924 
925 	return 0;
926 }
927 EXPORT_SYMBOL(drm_dp_link_power_down);
928 
929 static int read_payload_update_status(struct drm_dp_aux *aux)
930 {
931 	int ret;
932 	u8 status;
933 
934 	ret = drm_dp_dpcd_read_byte(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
935 	if (ret < 0)
936 		return ret;
937 
938 	return status;
939 }
940 
941 /**
942  * drm_dp_dpcd_write_payload() - Write Virtual Channel information to payload table
943  * @aux: DisplayPort AUX channel
944  * @vcpid: Virtual Channel Payload ID
945  * @start_time_slot: Starting time slot
946  * @time_slot_count: Time slot count
947  *
948  * Write the Virtual Channel payload allocation table, checking the payload
949  * update status and retrying as necessary.
950  *
951  * Returns:
952  * 0 on success, negative error otherwise
953  */
954 int drm_dp_dpcd_write_payload(struct drm_dp_aux *aux,
955 			      int vcpid, u8 start_time_slot, u8 time_slot_count)
956 {
957 	u8 payload_alloc[3], status;
958 	int ret;
959 	int retries = 0;
960 
961 	drm_dp_dpcd_write_byte(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
962 			       DP_PAYLOAD_TABLE_UPDATED);
963 
964 	payload_alloc[0] = vcpid;
965 	payload_alloc[1] = start_time_slot;
966 	payload_alloc[2] = time_slot_count;
967 
968 	ret = drm_dp_dpcd_write_data(aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
969 	if (ret < 0) {
970 		drm_dbg_kms(aux->drm_dev, "failed to write payload allocation %d\n", ret);
971 		goto fail;
972 	}
973 
974 retry:
975 	ret = drm_dp_dpcd_read_byte(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
976 	if (ret < 0) {
977 		drm_dbg_kms(aux->drm_dev, "failed to read payload table status %d\n", ret);
978 		goto fail;
979 	}
980 
981 	if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
982 		retries++;
983 		if (retries < 20) {
984 			usleep_range(10000, 20000);
985 			goto retry;
986 		}
987 		drm_dbg_kms(aux->drm_dev, "status not set after read payload table status %d\n",
988 			    status);
989 		ret = -EINVAL;
990 		goto fail;
991 	}
992 	ret = 0;
993 fail:
994 	return ret;
995 }
996 EXPORT_SYMBOL(drm_dp_dpcd_write_payload);
997 
998 /**
999  * drm_dp_dpcd_clear_payload() - Clear the entire VC Payload ID table
1000  * @aux: DisplayPort AUX channel
1001  *
1002  * Clear the entire VC Payload ID table.
1003  *
1004  * Returns: 0 on success, negative error code on errors.
1005  */
1006 int drm_dp_dpcd_clear_payload(struct drm_dp_aux *aux)
1007 {
1008 	return drm_dp_dpcd_write_payload(aux, 0, 0, 0x3f);
1009 }
1010 EXPORT_SYMBOL(drm_dp_dpcd_clear_payload);
1011 
1012 /**
1013  * drm_dp_dpcd_poll_act_handled() - Poll for ACT handled status
1014  * @aux: DisplayPort AUX channel
1015  * @timeout_ms: Timeout in ms
1016  *
1017  * Try waiting for the sink to finish updating its payload table by polling for
1018  * the ACT handled bit of DP_PAYLOAD_TABLE_UPDATE_STATUS for up to @timeout_ms
1019  * milliseconds, defaulting to 3000 ms if 0.
1020  *
1021  * Returns:
1022  * 0 if the ACT was handled in time, negative error code on failure.
1023  */
1024 int drm_dp_dpcd_poll_act_handled(struct drm_dp_aux *aux, int timeout_ms)
1025 {
1026 	int ret, status;
1027 
1028 	/* default to 3 seconds, this is arbitrary */
1029 	timeout_ms = timeout_ms ?: 3000;
1030 
1031 	ret = readx_poll_timeout(read_payload_update_status, aux, status,
1032 				 status & DP_PAYLOAD_ACT_HANDLED || status < 0,
1033 				 200, timeout_ms * USEC_PER_MSEC);
1034 	if (ret < 0 && status >= 0) {
1035 		drm_err(aux->drm_dev, "Failed to get ACT after %d ms, last status: %02x\n",
1036 			timeout_ms, status);
1037 		return -EINVAL;
1038 	} else if (status < 0) {
1039 		/*
1040 		 * Failure here isn't unexpected - the hub may have
1041 		 * just been unplugged
1042 		 */
1043 		drm_dbg_kms(aux->drm_dev, "Failed to read payload table status: %d\n", status);
1044 		return status;
1045 	}
1046 
1047 	return 0;
1048 }
1049 EXPORT_SYMBOL(drm_dp_dpcd_poll_act_handled);
1050 
1051 static bool is_edid_digital_input_dp(const struct drm_edid *drm_edid)
1052 {
1053 	/* FIXME: get rid of drm_edid_raw() */
1054 	const struct edid *edid = drm_edid_raw(drm_edid);
1055 
1056 	return edid && edid->revision >= 4 &&
1057 		edid->input & DRM_EDID_INPUT_DIGITAL &&
1058 		(edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
1059 }
1060 
1061 /**
1062  * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
1063  * @dpcd: DisplayPort configuration data
1064  * @port_cap: port capabilities
1065  * @type: port type to be checked. Can be:
1066  * 	  %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
1067  * 	  %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
1068  *	  %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
1069  *
1070  * Caveat: Only works with DPCD 1.1+ port caps.
1071  *
1072  * Returns: whether the downstream facing port matches the type.
1073  */
1074 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1075 			       const u8 port_cap[4], u8 type)
1076 {
1077 	return drm_dp_is_branch(dpcd) &&
1078 		dpcd[DP_DPCD_REV] >= 0x11 &&
1079 		(port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
1080 }
1081 EXPORT_SYMBOL(drm_dp_downstream_is_type);
1082 
1083 /**
1084  * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
1085  * @dpcd: DisplayPort configuration data
1086  * @port_cap: port capabilities
1087  * @drm_edid: EDID
1088  *
1089  * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
1090  */
1091 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1092 			       const u8 port_cap[4],
1093 			       const struct drm_edid *drm_edid)
1094 {
1095 	if (dpcd[DP_DPCD_REV] < 0x11) {
1096 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
1097 		case DP_DWN_STRM_PORT_TYPE_TMDS:
1098 			return true;
1099 		default:
1100 			return false;
1101 		}
1102 	}
1103 
1104 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1105 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1106 		if (is_edid_digital_input_dp(drm_edid))
1107 			return false;
1108 		fallthrough;
1109 	case DP_DS_PORT_TYPE_DVI:
1110 	case DP_DS_PORT_TYPE_HDMI:
1111 		return true;
1112 	default:
1113 		return false;
1114 	}
1115 }
1116 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
1117 
1118 /**
1119  * drm_dp_send_real_edid_checksum() - send back real edid checksum value
1120  * @aux: DisplayPort AUX channel
1121  * @real_edid_checksum: real edid checksum for the last block
1122  *
1123  * Returns:
1124  * True on success
1125  */
1126 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
1127 				    u8 real_edid_checksum)
1128 {
1129 	u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
1130 
1131 	if (drm_dp_dpcd_read_byte(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
1132 				  &auto_test_req) < 0) {
1133 		drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
1134 			aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
1135 		return false;
1136 	}
1137 	auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
1138 
1139 	if (drm_dp_dpcd_read_byte(aux, DP_TEST_REQUEST, &link_edid_read) < 0) {
1140 		drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
1141 			aux->name, DP_TEST_REQUEST);
1142 		return false;
1143 	}
1144 	link_edid_read &= DP_TEST_LINK_EDID_READ;
1145 
1146 	if (!auto_test_req || !link_edid_read) {
1147 		drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",
1148 			    aux->name);
1149 		return false;
1150 	}
1151 
1152 	if (drm_dp_dpcd_write_byte(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
1153 				   auto_test_req) < 0) {
1154 		drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
1155 			aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
1156 		return false;
1157 	}
1158 
1159 	/* send back checksum for the last edid extension block data */
1160 	if (drm_dp_dpcd_write_byte(aux, DP_TEST_EDID_CHECKSUM,
1161 				   real_edid_checksum) < 0) {
1162 		drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
1163 			aux->name, DP_TEST_EDID_CHECKSUM);
1164 		return false;
1165 	}
1166 
1167 	test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
1168 	if (drm_dp_dpcd_write_byte(aux, DP_TEST_RESPONSE, test_resp) < 0) {
1169 		drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
1170 			aux->name, DP_TEST_RESPONSE);
1171 		return false;
1172 	}
1173 
1174 	return true;
1175 }
1176 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
1177 
1178 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
1179 {
1180 	u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
1181 
1182 	if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
1183 		port_count = 4;
1184 
1185 	return port_count;
1186 }
1187 
1188 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
1189 					  u8 dpcd[DP_RECEIVER_CAP_SIZE])
1190 {
1191 	u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
1192 	int ret;
1193 
1194 	/*
1195 	 * Prior to DP1.3 the bit represented by
1196 	 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
1197 	 * If it is set DP_DPCD_REV at 0000h could be at a value less than
1198 	 * the true capability of the panel. The only way to check is to
1199 	 * then compare 0000h and 2200h.
1200 	 */
1201 	if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
1202 	      DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
1203 		return 0;
1204 
1205 	ret = drm_dp_dpcd_read_data(aux, DP_DP13_DPCD_REV, &dpcd_ext,
1206 				    sizeof(dpcd_ext));
1207 	if (ret < 0)
1208 		return ret;
1209 
1210 	if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
1211 		drm_dbg_kms(aux->drm_dev,
1212 			    "%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
1213 			    aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
1214 		return 0;
1215 	}
1216 
1217 	if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
1218 		return 0;
1219 
1220 	drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
1221 
1222 	memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
1223 
1224 	return 0;
1225 }
1226 
1227 /**
1228  * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
1229  * available
1230  * @aux: DisplayPort AUX channel
1231  * @dpcd: Buffer to store the resulting DPCD in
1232  *
1233  * Attempts to read the base DPCD caps for @aux. Additionally, this function
1234  * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
1235  * present.
1236  *
1237  * Returns: %0 if the DPCD was read successfully, negative error code
1238  * otherwise.
1239  */
1240 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
1241 			  u8 dpcd[DP_RECEIVER_CAP_SIZE])
1242 {
1243 	int ret;
1244 
1245 	ret = drm_dp_dpcd_read_data(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
1246 	if (ret < 0)
1247 		return ret;
1248 	if (dpcd[DP_DPCD_REV] == 0)
1249 		return -EIO;
1250 
1251 	ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
1252 	if (ret < 0)
1253 		return ret;
1254 
1255 	drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
1256 
1257 	return ret;
1258 }
1259 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
1260 
1261 /**
1262  * drm_dp_read_downstream_info() - read DPCD downstream port info if available
1263  * @aux: DisplayPort AUX channel
1264  * @dpcd: A cached copy of the port's DPCD
1265  * @downstream_ports: buffer to store the downstream port info in
1266  *
1267  * See also:
1268  * drm_dp_downstream_max_clock()
1269  * drm_dp_downstream_max_bpc()
1270  *
1271  * Returns: 0 if either the downstream port info was read successfully or
1272  * there was no downstream info to read, or a negative error code otherwise.
1273  */
1274 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
1275 				const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1276 				u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
1277 {
1278 	int ret;
1279 	u8 len;
1280 
1281 	memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
1282 
1283 	/* No downstream info to read */
1284 	if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)
1285 		return 0;
1286 
1287 	/* Some branches advertise having 0 downstream ports, despite also advertising they have a
1288 	 * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
1289 	 * some branches do it we need to handle it regardless.
1290 	 */
1291 	len = drm_dp_downstream_port_count(dpcd);
1292 	if (!len)
1293 		return 0;
1294 
1295 	if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
1296 		len *= 4;
1297 
1298 	ret = drm_dp_dpcd_read_data(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
1299 	if (ret < 0)
1300 		return ret;
1301 
1302 	drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);
1303 
1304 	return 0;
1305 }
1306 EXPORT_SYMBOL(drm_dp_read_downstream_info);
1307 
1308 /**
1309  * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
1310  * @dpcd: DisplayPort configuration data
1311  * @port_cap: port capabilities
1312  *
1313  * Returns: Downstream facing port max dot clock in kHz on success,
1314  * or 0 if max clock not defined
1315  */
1316 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1317 				   const u8 port_cap[4])
1318 {
1319 	if (!drm_dp_is_branch(dpcd))
1320 		return 0;
1321 
1322 	if (dpcd[DP_DPCD_REV] < 0x11)
1323 		return 0;
1324 
1325 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1326 	case DP_DS_PORT_TYPE_VGA:
1327 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1328 			return 0;
1329 		return port_cap[1] * 8000;
1330 	default:
1331 		return 0;
1332 	}
1333 }
1334 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
1335 
1336 /**
1337  * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
1338  * @dpcd: DisplayPort configuration data
1339  * @port_cap: port capabilities
1340  * @drm_edid: EDID
1341  *
1342  * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
1343  * or 0 if max TMDS clock not defined
1344  */
1345 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1346 				     const u8 port_cap[4],
1347 				     const struct drm_edid *drm_edid)
1348 {
1349 	if (!drm_dp_is_branch(dpcd))
1350 		return 0;
1351 
1352 	if (dpcd[DP_DPCD_REV] < 0x11) {
1353 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
1354 		case DP_DWN_STRM_PORT_TYPE_TMDS:
1355 			return 165000;
1356 		default:
1357 			return 0;
1358 		}
1359 	}
1360 
1361 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1362 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1363 		if (is_edid_digital_input_dp(drm_edid))
1364 			return 0;
1365 		/*
1366 		 * It's left up to the driver to check the
1367 		 * DP dual mode adapter's max TMDS clock.
1368 		 *
1369 		 * Unfortunately it looks like branch devices
1370 		 * may not fordward that the DP dual mode i2c
1371 		 * access so we just usually get i2c nak :(
1372 		 */
1373 		fallthrough;
1374 	case DP_DS_PORT_TYPE_HDMI:
1375 		 /*
1376 		  * We should perhaps assume 165 MHz when detailed cap
1377 		  * info is not available. But looks like many typical
1378 		  * branch devices fall into that category and so we'd
1379 		  * probably end up with users complaining that they can't
1380 		  * get high resolution modes with their favorite dongle.
1381 		  *
1382 		  * So let's limit to 300 MHz instead since DPCD 1.4
1383 		  * HDMI 2.0 DFPs are required to have the detailed cap
1384 		  * info. So it's more likely we're dealing with a HDMI 1.4
1385 		  * compatible* device here.
1386 		  */
1387 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1388 			return 300000;
1389 		return port_cap[1] * 2500;
1390 	case DP_DS_PORT_TYPE_DVI:
1391 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1392 			return 165000;
1393 		/* FIXME what to do about DVI dual link? */
1394 		return port_cap[1] * 2500;
1395 	default:
1396 		return 0;
1397 	}
1398 }
1399 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
1400 
1401 /**
1402  * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
1403  * @dpcd: DisplayPort configuration data
1404  * @port_cap: port capabilities
1405  * @drm_edid: EDID
1406  *
1407  * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
1408  * or 0 if max TMDS clock not defined
1409  */
1410 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1411 				     const u8 port_cap[4],
1412 				     const struct drm_edid *drm_edid)
1413 {
1414 	if (!drm_dp_is_branch(dpcd))
1415 		return 0;
1416 
1417 	if (dpcd[DP_DPCD_REV] < 0x11) {
1418 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
1419 		case DP_DWN_STRM_PORT_TYPE_TMDS:
1420 			return 25000;
1421 		default:
1422 			return 0;
1423 		}
1424 	}
1425 
1426 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1427 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1428 		if (is_edid_digital_input_dp(drm_edid))
1429 			return 0;
1430 		fallthrough;
1431 	case DP_DS_PORT_TYPE_DVI:
1432 	case DP_DS_PORT_TYPE_HDMI:
1433 		/*
1434 		 * Unclear whether the protocol converter could
1435 		 * utilize pixel replication. Assume it won't.
1436 		 */
1437 		return 25000;
1438 	default:
1439 		return 0;
1440 	}
1441 }
1442 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
1443 
1444 /**
1445  * drm_dp_downstream_max_bpc() - extract downstream facing port max
1446  *                               bits per component
1447  * @dpcd: DisplayPort configuration data
1448  * @port_cap: downstream facing port capabilities
1449  * @drm_edid: EDID
1450  *
1451  * Returns: Max bpc on success or 0 if max bpc not defined
1452  */
1453 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1454 			      const u8 port_cap[4],
1455 			      const struct drm_edid *drm_edid)
1456 {
1457 	if (!drm_dp_is_branch(dpcd))
1458 		return 0;
1459 
1460 	if (dpcd[DP_DPCD_REV] < 0x11) {
1461 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
1462 		case DP_DWN_STRM_PORT_TYPE_DP:
1463 			return 0;
1464 		default:
1465 			return 8;
1466 		}
1467 	}
1468 
1469 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1470 	case DP_DS_PORT_TYPE_DP:
1471 		return 0;
1472 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1473 		if (is_edid_digital_input_dp(drm_edid))
1474 			return 0;
1475 		fallthrough;
1476 	case DP_DS_PORT_TYPE_HDMI:
1477 	case DP_DS_PORT_TYPE_DVI:
1478 	case DP_DS_PORT_TYPE_VGA:
1479 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1480 			return 8;
1481 
1482 		switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
1483 		case DP_DS_8BPC:
1484 			return 8;
1485 		case DP_DS_10BPC:
1486 			return 10;
1487 		case DP_DS_12BPC:
1488 			return 12;
1489 		case DP_DS_16BPC:
1490 			return 16;
1491 		default:
1492 			return 8;
1493 		}
1494 		break;
1495 	default:
1496 		return 8;
1497 	}
1498 }
1499 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
1500 
1501 /**
1502  * drm_dp_downstream_420_passthrough() - determine downstream facing port
1503  *                                       YCbCr 4:2:0 pass-through capability
1504  * @dpcd: DisplayPort configuration data
1505  * @port_cap: downstream facing port capabilities
1506  *
1507  * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
1508  */
1509 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1510 				       const u8 port_cap[4])
1511 {
1512 	if (!drm_dp_is_branch(dpcd))
1513 		return false;
1514 
1515 	if (dpcd[DP_DPCD_REV] < 0x13)
1516 		return false;
1517 
1518 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1519 	case DP_DS_PORT_TYPE_DP:
1520 		return true;
1521 	case DP_DS_PORT_TYPE_HDMI:
1522 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1523 			return false;
1524 
1525 		return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
1526 	default:
1527 		return false;
1528 	}
1529 }
1530 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
1531 
1532 /**
1533  * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
1534  *                                             YCbCr 4:4:4->4:2:0 conversion capability
1535  * @dpcd: DisplayPort configuration data
1536  * @port_cap: downstream facing port capabilities
1537  *
1538  * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
1539  */
1540 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1541 					     const u8 port_cap[4])
1542 {
1543 	if (!drm_dp_is_branch(dpcd))
1544 		return false;
1545 
1546 	if (dpcd[DP_DPCD_REV] < 0x13)
1547 		return false;
1548 
1549 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1550 	case DP_DS_PORT_TYPE_HDMI:
1551 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1552 			return false;
1553 
1554 		return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
1555 	default:
1556 		return false;
1557 	}
1558 }
1559 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
1560 
1561 /**
1562  * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
1563  *                                               RGB->YCbCr conversion capability
1564  * @dpcd: DisplayPort configuration data
1565  * @port_cap: downstream facing port capabilities
1566  * @color_spc: Colorspace for which conversion cap is sought
1567  *
1568  * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
1569  * colorspace.
1570  */
1571 bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1572 					       const u8 port_cap[4],
1573 					       u8 color_spc)
1574 {
1575 	if (!drm_dp_is_branch(dpcd))
1576 		return false;
1577 
1578 	if (dpcd[DP_DPCD_REV] < 0x13)
1579 		return false;
1580 
1581 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1582 	case DP_DS_PORT_TYPE_HDMI:
1583 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1584 			return false;
1585 
1586 		return port_cap[3] & color_spc;
1587 	default:
1588 		return false;
1589 	}
1590 }
1591 EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
1592 
1593 /**
1594  * drm_dp_downstream_mode() - return a mode for downstream facing port
1595  * @dev: DRM device
1596  * @dpcd: DisplayPort configuration data
1597  * @port_cap: port capabilities
1598  *
1599  * Provides a suitable mode for downstream facing ports without EDID.
1600  *
1601  * Returns: A new drm_display_mode on success or NULL on failure
1602  */
1603 struct drm_display_mode *
1604 drm_dp_downstream_mode(struct drm_device *dev,
1605 		       const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1606 		       const u8 port_cap[4])
1607 
1608 {
1609 	u8 vic;
1610 
1611 	if (!drm_dp_is_branch(dpcd))
1612 		return NULL;
1613 
1614 	if (dpcd[DP_DPCD_REV] < 0x11)
1615 		return NULL;
1616 
1617 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1618 	case DP_DS_PORT_TYPE_NON_EDID:
1619 		switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
1620 		case DP_DS_NON_EDID_720x480i_60:
1621 			vic = 6;
1622 			break;
1623 		case DP_DS_NON_EDID_720x480i_50:
1624 			vic = 21;
1625 			break;
1626 		case DP_DS_NON_EDID_1920x1080i_60:
1627 			vic = 5;
1628 			break;
1629 		case DP_DS_NON_EDID_1920x1080i_50:
1630 			vic = 20;
1631 			break;
1632 		case DP_DS_NON_EDID_1280x720_60:
1633 			vic = 4;
1634 			break;
1635 		case DP_DS_NON_EDID_1280x720_50:
1636 			vic = 19;
1637 			break;
1638 		default:
1639 			return NULL;
1640 		}
1641 		return drm_display_mode_from_cea_vic(dev, vic);
1642 	default:
1643 		return NULL;
1644 	}
1645 }
1646 EXPORT_SYMBOL(drm_dp_downstream_mode);
1647 
1648 /**
1649  * drm_dp_downstream_id() - identify branch device
1650  * @aux: DisplayPort AUX channel
1651  * @id: DisplayPort branch device id
1652  *
1653  * Returns branch device id on success or NULL on failure
1654  */
1655 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
1656 {
1657 	return drm_dp_dpcd_read_data(aux, DP_BRANCH_ID, id, 6);
1658 }
1659 EXPORT_SYMBOL(drm_dp_downstream_id);
1660 
1661 /**
1662  * drm_dp_downstream_debug() - debug DP branch devices
1663  * @m: pointer for debugfs file
1664  * @dpcd: DisplayPort configuration data
1665  * @port_cap: port capabilities
1666  * @drm_edid: EDID
1667  * @aux: DisplayPort AUX channel
1668  *
1669  */
1670 void drm_dp_downstream_debug(struct seq_file *m,
1671 			     const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1672 			     const u8 port_cap[4],
1673 			     const struct drm_edid *drm_edid,
1674 			     struct drm_dp_aux *aux)
1675 {
1676 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1677 				 DP_DETAILED_CAP_INFO_AVAILABLE;
1678 	int clk;
1679 	int bpc;
1680 	char id[7];
1681 	int len;
1682 	uint8_t rev[2];
1683 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1684 	bool branch_device = drm_dp_is_branch(dpcd);
1685 
1686 	seq_printf(m, "\tDP branch device present: %s\n",
1687 		   str_yes_no(branch_device));
1688 
1689 	if (!branch_device)
1690 		return;
1691 
1692 	switch (type) {
1693 	case DP_DS_PORT_TYPE_DP:
1694 		seq_puts(m, "\t\tType: DisplayPort\n");
1695 		break;
1696 	case DP_DS_PORT_TYPE_VGA:
1697 		seq_puts(m, "\t\tType: VGA\n");
1698 		break;
1699 	case DP_DS_PORT_TYPE_DVI:
1700 		seq_puts(m, "\t\tType: DVI\n");
1701 		break;
1702 	case DP_DS_PORT_TYPE_HDMI:
1703 		seq_puts(m, "\t\tType: HDMI\n");
1704 		break;
1705 	case DP_DS_PORT_TYPE_NON_EDID:
1706 		seq_puts(m, "\t\tType: others without EDID support\n");
1707 		break;
1708 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1709 		seq_puts(m, "\t\tType: DP++\n");
1710 		break;
1711 	case DP_DS_PORT_TYPE_WIRELESS:
1712 		seq_puts(m, "\t\tType: Wireless\n");
1713 		break;
1714 	default:
1715 		seq_puts(m, "\t\tType: N/A\n");
1716 	}
1717 
1718 	memset(id, 0, sizeof(id));
1719 	drm_dp_downstream_id(aux, id);
1720 	seq_printf(m, "\t\tID: %s\n", id);
1721 
1722 	len = drm_dp_dpcd_read_data(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1723 	if (!len)
1724 		seq_printf(m, "\t\tHW: %d.%d\n",
1725 			   (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1726 
1727 	len = drm_dp_dpcd_read_data(aux, DP_BRANCH_SW_REV, rev, 2);
1728 	if (!len)
1729 		seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1730 
1731 	if (detailed_cap_info) {
1732 		clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1733 		if (clk > 0)
1734 			seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1735 
1736 		clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, drm_edid);
1737 		if (clk > 0)
1738 			seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1739 
1740 		clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, drm_edid);
1741 		if (clk > 0)
1742 			seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1743 
1744 		bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, drm_edid);
1745 
1746 		if (bpc > 0)
1747 			seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1748 	}
1749 }
1750 EXPORT_SYMBOL(drm_dp_downstream_debug);
1751 
1752 /**
1753  * drm_dp_subconnector_type() - get DP branch device type
1754  * @dpcd: DisplayPort configuration data
1755  * @port_cap: port capabilities
1756  */
1757 enum drm_mode_subconnector
1758 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1759 			 const u8 port_cap[4])
1760 {
1761 	int type;
1762 	if (!drm_dp_is_branch(dpcd))
1763 		return DRM_MODE_SUBCONNECTOR_Native;
1764 	/* DP 1.0 approach */
1765 	if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1766 		type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1767 		       DP_DWN_STRM_PORT_TYPE_MASK;
1768 
1769 		switch (type) {
1770 		case DP_DWN_STRM_PORT_TYPE_TMDS:
1771 			/* Can be HDMI or DVI-D, DVI-D is a safer option */
1772 			return DRM_MODE_SUBCONNECTOR_DVID;
1773 		case DP_DWN_STRM_PORT_TYPE_ANALOG:
1774 			/* Can be VGA or DVI-A, VGA is more popular */
1775 			return DRM_MODE_SUBCONNECTOR_VGA;
1776 		case DP_DWN_STRM_PORT_TYPE_DP:
1777 			return DRM_MODE_SUBCONNECTOR_DisplayPort;
1778 		case DP_DWN_STRM_PORT_TYPE_OTHER:
1779 		default:
1780 			return DRM_MODE_SUBCONNECTOR_Unknown;
1781 		}
1782 	}
1783 	type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1784 
1785 	switch (type) {
1786 	case DP_DS_PORT_TYPE_DP:
1787 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1788 		return DRM_MODE_SUBCONNECTOR_DisplayPort;
1789 	case DP_DS_PORT_TYPE_VGA:
1790 		return DRM_MODE_SUBCONNECTOR_VGA;
1791 	case DP_DS_PORT_TYPE_DVI:
1792 		return DRM_MODE_SUBCONNECTOR_DVID;
1793 	case DP_DS_PORT_TYPE_HDMI:
1794 		return DRM_MODE_SUBCONNECTOR_HDMIA;
1795 	case DP_DS_PORT_TYPE_WIRELESS:
1796 		return DRM_MODE_SUBCONNECTOR_Wireless;
1797 	case DP_DS_PORT_TYPE_NON_EDID:
1798 	default:
1799 		return DRM_MODE_SUBCONNECTOR_Unknown;
1800 	}
1801 }
1802 EXPORT_SYMBOL(drm_dp_subconnector_type);
1803 
1804 /**
1805  * drm_dp_set_subconnector_property - set subconnector for DP connector
1806  * @connector: connector to set property on
1807  * @status: connector status
1808  * @dpcd: DisplayPort configuration data
1809  * @port_cap: port capabilities
1810  *
1811  * Called by a driver on every detect event.
1812  */
1813 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1814 				      enum drm_connector_status status,
1815 				      const u8 *dpcd,
1816 				      const u8 port_cap[4])
1817 {
1818 	enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1819 
1820 	if (status == connector_status_connected)
1821 		subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1822 	drm_object_property_set_value(&connector->base,
1823 			connector->dev->mode_config.dp_subconnector_property,
1824 			subconnector);
1825 }
1826 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1827 
1828 /**
1829  * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1830  * count
1831  * @connector: The DRM connector to check
1832  * @dpcd: A cached copy of the connector's DPCD RX capabilities
1833  * @desc: A cached copy of the connector's DP descriptor
1834  *
1835  * See also: drm_dp_read_sink_count()
1836  *
1837  * Returns: %True if the (e)DP connector has a valid sink count that should
1838  * be probed, %false otherwise.
1839  */
1840 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1841 				const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1842 				const struct drm_dp_desc *desc)
1843 {
1844 	/* Some eDP panels don't set a valid value for the sink count */
1845 	return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1846 		dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1847 		dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1848 		!drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);
1849 }
1850 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1851 
1852 /**
1853  * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1854  * @aux: The DP AUX channel to use
1855  *
1856  * See also: drm_dp_read_sink_count_cap()
1857  *
1858  * Returns: The current sink count reported by @aux, or a negative error code
1859  * otherwise.
1860  */
1861 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1862 {
1863 	u8 count;
1864 	int ret;
1865 
1866 	ret = drm_dp_dpcd_read_byte(aux, DP_SINK_COUNT, &count);
1867 	if (ret < 0)
1868 		return ret;
1869 
1870 	return DP_GET_SINK_COUNT(count);
1871 }
1872 EXPORT_SYMBOL(drm_dp_read_sink_count);
1873 
1874 /*
1875  * I2C-over-AUX implementation
1876  */
1877 
1878 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1879 {
1880 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1881 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1882 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1883 	       I2C_FUNC_10BIT_ADDR;
1884 }
1885 
1886 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1887 {
1888 	/*
1889 	 * In case of i2c defer or short i2c ack reply to a write,
1890 	 * we need to switch to WRITE_STATUS_UPDATE to drain the
1891 	 * rest of the message
1892 	 */
1893 	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1894 		msg->request &= DP_AUX_I2C_MOT;
1895 		msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1896 	}
1897 }
1898 
1899 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1900 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1901 #define AUX_STOP_LEN 4
1902 #define AUX_CMD_LEN 4
1903 #define AUX_ADDRESS_LEN 20
1904 #define AUX_REPLY_PAD_LEN 4
1905 #define AUX_LENGTH_LEN 8
1906 
1907 /*
1908  * Calculate the duration of the AUX request/reply in usec. Gives the
1909  * "best" case estimate, ie. successful while as short as possible.
1910  */
1911 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1912 {
1913 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1914 		AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1915 
1916 	if ((msg->request & DP_AUX_I2C_READ) == 0)
1917 		len += msg->size * 8;
1918 
1919 	return len;
1920 }
1921 
1922 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1923 {
1924 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1925 		AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1926 
1927 	/*
1928 	 * For read we expect what was asked. For writes there will
1929 	 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1930 	 */
1931 	if (msg->request & DP_AUX_I2C_READ)
1932 		len += msg->size * 8;
1933 
1934 	return len;
1935 }
1936 
1937 #define I2C_START_LEN 1
1938 #define I2C_STOP_LEN 1
1939 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1940 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1941 
1942 /*
1943  * Calculate the length of the i2c transfer in usec, assuming
1944  * the i2c bus speed is as specified. Gives the "worst"
1945  * case estimate, ie. successful while as long as possible.
1946  * Doesn't account the "MOT" bit, and instead assumes each
1947  * message includes a START, ADDRESS and STOP. Neither does it
1948  * account for additional random variables such as clock stretching.
1949  */
1950 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1951 				   int i2c_speed_khz)
1952 {
1953 	/* AUX bitrate is 1MHz, i2c bitrate as specified */
1954 	return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1955 			     msg->size * I2C_DATA_LEN +
1956 			     I2C_STOP_LEN) * 1000, i2c_speed_khz);
1957 }
1958 
1959 /*
1960  * Determine how many retries should be attempted to successfully transfer
1961  * the specified message, based on the estimated durations of the
1962  * i2c and AUX transfers.
1963  */
1964 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1965 			      int i2c_speed_khz)
1966 {
1967 	int aux_time_us = drm_dp_aux_req_duration(msg) +
1968 		drm_dp_aux_reply_duration(msg);
1969 	int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1970 
1971 	return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1972 }
1973 
1974 /*
1975  * FIXME currently assumes 10 kHz as some real world devices seem
1976  * to require it. We should query/set the speed via DPCD if supported.
1977  */
1978 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1979 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1980 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1981 		 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1982 
1983 /*
1984  * Transfer a single I2C-over-AUX message and handle various error conditions,
1985  * retrying the transaction as appropriate.  It is assumed that the
1986  * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1987  * reply field.
1988  *
1989  * Returns bytes transferred on success, or a negative error code on failure.
1990  */
1991 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1992 {
1993 	unsigned int retry, defer_i2c;
1994 	int ret;
1995 	/*
1996 	 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1997 	 * is required to retry at least seven times upon receiving AUX_DEFER
1998 	 * before giving up the AUX transaction.
1999 	 *
2000 	 * We also try to account for the i2c bus speed.
2001 	 */
2002 	int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
2003 
2004 	for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
2005 		ret = aux->transfer(aux, msg);
2006 		if (ret < 0) {
2007 			if (ret == -EBUSY)
2008 				continue;
2009 
2010 			/*
2011 			 * While timeouts can be errors, they're usually normal
2012 			 * behavior (for instance, when a driver tries to
2013 			 * communicate with a non-existent DisplayPort device).
2014 			 * Avoid spamming the kernel log with timeout errors.
2015 			 */
2016 			if (ret == -ETIMEDOUT)
2017 				drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",
2018 							aux->name);
2019 			else
2020 				drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",
2021 					    aux->name, ret);
2022 			return ret;
2023 		}
2024 
2025 
2026 		switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
2027 		case DP_AUX_NATIVE_REPLY_ACK:
2028 			/*
2029 			 * For I2C-over-AUX transactions this isn't enough, we
2030 			 * need to check for the I2C ACK reply.
2031 			 */
2032 			break;
2033 
2034 		case DP_AUX_NATIVE_REPLY_NACK:
2035 			drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",
2036 				    aux->name, ret, msg->size);
2037 			return -EREMOTEIO;
2038 
2039 		case DP_AUX_NATIVE_REPLY_DEFER:
2040 			drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);
2041 			/*
2042 			 * We could check for I2C bit rate capabilities and if
2043 			 * available adjust this interval. We could also be
2044 			 * more careful with DP-to-legacy adapters where a
2045 			 * long legacy cable may force very low I2C bit rates.
2046 			 *
2047 			 * For now just defer for long enough to hopefully be
2048 			 * safe for all use-cases.
2049 			 */
2050 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
2051 			continue;
2052 
2053 		default:
2054 			drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",
2055 				aux->name, msg->reply);
2056 			return -EREMOTEIO;
2057 		}
2058 
2059 		switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
2060 		case DP_AUX_I2C_REPLY_ACK:
2061 			/*
2062 			 * Both native ACK and I2C ACK replies received. We
2063 			 * can assume the transfer was successful.
2064 			 */
2065 			if (ret != msg->size)
2066 				drm_dp_i2c_msg_write_status_update(msg);
2067 			return ret;
2068 
2069 		case DP_AUX_I2C_REPLY_NACK:
2070 			drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",
2071 				    aux->name, ret, msg->size);
2072 			aux->i2c_nack_count++;
2073 			return -EREMOTEIO;
2074 
2075 		case DP_AUX_I2C_REPLY_DEFER:
2076 			drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);
2077 			/* DP Compliance Test 4.2.2.5 Requirement:
2078 			 * Must have at least 7 retries for I2C defers on the
2079 			 * transaction to pass this test
2080 			 */
2081 			aux->i2c_defer_count++;
2082 			if (defer_i2c < 7)
2083 				defer_i2c++;
2084 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
2085 			drm_dp_i2c_msg_write_status_update(msg);
2086 
2087 			continue;
2088 
2089 		default:
2090 			drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",
2091 				aux->name, msg->reply);
2092 			return -EREMOTEIO;
2093 		}
2094 	}
2095 
2096 	drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);
2097 	return -EREMOTEIO;
2098 }
2099 
2100 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
2101 				       const struct i2c_msg *i2c_msg)
2102 {
2103 	msg->request = (i2c_msg->flags & I2C_M_RD) ?
2104 		DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
2105 	if (!(i2c_msg->flags & I2C_M_STOP))
2106 		msg->request |= DP_AUX_I2C_MOT;
2107 }
2108 
2109 /*
2110  * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
2111  *
2112  * Returns an error code on failure, or a recommended transfer size on success.
2113  */
2114 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
2115 {
2116 	int err, ret = orig_msg->size;
2117 	struct drm_dp_aux_msg msg = *orig_msg;
2118 
2119 	while (msg.size > 0) {
2120 		err = drm_dp_i2c_do_msg(aux, &msg);
2121 		if (err <= 0)
2122 			return err == 0 ? -EPROTO : err;
2123 
2124 		if (err < msg.size && err < ret) {
2125 			drm_dbg_kms(aux->drm_dev,
2126 				    "%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
2127 				    aux->name, msg.size, err);
2128 			ret = err;
2129 		}
2130 
2131 		msg.size -= err;
2132 		msg.buffer += err;
2133 	}
2134 
2135 	return ret;
2136 }
2137 
2138 /*
2139  * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
2140  * packets to be as large as possible. If not, the I2C transactions never
2141  * succeed. Hence the default is maximum.
2142  */
2143 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
2144 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
2145 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
2146 		 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
2147 
2148 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
2149 			   int num)
2150 {
2151 	struct drm_dp_aux *aux = adapter->algo_data;
2152 	unsigned int i, j;
2153 	unsigned transfer_size;
2154 	struct drm_dp_aux_msg msg;
2155 	int err = 0;
2156 
2157 	if (aux->powered_down)
2158 		return -EBUSY;
2159 
2160 	dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
2161 
2162 	memset(&msg, 0, sizeof(msg));
2163 
2164 	for (i = 0; i < num; i++) {
2165 		msg.address = msgs[i].addr;
2166 
2167 		if (!aux->no_zero_sized) {
2168 			drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
2169 			/* Send a bare address packet to start the transaction.
2170 			 * Zero sized messages specify an address only (bare
2171 			 * address) transaction.
2172 			 */
2173 			msg.buffer = NULL;
2174 			msg.size = 0;
2175 			err = drm_dp_i2c_do_msg(aux, &msg);
2176 		}
2177 
2178 		/*
2179 		 * Reset msg.request in case in case it got
2180 		 * changed into a WRITE_STATUS_UPDATE.
2181 		 */
2182 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
2183 
2184 		if (err < 0)
2185 			break;
2186 		/* We want each transaction to be as large as possible, but
2187 		 * we'll go to smaller sizes if the hardware gives us a
2188 		 * short reply.
2189 		 */
2190 		transfer_size = dp_aux_i2c_transfer_size;
2191 		for (j = 0; j < msgs[i].len; j += msg.size) {
2192 			msg.buffer = msgs[i].buf + j;
2193 			msg.size = min(transfer_size, msgs[i].len - j);
2194 
2195 			if (aux->no_zero_sized && i == num - 1 &&
2196 			    j + msg.size == msgs[i].len)
2197 				msg.request &= ~DP_AUX_I2C_MOT;
2198 			err = drm_dp_i2c_drain_msg(aux, &msg);
2199 
2200 			/*
2201 			 * Reset msg.request in case in case it got
2202 			 * changed into a WRITE_STATUS_UPDATE.
2203 			 */
2204 			drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
2205 
2206 			if (err < 0)
2207 				break;
2208 			transfer_size = err;
2209 		}
2210 		if (err < 0)
2211 			break;
2212 	}
2213 	if (err >= 0)
2214 		err = num;
2215 
2216 	if (!aux->no_zero_sized) {
2217 		/* Send a bare address packet to close out the transaction.
2218 		 * Zero sized messages specify an address only (bare
2219 		 * address) transaction.
2220 		 */
2221 		msg.request &= ~DP_AUX_I2C_MOT;
2222 		msg.buffer = NULL;
2223 		msg.size = 0;
2224 		(void)drm_dp_i2c_do_msg(aux, &msg);
2225 	}
2226 	return err;
2227 }
2228 
2229 static const struct i2c_algorithm drm_dp_i2c_algo = {
2230 	.functionality = drm_dp_i2c_functionality,
2231 	.master_xfer = drm_dp_i2c_xfer,
2232 };
2233 
2234 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
2235 {
2236 	return container_of(i2c, struct drm_dp_aux, ddc);
2237 }
2238 
2239 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
2240 {
2241 	mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
2242 }
2243 
2244 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
2245 {
2246 	return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
2247 }
2248 
2249 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
2250 {
2251 	mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
2252 }
2253 
2254 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
2255 	.lock_bus = lock_bus,
2256 	.trylock_bus = trylock_bus,
2257 	.unlock_bus = unlock_bus,
2258 };
2259 
2260 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
2261 {
2262 	u8 buf, count;
2263 	int ret;
2264 
2265 	ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK, &buf);
2266 	if (ret < 0)
2267 		return ret;
2268 
2269 	WARN_ON(!(buf & DP_TEST_SINK_START));
2270 
2271 	ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK_MISC, &buf);
2272 	if (ret < 0)
2273 		return ret;
2274 
2275 	count = buf & DP_TEST_COUNT_MASK;
2276 	if (count == aux->crc_count)
2277 		return -EAGAIN; /* No CRC yet */
2278 
2279 	aux->crc_count = count;
2280 
2281 	/*
2282 	 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
2283 	 * per component (RGB or CrYCb).
2284 	 */
2285 	return drm_dp_dpcd_read_data(aux, DP_TEST_CRC_R_CR, crc, 6);
2286 }
2287 
2288 static void drm_dp_aux_crc_work(struct work_struct *work)
2289 {
2290 	struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
2291 					      crc_work);
2292 	struct drm_crtc *crtc;
2293 	u8 crc_bytes[6];
2294 	uint32_t crcs[3];
2295 	int ret;
2296 
2297 	if (WARN_ON(!aux->crtc))
2298 		return;
2299 
2300 	crtc = aux->crtc;
2301 	while (crtc->crc.opened) {
2302 		drm_crtc_wait_one_vblank(crtc);
2303 		if (!crtc->crc.opened)
2304 			break;
2305 
2306 		ret = drm_dp_aux_get_crc(aux, crc_bytes);
2307 		if (ret == -EAGAIN) {
2308 			usleep_range(1000, 2000);
2309 			ret = drm_dp_aux_get_crc(aux, crc_bytes);
2310 		}
2311 
2312 		if (ret == -EAGAIN) {
2313 			drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",
2314 				    aux->name, ret);
2315 			continue;
2316 		} else if (ret) {
2317 			drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);
2318 			continue;
2319 		}
2320 
2321 		crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
2322 		crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
2323 		crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
2324 		drm_crtc_add_crc_entry(crtc, false, 0, crcs);
2325 	}
2326 }
2327 
2328 /**
2329  * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
2330  * @aux: DisplayPort AUX channel
2331  *
2332  * Used for remote aux channel in general. Merely initialize the crc work
2333  * struct.
2334  */
2335 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
2336 {
2337 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
2338 }
2339 EXPORT_SYMBOL(drm_dp_remote_aux_init);
2340 
2341 /**
2342  * drm_dp_aux_init() - minimally initialise an aux channel
2343  * @aux: DisplayPort AUX channel
2344  *
2345  * If you need to use the drm_dp_aux's i2c adapter prior to registering it with
2346  * the outside world, call drm_dp_aux_init() first. For drivers which are
2347  * grandparents to their AUX adapters (e.g. the AUX adapter is parented by a
2348  * &drm_connector), you must still call drm_dp_aux_register() once the connector
2349  * has been registered to allow userspace access to the auxiliary DP channel.
2350  * Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as
2351  * early as possible so that the &drm_device that corresponds to the AUX adapter
2352  * may be mentioned in debugging output from the DRM DP helpers.
2353  *
2354  * For devices which use a separate platform device for their AUX adapters, this
2355  * may be called as early as required by the driver.
2356  *
2357  */
2358 void drm_dp_aux_init(struct drm_dp_aux *aux)
2359 {
2360 	mutex_init(&aux->hw_mutex);
2361 	mutex_init(&aux->cec.lock);
2362 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
2363 
2364 	aux->ddc.algo = &drm_dp_i2c_algo;
2365 	aux->ddc.algo_data = aux;
2366 	aux->ddc.retries = 3;
2367 
2368 	aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
2369 }
2370 EXPORT_SYMBOL(drm_dp_aux_init);
2371 
2372 /**
2373  * drm_dp_aux_register() - initialise and register aux channel
2374  * @aux: DisplayPort AUX channel
2375  *
2376  * Automatically calls drm_dp_aux_init() if this hasn't been done yet. This
2377  * should only be called once the parent of @aux, &drm_dp_aux.dev, is
2378  * initialized. For devices which are grandparents of their AUX channels,
2379  * &drm_dp_aux.dev will typically be the &drm_connector &device which
2380  * corresponds to @aux. For these devices, it's advised to call
2381  * drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to
2382  * call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.
2383  * Functions which don't follow this will likely Oops when
2384  * %CONFIG_DRM_DISPLAY_DP_AUX_CHARDEV is enabled.
2385  *
2386  * For devices where the AUX channel is a device that exists independently of
2387  * the &drm_device that uses it, such as SoCs and bridge devices, it is
2388  * recommended to call drm_dp_aux_register() after a &drm_device has been
2389  * assigned to &drm_dp_aux.drm_dev, and likewise to call
2390  * drm_dp_aux_unregister() once the &drm_device should no longer be associated
2391  * with the AUX channel (e.g. on bridge detach).
2392  *
2393  * Drivers which need to use the aux channel before either of the two points
2394  * mentioned above need to call drm_dp_aux_init() in order to use the AUX
2395  * channel before registration.
2396  *
2397  * Returns 0 on success or a negative error code on failure.
2398  */
2399 int drm_dp_aux_register(struct drm_dp_aux *aux)
2400 {
2401 	int ret;
2402 
2403 	WARN_ON_ONCE(!aux->drm_dev);
2404 
2405 	if (!aux->ddc.algo)
2406 		drm_dp_aux_init(aux);
2407 
2408 	aux->ddc.owner = THIS_MODULE;
2409 	aux->ddc.dev.parent = aux->dev;
2410 
2411 	strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
2412 		sizeof(aux->ddc.name));
2413 
2414 	ret = drm_dp_aux_register_devnode(aux);
2415 	if (ret)
2416 		return ret;
2417 
2418 	ret = i2c_add_adapter(&aux->ddc);
2419 	if (ret) {
2420 		drm_dp_aux_unregister_devnode(aux);
2421 		return ret;
2422 	}
2423 
2424 	return 0;
2425 }
2426 EXPORT_SYMBOL(drm_dp_aux_register);
2427 
2428 /**
2429  * drm_dp_aux_unregister() - unregister an AUX adapter
2430  * @aux: DisplayPort AUX channel
2431  */
2432 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
2433 {
2434 	drm_dp_aux_unregister_devnode(aux);
2435 	i2c_del_adapter(&aux->ddc);
2436 }
2437 EXPORT_SYMBOL(drm_dp_aux_unregister);
2438 
2439 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
2440 
2441 /**
2442  * drm_dp_psr_setup_time() - PSR setup in time usec
2443  * @psr_cap: PSR capabilities from DPCD
2444  *
2445  * Returns:
2446  * PSR setup time for the panel in microseconds,  negative
2447  * error code on failure.
2448  */
2449 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
2450 {
2451 	static const u16 psr_setup_time_us[] = {
2452 		PSR_SETUP_TIME(330),
2453 		PSR_SETUP_TIME(275),
2454 		PSR_SETUP_TIME(220),
2455 		PSR_SETUP_TIME(165),
2456 		PSR_SETUP_TIME(110),
2457 		PSR_SETUP_TIME(55),
2458 		PSR_SETUP_TIME(0),
2459 	};
2460 	int i;
2461 
2462 	i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
2463 	if (i >= ARRAY_SIZE(psr_setup_time_us))
2464 		return -EINVAL;
2465 
2466 	return psr_setup_time_us[i];
2467 }
2468 EXPORT_SYMBOL(drm_dp_psr_setup_time);
2469 
2470 #undef PSR_SETUP_TIME
2471 
2472 /**
2473  * drm_dp_start_crc() - start capture of frame CRCs
2474  * @aux: DisplayPort AUX channel
2475  * @crtc: CRTC displaying the frames whose CRCs are to be captured
2476  *
2477  * Returns 0 on success or a negative error code on failure.
2478  */
2479 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
2480 {
2481 	u8 buf;
2482 	int ret;
2483 
2484 	ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK, &buf);
2485 	if (ret < 0)
2486 		return ret;
2487 
2488 	ret = drm_dp_dpcd_write_byte(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
2489 	if (ret < 0)
2490 		return ret;
2491 
2492 	aux->crc_count = 0;
2493 	aux->crtc = crtc;
2494 	schedule_work(&aux->crc_work);
2495 
2496 	return 0;
2497 }
2498 EXPORT_SYMBOL(drm_dp_start_crc);
2499 
2500 /**
2501  * drm_dp_stop_crc() - stop capture of frame CRCs
2502  * @aux: DisplayPort AUX channel
2503  *
2504  * Returns 0 on success or a negative error code on failure.
2505  */
2506 int drm_dp_stop_crc(struct drm_dp_aux *aux)
2507 {
2508 	u8 buf;
2509 	int ret;
2510 
2511 	ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK, &buf);
2512 	if (ret < 0)
2513 		return ret;
2514 
2515 	ret = drm_dp_dpcd_write_byte(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
2516 	if (ret < 0)
2517 		return ret;
2518 
2519 	flush_work(&aux->crc_work);
2520 	aux->crtc = NULL;
2521 
2522 	return 0;
2523 }
2524 EXPORT_SYMBOL(drm_dp_stop_crc);
2525 
2526 struct dpcd_quirk {
2527 	u8 oui[3];
2528 	u8 device_id[6];
2529 	bool is_branch;
2530 	u32 quirks;
2531 };
2532 
2533 #define OUI(first, second, third) { (first), (second), (third) }
2534 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
2535 	{ (first), (second), (third), (fourth), (fifth), (sixth) }
2536 
2537 #define DEVICE_ID_ANY	DEVICE_ID(0, 0, 0, 0, 0, 0)
2538 
2539 static const struct dpcd_quirk dpcd_quirk_list[] = {
2540 	/* Analogix 7737 needs reduced M and N at HBR2 link rates */
2541 	{ OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
2542 	/* LG LP140WF6-SPM1 eDP panel */
2543 	{ OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
2544 	/* Apple panels need some additional handling to support PSR */
2545 	{ OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
2546 	/* CH7511 seems to leave SINK_COUNT zeroed */
2547 	{ OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
2548 	/* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
2549 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
2550 	/* Realtek DP1.4 MST hubs can support DSC without virtual DPCD */
2551 	{ OUI(0x00, 0xe0, 0x4c), DEVICE_ID('D', 'p', '1', '.', '4', 0), true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
2552 	/* Synaptics DP1.4 MST hubs require DSC for some modes on which it applies HBLANK expansion. */
2553 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC) },
2554 	/* MediaTek panels (at least in U3224KBA) require DSC for modes with a short HBLANK on UHBR links. */
2555 	{ OUI(0x00, 0x0C, 0xE7), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC) },
2556 	/* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
2557 	{ OUI(0x00, 0x10, 0xfa), DEVICE_ID(101, 68, 21, 101, 98, 97), false, BIT(DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS) },
2558 	/* Synaptics Panamera supports only a compressed bpp of 12 above 50% of its max DSC pixel throughput */
2559 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID('S', 'Y', 'N', 'A', 0x53, 0x22), true, BIT(DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) },
2560 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID('S', 'Y', 'N', 'A', 0x53, 0x31), true, BIT(DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) },
2561 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID('S', 'Y', 'N', 'A', 0x53, 0x33), true, BIT(DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) },
2562 };
2563 
2564 #undef OUI
2565 
2566 /*
2567  * Get a bit mask of DPCD quirks for the sink/branch device identified by
2568  * ident. The quirk data is shared but it's up to the drivers to act on the
2569  * data.
2570  *
2571  * For now, only the OUI (first three bytes) is used, but this may be extended
2572  * to device identification string and hardware/firmware revisions later.
2573  */
2574 static u32
2575 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
2576 {
2577 	const struct dpcd_quirk *quirk;
2578 	u32 quirks = 0;
2579 	int i;
2580 	u8 any_device[] = DEVICE_ID_ANY;
2581 
2582 	for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
2583 		quirk = &dpcd_quirk_list[i];
2584 
2585 		if (quirk->is_branch != is_branch)
2586 			continue;
2587 
2588 		if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
2589 			continue;
2590 
2591 		if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
2592 		    memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
2593 			continue;
2594 
2595 		quirks |= quirk->quirks;
2596 	}
2597 
2598 	return quirks;
2599 }
2600 
2601 #undef DEVICE_ID_ANY
2602 #undef DEVICE_ID
2603 
2604 static int drm_dp_read_ident(struct drm_dp_aux *aux, unsigned int offset,
2605 			     struct drm_dp_dpcd_ident *ident)
2606 {
2607 	return drm_dp_dpcd_read_data(aux, offset, ident, sizeof(*ident));
2608 }
2609 
2610 static void drm_dp_dump_desc(struct drm_dp_aux *aux,
2611 			     const char *device_name, const struct drm_dp_desc *desc)
2612 {
2613 	const struct drm_dp_dpcd_ident *ident = &desc->ident;
2614 
2615 	drm_dbg_kms(aux->drm_dev,
2616 		    "%s: %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
2617 		    aux->name, device_name,
2618 		    (int)sizeof(ident->oui), ident->oui,
2619 		    (int)strnlen(ident->device_id, sizeof(ident->device_id)), ident->device_id,
2620 		    ident->hw_rev >> 4, ident->hw_rev & 0xf,
2621 		    ident->sw_major_rev, ident->sw_minor_rev,
2622 		    desc->quirks);
2623 }
2624 
2625 /**
2626  * drm_dp_read_desc - read sink/branch descriptor from DPCD
2627  * @aux: DisplayPort AUX channel
2628  * @desc: Device descriptor to fill from DPCD
2629  * @is_branch: true for branch devices, false for sink devices
2630  *
2631  * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
2632  * identification.
2633  *
2634  * Returns 0 on success or a negative error code on failure.
2635  */
2636 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
2637 		     bool is_branch)
2638 {
2639 	struct drm_dp_dpcd_ident *ident = &desc->ident;
2640 	unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
2641 	int ret;
2642 
2643 	ret = drm_dp_read_ident(aux, offset, ident);
2644 	if (ret < 0)
2645 		return ret;
2646 
2647 	desc->quirks = drm_dp_get_quirks(ident, is_branch);
2648 
2649 	drm_dp_dump_desc(aux, is_branch ? "DP branch" : "DP sink", desc);
2650 
2651 	return 0;
2652 }
2653 EXPORT_SYMBOL(drm_dp_read_desc);
2654 
2655 /**
2656  * drm_dp_dump_lttpr_desc - read and dump the DPCD descriptor for an LTTPR PHY
2657  * @aux: DisplayPort AUX channel
2658  * @dp_phy: LTTPR PHY instance
2659  *
2660  * Read the DPCD LTTPR PHY descriptor for @dp_phy and print a debug message
2661  * with its details to dmesg.
2662  *
2663  * Returns 0 on success or a negative error code on failure.
2664  */
2665 int drm_dp_dump_lttpr_desc(struct drm_dp_aux *aux, enum drm_dp_phy dp_phy)
2666 {
2667 	struct drm_dp_desc desc = {};
2668 	int ret;
2669 
2670 	if (drm_WARN_ON(aux->drm_dev, dp_phy < DP_PHY_LTTPR1 || dp_phy > DP_MAX_LTTPR_COUNT))
2671 		return -EINVAL;
2672 
2673 	ret = drm_dp_read_ident(aux, DP_OUI_PHY_REPEATER(dp_phy), &desc.ident);
2674 	if (ret < 0)
2675 		return ret;
2676 
2677 	drm_dp_dump_desc(aux, drm_dp_phy_name(dp_phy), &desc);
2678 
2679 	return 0;
2680 }
2681 EXPORT_SYMBOL(drm_dp_dump_lttpr_desc);
2682 
2683 /**
2684  * drm_dp_dsc_sink_bpp_incr() - Get bits per pixel increment
2685  * @dsc_dpcd: DSC capabilities from DPCD
2686  *
2687  * Returns the bpp precision supported by the DP sink.
2688  */
2689 u8 drm_dp_dsc_sink_bpp_incr(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2690 {
2691 	u8 bpp_increment_dpcd = dsc_dpcd[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT];
2692 
2693 	switch (bpp_increment_dpcd & DP_DSC_BITS_PER_PIXEL_MASK) {
2694 	case DP_DSC_BITS_PER_PIXEL_1_16:
2695 		return 16;
2696 	case DP_DSC_BITS_PER_PIXEL_1_8:
2697 		return 8;
2698 	case DP_DSC_BITS_PER_PIXEL_1_4:
2699 		return 4;
2700 	case DP_DSC_BITS_PER_PIXEL_1_2:
2701 		return 2;
2702 	case DP_DSC_BITS_PER_PIXEL_1_1:
2703 		return 1;
2704 	}
2705 
2706 	return 0;
2707 }
2708 EXPORT_SYMBOL(drm_dp_dsc_sink_bpp_incr);
2709 
2710 /**
2711  * drm_dp_dsc_slice_count_to_mask() - Convert a slice count to a slice count mask
2712  * @slice_count: slice count
2713  *
2714  * Convert @slice_count to a slice count mask.
2715  *
2716  * Returns the slice count mask.
2717  */
2718 u32 drm_dp_dsc_slice_count_to_mask(int slice_count)
2719 {
2720 	return BIT(slice_count - 1);
2721 }
2722 EXPORT_SYMBOL(drm_dp_dsc_slice_count_to_mask);
2723 
2724 /**
2725  * drm_dp_dsc_sink_slice_count_mask() - Get the mask of valid DSC sink slice counts
2726  * @dsc_dpcd: the sink's DSC DPCD capabilities
2727  * @is_edp: %true for an eDP sink
2728  *
2729  * Get the mask of supported slice counts from the sink's DSC DPCD register.
2730  *
2731  * Returns:
2732  * Mask of slice counts supported by the DSC sink:
2733  * - > 0: bit#0,1,3,5..,23 set if the sink supports 1,2,4,6..,24 slices
2734  * - 0:   if the sink doesn't support any slices
2735  */
2736 u32 drm_dp_dsc_sink_slice_count_mask(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2737 				     bool is_edp)
2738 {
2739 	u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
2740 	u32 mask = 0;
2741 
2742 	if (!is_edp) {
2743 		/* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2744 		u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2745 
2746 		if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2747 			mask |= drm_dp_dsc_slice_count_to_mask(24);
2748 		if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2749 			mask |= drm_dp_dsc_slice_count_to_mask(20);
2750 		if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2751 			mask |= drm_dp_dsc_slice_count_to_mask(16);
2752 	}
2753 
2754 	/* DP, eDP v1.5+ */
2755 	if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2756 		mask |= drm_dp_dsc_slice_count_to_mask(12);
2757 	if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2758 		mask |= drm_dp_dsc_slice_count_to_mask(10);
2759 	if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2760 		mask |= drm_dp_dsc_slice_count_to_mask(8);
2761 	if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2762 		mask |= drm_dp_dsc_slice_count_to_mask(6);
2763 	/* DP, eDP v1.4+ */
2764 	if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2765 		mask |= drm_dp_dsc_slice_count_to_mask(4);
2766 	if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2767 		mask |= drm_dp_dsc_slice_count_to_mask(2);
2768 	if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2769 		mask |= drm_dp_dsc_slice_count_to_mask(1);
2770 
2771 	return mask;
2772 }
2773 EXPORT_SYMBOL(drm_dp_dsc_sink_slice_count_mask);
2774 
2775 /**
2776  * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
2777  * supported by the DSC sink.
2778  * @dsc_dpcd: DSC capabilities from DPCD
2779  * @is_edp: true if its eDP, false for DP
2780  *
2781  * Read the slice capabilities DPCD register from DSC sink to get
2782  * the maximum slice count supported. This is used to populate
2783  * the DSC parameters in the &struct drm_dsc_config by the driver.
2784  * Driver creates an infoframe using these parameters to populate
2785  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2786  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2787  *
2788  * Returns:
2789  * Maximum slice count supported by DSC sink or 0 its invalid
2790  */
2791 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2792 				   bool is_edp)
2793 {
2794 	return fls(drm_dp_dsc_sink_slice_count_mask(dsc_dpcd, is_edp));
2795 }
2796 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2797 
2798 /**
2799  * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2800  * @dsc_dpcd: DSC capabilities from DPCD
2801  *
2802  * Read the DSC DPCD register to parse the line buffer depth in bits which is
2803  * number of bits of precision within the decoder line buffer supported by
2804  * the DSC sink. This is used to populate the DSC parameters in the
2805  * &struct drm_dsc_config by the driver.
2806  * Driver creates an infoframe using these parameters to populate
2807  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2808  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2809  *
2810  * Returns:
2811  * Line buffer depth supported by DSC panel or 0 its invalid
2812  */
2813 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2814 {
2815 	u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2816 
2817 	switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2818 	case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2819 		return 9;
2820 	case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2821 		return 10;
2822 	case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2823 		return 11;
2824 	case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2825 		return 12;
2826 	case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2827 		return 13;
2828 	case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2829 		return 14;
2830 	case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2831 		return 15;
2832 	case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2833 		return 16;
2834 	case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2835 		return 8;
2836 	}
2837 
2838 	return 0;
2839 }
2840 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2841 
2842 /**
2843  * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2844  * values supported by the DSC sink.
2845  * @dsc_dpcd: DSC capabilities from DPCD
2846  * @dsc_bpc: An array to be filled by this helper with supported
2847  *           input bpcs.
2848  *
2849  * Read the DSC DPCD from the sink device to parse the supported bits per
2850  * component values. This is used to populate the DSC parameters
2851  * in the &struct drm_dsc_config by the driver.
2852  * Driver creates an infoframe using these parameters to populate
2853  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2854  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2855  *
2856  * Returns:
2857  * Number of input BPC values parsed from the DPCD
2858  */
2859 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2860 					 u8 dsc_bpc[3])
2861 {
2862 	int num_bpc = 0;
2863 	u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2864 
2865 	if (!drm_dp_sink_supports_dsc(dsc_dpcd))
2866 		return 0;
2867 
2868 	if (color_depth & DP_DSC_12_BPC)
2869 		dsc_bpc[num_bpc++] = 12;
2870 	if (color_depth & DP_DSC_10_BPC)
2871 		dsc_bpc[num_bpc++] = 10;
2872 
2873 	/* A DP DSC Sink device shall support 8 bpc. */
2874 	dsc_bpc[num_bpc++] = 8;
2875 
2876 	return num_bpc;
2877 }
2878 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2879 
2880 /*
2881  * See DP Standard v2.1a 2.8.4 Minimum Slices/Display, Table 2-159 and
2882  * Appendix L.1 Derivation of Slice Count Requirements.
2883  */
2884 static int dsc_sink_min_slice_throughput(int peak_pixel_rate)
2885 {
2886 	if (peak_pixel_rate >= 4800000)
2887 		return 600000;
2888 	else if (peak_pixel_rate >= 2700000)
2889 		return 400000;
2890 	else
2891 		return 340000;
2892 }
2893 
2894 /**
2895  * drm_dp_dsc_sink_max_slice_throughput() - Get a DSC sink's maximum pixel throughput per slice
2896  * @dsc_dpcd: DSC sink's capabilities from DPCD
2897  * @peak_pixel_rate: Cumulative peak pixel rate in kHz
2898  * @is_rgb_yuv444: The mode is either RGB or YUV444
2899  *
2900  * Return the DSC sink device's maximum pixel throughput per slice, based on
2901  * the device's @dsc_dpcd capabilities, the @peak_pixel_rate of the transferred
2902  * stream(s) and whether the output format @is_rgb_yuv444 or yuv422/yuv420.
2903  *
2904  * Note that @peak_pixel_rate is the total pixel rate transferred to the same
2905  * DSC/display sink. For instance to calculate a tile's slice count of an MST
2906  * multi-tiled display sink (not considering here the required
2907  * rounding/alignment of slice count)::
2908  *
2909  *   @peak_pixel_rate = tile_pixel_rate * tile_count
2910  *   total_slice_count = @peak_pixel_rate / drm_dp_dsc_sink_max_slice_throughput(@peak_pixel_rate)
2911  *   tile_slice_count = total_slice_count / tile_count
2912  *
2913  * Returns:
2914  * The maximum pixel throughput per slice supported by the DSC sink device
2915  * in kPixels/sec.
2916  */
2917 int drm_dp_dsc_sink_max_slice_throughput(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2918 					 int peak_pixel_rate, bool is_rgb_yuv444)
2919 {
2920 	int throughput;
2921 	int delta = 0;
2922 	int base;
2923 
2924 	throughput = dsc_dpcd[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];
2925 
2926 	if (is_rgb_yuv444) {
2927 		throughput = (throughput & DP_DSC_THROUGHPUT_MODE_0_MASK) >>
2928 			     DP_DSC_THROUGHPUT_MODE_0_SHIFT;
2929 
2930 		delta = ((dsc_dpcd[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT]) &
2931 			 DP_DSC_THROUGHPUT_MODE_0_DELTA_MASK) >>
2932 			DP_DSC_THROUGHPUT_MODE_0_DELTA_SHIFT;	/* in units of 2 MPixels/sec */
2933 		delta *= 2000;
2934 	} else {
2935 		throughput = (throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >>
2936 			     DP_DSC_THROUGHPUT_MODE_1_SHIFT;
2937 	}
2938 
2939 	switch (throughput) {
2940 	case 0:
2941 		return dsc_sink_min_slice_throughput(peak_pixel_rate);
2942 	case 1:
2943 		base = 340000;
2944 		break;
2945 	case 2 ... 14:
2946 		base = 400000 + 50000 * (throughput - 2);
2947 		break;
2948 	case 15:
2949 		base = 170000;
2950 		break;
2951 	}
2952 
2953 	return base + delta;
2954 }
2955 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_throughput);
2956 
2957 static u8 dsc_branch_dpcd_cap(const u8 dpcd[DP_DSC_BRANCH_CAP_SIZE], int reg)
2958 {
2959 	return dpcd[reg - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
2960 }
2961 
2962 /**
2963  * drm_dp_dsc_branch_max_overall_throughput() - Branch device's max overall DSC pixel throughput
2964  * @dsc_branch_dpcd: DSC branch capabilities from DPCD
2965  * @is_rgb_yuv444: The mode is either RGB or YUV444
2966  *
2967  * Return the branch device's maximum overall DSC pixel throughput, based on
2968  * the device's DPCD DSC branch capabilities, and whether the output
2969  * format @is_rgb_yuv444 or yuv422/yuv420.
2970  *
2971  * Returns:
2972  * - 0:   The maximum overall throughput capability is not indicated by
2973  *        the device separately and it must be determined from the per-slice
2974  *        max throughput (see @drm_dp_dsc_branch_slice_max_throughput())
2975  *        and the maximum slice count supported by the device.
2976  * - > 0: The maximum overall DSC pixel throughput supported by the branch
2977  *        device in kPixels/sec.
2978  */
2979 int drm_dp_dsc_branch_max_overall_throughput(const u8 dsc_branch_dpcd[DP_DSC_BRANCH_CAP_SIZE],
2980 					     bool is_rgb_yuv444)
2981 {
2982 	int throughput;
2983 
2984 	if (is_rgb_yuv444)
2985 		throughput = dsc_branch_dpcd_cap(dsc_branch_dpcd,
2986 						 DP_DSC_BRANCH_OVERALL_THROUGHPUT_0);
2987 	else
2988 		throughput = dsc_branch_dpcd_cap(dsc_branch_dpcd,
2989 						 DP_DSC_BRANCH_OVERALL_THROUGHPUT_1);
2990 
2991 	switch (throughput) {
2992 	case 0:
2993 		return 0;
2994 	case 1:
2995 		return 680000;
2996 	default:
2997 		return 600000 + 50000 * throughput;
2998 	}
2999 }
3000 EXPORT_SYMBOL(drm_dp_dsc_branch_max_overall_throughput);
3001 
3002 /**
3003  * drm_dp_dsc_branch_max_line_width() - Branch device's max DSC line width
3004  * @dsc_branch_dpcd: DSC branch capabilities from DPCD
3005  *
3006  * Return the branch device's maximum overall DSC line width, based on
3007  * the device's @dsc_branch_dpcd capabilities.
3008  *
3009  * Returns:
3010  * - 0:        The maximum line width is not indicated by the device
3011  *             separately and it must be determined from the maximum
3012  *             slice count and slice-width supported by the device.
3013  * - %-EINVAL: The device indicates an invalid maximum line width
3014  *             (< 5120 pixels).
3015  * - >= 5120:  The maximum line width in pixels.
3016  */
3017 int drm_dp_dsc_branch_max_line_width(const u8 dsc_branch_dpcd[DP_DSC_BRANCH_CAP_SIZE])
3018 {
3019 	int line_width = dsc_branch_dpcd_cap(dsc_branch_dpcd, DP_DSC_BRANCH_MAX_LINE_WIDTH);
3020 
3021 	switch (line_width) {
3022 	case 0:
3023 		return 0;
3024 	case 1 ... 15:
3025 		return -EINVAL;
3026 	default:
3027 		return line_width * 320;
3028 	}
3029 }
3030 EXPORT_SYMBOL(drm_dp_dsc_branch_max_line_width);
3031 
3032 static int drm_dp_read_lttpr_regs(struct drm_dp_aux *aux,
3033 				  const u8 dpcd[DP_RECEIVER_CAP_SIZE], int address,
3034 				  u8 *buf, int buf_size)
3035 {
3036 	/*
3037 	 * At least the DELL P2715Q monitor with a DPCD_REV < 0x14 returns
3038 	 * corrupted values when reading from the 0xF0000- range with a block
3039 	 * size bigger than 1.
3040 	 */
3041 	int block_size = dpcd[DP_DPCD_REV] < 0x14 ? 1 : buf_size;
3042 	int offset;
3043 	int ret;
3044 
3045 	for (offset = 0; offset < buf_size; offset += block_size) {
3046 		ret = drm_dp_dpcd_read_data(aux,
3047 					    address + offset,
3048 					    &buf[offset], block_size);
3049 		if (ret < 0)
3050 			return ret;
3051 	}
3052 
3053 	return 0;
3054 }
3055 
3056 /**
3057  * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
3058  * @aux: DisplayPort AUX channel
3059  * @dpcd: DisplayPort configuration data
3060  * @caps: buffer to return the capability info in
3061  *
3062  * Read capabilities common to all LTTPRs.
3063  *
3064  * Returns 0 on success or a negative error code on failure.
3065  */
3066 int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
3067 				  const u8 dpcd[DP_RECEIVER_CAP_SIZE],
3068 				  u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
3069 {
3070 	return drm_dp_read_lttpr_regs(aux, dpcd,
3071 				      DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
3072 				      caps, DP_LTTPR_COMMON_CAP_SIZE);
3073 }
3074 EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
3075 
3076 /**
3077  * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
3078  * @aux: DisplayPort AUX channel
3079  * @dpcd: DisplayPort configuration data
3080  * @dp_phy: LTTPR PHY to read the capabilities for
3081  * @caps: buffer to return the capability info in
3082  *
3083  * Read the capabilities for the given LTTPR PHY.
3084  *
3085  * Returns 0 on success or a negative error code on failure.
3086  */
3087 int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
3088 			       const u8 dpcd[DP_RECEIVER_CAP_SIZE],
3089 			       enum drm_dp_phy dp_phy,
3090 			       u8 caps[DP_LTTPR_PHY_CAP_SIZE])
3091 {
3092 	return drm_dp_read_lttpr_regs(aux, dpcd,
3093 				      DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
3094 				      caps, DP_LTTPR_PHY_CAP_SIZE);
3095 }
3096 EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
3097 
3098 static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
3099 {
3100 	return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
3101 }
3102 
3103 /**
3104  * drm_dp_lttpr_count - get the number of detected LTTPRs
3105  * @caps: LTTPR common capabilities
3106  *
3107  * Get the number of detected LTTPRs from the LTTPR common capabilities info.
3108  *
3109  * Returns:
3110  *   -ERANGE if more than supported number (8) of LTTPRs are detected
3111  *   -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
3112  *   otherwise the number of detected LTTPRs
3113  */
3114 int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
3115 {
3116 	u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
3117 
3118 	switch (hweight8(count)) {
3119 	case 0:
3120 		return 0;
3121 	case 1:
3122 		return 8 - ilog2(count);
3123 	case 8:
3124 		return -ERANGE;
3125 	default:
3126 		return -EINVAL;
3127 	}
3128 }
3129 EXPORT_SYMBOL(drm_dp_lttpr_count);
3130 
3131 /**
3132  * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
3133  * @caps: LTTPR common capabilities
3134  *
3135  * Returns the maximum link rate supported by all detected LTTPRs.
3136  */
3137 int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
3138 {
3139 	u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
3140 
3141 	return drm_dp_bw_code_to_link_rate(rate);
3142 }
3143 EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
3144 
3145 /**
3146  * drm_dp_lttpr_set_transparent_mode() - set the LTTPR in transparent mode
3147  * @aux: DisplayPort AUX channel
3148  * @enable: Enable or disable transparent mode
3149  *
3150  * Returns: 0 on success or a negative error code on failure.
3151  */
3152 int drm_dp_lttpr_set_transparent_mode(struct drm_dp_aux *aux, bool enable)
3153 {
3154 	u8 val = enable ? DP_PHY_REPEATER_MODE_TRANSPARENT :
3155 			  DP_PHY_REPEATER_MODE_NON_TRANSPARENT;
3156 	int ret = drm_dp_dpcd_writeb(aux, DP_PHY_REPEATER_MODE, val);
3157 
3158 	if (ret < 0)
3159 		return ret;
3160 
3161 	return (ret == 1) ? 0 : -EIO;
3162 }
3163 EXPORT_SYMBOL(drm_dp_lttpr_set_transparent_mode);
3164 
3165 /**
3166  * drm_dp_lttpr_init() - init LTTPR transparency mode according to DP standard
3167  * @aux: DisplayPort AUX channel
3168  * @lttpr_count: Number of LTTPRs. Between 0 and 8, according to DP standard.
3169  *               Negative error code for any non-valid number.
3170  *               See drm_dp_lttpr_count().
3171  *
3172  * Returns: 0 on success or a negative error code on failure.
3173  */
3174 int drm_dp_lttpr_init(struct drm_dp_aux *aux, int lttpr_count)
3175 {
3176 	int ret;
3177 
3178 	if (!lttpr_count)
3179 		return 0;
3180 
3181 	/*
3182 	 * See DP Standard v2.0 3.6.6.1 about the explicit disabling of
3183 	 * non-transparent mode and the disable->enable non-transparent mode
3184 	 * sequence.
3185 	 */
3186 	ret = drm_dp_lttpr_set_transparent_mode(aux, true);
3187 	if (ret)
3188 		return ret;
3189 
3190 	if (lttpr_count < 0)
3191 		return -ENODEV;
3192 
3193 	if (drm_dp_lttpr_set_transparent_mode(aux, false)) {
3194 		/*
3195 		 * Roll-back to transparent mode if setting non-transparent
3196 		 * mode has failed
3197 		 */
3198 		drm_dp_lttpr_set_transparent_mode(aux, true);
3199 		return -EINVAL;
3200 	}
3201 
3202 	return 0;
3203 }
3204 EXPORT_SYMBOL(drm_dp_lttpr_init);
3205 
3206 /**
3207  * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
3208  * @caps: LTTPR common capabilities
3209  *
3210  * Returns the maximum lane count supported by all detected LTTPRs.
3211  */
3212 int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
3213 {
3214 	u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
3215 
3216 	return max_lanes & DP_MAX_LANE_COUNT_MASK;
3217 }
3218 EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
3219 
3220 /**
3221  * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
3222  * @caps: LTTPR PHY capabilities
3223  *
3224  * Returns true if the @caps for an LTTPR TX PHY indicate support for
3225  * voltage swing level 3.
3226  */
3227 bool
3228 drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
3229 {
3230 	u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
3231 
3232 	return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
3233 }
3234 EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
3235 
3236 /**
3237  * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
3238  * @caps: LTTPR PHY capabilities
3239  *
3240  * Returns true if the @caps for an LTTPR TX PHY indicate support for
3241  * pre-emphasis level 3.
3242  */
3243 bool
3244 drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
3245 {
3246 	u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
3247 
3248 	return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
3249 }
3250 EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
3251 
3252 /**
3253  * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
3254  * @aux: DisplayPort AUX channel
3255  * @data: DP phy compliance test parameters.
3256  *
3257  * Returns 0 on success or a negative error code on failure.
3258  */
3259 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
3260 				struct drm_dp_phy_test_params *data)
3261 {
3262 	int err;
3263 	u8 rate, lanes;
3264 
3265 	err = drm_dp_dpcd_read_byte(aux, DP_TEST_LINK_RATE, &rate);
3266 	if (err < 0)
3267 		return err;
3268 	data->link_rate = drm_dp_bw_code_to_link_rate(rate);
3269 
3270 	err = drm_dp_dpcd_read_byte(aux, DP_TEST_LANE_COUNT, &lanes);
3271 	if (err < 0)
3272 		return err;
3273 	data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
3274 
3275 	if (lanes & DP_ENHANCED_FRAME_CAP)
3276 		data->enhanced_frame_cap = true;
3277 
3278 	err = drm_dp_dpcd_read_byte(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
3279 	if (err < 0)
3280 		return err;
3281 
3282 	switch (data->phy_pattern) {
3283 	case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
3284 		err = drm_dp_dpcd_read_data(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
3285 					    &data->custom80, sizeof(data->custom80));
3286 		if (err < 0)
3287 			return err;
3288 
3289 		break;
3290 	case DP_PHY_TEST_PATTERN_CP2520:
3291 		err = drm_dp_dpcd_read_data(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
3292 					    &data->hbr2_reset,
3293 					    sizeof(data->hbr2_reset));
3294 		if (err < 0)
3295 			return err;
3296 	}
3297 
3298 	return 0;
3299 }
3300 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
3301 
3302 /**
3303  * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
3304  * @aux: DisplayPort AUX channel
3305  * @data: DP phy compliance test parameters.
3306  * @dp_rev: DP revision to use for compliance testing
3307  *
3308  * Returns 0 on success or a negative error code on failure.
3309  */
3310 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
3311 				struct drm_dp_phy_test_params *data, u8 dp_rev)
3312 {
3313 	int err, i;
3314 	u8 test_pattern;
3315 
3316 	test_pattern = data->phy_pattern;
3317 	if (dp_rev < 0x12) {
3318 		test_pattern = (test_pattern << 2) &
3319 			       DP_LINK_QUAL_PATTERN_11_MASK;
3320 		err = drm_dp_dpcd_write_byte(aux, DP_TRAINING_PATTERN_SET,
3321 					     test_pattern);
3322 		if (err < 0)
3323 			return err;
3324 	} else {
3325 		for (i = 0; i < data->num_lanes; i++) {
3326 			err = drm_dp_dpcd_write_byte(aux,
3327 						     DP_LINK_QUAL_LANE0_SET + i,
3328 						     test_pattern);
3329 			if (err < 0)
3330 				return err;
3331 		}
3332 	}
3333 
3334 	return 0;
3335 }
3336 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
3337 
3338 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
3339 {
3340 	if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
3341 		return "Invalid";
3342 
3343 	switch (pixelformat) {
3344 	case DP_PIXELFORMAT_RGB:
3345 		return "RGB";
3346 	case DP_PIXELFORMAT_YUV444:
3347 		return "YUV444";
3348 	case DP_PIXELFORMAT_YUV422:
3349 		return "YUV422";
3350 	case DP_PIXELFORMAT_YUV420:
3351 		return "YUV420";
3352 	case DP_PIXELFORMAT_Y_ONLY:
3353 		return "Y_ONLY";
3354 	case DP_PIXELFORMAT_RAW:
3355 		return "RAW";
3356 	default:
3357 		return "Reserved";
3358 	}
3359 }
3360 
3361 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
3362 					   enum dp_colorimetry colorimetry)
3363 {
3364 	if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
3365 		return "Invalid";
3366 
3367 	switch (colorimetry) {
3368 	case DP_COLORIMETRY_DEFAULT:
3369 		switch (pixelformat) {
3370 		case DP_PIXELFORMAT_RGB:
3371 			return "sRGB";
3372 		case DP_PIXELFORMAT_YUV444:
3373 		case DP_PIXELFORMAT_YUV422:
3374 		case DP_PIXELFORMAT_YUV420:
3375 			return "BT.601";
3376 		case DP_PIXELFORMAT_Y_ONLY:
3377 			return "DICOM PS3.14";
3378 		case DP_PIXELFORMAT_RAW:
3379 			return "Custom Color Profile";
3380 		default:
3381 			return "Reserved";
3382 		}
3383 	case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
3384 		switch (pixelformat) {
3385 		case DP_PIXELFORMAT_RGB:
3386 			return "Wide Fixed";
3387 		case DP_PIXELFORMAT_YUV444:
3388 		case DP_PIXELFORMAT_YUV422:
3389 		case DP_PIXELFORMAT_YUV420:
3390 			return "BT.709";
3391 		default:
3392 			return "Reserved";
3393 		}
3394 	case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
3395 		switch (pixelformat) {
3396 		case DP_PIXELFORMAT_RGB:
3397 			return "Wide Float";
3398 		case DP_PIXELFORMAT_YUV444:
3399 		case DP_PIXELFORMAT_YUV422:
3400 		case DP_PIXELFORMAT_YUV420:
3401 			return "xvYCC 601";
3402 		default:
3403 			return "Reserved";
3404 		}
3405 	case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
3406 		switch (pixelformat) {
3407 		case DP_PIXELFORMAT_RGB:
3408 			return "OpRGB";
3409 		case DP_PIXELFORMAT_YUV444:
3410 		case DP_PIXELFORMAT_YUV422:
3411 		case DP_PIXELFORMAT_YUV420:
3412 			return "xvYCC 709";
3413 		default:
3414 			return "Reserved";
3415 		}
3416 	case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
3417 		switch (pixelformat) {
3418 		case DP_PIXELFORMAT_RGB:
3419 			return "DCI-P3";
3420 		case DP_PIXELFORMAT_YUV444:
3421 		case DP_PIXELFORMAT_YUV422:
3422 		case DP_PIXELFORMAT_YUV420:
3423 			return "sYCC 601";
3424 		default:
3425 			return "Reserved";
3426 		}
3427 	case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
3428 		switch (pixelformat) {
3429 		case DP_PIXELFORMAT_RGB:
3430 			return "Custom Profile";
3431 		case DP_PIXELFORMAT_YUV444:
3432 		case DP_PIXELFORMAT_YUV422:
3433 		case DP_PIXELFORMAT_YUV420:
3434 			return "OpYCC 601";
3435 		default:
3436 			return "Reserved";
3437 		}
3438 	case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
3439 		switch (pixelformat) {
3440 		case DP_PIXELFORMAT_RGB:
3441 			return "BT.2020 RGB";
3442 		case DP_PIXELFORMAT_YUV444:
3443 		case DP_PIXELFORMAT_YUV422:
3444 		case DP_PIXELFORMAT_YUV420:
3445 			return "BT.2020 CYCC";
3446 		default:
3447 			return "Reserved";
3448 		}
3449 	case DP_COLORIMETRY_BT2020_YCC:
3450 		switch (pixelformat) {
3451 		case DP_PIXELFORMAT_YUV444:
3452 		case DP_PIXELFORMAT_YUV422:
3453 		case DP_PIXELFORMAT_YUV420:
3454 			return "BT.2020 YCC";
3455 		default:
3456 			return "Reserved";
3457 		}
3458 	default:
3459 		return "Invalid";
3460 	}
3461 }
3462 
3463 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
3464 {
3465 	switch (dynamic_range) {
3466 	case DP_DYNAMIC_RANGE_VESA:
3467 		return "VESA range";
3468 	case DP_DYNAMIC_RANGE_CTA:
3469 		return "CTA range";
3470 	default:
3471 		return "Invalid";
3472 	}
3473 }
3474 
3475 static const char *dp_content_type_get_name(enum dp_content_type content_type)
3476 {
3477 	switch (content_type) {
3478 	case DP_CONTENT_TYPE_NOT_DEFINED:
3479 		return "Not defined";
3480 	case DP_CONTENT_TYPE_GRAPHICS:
3481 		return "Graphics";
3482 	case DP_CONTENT_TYPE_PHOTO:
3483 		return "Photo";
3484 	case DP_CONTENT_TYPE_VIDEO:
3485 		return "Video";
3486 	case DP_CONTENT_TYPE_GAME:
3487 		return "Game";
3488 	default:
3489 		return "Reserved";
3490 	}
3491 }
3492 
3493 static const char *dp_sdp_type_get_name(unsigned char type)
3494 {
3495 	switch (type) {
3496 	case DP_SDP_AUDIO_TIMESTAMP:
3497 		return "Audio_TimeStamp";
3498 	case DP_SDP_AUDIO_STREAM:
3499 		return "Audio_Stream";
3500 	case DP_SDP_EXTENSION:
3501 		return "Extension";
3502 	case DP_SDP_AUDIO_COPYMANAGEMENT:
3503 		return "Audio_CopyManagement";
3504 	case DP_SDP_ISRC:
3505 		return "ISRC";
3506 	case DP_SDP_VSC:
3507 		return "VSC";
3508 	case DP_SDP_PPS:
3509 		return "PPS";
3510 	case DP_SDP_VSC_EXT_VESA:
3511 		return "VSC_EXT_VESA";
3512 	case DP_SDP_VSC_EXT_CEA:
3513 		return "VSC_EXT_CEA";
3514 	case DP_SDP_ADAPTIVE_SYNC:
3515 		return "Adaptive-Sync";
3516 	default:
3517 		return "Unknown";
3518 	}
3519 }
3520 
3521 void drm_dp_vsc_sdp_log(struct drm_printer *p, const struct drm_dp_vsc_sdp *vsc)
3522 {
3523 	drm_printf(p, "DP SDP: %s, revision %u, length %u\n",
3524 		   dp_sdp_type_get_name(vsc->sdp_type), vsc->revision, vsc->length);
3525 
3526 	drm_printf_indent(p, 1, "pixelformat: %s\n",
3527 			  dp_pixelformat_get_name(vsc->pixelformat));
3528 	drm_printf_indent(p, 1, "colorimetry: %s\n",
3529 			  dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
3530 	drm_printf_indent(p, 1, "bpc: %u\n", vsc->bpc);
3531 	drm_printf_indent(p, 1, "dynamic range: %s\n",
3532 			  dp_dynamic_range_get_name(vsc->dynamic_range));
3533 	drm_printf_indent(p, 1, "content type: %s\n",
3534 			  dp_content_type_get_name(vsc->content_type));
3535 }
3536 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
3537 
3538 void drm_dp_as_sdp_log(struct drm_printer *p, const struct drm_dp_as_sdp *as_sdp)
3539 {
3540 	drm_printf(p, "DP SDP: %s, revision %u, length %u\n",
3541 		   dp_sdp_type_get_name(as_sdp->sdp_type), as_sdp->revision, as_sdp->length);
3542 
3543 	drm_printf_indent(p, 1, "vtotal: %d\n", as_sdp->vtotal);
3544 	drm_printf_indent(p, 1, "target rr: %d\n", as_sdp->target_rr);
3545 	drm_printf_indent(p, 1, "duration increase ms: %d\n", as_sdp->duration_incr_ms);
3546 	drm_printf_indent(p, 1, "duration decrease ms: %d\n", as_sdp->duration_decr_ms);
3547 	drm_printf_indent(p, 1, "operation mode: %d\n", as_sdp->mode);
3548 	drm_printf_indent(p, 1, "target rr divider: %s\n",
3549 			  as_sdp->target_rr_divider ? "1.001" : "1.000");
3550 	drm_printf_indent(p, 1, "coasting vtotal: %d\n", as_sdp->coasting_vtotal);
3551 }
3552 EXPORT_SYMBOL(drm_dp_as_sdp_log);
3553 
3554 /**
3555  * drm_dp_as_sdp_supported() - check if adaptive sync sdp is supported
3556  * @aux: DisplayPort AUX channel
3557  * @dpcd: DisplayPort configuration data
3558  *
3559  * Returns true if adaptive sync sdp is supported, else returns false
3560  */
3561 bool drm_dp_as_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])
3562 {
3563 	u8 rx_feature;
3564 
3565 	if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)
3566 		return false;
3567 
3568 	if (drm_dp_dpcd_read_byte(aux, DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,
3569 				  &rx_feature) < 0) {
3570 		drm_dbg_dp(aux->drm_dev,
3571 			   "Failed to read DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1\n");
3572 		return false;
3573 	}
3574 
3575 	return (rx_feature & DP_ADAPTIVE_SYNC_SDP_SUPPORTED);
3576 }
3577 EXPORT_SYMBOL(drm_dp_as_sdp_supported);
3578 
3579 /**
3580  * drm_dp_vsc_sdp_supported() - check if vsc sdp is supported
3581  * @aux: DisplayPort AUX channel
3582  * @dpcd: DisplayPort configuration data
3583  *
3584  * Returns true if vsc sdp is supported, else returns false
3585  */
3586 bool drm_dp_vsc_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])
3587 {
3588 	u8 rx_feature;
3589 
3590 	if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)
3591 		return false;
3592 
3593 	if (drm_dp_dpcd_read_byte(aux, DP_DPRX_FEATURE_ENUMERATION_LIST, &rx_feature) < 0) {
3594 		drm_dbg_dp(aux->drm_dev, "failed to read DP_DPRX_FEATURE_ENUMERATION_LIST\n");
3595 		return false;
3596 	}
3597 
3598 	return (rx_feature & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED);
3599 }
3600 EXPORT_SYMBOL(drm_dp_vsc_sdp_supported);
3601 
3602 /**
3603  * drm_dp_vsc_sdp_pack() - pack a given vsc sdp into generic dp_sdp
3604  * @vsc: vsc sdp initialized according to its purpose as defined in
3605  *       table 2-118 - table 2-120 in DP 1.4a specification
3606  * @sdp: valid handle to the generic dp_sdp which will be packed
3607  *
3608  * Returns length of sdp on success and error code on failure
3609  */
3610 ssize_t drm_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,
3611 			    struct dp_sdp *sdp)
3612 {
3613 	size_t length = sizeof(struct dp_sdp);
3614 
3615 	memset(sdp, 0, sizeof(struct dp_sdp));
3616 
3617 	/*
3618 	 * Prepare VSC Header for SU as per DP 1.4a spec, Table 2-119
3619 	 * VSC SDP Header Bytes
3620 	 */
3621 	sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */
3622 	sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */
3623 	sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */
3624 	sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */
3625 
3626 	if (vsc->revision == 0x6) {
3627 		sdp->db[0] = 1;
3628 		sdp->db[3] = 1;
3629 	}
3630 
3631 	/*
3632 	 * Revision 0x5 and revision 0x7 supports Pixel Encoding/Colorimetry
3633 	 * Format as per DP 1.4a spec and DP 2.0 respectively.
3634 	 */
3635 	if (!(vsc->revision == 0x5 || vsc->revision == 0x7))
3636 		goto out;
3637 
3638 	/* VSC SDP Payload for DB16 through DB18 */
3639 	/* Pixel Encoding and Colorimetry Formats  */
3640 	sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */
3641 	sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */
3642 
3643 	switch (vsc->bpc) {
3644 	case 6:
3645 		/* 6bpc: 0x0 */
3646 		break;
3647 	case 8:
3648 		sdp->db[17] = 0x1; /* DB17[3:0] */
3649 		break;
3650 	case 10:
3651 		sdp->db[17] = 0x2;
3652 		break;
3653 	case 12:
3654 		sdp->db[17] = 0x3;
3655 		break;
3656 	case 16:
3657 		sdp->db[17] = 0x4;
3658 		break;
3659 	default:
3660 		WARN(1, "Missing case %d\n", vsc->bpc);
3661 		return -EINVAL;
3662 	}
3663 
3664 	/* Dynamic Range and Component Bit Depth */
3665 	if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)
3666 		sdp->db[17] |= 0x80;  /* DB17[7] */
3667 
3668 	/* Content Type */
3669 	sdp->db[18] = vsc->content_type & 0x7;
3670 
3671 out:
3672 	return length;
3673 }
3674 EXPORT_SYMBOL(drm_dp_vsc_sdp_pack);
3675 
3676 /**
3677  * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
3678  * @dpcd: DisplayPort configuration data
3679  * @port_cap: port capabilities
3680  *
3681  * Returns maximum frl bandwidth supported by PCON in GBPS,
3682  * returns 0 if not supported.
3683  */
3684 int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
3685 			       const u8 port_cap[4])
3686 {
3687 	int bw;
3688 	u8 buf;
3689 
3690 	if (!drm_dp_is_branch(dpcd))
3691 		return 0;
3692 
3693 	if (dpcd[DP_DPCD_REV] < 0x11)
3694 		return 0;
3695 
3696 	if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
3697 		return 0;
3698 
3699 	if ((port_cap[0] & DP_DS_PORT_TYPE_MASK) != DP_DS_PORT_TYPE_HDMI)
3700 		return 0;
3701 
3702 	buf = port_cap[2];
3703 	bw = buf & DP_PCON_MAX_FRL_BW;
3704 
3705 	switch (bw) {
3706 	case DP_PCON_MAX_9GBPS:
3707 		return 9;
3708 	case DP_PCON_MAX_18GBPS:
3709 		return 18;
3710 	case DP_PCON_MAX_24GBPS:
3711 		return 24;
3712 	case DP_PCON_MAX_32GBPS:
3713 		return 32;
3714 	case DP_PCON_MAX_40GBPS:
3715 		return 40;
3716 	case DP_PCON_MAX_48GBPS:
3717 		return 48;
3718 	case DP_PCON_MAX_0GBPS:
3719 	default:
3720 		return 0;
3721 	}
3722 
3723 	return 0;
3724 }
3725 EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
3726 
3727 /**
3728  * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
3729  * @aux: DisplayPort AUX channel
3730  * @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.
3731  *
3732  * Returns 0 if success, else returns negative error code.
3733  */
3734 int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
3735 {
3736 	u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
3737 		 DP_PCON_ENABLE_LINK_FRL_MODE;
3738 
3739 	if (enable_frl_ready_hpd)
3740 		buf |= DP_PCON_ENABLE_HPD_READY;
3741 
3742 	return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
3743 }
3744 EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
3745 
3746 /**
3747  * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
3748  * @aux: DisplayPort AUX channel
3749  *
3750  * Returns true if success, else returns false.
3751  */
3752 bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
3753 {
3754 	int ret;
3755 	u8 buf;
3756 
3757 	ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
3758 	if (ret < 0)
3759 		return false;
3760 
3761 	if (buf & DP_PCON_FRL_READY)
3762 		return true;
3763 
3764 	return false;
3765 }
3766 EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
3767 
3768 /**
3769  * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
3770  * @aux: DisplayPort AUX channel
3771  * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
3772  * @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.
3773  * In Concurrent Mode, the FRL link bring up can be done along with
3774  * DP Link training. In Sequential mode, the FRL link bring up is done prior to
3775  * the DP Link training.
3776  *
3777  * Returns 0 if success, else returns negative error code.
3778  */
3779 
3780 int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
3781 				u8 frl_mode)
3782 {
3783 	int ret;
3784 	u8 buf;
3785 
3786 	ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
3787 	if (ret < 0)
3788 		return ret;
3789 
3790 	if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)
3791 		buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
3792 	else
3793 		buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
3794 
3795 	switch (max_frl_gbps) {
3796 	case 9:
3797 		buf |=  DP_PCON_ENABLE_MAX_BW_9GBPS;
3798 		break;
3799 	case 18:
3800 		buf |=  DP_PCON_ENABLE_MAX_BW_18GBPS;
3801 		break;
3802 	case 24:
3803 		buf |=  DP_PCON_ENABLE_MAX_BW_24GBPS;
3804 		break;
3805 	case 32:
3806 		buf |=  DP_PCON_ENABLE_MAX_BW_32GBPS;
3807 		break;
3808 	case 40:
3809 		buf |=  DP_PCON_ENABLE_MAX_BW_40GBPS;
3810 		break;
3811 	case 48:
3812 		buf |=  DP_PCON_ENABLE_MAX_BW_48GBPS;
3813 		break;
3814 	case 0:
3815 		buf |=  DP_PCON_ENABLE_MAX_BW_0GBPS;
3816 		break;
3817 	default:
3818 		return -EINVAL;
3819 	}
3820 
3821 	return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
3822 }
3823 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
3824 
3825 /**
3826  * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
3827  * @aux: DisplayPort AUX channel
3828  * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
3829  * @frl_type : FRL training type, can be Extended, or Normal.
3830  * In Normal FRL training, the PCON tries each frl bw from the max_frl_mask
3831  * starting from min, and stops when link training is successful. In Extended
3832  * FRL training, all frl bw selected in the mask are trained by the PCON.
3833  *
3834  * Returns 0 if success, else returns negative error code.
3835  */
3836 int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
3837 				u8 frl_type)
3838 {
3839 	int ret;
3840 	u8 buf = max_frl_mask;
3841 
3842 	if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)
3843 		buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
3844 	else
3845 		buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;
3846 
3847 	return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
3848 	if (ret < 0)
3849 		return ret;
3850 
3851 	return 0;
3852 }
3853 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
3854 
3855 /**
3856  * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
3857  * @aux: DisplayPort AUX channel
3858  *
3859  * Returns 0 if success, else returns negative error code.
3860  */
3861 int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
3862 {
3863 	return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
3864 }
3865 EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
3866 
3867 /**
3868  * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
3869  * @aux: DisplayPort AUX channel
3870  *
3871  * Returns 0 if success, else returns negative error code.
3872  */
3873 int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
3874 {
3875 	int ret;
3876 	u8 buf = 0;
3877 
3878 	ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
3879 	if (ret < 0)
3880 		return ret;
3881 	if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
3882 		drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",
3883 			    aux->name);
3884 		return -EINVAL;
3885 	}
3886 	buf |= DP_PCON_ENABLE_HDMI_LINK;
3887 	return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
3888 }
3889 EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
3890 
3891 /**
3892  * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
3893  * @aux: DisplayPort AUX channel
3894  *
3895  * Returns true if link is active else returns false.
3896  */
3897 bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
3898 {
3899 	u8 buf;
3900 	int ret;
3901 
3902 	ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
3903 	if (ret < 0)
3904 		return false;
3905 
3906 	return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
3907 }
3908 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
3909 
3910 /**
3911  * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
3912  * @aux: DisplayPort AUX channel
3913  * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
3914  * Valid only if the MODE returned is FRL. For Normal Link training mode
3915  * only 1 of the bits will be set, but in case of Extended mode, more than
3916  * one bits can be set.
3917  *
3918  * Returns the link mode : TMDS or FRL on success, else returns negative error
3919  * code.
3920  */
3921 int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
3922 {
3923 	u8 buf;
3924 	int mode;
3925 	int ret;
3926 
3927 	ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
3928 	if (ret < 0)
3929 		return ret;
3930 
3931 	mode = buf & DP_PCON_HDMI_LINK_MODE;
3932 
3933 	if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
3934 		*frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
3935 
3936 	return mode;
3937 }
3938 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
3939 
3940 /**
3941  * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
3942  * during link failure between PCON and HDMI sink
3943  * @aux: DisplayPort AUX channel
3944  * @connector: DRM connector
3945  * code.
3946  **/
3947 
3948 void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
3949 					   struct drm_connector *connector)
3950 {
3951 	u8 buf, error_count;
3952 	int i, num_error;
3953 	struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
3954 
3955 	for (i = 0; i < hdmi->max_lanes; i++) {
3956 		if (drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
3957 			return;
3958 
3959 		error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
3960 		switch (error_count) {
3961 		case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
3962 			num_error = 100;
3963 			break;
3964 		case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
3965 			num_error = 10;
3966 			break;
3967 		case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
3968 			num_error = 3;
3969 			break;
3970 		default:
3971 			num_error = 0;
3972 		}
3973 
3974 		drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",
3975 			aux->name, num_error, i);
3976 	}
3977 }
3978 EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
3979 
3980 /*
3981  * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
3982  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3983  *
3984  * Returns true is PCON encoder is DSC 1.2 else returns false.
3985  */
3986 bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3987 {
3988 	u8 buf;
3989 	u8 major_v, minor_v;
3990 
3991 	buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
3992 	major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
3993 	minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
3994 
3995 	if (major_v == 1 && minor_v == 2)
3996 		return true;
3997 
3998 	return false;
3999 }
4000 EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
4001 
4002 /*
4003  * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
4004  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
4005  *
4006  * Returns maximum no. of slices supported by the PCON DSC Encoder.
4007  */
4008 int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
4009 {
4010 	u8 slice_cap1, slice_cap2;
4011 
4012 	slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
4013 	slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
4014 
4015 	if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
4016 		return 24;
4017 	if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
4018 		return 20;
4019 	if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
4020 		return 16;
4021 	if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
4022 		return 12;
4023 	if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
4024 		return 10;
4025 	if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
4026 		return 8;
4027 	if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
4028 		return 6;
4029 	if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
4030 		return 4;
4031 	if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
4032 		return 2;
4033 	if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
4034 		return 1;
4035 
4036 	return 0;
4037 }
4038 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
4039 
4040 /*
4041  * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
4042  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
4043  *
4044  * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
4045  */
4046 int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
4047 {
4048 	u8 buf;
4049 
4050 	buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
4051 
4052 	return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
4053 }
4054 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
4055 
4056 /*
4057  * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
4058  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
4059  *
4060  * Returns the bpp precision supported by the PCON encoder.
4061  */
4062 int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
4063 {
4064 	u8 buf;
4065 
4066 	buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
4067 
4068 	switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
4069 	case DP_PCON_DSC_ONE_16TH_BPP:
4070 		return 16;
4071 	case DP_PCON_DSC_ONE_8TH_BPP:
4072 		return 8;
4073 	case DP_PCON_DSC_ONE_4TH_BPP:
4074 		return 4;
4075 	case DP_PCON_DSC_ONE_HALF_BPP:
4076 		return 2;
4077 	case DP_PCON_DSC_ONE_BPP:
4078 		return 1;
4079 	}
4080 
4081 	return 0;
4082 }
4083 EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
4084 
4085 static
4086 int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
4087 {
4088 	u8 buf;
4089 	int ret;
4090 
4091 	ret = drm_dp_dpcd_read_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
4092 	if (ret < 0)
4093 		return ret;
4094 
4095 	buf |= DP_PCON_ENABLE_DSC_ENCODER;
4096 
4097 	if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
4098 		buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
4099 		buf |= pps_buf_config << 2;
4100 	}
4101 
4102 	return drm_dp_dpcd_write_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
4103 }
4104 
4105 /**
4106  * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
4107  * for DSC1.2 between PCON & HDMI2.1 sink
4108  * @aux: DisplayPort AUX channel
4109  *
4110  * Returns 0 on success, else returns negative error code.
4111  */
4112 int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
4113 {
4114 	return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
4115 }
4116 EXPORT_SYMBOL(drm_dp_pcon_pps_default);
4117 
4118 /**
4119  * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
4120  * HDMI sink
4121  * @aux: DisplayPort AUX channel
4122  * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
4123  *
4124  * Returns 0 on success, else returns negative error code.
4125  */
4126 int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
4127 {
4128 	int ret;
4129 
4130 	ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
4131 	if (ret < 0)
4132 		return ret;
4133 
4134 	return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
4135 }
4136 EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
4137 
4138 /*
4139  * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
4140  * override registers
4141  * @aux: DisplayPort AUX channel
4142  * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
4143  * bits_per_pixel.
4144  *
4145  * Returns 0 on success, else returns negative error code.
4146  */
4147 int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
4148 {
4149 	int ret;
4150 
4151 	ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
4152 	if (ret < 0)
4153 		return ret;
4154 	ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
4155 	if (ret < 0)
4156 		return ret;
4157 	ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
4158 	if (ret < 0)
4159 		return ret;
4160 
4161 	return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
4162 }
4163 EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
4164 
4165 /*
4166  * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
4167  * @aux: displayPort AUX channel
4168  * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
4169  *
4170  * Returns 0 on success, else returns negative error code.
4171  */
4172 int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
4173 {
4174 	int ret;
4175 	u8 buf;
4176 
4177 	ret = drm_dp_dpcd_read_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
4178 	if (ret < 0)
4179 		return ret;
4180 
4181 	if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
4182 		buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
4183 	else
4184 		buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
4185 
4186 	return drm_dp_dpcd_write_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
4187 }
4188 EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);
4189 
4190 /**
4191  * drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX
4192  * @aux: The DP AUX channel to use
4193  * @bl: Backlight capability info from drm_edp_backlight_init()
4194  * @level: The brightness level to set
4195  *
4196  * Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must
4197  * already have been enabled by the driver by calling drm_edp_backlight_enable().
4198  *
4199  * Returns: %0 on success, negative error code on failure
4200  */
4201 int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
4202 				u32 level)
4203 {
4204 	int ret;
4205 	unsigned int offset = DP_EDP_BACKLIGHT_BRIGHTNESS_MSB;
4206 	u8 buf[3] = { 0 };
4207 	size_t len = 2;
4208 
4209 	/* The panel uses the PWM for controlling brightness levels */
4210 	if (!(bl->aux_set || bl->luminance_set))
4211 		return 0;
4212 
4213 	if (bl->luminance_set) {
4214 		level = level * 1000;
4215 		level &= 0xffffff;
4216 		buf[0] = (level & 0x0000ff);
4217 		buf[1] = (level & 0x00ff00) >> 8;
4218 		buf[2] = (level & 0xff0000) >> 16;
4219 		offset = DP_EDP_PANEL_TARGET_LUMINANCE_VALUE;
4220 		len = 3;
4221 	} else if (bl->lsb_reg_used) {
4222 		buf[0] = (level & 0xff00) >> 8;
4223 		buf[1] = (level & 0x00ff);
4224 	} else {
4225 		buf[0] = level;
4226 	}
4227 
4228 	ret = drm_dp_dpcd_write_data(aux, offset, buf, len);
4229 	if (ret < 0) {
4230 		drm_err(aux->drm_dev,
4231 			"%s: Failed to write aux backlight level: %d\n",
4232 			aux->name, ret);
4233 		return ret;
4234 	}
4235 
4236 	return 0;
4237 }
4238 EXPORT_SYMBOL(drm_edp_backlight_set_level);
4239 
4240 static int
4241 drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
4242 			     bool enable)
4243 {
4244 	int ret;
4245 	u8 buf;
4246 
4247 	/* This panel uses the EDP_BL_PWR GPIO for enablement */
4248 	if (!bl->aux_enable)
4249 		return 0;
4250 
4251 	ret = drm_dp_dpcd_read_byte(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);
4252 	if (ret < 0) {
4253 		drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",
4254 			aux->name, ret);
4255 		return ret;
4256 	}
4257 	if (enable)
4258 		buf |= DP_EDP_BACKLIGHT_ENABLE;
4259 	else
4260 		buf &= ~DP_EDP_BACKLIGHT_ENABLE;
4261 
4262 	ret = drm_dp_dpcd_write_byte(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);
4263 	if (ret < 0) {
4264 		drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",
4265 			aux->name, ret);
4266 		return ret;
4267 	}
4268 
4269 	return 0;
4270 }
4271 
4272 /**
4273  * drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD
4274  * @aux: The DP AUX channel to use
4275  * @bl: Backlight capability info from drm_edp_backlight_init()
4276  * @level: The initial backlight level to set via AUX, if there is one
4277  *
4278  * This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally
4279  * restoring any important backlight state such as the given backlight level, the brightness byte
4280  * count, backlight frequency, etc.
4281  *
4282  * Note that certain panels do not support being enabled or disabled via DPCD, but instead require
4283  * that the driver handle enabling/disabling the panel through implementation-specific means using
4284  * the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,
4285  * this function becomes a no-op, and the driver is expected to handle powering the panel on using
4286  * the EDP_BL_PWR GPIO.
4287  *
4288  * Returns: %0 on success, negative error code on failure.
4289  */
4290 int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
4291 			     const u32 level)
4292 {
4293 	int ret;
4294 	u8 dpcd_buf;
4295 
4296 	if (bl->aux_set)
4297 		dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
4298 	else
4299 		dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_PWM;
4300 
4301 	if (bl->luminance_set)
4302 		dpcd_buf |= DP_EDP_PANEL_LUMINANCE_CONTROL_ENABLE;
4303 
4304 	if (bl->pwmgen_bit_count) {
4305 		ret = drm_dp_dpcd_write_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);
4306 		if (ret < 0)
4307 			drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
4308 				    aux->name, ret);
4309 	}
4310 
4311 	if (bl->pwm_freq_pre_divider) {
4312 		ret = drm_dp_dpcd_write_byte(aux, DP_EDP_BACKLIGHT_FREQ_SET,
4313 					     bl->pwm_freq_pre_divider);
4314 		if (ret < 0)
4315 			drm_dbg_kms(aux->drm_dev,
4316 				    "%s: Failed to write aux backlight frequency: %d\n",
4317 				    aux->name, ret);
4318 		else
4319 			dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;
4320 	}
4321 
4322 	ret = drm_dp_dpcd_write_byte(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf);
4323 	if (ret < 0) {
4324 		drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",
4325 			    aux->name, ret);
4326 		return ret < 0 ? ret : -EIO;
4327 	}
4328 
4329 	ret = drm_edp_backlight_set_level(aux, bl, level);
4330 	if (ret < 0)
4331 		return ret;
4332 	ret = drm_edp_backlight_set_enable(aux, bl, true);
4333 	if (ret < 0)
4334 		return ret;
4335 
4336 	return 0;
4337 }
4338 EXPORT_SYMBOL(drm_edp_backlight_enable);
4339 
4340 /**
4341  * drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported
4342  * @aux: The DP AUX channel to use
4343  * @bl: Backlight capability info from drm_edp_backlight_init()
4344  *
4345  * This function handles disabling DPCD backlight controls on a panel over AUX.
4346  *
4347  * Note that certain panels do not support being enabled or disabled via DPCD, but instead require
4348  * that the driver handle enabling/disabling the panel through implementation-specific means using
4349  * the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,
4350  * this function becomes a no-op, and the driver is expected to handle powering the panel off using
4351  * the EDP_BL_PWR GPIO.
4352  *
4353  * Returns: %0 on success or no-op, negative error code on failure.
4354  */
4355 int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)
4356 {
4357 	int ret;
4358 
4359 	ret = drm_edp_backlight_set_enable(aux, bl, false);
4360 	if (ret < 0)
4361 		return ret;
4362 
4363 	return 0;
4364 }
4365 EXPORT_SYMBOL(drm_edp_backlight_disable);
4366 
4367 static inline int
4368 drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
4369 			    u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])
4370 {
4371 	int fxp, fxp_min, fxp_max, fxp_actual, f = 1;
4372 	int ret;
4373 	u8 pn, pn_min, pn_max, bit_count;
4374 
4375 	if (!bl->aux_set)
4376 		return 0;
4377 
4378 	ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, &bit_count);
4379 	if (ret < 0) {
4380 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",
4381 			    aux->name, ret);
4382 		return -ENODEV;
4383 	}
4384 
4385 	bit_count &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
4386 
4387 	ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);
4388 	if (ret < 0) {
4389 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",
4390 			    aux->name, ret);
4391 		return -ENODEV;
4392 	}
4393 	pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
4394 
4395 	ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);
4396 	if (ret < 0) {
4397 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",
4398 			    aux->name, ret);
4399 		return -ENODEV;
4400 	}
4401 	pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
4402 
4403 	if (unlikely(pn_min > pn_max)) {
4404 		drm_dbg_kms(aux->drm_dev, "%s: Invalid pwmgen bit count cap min/max returned: %d %d\n",
4405 			    aux->name, pn_min, pn_max);
4406 		return -EINVAL;
4407 	}
4408 
4409 	/*
4410 	 * Per VESA eDP Spec v1.4b, section 3.3.10.2:
4411 	 * If DP_EDP_PWMGEN_BIT_COUNT is less than DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN,
4412 	 * the sink must use the MIN value as the effective PWM bit count.
4413 	 * Clamp the reported value to the [MIN, MAX] capability range to ensure
4414 	 * correct brightness scaling on compliant eDP panels.
4415 	 * Only enable this logic if the [MIN, MAX] range is valid in regard to Spec.
4416 	 */
4417 	pn = bit_count;
4418 	if (bit_count < pn_min)
4419 		pn = clamp(bit_count, pn_min, pn_max);
4420 
4421 	bl->max = (1 << pn) - 1;
4422 	if (!driver_pwm_freq_hz) {
4423 		if (pn != bit_count)
4424 			goto bit_count_write_back;
4425 
4426 		return 0;
4427 	}
4428 
4429 	/*
4430 	 * Set PWM Frequency divider to match desired frequency provided by the driver.
4431 	 * The PWM Frequency is calculated as 27Mhz / (F x P).
4432 	 * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the
4433 	 *             EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)
4434 	 * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the
4435 	 *             EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)
4436 	 */
4437 
4438 	/* Find desired value of (F x P)
4439 	 * Note that, if F x P is out of supported range, the maximum value or minimum value will
4440 	 * applied automatically. So no need to check that.
4441 	 */
4442 	fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);
4443 
4444 	/* Use highest possible value of Pn for more granularity of brightness adjustment while
4445 	 * satisfying the conditions below.
4446 	 * - Pn is in the range of Pn_min and Pn_max
4447 	 * - F is in the range of 1 and 255
4448 	 * - FxP is within 25% of desired value.
4449 	 *   Note: 25% is arbitrary value and may need some tweak.
4450 	 */
4451 	/* Ensure frequency is within 25% of desired value */
4452 	fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);
4453 	fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);
4454 	if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {
4455 		drm_dbg_kms(aux->drm_dev,
4456 			    "%s: Driver defined backlight frequency (%d) out of range\n",
4457 			    aux->name, driver_pwm_freq_hz);
4458 		return 0;
4459 	}
4460 
4461 	for (pn = pn_max; pn >= pn_min; pn--) {
4462 		f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);
4463 		fxp_actual = f << pn;
4464 		if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)
4465 			break;
4466 	}
4467 
4468 bit_count_write_back:
4469 	ret = drm_dp_dpcd_write_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);
4470 	if (ret < 0) {
4471 		drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
4472 			    aux->name, ret);
4473 		return 0;
4474 	}
4475 
4476 	if (!driver_pwm_freq_hz)
4477 		return 0;
4478 
4479 	bl->pwmgen_bit_count = pn;
4480 	bl->max = (1 << pn) - 1;
4481 
4482 	if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {
4483 		bl->pwm_freq_pre_divider = f;
4484 		drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",
4485 			    aux->name, driver_pwm_freq_hz);
4486 	}
4487 
4488 	return 0;
4489 }
4490 
4491 static inline int
4492 drm_edp_backlight_probe_state(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
4493 			      u8 *current_mode)
4494 {
4495 	int ret;
4496 	u8 buf[3];
4497 	u8 mode_reg;
4498 
4499 	ret = drm_dp_dpcd_read_byte(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);
4500 	if (ret < 0) {
4501 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",
4502 			    aux->name, ret);
4503 		return ret < 0 ? ret : -EIO;
4504 	}
4505 
4506 	*current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);
4507 	if (!bl->aux_set)
4508 		return 0;
4509 
4510 	if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
4511 		int size = 1 + bl->lsb_reg_used;
4512 
4513 		if (bl->luminance_set) {
4514 			ret = drm_dp_dpcd_read_data(aux, DP_EDP_PANEL_TARGET_LUMINANCE_VALUE,
4515 						    buf, sizeof(buf));
4516 			if (ret < 0) {
4517 				drm_dbg_kms(aux->drm_dev,
4518 					    "%s: Failed to read backlight level: %d\n",
4519 					    aux->name, ret);
4520 				return ret;
4521 			}
4522 
4523 			/*
4524 			 * Incase luminance is set we want to send the value back in nits but
4525 			 * since DP_EDP_PANEL_TARGET_LUMINANCE stores values in millinits we
4526 			 * need to divide by 1000.
4527 			 */
4528 			return (buf[0] | buf[1] << 8 | buf[2] << 16) / 1000;
4529 		} else {
4530 			ret = drm_dp_dpcd_read_data(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,
4531 						    buf, size);
4532 			if (ret < 0) {
4533 				drm_dbg_kms(aux->drm_dev,
4534 					    "%s: Failed to read backlight level: %d\n",
4535 					    aux->name, ret);
4536 				return ret;
4537 			}
4538 
4539 			if (bl->lsb_reg_used)
4540 				return (buf[0] << 8) | buf[1];
4541 			else
4542 				return buf[0];
4543 		}
4544 	}
4545 
4546 	/*
4547 	 * If we're not in DPCD control mode yet, the programmed brightness value is meaningless and
4548 	 * the driver should assume max brightness
4549 	 */
4550 	return bl->max;
4551 }
4552 
4553 /**
4554  * drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight
4555  * interface.
4556  * @aux: The DP aux device to use for probing
4557  * @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight
4558  * @max_luminance: max luminance when need luminance is set as true
4559  * @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz
4560  * @edp_dpcd: A cached copy of the eDP DPCD
4561  * @current_level: Where to store the probed brightness level, if any
4562  * @current_mode: Where to store the currently set backlight control mode
4563  * @need_luminance: Tells us if a we want to manipulate backlight using luminance values
4564  *
4565  * Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,
4566  * along with also probing the current and maximum supported brightness levels.
4567  *
4568  * If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the
4569  * default frequency from the panel is used.
4570  *
4571  * Returns: %0 on success, negative error code on failure.
4572  */
4573 int
4574 drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
4575 		       u32 max_luminance,
4576 		       u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],
4577 		       u32 *current_level, u8 *current_mode, bool need_luminance)
4578 {
4579 	int ret;
4580 
4581 	if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)
4582 		bl->aux_enable = true;
4583 	if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)
4584 		bl->aux_set = true;
4585 	if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
4586 		bl->lsb_reg_used = true;
4587 	if ((edp_dpcd[0] & DP_EDP_15) && edp_dpcd[3] &
4588 	    (DP_EDP_PANEL_LUMINANCE_CONTROL_CAPABLE) && need_luminance)
4589 		bl->luminance_set = true;
4590 
4591 	/* Sanity check caps */
4592 	if (!bl->aux_set && !(edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP) &&
4593 	    !bl->luminance_set) {
4594 		drm_dbg_kms(aux->drm_dev,
4595 			    "%s: Panel does not support AUX, PWM or luminance-based brightness control. Aborting\n",
4596 			    aux->name);
4597 		return -EINVAL;
4598 	}
4599 
4600 	if (bl->luminance_set) {
4601 		bl->max = max_luminance;
4602 	} else {
4603 		ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);
4604 		if (ret < 0)
4605 			return ret;
4606 	}
4607 
4608 	ret = drm_edp_backlight_probe_state(aux, bl, current_mode);
4609 	if (ret < 0)
4610 		return ret;
4611 	*current_level = ret;
4612 
4613 	drm_dbg_kms(aux->drm_dev,
4614 		    "%s: Found backlight: aux_set=%d aux_enable=%d mode=%d\n",
4615 		    aux->name, bl->aux_set, bl->aux_enable, *current_mode);
4616 	if (bl->aux_set) {
4617 		drm_dbg_kms(aux->drm_dev,
4618 			    "%s: Backlight caps: level=%d/%d pwm_freq_pre_divider=%d lsb_reg_used=%d\n",
4619 			    aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider,
4620 			    bl->lsb_reg_used);
4621 	}
4622 
4623 	return 0;
4624 }
4625 EXPORT_SYMBOL(drm_edp_backlight_init);
4626 
4627 #if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
4628 	(IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))
4629 
4630 static int dp_aux_backlight_update_status(struct backlight_device *bd)
4631 {
4632 	struct dp_aux_backlight *bl = bl_get_data(bd);
4633 	u16 brightness = backlight_get_brightness(bd);
4634 	int ret = 0;
4635 
4636 	if (!backlight_is_blank(bd)) {
4637 		if (!bl->enabled) {
4638 			ret = drm_edp_backlight_enable(bl->aux, &bl->info, brightness);
4639 			if (ret)
4640 				return ret;
4641 			bl->enabled = true;
4642 			return 0;
4643 		}
4644 		ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);
4645 	} else {
4646 		if (bl->enabled) {
4647 			drm_edp_backlight_disable(bl->aux, &bl->info);
4648 			bl->enabled = false;
4649 		}
4650 	}
4651 
4652 	return ret;
4653 }
4654 
4655 static const struct backlight_ops dp_aux_bl_ops = {
4656 	.update_status = dp_aux_backlight_update_status,
4657 };
4658 
4659 /**
4660  * drm_panel_dp_aux_backlight - create and use DP AUX backlight
4661  * @panel: DRM panel
4662  * @aux: The DP AUX channel to use
4663  *
4664  * Use this function to create and handle backlight if your panel
4665  * supports backlight control over DP AUX channel using DPCD
4666  * registers as per VESA's standard backlight control interface.
4667  *
4668  * When the panel is enabled backlight will be enabled after a
4669  * successful call to &drm_panel_funcs.enable()
4670  *
4671  * When the panel is disabled backlight will be disabled before the
4672  * call to &drm_panel_funcs.disable().
4673  *
4674  * A typical implementation for a panel driver supporting backlight
4675  * control over DP AUX will call this function at probe time.
4676  * Backlight will then be handled transparently without requiring
4677  * any intervention from the driver.
4678  *
4679  * Return: 0 on success or a negative error code on failure.
4680  */
4681 int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)
4682 {
4683 	struct dp_aux_backlight *bl;
4684 	struct backlight_properties props = { 0 };
4685 	u32 current_level;
4686 	u8 current_mode;
4687 	u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
4688 	int ret;
4689 
4690 	if (!panel || !panel->dev || !aux)
4691 		return -EINVAL;
4692 
4693 	ret = drm_dp_dpcd_read_data(aux, DP_EDP_DPCD_REV, edp_dpcd,
4694 				    EDP_DISPLAY_CTL_CAP_SIZE);
4695 	if (ret < 0)
4696 		return ret;
4697 
4698 	if (!drm_edp_backlight_supported(edp_dpcd)) {
4699 		DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");
4700 		return 0;
4701 	}
4702 
4703 	bl = devm_kzalloc_obj(panel->dev, *bl);
4704 	if (!bl)
4705 		return -ENOMEM;
4706 
4707 	bl->aux = aux;
4708 
4709 	ret = drm_edp_backlight_init(aux, &bl->info, 0, 0, edp_dpcd,
4710 				     &current_level, &current_mode, false);
4711 	if (ret < 0)
4712 		return ret;
4713 
4714 	props.type = BACKLIGHT_RAW;
4715 	props.brightness = current_level;
4716 	props.max_brightness = bl->info.max;
4717 
4718 	bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",
4719 						  panel->dev, bl,
4720 						  &dp_aux_bl_ops, &props);
4721 	if (IS_ERR(bl->base))
4722 		return PTR_ERR(bl->base);
4723 
4724 	backlight_disable(bl->base);
4725 
4726 	panel->backlight = bl->base;
4727 
4728 	return 0;
4729 }
4730 EXPORT_SYMBOL(drm_panel_dp_aux_backlight);
4731 
4732 #endif
4733 
4734 /* See DP Standard v2.1 2.6.4.4.1.1, 2.8.4.4, 2.8.7 */
4735 static int drm_dp_link_data_symbol_cycles(int lane_count, int pixels,
4736 					  int bpp_x16, int symbol_size,
4737 					  bool is_mst)
4738 {
4739 	int cycles = DIV_ROUND_UP(pixels * bpp_x16, 16 * symbol_size * lane_count);
4740 	int align = is_mst ? 4 / lane_count : 1;
4741 
4742 	return ALIGN(cycles, align);
4743 }
4744 
4745 /**
4746  * drm_dp_link_symbol_cycles - calculate the link symbol count with/without dsc
4747  * @lane_count: DP link lane count
4748  * @pixels: number of pixels in a scanline
4749  * @dsc_slice_count: number of slices for DSC or '0' for non-DSC
4750  * @bpp_x16: bits per pixel in .4 binary fixed format
4751  * @symbol_size: DP symbol size
4752  * @is_mst: %true for MST and %false for SST
4753  *
4754  * Calculate the link symbol cycles for both DSC (@dsc_slice_count !=0) and
4755  * non-DSC case (@dsc_slice_count == 0) and return the count.
4756  */
4757 int drm_dp_link_symbol_cycles(int lane_count, int pixels, int dsc_slice_count,
4758 			      int bpp_x16, int symbol_size, bool is_mst)
4759 {
4760 	int slice_count = dsc_slice_count ? : 1;
4761 	int slice_pixels = DIV_ROUND_UP(pixels, slice_count);
4762 	int slice_data_cycles = drm_dp_link_data_symbol_cycles(lane_count,
4763 							       slice_pixels,
4764 							       bpp_x16,
4765 							       symbol_size,
4766 							       is_mst);
4767 	int slice_eoc_cycles = 0;
4768 
4769 	if (dsc_slice_count)
4770 		slice_eoc_cycles = is_mst ? 4 / lane_count : 1;
4771 
4772 	return slice_count * (slice_data_cycles + slice_eoc_cycles);
4773 }
4774 EXPORT_SYMBOL(drm_dp_link_symbol_cycles);
4775 
4776 /**
4777  * drm_dp_bw_overhead - Calculate the BW overhead of a DP link stream
4778  * @lane_count: DP link lane count
4779  * @hactive: pixel count of the active period in one scanline of the stream
4780  * @dsc_slice_count: number of slices for DSC or '0' for non-DSC
4781  * @bpp_x16: bits per pixel in .4 binary fixed point
4782  * @flags: DRM_DP_OVERHEAD_x flags
4783  *
4784  * Calculate the BW allocation overhead of a DP link stream, depending
4785  * on the link's
4786  * - @lane_count
4787  * - SST/MST mode (@flags / %DRM_DP_OVERHEAD_MST)
4788  * - symbol size (@flags / %DRM_DP_OVERHEAD_UHBR)
4789  * - FEC mode (@flags / %DRM_DP_OVERHEAD_FEC)
4790  * - SSC/REF_CLK mode (@flags / %DRM_DP_OVERHEAD_SSC_REF_CLK)
4791  * as well as the stream's
4792  * - @hactive timing
4793  * - @bpp_x16 color depth
4794  * - compression mode (@dsc_slice_count != 0)
4795  * Note that this overhead doesn't account for the 8b/10b, 128b/132b
4796  * channel coding efficiency, for that see
4797  * @drm_dp_link_bw_channel_coding_efficiency().
4798  *
4799  * Returns the overhead as 100% + overhead% in 1ppm units.
4800  */
4801 int drm_dp_bw_overhead(int lane_count, int hactive,
4802 		       int dsc_slice_count,
4803 		       int bpp_x16, unsigned long flags)
4804 {
4805 	int symbol_size = flags & DRM_DP_BW_OVERHEAD_UHBR ? 32 : 8;
4806 	bool is_mst = flags & DRM_DP_BW_OVERHEAD_MST;
4807 	u32 overhead = 1000000;
4808 	int symbol_cycles;
4809 
4810 	if (lane_count == 0 || hactive == 0 || bpp_x16 == 0) {
4811 		DRM_DEBUG_KMS("Invalid BW overhead params: lane_count %d, hactive %d, bpp_x16 " FXP_Q4_FMT "\n",
4812 			      lane_count, hactive,
4813 			      FXP_Q4_ARGS(bpp_x16));
4814 		return 0;
4815 	}
4816 
4817 	/*
4818 	 * DP Standard v2.1 2.6.4.1
4819 	 * SSC downspread and ref clock variation margin:
4820 	 *   5300ppm + 300ppm ~ 0.6%
4821 	 */
4822 	if (flags & DRM_DP_BW_OVERHEAD_SSC_REF_CLK)
4823 		overhead += 6000;
4824 
4825 	/*
4826 	 * DP Standard v2.1 2.6.4.1.1, 3.5.1.5.4:
4827 	 * FEC symbol insertions for 8b/10b channel coding:
4828 	 * After each 250 data symbols on 2-4 lanes:
4829 	 *   250 LL + 5 FEC_PARITY_PH + 1 CD_ADJ   (256 byte FEC block)
4830 	 * After each 2 x 250 data symbols on 1 lane:
4831 	 *   2 * 250 LL + 11 FEC_PARITY_PH + 1 CD_ADJ (512 byte FEC block)
4832 	 * After 256 (2-4 lanes) or 128 (1 lane) FEC blocks:
4833 	 *   256 * 256 bytes + 1 FEC_PM
4834 	 * or
4835 	 *   128 * 512 bytes + 1 FEC_PM
4836 	 * (256 * 6 + 1) / (256 * 250) = 2.4015625 %
4837 	 */
4838 	if (flags & DRM_DP_BW_OVERHEAD_FEC)
4839 		overhead += 24016;
4840 
4841 	/*
4842 	 * DP Standard v2.1 2.7.9, 5.9.7
4843 	 * The FEC overhead for UHBR is accounted for in its 96.71% channel
4844 	 * coding efficiency.
4845 	 */
4846 	WARN_ON((flags & DRM_DP_BW_OVERHEAD_UHBR) &&
4847 		(flags & DRM_DP_BW_OVERHEAD_FEC));
4848 
4849 	symbol_cycles = drm_dp_link_symbol_cycles(lane_count, hactive,
4850 						  dsc_slice_count,
4851 						  bpp_x16, symbol_size,
4852 						  is_mst);
4853 
4854 	return DIV_ROUND_UP_ULL(mul_u32_u32(symbol_cycles * symbol_size * lane_count,
4855 					    overhead * 16),
4856 				hactive * bpp_x16);
4857 }
4858 EXPORT_SYMBOL(drm_dp_bw_overhead);
4859 
4860 /**
4861  * drm_dp_bw_channel_coding_efficiency - Get a DP link's channel coding efficiency
4862  * @is_uhbr: Whether the link has a 128b/132b channel coding
4863  *
4864  * Return the channel coding efficiency of the given DP link type, which is
4865  * either 8b/10b or 128b/132b (aka UHBR). The corresponding overhead includes
4866  * the 8b -> 10b, 128b -> 132b pixel data to link symbol conversion overhead
4867  * and for 128b/132b any link or PHY level control symbol insertion overhead
4868  * (LLCP, FEC, PHY sync, see DP Standard v2.1 3.5.2.18). For 8b/10b the
4869  * corresponding FEC overhead is BW allocation specific, included in the value
4870  * returned by drm_dp_bw_overhead().
4871  *
4872  * Returns the efficiency in the 100%/coding-overhead% ratio in
4873  * 1ppm units.
4874  */
4875 int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)
4876 {
4877 	if (is_uhbr)
4878 		return 967100;
4879 	else
4880 		/*
4881 		 * Note that on 8b/10b MST the efficiency is only
4882 		 * 78.75% due to the 1 out of 64 MTPH packet overhead,
4883 		 * not accounted for here.
4884 		 */
4885 		return 800000;
4886 }
4887 EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);
4888 
4889 /**
4890  * drm_dp_max_dprx_data_rate - Get the max data bandwidth of a DPRX sink
4891  * @max_link_rate: max DPRX link rate in 10kbps units
4892  * @max_lanes: max DPRX lane count
4893  *
4894  * Given a link rate and lanes, get the data bandwidth.
4895  *
4896  * Data bandwidth is the actual payload rate, which depends on the data
4897  * bandwidth efficiency and the link rate.
4898  *
4899  * Note that protocol layers above the DPRX link level considered here can
4900  * further limit the maximum data rate. Such layers are the MST topology (with
4901  * limits on the link between the source and first branch device as well as on
4902  * the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -
4903  * which in turn can encapsulate an MST link with its own limit - with each
4904  * SST or MST encapsulated tunnel sharing the BW of a tunnel group.
4905  *
4906  * Returns the maximum data rate in kBps units.
4907  */
4908 int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)
4909 {
4910 	int ch_coding_efficiency =
4911 		drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));
4912 
4913 	return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate * 10 * max_lanes,
4914 					      ch_coding_efficiency),
4915 				  1000000 * 8);
4916 }
4917 EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);
4918