blob: bbcb2e22675b127ce64454e69ba7a9abe55b6cc9 [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
Zhao Yakui5c612592009-06-22 13:17:10 +080064#define LEVEL_DMT 0
65#define LEVEL_GTF 1
66#define LEVEL_CVT 2
67
Dave Airlief453ba02008-11-07 14:05:41 -080068static struct edid_quirk {
69 char *vendor;
70 int product_id;
71 u32 quirks;
72} edid_quirk_list[] = {
73 /* Acer AL1706 */
74 { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
75 /* Acer F51 */
76 { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
77 /* Unknown Acer */
78 { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
79
80 /* Belinea 10 15 55 */
81 { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
82 { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
83
84 /* Envision Peripherals, Inc. EN-7100e */
85 { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
86
87 /* Funai Electronics PM36B */
88 { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
89 EDID_QUIRK_DETAILED_IN_CM },
90
91 /* LG Philips LCD LP154W01-A5 */
92 { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
93 { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
94
95 /* Philips 107p5 CRT */
96 { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
97
98 /* Proview AY765C */
99 { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
100
101 /* Samsung SyncMaster 205BW. Note: irony */
102 { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
103 /* Samsung SyncMaster 22[5-6]BW */
104 { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
105 { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
106};
107
108
109/* Valid EDID header has these bytes */
110static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
111
112/**
113 * edid_is_valid - sanity check EDID data
114 * @edid: EDID data
115 *
116 * Sanity check the EDID block by looking at the header, the version number
117 * and the checksum. Return 0 if the EDID doesn't check out, or 1 if it's
118 * valid.
119 */
120static bool edid_is_valid(struct edid *edid)
121{
122 int i;
123 u8 csum = 0;
124 u8 *raw_edid = (u8 *)edid;
125
126 if (memcmp(edid->header, edid_header, sizeof(edid_header)))
127 goto bad;
128 if (edid->version != 1) {
129 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
130 goto bad;
131 }
Jesse Barnesb94ee652009-04-02 14:56:24 -0700132 if (edid->revision > 4)
133 DRM_DEBUG("EDID minor > 4, assuming backward compatibility\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800134
135 for (i = 0; i < EDID_LENGTH; i++)
136 csum += raw_edid[i];
137 if (csum) {
138 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
139 goto bad;
140 }
141
142 return 1;
143
144bad:
145 if (raw_edid) {
146 DRM_ERROR("Raw EDID:\n");
147 print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
148 printk("\n");
149 }
150 return 0;
151}
152
153/**
154 * edid_vendor - match a string against EDID's obfuscated vendor field
155 * @edid: EDID to match
156 * @vendor: vendor string
157 *
158 * Returns true if @vendor is in @edid, false otherwise
159 */
160static bool edid_vendor(struct edid *edid, char *vendor)
161{
162 char edid_vendor[3];
163
164 edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
165 edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
166 ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
Dave Airlie16456c82009-04-03 09:10:33 +1000167 edid_vendor[2] = (edid->mfg_id[1] & 0x1f) + '@';
Dave Airlief453ba02008-11-07 14:05:41 -0800168
169 return !strncmp(edid_vendor, vendor, 3);
170}
171
172/**
173 * edid_get_quirks - return quirk flags for a given EDID
174 * @edid: EDID to process
175 *
176 * This tells subsequent routines what fixes they need to apply.
177 */
178static u32 edid_get_quirks(struct edid *edid)
179{
180 struct edid_quirk *quirk;
181 int i;
182
183 for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
184 quirk = &edid_quirk_list[i];
185
186 if (edid_vendor(edid, quirk->vendor) &&
187 (EDID_PRODUCT_ID(edid) == quirk->product_id))
188 return quirk->quirks;
189 }
190
191 return 0;
192}
193
194#define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
195#define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
196
197
198/**
199 * edid_fixup_preferred - set preferred modes based on quirk list
200 * @connector: has mode list to fix up
201 * @quirks: quirks list
202 *
203 * Walk the mode list for @connector, clearing the preferred status
204 * on existing modes and setting it anew for the right mode ala @quirks.
205 */
206static void edid_fixup_preferred(struct drm_connector *connector,
207 u32 quirks)
208{
209 struct drm_display_mode *t, *cur_mode, *preferred_mode;
Dave Airlief8906072008-12-18 16:59:02 +1000210 int target_refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800211
212 if (list_empty(&connector->probed_modes))
213 return;
214
215 if (quirks & EDID_QUIRK_PREFER_LARGE_60)
216 target_refresh = 60;
217 if (quirks & EDID_QUIRK_PREFER_LARGE_75)
218 target_refresh = 75;
219
220 preferred_mode = list_first_entry(&connector->probed_modes,
221 struct drm_display_mode, head);
222
223 list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
224 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
225
226 if (cur_mode == preferred_mode)
227 continue;
228
229 /* Largest mode is preferred */
230 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
231 preferred_mode = cur_mode;
232
233 /* At a given size, try to get closest to target refresh */
234 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
235 MODE_REFRESH_DIFF(cur_mode, target_refresh) <
236 MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
237 preferred_mode = cur_mode;
238 }
239 }
240
241 preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
242}
243
244/**
245 * drm_mode_std - convert standard mode info (width, height, refresh) into mode
246 * @t: standard timing params
Zhao Yakui5c612592009-06-22 13:17:10 +0800247 * @timing_level: standard timing level
Dave Airlief453ba02008-11-07 14:05:41 -0800248 *
249 * Take the standard timing params (in this case width, aspect, and refresh)
Zhao Yakui5c612592009-06-22 13:17:10 +0800250 * and convert them into a real mode using CVT/GTF/DMT.
Dave Airlief453ba02008-11-07 14:05:41 -0800251 *
252 * Punts for now, but should eventually use the FB layer's CVT based mode
253 * generation code.
254 */
255struct drm_display_mode *drm_mode_std(struct drm_device *dev,
Zhao Yakui5c612592009-06-22 13:17:10 +0800256 struct std_timing *t,
257 int timing_level)
Dave Airlief453ba02008-11-07 14:05:41 -0800258{
259 struct drm_display_mode *mode;
Zhao Yakui5c612592009-06-22 13:17:10 +0800260 int hsize, vsize;
261 int vrefresh_rate;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200262 unsigned aspect_ratio = (t->vfreq_aspect & EDID_TIMING_ASPECT_MASK)
263 >> EDID_TIMING_ASPECT_SHIFT;
Zhao Yakui5c612592009-06-22 13:17:10 +0800264 unsigned vfreq = (t->vfreq_aspect & EDID_TIMING_VFREQ_MASK)
265 >> EDID_TIMING_VFREQ_SHIFT;
Dave Airlief453ba02008-11-07 14:05:41 -0800266
Zhao Yakui5c612592009-06-22 13:17:10 +0800267 /* According to the EDID spec, the hdisplay = hsize * 8 + 248 */
268 hsize = t->hsize * 8 + 248;
269 /* vrefresh_rate = vfreq + 60 */
270 vrefresh_rate = vfreq + 60;
271 /* the vdisplay is calculated based on the aspect ratio */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200272 if (aspect_ratio == 0)
Dave Airlief453ba02008-11-07 14:05:41 -0800273 vsize = (hsize * 10) / 16;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200274 else if (aspect_ratio == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800275 vsize = (hsize * 3) / 4;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200276 else if (aspect_ratio == 2)
Dave Airlief453ba02008-11-07 14:05:41 -0800277 vsize = (hsize * 4) / 5;
278 else
279 vsize = (hsize * 9) / 16;
280
Zhao Yakui5c612592009-06-22 13:17:10 +0800281 mode = NULL;
282 switch (timing_level) {
283 case LEVEL_DMT:
284 mode = drm_mode_create(dev);
285 if (mode) {
286 mode->hdisplay = hsize;
287 mode->vdisplay = vsize;
288 drm_mode_set_name(mode);
289 }
290 break;
291 case LEVEL_GTF:
292 mode = drm_gtf_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
293 break;
294 case LEVEL_CVT:
295 mode = drm_cvt_mode(dev, hsize, vsize, vrefresh_rate, 0, 0);
296 break;
297 }
Dave Airlief453ba02008-11-07 14:05:41 -0800298 return mode;
299}
300
301/**
302 * drm_mode_detailed - create a new mode from an EDID detailed timing section
303 * @dev: DRM device (needed to create new mode)
304 * @edid: EDID block
305 * @timing: EDID detailed timing info
306 * @quirks: quirks to apply
307 *
308 * An EDID detailed timing block contains enough info for us to create and
309 * return a new struct drm_display_mode.
310 */
311static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
312 struct edid *edid,
313 struct detailed_timing *timing,
314 u32 quirks)
315{
316 struct drm_display_mode *mode;
317 struct detailed_pixel_timing *pt = &timing->data.pixel_data;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200318 unsigned hactive = (pt->hactive_hblank_hi & 0xf0) << 4 | pt->hactive_lo;
319 unsigned vactive = (pt->vactive_vblank_hi & 0xf0) << 4 | pt->vactive_lo;
320 unsigned hblank = (pt->hactive_hblank_hi & 0xf) << 8 | pt->hblank_lo;
321 unsigned vblank = (pt->vactive_vblank_hi & 0xf) << 8 | pt->vblank_lo;
Michel Dänzere14cbee2009-06-23 12:36:32 +0200322 unsigned hsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc0) << 2 | pt->hsync_offset_lo;
323 unsigned hsync_pulse_width = (pt->hsync_vsync_offset_pulse_width_hi & 0x30) << 4 | pt->hsync_pulse_width_lo;
324 unsigned vsync_offset = (pt->hsync_vsync_offset_pulse_width_hi & 0xc) >> 2 | pt->vsync_offset_pulse_width_lo >> 4;
325 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 -0800326
Adam Jacksonfc438962009-06-04 10:20:34 +1000327 /* ignore tiny modes */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200328 if (hactive < 64 || vactive < 64)
Adam Jacksonfc438962009-06-04 10:20:34 +1000329 return NULL;
330
Michel Dänzer0454bea2009-06-15 16:56:07 +0200331 if (pt->misc & DRM_EDID_PT_STEREO) {
Dave Airlief453ba02008-11-07 14:05:41 -0800332 printk(KERN_WARNING "stereo mode not supported\n");
333 return NULL;
334 }
Michel Dänzer0454bea2009-06-15 16:56:07 +0200335 if (!(pt->misc & DRM_EDID_PT_SEPARATE_SYNC)) {
Dave Airlief453ba02008-11-07 14:05:41 -0800336 printk(KERN_WARNING "integrated sync not supported\n");
337 return NULL;
338 }
339
340 mode = drm_mode_create(dev);
341 if (!mode)
342 return NULL;
343
344 mode->type = DRM_MODE_TYPE_DRIVER;
345
346 if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
Michel Dänzer0454bea2009-06-15 16:56:07 +0200347 timing->pixel_clock = cpu_to_le16(1088);
Dave Airlief453ba02008-11-07 14:05:41 -0800348
Michel Dänzer0454bea2009-06-15 16:56:07 +0200349 mode->clock = le16_to_cpu(timing->pixel_clock) * 10;
Dave Airlief453ba02008-11-07 14:05:41 -0800350
Michel Dänzer0454bea2009-06-15 16:56:07 +0200351 mode->hdisplay = hactive;
352 mode->hsync_start = mode->hdisplay + hsync_offset;
353 mode->hsync_end = mode->hsync_start + hsync_pulse_width;
354 mode->htotal = mode->hdisplay + hblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800355
Michel Dänzer0454bea2009-06-15 16:56:07 +0200356 mode->vdisplay = vactive;
357 mode->vsync_start = mode->vdisplay + vsync_offset;
358 mode->vsync_end = mode->vsync_start + vsync_pulse_width;
359 mode->vtotal = mode->vdisplay + vblank;
Dave Airlief453ba02008-11-07 14:05:41 -0800360
361 drm_mode_set_name(mode);
362
Michel Dänzer0454bea2009-06-15 16:56:07 +0200363 if (pt->misc & DRM_EDID_PT_INTERLACED)
Dave Airlief453ba02008-11-07 14:05:41 -0800364 mode->flags |= DRM_MODE_FLAG_INTERLACE;
365
366 if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
Michel Dänzer0454bea2009-06-15 16:56:07 +0200367 pt->misc |= DRM_EDID_PT_HSYNC_POSITIVE | DRM_EDID_PT_VSYNC_POSITIVE;
Dave Airlief453ba02008-11-07 14:05:41 -0800368 }
369
Michel Dänzer0454bea2009-06-15 16:56:07 +0200370 mode->flags |= (pt->misc & DRM_EDID_PT_HSYNC_POSITIVE) ?
371 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
372 mode->flags |= (pt->misc & DRM_EDID_PT_VSYNC_POSITIVE) ?
373 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
Dave Airlief453ba02008-11-07 14:05:41 -0800374
Michel Dänzere14cbee2009-06-23 12:36:32 +0200375 mode->width_mm = pt->width_mm_lo | (pt->width_height_mm_hi & 0xf0) << 4;
376 mode->height_mm = pt->height_mm_lo | (pt->width_height_mm_hi & 0xf) << 8;
Dave Airlief453ba02008-11-07 14:05:41 -0800377
378 if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
379 mode->width_mm *= 10;
380 mode->height_mm *= 10;
381 }
382
383 if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
384 mode->width_mm = edid->width_cm * 10;
385 mode->height_mm = edid->height_cm * 10;
386 }
387
388 return mode;
389}
390
391/*
392 * Detailed mode info for the EDID "established modes" data to use.
393 */
394static struct drm_display_mode edid_est_modes[] = {
395 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
396 968, 1056, 0, 600, 601, 605, 628, 0,
397 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@60Hz */
398 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
399 896, 1024, 0, 600, 601, 603, 625, 0,
400 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@56Hz */
401 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
402 720, 840, 0, 480, 481, 484, 500, 0,
403 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@75Hz */
404 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
405 704, 832, 0, 480, 489, 491, 520, 0,
406 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@72Hz */
407 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
408 768, 864, 0, 480, 483, 486, 525, 0,
409 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@67Hz */
410 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
411 752, 800, 0, 480, 490, 492, 525, 0,
412 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 640x480@60Hz */
413 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
414 846, 900, 0, 400, 421, 423, 449, 0,
415 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 720x400@88Hz */
416 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
417 846, 900, 0, 400, 412, 414, 449, 0,
418 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 720x400@70Hz */
419 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
420 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
421 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1280x1024@75Hz */
422 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
423 1136, 1312, 0, 768, 769, 772, 800, 0,
424 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1024x768@75Hz */
425 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
426 1184, 1328, 0, 768, 771, 777, 806, 0,
427 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@70Hz */
428 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
429 1184, 1344, 0, 768, 771, 777, 806, 0,
430 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 1024x768@60Hz */
431 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
432 1208, 1264, 0, 768, 768, 776, 817, 0,
433 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_INTERLACE) }, /* 1024x768@43Hz */
434 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
435 928, 1152, 0, 624, 625, 628, 667, 0,
436 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, /* 832x624@75Hz */
437 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
438 896, 1056, 0, 600, 601, 604, 625, 0,
439 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@75Hz */
440 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
441 976, 1040, 0, 600, 637, 643, 666, 0,
442 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 800x600@72Hz */
443 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
444 1344, 1600, 0, 864, 865, 868, 900, 0,
445 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, /* 1152x864@75Hz */
446};
447
448#define EDID_EST_TIMINGS 16
449#define EDID_STD_TIMINGS 8
450#define EDID_DETAILED_TIMINGS 4
451
452/**
453 * add_established_modes - get est. modes from EDID and add them
454 * @edid: EDID block to scan
455 *
456 * Each EDID block contains a bitmap of the supported "established modes" list
457 * (defined above). Tease them out and add them to the global modes list.
458 */
459static int add_established_modes(struct drm_connector *connector, struct edid *edid)
460{
461 struct drm_device *dev = connector->dev;
462 unsigned long est_bits = edid->established_timings.t1 |
463 (edid->established_timings.t2 << 8) |
464 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
465 int i, modes = 0;
466
467 for (i = 0; i <= EDID_EST_TIMINGS; i++)
468 if (est_bits & (1<<i)) {
469 struct drm_display_mode *newmode;
470 newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
471 if (newmode) {
472 drm_mode_probed_add(connector, newmode);
473 modes++;
474 }
475 }
476
477 return modes;
478}
Zhao Yakui5c612592009-06-22 13:17:10 +0800479/**
480 * stanard_timing_level - get std. timing level(CVT/GTF/DMT)
481 * @edid: EDID block to scan
482 */
483static int standard_timing_level(struct edid *edid)
484{
485 if (edid->revision >= 2) {
486 if (edid->revision >= 4 && (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF))
487 return LEVEL_CVT;
488 return LEVEL_GTF;
489 }
490 return LEVEL_DMT;
491}
Dave Airlief453ba02008-11-07 14:05:41 -0800492
493/**
494 * add_standard_modes - get std. modes from EDID and add them
495 * @edid: EDID block to scan
496 *
497 * Standard modes can be calculated using the CVT standard. Grab them from
498 * @edid, calculate them, and add them to the list.
499 */
500static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
501{
502 struct drm_device *dev = connector->dev;
503 int i, modes = 0;
Zhao Yakui5c612592009-06-22 13:17:10 +0800504 int timing_level;
505
506 timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800507
508 for (i = 0; i < EDID_STD_TIMINGS; i++) {
509 struct std_timing *t = &edid->standard_timings[i];
510 struct drm_display_mode *newmode;
511
512 /* If std timings bytes are 1, 1 it's empty */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200513 if (t->hsize == 1 && t->vfreq_aspect == 1)
Dave Airlief453ba02008-11-07 14:05:41 -0800514 continue;
515
Zhao Yakui5c612592009-06-22 13:17:10 +0800516 newmode = drm_mode_std(dev, &edid->standard_timings[i],
517 timing_level);
Dave Airlief453ba02008-11-07 14:05:41 -0800518 if (newmode) {
519 drm_mode_probed_add(connector, newmode);
520 modes++;
521 }
522 }
523
524 return modes;
525}
526
527/**
528 * add_detailed_modes - get detailed mode info from EDID data
529 * @connector: attached connector
530 * @edid: EDID block to scan
531 * @quirks: quirks to apply
532 *
533 * Some of the detailed timing sections may contain mode information. Grab
534 * it and add it to the list.
535 */
536static int add_detailed_info(struct drm_connector *connector,
537 struct edid *edid, u32 quirks)
538{
539 struct drm_device *dev = connector->dev;
540 int i, j, modes = 0;
Zhao Yakui5c612592009-06-22 13:17:10 +0800541 int timing_level;
542
543 timing_level = standard_timing_level(edid);
Dave Airlief453ba02008-11-07 14:05:41 -0800544
545 for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
546 struct detailed_timing *timing = &edid->detailed_timings[i];
547 struct detailed_non_pixel *data = &timing->data.other_data;
548 struct drm_display_mode *newmode;
549
550 /* EDID up to and including 1.2 may put monitor info here */
551 if (edid->version == 1 && edid->revision < 3)
552 continue;
553
554 /* Detailed mode timing */
555 if (timing->pixel_clock) {
556 newmode = drm_mode_detailed(dev, edid, timing, quirks);
557 if (!newmode)
558 continue;
559
560 /* First detailed mode is preferred */
Michel Dänzer0454bea2009-06-15 16:56:07 +0200561 if (i == 0 && (edid->features & DRM_EDID_FEATURE_PREFERRED_TIMING))
Dave Airlief453ba02008-11-07 14:05:41 -0800562 newmode->type |= DRM_MODE_TYPE_PREFERRED;
563 drm_mode_probed_add(connector, newmode);
564
565 modes++;
566 continue;
567 }
568
569 /* Other timing or info */
570 switch (data->type) {
571 case EDID_DETAIL_MONITOR_SERIAL:
572 break;
573 case EDID_DETAIL_MONITOR_STRING:
574 break;
575 case EDID_DETAIL_MONITOR_RANGE:
576 /* Get monitor range data */
577 break;
578 case EDID_DETAIL_MONITOR_NAME:
579 break;
580 case EDID_DETAIL_MONITOR_CPDATA:
581 break;
582 case EDID_DETAIL_STD_MODES:
583 /* Five modes per detailed section */
584 for (j = 0; j < 5; i++) {
585 struct std_timing *std;
586 struct drm_display_mode *newmode;
587
588 std = &data->data.timings[j];
Zhao Yakui5c612592009-06-22 13:17:10 +0800589 newmode = drm_mode_std(dev, std,
590 timing_level);
Dave Airlief453ba02008-11-07 14:05:41 -0800591 if (newmode) {
592 drm_mode_probed_add(connector, newmode);
593 modes++;
594 }
595 }
596 break;
597 default:
598 break;
599 }
600 }
601
602 return modes;
603}
604
605#define DDC_ADDR 0x50
Ma Ling167f3a02009-03-20 14:09:48 +0800606/**
607 * Get EDID information via I2C.
608 *
609 * \param adapter : i2c device adaptor
610 * \param buf : EDID data buffer to be filled
611 * \param len : EDID data buffer length
612 * \return 0 on success or -1 on failure.
613 *
614 * Try to fetch EDID information by calling i2c driver function.
615 */
616int drm_do_probe_ddc_edid(struct i2c_adapter *adapter,
617 unsigned char *buf, int len)
Dave Airlief453ba02008-11-07 14:05:41 -0800618{
619 unsigned char start = 0x0;
Dave Airlief453ba02008-11-07 14:05:41 -0800620 struct i2c_msg msgs[] = {
621 {
622 .addr = DDC_ADDR,
623 .flags = 0,
624 .len = 1,
625 .buf = &start,
626 }, {
627 .addr = DDC_ADDR,
628 .flags = I2C_M_RD,
Ma Ling167f3a02009-03-20 14:09:48 +0800629 .len = len,
Dave Airlief453ba02008-11-07 14:05:41 -0800630 .buf = buf,
631 }
632 };
633
Dave Airlief453ba02008-11-07 14:05:41 -0800634 if (i2c_transfer(adapter, msgs, 2) == 2)
Ma Ling167f3a02009-03-20 14:09:48 +0800635 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800636
637 dev_info(&adapter->dev, "unable to read EDID block.\n");
Ma Ling167f3a02009-03-20 14:09:48 +0800638 return -1;
Dave Airlief453ba02008-11-07 14:05:41 -0800639}
640EXPORT_SYMBOL(drm_do_probe_ddc_edid);
641
Ma Ling167f3a02009-03-20 14:09:48 +0800642static int drm_ddc_read_edid(struct drm_connector *connector,
643 struct i2c_adapter *adapter,
644 char *buf, int len)
645{
646 int ret;
647
Keith Packard61f11692009-05-30 20:42:27 -0700648 ret = drm_do_probe_ddc_edid(adapter, buf, len);
Ma Ling167f3a02009-03-20 14:09:48 +0800649 if (ret != 0) {
650 dev_info(&connector->dev->pdev->dev, "%s: no EDID data\n",
651 drm_get_connector_name(connector));
652 goto end;
653 }
654 if (!edid_is_valid((struct edid *)buf)) {
655 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
656 drm_get_connector_name(connector));
657 ret = -1;
658 }
659end:
660 return ret;
661}
662
663#define MAX_EDID_EXT_NUM 4
Dave Airlief453ba02008-11-07 14:05:41 -0800664/**
665 * drm_get_edid - get EDID data, if available
666 * @connector: connector we're probing
667 * @adapter: i2c adapter to use for DDC
668 *
669 * Poke the given connector's i2c channel to grab EDID data if possible.
670 *
671 * Return edid data or NULL if we couldn't find any.
672 */
673struct edid *drm_get_edid(struct drm_connector *connector,
674 struct i2c_adapter *adapter)
675{
Ma Ling167f3a02009-03-20 14:09:48 +0800676 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800677 struct edid *edid;
678
Ma Ling167f3a02009-03-20 14:09:48 +0800679 edid = kmalloc(EDID_LENGTH * (MAX_EDID_EXT_NUM + 1),
680 GFP_KERNEL);
681 if (edid == NULL) {
682 dev_warn(&connector->dev->pdev->dev,
683 "Failed to allocate EDID\n");
684 goto end;
Dave Airlief453ba02008-11-07 14:05:41 -0800685 }
Ma Ling167f3a02009-03-20 14:09:48 +0800686
687 /* Read first EDID block */
688 ret = drm_ddc_read_edid(connector, adapter,
689 (unsigned char *)edid, EDID_LENGTH);
690 if (ret != 0)
691 goto clean_up;
692
693 /* There are EDID extensions to be read */
694 if (edid->extensions != 0) {
695 int edid_ext_num = edid->extensions;
696
697 if (edid_ext_num > MAX_EDID_EXT_NUM) {
698 dev_warn(&connector->dev->pdev->dev,
699 "The number of extension(%d) is "
700 "over max (%d), actually read number (%d)\n",
701 edid_ext_num, MAX_EDID_EXT_NUM,
702 MAX_EDID_EXT_NUM);
703 /* Reset EDID extension number to be read */
704 edid_ext_num = MAX_EDID_EXT_NUM;
705 }
706 /* Read EDID including extensions too */
707 ret = drm_ddc_read_edid(connector, adapter, (char *)edid,
708 EDID_LENGTH * (edid_ext_num + 1));
709 if (ret != 0)
710 goto clean_up;
711
Dave Airlief453ba02008-11-07 14:05:41 -0800712 }
713
714 connector->display_info.raw_edid = (char *)edid;
Ma Ling167f3a02009-03-20 14:09:48 +0800715 goto end;
Dave Airlief453ba02008-11-07 14:05:41 -0800716
Ma Ling167f3a02009-03-20 14:09:48 +0800717clean_up:
718 kfree(edid);
719 edid = NULL;
720end:
Dave Airlief453ba02008-11-07 14:05:41 -0800721 return edid;
Ma Ling167f3a02009-03-20 14:09:48 +0800722
Dave Airlief453ba02008-11-07 14:05:41 -0800723}
724EXPORT_SYMBOL(drm_get_edid);
725
Ma Lingf23c20c2009-03-26 19:26:23 +0800726#define HDMI_IDENTIFIER 0x000C03
727#define VENDOR_BLOCK 0x03
728/**
729 * drm_detect_hdmi_monitor - detect whether monitor is hdmi.
730 * @edid: monitor EDID information
731 *
732 * Parse the CEA extension according to CEA-861-B.
733 * Return true if HDMI, false if not or unknown.
734 */
735bool drm_detect_hdmi_monitor(struct edid *edid)
736{
737 char *edid_ext = NULL;
738 int i, hdmi_id, edid_ext_num;
739 int start_offset, end_offset;
740 bool is_hdmi = false;
741
742 /* No EDID or EDID extensions */
743 if (edid == NULL || edid->extensions == 0)
744 goto end;
745
746 /* Chose real EDID extension number */
747 edid_ext_num = edid->extensions > MAX_EDID_EXT_NUM ?
748 MAX_EDID_EXT_NUM : edid->extensions;
749
750 /* Find CEA extension */
751 for (i = 0; i < edid_ext_num; i++) {
752 edid_ext = (char *)edid + EDID_LENGTH * (i + 1);
753 /* This block is CEA extension */
754 if (edid_ext[0] == 0x02)
755 break;
756 }
757
758 if (i == edid_ext_num)
759 goto end;
760
761 /* Data block offset in CEA extension block */
762 start_offset = 4;
763 end_offset = edid_ext[2];
764
765 /*
766 * Because HDMI identifier is in Vendor Specific Block,
767 * search it from all data blocks of CEA extension.
768 */
769 for (i = start_offset; i < end_offset;
770 /* Increased by data block len */
771 i += ((edid_ext[i] & 0x1f) + 1)) {
772 /* Find vendor specific block */
773 if ((edid_ext[i] >> 5) == VENDOR_BLOCK) {
774 hdmi_id = edid_ext[i + 1] | (edid_ext[i + 2] << 8) |
775 edid_ext[i + 3] << 16;
776 /* Find HDMI identifier */
777 if (hdmi_id == HDMI_IDENTIFIER)
778 is_hdmi = true;
779 break;
780 }
781 }
782
783end:
784 return is_hdmi;
785}
786EXPORT_SYMBOL(drm_detect_hdmi_monitor);
787
Dave Airlief453ba02008-11-07 14:05:41 -0800788/**
789 * drm_add_edid_modes - add modes from EDID data, if available
790 * @connector: connector we're probing
791 * @edid: edid data
792 *
793 * Add the specified modes to the connector's mode list.
794 *
795 * Return number of modes added or 0 if we couldn't find any.
796 */
797int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
798{
799 int num_modes = 0;
800 u32 quirks;
801
802 if (edid == NULL) {
803 return 0;
804 }
805 if (!edid_is_valid(edid)) {
806 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
807 drm_get_connector_name(connector));
808 return 0;
809 }
810
811 quirks = edid_get_quirks(edid);
812
813 num_modes += add_established_modes(connector, edid);
814 num_modes += add_standard_modes(connector, edid);
815 num_modes += add_detailed_info(connector, edid, quirks);
816
817 if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
818 edid_fixup_preferred(connector, quirks);
819
Michel Dänzer0454bea2009-06-15 16:56:07 +0200820 connector->display_info.serration_vsync = (edid->input & DRM_EDID_INPUT_SERRATION_VSYNC) ? 1 : 0;
821 connector->display_info.sync_on_green = (edid->input & DRM_EDID_INPUT_SYNC_ON_GREEN) ? 1 : 0;
822 connector->display_info.composite_sync = (edid->input & DRM_EDID_INPUT_COMPOSITE_SYNC) ? 1 : 0;
823 connector->display_info.separate_syncs = (edid->input & DRM_EDID_INPUT_SEPARATE_SYNCS) ? 1 : 0;
824 connector->display_info.blank_to_black = (edid->input & DRM_EDID_INPUT_BLANK_TO_BLACK) ? 1 : 0;
825 connector->display_info.video_level = (edid->input & DRM_EDID_INPUT_VIDEO_LEVEL) >> 5;
826 connector->display_info.digital = (edid->input & DRM_EDID_INPUT_DIGITAL) ? 1 : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800827 connector->display_info.width_mm = edid->width_cm * 10;
828 connector->display_info.height_mm = edid->height_cm * 10;
829 connector->display_info.gamma = edid->gamma;
Michel Dänzer0454bea2009-06-15 16:56:07 +0200830 connector->display_info.gtf_supported = (edid->features & DRM_EDID_FEATURE_DEFAULT_GTF) ? 1 : 0;
831 connector->display_info.standard_color = (edid->features & DRM_EDID_FEATURE_STANDARD_COLOR) ? 1 : 0;
832 connector->display_info.display_type = (edid->features & DRM_EDID_FEATURE_DISPLAY_TYPE) >> 3;
833 connector->display_info.active_off_supported = (edid->features & DRM_EDID_FEATURE_PM_ACTIVE_OFF) ? 1 : 0;
834 connector->display_info.suspend_supported = (edid->features & DRM_EDID_FEATURE_PM_SUSPEND) ? 1 : 0;
835 connector->display_info.standby_supported = (edid->features & DRM_EDID_FEATURE_PM_STANDBY) ? 1 : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800836 connector->display_info.gamma = edid->gamma;
837
838 return num_modes;
839}
840EXPORT_SYMBOL(drm_add_edid_modes);