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 struct drm_dp_aux *aux,
136 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
138 unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
139 DP_TRAINING_AUX_RD_MASK;
142 drm_dbg_kms(aux->drm_dev, "%s: AUX interval %lu, out of range (max 4)\n",
143 aux->name, rd_interval);
145 if (rd_interval == 0 || dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
148 rd_interval *= 4 * USEC_PER_MSEC;
150 usleep_range(rd_interval, rd_interval * 2);
152 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
154 static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
155 unsigned long rd_interval)
158 drm_dbg_kms(aux->drm_dev, "%s: AUX interval %lu, out of range (max 4)\n",
159 aux->name, rd_interval);
161 if (rd_interval == 0)
164 rd_interval *= 4 * USEC_PER_MSEC;
166 usleep_range(rd_interval, rd_interval * 2);
169 void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
170 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
172 __drm_dp_link_train_channel_eq_delay(aux,
173 dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
174 DP_TRAINING_AUX_RD_MASK);
176 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
178 void drm_dp_lttpr_link_train_clock_recovery_delay(void)
180 usleep_range(100, 200);
182 EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
184 static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
186 return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
189 void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
190 const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
192 u8 interval = dp_lttpr_phy_cap(phy_cap,
193 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
194 DP_TRAINING_AUX_RD_MASK;
196 __drm_dp_link_train_channel_eq_delay(aux, interval);
198 EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
200 u8 drm_dp_link_rate_to_bw_code(int link_rate)
202 /* Spec says link_bw = link_rate / 0.27Gbps */
203 return link_rate / 27000;
205 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
207 int drm_dp_bw_code_to_link_rate(u8 link_bw)
209 /* Spec says link_rate = link_bw * 0.27Gbps */
210 return link_bw * 27000;
212 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
214 #define AUX_RETRY_INTERVAL 500 /* us */
217 drm_dp_dump_access(const struct drm_dp_aux *aux,
218 u8 request, uint offset, void *buffer, int ret)
220 const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
223 drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
224 aux->name, offset, arrow, ret, min(ret, 20), buffer);
226 drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",
227 aux->name, offset, arrow, ret);
233 * The DisplayPort AUX channel is an abstraction to allow generic, driver-
234 * independent access to AUX functionality. Drivers can take advantage of
235 * this by filling in the fields of the drm_dp_aux structure.
237 * Transactions are described using a hardware-independent drm_dp_aux_msg
238 * structure, which is passed into a driver's .transfer() implementation.
239 * Both native and I2C-over-AUX transactions are supported.
242 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
243 unsigned int offset, void *buffer, size_t size)
245 struct drm_dp_aux_msg msg;
246 unsigned int retry, native_reply;
247 int err = 0, ret = 0;
249 memset(&msg, 0, sizeof(msg));
250 msg.address = offset;
251 msg.request = request;
255 mutex_lock(&aux->hw_mutex);
258 * The specification doesn't give any recommendation on how often to
259 * retry native transactions. We used to retry 7 times like for
260 * aux i2c transactions but real world devices this wasn't
261 * sufficient, bump to 32 which makes Dell 4k monitors happier.
263 for (retry = 0; retry < 32; retry++) {
264 if (ret != 0 && ret != -ETIMEDOUT) {
265 usleep_range(AUX_RETRY_INTERVAL,
266 AUX_RETRY_INTERVAL + 100);
269 ret = aux->transfer(aux, &msg);
271 native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
272 if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
282 * We want the error we return to be the error we received on
283 * the first transaction, since we may get a different error the
290 drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",
295 mutex_unlock(&aux->hw_mutex);
300 * drm_dp_dpcd_read() - read a series of bytes from the DPCD
301 * @aux: DisplayPort AUX channel (SST or MST)
302 * @offset: address of the (first) register to read
303 * @buffer: buffer to store the register values
304 * @size: number of bytes in @buffer
306 * Returns the number of bytes transferred on success, or a negative error
307 * code on failure. -EIO is returned if the request was NAKed by the sink or
308 * if the retry count was exceeded. If not all bytes were transferred, this
309 * function returns -EPROTO. Errors from the underlying AUX channel transfer
310 * function, with the exception of -EBUSY (which causes the transaction to
311 * be retried), are propagated to the caller.
313 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
314 void *buffer, size_t size)
319 * HP ZR24w corrupts the first DPCD access after entering power save
320 * mode. Eg. on a read, the entire buffer will be filled with the same
321 * byte. Do a throw away read to avoid corrupting anything we care
322 * about. Afterwards things will work correctly until the monitor
323 * gets woken up and subsequently re-enters power save mode.
325 * The user pressing any button on the monitor is enough to wake it
326 * up, so there is no particularly good place to do the workaround.
327 * We just have to do it before any DPCD access and hope that the
328 * monitor doesn't power down exactly after the throw away read.
330 if (!aux->is_remote) {
331 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
338 ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
340 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
344 drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
347 EXPORT_SYMBOL(drm_dp_dpcd_read);
350 * drm_dp_dpcd_write() - write a series of bytes to the DPCD
351 * @aux: DisplayPort AUX channel (SST or MST)
352 * @offset: address of the (first) register to write
353 * @buffer: buffer containing the values to write
354 * @size: number of bytes in @buffer
356 * Returns the number of bytes transferred on success, or a negative error
357 * code on failure. -EIO is returned if the request was NAKed by the sink or
358 * if the retry count was exceeded. If not all bytes were transferred, this
359 * function returns -EPROTO. Errors from the underlying AUX channel transfer
360 * function, with the exception of -EBUSY (which causes the transaction to
361 * be retried), are propagated to the caller.
363 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
364 void *buffer, size_t size)
369 ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
371 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
374 drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
377 EXPORT_SYMBOL(drm_dp_dpcd_write);
380 * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
381 * @aux: DisplayPort AUX channel
382 * @status: buffer to store the link status in (must be at least 6 bytes)
384 * Returns the number of bytes transferred on success or a negative error
387 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
388 u8 status[DP_LINK_STATUS_SIZE])
390 return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
391 DP_LINK_STATUS_SIZE);
393 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
396 * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
397 * @aux: DisplayPort AUX channel
398 * @dp_phy: the DP PHY to get the link status for
399 * @link_status: buffer to return the status in
401 * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
402 * layout of the returned @link_status matches the DPCD register layout of the
403 * DPRX PHY link status.
405 * Returns 0 if the information was read successfully or a negative error code
408 int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
409 enum drm_dp_phy dp_phy,
410 u8 link_status[DP_LINK_STATUS_SIZE])
414 if (dp_phy == DP_PHY_DPRX) {
415 ret = drm_dp_dpcd_read(aux,
418 DP_LINK_STATUS_SIZE);
423 WARN_ON(ret != DP_LINK_STATUS_SIZE);
428 ret = drm_dp_dpcd_read(aux,
429 DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
431 DP_LINK_STATUS_SIZE - 1);
436 WARN_ON(ret != DP_LINK_STATUS_SIZE - 1);
438 /* Convert the LTTPR to the sink PHY link status layout */
439 memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
440 &link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
441 DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
442 link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
446 EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
448 static bool is_edid_digital_input_dp(const struct edid *edid)
450 return edid && edid->revision >= 4 &&
451 edid->input & DRM_EDID_INPUT_DIGITAL &&
452 (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
456 * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
457 * @dpcd: DisplayPort configuration data
458 * @port_cap: port capabilities
459 * @type: port type to be checked. Can be:
460 * %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
461 * %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
462 * %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
464 * Caveat: Only works with DPCD 1.1+ port caps.
466 * Returns: whether the downstream facing port matches the type.
468 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
469 const u8 port_cap[4], u8 type)
471 return drm_dp_is_branch(dpcd) &&
472 dpcd[DP_DPCD_REV] >= 0x11 &&
473 (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
475 EXPORT_SYMBOL(drm_dp_downstream_is_type);
478 * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
479 * @dpcd: DisplayPort configuration data
480 * @port_cap: port capabilities
483 * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
485 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
486 const u8 port_cap[4],
487 const struct edid *edid)
489 if (dpcd[DP_DPCD_REV] < 0x11) {
490 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
491 case DP_DWN_STRM_PORT_TYPE_TMDS:
498 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
499 case DP_DS_PORT_TYPE_DP_DUALMODE:
500 if (is_edid_digital_input_dp(edid))
503 case DP_DS_PORT_TYPE_DVI:
504 case DP_DS_PORT_TYPE_HDMI:
510 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
513 * drm_dp_send_real_edid_checksum() - send back real edid checksum value
514 * @aux: DisplayPort AUX channel
515 * @real_edid_checksum: real edid checksum for the last block
520 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
521 u8 real_edid_checksum)
523 u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
525 if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
526 &auto_test_req, 1) < 1) {
527 drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
528 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
531 auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
533 if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
534 drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
535 aux->name, DP_TEST_REQUEST);
538 link_edid_read &= DP_TEST_LINK_EDID_READ;
540 if (!auto_test_req || !link_edid_read) {
541 drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",
546 if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
547 &auto_test_req, 1) < 1) {
548 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
549 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
553 /* send back checksum for the last edid extension block data */
554 if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
555 &real_edid_checksum, 1) < 1) {
556 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
557 aux->name, DP_TEST_EDID_CHECKSUM);
561 test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
562 if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
563 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
564 aux->name, DP_TEST_RESPONSE);
570 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
572 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
574 u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
576 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
582 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
583 u8 dpcd[DP_RECEIVER_CAP_SIZE])
589 * Prior to DP1.3 the bit represented by
590 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
591 * If it is set DP_DPCD_REV at 0000h could be at a value less than
592 * the true capability of the panel. The only way to check is to
593 * then compare 0000h and 2200h.
595 if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
596 DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
599 ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
603 if (ret != sizeof(dpcd_ext))
606 if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
607 drm_dbg_kms(aux->drm_dev,
608 "%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
609 aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
613 if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
616 drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
618 memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
624 * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
626 * @aux: DisplayPort AUX channel
627 * @dpcd: Buffer to store the resulting DPCD in
629 * Attempts to read the base DPCD caps for @aux. Additionally, this function
630 * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
633 * Returns: %0 if the DPCD was read successfully, negative error code
636 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
637 u8 dpcd[DP_RECEIVER_CAP_SIZE])
641 ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
644 if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
647 ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
651 drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
655 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
658 * drm_dp_read_downstream_info() - read DPCD downstream port info if available
659 * @aux: DisplayPort AUX channel
660 * @dpcd: A cached copy of the port's DPCD
661 * @downstream_ports: buffer to store the downstream port info in
664 * drm_dp_downstream_max_clock()
665 * drm_dp_downstream_max_bpc()
667 * Returns: 0 if either the downstream port info was read successfully or
668 * there was no downstream info to read, or a negative error code otherwise.
670 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
671 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
672 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
677 memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
679 /* No downstream info to read */
680 if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)
683 /* Some branches advertise having 0 downstream ports, despite also advertising they have a
684 * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
685 * some branches do it we need to handle it regardless.
687 len = drm_dp_downstream_port_count(dpcd);
691 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
694 ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
700 drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);
704 EXPORT_SYMBOL(drm_dp_read_downstream_info);
707 * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
708 * @dpcd: DisplayPort configuration data
709 * @port_cap: port capabilities
711 * Returns: Downstream facing port max dot clock in kHz on success,
712 * or 0 if max clock not defined
714 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
715 const u8 port_cap[4])
717 if (!drm_dp_is_branch(dpcd))
720 if (dpcd[DP_DPCD_REV] < 0x11)
723 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
724 case DP_DS_PORT_TYPE_VGA:
725 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
727 return port_cap[1] * 8000;
732 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
735 * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
736 * @dpcd: DisplayPort configuration data
737 * @port_cap: port capabilities
740 * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
741 * or 0 if max TMDS clock not defined
743 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
744 const u8 port_cap[4],
745 const struct edid *edid)
747 if (!drm_dp_is_branch(dpcd))
750 if (dpcd[DP_DPCD_REV] < 0x11) {
751 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
752 case DP_DWN_STRM_PORT_TYPE_TMDS:
759 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
760 case DP_DS_PORT_TYPE_DP_DUALMODE:
761 if (is_edid_digital_input_dp(edid))
764 * It's left up to the driver to check the
765 * DP dual mode adapter's max TMDS clock.
767 * Unfortunatley it looks like branch devices
768 * may not fordward that the DP dual mode i2c
769 * access so we just usually get i2c nak :(
772 case DP_DS_PORT_TYPE_HDMI:
774 * We should perhaps assume 165 MHz when detailed cap
775 * info is not available. But looks like many typical
776 * branch devices fall into that category and so we'd
777 * probably end up with users complaining that they can't
778 * get high resolution modes with their favorite dongle.
780 * So let's limit to 300 MHz instead since DPCD 1.4
781 * HDMI 2.0 DFPs are required to have the detailed cap
782 * info. So it's more likely we're dealing with a HDMI 1.4
783 * compatible* device here.
785 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
787 return port_cap[1] * 2500;
788 case DP_DS_PORT_TYPE_DVI:
789 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
791 /* FIXME what to do about DVI dual link? */
792 return port_cap[1] * 2500;
797 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
800 * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
801 * @dpcd: DisplayPort configuration data
802 * @port_cap: port capabilities
805 * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
806 * or 0 if max TMDS clock not defined
808 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
809 const u8 port_cap[4],
810 const struct edid *edid)
812 if (!drm_dp_is_branch(dpcd))
815 if (dpcd[DP_DPCD_REV] < 0x11) {
816 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
817 case DP_DWN_STRM_PORT_TYPE_TMDS:
824 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
825 case DP_DS_PORT_TYPE_DP_DUALMODE:
826 if (is_edid_digital_input_dp(edid))
829 case DP_DS_PORT_TYPE_DVI:
830 case DP_DS_PORT_TYPE_HDMI:
832 * Unclear whether the protocol converter could
833 * utilize pixel replication. Assume it won't.
840 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
843 * drm_dp_downstream_max_bpc() - extract downstream facing port max
845 * @dpcd: DisplayPort configuration data
846 * @port_cap: downstream facing port capabilities
849 * Returns: Max bpc on success or 0 if max bpc not defined
851 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
852 const u8 port_cap[4],
853 const struct edid *edid)
855 if (!drm_dp_is_branch(dpcd))
858 if (dpcd[DP_DPCD_REV] < 0x11) {
859 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
860 case DP_DWN_STRM_PORT_TYPE_DP:
867 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
868 case DP_DS_PORT_TYPE_DP:
870 case DP_DS_PORT_TYPE_DP_DUALMODE:
871 if (is_edid_digital_input_dp(edid))
874 case DP_DS_PORT_TYPE_HDMI:
875 case DP_DS_PORT_TYPE_DVI:
876 case DP_DS_PORT_TYPE_VGA:
877 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
880 switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
897 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
900 * drm_dp_downstream_420_passthrough() - determine downstream facing port
901 * YCbCr 4:2:0 pass-through capability
902 * @dpcd: DisplayPort configuration data
903 * @port_cap: downstream facing port capabilities
905 * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
907 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
908 const u8 port_cap[4])
910 if (!drm_dp_is_branch(dpcd))
913 if (dpcd[DP_DPCD_REV] < 0x13)
916 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
917 case DP_DS_PORT_TYPE_DP:
919 case DP_DS_PORT_TYPE_HDMI:
920 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
923 return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
928 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
931 * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
932 * YCbCr 4:4:4->4:2:0 conversion capability
933 * @dpcd: DisplayPort configuration data
934 * @port_cap: downstream facing port capabilities
936 * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
938 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
939 const u8 port_cap[4])
941 if (!drm_dp_is_branch(dpcd))
944 if (dpcd[DP_DPCD_REV] < 0x13)
947 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
948 case DP_DS_PORT_TYPE_HDMI:
949 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
952 return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
957 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
960 * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
961 * RGB->YCbCr conversion capability
962 * @dpcd: DisplayPort configuration data
963 * @port_cap: downstream facing port capabilities
964 * @color_spc: Colorspace for which conversion cap is sought
966 * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
969 bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
970 const u8 port_cap[4],
973 if (!drm_dp_is_branch(dpcd))
976 if (dpcd[DP_DPCD_REV] < 0x13)
979 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
980 case DP_DS_PORT_TYPE_HDMI:
981 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
984 return port_cap[3] & color_spc;
989 EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
992 * drm_dp_downstream_mode() - return a mode for downstream facing port
994 * @dpcd: DisplayPort configuration data
995 * @port_cap: port capabilities
997 * Provides a suitable mode for downstream facing ports without EDID.
999 * Returns: A new drm_display_mode on success or NULL on failure
1001 struct drm_display_mode *
1002 drm_dp_downstream_mode(struct drm_device *dev,
1003 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1004 const u8 port_cap[4])
1009 if (!drm_dp_is_branch(dpcd))
1012 if (dpcd[DP_DPCD_REV] < 0x11)
1015 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1016 case DP_DS_PORT_TYPE_NON_EDID:
1017 switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
1018 case DP_DS_NON_EDID_720x480i_60:
1021 case DP_DS_NON_EDID_720x480i_50:
1024 case DP_DS_NON_EDID_1920x1080i_60:
1027 case DP_DS_NON_EDID_1920x1080i_50:
1030 case DP_DS_NON_EDID_1280x720_60:
1033 case DP_DS_NON_EDID_1280x720_50:
1039 return drm_display_mode_from_cea_vic(dev, vic);
1044 EXPORT_SYMBOL(drm_dp_downstream_mode);
1047 * drm_dp_downstream_id() - identify branch device
1048 * @aux: DisplayPort AUX channel
1049 * @id: DisplayPort branch device id
1051 * Returns branch device id on success or NULL on failure
1053 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
1055 return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
1057 EXPORT_SYMBOL(drm_dp_downstream_id);
1060 * drm_dp_downstream_debug() - debug DP branch devices
1061 * @m: pointer for debugfs file
1062 * @dpcd: DisplayPort configuration data
1063 * @port_cap: port capabilities
1065 * @aux: DisplayPort AUX channel
1068 void drm_dp_downstream_debug(struct seq_file *m,
1069 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1070 const u8 port_cap[4],
1071 const struct edid *edid,
1072 struct drm_dp_aux *aux)
1074 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1075 DP_DETAILED_CAP_INFO_AVAILABLE;
1081 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1082 bool branch_device = drm_dp_is_branch(dpcd);
1084 seq_printf(m, "\tDP branch device present: %s\n",
1085 branch_device ? "yes" : "no");
1091 case DP_DS_PORT_TYPE_DP:
1092 seq_puts(m, "\t\tType: DisplayPort\n");
1094 case DP_DS_PORT_TYPE_VGA:
1095 seq_puts(m, "\t\tType: VGA\n");
1097 case DP_DS_PORT_TYPE_DVI:
1098 seq_puts(m, "\t\tType: DVI\n");
1100 case DP_DS_PORT_TYPE_HDMI:
1101 seq_puts(m, "\t\tType: HDMI\n");
1103 case DP_DS_PORT_TYPE_NON_EDID:
1104 seq_puts(m, "\t\tType: others without EDID support\n");
1106 case DP_DS_PORT_TYPE_DP_DUALMODE:
1107 seq_puts(m, "\t\tType: DP++\n");
1109 case DP_DS_PORT_TYPE_WIRELESS:
1110 seq_puts(m, "\t\tType: Wireless\n");
1113 seq_puts(m, "\t\tType: N/A\n");
1116 memset(id, 0, sizeof(id));
1117 drm_dp_downstream_id(aux, id);
1118 seq_printf(m, "\t\tID: %s\n", id);
1120 len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1122 seq_printf(m, "\t\tHW: %d.%d\n",
1123 (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1125 len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1127 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1129 if (detailed_cap_info) {
1130 clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1132 seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1134 clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1136 seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1138 clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1140 seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1142 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1145 seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1148 EXPORT_SYMBOL(drm_dp_downstream_debug);
1151 * drm_dp_subconnector_type() - get DP branch device type
1152 * @dpcd: DisplayPort configuration data
1153 * @port_cap: port capabilities
1155 enum drm_mode_subconnector
1156 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1157 const u8 port_cap[4])
1160 if (!drm_dp_is_branch(dpcd))
1161 return DRM_MODE_SUBCONNECTOR_Native;
1162 /* DP 1.0 approach */
1163 if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1164 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1165 DP_DWN_STRM_PORT_TYPE_MASK;
1168 case DP_DWN_STRM_PORT_TYPE_TMDS:
1169 /* Can be HDMI or DVI-D, DVI-D is a safer option */
1170 return DRM_MODE_SUBCONNECTOR_DVID;
1171 case DP_DWN_STRM_PORT_TYPE_ANALOG:
1172 /* Can be VGA or DVI-A, VGA is more popular */
1173 return DRM_MODE_SUBCONNECTOR_VGA;
1174 case DP_DWN_STRM_PORT_TYPE_DP:
1175 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1176 case DP_DWN_STRM_PORT_TYPE_OTHER:
1178 return DRM_MODE_SUBCONNECTOR_Unknown;
1181 type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1184 case DP_DS_PORT_TYPE_DP:
1185 case DP_DS_PORT_TYPE_DP_DUALMODE:
1186 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1187 case DP_DS_PORT_TYPE_VGA:
1188 return DRM_MODE_SUBCONNECTOR_VGA;
1189 case DP_DS_PORT_TYPE_DVI:
1190 return DRM_MODE_SUBCONNECTOR_DVID;
1191 case DP_DS_PORT_TYPE_HDMI:
1192 return DRM_MODE_SUBCONNECTOR_HDMIA;
1193 case DP_DS_PORT_TYPE_WIRELESS:
1194 return DRM_MODE_SUBCONNECTOR_Wireless;
1195 case DP_DS_PORT_TYPE_NON_EDID:
1197 return DRM_MODE_SUBCONNECTOR_Unknown;
1200 EXPORT_SYMBOL(drm_dp_subconnector_type);
1203 * drm_dp_set_subconnector_property - set subconnector for DP connector
1204 * @connector: connector to set property on
1205 * @status: connector status
1206 * @dpcd: DisplayPort configuration data
1207 * @port_cap: port capabilities
1209 * Called by a driver on every detect event.
1211 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1212 enum drm_connector_status status,
1214 const u8 port_cap[4])
1216 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1218 if (status == connector_status_connected)
1219 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1220 drm_object_property_set_value(&connector->base,
1221 connector->dev->mode_config.dp_subconnector_property,
1224 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1227 * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1229 * @connector: The DRM connector to check
1230 * @dpcd: A cached copy of the connector's DPCD RX capabilities
1231 * @desc: A cached copy of the connector's DP descriptor
1233 * See also: drm_dp_read_sink_count()
1235 * Returns: %True if the (e)DP connector has a valid sink count that should
1236 * be probed, %false otherwise.
1238 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1239 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1240 const struct drm_dp_desc *desc)
1242 /* Some eDP panels don't set a valid value for the sink count */
1243 return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1244 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1245 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1246 !drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);
1248 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1251 * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1252 * @aux: The DP AUX channel to use
1254 * See also: drm_dp_read_sink_count_cap()
1256 * Returns: The current sink count reported by @aux, or a negative error code
1259 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1264 ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1270 return DP_GET_SINK_COUNT(count);
1272 EXPORT_SYMBOL(drm_dp_read_sink_count);
1275 * I2C-over-AUX implementation
1278 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1280 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1281 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1282 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1283 I2C_FUNC_10BIT_ADDR;
1286 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1289 * In case of i2c defer or short i2c ack reply to a write,
1290 * we need to switch to WRITE_STATUS_UPDATE to drain the
1291 * rest of the message
1293 if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1294 msg->request &= DP_AUX_I2C_MOT;
1295 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1299 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1300 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1301 #define AUX_STOP_LEN 4
1302 #define AUX_CMD_LEN 4
1303 #define AUX_ADDRESS_LEN 20
1304 #define AUX_REPLY_PAD_LEN 4
1305 #define AUX_LENGTH_LEN 8
1308 * Calculate the duration of the AUX request/reply in usec. Gives the
1309 * "best" case estimate, ie. successful while as short as possible.
1311 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1313 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1314 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1316 if ((msg->request & DP_AUX_I2C_READ) == 0)
1317 len += msg->size * 8;
1322 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1324 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1325 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1328 * For read we expect what was asked. For writes there will
1329 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1331 if (msg->request & DP_AUX_I2C_READ)
1332 len += msg->size * 8;
1337 #define I2C_START_LEN 1
1338 #define I2C_STOP_LEN 1
1339 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1340 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1343 * Calculate the length of the i2c transfer in usec, assuming
1344 * the i2c bus speed is as specified. Gives the the "worst"
1345 * case estimate, ie. successful while as long as possible.
1346 * Doesn't account the the "MOT" bit, and instead assumes each
1347 * message includes a START, ADDRESS and STOP. Neither does it
1348 * account for additional random variables such as clock stretching.
1350 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1353 /* AUX bitrate is 1MHz, i2c bitrate as specified */
1354 return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1355 msg->size * I2C_DATA_LEN +
1356 I2C_STOP_LEN) * 1000, i2c_speed_khz);
1360 * Deterine how many retries should be attempted to successfully transfer
1361 * the specified message, based on the estimated durations of the
1362 * i2c and AUX transfers.
1364 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1367 int aux_time_us = drm_dp_aux_req_duration(msg) +
1368 drm_dp_aux_reply_duration(msg);
1369 int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1371 return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1375 * FIXME currently assumes 10 kHz as some real world devices seem
1376 * to require it. We should query/set the speed via DPCD if supported.
1378 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1379 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1380 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1381 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1384 * Transfer a single I2C-over-AUX message and handle various error conditions,
1385 * retrying the transaction as appropriate. It is assumed that the
1386 * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1389 * Returns bytes transferred on success, or a negative error code on failure.
1391 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1393 unsigned int retry, defer_i2c;
1396 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1397 * is required to retry at least seven times upon receiving AUX_DEFER
1398 * before giving up the AUX transaction.
1400 * We also try to account for the i2c bus speed.
1402 int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1404 for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1405 ret = aux->transfer(aux, msg);
1411 * While timeouts can be errors, they're usually normal
1412 * behavior (for instance, when a driver tries to
1413 * communicate with a non-existant DisplayPort device).
1414 * Avoid spamming the kernel log with timeout errors.
1416 if (ret == -ETIMEDOUT)
1417 drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",
1420 drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",
1426 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1427 case DP_AUX_NATIVE_REPLY_ACK:
1429 * For I2C-over-AUX transactions this isn't enough, we
1430 * need to check for the I2C ACK reply.
1434 case DP_AUX_NATIVE_REPLY_NACK:
1435 drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",
1436 aux->name, ret, msg->size);
1439 case DP_AUX_NATIVE_REPLY_DEFER:
1440 drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);
1442 * We could check for I2C bit rate capabilities and if
1443 * available adjust this interval. We could also be
1444 * more careful with DP-to-legacy adapters where a
1445 * long legacy cable may force very low I2C bit rates.
1447 * For now just defer for long enough to hopefully be
1448 * safe for all use-cases.
1450 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1454 drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",
1455 aux->name, msg->reply);
1459 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1460 case DP_AUX_I2C_REPLY_ACK:
1462 * Both native ACK and I2C ACK replies received. We
1463 * can assume the transfer was successful.
1465 if (ret != msg->size)
1466 drm_dp_i2c_msg_write_status_update(msg);
1469 case DP_AUX_I2C_REPLY_NACK:
1470 drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",
1471 aux->name, ret, msg->size);
1472 aux->i2c_nack_count++;
1475 case DP_AUX_I2C_REPLY_DEFER:
1476 drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);
1477 /* DP Compliance Test 4.2.2.5 Requirement:
1478 * Must have at least 7 retries for I2C defers on the
1479 * transaction to pass this test
1481 aux->i2c_defer_count++;
1484 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1485 drm_dp_i2c_msg_write_status_update(msg);
1490 drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",
1491 aux->name, msg->reply);
1496 drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);
1500 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1501 const struct i2c_msg *i2c_msg)
1503 msg->request = (i2c_msg->flags & I2C_M_RD) ?
1504 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1505 if (!(i2c_msg->flags & I2C_M_STOP))
1506 msg->request |= DP_AUX_I2C_MOT;
1510 * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1512 * Returns an error code on failure, or a recommended transfer size on success.
1514 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1516 int err, ret = orig_msg->size;
1517 struct drm_dp_aux_msg msg = *orig_msg;
1519 while (msg.size > 0) {
1520 err = drm_dp_i2c_do_msg(aux, &msg);
1522 return err == 0 ? -EPROTO : err;
1524 if (err < msg.size && err < ret) {
1525 drm_dbg_kms(aux->drm_dev,
1526 "%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1527 aux->name, msg.size, err);
1539 * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1540 * packets to be as large as possible. If not, the I2C transactions never
1541 * succeed. Hence the default is maximum.
1543 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1544 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1545 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1546 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1548 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1551 struct drm_dp_aux *aux = adapter->algo_data;
1553 unsigned transfer_size;
1554 struct drm_dp_aux_msg msg;
1557 dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1559 memset(&msg, 0, sizeof(msg));
1561 for (i = 0; i < num; i++) {
1562 msg.address = msgs[i].addr;
1563 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1564 /* Send a bare address packet to start the transaction.
1565 * Zero sized messages specify an address only (bare
1566 * address) transaction.
1570 err = drm_dp_i2c_do_msg(aux, &msg);
1573 * Reset msg.request in case in case it got
1574 * changed into a WRITE_STATUS_UPDATE.
1576 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1580 /* We want each transaction to be as large as possible, but
1581 * we'll go to smaller sizes if the hardware gives us a
1584 transfer_size = dp_aux_i2c_transfer_size;
1585 for (j = 0; j < msgs[i].len; j += msg.size) {
1586 msg.buffer = msgs[i].buf + j;
1587 msg.size = min(transfer_size, msgs[i].len - j);
1589 err = drm_dp_i2c_drain_msg(aux, &msg);
1592 * Reset msg.request in case in case it got
1593 * changed into a WRITE_STATUS_UPDATE.
1595 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1599 transfer_size = err;
1606 /* Send a bare address packet to close out the transaction.
1607 * Zero sized messages specify an address only (bare
1608 * address) transaction.
1610 msg.request &= ~DP_AUX_I2C_MOT;
1613 (void)drm_dp_i2c_do_msg(aux, &msg);
1618 static const struct i2c_algorithm drm_dp_i2c_algo = {
1619 .functionality = drm_dp_i2c_functionality,
1620 .master_xfer = drm_dp_i2c_xfer,
1623 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1625 return container_of(i2c, struct drm_dp_aux, ddc);
1628 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1630 mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1633 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1635 return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1638 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1640 mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1643 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1644 .lock_bus = lock_bus,
1645 .trylock_bus = trylock_bus,
1646 .unlock_bus = unlock_bus,
1649 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1654 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1658 WARN_ON(!(buf & DP_TEST_SINK_START));
1660 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1664 count = buf & DP_TEST_COUNT_MASK;
1665 if (count == aux->crc_count)
1666 return -EAGAIN; /* No CRC yet */
1668 aux->crc_count = count;
1671 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1672 * per component (RGB or CrYCb).
1674 ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1681 static void drm_dp_aux_crc_work(struct work_struct *work)
1683 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1685 struct drm_crtc *crtc;
1690 if (WARN_ON(!aux->crtc))
1694 while (crtc->crc.opened) {
1695 drm_crtc_wait_one_vblank(crtc);
1696 if (!crtc->crc.opened)
1699 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1700 if (ret == -EAGAIN) {
1701 usleep_range(1000, 2000);
1702 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1705 if (ret == -EAGAIN) {
1706 drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",
1710 drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);
1714 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1715 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1716 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1717 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1722 * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1723 * @aux: DisplayPort AUX channel
1725 * Used for remote aux channel in general. Merely initialize the crc work
1728 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1730 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1732 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1735 * drm_dp_aux_init() - minimally initialise an aux channel
1736 * @aux: DisplayPort AUX channel
1738 * If you need to use the drm_dp_aux's i2c adapter prior to registering it with
1739 * the outside world, call drm_dp_aux_init() first. For drivers which are
1740 * grandparents to their AUX adapters (e.g. the AUX adapter is parented by a
1741 * &drm_connector), you must still call drm_dp_aux_register() once the connector
1742 * has been registered to allow userspace access to the auxiliary DP channel.
1743 * Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as
1744 * early as possible so that the &drm_device that corresponds to the AUX adapter
1745 * may be mentioned in debugging output from the DRM DP helpers.
1747 * For devices which use a separate platform device for their AUX adapters, this
1748 * may be called as early as required by the driver.
1751 void drm_dp_aux_init(struct drm_dp_aux *aux)
1753 mutex_init(&aux->hw_mutex);
1754 mutex_init(&aux->cec.lock);
1755 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1757 aux->ddc.algo = &drm_dp_i2c_algo;
1758 aux->ddc.algo_data = aux;
1759 aux->ddc.retries = 3;
1761 aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1763 EXPORT_SYMBOL(drm_dp_aux_init);
1766 * drm_dp_aux_register() - initialise and register aux channel
1767 * @aux: DisplayPort AUX channel
1769 * Automatically calls drm_dp_aux_init() if this hasn't been done yet. This
1770 * should only be called once the parent of @aux, &drm_dp_aux.dev, is
1771 * initialized. For devices which are grandparents of their AUX channels,
1772 * &drm_dp_aux.dev will typically be the &drm_connector &device which
1773 * corresponds to @aux. For these devices, it's advised to call
1774 * drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to
1775 * call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.
1776 * Functions which don't follow this will likely Oops when
1777 * %CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1779 * For devices where the AUX channel is a device that exists independently of
1780 * the &drm_device that uses it, such as SoCs and bridge devices, it is
1781 * recommended to call drm_dp_aux_register() after a &drm_device has been
1782 * assigned to &drm_dp_aux.drm_dev, and likewise to call
1783 * drm_dp_aux_unregister() once the &drm_device should no longer be associated
1784 * with the AUX channel (e.g. on bridge detach).
1786 * Drivers which need to use the aux channel before either of the two points
1787 * mentioned above need to call drm_dp_aux_init() in order to use the AUX
1788 * channel before registration.
1790 * Returns 0 on success or a negative error code on failure.
1792 int drm_dp_aux_register(struct drm_dp_aux *aux)
1796 WARN_ON_ONCE(!aux->drm_dev);
1799 drm_dp_aux_init(aux);
1801 aux->ddc.class = I2C_CLASS_DDC;
1802 aux->ddc.owner = THIS_MODULE;
1803 aux->ddc.dev.parent = aux->dev;
1805 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1806 sizeof(aux->ddc.name));
1808 ret = drm_dp_aux_register_devnode(aux);
1812 ret = i2c_add_adapter(&aux->ddc);
1814 drm_dp_aux_unregister_devnode(aux);
1820 EXPORT_SYMBOL(drm_dp_aux_register);
1823 * drm_dp_aux_unregister() - unregister an AUX adapter
1824 * @aux: DisplayPort AUX channel
1826 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1828 drm_dp_aux_unregister_devnode(aux);
1829 i2c_del_adapter(&aux->ddc);
1831 EXPORT_SYMBOL(drm_dp_aux_unregister);
1833 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1836 * drm_dp_psr_setup_time() - PSR setup in time usec
1837 * @psr_cap: PSR capabilities from DPCD
1840 * PSR setup time for the panel in microseconds, negative
1841 * error code on failure.
1843 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1845 static const u16 psr_setup_time_us[] = {
1846 PSR_SETUP_TIME(330),
1847 PSR_SETUP_TIME(275),
1848 PSR_SETUP_TIME(220),
1849 PSR_SETUP_TIME(165),
1850 PSR_SETUP_TIME(110),
1856 i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1857 if (i >= ARRAY_SIZE(psr_setup_time_us))
1860 return psr_setup_time_us[i];
1862 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1864 #undef PSR_SETUP_TIME
1867 * drm_dp_start_crc() - start capture of frame CRCs
1868 * @aux: DisplayPort AUX channel
1869 * @crtc: CRTC displaying the frames whose CRCs are to be captured
1871 * Returns 0 on success or a negative error code on failure.
1873 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1878 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1882 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1888 schedule_work(&aux->crc_work);
1892 EXPORT_SYMBOL(drm_dp_start_crc);
1895 * drm_dp_stop_crc() - stop capture of frame CRCs
1896 * @aux: DisplayPort AUX channel
1898 * Returns 0 on success or a negative error code on failure.
1900 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1905 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1909 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1913 flush_work(&aux->crc_work);
1918 EXPORT_SYMBOL(drm_dp_stop_crc);
1927 #define OUI(first, second, third) { (first), (second), (third) }
1928 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1929 { (first), (second), (third), (fourth), (fifth), (sixth) }
1931 #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
1933 static const struct dpcd_quirk dpcd_quirk_list[] = {
1934 /* Analogix 7737 needs reduced M and N at HBR2 link rates */
1935 { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1936 /* LG LP140WF6-SPM1 eDP panel */
1937 { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1938 /* Apple panels need some additional handling to support PSR */
1939 { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1940 /* CH7511 seems to leave SINK_COUNT zeroed */
1941 { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1942 /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1943 { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1944 /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1945 { 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) },
1951 * Get a bit mask of DPCD quirks for the sink/branch device identified by
1952 * ident. The quirk data is shared but it's up to the drivers to act on the
1955 * For now, only the OUI (first three bytes) is used, but this may be extended
1956 * to device identification string and hardware/firmware revisions later.
1959 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1961 const struct dpcd_quirk *quirk;
1964 u8 any_device[] = DEVICE_ID_ANY;
1966 for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1967 quirk = &dpcd_quirk_list[i];
1969 if (quirk->is_branch != is_branch)
1972 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1975 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
1976 memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
1979 quirks |= quirk->quirks;
1985 #undef DEVICE_ID_ANY
1989 * drm_dp_read_desc - read sink/branch descriptor from DPCD
1990 * @aux: DisplayPort AUX channel
1991 * @desc: Device descriptor to fill from DPCD
1992 * @is_branch: true for branch devices, false for sink devices
1994 * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
1997 * Returns 0 on success or a negative error code on failure.
1999 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
2002 struct drm_dp_dpcd_ident *ident = &desc->ident;
2003 unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
2004 int ret, dev_id_len;
2006 ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
2010 desc->quirks = drm_dp_get_quirks(ident, is_branch);
2012 dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
2014 drm_dbg_kms(aux->drm_dev,
2015 "%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
2016 aux->name, is_branch ? "branch" : "sink",
2017 (int)sizeof(ident->oui), ident->oui, dev_id_len,
2018 ident->device_id, ident->hw_rev >> 4, ident->hw_rev & 0xf,
2019 ident->sw_major_rev, ident->sw_minor_rev, desc->quirks);
2023 EXPORT_SYMBOL(drm_dp_read_desc);
2026 * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
2027 * supported by the DSC sink.
2028 * @dsc_dpcd: DSC capabilities from DPCD
2029 * @is_edp: true if its eDP, false for DP
2031 * Read the slice capabilities DPCD register from DSC sink to get
2032 * the maximum slice count supported. This is used to populate
2033 * the DSC parameters in the &struct drm_dsc_config by the driver.
2034 * Driver creates an infoframe using these parameters to populate
2035 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2036 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2039 * Maximum slice count supported by DSC sink or 0 its invalid
2041 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2044 u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
2047 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
2048 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2050 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2052 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2055 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2056 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2058 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2060 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2062 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2064 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2066 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2068 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2070 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2072 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2074 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2076 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2082 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2085 * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2086 * @dsc_dpcd: DSC capabilities from DPCD
2088 * Read the DSC DPCD register to parse the line buffer depth in bits which is
2089 * number of bits of precision within the decoder line buffer supported by
2090 * the DSC sink. This is used to populate the DSC parameters in the
2091 * &struct drm_dsc_config by the driver.
2092 * Driver creates an infoframe using these parameters to populate
2093 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2094 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2097 * Line buffer depth supported by DSC panel or 0 its invalid
2099 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2101 u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2103 switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2104 case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2106 case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2108 case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2110 case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2112 case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2114 case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2116 case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2118 case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2120 case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2126 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2129 * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2130 * values supported by the DSC sink.
2131 * @dsc_dpcd: DSC capabilities from DPCD
2132 * @dsc_bpc: An array to be filled by this helper with supported
2135 * Read the DSC DPCD from the sink device to parse the supported bits per
2136 * component values. This is used to populate the DSC parameters
2137 * in the &struct drm_dsc_config by the driver.
2138 * Driver creates an infoframe using these parameters to populate
2139 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2140 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2143 * Number of input BPC values parsed from the DPCD
2145 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2149 u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2151 if (color_depth & DP_DSC_12_BPC)
2152 dsc_bpc[num_bpc++] = 12;
2153 if (color_depth & DP_DSC_10_BPC)
2154 dsc_bpc[num_bpc++] = 10;
2155 if (color_depth & DP_DSC_8_BPC)
2156 dsc_bpc[num_bpc++] = 8;
2160 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2163 * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
2164 * @aux: DisplayPort AUX channel
2165 * @caps: buffer to return the capability info in
2167 * Read capabilities common to all LTTPRs.
2169 * Returns 0 on success or a negative error code on failure.
2171 int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
2172 u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2176 ret = drm_dp_dpcd_read(aux,
2177 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
2178 caps, DP_LTTPR_COMMON_CAP_SIZE);
2182 WARN_ON(ret != DP_LTTPR_COMMON_CAP_SIZE);
2186 EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
2189 * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
2190 * @aux: DisplayPort AUX channel
2191 * @dp_phy: LTTPR PHY to read the capabilities for
2192 * @caps: buffer to return the capability info in
2194 * Read the capabilities for the given LTTPR PHY.
2196 * Returns 0 on success or a negative error code on failure.
2198 int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
2199 enum drm_dp_phy dp_phy,
2200 u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2204 ret = drm_dp_dpcd_read(aux,
2205 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
2206 caps, DP_LTTPR_PHY_CAP_SIZE);
2210 WARN_ON(ret != DP_LTTPR_PHY_CAP_SIZE);
2214 EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
2216 static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
2218 return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
2222 * drm_dp_lttpr_count - get the number of detected LTTPRs
2223 * @caps: LTTPR common capabilities
2225 * Get the number of detected LTTPRs from the LTTPR common capabilities info.
2228 * -ERANGE if more than supported number (8) of LTTPRs are detected
2229 * -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
2230 * otherwise the number of detected LTTPRs
2232 int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2234 u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
2236 switch (hweight8(count)) {
2240 return 8 - ilog2(count);
2247 EXPORT_SYMBOL(drm_dp_lttpr_count);
2250 * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
2251 * @caps: LTTPR common capabilities
2253 * Returns the maximum link rate supported by all detected LTTPRs.
2255 int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2257 u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
2259 return drm_dp_bw_code_to_link_rate(rate);
2261 EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
2264 * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
2265 * @caps: LTTPR common capabilities
2267 * Returns the maximum lane count supported by all detected LTTPRs.
2269 int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2271 u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
2273 return max_lanes & DP_MAX_LANE_COUNT_MASK;
2275 EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
2278 * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
2279 * @caps: LTTPR PHY capabilities
2281 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2282 * voltage swing level 3.
2285 drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2287 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2289 return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
2291 EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
2294 * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
2295 * @caps: LTTPR PHY capabilities
2297 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2298 * pre-emphasis level 3.
2301 drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2303 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2305 return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
2307 EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
2310 * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2311 * @aux: DisplayPort AUX channel
2312 * @data: DP phy compliance test parameters.
2314 * Returns 0 on success or a negative error code on failure.
2316 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2317 struct drm_dp_phy_test_params *data)
2322 err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2325 data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2327 err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2330 data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2332 if (lanes & DP_ENHANCED_FRAME_CAP)
2333 data->enhanced_frame_cap = true;
2335 err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2339 switch (data->phy_pattern) {
2340 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2341 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2342 &data->custom80, sizeof(data->custom80));
2347 case DP_PHY_TEST_PATTERN_CP2520:
2348 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2350 sizeof(data->hbr2_reset));
2357 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2360 * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2361 * @aux: DisplayPort AUX channel
2362 * @data: DP phy compliance test parameters.
2363 * @dp_rev: DP revision to use for compliance testing
2365 * Returns 0 on success or a negative error code on failure.
2367 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2368 struct drm_dp_phy_test_params *data, u8 dp_rev)
2374 link_config[0] = drm_dp_link_rate_to_bw_code(data->link_rate);
2375 link_config[1] = data->num_lanes;
2376 if (data->enhanced_frame_cap)
2377 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
2378 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, link_config, 2);
2382 test_pattern = data->phy_pattern;
2383 if (dp_rev < 0x12) {
2384 test_pattern = (test_pattern << 2) &
2385 DP_LINK_QUAL_PATTERN_11_MASK;
2386 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2391 for (i = 0; i < data->num_lanes; i++) {
2392 err = drm_dp_dpcd_writeb(aux,
2393 DP_LINK_QUAL_LANE0_SET + i,
2402 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2404 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2406 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2409 switch (pixelformat) {
2410 case DP_PIXELFORMAT_RGB:
2412 case DP_PIXELFORMAT_YUV444:
2414 case DP_PIXELFORMAT_YUV422:
2416 case DP_PIXELFORMAT_YUV420:
2418 case DP_PIXELFORMAT_Y_ONLY:
2420 case DP_PIXELFORMAT_RAW:
2427 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2428 enum dp_colorimetry colorimetry)
2430 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2433 switch (colorimetry) {
2434 case DP_COLORIMETRY_DEFAULT:
2435 switch (pixelformat) {
2436 case DP_PIXELFORMAT_RGB:
2438 case DP_PIXELFORMAT_YUV444:
2439 case DP_PIXELFORMAT_YUV422:
2440 case DP_PIXELFORMAT_YUV420:
2442 case DP_PIXELFORMAT_Y_ONLY:
2443 return "DICOM PS3.14";
2444 case DP_PIXELFORMAT_RAW:
2445 return "Custom Color Profile";
2449 case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2450 switch (pixelformat) {
2451 case DP_PIXELFORMAT_RGB:
2452 return "Wide Fixed";
2453 case DP_PIXELFORMAT_YUV444:
2454 case DP_PIXELFORMAT_YUV422:
2455 case DP_PIXELFORMAT_YUV420:
2460 case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2461 switch (pixelformat) {
2462 case DP_PIXELFORMAT_RGB:
2463 return "Wide Float";
2464 case DP_PIXELFORMAT_YUV444:
2465 case DP_PIXELFORMAT_YUV422:
2466 case DP_PIXELFORMAT_YUV420:
2471 case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2472 switch (pixelformat) {
2473 case DP_PIXELFORMAT_RGB:
2475 case DP_PIXELFORMAT_YUV444:
2476 case DP_PIXELFORMAT_YUV422:
2477 case DP_PIXELFORMAT_YUV420:
2482 case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2483 switch (pixelformat) {
2484 case DP_PIXELFORMAT_RGB:
2486 case DP_PIXELFORMAT_YUV444:
2487 case DP_PIXELFORMAT_YUV422:
2488 case DP_PIXELFORMAT_YUV420:
2493 case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2494 switch (pixelformat) {
2495 case DP_PIXELFORMAT_RGB:
2496 return "Custom Profile";
2497 case DP_PIXELFORMAT_YUV444:
2498 case DP_PIXELFORMAT_YUV422:
2499 case DP_PIXELFORMAT_YUV420:
2504 case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2505 switch (pixelformat) {
2506 case DP_PIXELFORMAT_RGB:
2507 return "BT.2020 RGB";
2508 case DP_PIXELFORMAT_YUV444:
2509 case DP_PIXELFORMAT_YUV422:
2510 case DP_PIXELFORMAT_YUV420:
2511 return "BT.2020 CYCC";
2515 case DP_COLORIMETRY_BT2020_YCC:
2516 switch (pixelformat) {
2517 case DP_PIXELFORMAT_YUV444:
2518 case DP_PIXELFORMAT_YUV422:
2519 case DP_PIXELFORMAT_YUV420:
2520 return "BT.2020 YCC";
2529 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2531 switch (dynamic_range) {
2532 case DP_DYNAMIC_RANGE_VESA:
2533 return "VESA range";
2534 case DP_DYNAMIC_RANGE_CTA:
2541 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2543 switch (content_type) {
2544 case DP_CONTENT_TYPE_NOT_DEFINED:
2545 return "Not defined";
2546 case DP_CONTENT_TYPE_GRAPHICS:
2548 case DP_CONTENT_TYPE_PHOTO:
2550 case DP_CONTENT_TYPE_VIDEO:
2552 case DP_CONTENT_TYPE_GAME:
2559 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2560 const struct drm_dp_vsc_sdp *vsc)
2562 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2563 DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2564 vsc->revision, vsc->length);
2565 DP_SDP_LOG(" pixelformat: %s\n",
2566 dp_pixelformat_get_name(vsc->pixelformat));
2567 DP_SDP_LOG(" colorimetry: %s\n",
2568 dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2569 DP_SDP_LOG(" bpc: %u\n", vsc->bpc);
2570 DP_SDP_LOG(" dynamic range: %s\n",
2571 dp_dynamic_range_get_name(vsc->dynamic_range));
2572 DP_SDP_LOG(" content type: %s\n",
2573 dp_content_type_get_name(vsc->content_type));
2576 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
2579 * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
2580 * @dpcd: DisplayPort configuration data
2581 * @port_cap: port capabilities
2583 * Returns maximum frl bandwidth supported by PCON in GBPS,
2584 * returns 0 if not supported.
2586 int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
2587 const u8 port_cap[4])
2593 bw = buf & DP_PCON_MAX_FRL_BW;
2596 case DP_PCON_MAX_9GBPS:
2598 case DP_PCON_MAX_18GBPS:
2600 case DP_PCON_MAX_24GBPS:
2602 case DP_PCON_MAX_32GBPS:
2604 case DP_PCON_MAX_40GBPS:
2606 case DP_PCON_MAX_48GBPS:
2608 case DP_PCON_MAX_0GBPS:
2615 EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
2618 * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
2619 * @aux: DisplayPort AUX channel
2620 * @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.
2622 * Returns 0 if success, else returns negative error code.
2624 int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
2627 u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
2628 DP_PCON_ENABLE_LINK_FRL_MODE;
2630 if (enable_frl_ready_hpd)
2631 buf |= DP_PCON_ENABLE_HPD_READY;
2633 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2637 EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
2640 * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
2641 * @aux: DisplayPort AUX channel
2643 * Returns true if success, else returns false.
2645 bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
2650 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2654 if (buf & DP_PCON_FRL_READY)
2659 EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
2662 * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
2663 * @aux: DisplayPort AUX channel
2664 * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
2665 * @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.
2666 * In Concurrent Mode, the FRL link bring up can be done along with
2667 * DP Link training. In Sequential mode, the FRL link bring up is done prior to
2668 * the DP Link training.
2670 * Returns 0 if success, else returns negative error code.
2673 int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
2679 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2683 if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)
2684 buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
2686 buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
2688 switch (max_frl_gbps) {
2690 buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;
2693 buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;
2696 buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;
2699 buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;
2702 buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;
2705 buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;
2708 buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;
2714 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2720 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
2723 * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
2724 * @aux: DisplayPort AUX channel
2725 * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
2726 * @frl_type : FRL training type, can be Extended, or Normal.
2727 * In Normal FRL training, the PCON tries each frl bw from the max_frl_mask
2728 * starting from min, and stops when link training is successful. In Extended
2729 * FRL training, all frl bw selected in the mask are trained by the PCON.
2731 * Returns 0 if success, else returns negative error code.
2733 int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
2737 u8 buf = max_frl_mask;
2739 if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)
2740 buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2742 buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2744 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
2750 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
2753 * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
2754 * @aux: DisplayPort AUX channel
2756 * Returns 0 if success, else returns negative error code.
2758 int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
2762 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
2768 EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
2771 * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
2772 * @aux: DisplayPort AUX channel
2774 * Returns 0 if success, else returns negative error code.
2776 int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
2781 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2784 if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
2785 drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",
2789 buf |= DP_PCON_ENABLE_HDMI_LINK;
2790 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2796 EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
2799 * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
2800 * @aux: DisplayPort AUX channel
2802 * Returns true if link is active else returns false.
2804 bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
2809 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2813 return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
2815 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
2818 * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
2819 * @aux: DisplayPort AUX channel
2820 * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
2821 * Valid only if the MODE returned is FRL. For Normal Link training mode
2822 * only 1 of the bits will be set, but in case of Extended mode, more than
2823 * one bits can be set.
2825 * Returns the link mode : TMDS or FRL on success, else returns negative error
2828 int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
2834 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
2838 mode = buf & DP_PCON_HDMI_LINK_MODE;
2840 if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
2841 *frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
2845 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
2848 * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
2849 * during link failure between PCON and HDMI sink
2850 * @aux: DisplayPort AUX channel
2851 * @connector: DRM connector
2855 void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
2856 struct drm_connector *connector)
2858 u8 buf, error_count;
2860 struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
2862 for (i = 0; i < hdmi->max_lanes; i++) {
2863 if (drm_dp_dpcd_readb(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
2866 error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
2867 switch (error_count) {
2868 case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
2871 case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
2874 case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
2881 drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",
2882 aux->name, num_error, i);
2885 EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
2888 * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
2889 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2891 * Returns true is PCON encoder is DSC 1.2 else returns false.
2893 bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2896 u8 major_v, minor_v;
2898 buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
2899 major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
2900 minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
2902 if (major_v == 1 && minor_v == 2)
2907 EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
2910 * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
2911 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2913 * Returns maximum no. of slices supported by the PCON DSC Encoder.
2915 int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2917 u8 slice_cap1, slice_cap2;
2919 slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
2920 slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
2922 if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
2924 if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
2926 if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
2928 if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
2930 if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
2932 if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
2934 if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
2936 if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
2938 if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
2940 if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
2945 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
2948 * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
2949 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2951 * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
2953 int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2957 buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
2959 return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
2961 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
2964 * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
2965 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2967 * Returns the bpp precision supported by the PCON encoder.
2969 int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2973 buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
2975 switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
2976 case DP_PCON_DSC_ONE_16TH_BPP:
2978 case DP_PCON_DSC_ONE_8TH_BPP:
2980 case DP_PCON_DSC_ONE_4TH_BPP:
2982 case DP_PCON_DSC_ONE_HALF_BPP:
2984 case DP_PCON_DSC_ONE_BPP:
2990 EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
2993 int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
2998 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3002 buf |= DP_PCON_ENABLE_DSC_ENCODER;
3004 if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
3005 buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
3006 buf |= pps_buf_config << 2;
3009 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3017 * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
3018 * for DSC1.2 between PCON & HDMI2.1 sink
3019 * @aux: DisplayPort AUX channel
3021 * Returns 0 on success, else returns negative error code.
3023 int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
3027 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
3033 EXPORT_SYMBOL(drm_dp_pcon_pps_default);
3036 * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
3038 * @aux: DisplayPort AUX channel
3039 * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
3041 * Returns 0 on success, else returns negative error code.
3043 int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
3047 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
3051 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3057 EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
3060 * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
3061 * override registers
3062 * @aux: DisplayPort AUX channel
3063 * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
3066 * Returns 0 on success, else returns negative error code.
3068 int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
3072 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
3075 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
3078 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
3082 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3088 EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
3091 * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
3092 * @aux: displayPort AUX channel
3093 * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
3095 * Returns 0 on success, else returns negative error code.
3097 int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
3102 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3106 if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
3107 buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
3109 buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
3111 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3117 EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);