drm: add connector info/property for non-desktop displays [v2]
[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 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/hdmi.h>
33 #include <linux/i2c.h>
34 #include <linux/module.h>
35 #include <linux/vga_switcheroo.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_encoder.h>
39 #include <drm/drm_displayid.h>
40 #include <drm/drm_scdc_helper.h>
41
42 #include "drm_crtc_internal.h"
43
44 #define version_greater(edid, maj, min) \
45         (((edid)->version > (maj)) || \
46          ((edid)->version == (maj) && (edid)->revision > (min)))
47
48 #define EDID_EST_TIMINGS 16
49 #define EDID_STD_TIMINGS 8
50 #define EDID_DETAILED_TIMINGS 4
51
52 /*
53  * EDID blocks out in the wild have a variety of bugs, try to collect
54  * them here (note that userspace may work around broken monitors first,
55  * but fixes should make their way here so that the kernel "just works"
56  * on as many displays as possible).
57  */
58
59 /* First detailed mode wrong, use largest 60Hz mode */
60 #define EDID_QUIRK_PREFER_LARGE_60              (1 << 0)
61 /* Reported 135MHz pixel clock is too high, needs adjustment */
62 #define EDID_QUIRK_135_CLOCK_TOO_HIGH           (1 << 1)
63 /* Prefer the largest mode at 75 Hz */
64 #define EDID_QUIRK_PREFER_LARGE_75              (1 << 2)
65 /* Detail timing is in cm not mm */
66 #define EDID_QUIRK_DETAILED_IN_CM               (1 << 3)
67 /* Detailed timing descriptors have bogus size values, so just take the
68  * maximum size and use that.
69  */
70 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE    (1 << 4)
71 /* Monitor forgot to set the first detailed is preferred bit. */
72 #define EDID_QUIRK_FIRST_DETAILED_PREFERRED     (1 << 5)
73 /* use +hsync +vsync for detailed mode */
74 #define EDID_QUIRK_DETAILED_SYNC_PP             (1 << 6)
75 /* Force reduced-blanking timings for detailed modes */
76 #define EDID_QUIRK_FORCE_REDUCED_BLANKING       (1 << 7)
77 /* Force 8bpc */
78 #define EDID_QUIRK_FORCE_8BPC                   (1 << 8)
79 /* Force 12bpc */
80 #define EDID_QUIRK_FORCE_12BPC                  (1 << 9)
81 /* Force 6bpc */
82 #define EDID_QUIRK_FORCE_6BPC                   (1 << 10)
83 /* Force 10bpc */
84 #define EDID_QUIRK_FORCE_10BPC                  (1 << 11)
85 /* Non desktop display (i.e. HMD) */
86 #define EDID_QUIRK_NON_DESKTOP                  (1 << 12)
87
88 struct detailed_mode_closure {
89         struct drm_connector *connector;
90         struct edid *edid;
91         bool preferred;
92         u32 quirks;
93         int modes;
94 };
95
96 #define LEVEL_DMT       0
97 #define LEVEL_GTF       1
98 #define LEVEL_GTF2      2
99 #define LEVEL_CVT       3
100
101 static const struct edid_quirk {
102         char vendor[4];
103         int product_id;
104         u32 quirks;
105 } edid_quirk_list[] = {
106         /* Acer AL1706 */
107         { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
108         /* Acer F51 */
109         { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
110         /* Unknown Acer */
111         { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
112
113         /* AEO model 0 reports 8 bpc, but is a 6 bpc panel */
114         { "AEO", 0, EDID_QUIRK_FORCE_6BPC },
115
116         /* Belinea 10 15 55 */
117         { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
118         { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
119
120         /* Envision Peripherals, Inc. EN-7100e */
121         { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
122         /* Envision EN2028 */
123         { "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
124
125         /* Funai Electronics PM36B */
126         { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
127           EDID_QUIRK_DETAILED_IN_CM },
128
129         /* LGD panel of HP zBook 17 G2, eDP 10 bpc, but reports unknown bpc */
130         { "LGD", 764, EDID_QUIRK_FORCE_10BPC },
131
132         /* LG Philips LCD LP154W01-A5 */
133         { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
134         { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
135
136         /* Philips 107p5 CRT */
137         { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
138
139         /* Proview AY765C */
140         { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
141
142         /* Samsung SyncMaster 205BW.  Note: irony */
143         { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
144         /* Samsung SyncMaster 22[5-6]BW */
145         { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
146         { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
147
148         /* Sony PVM-2541A does up to 12 bpc, but only reports max 8 bpc */
149         { "SNY", 0x2541, EDID_QUIRK_FORCE_12BPC },
150
151         /* ViewSonic VA2026w */
152         { "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
153
154         /* Medion MD 30217 PG */
155         { "MED", 0x7b8, EDID_QUIRK_PREFER_LARGE_75 },
156
157         /* Panel in Samsung NP700G7A-S01PL notebook reports 6bpc */
158         { "SEC", 0xd033, EDID_QUIRK_FORCE_8BPC },
159
160         /* Rotel RSX-1058 forwards sink's EDID but only does HDMI 1.1*/
161         { "ETR", 13896, EDID_QUIRK_FORCE_8BPC },
162 };
163
164 /*
165  * Autogenerated from the DMT spec.
166  * This table is copied from xfree86/modes/xf86EdidModes.c.
167  */
168 static const struct drm_display_mode drm_dmt_modes[] = {
169         /* 0x01 - 640x350@85Hz */
170         { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 31500, 640, 672,
171                    736, 832, 0, 350, 382, 385, 445, 0,
172                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
173         /* 0x02 - 640x400@85Hz */
174         { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 31500, 640, 672,
175                    736, 832, 0, 400, 401, 404, 445, 0,
176                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
177         /* 0x03 - 720x400@85Hz */
178         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 756,
179                    828, 936, 0, 400, 401, 404, 446, 0,
180                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
181         /* 0x04 - 640x480@60Hz */
182         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
183                    752, 800, 0, 480, 490, 492, 525, 0,
184                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
185         /* 0x05 - 640x480@72Hz */
186         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
187                    704, 832, 0, 480, 489, 492, 520, 0,
188                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
189         /* 0x06 - 640x480@75Hz */
190         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
191                    720, 840, 0, 480, 481, 484, 500, 0,
192                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
193         /* 0x07 - 640x480@85Hz */
194         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 36000, 640, 696,
195                    752, 832, 0, 480, 481, 484, 509, 0,
196                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
197         /* 0x08 - 800x600@56Hz */
198         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
199                    896, 1024, 0, 600, 601, 603, 625, 0,
200                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
201         /* 0x09 - 800x600@60Hz */
202         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
203                    968, 1056, 0, 600, 601, 605, 628, 0,
204                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
205         /* 0x0a - 800x600@72Hz */
206         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
207                    976, 1040, 0, 600, 637, 643, 666, 0,
208                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
209         /* 0x0b - 800x600@75Hz */
210         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
211                    896, 1056, 0, 600, 601, 604, 625, 0,
212                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
213         /* 0x0c - 800x600@85Hz */
214         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 56250, 800, 832,
215                    896, 1048, 0, 600, 601, 604, 631, 0,
216                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
217         /* 0x0d - 800x600@120Hz RB */
218         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 73250, 800, 848,
219                    880, 960, 0, 600, 603, 607, 636, 0,
220                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
221         /* 0x0e - 848x480@60Hz */
222         { DRM_MODE("848x480", DRM_MODE_TYPE_DRIVER, 33750, 848, 864,
223                    976, 1088, 0, 480, 486, 494, 517, 0,
224                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
225         /* 0x0f - 1024x768@43Hz, interlace */
226         { DRM_MODE("1024x768i", DRM_MODE_TYPE_DRIVER, 44900, 1024, 1032,
227                    1208, 1264, 0, 768, 768, 776, 817, 0,
228                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
229                    DRM_MODE_FLAG_INTERLACE) },
230         /* 0x10 - 1024x768@60Hz */
231         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
232                    1184, 1344, 0, 768, 771, 777, 806, 0,
233                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
234         /* 0x11 - 1024x768@70Hz */
235         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
236                    1184, 1328, 0, 768, 771, 777, 806, 0,
237                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
238         /* 0x12 - 1024x768@75Hz */
239         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78750, 1024, 1040,
240                    1136, 1312, 0, 768, 769, 772, 800, 0,
241                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
242         /* 0x13 - 1024x768@85Hz */
243         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 94500, 1024, 1072,
244                    1168, 1376, 0, 768, 769, 772, 808, 0,
245                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
246         /* 0x14 - 1024x768@120Hz RB */
247         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 115500, 1024, 1072,
248                    1104, 1184, 0, 768, 771, 775, 813, 0,
249                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
250         /* 0x15 - 1152x864@75Hz */
251         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
252                    1344, 1600, 0, 864, 865, 868, 900, 0,
253                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
254         /* 0x55 - 1280x720@60Hz */
255         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
256                    1430, 1650, 0, 720, 725, 730, 750, 0,
257                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
258         /* 0x16 - 1280x768@60Hz RB */
259         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 68250, 1280, 1328,
260                    1360, 1440, 0, 768, 771, 778, 790, 0,
261                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
262         /* 0x17 - 1280x768@60Hz */
263         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
264                    1472, 1664, 0, 768, 771, 778, 798, 0,
265                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
266         /* 0x18 - 1280x768@75Hz */
267         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 102250, 1280, 1360,
268                    1488, 1696, 0, 768, 771, 778, 805, 0,
269                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
270         /* 0x19 - 1280x768@85Hz */
271         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 117500, 1280, 1360,
272                    1496, 1712, 0, 768, 771, 778, 809, 0,
273                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
274         /* 0x1a - 1280x768@120Hz RB */
275         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 140250, 1280, 1328,
276                    1360, 1440, 0, 768, 771, 778, 813, 0,
277                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
278         /* 0x1b - 1280x800@60Hz RB */
279         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 71000, 1280, 1328,
280                    1360, 1440, 0, 800, 803, 809, 823, 0,
281                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
282         /* 0x1c - 1280x800@60Hz */
283         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
284                    1480, 1680, 0, 800, 803, 809, 831, 0,
285                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
286         /* 0x1d - 1280x800@75Hz */
287         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 106500, 1280, 1360,
288                    1488, 1696, 0, 800, 803, 809, 838, 0,
289                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
290         /* 0x1e - 1280x800@85Hz */
291         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 122500, 1280, 1360,
292                    1496, 1712, 0, 800, 803, 809, 843, 0,
293                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
294         /* 0x1f - 1280x800@120Hz RB */
295         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 146250, 1280, 1328,
296                    1360, 1440, 0, 800, 803, 809, 847, 0,
297                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
298         /* 0x20 - 1280x960@60Hz */
299         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
300                    1488, 1800, 0, 960, 961, 964, 1000, 0,
301                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
302         /* 0x21 - 1280x960@85Hz */
303         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1344,
304                    1504, 1728, 0, 960, 961, 964, 1011, 0,
305                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
306         /* 0x22 - 1280x960@120Hz RB */
307         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 175500, 1280, 1328,
308                    1360, 1440, 0, 960, 963, 967, 1017, 0,
309                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
310         /* 0x23 - 1280x1024@60Hz */
311         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
312                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
313                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
314         /* 0x24 - 1280x1024@75Hz */
315         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
316                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
317                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
318         /* 0x25 - 1280x1024@85Hz */
319         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 157500, 1280, 1344,
320                    1504, 1728, 0, 1024, 1025, 1028, 1072, 0,
321                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
322         /* 0x26 - 1280x1024@120Hz RB */
323         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 187250, 1280, 1328,
324                    1360, 1440, 0, 1024, 1027, 1034, 1084, 0,
325                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
326         /* 0x27 - 1360x768@60Hz */
327         { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
328                    1536, 1792, 0, 768, 771, 777, 795, 0,
329                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
330         /* 0x28 - 1360x768@120Hz RB */
331         { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 148250, 1360, 1408,
332                    1440, 1520, 0, 768, 771, 776, 813, 0,
333                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
334         /* 0x51 - 1366x768@60Hz */
335         { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 85500, 1366, 1436,
336                    1579, 1792, 0, 768, 771, 774, 798, 0,
337                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
338         /* 0x56 - 1366x768@60Hz */
339         { DRM_MODE("1366x768", DRM_MODE_TYPE_DRIVER, 72000, 1366, 1380,
340                    1436, 1500, 0, 768, 769, 772, 800, 0,
341                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
342         /* 0x29 - 1400x1050@60Hz RB */
343         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 101000, 1400, 1448,
344                    1480, 1560, 0, 1050, 1053, 1057, 1080, 0,
345                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
346         /* 0x2a - 1400x1050@60Hz */
347         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
348                    1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
349                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
350         /* 0x2b - 1400x1050@75Hz */
351         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 156000, 1400, 1504,
352                    1648, 1896, 0, 1050, 1053, 1057, 1099, 0,
353                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
354         /* 0x2c - 1400x1050@85Hz */
355         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 179500, 1400, 1504,
356                    1656, 1912, 0, 1050, 1053, 1057, 1105, 0,
357                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
358         /* 0x2d - 1400x1050@120Hz RB */
359         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 208000, 1400, 1448,
360                    1480, 1560, 0, 1050, 1053, 1057, 1112, 0,
361                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
362         /* 0x2e - 1440x900@60Hz RB */
363         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 88750, 1440, 1488,
364                    1520, 1600, 0, 900, 903, 909, 926, 0,
365                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
366         /* 0x2f - 1440x900@60Hz */
367         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
368                    1672, 1904, 0, 900, 903, 909, 934, 0,
369                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
370         /* 0x30 - 1440x900@75Hz */
371         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 136750, 1440, 1536,
372                    1688, 1936, 0, 900, 903, 909, 942, 0,
373                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
374         /* 0x31 - 1440x900@85Hz */
375         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 157000, 1440, 1544,
376                    1696, 1952, 0, 900, 903, 909, 948, 0,
377                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
378         /* 0x32 - 1440x900@120Hz RB */
379         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 182750, 1440, 1488,
380                    1520, 1600, 0, 900, 903, 909, 953, 0,
381                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
382         /* 0x53 - 1600x900@60Hz */
383         { DRM_MODE("1600x900", DRM_MODE_TYPE_DRIVER, 108000, 1600, 1624,
384                    1704, 1800, 0, 900, 901, 904, 1000, 0,
385                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
386         /* 0x33 - 1600x1200@60Hz */
387         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
388                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
389                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
390         /* 0x34 - 1600x1200@65Hz */
391         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 175500, 1600, 1664,
392                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
393                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
394         /* 0x35 - 1600x1200@70Hz */
395         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 189000, 1600, 1664,
396                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
397                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
398         /* 0x36 - 1600x1200@75Hz */
399         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 202500, 1600, 1664,
400                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
401                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
402         /* 0x37 - 1600x1200@85Hz */
403         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 229500, 1600, 1664,
404                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
405                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
406         /* 0x38 - 1600x1200@120Hz RB */
407         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 268250, 1600, 1648,
408                    1680, 1760, 0, 1200, 1203, 1207, 1271, 0,
409                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
410         /* 0x39 - 1680x1050@60Hz RB */
411         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 119000, 1680, 1728,
412                    1760, 1840, 0, 1050, 1053, 1059, 1080, 0,
413                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
414         /* 0x3a - 1680x1050@60Hz */
415         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
416                    1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
417                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
418         /* 0x3b - 1680x1050@75Hz */
419         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 187000, 1680, 1800,
420                    1976, 2272, 0, 1050, 1053, 1059, 1099, 0,
421                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
422         /* 0x3c - 1680x1050@85Hz */
423         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 214750, 1680, 1808,
424                    1984, 2288, 0, 1050, 1053, 1059, 1105, 0,
425                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
426         /* 0x3d - 1680x1050@120Hz RB */
427         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 245500, 1680, 1728,
428                    1760, 1840, 0, 1050, 1053, 1059, 1112, 0,
429                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
430         /* 0x3e - 1792x1344@60Hz */
431         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
432                    2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
433                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
434         /* 0x3f - 1792x1344@75Hz */
435         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 261000, 1792, 1888,
436                    2104, 2456, 0, 1344, 1345, 1348, 1417, 0,
437                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
438         /* 0x40 - 1792x1344@120Hz RB */
439         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 333250, 1792, 1840,
440                    1872, 1952, 0, 1344, 1347, 1351, 1423, 0,
441                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
442         /* 0x41 - 1856x1392@60Hz */
443         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
444                    2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
445                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
446         /* 0x42 - 1856x1392@75Hz */
447         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 288000, 1856, 1984,
448                    2208, 2560, 0, 1392, 1393, 1396, 1500, 0,
449                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
450         /* 0x43 - 1856x1392@120Hz RB */
451         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 356500, 1856, 1904,
452                    1936, 2016, 0, 1392, 1395, 1399, 1474, 0,
453                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
454         /* 0x52 - 1920x1080@60Hz */
455         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
456                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
457                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
458         /* 0x44 - 1920x1200@60Hz RB */
459         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 154000, 1920, 1968,
460                    2000, 2080, 0, 1200, 1203, 1209, 1235, 0,
461                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
462         /* 0x45 - 1920x1200@60Hz */
463         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
464                    2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
465                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
466         /* 0x46 - 1920x1200@75Hz */
467         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 245250, 1920, 2056,
468                    2264, 2608, 0, 1200, 1203, 1209, 1255, 0,
469                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
470         /* 0x47 - 1920x1200@85Hz */
471         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 281250, 1920, 2064,
472                    2272, 2624, 0, 1200, 1203, 1209, 1262, 0,
473                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
474         /* 0x48 - 1920x1200@120Hz RB */
475         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 317000, 1920, 1968,
476                    2000, 2080, 0, 1200, 1203, 1209, 1271, 0,
477                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
478         /* 0x49 - 1920x1440@60Hz */
479         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
480                    2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
481                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
482         /* 0x4a - 1920x1440@75Hz */
483         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2064,
484                    2288, 2640, 0, 1440, 1441, 1444, 1500, 0,
485                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
486         /* 0x4b - 1920x1440@120Hz RB */
487         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 380500, 1920, 1968,
488                    2000, 2080, 0, 1440, 1443, 1447, 1525, 0,
489                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
490         /* 0x54 - 2048x1152@60Hz */
491         { DRM_MODE("2048x1152", DRM_MODE_TYPE_DRIVER, 162000, 2048, 2074,
492                    2154, 2250, 0, 1152, 1153, 1156, 1200, 0,
493                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
494         /* 0x4c - 2560x1600@60Hz RB */
495         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 268500, 2560, 2608,
496                    2640, 2720, 0, 1600, 1603, 1609, 1646, 0,
497                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
498         /* 0x4d - 2560x1600@60Hz */
499         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
500                    3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
501                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
502         /* 0x4e - 2560x1600@75Hz */
503         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 443250, 2560, 2768,
504                    3048, 3536, 0, 1600, 1603, 1609, 1672, 0,
505                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
506         /* 0x4f - 2560x1600@85Hz */
507         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 505250, 2560, 2768,
508                    3048, 3536, 0, 1600, 1603, 1609, 1682, 0,
509                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
510         /* 0x50 - 2560x1600@120Hz RB */
511         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 552750, 2560, 2608,
512                    2640, 2720, 0, 1600, 1603, 1609, 1694, 0,
513                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
514         /* 0x57 - 4096x2160@60Hz RB */
515         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 556744, 4096, 4104,
516                    4136, 4176, 0, 2160, 2208, 2216, 2222, 0,
517                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
518         /* 0x58 - 4096x2160@59.94Hz RB */
519         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 556188, 4096, 4104,
520                    4136, 4176, 0, 2160, 2208, 2216, 2222, 0,
521                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
522 };
523
524 /*
525  * These more or less come from the DMT spec.  The 720x400 modes are
526  * inferred from historical 80x25 practice.  The 640x480@67 and 832x624@75
527  * modes are old-school Mac modes.  The EDID spec says the 1152x864@75 mode
528  * should be 1152x870, again for the Mac, but instead we use the x864 DMT
529  * mode.
530  *
531  * The DMT modes have been fact-checked; the rest are mild guesses.
532  */
533 static const struct drm_display_mode edid_est_modes[] = {
534         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
535                    968, 1056, 0, 600, 601, 605, 628, 0,
536                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
537         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
538                    896, 1024, 0, 600, 601, 603,  625, 0,
539                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
540         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
541                    720, 840, 0, 480, 481, 484, 500, 0,
542                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
543         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
544                    704,  832, 0, 480, 489, 492, 520, 0,
545                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
546         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
547                    768,  864, 0, 480, 483, 486, 525, 0,
548                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
549         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
550                    752, 800, 0, 480, 490, 492, 525, 0,
551                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
552         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
553                    846, 900, 0, 400, 421, 423,  449, 0,
554                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
555         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
556                    846,  900, 0, 400, 412, 414, 449, 0,
557                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
558         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
559                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
560                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
561         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78750, 1024, 1040,
562                    1136, 1312, 0,  768, 769, 772, 800, 0,
563                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
564         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
565                    1184, 1328, 0,  768, 771, 777, 806, 0,
566                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
567         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
568                    1184, 1344, 0,  768, 771, 777, 806, 0,
569                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
570         { DRM_MODE("1024x768i", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
571                    1208, 1264, 0, 768, 768, 776, 817, 0,
572                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
573         { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
574                    928, 1152, 0, 624, 625, 628, 667, 0,
575                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
576         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
577                    896, 1056, 0, 600, 601, 604,  625, 0,
578                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
579         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
580                    976, 1040, 0, 600, 637, 643, 666, 0,
581                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
582         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
583                    1344, 1600, 0,  864, 865, 868, 900, 0,
584                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
585 };
586
587 struct minimode {
588         short w;
589         short h;
590         short r;
591         short rb;
592 };
593
594 static const struct minimode est3_modes[] = {
595         /* byte 6 */
596         { 640, 350, 85, 0 },
597         { 640, 400, 85, 0 },
598         { 720, 400, 85, 0 },
599         { 640, 480, 85, 0 },
600         { 848, 480, 60, 0 },
601         { 800, 600, 85, 0 },
602         { 1024, 768, 85, 0 },
603         { 1152, 864, 75, 0 },
604         /* byte 7 */
605         { 1280, 768, 60, 1 },
606         { 1280, 768, 60, 0 },
607         { 1280, 768, 75, 0 },
608         { 1280, 768, 85, 0 },
609         { 1280, 960, 60, 0 },
610         { 1280, 960, 85, 0 },
611         { 1280, 1024, 60, 0 },
612         { 1280, 1024, 85, 0 },
613         /* byte 8 */
614         { 1360, 768, 60, 0 },
615         { 1440, 900, 60, 1 },
616         { 1440, 900, 60, 0 },
617         { 1440, 900, 75, 0 },
618         { 1440, 900, 85, 0 },
619         { 1400, 1050, 60, 1 },
620         { 1400, 1050, 60, 0 },
621         { 1400, 1050, 75, 0 },
622         /* byte 9 */
623         { 1400, 1050, 85, 0 },
624         { 1680, 1050, 60, 1 },
625         { 1680, 1050, 60, 0 },
626         { 1680, 1050, 75, 0 },
627         { 1680, 1050, 85, 0 },
628         { 1600, 1200, 60, 0 },
629         { 1600, 1200, 65, 0 },
630         { 1600, 1200, 70, 0 },
631         /* byte 10 */
632         { 1600, 1200, 75, 0 },
633         { 1600, 1200, 85, 0 },
634         { 1792, 1344, 60, 0 },
635         { 1792, 1344, 75, 0 },
636         { 1856, 1392, 60, 0 },
637         { 1856, 1392, 75, 0 },
638         { 1920, 1200, 60, 1 },
639         { 1920, 1200, 60, 0 },
640         /* byte 11 */
641         { 1920, 1200, 75, 0 },
642         { 1920, 1200, 85, 0 },
643         { 1920, 1440, 60, 0 },
644         { 1920, 1440, 75, 0 },
645 };
646
647 static const struct minimode extra_modes[] = {
648         { 1024, 576,  60, 0 },
649         { 1366, 768,  60, 0 },
650         { 1600, 900,  60, 0 },
651         { 1680, 945,  60, 0 },
652         { 1920, 1080, 60, 0 },
653         { 2048, 1152, 60, 0 },
654         { 2048, 1536, 60, 0 },
655 };
656
657 /*
658  * Probably taken from CEA-861 spec.
659  * This table is converted from xorg's hw/xfree86/modes/xf86EdidModes.c.
660  *
661  * Index using the VIC.
662  */
663 static const struct drm_display_mode edid_cea_modes[] = {
664         /* 0 - dummy, VICs start at 1 */
665         { },
666         /* 1 - 640x480@60Hz */
667         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
668                    752, 800, 0, 480, 490, 492, 525, 0,
669                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
670           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
671         /* 2 - 720x480@60Hz */
672         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
673                    798, 858, 0, 480, 489, 495, 525, 0,
674                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
675           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
676         /* 3 - 720x480@60Hz */
677         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
678                    798, 858, 0, 480, 489, 495, 525, 0,
679                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
680           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
681         /* 4 - 1280x720@60Hz */
682         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
683                    1430, 1650, 0, 720, 725, 730, 750, 0,
684                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
685           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
686         /* 5 - 1920x1080i@60Hz */
687         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
688                    2052, 2200, 0, 1080, 1084, 1094, 1125, 0,
689                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
690                         DRM_MODE_FLAG_INTERLACE),
691           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
692         /* 6 - 720(1440)x480i@60Hz */
693         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
694                    801, 858, 0, 480, 488, 494, 525, 0,
695                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
696                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
697           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
698         /* 7 - 720(1440)x480i@60Hz */
699         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
700                    801, 858, 0, 480, 488, 494, 525, 0,
701                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
702                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
703           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
704         /* 8 - 720(1440)x240@60Hz */
705         { DRM_MODE("720x240", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
706                    801, 858, 0, 240, 244, 247, 262, 0,
707                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
708                         DRM_MODE_FLAG_DBLCLK),
709           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
710         /* 9 - 720(1440)x240@60Hz */
711         { DRM_MODE("720x240", DRM_MODE_TYPE_DRIVER, 13500, 720, 739,
712                    801, 858, 0, 240, 244, 247, 262, 0,
713                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
714                         DRM_MODE_FLAG_DBLCLK),
715           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
716         /* 10 - 2880x480i@60Hz */
717         { DRM_MODE("2880x480i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
718                    3204, 3432, 0, 480, 488, 494, 525, 0,
719                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
720                         DRM_MODE_FLAG_INTERLACE),
721           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
722         /* 11 - 2880x480i@60Hz */
723         { DRM_MODE("2880x480i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
724                    3204, 3432, 0, 480, 488, 494, 525, 0,
725                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
726                         DRM_MODE_FLAG_INTERLACE),
727           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
728         /* 12 - 2880x240@60Hz */
729         { DRM_MODE("2880x240", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
730                    3204, 3432, 0, 240, 244, 247, 262, 0,
731                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
732           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
733         /* 13 - 2880x240@60Hz */
734         { DRM_MODE("2880x240", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2956,
735                    3204, 3432, 0, 240, 244, 247, 262, 0,
736                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
737           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
738         /* 14 - 1440x480@60Hz */
739         { DRM_MODE("1440x480", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1472,
740                    1596, 1716, 0, 480, 489, 495, 525, 0,
741                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
742           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
743         /* 15 - 1440x480@60Hz */
744         { DRM_MODE("1440x480", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1472,
745                    1596, 1716, 0, 480, 489, 495, 525, 0,
746                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
747           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
748         /* 16 - 1920x1080@60Hz */
749         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
750                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
751                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
752           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
753         /* 17 - 720x576@50Hz */
754         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
755                    796, 864, 0, 576, 581, 586, 625, 0,
756                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
757           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
758         /* 18 - 720x576@50Hz */
759         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
760                    796, 864, 0, 576, 581, 586, 625, 0,
761                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
762           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
763         /* 19 - 1280x720@50Hz */
764         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
765                    1760, 1980, 0, 720, 725, 730, 750, 0,
766                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
767           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
768         /* 20 - 1920x1080i@50Hz */
769         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
770                    2492, 2640, 0, 1080, 1084, 1094, 1125, 0,
771                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
772                         DRM_MODE_FLAG_INTERLACE),
773           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
774         /* 21 - 720(1440)x576i@50Hz */
775         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
776                    795, 864, 0, 576, 580, 586, 625, 0,
777                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
778                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
779           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
780         /* 22 - 720(1440)x576i@50Hz */
781         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
782                    795, 864, 0, 576, 580, 586, 625, 0,
783                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
784                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
785           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
786         /* 23 - 720(1440)x288@50Hz */
787         { DRM_MODE("720x288", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
788                    795, 864, 0, 288, 290, 293, 312, 0,
789                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
790                         DRM_MODE_FLAG_DBLCLK),
791           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
792         /* 24 - 720(1440)x288@50Hz */
793         { DRM_MODE("720x288", DRM_MODE_TYPE_DRIVER, 13500, 720, 732,
794                    795, 864, 0, 288, 290, 293, 312, 0,
795                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
796                         DRM_MODE_FLAG_DBLCLK),
797           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
798         /* 25 - 2880x576i@50Hz */
799         { DRM_MODE("2880x576i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
800                    3180, 3456, 0, 576, 580, 586, 625, 0,
801                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
802                         DRM_MODE_FLAG_INTERLACE),
803           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
804         /* 26 - 2880x576i@50Hz */
805         { DRM_MODE("2880x576i", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
806                    3180, 3456, 0, 576, 580, 586, 625, 0,
807                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
808                         DRM_MODE_FLAG_INTERLACE),
809           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
810         /* 27 - 2880x288@50Hz */
811         { DRM_MODE("2880x288", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
812                    3180, 3456, 0, 288, 290, 293, 312, 0,
813                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
814           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
815         /* 28 - 2880x288@50Hz */
816         { DRM_MODE("2880x288", DRM_MODE_TYPE_DRIVER, 54000, 2880, 2928,
817                    3180, 3456, 0, 288, 290, 293, 312, 0,
818                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
819           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
820         /* 29 - 1440x576@50Hz */
821         { DRM_MODE("1440x576", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1464,
822                    1592, 1728, 0, 576, 581, 586, 625, 0,
823                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
824           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
825         /* 30 - 1440x576@50Hz */
826         { DRM_MODE("1440x576", DRM_MODE_TYPE_DRIVER, 54000, 1440, 1464,
827                    1592, 1728, 0, 576, 581, 586, 625, 0,
828                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
829           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
830         /* 31 - 1920x1080@50Hz */
831         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
832                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
833                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
834           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
835         /* 32 - 1920x1080@24Hz */
836         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2558,
837                    2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
838                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
839           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
840         /* 33 - 1920x1080@25Hz */
841         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
842                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
843                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
844           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
845         /* 34 - 1920x1080@30Hz */
846         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
847                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
848                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
849           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
850         /* 35 - 2880x480@60Hz */
851         { DRM_MODE("2880x480", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2944,
852                    3192, 3432, 0, 480, 489, 495, 525, 0,
853                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
854           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
855         /* 36 - 2880x480@60Hz */
856         { DRM_MODE("2880x480", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2944,
857                    3192, 3432, 0, 480, 489, 495, 525, 0,
858                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
859           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
860         /* 37 - 2880x576@50Hz */
861         { DRM_MODE("2880x576", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2928,
862                    3184, 3456, 0, 576, 581, 586, 625, 0,
863                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
864           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
865         /* 38 - 2880x576@50Hz */
866         { DRM_MODE("2880x576", DRM_MODE_TYPE_DRIVER, 108000, 2880, 2928,
867                    3184, 3456, 0, 576, 581, 586, 625, 0,
868                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
869           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
870         /* 39 - 1920x1080i@50Hz */
871         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 72000, 1920, 1952,
872                    2120, 2304, 0, 1080, 1126, 1136, 1250, 0,
873                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC |
874                         DRM_MODE_FLAG_INTERLACE),
875           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
876         /* 40 - 1920x1080i@100Hz */
877         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
878                    2492, 2640, 0, 1080, 1084, 1094, 1125, 0,
879                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
880                         DRM_MODE_FLAG_INTERLACE),
881           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
882         /* 41 - 1280x720@100Hz */
883         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1720,
884                    1760, 1980, 0, 720, 725, 730, 750, 0,
885                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
886           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
887         /* 42 - 720x576@100Hz */
888         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
889                    796, 864, 0, 576, 581, 586, 625, 0,
890                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
891           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
892         /* 43 - 720x576@100Hz */
893         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
894                    796, 864, 0, 576, 581, 586, 625, 0,
895                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
896           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
897         /* 44 - 720(1440)x576i@100Hz */
898         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
899                    795, 864, 0, 576, 580, 586, 625, 0,
900                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
901                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
902           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
903         /* 45 - 720(1440)x576i@100Hz */
904         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
905                    795, 864, 0, 576, 580, 586, 625, 0,
906                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
907                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
908           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
909         /* 46 - 1920x1080i@120Hz */
910         { DRM_MODE("1920x1080i", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
911                    2052, 2200, 0, 1080, 1084, 1094, 1125, 0,
912                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC |
913                         DRM_MODE_FLAG_INTERLACE),
914           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
915         /* 47 - 1280x720@120Hz */
916         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1390,
917                    1430, 1650, 0, 720, 725, 730, 750, 0,
918                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
919           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
920         /* 48 - 720x480@120Hz */
921         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 54000, 720, 736,
922                    798, 858, 0, 480, 489, 495, 525, 0,
923                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
924           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
925         /* 49 - 720x480@120Hz */
926         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 54000, 720, 736,
927                    798, 858, 0, 480, 489, 495, 525, 0,
928                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
929           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
930         /* 50 - 720(1440)x480i@120Hz */
931         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 27000, 720, 739,
932                    801, 858, 0, 480, 488, 494, 525, 0,
933                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
934                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
935           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
936         /* 51 - 720(1440)x480i@120Hz */
937         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 27000, 720, 739,
938                    801, 858, 0, 480, 488, 494, 525, 0,
939                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
940                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
941           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
942         /* 52 - 720x576@200Hz */
943         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 108000, 720, 732,
944                    796, 864, 0, 576, 581, 586, 625, 0,
945                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
946           .vrefresh = 200, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
947         /* 53 - 720x576@200Hz */
948         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 108000, 720, 732,
949                    796, 864, 0, 576, 581, 586, 625, 0,
950                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
951           .vrefresh = 200, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
952         /* 54 - 720(1440)x576i@200Hz */
953         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
954                    795, 864, 0, 576, 580, 586, 625, 0,
955                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
956                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
957           .vrefresh = 200, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
958         /* 55 - 720(1440)x576i@200Hz */
959         { DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 54000, 720, 732,
960                    795, 864, 0, 576, 580, 586, 625, 0,
961                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
962                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
963           .vrefresh = 200, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
964         /* 56 - 720x480@240Hz */
965         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 108000, 720, 736,
966                    798, 858, 0, 480, 489, 495, 525, 0,
967                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
968           .vrefresh = 240, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
969         /* 57 - 720x480@240Hz */
970         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 108000, 720, 736,
971                    798, 858, 0, 480, 489, 495, 525, 0,
972                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
973           .vrefresh = 240, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
974         /* 58 - 720(1440)x480i@240Hz */
975         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 54000, 720, 739,
976                    801, 858, 0, 480, 488, 494, 525, 0,
977                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
978                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
979           .vrefresh = 240, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
980         /* 59 - 720(1440)x480i@240Hz */
981         { DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 54000, 720, 739,
982                    801, 858, 0, 480, 488, 494, 525, 0,
983                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC |
984                         DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_DBLCLK),
985           .vrefresh = 240, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
986         /* 60 - 1280x720@24Hz */
987         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 59400, 1280, 3040,
988                    3080, 3300, 0, 720, 725, 730, 750, 0,
989                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
990           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
991         /* 61 - 1280x720@25Hz */
992         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3700,
993                    3740, 3960, 0, 720, 725, 730, 750, 0,
994                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
995           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
996         /* 62 - 1280x720@30Hz */
997         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3040,
998                    3080, 3300, 0, 720, 725, 730, 750, 0,
999                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1000           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1001         /* 63 - 1920x1080@120Hz */
1002         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2008,
1003                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1004                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1005          .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1006         /* 64 - 1920x1080@100Hz */
1007         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2448,
1008                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1009                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1010          .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1011         /* 65 - 1280x720@24Hz */
1012         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 59400, 1280, 3040,
1013                    3080, 3300, 0, 720, 725, 730, 750, 0,
1014                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1015           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1016         /* 66 - 1280x720@25Hz */
1017         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3700,
1018                    3740, 3960, 0, 720, 725, 730, 750, 0,
1019                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1020           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1021         /* 67 - 1280x720@30Hz */
1022         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 3040,
1023                    3080, 3300, 0, 720, 725, 730, 750, 0,
1024                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1025           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1026         /* 68 - 1280x720@50Hz */
1027         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
1028                    1760, 1980, 0, 720, 725, 730, 750, 0,
1029                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1030           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1031         /* 69 - 1280x720@60Hz */
1032         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
1033                    1430, 1650, 0, 720, 725, 730, 750, 0,
1034                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1035           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1036         /* 70 - 1280x720@100Hz */
1037         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1720,
1038                    1760, 1980, 0, 720, 725, 730, 750, 0,
1039                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1040           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1041         /* 71 - 1280x720@120Hz */
1042         { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 148500, 1280, 1390,
1043                    1430, 1650, 0, 720, 725, 730, 750, 0,
1044                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1045           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1046         /* 72 - 1920x1080@24Hz */
1047         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2558,
1048                    2602, 2750, 0, 1080, 1084, 1089, 1125, 0,
1049                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1050           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1051         /* 73 - 1920x1080@25Hz */
1052         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2448,
1053                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1054                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1055           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1056         /* 74 - 1920x1080@30Hz */
1057         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 74250, 1920, 2008,
1058                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1059                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1060           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1061         /* 75 - 1920x1080@50Hz */
1062         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
1063                    2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
1064                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1065           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1066         /* 76 - 1920x1080@60Hz */
1067         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
1068                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1069                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1070           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1071         /* 77 - 1920x1080@100Hz */
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           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1076         /* 78 - 1920x1080@120Hz */
1077         { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 297000, 1920, 2008,
1078                    2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
1079                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1080           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1081         /* 79 - 1680x720@24Hz */
1082         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 59400, 1680, 3040,
1083                    3080, 3300, 0, 720, 725, 730, 750, 0,
1084                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1085           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1086         /* 80 - 1680x720@25Hz */
1087         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 59400, 1680, 2908,
1088                    2948, 3168, 0, 720, 725, 730, 750, 0,
1089                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1090           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1091         /* 81 - 1680x720@30Hz */
1092         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 59400, 1680, 2380,
1093                    2420, 2640, 0, 720, 725, 730, 750, 0,
1094                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1095           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1096         /* 82 - 1680x720@50Hz */
1097         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 82500, 1680, 1940,
1098                    1980, 2200, 0, 720, 725, 730, 750, 0,
1099                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1100           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1101         /* 83 - 1680x720@60Hz */
1102         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 99000, 1680, 1940,
1103                    1980, 2200, 0, 720, 725, 730, 750, 0,
1104                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1105           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1106         /* 84 - 1680x720@100Hz */
1107         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 165000, 1680, 1740,
1108                    1780, 2000, 0, 720, 725, 730, 825, 0,
1109                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1110           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1111         /* 85 - 1680x720@120Hz */
1112         { DRM_MODE("1680x720", DRM_MODE_TYPE_DRIVER, 198000, 1680, 1740,
1113                    1780, 2000, 0, 720, 725, 730, 825, 0,
1114                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1115           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1116         /* 86 - 2560x1080@24Hz */
1117         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 99000, 2560, 3558,
1118                    3602, 3750, 0, 1080, 1084, 1089, 1100, 0,
1119                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1120           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1121         /* 87 - 2560x1080@25Hz */
1122         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 90000, 2560, 3008,
1123                    3052, 3200, 0, 1080, 1084, 1089, 1125, 0,
1124                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1125           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1126         /* 88 - 2560x1080@30Hz */
1127         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 118800, 2560, 3328,
1128                    3372, 3520, 0, 1080, 1084, 1089, 1125, 0,
1129                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1130           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1131         /* 89 - 2560x1080@50Hz */
1132         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 185625, 2560, 3108,
1133                    3152, 3300, 0, 1080, 1084, 1089, 1125, 0,
1134                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1135           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1136         /* 90 - 2560x1080@60Hz */
1137         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 198000, 2560, 2808,
1138                    2852, 3000, 0, 1080, 1084, 1089, 1100, 0,
1139                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1140           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1141         /* 91 - 2560x1080@100Hz */
1142         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 371250, 2560, 2778,
1143                    2822, 2970, 0, 1080, 1084, 1089, 1250, 0,
1144                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1145           .vrefresh = 100, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1146         /* 92 - 2560x1080@120Hz */
1147         { DRM_MODE("2560x1080", DRM_MODE_TYPE_DRIVER, 495000, 2560, 3108,
1148                    3152, 3300, 0, 1080, 1084, 1089, 1250, 0,
1149                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1150           .vrefresh = 120, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1151         /* 93 - 3840x2160p@24Hz 16:9 */
1152         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 5116,
1153                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1154                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1155           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1156         /* 94 - 3840x2160p@25Hz 16:9 */
1157         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4896,
1158                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1159                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1160           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1161         /* 95 - 3840x2160p@30Hz 16:9 */
1162         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4016,
1163                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1164                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1165           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1166         /* 96 - 3840x2160p@50Hz 16:9 */
1167         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4896,
1168                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1169                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1170           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1171         /* 97 - 3840x2160p@60Hz 16:9 */
1172         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4016,
1173                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1174                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1175           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
1176         /* 98 - 4096x2160p@24Hz 256:135 */
1177         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000, 4096, 5116,
1178                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1179                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1180           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1181         /* 99 - 4096x2160p@25Hz 256:135 */
1182         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000, 4096, 5064,
1183                    5152, 5280, 0, 2160, 2168, 2178, 2250, 0,
1184                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1185           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1186         /* 100 - 4096x2160p@30Hz 256:135 */
1187         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000, 4096, 4184,
1188                    4272, 4400, 0, 2160, 2168, 2178, 2250, 0,
1189                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1190           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1191         /* 101 - 4096x2160p@50Hz 256:135 */
1192         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 594000, 4096, 5064,
1193                    5152, 5280, 0, 2160, 2168, 2178, 2250, 0,
1194                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1195           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1196         /* 102 - 4096x2160p@60Hz 256:135 */
1197         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 594000, 4096, 4184,
1198                    4272, 4400, 0, 2160, 2168, 2178, 2250, 0,
1199                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1200           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135, },
1201         /* 103 - 3840x2160p@24Hz 64:27 */
1202         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 5116,
1203                    5204, 5500, 0, 2160, 2168, 2178, 2250, 0,
1204                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1205           .vrefresh = 24, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1206         /* 104 - 3840x2160p@25Hz 64:27 */
1207         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4896,
1208                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1209                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1210           .vrefresh = 25, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1211         /* 105 - 3840x2160p@30Hz 64:27 */
1212         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000, 3840, 4016,
1213                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1214                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1215           .vrefresh = 30, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1216         /* 106 - 3840x2160p@50Hz 64:27 */
1217         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4896,
1218                    4984, 5280, 0, 2160, 2168, 2178, 2250, 0,
1219                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1220           .vrefresh = 50, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1221         /* 107 - 3840x2160p@60Hz 64:27 */
1222         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 594000, 3840, 4016,
1223                    4104, 4400, 0, 2160, 2168, 2178, 2250, 0,
1224                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1225           .vrefresh = 60, .picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27, },
1226 };
1227
1228 /*
1229  * HDMI 1.4 4k modes. Index using the VIC.
1230  */
1231 static const struct drm_display_mode edid_4k_modes[] = {
1232         /* 0 - dummy, VICs start at 1 */
1233         { },
1234         /* 1 - 3840x2160@30Hz */
1235         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000,
1236                    3840, 4016, 4104, 4400, 0,
1237                    2160, 2168, 2178, 2250, 0,
1238                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1239           .vrefresh = 30, },
1240         /* 2 - 3840x2160@25Hz */
1241         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000,
1242                    3840, 4896, 4984, 5280, 0,
1243                    2160, 2168, 2178, 2250, 0,
1244                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1245           .vrefresh = 25, },
1246         /* 3 - 3840x2160@24Hz */
1247         { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 297000,
1248                    3840, 5116, 5204, 5500, 0,
1249                    2160, 2168, 2178, 2250, 0,
1250                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1251           .vrefresh = 24, },
1252         /* 4 - 4096x2160@24Hz (SMPTE) */
1253         { DRM_MODE("4096x2160", DRM_MODE_TYPE_DRIVER, 297000,
1254                    4096, 5116, 5204, 5500, 0,
1255                    2160, 2168, 2178, 2250, 0,
1256                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
1257           .vrefresh = 24, },
1258 };
1259
1260 /*** DDC fetch and block validation ***/
1261
1262 static const u8 edid_header[] = {
1263         0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
1264 };
1265
1266 /**
1267  * drm_edid_header_is_valid - sanity check the header of the base EDID block
1268  * @raw_edid: pointer to raw base EDID block
1269  *
1270  * Sanity check the header of the base EDID block.
1271  *
1272  * Return: 8 if the header is perfect, down to 0 if it's totally wrong.
1273  */
1274 int drm_edid_header_is_valid(const u8 *raw_edid)
1275 {
1276         int i, score = 0;
1277
1278         for (i = 0; i < sizeof(edid_header); i++)
1279                 if (raw_edid[i] == edid_header[i])
1280                         score++;
1281
1282         return score;
1283 }
1284 EXPORT_SYMBOL(drm_edid_header_is_valid);
1285
1286 static int edid_fixup __read_mostly = 6;
1287 module_param_named(edid_fixup, edid_fixup, int, 0400);
1288 MODULE_PARM_DESC(edid_fixup,
1289                  "Minimum number of valid EDID header bytes (0-8, default 6)");
1290
1291 static void drm_get_displayid(struct drm_connector *connector,
1292                               struct edid *edid);
1293
1294 static int drm_edid_block_checksum(const u8 *raw_edid)
1295 {
1296         int i;
1297         u8 csum = 0;
1298         for (i = 0; i < EDID_LENGTH; i++)
1299                 csum += raw_edid[i];
1300
1301         return csum;
1302 }
1303
1304 static bool drm_edid_is_zero(const u8 *in_edid, int length)
1305 {
1306         if (memchr_inv(in_edid, 0, length))
1307                 return false;
1308
1309         return true;
1310 }
1311
1312 /**
1313  * drm_edid_block_valid - Sanity check the EDID block (base or extension)
1314  * @raw_edid: pointer to raw EDID block
1315  * @block: type of block to validate (0 for base, extension otherwise)
1316  * @print_bad_edid: if true, dump bad EDID blocks to the console
1317  * @edid_corrupt: if true, the header or checksum is invalid
1318  *
1319  * Validate a base or extension EDID block and optionally dump bad blocks to
1320  * the console.
1321  *
1322  * Return: True if the block is valid, false otherwise.
1323  */
1324 bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid,
1325                           bool *edid_corrupt)
1326 {
1327         u8 csum;
1328         struct edid *edid = (struct edid *)raw_edid;
1329
1330         if (WARN_ON(!raw_edid))
1331                 return false;
1332
1333         if (edid_fixup > 8 || edid_fixup < 0)
1334                 edid_fixup = 6;
1335
1336         if (block == 0) {
1337                 int score = drm_edid_header_is_valid(raw_edid);
1338                 if (score == 8) {
1339                         if (edid_corrupt)
1340                                 *edid_corrupt = false;
1341                 } else if (score >= edid_fixup) {
1342                         /* Displayport Link CTS Core 1.2 rev1.1 test 4.2.2.6
1343                          * The corrupt flag needs to be set here otherwise, the
1344                          * fix-up code here will correct the problem, the
1345                          * checksum is correct and the test fails
1346                          */
1347                         if (edid_corrupt)
1348                                 *edid_corrupt = true;
1349                         DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
1350                         memcpy(raw_edid, edid_header, sizeof(edid_header));
1351                 } else {
1352                         if (edid_corrupt)
1353                                 *edid_corrupt = true;
1354                         goto bad;
1355                 }
1356         }
1357
1358         csum = drm_edid_block_checksum(raw_edid);
1359         if (csum) {
1360                 if (edid_corrupt)
1361                         *edid_corrupt = true;
1362
1363                 /* allow CEA to slide through, switches mangle this */
1364                 if (raw_edid[0] == CEA_EXT) {
1365                         DRM_DEBUG("EDID checksum is invalid, remainder is %d\n", csum);
1366                         DRM_DEBUG("Assuming a KVM switch modified the CEA block but left the original checksum\n");
1367                 } else {
1368                         if (print_bad_edid)
1369                                 DRM_NOTE("EDID checksum is invalid, remainder is %d\n", csum);
1370
1371                         goto bad;
1372                 }
1373         }
1374
1375         /* per-block-type checks */
1376         switch (raw_edid[0]) {
1377         case 0: /* base */
1378                 if (edid->version != 1) {
1379                         DRM_NOTE("EDID has major version %d, instead of 1\n", edid->version);
1380                         goto bad;
1381                 }
1382
1383                 if (edid->revision > 4)
1384                         DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
1385                 break;
1386
1387         default:
1388                 break;
1389         }
1390
1391         return true;
1392
1393 bad:
1394         if (print_bad_edid) {
1395                 if (drm_edid_is_zero(raw_edid, EDID_LENGTH)) {
1396                         pr_notice("EDID block is all zeroes\n");
1397                 } else {
1398                         pr_notice("Raw EDID:\n");
1399                         print_hex_dump(KERN_NOTICE,
1400                                        " \t", DUMP_PREFIX_NONE, 16, 1,
1401                                        raw_edid, EDID_LENGTH, false);
1402                 }
1403         }
1404         return false;
1405 }
1406 EXPORT_SYMBOL(drm_edid_block_valid);
1407
1408 /**
1409  * drm_edid_is_valid - sanity check EDID data
1410  * @edid: EDID data
1411  *
1412  * Sanity-check an entire EDID record (including extensions)
1413  *
1414  * Return: True if the EDID data is valid, false otherwise.
1415  */
1416 bool drm_edid_is_valid(struct edid *edid)
1417 {
1418         int i;
1419         u8 *raw = (u8 *)edid;
1420
1421         if (!edid)
1422                 return false;
1423
1424         for (i = 0; i <= edid->extensions; i++)
1425                 if (!drm_edid_block_valid(raw + i * EDID_LENGTH, i, true, NULL))
1426                         return false;
1427
1428         return true;
1429 }
1430 EXPORT_SYMBOL(drm_edid_is_valid);
1431
1432 #define DDC_SEGMENT_ADDR 0x30
1433 /**
1434  * drm_do_probe_ddc_edid() - get EDID information via I2C
1435  * @data: I2C device adapter
1436  * @buf: EDID data buffer to be filled
1437  * @block: 128 byte EDID block to start fetching from
1438  * @len: EDID data buffer length to fetch
1439  *
1440  * Try to fetch EDID information by calling I2C driver functions.
1441  *
1442  * Return: 0 on success or -1 on failure.
1443  */
1444 static int
1445 drm_do_probe_ddc_edid(void *data, u8 *buf, unsigned int block, size_t len)
1446 {
1447         struct i2c_adapter *adapter = data;
1448         unsigned char start = block * EDID_LENGTH;
1449         unsigned char segment = block >> 1;
1450         unsigned char xfers = segment ? 3 : 2;
1451         int ret, retries = 5;
1452
1453         /*
1454          * The core I2C driver will automatically retry the transfer if the
1455          * adapter reports EAGAIN. However, we find that bit-banging transfers
1456          * are susceptible to errors under a heavily loaded machine and
1457          * generate spurious NAKs and timeouts. Retrying the transfer
1458          * of the individual block a few times seems to overcome this.
1459          */
1460         do {
1461                 struct i2c_msg msgs[] = {
1462                         {
1463                                 .addr   = DDC_SEGMENT_ADDR,
1464                                 .flags  = 0,
1465                                 .len    = 1,
1466                                 .buf    = &segment,
1467                         }, {
1468                                 .addr   = DDC_ADDR,
1469                                 .flags  = 0,
1470                                 .len    = 1,
1471                                 .buf    = &start,
1472                         }, {
1473                                 .addr   = DDC_ADDR,
1474                                 .flags  = I2C_M_RD,
1475                                 .len    = len,
1476                                 .buf    = buf,
1477                         }
1478                 };
1479
1480                 /*
1481                  * Avoid sending the segment addr to not upset non-compliant
1482                  * DDC monitors.
1483                  */
1484                 ret = i2c_transfer(adapter, &msgs[3 - xfers], xfers);
1485
1486                 if (ret == -ENXIO) {
1487                         DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
1488                                         adapter->name);
1489                         break;
1490                 }
1491         } while (ret != xfers && --retries);
1492
1493         return ret == xfers ? 0 : -1;
1494 }
1495
1496 static void connector_bad_edid(struct drm_connector *connector,
1497                                u8 *edid, int num_blocks)
1498 {
1499         int i;
1500
1501         if (connector->bad_edid_counter++ && !(drm_debug & DRM_UT_KMS))
1502                 return;
1503
1504         dev_warn(connector->dev->dev,
1505                  "%s: EDID is invalid:\n",
1506                  connector->name);
1507         for (i = 0; i < num_blocks; i++) {
1508                 u8 *block = edid + i * EDID_LENGTH;
1509                 char prefix[20];
1510
1511                 if (drm_edid_is_zero(block, EDID_LENGTH))
1512                         sprintf(prefix, "\t[%02x] ZERO ", i);
1513                 else if (!drm_edid_block_valid(block, i, false, NULL))
1514                         sprintf(prefix, "\t[%02x] BAD  ", i);
1515                 else
1516                         sprintf(prefix, "\t[%02x] GOOD ", i);
1517
1518                 print_hex_dump(KERN_WARNING,
1519                                prefix, DUMP_PREFIX_NONE, 16, 1,
1520                                block, EDID_LENGTH, false);
1521         }
1522 }
1523
1524 /**
1525  * drm_do_get_edid - get EDID data using a custom EDID block read function
1526  * @connector: connector we're probing
1527  * @get_edid_block: EDID block read function
1528  * @data: private data passed to the block read function
1529  *
1530  * When the I2C adapter connected to the DDC bus is hidden behind a device that
1531  * exposes a different interface to read EDID blocks this function can be used
1532  * to get EDID data using a custom block read function.
1533  *
1534  * As in the general case the DDC bus is accessible by the kernel at the I2C
1535  * level, drivers must make all reasonable efforts to expose it as an I2C
1536  * adapter and use drm_get_edid() instead of abusing this function.
1537  *
1538  * The EDID may be overridden using debugfs override_edid or firmare EDID
1539  * (drm_load_edid_firmware() and drm.edid_firmware parameter), in this priority
1540  * order. Having either of them bypasses actual EDID reads.
1541  *
1542  * Return: Pointer to valid EDID or NULL if we couldn't find any.
1543  */
1544 struct edid *drm_do_get_edid(struct drm_connector *connector,
1545         int (*get_edid_block)(void *data, u8 *buf, unsigned int block,
1546                               size_t len),
1547         void *data)
1548 {
1549         int i, j = 0, valid_extensions = 0;
1550         u8 *edid, *new;
1551         struct edid *override = NULL;
1552
1553         if (connector->override_edid)
1554                 override = drm_edid_duplicate((const struct edid *)
1555                                               connector->edid_blob_ptr->data);
1556
1557         if (!override)
1558                 override = drm_load_edid_firmware(connector);
1559
1560         if (!IS_ERR_OR_NULL(override))
1561                 return override;
1562
1563         if ((edid = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
1564                 return NULL;
1565
1566         /* base block fetch */
1567         for (i = 0; i < 4; i++) {
1568                 if (get_edid_block(data, edid, 0, EDID_LENGTH))
1569                         goto out;
1570                 if (drm_edid_block_valid(edid, 0, false,
1571                                          &connector->edid_corrupt))
1572                         break;
1573                 if (i == 0 && drm_edid_is_zero(edid, EDID_LENGTH)) {
1574                         connector->null_edid_counter++;
1575                         goto carp;
1576                 }
1577         }
1578         if (i == 4)
1579                 goto carp;
1580
1581         /* if there's no extensions, we're done */
1582         valid_extensions = edid[0x7e];
1583         if (valid_extensions == 0)
1584                 return (struct edid *)edid;
1585
1586         new = krealloc(edid, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
1587         if (!new)
1588                 goto out;
1589         edid = new;
1590
1591         for (j = 1; j <= edid[0x7e]; j++) {
1592                 u8 *block = edid + j * EDID_LENGTH;
1593
1594                 for (i = 0; i < 4; i++) {
1595                         if (get_edid_block(data, block, j, EDID_LENGTH))
1596                                 goto out;
1597                         if (drm_edid_block_valid(block, j, false, NULL))
1598                                 break;
1599                 }
1600
1601                 if (i == 4)
1602                         valid_extensions--;
1603         }
1604
1605         if (valid_extensions != edid[0x7e]) {
1606                 u8 *base;
1607
1608                 connector_bad_edid(connector, edid, edid[0x7e] + 1);
1609
1610                 edid[EDID_LENGTH-1] += edid[0x7e] - valid_extensions;
1611                 edid[0x7e] = valid_extensions;
1612
1613                 new = kmalloc((valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
1614                 if (!new)
1615                         goto out;
1616
1617                 base = new;
1618                 for (i = 0; i <= edid[0x7e]; i++) {
1619                         u8 *block = edid + i * EDID_LENGTH;
1620
1621                         if (!drm_edid_block_valid(block, i, false, NULL))
1622                                 continue;
1623
1624                         memcpy(base, block, EDID_LENGTH);
1625                         base += EDID_LENGTH;
1626                 }
1627
1628                 kfree(edid);
1629                 edid = new;
1630         }
1631
1632         return (struct edid *)edid;
1633
1634 carp:
1635         connector_bad_edid(connector, edid, 1);
1636 out:
1637         kfree(edid);
1638         return NULL;
1639 }
1640 EXPORT_SYMBOL_GPL(drm_do_get_edid);
1641
1642 /**
1643  * drm_probe_ddc() - probe DDC presence
1644  * @adapter: I2C adapter to probe
1645  *
1646  * Return: True on success, false on failure.
1647  */
1648 bool
1649 drm_probe_ddc(struct i2c_adapter *adapter)
1650 {
1651         unsigned char out;
1652
1653         return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
1654 }
1655 EXPORT_SYMBOL(drm_probe_ddc);
1656
1657 /**
1658  * drm_get_edid - get EDID data, if available
1659  * @connector: connector we're probing
1660  * @adapter: I2C adapter to use for DDC
1661  *
1662  * Poke the given I2C channel to grab EDID data if possible.  If found,
1663  * attach it to the connector.
1664  *
1665  * Return: Pointer to valid EDID or NULL if we couldn't find any.
1666  */
1667 struct edid *drm_get_edid(struct drm_connector *connector,
1668                           struct i2c_adapter *adapter)
1669 {
1670         struct edid *edid;
1671
1672         if (connector->force == DRM_FORCE_OFF)
1673                 return NULL;
1674
1675         if (connector->force == DRM_FORCE_UNSPECIFIED && !drm_probe_ddc(adapter))
1676                 return NULL;
1677
1678         edid = drm_do_get_edid(connector, drm_do_probe_ddc_edid, adapter);
1679         if (edid)
1680                 drm_get_displayid(connector, edid);
1681         return edid;
1682 }
1683 EXPORT_SYMBOL(drm_get_edid);
1684
1685 /**
1686  * drm_get_edid_switcheroo - get EDID data for a vga_switcheroo output
1687  * @connector: connector we're probing
1688  * @adapter: I2C adapter to use for DDC
1689  *
1690  * Wrapper around drm_get_edid() for laptops with dual GPUs using one set of
1691  * outputs. The wrapper adds the requisite vga_switcheroo calls to temporarily
1692  * switch DDC to the GPU which is retrieving EDID.
1693  *
1694  * Return: Pointer to valid EDID or %NULL if we couldn't find any.
1695  */
1696 struct edid *drm_get_edid_switcheroo(struct drm_connector *connector,
1697                                      struct i2c_adapter *adapter)
1698 {
1699         struct pci_dev *pdev = connector->dev->pdev;
1700         struct edid *edid;
1701
1702         vga_switcheroo_lock_ddc(pdev);
1703         edid = drm_get_edid(connector, adapter);
1704         vga_switcheroo_unlock_ddc(pdev);
1705
1706         return edid;
1707 }
1708 EXPORT_SYMBOL(drm_get_edid_switcheroo);
1709
1710 /**
1711  * drm_edid_duplicate - duplicate an EDID and the extensions
1712  * @edid: EDID to duplicate
1713  *
1714  * Return: Pointer to duplicated EDID or NULL on allocation failure.
1715  */
1716 struct edid *drm_edid_duplicate(const struct edid *edid)
1717 {
1718         return kmemdup(edid, (edid->extensions + 1) * EDID_LENGTH, GFP_KERNEL);
1719 }
1720 EXPORT_SYMBOL(drm_edid_duplicate);
1721
1722 /*** EDID parsing ***/
1723
1724 /**
1725  * edid_vendor - match a string against EDID's obfuscated vendor field
1726  * @edid: EDID to match
1727  * @vendor: vendor string
1728  *
1729  * Returns true if @vendor is in @edid, false otherwise
1730  */
1731 static bool edid_vendor(struct edid *edid, const char *vendor)
1732 {
1733         char edid_vendor[3];
1734
1735         edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
1736         edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
1737                           ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
1738         edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
1739
1740         return !strncmp(edid_vendor, vendor, 3);
1741 }
1742
1743 /**
1744  * edid_get_quirks - return quirk flags for a given EDID
1745  * @edid: EDID to process
1746  *
1747  * This tells subsequent routines what fixes they need to apply.
1748  */
1749 static u32 edid_get_quirks(struct edid *edid)
1750 {
1751         const struct edid_quirk *quirk;
1752         int i;
1753
1754         for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
1755                 quirk = &edid_quirk_list[i];
1756
1757                 if (edid_vendor(edid, quirk->vendor) &&
1758                     (EDID_PRODUCT_ID(edid) == quirk->product_id))
1759                         return quirk->quirks;
1760         }
1761
1762         return 0;
1763 }
1764
1765 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
1766 #define MODE_REFRESH_DIFF(c,t) (abs((c) - (t)))
1767
1768 /**
1769  * edid_fixup_preferred - set preferred modes based on quirk list
1770  * @connector: has mode list to fix up
1771  * @quirks: quirks list
1772  *
1773  * Walk the mode list for @connector, clearing the preferred status
1774  * on existing modes and setting it anew for the right mode ala @quirks.
1775  */
1776 static void edid_fixup_preferred(struct drm_connector *connector,
1777                                  u32 quirks)
1778 {
1779         struct drm_display_mode *t, *cur_mode, *preferred_mode;
1780         int target_refresh = 0;
1781         int cur_vrefresh, preferred_vrefresh;
1782
1783         if (list_empty(&connector->probed_modes))
1784                 return;
1785
1786         if (quirks & EDID_QUIRK_PREFER_LARGE_60)
1787                 target_refresh = 60;
1788         if (quirks & EDID_QUIRK_PREFER_LARGE_75)
1789                 target_refresh = 75;
1790
1791         preferred_mode = list_first_entry(&connector->probed_modes,
1792                                           struct drm_display_mode, head);
1793
1794         list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
1795                 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
1796
1797                 if (cur_mode == preferred_mode)
1798                         continue;
1799
1800                 /* Largest mode is preferred */
1801                 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
1802                         preferred_mode = cur_mode;
1803
1804                 cur_vrefresh = cur_mode->vrefresh ?
1805                         cur_mode->vrefresh : drm_mode_vrefresh(cur_mode);
1806                 preferred_vrefresh = preferred_mode->vrefresh ?
1807                         preferred_mode->vrefresh : drm_mode_vrefresh(preferred_mode);
1808                 /* At a given size, try to get closest to target refresh */
1809                 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
1810                     MODE_REFRESH_DIFF(cur_vrefresh, target_refresh) <
1811                     MODE_REFRESH_DIFF(preferred_vrefresh, target_refresh)) {
1812                         preferred_mode = cur_mode;
1813                 }
1814         }
1815
1816         preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
1817 }
1818
1819 static bool
1820 mode_is_rb(const struct drm_display_mode *mode)
1821 {
1822         return (mode->htotal - mode->hdisplay == 160) &&
1823                (mode->hsync_end - mode->hdisplay == 80) &&
1824                (mode->hsync_end - mode->hsync_start == 32) &&
1825                (mode->vsync_start - mode->vdisplay == 3);
1826 }
1827
1828 /*
1829  * drm_mode_find_dmt - Create a copy of a mode if present in DMT
1830  * @dev: Device to duplicate against
1831  * @hsize: Mode width
1832  * @vsize: Mode height
1833  * @fresh: Mode refresh rate
1834  * @rb: Mode reduced-blanking-ness
1835  *
1836  * Walk the DMT mode list looking for a match for the given parameters.
1837  *
1838  * Return: A newly allocated copy of the mode, or NULL if not found.
1839  */
1840 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
1841                                            int hsize, int vsize, int fresh,
1842                                            bool rb)
1843 {
1844         int i;
1845
1846         for (i = 0; i < ARRAY_SIZE(drm_dmt_modes); i++) {
1847                 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
1848                 if (hsize != ptr->hdisplay)
1849                         continue;
1850                 if (vsize != ptr->vdisplay)
1851                         continue;
1852                 if (fresh != drm_mode_vrefresh(ptr))
1853                         continue;
1854                 if (rb != mode_is_rb(ptr))
1855                         continue;
1856
1857                 return drm_mode_duplicate(dev, ptr);
1858         }
1859
1860         return NULL;
1861 }
1862 EXPORT_SYMBOL(drm_mode_find_dmt);
1863
1864 typedef void detailed_cb(struct detailed_timing *timing, void *closure);
1865
1866 static void
1867 cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
1868 {
1869         int i, n = 0;
1870         u8 d = ext[0x02];
1871         u8 *det_base = ext + d;
1872
1873         n = (127 - d) / 18;
1874         for (i = 0; i < n; i++)
1875                 cb((struct detailed_timing *)(det_base + 18 * i), closure);
1876 }
1877
1878 static void
1879 vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
1880 {
1881         unsigned int i, n = min((int)ext[0x02], 6);
1882         u8 *det_base = ext + 5;
1883
1884         if (ext[0x01] != 1)
1885                 return; /* unknown version */
1886
1887         for (i = 0; i < n; i++)
1888                 cb((struct detailed_timing *)(det_base + 18 * i), closure);
1889 }
1890
1891 static void
1892 drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
1893 {
1894         int i;
1895         struct edid *edid = (struct edid *)raw_edid;
1896
1897         if (edid == NULL)
1898                 return;
1899
1900         for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
1901                 cb(&(edid->detailed_timings[i]), closure);
1902
1903         for (i = 1; i <= raw_edid[0x7e]; i++) {
1904                 u8 *ext = raw_edid + (i * EDID_LENGTH);
1905                 switch (*ext) {
1906                 case CEA_EXT:
1907                         cea_for_each_detailed_block(ext, cb, closure);
1908                         break;
1909                 case VTB_EXT:
1910                         vtb_for_each_detailed_block(ext, cb, closure);
1911                         break;
1912                 default:
1913                         break;
1914                 }
1915         }
1916 }
1917
1918 static void
1919 is_rb(struct detailed_timing *t, void *data)
1920 {
1921         u8 *r = (u8 *)t;
1922         if (r[3] == EDID_DETAIL_MONITOR_RANGE)
1923                 if (r[15] & 0x10)
1924                         *(bool *)data = true;
1925 }
1926
1927 /* EDID 1.4 defines this explicitly.  For EDID 1.3, we guess, badly. */
1928 static bool
1929 drm_monitor_supports_rb(struct edid *edid)
1930 {
1931         if (edid->revision >= 4) {
1932                 bool ret = false;
1933                 drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
1934                 return ret;
1935         }
1936
1937         return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
1938 }
1939
1940 static void
1941 find_gtf2(struct detailed_timing *t, void *data)
1942 {
1943         u8 *r = (u8 *)t;
1944         if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
1945                 *(u8 **)data = r;
1946 }
1947
1948 /* Secondary GTF curve kicks in above some break frequency */
1949 static int
1950 drm_gtf2_hbreak(struct edid *edid)
1951 {
1952         u8 *r = NULL;
1953         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
1954         return r ? (r[12] * 2) : 0;
1955 }
1956
1957 static int
1958 drm_gtf2_2c(struct edid *edid)
1959 {
1960         u8 *r = NULL;
1961         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
1962         return r ? r[13] : 0;
1963 }
1964
1965 static int
1966 drm_gtf2_m(struct edid *edid)
1967 {
1968         u8 *r = NULL;
1969         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
1970         return r ? (r[15] << 8) + r[14] : 0;
1971 }
1972
1973 static int
1974 drm_gtf2_k(struct edid *edid)
1975 {
1976         u8 *r = NULL;
1977         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
1978         return r ? r[16] : 0;
1979 }
1980
1981 static int
1982 drm_gtf2_2j(struct edid *edid)
1983 {
1984         u8 *r = NULL;
1985         drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
1986         return r ? r[17] : 0;
1987 }
1988
1989 /**
1990  * standard_timing_level - get std. timing level(CVT/GTF/DMT)
1991  * @edid: EDID block to scan
1992  */
1993 static int standard_timing_level(struct edid *edid)
1994 {
1995         if (edid->revision >= 2) {
1996                 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
1997                         return LEVEL_CVT;
1998                 if (drm_gtf2_hbreak(edid))
1999                         return LEVEL_GTF2;
2000                 return LEVEL_GTF;
2001         }
2002         return LEVEL_DMT;
2003 }
2004
2005 /*
2006  * 0 is reserved.  The spec says 0x01 fill for unused timings.  Some old
2007  * monitors fill with ascii space (0x20) instead.
2008  */
2009 static int
2010 bad_std_timing(u8 a, u8 b)
2011 {
2012         return (a == 0x00 && b == 0x00) ||
2013                (a == 0x01 && b == 0x01) ||
2014                (a == 0x20 && b == 0x20);
2015 }
2016
2017 /**
2018  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
2019  * @connector: connector of for the EDID block
2020  * @edid: EDID block to scan
2021  * @t: standard timing params
2022  *
2023  * Take the standard timing params (in this case width, aspect, and refresh)
2024  * and convert them into a real mode using CVT/GTF/DMT.
2025  */
2026 static struct drm_display_mode *
2027 drm_mode_std(struct drm_connector *connector, struct edid *edid,
2028              struct std_timing *t)
2029 {
2030         struct drm_device *dev = connector->dev;
2031         struct drm_display_mode *m, *mode = NULL;
2032         int hsize, vsize;
2033         int vrefresh_rate;
2034         unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
2035                 >> EDID_TIMING_ASPECT_SHIFT;
2036         unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
2037                 >> EDID_TIMING_VFREQ_SHIFT;
2038         int timing_level = standard_timing_level(edid);
2039
2040         if (bad_std_timing(t->hsize, t->vfreq_aspect))
2041                 return NULL;
2042
2043         /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
2044         hsize = t->hsize * 8 + 248;
2045         /* vrefresh_rate = vfreq + 60 */
2046         vrefresh_rate = vfreq + 60;
2047         /* the vdisplay is calculated based on the aspect ratio */
2048         if (aspect_ratio == 0) {
2049                 if (edid->revision < 3)
2050                         vsize = hsize;
2051                 else
2052                         vsize = (hsize * 10) / 16;
2053         } else if (aspect_ratio == 1)
2054                 vsize = (hsize * 3) / 4;
2055         else if (aspect_ratio == 2)
2056                 vsize = (hsize * 4) / 5;
2057         else
2058                 vsize = (hsize * 9) / 16;
2059
2060         /* HDTV hack, part 1 */
2061         if (vrefresh_rate == 60 &&
2062             ((hsize == 1360 && vsize == 765) ||
2063              (hsize == 1368 && vsize == 769))) {
2064                 hsize = 1366;
2065                 vsize = 768;
2066         }
2067
2068         /*
2069          * If this connector already has a mode for this size and refresh
2070          * rate (because it came from detailed or CVT info), use that
2071          * instead.  This way we don't have to guess at interlace or
2072          * reduced blanking.
2073          */
2074         list_for_each_entry(m, &connector->probed_modes, head)
2075                 if (m->hdisplay == hsize && m->vdisplay == vsize &&
2076                     drm_mode_vrefresh(m) == vrefresh_rate)
2077                         return NULL;
2078
2079         /* HDTV hack, part 2 */
2080         if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
2081                 mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
2082                                     false);
2083                 mode->hdisplay = 1366;
2084                 mode->hsync_start = mode->hsync_start - 1;
2085                 mode->hsync_end = mode->hsync_end - 1;
2086                 return mode;
2087         }
2088
2089         /* check whether it can be found in default mode table */
2090         if (drm_monitor_supports_rb(edid)) {
2091                 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
2092                                          true);
2093                 if (mode)
2094                         return mode;
2095         }
2096         mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
2097         if (mode)
2098                 return mode;
2099
2100         /* okay, generate it */
2101         switch (timing_level) {
2102         case LEVEL_DMT:
2103                 break;
2104         case LEVEL_GTF:
2105                 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
2106                 break;
2107         case LEVEL_GTF2:
2108                 /*
2109                  * This is potentially wrong if there's ever a monitor with
2110                  * more than one ranges section, each claiming a different
2111                  * secondary GTF curve.  Please don't do that.
2112                  */
2113                 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
2114                 if (!mode)
2115                         return NULL;
2116                 if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
2117                         drm_mode_destroy(dev, mode);
2118                         mode = drm_gtf_mode_complex(dev, hsize, vsize,
2119                                                     vrefresh_rate, 0, 0,
2120                                                     drm_gtf2_m(edid),
2121                                                     drm_gtf2_2c(edid),
2122                                                     drm_gtf2_k(edid),
2123                                                     drm_gtf2_2j(edid));
2124                 }
2125                 break;
2126         case LEVEL_CVT:
2127                 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
2128                                     false);
2129                 break;
2130         }
2131         return mode;
2132 }
2133
2134 /*
2135  * EDID is delightfully ambiguous about how interlaced modes are to be
2136  * encoded.  Our internal representation is of frame height, but some
2137  * HDTV detailed timings are encoded as field height.
2138  *
2139  * The format list here is from CEA, in frame size.  Technically we
2140  * should be checking refresh rate too.  Whatever.
2141  */
2142 static void
2143 drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
2144                             struct detailed_pixel_timing *pt)
2145 {
2146         int i;
2147         static const struct {
2148                 int w, h;
2149         } cea_interlaced[] = {
2150                 { 1920, 1080 },
2151                 {  720,  480 },
2152                 { 1440,  480 },
2153                 { 2880,  480 },
2154                 {  720,  576 },
2155                 { 1440,  576 },
2156                 { 2880,  576 },
2157         };
2158
2159         if (!(pt->misc & DRM_EDID_PT_INTERLACED))
2160                 return;
2161
2162         for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
2163                 if ((mode->hdisplay == cea_interlaced[i].w) &&
2164                     (mode->vdisplay == cea_interlaced[i].h / 2)) {
2165                         mode->vdisplay *= 2;
2166                         mode->vsync_start *= 2;
2167                         mode->vsync_end *= 2;
2168                         mode->vtotal *= 2;
2169                         mode->vtotal |= 1;
2170                 }
2171         }
2172
2173         mode->flags |= DRM_MODE_FLAG_INTERLACE;
2174 }
2175
2176 /**
2177  * drm_mode_detailed - create a new mode from an EDID detailed timing section
2178  * @dev: DRM device (needed to create new mode)
2179  * @edid: EDID block
2180  * @timing: EDID detailed timing info
2181  * @quirks: quirks to apply
2182  *
2183  * An EDID detailed timing block contains enough info for us to create and
2184  * return a new struct drm_display_mode.
2185  */
2186 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
2187                                                   struct edid *edid,
2188                                                   struct detailed_timing *timing,
2189                                                   u32 quirks)
2190 {
2191         struct drm_display_mode *mode;
2192         struct detailed_pixel_timing *pt = &timing->data.pixel_data;
2193         unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
2194         unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
2195         unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
2196         unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
2197         unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
2198         unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
2199         unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) << 2 | pt->vsync_offset_pulse_width_lo >> 4;
2200         unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
2201
2202         /* ignore tiny modes */
2203         if (hactive < 64 || vactive < 64)
2204                 return NULL;
2205
2206         if (pt->misc & DRM_EDID_PT_STEREO) {
2207                 DRM_DEBUG_KMS("stereo mode not supported\n");
2208                 return NULL;
2209         }
2210         if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
2211                 DRM_DEBUG_KMS("composite sync not supported\n");
2212         }
2213
2214         /* it is incorrect if hsync/vsync width is zero */
2215         if (!hsync_pulse_width || !vsync_pulse_width) {
2216                 DRM_DEBUG_KMS("Incorrect Detailed timing. "
2217                                 "Wrong Hsync/Vsync pulse width\n");
2218                 return NULL;
2219         }
2220
2221         if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
2222                 mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
2223                 if (!mode)
2224                         return NULL;
2225
2226                 goto set_size;
2227         }
2228
2229         mode = drm_mode_create(dev);
2230         if (!mode)
2231                 return NULL;
2232
2233         if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
2234                 timing->pixel_clock = cpu_to_le16(1088);
2235
2236         mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
2237
2238         mode->hdisplay = hactive;
2239         mode->hsync_start = mode->hdisplay + hsync_offset;
2240         mode->hsync_end = mode->hsync_start + hsync_pulse_width;
2241         mode->htotal = mode->hdisplay + hblank;
2242
2243         mode->vdisplay = vactive;
2244         mode->vsync_start = mode->vdisplay + vsync_offset;
2245         mode->vsync_end = mode->vsync_start + vsync_pulse_width;
2246         mode->vtotal = mode->vdisplay + vblank;
2247
2248         /* Some EDIDs have bogus h/vtotal values */
2249         if (mode->hsync_end > mode->htotal)
2250                 mode->htotal = mode->hsync_end + 1;
2251         if (mode->vsync_end > mode->vtotal)
2252                 mode->vtotal = mode->vsync_end + 1;
2253
2254         drm_mode_do_interlace_quirk(mode, pt);
2255
2256         if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
2257                 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
2258         }
2259
2260         mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
2261                 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
2262         mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
2263                 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
2264
2265 set_size:
2266         mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
2267         mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
2268
2269         if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
2270                 mode->width_mm *= 10;
2271                 mode->height_mm *= 10;
2272         }
2273
2274         if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
2275                 mode->width_mm = edid->width_cm * 10;
2276                 mode->height_mm = edid->height_cm * 10;
2277         }
2278
2279         mode->type = DRM_MODE_TYPE_DRIVER;
2280         mode->vrefresh = drm_mode_vrefresh(mode);
2281         drm_mode_set_name(mode);
2282
2283         return mode;
2284 }
2285
2286 static bool
2287 mode_in_hsync_range(const struct drm_display_mode *mode,
2288                     struct edid *edid, u8 *t)
2289 {
2290         int hsync, hmin, hmax;
2291
2292         hmin = t[7];
2293         if (edid->revision >= 4)
2294             hmin += ((t[4] & 0x04) ? 255 : 0);
2295         hmax = t[8];
2296         if (edid->revision >= 4)
2297             hmax += ((t[4] & 0x08) ? 255 : 0);
2298         hsync = drm_mode_hsync(mode);
2299
2300         return (hsync <= hmax && hsync >= hmin);
2301 }
2302
2303 static bool
2304 mode_in_vsync_range(const struct drm_display_mode *mode,
2305                     struct edid *edid, u8 *t)
2306 {
2307         int vsync, vmin, vmax;
2308
2309         vmin = t[5];
2310         if (edid->revision >= 4)
2311             vmin += ((t[4] & 0x01) ? 255 : 0);
2312         vmax = t[6];
2313         if (edid->revision >= 4)
2314             vmax += ((t[4] & 0x02) ? 255 : 0);
2315         vsync = drm_mode_vrefresh(mode);
2316
2317         return (vsync <= vmax && vsync >= vmin);
2318 }
2319
2320 static u32
2321 range_pixel_clock(struct edid *edid, u8 *t)
2322 {
2323         /* unspecified */
2324         if (t[9] == 0 || t[9] == 255)
2325                 return 0;
2326
2327         /* 1.4 with CVT support gives us real precision, yay */
2328         if (edid->revision >= 4 && t[10] == 0x04)
2329                 return (t[9] * 10000) - ((t[12] >> 2) * 250);
2330
2331         /* 1.3 is pathetic, so fuzz up a bit */
2332         return t[9] * 10000 + 5001;
2333 }
2334
2335 static bool
2336 mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
2337               struct detailed_timing *timing)
2338 {
2339         u32 max_clock;
2340         u8 *t = (u8 *)timing;
2341
2342         if (!mode_in_hsync_range(mode, edid, t))
2343                 return false;
2344
2345         if (!mode_in_vsync_range(mode, edid, t))
2346                 return false;
2347
2348         if ((max_clock = range_pixel_clock(edid, t)))
2349                 if (mode->clock > max_clock)
2350                         return false;
2351
2352         /* 1.4 max horizontal check */
2353         if (edid->revision >= 4 && t[10] == 0x04)
2354                 if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
2355                         return false;
2356
2357         if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
2358                 return false;
2359
2360         return true;
2361 }
2362
2363 static bool valid_inferred_mode(const struct drm_connector *connector,
2364                                 const struct drm_display_mode *mode)
2365 {
2366         const struct drm_display_mode *m;
2367         bool ok = false;
2368
2369         list_for_each_entry(m, &connector->probed_modes, head) {
2370                 if (mode->hdisplay == m->hdisplay &&
2371                     mode->vdisplay == m->vdisplay &&
2372                     drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
2373                         return false; /* duplicated */
2374                 if (mode->hdisplay <= m->hdisplay &&
2375                     mode->vdisplay <= m->vdisplay)
2376                         ok = true;
2377         }
2378         return ok;
2379 }
2380
2381 static int
2382 drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
2383                         struct detailed_timing *timing)
2384 {
2385         int i, modes = 0;
2386         struct drm_display_mode *newmode;
2387         struct drm_device *dev = connector->dev;
2388
2389         for (i = 0; i < ARRAY_SIZE(drm_dmt_modes); i++) {
2390                 if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
2391                     valid_inferred_mode(connector, drm_dmt_modes + i)) {
2392                         newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
2393                         if (newmode) {
2394                                 drm_mode_probed_add(connector, newmode);
2395                                 modes++;
2396                         }
2397                 }
2398         }
2399
2400         return modes;
2401 }
2402
2403 /* fix up 1366x768 mode from 1368x768;
2404  * GFT/CVT can't express 1366 width which isn't dividable by 8
2405  */
2406 void drm_mode_fixup_1366x768(struct drm_display_mode *mode)
2407 {
2408         if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
2409                 mode->hdisplay = 1366;
2410                 mode->hsync_start--;
2411                 mode->hsync_end--;
2412                 drm_mode_set_name(mode);
2413         }
2414 }
2415
2416 static int
2417 drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
2418                         struct detailed_timing *timing)
2419 {
2420         int i, modes = 0;
2421         struct drm_display_mode *newmode;
2422         struct drm_device *dev = connector->dev;
2423
2424         for (i = 0; i < ARRAY_SIZE(extra_modes); i++) {
2425                 const struct minimode *m = &extra_modes[i];
2426                 newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
2427                 if (!newmode)
2428                         return modes;
2429
2430                 drm_mode_fixup_1366x768(newmode);
2431                 if (!mode_in_range(newmode, edid, timing) ||
2432                     !valid_inferred_mode(connector, newmode)) {
2433                         drm_mode_destroy(dev, newmode);
2434                         continue;
2435                 }
2436
2437                 drm_mode_probed_add(connector, newmode);
2438                 modes++;
2439         }
2440
2441         return modes;
2442 }
2443
2444 static int
2445 drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
2446                         struct detailed_timing *timing)
2447 {
2448         int i, modes = 0;
2449         struct drm_display_mode *newmode;
2450         struct drm_device *dev = connector->dev;
2451         bool rb = drm_monitor_supports_rb(edid);
2452
2453         for (i = 0; i < ARRAY_SIZE(extra_modes); i++) {
2454                 const struct minimode *m = &extra_modes[i];
2455                 newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
2456                 if (!newmode)
2457                         return modes;
2458
2459                 drm_mode_fixup_1366x768(newmode);
2460                 if (!mode_in_range(newmode, edid, timing) ||
2461                     !valid_inferred_mode(connector, newmode)) {
2462                         drm_mode_destroy(dev, newmode);
2463                         continue;
2464                 }
2465
2466                 drm_mode_probed_add(connector, newmode);
2467                 modes++;
2468         }
2469
2470         return modes;
2471 }
2472
2473 static void
2474 do_inferred_modes(struct detailed_timing *timing, void *c)
2475 {
2476         struct detailed_mode_closure *closure = c;
2477         struct detailed_non_pixel *data = &timing->data.other_data;
2478         struct detailed_data_monitor_range *range = &data->data.range;
2479
2480         if (data->type != EDID_DETAIL_MONITOR_RANGE)
2481                 return;
2482
2483         closure->modes += drm_dmt_modes_for_range(closure->connector,
2484                                                   closure->edid,
2485                                                   timing);
2486         
2487         if (!version_greater(closure->edid, 1, 1))
2488                 return; /* GTF not defined yet */
2489
2490         switch (range->flags) {
2491         case 0x02: /* secondary gtf, XXX could do more */
2492         case 0x00: /* default gtf */
2493                 closure->modes += drm_gtf_modes_for_range(closure->connector,
2494                                                           closure->edid,
2495                                                           timing);
2496                 break;
2497         case 0x04: /* cvt, only in 1.4+ */
2498                 if (!version_greater(closure->edid, 1, 3))
2499                         break;
2500
2501                 closure->modes += drm_cvt_modes_for_range(closure->connector,
2502                                                           closure->edid,
2503                                                           timing);
2504                 break;
2505         case 0x01: /* just the ranges, no formula */
2506         default:
2507                 break;
2508         }
2509 }
2510
2511 static int
2512 add_inferred_modes(struct drm_connector *connector, struct edid *edid)
2513 {
2514         struct detailed_mode_closure closure = {
2515                 .connector = connector,
2516                 .edid = edid,
2517         };
2518
2519         if (version_greater(edid, 1, 0))
2520                 drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
2521                                             &closure);
2522
2523         return closure.modes;
2524 }
2525
2526 static int
2527 drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
2528 {
2529         int i, j, m, modes = 0;
2530         struct drm_display_mode *mode;
2531         u8 *est = ((u8 *)timing) + 6;
2532
2533         for (i = 0; i < 6; i++) {
2534                 for (j = 7; j >= 0; j--) {
2535                         m = (i * 8) + (7 - j);
2536                         if (m >= ARRAY_SIZE(est3_modes))
2537                                 break;
2538                         if (est[i] & (1 << j)) {
2539                                 mode = drm_mode_find_dmt(connector->dev,
2540                                                          est3_modes[m].w,
2541                                                          est3_modes[m].h,
2542                                                          est3_modes[m].r,
2543                                                          est3_modes[m].rb);
2544                                 if (mode) {
2545                                         drm_mode_probed_add(connector, mode);
2546                                         modes++;
2547                                 }
2548                         }
2549                 }
2550         }
2551
2552         return modes;
2553 }
2554
2555 static void
2556 do_established_modes(struct detailed_timing *timing, void *c)
2557 {
2558         struct detailed_mode_closure *closure = c;
2559         struct detailed_non_pixel *data = &timing->data.other_data;
2560
2561         if (data->type == EDID_DETAIL_EST_TIMINGS)
2562                 closure->modes += drm_est3_modes(closure->connector, timing);
2563 }
2564
2565 /**
2566  * add_established_modes - get est. modes from EDID and add them
2567  * @connector: connector to add mode(s) to
2568  * @edid: EDID block to scan
2569  *
2570  * Each EDID block contains a bitmap of the supported "established modes" list
2571  * (defined above).  Tease them out and add them to the global modes list.
2572  */
2573 static int
2574 add_established_modes(struct drm_connector *connector, struct edid *edid)
2575 {
2576         struct drm_device *dev = connector->dev;
2577         unsigned long est_bits = edid->established_timings.t1 |
2578                 (edid->established_timings.t2 << 8) |
2579                 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
2580         int i, modes = 0;
2581         struct detailed_mode_closure closure = {
2582                 .connector = connector,
2583                 .edid = edid,
2584         };
2585
2586         for (i = 0; i <= EDID_EST_TIMINGS; i++) {
2587                 if (est_bits & (1<<i)) {
2588                         struct drm_display_mode *newmode;
2589                         newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
2590                         if (newmode) {
2591                                 drm_mode_probed_add(connector, newmode);
2592                                 modes++;
2593                         }
2594                 }
2595         }
2596
2597         if (version_greater(edid, 1, 0))
2598                     drm_for_each_detailed_block((u8 *)edid,
2599                                                 do_established_modes, &closure);
2600
2601         return modes + closure.modes;
2602 }
2603
2604 static void
2605 do_standard_modes(struct detailed_timing *timing, void *c)
2606 {
2607         struct detailed_mode_closure *closure = c;
2608         struct detailed_non_pixel *data = &timing->data.other_data;
2609         struct drm_connector *connector = closure->connector;
2610         struct edid *edid = closure->edid;
2611
2612         if (data->type == EDID_DETAIL_STD_MODES) {
2613                 int i;
2614                 for (i = 0; i < 6; i++) {
2615                         struct std_timing *std;
2616                         struct drm_display_mode *newmode;
2617
2618                         std = &data->data.timings[i];
2619                         newmode = drm_mode_std(connector, edid, std);
2620                         if (newmode) {
2621                                 drm_mode_probed_add(connector, newmode);
2622                                 closure->modes++;
2623                         }
2624                 }
2625         }
2626 }
2627
2628 /**
2629  * add_standard_modes - get std. modes from EDID and add them
2630  * @connector: connector to add mode(s) to
2631  * @edid: EDID block to scan
2632  *
2633  * Standard modes can be calculated using the appropriate standard (DMT,
2634  * GTF or CVT. Grab them from @edid and add them to the list.
2635  */
2636 static int
2637 add_standard_modes(struct drm_connector *connector, struct edid *edid)
2638 {
2639         int i, modes = 0;
2640         struct detailed_mode_closure closure = {
2641                 .connector = connector,
2642                 .edid = edid,
2643         };
2644
2645         for (i = 0; i < EDID_STD_TIMINGS; i++) {
2646                 struct drm_display_mode *newmode;
2647
2648                 newmode = drm_mode_std(connector, edid,
2649                                        &edid->standard_timings[i]);
2650                 if (newmode) {
2651                         drm_mode_probed_add(connector, newmode);
2652                         modes++;
2653                 }
2654         }
2655
2656         if (version_greater(edid, 1, 0))
2657                 drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
2658                                             &closure);
2659
2660         /* XXX should also look for standard codes in VTB blocks */
2661
2662         return modes + closure.modes;
2663 }
2664
2665 static int drm_cvt_modes(struct drm_connector *connector,
2666                          struct detailed_timing *timing)
2667 {
2668         int i, j, modes = 0;
2669         struct drm_display_mode *newmode;
2670         struct drm_device *dev = connector->dev;
2671         struct cvt_timing *cvt;
2672         const int rates[] = { 60, 85, 75, 60, 50 };
2673         const u8 empty[3] = { 0, 0, 0 };
2674
2675         for (i = 0; i < 4; i++) {
2676                 int uninitialized_var(width), height;
2677                 cvt = &(timing->data.other_data.data.cvt[i]);
2678
2679                 if (!memcmp(cvt->code, empty, 3))
2680                         continue;
2681
2682                 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
2683                 switch (cvt->code[1] & 0x0c) {
2684                 case 0x00:
2685                         width = height * 4 / 3;
2686                         break;
2687                 case 0x04:
2688                         width = height * 16 / 9;
2689                         break;
2690                 case 0x08:
2691                         width = height * 16 / 10;
2692                         break;
2693                 case 0x0c:
2694                         width = height * 15 / 9;
2695                         break;
2696                 }
2697
2698                 for (j = 1; j < 5; j++) {
2699                         if (cvt->code[2] & (1 << j)) {
2700                                 newmode = drm_cvt_mode(dev, width, height,
2701                                                        rates[j], j == 0,
2702                                                        false, false);
2703                                 if (newmode) {
2704                                         drm_mode_probed_add(connector, newmode);
2705                                         modes++;
2706                                 }
2707                         }
2708                 }
2709         }
2710
2711         return modes;
2712 }
2713
2714 static void
2715 do_cvt_mode(struct detailed_timing *timing, void *c)
2716 {
2717         struct detailed_mode_closure *closure = c;
2718         struct detailed_non_pixel *data = &timing->data.other_data;
2719
2720         if (data->type == EDID_DETAIL_CVT_3BYTE)
2721                 closure->modes += drm_cvt_modes(closure->connector, timing);
2722 }
2723
2724 static int
2725 add_cvt_modes(struct drm_connector *connector, struct edid *edid)
2726 {       
2727         struct detailed_mode_closure closure = {
2728                 .connector = connector,
2729                 .edid = edid,
2730         };
2731
2732         if (version_greater(edid, 1, 2))
2733                 drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
2734
2735         /* XXX should also look for CVT codes in VTB blocks */
2736
2737         return closure.modes;
2738 }
2739
2740 static void fixup_detailed_cea_mode_clock(struct drm_display_mode *mode);
2741
2742 static void
2743 do_detailed_mode(struct detailed_timing *timing, void *c)
2744 {
2745         struct detailed_mode_closure *closure = c;
2746         struct drm_display_mode *newmode;
2747
2748         if (timing->pixel_clock) {
2749                 newmode = drm_mode_detailed(closure->connector->dev,
2750                                             closure->edid, timing,
2751                                             closure->quirks);
2752                 if (!newmode)
2753                         return;
2754
2755                 if (closure->preferred)
2756                         newmode->type |= DRM_MODE_TYPE_PREFERRED;
2757
2758                 /*
2759                  * Detailed modes are limited to 10kHz pixel clock resolution,
2760                  * so fix up anything that looks like CEA/HDMI mode, but the clock
2761                  * is just slightly off.
2762                  */
2763                 fixup_detailed_cea_mode_clock(newmode);
2764
2765                 drm_mode_probed_add(closure->connector, newmode);
2766                 closure->modes++;
2767                 closure->preferred = 0;
2768         }
2769 }
2770
2771 /*
2772  * add_detailed_modes - Add modes from detailed timings
2773  * @connector: attached connector
2774  * @edid: EDID block to scan
2775  * @quirks: quirks to apply
2776  */
2777 static int
2778 add_detailed_modes(struct drm_connector *connector, struct edid *edid,
2779                    u32 quirks)
2780 {
2781         struct detailed_mode_closure closure = {
2782                 .connector = connector,
2783                 .edid = edid,
2784                 .preferred = 1,
2785                 .quirks = quirks,
2786         };
2787
2788         if (closure.preferred && !version_greater(edid, 1, 3))
2789                 closure.preferred =
2790                     (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
2791
2792         drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
2793
2794         return closure.modes;
2795 }
2796
2797 #define AUDIO_BLOCK     0x01
2798 #define VIDEO_BLOCK     0x02
2799 #define VENDOR_BLOCK    0x03
2800 #define SPEAKER_BLOCK   0x04
2801 #define USE_EXTENDED_TAG 0x07
2802 #define EXT_VIDEO_CAPABILITY_BLOCK 0x00
2803 #define EXT_VIDEO_DATA_BLOCK_420        0x0E
2804 #define EXT_VIDEO_CAP_BLOCK_Y420CMDB 0x0F
2805 #define EDID_BASIC_AUDIO        (1 << 6)
2806 #define EDID_CEA_YCRCB444       (1 << 5)
2807 #define EDID_CEA_YCRCB422       (1 << 4)
2808 #define EDID_CEA_VCDB_QS        (1 << 6)
2809
2810 /*
2811  * Search EDID for CEA extension block.
2812  */
2813 static u8 *drm_find_edid_extension(struct edid *edid, int ext_id)
2814 {
2815         u8 *edid_ext = NULL;
2816         int i;
2817
2818         /* No EDID or EDID extensions */
2819         if (edid == NULL || edid->extensions == 0)
2820                 return NULL;
2821
2822         /* Find CEA extension */
2823         for (i = 0; i < edid->extensions; i++) {
2824                 edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
2825                 if (edid_ext[0] == ext_id)
2826                         break;
2827         }
2828
2829         if (i == edid->extensions)
2830                 return NULL;
2831
2832         return edid_ext;
2833 }
2834
2835 static u8 *drm_find_cea_extension(struct edid *edid)
2836 {
2837         return drm_find_edid_extension(edid, CEA_EXT);
2838 }
2839
2840 static u8 *drm_find_displayid_extension(struct edid *edid)
2841 {
2842         return drm_find_edid_extension(edid, DISPLAYID_EXT);
2843 }
2844
2845 /*
2846  * Calculate the alternate clock for the CEA mode
2847  * (60Hz vs. 59.94Hz etc.)
2848  */
2849 static unsigned int
2850 cea_mode_alternate_clock(const struct drm_display_mode *cea_mode)
2851 {
2852         unsigned int clock = cea_mode->clock;
2853
2854         if (cea_mode->vrefresh % 6 != 0)
2855                 return clock;
2856
2857         /*
2858          * edid_cea_modes contains the 59.94Hz
2859          * variant for 240 and 480 line modes,
2860          * and the 60Hz variant otherwise.
2861          */
2862         if (cea_mode->vdisplay == 240 || cea_mode->vdisplay == 480)
2863                 clock = DIV_ROUND_CLOSEST(clock * 1001, 1000);
2864         else
2865                 clock = DIV_ROUND_CLOSEST(clock * 1000, 1001);
2866
2867         return clock;
2868 }
2869
2870 static bool
2871 cea_mode_alternate_timings(u8 vic, struct drm_display_mode *mode)
2872 {
2873         /*
2874          * For certain VICs the spec allows the vertical
2875          * front porch to vary by one or two lines.
2876          *
2877          * cea_modes[] stores the variant with the shortest
2878          * vertical front porch. We can adjust the mode to
2879          * get the other variants by simply increasing the
2880          * vertical front porch length.
2881          */
2882         BUILD_BUG_ON(edid_cea_modes[8].vtotal != 262 ||
2883                      edid_cea_modes[9].vtotal != 262 ||
2884                      edid_cea_modes[12].vtotal != 262 ||
2885                      edid_cea_modes[13].vtotal != 262 ||
2886                      edid_cea_modes[23].vtotal != 312 ||
2887                      edid_cea_modes[24].vtotal != 312 ||
2888                      edid_cea_modes[27].vtotal != 312 ||
2889                      edid_cea_modes[28].vtotal != 312);
2890
2891         if (((vic == 8 || vic == 9 ||
2892               vic == 12 || vic == 13) && mode->vtotal < 263) ||
2893             ((vic == 23 || vic == 24 ||
2894               vic == 27 || vic == 28) && mode->vtotal < 314)) {
2895                 mode->vsync_start++;
2896                 mode->vsync_end++;
2897                 mode->vtotal++;
2898
2899                 return true;
2900         }
2901
2902         return false;
2903 }
2904
2905 static u8 drm_match_cea_mode_clock_tolerance(const struct drm_display_mode *to_match,
2906                                              unsigned int clock_tolerance)
2907 {
2908         u8 vic;
2909
2910         if (!to_match->clock)
2911                 return 0;
2912
2913         for (vic = 1; vic < ARRAY_SIZE(edid_cea_modes); vic++) {
2914                 struct drm_display_mode cea_mode = edid_cea_modes[vic];
2915                 unsigned int clock1, clock2;
2916
2917                 /* Check both 60Hz and 59.94Hz */
2918                 clock1 = cea_mode.clock;
2919                 clock2 = cea_mode_alternate_clock(&cea_mode);
2920
2921                 if (abs(to_match->clock - clock1) > clock_tolerance &&
2922                     abs(to_match->clock - clock2) > clock_tolerance)
2923                         continue;
2924
2925                 do {
2926                         if (drm_mode_equal_no_clocks_no_stereo(to_match, &cea_mode))
2927                                 return vic;
2928                 } while (cea_mode_alternate_timings(vic, &cea_mode));
2929         }
2930
2931         return 0;
2932 }
2933
2934 /**
2935  * drm_match_cea_mode - look for a CEA mode matching given mode
2936  * @to_match: display mode
2937  *
2938  * Return: The CEA Video ID (VIC) of the mode or 0 if it isn't a CEA-861
2939  * mode.
2940  */
2941 u8 drm_match_cea_mode(const struct drm_display_mode *to_match)
2942 {
2943         u8 vic;
2944
2945         if (!to_match->clock)
2946                 return 0;
2947
2948         for (vic = 1; vic < ARRAY_SIZE(edid_cea_modes); vic++) {
2949                 struct drm_display_mode cea_mode = edid_cea_modes[vic];
2950                 unsigned int clock1, clock2;
2951
2952                 /* Check both 60Hz and 59.94Hz */
2953                 clock1 = cea_mode.clock;
2954                 clock2 = cea_mode_alternate_clock(&cea_mode);
2955
2956                 if (KHZ2PICOS(to_match->clock) != KHZ2PICOS(clock1) &&
2957                     KHZ2PICOS(to_match->clock) != KHZ2PICOS(clock2))
2958                         continue;
2959
2960                 do {
2961                         if (drm_mode_equal_no_clocks_no_stereo(to_match, &cea_mode))
2962                                 return vic;
2963                 } while (cea_mode_alternate_timings(vic, &cea_mode));
2964         }
2965
2966         return 0;
2967 }
2968 EXPORT_SYMBOL(drm_match_cea_mode);
2969
2970 static bool drm_valid_cea_vic(u8 vic)
2971 {
2972         return vic > 0 && vic < ARRAY_SIZE(edid_cea_modes);
2973 }
2974
2975 /**
2976  * drm_get_cea_aspect_ratio - get the picture aspect ratio corresponding to
2977  * the input VIC from the CEA mode list
2978  * @video_code: ID given to each of the CEA modes
2979  *
2980  * Returns picture aspect ratio
2981  */
2982 enum hdmi_picture_aspect drm_get_cea_aspect_ratio(const u8 video_code)
2983 {
2984         return edid_cea_modes[video_code].picture_aspect_ratio;
2985 }
2986 EXPORT_SYMBOL(drm_get_cea_aspect_ratio);
2987
2988 /*
2989  * Calculate the alternate clock for HDMI modes (those from the HDMI vendor
2990  * specific block).
2991  *
2992  * It's almost like cea_mode_alternate_clock(), we just need to add an
2993  * exception for the VIC 4 mode (4096x2160@24Hz): no alternate clock for this
2994  * one.
2995  */
2996 static unsigned int
2997 hdmi_mode_alternate_clock(const struct drm_display_mode *hdmi_mode)
2998 {
2999         if (hdmi_mode->vdisplay == 4096 && hdmi_mode->hdisplay == 2160)
3000                 return hdmi_mode->clock;
3001
3002         return cea_mode_alternate_clock(hdmi_mode);
3003 }
3004
3005 static u8 drm_match_hdmi_mode_clock_tolerance(const struct drm_display_mode *to_match,
3006                                               unsigned int clock_tolerance)
3007 {
3008         u8 vic;
3009
3010         if (!to_match->clock)
3011                 return 0;
3012
3013         for (vic = 1; vic < ARRAY_SIZE(edid_4k_modes); vic++) {
3014                 const struct drm_display_mode *hdmi_mode = &edid_4k_modes[vic];
3015                 unsigned int clock1, clock2;
3016
3017                 /* Make sure to also match alternate clocks */
3018                 clock1 = hdmi_mode->clock;
3019                 clock2 = hdmi_mode_alternate_clock(hdmi_mode);
3020
3021                 if (abs(to_match->clock - clock1) > clock_tolerance &&
3022                     abs(to_match->clock - clock2) > clock_tolerance)
3023                         continue;
3024
3025                 if (drm_mode_equal_no_clocks(to_match, hdmi_mode))
3026                         return vic;
3027         }
3028
3029         return 0;
3030 }
3031
3032 /*
3033  * drm_match_hdmi_mode - look for a HDMI mode matching given mode
3034  * @to_match: display mode
3035  *
3036  * An HDMI mode is one defined in the HDMI vendor specific block.
3037  *
3038  * Returns the HDMI Video ID (VIC) of the mode or 0 if it isn't one.
3039  */
3040 static u8 drm_match_hdmi_mode(const struct drm_display_mode *to_match)
3041 {
3042         u8 vic;
3043
3044         if (!to_match->clock)
3045                 return 0;
3046
3047         for (vic = 1; vic < ARRAY_SIZE(edid_4k_modes); vic++) {
3048                 const struct drm_display_mode *hdmi_mode = &edid_4k_modes[vic];
3049                 unsigned int clock1, clock2;
3050
3051                 /* Make sure to also match alternate clocks */
3052                 clock1 = hdmi_mode->clock;
3053                 clock2 = hdmi_mode_alternate_clock(hdmi_mode);
3054
3055                 if ((KHZ2PICOS(to_match->clock) == KHZ2PICOS(clock1) ||
3056                      KHZ2PICOS(to_match->clock) == KHZ2PICOS(clock2)) &&
3057                     drm_mode_equal_no_clocks_no_stereo(to_match, hdmi_mode))
3058                         return vic;
3059         }
3060         return 0;
3061 }
3062
3063 static bool drm_valid_hdmi_vic(u8 vic)
3064 {
3065         return vic > 0 && vic < ARRAY_SIZE(edid_4k_modes);
3066 }
3067
3068 static int
3069 add_alternate_cea_modes(struct drm_connector *connector, struct edid *edid)
3070 {
3071         struct drm_device *dev = connector->dev;
3072         struct drm_display_mode *mode, *tmp;
3073         LIST_HEAD(list);
3074         int modes = 0;
3075
3076         /* Don't add CEA modes if the CEA extension block is missing */
3077         if (!drm_find_cea_extension(edid))
3078                 return 0;
3079
3080         /*
3081          * Go through all probed modes and create a new mode
3082          * with the alternate clock for certain CEA modes.
3083          */
3084         list_for_each_entry(mode, &connector->probed_modes, head) {
3085                 const struct drm_display_mode *cea_mode = NULL;
3086                 struct drm_display_mode *newmode;
3087                 u8 vic = drm_match_cea_mode(mode);
3088                 unsigned int clock1, clock2;
3089
3090                 if (drm_valid_cea_vic(vic)) {
3091                         cea_mode = &edid_cea_modes[vic];
3092                         clock2 = cea_mode_alternate_clock(cea_mode);
3093                 } else {
3094                         vic = drm_match_hdmi_mode(mode);
3095                         if (drm_valid_hdmi_vic(vic)) {
3096                                 cea_mode = &edid_4k_modes[vic];
3097                                 clock2 = hdmi_mode_alternate_clock(cea_mode);
3098                         }
3099                 }
3100
3101                 if (!cea_mode)
3102                         continue;
3103
3104                 clock1 = cea_mode->clock;
3105
3106                 if (clock1 == clock2)
3107                         continue;
3108
3109                 if (mode->clock != clock1 && mode->clock != clock2)
3110                         continue;
3111
3112                 newmode = drm_mode_duplicate(dev, cea_mode);
3113                 if (!newmode)
3114                         continue;
3115
3116                 /* Carry over the stereo flags */
3117                 newmode->flags |= mode->flags & DRM_MODE_FLAG_3D_MASK;
3118
3119                 /*
3120                  * The current mode could be either variant. Make
3121                  * sure to pick the "other" clock for the new mode.
3122                  */
3123                 if (mode->clock != clock1)
3124                         newmode->clock = clock1;
3125                 else
3126                         newmode->clock = clock2;
3127
3128                 list_add_tail(&newmode->head, &list);
3129         }
3130
3131         list_for_each_entry_safe(mode, tmp, &list, head) {
3132                 list_del(&mode->head);
3133                 drm_mode_probed_add(connector, mode);
3134                 modes++;
3135         }
3136
3137         return modes;
3138 }
3139
3140 static u8 svd_to_vic(u8 svd)
3141 {
3142         /* 0-6 bit vic, 7th bit native mode indicator */
3143         if ((svd >= 1 &&  svd <= 64) || (svd >= 129 && svd <= 192))
3144                 return svd & 127;
3145
3146         return svd;
3147 }
3148
3149 static struct drm_display_mode *
3150 drm_display_mode_from_vic_index(struct drm_connector *connector,
3151                                 const u8 *video_db, u8 video_len,
3152                                 u8 video_index)
3153 {
3154         struct drm_device *dev = connector->dev;
3155         struct drm_display_mode *newmode;
3156         u8 vic;
3157
3158         if (video_db == NULL || video_index >= video_len)
3159                 return NULL;
3160
3161         /* CEA modes are numbered 1..127 */
3162         vic = svd_to_vic(video_db[video_index]);
3163         if (!drm_valid_cea_vic(vic))
3164                 return NULL;
3165
3166         newmode = drm_mode_duplicate(dev, &edid_cea_modes[vic]);
3167         if (!newmode)
3168                 return NULL;
3169
3170         newmode->vrefresh = 0;
3171
3172         return newmode;
3173 }
3174
3175 /*
3176  * do_y420vdb_modes - Parse YCBCR 420 only modes
3177  * @connector: connector corresponding to the HDMI sink
3178  * @svds: start of the data block of CEA YCBCR 420 VDB
3179  * @len: length of the CEA YCBCR 420 VDB
3180  *
3181  * Parse the CEA-861-F YCBCR 420 Video Data Block (Y420VDB)
3182  * which contains modes which can be supported in YCBCR 420
3183  * output format only.
3184  */
3185 static int do_y420vdb_modes(struct drm_connector *connector,
3186                             const u8 *svds, u8 svds_len)
3187 {
3188         int modes = 0, i;
3189         struct drm_device *dev = connector->dev;
3190         struct drm_display_info *info = &connector->display_info;
3191         struct drm_hdmi_info *hdmi = &info->hdmi;
3192
3193         for (i = 0; i < svds_len; i++) {
3194                 u8 vic = svd_to_vic(svds[i]);
3195                 struct drm_display_mode *newmode;
3196
3197                 if (!drm_valid_cea_vic(vic))
3198                         continue;
3199
3200                 newmode = drm_mode_duplicate(dev, &edid_cea_modes[vic]);
3201                 if (!newmode)
3202                         break;
3203                 bitmap_set(hdmi->y420_vdb_modes, vic, 1);
3204                 drm_mode_probed_add(connector, newmode);
3205                 modes++;
3206         }
3207
3208         if (modes > 0)
3209                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB420;
3210         return modes;
3211 }
3212
3213 /*
3214  * drm_add_cmdb_modes - Add a YCBCR 420 mode into bitmap
3215  * @connector: connector corresponding to the HDMI sink
3216  * @vic: CEA vic for the video mode to be added in the map
3217  *
3218  * Makes an entry for a videomode in the YCBCR 420 bitmap
3219  */
3220 static void
3221 drm_add_cmdb_modes(struct drm_connector *connector, u8 svd)
3222 {
3223         u8 vic = svd_to_vic(svd);
3224         struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
3225
3226         if (!drm_valid_cea_vic(vic))
3227                 return;
3228
3229         bitmap_set(hdmi->y420_cmdb_modes, vic, 1);
3230 }
3231
3232 static int
3233 do_cea_modes(struct drm_connector *connector, const u8 *db, u8 len)
3234 {
3235         int i, modes = 0;
3236         struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
3237
3238         for (i = 0; i < len; i++) {
3239                 struct drm_display_mode *mode;
3240                 mode = drm_display_mode_from_vic_index(connector, db, len, i);
3241                 if (mode) {
3242                         /*
3243                          * YCBCR420 capability block contains a bitmap which
3244                          * gives the index of CEA modes from CEA VDB, which
3245                          * can support YCBCR 420 sampling output also (apart
3246                          * from RGB/YCBCR444 etc).
3247                          * For example, if the bit 0 in bitmap is set,
3248                          * first mode in VDB can support YCBCR420 output too.
3249                          * Add YCBCR420 modes only if sink is HDMI 2.0 capable.
3250                          */
3251                         if (i < 64 && hdmi->y420_cmdb_map & (1ULL << i))
3252                                 drm_add_cmdb_modes(connector, db[i]);
3253
3254                         drm_mode_probed_add(connector, mode);
3255                         modes++;
3256                 }
3257         }
3258
3259         return modes;
3260 }
3261
3262 struct stereo_mandatory_mode {
3263         int width, height, vrefresh;
3264         unsigned int flags;
3265 };
3266
3267 static const struct stereo_mandatory_mode stereo_mandatory_modes[] = {
3268         { 1920, 1080, 24, DRM_MODE_FLAG_3D_TOP_AND_BOTTOM },
3269         { 1920, 1080, 24, DRM_MODE_FLAG_3D_FRAME_PACKING },
3270         { 1920, 1080, 50,
3271           DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF },
3272         { 1920, 1080, 60,
3273           DRM_MODE_FLAG_INTERLACE | DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF },
3274         { 1280, 720,  50, DRM_MODE_FLAG_3D_TOP_AND_BOTTOM },
3275         { 1280, 720,  50, DRM_MODE_FLAG_3D_FRAME_PACKING },
3276         { 1280, 720,  60, DRM_MODE_FLAG_3D_TOP_AND_BOTTOM },
3277         { 1280, 720,  60, DRM_MODE_FLAG_3D_FRAME_PACKING }
3278 };
3279
3280 static bool
3281 stereo_match_mandatory(const struct drm_display_mode *mode,
3282                        const struct stereo_mandatory_mode *stereo_mode)
3283 {
3284         unsigned int interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
3285
3286         return mode->hdisplay == stereo_mode->width &&
3287                mode->vdisplay == stereo_mode->height &&
3288                interlaced == (stereo_mode->flags & DRM_MODE_FLAG_INTERLACE) &&
3289                drm_mode_vrefresh(mode) == stereo_mode->vrefresh;
3290 }
3291
3292 static int add_hdmi_mandatory_stereo_modes(struct drm_connector *connector)
3293 {
3294         struct drm_device *dev = connector->dev;
3295         const struct drm_display_mode *mode;
3296         struct list_head stereo_modes;
3297         int modes = 0, i;
3298
3299         INIT_LIST_HEAD(&stereo_modes);
3300
3301         list_for_each_entry(mode, &connector->probed_modes, head) {
3302                 for (i = 0; i < ARRAY_SIZE(stereo_mandatory_modes); i++) {
3303                         const struct stereo_mandatory_mode *mandatory;
3304                         struct drm_display_mode *new_mode;
3305
3306                         if (!stereo_match_mandatory(mode,
3307                                                     &stereo_mandatory_modes[i]))
3308                                 continue;
3309
3310                         mandatory = &stereo_mandatory_modes[i];
3311                         new_mode = drm_mode_duplicate(dev, mode);
3312                         if (!new_mode)
3313                                 continue;
3314
3315                         new_mode->flags |= mandatory->flags;
3316                         list_add_tail(&new_mode->head, &stereo_modes);
3317                         modes++;
3318                 }
3319         }
3320
3321         list_splice_tail(&stereo_modes, &connector->probed_modes);
3322
3323         return modes;
3324 }
3325
3326 static int add_hdmi_mode(struct drm_connector *connector, u8 vic)
3327 {
3328         struct drm_device *dev = connector->dev;
3329         struct drm_display_mode *newmode;
3330
3331         if (!drm_valid_hdmi_vic(vic)) {
3332                 DRM_ERROR("Unknown HDMI VIC: %d\n", vic);
3333                 return 0;
3334         }
3335
3336         newmode = drm_mode_duplicate(dev, &edid_4k_modes[vic]);
3337         if (!newmode)
3338                 return 0;
3339
3340         drm_mode_probed_add(connector, newmode);
3341
3342         return 1;
3343 }
3344
3345 static int add_3d_struct_modes(struct drm_connector *connector, u16 structure,
3346                                const u8 *video_db, u8 video_len, u8 video_index)
3347 {
3348         struct drm_display_mode *newmode;
3349         int modes = 0;
3350
3351         if (structure & (1 << 0)) {
3352                 newmode = drm_display_mode_from_vic_index(connector, video_db,
3353                                                           video_len,
3354                                                           video_index);
3355                 if (newmode) {
3356                         newmode->flags |= DRM_MODE_FLAG_3D_FRAME_PACKING;
3357                         drm_mode_probed_add(connector, newmode);
3358                         modes++;
3359                 }
3360         }
3361         if (structure & (1 << 6)) {
3362                 newmode = drm_display_mode_from_vic_index(connector, video_db,
3363                                                           video_len,
3364                                                           video_index);
3365                 if (newmode) {
3366                         newmode->flags |= DRM_MODE_FLAG_3D_TOP_AND_BOTTOM;
3367                         drm_mode_probed_add(connector, newmode);
3368                         modes++;
3369                 }
3370         }
3371         if (structure & (1 << 8)) {
3372                 newmode = drm_display_mode_from_vic_index(connector, video_db,
3373                                                           video_len,
3374                                                           video_index);
3375                 if (newmode) {
3376                         newmode->flags |= DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF;
3377                         drm_mode_probed_add(connector, newmode);
3378                         modes++;
3379                 }
3380         }
3381
3382         return modes;
3383 }
3384
3385 /*
3386  * do_hdmi_vsdb_modes - Parse the HDMI Vendor Specific data block
3387  * @connector: connector corresponding to the HDMI sink
3388  * @db: start of the CEA vendor specific block
3389  * @len: length of the CEA block payload, ie. one can access up to db[len]
3390  *
3391  * Parses the HDMI VSDB looking for modes to add to @connector. This function
3392  * also adds the stereo 3d modes when applicable.
3393  */
3394 static int
3395 do_hdmi_vsdb_modes(struct drm_connector *connector, const u8 *db, u8 len,
3396                    const u8 *video_db, u8 video_len)
3397 {
3398         int modes = 0, offset = 0, i, multi_present = 0, multi_len;
3399         u8 vic_len, hdmi_3d_len = 0;
3400         u16 mask;
3401         u16 structure_all;
3402
3403         if (len < 8)
3404                 goto out;
3405
3406         /* no HDMI_Video_Present */
3407         if (!(db[8] & (1 << 5)))
3408                 goto out;
3409
3410         /* Latency_Fields_Present */
3411         if (db[8] & (1 << 7))
3412                 offset += 2;
3413
3414         /* I_Latency_Fields_Present */
3415         if (db[8] & (1 << 6))
3416                 offset += 2;
3417
3418         /* the declared length is not long enough for the 2 first bytes
3419          * of additional video format capabilities */
3420         if (len < (8 + offset + 2))
3421                 goto out;
3422
3423         /* 3D_Present */
3424         offset++;
3425         if (db[8 + offset] & (1 << 7)) {
3426                 modes += add_hdmi_mandatory_stereo_modes(connector);
3427
3428                 /* 3D_Multi_present */
3429                 multi_present = (db[8 + offset] & 0x60) >> 5;
3430         }
3431
3432         offset++;
3433         vic_len = db[8 + offset] >> 5;
3434         hdmi_3d_len = db[8 + offset] & 0x1f;
3435
3436         for (i = 0; i < vic_len && len >= (9 + offset + i); i++) {
3437                 u8 vic;
3438
3439                 vic = db[9 + offset + i];
3440                 modes += add_hdmi_mode(connector, vic);
3441         }
3442         offset += 1 + vic_len;
3443
3444         if (multi_present == 1)
3445                 multi_len = 2;
3446         else if (multi_present == 2)
3447                 multi_len = 4;
3448         else
3449                 multi_len = 0;
3450
3451         if (len < (8 + offset + hdmi_3d_len - 1))
3452                 goto out;
3453
3454         if (hdmi_3d_len < multi_len)
3455                 goto out;
3456
3457         if (multi_present == 1 || multi_present == 2) {
3458                 /* 3D_Structure_ALL */
3459                 structure_all = (db[8 + offset] << 8) | db[9 + offset];
3460
3461                 /* check if 3D_MASK is present */
3462                 if (multi_present == 2)
3463                         mask = (db[10 + offset] << 8) | db[11 + offset];
3464                 else
3465                         mask = 0xffff;
3466
3467                 for (i = 0; i < 16; i++) {
3468                         if (mask & (1 << i))
3469                                 modes += add_3d_struct_modes(connector,
3470                                                 structure_all,
3471                                                 video_db,
3472                                                 video_len, i);
3473                 }
3474         }
3475
3476         offset += multi_len;
3477
3478         for (i = 0; i < (hdmi_3d_len - multi_len); i++) {
3479                 int vic_index;
3480                 struct drm_display_mode *newmode = NULL;
3481                 unsigned int newflag = 0;
3482                 bool detail_present;
3483
3484                 detail_present = ((db[8 + offset + i] & 0x0f) > 7);
3485
3486                 if (detail_present && (i + 1 == hdmi_3d_len - multi_len))
3487                         break;
3488
3489                 /* 2D_VIC_order_X */
3490                 vic_index = db[8 + offset + i] >> 4;
3491
3492                 /* 3D_Structure_X */
3493                 switch (db[8 + offset + i] & 0x0f) {
3494                 case 0:
3495                         newflag = DRM_MODE_FLAG_3D_FRAME_PACKING;
3496                         break;
3497                 case 6:
3498                         newflag = DRM_MODE_FLAG_3D_TOP_AND_BOTTOM;
3499                         break;
3500                 case 8:
3501                         /* 3D_Detail_X */
3502                         if ((db[9 + offset + i] >> 4) == 1)
3503                                 newflag = DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF;
3504                         break;
3505                 }
3506
3507                 if (newflag != 0) {
3508                         newmode = drm_display_mode_from_vic_index(connector,
3509                                                                   video_db,
3510                                                                   video_len,
3511                                                                   vic_index);
3512
3513                         if (newmode) {
3514                                 newmode->flags |= newflag;
3515                                 drm_mode_probed_add(connector, newmode);
3516                                 modes++;
3517                         }
3518                 }
3519
3520                 if (detail_present)
3521                         i++;
3522         }
3523
3524 out:
3525         return modes;
3526 }
3527
3528 static int
3529 cea_db_payload_len(const u8 *db)
3530 {
3531         return db[0] & 0x1f;
3532 }
3533
3534 static int
3535 cea_db_extended_tag(const u8 *db)
3536 {
3537         return db[1];
3538 }
3539
3540 static int
3541 cea_db_tag(const u8 *db)
3542 {
3543         return db[0] >> 5;
3544 }
3545
3546 static int
3547 cea_revision(const u8 *cea)
3548 {
3549         return cea[1];
3550 }
3551
3552 static int
3553 cea_db_offsets(const u8 *cea, int *start, int *end)
3554 {
3555         /* Data block offset in CEA extension block */
3556         *start = 4;
3557         *end = cea[2];
3558         if (*end == 0)
3559                 *end = 127;
3560         if (*end < 4 || *end > 127)
3561                 return -ERANGE;
3562         return 0;
3563 }
3564
3565 static bool cea_db_is_hdmi_vsdb(const u8 *db)
3566 {
3567         int hdmi_id;
3568
3569         if (cea_db_tag(db) != VENDOR_BLOCK)
3570                 return false;
3571
3572         if (cea_db_payload_len(db) < 5)
3573                 return false;
3574
3575         hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
3576
3577         return hdmi_id == HDMI_IEEE_OUI;
3578 }
3579
3580 static bool cea_db_is_hdmi_forum_vsdb(const u8 *db)
3581 {
3582         unsigned int oui;
3583
3584         if (cea_db_tag(db) != VENDOR_BLOCK)
3585                 return false;
3586
3587         if (cea_db_payload_len(db) < 7)
3588                 return false;
3589
3590         oui = db[3] << 16 | db[2] << 8 | db[1];
3591
3592         return oui == HDMI_FORUM_IEEE_OUI;
3593 }
3594
3595 static bool cea_db_is_y420cmdb(const u8 *db)
3596 {
3597         if (cea_db_tag(db) != USE_EXTENDED_TAG)
3598                 return false;
3599
3600         if (!cea_db_payload_len(db))
3601                 return false;
3602
3603         if (cea_db_extended_tag(db) != EXT_VIDEO_CAP_BLOCK_Y420CMDB)
3604                 return false;
3605
3606         return true;
3607 }
3608
3609 static bool cea_db_is_y420vdb(const u8 *db)
3610 {
3611         if (cea_db_tag(db) != USE_EXTENDED_TAG)
3612                 return false;
3613
3614         if (!cea_db_payload_len(db))
3615                 return false;
3616
3617         if (cea_db_extended_tag(db) != EXT_VIDEO_DATA_BLOCK_420)
3618                 return false;
3619
3620         return true;
3621 }
3622
3623 #define for_each_cea_db(cea, i, start, end) \
3624         for ((i) = (start); (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
3625
3626 static void drm_parse_y420cmdb_bitmap(struct drm_connector *connector,
3627                                       const u8 *db)
3628 {
3629         struct drm_display_info *info = &connector->display_info;
3630         struct drm_hdmi_info *hdmi = &info->hdmi;
3631         u8 map_len = cea_db_payload_len(db) - 1;
3632         u8 count;
3633         u64 map = 0;
3634
3635         if (map_len == 0) {
3636                 /* All CEA modes support ycbcr420 sampling also.*/
3637                 hdmi->y420_cmdb_map = U64_MAX;
3638                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB420;
3639                 return;
3640         }
3641
3642         /*
3643          * This map indicates which of the existing CEA block modes
3644          * from VDB can support YCBCR420 output too. So if bit=0 is
3645          * set, first mode from VDB can support YCBCR420 output too.
3646          * We will parse and keep this map, before parsing VDB itself
3647          * to avoid going through the same block again and again.
3648          *
3649          * Spec is not clear about max possible size of this block.
3650          * Clamping max bitmap block size at 8 bytes. Every byte can
3651          * address 8 CEA modes, in this way this map can address
3652          * 8*8 = first 64 SVDs.
3653          */
3654         if (WARN_ON_ONCE(map_len > 8))
3655                 map_len = 8;
3656
3657         for (count = 0; count < map_len; count++)
3658                 map |= (u64)db[2 + count] << (8 * count);
3659
3660         if (map)
3661                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB420;
3662
3663         hdmi->y420_cmdb_map = map;
3664 }
3665
3666 static int
3667 add_cea_modes(struct drm_connector *connector, struct edid *edid)
3668 {
3669         const u8 *cea = drm_find_cea_extension(edid);
3670         const u8 *db, *hdmi = NULL, *video = NULL;
3671         u8 dbl, hdmi_len, video_len = 0;
3672         int modes = 0;
3673
3674         if (cea && cea_revision(cea) >= 3) {
3675                 int i, start, end;
3676
3677                 if (cea_db_offsets(cea, &start, &end))
3678                         return 0;
3679
3680                 for_each_cea_db(cea, i, start, end) {
3681                         db = &cea[i];
3682                         dbl = cea_db_payload_len(db);
3683
3684                         if (cea_db_tag(db) == VIDEO_BLOCK) {
3685                                 video = db + 1;
3686                                 video_len = dbl;
3687                                 modes += do_cea_modes(connector, video, dbl);
3688                         } else if (cea_db_is_hdmi_vsdb(db)) {
3689                                 hdmi = db;
3690                                 hdmi_len = dbl;
3691                         } else if (cea_db_is_y420vdb(db)) {
3692                                 const u8 *vdb420 = &db[2];
3693
3694                                 /* Add 4:2:0(only) modes present in EDID */
3695                                 modes += do_y420vdb_modes(connector,
3696                                                           vdb420,
3697                                                           dbl - 1);
3698                         }
3699                 }
3700         }
3701
3702         /*
3703          * We parse the HDMI VSDB after having added the cea modes as we will
3704          * be patching their flags when the sink supports stereo 3D.
3705          */
3706         if (hdmi)
3707                 modes += do_hdmi_vsdb_modes(connector, hdmi, hdmi_len, video,
3708                                             video_len);
3709
3710         return modes;
3711 }
3712
3713 static void fixup_detailed_cea_mode_clock(struct drm_display_mode *mode)
3714 {
3715         const struct drm_display_mode *cea_mode;
3716         int clock1, clock2, clock;
3717         u8 vic;
3718         const char *type;
3719
3720         /*
3721          * allow 5kHz clock difference either way to account for
3722          * the 10kHz clock resolution limit of detailed timings.
3723          */
3724         vic = drm_match_cea_mode_clock_tolerance(mode, 5);
3725         if (drm_valid_cea_vic(vic)) {
3726                 type = "CEA";
3727                 cea_mode = &edid_cea_modes[vic];
3728                 clock1 = cea_mode->clock;
3729                 clock2 = cea_mode_alternate_clock(cea_mode);
3730         } else {
3731                 vic = drm_match_hdmi_mode_clock_tolerance(mode, 5);
3732                 if (drm_valid_hdmi_vic(vic)) {
3733                         type = "HDMI";
3734                         cea_mode = &edid_4k_modes[vic];
3735                         clock1 = cea_mode->clock;
3736                         clock2 = hdmi_mode_alternate_clock(cea_mode);
3737                 } else {
3738                         return;
3739                 }
3740         }
3741
3742         /* pick whichever is closest */
3743         if (abs(mode->clock - clock1) < abs(mode->clock - clock2))
3744                 clock = clock1;
3745         else
3746                 clock = clock2;
3747
3748         if (mode->clock == clock)
3749                 return;
3750
3751         DRM_DEBUG("detailed mode matches %s VIC %d, adjusting clock %d -> %d\n",
3752                   type, vic, mode->clock, clock);
3753         mode->clock = clock;
3754 }
3755
3756 static void
3757 drm_parse_hdmi_vsdb_audio(struct drm_connector *connector, const u8 *db)
3758 {
3759         u8 len = cea_db_payload_len(db);
3760
3761         if (len >= 6)
3762                 connector->eld[5] |= (db[6] >> 7) << 1;  /* Supports_AI */
3763         if (len >= 8) {
3764                 connector->latency_present[0] = db[8] >> 7;
3765                 connector->latency_present[1] = (db[8] >> 6) & 1;
3766         }
3767         if (len >= 9)
3768                 connector->video_latency[0] = db[9];
3769         if (len >= 10)
3770                 connector->audio_latency[0] = db[10];
3771         if (len >= 11)
3772                 connector->video_latency[1] = db[11];
3773         if (len >= 12)
3774                 connector->audio_latency[1] = db[12];
3775
3776         DRM_DEBUG_KMS("HDMI: latency present %d %d, "
3777                       "video latency %d %d, "
3778                       "audio latency %d %d\n",
3779                       connector->latency_present[0],
3780                       connector->latency_present[1],
3781                       connector->video_latency[0],
3782                       connector->video_latency[1],
3783                       connector->audio_latency[0],
3784                       connector->audio_latency[1]);
3785 }
3786
3787 static void
3788 monitor_name(struct detailed_timing *t, void *data)
3789 {
3790         if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
3791                 *(u8 **)data = t->data.other_data.data.str.str;
3792 }
3793
3794 static int get_monitor_name(struct edid *edid, char name[13])
3795 {
3796         char *edid_name = NULL;
3797         int mnl;
3798
3799         if (!edid || !name)
3800                 return 0;
3801
3802         drm_for_each_detailed_block((u8 *)edid, monitor_name, &edid_name);
3803         for (mnl = 0; edid_name && mnl < 13; mnl++) {
3804                 if (edid_name[mnl] == 0x0a)
3805                         break;
3806
3807                 name[mnl] = edid_name[mnl];
3808         }
3809
3810         return mnl;
3811 }
3812
3813 /**
3814  * drm_edid_get_monitor_name - fetch the monitor name from the edid
3815  * @edid: monitor EDID information
3816  * @name: pointer to a character array to hold the name of the monitor
3817  * @bufsize: The size of the name buffer (should be at least 14 chars.)
3818  *
3819  */
3820 void drm_edid_get_monitor_name(struct edid *edid, char *name, int bufsize)
3821 {
3822         int name_length;
3823         char buf[13];
3824         
3825         if (bufsize <= 0)
3826                 return;
3827
3828         name_length = min(get_monitor_name(edid, buf), bufsize - 1);
3829         memcpy(name, buf, name_length);
3830         name[name_length] = '\0';
3831 }
3832 EXPORT_SYMBOL(drm_edid_get_monitor_name);
3833
3834 /**
3835  * drm_edid_to_eld - build ELD from EDID
3836  * @connector: connector corresponding to the HDMI/DP sink
3837  * @edid: EDID to parse
3838  *
3839  * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver. The
3840  * Conn_Type, HDCP and Port_ID ELD fields are left for the graphics driver to
3841  * fill in.
3842  */
3843 void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
3844 {
3845         uint8_t *eld = connector->eld;
3846         u8 *cea;
3847         u8 *db;
3848         int total_sad_count = 0;
3849         int mnl;
3850         int dbl;
3851
3852         memset(eld, 0, sizeof(connector->eld));
3853
3854         connector->latency_present[0] = false;
3855         connector->latency_present[1] = false;
3856         connector->video_latency[0] = 0;
3857         connector->audio_latency[0] = 0;
3858         connector->video_latency[1] = 0;
3859         connector->audio_latency[1] = 0;
3860
3861         if (!edid)
3862                 return;
3863
3864         cea = drm_find_cea_extension(edid);
3865         if (!cea) {
3866                 DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
3867                 return;
3868         }
3869
3870         mnl = get_monitor_name(edid, eld + 20);
3871
3872         eld[4] = (cea[1] << 5) | mnl;
3873         DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
3874
3875         eld[0] = 2 << 3;                /* ELD version: 2 */
3876
3877         eld[16] = edid->mfg_id[0];
3878         eld[17] = edid->mfg_id[1];
3879         eld[18] = edid->prod_code[0];
3880         eld[19] = edid->prod_code[1];
3881
3882         if (cea_revision(cea) >= 3) {
3883                 int i, start, end;
3884
3885                 if (cea_db_offsets(cea, &start, &end)) {
3886                         start = 0;
3887                         end = 0;
3888                 }
3889
3890                 for_each_cea_db(cea, i, start, end) {
3891                         db = &cea[i];
3892                         dbl = cea_db_payload_len(db);
3893
3894                         switch (cea_db_tag(db)) {
3895                                 int sad_count;
3896
3897                         case AUDIO_BLOCK:
3898                                 /* Audio Data Block, contains SADs */
3899                                 sad_count = min(dbl / 3, 15 - total_sad_count);
3900                                 if (sad_count >= 1)
3901                                         memcpy(eld + 20 + mnl + total_sad_count * 3,
3902                                                &db[1], sad_count * 3);
3903                                 total_sad_count += sad_count;
3904                                 break;
3905                         case SPEAKER_BLOCK:
3906                                 /* Speaker Allocation Data Block */
3907                                 if (dbl >= 1)
3908                                         eld[7] = db[1];
3909                                 break;
3910                         case VENDOR_BLOCK:
3911                                 /* HDMI Vendor-Specific Data Block */
3912                                 if (cea_db_is_hdmi_vsdb(db))
3913                                         drm_parse_hdmi_vsdb_audio(connector, db);
3914                                 break;
3915                         default:
3916                                 break;
3917                         }
3918                 }
3919         }
3920         eld[5] |= total_sad_count << 4;
3921
3922         eld[DRM_ELD_BASELINE_ELD_LEN] =
3923                 DIV_ROUND_UP(drm_eld_calc_baseline_block_size(eld), 4);
3924
3925         DRM_DEBUG_KMS("ELD size %d, SAD count %d\n",
3926                       drm_eld_size(eld), total_sad_count);
3927 }
3928 EXPORT_SYMBOL(drm_edid_to_eld);
3929
3930 /**
3931  * drm_edid_to_sad - extracts SADs from EDID
3932  * @edid: EDID to parse
3933  * @sads: pointer that will be set to the extracted SADs
3934  *
3935  * Looks for CEA EDID block and extracts SADs (Short Audio Descriptors) from it.
3936  *
3937  * Note: The returned pointer needs to be freed using kfree().
3938  *
3939  * Return: The number of found SADs or negative number on error.
3940  */
3941 int drm_edid_to_sad(struct edid *edid, struct cea_sad **sads)
3942 {
3943         int count = 0;
3944         int i, start, end, dbl;
3945         u8 *cea;
3946
3947         cea = drm_find_cea_extension(edid);
3948         if (!cea) {
3949                 DRM_DEBUG_KMS("SAD: no CEA Extension found\n");
3950                 return -ENOENT;
3951         }
3952
3953         if (cea_revision(cea) < 3) {
3954                 DRM_DEBUG_KMS("SAD: wrong CEA revision\n");
3955                 return -ENOTSUPP;
3956         }
3957
3958         if (cea_db_offsets(cea, &start, &end)) {
3959                 DRM_DEBUG_KMS("SAD: invalid data block offsets\n");
3960                 return -EPROTO;
3961         }
3962
3963         for_each_cea_db(cea, i, start, end) {
3964                 u8 *db = &cea[i];
3965
3966                 if (cea_db_tag(db) == AUDIO_BLOCK) {
3967                         int j;
3968                         dbl = cea_db_payload_len(db);
3969
3970                         count = dbl / 3; /* SAD is 3B */
3971                         *sads = kcalloc(count, sizeof(**sads), GFP_KERNEL);
3972                         if (!*sads)
3973                                 return -ENOMEM;
3974                         for (j = 0; j < count; j++) {
3975                                 u8 *sad = &db[1 + j * 3];
3976
3977                                 (*sads)[j].format = (sad[0] & 0x78) >> 3;
3978                                 (*sads)[j].channels = sad[0] & 0x7;
3979                                 (*sads)[j].freq = sad[1] & 0x7F;
3980                                 (*sads)[j].byte2 = sad[2];
3981                         }
3982                         break;
3983                 }
3984         }
3985
3986         return count;
3987 }
3988 EXPORT_SYMBOL(drm_edid_to_sad);
3989
3990 /**
3991  * drm_edid_to_speaker_allocation - extracts Speaker Allocation Data Blocks from EDID
3992  * @edid: EDID to parse
3993  * @sadb: pointer to the speaker block
3994  *
3995  * Looks for CEA EDID block and extracts the Speaker Allocation Data Block from it.
3996  *
3997  * Note: The returned pointer needs to be freed using kfree().
3998  *
3999  * Return: The number of found Speaker Allocation Blocks or negative number on
4000  * error.
4001  */
4002 int drm_edid_to_speaker_allocation(struct edid *edid, u8 **sadb)
4003 {
4004         int count = 0;
4005         int i, start, end, dbl;
4006         const u8 *cea;
4007
4008         cea = drm_find_cea_extension(edid);
4009         if (!cea) {
4010                 DRM_DEBUG_KMS("SAD: no CEA Extension found\n");
4011                 return -ENOENT;
4012         }
4013
4014         if (cea_revision(cea) < 3) {
4015                 DRM_DEBUG_KMS("SAD: wrong CEA revision\n");
4016                 return -ENOTSUPP;
4017         }
4018
4019         if (cea_db_offsets(cea, &start, &end)) {
4020                 DRM_DEBUG_KMS("SAD: invalid data block offsets\n");
4021                 return -EPROTO;
4022         }
4023
4024         for_each_cea_db(cea, i, start, end) {
4025                 const u8 *db = &cea[i];
4026
4027                 if (cea_db_tag(db) == SPEAKER_BLOCK) {
4028                         dbl = cea_db_payload_len(db);
4029
4030                         /* Speaker Allocation Data Block */
4031                         if (dbl == 3) {
4032                                 *sadb = kmemdup(&db[1], dbl, GFP_KERNEL);
4033                                 if (!*sadb)
4034                                         return -ENOMEM;
4035                                 count = dbl;
4036                                 break;
4037                         }
4038                 }
4039         }
4040
4041         return count;
4042 }
4043 EXPORT_SYMBOL(drm_edid_to_speaker_allocation);
4044
4045 /**
4046  * drm_av_sync_delay - compute the HDMI/DP sink audio-video sync delay
4047  * @connector: connector associated with the HDMI/DP sink
4048  * @mode: the display mode
4049  *
4050  * Return: The HDMI/DP sink's audio-video sync delay in milliseconds or 0 if
4051  * the sink doesn't support audio or video.
4052  */
4053 int drm_av_sync_delay(struct drm_connector *connector,
4054                       const struct drm_display_mode *mode)
4055 {
4056         int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
4057         int a, v;
4058
4059         if (!connector->latency_present[0])
4060                 return 0;
4061         if (!connector->latency_present[1])
4062                 i = 0;
4063
4064         a = connector->audio_latency[i];
4065         v = connector->video_latency[i];
4066
4067         /*
4068          * HDMI/DP sink doesn't support audio or video?
4069          */
4070         if (a == 255 || v == 255)
4071                 return 0;
4072
4073         /*
4074          * Convert raw EDID values to millisecond.
4075          * Treat unknown latency as 0ms.
4076          */
4077         if (a)
4078                 a = min(2 * (a - 1), 500);
4079         if (v)
4080                 v = min(2 * (v - 1), 500);
4081
4082         return max(v - a, 0);
4083 }
4084 EXPORT_SYMBOL(drm_av_sync_delay);
4085
4086 /**
4087  * drm_detect_hdmi_monitor - detect whether monitor is HDMI
4088  * @edid: monitor EDID information
4089  *
4090  * Parse the CEA extension according to CEA-861-B.
4091  *
4092  * Return: True if the monitor is HDMI, false if not or unknown.
4093  */
4094 bool drm_detect_hdmi_monitor(struct edid *edid)
4095 {
4096         u8 *edid_ext;
4097         int i;
4098         int start_offset, end_offset;
4099
4100         edid_ext = drm_find_cea_extension(edid);
4101         if (!edid_ext)
4102                 return false;
4103
4104         if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
4105                 return false;
4106
4107         /*
4108          * Because HDMI identifier is in Vendor Specific Block,
4109          * search it from all data blocks of CEA extension.
4110          */
4111         for_each_cea_db(edid_ext, i, start_offset, end_offset) {
4112                 if (cea_db_is_hdmi_vsdb(&edid_ext[i]))
4113                         return true;
4114         }
4115
4116         return false;
4117 }
4118 EXPORT_SYMBOL(drm_detect_hdmi_monitor);
4119
4120 /**
4121  * drm_detect_monitor_audio - check monitor audio capability
4122  * @edid: EDID block to scan
4123  *
4124  * Monitor should have CEA extension block.
4125  * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
4126  * audio' only. If there is any audio extension block and supported
4127  * audio format, assume at least 'basic audio' support, even if 'basic
4128  * audio' is not defined in EDID.
4129  *
4130  * Return: True if the monitor supports audio, false otherwise.
4131  */
4132 bool drm_detect_monitor_audio(struct edid *edid)
4133 {
4134         u8 *edid_ext;
4135         int i, j;
4136         bool has_audio = false;
4137         int start_offset, end_offset;
4138
4139         edid_ext = drm_find_cea_extension(edid);
4140         if (!edid_ext)
4141                 goto end;
4142
4143         has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
4144
4145         if (has_audio) {
4146                 DRM_DEBUG_KMS("Monitor has basic audio support\n");
4147                 goto end;
4148         }
4149
4150         if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
4151                 goto end;
4152
4153         for_each_cea_db(edid_ext, i, start_offset, end_offset) {
4154                 if (cea_db_tag(&edid_ext[i]) == AUDIO_BLOCK) {
4155                         has_audio = true;
4156                         for (j = 1; j < cea_db_payload_len(&edid_ext[i]) + 1; j += 3)
4157                                 DRM_DEBUG_KMS("CEA audio format %d\n",
4158                                               (edid_ext[i + j] >> 3) & 0xf);
4159                         goto end;
4160                 }
4161         }
4162 end:
4163         return has_audio;
4164 }
4165 EXPORT_SYMBOL(drm_detect_monitor_audio);
4166
4167 /**
4168  * drm_rgb_quant_range_selectable - is RGB quantization range selectable?
4169  * @edid: EDID block to scan
4170  *
4171  * Check whether the monitor reports the RGB quantization range selection
4172  * as supported. The AVI infoframe can then be used to inform the monitor
4173  * which quantization range (full or limited) is used.
4174  *
4175  * Return: True if the RGB quantization range is selectable, false otherwise.
4176  */
4177 bool drm_rgb_quant_range_selectable(struct edid *edid)
4178 {
4179         u8 *edid_ext;
4180         int i, start, end;
4181
4182         edid_ext = drm_find_cea_extension(edid);
4183         if (!edid_ext)
4184                 return false;
4185
4186         if (cea_db_offsets(edid_ext, &start, &end))
4187                 return false;
4188
4189         for_each_cea_db(edid_ext, i, start, end) {
4190                 if (cea_db_tag(&edid_ext[i]) == USE_EXTENDED_TAG &&
4191                     cea_db_payload_len(&edid_ext[i]) == 2 &&
4192                     cea_db_extended_tag(&edid_ext[i]) ==
4193                         EXT_VIDEO_CAPABILITY_BLOCK) {
4194                         DRM_DEBUG_KMS("CEA VCDB 0x%02x\n", edid_ext[i + 2]);
4195                         return edid_ext[i + 2] & EDID_CEA_VCDB_QS;
4196                 }
4197         }
4198
4199         return false;
4200 }
4201 EXPORT_SYMBOL(drm_rgb_quant_range_selectable);
4202
4203 /**
4204  * drm_default_rgb_quant_range - default RGB quantization range
4205  * @mode: display mode
4206  *
4207  * Determine the default RGB quantization range for the mode,
4208  * as specified in CEA-861.
4209  *
4210  * Return: The default RGB quantization range for the mode
4211  */
4212 enum hdmi_quantization_range
4213 drm_default_rgb_quant_range(const struct drm_display_mode *mode)
4214 {
4215         /* All CEA modes other than VIC 1 use limited quantization range. */
4216         return drm_match_cea_mode(mode) > 1 ?
4217                 HDMI_QUANTIZATION_RANGE_LIMITED :
4218                 HDMI_QUANTIZATION_RANGE_FULL;
4219 }
4220 EXPORT_SYMBOL(drm_default_rgb_quant_range);
4221
4222 static void drm_parse_ycbcr420_deep_color_info(struct drm_connector *connector,
4223                                                const u8 *db)
4224 {
4225         u8 dc_mask;
4226         struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
4227
4228         dc_mask = db[7] & DRM_EDID_YCBCR420_DC_MASK;
4229         hdmi->y420_dc_modes |= dc_mask;
4230 }
4231
4232 static void drm_parse_hdmi_forum_vsdb(struct drm_connector *connector,
4233                                  const u8 *hf_vsdb)
4234 {
4235         struct drm_display_info *display = &connector->display_info;
4236         struct drm_hdmi_info *hdmi = &display->hdmi;
4237
4238         if (hf_vsdb[6] & 0x80) {
4239                 hdmi->scdc.supported = true;
4240                 if (hf_vsdb[6] & 0x40)
4241                         hdmi->scdc.read_request = true;
4242         }
4243
4244         /*
4245          * All HDMI 2.0 monitors must support scrambling at rates > 340 MHz.
4246          * And as per the spec, three factors confirm this:
4247          * * Availability of a HF-VSDB block in EDID (check)
4248          * * Non zero Max_TMDS_Char_Rate filed in HF-VSDB (let's check)
4249          * * SCDC support available (let's check)
4250          * Lets check it out.
4251          */
4252
4253         if (hf_vsdb[5]) {
4254                 /* max clock is 5000 KHz times block value */
4255                 u32 max_tmds_clock = hf_vsdb[5] * 5000;
4256                 struct drm_scdc *scdc = &hdmi->scdc;
4257
4258                 if (max_tmds_clock > 340000) {
4259                         display->max_tmds_clock = max_tmds_clock;
4260                         DRM_DEBUG_KMS("HF-VSDB: max TMDS clock %d kHz\n",
4261                                 display->max_tmds_clock);
4262                 }
4263
4264                 if (scdc->supported) {
4265                         scdc->scrambling.supported = true;
4266
4267                         /* Few sinks support scrambling for cloks < 340M */
4268                         if ((hf_vsdb[6] & 0x8))
4269                                 scdc->scrambling.low_rates = true;
4270                 }
4271         }
4272
4273         drm_parse_ycbcr420_deep_color_info(connector, hf_vsdb);
4274 }
4275
4276 static void drm_parse_hdmi_deep_color_info(struct drm_connector *connector,
4277                                            const u8 *hdmi)
4278 {
4279         struct drm_display_info *info = &connector->display_info;
4280         unsigned int dc_bpc = 0;
4281
4282         /* HDMI supports at least 8 bpc */
4283         info->bpc = 8;
4284
4285         if (cea_db_payload_len(hdmi) < 6)
4286                 return;
4287
4288         if (hdmi[6] & DRM_EDID_HDMI_DC_30) {
4289                 dc_bpc = 10;
4290                 info->edid_hdmi_dc_modes |= DRM_EDID_HDMI_DC_30;
4291                 DRM_DEBUG("%s: HDMI sink does deep color 30.\n",
4292                           connector->name);
4293         }
4294
4295         if (hdmi[6] & DRM_EDID_HDMI_DC_36) {
4296                 dc_bpc = 12;
4297                 info->edid_hdmi_dc_modes |= DRM_EDID_HDMI_DC_36;
4298                 DRM_DEBUG("%s: HDMI sink does deep color 36.\n",
4299                           connector->name);
4300         }
4301
4302         if (hdmi[6] & DRM_EDID_HDMI_DC_48) {
4303                 dc_bpc = 16;
4304                 info->edid_hdmi_dc_modes |= DRM_EDID_HDMI_DC_48;
4305                 DRM_DEBUG("%s: HDMI sink does deep color 48.\n",
4306                           connector->name);
4307         }
4308
4309         if (dc_bpc == 0) {
4310                 DRM_DEBUG("%s: No deep color support on this HDMI sink.\n",
4311                           connector->name);
4312                 return;
4313         }
4314
4315         DRM_DEBUG("%s: Assigning HDMI sink color depth as %d bpc.\n",
4316                   connector->name, dc_bpc);
4317         info->bpc = dc_bpc;
4318
4319         /*
4320          * Deep color support mandates RGB444 support for all video
4321          * modes and forbids YCRCB422 support for all video modes per
4322          * HDMI 1.3 spec.
4323          */
4324         info->color_formats = DRM_COLOR_FORMAT_RGB444;
4325
4326         /* YCRCB444 is optional according to spec. */
4327         if (hdmi[6] & DRM_EDID_HDMI_DC_Y444) {
4328                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
4329                 DRM_DEBUG("%s: HDMI sink does YCRCB444 in deep color.\n",
4330                           connector->name);
4331         }
4332
4333         /*
4334          * Spec says that if any deep color mode is supported at all,
4335          * then deep color 36 bit must be supported.
4336          */
4337         if (!(hdmi[6] & DRM_EDID_HDMI_DC_36)) {
4338                 DRM_DEBUG("%s: HDMI sink should do DC_36, but does not!\n",
4339                           connector->name);
4340         }
4341 }
4342
4343 static void
4344 drm_parse_hdmi_vsdb_video(struct drm_connector *connector, const u8 *db)
4345 {
4346         struct drm_display_info *info = &connector->display_info;
4347         u8 len = cea_db_payload_len(db);
4348
4349         if (len >= 6)
4350                 info->dvi_dual = db[6] & 1;
4351         if (len >= 7)
4352                 info->max_tmds_clock = db[7] * 5000;
4353
4354         DRM_DEBUG_KMS("HDMI: DVI dual %d, "
4355                       "max TMDS clock %d kHz\n",
4356                       info->dvi_dual,
4357                       info->max_tmds_clock);
4358
4359         drm_parse_hdmi_deep_color_info(connector, db);
4360 }
4361
4362 static void drm_parse_cea_ext(struct drm_connector *connector,
4363                               struct edid *edid)
4364 {
4365         struct drm_display_info *info = &connector->display_info;
4366         const u8 *edid_ext;
4367         int i, start, end;
4368
4369         edid_ext = drm_find_cea_extension(edid);
4370         if (!edid_ext)
4371                 return;
4372
4373         info->cea_rev = edid_ext[1];
4374
4375         /* The existence of a CEA block should imply RGB support */
4376         info->color_formats = DRM_COLOR_FORMAT_RGB444;
4377         if (edid_ext[3] & EDID_CEA_YCRCB444)
4378                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
4379         if (edid_ext[3] & EDID_CEA_YCRCB422)
4380                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
4381
4382         if (cea_db_offsets(edid_ext, &start, &end))
4383                 return;
4384
4385         for_each_cea_db(edid_ext, i, start, end) {
4386                 const u8 *db = &edid_ext[i];
4387
4388                 if (cea_db_is_hdmi_vsdb(db))
4389                         drm_parse_hdmi_vsdb_video(connector, db);
4390                 if (cea_db_is_hdmi_forum_vsdb(db))
4391                         drm_parse_hdmi_forum_vsdb(connector, db);
4392                 if (cea_db_is_y420cmdb(db))
4393                         drm_parse_y420cmdb_bitmap(connector, db);
4394         }
4395 }
4396
4397 static void drm_add_display_info(struct drm_connector *connector,
4398                                  struct edid *edid, u32 quirks)
4399 {
4400         struct drm_display_info *info = &connector->display_info;
4401
4402         info->width_mm = edid->width_cm * 10;
4403         info->height_mm = edid->height_cm * 10;
4404
4405         /* driver figures it out in this case */
4406         info->bpc = 0;
4407         info->color_formats = 0;
4408         info->cea_rev = 0;
4409         info->max_tmds_clock = 0;
4410         info->dvi_dual = false;
4411
4412         info->non_desktop = !!(quirks & EDID_QUIRK_NON_DESKTOP);
4413
4414         if (edid->revision < 3)
4415                 return;
4416
4417         if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
4418                 return;
4419
4420         drm_parse_cea_ext(connector, edid);
4421
4422         /*
4423          * Digital sink with "DFP 1.x compliant TMDS" according to EDID 1.3?
4424          *
4425          * For such displays, the DFP spec 1.0, section 3.10 "EDID support"
4426          * tells us to assume 8 bpc color depth if the EDID doesn't have
4427          * extensions which tell otherwise.
4428          */
4429         if ((info->bpc == 0) && (edid->revision < 4) &&
4430             (edid->input & DRM_EDID_DIGITAL_TYPE_DVI)) {
4431                 info->bpc = 8;
4432                 DRM_DEBUG("%s: Assigning DFP sink color depth as %d bpc.\n",
4433                           connector->name, info->bpc);
4434         }
4435
4436         /* Only defined for 1.4 with digital displays */
4437         if (edid->revision < 4)
4438                 return;
4439
4440         switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
4441         case DRM_EDID_DIGITAL_DEPTH_6:
4442                 info->bpc = 6;
4443                 break;
4444         case DRM_EDID_DIGITAL_DEPTH_8:
4445                 info->bpc = 8;
4446                 break;
4447         case DRM_EDID_DIGITAL_DEPTH_10:
4448                 info->bpc = 10;
4449                 break;
4450         case DRM_EDID_DIGITAL_DEPTH_12:
4451                 info->bpc = 12;
4452                 break;
4453         case DRM_EDID_DIGITAL_DEPTH_14:
4454                 info->bpc = 14;
4455                 break;
4456         case DRM_EDID_DIGITAL_DEPTH_16:
4457                 info->bpc = 16;
4458                 break;
4459         case DRM_EDID_DIGITAL_DEPTH_UNDEF:
4460         default:
4461                 info->bpc = 0;
4462                 break;
4463         }
4464
4465         DRM_DEBUG("%s: Assigning EDID-1.4 digital sink color depth as %d bpc.\n",
4466                           connector->name, info->bpc);
4467
4468         info->color_formats |= DRM_COLOR_FORMAT_RGB444;
4469         if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
4470                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
4471         if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
4472                 info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
4473 }
4474
4475 static int validate_displayid(u8 *displayid, int length, int idx)
4476 {
4477         int i;
4478         u8 csum = 0;
4479         struct displayid_hdr *base;
4480
4481         base = (struct displayid_hdr *)&displayid[idx];
4482
4483         DRM_DEBUG_KMS("base revision 0x%x, length %d, %d %d\n",
4484                       base->rev, base->bytes, base->prod_id, base->ext_count);
4485
4486         if (base->bytes + 5 > length - idx)
4487                 return -EINVAL;
4488         for (i = idx; i <= base->bytes + 5; i++) {
4489                 csum += displayid[i];
4490         }
4491         if (csum) {
4492                 DRM_NOTE("DisplayID checksum invalid, remainder is %d\n", csum);
4493                 return -EINVAL;
4494         }
4495         return 0;
4496 }
4497
4498 static struct drm_display_mode *drm_mode_displayid_detailed(struct drm_device *dev,
4499                                                             struct displayid_detailed_timings_1 *timings)
4500 {
4501         struct drm_display_mode *mode;
4502         unsigned pixel_clock = (timings->pixel_clock[0] |
4503                                 (timings->pixel_clock[1] << 8) |
4504                                 (timings->pixel_clock[2] << 16));
4505         unsigned hactive = (timings->hactive[0] | timings->hactive[1] << 8) + 1;
4506         unsigned hblank = (timings->hblank[0] | timings->hblank[1] << 8) + 1;
4507         unsigned hsync = (timings->hsync[0] | (timings->hsync[1] & 0x7f) << 8) + 1;
4508         unsigned hsync_width = (timings->hsw[0] | timings->hsw[1] << 8) + 1;
4509         unsigned vactive = (timings->vactive[0] | timings->vactive[1] << 8) + 1;
4510         unsigned vblank = (timings->vblank[0] | timings->vblank[1] << 8) + 1;
4511         unsigned vsync = (timings->vsync[0] | (timings->vsync[1] & 0x7f) << 8) + 1;
4512         unsigned vsync_width = (timings->vsw[0] | timings->vsw[1] << 8) + 1;
4513         bool hsync_positive = (timings->hsync[1] >> 7) & 0x1;
4514         bool vsync_positive = (timings->vsync[1] >> 7) & 0x1;
4515         mode = drm_mode_create(dev);
4516         if (!mode)
4517                 return NULL;
4518
4519         mode->clock = pixel_clock * 10;
4520         mode->hdisplay = hactive;
4521         mode->hsync_start = mode->hdisplay + hsync;
4522         mode->hsync_end = mode->hsync_start + hsync_width;
4523         mode->htotal = mode->hdisplay + hblank;
4524
4525         mode->vdisplay = vactive;
4526         mode->vsync_start = mode->vdisplay + vsync;
4527         mode->vsync_end = mode->vsync_start + vsync_width;
4528         mode->vtotal = mode->vdisplay + vblank;
4529
4530         mode->flags = 0;
4531         mode->flags |= hsync_positive ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
4532         mode->flags |= vsync_positive ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
4533         mode->type = DRM_MODE_TYPE_DRIVER;
4534
4535         if (timings->flags & 0x80)
4536                 mode->type |= DRM_MODE_TYPE_PREFERRED;
4537         mode->vrefresh = drm_mode_vrefresh(mode);
4538         drm_mode_set_name(mode);
4539
4540         return mode;
4541 }
4542
4543 static int add_displayid_detailed_1_modes(struct drm_connector *connector,
4544                                           struct displayid_block *block)
4545 {
4546         struct displayid_detailed_timing_block *det = (struct displayid_detailed_timing_block *)block;
4547         int i;
4548         int num_timings;
4549         struct drm_display_mode *newmode;
4550         int num_modes = 0;
4551         /* blocks must be multiple of 20 bytes length */
4552         if (block->num_bytes % 20)
4553                 return 0;
4554
4555         num_timings = block->num_bytes / 20;
4556         for (i = 0; i < num_timings; i++) {
4557                 struct displayid_detailed_timings_1 *timings = &det->timings[i];
4558
4559                 newmode = drm_mode_displayid_detailed(connector->dev, timings);
4560                 if (!newmode)
4561                         continue;
4562
4563                 drm_mode_probed_add(connector, newmode);
4564                 num_modes++;
4565         }
4566         return num_modes;
4567 }
4568
4569 static int add_displayid_detailed_modes(struct drm_connector *connector,
4570                                         struct edid *edid)
4571 {
4572         u8 *displayid;
4573         int ret;
4574         int idx = 1;
4575         int length = EDID_LENGTH;
4576         struct displayid_block *block;
4577         int num_modes = 0;
4578
4579         displayid = drm_find_displayid_extension(edid);
4580         if (!displayid)
4581                 return 0;
4582
4583         ret = validate_displayid(displayid, length, idx);
4584         if (ret)
4585                 return 0;
4586
4587         idx += sizeof(struct displayid_hdr);
4588         while (block = (struct displayid_block *)&displayid[idx],
4589                idx + sizeof(struct displayid_block) <= length &&
4590                idx + sizeof(struct displayid_block) + block->num_bytes <= length &&
4591                block->num_bytes > 0) {
4592                 idx += block->num_bytes + sizeof(struct displayid_block);
4593                 switch (block->tag) {
4594                 case DATA_BLOCK_TYPE_1_DETAILED_TIMING:
4595                         num_modes += add_displayid_detailed_1_modes(connector, block);
4596                         break;
4597                 }
4598         }
4599         return num_modes;
4600 }
4601
4602 /**
4603  * drm_add_edid_modes - add modes from EDID data, if available
4604  * @connector: connector we're probing
4605  * @edid: EDID data
4606  *
4607  * Add the specified modes to the connector's mode list. Also fills out the
4608  * &drm_display_info structure in @connector with any information which can be
4609  * derived from the edid.
4610  *
4611  * Return: The number of modes added or 0 if we couldn't find any.
4612  */
4613 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
4614 {
4615         int num_modes = 0;
4616         u32 quirks;
4617
4618         if (edid == NULL) {
4619                 return 0;
4620         }
4621         if (!drm_edid_is_valid(edid)) {
4622                 dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
4623                          connector->name);
4624                 return 0;
4625         }
4626
4627         quirks = edid_get_quirks(edid);
4628
4629         /*
4630          * CEA-861-F adds ycbcr capability map block, for HDMI 2.0 sinks.
4631          * To avoid multiple parsing of same block, lets parse that map
4632          * from sink info, before parsing CEA modes.
4633          */
4634         drm_add_display_info(connector, edid, quirks);
4635
4636         /*
4637          * EDID spec says modes should be preferred in this order:
4638          * - preferred detailed mode
4639          * - other detailed modes from base block
4640          * - detailed modes from extension blocks
4641          * - CVT 3-byte code modes
4642          * - standard timing codes
4643          * - established timing codes
4644          * - modes inferred from GTF or CVT range information
4645          *
4646          * We get this pretty much right.
4647          *
4648          * XXX order for additional mode types in extension blocks?
4649          */
4650         num_modes += add_detailed_modes(connector, edid, quirks);
4651         num_modes += add_cvt_modes(connector, edid);
4652         num_modes += add_standard_modes(connector, edid);
4653         num_modes += add_established_modes(connector, edid);
4654         num_modes += add_cea_modes(connector, edid);
4655         num_modes += add_alternate_cea_modes(connector, edid);
4656         num_modes += add_displayid_detailed_modes(connector, edid);
4657         if (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF)
4658                 num_modes += add_inferred_modes(connector, edid);
4659
4660         if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
4661                 edid_fixup_preferred(connector, quirks);
4662
4663         if (quirks & EDID_QUIRK_FORCE_6BPC)
4664                 connector->display_info.bpc = 6;
4665
4666         if (quirks & EDID_QUIRK_FORCE_8BPC)
4667                 connector->display_info.bpc = 8;
4668
4669         if (quirks & EDID_QUIRK_FORCE_10BPC)
4670                 connector->display_info.bpc = 10;
4671
4672         if (quirks & EDID_QUIRK_FORCE_12BPC)
4673                 connector->display_info.bpc = 12;
4674
4675         return num_modes;
4676 }
4677 EXPORT_SYMBOL(drm_add_edid_modes);
4678
4679 /**
4680  * drm_add_modes_noedid - add modes for the connectors without EDID
4681  * @connector: connector we're probing
4682  * @hdisplay: the horizontal display limit
4683  * @vdisplay: the vertical display limit
4684  *
4685  * Add the specified modes to the connector's mode list. Only when the
4686  * hdisplay/vdisplay is not beyond the given limit, it will be added.
4687  *
4688  * Return: The number of modes added or 0 if we couldn't find any.
4689  */
4690 int drm_add_modes_noedid(struct drm_connector *connector,
4691                         int hdisplay, int vdisplay)
4692 {
4693         int i, count, num_modes = 0;
4694         struct drm_display_mode *mode;
4695         struct drm_device *dev = connector->dev;
4696
4697         count = ARRAY_SIZE(drm_dmt_modes);
4698         if (hdisplay < 0)
4699                 hdisplay = 0;
4700         if (vdisplay < 0)
4701                 vdisplay = 0;
4702
4703         for (i = 0; i < count; i++) {
4704                 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
4705                 if (hdisplay && vdisplay) {
4706                         /*
4707                          * Only when two are valid, they will be used to check
4708                          * whether the mode should be added to the mode list of
4709                          * the connector.
4710                          */
4711                         if (ptr->hdisplay > hdisplay ||
4712                                         ptr->vdisplay > vdisplay)
4713                                 continue;
4714                 }
4715                 if (drm_mode_vrefresh(ptr) > 61)
4716                         continue;
4717                 mode = drm_mode_duplicate(dev, ptr);
4718                 if (mode) {
4719                         drm_mode_probed_add(connector, mode);
4720                         num_modes++;
4721                 }
4722         }
4723         return num_modes;
4724 }
4725 EXPORT_SYMBOL(drm_add_modes_noedid);
4726
4727 /**
4728  * drm_set_preferred_mode - Sets the preferred mode of a connector
4729  * @connector: connector whose mode list should be processed
4730  * @hpref: horizontal resolution of preferred mode
4731  * @vpref: vertical resolution of preferred mode
4732  *
4733  * Marks a mode as preferred if it matches the resolution specified by @hpref
4734  * and @vpref.
4735  */
4736 void drm_set_preferred_mode(struct drm_connector *connector,
4737                            int hpref, int vpref)
4738 {
4739         struct drm_display_mode *mode;
4740
4741         list_for_each_entry(mode, &connector->probed_modes, head) {
4742                 if (mode->hdisplay == hpref &&
4743                     mode->vdisplay == vpref)
4744                         mode->type |= DRM_MODE_TYPE_PREFERRED;
4745         }
4746 }
4747 EXPORT_SYMBOL(drm_set_preferred_mode);
4748
4749 /**
4750  * drm_hdmi_avi_infoframe_from_display_mode() - fill an HDMI AVI infoframe with
4751  *                                              data from a DRM display mode
4752  * @frame: HDMI AVI infoframe
4753  * @mode: DRM display mode
4754  * @is_hdmi2_sink: Sink is HDMI 2.0 compliant
4755  *
4756  * Return: 0 on success or a negative error code on failure.
4757  */
4758 int
4759 drm_hdmi_avi_infoframe_from_display_mode(struct hdmi_avi_infoframe *frame,
4760                                          const struct drm_display_mode *mode,
4761                                          bool is_hdmi2_sink)
4762 {
4763         int err;
4764
4765         if (!frame || !mode)
4766                 return -EINVAL;
4767
4768         err = hdmi_avi_infoframe_init(frame);
4769         if (err < 0)
4770                 return err;
4771
4772         if (mode->flags & DRM_MODE_FLAG_DBLCLK)
4773                 frame->pixel_repeat = 1;
4774
4775         frame->video_code = drm_match_cea_mode(mode);
4776
4777         /*
4778          * HDMI 1.4 VIC range: 1 <= VIC <= 64 (CEA-861-D) but
4779          * HDMI 2.0 VIC range: 1 <= VIC <= 107 (CEA-861-F). So we
4780          * have to make sure we dont break HDMI 1.4 sinks.
4781          */
4782         if (!is_hdmi2_sink && frame->video_code > 64)
4783                 frame->video_code = 0;
4784
4785         /*
4786          * HDMI spec says if a mode is found in HDMI 1.4b 4K modes
4787          * we should send its VIC in vendor infoframes, else send the
4788          * VIC in AVI infoframes. Lets check if this mode is present in
4789          * HDMI 1.4b 4K modes
4790          */
4791         if (frame->video_code) {
4792                 u8 vendor_if_vic = drm_match_hdmi_mode(mode);
4793                 bool is_s3d = mode->flags & DRM_MODE_FLAG_3D_MASK;
4794
4795                 if (drm_valid_hdmi_vic(vendor_if_vic) && !is_s3d)
4796                         frame->video_code = 0;
4797         }
4798
4799         frame->picture_aspect = HDMI_PICTURE_ASPECT_NONE;
4800
4801         /*
4802          * Populate picture aspect ratio from either
4803          * user input (if specified) or from the CEA mode list.
4804          */
4805         if (mode->picture_aspect_ratio == HDMI_PICTURE_ASPECT_4_3 ||
4806                 mode->picture_aspect_ratio == HDMI_PICTURE_ASPECT_16_9)
4807                 frame->picture_aspect = mode->picture_aspect_ratio;
4808         else if (frame->video_code > 0)
4809                 frame->picture_aspect = drm_get_cea_aspect_ratio(
4810                                                 frame->video_code);
4811
4812         frame->active_aspect = HDMI_ACTIVE_ASPECT_PICTURE;
4813         frame->scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
4814
4815         return 0;
4816 }
4817 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_from_display_mode);
4818
4819 /**
4820  * drm_hdmi_avi_infoframe_quant_range() - fill the HDMI AVI infoframe
4821  *                                        quantization range information
4822  * @frame: HDMI AVI infoframe
4823  * @mode: DRM display mode
4824  * @rgb_quant_range: RGB quantization range (Q)
4825  * @rgb_quant_range_selectable: Sink support selectable RGB quantization range (QS)
4826  */
4827 void
4828 drm_hdmi_avi_infoframe_quant_range(struct hdmi_avi_infoframe *frame,
4829                                    const struct drm_display_mode *mode,
4830                                    enum hdmi_quantization_range rgb_quant_range,
4831                                    bool rgb_quant_range_selectable)
4832 {
4833         /*
4834          * CEA-861:
4835          * "A Source shall not send a non-zero Q value that does not correspond
4836          *  to the default RGB Quantization Range for the transmitted Picture
4837          *  unless the Sink indicates support for the Q bit in a Video
4838          *  Capabilities Data Block."
4839          *
4840          * HDMI 2.0 recommends sending non-zero Q when it does match the
4841          * default RGB quantization range for the mode, even when QS=0.
4842          */
4843         if (rgb_quant_range_selectable ||
4844             rgb_quant_range == drm_default_rgb_quant_range(mode))
4845                 frame->quantization_range = rgb_quant_range;
4846         else
4847                 frame->quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
4848
4849         /*
4850          * CEA-861-F:
4851          * "When transmitting any RGB colorimetry, the Source should set the
4852          *  YQ-field to match the RGB Quantization Range being transmitted
4853          *  (e.g., when Limited Range RGB, set YQ=0 or when Full Range RGB,
4854          *  set YQ=1) and the Sink shall ignore the YQ-field."
4855          */
4856         if (rgb_quant_range == HDMI_QUANTIZATION_RANGE_LIMITED)
4857                 frame->ycc_quantization_range =
4858                         HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
4859         else
4860                 frame->ycc_quantization_range =
4861                         HDMI_YCC_QUANTIZATION_RANGE_FULL;
4862 }
4863 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_quant_range);
4864
4865 static enum hdmi_3d_structure
4866 s3d_structure_from_display_mode(const struct drm_display_mode *mode)
4867 {
4868         u32 layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
4869
4870         switch (layout) {
4871         case DRM_MODE_FLAG_3D_FRAME_PACKING:
4872                 return HDMI_3D_STRUCTURE_FRAME_PACKING;
4873         case DRM_MODE_FLAG_3D_FIELD_ALTERNATIVE:
4874                 return HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE;
4875         case DRM_MODE_FLAG_3D_LINE_ALTERNATIVE:
4876                 return HDMI_3D_STRUCTURE_LINE_ALTERNATIVE;
4877         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_FULL:
4878                 return HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL;
4879         case DRM_MODE_FLAG_3D_L_DEPTH:
4880                 return HDMI_3D_STRUCTURE_L_DEPTH;
4881         case DRM_MODE_FLAG_3D_L_DEPTH_GFX_GFX_DEPTH:
4882                 return HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH;
4883         case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
4884                 return HDMI_3D_STRUCTURE_TOP_AND_BOTTOM;
4885         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
4886                 return HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF;
4887         default:
4888                 return HDMI_3D_STRUCTURE_INVALID;
4889         }
4890 }
4891
4892 /**
4893  * drm_hdmi_vendor_infoframe_from_display_mode() - fill an HDMI infoframe with
4894  * data from a DRM display mode
4895  * @frame: HDMI vendor infoframe
4896  * @mode: DRM display mode
4897  *
4898  * Note that there's is a need to send HDMI vendor infoframes only when using a
4899  * 4k or stereoscopic 3D mode. So when giving any other mode as input this
4900  * function will return -EINVAL, error that can be safely ignored.
4901  *
4902  * Return: 0 on success or a negative error code on failure.
4903  */
4904 int
4905 drm_hdmi_vendor_infoframe_from_display_mode(struct hdmi_vendor_infoframe *frame,
4906                                             const struct drm_display_mode *mode)
4907 {
4908         int err;
4909         u32 s3d_flags;
4910         u8 vic;
4911
4912         if (!frame || !mode)
4913                 return -EINVAL;
4914
4915         vic = drm_match_hdmi_mode(mode);
4916         s3d_flags = mode->flags & DRM_MODE_FLAG_3D_MASK;
4917
4918         if (!vic && !s3d_flags)
4919                 return -EINVAL;
4920
4921         if (vic && s3d_flags)
4922                 return -EINVAL;
4923
4924         err = hdmi_vendor_infoframe_init(frame);
4925         if (err < 0)
4926                 return err;
4927
4928         if (vic)
4929                 frame->vic = vic;
4930         else
4931                 frame->s3d_struct = s3d_structure_from_display_mode(mode);
4932
4933         return 0;
4934 }
4935 EXPORT_SYMBOL(drm_hdmi_vendor_infoframe_from_display_mode);
4936
4937 static int drm_parse_tiled_block(struct drm_connector *connector,
4938                                  struct displayid_block *block)
4939 {
4940         struct displayid_tiled_block *tile = (struct displayid_tiled_block *)block;
4941         u16 w, h;
4942         u8 tile_v_loc, tile_h_loc;
4943         u8 num_v_tile, num_h_tile;
4944         struct drm_tile_group *tg;
4945
4946         w = tile->tile_size[0] | tile->tile_size[1] << 8;
4947         h = tile->tile_size[2] | tile->tile_size[3] << 8;
4948
4949         num_v_tile = (tile->topo[0] & 0xf) | (tile->topo[2] & 0x30);
4950         num_h_tile = (tile->topo[0] >> 4) | ((tile->topo[2] >> 2) & 0x30);
4951         tile_v_loc = (tile->topo[1] & 0xf) | ((tile->topo[2] & 0x3) << 4);
4952         tile_h_loc = (tile->topo[1] >> 4) | (((tile->topo[2] >> 2) & 0x3) << 4);
4953
4954         connector->has_tile = true;
4955         if (tile->tile_cap & 0x80)
4956                 connector->tile_is_single_monitor = true;
4957
4958         connector->num_h_tile = num_h_tile + 1;
4959         connector->num_v_tile = num_v_tile + 1;
4960         connector->tile_h_loc = tile_h_loc;
4961         connector->tile_v_loc = tile_v_loc;
4962         connector->tile_h_size = w + 1;
4963         connector->tile_v_size = h + 1;
4964
4965         DRM_DEBUG_KMS("tile cap 0x%x\n", tile->tile_cap);
4966         DRM_DEBUG_KMS("tile_size %d x %d\n", w + 1, h + 1);
4967         DRM_DEBUG_KMS("topo num tiles %dx%d, location %dx%d\n",
4968                       num_h_tile + 1, num_v_tile + 1, tile_h_loc, tile_v_loc);
4969         DRM_DEBUG_KMS("vend %c%c%c\n", tile->topology_id[0], tile->topology_id[1], tile->topology_id[2]);
4970
4971         tg = drm_mode_get_tile_group(connector->dev, tile->topology_id);
4972         if (!tg) {
4973                 tg = drm_mode_create_tile_group(connector->dev, tile->topology_id);
4974         }
4975         if (!tg)
4976                 return -ENOMEM;
4977
4978         if (connector->tile_group != tg) {
4979                 /* if we haven't got a pointer,
4980                    take the reference, drop ref to old tile group */
4981                 if (connector->tile_group) {
4982                         drm_mode_put_tile_group(connector->dev, connector->tile_group);
4983                 }
4984                 connector->tile_group = tg;
4985         } else
4986                 /* if same tile group, then release the ref we just took. */
4987                 drm_mode_put_tile_group(connector->dev, tg);
4988         return 0;
4989 }
4990
4991 static int drm_parse_display_id(struct drm_connector *connector,
4992                                 u8 *displayid, int length,
4993                                 bool is_edid_extension)
4994 {
4995         /* if this is an EDID extension the first byte will be 0x70 */
4996         int idx = 0;
4997         struct displayid_block *block;
4998         int ret;
4999
5000         if (is_edid_extension)
5001                 idx = 1;
5002
5003         ret = validate_displayid(displayid, length, idx);
5004         if (ret)
5005                 return ret;
5006
5007         idx += sizeof(struct displayid_hdr);
5008         while (block = (struct displayid_block *)&displayid[idx],
5009                idx + sizeof(struct displayid_block) <= length &&
5010                idx + sizeof(struct displayid_block) + block->num_bytes <= length &&
5011                block->num_bytes > 0) {
5012                 idx += block->num_bytes + sizeof(struct displayid_block);
5013                 DRM_DEBUG_KMS("block id 0x%x, rev %d, len %d\n",
5014                               block->tag, block->rev, block->num_bytes);
5015
5016                 switch (block->tag) {
5017                 case DATA_BLOCK_TILED_DISPLAY:
5018                         ret = drm_parse_tiled_block(connector, block);
5019                         if (ret)
5020                                 return ret;
5021                         break;
5022                 case DATA_BLOCK_TYPE_1_DETAILED_TIMING:
5023                         /* handled in mode gathering code. */
5024                         break;
5025                 default:
5026                         DRM_DEBUG_KMS("found DisplayID tag 0x%x, unhandled\n", block->tag);
5027                         break;
5028                 }
5029         }
5030         return 0;
5031 }
5032
5033 static void drm_get_displayid(struct drm_connector *connector,
5034                               struct edid *edid)
5035 {
5036         void *displayid = NULL;
5037         int ret;
5038         connector->has_tile = false;
5039         displayid = drm_find_displayid_extension(edid);
5040         if (!displayid) {
5041                 /* drop reference to any tile group we had */
5042                 goto out_drop_ref;
5043         }
5044
5045         ret = drm_parse_display_id(connector, displayid, EDID_LENGTH, true);
5046         if (ret < 0)
5047                 goto out_drop_ref;
5048         if (!connector->has_tile)
5049                 goto out_drop_ref;
5050         return;
5051 out_drop_ref:
5052         if (connector->tile_group) {
5053                 drm_mode_put_tile_group(connector->dev, connector->tile_group);
5054                 connector->tile_group = NULL;
5055         }
5056         return;
5057 }