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 static int __8b10b_clock_recovery_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
160 drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",
161 aux->name, rd_interval);
163 if (rd_interval == 0)
166 return rd_interval * 4 * USEC_PER_MSEC;
169 static int __8b10b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
172 drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",
173 aux->name, rd_interval);
175 if (rd_interval == 0)
178 return rd_interval * 4 * USEC_PER_MSEC;
181 static int __128b132b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)
183 switch (rd_interval) {
185 drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x\n",
186 aux->name, rd_interval);
188 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_400_US:
190 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_4_MS:
192 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_8_MS:
194 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_12_MS:
196 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_16_MS:
198 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_32_MS:
200 case DP_128B132B_TRAINING_AUX_RD_INTERVAL_64_MS:
206 * The link training delays are different for:
208 * - Clock recovery vs. channel equalization
210 * - 128b/132b vs. 8b/10b
211 * - DPCD rev 1.3 vs. later
213 * Get the correct delay in us, reading DPCD if necessary.
215 static int __read_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
216 enum drm_dp_phy dp_phy, bool uhbr, bool cr)
218 int (*parse)(const struct drm_dp_aux *aux, u8 rd_interval);
220 u8 rd_interval, mask;
222 if (dp_phy == DP_PHY_DPRX) {
227 offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL;
228 mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
229 parse = __128b132b_channel_eq_delay_us;
231 if (cr && dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
234 offset = DP_TRAINING_AUX_RD_INTERVAL;
235 mask = DP_TRAINING_AUX_RD_MASK;
237 parse = __8b10b_clock_recovery_delay_us;
239 parse = __8b10b_channel_eq_delay_us;
243 offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);
244 mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;
245 parse = __128b132b_channel_eq_delay_us;
250 offset = DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);
251 mask = DP_TRAINING_AUX_RD_MASK;
252 parse = __8b10b_channel_eq_delay_us;
256 if (offset < DP_RECEIVER_CAP_SIZE) {
257 rd_interval = dpcd[offset];
259 if (drm_dp_dpcd_readb(aux, offset, &rd_interval) != 1) {
260 drm_dbg_kms(aux->drm_dev, "%s: failed rd interval read\n",
262 /* arbitrary default delay */
267 return parse(aux, rd_interval & mask);
270 int drm_dp_read_clock_recovery_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
271 enum drm_dp_phy dp_phy, bool uhbr)
273 return __read_delay(aux, dpcd, dp_phy, uhbr, true);
275 EXPORT_SYMBOL(drm_dp_read_clock_recovery_delay);
277 int drm_dp_read_channel_eq_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],
278 enum drm_dp_phy dp_phy, bool uhbr)
280 return __read_delay(aux, dpcd, dp_phy, uhbr, false);
282 EXPORT_SYMBOL(drm_dp_read_channel_eq_delay);
284 void drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux *aux,
285 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
287 u8 rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
288 DP_TRAINING_AUX_RD_MASK;
291 if (dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
294 delay_us = __8b10b_clock_recovery_delay_us(aux, rd_interval);
296 usleep_range(delay_us, delay_us * 2);
298 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
300 static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
303 int delay_us = __8b10b_channel_eq_delay_us(aux, rd_interval);
305 usleep_range(delay_us, delay_us * 2);
308 void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
309 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
311 __drm_dp_link_train_channel_eq_delay(aux,
312 dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
313 DP_TRAINING_AUX_RD_MASK);
315 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
317 void drm_dp_lttpr_link_train_clock_recovery_delay(void)
319 usleep_range(100, 200);
321 EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);
323 static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)
325 return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];
328 void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,
329 const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])
331 u8 interval = dp_lttpr_phy_cap(phy_cap,
332 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &
333 DP_TRAINING_AUX_RD_MASK;
335 __drm_dp_link_train_channel_eq_delay(aux, interval);
337 EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);
339 u8 drm_dp_link_rate_to_bw_code(int link_rate)
343 return DP_LINK_BW_10;
345 return DP_LINK_BW_13_5;
347 return DP_LINK_BW_20;
349 /* Spec says link_bw = link_rate / 0.27Gbps */
350 return link_rate / 27000;
353 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
355 int drm_dp_bw_code_to_link_rate(u8 link_bw)
360 case DP_LINK_BW_13_5:
365 /* Spec says link_rate = link_bw * 0.27Gbps */
366 return link_bw * 27000;
369 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
371 #define AUX_RETRY_INTERVAL 500 /* us */
374 drm_dp_dump_access(const struct drm_dp_aux *aux,
375 u8 request, uint offset, void *buffer, int ret)
377 const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
380 drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
381 aux->name, offset, arrow, ret, min(ret, 20), buffer);
383 drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",
384 aux->name, offset, arrow, ret);
390 * The DisplayPort AUX channel is an abstraction to allow generic, driver-
391 * independent access to AUX functionality. Drivers can take advantage of
392 * this by filling in the fields of the drm_dp_aux structure.
394 * Transactions are described using a hardware-independent drm_dp_aux_msg
395 * structure, which is passed into a driver's .transfer() implementation.
396 * Both native and I2C-over-AUX transactions are supported.
399 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
400 unsigned int offset, void *buffer, size_t size)
402 struct drm_dp_aux_msg msg;
403 unsigned int retry, native_reply;
404 int err = 0, ret = 0;
406 memset(&msg, 0, sizeof(msg));
407 msg.address = offset;
408 msg.request = request;
412 mutex_lock(&aux->hw_mutex);
415 * The specification doesn't give any recommendation on how often to
416 * retry native transactions. We used to retry 7 times like for
417 * aux i2c transactions but real world devices this wasn't
418 * sufficient, bump to 32 which makes Dell 4k monitors happier.
420 for (retry = 0; retry < 32; retry++) {
421 if (ret != 0 && ret != -ETIMEDOUT) {
422 usleep_range(AUX_RETRY_INTERVAL,
423 AUX_RETRY_INTERVAL + 100);
426 ret = aux->transfer(aux, &msg);
428 native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
429 if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
439 * We want the error we return to be the error we received on
440 * the first transaction, since we may get a different error the
447 drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",
452 mutex_unlock(&aux->hw_mutex);
457 * drm_dp_dpcd_read() - read a series of bytes from the DPCD
458 * @aux: DisplayPort AUX channel (SST or MST)
459 * @offset: address of the (first) register to read
460 * @buffer: buffer to store the register values
461 * @size: number of bytes in @buffer
463 * Returns the number of bytes transferred on success, or a negative error
464 * code on failure. -EIO is returned if the request was NAKed by the sink or
465 * if the retry count was exceeded. If not all bytes were transferred, this
466 * function returns -EPROTO. Errors from the underlying AUX channel transfer
467 * function, with the exception of -EBUSY (which causes the transaction to
468 * be retried), are propagated to the caller.
470 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
471 void *buffer, size_t size)
476 * HP ZR24w corrupts the first DPCD access after entering power save
477 * mode. Eg. on a read, the entire buffer will be filled with the same
478 * byte. Do a throw away read to avoid corrupting anything we care
479 * about. Afterwards things will work correctly until the monitor
480 * gets woken up and subsequently re-enters power save mode.
482 * The user pressing any button on the monitor is enough to wake it
483 * up, so there is no particularly good place to do the workaround.
484 * We just have to do it before any DPCD access and hope that the
485 * monitor doesn't power down exactly after the throw away read.
487 if (!aux->is_remote) {
488 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
495 ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
497 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
501 drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
504 EXPORT_SYMBOL(drm_dp_dpcd_read);
507 * drm_dp_dpcd_write() - write a series of bytes to the DPCD
508 * @aux: DisplayPort AUX channel (SST or MST)
509 * @offset: address of the (first) register to write
510 * @buffer: buffer containing the values to write
511 * @size: number of bytes in @buffer
513 * Returns the number of bytes transferred on success, or a negative error
514 * code on failure. -EIO is returned if the request was NAKed by the sink or
515 * if the retry count was exceeded. If not all bytes were transferred, this
516 * function returns -EPROTO. Errors from the underlying AUX channel transfer
517 * function, with the exception of -EBUSY (which causes the transaction to
518 * be retried), are propagated to the caller.
520 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
521 void *buffer, size_t size)
526 ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
528 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
531 drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
534 EXPORT_SYMBOL(drm_dp_dpcd_write);
537 * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
538 * @aux: DisplayPort AUX channel
539 * @status: buffer to store the link status in (must be at least 6 bytes)
541 * Returns the number of bytes transferred on success or a negative error
544 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
545 u8 status[DP_LINK_STATUS_SIZE])
547 return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
548 DP_LINK_STATUS_SIZE);
550 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
553 * drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY
554 * @aux: DisplayPort AUX channel
555 * @dp_phy: the DP PHY to get the link status for
556 * @link_status: buffer to return the status in
558 * Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The
559 * layout of the returned @link_status matches the DPCD register layout of the
560 * DPRX PHY link status.
562 * Returns 0 if the information was read successfully or a negative error code
565 int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,
566 enum drm_dp_phy dp_phy,
567 u8 link_status[DP_LINK_STATUS_SIZE])
571 if (dp_phy == DP_PHY_DPRX) {
572 ret = drm_dp_dpcd_read(aux,
575 DP_LINK_STATUS_SIZE);
580 WARN_ON(ret != DP_LINK_STATUS_SIZE);
585 ret = drm_dp_dpcd_read(aux,
586 DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),
588 DP_LINK_STATUS_SIZE - 1);
593 WARN_ON(ret != DP_LINK_STATUS_SIZE - 1);
595 /* Convert the LTTPR to the sink PHY link status layout */
596 memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],
597 &link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],
598 DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);
599 link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;
603 EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);
605 static bool is_edid_digital_input_dp(const struct edid *edid)
607 return edid && edid->revision >= 4 &&
608 edid->input & DRM_EDID_INPUT_DIGITAL &&
609 (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
613 * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
614 * @dpcd: DisplayPort configuration data
615 * @port_cap: port capabilities
616 * @type: port type to be checked. Can be:
617 * %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,
618 * %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,
619 * %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.
621 * Caveat: Only works with DPCD 1.1+ port caps.
623 * Returns: whether the downstream facing port matches the type.
625 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
626 const u8 port_cap[4], u8 type)
628 return drm_dp_is_branch(dpcd) &&
629 dpcd[DP_DPCD_REV] >= 0x11 &&
630 (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
632 EXPORT_SYMBOL(drm_dp_downstream_is_type);
635 * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
636 * @dpcd: DisplayPort configuration data
637 * @port_cap: port capabilities
640 * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
642 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
643 const u8 port_cap[4],
644 const struct edid *edid)
646 if (dpcd[DP_DPCD_REV] < 0x11) {
647 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
648 case DP_DWN_STRM_PORT_TYPE_TMDS:
655 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
656 case DP_DS_PORT_TYPE_DP_DUALMODE:
657 if (is_edid_digital_input_dp(edid))
660 case DP_DS_PORT_TYPE_DVI:
661 case DP_DS_PORT_TYPE_HDMI:
667 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
670 * drm_dp_send_real_edid_checksum() - send back real edid checksum value
671 * @aux: DisplayPort AUX channel
672 * @real_edid_checksum: real edid checksum for the last block
677 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
678 u8 real_edid_checksum)
680 u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
682 if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
683 &auto_test_req, 1) < 1) {
684 drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
685 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
688 auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
690 if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
691 drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",
692 aux->name, DP_TEST_REQUEST);
695 link_edid_read &= DP_TEST_LINK_EDID_READ;
697 if (!auto_test_req || !link_edid_read) {
698 drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",
703 if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
704 &auto_test_req, 1) < 1) {
705 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
706 aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
710 /* send back checksum for the last edid extension block data */
711 if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
712 &real_edid_checksum, 1) < 1) {
713 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
714 aux->name, DP_TEST_EDID_CHECKSUM);
718 test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
719 if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
720 drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",
721 aux->name, DP_TEST_RESPONSE);
727 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
729 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
731 u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
733 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
739 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
740 u8 dpcd[DP_RECEIVER_CAP_SIZE])
742 u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
746 * Prior to DP1.3 the bit represented by
747 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
748 * If it is set DP_DPCD_REV at 0000h could be at a value less than
749 * the true capability of the panel. The only way to check is to
750 * then compare 0000h and 2200h.
752 if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
753 DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
756 ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
760 if (ret != sizeof(dpcd_ext))
763 if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
764 drm_dbg_kms(aux->drm_dev,
765 "%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
766 aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
770 if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
773 drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
775 memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
781 * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
783 * @aux: DisplayPort AUX channel
784 * @dpcd: Buffer to store the resulting DPCD in
786 * Attempts to read the base DPCD caps for @aux. Additionally, this function
787 * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
790 * Returns: %0 if the DPCD was read successfully, negative error code
793 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
794 u8 dpcd[DP_RECEIVER_CAP_SIZE])
798 ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
801 if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
804 ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
808 drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
812 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
815 * drm_dp_read_downstream_info() - read DPCD downstream port info if available
816 * @aux: DisplayPort AUX channel
817 * @dpcd: A cached copy of the port's DPCD
818 * @downstream_ports: buffer to store the downstream port info in
821 * drm_dp_downstream_max_clock()
822 * drm_dp_downstream_max_bpc()
824 * Returns: 0 if either the downstream port info was read successfully or
825 * there was no downstream info to read, or a negative error code otherwise.
827 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
828 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
829 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
834 memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
836 /* No downstream info to read */
837 if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)
840 /* Some branches advertise having 0 downstream ports, despite also advertising they have a
841 * downstream port present. The DP spec isn't clear on if this is allowed or not, but since
842 * some branches do it we need to handle it regardless.
844 len = drm_dp_downstream_port_count(dpcd);
848 if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
851 ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
857 drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);
861 EXPORT_SYMBOL(drm_dp_read_downstream_info);
864 * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
865 * @dpcd: DisplayPort configuration data
866 * @port_cap: port capabilities
868 * Returns: Downstream facing port max dot clock in kHz on success,
869 * or 0 if max clock not defined
871 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
872 const u8 port_cap[4])
874 if (!drm_dp_is_branch(dpcd))
877 if (dpcd[DP_DPCD_REV] < 0x11)
880 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
881 case DP_DS_PORT_TYPE_VGA:
882 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
884 return port_cap[1] * 8000;
889 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
892 * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
893 * @dpcd: DisplayPort configuration data
894 * @port_cap: port capabilities
897 * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
898 * or 0 if max TMDS clock not defined
900 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
901 const u8 port_cap[4],
902 const struct edid *edid)
904 if (!drm_dp_is_branch(dpcd))
907 if (dpcd[DP_DPCD_REV] < 0x11) {
908 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
909 case DP_DWN_STRM_PORT_TYPE_TMDS:
916 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
917 case DP_DS_PORT_TYPE_DP_DUALMODE:
918 if (is_edid_digital_input_dp(edid))
921 * It's left up to the driver to check the
922 * DP dual mode adapter's max TMDS clock.
924 * Unfortunately it looks like branch devices
925 * may not fordward that the DP dual mode i2c
926 * access so we just usually get i2c nak :(
929 case DP_DS_PORT_TYPE_HDMI:
931 * We should perhaps assume 165 MHz when detailed cap
932 * info is not available. But looks like many typical
933 * branch devices fall into that category and so we'd
934 * probably end up with users complaining that they can't
935 * get high resolution modes with their favorite dongle.
937 * So let's limit to 300 MHz instead since DPCD 1.4
938 * HDMI 2.0 DFPs are required to have the detailed cap
939 * info. So it's more likely we're dealing with a HDMI 1.4
940 * compatible* device here.
942 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
944 return port_cap[1] * 2500;
945 case DP_DS_PORT_TYPE_DVI:
946 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
948 /* FIXME what to do about DVI dual link? */
949 return port_cap[1] * 2500;
954 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
957 * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
958 * @dpcd: DisplayPort configuration data
959 * @port_cap: port capabilities
962 * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
963 * or 0 if max TMDS clock not defined
965 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
966 const u8 port_cap[4],
967 const struct edid *edid)
969 if (!drm_dp_is_branch(dpcd))
972 if (dpcd[DP_DPCD_REV] < 0x11) {
973 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
974 case DP_DWN_STRM_PORT_TYPE_TMDS:
981 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
982 case DP_DS_PORT_TYPE_DP_DUALMODE:
983 if (is_edid_digital_input_dp(edid))
986 case DP_DS_PORT_TYPE_DVI:
987 case DP_DS_PORT_TYPE_HDMI:
989 * Unclear whether the protocol converter could
990 * utilize pixel replication. Assume it won't.
997 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
1000 * drm_dp_downstream_max_bpc() - extract downstream facing port max
1001 * bits per component
1002 * @dpcd: DisplayPort configuration data
1003 * @port_cap: downstream facing port capabilities
1006 * Returns: Max bpc on success or 0 if max bpc not defined
1008 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1009 const u8 port_cap[4],
1010 const struct edid *edid)
1012 if (!drm_dp_is_branch(dpcd))
1015 if (dpcd[DP_DPCD_REV] < 0x11) {
1016 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
1017 case DP_DWN_STRM_PORT_TYPE_DP:
1024 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1025 case DP_DS_PORT_TYPE_DP:
1027 case DP_DS_PORT_TYPE_DP_DUALMODE:
1028 if (is_edid_digital_input_dp(edid))
1031 case DP_DS_PORT_TYPE_HDMI:
1032 case DP_DS_PORT_TYPE_DVI:
1033 case DP_DS_PORT_TYPE_VGA:
1034 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1037 switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
1054 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
1057 * drm_dp_downstream_420_passthrough() - determine downstream facing port
1058 * YCbCr 4:2:0 pass-through capability
1059 * @dpcd: DisplayPort configuration data
1060 * @port_cap: downstream facing port capabilities
1062 * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
1064 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1065 const u8 port_cap[4])
1067 if (!drm_dp_is_branch(dpcd))
1070 if (dpcd[DP_DPCD_REV] < 0x13)
1073 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1074 case DP_DS_PORT_TYPE_DP:
1076 case DP_DS_PORT_TYPE_HDMI:
1077 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1080 return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
1085 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
1088 * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
1089 * YCbCr 4:4:4->4:2:0 conversion capability
1090 * @dpcd: DisplayPort configuration data
1091 * @port_cap: downstream facing port capabilities
1093 * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
1095 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1096 const u8 port_cap[4])
1098 if (!drm_dp_is_branch(dpcd))
1101 if (dpcd[DP_DPCD_REV] < 0x13)
1104 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1105 case DP_DS_PORT_TYPE_HDMI:
1106 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1109 return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
1114 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
1117 * drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port
1118 * RGB->YCbCr conversion capability
1119 * @dpcd: DisplayPort configuration data
1120 * @port_cap: downstream facing port capabilities
1121 * @color_spc: Colorspace for which conversion cap is sought
1123 * Returns: whether the downstream facing port can convert RGB->YCbCr for a given
1126 bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1127 const u8 port_cap[4],
1130 if (!drm_dp_is_branch(dpcd))
1133 if (dpcd[DP_DPCD_REV] < 0x13)
1136 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1137 case DP_DS_PORT_TYPE_HDMI:
1138 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
1141 return port_cap[3] & color_spc;
1146 EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);
1149 * drm_dp_downstream_mode() - return a mode for downstream facing port
1151 * @dpcd: DisplayPort configuration data
1152 * @port_cap: port capabilities
1154 * Provides a suitable mode for downstream facing ports without EDID.
1156 * Returns: A new drm_display_mode on success or NULL on failure
1158 struct drm_display_mode *
1159 drm_dp_downstream_mode(struct drm_device *dev,
1160 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1161 const u8 port_cap[4])
1166 if (!drm_dp_is_branch(dpcd))
1169 if (dpcd[DP_DPCD_REV] < 0x11)
1172 switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
1173 case DP_DS_PORT_TYPE_NON_EDID:
1174 switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
1175 case DP_DS_NON_EDID_720x480i_60:
1178 case DP_DS_NON_EDID_720x480i_50:
1181 case DP_DS_NON_EDID_1920x1080i_60:
1184 case DP_DS_NON_EDID_1920x1080i_50:
1187 case DP_DS_NON_EDID_1280x720_60:
1190 case DP_DS_NON_EDID_1280x720_50:
1196 return drm_display_mode_from_cea_vic(dev, vic);
1201 EXPORT_SYMBOL(drm_dp_downstream_mode);
1204 * drm_dp_downstream_id() - identify branch device
1205 * @aux: DisplayPort AUX channel
1206 * @id: DisplayPort branch device id
1208 * Returns branch device id on success or NULL on failure
1210 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
1212 return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
1214 EXPORT_SYMBOL(drm_dp_downstream_id);
1217 * drm_dp_downstream_debug() - debug DP branch devices
1218 * @m: pointer for debugfs file
1219 * @dpcd: DisplayPort configuration data
1220 * @port_cap: port capabilities
1222 * @aux: DisplayPort AUX channel
1225 void drm_dp_downstream_debug(struct seq_file *m,
1226 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1227 const u8 port_cap[4],
1228 const struct edid *edid,
1229 struct drm_dp_aux *aux)
1231 bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1232 DP_DETAILED_CAP_INFO_AVAILABLE;
1238 int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1239 bool branch_device = drm_dp_is_branch(dpcd);
1241 seq_printf(m, "\tDP branch device present: %s\n",
1242 branch_device ? "yes" : "no");
1248 case DP_DS_PORT_TYPE_DP:
1249 seq_puts(m, "\t\tType: DisplayPort\n");
1251 case DP_DS_PORT_TYPE_VGA:
1252 seq_puts(m, "\t\tType: VGA\n");
1254 case DP_DS_PORT_TYPE_DVI:
1255 seq_puts(m, "\t\tType: DVI\n");
1257 case DP_DS_PORT_TYPE_HDMI:
1258 seq_puts(m, "\t\tType: HDMI\n");
1260 case DP_DS_PORT_TYPE_NON_EDID:
1261 seq_puts(m, "\t\tType: others without EDID support\n");
1263 case DP_DS_PORT_TYPE_DP_DUALMODE:
1264 seq_puts(m, "\t\tType: DP++\n");
1266 case DP_DS_PORT_TYPE_WIRELESS:
1267 seq_puts(m, "\t\tType: Wireless\n");
1270 seq_puts(m, "\t\tType: N/A\n");
1273 memset(id, 0, sizeof(id));
1274 drm_dp_downstream_id(aux, id);
1275 seq_printf(m, "\t\tID: %s\n", id);
1277 len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1279 seq_printf(m, "\t\tHW: %d.%d\n",
1280 (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1282 len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1284 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1286 if (detailed_cap_info) {
1287 clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1289 seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1291 clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1293 seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1295 clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1297 seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1299 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1302 seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1305 EXPORT_SYMBOL(drm_dp_downstream_debug);
1308 * drm_dp_subconnector_type() - get DP branch device type
1309 * @dpcd: DisplayPort configuration data
1310 * @port_cap: port capabilities
1312 enum drm_mode_subconnector
1313 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1314 const u8 port_cap[4])
1317 if (!drm_dp_is_branch(dpcd))
1318 return DRM_MODE_SUBCONNECTOR_Native;
1319 /* DP 1.0 approach */
1320 if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1321 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1322 DP_DWN_STRM_PORT_TYPE_MASK;
1325 case DP_DWN_STRM_PORT_TYPE_TMDS:
1326 /* Can be HDMI or DVI-D, DVI-D is a safer option */
1327 return DRM_MODE_SUBCONNECTOR_DVID;
1328 case DP_DWN_STRM_PORT_TYPE_ANALOG:
1329 /* Can be VGA or DVI-A, VGA is more popular */
1330 return DRM_MODE_SUBCONNECTOR_VGA;
1331 case DP_DWN_STRM_PORT_TYPE_DP:
1332 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1333 case DP_DWN_STRM_PORT_TYPE_OTHER:
1335 return DRM_MODE_SUBCONNECTOR_Unknown;
1338 type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1341 case DP_DS_PORT_TYPE_DP:
1342 case DP_DS_PORT_TYPE_DP_DUALMODE:
1343 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1344 case DP_DS_PORT_TYPE_VGA:
1345 return DRM_MODE_SUBCONNECTOR_VGA;
1346 case DP_DS_PORT_TYPE_DVI:
1347 return DRM_MODE_SUBCONNECTOR_DVID;
1348 case DP_DS_PORT_TYPE_HDMI:
1349 return DRM_MODE_SUBCONNECTOR_HDMIA;
1350 case DP_DS_PORT_TYPE_WIRELESS:
1351 return DRM_MODE_SUBCONNECTOR_Wireless;
1352 case DP_DS_PORT_TYPE_NON_EDID:
1354 return DRM_MODE_SUBCONNECTOR_Unknown;
1357 EXPORT_SYMBOL(drm_dp_subconnector_type);
1360 * drm_dp_set_subconnector_property - set subconnector for DP connector
1361 * @connector: connector to set property on
1362 * @status: connector status
1363 * @dpcd: DisplayPort configuration data
1364 * @port_cap: port capabilities
1366 * Called by a driver on every detect event.
1368 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1369 enum drm_connector_status status,
1371 const u8 port_cap[4])
1373 enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1375 if (status == connector_status_connected)
1376 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1377 drm_object_property_set_value(&connector->base,
1378 connector->dev->mode_config.dp_subconnector_property,
1381 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1384 * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1386 * @connector: The DRM connector to check
1387 * @dpcd: A cached copy of the connector's DPCD RX capabilities
1388 * @desc: A cached copy of the connector's DP descriptor
1390 * See also: drm_dp_read_sink_count()
1392 * Returns: %True if the (e)DP connector has a valid sink count that should
1393 * be probed, %false otherwise.
1395 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1396 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1397 const struct drm_dp_desc *desc)
1399 /* Some eDP panels don't set a valid value for the sink count */
1400 return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1401 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1402 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1403 !drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);
1405 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1408 * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1409 * @aux: The DP AUX channel to use
1411 * See also: drm_dp_read_sink_count_cap()
1413 * Returns: The current sink count reported by @aux, or a negative error code
1416 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1421 ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1427 return DP_GET_SINK_COUNT(count);
1429 EXPORT_SYMBOL(drm_dp_read_sink_count);
1432 * I2C-over-AUX implementation
1435 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1437 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1438 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1439 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1440 I2C_FUNC_10BIT_ADDR;
1443 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1446 * In case of i2c defer or short i2c ack reply to a write,
1447 * we need to switch to WRITE_STATUS_UPDATE to drain the
1448 * rest of the message
1450 if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1451 msg->request &= DP_AUX_I2C_MOT;
1452 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1456 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1457 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1458 #define AUX_STOP_LEN 4
1459 #define AUX_CMD_LEN 4
1460 #define AUX_ADDRESS_LEN 20
1461 #define AUX_REPLY_PAD_LEN 4
1462 #define AUX_LENGTH_LEN 8
1465 * Calculate the duration of the AUX request/reply in usec. Gives the
1466 * "best" case estimate, ie. successful while as short as possible.
1468 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1470 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1471 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1473 if ((msg->request & DP_AUX_I2C_READ) == 0)
1474 len += msg->size * 8;
1479 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1481 int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1482 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1485 * For read we expect what was asked. For writes there will
1486 * be 0 or 1 data bytes. Assume 0 for the "best" case.
1488 if (msg->request & DP_AUX_I2C_READ)
1489 len += msg->size * 8;
1494 #define I2C_START_LEN 1
1495 #define I2C_STOP_LEN 1
1496 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1497 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1500 * Calculate the length of the i2c transfer in usec, assuming
1501 * the i2c bus speed is as specified. Gives the the "worst"
1502 * case estimate, ie. successful while as long as possible.
1503 * Doesn't account the the "MOT" bit, and instead assumes each
1504 * message includes a START, ADDRESS and STOP. Neither does it
1505 * account for additional random variables such as clock stretching.
1507 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1510 /* AUX bitrate is 1MHz, i2c bitrate as specified */
1511 return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1512 msg->size * I2C_DATA_LEN +
1513 I2C_STOP_LEN) * 1000, i2c_speed_khz);
1517 * Determine how many retries should be attempted to successfully transfer
1518 * the specified message, based on the estimated durations of the
1519 * i2c and AUX transfers.
1521 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1524 int aux_time_us = drm_dp_aux_req_duration(msg) +
1525 drm_dp_aux_reply_duration(msg);
1526 int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1528 return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1532 * FIXME currently assumes 10 kHz as some real world devices seem
1533 * to require it. We should query/set the speed via DPCD if supported.
1535 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1536 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1537 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1538 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1541 * Transfer a single I2C-over-AUX message and handle various error conditions,
1542 * retrying the transaction as appropriate. It is assumed that the
1543 * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1546 * Returns bytes transferred on success, or a negative error code on failure.
1548 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1550 unsigned int retry, defer_i2c;
1553 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1554 * is required to retry at least seven times upon receiving AUX_DEFER
1555 * before giving up the AUX transaction.
1557 * We also try to account for the i2c bus speed.
1559 int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1561 for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1562 ret = aux->transfer(aux, msg);
1568 * While timeouts can be errors, they're usually normal
1569 * behavior (for instance, when a driver tries to
1570 * communicate with a non-existent DisplayPort device).
1571 * Avoid spamming the kernel log with timeout errors.
1573 if (ret == -ETIMEDOUT)
1574 drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",
1577 drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",
1583 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1584 case DP_AUX_NATIVE_REPLY_ACK:
1586 * For I2C-over-AUX transactions this isn't enough, we
1587 * need to check for the I2C ACK reply.
1591 case DP_AUX_NATIVE_REPLY_NACK:
1592 drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",
1593 aux->name, ret, msg->size);
1596 case DP_AUX_NATIVE_REPLY_DEFER:
1597 drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);
1599 * We could check for I2C bit rate capabilities and if
1600 * available adjust this interval. We could also be
1601 * more careful with DP-to-legacy adapters where a
1602 * long legacy cable may force very low I2C bit rates.
1604 * For now just defer for long enough to hopefully be
1605 * safe for all use-cases.
1607 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1611 drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",
1612 aux->name, msg->reply);
1616 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1617 case DP_AUX_I2C_REPLY_ACK:
1619 * Both native ACK and I2C ACK replies received. We
1620 * can assume the transfer was successful.
1622 if (ret != msg->size)
1623 drm_dp_i2c_msg_write_status_update(msg);
1626 case DP_AUX_I2C_REPLY_NACK:
1627 drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",
1628 aux->name, ret, msg->size);
1629 aux->i2c_nack_count++;
1632 case DP_AUX_I2C_REPLY_DEFER:
1633 drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);
1634 /* DP Compliance Test 4.2.2.5 Requirement:
1635 * Must have at least 7 retries for I2C defers on the
1636 * transaction to pass this test
1638 aux->i2c_defer_count++;
1641 usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1642 drm_dp_i2c_msg_write_status_update(msg);
1647 drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",
1648 aux->name, msg->reply);
1653 drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);
1657 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1658 const struct i2c_msg *i2c_msg)
1660 msg->request = (i2c_msg->flags & I2C_M_RD) ?
1661 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1662 if (!(i2c_msg->flags & I2C_M_STOP))
1663 msg->request |= DP_AUX_I2C_MOT;
1667 * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1669 * Returns an error code on failure, or a recommended transfer size on success.
1671 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1673 int err, ret = orig_msg->size;
1674 struct drm_dp_aux_msg msg = *orig_msg;
1676 while (msg.size > 0) {
1677 err = drm_dp_i2c_do_msg(aux, &msg);
1679 return err == 0 ? -EPROTO : err;
1681 if (err < msg.size && err < ret) {
1682 drm_dbg_kms(aux->drm_dev,
1683 "%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1684 aux->name, msg.size, err);
1696 * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1697 * packets to be as large as possible. If not, the I2C transactions never
1698 * succeed. Hence the default is maximum.
1700 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1701 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1702 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1703 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1705 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1708 struct drm_dp_aux *aux = adapter->algo_data;
1710 unsigned transfer_size;
1711 struct drm_dp_aux_msg msg;
1714 dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1716 memset(&msg, 0, sizeof(msg));
1718 for (i = 0; i < num; i++) {
1719 msg.address = msgs[i].addr;
1720 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1721 /* Send a bare address packet to start the transaction.
1722 * Zero sized messages specify an address only (bare
1723 * address) transaction.
1727 err = drm_dp_i2c_do_msg(aux, &msg);
1730 * Reset msg.request in case in case it got
1731 * changed into a WRITE_STATUS_UPDATE.
1733 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1737 /* We want each transaction to be as large as possible, but
1738 * we'll go to smaller sizes if the hardware gives us a
1741 transfer_size = dp_aux_i2c_transfer_size;
1742 for (j = 0; j < msgs[i].len; j += msg.size) {
1743 msg.buffer = msgs[i].buf + j;
1744 msg.size = min(transfer_size, msgs[i].len - j);
1746 err = drm_dp_i2c_drain_msg(aux, &msg);
1749 * Reset msg.request in case in case it got
1750 * changed into a WRITE_STATUS_UPDATE.
1752 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1756 transfer_size = err;
1763 /* Send a bare address packet to close out the transaction.
1764 * Zero sized messages specify an address only (bare
1765 * address) transaction.
1767 msg.request &= ~DP_AUX_I2C_MOT;
1770 (void)drm_dp_i2c_do_msg(aux, &msg);
1775 static const struct i2c_algorithm drm_dp_i2c_algo = {
1776 .functionality = drm_dp_i2c_functionality,
1777 .master_xfer = drm_dp_i2c_xfer,
1780 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1782 return container_of(i2c, struct drm_dp_aux, ddc);
1785 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1787 mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1790 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1792 return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1795 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1797 mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1800 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1801 .lock_bus = lock_bus,
1802 .trylock_bus = trylock_bus,
1803 .unlock_bus = unlock_bus,
1806 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1811 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1815 WARN_ON(!(buf & DP_TEST_SINK_START));
1817 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1821 count = buf & DP_TEST_COUNT_MASK;
1822 if (count == aux->crc_count)
1823 return -EAGAIN; /* No CRC yet */
1825 aux->crc_count = count;
1828 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1829 * per component (RGB or CrYCb).
1831 ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1838 static void drm_dp_aux_crc_work(struct work_struct *work)
1840 struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1842 struct drm_crtc *crtc;
1847 if (WARN_ON(!aux->crtc))
1851 while (crtc->crc.opened) {
1852 drm_crtc_wait_one_vblank(crtc);
1853 if (!crtc->crc.opened)
1856 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1857 if (ret == -EAGAIN) {
1858 usleep_range(1000, 2000);
1859 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1862 if (ret == -EAGAIN) {
1863 drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",
1867 drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);
1871 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1872 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1873 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1874 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1879 * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1880 * @aux: DisplayPort AUX channel
1882 * Used for remote aux channel in general. Merely initialize the crc work
1885 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1887 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1889 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1892 * drm_dp_aux_init() - minimally initialise an aux channel
1893 * @aux: DisplayPort AUX channel
1895 * If you need to use the drm_dp_aux's i2c adapter prior to registering it with
1896 * the outside world, call drm_dp_aux_init() first. For drivers which are
1897 * grandparents to their AUX adapters (e.g. the AUX adapter is parented by a
1898 * &drm_connector), you must still call drm_dp_aux_register() once the connector
1899 * has been registered to allow userspace access to the auxiliary DP channel.
1900 * Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as
1901 * early as possible so that the &drm_device that corresponds to the AUX adapter
1902 * may be mentioned in debugging output from the DRM DP helpers.
1904 * For devices which use a separate platform device for their AUX adapters, this
1905 * may be called as early as required by the driver.
1908 void drm_dp_aux_init(struct drm_dp_aux *aux)
1910 mutex_init(&aux->hw_mutex);
1911 mutex_init(&aux->cec.lock);
1912 INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1914 aux->ddc.algo = &drm_dp_i2c_algo;
1915 aux->ddc.algo_data = aux;
1916 aux->ddc.retries = 3;
1918 aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1920 EXPORT_SYMBOL(drm_dp_aux_init);
1923 * drm_dp_aux_register() - initialise and register aux channel
1924 * @aux: DisplayPort AUX channel
1926 * Automatically calls drm_dp_aux_init() if this hasn't been done yet. This
1927 * should only be called once the parent of @aux, &drm_dp_aux.dev, is
1928 * initialized. For devices which are grandparents of their AUX channels,
1929 * &drm_dp_aux.dev will typically be the &drm_connector &device which
1930 * corresponds to @aux. For these devices, it's advised to call
1931 * drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to
1932 * call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.
1933 * Functions which don't follow this will likely Oops when
1934 * %CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1936 * For devices where the AUX channel is a device that exists independently of
1937 * the &drm_device that uses it, such as SoCs and bridge devices, it is
1938 * recommended to call drm_dp_aux_register() after a &drm_device has been
1939 * assigned to &drm_dp_aux.drm_dev, and likewise to call
1940 * drm_dp_aux_unregister() once the &drm_device should no longer be associated
1941 * with the AUX channel (e.g. on bridge detach).
1943 * Drivers which need to use the aux channel before either of the two points
1944 * mentioned above need to call drm_dp_aux_init() in order to use the AUX
1945 * channel before registration.
1947 * Returns 0 on success or a negative error code on failure.
1949 int drm_dp_aux_register(struct drm_dp_aux *aux)
1953 WARN_ON_ONCE(!aux->drm_dev);
1956 drm_dp_aux_init(aux);
1958 aux->ddc.class = I2C_CLASS_DDC;
1959 aux->ddc.owner = THIS_MODULE;
1960 aux->ddc.dev.parent = aux->dev;
1962 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1963 sizeof(aux->ddc.name));
1965 ret = drm_dp_aux_register_devnode(aux);
1969 ret = i2c_add_adapter(&aux->ddc);
1971 drm_dp_aux_unregister_devnode(aux);
1977 EXPORT_SYMBOL(drm_dp_aux_register);
1980 * drm_dp_aux_unregister() - unregister an AUX adapter
1981 * @aux: DisplayPort AUX channel
1983 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1985 drm_dp_aux_unregister_devnode(aux);
1986 i2c_del_adapter(&aux->ddc);
1988 EXPORT_SYMBOL(drm_dp_aux_unregister);
1990 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1993 * drm_dp_psr_setup_time() - PSR setup in time usec
1994 * @psr_cap: PSR capabilities from DPCD
1997 * PSR setup time for the panel in microseconds, negative
1998 * error code on failure.
2000 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
2002 static const u16 psr_setup_time_us[] = {
2003 PSR_SETUP_TIME(330),
2004 PSR_SETUP_TIME(275),
2005 PSR_SETUP_TIME(220),
2006 PSR_SETUP_TIME(165),
2007 PSR_SETUP_TIME(110),
2013 i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
2014 if (i >= ARRAY_SIZE(psr_setup_time_us))
2017 return psr_setup_time_us[i];
2019 EXPORT_SYMBOL(drm_dp_psr_setup_time);
2021 #undef PSR_SETUP_TIME
2024 * drm_dp_start_crc() - start capture of frame CRCs
2025 * @aux: DisplayPort AUX channel
2026 * @crtc: CRTC displaying the frames whose CRCs are to be captured
2028 * Returns 0 on success or a negative error code on failure.
2030 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
2035 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
2039 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
2045 schedule_work(&aux->crc_work);
2049 EXPORT_SYMBOL(drm_dp_start_crc);
2052 * drm_dp_stop_crc() - stop capture of frame CRCs
2053 * @aux: DisplayPort AUX channel
2055 * Returns 0 on success or a negative error code on failure.
2057 int drm_dp_stop_crc(struct drm_dp_aux *aux)
2062 ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
2066 ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
2070 flush_work(&aux->crc_work);
2075 EXPORT_SYMBOL(drm_dp_stop_crc);
2084 #define OUI(first, second, third) { (first), (second), (third) }
2085 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
2086 { (first), (second), (third), (fourth), (fifth), (sixth) }
2088 #define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)
2090 static const struct dpcd_quirk dpcd_quirk_list[] = {
2091 /* Analogix 7737 needs reduced M and N at HBR2 link rates */
2092 { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
2093 /* LG LP140WF6-SPM1 eDP panel */
2094 { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
2095 /* Apple panels need some additional handling to support PSR */
2096 { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
2097 /* CH7511 seems to leave SINK_COUNT zeroed */
2098 { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
2099 /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
2100 { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
2101 /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
2102 { 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) },
2108 * Get a bit mask of DPCD quirks for the sink/branch device identified by
2109 * ident. The quirk data is shared but it's up to the drivers to act on the
2112 * For now, only the OUI (first three bytes) is used, but this may be extended
2113 * to device identification string and hardware/firmware revisions later.
2116 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
2118 const struct dpcd_quirk *quirk;
2121 u8 any_device[] = DEVICE_ID_ANY;
2123 for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
2124 quirk = &dpcd_quirk_list[i];
2126 if (quirk->is_branch != is_branch)
2129 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
2132 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
2133 memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
2136 quirks |= quirk->quirks;
2142 #undef DEVICE_ID_ANY
2146 * drm_dp_read_desc - read sink/branch descriptor from DPCD
2147 * @aux: DisplayPort AUX channel
2148 * @desc: Device descriptor to fill from DPCD
2149 * @is_branch: true for branch devices, false for sink devices
2151 * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
2154 * Returns 0 on success or a negative error code on failure.
2156 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
2159 struct drm_dp_dpcd_ident *ident = &desc->ident;
2160 unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
2161 int ret, dev_id_len;
2163 ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
2167 desc->quirks = drm_dp_get_quirks(ident, is_branch);
2169 dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
2171 drm_dbg_kms(aux->drm_dev,
2172 "%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
2173 aux->name, is_branch ? "branch" : "sink",
2174 (int)sizeof(ident->oui), ident->oui, dev_id_len,
2175 ident->device_id, ident->hw_rev >> 4, ident->hw_rev & 0xf,
2176 ident->sw_major_rev, ident->sw_minor_rev, desc->quirks);
2180 EXPORT_SYMBOL(drm_dp_read_desc);
2183 * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
2184 * supported by the DSC sink.
2185 * @dsc_dpcd: DSC capabilities from DPCD
2186 * @is_edp: true if its eDP, false for DP
2188 * Read the slice capabilities DPCD register from DSC sink to get
2189 * the maximum slice count supported. This is used to populate
2190 * the DSC parameters in the &struct drm_dsc_config by the driver.
2191 * Driver creates an infoframe using these parameters to populate
2192 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2193 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2196 * Maximum slice count supported by DSC sink or 0 its invalid
2198 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2201 u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
2204 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
2205 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2207 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2209 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2212 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
2213 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
2215 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
2217 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
2219 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
2221 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
2223 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2225 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2227 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2229 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2231 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2233 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2239 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2242 * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2243 * @dsc_dpcd: DSC capabilities from DPCD
2245 * Read the DSC DPCD register to parse the line buffer depth in bits which is
2246 * number of bits of precision within the decoder line buffer supported by
2247 * the DSC sink. This is used to populate the DSC parameters in the
2248 * &struct drm_dsc_config by the driver.
2249 * Driver creates an infoframe using these parameters to populate
2250 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2251 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2254 * Line buffer depth supported by DSC panel or 0 its invalid
2256 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2258 u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2260 switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2261 case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2263 case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2265 case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2267 case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2269 case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2271 case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2273 case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2275 case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2277 case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2283 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2286 * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2287 * values supported by the DSC sink.
2288 * @dsc_dpcd: DSC capabilities from DPCD
2289 * @dsc_bpc: An array to be filled by this helper with supported
2292 * Read the DSC DPCD from the sink device to parse the supported bits per
2293 * component values. This is used to populate the DSC parameters
2294 * in the &struct drm_dsc_config by the driver.
2295 * Driver creates an infoframe using these parameters to populate
2296 * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2297 * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2300 * Number of input BPC values parsed from the DPCD
2302 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2306 u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2308 if (color_depth & DP_DSC_12_BPC)
2309 dsc_bpc[num_bpc++] = 12;
2310 if (color_depth & DP_DSC_10_BPC)
2311 dsc_bpc[num_bpc++] = 10;
2312 if (color_depth & DP_DSC_8_BPC)
2313 dsc_bpc[num_bpc++] = 8;
2317 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2320 * drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities
2321 * @aux: DisplayPort AUX channel
2322 * @caps: buffer to return the capability info in
2324 * Read capabilities common to all LTTPRs.
2326 * Returns 0 on success or a negative error code on failure.
2328 int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,
2329 u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2333 ret = drm_dp_dpcd_read(aux,
2334 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,
2335 caps, DP_LTTPR_COMMON_CAP_SIZE);
2339 WARN_ON(ret != DP_LTTPR_COMMON_CAP_SIZE);
2343 EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);
2346 * drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY
2347 * @aux: DisplayPort AUX channel
2348 * @dp_phy: LTTPR PHY to read the capabilities for
2349 * @caps: buffer to return the capability info in
2351 * Read the capabilities for the given LTTPR PHY.
2353 * Returns 0 on success or a negative error code on failure.
2355 int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,
2356 enum drm_dp_phy dp_phy,
2357 u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2361 ret = drm_dp_dpcd_read(aux,
2362 DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),
2363 caps, DP_LTTPR_PHY_CAP_SIZE);
2367 WARN_ON(ret != DP_LTTPR_PHY_CAP_SIZE);
2371 EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);
2373 static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)
2375 return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];
2379 * drm_dp_lttpr_count - get the number of detected LTTPRs
2380 * @caps: LTTPR common capabilities
2382 * Get the number of detected LTTPRs from the LTTPR common capabilities info.
2385 * -ERANGE if more than supported number (8) of LTTPRs are detected
2386 * -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value
2387 * otherwise the number of detected LTTPRs
2389 int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2391 u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);
2393 switch (hweight8(count)) {
2397 return 8 - ilog2(count);
2404 EXPORT_SYMBOL(drm_dp_lttpr_count);
2407 * drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs
2408 * @caps: LTTPR common capabilities
2410 * Returns the maximum link rate supported by all detected LTTPRs.
2412 int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2414 u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);
2416 return drm_dp_bw_code_to_link_rate(rate);
2418 EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);
2421 * drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs
2422 * @caps: LTTPR common capabilities
2424 * Returns the maximum lane count supported by all detected LTTPRs.
2426 int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])
2428 u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);
2430 return max_lanes & DP_MAX_LANE_COUNT_MASK;
2432 EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);
2435 * drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support
2436 * @caps: LTTPR PHY capabilities
2438 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2439 * voltage swing level 3.
2442 drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2444 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2446 return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;
2448 EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);
2451 * drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support
2452 * @caps: LTTPR PHY capabilities
2454 * Returns true if the @caps for an LTTPR TX PHY indicate support for
2455 * pre-emphasis level 3.
2458 drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])
2460 u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);
2462 return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;
2464 EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);
2467 * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2468 * @aux: DisplayPort AUX channel
2469 * @data: DP phy compliance test parameters.
2471 * Returns 0 on success or a negative error code on failure.
2473 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2474 struct drm_dp_phy_test_params *data)
2479 err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2482 data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2484 err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2487 data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2489 if (lanes & DP_ENHANCED_FRAME_CAP)
2490 data->enhanced_frame_cap = true;
2492 err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2496 switch (data->phy_pattern) {
2497 case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2498 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2499 &data->custom80, sizeof(data->custom80));
2504 case DP_PHY_TEST_PATTERN_CP2520:
2505 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2507 sizeof(data->hbr2_reset));
2514 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2517 * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2518 * @aux: DisplayPort AUX channel
2519 * @data: DP phy compliance test parameters.
2520 * @dp_rev: DP revision to use for compliance testing
2522 * Returns 0 on success or a negative error code on failure.
2524 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2525 struct drm_dp_phy_test_params *data, u8 dp_rev)
2531 link_config[0] = drm_dp_link_rate_to_bw_code(data->link_rate);
2532 link_config[1] = data->num_lanes;
2533 if (data->enhanced_frame_cap)
2534 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
2535 err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, link_config, 2);
2539 test_pattern = data->phy_pattern;
2540 if (dp_rev < 0x12) {
2541 test_pattern = (test_pattern << 2) &
2542 DP_LINK_QUAL_PATTERN_11_MASK;
2543 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2548 for (i = 0; i < data->num_lanes; i++) {
2549 err = drm_dp_dpcd_writeb(aux,
2550 DP_LINK_QUAL_LANE0_SET + i,
2559 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2561 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2563 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2566 switch (pixelformat) {
2567 case DP_PIXELFORMAT_RGB:
2569 case DP_PIXELFORMAT_YUV444:
2571 case DP_PIXELFORMAT_YUV422:
2573 case DP_PIXELFORMAT_YUV420:
2575 case DP_PIXELFORMAT_Y_ONLY:
2577 case DP_PIXELFORMAT_RAW:
2584 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2585 enum dp_colorimetry colorimetry)
2587 if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2590 switch (colorimetry) {
2591 case DP_COLORIMETRY_DEFAULT:
2592 switch (pixelformat) {
2593 case DP_PIXELFORMAT_RGB:
2595 case DP_PIXELFORMAT_YUV444:
2596 case DP_PIXELFORMAT_YUV422:
2597 case DP_PIXELFORMAT_YUV420:
2599 case DP_PIXELFORMAT_Y_ONLY:
2600 return "DICOM PS3.14";
2601 case DP_PIXELFORMAT_RAW:
2602 return "Custom Color Profile";
2606 case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2607 switch (pixelformat) {
2608 case DP_PIXELFORMAT_RGB:
2609 return "Wide Fixed";
2610 case DP_PIXELFORMAT_YUV444:
2611 case DP_PIXELFORMAT_YUV422:
2612 case DP_PIXELFORMAT_YUV420:
2617 case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2618 switch (pixelformat) {
2619 case DP_PIXELFORMAT_RGB:
2620 return "Wide Float";
2621 case DP_PIXELFORMAT_YUV444:
2622 case DP_PIXELFORMAT_YUV422:
2623 case DP_PIXELFORMAT_YUV420:
2628 case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2629 switch (pixelformat) {
2630 case DP_PIXELFORMAT_RGB:
2632 case DP_PIXELFORMAT_YUV444:
2633 case DP_PIXELFORMAT_YUV422:
2634 case DP_PIXELFORMAT_YUV420:
2639 case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2640 switch (pixelformat) {
2641 case DP_PIXELFORMAT_RGB:
2643 case DP_PIXELFORMAT_YUV444:
2644 case DP_PIXELFORMAT_YUV422:
2645 case DP_PIXELFORMAT_YUV420:
2650 case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2651 switch (pixelformat) {
2652 case DP_PIXELFORMAT_RGB:
2653 return "Custom Profile";
2654 case DP_PIXELFORMAT_YUV444:
2655 case DP_PIXELFORMAT_YUV422:
2656 case DP_PIXELFORMAT_YUV420:
2661 case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2662 switch (pixelformat) {
2663 case DP_PIXELFORMAT_RGB:
2664 return "BT.2020 RGB";
2665 case DP_PIXELFORMAT_YUV444:
2666 case DP_PIXELFORMAT_YUV422:
2667 case DP_PIXELFORMAT_YUV420:
2668 return "BT.2020 CYCC";
2672 case DP_COLORIMETRY_BT2020_YCC:
2673 switch (pixelformat) {
2674 case DP_PIXELFORMAT_YUV444:
2675 case DP_PIXELFORMAT_YUV422:
2676 case DP_PIXELFORMAT_YUV420:
2677 return "BT.2020 YCC";
2686 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2688 switch (dynamic_range) {
2689 case DP_DYNAMIC_RANGE_VESA:
2690 return "VESA range";
2691 case DP_DYNAMIC_RANGE_CTA:
2698 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2700 switch (content_type) {
2701 case DP_CONTENT_TYPE_NOT_DEFINED:
2702 return "Not defined";
2703 case DP_CONTENT_TYPE_GRAPHICS:
2705 case DP_CONTENT_TYPE_PHOTO:
2707 case DP_CONTENT_TYPE_VIDEO:
2709 case DP_CONTENT_TYPE_GAME:
2716 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2717 const struct drm_dp_vsc_sdp *vsc)
2719 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2720 DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2721 vsc->revision, vsc->length);
2722 DP_SDP_LOG(" pixelformat: %s\n",
2723 dp_pixelformat_get_name(vsc->pixelformat));
2724 DP_SDP_LOG(" colorimetry: %s\n",
2725 dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2726 DP_SDP_LOG(" bpc: %u\n", vsc->bpc);
2727 DP_SDP_LOG(" dynamic range: %s\n",
2728 dp_dynamic_range_get_name(vsc->dynamic_range));
2729 DP_SDP_LOG(" content type: %s\n",
2730 dp_content_type_get_name(vsc->content_type));
2733 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);
2736 * drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON
2737 * @dpcd: DisplayPort configuration data
2738 * @port_cap: port capabilities
2740 * Returns maximum frl bandwidth supported by PCON in GBPS,
2741 * returns 0 if not supported.
2743 int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
2744 const u8 port_cap[4])
2750 bw = buf & DP_PCON_MAX_FRL_BW;
2753 case DP_PCON_MAX_9GBPS:
2755 case DP_PCON_MAX_18GBPS:
2757 case DP_PCON_MAX_24GBPS:
2759 case DP_PCON_MAX_32GBPS:
2761 case DP_PCON_MAX_40GBPS:
2763 case DP_PCON_MAX_48GBPS:
2765 case DP_PCON_MAX_0GBPS:
2772 EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);
2775 * drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.
2776 * @aux: DisplayPort AUX channel
2777 * @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.
2779 * Returns 0 if success, else returns negative error code.
2781 int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)
2784 u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |
2785 DP_PCON_ENABLE_LINK_FRL_MODE;
2787 if (enable_frl_ready_hpd)
2788 buf |= DP_PCON_ENABLE_HPD_READY;
2790 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2794 EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);
2797 * drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL
2798 * @aux: DisplayPort AUX channel
2800 * Returns true if success, else returns false.
2802 bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)
2807 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2811 if (buf & DP_PCON_FRL_READY)
2816 EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);
2819 * drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step1
2820 * @aux: DisplayPort AUX channel
2821 * @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink
2822 * @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.
2823 * In Concurrent Mode, the FRL link bring up can be done along with
2824 * DP Link training. In Sequential mode, the FRL link bring up is done prior to
2825 * the DP Link training.
2827 * Returns 0 if success, else returns negative error code.
2830 int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,
2836 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2840 if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)
2841 buf |= DP_PCON_ENABLE_CONCURRENT_LINK;
2843 buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;
2845 switch (max_frl_gbps) {
2847 buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;
2850 buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;
2853 buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;
2856 buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;
2859 buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;
2862 buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;
2865 buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;
2871 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2877 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);
2880 * drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-2
2881 * @aux: DisplayPort AUX channel
2882 * @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink
2883 * @frl_type : FRL training type, can be Extended, or Normal.
2884 * In Normal FRL training, the PCON tries each frl bw from the max_frl_mask
2885 * starting from min, and stops when link training is successful. In Extended
2886 * FRL training, all frl bw selected in the mask are trained by the PCON.
2888 * Returns 0 if success, else returns negative error code.
2890 int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,
2894 u8 buf = max_frl_mask;
2896 if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)
2897 buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2899 buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;
2901 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);
2907 EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);
2910 * drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.
2911 * @aux: DisplayPort AUX channel
2913 * Returns 0 if success, else returns negative error code.
2915 int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)
2919 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);
2925 EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);
2928 * drm_dp_pcon_frl_enable() - Enable HDMI link through FRL
2929 * @aux: DisplayPort AUX channel
2931 * Returns 0 if success, else returns negative error code.
2933 int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)
2938 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);
2941 if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {
2942 drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",
2946 buf |= DP_PCON_ENABLE_HDMI_LINK;
2947 ret = drm_dp_dpcd_writeb(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);
2953 EXPORT_SYMBOL(drm_dp_pcon_frl_enable);
2956 * drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.
2957 * @aux: DisplayPort AUX channel
2959 * Returns true if link is active else returns false.
2961 bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)
2966 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);
2970 return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;
2972 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);
2975 * drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE
2976 * @aux: DisplayPort AUX channel
2977 * @frl_trained_mask: pointer to store bitmask of the trained bw configuration.
2978 * Valid only if the MODE returned is FRL. For Normal Link training mode
2979 * only 1 of the bits will be set, but in case of Extended mode, more than
2980 * one bits can be set.
2982 * Returns the link mode : TMDS or FRL on success, else returns negative error
2985 int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)
2991 ret = drm_dp_dpcd_readb(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);
2995 mode = buf & DP_PCON_HDMI_LINK_MODE;
2997 if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)
2998 *frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;
3002 EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);
3005 * drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane
3006 * during link failure between PCON and HDMI sink
3007 * @aux: DisplayPort AUX channel
3008 * @connector: DRM connector
3012 void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,
3013 struct drm_connector *connector)
3015 u8 buf, error_count;
3017 struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
3019 for (i = 0; i < hdmi->max_lanes; i++) {
3020 if (drm_dp_dpcd_readb(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)
3023 error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;
3024 switch (error_count) {
3025 case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:
3028 case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:
3031 case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:
3038 drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",
3039 aux->name, num_error, i);
3042 EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);
3045 * drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.2
3046 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3048 * Returns true is PCON encoder is DSC 1.2 else returns false.
3050 bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3053 u8 major_v, minor_v;
3055 buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];
3056 major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;
3057 minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;
3059 if (major_v == 1 && minor_v == 2)
3064 EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);
3067 * drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder
3068 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3070 * Returns maximum no. of slices supported by the PCON DSC Encoder.
3072 int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3074 u8 slice_cap1, slice_cap2;
3076 slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];
3077 slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];
3079 if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)
3081 if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)
3083 if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)
3085 if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)
3087 if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)
3089 if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)
3091 if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)
3093 if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)
3095 if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)
3097 if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)
3102 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);
3105 * drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder
3106 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3108 * Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.
3110 int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3114 buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];
3116 return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;
3118 EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);
3121 * drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder
3122 * @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder
3124 * Returns the bpp precision supported by the PCON encoder.
3126 int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])
3130 buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];
3132 switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {
3133 case DP_PCON_DSC_ONE_16TH_BPP:
3135 case DP_PCON_DSC_ONE_8TH_BPP:
3137 case DP_PCON_DSC_ONE_4TH_BPP:
3139 case DP_PCON_DSC_ONE_HALF_BPP:
3141 case DP_PCON_DSC_ONE_BPP:
3147 EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);
3150 int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)
3155 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3159 buf |= DP_PCON_ENABLE_DSC_ENCODER;
3161 if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {
3162 buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;
3163 buf |= pps_buf_config << 2;
3166 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3174 * drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters
3175 * for DSC1.2 between PCON & HDMI2.1 sink
3176 * @aux: DisplayPort AUX channel
3178 * Returns 0 on success, else returns negative error code.
3180 int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)
3184 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);
3190 EXPORT_SYMBOL(drm_dp_pcon_pps_default);
3193 * drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for
3195 * @aux: DisplayPort AUX channel
3196 * @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.
3198 * Returns 0 on success, else returns negative error code.
3200 int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])
3204 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);
3208 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3214 EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);
3217 * drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder
3218 * override registers
3219 * @aux: DisplayPort AUX channel
3220 * @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,
3223 * Returns 0 on success, else returns negative error code.
3225 int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])
3229 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);
3232 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);
3235 ret = drm_dp_dpcd_write(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);
3239 ret = drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);
3245 EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);
3248 * drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr
3249 * @aux: displayPort AUX channel
3250 * @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.
3252 * Returns 0 on success, else returns negative error code.
3254 int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)
3259 ret = drm_dp_dpcd_readb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);
3263 if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)
3264 buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);
3266 buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;
3268 ret = drm_dp_dpcd_writeb(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);
3274 EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);
3277 * drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX
3278 * @aux: The DP AUX channel to use
3279 * @bl: Backlight capability info from drm_edp_backlight_init()
3280 * @level: The brightness level to set
3282 * Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must
3283 * already have been enabled by the driver by calling drm_edp_backlight_enable().
3285 * Returns: %0 on success, negative error code on failure
3287 int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3293 /* The panel uses the PWM for controlling brightness levels */
3297 if (bl->lsb_reg_used) {
3298 buf[0] = (level & 0xff00) >> 8;
3299 buf[1] = (level & 0x00ff);
3304 ret = drm_dp_dpcd_write(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, sizeof(buf));
3305 if (ret != sizeof(buf)) {
3306 drm_err(aux->drm_dev,
3307 "%s: Failed to write aux backlight level: %d\n",
3309 return ret < 0 ? ret : -EIO;
3314 EXPORT_SYMBOL(drm_edp_backlight_set_level);
3317 drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3323 /* This panel uses the EDP_BL_PWR GPIO for enablement */
3324 if (!bl->aux_enable)
3327 ret = drm_dp_dpcd_readb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);
3329 drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",
3331 return ret < 0 ? ret : -EIO;
3334 buf |= DP_EDP_BACKLIGHT_ENABLE;
3336 buf &= ~DP_EDP_BACKLIGHT_ENABLE;
3338 ret = drm_dp_dpcd_writeb(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);
3340 drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",
3342 return ret < 0 ? ret : -EIO;
3349 * drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD
3350 * @aux: The DP AUX channel to use
3351 * @bl: Backlight capability info from drm_edp_backlight_init()
3352 * @level: The initial backlight level to set via AUX, if there is one
3354 * This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally
3355 * restoring any important backlight state such as the given backlight level, the brightness byte
3356 * count, backlight frequency, etc.
3358 * Note that certain panels do not support being enabled or disabled via DPCD, but instead require
3359 * that the driver handle enabling/disabling the panel through implementation-specific means using
3360 * the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,
3361 * this function becomes a no-op, and the driver is expected to handle powering the panel on using
3362 * the EDP_BL_PWR GPIO.
3364 * Returns: %0 on success, negative error code on failure.
3366 int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,
3373 dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;
3375 dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_PWM;
3377 if (bl->pwmgen_bit_count) {
3378 ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);
3380 drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
3384 if (bl->pwm_freq_pre_divider) {
3385 ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_FREQ_SET, bl->pwm_freq_pre_divider);
3387 drm_dbg_kms(aux->drm_dev,
3388 "%s: Failed to write aux backlight frequency: %d\n",
3391 dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;
3394 ret = drm_dp_dpcd_writeb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf);
3396 drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",
3398 return ret < 0 ? ret : -EIO;
3401 ret = drm_edp_backlight_set_level(aux, bl, level);
3404 ret = drm_edp_backlight_set_enable(aux, bl, true);
3410 EXPORT_SYMBOL(drm_edp_backlight_enable);
3413 * drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported
3414 * @aux: The DP AUX channel to use
3415 * @bl: Backlight capability info from drm_edp_backlight_init()
3417 * This function handles disabling DPCD backlight controls on a panel over AUX.
3419 * Note that certain panels do not support being enabled or disabled via DPCD, but instead require
3420 * that the driver handle enabling/disabling the panel through implementation-specific means using
3421 * the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,
3422 * this function becomes a no-op, and the driver is expected to handle powering the panel off using
3423 * the EDP_BL_PWR GPIO.
3425 * Returns: %0 on success or no-op, negative error code on failure.
3427 int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)
3431 ret = drm_edp_backlight_set_enable(aux, bl, false);
3437 EXPORT_SYMBOL(drm_edp_backlight_disable);
3440 drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3441 u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])
3443 int fxp, fxp_min, fxp_max, fxp_actual, f = 1;
3445 u8 pn, pn_min, pn_max;
3450 ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT, &pn);
3452 drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",
3457 pn &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3458 bl->max = (1 << pn) - 1;
3459 if (!driver_pwm_freq_hz)
3463 * Set PWM Frequency divider to match desired frequency provided by the driver.
3464 * The PWM Frequency is calculated as 27Mhz / (F x P).
3465 * - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the
3466 * EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)
3467 * - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the
3468 * EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)
3471 /* Find desired value of (F x P)
3472 * Note that, if F x P is out of supported range, the maximum value or minimum value will
3473 * applied automatically. So no need to check that.
3475 fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);
3477 /* Use highest possible value of Pn for more granularity of brightness adjustment while
3478 * satisfying the conditions below.
3479 * - Pn is in the range of Pn_min and Pn_max
3480 * - F is in the range of 1 and 255
3481 * - FxP is within 25% of desired value.
3482 * Note: 25% is arbitrary value and may need some tweak.
3484 ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);
3486 drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",
3490 ret = drm_dp_dpcd_readb(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);
3492 drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",
3496 pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3497 pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
3499 /* Ensure frequency is within 25% of desired value */
3500 fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);
3501 fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);
3502 if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {
3503 drm_dbg_kms(aux->drm_dev,
3504 "%s: Driver defined backlight frequency (%d) out of range\n",
3505 aux->name, driver_pwm_freq_hz);
3509 for (pn = pn_max; pn >= pn_min; pn--) {
3510 f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);
3511 fxp_actual = f << pn;
3512 if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)
3516 ret = drm_dp_dpcd_writeb(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);
3518 drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",
3522 bl->pwmgen_bit_count = pn;
3523 bl->max = (1 << pn) - 1;
3525 if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {
3526 bl->pwm_freq_pre_divider = f;
3527 drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",
3528 aux->name, driver_pwm_freq_hz);
3535 drm_edp_backlight_probe_state(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3542 ret = drm_dp_dpcd_readb(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);
3544 drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",
3546 return ret < 0 ? ret : -EIO;
3549 *current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);
3553 if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {
3554 int size = 1 + bl->lsb_reg_used;
3556 ret = drm_dp_dpcd_read(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB, buf, size);
3558 drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight level: %d\n",
3560 return ret < 0 ? ret : -EIO;
3563 if (bl->lsb_reg_used)
3564 return (buf[0] << 8) | buf[1];
3570 * If we're not in DPCD control mode yet, the programmed brightness value is meaningless and
3571 * the driver should assume max brightness
3577 * drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight
3579 * @aux: The DP aux device to use for probing
3580 * @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight
3581 * @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz
3582 * @edp_dpcd: A cached copy of the eDP DPCD
3583 * @current_level: Where to store the probed brightness level, if any
3584 * @current_mode: Where to store the currently set backlight control mode
3586 * Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,
3587 * along with also probing the current and maximum supported brightness levels.
3589 * If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the
3590 * default frequency from the panel is used.
3592 * Returns: %0 on success, negative error code on failure.
3595 drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,
3596 u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],
3597 u16 *current_level, u8 *current_mode)
3601 if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)
3602 bl->aux_enable = true;
3603 if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)
3605 if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)
3606 bl->lsb_reg_used = true;
3608 /* Sanity check caps */
3609 if (!bl->aux_set && !(edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP)) {
3610 drm_dbg_kms(aux->drm_dev,
3611 "%s: Panel supports neither AUX or PWM brightness control? Aborting\n",
3616 ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);
3620 ret = drm_edp_backlight_probe_state(aux, bl, current_mode);
3623 *current_level = ret;
3625 drm_dbg_kms(aux->drm_dev,
3626 "%s: Found backlight: aux_set=%d aux_enable=%d mode=%d\n",
3627 aux->name, bl->aux_set, bl->aux_enable, *current_mode);
3629 drm_dbg_kms(aux->drm_dev,
3630 "%s: Backlight caps: level=%d/%d pwm_freq_pre_divider=%d lsb_reg_used=%d\n",
3631 aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider,
3637 EXPORT_SYMBOL(drm_edp_backlight_init);
3639 #if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
3640 (IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))
3642 static int dp_aux_backlight_update_status(struct backlight_device *bd)
3644 struct dp_aux_backlight *bl = bl_get_data(bd);
3645 u16 brightness = backlight_get_brightness(bd);
3648 if (!backlight_is_blank(bd)) {
3650 drm_edp_backlight_enable(bl->aux, &bl->info, brightness);
3654 ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);
3657 drm_edp_backlight_disable(bl->aux, &bl->info);
3658 bl->enabled = false;
3665 static const struct backlight_ops dp_aux_bl_ops = {
3666 .update_status = dp_aux_backlight_update_status,
3670 * drm_panel_dp_aux_backlight - create and use DP AUX backlight
3672 * @aux: The DP AUX channel to use
3674 * Use this function to create and handle backlight if your panel
3675 * supports backlight control over DP AUX channel using DPCD
3676 * registers as per VESA's standard backlight control interface.
3678 * When the panel is enabled backlight will be enabled after a
3679 * successful call to &drm_panel_funcs.enable()
3681 * When the panel is disabled backlight will be disabled before the
3682 * call to &drm_panel_funcs.disable().
3684 * A typical implementation for a panel driver supporting backlight
3685 * control over DP AUX will call this function at probe time.
3686 * Backlight will then be handled transparently without requiring
3687 * any intervention from the driver.
3689 * drm_panel_dp_aux_backlight() must be called after the call to drm_panel_init().
3691 * Return: 0 on success or a negative error code on failure.
3693 int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)
3695 struct dp_aux_backlight *bl;
3696 struct backlight_properties props = { 0 };
3699 u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
3702 if (!panel || !panel->dev || !aux)
3705 ret = drm_dp_dpcd_read(aux, DP_EDP_DPCD_REV, edp_dpcd,
3706 EDP_DISPLAY_CTL_CAP_SIZE);
3710 if (!drm_edp_backlight_supported(edp_dpcd)) {
3711 DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");
3715 bl = devm_kzalloc(panel->dev, sizeof(*bl), GFP_KERNEL);
3721 ret = drm_edp_backlight_init(aux, &bl->info, 0, edp_dpcd,
3722 ¤t_level, ¤t_mode);
3726 props.type = BACKLIGHT_RAW;
3727 props.brightness = current_level;
3728 props.max_brightness = bl->info.max;
3730 bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",
3732 &dp_aux_bl_ops, &props);
3733 if (IS_ERR(bl->base))
3734 return PTR_ERR(bl->base);
3736 backlight_disable(bl->base);
3738 panel->backlight = bl->base;
3742 EXPORT_SYMBOL(drm_panel_dp_aux_backlight);