Home | History | Annotate | Line # | Download | only in drm
      1  1.17       mrg /*	$NetBSD: drm_dp_helper.c,v 1.17 2023/08/08 06:58:20 mrg Exp $	*/
      2   1.5  riastrad 
      3   1.1  riastrad /*
      4   1.1  riastrad  * Copyright  2009 Keith Packard
      5   1.1  riastrad  *
      6   1.1  riastrad  * Permission to use, copy, modify, distribute, and sell this software and its
      7   1.1  riastrad  * documentation for any purpose is hereby granted without fee, provided that
      8   1.1  riastrad  * the above copyright notice appear in all copies and that both that copyright
      9   1.1  riastrad  * notice and this permission notice appear in supporting documentation, and
     10   1.1  riastrad  * that the name of the copyright holders not be used in advertising or
     11   1.1  riastrad  * publicity pertaining to distribution of the software without specific,
     12   1.1  riastrad  * written prior permission.  The copyright holders make no representations
     13   1.1  riastrad  * about the suitability of this software for any purpose.  It is provided "as
     14   1.1  riastrad  * is" without express or implied warranty.
     15   1.1  riastrad  *
     16   1.1  riastrad  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     17   1.1  riastrad  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     18   1.1  riastrad  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     19   1.1  riastrad  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     20   1.1  riastrad  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     21   1.1  riastrad  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     22   1.1  riastrad  * OF THIS SOFTWARE.
     23   1.1  riastrad  */
     24   1.1  riastrad 
     25   1.5  riastrad #include <sys/cdefs.h>
     26  1.17       mrg __KERNEL_RCSID(0, "$NetBSD: drm_dp_helper.c,v 1.17 2023/08/08 06:58:20 mrg Exp $");
     27   1.5  riastrad 
     28  1.13  riastrad #include <linux/delay.h>
     29  1.13  riastrad #include <linux/errno.h>
     30  1.13  riastrad #include <linux/i2c.h>
     31  1.13  riastrad #include <linux/init.h>
     32   1.1  riastrad #include <linux/kernel.h>
     33   1.1  riastrad #include <linux/module.h>
     34   1.1  riastrad #include <linux/sched.h>
     35  1.13  riastrad #include <linux/seq_file.h>
     36  1.13  riastrad 
     37   1.1  riastrad #include <drm/drm_dp_helper.h>
     38  1.13  riastrad #include <drm/drm_print.h>
     39  1.13  riastrad #include <drm/drm_vblank.h>
     40  1.13  riastrad #include <drm/drm_dp_mst_helper.h>
     41  1.13  riastrad 
     42  1.13  riastrad #include "drm_crtc_helper_internal.h"
     43   1.1  riastrad 
     44  1.10  riastrad #include <linux/nbsd-namespace.h>
     45  1.10  riastrad 
     46   1.1  riastrad /**
     47   1.1  riastrad  * DOC: dp helpers
     48   1.1  riastrad  *
     49   1.1  riastrad  * These functions contain some common logic and helpers at various abstraction
     50   1.1  riastrad  * levels to deal with Display Port sink devices and related things like DP aux
     51   1.1  riastrad  * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
     52   1.1  riastrad  * blocks, ...
     53   1.1  riastrad  */
     54   1.1  riastrad 
     55   1.1  riastrad /* Helpers for DP link training */
     56   1.3  riastrad static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
     57   1.1  riastrad {
     58   1.1  riastrad 	return link_status[r - DP_LANE0_1_STATUS];
     59   1.1  riastrad }
     60   1.1  riastrad 
     61   1.3  riastrad static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
     62   1.1  riastrad 			     int lane)
     63   1.1  riastrad {
     64   1.1  riastrad 	int i = DP_LANE0_1_STATUS + (lane >> 1);
     65   1.1  riastrad 	int s = (lane & 1) * 4;
     66   1.1  riastrad 	u8 l = dp_link_status(link_status, i);
     67   1.1  riastrad 	return (l >> s) & 0xf;
     68   1.1  riastrad }
     69   1.1  riastrad 
     70   1.3  riastrad bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
     71   1.1  riastrad 			  int lane_count)
     72   1.1  riastrad {
     73   1.1  riastrad 	u8 lane_align;
     74   1.1  riastrad 	u8 lane_status;
     75   1.1  riastrad 	int lane;
     76   1.1  riastrad 
     77   1.1  riastrad 	lane_align = dp_link_status(link_status,
     78   1.1  riastrad 				    DP_LANE_ALIGN_STATUS_UPDATED);
     79   1.1  riastrad 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
     80   1.1  riastrad 		return false;
     81   1.1  riastrad 	for (lane = 0; lane < lane_count; lane++) {
     82   1.1  riastrad 		lane_status = dp_get_lane_status(link_status, lane);
     83   1.1  riastrad 		if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
     84   1.1  riastrad 			return false;
     85   1.1  riastrad 	}
     86   1.1  riastrad 	return true;
     87   1.1  riastrad }
     88   1.1  riastrad EXPORT_SYMBOL(drm_dp_channel_eq_ok);
     89   1.1  riastrad 
     90   1.3  riastrad bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
     91   1.1  riastrad 			      int lane_count)
     92   1.1  riastrad {
     93   1.1  riastrad 	int lane;
     94   1.1  riastrad 	u8 lane_status;
     95   1.1  riastrad 
     96   1.1  riastrad 	for (lane = 0; lane < lane_count; lane++) {
     97   1.1  riastrad 		lane_status = dp_get_lane_status(link_status, lane);
     98   1.1  riastrad 		if ((lane_status & DP_LANE_CR_DONE) == 0)
     99   1.1  riastrad 			return false;
    100   1.1  riastrad 	}
    101   1.1  riastrad 	return true;
    102   1.1  riastrad }
    103   1.1  riastrad EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
    104   1.1  riastrad 
    105   1.3  riastrad u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
    106   1.1  riastrad 				     int lane)
    107   1.1  riastrad {
    108   1.1  riastrad 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
    109   1.1  riastrad 	int s = ((lane & 1) ?
    110   1.1  riastrad 		 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
    111   1.1  riastrad 		 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
    112   1.1  riastrad 	u8 l = dp_link_status(link_status, i);
    113   1.1  riastrad 
    114   1.1  riastrad 	return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
    115   1.1  riastrad }
    116   1.1  riastrad EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
    117   1.1  riastrad 
    118   1.3  riastrad u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
    119   1.1  riastrad 					  int lane)
    120   1.1  riastrad {
    121   1.1  riastrad 	int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
    122   1.1  riastrad 	int s = ((lane & 1) ?
    123   1.1  riastrad 		 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
    124   1.1  riastrad 		 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
    125   1.1  riastrad 	u8 l = dp_link_status(link_status, i);
    126   1.1  riastrad 
    127   1.1  riastrad 	return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
    128   1.1  riastrad }
    129   1.1  riastrad EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
    130   1.1  riastrad 
    131  1.17       mrg #ifndef __NetBSD__
    132  1.17       mrg /*
    133  1.17       mrg  * XXXGCC12
    134  1.17       mrg  * this unused function is bad.  DP_LINK_STATUS_SIZE is 6, and
    135  1.17       mrg  * DP_ADJUST_REQUEST_POST_CURSOR2 triggers an offset of 10 into link_status[].
    136  1.17       mrg  * fortunately, it is not used.
    137  1.17       mrg  */
    138  1.13  riastrad u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
    139  1.13  riastrad 					 unsigned int lane)
    140  1.13  riastrad {
    141  1.13  riastrad 	unsigned int offset = DP_ADJUST_REQUEST_POST_CURSOR2;
    142  1.13  riastrad 	u8 value = dp_link_status(link_status, offset);
    143  1.13  riastrad 
    144  1.13  riastrad 	return (value >> (lane << 1)) & 0x3;
    145  1.13  riastrad }
    146  1.13  riastrad EXPORT_SYMBOL(drm_dp_get_adjust_request_post_cursor);
    147  1.17       mrg #endif
    148  1.13  riastrad 
    149  1.13  riastrad void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
    150  1.13  riastrad {
    151  1.13  riastrad 	unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
    152  1.13  riastrad 					 DP_TRAINING_AUX_RD_MASK;
    153  1.13  riastrad 
    154  1.13  riastrad 	if (rd_interval > 4)
    155  1.13  riastrad 		DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
    156  1.13  riastrad 			      rd_interval);
    157  1.13  riastrad 
    158  1.13  riastrad 	if (rd_interval == 0 || dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
    159  1.13  riastrad 		rd_interval = 100;
    160   1.1  riastrad 	else
    161  1.13  riastrad 		rd_interval *= 4 * USEC_PER_MSEC;
    162  1.13  riastrad 
    163  1.13  riastrad 	usleep_range(rd_interval, rd_interval * 2);
    164   1.1  riastrad }
    165   1.1  riastrad EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
    166   1.1  riastrad 
    167  1.13  riastrad void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
    168  1.13  riastrad {
    169  1.13  riastrad 	unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
    170  1.13  riastrad 					 DP_TRAINING_AUX_RD_MASK;
    171  1.13  riastrad 
    172  1.13  riastrad 	if (rd_interval > 4)
    173  1.13  riastrad 		DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
    174  1.13  riastrad 			      rd_interval);
    175  1.13  riastrad 
    176  1.13  riastrad 	if (rd_interval == 0)
    177  1.13  riastrad 		rd_interval = 400;
    178   1.1  riastrad 	else
    179  1.13  riastrad 		rd_interval *= 4 * USEC_PER_MSEC;
    180  1.13  riastrad 
    181  1.13  riastrad 	usleep_range(rd_interval, rd_interval * 2);
    182   1.1  riastrad }
    183   1.1  riastrad EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
    184   1.1  riastrad 
    185   1.1  riastrad u8 drm_dp_link_rate_to_bw_code(int link_rate)
    186   1.1  riastrad {
    187  1.13  riastrad 	/* Spec says link_bw = link_rate / 0.27Gbps */
    188  1.13  riastrad 	return link_rate / 27000;
    189   1.1  riastrad }
    190   1.1  riastrad EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
    191   1.1  riastrad 
    192   1.1  riastrad int drm_dp_bw_code_to_link_rate(u8 link_bw)
    193   1.1  riastrad {
    194  1.13  riastrad 	/* Spec says link_rate = link_bw * 0.27Gbps */
    195  1.13  riastrad 	return link_bw * 27000;
    196   1.1  riastrad }
    197   1.1  riastrad EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
    198   1.3  riastrad 
    199   1.5  riastrad #define AUX_RETRY_INTERVAL 500 /* us */
    200   1.5  riastrad 
    201  1.13  riastrad static inline void
    202  1.13  riastrad drm_dp_dump_access(const struct drm_dp_aux *aux,
    203  1.13  riastrad 		   u8 request, uint offset, void *buffer, int ret)
    204  1.13  riastrad {
    205  1.13  riastrad 	const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
    206  1.13  riastrad 
    207  1.13  riastrad 	if (ret > 0)
    208  1.13  riastrad 		DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
    209  1.13  riastrad 			     aux->name, offset, arrow, ret, min(ret, 20), buffer);
    210  1.13  riastrad 	else
    211  1.13  riastrad 		DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d)\n",
    212  1.13  riastrad 			     aux->name, offset, arrow, ret);
    213  1.13  riastrad }
    214  1.13  riastrad 
    215   1.3  riastrad /**
    216   1.3  riastrad  * DOC: dp helpers
    217   1.3  riastrad  *
    218   1.3  riastrad  * The DisplayPort AUX channel is an abstraction to allow generic, driver-
    219   1.3  riastrad  * independent access to AUX functionality. Drivers can take advantage of
    220   1.3  riastrad  * this by filling in the fields of the drm_dp_aux structure.
    221   1.3  riastrad  *
    222   1.3  riastrad  * Transactions are described using a hardware-independent drm_dp_aux_msg
    223   1.3  riastrad  * structure, which is passed into a driver's .transfer() implementation.
    224   1.3  riastrad  * Both native and I2C-over-AUX transactions are supported.
    225   1.3  riastrad  */
    226   1.3  riastrad 
    227   1.3  riastrad static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
    228   1.3  riastrad 			      unsigned int offset, void *buffer, size_t size)
    229   1.3  riastrad {
    230   1.3  riastrad 	struct drm_dp_aux_msg msg;
    231  1.13  riastrad 	unsigned int retry, native_reply;
    232  1.13  riastrad 	int err = 0, ret = 0;
    233   1.3  riastrad 
    234   1.3  riastrad 	memset(&msg, 0, sizeof(msg));
    235   1.3  riastrad 	msg.address = offset;
    236   1.3  riastrad 	msg.request = request;
    237   1.3  riastrad 	msg.buffer = buffer;
    238   1.3  riastrad 	msg.size = size;
    239   1.3  riastrad 
    240   1.5  riastrad 	mutex_lock(&aux->hw_mutex);
    241   1.5  riastrad 
    242   1.3  riastrad 	/*
    243   1.3  riastrad 	 * The specification doesn't give any recommendation on how often to
    244   1.5  riastrad 	 * retry native transactions. We used to retry 7 times like for
    245   1.5  riastrad 	 * aux i2c transactions but real world devices this wasn't
    246   1.5  riastrad 	 * sufficient, bump to 32 which makes Dell 4k monitors happier.
    247   1.3  riastrad 	 */
    248   1.5  riastrad 	for (retry = 0; retry < 32; retry++) {
    249  1.13  riastrad 		if (ret != 0 && ret != -ETIMEDOUT) {
    250  1.13  riastrad 			usleep_range(AUX_RETRY_INTERVAL,
    251  1.13  riastrad 				     AUX_RETRY_INTERVAL + 100);
    252  1.13  riastrad 		}
    253   1.5  riastrad 
    254  1.13  riastrad 		ret = aux->transfer(aux, &msg);
    255  1.13  riastrad 		if (ret >= 0) {
    256  1.13  riastrad 			native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
    257  1.13  riastrad 			if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
    258  1.13  riastrad 				if (ret == size)
    259  1.13  riastrad 					goto unlock;
    260  1.13  riastrad 
    261  1.13  riastrad 				ret = -EPROTO;
    262  1.13  riastrad 			} else
    263  1.13  riastrad 				ret = -EIO;
    264   1.3  riastrad 		}
    265   1.3  riastrad 
    266  1.13  riastrad 		/*
    267  1.13  riastrad 		 * We want the error we return to be the error we received on
    268  1.13  riastrad 		 * the first transaction, since we may get a different error the
    269  1.13  riastrad 		 * next time we retry
    270  1.13  riastrad 		 */
    271  1.13  riastrad 		if (!err)
    272  1.13  riastrad 			err = ret;
    273   1.3  riastrad 	}
    274   1.3  riastrad 
    275  1.13  riastrad 	DRM_DEBUG_KMS("Too many retries, giving up. First error: %d\n", err);
    276  1.13  riastrad 	ret = err;
    277   1.5  riastrad 
    278   1.5  riastrad unlock:
    279   1.5  riastrad 	mutex_unlock(&aux->hw_mutex);
    280  1.13  riastrad 	return ret;
    281   1.3  riastrad }
    282   1.3  riastrad 
    283   1.3  riastrad /**
    284   1.3  riastrad  * drm_dp_dpcd_read() - read a series of bytes from the DPCD
    285  1.13  riastrad  * @aux: DisplayPort AUX channel (SST or MST)
    286   1.3  riastrad  * @offset: address of the (first) register to read
    287   1.3  riastrad  * @buffer: buffer to store the register values
    288   1.3  riastrad  * @size: number of bytes in @buffer
    289   1.3  riastrad  *
    290   1.3  riastrad  * Returns the number of bytes transferred on success, or a negative error
    291   1.3  riastrad  * code on failure. -EIO is returned if the request was NAKed by the sink or
    292   1.3  riastrad  * if the retry count was exceeded. If not all bytes were transferred, this
    293   1.3  riastrad  * function returns -EPROTO. Errors from the underlying AUX channel transfer
    294   1.3  riastrad  * function, with the exception of -EBUSY (which causes the transaction to
    295   1.3  riastrad  * be retried), are propagated to the caller.
    296   1.3  riastrad  */
    297   1.3  riastrad ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
    298   1.3  riastrad 			 void *buffer, size_t size)
    299   1.3  riastrad {
    300  1.13  riastrad 	int ret;
    301  1.13  riastrad 
    302  1.13  riastrad 	/*
    303  1.13  riastrad 	 * HP ZR24w corrupts the first DPCD access after entering power save
    304  1.13  riastrad 	 * mode. Eg. on a read, the entire buffer will be filled with the same
    305  1.13  riastrad 	 * byte. Do a throw away read to avoid corrupting anything we care
    306  1.13  riastrad 	 * about. Afterwards things will work correctly until the monitor
    307  1.13  riastrad 	 * gets woken up and subsequently re-enters power save mode.
    308  1.13  riastrad 	 *
    309  1.13  riastrad 	 * The user pressing any button on the monitor is enough to wake it
    310  1.13  riastrad 	 * up, so there is no particularly good place to do the workaround.
    311  1.13  riastrad 	 * We just have to do it before any DPCD access and hope that the
    312  1.13  riastrad 	 * monitor doesn't power down exactly after the throw away read.
    313  1.13  riastrad 	 */
    314  1.13  riastrad 	if (!aux->is_remote) {
    315  1.13  riastrad 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
    316  1.13  riastrad 					 buffer, 1);
    317  1.13  riastrad 		if (ret != 1)
    318  1.13  riastrad 			goto out;
    319  1.13  riastrad 	}
    320  1.13  riastrad 
    321  1.13  riastrad 	if (aux->is_remote)
    322  1.13  riastrad 		ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
    323  1.13  riastrad 	else
    324  1.13  riastrad 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
    325  1.13  riastrad 					 buffer, size);
    326  1.13  riastrad 
    327  1.13  riastrad out:
    328  1.13  riastrad 	drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
    329  1.13  riastrad 	return ret;
    330   1.3  riastrad }
    331   1.3  riastrad EXPORT_SYMBOL(drm_dp_dpcd_read);
    332   1.3  riastrad 
    333   1.3  riastrad /**
    334   1.3  riastrad  * drm_dp_dpcd_write() - write a series of bytes to the DPCD
    335  1.13  riastrad  * @aux: DisplayPort AUX channel (SST or MST)
    336   1.3  riastrad  * @offset: address of the (first) register to write
    337   1.3  riastrad  * @buffer: buffer containing the values to write
    338   1.3  riastrad  * @size: number of bytes in @buffer
    339   1.3  riastrad  *
    340   1.3  riastrad  * Returns the number of bytes transferred on success, or a negative error
    341   1.3  riastrad  * code on failure. -EIO is returned if the request was NAKed by the sink or
    342   1.3  riastrad  * if the retry count was exceeded. If not all bytes were transferred, this
    343   1.3  riastrad  * function returns -EPROTO. Errors from the underlying AUX channel transfer
    344   1.3  riastrad  * function, with the exception of -EBUSY (which causes the transaction to
    345   1.3  riastrad  * be retried), are propagated to the caller.
    346   1.3  riastrad  */
    347   1.3  riastrad ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
    348   1.3  riastrad 			  void *buffer, size_t size)
    349   1.3  riastrad {
    350  1.13  riastrad 	int ret;
    351  1.13  riastrad 
    352  1.13  riastrad 	if (aux->is_remote)
    353  1.13  riastrad 		ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
    354  1.13  riastrad 	else
    355  1.13  riastrad 		ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
    356  1.13  riastrad 					 buffer, size);
    357  1.13  riastrad 
    358  1.13  riastrad 	drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
    359  1.13  riastrad 	return ret;
    360   1.3  riastrad }
    361   1.3  riastrad EXPORT_SYMBOL(drm_dp_dpcd_write);
    362   1.3  riastrad 
    363   1.3  riastrad /**
    364   1.3  riastrad  * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
    365   1.3  riastrad  * @aux: DisplayPort AUX channel
    366   1.3  riastrad  * @status: buffer to store the link status in (must be at least 6 bytes)
    367   1.3  riastrad  *
    368   1.3  riastrad  * Returns the number of bytes transferred on success or a negative error
    369   1.3  riastrad  * code on failure.
    370   1.3  riastrad  */
    371   1.3  riastrad int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
    372   1.3  riastrad 				 u8 status[DP_LINK_STATUS_SIZE])
    373   1.3  riastrad {
    374   1.3  riastrad 	return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
    375   1.3  riastrad 				DP_LINK_STATUS_SIZE);
    376   1.3  riastrad }
    377   1.3  riastrad EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
    378   1.3  riastrad 
    379   1.3  riastrad /**
    380  1.13  riastrad  * drm_dp_downstream_max_clock() - extract branch device max
    381  1.13  riastrad  *                                 pixel rate for legacy VGA
    382  1.13  riastrad  *                                 converter or max TMDS clock
    383  1.13  riastrad  *                                 rate for others
    384  1.13  riastrad  * @dpcd: DisplayPort configuration data
    385  1.13  riastrad  * @port_cap: port capabilities
    386   1.3  riastrad  *
    387  1.13  riastrad  * Returns max clock in kHz on success or 0 if max clock not defined
    388   1.3  riastrad  */
    389  1.13  riastrad int drm_dp_downstream_max_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
    390  1.13  riastrad 				const u8 port_cap[4])
    391   1.3  riastrad {
    392  1.13  riastrad 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
    393  1.13  riastrad 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
    394  1.13  riastrad 		DP_DETAILED_CAP_INFO_AVAILABLE;
    395   1.3  riastrad 
    396  1.13  riastrad 	if (!detailed_cap_info)
    397  1.13  riastrad 		return 0;
    398   1.3  riastrad 
    399  1.13  riastrad 	switch (type) {
    400  1.13  riastrad 	case DP_DS_PORT_TYPE_VGA:
    401  1.13  riastrad 		return port_cap[1] * 8 * 1000;
    402  1.13  riastrad 	case DP_DS_PORT_TYPE_DVI:
    403  1.13  riastrad 	case DP_DS_PORT_TYPE_HDMI:
    404  1.13  riastrad 	case DP_DS_PORT_TYPE_DP_DUALMODE:
    405  1.13  riastrad 		return port_cap[1] * 2500;
    406  1.13  riastrad 	default:
    407  1.13  riastrad 		return 0;
    408  1.13  riastrad 	}
    409   1.3  riastrad }
    410  1.13  riastrad EXPORT_SYMBOL(drm_dp_downstream_max_clock);
    411   1.3  riastrad 
    412   1.3  riastrad /**
    413  1.13  riastrad  * drm_dp_downstream_max_bpc() - extract branch device max
    414  1.13  riastrad  *                               bits per component
    415  1.13  riastrad  * @dpcd: DisplayPort configuration data
    416  1.13  riastrad  * @port_cap: port capabilities
    417   1.3  riastrad  *
    418  1.13  riastrad  * Returns max bpc on success or 0 if max bpc not defined
    419   1.3  riastrad  */
    420  1.13  riastrad int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
    421  1.13  riastrad 			      const u8 port_cap[4])
    422   1.3  riastrad {
    423  1.13  riastrad 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
    424  1.13  riastrad 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
    425  1.13  riastrad 		DP_DETAILED_CAP_INFO_AVAILABLE;
    426  1.13  riastrad 	int bpc;
    427   1.3  riastrad 
    428  1.13  riastrad 	if (!detailed_cap_info)
    429   1.3  riastrad 		return 0;
    430   1.3  riastrad 
    431  1.13  riastrad 	switch (type) {
    432  1.13  riastrad 	case DP_DS_PORT_TYPE_VGA:
    433  1.13  riastrad 	case DP_DS_PORT_TYPE_DVI:
    434  1.13  riastrad 	case DP_DS_PORT_TYPE_HDMI:
    435  1.13  riastrad 	case DP_DS_PORT_TYPE_DP_DUALMODE:
    436  1.13  riastrad 		bpc = port_cap[2] & DP_DS_MAX_BPC_MASK;
    437  1.13  riastrad 
    438  1.13  riastrad 		switch (bpc) {
    439  1.13  riastrad 		case DP_DS_8BPC:
    440  1.13  riastrad 			return 8;
    441  1.13  riastrad 		case DP_DS_10BPC:
    442  1.13  riastrad 			return 10;
    443  1.13  riastrad 		case DP_DS_12BPC:
    444  1.13  riastrad 			return 12;
    445  1.13  riastrad 		case DP_DS_16BPC:
    446  1.13  riastrad 			return 16;
    447  1.13  riastrad 		}
    448  1.13  riastrad 		/* fall through */
    449  1.13  riastrad 	default:
    450  1.13  riastrad 		return 0;
    451  1.13  riastrad 	}
    452   1.3  riastrad }
    453  1.13  riastrad EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
    454   1.3  riastrad 
    455   1.3  riastrad /**
    456  1.13  riastrad  * drm_dp_downstream_id() - identify branch device
    457   1.5  riastrad  * @aux: DisplayPort AUX channel
    458  1.13  riastrad  * @id: DisplayPort branch device id
    459   1.5  riastrad  *
    460  1.13  riastrad  * Returns branch device id on success or NULL on failure
    461   1.5  riastrad  */
    462  1.13  riastrad int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
    463   1.5  riastrad {
    464  1.13  riastrad 	return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
    465   1.5  riastrad }
    466  1.13  riastrad EXPORT_SYMBOL(drm_dp_downstream_id);
    467   1.5  riastrad 
    468   1.5  riastrad /**
    469  1.13  riastrad  * drm_dp_downstream_debug() - debug DP branch devices
    470  1.13  riastrad  * @m: pointer for debugfs file
    471  1.13  riastrad  * @dpcd: DisplayPort configuration data
    472  1.13  riastrad  * @port_cap: port capabilities
    473   1.3  riastrad  * @aux: DisplayPort AUX channel
    474   1.3  riastrad  *
    475   1.3  riastrad  */
    476  1.14  riastrad #ifndef __NetBSD__
    477  1.13  riastrad void drm_dp_downstream_debug(struct seq_file *m,
    478  1.13  riastrad 			     const u8 dpcd[DP_RECEIVER_CAP_SIZE],
    479  1.13  riastrad 			     const u8 port_cap[4], struct drm_dp_aux *aux)
    480  1.13  riastrad {
    481  1.13  riastrad 	bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
    482  1.13  riastrad 				 DP_DETAILED_CAP_INFO_AVAILABLE;
    483  1.13  riastrad 	int clk;
    484  1.13  riastrad 	int bpc;
    485  1.13  riastrad 	char id[7];
    486  1.13  riastrad 	int len;
    487  1.13  riastrad 	uint8_t rev[2];
    488  1.13  riastrad 	int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
    489  1.13  riastrad 	bool branch_device = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
    490  1.13  riastrad 			     DP_DWN_STRM_PORT_PRESENT;
    491  1.13  riastrad 
    492  1.13  riastrad 	seq_printf(m, "\tDP branch device present: %s\n",
    493  1.13  riastrad 		   branch_device ? "yes" : "no");
    494  1.13  riastrad 
    495  1.13  riastrad 	if (!branch_device)
    496  1.13  riastrad 		return;
    497  1.13  riastrad 
    498  1.13  riastrad 	switch (type) {
    499  1.13  riastrad 	case DP_DS_PORT_TYPE_DP:
    500  1.13  riastrad 		seq_puts(m, "\t\tType: DisplayPort\n");
    501  1.13  riastrad 		break;
    502  1.13  riastrad 	case DP_DS_PORT_TYPE_VGA:
    503  1.13  riastrad 		seq_puts(m, "\t\tType: VGA\n");
    504  1.13  riastrad 		break;
    505  1.13  riastrad 	case DP_DS_PORT_TYPE_DVI:
    506  1.13  riastrad 		seq_puts(m, "\t\tType: DVI\n");
    507  1.13  riastrad 		break;
    508  1.13  riastrad 	case DP_DS_PORT_TYPE_HDMI:
    509  1.13  riastrad 		seq_puts(m, "\t\tType: HDMI\n");
    510  1.13  riastrad 		break;
    511  1.13  riastrad 	case DP_DS_PORT_TYPE_NON_EDID:
    512  1.13  riastrad 		seq_puts(m, "\t\tType: others without EDID support\n");
    513  1.13  riastrad 		break;
    514  1.13  riastrad 	case DP_DS_PORT_TYPE_DP_DUALMODE:
    515  1.13  riastrad 		seq_puts(m, "\t\tType: DP++\n");
    516  1.13  riastrad 		break;
    517  1.13  riastrad 	case DP_DS_PORT_TYPE_WIRELESS:
    518  1.13  riastrad 		seq_puts(m, "\t\tType: Wireless\n");
    519  1.13  riastrad 		break;
    520  1.13  riastrad 	default:
    521  1.13  riastrad 		seq_puts(m, "\t\tType: N/A\n");
    522  1.13  riastrad 	}
    523   1.3  riastrad 
    524  1.13  riastrad 	memset(id, 0, sizeof(id));
    525  1.13  riastrad 	drm_dp_downstream_id(aux, id);
    526  1.13  riastrad 	seq_printf(m, "\t\tID: %s\n", id);
    527  1.13  riastrad 
    528  1.13  riastrad 	len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
    529  1.13  riastrad 	if (len > 0)
    530  1.13  riastrad 		seq_printf(m, "\t\tHW: %d.%d\n",
    531  1.13  riastrad 			   (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
    532  1.13  riastrad 
    533  1.13  riastrad 	len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
    534  1.13  riastrad 	if (len > 0)
    535  1.13  riastrad 		seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
    536  1.13  riastrad 
    537  1.13  riastrad 	if (detailed_cap_info) {
    538  1.13  riastrad 		clk = drm_dp_downstream_max_clock(dpcd, port_cap);
    539  1.13  riastrad 
    540  1.13  riastrad 		if (clk > 0) {
    541  1.13  riastrad 			if (type == DP_DS_PORT_TYPE_VGA)
    542  1.13  riastrad 				seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
    543  1.13  riastrad 			else
    544  1.13  riastrad 				seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
    545  1.13  riastrad 		}
    546   1.3  riastrad 
    547  1.13  riastrad 		bpc = drm_dp_downstream_max_bpc(dpcd, port_cap);
    548   1.3  riastrad 
    549  1.13  riastrad 		if (bpc > 0)
    550  1.13  riastrad 			seq_printf(m, "\t\tMax bpc: %d\n", bpc);
    551  1.13  riastrad 	}
    552   1.3  riastrad }
    553  1.13  riastrad EXPORT_SYMBOL(drm_dp_downstream_debug);
    554  1.14  riastrad #endif
    555   1.3  riastrad 
    556   1.3  riastrad /*
    557   1.3  riastrad  * I2C-over-AUX implementation
    558   1.3  riastrad  */
    559   1.3  riastrad 
    560   1.3  riastrad static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
    561   1.3  riastrad {
    562   1.3  riastrad 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
    563   1.3  riastrad 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
    564   1.3  riastrad 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
    565   1.3  riastrad 	       I2C_FUNC_10BIT_ADDR;
    566   1.3  riastrad }
    567   1.3  riastrad 
    568   1.5  riastrad static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
    569   1.5  riastrad {
    570   1.5  riastrad 	/*
    571   1.5  riastrad 	 * In case of i2c defer or short i2c ack reply to a write,
    572   1.5  riastrad 	 * we need to switch to WRITE_STATUS_UPDATE to drain the
    573   1.5  riastrad 	 * rest of the message
    574   1.5  riastrad 	 */
    575   1.5  riastrad 	if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
    576   1.5  riastrad 		msg->request &= DP_AUX_I2C_MOT;
    577   1.5  riastrad 		msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
    578   1.5  riastrad 	}
    579   1.5  riastrad }
    580   1.5  riastrad 
    581   1.5  riastrad #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
    582   1.5  riastrad #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
    583   1.5  riastrad #define AUX_STOP_LEN 4
    584   1.5  riastrad #define AUX_CMD_LEN 4
    585   1.5  riastrad #define AUX_ADDRESS_LEN 20
    586   1.5  riastrad #define AUX_REPLY_PAD_LEN 4
    587   1.5  riastrad #define AUX_LENGTH_LEN 8
    588   1.5  riastrad 
    589   1.5  riastrad /*
    590   1.5  riastrad  * Calculate the duration of the AUX request/reply in usec. Gives the
    591   1.5  riastrad  * "best" case estimate, ie. successful while as short as possible.
    592   1.5  riastrad  */
    593   1.5  riastrad static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
    594   1.5  riastrad {
    595   1.5  riastrad 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
    596   1.5  riastrad 		AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
    597   1.5  riastrad 
    598   1.5  riastrad 	if ((msg->request & DP_AUX_I2C_READ) == 0)
    599   1.5  riastrad 		len += msg->size * 8;
    600   1.5  riastrad 
    601   1.5  riastrad 	return len;
    602   1.5  riastrad }
    603   1.5  riastrad 
    604   1.5  riastrad static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
    605   1.5  riastrad {
    606   1.5  riastrad 	int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
    607   1.5  riastrad 		AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
    608   1.5  riastrad 
    609   1.5  riastrad 	/*
    610   1.5  riastrad 	 * For read we expect what was asked. For writes there will
    611   1.5  riastrad 	 * be 0 or 1 data bytes. Assume 0 for the "best" case.
    612   1.5  riastrad 	 */
    613   1.5  riastrad 	if (msg->request & DP_AUX_I2C_READ)
    614   1.5  riastrad 		len += msg->size * 8;
    615   1.5  riastrad 
    616   1.5  riastrad 	return len;
    617   1.5  riastrad }
    618   1.5  riastrad 
    619   1.5  riastrad #define I2C_START_LEN 1
    620   1.5  riastrad #define I2C_STOP_LEN 1
    621   1.5  riastrad #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
    622   1.5  riastrad #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
    623   1.5  riastrad 
    624   1.5  riastrad /*
    625   1.5  riastrad  * Calculate the length of the i2c transfer in usec, assuming
    626   1.5  riastrad  * the i2c bus speed is as specified. Gives the the "worst"
    627   1.5  riastrad  * case estimate, ie. successful while as long as possible.
    628   1.5  riastrad  * Doesn't account the the "MOT" bit, and instead assumes each
    629   1.5  riastrad  * message includes a START, ADDRESS and STOP. Neither does it
    630   1.5  riastrad  * account for additional random variables such as clock stretching.
    631   1.5  riastrad  */
    632   1.5  riastrad static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
    633   1.5  riastrad 				   int i2c_speed_khz)
    634   1.5  riastrad {
    635   1.5  riastrad 	/* AUX bitrate is 1MHz, i2c bitrate as specified */
    636   1.5  riastrad 	return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
    637   1.5  riastrad 			     msg->size * I2C_DATA_LEN +
    638   1.5  riastrad 			     I2C_STOP_LEN) * 1000, i2c_speed_khz);
    639   1.5  riastrad }
    640   1.5  riastrad 
    641   1.5  riastrad /*
    642   1.5  riastrad  * Deterine how many retries should be attempted to successfully transfer
    643   1.5  riastrad  * the specified message, based on the estimated durations of the
    644   1.5  riastrad  * i2c and AUX transfers.
    645   1.5  riastrad  */
    646   1.5  riastrad static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
    647   1.5  riastrad 			      int i2c_speed_khz)
    648   1.5  riastrad {
    649   1.5  riastrad 	int aux_time_us = drm_dp_aux_req_duration(msg) +
    650   1.5  riastrad 		drm_dp_aux_reply_duration(msg);
    651   1.5  riastrad 	int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
    652   1.5  riastrad 
    653   1.5  riastrad 	return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
    654   1.5  riastrad }
    655   1.5  riastrad 
    656   1.5  riastrad /*
    657   1.5  riastrad  * FIXME currently assumes 10 kHz as some real world devices seem
    658   1.5  riastrad  * to require it. We should query/set the speed via DPCD if supported.
    659   1.5  riastrad  */
    660   1.5  riastrad static int dp_aux_i2c_speed_khz __read_mostly = 10;
    661   1.5  riastrad module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
    662   1.5  riastrad MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
    663   1.5  riastrad 		 "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
    664   1.5  riastrad 
    665   1.3  riastrad /*
    666   1.3  riastrad  * Transfer a single I2C-over-AUX message and handle various error conditions,
    667   1.3  riastrad  * retrying the transaction as appropriate.  It is assumed that the
    668  1.13  riastrad  * &drm_dp_aux.transfer function does not modify anything in the msg other than the
    669   1.3  riastrad  * reply field.
    670   1.5  riastrad  *
    671   1.5  riastrad  * Returns bytes transferred on success, or a negative error code on failure.
    672   1.3  riastrad  */
    673   1.3  riastrad static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
    674   1.3  riastrad {
    675   1.5  riastrad 	unsigned int retry, defer_i2c;
    676   1.5  riastrad 	int ret;
    677   1.3  riastrad 	/*
    678   1.3  riastrad 	 * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
    679   1.3  riastrad 	 * is required to retry at least seven times upon receiving AUX_DEFER
    680   1.3  riastrad 	 * before giving up the AUX transaction.
    681   1.5  riastrad 	 *
    682   1.5  riastrad 	 * We also try to account for the i2c bus speed.
    683   1.3  riastrad 	 */
    684   1.5  riastrad 	int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
    685   1.5  riastrad 
    686   1.5  riastrad 	for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
    687   1.5  riastrad 		ret = aux->transfer(aux, msg);
    688   1.5  riastrad 		if (ret < 0) {
    689   1.5  riastrad 			if (ret == -EBUSY)
    690   1.3  riastrad 				continue;
    691   1.3  riastrad 
    692  1.13  riastrad 			/*
    693  1.13  riastrad 			 * While timeouts can be errors, they're usually normal
    694  1.13  riastrad 			 * behavior (for instance, when a driver tries to
    695  1.13  riastrad 			 * communicate with a non-existant DisplayPort device).
    696  1.13  riastrad 			 * Avoid spamming the kernel log with timeout errors.
    697  1.13  riastrad 			 */
    698  1.13  riastrad 			if (ret == -ETIMEDOUT)
    699  1.13  riastrad 				DRM_DEBUG_KMS_RATELIMITED("transaction timed out\n");
    700  1.13  riastrad 			else
    701  1.13  riastrad 				DRM_DEBUG_KMS("transaction failed: %d\n", ret);
    702  1.13  riastrad 
    703   1.5  riastrad 			return ret;
    704   1.3  riastrad 		}
    705   1.3  riastrad 
    706   1.3  riastrad 
    707   1.3  riastrad 		switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
    708   1.3  riastrad 		case DP_AUX_NATIVE_REPLY_ACK:
    709   1.3  riastrad 			/*
    710   1.3  riastrad 			 * For I2C-over-AUX transactions this isn't enough, we
    711   1.3  riastrad 			 * need to check for the I2C ACK reply.
    712   1.3  riastrad 			 */
    713   1.3  riastrad 			break;
    714   1.3  riastrad 
    715   1.3  riastrad 		case DP_AUX_NATIVE_REPLY_NACK:
    716   1.5  riastrad 			DRM_DEBUG_KMS("native nack (result=%d, size=%zu)\n", ret, msg->size);
    717   1.3  riastrad 			return -EREMOTEIO;
    718   1.3  riastrad 
    719   1.3  riastrad 		case DP_AUX_NATIVE_REPLY_DEFER:
    720   1.5  riastrad 			DRM_DEBUG_KMS("native defer\n");
    721   1.3  riastrad 			/*
    722   1.3  riastrad 			 * We could check for I2C bit rate capabilities and if
    723   1.3  riastrad 			 * available adjust this interval. We could also be
    724   1.3  riastrad 			 * more careful with DP-to-legacy adapters where a
    725   1.3  riastrad 			 * long legacy cable may force very low I2C bit rates.
    726   1.3  riastrad 			 *
    727   1.3  riastrad 			 * For now just defer for long enough to hopefully be
    728   1.3  riastrad 			 * safe for all use-cases.
    729   1.3  riastrad 			 */
    730   1.5  riastrad 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
    731   1.3  riastrad 			continue;
    732   1.3  riastrad 
    733   1.3  riastrad 		default:
    734   1.3  riastrad 			DRM_ERROR("invalid native reply %#04x\n", msg->reply);
    735   1.3  riastrad 			return -EREMOTEIO;
    736   1.3  riastrad 		}
    737   1.3  riastrad 
    738   1.3  riastrad 		switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
    739   1.3  riastrad 		case DP_AUX_I2C_REPLY_ACK:
    740   1.3  riastrad 			/*
    741   1.3  riastrad 			 * Both native ACK and I2C ACK replies received. We
    742   1.3  riastrad 			 * can assume the transfer was successful.
    743   1.3  riastrad 			 */
    744   1.5  riastrad 			if (ret != msg->size)
    745   1.5  riastrad 				drm_dp_i2c_msg_write_status_update(msg);
    746   1.5  riastrad 			return ret;
    747   1.3  riastrad 
    748   1.3  riastrad 		case DP_AUX_I2C_REPLY_NACK:
    749  1.13  riastrad 			DRM_DEBUG_KMS("I2C nack (result=%d, size=%zu)\n",
    750  1.13  riastrad 				      ret, msg->size);
    751   1.5  riastrad 			aux->i2c_nack_count++;
    752   1.3  riastrad 			return -EREMOTEIO;
    753   1.3  riastrad 
    754   1.3  riastrad 		case DP_AUX_I2C_REPLY_DEFER:
    755   1.3  riastrad 			DRM_DEBUG_KMS("I2C defer\n");
    756   1.5  riastrad 			/* DP Compliance Test 4.2.2.5 Requirement:
    757   1.5  riastrad 			 * Must have at least 7 retries for I2C defers on the
    758   1.5  riastrad 			 * transaction to pass this test
    759   1.5  riastrad 			 */
    760   1.5  riastrad 			aux->i2c_defer_count++;
    761   1.5  riastrad 			if (defer_i2c < 7)
    762   1.5  riastrad 				defer_i2c++;
    763   1.5  riastrad 			usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
    764   1.5  riastrad 			drm_dp_i2c_msg_write_status_update(msg);
    765   1.5  riastrad 
    766   1.3  riastrad 			continue;
    767   1.3  riastrad 
    768   1.3  riastrad 		default:
    769   1.3  riastrad 			DRM_ERROR("invalid I2C reply %#04x\n", msg->reply);
    770   1.3  riastrad 			return -EREMOTEIO;
    771   1.3  riastrad 		}
    772   1.3  riastrad 	}
    773   1.3  riastrad 
    774   1.3  riastrad 	DRM_DEBUG_KMS("too many retries, giving up\n");
    775   1.3  riastrad 	return -EREMOTEIO;
    776   1.3  riastrad }
    777   1.3  riastrad 
    778   1.5  riastrad static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
    779   1.5  riastrad 				       const struct i2c_msg *i2c_msg)
    780   1.5  riastrad {
    781   1.5  riastrad 	msg->request = (i2c_msg->flags & I2C_M_RD) ?
    782   1.5  riastrad 		DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
    783  1.13  riastrad 	if (!(i2c_msg->flags & I2C_M_STOP))
    784  1.13  riastrad 		msg->request |= DP_AUX_I2C_MOT;
    785   1.5  riastrad }
    786   1.5  riastrad 
    787   1.5  riastrad /*
    788   1.5  riastrad  * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
    789   1.5  riastrad  *
    790   1.5  riastrad  * Returns an error code on failure, or a recommended transfer size on success.
    791   1.5  riastrad  */
    792   1.5  riastrad static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
    793   1.5  riastrad {
    794   1.5  riastrad 	int err, ret = orig_msg->size;
    795   1.5  riastrad 	struct drm_dp_aux_msg msg = *orig_msg;
    796   1.5  riastrad 
    797   1.5  riastrad 	while (msg.size > 0) {
    798   1.5  riastrad 		err = drm_dp_i2c_do_msg(aux, &msg);
    799   1.5  riastrad 		if (err <= 0)
    800   1.5  riastrad 			return err == 0 ? -EPROTO : err;
    801   1.5  riastrad 
    802   1.5  riastrad 		if (err < msg.size && err < ret) {
    803   1.5  riastrad 			DRM_DEBUG_KMS("Partial I2C reply: requested %zu bytes got %d bytes\n",
    804   1.5  riastrad 				      msg.size, err);
    805   1.5  riastrad 			ret = err;
    806   1.5  riastrad 		}
    807   1.5  riastrad 
    808   1.5  riastrad 		msg.size -= err;
    809  1.11  riastrad 		msg.buffer += err;
    810   1.5  riastrad 	}
    811   1.5  riastrad 
    812   1.5  riastrad 	return ret;
    813   1.5  riastrad }
    814   1.5  riastrad 
    815   1.5  riastrad /*
    816   1.5  riastrad  * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
    817   1.5  riastrad  * packets to be as large as possible. If not, the I2C transactions never
    818   1.5  riastrad  * succeed. Hence the default is maximum.
    819   1.5  riastrad  */
    820   1.5  riastrad static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
    821   1.5  riastrad module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
    822   1.5  riastrad MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
    823   1.5  riastrad 		 "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
    824   1.5  riastrad 
    825   1.3  riastrad static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
    826   1.3  riastrad 			   int num)
    827   1.3  riastrad {
    828   1.3  riastrad 	struct drm_dp_aux *aux = adapter->algo_data;
    829   1.3  riastrad 	unsigned int i, j;
    830   1.5  riastrad 	unsigned transfer_size;
    831   1.3  riastrad 	struct drm_dp_aux_msg msg;
    832   1.3  riastrad 	int err = 0;
    833   1.3  riastrad 
    834   1.5  riastrad 	dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
    835   1.5  riastrad 
    836   1.3  riastrad 	memset(&msg, 0, sizeof(msg));
    837   1.3  riastrad 
    838   1.3  riastrad 	for (i = 0; i < num; i++) {
    839   1.3  riastrad 		msg.address = msgs[i].addr;
    840   1.5  riastrad 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
    841   1.3  riastrad 		/* Send a bare address packet to start the transaction.
    842   1.3  riastrad 		 * Zero sized messages specify an address only (bare
    843   1.3  riastrad 		 * address) transaction.
    844   1.3  riastrad 		 */
    845   1.3  riastrad 		msg.buffer = NULL;
    846   1.3  riastrad 		msg.size = 0;
    847   1.3  riastrad 		err = drm_dp_i2c_do_msg(aux, &msg);
    848   1.5  riastrad 
    849   1.5  riastrad 		/*
    850   1.5  riastrad 		 * Reset msg.request in case in case it got
    851   1.5  riastrad 		 * changed into a WRITE_STATUS_UPDATE.
    852   1.5  riastrad 		 */
    853   1.5  riastrad 		drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
    854   1.5  riastrad 
    855   1.3  riastrad 		if (err < 0)
    856   1.3  riastrad 			break;
    857   1.5  riastrad 		/* We want each transaction to be as large as possible, but
    858   1.5  riastrad 		 * we'll go to smaller sizes if the hardware gives us a
    859   1.5  riastrad 		 * short reply.
    860   1.3  riastrad 		 */
    861   1.5  riastrad 		transfer_size = dp_aux_i2c_transfer_size;
    862   1.5  riastrad 		for (j = 0; j < msgs[i].len; j += msg.size) {
    863   1.3  riastrad 			msg.buffer = msgs[i].buf + j;
    864   1.5  riastrad 			msg.size = min(transfer_size, msgs[i].len - j);
    865   1.5  riastrad 
    866   1.5  riastrad 			err = drm_dp_i2c_drain_msg(aux, &msg);
    867   1.5  riastrad 
    868   1.5  riastrad 			/*
    869   1.5  riastrad 			 * Reset msg.request in case in case it got
    870   1.5  riastrad 			 * changed into a WRITE_STATUS_UPDATE.
    871   1.5  riastrad 			 */
    872   1.5  riastrad 			drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
    873   1.3  riastrad 
    874   1.3  riastrad 			if (err < 0)
    875   1.3  riastrad 				break;
    876   1.5  riastrad 			transfer_size = err;
    877   1.3  riastrad 		}
    878   1.3  riastrad 		if (err < 0)
    879   1.3  riastrad 			break;
    880   1.3  riastrad 	}
    881   1.3  riastrad 	if (err >= 0)
    882   1.3  riastrad 		err = num;
    883   1.3  riastrad 	/* Send a bare address packet to close out the transaction.
    884   1.3  riastrad 	 * Zero sized messages specify an address only (bare
    885   1.3  riastrad 	 * address) transaction.
    886   1.3  riastrad 	 */
    887   1.3  riastrad 	msg.request &= ~DP_AUX_I2C_MOT;
    888   1.3  riastrad 	msg.buffer = NULL;
    889   1.3  riastrad 	msg.size = 0;
    890   1.3  riastrad 	(void)drm_dp_i2c_do_msg(aux, &msg);
    891   1.3  riastrad 
    892   1.3  riastrad 	return err;
    893   1.3  riastrad }
    894   1.3  riastrad 
    895   1.3  riastrad static const struct i2c_algorithm drm_dp_i2c_algo = {
    896   1.3  riastrad 	.functionality = drm_dp_i2c_functionality,
    897   1.3  riastrad 	.master_xfer = drm_dp_i2c_xfer,
    898   1.3  riastrad };
    899   1.3  riastrad 
    900  1.13  riastrad static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
    901  1.13  riastrad {
    902  1.13  riastrad 	return container_of(i2c, struct drm_dp_aux, ddc);
    903  1.13  riastrad }
    904  1.13  riastrad 
    905  1.13  riastrad static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
    906  1.13  riastrad {
    907  1.13  riastrad 	mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
    908  1.13  riastrad }
    909  1.13  riastrad 
    910  1.13  riastrad static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
    911  1.13  riastrad {
    912  1.13  riastrad 	return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
    913  1.13  riastrad }
    914  1.13  riastrad 
    915  1.13  riastrad static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
    916  1.13  riastrad {
    917  1.13  riastrad 	mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
    918  1.13  riastrad }
    919  1.13  riastrad 
    920  1.13  riastrad static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
    921  1.13  riastrad 	.lock_bus = lock_bus,
    922  1.13  riastrad 	.trylock_bus = trylock_bus,
    923  1.13  riastrad 	.unlock_bus = unlock_bus,
    924  1.13  riastrad };
    925  1.13  riastrad 
    926  1.13  riastrad static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
    927  1.13  riastrad {
    928  1.13  riastrad 	u8 buf, count;
    929  1.13  riastrad 	int ret;
    930  1.13  riastrad 
    931  1.13  riastrad 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
    932  1.13  riastrad 	if (ret < 0)
    933  1.13  riastrad 		return ret;
    934  1.13  riastrad 
    935  1.13  riastrad 	WARN_ON(!(buf & DP_TEST_SINK_START));
    936  1.13  riastrad 
    937  1.13  riastrad 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
    938  1.13  riastrad 	if (ret < 0)
    939  1.13  riastrad 		return ret;
    940  1.13  riastrad 
    941  1.13  riastrad 	count = buf & DP_TEST_COUNT_MASK;
    942  1.13  riastrad 	if (count == aux->crc_count)
    943  1.13  riastrad 		return -EAGAIN; /* No CRC yet */
    944  1.13  riastrad 
    945  1.13  riastrad 	aux->crc_count = count;
    946  1.13  riastrad 
    947  1.13  riastrad 	/*
    948  1.13  riastrad 	 * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
    949  1.13  riastrad 	 * per component (RGB or CrYCb).
    950  1.13  riastrad 	 */
    951  1.13  riastrad 	ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
    952  1.13  riastrad 	if (ret < 0)
    953  1.13  riastrad 		return ret;
    954  1.13  riastrad 
    955  1.13  riastrad 	return 0;
    956  1.13  riastrad }
    957  1.13  riastrad 
    958  1.13  riastrad static void drm_dp_aux_crc_work(struct work_struct *work)
    959  1.13  riastrad {
    960  1.13  riastrad 	struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
    961  1.13  riastrad 					      crc_work);
    962  1.13  riastrad 	struct drm_crtc *crtc;
    963  1.13  riastrad 	u8 crc_bytes[6];
    964  1.13  riastrad 	uint32_t crcs[3];
    965  1.13  riastrad 	int ret;
    966  1.13  riastrad 
    967  1.13  riastrad 	if (WARN_ON(!aux->crtc))
    968  1.13  riastrad 		return;
    969  1.13  riastrad 
    970  1.13  riastrad 	crtc = aux->crtc;
    971  1.13  riastrad 	while (crtc->crc.opened) {
    972  1.13  riastrad 		drm_crtc_wait_one_vblank(crtc);
    973  1.13  riastrad 		if (!crtc->crc.opened)
    974  1.13  riastrad 			break;
    975  1.13  riastrad 
    976  1.13  riastrad 		ret = drm_dp_aux_get_crc(aux, crc_bytes);
    977  1.13  riastrad 		if (ret == -EAGAIN) {
    978  1.13  riastrad 			usleep_range(1000, 2000);
    979  1.13  riastrad 			ret = drm_dp_aux_get_crc(aux, crc_bytes);
    980  1.13  riastrad 		}
    981  1.13  riastrad 
    982  1.13  riastrad 		if (ret == -EAGAIN) {
    983  1.13  riastrad 			DRM_DEBUG_KMS("Get CRC failed after retrying: %d\n",
    984  1.13  riastrad 				      ret);
    985  1.13  riastrad 			continue;
    986  1.13  riastrad 		} else if (ret) {
    987  1.13  riastrad 			DRM_DEBUG_KMS("Failed to get a CRC: %d\n", ret);
    988  1.13  riastrad 			continue;
    989  1.13  riastrad 		}
    990  1.13  riastrad 
    991  1.13  riastrad 		crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
    992  1.13  riastrad 		crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
    993  1.13  riastrad 		crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
    994  1.13  riastrad 		drm_crtc_add_crc_entry(crtc, false, 0, crcs);
    995  1.13  riastrad 	}
    996  1.13  riastrad }
    997  1.13  riastrad 
    998   1.3  riastrad /**
    999  1.13  riastrad  * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
   1000  1.13  riastrad  * @aux: DisplayPort AUX channel
   1001  1.13  riastrad  *
   1002  1.13  riastrad  * Used for remote aux channel in general. Merely initialize the crc work
   1003  1.13  riastrad  * struct.
   1004  1.13  riastrad  */
   1005  1.13  riastrad void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
   1006  1.13  riastrad {
   1007  1.13  riastrad 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
   1008  1.13  riastrad }
   1009  1.13  riastrad EXPORT_SYMBOL(drm_dp_remote_aux_init);
   1010  1.13  riastrad 
   1011  1.13  riastrad /**
   1012  1.13  riastrad  * drm_dp_aux_init() - minimally initialise an aux channel
   1013   1.3  riastrad  * @aux: DisplayPort AUX channel
   1014   1.3  riastrad  *
   1015  1.13  riastrad  * If you need to use the drm_dp_aux's i2c adapter prior to registering it
   1016  1.13  riastrad  * with the outside world, call drm_dp_aux_init() first. You must still
   1017  1.13  riastrad  * call drm_dp_aux_register() once the connector has been registered to
   1018  1.13  riastrad  * allow userspace access to the auxiliary DP channel.
   1019   1.3  riastrad  */
   1020  1.13  riastrad void drm_dp_aux_init(struct drm_dp_aux *aux)
   1021   1.3  riastrad {
   1022   1.5  riastrad 	mutex_init(&aux->hw_mutex);
   1023  1.13  riastrad 	mutex_init(&aux->cec.lock);
   1024  1.13  riastrad 	INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
   1025   1.5  riastrad 
   1026   1.3  riastrad 	aux->ddc.algo = &drm_dp_i2c_algo;
   1027   1.3  riastrad 	aux->ddc.algo_data = aux;
   1028   1.3  riastrad 	aux->ddc.retries = 3;
   1029   1.3  riastrad 
   1030  1.13  riastrad 	aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
   1031  1.13  riastrad }
   1032  1.13  riastrad EXPORT_SYMBOL(drm_dp_aux_init);
   1033  1.13  riastrad 
   1034  1.13  riastrad /**
   1035  1.16  riastrad  * drm_dp_aux_fini() - undo what drm_dp_aux_init() does.
   1036  1.16  riastrad  * @aux: DisplayPort AUX channel
   1037  1.16  riastrad  */
   1038  1.16  riastrad void drm_dp_aux_fini(struct drm_dp_aux *aux)
   1039  1.16  riastrad {
   1040  1.16  riastrad 	mutex_destroy(&aux->cec.lock);
   1041  1.16  riastrad 	mutex_destroy(&aux->hw_mutex);
   1042  1.16  riastrad }
   1043  1.16  riastrad EXPORT_SYMBOL(drm_dp_aux_fini);
   1044  1.16  riastrad 
   1045  1.16  riastrad /**
   1046  1.13  riastrad  * drm_dp_aux_register() - initialise and register aux channel
   1047  1.13  riastrad  * @aux: DisplayPort AUX channel
   1048  1.13  riastrad  *
   1049  1.13  riastrad  * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
   1050  1.13  riastrad  * This should only be called when the underlying &struct drm_connector is
   1051  1.13  riastrad  * initialiazed already. Therefore the best place to call this is from
   1052  1.13  riastrad  * &drm_connector_funcs.late_register. Not that drivers which don't follow this
   1053  1.13  riastrad  * will Oops when CONFIG_DRM_DP_AUX_CHARDEV is enabled.
   1054  1.13  riastrad  *
   1055  1.13  riastrad  * Drivers which need to use the aux channel before that point (e.g. at driver
   1056  1.13  riastrad  * load time, before drm_dev_register() has been called) need to call
   1057  1.13  riastrad  * drm_dp_aux_init().
   1058  1.13  riastrad  *
   1059  1.13  riastrad  * Returns 0 on success or a negative error code on failure.
   1060  1.13  riastrad  */
   1061  1.13  riastrad int drm_dp_aux_register(struct drm_dp_aux *aux)
   1062  1.13  riastrad {
   1063  1.13  riastrad 	int ret;
   1064  1.13  riastrad 
   1065  1.13  riastrad 	if (!aux->ddc.algo)
   1066  1.13  riastrad 		drm_dp_aux_init(aux);
   1067  1.13  riastrad 
   1068   1.3  riastrad 	aux->ddc.class = I2C_CLASS_DDC;
   1069   1.3  riastrad 	aux->ddc.owner = THIS_MODULE;
   1070   1.3  riastrad 	aux->ddc.dev.parent = aux->dev;
   1071   1.3  riastrad 
   1072   1.3  riastrad 	strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
   1073   1.3  riastrad 		sizeof(aux->ddc.name));
   1074   1.3  riastrad 
   1075  1.13  riastrad 	ret = drm_dp_aux_register_devnode(aux);
   1076  1.13  riastrad 	if (ret)
   1077  1.13  riastrad 		return ret;
   1078  1.13  riastrad 
   1079  1.13  riastrad 	ret = i2c_add_adapter(&aux->ddc);
   1080  1.13  riastrad 	if (ret) {
   1081  1.13  riastrad 		drm_dp_aux_unregister_devnode(aux);
   1082  1.13  riastrad 		return ret;
   1083  1.13  riastrad 	}
   1084  1.13  riastrad 
   1085  1.13  riastrad 	return 0;
   1086   1.3  riastrad }
   1087   1.5  riastrad EXPORT_SYMBOL(drm_dp_aux_register);
   1088   1.3  riastrad 
   1089   1.3  riastrad /**
   1090   1.5  riastrad  * drm_dp_aux_unregister() - unregister an AUX adapter
   1091   1.3  riastrad  * @aux: DisplayPort AUX channel
   1092   1.3  riastrad  */
   1093   1.5  riastrad void drm_dp_aux_unregister(struct drm_dp_aux *aux)
   1094   1.3  riastrad {
   1095  1.13  riastrad 	drm_dp_aux_unregister_devnode(aux);
   1096   1.3  riastrad 	i2c_del_adapter(&aux->ddc);
   1097  1.16  riastrad 	drm_dp_aux_fini(aux);
   1098   1.3  riastrad }
   1099   1.5  riastrad EXPORT_SYMBOL(drm_dp_aux_unregister);
   1100  1.13  riastrad 
   1101  1.13  riastrad #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
   1102  1.13  riastrad 
   1103  1.13  riastrad /**
   1104  1.13  riastrad  * drm_dp_psr_setup_time() - PSR setup in time usec
   1105  1.13  riastrad  * @psr_cap: PSR capabilities from DPCD
   1106  1.13  riastrad  *
   1107  1.13  riastrad  * Returns:
   1108  1.13  riastrad  * PSR setup time for the panel in microseconds,  negative
   1109  1.13  riastrad  * error code on failure.
   1110  1.13  riastrad  */
   1111  1.13  riastrad int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
   1112  1.13  riastrad {
   1113  1.13  riastrad 	static const u16 psr_setup_time_us[] = {
   1114  1.13  riastrad 		PSR_SETUP_TIME(330),
   1115  1.13  riastrad 		PSR_SETUP_TIME(275),
   1116  1.13  riastrad 		PSR_SETUP_TIME(220),
   1117  1.13  riastrad 		PSR_SETUP_TIME(165),
   1118  1.13  riastrad 		PSR_SETUP_TIME(110),
   1119  1.13  riastrad 		PSR_SETUP_TIME(55),
   1120  1.13  riastrad 		PSR_SETUP_TIME(0),
   1121  1.13  riastrad 	};
   1122  1.13  riastrad 	int i;
   1123  1.13  riastrad 
   1124  1.13  riastrad 	i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
   1125  1.13  riastrad 	if (i >= ARRAY_SIZE(psr_setup_time_us))
   1126  1.13  riastrad 		return -EINVAL;
   1127  1.13  riastrad 
   1128  1.13  riastrad 	return psr_setup_time_us[i];
   1129  1.13  riastrad }
   1130  1.13  riastrad EXPORT_SYMBOL(drm_dp_psr_setup_time);
   1131  1.13  riastrad 
   1132  1.13  riastrad #undef PSR_SETUP_TIME
   1133  1.13  riastrad 
   1134  1.13  riastrad /**
   1135  1.13  riastrad  * drm_dp_start_crc() - start capture of frame CRCs
   1136  1.13  riastrad  * @aux: DisplayPort AUX channel
   1137  1.13  riastrad  * @crtc: CRTC displaying the frames whose CRCs are to be captured
   1138  1.13  riastrad  *
   1139  1.13  riastrad  * Returns 0 on success or a negative error code on failure.
   1140  1.13  riastrad  */
   1141  1.13  riastrad int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
   1142  1.13  riastrad {
   1143  1.13  riastrad 	u8 buf;
   1144  1.13  riastrad 	int ret;
   1145  1.13  riastrad 
   1146  1.13  riastrad 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
   1147  1.13  riastrad 	if (ret < 0)
   1148  1.13  riastrad 		return ret;
   1149  1.13  riastrad 
   1150  1.13  riastrad 	ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
   1151  1.13  riastrad 	if (ret < 0)
   1152  1.13  riastrad 		return ret;
   1153  1.13  riastrad 
   1154  1.13  riastrad 	aux->crc_count = 0;
   1155  1.13  riastrad 	aux->crtc = crtc;
   1156  1.13  riastrad 	schedule_work(&aux->crc_work);
   1157  1.13  riastrad 
   1158  1.13  riastrad 	return 0;
   1159  1.13  riastrad }
   1160  1.13  riastrad EXPORT_SYMBOL(drm_dp_start_crc);
   1161  1.13  riastrad 
   1162  1.13  riastrad /**
   1163  1.13  riastrad  * drm_dp_stop_crc() - stop capture of frame CRCs
   1164  1.13  riastrad  * @aux: DisplayPort AUX channel
   1165  1.13  riastrad  *
   1166  1.13  riastrad  * Returns 0 on success or a negative error code on failure.
   1167  1.13  riastrad  */
   1168  1.13  riastrad int drm_dp_stop_crc(struct drm_dp_aux *aux)
   1169  1.13  riastrad {
   1170  1.13  riastrad 	u8 buf;
   1171  1.13  riastrad 	int ret;
   1172  1.13  riastrad 
   1173  1.13  riastrad 	ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
   1174  1.13  riastrad 	if (ret < 0)
   1175  1.13  riastrad 		return ret;
   1176  1.13  riastrad 
   1177  1.13  riastrad 	ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
   1178  1.13  riastrad 	if (ret < 0)
   1179  1.13  riastrad 		return ret;
   1180  1.13  riastrad 
   1181  1.13  riastrad 	flush_work(&aux->crc_work);
   1182  1.13  riastrad 	aux->crtc = NULL;
   1183  1.13  riastrad 
   1184  1.13  riastrad 	return 0;
   1185  1.13  riastrad }
   1186  1.13  riastrad EXPORT_SYMBOL(drm_dp_stop_crc);
   1187  1.13  riastrad 
   1188  1.13  riastrad struct dpcd_quirk {
   1189  1.13  riastrad 	u8 oui[3];
   1190  1.13  riastrad 	u8 device_id[6];
   1191  1.13  riastrad 	bool is_branch;
   1192  1.13  riastrad 	u32 quirks;
   1193  1.13  riastrad };
   1194  1.13  riastrad 
   1195  1.13  riastrad #define OUI(first, second, third) { (first), (second), (third) }
   1196  1.13  riastrad #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
   1197  1.13  riastrad 	{ (first), (second), (third), (fourth), (fifth), (sixth) }
   1198  1.13  riastrad 
   1199  1.13  riastrad #define DEVICE_ID_ANY	DEVICE_ID(0, 0, 0, 0, 0, 0)
   1200  1.13  riastrad 
   1201  1.13  riastrad static const struct dpcd_quirk dpcd_quirk_list[] = {
   1202  1.13  riastrad 	/* Analogix 7737 needs reduced M and N at HBR2 link rates */
   1203  1.13  riastrad 	{ OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
   1204  1.13  riastrad 	/* LG LP140WF6-SPM1 eDP panel */
   1205  1.13  riastrad 	{ OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
   1206  1.13  riastrad 	/* Apple panels need some additional handling to support PSR */
   1207  1.13  riastrad 	{ OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
   1208  1.13  riastrad 	/* CH7511 seems to leave SINK_COUNT zeroed */
   1209  1.13  riastrad 	{ OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
   1210  1.13  riastrad 	/* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
   1211  1.13  riastrad 	{ OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
   1212  1.13  riastrad };
   1213  1.13  riastrad 
   1214  1.13  riastrad #undef OUI
   1215  1.13  riastrad 
   1216  1.13  riastrad /*
   1217  1.13  riastrad  * Get a bit mask of DPCD quirks for the sink/branch device identified by
   1218  1.13  riastrad  * ident. The quirk data is shared but it's up to the drivers to act on the
   1219  1.13  riastrad  * data.
   1220  1.13  riastrad  *
   1221  1.13  riastrad  * For now, only the OUI (first three bytes) is used, but this may be extended
   1222  1.13  riastrad  * to device identification string and hardware/firmware revisions later.
   1223  1.13  riastrad  */
   1224  1.13  riastrad static u32
   1225  1.13  riastrad drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
   1226  1.13  riastrad {
   1227  1.13  riastrad 	const struct dpcd_quirk *quirk;
   1228  1.13  riastrad 	u32 quirks = 0;
   1229  1.13  riastrad 	int i;
   1230  1.13  riastrad 	u8 any_device[] = DEVICE_ID_ANY;
   1231  1.13  riastrad 
   1232  1.13  riastrad 	for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
   1233  1.13  riastrad 		quirk = &dpcd_quirk_list[i];
   1234  1.13  riastrad 
   1235  1.13  riastrad 		if (quirk->is_branch != is_branch)
   1236  1.13  riastrad 			continue;
   1237  1.13  riastrad 
   1238  1.13  riastrad 		if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
   1239  1.13  riastrad 			continue;
   1240  1.13  riastrad 
   1241  1.13  riastrad 		if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
   1242  1.13  riastrad 		    memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
   1243  1.13  riastrad 			continue;
   1244  1.13  riastrad 
   1245  1.13  riastrad 		quirks |= quirk->quirks;
   1246  1.13  riastrad 	}
   1247  1.13  riastrad 
   1248  1.13  riastrad 	return quirks;
   1249  1.13  riastrad }
   1250  1.13  riastrad 
   1251  1.13  riastrad #undef DEVICE_ID_ANY
   1252  1.13  riastrad #undef DEVICE_ID
   1253  1.13  riastrad 
   1254  1.13  riastrad /**
   1255  1.13  riastrad  * drm_dp_read_desc - read sink/branch descriptor from DPCD
   1256  1.13  riastrad  * @aux: DisplayPort AUX channel
   1257  1.13  riastrad  * @desc: Device decriptor to fill from DPCD
   1258  1.13  riastrad  * @is_branch: true for branch devices, false for sink devices
   1259  1.13  riastrad  *
   1260  1.13  riastrad  * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
   1261  1.13  riastrad  * identification.
   1262  1.13  riastrad  *
   1263  1.13  riastrad  * Returns 0 on success or a negative error code on failure.
   1264  1.13  riastrad  */
   1265  1.13  riastrad int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
   1266  1.13  riastrad 		     bool is_branch)
   1267  1.13  riastrad {
   1268  1.13  riastrad 	struct drm_dp_dpcd_ident *ident = &desc->ident;
   1269  1.13  riastrad 	unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
   1270  1.13  riastrad 	int ret, dev_id_len;
   1271  1.13  riastrad 
   1272  1.13  riastrad 	ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
   1273  1.13  riastrad 	if (ret < 0)
   1274  1.13  riastrad 		return ret;
   1275  1.13  riastrad 
   1276  1.13  riastrad 	desc->quirks = drm_dp_get_quirks(ident, is_branch);
   1277  1.13  riastrad 
   1278  1.13  riastrad 	dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
   1279  1.13  riastrad 
   1280  1.13  riastrad 	DRM_DEBUG_KMS("DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
   1281  1.13  riastrad 		      is_branch ? "branch" : "sink",
   1282  1.13  riastrad 		      (int)sizeof(ident->oui), ident->oui,
   1283  1.13  riastrad 		      dev_id_len, ident->device_id,
   1284  1.13  riastrad 		      ident->hw_rev >> 4, ident->hw_rev & 0xf,
   1285  1.13  riastrad 		      ident->sw_major_rev, ident->sw_minor_rev,
   1286  1.13  riastrad 		      desc->quirks);
   1287  1.13  riastrad 
   1288  1.13  riastrad 	return 0;
   1289  1.13  riastrad }
   1290  1.13  riastrad EXPORT_SYMBOL(drm_dp_read_desc);
   1291  1.13  riastrad 
   1292  1.13  riastrad /**
   1293  1.13  riastrad  * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
   1294  1.13  riastrad  * supported by the DSC sink.
   1295  1.13  riastrad  * @dsc_dpcd: DSC capabilities from DPCD
   1296  1.13  riastrad  * @is_edp: true if its eDP, false for DP
   1297  1.13  riastrad  *
   1298  1.13  riastrad  * Read the slice capabilities DPCD register from DSC sink to get
   1299  1.13  riastrad  * the maximum slice count supported. This is used to populate
   1300  1.13  riastrad  * the DSC parameters in the &struct drm_dsc_config by the driver.
   1301  1.13  riastrad  * Driver creates an infoframe using these parameters to populate
   1302  1.13  riastrad  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
   1303  1.13  riastrad  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
   1304  1.13  riastrad  *
   1305  1.13  riastrad  * Returns:
   1306  1.13  riastrad  * Maximum slice count supported by DSC sink or 0 its invalid
   1307  1.13  riastrad  */
   1308  1.13  riastrad u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
   1309  1.13  riastrad 				   bool is_edp)
   1310  1.13  riastrad {
   1311  1.13  riastrad 	u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
   1312  1.13  riastrad 
   1313  1.13  riastrad 	if (is_edp) {
   1314  1.13  riastrad 		/* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
   1315  1.13  riastrad 		if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
   1316  1.13  riastrad 			return 4;
   1317  1.13  riastrad 		if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
   1318  1.13  riastrad 			return 2;
   1319  1.13  riastrad 		if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
   1320  1.13  riastrad 			return 1;
   1321  1.13  riastrad 	} else {
   1322  1.13  riastrad 		/* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
   1323  1.13  riastrad 		u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
   1324  1.13  riastrad 
   1325  1.13  riastrad 		if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
   1326  1.13  riastrad 			return 24;
   1327  1.13  riastrad 		if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
   1328  1.13  riastrad 			return 20;
   1329  1.13  riastrad 		if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
   1330  1.13  riastrad 			return 16;
   1331  1.13  riastrad 		if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
   1332  1.13  riastrad 			return 12;
   1333  1.13  riastrad 		if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
   1334  1.13  riastrad 			return 10;
   1335  1.13  riastrad 		if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
   1336  1.13  riastrad 			return 8;
   1337  1.13  riastrad 		if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
   1338  1.13  riastrad 			return 6;
   1339  1.13  riastrad 		if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
   1340  1.13  riastrad 			return 4;
   1341  1.13  riastrad 		if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
   1342  1.13  riastrad 			return 2;
   1343  1.13  riastrad 		if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
   1344  1.13  riastrad 			return 1;
   1345  1.13  riastrad 	}
   1346  1.13  riastrad 
   1347  1.13  riastrad 	return 0;
   1348  1.13  riastrad }
   1349  1.13  riastrad EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
   1350  1.13  riastrad 
   1351  1.13  riastrad /**
   1352  1.13  riastrad  * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
   1353  1.13  riastrad  * @dsc_dpcd: DSC capabilities from DPCD
   1354  1.13  riastrad  *
   1355  1.13  riastrad  * Read the DSC DPCD register to parse the line buffer depth in bits which is
   1356  1.13  riastrad  * number of bits of precision within the decoder line buffer supported by
   1357  1.13  riastrad  * the DSC sink. This is used to populate the DSC parameters in the
   1358  1.13  riastrad  * &struct drm_dsc_config by the driver.
   1359  1.13  riastrad  * Driver creates an infoframe using these parameters to populate
   1360  1.13  riastrad  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
   1361  1.13  riastrad  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
   1362  1.13  riastrad  *
   1363  1.13  riastrad  * Returns:
   1364  1.13  riastrad  * Line buffer depth supported by DSC panel or 0 its invalid
   1365  1.13  riastrad  */
   1366  1.13  riastrad u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
   1367  1.13  riastrad {
   1368  1.13  riastrad 	u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
   1369  1.13  riastrad 
   1370  1.13  riastrad 	switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
   1371  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_9:
   1372  1.13  riastrad 		return 9;
   1373  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_10:
   1374  1.13  riastrad 		return 10;
   1375  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_11:
   1376  1.13  riastrad 		return 11;
   1377  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_12:
   1378  1.13  riastrad 		return 12;
   1379  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_13:
   1380  1.13  riastrad 		return 13;
   1381  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_14:
   1382  1.13  riastrad 		return 14;
   1383  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_15:
   1384  1.13  riastrad 		return 15;
   1385  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_16:
   1386  1.13  riastrad 		return 16;
   1387  1.13  riastrad 	case DP_DSC_LINE_BUF_BIT_DEPTH_8:
   1388  1.13  riastrad 		return 8;
   1389  1.13  riastrad 	}
   1390  1.13  riastrad 
   1391  1.13  riastrad 	return 0;
   1392  1.13  riastrad }
   1393  1.13  riastrad EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
   1394  1.13  riastrad 
   1395  1.13  riastrad /**
   1396  1.13  riastrad  * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
   1397  1.13  riastrad  * values supported by the DSC sink.
   1398  1.13  riastrad  * @dsc_dpcd: DSC capabilities from DPCD
   1399  1.13  riastrad  * @dsc_bpc: An array to be filled by this helper with supported
   1400  1.13  riastrad  *           input bpcs.
   1401  1.13  riastrad  *
   1402  1.13  riastrad  * Read the DSC DPCD from the sink device to parse the supported bits per
   1403  1.13  riastrad  * component values. This is used to populate the DSC parameters
   1404  1.13  riastrad  * in the &struct drm_dsc_config by the driver.
   1405  1.13  riastrad  * Driver creates an infoframe using these parameters to populate
   1406  1.13  riastrad  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
   1407  1.13  riastrad  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
   1408  1.13  riastrad  *
   1409  1.13  riastrad  * Returns:
   1410  1.13  riastrad  * Number of input BPC values parsed from the DPCD
   1411  1.13  riastrad  */
   1412  1.13  riastrad int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
   1413  1.13  riastrad 					 u8 dsc_bpc[3])
   1414  1.13  riastrad {
   1415  1.13  riastrad 	int num_bpc = 0;
   1416  1.13  riastrad 	u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
   1417  1.13  riastrad 
   1418  1.13  riastrad 	if (color_depth & DP_DSC_12_BPC)
   1419  1.13  riastrad 		dsc_bpc[num_bpc++] = 12;
   1420  1.13  riastrad 	if (color_depth & DP_DSC_10_BPC)
   1421  1.13  riastrad 		dsc_bpc[num_bpc++] = 10;
   1422  1.13  riastrad 	if (color_depth & DP_DSC_8_BPC)
   1423  1.13  riastrad 		dsc_bpc[num_bpc++] = 8;
   1424  1.13  riastrad 
   1425  1.13  riastrad 	return num_bpc;
   1426  1.13  riastrad }
   1427  1.13  riastrad EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
   1428