blob: e4f1cb5fa60e9260f14eeb373b5a5f6b1fc7ab01 [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)
Zhao Yakui882f0212009-08-26 18:20:49 +080063/* define the number of Extension EDID block */
64#define MAX_EDID_EXT_NUM 4
Dave Airlief453ba02008-11-07 14:05:41 -080065
Zhao Yakui5c612592009-06-22 13:17:10 +080066#define LEVEL_DMT 0
67#define LEVEL_GTF 1
68#define LEVEL_CVT 2
69
Dave Airlief453ba02008-11-07 14:05:41 -080070static struct edid_quirk {
71 char *vendor;
72 int product_id;
73 u32 quirks;
74} edid_quirk_list[] = {
75 /* Acer AL1706 */
76 { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
77 /* Acer F51 */
78 { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
79 /* Unknown Acer */
80 { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
81
82 /* Belinea 10 15 55 */
83 { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
84 { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
85
86 /* Envision Peripherals, Inc. EN-7100e */
87 { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
88
89 /* Funai Electronics PM36B */
90 { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
91 EDID_QUIRK_DETAILED_IN_CM },
92
93 /* LG Philips LCD LP154W01-A5 */
94 { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
95 { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
96
97 /* Philips 107p5 CRT */
98 { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
99
100 /* Proview AY765C */
101 { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
102
103 /* Samsung SyncMaster 205BW. Note: irony */
104 { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
105 /* Samsung SyncMaster 22[5-6]BW */
106 { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
107 { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
108};
109
110
111/* Valid EDID header has these bytes */
112static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
113
114/**
115 * edid_is_valid - sanity check EDID data
116 * @edid: EDID data
117 *
118 * Sanity check the EDID block by looking at the header, the version number
119 * and the checksum. Return 0 if the EDID doesn't check out, or 1 if it's
120 * valid.
121 */
122static bool edid_is_valid(struct edid *edid)
123{
124 int i;
125 u8 csum = 0;
126 u8 *raw_edid = (u8 *)edid;
127
128 if (memcmp(edid->header, edid_header, sizeof(edid_header)))
129 goto bad;
130 if (edid->version != 1) {
131 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
132 goto bad;
133 }
Jesse Barnesb94ee652009-04-02 14:56:24 -0700134 if (edid->revision > 4)
135 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800136
137 for (i = 0; i < EDID_LENGTH; i++)
138 csum += raw_edid[i];
139 if (csum) {
140 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
141 goto bad;
142 }
143
144 return 1;
145
146bad:
147 if (raw_edid) {
148 DRM_ERROR("Raw EDID:\n");
149 print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
150 printk("\n");
151 }
152 return 0;
153}
154
155/**
156 * edid_vendor - match a string against EDID's obfuscated vendor field
157 * @edid: EDID to match
158 * @vendor: vendor string
159 *
160 * Returns true if @vendor is in @edid, false otherwise
161 */
162static bool edid_vendor(struct edid *edid, char *vendor)
163{
164 char edid_vendor[3];
165
166 edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
167 edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
168 ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
Dave Airlie16456c82009-04-03 09:10:33 +1000169 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
Dave Airlief453ba02008-11-07 14:05:41 -0800170
171 return !strncmp(edid_vendor, vendor, 3);
172}
173
174/**
175 * edid_get_quirks - return quirk flags for a given EDID
176 * @edid: EDID to process
177 *
178 * This tells subsequent routines what fixes they need to apply.
179 */
180static u32 edid_get_quirks(struct edid *edid)
181{
182 struct edid_quirk *quirk;
183 int i;
184
185 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
186 quirk = &edid_quirk_list[i];
187
188 if (edid_vendor(edid, quirk->vendor) &&
189 (EDID_PRODUCT_ID(edid) == quirk->product_id))
190 return quirk->quirks;
191 }
192
193 return 0;
194}
195
196#define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
197#define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
198
199
200/**
201 * edid_fixup_preferred - set preferred modes based on quirk list
202 * @connector: has mode list to fix up
203 * @quirks: quirks list
204 *
205 * Walk the mode list for @connector, clearing the preferred status
206 * on existing modes and setting it anew for the right mode ala @quirks.
207 */
208static void edid_fixup_preferred(struct drm_connector *connector,
209 u32 quirks)
210{
211 struct drm_display_mode *t, *cur_mode, *preferred_mode;
Dave Airlief8906072008-12-18 16:59:02 +1000212 int target_refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800213
214 if (list_empty(&connector->probed_modes))
215 return;
216
217 if (quirks & EDID_QUIRK_PREFER_LARGE_60)
218 target_refresh = 60;
219 if (quirks & EDID_QUIRK_PREFER_LARGE_75)
220 target_refresh = 75;
221
222 preferred_mode = list_first_entry(&connector->probed_modes,
223 struct drm_display_mode, head);
224
225 list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
226 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
227
228 if (cur_mode == preferred_mode)
229 continue;
230
231 /* Largest mode is preferred */
232 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
233 preferred_mode = cur_mode;
234
235 /* At a given size, try to get closest to target refresh */
236 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
237 MODE_REFRESH_DIFF(cur_mode, target_refresh) <
238 MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
239 preferred_mode = cur_mode;
240 }
241 }
242
243 preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
244}
245
246/**
247 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
248 * @t: standard timing params
Zhao Yakui5c612592009-06-22 13:17:10 +0800249 * @timing_level: standard timing level
Dave Airlief453ba02008-11-07 14:05:41 -0800250 *
251 * Take the standard timing params (in this case width, aspect, and refresh)
Zhao Yakui5c612592009-06-22 13:17:10 +0800252 * and convert them into a real mode using CVT/GTF/DMT.
Dave Airlief453ba02008-11-07 14:05:41 -0800253 *
254 * Punts for now, but should eventually use the FB layer's CVT based mode
255 * generation code.
256 */
257struct drm_display_mode *drm_mode_std(struct drm_device *dev,
Zhao Yakui5c612592009-06-22 13:17:10 +0800258 struct std_timing *t,
259 int timing_level)
Dave Airlief453ba02008-11-07 14:05:41 -0800260{
261 struct drm_display_mode *mode;
Zhao Yakui5c612592009-06-22 13:17:10 +0800262 int hsize, vsize;
263 int vrefresh_rate;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200264 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
265 >> EDID_TIMING_ASPECT_SHIFT;
Zhao Yakui5c612592009-06-22 13:17:10 +0800266 unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
267 >> EDID_TIMING_VFREQ_SHIFT;
Dave Airlief453ba02008-11-07 14:05:41 -0800268
Zhao Yakui5c612592009-06-22 13:17:10 +0800269 /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
270 hsize = t->hsize * 8 + 248;
271 /* vrefresh_rate = vfreq + 60 */
272 vrefresh_rate = vfreq + 60;
273 /* the vdisplay is calculated based on the aspect ratio */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200274 if (aspect_ratio == 0)
Dave Airlief453ba02008-11-07 14:05:41 -0800275 vsize = (hsize * 10) / 16;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200276 else if (aspect_ratio == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800277 vsize = (hsize * 3) / 4;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200278 else if (aspect_ratio == 2)
Dave Airlief453ba02008-11-07 14:05:41 -0800279 vsize = (hsize * 4) / 5;
280 else
281 vsize = (hsize * 9) / 16;
282
Zhao Yakui5c612592009-06-22 13:17:10 +0800283 mode = NULL;
284 switch (timing_level) {
285 case LEVEL_DMT:
286 mode = drm_mode_create(dev);
287 if (mode) {
288 mode->hdisplay = hsize;
289 mode->vdisplay = vsize;
290 drm_mode_set_name(mode);
291 }
292 break;
293 case LEVEL_GTF:
294 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
295 break;
296 case LEVEL_CVT:
297 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
298 break;
299 }
Dave Airlief453ba02008-11-07 14:05:41 -0800300 return mode;
301}
302
303/**
304 * drm_mode_detailed - create a new mode from an EDID detailed timing section
305 * @dev: DRM device (needed to create new mode)
306 * @edid: EDID block
307 * @timing: EDID detailed timing info
308 * @quirks: quirks to apply
309 *
310 * An EDID detailed timing block contains enough info for us to create and
311 * return a new struct drm_display_mode.
312 */
313static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
314 struct edid *edid,
315 struct detailed_timing *timing,
316 u32 quirks)
317{
318 struct drm_display_mode *mode;
319 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200320 unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
321 unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
322 unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
323 unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200324 unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
325 unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
326 unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
327 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 -0800328
Adam Jacksonfc438962009-06-04 10:20:34 +1000329 /* ignore tiny modes */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200330 if (hactive < 64 || vactive < 64)
Adam Jacksonfc438962009-06-04 10:20:34 +1000331 return NULL;
332
Michel Dänzer0454bea2009-06-15 16:56:07 +0200333 if (pt->misc & DRM_EDID_PT_STEREO) {
Dave Airlief453ba02008-11-07 14:05:41 -0800334 printk(KERN_WARNING "stereo mode not supported\n");
335 return NULL;
336 }
Michel Dänzer0454bea2009-06-15 16:56:07 +0200337 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800338 printk(KERN_WARNING "integrated sync not supported\n");
339 return NULL;
340 }
341
342 mode = drm_mode_create(dev);
343 if (!mode)
344 return NULL;
345
346 mode->type = DRM_MODE_TYPE_DRIVER;
347
348 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
Michel Dänzer0454bea2009-06-15 16:56:07 +0200349 timing->pixel_clock = cpu_to_le16(1088);
Dave Airlief453ba02008-11-07 14:05:41 -0800350
Michel Dänzer0454bea2009-06-15 16:56:07 +0200351 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
Dave Airlief453ba02008-11-07 14:05:41 -0800352
Michel Dänzer0454bea2009-06-15 16:56:07 +0200353 mode->hdisplay = hactive;
354 mode->hsync_start = mode->hdisplay + hsync_offset;
355 mode->hsync_end = mode->hsync_start + hsync_pulse_width;
356 mode->htotal = mode->hdisplay + hblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800357
Michel Dänzer0454bea2009-06-15 16:56:07 +0200358 mode->vdisplay = vactive;
359 mode->vsync_start = mode->vdisplay + vsync_offset;
360 mode->vsync_end = mode->vsync_start + vsync_pulse_width;
361 mode->vtotal = mode->vdisplay + vblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800362
363 drm_mode_set_name(mode);
364
Michel Dänzer0454bea2009-06-15 16:56:07 +0200365 if (pt->misc & DRM_EDID_PT_INTERLACED)
Dave Airlief453ba02008-11-07 14:05:41 -0800366 mode->flags |= DRM_MODE_FLAG_INTERLACE;
367
368 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
Michel Dänzer0454bea2009-06-15 16:56:07 +0200369 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
Dave Airlief453ba02008-11-07 14:05:41 -0800370 }
371
Michel Dänzer0454bea2009-06-15 16:56:07 +0200372 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
373 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
374 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
375 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
Dave Airlief453ba02008-11-07 14:05:41 -0800376
Michel Dänzere14cbee2009-06-23 12:36:32 +0200377 mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
378 mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
Dave Airlief453ba02008-11-07 14:05:41 -0800379
380 if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
381 mode->width_mm *= 10;
382 mode->height_mm *= 10;
383 }
384
385 if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
386 mode->width_mm = edid->width_cm * 10;
387 mode->height_mm = edid->height_cm * 10;
388 }
389
390 return mode;
391}
392
393/*
394 * Detailed mode info for the EDID "established modes" data to use.
395 */
396static struct drm_display_mode edid_est_modes[] = {
397 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
398 968, 1056, 0, 600, 601, 605, 628, 0,
399 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
400 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
401 896, 1024, 0, 600, 601, 603, 625, 0,
402 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
403 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
404 720, 840, 0, 480, 481, 484, 500, 0,
405 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
406 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
407 704, 832, 0, 480, 489, 491, 520, 0,
408 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
409 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
410 768, 864, 0, 480, 483, 486, 525, 0,
411 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
412 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
413 752, 800, 0, 480, 490, 492, 525, 0,
414 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
415 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
416 846, 900, 0, 400, 421, 423, 449, 0,
417 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
418 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
419 846, 900, 0, 400, 412, 414, 449, 0,
420 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
421 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
422 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
423 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
424 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
425 1136, 1312, 0, 768, 769, 772, 800, 0,
426 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
427 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
428 1184, 1328, 0, 768, 771, 777, 806, 0,
429 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
430 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
431 1184, 1344, 0, 768, 771, 777, 806, 0,
432 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
433 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
434 1208, 1264, 0, 768, 768, 776, 817, 0,
435 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
436 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
437 928, 1152, 0, 624, 625, 628, 667, 0,
438 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
439 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
440 896, 1056, 0, 600, 601, 604, 625, 0,
441 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
442 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
443 976, 1040, 0, 600, 637, 643, 666, 0,
444 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
445 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
446 1344, 1600, 0, 864, 865, 868, 900, 0,
447 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
448};
449
450#define EDID_EST_TIMINGS 16
451#define EDID_STD_TIMINGS 8
452#define EDID_DETAILED_TIMINGS 4
453
454/**
455 * add_established_modes - get est. modes from EDID and add them
456 * @edid: EDID block to scan
457 *
458 * Each EDID block contains a bitmap of the supported "established modes" list
459 * (defined above). Tease them out and add them to the global modes list.
460 */
461static int add_established_modes(struct drm_connector *connector, struct edid *edid)
462{
463 struct drm_device *dev = connector->dev;
464 unsigned long est_bits = edid->established_timings.t1 |
465 (edid->established_timings.t2 << 8) |
466 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
467 int i, modes = 0;
468
469 for (i = 0; i <= EDID_EST_TIMINGS; i++)
470 if (est_bits & (1<<i)) {
471 struct drm_display_mode *newmode;
472 newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
473 if (newmode) {
474 drm_mode_probed_add(connector, newmode);
475 modes++;
476 }
477 }
478
479 return modes;
480}
Zhao Yakui5c612592009-06-22 13:17:10 +0800481/**
482 * stanard_timing_level - get std. timing level(CVT/GTF/DMT)
483 * @edid: EDID block to scan
484 */
485static int standard_timing_level(struct edid *edid)
486{
487 if (edid->revision >= 2) {
488 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
489 return LEVEL_CVT;
490 return LEVEL_GTF;
491 }
492 return LEVEL_DMT;
493}
Dave Airlief453ba02008-11-07 14:05:41 -0800494
495/**
496 * add_standard_modes - get std. modes from EDID and add them
497 * @edid: EDID block to scan
498 *
499 * Standard modes can be calculated using the CVT standard. Grab them from
500 * @edid, calculate them, and add them to the list.
501 */
502static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
503{
504 struct drm_device *dev = connector->dev;
505 int i, modes = 0;
Zhao Yakui5c612592009-06-22 13:17:10 +0800506 int timing_level;
507
508 timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800509
510 for (i = 0; i < EDID_STD_TIMINGS; i++) {
511 struct std_timing *t = &edid->standard_timings[i];
512 struct drm_display_mode *newmode;
513
514 /* If std timings bytes are 1, 1 it's empty */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200515 if (t->hsize == 1 && t->vfreq_aspect == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800516 continue;
517
Zhao Yakui5c612592009-06-22 13:17:10 +0800518 newmode = drm_mode_std(dev, &edid->standard_timings[i],
519 timing_level);
Dave Airlief453ba02008-11-07 14:05:41 -0800520 if (newmode) {
521 drm_mode_probed_add(connector, newmode);
522 modes++;
523 }
524 }
525
526 return modes;
527}
528
529/**
530 * add_detailed_modes - get detailed mode info from EDID data
531 * @connector: attached connector
532 * @edid: EDID block to scan
533 * @quirks: quirks to apply
534 *
535 * Some of the detailed timing sections may contain mode information. Grab
536 * it and add it to the list.
537 */
538static int add_detailed_info(struct drm_connector *connector,
539 struct edid *edid, u32 quirks)
540{
541 struct drm_device *dev = connector->dev;
542 int i, j, modes = 0;
Zhao Yakui5c612592009-06-22 13:17:10 +0800543 int timing_level;
544
545 timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800546
547 for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
548 struct detailed_timing *timing = &edid->detailed_timings[i];
549 struct detailed_non_pixel *data = &timing->data.other_data;
550 struct drm_display_mode *newmode;
551
Dave Airlieebb177d2009-08-15 12:25:08 +1000552 /* X server check is version 1.1 or higher */
553 if (edid->version == 1 && edid->revision >= 1 &&
554 !timing->pixel_clock) {
555 /* Other timing or info */
556 switch (data->type) {
557 case EDID_DETAIL_MONITOR_SERIAL:
558 break;
559 case EDID_DETAIL_MONITOR_STRING:
560 break;
561 case EDID_DETAIL_MONITOR_RANGE:
562 /* Get monitor range data */
563 break;
564 case EDID_DETAIL_MONITOR_NAME:
565 break;
566 case EDID_DETAIL_MONITOR_CPDATA:
567 break;
568 case EDID_DETAIL_STD_MODES:
569 /* Five modes per detailed section */
570 for (j = 0; j < 5; i++) {
571 struct std_timing *std;
572 struct drm_display_mode *newmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800573
Dave Airlieebb177d2009-08-15 12:25:08 +1000574 std = &data->data.timings[j];
Dave Airlie51c8b402009-08-20 13:38:04 +1000575 newmode = drm_mode_std(dev, std,
576 timing_level);
Dave Airlieebb177d2009-08-15 12:25:08 +1000577 if (newmode) {
578 drm_mode_probed_add(connector, newmode);
579 modes++;
580 }
581 }
582 break;
583 default:
584 break;
585 }
586 } else {
Dave Airlief453ba02008-11-07 14:05:41 -0800587 newmode = drm_mode_detailed(dev, edid, timing, quirks);
588 if (!newmode)
589 continue;
590
591 /* First detailed mode is preferred */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200592 if (i == 0 && (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING))
Dave Airlief453ba02008-11-07 14:05:41 -0800593 newmode->type |= DRM_MODE_TYPE_PREFERRED;
594 drm_mode_probed_add(connector, newmode);
595
596 modes++;
Dave Airlief453ba02008-11-07 14:05:41 -0800597 }
598 }
599
600 return modes;
601}
Zhao Yakui882f0212009-08-26 18:20:49 +0800602/**
603 * add_detailed_mode_eedid - get detailed mode info from addtional timing
604 * EDID block
605 * @connector: attached connector
606 * @edid: EDID block to scan(It is only to get addtional timing EDID block)
607 * @quirks: quirks to apply
608 *
609 * Some of the detailed timing sections may contain mode information. Grab
610 * it and add it to the list.
611 */
612static int add_detailed_info_eedid(struct drm_connector *connector,
613 struct edid *edid, u32 quirks)
614{
615 struct drm_device *dev = connector->dev;
616 int i, j, modes = 0;
617 char *edid_ext = NULL;
618 struct detailed_timing *timing;
619 struct detailed_non_pixel *data;
620 struct drm_display_mode *newmode;
621 int edid_ext_num;
622 int start_offset, end_offset;
623 int timing_level;
624
625 if (edid->version == 1 && edid->revision < 3) {
626 /* If the EDID version is less than 1.3, there is no
627 * extension EDID.
628 */
629 return 0;
630 }
631 if (!edid->extensions) {
632 /* if there is no extension EDID, it is unnecessary to
633 * parse the E-EDID to get detailed info
634 */
635 return 0;
636 }
637
638 /* Chose real EDID extension number */
639 edid_ext_num = edid->extensions > MAX_EDID_EXT_NUM ?
640 MAX_EDID_EXT_NUM : edid->extensions;
641
642 /* Find CEA extension */
643 for (i = 0; i < edid_ext_num; i++) {
644 edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
645 /* This block is CEA extension */
646 if (edid_ext[0] == 0x02)
647 break;
648 }
649
650 if (i == edid_ext_num) {
651 /* if there is no additional timing EDID block, return */
652 return 0;
653 }
654
655 /* Get the start offset of detailed timing block */
656 start_offset = edid_ext[2];
657 if (start_offset == 0) {
658 /* If the start_offset is zero, it means that neither detailed
659 * info nor data block exist. In such case it is also
660 * unnecessary to parse the detailed timing info.
661 */
662 return 0;
663 }
664
665 timing_level = standard_timing_level(edid);
666 end_offset = EDID_LENGTH;
667 end_offset -= sizeof(struct detailed_timing);
668 for (i = start_offset; i < end_offset;
669 i += sizeof(struct detailed_timing)) {
670 timing = (struct detailed_timing *)(edid_ext + i);
671 data = &timing->data.other_data;
672 /* Detailed mode timing */
673 if (timing->pixel_clock) {
674 newmode = drm_mode_detailed(dev, edid, timing, quirks);
675 if (!newmode)
676 continue;
677
678 drm_mode_probed_add(connector, newmode);
679
680 modes++;
681 continue;
682 }
683
684 /* Other timing or info */
685 switch (data->type) {
686 case EDID_DETAIL_MONITOR_SERIAL:
687 break;
688 case EDID_DETAIL_MONITOR_STRING:
689 break;
690 case EDID_DETAIL_MONITOR_RANGE:
691 /* Get monitor range data */
692 break;
693 case EDID_DETAIL_MONITOR_NAME:
694 break;
695 case EDID_DETAIL_MONITOR_CPDATA:
696 break;
697 case EDID_DETAIL_STD_MODES:
698 /* Five modes per detailed section */
699 for (j = 0; j < 5; i++) {
700 struct std_timing *std;
701 struct drm_display_mode *newmode;
702
703 std = &data->data.timings[j];
704 newmode = drm_mode_std(dev, std, timing_level);
705 if (newmode) {
706 drm_mode_probed_add(connector, newmode);
707 modes++;
708 }
709 }
710 break;
711 default:
712 break;
713 }
714 }
715
716 return modes;
717}
Dave Airlief453ba02008-11-07 14:05:41 -0800718
719#define DDC_ADDR 0x50
Ma Ling167f3a02009-03-20 14:09:48 +0800720/**
721 * Get EDID information via I2C.
722 *
723 * \param adapter : i2c device adaptor
724 * \param buf : EDID data buffer to be filled
725 * \param len : EDID data buffer length
726 * \return 0 on success or -1 on failure.
727 *
728 * Try to fetch EDID information by calling i2c driver function.
729 */
730int drm_do_probe_ddc_edid(struct i2c_adapter *adapter,
731 unsigned char *buf, int len)
Dave Airlief453ba02008-11-07 14:05:41 -0800732{
733 unsigned char start = 0x0;
Dave Airlief453ba02008-11-07 14:05:41 -0800734 struct i2c_msg msgs[] = {
735 {
736 .addr = DDC_ADDR,
737 .flags = 0,
738 .len = 1,
739 .buf = &start,
740 }, {
741 .addr = DDC_ADDR,
742 .flags = I2C_M_RD,
Ma Ling167f3a02009-03-20 14:09:48 +0800743 .len = len,
Dave Airlief453ba02008-11-07 14:05:41 -0800744 .buf = buf,
745 }
746 };
747
Dave Airlief453ba02008-11-07 14:05:41 -0800748 if (i2c_transfer(adapter, msgs, 2) == 2)
Ma Ling167f3a02009-03-20 14:09:48 +0800749 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800750
751 dev_info(&adapter->dev, "unable to read EDID block.\n");
Ma Ling167f3a02009-03-20 14:09:48 +0800752 return -1;
Dave Airlief453ba02008-11-07 14:05:41 -0800753}
754EXPORT_SYMBOL(drm_do_probe_ddc_edid);
755
Ma Ling167f3a02009-03-20 14:09:48 +0800756static int drm_ddc_read_edid(struct drm_connector *connector,
757 struct i2c_adapter *adapter,
758 char *buf, int len)
759{
760 int ret;
761
Keith Packard61f11692009-05-30 20:42:27 -0700762 ret = drm_do_probe_ddc_edid(adapter, buf, len);
Ma Ling167f3a02009-03-20 14:09:48 +0800763 if (ret != 0) {
764 dev_info(&connector->dev->pdev->dev, "%s: no EDID data\n",
765 drm_get_connector_name(connector));
766 goto end;
767 }
768 if (!edid_is_valid((struct edid *)buf)) {
769 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
770 drm_get_connector_name(connector));
771 ret = -1;
772 }
773end:
774 return ret;
775}
776
Dave Airlief453ba02008-11-07 14:05:41 -0800777/**
778 * drm_get_edid - get EDID data, if available
779 * @connector: connector we're probing
780 * @adapter: i2c adapter to use for DDC
781 *
782 * Poke the given connector's i2c channel to grab EDID data if possible.
783 *
784 * Return edid data or NULL if we couldn't find any.
785 */
786struct edid *drm_get_edid(struct drm_connector *connector,
787 struct i2c_adapter *adapter)
788{
Ma Ling167f3a02009-03-20 14:09:48 +0800789 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800790 struct edid *edid;
791
Ma Ling167f3a02009-03-20 14:09:48 +0800792 edid = kmalloc(EDID_LENGTH * (MAX_EDID_EXT_NUM + 1),
793 GFP_KERNEL);
794 if (edid == NULL) {
795 dev_warn(&connector->dev->pdev->dev,
796 "Failed to allocate EDID\n");
797 goto end;
Dave Airlief453ba02008-11-07 14:05:41 -0800798 }
Ma Ling167f3a02009-03-20 14:09:48 +0800799
800 /* Read first EDID block */
801 ret = drm_ddc_read_edid(connector, adapter,
802 (unsigned char *)edid, EDID_LENGTH);
803 if (ret != 0)
804 goto clean_up;
805
806 /* There are EDID extensions to be read */
807 if (edid->extensions != 0) {
808 int edid_ext_num = edid->extensions;
809
810 if (edid_ext_num > MAX_EDID_EXT_NUM) {
811 dev_warn(&connector->dev->pdev->dev,
812 "The number of extension(%d) is "
813 "over max (%d), actually read number (%d)\n",
814 edid_ext_num, MAX_EDID_EXT_NUM,
815 MAX_EDID_EXT_NUM);
816 /* Reset EDID extension number to be read */
817 edid_ext_num = MAX_EDID_EXT_NUM;
818 }
819 /* Read EDID including extensions too */
820 ret = drm_ddc_read_edid(connector, adapter, (char *)edid,
821 EDID_LENGTH * (edid_ext_num + 1));
822 if (ret != 0)
823 goto clean_up;
824
Dave Airlief453ba02008-11-07 14:05:41 -0800825 }
826
827 connector->display_info.raw_edid = (char *)edid;
Ma Ling167f3a02009-03-20 14:09:48 +0800828 goto end;
Dave Airlief453ba02008-11-07 14:05:41 -0800829
Ma Ling167f3a02009-03-20 14:09:48 +0800830clean_up:
831 kfree(edid);
832 edid = NULL;
833end:
Dave Airlief453ba02008-11-07 14:05:41 -0800834 return edid;
Ma Ling167f3a02009-03-20 14:09:48 +0800835
Dave Airlief453ba02008-11-07 14:05:41 -0800836}
837EXPORT_SYMBOL(drm_get_edid);
838
Ma Lingf23c20c2009-03-26 19:26:23 +0800839#define HDMI_IDENTIFIER 0x000C03
840#define VENDOR_BLOCK 0x03
841/**
842 * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
843 * @edid: monitor EDID information
844 *
845 * Parse the CEA extension according to CEA-861-B.
846 * Return true if HDMI, false if not or unknown.
847 */
848bool drm_detect_hdmi_monitor(struct edid *edid)
849{
850 char *edid_ext = NULL;
851 int i, hdmi_id, edid_ext_num;
852 int start_offset, end_offset;
853 bool is_hdmi = false;
854
855 /* No EDID or EDID extensions */
856 if (edid == NULL || edid->extensions == 0)
857 goto end;
858
859 /* Chose real EDID extension number */
860 edid_ext_num = edid->extensions > MAX_EDID_EXT_NUM ?
861 MAX_EDID_EXT_NUM : edid->extensions;
862
863 /* Find CEA extension */
864 for (i = 0; i < edid_ext_num; i++) {
865 edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
866 /* This block is CEA extension */
867 if (edid_ext[0] == 0x02)
868 break;
869 }
870
871 if (i == edid_ext_num)
872 goto end;
873
874 /* Data block offset in CEA extension block */
875 start_offset = 4;
876 end_offset = edid_ext[2];
877
878 /*
879 * Because HDMI identifier is in Vendor Specific Block,
880 * search it from all data blocks of CEA extension.
881 */
882 for (i = start_offset; i < end_offset;
883 /* Increased by data block len */
884 i += ((edid_ext[i] & 0x1f) + 1)) {
885 /* Find vendor specific block */
886 if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
887 hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
888 edid_ext[i + 3] << 16;
889 /* Find HDMI identifier */
890 if (hdmi_id == HDMI_IDENTIFIER)
891 is_hdmi = true;
892 break;
893 }
894 }
895
896end:
897 return is_hdmi;
898}
899EXPORT_SYMBOL(drm_detect_hdmi_monitor);
900
Dave Airlief453ba02008-11-07 14:05:41 -0800901/**
902 * drm_add_edid_modes - add modes from EDID data, if available
903 * @connector: connector we're probing
904 * @edid: edid data
905 *
906 * Add the specified modes to the connector's mode list.
907 *
908 * Return number of modes added or 0 if we couldn't find any.
909 */
910int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
911{
912 int num_modes = 0;
913 u32 quirks;
914
915 if (edid == NULL) {
916 return 0;
917 }
918 if (!edid_is_valid(edid)) {
919 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
920 drm_get_connector_name(connector));
921 return 0;
922 }
923
924 quirks = edid_get_quirks(edid);
925
926 num_modes += add_established_modes(connector, edid);
927 num_modes += add_standard_modes(connector, edid);
928 num_modes += add_detailed_info(connector, edid, quirks);
Zhao Yakui882f0212009-08-26 18:20:49 +0800929 num_modes += add_detailed_info_eedid(connector, edid, quirks);
Dave Airlief453ba02008-11-07 14:05:41 -0800930
931 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
932 edid_fixup_preferred(connector, quirks);
933
Michel Dänzer0454bea2009-06-15 16:56:07 +0200934 connector->display_info.serration_vsync = (edid->input & DRM_EDID_INPUT_SERRATION_VSYNC) ? 1 : 0;
935 connector->display_info.sync_on_green = (edid->input & DRM_EDID_INPUT_SYNC_ON_GREEN) ? 1 : 0;
936 connector->display_info.composite_sync = (edid->input & DRM_EDID_INPUT_COMPOSITE_SYNC) ? 1 : 0;
937 connector->display_info.separate_syncs = (edid->input & DRM_EDID_INPUT_SEPARATE_SYNCS) ? 1 : 0;
938 connector->display_info.blank_to_black = (edid->input & DRM_EDID_INPUT_BLANK_TO_BLACK) ? 1 : 0;
939 connector->display_info.video_level = (edid->input & DRM_EDID_INPUT_VIDEO_LEVEL) >> 5;
940 connector->display_info.digital = (edid->input & DRM_EDID_INPUT_DIGITAL) ? 1 : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800941 connector->display_info.width_mm = edid->width_cm * 10;
942 connector->display_info.height_mm = edid->height_cm * 10;
943 connector->display_info.gamma = edid->gamma;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200944 connector->display_info.gtf_supported = (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF) ? 1 : 0;
945 connector->display_info.standard_color = (edid->features & DRM_EDID_FEATURE_STANDARD_COLOR) ? 1 : 0;
946 connector->display_info.display_type = (edid->features & DRM_EDID_FEATURE_DISPLAY_TYPE) >> 3;
947 connector->display_info.active_off_supported = (edid->features & DRM_EDID_FEATURE_PM_ACTIVE_OFF) ? 1 : 0;
948 connector->display_info.suspend_supported = (edid->features & DRM_EDID_FEATURE_PM_SUSPEND) ? 1 : 0;
949 connector->display_info.standby_supported = (edid->features & DRM_EDID_FEATURE_PM_STANDBY) ? 1 : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800950 connector->display_info.gamma = edid->gamma;
951
952 return num_modes;
953}
954EXPORT_SYMBOL(drm_add_edid_modes);