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