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