blob: efe172f33602ac1b0645996034ba8aee83b0d991 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006 Luc Verhaegen (quirks list)
3 * Copyright (c) 2007-2008 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
Adam Jackson61e57a82010-03-29 21:43:18 +00005 * Copyright 2010 Red Hat, Inc.
Dave Airlief453ba02008-11-07 14:05:41 -08006 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Dave Airlief453ba02008-11-07 14:05:41 -080032#include <linux/i2c.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040033#include <linux/export.h>
Dave Airlief453ba02008-11-07 14:05:41 -080034#include "drmP.h"
35#include "drm_edid.h"
Adam Jackson38fcbb62010-08-03 14:38:20 -040036#include "drm_edid_modes.h"
Dave Airlief453ba02008-11-07 14:05:41 -080037
Adam Jackson13931572010-08-03 14:38:19 -040038#define version_greater(edid, maj, min) \
39 (((edid)->version > (maj)) || \
40 ((edid)->version == (maj) && (edid)->revision > (min)))
Dave Airlief453ba02008-11-07 14:05:41 -080041
Adam Jacksond1ff6402010-03-29 21:43:26 +000042#define EDID_EST_TIMINGS 16
43#define EDID_STD_TIMINGS 8
44#define EDID_DETAILED_TIMINGS 4
Dave Airlief453ba02008-11-07 14:05:41 -080045
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)
Adam Jacksonfd5f2542012-05-23 16:26:54 -040069/* Force reduced-blanking timings for detailed modes */
70#define EDID_QUIRK_FORCE_REDUCED_BLANKING (1 << 7)
Alex Deucher3c537882010-02-05 04:21:19 -050071
Adam Jackson13931572010-08-03 14:38:19 -040072struct detailed_mode_closure {
73 struct drm_connector *connector;
74 struct edid *edid;
75 bool preferred;
76 u32 quirks;
77 int modes;
78};
Dave Airlief453ba02008-11-07 14:05:41 -080079
Zhao Yakui5c612592009-06-22 13:17:10 +080080#define LEVEL_DMT 0
81#define LEVEL_GTF 1
Adam Jackson7a374352010-03-29 21:43:30 +000082#define LEVEL_GTF2 2
83#define LEVEL_CVT 3
Zhao Yakui5c612592009-06-22 13:17:10 +080084
Dave Airlief453ba02008-11-07 14:05:41 -080085static struct edid_quirk {
86 char *vendor;
87 int product_id;
88 u32 quirks;
89} edid_quirk_list[] = {
90 /* Acer AL1706 */
91 { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
92 /* Acer F51 */
93 { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
94 /* Unknown Acer */
95 { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
96
97 /* Belinea 10 15 55 */
98 { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
99 { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
100
101 /* Envision Peripherals, Inc. EN-7100e */
102 { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
Adam Jacksonba1163d2010-04-06 16:11:00 +0000103 /* Envision EN2028 */
104 { "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
Dave Airlief453ba02008-11-07 14:05:41 -0800105
106 /* Funai Electronics PM36B */
107 { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
108 EDID_QUIRK_DETAILED_IN_CM },
109
110 /* LG Philips LCD LP154W01-A5 */
111 { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
112 { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
113
114 /* Philips 107p5 CRT */
115 { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
116
117 /* Proview AY765C */
118 { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
119
120 /* Samsung SyncMaster 205BW. Note: irony */
121 { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
122 /* Samsung SyncMaster 22[5-6]BW */
123 { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
124 { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400125
126 /* ViewSonic VA2026w */
127 { "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
Alex Deucherd060c0b2013-08-12 11:04:29 -0400128
129 /* Medion MD 30217 PG */
130 { "MED", 0x7b8, EDID_QUIRK_PREFER_LARGE_75 },
Dave Airlief453ba02008-11-07 14:05:41 -0800131};
132
Adam Jackson61e57a82010-03-29 21:43:18 +0000133/*** DDC fetch and block validation ***/
Dave Airlief453ba02008-11-07 14:05:41 -0800134
Adam Jackson083ae052009-09-23 17:30:45 -0400135static const u8 edid_header[] = {
136 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
137};
Dave Airlief453ba02008-11-07 14:05:41 -0800138
Thomas Reim051963d2011-07-29 14:28:57 +0000139 /*
140 * Sanity check the header of the base EDID block. Return 8 if the header
141 * is perfect, down to 0 if it's totally wrong.
142 */
143int drm_edid_header_is_valid(const u8 *raw_edid)
144{
145 int i, score = 0;
146
147 for (i = 0; i < sizeof(edid_header); i++)
148 if (raw_edid[i] == edid_header[i])
149 score++;
150
151 return score;
152}
153EXPORT_SYMBOL(drm_edid_header_is_valid);
154
155
Adam Jackson61e57a82010-03-29 21:43:18 +0000156/*
157 * Sanity check the EDID block (base or extension). Return 0 if the block
158 * doesn't check out, or 1 if it's valid.
Dave Airlief453ba02008-11-07 14:05:41 -0800159 */
Carsten Emdeda0df922012-03-18 22:37:33 +0100160bool drm_edid_block_valid(u8 *raw_edid)
Dave Airlief453ba02008-11-07 14:05:41 -0800161{
Adam Jackson61e57a82010-03-29 21:43:18 +0000162 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800163 u8 csum = 0;
Adam Jackson61e57a82010-03-29 21:43:18 +0000164 struct edid *edid = (struct edid *)raw_edid;
Dave Airlief453ba02008-11-07 14:05:41 -0800165
Adam Jackson61e57a82010-03-29 21:43:18 +0000166 if (raw_edid[0] == 0x00) {
Thomas Reim051963d2011-07-29 14:28:57 +0000167 int score = drm_edid_header_is_valid(raw_edid);
Adam Jackson61e57a82010-03-29 21:43:18 +0000168 if (score == 8) ;
169 else if (score >= 6) {
170 DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
171 memcpy(raw_edid, edid_header, sizeof(edid_header));
172 } else {
173 goto bad;
174 }
175 }
Dave Airlief453ba02008-11-07 14:05:41 -0800176
177 for (i = 0; i < EDID_LENGTH; i++)
178 csum += raw_edid[i];
179 if (csum) {
180 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
Adam Jackson4a638b42010-05-25 16:33:09 -0400181
182 /* allow CEA to slide through, switches mangle this */
183 if (raw_edid[0] != 0x02)
184 goto bad;
Dave Airlief453ba02008-11-07 14:05:41 -0800185 }
186
Adam Jackson61e57a82010-03-29 21:43:18 +0000187 /* per-block-type checks */
188 switch (raw_edid[0]) {
189 case 0: /* base */
190 if (edid->version != 1) {
191 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
192 goto bad;
193 }
Adam Jackson862b89c2009-11-23 14:23:06 -0500194
Adam Jackson61e57a82010-03-29 21:43:18 +0000195 if (edid->revision > 4)
196 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
197 break;
198
199 default:
200 break;
201 }
Adam Jackson47ee4cc2009-11-23 14:23:05 -0500202
Dave Airlief453ba02008-11-07 14:05:41 -0800203 return 1;
204
205bad:
206 if (raw_edid) {
Dave Airlief49dadb2011-06-14 06:13:54 +0000207 printk(KERN_ERR "Raw EDID:\n");
Tormod Volden0aff47f2011-07-05 20:12:53 +0000208 print_hex_dump(KERN_ERR, " \t", DUMP_PREFIX_NONE, 16, 1,
209 raw_edid, EDID_LENGTH, false);
Dave Airlief453ba02008-11-07 14:05:41 -0800210 }
211 return 0;
212}
Carsten Emdeda0df922012-03-18 22:37:33 +0100213EXPORT_SYMBOL(drm_edid_block_valid);
Adam Jackson61e57a82010-03-29 21:43:18 +0000214
215/**
216 * drm_edid_is_valid - sanity check EDID data
217 * @edid: EDID data
218 *
219 * Sanity-check an entire EDID record (including extensions)
220 */
221bool drm_edid_is_valid(struct edid *edid)
222{
223 int i;
224 u8 *raw = (u8 *)edid;
225
226 if (!edid)
227 return false;
228
229 for (i = 0; i <= edid->extensions; i++)
230 if (!drm_edid_block_valid(raw + i * EDID_LENGTH))
231 return false;
232
233 return true;
234}
Alex Deucher3c537882010-02-05 04:21:19 -0500235EXPORT_SYMBOL(drm_edid_is_valid);
Dave Airlief453ba02008-11-07 14:05:41 -0800236
Adam Jackson61e57a82010-03-29 21:43:18 +0000237#define DDC_SEGMENT_ADDR 0x30
238/**
239 * Get EDID information via I2C.
240 *
241 * \param adapter : i2c device adaptor
242 * \param buf : EDID data buffer to be filled
243 * \param len : EDID data buffer length
244 * \return 0 on success or -1 on failure.
245 *
246 * Try to fetch EDID information by calling i2c driver function.
247 */
248static int
249drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
250 int block, int len)
251{
252 unsigned char start = block * EDID_LENGTH;
Chris Wilson4819d2e2011-03-15 11:04:41 +0000253 int ret, retries = 5;
Adam Jackson61e57a82010-03-29 21:43:18 +0000254
Chris Wilson4819d2e2011-03-15 11:04:41 +0000255 /* The core i2c driver will automatically retry the transfer if the
256 * adapter reports EAGAIN. However, we find that bit-banging transfers
257 * are susceptible to errors under a heavily loaded machine and
258 * generate spurious NAKs and timeouts. Retrying the transfer
259 * of the individual block a few times seems to overcome this.
260 */
261 do {
262 struct i2c_msg msgs[] = {
263 {
264 .addr = DDC_ADDR,
265 .flags = 0,
266 .len = 1,
267 .buf = &start,
268 }, {
269 .addr = DDC_ADDR,
270 .flags = I2C_M_RD,
271 .len = len,
272 .buf = buf,
273 }
274 };
275 ret = i2c_transfer(adapter, msgs, 2);
Eugeni Dodonov9292f372012-01-05 09:34:28 -0200276 if (ret == -ENXIO) {
277 DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
278 adapter->name);
279 break;
280 }
Chris Wilson4819d2e2011-03-15 11:04:41 +0000281 } while (ret != 2 && --retries);
Adam Jackson61e57a82010-03-29 21:43:18 +0000282
Chris Wilson4819d2e2011-03-15 11:04:41 +0000283 return ret == 2 ? 0 : -1;
Adam Jackson61e57a82010-03-29 21:43:18 +0000284}
285
Dave Airlie4a9a8b72011-06-14 06:13:55 +0000286static bool drm_edid_is_zero(u8 *in_edid, int length)
287{
288 int i;
289 u32 *raw_edid = (u32 *)in_edid;
290
291 for (i = 0; i < length / 4; i++)
292 if (*(raw_edid + i) != 0)
293 return false;
294 return true;
295}
296
Adam Jackson61e57a82010-03-29 21:43:18 +0000297static u8 *
298drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
299{
Sam Tygier0ea75e22010-09-23 10:11:01 +0100300 int i, j = 0, valid_extensions = 0;
Adam Jackson61e57a82010-03-29 21:43:18 +0000301 u8 *block, *new;
302
303 if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
304 return NULL;
305
306 /* base block fetch */
307 for (i = 0; i < 4; i++) {
308 if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
309 goto out;
310 if (drm_edid_block_valid(block))
311 break;
Dave Airlie4a9a8b72011-06-14 06:13:55 +0000312 if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
313 connector->null_edid_counter++;
314 goto carp;
315 }
Adam Jackson61e57a82010-03-29 21:43:18 +0000316 }
317 if (i == 4)
318 goto carp;
319
320 /* if there's no extensions, we're done */
321 if (block[0x7e] == 0)
322 return block;
323
324 new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
325 if (!new)
326 goto out;
327 block = new;
328
329 for (j = 1; j <= block[0x7e]; j++) {
330 for (i = 0; i < 4; i++) {
Sam Tygier0ea75e22010-09-23 10:11:01 +0100331 if (drm_do_probe_ddc_edid(adapter,
332 block + (valid_extensions + 1) * EDID_LENGTH,
333 j, EDID_LENGTH))
Adam Jackson61e57a82010-03-29 21:43:18 +0000334 goto out;
Sam Tygier0ea75e22010-09-23 10:11:01 +0100335 if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH)) {
336 valid_extensions++;
Adam Jackson61e57a82010-03-29 21:43:18 +0000337 break;
Sam Tygier0ea75e22010-09-23 10:11:01 +0100338 }
Adam Jackson61e57a82010-03-29 21:43:18 +0000339 }
340 if (i == 4)
Sam Tygier0ea75e22010-09-23 10:11:01 +0100341 dev_warn(connector->dev->dev,
342 "%s: Ignoring invalid EDID block %d.\n",
343 drm_get_connector_name(connector), j);
344 }
345
346 if (valid_extensions != block[0x7e]) {
347 block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
348 block[0x7e] = valid_extensions;
349 new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
350 if (!new)
351 goto out;
352 block = new;
Adam Jackson61e57a82010-03-29 21:43:18 +0000353 }
354
355 return block;
356
357carp:
Jordan Crousedcdb1672010-05-27 13:40:25 -0600358 dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
Adam Jackson61e57a82010-03-29 21:43:18 +0000359 drm_get_connector_name(connector), j);
360
361out:
362 kfree(block);
363 return NULL;
364}
365
366/**
367 * Probe DDC presence.
368 *
369 * \param adapter : i2c device adaptor
370 * \return 1 on success
371 */
372static bool
373drm_probe_ddc(struct i2c_adapter *adapter)
374{
375 unsigned char out;
376
377 return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
378}
379
380/**
381 * drm_get_edid - get EDID data, if available
382 * @connector: connector we're probing
383 * @adapter: i2c adapter to use for DDC
384 *
385 * Poke the given i2c channel to grab EDID data if possible. If found,
386 * attach it to the connector.
387 *
388 * Return edid data or NULL if we couldn't find any.
389 */
390struct edid *drm_get_edid(struct drm_connector *connector,
391 struct i2c_adapter *adapter)
392{
393 struct edid *edid = NULL;
394
395 if (drm_probe_ddc(adapter))
396 edid = (struct edid *)drm_do_get_edid(connector, adapter);
397
398 connector->display_info.raw_edid = (char *)edid;
399
400 return edid;
401
402}
403EXPORT_SYMBOL(drm_get_edid);
404
405/*** EDID parsing ***/
406
Dave Airlief453ba02008-11-07 14:05:41 -0800407/**
408 * edid_vendor - match a string against EDID's obfuscated vendor field
409 * @edid: EDID to match
410 * @vendor: vendor string
411 *
412 * Returns true if @vendor is in @edid, false otherwise
413 */
414static bool edid_vendor(struct edid *edid, char *vendor)
415{
416 char edid_vendor[3];
417
418 edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
419 edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
420 ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
Dave Airlie16456c82009-04-03 09:10:33 +1000421 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
Dave Airlief453ba02008-11-07 14:05:41 -0800422
423 return !strncmp(edid_vendor, vendor, 3);
424}
425
426/**
427 * edid_get_quirks - return quirk flags for a given EDID
428 * @edid: EDID to process
429 *
430 * This tells subsequent routines what fixes they need to apply.
431 */
432static u32 edid_get_quirks(struct edid *edid)
433{
434 struct edid_quirk *quirk;
435 int i;
436
437 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
438 quirk = &edid_quirk_list[i];
439
440 if (edid_vendor(edid, quirk->vendor) &&
441 (EDID_PRODUCT_ID(edid) == quirk->product_id))
442 return quirk->quirks;
443 }
444
445 return 0;
446}
447
448#define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
449#define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
450
Dave Airlief453ba02008-11-07 14:05:41 -0800451/**
452 * edid_fixup_preferred - set preferred modes based on quirk list
453 * @connector: has mode list to fix up
454 * @quirks: quirks list
455 *
456 * Walk the mode list for @connector, clearing the preferred status
457 * on existing modes and setting it anew for the right mode ala @quirks.
458 */
459static void edid_fixup_preferred(struct drm_connector *connector,
460 u32 quirks)
461{
462 struct drm_display_mode *t, *cur_mode, *preferred_mode;
Dave Airlief8906072008-12-18 16:59:02 +1000463 int target_refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800464
465 if (list_empty(&connector->probed_modes))
466 return;
467
468 if (quirks & EDID_QUIRK_PREFER_LARGE_60)
469 target_refresh = 60;
470 if (quirks & EDID_QUIRK_PREFER_LARGE_75)
471 target_refresh = 75;
472
473 preferred_mode = list_first_entry(&connector->probed_modes,
474 struct drm_display_mode, head);
475
476 list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
477 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
478
479 if (cur_mode == preferred_mode)
480 continue;
481
482 /* Largest mode is preferred */
483 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
484 preferred_mode = cur_mode;
485
486 /* At a given size, try to get closest to target refresh */
487 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
488 MODE_REFRESH_DIFF(cur_mode, target_refresh) <
489 MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
490 preferred_mode = cur_mode;
491 }
492 }
493
494 preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
495}
496
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000497struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
498 int hsize, int vsize, int fresh)
Zhao Yakui559ee212009-09-03 09:33:47 +0800499{
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000500 struct drm_display_mode *mode = NULL;
Adam Jackson07a5e632009-12-03 17:44:38 -0500501 int i;
Zhao Yakui559ee212009-09-03 09:33:47 +0800502
Adam Jackson07a5e632009-12-03 17:44:38 -0500503 for (i = 0; i < drm_num_dmt_modes; i++) {
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000504 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
Zhao Yakui559ee212009-09-03 09:33:47 +0800505 if (hsize == ptr->hdisplay &&
506 vsize == ptr->vdisplay &&
507 fresh == drm_mode_vrefresh(ptr)) {
508 /* get the expected default mode */
509 mode = drm_mode_duplicate(dev, ptr);
510 break;
511 }
512 }
513 return mode;
514}
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000515EXPORT_SYMBOL(drm_mode_find_dmt);
Adam Jackson23425ca2009-09-23 17:30:58 -0400516
Adam Jacksond1ff6402010-03-29 21:43:26 +0000517typedef void detailed_cb(struct detailed_timing *timing, void *closure);
518
519static void
Adam Jackson4d76a222010-08-03 14:38:17 -0400520cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
521{
522 int i, n = 0;
Christian Schmidt4966b2a2011-12-19 20:03:43 +0100523 u8 d = ext[0x02];
Adam Jackson4d76a222010-08-03 14:38:17 -0400524 u8 *det_base = ext + d;
525
Christian Schmidt4966b2a2011-12-19 20:03:43 +0100526 n = (127 - d) / 18;
Adam Jackson4d76a222010-08-03 14:38:17 -0400527 for (i = 0; i < n; i++)
528 cb((struct detailed_timing *)(det_base + 18 * i), closure);
529}
530
531static void
Adam Jacksoncbba98f2010-08-03 14:38:18 -0400532vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
533{
534 unsigned int i, n = min((int)ext[0x02], 6);
535 u8 *det_base = ext + 5;
536
537 if (ext[0x01] != 1)
538 return; /* unknown version */
539
540 for (i = 0; i < n; i++)
541 cb((struct detailed_timing *)(det_base + 18 * i), closure);
542}
543
544static void
Adam Jacksond1ff6402010-03-29 21:43:26 +0000545drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
546{
547 int i;
548 struct edid *edid = (struct edid *)raw_edid;
549
550 if (edid == NULL)
551 return;
552
553 for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
554 cb(&(edid->detailed_timings[i]), closure);
555
Adam Jackson4d76a222010-08-03 14:38:17 -0400556 for (i = 1; i <= raw_edid[0x7e]; i++) {
557 u8 *ext = raw_edid + (i * EDID_LENGTH);
558 switch (*ext) {
559 case CEA_EXT:
560 cea_for_each_detailed_block(ext, cb, closure);
561 break;
Adam Jacksoncbba98f2010-08-03 14:38:18 -0400562 case VTB_EXT:
563 vtb_for_each_detailed_block(ext, cb, closure);
564 break;
Adam Jackson4d76a222010-08-03 14:38:17 -0400565 default:
566 break;
567 }
568 }
Adam Jacksond1ff6402010-03-29 21:43:26 +0000569}
570
571static void
572is_rb(struct detailed_timing *t, void *data)
573{
574 u8 *r = (u8 *)t;
575 if (r[3] == EDID_DETAIL_MONITOR_RANGE)
576 if (r[15] & 0x10)
577 *(bool *)data = true;
578}
579
580/* EDID 1.4 defines this explicitly. For EDID 1.3, we guess, badly. */
581static bool
582drm_monitor_supports_rb(struct edid *edid)
583{
584 if (edid->revision >= 4) {
Daniel Vetterdec7e3a2012-06-19 11:33:06 +0200585 bool ret = false;
Adam Jacksond1ff6402010-03-29 21:43:26 +0000586 drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
587 return ret;
588 }
589
590 return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
591}
592
Adam Jackson7a374352010-03-29 21:43:30 +0000593static void
594find_gtf2(struct detailed_timing *t, void *data)
595{
596 u8 *r = (u8 *)t;
597 if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
598 *(u8 **)data = r;
599}
600
601/* Secondary GTF curve kicks in above some break frequency */
602static int
603drm_gtf2_hbreak(struct edid *edid)
604{
605 u8 *r = NULL;
606 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
607 return r ? (r[12] * 2) : 0;
608}
609
610static int
611drm_gtf2_2c(struct edid *edid)
612{
613 u8 *r = NULL;
614 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
615 return r ? r[13] : 0;
616}
617
618static int
619drm_gtf2_m(struct edid *edid)
620{
621 u8 *r = NULL;
622 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
623 return r ? (r[15] << 8) + r[14] : 0;
624}
625
626static int
627drm_gtf2_k(struct edid *edid)
628{
629 u8 *r = NULL;
630 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
631 return r ? r[16] : 0;
632}
633
634static int
635drm_gtf2_2j(struct edid *edid)
636{
637 u8 *r = NULL;
638 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
639 return r ? r[17] : 0;
640}
641
642/**
643 * standard_timing_level - get std. timing level(CVT/GTF/DMT)
644 * @edid: EDID block to scan
645 */
646static int standard_timing_level(struct edid *edid)
647{
648 if (edid->revision >= 2) {
649 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
650 return LEVEL_CVT;
651 if (drm_gtf2_hbreak(edid))
652 return LEVEL_GTF2;
653 return LEVEL_GTF;
654 }
655 return LEVEL_DMT;
656}
657
Adam Jackson23425ca2009-09-23 17:30:58 -0400658/*
659 * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
660 * monitors fill with ascii space (0x20) instead.
661 */
662static int
663bad_std_timing(u8 a, u8 b)
664{
665 return (a == 0x00 && b == 0x00) ||
666 (a == 0x01 && b == 0x01) ||
667 (a == 0x20 && b == 0x20);
668}
669
Dave Airlief453ba02008-11-07 14:05:41 -0800670/**
671 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
672 * @t: standard timing params
Zhao Yakui5c612592009-06-22 13:17:10 +0800673 * @timing_level: standard timing level
Dave Airlief453ba02008-11-07 14:05:41 -0800674 *
675 * Take the standard timing params (in this case width, aspect, and refresh)
Zhao Yakui5c612592009-06-22 13:17:10 +0800676 * and convert them into a real mode using CVT/GTF/DMT.
Dave Airlief453ba02008-11-07 14:05:41 -0800677 */
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000678static struct drm_display_mode *
Adam Jackson7a374352010-03-29 21:43:30 +0000679drm_mode_std(struct drm_connector *connector, struct edid *edid,
680 struct std_timing *t, int revision)
Dave Airlief453ba02008-11-07 14:05:41 -0800681{
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000682 struct drm_device *dev = connector->dev;
683 struct drm_display_mode *m, *mode = NULL;
Zhao Yakui5c612592009-06-22 13:17:10 +0800684 int hsize, vsize;
685 int vrefresh_rate;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200686 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
687 >> EDID_TIMING_ASPECT_SHIFT;
Zhao Yakui5c612592009-06-22 13:17:10 +0800688 unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
689 >> EDID_TIMING_VFREQ_SHIFT;
Adam Jackson7a374352010-03-29 21:43:30 +0000690 int timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800691
Adam Jackson23425ca2009-09-23 17:30:58 -0400692 if (bad_std_timing(t->hsize, t->vfreq_aspect))
693 return NULL;
694
Zhao Yakui5c612592009-06-22 13:17:10 +0800695 /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
696 hsize = t->hsize * 8 + 248;
697 /* vrefresh_rate = vfreq + 60 */
698 vrefresh_rate = vfreq + 60;
699 /* the vdisplay is calculated based on the aspect ratio */
Adam Jacksonf066a172009-09-23 17:31:21 -0400700 if (aspect_ratio == 0) {
701 if (revision < 3)
702 vsize = hsize;
703 else
704 vsize = (hsize * 10) / 16;
705 } else if (aspect_ratio == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800706 vsize = (hsize * 3) / 4;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200707 else if (aspect_ratio == 2)
Dave Airlief453ba02008-11-07 14:05:41 -0800708 vsize = (hsize * 4) / 5;
709 else
710 vsize = (hsize * 9) / 16;
Adam Jacksona0910c82010-03-29 21:43:28 +0000711
712 /* HDTV hack, part 1 */
713 if (vrefresh_rate == 60 &&
714 ((hsize == 1360 && vsize == 765) ||
715 (hsize == 1368 && vsize == 769))) {
716 hsize = 1366;
717 vsize = 768;
718 }
719
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000720 /*
721 * If this connector already has a mode for this size and refresh
722 * rate (because it came from detailed or CVT info), use that
723 * instead. This way we don't have to guess at interlace or
724 * reduced blanking.
725 */
Adam Jackson522032d2010-04-09 16:52:49 +0000726 list_for_each_entry(m, &connector->probed_modes, head)
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000727 if (m->hdisplay == hsize && m->vdisplay == vsize &&
728 drm_mode_vrefresh(m) == vrefresh_rate)
729 return NULL;
730
Adam Jacksona0910c82010-03-29 21:43:28 +0000731 /* HDTV hack, part 2 */
732 if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
733 mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
Dave Airlied50ba252009-09-23 14:44:08 +1000734 false);
Zhao Yakui559ee212009-09-03 09:33:47 +0800735 mode->hdisplay = 1366;
Adam Jacksona4967de2010-07-28 07:40:32 +1000736 mode->hsync_start = mode->hsync_start - 1;
737 mode->hsync_end = mode->hsync_end - 1;
Zhao Yakui559ee212009-09-03 09:33:47 +0800738 return mode;
739 }
Adam Jacksona0910c82010-03-29 21:43:28 +0000740
Zhao Yakui559ee212009-09-03 09:33:47 +0800741 /* check whether it can be found in default mode table */
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000742 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate);
Zhao Yakui559ee212009-09-03 09:33:47 +0800743 if (mode)
744 return mode;
745
Zhao Yakui5c612592009-06-22 13:17:10 +0800746 switch (timing_level) {
747 case LEVEL_DMT:
Zhao Yakui5c612592009-06-22 13:17:10 +0800748 break;
749 case LEVEL_GTF:
750 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
751 break;
Adam Jackson7a374352010-03-29 21:43:30 +0000752 case LEVEL_GTF2:
753 /*
754 * This is potentially wrong if there's ever a monitor with
755 * more than one ranges section, each claiming a different
756 * secondary GTF curve. Please don't do that.
757 */
758 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
759 if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
Sascha Haueraefd3302012-02-01 11:38:21 +0100760 drm_mode_destroy(dev, mode);
Adam Jackson7a374352010-03-29 21:43:30 +0000761 mode = drm_gtf_mode_complex(dev, hsize, vsize,
762 vrefresh_rate, 0, 0,
763 drm_gtf2_m(edid),
764 drm_gtf2_2c(edid),
765 drm_gtf2_k(edid),
766 drm_gtf2_2j(edid));
767 }
768 break;
Zhao Yakui5c612592009-06-22 13:17:10 +0800769 case LEVEL_CVT:
Dave Airlied50ba252009-09-23 14:44:08 +1000770 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
771 false);
Zhao Yakui5c612592009-06-22 13:17:10 +0800772 break;
773 }
Dave Airlief453ba02008-11-07 14:05:41 -0800774 return mode;
775}
776
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000777/*
778 * EDID is delightfully ambiguous about how interlaced modes are to be
779 * encoded. Our internal representation is of frame height, but some
780 * HDTV detailed timings are encoded as field height.
781 *
782 * The format list here is from CEA, in frame size. Technically we
783 * should be checking refresh rate too. Whatever.
784 */
785static void
786drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
787 struct detailed_pixel_timing *pt)
788{
789 int i;
790 static const struct {
791 int w, h;
792 } cea_interlaced[] = {
793 { 1920, 1080 },
794 { 720, 480 },
795 { 1440, 480 },
796 { 2880, 480 },
797 { 720, 576 },
798 { 1440, 576 },
799 { 2880, 576 },
800 };
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000801
802 if (!(pt->misc & DRM_EDID_PT_INTERLACED))
803 return;
804
Kulikov Vasiliy3c581412010-06-28 15:54:52 +0400805 for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000806 if ((mode->hdisplay == cea_interlaced[i].w) &&
807 (mode->vdisplay == cea_interlaced[i].h / 2)) {
808 mode->vdisplay *= 2;
809 mode->vsync_start *= 2;
810 mode->vsync_end *= 2;
811 mode->vtotal *= 2;
812 mode->vtotal |= 1;
813 }
814 }
815
816 mode->flags |= DRM_MODE_FLAG_INTERLACE;
817}
818
Dave Airlief453ba02008-11-07 14:05:41 -0800819/**
820 * drm_mode_detailed - create a new mode from an EDID detailed timing section
821 * @dev: DRM device (needed to create new mode)
822 * @edid: EDID block
823 * @timing: EDID detailed timing info
824 * @quirks: quirks to apply
825 *
826 * An EDID detailed timing block contains enough info for us to create and
827 * return a new struct drm_display_mode.
828 */
829static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
830 struct edid *edid,
831 struct detailed_timing *timing,
832 u32 quirks)
833{
834 struct drm_display_mode *mode;
835 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200836 unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
837 unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
838 unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
839 unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200840 unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
841 unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
Torsten Duwe2c5d8162013-03-23 15:38:22 +0100842 unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) << 2 | pt->vsync_offset_pulse_width_lo >> 4;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200843 unsigned vsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x3) << 4 | (pt->vsync_offset_pulse_width_lo & 0xf);
Dave Airlief453ba02008-11-07 14:05:41 -0800844
Adam Jacksonfc438962009-06-04 10:20:34 +1000845 /* ignore tiny modes */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200846 if (hactive < 64 || vactive < 64)
Adam Jacksonfc438962009-06-04 10:20:34 +1000847 return NULL;
848
Michel Dänzer0454bea2009-06-15 16:56:07 +0200849 if (pt->misc & DRM_EDID_PT_STEREO) {
Dave Airlief453ba02008-11-07 14:05:41 -0800850 printk(KERN_WARNING "stereo mode not supported\n");
851 return NULL;
852 }
Michel Dänzer0454bea2009-06-15 16:56:07 +0200853 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
Jerome Glisse79b7dcb2010-01-14 19:02:20 +0100854 printk(KERN_WARNING "composite sync not supported\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800855 }
856
Zhao Yakuifcb45612009-10-14 09:11:25 +0800857 /* it is incorrect if hsync/vsync width is zero */
858 if (!hsync_pulse_width || !vsync_pulse_width) {
859 DRM_DEBUG_KMS("Incorrect Detailed timing. "
860 "Wrong Hsync/Vsync pulse width\n");
861 return NULL;
862 }
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400863
864 if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
865 mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
866 if (!mode)
867 return NULL;
868
869 goto set_size;
870 }
871
Dave Airlief453ba02008-11-07 14:05:41 -0800872 mode = drm_mode_create(dev);
873 if (!mode)
874 return NULL;
875
Dave Airlief453ba02008-11-07 14:05:41 -0800876 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
Michel Dänzer0454bea2009-06-15 16:56:07 +0200877 timing->pixel_clock = cpu_to_le16(1088);
Dave Airlief453ba02008-11-07 14:05:41 -0800878
Michel Dänzer0454bea2009-06-15 16:56:07 +0200879 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
Dave Airlief453ba02008-11-07 14:05:41 -0800880
Michel Dänzer0454bea2009-06-15 16:56:07 +0200881 mode->hdisplay = hactive;
882 mode->hsync_start = mode->hdisplay + hsync_offset;
883 mode->hsync_end = mode->hsync_start + hsync_pulse_width;
884 mode->htotal = mode->hdisplay + hblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800885
Michel Dänzer0454bea2009-06-15 16:56:07 +0200886 mode->vdisplay = vactive;
887 mode->vsync_start = mode->vdisplay + vsync_offset;
888 mode->vsync_end = mode->vsync_start + vsync_pulse_width;
889 mode->vtotal = mode->vdisplay + vblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800890
Jesse Barnes7064fef2009-11-05 10:12:54 -0800891 /* Some EDIDs have bogus h/vtotal values */
892 if (mode->hsync_end > mode->htotal)
893 mode->htotal = mode->hsync_end + 1;
894 if (mode->vsync_end > mode->vtotal)
895 mode->vtotal = mode->vsync_end + 1;
896
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000897 drm_mode_do_interlace_quirk(mode, pt);
Dave Airlief453ba02008-11-07 14:05:41 -0800898
899 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
Michel Dänzer0454bea2009-06-15 16:56:07 +0200900 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
Dave Airlief453ba02008-11-07 14:05:41 -0800901 }
902
Michel Dänzer0454bea2009-06-15 16:56:07 +0200903 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
904 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
905 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
906 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
Dave Airlief453ba02008-11-07 14:05:41 -0800907
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400908set_size:
Michel Dänzere14cbee2009-06-23 12:36:32 +0200909 mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
910 mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
Dave Airlief453ba02008-11-07 14:05:41 -0800911
912 if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
913 mode->width_mm *= 10;
914 mode->height_mm *= 10;
915 }
916
917 if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
918 mode->width_mm = edid->width_cm * 10;
919 mode->height_mm = edid->height_cm * 10;
920 }
921
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400922 mode->type = DRM_MODE_TYPE_DRIVER;
Torsten Duwe612dade2013-03-23 15:39:34 +0100923 mode->vrefresh = drm_mode_vrefresh(mode);
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400924 drm_mode_set_name(mode);
925
Dave Airlief453ba02008-11-07 14:05:41 -0800926 return mode;
927}
928
Adam Jackson07a5e632009-12-03 17:44:38 -0500929static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000930mode_is_rb(const struct drm_display_mode *mode)
Adam Jackson07a5e632009-12-03 17:44:38 -0500931{
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000932 return (mode->htotal - mode->hdisplay == 160) &&
933 (mode->hsync_end - mode->hdisplay == 80) &&
934 (mode->hsync_end - mode->hsync_start == 32) &&
935 (mode->vsync_start - mode->vdisplay == 3);
936}
Adam Jackson07a5e632009-12-03 17:44:38 -0500937
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000938static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000939mode_in_hsync_range(const struct drm_display_mode *mode,
940 struct edid *edid, u8 *t)
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000941{
942 int hsync, hmin, hmax;
Adam Jackson07a5e632009-12-03 17:44:38 -0500943
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000944 hmin = t[7];
945 if (edid->revision >= 4)
946 hmin += ((t[4] & 0x04) ? 255 : 0);
947 hmax = t[8];
948 if (edid->revision >= 4)
949 hmax += ((t[4] & 0x08) ? 255 : 0);
Adam Jackson07a5e632009-12-03 17:44:38 -0500950 hsync = drm_mode_hsync(mode);
Adam Jackson07a5e632009-12-03 17:44:38 -0500951
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000952 return (hsync <= hmax && hsync >= hmin);
953}
954
955static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000956mode_in_vsync_range(const struct drm_display_mode *mode,
957 struct edid *edid, u8 *t)
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000958{
959 int vsync, vmin, vmax;
960
961 vmin = t[5];
962 if (edid->revision >= 4)
963 vmin += ((t[4] & 0x01) ? 255 : 0);
964 vmax = t[6];
965 if (edid->revision >= 4)
966 vmax += ((t[4] & 0x02) ? 255 : 0);
967 vsync = drm_mode_vrefresh(mode);
968
969 return (vsync <= vmax && vsync >= vmin);
970}
971
972static u32
973range_pixel_clock(struct edid *edid, u8 *t)
974{
975 /* unspecified */
976 if (t[9] == 0 || t[9] == 255)
977 return 0;
978
979 /* 1.4 with CVT support gives us real precision, yay */
980 if (edid->revision >= 4 && t[10] == 0x04)
981 return (t[9] * 10000) - ((t[12] >> 2) * 250);
982
983 /* 1.3 is pathetic, so fuzz up a bit */
984 return t[9] * 10000 + 5001;
985}
986
Adam Jackson07a5e632009-12-03 17:44:38 -0500987static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000988mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000989 struct detailed_timing *timing)
Adam Jackson07a5e632009-12-03 17:44:38 -0500990{
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000991 u32 max_clock;
992 u8 *t = (u8 *)timing;
Adam Jackson07a5e632009-12-03 17:44:38 -0500993
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000994 if (!mode_in_hsync_range(mode, edid, t))
Adam Jackson07a5e632009-12-03 17:44:38 -0500995 return false;
996
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000997 if (!mode_in_vsync_range(mode, edid, t))
Adam Jackson07a5e632009-12-03 17:44:38 -0500998 return false;
999
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001000 if ((max_clock = range_pixel_clock(edid, t)))
Adam Jackson07a5e632009-12-03 17:44:38 -05001001 if (mode->clock > max_clock)
1002 return false;
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001003
1004 /* 1.4 max horizontal check */
1005 if (edid->revision >= 4 && t[10] == 0x04)
1006 if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
1007 return false;
1008
1009 if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
1010 return false;
Adam Jackson07a5e632009-12-03 17:44:38 -05001011
1012 return true;
1013}
1014
1015/*
1016 * XXX If drm_dmt_modes ever regrows the CVT-R modes (and it will) this will
1017 * need to account for them.
1018 */
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001019static int
1020drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
1021 struct detailed_timing *timing)
Adam Jackson07a5e632009-12-03 17:44:38 -05001022{
1023 int i, modes = 0;
1024 struct drm_display_mode *newmode;
1025 struct drm_device *dev = connector->dev;
1026
1027 for (i = 0; i < drm_num_dmt_modes; i++) {
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001028 if (mode_in_range(drm_dmt_modes + i, edid, timing)) {
Adam Jackson07a5e632009-12-03 17:44:38 -05001029 newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
1030 if (newmode) {
1031 drm_mode_probed_add(connector, newmode);
1032 modes++;
1033 }
1034 }
1035 }
1036
1037 return modes;
1038}
1039
Adam Jackson13931572010-08-03 14:38:19 -04001040static void
1041do_inferred_modes(struct detailed_timing *timing, void *c)
Adam Jackson9340d8c2009-12-03 17:44:40 -05001042{
Adam Jackson13931572010-08-03 14:38:19 -04001043 struct detailed_mode_closure *closure = c;
1044 struct detailed_non_pixel *data = &timing->data.other_data;
1045 int gtf = (closure->edid->features & DRM_EDID_FEATURE_DEFAULT_GTF);
Adam Jackson9340d8c2009-12-03 17:44:40 -05001046
Adam Jackson13931572010-08-03 14:38:19 -04001047 if (gtf && data->type == EDID_DETAIL_MONITOR_RANGE)
1048 closure->modes += drm_gtf_modes_for_range(closure->connector,
1049 closure->edid,
1050 timing);
Adam Jackson9340d8c2009-12-03 17:44:40 -05001051}
1052
Adam Jackson13931572010-08-03 14:38:19 -04001053static int
1054add_inferred_modes(struct drm_connector *connector, struct edid *edid)
1055{
1056 struct detailed_mode_closure closure = {
1057 connector, edid, 0, 0, 0
1058 };
1059
1060 if (version_greater(edid, 1, 0))
1061 drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
1062 &closure);
1063
1064 return closure.modes;
1065}
1066
Adam Jackson2255be12010-03-29 21:43:22 +00001067static int
1068drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
1069{
1070 int i, j, m, modes = 0;
1071 struct drm_display_mode *mode;
1072 u8 *est = ((u8 *)timing) + 5;
1073
1074 for (i = 0; i < 6; i++) {
1075 for (j = 7; j > 0; j--) {
1076 m = (i * 8) + (7 - j);
Linus Torvaldsaa9f56b2010-08-12 09:21:39 -07001077 if (m >= ARRAY_SIZE(est3_modes))
Adam Jackson2255be12010-03-29 21:43:22 +00001078 break;
1079 if (est[i] & (1 << j)) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001080 mode = drm_mode_find_dmt(connector->dev,
1081 est3_modes[m].w,
1082 est3_modes[m].h,
1083 est3_modes[m].r
1084 /*, est3_modes[m].rb */);
Adam Jackson2255be12010-03-29 21:43:22 +00001085 if (mode) {
1086 drm_mode_probed_add(connector, mode);
1087 modes++;
1088 }
1089 }
1090 }
1091 }
1092
1093 return modes;
1094}
1095
Adam Jackson13931572010-08-03 14:38:19 -04001096static void
1097do_established_modes(struct detailed_timing *timing, void *c)
Adam Jackson9cf00972009-12-03 17:44:36 -05001098{
Adam Jackson13931572010-08-03 14:38:19 -04001099 struct detailed_mode_closure *closure = c;
Adam Jackson9cf00972009-12-03 17:44:36 -05001100 struct detailed_non_pixel *data = &timing->data.other_data;
Adam Jackson13931572010-08-03 14:38:19 -04001101
1102 if (data->type == EDID_DETAIL_EST_TIMINGS)
1103 closure->modes += drm_est3_modes(closure->connector, timing);
1104}
1105
1106/**
1107 * add_established_modes - get est. modes from EDID and add them
1108 * @edid: EDID block to scan
1109 *
1110 * Each EDID block contains a bitmap of the supported "established modes" list
1111 * (defined above). Tease them out and add them to the global modes list.
1112 */
1113static int
1114add_established_modes(struct drm_connector *connector, struct edid *edid)
1115{
Adam Jackson9cf00972009-12-03 17:44:36 -05001116 struct drm_device *dev = connector->dev;
Adam Jackson13931572010-08-03 14:38:19 -04001117 unsigned long est_bits = edid->established_timings.t1 |
1118 (edid->established_timings.t2 << 8) |
1119 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
1120 int i, modes = 0;
1121 struct detailed_mode_closure closure = {
1122 connector, edid, 0, 0, 0
1123 };
Adam Jackson9cf00972009-12-03 17:44:36 -05001124
Adam Jackson13931572010-08-03 14:38:19 -04001125 for (i = 0; i <= EDID_EST_TIMINGS; i++) {
1126 if (est_bits & (1<<i)) {
1127 struct drm_display_mode *newmode;
1128 newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
1129 if (newmode) {
1130 drm_mode_probed_add(connector, newmode);
1131 modes++;
1132 }
1133 }
Adam Jackson9cf00972009-12-03 17:44:36 -05001134 }
1135
Adam Jackson13931572010-08-03 14:38:19 -04001136 if (version_greater(edid, 1, 0))
1137 drm_for_each_detailed_block((u8 *)edid,
1138 do_established_modes, &closure);
1139
1140 return modes + closure.modes;
1141}
1142
1143static void
1144do_standard_modes(struct detailed_timing *timing, void *c)
1145{
1146 struct detailed_mode_closure *closure = c;
1147 struct detailed_non_pixel *data = &timing->data.other_data;
1148 struct drm_connector *connector = closure->connector;
1149 struct edid *edid = closure->edid;
1150
1151 if (data->type == EDID_DETAIL_STD_MODES) {
1152 int i;
Adam Jackson9cf00972009-12-03 17:44:36 -05001153 for (i = 0; i < 6; i++) {
1154 struct std_timing *std;
1155 struct drm_display_mode *newmode;
1156
1157 std = &data->data.timings[i];
Adam Jackson7a374352010-03-29 21:43:30 +00001158 newmode = drm_mode_std(connector, edid, std,
1159 edid->revision);
Adam Jackson9cf00972009-12-03 17:44:36 -05001160 if (newmode) {
1161 drm_mode_probed_add(connector, newmode);
Adam Jackson13931572010-08-03 14:38:19 -04001162 closure->modes++;
Adam Jackson9cf00972009-12-03 17:44:36 -05001163 }
1164 }
Adam Jackson13931572010-08-03 14:38:19 -04001165 }
1166}
1167
1168/**
1169 * add_standard_modes - get std. modes from EDID and add them
1170 * @edid: EDID block to scan
1171 *
1172 * Standard modes can be calculated using the appropriate standard (DMT,
1173 * GTF or CVT. Grab them from @edid and add them to the list.
1174 */
1175static int
1176add_standard_modes(struct drm_connector *connector, struct edid *edid)
1177{
1178 int i, modes = 0;
1179 struct detailed_mode_closure closure = {
1180 connector, edid, 0, 0, 0
1181 };
1182
1183 for (i = 0; i < EDID_STD_TIMINGS; i++) {
1184 struct drm_display_mode *newmode;
1185
1186 newmode = drm_mode_std(connector, edid,
1187 &edid->standard_timings[i],
1188 edid->revision);
1189 if (newmode) {
1190 drm_mode_probed_add(connector, newmode);
1191 modes++;
1192 }
1193 }
1194
1195 if (version_greater(edid, 1, 0))
1196 drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
1197 &closure);
1198
1199 /* XXX should also look for standard codes in VTB blocks */
1200
1201 return modes + closure.modes;
1202}
1203
Dave Airlief453ba02008-11-07 14:05:41 -08001204static int drm_cvt_modes(struct drm_connector *connector,
1205 struct detailed_timing *timing)
1206{
1207 int i, j, modes = 0;
1208 struct drm_display_mode *newmode;
1209 struct drm_device *dev = connector->dev;
Zhao Yakui5c612592009-06-22 13:17:10 +08001210 struct cvt_timing *cvt;
1211 const int rates[] = { 60, 85, 75, 60, 50 };
1212 const u8 empty[3] = { 0, 0, 0 };
Dave Airlief453ba02008-11-07 14:05:41 -08001213
1214 for (i = 0; i < 4; i++) {
1215 int uninitialized_var(width), height;
1216 cvt = &(timing->data.other_data.data.cvt[i]);
1217
1218 if (!memcmp(cvt->code, empty, 3))
Michel Dänzer0454bea2009-06-15 16:56:07 +02001219 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08001220
1221 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
Zhao Yakui5c612592009-06-22 13:17:10 +08001222 switch (cvt->code[1] & 0x0c) {
Adam Jacksonf066a172009-09-23 17:31:21 -04001223 case 0x00:
Dave Airlief453ba02008-11-07 14:05:41 -08001224 width = height * 4 / 3;
1225 break;
1226 case 0x04:
1227 width = height * 16 / 9;
1228 break;
1229 case 0x08:
1230 width = height * 16 / 10;
1231 break;
1232 case 0x0c:
Dave Airlief453ba02008-11-07 14:05:41 -08001233 width = height * 15 / 9;
1234 break;
1235 }
1236
1237 for (j = 1; j < 5; j++) {
1238 if (cvt->code[2] & (1 << j)) {
1239 newmode = drm_cvt_mode(dev, width, height,
1240 rates[j], j == 0,
1241 false, false);
1242 if (newmode) {
1243 drm_mode_probed_add(connector, newmode);
1244 modes++;
1245 }
1246 }
1247 }
1248 }
1249
1250 return modes;
1251}
1252
Adam Jackson13931572010-08-03 14:38:19 -04001253static void
1254do_cvt_mode(struct detailed_timing *timing, void *c)
1255{
1256 struct detailed_mode_closure *closure = c;
1257 struct detailed_non_pixel *data = &timing->data.other_data;
1258
1259 if (data->type == EDID_DETAIL_CVT_3BYTE)
1260 closure->modes += drm_cvt_modes(closure->connector, timing);
1261}
Adam Jackson9cf00972009-12-03 17:44:36 -05001262
1263static int
Adam Jackson13931572010-08-03 14:38:19 -04001264add_cvt_modes(struct drm_connector *connector, struct edid *edid)
1265{
1266 struct detailed_mode_closure closure = {
1267 connector, edid, 0, 0, 0
1268 };
Adam Jackson9cf00972009-12-03 17:44:36 -05001269
Adam Jackson13931572010-08-03 14:38:19 -04001270 if (version_greater(edid, 1, 2))
1271 drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
Dave Airlief453ba02008-11-07 14:05:41 -08001272
Adam Jackson13931572010-08-03 14:38:19 -04001273 /* XXX should also look for CVT codes in VTB blocks */
1274
1275 return closure.modes;
Dave Airlief453ba02008-11-07 14:05:41 -08001276}
1277
Adam Jackson13931572010-08-03 14:38:19 -04001278static void
1279do_detailed_mode(struct detailed_timing *timing, void *c)
Dave Airlief453ba02008-11-07 14:05:41 -08001280{
Adam Jackson13931572010-08-03 14:38:19 -04001281 struct detailed_mode_closure *closure = c;
Dave Airlief453ba02008-11-07 14:05:41 -08001282 struct drm_display_mode *newmode;
Adam Jackson9cf00972009-12-03 17:44:36 -05001283
1284 if (timing->pixel_clock) {
Adam Jackson13931572010-08-03 14:38:19 -04001285 newmode = drm_mode_detailed(closure->connector->dev,
1286 closure->edid, timing,
1287 closure->quirks);
Dave Airlief453ba02008-11-07 14:05:41 -08001288 if (!newmode)
Adam Jackson13931572010-08-03 14:38:19 -04001289 return;
Adam Jackson9cf00972009-12-03 17:44:36 -05001290
Adam Jackson13931572010-08-03 14:38:19 -04001291 if (closure->preferred)
Dave Airlief453ba02008-11-07 14:05:41 -08001292 newmode->type |= DRM_MODE_TYPE_PREFERRED;
1293
Adam Jackson13931572010-08-03 14:38:19 -04001294 drm_mode_probed_add(closure->connector, newmode);
1295 closure->modes++;
1296 closure->preferred = 0;
Zhao Yakui882f0212009-08-26 18:20:49 +08001297 }
Ma Ling167f3a02009-03-20 14:09:48 +08001298}
1299
Adam Jackson13931572010-08-03 14:38:19 -04001300/*
1301 * add_detailed_modes - Add modes from detailed timings
Dave Airlief453ba02008-11-07 14:05:41 -08001302 * @connector: attached connector
1303 * @edid: EDID block to scan
1304 * @quirks: quirks to apply
Dave Airlief453ba02008-11-07 14:05:41 -08001305 */
Adam Jackson13931572010-08-03 14:38:19 -04001306static int
1307add_detailed_modes(struct drm_connector *connector, struct edid *edid,
1308 u32 quirks)
Dave Airlief453ba02008-11-07 14:05:41 -08001309{
Adam Jackson13931572010-08-03 14:38:19 -04001310 struct detailed_mode_closure closure = {
1311 connector,
1312 edid,
1313 1,
1314 quirks,
1315 0
1316 };
Dave Airlief453ba02008-11-07 14:05:41 -08001317
Adam Jackson13931572010-08-03 14:38:19 -04001318 if (closure.preferred && !version_greater(edid, 1, 3))
1319 closure.preferred =
1320 (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
Adam Jacksona327f6b2010-03-29 21:43:25 +00001321
Adam Jackson13931572010-08-03 14:38:19 -04001322 drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
Dave Airlief453ba02008-11-07 14:05:41 -08001323
Adam Jackson13931572010-08-03 14:38:19 -04001324 return closure.modes;
Zhao Yakui882f0212009-08-26 18:20:49 +08001325}
Dave Airlief453ba02008-11-07 14:05:41 -08001326
Ma Lingf23c20c2009-03-26 19:26:23 +08001327#define HDMI_IDENTIFIER 0x000C03
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001328#define AUDIO_BLOCK 0x01
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001329#define VIDEO_BLOCK 0x02
Ma Lingf23c20c2009-03-26 19:26:23 +08001330#define VENDOR_BLOCK 0x03
Wu Fengguang76adaa32011-09-05 14:23:20 +08001331#define SPEAKER_BLOCK 0x04
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001332#define EDID_BASIC_AUDIO (1 << 6)
1333
1334/**
1335 * Search EDID for CEA extension block.
1336 */
Ben Skeggseccaca22011-03-30 05:03:47 +00001337u8 *drm_find_cea_extension(struct edid *edid)
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001338{
1339 u8 *edid_ext = NULL;
1340 int i;
1341
1342 /* No EDID or EDID extensions */
1343 if (edid == NULL || edid->extensions == 0)
1344 return NULL;
1345
1346 /* Find CEA extension */
1347 for (i = 0; i < edid->extensions; i++) {
1348 edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
1349 if (edid_ext[0] == CEA_EXT)
1350 break;
1351 }
1352
1353 if (i == edid->extensions)
1354 return NULL;
1355
1356 return edid_ext;
1357}
Ben Skeggseccaca22011-03-30 05:03:47 +00001358EXPORT_SYMBOL(drm_find_cea_extension);
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001359
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001360static int
1361do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
1362{
1363 struct drm_device *dev = connector->dev;
1364 u8 * mode, cea_mode;
1365 int modes = 0;
1366
1367 for (mode = db; mode < db + len; mode++) {
1368 cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
1369 if (cea_mode < drm_num_cea_modes) {
1370 struct drm_display_mode *newmode;
1371 newmode = drm_mode_duplicate(dev,
1372 &edid_cea_modes[cea_mode]);
1373 if (newmode) {
1374 drm_mode_probed_add(connector, newmode);
1375 modes++;
1376 }
1377 }
1378 }
1379
1380 return modes;
1381}
1382
1383static int
1384add_cea_modes(struct drm_connector *connector, struct edid *edid)
1385{
1386 u8 * cea = drm_find_cea_extension(edid);
1387 u8 * db, dbl;
1388 int modes = 0;
1389
1390 if (cea && cea[1] >= 3) {
1391 for (db = cea + 4; db < cea + cea[2]; db += dbl + 1) {
1392 dbl = db[0] & 0x1f;
1393 if (((db[0] & 0xe0) >> 5) == VIDEO_BLOCK)
1394 modes += do_cea_modes (connector, db+1, dbl);
1395 }
1396 }
1397
1398 return modes;
1399}
1400
Wu Fengguang76adaa32011-09-05 14:23:20 +08001401static void
1402parse_hdmi_vsdb(struct drm_connector *connector, uint8_t *db)
1403{
1404 connector->eld[5] |= (db[6] >> 7) << 1; /* Supports_AI */
1405
1406 connector->dvi_dual = db[6] & 1;
1407 connector->max_tmds_clock = db[7] * 5;
1408
1409 connector->latency_present[0] = db[8] >> 7;
1410 connector->latency_present[1] = (db[8] >> 6) & 1;
1411 connector->video_latency[0] = db[9];
1412 connector->audio_latency[0] = db[10];
1413 connector->video_latency[1] = db[11];
1414 connector->audio_latency[1] = db[12];
1415
1416 DRM_LOG_KMS("HDMI: DVI dual %d, "
1417 "max TMDS clock %d, "
1418 "latency present %d %d, "
1419 "video latency %d %d, "
1420 "audio latency %d %d\n",
1421 connector->dvi_dual,
1422 connector->max_tmds_clock,
1423 (int) connector->latency_present[0],
1424 (int) connector->latency_present[1],
1425 connector->video_latency[0],
1426 connector->video_latency[1],
1427 connector->audio_latency[0],
1428 connector->audio_latency[1]);
1429}
1430
1431static void
1432monitor_name(struct detailed_timing *t, void *data)
1433{
1434 if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
1435 *(u8 **)data = t->data.other_data.data.str.str;
1436}
1437
1438/**
1439 * drm_edid_to_eld - build ELD from EDID
1440 * @connector: connector corresponding to the HDMI/DP sink
1441 * @edid: EDID to parse
1442 *
1443 * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
1444 * Some ELD fields are left to the graphics driver caller:
1445 * - Conn_Type
1446 * - HDCP
1447 * - Port_ID
1448 */
1449void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
1450{
1451 uint8_t *eld = connector->eld;
1452 u8 *cea;
1453 u8 *name;
1454 u8 *db;
1455 int sad_count = 0;
1456 int mnl;
1457 int dbl;
1458
1459 memset(eld, 0, sizeof(connector->eld));
1460
1461 cea = drm_find_cea_extension(edid);
1462 if (!cea) {
1463 DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
1464 return;
1465 }
1466
1467 name = NULL;
1468 drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
1469 for (mnl = 0; name && mnl < 13; mnl++) {
1470 if (name[mnl] == 0x0a)
1471 break;
1472 eld[20 + mnl] = name[mnl];
1473 }
1474 eld[4] = (cea[1] << 5) | mnl;
1475 DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
1476
1477 eld[0] = 2 << 3; /* ELD version: 2 */
1478
1479 eld[16] = edid->mfg_id[0];
1480 eld[17] = edid->mfg_id[1];
1481 eld[18] = edid->prod_code[0];
1482 eld[19] = edid->prod_code[1];
1483
Christian Schmidta0ab7342011-12-19 20:03:38 +01001484 if (cea[1] >= 3)
1485 for (db = cea + 4; db < cea + cea[2]; db += dbl + 1) {
1486 dbl = db[0] & 0x1f;
1487
1488 switch ((db[0] & 0xe0) >> 5) {
1489 case AUDIO_BLOCK:
1490 /* Audio Data Block, contains SADs */
1491 sad_count = dbl / 3;
1492 memcpy(eld + 20 + mnl, &db[1], dbl);
1493 break;
1494 case SPEAKER_BLOCK:
1495 /* Speaker Allocation Data Block */
1496 eld[7] = db[1];
1497 break;
1498 case VENDOR_BLOCK:
1499 /* HDMI Vendor-Specific Data Block */
1500 if (db[1] == 0x03 && db[2] == 0x0c && db[3] == 0)
1501 parse_hdmi_vsdb(connector, db);
1502 break;
1503 default:
1504 break;
1505 }
Wu Fengguang76adaa32011-09-05 14:23:20 +08001506 }
Wu Fengguang76adaa32011-09-05 14:23:20 +08001507 eld[5] |= sad_count << 4;
1508 eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
1509
1510 DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
1511}
1512EXPORT_SYMBOL(drm_edid_to_eld);
1513
1514/**
1515 * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
1516 * @connector: connector associated with the HDMI/DP sink
1517 * @mode: the display mode
1518 */
1519int drm_av_sync_delay(struct drm_connector *connector,
1520 struct drm_display_mode *mode)
1521{
1522 int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1523 int a, v;
1524
1525 if (!connector->latency_present[0])
1526 return 0;
1527 if (!connector->latency_present[1])
1528 i = 0;
1529
1530 a = connector->audio_latency[i];
1531 v = connector->video_latency[i];
1532
1533 /*
1534 * HDMI/DP sink doesn't support audio or video?
1535 */
1536 if (a == 255 || v == 255)
1537 return 0;
1538
1539 /*
1540 * Convert raw EDID values to millisecond.
1541 * Treat unknown latency as 0ms.
1542 */
1543 if (a)
1544 a = min(2 * (a - 1), 500);
1545 if (v)
1546 v = min(2 * (v - 1), 500);
1547
1548 return max(v - a, 0);
1549}
1550EXPORT_SYMBOL(drm_av_sync_delay);
1551
1552/**
1553 * drm_select_eld - select one ELD from multiple HDMI/DP sinks
1554 * @encoder: the encoder just changed display mode
1555 * @mode: the adjusted display mode
1556 *
1557 * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
1558 * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
1559 */
1560struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
1561 struct drm_display_mode *mode)
1562{
1563 struct drm_connector *connector;
1564 struct drm_device *dev = encoder->dev;
1565
1566 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1567 if (connector->encoder == encoder && connector->eld[0])
1568 return connector;
1569
1570 return NULL;
1571}
1572EXPORT_SYMBOL(drm_select_eld);
1573
Ma Lingf23c20c2009-03-26 19:26:23 +08001574/**
1575 * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
1576 * @edid: monitor EDID information
1577 *
1578 * Parse the CEA extension according to CEA-861-B.
1579 * Return true if HDMI, false if not or unknown.
1580 */
1581bool drm_detect_hdmi_monitor(struct edid *edid)
1582{
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001583 u8 *edid_ext;
Adam Jackson7466f4c2010-03-29 21:43:23 +00001584 int i, hdmi_id;
Ma Lingf23c20c2009-03-26 19:26:23 +08001585 int start_offset, end_offset;
1586 bool is_hdmi = false;
1587
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001588 edid_ext = drm_find_cea_extension(edid);
1589 if (!edid_ext)
Ma Lingf23c20c2009-03-26 19:26:23 +08001590 goto end;
1591
1592 /* Data block offset in CEA extension block */
1593 start_offset = 4;
1594 end_offset = edid_ext[2];
1595
1596 /*
1597 * Because HDMI identifier is in Vendor Specific Block,
1598 * search it from all data blocks of CEA extension.
1599 */
1600 for (i = start_offset; i < end_offset;
1601 /* Increased by data block len */
1602 i += ((edid_ext[i] & 0x1f) + 1)) {
1603 /* Find vendor specific block */
1604 if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
1605 hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
1606 edid_ext[i + 3] << 16;
1607 /* Find HDMI identifier */
1608 if (hdmi_id == HDMI_IDENTIFIER)
1609 is_hdmi = true;
1610 break;
1611 }
1612 }
1613
1614end:
1615 return is_hdmi;
1616}
1617EXPORT_SYMBOL(drm_detect_hdmi_monitor);
1618
Dave Airlief453ba02008-11-07 14:05:41 -08001619/**
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001620 * drm_detect_monitor_audio - check monitor audio capability
1621 *
1622 * Monitor should have CEA extension block.
1623 * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
1624 * audio' only. If there is any audio extension block and supported
1625 * audio format, assume at least 'basic audio' support, even if 'basic
1626 * audio' is not defined in EDID.
1627 *
1628 */
1629bool drm_detect_monitor_audio(struct edid *edid)
1630{
1631 u8 *edid_ext;
1632 int i, j;
1633 bool has_audio = false;
1634 int start_offset, end_offset;
1635
1636 edid_ext = drm_find_cea_extension(edid);
1637 if (!edid_ext)
1638 goto end;
1639
1640 has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
1641
1642 if (has_audio) {
1643 DRM_DEBUG_KMS("Monitor has basic audio support\n");
1644 goto end;
1645 }
1646
1647 /* Data block offset in CEA extension block */
1648 start_offset = 4;
1649 end_offset = edid_ext[2];
1650
1651 for (i = start_offset; i < end_offset;
1652 i += ((edid_ext[i] & 0x1f) + 1)) {
1653 if ((edid_ext[i] >> 5) == AUDIO_BLOCK) {
1654 has_audio = true;
1655 for (j = 1; j < (edid_ext[i] & 0x1f); j += 3)
1656 DRM_DEBUG_KMS("CEA audio format %d\n",
1657 (edid_ext[i + j] >> 3) & 0xf);
1658 goto end;
1659 }
1660 }
1661end:
1662 return has_audio;
1663}
1664EXPORT_SYMBOL(drm_detect_monitor_audio);
1665
1666/**
Jesse Barnes3b112282011-04-15 12:49:23 -07001667 * drm_add_display_info - pull display info out if present
1668 * @edid: EDID data
1669 * @info: display info (attached to connector)
1670 *
1671 * Grab any available display info and stuff it into the drm_display_info
1672 * structure that's part of the connector. Useful for tracking bpp and
1673 * color spaces.
1674 */
1675static void drm_add_display_info(struct edid *edid,
1676 struct drm_display_info *info)
1677{
Jesse Barnesebec9a72011-08-03 09:22:54 -07001678 u8 *edid_ext;
1679
Jesse Barnes3b112282011-04-15 12:49:23 -07001680 info->width_mm = edid->width_cm * 10;
1681 info->height_mm = edid->height_cm * 10;
1682
1683 /* driver figures it out in this case */
1684 info->bpc = 0;
Jesse Barnesda05a5a2011-04-15 13:48:57 -07001685 info->color_formats = 0;
Jesse Barnes3b112282011-04-15 12:49:23 -07001686
1687 /* Only defined for 1.4 with digital displays */
1688 if (edid->revision < 4)
1689 return;
1690
1691 if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
1692 return;
1693
1694 switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
1695 case DRM_EDID_DIGITAL_DEPTH_6:
1696 info->bpc = 6;
1697 break;
1698 case DRM_EDID_DIGITAL_DEPTH_8:
1699 info->bpc = 8;
1700 break;
1701 case DRM_EDID_DIGITAL_DEPTH_10:
1702 info->bpc = 10;
1703 break;
1704 case DRM_EDID_DIGITAL_DEPTH_12:
1705 info->bpc = 12;
1706 break;
1707 case DRM_EDID_DIGITAL_DEPTH_14:
1708 info->bpc = 14;
1709 break;
1710 case DRM_EDID_DIGITAL_DEPTH_16:
1711 info->bpc = 16;
1712 break;
1713 case DRM_EDID_DIGITAL_DEPTH_UNDEF:
1714 default:
1715 info->bpc = 0;
1716 break;
1717 }
Jesse Barnesda05a5a2011-04-15 13:48:57 -07001718
1719 info->color_formats = DRM_COLOR_FORMAT_RGB444;
1720 if (info->color_formats & DRM_EDID_FEATURE_RGB_YCRCB444)
1721 info->color_formats = DRM_COLOR_FORMAT_YCRCB444;
1722 if (info->color_formats & DRM_EDID_FEATURE_RGB_YCRCB422)
1723 info->color_formats = DRM_COLOR_FORMAT_YCRCB422;
Jesse Barnesebec9a72011-08-03 09:22:54 -07001724
1725 /* Get data from CEA blocks if present */
1726 edid_ext = drm_find_cea_extension(edid);
1727 if (!edid_ext)
1728 return;
1729
1730 info->cea_rev = edid_ext[1];
Jesse Barnes3b112282011-04-15 12:49:23 -07001731}
1732
1733/**
Dave Airlief453ba02008-11-07 14:05:41 -08001734 * drm_add_edid_modes - add modes from EDID data, if available
1735 * @connector: connector we're probing
1736 * @edid: edid data
1737 *
1738 * Add the specified modes to the connector's mode list.
1739 *
1740 * Return number of modes added or 0 if we couldn't find any.
1741 */
1742int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
1743{
1744 int num_modes = 0;
1745 u32 quirks;
1746
1747 if (edid == NULL) {
1748 return 0;
1749 }
Alex Deucher3c537882010-02-05 04:21:19 -05001750 if (!drm_edid_is_valid(edid)) {
Jordan Crousedcdb1672010-05-27 13:40:25 -06001751 dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001752 drm_get_connector_name(connector));
1753 return 0;
1754 }
1755
1756 quirks = edid_get_quirks(edid);
1757
Adam Jacksonc867df72010-03-29 21:43:21 +00001758 /*
1759 * EDID spec says modes should be preferred in this order:
1760 * - preferred detailed mode
1761 * - other detailed modes from base block
1762 * - detailed modes from extension blocks
1763 * - CVT 3-byte code modes
1764 * - standard timing codes
1765 * - established timing codes
1766 * - modes inferred from GTF or CVT range information
1767 *
Adam Jackson13931572010-08-03 14:38:19 -04001768 * We get this pretty much right.
Adam Jacksonc867df72010-03-29 21:43:21 +00001769 *
1770 * XXX order for additional mode types in extension blocks?
1771 */
Adam Jackson13931572010-08-03 14:38:19 -04001772 num_modes += add_detailed_modes(connector, edid, quirks);
1773 num_modes += add_cvt_modes(connector, edid);
Adam Jacksonc867df72010-03-29 21:43:21 +00001774 num_modes += add_standard_modes(connector, edid);
1775 num_modes += add_established_modes(connector, edid);
Paulo Zanoni7b480b02013-02-15 13:36:27 -02001776 if (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF)
1777 num_modes += add_inferred_modes(connector, edid);
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001778 num_modes += add_cea_modes(connector, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08001779
1780 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
1781 edid_fixup_preferred(connector, quirks);
1782
Jesse Barnes3b112282011-04-15 12:49:23 -07001783 drm_add_display_info(edid, &connector->display_info);
Dave Airlief453ba02008-11-07 14:05:41 -08001784
1785 return num_modes;
1786}
1787EXPORT_SYMBOL(drm_add_edid_modes);
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001788
1789/**
1790 * drm_add_modes_noedid - add modes for the connectors without EDID
1791 * @connector: connector we're probing
1792 * @hdisplay: the horizontal display limit
1793 * @vdisplay: the vertical display limit
1794 *
1795 * Add the specified modes to the connector's mode list. Only when the
1796 * hdisplay/vdisplay is not beyond the given limit, it will be added.
1797 *
1798 * Return number of modes added or 0 if we couldn't find any.
1799 */
1800int drm_add_modes_noedid(struct drm_connector *connector,
1801 int hdisplay, int vdisplay)
1802{
1803 int i, count, num_modes = 0;
Chris Wilsonb1f559e2011-01-26 09:49:47 +00001804 struct drm_display_mode *mode;
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001805 struct drm_device *dev = connector->dev;
1806
1807 count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
1808 if (hdisplay < 0)
1809 hdisplay = 0;
1810 if (vdisplay < 0)
1811 vdisplay = 0;
1812
1813 for (i = 0; i < count; i++) {
Chris Wilsonb1f559e2011-01-26 09:49:47 +00001814 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001815 if (hdisplay && vdisplay) {
1816 /*
1817 * Only when two are valid, they will be used to check
1818 * whether the mode should be added to the mode list of
1819 * the connector.
1820 */
1821 if (ptr->hdisplay > hdisplay ||
1822 ptr->vdisplay > vdisplay)
1823 continue;
1824 }
Adam Jacksonf985ded2009-11-23 14:23:04 -05001825 if (drm_mode_vrefresh(ptr) > 61)
1826 continue;
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001827 mode = drm_mode_duplicate(dev, ptr);
1828 if (mode) {
1829 drm_mode_probed_add(connector, mode);
1830 num_modes++;
1831 }
1832 }
1833 return num_modes;
1834}
1835EXPORT_SYMBOL(drm_add_modes_noedid);