dma-mapping: fix 32-bit overflow with CONFIG_ARM_LPAE=n
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / drm_dp_helper.c
1 /*
2  * Copyright © 2009 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/seq_file.h>
31
32 #include <drm/drm_dp_helper.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_vblank.h>
35 #include <drm/drm_dp_mst_helper.h>
36
37 #include "drm_crtc_helper_internal.h"
38
39 /**
40  * DOC: dp helpers
41  *
42  * These functions contain some common logic and helpers at various abstraction
43  * levels to deal with Display Port sink devices and related things like DP aux
44  * channel transfers, EDID reading over DP aux channels, decoding certain DPCD
45  * blocks, ...
46  */
47
48 /* Helpers for DP link training */
49 static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)
50 {
51         return link_status[r - DP_LANE0_1_STATUS];
52 }
53
54 static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],
55                              int lane)
56 {
57         int i = DP_LANE0_1_STATUS + (lane >> 1);
58         int s = (lane & 1) * 4;
59         u8 l = dp_link_status(link_status, i);
60
61         return (l >> s) & 0xf;
62 }
63
64 bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
65                           int lane_count)
66 {
67         u8 lane_align;
68         u8 lane_status;
69         int lane;
70
71         lane_align = dp_link_status(link_status,
72                                     DP_LANE_ALIGN_STATUS_UPDATED);
73         if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
74                 return false;
75         for (lane = 0; lane < lane_count; lane++) {
76                 lane_status = dp_get_lane_status(link_status, lane);
77                 if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
78                         return false;
79         }
80         return true;
81 }
82 EXPORT_SYMBOL(drm_dp_channel_eq_ok);
83
84 bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],
85                               int lane_count)
86 {
87         int lane;
88         u8 lane_status;
89
90         for (lane = 0; lane < lane_count; lane++) {
91                 lane_status = dp_get_lane_status(link_status, lane);
92                 if ((lane_status & DP_LANE_CR_DONE) == 0)
93                         return false;
94         }
95         return true;
96 }
97 EXPORT_SYMBOL(drm_dp_clock_recovery_ok);
98
99 u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],
100                                      int lane)
101 {
102         int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
103         int s = ((lane & 1) ?
104                  DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
105                  DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
106         u8 l = dp_link_status(link_status, i);
107
108         return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
109 }
110 EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);
111
112 u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],
113                                           int lane)
114 {
115         int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
116         int s = ((lane & 1) ?
117                  DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
118                  DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
119         u8 l = dp_link_status(link_status, i);
120
121         return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
122 }
123 EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);
124
125 u8 drm_dp_get_adjust_request_post_cursor(const u8 link_status[DP_LINK_STATUS_SIZE],
126                                          unsigned int lane)
127 {
128         unsigned int offset = DP_ADJUST_REQUEST_POST_CURSOR2;
129         u8 value = dp_link_status(link_status, offset);
130
131         return (value >> (lane << 1)) & 0x3;
132 }
133 EXPORT_SYMBOL(drm_dp_get_adjust_request_post_cursor);
134
135 void drm_dp_link_train_clock_recovery_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
136 {
137         unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
138                                          DP_TRAINING_AUX_RD_MASK;
139
140         if (rd_interval > 4)
141                 DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
142                               rd_interval);
143
144         if (rd_interval == 0 || dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
145                 rd_interval = 100;
146         else
147                 rd_interval *= 4 * USEC_PER_MSEC;
148
149         usleep_range(rd_interval, rd_interval * 2);
150 }
151 EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);
152
153 void drm_dp_link_train_channel_eq_delay(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
154 {
155         unsigned long rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
156                                          DP_TRAINING_AUX_RD_MASK;
157
158         if (rd_interval > 4)
159                 DRM_DEBUG_KMS("AUX interval %lu, out of range (max 4)\n",
160                               rd_interval);
161
162         if (rd_interval == 0)
163                 rd_interval = 400;
164         else
165                 rd_interval *= 4 * USEC_PER_MSEC;
166
167         usleep_range(rd_interval, rd_interval * 2);
168 }
169 EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);
170
171 u8 drm_dp_link_rate_to_bw_code(int link_rate)
172 {
173         /* Spec says link_bw = link_rate / 0.27Gbps */
174         return link_rate / 27000;
175 }
176 EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);
177
178 int drm_dp_bw_code_to_link_rate(u8 link_bw)
179 {
180         /* Spec says link_rate = link_bw * 0.27Gbps */
181         return link_bw * 27000;
182 }
183 EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);
184
185 #define AUX_RETRY_INTERVAL 500 /* us */
186
187 static inline void
188 drm_dp_dump_access(const struct drm_dp_aux *aux,
189                    u8 request, uint offset, void *buffer, int ret)
190 {
191         const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";
192
193         if (ret > 0)
194                 DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d) %*ph\n",
195                              aux->name, offset, arrow, ret, min(ret, 20), buffer);
196         else
197                 DRM_DEBUG_DP("%s: 0x%05x AUX %s (ret=%3d)\n",
198                              aux->name, offset, arrow, ret);
199 }
200
201 /**
202  * DOC: dp helpers
203  *
204  * The DisplayPort AUX channel is an abstraction to allow generic, driver-
205  * independent access to AUX functionality. Drivers can take advantage of
206  * this by filling in the fields of the drm_dp_aux structure.
207  *
208  * Transactions are described using a hardware-independent drm_dp_aux_msg
209  * structure, which is passed into a driver's .transfer() implementation.
210  * Both native and I2C-over-AUX transactions are supported.
211  */
212
213 static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,
214                               unsigned int offset, void *buffer, size_t size)
215 {
216         struct drm_dp_aux_msg msg;
217         unsigned int retry, native_reply;
218         int err = 0, ret = 0;
219
220         memset(&msg, 0, sizeof(msg));
221         msg.address = offset;
222         msg.request = request;
223         msg.buffer = buffer;
224         msg.size = size;
225
226         mutex_lock(&aux->hw_mutex);
227
228         /*
229          * The specification doesn't give any recommendation on how often to
230          * retry native transactions. We used to retry 7 times like for
231          * aux i2c transactions but real world devices this wasn't
232          * sufficient, bump to 32 which makes Dell 4k monitors happier.
233          */
234         for (retry = 0; retry < 32; retry++) {
235                 if (ret != 0 && ret != -ETIMEDOUT) {
236                         usleep_range(AUX_RETRY_INTERVAL,
237                                      AUX_RETRY_INTERVAL + 100);
238                 }
239
240                 ret = aux->transfer(aux, &msg);
241                 if (ret >= 0) {
242                         native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;
243                         if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {
244                                 if (ret == size)
245                                         goto unlock;
246
247                                 ret = -EPROTO;
248                         } else
249                                 ret = -EIO;
250                 }
251
252                 /*
253                  * We want the error we return to be the error we received on
254                  * the first transaction, since we may get a different error the
255                  * next time we retry
256                  */
257                 if (!err)
258                         err = ret;
259         }
260
261         DRM_DEBUG_KMS("%s: Too many retries, giving up. First error: %d\n",
262                       aux->name, err);
263         ret = err;
264
265 unlock:
266         mutex_unlock(&aux->hw_mutex);
267         return ret;
268 }
269
270 /**
271  * drm_dp_dpcd_read() - read a series of bytes from the DPCD
272  * @aux: DisplayPort AUX channel (SST or MST)
273  * @offset: address of the (first) register to read
274  * @buffer: buffer to store the register values
275  * @size: number of bytes in @buffer
276  *
277  * Returns the number of bytes transferred on success, or a negative error
278  * code on failure. -EIO is returned if the request was NAKed by the sink or
279  * if the retry count was exceeded. If not all bytes were transferred, this
280  * function returns -EPROTO. Errors from the underlying AUX channel transfer
281  * function, with the exception of -EBUSY (which causes the transaction to
282  * be retried), are propagated to the caller.
283  */
284 ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,
285                          void *buffer, size_t size)
286 {
287         int ret;
288
289         /*
290          * HP ZR24w corrupts the first DPCD access after entering power save
291          * mode. Eg. on a read, the entire buffer will be filled with the same
292          * byte. Do a throw away read to avoid corrupting anything we care
293          * about. Afterwards things will work correctly until the monitor
294          * gets woken up and subsequently re-enters power save mode.
295          *
296          * The user pressing any button on the monitor is enough to wake it
297          * up, so there is no particularly good place to do the workaround.
298          * We just have to do it before any DPCD access and hope that the
299          * monitor doesn't power down exactly after the throw away read.
300          */
301         if (!aux->is_remote) {
302                 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, DP_DPCD_REV,
303                                          buffer, 1);
304                 if (ret != 1)
305                         goto out;
306         }
307
308         if (aux->is_remote)
309                 ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);
310         else
311                 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,
312                                          buffer, size);
313
314 out:
315         drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);
316         return ret;
317 }
318 EXPORT_SYMBOL(drm_dp_dpcd_read);
319
320 /**
321  * drm_dp_dpcd_write() - write a series of bytes to the DPCD
322  * @aux: DisplayPort AUX channel (SST or MST)
323  * @offset: address of the (first) register to write
324  * @buffer: buffer containing the values to write
325  * @size: number of bytes in @buffer
326  *
327  * Returns the number of bytes transferred on success, or a negative error
328  * code on failure. -EIO is returned if the request was NAKed by the sink or
329  * if the retry count was exceeded. If not all bytes were transferred, this
330  * function returns -EPROTO. Errors from the underlying AUX channel transfer
331  * function, with the exception of -EBUSY (which causes the transaction to
332  * be retried), are propagated to the caller.
333  */
334 ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,
335                           void *buffer, size_t size)
336 {
337         int ret;
338
339         if (aux->is_remote)
340                 ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);
341         else
342                 ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,
343                                          buffer, size);
344
345         drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);
346         return ret;
347 }
348 EXPORT_SYMBOL(drm_dp_dpcd_write);
349
350 /**
351  * drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)
352  * @aux: DisplayPort AUX channel
353  * @status: buffer to store the link status in (must be at least 6 bytes)
354  *
355  * Returns the number of bytes transferred on success or a negative error
356  * code on failure.
357  */
358 int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,
359                                  u8 status[DP_LINK_STATUS_SIZE])
360 {
361         return drm_dp_dpcd_read(aux, DP_LANE0_1_STATUS, status,
362                                 DP_LINK_STATUS_SIZE);
363 }
364 EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);
365
366 static bool is_edid_digital_input_dp(const struct edid *edid)
367 {
368         return edid && edid->revision >= 4 &&
369                 edid->input & DRM_EDID_INPUT_DIGITAL &&
370                 (edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;
371 }
372
373 /**
374  * drm_dp_downstream_is_type() - is the downstream facing port of certain type?
375  * @dpcd: DisplayPort configuration data
376  * @port_cap: port capabilities
377  *
378  * Caveat: Only works with DPCD 1.1+ port caps.
379  *
380  * Returns: whether the downstream facing port matches the type.
381  */
382 bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
383                                const u8 port_cap[4], u8 type)
384 {
385         return drm_dp_is_branch(dpcd) &&
386                 dpcd[DP_DPCD_REV] >= 0x11 &&
387                 (port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;
388 }
389 EXPORT_SYMBOL(drm_dp_downstream_is_type);
390
391 /**
392  * drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?
393  * @dpcd: DisplayPort configuration data
394  * @port_cap: port capabilities
395  * @edid: EDID
396  *
397  * Returns: whether the downstream facing port is TMDS (HDMI/DVI).
398  */
399 bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
400                                const u8 port_cap[4],
401                                const struct edid *edid)
402 {
403         if (dpcd[DP_DPCD_REV] < 0x11) {
404                 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
405                 case DP_DWN_STRM_PORT_TYPE_TMDS:
406                         return true;
407                 default:
408                         return false;
409                 }
410         }
411
412         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
413         case DP_DS_PORT_TYPE_DP_DUALMODE:
414                 if (is_edid_digital_input_dp(edid))
415                         return false;
416                 fallthrough;
417         case DP_DS_PORT_TYPE_DVI:
418         case DP_DS_PORT_TYPE_HDMI:
419                 return true;
420         default:
421                 return false;
422         }
423 }
424 EXPORT_SYMBOL(drm_dp_downstream_is_tmds);
425
426 /**
427  * drm_dp_send_real_edid_checksum() - send back real edid checksum value
428  * @aux: DisplayPort AUX channel
429  * @real_edid_checksum: real edid checksum for the last block
430  *
431  * Returns:
432  * True on success
433  */
434 bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,
435                                     u8 real_edid_checksum)
436 {
437         u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;
438
439         if (drm_dp_dpcd_read(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
440                              &auto_test_req, 1) < 1) {
441                 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
442                           aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
443                 return false;
444         }
445         auto_test_req &= DP_AUTOMATED_TEST_REQUEST;
446
447         if (drm_dp_dpcd_read(aux, DP_TEST_REQUEST, &link_edid_read, 1) < 1) {
448                 DRM_ERROR("%s: DPCD failed read at register 0x%x\n",
449                           aux->name, DP_TEST_REQUEST);
450                 return false;
451         }
452         link_edid_read &= DP_TEST_LINK_EDID_READ;
453
454         if (!auto_test_req || !link_edid_read) {
455                 DRM_DEBUG_KMS("%s: Source DUT does not support TEST_EDID_READ\n",
456                               aux->name);
457                 return false;
458         }
459
460         if (drm_dp_dpcd_write(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,
461                               &auto_test_req, 1) < 1) {
462                 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
463                           aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);
464                 return false;
465         }
466
467         /* send back checksum for the last edid extension block data */
468         if (drm_dp_dpcd_write(aux, DP_TEST_EDID_CHECKSUM,
469                               &real_edid_checksum, 1) < 1) {
470                 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
471                           aux->name, DP_TEST_EDID_CHECKSUM);
472                 return false;
473         }
474
475         test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;
476         if (drm_dp_dpcd_write(aux, DP_TEST_RESPONSE, &test_resp, 1) < 1) {
477                 DRM_ERROR("%s: DPCD failed write at register 0x%x\n",
478                           aux->name, DP_TEST_RESPONSE);
479                 return false;
480         }
481
482         return true;
483 }
484 EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);
485
486 static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])
487 {
488         u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;
489
490         if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)
491                 port_count = 4;
492
493         return port_count;
494 }
495
496 static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,
497                                           u8 dpcd[DP_RECEIVER_CAP_SIZE])
498 {
499         u8 dpcd_ext[6];
500         int ret;
501
502         /*
503          * Prior to DP1.3 the bit represented by
504          * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
505          * If it is set DP_DPCD_REV at 0000h could be at a value less than
506          * the true capability of the panel. The only way to check is to
507          * then compare 0000h and 2200h.
508          */
509         if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
510               DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
511                 return 0;
512
513         ret = drm_dp_dpcd_read(aux, DP_DP13_DPCD_REV, &dpcd_ext,
514                                sizeof(dpcd_ext));
515         if (ret < 0)
516                 return ret;
517         if (ret != sizeof(dpcd_ext))
518                 return -EIO;
519
520         if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
521                 DRM_DEBUG_KMS("%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",
522                               aux->name, dpcd[DP_DPCD_REV],
523                               dpcd_ext[DP_DPCD_REV]);
524                 return 0;
525         }
526
527         if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))
528                 return 0;
529
530         DRM_DEBUG_KMS("%s: Base DPCD: %*ph\n",
531                       aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
532
533         memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));
534
535         return 0;
536 }
537
538 /**
539  * drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if
540  * available
541  * @aux: DisplayPort AUX channel
542  * @dpcd: Buffer to store the resulting DPCD in
543  *
544  * Attempts to read the base DPCD caps for @aux. Additionally, this function
545  * checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if
546  * present.
547  *
548  * Returns: %0 if the DPCD was read successfully, negative error code
549  * otherwise.
550  */
551 int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,
552                           u8 dpcd[DP_RECEIVER_CAP_SIZE])
553 {
554         int ret;
555
556         ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
557         if (ret < 0)
558                 return ret;
559         if (ret != DP_RECEIVER_CAP_SIZE || dpcd[DP_DPCD_REV] == 0)
560                 return -EIO;
561
562         ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);
563         if (ret < 0)
564                 return ret;
565
566         DRM_DEBUG_KMS("%s: DPCD: %*ph\n",
567                       aux->name, DP_RECEIVER_CAP_SIZE, dpcd);
568
569         return ret;
570 }
571 EXPORT_SYMBOL(drm_dp_read_dpcd_caps);
572
573 /**
574  * drm_dp_read_downstream_info() - read DPCD downstream port info if available
575  * @aux: DisplayPort AUX channel
576  * @dpcd: A cached copy of the port's DPCD
577  * @downstream_ports: buffer to store the downstream port info in
578  *
579  * See also:
580  * drm_dp_downstream_max_clock()
581  * drm_dp_downstream_max_bpc()
582  *
583  * Returns: 0 if either the downstream port info was read successfully or
584  * there was no downstream info to read, or a negative error code otherwise.
585  */
586 int drm_dp_read_downstream_info(struct drm_dp_aux *aux,
587                                 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
588                                 u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])
589 {
590         int ret;
591         u8 len;
592
593         memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);
594
595         /* No downstream info to read */
596         if (!drm_dp_is_branch(dpcd) ||
597             dpcd[DP_DPCD_REV] < DP_DPCD_REV_10 ||
598             !(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
599                 return 0;
600
601         len = drm_dp_downstream_port_count(dpcd);
602         if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)
603                 len *= 4;
604
605         ret = drm_dp_dpcd_read(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);
606         if (ret < 0)
607                 return ret;
608         if (ret != len)
609                 return -EIO;
610
611         DRM_DEBUG_KMS("%s: DPCD DFP: %*ph\n",
612                       aux->name, len, downstream_ports);
613
614         return 0;
615 }
616 EXPORT_SYMBOL(drm_dp_read_downstream_info);
617
618 /**
619  * drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock
620  * @dpcd: DisplayPort configuration data
621  * @port_cap: port capabilities
622  *
623  * Returns: Downstream facing port max dot clock in kHz on success,
624  * or 0 if max clock not defined
625  */
626 int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
627                                    const u8 port_cap[4])
628 {
629         if (!drm_dp_is_branch(dpcd))
630                 return 0;
631
632         if (dpcd[DP_DPCD_REV] < 0x11)
633                 return 0;
634
635         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
636         case DP_DS_PORT_TYPE_VGA:
637                 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
638                         return 0;
639                 return port_cap[1] * 8000;
640         default:
641                 return 0;
642         }
643 }
644 EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);
645
646 /**
647  * drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock
648  * @dpcd: DisplayPort configuration data
649  * @port_cap: port capabilities
650  * @edid: EDID
651  *
652  * Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,
653  * or 0 if max TMDS clock not defined
654  */
655 int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
656                                      const u8 port_cap[4],
657                                      const struct edid *edid)
658 {
659         if (!drm_dp_is_branch(dpcd))
660                 return 0;
661
662         if (dpcd[DP_DPCD_REV] < 0x11) {
663                 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
664                 case DP_DWN_STRM_PORT_TYPE_TMDS:
665                         return 165000;
666                 default:
667                         return 0;
668                 }
669         }
670
671         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
672         case DP_DS_PORT_TYPE_DP_DUALMODE:
673                 if (is_edid_digital_input_dp(edid))
674                         return 0;
675                 /*
676                  * It's left up to the driver to check the
677                  * DP dual mode adapter's max TMDS clock.
678                  *
679                  * Unfortunatley it looks like branch devices
680                  * may not fordward that the DP dual mode i2c
681                  * access so we just usually get i2c nak :(
682                  */
683                 fallthrough;
684         case DP_DS_PORT_TYPE_HDMI:
685                  /*
686                   * We should perhaps assume 165 MHz when detailed cap
687                   * info is not available. But looks like many typical
688                   * branch devices fall into that category and so we'd
689                   * probably end up with users complaining that they can't
690                   * get high resolution modes with their favorite dongle.
691                   *
692                   * So let's limit to 300 MHz instead since DPCD 1.4
693                   * HDMI 2.0 DFPs are required to have the detailed cap
694                   * info. So it's more likely we're dealing with a HDMI 1.4
695                   * compatible* device here.
696                   */
697                 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
698                         return 300000;
699                 return port_cap[1] * 2500;
700         case DP_DS_PORT_TYPE_DVI:
701                 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
702                         return 165000;
703                 /* FIXME what to do about DVI dual link? */
704                 return port_cap[1] * 2500;
705         default:
706                 return 0;
707         }
708 }
709 EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);
710
711 /**
712  * drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock
713  * @dpcd: DisplayPort configuration data
714  * @port_cap: port capabilities
715  * @edid: EDID
716  *
717  * Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,
718  * or 0 if max TMDS clock not defined
719  */
720 int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
721                                      const u8 port_cap[4],
722                                      const struct edid *edid)
723 {
724         if (!drm_dp_is_branch(dpcd))
725                 return 0;
726
727         if (dpcd[DP_DPCD_REV] < 0x11) {
728                 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
729                 case DP_DWN_STRM_PORT_TYPE_TMDS:
730                         return 25000;
731                 default:
732                         return 0;
733                 }
734         }
735
736         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
737         case DP_DS_PORT_TYPE_DP_DUALMODE:
738                 if (is_edid_digital_input_dp(edid))
739                         return 0;
740                 fallthrough;
741         case DP_DS_PORT_TYPE_DVI:
742         case DP_DS_PORT_TYPE_HDMI:
743                 /*
744                  * Unclear whether the protocol converter could
745                  * utilize pixel replication. Assume it won't.
746                  */
747                 return 25000;
748         default:
749                 return 0;
750         }
751 }
752 EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);
753
754 /**
755  * drm_dp_downstream_max_bpc() - extract downstream facing port max
756  *                               bits per component
757  * @dpcd: DisplayPort configuration data
758  * @port_cap: downstream facing port capabilities
759  * @edid: EDID
760  *
761  * Returns: Max bpc on success or 0 if max bpc not defined
762  */
763 int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
764                               const u8 port_cap[4],
765                               const struct edid *edid)
766 {
767         if (!drm_dp_is_branch(dpcd))
768                 return 0;
769
770         if (dpcd[DP_DPCD_REV] < 0x11) {
771                 switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {
772                 case DP_DWN_STRM_PORT_TYPE_DP:
773                         return 0;
774                 default:
775                         return 8;
776                 }
777         }
778
779         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
780         case DP_DS_PORT_TYPE_DP:
781                 return 0;
782         case DP_DS_PORT_TYPE_DP_DUALMODE:
783                 if (is_edid_digital_input_dp(edid))
784                         return 0;
785                 fallthrough;
786         case DP_DS_PORT_TYPE_HDMI:
787         case DP_DS_PORT_TYPE_DVI:
788         case DP_DS_PORT_TYPE_VGA:
789                 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
790                         return 8;
791
792                 switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {
793                 case DP_DS_8BPC:
794                         return 8;
795                 case DP_DS_10BPC:
796                         return 10;
797                 case DP_DS_12BPC:
798                         return 12;
799                 case DP_DS_16BPC:
800                         return 16;
801                 default:
802                         return 8;
803                 }
804                 break;
805         default:
806                 return 8;
807         }
808 }
809 EXPORT_SYMBOL(drm_dp_downstream_max_bpc);
810
811 /**
812  * drm_dp_downstream_420_passthrough() - determine downstream facing port
813  *                                       YCbCr 4:2:0 pass-through capability
814  * @dpcd: DisplayPort configuration data
815  * @port_cap: downstream facing port capabilities
816  *
817  * Returns: whether the downstream facing port can pass through YCbCr 4:2:0
818  */
819 bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
820                                        const u8 port_cap[4])
821 {
822         if (!drm_dp_is_branch(dpcd))
823                 return false;
824
825         if (dpcd[DP_DPCD_REV] < 0x13)
826                 return false;
827
828         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
829         case DP_DS_PORT_TYPE_DP:
830                 return true;
831         case DP_DS_PORT_TYPE_HDMI:
832                 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
833                         return false;
834
835                 return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;
836         default:
837                 return false;
838         }
839 }
840 EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);
841
842 /**
843  * drm_dp_downstream_444_to_420_conversion() - determine downstream facing port
844  *                                             YCbCr 4:4:4->4:2:0 conversion capability
845  * @dpcd: DisplayPort configuration data
846  * @port_cap: downstream facing port capabilities
847  *
848  * Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:0
849  */
850 bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
851                                              const u8 port_cap[4])
852 {
853         if (!drm_dp_is_branch(dpcd))
854                 return false;
855
856         if (dpcd[DP_DPCD_REV] < 0x13)
857                 return false;
858
859         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
860         case DP_DS_PORT_TYPE_HDMI:
861                 if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)
862                         return false;
863
864                 return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;
865         default:
866                 return false;
867         }
868 }
869 EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);
870
871 /**
872  * drm_dp_downstream_mode() - return a mode for downstream facing port
873  * @dpcd: DisplayPort configuration data
874  * @port_cap: port capabilities
875  *
876  * Provides a suitable mode for downstream facing ports without EDID.
877  *
878  * Returns: A new drm_display_mode on success or NULL on failure
879  */
880 struct drm_display_mode *
881 drm_dp_downstream_mode(struct drm_device *dev,
882                        const u8 dpcd[DP_RECEIVER_CAP_SIZE],
883                        const u8 port_cap[4])
884
885 {
886         u8 vic;
887
888         if (!drm_dp_is_branch(dpcd))
889                 return NULL;
890
891         if (dpcd[DP_DPCD_REV] < 0x11)
892                 return NULL;
893
894         switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {
895         case DP_DS_PORT_TYPE_NON_EDID:
896                 switch (port_cap[0] & DP_DS_NON_EDID_MASK) {
897                 case DP_DS_NON_EDID_720x480i_60:
898                         vic = 6;
899                         break;
900                 case DP_DS_NON_EDID_720x480i_50:
901                         vic = 21;
902                         break;
903                 case DP_DS_NON_EDID_1920x1080i_60:
904                         vic = 5;
905                         break;
906                 case DP_DS_NON_EDID_1920x1080i_50:
907                         vic = 20;
908                         break;
909                 case DP_DS_NON_EDID_1280x720_60:
910                         vic = 4;
911                         break;
912                 case DP_DS_NON_EDID_1280x720_50:
913                         vic = 19;
914                         break;
915                 default:
916                         return NULL;
917                 }
918                 return drm_display_mode_from_cea_vic(dev, vic);
919         default:
920                 return NULL;
921         }
922 }
923 EXPORT_SYMBOL(drm_dp_downstream_mode);
924
925 /**
926  * drm_dp_downstream_id() - identify branch device
927  * @aux: DisplayPort AUX channel
928  * @id: DisplayPort branch device id
929  *
930  * Returns branch device id on success or NULL on failure
931  */
932 int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])
933 {
934         return drm_dp_dpcd_read(aux, DP_BRANCH_ID, id, 6);
935 }
936 EXPORT_SYMBOL(drm_dp_downstream_id);
937
938 /**
939  * drm_dp_downstream_debug() - debug DP branch devices
940  * @m: pointer for debugfs file
941  * @dpcd: DisplayPort configuration data
942  * @port_cap: port capabilities
943  * @edid: EDID
944  * @aux: DisplayPort AUX channel
945  *
946  */
947 void drm_dp_downstream_debug(struct seq_file *m,
948                              const u8 dpcd[DP_RECEIVER_CAP_SIZE],
949                              const u8 port_cap[4],
950                              const struct edid *edid,
951                              struct drm_dp_aux *aux)
952 {
953         bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
954                                  DP_DETAILED_CAP_INFO_AVAILABLE;
955         int clk;
956         int bpc;
957         char id[7];
958         int len;
959         uint8_t rev[2];
960         int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
961         bool branch_device = drm_dp_is_branch(dpcd);
962
963         seq_printf(m, "\tDP branch device present: %s\n",
964                    branch_device ? "yes" : "no");
965
966         if (!branch_device)
967                 return;
968
969         switch (type) {
970         case DP_DS_PORT_TYPE_DP:
971                 seq_puts(m, "\t\tType: DisplayPort\n");
972                 break;
973         case DP_DS_PORT_TYPE_VGA:
974                 seq_puts(m, "\t\tType: VGA\n");
975                 break;
976         case DP_DS_PORT_TYPE_DVI:
977                 seq_puts(m, "\t\tType: DVI\n");
978                 break;
979         case DP_DS_PORT_TYPE_HDMI:
980                 seq_puts(m, "\t\tType: HDMI\n");
981                 break;
982         case DP_DS_PORT_TYPE_NON_EDID:
983                 seq_puts(m, "\t\tType: others without EDID support\n");
984                 break;
985         case DP_DS_PORT_TYPE_DP_DUALMODE:
986                 seq_puts(m, "\t\tType: DP++\n");
987                 break;
988         case DP_DS_PORT_TYPE_WIRELESS:
989                 seq_puts(m, "\t\tType: Wireless\n");
990                 break;
991         default:
992                 seq_puts(m, "\t\tType: N/A\n");
993         }
994
995         memset(id, 0, sizeof(id));
996         drm_dp_downstream_id(aux, id);
997         seq_printf(m, "\t\tID: %s\n", id);
998
999         len = drm_dp_dpcd_read(aux, DP_BRANCH_HW_REV, &rev[0], 1);
1000         if (len > 0)
1001                 seq_printf(m, "\t\tHW: %d.%d\n",
1002                            (rev[0] & 0xf0) >> 4, rev[0] & 0xf);
1003
1004         len = drm_dp_dpcd_read(aux, DP_BRANCH_SW_REV, rev, 2);
1005         if (len > 0)
1006                 seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);
1007
1008         if (detailed_cap_info) {
1009                 clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);
1010                 if (clk > 0)
1011                         seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);
1012
1013                 clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, edid);
1014                 if (clk > 0)
1015                         seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);
1016
1017                 clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, edid);
1018                 if (clk > 0)
1019                         seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);
1020
1021                 bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, edid);
1022
1023                 if (bpc > 0)
1024                         seq_printf(m, "\t\tMax bpc: %d\n", bpc);
1025         }
1026 }
1027 EXPORT_SYMBOL(drm_dp_downstream_debug);
1028
1029 /**
1030  * drm_dp_subconnector_type() - get DP branch device type
1031  *
1032  */
1033 enum drm_mode_subconnector
1034 drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1035                          const u8 port_cap[4])
1036 {
1037         int type;
1038         if (!drm_dp_is_branch(dpcd))
1039                 return DRM_MODE_SUBCONNECTOR_Native;
1040         /* DP 1.0 approach */
1041         if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {
1042                 type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &
1043                        DP_DWN_STRM_PORT_TYPE_MASK;
1044
1045                 switch (type) {
1046                 case DP_DWN_STRM_PORT_TYPE_TMDS:
1047                         /* Can be HDMI or DVI-D, DVI-D is a safer option */
1048                         return DRM_MODE_SUBCONNECTOR_DVID;
1049                 case DP_DWN_STRM_PORT_TYPE_ANALOG:
1050                         /* Can be VGA or DVI-A, VGA is more popular */
1051                         return DRM_MODE_SUBCONNECTOR_VGA;
1052                 case DP_DWN_STRM_PORT_TYPE_DP:
1053                         return DRM_MODE_SUBCONNECTOR_DisplayPort;
1054                 case DP_DWN_STRM_PORT_TYPE_OTHER:
1055                 default:
1056                         return DRM_MODE_SUBCONNECTOR_Unknown;
1057                 }
1058         }
1059         type = port_cap[0] & DP_DS_PORT_TYPE_MASK;
1060
1061         switch (type) {
1062         case DP_DS_PORT_TYPE_DP:
1063         case DP_DS_PORT_TYPE_DP_DUALMODE:
1064                 return DRM_MODE_SUBCONNECTOR_DisplayPort;
1065         case DP_DS_PORT_TYPE_VGA:
1066                 return DRM_MODE_SUBCONNECTOR_VGA;
1067         case DP_DS_PORT_TYPE_DVI:
1068                 return DRM_MODE_SUBCONNECTOR_DVID;
1069         case DP_DS_PORT_TYPE_HDMI:
1070                 return DRM_MODE_SUBCONNECTOR_HDMIA;
1071         case DP_DS_PORT_TYPE_WIRELESS:
1072                 return DRM_MODE_SUBCONNECTOR_Wireless;
1073         case DP_DS_PORT_TYPE_NON_EDID:
1074         default:
1075                 return DRM_MODE_SUBCONNECTOR_Unknown;
1076         }
1077 }
1078 EXPORT_SYMBOL(drm_dp_subconnector_type);
1079
1080 /**
1081  * drm_mode_set_dp_subconnector_property - set subconnector for DP connector
1082  *
1083  * Called by a driver on every detect event.
1084  */
1085 void drm_dp_set_subconnector_property(struct drm_connector *connector,
1086                                       enum drm_connector_status status,
1087                                       const u8 *dpcd,
1088                                       const u8 port_cap[4])
1089 {
1090         enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1091
1092         if (status == connector_status_connected)
1093                 subconnector = drm_dp_subconnector_type(dpcd, port_cap);
1094         drm_object_property_set_value(&connector->base,
1095                         connector->dev->mode_config.dp_subconnector_property,
1096                         subconnector);
1097 }
1098 EXPORT_SYMBOL(drm_dp_set_subconnector_property);
1099
1100 /**
1101  * drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink
1102  * count
1103  * @connector: The DRM connector to check
1104  * @dpcd: A cached copy of the connector's DPCD RX capabilities
1105  * @desc: A cached copy of the connector's DP descriptor
1106  *
1107  * See also: drm_dp_read_sink_count()
1108  *
1109  * Returns: %True if the (e)DP connector has a valid sink count that should
1110  * be probed, %false otherwise.
1111  */
1112 bool drm_dp_read_sink_count_cap(struct drm_connector *connector,
1113                                 const u8 dpcd[DP_RECEIVER_CAP_SIZE],
1114                                 const struct drm_dp_desc *desc)
1115 {
1116         /* Some eDP panels don't set a valid value for the sink count */
1117         return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
1118                 dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&
1119                 dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&
1120                 !drm_dp_has_quirk(desc, 0, DP_DPCD_QUIRK_NO_SINK_COUNT);
1121 }
1122 EXPORT_SYMBOL(drm_dp_read_sink_count_cap);
1123
1124 /**
1125  * drm_dp_read_sink_count() - Retrieve the sink count for a given sink
1126  * @aux: The DP AUX channel to use
1127  *
1128  * See also: drm_dp_read_sink_count_cap()
1129  *
1130  * Returns: The current sink count reported by @aux, or a negative error code
1131  * otherwise.
1132  */
1133 int drm_dp_read_sink_count(struct drm_dp_aux *aux)
1134 {
1135         u8 count;
1136         int ret;
1137
1138         ret = drm_dp_dpcd_readb(aux, DP_SINK_COUNT, &count);
1139         if (ret < 0)
1140                 return ret;
1141         if (ret != 1)
1142                 return -EIO;
1143
1144         return DP_GET_SINK_COUNT(count);
1145 }
1146 EXPORT_SYMBOL(drm_dp_read_sink_count);
1147
1148 /*
1149  * I2C-over-AUX implementation
1150  */
1151
1152 static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)
1153 {
1154         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1155                I2C_FUNC_SMBUS_READ_BLOCK_DATA |
1156                I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
1157                I2C_FUNC_10BIT_ADDR;
1158 }
1159
1160 static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)
1161 {
1162         /*
1163          * In case of i2c defer or short i2c ack reply to a write,
1164          * we need to switch to WRITE_STATUS_UPDATE to drain the
1165          * rest of the message
1166          */
1167         if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {
1168                 msg->request &= DP_AUX_I2C_MOT;
1169                 msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;
1170         }
1171 }
1172
1173 #define AUX_PRECHARGE_LEN 10 /* 10 to 16 */
1174 #define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */
1175 #define AUX_STOP_LEN 4
1176 #define AUX_CMD_LEN 4
1177 #define AUX_ADDRESS_LEN 20
1178 #define AUX_REPLY_PAD_LEN 4
1179 #define AUX_LENGTH_LEN 8
1180
1181 /*
1182  * Calculate the duration of the AUX request/reply in usec. Gives the
1183  * "best" case estimate, ie. successful while as short as possible.
1184  */
1185 static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)
1186 {
1187         int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1188                 AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;
1189
1190         if ((msg->request & DP_AUX_I2C_READ) == 0)
1191                 len += msg->size * 8;
1192
1193         return len;
1194 }
1195
1196 static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)
1197 {
1198         int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +
1199                 AUX_CMD_LEN + AUX_REPLY_PAD_LEN;
1200
1201         /*
1202          * For read we expect what was asked. For writes there will
1203          * be 0 or 1 data bytes. Assume 0 for the "best" case.
1204          */
1205         if (msg->request & DP_AUX_I2C_READ)
1206                 len += msg->size * 8;
1207
1208         return len;
1209 }
1210
1211 #define I2C_START_LEN 1
1212 #define I2C_STOP_LEN 1
1213 #define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */
1214 #define I2C_DATA_LEN 9 /* DATA + ACK/NACK */
1215
1216 /*
1217  * Calculate the length of the i2c transfer in usec, assuming
1218  * the i2c bus speed is as specified. Gives the the "worst"
1219  * case estimate, ie. successful while as long as possible.
1220  * Doesn't account the the "MOT" bit, and instead assumes each
1221  * message includes a START, ADDRESS and STOP. Neither does it
1222  * account for additional random variables such as clock stretching.
1223  */
1224 static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,
1225                                    int i2c_speed_khz)
1226 {
1227         /* AUX bitrate is 1MHz, i2c bitrate as specified */
1228         return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +
1229                              msg->size * I2C_DATA_LEN +
1230                              I2C_STOP_LEN) * 1000, i2c_speed_khz);
1231 }
1232
1233 /*
1234  * Deterine how many retries should be attempted to successfully transfer
1235  * the specified message, based on the estimated durations of the
1236  * i2c and AUX transfers.
1237  */
1238 static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,
1239                               int i2c_speed_khz)
1240 {
1241         int aux_time_us = drm_dp_aux_req_duration(msg) +
1242                 drm_dp_aux_reply_duration(msg);
1243         int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);
1244
1245         return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);
1246 }
1247
1248 /*
1249  * FIXME currently assumes 10 kHz as some real world devices seem
1250  * to require it. We should query/set the speed via DPCD if supported.
1251  */
1252 static int dp_aux_i2c_speed_khz __read_mostly = 10;
1253 module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);
1254 MODULE_PARM_DESC(dp_aux_i2c_speed_khz,
1255                  "Assumed speed of the i2c bus in kHz, (1-400, default 10)");
1256
1257 /*
1258  * Transfer a single I2C-over-AUX message and handle various error conditions,
1259  * retrying the transaction as appropriate.  It is assumed that the
1260  * &drm_dp_aux.transfer function does not modify anything in the msg other than the
1261  * reply field.
1262  *
1263  * Returns bytes transferred on success, or a negative error code on failure.
1264  */
1265 static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1266 {
1267         unsigned int retry, defer_i2c;
1268         int ret;
1269         /*
1270          * DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device
1271          * is required to retry at least seven times upon receiving AUX_DEFER
1272          * before giving up the AUX transaction.
1273          *
1274          * We also try to account for the i2c bus speed.
1275          */
1276         int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));
1277
1278         for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {
1279                 ret = aux->transfer(aux, msg);
1280                 if (ret < 0) {
1281                         if (ret == -EBUSY)
1282                                 continue;
1283
1284                         /*
1285                          * While timeouts can be errors, they're usually normal
1286                          * behavior (for instance, when a driver tries to
1287                          * communicate with a non-existant DisplayPort device).
1288                          * Avoid spamming the kernel log with timeout errors.
1289                          */
1290                         if (ret == -ETIMEDOUT)
1291                                 DRM_DEBUG_KMS_RATELIMITED("%s: transaction timed out\n",
1292                                                           aux->name);
1293                         else
1294                                 DRM_DEBUG_KMS("%s: transaction failed: %d\n",
1295                                               aux->name, ret);
1296                         return ret;
1297                 }
1298
1299
1300                 switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {
1301                 case DP_AUX_NATIVE_REPLY_ACK:
1302                         /*
1303                          * For I2C-over-AUX transactions this isn't enough, we
1304                          * need to check for the I2C ACK reply.
1305                          */
1306                         break;
1307
1308                 case DP_AUX_NATIVE_REPLY_NACK:
1309                         DRM_DEBUG_KMS("%s: native nack (result=%d, size=%zu)\n",
1310                                       aux->name, ret, msg->size);
1311                         return -EREMOTEIO;
1312
1313                 case DP_AUX_NATIVE_REPLY_DEFER:
1314                         DRM_DEBUG_KMS("%s: native defer\n", aux->name);
1315                         /*
1316                          * We could check for I2C bit rate capabilities and if
1317                          * available adjust this interval. We could also be
1318                          * more careful with DP-to-legacy adapters where a
1319                          * long legacy cable may force very low I2C bit rates.
1320                          *
1321                          * For now just defer for long enough to hopefully be
1322                          * safe for all use-cases.
1323                          */
1324                         usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1325                         continue;
1326
1327                 default:
1328                         DRM_ERROR("%s: invalid native reply %#04x\n",
1329                                   aux->name, msg->reply);
1330                         return -EREMOTEIO;
1331                 }
1332
1333                 switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {
1334                 case DP_AUX_I2C_REPLY_ACK:
1335                         /*
1336                          * Both native ACK and I2C ACK replies received. We
1337                          * can assume the transfer was successful.
1338                          */
1339                         if (ret != msg->size)
1340                                 drm_dp_i2c_msg_write_status_update(msg);
1341                         return ret;
1342
1343                 case DP_AUX_I2C_REPLY_NACK:
1344                         DRM_DEBUG_KMS("%s: I2C nack (result=%d, size=%zu)\n",
1345                                       aux->name, ret, msg->size);
1346                         aux->i2c_nack_count++;
1347                         return -EREMOTEIO;
1348
1349                 case DP_AUX_I2C_REPLY_DEFER:
1350                         DRM_DEBUG_KMS("%s: I2C defer\n", aux->name);
1351                         /* DP Compliance Test 4.2.2.5 Requirement:
1352                          * Must have at least 7 retries for I2C defers on the
1353                          * transaction to pass this test
1354                          */
1355                         aux->i2c_defer_count++;
1356                         if (defer_i2c < 7)
1357                                 defer_i2c++;
1358                         usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);
1359                         drm_dp_i2c_msg_write_status_update(msg);
1360
1361                         continue;
1362
1363                 default:
1364                         DRM_ERROR("%s: invalid I2C reply %#04x\n",
1365                                   aux->name, msg->reply);
1366                         return -EREMOTEIO;
1367                 }
1368         }
1369
1370         DRM_DEBUG_KMS("%s: Too many retries, giving up\n", aux->name);
1371         return -EREMOTEIO;
1372 }
1373
1374 static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,
1375                                        const struct i2c_msg *i2c_msg)
1376 {
1377         msg->request = (i2c_msg->flags & I2C_M_RD) ?
1378                 DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;
1379         if (!(i2c_msg->flags & I2C_M_STOP))
1380                 msg->request |= DP_AUX_I2C_MOT;
1381 }
1382
1383 /*
1384  * Keep retrying drm_dp_i2c_do_msg until all data has been transferred.
1385  *
1386  * Returns an error code on failure, or a recommended transfer size on success.
1387  */
1388 static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)
1389 {
1390         int err, ret = orig_msg->size;
1391         struct drm_dp_aux_msg msg = *orig_msg;
1392
1393         while (msg.size > 0) {
1394                 err = drm_dp_i2c_do_msg(aux, &msg);
1395                 if (err <= 0)
1396                         return err == 0 ? -EPROTO : err;
1397
1398                 if (err < msg.size && err < ret) {
1399                         DRM_DEBUG_KMS("%s: Partial I2C reply: requested %zu bytes got %d bytes\n",
1400                                       aux->name, msg.size, err);
1401                         ret = err;
1402                 }
1403
1404                 msg.size -= err;
1405                 msg.buffer += err;
1406         }
1407
1408         return ret;
1409 }
1410
1411 /*
1412  * Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX
1413  * packets to be as large as possible. If not, the I2C transactions never
1414  * succeed. Hence the default is maximum.
1415  */
1416 static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;
1417 module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);
1418 MODULE_PARM_DESC(dp_aux_i2c_transfer_size,
1419                  "Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");
1420
1421 static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
1422                            int num)
1423 {
1424         struct drm_dp_aux *aux = adapter->algo_data;
1425         unsigned int i, j;
1426         unsigned transfer_size;
1427         struct drm_dp_aux_msg msg;
1428         int err = 0;
1429
1430         dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);
1431
1432         memset(&msg, 0, sizeof(msg));
1433
1434         for (i = 0; i < num; i++) {
1435                 msg.address = msgs[i].addr;
1436                 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1437                 /* Send a bare address packet to start the transaction.
1438                  * Zero sized messages specify an address only (bare
1439                  * address) transaction.
1440                  */
1441                 msg.buffer = NULL;
1442                 msg.size = 0;
1443                 err = drm_dp_i2c_do_msg(aux, &msg);
1444
1445                 /*
1446                  * Reset msg.request in case in case it got
1447                  * changed into a WRITE_STATUS_UPDATE.
1448                  */
1449                 drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1450
1451                 if (err < 0)
1452                         break;
1453                 /* We want each transaction to be as large as possible, but
1454                  * we'll go to smaller sizes if the hardware gives us a
1455                  * short reply.
1456                  */
1457                 transfer_size = dp_aux_i2c_transfer_size;
1458                 for (j = 0; j < msgs[i].len; j += msg.size) {
1459                         msg.buffer = msgs[i].buf + j;
1460                         msg.size = min(transfer_size, msgs[i].len - j);
1461
1462                         err = drm_dp_i2c_drain_msg(aux, &msg);
1463
1464                         /*
1465                          * Reset msg.request in case in case it got
1466                          * changed into a WRITE_STATUS_UPDATE.
1467                          */
1468                         drm_dp_i2c_msg_set_request(&msg, &msgs[i]);
1469
1470                         if (err < 0)
1471                                 break;
1472                         transfer_size = err;
1473                 }
1474                 if (err < 0)
1475                         break;
1476         }
1477         if (err >= 0)
1478                 err = num;
1479         /* Send a bare address packet to close out the transaction.
1480          * Zero sized messages specify an address only (bare
1481          * address) transaction.
1482          */
1483         msg.request &= ~DP_AUX_I2C_MOT;
1484         msg.buffer = NULL;
1485         msg.size = 0;
1486         (void)drm_dp_i2c_do_msg(aux, &msg);
1487
1488         return err;
1489 }
1490
1491 static const struct i2c_algorithm drm_dp_i2c_algo = {
1492         .functionality = drm_dp_i2c_functionality,
1493         .master_xfer = drm_dp_i2c_xfer,
1494 };
1495
1496 static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)
1497 {
1498         return container_of(i2c, struct drm_dp_aux, ddc);
1499 }
1500
1501 static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)
1502 {
1503         mutex_lock(&i2c_to_aux(i2c)->hw_mutex);
1504 }
1505
1506 static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)
1507 {
1508         return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);
1509 }
1510
1511 static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)
1512 {
1513         mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);
1514 }
1515
1516 static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {
1517         .lock_bus = lock_bus,
1518         .trylock_bus = trylock_bus,
1519         .unlock_bus = unlock_bus,
1520 };
1521
1522 static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)
1523 {
1524         u8 buf, count;
1525         int ret;
1526
1527         ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1528         if (ret < 0)
1529                 return ret;
1530
1531         WARN_ON(!(buf & DP_TEST_SINK_START));
1532
1533         ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK_MISC, &buf);
1534         if (ret < 0)
1535                 return ret;
1536
1537         count = buf & DP_TEST_COUNT_MASK;
1538         if (count == aux->crc_count)
1539                 return -EAGAIN; /* No CRC yet */
1540
1541         aux->crc_count = count;
1542
1543         /*
1544          * At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes
1545          * per component (RGB or CrYCb).
1546          */
1547         ret = drm_dp_dpcd_read(aux, DP_TEST_CRC_R_CR, crc, 6);
1548         if (ret < 0)
1549                 return ret;
1550
1551         return 0;
1552 }
1553
1554 static void drm_dp_aux_crc_work(struct work_struct *work)
1555 {
1556         struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,
1557                                               crc_work);
1558         struct drm_crtc *crtc;
1559         u8 crc_bytes[6];
1560         uint32_t crcs[3];
1561         int ret;
1562
1563         if (WARN_ON(!aux->crtc))
1564                 return;
1565
1566         crtc = aux->crtc;
1567         while (crtc->crc.opened) {
1568                 drm_crtc_wait_one_vblank(crtc);
1569                 if (!crtc->crc.opened)
1570                         break;
1571
1572                 ret = drm_dp_aux_get_crc(aux, crc_bytes);
1573                 if (ret == -EAGAIN) {
1574                         usleep_range(1000, 2000);
1575                         ret = drm_dp_aux_get_crc(aux, crc_bytes);
1576                 }
1577
1578                 if (ret == -EAGAIN) {
1579                         DRM_DEBUG_KMS("%s: Get CRC failed after retrying: %d\n",
1580                                       aux->name, ret);
1581                         continue;
1582                 } else if (ret) {
1583                         DRM_DEBUG_KMS("%s: Failed to get a CRC: %d\n",
1584                                       aux->name, ret);
1585                         continue;
1586                 }
1587
1588                 crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;
1589                 crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;
1590                 crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;
1591                 drm_crtc_add_crc_entry(crtc, false, 0, crcs);
1592         }
1593 }
1594
1595 /**
1596  * drm_dp_remote_aux_init() - minimally initialise a remote aux channel
1597  * @aux: DisplayPort AUX channel
1598  *
1599  * Used for remote aux channel in general. Merely initialize the crc work
1600  * struct.
1601  */
1602 void drm_dp_remote_aux_init(struct drm_dp_aux *aux)
1603 {
1604         INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1605 }
1606 EXPORT_SYMBOL(drm_dp_remote_aux_init);
1607
1608 /**
1609  * drm_dp_aux_init() - minimally initialise an aux channel
1610  * @aux: DisplayPort AUX channel
1611  *
1612  * If you need to use the drm_dp_aux's i2c adapter prior to registering it
1613  * with the outside world, call drm_dp_aux_init() first. You must still
1614  * call drm_dp_aux_register() once the connector has been registered to
1615  * allow userspace access to the auxiliary DP channel.
1616  */
1617 void drm_dp_aux_init(struct drm_dp_aux *aux)
1618 {
1619         mutex_init(&aux->hw_mutex);
1620         mutex_init(&aux->cec.lock);
1621         INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);
1622
1623         aux->ddc.algo = &drm_dp_i2c_algo;
1624         aux->ddc.algo_data = aux;
1625         aux->ddc.retries = 3;
1626
1627         aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;
1628 }
1629 EXPORT_SYMBOL(drm_dp_aux_init);
1630
1631 /**
1632  * drm_dp_aux_register() - initialise and register aux channel
1633  * @aux: DisplayPort AUX channel
1634  *
1635  * Automatically calls drm_dp_aux_init() if this hasn't been done yet.
1636  * This should only be called when the underlying &struct drm_connector is
1637  * initialiazed already. Therefore the best place to call this is from
1638  * &drm_connector_funcs.late_register. Not that drivers which don't follow this
1639  * will Oops when CONFIG_DRM_DP_AUX_CHARDEV is enabled.
1640  *
1641  * Drivers which need to use the aux channel before that point (e.g. at driver
1642  * load time, before drm_dev_register() has been called) need to call
1643  * drm_dp_aux_init().
1644  *
1645  * Returns 0 on success or a negative error code on failure.
1646  */
1647 int drm_dp_aux_register(struct drm_dp_aux *aux)
1648 {
1649         int ret;
1650
1651         if (!aux->ddc.algo)
1652                 drm_dp_aux_init(aux);
1653
1654         aux->ddc.class = I2C_CLASS_DDC;
1655         aux->ddc.owner = THIS_MODULE;
1656         aux->ddc.dev.parent = aux->dev;
1657
1658         strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
1659                 sizeof(aux->ddc.name));
1660
1661         ret = drm_dp_aux_register_devnode(aux);
1662         if (ret)
1663                 return ret;
1664
1665         ret = i2c_add_adapter(&aux->ddc);
1666         if (ret) {
1667                 drm_dp_aux_unregister_devnode(aux);
1668                 return ret;
1669         }
1670
1671         return 0;
1672 }
1673 EXPORT_SYMBOL(drm_dp_aux_register);
1674
1675 /**
1676  * drm_dp_aux_unregister() - unregister an AUX adapter
1677  * @aux: DisplayPort AUX channel
1678  */
1679 void drm_dp_aux_unregister(struct drm_dp_aux *aux)
1680 {
1681         drm_dp_aux_unregister_devnode(aux);
1682         i2c_del_adapter(&aux->ddc);
1683 }
1684 EXPORT_SYMBOL(drm_dp_aux_unregister);
1685
1686 #define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)
1687
1688 /**
1689  * drm_dp_psr_setup_time() - PSR setup in time usec
1690  * @psr_cap: PSR capabilities from DPCD
1691  *
1692  * Returns:
1693  * PSR setup time for the panel in microseconds,  negative
1694  * error code on failure.
1695  */
1696 int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])
1697 {
1698         static const u16 psr_setup_time_us[] = {
1699                 PSR_SETUP_TIME(330),
1700                 PSR_SETUP_TIME(275),
1701                 PSR_SETUP_TIME(220),
1702                 PSR_SETUP_TIME(165),
1703                 PSR_SETUP_TIME(110),
1704                 PSR_SETUP_TIME(55),
1705                 PSR_SETUP_TIME(0),
1706         };
1707         int i;
1708
1709         i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;
1710         if (i >= ARRAY_SIZE(psr_setup_time_us))
1711                 return -EINVAL;
1712
1713         return psr_setup_time_us[i];
1714 }
1715 EXPORT_SYMBOL(drm_dp_psr_setup_time);
1716
1717 #undef PSR_SETUP_TIME
1718
1719 /**
1720  * drm_dp_start_crc() - start capture of frame CRCs
1721  * @aux: DisplayPort AUX channel
1722  * @crtc: CRTC displaying the frames whose CRCs are to be captured
1723  *
1724  * Returns 0 on success or a negative error code on failure.
1725  */
1726 int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)
1727 {
1728         u8 buf;
1729         int ret;
1730
1731         ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1732         if (ret < 0)
1733                 return ret;
1734
1735         ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);
1736         if (ret < 0)
1737                 return ret;
1738
1739         aux->crc_count = 0;
1740         aux->crtc = crtc;
1741         schedule_work(&aux->crc_work);
1742
1743         return 0;
1744 }
1745 EXPORT_SYMBOL(drm_dp_start_crc);
1746
1747 /**
1748  * drm_dp_stop_crc() - stop capture of frame CRCs
1749  * @aux: DisplayPort AUX channel
1750  *
1751  * Returns 0 on success or a negative error code on failure.
1752  */
1753 int drm_dp_stop_crc(struct drm_dp_aux *aux)
1754 {
1755         u8 buf;
1756         int ret;
1757
1758         ret = drm_dp_dpcd_readb(aux, DP_TEST_SINK, &buf);
1759         if (ret < 0)
1760                 return ret;
1761
1762         ret = drm_dp_dpcd_writeb(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);
1763         if (ret < 0)
1764                 return ret;
1765
1766         flush_work(&aux->crc_work);
1767         aux->crtc = NULL;
1768
1769         return 0;
1770 }
1771 EXPORT_SYMBOL(drm_dp_stop_crc);
1772
1773 struct dpcd_quirk {
1774         u8 oui[3];
1775         u8 device_id[6];
1776         bool is_branch;
1777         u32 quirks;
1778 };
1779
1780 #define OUI(first, second, third) { (first), (second), (third) }
1781 #define DEVICE_ID(first, second, third, fourth, fifth, sixth) \
1782         { (first), (second), (third), (fourth), (fifth), (sixth) }
1783
1784 #define DEVICE_ID_ANY   DEVICE_ID(0, 0, 0, 0, 0, 0)
1785
1786 static const struct dpcd_quirk dpcd_quirk_list[] = {
1787         /* Analogix 7737 needs reduced M and N at HBR2 link rates */
1788         { OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1789         /* LG LP140WF6-SPM1 eDP panel */
1790         { OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },
1791         /* Apple panels need some additional handling to support PSR */
1792         { OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },
1793         /* CH7511 seems to leave SINK_COUNT zeroed */
1794         { OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },
1795         /* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */
1796         { OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },
1797         /* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */
1798         { OUI(0x00, 0x10, 0xfa), DEVICE_ID(101, 68, 21, 101, 98, 97), false, BIT(DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS) },
1799 };
1800
1801 #undef OUI
1802
1803 /*
1804  * Get a bit mask of DPCD quirks for the sink/branch device identified by
1805  * ident. The quirk data is shared but it's up to the drivers to act on the
1806  * data.
1807  *
1808  * For now, only the OUI (first three bytes) is used, but this may be extended
1809  * to device identification string and hardware/firmware revisions later.
1810  */
1811 static u32
1812 drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)
1813 {
1814         const struct dpcd_quirk *quirk;
1815         u32 quirks = 0;
1816         int i;
1817         u8 any_device[] = DEVICE_ID_ANY;
1818
1819         for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {
1820                 quirk = &dpcd_quirk_list[i];
1821
1822                 if (quirk->is_branch != is_branch)
1823                         continue;
1824
1825                 if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)
1826                         continue;
1827
1828                 if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&
1829                     memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)
1830                         continue;
1831
1832                 quirks |= quirk->quirks;
1833         }
1834
1835         return quirks;
1836 }
1837
1838 #undef DEVICE_ID_ANY
1839 #undef DEVICE_ID
1840
1841 struct edid_quirk {
1842         u8 mfg_id[2];
1843         u8 prod_id[2];
1844         u32 quirks;
1845 };
1846
1847 #define MFG(first, second) { (first), (second) }
1848 #define PROD_ID(first, second) { (first), (second) }
1849
1850 /*
1851  * Some devices have unreliable OUIDs where they don't set the device ID
1852  * correctly, and as a result we need to use the EDID for finding additional
1853  * DP quirks in such cases.
1854  */
1855 static const struct edid_quirk edid_quirk_list[] = {
1856         /* Optional 4K AMOLED panel in the ThinkPad X1 Extreme 2nd Generation
1857          * only supports DPCD backlight controls
1858          */
1859         { MFG(0x4c, 0x83), PROD_ID(0x41, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1860         /*
1861          * Some Dell CML 2020 systems have panels support both AUX and PWM
1862          * backlight control, and some only support AUX backlight control. All
1863          * said panels start up in AUX mode by default, and we don't have any
1864          * support for disabling HDR mode on these panels which would be
1865          * required to switch to PWM backlight control mode (plus, I'm not
1866          * even sure we want PWM backlight controls over DPCD backlight
1867          * controls anyway...). Until we have a better way of detecting these,
1868          * force DPCD backlight mode on all of them.
1869          */
1870         { MFG(0x06, 0xaf), PROD_ID(0x9b, 0x32), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1871         { MFG(0x06, 0xaf), PROD_ID(0xeb, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1872         { MFG(0x4d, 0x10), PROD_ID(0xc7, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1873         { MFG(0x4d, 0x10), PROD_ID(0xe6, 0x14), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1874         { MFG(0x4c, 0x83), PROD_ID(0x47, 0x41), BIT(DP_QUIRK_FORCE_DPCD_BACKLIGHT) },
1875 };
1876
1877 #undef MFG
1878 #undef PROD_ID
1879
1880 /**
1881  * drm_dp_get_edid_quirks() - Check the EDID of a DP device to find additional
1882  * DP-specific quirks
1883  * @edid: The EDID to check
1884  *
1885  * While OUIDs are meant to be used to recognize a DisplayPort device, a lot
1886  * of manufacturers don't seem to like following standards and neglect to fill
1887  * the dev-ID in, making it impossible to only use OUIDs for determining
1888  * quirks in some cases. This function can be used to check the EDID and look
1889  * up any additional DP quirks. The bits returned by this function correspond
1890  * to the quirk bits in &drm_dp_quirk.
1891  *
1892  * Returns: a bitmask of quirks, if any. The driver can check this using
1893  * drm_dp_has_quirk().
1894  */
1895 u32 drm_dp_get_edid_quirks(const struct edid *edid)
1896 {
1897         const struct edid_quirk *quirk;
1898         u32 quirks = 0;
1899         int i;
1900
1901         if (!edid)
1902                 return 0;
1903
1904         for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
1905                 quirk = &edid_quirk_list[i];
1906                 if (memcmp(quirk->mfg_id, edid->mfg_id,
1907                            sizeof(edid->mfg_id)) == 0 &&
1908                     memcmp(quirk->prod_id, edid->prod_code,
1909                            sizeof(edid->prod_code)) == 0)
1910                         quirks |= quirk->quirks;
1911         }
1912
1913         DRM_DEBUG_KMS("DP sink: EDID mfg %*phD prod-ID %*phD quirks: 0x%04x\n",
1914                       (int)sizeof(edid->mfg_id), edid->mfg_id,
1915                       (int)sizeof(edid->prod_code), edid->prod_code, quirks);
1916
1917         return quirks;
1918 }
1919 EXPORT_SYMBOL(drm_dp_get_edid_quirks);
1920
1921 /**
1922  * drm_dp_read_desc - read sink/branch descriptor from DPCD
1923  * @aux: DisplayPort AUX channel
1924  * @desc: Device descriptor to fill from DPCD
1925  * @is_branch: true for branch devices, false for sink devices
1926  *
1927  * Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the
1928  * identification.
1929  *
1930  * Returns 0 on success or a negative error code on failure.
1931  */
1932 int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,
1933                      bool is_branch)
1934 {
1935         struct drm_dp_dpcd_ident *ident = &desc->ident;
1936         unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;
1937         int ret, dev_id_len;
1938
1939         ret = drm_dp_dpcd_read(aux, offset, ident, sizeof(*ident));
1940         if (ret < 0)
1941                 return ret;
1942
1943         desc->quirks = drm_dp_get_quirks(ident, is_branch);
1944
1945         dev_id_len = strnlen(ident->device_id, sizeof(ident->device_id));
1946
1947         DRM_DEBUG_KMS("%s: DP %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",
1948                       aux->name, is_branch ? "branch" : "sink",
1949                       (int)sizeof(ident->oui), ident->oui,
1950                       dev_id_len, ident->device_id,
1951                       ident->hw_rev >> 4, ident->hw_rev & 0xf,
1952                       ident->sw_major_rev, ident->sw_minor_rev,
1953                       desc->quirks);
1954
1955         return 0;
1956 }
1957 EXPORT_SYMBOL(drm_dp_read_desc);
1958
1959 /**
1960  * drm_dp_dsc_sink_max_slice_count() - Get the max slice count
1961  * supported by the DSC sink.
1962  * @dsc_dpcd: DSC capabilities from DPCD
1963  * @is_edp: true if its eDP, false for DP
1964  *
1965  * Read the slice capabilities DPCD register from DSC sink to get
1966  * the maximum slice count supported. This is used to populate
1967  * the DSC parameters in the &struct drm_dsc_config by the driver.
1968  * Driver creates an infoframe using these parameters to populate
1969  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
1970  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
1971  *
1972  * Returns:
1973  * Maximum slice count supported by DSC sink or 0 its invalid
1974  */
1975 u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
1976                                    bool is_edp)
1977 {
1978         u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
1979
1980         if (is_edp) {
1981                 /* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */
1982                 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
1983                         return 4;
1984                 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
1985                         return 2;
1986                 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
1987                         return 1;
1988         } else {
1989                 /* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */
1990                 u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
1991
1992                 if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)
1993                         return 24;
1994                 if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)
1995                         return 20;
1996                 if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)
1997                         return 16;
1998                 if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)
1999                         return 12;
2000                 if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)
2001                         return 10;
2002                 if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)
2003                         return 8;
2004                 if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)
2005                         return 6;
2006                 if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)
2007                         return 4;
2008                 if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)
2009                         return 2;
2010                 if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)
2011                         return 1;
2012         }
2013
2014         return 0;
2015 }
2016 EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);
2017
2018 /**
2019  * drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits
2020  * @dsc_dpcd: DSC capabilities from DPCD
2021  *
2022  * Read the DSC DPCD register to parse the line buffer depth in bits which is
2023  * number of bits of precision within the decoder line buffer supported by
2024  * the DSC sink. This is used to populate the DSC parameters in the
2025  * &struct drm_dsc_config by the driver.
2026  * Driver creates an infoframe using these parameters to populate
2027  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2028  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2029  *
2030  * Returns:
2031  * Line buffer depth supported by DSC panel or 0 its invalid
2032  */
2033 u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])
2034 {
2035         u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];
2036
2037         switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {
2038         case DP_DSC_LINE_BUF_BIT_DEPTH_9:
2039                 return 9;
2040         case DP_DSC_LINE_BUF_BIT_DEPTH_10:
2041                 return 10;
2042         case DP_DSC_LINE_BUF_BIT_DEPTH_11:
2043                 return 11;
2044         case DP_DSC_LINE_BUF_BIT_DEPTH_12:
2045                 return 12;
2046         case DP_DSC_LINE_BUF_BIT_DEPTH_13:
2047                 return 13;
2048         case DP_DSC_LINE_BUF_BIT_DEPTH_14:
2049                 return 14;
2050         case DP_DSC_LINE_BUF_BIT_DEPTH_15:
2051                 return 15;
2052         case DP_DSC_LINE_BUF_BIT_DEPTH_16:
2053                 return 16;
2054         case DP_DSC_LINE_BUF_BIT_DEPTH_8:
2055                 return 8;
2056         }
2057
2058         return 0;
2059 }
2060 EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);
2061
2062 /**
2063  * drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component
2064  * values supported by the DSC sink.
2065  * @dsc_dpcd: DSC capabilities from DPCD
2066  * @dsc_bpc: An array to be filled by this helper with supported
2067  *           input bpcs.
2068  *
2069  * Read the DSC DPCD from the sink device to parse the supported bits per
2070  * component values. This is used to populate the DSC parameters
2071  * in the &struct drm_dsc_config by the driver.
2072  * Driver creates an infoframe using these parameters to populate
2073  * &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC
2074  * infoframe using the helper function drm_dsc_pps_infoframe_pack()
2075  *
2076  * Returns:
2077  * Number of input BPC values parsed from the DPCD
2078  */
2079 int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],
2080                                          u8 dsc_bpc[3])
2081 {
2082         int num_bpc = 0;
2083         u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
2084
2085         if (color_depth & DP_DSC_12_BPC)
2086                 dsc_bpc[num_bpc++] = 12;
2087         if (color_depth & DP_DSC_10_BPC)
2088                 dsc_bpc[num_bpc++] = 10;
2089         if (color_depth & DP_DSC_8_BPC)
2090                 dsc_bpc[num_bpc++] = 8;
2091
2092         return num_bpc;
2093 }
2094 EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);
2095
2096 /**
2097  * drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.
2098  * @aux: DisplayPort AUX channel
2099  * @data: DP phy compliance test parameters.
2100  *
2101  * Returns 0 on success or a negative error code on failure.
2102  */
2103 int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,
2104                                 struct drm_dp_phy_test_params *data)
2105 {
2106         int err;
2107         u8 rate, lanes;
2108
2109         err = drm_dp_dpcd_readb(aux, DP_TEST_LINK_RATE, &rate);
2110         if (err < 0)
2111                 return err;
2112         data->link_rate = drm_dp_bw_code_to_link_rate(rate);
2113
2114         err = drm_dp_dpcd_readb(aux, DP_TEST_LANE_COUNT, &lanes);
2115         if (err < 0)
2116                 return err;
2117         data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;
2118
2119         if (lanes & DP_ENHANCED_FRAME_CAP)
2120                 data->enhanced_frame_cap = true;
2121
2122         err = drm_dp_dpcd_readb(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);
2123         if (err < 0)
2124                 return err;
2125
2126         switch (data->phy_pattern) {
2127         case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:
2128                 err = drm_dp_dpcd_read(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,
2129                                        &data->custom80, sizeof(data->custom80));
2130                 if (err < 0)
2131                         return err;
2132
2133                 break;
2134         case DP_PHY_TEST_PATTERN_CP2520:
2135                 err = drm_dp_dpcd_read(aux, DP_TEST_HBR2_SCRAMBLER_RESET,
2136                                        &data->hbr2_reset,
2137                                        sizeof(data->hbr2_reset));
2138                 if (err < 0)
2139                         return err;
2140         }
2141
2142         return 0;
2143 }
2144 EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);
2145
2146 /**
2147  * drm_dp_set_phy_test_pattern() - set the pattern to the sink.
2148  * @aux: DisplayPort AUX channel
2149  * @data: DP phy compliance test parameters.
2150  * @dp_rev: DP revision to use for compliance testing
2151  *
2152  * Returns 0 on success or a negative error code on failure.
2153  */
2154 int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,
2155                                 struct drm_dp_phy_test_params *data, u8 dp_rev)
2156 {
2157         int err, i;
2158         u8 link_config[2];
2159         u8 test_pattern;
2160
2161         link_config[0] = drm_dp_link_rate_to_bw_code(data->link_rate);
2162         link_config[1] = data->num_lanes;
2163         if (data->enhanced_frame_cap)
2164                 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
2165         err = drm_dp_dpcd_write(aux, DP_LINK_BW_SET, link_config, 2);
2166         if (err < 0)
2167                 return err;
2168
2169         test_pattern = data->phy_pattern;
2170         if (dp_rev < 0x12) {
2171                 test_pattern = (test_pattern << 2) &
2172                                DP_LINK_QUAL_PATTERN_11_MASK;
2173                 err = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET,
2174                                          test_pattern);
2175                 if (err < 0)
2176                         return err;
2177         } else {
2178                 for (i = 0; i < data->num_lanes; i++) {
2179                         err = drm_dp_dpcd_writeb(aux,
2180                                                  DP_LINK_QUAL_LANE0_SET + i,
2181                                                  test_pattern);
2182                         if (err < 0)
2183                                 return err;
2184                 }
2185         }
2186
2187         return 0;
2188 }
2189 EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);
2190
2191 static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)
2192 {
2193         if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2194                 return "Invalid";
2195
2196         switch (pixelformat) {
2197         case DP_PIXELFORMAT_RGB:
2198                 return "RGB";
2199         case DP_PIXELFORMAT_YUV444:
2200                 return "YUV444";
2201         case DP_PIXELFORMAT_YUV422:
2202                 return "YUV422";
2203         case DP_PIXELFORMAT_YUV420:
2204                 return "YUV420";
2205         case DP_PIXELFORMAT_Y_ONLY:
2206                 return "Y_ONLY";
2207         case DP_PIXELFORMAT_RAW:
2208                 return "RAW";
2209         default:
2210                 return "Reserved";
2211         }
2212 }
2213
2214 static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,
2215                                            enum dp_colorimetry colorimetry)
2216 {
2217         if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)
2218                 return "Invalid";
2219
2220         switch (colorimetry) {
2221         case DP_COLORIMETRY_DEFAULT:
2222                 switch (pixelformat) {
2223                 case DP_PIXELFORMAT_RGB:
2224                         return "sRGB";
2225                 case DP_PIXELFORMAT_YUV444:
2226                 case DP_PIXELFORMAT_YUV422:
2227                 case DP_PIXELFORMAT_YUV420:
2228                         return "BT.601";
2229                 case DP_PIXELFORMAT_Y_ONLY:
2230                         return "DICOM PS3.14";
2231                 case DP_PIXELFORMAT_RAW:
2232                         return "Custom Color Profile";
2233                 default:
2234                         return "Reserved";
2235                 }
2236         case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */
2237                 switch (pixelformat) {
2238                 case DP_PIXELFORMAT_RGB:
2239                         return "Wide Fixed";
2240                 case DP_PIXELFORMAT_YUV444:
2241                 case DP_PIXELFORMAT_YUV422:
2242                 case DP_PIXELFORMAT_YUV420:
2243                         return "BT.709";
2244                 default:
2245                         return "Reserved";
2246                 }
2247         case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */
2248                 switch (pixelformat) {
2249                 case DP_PIXELFORMAT_RGB:
2250                         return "Wide Float";
2251                 case DP_PIXELFORMAT_YUV444:
2252                 case DP_PIXELFORMAT_YUV422:
2253                 case DP_PIXELFORMAT_YUV420:
2254                         return "xvYCC 601";
2255                 default:
2256                         return "Reserved";
2257                 }
2258         case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */
2259                 switch (pixelformat) {
2260                 case DP_PIXELFORMAT_RGB:
2261                         return "OpRGB";
2262                 case DP_PIXELFORMAT_YUV444:
2263                 case DP_PIXELFORMAT_YUV422:
2264                 case DP_PIXELFORMAT_YUV420:
2265                         return "xvYCC 709";
2266                 default:
2267                         return "Reserved";
2268                 }
2269         case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */
2270                 switch (pixelformat) {
2271                 case DP_PIXELFORMAT_RGB:
2272                         return "DCI-P3";
2273                 case DP_PIXELFORMAT_YUV444:
2274                 case DP_PIXELFORMAT_YUV422:
2275                 case DP_PIXELFORMAT_YUV420:
2276                         return "sYCC 601";
2277                 default:
2278                         return "Reserved";
2279                 }
2280         case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */
2281                 switch (pixelformat) {
2282                 case DP_PIXELFORMAT_RGB:
2283                         return "Custom Profile";
2284                 case DP_PIXELFORMAT_YUV444:
2285                 case DP_PIXELFORMAT_YUV422:
2286                 case DP_PIXELFORMAT_YUV420:
2287                         return "OpYCC 601";
2288                 default:
2289                         return "Reserved";
2290                 }
2291         case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */
2292                 switch (pixelformat) {
2293                 case DP_PIXELFORMAT_RGB:
2294                         return "BT.2020 RGB";
2295                 case DP_PIXELFORMAT_YUV444:
2296                 case DP_PIXELFORMAT_YUV422:
2297                 case DP_PIXELFORMAT_YUV420:
2298                         return "BT.2020 CYCC";
2299                 default:
2300                         return "Reserved";
2301                 }
2302         case DP_COLORIMETRY_BT2020_YCC:
2303                 switch (pixelformat) {
2304                 case DP_PIXELFORMAT_YUV444:
2305                 case DP_PIXELFORMAT_YUV422:
2306                 case DP_PIXELFORMAT_YUV420:
2307                         return "BT.2020 YCC";
2308                 default:
2309                         return "Reserved";
2310                 }
2311         default:
2312                 return "Invalid";
2313         }
2314 }
2315
2316 static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)
2317 {
2318         switch (dynamic_range) {
2319         case DP_DYNAMIC_RANGE_VESA:
2320                 return "VESA range";
2321         case DP_DYNAMIC_RANGE_CTA:
2322                 return "CTA range";
2323         default:
2324                 return "Invalid";
2325         }
2326 }
2327
2328 static const char *dp_content_type_get_name(enum dp_content_type content_type)
2329 {
2330         switch (content_type) {
2331         case DP_CONTENT_TYPE_NOT_DEFINED:
2332                 return "Not defined";
2333         case DP_CONTENT_TYPE_GRAPHICS:
2334                 return "Graphics";
2335         case DP_CONTENT_TYPE_PHOTO:
2336                 return "Photo";
2337         case DP_CONTENT_TYPE_VIDEO:
2338                 return "Video";
2339         case DP_CONTENT_TYPE_GAME:
2340                 return "Game";
2341         default:
2342                 return "Reserved";
2343         }
2344 }
2345
2346 void drm_dp_vsc_sdp_log(const char *level, struct device *dev,
2347                         const struct drm_dp_vsc_sdp *vsc)
2348 {
2349 #define DP_SDP_LOG(fmt, ...) dev_printk(level, dev, fmt, ##__VA_ARGS__)
2350         DP_SDP_LOG("DP SDP: %s, revision %u, length %u\n", "VSC",
2351                    vsc->revision, vsc->length);
2352         DP_SDP_LOG("    pixelformat: %s\n",
2353                    dp_pixelformat_get_name(vsc->pixelformat));
2354         DP_SDP_LOG("    colorimetry: %s\n",
2355                    dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));
2356         DP_SDP_LOG("    bpc: %u\n", vsc->bpc);
2357         DP_SDP_LOG("    dynamic range: %s\n",
2358                    dp_dynamic_range_get_name(vsc->dynamic_range));
2359         DP_SDP_LOG("    content type: %s\n",
2360                    dp_content_type_get_name(vsc->content_type));
2361 #undef DP_SDP_LOG
2362 }
2363 EXPORT_SYMBOL(drm_dp_vsc_sdp_log);