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 <linux/slab.h>
33 #include <video/edid.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
54 #define FBMON_FIX_TIMINGS 3
56 #ifdef CONFIG_FB_MODE_HELPERS
63 static const struct broken_edid brokendb
[] = {
66 .manufacturer
= "DEC",
68 .fix
= FBMON_FIX_HEADER
,
70 /* ViewSonic PF775a */
72 .manufacturer
= "VSC",
74 .fix
= FBMON_FIX_INPUT
,
78 .manufacturer
= "SHP",
80 .fix
= FBMON_FIX_TIMINGS
,
84 static const unsigned char edid_v1_header
[] = { 0x00, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0x00
88 static void copy_string(unsigned char *c
, unsigned char *s
)
92 for (i
= 0; (i
< 13 && *c
!= 0x0A); i
++)
95 while (i
-- && (*--s
== 0x20)) *s
= 0;
98 static int edid_is_serial_block(unsigned char *block
)
100 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
101 (block
[2] == 0x00) && (block
[3] == 0xff) &&
108 static int edid_is_ascii_block(unsigned char *block
)
110 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
111 (block
[2] == 0x00) && (block
[3] == 0xfe) &&
118 static int edid_is_limits_block(unsigned char *block
)
120 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
121 (block
[2] == 0x00) && (block
[3] == 0xfd) &&
128 static int edid_is_monitor_block(unsigned char *block
)
130 if ((block
[0] == 0x00) && (block
[1] == 0x00) &&
131 (block
[2] == 0x00) && (block
[3] == 0xfc) &&
138 static int edid_is_timing_block(unsigned char *block
)
140 if ((block
[0] != 0x00) || (block
[1] != 0x00) ||
141 (block
[2] != 0x00) || (block
[4] != 0x00))
147 static int check_edid(unsigned char *edid
)
149 unsigned char *block
= edid
+ ID_MANUFACTURER_NAME
, manufacturer
[4];
152 int i
, fix
= 0, ret
= 0;
154 manufacturer
[0] = ((block
[0] & 0x7c) >> 2) + '@';
155 manufacturer
[1] = ((block
[0] & 0x03) << 3) +
156 ((block
[1] & 0xe0) >> 5) + '@';
157 manufacturer
[2] = (block
[1] & 0x1f) + '@';
159 model
= block
[2] + (block
[3] << 8);
161 for (i
= 0; i
< ARRAY_SIZE(brokendb
); i
++) {
162 if (!strncmp(manufacturer
, brokendb
[i
].manufacturer
, 4) &&
163 brokendb
[i
].model
== model
) {
164 fix
= brokendb
[i
].fix
;
170 case FBMON_FIX_HEADER
:
171 for (i
= 0; i
< 8; i
++) {
172 if (edid
[i
] != edid_v1_header
[i
]) {
178 case FBMON_FIX_INPUT
:
179 b
= edid
+ EDID_STRUCT_DISPLAY
;
180 /* Only if display is GTF capable will
181 the input type be reset to analog */
182 if (b
[4] & 0x01 && b
[0] & 0x80)
185 case FBMON_FIX_TIMINGS
:
186 b
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
189 for (i
= 0; i
< 4; i
++) {
190 if (edid_is_limits_block(b
)) {
195 b
+= DETAILED_TIMING_DESCRIPTION_SIZE
;
202 printk("fbmon: The EDID Block of "
203 "Manufacturer: %s Model: 0x%x is known to "
204 "be broken,\n", manufacturer
, model
);
209 static void fix_edid(unsigned char *edid
, int fix
)
212 unsigned char *b
, csum
= 0;
215 case FBMON_FIX_HEADER
:
216 printk("fbmon: trying a header reconstruct\n");
217 memcpy(edid
, edid_v1_header
, 8);
219 case FBMON_FIX_INPUT
:
220 printk("fbmon: trying to fix input type\n");
221 b
= edid
+ EDID_STRUCT_DISPLAY
;
225 case FBMON_FIX_TIMINGS
:
226 printk("fbmon: trying to fix monitor timings\n");
227 b
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
228 for (i
= 0; i
< 4; i
++) {
229 if (!(edid_is_serial_block(b
) ||
230 edid_is_ascii_block(b
) ||
231 edid_is_monitor_block(b
) ||
232 edid_is_timing_block(b
))) {
238 b
[5] = 60; /* vfmin */
239 b
[6] = 60; /* vfmax */
240 b
[7] = 30; /* hfmin */
241 b
[8] = 75; /* hfmax */
242 b
[9] = 17; /* pixclock - 170 MHz*/
247 b
+= DETAILED_TIMING_DESCRIPTION_SIZE
;
250 for (i
= 0; i
< EDID_LENGTH
- 1; i
++)
253 edid
[127] = 256 - csum
;
258 static int edid_checksum(unsigned char *edid
)
260 unsigned char csum
= 0, all_null
= 0;
261 int i
, err
= 0, fix
= check_edid(edid
);
266 for (i
= 0; i
< EDID_LENGTH
; i
++) {
271 if (csum
== 0x00 && all_null
) {
272 /* checksum passed, everything's good */
279 static int edid_check_header(unsigned char *edid
)
281 int i
, err
= 1, fix
= check_edid(edid
);
286 for (i
= 0; i
< 8; i
++) {
287 if (edid
[i
] != edid_v1_header
[i
])
294 static void parse_vendor_block(unsigned char *block
, struct fb_monspecs
*specs
)
296 specs
->manufacturer
[0] = ((block
[0] & 0x7c) >> 2) + '@';
297 specs
->manufacturer
[1] = ((block
[0] & 0x03) << 3) +
298 ((block
[1] & 0xe0) >> 5) + '@';
299 specs
->manufacturer
[2] = (block
[1] & 0x1f) + '@';
300 specs
->manufacturer
[3] = 0;
301 specs
->model
= block
[2] + (block
[3] << 8);
302 specs
->serial
= block
[4] + (block
[5] << 8) +
303 (block
[6] << 16) + (block
[7] << 24);
304 specs
->year
= block
[9] + 1990;
305 specs
->week
= block
[8];
306 DPRINTK(" Manufacturer: %s\n", specs
->manufacturer
);
307 DPRINTK(" Model: %x\n", specs
->model
);
308 DPRINTK(" Serial#: %u\n", specs
->serial
);
309 DPRINTK(" Year: %u Week %u\n", specs
->year
, specs
->week
);
312 static void get_dpms_capabilities(unsigned char flags
,
313 struct fb_monspecs
*specs
)
316 if (flags
& DPMS_ACTIVE_OFF
)
317 specs
->dpms
|= FB_DPMS_ACTIVE_OFF
;
318 if (flags
& DPMS_SUSPEND
)
319 specs
->dpms
|= FB_DPMS_SUSPEND
;
320 if (flags
& DPMS_STANDBY
)
321 specs
->dpms
|= FB_DPMS_STANDBY
;
322 DPRINTK(" DPMS: Active %s, Suspend %s, Standby %s\n",
323 (flags
& DPMS_ACTIVE_OFF
) ? "yes" : "no",
324 (flags
& DPMS_SUSPEND
) ? "yes" : "no",
325 (flags
& DPMS_STANDBY
) ? "yes" : "no");
328 static void get_chroma(unsigned char *block
, struct fb_monspecs
*specs
)
332 DPRINTK(" Chroma\n");
333 /* Chromaticity data */
334 tmp
= ((block
[5] & (3 << 6)) >> 6) | (block
[0x7] << 2);
337 specs
->chroma
.redx
= tmp
/1024;
338 DPRINTK(" RedX: 0.%03d ", specs
->chroma
.redx
);
340 tmp
= ((block
[5] & (3 << 4)) >> 4) | (block
[0x8] << 2);
343 specs
->chroma
.redy
= tmp
/1024;
344 DPRINTK("RedY: 0.%03d\n", specs
->chroma
.redy
);
346 tmp
= ((block
[5] & (3 << 2)) >> 2) | (block
[0x9] << 2);
349 specs
->chroma
.greenx
= tmp
/1024;
350 DPRINTK(" GreenX: 0.%03d ", specs
->chroma
.greenx
);
352 tmp
= (block
[5] & 3) | (block
[0xa] << 2);
355 specs
->chroma
.greeny
= tmp
/1024;
356 DPRINTK("GreenY: 0.%03d\n", specs
->chroma
.greeny
);
358 tmp
= ((block
[6] & (3 << 6)) >> 6) | (block
[0xb] << 2);
361 specs
->chroma
.bluex
= tmp
/1024;
362 DPRINTK(" BlueX: 0.%03d ", specs
->chroma
.bluex
);
364 tmp
= ((block
[6] & (3 << 4)) >> 4) | (block
[0xc] << 2);
367 specs
->chroma
.bluey
= tmp
/1024;
368 DPRINTK("BlueY: 0.%03d\n", specs
->chroma
.bluey
);
370 tmp
= ((block
[6] & (3 << 2)) >> 2) | (block
[0xd] << 2);
373 specs
->chroma
.whitex
= tmp
/1024;
374 DPRINTK(" WhiteX: 0.%03d ", specs
->chroma
.whitex
);
376 tmp
= (block
[6] & 3) | (block
[0xe] << 2);
379 specs
->chroma
.whitey
= tmp
/1024;
380 DPRINTK("WhiteY: 0.%03d\n", specs
->chroma
.whitey
);
383 static void calc_mode_timings(int xres
, int yres
, int refresh
,
384 struct fb_videomode
*mode
)
386 struct fb_var_screeninfo
*var
;
388 var
= kzalloc(sizeof(struct fb_var_screeninfo
), GFP_KERNEL
);
393 fb_get_mode(FB_VSYNCTIMINGS
| FB_IGNOREMON
,
397 mode
->pixclock
= var
->pixclock
;
398 mode
->refresh
= refresh
;
399 mode
->left_margin
= var
->left_margin
;
400 mode
->right_margin
= var
->right_margin
;
401 mode
->upper_margin
= var
->upper_margin
;
402 mode
->lower_margin
= var
->lower_margin
;
403 mode
->hsync_len
= var
->hsync_len
;
404 mode
->vsync_len
= var
->vsync_len
;
411 static int get_est_timing(unsigned char *block
, struct fb_videomode
*mode
)
418 calc_mode_timings(720, 400, 70, &mode
[num
]);
419 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
420 DPRINTK(" 720x400@70Hz\n");
423 calc_mode_timings(720, 400, 88, &mode
[num
]);
424 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
425 DPRINTK(" 720x400@88Hz\n");
428 mode
[num
++] = vesa_modes
[3];
429 DPRINTK(" 640x480@60Hz\n");
432 calc_mode_timings(640, 480, 67, &mode
[num
]);
433 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
434 DPRINTK(" 640x480@67Hz\n");
437 mode
[num
++] = vesa_modes
[4];
438 DPRINTK(" 640x480@72Hz\n");
441 mode
[num
++] = vesa_modes
[5];
442 DPRINTK(" 640x480@75Hz\n");
445 mode
[num
++] = vesa_modes
[7];
446 DPRINTK(" 800x600@56Hz\n");
449 mode
[num
++] = vesa_modes
[8];
450 DPRINTK(" 800x600@60Hz\n");
455 mode
[num
++] = vesa_modes
[9];
456 DPRINTK(" 800x600@72Hz\n");
459 mode
[num
++] = vesa_modes
[10];
460 DPRINTK(" 800x600@75Hz\n");
463 calc_mode_timings(832, 624, 75, &mode
[num
]);
464 mode
[num
++].flag
= FB_MODE_IS_CALCULATED
;
465 DPRINTK(" 832x624@75Hz\n");
468 mode
[num
++] = vesa_modes
[12];
469 DPRINTK(" 1024x768@87Hz Interlaced\n");
472 mode
[num
++] = vesa_modes
[13];
473 DPRINTK(" 1024x768@60Hz\n");
476 mode
[num
++] = vesa_modes
[14];
477 DPRINTK(" 1024x768@70Hz\n");
480 mode
[num
++] = vesa_modes
[15];
481 DPRINTK(" 1024x768@75Hz\n");
484 mode
[num
++] = vesa_modes
[21];
485 DPRINTK(" 1280x1024@75Hz\n");
489 mode
[num
++] = vesa_modes
[17];
490 DPRINTK(" 1152x870@75Hz\n");
492 DPRINTK(" Manufacturer's mask: %x\n",c
&0x7F);
496 static int get_std_timing(unsigned char *block
, struct fb_videomode
*mode
)
498 int xres
, yres
= 0, refresh
, ratio
, i
;
500 xres
= (block
[0] + 31) * 8;
504 ratio
= (block
[1] & 0xc0) >> 6;
516 yres
= (xres
* 9)/16;
519 refresh
= (block
[1] & 0x3f) + 60;
521 DPRINTK(" %dx%d@%dHz\n", xres
, yres
, refresh
);
522 for (i
= 0; i
< VESA_MODEDB_SIZE
; i
++) {
523 if (vesa_modes
[i
].xres
== xres
&&
524 vesa_modes
[i
].yres
== yres
&&
525 vesa_modes
[i
].refresh
== refresh
) {
526 *mode
= vesa_modes
[i
];
527 mode
->flag
|= FB_MODE_IS_STANDARD
;
531 calc_mode_timings(xres
, yres
, refresh
, mode
);
535 static int get_dst_timing(unsigned char *block
,
536 struct fb_videomode
*mode
)
540 for (j
= 0; j
< 6; j
++, block
+= STD_TIMING_DESCRIPTION_SIZE
)
541 num
+= get_std_timing(block
, &mode
[num
]);
546 static void get_detailed_timing(unsigned char *block
,
547 struct fb_videomode
*mode
)
549 mode
->xres
= H_ACTIVE
;
550 mode
->yres
= V_ACTIVE
;
551 mode
->pixclock
= PIXEL_CLOCK
;
552 mode
->pixclock
/= 1000;
553 mode
->pixclock
= KHZ2PICOS(mode
->pixclock
);
554 mode
->right_margin
= H_SYNC_OFFSET
;
555 mode
->left_margin
= (H_ACTIVE
+ H_BLANKING
) -
556 (H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
);
557 mode
->upper_margin
= V_BLANKING
- V_SYNC_OFFSET
-
559 mode
->lower_margin
= V_SYNC_OFFSET
;
560 mode
->hsync_len
= H_SYNC_WIDTH
;
561 mode
->vsync_len
= V_SYNC_WIDTH
;
563 mode
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
565 mode
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
566 mode
->refresh
= PIXEL_CLOCK
/((H_ACTIVE
+ H_BLANKING
) *
567 (V_ACTIVE
+ V_BLANKING
));
570 mode
->upper_margin
*= 2;
571 mode
->lower_margin
*= 2;
572 mode
->vsync_len
*= 2;
573 mode
->vmode
|= FB_VMODE_INTERLACED
;
575 mode
->flag
= FB_MODE_IS_DETAILED
;
577 DPRINTK(" %d MHz ", PIXEL_CLOCK
/1000000);
578 DPRINTK("%d %d %d %d ", H_ACTIVE
, H_ACTIVE
+ H_SYNC_OFFSET
,
579 H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
, H_ACTIVE
+ H_BLANKING
);
580 DPRINTK("%d %d %d %d ", V_ACTIVE
, V_ACTIVE
+ V_SYNC_OFFSET
,
581 V_ACTIVE
+ V_SYNC_OFFSET
+ V_SYNC_WIDTH
, V_ACTIVE
+ V_BLANKING
);
582 DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE
) ? "+" : "-",
583 (VSYNC_POSITIVE
) ? "+" : "-");
587 * fb_create_modedb - create video mode database
589 * @dbsize: database size
591 * RETURNS: struct fb_videomode, @dbsize contains length of database
594 * This function builds a mode database using the contents of the EDID
597 static struct fb_videomode
*fb_create_modedb(unsigned char *edid
, int *dbsize
)
599 struct fb_videomode
*mode
, *m
;
600 unsigned char *block
;
601 int num
= 0, i
, first
= 1;
603 mode
= kzalloc(50 * sizeof(struct fb_videomode
), GFP_KERNEL
);
607 if (edid
== NULL
|| !edid_checksum(edid
) ||
608 !edid_check_header(edid
)) {
615 DPRINTK(" Detailed Timings\n");
616 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
617 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
618 if (!(block
[0] == 0x00 && block
[1] == 0x00)) {
619 get_detailed_timing(block
, &mode
[num
]);
621 mode
[num
].flag
|= FB_MODE_IS_FIRST
;
628 DPRINTK(" Supported VESA Modes\n");
629 block
= edid
+ ESTABLISHED_TIMING_1
;
630 num
+= get_est_timing(block
, &mode
[num
]);
632 DPRINTK(" Standard Timings\n");
633 block
= edid
+ STD_TIMING_DESCRIPTIONS_START
;
634 for (i
= 0; i
< STD_TIMING
; i
++, block
+= STD_TIMING_DESCRIPTION_SIZE
)
635 num
+= get_std_timing(block
, &mode
[num
]);
637 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
638 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
639 if (block
[0] == 0x00 && block
[1] == 0x00 && block
[3] == 0xfa)
640 num
+= get_dst_timing(block
+ 5, &mode
[num
]);
643 /* Yikes, EDID data is totally useless */
650 m
= kmalloc(num
* sizeof(struct fb_videomode
), GFP_KERNEL
);
653 memmove(m
, mode
, num
* sizeof(struct fb_videomode
));
659 * fb_destroy_modedb - destroys mode database
660 * @modedb: mode database to destroy
663 * Destroy mode database created by fb_create_modedb
665 void fb_destroy_modedb(struct fb_videomode
*modedb
)
670 static int fb_get_monitor_limits(unsigned char *edid
, struct fb_monspecs
*specs
)
673 unsigned char *block
;
675 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
677 DPRINTK(" Monitor Operating Limits: ");
679 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
680 if (edid_is_limits_block(block
)) {
681 specs
->hfmin
= H_MIN_RATE
* 1000;
682 specs
->hfmax
= H_MAX_RATE
* 1000;
683 specs
->vfmin
= V_MIN_RATE
;
684 specs
->vfmax
= V_MAX_RATE
;
685 specs
->dclkmax
= MAX_PIXEL_CLOCK
* 1000000;
686 specs
->gtf
= (GTF_SUPPORT
) ? 1 : 0;
688 DPRINTK("From EDID\n");
693 /* estimate monitor limits based on modes supported */
695 struct fb_videomode
*modes
, *mode
;
696 int num_modes
, hz
, hscan
, pixclock
;
699 modes
= fb_create_modedb(edid
, &num_modes
);
701 DPRINTK("None Available\n");
706 for (i
= 0; i
< num_modes
; i
++) {
708 pixclock
= PICOS2KHZ(modes
[i
].pixclock
) * 1000;
709 htotal
= mode
->xres
+ mode
->right_margin
+ mode
->hsync_len
711 vtotal
= mode
->yres
+ mode
->lower_margin
+ mode
->vsync_len
712 + mode
->upper_margin
;
714 if (mode
->vmode
& FB_VMODE_INTERLACED
)
717 if (mode
->vmode
& FB_VMODE_DOUBLE
)
720 hscan
= (pixclock
+ htotal
/ 2) / htotal
;
721 hscan
= (hscan
+ 500) / 1000 * 1000;
722 hz
= (hscan
+ vtotal
/ 2) / vtotal
;
724 if (specs
->dclkmax
== 0 || specs
->dclkmax
< pixclock
)
725 specs
->dclkmax
= pixclock
;
727 if (specs
->dclkmin
== 0 || specs
->dclkmin
> pixclock
)
728 specs
->dclkmin
= pixclock
;
730 if (specs
->hfmax
== 0 || specs
->hfmax
< hscan
)
731 specs
->hfmax
= hscan
;
733 if (specs
->hfmin
== 0 || specs
->hfmin
> hscan
)
734 specs
->hfmin
= hscan
;
736 if (specs
->vfmax
== 0 || specs
->vfmax
< hz
)
739 if (specs
->vfmin
== 0 || specs
->vfmin
> hz
)
742 DPRINTK("Extrapolated\n");
743 fb_destroy_modedb(modes
);
745 DPRINTK(" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
746 specs
->hfmin
/1000, specs
->hfmax
/1000, specs
->vfmin
,
747 specs
->vfmax
, specs
->dclkmax
/1000000);
751 static void get_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
753 unsigned char c
, *block
;
755 block
= edid
+ EDID_STRUCT_DISPLAY
;
757 fb_get_monitor_limits(edid
, specs
);
762 specs
->input
|= FB_DISP_DDI
;
763 DPRINTK(" Digital Display Input");
765 DPRINTK(" Analog Display Input: Input Voltage - ");
766 switch ((block
[0] & 0x60) >> 5) {
768 DPRINTK("0.700V/0.300V");
769 specs
->input
|= FB_DISP_ANA_700_300
;
772 DPRINTK("0.714V/0.286V");
773 specs
->input
|= FB_DISP_ANA_714_286
;
776 DPRINTK("1.000V/0.400V");
777 specs
->input
|= FB_DISP_ANA_1000_400
;
780 DPRINTK("0.700V/0.000V");
781 specs
->input
|= FB_DISP_ANA_700_000
;
785 DPRINTK("\n Sync: ");
788 DPRINTK(" Configurable signal level\n");
792 DPRINTK("Blank to Blank ");
793 specs
->signal
|= FB_SIGNAL_BLANK_BLANK
;
796 DPRINTK("Separate ");
797 specs
->signal
|= FB_SIGNAL_SEPARATE
;
800 DPRINTK("Composite ");
801 specs
->signal
|= FB_SIGNAL_COMPOSITE
;
804 DPRINTK("Sync on Green ");
805 specs
->signal
|= FB_SIGNAL_SYNC_ON_GREEN
;
808 DPRINTK("Serration on ");
809 specs
->signal
|= FB_SIGNAL_SERRATION_ON
;
812 specs
->max_x
= block
[1];
813 specs
->max_y
= block
[2];
814 DPRINTK(" Max H-size in cm: ");
816 DPRINTK("%d\n", specs
->max_x
);
818 DPRINTK("variable\n");
819 DPRINTK(" Max V-size in cm: ");
821 DPRINTK("%d\n", specs
->max_y
);
823 DPRINTK("variable\n");
826 specs
->gamma
= c
+100;
828 DPRINTK("%d.%d\n", specs
->gamma
/100, specs
->gamma
% 100);
830 get_dpms_capabilities(block
[4], specs
);
832 switch ((block
[4] & 0x18) >> 3) {
834 DPRINTK(" Monochrome/Grayscale\n");
835 specs
->input
|= FB_DISP_MONO
;
838 DPRINTK(" RGB Color Display\n");
839 specs
->input
|= FB_DISP_RGB
;
842 DPRINTK(" Non-RGB Multicolor Display\n");
843 specs
->input
|= FB_DISP_MULTI
;
846 DPRINTK(" Unknown\n");
847 specs
->input
|= FB_DISP_UNKNOWN
;
851 get_chroma(block
, specs
);
856 DPRINTK(" Default color format is primary\n");
857 specs
->misc
|= FB_MISC_PRIM_COLOR
;
860 DPRINTK(" First DETAILED Timing is preferred\n");
861 specs
->misc
|= FB_MISC_1ST_DETAIL
;
864 printk(" Display is GTF capable\n");
869 int fb_parse_edid(unsigned char *edid
, struct fb_var_screeninfo
*var
)
872 unsigned char *block
;
874 if (edid
== NULL
|| var
== NULL
)
877 if (!(edid_checksum(edid
)))
880 if (!(edid_check_header(edid
)))
883 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
885 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
886 if (edid_is_timing_block(block
)) {
887 var
->xres
= var
->xres_virtual
= H_ACTIVE
;
888 var
->yres
= var
->yres_virtual
= V_ACTIVE
;
889 var
->height
= var
->width
= 0;
890 var
->right_margin
= H_SYNC_OFFSET
;
891 var
->left_margin
= (H_ACTIVE
+ H_BLANKING
) -
892 (H_ACTIVE
+ H_SYNC_OFFSET
+ H_SYNC_WIDTH
);
893 var
->upper_margin
= V_BLANKING
- V_SYNC_OFFSET
-
895 var
->lower_margin
= V_SYNC_OFFSET
;
896 var
->hsync_len
= H_SYNC_WIDTH
;
897 var
->vsync_len
= V_SYNC_WIDTH
;
898 var
->pixclock
= PIXEL_CLOCK
;
899 var
->pixclock
/= 1000;
900 var
->pixclock
= KHZ2PICOS(var
->pixclock
);
903 var
->sync
|= FB_SYNC_HOR_HIGH_ACT
;
905 var
->sync
|= FB_SYNC_VERT_HIGH_ACT
;
912 void fb_edid_to_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
914 unsigned char *block
;
920 if (!(edid_checksum(edid
)))
923 if (!(edid_check_header(edid
)))
926 memset(specs
, 0, sizeof(struct fb_monspecs
));
928 specs
->version
= edid
[EDID_STRUCT_VERSION
];
929 specs
->revision
= edid
[EDID_STRUCT_REVISION
];
931 DPRINTK("========================================\n");
932 DPRINTK("Display Information (EDID)\n");
933 DPRINTK("========================================\n");
934 DPRINTK(" EDID Version %d.%d\n", (int) specs
->version
,
935 (int) specs
->revision
);
937 parse_vendor_block(edid
+ ID_MANUFACTURER_NAME
, specs
);
939 block
= edid
+ DETAILED_TIMING_DESCRIPTIONS_START
;
940 for (i
= 0; i
< 4; i
++, block
+= DETAILED_TIMING_DESCRIPTION_SIZE
) {
941 if (edid_is_serial_block(block
)) {
942 copy_string(block
, specs
->serial_no
);
943 DPRINTK(" Serial Number: %s\n", specs
->serial_no
);
944 } else if (edid_is_ascii_block(block
)) {
945 copy_string(block
, specs
->ascii
);
946 DPRINTK(" ASCII Block: %s\n", specs
->ascii
);
947 } else if (edid_is_monitor_block(block
)) {
948 copy_string(block
, specs
->monitor
);
949 DPRINTK(" Monitor Name: %s\n", specs
->monitor
);
953 DPRINTK(" Display Characteristics:\n");
954 get_monspecs(edid
, specs
);
956 specs
->modedb
= fb_create_modedb(edid
, &specs
->modedb_len
);
959 * Workaround for buggy EDIDs that sets that the first
960 * detailed timing is preferred but has not detailed
963 for (i
= 0; i
< specs
->modedb_len
; i
++) {
964 if (specs
->modedb
[i
].flag
& FB_MODE_IS_DETAILED
) {
971 specs
->misc
&= ~FB_MISC_1ST_DETAIL
;
973 DPRINTK("========================================\n");
977 * VESA Generalized Timing Formula (GTF)
981 #define V_FRONTPORCH 1
983 #define H_SCALEFACTOR 20
984 #define H_BLANKSCALE 128
985 #define H_GRADIENT 600
989 struct __fb_timings
{
1002 * fb_get_vblank - get vertical blank time
1003 * @hfreq: horizontal freq
1006 * vblank = right_margin + vsync_len + left_margin
1008 * given: right_margin = 1 (V_FRONTPORCH)
1013 * left_margin = --------------- - vsync_len
1016 static u32
fb_get_vblank(u32 hfreq
)
1020 vblank
= (hfreq
* FLYBACK
)/1000;
1021 vblank
= (vblank
+ 500)/1000;
1022 return (vblank
+ V_FRONTPORCH
);
1026 * fb_get_hblank_by_freq - get horizontal blank time given hfreq
1027 * @hfreq: horizontal freq
1028 * @xres: horizontal resolution in pixels
1033 * hblank = ------------------
1036 * duty cycle = percent of htotal assigned to inactive display
1037 * duty cycle = C - (M/Hfreq)
1039 * where: C = ((offset - scale factor) * blank_scale)
1040 * -------------------------------------- + scale factor
1042 * M = blank_scale * gradient
1045 static u32
fb_get_hblank_by_hfreq(u32 hfreq
, u32 xres
)
1047 u32 c_val
, m_val
, duty_cycle
, hblank
;
1049 c_val
= (((H_OFFSET
- H_SCALEFACTOR
) * H_BLANKSCALE
)/256 +
1050 H_SCALEFACTOR
) * 1000;
1051 m_val
= (H_BLANKSCALE
* H_GRADIENT
)/256;
1052 m_val
= (m_val
* 1000000)/hfreq
;
1053 duty_cycle
= c_val
- m_val
;
1054 hblank
= (xres
* duty_cycle
)/(100000 - duty_cycle
);
1059 * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
1060 * @dclk: pixelclock in Hz
1061 * @xres: horizontal resolution in pixels
1066 * hblank = ------------------
1069 * duty cycle = percent of htotal assigned to inactive display
1070 * duty cycle = C - (M * h_period)
1072 * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
1073 * -----------------------------------------------
1079 static u32
fb_get_hblank_by_dclk(u32 dclk
, u32 xres
)
1081 u32 duty_cycle
, h_period
, hblank
;
1084 h_period
= 100 - C_VAL
;
1085 h_period
*= h_period
;
1086 h_period
+= (M_VAL
* xres
* 2 * 1000)/(5 * dclk
);
1089 h_period
= int_sqrt(h_period
);
1090 h_period
-= (100 - C_VAL
) * 100;
1092 h_period
/= 2 * M_VAL
;
1094 duty_cycle
= C_VAL
* 1000 - (M_VAL
* h_period
)/100;
1095 hblank
= (xres
* duty_cycle
)/(100000 - duty_cycle
) + 8;
1101 * fb_get_hfreq - estimate hsync
1102 * @vfreq: vertical refresh rate
1103 * @yres: vertical resolution
1107 * (yres + front_port) * vfreq * 1000000
1108 * hfreq = -------------------------------------
1109 * (1000000 - (vfreq * FLYBACK)
1113 static u32
fb_get_hfreq(u32 vfreq
, u32 yres
)
1117 divisor
= (1000000 - (vfreq
* FLYBACK
))/1000;
1118 hfreq
= (yres
+ V_FRONTPORCH
) * vfreq
* 1000;
1119 return (hfreq
/divisor
);
1122 static void fb_timings_vfreq(struct __fb_timings
*timings
)
1124 timings
->hfreq
= fb_get_hfreq(timings
->vfreq
, timings
->vactive
);
1125 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1126 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1127 timings
->hblank
= fb_get_hblank_by_hfreq(timings
->hfreq
,
1129 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1130 timings
->dclk
= timings
->htotal
* timings
->hfreq
;
1133 static void fb_timings_hfreq(struct __fb_timings
*timings
)
1135 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1136 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1137 timings
->vfreq
= timings
->hfreq
/timings
->vtotal
;
1138 timings
->hblank
= fb_get_hblank_by_hfreq(timings
->hfreq
,
1140 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1141 timings
->dclk
= timings
->htotal
* timings
->hfreq
;
1144 static void fb_timings_dclk(struct __fb_timings
*timings
)
1146 timings
->hblank
= fb_get_hblank_by_dclk(timings
->dclk
,
1148 timings
->htotal
= timings
->hactive
+ timings
->hblank
;
1149 timings
->hfreq
= timings
->dclk
/timings
->htotal
;
1150 timings
->vblank
= fb_get_vblank(timings
->hfreq
);
1151 timings
->vtotal
= timings
->vactive
+ timings
->vblank
;
1152 timings
->vfreq
= timings
->hfreq
/timings
->vtotal
;
1156 * fb_get_mode - calculates video mode using VESA GTF
1157 * @flags: if: 0 - maximize vertical refresh rate
1158 * 1 - vrefresh-driven calculation;
1159 * 2 - hscan-driven calculation;
1160 * 3 - pixelclock-driven calculation;
1161 * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock
1162 * @var: pointer to fb_var_screeninfo
1163 * @info: pointer to fb_info
1166 * Calculates video mode based on monitor specs using VESA GTF.
1167 * The GTF is best for VESA GTF compliant monitors but is
1168 * specifically formulated to work for older monitors as well.
1170 * If @flag==0, the function will attempt to maximize the
1171 * refresh rate. Otherwise, it will calculate timings based on
1172 * the flag and accompanying value.
1174 * If FB_IGNOREMON bit is set in @flags, monitor specs will be
1175 * ignored and @var will be filled with the calculated timings.
1177 * All calculations are based on the VESA GTF Spreadsheet
1178 * available at VESA's public ftp (http://www.vesa.org).
1181 * The timings generated by the GTF will be different from VESA
1182 * DMT. It might be a good idea to keep a table of standard
1183 * VESA modes as well. The GTF may also not work for some displays,
1184 * such as, and especially, analog TV.
1187 * A valid info->monspecs, otherwise 'safe numbers' will be used.
1189 int fb_get_mode(int flags
, u32 val
, struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1191 struct __fb_timings
*timings
;
1192 u32 interlace
= 1, dscan
= 1;
1193 u32 hfmin
, hfmax
, vfmin
, vfmax
, dclkmin
, dclkmax
, err
= 0;
1196 timings
= kzalloc(sizeof(struct __fb_timings
), GFP_KERNEL
);
1202 * If monspecs are invalid, use values that are enough
1205 if (!info
|| !info
->monspecs
.hfmax
|| !info
->monspecs
.vfmax
||
1206 !info
->monspecs
.dclkmax
||
1207 info
->monspecs
.hfmax
< info
->monspecs
.hfmin
||
1208 info
->monspecs
.vfmax
< info
->monspecs
.vfmin
||
1209 info
->monspecs
.dclkmax
< info
->monspecs
.dclkmin
) {
1210 hfmin
= 29000; hfmax
= 30000;
1211 vfmin
= 60; vfmax
= 60;
1212 dclkmin
= 0; dclkmax
= 25000000;
1214 hfmin
= info
->monspecs
.hfmin
;
1215 hfmax
= info
->monspecs
.hfmax
;
1216 vfmin
= info
->monspecs
.vfmin
;
1217 vfmax
= info
->monspecs
.vfmax
;
1218 dclkmin
= info
->monspecs
.dclkmin
;
1219 dclkmax
= info
->monspecs
.dclkmax
;
1222 timings
->hactive
= var
->xres
;
1223 timings
->vactive
= var
->yres
;
1224 if (var
->vmode
& FB_VMODE_INTERLACED
) {
1225 timings
->vactive
/= 2;
1228 if (var
->vmode
& FB_VMODE_DOUBLE
) {
1229 timings
->vactive
*= 2;
1233 switch (flags
& ~FB_IGNOREMON
) {
1234 case FB_MAXTIMINGS
: /* maximize refresh rate */
1235 timings
->hfreq
= hfmax
;
1236 fb_timings_hfreq(timings
);
1237 if (timings
->vfreq
> vfmax
) {
1238 timings
->vfreq
= vfmax
;
1239 fb_timings_vfreq(timings
);
1241 if (timings
->dclk
> dclkmax
) {
1242 timings
->dclk
= dclkmax
;
1243 fb_timings_dclk(timings
);
1246 case FB_VSYNCTIMINGS
: /* vrefresh driven */
1247 timings
->vfreq
= val
;
1248 fb_timings_vfreq(timings
);
1250 case FB_HSYNCTIMINGS
: /* hsync driven */
1251 timings
->hfreq
= val
;
1252 fb_timings_hfreq(timings
);
1254 case FB_DCLKTIMINGS
: /* pixelclock driven */
1255 timings
->dclk
= PICOS2KHZ(val
) * 1000;
1256 fb_timings_dclk(timings
);
1263 if (err
|| (!(flags
& FB_IGNOREMON
) &&
1264 (timings
->vfreq
< vfmin
|| timings
->vfreq
> vfmax
||
1265 timings
->hfreq
< hfmin
|| timings
->hfreq
> hfmax
||
1266 timings
->dclk
< dclkmin
|| timings
->dclk
> dclkmax
))) {
1269 var
->pixclock
= KHZ2PICOS(timings
->dclk
/1000);
1270 var
->hsync_len
= (timings
->htotal
* 8)/100;
1271 var
->right_margin
= (timings
->hblank
/2) - var
->hsync_len
;
1272 var
->left_margin
= timings
->hblank
- var
->right_margin
-
1274 var
->vsync_len
= (3 * interlace
)/dscan
;
1275 var
->lower_margin
= (1 * interlace
)/dscan
;
1276 var
->upper_margin
= (timings
->vblank
* interlace
)/dscan
-
1277 (var
->vsync_len
+ var
->lower_margin
);
1284 int fb_parse_edid(unsigned char *edid
, struct fb_var_screeninfo
*var
)
1288 void fb_edid_to_monspecs(unsigned char *edid
, struct fb_monspecs
*specs
)
1292 void fb_destroy_modedb(struct fb_videomode
*modedb
)
1295 int fb_get_mode(int flags
, u32 val
, struct fb_var_screeninfo
*var
,
1296 struct fb_info
*info
)
1300 #endif /* CONFIG_FB_MODE_HELPERS */
1303 * fb_validate_mode - validates var against monitor capabilities
1304 * @var: pointer to fb_var_screeninfo
1305 * @info: pointer to fb_info
1308 * Validates video mode against monitor capabilities specified in
1312 * A valid info->monspecs.
1314 int fb_validate_mode(const struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1316 u32 hfreq
, vfreq
, htotal
, vtotal
, pixclock
;
1317 u32 hfmin
, hfmax
, vfmin
, vfmax
, dclkmin
, dclkmax
;
1320 * If monspecs are invalid, use values that are enough
1323 if (!info
->monspecs
.hfmax
|| !info
->monspecs
.vfmax
||
1324 !info
->monspecs
.dclkmax
||
1325 info
->monspecs
.hfmax
< info
->monspecs
.hfmin
||
1326 info
->monspecs
.vfmax
< info
->monspecs
.vfmin
||
1327 info
->monspecs
.dclkmax
< info
->monspecs
.dclkmin
) {
1328 hfmin
= 29000; hfmax
= 30000;
1329 vfmin
= 60; vfmax
= 60;
1330 dclkmin
= 0; dclkmax
= 25000000;
1332 hfmin
= info
->monspecs
.hfmin
;
1333 hfmax
= info
->monspecs
.hfmax
;
1334 vfmin
= info
->monspecs
.vfmin
;
1335 vfmax
= info
->monspecs
.vfmax
;
1336 dclkmin
= info
->monspecs
.dclkmin
;
1337 dclkmax
= info
->monspecs
.dclkmax
;
1342 pixclock
= PICOS2KHZ(var
->pixclock
) * 1000;
1344 htotal
= var
->xres
+ var
->right_margin
+ var
->hsync_len
+
1346 vtotal
= var
->yres
+ var
->lower_margin
+ var
->vsync_len
+
1349 if (var
->vmode
& FB_VMODE_INTERLACED
)
1351 if (var
->vmode
& FB_VMODE_DOUBLE
)
1354 hfreq
= pixclock
/htotal
;
1355 hfreq
= (hfreq
+ 500) / 1000 * 1000;
1357 vfreq
= hfreq
/vtotal
;
1359 return (vfreq
< vfmin
|| vfreq
> vfmax
||
1360 hfreq
< hfmin
|| hfreq
> hfmax
||
1361 pixclock
< dclkmin
|| pixclock
> dclkmax
) ?
1365 #if defined(CONFIG_FIRMWARE_EDID) && defined(CONFIG_X86)
1368 * We need to ensure that the EDID block is only returned for
1369 * the primary graphics adapter.
1372 const unsigned char *fb_firmware_edid(struct device
*device
)
1374 struct pci_dev
*dev
= NULL
;
1375 struct resource
*res
= NULL
;
1376 unsigned char *edid
= NULL
;
1379 dev
= to_pci_dev(device
);
1382 res
= &dev
->resource
[PCI_ROM_RESOURCE
];
1384 if (res
&& res
->flags
& IORESOURCE_ROM_SHADOW
)
1385 edid
= edid_info
.dummy
;
1390 const unsigned char *fb_firmware_edid(struct device
*device
)
1395 EXPORT_SYMBOL(fb_firmware_edid
);
1397 EXPORT_SYMBOL(fb_parse_edid
);
1398 EXPORT_SYMBOL(fb_edid_to_monspecs
);
1399 EXPORT_SYMBOL(fb_get_mode
);
1400 EXPORT_SYMBOL(fb_validate_mode
);
1401 EXPORT_SYMBOL(fb_destroy_modedb
);