blob: d75dccb562f35f287c7925746d2d4318fed216da [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)
Rafał Miłeckib2664052013-12-07 13:22:42 +010071/* Force 8bpc */
72#define EDID_QUIRK_FORCE_8BPC (1 << 8)
Alex Deucher3c537882010-02-05 04:21:19 -050073
Adam Jackson13931572010-08-03 14:38:19 -040074struct detailed_mode_closure {
75 struct drm_connector *connector;
76 struct edid *edid;
77 bool preferred;
78 u32 quirks;
79 int modes;
80};
Dave Airlief453ba02008-11-07 14:05:41 -080081
Zhao Yakui5c612592009-06-22 13:17:10 +080082#define LEVEL_DMT 0
83#define LEVEL_GTF 1
Adam Jackson7a374352010-03-29 21:43:30 +000084#define LEVEL_GTF2 2
85#define LEVEL_CVT 3
Zhao Yakui5c612592009-06-22 13:17:10 +080086
Dave Airlief453ba02008-11-07 14:05:41 -080087static 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 },
Adam Jacksonba1163d2010-04-06 16:11:00 +0000105 /* Envision EN2028 */
106 { "EPI", 8232, EDID_QUIRK_PREFER_LARGE_60 },
Dave Airlief453ba02008-11-07 14:05:41 -0800107
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 },
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400127
128 /* ViewSonic VA2026w */
129 { "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
Alex Deucherd060c0b2013-08-12 11:04:29 -0400130
131 /* Medion MD 30217 PG */
132 { "MED", 0x7b8, EDID_QUIRK_PREFER_LARGE_75 },
Rafał Miłeckib2664052013-12-07 13:22:42 +0100133
134 /* Panel in Samsung NP700G7A-S01PL notebook reports 6bpc */
135 { "SEC", 0xd033, EDID_QUIRK_FORCE_8BPC },
Dave Airlief453ba02008-11-07 14:05:41 -0800136};
137
Adam Jackson61e57a82010-03-29 21:43:18 +0000138/*** DDC fetch and block validation ***/
Dave Airlief453ba02008-11-07 14:05:41 -0800139
Adam Jackson083ae052009-09-23 17:30:45 -0400140static const u8 edid_header[] = {
141 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
142};
Dave Airlief453ba02008-11-07 14:05:41 -0800143
Thomas Reim051963d2011-07-29 14:28:57 +0000144 /*
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 */
148int 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}
158EXPORT_SYMBOL(drm_edid_header_is_valid);
159
160
Adam Jackson61e57a82010-03-29 21:43:18 +0000161/*
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.
Dave Airlief453ba02008-11-07 14:05:41 -0800164 */
Carsten Emdeda0df922012-03-18 22:37:33 +0100165bool drm_edid_block_valid(u8 *raw_edid)
Dave Airlief453ba02008-11-07 14:05:41 -0800166{
Adam Jackson61e57a82010-03-29 21:43:18 +0000167 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800168 u8 csum = 0;
Adam Jackson61e57a82010-03-29 21:43:18 +0000169 struct edid *edid = (struct edid *)raw_edid;
Dave Airlief453ba02008-11-07 14:05:41 -0800170
Adam Jackson61e57a82010-03-29 21:43:18 +0000171 if (raw_edid[0] == 0x00) {
Thomas Reim051963d2011-07-29 14:28:57 +0000172 int score = drm_edid_header_is_valid(raw_edid);
Adam Jackson61e57a82010-03-29 21:43:18 +0000173 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 }
Dave Airlief453ba02008-11-07 14:05:41 -0800181
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);
Adam Jackson4a638b42010-05-25 16:33:09 -0400186
187 /* allow CEA to slide through, switches mangle this */
188 if (raw_edid[0] != 0x02)
189 goto bad;
Dave Airlief453ba02008-11-07 14:05:41 -0800190 }
191
Adam Jackson61e57a82010-03-29 21:43:18 +0000192 /* 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 }
Adam Jackson862b89c2009-11-23 14:23:06 -0500199
Adam Jackson61e57a82010-03-29 21:43:18 +0000200 if (edid->revision > 4)
201 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
202 break;
203
204 default:
205 break;
206 }
Adam Jackson47ee4cc2009-11-23 14:23:05 -0500207
Dave Airlief453ba02008-11-07 14:05:41 -0800208 return 1;
209
210bad:
211 if (raw_edid) {
Dave Airlief49dadb2011-06-14 06:13:54 +0000212 printk(KERN_ERR "Raw EDID:\n");
Tormod Volden0aff47f2011-07-05 20:12:53 +0000213 print_hex_dump(KERN_ERR, " \t", DUMP_PREFIX_NONE, 16, 1,
214 raw_edid, EDID_LENGTH, false);
Dave Airlief453ba02008-11-07 14:05:41 -0800215 }
216 return 0;
217}
Carsten Emdeda0df922012-03-18 22:37:33 +0100218EXPORT_SYMBOL(drm_edid_block_valid);
Adam Jackson61e57a82010-03-29 21:43:18 +0000219
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 */
226bool 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}
Alex Deucher3c537882010-02-05 04:21:19 -0500240EXPORT_SYMBOL(drm_edid_is_valid);
Dave Airlief453ba02008-11-07 14:05:41 -0800241
Adam Jackson61e57a82010-03-29 21:43:18 +0000242#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 */
253static int
254drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
255 int block, int len)
256{
257 unsigned char start = block * EDID_LENGTH;
Chris Wilson4819d2e2011-03-15 11:04:41 +0000258 int ret, retries = 5;
Adam Jackson61e57a82010-03-29 21:43:18 +0000259
Chris Wilson4819d2e2011-03-15 11:04:41 +0000260 /* 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);
Eugeni Dodonov9292f372012-01-05 09:34:28 -0200281 if (ret == -ENXIO) {
282 DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
283 adapter->name);
284 break;
285 }
Chris Wilson4819d2e2011-03-15 11:04:41 +0000286 } while (ret != 2 && --retries);
Adam Jackson61e57a82010-03-29 21:43:18 +0000287
Chris Wilson4819d2e2011-03-15 11:04:41 +0000288 return ret == 2 ? 0 : -1;
Adam Jackson61e57a82010-03-29 21:43:18 +0000289}
290
Dave Airlie4a9a8b72011-06-14 06:13:55 +0000291static 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
Adam Jackson61e57a82010-03-29 21:43:18 +0000302static u8 *
303drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
304{
Sam Tygier0ea75e22010-09-23 10:11:01 +0100305 int i, j = 0, valid_extensions = 0;
Adam Jackson61e57a82010-03-29 21:43:18 +0000306 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;
Dave Airlie4a9a8b72011-06-14 06:13:55 +0000317 if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
318 connector->null_edid_counter++;
319 goto carp;
320 }
Adam Jackson61e57a82010-03-29 21:43:18 +0000321 }
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++) {
Sam Tygier0ea75e22010-09-23 10:11:01 +0100336 if (drm_do_probe_ddc_edid(adapter,
337 block + (valid_extensions + 1) * EDID_LENGTH,
338 j, EDID_LENGTH))
Adam Jackson61e57a82010-03-29 21:43:18 +0000339 goto out;
Sam Tygier0ea75e22010-09-23 10:11:01 +0100340 if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH)) {
341 valid_extensions++;
Adam Jackson61e57a82010-03-29 21:43:18 +0000342 break;
Sam Tygier0ea75e22010-09-23 10:11:01 +0100343 }
Adam Jackson61e57a82010-03-29 21:43:18 +0000344 }
345 if (i == 4)
Sam Tygier0ea75e22010-09-23 10:11:01 +0100346 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;
Adam Jackson61e57a82010-03-29 21:43:18 +0000358 }
359
360 return block;
361
362carp:
Jordan Crousedcdb1672010-05-27 13:40:25 -0600363 dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
Adam Jackson61e57a82010-03-29 21:43:18 +0000364 drm_get_connector_name(connector), j);
365
366out:
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 */
377static bool
378drm_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 */
395struct 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}
408EXPORT_SYMBOL(drm_get_edid);
409
410/*** EDID parsing ***/
411
Dave Airlief453ba02008-11-07 14:05:41 -0800412/**
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 */
419static 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)) + '@';
Dave Airlie16456c82009-04-03 09:10:33 +1000426 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
Dave Airlief453ba02008-11-07 14:05:41 -0800427
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 */
437static 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
Dave Airlief453ba02008-11-07 14:05:41 -0800456/**
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 */
464static void edid_fixup_preferred(struct drm_connector *connector,
465 u32 quirks)
466{
467 struct drm_display_mode *t, *cur_mode, *preferred_mode;
Dave Airlief8906072008-12-18 16:59:02 +1000468 int target_refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800469
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
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000502struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
503 int hsize, int vsize, int fresh)
Zhao Yakui559ee212009-09-03 09:33:47 +0800504{
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000505 struct drm_display_mode *mode = NULL;
Adam Jackson07a5e632009-12-03 17:44:38 -0500506 int i;
Zhao Yakui559ee212009-09-03 09:33:47 +0800507
Adam Jackson07a5e632009-12-03 17:44:38 -0500508 for (i = 0; i < drm_num_dmt_modes; i++) {
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000509 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
Zhao Yakui559ee212009-09-03 09:33:47 +0800510 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}
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000520EXPORT_SYMBOL(drm_mode_find_dmt);
Adam Jackson23425ca2009-09-23 17:30:58 -0400521
Adam Jacksond1ff6402010-03-29 21:43:26 +0000522typedef void detailed_cb(struct detailed_timing *timing, void *closure);
523
524static void
Adam Jackson4d76a222010-08-03 14:38:17 -0400525cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
526{
527 int i, n = 0;
Christian Schmidt4966b2a2011-12-19 20:03:43 +0100528 u8 d = ext[0x02];
Adam Jackson4d76a222010-08-03 14:38:17 -0400529 u8 *det_base = ext + d;
530
Christian Schmidt4966b2a2011-12-19 20:03:43 +0100531 n = (127 - d) / 18;
Adam Jackson4d76a222010-08-03 14:38:17 -0400532 for (i = 0; i < n; i++)
533 cb((struct detailed_timing *)(det_base + 18 * i), closure);
534}
535
536static void
Adam Jacksoncbba98f2010-08-03 14:38:18 -0400537vtb_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
549static void
Adam Jacksond1ff6402010-03-29 21:43:26 +0000550drm_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
Adam Jackson4d76a222010-08-03 14:38:17 -0400561 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;
Adam Jacksoncbba98f2010-08-03 14:38:18 -0400567 case VTB_EXT:
568 vtb_for_each_detailed_block(ext, cb, closure);
569 break;
Adam Jackson4d76a222010-08-03 14:38:17 -0400570 default:
571 break;
572 }
573 }
Adam Jacksond1ff6402010-03-29 21:43:26 +0000574}
575
576static void
577is_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. */
586static bool
587drm_monitor_supports_rb(struct edid *edid)
588{
589 if (edid->revision >= 4) {
Daniel Vetterdec7e3a2012-06-19 11:33:06 +0200590 bool ret = false;
Adam Jacksond1ff6402010-03-29 21:43:26 +0000591 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
Adam Jackson7a374352010-03-29 21:43:30 +0000598static void
599find_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 */
607static int
608drm_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
615static int
616drm_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
623static int
624drm_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
631static int
632drm_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
639static int
640drm_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 */
651static 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
Adam Jackson23425ca2009-09-23 17:30:58 -0400663/*
664 * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
665 * monitors fill with ascii space (0x20) instead.
666 */
667static int
668bad_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
Dave Airlief453ba02008-11-07 14:05:41 -0800675/**
676 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
677 * @t: standard timing params
Zhao Yakui5c612592009-06-22 13:17:10 +0800678 * @timing_level: standard timing level
Dave Airlief453ba02008-11-07 14:05:41 -0800679 *
680 * Take the standard timing params (in this case width, aspect, and refresh)
Zhao Yakui5c612592009-06-22 13:17:10 +0800681 * and convert them into a real mode using CVT/GTF/DMT.
Dave Airlief453ba02008-11-07 14:05:41 -0800682 */
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000683static struct drm_display_mode *
Adam Jackson7a374352010-03-29 21:43:30 +0000684drm_mode_std(struct drm_connector *connector, struct edid *edid,
685 struct std_timing *t, int revision)
Dave Airlief453ba02008-11-07 14:05:41 -0800686{
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000687 struct drm_device *dev = connector->dev;
688 struct drm_display_mode *m, *mode = NULL;
Zhao Yakui5c612592009-06-22 13:17:10 +0800689 int hsize, vsize;
690 int vrefresh_rate;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200691 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
692 >> EDID_TIMING_ASPECT_SHIFT;
Zhao Yakui5c612592009-06-22 13:17:10 +0800693 unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
694 >> EDID_TIMING_VFREQ_SHIFT;
Adam Jackson7a374352010-03-29 21:43:30 +0000695 int timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800696
Adam Jackson23425ca2009-09-23 17:30:58 -0400697 if (bad_std_timing(t->hsize, t->vfreq_aspect))
698 return NULL;
699
Zhao Yakui5c612592009-06-22 13:17:10 +0800700 /* 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 */
Adam Jacksonf066a172009-09-23 17:31:21 -0400705 if (aspect_ratio == 0) {
706 if (revision < 3)
707 vsize = hsize;
708 else
709 vsize = (hsize * 10) / 16;
710 } else if (aspect_ratio == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800711 vsize = (hsize * 3) / 4;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200712 else if (aspect_ratio == 2)
Dave Airlief453ba02008-11-07 14:05:41 -0800713 vsize = (hsize * 4) / 5;
714 else
715 vsize = (hsize * 9) / 16;
Adam Jacksona0910c82010-03-29 21:43:28 +0000716
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
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000725 /*
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 */
Adam Jackson522032d2010-04-09 16:52:49 +0000731 list_for_each_entry(m, &connector->probed_modes, head)
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000732 if (m->hdisplay == hsize && m->vdisplay == vsize &&
733 drm_mode_vrefresh(m) == vrefresh_rate)
734 return NULL;
735
Adam Jacksona0910c82010-03-29 21:43:28 +0000736 /* 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,
Dave Airlied50ba252009-09-23 14:44:08 +1000739 false);
Zhao Yakui559ee212009-09-03 09:33:47 +0800740 mode->hdisplay = 1366;
Adam Jacksona4967de2010-07-28 07:40:32 +1000741 mode->hsync_start = mode->hsync_start - 1;
742 mode->hsync_end = mode->hsync_end - 1;
Zhao Yakui559ee212009-09-03 09:33:47 +0800743 return mode;
744 }
Adam Jacksona0910c82010-03-29 21:43:28 +0000745
Zhao Yakui559ee212009-09-03 09:33:47 +0800746 /* check whether it can be found in default mode table */
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000747 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate);
Zhao Yakui559ee212009-09-03 09:33:47 +0800748 if (mode)
749 return mode;
750
Zhao Yakui5c612592009-06-22 13:17:10 +0800751 switch (timing_level) {
752 case LEVEL_DMT:
Zhao Yakui5c612592009-06-22 13:17:10 +0800753 break;
754 case LEVEL_GTF:
755 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
756 break;
Adam Jackson7a374352010-03-29 21:43:30 +0000757 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)) {
Sascha Haueraefd3302012-02-01 11:38:21 +0100765 drm_mode_destroy(dev, mode);
Adam Jackson7a374352010-03-29 21:43:30 +0000766 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;
Zhao Yakui5c612592009-06-22 13:17:10 +0800774 case LEVEL_CVT:
Dave Airlied50ba252009-09-23 14:44:08 +1000775 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
776 false);
Zhao Yakui5c612592009-06-22 13:17:10 +0800777 break;
778 }
Dave Airlief453ba02008-11-07 14:05:41 -0800779 return mode;
780}
781
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000782/*
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 */
790static void
791drm_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 };
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000806
807 if (!(pt->misc & DRM_EDID_PT_INTERLACED))
808 return;
809
Kulikov Vasiliy3c581412010-06-28 15:54:52 +0400810 for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000811 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
Dave Airlief453ba02008-11-07 14:05:41 -0800824/**
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 */
834static 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;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200841 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;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200845 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;
Torsten Duwe2c5d8162013-03-23 15:38:22 +0100847 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 +0200848 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 -0800849
Adam Jacksonfc438962009-06-04 10:20:34 +1000850 /* ignore tiny modes */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200851 if (hactive < 64 || vactive < 64)
Adam Jacksonfc438962009-06-04 10:20:34 +1000852 return NULL;
853
Michel Dänzer0454bea2009-06-15 16:56:07 +0200854 if (pt->misc & DRM_EDID_PT_STEREO) {
Dave Airlief453ba02008-11-07 14:05:41 -0800855 printk(KERN_WARNING "stereo mode not supported\n");
856 return NULL;
857 }
Michel Dänzer0454bea2009-06-15 16:56:07 +0200858 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
Jerome Glisse79b7dcb2010-01-14 19:02:20 +0100859 printk(KERN_WARNING "composite sync not supported\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800860 }
861
Zhao Yakuifcb45612009-10-14 09:11:25 +0800862 /* 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 }
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400868
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
Dave Airlief453ba02008-11-07 14:05:41 -0800877 mode = drm_mode_create(dev);
878 if (!mode)
879 return NULL;
880
Dave Airlief453ba02008-11-07 14:05:41 -0800881 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
Michel Dänzer0454bea2009-06-15 16:56:07 +0200882 timing->pixel_clock = cpu_to_le16(1088);
Dave Airlief453ba02008-11-07 14:05:41 -0800883
Michel Dänzer0454bea2009-06-15 16:56:07 +0200884 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
Dave Airlief453ba02008-11-07 14:05:41 -0800885
Michel Dänzer0454bea2009-06-15 16:56:07 +0200886 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;
Dave Airlief453ba02008-11-07 14:05:41 -0800890
Michel Dänzer0454bea2009-06-15 16:56:07 +0200891 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;
Dave Airlief453ba02008-11-07 14:05:41 -0800895
Jesse Barnes7064fef2009-11-05 10:12:54 -0800896 /* 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
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000902 drm_mode_do_interlace_quirk(mode, pt);
Dave Airlief453ba02008-11-07 14:05:41 -0800903
904 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
Michel Dänzer0454bea2009-06-15 16:56:07 +0200905 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
Dave Airlief453ba02008-11-07 14:05:41 -0800906 }
907
Michel Dänzer0454bea2009-06-15 16:56:07 +0200908 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;
Dave Airlief453ba02008-11-07 14:05:41 -0800912
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400913set_size:
Michel Dänzere14cbee2009-06-23 12:36:32 +0200914 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;
Dave Airlief453ba02008-11-07 14:05:41 -0800916
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
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400927 mode->type = DRM_MODE_TYPE_DRIVER;
Torsten Duwe612dade2013-03-23 15:39:34 +0100928 mode->vrefresh = drm_mode_vrefresh(mode);
Adam Jacksonfd5f2542012-05-23 16:26:54 -0400929 drm_mode_set_name(mode);
930
Dave Airlief453ba02008-11-07 14:05:41 -0800931 return mode;
932}
933
Adam Jackson07a5e632009-12-03 17:44:38 -0500934static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000935mode_is_rb(const struct drm_display_mode *mode)
Adam Jackson07a5e632009-12-03 17:44:38 -0500936{
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000937 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}
Adam Jackson07a5e632009-12-03 17:44:38 -0500942
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000943static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000944mode_in_hsync_range(const struct drm_display_mode *mode,
945 struct edid *edid, u8 *t)
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000946{
947 int hsync, hmin, hmax;
Adam Jackson07a5e632009-12-03 17:44:38 -0500948
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000949 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);
Adam Jackson07a5e632009-12-03 17:44:38 -0500955 hsync = drm_mode_hsync(mode);
Adam Jackson07a5e632009-12-03 17:44:38 -0500956
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000957 return (hsync <= hmax && hsync >= hmin);
958}
959
960static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000961mode_in_vsync_range(const struct drm_display_mode *mode,
962 struct edid *edid, u8 *t)
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000963{
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
977static u32
978range_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
Adam Jackson07a5e632009-12-03 17:44:38 -0500992static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000993mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000994 struct detailed_timing *timing)
Adam Jackson07a5e632009-12-03 17:44:38 -0500995{
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000996 u32 max_clock;
997 u8 *t = (u8 *)timing;
Adam Jackson07a5e632009-12-03 17:44:38 -0500998
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000999 if (!mode_in_hsync_range(mode, edid, t))
Adam Jackson07a5e632009-12-03 17:44:38 -05001000 return false;
1001
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001002 if (!mode_in_vsync_range(mode, edid, t))
Adam Jackson07a5e632009-12-03 17:44:38 -05001003 return false;
1004
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001005 if ((max_clock = range_pixel_clock(edid, t)))
Adam Jackson07a5e632009-12-03 17:44:38 -05001006 if (mode->clock > max_clock)
1007 return false;
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001008
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;
Adam Jackson07a5e632009-12-03 17:44:38 -05001016
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 */
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001024static int
1025drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
1026 struct detailed_timing *timing)
Adam Jackson07a5e632009-12-03 17:44:38 -05001027{
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++) {
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001033 if (mode_in_range(drm_dmt_modes + i, edid, timing)) {
Adam Jackson07a5e632009-12-03 17:44:38 -05001034 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
Adam Jackson13931572010-08-03 14:38:19 -04001045static void
1046do_inferred_modes(struct detailed_timing *timing, void *c)
Adam Jackson9340d8c2009-12-03 17:44:40 -05001047{
Adam Jackson13931572010-08-03 14:38:19 -04001048 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);
Adam Jackson9340d8c2009-12-03 17:44:40 -05001051
Adam Jackson13931572010-08-03 14:38:19 -04001052 if (gtf && data->type == EDID_DETAIL_MONITOR_RANGE)
1053 closure->modes += drm_gtf_modes_for_range(closure->connector,
1054 closure->edid,
1055 timing);
Adam Jackson9340d8c2009-12-03 17:44:40 -05001056}
1057
Adam Jackson13931572010-08-03 14:38:19 -04001058static int
1059add_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
Adam Jackson2255be12010-03-29 21:43:22 +00001072static int
1073drm_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);
Linus Torvaldsaa9f56b2010-08-12 09:21:39 -07001082 if (m >= ARRAY_SIZE(est3_modes))
Adam Jackson2255be12010-03-29 21:43:22 +00001083 break;
1084 if (est[i] & (1 << j)) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001085 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 */);
Adam Jackson2255be12010-03-29 21:43:22 +00001090 if (mode) {
1091 drm_mode_probed_add(connector, mode);
1092 modes++;
1093 }
1094 }
1095 }
1096 }
1097
1098 return modes;
1099}
1100
Adam Jackson13931572010-08-03 14:38:19 -04001101static void
1102do_established_modes(struct detailed_timing *timing, void *c)
Adam Jackson9cf00972009-12-03 17:44:36 -05001103{
Adam Jackson13931572010-08-03 14:38:19 -04001104 struct detailed_mode_closure *closure = c;
Adam Jackson9cf00972009-12-03 17:44:36 -05001105 struct detailed_non_pixel *data = &timing->data.other_data;
Adam Jackson13931572010-08-03 14:38:19 -04001106
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 */
1118static int
1119add_established_modes(struct drm_connector *connector, struct edid *edid)
1120{
Adam Jackson9cf00972009-12-03 17:44:36 -05001121 struct drm_device *dev = connector->dev;
Adam Jackson13931572010-08-03 14:38:19 -04001122 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 };
Adam Jackson9cf00972009-12-03 17:44:36 -05001129
Adam Jackson13931572010-08-03 14:38:19 -04001130 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 }
Adam Jackson9cf00972009-12-03 17:44:36 -05001139 }
1140
Adam Jackson13931572010-08-03 14:38:19 -04001141 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
1148static void
1149do_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;
Adam Jackson9cf00972009-12-03 17:44:36 -05001158 for (i = 0; i < 6; i++) {
1159 struct std_timing *std;
1160 struct drm_display_mode *newmode;
1161
1162 std = &data->data.timings[i];
Adam Jackson7a374352010-03-29 21:43:30 +00001163 newmode = drm_mode_std(connector, edid, std,
1164 edid->revision);
Adam Jackson9cf00972009-12-03 17:44:36 -05001165 if (newmode) {
1166 drm_mode_probed_add(connector, newmode);
Adam Jackson13931572010-08-03 14:38:19 -04001167 closure->modes++;
Adam Jackson9cf00972009-12-03 17:44:36 -05001168 }
1169 }
Adam Jackson13931572010-08-03 14:38:19 -04001170 }
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 */
1180static int
1181add_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
Dave Airlief453ba02008-11-07 14:05:41 -08001209static 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;
Zhao Yakui5c612592009-06-22 13:17:10 +08001215 struct cvt_timing *cvt;
1216 const int rates[] = { 60, 85, 75, 60, 50 };
1217 const u8 empty[3] = { 0, 0, 0 };
Dave Airlief453ba02008-11-07 14:05:41 -08001218
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))
Michel Dänzer0454bea2009-06-15 16:56:07 +02001224 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08001225
1226 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
Zhao Yakui5c612592009-06-22 13:17:10 +08001227 switch (cvt->code[1] & 0x0c) {
Adam Jacksonf066a172009-09-23 17:31:21 -04001228 case 0x00:
Dave Airlief453ba02008-11-07 14:05:41 -08001229 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:
Dave Airlief453ba02008-11-07 14:05:41 -08001238 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
Adam Jackson13931572010-08-03 14:38:19 -04001258static void
1259do_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}
Adam Jackson9cf00972009-12-03 17:44:36 -05001267
1268static int
Adam Jackson13931572010-08-03 14:38:19 -04001269add_cvt_modes(struct drm_connector *connector, struct edid *edid)
1270{
1271 struct detailed_mode_closure closure = {
1272 connector, edid, 0, 0, 0
1273 };
Adam Jackson9cf00972009-12-03 17:44:36 -05001274
Adam Jackson13931572010-08-03 14:38:19 -04001275 if (version_greater(edid, 1, 2))
1276 drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
Dave Airlief453ba02008-11-07 14:05:41 -08001277
Adam Jackson13931572010-08-03 14:38:19 -04001278 /* XXX should also look for CVT codes in VTB blocks */
1279
1280 return closure.modes;
Dave Airlief453ba02008-11-07 14:05:41 -08001281}
1282
Adam Jackson13931572010-08-03 14:38:19 -04001283static void
1284do_detailed_mode(struct detailed_timing *timing, void *c)
Dave Airlief453ba02008-11-07 14:05:41 -08001285{
Adam Jackson13931572010-08-03 14:38:19 -04001286 struct detailed_mode_closure *closure = c;
Dave Airlief453ba02008-11-07 14:05:41 -08001287 struct drm_display_mode *newmode;
Adam Jackson9cf00972009-12-03 17:44:36 -05001288
1289 if (timing->pixel_clock) {
Adam Jackson13931572010-08-03 14:38:19 -04001290 newmode = drm_mode_detailed(closure->connector->dev,
1291 closure->edid, timing,
1292 closure->quirks);
Dave Airlief453ba02008-11-07 14:05:41 -08001293 if (!newmode)
Adam Jackson13931572010-08-03 14:38:19 -04001294 return;
Adam Jackson9cf00972009-12-03 17:44:36 -05001295
Adam Jackson13931572010-08-03 14:38:19 -04001296 if (closure->preferred)
Dave Airlief453ba02008-11-07 14:05:41 -08001297 newmode->type |= DRM_MODE_TYPE_PREFERRED;
1298
Adam Jackson13931572010-08-03 14:38:19 -04001299 drm_mode_probed_add(closure->connector, newmode);
1300 closure->modes++;
1301 closure->preferred = 0;
Zhao Yakui882f0212009-08-26 18:20:49 +08001302 }
Ma Ling167f3a02009-03-20 14:09:48 +08001303}
1304
Adam Jackson13931572010-08-03 14:38:19 -04001305/*
1306 * add_detailed_modes - Add modes from detailed timings
Dave Airlief453ba02008-11-07 14:05:41 -08001307 * @connector: attached connector
1308 * @edid: EDID block to scan
1309 * @quirks: quirks to apply
Dave Airlief453ba02008-11-07 14:05:41 -08001310 */
Adam Jackson13931572010-08-03 14:38:19 -04001311static int
1312add_detailed_modes(struct drm_connector *connector, struct edid *edid,
1313 u32 quirks)
Dave Airlief453ba02008-11-07 14:05:41 -08001314{
Adam Jackson13931572010-08-03 14:38:19 -04001315 struct detailed_mode_closure closure = {
1316 connector,
1317 edid,
1318 1,
1319 quirks,
1320 0
1321 };
Dave Airlief453ba02008-11-07 14:05:41 -08001322
Adam Jackson13931572010-08-03 14:38:19 -04001323 if (closure.preferred && !version_greater(edid, 1, 3))
1324 closure.preferred =
1325 (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
Adam Jacksona327f6b2010-03-29 21:43:25 +00001326
Adam Jackson13931572010-08-03 14:38:19 -04001327 drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
Dave Airlief453ba02008-11-07 14:05:41 -08001328
Adam Jackson13931572010-08-03 14:38:19 -04001329 return closure.modes;
Zhao Yakui882f0212009-08-26 18:20:49 +08001330}
Dave Airlief453ba02008-11-07 14:05:41 -08001331
Ma Lingf23c20c2009-03-26 19:26:23 +08001332#define HDMI_IDENTIFIER 0x000C03
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001333#define AUDIO_BLOCK 0x01
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001334#define VIDEO_BLOCK 0x02
Ma Lingf23c20c2009-03-26 19:26:23 +08001335#define VENDOR_BLOCK 0x03
Wu Fengguang76adaa32011-09-05 14:23:20 +08001336#define SPEAKER_BLOCK 0x04
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001337#define EDID_BASIC_AUDIO (1 << 6)
1338
1339/**
1340 * Search EDID for CEA extension block.
1341 */
Ben Skeggseccaca22011-03-30 05:03:47 +00001342u8 *drm_find_cea_extension(struct edid *edid)
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001343{
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}
Ben Skeggseccaca22011-03-30 05:03:47 +00001363EXPORT_SYMBOL(drm_find_cea_extension);
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001364
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001365static int
1366do_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
1388static int
1389add_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
Wu Fengguang76adaa32011-09-05 14:23:20 +08001406static void
1407parse_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
1436static void
1437monitor_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 */
1454void 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
Christian Schmidta0ab7342011-12-19 20:03:38 +01001489 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 }
Wu Fengguang76adaa32011-09-05 14:23:20 +08001511 }
Wu Fengguang76adaa32011-09-05 14:23:20 +08001512 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}
1517EXPORT_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 */
1524int 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}
1555EXPORT_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 */
1565struct 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}
1577EXPORT_SYMBOL(drm_select_eld);
1578
Ma Lingf23c20c2009-03-26 19:26:23 +08001579/**
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 */
1586bool drm_detect_hdmi_monitor(struct edid *edid)
1587{
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001588 u8 *edid_ext;
Adam Jackson7466f4c2010-03-29 21:43:23 +00001589 int i, hdmi_id;
Ma Lingf23c20c2009-03-26 19:26:23 +08001590 int start_offset, end_offset;
1591 bool is_hdmi = false;
1592
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001593 edid_ext = drm_find_cea_extension(edid);
1594 if (!edid_ext)
Ma Lingf23c20c2009-03-26 19:26:23 +08001595 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
1619end:
1620 return is_hdmi;
1621}
1622EXPORT_SYMBOL(drm_detect_hdmi_monitor);
1623
Dave Airlief453ba02008-11-07 14:05:41 -08001624/**
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001625 * 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 */
1634bool 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 }
1666end:
1667 return has_audio;
1668}
1669EXPORT_SYMBOL(drm_detect_monitor_audio);
1670
1671/**
Jesse Barnes3b112282011-04-15 12:49:23 -07001672 * 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 */
1680static void drm_add_display_info(struct edid *edid,
1681 struct drm_display_info *info)
1682{
Jesse Barnesebec9a72011-08-03 09:22:54 -07001683 u8 *edid_ext;
1684
Jesse Barnes3b112282011-04-15 12:49:23 -07001685 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;
Jesse Barnesda05a5a2011-04-15 13:48:57 -07001690 info->color_formats = 0;
Jesse Barnes3b112282011-04-15 12:49:23 -07001691
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 }
Jesse Barnesda05a5a2011-04-15 13:48:57 -07001723
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;
Jesse Barnesebec9a72011-08-03 09:22:54 -07001729
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];
Jesse Barnes3b112282011-04-15 12:49:23 -07001736}
1737
1738/**
Dave Airlief453ba02008-11-07 14:05:41 -08001739 * 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 */
1747int 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 }
Alex Deucher3c537882010-02-05 04:21:19 -05001755 if (!drm_edid_is_valid(edid)) {
Jordan Crousedcdb1672010-05-27 13:40:25 -06001756 dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001757 drm_get_connector_name(connector));
1758 return 0;
1759 }
1760
1761 quirks = edid_get_quirks(edid);
1762
Adam Jacksonc867df72010-03-29 21:43:21 +00001763 /*
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 *
Adam Jackson13931572010-08-03 14:38:19 -04001773 * We get this pretty much right.
Adam Jacksonc867df72010-03-29 21:43:21 +00001774 *
1775 * XXX order for additional mode types in extension blocks?
1776 */
Adam Jackson13931572010-08-03 14:38:19 -04001777 num_modes += add_detailed_modes(connector, edid, quirks);
1778 num_modes += add_cvt_modes(connector, edid);
Adam Jacksonc867df72010-03-29 21:43:21 +00001779 num_modes += add_standard_modes(connector, edid);
1780 num_modes += add_established_modes(connector, edid);
Paulo Zanoni7b480b02013-02-15 13:36:27 -02001781 if (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF)
1782 num_modes += add_inferred_modes(connector, edid);
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001783 num_modes += add_cea_modes(connector, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08001784
1785 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
1786 edid_fixup_preferred(connector, quirks);
1787
Jesse Barnes3b112282011-04-15 12:49:23 -07001788 drm_add_display_info(edid, &connector->display_info);
Dave Airlief453ba02008-11-07 14:05:41 -08001789
Rafał Miłeckib2664052013-12-07 13:22:42 +01001790 if (quirks & EDID_QUIRK_FORCE_8BPC)
1791 connector->display_info.bpc = 8;
1792
Dave Airlief453ba02008-11-07 14:05:41 -08001793 return num_modes;
1794}
1795EXPORT_SYMBOL(drm_add_edid_modes);
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001796
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 */
1808int drm_add_modes_noedid(struct drm_connector *connector,
1809 int hdisplay, int vdisplay)
1810{
1811 int i, count, num_modes = 0;
Chris Wilsonb1f559e2011-01-26 09:49:47 +00001812 struct drm_display_mode *mode;
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001813 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++) {
Chris Wilsonb1f559e2011-01-26 09:49:47 +00001822 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001823 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 }
Adam Jacksonf985ded2009-11-23 14:23:04 -05001833 if (drm_mode_vrefresh(ptr) > 61)
1834 continue;
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08001835 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}
1843EXPORT_SYMBOL(drm_add_modes_noedid);