xref: /dflybsd-src/sys/dev/drm/drm_dp_helper.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
16f486c69SFrançois Tigeot /*
26f486c69SFrançois Tigeot  * Copyright © 2009 Keith Packard
36f486c69SFrançois Tigeot  *
46f486c69SFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
56f486c69SFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
66f486c69SFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
76f486c69SFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
86f486c69SFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
96f486c69SFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
106f486c69SFrançois Tigeot  * written prior permission.  The copyright holders make no representations
116f486c69SFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
126f486c69SFrançois Tigeot  * is" without express or implied warranty.
136f486c69SFrançois Tigeot  *
146f486c69SFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
156f486c69SFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
166f486c69SFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
176f486c69SFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
186f486c69SFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
196f486c69SFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
206f486c69SFrançois Tigeot  * OF THIS SOFTWARE.
216f486c69SFrançois Tigeot  */
226f486c69SFrançois Tigeot 
23477eb7f9SFrançois Tigeot #include <linux/kernel.h>
24477eb7f9SFrançois Tigeot #include <linux/module.h>
25477eb7f9SFrançois Tigeot #include <linux/delay.h>
268b983a81SFrançois Tigeot #include <linux/init.h>
27477eb7f9SFrançois Tigeot #include <linux/errno.h>
28477eb7f9SFrançois Tigeot #include <linux/sched.h>
29477eb7f9SFrançois Tigeot #include <linux/i2c.h>
301dedbd3bSFrançois Tigeot #include <linux/seq_file.h>
3118e26a6dSFrançois Tigeot #include <drm/drm_dp_helper.h>
32477eb7f9SFrançois Tigeot #include <drm/drmP.h>
336f486c69SFrançois Tigeot 
341dedbd3bSFrançois Tigeot #include "drm_crtc_helper_internal.h"
351dedbd3bSFrançois Tigeot 
36477eb7f9SFrançois Tigeot /**
37477eb7f9SFrançois Tigeot  * DOC: dp helpers
38477eb7f9SFrançois Tigeot  *
39477eb7f9SFrançois Tigeot  * These functions contain some common logic and helpers at various abstraction
40477eb7f9SFrançois Tigeot  * levels to deal with Display Port sink devices and related things like DP aux
41477eb7f9SFrançois Tigeot  * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
42477eb7f9SFrançois Tigeot  * blocks, ...
43477eb7f9SFrançois Tigeot  */
446f486c69SFrançois Tigeot 
4519df918dSFrançois Tigeot /* Helpers for DP link training */
dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE],int r)469edbd4a0SFrançois Tigeot static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
476f486c69SFrançois Tigeot {
486f486c69SFrançois Tigeot 	return link_status[r - DP_LANE0_1_STATUS];
496f486c69SFrançois Tigeot }
506f486c69SFrançois Tigeot 
dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],int lane)519edbd4a0SFrançois Tigeot static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
526f486c69SFrançois Tigeot 			     int lane)
536f486c69SFrançois Tigeot {
546f486c69SFrançois Tigeot 	int i = DP_LANE0_1_STATUS + (lane >> 1);
556f486c69SFrançois Tigeot 	int s = (lane & 1) * 4;
566f486c69SFrançois Tigeot 	u8 l = dp_link_status(link_status, i);
576f486c69SFrançois Tigeot 	return (l >> s) & 0xf;
586f486c69SFrançois Tigeot }
596f486c69SFrançois Tigeot 
drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],int lane_count)609edbd4a0SFrançois Tigeot bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
616f486c69SFrançois Tigeot 			  int lane_count)
626f486c69SFrançois Tigeot {
636f486c69SFrançois Tigeot 	u8 lane_align;
646f486c69SFrançois Tigeot 	u8 lane_status;
656f486c69SFrançois Tigeot 	int lane;
666f486c69SFrançois Tigeot 
676f486c69SFrançois Tigeot 	lane_align = dp_link_status(link_status,
686f486c69SFrançois Tigeot 				    DP_LANE_ALIGN_STATUS_UPDATED);
696f486c69SFrançois Tigeot 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
706f486c69SFrançois Tigeot 		return false;
716f486c69SFrançois Tigeot 	for (lane = 0; lane < lane_count; lane++) {
726f486c69SFrançois Tigeot 		lane_status = dp_get_lane_status(link_status, lane);
736f486c69SFrançois Tigeot 		if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
746f486c69SFrançois Tigeot 			return false;
756f486c69SFrançois Tigeot 	}
766f486c69SFrançois Tigeot 	return true;
776f486c69SFrançois Tigeot }
7819df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_channel_eq_ok);
796f486c69SFrançois Tigeot 
drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],int lane_count)809edbd4a0SFrançois Tigeot bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
816f486c69SFrançois Tigeot 			      int lane_count)
826f486c69SFrançois Tigeot {
836f486c69SFrançois Tigeot 	int lane;
846f486c69SFrançois Tigeot 	u8 lane_status;
856f486c69SFrançois Tigeot 
866f486c69SFrançois Tigeot 	for (lane = 0; lane < lane_count; lane++) {
876f486c69SFrançois Tigeot 		lane_status = dp_get_lane_status(link_status, lane);
886f486c69SFrançois Tigeot 		if ((lane_status & DP_LANE_CR_DONE) == 0)
896f486c69SFrançois Tigeot 			return false;
906f486c69SFrançois Tigeot 	}
916f486c69SFrançois Tigeot 	return true;
926f486c69SFrançois Tigeot }
9319df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
946f486c69SFrançois Tigeot 
drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],int lane)959edbd4a0SFrançois Tigeot u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
966f486c69SFrançois Tigeot 				     int lane)
976f486c69SFrançois Tigeot {
986f486c69SFrançois Tigeot 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
996f486c69SFrançois Tigeot 	int s = ((lane & 1) ?
1006f486c69SFrançois Tigeot 		 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
1016f486c69SFrançois Tigeot 		 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
1026f486c69SFrançois Tigeot 	u8 l = dp_link_status(link_status, i);
1036f486c69SFrançois Tigeot 
1046f486c69SFrançois Tigeot 	return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
1056f486c69SFrançois Tigeot }
10619df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
1076f486c69SFrançois Tigeot 
drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],int lane)1089edbd4a0SFrançois Tigeot u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
1096f486c69SFrançois Tigeot 					  int lane)
1106f486c69SFrançois Tigeot {
1116f486c69SFrançois Tigeot 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
1126f486c69SFrançois Tigeot 	int s = ((lane & 1) ?
1136f486c69SFrançois Tigeot 		 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
1146f486c69SFrançois Tigeot 		 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
1156f486c69SFrançois Tigeot 	u8 l = dp_link_status(link_status, i);
1166f486c69SFrançois Tigeot 
1176f486c69SFrançois Tigeot 	return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
1186f486c69SFrançois Tigeot }
11919df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
1206f486c69SFrançois Tigeot 
drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])1219edbd4a0SFrançois Tigeot void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
1226f486c69SFrançois Tigeot 	if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
12319df918dSFrançois Tigeot 		udelay(100);
1246f486c69SFrançois Tigeot 	else
12519df918dSFrançois Tigeot 		mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
1266f486c69SFrançois Tigeot }
12719df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
1286f486c69SFrançois Tigeot 
drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])1299edbd4a0SFrançois Tigeot void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE]) {
1306f486c69SFrançois Tigeot 	if (dpcd[DP_TRAINING_AUX_RD_INTERVAL] == 0)
13119df918dSFrançois Tigeot 		udelay(400);
1326f486c69SFrançois Tigeot 	else
13319df918dSFrançois Tigeot 		mdelay(dpcd[DP_TRAINING_AUX_RD_INTERVAL] * 4);
1346f486c69SFrançois Tigeot }
13519df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
1366f486c69SFrançois Tigeot 
drm_dp_link_rate_to_bw_code(int link_rate)1376f486c69SFrançois Tigeot u8 drm_dp_link_rate_to_bw_code(int link_rate)
1386f486c69SFrançois Tigeot {
1396f486c69SFrançois Tigeot 	switch (link_rate) {
1406f486c69SFrançois Tigeot 	default:
141*3f2dd94aSFrançois Tigeot 		WARN(1, "unknown DP link rate %d, using %x\n", link_rate,
142*3f2dd94aSFrançois Tigeot 		     DP_LINK_BW_1_62);
143*3f2dd94aSFrançois Tigeot 	case 162000:
1446f486c69SFrançois Tigeot 		return DP_LINK_BW_1_62;
1456f486c69SFrançois Tigeot 	case 270000:
1466f486c69SFrançois Tigeot 		return DP_LINK_BW_2_7;
1476f486c69SFrançois Tigeot 	case 540000:
1486f486c69SFrançois Tigeot 		return DP_LINK_BW_5_4;
1496f486c69SFrançois Tigeot 	}
1506f486c69SFrançois Tigeot }
15119df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
1526f486c69SFrançois Tigeot 
drm_dp_bw_code_to_link_rate(u8 link_bw)1536f486c69SFrançois Tigeot int drm_dp_bw_code_to_link_rate(u8 link_bw)
1546f486c69SFrançois Tigeot {
1556f486c69SFrançois Tigeot 	switch (link_bw) {
1566f486c69SFrançois Tigeot 	default:
157*3f2dd94aSFrançois Tigeot 		WARN(1, "unknown DP link BW code %x, using 162000\n", link_bw);
158*3f2dd94aSFrançois Tigeot 	case DP_LINK_BW_1_62:
1596f486c69SFrançois Tigeot 		return 162000;
1606f486c69SFrançois Tigeot 	case DP_LINK_BW_2_7:
1616f486c69SFrançois Tigeot 		return 270000;
1626f486c69SFrançois Tigeot 	case DP_LINK_BW_5_4:
1636f486c69SFrançois Tigeot 		return 540000;
1646f486c69SFrançois Tigeot 	}
1656f486c69SFrançois Tigeot }
16619df918dSFrançois Tigeot EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
16724edb884SFrançois Tigeot 
1689f4ca867SFrançois Tigeot #define AUX_RETRY_INTERVAL 500 /* us */
1699f4ca867SFrançois Tigeot 
17024edb884SFrançois Tigeot /**
17124edb884SFrançois Tigeot  * DOC: dp helpers
17224edb884SFrançois Tigeot  *
17324edb884SFrançois Tigeot  * The DisplayPort AUX channel is an abstraction to allow generic, driver-
17424edb884SFrançois Tigeot  * independent access to AUX functionality. Drivers can take advantage of
17524edb884SFrançois Tigeot  * this by filling in the fields of the drm_dp_aux structure.
17624edb884SFrançois Tigeot  *
17724edb884SFrançois Tigeot  * Transactions are described using a hardware-independent drm_dp_aux_msg
17824edb884SFrançois Tigeot  * structure, which is passed into a driver's .transfer() implementation.
17924edb884SFrançois Tigeot  * Both native and I2C-over-AUX transactions are supported.
18024edb884SFrançois Tigeot  */
18124edb884SFrançois Tigeot 
drm_dp_dpcd_access(struct drm_dp_aux * aux,u8 request,unsigned int offset,void * buffer,size_t size)18224edb884SFrançois Tigeot static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
18324edb884SFrançois Tigeot 			      unsigned int offset, void *buffer, size_t size)
18424edb884SFrançois Tigeot {
18524edb884SFrançois Tigeot 	struct drm_dp_aux_msg msg;
1868621f407SFrançois Tigeot 	unsigned int retry, native_reply;
1878621f407SFrançois Tigeot 	int err = 0, ret = 0;
18824edb884SFrançois Tigeot 
18924edb884SFrançois Tigeot 	memset(&msg, 0, sizeof(msg));
19024edb884SFrançois Tigeot 	msg.address = offset;
19124edb884SFrançois Tigeot 	msg.request = request;
19224edb884SFrançois Tigeot 	msg.buffer = buffer;
19324edb884SFrançois Tigeot 	msg.size = size;
19424edb884SFrançois Tigeot 
195c0e85e96SFrançois Tigeot 	mutex_lock(&aux->hw_mutex);
196c0e85e96SFrançois Tigeot 
19724edb884SFrançois Tigeot 	/*
19824edb884SFrançois Tigeot 	 * The specification doesn't give any recommendation on how often to
1992c9916cdSFrançois Tigeot 	 * retry native transactions. We used to retry 7 times like for
2002c9916cdSFrançois Tigeot 	 * aux i2c transactions but real world devices this wasn't
2012c9916cdSFrançois Tigeot 	 * sufficient, bump to 32 which makes Dell 4k monitors happier.
20224edb884SFrançois Tigeot 	 */
2032c9916cdSFrançois Tigeot 	for (retry = 0; retry < 32; retry++) {
2048621f407SFrançois Tigeot 		if (ret != 0 && ret != -ETIMEDOUT) {
2058621f407SFrançois Tigeot 			usleep_range(AUX_RETRY_INTERVAL,
2068621f407SFrançois Tigeot 				     AUX_RETRY_INTERVAL + 100);
20724edb884SFrançois Tigeot 		}
20824edb884SFrançois Tigeot 
2098621f407SFrançois Tigeot 		ret = aux->transfer(aux, &msg);
21024edb884SFrançois Tigeot 
2118b983a81SFrançois Tigeot 		if (ret >= 0) {
2128621f407SFrançois Tigeot 			native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
2138621f407SFrançois Tigeot 			if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
2148621f407SFrançois Tigeot 				if (ret == size)
215c0e85e96SFrançois Tigeot 					goto unlock;
21624edb884SFrançois Tigeot 
2178621f407SFrançois Tigeot 				ret = -EPROTO;
2188621f407SFrançois Tigeot 			} else
2198621f407SFrançois Tigeot 				ret = -EIO;
22024edb884SFrançois Tigeot 		}
2218621f407SFrançois Tigeot 
2228621f407SFrançois Tigeot 		/*
2238621f407SFrançois Tigeot 		 * We want the error we return to be the error we received on
2248621f407SFrançois Tigeot 		 * the first transaction, since we may get a different error the
2258621f407SFrançois Tigeot 		 * next time we retry
2268621f407SFrançois Tigeot 		 */
2278621f407SFrançois Tigeot 		if (!err)
2288621f407SFrançois Tigeot 			err = ret;
22924edb884SFrançois Tigeot 	}
23024edb884SFrançois Tigeot 
2311dedbd3bSFrançois Tigeot 	DRM_DEBUG_KMS("Too many retries, giving up. First error: %d\n", err);
2328621f407SFrançois Tigeot 	ret = err;
233c0e85e96SFrançois Tigeot 
234c0e85e96SFrançois Tigeot unlock:
235c0e85e96SFrançois Tigeot 	mutex_unlock(&aux->hw_mutex);
2368621f407SFrançois Tigeot 	return ret;
23724edb884SFrançois Tigeot }
23824edb884SFrançois Tigeot 
23924edb884SFrançois Tigeot /**
24024edb884SFrançois Tigeot  * drm_dp_dpcd_read() - read a series of bytes from the DPCD
24124edb884SFrançois Tigeot  * @aux: DisplayPort AUX channel
24224edb884SFrançois Tigeot  * @offset: address of the (first) register to read
24324edb884SFrançois Tigeot  * @buffer: buffer to store the register values
24424edb884SFrançois Tigeot  * @size: number of bytes in @buffer
24524edb884SFrançois Tigeot  *
24624edb884SFrançois Tigeot  * Returns the number of bytes transferred on success, or a negative error
24724edb884SFrançois Tigeot  * code on failure. -EIO is returned if the request was NAKed by the sink or
24824edb884SFrançois Tigeot  * if the retry count was exceeded. If not all bytes were transferred, this
24924edb884SFrançois Tigeot  * function returns -EPROTO. Errors from the underlying AUX channel transfer
25024edb884SFrançois Tigeot  * function, with the exception of -EBUSY (which causes the transaction to
25124edb884SFrançois Tigeot  * be retried), are propagated to the caller.
25224edb884SFrançois Tigeot  */
drm_dp_dpcd_read(struct drm_dp_aux * aux,unsigned int offset,void * buffer,size_t size)25324edb884SFrançois Tigeot ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
25424edb884SFrançois Tigeot 			 void *buffer, size_t size)
25524edb884SFrançois Tigeot {
2568621f407SFrançois Tigeot 	int ret;
2578621f407SFrançois Tigeot 
2588621f407SFrançois Tigeot 	/*
2598621f407SFrançois Tigeot 	 * HP ZR24w corrupts the first DPCD access after entering power save
2608621f407SFrançois Tigeot 	 * mode. Eg. on a read, the entire buffer will be filled with the same
2618621f407SFrançois Tigeot 	 * byte. Do a throw away read to avoid corrupting anything we care
2628621f407SFrançois Tigeot 	 * about. Afterwards things will work correctly until the monitor
2638621f407SFrançois Tigeot 	 * gets woken up and subsequently re-enters power save mode.
2648621f407SFrançois Tigeot 	 *
2658621f407SFrançois Tigeot 	 * The user pressing any button on the monitor is enough to wake it
2668621f407SFrançois Tigeot 	 * up, so there is no particularly good place to do the workaround.
2678621f407SFrançois Tigeot 	 * We just have to do it before any DPCD access and hope that the
2688621f407SFrançois Tigeot 	 * monitor doesn't power down exactly after the throw away read.
2698621f407SFrançois Tigeot 	 */
2708621f407SFrançois Tigeot 	ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV, buffer,
2718621f407SFrançois Tigeot 				 1);
2728621f407SFrançois Tigeot 	if (ret != 1)
2738621f407SFrançois Tigeot 		return ret;
2748621f407SFrançois Tigeot 
27524edb884SFrançois Tigeot 	return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, buffer,
27624edb884SFrançois Tigeot 				  size);
27724edb884SFrançois Tigeot }
27824edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dp_dpcd_read);
27924edb884SFrançois Tigeot 
28024edb884SFrançois Tigeot /**
28124edb884SFrançois Tigeot  * drm_dp_dpcd_write() - write a series of bytes to the DPCD
28224edb884SFrançois Tigeot  * @aux: DisplayPort AUX channel
28324edb884SFrançois Tigeot  * @offset: address of the (first) register to write
28424edb884SFrançois Tigeot  * @buffer: buffer containing the values to write
28524edb884SFrançois Tigeot  * @size: number of bytes in @buffer
28624edb884SFrançois Tigeot  *
28724edb884SFrançois Tigeot  * Returns the number of bytes transferred on success, or a negative error
28824edb884SFrançois Tigeot  * code on failure. -EIO is returned if the request was NAKed by the sink or
28924edb884SFrançois Tigeot  * if the retry count was exceeded. If not all bytes were transferred, this
29024edb884SFrançois Tigeot  * function returns -EPROTO. Errors from the underlying AUX channel transfer
29124edb884SFrançois Tigeot  * function, with the exception of -EBUSY (which causes the transaction to
29224edb884SFrançois Tigeot  * be retried), are propagated to the caller.
29324edb884SFrançois Tigeot  */
drm_dp_dpcd_write(struct drm_dp_aux * aux,unsigned int offset,void * buffer,size_t size)29424edb884SFrançois Tigeot ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
29524edb884SFrançois Tigeot 			  void *buffer, size_t size)
29624edb884SFrançois Tigeot {
29724edb884SFrançois Tigeot 	return drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer,
29824edb884SFrançois Tigeot 				  size);
29924edb884SFrançois Tigeot }
30024edb884SFrançois Tigeot EXPORT_SYMBOL(drm_dp_dpcd_write);
30124edb884SFrançois Tigeot 
3021b13d190SFrançois Tigeot /**
303477eb7f9SFrançois Tigeot  * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
304477eb7f9SFrançois Tigeot  * @aux: DisplayPort AUX channel
305477eb7f9SFrançois Tigeot  * @status: buffer to store the link status in (must be at least 6 bytes)
306477eb7f9SFrançois Tigeot  *
307477eb7f9SFrançois Tigeot  * Returns the number of bytes transferred on success or a negative error
308477eb7f9SFrançois Tigeot  * code on failure.
309477eb7f9SFrançois Tigeot  */
drm_dp_dpcd_read_link_status(struct drm_dp_aux * aux,u8 status[DP_LINK_STATUS_SIZE])310477eb7f9SFrançois Tigeot int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
311477eb7f9SFrançois Tigeot 				 u8 status[DP_LINK_STATUS_SIZE])
312477eb7f9SFrançois Tigeot {
313477eb7f9SFrançois Tigeot 	return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
314477eb7f9SFrançois Tigeot 				DP_LINK_STATUS_SIZE);
315477eb7f9SFrançois Tigeot }
316477eb7f9SFrançois Tigeot EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
317477eb7f9SFrançois Tigeot 
318477eb7f9SFrançois Tigeot /**
319477eb7f9SFrançois Tigeot  * drm_dp_link_probe() - probe a DisplayPort link for capabilities
320477eb7f9SFrançois Tigeot  * @aux: DisplayPort AUX channel
321477eb7f9SFrançois Tigeot  * @link: pointer to structure in which to return link capabilities
322477eb7f9SFrançois Tigeot  *
323477eb7f9SFrançois Tigeot  * The structure filled in by this function can usually be passed directly
324477eb7f9SFrançois Tigeot  * into drm_dp_link_power_up() and drm_dp_link_configure() to power up and
325477eb7f9SFrançois Tigeot  * configure the link based on the link's capabilities.
326477eb7f9SFrançois Tigeot  *
327477eb7f9SFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
328477eb7f9SFrançois Tigeot  */
drm_dp_link_probe(struct drm_dp_aux * aux,struct drm_dp_link * link)329477eb7f9SFrançois Tigeot int drm_dp_link_probe(struct drm_dp_aux *aux, struct drm_dp_link *link)
330477eb7f9SFrançois Tigeot {
331477eb7f9SFrançois Tigeot 	u8 values[3];
332477eb7f9SFrançois Tigeot 	int err;
333477eb7f9SFrançois Tigeot 
334477eb7f9SFrançois Tigeot 	memset(link, 0, sizeof(*link));
335477eb7f9SFrançois Tigeot 
336477eb7f9SFrançois Tigeot 	err = drm_dp_dpcd_read(aux, DP_DPCD_REV, values, sizeof(values));
337477eb7f9SFrançois Tigeot 	if (err < 0)
338477eb7f9SFrançois Tigeot 		return err;
339477eb7f9SFrançois Tigeot 
340477eb7f9SFrançois Tigeot 	link->revision = values[0];
341477eb7f9SFrançois Tigeot 	link->rate = drm_dp_bw_code_to_link_rate(values[1]);
342477eb7f9SFrançois Tigeot 	link->num_lanes = values[2] & DP_MAX_LANE_COUNT_MASK;
343477eb7f9SFrançois Tigeot 
344477eb7f9SFrançois Tigeot 	if (values[2] & DP_ENHANCED_FRAME_CAP)
345477eb7f9SFrançois Tigeot 		link->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
346477eb7f9SFrançois Tigeot 
347477eb7f9SFrançois Tigeot 	return 0;
348477eb7f9SFrançois Tigeot }
349477eb7f9SFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_probe);
350477eb7f9SFrançois Tigeot 
351477eb7f9SFrançois Tigeot /**
352477eb7f9SFrançois Tigeot  * drm_dp_link_power_up() - power up a DisplayPort link
353477eb7f9SFrançois Tigeot  * @aux: DisplayPort AUX channel
354477eb7f9SFrançois Tigeot  * @link: pointer to a structure containing the link configuration
355477eb7f9SFrançois Tigeot  *
356477eb7f9SFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
357477eb7f9SFrançois Tigeot  */
drm_dp_link_power_up(struct drm_dp_aux * aux,struct drm_dp_link * link)358477eb7f9SFrançois Tigeot int drm_dp_link_power_up(struct drm_dp_aux *aux, struct drm_dp_link *link)
359477eb7f9SFrançois Tigeot {
360477eb7f9SFrançois Tigeot 	u8 value;
361477eb7f9SFrançois Tigeot 	int err;
362477eb7f9SFrançois Tigeot 
363477eb7f9SFrançois Tigeot 	/* DP_SET_POWER register is only available on DPCD v1.1 and later */
364477eb7f9SFrançois Tigeot 	if (link->revision < 0x11)
365477eb7f9SFrançois Tigeot 		return 0;
366477eb7f9SFrançois Tigeot 
367477eb7f9SFrançois Tigeot 	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
368477eb7f9SFrançois Tigeot 	if (err < 0)
369477eb7f9SFrançois Tigeot 		return err;
370477eb7f9SFrançois Tigeot 
371477eb7f9SFrançois Tigeot 	value &= ~DP_SET_POWER_MASK;
372477eb7f9SFrançois Tigeot 	value |= DP_SET_POWER_D0;
373477eb7f9SFrançois Tigeot 
374477eb7f9SFrançois Tigeot 	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
375477eb7f9SFrançois Tigeot 	if (err < 0)
376477eb7f9SFrançois Tigeot 		return err;
377477eb7f9SFrançois Tigeot 
378477eb7f9SFrançois Tigeot 	/*
379477eb7f9SFrançois Tigeot 	 * According to the DP 1.1 specification, a "Sink Device must exit the
380477eb7f9SFrançois Tigeot 	 * power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink
381477eb7f9SFrançois Tigeot 	 * Control Field" (register 0x600).
382477eb7f9SFrançois Tigeot 	 */
383477eb7f9SFrançois Tigeot 	usleep_range(1000, 2000);
384477eb7f9SFrançois Tigeot 
385477eb7f9SFrançois Tigeot 	return 0;
386477eb7f9SFrançois Tigeot }
387477eb7f9SFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_power_up);
388477eb7f9SFrançois Tigeot 
389477eb7f9SFrançois Tigeot /**
390477eb7f9SFrançois Tigeot  * drm_dp_link_power_down() - power down a DisplayPort link
391477eb7f9SFrançois Tigeot  * @aux: DisplayPort AUX channel
392477eb7f9SFrançois Tigeot  * @link: pointer to a structure containing the link configuration
393477eb7f9SFrançois Tigeot  *
394477eb7f9SFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
395477eb7f9SFrançois Tigeot  */
drm_dp_link_power_down(struct drm_dp_aux * aux,struct drm_dp_link * link)396477eb7f9SFrançois Tigeot int drm_dp_link_power_down(struct drm_dp_aux *aux, struct drm_dp_link *link)
397477eb7f9SFrançois Tigeot {
398477eb7f9SFrançois Tigeot 	u8 value;
399477eb7f9SFrançois Tigeot 	int err;
400477eb7f9SFrançois Tigeot 
401477eb7f9SFrançois Tigeot 	/* DP_SET_POWER register is only available on DPCD v1.1 and later */
402477eb7f9SFrançois Tigeot 	if (link->revision < 0x11)
403477eb7f9SFrançois Tigeot 		return 0;
404477eb7f9SFrançois Tigeot 
405477eb7f9SFrançois Tigeot 	err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);
406477eb7f9SFrançois Tigeot 	if (err < 0)
407477eb7f9SFrançois Tigeot 		return err;
408477eb7f9SFrançois Tigeot 
409477eb7f9SFrançois Tigeot 	value &= ~DP_SET_POWER_MASK;
410477eb7f9SFrançois Tigeot 	value |= DP_SET_POWER_D3;
411477eb7f9SFrançois Tigeot 
412477eb7f9SFrançois Tigeot 	err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);
413477eb7f9SFrançois Tigeot 	if (err < 0)
414477eb7f9SFrançois Tigeot 		return err;
415477eb7f9SFrançois Tigeot 
416477eb7f9SFrançois Tigeot 	return 0;
417477eb7f9SFrançois Tigeot }
418477eb7f9SFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_power_down);
419477eb7f9SFrançois Tigeot 
420477eb7f9SFrançois Tigeot /**
421477eb7f9SFrançois Tigeot  * drm_dp_link_configure() - configure a DisplayPort link
422477eb7f9SFrançois Tigeot  * @aux: DisplayPort AUX channel
423477eb7f9SFrançois Tigeot  * @link: pointer to a structure containing the link configuration
424477eb7f9SFrançois Tigeot  *
425477eb7f9SFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
426477eb7f9SFrançois Tigeot  */
drm_dp_link_configure(struct drm_dp_aux * aux,struct drm_dp_link * link)427477eb7f9SFrançois Tigeot int drm_dp_link_configure(struct drm_dp_aux *aux, struct drm_dp_link *link)
428477eb7f9SFrançois Tigeot {
429477eb7f9SFrançois Tigeot 	u8 values[2];
430477eb7f9SFrançois Tigeot 	int err;
431477eb7f9SFrançois Tigeot 
432477eb7f9SFrançois Tigeot 	values[0] = drm_dp_link_rate_to_bw_code(link->rate);
433477eb7f9SFrançois Tigeot 	values[1] = link->num_lanes;
434477eb7f9SFrançois Tigeot 
435477eb7f9SFrançois Tigeot 	if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
436477eb7f9SFrançois Tigeot 		values[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
437477eb7f9SFrançois Tigeot 
438477eb7f9SFrançois Tigeot 	err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, values, sizeof(values));
439477eb7f9SFrançois Tigeot 	if (err < 0)
440477eb7f9SFrançois Tigeot 		return err;
441477eb7f9SFrançois Tigeot 
442477eb7f9SFrançois Tigeot 	return 0;
443477eb7f9SFrançois Tigeot }
444477eb7f9SFrançois Tigeot EXPORT_SYMBOL(drm_dp_link_configure);
445477eb7f9SFrançois Tigeot 
4461dedbd3bSFrançois Tigeot /**
4471dedbd3bSFrançois Tigeot  * drm_dp_downstream_max_clock() - extract branch device max
4481dedbd3bSFrançois Tigeot  *                                 pixel rate for legacy VGA
4491dedbd3bSFrançois Tigeot  *                                 converter or max TMDS clock
4501dedbd3bSFrançois Tigeot  *                                 rate for others
4511dedbd3bSFrançois Tigeot  * @dpcd: DisplayPort configuration data
4521dedbd3bSFrançois Tigeot  * @port_cap: port capabilities
4531dedbd3bSFrançois Tigeot  *
4541dedbd3bSFrançois Tigeot  * Returns max clock in kHz on success or 0 if max clock not defined
4551dedbd3bSFrançois Tigeot  */
drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])4561dedbd3bSFrançois Tigeot int drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
4571dedbd3bSFrançois Tigeot 				const u8 port_cap[4])
4581dedbd3bSFrançois Tigeot {
4591dedbd3bSFrançois Tigeot 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
4601dedbd3bSFrançois Tigeot 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4611dedbd3bSFrançois Tigeot 		DP_DETAILED_CAP_INFO_AVAILABLE;
4621dedbd3bSFrançois Tigeot 
4631dedbd3bSFrançois Tigeot 	if (!detailed_cap_info)
4641dedbd3bSFrançois Tigeot 		return 0;
4651dedbd3bSFrançois Tigeot 
4661dedbd3bSFrançois Tigeot 	switch (type) {
4671dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_VGA:
4681dedbd3bSFrançois Tigeot 		return port_cap[1] * 8 * 1000;
4691dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DVI:
4701dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_HDMI:
4711dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DP_DUALMODE:
4721dedbd3bSFrançois Tigeot 		return port_cap[1] * 2500;
4731dedbd3bSFrançois Tigeot 	default:
4741dedbd3bSFrançois Tigeot 		return 0;
4751dedbd3bSFrançois Tigeot 	}
4761dedbd3bSFrançois Tigeot }
4771dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_dp_downstream_max_clock);
4781dedbd3bSFrançois Tigeot 
4791dedbd3bSFrançois Tigeot /**
4801dedbd3bSFrançois Tigeot  * drm_dp_downstream_max_bpc() - extract branch device max
4811dedbd3bSFrançois Tigeot  *                               bits per component
4821dedbd3bSFrançois Tigeot  * @dpcd: DisplayPort configuration data
4831dedbd3bSFrançois Tigeot  * @port_cap: port capabilities
4841dedbd3bSFrançois Tigeot  *
4851dedbd3bSFrançois Tigeot  * Returns max bpc on success or 0 if max bpc not defined
4861dedbd3bSFrançois Tigeot  */
drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4])4871dedbd3bSFrançois Tigeot int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
4881dedbd3bSFrançois Tigeot 			      const u8 port_cap[4])
4891dedbd3bSFrançois Tigeot {
4901dedbd3bSFrançois Tigeot 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
4911dedbd3bSFrançois Tigeot 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
4921dedbd3bSFrançois Tigeot 		DP_DETAILED_CAP_INFO_AVAILABLE;
4931dedbd3bSFrançois Tigeot 	int bpc;
4941dedbd3bSFrançois Tigeot 
4951dedbd3bSFrançois Tigeot 	if (!detailed_cap_info)
4961dedbd3bSFrançois Tigeot 		return 0;
4971dedbd3bSFrançois Tigeot 
4981dedbd3bSFrançois Tigeot 	switch (type) {
4991dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_VGA:
5001dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DVI:
5011dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_HDMI:
5021dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DP_DUALMODE:
5031dedbd3bSFrançois Tigeot 		bpc = port_cap[2] & DP_DS_MAX_BPC_MASK;
5041dedbd3bSFrançois Tigeot 
5051dedbd3bSFrançois Tigeot 		switch (bpc) {
5061dedbd3bSFrançois Tigeot 		case DP_DS_8BPC:
5071dedbd3bSFrançois Tigeot 			return 8;
5081dedbd3bSFrançois Tigeot 		case DP_DS_10BPC:
5091dedbd3bSFrançois Tigeot 			return 10;
5101dedbd3bSFrançois Tigeot 		case DP_DS_12BPC:
5111dedbd3bSFrançois Tigeot 			return 12;
5121dedbd3bSFrançois Tigeot 		case DP_DS_16BPC:
5131dedbd3bSFrançois Tigeot 			return 16;
5141dedbd3bSFrançois Tigeot 		}
5151dedbd3bSFrançois Tigeot 	default:
5161dedbd3bSFrançois Tigeot 		return 0;
5171dedbd3bSFrançois Tigeot 	}
5181dedbd3bSFrançois Tigeot }
5191dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
5201dedbd3bSFrançois Tigeot 
5211dedbd3bSFrançois Tigeot /**
5221dedbd3bSFrançois Tigeot  * drm_dp_downstream_id() - identify branch device
5231dedbd3bSFrançois Tigeot  * @aux: DisplayPort AUX channel
5241dedbd3bSFrançois Tigeot  * @id: DisplayPort branch device id
5251dedbd3bSFrançois Tigeot  *
5261dedbd3bSFrançois Tigeot  * Returns branch device id on success or NULL on failure
5271dedbd3bSFrançois Tigeot  */
drm_dp_downstream_id(struct drm_dp_aux * aux,char id[6])5281dedbd3bSFrançois Tigeot int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
5291dedbd3bSFrançois Tigeot {
5301dedbd3bSFrançois Tigeot 	return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
5311dedbd3bSFrançois Tigeot }
5321dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_dp_downstream_id);
5331dedbd3bSFrançois Tigeot 
5341dedbd3bSFrançois Tigeot /**
5351dedbd3bSFrançois Tigeot  * drm_dp_downstream_debug() - debug DP branch devices
5361dedbd3bSFrançois Tigeot  * @m: pointer for debugfs file
5371dedbd3bSFrançois Tigeot  * @dpcd: DisplayPort configuration data
5381dedbd3bSFrançois Tigeot  * @port_cap: port capabilities
5391dedbd3bSFrançois Tigeot  * @aux: DisplayPort AUX channel
5401dedbd3bSFrançois Tigeot  *
5411dedbd3bSFrançois Tigeot  */
drm_dp_downstream_debug(struct seq_file * m,const u8 dpcd[DP_RECEIVER_CAP_SIZE],const u8 port_cap[4],struct drm_dp_aux * aux)5421dedbd3bSFrançois Tigeot void drm_dp_downstream_debug(struct seq_file *m,
5431dedbd3bSFrançois Tigeot 			     const u8 dpcd[DP_RECEIVER_CAP_SIZE],
5441dedbd3bSFrançois Tigeot 			     const u8 port_cap[4], struct drm_dp_aux *aux)
5451dedbd3bSFrançois Tigeot {
5461dedbd3bSFrançois Tigeot 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
5471dedbd3bSFrançois Tigeot 				 DP_DETAILED_CAP_INFO_AVAILABLE;
5481dedbd3bSFrançois Tigeot 	int clk;
5491dedbd3bSFrançois Tigeot 	int bpc;
550*3f2dd94aSFrançois Tigeot 	char id[7];
5511dedbd3bSFrançois Tigeot 	int len;
5521dedbd3bSFrançois Tigeot 	uint8_t rev[2];
5531dedbd3bSFrançois Tigeot 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
5541dedbd3bSFrançois Tigeot 	bool branch_device = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
5551dedbd3bSFrançois Tigeot 			     DP_DWN_STRM_PORT_PRESENT;
5561dedbd3bSFrançois Tigeot 
5571dedbd3bSFrançois Tigeot 	seq_printf(m, "\tDP branch device present: %s\n",
5581dedbd3bSFrançois Tigeot 		   branch_device ? "yes" : "no");
5591dedbd3bSFrançois Tigeot 
5601dedbd3bSFrançois Tigeot 	if (!branch_device)
5611dedbd3bSFrançois Tigeot 		return;
5621dedbd3bSFrançois Tigeot 
5631dedbd3bSFrançois Tigeot 	switch (type) {
5641dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DP:
5651dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: DisplayPort\n");
5661dedbd3bSFrançois Tigeot 		break;
5671dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_VGA:
5681dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: VGA\n");
5691dedbd3bSFrançois Tigeot 		break;
5701dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DVI:
5711dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: DVI\n");
5721dedbd3bSFrançois Tigeot 		break;
5731dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_HDMI:
5741dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: HDMI\n");
5751dedbd3bSFrançois Tigeot 		break;
5761dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_NON_EDID:
5771dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: others without EDID support\n");
5781dedbd3bSFrançois Tigeot 		break;
5791dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_DP_DUALMODE:
5801dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: DP++\n");
5811dedbd3bSFrançois Tigeot 		break;
5821dedbd3bSFrançois Tigeot 	case DP_DS_PORT_TYPE_WIRELESS:
5831dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: Wireless\n");
5841dedbd3bSFrançois Tigeot 		break;
5851dedbd3bSFrançois Tigeot 	default:
5861dedbd3bSFrançois Tigeot 		seq_puts(m, "\t\tType: N/A\n");
5871dedbd3bSFrançois Tigeot 	}
5881dedbd3bSFrançois Tigeot 
589*3f2dd94aSFrançois Tigeot 	memset(id, 0, sizeof(id));
5901dedbd3bSFrançois Tigeot 	drm_dp_downstream_id(aux, id);
5911dedbd3bSFrançois Tigeot 	seq_printf(m, "\t\tID: %s\n", id);
5921dedbd3bSFrançois Tigeot 
5931dedbd3bSFrançois Tigeot 	len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
5941dedbd3bSFrançois Tigeot 	if (len > 0)
5951dedbd3bSFrançois Tigeot 		seq_printf(m, "\t\tHW: %d.%d\n",
5961dedbd3bSFrançois Tigeot 			   (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
5971dedbd3bSFrançois Tigeot 
598*3f2dd94aSFrançois Tigeot 	len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
5991dedbd3bSFrançois Tigeot 	if (len > 0)
6001dedbd3bSFrançois Tigeot 		seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
6011dedbd3bSFrançois Tigeot 
6021dedbd3bSFrançois Tigeot 	if (detailed_cap_info) {
6031dedbd3bSFrançois Tigeot 		clk = drm_dp_downstream_max_clock(dpcd, port_cap);
6041dedbd3bSFrançois Tigeot 
6051dedbd3bSFrançois Tigeot 		if (clk > 0) {
6061dedbd3bSFrançois Tigeot 			if (type == DP_DS_PORT_TYPE_VGA)
6071dedbd3bSFrançois Tigeot 				seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
6081dedbd3bSFrançois Tigeot 			else
6091dedbd3bSFrançois Tigeot 				seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
6101dedbd3bSFrançois Tigeot 		}
6111dedbd3bSFrançois Tigeot 
6121dedbd3bSFrançois Tigeot 		bpc = drm_dp_downstream_max_bpc(dpcd, port_cap);
6131dedbd3bSFrançois Tigeot 
6141dedbd3bSFrançois Tigeot 		if (bpc > 0)
6151dedbd3bSFrançois Tigeot 			seq_printf(m, "\t\tMax bpc: %d\n", bpc);
6161dedbd3bSFrançois Tigeot 	}
6171dedbd3bSFrançois Tigeot }
6181dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_dp_downstream_debug);
6191dedbd3bSFrançois Tigeot 
620477eb7f9SFrançois Tigeot /*
621477eb7f9SFrançois Tigeot  * I2C-over-AUX implementation
622477eb7f9SFrançois Tigeot  */
623477eb7f9SFrançois Tigeot 
drm_dp_i2c_functionality(struct i2c_adapter * adapter)624477eb7f9SFrançois Tigeot static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
625477eb7f9SFrançois Tigeot {
626477eb7f9SFrançois Tigeot 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
627477eb7f9SFrançois Tigeot 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
628477eb7f9SFrançois Tigeot 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
629477eb7f9SFrançois Tigeot 	       I2C_FUNC_10BIT_ADDR;
630477eb7f9SFrançois Tigeot }
631477eb7f9SFrançois Tigeot 
drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg * msg)632352ff8bdSFrançois Tigeot static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
633a05eeebfSFrançois Tigeot {
634a05eeebfSFrançois Tigeot 	/*
635352ff8bdSFrançois Tigeot 	 * In case of i2c defer or short i2c ack reply to a write,
636352ff8bdSFrançois Tigeot 	 * we need to switch to WRITE_STATUS_UPDATE to drain the
637352ff8bdSFrançois Tigeot 	 * rest of the message
638a05eeebfSFrançois Tigeot 	 */
639352ff8bdSFrançois Tigeot 	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
640352ff8bdSFrançois Tigeot 		msg->request &= DP_AUX_I2C_MOT;
641352ff8bdSFrançois Tigeot 		msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
642a05eeebfSFrançois Tigeot 	}
643a05eeebfSFrançois Tigeot }
644a05eeebfSFrançois Tigeot 
6459f4ca867SFrançois Tigeot #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
6469f4ca867SFrançois Tigeot #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
6479f4ca867SFrançois Tigeot #define AUX_STOP_LEN 4
6489f4ca867SFrançois Tigeot #define AUX_CMD_LEN 4
6499f4ca867SFrançois Tigeot #define AUX_ADDRESS_LEN 20
6509f4ca867SFrançois Tigeot #define AUX_REPLY_PAD_LEN 4
6519f4ca867SFrançois Tigeot #define AUX_LENGTH_LEN 8
6529f4ca867SFrançois Tigeot 
6539f4ca867SFrançois Tigeot /*
6549f4ca867SFrançois Tigeot  * Calculate the duration of the AUX request/reply in usec. Gives the
6559f4ca867SFrançois Tigeot  * "best" case estimate, ie. successful while as short as possible.
6569f4ca867SFrançois Tigeot  */
drm_dp_aux_req_duration(const struct drm_dp_aux_msg * msg)6579f4ca867SFrançois Tigeot static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
6589f4ca867SFrançois Tigeot {
6599f4ca867SFrançois Tigeot 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
6609f4ca867SFrançois Tigeot 		AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
6619f4ca867SFrançois Tigeot 
6629f4ca867SFrançois Tigeot 	if ((msg->request & DP_AUX_I2C_READ) == 0)
6639f4ca867SFrançois Tigeot 		len += msg->size * 8;
6649f4ca867SFrançois Tigeot 
6659f4ca867SFrançois Tigeot 	return len;
6669f4ca867SFrançois Tigeot }
6679f4ca867SFrançois Tigeot 
drm_dp_aux_reply_duration(const struct drm_dp_aux_msg * msg)6689f4ca867SFrançois Tigeot static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
6699f4ca867SFrançois Tigeot {
6709f4ca867SFrançois Tigeot 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
6719f4ca867SFrançois Tigeot 		AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
6729f4ca867SFrançois Tigeot 
6739f4ca867SFrançois Tigeot 	/*
6749f4ca867SFrançois Tigeot 	 * For read we expect what was asked. For writes there will
6759f4ca867SFrançois Tigeot 	 * be 0 or 1 data bytes. Assume 0 for the "best" case.
6769f4ca867SFrançois Tigeot 	 */
6779f4ca867SFrançois Tigeot 	if (msg->request & DP_AUX_I2C_READ)
6789f4ca867SFrançois Tigeot 		len += msg->size * 8;
6799f4ca867SFrançois Tigeot 
6809f4ca867SFrançois Tigeot 	return len;
6819f4ca867SFrançois Tigeot }
6829f4ca867SFrançois Tigeot 
6839f4ca867SFrançois Tigeot #define I2C_START_LEN 1
6849f4ca867SFrançois Tigeot #define I2C_STOP_LEN 1
6859f4ca867SFrançois Tigeot #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
6869f4ca867SFrançois Tigeot #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
6879f4ca867SFrançois Tigeot 
6889f4ca867SFrançois Tigeot /*
6899f4ca867SFrançois Tigeot  * Calculate the length of the i2c transfer in usec, assuming
6909f4ca867SFrançois Tigeot  * the i2c bus speed is as specified. Gives the the "worst"
6919f4ca867SFrançois Tigeot  * case estimate, ie. successful while as long as possible.
6929f4ca867SFrançois Tigeot  * Doesn't account the the "MOT" bit, and instead assumes each
6939f4ca867SFrançois Tigeot  * message includes a START, ADDRESS and STOP. Neither does it
6949f4ca867SFrançois Tigeot  * account for additional random variables such as clock stretching.
6959f4ca867SFrançois Tigeot  */
drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg * msg,int i2c_speed_khz)6969f4ca867SFrançois Tigeot static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
6979f4ca867SFrançois Tigeot 				   int i2c_speed_khz)
6989f4ca867SFrançois Tigeot {
6999f4ca867SFrançois Tigeot 	/* AUX bitrate is 1MHz, i2c bitrate as specified */
7009f4ca867SFrançois Tigeot 	return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
7019f4ca867SFrançois Tigeot 			     msg->size * I2C_DATA_LEN +
7029f4ca867SFrançois Tigeot 			     I2C_STOP_LEN) * 1000, i2c_speed_khz);
7039f4ca867SFrançois Tigeot }
7049f4ca867SFrançois Tigeot 
7059f4ca867SFrançois Tigeot /*
7069f4ca867SFrançois Tigeot  * Deterine how many retries should be attempted to successfully transfer
7079f4ca867SFrançois Tigeot  * the specified message, based on the estimated durations of the
7089f4ca867SFrançois Tigeot  * i2c and AUX transfers.
7099f4ca867SFrançois Tigeot  */
drm_dp_i2c_retry_count(const struct drm_dp_aux_msg * msg,int i2c_speed_khz)7109f4ca867SFrançois Tigeot static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
7119f4ca867SFrançois Tigeot 			      int i2c_speed_khz)
7129f4ca867SFrançois Tigeot {
7139f4ca867SFrançois Tigeot 	int aux_time_us = drm_dp_aux_req_duration(msg) +
7149f4ca867SFrançois Tigeot 		drm_dp_aux_reply_duration(msg);
7159f4ca867SFrançois Tigeot 	int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
7169f4ca867SFrançois Tigeot 
7179f4ca867SFrançois Tigeot 	return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
7189f4ca867SFrançois Tigeot }
7199f4ca867SFrançois Tigeot 
7209f4ca867SFrançois Tigeot /*
7219f4ca867SFrançois Tigeot  * FIXME currently assumes 10 kHz as some real world devices seem
7229f4ca867SFrançois Tigeot  * to require it. We should query/set the speed via DPCD if supported.
7239f4ca867SFrançois Tigeot  */
7249f4ca867SFrançois Tigeot static int dp_aux_i2c_speed_khz __read_mostly = 10;
725a85cb24fSFrançois Tigeot module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
7269f4ca867SFrançois Tigeot MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
7279f4ca867SFrançois Tigeot 		 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
7289f4ca867SFrançois Tigeot 
729a05eeebfSFrançois Tigeot /*
730477eb7f9SFrançois Tigeot  * Transfer a single I2C-over-AUX message and handle various error conditions,
731477eb7f9SFrançois Tigeot  * retrying the transaction as appropriate.  It is assumed that the
732a85cb24fSFrançois Tigeot  * &drm_dp_aux.transfer function does not modify anything in the msg other than the
733477eb7f9SFrançois Tigeot  * reply field.
734477eb7f9SFrançois Tigeot  *
735477eb7f9SFrançois Tigeot  * Returns bytes transferred on success, or a negative error code on failure.
736477eb7f9SFrançois Tigeot  */
drm_dp_i2c_do_msg(struct drm_dp_aux * aux,struct drm_dp_aux_msg * msg)737477eb7f9SFrançois Tigeot static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
738477eb7f9SFrançois Tigeot {
73919c468b4SFrançois Tigeot 	unsigned int retry, defer_i2c;
740477eb7f9SFrançois Tigeot 	int ret;
741477eb7f9SFrançois Tigeot 	/*
742477eb7f9SFrançois Tigeot 	 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
743477eb7f9SFrançois Tigeot 	 * is required to retry at least seven times upon receiving AUX_DEFER
744477eb7f9SFrançois Tigeot 	 * before giving up the AUX transaction.
7459f4ca867SFrançois Tigeot 	 *
7469f4ca867SFrançois Tigeot 	 * We also try to account for the i2c bus speed.
747477eb7f9SFrançois Tigeot 	 */
7489f4ca867SFrançois Tigeot 	int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
7499f4ca867SFrançois Tigeot 
7509f4ca867SFrançois Tigeot 	for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
751477eb7f9SFrançois Tigeot 		ret = aux->transfer(aux, msg);
752477eb7f9SFrançois Tigeot 		if (ret < 0) {
753477eb7f9SFrançois Tigeot 			if (ret == -EBUSY)
754477eb7f9SFrançois Tigeot 				continue;
755477eb7f9SFrançois Tigeot 
7561dedbd3bSFrançois Tigeot 			/*
7571dedbd3bSFrançois Tigeot 			 * While timeouts can be errors, they're usually normal
7581dedbd3bSFrançois Tigeot 			 * behavior (for instance, when a driver tries to
7591dedbd3bSFrançois Tigeot 			 * communicate with a non-existant DisplayPort device).
7601dedbd3bSFrançois Tigeot 			 * Avoid spamming the kernel log with timeout errors.
7611dedbd3bSFrançois Tigeot 			 */
7621dedbd3bSFrançois Tigeot 			if (ret == -ETIMEDOUT)
7631dedbd3bSFrançois Tigeot 				DRM_DEBUG_KMS_RATELIMITED("transaction timed out\n");
7641dedbd3bSFrançois Tigeot 			else
765477eb7f9SFrançois Tigeot 				DRM_DEBUG_KMS("transaction failed: %d\n", ret);
7661dedbd3bSFrançois Tigeot 
767477eb7f9SFrançois Tigeot 			return ret;
768477eb7f9SFrançois Tigeot 		}
769477eb7f9SFrançois Tigeot 
770477eb7f9SFrançois Tigeot 
771477eb7f9SFrançois Tigeot 		switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
772477eb7f9SFrançois Tigeot 		case DP_AUX_NATIVE_REPLY_ACK:
773477eb7f9SFrançois Tigeot 			/*
774477eb7f9SFrançois Tigeot 			 * For I2C-over-AUX transactions this isn't enough, we
775477eb7f9SFrançois Tigeot 			 * need to check for the I2C ACK reply.
776477eb7f9SFrançois Tigeot 			 */
777477eb7f9SFrançois Tigeot 			break;
778477eb7f9SFrançois Tigeot 
779477eb7f9SFrançois Tigeot 		case DP_AUX_NATIVE_REPLY_NACK:
780477eb7f9SFrançois Tigeot 			DRM_DEBUG_KMS("native nack (result=%d, size=%zu)\n", ret, msg->size);
781477eb7f9SFrançois Tigeot 			return -EREMOTEIO;
782477eb7f9SFrançois Tigeot 
783477eb7f9SFrançois Tigeot 		case DP_AUX_NATIVE_REPLY_DEFER:
78419c468b4SFrançois Tigeot 			DRM_DEBUG_KMS("native defer\n");
785477eb7f9SFrançois Tigeot 			/*
786477eb7f9SFrançois Tigeot 			 * We could check for I2C bit rate capabilities and if
787477eb7f9SFrançois Tigeot 			 * available adjust this interval. We could also be
788477eb7f9SFrançois Tigeot 			 * more careful with DP-to-legacy adapters where a
789477eb7f9SFrançois Tigeot 			 * long legacy cable may force very low I2C bit rates.
790477eb7f9SFrançois Tigeot 			 *
791477eb7f9SFrançois Tigeot 			 * For now just defer for long enough to hopefully be
792477eb7f9SFrançois Tigeot 			 * safe for all use-cases.
793477eb7f9SFrançois Tigeot 			 */
7949f4ca867SFrançois Tigeot 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
795477eb7f9SFrançois Tigeot 			continue;
796477eb7f9SFrançois Tigeot 
797477eb7f9SFrançois Tigeot 		default:
798477eb7f9SFrançois Tigeot 			DRM_ERROR("invalid native reply %#04x\n", msg->reply);
799477eb7f9SFrançois Tigeot 			return -EREMOTEIO;
800477eb7f9SFrançois Tigeot 		}
801477eb7f9SFrançois Tigeot 
802477eb7f9SFrançois Tigeot 		switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
803477eb7f9SFrançois Tigeot 		case DP_AUX_I2C_REPLY_ACK:
804477eb7f9SFrançois Tigeot 			/*
805477eb7f9SFrançois Tigeot 			 * Both native ACK and I2C ACK replies received. We
806477eb7f9SFrançois Tigeot 			 * can assume the transfer was successful.
807477eb7f9SFrançois Tigeot 			 */
808352ff8bdSFrançois Tigeot 			if (ret != msg->size)
809352ff8bdSFrançois Tigeot 				drm_dp_i2c_msg_write_status_update(msg);
810477eb7f9SFrançois Tigeot 			return ret;
811477eb7f9SFrançois Tigeot 
812477eb7f9SFrançois Tigeot 		case DP_AUX_I2C_REPLY_NACK:
813477eb7f9SFrançois Tigeot 			DRM_DEBUG_KMS("I2C nack (result=%d, size=%zu\n", ret, msg->size);
814477eb7f9SFrançois Tigeot 			aux->i2c_nack_count++;
815477eb7f9SFrançois Tigeot 			return -EREMOTEIO;
816477eb7f9SFrançois Tigeot 
817477eb7f9SFrançois Tigeot 		case DP_AUX_I2C_REPLY_DEFER:
818477eb7f9SFrançois Tigeot 			DRM_DEBUG_KMS("I2C defer\n");
81919c468b4SFrançois Tigeot 			/* DP Compliance Test 4.2.2.5 Requirement:
82019c468b4SFrançois Tigeot 			 * Must have at least 7 retries for I2C defers on the
82119c468b4SFrançois Tigeot 			 * transaction to pass this test
82219c468b4SFrançois Tigeot 			 */
823477eb7f9SFrançois Tigeot 			aux->i2c_defer_count++;
82419c468b4SFrançois Tigeot 			if (defer_i2c < 7)
82519c468b4SFrançois Tigeot 				defer_i2c++;
8269f4ca867SFrançois Tigeot 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
827352ff8bdSFrançois Tigeot 			drm_dp_i2c_msg_write_status_update(msg);
8289f4ca867SFrançois Tigeot 
829477eb7f9SFrançois Tigeot 			continue;
830477eb7f9SFrançois Tigeot 
831477eb7f9SFrançois Tigeot 		default:
832477eb7f9SFrançois Tigeot 			DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
833477eb7f9SFrançois Tigeot 			return -EREMOTEIO;
834477eb7f9SFrançois Tigeot 		}
835477eb7f9SFrançois Tigeot 	}
836477eb7f9SFrançois Tigeot 
837477eb7f9SFrançois Tigeot 	DRM_DEBUG_KMS("too many retries, giving up\n");
838477eb7f9SFrançois Tigeot 	return -EREMOTEIO;
839477eb7f9SFrançois Tigeot }
840477eb7f9SFrançois Tigeot 
drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg * msg,const struct i2c_msg * i2c_msg)841352ff8bdSFrançois Tigeot static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
842352ff8bdSFrançois Tigeot 				       const struct i2c_msg *i2c_msg)
843352ff8bdSFrançois Tigeot {
844352ff8bdSFrançois Tigeot 	msg->request = (i2c_msg->flags & I2C_M_RD) ?
845352ff8bdSFrançois Tigeot 		DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
846352ff8bdSFrançois Tigeot 	msg->request |= DP_AUX_I2C_MOT;
847352ff8bdSFrançois Tigeot }
848352ff8bdSFrançois Tigeot 
849477eb7f9SFrançois Tigeot /*
850477eb7f9SFrançois Tigeot  * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
851477eb7f9SFrançois Tigeot  *
852477eb7f9SFrançois Tigeot  * Returns an error code on failure, or a recommended transfer size on success.
853477eb7f9SFrançois Tigeot  */
drm_dp_i2c_drain_msg(struct drm_dp_aux * aux,struct drm_dp_aux_msg * orig_msg)854477eb7f9SFrançois Tigeot static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
855477eb7f9SFrançois Tigeot {
856477eb7f9SFrançois Tigeot 	int err, ret = orig_msg->size;
857477eb7f9SFrançois Tigeot 	struct drm_dp_aux_msg msg = *orig_msg;
858477eb7f9SFrançois Tigeot 
859477eb7f9SFrançois Tigeot 	while (msg.size > 0) {
860477eb7f9SFrançois Tigeot 		err = drm_dp_i2c_do_msg(aux, &msg);
861477eb7f9SFrançois Tigeot 		if (err <= 0)
862477eb7f9SFrançois Tigeot 			return err == 0 ? -EPROTO : err;
863477eb7f9SFrançois Tigeot 
864477eb7f9SFrançois Tigeot 		if (err < msg.size && err < ret) {
865477eb7f9SFrançois Tigeot 			DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
866477eb7f9SFrançois Tigeot 				      msg.size, err);
867477eb7f9SFrançois Tigeot 			ret = err;
868477eb7f9SFrançois Tigeot 		}
869477eb7f9SFrançois Tigeot 
870477eb7f9SFrançois Tigeot 		msg.size -= err;
871477eb7f9SFrançois Tigeot 		msg.buffer += err;
872477eb7f9SFrançois Tigeot 	}
873477eb7f9SFrançois Tigeot 
874477eb7f9SFrançois Tigeot 	return ret;
875477eb7f9SFrançois Tigeot }
876477eb7f9SFrançois Tigeot 
877477eb7f9SFrançois Tigeot /*
878477eb7f9SFrançois Tigeot  * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
879477eb7f9SFrançois Tigeot  * packets to be as large as possible. If not, the I2C transactions never
880477eb7f9SFrançois Tigeot  * succeed. Hence the default is maximum.
881477eb7f9SFrançois Tigeot  */
882477eb7f9SFrançois Tigeot static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
883a85cb24fSFrançois Tigeot module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
884477eb7f9SFrançois Tigeot MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
885477eb7f9SFrançois Tigeot 		 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
886477eb7f9SFrançois Tigeot 
drm_dp_i2c_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)887477eb7f9SFrançois Tigeot static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
888477eb7f9SFrançois Tigeot 			   int num)
889477eb7f9SFrançois Tigeot {
890477eb7f9SFrançois Tigeot 	struct drm_dp_aux *aux = adapter->algo_data;
891477eb7f9SFrançois Tigeot 	unsigned int i, j;
892477eb7f9SFrançois Tigeot 	unsigned transfer_size;
893477eb7f9SFrançois Tigeot 	struct drm_dp_aux_msg msg;
894477eb7f9SFrançois Tigeot 	int err = 0;
895477eb7f9SFrançois Tigeot 
896477eb7f9SFrançois Tigeot 	dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
897477eb7f9SFrançois Tigeot 
898477eb7f9SFrançois Tigeot 	memset(&msg, 0, sizeof(msg));
899477eb7f9SFrançois Tigeot 
900477eb7f9SFrançois Tigeot 	for (i = 0; i < num; i++) {
901477eb7f9SFrançois Tigeot 		msg.address = msgs[i].addr;
902352ff8bdSFrançois Tigeot 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
903477eb7f9SFrançois Tigeot 		/* Send a bare address packet to start the transaction.
904477eb7f9SFrançois Tigeot 		 * Zero sized messages specify an address only (bare
905477eb7f9SFrançois Tigeot 		 * address) transaction.
906477eb7f9SFrançois Tigeot 		 */
907477eb7f9SFrançois Tigeot 		msg.buffer = NULL;
908477eb7f9SFrançois Tigeot 		msg.size = 0;
909477eb7f9SFrançois Tigeot 		err = drm_dp_i2c_do_msg(aux, &msg);
910352ff8bdSFrançois Tigeot 
911352ff8bdSFrançois Tigeot 		/*
912352ff8bdSFrançois Tigeot 		 * Reset msg.request in case in case it got
913352ff8bdSFrançois Tigeot 		 * changed into a WRITE_STATUS_UPDATE.
914352ff8bdSFrançois Tigeot 		 */
915352ff8bdSFrançois Tigeot 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
916352ff8bdSFrançois Tigeot 
917477eb7f9SFrançois Tigeot 		if (err < 0)
918477eb7f9SFrançois Tigeot 			break;
919477eb7f9SFrançois Tigeot 		/* We want each transaction to be as large as possible, but
920477eb7f9SFrançois Tigeot 		 * we'll go to smaller sizes if the hardware gives us a
921477eb7f9SFrançois Tigeot 		 * short reply.
922477eb7f9SFrançois Tigeot 		 */
923477eb7f9SFrançois Tigeot 		transfer_size = dp_aux_i2c_transfer_size;
924477eb7f9SFrançois Tigeot 		for (j = 0; j < msgs[i].len; j += msg.size) {
925477eb7f9SFrançois Tigeot 			msg.buffer = msgs[i].buf + j;
926477eb7f9SFrançois Tigeot 			msg.size = min(transfer_size, msgs[i].len - j);
927477eb7f9SFrançois Tigeot 
928477eb7f9SFrançois Tigeot 			err = drm_dp_i2c_drain_msg(aux, &msg);
929352ff8bdSFrançois Tigeot 
930352ff8bdSFrançois Tigeot 			/*
931352ff8bdSFrançois Tigeot 			 * Reset msg.request in case in case it got
932352ff8bdSFrançois Tigeot 			 * changed into a WRITE_STATUS_UPDATE.
933352ff8bdSFrançois Tigeot 			 */
934352ff8bdSFrançois Tigeot 			drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
935352ff8bdSFrançois Tigeot 
936477eb7f9SFrançois Tigeot 			if (err < 0)
937477eb7f9SFrançois Tigeot 				break;
938477eb7f9SFrançois Tigeot 			transfer_size = err;
939477eb7f9SFrançois Tigeot 		}
940477eb7f9SFrançois Tigeot 		if (err < 0)
941477eb7f9SFrançois Tigeot 			break;
942477eb7f9SFrançois Tigeot 	}
943477eb7f9SFrançois Tigeot 	if (err >= 0)
944477eb7f9SFrançois Tigeot 		err = num;
945477eb7f9SFrançois Tigeot 	/* Send a bare address packet to close out the transaction.
946477eb7f9SFrançois Tigeot 	 * Zero sized messages specify an address only (bare
947477eb7f9SFrançois Tigeot 	 * address) transaction.
948477eb7f9SFrançois Tigeot 	 */
949477eb7f9SFrançois Tigeot 	msg.request &= ~DP_AUX_I2C_MOT;
950477eb7f9SFrançois Tigeot 	msg.buffer = NULL;
951477eb7f9SFrançois Tigeot 	msg.size = 0;
952477eb7f9SFrançois Tigeot 	(void)drm_dp_i2c_do_msg(aux, &msg);
953477eb7f9SFrançois Tigeot 
954477eb7f9SFrançois Tigeot 	return err;
955477eb7f9SFrançois Tigeot }
956477eb7f9SFrançois Tigeot 
957477eb7f9SFrançois Tigeot static const struct i2c_algorithm drm_dp_i2c_algo = {
958477eb7f9SFrançois Tigeot 	.functionality = drm_dp_i2c_functionality,
959477eb7f9SFrançois Tigeot 	.master_xfer = drm_dp_i2c_xfer,
960477eb7f9SFrançois Tigeot };
961477eb7f9SFrançois Tigeot 
i2c_to_aux(struct i2c_adapter * i2c)9621dedbd3bSFrançois Tigeot static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
9631dedbd3bSFrançois Tigeot {
9641dedbd3bSFrançois Tigeot 	return container_of(i2c, struct drm_dp_aux, ddc);
9651dedbd3bSFrançois Tigeot }
9661dedbd3bSFrançois Tigeot 
lock_bus(struct i2c_adapter * i2c,unsigned int flags)9671dedbd3bSFrançois Tigeot static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
9681dedbd3bSFrançois Tigeot {
9691dedbd3bSFrançois Tigeot 	mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
9701dedbd3bSFrançois Tigeot }
9711dedbd3bSFrançois Tigeot 
trylock_bus(struct i2c_adapter * i2c,unsigned int flags)9721dedbd3bSFrançois Tigeot static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
9731dedbd3bSFrançois Tigeot {
9741dedbd3bSFrançois Tigeot 	return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
9751dedbd3bSFrançois Tigeot }
9761dedbd3bSFrançois Tigeot 
unlock_bus(struct i2c_adapter * i2c,unsigned int flags)9771dedbd3bSFrançois Tigeot static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
9781dedbd3bSFrançois Tigeot {
9791dedbd3bSFrançois Tigeot 	mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
9801dedbd3bSFrançois Tigeot }
9811dedbd3bSFrançois Tigeot 
9821dedbd3bSFrançois Tigeot static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
9831dedbd3bSFrançois Tigeot 	.lock_bus = lock_bus,
9841dedbd3bSFrançois Tigeot 	.trylock_bus = trylock_bus,
9851dedbd3bSFrançois Tigeot 	.unlock_bus = unlock_bus,
9861dedbd3bSFrançois Tigeot };
9871dedbd3bSFrançois Tigeot 
drm_dp_aux_get_crc(struct drm_dp_aux * aux,u8 * crc)988a85cb24fSFrançois Tigeot static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
989a85cb24fSFrançois Tigeot {
990a85cb24fSFrançois Tigeot 	u8 buf, count;
991a85cb24fSFrançois Tigeot 	int ret;
992a85cb24fSFrançois Tigeot 
993a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
994a85cb24fSFrançois Tigeot 	if (ret < 0)
995a85cb24fSFrançois Tigeot 		return ret;
996a85cb24fSFrançois Tigeot 
997a85cb24fSFrançois Tigeot 	WARN_ON(!(buf & DP_TEST_SINK_START));
998a85cb24fSFrançois Tigeot 
999a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1000a85cb24fSFrançois Tigeot 	if (ret < 0)
1001a85cb24fSFrançois Tigeot 		return ret;
1002a85cb24fSFrançois Tigeot 
1003a85cb24fSFrançois Tigeot 	count = buf & DP_TEST_COUNT_MASK;
1004a85cb24fSFrançois Tigeot 	if (count == aux->crc_count)
1005a85cb24fSFrançois Tigeot 		return -EAGAIN; /* No CRC yet */
1006a85cb24fSFrançois Tigeot 
1007a85cb24fSFrançois Tigeot 	aux->crc_count = count;
1008a85cb24fSFrançois Tigeot 
1009a85cb24fSFrançois Tigeot 	/*
1010a85cb24fSFrançois Tigeot 	 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1011a85cb24fSFrançois Tigeot 	 * per component (RGB or CrYCb).
1012a85cb24fSFrançois Tigeot 	 */
1013a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1014a85cb24fSFrançois Tigeot 	if (ret < 0)
1015a85cb24fSFrançois Tigeot 		return ret;
1016a85cb24fSFrançois Tigeot 
1017a85cb24fSFrançois Tigeot 	return 0;
1018a85cb24fSFrançois Tigeot }
1019a85cb24fSFrançois Tigeot 
drm_dp_aux_crc_work(struct work_struct * work)1020a85cb24fSFrançois Tigeot static void drm_dp_aux_crc_work(struct work_struct *work)
1021a85cb24fSFrançois Tigeot {
1022a85cb24fSFrançois Tigeot 	struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1023a85cb24fSFrançois Tigeot 					      crc_work);
1024a85cb24fSFrançois Tigeot 	struct drm_crtc *crtc;
1025a85cb24fSFrançois Tigeot 	u8 crc_bytes[6];
1026a85cb24fSFrançois Tigeot 	uint32_t crcs[3];
1027a85cb24fSFrançois Tigeot 	int ret;
1028a85cb24fSFrançois Tigeot 
1029a85cb24fSFrançois Tigeot 	if (WARN_ON(!aux->crtc))
1030a85cb24fSFrançois Tigeot 		return;
1031a85cb24fSFrançois Tigeot 
1032a85cb24fSFrançois Tigeot 	crtc = aux->crtc;
1033a85cb24fSFrançois Tigeot 	while (crtc->crc.opened) {
1034a85cb24fSFrançois Tigeot 		drm_crtc_wait_one_vblank(crtc);
1035a85cb24fSFrançois Tigeot 		if (!crtc->crc.opened)
1036a85cb24fSFrançois Tigeot 			break;
1037a85cb24fSFrançois Tigeot 
1038a85cb24fSFrançois Tigeot 		ret = drm_dp_aux_get_crc(aux, crc_bytes);
1039a85cb24fSFrançois Tigeot 		if (ret == -EAGAIN) {
1040a85cb24fSFrançois Tigeot 			usleep_range(1000, 2000);
1041a85cb24fSFrançois Tigeot 			ret = drm_dp_aux_get_crc(aux, crc_bytes);
1042a85cb24fSFrançois Tigeot 		}
1043a85cb24fSFrançois Tigeot 
1044a85cb24fSFrançois Tigeot 		if (ret == -EAGAIN) {
1045a85cb24fSFrançois Tigeot 			DRM_DEBUG_KMS("Get CRC failed after retrying: %d\n",
1046a85cb24fSFrançois Tigeot 				      ret);
1047a85cb24fSFrançois Tigeot 			continue;
1048a85cb24fSFrançois Tigeot 		} else if (ret) {
1049a85cb24fSFrançois Tigeot 			DRM_DEBUG_KMS("Failed to get a CRC: %d\n", ret);
1050a85cb24fSFrançois Tigeot 			continue;
1051a85cb24fSFrançois Tigeot 		}
1052a85cb24fSFrançois Tigeot 
1053a85cb24fSFrançois Tigeot 		crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1054a85cb24fSFrançois Tigeot 		crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1055a85cb24fSFrançois Tigeot 		crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1056a85cb24fSFrançois Tigeot 		drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1057a85cb24fSFrançois Tigeot 	}
1058a85cb24fSFrançois Tigeot }
1059a85cb24fSFrançois Tigeot 
10601dedbd3bSFrançois Tigeot /**
10611dedbd3bSFrançois Tigeot  * drm_dp_aux_init() - minimally initialise an aux channel
10621dedbd3bSFrançois Tigeot  * @aux: DisplayPort AUX channel
10631dedbd3bSFrançois Tigeot  *
10641dedbd3bSFrançois Tigeot  * If you need to use the drm_dp_aux's i2c adapter prior to registering it
10651dedbd3bSFrançois Tigeot  * with the outside world, call drm_dp_aux_init() first. You must still
10661dedbd3bSFrançois Tigeot  * call drm_dp_aux_register() once the connector has been registered to
10671dedbd3bSFrançois Tigeot  * allow userspace access to the auxiliary DP channel.
10681dedbd3bSFrançois Tigeot  */
drm_dp_aux_init(struct drm_dp_aux * aux)10691dedbd3bSFrançois Tigeot void drm_dp_aux_init(struct drm_dp_aux *aux)
10701dedbd3bSFrançois Tigeot {
10711dedbd3bSFrançois Tigeot 	lockinit(&aux->hw_mutex, "ahwm", 0, LK_CANRECURSE);
1072a85cb24fSFrançois Tigeot 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
10731dedbd3bSFrançois Tigeot 
10741dedbd3bSFrançois Tigeot 	aux->ddc.algo = &drm_dp_i2c_algo;
10751dedbd3bSFrançois Tigeot 	aux->ddc.algo_data = aux;
10761dedbd3bSFrançois Tigeot 	aux->ddc.retries = 3;
10771dedbd3bSFrançois Tigeot 
10781dedbd3bSFrançois Tigeot 	aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
10791dedbd3bSFrançois Tigeot }
10801dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_dp_aux_init);
10811dedbd3bSFrançois Tigeot 
1082477eb7f9SFrançois Tigeot /**
1083477eb7f9SFrançois Tigeot  * drm_dp_aux_register() - initialise and register aux channel
1084477eb7f9SFrançois Tigeot  * @aux: DisplayPort AUX channel
1085477eb7f9SFrançois Tigeot  *
10861dedbd3bSFrançois Tigeot  * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
10871dedbd3bSFrançois Tigeot  *
1088477eb7f9SFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
1089477eb7f9SFrançois Tigeot  */
drm_dp_aux_register(struct drm_dp_aux * aux)1090477eb7f9SFrançois Tigeot int drm_dp_aux_register(struct drm_dp_aux *aux)
1091477eb7f9SFrançois Tigeot {
1092c0e85e96SFrançois Tigeot 	int ret;
1093c0e85e96SFrançois Tigeot 
10941dedbd3bSFrançois Tigeot 	if (!aux->ddc.algo)
10951dedbd3bSFrançois Tigeot 		drm_dp_aux_init(aux);
1096477eb7f9SFrançois Tigeot 
10979f4ca867SFrançois Tigeot #if 0
1098477eb7f9SFrançois Tigeot 	aux->ddc.class = I2C_CLASS_DDC;
1099477eb7f9SFrançois Tigeot 	aux->ddc.owner = THIS_MODULE;
11009f4ca867SFrançois Tigeot #endif
1101477eb7f9SFrançois Tigeot 	aux->ddc.dev.parent = aux->dev;
11029f4ca867SFrançois Tigeot #if 0
1103477eb7f9SFrançois Tigeot 	aux->ddc.dev.of_node = aux->dev->of_node;
11049f4ca867SFrançois Tigeot #endif
1105477eb7f9SFrançois Tigeot 
1106477eb7f9SFrançois Tigeot 	strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1107477eb7f9SFrançois Tigeot 		sizeof(aux->ddc.name));
1108477eb7f9SFrançois Tigeot 
1109c0e85e96SFrançois Tigeot 	ret = drm_dp_aux_register_devnode(aux);
1110c0e85e96SFrançois Tigeot 	if (ret)
1111c0e85e96SFrançois Tigeot 		return ret;
1112c0e85e96SFrançois Tigeot 
1113c0e85e96SFrançois Tigeot 	ret = i2c_add_adapter(&aux->ddc);
1114c0e85e96SFrançois Tigeot 	if (ret) {
1115c0e85e96SFrançois Tigeot 		drm_dp_aux_unregister_devnode(aux);
1116c0e85e96SFrançois Tigeot 		return ret;
1117c0e85e96SFrançois Tigeot 	}
1118c0e85e96SFrançois Tigeot 
1119c0e85e96SFrançois Tigeot 	return 0;
1120477eb7f9SFrançois Tigeot }
1121477eb7f9SFrançois Tigeot EXPORT_SYMBOL(drm_dp_aux_register);
1122477eb7f9SFrançois Tigeot 
1123477eb7f9SFrançois Tigeot /**
11241b13d190SFrançois Tigeot  * drm_dp_aux_unregister() - unregister an AUX adapter
11251b13d190SFrançois Tigeot  * @aux: DisplayPort AUX channel
11261b13d190SFrançois Tigeot  */
drm_dp_aux_unregister(struct drm_dp_aux * aux)11271b13d190SFrançois Tigeot void drm_dp_aux_unregister(struct drm_dp_aux *aux)
11281b13d190SFrançois Tigeot {
1129c0e85e96SFrançois Tigeot 	drm_dp_aux_unregister_devnode(aux);
11301b13d190SFrançois Tigeot 	i2c_del_adapter(&aux->ddc);
11311b13d190SFrançois Tigeot }
11321b13d190SFrançois Tigeot EXPORT_SYMBOL(drm_dp_aux_unregister);
11331dedbd3bSFrançois Tigeot 
11341dedbd3bSFrançois Tigeot #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
11351dedbd3bSFrançois Tigeot 
11361dedbd3bSFrançois Tigeot /**
11371dedbd3bSFrançois Tigeot  * drm_dp_psr_setup_time() - PSR setup in time usec
11381dedbd3bSFrançois Tigeot  * @psr_cap: PSR capabilities from DPCD
11391dedbd3bSFrançois Tigeot  *
11401dedbd3bSFrançois Tigeot  * Returns:
11411dedbd3bSFrançois Tigeot  * PSR setup time for the panel in microseconds,  negative
11421dedbd3bSFrançois Tigeot  * error code on failure.
11431dedbd3bSFrançois Tigeot  */
drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])11441dedbd3bSFrançois Tigeot int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
11451dedbd3bSFrançois Tigeot {
11461dedbd3bSFrançois Tigeot 	static const u16 psr_setup_time_us[] = {
11471dedbd3bSFrançois Tigeot 		PSR_SETUP_TIME(330),
11481dedbd3bSFrançois Tigeot 		PSR_SETUP_TIME(275),
11491dedbd3bSFrançois Tigeot 		PSR_SETUP_TIME(165),
11501dedbd3bSFrançois Tigeot 		PSR_SETUP_TIME(110),
11511dedbd3bSFrançois Tigeot 		PSR_SETUP_TIME(55),
11521dedbd3bSFrançois Tigeot 		PSR_SETUP_TIME(0),
11531dedbd3bSFrançois Tigeot 	};
11541dedbd3bSFrançois Tigeot 	int i;
11551dedbd3bSFrançois Tigeot 
11561dedbd3bSFrançois Tigeot 	i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
11571dedbd3bSFrançois Tigeot 	if (i >= ARRAY_SIZE(psr_setup_time_us))
11581dedbd3bSFrançois Tigeot 		return -EINVAL;
11591dedbd3bSFrançois Tigeot 
11601dedbd3bSFrançois Tigeot 	return psr_setup_time_us[i];
11611dedbd3bSFrançois Tigeot }
11621dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_dp_psr_setup_time);
11631dedbd3bSFrançois Tigeot 
11641dedbd3bSFrançois Tigeot #undef PSR_SETUP_TIME
1165a85cb24fSFrançois Tigeot 
1166a85cb24fSFrançois Tigeot /**
1167a85cb24fSFrançois Tigeot  * drm_dp_start_crc() - start capture of frame CRCs
1168a85cb24fSFrançois Tigeot  * @aux: DisplayPort AUX channel
1169a85cb24fSFrançois Tigeot  * @crtc: CRTC displaying the frames whose CRCs are to be captured
1170a85cb24fSFrançois Tigeot  *
1171a85cb24fSFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
1172a85cb24fSFrançois Tigeot  */
drm_dp_start_crc(struct drm_dp_aux * aux,struct drm_crtc * crtc)1173a85cb24fSFrançois Tigeot int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1174a85cb24fSFrançois Tigeot {
1175a85cb24fSFrançois Tigeot 	u8 buf;
1176a85cb24fSFrançois Tigeot 	int ret;
1177a85cb24fSFrançois Tigeot 
1178a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1179a85cb24fSFrançois Tigeot 	if (ret < 0)
1180a85cb24fSFrançois Tigeot 		return ret;
1181a85cb24fSFrançois Tigeot 
1182a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1183a85cb24fSFrançois Tigeot 	if (ret < 0)
1184a85cb24fSFrançois Tigeot 		return ret;
1185a85cb24fSFrançois Tigeot 
1186a85cb24fSFrançois Tigeot 	aux->crc_count = 0;
1187a85cb24fSFrançois Tigeot 	aux->crtc = crtc;
1188a85cb24fSFrançois Tigeot 	schedule_work(&aux->crc_work);
1189a85cb24fSFrançois Tigeot 
1190a85cb24fSFrançois Tigeot 	return 0;
1191a85cb24fSFrançois Tigeot }
1192a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_dp_start_crc);
1193a85cb24fSFrançois Tigeot 
1194a85cb24fSFrançois Tigeot /**
1195a85cb24fSFrançois Tigeot  * drm_dp_stop_crc() - stop capture of frame CRCs
1196a85cb24fSFrançois Tigeot  * @aux: DisplayPort AUX channel
1197a85cb24fSFrançois Tigeot  *
1198a85cb24fSFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
1199a85cb24fSFrançois Tigeot  */
drm_dp_stop_crc(struct drm_dp_aux * aux)1200a85cb24fSFrançois Tigeot int drm_dp_stop_crc(struct drm_dp_aux *aux)
1201a85cb24fSFrançois Tigeot {
1202a85cb24fSFrançois Tigeot 	u8 buf;
1203a85cb24fSFrançois Tigeot 	int ret;
1204a85cb24fSFrançois Tigeot 
1205a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1206a85cb24fSFrançois Tigeot 	if (ret < 0)
1207a85cb24fSFrançois Tigeot 		return ret;
1208a85cb24fSFrançois Tigeot 
1209a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1210a85cb24fSFrançois Tigeot 	if (ret < 0)
1211a85cb24fSFrançois Tigeot 		return ret;
1212a85cb24fSFrançois Tigeot 
1213a85cb24fSFrançois Tigeot 	flush_work(&aux->crc_work);
1214a85cb24fSFrançois Tigeot 	aux->crtc = NULL;
1215a85cb24fSFrançois Tigeot 
1216a85cb24fSFrançois Tigeot 	return 0;
1217a85cb24fSFrançois Tigeot }
1218a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_dp_stop_crc);
1219a85cb24fSFrançois Tigeot 
1220a85cb24fSFrançois Tigeot struct dpcd_quirk {
1221a85cb24fSFrançois Tigeot 	u8 oui[3];
1222a85cb24fSFrançois Tigeot 	bool is_branch;
1223a85cb24fSFrançois Tigeot 	u32 quirks;
1224a85cb24fSFrançois Tigeot };
1225a85cb24fSFrançois Tigeot 
1226a85cb24fSFrançois Tigeot #define OUI(first, second, third) { (first), (second), (third) }
1227a85cb24fSFrançois Tigeot 
1228a85cb24fSFrançois Tigeot static const struct dpcd_quirk dpcd_quirk_list[] = {
1229a85cb24fSFrançois Tigeot 	/* Analogix 7737 needs reduced M and N at HBR2 link rates */
1230a85cb24fSFrançois Tigeot 	{ OUI(0x00, 0x22, 0xb9), true, BIT(DP_DPCD_QUIRK_LIMITED_M_N) },
1231a85cb24fSFrançois Tigeot };
1232a85cb24fSFrançois Tigeot 
1233a85cb24fSFrançois Tigeot #undef OUI
1234a85cb24fSFrançois Tigeot 
1235a85cb24fSFrançois Tigeot /*
1236a85cb24fSFrançois Tigeot  * Get a bit mask of DPCD quirks for the sink/branch device identified by
1237a85cb24fSFrançois Tigeot  * ident. The quirk data is shared but it's up to the drivers to act on the
1238a85cb24fSFrançois Tigeot  * data.
1239a85cb24fSFrançois Tigeot  *
1240a85cb24fSFrançois Tigeot  * For now, only the OUI (first three bytes) is used, but this may be extended
1241a85cb24fSFrançois Tigeot  * to device identification string and hardware/firmware revisions later.
1242a85cb24fSFrançois Tigeot  */
1243a85cb24fSFrançois Tigeot static u32
drm_dp_get_quirks(const struct drm_dp_dpcd_ident * ident,bool is_branch)1244a85cb24fSFrançois Tigeot drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1245a85cb24fSFrançois Tigeot {
1246a85cb24fSFrançois Tigeot 	const struct dpcd_quirk *quirk;
1247a85cb24fSFrançois Tigeot 	u32 quirks = 0;
1248a85cb24fSFrançois Tigeot 	int i;
1249a85cb24fSFrançois Tigeot 
1250a85cb24fSFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1251a85cb24fSFrançois Tigeot 		quirk = &dpcd_quirk_list[i];
1252a85cb24fSFrançois Tigeot 
1253a85cb24fSFrançois Tigeot 		if (quirk->is_branch != is_branch)
1254a85cb24fSFrançois Tigeot 			continue;
1255a85cb24fSFrançois Tigeot 
1256a85cb24fSFrançois Tigeot 		if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1257a85cb24fSFrançois Tigeot 			continue;
1258a85cb24fSFrançois Tigeot 
1259a85cb24fSFrançois Tigeot 		quirks |= quirk->quirks;
1260a85cb24fSFrançois Tigeot 	}
1261a85cb24fSFrançois Tigeot 
1262a85cb24fSFrançois Tigeot 	return quirks;
1263a85cb24fSFrançois Tigeot }
1264a85cb24fSFrançois Tigeot 
1265a85cb24fSFrançois Tigeot /**
1266a85cb24fSFrançois Tigeot  * drm_dp_read_desc - read sink/branch descriptor from DPCD
1267a85cb24fSFrançois Tigeot  * @aux: DisplayPort AUX channel
1268a85cb24fSFrançois Tigeot  * @desc: Device decriptor to fill from DPCD
1269a85cb24fSFrançois Tigeot  * @is_branch: true for branch devices, false for sink devices
1270a85cb24fSFrançois Tigeot  *
1271a85cb24fSFrançois Tigeot  * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
1272a85cb24fSFrançois Tigeot  * identification.
1273a85cb24fSFrançois Tigeot  *
1274a85cb24fSFrançois Tigeot  * Returns 0 on success or a negative error code on failure.
1275a85cb24fSFrançois Tigeot  */
drm_dp_read_desc(struct drm_dp_aux * aux,struct drm_dp_desc * desc,bool is_branch)1276a85cb24fSFrançois Tigeot int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
1277a85cb24fSFrançois Tigeot 		     bool is_branch)
1278a85cb24fSFrançois Tigeot {
1279a85cb24fSFrançois Tigeot 	struct drm_dp_dpcd_ident *ident = &desc->ident;
1280a85cb24fSFrançois Tigeot 	unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
1281a85cb24fSFrançois Tigeot 	int ret, dev_id_len;
1282a85cb24fSFrançois Tigeot 
1283a85cb24fSFrançois Tigeot 	ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
1284a85cb24fSFrançois Tigeot 	if (ret < 0)
1285a85cb24fSFrançois Tigeot 		return ret;
1286a85cb24fSFrançois Tigeot 
1287a85cb24fSFrançois Tigeot 	desc->quirks = drm_dp_get_quirks(ident, is_branch);
1288a85cb24fSFrançois Tigeot 
1289a85cb24fSFrançois Tigeot 	dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
1290a85cb24fSFrançois Tigeot 
1291a85cb24fSFrançois Tigeot 	DRM_DEBUG_KMS("DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
1292a85cb24fSFrançois Tigeot 		      is_branch ? "branch" : "sink",
1293a85cb24fSFrançois Tigeot 		      (int)sizeof(ident->oui), ident->oui,
1294a85cb24fSFrançois Tigeot 		      dev_id_len, ident->device_id,
1295a85cb24fSFrançois Tigeot 		      ident->hw_rev >> 4, ident->hw_rev & 0xf,
1296a85cb24fSFrançois Tigeot 		      ident->sw_major_rev, ident->sw_minor_rev,
1297a85cb24fSFrançois Tigeot 		      desc->quirks);
1298a85cb24fSFrançois Tigeot 
1299a85cb24fSFrançois Tigeot 	return 0;
1300a85cb24fSFrançois Tigeot }
1301a85cb24fSFrançois Tigeot EXPORT_SYMBOL(drm_dp_read_desc);
1302