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