First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / modes / xf86EdidModes.c
blob8b5e69d9a5fea419ecb487f2314f5ecc1a14e429
1 /*
2 * Copyright 2006 Luc Verhaegen.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /**
25 * @file This file covers code to convert a xf86MonPtr containing EDID-probed
26 * information into a list of modes, including applying monitor-specific
27 * quirks to fix broken EDID data.
29 #ifdef HAVE_XORG_CONFIG_H
30 #include <xorg-config.h>
31 #else
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 #endif
37 #include "xf86.h"
38 #include "xf86DDC.h"
39 #include <X11/Xatom.h>
40 #include "property.h"
41 #include "propertyst.h"
42 #include "xf86DDC.h"
43 #include "xf86Crtc.h"
44 #include <string.h>
45 #include <math.h>
48 * Quirks to work around broken EDID data from various monitors.
51 typedef enum {
52 DDC_QUIRK_NONE = 0,
53 /* First detailed mode is bogus, prefer largest mode at 60hz */
54 DDC_QUIRK_PREFER_LARGE_60 = 1 << 0,
55 /* 135MHz clock is too high, drop a bit */
56 DDC_QUIRK_135_CLOCK_TOO_HIGH = 1 << 1,
57 } ddc_quirk_t;
59 static Bool quirk_prefer_large_60 (int scrnIndex, xf86MonPtr DDC)
61 /* Belinea 10 15 55 */
62 if (memcmp (DDC->vendor.name, "MAX", 4) == 0 &&
63 DDC->vendor.prod_id == 1516)
64 return TRUE;
66 /* Acer AL1706 */
67 if (memcmp (DDC->vendor.name, "ACR", 4) == 0 &&
68 DDC->vendor.prod_id == 44358)
69 return TRUE;
71 /* Bug #10814: Samsung SyncMaster 225BW */
72 if (memcmp (DDC->vendor.name, "SAM", 4) == 0 &&
73 DDC->vendor.prod_id == 596)
74 return TRUE;
76 /* Bug #10545: Samsung SyncMaster 226BW */
77 if (memcmp (DDC->vendor.name, "SAM", 4) == 0 &&
78 DDC->vendor.prod_id == 638)
79 return TRUE;
81 return FALSE;
84 static Bool quirk_135_clock_too_high (int scrnIndex, xf86MonPtr DDC)
86 /* Envision Peripherals, Inc. EN-7100e. See bug #9550. */
87 if (memcmp (DDC->vendor.name, "EPI", 4) == 0 &&
88 DDC->vendor.prod_id == 59264)
89 return TRUE;
91 return FALSE;
94 typedef struct {
95 Bool (*detect) (int scrnIndex, xf86MonPtr DDC);
96 ddc_quirk_t quirk;
97 char *description;
98 } ddc_quirk_map_t;
100 static const ddc_quirk_map_t ddc_quirks[] = {
102 quirk_prefer_large_60, DDC_QUIRK_PREFER_LARGE_60,
103 "Detailed timing is not preferred, use largest mode at 60Hz"
106 quirk_135_clock_too_high, DDC_QUIRK_135_CLOCK_TOO_HIGH,
107 "Recommended 135MHz pixel clock is too high"
110 NULL, DDC_QUIRK_NONE,
111 "No known quirks"
116 * TODO:
117 * - for those with access to the VESA DMT standard; review please.
119 #define MODEPREFIX(name) NULL, NULL, name, 0,M_T_DRIVER
120 #define MODESUFFIX 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,FALSE,FALSE,0,NULL,0,0.0,0.0
122 static DisplayModeRec DDCEstablishedModes[17] = {
123 { MODEPREFIX("800x600"), 40000, 800, 840, 968, 1056, 0, 600, 601, 605, 628, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@60Hz */
124 { MODEPREFIX("800x600"), 36000, 800, 824, 896, 1024, 0, 600, 601, 603, 625, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@56Hz */
125 { MODEPREFIX("640x480"), 31500, 640, 656, 720, 840, 0, 480, 481, 484, 500, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@75Hz */
126 { MODEPREFIX("640x480"), 31500, 640, 664, 704, 832, 0, 480, 489, 491, 520, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@72Hz */
127 { MODEPREFIX("640x480"), 30240, 640, 704, 768, 864, 0, 480, 483, 486, 525, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@67Hz */
128 { MODEPREFIX("640x480"), 25200, 640, 656, 752, 800, 0, 480, 490, 492, 525, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 640x480@60Hz */
129 { MODEPREFIX("720x400"), 35500, 720, 738, 846, 900, 0, 400, 421, 423, 449, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 720x400@88Hz */
130 { MODEPREFIX("720x400"), 28320, 720, 738, 846, 900, 0, 400, 412, 414, 449, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 720x400@70Hz */
131 { MODEPREFIX("1280x1024"), 135000, 1280, 1296, 1440, 1688, 0, 1024, 1025, 1028, 1066, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1280x1024@75Hz */
132 { MODEPREFIX("1024x768"), 78800, 1024, 1040, 1136, 1312, 0, 768, 769, 772, 800, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1024x768@75Hz */
133 { MODEPREFIX("1024x768"), 75000, 1024, 1048, 1184, 1328, 0, 768, 771, 777, 806, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@70Hz */
134 { MODEPREFIX("1024x768"), 65000, 1024, 1048, 1184, 1344, 0, 768, 771, 777, 806, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 1024x768@60Hz */
135 { MODEPREFIX("1024x768"), 44900, 1024, 1032, 1208, 1264, 0, 768, 768, 776, 817, 0, V_PHSYNC | V_PVSYNC | V_INTERLACE, MODESUFFIX }, /* 1024x768@43Hz */
136 { MODEPREFIX("832x624"), 57284, 832, 864, 928, 1152, 0, 624, 625, 628, 667, 0, V_NHSYNC | V_NVSYNC, MODESUFFIX }, /* 832x624@75Hz */
137 { MODEPREFIX("800x600"), 49500, 800, 816, 896, 1056, 0, 600, 601, 604, 625, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@75Hz */
138 { MODEPREFIX("800x600"), 50000, 800, 856, 976, 1040, 0, 600, 637, 643, 666, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 800x600@72Hz */
139 { MODEPREFIX("1152x864"), 108000, 1152, 1216, 1344, 1600, 0, 864, 865, 868, 900, 0, V_PHSYNC | V_PVSYNC, MODESUFFIX }, /* 1152x864@75Hz */
142 static DisplayModePtr
143 DDCModesFromEstablished(int scrnIndex, struct established_timings *timing,
144 ddc_quirk_t quirks)
146 DisplayModePtr Modes = NULL, Mode = NULL;
147 CARD32 bits = (timing->t1) | (timing->t2 << 8) |
148 ((timing->t_manu & 0x80) << 9);
149 int i;
151 for (i = 0; i < 17; i++) {
152 if (bits & (0x01 << i)) {
153 Mode = xf86DuplicateMode(&DDCEstablishedModes[i]);
154 Modes = xf86ModesAdd(Modes, Mode);
158 return Modes;
164 static DisplayModePtr
165 DDCModesFromStandardTiming(int scrnIndex, struct std_timings *timing,
166 ddc_quirk_t quirks)
168 DisplayModePtr Modes = NULL, Mode = NULL;
169 int i;
171 for (i = 0; i < STD_TIMINGS; i++) {
172 if (timing[i].hsize && timing[i].vsize && timing[i].refresh) {
173 Mode = xf86CVTMode(timing[i].hsize, timing[i].vsize,
174 timing[i].refresh, FALSE, FALSE);
175 Mode->type = M_T_DRIVER;
176 Modes = xf86ModesAdd(Modes, Mode);
180 return Modes;
186 static DisplayModePtr
187 DDCModeFromDetailedTiming(int scrnIndex, struct detailed_timings *timing,
188 int preferred, ddc_quirk_t quirks)
190 DisplayModePtr Mode;
193 * Refuse to create modes that are insufficiently large. 64 is a random
194 * number, maybe the spec says something about what the minimum is. In
195 * particular I see this frequently with _old_ EDID, 1.0 or so, so maybe
196 * our parser is just being too aggresive there.
198 if (timing->h_active < 64 || timing->v_active < 64) {
199 xf86DrvMsg(scrnIndex, X_INFO,
200 "%s: Ignoring tiny %dx%d mode\n", __func__,
201 timing->h_active, timing->v_active);
202 return NULL;
205 /* We don't do stereo */
206 if (timing->stereo) {
207 xf86DrvMsg(scrnIndex, X_INFO,
208 "%s: Ignoring: We don't handle stereo.\n", __func__);
209 return NULL;
212 /* We only do seperate sync currently */
213 if (timing->sync != 0x03) {
214 xf86DrvMsg(scrnIndex, X_INFO,
215 "%s: %dx%d Warning: We only handle seperate"
216 " sync.\n", __func__, timing->h_active, timing->v_active);
219 Mode = xnfalloc(sizeof(DisplayModeRec));
220 memset(Mode, 0, sizeof(DisplayModeRec));
222 Mode->type = M_T_DRIVER;
223 if (preferred)
224 Mode->type |= M_T_PREFERRED;
226 if( ( quirks & DDC_QUIRK_135_CLOCK_TOO_HIGH ) &&
227 timing->clock == 135000000 )
228 Mode->Clock = 108880;
229 else
230 Mode->Clock = timing->clock / 1000.0;
232 Mode->HDisplay = timing->h_active;
233 Mode->HSyncStart = timing->h_active + timing->h_sync_off;
234 Mode->HSyncEnd = Mode->HSyncStart + timing->h_sync_width;
235 Mode->HTotal = timing->h_active + timing->h_blanking;
237 Mode->VDisplay = timing->v_active;
238 Mode->VSyncStart = timing->v_active + timing->v_sync_off;
239 Mode->VSyncEnd = Mode->VSyncStart + timing->v_sync_width;
240 Mode->VTotal = timing->v_active + timing->v_blanking;
242 xf86SetModeDefaultName(Mode);
244 /* We ignore h/v_size and h/v_border for now. */
246 if (timing->interlaced)
247 Mode->Flags |= V_INTERLACE;
249 if (timing->misc & 0x02)
250 Mode->Flags |= V_PVSYNC;
251 else
252 Mode->Flags |= V_NVSYNC;
254 if (timing->misc & 0x01)
255 Mode->Flags |= V_PHSYNC;
256 else
257 Mode->Flags |= V_NHSYNC;
259 return Mode;
265 static void
266 DDCGuessRangesFromModes(int scrnIndex, MonPtr Monitor, DisplayModePtr Modes)
268 DisplayModePtr Mode = Modes;
270 if (!Monitor || !Modes)
271 return;
273 /* set up the ranges for scanning through the modes */
274 Monitor->nHsync = 1;
275 Monitor->hsync[0].lo = 1024.0;
276 Monitor->hsync[0].hi = 0.0;
278 Monitor->nVrefresh = 1;
279 Monitor->vrefresh[0].lo = 1024.0;
280 Monitor->vrefresh[0].hi = 0.0;
282 while (Mode) {
283 if (!Mode->HSync)
284 Mode->HSync = ((float) Mode->Clock ) / ((float) Mode->HTotal);
286 if (!Mode->VRefresh)
287 Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
288 ((float) (Mode->HTotal * Mode->VTotal));
290 if (Mode->HSync < Monitor->hsync[0].lo)
291 Monitor->hsync[0].lo = Mode->HSync;
293 if (Mode->HSync > Monitor->hsync[0].hi)
294 Monitor->hsync[0].hi = Mode->HSync;
296 if (Mode->VRefresh < Monitor->vrefresh[0].lo)
297 Monitor->vrefresh[0].lo = Mode->VRefresh;
299 if (Mode->VRefresh > Monitor->vrefresh[0].hi)
300 Monitor->vrefresh[0].hi = Mode->VRefresh;
302 Mode = Mode->next;
306 _X_EXPORT DisplayModePtr
307 xf86DDCGetModes(int scrnIndex, xf86MonPtr DDC)
309 int preferred, i;
310 DisplayModePtr Modes = NULL, Mode;
311 ddc_quirk_t quirks;
313 xf86DrvMsg (scrnIndex, X_INFO, "EDID vendor \"%s\", prod id %d\n",
314 DDC->vendor.name, DDC->vendor.prod_id);
315 quirks = DDC_QUIRK_NONE;
316 for (i = 0; ddc_quirks[i].detect; i++)
317 if (ddc_quirks[i].detect (scrnIndex, DDC))
319 xf86DrvMsg (scrnIndex, X_INFO, " EDID quirk: %s\n",
320 ddc_quirks[i].description);
321 quirks |= ddc_quirks[i].quirk;
324 preferred = PREFERRED_TIMING_MODE(DDC->features.msc);
325 if (quirks & DDC_QUIRK_PREFER_LARGE_60)
326 preferred = 0;
328 for (i = 0; i < DET_TIMINGS; i++) {
329 struct detailed_monitor_section *det_mon = &DDC->det_mon[i];
331 switch (det_mon->type) {
332 case DT:
333 Mode = DDCModeFromDetailedTiming(scrnIndex,
334 &det_mon->section.d_timings,
335 preferred,
336 quirks);
337 preferred = 0;
338 Modes = xf86ModesAdd(Modes, Mode);
339 break;
340 case DS_STD_TIMINGS:
341 Mode = DDCModesFromStandardTiming(scrnIndex,
342 det_mon->section.std_t,
343 quirks);
344 Modes = xf86ModesAdd(Modes, Mode);
345 break;
346 default:
347 break;
351 /* Add established timings */
352 Mode = DDCModesFromEstablished(scrnIndex, &DDC->timings1, quirks);
353 Modes = xf86ModesAdd(Modes, Mode);
355 /* Add standard timings */
356 Mode = DDCModesFromStandardTiming(scrnIndex, DDC->timings2, quirks);
357 Modes = xf86ModesAdd(Modes, Mode);
359 if (quirks & DDC_QUIRK_PREFER_LARGE_60)
361 DisplayModePtr best = Modes;
362 for (Mode = Modes; Mode; Mode = Mode->next)
364 if (Mode == best) continue;
365 if (Mode->HDisplay * Mode->VDisplay > best->HDisplay * best->VDisplay)
367 best = Mode;
368 continue;
370 if (Mode->HDisplay * Mode->VDisplay == best->HDisplay * best->VDisplay)
372 double mode_refresh = xf86ModeVRefresh (Mode);
373 double best_refresh = xf86ModeVRefresh (best);
374 double mode_dist = fabs(mode_refresh - 60.0);
375 double best_dist = fabs(best_refresh - 60.0);
376 if (mode_dist < best_dist)
378 best = Mode;
379 continue;
383 if (best)
384 best->type |= M_T_PREFERRED;
386 return Modes;
390 * Fill out MonPtr with xf86MonPtr information.
392 _X_EXPORT void
393 xf86DDCMonitorSet(int scrnIndex, MonPtr Monitor, xf86MonPtr DDC)
395 DisplayModePtr Modes = NULL, Mode;
396 int i, clock;
397 Bool have_hsync = FALSE, have_vrefresh = FALSE, have_maxpixclock = FALSE;
399 if (!Monitor || !DDC)
400 return;
402 Monitor->DDC = DDC;
404 Monitor->widthmm = 10 * DDC->features.hsize;
405 Monitor->heightmm = 10 * DDC->features.vsize;
407 /* If this is a digital display, then we can use reduced blanking */
408 if (DDC->features.input_type)
409 Monitor->reducedblanking = TRUE;
410 /* Allow the user to also enable this through config */
412 Modes = xf86DDCGetModes(scrnIndex, DDC);
414 /* Skip EDID ranges if they were specified in the config file */
415 have_hsync = (Monitor->nHsync != 0);
416 have_vrefresh = (Monitor->nVrefresh != 0);
417 have_maxpixclock = (Monitor->maxPixClock != 0);
419 /* Go through the detailed monitor sections */
420 for (i = 0; i < DET_TIMINGS; i++) {
421 switch (DDC->det_mon[i].type) {
422 case DS_RANGES:
423 if (!have_hsync) {
424 if (!Monitor->nHsync)
425 xf86DrvMsg(scrnIndex, X_INFO,
426 "Using EDID range info for horizontal sync\n");
427 Monitor->hsync[Monitor->nHsync].lo =
428 DDC->det_mon[i].section.ranges.min_h;
429 Monitor->hsync[Monitor->nHsync].hi =
430 DDC->det_mon[i].section.ranges.max_h;
431 Monitor->nHsync++;
432 } else {
433 xf86DrvMsg(scrnIndex, X_INFO,
434 "Using hsync ranges from config file\n");
437 if (!have_vrefresh) {
438 if (!Monitor->nVrefresh)
439 xf86DrvMsg(scrnIndex, X_INFO,
440 "Using EDID range info for vertical refresh\n");
441 Monitor->vrefresh[Monitor->nVrefresh].lo =
442 DDC->det_mon[i].section.ranges.min_v;
443 Monitor->vrefresh[Monitor->nVrefresh].hi =
444 DDC->det_mon[i].section.ranges.max_v;
445 Monitor->nVrefresh++;
446 } else {
447 xf86DrvMsg(scrnIndex, X_INFO,
448 "Using vrefresh ranges from config file\n");
451 clock = DDC->det_mon[i].section.ranges.max_clock * 1000;
452 if (!have_maxpixclock && clock > Monitor->maxPixClock)
453 Monitor->maxPixClock = clock;
455 break;
456 default:
457 break;
461 if (Modes) {
462 /* Print Modes */
463 xf86DrvMsg(scrnIndex, X_INFO, "Printing DDC gathered Modelines:\n");
465 Mode = Modes;
466 while (Mode) {
467 xf86PrintModeline(scrnIndex, Mode);
468 Mode = Mode->next;
471 /* Do we still need ranges to be filled in? */
472 if (!Monitor->nHsync || !Monitor->nVrefresh)
473 DDCGuessRangesFromModes(scrnIndex, Monitor, Modes);
475 /* look for last Mode */
476 Mode = Modes;
478 while (Mode->next)
479 Mode = Mode->next;
481 /* add to MonPtr */
482 if (Monitor->Modes) {
483 Monitor->Last->next = Modes;
484 Modes->prev = Monitor->Last;
485 Monitor->Last = Mode;
486 } else {
487 Monitor->Modes = Modes;
488 Monitor->Last = Mode;