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