d85f9eb301bd8495493869645d365b9d18ae37f0
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_edid.c
1 /*
2  * Copyright (c) 2006 Luc Verhaegen (quirks list)
3  * Copyright (c) 2007-2008 Intel Corporation
4  *   Jesse Barnes <jesse.barnes@intel.com>
5  * Copyright 2010 Red Hat, Inc.
6  *
7  * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
8  * FB layer.
9  *   Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sub license,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the
19  * next paragraph) shall be included in all copies or substantial portions
20  * of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30
31 #include <linux/bitfield.h>
32 #include <linux/hdmi.h>
33 #include <linux/i2c.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/slab.h>
38 #include <linux/vga_switcheroo.h>
39
40 #include <drm/drm_displayid.h>
41 #include <drm/drm_drv.h>
42 #include <drm/drm_edid.h>
43 #include <drm/drm_encoder.h>
44 #include <drm/drm_print.h>
45
46 #include "drm_crtc_internal.h"
47
48 #define version_greater(edid, maj, min) \
49         (((edid)->version > (maj)) || \
50          ((edid)->version == (maj) && (edid)->revision > (min)))
51
52 static int oui(u8 first, u8 second, u8 third)
53 {
54         return (first << 16) | (second << 8) | third;
55 }
56
57 #define EDID_EST_TIMINGS 16
58 #define EDID_STD_TIMINGS 8
59 #define EDID_DETAILED_TIMINGS 4
60
61 /*
62  * EDID blocks out in the wild have a variety of bugs, try to collect
63  * them here (note that userspace may work around broken monitors first,
64  * but fixes should make their way here so that the kernel "just works"
65  * on as many displays as possible).
66  */
67
68 /* First detailed mode wrong, use largest 60Hz mode */
69 #define EDID_QUIRK_PREFER_LARGE_60              (1 << 0)
70 /* Reported 135MHz pixel clock is too high, needs adjustment */
71 #define EDID_QUIRK_135_CLOCK_TOO_HIGH           (1 << 1)
72 /* Prefer the largest mode at 75 Hz */
73 #define EDID_QUIRK_PREFER_LARGE_75              (1 << 2)
74 /* Detail timing is in cm not mm */
75 #define EDID_QUIRK_DETAILED_IN_CM               (1 << 3)
76 /* Detailed timing descriptors have bogus size values, so just take the
77  * maximum size and use that.
78  */
79 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE    (1 << 4)
80 /* use +hsync +vsync for detailed mode */
81 #define EDID_QUIRK_DETAILED_SYNC_PP             (1 << 6)
82 /* Force reduced-blanking timings for detailed modes */
83 #define EDID_QUIRK_FORCE_REDUCED_BLANKING       (1 << 7)
84 /* Force 8bpc */
85 #define EDID_QUIRK_FORCE_8BPC                   (1 << 8)
86 /* Force 12bpc */
87 #define EDID_QUIRK_FORCE_12BPC                  (1 << 9)
88 /* Force 6bpc */
89 #define EDID_QUIRK_FORCE_6BPC                   (1 << 10)
90 /* Force 10bpc */
91 #define EDID_QUIRK_FORCE_10BPC                  (1 << 11)
92 /* Non desktop display (i.e. HMD) */
93 #define EDID_QUIRK_NON_DESKTOP                  (1 << 12)
94
95 #define MICROSOFT_IEEE_OUI      0xca125c
96
97 struct detailed_mode_closure {
98         struct drm_connector *connector;
99         const struct drm_edid *drm_edid;
100         bool preferred;
101         u32 quirks;
102         int modes;
103 };
104
105 #define LEVEL_DMT       0
106 #define LEVEL_GTF       1
107 #define LEVEL_GTF2      2
108 #define LEVEL_CVT       3
109
110 #define EDID_QUIRK(vend_chr_0, vend_chr_1, vend_chr_2, product_id, _quirks) \
111 { \
112         .panel_id = drm_edid_encode_panel_id(vend_chr_0, vend_chr_1, vend_chr_2, \
113                                              product_id), \
114         .quirks = _quirks \
115 }
116
117 static const struct edid_quirk {
118         u32 panel_id;
119         u32 quirks;
120 } edid_quirk_list[] = {
121         /* Acer AL1706 */
122         EDID_QUIRK('A', 'C', 'R', 44358, EDID_QUIRK_PREFER_LARGE_60),
123         /* Acer F51 */
124         EDID_QUIRK('A', 'P', 'I', 0x7602, EDID_QUIRK_PREFER_LARGE_60),
125
126         /* AEO model 0 reports 8 bpc, but is a 6 bpc panel */
127         EDID_QUIRK('A', 'E', 'O', 0, EDID_QUIRK_FORCE_6BPC),
128
129         /* BOE model on HP Pavilion 15-n233sl reports 8 bpc, but is a 6 bpc panel */
130         EDID_QUIRK('B', 'O', 'E', 0x78b, EDID_QUIRK_FORCE_6BPC),
131
132         /* CPT panel of Asus UX303LA reports 8 bpc, but is a 6 bpc panel */
133         EDID_QUIRK('C', 'P', 'T', 0x17df, EDID_QUIRK_FORCE_6BPC),
134
135         /* SDC panel of Lenovo B50-80 reports 8 bpc, but is a 6 bpc panel */
136         EDID_QUIRK('S', 'D', 'C', 0x3652, EDID_QUIRK_FORCE_6BPC),
137
138         /* BOE model 0x0771 reports 8 bpc, but is a 6 bpc panel */
139         EDID_QUIRK('B', 'O', 'E', 0x0771, EDID_QUIRK_FORCE_6BPC),
140
141         /* Belinea 10 15 55 */
142         EDID_QUIRK('M', 'A', 'X', 1516, EDID_QUIRK_PREFER_LARGE_60),
143         EDID_QUIRK('M', 'A', 'X', 0x77e, EDID_QUIRK_PREFER_LARGE_60),
144
145         /* Envision Peripherals, Inc. EN-7100e */
146         EDID_QUIRK('E', 'P', 'I', 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH),
147         /* Envision EN2028 */
148         EDID_QUIRK('E', 'P', 'I', 8232, EDID_QUIRK_PREFER_LARGE_60),
149
150         /* Funai Electronics PM36B */
151         EDID_QUIRK('F', 'C', 'M', 13600, EDID_QUIRK_PREFER_LARGE_75 |
152                                        EDID_QUIRK_DETAILED_IN_CM),
153
154         /* LGD panel of HP zBook 17 G2, eDP 10 bpc, but reports unknown bpc */
155         EDID_QUIRK('L', 'G', 'D', 764, EDID_QUIRK_FORCE_10BPC),
156
157         /* LG Philips LCD LP154W01-A5 */
158         EDID_QUIRK('L', 'P', 'L', 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE),
159         EDID_QUIRK('L', 'P', 'L', 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE),
160
161         /* Samsung SyncMaster 205BW.  Note: irony */
162         EDID_QUIRK('S', 'A', 'M', 541, EDID_QUIRK_DETAILED_SYNC_PP),
163         /* Samsung SyncMaster 22[5-6]BW */
164         EDID_QUIRK('S', 'A', 'M', 596, EDID_QUIRK_PREFER_LARGE_60),
165         EDID_QUIRK('S', 'A', 'M', 638, EDID_QUIRK_PREFER_LARGE_60),
166
167         /* Sony PVM-2541A does up to 12 bpc, but only reports max 8 bpc */
168         EDID_QUIRK('S', 'N', 'Y', 0x2541, EDID_QUIRK_FORCE_12BPC),
169
170         /* ViewSonic VA2026w */
171         EDID_QUIRK('V', 'S', 'C', 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING),
172
173         /* Medion MD 30217 PG */
174         EDID_QUIRK('M', 'E', 'D', 0x7b8, EDID_QUIRK_PREFER_LARGE_75),
175
176         /* Lenovo G50 */
177         EDID_QUIRK('S', 'D', 'C', 18514, EDID_QUIRK_FORCE_6BPC),
178
179         /* Panel in Samsung NP700G7A-S01PL notebook reports 6bpc */
180         EDID_QUIRK('S', 'E', 'C', 0xd033, EDID_QUIRK_FORCE_8BPC),
181
182         /* Rotel RSX-1058 forwards sink's EDID but only does HDMI 1.1*/
183         EDID_QUIRK('E', 'T', 'R', 13896, EDID_QUIRK_FORCE_8BPC),
184
185         /* Valve Index Headset */
186         EDID_QUIRK('V', 'L', 'V', 0x91a8, EDID_QUIRK_NON_DESKTOP),
187         EDID_QUIRK('V', 'L', 'V', 0x91b0, EDID_QUIRK_NON_DESKTOP),
188         EDID_QUIRK('V', 'L', 'V', 0x91b1, EDID_QUIRK_NON_DESKTOP),
189         EDID_QUIRK('V', 'L', 'V', 0x91b2, EDID_QUIRK_NON_DESKTOP),
190         EDID_QUIRK('V', 'L', 'V', 0x91b3, EDID_QUIRK_NON_DESKTOP),
191         EDID_QUIRK('V', 'L', 'V', 0x91b4, EDID_QUIRK_NON_DESKTOP),
192         EDID_QUIRK('V', 'L', 'V', 0x91b5, EDID_QUIRK_NON_DESKTOP),
193         EDID_QUIRK('V', 'L', 'V', 0x91b6, EDID_QUIRK_NON_DESKTOP),
194         EDID_QUIRK('V', 'L', 'V', 0x91b7, EDID_QUIRK_NON_DESKTOP),
195         EDID_QUIRK('V', 'L', 'V', 0x91b8, EDID_QUIRK_NON_DESKTOP),
196         EDID_QUIRK('V', 'L', 'V', 0x91b9, EDID_QUIRK_NON_DESKTOP),
197         EDID_QUIRK('V', 'L', 'V', 0x91ba, EDID_QUIRK_NON_DESKTOP),
198         EDID_QUIRK('V', 'L', 'V', 0x91bb, EDID_QUIRK_NON_DESKTOP),
199         EDID_QUIRK('V', 'L', 'V', 0x91bc, EDID_QUIRK_NON_DESKTOP),
200         EDID_QUIRK('V', 'L', 'V', 0x91bd, EDID_QUIRK_NON_DESKTOP),
201         EDID_QUIRK('V', 'L', 'V', 0x91be, EDID_QUIRK_NON_DESKTOP),
202         EDID_QUIRK('V', 'L', 'V', 0x91bf, EDID_QUIRK_NON_DESKTOP),
203
204         /* HTC Vive and Vive Pro VR Headsets */
205         EDID_QUIRK('H', 'V', 'R', 0xaa01, EDID_QUIRK_NON_DESKTOP),
206         EDID_QUIRK('H', 'V', 'R', 0xaa02, EDID_QUIRK_NON_DESKTOP),
207
208         /* Oculus Rift DK1, DK2, CV1 and Rift S VR Headsets */
209         EDID_QUIRK('O', 'V', 'R', 0x0001, EDID_QUIRK_NON_DESKTOP),
210         EDID_QUIRK('O', 'V', 'R', 0x0003, EDID_QUIRK_NON_DESKTOP),
211         EDID_QUIRK('O', 'V', 'R', 0x0004, EDID_QUIRK_NON_DESKTOP),
212         EDID_QUIRK('O', 'V', 'R', 0x0012, EDID_QUIRK_NON_DESKTOP),
213
214         /* Windows Mixed Reality Headsets */
215         EDID_QUIRK('A', 'C', 'R', 0x7fce, EDID_QUIRK_NON_DESKTOP),
216         EDID_QUIRK('L', 'E', 'N', 0x0408, EDID_QUIRK_NON_DESKTOP),
217         EDID_QUIRK('F', 'U', 'J', 0x1970, EDID_QUIRK_NON_DESKTOP),
218         EDID_QUIRK('D', 'E', 'L', 0x7fce, EDID_QUIRK_NON_DESKTOP),
219         EDID_QUIRK('S', 'E', 'C', 0x144a, EDID_QUIRK_NON_DESKTOP),
220         EDID_QUIRK('A', 'U', 'S', 0xc102, EDID_QUIRK_NON_DESKTOP),
221
222         /* Sony PlayStation VR Headset */
223         EDID_QUIRK('S', 'N', 'Y', 0x0704, EDID_QUIRK_NON_DESKTOP),
224
225         /* Sensics VR Headsets */
226         EDID_QUIRK('S', 'E', 'N', 0x1019, EDID_QUIRK_NON_DESKTOP),
227
228         /* OSVR HDK and HDK2 VR Headsets */
229         EDID_QUIRK('S', 'V', 'R', 0x1019, EDID_QUIRK_NON_DESKTOP),
230 };
231
232 /*
233  * Autogenerated from the DMT spec.
234  * This table is copied from xfree86/modes/xf86EdidModes.c.
235  */
236 static const struct drm_display_mode drm_dmt_modes[] = {
237         /* 0x01 - 640x350@85Hz */
238         { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 31500, 640, 672,
239                    736, 832, 0, 350, 382, 385, 445, 0,
240                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
241         /* 0x02 - 640x400@85Hz */
242         { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 31500, 640, 672,
243                    736, 832, 0, 400, 401, 404, 445, 0,
244                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
245         /* 0x03 - 720x400@85Hz */
246         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 756,
247                    828, 936, 0, 400, 401, 404, 446, 0,
248                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
249         /* 0x04 - 640x480@60Hz */
250         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
251                    752, 800, 0, 480, 490, 492, 525, 0,
252                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
253         /* 0x05 - 640x480@72Hz */
254         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
255                    704, 832, 0, 480, 489, 492, 520, 0,
256                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
257         /* 0x06 - 640x480@75Hz */
258         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
259                    720, 840, 0, 480, 481, 484, 500, 0,
260                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
261         /* 0x07 - 640x480@85Hz */
262         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 36000, 640, 696,
263                    752, 832, 0, 480, 481, 484, 509, 0,
264                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
265         /* 0x08 - 800x600@56Hz */
266         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
267                    896, 1024, 0, 600, 601, 603, 625, 0,
268                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
269         /* 0x09 - 800x600@60Hz */
270         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
271                    968, 1056, 0, 600, 601, 605, 628, 0,
272                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
273         /* 0x0a - 800x600@72Hz */
274         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
275                    976, 1040, 0, 600, 637, 643, 666, 0,
276                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
277         /* 0x0b - 800x600@75Hz */
278         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
279                    896, 1056, 0, 600, 601, 604, 625, 0,
280                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
281         /* 0x0c - 800x600@85Hz */
282         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 56250, 800, 832,
283                    896, 1048, 0, 600, 601, 604, 631, 0,
284                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
285         /* 0x0d - 800x600@120Hz RB */
286         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 73250, 800, 848,
287                    880, 960, 0, 600, 603, 607, 636, 0,
288                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
289         /* 0x0e - 848x480@60Hz */
290         { DRM_MODE("848x480", DRM_MODE_TYPE_DRIVER, 33750, 848, 864,
291                    976, 1088, 0, 480, 486, 494, 517, 0,
292                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
293         /* 0x0f - 1024x768@43Hz, interlace */
294         { DRM_MODE("1024x768i", DRM_MODE_TYPE_DRIVER, 44900, 1024, 1032,
295                    1208, 1264, 0, 768, 768, 776, 817, 0,
296                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
297                    DRM_MODE_FLAG_INTERLACE) },
298         /* 0x10 - 1024x768@60Hz */
299         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
300                    1184, 1344, 0, 768, 771, 777, 806, 0,
301                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
302         /* 0x11 - 1024x768@70Hz */
303         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
304                    1184, 1328, 0, 768, 771, 777, 806, 0,
305                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
306         /* 0x12 - 1024x768@75Hz */
307         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78750, 1024, 1040,
308                    1136, 1312, 0, 768, 769, 772, 800, 0,
309                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
310         /* 0x13 - 1024x768@85Hz */
311         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 94500, 1024, 1072,
312                    1168, 1376, 0, 768, 769, 772, 808, 0,
313                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
314         /* 0x14 - 1024x768@120Hz RB */
315         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 115500, 1024, 1072,
316                    1104, 1184, 0, 768, 771, 775, 813, 0,
317                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
318         /* 0x15 - 1152x864@75Hz */
319         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
320                    1344, 1600, 0, 864, 865, 868, 900, 0,
321                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
322         /* 0x55 - 1280x720@60Hz */
323         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
324                    1430, 1650, 0, 720, 725, 730, 750, 0,
325                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
326         /* 0x16 - 1280x768@60Hz RB */
327         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 68250, 1280, 1328,
328                    1360, 1440, 0, 768, 771, 778, 790, 0,
329                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
330         /* 0x17 - 1280x768@60Hz */
331         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
332                    1472, 1664, 0, 768, 771, 778, 798, 0,
333                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
334         /* 0x18 - 1280x768@75Hz */
335         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 102250, 1280, 1360,
336                    1488, 1696, 0, 768, 771, 778, 805, 0,
337                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
338         /* 0x19 - 1280x768@85Hz */
339         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 117500, 1280, 1360,
340                    1496, 1712, 0, 768, 771, 778, 809, 0,
341                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
342         /* 0x1a - 1280x768@120Hz RB */
343         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 140250, 1280, 1328,
344                    1360, 1440, 0, 768, 771, 778, 813, 0,
345                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
346         /* 0x1b - 1280x800@60Hz RB */
347         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 71000, 1280, 1328,
348                    1360, 1440, 0, 800, 803, 809, 823, 0,
349                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
350         /* 0x1c - 1280x800@60Hz */
351         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
352                    1480, 1680, 0, 800, 803, 809, 831, 0,
353                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
354         /* 0x1d - 1280x800@75Hz */
355         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 106500, 1280, 1360,
356                    1488, 1696, 0, 800, 803, 809, 838, 0,
357                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
358         /* 0x1e - 1280x800@85Hz */
359         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 122500, 1280, 1360,
360                    1496, 1712, 0, 800, 803, 809, 843, 0,
361                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
362         /* 0x1f - 1280x800@120Hz RB */
363         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 146250, 1280, 1328,
364                    1360, 1440, 0, 800, 803, 809, 847, 0,
365                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
366         /* 0x20 - 1280x960@60Hz */
367         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
368                    1488, 1800, 0, 960, 961, 964, 1000, 0,
369                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
370         /* 0x21 - 1280x960@85Hz */
371         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1344,
372                    1504, 1728, 0, 960, 961, 964, 1011, 0,
373                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
374         /* 0x22 - 1280x960@120Hz RB */
375         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 175500, 1280, 1328,
376                    1360, 1440, 0, 960, 963, 967, 1017, 0,
377                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
378         /* 0x23 - 1280x1024@60Hz */
379         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
380                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
381                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
382         /* 0x24 - 1280x1024@75Hz */
383         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
384                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
385                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
386         /* 0x25 - 1280x1024@85Hz */
387         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 157500, 1280, 1344,
388                    1504, 1728, 0, 1024, 1025, 1028, 1072, 0,
389                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
390         /* 0x26 - 1280x1024@120Hz RB */
391         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 187250, 1280, 1328,
392                    1360, 1440, 0, 1024, 1027, 1034, 1084, 0,
393                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
394         /* 0x27 - 1360x768@60Hz */
395         { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
396                    1536, 1792, 0, 768, 771, 777, 795, 0,
397                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
398         /* 0x28 - 1360x768@120Hz RB */
399         { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 148250, 1360, 1408,
400                    1440, 1520, 0, 768, 771, 776, 813, 0,
401                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
402         /* 0x51 - 1366x768@60Hz */
403         { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 85500, 1366, 1436,
404                    1579, 1792, 0, 768, 771, 774, 798, 0,
405                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
406         /* 0x56 - 1366x768@60Hz */
407         { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 72000, 1366, 1380,
408                    1436, 1500, 0, 768, 769, 772, 800, 0,
409                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
410         /* 0x29 - 1400x1050@60Hz RB */
411         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 101000, 1400, 1448,
412                    1480, 1560, 0, 1050, 1053, 1057, 1080, 0,
413                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
414         /* 0x2a - 1400x1050@60Hz */
415         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
416                    1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
417                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
418         /* 0x2b - 1400x1050@75Hz */
419         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 156000, 1400, 1504,
420                    1648, 1896, 0, 1050, 1053, 1057, 1099, 0,
421                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
422         /* 0x2c - 1400x1050@85Hz */
423         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 179500, 1400, 1504,
424                    1656, 1912, 0, 1050, 1053, 1057, 1105, 0,
425                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
426         /* 0x2d - 1400x1050@120Hz RB */
427         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 208000, 1400, 1448,
428                    1480, 1560, 0, 1050, 1053, 1057, 1112, 0,
429                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
430         /* 0x2e - 1440x900@60Hz RB */
431         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 88750, 1440, 1488,
432                    1520, 1600, 0, 900, 903, 909, 926, 0,
433                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
434         /* 0x2f - 1440x900@60Hz */
435         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
436                    1672, 1904, 0, 900, 903, 909, 934, 0,
437                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
438         /* 0x30 - 1440x900@75Hz */
439         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 136750, 1440, 1536,
440                    1688, 1936, 0, 900, 903, 909, 942, 0,
441                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
442         /* 0x31 - 1440x900@85Hz */
443         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 157000, 1440, 1544,
444                    1696, 1952, 0, 900, 903, 909, 948, 0,
445                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
446         /* 0x32 - 1440x900@120Hz RB */
447         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 182750, 1440, 1488,
448                    1520, 1600, 0, 900, 903, 909, 953, 0,
449                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
450         /* 0x53 - 1600x900@60Hz */
451         { DRM_MODE("1600x900", DRM_MODE_TYPE_DRIVER, 108000, 1600, 1624,
452                    1704, 1800, 0, 900, 901, 904, 1000, 0,
453                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
454         /* 0x33 - 1600x1200@60Hz */
455         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
456                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
457                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
458         /* 0x34 - 1600x1200@65Hz */
459         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 175500, 1600, 1664,
460                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
461                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
462         /* 0x35 - 1600x1200@70Hz */
463         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 189000, 1600, 1664,
464                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
465                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
466         /* 0x36 - 1600x1200@75Hz */
467         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 202500, 1600, 1664,
468                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
469                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
470         /* 0x37 - 1600x1200@85Hz */
471         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 229500, 1600, 1664,
472                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
473                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
474         /* 0x38 - 1600x1200@120Hz RB */
475         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 268250, 1600, 1648,
476                    1680, 1760, 0, 1200, 1203, 1207, 1271, 0,
477                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
478         /* 0x39 - 1680x1050@60Hz RB */
479         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 119000, 1680, 1728,
480                    1760, 1840, 0, 1050, 1053, 1059, 1080, 0,
481                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
482         /* 0x3a - 1680x1050@60Hz */
483         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
484                    1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
485                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
486         /* 0x3b - 1680x1050@75Hz */
487         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 187000, 1680, 1800,
488                    1976, 2272, 0, 1050, 1053, 1059, 1099, 0,
489                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
490         /* 0x3c - 1680x1050@85Hz */
491         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 214750, 1680, 1808,
492                    1984, 2288, 0, 1050, 1053, 1059, 1105, 0,
493                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
494         /* 0x3d - 1680x1050@120Hz RB */
495         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 245500, 1680, 1728,
496                    1760, 1840, 0, 1050, 1053, 1059, 1112, 0,
497                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
498         /* 0x3e - 1792x1344@60Hz */
499         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
500                    2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
501                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
502         /* 0x3f - 1792x1344@75Hz */
503         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 261000, 1792, 1888,
504                    2104, 2456, 0, 1344, 1345, 1348, 1417, 0,
505                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
506         /* 0x40 - 1792x1344@120Hz RB */
507         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 333250, 1792, 1840,
508                    1872, 1952, 0, 1344, 1347, 1351, 1423, 0,
509                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
510         /* 0x41 - 1856x1392@60Hz */
511         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
512                    2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
513                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
514         /* 0x42 - 1856x1392@75Hz */
515         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 288000, 1856, 1984,
516                    2208, 2560, 0, 1392, 1393, 1396, 1500, 0,
517                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
518         /* 0x43 - 1856x1392@120Hz RB */
519         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 356500, 1856, 1904,
520                    1936, 2016, 0, 1392, 1395, 1399, 1474, 0,
521                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
522         /* 0x52 - 1920x1080@60Hz */
523         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
524                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
525                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
526         /* 0x44 - 1920x1200@60Hz RB */
527         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 154000, 1920, 1968,
528                    2000, 2080, 0, 1200, 1203, 1209, 1235, 0,
529                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
530         /* 0x45 - 1920x1200@60Hz */
531         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
532                    2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
533                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
534         /* 0x46 - 1920x1200@75Hz */
535         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 245250, 1920, 2056,
536                    2264, 2608, 0, 1200, 1203, 1209, 1255, 0,
537                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
538         /* 0x47 - 1920x1200@85Hz */
539         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 281250, 1920, 2064,
540                    2272, 2624, 0, 1200, 1203, 1209, 1262, 0,
541                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
542         /* 0x48 - 1920x1200@120Hz RB */
543         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 317000, 1920, 1968,
544                    2000, 2080, 0, 1200, 1203, 1209, 1271, 0,
545                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
546         /* 0x49 - 1920x1440@60Hz */
547         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
548                    2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
549                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
550         /* 0x4a - 1920x1440@75Hz */
551         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2064,
552                    2288, 2640, 0, 1440, 1441, 1444, 1500, 0,
553                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
554         /* 0x4b - 1920x1440@120Hz RB */
555         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 380500, 1920, 1968,
556                    2000, 2080, 0, 1440, 1443, 1447, 1525, 0,
557                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
558         /* 0x54 - 2048x1152@60Hz */
559         { DRM_MODE("2048x1152", DRM_MODE_TYPE_DRIVER, 162000, 2048, 2074,
560                    2154, 2250, 0, 1152, 1153, 1156, 1200, 0,
561                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
562         /* 0x4c - 2560x1600@60Hz RB */
563         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 268500, 2560, 2608,
564                    2640, 2720, 0, 1600, 1603, 1609, 1646, 0,
565                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
566         /* 0x4d - 2560x1600@60Hz */
567         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
568                    3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
569                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
570         /* 0x4e - 2560x1600@75Hz */
571         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 443250, 2560, 2768,
572                    3048, 3536, 0, 1600, 1603, 1609, 1672, 0,
573                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
574         /* 0x4f - 2560x1600@85Hz */
575         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 505250, 2560, 2768,
576                    3048, 3536, 0, 1600, 1603, 1609, 1682, 0,
577                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
578         /* 0x50 - 2560x1600@120Hz RB */
579         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 552750, 2560, 2608,
580                    2640, 2720, 0, 1600, 1603, 1609, 1694, 0,
581                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
582         /* 0x57 - 4096x2160@60Hz RB */
583         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 556744, 4096, 4104,
584                    4136, 4176, 0, 2160, 2208, 2216, 2222, 0,
585                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
586         /* 0x58 - 4096x2160@59.94Hz RB */
587         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 556188, 4096, 4104,
588                    4136, 4176, 0, 2160, 2208, 2216, 2222, 0,
589                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
590 };
591
592 /*
593  * These more or less come from the DMT spec.  The 720x400 modes are
594  * inferred from historical 80x25 practice.  The 640x480@67 and 832x624@75
595  * modes are old-school Mac modes.  The EDID spec says the 1152x864@75 mode
596  * should be 1152x870, again for the Mac, but instead we use the x864 DMT
597  * mode.
598  *
599  * The DMT modes have been fact-checked; the rest are mild guesses.
600  */
601 static const struct drm_display_mode edid_est_modes[] = {
602         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
603                    968, 1056, 0, 600, 601, 605, 628, 0,
604                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
605         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
606                    896, 1024, 0, 600, 601, 603,  625, 0,
607                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
608         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
609                    720, 840, 0, 480, 481, 484, 500, 0,
610                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
611         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
612                    704,  832, 0, 480, 489, 492, 520, 0,
613                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
614         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
615                    768,  864, 0, 480, 483, 486, 525, 0,
616                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
617         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
618                    752, 800, 0, 480, 490, 492, 525, 0,
619                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
620         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
621                    846, 900, 0, 400, 421, 423,  449, 0,
622                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
623         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
624                    846,  900, 0, 400, 412, 414, 449, 0,
625                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
626         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
627                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
628                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
629         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78750, 1024, 1040,
630                    1136, 1312, 0,  768, 769, 772, 800, 0,
631                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
632         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
633                    1184, 1328, 0,  768, 771, 777, 806, 0,
634                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
635         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
636                    1184, 1344, 0,  768, 771, 777, 806, 0,
637                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
638         { DRM_MODE("1024x768i", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
639                    1208, 1264, 0, 768, 768, 776, 817, 0,
640                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
641         { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
642                    928, 1152, 0, 624, 625, 628, 667, 0,
643                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
644         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
645                    896, 1056, 0, 600, 601, 604,  625, 0,
646                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
647         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
648                    976, 1040, 0, 600, 637, 643, 666, 0,
649                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
650         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
651                    1344, 1600, 0,  864, 865, 868, 900, 0,
652                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
653 };
654
655 struct minimode {
656         short w;
657         short h;
658         short r;
659         short rb;
660 };
661
662 static const struct minimode est3_modes[] = {
663         /* byte 6 */
664         { 640, 350, 85, 0 },
665         { 640, 400, 85, 0 },
666         { 720, 400, 85, 0 },
667         { 640, 480, 85, 0 },
668         { 848, 480, 60, 0 },
669         { 800, 600, 85, 0 },
670         { 1024, 768, 85, 0 },
671         { 1152, 864, 75, 0 },
672         /* byte 7 */
673         { 1280, 768, 60, 1 },
674         { 1280, 768, 60, 0 },
675         { 1280, 768, 75, 0 },
676         { 1280, 768, 85, 0 },
677         { 1280, 960, 60, 0 },
678         { 1280, 960, 85, 0 },
679         { 1280, 1024, 60, 0 },
680         { 1280, 1024, 85, 0 },
681         /* byte 8 */
682         { 1360, 768, 60, 0 },
683         { 1440, 900, 60, 1 },
684         { 1440, 900, 60, 0 },
685         { 1440, 900, 75, 0 },
686         { 1440, 900, 85, 0 },
687         { 1400, 1050, 60, 1 },
688         { 1400, 1050, 60, 0 },
689         { 1400, 1050, 75, 0 },
690         /* byte 9 */
691         { 1400, 1050, 85, 0 },
692         { 1680, 1050, 60, 1 },
693         { 1680, 1050, 60, 0 },
694         { 1680, 1050, 75, 0 },
695         { 1680, 1050, 85, 0 },
696         { 1600, 1200, 60, 0 },
697         { 1600, 1200, 65, 0 },
698         { 1600, 1200, 70, 0 },
699         /* byte 10 */
700         { 1600, 1200, 75, 0 },
701         { 1600, 1200, 85, 0 },
702         { 1792, 1344, 60, 0 },
703         { 1792, 1344, 75, 0 },
704         { 1856, 1392, 60, 0 },
705         { 1856, 1392, 75, 0 },
706         { 1920, 1200, 60, 1 },
707         { 1920, 1200, 60, 0 },
708         /* byte 11 */
709         { 1920, 1200, 75, 0 },
710         { 1920, 1200, 85, 0 },
711         { 1920, 1440, 60, 0 },
712         { 1920, 1440, 75, 0 },
713 };
714
715 static const struct minimode extra_modes[] = {
716         { 1024, 576,  60, 0 },
717         { 1366, 768,  60, 0 },
718         { 1600, 900,  60, 0 },
719         { 1680, 945,  60, 0 },
720         { 1920, 1080, 60, 0 },
721         { 2048, 1152, 60, 0 },
722         { 2048, 1536, 60, 0 },
723 };
724
725 /*
726  * From CEA/CTA-861 spec.
727  *
728  * Do not access directly, instead always use cea_mode_for_vic().
729  */
730 static const struct drm_display_mode edid_cea_modes_1[] = {
731         /* 1 - 640x480@60Hz 4:3 */
732         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
733                    752, 800, 0, 480, 490, 492, 525, 0,
734                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
735           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
736         /* 2 - 720x480@60Hz 4:3 */
737         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
738                    798, 858, 0, 480, 489, 495, 525, 0,
739                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
740           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
741         /* 3 - 720x480@60Hz 16:9 */
742         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
743                    798, 858, 0, 480, 489, 495, 525, 0,
744                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
745           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
746         /* 4 - 1280x720@60Hz 16:9 */
747         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
748                    1430, 1650, 0, 720, 725, 730, 750, 0,
749                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
750           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
751         /* 5 - 1920x1080i@60Hz 16:9 */
752         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
753                    2052, 2200, 0, 1080, 1084, 1094, 1125, 0,
754                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
755                    DRM_MODE_FLAG_INTERLACE),
756           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
757         /* 6 - 720(1440)x480i@60Hz 4:3 */
758         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
759                    801, 858, 0, 480, 488, 494, 525, 0,
760                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
761                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
762           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
763         /* 7 - 720(1440)x480i@60Hz 16:9 */
764         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
765                    801, 858, 0, 480, 488, 494, 525, 0,
766                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
767                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
768           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
769         /* 8 - 720(1440)x240@60Hz 4:3 */
770         { DRM_MODE("720x240", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
771                    801, 858, 0, 240, 244, 247, 262, 0,
772                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
773                    DRM_MODE_FLAG_DBLCLK),
774           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
775         /* 9 - 720(1440)x240@60Hz 16:9 */
776         { DRM_MODE("720x240", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
777                    801, 858, 0, 240, 244, 247, 262, 0,
778                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
779                    DRM_MODE_FLAG_DBLCLK),
780           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
781         /* 10 - 2880x480i@60Hz 4:3 */
782         { DRM_MODE("2880x480i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
783                    3204, 3432, 0, 480, 488, 494, 525, 0,
784                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
785                    DRM_MODE_FLAG_INTERLACE),
786           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
787         /* 11 - 2880x480i@60Hz 16:9 */
788         { DRM_MODE("2880x480i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
789                    3204, 3432, 0, 480, 488, 494, 525, 0,
790                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
791                    DRM_MODE_FLAG_INTERLACE),
792           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
793         /* 12 - 2880x240@60Hz 4:3 */
794         { DRM_MODE("2880x240", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
795                    3204, 3432, 0, 240, 244, 247, 262, 0,
796                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
797           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
798         /* 13 - 2880x240@60Hz 16:9 */
799         { DRM_MODE("2880x240", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
800                    3204, 3432, 0, 240, 244, 247, 262, 0,
801                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
802           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
803         /* 14 - 1440x480@60Hz 4:3 */
804         { DRM_MODE("1440x480", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1472,
805                    1596, 1716, 0, 480, 489, 495, 525, 0,
806                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
807           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
808         /* 15 - 1440x480@60Hz 16:9 */
809         { DRM_MODE("1440x480", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1472,
810                    1596, 1716, 0, 480, 489, 495, 525, 0,
811                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
812           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
813         /* 16 - 1920x1080@60Hz 16:9 */
814         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
815                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
816                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
817           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
818         /* 17 - 720x576@50Hz 4:3 */
819         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
820                    796, 864, 0, 576, 581, 586, 625, 0,
821                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
822           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
823         /* 18 - 720x576@50Hz 16:9 */
824         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
825                    796, 864, 0, 576, 581, 586, 625, 0,
826                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
827           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
828         /* 19 - 1280x720@50Hz 16:9 */
829         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
830                    1760, 1980, 0, 720, 725, 730, 750, 0,
831                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
832           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
833         /* 20 - 1920x1080i@50Hz 16:9 */
834         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
835                    2492, 2640, 0, 1080, 1084, 1094, 1125, 0,
836                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
837                    DRM_MODE_FLAG_INTERLACE),
838           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
839         /* 21 - 720(1440)x576i@50Hz 4:3 */
840         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
841                    795, 864, 0, 576, 580, 586, 625, 0,
842                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
843                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
844           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
845         /* 22 - 720(1440)x576i@50Hz 16:9 */
846         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
847                    795, 864, 0, 576, 580, 586, 625, 0,
848                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
849                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
850           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
851         /* 23 - 720(1440)x288@50Hz 4:3 */
852         { DRM_MODE("720x288", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
853                    795, 864, 0, 288, 290, 293, 312, 0,
854                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
855                    DRM_MODE_FLAG_DBLCLK),
856           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
857         /* 24 - 720(1440)x288@50Hz 16:9 */
858         { DRM_MODE("720x288", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
859                    795, 864, 0, 288, 290, 293, 312, 0,
860                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
861                    DRM_MODE_FLAG_DBLCLK),
862           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
863         /* 25 - 2880x576i@50Hz 4:3 */
864         { DRM_MODE("2880x576i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
865                    3180, 3456, 0, 576, 580, 586, 625, 0,
866                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
867                    DRM_MODE_FLAG_INTERLACE),
868           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
869         /* 26 - 2880x576i@50Hz 16:9 */
870         { DRM_MODE("2880x576i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
871                    3180, 3456, 0, 576, 580, 586, 625, 0,
872                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
873                    DRM_MODE_FLAG_INTERLACE),
874           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
875         /* 27 - 2880x288@50Hz 4:3 */
876         { DRM_MODE("2880x288", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
877                    3180, 3456, 0, 288, 290, 293, 312, 0,
878                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
879           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
880         /* 28 - 2880x288@50Hz 16:9 */
881         { DRM_MODE("2880x288", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
882                    3180, 3456, 0, 288, 290, 293, 312, 0,
883                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
884           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
885         /* 29 - 1440x576@50Hz 4:3 */
886         { DRM_MODE("1440x576", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1464,
887                    1592, 1728, 0, 576, 581, 586, 625, 0,
888                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
889           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
890         /* 30 - 1440x576@50Hz 16:9 */
891         { DRM_MODE("1440x576", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1464,
892                    1592, 1728, 0, 576, 581, 586, 625, 0,
893                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
894           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
895         /* 31 - 1920x1080@50Hz 16:9 */
896         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
897                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
898                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
899           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
900         /* 32 - 1920x1080@24Hz 16:9 */
901         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2558,
902                    2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
903                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
904           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
905         /* 33 - 1920x1080@25Hz 16:9 */
906         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
907                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
908                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
909           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
910         /* 34 - 1920x1080@30Hz 16:9 */
911         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
912                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
913                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
914           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
915         /* 35 - 2880x480@60Hz 4:3 */
916         { DRM_MODE("2880x480", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2944,
917                    3192, 3432, 0, 480, 489, 495, 525, 0,
918                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
919           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
920         /* 36 - 2880x480@60Hz 16:9 */
921         { DRM_MODE("2880x480", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2944,
922                    3192, 3432, 0, 480, 489, 495, 525, 0,
923                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
924           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
925         /* 37 - 2880x576@50Hz 4:3 */
926         { DRM_MODE("2880x576", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2928,
927                    3184, 3456, 0, 576, 581, 586, 625, 0,
928                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
929           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
930         /* 38 - 2880x576@50Hz 16:9 */
931         { DRM_MODE("2880x576", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2928,
932                    3184, 3456, 0, 576, 581, 586, 625, 0,
933                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
934           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
935         /* 39 - 1920x1080i@50Hz 16:9 */
936         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 72000, 1920, 1952,
937                    2120, 2304, 0, 1080, 1126, 1136, 1250, 0,
938                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC |
939                    DRM_MODE_FLAG_INTERLACE),
940           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
941         /* 40 - 1920x1080i@100Hz 16:9 */
942         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
943                    2492, 2640, 0, 1080, 1084, 1094, 1125, 0,
944                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
945                    DRM_MODE_FLAG_INTERLACE),
946           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
947         /* 41 - 1280x720@100Hz 16:9 */
948         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1720,
949                    1760, 1980, 0, 720, 725, 730, 750, 0,
950                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
951           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
952         /* 42 - 720x576@100Hz 4:3 */
953         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
954                    796, 864, 0, 576, 581, 586, 625, 0,
955                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
956           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
957         /* 43 - 720x576@100Hz 16:9 */
958         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
959                    796, 864, 0, 576, 581, 586, 625, 0,
960                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
961           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
962         /* 44 - 720(1440)x576i@100Hz 4:3 */
963         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
964                    795, 864, 0, 576, 580, 586, 625, 0,
965                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
966                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
967           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
968         /* 45 - 720(1440)x576i@100Hz 16:9 */
969         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
970                    795, 864, 0, 576, 580, 586, 625, 0,
971                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
972                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
973           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
974         /* 46 - 1920x1080i@120Hz 16:9 */
975         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
976                    2052, 2200, 0, 1080, 1084, 1094, 1125, 0,
977                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
978                    DRM_MODE_FLAG_INTERLACE),
979           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
980         /* 47 - 1280x720@120Hz 16:9 */
981         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1390,
982                    1430, 1650, 0, 720, 725, 730, 750, 0,
983                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
984           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
985         /* 48 - 720x480@120Hz 4:3 */
986         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 54000, 720, 736,
987                    798, 858, 0, 480, 489, 495, 525, 0,
988                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
989           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
990         /* 49 - 720x480@120Hz 16:9 */
991         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 54000, 720, 736,
992                    798, 858, 0, 480, 489, 495, 525, 0,
993                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
994           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
995         /* 50 - 720(1440)x480i@120Hz 4:3 */
996         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 27000, 720, 739,
997                    801, 858, 0, 480, 488, 494, 525, 0,
998                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
999                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
1000           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
1001         /* 51 - 720(1440)x480i@120Hz 16:9 */
1002         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 27000, 720, 739,
1003                    801, 858, 0, 480, 488, 494, 525, 0,
1004                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
1005                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
1006           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1007         /* 52 - 720x576@200Hz 4:3 */
1008         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 108000, 720, 732,
1009                    796, 864, 0, 576, 581, 586, 625, 0,
1010                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
1011           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
1012         /* 53 - 720x576@200Hz 16:9 */
1013         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 108000, 720, 732,
1014                    796, 864, 0, 576, 581, 586, 625, 0,
1015                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
1016           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1017         /* 54 - 720(1440)x576i@200Hz 4:3 */
1018         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
1019                    795, 864, 0, 576, 580, 586, 625, 0,
1020                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
1021                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
1022           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
1023         /* 55 - 720(1440)x576i@200Hz 16:9 */
1024         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
1025                    795, 864, 0, 576, 580, 586, 625, 0,
1026                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
1027                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
1028           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1029         /* 56 - 720x480@240Hz 4:3 */
1030         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 108000, 720, 736,
1031                    798, 858, 0, 480, 489, 495, 525, 0,
1032                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
1033           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
1034         /* 57 - 720x480@240Hz 16:9 */
1035         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 108000, 720, 736,
1036                    798, 858, 0, 480, 489, 495, 525, 0,
1037                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
1038           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1039         /* 58 - 720(1440)x480i@240Hz 4:3 */
1040         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 54000, 720, 739,
1041                    801, 858, 0, 480, 488, 494, 525, 0,
1042                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
1043                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
1044           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
1045         /* 59 - 720(1440)x480i@240Hz 16:9 */
1046         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 54000, 720, 739,
1047                    801, 858, 0, 480, 488, 494, 525, 0,
1048                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
1049                    DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
1050           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1051         /* 60 - 1280x720@24Hz 16:9 */
1052         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 59400, 1280, 3040,
1053                    3080, 3300, 0, 720, 725, 730, 750, 0,
1054                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1055           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1056         /* 61 - 1280x720@25Hz 16:9 */
1057         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3700,
1058                    3740, 3960, 0, 720, 725, 730, 750, 0,
1059                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1060           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1061         /* 62 - 1280x720@30Hz 16:9 */
1062         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3040,
1063                    3080, 3300, 0, 720, 725, 730, 750, 0,
1064                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1065           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1066         /* 63 - 1920x1080@120Hz 16:9 */
1067         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2008,
1068                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1069                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1070           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1071         /* 64 - 1920x1080@100Hz 16:9 */
1072         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2448,
1073                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1074                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1075           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1076         /* 65 - 1280x720@24Hz 64:27 */
1077         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 59400, 1280, 3040,
1078                    3080, 3300, 0, 720, 725, 730, 750, 0,
1079                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1080           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1081         /* 66 - 1280x720@25Hz 64:27 */
1082         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3700,
1083                    3740, 3960, 0, 720, 725, 730, 750, 0,
1084                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1085           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1086         /* 67 - 1280x720@30Hz 64:27 */
1087         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3040,
1088                    3080, 3300, 0, 720, 725, 730, 750, 0,
1089                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1090           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1091         /* 68 - 1280x720@50Hz 64:27 */
1092         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
1093                    1760, 1980, 0, 720, 725, 730, 750, 0,
1094                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1095           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1096         /* 69 - 1280x720@60Hz 64:27 */
1097         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
1098                    1430, 1650, 0, 720, 725, 730, 750, 0,
1099                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1100           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1101         /* 70 - 1280x720@100Hz 64:27 */
1102         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1720,
1103                    1760, 1980, 0, 720, 725, 730, 750, 0,
1104                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1105           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1106         /* 71 - 1280x720@120Hz 64:27 */
1107         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1390,
1108                    1430, 1650, 0, 720, 725, 730, 750, 0,
1109                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1110           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1111         /* 72 - 1920x1080@24Hz 64:27 */
1112         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2558,
1113                    2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
1114                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1115           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1116         /* 73 - 1920x1080@25Hz 64:27 */
1117         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
1118                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1119                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1120           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1121         /* 74 - 1920x1080@30Hz 64:27 */
1122         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
1123                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1124                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1125           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1126         /* 75 - 1920x1080@50Hz 64:27 */
1127         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
1128                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1129                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1130           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1131         /* 76 - 1920x1080@60Hz 64:27 */
1132         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
1133                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1134                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1135           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1136         /* 77 - 1920x1080@100Hz 64:27 */
1137         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2448,
1138                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1139                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1140           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1141         /* 78 - 1920x1080@120Hz 64:27 */
1142         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2008,
1143                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1144                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1145           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1146         /* 79 - 1680x720@24Hz 64:27 */
1147         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 59400, 1680, 3040,
1148                    3080, 3300, 0, 720, 725, 730, 750, 0,
1149                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1150           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1151         /* 80 - 1680x720@25Hz 64:27 */
1152         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 59400, 1680, 2908,
1153                    2948, 3168, 0, 720, 725, 730, 750, 0,
1154                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1155           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1156         /* 81 - 1680x720@30Hz 64:27 */
1157         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 59400, 1680, 2380,
1158                    2420, 2640, 0, 720, 725, 730, 750, 0,
1159                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1160           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1161         /* 82 - 1680x720@50Hz 64:27 */
1162         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 82500, 1680, 1940,
1163                    1980, 2200, 0, 720, 725, 730, 750, 0,
1164                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1165           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1166         /* 83 - 1680x720@60Hz 64:27 */
1167         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 99000, 1680, 1940,
1168                    1980, 2200, 0, 720, 725, 730, 750, 0,
1169                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1170           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1171         /* 84 - 1680x720@100Hz 64:27 */
1172         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 165000, 1680, 1740,
1173                    1780, 2000, 0, 720, 725, 730, 825, 0,
1174                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1175           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1176         /* 85 - 1680x720@120Hz 64:27 */
1177         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 198000, 1680, 1740,
1178                    1780, 2000, 0, 720, 725, 730, 825, 0,
1179                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1180           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1181         /* 86 - 2560x1080@24Hz 64:27 */
1182         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 99000, 2560, 3558,
1183                    3602, 3750, 0, 1080, 1084, 1089, 1100, 0,
1184                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1185           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1186         /* 87 - 2560x1080@25Hz 64:27 */
1187         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 90000, 2560, 3008,
1188                    3052, 3200, 0, 1080, 1084, 1089, 1125, 0,
1189                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1190           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1191         /* 88 - 2560x1080@30Hz 64:27 */
1192         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 118800, 2560, 3328,
1193                    3372, 3520, 0, 1080, 1084, 1089, 1125, 0,
1194                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1195           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1196         /* 89 - 2560x1080@50Hz 64:27 */
1197         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 185625, 2560, 3108,
1198                    3152, 3300, 0, 1080, 1084, 1089, 1125, 0,
1199                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1200           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1201         /* 90 - 2560x1080@60Hz 64:27 */
1202         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 198000, 2560, 2808,
1203                    2852, 3000, 0, 1080, 1084, 1089, 1100, 0,
1204                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1205           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1206         /* 91 - 2560x1080@100Hz 64:27 */
1207         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 371250, 2560, 2778,
1208                    2822, 2970, 0, 1080, 1084, 1089, 1250, 0,
1209                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1210           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1211         /* 92 - 2560x1080@120Hz 64:27 */
1212         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 495000, 2560, 3108,
1213                    3152, 3300, 0, 1080, 1084, 1089, 1250, 0,
1214                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1215           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1216         /* 93 - 3840x2160@24Hz 16:9 */
1217         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 5116,
1218                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1219                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1220           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1221         /* 94 - 3840x2160@25Hz 16:9 */
1222         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4896,
1223                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1224                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1225           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1226         /* 95 - 3840x2160@30Hz 16:9 */
1227         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4016,
1228                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1229                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1230           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1231         /* 96 - 3840x2160@50Hz 16:9 */
1232         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4896,
1233                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1234                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1235           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1236         /* 97 - 3840x2160@60Hz 16:9 */
1237         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4016,
1238                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1239                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1240           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1241         /* 98 - 4096x2160@24Hz 256:135 */
1242         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000, 4096, 5116,
1243                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1244                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1245           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1246         /* 99 - 4096x2160@25Hz 256:135 */
1247         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000, 4096, 5064,
1248                    5152, 5280, 0, 2160, 2168, 2178, 2250, 0,
1249                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1250           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1251         /* 100 - 4096x2160@30Hz 256:135 */
1252         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000, 4096, 4184,
1253                    4272, 4400, 0, 2160, 2168, 2178, 2250, 0,
1254                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1255           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1256         /* 101 - 4096x2160@50Hz 256:135 */
1257         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 594000, 4096, 5064,
1258                    5152, 5280, 0, 2160, 2168, 2178, 2250, 0,
1259                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1260           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1261         /* 102 - 4096x2160@60Hz 256:135 */
1262         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 594000, 4096, 4184,
1263                    4272, 4400, 0, 2160, 2168, 2178, 2250, 0,
1264                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1265           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1266         /* 103 - 3840x2160@24Hz 64:27 */
1267         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 5116,
1268                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1269                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1270           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1271         /* 104 - 3840x2160@25Hz 64:27 */
1272         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4896,
1273                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1274                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1275           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1276         /* 105 - 3840x2160@30Hz 64:27 */
1277         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4016,
1278                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1279                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1280           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1281         /* 106 - 3840x2160@50Hz 64:27 */
1282         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4896,
1283                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1284                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1285           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1286         /* 107 - 3840x2160@60Hz 64:27 */
1287         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4016,
1288                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1289                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1290           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1291         /* 108 - 1280x720@48Hz 16:9 */
1292         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 90000, 1280, 2240,
1293                    2280, 2500, 0, 720, 725, 730, 750, 0,
1294                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1295           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1296         /* 109 - 1280x720@48Hz 64:27 */
1297         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 90000, 1280, 2240,
1298                    2280, 2500, 0, 720, 725, 730, 750, 0,
1299                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1300           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1301         /* 110 - 1680x720@48Hz 64:27 */
1302         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 99000, 1680, 2490,
1303                    2530, 2750, 0, 720, 725, 730, 750, 0,
1304                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1305           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1306         /* 111 - 1920x1080@48Hz 16:9 */
1307         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2558,
1308                    2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
1309                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1310           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1311         /* 112 - 1920x1080@48Hz 64:27 */
1312         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2558,
1313                    2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
1314                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1315           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1316         /* 113 - 2560x1080@48Hz 64:27 */
1317         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 198000, 2560, 3558,
1318                    3602, 3750, 0, 1080, 1084, 1089, 1100, 0,
1319                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1320           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1321         /* 114 - 3840x2160@48Hz 16:9 */
1322         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 5116,
1323                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1324                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1325           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1326         /* 115 - 4096x2160@48Hz 256:135 */
1327         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 594000, 4096, 5116,
1328                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1329                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1330           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1331         /* 116 - 3840x2160@48Hz 64:27 */
1332         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 5116,
1333                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1334                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1335           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1336         /* 117 - 3840x2160@100Hz 16:9 */
1337         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 1188000, 3840, 4896,
1338                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1339                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1340           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1341         /* 118 - 3840x2160@120Hz 16:9 */
1342         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 1188000, 3840, 4016,
1343                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1344                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1345           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1346         /* 119 - 3840x2160@100Hz 64:27 */
1347         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 1188000, 3840, 4896,
1348                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1349                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1350           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1351         /* 120 - 3840x2160@120Hz 64:27 */
1352         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 1188000, 3840, 4016,
1353                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1354                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1355           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1356         /* 121 - 5120x2160@24Hz 64:27 */
1357         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 396000, 5120, 7116,
1358                    7204, 7500, 0, 2160, 2168, 2178, 2200, 0,
1359                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1360           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1361         /* 122 - 5120x2160@25Hz 64:27 */
1362         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 396000, 5120, 6816,
1363                    6904, 7200, 0, 2160, 2168, 2178, 2200, 0,
1364                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1365           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1366         /* 123 - 5120x2160@30Hz 64:27 */
1367         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 396000, 5120, 5784,
1368                    5872, 6000, 0, 2160, 2168, 2178, 2200, 0,
1369                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1370           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1371         /* 124 - 5120x2160@48Hz 64:27 */
1372         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 742500, 5120, 5866,
1373                    5954, 6250, 0, 2160, 2168, 2178, 2475, 0,
1374                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1375           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1376         /* 125 - 5120x2160@50Hz 64:27 */
1377         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 742500, 5120, 6216,
1378                    6304, 6600, 0, 2160, 2168, 2178, 2250, 0,
1379                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1380           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1381         /* 126 - 5120x2160@60Hz 64:27 */
1382         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 742500, 5120, 5284,
1383                    5372, 5500, 0, 2160, 2168, 2178, 2250, 0,
1384                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1385           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1386         /* 127 - 5120x2160@100Hz 64:27 */
1387         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 1485000, 5120, 6216,
1388                    6304, 6600, 0, 2160, 2168, 2178, 2250, 0,
1389                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1390           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1391 };
1392
1393 /*
1394  * From CEA/CTA-861 spec.
1395  *
1396  * Do not access directly, instead always use cea_mode_for_vic().
1397  */
1398 static const struct drm_display_mode edid_cea_modes_193[] = {
1399         /* 193 - 5120x2160@120Hz 64:27 */
1400         { DRM_MODE("5120x2160", DRM_MODE_TYPE_DRIVER, 1485000, 5120, 5284,
1401                    5372, 5500, 0, 2160, 2168, 2178, 2250, 0,
1402                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1403           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1404         /* 194 - 7680x4320@24Hz 16:9 */
1405         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 1188000, 7680, 10232,
1406                    10408, 11000, 0, 4320, 4336, 4356, 4500, 0,
1407                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1408           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1409         /* 195 - 7680x4320@25Hz 16:9 */
1410         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 1188000, 7680, 10032,
1411                    10208, 10800, 0, 4320, 4336, 4356, 4400, 0,
1412                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1413           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1414         /* 196 - 7680x4320@30Hz 16:9 */
1415         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 1188000, 7680, 8232,
1416                    8408, 9000, 0, 4320, 4336, 4356, 4400, 0,
1417                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1418           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1419         /* 197 - 7680x4320@48Hz 16:9 */
1420         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 2376000, 7680, 10232,
1421                    10408, 11000, 0, 4320, 4336, 4356, 4500, 0,
1422                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1423           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1424         /* 198 - 7680x4320@50Hz 16:9 */
1425         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 2376000, 7680, 10032,
1426                    10208, 10800, 0, 4320, 4336, 4356, 4400, 0,
1427                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1428           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1429         /* 199 - 7680x4320@60Hz 16:9 */
1430         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 2376000, 7680, 8232,
1431                    8408, 9000, 0, 4320, 4336, 4356, 4400, 0,
1432                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1433           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1434         /* 200 - 7680x4320@100Hz 16:9 */
1435         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 4752000, 7680, 9792,
1436                    9968, 10560, 0, 4320, 4336, 4356, 4500, 0,
1437                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1438           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1439         /* 201 - 7680x4320@120Hz 16:9 */
1440         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 4752000, 7680, 8032,
1441                    8208, 8800, 0, 4320, 4336, 4356, 4500, 0,
1442                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1443           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1444         /* 202 - 7680x4320@24Hz 64:27 */
1445         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 1188000, 7680, 10232,
1446                    10408, 11000, 0, 4320, 4336, 4356, 4500, 0,
1447                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1448           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1449         /* 203 - 7680x4320@25Hz 64:27 */
1450         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 1188000, 7680, 10032,
1451                    10208, 10800, 0, 4320, 4336, 4356, 4400, 0,
1452                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1453           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1454         /* 204 - 7680x4320@30Hz 64:27 */
1455         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 1188000, 7680, 8232,
1456                    8408, 9000, 0, 4320, 4336, 4356, 4400, 0,
1457                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1458           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1459         /* 205 - 7680x4320@48Hz 64:27 */
1460         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 2376000, 7680, 10232,
1461                    10408, 11000, 0, 4320, 4336, 4356, 4500, 0,
1462                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1463           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1464         /* 206 - 7680x4320@50Hz 64:27 */
1465         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 2376000, 7680, 10032,
1466                    10208, 10800, 0, 4320, 4336, 4356, 4400, 0,
1467                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1468           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1469         /* 207 - 7680x4320@60Hz 64:27 */
1470         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 2376000, 7680, 8232,
1471                    8408, 9000, 0, 4320, 4336, 4356, 4400, 0,
1472                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1473           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1474         /* 208 - 7680x4320@100Hz 64:27 */
1475         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 4752000, 7680, 9792,
1476                    9968, 10560, 0, 4320, 4336, 4356, 4500, 0,
1477                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1478           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1479         /* 209 - 7680x4320@120Hz 64:27 */
1480         { DRM_MODE("7680x4320", DRM_MODE_TYPE_DRIVER, 4752000, 7680, 8032,
1481                    8208, 8800, 0, 4320, 4336, 4356, 4500, 0,
1482                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1483           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1484         /* 210 - 10240x4320@24Hz 64:27 */
1485         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 1485000, 10240, 11732,
1486                    11908, 12500, 0, 4320, 4336, 4356, 4950, 0,
1487                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1488           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1489         /* 211 - 10240x4320@25Hz 64:27 */
1490         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 1485000, 10240, 12732,
1491                    12908, 13500, 0, 4320, 4336, 4356, 4400, 0,
1492                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1493           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1494         /* 212 - 10240x4320@30Hz 64:27 */
1495         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 1485000, 10240, 10528,
1496                    10704, 11000, 0, 4320, 4336, 4356, 4500, 0,
1497                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1498           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1499         /* 213 - 10240x4320@48Hz 64:27 */
1500         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 2970000, 10240, 11732,
1501                    11908, 12500, 0, 4320, 4336, 4356, 4950, 0,
1502                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1503           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1504         /* 214 - 10240x4320@50Hz 64:27 */
1505         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 2970000, 10240, 12732,
1506                    12908, 13500, 0, 4320, 4336, 4356, 4400, 0,
1507                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1508           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1509         /* 215 - 10240x4320@60Hz 64:27 */
1510         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 2970000, 10240, 10528,
1511                    10704, 11000, 0, 4320, 4336, 4356, 4500, 0,
1512                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1513           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1514         /* 216 - 10240x4320@100Hz 64:27 */
1515         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 5940000, 10240, 12432,
1516                    12608, 13200, 0, 4320, 4336, 4356, 4500, 0,
1517                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1518           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1519         /* 217 - 10240x4320@120Hz 64:27 */
1520         { DRM_MODE("10240x4320", DRM_MODE_TYPE_DRIVER, 5940000, 10240, 10528,
1521                    10704, 11000, 0, 4320, 4336, 4356, 4500, 0,
1522                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1523           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1524         /* 218 - 4096x2160@100Hz 256:135 */
1525         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 1188000, 4096, 4896,
1526                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1527                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1528           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1529         /* 219 - 4096x2160@120Hz 256:135 */
1530         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 1188000, 4096, 4184,
1531                    4272, 4400, 0, 2160, 2168, 2178, 2250, 0,
1532                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1533           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1534 };
1535
1536 /*
1537  * HDMI 1.4 4k modes. Index using the VIC.
1538  */
1539 static const struct drm_display_mode edid_4k_modes[] = {
1540         /* 0 - dummy, VICs start at 1 */
1541         { },
1542         /* 1 - 3840x2160@30Hz */
1543         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000,
1544                    3840, 4016, 4104, 4400, 0,
1545                    2160, 2168, 2178, 2250, 0,
1546                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1547           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1548         /* 2 - 3840x2160@25Hz */
1549         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000,
1550                    3840, 4896, 4984, 5280, 0,
1551                    2160, 2168, 2178, 2250, 0,
1552                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1553           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1554         /* 3 - 3840x2160@24Hz */
1555         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000,
1556                    3840, 5116, 5204, 5500, 0,
1557                    2160, 2168, 2178, 2250, 0,
1558                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1559           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1560         /* 4 - 4096x2160@24Hz (SMPTE) */
1561         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000,
1562                    4096, 5116, 5204, 5500, 0,
1563                    2160, 2168, 2178, 2250, 0,
1564                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1565           .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1566 };
1567
1568 /*** DDC fetch and block validation ***/
1569
1570 /*
1571  * The opaque EDID type, internal to drm_edid.c.
1572  */
1573 struct drm_edid {
1574         /* Size allocated for edid */
1575         size_t size;
1576         const struct edid *edid;
1577 };
1578
1579 static int edid_extension_block_count(const struct edid *edid)
1580 {
1581         return edid->extensions;
1582 }
1583
1584 static int edid_block_count(const struct edid *edid)
1585 {
1586         return edid_extension_block_count(edid) + 1;
1587 }
1588
1589 static int edid_size_by_blocks(int num_blocks)
1590 {
1591         return num_blocks * EDID_LENGTH;
1592 }
1593
1594 static int edid_size(const struct edid *edid)
1595 {
1596         return edid_size_by_blocks(edid_block_count(edid));
1597 }
1598
1599 static const void *edid_block_data(const struct edid *edid, int index)
1600 {
1601         BUILD_BUG_ON(sizeof(*edid) != EDID_LENGTH);
1602
1603         return edid + index;
1604 }
1605
1606 static const void *edid_extension_block_data(const struct edid *edid, int index)
1607 {
1608         return edid_block_data(edid, index + 1);
1609 }
1610
1611 /*
1612  * Initializer helper for legacy interfaces, where we have no choice but to
1613  * trust edid size. Not for general purpose use.
1614  */
1615 static const struct drm_edid *drm_edid_legacy_init(struct drm_edid *drm_edid,
1616                                                    const struct edid *edid)
1617 {
1618         if (!edid)
1619                 return NULL;
1620
1621         memset(drm_edid, 0, sizeof(*drm_edid));
1622
1623         drm_edid->edid = edid;
1624         drm_edid->size = edid_size(edid);
1625
1626         return drm_edid;
1627 }
1628
1629 /*
1630  * EDID base and extension block iterator.
1631  *
1632  * struct drm_edid_iter iter;
1633  * const u8 *block;
1634  *
1635  * drm_edid_iter_begin(edid, &iter);
1636  * drm_edid_iter_for_each(block, &iter) {
1637  *         // do stuff with block
1638  * }
1639  * drm_edid_iter_end(&iter);
1640  */
1641 struct drm_edid_iter {
1642         const struct edid *edid;
1643
1644         /* Current block index. */
1645         int index;
1646 };
1647
1648 static void drm_edid_iter_begin(const struct edid *edid,
1649                                 struct drm_edid_iter *iter)
1650 {
1651         memset(iter, 0, sizeof(*iter));
1652
1653         iter->edid = edid;
1654 }
1655
1656 static const void *__drm_edid_iter_next(struct drm_edid_iter *iter)
1657 {
1658         const void *block = NULL;
1659
1660         if (!iter->edid)
1661                 return NULL;
1662
1663         if (iter->index < edid_block_count(iter->edid))
1664                 block = edid_block_data(iter->edid, iter->index++);
1665
1666         return block;
1667 }
1668
1669 #define drm_edid_iter_for_each(__block, __iter)                 \
1670         while (((__block) = __drm_edid_iter_next(__iter)))
1671
1672 static void drm_edid_iter_end(struct drm_edid_iter *iter)
1673 {
1674         memset(iter, 0, sizeof(*iter));
1675 }
1676
1677 static const u8 edid_header[] = {
1678         0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
1679 };
1680
1681 static void edid_header_fix(void *edid)
1682 {
1683         memcpy(edid, edid_header, sizeof(edid_header));
1684 }
1685
1686 /**
1687  * drm_edid_header_is_valid - sanity check the header of the base EDID block
1688  * @_edid: pointer to raw base EDID block
1689  *
1690  * Sanity check the header of the base EDID block.
1691  *
1692  * Return: 8 if the header is perfect, down to 0 if it's totally wrong.
1693  */
1694 int drm_edid_header_is_valid(const void *_edid)
1695 {
1696         const struct edid *edid = _edid;
1697         int i, score = 0;
1698
1699         for (i = 0; i < sizeof(edid_header); i++) {
1700                 if (edid->header[i] == edid_header[i])
1701                         score++;
1702         }
1703
1704         return score;
1705 }
1706 EXPORT_SYMBOL(drm_edid_header_is_valid);
1707
1708 static int edid_fixup __read_mostly = 6;
1709 module_param_named(edid_fixup, edid_fixup, int, 0400);
1710 MODULE_PARM_DESC(edid_fixup,
1711                  "Minimum number of valid EDID header bytes (0-8, default 6)");
1712
1713 static int edid_block_compute_checksum(const void *_block)
1714 {
1715         const u8 *block = _block;
1716         int i;
1717         u8 csum = 0, crc = 0;
1718
1719         for (i = 0; i < EDID_LENGTH - 1; i++)
1720                 csum += block[i];
1721
1722         crc = 0x100 - csum;
1723
1724         return crc;
1725 }
1726
1727 static int edid_block_get_checksum(const void *_block)
1728 {
1729         const struct edid *block = _block;
1730
1731         return block->checksum;
1732 }
1733
1734 static int edid_block_tag(const void *_block)
1735 {
1736         const u8 *block = _block;
1737
1738         return block[0];
1739 }
1740
1741 static bool edid_block_is_zero(const void *edid)
1742 {
1743         return !memchr_inv(edid, 0, EDID_LENGTH);
1744 }
1745
1746 /**
1747  * drm_edid_are_equal - compare two edid blobs.
1748  * @edid1: pointer to first blob
1749  * @edid2: pointer to second blob
1750  * This helper can be used during probing to determine if
1751  * edid had changed.
1752  */
1753 bool drm_edid_are_equal(const struct edid *edid1, const struct edid *edid2)
1754 {
1755         int edid1_len, edid2_len;
1756         bool edid1_present = edid1 != NULL;
1757         bool edid2_present = edid2 != NULL;
1758
1759         if (edid1_present != edid2_present)
1760                 return false;
1761
1762         if (edid1) {
1763                 edid1_len = edid_size(edid1);
1764                 edid2_len = edid_size(edid2);
1765
1766                 if (edid1_len != edid2_len)
1767                         return false;
1768
1769                 if (memcmp(edid1, edid2, edid1_len))
1770                         return false;
1771         }
1772
1773         return true;
1774 }
1775 EXPORT_SYMBOL(drm_edid_are_equal);
1776
1777 enum edid_block_status {
1778         EDID_BLOCK_OK = 0,
1779         EDID_BLOCK_READ_FAIL,
1780         EDID_BLOCK_NULL,
1781         EDID_BLOCK_ZERO,
1782         EDID_BLOCK_HEADER_CORRUPT,
1783         EDID_BLOCK_HEADER_REPAIR,
1784         EDID_BLOCK_HEADER_FIXED,
1785         EDID_BLOCK_CHECKSUM,
1786         EDID_BLOCK_VERSION,
1787 };
1788
1789 static enum edid_block_status edid_block_check(const void *_block,
1790                                                bool is_base_block)
1791 {
1792         const struct edid *block = _block;
1793
1794         if (!block)
1795                 return EDID_BLOCK_NULL;
1796
1797         if (is_base_block) {
1798                 int score = drm_edid_header_is_valid(block);
1799
1800                 if (score < clamp(edid_fixup, 0, 8)) {
1801                         if (edid_block_is_zero(block))
1802                                 return EDID_BLOCK_ZERO;
1803                         else
1804                                 return EDID_BLOCK_HEADER_CORRUPT;
1805                 }
1806
1807                 if (score < 8)
1808                         return EDID_BLOCK_HEADER_REPAIR;
1809         }
1810
1811         if (edid_block_compute_checksum(block) != edid_block_get_checksum(block)) {
1812                 if (edid_block_is_zero(block))
1813                         return EDID_BLOCK_ZERO;
1814                 else
1815                         return EDID_BLOCK_CHECKSUM;
1816         }
1817
1818         if (is_base_block) {
1819                 if (block->version != 1)
1820                         return EDID_BLOCK_VERSION;
1821         }
1822
1823         return EDID_BLOCK_OK;
1824 }
1825
1826 static bool edid_block_status_valid(enum edid_block_status status, int tag)
1827 {
1828         return status == EDID_BLOCK_OK ||
1829                 status == EDID_BLOCK_HEADER_FIXED ||
1830                 (status == EDID_BLOCK_CHECKSUM && tag == CEA_EXT);
1831 }
1832
1833 static bool edid_block_valid(const void *block, bool base)
1834 {
1835         return edid_block_status_valid(edid_block_check(block, base),
1836                                        edid_block_tag(block));
1837 }
1838
1839 static void edid_block_status_print(enum edid_block_status status,
1840                                     const struct edid *block,
1841                                     int block_num)
1842 {
1843         switch (status) {
1844         case EDID_BLOCK_OK:
1845                 break;
1846         case EDID_BLOCK_READ_FAIL:
1847                 pr_debug("EDID block %d read failed\n", block_num);
1848                 break;
1849         case EDID_BLOCK_NULL:
1850                 pr_debug("EDID block %d pointer is NULL\n", block_num);
1851                 break;
1852         case EDID_BLOCK_ZERO:
1853                 pr_notice("EDID block %d is all zeroes\n", block_num);
1854                 break;
1855         case EDID_BLOCK_HEADER_CORRUPT:
1856                 pr_notice("EDID has corrupt header\n");
1857                 break;
1858         case EDID_BLOCK_HEADER_REPAIR:
1859                 pr_debug("EDID corrupt header needs repair\n");
1860                 break;
1861         case EDID_BLOCK_HEADER_FIXED:
1862                 pr_debug("EDID corrupt header fixed\n");
1863                 break;
1864         case EDID_BLOCK_CHECKSUM:
1865                 if (edid_block_status_valid(status, edid_block_tag(block))) {
1866                         pr_debug("EDID block %d (tag 0x%02x) checksum is invalid, remainder is %d, ignoring\n",
1867                                  block_num, edid_block_tag(block),
1868                                  edid_block_compute_checksum(block));
1869                 } else {
1870                         pr_notice("EDID block %d (tag 0x%02x) checksum is invalid, remainder is %d\n",
1871                                   block_num, edid_block_tag(block),
1872                                   edid_block_compute_checksum(block));
1873                 }
1874                 break;
1875         case EDID_BLOCK_VERSION:
1876                 pr_notice("EDID has major version %d, instead of 1\n",
1877                           block->version);
1878                 break;
1879         default:
1880                 WARN(1, "EDID block %d unknown edid block status code %d\n",
1881                      block_num, status);
1882                 break;
1883         }
1884 }
1885
1886 static void edid_block_dump(const char *level, const void *block, int block_num)
1887 {
1888         enum edid_block_status status;
1889         char prefix[20];
1890
1891         status = edid_block_check(block, block_num == 0);
1892         if (status == EDID_BLOCK_ZERO)
1893                 sprintf(prefix, "\t[%02x] ZERO ", block_num);
1894         else if (!edid_block_status_valid(status, edid_block_tag(block)))
1895                 sprintf(prefix, "\t[%02x] BAD  ", block_num);
1896         else
1897                 sprintf(prefix, "\t[%02x] GOOD ", block_num);
1898
1899         print_hex_dump(level, prefix, DUMP_PREFIX_NONE, 16, 1,
1900                        block, EDID_LENGTH, false);
1901 }
1902
1903 /**
1904  * drm_edid_block_valid - Sanity check the EDID block (base or extension)
1905  * @_block: pointer to raw EDID block
1906  * @block_num: type of block to validate (0 for base, extension otherwise)
1907  * @print_bad_edid: if true, dump bad EDID blocks to the console
1908  * @edid_corrupt: if true, the header or checksum is invalid
1909  *
1910  * Validate a base or extension EDID block and optionally dump bad blocks to
1911  * the console.
1912  *
1913  * Return: True if the block is valid, false otherwise.
1914  */
1915 bool drm_edid_block_valid(u8 *_block, int block_num, bool print_bad_edid,
1916                           bool *edid_corrupt)
1917 {
1918         struct edid *block = (struct edid *)_block;
1919         enum edid_block_status status;
1920         bool is_base_block = block_num == 0;
1921         bool valid;
1922
1923         if (WARN_ON(!block))
1924                 return false;
1925
1926         status = edid_block_check(block, is_base_block);
1927         if (status == EDID_BLOCK_HEADER_REPAIR) {
1928                 DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
1929                 edid_header_fix(block);
1930
1931                 /* Retry with fixed header, update status if that worked. */
1932                 status = edid_block_check(block, is_base_block);
1933                 if (status == EDID_BLOCK_OK)
1934                         status = EDID_BLOCK_HEADER_FIXED;
1935         }
1936
1937         if (edid_corrupt) {
1938                 /*
1939                  * Unknown major version isn't corrupt but we can't use it. Only
1940                  * the base block can reset edid_corrupt to false.
1941                  */
1942                 if (is_base_block &&
1943                     (status == EDID_BLOCK_OK || status == EDID_BLOCK_VERSION))
1944                         *edid_corrupt = false;
1945                 else if (status != EDID_BLOCK_OK)
1946                         *edid_corrupt = true;
1947         }
1948
1949         edid_block_status_print(status, block, block_num);
1950
1951         /* Determine whether we can use this block with this status. */
1952         valid = edid_block_status_valid(status, edid_block_tag(block));
1953
1954         if (!valid && print_bad_edid && status != EDID_BLOCK_ZERO) {
1955                 pr_notice("Raw EDID:\n");
1956                 edid_block_dump(KERN_NOTICE, block, block_num);
1957         }
1958
1959         return valid;
1960 }
1961 EXPORT_SYMBOL(drm_edid_block_valid);
1962
1963 /**
1964  * drm_edid_is_valid - sanity check EDID data
1965  * @edid: EDID data
1966  *
1967  * Sanity-check an entire EDID record (including extensions)
1968  *
1969  * Return: True if the EDID data is valid, false otherwise.
1970  */
1971 bool drm_edid_is_valid(struct edid *edid)
1972 {
1973         int i;
1974
1975         if (!edid)
1976                 return false;
1977
1978         for (i = 0; i < edid_block_count(edid); i++) {
1979                 void *block = (void *)edid_block_data(edid, i);
1980
1981                 if (!drm_edid_block_valid(block, i, true, NULL))
1982                         return false;
1983         }
1984
1985         return true;
1986 }
1987 EXPORT_SYMBOL(drm_edid_is_valid);
1988
1989 static struct edid *edid_filter_invalid_blocks(const struct edid *edid,
1990                                                int invalid_blocks)
1991 {
1992         struct edid *new, *dest_block;
1993         int valid_extensions = edid->extensions - invalid_blocks;
1994         int i;
1995
1996         new = kmalloc(edid_size_by_blocks(valid_extensions + 1), GFP_KERNEL);
1997         if (!new)
1998                 goto out;
1999
2000         dest_block = new;
2001         for (i = 0; i < edid_block_count(edid); i++) {
2002                 const void *block = edid_block_data(edid, i);
2003
2004                 if (edid_block_valid(block, i == 0))
2005                         memcpy(dest_block++, block, EDID_LENGTH);
2006         }
2007
2008         new->extensions = valid_extensions;
2009         new->checksum = edid_block_compute_checksum(new);
2010
2011 out:
2012         kfree(edid);
2013
2014         return new;
2015 }
2016
2017 #define DDC_SEGMENT_ADDR 0x30
2018 /**
2019  * drm_do_probe_ddc_edid() - get EDID information via I2C
2020  * @data: I2C device adapter
2021  * @buf: EDID data buffer to be filled
2022  * @block: 128 byte EDID block to start fetching from
2023  * @len: EDID data buffer length to fetch
2024  *
2025  * Try to fetch EDID information by calling I2C driver functions.
2026  *
2027  * Return: 0 on success or -1 on failure.
2028  */
2029 static int
2030 drm_do_probe_ddc_edid(void *data, u8 *buf, unsigned int block, size_t len)
2031 {
2032         struct i2c_adapter *adapter = data;
2033         unsigned char start = block * EDID_LENGTH;
2034         unsigned char segment = block >> 1;
2035         unsigned char xfers = segment ? 3 : 2;
2036         int ret, retries = 5;
2037
2038         /*
2039          * The core I2C driver will automatically retry the transfer if the
2040          * adapter reports EAGAIN. However, we find that bit-banging transfers
2041          * are susceptible to errors under a heavily loaded machine and
2042          * generate spurious NAKs and timeouts. Retrying the transfer
2043          * of the individual block a few times seems to overcome this.
2044          */
2045         do {
2046                 struct i2c_msg msgs[] = {
2047                         {
2048                                 .addr   = DDC_SEGMENT_ADDR,
2049                                 .flags  = 0,
2050                                 .len    = 1,
2051                                 .buf    = &segment,
2052                         }, {
2053                                 .addr   = DDC_ADDR,
2054                                 .flags  = 0,
2055                                 .len    = 1,
2056                                 .buf    = &start,
2057                         }, {
2058                                 .addr   = DDC_ADDR,
2059                                 .flags  = I2C_M_RD,
2060                                 .len    = len,
2061                                 .buf    = buf,
2062                         }
2063                 };
2064
2065                 /*
2066                  * Avoid sending the segment addr to not upset non-compliant
2067                  * DDC monitors.
2068                  */
2069                 ret = i2c_transfer(adapter, &msgs[3 - xfers], xfers);
2070
2071                 if (ret == -ENXIO) {
2072                         DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
2073                                         adapter->name);
2074                         break;
2075                 }
2076         } while (ret != xfers && --retries);
2077
2078         return ret == xfers ? 0 : -1;
2079 }
2080
2081 static void connector_bad_edid(struct drm_connector *connector,
2082                                const struct edid *edid, int num_blocks)
2083 {
2084         int i;
2085         u8 last_block;
2086
2087         /*
2088          * 0x7e in the EDID is the number of extension blocks. The EDID
2089          * is 1 (base block) + num_ext_blocks big. That means we can think
2090          * of 0x7e in the EDID of the _index_ of the last block in the
2091          * combined chunk of memory.
2092          */
2093         last_block = edid->extensions;
2094
2095         /* Calculate real checksum for the last edid extension block data */
2096         if (last_block < num_blocks)
2097                 connector->real_edid_checksum =
2098                         edid_block_compute_checksum(edid + last_block);
2099
2100         if (connector->bad_edid_counter++ && !drm_debug_enabled(DRM_UT_KMS))
2101                 return;
2102
2103         drm_dbg_kms(connector->dev, "%s: EDID is invalid:\n", connector->name);
2104         for (i = 0; i < num_blocks; i++)
2105                 edid_block_dump(KERN_DEBUG, edid + i, i);
2106 }
2107
2108 /* Get override or firmware EDID */
2109 static struct edid *drm_get_override_edid(struct drm_connector *connector)
2110 {
2111         struct edid *override = NULL;
2112
2113         if (connector->override_edid)
2114                 override = drm_edid_duplicate(connector->edid_blob_ptr->data);
2115
2116         if (!override)
2117                 override = drm_load_edid_firmware(connector);
2118
2119         return IS_ERR(override) ? NULL : override;
2120 }
2121
2122 /**
2123  * drm_add_override_edid_modes - add modes from override/firmware EDID
2124  * @connector: connector we're probing
2125  *
2126  * Add modes from the override/firmware EDID, if available. Only to be used from
2127  * drm_helper_probe_single_connector_modes() as a fallback for when DDC probe
2128  * failed during drm_get_edid() and caused the override/firmware EDID to be
2129  * skipped.
2130  *
2131  * Return: The number of modes added or 0 if we couldn't find any.
2132  */
2133 int drm_add_override_edid_modes(struct drm_connector *connector)
2134 {
2135         struct edid *override;
2136         int num_modes = 0;
2137
2138         override = drm_get_override_edid(connector);
2139         if (override) {
2140                 drm_connector_update_edid_property(connector, override);
2141                 num_modes = drm_add_edid_modes(connector, override);
2142                 kfree(override);
2143
2144                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] adding %d modes via fallback override/firmware EDID\n",
2145                               connector->base.id, connector->name, num_modes);
2146         }
2147
2148         return num_modes;
2149 }
2150 EXPORT_SYMBOL(drm_add_override_edid_modes);
2151
2152 typedef int read_block_fn(void *context, u8 *buf, unsigned int block, size_t len);
2153
2154 static enum edid_block_status edid_block_read(void *block, unsigned int block_num,
2155                                               read_block_fn read_block,
2156                                               void *context)
2157 {
2158         enum edid_block_status status;
2159         bool is_base_block = block_num == 0;
2160         int try;
2161
2162         for (try = 0; try < 4; try++) {
2163                 if (read_block(context, block, block_num, EDID_LENGTH))
2164                         return EDID_BLOCK_READ_FAIL;
2165
2166                 status = edid_block_check(block, is_base_block);
2167                 if (status == EDID_BLOCK_HEADER_REPAIR) {
2168                         edid_header_fix(block);
2169
2170                         /* Retry with fixed header, update status if that worked. */
2171                         status = edid_block_check(block, is_base_block);
2172                         if (status == EDID_BLOCK_OK)
2173                                 status = EDID_BLOCK_HEADER_FIXED;
2174                 }
2175
2176                 if (edid_block_status_valid(status, edid_block_tag(block)))
2177                         break;
2178
2179                 /* Fail early for unrepairable base block all zeros. */
2180                 if (try == 0 && is_base_block && status == EDID_BLOCK_ZERO)
2181                         break;
2182         }
2183
2184         return status;
2185 }
2186
2187 /**
2188  * drm_do_get_edid - get EDID data using a custom EDID block read function
2189  * @connector: connector we're probing
2190  * @read_block: EDID block read function
2191  * @context: private data passed to the block read function
2192  *
2193  * When the I2C adapter connected to the DDC bus is hidden behind a device that
2194  * exposes a different interface to read EDID blocks this function can be used
2195  * to get EDID data using a custom block read function.
2196  *
2197  * As in the general case the DDC bus is accessible by the kernel at the I2C
2198  * level, drivers must make all reasonable efforts to expose it as an I2C
2199  * adapter and use drm_get_edid() instead of abusing this function.
2200  *
2201  * The EDID may be overridden using debugfs override_edid or firmware EDID
2202  * (drm_load_edid_firmware() and drm.edid_firmware parameter), in this priority
2203  * order. Having either of them bypasses actual EDID reads.
2204  *
2205  * Return: Pointer to valid EDID or NULL if we couldn't find any.
2206  */
2207 struct edid *drm_do_get_edid(struct drm_connector *connector,
2208                              read_block_fn read_block,
2209                              void *context)
2210 {
2211         enum edid_block_status status;
2212         int i, invalid_blocks = 0;
2213         struct edid *edid, *new;
2214
2215         edid = drm_get_override_edid(connector);
2216         if (edid)
2217                 goto ok;
2218
2219         edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
2220         if (!edid)
2221                 return NULL;
2222
2223         status = edid_block_read(edid, 0, read_block, context);
2224
2225         edid_block_status_print(status, edid, 0);
2226
2227         if (status == EDID_BLOCK_READ_FAIL)
2228                 goto fail;
2229
2230         /* FIXME: Clarify what a corrupt EDID actually means. */
2231         if (status == EDID_BLOCK_OK || status == EDID_BLOCK_VERSION)
2232                 connector->edid_corrupt = false;
2233         else
2234                 connector->edid_corrupt = true;
2235
2236         if (!edid_block_status_valid(status, edid_block_tag(edid))) {
2237                 if (status == EDID_BLOCK_ZERO)
2238                         connector->null_edid_counter++;
2239
2240                 connector_bad_edid(connector, edid, 1);
2241                 goto fail;
2242         }
2243
2244         if (!edid_extension_block_count(edid))
2245                 goto ok;
2246
2247         new = krealloc(edid, edid_size(edid), GFP_KERNEL);
2248         if (!new)
2249                 goto fail;
2250         edid = new;
2251
2252         for (i = 1; i < edid_block_count(edid); i++) {
2253                 void *block = (void *)edid_block_data(edid, i);
2254
2255                 status = edid_block_read(block, i, read_block, context);
2256
2257                 edid_block_status_print(status, block, i);
2258
2259                 if (!edid_block_status_valid(status, edid_block_tag(block))) {
2260                         if (status == EDID_BLOCK_READ_FAIL)
2261                                 goto fail;
2262                         invalid_blocks++;
2263                 }
2264         }
2265
2266         if (invalid_blocks) {
2267                 connector_bad_edid(connector, edid, edid_block_count(edid));
2268
2269                 edid = edid_filter_invalid_blocks(edid, invalid_blocks);
2270         }
2271
2272 ok:
2273         return edid;
2274
2275 fail:
2276         kfree(edid);
2277         return NULL;
2278 }
2279 EXPORT_SYMBOL_GPL(drm_do_get_edid);
2280
2281 /**
2282  * drm_probe_ddc() - probe DDC presence
2283  * @adapter: I2C adapter to probe
2284  *
2285  * Return: True on success, false on failure.
2286  */
2287 bool
2288 drm_probe_ddc(struct i2c_adapter *adapter)
2289 {
2290         unsigned char out;
2291
2292         return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
2293 }
2294 EXPORT_SYMBOL(drm_probe_ddc);
2295
2296 /**
2297  * drm_get_edid - get EDID data, if available
2298  * @connector: connector we're probing
2299  * @adapter: I2C adapter to use for DDC
2300  *
2301  * Poke the given I2C channel to grab EDID data if possible.  If found,
2302  * attach it to the connector.
2303  *
2304  * Return: Pointer to valid EDID or NULL if we couldn't find any.
2305  */
2306 struct edid *drm_get_edid(struct drm_connector *connector,
2307                           struct i2c_adapter *adapter)
2308 {
2309         struct edid *edid;
2310
2311         if (connector->force == DRM_FORCE_OFF)
2312                 return NULL;
2313
2314         if (connector->force == DRM_FORCE_UNSPECIFIED && !drm_probe_ddc(adapter))
2315                 return NULL;
2316
2317         edid = drm_do_get_edid(connector, drm_do_probe_ddc_edid, adapter);
2318         drm_connector_update_edid_property(connector, edid);
2319         return edid;
2320 }
2321 EXPORT_SYMBOL(drm_get_edid);
2322
2323 static u32 edid_extract_panel_id(const struct edid *edid)
2324 {
2325         /*
2326          * We represent the ID as a 32-bit number so it can easily be compared
2327          * with "==".
2328          *
2329          * NOTE that we deal with endianness differently for the top half
2330          * of this ID than for the bottom half. The bottom half (the product
2331          * id) gets decoded as little endian by the EDID_PRODUCT_ID because
2332          * that's how everyone seems to interpret it. The top half (the mfg_id)
2333          * gets stored as big endian because that makes
2334          * drm_edid_encode_panel_id() and drm_edid_decode_panel_id() easier
2335          * to write (it's easier to extract the ASCII). It doesn't really
2336          * matter, though, as long as the number here is unique.
2337          */
2338         return (u32)edid->mfg_id[0] << 24   |
2339                (u32)edid->mfg_id[1] << 16   |
2340                (u32)EDID_PRODUCT_ID(edid);
2341 }
2342
2343 /**
2344  * drm_edid_get_panel_id - Get a panel's ID through DDC
2345  * @adapter: I2C adapter to use for DDC
2346  *
2347  * This function reads the first block of the EDID of a panel and (assuming
2348  * that the EDID is valid) extracts the ID out of it. The ID is a 32-bit value
2349  * (16 bits of manufacturer ID and 16 bits of per-manufacturer ID) that's
2350  * supposed to be different for each different modem of panel.
2351  *
2352  * This function is intended to be used during early probing on devices where
2353  * more than one panel might be present. Because of its intended use it must
2354  * assume that the EDID of the panel is correct, at least as far as the ID
2355  * is concerned (in other words, we don't process any overrides here).
2356  *
2357  * NOTE: it's expected that this function and drm_do_get_edid() will both
2358  * be read the EDID, but there is no caching between them. Since we're only
2359  * reading the first block, hopefully this extra overhead won't be too big.
2360  *
2361  * Return: A 32-bit ID that should be different for each make/model of panel.
2362  *         See the functions drm_edid_encode_panel_id() and
2363  *         drm_edid_decode_panel_id() for some details on the structure of this
2364  *         ID.
2365  */
2366
2367 u32 drm_edid_get_panel_id(struct i2c_adapter *adapter)
2368 {
2369         enum edid_block_status status;
2370         void *base_block;
2371         u32 panel_id = 0;
2372
2373         /*
2374          * There are no manufacturer IDs of 0, so if there is a problem reading
2375          * the EDID then we'll just return 0.
2376          */
2377
2378         base_block = kmalloc(EDID_LENGTH, GFP_KERNEL);
2379         if (!base_block)
2380                 return 0;
2381
2382         status = edid_block_read(base_block, 0, drm_do_probe_ddc_edid, adapter);
2383
2384         edid_block_status_print(status, base_block, 0);
2385
2386         if (edid_block_status_valid(status, edid_block_tag(base_block)))
2387                 panel_id = edid_extract_panel_id(base_block);
2388
2389         kfree(base_block);
2390
2391         return panel_id;
2392 }
2393 EXPORT_SYMBOL(drm_edid_get_panel_id);
2394
2395 /**
2396  * drm_get_edid_switcheroo - get EDID data for a vga_switcheroo output
2397  * @connector: connector we're probing
2398  * @adapter: I2C adapter to use for DDC
2399  *
2400  * Wrapper around drm_get_edid() for laptops with dual GPUs using one set of
2401  * outputs. The wrapper adds the requisite vga_switcheroo calls to temporarily
2402  * switch DDC to the GPU which is retrieving EDID.
2403  *
2404  * Return: Pointer to valid EDID or %NULL if we couldn't find any.
2405  */
2406 struct edid *drm_get_edid_switcheroo(struct drm_connector *connector,
2407                                      struct i2c_adapter *adapter)
2408 {
2409         struct drm_device *dev = connector->dev;
2410         struct pci_dev *pdev = to_pci_dev(dev->dev);
2411         struct edid *edid;
2412
2413         if (drm_WARN_ON_ONCE(dev, !dev_is_pci(dev->dev)))
2414                 return NULL;
2415
2416         vga_switcheroo_lock_ddc(pdev);
2417         edid = drm_get_edid(connector, adapter);
2418         vga_switcheroo_unlock_ddc(pdev);
2419
2420         return edid;
2421 }
2422 EXPORT_SYMBOL(drm_get_edid_switcheroo);
2423
2424 /**
2425  * drm_edid_duplicate - duplicate an EDID and the extensions
2426  * @edid: EDID to duplicate
2427  *
2428  * Return: Pointer to duplicated EDID or NULL on allocation failure.
2429  */
2430 struct edid *drm_edid_duplicate(const struct edid *edid)
2431 {
2432         return kmemdup(edid, edid_size(edid), GFP_KERNEL);
2433 }
2434 EXPORT_SYMBOL(drm_edid_duplicate);
2435
2436 /*** EDID parsing ***/
2437
2438 /**
2439  * edid_get_quirks - return quirk flags for a given EDID
2440  * @drm_edid: EDID to process
2441  *
2442  * This tells subsequent routines what fixes they need to apply.
2443  */
2444 static u32 edid_get_quirks(const struct drm_edid *drm_edid)
2445 {
2446         u32 panel_id = edid_extract_panel_id(drm_edid->edid);
2447         const struct edid_quirk *quirk;
2448         int i;
2449
2450         for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
2451                 quirk = &edid_quirk_list[i];
2452                 if (quirk->panel_id == panel_id)
2453                         return quirk->quirks;
2454         }
2455
2456         return 0;
2457 }
2458
2459 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
2460 #define MODE_REFRESH_DIFF(c,t) (abs((c) - (t)))
2461
2462 /*
2463  * Walk the mode list for connector, clearing the preferred status on existing
2464  * modes and setting it anew for the right mode ala quirks.
2465  */
2466 static void edid_fixup_preferred(struct drm_connector *connector,
2467                                  u32 quirks)
2468 {
2469         struct drm_display_mode *t, *cur_mode, *preferred_mode;
2470         int target_refresh = 0;
2471         int cur_vrefresh, preferred_vrefresh;
2472
2473         if (list_empty(&connector->probed_modes))
2474                 return;
2475
2476         if (quirks & EDID_QUIRK_PREFER_LARGE_60)
2477                 target_refresh = 60;
2478         if (quirks & EDID_QUIRK_PREFER_LARGE_75)
2479                 target_refresh = 75;
2480
2481         preferred_mode = list_first_entry(&connector->probed_modes,
2482                                           struct drm_display_mode, head);
2483
2484         list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
2485                 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
2486
2487                 if (cur_mode == preferred_mode)
2488                         continue;
2489
2490                 /* Largest mode is preferred */
2491                 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
2492                         preferred_mode = cur_mode;
2493
2494                 cur_vrefresh = drm_mode_vrefresh(cur_mode);
2495                 preferred_vrefresh = drm_mode_vrefresh(preferred_mode);
2496                 /* At a given size, try to get closest to target refresh */
2497                 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
2498                     MODE_REFRESH_DIFF(cur_vrefresh, target_refresh) <
2499                     MODE_REFRESH_DIFF(preferred_vrefresh, target_refresh)) {
2500                         preferred_mode = cur_mode;
2501                 }
2502         }
2503
2504         preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
2505 }
2506
2507 static bool
2508 mode_is_rb(const struct drm_display_mode *mode)
2509 {
2510         return (mode->htotal - mode->hdisplay == 160) &&
2511                (mode->hsync_end - mode->hdisplay == 80) &&
2512                (mode->hsync_end - mode->hsync_start == 32) &&
2513                (mode->vsync_start - mode->vdisplay == 3);
2514 }
2515
2516 /*
2517  * drm_mode_find_dmt - Create a copy of a mode if present in DMT
2518  * @dev: Device to duplicate against
2519  * @hsize: Mode width
2520  * @vsize: Mode height
2521  * @fresh: Mode refresh rate
2522  * @rb: Mode reduced-blanking-ness
2523  *
2524  * Walk the DMT mode list looking for a match for the given parameters.
2525  *
2526  * Return: A newly allocated copy of the mode, or NULL if not found.
2527  */
2528 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
2529                                            int hsize, int vsize, int fresh,
2530                                            bool rb)
2531 {
2532         int i;
2533
2534         for (i = 0; i < ARRAY_SIZE(drm_dmt_modes); i++) {
2535                 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
2536
2537                 if (hsize != ptr->hdisplay)
2538                         continue;
2539                 if (vsize != ptr->vdisplay)
2540                         continue;
2541                 if (fresh != drm_mode_vrefresh(ptr))
2542                         continue;
2543                 if (rb != mode_is_rb(ptr))
2544                         continue;
2545
2546                 return drm_mode_duplicate(dev, ptr);
2547         }
2548
2549         return NULL;
2550 }
2551 EXPORT_SYMBOL(drm_mode_find_dmt);
2552
2553 static bool is_display_descriptor(const struct detailed_timing *descriptor, u8 type)
2554 {
2555         BUILD_BUG_ON(offsetof(typeof(*descriptor), pixel_clock) != 0);
2556         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.pad1) != 2);
2557         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.type) != 3);
2558
2559         return descriptor->pixel_clock == 0 &&
2560                 descriptor->data.other_data.pad1 == 0 &&
2561                 descriptor->data.other_data.type == type;
2562 }
2563
2564 static bool is_detailed_timing_descriptor(const struct detailed_timing *descriptor)
2565 {
2566         BUILD_BUG_ON(offsetof(typeof(*descriptor), pixel_clock) != 0);
2567
2568         return descriptor->pixel_clock != 0;
2569 }
2570
2571 typedef void detailed_cb(const struct detailed_timing *timing, void *closure);
2572
2573 static void
2574 cea_for_each_detailed_block(const u8 *ext, detailed_cb *cb, void *closure)
2575 {
2576         int i, n;
2577         u8 d = ext[0x02];
2578         const u8 *det_base = ext + d;
2579
2580         if (d < 4 || d > 127)
2581                 return;
2582
2583         n = (127 - d) / 18;
2584         for (i = 0; i < n; i++)
2585                 cb((const struct detailed_timing *)(det_base + 18 * i), closure);
2586 }
2587
2588 static void
2589 vtb_for_each_detailed_block(const u8 *ext, detailed_cb *cb, void *closure)
2590 {
2591         unsigned int i, n = min((int)ext[0x02], 6);
2592         const u8 *det_base = ext + 5;
2593
2594         if (ext[0x01] != 1)
2595                 return; /* unknown version */
2596
2597         for (i = 0; i < n; i++)
2598                 cb((const struct detailed_timing *)(det_base + 18 * i), closure);
2599 }
2600
2601 static void
2602 drm_for_each_detailed_block(const struct edid *edid, detailed_cb *cb, void *closure)
2603 {
2604         struct drm_edid_iter edid_iter;
2605         const u8 *ext;
2606         int i;
2607
2608         if (edid == NULL)
2609                 return;
2610
2611         for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
2612                 cb(&(edid->detailed_timings[i]), closure);
2613
2614         drm_edid_iter_begin(edid, &edid_iter);
2615         drm_edid_iter_for_each(ext, &edid_iter) {
2616                 switch (*ext) {
2617                 case CEA_EXT:
2618                         cea_for_each_detailed_block(ext, cb, closure);
2619                         break;
2620                 case VTB_EXT:
2621                         vtb_for_each_detailed_block(ext, cb, closure);
2622                         break;
2623                 default:
2624                         break;
2625                 }
2626         }
2627         drm_edid_iter_end(&edid_iter);
2628 }
2629
2630 static void
2631 is_rb(const struct detailed_timing *descriptor, void *data)
2632 {
2633         bool *res = data;
2634
2635         if (!is_display_descriptor(descriptor, EDID_DETAIL_MONITOR_RANGE))
2636                 return;
2637
2638         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.flags) != 10);
2639         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.formula.cvt.flags) != 15);
2640
2641         if (descriptor->data.other_data.data.range.flags == DRM_EDID_CVT_SUPPORT_FLAG &&
2642             descriptor->data.other_data.data.range.formula.cvt.flags & 0x10)
2643                 *res = true;
2644 }
2645
2646 /* EDID 1.4 defines this explicitly.  For EDID 1.3, we guess, badly. */
2647 static bool
2648 drm_monitor_supports_rb(const struct edid *edid)
2649 {
2650         if (edid->revision >= 4) {
2651                 bool ret = false;
2652
2653                 drm_for_each_detailed_block(edid, is_rb, &ret);
2654                 return ret;
2655         }
2656
2657         return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
2658 }
2659
2660 static void
2661 find_gtf2(const struct detailed_timing *descriptor, void *data)
2662 {
2663         const struct detailed_timing **res = data;
2664
2665         if (!is_display_descriptor(descriptor, EDID_DETAIL_MONITOR_RANGE))
2666                 return;
2667
2668         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.flags) != 10);
2669
2670         if (descriptor->data.other_data.data.range.flags == 0x02)
2671                 *res = descriptor;
2672 }
2673
2674 /* Secondary GTF curve kicks in above some break frequency */
2675 static int
2676 drm_gtf2_hbreak(const struct edid *edid)
2677 {
2678         const struct detailed_timing *descriptor = NULL;
2679
2680         drm_for_each_detailed_block(edid, find_gtf2, &descriptor);
2681
2682         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.formula.gtf2.hfreq_start_khz) != 12);
2683
2684         return descriptor ? descriptor->data.other_data.data.range.formula.gtf2.hfreq_start_khz * 2 : 0;
2685 }
2686
2687 static int
2688 drm_gtf2_2c(const struct edid *edid)
2689 {
2690         const struct detailed_timing *descriptor = NULL;
2691
2692         drm_for_each_detailed_block(edid, find_gtf2, &descriptor);
2693
2694         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.formula.gtf2.c) != 13);
2695
2696         return descriptor ? descriptor->data.other_data.data.range.formula.gtf2.c : 0;
2697 }
2698
2699 static int
2700 drm_gtf2_m(const struct edid *edid)
2701 {
2702         const struct detailed_timing *descriptor = NULL;
2703
2704         drm_for_each_detailed_block(edid, find_gtf2, &descriptor);
2705
2706         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.formula.gtf2.m) != 14);
2707
2708         return descriptor ? le16_to_cpu(descriptor->data.other_data.data.range.formula.gtf2.m) : 0;
2709 }
2710
2711 static int
2712 drm_gtf2_k(const struct edid *edid)
2713 {
2714         const struct detailed_timing *descriptor = NULL;
2715
2716         drm_for_each_detailed_block(edid, find_gtf2, &descriptor);
2717
2718         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.formula.gtf2.k) != 16);
2719
2720         return descriptor ? descriptor->data.other_data.data.range.formula.gtf2.k : 0;
2721 }
2722
2723 static int
2724 drm_gtf2_2j(const struct edid *edid)
2725 {
2726         const struct detailed_timing *descriptor = NULL;
2727
2728         drm_for_each_detailed_block(edid, find_gtf2, &descriptor);
2729
2730         BUILD_BUG_ON(offsetof(typeof(*descriptor), data.other_data.data.range.formula.gtf2.j) != 17);
2731
2732         return descriptor ? descriptor->data.other_data.data.range.formula.gtf2.j : 0;
2733 }
2734
2735 /* Get standard timing level (CVT/GTF/DMT). */
2736 static int standard_timing_level(const struct edid *edid)
2737 {
2738         if (edid->revision >= 2) {
2739                 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
2740                         return LEVEL_CVT;
2741                 if (drm_gtf2_hbreak(edid))
2742                         return LEVEL_GTF2;
2743                 if (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF)
2744                         return LEVEL_GTF;
2745         }
2746         return LEVEL_DMT;
2747 }
2748
2749 /*
2750  * 0 is reserved.  The spec says 0x01 fill for unused timings.  Some old
2751  * monitors fill with ascii space (0x20) instead.
2752  */
2753 static int
2754 bad_std_timing(u8 a, u8 b)
2755 {
2756         return (a == 0x00 && b == 0x00) ||
2757                (a == 0x01 && b == 0x01) ||
2758                (a == 0x20 && b == 0x20);
2759 }
2760
2761 static int drm_mode_hsync(const struct drm_display_mode *mode)
2762 {
2763         if (mode->htotal <= 0)
2764                 return 0;
2765
2766         return DIV_ROUND_CLOSEST(mode->clock, mode->htotal);
2767 }
2768
2769 /*
2770  * Take the standard timing params (in this case width, aspect, and refresh)
2771  * and convert them into a real mode using CVT/GTF/DMT.
2772  */
2773 static struct drm_display_mode *
2774 drm_mode_std(struct drm_connector *connector, const struct edid *edid,
2775              const struct std_timing *t)
2776 {
2777         struct drm_device *dev = connector->dev;
2778         struct drm_display_mode *m, *mode = NULL;
2779         int hsize, vsize;
2780         int vrefresh_rate;
2781         unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
2782                 >> EDID_TIMING_ASPECT_SHIFT;
2783         unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
2784                 >> EDID_TIMING_VFREQ_SHIFT;
2785         int timing_level = standard_timing_level(edid);
2786
2787         if (bad_std_timing(t->hsize, t->vfreq_aspect))
2788                 return NULL;
2789
2790         /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
2791         hsize = t->hsize * 8 + 248;
2792         /* vrefresh_rate = vfreq + 60 */
2793         vrefresh_rate = vfreq + 60;
2794         /* the vdisplay is calculated based on the aspect ratio */
2795         if (aspect_ratio == 0) {
2796                 if (edid->revision < 3)
2797                         vsize = hsize;
2798                 else
2799                         vsize = (hsize * 10) / 16;
2800         } else if (aspect_ratio == 1)
2801                 vsize = (hsize * 3) / 4;
2802         else if (aspect_ratio == 2)
2803                 vsize = (hsize * 4) / 5;
2804         else
2805                 vsize = (hsize * 9) / 16;
2806
2807         /* HDTV hack, part 1 */
2808         if (vrefresh_rate == 60 &&
2809             ((hsize == 1360 && vsize == 765) ||
2810              (hsize == 1368 && vsize == 769))) {
2811                 hsize = 1366;
2812                 vsize = 768;
2813         }
2814
2815         /*
2816          * If this connector already has a mode for this size and refresh
2817          * rate (because it came from detailed or CVT info), use that
2818          * instead.  This way we don't have to guess at interlace or
2819          * reduced blanking.
2820          */
2821         list_for_each_entry(m, &connector->probed_modes, head)
2822                 if (m->hdisplay == hsize && m->vdisplay == vsize &&
2823                     drm_mode_vrefresh(m) == vrefresh_rate)
2824                         return NULL;
2825
2826         /* HDTV hack, part 2 */
2827         if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
2828                 mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
2829                                     false);
2830                 if (!mode)
2831                         return NULL;
2832                 mode->hdisplay = 1366;
2833                 mode->hsync_start = mode->hsync_start - 1;
2834                 mode->hsync_end = mode->hsync_end - 1;
2835                 return mode;
2836         }
2837
2838         /* check whether it can be found in default mode table */
2839         if (drm_monitor_supports_rb(edid)) {
2840                 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
2841                                          true);
2842                 if (mode)
2843                         return mode;
2844         }
2845         mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
2846         if (mode)
2847                 return mode;
2848
2849         /* okay, generate it */
2850         switch (timing_level) {
2851         case LEVEL_DMT:
2852                 break;
2853         case LEVEL_GTF:
2854                 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
2855                 break;
2856         case LEVEL_GTF2:
2857                 /*
2858                  * This is potentially wrong if there's ever a monitor with
2859                  * more than one ranges section, each claiming a different
2860                  * secondary GTF curve.  Please don't do that.
2861                  */
2862                 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
2863                 if (!mode)
2864                         return NULL;
2865                 if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
2866                         drm_mode_destroy(dev, mode);
2867                         mode = drm_gtf_mode_complex(dev, hsize, vsize,
2868                                                     vrefresh_rate, 0, 0,
2869                                                     drm_gtf2_m(edid),
2870                                                     drm_gtf2_2c(edid),
2871                                                     drm_gtf2_k(edid),
2872                                                     drm_gtf2_2j(edid));
2873                 }
2874                 break;
2875         case LEVEL_CVT:
2876                 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
2877                                     false);
2878                 break;
2879         }
2880         return mode;
2881 }
2882
2883 /*
2884  * EDID is delightfully ambiguous about how interlaced modes are to be
2885  * encoded.  Our internal representation is of frame height, but some
2886  * HDTV detailed timings are encoded as field height.
2887  *
2888  * The format list here is from CEA, in frame size.  Technically we
2889  * should be checking refresh rate too.  Whatever.
2890  */
2891 static void
2892 drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
2893                             const struct detailed_pixel_timing *pt)
2894 {
2895         int i;
2896         static const struct {
2897                 int w, h;
2898         } cea_interlaced[] = {
2899                 { 1920, 1080 },
2900                 {  720,  480 },
2901                 { 1440,  480 },
2902                 { 2880,  480 },
2903                 {  720,  576 },
2904                 { 1440,  576 },
2905                 { 2880,  576 },
2906         };
2907
2908         if (!(pt->misc & DRM_EDID_PT_INTERLACED))
2909                 return;
2910
2911         for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
2912                 if ((mode->hdisplay == cea_interlaced[i].w) &&
2913                     (mode->vdisplay == cea_interlaced[i].h / 2)) {
2914                         mode->vdisplay *= 2;
2915                         mode->vsync_start *= 2;
2916                         mode->vsync_end *= 2;
2917                         mode->vtotal *= 2;
2918                         mode->vtotal |= 1;
2919                 }
2920         }
2921
2922         mode->flags |= DRM_MODE_FLAG_INTERLACE;
2923 }
2924
2925 /*
2926  * Create a new mode from an EDID detailed timing section. An EDID detailed
2927  * timing block contains enough info for us to create and return a new struct
2928  * drm_display_mode.
2929  */
2930 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
2931                                                   const struct edid *edid,
2932                                                   const struct detailed_timing *timing,
2933                                                   u32 quirks)
2934 {
2935         struct drm_display_mode *mode;
2936         const struct detailed_pixel_timing *pt = &timing->data.pixel_data;
2937         unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
2938         unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
2939         unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
2940         unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
2941         unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
2942         unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
2943         unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) << 2 | pt->vsync_offset_pulse_width_lo >> 4;
2944         unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
2945
2946         /* ignore tiny modes */
2947         if (hactive < 64 || vactive < 64)
2948                 return NULL;
2949
2950         if (pt->misc & DRM_EDID_PT_STEREO) {
2951                 DRM_DEBUG_KMS("stereo mode not supported\n");
2952                 return NULL;
2953         }
2954         if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
2955                 DRM_DEBUG_KMS("composite sync not supported\n");
2956         }
2957
2958         /* it is incorrect if hsync/vsync width is zero */
2959         if (!hsync_pulse_width || !vsync_pulse_width) {
2960                 DRM_DEBUG_KMS("Incorrect Detailed timing. "
2961                                 "Wrong Hsync/Vsync pulse width\n");
2962                 return NULL;
2963         }
2964
2965         if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
2966                 mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
2967                 if (!mode)
2968                         return NULL;
2969
2970                 goto set_size;
2971         }
2972
2973         mode = drm_mode_create(dev);
2974         if (!mode)
2975                 return NULL;
2976
2977         if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
2978                 mode->clock = 1088 * 10;
2979         else
2980                 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
2981
2982         mode->hdisplay = hactive;
2983         mode->hsync_start = mode->hdisplay + hsync_offset;
2984         mode->hsync_end = mode->hsync_start + hsync_pulse_width;
2985         mode->htotal = mode->hdisplay + hblank;
2986
2987         mode->vdisplay = vactive;
2988         mode->vsync_start = mode->vdisplay + vsync_offset;
2989         mode->vsync_end = mode->vsync_start + vsync_pulse_width;
2990         mode->vtotal = mode->vdisplay + vblank;
2991
2992         /* Some EDIDs have bogus h/vtotal values */
2993         if (mode->hsync_end > mode->htotal)
2994                 mode->htotal = mode->hsync_end + 1;
2995         if (mode->vsync_end > mode->vtotal)
2996                 mode->vtotal = mode->vsync_end + 1;
2997
2998         drm_mode_do_interlace_quirk(mode, pt);
2999
3000         if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
3001                 mode->flags |= DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC;
3002         } else {
3003                 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
3004                         DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
3005                 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
3006                         DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
3007         }
3008
3009 set_size:
3010         mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
3011         mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
3012
3013         if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
3014                 mode->width_mm *= 10;
3015                 mode->height_mm *= 10;
3016         }
3017
3018         if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
3019                 mode->width_mm = edid->width_cm * 10;
3020                 mode->height_mm = edid->height_cm * 10;
3021         }
3022
3023         mode->type = DRM_MODE_TYPE_DRIVER;
3024         drm_mode_set_name(mode);
3025
3026         return mode;
3027 }
3028
3029 static bool
3030 mode_in_hsync_range(const struct drm_display_mode *mode,
3031                     const struct edid *edid, const u8 *t)
3032 {
3033         int hsync, hmin, hmax;
3034
3035         hmin = t[7];
3036         if (edid->revision >= 4)
3037             hmin += ((t[4] & 0x04) ? 255 : 0);
3038         hmax = t[8];
3039         if (edid->revision >= 4)
3040             hmax += ((t[4] & 0x08) ? 255 : 0);
3041         hsync = drm_mode_hsync(mode);
3042
3043         return (hsync <= hmax && hsync >= hmin);
3044 }
3045
3046 static bool
3047 mode_in_vsync_range(const struct drm_display_mode *mode,
3048                     const struct edid *edid, const u8 *t)
3049 {
3050         int vsync, vmin, vmax;
3051
3052         vmin = t[5];
3053         if (edid->revision >= 4)
3054             vmin += ((t[4] & 0x01) ? 255 : 0);
3055         vmax = t[6];
3056         if (edid->revision >= 4)
3057             vmax += ((t[4] & 0x02) ? 255 : 0);
3058         vsync = drm_mode_vrefresh(mode);
3059
3060         return (vsync <= vmax && vsync >= vmin);
3061 }
3062
3063 static u32
3064 range_pixel_clock(const struct edid *edid, const u8 *t)
3065 {
3066         /* unspecified */
3067         if (t[9] == 0 || t[9] == 255)
3068                 return 0;
3069
3070         /* 1.4 with CVT support gives us real precision, yay */
3071         if (edid->revision >= 4 && t[10] == 0x04)
3072                 return (t[9] * 10000) - ((t[12] >> 2) * 250);
3073
3074         /* 1.3 is pathetic, so fuzz up a bit */
3075         return t[9] * 10000 + 5001;
3076 }
3077
3078 static bool
3079 mode_in_range(const struct drm_display_mode *mode, const struct edid *edid,
3080               const struct detailed_timing *timing)
3081 {
3082         u32 max_clock;
3083         const u8 *t = (const u8 *)timing;
3084
3085         if (!mode_in_hsync_range(mode, edid, t))
3086                 return false;
3087
3088         if (!mode_in_vsync_range(mode, edid, t))
3089                 return false;
3090
3091         if ((max_clock = range_pixel_clock(edid, t)))
3092                 if (mode->clock > max_clock)
3093                         return false;
3094
3095         /* 1.4 max horizontal check */
3096         if (edid->revision >= 4 && t[10] == 0x04)
3097                 if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
3098                         return false;
3099
3100         if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
3101                 return false;
3102
3103         return true;
3104 }
3105
3106 static bool valid_inferred_mode(const struct drm_connector *connector,
3107                                 const struct drm_display_mode *mode)
3108 {
3109         const struct drm_display_mode *m;
3110         bool ok = false;
3111
3112         list_for_each_entry(m, &connector->probed_modes, head) {
3113                 if (mode->hdisplay == m->hdisplay &&
3114                     mode->vdisplay == m->vdisplay &&
3115                     drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
3116                         return false; /* duplicated */
3117                 if (mode->hdisplay <= m->hdisplay &&
3118                     mode->vdisplay <= m->vdisplay)
3119                         ok = true;
3120         }
3121         return ok;
3122 }
3123
3124 static int
3125 drm_dmt_modes_for_range(struct drm_connector *connector, const struct edid *edid,
3126                         const struct detailed_timing *timing)
3127 {
3128         int i, modes = 0;
3129         struct drm_display_mode *newmode;
3130         struct drm_device *dev = connector->dev;
3131
3132         for (i = 0; i < ARRAY_SIZE(drm_dmt_modes); i++) {
3133                 if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
3134                     valid_inferred_mode(connector, drm_dmt_modes + i)) {
3135                         newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
3136                         if (newmode) {
3137                                 drm_mode_probed_add(connector, newmode);
3138                                 modes++;
3139                         }
3140                 }
3141         }
3142
3143         return modes;
3144 }
3145
3146 /* fix up 1366x768 mode from 1368x768;
3147  * GFT/CVT can't express 1366 width which isn't dividable by 8
3148  */
3149 void drm_mode_fixup_1366x768(struct drm_display_mode *mode)
3150 {
3151         if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
3152                 mode->hdisplay = 1366;
3153                 mode->hsync_start--;
3154                 mode->hsync_end--;
3155                 drm_mode_set_name(mode);
3156         }
3157 }
3158
3159 static int
3160 drm_gtf_modes_for_range(struct drm_connector *connector, const struct edid *edid,
3161                         const struct detailed_timing *timing)
3162 {
3163         int i, modes = 0;
3164         struct drm_display_mode *newmode;
3165         struct drm_device *dev = connector->dev;
3166
3167         for (i = 0; i < ARRAY_SIZE(extra_modes); i++) {
3168                 const struct minimode *m = &extra_modes[i];
3169
3170                 newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
3171                 if (!newmode)
3172                         return modes;
3173
3174                 drm_mode_fixup_1366x768(newmode);
3175                 if (!mode_in_range(newmode, edid, timing) ||
3176                     !valid_inferred_mode(connector, newmode)) {
3177                         drm_mode_destroy(dev, newmode);
3178                         continue;
3179                 }
3180
3181                 drm_mode_probed_add(connector, newmode);
3182                 modes++;
3183         }
3184
3185         return modes;
3186 }
3187
3188 static int
3189 drm_cvt_modes_for_range(struct drm_connector *connector, const struct edid *edid,
3190                         const struct detailed_timing *timing)
3191 {
3192         int i, modes = 0;
3193         struct drm_display_mode *newmode;
3194         struct drm_device *dev = connector->dev;
3195         bool rb = drm_monitor_supports_rb(edid);
3196
3197         for (i = 0; i < ARRAY_SIZE(extra_modes); i++) {
3198                 const struct minimode *m = &extra_modes[i];
3199
3200                 newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
3201                 if (!newmode)
3202                         return modes;
3203
3204                 drm_mode_fixup_1366x768(newmode);
3205                 if (!mode_in_range(newmode, edid, timing) ||
3206                     !valid_inferred_mode(connector, newmode)) {
3207                         drm_mode_destroy(dev, newmode);
3208                         continue;
3209                 }
3210
3211                 drm_mode_probed_add(connector, newmode);
3212                 modes++;
3213         }
3214
3215         return modes;
3216 }
3217
3218 static void
3219 do_inferred_modes(const struct detailed_timing *timing, void *c)
3220 {
3221         struct detailed_mode_closure *closure = c;
3222         const struct detailed_non_pixel *data = &timing->data.other_data;
3223         const struct detailed_data_monitor_range *range = &data->data.range;
3224
3225         if (!is_display_descriptor(timing, EDID_DETAIL_MONITOR_RANGE))
3226                 return;
3227
3228         closure->modes += drm_dmt_modes_for_range(closure->connector,
3229                                                   closure->drm_edid->edid,
3230                                                   timing);
3231
3232         if (!version_greater(closure->drm_edid->edid, 1, 1))
3233                 return; /* GTF not defined yet */
3234
3235         switch (range->flags) {
3236         case 0x02: /* secondary gtf, XXX could do more */
3237         case 0x00: /* default gtf */
3238                 closure->modes += drm_gtf_modes_for_range(closure->connector,
3239                                                           closure->drm_edid->edid,
3240                                                           timing);
3241                 break;
3242         case 0x04: /* cvt, only in 1.4+ */
3243                 if (!version_greater(closure->drm_edid->edid, 1, 3))
3244                         break;
3245
3246                 closure->modes += drm_cvt_modes_for_range(closure->connector,
3247                                                           closure->drm_edid->edid,
3248                                                           timing);
3249                 break;
3250         case 0x01: /* just the ranges, no formula */
3251         default:
3252                 break;
3253         }
3254 }
3255
3256 static int add_inferred_modes(struct drm_connector *connector,
3257                               const struct drm_edid *drm_edid)
3258 {
3259         struct detailed_mode_closure closure = {
3260                 .connector = connector,
3261                 .drm_edid = drm_edid,
3262         };
3263
3264         if (version_greater(drm_edid->edid, 1, 0))
3265                 drm_for_each_detailed_block(drm_edid->edid, do_inferred_modes, &closure);
3266
3267         return closure.modes;
3268 }
3269
3270 static int
3271 drm_est3_modes(struct drm_connector *connector, const struct detailed_timing *timing)
3272 {
3273         int i, j, m, modes = 0;
3274         struct drm_display_mode *mode;
3275         const u8 *est = ((const u8 *)timing) + 6;
3276
3277         for (i = 0; i < 6; i++) {
3278                 for (j = 7; j >= 0; j--) {
3279                         m = (i * 8) + (7 - j);
3280                         if (m >= ARRAY_SIZE(est3_modes))
3281                                 break;
3282                         if (est[i] & (1 << j)) {
3283                                 mode = drm_mode_find_dmt(connector->dev,
3284                                                          est3_modes[m].w,
3285                                                          est3_modes[m].h,
3286                                                          est3_modes[m].r,
3287                                                          est3_modes[m].rb);
3288                                 if (mode) {
3289                                         drm_mode_probed_add(connector, mode);
3290                                         modes++;
3291                                 }
3292                         }
3293                 }
3294         }
3295
3296         return modes;
3297 }
3298
3299 static void
3300 do_established_modes(const struct detailed_timing *timing, void *c)
3301 {
3302         struct detailed_mode_closure *closure = c;
3303
3304         if (!is_display_descriptor(timing, EDID_DETAIL_EST_TIMINGS))
3305                 return;
3306
3307         closure->modes += drm_est3_modes(closure->connector, timing);
3308 }
3309
3310 /*
3311  * Get established modes from EDID and add them. Each EDID block contains a
3312  * bitmap of the supported "established modes" list (defined above). Tease them
3313  * out and add them to the global modes list.
3314  */
3315 static int add_established_modes(struct drm_connector *connector,
3316                                  const struct drm_edid *drm_edid)
3317 {
3318         struct drm_device *dev = connector->dev;
3319         const struct edid *edid = drm_edid->edid;
3320         unsigned long est_bits = edid->established_timings.t1 |
3321                 (edid->established_timings.t2 << 8) |
3322                 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
3323         int i, modes = 0;
3324         struct detailed_mode_closure closure = {
3325                 .connector = connector,
3326                 .drm_edid = drm_edid,
3327         };
3328
3329         for (i = 0; i <= EDID_EST_TIMINGS; i++) {
3330                 if (est_bits & (1<<i)) {
3331                         struct drm_display_mode *newmode;
3332
3333                         newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
3334                         if (newmode) {
3335                                 drm_mode_probed_add(connector, newmode);
3336                                 modes++;
3337                         }
3338                 }
3339         }
3340
3341         if (version_greater(edid, 1, 0))
3342                 drm_for_each_detailed_block(drm_edid->edid, do_established_modes,
3343                                             &closure);
3344
3345         return modes + closure.modes;
3346 }
3347
3348 static void
3349 do_standard_modes(const struct detailed_timing *timing, void *c)
3350 {
3351         struct detailed_mode_closure *closure = c;
3352         const struct detailed_non_pixel *data = &timing->data.other_data;
3353         struct drm_connector *connector = closure->connector;
3354         int i;
3355
3356         if (!is_display_descriptor(timing, EDID_DETAIL_STD_MODES))
3357                 return;
3358
3359         for (i = 0; i < 6; i++) {
3360                 const struct std_timing *std = &data->data.timings[i];
3361                 struct drm_display_mode *newmode;
3362
3363                 newmode = drm_mode_std(connector, closure->drm_edid->edid, std);
3364                 if (newmode) {
3365                         drm_mode_probed_add(connector, newmode);
3366                         closure->modes++;
3367                 }
3368         }
3369 }
3370
3371 /*
3372  * Get standard modes from EDID and add them. Standard modes can be calculated
3373  * using the appropriate standard (DMT, GTF, or CVT). Grab them from EDID and
3374  * add them to the list.
3375  */
3376 static int add_standard_modes(struct drm_connector *connector,
3377                               const struct drm_edid *drm_edid)
3378 {
3379         int i, modes = 0;
3380         struct detailed_mode_closure closure = {
3381                 .connector = connector,
3382                 .drm_edid = drm_edid,
3383         };
3384
3385         for (i = 0; i < EDID_STD_TIMINGS; i++) {
3386                 struct drm_display_mode *newmode;
3387
3388                 newmode = drm_mode_std(connector, drm_edid->edid,
3389                                        &drm_edid->edid->standard_timings[i]);
3390                 if (newmode) {
3391                         drm_mode_probed_add(connector, newmode);
3392                         modes++;
3393                 }
3394         }
3395
3396         if (version_greater(drm_edid->edid, 1, 0))
3397                 drm_for_each_detailed_block(drm_edid->edid, do_standard_modes,
3398                                             &closure);
3399
3400         /* XXX should also look for standard codes in VTB blocks */
3401
3402         return modes + closure.modes;
3403 }
3404
3405 static int drm_cvt_modes(struct drm_connector *connector,
3406                          const struct detailed_timing *timing)
3407 {
3408         int i, j, modes = 0;
3409         struct drm_display_mode *newmode;
3410         struct drm_device *dev = connector->dev;
3411         const struct cvt_timing *cvt;
3412         const int rates[] = { 60, 85, 75, 60, 50 };
3413         const u8 empty[3] = { 0, 0, 0 };
3414
3415         for (i = 0; i < 4; i++) {
3416                 int width, height;
3417
3418                 cvt = &(timing->data.other_data.data.cvt[i]);
3419
3420                 if (!memcmp(cvt->code, empty, 3))
3421                         continue;
3422
3423                 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
3424                 switch (cvt->code[1] & 0x0c) {
3425                 /* default - because compiler doesn't see that we've enumerated all cases */
3426                 default:
3427                 case 0x00:
3428                         width = height * 4 / 3;
3429                         break;
3430                 case 0x04:
3431                         width = height * 16 / 9;
3432                         break;
3433                 case 0x08:
3434                         width = height * 16 / 10;
3435                         break;
3436                 case 0x0c:
3437                         width = height * 15 / 9;
3438                         break;
3439                 }
3440
3441                 for (j = 1; j < 5; j++) {
3442                         if (cvt->code[2] & (1 << j)) {
3443                                 newmode = drm_cvt_mode(dev, width, height,
3444                                                        rates[j], j == 0,
3445                                                        false, false);
3446                                 if (newmode) {
3447                                         drm_mode_probed_add(connector, newmode);
3448                                         modes++;
3449                                 }
3450                         }
3451                 }
3452         }
3453
3454         return modes;
3455 }
3456
3457 static void
3458 do_cvt_mode(const struct detailed_timing *timing, void *c)
3459 {
3460         struct detailed_mode_closure *closure = c;
3461
3462         if (!is_display_descriptor(timing, EDID_DETAIL_CVT_3BYTE))
3463                 return;
3464
3465         closure->modes += drm_cvt_modes(closure->connector, timing);
3466 }
3467
3468 static int
3469 add_cvt_modes(struct drm_connector *connector, const struct drm_edid *drm_edid)
3470 {
3471         struct detailed_mode_closure closure = {
3472                 .connector = connector,
3473                 .drm_edid = drm_edid,
3474         };
3475
3476         if (version_greater(drm_edid->edid, 1, 2))
3477                 drm_for_each_detailed_block(drm_edid->edid, do_cvt_mode, &closure);
3478
3479         /* XXX should also look for CVT codes in VTB blocks */
3480
3481         return closure.modes;
3482 }
3483
3484 static void fixup_detailed_cea_mode_clock(struct drm_display_mode *mode);
3485
3486 static void
3487 do_detailed_mode(const struct detailed_timing *timing, void *c)
3488 {
3489         struct detailed_mode_closure *closure = c;
3490         struct drm_display_mode *newmode;
3491
3492         if (!is_detailed_timing_descriptor(timing))
3493                 return;
3494
3495         newmode = drm_mode_detailed(closure->connector->dev,
3496                                     closure->drm_edid->edid, timing,
3497                                     closure->quirks);
3498         if (!newmode)
3499                 return;
3500
3501         if (closure->preferred)
3502                 newmode->type |= DRM_MODE_TYPE_PREFERRED;
3503
3504         /*
3505          * Detailed modes are limited to 10kHz pixel clock resolution,
3506          * so fix up anything that looks like CEA/HDMI mode, but the clock
3507          * is just slightly off.
3508          */
3509         fixup_detailed_cea_mode_clock(newmode);
3510
3511         drm_mode_probed_add(closure->connector, newmode);
3512         closure->modes++;
3513         closure->preferred = false;
3514 }
3515
3516 /*
3517  * add_detailed_modes - Add modes from detailed timings
3518  * @connector: attached connector
3519  * @drm_edid: EDID block to scan
3520  * @quirks: quirks to apply
3521  */
3522 static int add_detailed_modes(struct drm_connector *connector,
3523                               const struct drm_edid *drm_edid, u32 quirks)
3524 {
3525         struct detailed_mode_closure closure = {
3526                 .connector = connector,
3527                 .drm_edid = drm_edid,
3528                 .preferred = true,
3529                 .quirks = quirks,
3530         };
3531
3532         if (closure.preferred && !version_greater(drm_edid->edid, 1, 3))
3533                 closure.preferred =
3534                     (drm_edid->edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
3535
3536         drm_for_each_detailed_block(drm_edid->edid, do_detailed_mode, &closure);
3537
3538         return closure.modes;
3539 }
3540
3541 /* CTA-861-H Table 60 - CTA Tag Codes */
3542 #define CTA_DB_AUDIO                    1
3543 #define CTA_DB_VIDEO                    2
3544 #define CTA_DB_VENDOR                   3
3545 #define CTA_DB_SPEAKER                  4
3546 #define CTA_DB_EXTENDED_TAG             7
3547
3548 /* CTA-861-H Table 62 - CTA Extended Tag Codes */
3549 #define CTA_EXT_DB_VIDEO_CAP            0
3550 #define CTA_EXT_DB_VENDOR               1
3551 #define CTA_EXT_DB_HDR_STATIC_METADATA  6
3552 #define CTA_EXT_DB_420_VIDEO_DATA       14
3553 #define CTA_EXT_DB_420_VIDEO_CAP_MAP    15
3554 #define CTA_EXT_DB_HF_SCDB              0x79
3555
3556 #define EDID_BASIC_AUDIO        (1 << 6)
3557 #define EDID_CEA_YCRCB444       (1 << 5)
3558 #define EDID_CEA_YCRCB422       (1 << 4)
3559 #define EDID_CEA_VCDB_QS        (1 << 6)
3560
3561 /*
3562  * Search EDID for CEA extension block.
3563  */
3564 const u8 *drm_find_edid_extension(const struct edid *edid,
3565                                   int ext_id, int *ext_index)
3566 {
3567         const u8 *edid_ext = NULL;
3568         int i;
3569
3570         /* No EDID or EDID extensions */
3571         if (!edid || !edid_extension_block_count(edid))
3572                 return NULL;
3573
3574         /* Find CEA extension */
3575         for (i = *ext_index; i < edid_extension_block_count(edid); i++) {
3576                 edid_ext = edid_extension_block_data(edid, i);
3577                 if (edid_block_tag(edid_ext) == ext_id)
3578                         break;
3579         }
3580
3581         if (i >= edid_extension_block_count(edid))
3582                 return NULL;
3583
3584         *ext_index = i + 1;
3585
3586         return edid_ext;
3587 }
3588
3589 /* Return true if the EDID has a CTA extension or a DisplayID CTA data block */
3590 static bool drm_edid_has_cta_extension(const struct drm_edid *drm_edid)
3591 {
3592         const struct displayid_block *block;
3593         struct displayid_iter iter;
3594         int ext_index = 0;
3595         bool found = false;
3596
3597         /* Look for a top level CEA extension block */
3598         if (drm_find_edid_extension(drm_edid->edid, CEA_EXT, &ext_index))
3599                 return true;
3600
3601         /* CEA blocks can also be found embedded in a DisplayID block */
3602         displayid_iter_edid_begin(drm_edid->edid, &iter);
3603         displayid_iter_for_each(block, &iter) {
3604                 if (block->tag == DATA_BLOCK_CTA) {
3605                         found = true;
3606                         break;
3607                 }
3608         }
3609         displayid_iter_end(&iter);
3610
3611         return found;
3612 }
3613
3614 static __always_inline const struct drm_display_mode *cea_mode_for_vic(u8 vic)
3615 {
3616         BUILD_BUG_ON(1 + ARRAY_SIZE(edid_cea_modes_1) - 1 != 127);
3617         BUILD_BUG_ON(193 + ARRAY_SIZE(edid_cea_modes_193) - 1 != 219);
3618
3619         if (vic >= 1 && vic < 1 + ARRAY_SIZE(edid_cea_modes_1))
3620                 return &edid_cea_modes_1[vic - 1];
3621         if (vic >= 193 && vic < 193 + ARRAY_SIZE(edid_cea_modes_193))
3622                 return &edid_cea_modes_193[vic - 193];
3623         return NULL;
3624 }
3625
3626 static u8 cea_num_vics(void)
3627 {
3628         return 193 + ARRAY_SIZE(edid_cea_modes_193);
3629 }
3630
3631 static u8 cea_next_vic(u8 vic)
3632 {
3633         if (++vic == 1 + ARRAY_SIZE(edid_cea_modes_1))
3634                 vic = 193;
3635         return vic;
3636 }
3637
3638 /*
3639  * Calculate the alternate clock for the CEA mode
3640  * (60Hz vs. 59.94Hz etc.)
3641  */
3642 static unsigned int
3643 cea_mode_alternate_clock(const struct drm_display_mode *cea_mode)
3644 {
3645         unsigned int clock = cea_mode->clock;
3646
3647         if (drm_mode_vrefresh(cea_mode) % 6 != 0)
3648                 return clock;
3649
3650         /*
3651          * edid_cea_modes contains the 59.94Hz
3652          * variant for 240 and 480 line modes,
3653          * and the 60Hz variant otherwise.
3654          */
3655         if (cea_mode->vdisplay == 240 || cea_mode->vdisplay == 480)
3656                 clock = DIV_ROUND_CLOSEST(clock * 1001, 1000);
3657         else
3658                 clock = DIV_ROUND_CLOSEST(clock * 1000, 1001);
3659
3660         return clock;
3661 }
3662
3663 static bool
3664 cea_mode_alternate_timings(u8 vic, struct drm_display_mode *mode)
3665 {
3666         /*
3667          * For certain VICs the spec allows the vertical
3668          * front porch to vary by one or two lines.
3669          *
3670          * cea_modes[] stores the variant with the shortest
3671          * vertical front porch. We can adjust the mode to
3672          * get the other variants by simply increasing the
3673          * vertical front porch length.
3674          */
3675         BUILD_BUG_ON(cea_mode_for_vic(8)->vtotal != 262 ||
3676                      cea_mode_for_vic(9)->vtotal != 262 ||
3677                      cea_mode_for_vic(12)->vtotal != 262 ||
3678                      cea_mode_for_vic(13)->vtotal != 262 ||
3679                      cea_mode_for_vic(23)->vtotal != 312 ||
3680                      cea_mode_for_vic(24)->vtotal != 312 ||
3681                      cea_mode_for_vic(27)->vtotal != 312 ||
3682                      cea_mode_for_vic(28)->vtotal != 312);
3683
3684         if (((vic == 8 || vic == 9 ||
3685               vic == 12 || vic == 13) && mode->vtotal < 263) ||
3686             ((vic == 23 || vic == 24 ||
3687               vic == 27 || vic == 28) && mode->vtotal < 314)) {
3688                 mode->vsync_start++;
3689                 mode->vsync_end++;
3690                 mode->vtotal++;
3691
3692                 return true;
3693         }
3694
3695         return false;
3696 }
3697
3698 static u8 drm_match_cea_mode_clock_tolerance(const struct drm_display_mode *to_match,
3699                                              unsigned int clock_tolerance)
3700 {
3701         unsigned int match_flags = DRM_MODE_MATCH_TIMINGS | DRM_MODE_MATCH_FLAGS;
3702         u8 vic;
3703
3704         if (!to_match->clock)
3705                 return 0;
3706
3707         if (to_match->picture_aspect_ratio)
3708                 match_flags |= DRM_MODE_MATCH_ASPECT_RATIO;
3709
3710         for (vic = 1; vic < cea_num_vics(); vic = cea_next_vic(vic)) {
3711                 struct drm_display_mode cea_mode;
3712                 unsigned int clock1, clock2;
3713
3714                 drm_mode_init(&cea_mode, cea_mode_for_vic(vic));
3715
3716                 /* Check both 60Hz and 59.94Hz */
3717                 clock1 = cea_mode.clock;
3718                 clock2 = cea_mode_alternate_clock(&cea_mode);
3719
3720                 if (abs(to_match->clock - clock1) > clock_tolerance &&
3721                     abs(to_match->clock - clock2) > clock_tolerance)
3722                         continue;
3723
3724                 do {
3725                         if (drm_mode_match(to_match, &cea_mode, match_flags))
3726                                 return vic;
3727                 } while (cea_mode_alternate_timings(vic, &cea_mode));
3728         }
3729
3730         return 0;
3731 }
3732
3733 /**
3734  * drm_match_cea_mode - look for a CEA mode matching given mode
3735  * @to_match: display mode
3736  *
3737  * Return: The CEA Video ID (VIC) of the mode or 0 if it isn't a CEA-861
3738  * mode.
3739  */
3740 u8 drm_match_cea_mode(const struct drm_display_mode *to_match)
3741 {
3742         unsigned int match_flags = DRM_MODE_MATCH_TIMINGS | DRM_MODE_MATCH_FLAGS;
3743         u8 vic;
3744
3745         if (!to_match->clock)
3746                 return 0;
3747
3748         if (to_match->picture_aspect_ratio)
3749                 match_flags |= DRM_MODE_MATCH_ASPECT_RATIO;
3750
3751         for (vic = 1; vic < cea_num_vics(); vic = cea_next_vic(vic)) {
3752                 struct drm_display_mode cea_mode;
3753                 unsigned int clock1, clock2;
3754
3755                 drm_mode_init(&cea_mode, cea_mode_for_vic(vic));
3756
3757                 /* Check both 60Hz and 59.94Hz */
3758                 clock1 = cea_mode.clock;
3759                 clock2 = cea_mode_alternate_clock(&cea_mode);
3760
3761                 if (KHZ2PICOS(to_match->clock) != KHZ2PICOS(clock1) &&
3762                     KHZ2PICOS(to_match->clock) != KHZ2PICOS(clock2))
3763                         continue;
3764
3765                 do {
3766                         if (drm_mode_match(to_match, &cea_mode, match_flags))
3767                                 return vic;
3768                 } while (cea_mode_alternate_timings(vic, &cea_mode));
3769         }
3770
3771         return 0;
3772 }
3773 EXPORT_SYMBOL(drm_match_cea_mode);
3774
3775 static bool drm_valid_cea_vic(u8 vic)
3776 {
3777         return cea_mode_for_vic(vic) != NULL;
3778 }
3779
3780 static enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code)
3781 {
3782         const struct drm_display_mode *mode = cea_mode_for_vic(video_code);
3783
3784         if (mode)
3785                 return mode->picture_aspect_ratio;
3786
3787         return HDMI_PICTURE_ASPECT_NONE;
3788 }
3789
3790 static enum hdmi_picture_aspect drm_get_hdmi_aspect_ratio(const u8 video_code)
3791 {
3792         return edid_4k_modes[video_code].picture_aspect_ratio;
3793 }
3794
3795 /*
3796  * Calculate the alternate clock for HDMI modes (those from the HDMI vendor
3797  * specific block).
3798  */
3799 static unsigned int
3800 hdmi_mode_alternate_clock(const struct drm_display_mode *hdmi_mode)
3801 {
3802         return cea_mode_alternate_clock(hdmi_mode);
3803 }
3804
3805 static u8 drm_match_hdmi_mode_clock_tolerance(const struct drm_display_mode *to_match,
3806                                               unsigned int clock_tolerance)
3807 {
3808         unsigned int match_flags = DRM_MODE_MATCH_TIMINGS | DRM_MODE_MATCH_FLAGS;
3809         u8 vic;
3810
3811         if (!to_match->clock)
3812                 return 0;
3813
3814         if (to_match->picture_aspect_ratio)
3815                 match_flags |= DRM_MODE_MATCH_ASPECT_RATIO;
3816
3817         for (vic = 1; vic < ARRAY_SIZE(edid_4k_modes); vic++) {
3818                 const struct drm_display_mode *hdmi_mode = &edid_4k_modes[vic];
3819                 unsigned int clock1, clock2;
3820
3821                 /* Make sure to also match alternate clocks */
3822                 clock1 = hdmi_mode->clock;
3823                 clock2 = hdmi_mode_alternate_clock(hdmi_mode);
3824
3825                 if (abs(to_match->clock - clock1) > clock_tolerance &&
3826                     abs(to_match->clock - clock2) > clock_tolerance)
3827                         continue;
3828
3829                 if (drm_mode_match(to_match, hdmi_mode, match_flags))
3830                         return vic;
3831         }
3832
3833         return 0;
3834 }
3835
3836 /*
3837  * drm_match_hdmi_mode - look for a HDMI mode matching given mode
3838  * @to_match: display mode
3839  *
3840  * An HDMI mode is one defined in the HDMI vendor specific block.
3841  *
3842  * Returns the HDMI Video ID (VIC) of the mode or 0 if it isn't one.
3843  */
3844 static u8 drm_match_hdmi_mode(const struct drm_display_mode *to_match)
3845 {
3846         unsigned int match_flags = DRM_MODE_MATCH_TIMINGS | DRM_MODE_MATCH_FLAGS;
3847         u8 vic;
3848
3849         if (!to_match->clock)
3850                 return 0;
3851
3852         if (to_match->picture_aspect_ratio)
3853                 match_flags |= DRM_MODE_MATCH_ASPECT_RATIO;
3854
3855         for (vic = 1; vic < ARRAY_SIZE(edid_4k_modes); vic++) {
3856                 const struct drm_display_mode *hdmi_mode = &edid_4k_modes[vic];
3857                 unsigned int clock1, clock2;
3858
3859                 /* Make sure to also match alternate clocks */
3860                 clock1 = hdmi_mode->clock;
3861                 clock2 = hdmi_mode_alternate_clock(hdmi_mode);
3862
3863                 if ((KHZ2PICOS(to_match->clock) == KHZ2PICOS(clock1) ||
3864                      KHZ2PICOS(to_match->clock) == KHZ2PICOS(clock2)) &&
3865                     drm_mode_match(to_match, hdmi_mode, match_flags))
3866                         return vic;
3867         }
3868         return 0;
3869 }
3870
3871 static bool drm_valid_hdmi_vic(u8 vic)
3872 {
3873         return vic > 0 && vic < ARRAY_SIZE(edid_4k_modes);
3874 }
3875
3876 static int add_alternate_cea_modes(struct drm_connector *connector,
3877                                    const struct drm_edid *drm_edid)
3878 {
3879         struct drm_device *dev = connector->dev;
3880         struct drm_display_mode *mode, *tmp;
3881         LIST_HEAD(list);
3882         int modes = 0;
3883
3884         /* Don't add CTA modes if the CTA extension block is missing */
3885         if (!drm_edid_has_cta_extension(drm_edid))
3886                 return 0;
3887
3888         /*
3889          * Go through all probed modes and create a new mode
3890          * with the alternate clock for certain CEA modes.
3891          */
3892         list_for_each_entry(mode, &connector->probed_modes, head) {
3893                 const struct drm_display_mode *cea_mode = NULL;
3894                 struct drm_display_mode *newmode;
3895                 u8 vic = drm_match_cea_mode(mode);
3896                 unsigned int clock1, clock2;
3897
3898                 if (drm_valid_cea_vic(vic)) {
3899                         cea_mode = cea_mode_for_vic(vic);
3900                         clock2 = cea_mode_alternate_clock(cea_mode);
3901                 } else {
3902                         vic = drm_match_hdmi_mode(mode);
3903                         if (drm_valid_hdmi_vic(vic)) {
3904                                 cea_mode = &edid_4k_modes[vic];
3905                                 clock2 = hdmi_mode_alternate_clock(cea_mode);
3906                         }
3907                 }
3908
3909                 if (!cea_mode)
3910                         continue;
3911
3912                 clock1 = cea_mode->clock;
3913
3914                 if (clock1 == clock2)
3915                         continue;
3916
3917                 if (mode->clock != clock1 && mode->clock != clock2)
3918                         continue;
3919
3920                 newmode = drm_mode_duplicate(dev, cea_mode);
3921                 if (!newmode)
3922                         continue;
3923
3924                 /* Carry over the stereo flags */
3925                 newmode->flags |= mode->flags & DRM_MODE_FLAG_3D_MASK;
3926
3927                 /*
3928                  * The current mode could be either variant. Make
3929                  * sure to pick the "other" clock for the new mode.
3930                  */
3931                 if (mode->clock != clock1)
3932                         newmode->clock = clock1;
3933                 else
3934                         newmode->clock = clock2;
3935
3936                 list_add_tail(&newmode->head, &list);
3937         }
3938
3939         list_for_each_entry_safe(mode, tmp, &list, head) {
3940                 list_del(&mode->head);
3941                 drm_mode_probed_add(connector, mode);
3942                 modes++;
3943         }
3944
3945         return modes;
3946 }
3947
3948 static u8 svd_to_vic(u8 svd)
3949 {
3950         /* 0-6 bit vic, 7th bit native mode indicator */
3951         if ((svd >= 1 &&  svd <= 64) || (svd >= 129 && svd <= 192))
3952                 return svd & 127;
3953
3954         return svd;
3955 }
3956
3957 static struct drm_display_mode *
3958 drm_display_mode_from_vic_index(struct drm_connector *connector,
3959                                 const u8 *video_db, u8 video_len,
3960                                 u8 video_index)
3961 {
3962         struct drm_device *dev = connector->dev;
3963         struct drm_display_mode *newmode;
3964         u8 vic;
3965
3966         if (video_db == NULL || video_index >= video_len)
3967                 return NULL;
3968
3969         /* CEA modes are numbered 1..127 */
3970         vic = svd_to_vic(video_db[video_index]);
3971         if (!drm_valid_cea_vic(vic))
3972                 return NULL;
3973
3974         newmode = drm_mode_duplicate(dev, cea_mode_for_vic(vic));
3975         if (!newmode)
3976                 return NULL;
3977
3978         return newmode;
3979 }
3980
3981 /*
3982  * do_y420vdb_modes - Parse YCBCR 420 only modes
3983  * @connector: connector corresponding to the HDMI sink
3984  * @svds: start of the data block of CEA YCBCR 420 VDB
3985  * @len: length of the CEA YCBCR 420 VDB
3986  *
3987  * Parse the CEA-861-F YCBCR 420 Video Data Block (Y420VDB)
3988  * which contains modes which can be supported in YCBCR 420
3989  * output format only.
3990  */
3991 static int do_y420vdb_modes(struct drm_connector *connector,
3992                             const u8 *svds, u8 svds_len)
3993 {
3994         int modes = 0, i;
3995         struct drm_device *dev = connector->dev;
3996         struct drm_display_info *info = &connector->display_info;
3997         struct drm_hdmi_info *hdmi = &info->hdmi;
3998
3999         for (i = 0; i < svds_len; i++) {
4000                 u8 vic = svd_to_vic(svds[i]);
4001                 struct drm_display_mode *newmode;
4002
4003                 if (!drm_valid_cea_vic(vic))
4004                         continue;
4005
4006                 newmode = drm_mode_duplicate(dev, cea_mode_for_vic(vic));
4007                 if (!newmode)
4008                         break;
4009                 bitmap_set(hdmi->y420_vdb_modes, vic, 1);
4010                 drm_mode_probed_add(connector, newmode);
4011                 modes++;
4012         }
4013
4014         if (modes > 0)
4015                 info->color_formats |= DRM_COLOR_FORMAT_YCBCR420;
4016         return modes;
4017 }
4018
4019 /*
4020  * drm_add_cmdb_modes - Add a YCBCR 420 mode into bitmap
4021  * @connector: connector corresponding to the HDMI sink
4022  * @vic: CEA vic for the video mode to be added in the map
4023  *
4024  * Makes an entry for a videomode in the YCBCR 420 bitmap
4025  */
4026 static void
4027 drm_add_cmdb_modes(struct drm_connector *connector, u8 svd)
4028 {
4029         u8 vic = svd_to_vic(svd);
4030         struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
4031
4032         if (!drm_valid_cea_vic(vic))
4033                 return;
4034
4035         bitmap_set(hdmi->y420_cmdb_modes, vic, 1);
4036 }
4037
4038 /**
4039  * drm_display_mode_from_cea_vic() - return a mode for CEA VIC
4040  * @dev: DRM device
4041  * @video_code: CEA VIC of the mode
4042  *
4043  * Creates a new mode matching the specified CEA VIC.
4044  *
4045  * Returns: A new drm_display_mode on success or NULL on failure
4046  */
4047 struct drm_display_mode *
4048 drm_display_mode_from_cea_vic(struct drm_device *dev,
4049                               u8 video_code)
4050 {
4051         const struct drm_display_mode *cea_mode;
4052         struct drm_display_mode *newmode;
4053
4054         cea_mode = cea_mode_for_vic(video_code);
4055         if (!cea_mode)
4056                 return NULL;
4057
4058         newmode = drm_mode_duplicate(dev, cea_mode);
4059         if (!newmode)
4060                 return NULL;
4061
4062         return newmode;
4063 }
4064 EXPORT_SYMBOL(drm_display_mode_from_cea_vic);
4065
4066 static int
4067 do_cea_modes(struct drm_connector *connector, const u8 *db, u8 len)
4068 {
4069         int i, modes = 0;
4070         struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
4071
4072         for (i = 0; i < len; i++) {
4073                 struct drm_display_mode *mode;
4074
4075                 mode = drm_display_mode_from_vic_index(connector, db, len, i);
4076                 if (mode) {
4077                         /*
4078                          * YCBCR420 capability block contains a bitmap which
4079                          * gives the index of CEA modes from CEA VDB, which
4080                          * can support YCBCR 420 sampling output also (apart
4081                          * from RGB/YCBCR444 etc).
4082                          * For example, if the bit 0 in bitmap is set,
4083                          * first mode in VDB can support YCBCR420 output too.
4084                          * Add YCBCR420 modes only if sink is HDMI 2.0 capable.
4085                          */
4086                         if (i < 64 && hdmi->y420_cmdb_map & (1ULL << i))
4087                                 drm_add_cmdb_modes(connector, db[i]);
4088
4089                         drm_mode_probed_add(connector, mode);
4090                         modes++;
4091                 }
4092         }
4093
4094         return modes;
4095 }
4096
4097 struct stereo_mandatory_mode {
4098         int width, height, vrefresh;
4099         unsigned int flags;
4100 };
4101
4102 static const struct stereo_mandatory_mode stereo_mandatory_modes[] = {
4103         { 1920, 1080, 24, DRM_MODE_FLAG_3D_TOP_AND_BOTTOM },
4104         { 1920, 1080, 24, DRM_MODE_FLAG_3D_FRAME_PACKING },
4105         { 1920, 1080, 50,
4106           DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF },
4107         { 1920, 1080, 60,
4108           DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF },
4109         { 1280, 720,  50, DRM_MODE_FLAG_3D_TOP_AND_BOTTOM },
4110         { 1280, 720,  50, DRM_MODE_FLAG_3D_FRAME_PACKING },
4111         { 1280, 720,  60, DRM_MODE_FLAG_3D_TOP_AND_BOTTOM },
4112         { 1280, 720,  60, DRM_MODE_FLAG_3D_FRAME_PACKING }
4113 };
4114
4115 static bool
4116 stereo_match_mandatory(const struct drm_display_mode *mode,
4117                        const struct stereo_mandatory_mode *stereo_mode)
4118 {
4119         unsigned int interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
4120
4121         return mode->hdisplay == stereo_mode->width &&
4122                mode->vdisplay == stereo_mode->height &&
4123                interlaced == (stereo_mode->flags & DRM_MODE_FLAG_INTERLACE) &&
4124                drm_mode_vrefresh(mode) == stereo_mode->vrefresh;
4125 }
4126
4127 static int add_hdmi_mandatory_stereo_modes(struct drm_connector *connector)
4128 {
4129         struct drm_device *dev = connector->dev;
4130         const struct drm_display_mode *mode;
4131         struct list_head stereo_modes;
4132         int modes = 0, i;
4133
4134         INIT_LIST_HEAD(&stereo_modes);
4135
4136         list_for_each_entry(mode, &connector->probed_modes, head) {
4137                 for (i = 0; i < ARRAY_SIZE(stereo_mandatory_modes); i++) {
4138                         const struct stereo_mandatory_mode *mandatory;
4139                         struct drm_display_mode *new_mode;
4140
4141                         if (!stereo_match_mandatory(mode,
4142                                                     &stereo_mandatory_modes[i]))
4143                                 continue;
4144
4145                         mandatory = &stereo_mandatory_modes[i];
4146                         new_mode = drm_mode_duplicate(dev, mode);
4147                         if (!new_mode)
4148                                 continue;
4149
4150                         new_mode->flags |= mandatory->flags;
4151                         list_add_tail(&new_mode->head, &stereo_modes);
4152                         modes++;
4153                 }
4154         }
4155
4156         list_splice_tail(&stereo_modes, &connector->probed_modes);
4157
4158         return modes;
4159 }
4160
4161 static int add_hdmi_mode(struct drm_connector *connector, u8 vic)
4162 {
4163         struct drm_device *dev = connector->dev;
4164         struct drm_display_mode *newmode;
4165
4166         if (!drm_valid_hdmi_vic(vic)) {
4167                 DRM_ERROR("Unknown HDMI VIC: %d\n", vic);
4168                 return 0;
4169         }
4170
4171         newmode = drm_mode_duplicate(dev, &edid_4k_modes[vic]);
4172         if (!newmode)
4173                 return 0;
4174
4175         drm_mode_probed_add(connector, newmode);
4176
4177         return 1;
4178 }
4179
4180 static int add_3d_struct_modes(struct drm_connector *connector, u16 structure,
4181                                const u8 *video_db, u8 video_len, u8 video_index)
4182 {
4183         struct drm_display_mode *newmode;
4184         int modes = 0;
4185
4186         if (structure & (1 << 0)) {
4187                 newmode = drm_display_mode_from_vic_index(connector, video_db,
4188                                                           video_len,
4189                                                           video_index);
4190                 if (newmode) {
4191                         newmode->flags |= DRM_MODE_FLAG_3D_FRAME_PACKING;
4192                         drm_mode_probed_add(connector, newmode);
4193                         modes++;
4194                 }
4195         }
4196         if (structure & (1 << 6)) {
4197                 newmode = drm_display_mode_from_vic_index(connector, video_db,
4198                                                           video_len,
4199                                                           video_index);
4200                 if (newmode) {
4201                         newmode->flags |= DRM_MODE_FLAG_3D_TOP_AND_BOTTOM;
4202                         drm_mode_probed_add(connector, newmode);
4203                         modes++;
4204                 }
4205         }
4206         if (structure & (1 << 8)) {
4207                 newmode = drm_display_mode_from_vic_index(connector, video_db,
4208                                                           video_len,
4209                                                           video_index);
4210                 if (newmode) {
4211                         newmode->flags |= DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF;
4212                         drm_mode_probed_add(connector, newmode);
4213                         modes++;
4214                 }
4215         }
4216
4217         return modes;
4218 }
4219
4220 /*
4221  * do_hdmi_vsdb_modes - Parse the HDMI Vendor Specific data block
4222  * @connector: connector corresponding to the HDMI sink
4223  * @db: start of the CEA vendor specific block
4224  * @len: length of the CEA block payload, ie. one can access up to db[len]
4225  *
4226  * Parses the HDMI VSDB looking for modes to add to @connector. This function
4227  * also adds the stereo 3d modes when applicable.
4228  */
4229 static int
4230 do_hdmi_vsdb_modes(struct drm_connector *connector, const u8 *db, u8 len,
4231                    const u8 *video_db, u8 video_len)
4232 {
4233         struct drm_display_info *info = &connector->display_info;
4234         int modes = 0, offset = 0, i, multi_present = 0, multi_len;
4235         u8 vic_len, hdmi_3d_len = 0;
4236         u16 mask;
4237         u16 structure_all;
4238
4239         if (len < 8)
4240                 goto out;
4241
4242         /* no HDMI_Video_Present */
4243         if (!(db[8] & (1 << 5)))
4244                 goto out;
4245
4246         /* Latency_Fields_Present */
4247         if (db[8] & (1 << 7))
4248                 offset += 2;
4249
4250         /* I_Latency_Fields_Present */
4251         if (db[8] & (1 << 6))
4252                 offset += 2;
4253
4254         /* the declared length is not long enough for the 2 first bytes
4255          * of additional video format capabilities */
4256         if (len < (8 + offset + 2))
4257                 goto out;
4258
4259         /* 3D_Present */
4260         offset++;
4261         if (db[8 + offset] & (1 << 7)) {
4262                 modes += add_hdmi_mandatory_stereo_modes(connector);
4263
4264                 /* 3D_Multi_present */
4265                 multi_present = (db[8 + offset] & 0x60) >> 5;
4266         }
4267
4268         offset++;
4269         vic_len = db[8 + offset] >> 5;
4270         hdmi_3d_len = db[8 + offset] & 0x1f;
4271
4272         for (i = 0; i < vic_len && len >= (9 + offset + i); i++) {
4273                 u8 vic;
4274
4275                 vic = db[9 + offset + i];
4276                 modes += add_hdmi_mode(connector, vic);
4277         }
4278         offset += 1 + vic_len;
4279
4280         if (multi_present == 1)
4281                 multi_len = 2;
4282         else if (multi_present == 2)
4283                 multi_len = 4;
4284         else
4285                 multi_len = 0;
4286
4287         if (len < (8 + offset + hdmi_3d_len - 1))
4288                 goto out;
4289
4290         if (hdmi_3d_len < multi_len)
4291                 goto out;
4292
4293         if (multi_present == 1 || multi_present == 2) {
4294                 /* 3D_Structure_ALL */
4295                 structure_all = (db[8 + offset] << 8) | db[9 + offset];
4296
4297                 /* check if 3D_MASK is present */
4298                 if (multi_present == 2)
4299                         mask = (db[10 + offset] << 8) | db[11 + offset];
4300                 else
4301                         mask = 0xffff;
4302
4303                 for (i = 0; i < 16; i++) {
4304                         if (mask & (1 << i))
4305                                 modes += add_3d_struct_modes(connector,
4306                                                 structure_all,
4307                                                 video_db,
4308                                                 video_len, i);
4309                 }
4310         }
4311
4312         offset += multi_len;
4313
4314         for (i = 0; i < (hdmi_3d_len - multi_len); i++) {
4315                 int vic_index;
4316                 struct drm_display_mode *newmode = NULL;
4317                 unsigned int newflag = 0;
4318                 bool detail_present;
4319
4320                 detail_present = ((db[8 + offset + i] & 0x0f) > 7);
4321
4322                 if (detail_present && (i + 1 == hdmi_3d_len - multi_len))
4323                         break;
4324
4325                 /* 2D_VIC_order_X */
4326                 vic_index = db[8 + offset + i] >> 4;
4327
4328                 /* 3D_Structure_X */
4329                 switch (db[8 + offset + i] & 0x0f) {
4330                 case 0:
4331                         newflag = DRM_MODE_FLAG_3D_FRAME_PACKING;
4332                         break;
4333                 case 6:
4334                         newflag = DRM_MODE_FLAG_3D_TOP_AND_BOTTOM;
4335                         break;
4336                 case 8:
4337                         /* 3D_Detail_X */
4338                         if ((db[9 + offset + i] >> 4) == 1)
4339                                 newflag = DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF;
4340                         break;
4341                 }
4342
4343                 if (newflag != 0) {
4344                         newmode = drm_display_mode_from_vic_index(connector,
4345                                                                   video_db,
4346                                                                   video_len,
4347                                                                   vic_index);
4348
4349                         if (newmode) {
4350                                 newmode->flags |= newflag;
4351                                 drm_mode_probed_add(connector, newmode);
4352                                 modes++;
4353                         }
4354                 }
4355
4356                 if (detail_present)
4357                         i++;
4358         }
4359
4360 out:
4361         if (modes > 0)
4362                 info->has_hdmi_infoframe = true;
4363         return modes;
4364 }
4365
4366 static int
4367 cea_revision(const u8 *cea)
4368 {
4369         /*
4370          * FIXME is this correct for the DispID variant?
4371          * The DispID spec doesn't really specify whether
4372          * this is the revision of the CEA extension or
4373          * the DispID CEA data block. And the only value
4374          * given as an example is 0.
4375          */
4376         return cea[1];
4377 }
4378
4379 /*
4380  * CTA Data Block iterator.
4381  *
4382  * Iterate through all CTA Data Blocks in both EDID CTA Extensions and DisplayID
4383  * CTA Data Blocks.
4384  *
4385  * struct cea_db *db:
4386  * struct cea_db_iter iter;
4387  *
4388  * cea_db_iter_edid_begin(edid, &iter);
4389  * cea_db_iter_for_each(db, &iter) {
4390  *         // do stuff with db
4391  * }
4392  * cea_db_iter_end(&iter);
4393  */
4394 struct cea_db_iter {
4395         struct drm_edid_iter edid_iter;
4396         struct displayid_iter displayid_iter;
4397
4398         /* Current Data Block Collection. */
4399         const u8 *collection;
4400
4401         /* Current Data Block index in current collection. */
4402         int index;
4403
4404         /* End index in current collection. */
4405         int end;
4406 };
4407
4408 /* CTA-861-H section 7.4 CTA Data BLock Collection */
4409 struct cea_db {
4410         u8 tag_length;
4411         u8 data[];
4412 } __packed;
4413
4414 static int cea_db_tag(const struct cea_db *db)
4415 {
4416         return db->tag_length >> 5;
4417 }
4418
4419 static int cea_db_payload_len(const void *_db)
4420 {
4421         /* FIXME: Transition to passing struct cea_db * everywhere. */
4422         const struct cea_db *db = _db;
4423
4424         return db->tag_length & 0x1f;
4425 }
4426
4427 static const void *cea_db_data(const struct cea_db *db)
4428 {
4429         return db->data;
4430 }
4431
4432 static bool cea_db_is_extended_tag(const struct cea_db *db, int tag)
4433 {
4434         return cea_db_tag(db) == CTA_DB_EXTENDED_TAG &&
4435                 cea_db_payload_len(db) >= 1 &&
4436                 db->data[0] == tag;
4437 }
4438
4439 static bool cea_db_is_vendor(const struct cea_db *db, int vendor_oui)
4440 {
4441         const u8 *data = cea_db_data(db);
4442
4443         return cea_db_tag(db) == CTA_DB_VENDOR &&
4444                 cea_db_payload_len(db) >= 3 &&
4445                 oui(data[2], data[1], data[0]) == vendor_oui;
4446 }
4447
4448 static void cea_db_iter_edid_begin(const struct edid *edid, struct cea_db_iter *iter)
4449 {
4450         memset(iter, 0, sizeof(*iter));
4451
4452         drm_edid_iter_begin(edid, &iter->edid_iter);
4453         displayid_iter_edid_begin(edid, &iter->displayid_iter);
4454 }
4455
4456 static const struct cea_db *
4457 __cea_db_iter_current_block(const struct cea_db_iter *iter)
4458 {
4459         const struct cea_db *db;
4460
4461         if (!iter->collection)
4462                 return NULL;
4463
4464         db = (const struct cea_db *)&iter->collection[iter->index];
4465
4466         if (iter->index + sizeof(*db) <= iter->end &&
4467             iter->index + sizeof(*db) + cea_db_payload_len(db) <= iter->end)
4468                 return db;
4469
4470         return NULL;
4471 }
4472
4473 /*
4474  * References:
4475  * - VESA E-EDID v1.4
4476  * - CTA-861-H section 7.3.3 CTA Extension Version 3
4477  */
4478 static const void *__cea_db_iter_edid_next(struct cea_db_iter *iter)
4479 {
4480         const u8 *ext;
4481
4482         drm_edid_iter_for_each(ext, &iter->edid_iter) {
4483                 /* Only support CTA Extension revision 3+ */
4484                 if (ext[0] != CEA_EXT || cea_revision(ext) < 3)
4485                         continue;
4486
4487                 iter->index = 4;
4488                 iter->end = ext[2];
4489                 if (iter->end == 0)
4490                         iter->end = 127;
4491                 if (iter->end < 4 || iter->end > 127)
4492                         continue;
4493
4494                 return ext;
4495         }
4496
4497         return NULL;
4498 }
4499
4500 /*
4501  * References:
4502  * - DisplayID v1.3 Appendix C: CEA Data Block within a DisplayID Data Block
4503  * - DisplayID v2.0 section 4.10 CTA DisplayID Data Block
4504  *
4505  * Note that the above do not specify any connection between DisplayID Data
4506  * Block revision and CTA Extension versions.
4507  */
4508 static const void *__cea_db_iter_displayid_next(struct cea_db_iter *iter)
4509 {
4510         const struct displayid_block *block;
4511
4512         displayid_iter_for_each(block, &iter->displayid_iter) {
4513                 if (block->tag != DATA_BLOCK_CTA)
4514                         continue;
4515
4516                 /*
4517                  * The displayid iterator has already verified the block bounds
4518                  * in displayid_iter_block().
4519                  */
4520                 iter->index = sizeof(*block);
4521                 iter->end = iter->index + block->num_bytes;
4522
4523                 return block;
4524         }
4525
4526         return NULL;
4527 }
4528
4529 static const struct cea_db *__cea_db_iter_next(struct cea_db_iter *iter)
4530 {
4531         const struct cea_db *db;
4532
4533         if (iter->collection) {
4534                 /* Current collection should always be valid. */
4535                 db = __cea_db_iter_current_block(iter);
4536                 if (WARN_ON(!db)) {
4537                         iter->collection = NULL;
4538                         return NULL;
4539                 }
4540
4541                 /* Next block in CTA Data Block Collection */
4542                 iter->index += sizeof(*db) + cea_db_payload_len(db);
4543
4544                 db = __cea_db_iter_current_block(iter);
4545                 if (db)
4546                         return db;
4547         }
4548
4549         for (;;) {
4550                 /*
4551                  * Find the next CTA Data Block Collection. First iterate all
4552                  * the EDID CTA Extensions, then all the DisplayID CTA blocks.
4553                  *
4554                  * Per DisplayID v1.3 Appendix B: DisplayID as an EDID
4555                  * Extension, it's recommended that DisplayID extensions are
4556                  * exposed after all of the CTA Extensions.
4557                  */
4558                 iter->collection = __cea_db_iter_edid_next(iter);
4559                 if (!iter->collection)
4560                         iter->collection = __cea_db_iter_displayid_next(iter);
4561
4562                 if (!iter->collection)
4563                         return NULL;
4564
4565                 db = __cea_db_iter_current_block(iter);
4566                 if (db)
4567                         return db;
4568         }
4569 }
4570
4571 #define cea_db_iter_for_each(__db, __iter) \
4572         while (((__db) = __cea_db_iter_next(__iter)))
4573
4574 static void cea_db_iter_end(struct cea_db_iter *iter)
4575 {
4576         displayid_iter_end(&iter->displayid_iter);
4577         drm_edid_iter_end(&iter->edid_iter);
4578
4579         memset(iter, 0, sizeof(*iter));
4580 }
4581
4582 static bool cea_db_is_hdmi_vsdb(const struct cea_db *db)
4583 {
4584         return cea_db_is_vendor(db, HDMI_IEEE_OUI) &&
4585                 cea_db_payload_len(db) >= 5;
4586 }
4587
4588 static bool cea_db_is_hdmi_forum_vsdb(const struct cea_db *db)
4589 {
4590         return cea_db_is_vendor(db, HDMI_FORUM_IEEE_OUI) &&
4591                 cea_db_payload_len(db) >= 7;
4592 }
4593
4594 static bool cea_db_is_microsoft_vsdb(const struct cea_db *db)
4595 {
4596         return cea_db_is_vendor(db, MICROSOFT_IEEE_OUI) &&
4597                 cea_db_payload_len(db) == 21;
4598 }
4599
4600 static bool cea_db_is_vcdb(const struct cea_db *db)
4601 {
4602         return cea_db_is_extended_tag(db, CTA_EXT_DB_VIDEO_CAP) &&
4603                 cea_db_payload_len(db) == 2;
4604 }
4605
4606 static bool cea_db_is_hdmi_forum_scdb(const struct cea_db *db)
4607 {
4608         return cea_db_is_extended_tag(db, CTA_EXT_DB_HF_SCDB) &&
4609                 cea_db_payload_len(db) >= 7;
4610 }
4611
4612 static bool cea_db_is_y420cmdb(const struct cea_db *db)
4613 {
4614         return cea_db_is_extended_tag(db, CTA_EXT_DB_420_VIDEO_CAP_MAP);
4615 }
4616
4617 static bool cea_db_is_y420vdb(const struct cea_db *db)
4618 {
4619         return cea_db_is_extended_tag(db, CTA_EXT_DB_420_VIDEO_DATA);
4620 }
4621
4622 static bool cea_db_is_hdmi_hdr_metadata_block(const struct cea_db *db)
4623 {
4624         return cea_db_is_extended_tag(db, CTA_EXT_DB_HDR_STATIC_METADATA) &&
4625                 cea_db_payload_len(db) >= 3;
4626 }
4627
4628 static void drm_parse_y420cmdb_bitmap(struct drm_connector *connector,
4629                                       const u8 *db)
4630 {
4631         struct drm_display_info *info = &connector->display_info;
4632         struct drm_hdmi_info *hdmi = &info->hdmi;
4633         u8 map_len = cea_db_payload_len(db) - 1;
4634         u8 count;
4635         u64 map = 0;
4636
4637         if (map_len == 0) {
4638                 /* All CEA modes support ycbcr420 sampling also.*/
4639                 hdmi->y420_cmdb_map = U64_MAX;
4640                 info->color_formats |= DRM_COLOR_FORMAT_YCBCR420;
4641                 return;
4642         }
4643
4644         /*
4645          * This map indicates which of the existing CEA block modes
4646          * from VDB can support YCBCR420 output too. So if bit=0 is
4647          * set, first mode from VDB can support YCBCR420 output too.
4648          * We will parse and keep this map, before parsing VDB itself
4649          * to avoid going through the same block again and again.
4650          *
4651          * Spec is not clear about max possible size of this block.
4652          * Clamping max bitmap block size at 8 bytes. Every byte can
4653          * address 8 CEA modes, in this way this map can address
4654          * 8*8 = first 64 SVDs.
4655          */
4656         if (WARN_ON_ONCE(map_len > 8))
4657                 map_len = 8;
4658
4659         for (count = 0; count < map_len; count++)
4660                 map |= (u64)db[2 + count] << (8 * count);
4661
4662         if (map)
4663                 info->color_formats |= DRM_COLOR_FORMAT_YCBCR420;
4664
4665         hdmi->y420_cmdb_map = map;
4666 }
4667
4668 static int add_cea_modes(struct drm_connector *connector,
4669                          const struct drm_edid *drm_edid)
4670 {
4671         const struct cea_db *db;
4672         struct cea_db_iter iter;
4673         int modes = 0;
4674
4675         cea_db_iter_edid_begin(drm_edid->edid, &iter);
4676         cea_db_iter_for_each(db, &iter) {
4677                 const u8 *hdmi = NULL, *video = NULL;
4678                 u8 hdmi_len = 0, video_len = 0;
4679
4680                 if (cea_db_tag(db) == CTA_DB_VIDEO) {
4681                         video = cea_db_data(db);
4682                         video_len = cea_db_payload_len(db);
4683                         modes += do_cea_modes(connector, video, video_len);
4684                 } else if (cea_db_is_hdmi_vsdb(db)) {
4685                         /* FIXME: Switch to use cea_db_data() */
4686                         hdmi = (const u8 *)db;
4687                         hdmi_len = cea_db_payload_len(db);
4688                 } else if (cea_db_is_y420vdb(db)) {
4689                         const u8 *vdb420 = cea_db_data(db) + 1;
4690
4691                         /* Add 4:2:0(only) modes present in EDID */
4692                         modes += do_y420vdb_modes(connector, vdb420,
4693                                                   cea_db_payload_len(db) - 1);
4694                 }
4695
4696                 /*
4697                  * We parse the HDMI VSDB after having added the cea modes as we
4698                  * will be patching their flags when the sink supports stereo
4699                  * 3D.
4700                  */
4701                 if (hdmi)
4702                         modes += do_hdmi_vsdb_modes(connector, hdmi, hdmi_len,
4703                                                     video, video_len);
4704         }
4705         cea_db_iter_end(&iter);
4706
4707         return modes;
4708 }
4709
4710 static void fixup_detailed_cea_mode_clock(struct drm_display_mode *mode)
4711 {
4712         const struct drm_display_mode *cea_mode;
4713         int clock1, clock2, clock;
4714         u8 vic;
4715         const char *type;
4716
4717         /*
4718          * allow 5kHz clock difference either way to account for
4719          * the 10kHz clock resolution limit of detailed timings.
4720          */
4721         vic = drm_match_cea_mode_clock_tolerance(mode, 5);
4722         if (drm_valid_cea_vic(vic)) {
4723                 type = "CEA";
4724                 cea_mode = cea_mode_for_vic(vic);
4725                 clock1 = cea_mode->clock;
4726                 clock2 = cea_mode_alternate_clock(cea_mode);
4727         } else {
4728                 vic = drm_match_hdmi_mode_clock_tolerance(mode, 5);
4729                 if (drm_valid_hdmi_vic(vic)) {
4730                         type = "HDMI";
4731                         cea_mode = &edid_4k_modes[vic];
4732                         clock1 = cea_mode->clock;
4733                         clock2 = hdmi_mode_alternate_clock(cea_mode);
4734                 } else {
4735                         return;
4736                 }
4737         }
4738
4739         /* pick whichever is closest */
4740         if (abs(mode->clock - clock1) < abs(mode->clock - clock2))
4741                 clock = clock1;
4742         else
4743                 clock = clock2;
4744
4745         if (mode->clock == clock)
4746                 return;
4747
4748         DRM_DEBUG("detailed mode matches %s VIC %d, adjusting clock %d -> %d\n",
4749                   type, vic, mode->clock, clock);
4750         mode->clock = clock;
4751 }
4752
4753 static uint8_t eotf_supported(const u8 *edid_ext)
4754 {
4755         return edid_ext[2] &
4756                 (BIT(HDMI_EOTF_TRADITIONAL_GAMMA_SDR) |
4757                  BIT(HDMI_EOTF_TRADITIONAL_GAMMA_HDR) |
4758                  BIT(HDMI_EOTF_SMPTE_ST2084) |
4759                  BIT(HDMI_EOTF_BT_2100_HLG));
4760 }
4761
4762 static uint8_t hdr_metadata_type(const u8 *edid_ext)
4763 {
4764         return edid_ext[3] &
4765                 BIT(HDMI_STATIC_METADATA_TYPE1);
4766 }
4767
4768 static void
4769 drm_parse_hdr_metadata_block(struct drm_connector *connector, const u8 *db)
4770 {
4771         u16 len;
4772
4773         len = cea_db_payload_len(db);
4774
4775         connector->hdr_sink_metadata.hdmi_type1.eotf =
4776                                                 eotf_supported(db);
4777         connector->hdr_sink_metadata.hdmi_type1.metadata_type =
4778                                                 hdr_metadata_type(db);
4779
4780         if (len >= 4)
4781                 connector->hdr_sink_metadata.hdmi_type1.max_cll = db[4];
4782         if (len >= 5)
4783                 connector->hdr_sink_metadata.hdmi_type1.max_fall = db[5];
4784         if (len >= 6)
4785                 connector->hdr_sink_metadata.hdmi_type1.min_cll = db[6];
4786 }
4787
4788 static void
4789 drm_parse_hdmi_vsdb_audio(struct drm_connector *connector, const u8 *db)
4790 {
4791         u8 len = cea_db_payload_len(db);
4792
4793         if (len >= 6 && (db[6] & (1 << 7)))
4794                 connector->eld[DRM_ELD_SAD_COUNT_CONN_TYPE] |= DRM_ELD_SUPPORTS_AI;
4795         if (len >= 8) {
4796                 connector->latency_present[0] = db[8] >> 7;
4797                 connector->latency_present[1] = (db[8] >> 6) & 1;
4798         }
4799         if (len >= 9)
4800                 connector->video_latency[0] = db[9];
4801         if (len >= 10)
4802                 connector->audio_latency[0] = db[10];
4803         if (len >= 11)
4804                 connector->video_latency[1] = db[11];
4805         if (len >= 12)
4806                 connector->audio_latency[1] = db[12];
4807
4808         DRM_DEBUG_KMS("HDMI: latency present %d %d, "
4809                       "video latency %d %d, "
4810                       "audio latency %d %d\n",
4811                       connector->latency_present[0],
4812                       connector->latency_present[1],
4813                       connector->video_latency[0],
4814                       connector->video_latency[1],
4815                       connector->audio_latency[0],
4816                       connector->audio_latency[1]);
4817 }
4818
4819 static void
4820 monitor_name(const struct detailed_timing *timing, void *data)
4821 {
4822         const char **res = data;
4823
4824         if (!is_display_descriptor(timing, EDID_DETAIL_MONITOR_NAME))
4825                 return;
4826
4827         *res = timing->data.other_data.data.str.str;
4828 }
4829
4830 static int get_monitor_name(const struct edid *edid, char name[13])
4831 {
4832         const char *edid_name = NULL;
4833         int mnl;
4834
4835         if (!edid || !name)
4836                 return 0;
4837
4838         drm_for_each_detailed_block(edid, monitor_name, &edid_name);
4839         for (mnl = 0; edid_name && mnl < 13; mnl++) {
4840                 if (edid_name[mnl] == 0x0a)
4841                         break;
4842
4843                 name[mnl] = edid_name[mnl];
4844         }
4845
4846         return mnl;
4847 }
4848
4849 /**
4850  * drm_edid_get_monitor_name - fetch the monitor name from the edid
4851  * @edid: monitor EDID information
4852  * @name: pointer to a character array to hold the name of the monitor
4853  * @bufsize: The size of the name buffer (should be at least 14 chars.)
4854  *
4855  */
4856 void drm_edid_get_monitor_name(const struct edid *edid, char *name, int bufsize)
4857 {
4858         int name_length;
4859         char buf[13];
4860
4861         if (bufsize <= 0)
4862                 return;
4863
4864         name_length = min(get_monitor_name(edid, buf), bufsize - 1);
4865         memcpy(name, buf, name_length);
4866         name[name_length] = '\0';
4867 }
4868 EXPORT_SYMBOL(drm_edid_get_monitor_name);
4869
4870 static void clear_eld(struct drm_connector *connector)
4871 {
4872         memset(connector->eld, 0, sizeof(connector->eld));
4873
4874         connector->latency_present[0] = false;
4875         connector->latency_present[1] = false;
4876         connector->video_latency[0] = 0;
4877         connector->audio_latency[0] = 0;
4878         connector->video_latency[1] = 0;
4879         connector->audio_latency[1] = 0;
4880 }
4881
4882 /*
4883  * drm_edid_to_eld - build ELD from EDID
4884  * @connector: connector corresponding to the HDMI/DP sink
4885  * @drm_edid: EDID to parse
4886  *
4887  * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver. The
4888  * HDCP and Port_ID ELD fields are left for the graphics driver to fill in.
4889  */
4890 static void drm_edid_to_eld(struct drm_connector *connector,
4891                             const struct drm_edid *drm_edid)
4892 {
4893         const struct drm_display_info *info = &connector->display_info;
4894         const struct cea_db *db;
4895         struct cea_db_iter iter;
4896         uint8_t *eld = connector->eld;
4897         int total_sad_count = 0;
4898         int mnl;
4899
4900         clear_eld(connector);
4901
4902         if (!drm_edid)
4903                 return;
4904
4905         mnl = get_monitor_name(drm_edid->edid, &eld[DRM_ELD_MONITOR_NAME_STRING]);
4906         DRM_DEBUG_KMS("ELD monitor %s\n", &eld[DRM_ELD_MONITOR_NAME_STRING]);
4907
4908         eld[DRM_ELD_CEA_EDID_VER_MNL] = info->cea_rev << DRM_ELD_CEA_EDID_VER_SHIFT;
4909         eld[DRM_ELD_CEA_EDID_VER_MNL] |= mnl;
4910
4911         eld[DRM_ELD_VER] = DRM_ELD_VER_CEA861D;
4912
4913         eld[DRM_ELD_MANUFACTURER_NAME0] = drm_edid->edid->mfg_id[0];
4914         eld[DRM_ELD_MANUFACTURER_NAME1] = drm_edid->edid->mfg_id[1];
4915         eld[DRM_ELD_PRODUCT_CODE0] = drm_edid->edid->prod_code[0];
4916         eld[DRM_ELD_PRODUCT_CODE1] = drm_edid->edid->prod_code[1];
4917
4918         cea_db_iter_edid_begin(drm_edid->edid, &iter);
4919         cea_db_iter_for_each(db, &iter) {
4920                 const u8 *data = cea_db_data(db);
4921                 int len = cea_db_payload_len(db);
4922                 int sad_count;
4923
4924                 switch (cea_db_tag(db)) {
4925                 case CTA_DB_AUDIO:
4926                         /* Audio Data Block, contains SADs */
4927                         sad_count = min(len / 3, 15 - total_sad_count);
4928                         if (sad_count >= 1)
4929                                 memcpy(&eld[DRM_ELD_CEA_SAD(mnl, total_sad_count)],
4930                                        data, sad_count * 3);
4931                         total_sad_count += sad_count;
4932                         break;
4933                 case CTA_DB_SPEAKER:
4934                         /* Speaker Allocation Data Block */
4935                         if (len >= 1)
4936                                 eld[DRM_ELD_SPEAKER] = data[0];
4937                         break;
4938                 case CTA_DB_VENDOR:
4939                         /* HDMI Vendor-Specific Data Block */
4940                         if (cea_db_is_hdmi_vsdb(db))
4941                                 drm_parse_hdmi_vsdb_audio(connector, (const u8 *)db);
4942                         break;
4943                 default:
4944                         break;
4945                 }
4946         }
4947         cea_db_iter_end(&iter);
4948
4949         eld[DRM_ELD_SAD_COUNT_CONN_TYPE] |= total_sad_count << DRM_ELD_SAD_COUNT_SHIFT;
4950
4951         if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
4952             connector->connector_type == DRM_MODE_CONNECTOR_eDP)
4953                 eld[DRM_ELD_SAD_COUNT_CONN_TYPE] |= DRM_ELD_CONN_TYPE_DP;
4954         else
4955                 eld[DRM_ELD_SAD_COUNT_CONN_TYPE] |= DRM_ELD_CONN_TYPE_HDMI;
4956
4957         eld[DRM_ELD_BASELINE_ELD_LEN] =
4958                 DIV_ROUND_UP(drm_eld_calc_baseline_block_size(eld), 4);
4959
4960         DRM_DEBUG_KMS("ELD size %d, SAD count %d\n",
4961                       drm_eld_size(eld), total_sad_count);
4962 }
4963
4964 /**
4965  * drm_edid_to_sad - extracts SADs from EDID
4966  * @edid: EDID to parse
4967  * @sads: pointer that will be set to the extracted SADs
4968  *
4969  * Looks for CEA EDID block and extracts SADs (Short Audio Descriptors) from it.
4970  *
4971  * Note: The returned pointer needs to be freed using kfree().
4972  *
4973  * Return: The number of found SADs or negative number on error.
4974  */
4975 int drm_edid_to_sad(const struct edid *edid, struct cea_sad **sads)
4976 {
4977         const struct cea_db *db;
4978         struct cea_db_iter iter;
4979         int count = 0;
4980
4981         cea_db_iter_edid_begin(edid, &iter);
4982         cea_db_iter_for_each(db, &iter) {
4983                 if (cea_db_tag(db) == CTA_DB_AUDIO) {
4984                         int j;
4985
4986                         count = cea_db_payload_len(db) / 3; /* SAD is 3B */
4987                         *sads = kcalloc(count, sizeof(**sads), GFP_KERNEL);
4988                         if (!*sads)
4989                                 return -ENOMEM;
4990                         for (j = 0; j < count; j++) {
4991                                 const u8 *sad = &db->data[j * 3];
4992
4993                                 (*sads)[j].format = (sad[0] & 0x78) >> 3;
4994                                 (*sads)[j].channels = sad[0] & 0x7;
4995                                 (*sads)[j].freq = sad[1] & 0x7F;
4996                                 (*sads)[j].byte2 = sad[2];
4997                         }
4998                         break;
4999                 }
5000         }
5001         cea_db_iter_end(&iter);
5002
5003         DRM_DEBUG_KMS("Found %d Short Audio Descriptors\n", count);
5004
5005         return count;
5006 }
5007 EXPORT_SYMBOL(drm_edid_to_sad);
5008
5009 /**
5010  * drm_edid_to_speaker_allocation - extracts Speaker Allocation Data Blocks from EDID
5011  * @edid: EDID to parse
5012  * @sadb: pointer to the speaker block
5013  *
5014  * Looks for CEA EDID block and extracts the Speaker Allocation Data Block from it.
5015  *
5016  * Note: The returned pointer needs to be freed using kfree().
5017  *
5018  * Return: The number of found Speaker Allocation Blocks or negative number on
5019  * error.
5020  */
5021 int drm_edid_to_speaker_allocation(const struct edid *edid, u8 **sadb)
5022 {
5023         const struct cea_db *db;
5024         struct cea_db_iter iter;
5025         int count = 0;
5026
5027         cea_db_iter_edid_begin(edid, &iter);
5028         cea_db_iter_for_each(db, &iter) {
5029                 if (cea_db_tag(db) == CTA_DB_SPEAKER &&
5030                     cea_db_payload_len(db) == 3) {
5031                         *sadb = kmemdup(db->data, cea_db_payload_len(db),
5032                                         GFP_KERNEL);
5033                         if (!*sadb)
5034                                 return -ENOMEM;
5035                         count = cea_db_payload_len(db);
5036                         break;
5037                 }
5038         }
5039         cea_db_iter_end(&iter);
5040
5041         DRM_DEBUG_KMS("Found %d Speaker Allocation Data Blocks\n", count);
5042
5043         return count;
5044 }
5045 EXPORT_SYMBOL(drm_edid_to_speaker_allocation);
5046
5047 /**
5048  * drm_av_sync_delay - compute the HDMI/DP sink audio-video sync delay
5049  * @connector: connector associated with the HDMI/DP sink
5050  * @mode: the display mode
5051  *
5052  * Return: The HDMI/DP sink's audio-video sync delay in milliseconds or 0 if
5053  * the sink doesn't support audio or video.
5054  */
5055 int drm_av_sync_delay(struct drm_connector *connector,
5056                       const struct drm_display_mode *mode)
5057 {
5058         int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
5059         int a, v;
5060
5061         if (!connector->latency_present[0])
5062                 return 0;
5063         if (!connector->latency_present[1])
5064                 i = 0;
5065
5066         a = connector->audio_latency[i];
5067         v = connector->video_latency[i];
5068
5069         /*
5070          * HDMI/DP sink doesn't support audio or video?
5071          */
5072         if (a == 255 || v == 255)
5073                 return 0;
5074
5075         /*
5076          * Convert raw EDID values to millisecond.
5077          * Treat unknown latency as 0ms.
5078          */
5079         if (a)
5080                 a = min(2 * (a - 1), 500);
5081         if (v)
5082                 v = min(2 * (v - 1), 500);
5083
5084         return max(v - a, 0);
5085 }
5086 EXPORT_SYMBOL(drm_av_sync_delay);
5087
5088 /**
5089  * drm_detect_hdmi_monitor - detect whether monitor is HDMI
5090  * @edid: monitor EDID information
5091  *
5092  * Parse the CEA extension according to CEA-861-B.
5093  *
5094  * Drivers that have added the modes parsed from EDID to drm_display_info
5095  * should use &drm_display_info.is_hdmi instead of calling this function.
5096  *
5097  * Return: True if the monitor is HDMI, false if not or unknown.
5098  */
5099 bool drm_detect_hdmi_monitor(const struct edid *edid)
5100 {
5101         const struct cea_db *db;
5102         struct cea_db_iter iter;
5103         bool hdmi = false;
5104
5105         /*
5106          * Because HDMI identifier is in Vendor Specific Block,
5107          * search it from all data blocks of CEA extension.
5108          */
5109         cea_db_iter_edid_begin(edid, &iter);
5110         cea_db_iter_for_each(db, &iter) {
5111                 if (cea_db_is_hdmi_vsdb(db)) {
5112                         hdmi = true;
5113                         break;
5114                 }
5115         }
5116         cea_db_iter_end(&iter);
5117
5118         return hdmi;
5119 }
5120 EXPORT_SYMBOL(drm_detect_hdmi_monitor);
5121
5122 /**
5123  * drm_detect_monitor_audio - check monitor audio capability
5124  * @edid: EDID block to scan
5125  *
5126  * Monitor should have CEA extension block.
5127  * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
5128  * audio' only. If there is any audio extension block and supported
5129  * audio format, assume at least 'basic audio' support, even if 'basic
5130  * audio' is not defined in EDID.
5131  *
5132  * Return: True if the monitor supports audio, false otherwise.
5133  */
5134 bool drm_detect_monitor_audio(const struct edid *edid)
5135 {
5136         struct drm_edid_iter edid_iter;
5137         const struct cea_db *db;
5138         struct cea_db_iter iter;
5139         const u8 *edid_ext;
5140         bool has_audio = false;
5141
5142         drm_edid_iter_begin(edid, &edid_iter);
5143         drm_edid_iter_for_each(edid_ext, &edid_iter) {
5144                 if (edid_ext[0] == CEA_EXT) {
5145                         has_audio = edid_ext[3] & EDID_BASIC_AUDIO;
5146                         if (has_audio)
5147                                 break;
5148                 }
5149         }
5150         drm_edid_iter_end(&edid_iter);
5151
5152         if (has_audio) {
5153                 DRM_DEBUG_KMS("Monitor has basic audio support\n");
5154                 goto end;
5155         }
5156
5157         cea_db_iter_edid_begin(edid, &iter);
5158         cea_db_iter_for_each(db, &iter) {
5159                 if (cea_db_tag(db) == CTA_DB_AUDIO) {
5160                         const u8 *data = cea_db_data(db);
5161                         int i;
5162
5163                         for (i = 0; i < cea_db_payload_len(db); i += 3)
5164                                 DRM_DEBUG_KMS("CEA audio format %d\n",
5165                                               (data[i] >> 3) & 0xf);
5166                         has_audio = true;
5167                         break;
5168                 }
5169         }
5170         cea_db_iter_end(&iter);
5171
5172 end:
5173         return has_audio;
5174 }
5175 EXPORT_SYMBOL(drm_detect_monitor_audio);
5176
5177
5178 /**
5179  * drm_default_rgb_quant_range - default RGB quantization range
5180  * @mode: display mode
5181  *
5182  * Determine the default RGB quantization range for the mode,
5183  * as specified in CEA-861.
5184  *
5185  * Return: The default RGB quantization range for the mode
5186  */
5187 enum hdmi_quantization_range
5188 drm_default_rgb_quant_range(const struct drm_display_mode *mode)
5189 {
5190         /* All CEA modes other than VIC 1 use limited quantization range. */
5191         return drm_match_cea_mode(mode) > 1 ?
5192                 HDMI_QUANTIZATION_RANGE_LIMITED :
5193                 HDMI_QUANTIZATION_RANGE_FULL;
5194 }
5195 EXPORT_SYMBOL(drm_default_rgb_quant_range);
5196
5197 static void drm_parse_vcdb(struct drm_connector *connector, const u8 *db)
5198 {
5199         struct drm_display_info *info = &connector->display_info;
5200
5201         DRM_DEBUG_KMS("CEA VCDB 0x%02x\n", db[2]);
5202
5203         if (db[2] & EDID_CEA_VCDB_QS)
5204                 info->rgb_quant_range_selectable = true;
5205 }
5206
5207 static
5208 void drm_get_max_frl_rate(int max_frl_rate, u8 *max_lanes, u8 *max_rate_per_lane)
5209 {
5210         switch (max_frl_rate) {
5211         case 1:
5212                 *max_lanes = 3;
5213                 *max_rate_per_lane = 3;
5214                 break;
5215         case 2:
5216                 *max_lanes = 3;
5217                 *max_rate_per_lane = 6;
5218                 break;
5219         case 3:
5220                 *max_lanes = 4;
5221                 *max_rate_per_lane = 6;
5222                 break;
5223         case 4:
5224                 *max_lanes = 4;
5225                 *max_rate_per_lane = 8;
5226                 break;
5227         case 5:
5228                 *max_lanes = 4;
5229                 *max_rate_per_lane = 10;
5230                 break;
5231         case 6:
5232                 *max_lanes = 4;
5233                 *max_rate_per_lane = 12;
5234                 break;
5235         case 0:
5236         default:
5237                 *max_lanes = 0;
5238                 *max_rate_per_lane = 0;
5239         }
5240 }
5241
5242 static void drm_parse_ycbcr420_deep_color_info(struct drm_connector *connector,
5243                                                const u8 *db)
5244 {
5245         u8 dc_mask;
5246         struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
5247
5248         dc_mask = db[7] & DRM_EDID_YCBCR420_DC_MASK;
5249         hdmi->y420_dc_modes = dc_mask;
5250 }
5251
5252 /* Sink Capability Data Structure */
5253 static void drm_parse_hdmi_forum_scds(struct drm_connector *connector,
5254                                       const u8 *hf_scds)
5255 {
5256         struct drm_display_info *display = &connector->display_info;
5257         struct drm_hdmi_info *hdmi = &display->hdmi;
5258
5259         display->has_hdmi_infoframe = true;
5260
5261         if (hf_scds[6] & 0x80) {
5262                 hdmi->scdc.supported = true;
5263                 if (hf_scds[6] & 0x40)
5264                         hdmi->scdc.read_request = true;
5265         }
5266
5267         /*
5268          * All HDMI 2.0 monitors must support scrambling at rates > 340 MHz.
5269          * And as per the spec, three factors confirm this:
5270          * * Availability of a HF-VSDB block in EDID (check)
5271          * * Non zero Max_TMDS_Char_Rate filed in HF-VSDB (let's check)
5272          * * SCDC support available (let's check)
5273          * Lets check it out.
5274          */
5275
5276         if (hf_scds[5]) {
5277                 /* max clock is 5000 KHz times block value */
5278                 u32 max_tmds_clock = hf_scds[5] * 5000;
5279                 struct drm_scdc *scdc = &hdmi->scdc;
5280
5281                 if (max_tmds_clock > 340000) {
5282                         display->max_tmds_clock = max_tmds_clock;
5283                         DRM_DEBUG_KMS("HF-VSDB: max TMDS clock %d kHz\n",
5284                                 display->max_tmds_clock);
5285                 }
5286
5287                 if (scdc->supported) {
5288                         scdc->scrambling.supported = true;
5289
5290                         /* Few sinks support scrambling for clocks < 340M */
5291                         if ((hf_scds[6] & 0x8))
5292                                 scdc->scrambling.low_rates = true;
5293                 }
5294         }
5295
5296         if (hf_scds[7]) {
5297                 u8 max_frl_rate;
5298                 u8 dsc_max_frl_rate;
5299                 u8 dsc_max_slices;
5300                 struct drm_hdmi_dsc_cap *hdmi_dsc = &hdmi->dsc_cap;
5301
5302                 DRM_DEBUG_KMS("hdmi_21 sink detected. parsing edid\n");
5303                 max_frl_rate = (hf_scds[7] & DRM_EDID_MAX_FRL_RATE_MASK) >> 4;
5304                 drm_get_max_frl_rate(max_frl_rate, &hdmi->max_lanes,
5305                                      &hdmi->max_frl_rate_per_lane);
5306                 hdmi_dsc->v_1p2 = hf_scds[11] & DRM_EDID_DSC_1P2;
5307
5308                 if (hdmi_dsc->v_1p2) {
5309                         hdmi_dsc->native_420 = hf_scds[11] & DRM_EDID_DSC_NATIVE_420;
5310                         hdmi_dsc->all_bpp = hf_scds[11] & DRM_EDID_DSC_ALL_BPP;
5311
5312                         if (hf_scds[11] & DRM_EDID_DSC_16BPC)
5313                                 hdmi_dsc->bpc_supported = 16;
5314                         else if (hf_scds[11] & DRM_EDID_DSC_12BPC)
5315                                 hdmi_dsc->bpc_supported = 12;
5316                         else if (hf_scds[11] & DRM_EDID_DSC_10BPC)
5317                                 hdmi_dsc->bpc_supported = 10;
5318                         else
5319                                 hdmi_dsc->bpc_supported = 0;
5320
5321                         dsc_max_frl_rate = (hf_scds[12] & DRM_EDID_DSC_MAX_FRL_RATE_MASK) >> 4;
5322                         drm_get_max_frl_rate(dsc_max_frl_rate, &hdmi_dsc->max_lanes,
5323                                              &hdmi_dsc->max_frl_rate_per_lane);
5324                         hdmi_dsc->total_chunk_kbytes = hf_scds[13] & DRM_EDID_DSC_TOTAL_CHUNK_KBYTES;
5325
5326                         dsc_max_slices = hf_scds[12] & DRM_EDID_DSC_MAX_SLICES;
5327                         switch (dsc_max_slices) {
5328                         case 1:
5329                                 hdmi_dsc->max_slices = 1;
5330                                 hdmi_dsc->clk_per_slice = 340;
5331                                 break;
5332                         case 2:
5333                                 hdmi_dsc->max_slices = 2;
5334                                 hdmi_dsc->clk_per_slice = 340;
5335                                 break;
5336                         case 3:
5337                                 hdmi_dsc->max_slices = 4;
5338                                 hdmi_dsc->clk_per_slice = 340;
5339                                 break;
5340                         case 4:
5341                                 hdmi_dsc->max_slices = 8;
5342                                 hdmi_dsc->clk_per_slice = 340;
5343                                 break;
5344                         case 5:
5345                                 hdmi_dsc->max_slices = 8;
5346                                 hdmi_dsc->clk_per_slice = 400;
5347                                 break;
5348                         case 6:
5349                                 hdmi_dsc->max_slices = 12;
5350                                 hdmi_dsc->clk_per_slice = 400;
5351                                 break;
5352                         case 7:
5353                                 hdmi_dsc->max_slices = 16;
5354                                 hdmi_dsc->clk_per_slice = 400;
5355                                 break;
5356                         case 0:
5357                         default:
5358                                 hdmi_dsc->max_slices = 0;
5359                                 hdmi_dsc->clk_per_slice = 0;
5360                         }
5361                 }
5362         }
5363
5364         drm_parse_ycbcr420_deep_color_info(connector, hf_scds);
5365 }
5366
5367 static void drm_parse_hdmi_deep_color_info(struct drm_connector *connector,
5368                                            const u8 *hdmi)
5369 {
5370         struct drm_display_info *info = &connector->display_info;
5371         unsigned int dc_bpc = 0;
5372
5373         /* HDMI supports at least 8 bpc */
5374         info->bpc = 8;
5375
5376         if (cea_db_payload_len(hdmi) < 6)
5377                 return;
5378
5379         if (hdmi[6] & DRM_EDID_HDMI_DC_30) {
5380                 dc_bpc = 10;
5381                 info->edid_hdmi_rgb444_dc_modes |= DRM_EDID_HDMI_DC_30;
5382                 DRM_DEBUG("%s: HDMI sink does deep color 30.\n",
5383                           connector->name);
5384         }
5385
5386         if (hdmi[6] & DRM_EDID_HDMI_DC_36) {
5387                 dc_bpc = 12;
5388                 info->edid_hdmi_rgb444_dc_modes |= DRM_EDID_HDMI_DC_36;
5389                 DRM_DEBUG("%s: HDMI sink does deep color 36.\n",
5390                           connector->name);
5391         }
5392
5393         if (hdmi[6] & DRM_EDID_HDMI_DC_48) {
5394                 dc_bpc = 16;
5395                 info->edid_hdmi_rgb444_dc_modes |= DRM_EDID_HDMI_DC_48;
5396                 DRM_DEBUG("%s: HDMI sink does deep color 48.\n",
5397                           connector->name);
5398         }
5399
5400         if (dc_bpc == 0) {
5401                 DRM_DEBUG("%s: No deep color support on this HDMI sink.\n",
5402                           connector->name);
5403                 return;
5404         }
5405
5406         DRM_DEBUG("%s: Assigning HDMI sink color depth as %d bpc.\n",
5407                   connector->name, dc_bpc);
5408         info->bpc = dc_bpc;
5409
5410         /* YCRCB444 is optional according to spec. */
5411         if (hdmi[6] & DRM_EDID_HDMI_DC_Y444) {
5412                 info->edid_hdmi_ycbcr444_dc_modes = info->edid_hdmi_rgb444_dc_modes;
5413                 DRM_DEBUG("%s: HDMI sink does YCRCB444 in deep color.\n",
5414                           connector->name);
5415         }
5416
5417         /*
5418          * Spec says that if any deep color mode is supported at all,
5419          * then deep color 36 bit must be supported.
5420          */
5421         if (!(hdmi[6] & DRM_EDID_HDMI_DC_36)) {
5422                 DRM_DEBUG("%s: HDMI sink should do DC_36, but does not!\n",
5423                           connector->name);
5424         }
5425 }
5426
5427 static void
5428 drm_parse_hdmi_vsdb_video(struct drm_connector *connector, const u8 *db)
5429 {
5430         struct drm_display_info *info = &connector->display_info;
5431         u8 len = cea_db_payload_len(db);
5432
5433         info->is_hdmi = true;
5434
5435         if (len >= 6)
5436                 info->dvi_dual = db[6] & 1;
5437         if (len >= 7)
5438                 info->max_tmds_clock = db[7] * 5000;
5439
5440         DRM_DEBUG_KMS("HDMI: DVI dual %d, "
5441                       "max TMDS clock %d kHz\n",
5442                       info->dvi_dual,
5443                       info->max_tmds_clock);
5444
5445         drm_parse_hdmi_deep_color_info(connector, db);
5446 }
5447
5448 /*
5449  * See EDID extension for head-mounted and specialized monitors, specified at:
5450  * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/specialized-monitors-edid-extension
5451  */
5452 static void drm_parse_microsoft_vsdb(struct drm_connector *connector,
5453                                      const u8 *db)
5454 {
5455         struct drm_display_info *info = &connector->display_info;
5456         u8 version = db[4];
5457         bool desktop_usage = db[5] & BIT(6);
5458
5459         /* Version 1 and 2 for HMDs, version 3 flags desktop usage explicitly */
5460         if (version == 1 || version == 2 || (version == 3 && !desktop_usage))
5461                 info->non_desktop = true;
5462
5463         drm_dbg_kms(connector->dev, "HMD or specialized display VSDB version %u: 0x%02x\n",
5464                     version, db[5]);
5465 }
5466
5467 static void drm_parse_cea_ext(struct drm_connector *connector,
5468                               const struct drm_edid *drm_edid)
5469 {
5470         struct drm_display_info *info = &connector->display_info;
5471         struct drm_edid_iter edid_iter;
5472         const struct cea_db *db;
5473         struct cea_db_iter iter;
5474         const u8 *edid_ext;
5475
5476         drm_edid_iter_begin(drm_edid->edid, &edid_iter);
5477         drm_edid_iter_for_each(edid_ext, &edid_iter) {
5478                 if (edid_ext[0] != CEA_EXT)
5479                         continue;
5480
5481                 if (!info->cea_rev)
5482                         info->cea_rev = edid_ext[1];
5483
5484                 if (info->cea_rev != edid_ext[1])
5485                         DRM_DEBUG_KMS("CEA extension version mismatch %u != %u\n",
5486                                       info->cea_rev, edid_ext[1]);
5487
5488                 /* The existence of a CTA extension should imply RGB support */
5489                 info->color_formats = DRM_COLOR_FORMAT_RGB444;
5490                 if (edid_ext[3] & EDID_CEA_YCRCB444)
5491                         info->color_formats |= DRM_COLOR_FORMAT_YCBCR444;
5492                 if (edid_ext[3] & EDID_CEA_YCRCB422)
5493                         info->color_formats |= DRM_COLOR_FORMAT_YCBCR422;
5494         }
5495         drm_edid_iter_end(&edid_iter);
5496
5497         cea_db_iter_edid_begin(drm_edid->edid, &iter);
5498         cea_db_iter_for_each(db, &iter) {
5499                 /* FIXME: convert parsers to use struct cea_db */
5500                 const u8 *data = (const u8 *)db;
5501
5502                 if (cea_db_is_hdmi_vsdb(db))
5503                         drm_parse_hdmi_vsdb_video(connector, data);
5504                 else if (cea_db_is_hdmi_forum_vsdb(db) ||
5505                          cea_db_is_hdmi_forum_scdb(db))
5506                         drm_parse_hdmi_forum_scds(connector, data);
5507                 else if (cea_db_is_microsoft_vsdb(db))
5508                         drm_parse_microsoft_vsdb(connector, data);
5509                 else if (cea_db_is_y420cmdb(db))
5510                         drm_parse_y420cmdb_bitmap(connector, data);
5511                 else if (cea_db_is_vcdb(db))
5512                         drm_parse_vcdb(connector, data);
5513                 else if (cea_db_is_hdmi_hdr_metadata_block(db))
5514                         drm_parse_hdr_metadata_block(connector, data);
5515         }
5516         cea_db_iter_end(&iter);
5517 }
5518
5519 static
5520 void get_monitor_range(const struct detailed_timing *timing,
5521                        void *info_monitor_range)
5522 {
5523         struct drm_monitor_range_info *monitor_range = info_monitor_range;
5524         const struct detailed_non_pixel *data = &timing->data.other_data;
5525         const struct detailed_data_monitor_range *range = &data->data.range;
5526
5527         if (!is_display_descriptor(timing, EDID_DETAIL_MONITOR_RANGE))
5528                 return;
5529
5530         /*
5531          * Check for flag range limits only. If flag == 1 then
5532          * no additional timing information provided.
5533          * Default GTF, GTF Secondary curve and CVT are not
5534          * supported
5535          */
5536         if (range->flags != DRM_EDID_RANGE_LIMITS_ONLY_FLAG)
5537                 return;
5538
5539         monitor_range->min_vfreq = range->min_vfreq;
5540         monitor_range->max_vfreq = range->max_vfreq;
5541 }
5542
5543 static void drm_get_monitor_range(struct drm_connector *connector,
5544                                   const struct drm_edid *drm_edid)
5545 {
5546         struct drm_display_info *info = &connector->display_info;
5547
5548         if (!version_greater(drm_edid->edid, 1, 1))
5549                 return;
5550
5551         drm_for_each_detailed_block(drm_edid->edid, get_monitor_range,
5552                                     &info->monitor_range);
5553
5554         DRM_DEBUG_KMS("Supported Monitor Refresh rate range is %d Hz - %d Hz\n",
5555                       info->monitor_range.min_vfreq,
5556                       info->monitor_range.max_vfreq);
5557 }
5558
5559 static void drm_parse_vesa_mso_data(struct drm_connector *connector,
5560                                     const struct displayid_block *block)
5561 {
5562         struct displayid_vesa_vendor_specific_block *vesa =
5563                 (struct displayid_vesa_vendor_specific_block *)block;
5564         struct drm_display_info *info = &connector->display_info;
5565
5566         if (block->num_bytes < 3) {
5567                 drm_dbg_kms(connector->dev, "Unexpected vendor block size %u\n",
5568                             block->num_bytes);
5569                 return;
5570         }
5571
5572         if (oui(vesa->oui[0], vesa->oui[1], vesa->oui[2]) != VESA_IEEE_OUI)
5573                 return;
5574
5575         if (sizeof(*vesa) != sizeof(*block) + block->num_bytes) {
5576                 drm_dbg_kms(connector->dev, "Unexpected VESA vendor block size\n");
5577                 return;
5578         }
5579
5580         switch (FIELD_GET(DISPLAYID_VESA_MSO_MODE, vesa->mso)) {
5581         default:
5582                 drm_dbg_kms(connector->dev, "Reserved MSO mode value\n");
5583                 fallthrough;
5584         case 0:
5585                 info->mso_stream_count = 0;
5586                 break;
5587         case 1:
5588                 info->mso_stream_count = 2; /* 2 or 4 links */
5589                 break;
5590         case 2:
5591                 info->mso_stream_count = 4; /* 4 links */
5592                 break;
5593         }
5594
5595         if (!info->mso_stream_count) {
5596                 info->mso_pixel_overlap = 0;
5597                 return;
5598         }
5599
5600         info->mso_pixel_overlap = FIELD_GET(DISPLAYID_VESA_MSO_OVERLAP, vesa->mso);
5601         if (info->mso_pixel_overlap > 8) {
5602                 drm_dbg_kms(connector->dev, "Reserved MSO pixel overlap value %u\n",
5603                             info->mso_pixel_overlap);
5604                 info->mso_pixel_overlap = 8;
5605         }
5606
5607         drm_dbg_kms(connector->dev, "MSO stream count %u, pixel overlap %u\n",
5608                     info->mso_stream_count, info->mso_pixel_overlap);
5609 }
5610
5611 static void drm_update_mso(struct drm_connector *connector,
5612                            const struct drm_edid *drm_edid)
5613 {
5614         const struct displayid_block *block;
5615         struct displayid_iter iter;
5616
5617         displayid_iter_edid_begin(drm_edid->edid, &iter);
5618         displayid_iter_for_each(block, &iter) {
5619                 if (block->tag == DATA_BLOCK_2_VENDOR_SPECIFIC)
5620                         drm_parse_vesa_mso_data(connector, block);
5621         }
5622         displayid_iter_end(&iter);
5623 }
5624
5625 /* A connector has no EDID information, so we've got no EDID to compute quirks from. Reset
5626  * all of the values which would have been set from EDID
5627  */
5628 void
5629 drm_reset_display_info(struct drm_connector *connector)
5630 {
5631         struct drm_display_info *info = &connector->display_info;
5632
5633         info->width_mm = 0;
5634         info->height_mm = 0;
5635
5636         info->bpc = 0;
5637         info->color_formats = 0;
5638         info->cea_rev = 0;
5639         info->max_tmds_clock = 0;
5640         info->dvi_dual = false;
5641         info->is_hdmi = false;
5642         info->has_hdmi_infoframe = false;
5643         info->rgb_quant_range_selectable = false;
5644         memset(&info->hdmi, 0, sizeof(info->hdmi));
5645
5646         info->edid_hdmi_rgb444_dc_modes = 0;
5647         info->edid_hdmi_ycbcr444_dc_modes = 0;
5648
5649         info->non_desktop = 0;
5650         memset(&info->monitor_range, 0, sizeof(info->monitor_range));
5651
5652         info->mso_stream_count = 0;
5653         info->mso_pixel_overlap = 0;
5654 }
5655
5656 static u32 update_display_info(struct drm_connector *connector,
5657                                const struct drm_edid *drm_edid)
5658 {
5659         struct drm_display_info *info = &connector->display_info;
5660         const struct edid *edid = drm_edid->edid;
5661
5662         u32 quirks = edid_get_quirks(drm_edid);
5663
5664         drm_reset_display_info(connector);
5665
5666         info->width_mm = edid->width_cm * 10;
5667         info->height_mm = edid->height_cm * 10;
5668
5669         drm_get_monitor_range(connector, drm_edid);
5670
5671         if (edid->revision < 3)
5672                 goto out;
5673
5674         if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
5675                 goto out;
5676
5677         info->color_formats |= DRM_COLOR_FORMAT_RGB444;
5678         drm_parse_cea_ext(connector, drm_edid);
5679
5680         /*
5681          * Digital sink with "DFP 1.x compliant TMDS" according to EDID 1.3?
5682          *
5683          * For such displays, the DFP spec 1.0, section 3.10 "EDID support"
5684          * tells us to assume 8 bpc color depth if the EDID doesn't have
5685          * extensions which tell otherwise.
5686          */
5687         if (info->bpc == 0 && edid->revision == 3 &&
5688             edid->input & DRM_EDID_DIGITAL_DFP_1_X) {
5689                 info->bpc = 8;
5690                 DRM_DEBUG("%s: Assigning DFP sink color depth as %d bpc.\n",
5691                           connector->name, info->bpc);
5692         }
5693
5694         /* Only defined for 1.4 with digital displays */
5695         if (edid->revision < 4)
5696                 goto out;
5697
5698         switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
5699         case DRM_EDID_DIGITAL_DEPTH_6:
5700                 info->bpc = 6;
5701                 break;
5702         case DRM_EDID_DIGITAL_DEPTH_8:
5703                 info->bpc = 8;
5704                 break;
5705         case DRM_EDID_DIGITAL_DEPTH_10:
5706                 info->bpc = 10;
5707                 break;
5708         case DRM_EDID_DIGITAL_DEPTH_12:
5709                 info->bpc = 12;
5710                 break;
5711         case DRM_EDID_DIGITAL_DEPTH_14:
5712                 info->bpc = 14;
5713                 break;
5714         case DRM_EDID_DIGITAL_DEPTH_16:
5715                 info->bpc = 16;
5716                 break;
5717         case DRM_EDID_DIGITAL_DEPTH_UNDEF:
5718         default:
5719                 info->bpc = 0;
5720                 break;
5721         }
5722
5723         DRM_DEBUG("%s: Assigning EDID-1.4 digital sink color depth as %d bpc.\n",
5724                           connector->name, info->bpc);
5725
5726         if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
5727                 info->color_formats |= DRM_COLOR_FORMAT_YCBCR444;
5728         if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
5729                 info->color_formats |= DRM_COLOR_FORMAT_YCBCR422;
5730
5731         drm_update_mso(connector, drm_edid);
5732
5733 out:
5734         if (quirks & EDID_QUIRK_NON_DESKTOP) {
5735                 drm_dbg_kms(connector->dev, "Non-desktop display%s\n",
5736                             info->non_desktop ? " (redundant quirk)" : "");
5737                 info->non_desktop = true;
5738         }
5739
5740         return quirks;
5741 }
5742
5743 u32 drm_add_display_info(struct drm_connector *connector, const struct edid *edid)
5744 {
5745         struct drm_edid drm_edid;
5746
5747         return update_display_info(connector,
5748                                    drm_edid_legacy_init(&drm_edid, edid));
5749 }
5750
5751 static struct drm_display_mode *drm_mode_displayid_detailed(struct drm_device *dev,
5752                                                             struct displayid_detailed_timings_1 *timings,
5753                                                             bool type_7)
5754 {
5755         struct drm_display_mode *mode;
5756         unsigned pixel_clock = (timings->pixel_clock[0] |
5757                                 (timings->pixel_clock[1] << 8) |
5758                                 (timings->pixel_clock[2] << 16)) + 1;
5759         unsigned hactive = (timings->hactive[0] | timings->hactive[1] << 8) + 1;
5760         unsigned hblank = (timings->hblank[0] | timings->hblank[1] << 8) + 1;
5761         unsigned hsync = (timings->hsync[0] | (timings->hsync[1] & 0x7f) << 8) + 1;
5762         unsigned hsync_width = (timings->hsw[0] | timings->hsw[1] << 8) + 1;
5763         unsigned vactive = (timings->vactive[0] | timings->vactive[1] << 8) + 1;
5764         unsigned vblank = (timings->vblank[0] | timings->vblank[1] << 8) + 1;
5765         unsigned vsync = (timings->vsync[0] | (timings->vsync[1] & 0x7f) << 8) + 1;
5766         unsigned vsync_width = (timings->vsw[0] | timings->vsw[1] << 8) + 1;
5767         bool hsync_positive = (timings->hsync[1] >> 7) & 0x1;
5768         bool vsync_positive = (timings->vsync[1] >> 7) & 0x1;
5769
5770         mode = drm_mode_create(dev);
5771         if (!mode)
5772                 return NULL;
5773
5774         /* resolution is kHz for type VII, and 10 kHz for type I */
5775         mode->clock = type_7 ? pixel_clock : pixel_clock * 10;
5776         mode->hdisplay = hactive;
5777         mode->hsync_start = mode->hdisplay + hsync;
5778         mode->hsync_end = mode->hsync_start + hsync_width;
5779         mode->htotal = mode->hdisplay + hblank;
5780
5781         mode->vdisplay = vactive;
5782         mode->vsync_start = mode->vdisplay + vsync;
5783         mode->vsync_end = mode->vsync_start + vsync_width;
5784         mode->vtotal = mode->vdisplay + vblank;
5785
5786         mode->flags = 0;
5787         mode->flags |= hsync_positive ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
5788         mode->flags |= vsync_positive ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
5789         mode->type = DRM_MODE_TYPE_DRIVER;
5790
5791         if (timings->flags & 0x80)
5792                 mode->type |= DRM_MODE_TYPE_PREFERRED;
5793         drm_mode_set_name(mode);
5794
5795         return mode;
5796 }
5797
5798 static int add_displayid_detailed_1_modes(struct drm_connector *connector,
5799                                           const struct displayid_block *block)
5800 {
5801         struct displayid_detailed_timing_block *det = (struct displayid_detailed_timing_block *)block;
5802         int i;
5803         int num_timings;
5804         struct drm_display_mode *newmode;
5805         int num_modes = 0;
5806         bool type_7 = block->tag == DATA_BLOCK_2_TYPE_7_DETAILED_TIMING;
5807         /* blocks must be multiple of 20 bytes length */
5808         if (block->num_bytes % 20)
5809                 return 0;
5810
5811         num_timings = block->num_bytes / 20;
5812         for (i = 0; i < num_timings; i++) {
5813                 struct displayid_detailed_timings_1 *timings = &det->timings[i];
5814
5815                 newmode = drm_mode_displayid_detailed(connector->dev, timings, type_7);
5816                 if (!newmode)
5817                         continue;
5818
5819                 drm_mode_probed_add(connector, newmode);
5820                 num_modes++;
5821         }
5822         return num_modes;
5823 }
5824
5825 static int add_displayid_detailed_modes(struct drm_connector *connector,
5826                                         const struct drm_edid *drm_edid)
5827 {
5828         const struct displayid_block *block;
5829         struct displayid_iter iter;
5830         int num_modes = 0;
5831
5832         displayid_iter_edid_begin(drm_edid->edid, &iter);
5833         displayid_iter_for_each(block, &iter) {
5834                 if (block->tag == DATA_BLOCK_TYPE_1_DETAILED_TIMING ||
5835                     block->tag == DATA_BLOCK_2_TYPE_7_DETAILED_TIMING)
5836                         num_modes += add_displayid_detailed_1_modes(connector, block);
5837         }
5838         displayid_iter_end(&iter);
5839
5840         return num_modes;
5841 }
5842
5843 static int drm_edid_connector_update(struct drm_connector *connector,
5844                                      const struct drm_edid *drm_edid)
5845 {
5846         int num_modes = 0;
5847         u32 quirks;
5848
5849         if (!drm_edid) {
5850                 drm_reset_display_info(connector);
5851                 clear_eld(connector);
5852                 return 0;
5853         }
5854
5855         /*
5856          * CEA-861-F adds ycbcr capability map block, for HDMI 2.0 sinks.
5857          * To avoid multiple parsing of same block, lets parse that map
5858          * from sink info, before parsing CEA modes.
5859          */
5860         quirks = update_display_info(connector, drm_edid);
5861
5862         /* Depends on info->cea_rev set by update_display_info() above */
5863         drm_edid_to_eld(connector, drm_edid);
5864
5865         /*
5866          * EDID spec says modes should be preferred in this order:
5867          * - preferred detailed mode
5868          * - other detailed modes from base block
5869          * - detailed modes from extension blocks
5870          * - CVT 3-byte code modes
5871          * - standard timing codes
5872          * - established timing codes
5873          * - modes inferred from GTF or CVT range information
5874          *
5875          * We get this pretty much right.
5876          *
5877          * XXX order for additional mode types in extension blocks?
5878          */
5879         num_modes += add_detailed_modes(connector, drm_edid, quirks);
5880         num_modes += add_cvt_modes(connector, drm_edid);
5881         num_modes += add_standard_modes(connector, drm_edid);
5882         num_modes += add_established_modes(connector, drm_edid);
5883         num_modes += add_cea_modes(connector, drm_edid);
5884         num_modes += add_alternate_cea_modes(connector, drm_edid);
5885         num_modes += add_displayid_detailed_modes(connector, drm_edid);
5886         if (drm_edid->edid->features & DRM_EDID_FEATURE_DEFAULT_GTF)
5887                 num_modes += add_inferred_modes(connector, drm_edid);
5888
5889         if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
5890                 edid_fixup_preferred(connector, quirks);
5891
5892         if (quirks & EDID_QUIRK_FORCE_6BPC)
5893                 connector->display_info.bpc = 6;
5894
5895         if (quirks & EDID_QUIRK_FORCE_8BPC)
5896                 connector->display_info.bpc = 8;
5897
5898         if (quirks & EDID_QUIRK_FORCE_10BPC)
5899                 connector->display_info.bpc = 10;
5900
5901         if (quirks & EDID_QUIRK_FORCE_12BPC)
5902                 connector->display_info.bpc = 12;
5903
5904         return num_modes;
5905 }
5906
5907 /**
5908  * drm_add_edid_modes - add modes from EDID data, if available
5909  * @connector: connector we're probing
5910  * @edid: EDID data
5911  *
5912  * Add the specified modes to the connector's mode list. Also fills out the
5913  * &drm_display_info structure and ELD in @connector with any information which
5914  * can be derived from the edid.
5915  *
5916  * Return: The number of modes added or 0 if we couldn't find any.
5917  */
5918 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
5919 {
5920         struct drm_edid drm_edid;
5921
5922         if (edid && !drm_edid_is_valid(edid)) {
5923                 drm_warn(connector->dev, "%s: EDID invalid.\n",
5924                          connector->name);
5925                 edid = NULL;
5926         }
5927
5928         return drm_edid_connector_update(connector,
5929                                          drm_edid_legacy_init(&drm_edid, edid));
5930 }
5931 EXPORT_SYMBOL(drm_add_edid_modes);
5932
5933 /**
5934  * drm_add_modes_noedid - add modes for the connectors without EDID
5935  * @connector: connector we're probing
5936  * @hdisplay: the horizontal display limit
5937  * @vdisplay: the vertical display limit
5938  *
5939  * Add the specified modes to the connector's mode list. Only when the
5940  * hdisplay/vdisplay is not beyond the given limit, it will be added.
5941  *
5942  * Return: The number of modes added or 0 if we couldn't find any.
5943  */
5944 int drm_add_modes_noedid(struct drm_connector *connector,
5945                         int hdisplay, int vdisplay)
5946 {
5947         int i, count, num_modes = 0;
5948         struct drm_display_mode *mode;
5949         struct drm_device *dev = connector->dev;
5950
5951         count = ARRAY_SIZE(drm_dmt_modes);
5952         if (hdisplay < 0)
5953                 hdisplay = 0;
5954         if (vdisplay < 0)
5955                 vdisplay = 0;
5956
5957         for (i = 0; i < count; i++) {
5958                 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
5959
5960                 if (hdisplay && vdisplay) {
5961                         /*
5962                          * Only when two are valid, they will be used to check
5963                          * whether the mode should be added to the mode list of
5964                          * the connector.
5965                          */
5966                         if (ptr->hdisplay > hdisplay ||
5967                                         ptr->vdisplay > vdisplay)
5968                                 continue;
5969                 }
5970                 if (drm_mode_vrefresh(ptr) > 61)
5971                         continue;
5972                 mode = drm_mode_duplicate(dev, ptr);
5973                 if (mode) {
5974                         drm_mode_probed_add(connector, mode);
5975                         num_modes++;
5976                 }
5977         }
5978         return num_modes;
5979 }
5980 EXPORT_SYMBOL(drm_add_modes_noedid);
5981
5982 /**
5983  * drm_set_preferred_mode - Sets the preferred mode of a connector
5984  * @connector: connector whose mode list should be processed
5985  * @hpref: horizontal resolution of preferred mode
5986  * @vpref: vertical resolution of preferred mode
5987  *
5988  * Marks a mode as preferred if it matches the resolution specified by @hpref
5989  * and @vpref.
5990  */
5991 void drm_set_preferred_mode(struct drm_connector *connector,
5992                            int hpref, int vpref)
5993 {
5994         struct drm_display_mode *mode;
5995
5996         list_for_each_entry(mode, &connector->probed_modes, head) {
5997                 if (mode->hdisplay == hpref &&
5998                     mode->vdisplay == vpref)
5999                         mode->type |= DRM_MODE_TYPE_PREFERRED;
6000         }
6001 }
6002 EXPORT_SYMBOL(drm_set_preferred_mode);
6003
6004 static bool is_hdmi2_sink(const struct drm_connector *connector)
6005 {
6006         /*
6007          * FIXME: sil-sii8620 doesn't have a connector around when
6008          * we need one, so we have to be prepared for a NULL connector.
6009          */
6010         if (!connector)
6011                 return true;
6012
6013         return connector->display_info.hdmi.scdc.supported ||
6014                 connector->display_info.color_formats & DRM_COLOR_FORMAT_YCBCR420;
6015 }
6016
6017 static u8 drm_mode_hdmi_vic(const struct drm_connector *connector,
6018                             const struct drm_display_mode *mode)
6019 {
6020         bool has_hdmi_infoframe = connector ?
6021                 connector->display_info.has_hdmi_infoframe : false;
6022
6023         if (!has_hdmi_infoframe)
6024                 return 0;
6025
6026         /* No HDMI VIC when signalling 3D video format */
6027         if (mode->flags & DRM_MODE_FLAG_3D_MASK)
6028                 return 0;
6029
6030         return drm_match_hdmi_mode(mode);
6031 }
6032
6033 static u8 drm_mode_cea_vic(const struct drm_connector *connector,
6034                            const struct drm_display_mode *mode)
6035 {
6036         u8 vic;
6037
6038         /*
6039          * HDMI spec says if a mode is found in HDMI 1.4b 4K modes
6040          * we should send its VIC in vendor infoframes, else send the
6041          * VIC in AVI infoframes. Lets check if this mode is present in
6042          * HDMI 1.4b 4K modes
6043          */
6044         if (drm_mode_hdmi_vic(connector, mode))
6045                 return 0;
6046
6047         vic = drm_match_cea_mode(mode);
6048
6049         /*
6050          * HDMI 1.4 VIC range: 1 <= VIC <= 64 (CEA-861-D) but
6051          * HDMI 2.0 VIC range: 1 <= VIC <= 107 (CEA-861-F). So we
6052          * have to make sure we dont break HDMI 1.4 sinks.
6053          */
6054         if (!is_hdmi2_sink(connector) && vic > 64)
6055                 return 0;
6056
6057         return vic;
6058 }
6059
6060 /**
6061  * drm_hdmi_avi_infoframe_from_display_mode() - fill an HDMI AVI infoframe with
6062  *                                              data from a DRM display mode
6063  * @frame: HDMI AVI infoframe
6064  * @connector: the connector
6065  * @mode: DRM display mode
6066  *
6067  * Return: 0 on success or a negative error code on failure.
6068  */
6069 int
6070 drm_hdmi_avi_infoframe_from_display_mode(struct hdmi_avi_infoframe *frame,
6071                                          const struct drm_connector *connector,
6072                                          const struct drm_display_mode *mode)
6073 {
6074         enum hdmi_picture_aspect picture_aspect;
6075         u8 vic, hdmi_vic;
6076
6077         if (!frame || !mode)
6078                 return -EINVAL;
6079
6080         hdmi_avi_infoframe_init(frame);
6081
6082         if (mode->flags & DRM_MODE_FLAG_DBLCLK)
6083                 frame->pixel_repeat = 1;
6084
6085         vic = drm_mode_cea_vic(connector, mode);
6086         hdmi_vic = drm_mode_hdmi_vic(connector, mode);
6087
6088         frame->picture_aspect = HDMI_PICTURE_ASPECT_NONE;
6089
6090         /*
6091          * As some drivers don't support atomic, we can't use connector state.
6092          * So just initialize the frame with default values, just the same way
6093          * as it's done with other properties here.
6094          */
6095         frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
6096         frame->itc = 0;
6097
6098         /*
6099          * Populate picture aspect ratio from either
6100          * user input (if specified) or from the CEA/HDMI mode lists.
6101          */
6102         picture_aspect = mode->picture_aspect_ratio;
6103         if (picture_aspect == HDMI_PICTURE_ASPECT_NONE) {
6104                 if (vic)
6105                         picture_aspect = drm_get_cea_aspect_ratio(vic);
6106                 else if (hdmi_vic)
6107                         picture_aspect = drm_get_hdmi_aspect_ratio(hdmi_vic);
6108         }
6109
6110         /*
6111          * The infoframe can't convey anything but none, 4:3
6112          * and 16:9, so if the user has asked for anything else
6113          * we can only satisfy it by specifying the right VIC.
6114          */
6115         if (picture_aspect > HDMI_PICTURE_ASPECT_16_9) {
6116                 if (vic) {
6117                         if (picture_aspect != drm_get_cea_aspect_ratio(vic))
6118                                 return -EINVAL;
6119                 } else if (hdmi_vic) {
6120                         if (picture_aspect != drm_get_hdmi_aspect_ratio(hdmi_vic))
6121                                 return -EINVAL;
6122                 } else {
6123                         return -EINVAL;
6124                 }
6125
6126                 picture_aspect = HDMI_PICTURE_ASPECT_NONE;
6127         }
6128
6129         frame->video_code = vic;
6130         frame->picture_aspect = picture_aspect;
6131         frame->active_aspect = HDMI_ACTIVE_ASPECT_PICTURE;
6132         frame->scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
6133
6134         return 0;
6135 }
6136 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_from_display_mode);
6137
6138 /**
6139  * drm_hdmi_avi_infoframe_quant_range() - fill the HDMI AVI infoframe
6140  *                                        quantization range information
6141  * @frame: HDMI AVI infoframe
6142  * @connector: the connector
6143  * @mode: DRM display mode
6144  * @rgb_quant_range: RGB quantization range (Q)
6145  */
6146 void
6147 drm_hdmi_avi_infoframe_quant_range(struct hdmi_avi_infoframe *frame,
6148                                    const struct drm_connector *connector,
6149                                    const struct drm_display_mode *mode,
6150                                    enum hdmi_quantization_range rgb_quant_range)
6151 {
6152         const struct drm_display_info *info = &connector->display_info;
6153
6154         /*
6155          * CEA-861:
6156          * "A Source shall not send a non-zero Q value that does not correspond
6157          *  to the default RGB Quantization Range for the transmitted Picture
6158          *  unless the Sink indicates support for the Q bit in a Video
6159          *  Capabilities Data Block."
6160          *
6161          * HDMI 2.0 recommends sending non-zero Q when it does match the
6162          * default RGB quantization range for the mode, even when QS=0.
6163          */
6164         if (info->rgb_quant_range_selectable ||
6165             rgb_quant_range == drm_default_rgb_quant_range(mode))
6166                 frame->quantization_range = rgb_quant_range;
6167         else
6168                 frame->quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
6169
6170         /*
6171          * CEA-861-F:
6172          * "When transmitting any RGB colorimetry, the Source should set the
6173          *  YQ-field to match the RGB Quantization Range being transmitted
6174          *  (e.g., when Limited Range RGB, set YQ=0 or when Full Range RGB,
6175          *  set YQ=1) and the Sink shall ignore the YQ-field."
6176          *
6177          * Unfortunate certain sinks (eg. VIZ Model 67/E261VA) get confused
6178          * by non-zero YQ when receiving RGB. There doesn't seem to be any
6179          * good way to tell which version of CEA-861 the sink supports, so
6180          * we limit non-zero YQ to HDMI 2.0 sinks only as HDMI 2.0 is based
6181          * on on CEA-861-F.
6182          */
6183         if (!is_hdmi2_sink(connector) ||
6184             rgb_quant_range == HDMI_QUANTIZATION_RANGE_LIMITED)
6185                 frame->ycc_quantization_range =
6186                         HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
6187         else
6188                 frame->ycc_quantization_range =
6189                         HDMI_YCC_QUANTIZATION_RANGE_FULL;
6190 }
6191 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_quant_range);
6192
6193 static enum hdmi_3d_structure
6194 s3d_structure_from_display_mode(const struct drm_display_mode *mode)
6195 {
6196         u32 layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
6197
6198         switch (layout) {
6199         case DRM_MODE_FLAG_3D_FRAME_PACKING:
6200                 return HDMI_3D_STRUCTURE_FRAME_PACKING;
6201         case DRM_MODE_FLAG_3D_FIELD_ALTERNATIVE:
6202                 return HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE;
6203         case DRM_MODE_FLAG_3D_LINE_ALTERNATIVE:
6204                 return HDMI_3D_STRUCTURE_LINE_ALTERNATIVE;
6205         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_FULL:
6206                 return HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL;
6207         case DRM_MODE_FLAG_3D_L_DEPTH:
6208                 return HDMI_3D_STRUCTURE_L_DEPTH;
6209         case DRM_MODE_FLAG_3D_L_DEPTH_GFX_GFX_DEPTH:
6210                 return HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH;
6211         case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
6212                 return HDMI_3D_STRUCTURE_TOP_AND_BOTTOM;
6213         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
6214                 return HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF;
6215         default:
6216                 return HDMI_3D_STRUCTURE_INVALID;
6217         }
6218 }
6219
6220 /**
6221  * drm_hdmi_vendor_infoframe_from_display_mode() - fill an HDMI infoframe with
6222  * data from a DRM display mode
6223  * @frame: HDMI vendor infoframe
6224  * @connector: the connector
6225  * @mode: DRM display mode
6226  *
6227  * Note that there's is a need to send HDMI vendor infoframes only when using a
6228  * 4k or stereoscopic 3D mode. So when giving any other mode as input this
6229  * function will return -EINVAL, error that can be safely ignored.
6230  *
6231  * Return: 0 on success or a negative error code on failure.
6232  */
6233 int
6234 drm_hdmi_vendor_infoframe_from_display_mode(struct hdmi_vendor_infoframe *frame,
6235                                             const struct drm_connector *connector,
6236                                             const struct drm_display_mode *mode)
6237 {
6238         /*
6239          * FIXME: sil-sii8620 doesn't have a connector around when
6240          * we need one, so we have to be prepared for a NULL connector.
6241          */
6242         bool has_hdmi_infoframe = connector ?
6243                 connector->display_info.has_hdmi_infoframe : false;
6244         int err;
6245
6246         if (!frame || !mode)
6247                 return -EINVAL;
6248
6249         if (!has_hdmi_infoframe)
6250                 return -EINVAL;
6251
6252         err = hdmi_vendor_infoframe_init(frame);
6253         if (err < 0)
6254                 return err;
6255
6256         /*
6257          * Even if it's not absolutely necessary to send the infoframe
6258          * (ie.vic==0 and s3d_struct==0) we will still send it if we
6259          * know that the sink can handle it. This is based on a
6260          * suggestion in HDMI 2.0 Appendix F. Apparently some sinks
6261          * have trouble realizing that they should switch from 3D to 2D
6262          * mode if the source simply stops sending the infoframe when
6263          * it wants to switch from 3D to 2D.
6264          */
6265         frame->vic = drm_mode_hdmi_vic(connector, mode);
6266         frame->s3d_struct = s3d_structure_from_display_mode(mode);
6267
6268         return 0;
6269 }
6270 EXPORT_SYMBOL(drm_hdmi_vendor_infoframe_from_display_mode);
6271
6272 static void drm_parse_tiled_block(struct drm_connector *connector,
6273                                   const struct displayid_block *block)
6274 {
6275         const struct displayid_tiled_block *tile = (struct displayid_tiled_block *)block;
6276         u16 w, h;
6277         u8 tile_v_loc, tile_h_loc;
6278         u8 num_v_tile, num_h_tile;
6279         struct drm_tile_group *tg;
6280
6281         w = tile->tile_size[0] | tile->tile_size[1] << 8;
6282         h = tile->tile_size[2] | tile->tile_size[3] << 8;
6283
6284         num_v_tile = (tile->topo[0] & 0xf) | (tile->topo[2] & 0x30);
6285         num_h_tile = (tile->topo[0] >> 4) | ((tile->topo[2] >> 2) & 0x30);
6286         tile_v_loc = (tile->topo[1] & 0xf) | ((tile->topo[2] & 0x3) << 4);
6287         tile_h_loc = (tile->topo[1] >> 4) | (((tile->topo[2] >> 2) & 0x3) << 4);
6288
6289         connector->has_tile = true;
6290         if (tile->tile_cap & 0x80)
6291                 connector->tile_is_single_monitor = true;
6292
6293         connector->num_h_tile = num_h_tile + 1;
6294         connector->num_v_tile = num_v_tile + 1;
6295         connector->tile_h_loc = tile_h_loc;
6296         connector->tile_v_loc = tile_v_loc;
6297         connector->tile_h_size = w + 1;
6298         connector->tile_v_size = h + 1;
6299
6300         DRM_DEBUG_KMS("tile cap 0x%x\n", tile->tile_cap);
6301         DRM_DEBUG_KMS("tile_size %d x %d\n", w + 1, h + 1);
6302         DRM_DEBUG_KMS("topo num tiles %dx%d, location %dx%d\n",
6303                       num_h_tile + 1, num_v_tile + 1, tile_h_loc, tile_v_loc);
6304         DRM_DEBUG_KMS("vend %c%c%c\n", tile->topology_id[0], tile->topology_id[1], tile->topology_id[2]);
6305
6306         tg = drm_mode_get_tile_group(connector->dev, tile->topology_id);
6307         if (!tg)
6308                 tg = drm_mode_create_tile_group(connector->dev, tile->topology_id);
6309         if (!tg)
6310                 return;
6311
6312         if (connector->tile_group != tg) {
6313                 /* if we haven't got a pointer,
6314                    take the reference, drop ref to old tile group */
6315                 if (connector->tile_group)
6316                         drm_mode_put_tile_group(connector->dev, connector->tile_group);
6317                 connector->tile_group = tg;
6318         } else {
6319                 /* if same tile group, then release the ref we just took. */
6320                 drm_mode_put_tile_group(connector->dev, tg);
6321         }
6322 }
6323
6324 void drm_update_tile_info(struct drm_connector *connector,
6325                           const struct edid *edid)
6326 {
6327         const struct displayid_block *block;
6328         struct displayid_iter iter;
6329
6330         connector->has_tile = false;
6331
6332         displayid_iter_edid_begin(edid, &iter);
6333         displayid_iter_for_each(block, &iter) {
6334                 if (block->tag == DATA_BLOCK_TILED_DISPLAY)
6335                         drm_parse_tiled_block(connector, block);
6336         }
6337         displayid_iter_end(&iter);
6338
6339         if (!connector->has_tile && connector->tile_group) {
6340                 drm_mode_put_tile_group(connector->dev, connector->tile_group);
6341                 connector->tile_group = NULL;
6342         }
6343 }