xref: /linux/drivers/gpu/drm/drm_edid.c (revision fa19199dc93ccf09ff5aaee7014f4596280911f8)
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 7:
6198 		*max_lanes = 4;
6199 		*max_rate_per_lane = 16;
6200 		break;
6201 	case 8:
6202 		*max_lanes = 4;
6203 		*max_rate_per_lane = 20;
6204 		break;
6205 	case 9:
6206 		*max_lanes = 4;
6207 		*max_rate_per_lane = 24;
6208 		break;
6209 	case 0:
6210 	default:
6211 		*max_lanes = 0;
6212 		*max_rate_per_lane = 0;
6213 	}
6214 }
6215 
6216 static void drm_parse_ycbcr420_deep_color_info(struct drm_connector *connector,
6217 					       const u8 *db)
6218 {
6219 	u8 dc_mask;
6220 	struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
6221 
6222 	dc_mask = db[7] & DRM_EDID_YCBCR420_DC_MASK;
6223 	hdmi->y420_dc_modes = dc_mask;
6224 }
6225 
6226 static void drm_parse_hdmi_gaming_info(struct drm_hdmi_info *hdmi, const u8 *db)
6227 {
6228 	struct drm_hdmi_vrr_cap *vrr = &hdmi->vrr_cap;
6229 
6230 	if (cea_db_payload_len(db) < 8)
6231 		return;
6232 
6233 	hdmi->fapa_start_location = db[8] & DRM_EDID_FAPA_START_LOCATION;
6234 	hdmi->allm = db[8] & DRM_EDID_ALLM;
6235 	vrr->fva = db[8] & DRM_EDID_FVA;
6236 	vrr->cnmvrr = db[8] & DRM_EDID_CNMVRR;
6237 	vrr->cinema_vrr = db[8] & DRM_EDID_CINEMA_VRR;
6238 	vrr->mdelta = db[8] & DRM_EDID_MDELTA;
6239 
6240 	if (cea_db_payload_len(db) < 9)
6241 		return;
6242 
6243 	vrr->vrr_min = db[9] & DRM_EDID_VRR_MIN_MASK;
6244 	vrr->supported = (vrr->vrr_min > 0 && vrr->vrr_min <= 48);
6245 
6246 	if (cea_db_payload_len(db) < 10)
6247 		return;
6248 
6249 	vrr->vrr_max = (db[9] & DRM_EDID_VRR_MAX_UPPER_MASK) << 2 | db[10];
6250 	vrr->supported &= (vrr->vrr_max == 0 || vrr->vrr_max >= 100);
6251 }
6252 
6253 static void drm_parse_dsc_info(struct drm_hdmi_dsc_cap *hdmi_dsc,
6254 			       const u8 *hf_scds)
6255 {
6256 	hdmi_dsc->v_1p2 = hf_scds[11] & DRM_EDID_DSC_1P2;
6257 
6258 	if (!hdmi_dsc->v_1p2)
6259 		return;
6260 
6261 	hdmi_dsc->native_420 = hf_scds[11] & DRM_EDID_DSC_NATIVE_420;
6262 	hdmi_dsc->all_bpp = hf_scds[11] & DRM_EDID_DSC_ALL_BPP;
6263 
6264 	if (hf_scds[11] & DRM_EDID_DSC_16BPC)
6265 		hdmi_dsc->bpc_supported = 16;
6266 	else if (hf_scds[11] & DRM_EDID_DSC_12BPC)
6267 		hdmi_dsc->bpc_supported = 12;
6268 	else if (hf_scds[11] & DRM_EDID_DSC_10BPC)
6269 		hdmi_dsc->bpc_supported = 10;
6270 	else
6271 		/* Supports min 8 BPC if DSC 1.2 is supported*/
6272 		hdmi_dsc->bpc_supported = 8;
6273 
6274 	if (cea_db_payload_len(hf_scds) >= 12 && hf_scds[12]) {
6275 		u8 dsc_max_slices;
6276 		u8 dsc_max_frl_rate;
6277 
6278 		dsc_max_frl_rate = (hf_scds[12] & DRM_EDID_DSC_MAX_FRL_RATE_MASK) >> 4;
6279 		drm_get_max_frl_rate(dsc_max_frl_rate, &hdmi_dsc->max_lanes,
6280 				     &hdmi_dsc->max_frl_rate_per_lane);
6281 
6282 		dsc_max_slices = hf_scds[12] & DRM_EDID_DSC_MAX_SLICES;
6283 
6284 		switch (dsc_max_slices) {
6285 		case 1:
6286 			hdmi_dsc->max_slices = 1;
6287 			hdmi_dsc->clk_per_slice = 340;
6288 			break;
6289 		case 2:
6290 			hdmi_dsc->max_slices = 2;
6291 			hdmi_dsc->clk_per_slice = 340;
6292 			break;
6293 		case 3:
6294 			hdmi_dsc->max_slices = 4;
6295 			hdmi_dsc->clk_per_slice = 340;
6296 			break;
6297 		case 4:
6298 			hdmi_dsc->max_slices = 8;
6299 			hdmi_dsc->clk_per_slice = 340;
6300 			break;
6301 		case 5:
6302 			hdmi_dsc->max_slices = 8;
6303 			hdmi_dsc->clk_per_slice = 400;
6304 			break;
6305 		case 6:
6306 			hdmi_dsc->max_slices = 12;
6307 			hdmi_dsc->clk_per_slice = 400;
6308 			break;
6309 		case 7:
6310 			hdmi_dsc->max_slices = 16;
6311 			hdmi_dsc->clk_per_slice = 400;
6312 			break;
6313 		case 0:
6314 		default:
6315 			hdmi_dsc->max_slices = 0;
6316 			hdmi_dsc->clk_per_slice = 0;
6317 		}
6318 	}
6319 
6320 	if (cea_db_payload_len(hf_scds) >= 13 && hf_scds[13])
6321 		hdmi_dsc->total_chunk_kbytes = hf_scds[13] & DRM_EDID_DSC_TOTAL_CHUNK_KBYTES;
6322 }
6323 
6324 /* Sink Capability Data Structure */
6325 static void drm_parse_hdmi_forum_scds(struct drm_connector *connector,
6326 				      const u8 *hf_scds)
6327 {
6328 	struct drm_display_info *info = &connector->display_info;
6329 	struct drm_hdmi_info *hdmi = &info->hdmi;
6330 	struct drm_hdmi_dsc_cap *hdmi_dsc = &hdmi->dsc_cap;
6331 	int max_tmds_clock = 0;
6332 	u8 max_frl_rate = 0;
6333 	bool dsc_support = false;
6334 
6335 	info->has_hdmi_infoframe = true;
6336 
6337 	if (hf_scds[6] & 0x80) {
6338 		hdmi->scdc.supported = true;
6339 		if (hf_scds[6] & 0x40)
6340 			hdmi->scdc.read_request = true;
6341 	}
6342 
6343 	/*
6344 	 * All HDMI 2.0 monitors must support scrambling at rates > 340 MHz.
6345 	 * And as per the spec, three factors confirm this:
6346 	 * * Availability of a HF-VSDB block in EDID (check)
6347 	 * * Non zero Max_TMDS_Char_Rate filed in HF-VSDB (let's check)
6348 	 * * SCDC support available (let's check)
6349 	 * Lets check it out.
6350 	 */
6351 
6352 	if (hf_scds[5]) {
6353 		struct drm_scdc *scdc = &hdmi->scdc;
6354 
6355 		/* max clock is 5000 KHz times block value */
6356 		max_tmds_clock = hf_scds[5] * 5000;
6357 
6358 		if (max_tmds_clock > 340000) {
6359 			info->max_tmds_clock = max_tmds_clock;
6360 		}
6361 
6362 		if (scdc->supported) {
6363 			scdc->scrambling.supported = true;
6364 
6365 			/* Few sinks support scrambling for clocks < 340M */
6366 			if ((hf_scds[6] & 0x8))
6367 				scdc->scrambling.low_rates = true;
6368 		}
6369 	}
6370 
6371 	if (hf_scds[7]) {
6372 		max_frl_rate = (hf_scds[7] & DRM_EDID_MAX_FRL_RATE_MASK) >> 4;
6373 		drm_get_max_frl_rate(max_frl_rate, &hdmi->max_lanes,
6374 				     &hdmi->max_frl_rate_per_lane);
6375 	}
6376 
6377 	drm_parse_ycbcr420_deep_color_info(connector, hf_scds);
6378 
6379 	drm_parse_hdmi_gaming_info(&connector->display_info.hdmi, hf_scds);
6380 
6381 	if (cea_db_payload_len(hf_scds) >= 11 && hf_scds[11]) {
6382 		drm_parse_dsc_info(hdmi_dsc, hf_scds);
6383 		dsc_support = true;
6384 	}
6385 
6386 	drm_dbg_kms(connector->dev,
6387 		    "[CONNECTOR:%d:%s] HF-VSDB: max TMDS clock: %d KHz, HDMI 2.1 support: %s, DSC 1.2 support: %s\n",
6388 		    connector->base.id, connector->name,
6389 		    max_tmds_clock, str_yes_no(max_frl_rate), str_yes_no(dsc_support));
6390 	drm_dbg_kms(connector->dev,
6391 		    "[CONNECTOR:%d:%s] FAPA in blanking: %s, ALLM support: %s, Fast Vactive support: %s\n",
6392 		    connector->base.id, connector->name, str_yes_no(hdmi->fapa_start_location),
6393 		    str_yes_no(hdmi->allm), str_yes_no(hdmi->vrr_cap.fva));
6394 	drm_dbg_kms(connector->dev,
6395 		    "[CONNECTOR:%d:%s] Negative M VRR support: %s, CinemaVRR support: %s, Mdelta: %d\n",
6396 		    connector->base.id, connector->name, str_yes_no(hdmi->vrr_cap.cnmvrr),
6397 		    str_yes_no(hdmi->vrr_cap.cinema_vrr), hdmi->vrr_cap.mdelta);
6398 	drm_dbg_kms(connector->dev,
6399 		    "[CONNECTOR:%d:%s] VRRmin: %u, VRRmax: %u, VRR supported: %s\n",
6400 		    connector->base.id, connector->name, hdmi->vrr_cap.vrr_min,
6401 		    hdmi->vrr_cap.vrr_max, str_yes_no(hdmi->vrr_cap.supported));
6402 
6403 }
6404 
6405 static void drm_parse_hdmi_deep_color_info(struct drm_connector *connector,
6406 					   const u8 *hdmi)
6407 {
6408 	struct drm_display_info *info = &connector->display_info;
6409 	unsigned int dc_bpc = 0;
6410 
6411 	/* HDMI supports at least 8 bpc */
6412 	info->bpc = 8;
6413 
6414 	if (cea_db_payload_len(hdmi) < 6)
6415 		return;
6416 
6417 	if (hdmi[6] & DRM_EDID_HDMI_DC_30) {
6418 		dc_bpc = 10;
6419 		info->edid_hdmi_rgb444_dc_modes |= DRM_EDID_HDMI_DC_30;
6420 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] HDMI sink does deep color 30.\n",
6421 			    connector->base.id, connector->name);
6422 	}
6423 
6424 	if (hdmi[6] & DRM_EDID_HDMI_DC_36) {
6425 		dc_bpc = 12;
6426 		info->edid_hdmi_rgb444_dc_modes |= DRM_EDID_HDMI_DC_36;
6427 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] HDMI sink does deep color 36.\n",
6428 			    connector->base.id, connector->name);
6429 	}
6430 
6431 	if (hdmi[6] & DRM_EDID_HDMI_DC_48) {
6432 		dc_bpc = 16;
6433 		info->edid_hdmi_rgb444_dc_modes |= DRM_EDID_HDMI_DC_48;
6434 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] HDMI sink does deep color 48.\n",
6435 			    connector->base.id, connector->name);
6436 	}
6437 
6438 	if (dc_bpc == 0) {
6439 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] No deep color support on this HDMI sink.\n",
6440 			    connector->base.id, connector->name);
6441 		return;
6442 	}
6443 
6444 	drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] Assigning HDMI sink color depth as %d bpc.\n",
6445 		    connector->base.id, connector->name, dc_bpc);
6446 	info->bpc = dc_bpc;
6447 
6448 	/* YCRCB444 is optional according to spec. */
6449 	if (hdmi[6] & DRM_EDID_HDMI_DC_Y444) {
6450 		info->edid_hdmi_ycbcr444_dc_modes = info->edid_hdmi_rgb444_dc_modes;
6451 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] HDMI sink does YCRCB444 in deep color.\n",
6452 			    connector->base.id, connector->name);
6453 	}
6454 
6455 	/*
6456 	 * Spec says that if any deep color mode is supported at all,
6457 	 * then deep color 36 bit must be supported.
6458 	 */
6459 	if (!(hdmi[6] & DRM_EDID_HDMI_DC_36)) {
6460 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] HDMI sink should do DC_36, but does not!\n",
6461 			    connector->base.id, connector->name);
6462 	}
6463 }
6464 
6465 /* HDMI Vendor-Specific Data Block (HDMI VSDB, H14b-VSDB) */
6466 static void
6467 drm_parse_hdmi_vsdb_video(struct drm_connector *connector, const u8 *db)
6468 {
6469 	struct drm_display_info *info = &connector->display_info;
6470 	u8 len = cea_db_payload_len(db);
6471 
6472 	info->is_hdmi = true;
6473 
6474 	info->source_physical_address = (db[4] << 8) | db[5];
6475 
6476 	if (len >= 6)
6477 		info->dvi_dual = db[6] & 1;
6478 	if (len >= 7)
6479 		info->max_tmds_clock = db[7] * 5000;
6480 
6481 	/*
6482 	 * Try to infer whether the sink supports HDMI infoframes.
6483 	 *
6484 	 * HDMI infoframe support was first added in HDMI 1.4. Assume the sink
6485 	 * supports infoframes if HDMI_Video_present is set.
6486 	 */
6487 	if (len >= 8 && db[8] & BIT(5))
6488 		info->has_hdmi_infoframe = true;
6489 
6490 	drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] HDMI: DVI dual %d, max TMDS clock %d kHz\n",
6491 		    connector->base.id, connector->name,
6492 		    info->dvi_dual, info->max_tmds_clock);
6493 
6494 	drm_parse_hdmi_deep_color_info(connector, db);
6495 }
6496 
6497 /*
6498  * See EDID extension for head-mounted and specialized monitors, specified at:
6499  * https://docs.microsoft.com/en-us/windows-hardware/drivers/display/specialized-monitors-edid-extension
6500  */
6501 static void drm_parse_microsoft_vsdb(struct drm_connector *connector,
6502 				     const u8 *db)
6503 {
6504 	struct drm_display_info *info = &connector->display_info;
6505 	u8 version = db[4];
6506 	bool desktop_usage = db[5] & BIT(6);
6507 
6508 	/* Version 1 and 2 for HMDs, version 3 flags desktop usage explicitly */
6509 	if (version == 1 || version == 2 || (version == 3 && !desktop_usage))
6510 		info->non_desktop = true;
6511 
6512 	drm_dbg_kms(connector->dev,
6513 		    "[CONNECTOR:%d:%s] HMD or specialized display VSDB version %u: 0x%02x\n",
6514 		    connector->base.id, connector->name, version, db[5]);
6515 }
6516 
6517 static void drm_parse_amd_vsdb_common(struct drm_display_info *info,
6518 				  const struct amd_vsdb_common_payload *data)
6519 {
6520 	info->amd_vsdb.version = data->version;
6521 }
6522 
6523 static void drm_parse_amd_vsdb_v1(struct drm_display_info *info,
6524 				  const struct amd_vsdb_v1_payload *data)
6525 {
6526 	info->amd_vsdb.freesync_supported = data->feature_caps & 0x1;
6527 	info->amd_vsdb.min_frame_rate = data->min_vfreq;
6528 	info->amd_vsdb.max_frame_rate = data->max_vfreq;
6529 	info->amd_vsdb.freesync_vcp_code = data->freesync_vcp_code;
6530 
6531 	drm_parse_amd_vsdb_common(info, &data->common);
6532 }
6533 
6534 static void drm_parse_amd_vsdb_v2(struct drm_display_info *info,
6535 				  const struct amd_vsdb_v2_payload *data)
6536 {
6537 	info->amd_vsdb.luminance_range1.max_luminance = data->lum1_max;
6538 	info->amd_vsdb.luminance_range1.min_luminance = data->lum1_min;
6539 	info->amd_vsdb.luminance_range2.max_luminance = data->lum2_max;
6540 	info->amd_vsdb.luminance_range2.min_luminance = data->lum2_min;
6541 
6542 	drm_parse_amd_vsdb_v1(info, &data->v1);
6543 }
6544 
6545 static void drm_parse_amd_vsdb_v3(struct drm_display_info *info,
6546 				  const struct amd_vsdb_v3_payload *data,
6547 				  int payload_len)
6548 {
6549 	u16 max_frame_rate;
6550 
6551 	info->amd_vsdb.replay_mode = data->v2.v1.feature_caps & 0x40;
6552 	info->amd_vsdb.panel_type = (data->v2.cs_eotf_support & 0xC0) >> 6;
6553 
6554 	drm_parse_amd_vsdb_v2(info, &data->v2);
6555 
6556 	/* vfreq is provided in a different set of fields for v3. */
6557 	max_frame_rate = data->max_vfreq_ext_low |
6558 			 (data->max_vfreq_ext_high & 0x3) << 8;
6559 
6560 	/*
6561 	 * The AMD VSDB v3 payload length is variable (15..21 bytes).
6562 	 * All fields through max_vfreq_ext_high (byte 14) are always
6563 	 * present, but data->extra[] (bytes 15+) may not be, so any access
6564 	 * to extra[] must be guarded with a runtime length check to
6565 	 * avoid out-of-bounds reads on shorter (but spec-valid) payloads.
6566 	 */
6567 	if (payload_len >= AMD_VSDB_V3_MAX_VFREQ_EXT2_LEN)
6568 		max_frame_rate |= (data->extra[5] & 0x3) << 10;
6569 
6570 	info->amd_vsdb.max_frame_rate = max_frame_rate;
6571 }
6572 
6573 static void drm_parse_amd_vsdb(struct drm_connector *connector,
6574 			       const struct cea_db *db)
6575 {
6576 	struct drm_display_info *info = &connector->display_info;
6577 	const u8 *data = cea_db_data(db);
6578 	int payload_len = cea_db_payload_len(db);
6579 	u8 version;
6580 
6581 	version = ((const struct amd_vsdb_common_payload *)data)->version;
6582 
6583 	if (version < 1) {
6584 		drm_dbg_kms(connector->dev,
6585 			    "[CONNECTOR:%d:%s] Unsupported AMD VSDB version %u\n",
6586 			    connector->base.id, connector->name, version);
6587 		return;
6588 	}
6589 
6590 	if ((version <= 3 && payload_len < amd_vsdb_min_len[version]) ||
6591 		(version > 3 && payload_len < AMD_VSDB_V3_PAYLOAD_MIN_LEN)) {
6592 		drm_dbg_kms(connector->dev,
6593 			    "[CONNECTOR:%d:%s] AMD VSDB v%u payload too short (%d bytes)\n",
6594 			    connector->base.id, connector->name, version, payload_len);
6595 		return;
6596 	}
6597 
6598 	switch (version) {
6599 	default:
6600 		drm_dbg_kms(connector->dev,
6601 				"[CONNECTOR:%d:%s] AMD VSDB v%u is unsupported, VSDB will be parsed as v3.\n",
6602 				connector->base.id, connector->name, version);
6603 		fallthrough;
6604 	case 3:
6605 		drm_parse_amd_vsdb_v3(info, (const struct amd_vsdb_v3_payload *)data,
6606 				      payload_len);
6607 		break;
6608 	case 2:
6609 		drm_parse_amd_vsdb_v2(info, (const struct amd_vsdb_v2_payload *)data);
6610 		break;
6611 	case 1:
6612 		drm_parse_amd_vsdb_v1(info, (const struct amd_vsdb_v1_payload *)data);
6613 		break;
6614 	}
6615 }
6616 
6617 static void drm_parse_cea_ext(struct drm_connector *connector,
6618 			      const struct drm_edid *drm_edid)
6619 {
6620 	struct drm_display_info *info = &connector->display_info;
6621 	struct drm_edid_iter edid_iter;
6622 	const struct cea_db *db;
6623 	struct cea_db_iter iter;
6624 	const u8 *edid_ext;
6625 	u64 y420cmdb_map = 0;
6626 
6627 	drm_edid_iter_begin(drm_edid, &edid_iter);
6628 	drm_edid_iter_for_each(edid_ext, &edid_iter) {
6629 		if (edid_ext[0] != CEA_EXT)
6630 			continue;
6631 
6632 		if (!info->cea_rev)
6633 			info->cea_rev = edid_ext[1];
6634 
6635 		if (info->cea_rev != edid_ext[1])
6636 			drm_dbg_kms(connector->dev,
6637 				    "[CONNECTOR:%d:%s] CEA extension version mismatch %u != %u\n",
6638 				    connector->base.id, connector->name,
6639 				    info->cea_rev, edid_ext[1]);
6640 
6641 		/* The existence of a CTA extension should imply RGB support */
6642 		info->color_formats = BIT(DRM_OUTPUT_COLOR_FORMAT_RGB444);
6643 		if (edid_ext[3] & EDID_CEA_YCRCB444)
6644 			info->color_formats |= BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR444);
6645 		if (edid_ext[3] & EDID_CEA_YCRCB422)
6646 			info->color_formats |= BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422);
6647 		if (edid_ext[3] & EDID_BASIC_AUDIO)
6648 			info->has_audio = true;
6649 
6650 	}
6651 	drm_edid_iter_end(&edid_iter);
6652 
6653 	cea_db_iter_edid_begin(drm_edid, &iter);
6654 	cea_db_iter_for_each(db, &iter) {
6655 		/* FIXME: convert parsers to use struct cea_db */
6656 		const u8 *data = (const u8 *)db;
6657 
6658 		if (cea_db_is_hdmi_vsdb(db))
6659 			drm_parse_hdmi_vsdb_video(connector, data);
6660 		else if (cea_db_is_hdmi_forum_vsdb(db) ||
6661 			 cea_db_is_hdmi_forum_scdb(db))
6662 			drm_parse_hdmi_forum_scds(connector, data);
6663 		else if (cea_db_is_microsoft_vsdb(db))
6664 			drm_parse_microsoft_vsdb(connector, data);
6665 		else if (cea_db_is_amd_vsdb(db))
6666 			drm_parse_amd_vsdb(connector, db);
6667 		else if (cea_db_is_y420cmdb(db))
6668 			parse_cta_y420cmdb(connector, db, &y420cmdb_map);
6669 		else if (cea_db_is_y420vdb(db))
6670 			parse_cta_y420vdb(connector, db);
6671 		else if (cea_db_is_vcdb(db))
6672 			drm_parse_vcdb(connector, data);
6673 		else if (cea_db_is_hdmi_hdr_metadata_block(db))
6674 			drm_parse_hdr_metadata_block(connector, data);
6675 		else if (cea_db_tag(db) == CTA_DB_VIDEO)
6676 			parse_cta_vdb(connector, db);
6677 		else if (cea_db_tag(db) == CTA_DB_AUDIO)
6678 			info->has_audio = true;
6679 	}
6680 	cea_db_iter_end(&iter);
6681 
6682 	if (y420cmdb_map)
6683 		update_cta_y420cmdb(connector, y420cmdb_map);
6684 }
6685 
6686 static
6687 void get_monitor_range(const struct detailed_timing *timing, void *c)
6688 {
6689 	struct detailed_mode_closure *closure = c;
6690 	struct drm_display_info *info = &closure->connector->display_info;
6691 	struct drm_monitor_range_info *monitor_range = &info->monitor_range;
6692 	const struct detailed_non_pixel *data = &timing->data.other_data;
6693 	const struct detailed_data_monitor_range *range = &data->data.range;
6694 	const struct edid *edid = closure->drm_edid->edid;
6695 
6696 	if (!is_display_descriptor(timing, EDID_DETAIL_MONITOR_RANGE))
6697 		return;
6698 
6699 	/*
6700 	 * These limits are used to determine the VRR refresh
6701 	 * rate range. Only the "range limits only" variant
6702 	 * of the range descriptor seems to guarantee that
6703 	 * any and all timings are accepted by the sink, as
6704 	 * opposed to just timings conforming to the indicated
6705 	 * formula (GTF/GTF2/CVT). Thus other variants of the
6706 	 * range descriptor are not accepted here.
6707 	 */
6708 	if (range->flags != DRM_EDID_RANGE_LIMITS_ONLY_FLAG)
6709 		return;
6710 
6711 	monitor_range->min_vfreq = range->min_vfreq;
6712 	monitor_range->max_vfreq = range->max_vfreq;
6713 
6714 	if (edid->revision >= 4) {
6715 		if (data->pad2 & DRM_EDID_RANGE_OFFSET_MIN_VFREQ)
6716 			monitor_range->min_vfreq += 255;
6717 		if (data->pad2 & DRM_EDID_RANGE_OFFSET_MAX_VFREQ)
6718 			monitor_range->max_vfreq += 255;
6719 	}
6720 }
6721 
6722 static void drm_get_monitor_range(struct drm_connector *connector,
6723 				  const struct drm_edid *drm_edid)
6724 {
6725 	const struct drm_display_info *info = &connector->display_info;
6726 	struct detailed_mode_closure closure = {
6727 		.connector = connector,
6728 		.drm_edid = drm_edid,
6729 	};
6730 
6731 	if (drm_edid->edid->revision < 4)
6732 		return;
6733 
6734 	if (!(drm_edid->edid->features & DRM_EDID_FEATURE_CONTINUOUS_FREQ))
6735 		return;
6736 
6737 	drm_for_each_detailed_block(drm_edid, get_monitor_range, &closure);
6738 
6739 	drm_dbg_kms(connector->dev,
6740 		    "[CONNECTOR:%d:%s] Supported Monitor Refresh rate range is %d Hz - %d Hz\n",
6741 		    connector->base.id, connector->name,
6742 		    info->monitor_range.min_vfreq, info->monitor_range.max_vfreq);
6743 }
6744 
6745 static void drm_parse_vesa_mso_data(struct drm_connector *connector,
6746 				    const struct displayid_block *block)
6747 {
6748 	struct displayid_vesa_vendor_specific_block *vesa =
6749 		(struct displayid_vesa_vendor_specific_block *)block;
6750 	struct drm_display_info *info = &connector->display_info;
6751 
6752 	if (block->num_bytes < 3) {
6753 		drm_dbg_kms(connector->dev,
6754 			    "[CONNECTOR:%d:%s] Unexpected vendor block size %u\n",
6755 			    connector->base.id, connector->name, block->num_bytes);
6756 		return;
6757 	}
6758 
6759 	if (oui(vesa->oui[0], vesa->oui[1], vesa->oui[2]) != VESA_IEEE_OUI)
6760 		return;
6761 
6762 	if (sizeof(*vesa) != sizeof(*block) + block->num_bytes) {
6763 		drm_dbg_kms(connector->dev,
6764 			    "[CONNECTOR:%d:%s] Unexpected VESA vendor block size\n",
6765 			    connector->base.id, connector->name);
6766 		return;
6767 	}
6768 
6769 	switch (FIELD_GET(DISPLAYID_VESA_MSO_MODE, vesa->mso)) {
6770 	default:
6771 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] Reserved MSO mode value\n",
6772 			    connector->base.id, connector->name);
6773 		fallthrough;
6774 	case 0:
6775 		info->mso_stream_count = 0;
6776 		break;
6777 	case 1:
6778 		info->mso_stream_count = 2; /* 2 or 4 links */
6779 		break;
6780 	case 2:
6781 		info->mso_stream_count = 4; /* 4 links */
6782 		break;
6783 	}
6784 
6785 	if (!info->mso_stream_count) {
6786 		info->mso_pixel_overlap = 0;
6787 		return;
6788 	}
6789 
6790 	info->mso_pixel_overlap = FIELD_GET(DISPLAYID_VESA_MSO_OVERLAP, vesa->mso);
6791 	if (info->mso_pixel_overlap > 8) {
6792 		drm_dbg_kms(connector->dev,
6793 			    "[CONNECTOR:%d:%s] Reserved MSO pixel overlap value %u\n",
6794 			    connector->base.id, connector->name,
6795 			    info->mso_pixel_overlap);
6796 		info->mso_pixel_overlap = 8;
6797 	}
6798 
6799 	drm_dbg_kms(connector->dev,
6800 		    "[CONNECTOR:%d:%s] MSO stream count %u, pixel overlap %u\n",
6801 		    connector->base.id, connector->name,
6802 		    info->mso_stream_count, info->mso_pixel_overlap);
6803 }
6804 
6805 static void drm_update_mso(struct drm_connector *connector,
6806 			   const struct drm_edid *drm_edid)
6807 {
6808 	const struct displayid_block *block;
6809 	struct displayid_iter iter;
6810 
6811 	displayid_iter_edid_begin(drm_edid, &iter);
6812 	displayid_iter_for_each(block, &iter) {
6813 		if (block->tag == DATA_BLOCK_2_VENDOR_SPECIFIC)
6814 			drm_parse_vesa_mso_data(connector, block);
6815 	}
6816 	displayid_iter_end(&iter);
6817 }
6818 
6819 /* A connector has no EDID information, so we've got no EDID to compute quirks from. Reset
6820  * all of the values which would have been set from EDID
6821  */
6822 static void drm_reset_display_info(struct drm_connector *connector)
6823 {
6824 	struct drm_display_info *info = &connector->display_info;
6825 
6826 	info->width_mm = 0;
6827 	info->height_mm = 0;
6828 
6829 	info->bpc = 0;
6830 	info->color_formats = 0;
6831 	info->cea_rev = 0;
6832 	info->max_tmds_clock = 0;
6833 	info->dvi_dual = false;
6834 	info->is_hdmi = false;
6835 	info->has_audio = false;
6836 	info->has_hdmi_infoframe = false;
6837 	info->rgb_quant_range_selectable = false;
6838 	memset(&info->hdmi, 0, sizeof(info->hdmi));
6839 	memset(&info->hdr_sink_metadata, 0, sizeof(info->hdr_sink_metadata));
6840 
6841 	info->edid_hdmi_rgb444_dc_modes = 0;
6842 	info->edid_hdmi_ycbcr444_dc_modes = 0;
6843 
6844 	info->non_desktop = 0;
6845 	memset(&info->monitor_range, 0, sizeof(info->monitor_range));
6846 	memset(&info->luminance_range, 0, sizeof(info->luminance_range));
6847 
6848 	info->mso_stream_count = 0;
6849 	info->mso_pixel_overlap = 0;
6850 	info->max_dsc_bpp = 0;
6851 
6852 	kfree(info->vics);
6853 	info->vics = NULL;
6854 	info->vics_len = 0;
6855 
6856 	info->quirks = 0;
6857 
6858 	info->source_physical_address = CEC_PHYS_ADDR_INVALID;
6859 	memset(&info->amd_vsdb, 0, sizeof(info->amd_vsdb));
6860 
6861 	info->panel_type = DRM_MODE_PANEL_TYPE_UNKNOWN;
6862 }
6863 
6864 static void drm_displayid_process_base_section_header(struct drm_connector *connector,
6865 						      const struct displayid_iter *iter)
6866 {
6867 	struct drm_display_info *info = &connector->display_info;
6868 
6869 	drm_dbg_kms(connector->dev,
6870 		    "[CONNECTOR:%d:%s] DisplayID extension version 0x%02x, primary use 0x%02x\n",
6871 		    connector->base.id, connector->name,
6872 		    displayid_version(iter),
6873 		    displayid_primary_use(iter));
6874 	if (displayid_version(iter) == DISPLAY_ID_STRUCTURE_VER_20 &&
6875 	    (displayid_primary_use(iter) == PRIMARY_USE_HEAD_MOUNTED_VR ||
6876 	     displayid_primary_use(iter) == PRIMARY_USE_HEAD_MOUNTED_AR))
6877 		info->non_desktop = true;
6878 }
6879 
6880 static void
6881 drm_displayid_parse_display_params(struct drm_connector *connector,
6882 				   const struct displayid_block *block)
6883 {
6884 	struct drm_display_info *info = &connector->display_info;
6885 	const struct displayid_display_params_block *params =
6886 		(const struct displayid_display_params_block *)block;
6887 	u8 tech;
6888 
6889 	if (block->num_bytes < sizeof(*params) - sizeof(params->base)) {
6890 		drm_dbg_kms(connector->dev,
6891 			    "[CONNECTOR:%d:%s] DisplayID Display Parameters block too short (%u < %zu)\n",
6892 			    connector->base.id, connector->name,
6893 			    block->num_bytes,
6894 			    sizeof(*params) - sizeof(params->base));
6895 		return;
6896 	}
6897 
6898 	tech = FIELD_GET(DISPLAYID_DISPLAY_PARAMS_DEVICE_TECH,
6899 			 params->color_depth_and_tech);
6900 
6901 	drm_dbg_kms(connector->dev,
6902 		    "[CONNECTOR:%d:%s] DisplayID Display Parameters: device technology %s\n",
6903 		    connector->base.id, connector->name,
6904 		    tech == DISPLAYID_DEVICE_TECH_LCD ? "LCD" :
6905 		    tech == DISPLAYID_DEVICE_TECH_OLED ? "OLED" : "unspecified");
6906 
6907 	switch (tech) {
6908 	case DISPLAYID_DEVICE_TECH_LCD:
6909 		info->panel_type = DRM_MODE_PANEL_TYPE_LCD;
6910 		break;
6911 	case DISPLAYID_DEVICE_TECH_OLED:
6912 		info->panel_type = DRM_MODE_PANEL_TYPE_OLED;
6913 		break;
6914 	default:
6915 		break;
6916 	}
6917 }
6918 
6919 static void update_displayid_info(struct drm_connector *connector,
6920 				  const struct drm_edid *drm_edid)
6921 {
6922 	const struct displayid_block *block;
6923 	struct displayid_iter iter;
6924 	bool base_section_header_processed = false;
6925 
6926 	displayid_iter_edid_begin(drm_edid, &iter);
6927 	displayid_iter_for_each(block, &iter) {
6928 		if (!base_section_header_processed) {
6929 			drm_displayid_process_base_section_header(connector, &iter);
6930 			base_section_header_processed = true;
6931 		}
6932 
6933 		if (displayid_version(&iter) == DISPLAY_ID_STRUCTURE_VER_20 &&
6934 		    block->tag == DATA_BLOCK_2_DISPLAY_PARAMETERS)
6935 			drm_displayid_parse_display_params(connector, block);
6936 	}
6937 	displayid_iter_end(&iter);
6938 }
6939 
6940 static void update_display_info(struct drm_connector *connector,
6941 				const struct drm_edid *drm_edid)
6942 {
6943 	struct drm_display_info *info = &connector->display_info;
6944 	const struct edid *edid;
6945 
6946 	drm_reset_display_info(connector);
6947 	clear_eld(connector);
6948 
6949 	if (!drm_edid)
6950 		return;
6951 
6952 	edid = drm_edid->edid;
6953 
6954 	info->quirks = edid_get_quirks(drm_edid);
6955 
6956 	info->width_mm = edid->width_cm * 10;
6957 	info->height_mm = edid->height_cm * 10;
6958 
6959 	drm_get_monitor_range(connector, drm_edid);
6960 
6961 	if (edid->revision < 3)
6962 		goto out;
6963 
6964 	if (!drm_edid_is_digital(drm_edid))
6965 		goto out;
6966 
6967 	info->color_formats |= BIT(DRM_OUTPUT_COLOR_FORMAT_RGB444);
6968 	drm_parse_cea_ext(connector, drm_edid);
6969 
6970 	update_displayid_info(connector, drm_edid);
6971 
6972 	/*
6973 	 * Digital sink with "DFP 1.x compliant TMDS" according to EDID 1.3?
6974 	 *
6975 	 * For such displays, the DFP spec 1.0, section 3.10 "EDID support"
6976 	 * tells us to assume 8 bpc color depth if the EDID doesn't have
6977 	 * extensions which tell otherwise.
6978 	 */
6979 	if (info->bpc == 0 && edid->revision == 3 &&
6980 	    edid->input & DRM_EDID_DIGITAL_DFP_1_X) {
6981 		info->bpc = 8;
6982 		drm_dbg_kms(connector->dev,
6983 			    "[CONNECTOR:%d:%s] Assigning DFP sink color depth as %d bpc.\n",
6984 			    connector->base.id, connector->name, info->bpc);
6985 	}
6986 
6987 	/* Only defined for 1.4 with digital displays */
6988 	if (edid->revision < 4)
6989 		goto out;
6990 
6991 	switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
6992 	case DRM_EDID_DIGITAL_DEPTH_6:
6993 		info->bpc = 6;
6994 		break;
6995 	case DRM_EDID_DIGITAL_DEPTH_8:
6996 		info->bpc = 8;
6997 		break;
6998 	case DRM_EDID_DIGITAL_DEPTH_10:
6999 		info->bpc = 10;
7000 		break;
7001 	case DRM_EDID_DIGITAL_DEPTH_12:
7002 		info->bpc = 12;
7003 		break;
7004 	case DRM_EDID_DIGITAL_DEPTH_14:
7005 		info->bpc = 14;
7006 		break;
7007 	case DRM_EDID_DIGITAL_DEPTH_16:
7008 		info->bpc = 16;
7009 		break;
7010 	case DRM_EDID_DIGITAL_DEPTH_UNDEF:
7011 	default:
7012 		info->bpc = 0;
7013 		break;
7014 	}
7015 
7016 	drm_dbg_kms(connector->dev,
7017 		    "[CONNECTOR:%d:%s] Assigning EDID-1.4 digital sink color depth as %d bpc.\n",
7018 		    connector->base.id, connector->name, info->bpc);
7019 
7020 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
7021 		info->color_formats |= BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR444);
7022 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
7023 		info->color_formats |= BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR422);
7024 
7025 	drm_update_mso(connector, drm_edid);
7026 
7027 out:
7028 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_NON_DESKTOP)) {
7029 		drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] Non-desktop display%s\n",
7030 			    connector->base.id, connector->name,
7031 			    info->non_desktop ? " (redundant quirk)" : "");
7032 		info->non_desktop = true;
7033 	}
7034 
7035 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_CAP_DSC_15BPP))
7036 		info->max_dsc_bpp = 15;
7037 
7038 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_FORCE_6BPC))
7039 		info->bpc = 6;
7040 
7041 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_FORCE_8BPC))
7042 		info->bpc = 8;
7043 
7044 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_FORCE_10BPC))
7045 		info->bpc = 10;
7046 
7047 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_FORCE_12BPC))
7048 		info->bpc = 12;
7049 
7050 	/* Depends on info->cea_rev set by drm_parse_cea_ext() above */
7051 	drm_edid_to_eld(connector, drm_edid);
7052 }
7053 
7054 static struct drm_display_mode *drm_mode_displayid_detailed(struct drm_device *dev,
7055 							    const struct displayid_detailed_timings_1 *timings,
7056 							    bool type_7)
7057 {
7058 	struct drm_display_mode *mode;
7059 	unsigned int pixel_clock = (timings->pixel_clock[0] |
7060 				    (timings->pixel_clock[1] << 8) |
7061 				    (timings->pixel_clock[2] << 16)) + 1;
7062 	unsigned int hactive = le16_to_cpu(timings->hactive) + 1;
7063 	unsigned int hblank = le16_to_cpu(timings->hblank) + 1;
7064 	unsigned int hsync = (le16_to_cpu(timings->hsync) & 0x7fff) + 1;
7065 	unsigned int hsync_width = le16_to_cpu(timings->hsw) + 1;
7066 	unsigned int vactive = le16_to_cpu(timings->vactive) + 1;
7067 	unsigned int vblank = le16_to_cpu(timings->vblank) + 1;
7068 	unsigned int vsync = (le16_to_cpu(timings->vsync) & 0x7fff) + 1;
7069 	unsigned int vsync_width = le16_to_cpu(timings->vsw) + 1;
7070 	bool hsync_positive = le16_to_cpu(timings->hsync) & (1 << 15);
7071 	bool vsync_positive = le16_to_cpu(timings->vsync) & (1 << 15);
7072 
7073 	mode = drm_mode_create(dev);
7074 	if (!mode)
7075 		return NULL;
7076 
7077 	/* resolution is kHz for type VII, and 10 kHz for type I */
7078 	mode->clock = type_7 ? pixel_clock : pixel_clock * 10;
7079 	mode->hdisplay = hactive;
7080 	mode->hsync_start = mode->hdisplay + hsync;
7081 	mode->hsync_end = mode->hsync_start + hsync_width;
7082 	mode->htotal = mode->hdisplay + hblank;
7083 
7084 	mode->vdisplay = vactive;
7085 	mode->vsync_start = mode->vdisplay + vsync;
7086 	mode->vsync_end = mode->vsync_start + vsync_width;
7087 	mode->vtotal = mode->vdisplay + vblank;
7088 
7089 	mode->flags = 0;
7090 	mode->flags |= hsync_positive ? DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
7091 	mode->flags |= vsync_positive ? DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
7092 	mode->type = DRM_MODE_TYPE_DRIVER;
7093 
7094 	if (timings->flags & 0x80)
7095 		mode->type |= DRM_MODE_TYPE_PREFERRED;
7096 	drm_mode_set_name(mode);
7097 
7098 	return mode;
7099 }
7100 
7101 static int add_displayid_detailed_1_modes(struct drm_connector *connector,
7102 					  const struct displayid_block *block)
7103 {
7104 	struct displayid_detailed_timing_block *det = (struct displayid_detailed_timing_block *)block;
7105 	int i;
7106 	int num_timings;
7107 	struct drm_display_mode *newmode;
7108 	int num_modes = 0;
7109 	bool type_7 = block->tag == DATA_BLOCK_2_TYPE_7_DETAILED_TIMING;
7110 	/* blocks must be multiple of 20 bytes length */
7111 	if (block->num_bytes % 20)
7112 		return 0;
7113 
7114 	num_timings = block->num_bytes / 20;
7115 	for (i = 0; i < num_timings; i++) {
7116 		struct displayid_detailed_timings_1 *timings = &det->timings[i];
7117 
7118 		newmode = drm_mode_displayid_detailed(connector->dev, timings, type_7);
7119 		if (!newmode)
7120 			continue;
7121 
7122 		drm_mode_probed_add(connector, newmode);
7123 		num_modes++;
7124 	}
7125 	return num_modes;
7126 }
7127 
7128 static struct drm_display_mode *drm_mode_displayid_formula(struct drm_device *dev,
7129 							   const struct displayid_formula_timings_9 *timings,
7130 							   bool type_10)
7131 {
7132 	struct drm_display_mode *mode;
7133 	u16 hactive = le16_to_cpu(timings->hactive) + 1;
7134 	u16 vactive = le16_to_cpu(timings->vactive) + 1;
7135 	u8 timing_formula = timings->flags & 0x7;
7136 
7137 	/* TODO: support RB-v2 & RB-v3 */
7138 	if (timing_formula > 1)
7139 		return NULL;
7140 
7141 	/* TODO: support video-optimized refresh rate */
7142 	if (timings->flags & (1 << 4))
7143 		drm_dbg_kms(dev, "Fractional vrefresh is not implemented, proceeding with non-video-optimized refresh rate");
7144 
7145 	mode = drm_cvt_mode(dev, hactive, vactive, timings->vrefresh + 1, timing_formula == 1, false, false);
7146 	if (!mode)
7147 		return NULL;
7148 
7149 	/* TODO: interpret S3D flags */
7150 
7151 	mode->type = DRM_MODE_TYPE_DRIVER;
7152 	drm_mode_set_name(mode);
7153 
7154 	return mode;
7155 }
7156 
7157 static int add_displayid_formula_modes(struct drm_connector *connector,
7158 				       const struct displayid_block *block)
7159 {
7160 	const struct displayid_formula_timing_block *formula_block = (struct displayid_formula_timing_block *)block;
7161 	int num_timings;
7162 	struct drm_display_mode *newmode;
7163 	int num_modes = 0;
7164 	bool type_10 = block->tag == DATA_BLOCK_2_TYPE_10_FORMULA_TIMING;
7165 	int timing_size = 6 + ((formula_block->base.rev & 0x70) >> 4);
7166 
7167 	/* extended blocks are not supported yet */
7168 	if (timing_size != 6)
7169 		return 0;
7170 
7171 	if (block->num_bytes % timing_size)
7172 		return 0;
7173 
7174 	num_timings = block->num_bytes / timing_size;
7175 	for (int i = 0; i < num_timings; i++) {
7176 		const struct displayid_formula_timings_9 *timings = &formula_block->timings[i];
7177 
7178 		newmode = drm_mode_displayid_formula(connector->dev, timings, type_10);
7179 		if (!newmode)
7180 			continue;
7181 
7182 		drm_mode_probed_add(connector, newmode);
7183 		num_modes++;
7184 	}
7185 	return num_modes;
7186 }
7187 
7188 static int add_displayid_detailed_modes(struct drm_connector *connector,
7189 					const struct drm_edid *drm_edid)
7190 {
7191 	const struct displayid_block *block;
7192 	struct displayid_iter iter;
7193 	int num_modes = 0;
7194 
7195 	displayid_iter_edid_begin(drm_edid, &iter);
7196 	displayid_iter_for_each(block, &iter) {
7197 		if (block->tag == DATA_BLOCK_TYPE_1_DETAILED_TIMING ||
7198 		    block->tag == DATA_BLOCK_2_TYPE_7_DETAILED_TIMING)
7199 			num_modes += add_displayid_detailed_1_modes(connector, block);
7200 		else if (block->tag == DATA_BLOCK_2_TYPE_9_FORMULA_TIMING ||
7201 			 block->tag == DATA_BLOCK_2_TYPE_10_FORMULA_TIMING)
7202 			num_modes += add_displayid_formula_modes(connector, block);
7203 	}
7204 	displayid_iter_end(&iter);
7205 
7206 	return num_modes;
7207 }
7208 
7209 static int _drm_edid_connector_add_modes(struct drm_connector *connector,
7210 					 const struct drm_edid *drm_edid)
7211 {
7212 	int num_modes = 0;
7213 
7214 	if (!drm_edid)
7215 		return 0;
7216 
7217 	/*
7218 	 * EDID spec says modes should be preferred in this order:
7219 	 * - preferred detailed mode
7220 	 * - other detailed modes from base block
7221 	 * - detailed modes from extension blocks
7222 	 * - CVT 3-byte code modes
7223 	 * - standard timing codes
7224 	 * - established timing codes
7225 	 * - modes inferred from GTF or CVT range information
7226 	 *
7227 	 * We get this pretty much right.
7228 	 *
7229 	 * XXX order for additional mode types in extension blocks?
7230 	 */
7231 	num_modes += add_detailed_modes(connector, drm_edid);
7232 	num_modes += add_cvt_modes(connector, drm_edid);
7233 	num_modes += add_standard_modes(connector, drm_edid);
7234 	num_modes += add_established_modes(connector, drm_edid);
7235 	num_modes += add_cea_modes(connector, drm_edid);
7236 	num_modes += add_alternate_cea_modes(connector, drm_edid);
7237 	num_modes += add_displayid_detailed_modes(connector, drm_edid);
7238 	if (drm_edid->edid->features & DRM_EDID_FEATURE_CONTINUOUS_FREQ)
7239 		num_modes += add_inferred_modes(connector, drm_edid);
7240 
7241 	if (drm_edid_has_internal_quirk(connector, EDID_QUIRK_PREFER_LARGE_60) ||
7242 	    drm_edid_has_internal_quirk(connector, EDID_QUIRK_PREFER_LARGE_75))
7243 		edid_fixup_preferred(connector);
7244 
7245 	return num_modes;
7246 }
7247 
7248 static void _drm_update_tile_info(struct drm_connector *connector,
7249 				  const struct drm_edid *drm_edid);
7250 
7251 static int _drm_edid_connector_property_update(struct drm_connector *connector,
7252 					       const struct drm_edid *drm_edid)
7253 {
7254 	struct drm_device *dev = connector->dev;
7255 	int ret;
7256 
7257 	if (connector->edid_blob_ptr) {
7258 		const void *old_edid = connector->edid_blob_ptr->data;
7259 		size_t old_edid_size = connector->edid_blob_ptr->length;
7260 
7261 		if (old_edid && !drm_edid_eq(drm_edid, old_edid, old_edid_size)) {
7262 			connector->epoch_counter++;
7263 			drm_dbg_kms(dev, "[CONNECTOR:%d:%s] EDID changed, epoch counter %llu\n",
7264 				    connector->base.id, connector->name,
7265 				    connector->epoch_counter);
7266 		}
7267 	}
7268 
7269 	ret = drm_property_replace_global_blob(dev,
7270 					       &connector->edid_blob_ptr,
7271 					       drm_edid ? drm_edid->size : 0,
7272 					       drm_edid ? drm_edid->edid : NULL,
7273 					       &connector->base,
7274 					       dev->mode_config.edid_property);
7275 	if (ret) {
7276 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] EDID property update failed (%d)\n",
7277 			    connector->base.id, connector->name, ret);
7278 		goto out;
7279 	}
7280 
7281 	ret = drm_object_property_set_value(&connector->base,
7282 					    dev->mode_config.non_desktop_property,
7283 					    connector->display_info.non_desktop);
7284 	if (ret) {
7285 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Non-desktop property update failed (%d)\n",
7286 			    connector->base.id, connector->name, ret);
7287 		goto out;
7288 	}
7289 
7290 	ret = drm_connector_set_tile_property(connector);
7291 	if (ret) {
7292 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] Tile property update failed (%d)\n",
7293 			    connector->base.id, connector->name, ret);
7294 		goto out;
7295 	}
7296 
7297 out:
7298 	return ret;
7299 }
7300 
7301 /* For sysfs edid show implementation */
7302 ssize_t drm_edid_connector_property_show(struct drm_connector *connector,
7303 					 char *buf, loff_t off, size_t count)
7304 {
7305 	const void *edid;
7306 	size_t size;
7307 	ssize_t ret = 0;
7308 
7309 	mutex_lock(&connector->dev->mode_config.mutex);
7310 
7311 	if (!connector->edid_blob_ptr)
7312 		goto unlock;
7313 
7314 	edid = connector->edid_blob_ptr->data;
7315 	size = connector->edid_blob_ptr->length;
7316 	if (!edid)
7317 		goto unlock;
7318 
7319 	if (off >= size)
7320 		goto unlock;
7321 
7322 	if (off + count > size)
7323 		count = size - off;
7324 
7325 	memcpy(buf, edid + off, count);
7326 
7327 	ret = count;
7328 unlock:
7329 	mutex_unlock(&connector->dev->mode_config.mutex);
7330 
7331 	return ret;
7332 }
7333 
7334 /**
7335  * drm_edid_connector_update - Update connector information from EDID
7336  * @connector: Connector
7337  * @drm_edid: EDID
7338  *
7339  * Update the connector display info, ELD, HDR metadata, relevant properties,
7340  * etc. from the passed in EDID.
7341  *
7342  * If EDID is NULL, reset the information.
7343  *
7344  * Must be called before calling drm_edid_connector_add_modes().
7345  *
7346  * Return: 0 on success, negative error on errors.
7347  */
7348 int drm_edid_connector_update(struct drm_connector *connector,
7349 			      const struct drm_edid *drm_edid)
7350 {
7351 	update_display_info(connector, drm_edid);
7352 
7353 	_drm_update_tile_info(connector, drm_edid);
7354 
7355 	return _drm_edid_connector_property_update(connector, drm_edid);
7356 }
7357 EXPORT_SYMBOL(drm_edid_connector_update);
7358 
7359 /**
7360  * drm_edid_connector_add_modes - Update probed modes from the EDID property
7361  * @connector: Connector
7362  *
7363  * Add the modes from the previously updated EDID property to the connector
7364  * probed modes list.
7365  *
7366  * drm_edid_connector_update() must have been called before this to update the
7367  * EDID property.
7368  *
7369  * Return: The number of modes added, or 0 if we couldn't find any.
7370  */
7371 int drm_edid_connector_add_modes(struct drm_connector *connector)
7372 {
7373 	const struct drm_edid *drm_edid = NULL;
7374 	int count;
7375 
7376 	if (connector->edid_blob_ptr)
7377 		drm_edid = drm_edid_alloc(connector->edid_blob_ptr->data,
7378 					  connector->edid_blob_ptr->length);
7379 
7380 	count = _drm_edid_connector_add_modes(connector, drm_edid);
7381 
7382 	drm_edid_free(drm_edid);
7383 
7384 	return count;
7385 }
7386 EXPORT_SYMBOL(drm_edid_connector_add_modes);
7387 
7388 /**
7389  * drm_connector_update_edid_property - update the edid property of a connector
7390  * @connector: drm connector
7391  * @edid: new value of the edid property
7392  *
7393  * This function creates a new blob modeset object and assigns its id to the
7394  * connector's edid property.
7395  * Since we also parse tile information from EDID's displayID block, we also
7396  * set the connector's tile property here. See drm_connector_set_tile_property()
7397  * for more details.
7398  *
7399  * This function is deprecated. Use drm_edid_connector_update() instead.
7400  *
7401  * Returns:
7402  * Zero on success, negative errno on failure.
7403  */
7404 int drm_connector_update_edid_property(struct drm_connector *connector,
7405 				       const struct edid *edid)
7406 {
7407 	struct drm_edid drm_edid;
7408 
7409 	return drm_edid_connector_update(connector, drm_edid_legacy_init(&drm_edid, edid));
7410 }
7411 EXPORT_SYMBOL(drm_connector_update_edid_property);
7412 
7413 /**
7414  * drm_add_edid_modes - add modes from EDID data, if available
7415  * @connector: connector we're probing
7416  * @edid: EDID data
7417  *
7418  * Add the specified modes to the connector's mode list. Also fills out the
7419  * &drm_display_info structure and ELD in @connector with any information which
7420  * can be derived from the edid.
7421  *
7422  * This function is deprecated. Use drm_edid_connector_add_modes() instead.
7423  *
7424  * Return: The number of modes added or 0 if we couldn't find any.
7425  */
7426 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
7427 {
7428 	struct drm_edid _drm_edid;
7429 	const struct drm_edid *drm_edid;
7430 
7431 	if (edid && !drm_edid_is_valid(edid)) {
7432 		drm_warn(connector->dev, "[CONNECTOR:%d:%s] EDID invalid.\n",
7433 			 connector->base.id, connector->name);
7434 		edid = NULL;
7435 	}
7436 
7437 	drm_edid = drm_edid_legacy_init(&_drm_edid, edid);
7438 
7439 	update_display_info(connector, drm_edid);
7440 
7441 	return _drm_edid_connector_add_modes(connector, drm_edid);
7442 }
7443 EXPORT_SYMBOL(drm_add_edid_modes);
7444 
7445 /**
7446  * drm_add_modes_noedid - add modes for the connectors without EDID
7447  * @connector: connector we're probing
7448  * @hdisplay: the horizontal display limit
7449  * @vdisplay: the vertical display limit
7450  *
7451  * Add the specified modes to the connector's mode list. Only when the
7452  * hdisplay/vdisplay is not beyond the given limit, it will be added.
7453  *
7454  * Return: The number of modes added or 0 if we couldn't find any.
7455  */
7456 int drm_add_modes_noedid(struct drm_connector *connector,
7457 			 unsigned int hdisplay, unsigned int vdisplay)
7458 {
7459 	int i, count = ARRAY_SIZE(drm_dmt_modes), num_modes = 0;
7460 	struct drm_display_mode *mode;
7461 	struct drm_device *dev = connector->dev;
7462 
7463 	for (i = 0; i < count; i++) {
7464 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
7465 
7466 		if (hdisplay && vdisplay) {
7467 			/*
7468 			 * Only when two are valid, they will be used to check
7469 			 * whether the mode should be added to the mode list of
7470 			 * the connector.
7471 			 */
7472 			if (ptr->hdisplay > hdisplay ||
7473 					ptr->vdisplay > vdisplay)
7474 				continue;
7475 		}
7476 		if (drm_mode_vrefresh(ptr) > 61)
7477 			continue;
7478 		mode = drm_mode_duplicate(dev, ptr);
7479 		if (mode) {
7480 			drm_mode_probed_add(connector, mode);
7481 			num_modes++;
7482 		}
7483 	}
7484 	return num_modes;
7485 }
7486 EXPORT_SYMBOL(drm_add_modes_noedid);
7487 
7488 static bool is_hdmi2_sink(const struct drm_connector *connector)
7489 {
7490 	/*
7491 	 * FIXME: sil-sii8620 doesn't have a connector around when
7492 	 * we need one, so we have to be prepared for a NULL connector.
7493 	 */
7494 	if (!connector)
7495 		return true;
7496 
7497 	return connector->display_info.hdmi.scdc.supported ||
7498 		connector->display_info.color_formats & BIT(DRM_OUTPUT_COLOR_FORMAT_YCBCR420);
7499 }
7500 
7501 static u8 drm_mode_hdmi_vic(const struct drm_connector *connector,
7502 			    const struct drm_display_mode *mode)
7503 {
7504 	bool has_hdmi_infoframe = connector ?
7505 		connector->display_info.has_hdmi_infoframe : false;
7506 
7507 	if (!has_hdmi_infoframe)
7508 		return 0;
7509 
7510 	/* No HDMI VIC when signalling 3D video format */
7511 	if (mode->flags & DRM_MODE_FLAG_3D_MASK)
7512 		return 0;
7513 
7514 	return drm_match_hdmi_mode(mode);
7515 }
7516 
7517 static u8 drm_mode_cea_vic(const struct drm_connector *connector,
7518 			   const struct drm_display_mode *mode)
7519 {
7520 	/*
7521 	 * HDMI spec says if a mode is found in HDMI 1.4b 4K modes
7522 	 * we should send its VIC in vendor infoframes, else send the
7523 	 * VIC in AVI infoframes. Lets check if this mode is present in
7524 	 * HDMI 1.4b 4K modes
7525 	 */
7526 	if (drm_mode_hdmi_vic(connector, mode))
7527 		return 0;
7528 
7529 	return drm_match_cea_mode(mode);
7530 }
7531 
7532 /*
7533  * Avoid sending VICs defined in HDMI 2.0 in AVI infoframes to sinks that
7534  * conform to HDMI 1.4.
7535  *
7536  * HDMI 1.4 (CTA-861-D) VIC range: [1..64]
7537  * HDMI 2.0 (CTA-861-F) VIC range: [1..107]
7538  *
7539  * If the sink lists the VIC in CTA VDB, assume it's fine, regardless of HDMI
7540  * version.
7541  */
7542 static u8 vic_for_avi_infoframe(const struct drm_connector *connector, u8 vic)
7543 {
7544 	if (!is_hdmi2_sink(connector) && vic > 64 &&
7545 	    !cta_vdb_has_vic(connector, vic))
7546 		return 0;
7547 
7548 	return vic;
7549 }
7550 
7551 /**
7552  * drm_hdmi_avi_infoframe_from_display_mode() - fill an HDMI AVI infoframe with
7553  *                                              data from a DRM display mode
7554  * @frame: HDMI AVI infoframe
7555  * @connector: the connector
7556  * @mode: DRM display mode
7557  *
7558  * Return: 0 on success or a negative error code on failure.
7559  */
7560 int
7561 drm_hdmi_avi_infoframe_from_display_mode(struct hdmi_avi_infoframe *frame,
7562 					 const struct drm_connector *connector,
7563 					 const struct drm_display_mode *mode)
7564 {
7565 	enum hdmi_picture_aspect picture_aspect;
7566 	u8 vic, hdmi_vic;
7567 
7568 	if (!frame || !mode)
7569 		return -EINVAL;
7570 
7571 	hdmi_avi_infoframe_init(frame);
7572 
7573 	if (mode->flags & DRM_MODE_FLAG_DBLCLK)
7574 		frame->pixel_repeat = 1;
7575 
7576 	vic = drm_mode_cea_vic(connector, mode);
7577 	hdmi_vic = drm_mode_hdmi_vic(connector, mode);
7578 
7579 	frame->picture_aspect = HDMI_PICTURE_ASPECT_NONE;
7580 
7581 	/*
7582 	 * As some drivers don't support atomic, we can't use connector state.
7583 	 * So just initialize the frame with default values, just the same way
7584 	 * as it's done with other properties here.
7585 	 */
7586 	frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
7587 	frame->itc = 0;
7588 
7589 	/*
7590 	 * Populate picture aspect ratio from either
7591 	 * user input (if specified) or from the CEA/HDMI mode lists.
7592 	 */
7593 	picture_aspect = mode->picture_aspect_ratio;
7594 	if (picture_aspect == HDMI_PICTURE_ASPECT_NONE) {
7595 		if (vic)
7596 			picture_aspect = drm_get_cea_aspect_ratio(vic);
7597 		else if (hdmi_vic)
7598 			picture_aspect = drm_get_hdmi_aspect_ratio(hdmi_vic);
7599 	}
7600 
7601 	/*
7602 	 * The infoframe can't convey anything but none, 4:3
7603 	 * and 16:9, so if the user has asked for anything else
7604 	 * we can only satisfy it by specifying the right VIC.
7605 	 */
7606 	if (picture_aspect > HDMI_PICTURE_ASPECT_16_9) {
7607 		if (vic) {
7608 			if (picture_aspect != drm_get_cea_aspect_ratio(vic))
7609 				return -EINVAL;
7610 		} else if (hdmi_vic) {
7611 			if (picture_aspect != drm_get_hdmi_aspect_ratio(hdmi_vic))
7612 				return -EINVAL;
7613 		} else {
7614 			return -EINVAL;
7615 		}
7616 
7617 		picture_aspect = HDMI_PICTURE_ASPECT_NONE;
7618 	}
7619 
7620 	frame->video_code = vic_for_avi_infoframe(connector, vic);
7621 	frame->picture_aspect = picture_aspect;
7622 	frame->active_aspect = HDMI_ACTIVE_ASPECT_PICTURE;
7623 	frame->scan_mode = HDMI_SCAN_MODE_UNDERSCAN;
7624 
7625 	return 0;
7626 }
7627 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_from_display_mode);
7628 
7629 /**
7630  * drm_hdmi_avi_infoframe_quant_range() - fill the HDMI AVI infoframe
7631  *                                        quantization range information
7632  * @frame: HDMI AVI infoframe
7633  * @connector: the connector
7634  * @mode: DRM display mode
7635  * @rgb_quant_range: RGB quantization range (Q)
7636  */
7637 void
7638 drm_hdmi_avi_infoframe_quant_range(struct hdmi_avi_infoframe *frame,
7639 				   const struct drm_connector *connector,
7640 				   const struct drm_display_mode *mode,
7641 				   enum hdmi_quantization_range rgb_quant_range)
7642 {
7643 	const struct drm_display_info *info = &connector->display_info;
7644 
7645 	/*
7646 	 * CEA-861:
7647 	 * "A Source shall not send a non-zero Q value that does not correspond
7648 	 *  to the default RGB Quantization Range for the transmitted Picture
7649 	 *  unless the Sink indicates support for the Q bit in a Video
7650 	 *  Capabilities Data Block."
7651 	 *
7652 	 * HDMI 2.0 recommends sending non-zero Q when it does match the
7653 	 * default RGB quantization range for the mode, even when QS=0.
7654 	 */
7655 	if (info->rgb_quant_range_selectable ||
7656 	    rgb_quant_range == drm_default_rgb_quant_range(mode))
7657 		frame->quantization_range = rgb_quant_range;
7658 	else
7659 		frame->quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
7660 
7661 	/*
7662 	 * CEA-861-F:
7663 	 * "When transmitting any RGB colorimetry, the Source should set the
7664 	 *  YQ-field to match the RGB Quantization Range being transmitted
7665 	 *  (e.g., when Limited Range RGB, set YQ=0 or when Full Range RGB,
7666 	 *  set YQ=1) and the Sink shall ignore the YQ-field."
7667 	 *
7668 	 * Unfortunate certain sinks (eg. VIZ Model 67/E261VA) get confused
7669 	 * by non-zero YQ when receiving RGB. There doesn't seem to be any
7670 	 * good way to tell which version of CEA-861 the sink supports, so
7671 	 * we limit non-zero YQ to HDMI 2.0 sinks only as HDMI 2.0 is based
7672 	 * on CEA-861-F.
7673 	 */
7674 	if (!is_hdmi2_sink(connector) ||
7675 	    rgb_quant_range == HDMI_QUANTIZATION_RANGE_LIMITED)
7676 		frame->ycc_quantization_range =
7677 			HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
7678 	else
7679 		frame->ycc_quantization_range =
7680 			HDMI_YCC_QUANTIZATION_RANGE_FULL;
7681 }
7682 EXPORT_SYMBOL(drm_hdmi_avi_infoframe_quant_range);
7683 
7684 static enum hdmi_3d_structure
7685 s3d_structure_from_display_mode(const struct drm_display_mode *mode)
7686 {
7687 	u32 layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
7688 
7689 	switch (layout) {
7690 	case DRM_MODE_FLAG_3D_FRAME_PACKING:
7691 		return HDMI_3D_STRUCTURE_FRAME_PACKING;
7692 	case DRM_MODE_FLAG_3D_FIELD_ALTERNATIVE:
7693 		return HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE;
7694 	case DRM_MODE_FLAG_3D_LINE_ALTERNATIVE:
7695 		return HDMI_3D_STRUCTURE_LINE_ALTERNATIVE;
7696 	case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_FULL:
7697 		return HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL;
7698 	case DRM_MODE_FLAG_3D_L_DEPTH:
7699 		return HDMI_3D_STRUCTURE_L_DEPTH;
7700 	case DRM_MODE_FLAG_3D_L_DEPTH_GFX_GFX_DEPTH:
7701 		return HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH;
7702 	case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
7703 		return HDMI_3D_STRUCTURE_TOP_AND_BOTTOM;
7704 	case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
7705 		return HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF;
7706 	default:
7707 		return HDMI_3D_STRUCTURE_INVALID;
7708 	}
7709 }
7710 
7711 /**
7712  * drm_hdmi_vendor_infoframe_from_display_mode() - fill an HDMI infoframe with
7713  * data from a DRM display mode
7714  * @frame: HDMI vendor infoframe
7715  * @connector: the connector
7716  * @mode: DRM display mode
7717  *
7718  * Note that there's is a need to send HDMI vendor infoframes only when using a
7719  * 4k or stereoscopic 3D mode. So when giving any other mode as input this
7720  * function will return -EINVAL, error that can be safely ignored.
7721  *
7722  * Return: 0 on success or a negative error code on failure.
7723  */
7724 int
7725 drm_hdmi_vendor_infoframe_from_display_mode(struct hdmi_vendor_infoframe *frame,
7726 					    const struct drm_connector *connector,
7727 					    const struct drm_display_mode *mode)
7728 {
7729 	/*
7730 	 * FIXME: sil-sii8620 doesn't have a connector around when
7731 	 * we need one, so we have to be prepared for a NULL connector.
7732 	 */
7733 	bool has_hdmi_infoframe = connector ?
7734 		connector->display_info.has_hdmi_infoframe : false;
7735 	int err;
7736 
7737 	if (!frame || !mode)
7738 		return -EINVAL;
7739 
7740 	if (!has_hdmi_infoframe)
7741 		return -EINVAL;
7742 
7743 	err = hdmi_vendor_infoframe_init(frame);
7744 	if (err < 0)
7745 		return err;
7746 
7747 	/*
7748 	 * Even if it's not absolutely necessary to send the infoframe
7749 	 * (ie.vic==0 and s3d_struct==0) we will still send it if we
7750 	 * know that the sink can handle it. This is based on a
7751 	 * suggestion in HDMI 2.0 Appendix F. Apparently some sinks
7752 	 * have trouble realizing that they should switch from 3D to 2D
7753 	 * mode if the source simply stops sending the infoframe when
7754 	 * it wants to switch from 3D to 2D.
7755 	 */
7756 	frame->vic = drm_mode_hdmi_vic(connector, mode);
7757 	frame->s3d_struct = s3d_structure_from_display_mode(mode);
7758 
7759 	return 0;
7760 }
7761 EXPORT_SYMBOL(drm_hdmi_vendor_infoframe_from_display_mode);
7762 
7763 static void drm_parse_tiled_block(struct drm_connector *connector,
7764 				  const struct displayid_block *block)
7765 {
7766 	const struct displayid_tiled_block *tile = (struct displayid_tiled_block *)block;
7767 	u16 w, h;
7768 	u8 tile_v_loc, tile_h_loc;
7769 	u8 num_v_tile, num_h_tile;
7770 	struct drm_tile_group *tg;
7771 
7772 	/* tiled block payload per spec: cap 1 + topo 3 + size 4 + bezel 5 + id 9 = 22 */
7773 	if (block->num_bytes < 22) {
7774 		drm_dbg_kms(connector->dev,
7775 			    "[CONNECTOR:%d:%s] Unexpected tiled block size %u\n",
7776 			    connector->base.id, connector->name, block->num_bytes);
7777 		return;
7778 	}
7779 
7780 	w = tile->tile_size[0] | tile->tile_size[1] << 8;
7781 	h = tile->tile_size[2] | tile->tile_size[3] << 8;
7782 
7783 	num_v_tile = (tile->topo[0] & 0xf) | (tile->topo[2] & 0x30);
7784 	num_h_tile = (tile->topo[0] >> 4) | ((tile->topo[2] >> 2) & 0x30);
7785 	tile_v_loc = (tile->topo[1] & 0xf) | ((tile->topo[2] & 0x3) << 4);
7786 	tile_h_loc = (tile->topo[1] >> 4) | (((tile->topo[2] >> 2) & 0x3) << 4);
7787 
7788 	connector->has_tile = true;
7789 	if (tile->tile_cap & 0x80)
7790 		connector->tile_is_single_monitor = true;
7791 
7792 	connector->num_h_tile = num_h_tile + 1;
7793 	connector->num_v_tile = num_v_tile + 1;
7794 	connector->tile_h_loc = tile_h_loc;
7795 	connector->tile_v_loc = tile_v_loc;
7796 	connector->tile_h_size = w + 1;
7797 	connector->tile_v_size = h + 1;
7798 
7799 	drm_dbg_kms(connector->dev,
7800 		    "[CONNECTOR:%d:%s] tile cap 0x%x, size %dx%d, num tiles %dx%d, location %dx%d, vend %c%c%c",
7801 		    connector->base.id, connector->name,
7802 		    tile->tile_cap,
7803 		    connector->tile_h_size, connector->tile_v_size,
7804 		    connector->num_h_tile, connector->num_v_tile,
7805 		    connector->tile_h_loc, connector->tile_v_loc,
7806 		    tile->topology_id[0], tile->topology_id[1], tile->topology_id[2]);
7807 
7808 	tg = drm_mode_get_tile_group(connector->dev, tile->topology_id);
7809 	if (!tg)
7810 		tg = drm_mode_create_tile_group(connector->dev, tile->topology_id);
7811 	if (!tg)
7812 		return;
7813 
7814 	if (connector->tile_group != tg) {
7815 		/* if we haven't got a pointer,
7816 		   take the reference, drop ref to old tile group */
7817 		if (connector->tile_group)
7818 			drm_mode_put_tile_group(connector->dev, connector->tile_group);
7819 		connector->tile_group = tg;
7820 	} else {
7821 		/* if same tile group, then release the ref we just took. */
7822 		drm_mode_put_tile_group(connector->dev, tg);
7823 	}
7824 }
7825 
7826 static bool displayid_is_tiled_block(const struct displayid_iter *iter,
7827 				     const struct displayid_block *block)
7828 {
7829 	return (displayid_version(iter) < DISPLAY_ID_STRUCTURE_VER_20 &&
7830 		block->tag == DATA_BLOCK_TILED_DISPLAY) ||
7831 		(displayid_version(iter) == DISPLAY_ID_STRUCTURE_VER_20 &&
7832 		 block->tag == DATA_BLOCK_2_TILED_DISPLAY_TOPOLOGY);
7833 }
7834 
7835 static void _drm_update_tile_info(struct drm_connector *connector,
7836 				  const struct drm_edid *drm_edid)
7837 {
7838 	const struct displayid_block *block;
7839 	struct displayid_iter iter;
7840 
7841 	connector->has_tile = false;
7842 
7843 	displayid_iter_edid_begin(drm_edid, &iter);
7844 	displayid_iter_for_each(block, &iter) {
7845 		if (displayid_is_tiled_block(&iter, block))
7846 			drm_parse_tiled_block(connector, block);
7847 	}
7848 	displayid_iter_end(&iter);
7849 
7850 	if (!connector->has_tile && connector->tile_group) {
7851 		drm_mode_put_tile_group(connector->dev, connector->tile_group);
7852 		connector->tile_group = NULL;
7853 	}
7854 }
7855 
7856 /**
7857  * drm_edid_is_digital - is digital?
7858  * @drm_edid: The EDID
7859  *
7860  * Return true if input is digital.
7861  */
7862 bool drm_edid_is_digital(const struct drm_edid *drm_edid)
7863 {
7864 	return drm_edid && drm_edid->edid &&
7865 		drm_edid->edid->input & DRM_EDID_INPUT_DIGITAL;
7866 }
7867 EXPORT_SYMBOL(drm_edid_is_digital);
7868 
7869 /**
7870  * drm_edid_detect_panel_size - Get a panel's size from EDID
7871  * @drm_edid: EDID of the panel.
7872  * @width: Returns the panel's width in pixels per scanline, if given
7873  * @height: Returns the panel's height in scanlines, if given
7874  *
7875  * This function detects the preferred size of a panel from the given
7876  * EDID. There is no such information stored in the EDID block directly,
7877  * but the preferred mode often corresponds to the panel's native geometry.
7878  *
7879  * This helper should only be used during initialization before the
7880  * connector is available. For regular use, retrieve the available display
7881  * modes with the connector functions.
7882  *
7883  * Return: Zero on success, or a negative errno code otherwise.
7884  */
7885 int drm_edid_detect_panel_size(const struct drm_edid *drm_edid,
7886 			       unsigned int *width, unsigned int *height)
7887 {
7888 	const struct edid *edid = drm_edid->edid;
7889 	const struct detailed_timing *dt;
7890 	const struct detailed_pixel_timing *pt;
7891 
7892 	/*
7893 	 * Use display mode from the Preferred Timing Descriptor. For old
7894 	 * and obscure displays, we might need better heuristics.
7895 	 */
7896 
7897 	if (edid->revision < 4 || !(edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING))
7898 		return -EINVAL; /* no Preferred Timing Descriptor */
7899 
7900 	dt = &edid->detailed_timings[0];
7901 
7902 	if (!is_detailed_timing_descriptor(dt))
7903 		return -EINVAL;
7904 
7905 	pt = &dt->data.pixel_data;
7906 
7907 	if (width)
7908 		*width = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
7909 	if (height)
7910 		*height = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
7911 
7912 	return 0;
7913 }
7914 EXPORT_SYMBOL(drm_edid_detect_panel_size);
7915