blob: de2a0ed8e9456ead489ae6ed5995f100621b31e1 [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>
Adam Jackson47819ba2012-05-30 16:42:39 -040033#include <linux/module.h>
Dave Airlief453ba02008-11-07 14:05:41 -080034#include "drmP.h"
35#include "drm_edid.h"
Adam Jackson38fcbb672010-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 Jacksonbc42aab2012-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 {
Ian Pilcherc51a3fd62012-04-22 11:40:26 -050086 char vendor[4];
Dave Airlief453ba02008-11-07 14:05:41 -080087 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 Jacksonbc42aab2012-05-23 16:26:54 -0400125
126 /* ViewSonic VA2026w */
127 { "VSC", 5020, EDID_QUIRK_FORCE_REDUCED_BLANKING },
Dave Airlief453ba02008-11-07 14:05:41 -0800128};
129
Adam Jackson61e57a82010-03-29 21:43:18 +0000130/*** DDC fetch and block validation ***/
Dave Airlief453ba02008-11-07 14:05:41 -0800131
Adam Jackson083ae052009-09-23 17:30:45 -0400132static const u8 edid_header[] = {
133 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
134};
Dave Airlief453ba02008-11-07 14:05:41 -0800135
Thomas Reim051963d2011-07-29 14:28:57 +0000136 /*
137 * Sanity check the header of the base EDID block. Return 8 if the header
138 * is perfect, down to 0 if it's totally wrong.
139 */
140int drm_edid_header_is_valid(const u8 *raw_edid)
141{
142 int i, score = 0;
143
144 for (i = 0; i < sizeof(edid_header); i++)
145 if (raw_edid[i] == edid_header[i])
146 score++;
147
148 return score;
149}
150EXPORT_SYMBOL(drm_edid_header_is_valid);
151
Adam Jackson47819ba2012-05-30 16:42:39 -0400152static int edid_fixup __read_mostly = 6;
153module_param_named(edid_fixup, edid_fixup, int, 0400);
154MODULE_PARM_DESC(edid_fixup,
155 "Minimum number of valid EDID header bytes (0-8, default 6)");
Thomas Reim051963d2011-07-29 14:28:57 +0000156
Adam Jackson61e57a82010-03-29 21:43:18 +0000157/*
158 * Sanity check the EDID block (base or extension). Return 0 if the block
159 * doesn't check out, or 1 if it's valid.
Dave Airlief453ba02008-11-07 14:05:41 -0800160 */
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400161bool drm_edid_block_valid(u8 *raw_edid, int block, bool print_bad_edid)
Dave Airlief453ba02008-11-07 14:05:41 -0800162{
Adam Jackson61e57a82010-03-29 21:43:18 +0000163 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800164 u8 csum = 0;
Adam Jackson61e57a82010-03-29 21:43:18 +0000165 struct edid *edid = (struct edid *)raw_edid;
Dave Airlief453ba02008-11-07 14:05:41 -0800166
Adam Jackson47819ba2012-05-30 16:42:39 -0400167 if (edid_fixup > 8 || edid_fixup < 0)
168 edid_fixup = 6;
169
Adam Jacksonf89ec8a2012-04-16 10:40:08 -0400170 if (block == 0) {
Thomas Reim051963d2011-07-29 14:28:57 +0000171 int score = drm_edid_header_is_valid(raw_edid);
Adam Jackson61e57a82010-03-29 21:43:18 +0000172 if (score == 8) ;
Adam Jackson47819ba2012-05-30 16:42:39 -0400173 else if (score >= edid_fixup) {
Adam Jackson61e57a82010-03-29 21:43:18 +0000174 DRM_DEBUG("Fixing EDID header, your hardware may be failing\n");
175 memcpy(raw_edid, edid_header, sizeof(edid_header));
176 } else {
177 goto bad;
178 }
179 }
Dave Airlief453ba02008-11-07 14:05:41 -0800180
181 for (i = 0; i < EDID_LENGTH; i++)
182 csum += raw_edid[i];
183 if (csum) {
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400184 if (print_bad_edid) {
185 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
186 }
Adam Jackson4a638b42010-05-25 16:33:09 -0400187
188 /* allow CEA to slide through, switches mangle this */
189 if (raw_edid[0] != 0x02)
190 goto bad;
Dave Airlief453ba02008-11-07 14:05:41 -0800191 }
192
Adam Jackson61e57a82010-03-29 21:43:18 +0000193 /* per-block-type checks */
194 switch (raw_edid[0]) {
195 case 0: /* base */
196 if (edid->version != 1) {
197 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
198 goto bad;
199 }
Adam Jackson862b89c2009-11-23 14:23:06 -0500200
Adam Jackson61e57a82010-03-29 21:43:18 +0000201 if (edid->revision > 4)
202 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
203 break;
204
205 default:
206 break;
207 }
Adam Jackson47ee4cc2009-11-23 14:23:05 -0500208
Dave Airlief453ba02008-11-07 14:05:41 -0800209 return 1;
210
211bad:
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400212 if (raw_edid && print_bad_edid) {
Dave Airlief49dadb2011-06-14 06:13:54 +0000213 printk(KERN_ERR "Raw EDID:\n");
Tormod Volden0aff47f2011-07-05 20:12:53 +0000214 print_hex_dump(KERN_ERR, " \t", DUMP_PREFIX_NONE, 16, 1,
215 raw_edid, EDID_LENGTH, false);
Dave Airlief453ba02008-11-07 14:05:41 -0800216 }
217 return 0;
218}
Carsten Emdeda0df922012-03-18 22:37:33 +0100219EXPORT_SYMBOL(drm_edid_block_valid);
Adam Jackson61e57a82010-03-29 21:43:18 +0000220
221/**
222 * drm_edid_is_valid - sanity check EDID data
223 * @edid: EDID data
224 *
225 * Sanity-check an entire EDID record (including extensions)
226 */
227bool drm_edid_is_valid(struct edid *edid)
228{
229 int i;
230 u8 *raw = (u8 *)edid;
231
232 if (!edid)
233 return false;
234
235 for (i = 0; i <= edid->extensions; i++)
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400236 if (!drm_edid_block_valid(raw + i * EDID_LENGTH, i, true))
Adam Jackson61e57a82010-03-29 21:43:18 +0000237 return false;
238
239 return true;
240}
Alex Deucher3c537882010-02-05 04:21:19 -0500241EXPORT_SYMBOL(drm_edid_is_valid);
Dave Airlief453ba02008-11-07 14:05:41 -0800242
Adam Jackson61e57a82010-03-29 21:43:18 +0000243#define DDC_SEGMENT_ADDR 0x30
244/**
245 * Get EDID information via I2C.
246 *
247 * \param adapter : i2c device adaptor
248 * \param buf : EDID data buffer to be filled
249 * \param len : EDID data buffer length
250 * \return 0 on success or -1 on failure.
251 *
252 * Try to fetch EDID information by calling i2c driver function.
253 */
254static int
255drm_do_probe_ddc_edid(struct i2c_adapter *adapter, unsigned char *buf,
256 int block, int len)
257{
258 unsigned char start = block * EDID_LENGTH;
Shirish Scd004b32012-08-30 07:04:06 +0000259 unsigned char segment = block >> 1;
260 unsigned char xfers = segment ? 3 : 2;
Chris Wilson4819d2e2011-03-15 11:04:41 +0000261 int ret, retries = 5;
Adam Jackson61e57a82010-03-29 21:43:18 +0000262
Chris Wilson4819d2e2011-03-15 11:04:41 +0000263 /* The core i2c driver will automatically retry the transfer if the
264 * adapter reports EAGAIN. However, we find that bit-banging transfers
265 * are susceptible to errors under a heavily loaded machine and
266 * generate spurious NAKs and timeouts. Retrying the transfer
267 * of the individual block a few times seems to overcome this.
268 */
269 do {
270 struct i2c_msg msgs[] = {
271 {
Shirish Scd004b32012-08-30 07:04:06 +0000272 .addr = DDC_SEGMENT_ADDR,
273 .flags = 0,
274 .len = 1,
275 .buf = &segment,
276 }, {
Chris Wilson4819d2e2011-03-15 11:04:41 +0000277 .addr = DDC_ADDR,
278 .flags = 0,
279 .len = 1,
280 .buf = &start,
281 }, {
282 .addr = DDC_ADDR,
283 .flags = I2C_M_RD,
284 .len = len,
285 .buf = buf,
286 }
287 };
Shirish Scd004b32012-08-30 07:04:06 +0000288
289 /*
290 * Avoid sending the segment addr to not upset non-compliant ddc
291 * monitors.
292 */
293 ret = i2c_transfer(adapter, &msgs[3 - xfers], xfers);
294
Eugeni Dodonov9292f372012-01-05 09:34:28 -0200295 if (ret == -ENXIO) {
296 DRM_DEBUG_KMS("drm: skipping non-existent adapter %s\n",
297 adapter->name);
298 break;
299 }
Shirish Scd004b32012-08-30 07:04:06 +0000300 } while (ret != xfers && --retries);
Adam Jackson61e57a82010-03-29 21:43:18 +0000301
Shirish Scd004b32012-08-30 07:04:06 +0000302 return ret == xfers ? 0 : -1;
Adam Jackson61e57a82010-03-29 21:43:18 +0000303}
304
Dave Airlie4a9a8b72011-06-14 06:13:55 +0000305static bool drm_edid_is_zero(u8 *in_edid, int length)
306{
307 int i;
308 u32 *raw_edid = (u32 *)in_edid;
309
310 for (i = 0; i < length / 4; i++)
311 if (*(raw_edid + i) != 0)
312 return false;
313 return true;
314}
315
Adam Jackson61e57a82010-03-29 21:43:18 +0000316static u8 *
317drm_do_get_edid(struct drm_connector *connector, struct i2c_adapter *adapter)
318{
Sam Tygier0ea75e22010-09-23 10:11:01 +0100319 int i, j = 0, valid_extensions = 0;
Adam Jackson61e57a82010-03-29 21:43:18 +0000320 u8 *block, *new;
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400321 bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
Adam Jackson61e57a82010-03-29 21:43:18 +0000322
323 if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
324 return NULL;
325
326 /* base block fetch */
327 for (i = 0; i < 4; i++) {
328 if (drm_do_probe_ddc_edid(adapter, block, 0, EDID_LENGTH))
329 goto out;
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400330 if (drm_edid_block_valid(block, 0, print_bad_edid))
Adam Jackson61e57a82010-03-29 21:43:18 +0000331 break;
Dave Airlie4a9a8b72011-06-14 06:13:55 +0000332 if (i == 0 && drm_edid_is_zero(block, EDID_LENGTH)) {
333 connector->null_edid_counter++;
334 goto carp;
335 }
Adam Jackson61e57a82010-03-29 21:43:18 +0000336 }
337 if (i == 4)
338 goto carp;
339
340 /* if there's no extensions, we're done */
341 if (block[0x7e] == 0)
342 return block;
343
344 new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
345 if (!new)
346 goto out;
347 block = new;
348
349 for (j = 1; j <= block[0x7e]; j++) {
350 for (i = 0; i < 4; i++) {
Sam Tygier0ea75e22010-09-23 10:11:01 +0100351 if (drm_do_probe_ddc_edid(adapter,
352 block + (valid_extensions + 1) * EDID_LENGTH,
353 j, EDID_LENGTH))
Adam Jackson61e57a82010-03-29 21:43:18 +0000354 goto out;
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400355 if (drm_edid_block_valid(block + (valid_extensions + 1) * EDID_LENGTH, j, print_bad_edid)) {
Sam Tygier0ea75e22010-09-23 10:11:01 +0100356 valid_extensions++;
Adam Jackson61e57a82010-03-29 21:43:18 +0000357 break;
Sam Tygier0ea75e22010-09-23 10:11:01 +0100358 }
Adam Jackson61e57a82010-03-29 21:43:18 +0000359 }
360 if (i == 4)
Sam Tygier0ea75e22010-09-23 10:11:01 +0100361 dev_warn(connector->dev->dev,
362 "%s: Ignoring invalid EDID block %d.\n",
363 drm_get_connector_name(connector), j);
364 }
365
366 if (valid_extensions != block[0x7e]) {
367 block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
368 block[0x7e] = valid_extensions;
369 new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
370 if (!new)
371 goto out;
372 block = new;
Adam Jackson61e57a82010-03-29 21:43:18 +0000373 }
374
375 return block;
376
377carp:
Jerome Glisse0b2443e2012-08-09 11:25:51 -0400378 if (print_bad_edid) {
379 dev_warn(connector->dev->dev, "%s: EDID block %d invalid.\n",
380 drm_get_connector_name(connector), j);
381 }
382 connector->bad_edid_counter++;
Adam Jackson61e57a82010-03-29 21:43:18 +0000383
384out:
385 kfree(block);
386 return NULL;
387}
388
389/**
390 * Probe DDC presence.
391 *
392 * \param adapter : i2c device adaptor
393 * \return 1 on success
394 */
395static bool
396drm_probe_ddc(struct i2c_adapter *adapter)
397{
398 unsigned char out;
399
400 return (drm_do_probe_ddc_edid(adapter, &out, 0, 1) == 0);
401}
402
403/**
404 * drm_get_edid - get EDID data, if available
405 * @connector: connector we're probing
406 * @adapter: i2c adapter to use for DDC
407 *
408 * Poke the given i2c channel to grab EDID data if possible. If found,
409 * attach it to the connector.
410 *
411 * Return edid data or NULL if we couldn't find any.
412 */
413struct edid *drm_get_edid(struct drm_connector *connector,
414 struct i2c_adapter *adapter)
415{
416 struct edid *edid = NULL;
417
418 if (drm_probe_ddc(adapter))
419 edid = (struct edid *)drm_do_get_edid(connector, adapter);
420
Adam Jackson61e57a82010-03-29 21:43:18 +0000421 return edid;
Adam Jackson61e57a82010-03-29 21:43:18 +0000422}
423EXPORT_SYMBOL(drm_get_edid);
424
425/*** EDID parsing ***/
426
Dave Airlief453ba02008-11-07 14:05:41 -0800427/**
428 * edid_vendor - match a string against EDID's obfuscated vendor field
429 * @edid: EDID to match
430 * @vendor: vendor string
431 *
432 * Returns true if @vendor is in @edid, false otherwise
433 */
434static bool edid_vendor(struct edid *edid, char *vendor)
435{
436 char edid_vendor[3];
437
438 edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
439 edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
440 ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
Dave Airlie16456c82009-04-03 09:10:33 +1000441 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
Dave Airlief453ba02008-11-07 14:05:41 -0800442
443 return !strncmp(edid_vendor, vendor, 3);
444}
445
446/**
447 * edid_get_quirks - return quirk flags for a given EDID
448 * @edid: EDID to process
449 *
450 * This tells subsequent routines what fixes they need to apply.
451 */
452static u32 edid_get_quirks(struct edid *edid)
453{
454 struct edid_quirk *quirk;
455 int i;
456
457 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
458 quirk = &edid_quirk_list[i];
459
460 if (edid_vendor(edid, quirk->vendor) &&
461 (EDID_PRODUCT_ID(edid) == quirk->product_id))
462 return quirk->quirks;
463 }
464
465 return 0;
466}
467
468#define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
469#define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
470
Dave Airlief453ba02008-11-07 14:05:41 -0800471/**
472 * edid_fixup_preferred - set preferred modes based on quirk list
473 * @connector: has mode list to fix up
474 * @quirks: quirks list
475 *
476 * Walk the mode list for @connector, clearing the preferred status
477 * on existing modes and setting it anew for the right mode ala @quirks.
478 */
479static void edid_fixup_preferred(struct drm_connector *connector,
480 u32 quirks)
481{
482 struct drm_display_mode *t, *cur_mode, *preferred_mode;
Dave Airlief8906072008-12-18 16:59:02 +1000483 int target_refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800484
485 if (list_empty(&connector->probed_modes))
486 return;
487
488 if (quirks & EDID_QUIRK_PREFER_LARGE_60)
489 target_refresh = 60;
490 if (quirks & EDID_QUIRK_PREFER_LARGE_75)
491 target_refresh = 75;
492
493 preferred_mode = list_first_entry(&connector->probed_modes,
494 struct drm_display_mode, head);
495
496 list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
497 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
498
499 if (cur_mode == preferred_mode)
500 continue;
501
502 /* Largest mode is preferred */
503 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
504 preferred_mode = cur_mode;
505
506 /* At a given size, try to get closest to target refresh */
507 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
508 MODE_REFRESH_DIFF(cur_mode, target_refresh) <
509 MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
510 preferred_mode = cur_mode;
511 }
512 }
513
514 preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
515}
516
Adam Jacksonf6e252b2012-04-13 16:33:31 -0400517static bool
518mode_is_rb(const struct drm_display_mode *mode)
519{
520 return (mode->htotal - mode->hdisplay == 160) &&
521 (mode->hsync_end - mode->hdisplay == 80) &&
522 (mode->hsync_end - mode->hsync_start == 32) &&
523 (mode->vsync_start - mode->vdisplay == 3);
524}
525
Adam Jackson33c75312012-04-13 16:33:29 -0400526/*
527 * drm_mode_find_dmt - Create a copy of a mode if present in DMT
528 * @dev: Device to duplicate against
529 * @hsize: Mode width
530 * @vsize: Mode height
531 * @fresh: Mode refresh rate
Adam Jacksonf6e252b2012-04-13 16:33:31 -0400532 * @rb: Mode reduced-blanking-ness
Adam Jackson33c75312012-04-13 16:33:29 -0400533 *
534 * Walk the DMT mode list looking for a match for the given parameters.
535 * Return a newly allocated copy of the mode, or NULL if not found.
536 */
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000537struct drm_display_mode *drm_mode_find_dmt(struct drm_device *dev,
Adam Jacksonf6e252b2012-04-13 16:33:31 -0400538 int hsize, int vsize, int fresh,
539 bool rb)
Zhao Yakui559ee212009-09-03 09:33:47 +0800540{
Adam Jackson07a5e632009-12-03 17:44:38 -0500541 int i;
Zhao Yakui559ee212009-09-03 09:33:47 +0800542
Adam Jackson07a5e632009-12-03 17:44:38 -0500543 for (i = 0; i < drm_num_dmt_modes; i++) {
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000544 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
Adam Jacksonf8b46a02012-04-13 16:33:30 -0400545 if (hsize != ptr->hdisplay)
546 continue;
547 if (vsize != ptr->vdisplay)
548 continue;
549 if (fresh != drm_mode_vrefresh(ptr))
550 continue;
Adam Jacksonf6e252b2012-04-13 16:33:31 -0400551 if (rb != mode_is_rb(ptr))
552 continue;
Adam Jacksonf8b46a02012-04-13 16:33:30 -0400553
554 return drm_mode_duplicate(dev, ptr);
Zhao Yakui559ee212009-09-03 09:33:47 +0800555 }
Adam Jacksonf8b46a02012-04-13 16:33:30 -0400556
557 return NULL;
Zhao Yakui559ee212009-09-03 09:33:47 +0800558}
Dave Airlie1d42bbc2010-05-07 05:02:30 +0000559EXPORT_SYMBOL(drm_mode_find_dmt);
Adam Jackson23425ca2009-09-23 17:30:58 -0400560
Adam Jacksond1ff6402010-03-29 21:43:26 +0000561typedef void detailed_cb(struct detailed_timing *timing, void *closure);
562
563static void
Adam Jackson4d76a222010-08-03 14:38:17 -0400564cea_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
565{
566 int i, n = 0;
Christian Schmidt4966b2a2011-12-19 20:03:43 +0100567 u8 d = ext[0x02];
Adam Jackson4d76a222010-08-03 14:38:17 -0400568 u8 *det_base = ext + d;
569
Christian Schmidt4966b2a2011-12-19 20:03:43 +0100570 n = (127 - d) / 18;
Adam Jackson4d76a222010-08-03 14:38:17 -0400571 for (i = 0; i < n; i++)
572 cb((struct detailed_timing *)(det_base + 18 * i), closure);
573}
574
575static void
Adam Jacksoncbba98f2010-08-03 14:38:18 -0400576vtb_for_each_detailed_block(u8 *ext, detailed_cb *cb, void *closure)
577{
578 unsigned int i, n = min((int)ext[0x02], 6);
579 u8 *det_base = ext + 5;
580
581 if (ext[0x01] != 1)
582 return; /* unknown version */
583
584 for (i = 0; i < n; i++)
585 cb((struct detailed_timing *)(det_base + 18 * i), closure);
586}
587
588static void
Adam Jacksond1ff6402010-03-29 21:43:26 +0000589drm_for_each_detailed_block(u8 *raw_edid, detailed_cb *cb, void *closure)
590{
591 int i;
592 struct edid *edid = (struct edid *)raw_edid;
593
594 if (edid == NULL)
595 return;
596
597 for (i = 0; i < EDID_DETAILED_TIMINGS; i++)
598 cb(&(edid->detailed_timings[i]), closure);
599
Adam Jackson4d76a222010-08-03 14:38:17 -0400600 for (i = 1; i <= raw_edid[0x7e]; i++) {
601 u8 *ext = raw_edid + (i * EDID_LENGTH);
602 switch (*ext) {
603 case CEA_EXT:
604 cea_for_each_detailed_block(ext, cb, closure);
605 break;
Adam Jacksoncbba98f2010-08-03 14:38:18 -0400606 case VTB_EXT:
607 vtb_for_each_detailed_block(ext, cb, closure);
608 break;
Adam Jackson4d76a222010-08-03 14:38:17 -0400609 default:
610 break;
611 }
612 }
Adam Jacksond1ff6402010-03-29 21:43:26 +0000613}
614
615static void
616is_rb(struct detailed_timing *t, void *data)
617{
618 u8 *r = (u8 *)t;
619 if (r[3] == EDID_DETAIL_MONITOR_RANGE)
620 if (r[15] & 0x10)
621 *(bool *)data = true;
622}
623
624/* EDID 1.4 defines this explicitly. For EDID 1.3, we guess, badly. */
625static bool
626drm_monitor_supports_rb(struct edid *edid)
627{
628 if (edid->revision >= 4) {
Daniel Vetterb196a492012-06-19 11:33:06 +0200629 bool ret = false;
Adam Jacksond1ff6402010-03-29 21:43:26 +0000630 drm_for_each_detailed_block((u8 *)edid, is_rb, &ret);
631 return ret;
632 }
633
634 return ((edid->input & DRM_EDID_INPUT_DIGITAL) != 0);
635}
636
Adam Jackson7a374352010-03-29 21:43:30 +0000637static void
638find_gtf2(struct detailed_timing *t, void *data)
639{
640 u8 *r = (u8 *)t;
641 if (r[3] == EDID_DETAIL_MONITOR_RANGE && r[10] == 0x02)
642 *(u8 **)data = r;
643}
644
645/* Secondary GTF curve kicks in above some break frequency */
646static int
647drm_gtf2_hbreak(struct edid *edid)
648{
649 u8 *r = NULL;
650 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
651 return r ? (r[12] * 2) : 0;
652}
653
654static int
655drm_gtf2_2c(struct edid *edid)
656{
657 u8 *r = NULL;
658 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
659 return r ? r[13] : 0;
660}
661
662static int
663drm_gtf2_m(struct edid *edid)
664{
665 u8 *r = NULL;
666 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
667 return r ? (r[15] << 8) + r[14] : 0;
668}
669
670static int
671drm_gtf2_k(struct edid *edid)
672{
673 u8 *r = NULL;
674 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
675 return r ? r[16] : 0;
676}
677
678static int
679drm_gtf2_2j(struct edid *edid)
680{
681 u8 *r = NULL;
682 drm_for_each_detailed_block((u8 *)edid, find_gtf2, &r);
683 return r ? r[17] : 0;
684}
685
686/**
687 * standard_timing_level - get std. timing level(CVT/GTF/DMT)
688 * @edid: EDID block to scan
689 */
690static int standard_timing_level(struct edid *edid)
691{
692 if (edid->revision >= 2) {
693 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
694 return LEVEL_CVT;
695 if (drm_gtf2_hbreak(edid))
696 return LEVEL_GTF2;
697 return LEVEL_GTF;
698 }
699 return LEVEL_DMT;
700}
701
Adam Jackson23425ca2009-09-23 17:30:58 -0400702/*
703 * 0 is reserved. The spec says 0x01 fill for unused timings. Some old
704 * monitors fill with ascii space (0x20) instead.
705 */
706static int
707bad_std_timing(u8 a, u8 b)
708{
709 return (a == 0x00 && b == 0x00) ||
710 (a == 0x01 && b == 0x01) ||
711 (a == 0x20 && b == 0x20);
712}
713
Dave Airlief453ba02008-11-07 14:05:41 -0800714/**
715 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
716 * @t: standard timing params
Zhao Yakui5c612592009-06-22 13:17:10 +0800717 * @timing_level: standard timing level
Dave Airlief453ba02008-11-07 14:05:41 -0800718 *
719 * Take the standard timing params (in this case width, aspect, and refresh)
Zhao Yakui5c612592009-06-22 13:17:10 +0800720 * and convert them into a real mode using CVT/GTF/DMT.
Dave Airlief453ba02008-11-07 14:05:41 -0800721 */
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000722static struct drm_display_mode *
Adam Jackson7a374352010-03-29 21:43:30 +0000723drm_mode_std(struct drm_connector *connector, struct edid *edid,
724 struct std_timing *t, int revision)
Dave Airlief453ba02008-11-07 14:05:41 -0800725{
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000726 struct drm_device *dev = connector->dev;
727 struct drm_display_mode *m, *mode = NULL;
Zhao Yakui5c612592009-06-22 13:17:10 +0800728 int hsize, vsize;
729 int vrefresh_rate;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200730 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
731 >> EDID_TIMING_ASPECT_SHIFT;
Zhao Yakui5c612592009-06-22 13:17:10 +0800732 unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
733 >> EDID_TIMING_VFREQ_SHIFT;
Adam Jackson7a374352010-03-29 21:43:30 +0000734 int timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800735
Adam Jackson23425ca2009-09-23 17:30:58 -0400736 if (bad_std_timing(t->hsize, t->vfreq_aspect))
737 return NULL;
738
Zhao Yakui5c612592009-06-22 13:17:10 +0800739 /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
740 hsize = t->hsize * 8 + 248;
741 /* vrefresh_rate = vfreq + 60 */
742 vrefresh_rate = vfreq + 60;
743 /* the vdisplay is calculated based on the aspect ratio */
Adam Jacksonf066a172009-09-23 17:31:21 -0400744 if (aspect_ratio == 0) {
745 if (revision < 3)
746 vsize = hsize;
747 else
748 vsize = (hsize * 10) / 16;
749 } else if (aspect_ratio == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800750 vsize = (hsize * 3) / 4;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200751 else if (aspect_ratio == 2)
Dave Airlief453ba02008-11-07 14:05:41 -0800752 vsize = (hsize * 4) / 5;
753 else
754 vsize = (hsize * 9) / 16;
Adam Jacksona0910c82010-03-29 21:43:28 +0000755
756 /* HDTV hack, part 1 */
757 if (vrefresh_rate == 60 &&
758 ((hsize == 1360 && vsize == 765) ||
759 (hsize == 1368 && vsize == 769))) {
760 hsize = 1366;
761 vsize = 768;
762 }
763
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000764 /*
765 * If this connector already has a mode for this size and refresh
766 * rate (because it came from detailed or CVT info), use that
767 * instead. This way we don't have to guess at interlace or
768 * reduced blanking.
769 */
Adam Jackson522032d2010-04-09 16:52:49 +0000770 list_for_each_entry(m, &connector->probed_modes, head)
Adam Jackson7ca6adb2010-03-29 21:43:29 +0000771 if (m->hdisplay == hsize && m->vdisplay == vsize &&
772 drm_mode_vrefresh(m) == vrefresh_rate)
773 return NULL;
774
Adam Jacksona0910c82010-03-29 21:43:28 +0000775 /* HDTV hack, part 2 */
776 if (hsize == 1366 && vsize == 768 && vrefresh_rate == 60) {
777 mode = drm_cvt_mode(dev, 1366, 768, vrefresh_rate, 0, 0,
Dave Airlied50ba252009-09-23 14:44:08 +1000778 false);
Zhao Yakui559ee212009-09-03 09:33:47 +0800779 mode->hdisplay = 1366;
Adam Jacksona4967de2010-07-28 07:40:32 +1000780 mode->hsync_start = mode->hsync_start - 1;
781 mode->hsync_end = mode->hsync_end - 1;
Zhao Yakui559ee212009-09-03 09:33:47 +0800782 return mode;
783 }
Adam Jacksona0910c82010-03-29 21:43:28 +0000784
Zhao Yakui559ee212009-09-03 09:33:47 +0800785 /* check whether it can be found in default mode table */
Adam Jacksonf6e252b2012-04-13 16:33:31 -0400786 if (drm_monitor_supports_rb(edid)) {
787 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate,
788 true);
789 if (mode)
790 return mode;
791 }
792 mode = drm_mode_find_dmt(dev, hsize, vsize, vrefresh_rate, false);
Zhao Yakui559ee212009-09-03 09:33:47 +0800793 if (mode)
794 return mode;
795
Adam Jacksonf6e252b2012-04-13 16:33:31 -0400796 /* okay, generate it */
Zhao Yakui5c612592009-06-22 13:17:10 +0800797 switch (timing_level) {
798 case LEVEL_DMT:
Zhao Yakui5c612592009-06-22 13:17:10 +0800799 break;
800 case LEVEL_GTF:
801 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
802 break;
Adam Jackson7a374352010-03-29 21:43:30 +0000803 case LEVEL_GTF2:
804 /*
805 * This is potentially wrong if there's ever a monitor with
806 * more than one ranges section, each claiming a different
807 * secondary GTF curve. Please don't do that.
808 */
809 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
Takashi Iwaifc48f162012-04-20 12:59:33 +0100810 if (!mode)
811 return NULL;
Adam Jackson7a374352010-03-29 21:43:30 +0000812 if (drm_mode_hsync(mode) > drm_gtf2_hbreak(edid)) {
Sascha Haueraefd3302012-02-01 11:38:21 +0100813 drm_mode_destroy(dev, mode);
Adam Jackson7a374352010-03-29 21:43:30 +0000814 mode = drm_gtf_mode_complex(dev, hsize, vsize,
815 vrefresh_rate, 0, 0,
816 drm_gtf2_m(edid),
817 drm_gtf2_2c(edid),
818 drm_gtf2_k(edid),
819 drm_gtf2_2j(edid));
820 }
821 break;
Zhao Yakui5c612592009-06-22 13:17:10 +0800822 case LEVEL_CVT:
Dave Airlied50ba252009-09-23 14:44:08 +1000823 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0,
824 false);
Zhao Yakui5c612592009-06-22 13:17:10 +0800825 break;
826 }
Dave Airlief453ba02008-11-07 14:05:41 -0800827 return mode;
828}
829
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000830/*
831 * EDID is delightfully ambiguous about how interlaced modes are to be
832 * encoded. Our internal representation is of frame height, but some
833 * HDTV detailed timings are encoded as field height.
834 *
835 * The format list here is from CEA, in frame size. Technically we
836 * should be checking refresh rate too. Whatever.
837 */
838static void
839drm_mode_do_interlace_quirk(struct drm_display_mode *mode,
840 struct detailed_pixel_timing *pt)
841{
842 int i;
843 static const struct {
844 int w, h;
845 } cea_interlaced[] = {
846 { 1920, 1080 },
847 { 720, 480 },
848 { 1440, 480 },
849 { 2880, 480 },
850 { 720, 576 },
851 { 1440, 576 },
852 { 2880, 576 },
853 };
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000854
855 if (!(pt->misc & DRM_EDID_PT_INTERLACED))
856 return;
857
Kulikov Vasiliy3c581412010-06-28 15:54:52 +0400858 for (i = 0; i < ARRAY_SIZE(cea_interlaced); i++) {
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000859 if ((mode->hdisplay == cea_interlaced[i].w) &&
860 (mode->vdisplay == cea_interlaced[i].h / 2)) {
861 mode->vdisplay *= 2;
862 mode->vsync_start *= 2;
863 mode->vsync_end *= 2;
864 mode->vtotal *= 2;
865 mode->vtotal |= 1;
866 }
867 }
868
869 mode->flags |= DRM_MODE_FLAG_INTERLACE;
870}
871
Dave Airlief453ba02008-11-07 14:05:41 -0800872/**
873 * drm_mode_detailed - create a new mode from an EDID detailed timing section
874 * @dev: DRM device (needed to create new mode)
875 * @edid: EDID block
876 * @timing: EDID detailed timing info
877 * @quirks: quirks to apply
878 *
879 * An EDID detailed timing block contains enough info for us to create and
880 * return a new struct drm_display_mode.
881 */
882static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
883 struct edid *edid,
884 struct detailed_timing *timing,
885 u32 quirks)
886{
887 struct drm_display_mode *mode;
888 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200889 unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
890 unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
891 unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
892 unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200893 unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
894 unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
895 unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
896 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 -0800897
Adam Jacksonfc438962009-06-04 10:20:34 +1000898 /* ignore tiny modes */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200899 if (hactive < 64 || vactive < 64)
Adam Jacksonfc438962009-06-04 10:20:34 +1000900 return NULL;
901
Michel Dänzer0454bea2009-06-15 16:56:07 +0200902 if (pt->misc & DRM_EDID_PT_STEREO) {
Dave Airlief453ba02008-11-07 14:05:41 -0800903 printk(KERN_WARNING "stereo mode not supported\n");
904 return NULL;
905 }
Michel Dänzer0454bea2009-06-15 16:56:07 +0200906 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
Jerome Glisse79b7dcb2010-01-14 19:02:20 +0100907 printk(KERN_WARNING "composite sync not supported\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800908 }
909
Zhao Yakuifcb45612009-10-14 09:11:25 +0800910 /* it is incorrect if hsync/vsync width is zero */
911 if (!hsync_pulse_width || !vsync_pulse_width) {
912 DRM_DEBUG_KMS("Incorrect Detailed timing. "
913 "Wrong Hsync/Vsync pulse width\n");
914 return NULL;
915 }
Adam Jacksonbc42aab2012-05-23 16:26:54 -0400916
917 if (quirks & EDID_QUIRK_FORCE_REDUCED_BLANKING) {
918 mode = drm_cvt_mode(dev, hactive, vactive, 60, true, false, false);
919 if (!mode)
920 return NULL;
921
922 goto set_size;
923 }
924
Dave Airlief453ba02008-11-07 14:05:41 -0800925 mode = drm_mode_create(dev);
926 if (!mode)
927 return NULL;
928
Dave Airlief453ba02008-11-07 14:05:41 -0800929 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
Michel Dänzer0454bea2009-06-15 16:56:07 +0200930 timing->pixel_clock = cpu_to_le16(1088);
Dave Airlief453ba02008-11-07 14:05:41 -0800931
Michel Dänzer0454bea2009-06-15 16:56:07 +0200932 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
Dave Airlief453ba02008-11-07 14:05:41 -0800933
Michel Dänzer0454bea2009-06-15 16:56:07 +0200934 mode->hdisplay = hactive;
935 mode->hsync_start = mode->hdisplay + hsync_offset;
936 mode->hsync_end = mode->hsync_start + hsync_pulse_width;
937 mode->htotal = mode->hdisplay + hblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800938
Michel Dänzer0454bea2009-06-15 16:56:07 +0200939 mode->vdisplay = vactive;
940 mode->vsync_start = mode->vdisplay + vsync_offset;
941 mode->vsync_end = mode->vsync_start + vsync_pulse_width;
942 mode->vtotal = mode->vdisplay + vblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800943
Jesse Barnes7064fef2009-11-05 10:12:54 -0800944 /* Some EDIDs have bogus h/vtotal values */
945 if (mode->hsync_end > mode->htotal)
946 mode->htotal = mode->hsync_end + 1;
947 if (mode->vsync_end > mode->vtotal)
948 mode->vtotal = mode->vsync_end + 1;
949
Adam Jacksonb58db2c2010-02-15 22:15:39 +0000950 drm_mode_do_interlace_quirk(mode, pt);
Dave Airlief453ba02008-11-07 14:05:41 -0800951
952 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
Michel Dänzer0454bea2009-06-15 16:56:07 +0200953 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
Dave Airlief453ba02008-11-07 14:05:41 -0800954 }
955
Michel Dänzer0454bea2009-06-15 16:56:07 +0200956 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
957 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
958 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
959 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
Dave Airlief453ba02008-11-07 14:05:41 -0800960
Adam Jacksonbc42aab2012-05-23 16:26:54 -0400961set_size:
Michel Dänzere14cbee2009-06-23 12:36:32 +0200962 mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
963 mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
Dave Airlief453ba02008-11-07 14:05:41 -0800964
965 if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
966 mode->width_mm *= 10;
967 mode->height_mm *= 10;
968 }
969
970 if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
971 mode->width_mm = edid->width_cm * 10;
972 mode->height_mm = edid->height_cm * 10;
973 }
974
Adam Jacksonbc42aab2012-05-23 16:26:54 -0400975 mode->type = DRM_MODE_TYPE_DRIVER;
976 drm_mode_set_name(mode);
977
Dave Airlief453ba02008-11-07 14:05:41 -0800978 return mode;
979}
980
Adam Jackson07a5e632009-12-03 17:44:38 -0500981static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000982mode_in_hsync_range(const struct drm_display_mode *mode,
983 struct edid *edid, u8 *t)
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000984{
985 int hsync, hmin, hmax;
Adam Jackson07a5e632009-12-03 17:44:38 -0500986
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000987 hmin = t[7];
988 if (edid->revision >= 4)
989 hmin += ((t[4] & 0x04) ? 255 : 0);
990 hmax = t[8];
991 if (edid->revision >= 4)
992 hmax += ((t[4] & 0x08) ? 255 : 0);
Adam Jackson07a5e632009-12-03 17:44:38 -0500993 hsync = drm_mode_hsync(mode);
Adam Jackson07a5e632009-12-03 17:44:38 -0500994
Adam Jacksonb17e52e2010-03-29 21:43:27 +0000995 return (hsync <= hmax && hsync >= hmin);
996}
997
998static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000999mode_in_vsync_range(const struct drm_display_mode *mode,
1000 struct edid *edid, u8 *t)
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001001{
1002 int vsync, vmin, vmax;
1003
1004 vmin = t[5];
1005 if (edid->revision >= 4)
1006 vmin += ((t[4] & 0x01) ? 255 : 0);
1007 vmax = t[6];
1008 if (edid->revision >= 4)
1009 vmax += ((t[4] & 0x02) ? 255 : 0);
1010 vsync = drm_mode_vrefresh(mode);
1011
1012 return (vsync <= vmax && vsync >= vmin);
1013}
1014
1015static u32
1016range_pixel_clock(struct edid *edid, u8 *t)
1017{
1018 /* unspecified */
1019 if (t[9] == 0 || t[9] == 255)
1020 return 0;
1021
1022 /* 1.4 with CVT support gives us real precision, yay */
1023 if (edid->revision >= 4 && t[10] == 0x04)
1024 return (t[9] * 10000) - ((t[12] >> 2) * 250);
1025
1026 /* 1.3 is pathetic, so fuzz up a bit */
1027 return t[9] * 10000 + 5001;
1028}
1029
Adam Jackson07a5e632009-12-03 17:44:38 -05001030static bool
Chris Wilsonb1f559e2011-01-26 09:49:47 +00001031mode_in_range(const struct drm_display_mode *mode, struct edid *edid,
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001032 struct detailed_timing *timing)
Adam Jackson07a5e632009-12-03 17:44:38 -05001033{
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001034 u32 max_clock;
1035 u8 *t = (u8 *)timing;
Adam Jackson07a5e632009-12-03 17:44:38 -05001036
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001037 if (!mode_in_hsync_range(mode, edid, t))
Adam Jackson07a5e632009-12-03 17:44:38 -05001038 return false;
1039
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001040 if (!mode_in_vsync_range(mode, edid, t))
Adam Jackson07a5e632009-12-03 17:44:38 -05001041 return false;
1042
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001043 if ((max_clock = range_pixel_clock(edid, t)))
Adam Jackson07a5e632009-12-03 17:44:38 -05001044 if (mode->clock > max_clock)
1045 return false;
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001046
1047 /* 1.4 max horizontal check */
1048 if (edid->revision >= 4 && t[10] == 0x04)
1049 if (t[13] && mode->hdisplay > 8 * (t[13] + (256 * (t[12]&0x3))))
1050 return false;
1051
1052 if (mode_is_rb(mode) && !drm_monitor_supports_rb(edid))
1053 return false;
Adam Jackson07a5e632009-12-03 17:44:38 -05001054
1055 return true;
1056}
1057
Takashi Iwai7b668eb2012-07-03 11:22:11 +02001058static bool valid_inferred_mode(const struct drm_connector *connector,
1059 const struct drm_display_mode *mode)
1060{
1061 struct drm_display_mode *m;
1062 bool ok = false;
1063
1064 list_for_each_entry(m, &connector->probed_modes, head) {
1065 if (mode->hdisplay == m->hdisplay &&
1066 mode->vdisplay == m->vdisplay &&
1067 drm_mode_vrefresh(mode) == drm_mode_vrefresh(m))
1068 return false; /* duplicated */
1069 if (mode->hdisplay <= m->hdisplay &&
1070 mode->vdisplay <= m->vdisplay)
1071 ok = true;
1072 }
1073 return ok;
1074}
1075
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001076static int
Adam Jacksoncd4cd3d2012-04-13 16:33:33 -04001077drm_dmt_modes_for_range(struct drm_connector *connector, struct edid *edid,
Adam Jacksonb17e52e2010-03-29 21:43:27 +00001078 struct detailed_timing *timing)
Adam Jackson07a5e632009-12-03 17:44:38 -05001079{
1080 int i, modes = 0;
1081 struct drm_display_mode *newmode;
1082 struct drm_device *dev = connector->dev;
1083
1084 for (i = 0; i < drm_num_dmt_modes; i++) {
Takashi Iwai7b668eb2012-07-03 11:22:11 +02001085 if (mode_in_range(drm_dmt_modes + i, edid, timing) &&
1086 valid_inferred_mode(connector, drm_dmt_modes + i)) {
Adam Jackson07a5e632009-12-03 17:44:38 -05001087 newmode = drm_mode_duplicate(dev, &drm_dmt_modes[i]);
1088 if (newmode) {
1089 drm_mode_probed_add(connector, newmode);
1090 modes++;
1091 }
1092 }
1093 }
1094
1095 return modes;
1096}
1097
Takashi Iwaic09dedb2012-04-23 17:40:33 +01001098/* fix up 1366x768 mode from 1368x768;
1099 * GFT/CVT can't express 1366 width which isn't dividable by 8
1100 */
1101static void fixup_mode_1366x768(struct drm_display_mode *mode)
1102{
1103 if (mode->hdisplay == 1368 && mode->vdisplay == 768) {
1104 mode->hdisplay = 1366;
1105 mode->hsync_start--;
1106 mode->hsync_end--;
1107 drm_mode_set_name(mode);
1108 }
1109}
1110
Adam Jacksonb309bd32012-04-13 16:33:40 -04001111static int
1112drm_gtf_modes_for_range(struct drm_connector *connector, struct edid *edid,
1113 struct detailed_timing *timing)
1114{
1115 int i, modes = 0;
1116 struct drm_display_mode *newmode;
1117 struct drm_device *dev = connector->dev;
1118
1119 for (i = 0; i < num_extra_modes; i++) {
1120 const struct minimode *m = &extra_modes[i];
1121 newmode = drm_gtf_mode(dev, m->w, m->h, m->r, 0, 0);
Takashi Iwaifc48f162012-04-20 12:59:33 +01001122 if (!newmode)
1123 return modes;
Adam Jacksonb309bd32012-04-13 16:33:40 -04001124
Takashi Iwaic09dedb2012-04-23 17:40:33 +01001125 fixup_mode_1366x768(newmode);
Takashi Iwai7b668eb2012-07-03 11:22:11 +02001126 if (!mode_in_range(newmode, edid, timing) ||
1127 !valid_inferred_mode(connector, newmode)) {
Adam Jacksonb309bd32012-04-13 16:33:40 -04001128 drm_mode_destroy(dev, newmode);
1129 continue;
1130 }
1131
1132 drm_mode_probed_add(connector, newmode);
1133 modes++;
1134 }
1135
1136 return modes;
1137}
1138
1139static int
1140drm_cvt_modes_for_range(struct drm_connector *connector, struct edid *edid,
1141 struct detailed_timing *timing)
1142{
1143 int i, modes = 0;
1144 struct drm_display_mode *newmode;
1145 struct drm_device *dev = connector->dev;
1146 bool rb = drm_monitor_supports_rb(edid);
1147
1148 for (i = 0; i < num_extra_modes; i++) {
1149 const struct minimode *m = &extra_modes[i];
1150 newmode = drm_cvt_mode(dev, m->w, m->h, m->r, rb, 0, 0);
Takashi Iwaifc48f162012-04-20 12:59:33 +01001151 if (!newmode)
1152 return modes;
Adam Jacksonb309bd32012-04-13 16:33:40 -04001153
Takashi Iwaic09dedb2012-04-23 17:40:33 +01001154 fixup_mode_1366x768(newmode);
Takashi Iwai7b668eb2012-07-03 11:22:11 +02001155 if (!mode_in_range(newmode, edid, timing) ||
1156 !valid_inferred_mode(connector, newmode)) {
Adam Jacksonb309bd32012-04-13 16:33:40 -04001157 drm_mode_destroy(dev, newmode);
1158 continue;
1159 }
1160
1161 drm_mode_probed_add(connector, newmode);
1162 modes++;
1163 }
1164
1165 return modes;
1166}
1167
Adam Jackson13931572010-08-03 14:38:19 -04001168static void
1169do_inferred_modes(struct detailed_timing *timing, void *c)
Adam Jackson9340d8c2009-12-03 17:44:40 -05001170{
Adam Jackson13931572010-08-03 14:38:19 -04001171 struct detailed_mode_closure *closure = c;
1172 struct detailed_non_pixel *data = &timing->data.other_data;
Adam Jacksonb309bd32012-04-13 16:33:40 -04001173 struct detailed_data_monitor_range *range = &data->data.range;
Adam Jackson9340d8c2009-12-03 17:44:40 -05001174
Adam Jacksoncb21aaf2012-04-13 16:33:36 -04001175 if (data->type != EDID_DETAIL_MONITOR_RANGE)
1176 return;
1177
1178 closure->modes += drm_dmt_modes_for_range(closure->connector,
1179 closure->edid,
1180 timing);
Adam Jacksonb309bd32012-04-13 16:33:40 -04001181
1182 if (!version_greater(closure->edid, 1, 1))
1183 return; /* GTF not defined yet */
1184
1185 switch (range->flags) {
1186 case 0x02: /* secondary gtf, XXX could do more */
1187 case 0x00: /* default gtf */
1188 closure->modes += drm_gtf_modes_for_range(closure->connector,
1189 closure->edid,
1190 timing);
1191 break;
1192 case 0x04: /* cvt, only in 1.4+ */
1193 if (!version_greater(closure->edid, 1, 3))
1194 break;
1195
1196 closure->modes += drm_cvt_modes_for_range(closure->connector,
1197 closure->edid,
1198 timing);
1199 break;
1200 case 0x01: /* just the ranges, no formula */
1201 default:
1202 break;
1203 }
Adam Jackson9340d8c2009-12-03 17:44:40 -05001204}
1205
Adam Jackson13931572010-08-03 14:38:19 -04001206static int
1207add_inferred_modes(struct drm_connector *connector, struct edid *edid)
1208{
1209 struct detailed_mode_closure closure = {
1210 connector, edid, 0, 0, 0
1211 };
1212
1213 if (version_greater(edid, 1, 0))
1214 drm_for_each_detailed_block((u8 *)edid, do_inferred_modes,
1215 &closure);
1216
1217 return closure.modes;
1218}
1219
Adam Jackson2255be12010-03-29 21:43:22 +00001220static int
1221drm_est3_modes(struct drm_connector *connector, struct detailed_timing *timing)
1222{
1223 int i, j, m, modes = 0;
1224 struct drm_display_mode *mode;
1225 u8 *est = ((u8 *)timing) + 5;
1226
1227 for (i = 0; i < 6; i++) {
1228 for (j = 7; j > 0; j--) {
1229 m = (i * 8) + (7 - j);
Linus Torvaldsaa9f56b2010-08-12 09:21:39 -07001230 if (m >= ARRAY_SIZE(est3_modes))
Adam Jackson2255be12010-03-29 21:43:22 +00001231 break;
1232 if (est[i] & (1 << j)) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001233 mode = drm_mode_find_dmt(connector->dev,
1234 est3_modes[m].w,
1235 est3_modes[m].h,
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001236 est3_modes[m].r,
1237 est3_modes[m].rb);
Adam Jackson2255be12010-03-29 21:43:22 +00001238 if (mode) {
1239 drm_mode_probed_add(connector, mode);
1240 modes++;
1241 }
1242 }
1243 }
1244 }
1245
1246 return modes;
1247}
1248
Adam Jackson13931572010-08-03 14:38:19 -04001249static void
1250do_established_modes(struct detailed_timing *timing, void *c)
Adam Jackson9cf00972009-12-03 17:44:36 -05001251{
Adam Jackson13931572010-08-03 14:38:19 -04001252 struct detailed_mode_closure *closure = c;
Adam Jackson9cf00972009-12-03 17:44:36 -05001253 struct detailed_non_pixel *data = &timing->data.other_data;
Adam Jackson13931572010-08-03 14:38:19 -04001254
1255 if (data->type == EDID_DETAIL_EST_TIMINGS)
1256 closure->modes += drm_est3_modes(closure->connector, timing);
1257}
1258
1259/**
1260 * add_established_modes - get est. modes from EDID and add them
1261 * @edid: EDID block to scan
1262 *
1263 * Each EDID block contains a bitmap of the supported "established modes" list
1264 * (defined above). Tease them out and add them to the global modes list.
1265 */
1266static int
1267add_established_modes(struct drm_connector *connector, struct edid *edid)
1268{
Adam Jackson9cf00972009-12-03 17:44:36 -05001269 struct drm_device *dev = connector->dev;
Adam Jackson13931572010-08-03 14:38:19 -04001270 unsigned long est_bits = edid->established_timings.t1 |
1271 (edid->established_timings.t2 << 8) |
1272 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
1273 int i, modes = 0;
1274 struct detailed_mode_closure closure = {
1275 connector, edid, 0, 0, 0
1276 };
Adam Jackson9cf00972009-12-03 17:44:36 -05001277
Adam Jackson13931572010-08-03 14:38:19 -04001278 for (i = 0; i <= EDID_EST_TIMINGS; i++) {
1279 if (est_bits & (1<<i)) {
1280 struct drm_display_mode *newmode;
1281 newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
1282 if (newmode) {
1283 drm_mode_probed_add(connector, newmode);
1284 modes++;
1285 }
1286 }
Adam Jackson9cf00972009-12-03 17:44:36 -05001287 }
1288
Adam Jackson13931572010-08-03 14:38:19 -04001289 if (version_greater(edid, 1, 0))
1290 drm_for_each_detailed_block((u8 *)edid,
1291 do_established_modes, &closure);
1292
1293 return modes + closure.modes;
1294}
1295
1296static void
1297do_standard_modes(struct detailed_timing *timing, void *c)
1298{
1299 struct detailed_mode_closure *closure = c;
1300 struct detailed_non_pixel *data = &timing->data.other_data;
1301 struct drm_connector *connector = closure->connector;
1302 struct edid *edid = closure->edid;
1303
1304 if (data->type == EDID_DETAIL_STD_MODES) {
1305 int i;
Adam Jackson9cf00972009-12-03 17:44:36 -05001306 for (i = 0; i < 6; i++) {
1307 struct std_timing *std;
1308 struct drm_display_mode *newmode;
1309
1310 std = &data->data.timings[i];
Adam Jackson7a374352010-03-29 21:43:30 +00001311 newmode = drm_mode_std(connector, edid, std,
1312 edid->revision);
Adam Jackson9cf00972009-12-03 17:44:36 -05001313 if (newmode) {
1314 drm_mode_probed_add(connector, newmode);
Adam Jackson13931572010-08-03 14:38:19 -04001315 closure->modes++;
Adam Jackson9cf00972009-12-03 17:44:36 -05001316 }
1317 }
Adam Jackson13931572010-08-03 14:38:19 -04001318 }
1319}
1320
1321/**
1322 * add_standard_modes - get std. modes from EDID and add them
1323 * @edid: EDID block to scan
1324 *
1325 * Standard modes can be calculated using the appropriate standard (DMT,
1326 * GTF or CVT. Grab them from @edid and add them to the list.
1327 */
1328static int
1329add_standard_modes(struct drm_connector *connector, struct edid *edid)
1330{
1331 int i, modes = 0;
1332 struct detailed_mode_closure closure = {
1333 connector, edid, 0, 0, 0
1334 };
1335
1336 for (i = 0; i < EDID_STD_TIMINGS; i++) {
1337 struct drm_display_mode *newmode;
1338
1339 newmode = drm_mode_std(connector, edid,
1340 &edid->standard_timings[i],
1341 edid->revision);
1342 if (newmode) {
1343 drm_mode_probed_add(connector, newmode);
1344 modes++;
1345 }
1346 }
1347
1348 if (version_greater(edid, 1, 0))
1349 drm_for_each_detailed_block((u8 *)edid, do_standard_modes,
1350 &closure);
1351
1352 /* XXX should also look for standard codes in VTB blocks */
1353
1354 return modes + closure.modes;
1355}
1356
Dave Airlief453ba02008-11-07 14:05:41 -08001357static int drm_cvt_modes(struct drm_connector *connector,
1358 struct detailed_timing *timing)
1359{
1360 int i, j, modes = 0;
1361 struct drm_display_mode *newmode;
1362 struct drm_device *dev = connector->dev;
Zhao Yakui5c612592009-06-22 13:17:10 +08001363 struct cvt_timing *cvt;
1364 const int rates[] = { 60, 85, 75, 60, 50 };
1365 const u8 empty[3] = { 0, 0, 0 };
Dave Airlief453ba02008-11-07 14:05:41 -08001366
1367 for (i = 0; i < 4; i++) {
1368 int uninitialized_var(width), height;
1369 cvt = &(timing->data.other_data.data.cvt[i]);
1370
1371 if (!memcmp(cvt->code, empty, 3))
Michel Dänzer0454bea2009-06-15 16:56:07 +02001372 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08001373
1374 height = (cvt->code[0] + ((cvt->code[1] & 0xf0) << 4) + 1) * 2;
Zhao Yakui5c612592009-06-22 13:17:10 +08001375 switch (cvt->code[1] & 0x0c) {
Adam Jacksonf066a172009-09-23 17:31:21 -04001376 case 0x00:
Dave Airlief453ba02008-11-07 14:05:41 -08001377 width = height * 4 / 3;
1378 break;
1379 case 0x04:
1380 width = height * 16 / 9;
1381 break;
1382 case 0x08:
1383 width = height * 16 / 10;
1384 break;
1385 case 0x0c:
Dave Airlief453ba02008-11-07 14:05:41 -08001386 width = height * 15 / 9;
1387 break;
1388 }
1389
1390 for (j = 1; j < 5; j++) {
1391 if (cvt->code[2] & (1 << j)) {
1392 newmode = drm_cvt_mode(dev, width, height,
1393 rates[j], j == 0,
1394 false, false);
1395 if (newmode) {
1396 drm_mode_probed_add(connector, newmode);
1397 modes++;
1398 }
1399 }
1400 }
1401 }
1402
1403 return modes;
1404}
1405
Adam Jackson13931572010-08-03 14:38:19 -04001406static void
1407do_cvt_mode(struct detailed_timing *timing, void *c)
1408{
1409 struct detailed_mode_closure *closure = c;
1410 struct detailed_non_pixel *data = &timing->data.other_data;
1411
1412 if (data->type == EDID_DETAIL_CVT_3BYTE)
1413 closure->modes += drm_cvt_modes(closure->connector, timing);
1414}
Adam Jackson9cf00972009-12-03 17:44:36 -05001415
1416static int
Adam Jackson13931572010-08-03 14:38:19 -04001417add_cvt_modes(struct drm_connector *connector, struct edid *edid)
1418{
1419 struct detailed_mode_closure closure = {
1420 connector, edid, 0, 0, 0
1421 };
Adam Jackson9cf00972009-12-03 17:44:36 -05001422
Adam Jackson13931572010-08-03 14:38:19 -04001423 if (version_greater(edid, 1, 2))
1424 drm_for_each_detailed_block((u8 *)edid, do_cvt_mode, &closure);
Dave Airlief453ba02008-11-07 14:05:41 -08001425
Adam Jackson13931572010-08-03 14:38:19 -04001426 /* XXX should also look for CVT codes in VTB blocks */
1427
1428 return closure.modes;
Dave Airlief453ba02008-11-07 14:05:41 -08001429}
1430
Adam Jackson13931572010-08-03 14:38:19 -04001431static void
1432do_detailed_mode(struct detailed_timing *timing, void *c)
Dave Airlief453ba02008-11-07 14:05:41 -08001433{
Adam Jackson13931572010-08-03 14:38:19 -04001434 struct detailed_mode_closure *closure = c;
Dave Airlief453ba02008-11-07 14:05:41 -08001435 struct drm_display_mode *newmode;
Adam Jackson9cf00972009-12-03 17:44:36 -05001436
1437 if (timing->pixel_clock) {
Adam Jackson13931572010-08-03 14:38:19 -04001438 newmode = drm_mode_detailed(closure->connector->dev,
1439 closure->edid, timing,
1440 closure->quirks);
Dave Airlief453ba02008-11-07 14:05:41 -08001441 if (!newmode)
Adam Jackson13931572010-08-03 14:38:19 -04001442 return;
Adam Jackson9cf00972009-12-03 17:44:36 -05001443
Adam Jackson13931572010-08-03 14:38:19 -04001444 if (closure->preferred)
Dave Airlief453ba02008-11-07 14:05:41 -08001445 newmode->type |= DRM_MODE_TYPE_PREFERRED;
1446
Adam Jackson13931572010-08-03 14:38:19 -04001447 drm_mode_probed_add(closure->connector, newmode);
1448 closure->modes++;
1449 closure->preferred = 0;
Zhao Yakui882f0212009-08-26 18:20:49 +08001450 }
Ma Ling167f3a02009-03-20 14:09:48 +08001451}
1452
Adam Jackson13931572010-08-03 14:38:19 -04001453/*
1454 * add_detailed_modes - Add modes from detailed timings
Dave Airlief453ba02008-11-07 14:05:41 -08001455 * @connector: attached connector
1456 * @edid: EDID block to scan
1457 * @quirks: quirks to apply
Dave Airlief453ba02008-11-07 14:05:41 -08001458 */
Adam Jackson13931572010-08-03 14:38:19 -04001459static int
1460add_detailed_modes(struct drm_connector *connector, struct edid *edid,
1461 u32 quirks)
Dave Airlief453ba02008-11-07 14:05:41 -08001462{
Adam Jackson13931572010-08-03 14:38:19 -04001463 struct detailed_mode_closure closure = {
1464 connector,
1465 edid,
1466 1,
1467 quirks,
1468 0
1469 };
Dave Airlief453ba02008-11-07 14:05:41 -08001470
Adam Jackson13931572010-08-03 14:38:19 -04001471 if (closure.preferred && !version_greater(edid, 1, 3))
1472 closure.preferred =
1473 (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING);
Adam Jacksona327f6b2010-03-29 21:43:25 +00001474
Adam Jackson13931572010-08-03 14:38:19 -04001475 drm_for_each_detailed_block((u8 *)edid, do_detailed_mode, &closure);
Dave Airlief453ba02008-11-07 14:05:41 -08001476
Adam Jackson13931572010-08-03 14:38:19 -04001477 return closure.modes;
Zhao Yakui882f0212009-08-26 18:20:49 +08001478}
Dave Airlief453ba02008-11-07 14:05:41 -08001479
Ma Lingf23c20c2009-03-26 19:26:23 +08001480#define HDMI_IDENTIFIER 0x000C03
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001481#define AUDIO_BLOCK 0x01
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001482#define VIDEO_BLOCK 0x02
Ma Lingf23c20c2009-03-26 19:26:23 +08001483#define VENDOR_BLOCK 0x03
Wu Fengguang76adaa342011-09-05 14:23:20 +08001484#define SPEAKER_BLOCK 0x04
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001485#define EDID_BASIC_AUDIO (1 << 6)
Lars-Peter Clausena988bc72012-04-16 15:16:19 +02001486#define EDID_CEA_YCRCB444 (1 << 5)
1487#define EDID_CEA_YCRCB422 (1 << 4)
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001488
1489/**
1490 * Search EDID for CEA extension block.
1491 */
Ben Skeggseccaca22011-03-30 05:03:47 +00001492u8 *drm_find_cea_extension(struct edid *edid)
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001493{
1494 u8 *edid_ext = NULL;
1495 int i;
1496
1497 /* No EDID or EDID extensions */
1498 if (edid == NULL || edid->extensions == 0)
1499 return NULL;
1500
1501 /* Find CEA extension */
1502 for (i = 0; i < edid->extensions; i++) {
1503 edid_ext = (u8 *)edid + EDID_LENGTH * (i + 1);
1504 if (edid_ext[0] == CEA_EXT)
1505 break;
1506 }
1507
1508 if (i == edid->extensions)
1509 return NULL;
1510
1511 return edid_ext;
1512}
Ben Skeggseccaca22011-03-30 05:03:47 +00001513EXPORT_SYMBOL(drm_find_cea_extension);
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001514
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001515static int
1516do_cea_modes (struct drm_connector *connector, u8 *db, u8 len)
1517{
1518 struct drm_device *dev = connector->dev;
1519 u8 * mode, cea_mode;
1520 int modes = 0;
1521
1522 for (mode = db; mode < db + len; mode++) {
1523 cea_mode = (*mode & 127) - 1; /* CEA modes are numbered 1..127 */
1524 if (cea_mode < drm_num_cea_modes) {
1525 struct drm_display_mode *newmode;
1526 newmode = drm_mode_duplicate(dev,
1527 &edid_cea_modes[cea_mode]);
1528 if (newmode) {
1529 drm_mode_probed_add(connector, newmode);
1530 modes++;
1531 }
1532 }
1533 }
1534
1535 return modes;
1536}
1537
1538static int
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001539cea_db_payload_len(const u8 *db)
1540{
1541 return db[0] & 0x1f;
1542}
1543
1544static int
1545cea_db_tag(const u8 *db)
1546{
1547 return db[0] >> 5;
1548}
1549
1550static int
1551cea_revision(const u8 *cea)
1552{
1553 return cea[1];
1554}
1555
1556static int
1557cea_db_offsets(const u8 *cea, int *start, int *end)
1558{
1559 /* Data block offset in CEA extension block */
1560 *start = 4;
1561 *end = cea[2];
1562 if (*end == 0)
1563 *end = 127;
1564 if (*end < 4 || *end > 127)
1565 return -ERANGE;
1566 return 0;
1567}
1568
1569#define for_each_cea_db(cea, i, start, end) \
1570 for ((i) = (start); (i) < (end) && (i) + cea_db_payload_len(&(cea)[(i)]) < (end); (i) += cea_db_payload_len(&(cea)[(i)]) + 1)
1571
1572static int
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001573add_cea_modes(struct drm_connector *connector, struct edid *edid)
1574{
1575 u8 * cea = drm_find_cea_extension(edid);
1576 u8 * db, dbl;
1577 int modes = 0;
1578
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001579 if (cea && cea_revision(cea) >= 3) {
1580 int i, start, end;
1581
1582 if (cea_db_offsets(cea, &start, &end))
1583 return 0;
1584
1585 for_each_cea_db(cea, i, start, end) {
1586 db = &cea[i];
1587 dbl = cea_db_payload_len(db);
1588
1589 if (cea_db_tag(db) == VIDEO_BLOCK)
Christian Schmidt54ac76f2011-12-19 14:53:16 +00001590 modes += do_cea_modes (connector, db+1, dbl);
1591 }
1592 }
1593
1594 return modes;
1595}
1596
Wu Fengguang76adaa342011-09-05 14:23:20 +08001597static void
Ville Syrjälä85040722012-08-16 14:55:05 +00001598parse_hdmi_vsdb(struct drm_connector *connector, const u8 *db)
Wu Fengguang76adaa342011-09-05 14:23:20 +08001599{
Ville Syrjälä85040722012-08-16 14:55:05 +00001600 u8 len = cea_db_payload_len(db);
Wu Fengguang76adaa342011-09-05 14:23:20 +08001601
Ville Syrjälä85040722012-08-16 14:55:05 +00001602 if (len >= 6) {
1603 connector->eld[5] |= (db[6] >> 7) << 1; /* Supports_AI */
1604 connector->dvi_dual = db[6] & 1;
1605 }
1606 if (len >= 7)
1607 connector->max_tmds_clock = db[7] * 5;
1608 if (len >= 8) {
1609 connector->latency_present[0] = db[8] >> 7;
1610 connector->latency_present[1] = (db[8] >> 6) & 1;
1611 }
1612 if (len >= 9)
1613 connector->video_latency[0] = db[9];
1614 if (len >= 10)
1615 connector->audio_latency[0] = db[10];
1616 if (len >= 11)
1617 connector->video_latency[1] = db[11];
1618 if (len >= 12)
1619 connector->audio_latency[1] = db[12];
Wu Fengguang76adaa342011-09-05 14:23:20 +08001620
1621 DRM_LOG_KMS("HDMI: DVI dual %d, "
1622 "max TMDS clock %d, "
1623 "latency present %d %d, "
1624 "video latency %d %d, "
1625 "audio latency %d %d\n",
1626 connector->dvi_dual,
1627 connector->max_tmds_clock,
1628 (int) connector->latency_present[0],
1629 (int) connector->latency_present[1],
1630 connector->video_latency[0],
1631 connector->video_latency[1],
1632 connector->audio_latency[0],
1633 connector->audio_latency[1]);
1634}
1635
1636static void
1637monitor_name(struct detailed_timing *t, void *data)
1638{
1639 if (t->data.other_data.type == EDID_DETAIL_MONITOR_NAME)
1640 *(u8 **)data = t->data.other_data.data.str.str;
1641}
1642
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001643static bool cea_db_is_hdmi_vsdb(const u8 *db)
1644{
1645 int hdmi_id;
1646
1647 if (cea_db_tag(db) != VENDOR_BLOCK)
1648 return false;
1649
1650 if (cea_db_payload_len(db) < 5)
1651 return false;
1652
1653 hdmi_id = db[1] | (db[2] << 8) | (db[3] << 16);
1654
1655 return hdmi_id == HDMI_IDENTIFIER;
1656}
1657
Wu Fengguang76adaa342011-09-05 14:23:20 +08001658/**
1659 * drm_edid_to_eld - build ELD from EDID
1660 * @connector: connector corresponding to the HDMI/DP sink
1661 * @edid: EDID to parse
1662 *
1663 * Fill the ELD (EDID-Like Data) buffer for passing to the audio driver.
1664 * Some ELD fields are left to the graphics driver caller:
1665 * - Conn_Type
1666 * - HDCP
1667 * - Port_ID
1668 */
1669void drm_edid_to_eld(struct drm_connector *connector, struct edid *edid)
1670{
1671 uint8_t *eld = connector->eld;
1672 u8 *cea;
1673 u8 *name;
1674 u8 *db;
1675 int sad_count = 0;
1676 int mnl;
1677 int dbl;
1678
1679 memset(eld, 0, sizeof(connector->eld));
1680
1681 cea = drm_find_cea_extension(edid);
1682 if (!cea) {
1683 DRM_DEBUG_KMS("ELD: no CEA Extension found\n");
1684 return;
1685 }
1686
1687 name = NULL;
1688 drm_for_each_detailed_block((u8 *)edid, monitor_name, &name);
1689 for (mnl = 0; name && mnl < 13; mnl++) {
1690 if (name[mnl] == 0x0a)
1691 break;
1692 eld[20 + mnl] = name[mnl];
1693 }
1694 eld[4] = (cea[1] << 5) | mnl;
1695 DRM_DEBUG_KMS("ELD monitor %s\n", eld + 20);
1696
1697 eld[0] = 2 << 3; /* ELD version: 2 */
1698
1699 eld[16] = edid->mfg_id[0];
1700 eld[17] = edid->mfg_id[1];
1701 eld[18] = edid->prod_code[0];
1702 eld[19] = edid->prod_code[1];
1703
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001704 if (cea_revision(cea) >= 3) {
1705 int i, start, end;
1706
1707 if (cea_db_offsets(cea, &start, &end)) {
1708 start = 0;
1709 end = 0;
1710 }
1711
1712 for_each_cea_db(cea, i, start, end) {
1713 db = &cea[i];
1714 dbl = cea_db_payload_len(db);
1715
1716 switch (cea_db_tag(db)) {
Christian Schmidta0ab7342011-12-19 20:03:38 +01001717 case AUDIO_BLOCK:
1718 /* Audio Data Block, contains SADs */
1719 sad_count = dbl / 3;
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001720 if (dbl >= 1)
1721 memcpy(eld + 20 + mnl, &db[1], dbl);
Christian Schmidta0ab7342011-12-19 20:03:38 +01001722 break;
1723 case SPEAKER_BLOCK:
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001724 /* Speaker Allocation Data Block */
1725 if (dbl >= 1)
1726 eld[7] = db[1];
Christian Schmidta0ab7342011-12-19 20:03:38 +01001727 break;
1728 case VENDOR_BLOCK:
1729 /* HDMI Vendor-Specific Data Block */
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001730 if (cea_db_is_hdmi_vsdb(db))
Christian Schmidta0ab7342011-12-19 20:03:38 +01001731 parse_hdmi_vsdb(connector, db);
1732 break;
1733 default:
1734 break;
1735 }
Wu Fengguang76adaa342011-09-05 14:23:20 +08001736 }
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001737 }
Wu Fengguang76adaa342011-09-05 14:23:20 +08001738 eld[5] |= sad_count << 4;
1739 eld[2] = (20 + mnl + sad_count * 3 + 3) / 4;
1740
1741 DRM_DEBUG_KMS("ELD size %d, SAD count %d\n", (int)eld[2], sad_count);
1742}
1743EXPORT_SYMBOL(drm_edid_to_eld);
1744
1745/**
1746 * drm_av_sync_delay - HDMI/DP sink audio-video sync delay in millisecond
1747 * @connector: connector associated with the HDMI/DP sink
1748 * @mode: the display mode
1749 */
1750int drm_av_sync_delay(struct drm_connector *connector,
1751 struct drm_display_mode *mode)
1752{
1753 int i = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1754 int a, v;
1755
1756 if (!connector->latency_present[0])
1757 return 0;
1758 if (!connector->latency_present[1])
1759 i = 0;
1760
1761 a = connector->audio_latency[i];
1762 v = connector->video_latency[i];
1763
1764 /*
1765 * HDMI/DP sink doesn't support audio or video?
1766 */
1767 if (a == 255 || v == 255)
1768 return 0;
1769
1770 /*
1771 * Convert raw EDID values to millisecond.
1772 * Treat unknown latency as 0ms.
1773 */
1774 if (a)
1775 a = min(2 * (a - 1), 500);
1776 if (v)
1777 v = min(2 * (v - 1), 500);
1778
1779 return max(v - a, 0);
1780}
1781EXPORT_SYMBOL(drm_av_sync_delay);
1782
1783/**
1784 * drm_select_eld - select one ELD from multiple HDMI/DP sinks
1785 * @encoder: the encoder just changed display mode
1786 * @mode: the adjusted display mode
1787 *
1788 * It's possible for one encoder to be associated with multiple HDMI/DP sinks.
1789 * The policy is now hard coded to simply use the first HDMI/DP sink's ELD.
1790 */
1791struct drm_connector *drm_select_eld(struct drm_encoder *encoder,
1792 struct drm_display_mode *mode)
1793{
1794 struct drm_connector *connector;
1795 struct drm_device *dev = encoder->dev;
1796
1797 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1798 if (connector->encoder == encoder && connector->eld[0])
1799 return connector;
1800
1801 return NULL;
1802}
1803EXPORT_SYMBOL(drm_select_eld);
1804
Ma Lingf23c20c2009-03-26 19:26:23 +08001805/**
1806 * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
1807 * @edid: monitor EDID information
1808 *
1809 * Parse the CEA extension according to CEA-861-B.
1810 * Return true if HDMI, false if not or unknown.
1811 */
1812bool drm_detect_hdmi_monitor(struct edid *edid)
1813{
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001814 u8 *edid_ext;
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001815 int i;
Ma Lingf23c20c2009-03-26 19:26:23 +08001816 int start_offset, end_offset;
Ma Lingf23c20c2009-03-26 19:26:23 +08001817
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001818 edid_ext = drm_find_cea_extension(edid);
1819 if (!edid_ext)
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001820 return false;
Ma Lingf23c20c2009-03-26 19:26:23 +08001821
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001822 if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001823 return false;
Ma Lingf23c20c2009-03-26 19:26:23 +08001824
1825 /*
1826 * Because HDMI identifier is in Vendor Specific Block,
1827 * search it from all data blocks of CEA extension.
1828 */
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001829 for_each_cea_db(edid_ext, i, start_offset, end_offset) {
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001830 if (cea_db_is_hdmi_vsdb(&edid_ext[i]))
1831 return true;
Ma Lingf23c20c2009-03-26 19:26:23 +08001832 }
1833
Ville Syrjälä14f77fd2012-08-16 14:55:06 +00001834 return false;
Ma Lingf23c20c2009-03-26 19:26:23 +08001835}
1836EXPORT_SYMBOL(drm_detect_hdmi_monitor);
1837
Dave Airlief453ba02008-11-07 14:05:41 -08001838/**
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001839 * drm_detect_monitor_audio - check monitor audio capability
1840 *
1841 * Monitor should have CEA extension block.
1842 * If monitor has 'basic audio', but no CEA audio blocks, it's 'basic
1843 * audio' only. If there is any audio extension block and supported
1844 * audio format, assume at least 'basic audio' support, even if 'basic
1845 * audio' is not defined in EDID.
1846 *
1847 */
1848bool drm_detect_monitor_audio(struct edid *edid)
1849{
1850 u8 *edid_ext;
1851 int i, j;
1852 bool has_audio = false;
1853 int start_offset, end_offset;
1854
1855 edid_ext = drm_find_cea_extension(edid);
1856 if (!edid_ext)
1857 goto end;
1858
1859 has_audio = ((edid_ext[3] & EDID_BASIC_AUDIO) != 0);
1860
1861 if (has_audio) {
1862 DRM_DEBUG_KMS("Monitor has basic audio support\n");
1863 goto end;
1864 }
1865
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001866 if (cea_db_offsets(edid_ext, &start_offset, &end_offset))
1867 goto end;
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001868
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001869 for_each_cea_db(edid_ext, i, start_offset, end_offset) {
1870 if (cea_db_tag(&edid_ext[i]) == AUDIO_BLOCK) {
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001871 has_audio = true;
Ville Syrjälä9e50b9d2012-08-16 14:55:04 +00001872 for (j = 1; j < cea_db_payload_len(&edid_ext[i]) + 1; j += 3)
Zhenyu Wang8fe97902010-09-19 14:27:28 +08001873 DRM_DEBUG_KMS("CEA audio format %d\n",
1874 (edid_ext[i + j] >> 3) & 0xf);
1875 goto end;
1876 }
1877 }
1878end:
1879 return has_audio;
1880}
1881EXPORT_SYMBOL(drm_detect_monitor_audio);
1882
1883/**
Jesse Barnes3b112282011-04-15 12:49:23 -07001884 * drm_add_display_info - pull display info out if present
1885 * @edid: EDID data
1886 * @info: display info (attached to connector)
1887 *
1888 * Grab any available display info and stuff it into the drm_display_info
1889 * structure that's part of the connector. Useful for tracking bpp and
1890 * color spaces.
1891 */
1892static void drm_add_display_info(struct edid *edid,
1893 struct drm_display_info *info)
1894{
Jesse Barnesebec9a72011-08-03 09:22:54 -07001895 u8 *edid_ext;
1896
Jesse Barnes3b112282011-04-15 12:49:23 -07001897 info->width_mm = edid->width_cm * 10;
1898 info->height_mm = edid->height_cm * 10;
1899
1900 /* driver figures it out in this case */
1901 info->bpc = 0;
Jesse Barnesda05a5a72011-04-15 13:48:57 -07001902 info->color_formats = 0;
Jesse Barnes3b112282011-04-15 12:49:23 -07001903
Lars-Peter Clausena988bc72012-04-16 15:16:19 +02001904 if (edid->revision < 3)
Jesse Barnes3b112282011-04-15 12:49:23 -07001905 return;
1906
1907 if (!(edid->input & DRM_EDID_INPUT_DIGITAL))
1908 return;
1909
Lars-Peter Clausena988bc72012-04-16 15:16:19 +02001910 /* Get data from CEA blocks if present */
1911 edid_ext = drm_find_cea_extension(edid);
1912 if (edid_ext) {
1913 info->cea_rev = edid_ext[1];
1914
1915 /* The existence of a CEA block should imply RGB support */
1916 info->color_formats = DRM_COLOR_FORMAT_RGB444;
1917 if (edid_ext[3] & EDID_CEA_YCRCB444)
1918 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
1919 if (edid_ext[3] & EDID_CEA_YCRCB422)
1920 info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
1921 }
1922
1923 /* Only defined for 1.4 with digital displays */
1924 if (edid->revision < 4)
1925 return;
1926
Jesse Barnes3b112282011-04-15 12:49:23 -07001927 switch (edid->input & DRM_EDID_DIGITAL_DEPTH_MASK) {
1928 case DRM_EDID_DIGITAL_DEPTH_6:
1929 info->bpc = 6;
1930 break;
1931 case DRM_EDID_DIGITAL_DEPTH_8:
1932 info->bpc = 8;
1933 break;
1934 case DRM_EDID_DIGITAL_DEPTH_10:
1935 info->bpc = 10;
1936 break;
1937 case DRM_EDID_DIGITAL_DEPTH_12:
1938 info->bpc = 12;
1939 break;
1940 case DRM_EDID_DIGITAL_DEPTH_14:
1941 info->bpc = 14;
1942 break;
1943 case DRM_EDID_DIGITAL_DEPTH_16:
1944 info->bpc = 16;
1945 break;
1946 case DRM_EDID_DIGITAL_DEPTH_UNDEF:
1947 default:
1948 info->bpc = 0;
1949 break;
1950 }
Jesse Barnesda05a5a72011-04-15 13:48:57 -07001951
Lars-Peter Clausena988bc72012-04-16 15:16:19 +02001952 info->color_formats |= DRM_COLOR_FORMAT_RGB444;
Lars-Peter Clausenee588082012-04-16 15:16:18 +02001953 if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB444)
1954 info->color_formats |= DRM_COLOR_FORMAT_YCRCB444;
1955 if (edid->features & DRM_EDID_FEATURE_RGB_YCRCB422)
1956 info->color_formats |= DRM_COLOR_FORMAT_YCRCB422;
Jesse Barnes3b112282011-04-15 12:49:23 -07001957}
1958
1959/**
Dave Airlief453ba02008-11-07 14:05:41 -08001960 * drm_add_edid_modes - add modes from EDID data, if available
1961 * @connector: connector we're probing
1962 * @edid: edid data
1963 *
1964 * Add the specified modes to the connector's mode list.
1965 *
1966 * Return number of modes added or 0 if we couldn't find any.
1967 */
1968int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
1969{
1970 int num_modes = 0;
1971 u32 quirks;
1972
1973 if (edid == NULL) {
1974 return 0;
1975 }
Alex Deucher3c537882010-02-05 04:21:19 -05001976 if (!drm_edid_is_valid(edid)) {
Jordan Crousedcdb1672010-05-27 13:40:25 -06001977 dev_warn(connector->dev->dev, "%s: EDID invalid.\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001978 drm_get_connector_name(connector));
1979 return 0;
1980 }
1981
1982 quirks = edid_get_quirks(edid);
1983
Adam Jacksonc867df72010-03-29 21:43:21 +00001984 /*
1985 * EDID spec says modes should be preferred in this order:
1986 * - preferred detailed mode
1987 * - other detailed modes from base block
1988 * - detailed modes from extension blocks
1989 * - CVT 3-byte code modes
1990 * - standard timing codes
1991 * - established timing codes
1992 * - modes inferred from GTF or CVT range information
1993 *
Adam Jackson13931572010-08-03 14:38:19 -04001994 * We get this pretty much right.
Adam Jacksonc867df72010-03-29 21:43:21 +00001995 *
1996 * XXX order for additional mode types in extension blocks?
1997 */
Adam Jackson13931572010-08-03 14:38:19 -04001998 num_modes += add_detailed_modes(connector, edid, quirks);
1999 num_modes += add_cvt_modes(connector, edid);
Adam Jacksonc867df72010-03-29 21:43:21 +00002000 num_modes += add_standard_modes(connector, edid);
2001 num_modes += add_established_modes(connector, edid);
Adam Jackson13931572010-08-03 14:38:19 -04002002 num_modes += add_inferred_modes(connector, edid);
Christian Schmidt54ac76f2011-12-19 14:53:16 +00002003 num_modes += add_cea_modes(connector, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002004
2005 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
2006 edid_fixup_preferred(connector, quirks);
2007
Jesse Barnes3b112282011-04-15 12:49:23 -07002008 drm_add_display_info(edid, &connector->display_info);
Dave Airlief453ba02008-11-07 14:05:41 -08002009
2010 return num_modes;
2011}
2012EXPORT_SYMBOL(drm_add_edid_modes);
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08002013
2014/**
2015 * drm_add_modes_noedid - add modes for the connectors without EDID
2016 * @connector: connector we're probing
2017 * @hdisplay: the horizontal display limit
2018 * @vdisplay: the vertical display limit
2019 *
2020 * Add the specified modes to the connector's mode list. Only when the
2021 * hdisplay/vdisplay is not beyond the given limit, it will be added.
2022 *
2023 * Return number of modes added or 0 if we couldn't find any.
2024 */
2025int drm_add_modes_noedid(struct drm_connector *connector,
2026 int hdisplay, int vdisplay)
2027{
2028 int i, count, num_modes = 0;
Chris Wilsonb1f559e2011-01-26 09:49:47 +00002029 struct drm_display_mode *mode;
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08002030 struct drm_device *dev = connector->dev;
2031
2032 count = sizeof(drm_dmt_modes) / sizeof(struct drm_display_mode);
2033 if (hdisplay < 0)
2034 hdisplay = 0;
2035 if (vdisplay < 0)
2036 vdisplay = 0;
2037
2038 for (i = 0; i < count; i++) {
Chris Wilsonb1f559e2011-01-26 09:49:47 +00002039 const struct drm_display_mode *ptr = &drm_dmt_modes[i];
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08002040 if (hdisplay && vdisplay) {
2041 /*
2042 * Only when two are valid, they will be used to check
2043 * whether the mode should be added to the mode list of
2044 * the connector.
2045 */
2046 if (ptr->hdisplay > hdisplay ||
2047 ptr->vdisplay > vdisplay)
2048 continue;
2049 }
Adam Jacksonf985ded2009-11-23 14:23:04 -05002050 if (drm_mode_vrefresh(ptr) > 61)
2051 continue;
Zhao Yakuif0fda0a2009-09-03 09:33:48 +08002052 mode = drm_mode_duplicate(dev, ptr);
2053 if (mode) {
2054 drm_mode_probed_add(connector, mode);
2055 num_modes++;
2056 }
2057 }
2058 return num_modes;
2059}
2060EXPORT_SYMBOL(drm_add_modes_noedid);