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);
367 * drm_dp_send_real_edid_checksum() - send back real edid checksum value
368 * @aux: DisplayPort AUX channel
369 * @real_edid_checksum: real edid checksum for the last block
374 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
375 u8 real_edid_checksum)
377 u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
379 if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
380 &auto_test_req, 1) < 1) {
381 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
382 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
385 auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
387 if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
388 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
389 aux->name, DP_TEST_REQUEST);
392 link_edid_read &= DP_TEST_LINK_EDID_READ;
394 if (!auto_test_req || !link_edid_read) {
395 DRM_DEBUG_KMS("%s: Source DUT does not support TEST_EDID_READ\n",
400 if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
401 &auto_test_req, 1) < 1) {
402 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
403 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
407 /* send back checksum for the last edid extension block data */
408 if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
409 &real_edid_checksum, 1) < 1) {
410 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
411 aux->name, DP_TEST_EDID_CHECKSUM);
415 test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
416 if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
417 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
418 aux->name, DP_TEST_RESPONSE);
424 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
426 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
428 u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
430 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
437 * drm_dp_read_downstream_info() - read DPCD downstream port info if available
438 * @aux: DisplayPort AUX channel
439 * @dpcd: A cached copy of the port's DPCD
440 * @downstream_ports: buffer to store the downstream port info in
443 * drm_dp_downstream_max_clock()
444 * drm_dp_downstream_max_bpc()
446 * Returns: 0 if either the downstream port info was read successfully or
447 * there was no downstream info to read, or a negative error code otherwise.
449 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
450 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
451 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
456 memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
458 /* No downstream info to read */
459 if (!drm_dp_is_branch(dpcd) ||
460 dpcd[DP_DPCD_REV] < DP_DPCD_REV_10 ||
461 !(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
464 len = drm_dp_downstream_port_count(dpcd);
465 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
468 ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
472 return ret == len ? 0 : -EIO;
474 EXPORT_SYMBOL(drm_dp_read_downstream_info);
477 * drm_dp_downstream_max_clock() - extract branch device max
478 * pixel rate for legacy VGA
479 * converter or max TMDS clock
481 * @dpcd: DisplayPort configuration data
482 * @port_cap: port capabilities
485 * drm_dp_read_downstream_info()
486 * drm_dp_downstream_max_bpc()
488 * Returns: Max clock in kHz on success or 0 if max clock not defined
490 int drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
491 const u8 port_cap[4])
493 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
494 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
495 DP_DETAILED_CAP_INFO_AVAILABLE;
497 if (!detailed_cap_info)
501 case DP_DS_PORT_TYPE_VGA:
502 return port_cap[1] * 8 * 1000;
503 case DP_DS_PORT_TYPE_DVI:
504 case DP_DS_PORT_TYPE_HDMI:
505 case DP_DS_PORT_TYPE_DP_DUALMODE:
506 return port_cap[1] * 2500;
511 EXPORT_SYMBOL(drm_dp_downstream_max_clock);
514 * drm_dp_downstream_max_bpc() - extract branch device max
516 * @dpcd: DisplayPort configuration data
517 * @port_cap: port capabilities
520 * drm_dp_read_downstream_info()
521 * drm_dp_downstream_max_clock()
523 * Returns: Max bpc on success or 0 if max bpc not defined
525 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
526 const u8 port_cap[4])
528 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
529 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
530 DP_DETAILED_CAP_INFO_AVAILABLE;
533 if (!detailed_cap_info)
537 case DP_DS_PORT_TYPE_VGA:
538 case DP_DS_PORT_TYPE_DVI:
539 case DP_DS_PORT_TYPE_HDMI:
540 case DP_DS_PORT_TYPE_DP_DUALMODE:
541 bpc = port_cap[2] & DP_DS_MAX_BPC_MASK;
558 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
561 * drm_dp_downstream_id() - identify branch device
562 * @aux: DisplayPort AUX channel
563 * @id: DisplayPort branch device id
565 * Returns branch device id on success or NULL on failure
567 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
569 return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
571 EXPORT_SYMBOL(drm_dp_downstream_id);
574 * drm_dp_downstream_debug() - debug DP branch devices
575 * @m: pointer for debugfs file
576 * @dpcd: DisplayPort configuration data
577 * @port_cap: port capabilities
578 * @aux: DisplayPort AUX channel
581 void drm_dp_downstream_debug(struct seq_file *m,
582 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
583 const u8 port_cap[4], struct drm_dp_aux *aux)
585 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
586 DP_DETAILED_CAP_INFO_AVAILABLE;
592 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
593 bool branch_device = drm_dp_is_branch(dpcd);
595 seq_printf(m, "\tDP branch device present: %s\n",
596 branch_device ? "yes" : "no");
602 case DP_DS_PORT_TYPE_DP:
603 seq_puts(m, "\t\tType: DisplayPort\n");
605 case DP_DS_PORT_TYPE_VGA:
606 seq_puts(m, "\t\tType: VGA\n");
608 case DP_DS_PORT_TYPE_DVI:
609 seq_puts(m, "\t\tType: DVI\n");
611 case DP_DS_PORT_TYPE_HDMI:
612 seq_puts(m, "\t\tType: HDMI\n");
614 case DP_DS_PORT_TYPE_NON_EDID:
615 seq_puts(m, "\t\tType: others without EDID support\n");
617 case DP_DS_PORT_TYPE_DP_DUALMODE:
618 seq_puts(m, "\t\tType: DP++\n");
620 case DP_DS_PORT_TYPE_WIRELESS:
621 seq_puts(m, "\t\tType: Wireless\n");
624 seq_puts(m, "\t\tType: N/A\n");
627 memset(id, 0, sizeof(id));
628 drm_dp_downstream_id(aux, id);
629 seq_printf(m, "\t\tID: %s\n", id);
631 len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
633 seq_printf(m, "\t\tHW: %d.%d\n",
634 (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
636 len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
638 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
640 if (detailed_cap_info) {
641 clk = drm_dp_downstream_max_clock(dpcd, port_cap);
644 if (type == DP_DS_PORT_TYPE_VGA)
645 seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
647 seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
650 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap);
653 seq_printf(m, "\t\tMax bpc: %d\n", bpc);
656 EXPORT_SYMBOL(drm_dp_downstream_debug);
659 * drm_dp_subconnector_type() - get DP branch device type
662 enum drm_mode_subconnector
663 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
664 const u8 port_cap[4])
667 if (!drm_dp_is_branch(dpcd))
668 return DRM_MODE_SUBCONNECTOR_Native;
669 /* DP 1.0 approach */
670 if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
671 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
672 DP_DWN_STRM_PORT_TYPE_MASK;
675 case DP_DWN_STRM_PORT_TYPE_TMDS:
676 /* Can be HDMI or DVI-D, DVI-D is a safer option */
677 return DRM_MODE_SUBCONNECTOR_DVID;
678 case DP_DWN_STRM_PORT_TYPE_ANALOG:
679 /* Can be VGA or DVI-A, VGA is more popular */
680 return DRM_MODE_SUBCONNECTOR_VGA;
681 case DP_DWN_STRM_PORT_TYPE_DP:
682 return DRM_MODE_SUBCONNECTOR_DisplayPort;
683 case DP_DWN_STRM_PORT_TYPE_OTHER:
685 return DRM_MODE_SUBCONNECTOR_Unknown;
688 type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
691 case DP_DS_PORT_TYPE_DP:
692 case DP_DS_PORT_TYPE_DP_DUALMODE:
693 return DRM_MODE_SUBCONNECTOR_DisplayPort;
694 case DP_DS_PORT_TYPE_VGA:
695 return DRM_MODE_SUBCONNECTOR_VGA;
696 case DP_DS_PORT_TYPE_DVI:
697 return DRM_MODE_SUBCONNECTOR_DVID;
698 case DP_DS_PORT_TYPE_HDMI:
699 return DRM_MODE_SUBCONNECTOR_HDMIA;
700 case DP_DS_PORT_TYPE_WIRELESS:
701 return DRM_MODE_SUBCONNECTOR_Wireless;
702 case DP_DS_PORT_TYPE_NON_EDID:
704 return DRM_MODE_SUBCONNECTOR_Unknown;
707 EXPORT_SYMBOL(drm_dp_subconnector_type);
710 * drm_mode_set_dp_subconnector_property - set subconnector for DP connector
712 * Called by a driver on every detect event.
714 void drm_dp_set_subconnector_property(struct drm_connector *connector,
715 enum drm_connector_status status,
717 const u8 port_cap[4])
719 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
721 if (status == connector_status_connected)
722 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
723 drm_object_property_set_value(&connector->base,
724 connector->dev->mode_config.dp_subconnector_property,
727 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
730 * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
732 * @connector: The DRM connector to check
733 * @dpcd: A cached copy of the connector's DPCD RX capabilities
734 * @desc: A cached copy of the connector's DP descriptor
736 * See also: drm_dp_read_sink_count()
738 * Returns: %True if the (e)DP connector has a valid sink count that should
739 * be probed, %false otherwise.
741 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
742 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
743 const struct drm_dp_desc *desc)
745 /* Some eDP panels don't set a valid value for the sink count */
746 return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
747 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
748 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
749 !drm_dp_has_quirk(desc, 0, DP_DPCD_QUIRK_NO_SINK_COUNT);
751 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
754 * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
755 * @aux: The DP AUX channel to use
757 * See also: drm_dp_read_sink_count_cap()
759 * Returns: The current sink count reported by @aux, or a negative error code
762 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
767 ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
773 return DP_GET_SINK_COUNT(count);
775 EXPORT_SYMBOL(drm_dp_read_sink_count);
778 * I2C-over-AUX implementation
781 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
783 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
784 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
785 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
789 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
792 * In case of i2c defer or short i2c ack reply to a write,
793 * we need to switch to WRITE_STATUS_UPDATE to drain the
794 * rest of the message
796 if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
797 msg->request &= DP_AUX_I2C_MOT;
798 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
802 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
803 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
804 #define AUX_STOP_LEN 4
805 #define AUX_CMD_LEN 4
806 #define AUX_ADDRESS_LEN 20
807 #define AUX_REPLY_PAD_LEN 4
808 #define AUX_LENGTH_LEN 8
811 * Calculate the duration of the AUX request/reply in usec. Gives the
812 * "best" case estimate, ie. successful while as short as possible.
814 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
816 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
817 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
819 if ((msg->request & DP_AUX_I2C_READ) == 0)
820 len += msg->size * 8;
825 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
827 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
828 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
831 * For read we expect what was asked. For writes there will
832 * be 0 or 1 data bytes. Assume 0 for the "best" case.
834 if (msg->request & DP_AUX_I2C_READ)
835 len += msg->size * 8;
840 #define I2C_START_LEN 1
841 #define I2C_STOP_LEN 1
842 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
843 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
846 * Calculate the length of the i2c transfer in usec, assuming
847 * the i2c bus speed is as specified. Gives the the "worst"
848 * case estimate, ie. successful while as long as possible.
849 * Doesn't account the the "MOT" bit, and instead assumes each
850 * message includes a START, ADDRESS and STOP. Neither does it
851 * account for additional random variables such as clock stretching.
853 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
856 /* AUX bitrate is 1MHz, i2c bitrate as specified */
857 return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
858 msg->size * I2C_DATA_LEN +
859 I2C_STOP_LEN) * 1000, i2c_speed_khz);
863 * Deterine how many retries should be attempted to successfully transfer
864 * the specified message, based on the estimated durations of the
865 * i2c and AUX transfers.
867 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
870 int aux_time_us = drm_dp_aux_req_duration(msg) +
871 drm_dp_aux_reply_duration(msg);
872 int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
874 return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
878 * FIXME currently assumes 10 kHz as some real world devices seem
879 * to require it. We should query/set the speed via DPCD if supported.
881 static int dp_aux_i2c_speed_khz __read_mostly = 10;
882 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
883 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
884 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
887 * Transfer a single I2C-over-AUX message and handle various error conditions,
888 * retrying the transaction as appropriate. It is assumed that the
889 * &drm_dp_aux.transfer function does not modify anything in the msg other than the
892 * Returns bytes transferred on success, or a negative error code on failure.
894 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
896 unsigned int retry, defer_i2c;
899 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
900 * is required to retry at least seven times upon receiving AUX_DEFER
901 * before giving up the AUX transaction.
903 * We also try to account for the i2c bus speed.
905 int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
907 for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
908 ret = aux->transfer(aux, msg);
914 * While timeouts can be errors, they're usually normal
915 * behavior (for instance, when a driver tries to
916 * communicate with a non-existant DisplayPort device).
917 * Avoid spamming the kernel log with timeout errors.
919 if (ret == -ETIMEDOUT)
920 DRM_DEBUG_KMS_RATELIMITED("%s: transaction timed out\n",
923 DRM_DEBUG_KMS("%s: transaction failed: %d\n",
929 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
930 case DP_AUX_NATIVE_REPLY_ACK:
932 * For I2C-over-AUX transactions this isn't enough, we
933 * need to check for the I2C ACK reply.
937 case DP_AUX_NATIVE_REPLY_NACK:
938 DRM_DEBUG_KMS("%s: native nack (result=%d, size=%zu)\n",
939 aux->name, ret, msg->size);
942 case DP_AUX_NATIVE_REPLY_DEFER:
943 DRM_DEBUG_KMS("%s: native defer\n", aux->name);
945 * We could check for I2C bit rate capabilities and if
946 * available adjust this interval. We could also be
947 * more careful with DP-to-legacy adapters where a
948 * long legacy cable may force very low I2C bit rates.
950 * For now just defer for long enough to hopefully be
951 * safe for all use-cases.
953 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
957 DRM_ERROR("%s: invalid native reply %#04x\n",
958 aux->name, msg->reply);
962 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
963 case DP_AUX_I2C_REPLY_ACK:
965 * Both native ACK and I2C ACK replies received. We
966 * can assume the transfer was successful.
968 if (ret != msg->size)
969 drm_dp_i2c_msg_write_status_update(msg);
972 case DP_AUX_I2C_REPLY_NACK:
973 DRM_DEBUG_KMS("%s: I2C nack (result=%d, size=%zu)\n",
974 aux->name, ret, msg->size);
975 aux->i2c_nack_count++;
978 case DP_AUX_I2C_REPLY_DEFER:
979 DRM_DEBUG_KMS("%s: I2C defer\n", aux->name);
980 /* DP Compliance Test 4.2.2.5 Requirement:
981 * Must have at least 7 retries for I2C defers on the
982 * transaction to pass this test
984 aux->i2c_defer_count++;
987 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
988 drm_dp_i2c_msg_write_status_update(msg);
993 DRM_ERROR("%s: invalid I2C reply %#04x\n",
994 aux->name, msg->reply);
999 DRM_DEBUG_KMS("%s: Too many retries, giving up\n", aux->name);
1003 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1004 const struct i2c_msg *i2c_msg)
1006 msg->request = (i2c_msg->flags & I2C_M_RD) ?
1007 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1008 if (!(i2c_msg->flags & I2C_M_STOP))
1009 msg->request |= DP_AUX_I2C_MOT;
1013 * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1015 * Returns an error code on failure, or a recommended transfer size on success.
1017 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1019 int err, ret = orig_msg->size;
1020 struct drm_dp_aux_msg msg = *orig_msg;
1022 while (msg.size > 0) {
1023 err = drm_dp_i2c_do_msg(aux, &msg);
1025 return err == 0 ? -EPROTO : err;
1027 if (err < msg.size && err < ret) {
1028 DRM_DEBUG_KMS("%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1029 aux->name, msg.size, err);
1041 * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1042 * packets to be as large as possible. If not, the I2C transactions never
1043 * succeed. Hence the default is maximum.
1045 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1046 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1047 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1048 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1050 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1053 struct drm_dp_aux *aux = adapter->algo_data;
1055 unsigned transfer_size;
1056 struct drm_dp_aux_msg msg;
1059 dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1061 memset(&msg, 0, sizeof(msg));
1063 for (i = 0; i < num; i++) {
1064 msg.address = msgs[i].addr;
1065 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1066 /* Send a bare address packet to start the transaction.
1067 * Zero sized messages specify an address only (bare
1068 * address) transaction.
1072 err = drm_dp_i2c_do_msg(aux, &msg);
1075 * Reset msg.request in case in case it got
1076 * changed into a WRITE_STATUS_UPDATE.
1078 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1082 /* We want each transaction to be as large as possible, but
1083 * we'll go to smaller sizes if the hardware gives us a
1086 transfer_size = dp_aux_i2c_transfer_size;
1087 for (j = 0; j < msgs[i].len; j += msg.size) {
1088 msg.buffer = msgs[i].buf + j;
1089 msg.size = min(transfer_size, msgs[i].len - j);
1091 err = drm_dp_i2c_drain_msg(aux, &msg);
1094 * Reset msg.request in case in case it got
1095 * changed into a WRITE_STATUS_UPDATE.
1097 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1101 transfer_size = err;
1108 /* Send a bare address packet to close out the transaction.
1109 * Zero sized messages specify an address only (bare
1110 * address) transaction.
1112 msg.request &= ~DP_AUX_I2C_MOT;
1115 (void)drm_dp_i2c_do_msg(aux, &msg);
1120 static const struct i2c_algorithm drm_dp_i2c_algo = {
1121 .functionality = drm_dp_i2c_functionality,
1122 .master_xfer = drm_dp_i2c_xfer,
1125 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1127 return container_of(i2c, struct drm_dp_aux, ddc);
1130 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1132 mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1135 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1137 return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1140 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1142 mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1145 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1146 .lock_bus = lock_bus,
1147 .trylock_bus = trylock_bus,
1148 .unlock_bus = unlock_bus,
1151 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1156 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1160 WARN_ON(!(buf & DP_TEST_SINK_START));
1162 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1166 count = buf & DP_TEST_COUNT_MASK;
1167 if (count == aux->crc_count)
1168 return -EAGAIN; /* No CRC yet */
1170 aux->crc_count = count;
1173 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1174 * per component (RGB or CrYCb).
1176 ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1183 static void drm_dp_aux_crc_work(struct work_struct *work)
1185 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1187 struct drm_crtc *crtc;
1192 if (WARN_ON(!aux->crtc))
1196 while (crtc->crc.opened) {
1197 drm_crtc_wait_one_vblank(crtc);
1198 if (!crtc->crc.opened)
1201 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1202 if (ret == -EAGAIN) {
1203 usleep_range(1000, 2000);
1204 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1207 if (ret == -EAGAIN) {
1208 DRM_DEBUG_KMS("%s: Get CRC failed after retrying: %d\n",
1212 DRM_DEBUG_KMS("%s: Failed to get a CRC: %d\n",
1217 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1218 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1219 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1220 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1225 * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1226 * @aux: DisplayPort AUX channel
1228 * Used for remote aux channel in general. Merely initialize the crc work
1231 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1233 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1235 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1238 * drm_dp_aux_init() - minimally initialise an aux channel
1239 * @aux: DisplayPort AUX channel
1241 * If you need to use the drm_dp_aux's i2c adapter prior to registering it
1242 * with the outside world, call drm_dp_aux_init() first. You must still
1243 * call drm_dp_aux_register() once the connector has been registered to
1244 * allow userspace access to the auxiliary DP channel.
1246 void drm_dp_aux_init(struct drm_dp_aux *aux)
1248 mutex_init(&aux->hw_mutex);
1249 mutex_init(&aux->cec.lock);
1250 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1252 aux->ddc.algo = &drm_dp_i2c_algo;
1253 aux->ddc.algo_data = aux;
1254 aux->ddc.retries = 3;
1256 aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1258 EXPORT_SYMBOL(drm_dp_aux_init);
1261 * drm_dp_aux_register() - initialise and register aux channel
1262 * @aux: DisplayPort AUX channel
1264 * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
1265 * This should only be called when the underlying &struct drm_connector is
1266 * initialiazed already. Therefore the best place to call this is from
1267 * &drm_connector_funcs.late_register. Not that drivers which don't follow this
1268 * will Oops when CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1270 * Drivers which need to use the aux channel before that point (e.g. at driver
1271 * load time, before drm_dev_register() has been called) need to call
1272 * drm_dp_aux_init().
1274 * Returns 0 on success or a negative error code on failure.
1276 int drm_dp_aux_register(struct drm_dp_aux *aux)
1281 drm_dp_aux_init(aux);
1283 aux->ddc.class = I2C_CLASS_DDC;
1284 aux->ddc.owner = THIS_MODULE;
1285 aux->ddc.dev.parent = aux->dev;
1287 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1288 sizeof(aux->ddc.name));
1290 ret = drm_dp_aux_register_devnode(aux);
1294 ret = i2c_add_adapter(&aux->ddc);
1296 drm_dp_aux_unregister_devnode(aux);
1302 EXPORT_SYMBOL(drm_dp_aux_register);
1305 * drm_dp_aux_unregister() - unregister an AUX adapter
1306 * @aux: DisplayPort AUX channel
1308 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1310 drm_dp_aux_unregister_devnode(aux);
1311 i2c_del_adapter(&aux->ddc);
1313 EXPORT_SYMBOL(drm_dp_aux_unregister);
1315 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1318 * drm_dp_psr_setup_time() - PSR setup in time usec
1319 * @psr_cap: PSR capabilities from DPCD
1322 * PSR setup time for the panel in microseconds, negative
1323 * error code on failure.
1325 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1327 static const u16 psr_setup_time_us[] = {
1328 PSR_SETUP_TIME(330),
1329 PSR_SETUP_TIME(275),
1330 PSR_SETUP_TIME(220),
1331 PSR_SETUP_TIME(165),
1332 PSR_SETUP_TIME(110),
1338 i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1339 if (i >= ARRAY_SIZE(psr_setup_time_us))
1342 return psr_setup_time_us[i];
1344 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1346 #undef PSR_SETUP_TIME
1349 * drm_dp_start_crc() - start capture of frame CRCs
1350 * @aux: DisplayPort AUX channel
1351 * @crtc: CRTC displaying the frames whose CRCs are to be captured
1353 * Returns 0 on success or a negative error code on failure.
1355 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1360 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1364 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1370 schedule_work(&aux->crc_work);
1374 EXPORT_SYMBOL(drm_dp_start_crc);
1377 * drm_dp_stop_crc() - stop capture of frame CRCs
1378 * @aux: DisplayPort AUX channel
1380 * Returns 0 on success or a negative error code on failure.
1382 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1387 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1391 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1395 flush_work(&aux->crc_work);
1400 EXPORT_SYMBOL(drm_dp_stop_crc);
1409 #define OUI(first, second, third) { (first), (second), (third) }
1410 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1411 { (first), (second), (third), (fourth), (fifth), (sixth) }
1413 #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
1415 static const struct dpcd_quirk dpcd_quirk_list[] = {
1416 /* Analogix 7737 needs reduced M and N at HBR2 link rates */
1417 { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1418 /* LG LP140WF6-SPM1 eDP panel */
1419 { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1420 /* Apple panels need some additional handling to support PSR */
1421 { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1422 /* CH7511 seems to leave SINK_COUNT zeroed */
1423 { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1424 /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1425 { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1426 /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1427 { 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) },
1433 * Get a bit mask of DPCD quirks for the sink/branch device identified by
1434 * ident. The quirk data is shared but it's up to the drivers to act on the
1437 * For now, only the OUI (first three bytes) is used, but this may be extended
1438 * to device identification string and hardware/firmware revisions later.
1441 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1443 const struct dpcd_quirk *quirk;
1446 u8 any_device[] = DEVICE_ID_ANY;
1448 for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1449 quirk = &dpcd_quirk_list[i];
1451 if (quirk->is_branch != is_branch)
1454 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1457 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
1458 memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
1461 quirks |= quirk->quirks;
1467 #undef DEVICE_ID_ANY
1476 #define MFG(first, second) { (first), (second) }
1477 #define PROD_ID(first, second) { (first), (second) }
1480 * Some devices have unreliable OUIDs where they don't set the device ID
1481 * correctly, and as a result we need to use the EDID for finding additional
1482 * DP quirks in such cases.
1484 static const struct edid_quirk edid_quirk_list[] = {
1485 /* Optional 4K AMOLED panel in the ThinkPad X1 Extreme 2nd Generation
1486 * only supports DPCD backlight controls
1488 { MFG(0x4c, 0x83), PROD_ID(0x41, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1490 * Some Dell CML 2020 systems have panels support both AUX and PWM
1491 * backlight control, and some only support AUX backlight control. All
1492 * said panels start up in AUX mode by default, and we don't have any
1493 * support for disabling HDR mode on these panels which would be
1494 * required to switch to PWM backlight control mode (plus, I'm not
1495 * even sure we want PWM backlight controls over DPCD backlight
1496 * controls anyway...). Until we have a better way of detecting these,
1497 * force DPCD backlight mode on all of them.
1499 { MFG(0x06, 0xaf), PROD_ID(0x9b, 0x32), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1500 { MFG(0x06, 0xaf), PROD_ID(0xeb, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1501 { MFG(0x4d, 0x10), PROD_ID(0xc7, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1502 { MFG(0x4d, 0x10), PROD_ID(0xe6, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1503 { MFG(0x4c, 0x83), PROD_ID(0x47, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1510 * drm_dp_get_edid_quirks() - Check the EDID of a DP device to find additional
1511 * DP-specific quirks
1512 * @edid: The EDID to check
1514 * While OUIDs are meant to be used to recognize a DisplayPort device, a lot
1515 * of manufacturers don't seem to like following standards and neglect to fill
1516 * the dev-ID in, making it impossible to only use OUIDs for determining
1517 * quirks in some cases. This function can be used to check the EDID and look
1518 * up any additional DP quirks. The bits returned by this function correspond
1519 * to the quirk bits in &drm_dp_quirk.
1521 * Returns: a bitmask of quirks, if any. The driver can check this using
1522 * drm_dp_has_quirk().
1524 u32 drm_dp_get_edid_quirks(const struct edid *edid)
1526 const struct edid_quirk *quirk;
1533 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
1534 quirk = &edid_quirk_list[i];
1535 if (memcmp(quirk->mfg_id, edid->mfg_id,
1536 sizeof(edid->mfg_id)) == 0 &&
1537 memcmp(quirk->prod_id, edid->prod_code,
1538 sizeof(edid->prod_code)) == 0)
1539 quirks |= quirk->quirks;
1542 DRM_DEBUG_KMS("DP sink: EDID mfg %*phD prod-ID %*phD quirks: 0x%04x\n",
1543 (int)sizeof(edid->mfg_id), edid->mfg_id,
1544 (int)sizeof(edid->prod_code), edid->prod_code, quirks);
1548 EXPORT_SYMBOL(drm_dp_get_edid_quirks);
1551 * drm_dp_read_desc - read sink/branch descriptor from DPCD
1552 * @aux: DisplayPort AUX channel
1553 * @desc: Device descriptor to fill from DPCD
1554 * @is_branch: true for branch devices, false for sink devices
1556 * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
1559 * Returns 0 on success or a negative error code on failure.
1561 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
1564 struct drm_dp_dpcd_ident *ident = &desc->ident;
1565 unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
1566 int ret, dev_id_len;
1568 ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
1572 desc->quirks = drm_dp_get_quirks(ident, is_branch);
1574 dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
1576 DRM_DEBUG_KMS("%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
1577 aux->name, is_branch ? "branch" : "sink",
1578 (int)sizeof(ident->oui), ident->oui,
1579 dev_id_len, ident->device_id,
1580 ident->hw_rev >> 4, ident->hw_rev & 0xf,
1581 ident->sw_major_rev, ident->sw_minor_rev,
1586 EXPORT_SYMBOL(drm_dp_read_desc);
1589 * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
1590 * supported by the DSC sink.
1591 * @dsc_dpcd: DSC capabilities from DPCD
1592 * @is_edp: true if its eDP, false for DP
1594 * Read the slice capabilities DPCD register from DSC sink to get
1595 * the maximum slice count supported. This is used to populate
1596 * the DSC parameters in the &struct drm_dsc_config by the driver.
1597 * Driver creates an infoframe using these parameters to populate
1598 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
1599 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
1602 * Maximum slice count supported by DSC sink or 0 its invalid
1604 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
1607 u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
1610 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
1611 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
1613 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
1615 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
1618 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
1619 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
1621 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
1623 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
1625 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
1627 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
1629 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
1631 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
1633 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
1635 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
1637 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
1639 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
1645 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
1648 * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
1649 * @dsc_dpcd: DSC capabilities from DPCD
1651 * Read the DSC DPCD register to parse the line buffer depth in bits which is
1652 * number of bits of precision within the decoder line buffer supported by
1653 * the DSC sink. This is used to populate the DSC parameters in the
1654 * &struct drm_dsc_config by the driver.
1655 * Driver creates an infoframe using these parameters to populate
1656 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
1657 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
1660 * Line buffer depth supported by DSC panel or 0 its invalid
1662 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
1664 u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
1666 switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
1667 case DP_DSC_LINE_BUF_BIT_DEPTH_9:
1669 case DP_DSC_LINE_BUF_BIT_DEPTH_10:
1671 case DP_DSC_LINE_BUF_BIT_DEPTH_11:
1673 case DP_DSC_LINE_BUF_BIT_DEPTH_12:
1675 case DP_DSC_LINE_BUF_BIT_DEPTH_13:
1677 case DP_DSC_LINE_BUF_BIT_DEPTH_14:
1679 case DP_DSC_LINE_BUF_BIT_DEPTH_15:
1681 case DP_DSC_LINE_BUF_BIT_DEPTH_16:
1683 case DP_DSC_LINE_BUF_BIT_DEPTH_8:
1689 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
1692 * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
1693 * values supported by the DSC sink.
1694 * @dsc_dpcd: DSC capabilities from DPCD
1695 * @dsc_bpc: An array to be filled by this helper with supported
1698 * Read the DSC DPCD from the sink device to parse the supported bits per
1699 * component values. This is used to populate the DSC parameters
1700 * in the &struct drm_dsc_config by the driver.
1701 * Driver creates an infoframe using these parameters to populate
1702 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
1703 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
1706 * Number of input BPC values parsed from the DPCD
1708 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
1712 u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
1714 if (color_depth & DP_DSC_12_BPC)
1715 dsc_bpc[num_bpc++] = 12;
1716 if (color_depth & DP_DSC_10_BPC)
1717 dsc_bpc[num_bpc++] = 10;
1718 if (color_depth & DP_DSC_8_BPC)
1719 dsc_bpc[num_bpc++] = 8;
1723 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
1726 * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
1727 * @aux: DisplayPort AUX channel
1728 * @data: DP phy compliance test parameters.
1730 * Returns 0 on success or a negative error code on failure.
1732 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
1733 struct drm_dp_phy_test_params *data)
1738 err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
1741 data->link_rate = drm_dp_bw_code_to_link_rate(rate);
1743 err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
1746 data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
1748 if (lanes & DP_ENHANCED_FRAME_CAP)
1749 data->enhanced_frame_cap = true;
1751 err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
1755 switch (data->phy_pattern) {
1756 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
1757 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
1758 &data->custom80, sizeof(data->custom80));
1763 case DP_PHY_TEST_PATTERN_CP2520:
1764 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
1766 sizeof(data->hbr2_reset));
1773 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
1776 * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
1777 * @aux: DisplayPort AUX channel
1778 * @data: DP phy compliance test parameters.
1779 * @dp_rev: DP revision to use for compliance testing
1781 * Returns 0 on success or a negative error code on failure.
1783 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
1784 struct drm_dp_phy_test_params *data, u8 dp_rev)
1790 link_config[0] = drm_dp_link_rate_to_bw_code(data->link_rate);
1791 link_config[1] = data->num_lanes;
1792 if (data->enhanced_frame_cap)
1793 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1794 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, link_config, 2);
1798 test_pattern = data->phy_pattern;
1799 if (dp_rev < 0x12) {
1800 test_pattern = (test_pattern << 2) &
1801 DP_LINK_QUAL_PATTERN_11_MASK;
1802 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
1807 for (i = 0; i < data->num_lanes; i++) {
1808 err = drm_dp_dpcd_writeb(aux,
1809 DP_LINK_QUAL_LANE0_SET + i,
1818 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
1820 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
1822 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
1825 switch (pixelformat) {
1826 case DP_PIXELFORMAT_RGB:
1828 case DP_PIXELFORMAT_YUV444:
1830 case DP_PIXELFORMAT_YUV422:
1832 case DP_PIXELFORMAT_YUV420:
1834 case DP_PIXELFORMAT_Y_ONLY:
1836 case DP_PIXELFORMAT_RAW:
1843 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
1844 enum dp_colorimetry colorimetry)
1846 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
1849 switch (colorimetry) {
1850 case DP_COLORIMETRY_DEFAULT:
1851 switch (pixelformat) {
1852 case DP_PIXELFORMAT_RGB:
1854 case DP_PIXELFORMAT_YUV444:
1855 case DP_PIXELFORMAT_YUV422:
1856 case DP_PIXELFORMAT_YUV420:
1858 case DP_PIXELFORMAT_Y_ONLY:
1859 return "DICOM PS3.14";
1860 case DP_PIXELFORMAT_RAW:
1861 return "Custom Color Profile";
1865 case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
1866 switch (pixelformat) {
1867 case DP_PIXELFORMAT_RGB:
1868 return "Wide Fixed";
1869 case DP_PIXELFORMAT_YUV444:
1870 case DP_PIXELFORMAT_YUV422:
1871 case DP_PIXELFORMAT_YUV420:
1876 case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
1877 switch (pixelformat) {
1878 case DP_PIXELFORMAT_RGB:
1879 return "Wide Float";
1880 case DP_PIXELFORMAT_YUV444:
1881 case DP_PIXELFORMAT_YUV422:
1882 case DP_PIXELFORMAT_YUV420:
1887 case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
1888 switch (pixelformat) {
1889 case DP_PIXELFORMAT_RGB:
1891 case DP_PIXELFORMAT_YUV444:
1892 case DP_PIXELFORMAT_YUV422:
1893 case DP_PIXELFORMAT_YUV420:
1898 case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
1899 switch (pixelformat) {
1900 case DP_PIXELFORMAT_RGB:
1902 case DP_PIXELFORMAT_YUV444:
1903 case DP_PIXELFORMAT_YUV422:
1904 case DP_PIXELFORMAT_YUV420:
1909 case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
1910 switch (pixelformat) {
1911 case DP_PIXELFORMAT_RGB:
1912 return "Custom Profile";
1913 case DP_PIXELFORMAT_YUV444:
1914 case DP_PIXELFORMAT_YUV422:
1915 case DP_PIXELFORMAT_YUV420:
1920 case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
1921 switch (pixelformat) {
1922 case DP_PIXELFORMAT_RGB:
1923 return "BT.2020 RGB";
1924 case DP_PIXELFORMAT_YUV444:
1925 case DP_PIXELFORMAT_YUV422:
1926 case DP_PIXELFORMAT_YUV420:
1927 return "BT.2020 CYCC";
1931 case DP_COLORIMETRY_BT2020_YCC:
1932 switch (pixelformat) {
1933 case DP_PIXELFORMAT_YUV444:
1934 case DP_PIXELFORMAT_YUV422:
1935 case DP_PIXELFORMAT_YUV420:
1936 return "BT.2020 YCC";
1945 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
1947 switch (dynamic_range) {
1948 case DP_DYNAMIC_RANGE_VESA:
1949 return "VESA range";
1950 case DP_DYNAMIC_RANGE_CTA:
1957 static const char *dp_content_type_get_name(enum dp_content_type content_type)
1959 switch (content_type) {
1960 case DP_CONTENT_TYPE_NOT_DEFINED:
1961 return "Not defined";
1962 case DP_CONTENT_TYPE_GRAPHICS:
1964 case DP_CONTENT_TYPE_PHOTO:
1966 case DP_CONTENT_TYPE_VIDEO:
1968 case DP_CONTENT_TYPE_GAME:
1975 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
1976 const struct drm_dp_vsc_sdp *vsc)
1978 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
1979 DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
1980 vsc->revision, vsc->length);
1981 DP_SDP_LOG(" pixelformat: %s\n",
1982 dp_pixelformat_get_name(vsc->pixelformat));
1983 DP_SDP_LOG(" colorimetry: %s\n",
1984 dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
1985 DP_SDP_LOG(" bpc: %u\n", vsc->bpc);
1986 DP_SDP_LOG(" dynamic range: %s\n",
1987 dp_dynamic_range_get_name(vsc->dynamic_range));
1988 DP_SDP_LOG(" content type: %s\n",
1989 dp_content_type_get_name(vsc->content_type));
1992 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);