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