NV50: A minor change.
[platform/upstream/libdrm.git] / linux-core / drm_edid.c
1 /*
2  * Copyright (c) 2006 Luc Verhaegen (quirks list)
3  * Copyright (c) 2007 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
38  *   - port quirks from X server code
39  */
40
41 /*
42  * EDID blocks out in the wild have a variety of bugs, try to collect
43  * them here (note that userspace may work around broken monitors first,
44  * but fixes should make their way here so that the kernel "just works"
45  * on as many displays as possible).
46  */
47
48 /* First detailed mode wrong, use largest 60Hz mode */
49 #define EDID_QUIRK_PREFER_LARGE_60              (1 << 0)
50 /* Reported 135MHz pixel clock is too high, needs adjustment */
51 #define EDID_QUIRK_135_CLOCK_TOO_HIGH           (1 << 1)
52 /* Prefer the largest mode at 75 Hz */
53 #define EDID_QUIRK_PREFER_LARGE_75              (1 << 2)
54 /* Detail timing is in cm not mm */
55 #define EDID_QUIRK_DETAILED_IN_CM               (1 << 3)
56 /* Detailed timing descriptors have bogus size values, so just take the
57  * maximum size and use that.
58  */
59 #define EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE    (1 << 4)
60 /* Monitor forgot to set the first detailed is preferred bit. */
61 #define EDID_QUIRK_FIRST_DETAILED_PREFERRED     (1 << 5)
62 /* use +hsync +vsync for detailed mode */
63 #define EDID_QUIRK_DETAILED_SYNC_PP             (1 << 6)
64
65 static struct edid_quirk {
66         char *vendor;
67         int product_id;
68         u32 quirks;
69 } edid_quirk_list[] = {
70         /* Acer AL1706 */
71         { "ACR", 44358, EDID_QUIRK_PREFER_LARGE_60 },
72         /* Acer F51 */
73         { "API", 0x7602, EDID_QUIRK_PREFER_LARGE_60 },
74         /* Unknown Acer */
75         { "ACR", 2423, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
76
77         /* Belinea 10 15 55 */
78         { "MAX", 1516, EDID_QUIRK_PREFER_LARGE_60 },
79         { "MAX", 0x77e, EDID_QUIRK_PREFER_LARGE_60 },
80
81         /* Envision Peripherals, Inc. EN-7100e */
82         { "EPI", 59264, EDID_QUIRK_135_CLOCK_TOO_HIGH },
83
84         /* Funai Electronics PM36B */
85         { "FCM", 13600, EDID_QUIRK_PREFER_LARGE_75 |
86           EDID_QUIRK_DETAILED_IN_CM },
87
88         /* LG Philips LCD LP154W01-A5 */
89         { "LPL", 0, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
90         { "LPL", 0x2a00, EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE },
91
92         /* Philips 107p5 CRT */
93         { "PHL", 57364, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
94
95         /* Proview AY765C */
96         { "PTS", 765, EDID_QUIRK_FIRST_DETAILED_PREFERRED },
97
98         /* Samsung SyncMaster 205BW.  Note: irony */
99         { "SAM", 541, EDID_QUIRK_DETAILED_SYNC_PP },
100         /* Samsung SyncMaster 22[5-6]BW */
101         { "SAM", 596, EDID_QUIRK_PREFER_LARGE_60 },
102         { "SAM", 638, EDID_QUIRK_PREFER_LARGE_60 },
103 };
104
105
106 /* Valid EDID header has these bytes */
107 static u8 edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
108
109 /**
110  * edid_is_valid - sanity check EDID data
111  * @edid: EDID data
112  *
113  * Sanity check the EDID block by looking at the header, the version number
114  * and the checksum.  Return 0 if the EDID doesn't check out, or 1 if it's
115  * valid.
116  */
117 static bool edid_is_valid(struct edid *edid)
118 {
119         int i;
120         u8 csum = 0;
121         u8 *raw_edid = (u8 *)edid;
122
123         if (memcmp(edid->header, edid_header, sizeof(edid_header)))
124                 goto bad;
125         if (edid->version != 1) {
126                 DRM_ERROR("EDID has major version %d, instead of 1\n", edid->version);
127                 goto bad;
128         }
129         if (edid->revision <= 0 || edid->revision > 3) {
130                 DRM_ERROR("EDID has minor version %d, which is not between 0-3\n", edid->revision);
131                 goto bad;
132         }
133
134         for (i = 0; i < EDID_LENGTH; i++)
135                 csum += raw_edid[i];
136         if (csum) {
137                 DRM_ERROR("EDID checksum is invalid, remainder is %d\n", csum);
138                 goto bad;
139         }
140
141         return 1;
142
143 bad:
144         if (raw_edid) {
145                 DRM_ERROR("Raw EDID:\n");
146                 print_hex_dump_bytes(KERN_ERR, DUMP_PREFIX_NONE, raw_edid, EDID_LENGTH);
147                 printk("\n");
148         }
149         return 0;
150 }
151
152 /**
153  * edid_vendor - match a string against EDID's obfuscated vendor field
154  * @edid: EDID to match
155  * @vendor: vendor string
156  *
157  * Returns true if @vendor is in @edid, false otherwise
158  */
159 static bool edid_vendor(struct edid *edid, char *vendor)
160 {
161         char edid_vendor[3];
162
163         edid_vendor[0] = ((edid->mfg_id[0] & 0x7c) >> 2) + '@';
164         edid_vendor[1] = (((edid->mfg_id[0] & 0x3) << 3) |
165                           ((edid->mfg_id[1] & 0xe0) >> 5)) + '@';
166         edid_vendor[2] = (edid->mfg_id[2] & 0x1f) + '@';
167
168         return !strncmp(edid_vendor, vendor, 3);
169 }
170
171 /**
172  * edid_get_quirks - return quirk flags for a given EDID
173  * @edid: EDID to process
174  *
175  * This tells subsequent routines what fixes they need to apply.
176  */
177 static u32 edid_get_quirks(struct edid *edid)
178 {
179         struct edid_quirk *quirk;
180         int i;
181
182         for (i = 0; i < ARRAY_SIZE(edid_quirk_list); i++) {
183                 quirk = &edid_quirk_list[i];
184
185                 if (edid_vendor(edid, quirk->vendor) &&
186                     (EDID_PRODUCT_ID(edid) == quirk->product_id))
187                         return quirk->quirks;
188         }
189
190         return 0;
191 }
192
193 #define MODE_SIZE(m) ((m)->hdisplay * (m)->vdisplay)
194 #define MODE_REFRESH_DIFF(m,r) (abs((m)->vrefresh - target_refresh))
195
196
197 /**
198  * edid_fixup_preferred - set preferred modes based on quirk list
199  * @connector: has mode list to fix up
200  * @quirks: quirks list
201  *
202  * Walk the mode list for @connector, clearing the preferred status
203  * on existing modes and setting it anew for the right mode ala @quirks.
204  */
205 static void edid_fixup_preferred(struct drm_connector *connector,
206                                  u32 quirks)
207 {
208         struct drm_display_mode *t, *cur_mode, *preferred_mode;
209         int target_refresh;
210
211         if (list_empty(&connector->probed_modes))
212                 return;
213
214         if (quirks & EDID_QUIRK_PREFER_LARGE_60)
215                 target_refresh = 60;
216         if (quirks & EDID_QUIRK_PREFER_LARGE_75)
217                 target_refresh = 75;
218
219         preferred_mode = list_first_entry(&connector->probed_modes,
220                                           struct drm_display_mode, head);
221
222         list_for_each_entry_safe(cur_mode, t, &connector->probed_modes, head) {
223                 cur_mode->type &= ~DRM_MODE_TYPE_PREFERRED;
224
225                 if (cur_mode == preferred_mode)
226                         continue;
227
228                 /* Largest mode is preferred */
229                 if (MODE_SIZE(cur_mode) > MODE_SIZE(preferred_mode))
230                         preferred_mode = cur_mode;
231
232                 /* At a given size, try to get closest to target refresh */
233                 if ((MODE_SIZE(cur_mode) == MODE_SIZE(preferred_mode)) &&
234                     MODE_REFRESH_DIFF(cur_mode, target_refresh) <
235                     MODE_REFRESH_DIFF(preferred_mode, target_refresh)) {
236                         preferred_mode = cur_mode;
237                 }
238         }
239
240         preferred_mode->type |= DRM_MODE_TYPE_PREFERRED;
241 }
242
243 /**
244  * drm_mode_std - convert standard mode info (width, height, refresh) into mode
245  * @t: standard timing params
246  *
247  * Take the standard timing params (in this case width, aspect, and refresh)
248  * and convert them into a real mode using CVT.
249  *
250  * Punts for now, but should eventually use the FB layer's CVT based mode
251  * generation code.
252  */
253 struct drm_display_mode *drm_mode_std(struct drm_device *dev,
254                                       struct std_timing *t)
255 {
256 //      struct fb_videomode mode;
257
258 //      fb_find_mode_cvt(&mode, 0, 0);
259         /* JJJ:  convert to drm_display_mode */
260         struct drm_display_mode *mode;
261         int hsize = t->hsize * 8 + 248, vsize;
262
263         mode = drm_mode_create(dev);
264         if (!mode)
265                 return NULL;
266
267         if (t->aspect_ratio == 0)
268                 vsize = (hsize * 10) / 16;
269         else if (t->aspect_ratio == 1)
270                 vsize = (hsize * 3) / 4;
271         else if (t->aspect_ratio == 2)
272                 vsize = (hsize * 4) / 5;
273         else
274                 vsize = (hsize * 9) / 16;
275
276         drm_mode_set_name(mode);
277
278         return mode;
279 }
280
281 /**
282  * drm_mode_detailed - create a new mode from an EDID detailed timing section
283  * @dev: DRM device (needed to create new mode)
284  * @edid: EDID block
285  * @timing: EDID detailed timing info
286  * @quirks: quirks to apply
287  *
288  * An EDID detailed timing block contains enough info for us to create and
289  * return a new struct drm_display_mode.
290  */
291 static struct drm_display_mode *drm_mode_detailed(struct drm_device *dev,
292                                                   struct edid *edid,
293                                                   struct detailed_timing *timing,
294                                                   u32 quirks)
295 {
296         struct drm_display_mode *mode;
297         struct detailed_pixel_timing *pt = &timing->data.pixel_data;
298
299         if (pt->stereo) {
300                 printk(KERN_WARNING "stereo mode not supported\n");
301                 return NULL;
302         }
303         if (!pt->separate_sync) {
304                 printk(KERN_WARNING "integrated sync not supported\n");
305                 return NULL;
306         }
307
308         mode = drm_mode_create(dev);
309         if (!mode)
310                 return NULL;
311
312         mode->type = DRM_MODE_TYPE_DRIVER;
313
314         if (quirks & EDID_QUIRK_135_CLOCK_TOO_HIGH)
315                 timing->pixel_clock = 1088;
316
317         mode->clock = timing->pixel_clock * 10;
318
319         mode->hdisplay = (pt->hactive_hi << 8) | pt->hactive_lo;
320         mode->hsync_start = mode->hdisplay + ((pt->hsync_offset_hi << 8) |
321                                               pt->hsync_offset_lo);
322         mode->hsync_end = mode->hsync_start +
323                 ((pt->hsync_pulse_width_hi << 8) |
324                  pt->hsync_pulse_width_lo);
325         mode->htotal = mode->hdisplay + ((pt->hblank_hi << 8) | pt->hblank_lo);
326
327         mode->vdisplay = (pt->vactive_hi << 8) | pt->vactive_lo;
328         mode->vsync_start = mode->vdisplay + ((pt->vsync_offset_hi << 8) |
329                                               pt->vsync_offset_lo);
330         mode->vsync_end = mode->vsync_start +
331                 ((pt->vsync_pulse_width_hi << 8) |
332                  pt->vsync_pulse_width_lo);
333         mode->vtotal = mode->vdisplay + ((pt->vblank_hi << 8) | pt->vblank_lo);
334
335         drm_mode_set_name(mode);
336
337         if (pt->interlaced)
338                 mode->flags |= V_INTERLACE;
339
340         if (quirks & EDID_QUIRK_DETAILED_SYNC_PP) {
341                 pt->hsync_positive = 1;
342                 pt->vsync_positive = 1;
343         }
344
345         mode->flags |= pt->hsync_positive ? V_PHSYNC : V_NHSYNC;
346         mode->flags |= pt->vsync_positive ? V_PVSYNC : V_NVSYNC;
347
348         mode->width_mm = pt->width_mm_lo | (pt->width_mm_hi << 8);
349         mode->height_mm = pt->height_mm_lo | (pt->height_mm_hi << 8);
350
351         if (quirks & EDID_QUIRK_DETAILED_IN_CM) {
352                 mode->width_mm *= 10;
353                 mode->height_mm *= 10;
354         }
355
356         if (quirks & EDID_QUIRK_DETAILED_USE_MAXIMUM_SIZE) {
357                 mode->width_mm = edid->width_cm * 10;
358                 mode->height_mm = edid->height_cm * 10;
359         }
360
361         return mode;
362 }
363
364 /*
365  * Detailed mode info for the EDID "established modes" data to use.
366  */
367 static struct drm_display_mode edid_est_modes[] = {
368         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
369                    968, 1056, 0, 600, 601, 605, 628, 0,
370                    V_PHSYNC | V_PVSYNC) }, /* 800x600@60Hz */
371         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 36000, 800, 824,
372                    896, 1024, 0, 600, 601, 603,  625, 0,
373                    V_PHSYNC | V_PVSYNC) }, /* 800x600@56Hz */
374         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 656,
375                    720, 840, 0, 480, 481, 484, 500, 0,
376                    V_NHSYNC | V_NVSYNC) }, /* 640x480@75Hz */
377         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 31500, 640, 664,
378                    704,  832, 0, 480, 489, 491, 520, 0,
379                    V_NHSYNC | V_NVSYNC) }, /* 640x480@72Hz */
380         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 30240, 640, 704,
381                    768,  864, 0, 480, 483, 486, 525, 0,
382                    V_NHSYNC | V_NVSYNC) }, /* 640x480@67Hz */
383         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25200, 640, 656,
384                    752, 800, 0, 480, 490, 492, 525, 0,
385                    V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
386         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 35500, 720, 738,
387                    846, 900, 0, 400, 421, 423,  449, 0,
388                    V_NHSYNC | V_NVSYNC) }, /* 720x400@88Hz */
389         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 28320, 720, 738,
390                    846,  900, 0, 400, 412, 414, 449, 0,
391                    V_NHSYNC | V_PVSYNC) }, /* 720x400@70Hz */
392         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 135000, 1280, 1296,
393                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
394                    V_PHSYNC | V_PVSYNC) }, /* 1280x1024@75Hz */
395         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 78800, 1024, 1040,
396                    1136, 1312, 0,  768, 769, 772, 800, 0,
397                    V_PHSYNC | V_PVSYNC) }, /* 1024x768@75Hz */
398         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 75000, 1024, 1048,
399                    1184, 1328, 0,  768, 771, 777, 806, 0,
400                    V_NHSYNC | V_NVSYNC) }, /* 1024x768@70Hz */
401         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
402                    1184, 1344, 0,  768, 771, 777, 806, 0,
403                    V_NHSYNC | V_NVSYNC) }, /* 1024x768@60Hz */
404         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER,44900, 1024, 1032,
405                    1208, 1264, 0, 768, 768, 776, 817, 0,
406                    V_PHSYNC | V_PVSYNC | V_INTERLACE) }, /* 1024x768@43Hz */
407         { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 57284, 832, 864,
408                    928, 1152, 0, 624, 625, 628, 667, 0,
409                    V_NHSYNC | V_NVSYNC) }, /* 832x624@75Hz */
410         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 49500, 800, 816,
411                    896, 1056, 0, 600, 601, 604,  625, 0,
412                    V_PHSYNC | V_PVSYNC) }, /* 800x600@75Hz */
413         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 50000, 800, 856,
414                    976, 1040, 0, 600, 637, 643, 666, 0,
415                    V_PHSYNC | V_PVSYNC) }, /* 800x600@72Hz */
416         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
417                    1344, 1600, 0,  864, 865, 868, 900, 0,
418                    V_PHSYNC | V_PVSYNC) }, /* 1152x864@75Hz */
419 };
420
421 #define EDID_EST_TIMINGS 16
422 #define EDID_STD_TIMINGS 8
423 #define EDID_DETAILED_TIMINGS 4
424
425 /**
426  * add_established_modes - get est. modes from EDID and add them
427  * @edid: EDID block to scan
428  *
429  * Each EDID block contains a bitmap of the supported "established modes" list
430  * (defined above).  Tease them out and add them to the global modes list.
431  */
432 static int add_established_modes(struct drm_connector *connector, struct edid *edid)
433 {
434         struct drm_device *dev = connector->dev;
435         unsigned long est_bits = edid->established_timings.t1 |
436                 (edid->established_timings.t2 << 8) |
437                 ((edid->established_timings.mfg_rsvd & 0x80) << 9);
438         int i, modes = 0;
439
440         for (i = 0; i <= EDID_EST_TIMINGS; i++)
441                 if (est_bits & (1<<i)) {
442                         struct drm_display_mode *newmode;
443                         newmode = drm_mode_duplicate(dev, &edid_est_modes[i]);
444                         if (newmode) {
445                                 drm_mode_probed_add(connector, newmode);
446                                 modes++;
447                         }
448                 }
449
450         return modes;
451 }
452
453 /**
454  * add_standard_modes - get std. modes from EDID and add them
455  * @edid: EDID block to scan
456  *
457  * Standard modes can be calculated using the CVT standard.  Grab them from
458  * @edid, calculate them, and add them to the list.
459  */
460 static int add_standard_modes(struct drm_connector *connector, struct edid *edid)
461 {
462         struct drm_device *dev = connector->dev;
463         int i, modes = 0;
464
465         for (i = 0; i < EDID_STD_TIMINGS; i++) {
466                 struct std_timing *t = &edid->standard_timings[i];
467                 struct drm_display_mode *newmode;
468
469                 /* If std timings bytes are 1, 1 it's empty */
470                 if (t->hsize == 1 && (t->aspect_ratio | t->vfreq) == 1)
471                         continue;
472
473                 newmode = drm_mode_std(dev, &edid->standard_timings[i]);
474                 if (newmode) {
475                         drm_mode_probed_add(connector, newmode);
476                         modes++;
477                 }
478         }
479
480         return modes;
481 }
482
483 /**
484  * add_detailed_modes - get detailed mode info from EDID data
485  * @connector: attached connector
486  * @edid: EDID block to scan
487  * @quirks: quirks to apply
488  *
489  * Some of the detailed timing sections may contain mode information.  Grab
490  * it and add it to the list.
491  */
492 static int add_detailed_info(struct drm_connector *connector,
493                              struct edid *edid, u32 quirks)
494 {
495         struct drm_device *dev = connector->dev;
496         int i, j, modes = 0;
497
498         for (i = 0; i < EDID_DETAILED_TIMINGS; i++) {
499                 struct detailed_timing *timing = &edid->detailed_timings[i];
500                 struct detailed_non_pixel *data = &timing->data.other_data;
501                 struct drm_display_mode *newmode;
502
503                 /* EDID up to and including 1.2 may put monitor info here */
504                 if (edid->version == 1 && edid->revision < 3)
505                         continue;
506
507                 /* Detailed mode timing */
508                 if (timing->pixel_clock) {
509                         newmode = drm_mode_detailed(dev, edid, timing, quirks);
510                         if (!newmode)
511                                 continue;
512
513                         /* First detailed mode is preferred */
514                         if (i == 0 && edid->preferred_timing)
515                                 newmode->type |= DRM_MODE_TYPE_PREFERRED;
516                         drm_mode_probed_add(connector, newmode);
517
518                         modes++;
519                         continue;
520                 }
521
522                 /* Other timing or info */
523                 switch (data->type) {
524                 case EDID_DETAIL_MONITOR_SERIAL:
525                         break;
526                 case EDID_DETAIL_MONITOR_STRING:
527                         break;
528                 case EDID_DETAIL_MONITOR_RANGE:
529                         /* Get monitor range data */
530                         break;
531                 case EDID_DETAIL_MONITOR_NAME:
532                         break;
533                 case EDID_DETAIL_MONITOR_CPDATA:
534                         break;
535                 case EDID_DETAIL_STD_MODES:
536                         /* Five modes per detailed section */
537                         for (j = 0; j < 5; i++) {
538                                 struct std_timing *std;
539                                 struct drm_display_mode *newmode;
540
541                                 std = &data->data.timings[j];
542                                 newmode = drm_mode_std(dev, std);
543                                 if (newmode) {
544                                         drm_mode_probed_add(connector, newmode);
545                                         modes++;
546                                 }
547                         }
548                         break;
549                 default:
550                         break;
551                 }
552         }
553
554         return modes;
555 }
556
557 #define DDC_ADDR 0x50
558
559 unsigned char *drm_do_probe_ddc_edid(struct i2c_adapter *adapter)
560 {
561         unsigned char start = 0x0;
562         unsigned char *buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
563         struct i2c_msg msgs[] = {
564                 {
565                         .addr   = DDC_ADDR,
566                         .flags  = 0,
567                         .len    = 1,
568                         .buf    = &start,
569                 }, {
570                         .addr   = DDC_ADDR,
571                         .flags  = I2C_M_RD,
572                         .len    = EDID_LENGTH,
573                         .buf    = buf,
574                 }
575         };
576
577         if (!buf) {
578                 dev_warn(&adapter->dev, "unable to allocate memory for EDID "
579                          "block.\n");
580                 return NULL;
581         }
582
583         if (i2c_transfer(adapter, msgs, 2) == 2)
584                 return buf;
585
586         dev_info(&adapter->dev, "unable to read EDID block.\n");
587         kfree(buf);
588         return NULL;
589 }
590 EXPORT_SYMBOL(drm_do_probe_ddc_edid);
591
592 static unsigned char *drm_ddc_read(struct i2c_adapter *adapter)
593 {
594         struct i2c_algo_bit_data *algo_data = adapter->algo_data;
595         unsigned char *edid = NULL;
596         int i, j;
597
598         /*
599          * Startup the bus:
600          *   Set clock line high (but give it time to come up)
601          *   Then set clock & data low
602          */
603         algo_data->setscl(algo_data->data, 1);
604         udelay(550); /* startup delay */
605         algo_data->setscl(algo_data->data, 0);
606         algo_data->setsda(algo_data->data, 0);
607
608         for (i = 0; i < 3; i++) {
609                 /* For some old monitors we need the
610                  * following process to initialize/stop DDC
611                  */
612                 algo_data->setsda(algo_data->data, 0);
613                 msleep(13);
614
615                 algo_data->setscl(algo_data->data, 1);
616                 for (j = 0; j < 5; j++) {
617                         msleep(10);
618                         if (algo_data->getscl(algo_data->data))
619                                 break;
620                 }
621                 if (j == 5)
622                         continue;
623
624                 algo_data->setsda(algo_data->data, 0);
625                 msleep(15);
626                 algo_data->setscl(algo_data->data, 0);
627                 msleep(15);
628                 algo_data->setsda(algo_data->data, 1);
629                 msleep(15);
630
631                 /* Do the real work */
632                 edid = drm_do_probe_ddc_edid(adapter);
633                 algo_data->setsda(algo_data->data, 0);
634                 algo_data->setscl(algo_data->data, 0);
635                 msleep(15);
636
637                 algo_data->setscl(algo_data->data, 1);
638                 for (j = 0; j < 10; j++) {
639                         msleep(10);
640                         if (algo_data->getscl(algo_data->data))
641                                 break;
642                 }
643
644                 algo_data->setsda(algo_data->data, 1);
645                 msleep(15);
646                 algo_data->setscl(algo_data->data, 0);
647                 if (edid)
648                         break;
649         }
650         /* Release the DDC lines when done or the Apple Cinema HD display
651          * will switch off
652          */
653         algo_data->setsda(algo_data->data, 1);
654         algo_data->setscl(algo_data->data, 1);
655         
656         return edid;
657 }
658
659 /**
660  * drm_get_edid - get EDID data, if available
661  * @connector: connector we're probing
662  * @adapter: i2c adapter to use for DDC
663  *
664  * Poke the given connector's i2c channel to grab EDID data if possible.
665  * 
666  * Return edid data or NULL if we couldn't find any.
667  */
668 struct edid *drm_get_edid(struct drm_connector *connector,
669                           struct i2c_adapter *adapter)
670 {
671         struct edid *edid;
672
673         edid = (struct edid *)drm_ddc_read(adapter);
674         if (!edid) {
675                 dev_warn(&connector->dev->pdev->dev, "%s: no EDID data\n",
676                          drm_get_connector_name(connector));
677                 return NULL;
678         }
679         if (!edid_is_valid(edid)) {
680                 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
681                          drm_get_connector_name(connector));
682                 kfree(edid);
683                 return NULL;
684         }
685
686         connector->display_info.raw_edid = (char *)edid;
687
688         return edid;
689 }
690 EXPORT_SYMBOL(drm_get_edid);
691
692 /**
693  * drm_add_edid_modes - add modes from EDID data, if available
694  * @connector: connector we're probing
695  * @edid: edid data
696  *
697  * Add the specified modes to the connector's mode list.
698  *
699  * Return number of modes added or 0 if we couldn't find any.
700  */
701 int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid)
702 {
703         int num_modes = 0;
704         u32 quirks;
705
706         if (edid == NULL) {
707                 return 0;
708         }
709         if (!edid_is_valid(edid)) {
710                 dev_warn(&connector->dev->pdev->dev, "%s: EDID invalid.\n",
711                          drm_get_connector_name(connector));
712                 return 0;
713         }
714
715         quirks = edid_get_quirks(edid);
716
717         num_modes += add_established_modes(connector, edid);
718         num_modes += add_standard_modes(connector, edid);
719         num_modes += add_detailed_info(connector, edid, quirks);
720
721         if (quirks & (EDID_QUIRK_PREFER_LARGE_60 | EDID_QUIRK_PREFER_LARGE_75))
722                 edid_fixup_preferred(connector, quirks);
723
724         connector->display_info.serration_vsync = edid->serration_vsync;
725         connector->display_info.sync_on_green = edid->sync_on_green;
726         connector->display_info.composite_sync = edid->composite_sync;
727         connector->display_info.separate_syncs = edid->separate_syncs;
728         connector->display_info.blank_to_black = edid->blank_to_black;
729         connector->display_info.video_level = edid->video_level;
730         connector->display_info.digital = edid->digital;
731         connector->display_info.width_mm = edid->width_cm * 10;
732         connector->display_info.height_mm = edid->height_cm * 10;
733         connector->display_info.gamma = edid->gamma;
734         connector->display_info.gtf_supported = edid->default_gtf;
735         connector->display_info.standard_color = edid->standard_color;
736         connector->display_info.display_type = edid->display_type;
737         connector->display_info.active_off_supported = edid->pm_active_off;
738         connector->display_info.suspend_supported = edid->pm_suspend;
739         connector->display_info.standby_supported = edid->pm_standby;
740         connector->display_info.gamma = edid->gamma;
741
742         return num_modes;
743 }
744 EXPORT_SYMBOL(drm_add_edid_modes);