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