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