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
29 #include <linux/tty.h>
31 #include <linux/module.h>
32 #include <video/edid.h>
34 #include <linux/pci.h>
36 #include <asm/pci-bridge.h>
44 #undef DEBUG /* define this for verbose EDID parsing output */
47 #define DPRINTK(fmt, args...) printk(fmt,## args)
49 #define DPRINTK(fmt, args...)
52 #define FBMON_FIX_HEADER 1
53 #define FBMON_FIX_INPUT 2
55 #ifdef CONFIG_FB_MODE_HELPERS
62 static struct broken_edid brokendb
[] = {
65 .manufacturer
= "DEC",
67 .fix
= FBMON_FIX_HEADER
,
69 /* ViewSonic PF775a */
71 .manufacturer
= "VSC",
73 .fix
= FBMON_FIX_INPUT
,
77 static const unsigned char edid_v1_header
[] = { 0x00, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0x00
81 static void copy_string(unsigned char *c
, unsigned char *s
)
85 for (i
= 0; (i
< 13 && *c
!= 0x0A); i
++)
88 while (i
-- && (*--s
== 0x20)) *s
= 0;
91 static int check_edid(unsigned char *edid
)
93 unsigned char *block
= edid
+ ID_MANUFACTURER_NAME
, manufacturer
[4];
96 int i
, fix
= 0, ret
= 0;
98 manufacturer
[0] = ((block
[0] & 0x7c) >> 2) + '@';
99 manufacturer
[1] = ((block
[0] & 0x03) << 3) +
100 ((block
[1] & 0xe0) >> 5) + '@';
101 manufacturer
[2] = (block
[1] & 0x1f) + '@';
103 model
= block
[2] + (block
[3] << 8);
105 for (i
= 0; i
< ARRAY_SIZE(brokendb
); i
++) {
106 if (!strncmp(manufacturer
, brokendb
[i
].manufacturer
, 4) &&
107 brokendb
[i
].model
== model
) {
108 printk("fbmon: The EDID Block of "
109 "Manufacturer: %s Model: 0x%x is known to "
110 "be broken,\n", manufacturer
, model
);
111 fix
= brokendb
[i
].fix
;
117 case FBMON_FIX_HEADER
:
118 for (i
= 0; i
< 8; i
++) {
119 if (edid
[i
] != edid_v1_header
[i
])
123 case FBMON_FIX_INPUT
:
124 b
= edid
+ EDID_STRUCT_DISPLAY
;
125 /* Only if display is GTF capable will
126 the input type be reset to analog */
127 if (b
[4] & 0x01 && b
[0] & 0x80)
135 static void fix_edid(unsigned char *edid
, int fix
)
140 case FBMON_FIX_HEADER
:
141 printk("fbmon: trying a header reconstruct\n");
142 memcpy(edid
, edid_v1_header
, 8);
144 case FBMON_FIX_INPUT
:
145 printk("fbmon: trying to fix input type\n");
146 b
= edid
+ EDID_STRUCT_DISPLAY
;
152 static int edid_checksum(unsigned char *edid
)
154 unsigned char i
, csum
= 0, all_null
= 0;
155 int err
= 0, fix
= check_edid(edid
);
160 for (i
= 0; i
< EDID_LENGTH
; i
++) {
165 if (csum
== 0x00 && all_null
) {
166 /* checksum passed, everything's good */
173 static int edid_check_header(unsigned char *edid
)
175 int i
, err
= 1, fix
= check_edid(edid
);
180 for (i
= 0; i
< 8; i
++) {
181 if (edid
[i
] != edid_v1_header
[i
])
188 static void parse_vendor_block(unsigned char *block
, struct fb_monspecs
*specs
)
190 specs
->manufacturer
[0] = ((block
[0] & 0x7c) >> 2) + '@';
191 specs
->manufacturer
[1] = ((block
[0] & 0x03) << 3) +
192 ((block
[1] & 0xe0) >> 5) + '@';
193 specs
->manufacturer
[2] = (block
[1] & 0x1f) + '@';
194 specs
->manufacturer
[3] = 0;
195 specs
->model
= block
[2] + (block
[3] << 8);
196 specs
->serial
= block
[4] + (block
[5] << 8) +
197 (block
[6] << 16) + (block
[7] << 24);
198 specs
->year
= block
[9] + 1990;
199 specs
->week
= block
[8];
200 DPRINTK(" Manufacturer: %s\n", specs
->manufacturer
);
201 DPRINTK(" Model: %x\n", specs
->model
);
202 DPRINTK(" Serial#: %u\n", specs
->serial
);
203 DPRINTK(" Year: %u Week %u\n", specs
->year
, specs
->week
);
206 static void get_dpms_capabilities(unsigned char flags
,
207 struct fb_monspecs
*specs
)
210 if (flags
& DPMS_ACTIVE_OFF
)
211 specs
->dpms
|= FB_DPMS_ACTIVE_OFF
;
212 if (flags
& DPMS_SUSPEND
)
213 specs
->dpms
|= FB_DPMS_SUSPEND
;
214 if (flags
& DPMS_STANDBY
)
215 specs
->dpms
|= FB_DPMS_STANDBY
;
216 DPRINTK(" DPMS: Active %s, Suspend %s, Standby %s\n",
217 (flags
& DPMS_ACTIVE_OFF
) ? "yes" : "no",
218 (flags
& DPMS_SUSPEND
) ? "yes" : "no",
219 (flags
& DPMS_STANDBY
) ? "yes" : "no");
222 static void get_chroma(unsigned char *block
, struct fb_monspecs
*specs
)
226 DPRINTK(" Chroma\n");
227 /* Chromaticity data */
228 tmp
= ((block
[5] & (3 << 6)) >> 6) | (block
[0x7] << 2);
231 specs
->chroma
.redx
= tmp
/1024;
232 DPRINTK(" RedX: 0.%03d ", specs
->chroma
.redx
);
234 tmp
= ((block
[5] & (3 << 4)) >> 4) | (block
[0x8] << 2);
237 specs
->chroma
.redy
= tmp
/1024;
238 DPRINTK("RedY: 0.%03d\n", specs
->chroma
.redy
);
240 tmp
= ((block
[5] & (3 << 2)) >> 2) | (block
[0x9] << 2);
243 specs
->chroma
.greenx
= tmp
/1024;
244 DPRINTK(" GreenX: 0.%03d ", specs
->chroma
.greenx
);
246 tmp
= (block
[5] & 3) | (block
[0xa] << 2);
249 specs
->chroma
.greeny
= tmp
/1024;
250 DPRINTK("GreenY: 0.%03d\n", specs
->chroma
.greeny
);
252 tmp
= ((block
[6] & (3 << 6)) >> 6) | (block
[0xb] << 2);
255 specs
->chroma
.bluex
= tmp
/1024;
256 DPRINTK(" BlueX: 0.%03d ", specs
->chroma
.bluex
);
258 tmp
= ((block
[6] & (3 << 4)) >> 4) | (block
[0xc] << 2);
261 specs
->chroma
.bluey
= tmp
/1024;
262 DPRINTK("BlueY: 0.%03d\n", specs
->chroma
.bluey
);
264 tmp
= ((block
[6] & (3 << 2)) >> 2) | (block
[0xd] << 2);
267 specs
->chroma
.whitex
= tmp
/1024;
268 DPRINTK(" WhiteX: 0.%03d ", specs
->chroma
.whitex
);
270 tmp
= (block
[6] & 3) | (block
[0xe] << 2);
273 specs
->chroma
.whitey
= tmp
/1024;
274 DPRINTK("WhiteY: 0.%03d\n", specs
->chroma
.whitey
);
277 static int edid_is_serial_block(unsigned char *block
)
279 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
280 (block
[2] == 0x00) && (block
[3] == 0xff) &&
287 static int edid_is_ascii_block(unsigned char *block
)
289 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
290 (block
[2] == 0x00) && (block
[3] == 0xfe) &&
297 static int edid_is_limits_block(unsigned char *block
)
299 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
300 (block
[2] == 0x00) && (block
[3] == 0xfd) &&
307 static int edid_is_monitor_block(unsigned char *block
)
309 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
310 (block
[2] == 0x00) && (block
[3] == 0xfc) &&
317 static void calc_mode_timings(int xres
, int yres
, int refresh
,
318 struct fb_videomode
*mode
)
320 struct fb_var_screeninfo var
;
323 memset(&var
, 0, sizeof(struct fb_var_screeninfo
));
326 fb_get_mode(FB_VSYNCTIMINGS
| FB_IGNOREMON
,
327 refresh
, &var
, &info
);
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
;
342 static int get_est_timing(unsigned char *block
, struct fb_videomode
*mode
)
349 calc_mode_timings(720, 400, 70, &mode
[num
]);
350 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
351 DPRINTK(" 720x400@70Hz\n");
354 calc_mode_timings(720, 400, 88, &mode
[num
]);
355 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
356 DPRINTK(" 720x400@88Hz\n");
359 mode
[num
++] = vesa_modes
[3];
360 DPRINTK(" 640x480@60Hz\n");
363 calc_mode_timings(640, 480, 67, &mode
[num
]);
364 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
365 DPRINTK(" 640x480@67Hz\n");
368 mode
[num
++] = vesa_modes
[4];
369 DPRINTK(" 640x480@72Hz\n");
372 mode
[num
++] = vesa_modes
[5];
373 DPRINTK(" 640x480@75Hz\n");
376 mode
[num
++] = vesa_modes
[7];
377 DPRINTK(" 800x600@56Hz\n");
380 mode
[num
++] = vesa_modes
[8];
381 DPRINTK(" 800x600@60Hz\n");
386 mode
[num
++] = vesa_modes
[9];
387 DPRINTK(" 800x600@72Hz\n");
390 mode
[num
++] = vesa_modes
[10];
391 DPRINTK(" 800x600@75Hz\n");
394 calc_mode_timings(832, 624, 75, &mode
[num
]);
395 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
396 DPRINTK(" 832x624@75Hz\n");
399 mode
[num
++] = vesa_modes
[12];
400 DPRINTK(" 1024x768@87Hz Interlaced\n");
403 mode
[num
++] = vesa_modes
[13];
404 DPRINTK(" 1024x768@60Hz\n");
407 mode
[num
++] = vesa_modes
[14];
408 DPRINTK(" 1024x768@70Hz\n");
411 mode
[num
++] = vesa_modes
[15];
412 DPRINTK(" 1024x768@75Hz\n");
415 mode
[num
++] = vesa_modes
[21];
416 DPRINTK(" 1280x1024@75Hz\n");
420 mode
[num
++] = vesa_modes
[17];
421 DPRINTK(" 1152x870@75Hz\n");
423 DPRINTK(" Manufacturer's mask: %x\n",c
&0x7F);
427 static int get_std_timing(unsigned char *block
, struct fb_videomode
*mode
)
429 int xres
, yres
= 0, refresh
, ratio
, i
;
431 xres
= (block
[0] + 31) * 8;
435 ratio
= (block
[1] & 0xc0) >> 6;
447 yres
= (xres
* 9)/16;
450 refresh
= (block
[1] & 0x3f) + 60;
452 DPRINTK(" %dx%d@%dHz\n", xres
, yres
, refresh
);
453 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
454 if (vesa_modes
[i
].xres
== xres
&&
455 vesa_modes
[i
].yres
== yres
&&
456 vesa_modes
[i
].refresh
== refresh
) {
457 *mode
= vesa_modes
[i
];
458 mode
->flag
|= FB_MODE_IS_STANDARD
;
462 calc_mode_timings(xres
, yres
, refresh
, mode
);
466 static int get_dst_timing(unsigned char *block
,
467 struct fb_videomode
*mode
)
471 for (j
= 0; j
< 6; j
++, block
+= STD_TIMING_DESCRIPTION_SIZE
)
472 num
+= get_std_timing(block
, &mode
[num
]);
477 static void get_detailed_timing(unsigned char *block
,
478 struct fb_videomode
*mode
)
480 mode
->xres
= H_ACTIVE
;
481 mode
->yres
= V_ACTIVE
;
482 mode
->pixclock
= PIXEL_CLOCK
;
483 mode
->pixclock
/= 1000;
484 mode
->pixclock
= KHZ2PICOS(mode
->pixclock
);
485 mode
->right_margin
= H_SYNC_OFFSET
;
486 mode
->left_margin
= (H_ACTIVE
+ H_BLANKING
) -
487 (H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
);
488 mode
->upper_margin
= V_BLANKING
- V_SYNC_OFFSET
-
490 mode
->lower_margin
= V_SYNC_OFFSET
;
491 mode
->hsync_len
= H_SYNC_WIDTH
;
492 mode
->vsync_len
= V_SYNC_WIDTH
;
494 mode
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
496 mode
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
497 mode
->refresh
= PIXEL_CLOCK
/((H_ACTIVE
+ H_BLANKING
) *
498 (V_ACTIVE
+ V_BLANKING
));
500 mode
->flag
= FB_MODE_IS_DETAILED
;
502 DPRINTK(" %d MHz ", PIXEL_CLOCK
/1000000);
503 DPRINTK("%d %d %d %d ", H_ACTIVE
, H_ACTIVE
+ H_SYNC_OFFSET
,
504 H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
, H_ACTIVE
+ H_BLANKING
);
505 DPRINTK("%d %d %d %d ", V_ACTIVE
, V_ACTIVE
+ V_SYNC_OFFSET
,
506 V_ACTIVE
+ V_SYNC_OFFSET
+ V_SYNC_WIDTH
, V_ACTIVE
+ V_BLANKING
);
507 DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE
) ? "+" : "-",
508 (VSYNC_POSITIVE
) ? "+" : "-");
512 * fb_create_modedb - create video mode database
514 * @dbsize: database size
516 * RETURNS: struct fb_videomode, @dbsize contains length of database
519 * This function builds a mode database using the contents of the EDID
522 static struct fb_videomode
*fb_create_modedb(unsigned char *edid
, int *dbsize
)
524 struct fb_videomode
*mode
, *m
;
525 unsigned char *block
;
528 mode
= kmalloc(50 * sizeof(struct fb_videomode
), GFP_KERNEL
);
531 memset(mode
, 0, 50 * sizeof(struct fb_videomode
));
533 if (edid
== NULL
|| !edid_checksum(edid
) ||
534 !edid_check_header(edid
)) {
541 DPRINTK(" Supported VESA Modes\n");
542 block
= edid
+ ESTABLISHED_TIMING_1
;
543 num
+= get_est_timing(block
, &mode
[num
]);
545 DPRINTK(" Standard Timings\n");
546 block
= edid
+ STD_TIMING_DESCRIPTIONS_START
;
547 for (i
= 0; i
< STD_TIMING
; i
++, block
+= STD_TIMING_DESCRIPTION_SIZE
)
548 num
+= get_std_timing(block
, &mode
[num
]);
550 DPRINTK(" Detailed Timings\n");
551 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
552 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
555 if (block
[0] == 0x00 && block
[1] == 0x00) {
556 if (block
[3] == 0xfa) {
557 num
+= get_dst_timing(block
+ 5, &mode
[num
]);
560 get_detailed_timing(block
, &mode
[num
]);
562 mode
[num
].flag
|= FB_MODE_IS_FIRST
;
569 /* Yikes, EDID data is totally useless */
576 m
= kmalloc(num
* sizeof(struct fb_videomode
), GFP_KERNEL
);
579 memmove(m
, mode
, num
* sizeof(struct fb_videomode
));
585 * fb_destroy_modedb - destroys mode database
586 * @modedb: mode database to destroy
589 * Destroy mode database created by fb_create_modedb
591 void fb_destroy_modedb(struct fb_videomode
*modedb
)
596 static int fb_get_monitor_limits(unsigned char *edid
, struct fb_monspecs
*specs
)
599 unsigned char *block
;
601 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
603 DPRINTK(" Monitor Operating Limits: ");
604 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
605 if (edid_is_limits_block(block
)) {
606 specs
->hfmin
= H_MIN_RATE
* 1000;
607 specs
->hfmax
= H_MAX_RATE
* 1000;
608 specs
->vfmin
= V_MIN_RATE
;
609 specs
->vfmax
= V_MAX_RATE
;
610 specs
->dclkmax
= MAX_PIXEL_CLOCK
* 1000000;
611 specs
->gtf
= (GTF_SUPPORT
) ? 1 : 0;
613 DPRINTK("From EDID\n");
618 /* estimate monitor limits based on modes supported */
620 struct fb_videomode
*modes
;
621 int num_modes
, i
, hz
, hscan
, pixclock
;
623 modes
= fb_create_modedb(edid
, &num_modes
);
625 DPRINTK("None Available\n");
630 for (i
= 0; i
< num_modes
; i
++) {
631 hz
= modes
[i
].refresh
;
632 pixclock
= PICOS2KHZ(modes
[i
].pixclock
) * 1000;
633 hscan
= (modes
[i
].yres
* 105 * hz
+ 5000)/100;
635 if (specs
->dclkmax
== 0 || specs
->dclkmax
< pixclock
)
636 specs
->dclkmax
= pixclock
;
637 if (specs
->dclkmin
== 0 || specs
->dclkmin
> pixclock
)
638 specs
->dclkmin
= pixclock
;
639 if (specs
->hfmax
== 0 || specs
->hfmax
< hscan
)
640 specs
->hfmax
= hscan
;
641 if (specs
->hfmin
== 0 || specs
->hfmin
> hscan
)
642 specs
->hfmin
= hscan
;
643 if (specs
->vfmax
== 0 || specs
->vfmax
< hz
)
645 if (specs
->vfmin
== 0 || specs
->vfmin
> hz
)
648 DPRINTK("Extrapolated\n");
649 fb_destroy_modedb(modes
);
651 DPRINTK(" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
652 specs
->hfmin
/1000, specs
->hfmax
/1000, specs
->vfmin
,
653 specs
->vfmax
, specs
->dclkmax
/1000000);
657 static void get_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
659 unsigned char c
, *block
;
661 block
= edid
+ EDID_STRUCT_DISPLAY
;
663 fb_get_monitor_limits(edid
, specs
);
668 specs
->input
|= FB_DISP_DDI
;
669 DPRINTK(" Digital Display Input");
671 DPRINTK(" Analog Display Input: Input Voltage - ");
672 switch ((block
[0] & 0x60) >> 5) {
674 DPRINTK("0.700V/0.300V");
675 specs
->input
|= FB_DISP_ANA_700_300
;
678 DPRINTK("0.714V/0.286V");
679 specs
->input
|= FB_DISP_ANA_714_286
;
682 DPRINTK("1.000V/0.400V");
683 specs
->input
|= FB_DISP_ANA_1000_400
;
686 DPRINTK("0.700V/0.000V");
687 specs
->input
|= FB_DISP_ANA_700_000
;
691 DPRINTK("\n Sync: ");
694 DPRINTK(" Configurable signal level\n");
698 DPRINTK("Blank to Blank ");
699 specs
->signal
|= FB_SIGNAL_BLANK_BLANK
;
702 DPRINTK("Separate ");
703 specs
->signal
|= FB_SIGNAL_SEPARATE
;
706 DPRINTK("Composite ");
707 specs
->signal
|= FB_SIGNAL_COMPOSITE
;
710 DPRINTK("Sync on Green ");
711 specs
->signal
|= FB_SIGNAL_SYNC_ON_GREEN
;
714 DPRINTK("Serration on ");
715 specs
->signal
|= FB_SIGNAL_SERRATION_ON
;
718 specs
->max_x
= block
[1];
719 specs
->max_y
= block
[2];
720 DPRINTK(" Max H-size in cm: ");
722 DPRINTK("%d\n", specs
->max_x
);
724 DPRINTK("variable\n");
725 DPRINTK(" Max V-size in cm: ");
727 DPRINTK("%d\n", specs
->max_y
);
729 DPRINTK("variable\n");
732 specs
->gamma
= c
+100;
734 DPRINTK("%d.%d\n", specs
->gamma
/100, specs
->gamma
% 100);
736 get_dpms_capabilities(block
[4], specs
);
738 switch ((block
[4] & 0x18) >> 3) {
740 DPRINTK(" Monochrome/Grayscale\n");
741 specs
->input
|= FB_DISP_MONO
;
744 DPRINTK(" RGB Color Display\n");
745 specs
->input
|= FB_DISP_RGB
;
748 DPRINTK(" Non-RGB Multicolor Display\n");
749 specs
->input
|= FB_DISP_MULTI
;
752 DPRINTK(" Unknown\n");
753 specs
->input
|= FB_DISP_UNKNOWN
;
757 get_chroma(block
, specs
);
762 DPRINTK(" Default color format is primary\n");
763 specs
->misc
|= FB_MISC_PRIM_COLOR
;
766 DPRINTK(" First DETAILED Timing is preferred\n");
767 specs
->misc
|= FB_MISC_1ST_DETAIL
;
770 printk(" Display is GTF capable\n");
775 static int edid_is_timing_block(unsigned char *block
)
777 if ((block
[0] != 0x00) || (block
[1] != 0x00) ||
778 (block
[2] != 0x00) || (block
[4] != 0x00))
784 int fb_parse_edid(unsigned char *edid
, struct fb_var_screeninfo
*var
)
787 unsigned char *block
;
789 if (edid
== NULL
|| var
== NULL
)
792 if (!(edid_checksum(edid
)))
795 if (!(edid_check_header(edid
)))
798 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
800 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
801 if (edid_is_timing_block(block
)) {
802 var
->xres
= var
->xres_virtual
= H_ACTIVE
;
803 var
->yres
= var
->yres_virtual
= V_ACTIVE
;
804 var
->height
= var
->width
= -1;
805 var
->right_margin
= H_SYNC_OFFSET
;
806 var
->left_margin
= (H_ACTIVE
+ H_BLANKING
) -
807 (H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
);
808 var
->upper_margin
= V_BLANKING
- V_SYNC_OFFSET
-
810 var
->lower_margin
= V_SYNC_OFFSET
;
811 var
->hsync_len
= H_SYNC_WIDTH
;
812 var
->vsync_len
= V_SYNC_WIDTH
;
813 var
->pixclock
= PIXEL_CLOCK
;
814 var
->pixclock
/= 1000;
815 var
->pixclock
= KHZ2PICOS(var
->pixclock
);
818 var
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
820 var
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
827 void fb_edid_to_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
829 unsigned char *block
;
835 if (!(edid_checksum(edid
)))
838 if (!(edid_check_header(edid
)))
841 memset(specs
, 0, sizeof(struct fb_monspecs
));
843 specs
->version
= edid
[EDID_STRUCT_VERSION
];
844 specs
->revision
= edid
[EDID_STRUCT_REVISION
];
846 DPRINTK("========================================\n");
847 DPRINTK("Display Information (EDID)\n");
848 DPRINTK("========================================\n");
849 DPRINTK(" EDID Version %d.%d\n", (int) specs
->version
,
850 (int) specs
->revision
);
852 parse_vendor_block(edid
+ ID_MANUFACTURER_NAME
, specs
);
854 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
855 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
856 if (edid_is_serial_block(block
)) {
857 copy_string(block
, specs
->serial_no
);
858 DPRINTK(" Serial Number: %s\n", specs
->serial_no
);
859 } else if (edid_is_ascii_block(block
)) {
860 copy_string(block
, specs
->ascii
);
861 DPRINTK(" ASCII Block: %s\n", specs
->ascii
);
862 } else if (edid_is_monitor_block(block
)) {
863 copy_string(block
, specs
->monitor
);
864 DPRINTK(" Monitor Name: %s\n", specs
->monitor
);
868 DPRINTK(" Display Characteristics:\n");
869 get_monspecs(edid
, specs
);
871 specs
->modedb
= fb_create_modedb(edid
, &specs
->modedb_len
);
872 DPRINTK("========================================\n");
876 * VESA Generalized Timing Formula (GTF)
880 #define V_FRONTPORCH 1
882 #define H_SCALEFACTOR 20
883 #define H_BLANKSCALE 128
884 #define H_GRADIENT 600
888 struct __fb_timings
{
901 * fb_get_vblank - get vertical blank time
902 * @hfreq: horizontal freq
905 * vblank = right_margin + vsync_len + left_margin
907 * given: right_margin = 1 (V_FRONTPORCH)
912 * left_margin = --------------- - vsync_len
915 static u32
fb_get_vblank(u32 hfreq
)
919 vblank
= (hfreq
* FLYBACK
)/1000;
920 vblank
= (vblank
+ 500)/1000;
921 return (vblank
+ V_FRONTPORCH
);
925 * fb_get_hblank_by_freq - get horizontal blank time given hfreq
926 * @hfreq: horizontal freq
927 * @xres: horizontal resolution in pixels
932 * hblank = ------------------
935 * duty cycle = percent of htotal assigned to inactive display
936 * duty cycle = C - (M/Hfreq)
938 * where: C = ((offset - scale factor) * blank_scale)
939 * -------------------------------------- + scale factor
941 * M = blank_scale * gradient
944 static u32
fb_get_hblank_by_hfreq(u32 hfreq
, u32 xres
)
946 u32 c_val
, m_val
, duty_cycle
, hblank
;
948 c_val
= (((H_OFFSET
- H_SCALEFACTOR
) * H_BLANKSCALE
)/256 +
949 H_SCALEFACTOR
) * 1000;
950 m_val
= (H_BLANKSCALE
* H_GRADIENT
)/256;
951 m_val
= (m_val
* 1000000)/hfreq
;
952 duty_cycle
= c_val
- m_val
;
953 hblank
= (xres
* duty_cycle
)/(100000 - duty_cycle
);
958 * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
959 * @dclk: pixelclock in Hz
960 * @xres: horizontal resolution in pixels
965 * hblank = ------------------
968 * duty cycle = percent of htotal assigned to inactive display
969 * duty cycle = C - (M * h_period)
971 * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
972 * -----------------------------------------------
978 static u32
fb_get_hblank_by_dclk(u32 dclk
, u32 xres
)
980 u32 duty_cycle
, h_period
, hblank
;
983 h_period
= 100 - C_VAL
;
984 h_period
*= h_period
;
985 h_period
+= (M_VAL
* xres
* 2 * 1000)/(5 * dclk
);
988 h_period
= int_sqrt(h_period
);
989 h_period
-= (100 - C_VAL
) * 100;
991 h_period
/= 2 * M_VAL
;
993 duty_cycle
= C_VAL
* 1000 - (M_VAL
* h_period
)/100;
994 hblank
= (xres
* duty_cycle
)/(100000 - duty_cycle
) + 8;
1000 * fb_get_hfreq - estimate hsync
1001 * @vfreq: vertical refresh rate
1002 * @yres: vertical resolution
1006 * (yres + front_port) * vfreq * 1000000
1007 * hfreq = -------------------------------------
1008 * (1000000 - (vfreq * FLYBACK)
1012 static u32
fb_get_hfreq(u32 vfreq
, u32 yres
)
1016 divisor
= (1000000 - (vfreq
* FLYBACK
))/1000;
1017 hfreq
= (yres
+ V_FRONTPORCH
) * vfreq
* 1000;
1018 return (hfreq
/divisor
);
1021 static void fb_timings_vfreq(struct __fb_timings
*timings
)
1023 timings
->hfreq
= fb_get_hfreq(timings
->vfreq
, timings
->vactive
);
1024 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1025 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1026 timings
->hblank
= fb_get_hblank_by_hfreq(timings
->hfreq
,
1028 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1029 timings
->dclk
= timings
->htotal
* timings
->hfreq
;
1032 static void fb_timings_hfreq(struct __fb_timings
*timings
)
1034 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1035 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1036 timings
->vfreq
= timings
->hfreq
/timings
->vtotal
;
1037 timings
->hblank
= fb_get_hblank_by_hfreq(timings
->hfreq
,
1039 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1040 timings
->dclk
= timings
->htotal
* timings
->hfreq
;
1043 static void fb_timings_dclk(struct __fb_timings
*timings
)
1045 timings
->hblank
= fb_get_hblank_by_dclk(timings
->dclk
,
1047 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1048 timings
->hfreq
= timings
->dclk
/timings
->htotal
;
1049 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1050 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1051 timings
->vfreq
= timings
->hfreq
/timings
->vtotal
;
1055 * fb_get_mode - calculates video mode using VESA GTF
1056 * @flags: if: 0 - maximize vertical refresh rate
1057 * 1 - vrefresh-driven calculation;
1058 * 2 - hscan-driven calculation;
1059 * 3 - pixelclock-driven calculation;
1060 * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock
1061 * @var: pointer to fb_var_screeninfo
1062 * @info: pointer to fb_info
1065 * Calculates video mode based on monitor specs using VESA GTF.
1066 * The GTF is best for VESA GTF compliant monitors but is
1067 * specifically formulated to work for older monitors as well.
1069 * If @flag==0, the function will attempt to maximize the
1070 * refresh rate. Otherwise, it will calculate timings based on
1071 * the flag and accompanying value.
1073 * If FB_IGNOREMON bit is set in @flags, monitor specs will be
1074 * ignored and @var will be filled with the calculated timings.
1076 * All calculations are based on the VESA GTF Spreadsheet
1077 * available at VESA's public ftp (http://www.vesa.org).
1080 * The timings generated by the GTF will be different from VESA
1081 * DMT. It might be a good idea to keep a table of standard
1082 * VESA modes as well. The GTF may also not work for some displays,
1083 * such as, and especially, analog TV.
1086 * A valid info->monspecs, otherwise 'safe numbers' will be used.
1088 int fb_get_mode(int flags
, u32 val
, struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1090 struct __fb_timings timings
;
1091 u32 interlace
= 1, dscan
= 1;
1092 u32 hfmin
, hfmax
, vfmin
, vfmax
, dclkmin
, dclkmax
;
1095 * If monspecs are invalid, use values that are enough
1098 if (!info
->monspecs
.hfmax
|| !info
->monspecs
.vfmax
||
1099 !info
->monspecs
.dclkmax
||
1100 info
->monspecs
.hfmax
< info
->monspecs
.hfmin
||
1101 info
->monspecs
.vfmax
< info
->monspecs
.vfmin
||
1102 info
->monspecs
.dclkmax
< info
->monspecs
.dclkmin
) {
1103 hfmin
= 29000; hfmax
= 30000;
1104 vfmin
= 60; vfmax
= 60;
1105 dclkmin
= 0; dclkmax
= 25000000;
1107 hfmin
= info
->monspecs
.hfmin
;
1108 hfmax
= info
->monspecs
.hfmax
;
1109 vfmin
= info
->monspecs
.vfmin
;
1110 vfmax
= info
->monspecs
.vfmax
;
1111 dclkmin
= info
->monspecs
.dclkmin
;
1112 dclkmax
= info
->monspecs
.dclkmax
;
1115 memset(&timings
, 0, sizeof(struct __fb_timings
));
1116 timings
.hactive
= var
->xres
;
1117 timings
.vactive
= var
->yres
;
1118 if (var
->vmode
& FB_VMODE_INTERLACED
) {
1119 timings
.vactive
/= 2;
1122 if (var
->vmode
& FB_VMODE_DOUBLE
) {
1123 timings
.vactive
*= 2;
1127 switch (flags
& ~FB_IGNOREMON
) {
1128 case FB_MAXTIMINGS
: /* maximize refresh rate */
1129 timings
.hfreq
= hfmax
;
1130 fb_timings_hfreq(&timings
);
1131 if (timings
.vfreq
> vfmax
) {
1132 timings
.vfreq
= vfmax
;
1133 fb_timings_vfreq(&timings
);
1135 if (timings
.dclk
> dclkmax
) {
1136 timings
.dclk
= dclkmax
;
1137 fb_timings_dclk(&timings
);
1140 case FB_VSYNCTIMINGS
: /* vrefresh driven */
1141 timings
.vfreq
= val
;
1142 fb_timings_vfreq(&timings
);
1144 case FB_HSYNCTIMINGS
: /* hsync driven */
1145 timings
.hfreq
= val
;
1146 fb_timings_hfreq(&timings
);
1148 case FB_DCLKTIMINGS
: /* pixelclock driven */
1149 timings
.dclk
= PICOS2KHZ(val
) * 1000;
1150 fb_timings_dclk(&timings
);
1157 if (!(flags
& FB_IGNOREMON
) &&
1158 (timings
.vfreq
< vfmin
|| timings
.vfreq
> vfmax
||
1159 timings
.hfreq
< hfmin
|| timings
.hfreq
> hfmax
||
1160 timings
.dclk
< dclkmin
|| timings
.dclk
> dclkmax
))
1163 var
->pixclock
= KHZ2PICOS(timings
.dclk
/1000);
1164 var
->hsync_len
= (timings
.htotal
* 8)/100;
1165 var
->right_margin
= (timings
.hblank
/2) - var
->hsync_len
;
1166 var
->left_margin
= timings
.hblank
- var
->right_margin
- var
->hsync_len
;
1168 var
->vsync_len
= (3 * interlace
)/dscan
;
1169 var
->lower_margin
= (1 * interlace
)/dscan
;
1170 var
->upper_margin
= (timings
.vblank
* interlace
)/dscan
-
1171 (var
->vsync_len
+ var
->lower_margin
);
1176 int fb_parse_edid(unsigned char *edid
, struct fb_var_screeninfo
*var
)
1180 void fb_edid_to_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
1184 void fb_destroy_modedb(struct fb_videomode
*modedb
)
1187 int fb_get_mode(int flags
, u32 val
, struct fb_var_screeninfo
*var
,
1188 struct fb_info
*info
)
1192 #endif /* CONFIG_FB_MODE_HELPERS */
1195 * fb_validate_mode - validates var against monitor capabilities
1196 * @var: pointer to fb_var_screeninfo
1197 * @info: pointer to fb_info
1200 * Validates video mode against monitor capabilities specified in
1204 * A valid info->monspecs.
1206 int fb_validate_mode(const struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1208 u32 hfreq
, vfreq
, htotal
, vtotal
, pixclock
;
1209 u32 hfmin
, hfmax
, vfmin
, vfmax
, dclkmin
, dclkmax
;
1212 * If monspecs are invalid, use values that are enough
1215 if (!info
->monspecs
.hfmax
|| !info
->monspecs
.vfmax
||
1216 !info
->monspecs
.dclkmax
||
1217 info
->monspecs
.hfmax
< info
->monspecs
.hfmin
||
1218 info
->monspecs
.vfmax
< info
->monspecs
.vfmin
||
1219 info
->monspecs
.dclkmax
< info
->monspecs
.dclkmin
) {
1220 hfmin
= 29000; hfmax
= 30000;
1221 vfmin
= 60; vfmax
= 60;
1222 dclkmin
= 0; dclkmax
= 25000000;
1224 hfmin
= info
->monspecs
.hfmin
;
1225 hfmax
= info
->monspecs
.hfmax
;
1226 vfmin
= info
->monspecs
.vfmin
;
1227 vfmax
= info
->monspecs
.vfmax
;
1228 dclkmin
= info
->monspecs
.dclkmin
;
1229 dclkmax
= info
->monspecs
.dclkmax
;
1234 pixclock
= PICOS2KHZ(var
->pixclock
) * 1000;
1236 htotal
= var
->xres
+ var
->right_margin
+ var
->hsync_len
+
1238 vtotal
= var
->yres
+ var
->lower_margin
+ var
->vsync_len
+
1241 if (var
->vmode
& FB_VMODE_INTERLACED
)
1243 if (var
->vmode
& FB_VMODE_DOUBLE
)
1246 hfreq
= pixclock
/htotal
;
1247 hfreq
= (hfreq
+ 500) / 1000 * 1000;
1249 vfreq
= hfreq
/vtotal
;
1251 return (vfreq
< vfmin
|| vfreq
> vfmax
||
1252 hfreq
< hfmin
|| hfreq
> hfmax
||
1253 pixclock
< dclkmin
|| pixclock
> dclkmax
) ?
1257 #if defined(__i386__)
1258 #include <linux/pci.h>
1261 * We need to ensure that the EDID block is only returned for
1262 * the primary graphics adapter.
1265 const unsigned char *fb_firmware_edid(struct device
*device
)
1267 struct pci_dev
*dev
= NULL
;
1268 struct resource
*res
= NULL
;
1269 unsigned char *edid
= NULL
;
1272 dev
= to_pci_dev(device
);
1275 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
1277 if (res
&& res
->flags
& IORESOURCE_ROM_SHADOW
)
1278 edid
= edid_info
.dummy
;
1283 const unsigned char *fb_firmware_edid(struct device
*device
)
1289 EXPORT_SYMBOL(fb_parse_edid
);
1290 EXPORT_SYMBOL(fb_edid_to_monspecs
);
1291 EXPORT_SYMBOL(fb_firmware_edid
);
1292 EXPORT_SYMBOL(fb_get_mode
);
1293 EXPORT_SYMBOL(fb_validate_mode
);
1294 EXPORT_SYMBOL(fb_destroy_modedb
);