xref: /openbsd-src/sys/dev/pci/drm/drm_edid.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: drm_edid.c,v 1.10 2014/07/12 18:48:52 tedu Exp $	*/
2 /*
3  * Copyright (c) 2006 Luc Verhaegen (quirks list)
4  * Copyright (c) 2007-2008 Intel Corporation
5  *   Jesse Barnes <jesse.barnes@intel.com>
6  * Copyright 2010 Red Hat, Inc.
7  *
8  * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
9  * FB layer.
10  *   Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sub license,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice (including the
20  * next paragraph) shall be included in all copies or substantial portions
21  * of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  */
31 #include "drmP.h"
32 #include "drm_edid.h"
33 #include "drm_edid_modes.h"
34 
35 #include <dev/i2c/i2cvar.h>
36 
37 #define version_greater(edid, maj, min) \
38 	(((edid)->version > (maj)) || \
39 	 ((edid)->version == (maj) && (edid)->revision > (min)))
40 
41 #define EDID_EST_TIMINGS 16
42 #define EDID_STD_TIMINGS 8
43 #define EDID_DETAILED_TIMINGS 4
44 
45 /*
46  * EDID blocks out in the wild have a variety of bugs, try to collect
47  * them here (note that userspace may work around broken monitors first,
48  * but fixes should make their way here so that the kernel "just works"
49  * on as many displays as possible).
50  */
51 
52 /* First detailed mode wrong, use largest 60Hz mode */
53 #define EDID_QUIRK_PREFER_LARGE_60		(1 << 0)
54 /* Reported 135MHz pixel clock is too high, needs adjustment */
55 #define EDID_QUIRK_135_CLOCK_TOO_HIGH		(1 << 1)
56 /* Prefer the largest mode at 75 Hz */
57 #define EDID_QUIRK_PREFER_LARGE_75		(1 << 2)
58 /* Detail timing is in cm not mm */
59 #define EDID_QUIRK_DETAILED_IN_CM		(1 << 3)
60 /* Detailed timing descriptors have bogus size values, so just take the
61  * maximum size and use that.
62  */
63 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE	(1 << 4)
64 /* Monitor forgot to set the first detailed is preferred bit. */
65 #define EDID_QUIRK_FIRST_DETAILED_PREFERRED	(1 << 5)
66 /* use +hsync +vsync for detailed mode */
67 #define EDID_QUIRK_DETAILED_SYNC_PP		(1 << 6)
68 /* Force reduced-blanking timings for detailed modes */
69 #define EDID_QUIRK_FORCE_REDUCED_BLANKING	(1 << 7)
70 /* Force 8bpc */
71 #define EDID_QUIRK_FORCE_8BPC			(1 << 8)
72 
73 struct detailed_mode_closure {
74 	struct drm_connector *connector;
75 	struct edid *edid;
76 	bool preferred;
77 	u32 quirks;
78 	int modes;
79 };
80 
81 #define LEVEL_DMT	0
82 #define LEVEL_GTF	1
83 #define LEVEL_GTF2	2
84 #define LEVEL_CVT	3
85 
86 static struct edid_quirk {
87 	char vendor[4];
88 	int product_id;
89 	u32 quirks;
90 } edid_quirk_list[] = {
91 	/* Acer AL1706 */
92 	{ "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
93 	/* Acer F51 */
94 	{ "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
95 	/* Unknown Acer */
96 	{ "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
97 
98 	/* Belinea 10 15 55 */
99 	{ "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
100 	{ "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
101 
102 	/* Envision Peripherals, Inc. EN-7100e */
103 	{ "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
104 	/* Envision EN2028 */
105 	{ "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
106 
107 	/* Funai Electronics PM36B */
108 	{ "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
109 	  EDID_QUIRK_DETAILED_IN_CM },
110 
111 	/* LG Philips LCD LP154W01-A5 */
112 	{ "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
113 	{ "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
114 
115 	/* Philips 107p5 CRT */
116 	{ "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
117 
118 	/* Proview AY765C */
119 	{ "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
120 
121 	/* Samsung SyncMaster 205BW.  Note: irony */
122 	{ "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
123 	/* Samsung SyncMaster 22[5-6]BW */
124 	{ "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
125 	{ "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
126 
127 	/* ViewSonic VA2026w */
128 	{ "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
129 
130 	/* Medion MD 30217 PG */
131 	{ "MED", 0x7b8, EDID_QUIRK_PREFER_LARGE_75 },
132 
133 	/* Panel in Samsung NP700G7A-S01PL notebook reports 6bpc */
134 	{ "SEC", 0xd033, EDID_QUIRK_FORCE_8BPC },
135 };
136 
137 /*** DDC fetch and block validation ***/
138 
139 static const u8 edid_header[] = {
140 	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
141 };
142 
143  /*
144  * Sanity check the header of the base EDID block.  Return 8 if the header
145  * is perfect, down to 0 if it's totally wrong.
146  */
147 int drm_edid_header_is_valid(const u8 *raw_edid)
148 {
149 	int i, score = 0;
150 
151 	for (i = 0; i < sizeof(edid_header); i++)
152 		if (raw_edid[i] == edid_header[i])
153 			score++;
154 
155 	return score;
156 }
157 EXPORT_SYMBOL(drm_edid_header_is_valid);
158 
159 /* Minimum number of valid EDID header bytes (0-8, default 6) */
160 static int edid_fixup = 6;
161 
162 /*
163  * Sanity check the EDID block (base or extension).  Return 0 if the block
164  * doesn't check out, or 1 if it's valid.
165  */
166 bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid)
167 {
168 	int i;
169 	u8 csum = 0;
170 	struct edid *edid = (struct edid *)raw_edid;
171 
172 	if (edid_fixup > 8 || edid_fixup < 0)
173 		edid_fixup = 6;
174 
175 	if (block == 0) {
176 		int score = drm_edid_header_is_valid(raw_edid);
177 		if (score == 8) ;
178 		else if (score >= edid_fixup) {
179 			DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
180 			memcpy(raw_edid, edid_header, sizeof(edid_header));
181 		} else {
182 			goto bad;
183 		}
184 	}
185 
186 	for (i = 0; i < EDID_LENGTH; i++)
187 		csum += raw_edid[i];
188 	if (csum) {
189 		if (print_bad_edid) {
190 			DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
191 		}
192 
193 		/* allow CEA to slide through, switches mangle this */
194 		if (raw_edid[0] != 0x02)
195 			goto bad;
196 	}
197 
198 	/* per-block-type checks */
199 	switch (raw_edid[0]) {
200 	case 0: /* base */
201 		if (edid->version != 1) {
202 			DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
203 			goto bad;
204 		}
205 
206 		if (edid->revision > 4)
207 			DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
208 		break;
209 
210 	default:
211 		break;
212 	}
213 
214 	return 1;
215 
216 bad:
217 	if (raw_edid && print_bad_edid) {
218 		printf("Raw EDID:\n");
219 		for (i = 0; i < EDID_LENGTH; i++) {
220 			if (i % 16 == 0)
221 				printf("\n");
222 			else if (i % 8 == 0)
223 				printf(" ");
224 			printf("%02x ", raw_edid[i]);
225 		}
226 		printf("\n");
227 	}
228 	return 0;
229 }
230 EXPORT_SYMBOL(drm_edid_block_valid);
231 
232 /**
233  * drm_edid_is_valid - sanity check EDID data
234  * @edid: EDID data
235  *
236  * Sanity-check an entire EDID record (including extensions)
237  */
238 bool drm_edid_is_valid(struct edid *edid)
239 {
240 	int i;
241 	u8 *raw = (u8 *)edid;
242 
243 	if (!edid)
244 		return false;
245 
246 	for (i = 0; i <= edid->extensions; i++)
247 		if (!drm_edid_block_valid(raw + i * EDID_LENGTH, i, true))
248 			return false;
249 
250 	return true;
251 }
252 EXPORT_SYMBOL(drm_edid_is_valid);
253 
254 #define DDC_SEGMENT_ADDR 0x30
255 /**
256  * Get EDID information via I2C.
257  *
258  * \param adapter : i2c device adaptor
259  * \param buf     : EDID data buffer to be filled
260  * \param len     : EDID data buffer length
261  * \return 0 on success or -1 on failure.
262  *
263  * Try to fetch EDID information by calling i2c driver function.
264  */
265 static int
266 drm_do_probe_ddc_edid(struct i2c_controller *adapter, unsigned char *buf,
267 		      int block, int len)
268 {
269 	unsigned char start = block * EDID_LENGTH;
270 	unsigned char segment = block >> 1;
271 	int ret = 0;
272 
273 	iic_acquire_bus(adapter, 0);
274 	if (segment) {
275 		ret = iic_exec(adapter, I2C_OP_WRITE,
276 		    DDC_SEGMENT_ADDR, NULL, 0, &segment, 1, 0);
277 		if (ret)
278 			goto i2c_err;
279 	}
280 	ret = iic_exec(adapter, I2C_OP_READ_WITH_STOP, DDC_ADDR,
281 	    &start, 1, buf, len, 0);
282 
283 i2c_err:
284 	iic_release_bus(adapter, 0);
285 
286 	return (ret);
287 }
288 
289 static bool drm_edid_is_zero(u8 *in_edid, int length)
290 {
291 	int i;
292 	u32 *raw_edid = (u32 *)in_edid;
293 
294 	for (i = 0; i < length / 4; i++)
295 		if (*(raw_edid + i) != 0)
296 			return false;
297 	return true;
298 }
299 
300 static u8 *
301 drm_do_get_edid(struct drm_connector *connector, struct i2c_controller *adapter)
302 {
303 	int i, j = 0, valid_extensions = 0;
304 	u8 *block, *new;
305 	bool print_bad_edid = !connector->bad_edid_counter;
306 
307 	if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
308 		return NULL;
309 
310 	/* base block fetch */
311 	for (i = 0; i < 4; i++) {
312 		if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
313 			goto out;
314 		if (drm_edid_block_valid(block, 0, print_bad_edid))
315 			break;
316 		if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
317 			connector->null_edid_counter++;
318 			goto carp;
319 		}
320 	}
321 	if (i == 4)
322 		goto carp;
323 
324 	/* if there's no extensions, we're done */
325 	if (block[0x7e] == 0)
326 		return block;
327 
328 	new = malloc((block[0x7e] + 1) * EDID_LENGTH, M_DRM, M_WAITOK);
329 	if (!new)
330 		goto out;
331 	bcopy(block, new, EDID_LENGTH);
332 	free(block, M_DRM, 0);
333 	block = new;
334 
335 	for (j = 1; j <= block[0x7e]; j++) {
336 		for (i = 0; i < 4; i++) {
337 			if (drm_do_probe_ddc_edid(adapter,
338 				  block + (valid_extensions + 1) * EDID_LENGTH,
339 				  j, EDID_LENGTH))
340 				goto out;
341 			if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH, j, print_bad_edid)) {
342 				valid_extensions++;
343 				break;
344 			}
345 		}
346 
347 		if (i == 4 && print_bad_edid) {
348 			printf("%s: Ignoring invalid EDID block %d.\n",
349 			 drm_get_connector_name(connector), j);
350 
351 			connector->bad_edid_counter++;
352 		}
353 	}
354 
355 	if (valid_extensions != block[0x7e]) {
356 		block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
357 		block[0x7e] = valid_extensions;
358 		new = malloc((valid_extensions + 1) * EDID_LENGTH,
359 		    M_DRM, M_WAITOK);
360 		if (!new)
361 			goto out;
362 		bcopy(block, new, (valid_extensions + 1) * EDID_LENGTH);
363 		free(block, M_DRM, 0);
364 		block = new;
365 	}
366 
367 	return block;
368 
369 carp:
370 	if (print_bad_edid) {
371 		printf("%s: EDID block %d invalid.\n",
372 		    drm_get_connector_name(connector), j);
373 	}
374 	connector->bad_edid_counter++;
375 
376 out:
377 	kfree(block);
378 	return NULL;
379 }
380 
381 /**
382  * Probe DDC presence.
383  *
384  * \param adapter : i2c device adaptor
385  * \return 1 on success
386  */
387 bool
388 drm_probe_ddc(struct i2c_controller *adapter)
389 {
390 	unsigned char out;
391 
392 	return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
393 }
394 EXPORT_SYMBOL(drm_probe_ddc);
395 
396 /**
397  * drm_get_edid - get EDID data, if available
398  * @connector: connector we're probing
399  * @adapter: i2c adapter to use for DDC
400  *
401  * Poke the given i2c channel to grab EDID data if possible.  If found,
402  * attach it to the connector.
403  *
404  * Return edid data or NULL if we couldn't find any.
405  */
406 struct edid *drm_get_edid(struct drm_connector *connector,
407 			  struct i2c_controller *adapter)
408 {
409 	struct edid *edid = NULL;
410 
411 	if (drm_probe_ddc(adapter))
412 		edid = (struct edid *)drm_do_get_edid(connector, adapter);
413 
414 	return edid;
415 }
416 EXPORT_SYMBOL(drm_get_edid);
417 
418 /*** EDID parsing ***/
419 
420 /**
421  * edid_vendor - match a string against EDID's obfuscated vendor field
422  * @edid: EDID to match
423  * @vendor: vendor string
424  *
425  * Returns true if @vendor is in @edid, false otherwise
426  */
427 static bool edid_vendor(struct edid *edid, char *vendor)
428 {
429 	char edid_vendor[3];
430 
431 	edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
432 	edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
433 			  ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
434 	edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
435 
436 	return !strncmp(edid_vendor, vendor, 3);
437 }
438 
439 /**
440  * edid_get_quirks - return quirk flags for a given EDID
441  * @edid: EDID to process
442  *
443  * This tells subsequent routines what fixes they need to apply.
444  */
445 static u32 edid_get_quirks(struct edid *edid)
446 {
447 	struct edid_quirk *quirk;
448 	int i;
449 
450 	for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
451 		quirk = &edid_quirk_list[i];
452 
453 		if (edid_vendor(edid, quirk->vendor) &&
454 		    (EDID_PRODUCT_ID(edid) == quirk->product_id))
455 			return quirk->quirks;
456 	}
457 
458 	return 0;
459 }
460 
461 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
462 #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
463 
464 /**
465  * edid_fixup_preferred - set preferred modes based on quirk list
466  * @connector: has mode list to fix up
467  * @quirks: quirks list
468  *
469  * Walk the mode list for @connector, clearing the preferred status
470  * on existing modes and setting it anew for the right mode ala @quirks.
471  */
472 static void edid_fixup_preferred(struct drm_connector *connector,
473 				 u32 quirks)
474 {
475 	struct drm_display_mode *t, *cur_mode, *preferred_mode;
476 	int target_refresh = 0;
477 
478 	if (list_empty(&connector->probed_modes))
479 		return;
480 
481 	if (quirks & EDID_QUIRK_PREFER_LARGE_60)
482 		target_refresh = 60;
483 	if (quirks & EDID_QUIRK_PREFER_LARGE_75)
484 		target_refresh = 75;
485 
486 	preferred_mode = list_first_entry(&connector->probed_modes,
487 					  struct drm_display_mode, head);
488 
489 	list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
490 		cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
491 
492 		if (cur_mode == preferred_mode)
493 			continue;
494 
495 		/* Largest mode is preferred */
496 		if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
497 			preferred_mode = cur_mode;
498 
499 		/* At a given size, try to get closest to target refresh */
500 		if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
501 		    MODE_REFRESH_DIFF(cur_mode, target_refresh) <
502 		    MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
503 			preferred_mode = cur_mode;
504 		}
505 	}
506 
507 	preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
508 }
509 
510 static bool
511 mode_is_rb(const struct drm_display_mode *mode)
512 {
513 	return (mode->htotal - mode->hdisplay == 160) &&
514 	       (mode->hsync_end - mode->hdisplay == 80) &&
515 	       (mode->hsync_end - mode->hsync_start == 32) &&
516 	       (mode->vsync_start - mode->vdisplay == 3);
517 }
518 
519 /*
520  * drm_mode_find_dmt - Create a copy of a mode if present in DMT
521  * @dev: Device to duplicate against
522  * @hsize: Mode width
523  * @vsize: Mode height
524  * @fresh: Mode refresh rate
525  * @rb: Mode reduced-blanking-ness
526  *
527  * Walk the DMT mode list looking for a match for the given parameters.
528  * Return a newly allocated copy of the mode, or NULL if not found.
529  */
530 struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
531 					   int hsize, int vsize, int fresh,
532 					   bool rb)
533 {
534 	int i;
535 
536 	for (i = 0; i < drm_num_dmt_modes; i++) {
537 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
538 		if (hsize != ptr->hdisplay)
539 			continue;
540 		if (vsize != ptr->vdisplay)
541 			continue;
542 		if (fresh != drm_mode_vrefresh(ptr))
543 			continue;
544 		if (rb != mode_is_rb(ptr))
545 			continue;
546 
547 		return drm_mode_duplicate(dev, ptr);
548 	}
549 
550 	return NULL;
551 }
552 EXPORT_SYMBOL(drm_mode_find_dmt);
553 
554 typedef void detailed_cb(struct detailed_timing *timing, void *closure);
555 
556 static void
557 cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
558 {
559 	int i, n = 0;
560 	u8 d = ext[0x02];
561 	u8 *det_base = ext + d;
562 
563 	n = (127 - d) / 18;
564 	for (i = 0; i < n; i++)
565 		cb((struct detailed_timing *)(det_base + 18 * i), closure);
566 }
567 
568 static void
569 vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
570 {
571 	unsigned int i, n = min((int)ext[0x02], 6);
572 	u8 *det_base = ext + 5;
573 
574 	if (ext[0x01] != 1)
575 		return; /* unknown version */
576 
577 	for (i = 0; i < n; i++)
578 		cb((struct detailed_timing *)(det_base + 18 * i), closure);
579 }
580 
581 static void
582 drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
583 {
584 	int i;
585 	struct edid *edid = (struct edid *)raw_edid;
586 
587 	if (edid == NULL)
588 		return;
589 
590 	for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
591 		cb(&(edid->detailed_timings[i]), closure);
592 
593 	for (i = 1; i <= raw_edid[0x7e]; i++) {
594 		u8 *ext = raw_edid + (i * EDID_LENGTH);
595 		switch (*ext) {
596 		case CEA_EXT:
597 			cea_for_each_detailed_block(ext, cb, closure);
598 			break;
599 		case VTB_EXT:
600 			vtb_for_each_detailed_block(ext, cb, closure);
601 			break;
602 		default:
603 			break;
604 		}
605 	}
606 }
607 
608 static void
609 is_rb(struct detailed_timing *t, void *data)
610 {
611 	u8 *r = (u8 *)t;
612 	if (r[3] == EDID_DETAIL_MONITOR_RANGE)
613 		if (r[15] & 0x10)
614 			*(bool *)data = true;
615 }
616 
617 /* EDID 1.4 defines this explicitly.  For EDID 1.3, we guess, badly. */
618 static bool
619 drm_monitor_supports_rb(struct edid *edid)
620 {
621 	if (edid->revision >= 4) {
622 		bool ret = false;
623 		drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
624 		return ret;
625 	}
626 
627 	return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
628 }
629 
630 static void
631 find_gtf2(struct detailed_timing *t, void *data)
632 {
633 	u8 *r = (u8 *)t;
634 	if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
635 		*(u8 **)data = r;
636 }
637 
638 /* Secondary GTF curve kicks in above some break frequency */
639 static int
640 drm_gtf2_hbreak(struct edid *edid)
641 {
642 	u8 *r = NULL;
643 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
644 	return r ? (r[12] * 2) : 0;
645 }
646 
647 static int
648 drm_gtf2_2c(struct edid *edid)
649 {
650 	u8 *r = NULL;
651 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
652 	return r ? r[13] : 0;
653 }
654 
655 static int
656 drm_gtf2_m(struct edid *edid)
657 {
658 	u8 *r = NULL;
659 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
660 	return r ? (r[15] << 8) + r[14] : 0;
661 }
662 
663 static int
664 drm_gtf2_k(struct edid *edid)
665 {
666 	u8 *r = NULL;
667 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
668 	return r ? r[16] : 0;
669 }
670 
671 static int
672 drm_gtf2_2j(struct edid *edid)
673 {
674 	u8 *r = NULL;
675 	drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
676 	return r ? r[17] : 0;
677 }
678 
679 /**
680  * standard_timing_level - get std. timing level(CVT/GTF/DMT)
681  * @edid: EDID block to scan
682  */
683 static int standard_timing_level(struct edid *edid)
684 {
685 	if (edid->revision >= 2) {
686 		if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
687 			return LEVEL_CVT;
688 		if (drm_gtf2_hbreak(edid))
689 			return LEVEL_GTF2;
690 		return LEVEL_GTF;
691 	}
692 	return LEVEL_DMT;
693 }
694 
695 /*
696  * 0 is reserved.  The spec says 0x01 fill for unused timings.  Some old
697  * monitors fill with ascii space (0x20) instead.
698  */
699 static int
700 bad_std_timing(u8 a, u8 b)
701 {
702 	return (a == 0x00 && b == 0x00) ||
703 	       (a == 0x01 && b == 0x01) ||
704 	       (a == 0x20 && b == 0x20);
705 }
706 
707 /**
708  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
709  * @t: standard timing params
710  * @timing_level: standard timing level
711  *
712  * Take the standard timing params (in this case width, aspect, and refresh)
713  * and convert them into a real mode using CVT/GTF/DMT.
714  */
715 static struct drm_display_mode *
716 drm_mode_std(struct drm_connector *connector, struct edid *edid,
717 	     struct std_timing *t, int revision)
718 {
719 	struct drm_device *dev = connector->dev;
720 	struct drm_display_mode *m, *mode = NULL;
721 	int hsize, vsize;
722 	int vrefresh_rate;
723 	unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
724 		>> EDID_TIMING_ASPECT_SHIFT;
725 	unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
726 		>> EDID_TIMING_VFREQ_SHIFT;
727 	int timing_level = standard_timing_level(edid);
728 
729 	if (bad_std_timing(t->hsize, t->vfreq_aspect))
730 		return NULL;
731 
732 	/* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
733 	hsize = t->hsize * 8 + 248;
734 	/* vrefresh_rate = vfreq + 60 */
735 	vrefresh_rate = vfreq + 60;
736 	/* the vdisplay is calculated based on the aspect ratio */
737 	if (aspect_ratio == 0) {
738 		if (revision < 3)
739 			vsize = hsize;
740 		else
741 			vsize = (hsize * 10) / 16;
742 	} else if (aspect_ratio == 1)
743 		vsize = (hsize * 3) / 4;
744 	else if (aspect_ratio == 2)
745 		vsize = (hsize * 4) / 5;
746 	else
747 		vsize = (hsize * 9) / 16;
748 
749 	/* HDTV hack, part 1 */
750 	if (vrefresh_rate == 60 &&
751 	    ((hsize == 1360 && vsize == 765) ||
752 	     (hsize == 1368 && vsize == 769))) {
753 		hsize = 1366;
754 		vsize = 768;
755 	}
756 
757 	/*
758 	 * If this connector already has a mode for this size and refresh
759 	 * rate (because it came from detailed or CVT info), use that
760 	 * instead.  This way we don't have to guess at interlace or
761 	 * reduced blanking.
762 	 */
763 	list_for_each_entry(m, &connector->probed_modes, head)
764 		if (m->hdisplay == hsize && m->vdisplay == vsize &&
765 		    drm_mode_vrefresh(m) == vrefresh_rate)
766 			return NULL;
767 
768 	/* HDTV hack, part 2 */
769 	if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
770 		mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
771 				    false);
772 		mode->hdisplay = 1366;
773 		mode->hsync_start = mode->hsync_start - 1;
774 		mode->hsync_end = mode->hsync_end - 1;
775 		return mode;
776 	}
777 
778 	/* check whether it can be found in default mode table */
779 	if (drm_monitor_supports_rb(edid)) {
780 		mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
781 					 true);
782 		if (mode)
783 			return mode;
784 	}
785 	mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
786 	if (mode)
787 		return mode;
788 
789 	/* okay, generate it */
790 	switch (timing_level) {
791 	case LEVEL_DMT:
792 		break;
793 	case LEVEL_GTF:
794 		mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
795 		break;
796 	case LEVEL_GTF2:
797 		/*
798 		 * This is potentially wrong if there's ever a monitor with
799 		 * more than one ranges section, each claiming a different
800 		 * secondary GTF curve.  Please don't do that.
801 		 */
802 		mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
803 		if (!mode)
804 			return NULL;
805 		if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
806 			drm_mode_destroy(dev, mode);
807 			mode = drm_gtf_mode_complex(dev, hsize, vsize,
808 						    vrefresh_rate, 0, 0,
809 						    drm_gtf2_m(edid),
810 						    drm_gtf2_2c(edid),
811 						    drm_gtf2_k(edid),
812 						    drm_gtf2_2j(edid));
813 		}
814 		break;
815 	case LEVEL_CVT:
816 		mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
817 				    false);
818 		break;
819 	}
820 	return mode;
821 }
822 
823 /*
824  * EDID is delightfully ambiguous about how interlaced modes are to be
825  * encoded.  Our internal representation is of frame height, but some
826  * HDTV detailed timings are encoded as field height.
827  *
828  * The format list here is from CEA, in frame size.  Technically we
829  * should be checking refresh rate too.  Whatever.
830  */
831 static void
832 drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
833 			    struct detailed_pixel_timing *pt)
834 {
835 	int i;
836 	static const struct {
837 		int w, h;
838 	} cea_interlaced[] = {
839 		{ 1920, 1080 },
840 		{  720,  480 },
841 		{ 1440,  480 },
842 		{ 2880,  480 },
843 		{  720,  576 },
844 		{ 1440,  576 },
845 		{ 2880,  576 },
846 	};
847 
848 	if (!(pt->misc & DRM_EDID_PT_INTERLACED))
849 		return;
850 
851 	for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
852 		if ((mode->hdisplay == cea_interlaced[i].w) &&
853 		    (mode->vdisplay == cea_interlaced[i].h / 2)) {
854 			mode->vdisplay *= 2;
855 			mode->vsync_start *= 2;
856 			mode->vsync_end *= 2;
857 			mode->vtotal *= 2;
858 			mode->vtotal |= 1;
859 		}
860 	}
861 
862 	mode->flags |= DRM_MODE_FLAG_INTERLACE;
863 }
864 
865 /**
866  * drm_mode_detailed - create a new mode from an EDID detailed timing section
867  * @dev: DRM device (needed to create new mode)
868  * @edid: EDID block
869  * @timing: EDID detailed timing info
870  * @quirks: quirks to apply
871  *
872  * An EDID detailed timing block contains enough info for us to create and
873  * return a new struct drm_display_mode.
874  */
875 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
876 						  struct edid *edid,
877 						  struct detailed_timing *timing,
878 						  u32 quirks)
879 {
880 	struct drm_display_mode *mode;
881 	struct detailed_pixel_timing *pt = &timing->data.pixel_data;
882 	unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
883 	unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
884 	unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
885 	unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
886 	unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
887 	unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
888 	unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) << 2 | pt->vsync_offset_pulse_width_lo >> 4;
889 	unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
890 
891 	/* ignore tiny modes */
892 	if (hactive < 64 || vactive < 64)
893 		return NULL;
894 
895 	if (pt->misc & DRM_EDID_PT_STEREO) {
896 		printf("stereo mode not supported\n");
897 		return NULL;
898 	}
899 	if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
900 		printf("composite sync not supported\n");
901 	}
902 
903 	/* it is incorrect if hsync/vsync width is zero */
904 	if (!hsync_pulse_width || !vsync_pulse_width) {
905 		DRM_DEBUG_KMS("Incorrect Detailed timing. "
906 				"Wrong Hsync/Vsync pulse width\n");
907 		return NULL;
908 	}
909 
910 	if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
911 		mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
912 		if (!mode)
913 			return NULL;
914 
915 		goto set_size;
916 	}
917 
918 	mode = drm_mode_create(dev);
919 	if (!mode)
920 		return NULL;
921 
922 	if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
923 		timing->pixel_clock = htole16(1088);
924 
925 	mode->clock = letoh16(timing->pixel_clock) * 10;
926 
927 	mode->hdisplay = hactive;
928 	mode->hsync_start = mode->hdisplay + hsync_offset;
929 	mode->hsync_end = mode->hsync_start + hsync_pulse_width;
930 	mode->htotal = mode->hdisplay + hblank;
931 
932 	mode->vdisplay = vactive;
933 	mode->vsync_start = mode->vdisplay + vsync_offset;
934 	mode->vsync_end = mode->vsync_start + vsync_pulse_width;
935 	mode->vtotal = mode->vdisplay + vblank;
936 
937 	/* Some EDIDs have bogus h/vtotal values */
938 	if (mode->hsync_end > mode->htotal)
939 		mode->htotal = mode->hsync_end + 1;
940 	if (mode->vsync_end > mode->vtotal)
941 		mode->vtotal = mode->vsync_end + 1;
942 
943 	drm_mode_do_interlace_quirk(mode, pt);
944 
945 	if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
946 		pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
947 	}
948 
949 	mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
950 		DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
951 	mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
952 		DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
953 
954 set_size:
955 	mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
956 	mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
957 
958 	if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
959 		mode->width_mm *= 10;
960 		mode->height_mm *= 10;
961 	}
962 
963 	if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
964 		mode->width_mm = edid->width_cm * 10;
965 		mode->height_mm = edid->height_cm * 10;
966 	}
967 
968 	mode->type = DRM_MODE_TYPE_DRIVER;
969 	mode->vrefresh = drm_mode_vrefresh(mode);
970 	drm_mode_set_name(mode);
971 
972 	return mode;
973 }
974 
975 static bool
976 mode_in_hsync_range(const struct drm_display_mode *mode,
977 		    struct edid *edid, u8 *t)
978 {
979 	int hsync, hmin, hmax;
980 
981 	hmin = t[7];
982 	if (edid->revision >= 4)
983 	    hmin += ((t[4] & 0x04) ? 255 : 0);
984 	hmax = t[8];
985 	if (edid->revision >= 4)
986 	    hmax += ((t[4] & 0x08) ? 255 : 0);
987 	hsync = drm_mode_hsync(mode);
988 
989 	return (hsync <= hmax && hsync >= hmin);
990 }
991 
992 static bool
993 mode_in_vsync_range(const struct drm_display_mode *mode,
994 		    struct edid *edid, u8 *t)
995 {
996 	int vsync, vmin, vmax;
997 
998 	vmin = t[5];
999 	if (edid->revision >= 4)
1000 	    vmin += ((t[4] & 0x01) ? 255 : 0);
1001 	vmax = t[6];
1002 	if (edid->revision >= 4)
1003 	    vmax += ((t[4] & 0x02) ? 255 : 0);
1004 	vsync = drm_mode_vrefresh(mode);
1005 
1006 	return (vsync <= vmax && vsync >= vmin);
1007 }
1008 
1009 static u32
1010 range_pixel_clock(struct edid *edid, u8 *t)
1011 {
1012 	/* unspecified */
1013 	if (t[9] == 0 || t[9] == 255)
1014 		return 0;
1015 
1016 	/* 1.4 with CVT support gives us real precision, yay */
1017 	if (edid->revision >= 4 && t[10] == 0x04)
1018 		return (t[9] * 10000) - ((t[12] >> 2) * 250);
1019 
1020 	/* 1.3 is pathetic, so fuzz up a bit */
1021 	return t[9] * 10000 + 5001;
1022 }
1023 
1024 static bool
1025 mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
1026 	      struct detailed_timing *timing)
1027 {
1028 	u32 max_clock;
1029 	u8 *t = (u8 *)timing;
1030 
1031 	if (!mode_in_hsync_range(mode, edid, t))
1032 		return false;
1033 
1034 	if (!mode_in_vsync_range(mode, edid, t))
1035 		return false;
1036 
1037 	if ((max_clock = range_pixel_clock(edid, t)))
1038 		if (mode->clock > max_clock)
1039 			return false;
1040 
1041 	/* 1.4 max horizontal check */
1042 	if (edid->revision >= 4 && t[10] == 0x04)
1043 		if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
1044 			return false;
1045 
1046 	if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
1047 		return false;
1048 
1049 	return true;
1050 }
1051 
1052 static bool valid_inferred_mode(const struct drm_connector *connector,
1053 				const struct drm_display_mode *mode)
1054 {
1055 	struct drm_display_mode *m;
1056 	bool ok = false;
1057 
1058 	list_for_each_entry(m, &connector->probed_modes, head) {
1059 		if (mode->hdisplay == m->hdisplay &&
1060 		    mode->vdisplay == m->vdisplay &&
1061 		    drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
1062 			return false; /* duplicated */
1063 		if (mode->hdisplay <= m->hdisplay &&
1064 		    mode->vdisplay <= m->vdisplay)
1065 			ok = true;
1066 	}
1067 	return ok;
1068 }
1069 
1070 static int
1071 drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
1072 			struct detailed_timing *timing)
1073 {
1074 	int i, modes = 0;
1075 	struct drm_display_mode *newmode;
1076 	struct drm_device *dev = connector->dev;
1077 
1078 	for (i = 0; i < drm_num_dmt_modes; i++) {
1079 		if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
1080 		    valid_inferred_mode(connector, drm_dmt_modes + i)) {
1081 			newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
1082 			if (newmode) {
1083 				drm_mode_probed_add(connector, newmode);
1084 				modes++;
1085 			}
1086 		}
1087 	}
1088 
1089 	return modes;
1090 }
1091 
1092 /* fix up 1366x768 mode from 1368x768;
1093  * GFT/CVT can't express 1366 width which isn't dividable by 8
1094  */
1095 static void fixup_mode_1366x768(struct drm_display_mode *mode)
1096 {
1097 	if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
1098 		mode->hdisplay = 1366;
1099 		mode->hsync_start--;
1100 		mode->hsync_end--;
1101 		drm_mode_set_name(mode);
1102 	}
1103 }
1104 
1105 static int
1106 drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
1107 			struct detailed_timing *timing)
1108 {
1109 	int i, modes = 0;
1110 	struct drm_display_mode *newmode;
1111 	struct drm_device *dev = connector->dev;
1112 
1113 	for (i = 0; i < num_extra_modes; i++) {
1114 		const struct minimode *m = &extra_modes[i];
1115 		newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
1116 		if (!newmode)
1117 			return modes;
1118 
1119 		fixup_mode_1366x768(newmode);
1120 		if (!mode_in_range(newmode, edid, timing) ||
1121 		    !valid_inferred_mode(connector, newmode)) {
1122 			drm_mode_destroy(dev, newmode);
1123 			continue;
1124 		}
1125 
1126 		drm_mode_probed_add(connector, newmode);
1127 		modes++;
1128 	}
1129 
1130 	return modes;
1131 }
1132 
1133 static int
1134 drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
1135 			struct detailed_timing *timing)
1136 {
1137 	int i, modes = 0;
1138 	struct drm_display_mode *newmode;
1139 	struct drm_device *dev = connector->dev;
1140 	bool rb = drm_monitor_supports_rb(edid);
1141 
1142 	for (i = 0; i < num_extra_modes; i++) {
1143 		const struct minimode *m = &extra_modes[i];
1144 		newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
1145 		if (!newmode)
1146 			return modes;
1147 
1148 		fixup_mode_1366x768(newmode);
1149 		if (!mode_in_range(newmode, edid, timing) ||
1150 		    !valid_inferred_mode(connector, newmode)) {
1151 			drm_mode_destroy(dev, newmode);
1152 			continue;
1153 		}
1154 
1155 		drm_mode_probed_add(connector, newmode);
1156 		modes++;
1157 	}
1158 
1159 	return modes;
1160 }
1161 
1162 static void
1163 do_inferred_modes(struct detailed_timing *timing, void *c)
1164 {
1165 	struct detailed_mode_closure *closure = c;
1166 	struct detailed_non_pixel *data = &timing->data.other_data;
1167 	struct detailed_data_monitor_range *range = &data->data.range;
1168 
1169 	if (data->type != EDID_DETAIL_MONITOR_RANGE)
1170 		return;
1171 
1172 	closure->modes += drm_dmt_modes_for_range(closure->connector,
1173 						  closure->edid,
1174 						  timing);
1175 
1176 	if (!version_greater(closure->edid, 1, 1))
1177 		return; /* GTF not defined yet */
1178 
1179 	switch (range->flags) {
1180 	case 0x02: /* secondary gtf, XXX could do more */
1181 	case 0x00: /* default gtf */
1182 		closure->modes += drm_gtf_modes_for_range(closure->connector,
1183 							  closure->edid,
1184 							  timing);
1185 		break;
1186 	case 0x04: /* cvt, only in 1.4+ */
1187 		if (!version_greater(closure->edid, 1, 3))
1188 			break;
1189 
1190 		closure->modes += drm_cvt_modes_for_range(closure->connector,
1191 							  closure->edid,
1192 							  timing);
1193 		break;
1194 	case 0x01: /* just the ranges, no formula */
1195 	default:
1196 		break;
1197 	}
1198 }
1199 
1200 static int
1201 add_inferred_modes(struct drm_connector *connector, struct edid *edid)
1202 {
1203 	struct detailed_mode_closure closure = {
1204 		connector, edid, 0, 0, 0
1205 	};
1206 
1207 	if (version_greater(edid, 1, 0))
1208 		drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
1209 					    &closure);
1210 
1211 	return closure.modes;
1212 }
1213 
1214 static int
1215 drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
1216 {
1217 	int i, j, m, modes = 0;
1218 	struct drm_display_mode *mode;
1219 	u8 *est = ((u8 *)timing) + 5;
1220 
1221 	for (i = 0; i < 6; i++) {
1222 		for (j = 7; j > 0; j--) {
1223 			m = (i * 8) + (7 - j);
1224 			if (m >= ARRAY_SIZE(est3_modes))
1225 				break;
1226 			if (est[i] & (1 << j)) {
1227 				mode = drm_mode_find_dmt(connector->dev,
1228 							 est3_modes[m].w,
1229 							 est3_modes[m].h,
1230 							 est3_modes[m].r,
1231 							 est3_modes[m].rb);
1232 				if (mode) {
1233 					drm_mode_probed_add(connector, mode);
1234 					modes++;
1235 				}
1236 			}
1237 		}
1238 	}
1239 
1240 	return modes;
1241 }
1242 
1243 static void
1244 do_established_modes(struct detailed_timing *timing, void *c)
1245 {
1246 	struct detailed_mode_closure *closure = c;
1247 	struct detailed_non_pixel *data = &timing->data.other_data;
1248 
1249 	if (data->type == EDID_DETAIL_EST_TIMINGS)
1250 		closure->modes += drm_est3_modes(closure->connector, timing);
1251 }
1252 
1253 /**
1254  * add_established_modes - get est. modes from EDID and add them
1255  * @edid: EDID block to scan
1256  *
1257  * Each EDID block contains a bitmap of the supported "established modes" list
1258  * (defined above).  Tease them out and add them to the global modes list.
1259  */
1260 static int
1261 add_established_modes(struct drm_connector *connector, struct edid *edid)
1262 {
1263 	struct drm_device *dev = connector->dev;
1264 	unsigned long est_bits = edid->established_timings.t1 |
1265 		(edid->established_timings.t2 << 8) |
1266 		((edid->established_timings.mfg_rsvd & 0x80) << 9);
1267 	int i, modes = 0;
1268 	struct detailed_mode_closure closure = {
1269 		connector, edid, 0, 0, 0
1270 	};
1271 
1272 	for (i = 0; i <= EDID_EST_TIMINGS; i++) {
1273 		if (est_bits & (1<<i)) {
1274 			struct drm_display_mode *newmode;
1275 			newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
1276 			if (newmode) {
1277 				drm_mode_probed_add(connector, newmode);
1278 				modes++;
1279 			}
1280 		}
1281 	}
1282 
1283 	if (version_greater(edid, 1, 0))
1284 		    drm_for_each_detailed_block((u8 *)edid,
1285 						do_established_modes, &closure);
1286 
1287 	return modes + closure.modes;
1288 }
1289 
1290 static void
1291 do_standard_modes(struct detailed_timing *timing, void *c)
1292 {
1293 	struct detailed_mode_closure *closure = c;
1294 	struct detailed_non_pixel *data = &timing->data.other_data;
1295 	struct drm_connector *connector = closure->connector;
1296 	struct edid *edid = closure->edid;
1297 
1298 	if (data->type == EDID_DETAIL_STD_MODES) {
1299 		int i;
1300 		for (i = 0; i < 6; i++) {
1301 			struct std_timing *std;
1302 			struct drm_display_mode *newmode;
1303 
1304 			std = &data->data.timings[i];
1305 			newmode = drm_mode_std(connector, edid, std,
1306 					       edid->revision);
1307 			if (newmode) {
1308 				drm_mode_probed_add(connector, newmode);
1309 				closure->modes++;
1310 			}
1311 		}
1312 	}
1313 }
1314 
1315 /**
1316  * add_standard_modes - get std. modes from EDID and add them
1317  * @edid: EDID block to scan
1318  *
1319  * Standard modes can be calculated using the appropriate standard (DMT,
1320  * GTF or CVT. Grab them from @edid and add them to the list.
1321  */
1322 static int
1323 add_standard_modes(struct drm_connector *connector, struct edid *edid)
1324 {
1325 	int i, modes = 0;
1326 	struct detailed_mode_closure closure = {
1327 		connector, edid, 0, 0, 0
1328 	};
1329 
1330 	for (i = 0; i < EDID_STD_TIMINGS; i++) {
1331 		struct drm_display_mode *newmode;
1332 
1333 		newmode = drm_mode_std(connector, edid,
1334 				       &edid->standard_timings[i],
1335 				       edid->revision);
1336 		if (newmode) {
1337 			drm_mode_probed_add(connector, newmode);
1338 			modes++;
1339 		}
1340 	}
1341 
1342 	if (version_greater(edid, 1, 0))
1343 		drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
1344 					    &closure);
1345 
1346 	/* XXX should also look for standard codes in VTB blocks */
1347 
1348 	return modes + closure.modes;
1349 }
1350 
1351 static int drm_cvt_modes(struct drm_connector *connector,
1352 			 struct detailed_timing *timing)
1353 {
1354 	int i, j, modes = 0;
1355 	struct drm_display_mode *newmode;
1356 	struct drm_device *dev = connector->dev;
1357 	struct cvt_timing *cvt;
1358 	const int rates[] = { 60, 85, 75, 60, 50 };
1359 	const u8 empty[3] = { 0, 0, 0 };
1360 
1361 	for (i = 0; i < 4; i++) {
1362 		int width, height;
1363 		cvt = &(timing->data.other_data.data.cvt[i]);
1364 
1365 		if (!memcmp(cvt->code, empty, 3))
1366 			continue;
1367 
1368 		height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
1369 		switch (cvt->code[1] & 0x0c) {
1370 		case 0x00:
1371 			width = height * 4 / 3;
1372 			break;
1373 		case 0x04:
1374 			width = height * 16 / 9;
1375 			break;
1376 		case 0x08:
1377 			width = height * 16 / 10;
1378 			break;
1379 		case 0x0c:
1380 			width = height * 15 / 9;
1381 			break;
1382 		}
1383 
1384 		for (j = 1; j < 5; j++) {
1385 			if (cvt->code[2] & (1 << j)) {
1386 				newmode = drm_cvt_mode(dev, width, height,
1387 						       rates[j], j == 0,
1388 						       false, false);
1389 				if (newmode) {
1390 					drm_mode_probed_add(connector, newmode);
1391 					modes++;
1392 				}
1393 			}
1394 		}
1395 	}
1396 
1397 	return modes;
1398 }
1399 
1400 static void
1401 do_cvt_mode(struct detailed_timing *timing, void *c)
1402 {
1403 	struct detailed_mode_closure *closure = c;
1404 	struct detailed_non_pixel *data = &timing->data.other_data;
1405 
1406 	if (data->type == EDID_DETAIL_CVT_3BYTE)
1407 		closure->modes += drm_cvt_modes(closure->connector, timing);
1408 }
1409 
1410 static int
1411 add_cvt_modes(struct drm_connector *connector, struct edid *edid)
1412 {
1413 	struct detailed_mode_closure closure = {
1414 		connector, edid, 0, 0, 0
1415 	};
1416 
1417 	if (version_greater(edid, 1, 2))
1418 		drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
1419 
1420 	/* XXX should also look for CVT codes in VTB blocks */
1421 
1422 	return closure.modes;
1423 }
1424 
1425 static void
1426 do_detailed_mode(struct detailed_timing *timing, void *c)
1427 {
1428 	struct detailed_mode_closure *closure = c;
1429 	struct drm_display_mode *newmode;
1430 
1431 	if (timing->pixel_clock) {
1432 		newmode = drm_mode_detailed(closure->connector->dev,
1433 					    closure->edid, timing,
1434 					    closure->quirks);
1435 		if (!newmode)
1436 			return;
1437 
1438 		if (closure->preferred)
1439 			newmode->type |= DRM_MODE_TYPE_PREFERRED;
1440 
1441 		drm_mode_probed_add(closure->connector, newmode);
1442 		closure->modes++;
1443 		closure->preferred = 0;
1444 	}
1445 }
1446 
1447 /*
1448  * add_detailed_modes - Add modes from detailed timings
1449  * @connector: attached connector
1450  * @edid: EDID block to scan
1451  * @quirks: quirks to apply
1452  */
1453 static int
1454 add_detailed_modes(struct drm_connector *connector, struct edid *edid,
1455 		   u32 quirks)
1456 {
1457 	struct detailed_mode_closure closure = {
1458 		connector,
1459 		edid,
1460 		1,
1461 		quirks,
1462 		0
1463 	};
1464 
1465 	if (closure.preferred && !version_greater(edid, 1, 3))
1466 		closure.preferred =
1467 		    (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
1468 
1469 	drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
1470 
1471 	return closure.modes;
1472 }
1473 
1474 #define HDMI_IDENTIFIER 0x000C03
1475 #define AUDIO_BLOCK	0x01
1476 #define VIDEO_BLOCK     0x02
1477 #define VENDOR_BLOCK    0x03
1478 #define SPEAKER_BLOCK	0x04
1479 #define EDID_BASIC_AUDIO	(1 << 6)
1480 #define EDID_CEA_YCRCB444	(1 << 5)
1481 #define EDID_CEA_YCRCB422	(1 << 4)
1482 
1483 /**
1484  * Search EDID for CEA extension block.
1485  */
1486 u8 *drm_find_cea_extension(struct edid *edid)
1487 {
1488 	u8 *edid_ext = NULL;
1489 	int i;
1490 
1491 	/* No EDID or EDID extensions */
1492 	if (edid == NULL || edid->extensions == 0)
1493 		return NULL;
1494 
1495 	/* Find CEA extension */
1496 	for (i = 0; i < edid->extensions; i++) {
1497 		edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
1498 		if (edid_ext[0] == CEA_EXT)
1499 			break;
1500 	}
1501 
1502 	if (i == edid->extensions)
1503 		return NULL;
1504 
1505 	return edid_ext;
1506 }
1507 EXPORT_SYMBOL(drm_find_cea_extension);
1508 
1509 /*
1510  * Looks for a CEA mode matching given drm_display_mode.
1511  * Returns its CEA Video ID code, or 0 if not found.
1512  */
1513 u8 drm_match_cea_mode(struct drm_display_mode *to_match)
1514 {
1515 	struct drm_display_mode *cea_mode;
1516 	u8 mode;
1517 
1518 	for (mode = 0; mode < drm_num_cea_modes; mode++) {
1519 		cea_mode = (struct drm_display_mode *)&edid_cea_modes[mode];
1520 
1521 		if (drm_mode_equal(to_match, cea_mode))
1522 			return mode + 1;
1523 	}
1524 	return 0;
1525 }
1526 EXPORT_SYMBOL(drm_match_cea_mode);
1527 
1528 
1529 static int
1530 do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
1531 {
1532 	struct drm_device *dev = connector->dev;
1533 	u8 * mode, cea_mode;
1534 	int modes = 0;
1535 
1536 	for (mode = db; mode < db + len; mode++) {
1537 		cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
1538 		if (cea_mode < drm_num_cea_modes) {
1539 			struct drm_display_mode *newmode;
1540 			newmode = drm_mode_duplicate(dev,
1541 						     &edid_cea_modes[cea_mode]);
1542 			if (newmode) {
1543 				drm_mode_probed_add(connector, newmode);
1544 				modes++;
1545 			}
1546 		}
1547 	}
1548 
1549 	return modes;
1550 }
1551 
1552 static int
1553 cea_db_payload_len(const u8 *db)
1554 {
1555 	return db[0] & 0x1f;
1556 }
1557 
1558 static int
1559 cea_db_tag(const u8 *db)
1560 {
1561 	return db[0] >> 5;
1562 }
1563 
1564 static int
1565 cea_revision(const u8 *cea)
1566 {
1567 	return cea[1];
1568 }
1569 
1570 static int
1571 cea_db_offsets(const u8 *cea, int *start, int *end)
1572 {
1573 	/* Data block offset in CEA extension block */
1574 	*start = 4;
1575 	*end = cea[2];
1576 	if (*end == 0)
1577 		*end = 127;
1578 	if (*end < 4 || *end > 127)
1579 		return -ERANGE;
1580 	return 0;
1581 }
1582 
1583 #define for_each_cea_db(cea, i, start, end) \
1584 	for ((i) = (start); (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
1585 
1586 static int
1587 add_cea_modes(struct drm_connector *connector, struct edid *edid)
1588 {
1589 	u8 * cea = drm_find_cea_extension(edid);
1590 	u8 * db, dbl;
1591 	int modes = 0;
1592 
1593 	if (cea && cea_revision(cea) >= 3) {
1594 		int i, start, end;
1595 
1596 		if (cea_db_offsets(cea, &start, &end))
1597 			return 0;
1598 
1599 		for_each_cea_db(cea, i, start, end) {
1600 			db = &cea[i];
1601 			dbl = cea_db_payload_len(db);
1602 
1603 			if (cea_db_tag(db) == VIDEO_BLOCK)
1604 				modes += do_cea_modes (connector, db+1, dbl);
1605 		}
1606 	}
1607 
1608 	return modes;
1609 }
1610 
1611 static void
1612 parse_hdmi_vsdb(struct drm_connector *connector, const u8 *db)
1613 {
1614 	u8 len = cea_db_payload_len(db);
1615 
1616 	if (len >= 6) {
1617 		connector->eld[5] |= (db[6] >> 7) << 1;  /* Supports_AI */
1618 		connector->dvi_dual = db[6] & 1;
1619 	}
1620 	if (len >= 7)
1621 		connector->max_tmds_clock = db[7] * 5;
1622 	if (len >= 8) {
1623 		connector->latency_present[0] = db[8] >> 7;
1624 		connector->latency_present[1] = (db[8] >> 6) & 1;
1625 	}
1626 	if (len >= 9)
1627 		connector->video_latency[0] = db[9];
1628 	if (len >= 10)
1629 		connector->audio_latency[0] = db[10];
1630 	if (len >= 11)
1631 		connector->video_latency[1] = db[11];
1632 	if (len >= 12)
1633 		connector->audio_latency[1] = db[12];
1634 
1635 	DRM_DEBUG_KMS("HDMI: DVI dual %d, "
1636 		    "max TMDS clock %d, "
1637 		    "latency present %d %d, "
1638 		    "video latency %d %d, "
1639 		    "audio latency %d %d\n",
1640 		    connector->dvi_dual,
1641 		    connector->max_tmds_clock,
1642 	      (int) connector->latency_present[0],
1643 	      (int) connector->latency_present[1],
1644 		    connector->video_latency[0],
1645 		    connector->video_latency[1],
1646 		    connector->audio_latency[0],
1647 		    connector->audio_latency[1]);
1648 }
1649 
1650 static void
1651 monitor_name(struct detailed_timing *t, void *data)
1652 {
1653 	if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
1654 		*(u8 **)data = t->data.other_data.data.str.str;
1655 }
1656 
1657 static bool cea_db_is_hdmi_vsdb(const u8 *db)
1658 {
1659 	int hdmi_id;
1660 
1661 	if (cea_db_tag(db) != VENDOR_BLOCK)
1662 		return false;
1663 
1664 	if (cea_db_payload_len(db) < 5)
1665 		return false;
1666 
1667 	hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
1668 
1669 	return hdmi_id == HDMI_IDENTIFIER;
1670 }
1671 
1672 /**
1673  * drm_edid_to_eld - build ELD from EDID
1674  * @connector: connector corresponding to the HDMI/DP sink
1675  * @edid: EDID to parse
1676  *
1677  * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
1678  * Some ELD fields are left to the graphics driver caller:
1679  * - Conn_Type
1680  * - HDCP
1681  * - Port_ID
1682  */
1683 void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
1684 {
1685 	uint8_t *eld = connector->eld;
1686 	u8 *cea;
1687 	u8 *name;
1688 	u8 *db;
1689 	int sad_count = 0;
1690 	int mnl;
1691 	int dbl;
1692 
1693 	memset(eld, 0, sizeof(connector->eld));
1694 
1695 	cea = drm_find_cea_extension(edid);
1696 	if (!cea) {
1697 		DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
1698 		return;
1699 	}
1700 
1701 	name = NULL;
1702 	drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
1703 	for (mnl = 0; name && mnl < 13; mnl++) {
1704 		if (name[mnl] == 0x0a)
1705 			break;
1706 		eld[20 + mnl] = name[mnl];
1707 	}
1708 	eld[4] = (cea[1] << 5) | mnl;
1709 	DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
1710 
1711 	eld[0] = 2 << 3;		/* ELD version: 2 */
1712 
1713 	eld[16] = edid->mfg_id[0];
1714 	eld[17] = edid->mfg_id[1];
1715 	eld[18] = edid->prod_code[0];
1716 	eld[19] = edid->prod_code[1];
1717 
1718 	if (cea_revision(cea) >= 3) {
1719 		int i, start, end;
1720 
1721 		if (cea_db_offsets(cea, &start, &end)) {
1722 			start = 0;
1723 			end = 0;
1724 		}
1725 
1726 		for_each_cea_db(cea, i, start, end) {
1727 			db = &cea[i];
1728 			dbl = cea_db_payload_len(db);
1729 
1730 			switch (cea_db_tag(db)) {
1731 			case AUDIO_BLOCK:
1732 				/* Audio Data Block, contains SADs */
1733 				sad_count = dbl / 3;
1734 				if (dbl >= 1)
1735 					memcpy(eld + 20 + mnl, &db[1], dbl);
1736 				break;
1737 			case SPEAKER_BLOCK:
1738 				/* Speaker Allocation Data Block */
1739 				if (dbl >= 1)
1740 					eld[7] = db[1];
1741 				break;
1742 			case VENDOR_BLOCK:
1743 				/* HDMI Vendor-Specific Data Block */
1744 				if (cea_db_is_hdmi_vsdb(db))
1745 					parse_hdmi_vsdb(connector, db);
1746 				break;
1747 			default:
1748 				break;
1749 			}
1750 		}
1751 	}
1752 	eld[5] |= sad_count << 4;
1753 	eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
1754 
1755 	DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
1756 }
1757 EXPORT_SYMBOL(drm_edid_to_eld);
1758 
1759 /**
1760  * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
1761  * @connector: connector associated with the HDMI/DP sink
1762  * @mode: the display mode
1763  */
1764 int drm_av_sync_delay(struct drm_connector *connector,
1765 		      struct drm_display_mode *mode)
1766 {
1767 	int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1768 	int a, v;
1769 
1770 	if (!connector->latency_present[0])
1771 		return 0;
1772 	if (!connector->latency_present[1])
1773 		i = 0;
1774 
1775 	a = connector->audio_latency[i];
1776 	v = connector->video_latency[i];
1777 
1778 	/*
1779 	 * HDMI/DP sink doesn't support audio or video?
1780 	 */
1781 	if (a == 255 || v == 255)
1782 		return 0;
1783 
1784 	/*
1785 	 * Convert raw EDID values to millisecond.
1786 	 * Treat unknown latency as 0ms.
1787 	 */
1788 	if (a)
1789 		a = min(2 * (a - 1), 500);
1790 	if (v)
1791 		v = min(2 * (v - 1), 500);
1792 
1793 	return max(v - a, 0);
1794 }
1795 EXPORT_SYMBOL(drm_av_sync_delay);
1796 
1797 /**
1798  * drm_select_eld - select one ELD from multiple HDMI/DP sinks
1799  * @encoder: the encoder just changed display mode
1800  * @mode: the adjusted display mode
1801  *
1802  * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
1803  * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
1804  */
1805 struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
1806 				     struct drm_display_mode *mode)
1807 {
1808 	struct drm_connector *connector;
1809 	struct drm_device *dev = encoder->dev;
1810 
1811 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1812 		if (connector->encoder == encoder && connector->eld[0])
1813 			return connector;
1814 
1815 	return NULL;
1816 }
1817 EXPORT_SYMBOL(drm_select_eld);
1818 
1819 /**
1820  * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
1821  * @edid: monitor EDID information
1822  *
1823  * Parse the CEA extension according to CEA-861-B.
1824  * Return true if HDMI, false if not or unknown.
1825  */
1826 bool drm_detect_hdmi_monitor(struct edid *edid)
1827 {
1828 	u8 *edid_ext;
1829 	int i;
1830 	int start_offset, end_offset;
1831 
1832 	edid_ext = drm_find_cea_extension(edid);
1833 	if (!edid_ext)
1834 		return false;
1835 
1836 	if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
1837 		return false;
1838 
1839 	/*
1840 	 * Because HDMI identifier is in Vendor Specific Block,
1841 	 * search it from all data blocks of CEA extension.
1842 	 */
1843 	for_each_cea_db(edid_ext, i, start_offset, end_offset) {
1844 		if (cea_db_is_hdmi_vsdb(&edid_ext[i]))
1845 			return true;
1846 	}
1847 
1848 	return false;
1849 }
1850 EXPORT_SYMBOL(drm_detect_hdmi_monitor);
1851 
1852 /**
1853  * drm_detect_monitor_audio - check monitor audio capability
1854  *
1855  * Monitor should have CEA extension block.
1856  * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
1857  * audio' only. If there is any audio extension block and supported
1858  * audio format, assume at least 'basic audio' support, even if 'basic
1859  * audio' is not defined in EDID.
1860  *
1861  */
1862 bool drm_detect_monitor_audio(struct edid *edid)
1863 {
1864 	u8 *edid_ext;
1865 	int i, j;
1866 	bool has_audio = false;
1867 	int start_offset, end_offset;
1868 
1869 	edid_ext = drm_find_cea_extension(edid);
1870 	if (!edid_ext)
1871 		goto end;
1872 
1873 	has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
1874 
1875 	if (has_audio) {
1876 		DRM_DEBUG_KMS("Monitor has basic audio support\n");
1877 		goto end;
1878 	}
1879 
1880 	if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
1881 		goto end;
1882 
1883 	for_each_cea_db(edid_ext, i, start_offset, end_offset) {
1884 		if (cea_db_tag(&edid_ext[i]) == AUDIO_BLOCK) {
1885 			has_audio = true;
1886 			for (j = 1; j < cea_db_payload_len(&edid_ext[i]) + 1; j += 3)
1887 				DRM_DEBUG_KMS("CEA audio format %d\n",
1888 					      (edid_ext[i + j] >> 3) & 0xf);
1889 			goto end;
1890 		}
1891 	}
1892 end:
1893 	return has_audio;
1894 }
1895 EXPORT_SYMBOL(drm_detect_monitor_audio);
1896 
1897 /**
1898  * drm_add_display_info - pull display info out if present
1899  * @edid: EDID data
1900  * @info: display info (attached to connector)
1901  *
1902  * Grab any available display info and stuff it into the drm_display_info
1903  * structure that's part of the connector.  Useful for tracking bpp and
1904  * color spaces.
1905  */
1906 static void drm_add_display_info(struct edid *edid,
1907 				 struct drm_display_info *info)
1908 {
1909 	u8 *edid_ext;
1910 
1911 	info->width_mm = edid->width_cm * 10;
1912 	info->height_mm = edid->height_cm * 10;
1913 
1914 	/* driver figures it out in this case */
1915 	info->bpc = 0;
1916 	info->color_formats = 0;
1917 
1918 	if (edid->revision < 3)
1919 		return;
1920 
1921 	if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
1922 		return;
1923 
1924 	/* Get data from CEA blocks if present */
1925 	edid_ext = drm_find_cea_extension(edid);
1926 	if (edid_ext) {
1927 		info->cea_rev = edid_ext[1];
1928 
1929 		/* The existence of a CEA block should imply RGB support */
1930 		info->color_formats = DRM_COLOR_FORMAT_RGB444;
1931 		if (edid_ext[3] & EDID_CEA_YCRCB444)
1932 			info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
1933 		if (edid_ext[3] & EDID_CEA_YCRCB422)
1934 			info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
1935 	}
1936 
1937 	/* Only defined for 1.4 with digital displays */
1938 	if (edid->revision < 4)
1939 		return;
1940 
1941 	switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
1942 	case DRM_EDID_DIGITAL_DEPTH_6:
1943 		info->bpc = 6;
1944 		break;
1945 	case DRM_EDID_DIGITAL_DEPTH_8:
1946 		info->bpc = 8;
1947 		break;
1948 	case DRM_EDID_DIGITAL_DEPTH_10:
1949 		info->bpc = 10;
1950 		break;
1951 	case DRM_EDID_DIGITAL_DEPTH_12:
1952 		info->bpc = 12;
1953 		break;
1954 	case DRM_EDID_DIGITAL_DEPTH_14:
1955 		info->bpc = 14;
1956 		break;
1957 	case DRM_EDID_DIGITAL_DEPTH_16:
1958 		info->bpc = 16;
1959 		break;
1960 	case DRM_EDID_DIGITAL_DEPTH_UNDEF:
1961 	default:
1962 		info->bpc = 0;
1963 		break;
1964 	}
1965 
1966 	info->color_formats |= DRM_COLOR_FORMAT_RGB444;
1967 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
1968 		info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
1969 	if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
1970 		info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
1971 }
1972 
1973 /**
1974  * drm_add_edid_modes - add modes from EDID data, if available
1975  * @connector: connector we're probing
1976  * @edid: edid data
1977  *
1978  * Add the specified modes to the connector's mode list.
1979  *
1980  * Return number of modes added or 0 if we couldn't find any.
1981  */
1982 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
1983 {
1984 	int num_modes = 0;
1985 	u32 quirks;
1986 
1987 	if (edid == NULL) {
1988 		return 0;
1989 	}
1990 	if (!drm_edid_is_valid(edid)) {
1991 		printf("%s: EDID invalid.\n",
1992 			 drm_get_connector_name(connector));
1993 		return 0;
1994 	}
1995 
1996 	quirks = edid_get_quirks(edid);
1997 
1998 	/*
1999 	 * EDID spec says modes should be preferred in this order:
2000 	 * - preferred detailed mode
2001 	 * - other detailed modes from base block
2002 	 * - detailed modes from extension blocks
2003 	 * - CVT 3-byte code modes
2004 	 * - standard timing codes
2005 	 * - established timing codes
2006 	 * - modes inferred from GTF or CVT range information
2007 	 *
2008 	 * We get this pretty much right.
2009 	 *
2010 	 * XXX order for additional mode types in extension blocks?
2011 	 */
2012 	num_modes += add_detailed_modes(connector, edid, quirks);
2013 	num_modes += add_cvt_modes(connector, edid);
2014 	num_modes += add_standard_modes(connector, edid);
2015 	num_modes += add_established_modes(connector, edid);
2016 	if (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF)
2017 		num_modes += add_inferred_modes(connector, edid);
2018 	num_modes += add_cea_modes(connector, edid);
2019 
2020 	if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
2021 		edid_fixup_preferred(connector, quirks);
2022 
2023 	drm_add_display_info(edid, &connector->display_info);
2024 
2025 	if (quirks & EDID_QUIRK_FORCE_8BPC)
2026 		connector->display_info.bpc = 8;
2027 
2028 	return num_modes;
2029 }
2030 EXPORT_SYMBOL(drm_add_edid_modes);
2031 
2032 /**
2033  * drm_add_modes_noedid - add modes for the connectors without EDID
2034  * @connector: connector we're probing
2035  * @hdisplay: the horizontal display limit
2036  * @vdisplay: the vertical display limit
2037  *
2038  * Add the specified modes to the connector's mode list. Only when the
2039  * hdisplay/vdisplay is not beyond the given limit, it will be added.
2040  *
2041  * Return number of modes added or 0 if we couldn't find any.
2042  */
2043 int drm_add_modes_noedid(struct drm_connector *connector,
2044 			int hdisplay, int vdisplay)
2045 {
2046 	int i, count, num_modes = 0;
2047 	struct drm_display_mode *mode;
2048 	struct drm_device *dev = connector->dev;
2049 
2050 	count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
2051 	if (hdisplay < 0)
2052 		hdisplay = 0;
2053 	if (vdisplay < 0)
2054 		vdisplay = 0;
2055 
2056 	for (i = 0; i < count; i++) {
2057 		const struct drm_display_mode *ptr = &drm_dmt_modes[i];
2058 		if (hdisplay && vdisplay) {
2059 			/*
2060 			 * Only when two are valid, they will be used to check
2061 			 * whether the mode should be added to the mode list of
2062 			 * the connector.
2063 			 */
2064 			if (ptr->hdisplay > hdisplay ||
2065 					ptr->vdisplay > vdisplay)
2066 				continue;
2067 		}
2068 		if (drm_mode_vrefresh(ptr) > 61)
2069 			continue;
2070 		mode = drm_mode_duplicate(dev, ptr);
2071 		if (mode) {
2072 			drm_mode_probed_add(connector, mode);
2073 			num_modes++;
2074 		}
2075 	}
2076 	return num_modes;
2077 }
2078 EXPORT_SYMBOL(drm_add_modes_noedid);
2079 
2080 /**
2081  * drm_mode_cea_vic - return the CEA-861 VIC of a given mode
2082  * @mode: mode
2083  *
2084  * RETURNS:
2085  * The VIC number, 0 in case it's not a CEA-861 mode.
2086  */
2087 uint8_t drm_mode_cea_vic(const struct drm_display_mode *mode)
2088 {
2089 	uint8_t i;
2090 
2091 	for (i = 0; i < drm_num_cea_modes; i++)
2092 		if (drm_mode_equal(mode, &edid_cea_modes[i]))
2093 			return i + 1;
2094 
2095 	return 0;
2096 }
2097 EXPORT_SYMBOL(drm_mode_cea_vic);
2098