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 void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
155 unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
156 DP_TRAINING_AUX_RD_MASK;
159 DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
162 if (rd_interval == 0)
165 rd_interval *= 4 * USEC_PER_MSEC;
167 usleep_range(rd_interval, rd_interval * 2);
169 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
171 u8 drm_dp_link_rate_to_bw_code(int link_rate)
173 /* Spec says link_bw = link_rate / 0.27Gbps */
174 return link_rate / 27000;
176 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
178 int drm_dp_bw_code_to_link_rate(u8 link_bw)
180 /* Spec says link_rate = link_bw * 0.27Gbps */
181 return link_bw * 27000;
183 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
185 #define AUX_RETRY_INTERVAL 500 /* us */
188 drm_dp_dump_access(const struct drm_dp_aux *aux,
189 u8 request, uint offset, void *buffer, int ret)
191 const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
194 DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
195 aux->name, offset, arrow, ret, min(ret, 20), buffer);
197 DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d)\n",
198 aux->name, offset, arrow, ret);
204 * The DisplayPort AUX channel is an abstraction to allow generic, driver-
205 * independent access to AUX functionality. Drivers can take advantage of
206 * this by filling in the fields of the drm_dp_aux structure.
208 * Transactions are described using a hardware-independent drm_dp_aux_msg
209 * structure, which is passed into a driver's .transfer() implementation.
210 * Both native and I2C-over-AUX transactions are supported.
213 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
214 unsigned int offset, void *buffer, size_t size)
216 struct drm_dp_aux_msg msg;
217 unsigned int retry, native_reply;
218 int err = 0, ret = 0;
220 memset(&msg, 0, sizeof(msg));
221 msg.address = offset;
222 msg.request = request;
226 mutex_lock(&aux->hw_mutex);
229 * The specification doesn't give any recommendation on how often to
230 * retry native transactions. We used to retry 7 times like for
231 * aux i2c transactions but real world devices this wasn't
232 * sufficient, bump to 32 which makes Dell 4k monitors happier.
234 for (retry = 0; retry < 32; retry++) {
235 if (ret != 0 && ret != -ETIMEDOUT) {
236 usleep_range(AUX_RETRY_INTERVAL,
237 AUX_RETRY_INTERVAL + 100);
240 ret = aux->transfer(aux, &msg);
242 native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
243 if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
253 * We want the error we return to be the error we received on
254 * the first transaction, since we may get a different error the
261 DRM_DEBUG_KMS("%s: Too many retries, giving up. First error: %d\n",
266 mutex_unlock(&aux->hw_mutex);
271 * drm_dp_dpcd_read() - read a series of bytes from the DPCD
272 * @aux: DisplayPort AUX channel (SST or MST)
273 * @offset: address of the (first) register to read
274 * @buffer: buffer to store the register values
275 * @size: number of bytes in @buffer
277 * Returns the number of bytes transferred on success, or a negative error
278 * code on failure. -EIO is returned if the request was NAKed by the sink or
279 * if the retry count was exceeded. If not all bytes were transferred, this
280 * function returns -EPROTO. Errors from the underlying AUX channel transfer
281 * function, with the exception of -EBUSY (which causes the transaction to
282 * be retried), are propagated to the caller.
284 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
285 void *buffer, size_t size)
290 * HP ZR24w corrupts the first DPCD access after entering power save
291 * mode. Eg. on a read, the entire buffer will be filled with the same
292 * byte. Do a throw away read to avoid corrupting anything we care
293 * about. Afterwards things will work correctly until the monitor
294 * gets woken up and subsequently re-enters power save mode.
296 * The user pressing any button on the monitor is enough to wake it
297 * up, so there is no particularly good place to do the workaround.
298 * We just have to do it before any DPCD access and hope that the
299 * monitor doesn't power down exactly after the throw away read.
301 if (!aux->is_remote) {
302 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
309 ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
311 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
315 drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
318 EXPORT_SYMBOL(drm_dp_dpcd_read);
321 * drm_dp_dpcd_write() - write a series of bytes to the DPCD
322 * @aux: DisplayPort AUX channel (SST or MST)
323 * @offset: address of the (first) register to write
324 * @buffer: buffer containing the values to write
325 * @size: number of bytes in @buffer
327 * Returns the number of bytes transferred on success, or a negative error
328 * code on failure. -EIO is returned if the request was NAKed by the sink or
329 * if the retry count was exceeded. If not all bytes were transferred, this
330 * function returns -EPROTO. Errors from the underlying AUX channel transfer
331 * function, with the exception of -EBUSY (which causes the transaction to
332 * be retried), are propagated to the caller.
334 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
335 void *buffer, size_t size)
340 ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
342 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
345 drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
348 EXPORT_SYMBOL(drm_dp_dpcd_write);
351 * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
352 * @aux: DisplayPort AUX channel
353 * @status: buffer to store the link status in (must be at least 6 bytes)
355 * Returns the number of bytes transferred on success or a negative error
358 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
359 u8 status[DP_LINK_STATUS_SIZE])
361 return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
362 DP_LINK_STATUS_SIZE);
364 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
366 static bool is_edid_digital_input_dp(const struct edid *edid)
368 return edid && edid->revision >= 4 &&
369 edid->input & DRM_EDID_INPUT_DIGITAL &&
370 (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
374 * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
375 * @dpcd: DisplayPort configuration data
376 * @port_cap: port capabilities
377 * @type: port type to be checked. Can be:
378 * %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
379 * %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
380 * %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
382 * Caveat: Only works with DPCD 1.1+ port caps.
384 * Returns: whether the downstream facing port matches the type.
386 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
387 const u8 port_cap[4], u8 type)
389 return drm_dp_is_branch(dpcd) &&
390 dpcd[DP_DPCD_REV] >= 0x11 &&
391 (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
393 EXPORT_SYMBOL(drm_dp_downstream_is_type);
396 * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
397 * @dpcd: DisplayPort configuration data
398 * @port_cap: port capabilities
401 * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
403 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
404 const u8 port_cap[4],
405 const struct edid *edid)
407 if (dpcd[DP_DPCD_REV] < 0x11) {
408 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
409 case DP_DWN_STRM_PORT_TYPE_TMDS:
416 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
417 case DP_DS_PORT_TYPE_DP_DUALMODE:
418 if (is_edid_digital_input_dp(edid))
421 case DP_DS_PORT_TYPE_DVI:
422 case DP_DS_PORT_TYPE_HDMI:
428 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
431 * drm_dp_send_real_edid_checksum() - send back real edid checksum value
432 * @aux: DisplayPort AUX channel
433 * @real_edid_checksum: real edid checksum for the last block
438 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
439 u8 real_edid_checksum)
441 u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
443 if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
444 &auto_test_req, 1) < 1) {
445 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
446 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
449 auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
451 if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
452 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
453 aux->name, DP_TEST_REQUEST);
456 link_edid_read &= DP_TEST_LINK_EDID_READ;
458 if (!auto_test_req || !link_edid_read) {
459 DRM_DEBUG_KMS("%s: Source DUT does not support TEST_EDID_READ\n",
464 if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
465 &auto_test_req, 1) < 1) {
466 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
467 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
471 /* send back checksum for the last edid extension block data */
472 if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
473 &real_edid_checksum, 1) < 1) {
474 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
475 aux->name, DP_TEST_EDID_CHECKSUM);
479 test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
480 if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
481 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
482 aux->name, DP_TEST_RESPONSE);
488 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
490 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
492 u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
494 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
500 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
501 u8 dpcd[DP_RECEIVER_CAP_SIZE])
507 * Prior to DP1.3 the bit represented by
508 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
509 * If it is set DP_DPCD_REV at 0000h could be at a value less than
510 * the true capability of the panel. The only way to check is to
511 * then compare 0000h and 2200h.
513 if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
514 DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
517 ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
521 if (ret != sizeof(dpcd_ext))
524 if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
525 DRM_DEBUG_KMS("%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
526 aux->name, dpcd[DP_DPCD_REV],
527 dpcd_ext[DP_DPCD_REV]);
531 if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
534 DRM_DEBUG_KMS("%s: Base DPCD: %*ph\n",
535 aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
537 memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
543 * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
545 * @aux: DisplayPort AUX channel
546 * @dpcd: Buffer to store the resulting DPCD in
548 * Attempts to read the base DPCD caps for @aux. Additionally, this function
549 * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
552 * Returns: %0 if the DPCD was read successfully, negative error code
555 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
556 u8 dpcd[DP_RECEIVER_CAP_SIZE])
560 ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
563 if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
566 ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
570 DRM_DEBUG_KMS("%s: DPCD: %*ph\n",
571 aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
575 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
578 * drm_dp_read_downstream_info() - read DPCD downstream port info if available
579 * @aux: DisplayPort AUX channel
580 * @dpcd: A cached copy of the port's DPCD
581 * @downstream_ports: buffer to store the downstream port info in
584 * drm_dp_downstream_max_clock()
585 * drm_dp_downstream_max_bpc()
587 * Returns: 0 if either the downstream port info was read successfully or
588 * there was no downstream info to read, or a negative error code otherwise.
590 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
591 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
592 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
597 memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
599 /* No downstream info to read */
600 if (!drm_dp_is_branch(dpcd) ||
601 dpcd[DP_DPCD_REV] < DP_DPCD_REV_10 ||
602 !(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
605 /* Some branches advertise having 0 downstream ports, despite also advertising they have a
606 * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
607 * some branches do it we need to handle it regardless.
609 len = drm_dp_downstream_port_count(dpcd);
613 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
616 ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
622 DRM_DEBUG_KMS("%s: DPCD DFP: %*ph\n",
623 aux->name, len, downstream_ports);
627 EXPORT_SYMBOL(drm_dp_read_downstream_info);
630 * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
631 * @dpcd: DisplayPort configuration data
632 * @port_cap: port capabilities
634 * Returns: Downstream facing port max dot clock in kHz on success,
635 * or 0 if max clock not defined
637 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
638 const u8 port_cap[4])
640 if (!drm_dp_is_branch(dpcd))
643 if (dpcd[DP_DPCD_REV] < 0x11)
646 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
647 case DP_DS_PORT_TYPE_VGA:
648 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
650 return port_cap[1] * 8000;
655 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
658 * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
659 * @dpcd: DisplayPort configuration data
660 * @port_cap: port capabilities
663 * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
664 * or 0 if max TMDS clock not defined
666 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
667 const u8 port_cap[4],
668 const struct edid *edid)
670 if (!drm_dp_is_branch(dpcd))
673 if (dpcd[DP_DPCD_REV] < 0x11) {
674 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
675 case DP_DWN_STRM_PORT_TYPE_TMDS:
682 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
683 case DP_DS_PORT_TYPE_DP_DUALMODE:
684 if (is_edid_digital_input_dp(edid))
687 * It's left up to the driver to check the
688 * DP dual mode adapter's max TMDS clock.
690 * Unfortunatley it looks like branch devices
691 * may not fordward that the DP dual mode i2c
692 * access so we just usually get i2c nak :(
695 case DP_DS_PORT_TYPE_HDMI:
697 * We should perhaps assume 165 MHz when detailed cap
698 * info is not available. But looks like many typical
699 * branch devices fall into that category and so we'd
700 * probably end up with users complaining that they can't
701 * get high resolution modes with their favorite dongle.
703 * So let's limit to 300 MHz instead since DPCD 1.4
704 * HDMI 2.0 DFPs are required to have the detailed cap
705 * info. So it's more likely we're dealing with a HDMI 1.4
706 * compatible* device here.
708 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
710 return port_cap[1] * 2500;
711 case DP_DS_PORT_TYPE_DVI:
712 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
714 /* FIXME what to do about DVI dual link? */
715 return port_cap[1] * 2500;
720 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
723 * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
724 * @dpcd: DisplayPort configuration data
725 * @port_cap: port capabilities
728 * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
729 * or 0 if max TMDS clock not defined
731 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
732 const u8 port_cap[4],
733 const struct edid *edid)
735 if (!drm_dp_is_branch(dpcd))
738 if (dpcd[DP_DPCD_REV] < 0x11) {
739 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
740 case DP_DWN_STRM_PORT_TYPE_TMDS:
747 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
748 case DP_DS_PORT_TYPE_DP_DUALMODE:
749 if (is_edid_digital_input_dp(edid))
752 case DP_DS_PORT_TYPE_DVI:
753 case DP_DS_PORT_TYPE_HDMI:
755 * Unclear whether the protocol converter could
756 * utilize pixel replication. Assume it won't.
763 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
766 * drm_dp_downstream_max_bpc() - extract downstream facing port max
768 * @dpcd: DisplayPort configuration data
769 * @port_cap: downstream facing port capabilities
772 * Returns: Max bpc on success or 0 if max bpc not defined
774 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
775 const u8 port_cap[4],
776 const struct edid *edid)
778 if (!drm_dp_is_branch(dpcd))
781 if (dpcd[DP_DPCD_REV] < 0x11) {
782 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
783 case DP_DWN_STRM_PORT_TYPE_DP:
790 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
791 case DP_DS_PORT_TYPE_DP:
793 case DP_DS_PORT_TYPE_DP_DUALMODE:
794 if (is_edid_digital_input_dp(edid))
797 case DP_DS_PORT_TYPE_HDMI:
798 case DP_DS_PORT_TYPE_DVI:
799 case DP_DS_PORT_TYPE_VGA:
800 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
803 switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
820 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
823 * drm_dp_downstream_420_passthrough() - determine downstream facing port
824 * YCbCr 4:2:0 pass-through capability
825 * @dpcd: DisplayPort configuration data
826 * @port_cap: downstream facing port capabilities
828 * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
830 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
831 const u8 port_cap[4])
833 if (!drm_dp_is_branch(dpcd))
836 if (dpcd[DP_DPCD_REV] < 0x13)
839 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
840 case DP_DS_PORT_TYPE_DP:
842 case DP_DS_PORT_TYPE_HDMI:
843 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
846 return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
851 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
854 * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
855 * YCbCr 4:4:4->4:2:0 conversion capability
856 * @dpcd: DisplayPort configuration data
857 * @port_cap: downstream facing port capabilities
859 * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
861 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
862 const u8 port_cap[4])
864 if (!drm_dp_is_branch(dpcd))
867 if (dpcd[DP_DPCD_REV] < 0x13)
870 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
871 case DP_DS_PORT_TYPE_HDMI:
872 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
875 return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
880 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
883 * drm_dp_downstream_mode() - return a mode for downstream facing port
885 * @dpcd: DisplayPort configuration data
886 * @port_cap: port capabilities
888 * Provides a suitable mode for downstream facing ports without EDID.
890 * Returns: A new drm_display_mode on success or NULL on failure
892 struct drm_display_mode *
893 drm_dp_downstream_mode(struct drm_device *dev,
894 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
895 const u8 port_cap[4])
900 if (!drm_dp_is_branch(dpcd))
903 if (dpcd[DP_DPCD_REV] < 0x11)
906 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
907 case DP_DS_PORT_TYPE_NON_EDID:
908 switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
909 case DP_DS_NON_EDID_720x480i_60:
912 case DP_DS_NON_EDID_720x480i_50:
915 case DP_DS_NON_EDID_1920x1080i_60:
918 case DP_DS_NON_EDID_1920x1080i_50:
921 case DP_DS_NON_EDID_1280x720_60:
924 case DP_DS_NON_EDID_1280x720_50:
930 return drm_display_mode_from_cea_vic(dev, vic);
935 EXPORT_SYMBOL(drm_dp_downstream_mode);
938 * drm_dp_downstream_id() - identify branch device
939 * @aux: DisplayPort AUX channel
940 * @id: DisplayPort branch device id
942 * Returns branch device id on success or NULL on failure
944 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
946 return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
948 EXPORT_SYMBOL(drm_dp_downstream_id);
951 * drm_dp_downstream_debug() - debug DP branch devices
952 * @m: pointer for debugfs file
953 * @dpcd: DisplayPort configuration data
954 * @port_cap: port capabilities
956 * @aux: DisplayPort AUX channel
959 void drm_dp_downstream_debug(struct seq_file *m,
960 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
961 const u8 port_cap[4],
962 const struct edid *edid,
963 struct drm_dp_aux *aux)
965 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
966 DP_DETAILED_CAP_INFO_AVAILABLE;
972 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
973 bool branch_device = drm_dp_is_branch(dpcd);
975 seq_printf(m, "\tDP branch device present: %s\n",
976 branch_device ? "yes" : "no");
982 case DP_DS_PORT_TYPE_DP:
983 seq_puts(m, "\t\tType: DisplayPort\n");
985 case DP_DS_PORT_TYPE_VGA:
986 seq_puts(m, "\t\tType: VGA\n");
988 case DP_DS_PORT_TYPE_DVI:
989 seq_puts(m, "\t\tType: DVI\n");
991 case DP_DS_PORT_TYPE_HDMI:
992 seq_puts(m, "\t\tType: HDMI\n");
994 case DP_DS_PORT_TYPE_NON_EDID:
995 seq_puts(m, "\t\tType: others without EDID support\n");
997 case DP_DS_PORT_TYPE_DP_DUALMODE:
998 seq_puts(m, "\t\tType: DP++\n");
1000 case DP_DS_PORT_TYPE_WIRELESS:
1001 seq_puts(m, "\t\tType: Wireless\n");
1004 seq_puts(m, "\t\tType: N/A\n");
1007 memset(id, 0, sizeof(id));
1008 drm_dp_downstream_id(aux, id);
1009 seq_printf(m, "\t\tID: %s\n", id);
1011 len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1013 seq_printf(m, "\t\tHW: %d.%d\n",
1014 (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1016 len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1018 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1020 if (detailed_cap_info) {
1021 clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1023 seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1025 clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1027 seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1029 clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1031 seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1033 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1036 seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1039 EXPORT_SYMBOL(drm_dp_downstream_debug);
1042 * drm_dp_subconnector_type() - get DP branch device type
1043 * @dpcd: DisplayPort configuration data
1044 * @port_cap: port capabilities
1046 enum drm_mode_subconnector
1047 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1048 const u8 port_cap[4])
1051 if (!drm_dp_is_branch(dpcd))
1052 return DRM_MODE_SUBCONNECTOR_Native;
1053 /* DP 1.0 approach */
1054 if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1055 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1056 DP_DWN_STRM_PORT_TYPE_MASK;
1059 case DP_DWN_STRM_PORT_TYPE_TMDS:
1060 /* Can be HDMI or DVI-D, DVI-D is a safer option */
1061 return DRM_MODE_SUBCONNECTOR_DVID;
1062 case DP_DWN_STRM_PORT_TYPE_ANALOG:
1063 /* Can be VGA or DVI-A, VGA is more popular */
1064 return DRM_MODE_SUBCONNECTOR_VGA;
1065 case DP_DWN_STRM_PORT_TYPE_DP:
1066 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1067 case DP_DWN_STRM_PORT_TYPE_OTHER:
1069 return DRM_MODE_SUBCONNECTOR_Unknown;
1072 type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1075 case DP_DS_PORT_TYPE_DP:
1076 case DP_DS_PORT_TYPE_DP_DUALMODE:
1077 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1078 case DP_DS_PORT_TYPE_VGA:
1079 return DRM_MODE_SUBCONNECTOR_VGA;
1080 case DP_DS_PORT_TYPE_DVI:
1081 return DRM_MODE_SUBCONNECTOR_DVID;
1082 case DP_DS_PORT_TYPE_HDMI:
1083 return DRM_MODE_SUBCONNECTOR_HDMIA;
1084 case DP_DS_PORT_TYPE_WIRELESS:
1085 return DRM_MODE_SUBCONNECTOR_Wireless;
1086 case DP_DS_PORT_TYPE_NON_EDID:
1088 return DRM_MODE_SUBCONNECTOR_Unknown;
1091 EXPORT_SYMBOL(drm_dp_subconnector_type);
1094 * drm_mode_set_dp_subconnector_property - set subconnector for DP connector
1095 * @connector: connector to set property on
1096 * @status: connector status
1097 * @dpcd: DisplayPort configuration data
1098 * @port_cap: port capabilities
1100 * Called by a driver on every detect event.
1102 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1103 enum drm_connector_status status,
1105 const u8 port_cap[4])
1107 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1109 if (status == connector_status_connected)
1110 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1111 drm_object_property_set_value(&connector->base,
1112 connector->dev->mode_config.dp_subconnector_property,
1115 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1118 * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1120 * @connector: The DRM connector to check
1121 * @dpcd: A cached copy of the connector's DPCD RX capabilities
1122 * @desc: A cached copy of the connector's DP descriptor
1124 * See also: drm_dp_read_sink_count()
1126 * Returns: %True if the (e)DP connector has a valid sink count that should
1127 * be probed, %false otherwise.
1129 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1130 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1131 const struct drm_dp_desc *desc)
1133 /* Some eDP panels don't set a valid value for the sink count */
1134 return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1135 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1136 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1137 !drm_dp_has_quirk(desc, 0, DP_DPCD_QUIRK_NO_SINK_COUNT);
1139 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1142 * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1143 * @aux: The DP AUX channel to use
1145 * See also: drm_dp_read_sink_count_cap()
1147 * Returns: The current sink count reported by @aux, or a negative error code
1150 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1155 ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1161 return DP_GET_SINK_COUNT(count);
1163 EXPORT_SYMBOL(drm_dp_read_sink_count);
1166 * I2C-over-AUX implementation
1169 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1171 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1172 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1173 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1174 I2C_FUNC_10BIT_ADDR;
1177 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1180 * In case of i2c defer or short i2c ack reply to a write,
1181 * we need to switch to WRITE_STATUS_UPDATE to drain the
1182 * rest of the message
1184 if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1185 msg->request &= DP_AUX_I2C_MOT;
1186 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1190 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1191 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1192 #define AUX_STOP_LEN 4
1193 #define AUX_CMD_LEN 4
1194 #define AUX_ADDRESS_LEN 20
1195 #define AUX_REPLY_PAD_LEN 4
1196 #define AUX_LENGTH_LEN 8
1199 * Calculate the duration of the AUX request/reply in usec. Gives the
1200 * "best" case estimate, ie. successful while as short as possible.
1202 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1204 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1205 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1207 if ((msg->request & DP_AUX_I2C_READ) == 0)
1208 len += msg->size * 8;
1213 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1215 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1216 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1219 * For read we expect what was asked. For writes there will
1220 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1222 if (msg->request & DP_AUX_I2C_READ)
1223 len += msg->size * 8;
1228 #define I2C_START_LEN 1
1229 #define I2C_STOP_LEN 1
1230 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1231 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1234 * Calculate the length of the i2c transfer in usec, assuming
1235 * the i2c bus speed is as specified. Gives the the "worst"
1236 * case estimate, ie. successful while as long as possible.
1237 * Doesn't account the the "MOT" bit, and instead assumes each
1238 * message includes a START, ADDRESS and STOP. Neither does it
1239 * account for additional random variables such as clock stretching.
1241 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1244 /* AUX bitrate is 1MHz, i2c bitrate as specified */
1245 return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1246 msg->size * I2C_DATA_LEN +
1247 I2C_STOP_LEN) * 1000, i2c_speed_khz);
1251 * Deterine how many retries should be attempted to successfully transfer
1252 * the specified message, based on the estimated durations of the
1253 * i2c and AUX transfers.
1255 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1258 int aux_time_us = drm_dp_aux_req_duration(msg) +
1259 drm_dp_aux_reply_duration(msg);
1260 int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1262 return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1266 * FIXME currently assumes 10 kHz as some real world devices seem
1267 * to require it. We should query/set the speed via DPCD if supported.
1269 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1270 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1271 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1272 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1275 * Transfer a single I2C-over-AUX message and handle various error conditions,
1276 * retrying the transaction as appropriate. It is assumed that the
1277 * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1280 * Returns bytes transferred on success, or a negative error code on failure.
1282 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1284 unsigned int retry, defer_i2c;
1287 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1288 * is required to retry at least seven times upon receiving AUX_DEFER
1289 * before giving up the AUX transaction.
1291 * We also try to account for the i2c bus speed.
1293 int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1295 for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1296 ret = aux->transfer(aux, msg);
1302 * While timeouts can be errors, they're usually normal
1303 * behavior (for instance, when a driver tries to
1304 * communicate with a non-existant DisplayPort device).
1305 * Avoid spamming the kernel log with timeout errors.
1307 if (ret == -ETIMEDOUT)
1308 DRM_DEBUG_KMS_RATELIMITED("%s: transaction timed out\n",
1311 DRM_DEBUG_KMS("%s: transaction failed: %d\n",
1317 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1318 case DP_AUX_NATIVE_REPLY_ACK:
1320 * For I2C-over-AUX transactions this isn't enough, we
1321 * need to check for the I2C ACK reply.
1325 case DP_AUX_NATIVE_REPLY_NACK:
1326 DRM_DEBUG_KMS("%s: native nack (result=%d, size=%zu)\n",
1327 aux->name, ret, msg->size);
1330 case DP_AUX_NATIVE_REPLY_DEFER:
1331 DRM_DEBUG_KMS("%s: native defer\n", aux->name);
1333 * We could check for I2C bit rate capabilities and if
1334 * available adjust this interval. We could also be
1335 * more careful with DP-to-legacy adapters where a
1336 * long legacy cable may force very low I2C bit rates.
1338 * For now just defer for long enough to hopefully be
1339 * safe for all use-cases.
1341 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1345 DRM_ERROR("%s: invalid native reply %#04x\n",
1346 aux->name, msg->reply);
1350 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1351 case DP_AUX_I2C_REPLY_ACK:
1353 * Both native ACK and I2C ACK replies received. We
1354 * can assume the transfer was successful.
1356 if (ret != msg->size)
1357 drm_dp_i2c_msg_write_status_update(msg);
1360 case DP_AUX_I2C_REPLY_NACK:
1361 DRM_DEBUG_KMS("%s: I2C nack (result=%d, size=%zu)\n",
1362 aux->name, ret, msg->size);
1363 aux->i2c_nack_count++;
1366 case DP_AUX_I2C_REPLY_DEFER:
1367 DRM_DEBUG_KMS("%s: I2C defer\n", aux->name);
1368 /* DP Compliance Test 4.2.2.5 Requirement:
1369 * Must have at least 7 retries for I2C defers on the
1370 * transaction to pass this test
1372 aux->i2c_defer_count++;
1375 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1376 drm_dp_i2c_msg_write_status_update(msg);
1381 DRM_ERROR("%s: invalid I2C reply %#04x\n",
1382 aux->name, msg->reply);
1387 DRM_DEBUG_KMS("%s: Too many retries, giving up\n", aux->name);
1391 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1392 const struct i2c_msg *i2c_msg)
1394 msg->request = (i2c_msg->flags & I2C_M_RD) ?
1395 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1396 if (!(i2c_msg->flags & I2C_M_STOP))
1397 msg->request |= DP_AUX_I2C_MOT;
1401 * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1403 * Returns an error code on failure, or a recommended transfer size on success.
1405 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1407 int err, ret = orig_msg->size;
1408 struct drm_dp_aux_msg msg = *orig_msg;
1410 while (msg.size > 0) {
1411 err = drm_dp_i2c_do_msg(aux, &msg);
1413 return err == 0 ? -EPROTO : err;
1415 if (err < msg.size && err < ret) {
1416 DRM_DEBUG_KMS("%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1417 aux->name, msg.size, err);
1429 * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1430 * packets to be as large as possible. If not, the I2C transactions never
1431 * succeed. Hence the default is maximum.
1433 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1434 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1435 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1436 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1438 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1441 struct drm_dp_aux *aux = adapter->algo_data;
1443 unsigned transfer_size;
1444 struct drm_dp_aux_msg msg;
1447 dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1449 memset(&msg, 0, sizeof(msg));
1451 for (i = 0; i < num; i++) {
1452 msg.address = msgs[i].addr;
1453 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1454 /* Send a bare address packet to start the transaction.
1455 * Zero sized messages specify an address only (bare
1456 * address) transaction.
1460 err = drm_dp_i2c_do_msg(aux, &msg);
1463 * Reset msg.request in case in case it got
1464 * changed into a WRITE_STATUS_UPDATE.
1466 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1470 /* We want each transaction to be as large as possible, but
1471 * we'll go to smaller sizes if the hardware gives us a
1474 transfer_size = dp_aux_i2c_transfer_size;
1475 for (j = 0; j < msgs[i].len; j += msg.size) {
1476 msg.buffer = msgs[i].buf + j;
1477 msg.size = min(transfer_size, msgs[i].len - j);
1479 err = drm_dp_i2c_drain_msg(aux, &msg);
1482 * Reset msg.request in case in case it got
1483 * changed into a WRITE_STATUS_UPDATE.
1485 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1489 transfer_size = err;
1496 /* Send a bare address packet to close out the transaction.
1497 * Zero sized messages specify an address only (bare
1498 * address) transaction.
1500 msg.request &= ~DP_AUX_I2C_MOT;
1503 (void)drm_dp_i2c_do_msg(aux, &msg);
1508 static const struct i2c_algorithm drm_dp_i2c_algo = {
1509 .functionality = drm_dp_i2c_functionality,
1510 .master_xfer = drm_dp_i2c_xfer,
1513 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1515 return container_of(i2c, struct drm_dp_aux, ddc);
1518 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1520 mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1523 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1525 return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1528 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1530 mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1533 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1534 .lock_bus = lock_bus,
1535 .trylock_bus = trylock_bus,
1536 .unlock_bus = unlock_bus,
1539 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1544 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1548 WARN_ON(!(buf & DP_TEST_SINK_START));
1550 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1554 count = buf & DP_TEST_COUNT_MASK;
1555 if (count == aux->crc_count)
1556 return -EAGAIN; /* No CRC yet */
1558 aux->crc_count = count;
1561 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1562 * per component (RGB or CrYCb).
1564 ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1571 static void drm_dp_aux_crc_work(struct work_struct *work)
1573 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1575 struct drm_crtc *crtc;
1580 if (WARN_ON(!aux->crtc))
1584 while (crtc->crc.opened) {
1585 drm_crtc_wait_one_vblank(crtc);
1586 if (!crtc->crc.opened)
1589 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1590 if (ret == -EAGAIN) {
1591 usleep_range(1000, 2000);
1592 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1595 if (ret == -EAGAIN) {
1596 DRM_DEBUG_KMS("%s: Get CRC failed after retrying: %d\n",
1600 DRM_DEBUG_KMS("%s: Failed to get a CRC: %d\n",
1605 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1606 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1607 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1608 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1613 * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1614 * @aux: DisplayPort AUX channel
1616 * Used for remote aux channel in general. Merely initialize the crc work
1619 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1621 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1623 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1626 * drm_dp_aux_init() - minimally initialise an aux channel
1627 * @aux: DisplayPort AUX channel
1629 * If you need to use the drm_dp_aux's i2c adapter prior to registering it
1630 * with the outside world, call drm_dp_aux_init() first. You must still
1631 * call drm_dp_aux_register() once the connector has been registered to
1632 * allow userspace access to the auxiliary DP channel.
1634 void drm_dp_aux_init(struct drm_dp_aux *aux)
1636 mutex_init(&aux->hw_mutex);
1637 mutex_init(&aux->cec.lock);
1638 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1640 aux->ddc.algo = &drm_dp_i2c_algo;
1641 aux->ddc.algo_data = aux;
1642 aux->ddc.retries = 3;
1644 aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1646 EXPORT_SYMBOL(drm_dp_aux_init);
1649 * drm_dp_aux_register() - initialise and register aux channel
1650 * @aux: DisplayPort AUX channel
1652 * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
1653 * This should only be called when the underlying &struct drm_connector is
1654 * initialiazed already. Therefore the best place to call this is from
1655 * &drm_connector_funcs.late_register. Not that drivers which don't follow this
1656 * will Oops when CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1658 * Drivers which need to use the aux channel before that point (e.g. at driver
1659 * load time, before drm_dev_register() has been called) need to call
1660 * drm_dp_aux_init().
1662 * Returns 0 on success or a negative error code on failure.
1664 int drm_dp_aux_register(struct drm_dp_aux *aux)
1669 drm_dp_aux_init(aux);
1671 aux->ddc.class = I2C_CLASS_DDC;
1672 aux->ddc.owner = THIS_MODULE;
1673 aux->ddc.dev.parent = aux->dev;
1675 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1676 sizeof(aux->ddc.name));
1678 ret = drm_dp_aux_register_devnode(aux);
1682 ret = i2c_add_adapter(&aux->ddc);
1684 drm_dp_aux_unregister_devnode(aux);
1690 EXPORT_SYMBOL(drm_dp_aux_register);
1693 * drm_dp_aux_unregister() - unregister an AUX adapter
1694 * @aux: DisplayPort AUX channel
1696 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1698 drm_dp_aux_unregister_devnode(aux);
1699 i2c_del_adapter(&aux->ddc);
1701 EXPORT_SYMBOL(drm_dp_aux_unregister);
1703 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1706 * drm_dp_psr_setup_time() - PSR setup in time usec
1707 * @psr_cap: PSR capabilities from DPCD
1710 * PSR setup time for the panel in microseconds, negative
1711 * error code on failure.
1713 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1715 static const u16 psr_setup_time_us[] = {
1716 PSR_SETUP_TIME(330),
1717 PSR_SETUP_TIME(275),
1718 PSR_SETUP_TIME(220),
1719 PSR_SETUP_TIME(165),
1720 PSR_SETUP_TIME(110),
1726 i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1727 if (i >= ARRAY_SIZE(psr_setup_time_us))
1730 return psr_setup_time_us[i];
1732 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1734 #undef PSR_SETUP_TIME
1737 * drm_dp_start_crc() - start capture of frame CRCs
1738 * @aux: DisplayPort AUX channel
1739 * @crtc: CRTC displaying the frames whose CRCs are to be captured
1741 * Returns 0 on success or a negative error code on failure.
1743 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1748 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1752 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1758 schedule_work(&aux->crc_work);
1762 EXPORT_SYMBOL(drm_dp_start_crc);
1765 * drm_dp_stop_crc() - stop capture of frame CRCs
1766 * @aux: DisplayPort AUX channel
1768 * Returns 0 on success or a negative error code on failure.
1770 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1775 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1779 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1783 flush_work(&aux->crc_work);
1788 EXPORT_SYMBOL(drm_dp_stop_crc);
1797 #define OUI(first, second, third) { (first), (second), (third) }
1798 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1799 { (first), (second), (third), (fourth), (fifth), (sixth) }
1801 #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
1803 static const struct dpcd_quirk dpcd_quirk_list[] = {
1804 /* Analogix 7737 needs reduced M and N at HBR2 link rates */
1805 { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1806 /* LG LP140WF6-SPM1 eDP panel */
1807 { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1808 /* Apple panels need some additional handling to support PSR */
1809 { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1810 /* CH7511 seems to leave SINK_COUNT zeroed */
1811 { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1812 /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1813 { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1814 /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1815 { 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) },
1821 * Get a bit mask of DPCD quirks for the sink/branch device identified by
1822 * ident. The quirk data is shared but it's up to the drivers to act on the
1825 * For now, only the OUI (first three bytes) is used, but this may be extended
1826 * to device identification string and hardware/firmware revisions later.
1829 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1831 const struct dpcd_quirk *quirk;
1834 u8 any_device[] = DEVICE_ID_ANY;
1836 for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1837 quirk = &dpcd_quirk_list[i];
1839 if (quirk->is_branch != is_branch)
1842 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1845 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
1846 memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
1849 quirks |= quirk->quirks;
1855 #undef DEVICE_ID_ANY
1864 #define MFG(first, second) { (first), (second) }
1865 #define PROD_ID(first, second) { (first), (second) }
1868 * Some devices have unreliable OUIDs where they don't set the device ID
1869 * correctly, and as a result we need to use the EDID for finding additional
1870 * DP quirks in such cases.
1872 static const struct edid_quirk edid_quirk_list[] = {
1873 /* Optional 4K AMOLED panel in the ThinkPad X1 Extreme 2nd Generation
1874 * only supports DPCD backlight controls
1876 { MFG(0x4c, 0x83), PROD_ID(0x41, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1878 * Some Dell CML 2020 systems have panels support both AUX and PWM
1879 * backlight control, and some only support AUX backlight control. All
1880 * said panels start up in AUX mode by default, and we don't have any
1881 * support for disabling HDR mode on these panels which would be
1882 * required to switch to PWM backlight control mode (plus, I'm not
1883 * even sure we want PWM backlight controls over DPCD backlight
1884 * controls anyway...). Until we have a better way of detecting these,
1885 * force DPCD backlight mode on all of them.
1887 { MFG(0x06, 0xaf), PROD_ID(0x9b, 0x32), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1888 { MFG(0x06, 0xaf), PROD_ID(0xeb, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1889 { MFG(0x4d, 0x10), PROD_ID(0xc7, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1890 { MFG(0x4d, 0x10), PROD_ID(0xe6, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1891 { MFG(0x4c, 0x83), PROD_ID(0x47, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1898 * drm_dp_get_edid_quirks() - Check the EDID of a DP device to find additional
1899 * DP-specific quirks
1900 * @edid: The EDID to check
1902 * While OUIDs are meant to be used to recognize a DisplayPort device, a lot
1903 * of manufacturers don't seem to like following standards and neglect to fill
1904 * the dev-ID in, making it impossible to only use OUIDs for determining
1905 * quirks in some cases. This function can be used to check the EDID and look
1906 * up any additional DP quirks. The bits returned by this function correspond
1907 * to the quirk bits in &drm_dp_quirk.
1909 * Returns: a bitmask of quirks, if any. The driver can check this using
1910 * drm_dp_has_quirk().
1912 u32 drm_dp_get_edid_quirks(const struct edid *edid)
1914 const struct edid_quirk *quirk;
1921 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
1922 quirk = &edid_quirk_list[i];
1923 if (memcmp(quirk->mfg_id, edid->mfg_id,
1924 sizeof(edid->mfg_id)) == 0 &&
1925 memcmp(quirk->prod_id, edid->prod_code,
1926 sizeof(edid->prod_code)) == 0)
1927 quirks |= quirk->quirks;
1930 DRM_DEBUG_KMS("DP sink: EDID mfg %*phD prod-ID %*phD quirks: 0x%04x\n",
1931 (int)sizeof(edid->mfg_id), edid->mfg_id,
1932 (int)sizeof(edid->prod_code), edid->prod_code, quirks);
1936 EXPORT_SYMBOL(drm_dp_get_edid_quirks);
1939 * drm_dp_read_desc - read sink/branch descriptor from DPCD
1940 * @aux: DisplayPort AUX channel
1941 * @desc: Device descriptor to fill from DPCD
1942 * @is_branch: true for branch devices, false for sink devices
1944 * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
1947 * Returns 0 on success or a negative error code on failure.
1949 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
1952 struct drm_dp_dpcd_ident *ident = &desc->ident;
1953 unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
1954 int ret, dev_id_len;
1956 ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
1960 desc->quirks = drm_dp_get_quirks(ident, is_branch);
1962 dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
1964 DRM_DEBUG_KMS("%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
1965 aux->name, is_branch ? "branch" : "sink",
1966 (int)sizeof(ident->oui), ident->oui,
1967 dev_id_len, ident->device_id,
1968 ident->hw_rev >> 4, ident->hw_rev & 0xf,
1969 ident->sw_major_rev, ident->sw_minor_rev,
1974 EXPORT_SYMBOL(drm_dp_read_desc);
1977 * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
1978 * supported by the DSC sink.
1979 * @dsc_dpcd: DSC capabilities from DPCD
1980 * @is_edp: true if its eDP, false for DP
1982 * Read the slice capabilities DPCD register from DSC sink to get
1983 * the maximum slice count supported. This is used to populate
1984 * the DSC parameters in the &struct drm_dsc_config by the driver.
1985 * Driver creates an infoframe using these parameters to populate
1986 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
1987 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
1990 * Maximum slice count supported by DSC sink or 0 its invalid
1992 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
1995 u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
1998 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
1999 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2001 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2003 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2006 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2007 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2009 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2011 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2013 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2015 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2017 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2019 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2021 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2023 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2025 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2027 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2033 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2036 * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2037 * @dsc_dpcd: DSC capabilities from DPCD
2039 * Read the DSC DPCD register to parse the line buffer depth in bits which is
2040 * number of bits of precision within the decoder line buffer supported by
2041 * the DSC sink. This is used to populate the DSC parameters in the
2042 * &struct drm_dsc_config by the driver.
2043 * Driver creates an infoframe using these parameters to populate
2044 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2045 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2048 * Line buffer depth supported by DSC panel or 0 its invalid
2050 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2052 u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2054 switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2055 case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2057 case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2059 case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2061 case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2063 case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2065 case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2067 case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2069 case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2071 case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2077 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2080 * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2081 * values supported by the DSC sink.
2082 * @dsc_dpcd: DSC capabilities from DPCD
2083 * @dsc_bpc: An array to be filled by this helper with supported
2086 * Read the DSC DPCD from the sink device to parse the supported bits per
2087 * component values. This is used to populate the DSC parameters
2088 * in the &struct drm_dsc_config by the driver.
2089 * Driver creates an infoframe using these parameters to populate
2090 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2091 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2094 * Number of input BPC values parsed from the DPCD
2096 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2100 u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2102 if (color_depth & DP_DSC_12_BPC)
2103 dsc_bpc[num_bpc++] = 12;
2104 if (color_depth & DP_DSC_10_BPC)
2105 dsc_bpc[num_bpc++] = 10;
2106 if (color_depth & DP_DSC_8_BPC)
2107 dsc_bpc[num_bpc++] = 8;
2111 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2114 * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2115 * @aux: DisplayPort AUX channel
2116 * @data: DP phy compliance test parameters.
2118 * Returns 0 on success or a negative error code on failure.
2120 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2121 struct drm_dp_phy_test_params *data)
2126 err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2129 data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2131 err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2134 data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2136 if (lanes & DP_ENHANCED_FRAME_CAP)
2137 data->enhanced_frame_cap = true;
2139 err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2143 switch (data->phy_pattern) {
2144 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2145 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2146 &data->custom80, sizeof(data->custom80));
2151 case DP_PHY_TEST_PATTERN_CP2520:
2152 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2154 sizeof(data->hbr2_reset));
2161 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2164 * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2165 * @aux: DisplayPort AUX channel
2166 * @data: DP phy compliance test parameters.
2167 * @dp_rev: DP revision to use for compliance testing
2169 * Returns 0 on success or a negative error code on failure.
2171 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2172 struct drm_dp_phy_test_params *data, u8 dp_rev)
2177 test_pattern = data->phy_pattern;
2178 if (dp_rev < 0x12) {
2179 test_pattern = (test_pattern << 2) &
2180 DP_LINK_QUAL_PATTERN_11_MASK;
2181 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2186 for (i = 0; i < data->num_lanes; i++) {
2187 err = drm_dp_dpcd_writeb(aux,
2188 DP_LINK_QUAL_LANE0_SET + i,
2197 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2199 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2201 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2204 switch (pixelformat) {
2205 case DP_PIXELFORMAT_RGB:
2207 case DP_PIXELFORMAT_YUV444:
2209 case DP_PIXELFORMAT_YUV422:
2211 case DP_PIXELFORMAT_YUV420:
2213 case DP_PIXELFORMAT_Y_ONLY:
2215 case DP_PIXELFORMAT_RAW:
2222 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2223 enum dp_colorimetry colorimetry)
2225 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2228 switch (colorimetry) {
2229 case DP_COLORIMETRY_DEFAULT:
2230 switch (pixelformat) {
2231 case DP_PIXELFORMAT_RGB:
2233 case DP_PIXELFORMAT_YUV444:
2234 case DP_PIXELFORMAT_YUV422:
2235 case DP_PIXELFORMAT_YUV420:
2237 case DP_PIXELFORMAT_Y_ONLY:
2238 return "DICOM PS3.14";
2239 case DP_PIXELFORMAT_RAW:
2240 return "Custom Color Profile";
2244 case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2245 switch (pixelformat) {
2246 case DP_PIXELFORMAT_RGB:
2247 return "Wide Fixed";
2248 case DP_PIXELFORMAT_YUV444:
2249 case DP_PIXELFORMAT_YUV422:
2250 case DP_PIXELFORMAT_YUV420:
2255 case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2256 switch (pixelformat) {
2257 case DP_PIXELFORMAT_RGB:
2258 return "Wide Float";
2259 case DP_PIXELFORMAT_YUV444:
2260 case DP_PIXELFORMAT_YUV422:
2261 case DP_PIXELFORMAT_YUV420:
2266 case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2267 switch (pixelformat) {
2268 case DP_PIXELFORMAT_RGB:
2270 case DP_PIXELFORMAT_YUV444:
2271 case DP_PIXELFORMAT_YUV422:
2272 case DP_PIXELFORMAT_YUV420:
2277 case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2278 switch (pixelformat) {
2279 case DP_PIXELFORMAT_RGB:
2281 case DP_PIXELFORMAT_YUV444:
2282 case DP_PIXELFORMAT_YUV422:
2283 case DP_PIXELFORMAT_YUV420:
2288 case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2289 switch (pixelformat) {
2290 case DP_PIXELFORMAT_RGB:
2291 return "Custom Profile";
2292 case DP_PIXELFORMAT_YUV444:
2293 case DP_PIXELFORMAT_YUV422:
2294 case DP_PIXELFORMAT_YUV420:
2299 case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2300 switch (pixelformat) {
2301 case DP_PIXELFORMAT_RGB:
2302 return "BT.2020 RGB";
2303 case DP_PIXELFORMAT_YUV444:
2304 case DP_PIXELFORMAT_YUV422:
2305 case DP_PIXELFORMAT_YUV420:
2306 return "BT.2020 CYCC";
2310 case DP_COLORIMETRY_BT2020_YCC:
2311 switch (pixelformat) {
2312 case DP_PIXELFORMAT_YUV444:
2313 case DP_PIXELFORMAT_YUV422:
2314 case DP_PIXELFORMAT_YUV420:
2315 return "BT.2020 YCC";
2324 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2326 switch (dynamic_range) {
2327 case DP_DYNAMIC_RANGE_VESA:
2328 return "VESA range";
2329 case DP_DYNAMIC_RANGE_CTA:
2336 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2338 switch (content_type) {
2339 case DP_CONTENT_TYPE_NOT_DEFINED:
2340 return "Not defined";
2341 case DP_CONTENT_TYPE_GRAPHICS:
2343 case DP_CONTENT_TYPE_PHOTO:
2345 case DP_CONTENT_TYPE_VIDEO:
2347 case DP_CONTENT_TYPE_GAME:
2354 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2355 const struct drm_dp_vsc_sdp *vsc)
2357 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2358 DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2359 vsc->revision, vsc->length);
2360 DP_SDP_LOG(" pixelformat: %s\n",
2361 dp_pixelformat_get_name(vsc->pixelformat));
2362 DP_SDP_LOG(" colorimetry: %s\n",
2363 dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2364 DP_SDP_LOG(" bpc: %u\n", vsc->bpc);
2365 DP_SDP_LOG(" dynamic range: %s\n",
2366 dp_dynamic_range_get_name(vsc->dynamic_range));
2367 DP_SDP_LOG(" content type: %s\n",
2368 dp_content_type_get_name(vsc->content_type));
2371 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);