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