blob: 7f2728bbc16c7078e7e249d54f68743c228d4f55 [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>
5 *
6 * DDC probing routines (drm_ddc_read & drm_do_probe_ddc_edid) originally from
7 * FB layer.
8 * Copyright (C) 2006 Dennis Munsie <dmunsie@cecropia.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sub license,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
19 * of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29#include <linux/kernel.h>
30#include <linux/i2c.h>
31#include <linux/i2c-algo-bit.h>
32#include "drmP.h"
33#include "drm_edid.h"
34
35/*
36 * TODO:
37 * - support EDID 1.4 (incl. CE blocks)
38 */
39
40/*
41 * EDID blocks out in the wild have a variety of bugs, try to collect
42 * them here (note that userspace may work around broken monitors first,
43 * but fixes should make their way here so that the kernel "just works"
44 * on as many displays as possible).
45 */
46
47/* First detailed mode wrong, use largest 60Hz mode */
48#define EDID_QUIRK_PREFER_LARGE_60 (1 << 0)
49/* Reported 135MHz pixel clock is too high, needs adjustment */
50#define EDID_QUIRK_135_CLOCK_TOO_HIGH (1 << 1)
51/* Prefer the largest mode at 75 Hz */
52#define EDID_QUIRK_PREFER_LARGE_75 (1 << 2)
53/* Detail timing is in cm not mm */
54#define EDID_QUIRK_DETAILED_IN_CM (1 << 3)
55/* Detailed timing descriptors have bogus size values, so just take the
56 * maximum size and use that.
57 */
58#define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE (1 << 4)
59/* Monitor forgot to set the first detailed is preferred bit. */
60#define EDID_QUIRK_FIRST_DETAILED_PREFERRED (1 << 5)
61/* use +hsync +vsync for detailed mode */
62#define EDID_QUIRK_DETAILED_SYNC_PP (1 << 6)
63
64static struct edid_quirk {
65 char *vendor;
66 int product_id;
67 u32 quirks;
68} edid_quirk_list[] = {
69 /* Acer AL1706 */
70 { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
71 /* Acer F51 */
72 { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
73 /* Unknown Acer */
74 { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
75
76 /* Belinea 10 15 55 */
77 { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
78 { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
79
80 /* Envision Peripherals, Inc. EN-7100e */
81 { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
82
83 /* Funai Electronics PM36B */
84 { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
85 EDID_QUIRK_DETAILED_IN_CM },
86
87 /* LG Philips LCD LP154W01-A5 */
88 { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
89 { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
90
91 /* Philips 107p5 CRT */
92 { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
93
94 /* Proview AY765C */
95 { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
96
97 /* Samsung SyncMaster 205BW. Note: irony */
98 { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
99 /* Samsung SyncMaster 22[5-6]BW */
100 { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
101 { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
102};
103
104
105/* Valid EDID header has these bytes */
106static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
107
108/**
109 * edid_is_valid - sanity check EDID data
110 * @edid: EDID data
111 *
112 * Sanity check the EDID block by looking at the header, the version number
113 * and the checksum. Return 0 if the EDID doesn't check out, or 1 if it's
114 * valid.
115 */
116static bool edid_is_valid(struct edid *edid)
117{
118 int i;
119 u8 csum = 0;
120 u8 *raw_edid = (u8 *)edid;
121
122 if (memcmp(edid->header, edid_header, sizeof(edid_header)))
123 goto bad;
124 if (edid->version != 1) {
125 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
126 goto bad;
127 }
Jesse Barnesb94ee652009-04-02 14:56:24 -0700128 if (edid->revision > 4)
129 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800130
131 for (i = 0; i < EDID_LENGTH; i++)
132 csum += raw_edid[i];
133 if (csum) {
134 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
135 goto bad;
136 }
137
138 return 1;
139
140bad:
141 if (raw_edid) {
142 DRM_ERROR("Raw EDID:\n");
143 print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
144 printk("\n");
145 }
146 return 0;
147}
148
149/**
150 * edid_vendor - match a string against EDID's obfuscated vendor field
151 * @edid: EDID to match
152 * @vendor: vendor string
153 *
154 * Returns true if @vendor is in @edid, false otherwise
155 */
156static bool edid_vendor(struct edid *edid, char *vendor)
157{
158 char edid_vendor[3];
159
160 edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
161 edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
162 ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
Dave Airlie16456c82009-04-03 09:10:33 +1000163 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
Dave Airlief453ba02008-11-07 14:05:41 -0800164
165 return !strncmp(edid_vendor, vendor, 3);
166}
167
168/**
169 * edid_get_quirks - return quirk flags for a given EDID
170 * @edid: EDID to process
171 *
172 * This tells subsequent routines what fixes they need to apply.
173 */
174static u32 edid_get_quirks(struct edid *edid)
175{
176 struct edid_quirk *quirk;
177 int i;
178
179 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
180 quirk = &edid_quirk_list[i];
181
182 if (edid_vendor(edid, quirk->vendor) &&
183 (EDID_PRODUCT_ID(edid) == quirk->product_id))
184 return quirk->quirks;
185 }
186
187 return 0;
188}
189
190#define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
191#define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
192
193
194/**
195 * edid_fixup_preferred - set preferred modes based on quirk list
196 * @connector: has mode list to fix up
197 * @quirks: quirks list
198 *
199 * Walk the mode list for @connector, clearing the preferred status
200 * on existing modes and setting it anew for the right mode ala @quirks.
201 */
202static void edid_fixup_preferred(struct drm_connector *connector,
203 u32 quirks)
204{
205 struct drm_display_mode *t, *cur_mode, *preferred_mode;
Dave Airlief8906072008-12-18 16:59:02 +1000206 int target_refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800207
208 if (list_empty(&connector->probed_modes))
209 return;
210
211 if (quirks & EDID_QUIRK_PREFER_LARGE_60)
212 target_refresh = 60;
213 if (quirks & EDID_QUIRK_PREFER_LARGE_75)
214 target_refresh = 75;
215
216 preferred_mode = list_first_entry(&connector->probed_modes,
217 struct drm_display_mode, head);
218
219 list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
220 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
221
222 if (cur_mode == preferred_mode)
223 continue;
224
225 /* Largest mode is preferred */
226 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
227 preferred_mode = cur_mode;
228
229 /* At a given size, try to get closest to target refresh */
230 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
231 MODE_REFRESH_DIFF(cur_mode, target_refresh) <
232 MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
233 preferred_mode = cur_mode;
234 }
235 }
236
237 preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
238}
239
240/**
241 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
242 * @t: standard timing params
243 *
244 * Take the standard timing params (in this case width, aspect, and refresh)
245 * and convert them into a real mode using CVT.
246 *
247 * Punts for now, but should eventually use the FB layer's CVT based mode
248 * generation code.
249 */
250struct drm_display_mode *drm_mode_std(struct drm_device *dev,
251 struct std_timing *t)
252{
253 struct drm_display_mode *mode;
254 int hsize = t->hsize * 8 + 248, vsize;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200255 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
256 >> EDID_TIMING_ASPECT_SHIFT;
Dave Airlief453ba02008-11-07 14:05:41 -0800257
258 mode = drm_mode_create(dev);
259 if (!mode)
260 return NULL;
261
Michel Dänzer0454bea2009-06-15 16:56:07 +0200262 if (aspect_ratio == 0)
Dave Airlief453ba02008-11-07 14:05:41 -0800263 vsize = (hsize * 10) / 16;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200264 else if (aspect_ratio == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800265 vsize = (hsize * 3) / 4;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200266 else if (aspect_ratio == 2)
Dave Airlief453ba02008-11-07 14:05:41 -0800267 vsize = (hsize * 4) / 5;
268 else
269 vsize = (hsize * 9) / 16;
270
271 drm_mode_set_name(mode);
272
273 return mode;
274}
275
276/**
277 * drm_mode_detailed - create a new mode from an EDID detailed timing section
278 * @dev: DRM device (needed to create new mode)
279 * @edid: EDID block
280 * @timing: EDID detailed timing info
281 * @quirks: quirks to apply
282 *
283 * An EDID detailed timing block contains enough info for us to create and
284 * return a new struct drm_display_mode.
285 */
286static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
287 struct edid *edid,
288 struct detailed_timing *timing,
289 u32 quirks)
290{
291 struct drm_display_mode *mode;
292 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200293 unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
294 unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
295 unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
296 unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200297 unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
298 unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
299 unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
300 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 -0800301
Adam Jacksonfc438962009-06-04 10:20:34 +1000302 /* ignore tiny modes */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200303 if (hactive < 64 || vactive < 64)
Adam Jacksonfc438962009-06-04 10:20:34 +1000304 return NULL;
305
Michel Dänzer0454bea2009-06-15 16:56:07 +0200306 if (pt->misc & DRM_EDID_PT_STEREO) {
Dave Airlief453ba02008-11-07 14:05:41 -0800307 printk(KERN_WARNING "stereo mode not supported\n");
308 return NULL;
309 }
Michel Dänzer0454bea2009-06-15 16:56:07 +0200310 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800311 printk(KERN_WARNING "integrated sync not supported\n");
312 return NULL;
313 }
314
315 mode = drm_mode_create(dev);
316 if (!mode)
317 return NULL;
318
319 mode->type = DRM_MODE_TYPE_DRIVER;
320
321 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
Michel Dänzer0454bea2009-06-15 16:56:07 +0200322 timing->pixel_clock = cpu_to_le16(1088);
Dave Airlief453ba02008-11-07 14:05:41 -0800323
Michel Dänzer0454bea2009-06-15 16:56:07 +0200324 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
Dave Airlief453ba02008-11-07 14:05:41 -0800325
Michel Dänzer0454bea2009-06-15 16:56:07 +0200326 mode->hdisplay = hactive;
327 mode->hsync_start = mode->hdisplay + hsync_offset;
328 mode->hsync_end = mode->hsync_start + hsync_pulse_width;
329 mode->htotal = mode->hdisplay + hblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800330
Michel Dänzer0454bea2009-06-15 16:56:07 +0200331 mode->vdisplay = vactive;
332 mode->vsync_start = mode->vdisplay + vsync_offset;
333 mode->vsync_end = mode->vsync_start + vsync_pulse_width;
334 mode->vtotal = mode->vdisplay + vblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800335
336 drm_mode_set_name(mode);
337
Michel Dänzer0454bea2009-06-15 16:56:07 +0200338 if (pt->misc & DRM_EDID_PT_INTERLACED)
Dave Airlief453ba02008-11-07 14:05:41 -0800339 mode->flags |= DRM_MODE_FLAG_INTERLACE;
340
341 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
Michel Dänzer0454bea2009-06-15 16:56:07 +0200342 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
Dave Airlief453ba02008-11-07 14:05:41 -0800343 }
344
Michel Dänzer0454bea2009-06-15 16:56:07 +0200345 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
346 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
347 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
348 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
Dave Airlief453ba02008-11-07 14:05:41 -0800349
Michel Dänzere14cbee2009-06-23 12:36:32 +0200350 mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
351 mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
Dave Airlief453ba02008-11-07 14:05:41 -0800352
353 if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
354 mode->width_mm *= 10;
355 mode->height_mm *= 10;
356 }
357
358 if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
359 mode->width_mm = edid->width_cm * 10;
360 mode->height_mm = edid->height_cm * 10;
361 }
362
363 return mode;
364}
365
366/*
367 * Detailed mode info for the EDID "established modes" data to use.
368 */
369static struct drm_display_mode edid_est_modes[] = {
370 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
371 968, 1056, 0, 600, 601, 605, 628, 0,
372 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
373 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
374 896, 1024, 0, 600, 601, 603, 625, 0,
375 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
376 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
377 720, 840, 0, 480, 481, 484, 500, 0,
378 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
379 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
380 704, 832, 0, 480, 489, 491, 520, 0,
381 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
382 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
383 768, 864, 0, 480, 483, 486, 525, 0,
384 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
385 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
386 752, 800, 0, 480, 490, 492, 525, 0,
387 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
388 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
389 846, 900, 0, 400, 421, 423, 449, 0,
390 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
391 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
392 846, 900, 0, 400, 412, 414, 449, 0,
393 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
394 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
395 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
396 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
397 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
398 1136, 1312, 0, 768, 769, 772, 800, 0,
399 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
400 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
401 1184, 1328, 0, 768, 771, 777, 806, 0,
402 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
403 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
404 1184, 1344, 0, 768, 771, 777, 806, 0,
405 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
406 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
407 1208, 1264, 0, 768, 768, 776, 817, 0,
408 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
409 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
410 928, 1152, 0, 624, 625, 628, 667, 0,
411 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
412 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
413 896, 1056, 0, 600, 601, 604, 625, 0,
414 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
415 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
416 976, 1040, 0, 600, 637, 643, 666, 0,
417 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
418 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
419 1344, 1600, 0, 864, 865, 868, 900, 0,
420 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
421};
422
423#define EDID_EST_TIMINGS 16
424#define EDID_STD_TIMINGS 8
425#define EDID_DETAILED_TIMINGS 4
426
427/**
428 * add_established_modes - get est. modes from EDID and add them
429 * @edid: EDID block to scan
430 *
431 * Each EDID block contains a bitmap of the supported "established modes" list
432 * (defined above). Tease them out and add them to the global modes list.
433 */
434static int add_established_modes(struct drm_connector *connector, struct edid *edid)
435{
436 struct drm_device *dev = connector->dev;
437 unsigned long est_bits = edid->established_timings.t1 |
438 (edid->established_timings.t2 << 8) |
439 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
440 int i, modes = 0;
441
442 for (i = 0; i <= EDID_EST_TIMINGS; i++)
443 if (est_bits & (1<<i)) {
444 struct drm_display_mode *newmode;
445 newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
446 if (newmode) {
447 drm_mode_probed_add(connector, newmode);
448 modes++;
449 }
450 }
451
452 return modes;
453}
454
455/**
456 * add_standard_modes - get std. modes from EDID and add them
457 * @edid: EDID block to scan
458 *
459 * Standard modes can be calculated using the CVT standard. Grab them from
460 * @edid, calculate them, and add them to the list.
461 */
462static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
463{
464 struct drm_device *dev = connector->dev;
465 int i, modes = 0;
466
467 for (i = 0; i < EDID_STD_TIMINGS; i++) {
468 struct std_timing *t = &edid->standard_timings[i];
469 struct drm_display_mode *newmode;
470
471 /* If std timings bytes are 1, 1 it's empty */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200472 if (t->hsize == 1 && t->vfreq_aspect == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800473 continue;
474
475 newmode = drm_mode_std(dev, &edid->standard_timings[i]);
476 if (newmode) {
477 drm_mode_probed_add(connector, newmode);
478 modes++;
479 }
480 }
481
482 return modes;
483}
484
485/**
486 * add_detailed_modes - get detailed mode info from EDID data
487 * @connector: attached connector
488 * @edid: EDID block to scan
489 * @quirks: quirks to apply
490 *
491 * Some of the detailed timing sections may contain mode information. Grab
492 * it and add it to the list.
493 */
494static int add_detailed_info(struct drm_connector *connector,
495 struct edid *edid, u32 quirks)
496{
497 struct drm_device *dev = connector->dev;
498 int i, j, modes = 0;
499
500 for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
501 struct detailed_timing *timing = &edid->detailed_timings[i];
502 struct detailed_non_pixel *data = &timing->data.other_data;
503 struct drm_display_mode *newmode;
504
Dave Airlieebb177d2009-08-15 12:25:08 +1000505 /* X server check is version 1.1 or higher */
506 if (edid->version == 1 && edid->revision >= 1 &&
507 !timing->pixel_clock) {
508 /* Other timing or info */
509 switch (data->type) {
510 case EDID_DETAIL_MONITOR_SERIAL:
511 break;
512 case EDID_DETAIL_MONITOR_STRING:
513 break;
514 case EDID_DETAIL_MONITOR_RANGE:
515 /* Get monitor range data */
516 break;
517 case EDID_DETAIL_MONITOR_NAME:
518 break;
519 case EDID_DETAIL_MONITOR_CPDATA:
520 break;
521 case EDID_DETAIL_STD_MODES:
522 /* Five modes per detailed section */
523 for (j = 0; j < 5; i++) {
524 struct std_timing *std;
525 struct drm_display_mode *newmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800526
Dave Airlieebb177d2009-08-15 12:25:08 +1000527 std = &data->data.timings[j];
528 newmode = drm_mode_std(dev, std);
529 if (newmode) {
530 drm_mode_probed_add(connector, newmode);
531 modes++;
532 }
533 }
534 break;
535 default:
536 break;
537 }
538 } else {
Dave Airlief453ba02008-11-07 14:05:41 -0800539 newmode = drm_mode_detailed(dev, edid, timing, quirks);
540 if (!newmode)
541 continue;
542
543 /* First detailed mode is preferred */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200544 if (i == 0 && (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING))
Dave Airlief453ba02008-11-07 14:05:41 -0800545 newmode->type |= DRM_MODE_TYPE_PREFERRED;
546 drm_mode_probed_add(connector, newmode);
547
548 modes++;
Dave Airlief453ba02008-11-07 14:05:41 -0800549 }
550 }
551
552 return modes;
553}
554
555#define DDC_ADDR 0x50
Ma Ling167f3a02009-03-20 14:09:48 +0800556/**
557 * Get EDID information via I2C.
558 *
559 * \param adapter : i2c device adaptor
560 * \param buf : EDID data buffer to be filled
561 * \param len : EDID data buffer length
562 * \return 0 on success or -1 on failure.
563 *
564 * Try to fetch EDID information by calling i2c driver function.
565 */
566int drm_do_probe_ddc_edid(struct i2c_adapter *adapter,
567 unsigned char *buf, int len)
Dave Airlief453ba02008-11-07 14:05:41 -0800568{
569 unsigned char start = 0x0;
Dave Airlief453ba02008-11-07 14:05:41 -0800570 struct i2c_msg msgs[] = {
571 {
572 .addr = DDC_ADDR,
573 .flags = 0,
574 .len = 1,
575 .buf = &start,
576 }, {
577 .addr = DDC_ADDR,
578 .flags = I2C_M_RD,
Ma Ling167f3a02009-03-20 14:09:48 +0800579 .len = len,
Dave Airlief453ba02008-11-07 14:05:41 -0800580 .buf = buf,
581 }
582 };
583
Dave Airlief453ba02008-11-07 14:05:41 -0800584 if (i2c_transfer(adapter, msgs, 2) == 2)
Ma Ling167f3a02009-03-20 14:09:48 +0800585 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800586
587 dev_info(&adapter->dev, "unable to read EDID block.\n");
Ma Ling167f3a02009-03-20 14:09:48 +0800588 return -1;
Dave Airlief453ba02008-11-07 14:05:41 -0800589}
590EXPORT_SYMBOL(drm_do_probe_ddc_edid);
591
Ma Ling167f3a02009-03-20 14:09:48 +0800592static int drm_ddc_read_edid(struct drm_connector *connector,
593 struct i2c_adapter *adapter,
594 char *buf, int len)
595{
596 int ret;
597
Keith Packard61f11692009-05-30 20:42:27 -0700598 ret = drm_do_probe_ddc_edid(adapter, buf, len);
Ma Ling167f3a02009-03-20 14:09:48 +0800599 if (ret != 0) {
600 dev_info(&connector->dev->pdev->dev, "%s: no EDID data\n",
601 drm_get_connector_name(connector));
602 goto end;
603 }
604 if (!edid_is_valid((struct edid *)buf)) {
605 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
606 drm_get_connector_name(connector));
607 ret = -1;
608 }
609end:
610 return ret;
611}
612
613#define MAX_EDID_EXT_NUM 4
Dave Airlief453ba02008-11-07 14:05:41 -0800614/**
615 * drm_get_edid - get EDID data, if available
616 * @connector: connector we're probing
617 * @adapter: i2c adapter to use for DDC
618 *
619 * Poke the given connector's i2c channel to grab EDID data if possible.
620 *
621 * Return edid data or NULL if we couldn't find any.
622 */
623struct edid *drm_get_edid(struct drm_connector *connector,
624 struct i2c_adapter *adapter)
625{
Ma Ling167f3a02009-03-20 14:09:48 +0800626 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800627 struct edid *edid;
628
Ma Ling167f3a02009-03-20 14:09:48 +0800629 edid = kmalloc(EDID_LENGTH * (MAX_EDID_EXT_NUM + 1),
630 GFP_KERNEL);
631 if (edid == NULL) {
632 dev_warn(&connector->dev->pdev->dev,
633 "Failed to allocate EDID\n");
634 goto end;
Dave Airlief453ba02008-11-07 14:05:41 -0800635 }
Ma Ling167f3a02009-03-20 14:09:48 +0800636
637 /* Read first EDID block */
638 ret = drm_ddc_read_edid(connector, adapter,
639 (unsigned char *)edid, EDID_LENGTH);
640 if (ret != 0)
641 goto clean_up;
642
643 /* There are EDID extensions to be read */
644 if (edid->extensions != 0) {
645 int edid_ext_num = edid->extensions;
646
647 if (edid_ext_num > MAX_EDID_EXT_NUM) {
648 dev_warn(&connector->dev->pdev->dev,
649 "The number of extension(%d) is "
650 "over max (%d), actually read number (%d)\n",
651 edid_ext_num, MAX_EDID_EXT_NUM,
652 MAX_EDID_EXT_NUM);
653 /* Reset EDID extension number to be read */
654 edid_ext_num = MAX_EDID_EXT_NUM;
655 }
656 /* Read EDID including extensions too */
657 ret = drm_ddc_read_edid(connector, adapter, (char *)edid,
658 EDID_LENGTH * (edid_ext_num + 1));
659 if (ret != 0)
660 goto clean_up;
661
Dave Airlief453ba02008-11-07 14:05:41 -0800662 }
663
664 connector->display_info.raw_edid = (char *)edid;
Ma Ling167f3a02009-03-20 14:09:48 +0800665 goto end;
Dave Airlief453ba02008-11-07 14:05:41 -0800666
Ma Ling167f3a02009-03-20 14:09:48 +0800667clean_up:
668 kfree(edid);
669 edid = NULL;
670end:
Dave Airlief453ba02008-11-07 14:05:41 -0800671 return edid;
Ma Ling167f3a02009-03-20 14:09:48 +0800672
Dave Airlief453ba02008-11-07 14:05:41 -0800673}
674EXPORT_SYMBOL(drm_get_edid);
675
Ma Lingf23c20c2009-03-26 19:26:23 +0800676#define HDMI_IDENTIFIER 0x000C03
677#define VENDOR_BLOCK 0x03
678/**
679 * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
680 * @edid: monitor EDID information
681 *
682 * Parse the CEA extension according to CEA-861-B.
683 * Return true if HDMI, false if not or unknown.
684 */
685bool drm_detect_hdmi_monitor(struct edid *edid)
686{
687 char *edid_ext = NULL;
688 int i, hdmi_id, edid_ext_num;
689 int start_offset, end_offset;
690 bool is_hdmi = false;
691
692 /* No EDID or EDID extensions */
693 if (edid == NULL || edid->extensions == 0)
694 goto end;
695
696 /* Chose real EDID extension number */
697 edid_ext_num = edid->extensions > MAX_EDID_EXT_NUM ?
698 MAX_EDID_EXT_NUM : edid->extensions;
699
700 /* Find CEA extension */
701 for (i = 0; i < edid_ext_num; i++) {
702 edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
703 /* This block is CEA extension */
704 if (edid_ext[0] == 0x02)
705 break;
706 }
707
708 if (i == edid_ext_num)
709 goto end;
710
711 /* Data block offset in CEA extension block */
712 start_offset = 4;
713 end_offset = edid_ext[2];
714
715 /*
716 * Because HDMI identifier is in Vendor Specific Block,
717 * search it from all data blocks of CEA extension.
718 */
719 for (i = start_offset; i < end_offset;
720 /* Increased by data block len */
721 i += ((edid_ext[i] & 0x1f) + 1)) {
722 /* Find vendor specific block */
723 if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
724 hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
725 edid_ext[i + 3] << 16;
726 /* Find HDMI identifier */
727 if (hdmi_id == HDMI_IDENTIFIER)
728 is_hdmi = true;
729 break;
730 }
731 }
732
733end:
734 return is_hdmi;
735}
736EXPORT_SYMBOL(drm_detect_hdmi_monitor);
737
Dave Airlief453ba02008-11-07 14:05:41 -0800738/**
739 * drm_add_edid_modes - add modes from EDID data, if available
740 * @connector: connector we're probing
741 * @edid: edid data
742 *
743 * Add the specified modes to the connector's mode list.
744 *
745 * Return number of modes added or 0 if we couldn't find any.
746 */
747int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
748{
749 int num_modes = 0;
750 u32 quirks;
751
752 if (edid == NULL) {
753 return 0;
754 }
755 if (!edid_is_valid(edid)) {
756 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
757 drm_get_connector_name(connector));
758 return 0;
759 }
760
761 quirks = edid_get_quirks(edid);
762
763 num_modes += add_established_modes(connector, edid);
764 num_modes += add_standard_modes(connector, edid);
765 num_modes += add_detailed_info(connector, edid, quirks);
766
767 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
768 edid_fixup_preferred(connector, quirks);
769
Michel Dänzer0454bea2009-06-15 16:56:07 +0200770 connector->display_info.serration_vsync = (edid->input & DRM_EDID_INPUT_SERRATION_VSYNC) ? 1 : 0;
771 connector->display_info.sync_on_green = (edid->input & DRM_EDID_INPUT_SYNC_ON_GREEN) ? 1 : 0;
772 connector->display_info.composite_sync = (edid->input & DRM_EDID_INPUT_COMPOSITE_SYNC) ? 1 : 0;
773 connector->display_info.separate_syncs = (edid->input & DRM_EDID_INPUT_SEPARATE_SYNCS) ? 1 : 0;
774 connector->display_info.blank_to_black = (edid->input & DRM_EDID_INPUT_BLANK_TO_BLACK) ? 1 : 0;
775 connector->display_info.video_level = (edid->input & DRM_EDID_INPUT_VIDEO_LEVEL) >> 5;
776 connector->display_info.digital = (edid->input & DRM_EDID_INPUT_DIGITAL) ? 1 : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800777 connector->display_info.width_mm = edid->width_cm * 10;
778 connector->display_info.height_mm = edid->height_cm * 10;
779 connector->display_info.gamma = edid->gamma;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200780 connector->display_info.gtf_supported = (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF) ? 1 : 0;
781 connector->display_info.standard_color = (edid->features & DRM_EDID_FEATURE_STANDARD_COLOR) ? 1 : 0;
782 connector->display_info.display_type = (edid->features & DRM_EDID_FEATURE_DISPLAY_TYPE) >> 3;
783 connector->display_info.active_off_supported = (edid->features & DRM_EDID_FEATURE_PM_ACTIVE_OFF) ? 1 : 0;
784 connector->display_info.suspend_supported = (edid->features & DRM_EDID_FEATURE_PM_SUSPEND) ? 1 : 0;
785 connector->display_info.standby_supported = (edid->features & DRM_EDID_FEATURE_PM_STANDBY) ? 1 : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800786 connector->display_info.gamma = edid->gamma;
787
788 return num_modes;
789}
790EXPORT_SYMBOL(drm_add_edid_modes);