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