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