• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/delay.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/seq_file.h>
31 
32 #include <drm/drm_dp_helper.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_vblank.h>
35 #include <drm/drm_dp_mst_helper.h>
36 #include <drm/drm_panel.h>
37 
38 #include "drm_crtc_helper_internal.h"
39 
40 struct dp_aux_backlight {
41 	struct backlight_device *base;
42 	struct drm_dp_aux *aux;
43 	struct drm_edp_backlight_info info;
44 	bool enabled;
45 };
46 
47 /**
48  * DOC: dp helpers
49  *
50  * These functions contain some common logic and helpers at various abstraction
51  * levels to deal with Display Port sink devices and related things like DP aux
52  * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
53  * blocks, ...
54  */
55 
56 /* Helpers for DP link training */
dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE],int r)57 static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
58 {
59 	return link_status[r - DP_LANE0_1_STATUS];
60 }
61 
dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],int lane)62 static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
63 			     int lane)
64 {
65 	int i = DP_LANE0_1_STATUS + (lane >> 1);
66 	int s = (lane & 1) * 4;
67 	u8 l = dp_link_status(link_status, i);
68 
69 	return (l >> s) & 0xf;
70 }
71 
drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],int lane_count)72 bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
73 			  int lane_count)
74 {
75 	u8 lane_align;
76 	u8 lane_status;
77 	int lane;
78 
79 	lane_align = dp_link_status(link_status,
80 				    DP_LANE_ALIGN_STATUS_UPDATED);
81 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
82 		return false;
83 	for (lane = 0; lane < lane_count; lane++) {
84 		lane_status = dp_get_lane_status(link_status, lane);
85 		if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
86 			return false;
87 	}
88 	return true;
89 }
90 EXPORT_SYMBOL(drm_dp_channel_eq_ok);
91 
drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],int lane_count)92 bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
93 			      int lane_count)
94 {
95 	int lane;
96 	u8 lane_status;
97 
98 	for (lane = 0; lane < lane_count; lane++) {
99 		lane_status = dp_get_lane_status(link_status, lane);
100 		if ((lane_status & DP_LANE_CR_DONE) == 0)
101 			return false;
102 	}
103 	return true;
104 }
105 EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
106 
drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],int lane)107 u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
108 				     int lane)
109 {
110 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
111 	int s = ((lane & 1) ?
112 		 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
113 		 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
114 	u8 l = dp_link_status(link_status, i);
115 
116 	return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
117 }
118 EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
119 
drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],int lane)120 u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
121 					  int lane)
122 {
123 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
124 	int s = ((lane & 1) ?
125 		 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
126 		 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
127 	u8 l = dp_link_status(link_status, i);
128 
129 	return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
130 }
131 EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
132 
drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],unsigned int lane)133 u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
134 					 unsigned int lane)
135 {
136 	unsigned int offset = DP_ADJUST_REQUEST_POST_CURSOR2;
137 	u8 value = dp_link_status(link_status, offset);
138 
139 	return (value >> (lane << 1)) & 0x3;
140 }
141 EXPORT_SYMBOL(drm_dp_get_adjust_request_post_cursor);
142 
drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux * aux,const u8 dpcd[DP_RECEIVER_CAP_SIZE])143 void drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux *aux,
144 					    const u8 dpcd[DP_RECEIVER_CAP_SIZE])
145 {
146 	unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
147 					 DP_TRAINING_AUX_RD_MASK;
148 
149 	if (rd_interval > 4)
150 		drm_dbg_kms(aux->drm_dev, "%s: AUX interval %lu, out of range (max 4)\n",
151 			    aux->name, rd_interval);
152 
153 	if (rd_interval == 0 || dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
154 		rd_interval = 100;
155 	else
156 		rd_interval *= 4 * USEC_PER_MSEC;
157 
158 	usleep_range(rd_interval, rd_interval * 2);
159 }
160 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
161 
__drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux * aux,unsigned long rd_interval)162 static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
163 						 unsigned long rd_interval)
164 {
165 	if (rd_interval > 4)
166 		drm_dbg_kms(aux->drm_dev, "%s: AUX interval %lu, out of range (max 4)\n",
167 			    aux->name, rd_interval);
168 
169 	if (rd_interval == 0)
170 		rd_interval = 400;
171 	else
172 		rd_interval *= 4 * USEC_PER_MSEC;
173 
174 	usleep_range(rd_interval, rd_interval * 2);
175 }
176 
drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux * aux,const u8 dpcd[DP_RECEIVER_CAP_SIZE])177 void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
178 					const u8 dpcd[DP_RECEIVER_CAP_SIZE])
179 {
180 	__drm_dp_link_train_channel_eq_delay(aux,
181 					     dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
182 					     DP_TRAINING_AUX_RD_MASK);
183 }
184 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
185 
drm_dp_lttpr_link_train_clock_recovery_delay(void)186 void drm_dp_lttpr_link_train_clock_recovery_delay(void)
187 {
188 	usleep_range(100, 200);
189 }
190 EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
191 
dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE],int r)192 static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
193 {
194 	return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
195 }
196 
drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux * aux,const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])197 void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
198 					      const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
199 {
200 	u8 interval = dp_lttpr_phy_cap(phy_cap,
201 				       DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
202 		      DP_TRAINING_AUX_RD_MASK;
203 
204 	__drm_dp_link_train_channel_eq_delay(aux, interval);
205 }
206 EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
207 
drm_dp_link_rate_to_bw_code(int link_rate)208 u8 drm_dp_link_rate_to_bw_code(int link_rate)
209 {
210 	/* Spec says link_bw = link_rate / 0.27Gbps */
211 	return link_rate / 27000;
212 }
213 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
214 
drm_dp_bw_code_to_link_rate(u8 link_bw)215 int drm_dp_bw_code_to_link_rate(u8 link_bw)
216 {
217 	/* Spec says link_rate = link_bw * 0.27Gbps */
218 	return link_bw * 27000;
219 }
220 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
221 
222 #define AUX_RETRY_INTERVAL 500 /* us */
223 
224 static inline void
drm_dp_dump_access(const struct drm_dp_aux * aux,u8 request,uint offset,void * buffer,int ret)225 drm_dp_dump_access(const struct drm_dp_aux *aux,
226 		   u8 request, uint offset, void *buffer, int ret)
227 {
228 	const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
229 
230 	if (ret > 0)
231 		drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
232 			   aux->name, offset, arrow, ret, min(ret, 20), buffer);
233 	else
234 		drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",
235 			   aux->name, offset, arrow, ret);
236 }
237 
238 /**
239  * DOC: dp helpers
240  *
241  * The DisplayPort AUX channel is an abstraction to allow generic, driver-
242  * independent access to AUX functionality. Drivers can take advantage of
243  * this by filling in the fields of the drm_dp_aux structure.
244  *
245  * Transactions are described using a hardware-independent drm_dp_aux_msg
246  * structure, which is passed into a driver's .transfer() implementation.
247  * Both native and I2C-over-AUX transactions are supported.
248  */
249 
drm_dp_dpcd_access(struct drm_dp_aux * aux,u8 request,unsigned int offset,void * buffer,size_t size)250 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
251 			      unsigned int offset, void *buffer, size_t size)
252 {
253 	struct drm_dp_aux_msg msg;
254 	unsigned int retry, native_reply;
255 	int err = 0, ret = 0;
256 
257 	memset(&msg, 0, sizeof(msg));
258 	msg.address = offset;
259 	msg.request = request;
260 	msg.buffer = buffer;
261 	msg.size = size;
262 
263 	mutex_lock(&aux->hw_mutex);
264 
265 	/*
266 	 * The specification doesn't give any recommendation on how often to
267 	 * retry native transactions. We used to retry 7 times like for
268 	 * aux i2c transactions but real world devices this wasn't
269 	 * sufficient, bump to 32 which makes Dell 4k monitors happier.
270 	 */
271 	for (retry = 0; retry < 32; retry++) {
272 		if (ret != 0 && ret != -ETIMEDOUT) {
273 			usleep_range(AUX_RETRY_INTERVAL,
274 				     AUX_RETRY_INTERVAL + 100);
275 		}
276 
277 		ret = aux->transfer(aux, &msg);
278 		if (ret >= 0) {
279 			native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
280 			if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
281 				if (ret == size)
282 					goto unlock;
283 
284 				ret = -EPROTO;
285 			} else
286 				ret = -EIO;
287 		}
288 
289 		/*
290 		 * We want the error we return to be the error we received on
291 		 * the first transaction, since we may get a different error the
292 		 * next time we retry
293 		 */
294 		if (!err)
295 			err = ret;
296 	}
297 
298 	drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",
299 		    aux->name, err);
300 	ret = err;
301 
302 unlock:
303 	mutex_unlock(&aux->hw_mutex);
304 	return ret;
305 }
306 
307 /**
308  * drm_dp_dpcd_read() - read a series of bytes from the DPCD
309  * @aux: DisplayPort AUX channel (SST or MST)
310  * @offset: address of the (first) register to read
311  * @buffer: buffer to store the register values
312  * @size: number of bytes in @buffer
313  *
314  * Returns the number of bytes transferred on success, or a negative error
315  * code on failure. -EIO is returned if the request was NAKed by the sink or
316  * if the retry count was exceeded. If not all bytes were transferred, this
317  * function returns -EPROTO. Errors from the underlying AUX channel transfer
318  * function, with the exception of -EBUSY (which causes the transaction to
319  * be retried), are propagated to the caller.
320  */
drm_dp_dpcd_read(struct drm_dp_aux * aux,unsigned int offset,void * buffer,size_t size)321 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
322 			 void *buffer, size_t size)
323 {
324 	int ret;
325 
326 	/*
327 	 * HP ZR24w corrupts the first DPCD access after entering power save
328 	 * mode. Eg. on a read, the entire buffer will be filled with the same
329 	 * byte. Do a throw away read to avoid corrupting anything we care
330 	 * about. Afterwards things will work correctly until the monitor
331 	 * gets woken up and subsequently re-enters power save mode.
332 	 *
333 	 * The user pressing any button on the monitor is enough to wake it
334 	 * up, so there is no particularly good place to do the workaround.
335 	 * We just have to do it before any DPCD access and hope that the
336 	 * monitor doesn't power down exactly after the throw away read.
337 	 */
338 	if (!aux->is_remote) {
339 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
340 					 buffer, 1);
341 		if (ret != 1)
342 			goto out;
343 	}
344 
345 	if (aux->is_remote)
346 		ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
347 	else
348 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
349 					 buffer, size);
350 
351 out:
352 	drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
353 	return ret;
354 }
355 EXPORT_SYMBOL(drm_dp_dpcd_read);
356 
357 /**
358  * drm_dp_dpcd_write() - write a series of bytes to the DPCD
359  * @aux: DisplayPort AUX channel (SST or MST)
360  * @offset: address of the (first) register to write
361  * @buffer: buffer containing the values to write
362  * @size: number of bytes in @buffer
363  *
364  * Returns the number of bytes transferred on success, or a negative error
365  * code on failure. -EIO is returned if the request was NAKed by the sink or
366  * if the retry count was exceeded. If not all bytes were transferred, this
367  * function returns -EPROTO. Errors from the underlying AUX channel transfer
368  * function, with the exception of -EBUSY (which causes the transaction to
369  * be retried), are propagated to the caller.
370  */
drm_dp_dpcd_write(struct drm_dp_aux * aux,unsigned int offset,void * buffer,size_t size)371 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
372 			  void *buffer, size_t size)
373 {
374 	int ret;
375 
376 	if (aux->is_remote)
377 		ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
378 	else
379 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
380 					 buffer, size);
381 
382 	drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
383 	return ret;
384 }
385 EXPORT_SYMBOL(drm_dp_dpcd_write);
386 
387 /**
388  * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
389  * @aux: DisplayPort AUX channel
390  * @status: buffer to store the link status in (must be at least 6 bytes)
391  *
392  * Returns the number of bytes transferred on success or a negative error
393  * code on failure.
394  */
drm_dp_dpcd_read_link_status(struct drm_dp_aux * aux,u8 status[DP_LINK_STATUS_SIZE])395 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
396 				 u8 status[DP_LINK_STATUS_SIZE])
397 {
398 	return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
399 				DP_LINK_STATUS_SIZE);
400 }
401 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
402 
403 /**
404  * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
405  * @aux: DisplayPort AUX channel
406  * @dp_phy: the DP PHY to get the link status for
407  * @link_status: buffer to return the status in
408  *
409  * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
410  * layout of the returned @link_status matches the DPCD register layout of the
411  * DPRX PHY link status.
412  *
413  * Returns 0 if the information was read successfully or a negative error code
414  * on failure.
415  */
drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux * aux,enum drm_dp_phy dp_phy,u8 link_status[DP_LINK_STATUS_SIZE])416 int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
417 				     enum drm_dp_phy dp_phy,
418 				     u8 link_status[DP_LINK_STATUS_SIZE])
419 {
420 	int ret;
421 
422 	if (dp_phy == DP_PHY_DPRX) {
423 		ret = drm_dp_dpcd_read(aux,
424 				       DP_LANE0_1_STATUS,
425 				       link_status,
426 				       DP_LINK_STATUS_SIZE);
427 
428 		if (ret < 0)
429 			return ret;
430 
431 		WARN_ON(ret != DP_LINK_STATUS_SIZE);
432 
433 		return 0;
434 	}
435 
436 	ret = drm_dp_dpcd_read(aux,
437 			       DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
438 			       link_status,
439 			       DP_LINK_STATUS_SIZE - 1);
440 
441 	if (ret < 0)
442 		return ret;
443 
444 	WARN_ON(ret != DP_LINK_STATUS_SIZE - 1);
445 
446 	/* Convert the LTTPR to the sink PHY link status layout */
447 	memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
448 		&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
449 		DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
450 	link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
451 
452 	return 0;
453 }
454 EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
455 
is_edid_digital_input_dp(const struct edid * edid)456 static bool is_edid_digital_input_dp(const struct edid *edid)
457 {
458 	return edid && edid->revision >= 4 &&
459 		edid->input & DRM_EDID_INPUT_DIGITAL &&
460 		(edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
461 }
462 
463 /**
464  * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
465  * @dpcd: DisplayPort configuration data
466  * @port_cap: port capabilities
467  * @type: port type to be checked. Can be:
468  * 	  %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
469  * 	  %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
470  *	  %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
471  *
472  * Caveat: Only works with DPCD 1.1+ port caps.
473  *
474  * Returns: whether the downstream facing port matches the type.
475  */
drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],u8 type)476 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
477 			       const u8 port_cap[4], u8 type)
478 {
479 	return drm_dp_is_branch(dpcd) &&
480 		dpcd[DP_DPCD_REV] >= 0x11 &&
481 		(port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
482 }
483 EXPORT_SYMBOL(drm_dp_downstream_is_type);
484 
485 /**
486  * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
487  * @dpcd: DisplayPort configuration data
488  * @port_cap: port capabilities
489  * @edid: EDID
490  *
491  * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
492  */
drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],const struct edid * edid)493 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
494 			       const u8 port_cap[4],
495 			       const struct edid *edid)
496 {
497 	if (dpcd[DP_DPCD_REV] < 0x11) {
498 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
499 		case DP_DWN_STRM_PORT_TYPE_TMDS:
500 			return true;
501 		default:
502 			return false;
503 		}
504 	}
505 
506 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
507 	case DP_DS_PORT_TYPE_DP_DUALMODE:
508 		if (is_edid_digital_input_dp(edid))
509 			return false;
510 		fallthrough;
511 	case DP_DS_PORT_TYPE_DVI:
512 	case DP_DS_PORT_TYPE_HDMI:
513 		return true;
514 	default:
515 		return false;
516 	}
517 }
518 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
519 
520 /**
521  * drm_dp_send_real_edid_checksum() - send back real edid checksum value
522  * @aux: DisplayPort AUX channel
523  * @real_edid_checksum: real edid checksum for the last block
524  *
525  * Returns:
526  * True on success
527  */
drm_dp_send_real_edid_checksum(struct drm_dp_aux * aux,u8 real_edid_checksum)528 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
529 				    u8 real_edid_checksum)
530 {
531 	u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
532 
533 	if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
534 			     &auto_test_req, 1) < 1) {
535 		drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
536 			aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
537 		return false;
538 	}
539 	auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
540 
541 	if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
542 		drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
543 			aux->name, DP_TEST_REQUEST);
544 		return false;
545 	}
546 	link_edid_read &= DP_TEST_LINK_EDID_READ;
547 
548 	if (!auto_test_req || !link_edid_read) {
549 		drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",
550 			    aux->name);
551 		return false;
552 	}
553 
554 	if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
555 			      &auto_test_req, 1) < 1) {
556 		drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
557 			aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
558 		return false;
559 	}
560 
561 	/* send back checksum for the last edid extension block data */
562 	if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
563 			      &real_edid_checksum, 1) < 1) {
564 		drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
565 			aux->name, DP_TEST_EDID_CHECKSUM);
566 		return false;
567 	}
568 
569 	test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
570 	if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
571 		drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
572 			aux->name, DP_TEST_RESPONSE);
573 		return false;
574 	}
575 
576 	return true;
577 }
578 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
579 
drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])580 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
581 {
582 	u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
583 
584 	if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
585 		port_count = 4;
586 
587 	return port_count;
588 }
589 
drm_dp_read_extended_dpcd_caps(struct drm_dp_aux * aux,u8 dpcd[DP_RECEIVER_CAP_SIZE])590 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
591 					  u8 dpcd[DP_RECEIVER_CAP_SIZE])
592 {
593 	u8 dpcd_ext[6];
594 	int ret;
595 
596 	/*
597 	 * Prior to DP1.3 the bit represented by
598 	 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
599 	 * If it is set DP_DPCD_REV at 0000h could be at a value less than
600 	 * the true capability of the panel. The only way to check is to
601 	 * then compare 0000h and 2200h.
602 	 */
603 	if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
604 	      DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
605 		return 0;
606 
607 	ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
608 			       sizeof(dpcd_ext));
609 	if (ret < 0)
610 		return ret;
611 	if (ret != sizeof(dpcd_ext))
612 		return -EIO;
613 
614 	if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
615 		drm_dbg_kms(aux->drm_dev,
616 			    "%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
617 			    aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
618 		return 0;
619 	}
620 
621 	if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
622 		return 0;
623 
624 	drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
625 
626 	memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
627 
628 	return 0;
629 }
630 
631 /**
632  * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
633  * available
634  * @aux: DisplayPort AUX channel
635  * @dpcd: Buffer to store the resulting DPCD in
636  *
637  * Attempts to read the base DPCD caps for @aux. Additionally, this function
638  * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
639  * present.
640  *
641  * Returns: %0 if the DPCD was read successfully, negative error code
642  * otherwise.
643  */
drm_dp_read_dpcd_caps(struct drm_dp_aux * aux,u8 dpcd[DP_RECEIVER_CAP_SIZE])644 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
645 			  u8 dpcd[DP_RECEIVER_CAP_SIZE])
646 {
647 	int ret;
648 
649 	ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
650 	if (ret < 0)
651 		return ret;
652 	if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
653 		return -EIO;
654 
655 	ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
656 	if (ret < 0)
657 		return ret;
658 
659 	drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
660 
661 	return ret;
662 }
663 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
664 
665 /**
666  * drm_dp_read_downstream_info() - read DPCD downstream port info if available
667  * @aux: DisplayPort AUX channel
668  * @dpcd: A cached copy of the port's DPCD
669  * @downstream_ports: buffer to store the downstream port info in
670  *
671  * See also:
672  * drm_dp_downstream_max_clock()
673  * drm_dp_downstream_max_bpc()
674  *
675  * Returns: 0 if either the downstream port info was read successfully or
676  * there was no downstream info to read, or a negative error code otherwise.
677  */
drm_dp_read_downstream_info(struct drm_dp_aux * aux,const u8 dpcd[DP_RECEIVER_CAP_SIZE],u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])678 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
679 				const u8 dpcd[DP_RECEIVER_CAP_SIZE],
680 				u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
681 {
682 	int ret;
683 	u8 len;
684 
685 	memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
686 
687 	/* No downstream info to read */
688 	if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)
689 		return 0;
690 
691 	/* Some branches advertise having 0 downstream ports, despite also advertising they have a
692 	 * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
693 	 * some branches do it we need to handle it regardless.
694 	 */
695 	len = drm_dp_downstream_port_count(dpcd);
696 	if (!len)
697 		return 0;
698 
699 	if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
700 		len *= 4;
701 
702 	ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
703 	if (ret < 0)
704 		return ret;
705 	if (ret != len)
706 		return -EIO;
707 
708 	drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);
709 
710 	return 0;
711 }
712 EXPORT_SYMBOL(drm_dp_read_downstream_info);
713 
714 /**
715  * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
716  * @dpcd: DisplayPort configuration data
717  * @port_cap: port capabilities
718  *
719  * Returns: Downstream facing port max dot clock in kHz on success,
720  * or 0 if max clock not defined
721  */
drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])722 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
723 				   const u8 port_cap[4])
724 {
725 	if (!drm_dp_is_branch(dpcd))
726 		return 0;
727 
728 	if (dpcd[DP_DPCD_REV] < 0x11)
729 		return 0;
730 
731 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
732 	case DP_DS_PORT_TYPE_VGA:
733 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
734 			return 0;
735 		return port_cap[1] * 8000;
736 	default:
737 		return 0;
738 	}
739 }
740 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
741 
742 /**
743  * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
744  * @dpcd: DisplayPort configuration data
745  * @port_cap: port capabilities
746  * @edid: EDID
747  *
748  * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
749  * or 0 if max TMDS clock not defined
750  */
drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],const struct edid * edid)751 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
752 				     const u8 port_cap[4],
753 				     const struct edid *edid)
754 {
755 	if (!drm_dp_is_branch(dpcd))
756 		return 0;
757 
758 	if (dpcd[DP_DPCD_REV] < 0x11) {
759 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
760 		case DP_DWN_STRM_PORT_TYPE_TMDS:
761 			return 165000;
762 		default:
763 			return 0;
764 		}
765 	}
766 
767 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
768 	case DP_DS_PORT_TYPE_DP_DUALMODE:
769 		if (is_edid_digital_input_dp(edid))
770 			return 0;
771 		/*
772 		 * It's left up to the driver to check the
773 		 * DP dual mode adapter's max TMDS clock.
774 		 *
775 		 * Unfortunately it looks like branch devices
776 		 * may not fordward that the DP dual mode i2c
777 		 * access so we just usually get i2c nak :(
778 		 */
779 		fallthrough;
780 	case DP_DS_PORT_TYPE_HDMI:
781 		 /*
782 		  * We should perhaps assume 165 MHz when detailed cap
783 		  * info is not available. But looks like many typical
784 		  * branch devices fall into that category and so we'd
785 		  * probably end up with users complaining that they can't
786 		  * get high resolution modes with their favorite dongle.
787 		  *
788 		  * So let's limit to 300 MHz instead since DPCD 1.4
789 		  * HDMI 2.0 DFPs are required to have the detailed cap
790 		  * info. So it's more likely we're dealing with a HDMI 1.4
791 		  * compatible* device here.
792 		  */
793 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
794 			return 300000;
795 		return port_cap[1] * 2500;
796 	case DP_DS_PORT_TYPE_DVI:
797 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
798 			return 165000;
799 		/* FIXME what to do about DVI dual link? */
800 		return port_cap[1] * 2500;
801 	default:
802 		return 0;
803 	}
804 }
805 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
806 
807 /**
808  * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
809  * @dpcd: DisplayPort configuration data
810  * @port_cap: port capabilities
811  * @edid: EDID
812  *
813  * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
814  * or 0 if max TMDS clock not defined
815  */
drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],const struct edid * edid)816 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
817 				     const u8 port_cap[4],
818 				     const struct edid *edid)
819 {
820 	if (!drm_dp_is_branch(dpcd))
821 		return 0;
822 
823 	if (dpcd[DP_DPCD_REV] < 0x11) {
824 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
825 		case DP_DWN_STRM_PORT_TYPE_TMDS:
826 			return 25000;
827 		default:
828 			return 0;
829 		}
830 	}
831 
832 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
833 	case DP_DS_PORT_TYPE_DP_DUALMODE:
834 		if (is_edid_digital_input_dp(edid))
835 			return 0;
836 		fallthrough;
837 	case DP_DS_PORT_TYPE_DVI:
838 	case DP_DS_PORT_TYPE_HDMI:
839 		/*
840 		 * Unclear whether the protocol converter could
841 		 * utilize pixel replication. Assume it won't.
842 		 */
843 		return 25000;
844 	default:
845 		return 0;
846 	}
847 }
848 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
849 
850 /**
851  * drm_dp_downstream_max_bpc() - extract downstream facing port max
852  *                               bits per component
853  * @dpcd: DisplayPort configuration data
854  * @port_cap: downstream facing port capabilities
855  * @edid: EDID
856  *
857  * Returns: Max bpc on success or 0 if max bpc not defined
858  */
drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],const struct edid * edid)859 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
860 			      const u8 port_cap[4],
861 			      const struct edid *edid)
862 {
863 	if (!drm_dp_is_branch(dpcd))
864 		return 0;
865 
866 	if (dpcd[DP_DPCD_REV] < 0x11) {
867 		switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
868 		case DP_DWN_STRM_PORT_TYPE_DP:
869 			return 0;
870 		default:
871 			return 8;
872 		}
873 	}
874 
875 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
876 	case DP_DS_PORT_TYPE_DP:
877 		return 0;
878 	case DP_DS_PORT_TYPE_DP_DUALMODE:
879 		if (is_edid_digital_input_dp(edid))
880 			return 0;
881 		fallthrough;
882 	case DP_DS_PORT_TYPE_HDMI:
883 	case DP_DS_PORT_TYPE_DVI:
884 	case DP_DS_PORT_TYPE_VGA:
885 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
886 			return 8;
887 
888 		switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
889 		case DP_DS_8BPC:
890 			return 8;
891 		case DP_DS_10BPC:
892 			return 10;
893 		case DP_DS_12BPC:
894 			return 12;
895 		case DP_DS_16BPC:
896 			return 16;
897 		default:
898 			return 8;
899 		}
900 		break;
901 	default:
902 		return 8;
903 	}
904 }
905 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
906 
907 /**
908  * drm_dp_downstream_420_passthrough() - determine downstream facing port
909  *                                       YCbCr 4:2:0 pass-through capability
910  * @dpcd: DisplayPort configuration data
911  * @port_cap: downstream facing port capabilities
912  *
913  * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
914  */
drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])915 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
916 				       const u8 port_cap[4])
917 {
918 	if (!drm_dp_is_branch(dpcd))
919 		return false;
920 
921 	if (dpcd[DP_DPCD_REV] < 0x13)
922 		return false;
923 
924 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
925 	case DP_DS_PORT_TYPE_DP:
926 		return true;
927 	case DP_DS_PORT_TYPE_HDMI:
928 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
929 			return false;
930 
931 		return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
932 	default:
933 		return false;
934 	}
935 }
936 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
937 
938 /**
939  * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
940  *                                             YCbCr 4:4:4->4:2:0 conversion capability
941  * @dpcd: DisplayPort configuration data
942  * @port_cap: downstream facing port capabilities
943  *
944  * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
945  */
drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])946 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
947 					     const u8 port_cap[4])
948 {
949 	if (!drm_dp_is_branch(dpcd))
950 		return false;
951 
952 	if (dpcd[DP_DPCD_REV] < 0x13)
953 		return false;
954 
955 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
956 	case DP_DS_PORT_TYPE_HDMI:
957 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
958 			return false;
959 
960 		return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
961 	default:
962 		return false;
963 	}
964 }
965 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
966 
967 /**
968  * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
969  *                                               RGB->YCbCr conversion capability
970  * @dpcd: DisplayPort configuration data
971  * @port_cap: downstream facing port capabilities
972  * @color_spc: Colorspace for which conversion cap is sought
973  *
974  * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
975  * colorspace.
976  */
drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],u8 color_spc)977 bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
978 					       const u8 port_cap[4],
979 					       u8 color_spc)
980 {
981 	if (!drm_dp_is_branch(dpcd))
982 		return false;
983 
984 	if (dpcd[DP_DPCD_REV] < 0x13)
985 		return false;
986 
987 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
988 	case DP_DS_PORT_TYPE_HDMI:
989 		if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
990 			return false;
991 
992 		return port_cap[3] & color_spc;
993 	default:
994 		return false;
995 	}
996 }
997 EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
998 
999 /**
1000  * drm_dp_downstream_mode() - return a mode for downstream facing port
1001  * @dev: DRM device
1002  * @dpcd: DisplayPort configuration data
1003  * @port_cap: port capabilities
1004  *
1005  * Provides a suitable mode for downstream facing ports without EDID.
1006  *
1007  * Returns: A new drm_display_mode on success or NULL on failure
1008  */
1009 struct drm_display_mode *
drm_dp_downstream_mode(struct drm_device * dev,const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])1010 drm_dp_downstream_mode(struct drm_device *dev,
1011 		       const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1012 		       const u8 port_cap[4])
1013 
1014 {
1015 	u8 vic;
1016 
1017 	if (!drm_dp_is_branch(dpcd))
1018 		return NULL;
1019 
1020 	if (dpcd[DP_DPCD_REV] < 0x11)
1021 		return NULL;
1022 
1023 	switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1024 	case DP_DS_PORT_TYPE_NON_EDID:
1025 		switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
1026 		case DP_DS_NON_EDID_720x480i_60:
1027 			vic = 6;
1028 			break;
1029 		case DP_DS_NON_EDID_720x480i_50:
1030 			vic = 21;
1031 			break;
1032 		case DP_DS_NON_EDID_1920x1080i_60:
1033 			vic = 5;
1034 			break;
1035 		case DP_DS_NON_EDID_1920x1080i_50:
1036 			vic = 20;
1037 			break;
1038 		case DP_DS_NON_EDID_1280x720_60:
1039 			vic = 4;
1040 			break;
1041 		case DP_DS_NON_EDID_1280x720_50:
1042 			vic = 19;
1043 			break;
1044 		default:
1045 			return NULL;
1046 		}
1047 		return drm_display_mode_from_cea_vic(dev, vic);
1048 	default:
1049 		return NULL;
1050 	}
1051 }
1052 EXPORT_SYMBOL(drm_dp_downstream_mode);
1053 
1054 /**
1055  * drm_dp_downstream_id() - identify branch device
1056  * @aux: DisplayPort AUX channel
1057  * @id: DisplayPort branch device id
1058  *
1059  * Returns branch device id on success or NULL on failure
1060  */
drm_dp_downstream_id(struct drm_dp_aux * aux,char id[6])1061 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
1062 {
1063 	return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
1064 }
1065 EXPORT_SYMBOL(drm_dp_downstream_id);
1066 
1067 /**
1068  * drm_dp_downstream_debug() - debug DP branch devices
1069  * @m: pointer for debugfs file
1070  * @dpcd: DisplayPort configuration data
1071  * @port_cap: port capabilities
1072  * @edid: EDID
1073  * @aux: DisplayPort AUX channel
1074  *
1075  */
drm_dp_downstream_debug(struct seq_file * m,const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],const struct edid * edid,struct drm_dp_aux * aux)1076 void drm_dp_downstream_debug(struct seq_file *m,
1077 			     const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1078 			     const u8 port_cap[4],
1079 			     const struct edid *edid,
1080 			     struct drm_dp_aux *aux)
1081 {
1082 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1083 				 DP_DETAILED_CAP_INFO_AVAILABLE;
1084 	int clk;
1085 	int bpc;
1086 	char id[7];
1087 	int len;
1088 	uint8_t rev[2];
1089 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1090 	bool branch_device = drm_dp_is_branch(dpcd);
1091 
1092 	seq_printf(m, "\tDP branch device present: %s\n",
1093 		   branch_device ? "yes" : "no");
1094 
1095 	if (!branch_device)
1096 		return;
1097 
1098 	switch (type) {
1099 	case DP_DS_PORT_TYPE_DP:
1100 		seq_puts(m, "\t\tType: DisplayPort\n");
1101 		break;
1102 	case DP_DS_PORT_TYPE_VGA:
1103 		seq_puts(m, "\t\tType: VGA\n");
1104 		break;
1105 	case DP_DS_PORT_TYPE_DVI:
1106 		seq_puts(m, "\t\tType: DVI\n");
1107 		break;
1108 	case DP_DS_PORT_TYPE_HDMI:
1109 		seq_puts(m, "\t\tType: HDMI\n");
1110 		break;
1111 	case DP_DS_PORT_TYPE_NON_EDID:
1112 		seq_puts(m, "\t\tType: others without EDID support\n");
1113 		break;
1114 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1115 		seq_puts(m, "\t\tType: DP++\n");
1116 		break;
1117 	case DP_DS_PORT_TYPE_WIRELESS:
1118 		seq_puts(m, "\t\tType: Wireless\n");
1119 		break;
1120 	default:
1121 		seq_puts(m, "\t\tType: N/A\n");
1122 	}
1123 
1124 	memset(id, 0, sizeof(id));
1125 	drm_dp_downstream_id(aux, id);
1126 	seq_printf(m, "\t\tID: %s\n", id);
1127 
1128 	len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1129 	if (len > 0)
1130 		seq_printf(m, "\t\tHW: %d.%d\n",
1131 			   (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1132 
1133 	len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1134 	if (len > 0)
1135 		seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1136 
1137 	if (detailed_cap_info) {
1138 		clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1139 		if (clk > 0)
1140 			seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1141 
1142 		clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1143 		if (clk > 0)
1144 			seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1145 
1146 		clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1147 		if (clk > 0)
1148 			seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1149 
1150 		bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1151 
1152 		if (bpc > 0)
1153 			seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1154 	}
1155 }
1156 EXPORT_SYMBOL(drm_dp_downstream_debug);
1157 
1158 /**
1159  * drm_dp_subconnector_type() - get DP branch device type
1160  * @dpcd: DisplayPort configuration data
1161  * @port_cap: port capabilities
1162  */
1163 enum drm_mode_subconnector
drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])1164 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1165 			 const u8 port_cap[4])
1166 {
1167 	int type;
1168 	if (!drm_dp_is_branch(dpcd))
1169 		return DRM_MODE_SUBCONNECTOR_Native;
1170 	/* DP 1.0 approach */
1171 	if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1172 		type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1173 		       DP_DWN_STRM_PORT_TYPE_MASK;
1174 
1175 		switch (type) {
1176 		case DP_DWN_STRM_PORT_TYPE_TMDS:
1177 			/* Can be HDMI or DVI-D, DVI-D is a safer option */
1178 			return DRM_MODE_SUBCONNECTOR_DVID;
1179 		case DP_DWN_STRM_PORT_TYPE_ANALOG:
1180 			/* Can be VGA or DVI-A, VGA is more popular */
1181 			return DRM_MODE_SUBCONNECTOR_VGA;
1182 		case DP_DWN_STRM_PORT_TYPE_DP:
1183 			return DRM_MODE_SUBCONNECTOR_DisplayPort;
1184 		case DP_DWN_STRM_PORT_TYPE_OTHER:
1185 		default:
1186 			return DRM_MODE_SUBCONNECTOR_Unknown;
1187 		}
1188 	}
1189 	type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1190 
1191 	switch (type) {
1192 	case DP_DS_PORT_TYPE_DP:
1193 	case DP_DS_PORT_TYPE_DP_DUALMODE:
1194 		return DRM_MODE_SUBCONNECTOR_DisplayPort;
1195 	case DP_DS_PORT_TYPE_VGA:
1196 		return DRM_MODE_SUBCONNECTOR_VGA;
1197 	case DP_DS_PORT_TYPE_DVI:
1198 		return DRM_MODE_SUBCONNECTOR_DVID;
1199 	case DP_DS_PORT_TYPE_HDMI:
1200 		return DRM_MODE_SUBCONNECTOR_HDMIA;
1201 	case DP_DS_PORT_TYPE_WIRELESS:
1202 		return DRM_MODE_SUBCONNECTOR_Wireless;
1203 	case DP_DS_PORT_TYPE_NON_EDID:
1204 	default:
1205 		return DRM_MODE_SUBCONNECTOR_Unknown;
1206 	}
1207 }
1208 EXPORT_SYMBOL(drm_dp_subconnector_type);
1209 
1210 /**
1211  * drm_dp_set_subconnector_property - set subconnector for DP connector
1212  * @connector: connector to set property on
1213  * @status: connector status
1214  * @dpcd: DisplayPort configuration data
1215  * @port_cap: port capabilities
1216  *
1217  * Called by a driver on every detect event.
1218  */
drm_dp_set_subconnector_property(struct drm_connector * connector,enum drm_connector_status status,const u8 * dpcd,const u8 port_cap[4])1219 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1220 				      enum drm_connector_status status,
1221 				      const u8 *dpcd,
1222 				      const u8 port_cap[4])
1223 {
1224 	enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1225 
1226 	if (status == connector_status_connected)
1227 		subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1228 	drm_object_property_set_value(&connector->base,
1229 			connector->dev->mode_config.dp_subconnector_property,
1230 			subconnector);
1231 }
1232 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1233 
1234 /**
1235  * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1236  * count
1237  * @connector: The DRM connector to check
1238  * @dpcd: A cached copy of the connector's DPCD RX capabilities
1239  * @desc: A cached copy of the connector's DP descriptor
1240  *
1241  * See also: drm_dp_read_sink_count()
1242  *
1243  * Returns: %True if the (e)DP connector has a valid sink count that should
1244  * be probed, %false otherwise.
1245  */
drm_dp_read_sink_count_cap(struct drm_connector * connector,const u8 dpcd[DP_RECEIVER_CAP_SIZE],const struct drm_dp_desc * desc)1246 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1247 				const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1248 				const struct drm_dp_desc *desc)
1249 {
1250 	/* Some eDP panels don't set a valid value for the sink count */
1251 	return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1252 		dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1253 		dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1254 		!drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);
1255 }
1256 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1257 
1258 /**
1259  * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1260  * @aux: The DP AUX channel to use
1261  *
1262  * See also: drm_dp_read_sink_count_cap()
1263  *
1264  * Returns: The current sink count reported by @aux, or a negative error code
1265  * otherwise.
1266  */
drm_dp_read_sink_count(struct drm_dp_aux * aux)1267 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1268 {
1269 	u8 count;
1270 	int ret;
1271 
1272 	ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1273 	if (ret < 0)
1274 		return ret;
1275 	if (ret != 1)
1276 		return -EIO;
1277 
1278 	return DP_GET_SINK_COUNT(count);
1279 }
1280 EXPORT_SYMBOL(drm_dp_read_sink_count);
1281 
1282 /*
1283  * I2C-over-AUX implementation
1284  */
1285 
drm_dp_i2c_functionality(struct i2c_adapter * adapter)1286 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1287 {
1288 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1289 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1290 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1291 	       I2C_FUNC_10BIT_ADDR;
1292 }
1293 
drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg * msg)1294 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1295 {
1296 	/*
1297 	 * In case of i2c defer or short i2c ack reply to a write,
1298 	 * we need to switch to WRITE_STATUS_UPDATE to drain the
1299 	 * rest of the message
1300 	 */
1301 	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1302 		msg->request &= DP_AUX_I2C_MOT;
1303 		msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1304 	}
1305 }
1306 
1307 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1308 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1309 #define AUX_STOP_LEN 4
1310 #define AUX_CMD_LEN 4
1311 #define AUX_ADDRESS_LEN 20
1312 #define AUX_REPLY_PAD_LEN 4
1313 #define AUX_LENGTH_LEN 8
1314 
1315 /*
1316  * Calculate the duration of the AUX request/reply in usec. Gives the
1317  * "best" case estimate, ie. successful while as short as possible.
1318  */
drm_dp_aux_req_duration(const struct drm_dp_aux_msg * msg)1319 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1320 {
1321 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1322 		AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1323 
1324 	if ((msg->request & DP_AUX_I2C_READ) == 0)
1325 		len += msg->size * 8;
1326 
1327 	return len;
1328 }
1329 
drm_dp_aux_reply_duration(const struct drm_dp_aux_msg * msg)1330 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1331 {
1332 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1333 		AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1334 
1335 	/*
1336 	 * For read we expect what was asked. For writes there will
1337 	 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1338 	 */
1339 	if (msg->request & DP_AUX_I2C_READ)
1340 		len += msg->size * 8;
1341 
1342 	return len;
1343 }
1344 
1345 #define I2C_START_LEN 1
1346 #define I2C_STOP_LEN 1
1347 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1348 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1349 
1350 /*
1351  * Calculate the length of the i2c transfer in usec, assuming
1352  * the i2c bus speed is as specified. Gives the the "worst"
1353  * case estimate, ie. successful while as long as possible.
1354  * Doesn't account the the "MOT" bit, and instead assumes each
1355  * message includes a START, ADDRESS and STOP. Neither does it
1356  * account for additional random variables such as clock stretching.
1357  */
drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg * msg,int i2c_speed_khz)1358 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1359 				   int i2c_speed_khz)
1360 {
1361 	/* AUX bitrate is 1MHz, i2c bitrate as specified */
1362 	return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1363 			     msg->size * I2C_DATA_LEN +
1364 			     I2C_STOP_LEN) * 1000, i2c_speed_khz);
1365 }
1366 
1367 /*
1368  * Determine how many retries should be attempted to successfully transfer
1369  * the specified message, based on the estimated durations of the
1370  * i2c and AUX transfers.
1371  */
drm_dp_i2c_retry_count(const struct drm_dp_aux_msg * msg,int i2c_speed_khz)1372 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1373 			      int i2c_speed_khz)
1374 {
1375 	int aux_time_us = drm_dp_aux_req_duration(msg) +
1376 		drm_dp_aux_reply_duration(msg);
1377 	int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1378 
1379 	return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1380 }
1381 
1382 /*
1383  * FIXME currently assumes 10 kHz as some real world devices seem
1384  * to require it. We should query/set the speed via DPCD if supported.
1385  */
1386 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1387 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1388 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1389 		 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1390 
1391 /*
1392  * Transfer a single I2C-over-AUX message and handle various error conditions,
1393  * retrying the transaction as appropriate.  It is assumed that the
1394  * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1395  * reply field.
1396  *
1397  * Returns bytes transferred on success, or a negative error code on failure.
1398  */
drm_dp_i2c_do_msg(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)1399 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1400 {
1401 	unsigned int retry, defer_i2c;
1402 	int ret;
1403 	/*
1404 	 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1405 	 * is required to retry at least seven times upon receiving AUX_DEFER
1406 	 * before giving up the AUX transaction.
1407 	 *
1408 	 * We also try to account for the i2c bus speed.
1409 	 */
1410 	int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1411 
1412 	for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1413 		ret = aux->transfer(aux, msg);
1414 		if (ret < 0) {
1415 			if (ret == -EBUSY)
1416 				continue;
1417 
1418 			/*
1419 			 * While timeouts can be errors, they're usually normal
1420 			 * behavior (for instance, when a driver tries to
1421 			 * communicate with a non-existent DisplayPort device).
1422 			 * Avoid spamming the kernel log with timeout errors.
1423 			 */
1424 			if (ret == -ETIMEDOUT)
1425 				drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",
1426 							aux->name);
1427 			else
1428 				drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",
1429 					    aux->name, ret);
1430 			return ret;
1431 		}
1432 
1433 
1434 		switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1435 		case DP_AUX_NATIVE_REPLY_ACK:
1436 			/*
1437 			 * For I2C-over-AUX transactions this isn't enough, we
1438 			 * need to check for the I2C ACK reply.
1439 			 */
1440 			break;
1441 
1442 		case DP_AUX_NATIVE_REPLY_NACK:
1443 			drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",
1444 				    aux->name, ret, msg->size);
1445 			return -EREMOTEIO;
1446 
1447 		case DP_AUX_NATIVE_REPLY_DEFER:
1448 			drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);
1449 			/*
1450 			 * We could check for I2C bit rate capabilities and if
1451 			 * available adjust this interval. We could also be
1452 			 * more careful with DP-to-legacy adapters where a
1453 			 * long legacy cable may force very low I2C bit rates.
1454 			 *
1455 			 * For now just defer for long enough to hopefully be
1456 			 * safe for all use-cases.
1457 			 */
1458 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1459 			continue;
1460 
1461 		default:
1462 			drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",
1463 				aux->name, msg->reply);
1464 			return -EREMOTEIO;
1465 		}
1466 
1467 		switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1468 		case DP_AUX_I2C_REPLY_ACK:
1469 			/*
1470 			 * Both native ACK and I2C ACK replies received. We
1471 			 * can assume the transfer was successful.
1472 			 */
1473 			if (ret != msg->size)
1474 				drm_dp_i2c_msg_write_status_update(msg);
1475 			return ret;
1476 
1477 		case DP_AUX_I2C_REPLY_NACK:
1478 			drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",
1479 				    aux->name, ret, msg->size);
1480 			aux->i2c_nack_count++;
1481 			return -EREMOTEIO;
1482 
1483 		case DP_AUX_I2C_REPLY_DEFER:
1484 			drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);
1485 			/* DP Compliance Test 4.2.2.5 Requirement:
1486 			 * Must have at least 7 retries for I2C defers on the
1487 			 * transaction to pass this test
1488 			 */
1489 			aux->i2c_defer_count++;
1490 			if (defer_i2c < 7)
1491 				defer_i2c++;
1492 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1493 			drm_dp_i2c_msg_write_status_update(msg);
1494 
1495 			continue;
1496 
1497 		default:
1498 			drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",
1499 				aux->name, msg->reply);
1500 			return -EREMOTEIO;
1501 		}
1502 	}
1503 
1504 	drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);
1505 	return -EREMOTEIO;
1506 }
1507 
drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg * msg,const struct i2c_msg * i2c_msg)1508 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1509 				       const struct i2c_msg *i2c_msg)
1510 {
1511 	msg->request = (i2c_msg->flags & I2C_M_RD) ?
1512 		DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1513 	if (!(i2c_msg->flags & I2C_M_STOP))
1514 		msg->request |= DP_AUX_I2C_MOT;
1515 }
1516 
1517 /*
1518  * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1519  *
1520  * Returns an error code on failure, or a recommended transfer size on success.
1521  */
drm_dp_i2c_drain_msg(struct drm_dp_aux * aux,struct drm_dp_aux_msg * orig_msg)1522 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1523 {
1524 	int err, ret = orig_msg->size;
1525 	struct drm_dp_aux_msg msg = *orig_msg;
1526 
1527 	while (msg.size > 0) {
1528 		err = drm_dp_i2c_do_msg(aux, &msg);
1529 		if (err <= 0)
1530 			return err == 0 ? -EPROTO : err;
1531 
1532 		if (err < msg.size && err < ret) {
1533 			drm_dbg_kms(aux->drm_dev,
1534 				    "%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1535 				    aux->name, msg.size, err);
1536 			ret = err;
1537 		}
1538 
1539 		msg.size -= err;
1540 		msg.buffer += err;
1541 	}
1542 
1543 	return ret;
1544 }
1545 
1546 /*
1547  * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1548  * packets to be as large as possible. If not, the I2C transactions never
1549  * succeed. Hence the default is maximum.
1550  */
1551 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1552 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1553 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1554 		 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1555 
drm_dp_i2c_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)1556 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1557 			   int num)
1558 {
1559 	struct drm_dp_aux *aux = adapter->algo_data;
1560 	unsigned int i, j;
1561 	unsigned transfer_size;
1562 	struct drm_dp_aux_msg msg;
1563 	int err = 0;
1564 
1565 	dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1566 
1567 	memset(&msg, 0, sizeof(msg));
1568 
1569 	for (i = 0; i < num; i++) {
1570 		msg.address = msgs[i].addr;
1571 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1572 		/* Send a bare address packet to start the transaction.
1573 		 * Zero sized messages specify an address only (bare
1574 		 * address) transaction.
1575 		 */
1576 		msg.buffer = NULL;
1577 		msg.size = 0;
1578 		err = drm_dp_i2c_do_msg(aux, &msg);
1579 
1580 		/*
1581 		 * Reset msg.request in case in case it got
1582 		 * changed into a WRITE_STATUS_UPDATE.
1583 		 */
1584 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1585 
1586 		if (err < 0)
1587 			break;
1588 		/* We want each transaction to be as large as possible, but
1589 		 * we'll go to smaller sizes if the hardware gives us a
1590 		 * short reply.
1591 		 */
1592 		transfer_size = dp_aux_i2c_transfer_size;
1593 		for (j = 0; j < msgs[i].len; j += msg.size) {
1594 			msg.buffer = msgs[i].buf + j;
1595 			msg.size = min(transfer_size, msgs[i].len - j);
1596 
1597 			err = drm_dp_i2c_drain_msg(aux, &msg);
1598 
1599 			/*
1600 			 * Reset msg.request in case in case it got
1601 			 * changed into a WRITE_STATUS_UPDATE.
1602 			 */
1603 			drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1604 
1605 			if (err < 0)
1606 				break;
1607 			transfer_size = err;
1608 		}
1609 		if (err < 0)
1610 			break;
1611 	}
1612 	if (err >= 0)
1613 		err = num;
1614 	/* Send a bare address packet to close out the transaction.
1615 	 * Zero sized messages specify an address only (bare
1616 	 * address) transaction.
1617 	 */
1618 	msg.request &= ~DP_AUX_I2C_MOT;
1619 	msg.buffer = NULL;
1620 	msg.size = 0;
1621 	(void)drm_dp_i2c_do_msg(aux, &msg);
1622 
1623 	return err;
1624 }
1625 
1626 static const struct i2c_algorithm drm_dp_i2c_algo = {
1627 	.functionality = drm_dp_i2c_functionality,
1628 	.master_xfer = drm_dp_i2c_xfer,
1629 };
1630 
i2c_to_aux(struct i2c_adapter * i2c)1631 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1632 {
1633 	return container_of(i2c, struct drm_dp_aux, ddc);
1634 }
1635 
lock_bus(struct i2c_adapter * i2c,unsigned int flags)1636 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1637 {
1638 	mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1639 }
1640 
trylock_bus(struct i2c_adapter * i2c,unsigned int flags)1641 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1642 {
1643 	return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1644 }
1645 
unlock_bus(struct i2c_adapter * i2c,unsigned int flags)1646 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1647 {
1648 	mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1649 }
1650 
1651 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1652 	.lock_bus = lock_bus,
1653 	.trylock_bus = trylock_bus,
1654 	.unlock_bus = unlock_bus,
1655 };
1656 
drm_dp_aux_get_crc(struct drm_dp_aux * aux,u8 * crc)1657 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1658 {
1659 	u8 buf, count;
1660 	int ret;
1661 
1662 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1663 	if (ret < 0)
1664 		return ret;
1665 
1666 	WARN_ON(!(buf & DP_TEST_SINK_START));
1667 
1668 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1669 	if (ret < 0)
1670 		return ret;
1671 
1672 	count = buf & DP_TEST_COUNT_MASK;
1673 	if (count == aux->crc_count)
1674 		return -EAGAIN; /* No CRC yet */
1675 
1676 	aux->crc_count = count;
1677 
1678 	/*
1679 	 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1680 	 * per component (RGB or CrYCb).
1681 	 */
1682 	ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1683 	if (ret < 0)
1684 		return ret;
1685 
1686 	return 0;
1687 }
1688 
drm_dp_aux_crc_work(struct work_struct * work)1689 static void drm_dp_aux_crc_work(struct work_struct *work)
1690 {
1691 	struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1692 					      crc_work);
1693 	struct drm_crtc *crtc;
1694 	u8 crc_bytes[6];
1695 	uint32_t crcs[3];
1696 	int ret;
1697 
1698 	if (WARN_ON(!aux->crtc))
1699 		return;
1700 
1701 	crtc = aux->crtc;
1702 	while (crtc->crc.opened) {
1703 		drm_crtc_wait_one_vblank(crtc);
1704 		if (!crtc->crc.opened)
1705 			break;
1706 
1707 		ret = drm_dp_aux_get_crc(aux, crc_bytes);
1708 		if (ret == -EAGAIN) {
1709 			usleep_range(1000, 2000);
1710 			ret = drm_dp_aux_get_crc(aux, crc_bytes);
1711 		}
1712 
1713 		if (ret == -EAGAIN) {
1714 			drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",
1715 				    aux->name, ret);
1716 			continue;
1717 		} else if (ret) {
1718 			drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);
1719 			continue;
1720 		}
1721 
1722 		crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1723 		crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1724 		crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1725 		drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1726 	}
1727 }
1728 
1729 /**
1730  * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1731  * @aux: DisplayPort AUX channel
1732  *
1733  * Used for remote aux channel in general. Merely initialize the crc work
1734  * struct.
1735  */
drm_dp_remote_aux_init(struct drm_dp_aux * aux)1736 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1737 {
1738 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1739 }
1740 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1741 
1742 /**
1743  * drm_dp_aux_init() - minimally initialise an aux channel
1744  * @aux: DisplayPort AUX channel
1745  *
1746  * If you need to use the drm_dp_aux's i2c adapter prior to registering it with
1747  * the outside world, call drm_dp_aux_init() first. For drivers which are
1748  * grandparents to their AUX adapters (e.g. the AUX adapter is parented by a
1749  * &drm_connector), you must still call drm_dp_aux_register() once the connector
1750  * has been registered to allow userspace access to the auxiliary DP channel.
1751  * Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as
1752  * early as possible so that the &drm_device that corresponds to the AUX adapter
1753  * may be mentioned in debugging output from the DRM DP helpers.
1754  *
1755  * For devices which use a separate platform device for their AUX adapters, this
1756  * may be called as early as required by the driver.
1757  *
1758  */
drm_dp_aux_init(struct drm_dp_aux * aux)1759 void drm_dp_aux_init(struct drm_dp_aux *aux)
1760 {
1761 	mutex_init(&aux->hw_mutex);
1762 	mutex_init(&aux->cec.lock);
1763 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1764 
1765 	aux->ddc.algo = &drm_dp_i2c_algo;
1766 	aux->ddc.algo_data = aux;
1767 	aux->ddc.retries = 3;
1768 
1769 	aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1770 }
1771 EXPORT_SYMBOL(drm_dp_aux_init);
1772 
1773 /**
1774  * drm_dp_aux_register() - initialise and register aux channel
1775  * @aux: DisplayPort AUX channel
1776  *
1777  * Automatically calls drm_dp_aux_init() if this hasn't been done yet. This
1778  * should only be called once the parent of @aux, &drm_dp_aux.dev, is
1779  * initialized. For devices which are grandparents of their AUX channels,
1780  * &drm_dp_aux.dev will typically be the &drm_connector &device which
1781  * corresponds to @aux. For these devices, it's advised to call
1782  * drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to
1783  * call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.
1784  * Functions which don't follow this will likely Oops when
1785  * %CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1786  *
1787  * For devices where the AUX channel is a device that exists independently of
1788  * the &drm_device that uses it, such as SoCs and bridge devices, it is
1789  * recommended to call drm_dp_aux_register() after a &drm_device has been
1790  * assigned to &drm_dp_aux.drm_dev, and likewise to call
1791  * drm_dp_aux_unregister() once the &drm_device should no longer be associated
1792  * with the AUX channel (e.g. on bridge detach).
1793  *
1794  * Drivers which need to use the aux channel before either of the two points
1795  * mentioned above need to call drm_dp_aux_init() in order to use the AUX
1796  * channel before registration.
1797  *
1798  * Returns 0 on success or a negative error code on failure.
1799  */
drm_dp_aux_register(struct drm_dp_aux * aux)1800 int drm_dp_aux_register(struct drm_dp_aux *aux)
1801 {
1802 	int ret;
1803 
1804 	WARN_ON_ONCE(!aux->drm_dev);
1805 
1806 	if (!aux->ddc.algo)
1807 		drm_dp_aux_init(aux);
1808 
1809 	aux->ddc.class = I2C_CLASS_DDC;
1810 	aux->ddc.owner = THIS_MODULE;
1811 	aux->ddc.dev.parent = aux->dev;
1812 
1813 	strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1814 		sizeof(aux->ddc.name));
1815 
1816 	ret = drm_dp_aux_register_devnode(aux);
1817 	if (ret)
1818 		return ret;
1819 
1820 	ret = i2c_add_adapter(&aux->ddc);
1821 	if (ret) {
1822 		drm_dp_aux_unregister_devnode(aux);
1823 		return ret;
1824 	}
1825 
1826 	return 0;
1827 }
1828 EXPORT_SYMBOL(drm_dp_aux_register);
1829 
1830 /**
1831  * drm_dp_aux_unregister() - unregister an AUX adapter
1832  * @aux: DisplayPort AUX channel
1833  */
drm_dp_aux_unregister(struct drm_dp_aux * aux)1834 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1835 {
1836 	drm_dp_aux_unregister_devnode(aux);
1837 	i2c_del_adapter(&aux->ddc);
1838 }
1839 EXPORT_SYMBOL(drm_dp_aux_unregister);
1840 
1841 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1842 
1843 /**
1844  * drm_dp_psr_setup_time() - PSR setup in time usec
1845  * @psr_cap: PSR capabilities from DPCD
1846  *
1847  * Returns:
1848  * PSR setup time for the panel in microseconds,  negative
1849  * error code on failure.
1850  */
drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])1851 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1852 {
1853 	static const u16 psr_setup_time_us[] = {
1854 		PSR_SETUP_TIME(330),
1855 		PSR_SETUP_TIME(275),
1856 		PSR_SETUP_TIME(220),
1857 		PSR_SETUP_TIME(165),
1858 		PSR_SETUP_TIME(110),
1859 		PSR_SETUP_TIME(55),
1860 		PSR_SETUP_TIME(0),
1861 	};
1862 	int i;
1863 
1864 	i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1865 	if (i >= ARRAY_SIZE(psr_setup_time_us))
1866 		return -EINVAL;
1867 
1868 	return psr_setup_time_us[i];
1869 }
1870 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1871 
1872 #undef PSR_SETUP_TIME
1873 
1874 /**
1875  * drm_dp_start_crc() - start capture of frame CRCs
1876  * @aux: DisplayPort AUX channel
1877  * @crtc: CRTC displaying the frames whose CRCs are to be captured
1878  *
1879  * Returns 0 on success or a negative error code on failure.
1880  */
drm_dp_start_crc(struct drm_dp_aux * aux,struct drm_crtc * crtc)1881 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1882 {
1883 	u8 buf;
1884 	int ret;
1885 
1886 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1887 	if (ret < 0)
1888 		return ret;
1889 
1890 	ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1891 	if (ret < 0)
1892 		return ret;
1893 
1894 	aux->crc_count = 0;
1895 	aux->crtc = crtc;
1896 	schedule_work(&aux->crc_work);
1897 
1898 	return 0;
1899 }
1900 EXPORT_SYMBOL(drm_dp_start_crc);
1901 
1902 /**
1903  * drm_dp_stop_crc() - stop capture of frame CRCs
1904  * @aux: DisplayPort AUX channel
1905  *
1906  * Returns 0 on success or a negative error code on failure.
1907  */
drm_dp_stop_crc(struct drm_dp_aux * aux)1908 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1909 {
1910 	u8 buf;
1911 	int ret;
1912 
1913 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1914 	if (ret < 0)
1915 		return ret;
1916 
1917 	ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1918 	if (ret < 0)
1919 		return ret;
1920 
1921 	flush_work(&aux->crc_work);
1922 	aux->crtc = NULL;
1923 
1924 	return 0;
1925 }
1926 EXPORT_SYMBOL(drm_dp_stop_crc);
1927 
1928 struct dpcd_quirk {
1929 	u8 oui[3];
1930 	u8 device_id[6];
1931 	bool is_branch;
1932 	u32 quirks;
1933 };
1934 
1935 #define OUI(first, second, third) { (first), (second), (third) }
1936 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1937 	{ (first), (second), (third), (fourth), (fifth), (sixth) }
1938 
1939 #define DEVICE_ID_ANY	DEVICE_ID(0, 0, 0, 0, 0, 0)
1940 
1941 static const struct dpcd_quirk dpcd_quirk_list[] = {
1942 	/* Analogix 7737 needs reduced M and N at HBR2 link rates */
1943 	{ OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1944 	/* LG LP140WF6-SPM1 eDP panel */
1945 	{ OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1946 	/* Apple panels need some additional handling to support PSR */
1947 	{ OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1948 	/* CH7511 seems to leave SINK_COUNT zeroed */
1949 	{ OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1950 	/* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1951 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1952 	/* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1953 	{ 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) },
1954 };
1955 
1956 #undef OUI
1957 
1958 /*
1959  * Get a bit mask of DPCD quirks for the sink/branch device identified by
1960  * ident. The quirk data is shared but it's up to the drivers to act on the
1961  * data.
1962  *
1963  * For now, only the OUI (first three bytes) is used, but this may be extended
1964  * to device identification string and hardware/firmware revisions later.
1965  */
1966 static u32
drm_dp_get_quirks(const struct drm_dp_dpcd_ident * ident,bool is_branch)1967 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1968 {
1969 	const struct dpcd_quirk *quirk;
1970 	u32 quirks = 0;
1971 	int i;
1972 	u8 any_device[] = DEVICE_ID_ANY;
1973 
1974 	for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1975 		quirk = &dpcd_quirk_list[i];
1976 
1977 		if (quirk->is_branch != is_branch)
1978 			continue;
1979 
1980 		if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1981 			continue;
1982 
1983 		if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
1984 		    memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
1985 			continue;
1986 
1987 		quirks |= quirk->quirks;
1988 	}
1989 
1990 	return quirks;
1991 }
1992 
1993 #undef DEVICE_ID_ANY
1994 #undef DEVICE_ID
1995 
1996 /**
1997  * drm_dp_read_desc - read sink/branch descriptor from DPCD
1998  * @aux: DisplayPort AUX channel
1999  * @desc: Device descriptor to fill from DPCD
2000  * @is_branch: true for branch devices, false for sink devices
2001  *
2002  * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
2003  * identification.
2004  *
2005  * Returns 0 on success or a negative error code on failure.
2006  */
drm_dp_read_desc(struct drm_dp_aux * aux,struct drm_dp_desc * desc,bool is_branch)2007 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
2008 		     bool is_branch)
2009 {
2010 	struct drm_dp_dpcd_ident *ident = &desc->ident;
2011 	unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
2012 	int ret, dev_id_len;
2013 
2014 	ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
2015 	if (ret < 0)
2016 		return ret;
2017 
2018 	desc->quirks = drm_dp_get_quirks(ident, is_branch);
2019 
2020 	dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
2021 
2022 	drm_dbg_kms(aux->drm_dev,
2023 		    "%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
2024 		    aux->name, is_branch ? "branch" : "sink",
2025 		    (int)sizeof(ident->oui), ident->oui, dev_id_len,
2026 		    ident->device_id, ident->hw_rev >> 4, ident->hw_rev & 0xf,
2027 		    ident->sw_major_rev, ident->sw_minor_rev, desc->quirks);
2028 
2029 	return 0;
2030 }
2031 EXPORT_SYMBOL(drm_dp_read_desc);
2032 
2033 /**
2034  * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
2035  * supported by the DSC sink.
2036  * @dsc_dpcd: DSC capabilities from DPCD
2037  * @is_edp: true if its eDP, false for DP
2038  *
2039  * Read the slice capabilities DPCD register from DSC sink to get
2040  * the maximum slice count supported. This is used to populate
2041  * the DSC parameters in the &struct drm_dsc_config by the driver.
2042  * Driver creates an infoframe using these parameters to populate
2043  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2044  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2045  *
2046  * Returns:
2047  * Maximum slice count supported by DSC sink or 0 its invalid
2048  */
drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],bool is_edp)2049 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2050 				   bool is_edp)
2051 {
2052 	u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
2053 
2054 	if (is_edp) {
2055 		/* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
2056 		if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2057 			return 4;
2058 		if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2059 			return 2;
2060 		if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2061 			return 1;
2062 	} else {
2063 		/* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2064 		u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2065 
2066 		if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2067 			return 24;
2068 		if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2069 			return 20;
2070 		if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2071 			return 16;
2072 		if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2073 			return 12;
2074 		if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2075 			return 10;
2076 		if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2077 			return 8;
2078 		if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2079 			return 6;
2080 		if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2081 			return 4;
2082 		if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2083 			return 2;
2084 		if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2085 			return 1;
2086 	}
2087 
2088 	return 0;
2089 }
2090 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2091 
2092 /**
2093  * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2094  * @dsc_dpcd: DSC capabilities from DPCD
2095  *
2096  * Read the DSC DPCD register to parse the line buffer depth in bits which is
2097  * number of bits of precision within the decoder line buffer supported by
2098  * the DSC sink. This is used to populate the DSC parameters in the
2099  * &struct drm_dsc_config by the driver.
2100  * Driver creates an infoframe using these parameters to populate
2101  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2102  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2103  *
2104  * Returns:
2105  * Line buffer depth supported by DSC panel or 0 its invalid
2106  */
drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])2107 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2108 {
2109 	u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2110 
2111 	switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2112 	case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2113 		return 9;
2114 	case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2115 		return 10;
2116 	case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2117 		return 11;
2118 	case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2119 		return 12;
2120 	case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2121 		return 13;
2122 	case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2123 		return 14;
2124 	case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2125 		return 15;
2126 	case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2127 		return 16;
2128 	case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2129 		return 8;
2130 	}
2131 
2132 	return 0;
2133 }
2134 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2135 
2136 /**
2137  * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2138  * values supported by the DSC sink.
2139  * @dsc_dpcd: DSC capabilities from DPCD
2140  * @dsc_bpc: An array to be filled by this helper with supported
2141  *           input bpcs.
2142  *
2143  * Read the DSC DPCD from the sink device to parse the supported bits per
2144  * component values. This is used to populate the DSC parameters
2145  * in the &struct drm_dsc_config by the driver.
2146  * Driver creates an infoframe using these parameters to populate
2147  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2148  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2149  *
2150  * Returns:
2151  * Number of input BPC values parsed from the DPCD
2152  */
drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],u8 dsc_bpc[3])2153 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2154 					 u8 dsc_bpc[3])
2155 {
2156 	int num_bpc = 0;
2157 	u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2158 
2159 	if (color_depth & DP_DSC_12_BPC)
2160 		dsc_bpc[num_bpc++] = 12;
2161 	if (color_depth & DP_DSC_10_BPC)
2162 		dsc_bpc[num_bpc++] = 10;
2163 	if (color_depth & DP_DSC_8_BPC)
2164 		dsc_bpc[num_bpc++] = 8;
2165 
2166 	return num_bpc;
2167 }
2168 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2169 
2170 /**
2171  * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
2172  * @aux: DisplayPort AUX channel
2173  * @caps: buffer to return the capability info in
2174  *
2175  * Read capabilities common to all LTTPRs.
2176  *
2177  * Returns 0 on success or a negative error code on failure.
2178  */
drm_dp_read_lttpr_common_caps(struct drm_dp_aux * aux,u8 caps[DP_LTTPR_COMMON_CAP_SIZE])2179 int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
2180 				  u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2181 {
2182 	int ret;
2183 
2184 	ret = drm_dp_dpcd_read(aux,
2185 			       DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
2186 			       caps, DP_LTTPR_COMMON_CAP_SIZE);
2187 	if (ret < 0)
2188 		return ret;
2189 
2190 	WARN_ON(ret != DP_LTTPR_COMMON_CAP_SIZE);
2191 
2192 	return 0;
2193 }
2194 EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
2195 
2196 /**
2197  * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
2198  * @aux: DisplayPort AUX channel
2199  * @dp_phy: LTTPR PHY to read the capabilities for
2200  * @caps: buffer to return the capability info in
2201  *
2202  * Read the capabilities for the given LTTPR PHY.
2203  *
2204  * Returns 0 on success or a negative error code on failure.
2205  */
drm_dp_read_lttpr_phy_caps(struct drm_dp_aux * aux,enum drm_dp_phy dp_phy,u8 caps[DP_LTTPR_PHY_CAP_SIZE])2206 int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
2207 			       enum drm_dp_phy dp_phy,
2208 			       u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2209 {
2210 	int ret;
2211 
2212 	ret = drm_dp_dpcd_read(aux,
2213 			       DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
2214 			       caps, DP_LTTPR_PHY_CAP_SIZE);
2215 	if (ret < 0)
2216 		return ret;
2217 
2218 	WARN_ON(ret != DP_LTTPR_PHY_CAP_SIZE);
2219 
2220 	return 0;
2221 }
2222 EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
2223 
dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE],int r)2224 static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
2225 {
2226 	return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
2227 }
2228 
2229 /**
2230  * drm_dp_lttpr_count - get the number of detected LTTPRs
2231  * @caps: LTTPR common capabilities
2232  *
2233  * Get the number of detected LTTPRs from the LTTPR common capabilities info.
2234  *
2235  * Returns:
2236  *   -ERANGE if more than supported number (8) of LTTPRs are detected
2237  *   -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
2238  *   otherwise the number of detected LTTPRs
2239  */
drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])2240 int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2241 {
2242 	u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
2243 
2244 	switch (hweight8(count)) {
2245 	case 0:
2246 		return 0;
2247 	case 1:
2248 		return 8 - ilog2(count);
2249 	case 8:
2250 		return -ERANGE;
2251 	default:
2252 		return -EINVAL;
2253 	}
2254 }
2255 EXPORT_SYMBOL(drm_dp_lttpr_count);
2256 
2257 /**
2258  * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
2259  * @caps: LTTPR common capabilities
2260  *
2261  * Returns the maximum link rate supported by all detected LTTPRs.
2262  */
drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])2263 int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2264 {
2265 	u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
2266 
2267 	return drm_dp_bw_code_to_link_rate(rate);
2268 }
2269 EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
2270 
2271 /**
2272  * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
2273  * @caps: LTTPR common capabilities
2274  *
2275  * Returns the maximum lane count supported by all detected LTTPRs.
2276  */
drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])2277 int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2278 {
2279 	u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
2280 
2281 	return max_lanes & DP_MAX_LANE_COUNT_MASK;
2282 }
2283 EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
2284 
2285 /**
2286  * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
2287  * @caps: LTTPR PHY capabilities
2288  *
2289  * Returns true if the @caps for an LTTPR TX PHY indicate support for
2290  * voltage swing level 3.
2291  */
2292 bool
drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])2293 drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2294 {
2295 	u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2296 
2297 	return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
2298 }
2299 EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
2300 
2301 /**
2302  * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
2303  * @caps: LTTPR PHY capabilities
2304  *
2305  * Returns true if the @caps for an LTTPR TX PHY indicate support for
2306  * pre-emphasis level 3.
2307  */
2308 bool
drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])2309 drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2310 {
2311 	u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2312 
2313 	return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
2314 }
2315 EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
2316 
2317 /**
2318  * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2319  * @aux: DisplayPort AUX channel
2320  * @data: DP phy compliance test parameters.
2321  *
2322  * Returns 0 on success or a negative error code on failure.
2323  */
drm_dp_get_phy_test_pattern(struct drm_dp_aux * aux,struct drm_dp_phy_test_params * data)2324 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2325 				struct drm_dp_phy_test_params *data)
2326 {
2327 	int err;
2328 	u8 rate, lanes;
2329 
2330 	err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2331 	if (err < 0)
2332 		return err;
2333 	data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2334 
2335 	err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2336 	if (err < 0)
2337 		return err;
2338 	data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2339 
2340 	if (lanes & DP_ENHANCED_FRAME_CAP)
2341 		data->enhanced_frame_cap = true;
2342 
2343 	err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2344 	if (err < 0)
2345 		return err;
2346 
2347 	switch (data->phy_pattern) {
2348 	case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2349 		err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2350 				       &data->custom80, sizeof(data->custom80));
2351 		if (err < 0)
2352 			return err;
2353 
2354 		break;
2355 	case DP_PHY_TEST_PATTERN_CP2520:
2356 		err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2357 				       &data->hbr2_reset,
2358 				       sizeof(data->hbr2_reset));
2359 		if (err < 0)
2360 			return err;
2361 	}
2362 
2363 	return 0;
2364 }
2365 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2366 
2367 /**
2368  * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2369  * @aux: DisplayPort AUX channel
2370  * @data: DP phy compliance test parameters.
2371  * @dp_rev: DP revision to use for compliance testing
2372  *
2373  * Returns 0 on success or a negative error code on failure.
2374  */
drm_dp_set_phy_test_pattern(struct drm_dp_aux * aux,struct drm_dp_phy_test_params * data,u8 dp_rev)2375 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2376 				struct drm_dp_phy_test_params *data, u8 dp_rev)
2377 {
2378 	int err, i;
2379 	u8 test_pattern;
2380 
2381 	test_pattern = data->phy_pattern;
2382 	if (dp_rev < 0x12) {
2383 		test_pattern = (test_pattern << 2) &
2384 			       DP_LINK_QUAL_PATTERN_11_MASK;
2385 		err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2386 					 test_pattern);
2387 		if (err < 0)
2388 			return err;
2389 	} else {
2390 		for (i = 0; i < data->num_lanes; i++) {
2391 			err = drm_dp_dpcd_writeb(aux,
2392 						 DP_LINK_QUAL_LANE0_SET + i,
2393 						 test_pattern);
2394 			if (err < 0)
2395 				return err;
2396 		}
2397 	}
2398 
2399 	return 0;
2400 }
2401 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2402 
dp_pixelformat_get_name(enum dp_pixelformat pixelformat)2403 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2404 {
2405 	if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2406 		return "Invalid";
2407 
2408 	switch (pixelformat) {
2409 	case DP_PIXELFORMAT_RGB:
2410 		return "RGB";
2411 	case DP_PIXELFORMAT_YUV444:
2412 		return "YUV444";
2413 	case DP_PIXELFORMAT_YUV422:
2414 		return "YUV422";
2415 	case DP_PIXELFORMAT_YUV420:
2416 		return "YUV420";
2417 	case DP_PIXELFORMAT_Y_ONLY:
2418 		return "Y_ONLY";
2419 	case DP_PIXELFORMAT_RAW:
2420 		return "RAW";
2421 	default:
2422 		return "Reserved";
2423 	}
2424 }
2425 
dp_colorimetry_get_name(enum dp_pixelformat pixelformat,enum dp_colorimetry colorimetry)2426 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2427 					   enum dp_colorimetry colorimetry)
2428 {
2429 	if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2430 		return "Invalid";
2431 
2432 	switch (colorimetry) {
2433 	case DP_COLORIMETRY_DEFAULT:
2434 		switch (pixelformat) {
2435 		case DP_PIXELFORMAT_RGB:
2436 			return "sRGB";
2437 		case DP_PIXELFORMAT_YUV444:
2438 		case DP_PIXELFORMAT_YUV422:
2439 		case DP_PIXELFORMAT_YUV420:
2440 			return "BT.601";
2441 		case DP_PIXELFORMAT_Y_ONLY:
2442 			return "DICOM PS3.14";
2443 		case DP_PIXELFORMAT_RAW:
2444 			return "Custom Color Profile";
2445 		default:
2446 			return "Reserved";
2447 		}
2448 	case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2449 		switch (pixelformat) {
2450 		case DP_PIXELFORMAT_RGB:
2451 			return "Wide Fixed";
2452 		case DP_PIXELFORMAT_YUV444:
2453 		case DP_PIXELFORMAT_YUV422:
2454 		case DP_PIXELFORMAT_YUV420:
2455 			return "BT.709";
2456 		default:
2457 			return "Reserved";
2458 		}
2459 	case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2460 		switch (pixelformat) {
2461 		case DP_PIXELFORMAT_RGB:
2462 			return "Wide Float";
2463 		case DP_PIXELFORMAT_YUV444:
2464 		case DP_PIXELFORMAT_YUV422:
2465 		case DP_PIXELFORMAT_YUV420:
2466 			return "xvYCC 601";
2467 		default:
2468 			return "Reserved";
2469 		}
2470 	case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2471 		switch (pixelformat) {
2472 		case DP_PIXELFORMAT_RGB:
2473 			return "OpRGB";
2474 		case DP_PIXELFORMAT_YUV444:
2475 		case DP_PIXELFORMAT_YUV422:
2476 		case DP_PIXELFORMAT_YUV420:
2477 			return "xvYCC 709";
2478 		default:
2479 			return "Reserved";
2480 		}
2481 	case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2482 		switch (pixelformat) {
2483 		case DP_PIXELFORMAT_RGB:
2484 			return "DCI-P3";
2485 		case DP_PIXELFORMAT_YUV444:
2486 		case DP_PIXELFORMAT_YUV422:
2487 		case DP_PIXELFORMAT_YUV420:
2488 			return "sYCC 601";
2489 		default:
2490 			return "Reserved";
2491 		}
2492 	case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2493 		switch (pixelformat) {
2494 		case DP_PIXELFORMAT_RGB:
2495 			return "Custom Profile";
2496 		case DP_PIXELFORMAT_YUV444:
2497 		case DP_PIXELFORMAT_YUV422:
2498 		case DP_PIXELFORMAT_YUV420:
2499 			return "OpYCC 601";
2500 		default:
2501 			return "Reserved";
2502 		}
2503 	case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2504 		switch (pixelformat) {
2505 		case DP_PIXELFORMAT_RGB:
2506 			return "BT.2020 RGB";
2507 		case DP_PIXELFORMAT_YUV444:
2508 		case DP_PIXELFORMAT_YUV422:
2509 		case DP_PIXELFORMAT_YUV420:
2510 			return "BT.2020 CYCC";
2511 		default:
2512 			return "Reserved";
2513 		}
2514 	case DP_COLORIMETRY_BT2020_YCC:
2515 		switch (pixelformat) {
2516 		case DP_PIXELFORMAT_YUV444:
2517 		case DP_PIXELFORMAT_YUV422:
2518 		case DP_PIXELFORMAT_YUV420:
2519 			return "BT.2020 YCC";
2520 		default:
2521 			return "Reserved";
2522 		}
2523 	default:
2524 		return "Invalid";
2525 	}
2526 }
2527 
dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)2528 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2529 {
2530 	switch (dynamic_range) {
2531 	case DP_DYNAMIC_RANGE_VESA:
2532 		return "VESA range";
2533 	case DP_DYNAMIC_RANGE_CTA:
2534 		return "CTA range";
2535 	default:
2536 		return "Invalid";
2537 	}
2538 }
2539 
dp_content_type_get_name(enum dp_content_type content_type)2540 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2541 {
2542 	switch (content_type) {
2543 	case DP_CONTENT_TYPE_NOT_DEFINED:
2544 		return "Not defined";
2545 	case DP_CONTENT_TYPE_GRAPHICS:
2546 		return "Graphics";
2547 	case DP_CONTENT_TYPE_PHOTO:
2548 		return "Photo";
2549 	case DP_CONTENT_TYPE_VIDEO:
2550 		return "Video";
2551 	case DP_CONTENT_TYPE_GAME:
2552 		return "Game";
2553 	default:
2554 		return "Reserved";
2555 	}
2556 }
2557 
drm_dp_vsc_sdp_log(const char * level,struct device * dev,const struct drm_dp_vsc_sdp * vsc)2558 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2559 			const struct drm_dp_vsc_sdp *vsc)
2560 {
2561 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2562 	DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2563 		   vsc->revision, vsc->length);
2564 	DP_SDP_LOG("    pixelformat: %s\n",
2565 		   dp_pixelformat_get_name(vsc->pixelformat));
2566 	DP_SDP_LOG("    colorimetry: %s\n",
2567 		   dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2568 	DP_SDP_LOG("    bpc: %u\n", vsc->bpc);
2569 	DP_SDP_LOG("    dynamic range: %s\n",
2570 		   dp_dynamic_range_get_name(vsc->dynamic_range));
2571 	DP_SDP_LOG("    content type: %s\n",
2572 		   dp_content_type_get_name(vsc->content_type));
2573 #undef DP_SDP_LOG
2574 }
2575 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
2576 
2577 /**
2578  * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
2579  * @dpcd: DisplayPort configuration data
2580  * @port_cap: port capabilities
2581  *
2582  * Returns maximum frl bandwidth supported by PCON in GBPS,
2583  * returns 0 if not supported.
2584  */
drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])2585 int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
2586 			       const u8 port_cap[4])
2587 {
2588 	int bw;
2589 	u8 buf;
2590 
2591 	buf = port_cap[2];
2592 	bw = buf & DP_PCON_MAX_FRL_BW;
2593 
2594 	switch (bw) {
2595 	case DP_PCON_MAX_9GBPS:
2596 		return 9;
2597 	case DP_PCON_MAX_18GBPS:
2598 		return 18;
2599 	case DP_PCON_MAX_24GBPS:
2600 		return 24;
2601 	case DP_PCON_MAX_32GBPS:
2602 		return 32;
2603 	case DP_PCON_MAX_40GBPS:
2604 		return 40;
2605 	case DP_PCON_MAX_48GBPS:
2606 		return 48;
2607 	case DP_PCON_MAX_0GBPS:
2608 	default:
2609 		return 0;
2610 	}
2611 
2612 	return 0;
2613 }
2614 EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
2615 
2616 /**
2617  * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
2618  * @aux: DisplayPort AUX channel
2619  * @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.
2620  *
2621  * Returns 0 if success, else returns negative error code.
2622  */
drm_dp_pcon_frl_prepare(struct drm_dp_aux * aux,bool enable_frl_ready_hpd)2623 int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
2624 {
2625 	int ret;
2626 	u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
2627 		 DP_PCON_ENABLE_LINK_FRL_MODE;
2628 
2629 	if (enable_frl_ready_hpd)
2630 		buf |= DP_PCON_ENABLE_HPD_READY;
2631 
2632 	ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2633 
2634 	return ret;
2635 }
2636 EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
2637 
2638 /**
2639  * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
2640  * @aux: DisplayPort AUX channel
2641  *
2642  * Returns true if success, else returns false.
2643  */
drm_dp_pcon_is_frl_ready(struct drm_dp_aux * aux)2644 bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
2645 {
2646 	int ret;
2647 	u8 buf;
2648 
2649 	ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2650 	if (ret < 0)
2651 		return false;
2652 
2653 	if (buf & DP_PCON_FRL_READY)
2654 		return true;
2655 
2656 	return false;
2657 }
2658 EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
2659 
2660 /**
2661  * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
2662  * @aux: DisplayPort AUX channel
2663  * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
2664  * @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.
2665  * In Concurrent Mode, the FRL link bring up can be done along with
2666  * DP Link training. In Sequential mode, the FRL link bring up is done prior to
2667  * the DP Link training.
2668  *
2669  * Returns 0 if success, else returns negative error code.
2670  */
2671 
drm_dp_pcon_frl_configure_1(struct drm_dp_aux * aux,int max_frl_gbps,u8 frl_mode)2672 int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
2673 				u8 frl_mode)
2674 {
2675 	int ret;
2676 	u8 buf;
2677 
2678 	ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2679 	if (ret < 0)
2680 		return ret;
2681 
2682 	if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)
2683 		buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
2684 	else
2685 		buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
2686 
2687 	switch (max_frl_gbps) {
2688 	case 9:
2689 		buf |=  DP_PCON_ENABLE_MAX_BW_9GBPS;
2690 		break;
2691 	case 18:
2692 		buf |=  DP_PCON_ENABLE_MAX_BW_18GBPS;
2693 		break;
2694 	case 24:
2695 		buf |=  DP_PCON_ENABLE_MAX_BW_24GBPS;
2696 		break;
2697 	case 32:
2698 		buf |=  DP_PCON_ENABLE_MAX_BW_32GBPS;
2699 		break;
2700 	case 40:
2701 		buf |=  DP_PCON_ENABLE_MAX_BW_40GBPS;
2702 		break;
2703 	case 48:
2704 		buf |=  DP_PCON_ENABLE_MAX_BW_48GBPS;
2705 		break;
2706 	case 0:
2707 		buf |=  DP_PCON_ENABLE_MAX_BW_0GBPS;
2708 		break;
2709 	default:
2710 		return -EINVAL;
2711 	}
2712 
2713 	ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2714 	if (ret < 0)
2715 		return ret;
2716 
2717 	return 0;
2718 }
2719 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
2720 
2721 /**
2722  * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
2723  * @aux: DisplayPort AUX channel
2724  * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
2725  * @frl_type : FRL training type, can be Extended, or Normal.
2726  * In Normal FRL training, the PCON tries each frl bw from the max_frl_mask
2727  * starting from min, and stops when link training is successful. In Extended
2728  * FRL training, all frl bw selected in the mask are trained by the PCON.
2729  *
2730  * Returns 0 if success, else returns negative error code.
2731  */
drm_dp_pcon_frl_configure_2(struct drm_dp_aux * aux,int max_frl_mask,u8 frl_type)2732 int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
2733 				u8 frl_type)
2734 {
2735 	int ret;
2736 	u8 buf = max_frl_mask;
2737 
2738 	if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)
2739 		buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2740 	else
2741 		buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2742 
2743 	ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
2744 	if (ret < 0)
2745 		return ret;
2746 
2747 	return 0;
2748 }
2749 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
2750 
2751 /**
2752  * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
2753  * @aux: DisplayPort AUX channel
2754  *
2755  * Returns 0 if success, else returns negative error code.
2756  */
drm_dp_pcon_reset_frl_config(struct drm_dp_aux * aux)2757 int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
2758 {
2759 	int ret;
2760 
2761 	ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
2762 	if (ret < 0)
2763 		return ret;
2764 
2765 	return 0;
2766 }
2767 EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
2768 
2769 /**
2770  * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
2771  * @aux: DisplayPort AUX channel
2772  *
2773  * Returns 0 if success, else returns negative error code.
2774  */
drm_dp_pcon_frl_enable(struct drm_dp_aux * aux)2775 int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
2776 {
2777 	int ret;
2778 	u8 buf = 0;
2779 
2780 	ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2781 	if (ret < 0)
2782 		return ret;
2783 	if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
2784 		drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",
2785 			    aux->name);
2786 		return -EINVAL;
2787 	}
2788 	buf |= DP_PCON_ENABLE_HDMI_LINK;
2789 	ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2790 	if (ret < 0)
2791 		return ret;
2792 
2793 	return 0;
2794 }
2795 EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
2796 
2797 /**
2798  * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
2799  * @aux: DisplayPort AUX channel
2800  *
2801  * Returns true if link is active else returns false.
2802  */
drm_dp_pcon_hdmi_link_active(struct drm_dp_aux * aux)2803 bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
2804 {
2805 	u8 buf;
2806 	int ret;
2807 
2808 	ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2809 	if (ret < 0)
2810 		return false;
2811 
2812 	return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
2813 }
2814 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
2815 
2816 /**
2817  * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
2818  * @aux: DisplayPort AUX channel
2819  * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
2820  * Valid only if the MODE returned is FRL. For Normal Link training mode
2821  * only 1 of the bits will be set, but in case of Extended mode, more than
2822  * one bits can be set.
2823  *
2824  * Returns the link mode : TMDS or FRL on success, else returns negative error
2825  * code.
2826  */
drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux * aux,u8 * frl_trained_mask)2827 int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
2828 {
2829 	u8 buf;
2830 	int mode;
2831 	int ret;
2832 
2833 	ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
2834 	if (ret < 0)
2835 		return ret;
2836 
2837 	mode = buf & DP_PCON_HDMI_LINK_MODE;
2838 
2839 	if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
2840 		*frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
2841 
2842 	return mode;
2843 }
2844 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
2845 
2846 /**
2847  * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
2848  * during link failure between PCON and HDMI sink
2849  * @aux: DisplayPort AUX channel
2850  * @connector: DRM connector
2851  * code.
2852  **/
2853 
drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux * aux,struct drm_connector * connector)2854 void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
2855 					   struct drm_connector *connector)
2856 {
2857 	u8 buf, error_count;
2858 	int i, num_error;
2859 	struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
2860 
2861 	for (i = 0; i < hdmi->max_lanes; i++) {
2862 		if (drm_dp_dpcd_readb(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
2863 			return;
2864 
2865 		error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
2866 		switch (error_count) {
2867 		case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
2868 			num_error = 100;
2869 			break;
2870 		case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
2871 			num_error = 10;
2872 			break;
2873 		case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
2874 			num_error = 3;
2875 			break;
2876 		default:
2877 			num_error = 0;
2878 		}
2879 
2880 		drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",
2881 			aux->name, num_error, i);
2882 	}
2883 }
2884 EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
2885 
2886 /*
2887  * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
2888  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2889  *
2890  * Returns true is PCON encoder is DSC 1.2 else returns false.
2891  */
drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])2892 bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2893 {
2894 	u8 buf;
2895 	u8 major_v, minor_v;
2896 
2897 	buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
2898 	major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
2899 	minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
2900 
2901 	if (major_v == 1 && minor_v == 2)
2902 		return true;
2903 
2904 	return false;
2905 }
2906 EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
2907 
2908 /*
2909  * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
2910  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2911  *
2912  * Returns maximum no. of slices supported by the PCON DSC Encoder.
2913  */
drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])2914 int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2915 {
2916 	u8 slice_cap1, slice_cap2;
2917 
2918 	slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
2919 	slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
2920 
2921 	if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
2922 		return 24;
2923 	if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
2924 		return 20;
2925 	if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
2926 		return 16;
2927 	if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
2928 		return 12;
2929 	if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
2930 		return 10;
2931 	if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
2932 		return 8;
2933 	if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
2934 		return 6;
2935 	if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
2936 		return 4;
2937 	if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
2938 		return 2;
2939 	if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
2940 		return 1;
2941 
2942 	return 0;
2943 }
2944 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
2945 
2946 /*
2947  * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
2948  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2949  *
2950  * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
2951  */
drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])2952 int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2953 {
2954 	u8 buf;
2955 
2956 	buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
2957 
2958 	return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
2959 }
2960 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
2961 
2962 /*
2963  * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
2964  * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2965  *
2966  * Returns the bpp precision supported by the PCON encoder.
2967  */
drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])2968 int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2969 {
2970 	u8 buf;
2971 
2972 	buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
2973 
2974 	switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
2975 	case DP_PCON_DSC_ONE_16TH_BPP:
2976 		return 16;
2977 	case DP_PCON_DSC_ONE_8TH_BPP:
2978 		return 8;
2979 	case DP_PCON_DSC_ONE_4TH_BPP:
2980 		return 4;
2981 	case DP_PCON_DSC_ONE_HALF_BPP:
2982 		return 2;
2983 	case DP_PCON_DSC_ONE_BPP:
2984 		return 1;
2985 	}
2986 
2987 	return 0;
2988 }
2989 EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
2990 
2991 static
drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux * aux,u8 pps_buf_config)2992 int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
2993 {
2994 	u8 buf;
2995 	int ret;
2996 
2997 	ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
2998 	if (ret < 0)
2999 		return ret;
3000 
3001 	buf |= DP_PCON_ENABLE_DSC_ENCODER;
3002 
3003 	if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
3004 		buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
3005 		buf |= pps_buf_config << 2;
3006 	}
3007 
3008 	ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3009 	if (ret < 0)
3010 		return ret;
3011 
3012 	return 0;
3013 }
3014 
3015 /**
3016  * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
3017  * for DSC1.2 between PCON & HDMI2.1 sink
3018  * @aux: DisplayPort AUX channel
3019  *
3020  * Returns 0 on success, else returns negative error code.
3021  */
drm_dp_pcon_pps_default(struct drm_dp_aux * aux)3022 int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
3023 {
3024 	int ret;
3025 
3026 	ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
3027 	if (ret < 0)
3028 		return ret;
3029 
3030 	return 0;
3031 }
3032 EXPORT_SYMBOL(drm_dp_pcon_pps_default);
3033 
3034 /**
3035  * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
3036  * HDMI sink
3037  * @aux: DisplayPort AUX channel
3038  * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
3039  *
3040  * Returns 0 on success, else returns negative error code.
3041  */
drm_dp_pcon_pps_override_buf(struct drm_dp_aux * aux,u8 pps_buf[128])3042 int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
3043 {
3044 	int ret;
3045 
3046 	ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
3047 	if (ret < 0)
3048 		return ret;
3049 
3050 	ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3051 	if (ret < 0)
3052 		return ret;
3053 
3054 	return 0;
3055 }
3056 EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
3057 
3058 /*
3059  * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
3060  * override registers
3061  * @aux: DisplayPort AUX channel
3062  * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
3063  * bits_per_pixel.
3064  *
3065  * Returns 0 on success, else returns negative error code.
3066  */
drm_dp_pcon_pps_override_param(struct drm_dp_aux * aux,u8 pps_param[6])3067 int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
3068 {
3069 	int ret;
3070 
3071 	ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
3072 	if (ret < 0)
3073 		return ret;
3074 	ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
3075 	if (ret < 0)
3076 		return ret;
3077 	ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
3078 	if (ret < 0)
3079 		return ret;
3080 
3081 	ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3082 	if (ret < 0)
3083 		return ret;
3084 
3085 	return 0;
3086 }
3087 EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
3088 
3089 /*
3090  * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
3091  * @aux: displayPort AUX channel
3092  * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
3093  *
3094  * Returns 0 on success, else returns negative error code.
3095  */
drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux * aux,u8 color_spc)3096 int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
3097 {
3098 	int ret;
3099 	u8 buf;
3100 
3101 	ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3102 	if (ret < 0)
3103 		return ret;
3104 
3105 	if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
3106 		buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
3107 	else
3108 		buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
3109 
3110 	ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3111 	if (ret < 0)
3112 		return ret;
3113 
3114 	return 0;
3115 }
3116 EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);
3117 
3118 /**
3119  * drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX
3120  * @aux: The DP AUX channel to use
3121  * @bl: Backlight capability info from drm_edp_backlight_init()
3122  * @level: The brightness level to set
3123  *
3124  * Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must
3125  * already have been enabled by the driver by calling drm_edp_backlight_enable().
3126  *
3127  * Returns: %0 on success, negative error code on failure
3128  */
drm_edp_backlight_set_level(struct drm_dp_aux * aux,const struct drm_edp_backlight_info * bl,u16 level)3129 int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3130 				u16 level)
3131 {
3132 	int ret;
3133 	u8 buf[2] = { 0 };
3134 
3135 	if (bl->lsb_reg_used) {
3136 		buf[0] = (level & 0xff00) >> 8;
3137 		buf[1] = (level & 0x00ff);
3138 	} else {
3139 		buf[0] = level;
3140 	}
3141 
3142 	ret = drm_dp_dpcd_write(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, sizeof(buf));
3143 	if (ret != sizeof(buf)) {
3144 		drm_err(aux->drm_dev,
3145 			"%s: Failed to write aux backlight level: %d\n",
3146 			aux->name, ret);
3147 		return ret < 0 ? ret : -EIO;
3148 	}
3149 
3150 	return 0;
3151 }
3152 EXPORT_SYMBOL(drm_edp_backlight_set_level);
3153 
3154 static int
drm_edp_backlight_set_enable(struct drm_dp_aux * aux,const struct drm_edp_backlight_info * bl,bool enable)3155 drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3156 			     bool enable)
3157 {
3158 	int ret;
3159 	u8 buf;
3160 
3161 	/* The panel uses something other then DPCD for enabling its backlight */
3162 	if (!bl->aux_enable)
3163 		return 0;
3164 
3165 	ret = drm_dp_dpcd_readb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);
3166 	if (ret != 1) {
3167 		drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",
3168 			aux->name, ret);
3169 		return ret < 0 ? ret : -EIO;
3170 	}
3171 	if (enable)
3172 		buf |= DP_EDP_BACKLIGHT_ENABLE;
3173 	else
3174 		buf &= ~DP_EDP_BACKLIGHT_ENABLE;
3175 
3176 	ret = drm_dp_dpcd_writeb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);
3177 	if (ret != 1) {
3178 		drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",
3179 			aux->name, ret);
3180 		return ret < 0 ? ret : -EIO;
3181 	}
3182 
3183 	return 0;
3184 }
3185 
3186 /**
3187  * drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD
3188  * @aux: The DP AUX channel to use
3189  * @bl: Backlight capability info from drm_edp_backlight_init()
3190  * @level: The initial backlight level to set via AUX, if there is one
3191  *
3192  * This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally
3193  * restoring any important backlight state such as the given backlight level, the brightness byte
3194  * count, backlight frequency, etc.
3195  *
3196  * Note that certain panels, while supporting brightness level controls over DPCD, may not support
3197  * having their backlights enabled via the standard %DP_EDP_DISPLAY_CONTROL_REGISTER. On such panels
3198  * &drm_edp_backlight_info.aux_enable will be set to %false, this function will skip the step of
3199  * programming the %DP_EDP_DISPLAY_CONTROL_REGISTER, and the driver must perform the required
3200  * implementation specific step for enabling the backlight after calling this function.
3201  *
3202  * Returns: %0 on success, negative error code on failure.
3203  */
drm_edp_backlight_enable(struct drm_dp_aux * aux,const struct drm_edp_backlight_info * bl,const u16 level)3204 int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3205 			     const u16 level)
3206 {
3207 	int ret;
3208 	u8 dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
3209 
3210 	if (bl->pwmgen_bit_count) {
3211 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);
3212 		if (ret != 1)
3213 			drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
3214 				    aux->name, ret);
3215 	}
3216 
3217 	if (bl->pwm_freq_pre_divider) {
3218 		ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_FREQ_SET, bl->pwm_freq_pre_divider);
3219 		if (ret != 1)
3220 			drm_dbg_kms(aux->drm_dev,
3221 				    "%s: Failed to write aux backlight frequency: %d\n",
3222 				    aux->name, ret);
3223 		else
3224 			dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;
3225 	}
3226 
3227 	ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf);
3228 	if (ret != 1) {
3229 		drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",
3230 			    aux->name, ret);
3231 		return ret < 0 ? ret : -EIO;
3232 	}
3233 
3234 	ret = drm_edp_backlight_set_level(aux, bl, level);
3235 	if (ret < 0)
3236 		return ret;
3237 	ret = drm_edp_backlight_set_enable(aux, bl, true);
3238 	if (ret < 0)
3239 		return ret;
3240 
3241 	return 0;
3242 }
3243 EXPORT_SYMBOL(drm_edp_backlight_enable);
3244 
3245 /**
3246  * drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported
3247  * @aux: The DP AUX channel to use
3248  * @bl: Backlight capability info from drm_edp_backlight_init()
3249  *
3250  * This function handles disabling DPCD backlight controls on a panel over AUX. Note that some
3251  * panels have backlights that are enabled/disabled by other means, despite having their brightness
3252  * values controlled through DPCD. On such panels &drm_edp_backlight_info.aux_enable will be set to
3253  * %false, this function will become a no-op (and we will skip updating
3254  * %DP_EDP_DISPLAY_CONTROL_REGISTER), and the driver must take care to perform it's own
3255  * implementation specific step for disabling the backlight.
3256  *
3257  * Returns: %0 on success or no-op, negative error code on failure.
3258  */
drm_edp_backlight_disable(struct drm_dp_aux * aux,const struct drm_edp_backlight_info * bl)3259 int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)
3260 {
3261 	int ret;
3262 
3263 	ret = drm_edp_backlight_set_enable(aux, bl, false);
3264 	if (ret < 0)
3265 		return ret;
3266 
3267 	return 0;
3268 }
3269 EXPORT_SYMBOL(drm_edp_backlight_disable);
3270 
3271 static inline int
drm_edp_backlight_probe_max(struct drm_dp_aux * aux,struct drm_edp_backlight_info * bl,u16 driver_pwm_freq_hz,const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])3272 drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3273 			    u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])
3274 {
3275 	int fxp, fxp_min, fxp_max, fxp_actual, f = 1;
3276 	int ret;
3277 	u8 pn, pn_min, pn_max;
3278 
3279 	ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT, &pn);
3280 	if (ret != 1) {
3281 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",
3282 			    aux->name, ret);
3283 		return -ENODEV;
3284 	}
3285 
3286 	pn &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3287 	bl->max = (1 << pn) - 1;
3288 	if (!driver_pwm_freq_hz)
3289 		return 0;
3290 
3291 	/*
3292 	 * Set PWM Frequency divider to match desired frequency provided by the driver.
3293 	 * The PWM Frequency is calculated as 27Mhz / (F x P).
3294 	 * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the
3295 	 *             EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)
3296 	 * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the
3297 	 *             EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)
3298 	 */
3299 
3300 	/* Find desired value of (F x P)
3301 	 * Note that, if F x P is out of supported range, the maximum value or minimum value will
3302 	 * applied automatically. So no need to check that.
3303 	 */
3304 	fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);
3305 
3306 	/* Use highest possible value of Pn for more granularity of brightness adjustment while
3307 	 * satisfying the conditions below.
3308 	 * - Pn is in the range of Pn_min and Pn_max
3309 	 * - F is in the range of 1 and 255
3310 	 * - FxP is within 25% of desired value.
3311 	 *   Note: 25% is arbitrary value and may need some tweak.
3312 	 */
3313 	ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);
3314 	if (ret != 1) {
3315 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",
3316 			    aux->name, ret);
3317 		return 0;
3318 	}
3319 	ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);
3320 	if (ret != 1) {
3321 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",
3322 			    aux->name, ret);
3323 		return 0;
3324 	}
3325 	pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3326 	pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3327 
3328 	/* Ensure frequency is within 25% of desired value */
3329 	fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);
3330 	fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);
3331 	if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {
3332 		drm_dbg_kms(aux->drm_dev,
3333 			    "%s: Driver defined backlight frequency (%d) out of range\n",
3334 			    aux->name, driver_pwm_freq_hz);
3335 		return 0;
3336 	}
3337 
3338 	for (pn = pn_max; pn >= pn_min; pn--) {
3339 		f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);
3340 		fxp_actual = f << pn;
3341 		if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)
3342 			break;
3343 	}
3344 
3345 	ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);
3346 	if (ret != 1) {
3347 		drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
3348 			    aux->name, ret);
3349 		return 0;
3350 	}
3351 	bl->pwmgen_bit_count = pn;
3352 	bl->max = (1 << pn) - 1;
3353 
3354 	if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {
3355 		bl->pwm_freq_pre_divider = f;
3356 		drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",
3357 			    aux->name, driver_pwm_freq_hz);
3358 	}
3359 
3360 	return 0;
3361 }
3362 
3363 static inline int
drm_edp_backlight_probe_level(struct drm_dp_aux * aux,struct drm_edp_backlight_info * bl,u8 * current_mode)3364 drm_edp_backlight_probe_level(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3365 			      u8 *current_mode)
3366 {
3367 	int ret;
3368 	u8 buf[2];
3369 	u8 mode_reg;
3370 
3371 	ret = drm_dp_dpcd_readb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);
3372 	if (ret != 1) {
3373 		drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",
3374 			    aux->name, ret);
3375 		return ret < 0 ? ret : -EIO;
3376 	}
3377 
3378 	*current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);
3379 	if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
3380 		int size = 1 + bl->lsb_reg_used;
3381 
3382 		ret = drm_dp_dpcd_read(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, size);
3383 		if (ret != size) {
3384 			drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight level: %d\n",
3385 				    aux->name, ret);
3386 			return ret < 0 ? ret : -EIO;
3387 		}
3388 
3389 		if (bl->lsb_reg_used)
3390 			return (buf[0] << 8) | buf[1];
3391 		else
3392 			return buf[0];
3393 	}
3394 
3395 	/*
3396 	 * If we're not in DPCD control mode yet, the programmed brightness value is meaningless and
3397 	 * the driver should assume max brightness
3398 	 */
3399 	return bl->max;
3400 }
3401 
3402 /**
3403  * drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight
3404  * interface.
3405  * @aux: The DP aux device to use for probing
3406  * @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight
3407  * @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz
3408  * @edp_dpcd: A cached copy of the eDP DPCD
3409  * @current_level: Where to store the probed brightness level
3410  * @current_mode: Where to store the currently set backlight control mode
3411  *
3412  * Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,
3413  * along with also probing the current and maximum supported brightness levels.
3414  *
3415  * If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the
3416  * default frequency from the panel is used.
3417  *
3418  * Returns: %0 on success, negative error code on failure.
3419  */
3420 int
drm_edp_backlight_init(struct drm_dp_aux * aux,struct drm_edp_backlight_info * bl,u16 driver_pwm_freq_hz,const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],u16 * current_level,u8 * current_mode)3421 drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3422 		       u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],
3423 		       u16 *current_level, u8 *current_mode)
3424 {
3425 	int ret;
3426 
3427 	if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)
3428 		bl->aux_enable = true;
3429 	if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
3430 		bl->lsb_reg_used = true;
3431 
3432 	ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);
3433 	if (ret < 0)
3434 		return ret;
3435 
3436 	ret = drm_edp_backlight_probe_level(aux, bl, current_mode);
3437 	if (ret < 0)
3438 		return ret;
3439 	*current_level = ret;
3440 
3441 	drm_dbg_kms(aux->drm_dev,
3442 		    "%s: Found backlight level=%d/%d pwm_freq_pre_divider=%d mode=%x\n",
3443 		    aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider, *current_mode);
3444 	drm_dbg_kms(aux->drm_dev,
3445 		    "%s: Backlight caps: pwmgen_bit_count=%d lsb_reg_used=%d aux_enable=%d\n",
3446 		    aux->name, bl->pwmgen_bit_count, bl->lsb_reg_used, bl->aux_enable);
3447 	return 0;
3448 }
3449 EXPORT_SYMBOL(drm_edp_backlight_init);
3450 
3451 #if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
3452 	(IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))
3453 
dp_aux_backlight_update_status(struct backlight_device * bd)3454 static int dp_aux_backlight_update_status(struct backlight_device *bd)
3455 {
3456 	struct dp_aux_backlight *bl = bl_get_data(bd);
3457 	u16 brightness = backlight_get_brightness(bd);
3458 	int ret = 0;
3459 
3460 	if (!backlight_is_blank(bd)) {
3461 		if (!bl->enabled) {
3462 			drm_edp_backlight_enable(bl->aux, &bl->info, brightness);
3463 			bl->enabled = true;
3464 			return 0;
3465 		}
3466 		ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);
3467 	} else {
3468 		if (bl->enabled) {
3469 			drm_edp_backlight_disable(bl->aux, &bl->info);
3470 			bl->enabled = false;
3471 		}
3472 	}
3473 
3474 	return ret;
3475 }
3476 
3477 static const struct backlight_ops dp_aux_bl_ops = {
3478 	.update_status = dp_aux_backlight_update_status,
3479 };
3480 
3481 /**
3482  * drm_panel_dp_aux_backlight - create and use DP AUX backlight
3483  * @panel: DRM panel
3484  * @aux: The DP AUX channel to use
3485  *
3486  * Use this function to create and handle backlight if your panel
3487  * supports backlight control over DP AUX channel using DPCD
3488  * registers as per VESA's standard backlight control interface.
3489  *
3490  * When the panel is enabled backlight will be enabled after a
3491  * successful call to &drm_panel_funcs.enable()
3492  *
3493  * When the panel is disabled backlight will be disabled before the
3494  * call to &drm_panel_funcs.disable().
3495  *
3496  * A typical implementation for a panel driver supporting backlight
3497  * control over DP AUX will call this function at probe time.
3498  * Backlight will then be handled transparently without requiring
3499  * any intervention from the driver.
3500  *
3501  * drm_panel_dp_aux_backlight() must be called after the call to drm_panel_init().
3502  *
3503  * Return: 0 on success or a negative error code on failure.
3504  */
drm_panel_dp_aux_backlight(struct drm_panel * panel,struct drm_dp_aux * aux)3505 int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)
3506 {
3507 	struct dp_aux_backlight *bl;
3508 	struct backlight_properties props = { 0 };
3509 	u16 current_level;
3510 	u8 current_mode;
3511 	u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
3512 	int ret;
3513 
3514 	if (!panel || !panel->dev || !aux)
3515 		return -EINVAL;
3516 
3517 	ret = drm_dp_dpcd_read(aux, DP_EDP_DPCD_REV, edp_dpcd,
3518 			       EDP_DISPLAY_CTL_CAP_SIZE);
3519 	if (ret < 0)
3520 		return ret;
3521 
3522 	if (!drm_edp_backlight_supported(edp_dpcd)) {
3523 		DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");
3524 		return 0;
3525 	}
3526 
3527 	bl = devm_kzalloc(panel->dev, sizeof(*bl), GFP_KERNEL);
3528 	if (!bl)
3529 		return -ENOMEM;
3530 
3531 	bl->aux = aux;
3532 
3533 	ret = drm_edp_backlight_init(aux, &bl->info, 0, edp_dpcd,
3534 				     &current_level, &current_mode);
3535 	if (ret < 0)
3536 		return ret;
3537 
3538 	props.type = BACKLIGHT_RAW;
3539 	props.brightness = current_level;
3540 	props.max_brightness = bl->info.max;
3541 
3542 	bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",
3543 						  panel->dev, bl,
3544 						  &dp_aux_bl_ops, &props);
3545 	if (IS_ERR(bl->base))
3546 		return PTR_ERR(bl->base);
3547 
3548 	backlight_disable(bl->base);
3549 
3550 	panel->backlight = bl->base;
3551 
3552 	return 0;
3553 }
3554 EXPORT_SYMBOL(drm_panel_dp_aux_backlight);
3555 
3556 #endif
3557