2 * linux/drivers/video/fbmon.c
4 * Copyright (C) 2002 James Simmons <jsimmons@users.sf.net>
8 * The EDID Parser is a conglomeration from the following sources:
10 * 1. SciTech SNAP Graphics Architecture
11 * Copyright (C) 1991-2002 SciTech Software, Inc. All rights reserved.
13 * 2. XFree86 4.3.0, interpret_edid.c
14 * Copyright 1998 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
16 * 3. John Fremlin <vii@users.sourceforge.net> and
17 * Ani Joshi <ajoshi@unixbox.com>
19 * Generalized Timing Formula is derived from:
21 * GTF Spreadsheet by Andy Morrish (1/5/97)
22 * available at http://www.vesa.org
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of this archive
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <video/edid.h>
35 #include <asm/pci-bridge.h>
43 #undef DEBUG /* define this for verbose EDID parsing output */
46 #define DPRINTK(fmt, args...) printk(fmt,## args)
48 #define DPRINTK(fmt, args...)
51 #define FBMON_FIX_HEADER 1
52 #define FBMON_FIX_INPUT 2
54 #ifdef CONFIG_FB_MODE_HELPERS
61 static const struct broken_edid brokendb
[] = {
64 .manufacturer
= "DEC",
66 .fix
= FBMON_FIX_HEADER
,
68 /* ViewSonic PF775a */
70 .manufacturer
= "VSC",
72 .fix
= FBMON_FIX_INPUT
,
76 static const unsigned char edid_v1_header
[] = { 0x00, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0x00
80 static void copy_string(unsigned char *c
, unsigned char *s
)
84 for (i
= 0; (i
< 13 && *c
!= 0x0A); i
++)
87 while (i
-- && (*--s
== 0x20)) *s
= 0;
90 static int check_edid(unsigned char *edid
)
92 unsigned char *block
= edid
+ ID_MANUFACTURER_NAME
, manufacturer
[4];
95 int i
, fix
= 0, ret
= 0;
97 manufacturer
[0] = ((block
[0] & 0x7c) >> 2) + '@';
98 manufacturer
[1] = ((block
[0] & 0x03) << 3) +
99 ((block
[1] & 0xe0) >> 5) + '@';
100 manufacturer
[2] = (block
[1] & 0x1f) + '@';
102 model
= block
[2] + (block
[3] << 8);
104 for (i
= 0; i
< ARRAY_SIZE(brokendb
); i
++) {
105 if (!strncmp(manufacturer
, brokendb
[i
].manufacturer
, 4) &&
106 brokendb
[i
].model
== model
) {
107 printk("fbmon: The EDID Block of "
108 "Manufacturer: %s Model: 0x%x is known to "
109 "be broken,\n", manufacturer
, model
);
110 fix
= brokendb
[i
].fix
;
116 case FBMON_FIX_HEADER
:
117 for (i
= 0; i
< 8; i
++) {
118 if (edid
[i
] != edid_v1_header
[i
])
122 case FBMON_FIX_INPUT
:
123 b
= edid
+ EDID_STRUCT_DISPLAY
;
124 /* Only if display is GTF capable will
125 the input type be reset to analog */
126 if (b
[4] & 0x01 && b
[0] & 0x80)
134 static void fix_edid(unsigned char *edid
, int fix
)
139 case FBMON_FIX_HEADER
:
140 printk("fbmon: trying a header reconstruct\n");
141 memcpy(edid
, edid_v1_header
, 8);
143 case FBMON_FIX_INPUT
:
144 printk("fbmon: trying to fix input type\n");
145 b
= edid
+ EDID_STRUCT_DISPLAY
;
151 static int edid_checksum(unsigned char *edid
)
153 unsigned char i
, csum
= 0, all_null
= 0;
154 int err
= 0, fix
= check_edid(edid
);
159 for (i
= 0; i
< EDID_LENGTH
; i
++) {
164 if (csum
== 0x00 && all_null
) {
165 /* checksum passed, everything's good */
172 static int edid_check_header(unsigned char *edid
)
174 int i
, err
= 1, fix
= check_edid(edid
);
179 for (i
= 0; i
< 8; i
++) {
180 if (edid
[i
] != edid_v1_header
[i
])
187 static void parse_vendor_block(unsigned char *block
, struct fb_monspecs
*specs
)
189 specs
->manufacturer
[0] = ((block
[0] & 0x7c) >> 2) + '@';
190 specs
->manufacturer
[1] = ((block
[0] & 0x03) << 3) +
191 ((block
[1] & 0xe0) >> 5) + '@';
192 specs
->manufacturer
[2] = (block
[1] & 0x1f) + '@';
193 specs
->manufacturer
[3] = 0;
194 specs
->model
= block
[2] + (block
[3] << 8);
195 specs
->serial
= block
[4] + (block
[5] << 8) +
196 (block
[6] << 16) + (block
[7] << 24);
197 specs
->year
= block
[9] + 1990;
198 specs
->week
= block
[8];
199 DPRINTK(" Manufacturer: %s\n", specs
->manufacturer
);
200 DPRINTK(" Model: %x\n", specs
->model
);
201 DPRINTK(" Serial#: %u\n", specs
->serial
);
202 DPRINTK(" Year: %u Week %u\n", specs
->year
, specs
->week
);
205 static void get_dpms_capabilities(unsigned char flags
,
206 struct fb_monspecs
*specs
)
209 if (flags
& DPMS_ACTIVE_OFF
)
210 specs
->dpms
|= FB_DPMS_ACTIVE_OFF
;
211 if (flags
& DPMS_SUSPEND
)
212 specs
->dpms
|= FB_DPMS_SUSPEND
;
213 if (flags
& DPMS_STANDBY
)
214 specs
->dpms
|= FB_DPMS_STANDBY
;
215 DPRINTK(" DPMS: Active %s, Suspend %s, Standby %s\n",
216 (flags
& DPMS_ACTIVE_OFF
) ? "yes" : "no",
217 (flags
& DPMS_SUSPEND
) ? "yes" : "no",
218 (flags
& DPMS_STANDBY
) ? "yes" : "no");
221 static void get_chroma(unsigned char *block
, struct fb_monspecs
*specs
)
225 DPRINTK(" Chroma\n");
226 /* Chromaticity data */
227 tmp
= ((block
[5] & (3 << 6)) >> 6) | (block
[0x7] << 2);
230 specs
->chroma
.redx
= tmp
/1024;
231 DPRINTK(" RedX: 0.%03d ", specs
->chroma
.redx
);
233 tmp
= ((block
[5] & (3 << 4)) >> 4) | (block
[0x8] << 2);
236 specs
->chroma
.redy
= tmp
/1024;
237 DPRINTK("RedY: 0.%03d\n", specs
->chroma
.redy
);
239 tmp
= ((block
[5] & (3 << 2)) >> 2) | (block
[0x9] << 2);
242 specs
->chroma
.greenx
= tmp
/1024;
243 DPRINTK(" GreenX: 0.%03d ", specs
->chroma
.greenx
);
245 tmp
= (block
[5] & 3) | (block
[0xa] << 2);
248 specs
->chroma
.greeny
= tmp
/1024;
249 DPRINTK("GreenY: 0.%03d\n", specs
->chroma
.greeny
);
251 tmp
= ((block
[6] & (3 << 6)) >> 6) | (block
[0xb] << 2);
254 specs
->chroma
.bluex
= tmp
/1024;
255 DPRINTK(" BlueX: 0.%03d ", specs
->chroma
.bluex
);
257 tmp
= ((block
[6] & (3 << 4)) >> 4) | (block
[0xc] << 2);
260 specs
->chroma
.bluey
= tmp
/1024;
261 DPRINTK("BlueY: 0.%03d\n", specs
->chroma
.bluey
);
263 tmp
= ((block
[6] & (3 << 2)) >> 2) | (block
[0xd] << 2);
266 specs
->chroma
.whitex
= tmp
/1024;
267 DPRINTK(" WhiteX: 0.%03d ", specs
->chroma
.whitex
);
269 tmp
= (block
[6] & 3) | (block
[0xe] << 2);
272 specs
->chroma
.whitey
= tmp
/1024;
273 DPRINTK("WhiteY: 0.%03d\n", specs
->chroma
.whitey
);
276 static int edid_is_serial_block(unsigned char *block
)
278 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
279 (block
[2] == 0x00) && (block
[3] == 0xff) &&
286 static int edid_is_ascii_block(unsigned char *block
)
288 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
289 (block
[2] == 0x00) && (block
[3] == 0xfe) &&
296 static int edid_is_limits_block(unsigned char *block
)
298 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
299 (block
[2] == 0x00) && (block
[3] == 0xfd) &&
306 static int edid_is_monitor_block(unsigned char *block
)
308 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
309 (block
[2] == 0x00) && (block
[3] == 0xfc) &&
316 static void calc_mode_timings(int xres
, int yres
, int refresh
,
317 struct fb_videomode
*mode
)
319 struct fb_var_screeninfo
*var
;
321 var
= kzalloc(sizeof(struct fb_var_screeninfo
), GFP_KERNEL
);
326 fb_get_mode(FB_VSYNCTIMINGS
| FB_IGNOREMON
,
330 mode
->pixclock
= var
->pixclock
;
331 mode
->refresh
= refresh
;
332 mode
->left_margin
= var
->left_margin
;
333 mode
->right_margin
= var
->right_margin
;
334 mode
->upper_margin
= var
->upper_margin
;
335 mode
->lower_margin
= var
->lower_margin
;
336 mode
->hsync_len
= var
->hsync_len
;
337 mode
->vsync_len
= var
->vsync_len
;
344 static int get_est_timing(unsigned char *block
, struct fb_videomode
*mode
)
351 calc_mode_timings(720, 400, 70, &mode
[num
]);
352 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
353 DPRINTK(" 720x400@70Hz\n");
356 calc_mode_timings(720, 400, 88, &mode
[num
]);
357 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
358 DPRINTK(" 720x400@88Hz\n");
361 mode
[num
++] = vesa_modes
[3];
362 DPRINTK(" 640x480@60Hz\n");
365 calc_mode_timings(640, 480, 67, &mode
[num
]);
366 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
367 DPRINTK(" 640x480@67Hz\n");
370 mode
[num
++] = vesa_modes
[4];
371 DPRINTK(" 640x480@72Hz\n");
374 mode
[num
++] = vesa_modes
[5];
375 DPRINTK(" 640x480@75Hz\n");
378 mode
[num
++] = vesa_modes
[7];
379 DPRINTK(" 800x600@56Hz\n");
382 mode
[num
++] = vesa_modes
[8];
383 DPRINTK(" 800x600@60Hz\n");
388 mode
[num
++] = vesa_modes
[9];
389 DPRINTK(" 800x600@72Hz\n");
392 mode
[num
++] = vesa_modes
[10];
393 DPRINTK(" 800x600@75Hz\n");
396 calc_mode_timings(832, 624, 75, &mode
[num
]);
397 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
398 DPRINTK(" 832x624@75Hz\n");
401 mode
[num
++] = vesa_modes
[12];
402 DPRINTK(" 1024x768@87Hz Interlaced\n");
405 mode
[num
++] = vesa_modes
[13];
406 DPRINTK(" 1024x768@60Hz\n");
409 mode
[num
++] = vesa_modes
[14];
410 DPRINTK(" 1024x768@70Hz\n");
413 mode
[num
++] = vesa_modes
[15];
414 DPRINTK(" 1024x768@75Hz\n");
417 mode
[num
++] = vesa_modes
[21];
418 DPRINTK(" 1280x1024@75Hz\n");
422 mode
[num
++] = vesa_modes
[17];
423 DPRINTK(" 1152x870@75Hz\n");
425 DPRINTK(" Manufacturer's mask: %x\n",c
&0x7F);
429 static int get_std_timing(unsigned char *block
, struct fb_videomode
*mode
)
431 int xres
, yres
= 0, refresh
, ratio
, i
;
433 xres
= (block
[0] + 31) * 8;
437 ratio
= (block
[1] & 0xc0) >> 6;
449 yres
= (xres
* 9)/16;
452 refresh
= (block
[1] & 0x3f) + 60;
454 DPRINTK(" %dx%d@%dHz\n", xres
, yres
, refresh
);
455 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
456 if (vesa_modes
[i
].xres
== xres
&&
457 vesa_modes
[i
].yres
== yres
&&
458 vesa_modes
[i
].refresh
== refresh
) {
459 *mode
= vesa_modes
[i
];
460 mode
->flag
|= FB_MODE_IS_STANDARD
;
464 calc_mode_timings(xres
, yres
, refresh
, mode
);
468 static int get_dst_timing(unsigned char *block
,
469 struct fb_videomode
*mode
)
473 for (j
= 0; j
< 6; j
++, block
+= STD_TIMING_DESCRIPTION_SIZE
)
474 num
+= get_std_timing(block
, &mode
[num
]);
479 static void get_detailed_timing(unsigned char *block
,
480 struct fb_videomode
*mode
)
482 mode
->xres
= H_ACTIVE
;
483 mode
->yres
= V_ACTIVE
;
484 mode
->pixclock
= PIXEL_CLOCK
;
485 mode
->pixclock
/= 1000;
486 mode
->pixclock
= KHZ2PICOS(mode
->pixclock
);
487 mode
->right_margin
= H_SYNC_OFFSET
;
488 mode
->left_margin
= (H_ACTIVE
+ H_BLANKING
) -
489 (H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
);
490 mode
->upper_margin
= V_BLANKING
- V_SYNC_OFFSET
-
492 mode
->lower_margin
= V_SYNC_OFFSET
;
493 mode
->hsync_len
= H_SYNC_WIDTH
;
494 mode
->vsync_len
= V_SYNC_WIDTH
;
496 mode
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
498 mode
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
499 mode
->refresh
= PIXEL_CLOCK
/((H_ACTIVE
+ H_BLANKING
) *
500 (V_ACTIVE
+ V_BLANKING
));
502 mode
->flag
= FB_MODE_IS_DETAILED
;
504 DPRINTK(" %d MHz ", PIXEL_CLOCK
/1000000);
505 DPRINTK("%d %d %d %d ", H_ACTIVE
, H_ACTIVE
+ H_SYNC_OFFSET
,
506 H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
, H_ACTIVE
+ H_BLANKING
);
507 DPRINTK("%d %d %d %d ", V_ACTIVE
, V_ACTIVE
+ V_SYNC_OFFSET
,
508 V_ACTIVE
+ V_SYNC_OFFSET
+ V_SYNC_WIDTH
, V_ACTIVE
+ V_BLANKING
);
509 DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE
) ? "+" : "-",
510 (VSYNC_POSITIVE
) ? "+" : "-");
514 * fb_create_modedb - create video mode database
516 * @dbsize: database size
518 * RETURNS: struct fb_videomode, @dbsize contains length of database
521 * This function builds a mode database using the contents of the EDID
524 static struct fb_videomode
*fb_create_modedb(unsigned char *edid
, int *dbsize
)
526 struct fb_videomode
*mode
, *m
;
527 unsigned char *block
;
530 mode
= kzalloc(50 * sizeof(struct fb_videomode
), GFP_KERNEL
);
534 if (edid
== NULL
|| !edid_checksum(edid
) ||
535 !edid_check_header(edid
)) {
542 DPRINTK(" Detailed Timings\n");
543 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
544 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
547 if (!(block
[0] == 0x00 && block
[1] == 0x00)) {
548 get_detailed_timing(block
, &mode
[num
]);
550 mode
[num
].flag
|= FB_MODE_IS_FIRST
;
557 DPRINTK(" Supported VESA Modes\n");
558 block
= edid
+ ESTABLISHED_TIMING_1
;
559 num
+= get_est_timing(block
, &mode
[num
]);
561 DPRINTK(" Standard Timings\n");
562 block
= edid
+ STD_TIMING_DESCRIPTIONS_START
;
563 for (i
= 0; i
< STD_TIMING
; i
++, block
+= STD_TIMING_DESCRIPTION_SIZE
)
564 num
+= get_std_timing(block
, &mode
[num
]);
566 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
567 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
568 if (block
[0] == 0x00 && block
[1] == 0x00 && block
[3] == 0xfa)
569 num
+= get_dst_timing(block
+ 5, &mode
[num
]);
572 /* Yikes, EDID data is totally useless */
579 m
= kmalloc(num
* sizeof(struct fb_videomode
), GFP_KERNEL
);
582 memmove(m
, mode
, num
* sizeof(struct fb_videomode
));
588 * fb_destroy_modedb - destroys mode database
589 * @modedb: mode database to destroy
592 * Destroy mode database created by fb_create_modedb
594 void fb_destroy_modedb(struct fb_videomode
*modedb
)
599 static int fb_get_monitor_limits(unsigned char *edid
, struct fb_monspecs
*specs
)
602 unsigned char *block
;
604 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
606 DPRINTK(" Monitor Operating Limits: ");
608 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
609 if (edid_is_limits_block(block
)) {
610 specs
->hfmin
= H_MIN_RATE
* 1000;
611 specs
->hfmax
= H_MAX_RATE
* 1000;
612 specs
->vfmin
= V_MIN_RATE
;
613 specs
->vfmax
= V_MAX_RATE
;
614 specs
->dclkmax
= MAX_PIXEL_CLOCK
* 1000000;
615 specs
->gtf
= (GTF_SUPPORT
) ? 1 : 0;
617 DPRINTK("From EDID\n");
622 /* estimate monitor limits based on modes supported */
624 struct fb_videomode
*modes
, *mode
;
625 int num_modes
, i
, hz
, hscan
, pixclock
;
628 modes
= fb_create_modedb(edid
, &num_modes
);
630 DPRINTK("None Available\n");
635 for (i
= 0; i
< num_modes
; i
++) {
637 pixclock
= PICOS2KHZ(modes
[i
].pixclock
) * 1000;
638 htotal
= mode
->xres
+ mode
->right_margin
+ mode
->hsync_len
640 vtotal
= mode
->yres
+ mode
->lower_margin
+ mode
->vsync_len
641 + mode
->upper_margin
;
643 if (mode
->vmode
& FB_VMODE_INTERLACED
)
646 if (mode
->vmode
& FB_VMODE_DOUBLE
)
649 hscan
= (pixclock
+ htotal
/ 2) / htotal
;
650 hscan
= (hscan
+ 500) / 1000 * 1000;
651 hz
= (hscan
+ vtotal
/ 2) / vtotal
;
653 if (specs
->dclkmax
== 0 || specs
->dclkmax
< pixclock
)
654 specs
->dclkmax
= pixclock
;
656 if (specs
->dclkmin
== 0 || specs
->dclkmin
> pixclock
)
657 specs
->dclkmin
= pixclock
;
659 if (specs
->hfmax
== 0 || specs
->hfmax
< hscan
)
660 specs
->hfmax
= hscan
;
662 if (specs
->hfmin
== 0 || specs
->hfmin
> hscan
)
663 specs
->hfmin
= hscan
;
665 if (specs
->vfmax
== 0 || specs
->vfmax
< hz
)
668 if (specs
->vfmin
== 0 || specs
->vfmin
> hz
)
671 DPRINTK("Extrapolated\n");
672 fb_destroy_modedb(modes
);
674 DPRINTK(" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
675 specs
->hfmin
/1000, specs
->hfmax
/1000, specs
->vfmin
,
676 specs
->vfmax
, specs
->dclkmax
/1000000);
680 static void get_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
682 unsigned char c
, *block
;
684 block
= edid
+ EDID_STRUCT_DISPLAY
;
686 fb_get_monitor_limits(edid
, specs
);
691 specs
->input
|= FB_DISP_DDI
;
692 DPRINTK(" Digital Display Input");
694 DPRINTK(" Analog Display Input: Input Voltage - ");
695 switch ((block
[0] & 0x60) >> 5) {
697 DPRINTK("0.700V/0.300V");
698 specs
->input
|= FB_DISP_ANA_700_300
;
701 DPRINTK("0.714V/0.286V");
702 specs
->input
|= FB_DISP_ANA_714_286
;
705 DPRINTK("1.000V/0.400V");
706 specs
->input
|= FB_DISP_ANA_1000_400
;
709 DPRINTK("0.700V/0.000V");
710 specs
->input
|= FB_DISP_ANA_700_000
;
714 DPRINTK("\n Sync: ");
717 DPRINTK(" Configurable signal level\n");
721 DPRINTK("Blank to Blank ");
722 specs
->signal
|= FB_SIGNAL_BLANK_BLANK
;
725 DPRINTK("Separate ");
726 specs
->signal
|= FB_SIGNAL_SEPARATE
;
729 DPRINTK("Composite ");
730 specs
->signal
|= FB_SIGNAL_COMPOSITE
;
733 DPRINTK("Sync on Green ");
734 specs
->signal
|= FB_SIGNAL_SYNC_ON_GREEN
;
737 DPRINTK("Serration on ");
738 specs
->signal
|= FB_SIGNAL_SERRATION_ON
;
741 specs
->max_x
= block
[1];
742 specs
->max_y
= block
[2];
743 DPRINTK(" Max H-size in cm: ");
745 DPRINTK("%d\n", specs
->max_x
);
747 DPRINTK("variable\n");
748 DPRINTK(" Max V-size in cm: ");
750 DPRINTK("%d\n", specs
->max_y
);
752 DPRINTK("variable\n");
755 specs
->gamma
= c
+100;
757 DPRINTK("%d.%d\n", specs
->gamma
/100, specs
->gamma
% 100);
759 get_dpms_capabilities(block
[4], specs
);
761 switch ((block
[4] & 0x18) >> 3) {
763 DPRINTK(" Monochrome/Grayscale\n");
764 specs
->input
|= FB_DISP_MONO
;
767 DPRINTK(" RGB Color Display\n");
768 specs
->input
|= FB_DISP_RGB
;
771 DPRINTK(" Non-RGB Multicolor Display\n");
772 specs
->input
|= FB_DISP_MULTI
;
775 DPRINTK(" Unknown\n");
776 specs
->input
|= FB_DISP_UNKNOWN
;
780 get_chroma(block
, specs
);
785 DPRINTK(" Default color format is primary\n");
786 specs
->misc
|= FB_MISC_PRIM_COLOR
;
789 DPRINTK(" First DETAILED Timing is preferred\n");
790 specs
->misc
|= FB_MISC_1ST_DETAIL
;
793 printk(" Display is GTF capable\n");
798 static int edid_is_timing_block(unsigned char *block
)
800 if ((block
[0] != 0x00) || (block
[1] != 0x00) ||
801 (block
[2] != 0x00) || (block
[4] != 0x00))
807 int fb_parse_edid(unsigned char *edid
, struct fb_var_screeninfo
*var
)
810 unsigned char *block
;
812 if (edid
== NULL
|| var
== NULL
)
815 if (!(edid_checksum(edid
)))
818 if (!(edid_check_header(edid
)))
821 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
823 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
824 if (edid_is_timing_block(block
)) {
825 var
->xres
= var
->xres_virtual
= H_ACTIVE
;
826 var
->yres
= var
->yres_virtual
= V_ACTIVE
;
827 var
->height
= var
->width
= -1;
828 var
->right_margin
= H_SYNC_OFFSET
;
829 var
->left_margin
= (H_ACTIVE
+ H_BLANKING
) -
830 (H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
);
831 var
->upper_margin
= V_BLANKING
- V_SYNC_OFFSET
-
833 var
->lower_margin
= V_SYNC_OFFSET
;
834 var
->hsync_len
= H_SYNC_WIDTH
;
835 var
->vsync_len
= V_SYNC_WIDTH
;
836 var
->pixclock
= PIXEL_CLOCK
;
837 var
->pixclock
/= 1000;
838 var
->pixclock
= KHZ2PICOS(var
->pixclock
);
841 var
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
843 var
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
850 void fb_edid_to_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
852 unsigned char *block
;
858 if (!(edid_checksum(edid
)))
861 if (!(edid_check_header(edid
)))
864 memset(specs
, 0, sizeof(struct fb_monspecs
));
866 specs
->version
= edid
[EDID_STRUCT_VERSION
];
867 specs
->revision
= edid
[EDID_STRUCT_REVISION
];
869 DPRINTK("========================================\n");
870 DPRINTK("Display Information (EDID)\n");
871 DPRINTK("========================================\n");
872 DPRINTK(" EDID Version %d.%d\n", (int) specs
->version
,
873 (int) specs
->revision
);
875 parse_vendor_block(edid
+ ID_MANUFACTURER_NAME
, specs
);
877 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
878 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
879 if (edid_is_serial_block(block
)) {
880 copy_string(block
, specs
->serial_no
);
881 DPRINTK(" Serial Number: %s\n", specs
->serial_no
);
882 } else if (edid_is_ascii_block(block
)) {
883 copy_string(block
, specs
->ascii
);
884 DPRINTK(" ASCII Block: %s\n", specs
->ascii
);
885 } else if (edid_is_monitor_block(block
)) {
886 copy_string(block
, specs
->monitor
);
887 DPRINTK(" Monitor Name: %s\n", specs
->monitor
);
891 DPRINTK(" Display Characteristics:\n");
892 get_monspecs(edid
, specs
);
894 specs
->modedb
= fb_create_modedb(edid
, &specs
->modedb_len
);
897 * Workaround for buggy EDIDs that sets that the first
898 * detailed timing is preferred but has not detailed
901 for (i
= 0; i
< specs
->modedb_len
; i
++) {
902 if (specs
->modedb
[i
].flag
& FB_MODE_IS_DETAILED
) {
909 specs
->misc
&= ~FB_MISC_1ST_DETAIL
;
911 DPRINTK("========================================\n");
915 * VESA Generalized Timing Formula (GTF)
919 #define V_FRONTPORCH 1
921 #define H_SCALEFACTOR 20
922 #define H_BLANKSCALE 128
923 #define H_GRADIENT 600
927 struct __fb_timings
{
940 * fb_get_vblank - get vertical blank time
941 * @hfreq: horizontal freq
944 * vblank = right_margin + vsync_len + left_margin
946 * given: right_margin = 1 (V_FRONTPORCH)
951 * left_margin = --------------- - vsync_len
954 static u32
fb_get_vblank(u32 hfreq
)
958 vblank
= (hfreq
* FLYBACK
)/1000;
959 vblank
= (vblank
+ 500)/1000;
960 return (vblank
+ V_FRONTPORCH
);
964 * fb_get_hblank_by_freq - get horizontal blank time given hfreq
965 * @hfreq: horizontal freq
966 * @xres: horizontal resolution in pixels
971 * hblank = ------------------
974 * duty cycle = percent of htotal assigned to inactive display
975 * duty cycle = C - (M/Hfreq)
977 * where: C = ((offset - scale factor) * blank_scale)
978 * -------------------------------------- + scale factor
980 * M = blank_scale * gradient
983 static u32
fb_get_hblank_by_hfreq(u32 hfreq
, u32 xres
)
985 u32 c_val
, m_val
, duty_cycle
, hblank
;
987 c_val
= (((H_OFFSET
- H_SCALEFACTOR
) * H_BLANKSCALE
)/256 +
988 H_SCALEFACTOR
) * 1000;
989 m_val
= (H_BLANKSCALE
* H_GRADIENT
)/256;
990 m_val
= (m_val
* 1000000)/hfreq
;
991 duty_cycle
= c_val
- m_val
;
992 hblank
= (xres
* duty_cycle
)/(100000 - duty_cycle
);
997 * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
998 * @dclk: pixelclock in Hz
999 * @xres: horizontal resolution in pixels
1004 * hblank = ------------------
1007 * duty cycle = percent of htotal assigned to inactive display
1008 * duty cycle = C - (M * h_period)
1010 * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
1011 * -----------------------------------------------
1017 static u32
fb_get_hblank_by_dclk(u32 dclk
, u32 xres
)
1019 u32 duty_cycle
, h_period
, hblank
;
1022 h_period
= 100 - C_VAL
;
1023 h_period
*= h_period
;
1024 h_period
+= (M_VAL
* xres
* 2 * 1000)/(5 * dclk
);
1027 h_period
= int_sqrt(h_period
);
1028 h_period
-= (100 - C_VAL
) * 100;
1030 h_period
/= 2 * M_VAL
;
1032 duty_cycle
= C_VAL
* 1000 - (M_VAL
* h_period
)/100;
1033 hblank
= (xres
* duty_cycle
)/(100000 - duty_cycle
) + 8;
1039 * fb_get_hfreq - estimate hsync
1040 * @vfreq: vertical refresh rate
1041 * @yres: vertical resolution
1045 * (yres + front_port) * vfreq * 1000000
1046 * hfreq = -------------------------------------
1047 * (1000000 - (vfreq * FLYBACK)
1051 static u32
fb_get_hfreq(u32 vfreq
, u32 yres
)
1055 divisor
= (1000000 - (vfreq
* FLYBACK
))/1000;
1056 hfreq
= (yres
+ V_FRONTPORCH
) * vfreq
* 1000;
1057 return (hfreq
/divisor
);
1060 static void fb_timings_vfreq(struct __fb_timings
*timings
)
1062 timings
->hfreq
= fb_get_hfreq(timings
->vfreq
, timings
->vactive
);
1063 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1064 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1065 timings
->hblank
= fb_get_hblank_by_hfreq(timings
->hfreq
,
1067 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1068 timings
->dclk
= timings
->htotal
* timings
->hfreq
;
1071 static void fb_timings_hfreq(struct __fb_timings
*timings
)
1073 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1074 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1075 timings
->vfreq
= timings
->hfreq
/timings
->vtotal
;
1076 timings
->hblank
= fb_get_hblank_by_hfreq(timings
->hfreq
,
1078 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1079 timings
->dclk
= timings
->htotal
* timings
->hfreq
;
1082 static void fb_timings_dclk(struct __fb_timings
*timings
)
1084 timings
->hblank
= fb_get_hblank_by_dclk(timings
->dclk
,
1086 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1087 timings
->hfreq
= timings
->dclk
/timings
->htotal
;
1088 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1089 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1090 timings
->vfreq
= timings
->hfreq
/timings
->vtotal
;
1094 * fb_get_mode - calculates video mode using VESA GTF
1095 * @flags: if: 0 - maximize vertical refresh rate
1096 * 1 - vrefresh-driven calculation;
1097 * 2 - hscan-driven calculation;
1098 * 3 - pixelclock-driven calculation;
1099 * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock
1100 * @var: pointer to fb_var_screeninfo
1101 * @info: pointer to fb_info
1104 * Calculates video mode based on monitor specs using VESA GTF.
1105 * The GTF is best for VESA GTF compliant monitors but is
1106 * specifically formulated to work for older monitors as well.
1108 * If @flag==0, the function will attempt to maximize the
1109 * refresh rate. Otherwise, it will calculate timings based on
1110 * the flag and accompanying value.
1112 * If FB_IGNOREMON bit is set in @flags, monitor specs will be
1113 * ignored and @var will be filled with the calculated timings.
1115 * All calculations are based on the VESA GTF Spreadsheet
1116 * available at VESA's public ftp (http://www.vesa.org).
1119 * The timings generated by the GTF will be different from VESA
1120 * DMT. It might be a good idea to keep a table of standard
1121 * VESA modes as well. The GTF may also not work for some displays,
1122 * such as, and especially, analog TV.
1125 * A valid info->monspecs, otherwise 'safe numbers' will be used.
1127 int fb_get_mode(int flags
, u32 val
, struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1129 struct __fb_timings
*timings
;
1130 u32 interlace
= 1, dscan
= 1;
1131 u32 hfmin
, hfmax
, vfmin
, vfmax
, dclkmin
, dclkmax
, err
= 0;
1134 timings
= kzalloc(sizeof(struct __fb_timings
), GFP_KERNEL
);
1140 * If monspecs are invalid, use values that are enough
1143 if (!info
|| !info
->monspecs
.hfmax
|| !info
->monspecs
.vfmax
||
1144 !info
->monspecs
.dclkmax
||
1145 info
->monspecs
.hfmax
< info
->monspecs
.hfmin
||
1146 info
->monspecs
.vfmax
< info
->monspecs
.vfmin
||
1147 info
->monspecs
.dclkmax
< info
->monspecs
.dclkmin
) {
1148 hfmin
= 29000; hfmax
= 30000;
1149 vfmin
= 60; vfmax
= 60;
1150 dclkmin
= 0; dclkmax
= 25000000;
1152 hfmin
= info
->monspecs
.hfmin
;
1153 hfmax
= info
->monspecs
.hfmax
;
1154 vfmin
= info
->monspecs
.vfmin
;
1155 vfmax
= info
->monspecs
.vfmax
;
1156 dclkmin
= info
->monspecs
.dclkmin
;
1157 dclkmax
= info
->monspecs
.dclkmax
;
1160 timings
->hactive
= var
->xres
;
1161 timings
->vactive
= var
->yres
;
1162 if (var
->vmode
& FB_VMODE_INTERLACED
) {
1163 timings
->vactive
/= 2;
1166 if (var
->vmode
& FB_VMODE_DOUBLE
) {
1167 timings
->vactive
*= 2;
1171 switch (flags
& ~FB_IGNOREMON
) {
1172 case FB_MAXTIMINGS
: /* maximize refresh rate */
1173 timings
->hfreq
= hfmax
;
1174 fb_timings_hfreq(timings
);
1175 if (timings
->vfreq
> vfmax
) {
1176 timings
->vfreq
= vfmax
;
1177 fb_timings_vfreq(timings
);
1179 if (timings
->dclk
> dclkmax
) {
1180 timings
->dclk
= dclkmax
;
1181 fb_timings_dclk(timings
);
1184 case FB_VSYNCTIMINGS
: /* vrefresh driven */
1185 timings
->vfreq
= val
;
1186 fb_timings_vfreq(timings
);
1188 case FB_HSYNCTIMINGS
: /* hsync driven */
1189 timings
->hfreq
= val
;
1190 fb_timings_hfreq(timings
);
1192 case FB_DCLKTIMINGS
: /* pixelclock driven */
1193 timings
->dclk
= PICOS2KHZ(val
) * 1000;
1194 fb_timings_dclk(timings
);
1201 if (err
|| (!(flags
& FB_IGNOREMON
) &&
1202 (timings
->vfreq
< vfmin
|| timings
->vfreq
> vfmax
||
1203 timings
->hfreq
< hfmin
|| timings
->hfreq
> hfmax
||
1204 timings
->dclk
< dclkmin
|| timings
->dclk
> dclkmax
))) {
1207 var
->pixclock
= KHZ2PICOS(timings
->dclk
/1000);
1208 var
->hsync_len
= (timings
->htotal
* 8)/100;
1209 var
->right_margin
= (timings
->hblank
/2) - var
->hsync_len
;
1210 var
->left_margin
= timings
->hblank
- var
->right_margin
-
1212 var
->vsync_len
= (3 * interlace
)/dscan
;
1213 var
->lower_margin
= (1 * interlace
)/dscan
;
1214 var
->upper_margin
= (timings
->vblank
* interlace
)/dscan
-
1215 (var
->vsync_len
+ var
->lower_margin
);
1222 int fb_parse_edid(unsigned char *edid
, struct fb_var_screeninfo
*var
)
1226 void fb_edid_to_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
1230 void fb_destroy_modedb(struct fb_videomode
*modedb
)
1233 int fb_get_mode(int flags
, u32 val
, struct fb_var_screeninfo
*var
,
1234 struct fb_info
*info
)
1238 #endif /* CONFIG_FB_MODE_HELPERS */
1241 * fb_validate_mode - validates var against monitor capabilities
1242 * @var: pointer to fb_var_screeninfo
1243 * @info: pointer to fb_info
1246 * Validates video mode against monitor capabilities specified in
1250 * A valid info->monspecs.
1252 int fb_validate_mode(const struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1254 u32 hfreq
, vfreq
, htotal
, vtotal
, pixclock
;
1255 u32 hfmin
, hfmax
, vfmin
, vfmax
, dclkmin
, dclkmax
;
1258 * If monspecs are invalid, use values that are enough
1261 if (!info
->monspecs
.hfmax
|| !info
->monspecs
.vfmax
||
1262 !info
->monspecs
.dclkmax
||
1263 info
->monspecs
.hfmax
< info
->monspecs
.hfmin
||
1264 info
->monspecs
.vfmax
< info
->monspecs
.vfmin
||
1265 info
->monspecs
.dclkmax
< info
->monspecs
.dclkmin
) {
1266 hfmin
= 29000; hfmax
= 30000;
1267 vfmin
= 60; vfmax
= 60;
1268 dclkmin
= 0; dclkmax
= 25000000;
1270 hfmin
= info
->monspecs
.hfmin
;
1271 hfmax
= info
->monspecs
.hfmax
;
1272 vfmin
= info
->monspecs
.vfmin
;
1273 vfmax
= info
->monspecs
.vfmax
;
1274 dclkmin
= info
->monspecs
.dclkmin
;
1275 dclkmax
= info
->monspecs
.dclkmax
;
1280 pixclock
= PICOS2KHZ(var
->pixclock
) * 1000;
1282 htotal
= var
->xres
+ var
->right_margin
+ var
->hsync_len
+
1284 vtotal
= var
->yres
+ var
->lower_margin
+ var
->vsync_len
+
1287 if (var
->vmode
& FB_VMODE_INTERLACED
)
1289 if (var
->vmode
& FB_VMODE_DOUBLE
)
1292 hfreq
= pixclock
/htotal
;
1293 hfreq
= (hfreq
+ 500) / 1000 * 1000;
1295 vfreq
= hfreq
/vtotal
;
1297 return (vfreq
< vfmin
|| vfreq
> vfmax
||
1298 hfreq
< hfmin
|| hfreq
> hfmax
||
1299 pixclock
< dclkmin
|| pixclock
> dclkmax
) ?
1303 #if defined(CONFIG_FIRMWARE_EDID) && defined(CONFIG_X86)
1306 * We need to ensure that the EDID block is only returned for
1307 * the primary graphics adapter.
1310 const unsigned char *fb_firmware_edid(struct device
*device
)
1312 struct pci_dev
*dev
= NULL
;
1313 struct resource
*res
= NULL
;
1314 unsigned char *edid
= NULL
;
1317 dev
= to_pci_dev(device
);
1320 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
1322 if (res
&& res
->flags
& IORESOURCE_ROM_SHADOW
)
1323 edid
= edid_info
.dummy
;
1328 const unsigned char *fb_firmware_edid(struct device
*device
)
1333 EXPORT_SYMBOL(fb_firmware_edid
);
1335 EXPORT_SYMBOL(fb_parse_edid
);
1336 EXPORT_SYMBOL(fb_edid_to_monspecs
);
1337 EXPORT_SYMBOL(fb_get_mode
);
1338 EXPORT_SYMBOL(fb_validate_mode
);
1339 EXPORT_SYMBOL(fb_destroy_modedb
);