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