2 * Copyright © 2009 Keith Packard
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.
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
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>
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>
37 #include "drm_crtc_helper_internal.h"
42 * These functions contain some common logic and helpers at various abstraction
43 * levels to deal with Display Port sink devices and related things like DP aux
44 * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
48 /* Helpers for DP link training */
49 static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
51 return link_status[r - DP_LANE0_1_STATUS];
54 static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
57 int i = DP_LANE0_1_STATUS + (lane >> 1);
58 int s = (lane & 1) * 4;
59 u8 l = dp_link_status(link_status, i);
61 return (l >> s) & 0xf;
64 bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
71 lane_align = dp_link_status(link_status,
72 DP_LANE_ALIGN_STATUS_UPDATED);
73 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
75 for (lane = 0; lane < lane_count; lane++) {
76 lane_status = dp_get_lane_status(link_status, lane);
77 if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
82 EXPORT_SYMBOL(drm_dp_channel_eq_ok);
84 bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
90 for (lane = 0; lane < lane_count; lane++) {
91 lane_status = dp_get_lane_status(link_status, lane);
92 if ((lane_status & DP_LANE_CR_DONE) == 0)
97 EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
99 u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
102 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
103 int s = ((lane & 1) ?
104 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
105 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
106 u8 l = dp_link_status(link_status, i);
108 return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
110 EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
112 u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
115 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
116 int s = ((lane & 1) ?
117 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
118 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
119 u8 l = dp_link_status(link_status, i);
121 return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
123 EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
125 u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
128 unsigned int offset = DP_ADJUST_REQUEST_POST_CURSOR2;
129 u8 value = dp_link_status(link_status, offset);
131 return (value >> (lane << 1)) & 0x3;
133 EXPORT_SYMBOL(drm_dp_get_adjust_request_post_cursor);
135 void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
137 unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
138 DP_TRAINING_AUX_RD_MASK;
141 DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
144 if (rd_interval == 0 || dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
147 rd_interval *= 4 * USEC_PER_MSEC;
149 usleep_range(rd_interval, rd_interval * 2);
151 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
153 static void __drm_dp_link_train_channel_eq_delay(unsigned long rd_interval)
156 DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
159 if (rd_interval == 0)
162 rd_interval *= 4 * USEC_PER_MSEC;
164 usleep_range(rd_interval, rd_interval * 2);
167 void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
169 __drm_dp_link_train_channel_eq_delay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
170 DP_TRAINING_AUX_RD_MASK);
172 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
174 void drm_dp_lttpr_link_train_clock_recovery_delay(void)
176 usleep_range(100, 200);
178 EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
180 static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
182 return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
185 void drm_dp_lttpr_link_train_channel_eq_delay(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
187 u8 interval = dp_lttpr_phy_cap(phy_cap,
188 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
189 DP_TRAINING_AUX_RD_MASK;
191 __drm_dp_link_train_channel_eq_delay(interval);
193 EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
195 u8 drm_dp_link_rate_to_bw_code(int link_rate)
197 /* Spec says link_bw = link_rate / 0.27Gbps */
198 return link_rate / 27000;
200 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
202 int drm_dp_bw_code_to_link_rate(u8 link_bw)
204 /* Spec says link_rate = link_bw * 0.27Gbps */
205 return link_bw * 27000;
207 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
209 #define AUX_RETRY_INTERVAL 500 /* us */
212 drm_dp_dump_access(const struct drm_dp_aux *aux,
213 u8 request, uint offset, void *buffer, int ret)
215 const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
218 DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
219 aux->name, offset, arrow, ret, min(ret, 20), buffer);
221 DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d)\n",
222 aux->name, offset, arrow, ret);
228 * The DisplayPort AUX channel is an abstraction to allow generic, driver-
229 * independent access to AUX functionality. Drivers can take advantage of
230 * this by filling in the fields of the drm_dp_aux structure.
232 * Transactions are described using a hardware-independent drm_dp_aux_msg
233 * structure, which is passed into a driver's .transfer() implementation.
234 * Both native and I2C-over-AUX transactions are supported.
237 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
238 unsigned int offset, void *buffer, size_t size)
240 struct drm_dp_aux_msg msg;
241 unsigned int retry, native_reply;
242 int err = 0, ret = 0;
244 memset(&msg, 0, sizeof(msg));
245 msg.address = offset;
246 msg.request = request;
250 mutex_lock(&aux->hw_mutex);
253 * The specification doesn't give any recommendation on how often to
254 * retry native transactions. We used to retry 7 times like for
255 * aux i2c transactions but real world devices this wasn't
256 * sufficient, bump to 32 which makes Dell 4k monitors happier.
258 for (retry = 0; retry < 32; retry++) {
259 if (ret != 0 && ret != -ETIMEDOUT) {
260 usleep_range(AUX_RETRY_INTERVAL,
261 AUX_RETRY_INTERVAL + 100);
264 ret = aux->transfer(aux, &msg);
266 native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
267 if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
277 * We want the error we return to be the error we received on
278 * the first transaction, since we may get a different error the
285 DRM_DEBUG_KMS("%s: Too many retries, giving up. First error: %d\n",
290 mutex_unlock(&aux->hw_mutex);
295 * drm_dp_dpcd_read() - read a series of bytes from the DPCD
296 * @aux: DisplayPort AUX channel (SST or MST)
297 * @offset: address of the (first) register to read
298 * @buffer: buffer to store the register values
299 * @size: number of bytes in @buffer
301 * Returns the number of bytes transferred on success, or a negative error
302 * code on failure. -EIO is returned if the request was NAKed by the sink or
303 * if the retry count was exceeded. If not all bytes were transferred, this
304 * function returns -EPROTO. Errors from the underlying AUX channel transfer
305 * function, with the exception of -EBUSY (which causes the transaction to
306 * be retried), are propagated to the caller.
308 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
309 void *buffer, size_t size)
314 * HP ZR24w corrupts the first DPCD access after entering power save
315 * mode. Eg. on a read, the entire buffer will be filled with the same
316 * byte. Do a throw away read to avoid corrupting anything we care
317 * about. Afterwards things will work correctly until the monitor
318 * gets woken up and subsequently re-enters power save mode.
320 * The user pressing any button on the monitor is enough to wake it
321 * up, so there is no particularly good place to do the workaround.
322 * We just have to do it before any DPCD access and hope that the
323 * monitor doesn't power down exactly after the throw away read.
325 if (!aux->is_remote) {
326 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
333 ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
335 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
339 drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
342 EXPORT_SYMBOL(drm_dp_dpcd_read);
345 * drm_dp_dpcd_write() - write a series of bytes to the DPCD
346 * @aux: DisplayPort AUX channel (SST or MST)
347 * @offset: address of the (first) register to write
348 * @buffer: buffer containing the values to write
349 * @size: number of bytes in @buffer
351 * Returns the number of bytes transferred on success, or a negative error
352 * code on failure. -EIO is returned if the request was NAKed by the sink or
353 * if the retry count was exceeded. If not all bytes were transferred, this
354 * function returns -EPROTO. Errors from the underlying AUX channel transfer
355 * function, with the exception of -EBUSY (which causes the transaction to
356 * be retried), are propagated to the caller.
358 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
359 void *buffer, size_t size)
364 ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
366 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
369 drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
372 EXPORT_SYMBOL(drm_dp_dpcd_write);
375 * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
376 * @aux: DisplayPort AUX channel
377 * @status: buffer to store the link status in (must be at least 6 bytes)
379 * Returns the number of bytes transferred on success or a negative error
382 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
383 u8 status[DP_LINK_STATUS_SIZE])
385 return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
386 DP_LINK_STATUS_SIZE);
388 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
391 * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
392 * @aux: DisplayPort AUX channel
393 * @dp_phy: the DP PHY to get the link status for
394 * @link_status: buffer to return the status in
396 * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
397 * layout of the returned @link_status matches the DPCD register layout of the
398 * DPRX PHY link status.
400 * Returns 0 if the information was read successfully or a negative error code
403 int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
404 enum drm_dp_phy dp_phy,
405 u8 link_status[DP_LINK_STATUS_SIZE])
409 if (dp_phy == DP_PHY_DPRX) {
410 ret = drm_dp_dpcd_read(aux,
413 DP_LINK_STATUS_SIZE);
418 WARN_ON(ret != DP_LINK_STATUS_SIZE);
423 ret = drm_dp_dpcd_read(aux,
424 DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
426 DP_LINK_STATUS_SIZE - 1);
431 WARN_ON(ret != DP_LINK_STATUS_SIZE - 1);
433 /* Convert the LTTPR to the sink PHY link status layout */
434 memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
435 &link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
436 DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
437 link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
441 EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
443 static bool is_edid_digital_input_dp(const struct edid *edid)
445 return edid && edid->revision >= 4 &&
446 edid->input & DRM_EDID_INPUT_DIGITAL &&
447 (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
451 * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
452 * @dpcd: DisplayPort configuration data
453 * @port_cap: port capabilities
454 * @type: port type to be checked. Can be:
455 * %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
456 * %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
457 * %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
459 * Caveat: Only works with DPCD 1.1+ port caps.
461 * Returns: whether the downstream facing port matches the type.
463 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
464 const u8 port_cap[4], u8 type)
466 return drm_dp_is_branch(dpcd) &&
467 dpcd[DP_DPCD_REV] >= 0x11 &&
468 (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
470 EXPORT_SYMBOL(drm_dp_downstream_is_type);
473 * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
474 * @dpcd: DisplayPort configuration data
475 * @port_cap: port capabilities
478 * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
480 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
481 const u8 port_cap[4],
482 const struct edid *edid)
484 if (dpcd[DP_DPCD_REV] < 0x11) {
485 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
486 case DP_DWN_STRM_PORT_TYPE_TMDS:
493 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
494 case DP_DS_PORT_TYPE_DP_DUALMODE:
495 if (is_edid_digital_input_dp(edid))
498 case DP_DS_PORT_TYPE_DVI:
499 case DP_DS_PORT_TYPE_HDMI:
505 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
508 * drm_dp_send_real_edid_checksum() - send back real edid checksum value
509 * @aux: DisplayPort AUX channel
510 * @real_edid_checksum: real edid checksum for the last block
515 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
516 u8 real_edid_checksum)
518 u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
520 if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
521 &auto_test_req, 1) < 1) {
522 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
523 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
526 auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
528 if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
529 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
530 aux->name, DP_TEST_REQUEST);
533 link_edid_read &= DP_TEST_LINK_EDID_READ;
535 if (!auto_test_req || !link_edid_read) {
536 DRM_DEBUG_KMS("%s: Source DUT does not support TEST_EDID_READ\n",
541 if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
542 &auto_test_req, 1) < 1) {
543 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
544 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
548 /* send back checksum for the last edid extension block data */
549 if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
550 &real_edid_checksum, 1) < 1) {
551 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
552 aux->name, DP_TEST_EDID_CHECKSUM);
556 test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
557 if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
558 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
559 aux->name, DP_TEST_RESPONSE);
565 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
567 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
569 u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
571 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
577 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
578 u8 dpcd[DP_RECEIVER_CAP_SIZE])
584 * Prior to DP1.3 the bit represented by
585 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
586 * If it is set DP_DPCD_REV at 0000h could be at a value less than
587 * the true capability of the panel. The only way to check is to
588 * then compare 0000h and 2200h.
590 if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
591 DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
594 ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
598 if (ret != sizeof(dpcd_ext))
601 if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
602 DRM_DEBUG_KMS("%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
603 aux->name, dpcd[DP_DPCD_REV],
604 dpcd_ext[DP_DPCD_REV]);
608 if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
611 DRM_DEBUG_KMS("%s: Base DPCD: %*ph\n",
612 aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
614 memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
620 * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
622 * @aux: DisplayPort AUX channel
623 * @dpcd: Buffer to store the resulting DPCD in
625 * Attempts to read the base DPCD caps for @aux. Additionally, this function
626 * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
629 * Returns: %0 if the DPCD was read successfully, negative error code
632 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
633 u8 dpcd[DP_RECEIVER_CAP_SIZE])
637 ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
640 if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
643 ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
647 DRM_DEBUG_KMS("%s: DPCD: %*ph\n",
648 aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
652 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
655 * drm_dp_read_downstream_info() - read DPCD downstream port info if available
656 * @aux: DisplayPort AUX channel
657 * @dpcd: A cached copy of the port's DPCD
658 * @downstream_ports: buffer to store the downstream port info in
661 * drm_dp_downstream_max_clock()
662 * drm_dp_downstream_max_bpc()
664 * Returns: 0 if either the downstream port info was read successfully or
665 * there was no downstream info to read, or a negative error code otherwise.
667 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
668 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
669 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
674 memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
676 /* No downstream info to read */
677 if (!drm_dp_is_branch(dpcd) ||
678 dpcd[DP_DPCD_REV] < DP_DPCD_REV_10 ||
679 !(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
682 len = drm_dp_downstream_port_count(dpcd);
683 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
686 ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
692 DRM_DEBUG_KMS("%s: DPCD DFP: %*ph\n",
693 aux->name, len, downstream_ports);
697 EXPORT_SYMBOL(drm_dp_read_downstream_info);
700 * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
701 * @dpcd: DisplayPort configuration data
702 * @port_cap: port capabilities
704 * Returns: Downstream facing port max dot clock in kHz on success,
705 * or 0 if max clock not defined
707 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
708 const u8 port_cap[4])
710 if (!drm_dp_is_branch(dpcd))
713 if (dpcd[DP_DPCD_REV] < 0x11)
716 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
717 case DP_DS_PORT_TYPE_VGA:
718 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
720 return port_cap[1] * 8000;
725 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
728 * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
729 * @dpcd: DisplayPort configuration data
730 * @port_cap: port capabilities
733 * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
734 * or 0 if max TMDS clock not defined
736 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
737 const u8 port_cap[4],
738 const struct edid *edid)
740 if (!drm_dp_is_branch(dpcd))
743 if (dpcd[DP_DPCD_REV] < 0x11) {
744 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
745 case DP_DWN_STRM_PORT_TYPE_TMDS:
752 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
753 case DP_DS_PORT_TYPE_DP_DUALMODE:
754 if (is_edid_digital_input_dp(edid))
757 * It's left up to the driver to check the
758 * DP dual mode adapter's max TMDS clock.
760 * Unfortunatley it looks like branch devices
761 * may not fordward that the DP dual mode i2c
762 * access so we just usually get i2c nak :(
765 case DP_DS_PORT_TYPE_HDMI:
767 * We should perhaps assume 165 MHz when detailed cap
768 * info is not available. But looks like many typical
769 * branch devices fall into that category and so we'd
770 * probably end up with users complaining that they can't
771 * get high resolution modes with their favorite dongle.
773 * So let's limit to 300 MHz instead since DPCD 1.4
774 * HDMI 2.0 DFPs are required to have the detailed cap
775 * info. So it's more likely we're dealing with a HDMI 1.4
776 * compatible* device here.
778 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
780 return port_cap[1] * 2500;
781 case DP_DS_PORT_TYPE_DVI:
782 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
784 /* FIXME what to do about DVI dual link? */
785 return port_cap[1] * 2500;
790 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
793 * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
794 * @dpcd: DisplayPort configuration data
795 * @port_cap: port capabilities
798 * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
799 * or 0 if max TMDS clock not defined
801 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
802 const u8 port_cap[4],
803 const struct edid *edid)
805 if (!drm_dp_is_branch(dpcd))
808 if (dpcd[DP_DPCD_REV] < 0x11) {
809 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
810 case DP_DWN_STRM_PORT_TYPE_TMDS:
817 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
818 case DP_DS_PORT_TYPE_DP_DUALMODE:
819 if (is_edid_digital_input_dp(edid))
822 case DP_DS_PORT_TYPE_DVI:
823 case DP_DS_PORT_TYPE_HDMI:
825 * Unclear whether the protocol converter could
826 * utilize pixel replication. Assume it won't.
833 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
836 * drm_dp_downstream_max_bpc() - extract downstream facing port max
838 * @dpcd: DisplayPort configuration data
839 * @port_cap: downstream facing port capabilities
842 * Returns: Max bpc on success or 0 if max bpc not defined
844 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
845 const u8 port_cap[4],
846 const struct edid *edid)
848 if (!drm_dp_is_branch(dpcd))
851 if (dpcd[DP_DPCD_REV] < 0x11) {
852 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
853 case DP_DWN_STRM_PORT_TYPE_DP:
860 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
861 case DP_DS_PORT_TYPE_DP:
863 case DP_DS_PORT_TYPE_DP_DUALMODE:
864 if (is_edid_digital_input_dp(edid))
867 case DP_DS_PORT_TYPE_HDMI:
868 case DP_DS_PORT_TYPE_DVI:
869 case DP_DS_PORT_TYPE_VGA:
870 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
873 switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
890 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
893 * drm_dp_downstream_420_passthrough() - determine downstream facing port
894 * YCbCr 4:2:0 pass-through capability
895 * @dpcd: DisplayPort configuration data
896 * @port_cap: downstream facing port capabilities
898 * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
900 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
901 const u8 port_cap[4])
903 if (!drm_dp_is_branch(dpcd))
906 if (dpcd[DP_DPCD_REV] < 0x13)
909 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
910 case DP_DS_PORT_TYPE_DP:
912 case DP_DS_PORT_TYPE_HDMI:
913 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
916 return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
921 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
924 * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
925 * YCbCr 4:4:4->4:2:0 conversion capability
926 * @dpcd: DisplayPort configuration data
927 * @port_cap: downstream facing port capabilities
929 * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
931 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
932 const u8 port_cap[4])
934 if (!drm_dp_is_branch(dpcd))
937 if (dpcd[DP_DPCD_REV] < 0x13)
940 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
941 case DP_DS_PORT_TYPE_HDMI:
942 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
945 return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
950 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
953 * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
954 * RGB->YCbCr conversion capability
955 * @dpcd: DisplayPort configuration data
956 * @port_cap: downstream facing port capabilities
957 * @colorspc: Colorspace for which conversion cap is sought
959 * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
962 bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
963 const u8 port_cap[4],
966 if (!drm_dp_is_branch(dpcd))
969 if (dpcd[DP_DPCD_REV] < 0x13)
972 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
973 case DP_DS_PORT_TYPE_HDMI:
974 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
977 return port_cap[3] & color_spc;
982 EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
985 * drm_dp_downstream_mode() - return a mode for downstream facing port
987 * @dpcd: DisplayPort configuration data
988 * @port_cap: port capabilities
990 * Provides a suitable mode for downstream facing ports without EDID.
992 * Returns: A new drm_display_mode on success or NULL on failure
994 struct drm_display_mode *
995 drm_dp_downstream_mode(struct drm_device *dev,
996 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
997 const u8 port_cap[4])
1002 if (!drm_dp_is_branch(dpcd))
1005 if (dpcd[DP_DPCD_REV] < 0x11)
1008 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1009 case DP_DS_PORT_TYPE_NON_EDID:
1010 switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
1011 case DP_DS_NON_EDID_720x480i_60:
1014 case DP_DS_NON_EDID_720x480i_50:
1017 case DP_DS_NON_EDID_1920x1080i_60:
1020 case DP_DS_NON_EDID_1920x1080i_50:
1023 case DP_DS_NON_EDID_1280x720_60:
1026 case DP_DS_NON_EDID_1280x720_50:
1032 return drm_display_mode_from_cea_vic(dev, vic);
1037 EXPORT_SYMBOL(drm_dp_downstream_mode);
1040 * drm_dp_downstream_id() - identify branch device
1041 * @aux: DisplayPort AUX channel
1042 * @id: DisplayPort branch device id
1044 * Returns branch device id on success or NULL on failure
1046 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
1048 return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
1050 EXPORT_SYMBOL(drm_dp_downstream_id);
1053 * drm_dp_downstream_debug() - debug DP branch devices
1054 * @m: pointer for debugfs file
1055 * @dpcd: DisplayPort configuration data
1056 * @port_cap: port capabilities
1058 * @aux: DisplayPort AUX channel
1061 void drm_dp_downstream_debug(struct seq_file *m,
1062 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1063 const u8 port_cap[4],
1064 const struct edid *edid,
1065 struct drm_dp_aux *aux)
1067 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1068 DP_DETAILED_CAP_INFO_AVAILABLE;
1074 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1075 bool branch_device = drm_dp_is_branch(dpcd);
1077 seq_printf(m, "\tDP branch device present: %s\n",
1078 branch_device ? "yes" : "no");
1084 case DP_DS_PORT_TYPE_DP:
1085 seq_puts(m, "\t\tType: DisplayPort\n");
1087 case DP_DS_PORT_TYPE_VGA:
1088 seq_puts(m, "\t\tType: VGA\n");
1090 case DP_DS_PORT_TYPE_DVI:
1091 seq_puts(m, "\t\tType: DVI\n");
1093 case DP_DS_PORT_TYPE_HDMI:
1094 seq_puts(m, "\t\tType: HDMI\n");
1096 case DP_DS_PORT_TYPE_NON_EDID:
1097 seq_puts(m, "\t\tType: others without EDID support\n");
1099 case DP_DS_PORT_TYPE_DP_DUALMODE:
1100 seq_puts(m, "\t\tType: DP++\n");
1102 case DP_DS_PORT_TYPE_WIRELESS:
1103 seq_puts(m, "\t\tType: Wireless\n");
1106 seq_puts(m, "\t\tType: N/A\n");
1109 memset(id, 0, sizeof(id));
1110 drm_dp_downstream_id(aux, id);
1111 seq_printf(m, "\t\tID: %s\n", id);
1113 len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1115 seq_printf(m, "\t\tHW: %d.%d\n",
1116 (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1118 len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1120 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1122 if (detailed_cap_info) {
1123 clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1125 seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1127 clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1129 seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1131 clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1133 seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1135 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1138 seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1141 EXPORT_SYMBOL(drm_dp_downstream_debug);
1144 * drm_dp_subconnector_type() - get DP branch device type
1145 * @dpcd: DisplayPort configuration data
1146 * @port_cap: port capabilities
1148 enum drm_mode_subconnector
1149 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1150 const u8 port_cap[4])
1153 if (!drm_dp_is_branch(dpcd))
1154 return DRM_MODE_SUBCONNECTOR_Native;
1155 /* DP 1.0 approach */
1156 if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1157 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1158 DP_DWN_STRM_PORT_TYPE_MASK;
1161 case DP_DWN_STRM_PORT_TYPE_TMDS:
1162 /* Can be HDMI or DVI-D, DVI-D is a safer option */
1163 return DRM_MODE_SUBCONNECTOR_DVID;
1164 case DP_DWN_STRM_PORT_TYPE_ANALOG:
1165 /* Can be VGA or DVI-A, VGA is more popular */
1166 return DRM_MODE_SUBCONNECTOR_VGA;
1167 case DP_DWN_STRM_PORT_TYPE_DP:
1168 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1169 case DP_DWN_STRM_PORT_TYPE_OTHER:
1171 return DRM_MODE_SUBCONNECTOR_Unknown;
1174 type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1177 case DP_DS_PORT_TYPE_DP:
1178 case DP_DS_PORT_TYPE_DP_DUALMODE:
1179 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1180 case DP_DS_PORT_TYPE_VGA:
1181 return DRM_MODE_SUBCONNECTOR_VGA;
1182 case DP_DS_PORT_TYPE_DVI:
1183 return DRM_MODE_SUBCONNECTOR_DVID;
1184 case DP_DS_PORT_TYPE_HDMI:
1185 return DRM_MODE_SUBCONNECTOR_HDMIA;
1186 case DP_DS_PORT_TYPE_WIRELESS:
1187 return DRM_MODE_SUBCONNECTOR_Wireless;
1188 case DP_DS_PORT_TYPE_NON_EDID:
1190 return DRM_MODE_SUBCONNECTOR_Unknown;
1193 EXPORT_SYMBOL(drm_dp_subconnector_type);
1196 * drm_dp_set_subconnector_property - set subconnector for DP connector
1197 * @connector: connector to set property on
1198 * @status: connector status
1199 * @dpcd: DisplayPort configuration data
1200 * @port_cap: port capabilities
1202 * Called by a driver on every detect event.
1204 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1205 enum drm_connector_status status,
1207 const u8 port_cap[4])
1209 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1211 if (status == connector_status_connected)
1212 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1213 drm_object_property_set_value(&connector->base,
1214 connector->dev->mode_config.dp_subconnector_property,
1217 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1220 * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1222 * @connector: The DRM connector to check
1223 * @dpcd: A cached copy of the connector's DPCD RX capabilities
1224 * @desc: A cached copy of the connector's DP descriptor
1226 * See also: drm_dp_read_sink_count()
1228 * Returns: %True if the (e)DP connector has a valid sink count that should
1229 * be probed, %false otherwise.
1231 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1232 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1233 const struct drm_dp_desc *desc)
1235 /* Some eDP panels don't set a valid value for the sink count */
1236 return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1237 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1238 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1239 !drm_dp_has_quirk(desc, 0, DP_DPCD_QUIRK_NO_SINK_COUNT);
1241 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1244 * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1245 * @aux: The DP AUX channel to use
1247 * See also: drm_dp_read_sink_count_cap()
1249 * Returns: The current sink count reported by @aux, or a negative error code
1252 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1257 ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1263 return DP_GET_SINK_COUNT(count);
1265 EXPORT_SYMBOL(drm_dp_read_sink_count);
1268 * I2C-over-AUX implementation
1271 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1273 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1274 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1275 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1276 I2C_FUNC_10BIT_ADDR;
1279 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1282 * In case of i2c defer or short i2c ack reply to a write,
1283 * we need to switch to WRITE_STATUS_UPDATE to drain the
1284 * rest of the message
1286 if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1287 msg->request &= DP_AUX_I2C_MOT;
1288 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1292 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1293 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1294 #define AUX_STOP_LEN 4
1295 #define AUX_CMD_LEN 4
1296 #define AUX_ADDRESS_LEN 20
1297 #define AUX_REPLY_PAD_LEN 4
1298 #define AUX_LENGTH_LEN 8
1301 * Calculate the duration of the AUX request/reply in usec. Gives the
1302 * "best" case estimate, ie. successful while as short as possible.
1304 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1306 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1307 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1309 if ((msg->request & DP_AUX_I2C_READ) == 0)
1310 len += msg->size * 8;
1315 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1317 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1318 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1321 * For read we expect what was asked. For writes there will
1322 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1324 if (msg->request & DP_AUX_I2C_READ)
1325 len += msg->size * 8;
1330 #define I2C_START_LEN 1
1331 #define I2C_STOP_LEN 1
1332 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1333 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1336 * Calculate the length of the i2c transfer in usec, assuming
1337 * the i2c bus speed is as specified. Gives the the "worst"
1338 * case estimate, ie. successful while as long as possible.
1339 * Doesn't account the the "MOT" bit, and instead assumes each
1340 * message includes a START, ADDRESS and STOP. Neither does it
1341 * account for additional random variables such as clock stretching.
1343 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1346 /* AUX bitrate is 1MHz, i2c bitrate as specified */
1347 return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1348 msg->size * I2C_DATA_LEN +
1349 I2C_STOP_LEN) * 1000, i2c_speed_khz);
1353 * Deterine how many retries should be attempted to successfully transfer
1354 * the specified message, based on the estimated durations of the
1355 * i2c and AUX transfers.
1357 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1360 int aux_time_us = drm_dp_aux_req_duration(msg) +
1361 drm_dp_aux_reply_duration(msg);
1362 int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1364 return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1368 * FIXME currently assumes 10 kHz as some real world devices seem
1369 * to require it. We should query/set the speed via DPCD if supported.
1371 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1372 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1373 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1374 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1377 * Transfer a single I2C-over-AUX message and handle various error conditions,
1378 * retrying the transaction as appropriate. It is assumed that the
1379 * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1382 * Returns bytes transferred on success, or a negative error code on failure.
1384 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1386 unsigned int retry, defer_i2c;
1389 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1390 * is required to retry at least seven times upon receiving AUX_DEFER
1391 * before giving up the AUX transaction.
1393 * We also try to account for the i2c bus speed.
1395 int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1397 for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1398 ret = aux->transfer(aux, msg);
1404 * While timeouts can be errors, they're usually normal
1405 * behavior (for instance, when a driver tries to
1406 * communicate with a non-existant DisplayPort device).
1407 * Avoid spamming the kernel log with timeout errors.
1409 if (ret == -ETIMEDOUT)
1410 DRM_DEBUG_KMS_RATELIMITED("%s: transaction timed out\n",
1413 DRM_DEBUG_KMS("%s: transaction failed: %d\n",
1419 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1420 case DP_AUX_NATIVE_REPLY_ACK:
1422 * For I2C-over-AUX transactions this isn't enough, we
1423 * need to check for the I2C ACK reply.
1427 case DP_AUX_NATIVE_REPLY_NACK:
1428 DRM_DEBUG_KMS("%s: native nack (result=%d, size=%zu)\n",
1429 aux->name, ret, msg->size);
1432 case DP_AUX_NATIVE_REPLY_DEFER:
1433 DRM_DEBUG_KMS("%s: native defer\n", aux->name);
1435 * We could check for I2C bit rate capabilities and if
1436 * available adjust this interval. We could also be
1437 * more careful with DP-to-legacy adapters where a
1438 * long legacy cable may force very low I2C bit rates.
1440 * For now just defer for long enough to hopefully be
1441 * safe for all use-cases.
1443 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1447 DRM_ERROR("%s: invalid native reply %#04x\n",
1448 aux->name, msg->reply);
1452 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1453 case DP_AUX_I2C_REPLY_ACK:
1455 * Both native ACK and I2C ACK replies received. We
1456 * can assume the transfer was successful.
1458 if (ret != msg->size)
1459 drm_dp_i2c_msg_write_status_update(msg);
1462 case DP_AUX_I2C_REPLY_NACK:
1463 DRM_DEBUG_KMS("%s: I2C nack (result=%d, size=%zu)\n",
1464 aux->name, ret, msg->size);
1465 aux->i2c_nack_count++;
1468 case DP_AUX_I2C_REPLY_DEFER:
1469 DRM_DEBUG_KMS("%s: I2C defer\n", aux->name);
1470 /* DP Compliance Test 4.2.2.5 Requirement:
1471 * Must have at least 7 retries for I2C defers on the
1472 * transaction to pass this test
1474 aux->i2c_defer_count++;
1477 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1478 drm_dp_i2c_msg_write_status_update(msg);
1483 DRM_ERROR("%s: invalid I2C reply %#04x\n",
1484 aux->name, msg->reply);
1489 DRM_DEBUG_KMS("%s: Too many retries, giving up\n", aux->name);
1493 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1494 const struct i2c_msg *i2c_msg)
1496 msg->request = (i2c_msg->flags & I2C_M_RD) ?
1497 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1498 if (!(i2c_msg->flags & I2C_M_STOP))
1499 msg->request |= DP_AUX_I2C_MOT;
1503 * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1505 * Returns an error code on failure, or a recommended transfer size on success.
1507 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1509 int err, ret = orig_msg->size;
1510 struct drm_dp_aux_msg msg = *orig_msg;
1512 while (msg.size > 0) {
1513 err = drm_dp_i2c_do_msg(aux, &msg);
1515 return err == 0 ? -EPROTO : err;
1517 if (err < msg.size && err < ret) {
1518 DRM_DEBUG_KMS("%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1519 aux->name, msg.size, err);
1531 * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1532 * packets to be as large as possible. If not, the I2C transactions never
1533 * succeed. Hence the default is maximum.
1535 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1536 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1537 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1538 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1540 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1543 struct drm_dp_aux *aux = adapter->algo_data;
1545 unsigned transfer_size;
1546 struct drm_dp_aux_msg msg;
1549 dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1551 memset(&msg, 0, sizeof(msg));
1553 for (i = 0; i < num; i++) {
1554 msg.address = msgs[i].addr;
1555 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1556 /* Send a bare address packet to start the transaction.
1557 * Zero sized messages specify an address only (bare
1558 * address) transaction.
1562 err = drm_dp_i2c_do_msg(aux, &msg);
1565 * Reset msg.request in case in case it got
1566 * changed into a WRITE_STATUS_UPDATE.
1568 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1572 /* We want each transaction to be as large as possible, but
1573 * we'll go to smaller sizes if the hardware gives us a
1576 transfer_size = dp_aux_i2c_transfer_size;
1577 for (j = 0; j < msgs[i].len; j += msg.size) {
1578 msg.buffer = msgs[i].buf + j;
1579 msg.size = min(transfer_size, msgs[i].len - j);
1581 err = drm_dp_i2c_drain_msg(aux, &msg);
1584 * Reset msg.request in case in case it got
1585 * changed into a WRITE_STATUS_UPDATE.
1587 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1591 transfer_size = err;
1598 /* Send a bare address packet to close out the transaction.
1599 * Zero sized messages specify an address only (bare
1600 * address) transaction.
1602 msg.request &= ~DP_AUX_I2C_MOT;
1605 (void)drm_dp_i2c_do_msg(aux, &msg);
1610 static const struct i2c_algorithm drm_dp_i2c_algo = {
1611 .functionality = drm_dp_i2c_functionality,
1612 .master_xfer = drm_dp_i2c_xfer,
1615 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1617 return container_of(i2c, struct drm_dp_aux, ddc);
1620 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1622 mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1625 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1627 return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1630 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1632 mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1635 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1636 .lock_bus = lock_bus,
1637 .trylock_bus = trylock_bus,
1638 .unlock_bus = unlock_bus,
1641 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1646 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1650 WARN_ON(!(buf & DP_TEST_SINK_START));
1652 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1656 count = buf & DP_TEST_COUNT_MASK;
1657 if (count == aux->crc_count)
1658 return -EAGAIN; /* No CRC yet */
1660 aux->crc_count = count;
1663 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1664 * per component (RGB or CrYCb).
1666 ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1673 static void drm_dp_aux_crc_work(struct work_struct *work)
1675 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1677 struct drm_crtc *crtc;
1682 if (WARN_ON(!aux->crtc))
1686 while (crtc->crc.opened) {
1687 drm_crtc_wait_one_vblank(crtc);
1688 if (!crtc->crc.opened)
1691 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1692 if (ret == -EAGAIN) {
1693 usleep_range(1000, 2000);
1694 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1697 if (ret == -EAGAIN) {
1698 DRM_DEBUG_KMS("%s: Get CRC failed after retrying: %d\n",
1702 DRM_DEBUG_KMS("%s: Failed to get a CRC: %d\n",
1707 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1708 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1709 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1710 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1715 * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1716 * @aux: DisplayPort AUX channel
1718 * Used for remote aux channel in general. Merely initialize the crc work
1721 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1723 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1725 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1728 * drm_dp_aux_init() - minimally initialise an aux channel
1729 * @aux: DisplayPort AUX channel
1731 * If you need to use the drm_dp_aux's i2c adapter prior to registering it
1732 * with the outside world, call drm_dp_aux_init() first. You must still
1733 * call drm_dp_aux_register() once the connector has been registered to
1734 * allow userspace access to the auxiliary DP channel.
1736 void drm_dp_aux_init(struct drm_dp_aux *aux)
1738 mutex_init(&aux->hw_mutex);
1739 mutex_init(&aux->cec.lock);
1740 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1742 aux->ddc.algo = &drm_dp_i2c_algo;
1743 aux->ddc.algo_data = aux;
1744 aux->ddc.retries = 3;
1746 aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1748 EXPORT_SYMBOL(drm_dp_aux_init);
1751 * drm_dp_aux_register() - initialise and register aux channel
1752 * @aux: DisplayPort AUX channel
1754 * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
1755 * This should only be called when the underlying &struct drm_connector is
1756 * initialiazed already. Therefore the best place to call this is from
1757 * &drm_connector_funcs.late_register. Not that drivers which don't follow this
1758 * will Oops when CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1760 * Drivers which need to use the aux channel before that point (e.g. at driver
1761 * load time, before drm_dev_register() has been called) need to call
1762 * drm_dp_aux_init().
1764 * Returns 0 on success or a negative error code on failure.
1766 int drm_dp_aux_register(struct drm_dp_aux *aux)
1771 drm_dp_aux_init(aux);
1773 aux->ddc.class = I2C_CLASS_DDC;
1774 aux->ddc.owner = THIS_MODULE;
1775 aux->ddc.dev.parent = aux->dev;
1777 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1778 sizeof(aux->ddc.name));
1780 ret = drm_dp_aux_register_devnode(aux);
1784 ret = i2c_add_adapter(&aux->ddc);
1786 drm_dp_aux_unregister_devnode(aux);
1792 EXPORT_SYMBOL(drm_dp_aux_register);
1795 * drm_dp_aux_unregister() - unregister an AUX adapter
1796 * @aux: DisplayPort AUX channel
1798 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1800 drm_dp_aux_unregister_devnode(aux);
1801 i2c_del_adapter(&aux->ddc);
1803 EXPORT_SYMBOL(drm_dp_aux_unregister);
1805 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1808 * drm_dp_psr_setup_time() - PSR setup in time usec
1809 * @psr_cap: PSR capabilities from DPCD
1812 * PSR setup time for the panel in microseconds, negative
1813 * error code on failure.
1815 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1817 static const u16 psr_setup_time_us[] = {
1818 PSR_SETUP_TIME(330),
1819 PSR_SETUP_TIME(275),
1820 PSR_SETUP_TIME(220),
1821 PSR_SETUP_TIME(165),
1822 PSR_SETUP_TIME(110),
1828 i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1829 if (i >= ARRAY_SIZE(psr_setup_time_us))
1832 return psr_setup_time_us[i];
1834 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1836 #undef PSR_SETUP_TIME
1839 * drm_dp_start_crc() - start capture of frame CRCs
1840 * @aux: DisplayPort AUX channel
1841 * @crtc: CRTC displaying the frames whose CRCs are to be captured
1843 * Returns 0 on success or a negative error code on failure.
1845 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1850 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1854 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1860 schedule_work(&aux->crc_work);
1864 EXPORT_SYMBOL(drm_dp_start_crc);
1867 * drm_dp_stop_crc() - stop capture of frame CRCs
1868 * @aux: DisplayPort AUX channel
1870 * Returns 0 on success or a negative error code on failure.
1872 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1877 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1881 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1885 flush_work(&aux->crc_work);
1890 EXPORT_SYMBOL(drm_dp_stop_crc);
1899 #define OUI(first, second, third) { (first), (second), (third) }
1900 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1901 { (first), (second), (third), (fourth), (fifth), (sixth) }
1903 #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
1905 static const struct dpcd_quirk dpcd_quirk_list[] = {
1906 /* Analogix 7737 needs reduced M and N at HBR2 link rates */
1907 { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1908 /* LG LP140WF6-SPM1 eDP panel */
1909 { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1910 /* Apple panels need some additional handling to support PSR */
1911 { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1912 /* CH7511 seems to leave SINK_COUNT zeroed */
1913 { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1914 /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1915 { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1916 /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1917 { 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) },
1923 * Get a bit mask of DPCD quirks for the sink/branch device identified by
1924 * ident. The quirk data is shared but it's up to the drivers to act on the
1927 * For now, only the OUI (first three bytes) is used, but this may be extended
1928 * to device identification string and hardware/firmware revisions later.
1931 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1933 const struct dpcd_quirk *quirk;
1936 u8 any_device[] = DEVICE_ID_ANY;
1938 for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1939 quirk = &dpcd_quirk_list[i];
1941 if (quirk->is_branch != is_branch)
1944 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1947 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
1948 memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
1951 quirks |= quirk->quirks;
1957 #undef DEVICE_ID_ANY
1966 #define MFG(first, second) { (first), (second) }
1967 #define PROD_ID(first, second) { (first), (second) }
1970 * Some devices have unreliable OUIDs where they don't set the device ID
1971 * correctly, and as a result we need to use the EDID for finding additional
1972 * DP quirks in such cases.
1974 static const struct edid_quirk edid_quirk_list[] = {
1975 /* Optional 4K AMOLED panel in the ThinkPad X1 Extreme 2nd Generation
1976 * only supports DPCD backlight controls
1978 { MFG(0x4c, 0x83), PROD_ID(0x41, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1980 * Some Dell CML 2020 systems have panels support both AUX and PWM
1981 * backlight control, and some only support AUX backlight control. All
1982 * said panels start up in AUX mode by default, and we don't have any
1983 * support for disabling HDR mode on these panels which would be
1984 * required to switch to PWM backlight control mode (plus, I'm not
1985 * even sure we want PWM backlight controls over DPCD backlight
1986 * controls anyway...). Until we have a better way of detecting these,
1987 * force DPCD backlight mode on all of them.
1989 { MFG(0x06, 0xaf), PROD_ID(0x9b, 0x32), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1990 { MFG(0x06, 0xaf), PROD_ID(0xeb, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1991 { MFG(0x4d, 0x10), PROD_ID(0xc7, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1992 { MFG(0x4d, 0x10), PROD_ID(0xe6, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1993 { MFG(0x4c, 0x83), PROD_ID(0x47, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1994 { MFG(0x09, 0xe5), PROD_ID(0xde, 0x08), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
2001 * drm_dp_get_edid_quirks() - Check the EDID of a DP device to find additional
2002 * DP-specific quirks
2003 * @edid: The EDID to check
2005 * While OUIDs are meant to be used to recognize a DisplayPort device, a lot
2006 * of manufacturers don't seem to like following standards and neglect to fill
2007 * the dev-ID in, making it impossible to only use OUIDs for determining
2008 * quirks in some cases. This function can be used to check the EDID and look
2009 * up any additional DP quirks. The bits returned by this function correspond
2010 * to the quirk bits in &drm_dp_quirk.
2012 * Returns: a bitmask of quirks, if any. The driver can check this using
2013 * drm_dp_has_quirk().
2015 u32 drm_dp_get_edid_quirks(const struct edid *edid)
2017 const struct edid_quirk *quirk;
2024 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
2025 quirk = &edid_quirk_list[i];
2026 if (memcmp(quirk->mfg_id, edid->mfg_id,
2027 sizeof(edid->mfg_id)) == 0 &&
2028 memcmp(quirk->prod_id, edid->prod_code,
2029 sizeof(edid->prod_code)) == 0)
2030 quirks |= quirk->quirks;
2033 DRM_DEBUG_KMS("DP sink: EDID mfg %*phD prod-ID %*phD quirks: 0x%04x\n",
2034 (int)sizeof(edid->mfg_id), edid->mfg_id,
2035 (int)sizeof(edid->prod_code), edid->prod_code, quirks);
2039 EXPORT_SYMBOL(drm_dp_get_edid_quirks);
2042 * drm_dp_read_desc - read sink/branch descriptor from DPCD
2043 * @aux: DisplayPort AUX channel
2044 * @desc: Device descriptor to fill from DPCD
2045 * @is_branch: true for branch devices, false for sink devices
2047 * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
2050 * Returns 0 on success or a negative error code on failure.
2052 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
2055 struct drm_dp_dpcd_ident *ident = &desc->ident;
2056 unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
2057 int ret, dev_id_len;
2059 ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
2063 desc->quirks = drm_dp_get_quirks(ident, is_branch);
2065 dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
2067 DRM_DEBUG_KMS("%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
2068 aux->name, is_branch ? "branch" : "sink",
2069 (int)sizeof(ident->oui), ident->oui,
2070 dev_id_len, ident->device_id,
2071 ident->hw_rev >> 4, ident->hw_rev & 0xf,
2072 ident->sw_major_rev, ident->sw_minor_rev,
2077 EXPORT_SYMBOL(drm_dp_read_desc);
2080 * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
2081 * supported by the DSC sink.
2082 * @dsc_dpcd: DSC capabilities from DPCD
2083 * @is_edp: true if its eDP, false for DP
2085 * Read the slice capabilities DPCD register from DSC sink to get
2086 * the maximum slice count supported. This is used to populate
2087 * the DSC parameters in the &struct drm_dsc_config by the driver.
2088 * Driver creates an infoframe using these parameters to populate
2089 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2090 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2093 * Maximum slice count supported by DSC sink or 0 its invalid
2095 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2098 u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
2101 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
2102 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2104 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2106 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2109 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2110 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2112 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2114 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2116 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2118 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2120 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2122 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2124 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2126 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2128 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2130 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2136 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2139 * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2140 * @dsc_dpcd: DSC capabilities from DPCD
2142 * Read the DSC DPCD register to parse the line buffer depth in bits which is
2143 * number of bits of precision within the decoder line buffer supported by
2144 * the DSC sink. This is used to populate the DSC parameters in the
2145 * &struct drm_dsc_config by the driver.
2146 * Driver creates an infoframe using these parameters to populate
2147 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2148 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2151 * Line buffer depth supported by DSC panel or 0 its invalid
2153 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2155 u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2157 switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2158 case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2160 case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2162 case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2164 case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2166 case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2168 case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2170 case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2172 case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2174 case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2180 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2183 * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2184 * values supported by the DSC sink.
2185 * @dsc_dpcd: DSC capabilities from DPCD
2186 * @dsc_bpc: An array to be filled by this helper with supported
2189 * Read the DSC DPCD from the sink device to parse the supported bits per
2190 * component values. This is used to populate the DSC parameters
2191 * in the &struct drm_dsc_config by the driver.
2192 * Driver creates an infoframe using these parameters to populate
2193 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2194 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2197 * Number of input BPC values parsed from the DPCD
2199 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2203 u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2205 if (color_depth & DP_DSC_12_BPC)
2206 dsc_bpc[num_bpc++] = 12;
2207 if (color_depth & DP_DSC_10_BPC)
2208 dsc_bpc[num_bpc++] = 10;
2209 if (color_depth & DP_DSC_8_BPC)
2210 dsc_bpc[num_bpc++] = 8;
2214 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2217 * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
2218 * @aux: DisplayPort AUX channel
2219 * @caps: buffer to return the capability info in
2221 * Read capabilities common to all LTTPRs.
2223 * Returns 0 on success or a negative error code on failure.
2225 int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
2226 u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2230 ret = drm_dp_dpcd_read(aux,
2231 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
2232 caps, DP_LTTPR_COMMON_CAP_SIZE);
2236 WARN_ON(ret != DP_LTTPR_COMMON_CAP_SIZE);
2240 EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
2243 * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
2244 * @aux: DisplayPort AUX channel
2245 * @dp_phy: LTTPR PHY to read the capabilities for
2246 * @caps: buffer to return the capability info in
2248 * Read the capabilities for the given LTTPR PHY.
2250 * Returns 0 on success or a negative error code on failure.
2252 int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
2253 enum drm_dp_phy dp_phy,
2254 u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2258 ret = drm_dp_dpcd_read(aux,
2259 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
2260 caps, DP_LTTPR_PHY_CAP_SIZE);
2264 WARN_ON(ret != DP_LTTPR_PHY_CAP_SIZE);
2268 EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
2270 static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
2272 return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
2276 * drm_dp_lttpr_count - get the number of detected LTTPRs
2277 * @caps: LTTPR common capabilities
2279 * Get the number of detected LTTPRs from the LTTPR common capabilities info.
2282 * -ERANGE if more than supported number (8) of LTTPRs are detected
2283 * -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
2284 * otherwise the number of detected LTTPRs
2286 int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2288 u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
2290 switch (hweight8(count)) {
2294 return 8 - ilog2(count);
2301 EXPORT_SYMBOL(drm_dp_lttpr_count);
2304 * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
2305 * @caps: LTTPR common capabilities
2307 * Returns the maximum link rate supported by all detected LTTPRs.
2309 int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2311 u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
2313 return drm_dp_bw_code_to_link_rate(rate);
2315 EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
2318 * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
2319 * @caps: LTTPR common capabilities
2321 * Returns the maximum lane count supported by all detected LTTPRs.
2323 int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2325 u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
2327 return max_lanes & DP_MAX_LANE_COUNT_MASK;
2329 EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
2332 * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
2333 * @caps: LTTPR PHY capabilities
2335 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2336 * voltage swing level 3.
2339 drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2341 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2343 return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
2345 EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
2348 * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
2349 * @caps: LTTPR PHY capabilities
2351 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2352 * pre-emphasis level 3.
2355 drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2357 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2359 return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
2361 EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
2364 * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2365 * @aux: DisplayPort AUX channel
2366 * @data: DP phy compliance test parameters.
2368 * Returns 0 on success or a negative error code on failure.
2370 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2371 struct drm_dp_phy_test_params *data)
2376 err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2379 data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2381 err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2384 data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2386 if (lanes & DP_ENHANCED_FRAME_CAP)
2387 data->enhanced_frame_cap = true;
2389 err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2393 switch (data->phy_pattern) {
2394 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2395 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2396 &data->custom80, sizeof(data->custom80));
2401 case DP_PHY_TEST_PATTERN_CP2520:
2402 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2404 sizeof(data->hbr2_reset));
2411 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2414 * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2415 * @aux: DisplayPort AUX channel
2416 * @data: DP phy compliance test parameters.
2417 * @dp_rev: DP revision to use for compliance testing
2419 * Returns 0 on success or a negative error code on failure.
2421 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2422 struct drm_dp_phy_test_params *data, u8 dp_rev)
2428 link_config[0] = drm_dp_link_rate_to_bw_code(data->link_rate);
2429 link_config[1] = data->num_lanes;
2430 if (data->enhanced_frame_cap)
2431 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
2432 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, link_config, 2);
2436 test_pattern = data->phy_pattern;
2437 if (dp_rev < 0x12) {
2438 test_pattern = (test_pattern << 2) &
2439 DP_LINK_QUAL_PATTERN_11_MASK;
2440 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2445 for (i = 0; i < data->num_lanes; i++) {
2446 err = drm_dp_dpcd_writeb(aux,
2447 DP_LINK_QUAL_LANE0_SET + i,
2456 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2458 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2460 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2463 switch (pixelformat) {
2464 case DP_PIXELFORMAT_RGB:
2466 case DP_PIXELFORMAT_YUV444:
2468 case DP_PIXELFORMAT_YUV422:
2470 case DP_PIXELFORMAT_YUV420:
2472 case DP_PIXELFORMAT_Y_ONLY:
2474 case DP_PIXELFORMAT_RAW:
2481 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2482 enum dp_colorimetry colorimetry)
2484 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2487 switch (colorimetry) {
2488 case DP_COLORIMETRY_DEFAULT:
2489 switch (pixelformat) {
2490 case DP_PIXELFORMAT_RGB:
2492 case DP_PIXELFORMAT_YUV444:
2493 case DP_PIXELFORMAT_YUV422:
2494 case DP_PIXELFORMAT_YUV420:
2496 case DP_PIXELFORMAT_Y_ONLY:
2497 return "DICOM PS3.14";
2498 case DP_PIXELFORMAT_RAW:
2499 return "Custom Color Profile";
2503 case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2504 switch (pixelformat) {
2505 case DP_PIXELFORMAT_RGB:
2506 return "Wide Fixed";
2507 case DP_PIXELFORMAT_YUV444:
2508 case DP_PIXELFORMAT_YUV422:
2509 case DP_PIXELFORMAT_YUV420:
2514 case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2515 switch (pixelformat) {
2516 case DP_PIXELFORMAT_RGB:
2517 return "Wide Float";
2518 case DP_PIXELFORMAT_YUV444:
2519 case DP_PIXELFORMAT_YUV422:
2520 case DP_PIXELFORMAT_YUV420:
2525 case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2526 switch (pixelformat) {
2527 case DP_PIXELFORMAT_RGB:
2529 case DP_PIXELFORMAT_YUV444:
2530 case DP_PIXELFORMAT_YUV422:
2531 case DP_PIXELFORMAT_YUV420:
2536 case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2537 switch (pixelformat) {
2538 case DP_PIXELFORMAT_RGB:
2540 case DP_PIXELFORMAT_YUV444:
2541 case DP_PIXELFORMAT_YUV422:
2542 case DP_PIXELFORMAT_YUV420:
2547 case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2548 switch (pixelformat) {
2549 case DP_PIXELFORMAT_RGB:
2550 return "Custom Profile";
2551 case DP_PIXELFORMAT_YUV444:
2552 case DP_PIXELFORMAT_YUV422:
2553 case DP_PIXELFORMAT_YUV420:
2558 case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2559 switch (pixelformat) {
2560 case DP_PIXELFORMAT_RGB:
2561 return "BT.2020 RGB";
2562 case DP_PIXELFORMAT_YUV444:
2563 case DP_PIXELFORMAT_YUV422:
2564 case DP_PIXELFORMAT_YUV420:
2565 return "BT.2020 CYCC";
2569 case DP_COLORIMETRY_BT2020_YCC:
2570 switch (pixelformat) {
2571 case DP_PIXELFORMAT_YUV444:
2572 case DP_PIXELFORMAT_YUV422:
2573 case DP_PIXELFORMAT_YUV420:
2574 return "BT.2020 YCC";
2583 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2585 switch (dynamic_range) {
2586 case DP_DYNAMIC_RANGE_VESA:
2587 return "VESA range";
2588 case DP_DYNAMIC_RANGE_CTA:
2595 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2597 switch (content_type) {
2598 case DP_CONTENT_TYPE_NOT_DEFINED:
2599 return "Not defined";
2600 case DP_CONTENT_TYPE_GRAPHICS:
2602 case DP_CONTENT_TYPE_PHOTO:
2604 case DP_CONTENT_TYPE_VIDEO:
2606 case DP_CONTENT_TYPE_GAME:
2613 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2614 const struct drm_dp_vsc_sdp *vsc)
2616 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2617 DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2618 vsc->revision, vsc->length);
2619 DP_SDP_LOG(" pixelformat: %s\n",
2620 dp_pixelformat_get_name(vsc->pixelformat));
2621 DP_SDP_LOG(" colorimetry: %s\n",
2622 dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2623 DP_SDP_LOG(" bpc: %u\n", vsc->bpc);
2624 DP_SDP_LOG(" dynamic range: %s\n",
2625 dp_dynamic_range_get_name(vsc->dynamic_range));
2626 DP_SDP_LOG(" content type: %s\n",
2627 dp_content_type_get_name(vsc->content_type));
2630 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
2633 * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
2634 * @dpcd: DisplayPort configuration data
2635 * @port_cap: port capabilities
2637 * Returns maximum frl bandwidth supported by PCON in GBPS,
2638 * returns 0 if not supported.
2640 int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
2641 const u8 port_cap[4])
2647 bw = buf & DP_PCON_MAX_FRL_BW;
2650 case DP_PCON_MAX_9GBPS:
2652 case DP_PCON_MAX_18GBPS:
2654 case DP_PCON_MAX_24GBPS:
2656 case DP_PCON_MAX_32GBPS:
2658 case DP_PCON_MAX_40GBPS:
2660 case DP_PCON_MAX_48GBPS:
2662 case DP_PCON_MAX_0GBPS:
2669 EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
2672 * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
2673 * @aux: DisplayPort AUX channel
2675 * Returns 0 if success, else returns negative error code.
2677 int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
2680 u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
2681 DP_PCON_ENABLE_LINK_FRL_MODE;
2683 if (enable_frl_ready_hpd)
2684 buf |= DP_PCON_ENABLE_HPD_READY;
2686 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2690 EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
2693 * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
2694 * @aux: DisplayPort AUX channel
2696 * Returns true if success, else returns false.
2698 bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
2703 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2707 if (buf & DP_PCON_FRL_READY)
2712 EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
2715 * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
2716 * @aux: DisplayPort AUX channel
2717 * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
2718 * @concurrent_mode: true if concurrent mode or operation is required,
2721 * Returns 0 if success, else returns negative error code.
2724 int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
2725 bool concurrent_mode)
2730 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2734 if (concurrent_mode)
2735 buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
2737 buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
2739 switch (max_frl_gbps) {
2741 buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;
2744 buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;
2747 buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;
2750 buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;
2753 buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;
2756 buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;
2759 buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;
2765 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2771 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
2774 * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
2775 * @aux: DisplayPort AUX channel
2776 * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
2777 * @extended_train_mode : true for Extended Mode, false for Normal Mode.
2778 * In Normal mode, the PCON tries each frl bw from the max_frl_mask starting
2779 * from min, and stops when link training is successful. In Extended mode, all
2780 * frl bw selected in the mask are trained by the PCON.
2782 * Returns 0 if success, else returns negative error code.
2784 int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
2785 bool extended_train_mode)
2788 u8 buf = max_frl_mask;
2790 if (extended_train_mode)
2791 buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2793 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
2799 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
2802 * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
2803 * @aux: DisplayPort AUX channel
2805 * Returns 0 if success, else returns negative error code.
2807 int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
2811 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
2817 EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
2820 * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
2821 * @aux: DisplayPort AUX channel
2823 * Returns 0 if success, else returns negative error code.
2825 int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
2830 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2833 if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
2834 DRM_DEBUG_KMS("PCON in Autonomous mode, can't enable FRL\n");
2837 buf |= DP_PCON_ENABLE_HDMI_LINK;
2838 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2844 EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
2847 * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
2848 * @aux: DisplayPort AUX channel
2850 * Returns true if link is active else returns false.
2852 bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
2857 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2861 return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
2863 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
2866 * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
2867 * @aux: DisplayPort AUX channel
2868 * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
2869 * Valid only if the MODE returned is FRL. For Normal Link training mode
2870 * only 1 of the bits will be set, but in case of Extended mode, more than
2871 * one bits can be set.
2873 * Returns the link mode : TMDS or FRL on success, else returns negative error
2876 int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
2882 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
2886 mode = buf & DP_PCON_HDMI_LINK_MODE;
2888 if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
2889 *frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
2893 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
2896 * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
2897 * during link failure between PCON and HDMI sink
2898 * @aux: DisplayPort AUX channel
2899 * @connector: DRM connector
2903 void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
2904 struct drm_connector *connector)
2906 u8 buf, error_count;
2908 struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
2910 for (i = 0; i < hdmi->max_lanes; i++) {
2911 if (drm_dp_dpcd_readb(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
2914 error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
2915 switch (error_count) {
2916 case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
2919 case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
2922 case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
2929 DRM_ERROR("More than %d errors since the last read for lane %d", num_error, i);
2932 EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
2935 * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
2936 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2938 * Returns true is PCON encoder is DSC 1.2 else returns false.
2940 bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2943 u8 major_v, minor_v;
2945 buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
2946 major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
2947 minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
2949 if (major_v == 1 && minor_v == 2)
2954 EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
2957 * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
2958 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2960 * Returns maximum no. of slices supported by the PCON DSC Encoder.
2962 int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2964 u8 slice_cap1, slice_cap2;
2966 slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
2967 slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
2969 if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
2971 if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
2973 if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
2975 if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
2977 if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
2979 if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
2981 if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
2983 if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
2985 if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
2987 if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
2992 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
2995 * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
2996 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2998 * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
3000 int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3004 buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
3006 return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
3008 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
3011 * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
3012 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3014 * Returns the bpp precision supported by the PCON encoder.
3016 int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3020 buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
3022 switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
3023 case DP_PCON_DSC_ONE_16TH_BPP:
3025 case DP_PCON_DSC_ONE_8TH_BPP:
3027 case DP_PCON_DSC_ONE_4TH_BPP:
3029 case DP_PCON_DSC_ONE_HALF_BPP:
3031 case DP_PCON_DSC_ONE_BPP:
3037 EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
3040 int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
3045 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3049 buf |= DP_PCON_ENABLE_DSC_ENCODER;
3051 if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
3052 buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
3053 buf |= pps_buf_config << 2;
3056 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3064 * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
3065 * for DSC1.2 between PCON & HDMI2.1 sink
3066 * @aux: DisplayPort AUX channel
3068 * Returns 0 on success, else returns negative error code.
3070 int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
3074 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
3080 EXPORT_SYMBOL(drm_dp_pcon_pps_default);
3083 * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
3085 * @aux: DisplayPort AUX channel
3086 * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
3088 * Returns 0 on success, else returns negative error code.
3090 int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
3094 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
3098 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3104 EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
3107 * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
3108 * override registers
3109 * @aux: DisplayPort AUX channel
3110 * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
3113 * Returns 0 on success, else returns negative error code.
3115 int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
3119 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
3122 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
3125 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
3129 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3135 EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
3138 * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
3139 * @aux: displayPort AUX channel
3140 * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
3142 * Returns 0 on success, else returns negative error code.
3144 int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
3149 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3153 if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
3154 buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
3156 buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
3158 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3164 EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);