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>
36 #include <drm/drm_panel.h>
38 #include "drm_crtc_helper_internal.h"
40 struct dp_aux_backlight {
41 struct backlight_device *base;
42 struct drm_dp_aux *aux;
43 struct drm_edp_backlight_info info;
50 * These functions contain some common logic and helpers at various abstraction
51 * levels to deal with Display Port sink devices and related things like DP aux
52 * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
56 /* Helpers for DP link training */
57 static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
59 return link_status[r - DP_LANE0_1_STATUS];
62 static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
65 int i = DP_LANE0_1_STATUS + (lane >> 1);
66 int s = (lane & 1) * 4;
67 u8 l = dp_link_status(link_status, i);
69 return (l >> s) & 0xf;
72 bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
79 lane_align = dp_link_status(link_status,
80 DP_LANE_ALIGN_STATUS_UPDATED);
81 if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
83 for (lane = 0; lane < lane_count; lane++) {
84 lane_status = dp_get_lane_status(link_status, lane);
85 if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
90 EXPORT_SYMBOL(drm_dp_channel_eq_ok);
92 bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
98 for (lane = 0; lane < lane_count; lane++) {
99 lane_status = dp_get_lane_status(link_status, lane);
100 if ((lane_status & DP_LANE_CR_DONE) == 0)
105 EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
107 u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
110 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
111 int s = ((lane & 1) ?
112 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
113 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
114 u8 l = dp_link_status(link_status, i);
116 return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
118 EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
120 u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
123 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
124 int s = ((lane & 1) ?
125 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
126 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
127 u8 l = dp_link_status(link_status, i);
129 return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
131 EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
133 /* DP 2.0 128b/132b */
134 u8 drm_dp_get_adjust_tx_ffe_preset(const u8 link_status[DP_LINK_STATUS_SIZE],
137 int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
138 int s = ((lane & 1) ?
139 DP_ADJUST_TX_FFE_PRESET_LANE1_SHIFT :
140 DP_ADJUST_TX_FFE_PRESET_LANE0_SHIFT);
141 u8 l = dp_link_status(link_status, i);
143 return (l >> s) & 0xf;
145 EXPORT_SYMBOL(drm_dp_get_adjust_tx_ffe_preset);
147 u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
150 unsigned int offset = DP_ADJUST_REQUEST_POST_CURSOR2;
151 u8 value = dp_link_status(link_status, offset);
153 return (value >> (lane << 1)) & 0x3;
155 EXPORT_SYMBOL(drm_dp_get_adjust_request_post_cursor);
157 void drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux *aux,
158 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
160 unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
161 DP_TRAINING_AUX_RD_MASK;
164 drm_dbg_kms(aux->drm_dev, "%s: AUX interval %lu, out of range (max 4)\n",
165 aux->name, rd_interval);
167 if (rd_interval == 0 || dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
170 rd_interval *= 4 * USEC_PER_MSEC;
172 usleep_range(rd_interval, rd_interval * 2);
174 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
176 static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
177 unsigned long rd_interval)
180 drm_dbg_kms(aux->drm_dev, "%s: AUX interval %lu, out of range (max 4)\n",
181 aux->name, rd_interval);
183 if (rd_interval == 0)
186 rd_interval *= 4 * USEC_PER_MSEC;
188 usleep_range(rd_interval, rd_interval * 2);
191 void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
192 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
194 __drm_dp_link_train_channel_eq_delay(aux,
195 dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
196 DP_TRAINING_AUX_RD_MASK);
198 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
200 void drm_dp_lttpr_link_train_clock_recovery_delay(void)
202 usleep_range(100, 200);
204 EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
206 static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
208 return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
211 void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
212 const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
214 u8 interval = dp_lttpr_phy_cap(phy_cap,
215 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
216 DP_TRAINING_AUX_RD_MASK;
218 __drm_dp_link_train_channel_eq_delay(aux, interval);
220 EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
222 u8 drm_dp_link_rate_to_bw_code(int link_rate)
226 return DP_LINK_BW_10;
228 return DP_LINK_BW_13_5;
230 return DP_LINK_BW_20;
232 /* Spec says link_bw = link_rate / 0.27Gbps */
233 return link_rate / 27000;
236 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
238 int drm_dp_bw_code_to_link_rate(u8 link_bw)
243 case DP_LINK_BW_13_5:
248 /* Spec says link_rate = link_bw * 0.27Gbps */
249 return link_bw * 27000;
252 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
254 #define AUX_RETRY_INTERVAL 500 /* us */
257 drm_dp_dump_access(const struct drm_dp_aux *aux,
258 u8 request, uint offset, void *buffer, int ret)
260 const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
263 drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
264 aux->name, offset, arrow, ret, min(ret, 20), buffer);
266 drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",
267 aux->name, offset, arrow, ret);
273 * The DisplayPort AUX channel is an abstraction to allow generic, driver-
274 * independent access to AUX functionality. Drivers can take advantage of
275 * this by filling in the fields of the drm_dp_aux structure.
277 * Transactions are described using a hardware-independent drm_dp_aux_msg
278 * structure, which is passed into a driver's .transfer() implementation.
279 * Both native and I2C-over-AUX transactions are supported.
282 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
283 unsigned int offset, void *buffer, size_t size)
285 struct drm_dp_aux_msg msg;
286 unsigned int retry, native_reply;
287 int err = 0, ret = 0;
289 memset(&msg, 0, sizeof(msg));
290 msg.address = offset;
291 msg.request = request;
295 mutex_lock(&aux->hw_mutex);
298 * The specification doesn't give any recommendation on how often to
299 * retry native transactions. We used to retry 7 times like for
300 * aux i2c transactions but real world devices this wasn't
301 * sufficient, bump to 32 which makes Dell 4k monitors happier.
303 for (retry = 0; retry < 32; retry++) {
304 if (ret != 0 && ret != -ETIMEDOUT) {
305 usleep_range(AUX_RETRY_INTERVAL,
306 AUX_RETRY_INTERVAL + 100);
309 ret = aux->transfer(aux, &msg);
311 native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
312 if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
322 * We want the error we return to be the error we received on
323 * the first transaction, since we may get a different error the
330 drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",
335 mutex_unlock(&aux->hw_mutex);
340 * drm_dp_dpcd_read() - read a series of bytes from the DPCD
341 * @aux: DisplayPort AUX channel (SST or MST)
342 * @offset: address of the (first) register to read
343 * @buffer: buffer to store the register values
344 * @size: number of bytes in @buffer
346 * Returns the number of bytes transferred on success, or a negative error
347 * code on failure. -EIO is returned if the request was NAKed by the sink or
348 * if the retry count was exceeded. If not all bytes were transferred, this
349 * function returns -EPROTO. Errors from the underlying AUX channel transfer
350 * function, with the exception of -EBUSY (which causes the transaction to
351 * be retried), are propagated to the caller.
353 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
354 void *buffer, size_t size)
359 * HP ZR24w corrupts the first DPCD access after entering power save
360 * mode. Eg. on a read, the entire buffer will be filled with the same
361 * byte. Do a throw away read to avoid corrupting anything we care
362 * about. Afterwards things will work correctly until the monitor
363 * gets woken up and subsequently re-enters power save mode.
365 * The user pressing any button on the monitor is enough to wake it
366 * up, so there is no particularly good place to do the workaround.
367 * We just have to do it before any DPCD access and hope that the
368 * monitor doesn't power down exactly after the throw away read.
370 if (!aux->is_remote) {
371 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
378 ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
380 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
384 drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
387 EXPORT_SYMBOL(drm_dp_dpcd_read);
390 * drm_dp_dpcd_write() - write a series of bytes to the DPCD
391 * @aux: DisplayPort AUX channel (SST or MST)
392 * @offset: address of the (first) register to write
393 * @buffer: buffer containing the values to write
394 * @size: number of bytes in @buffer
396 * Returns the number of bytes transferred on success, or a negative error
397 * code on failure. -EIO is returned if the request was NAKed by the sink or
398 * if the retry count was exceeded. If not all bytes were transferred, this
399 * function returns -EPROTO. Errors from the underlying AUX channel transfer
400 * function, with the exception of -EBUSY (which causes the transaction to
401 * be retried), are propagated to the caller.
403 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
404 void *buffer, size_t size)
409 ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
411 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
414 drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
417 EXPORT_SYMBOL(drm_dp_dpcd_write);
420 * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
421 * @aux: DisplayPort AUX channel
422 * @status: buffer to store the link status in (must be at least 6 bytes)
424 * Returns the number of bytes transferred on success or a negative error
427 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
428 u8 status[DP_LINK_STATUS_SIZE])
430 return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
431 DP_LINK_STATUS_SIZE);
433 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
436 * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
437 * @aux: DisplayPort AUX channel
438 * @dp_phy: the DP PHY to get the link status for
439 * @link_status: buffer to return the status in
441 * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
442 * layout of the returned @link_status matches the DPCD register layout of the
443 * DPRX PHY link status.
445 * Returns 0 if the information was read successfully or a negative error code
448 int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
449 enum drm_dp_phy dp_phy,
450 u8 link_status[DP_LINK_STATUS_SIZE])
454 if (dp_phy == DP_PHY_DPRX) {
455 ret = drm_dp_dpcd_read(aux,
458 DP_LINK_STATUS_SIZE);
463 WARN_ON(ret != DP_LINK_STATUS_SIZE);
468 ret = drm_dp_dpcd_read(aux,
469 DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
471 DP_LINK_STATUS_SIZE - 1);
476 WARN_ON(ret != DP_LINK_STATUS_SIZE - 1);
478 /* Convert the LTTPR to the sink PHY link status layout */
479 memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
480 &link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
481 DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
482 link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
486 EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
488 static bool is_edid_digital_input_dp(const struct edid *edid)
490 return edid && edid->revision >= 4 &&
491 edid->input & DRM_EDID_INPUT_DIGITAL &&
492 (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
496 * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
497 * @dpcd: DisplayPort configuration data
498 * @port_cap: port capabilities
499 * @type: port type to be checked. Can be:
500 * %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
501 * %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
502 * %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
504 * Caveat: Only works with DPCD 1.1+ port caps.
506 * Returns: whether the downstream facing port matches the type.
508 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
509 const u8 port_cap[4], u8 type)
511 return drm_dp_is_branch(dpcd) &&
512 dpcd[DP_DPCD_REV] >= 0x11 &&
513 (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
515 EXPORT_SYMBOL(drm_dp_downstream_is_type);
518 * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
519 * @dpcd: DisplayPort configuration data
520 * @port_cap: port capabilities
523 * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
525 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
526 const u8 port_cap[4],
527 const struct edid *edid)
529 if (dpcd[DP_DPCD_REV] < 0x11) {
530 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
531 case DP_DWN_STRM_PORT_TYPE_TMDS:
538 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
539 case DP_DS_PORT_TYPE_DP_DUALMODE:
540 if (is_edid_digital_input_dp(edid))
543 case DP_DS_PORT_TYPE_DVI:
544 case DP_DS_PORT_TYPE_HDMI:
550 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
553 * drm_dp_send_real_edid_checksum() - send back real edid checksum value
554 * @aux: DisplayPort AUX channel
555 * @real_edid_checksum: real edid checksum for the last block
560 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
561 u8 real_edid_checksum)
563 u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
565 if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
566 &auto_test_req, 1) < 1) {
567 drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
568 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
571 auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
573 if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
574 drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
575 aux->name, DP_TEST_REQUEST);
578 link_edid_read &= DP_TEST_LINK_EDID_READ;
580 if (!auto_test_req || !link_edid_read) {
581 drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",
586 if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
587 &auto_test_req, 1) < 1) {
588 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
589 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
593 /* send back checksum for the last edid extension block data */
594 if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
595 &real_edid_checksum, 1) < 1) {
596 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
597 aux->name, DP_TEST_EDID_CHECKSUM);
601 test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
602 if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
603 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
604 aux->name, DP_TEST_RESPONSE);
610 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
612 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
614 u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
616 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
622 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
623 u8 dpcd[DP_RECEIVER_CAP_SIZE])
625 u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
629 * Prior to DP1.3 the bit represented by
630 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
631 * If it is set DP_DPCD_REV at 0000h could be at a value less than
632 * the true capability of the panel. The only way to check is to
633 * then compare 0000h and 2200h.
635 if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
636 DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
639 ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
643 if (ret != sizeof(dpcd_ext))
646 if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
647 drm_dbg_kms(aux->drm_dev,
648 "%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
649 aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
653 if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
656 drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
658 memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
664 * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
666 * @aux: DisplayPort AUX channel
667 * @dpcd: Buffer to store the resulting DPCD in
669 * Attempts to read the base DPCD caps for @aux. Additionally, this function
670 * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
673 * Returns: %0 if the DPCD was read successfully, negative error code
676 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
677 u8 dpcd[DP_RECEIVER_CAP_SIZE])
681 ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
684 if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
687 ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
691 drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
695 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
698 * drm_dp_read_downstream_info() - read DPCD downstream port info if available
699 * @aux: DisplayPort AUX channel
700 * @dpcd: A cached copy of the port's DPCD
701 * @downstream_ports: buffer to store the downstream port info in
704 * drm_dp_downstream_max_clock()
705 * drm_dp_downstream_max_bpc()
707 * Returns: 0 if either the downstream port info was read successfully or
708 * there was no downstream info to read, or a negative error code otherwise.
710 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
711 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
712 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
717 memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
719 /* No downstream info to read */
720 if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)
723 /* Some branches advertise having 0 downstream ports, despite also advertising they have a
724 * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
725 * some branches do it we need to handle it regardless.
727 len = drm_dp_downstream_port_count(dpcd);
731 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
734 ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
740 drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);
744 EXPORT_SYMBOL(drm_dp_read_downstream_info);
747 * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
748 * @dpcd: DisplayPort configuration data
749 * @port_cap: port capabilities
751 * Returns: Downstream facing port max dot clock in kHz on success,
752 * or 0 if max clock not defined
754 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
755 const u8 port_cap[4])
757 if (!drm_dp_is_branch(dpcd))
760 if (dpcd[DP_DPCD_REV] < 0x11)
763 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
764 case DP_DS_PORT_TYPE_VGA:
765 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
767 return port_cap[1] * 8000;
772 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
775 * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
776 * @dpcd: DisplayPort configuration data
777 * @port_cap: port capabilities
780 * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
781 * or 0 if max TMDS clock not defined
783 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
784 const u8 port_cap[4],
785 const struct edid *edid)
787 if (!drm_dp_is_branch(dpcd))
790 if (dpcd[DP_DPCD_REV] < 0x11) {
791 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
792 case DP_DWN_STRM_PORT_TYPE_TMDS:
799 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
800 case DP_DS_PORT_TYPE_DP_DUALMODE:
801 if (is_edid_digital_input_dp(edid))
804 * It's left up to the driver to check the
805 * DP dual mode adapter's max TMDS clock.
807 * Unfortunately it looks like branch devices
808 * may not fordward that the DP dual mode i2c
809 * access so we just usually get i2c nak :(
812 case DP_DS_PORT_TYPE_HDMI:
814 * We should perhaps assume 165 MHz when detailed cap
815 * info is not available. But looks like many typical
816 * branch devices fall into that category and so we'd
817 * probably end up with users complaining that they can't
818 * get high resolution modes with their favorite dongle.
820 * So let's limit to 300 MHz instead since DPCD 1.4
821 * HDMI 2.0 DFPs are required to have the detailed cap
822 * info. So it's more likely we're dealing with a HDMI 1.4
823 * compatible* device here.
825 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
827 return port_cap[1] * 2500;
828 case DP_DS_PORT_TYPE_DVI:
829 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
831 /* FIXME what to do about DVI dual link? */
832 return port_cap[1] * 2500;
837 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
840 * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
841 * @dpcd: DisplayPort configuration data
842 * @port_cap: port capabilities
845 * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
846 * or 0 if max TMDS clock not defined
848 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
849 const u8 port_cap[4],
850 const struct edid *edid)
852 if (!drm_dp_is_branch(dpcd))
855 if (dpcd[DP_DPCD_REV] < 0x11) {
856 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
857 case DP_DWN_STRM_PORT_TYPE_TMDS:
864 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
865 case DP_DS_PORT_TYPE_DP_DUALMODE:
866 if (is_edid_digital_input_dp(edid))
869 case DP_DS_PORT_TYPE_DVI:
870 case DP_DS_PORT_TYPE_HDMI:
872 * Unclear whether the protocol converter could
873 * utilize pixel replication. Assume it won't.
880 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
883 * drm_dp_downstream_max_bpc() - extract downstream facing port max
885 * @dpcd: DisplayPort configuration data
886 * @port_cap: downstream facing port capabilities
889 * Returns: Max bpc on success or 0 if max bpc not defined
891 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
892 const u8 port_cap[4],
893 const struct edid *edid)
895 if (!drm_dp_is_branch(dpcd))
898 if (dpcd[DP_DPCD_REV] < 0x11) {
899 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
900 case DP_DWN_STRM_PORT_TYPE_DP:
907 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
908 case DP_DS_PORT_TYPE_DP:
910 case DP_DS_PORT_TYPE_DP_DUALMODE:
911 if (is_edid_digital_input_dp(edid))
914 case DP_DS_PORT_TYPE_HDMI:
915 case DP_DS_PORT_TYPE_DVI:
916 case DP_DS_PORT_TYPE_VGA:
917 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
920 switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
937 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
940 * drm_dp_downstream_420_passthrough() - determine downstream facing port
941 * YCbCr 4:2:0 pass-through capability
942 * @dpcd: DisplayPort configuration data
943 * @port_cap: downstream facing port capabilities
945 * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
947 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
948 const u8 port_cap[4])
950 if (!drm_dp_is_branch(dpcd))
953 if (dpcd[DP_DPCD_REV] < 0x13)
956 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
957 case DP_DS_PORT_TYPE_DP:
959 case DP_DS_PORT_TYPE_HDMI:
960 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
963 return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
968 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
971 * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
972 * YCbCr 4:4:4->4:2:0 conversion capability
973 * @dpcd: DisplayPort configuration data
974 * @port_cap: downstream facing port capabilities
976 * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
978 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
979 const u8 port_cap[4])
981 if (!drm_dp_is_branch(dpcd))
984 if (dpcd[DP_DPCD_REV] < 0x13)
987 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
988 case DP_DS_PORT_TYPE_HDMI:
989 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
992 return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
997 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
1000 * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
1001 * RGB->YCbCr conversion capability
1002 * @dpcd: DisplayPort configuration data
1003 * @port_cap: downstream facing port capabilities
1004 * @color_spc: Colorspace for which conversion cap is sought
1006 * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
1009 bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1010 const u8 port_cap[4],
1013 if (!drm_dp_is_branch(dpcd))
1016 if (dpcd[DP_DPCD_REV] < 0x13)
1019 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1020 case DP_DS_PORT_TYPE_HDMI:
1021 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1024 return port_cap[3] & color_spc;
1029 EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
1032 * drm_dp_downstream_mode() - return a mode for downstream facing port
1034 * @dpcd: DisplayPort configuration data
1035 * @port_cap: port capabilities
1037 * Provides a suitable mode for downstream facing ports without EDID.
1039 * Returns: A new drm_display_mode on success or NULL on failure
1041 struct drm_display_mode *
1042 drm_dp_downstream_mode(struct drm_device *dev,
1043 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1044 const u8 port_cap[4])
1049 if (!drm_dp_is_branch(dpcd))
1052 if (dpcd[DP_DPCD_REV] < 0x11)
1055 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1056 case DP_DS_PORT_TYPE_NON_EDID:
1057 switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
1058 case DP_DS_NON_EDID_720x480i_60:
1061 case DP_DS_NON_EDID_720x480i_50:
1064 case DP_DS_NON_EDID_1920x1080i_60:
1067 case DP_DS_NON_EDID_1920x1080i_50:
1070 case DP_DS_NON_EDID_1280x720_60:
1073 case DP_DS_NON_EDID_1280x720_50:
1079 return drm_display_mode_from_cea_vic(dev, vic);
1084 EXPORT_SYMBOL(drm_dp_downstream_mode);
1087 * drm_dp_downstream_id() - identify branch device
1088 * @aux: DisplayPort AUX channel
1089 * @id: DisplayPort branch device id
1091 * Returns branch device id on success or NULL on failure
1093 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
1095 return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
1097 EXPORT_SYMBOL(drm_dp_downstream_id);
1100 * drm_dp_downstream_debug() - debug DP branch devices
1101 * @m: pointer for debugfs file
1102 * @dpcd: DisplayPort configuration data
1103 * @port_cap: port capabilities
1105 * @aux: DisplayPort AUX channel
1108 void drm_dp_downstream_debug(struct seq_file *m,
1109 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1110 const u8 port_cap[4],
1111 const struct edid *edid,
1112 struct drm_dp_aux *aux)
1114 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1115 DP_DETAILED_CAP_INFO_AVAILABLE;
1121 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1122 bool branch_device = drm_dp_is_branch(dpcd);
1124 seq_printf(m, "\tDP branch device present: %s\n",
1125 branch_device ? "yes" : "no");
1131 case DP_DS_PORT_TYPE_DP:
1132 seq_puts(m, "\t\tType: DisplayPort\n");
1134 case DP_DS_PORT_TYPE_VGA:
1135 seq_puts(m, "\t\tType: VGA\n");
1137 case DP_DS_PORT_TYPE_DVI:
1138 seq_puts(m, "\t\tType: DVI\n");
1140 case DP_DS_PORT_TYPE_HDMI:
1141 seq_puts(m, "\t\tType: HDMI\n");
1143 case DP_DS_PORT_TYPE_NON_EDID:
1144 seq_puts(m, "\t\tType: others without EDID support\n");
1146 case DP_DS_PORT_TYPE_DP_DUALMODE:
1147 seq_puts(m, "\t\tType: DP++\n");
1149 case DP_DS_PORT_TYPE_WIRELESS:
1150 seq_puts(m, "\t\tType: Wireless\n");
1153 seq_puts(m, "\t\tType: N/A\n");
1156 memset(id, 0, sizeof(id));
1157 drm_dp_downstream_id(aux, id);
1158 seq_printf(m, "\t\tID: %s\n", id);
1160 len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1162 seq_printf(m, "\t\tHW: %d.%d\n",
1163 (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1165 len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1167 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1169 if (detailed_cap_info) {
1170 clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1172 seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1174 clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1176 seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1178 clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1180 seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1182 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1185 seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1188 EXPORT_SYMBOL(drm_dp_downstream_debug);
1191 * drm_dp_subconnector_type() - get DP branch device type
1192 * @dpcd: DisplayPort configuration data
1193 * @port_cap: port capabilities
1195 enum drm_mode_subconnector
1196 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1197 const u8 port_cap[4])
1200 if (!drm_dp_is_branch(dpcd))
1201 return DRM_MODE_SUBCONNECTOR_Native;
1202 /* DP 1.0 approach */
1203 if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1204 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1205 DP_DWN_STRM_PORT_TYPE_MASK;
1208 case DP_DWN_STRM_PORT_TYPE_TMDS:
1209 /* Can be HDMI or DVI-D, DVI-D is a safer option */
1210 return DRM_MODE_SUBCONNECTOR_DVID;
1211 case DP_DWN_STRM_PORT_TYPE_ANALOG:
1212 /* Can be VGA or DVI-A, VGA is more popular */
1213 return DRM_MODE_SUBCONNECTOR_VGA;
1214 case DP_DWN_STRM_PORT_TYPE_DP:
1215 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1216 case DP_DWN_STRM_PORT_TYPE_OTHER:
1218 return DRM_MODE_SUBCONNECTOR_Unknown;
1221 type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1224 case DP_DS_PORT_TYPE_DP:
1225 case DP_DS_PORT_TYPE_DP_DUALMODE:
1226 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1227 case DP_DS_PORT_TYPE_VGA:
1228 return DRM_MODE_SUBCONNECTOR_VGA;
1229 case DP_DS_PORT_TYPE_DVI:
1230 return DRM_MODE_SUBCONNECTOR_DVID;
1231 case DP_DS_PORT_TYPE_HDMI:
1232 return DRM_MODE_SUBCONNECTOR_HDMIA;
1233 case DP_DS_PORT_TYPE_WIRELESS:
1234 return DRM_MODE_SUBCONNECTOR_Wireless;
1235 case DP_DS_PORT_TYPE_NON_EDID:
1237 return DRM_MODE_SUBCONNECTOR_Unknown;
1240 EXPORT_SYMBOL(drm_dp_subconnector_type);
1243 * drm_dp_set_subconnector_property - set subconnector for DP connector
1244 * @connector: connector to set property on
1245 * @status: connector status
1246 * @dpcd: DisplayPort configuration data
1247 * @port_cap: port capabilities
1249 * Called by a driver on every detect event.
1251 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1252 enum drm_connector_status status,
1254 const u8 port_cap[4])
1256 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1258 if (status == connector_status_connected)
1259 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1260 drm_object_property_set_value(&connector->base,
1261 connector->dev->mode_config.dp_subconnector_property,
1264 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1267 * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1269 * @connector: The DRM connector to check
1270 * @dpcd: A cached copy of the connector's DPCD RX capabilities
1271 * @desc: A cached copy of the connector's DP descriptor
1273 * See also: drm_dp_read_sink_count()
1275 * Returns: %True if the (e)DP connector has a valid sink count that should
1276 * be probed, %false otherwise.
1278 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1279 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1280 const struct drm_dp_desc *desc)
1282 /* Some eDP panels don't set a valid value for the sink count */
1283 return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1284 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1285 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1286 !drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);
1288 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1291 * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1292 * @aux: The DP AUX channel to use
1294 * See also: drm_dp_read_sink_count_cap()
1296 * Returns: The current sink count reported by @aux, or a negative error code
1299 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1304 ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1310 return DP_GET_SINK_COUNT(count);
1312 EXPORT_SYMBOL(drm_dp_read_sink_count);
1315 * I2C-over-AUX implementation
1318 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1320 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1321 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1322 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1323 I2C_FUNC_10BIT_ADDR;
1326 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1329 * In case of i2c defer or short i2c ack reply to a write,
1330 * we need to switch to WRITE_STATUS_UPDATE to drain the
1331 * rest of the message
1333 if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1334 msg->request &= DP_AUX_I2C_MOT;
1335 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1339 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1340 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1341 #define AUX_STOP_LEN 4
1342 #define AUX_CMD_LEN 4
1343 #define AUX_ADDRESS_LEN 20
1344 #define AUX_REPLY_PAD_LEN 4
1345 #define AUX_LENGTH_LEN 8
1348 * Calculate the duration of the AUX request/reply in usec. Gives the
1349 * "best" case estimate, ie. successful while as short as possible.
1351 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1353 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1354 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1356 if ((msg->request & DP_AUX_I2C_READ) == 0)
1357 len += msg->size * 8;
1362 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1364 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1365 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1368 * For read we expect what was asked. For writes there will
1369 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1371 if (msg->request & DP_AUX_I2C_READ)
1372 len += msg->size * 8;
1377 #define I2C_START_LEN 1
1378 #define I2C_STOP_LEN 1
1379 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1380 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1383 * Calculate the length of the i2c transfer in usec, assuming
1384 * the i2c bus speed is as specified. Gives the the "worst"
1385 * case estimate, ie. successful while as long as possible.
1386 * Doesn't account the the "MOT" bit, and instead assumes each
1387 * message includes a START, ADDRESS and STOP. Neither does it
1388 * account for additional random variables such as clock stretching.
1390 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1393 /* AUX bitrate is 1MHz, i2c bitrate as specified */
1394 return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1395 msg->size * I2C_DATA_LEN +
1396 I2C_STOP_LEN) * 1000, i2c_speed_khz);
1400 * Determine how many retries should be attempted to successfully transfer
1401 * the specified message, based on the estimated durations of the
1402 * i2c and AUX transfers.
1404 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1407 int aux_time_us = drm_dp_aux_req_duration(msg) +
1408 drm_dp_aux_reply_duration(msg);
1409 int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1411 return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1415 * FIXME currently assumes 10 kHz as some real world devices seem
1416 * to require it. We should query/set the speed via DPCD if supported.
1418 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1419 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1420 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1421 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1424 * Transfer a single I2C-over-AUX message and handle various error conditions,
1425 * retrying the transaction as appropriate. It is assumed that the
1426 * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1429 * Returns bytes transferred on success, or a negative error code on failure.
1431 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1433 unsigned int retry, defer_i2c;
1436 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1437 * is required to retry at least seven times upon receiving AUX_DEFER
1438 * before giving up the AUX transaction.
1440 * We also try to account for the i2c bus speed.
1442 int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1444 for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1445 ret = aux->transfer(aux, msg);
1451 * While timeouts can be errors, they're usually normal
1452 * behavior (for instance, when a driver tries to
1453 * communicate with a non-existent DisplayPort device).
1454 * Avoid spamming the kernel log with timeout errors.
1456 if (ret == -ETIMEDOUT)
1457 drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",
1460 drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",
1466 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1467 case DP_AUX_NATIVE_REPLY_ACK:
1469 * For I2C-over-AUX transactions this isn't enough, we
1470 * need to check for the I2C ACK reply.
1474 case DP_AUX_NATIVE_REPLY_NACK:
1475 drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",
1476 aux->name, ret, msg->size);
1479 case DP_AUX_NATIVE_REPLY_DEFER:
1480 drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);
1482 * We could check for I2C bit rate capabilities and if
1483 * available adjust this interval. We could also be
1484 * more careful with DP-to-legacy adapters where a
1485 * long legacy cable may force very low I2C bit rates.
1487 * For now just defer for long enough to hopefully be
1488 * safe for all use-cases.
1490 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1494 drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",
1495 aux->name, msg->reply);
1499 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1500 case DP_AUX_I2C_REPLY_ACK:
1502 * Both native ACK and I2C ACK replies received. We
1503 * can assume the transfer was successful.
1505 if (ret != msg->size)
1506 drm_dp_i2c_msg_write_status_update(msg);
1509 case DP_AUX_I2C_REPLY_NACK:
1510 drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",
1511 aux->name, ret, msg->size);
1512 aux->i2c_nack_count++;
1515 case DP_AUX_I2C_REPLY_DEFER:
1516 drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);
1517 /* DP Compliance Test 4.2.2.5 Requirement:
1518 * Must have at least 7 retries for I2C defers on the
1519 * transaction to pass this test
1521 aux->i2c_defer_count++;
1524 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1525 drm_dp_i2c_msg_write_status_update(msg);
1530 drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",
1531 aux->name, msg->reply);
1536 drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);
1540 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1541 const struct i2c_msg *i2c_msg)
1543 msg->request = (i2c_msg->flags & I2C_M_RD) ?
1544 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1545 if (!(i2c_msg->flags & I2C_M_STOP))
1546 msg->request |= DP_AUX_I2C_MOT;
1550 * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1552 * Returns an error code on failure, or a recommended transfer size on success.
1554 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1556 int err, ret = orig_msg->size;
1557 struct drm_dp_aux_msg msg = *orig_msg;
1559 while (msg.size > 0) {
1560 err = drm_dp_i2c_do_msg(aux, &msg);
1562 return err == 0 ? -EPROTO : err;
1564 if (err < msg.size && err < ret) {
1565 drm_dbg_kms(aux->drm_dev,
1566 "%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1567 aux->name, msg.size, err);
1579 * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1580 * packets to be as large as possible. If not, the I2C transactions never
1581 * succeed. Hence the default is maximum.
1583 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1584 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1585 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1586 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1588 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1591 struct drm_dp_aux *aux = adapter->algo_data;
1593 unsigned transfer_size;
1594 struct drm_dp_aux_msg msg;
1597 dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1599 memset(&msg, 0, sizeof(msg));
1601 for (i = 0; i < num; i++) {
1602 msg.address = msgs[i].addr;
1603 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1604 /* Send a bare address packet to start the transaction.
1605 * Zero sized messages specify an address only (bare
1606 * address) transaction.
1610 err = drm_dp_i2c_do_msg(aux, &msg);
1613 * Reset msg.request in case in case it got
1614 * changed into a WRITE_STATUS_UPDATE.
1616 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1620 /* We want each transaction to be as large as possible, but
1621 * we'll go to smaller sizes if the hardware gives us a
1624 transfer_size = dp_aux_i2c_transfer_size;
1625 for (j = 0; j < msgs[i].len; j += msg.size) {
1626 msg.buffer = msgs[i].buf + j;
1627 msg.size = min(transfer_size, msgs[i].len - j);
1629 err = drm_dp_i2c_drain_msg(aux, &msg);
1632 * Reset msg.request in case in case it got
1633 * changed into a WRITE_STATUS_UPDATE.
1635 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1639 transfer_size = err;
1646 /* Send a bare address packet to close out the transaction.
1647 * Zero sized messages specify an address only (bare
1648 * address) transaction.
1650 msg.request &= ~DP_AUX_I2C_MOT;
1653 (void)drm_dp_i2c_do_msg(aux, &msg);
1658 static const struct i2c_algorithm drm_dp_i2c_algo = {
1659 .functionality = drm_dp_i2c_functionality,
1660 .master_xfer = drm_dp_i2c_xfer,
1663 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1665 return container_of(i2c, struct drm_dp_aux, ddc);
1668 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1670 mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1673 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1675 return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1678 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1680 mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1683 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1684 .lock_bus = lock_bus,
1685 .trylock_bus = trylock_bus,
1686 .unlock_bus = unlock_bus,
1689 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1694 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1698 WARN_ON(!(buf & DP_TEST_SINK_START));
1700 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1704 count = buf & DP_TEST_COUNT_MASK;
1705 if (count == aux->crc_count)
1706 return -EAGAIN; /* No CRC yet */
1708 aux->crc_count = count;
1711 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1712 * per component (RGB or CrYCb).
1714 ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1721 static void drm_dp_aux_crc_work(struct work_struct *work)
1723 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1725 struct drm_crtc *crtc;
1730 if (WARN_ON(!aux->crtc))
1734 while (crtc->crc.opened) {
1735 drm_crtc_wait_one_vblank(crtc);
1736 if (!crtc->crc.opened)
1739 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1740 if (ret == -EAGAIN) {
1741 usleep_range(1000, 2000);
1742 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1745 if (ret == -EAGAIN) {
1746 drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",
1750 drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);
1754 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1755 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1756 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1757 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1762 * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1763 * @aux: DisplayPort AUX channel
1765 * Used for remote aux channel in general. Merely initialize the crc work
1768 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1770 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1772 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1775 * drm_dp_aux_init() - minimally initialise an aux channel
1776 * @aux: DisplayPort AUX channel
1778 * If you need to use the drm_dp_aux's i2c adapter prior to registering it with
1779 * the outside world, call drm_dp_aux_init() first. For drivers which are
1780 * grandparents to their AUX adapters (e.g. the AUX adapter is parented by a
1781 * &drm_connector), you must still call drm_dp_aux_register() once the connector
1782 * has been registered to allow userspace access to the auxiliary DP channel.
1783 * Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as
1784 * early as possible so that the &drm_device that corresponds to the AUX adapter
1785 * may be mentioned in debugging output from the DRM DP helpers.
1787 * For devices which use a separate platform device for their AUX adapters, this
1788 * may be called as early as required by the driver.
1791 void drm_dp_aux_init(struct drm_dp_aux *aux)
1793 mutex_init(&aux->hw_mutex);
1794 mutex_init(&aux->cec.lock);
1795 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1797 aux->ddc.algo = &drm_dp_i2c_algo;
1798 aux->ddc.algo_data = aux;
1799 aux->ddc.retries = 3;
1801 aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1803 EXPORT_SYMBOL(drm_dp_aux_init);
1806 * drm_dp_aux_register() - initialise and register aux channel
1807 * @aux: DisplayPort AUX channel
1809 * Automatically calls drm_dp_aux_init() if this hasn't been done yet. This
1810 * should only be called once the parent of @aux, &drm_dp_aux.dev, is
1811 * initialized. For devices which are grandparents of their AUX channels,
1812 * &drm_dp_aux.dev will typically be the &drm_connector &device which
1813 * corresponds to @aux. For these devices, it's advised to call
1814 * drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to
1815 * call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.
1816 * Functions which don't follow this will likely Oops when
1817 * %CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1819 * For devices where the AUX channel is a device that exists independently of
1820 * the &drm_device that uses it, such as SoCs and bridge devices, it is
1821 * recommended to call drm_dp_aux_register() after a &drm_device has been
1822 * assigned to &drm_dp_aux.drm_dev, and likewise to call
1823 * drm_dp_aux_unregister() once the &drm_device should no longer be associated
1824 * with the AUX channel (e.g. on bridge detach).
1826 * Drivers which need to use the aux channel before either of the two points
1827 * mentioned above need to call drm_dp_aux_init() in order to use the AUX
1828 * channel before registration.
1830 * Returns 0 on success or a negative error code on failure.
1832 int drm_dp_aux_register(struct drm_dp_aux *aux)
1836 WARN_ON_ONCE(!aux->drm_dev);
1839 drm_dp_aux_init(aux);
1841 aux->ddc.class = I2C_CLASS_DDC;
1842 aux->ddc.owner = THIS_MODULE;
1843 aux->ddc.dev.parent = aux->dev;
1845 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1846 sizeof(aux->ddc.name));
1848 ret = drm_dp_aux_register_devnode(aux);
1852 ret = i2c_add_adapter(&aux->ddc);
1854 drm_dp_aux_unregister_devnode(aux);
1860 EXPORT_SYMBOL(drm_dp_aux_register);
1863 * drm_dp_aux_unregister() - unregister an AUX adapter
1864 * @aux: DisplayPort AUX channel
1866 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1868 drm_dp_aux_unregister_devnode(aux);
1869 i2c_del_adapter(&aux->ddc);
1871 EXPORT_SYMBOL(drm_dp_aux_unregister);
1873 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1876 * drm_dp_psr_setup_time() - PSR setup in time usec
1877 * @psr_cap: PSR capabilities from DPCD
1880 * PSR setup time for the panel in microseconds, negative
1881 * error code on failure.
1883 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1885 static const u16 psr_setup_time_us[] = {
1886 PSR_SETUP_TIME(330),
1887 PSR_SETUP_TIME(275),
1888 PSR_SETUP_TIME(220),
1889 PSR_SETUP_TIME(165),
1890 PSR_SETUP_TIME(110),
1896 i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1897 if (i >= ARRAY_SIZE(psr_setup_time_us))
1900 return psr_setup_time_us[i];
1902 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1904 #undef PSR_SETUP_TIME
1907 * drm_dp_start_crc() - start capture of frame CRCs
1908 * @aux: DisplayPort AUX channel
1909 * @crtc: CRTC displaying the frames whose CRCs are to be captured
1911 * Returns 0 on success or a negative error code on failure.
1913 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1918 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1922 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1928 schedule_work(&aux->crc_work);
1932 EXPORT_SYMBOL(drm_dp_start_crc);
1935 * drm_dp_stop_crc() - stop capture of frame CRCs
1936 * @aux: DisplayPort AUX channel
1938 * Returns 0 on success or a negative error code on failure.
1940 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1945 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1949 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1953 flush_work(&aux->crc_work);
1958 EXPORT_SYMBOL(drm_dp_stop_crc);
1967 #define OUI(first, second, third) { (first), (second), (third) }
1968 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1969 { (first), (second), (third), (fourth), (fifth), (sixth) }
1971 #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
1973 static const struct dpcd_quirk dpcd_quirk_list[] = {
1974 /* Analogix 7737 needs reduced M and N at HBR2 link rates */
1975 { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1976 /* LG LP140WF6-SPM1 eDP panel */
1977 { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1978 /* Apple panels need some additional handling to support PSR */
1979 { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1980 /* CH7511 seems to leave SINK_COUNT zeroed */
1981 { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1982 /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1983 { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1984 /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1985 { OUI(0x00, 0x10, 0xfa), DEVICE_ID(101, 68, 21, 101, 98, 97), false, BIT(DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS) },
1991 * Get a bit mask of DPCD quirks for the sink/branch device identified by
1992 * ident. The quirk data is shared but it's up to the drivers to act on the
1995 * For now, only the OUI (first three bytes) is used, but this may be extended
1996 * to device identification string and hardware/firmware revisions later.
1999 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
2001 const struct dpcd_quirk *quirk;
2004 u8 any_device[] = DEVICE_ID_ANY;
2006 for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
2007 quirk = &dpcd_quirk_list[i];
2009 if (quirk->is_branch != is_branch)
2012 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
2015 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
2016 memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
2019 quirks |= quirk->quirks;
2025 #undef DEVICE_ID_ANY
2029 * drm_dp_read_desc - read sink/branch descriptor from DPCD
2030 * @aux: DisplayPort AUX channel
2031 * @desc: Device descriptor to fill from DPCD
2032 * @is_branch: true for branch devices, false for sink devices
2034 * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
2037 * Returns 0 on success or a negative error code on failure.
2039 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
2042 struct drm_dp_dpcd_ident *ident = &desc->ident;
2043 unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
2044 int ret, dev_id_len;
2046 ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
2050 desc->quirks = drm_dp_get_quirks(ident, is_branch);
2052 dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
2054 drm_dbg_kms(aux->drm_dev,
2055 "%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
2056 aux->name, is_branch ? "branch" : "sink",
2057 (int)sizeof(ident->oui), ident->oui, dev_id_len,
2058 ident->device_id, ident->hw_rev >> 4, ident->hw_rev & 0xf,
2059 ident->sw_major_rev, ident->sw_minor_rev, desc->quirks);
2063 EXPORT_SYMBOL(drm_dp_read_desc);
2066 * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
2067 * supported by the DSC sink.
2068 * @dsc_dpcd: DSC capabilities from DPCD
2069 * @is_edp: true if its eDP, false for DP
2071 * Read the slice capabilities DPCD register from DSC sink to get
2072 * the maximum slice count supported. This is used to populate
2073 * the DSC parameters in the &struct drm_dsc_config by the driver.
2074 * Driver creates an infoframe using these parameters to populate
2075 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2076 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2079 * Maximum slice count supported by DSC sink or 0 its invalid
2081 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2084 u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
2087 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
2088 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2090 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2092 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2095 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2096 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2098 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2100 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2102 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2104 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2106 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2108 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2110 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2112 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2114 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2116 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2122 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2125 * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2126 * @dsc_dpcd: DSC capabilities from DPCD
2128 * Read the DSC DPCD register to parse the line buffer depth in bits which is
2129 * number of bits of precision within the decoder line buffer supported by
2130 * the DSC sink. This is used to populate the DSC parameters in the
2131 * &struct drm_dsc_config by the driver.
2132 * Driver creates an infoframe using these parameters to populate
2133 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2134 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2137 * Line buffer depth supported by DSC panel or 0 its invalid
2139 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2141 u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2143 switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2144 case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2146 case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2148 case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2150 case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2152 case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2154 case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2156 case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2158 case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2160 case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2166 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2169 * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2170 * values supported by the DSC sink.
2171 * @dsc_dpcd: DSC capabilities from DPCD
2172 * @dsc_bpc: An array to be filled by this helper with supported
2175 * Read the DSC DPCD from the sink device to parse the supported bits per
2176 * component values. This is used to populate the DSC parameters
2177 * in the &struct drm_dsc_config by the driver.
2178 * Driver creates an infoframe using these parameters to populate
2179 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2180 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2183 * Number of input BPC values parsed from the DPCD
2185 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2189 u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2191 if (color_depth & DP_DSC_12_BPC)
2192 dsc_bpc[num_bpc++] = 12;
2193 if (color_depth & DP_DSC_10_BPC)
2194 dsc_bpc[num_bpc++] = 10;
2195 if (color_depth & DP_DSC_8_BPC)
2196 dsc_bpc[num_bpc++] = 8;
2200 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2203 * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
2204 * @aux: DisplayPort AUX channel
2205 * @caps: buffer to return the capability info in
2207 * Read capabilities common to all LTTPRs.
2209 * Returns 0 on success or a negative error code on failure.
2211 int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
2212 u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2216 ret = drm_dp_dpcd_read(aux,
2217 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
2218 caps, DP_LTTPR_COMMON_CAP_SIZE);
2222 WARN_ON(ret != DP_LTTPR_COMMON_CAP_SIZE);
2226 EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
2229 * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
2230 * @aux: DisplayPort AUX channel
2231 * @dp_phy: LTTPR PHY to read the capabilities for
2232 * @caps: buffer to return the capability info in
2234 * Read the capabilities for the given LTTPR PHY.
2236 * Returns 0 on success or a negative error code on failure.
2238 int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
2239 enum drm_dp_phy dp_phy,
2240 u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2244 ret = drm_dp_dpcd_read(aux,
2245 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
2246 caps, DP_LTTPR_PHY_CAP_SIZE);
2250 WARN_ON(ret != DP_LTTPR_PHY_CAP_SIZE);
2254 EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
2256 static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
2258 return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
2262 * drm_dp_lttpr_count - get the number of detected LTTPRs
2263 * @caps: LTTPR common capabilities
2265 * Get the number of detected LTTPRs from the LTTPR common capabilities info.
2268 * -ERANGE if more than supported number (8) of LTTPRs are detected
2269 * -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
2270 * otherwise the number of detected LTTPRs
2272 int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2274 u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
2276 switch (hweight8(count)) {
2280 return 8 - ilog2(count);
2287 EXPORT_SYMBOL(drm_dp_lttpr_count);
2290 * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
2291 * @caps: LTTPR common capabilities
2293 * Returns the maximum link rate supported by all detected LTTPRs.
2295 int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2297 u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
2299 return drm_dp_bw_code_to_link_rate(rate);
2301 EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
2304 * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
2305 * @caps: LTTPR common capabilities
2307 * Returns the maximum lane count supported by all detected LTTPRs.
2309 int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2311 u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
2313 return max_lanes & DP_MAX_LANE_COUNT_MASK;
2315 EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
2318 * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
2319 * @caps: LTTPR PHY capabilities
2321 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2322 * voltage swing level 3.
2325 drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2327 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2329 return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
2331 EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
2334 * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
2335 * @caps: LTTPR PHY capabilities
2337 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2338 * pre-emphasis level 3.
2341 drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2343 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2345 return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
2347 EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
2350 * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2351 * @aux: DisplayPort AUX channel
2352 * @data: DP phy compliance test parameters.
2354 * Returns 0 on success or a negative error code on failure.
2356 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2357 struct drm_dp_phy_test_params *data)
2362 err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2365 data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2367 err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2370 data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2372 if (lanes & DP_ENHANCED_FRAME_CAP)
2373 data->enhanced_frame_cap = true;
2375 err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2379 switch (data->phy_pattern) {
2380 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2381 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2382 &data->custom80, sizeof(data->custom80));
2387 case DP_PHY_TEST_PATTERN_CP2520:
2388 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2390 sizeof(data->hbr2_reset));
2397 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2400 * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2401 * @aux: DisplayPort AUX channel
2402 * @data: DP phy compliance test parameters.
2403 * @dp_rev: DP revision to use for compliance testing
2405 * Returns 0 on success or a negative error code on failure.
2407 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2408 struct drm_dp_phy_test_params *data, u8 dp_rev)
2414 link_config[0] = drm_dp_link_rate_to_bw_code(data->link_rate);
2415 link_config[1] = data->num_lanes;
2416 if (data->enhanced_frame_cap)
2417 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
2418 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, link_config, 2);
2422 test_pattern = data->phy_pattern;
2423 if (dp_rev < 0x12) {
2424 test_pattern = (test_pattern << 2) &
2425 DP_LINK_QUAL_PATTERN_11_MASK;
2426 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2431 for (i = 0; i < data->num_lanes; i++) {
2432 err = drm_dp_dpcd_writeb(aux,
2433 DP_LINK_QUAL_LANE0_SET + i,
2442 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2444 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2446 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2449 switch (pixelformat) {
2450 case DP_PIXELFORMAT_RGB:
2452 case DP_PIXELFORMAT_YUV444:
2454 case DP_PIXELFORMAT_YUV422:
2456 case DP_PIXELFORMAT_YUV420:
2458 case DP_PIXELFORMAT_Y_ONLY:
2460 case DP_PIXELFORMAT_RAW:
2467 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2468 enum dp_colorimetry colorimetry)
2470 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2473 switch (colorimetry) {
2474 case DP_COLORIMETRY_DEFAULT:
2475 switch (pixelformat) {
2476 case DP_PIXELFORMAT_RGB:
2478 case DP_PIXELFORMAT_YUV444:
2479 case DP_PIXELFORMAT_YUV422:
2480 case DP_PIXELFORMAT_YUV420:
2482 case DP_PIXELFORMAT_Y_ONLY:
2483 return "DICOM PS3.14";
2484 case DP_PIXELFORMAT_RAW:
2485 return "Custom Color Profile";
2489 case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2490 switch (pixelformat) {
2491 case DP_PIXELFORMAT_RGB:
2492 return "Wide Fixed";
2493 case DP_PIXELFORMAT_YUV444:
2494 case DP_PIXELFORMAT_YUV422:
2495 case DP_PIXELFORMAT_YUV420:
2500 case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2501 switch (pixelformat) {
2502 case DP_PIXELFORMAT_RGB:
2503 return "Wide Float";
2504 case DP_PIXELFORMAT_YUV444:
2505 case DP_PIXELFORMAT_YUV422:
2506 case DP_PIXELFORMAT_YUV420:
2511 case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2512 switch (pixelformat) {
2513 case DP_PIXELFORMAT_RGB:
2515 case DP_PIXELFORMAT_YUV444:
2516 case DP_PIXELFORMAT_YUV422:
2517 case DP_PIXELFORMAT_YUV420:
2522 case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2523 switch (pixelformat) {
2524 case DP_PIXELFORMAT_RGB:
2526 case DP_PIXELFORMAT_YUV444:
2527 case DP_PIXELFORMAT_YUV422:
2528 case DP_PIXELFORMAT_YUV420:
2533 case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2534 switch (pixelformat) {
2535 case DP_PIXELFORMAT_RGB:
2536 return "Custom Profile";
2537 case DP_PIXELFORMAT_YUV444:
2538 case DP_PIXELFORMAT_YUV422:
2539 case DP_PIXELFORMAT_YUV420:
2544 case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2545 switch (pixelformat) {
2546 case DP_PIXELFORMAT_RGB:
2547 return "BT.2020 RGB";
2548 case DP_PIXELFORMAT_YUV444:
2549 case DP_PIXELFORMAT_YUV422:
2550 case DP_PIXELFORMAT_YUV420:
2551 return "BT.2020 CYCC";
2555 case DP_COLORIMETRY_BT2020_YCC:
2556 switch (pixelformat) {
2557 case DP_PIXELFORMAT_YUV444:
2558 case DP_PIXELFORMAT_YUV422:
2559 case DP_PIXELFORMAT_YUV420:
2560 return "BT.2020 YCC";
2569 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2571 switch (dynamic_range) {
2572 case DP_DYNAMIC_RANGE_VESA:
2573 return "VESA range";
2574 case DP_DYNAMIC_RANGE_CTA:
2581 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2583 switch (content_type) {
2584 case DP_CONTENT_TYPE_NOT_DEFINED:
2585 return "Not defined";
2586 case DP_CONTENT_TYPE_GRAPHICS:
2588 case DP_CONTENT_TYPE_PHOTO:
2590 case DP_CONTENT_TYPE_VIDEO:
2592 case DP_CONTENT_TYPE_GAME:
2599 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2600 const struct drm_dp_vsc_sdp *vsc)
2602 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2603 DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2604 vsc->revision, vsc->length);
2605 DP_SDP_LOG(" pixelformat: %s\n",
2606 dp_pixelformat_get_name(vsc->pixelformat));
2607 DP_SDP_LOG(" colorimetry: %s\n",
2608 dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2609 DP_SDP_LOG(" bpc: %u\n", vsc->bpc);
2610 DP_SDP_LOG(" dynamic range: %s\n",
2611 dp_dynamic_range_get_name(vsc->dynamic_range));
2612 DP_SDP_LOG(" content type: %s\n",
2613 dp_content_type_get_name(vsc->content_type));
2616 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
2619 * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
2620 * @dpcd: DisplayPort configuration data
2621 * @port_cap: port capabilities
2623 * Returns maximum frl bandwidth supported by PCON in GBPS,
2624 * returns 0 if not supported.
2626 int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
2627 const u8 port_cap[4])
2633 bw = buf & DP_PCON_MAX_FRL_BW;
2636 case DP_PCON_MAX_9GBPS:
2638 case DP_PCON_MAX_18GBPS:
2640 case DP_PCON_MAX_24GBPS:
2642 case DP_PCON_MAX_32GBPS:
2644 case DP_PCON_MAX_40GBPS:
2646 case DP_PCON_MAX_48GBPS:
2648 case DP_PCON_MAX_0GBPS:
2655 EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
2658 * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
2659 * @aux: DisplayPort AUX channel
2660 * @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.
2662 * Returns 0 if success, else returns negative error code.
2664 int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
2667 u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
2668 DP_PCON_ENABLE_LINK_FRL_MODE;
2670 if (enable_frl_ready_hpd)
2671 buf |= DP_PCON_ENABLE_HPD_READY;
2673 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2677 EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
2680 * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
2681 * @aux: DisplayPort AUX channel
2683 * Returns true if success, else returns false.
2685 bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
2690 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2694 if (buf & DP_PCON_FRL_READY)
2699 EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
2702 * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
2703 * @aux: DisplayPort AUX channel
2704 * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
2705 * @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.
2706 * In Concurrent Mode, the FRL link bring up can be done along with
2707 * DP Link training. In Sequential mode, the FRL link bring up is done prior to
2708 * the DP Link training.
2710 * Returns 0 if success, else returns negative error code.
2713 int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
2719 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2723 if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)
2724 buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
2726 buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
2728 switch (max_frl_gbps) {
2730 buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;
2733 buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;
2736 buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;
2739 buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;
2742 buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;
2745 buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;
2748 buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;
2754 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2760 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
2763 * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
2764 * @aux: DisplayPort AUX channel
2765 * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
2766 * @frl_type : FRL training type, can be Extended, or Normal.
2767 * In Normal FRL training, the PCON tries each frl bw from the max_frl_mask
2768 * starting from min, and stops when link training is successful. In Extended
2769 * FRL training, all frl bw selected in the mask are trained by the PCON.
2771 * Returns 0 if success, else returns negative error code.
2773 int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
2777 u8 buf = max_frl_mask;
2779 if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)
2780 buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2782 buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2784 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
2790 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
2793 * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
2794 * @aux: DisplayPort AUX channel
2796 * Returns 0 if success, else returns negative error code.
2798 int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
2802 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
2808 EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
2811 * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
2812 * @aux: DisplayPort AUX channel
2814 * Returns 0 if success, else returns negative error code.
2816 int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
2821 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2824 if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
2825 drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",
2829 buf |= DP_PCON_ENABLE_HDMI_LINK;
2830 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2836 EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
2839 * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
2840 * @aux: DisplayPort AUX channel
2842 * Returns true if link is active else returns false.
2844 bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
2849 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2853 return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
2855 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
2858 * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
2859 * @aux: DisplayPort AUX channel
2860 * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
2861 * Valid only if the MODE returned is FRL. For Normal Link training mode
2862 * only 1 of the bits will be set, but in case of Extended mode, more than
2863 * one bits can be set.
2865 * Returns the link mode : TMDS or FRL on success, else returns negative error
2868 int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
2874 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
2878 mode = buf & DP_PCON_HDMI_LINK_MODE;
2880 if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
2881 *frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
2885 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
2888 * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
2889 * during link failure between PCON and HDMI sink
2890 * @aux: DisplayPort AUX channel
2891 * @connector: DRM connector
2895 void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
2896 struct drm_connector *connector)
2898 u8 buf, error_count;
2900 struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
2902 for (i = 0; i < hdmi->max_lanes; i++) {
2903 if (drm_dp_dpcd_readb(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
2906 error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
2907 switch (error_count) {
2908 case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
2911 case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
2914 case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
2921 drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",
2922 aux->name, num_error, i);
2925 EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
2928 * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
2929 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2931 * Returns true is PCON encoder is DSC 1.2 else returns false.
2933 bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2936 u8 major_v, minor_v;
2938 buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
2939 major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
2940 minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
2942 if (major_v == 1 && minor_v == 2)
2947 EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
2950 * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
2951 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2953 * Returns maximum no. of slices supported by the PCON DSC Encoder.
2955 int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2957 u8 slice_cap1, slice_cap2;
2959 slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
2960 slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
2962 if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
2964 if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
2966 if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
2968 if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
2970 if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
2972 if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
2974 if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
2976 if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
2978 if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
2980 if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
2985 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
2988 * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
2989 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
2991 * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
2993 int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
2997 buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
2999 return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
3001 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
3004 * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
3005 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3007 * Returns the bpp precision supported by the PCON encoder.
3009 int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3013 buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
3015 switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
3016 case DP_PCON_DSC_ONE_16TH_BPP:
3018 case DP_PCON_DSC_ONE_8TH_BPP:
3020 case DP_PCON_DSC_ONE_4TH_BPP:
3022 case DP_PCON_DSC_ONE_HALF_BPP:
3024 case DP_PCON_DSC_ONE_BPP:
3030 EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
3033 int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
3038 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3042 buf |= DP_PCON_ENABLE_DSC_ENCODER;
3044 if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
3045 buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
3046 buf |= pps_buf_config << 2;
3049 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3057 * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
3058 * for DSC1.2 between PCON & HDMI2.1 sink
3059 * @aux: DisplayPort AUX channel
3061 * Returns 0 on success, else returns negative error code.
3063 int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
3067 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
3073 EXPORT_SYMBOL(drm_dp_pcon_pps_default);
3076 * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
3078 * @aux: DisplayPort AUX channel
3079 * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
3081 * Returns 0 on success, else returns negative error code.
3083 int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
3087 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
3091 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3097 EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
3100 * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
3101 * override registers
3102 * @aux: DisplayPort AUX channel
3103 * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
3106 * Returns 0 on success, else returns negative error code.
3108 int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
3112 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
3115 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
3118 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
3122 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3128 EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
3131 * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
3132 * @aux: displayPort AUX channel
3133 * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
3135 * Returns 0 on success, else returns negative error code.
3137 int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
3142 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3146 if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
3147 buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
3149 buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
3151 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3157 EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);
3160 * drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX
3161 * @aux: The DP AUX channel to use
3162 * @bl: Backlight capability info from drm_edp_backlight_init()
3163 * @level: The brightness level to set
3165 * Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must
3166 * already have been enabled by the driver by calling drm_edp_backlight_enable().
3168 * Returns: %0 on success, negative error code on failure
3170 int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3176 if (bl->lsb_reg_used) {
3177 buf[0] = (level & 0xff00) >> 8;
3178 buf[1] = (level & 0x00ff);
3183 ret = drm_dp_dpcd_write(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, sizeof(buf));
3184 if (ret != sizeof(buf)) {
3185 drm_err(aux->drm_dev,
3186 "%s: Failed to write aux backlight level: %d\n",
3188 return ret < 0 ? ret : -EIO;
3193 EXPORT_SYMBOL(drm_edp_backlight_set_level);
3196 drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3202 /* The panel uses something other then DPCD for enabling its backlight */
3203 if (!bl->aux_enable)
3206 ret = drm_dp_dpcd_readb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);
3208 drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",
3210 return ret < 0 ? ret : -EIO;
3213 buf |= DP_EDP_BACKLIGHT_ENABLE;
3215 buf &= ~DP_EDP_BACKLIGHT_ENABLE;
3217 ret = drm_dp_dpcd_writeb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);
3219 drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",
3221 return ret < 0 ? ret : -EIO;
3228 * drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD
3229 * @aux: The DP AUX channel to use
3230 * @bl: Backlight capability info from drm_edp_backlight_init()
3231 * @level: The initial backlight level to set via AUX, if there is one
3233 * This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally
3234 * restoring any important backlight state such as the given backlight level, the brightness byte
3235 * count, backlight frequency, etc.
3237 * Note that certain panels, while supporting brightness level controls over DPCD, may not support
3238 * having their backlights enabled via the standard %DP_EDP_DISPLAY_CONTROL_REGISTER. On such panels
3239 * &drm_edp_backlight_info.aux_enable will be set to %false, this function will skip the step of
3240 * programming the %DP_EDP_DISPLAY_CONTROL_REGISTER, and the driver must perform the required
3241 * implementation specific step for enabling the backlight after calling this function.
3243 * Returns: %0 on success, negative error code on failure.
3245 int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3249 u8 dpcd_buf, new_dpcd_buf;
3251 ret = drm_dp_dpcd_readb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &dpcd_buf);
3253 drm_dbg_kms(aux->drm_dev,
3254 "%s: Failed to read backlight mode: %d\n", aux->name, ret);
3255 return ret < 0 ? ret : -EIO;
3258 new_dpcd_buf = dpcd_buf;
3260 if ((dpcd_buf & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK) != DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
3261 new_dpcd_buf &= ~DP_EDP_BACKLIGHT_CONTROL_MODE_MASK;
3262 new_dpcd_buf |= DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
3264 if (bl->pwmgen_bit_count) {
3265 ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);
3267 drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
3272 if (bl->pwm_freq_pre_divider) {
3273 ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_FREQ_SET, bl->pwm_freq_pre_divider);
3275 drm_dbg_kms(aux->drm_dev,
3276 "%s: Failed to write aux backlight frequency: %d\n",
3279 new_dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;
3282 if (new_dpcd_buf != dpcd_buf) {
3283 ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, new_dpcd_buf);
3285 drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",
3287 return ret < 0 ? ret : -EIO;
3291 ret = drm_edp_backlight_set_level(aux, bl, level);
3294 ret = drm_edp_backlight_set_enable(aux, bl, true);
3300 EXPORT_SYMBOL(drm_edp_backlight_enable);
3303 * drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported
3304 * @aux: The DP AUX channel to use
3305 * @bl: Backlight capability info from drm_edp_backlight_init()
3307 * This function handles disabling DPCD backlight controls on a panel over AUX. Note that some
3308 * panels have backlights that are enabled/disabled by other means, despite having their brightness
3309 * values controlled through DPCD. On such panels &drm_edp_backlight_info.aux_enable will be set to
3310 * %false, this function will become a no-op (and we will skip updating
3311 * %DP_EDP_DISPLAY_CONTROL_REGISTER), and the driver must take care to perform it's own
3312 * implementation specific step for disabling the backlight.
3314 * Returns: %0 on success or no-op, negative error code on failure.
3316 int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)
3320 ret = drm_edp_backlight_set_enable(aux, bl, false);
3326 EXPORT_SYMBOL(drm_edp_backlight_disable);
3329 drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3330 u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])
3332 int fxp, fxp_min, fxp_max, fxp_actual, f = 1;
3334 u8 pn, pn_min, pn_max;
3336 ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT, &pn);
3338 drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",
3343 pn &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3344 bl->max = (1 << pn) - 1;
3345 if (!driver_pwm_freq_hz)
3349 * Set PWM Frequency divider to match desired frequency provided by the driver.
3350 * The PWM Frequency is calculated as 27Mhz / (F x P).
3351 * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the
3352 * EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)
3353 * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the
3354 * EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)
3357 /* Find desired value of (F x P)
3358 * Note that, if F x P is out of supported range, the maximum value or minimum value will
3359 * applied automatically. So no need to check that.
3361 fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);
3363 /* Use highest possible value of Pn for more granularity of brightness adjustment while
3364 * satisfying the conditions below.
3365 * - Pn is in the range of Pn_min and Pn_max
3366 * - F is in the range of 1 and 255
3367 * - FxP is within 25% of desired value.
3368 * Note: 25% is arbitrary value and may need some tweak.
3370 ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);
3372 drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",
3376 ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);
3378 drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",
3382 pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3383 pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3385 /* Ensure frequency is within 25% of desired value */
3386 fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);
3387 fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);
3388 if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {
3389 drm_dbg_kms(aux->drm_dev,
3390 "%s: Driver defined backlight frequency (%d) out of range\n",
3391 aux->name, driver_pwm_freq_hz);
3395 for (pn = pn_max; pn >= pn_min; pn--) {
3396 f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);
3397 fxp_actual = f << pn;
3398 if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)
3402 ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);
3404 drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
3408 bl->pwmgen_bit_count = pn;
3409 bl->max = (1 << pn) - 1;
3411 if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {
3412 bl->pwm_freq_pre_divider = f;
3413 drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",
3414 aux->name, driver_pwm_freq_hz);
3421 drm_edp_backlight_probe_level(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3428 ret = drm_dp_dpcd_readb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);
3430 drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",
3432 return ret < 0 ? ret : -EIO;
3435 *current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);
3436 if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
3437 int size = 1 + bl->lsb_reg_used;
3439 ret = drm_dp_dpcd_read(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, size);
3441 drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight level: %d\n",
3443 return ret < 0 ? ret : -EIO;
3446 if (bl->lsb_reg_used)
3447 return (buf[0] << 8) | buf[1];
3453 * If we're not in DPCD control mode yet, the programmed brightness value is meaningless and
3454 * the driver should assume max brightness
3460 * drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight
3462 * @aux: The DP aux device to use for probing
3463 * @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight
3464 * @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz
3465 * @edp_dpcd: A cached copy of the eDP DPCD
3466 * @current_level: Where to store the probed brightness level
3467 * @current_mode: Where to store the currently set backlight control mode
3469 * Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,
3470 * along with also probing the current and maximum supported brightness levels.
3472 * If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the
3473 * default frequency from the panel is used.
3475 * Returns: %0 on success, negative error code on failure.
3478 drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3479 u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],
3480 u16 *current_level, u8 *current_mode)
3484 if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)
3485 bl->aux_enable = true;
3486 if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
3487 bl->lsb_reg_used = true;
3489 ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);
3493 ret = drm_edp_backlight_probe_level(aux, bl, current_mode);
3496 *current_level = ret;
3498 drm_dbg_kms(aux->drm_dev,
3499 "%s: Found backlight level=%d/%d pwm_freq_pre_divider=%d mode=%x\n",
3500 aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider, *current_mode);
3501 drm_dbg_kms(aux->drm_dev,
3502 "%s: Backlight caps: pwmgen_bit_count=%d lsb_reg_used=%d aux_enable=%d\n",
3503 aux->name, bl->pwmgen_bit_count, bl->lsb_reg_used, bl->aux_enable);
3506 EXPORT_SYMBOL(drm_edp_backlight_init);
3508 #if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
3509 (IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))
3511 static int dp_aux_backlight_update_status(struct backlight_device *bd)
3513 struct dp_aux_backlight *bl = bl_get_data(bd);
3514 u16 brightness = backlight_get_brightness(bd);
3517 if (!backlight_is_blank(bd)) {
3519 drm_edp_backlight_enable(bl->aux, &bl->info, brightness);
3523 ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);
3526 drm_edp_backlight_disable(bl->aux, &bl->info);
3527 bl->enabled = false;
3534 static const struct backlight_ops dp_aux_bl_ops = {
3535 .update_status = dp_aux_backlight_update_status,
3539 * drm_panel_dp_aux_backlight - create and use DP AUX backlight
3541 * @aux: The DP AUX channel to use
3543 * Use this function to create and handle backlight if your panel
3544 * supports backlight control over DP AUX channel using DPCD
3545 * registers as per VESA's standard backlight control interface.
3547 * When the panel is enabled backlight will be enabled after a
3548 * successful call to &drm_panel_funcs.enable()
3550 * When the panel is disabled backlight will be disabled before the
3551 * call to &drm_panel_funcs.disable().
3553 * A typical implementation for a panel driver supporting backlight
3554 * control over DP AUX will call this function at probe time.
3555 * Backlight will then be handled transparently without requiring
3556 * any intervention from the driver.
3558 * drm_panel_dp_aux_backlight() must be called after the call to drm_panel_init().
3560 * Return: 0 on success or a negative error code on failure.
3562 int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)
3564 struct dp_aux_backlight *bl;
3565 struct backlight_properties props = { 0 };
3568 u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
3571 if (!panel || !panel->dev || !aux)
3574 ret = drm_dp_dpcd_read(aux, DP_EDP_DPCD_REV, edp_dpcd,
3575 EDP_DISPLAY_CTL_CAP_SIZE);
3579 if (!drm_edp_backlight_supported(edp_dpcd)) {
3580 DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");
3584 bl = devm_kzalloc(panel->dev, sizeof(*bl), GFP_KERNEL);
3590 ret = drm_edp_backlight_init(aux, &bl->info, 0, edp_dpcd,
3591 ¤t_level, ¤t_mode);
3595 props.type = BACKLIGHT_RAW;
3596 props.brightness = current_level;
3597 props.max_brightness = bl->info.max;
3599 bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",
3601 &dp_aux_bl_ops, &props);
3602 if (IS_ERR(bl->base))
3603 return PTR_ERR(bl->base);
3605 backlight_disable(bl->base);
3607 panel->backlight = bl->base;
3611 EXPORT_SYMBOL(drm_panel_dp_aux_backlight);