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>
33 #include <linux/pci.h>
35 #include <asm/pci-bridge.h>
37 #include <video/edid.h>
44 #undef DEBUG /* define this for verbose EDID parsing output */
47 #define DPRINTK(fmt, args...) printk(fmt,## args)
49 #define DPRINTK(fmt, args...)
53 const unsigned char edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
54 0xff, 0xff, 0xff, 0x00
56 const unsigned char edid_v1_descriptor_flag[] = { 0x00, 0x00 };
58 static void copy_string(unsigned char *c, unsigned char *s)
62 for (i = 0; (i < 13 && *c != 0x0A); i++)
65 while (i-- && (*--s == 0x20)) *s = 0;
68 static int edid_checksum(unsigned char *edid)
70 unsigned char i, csum = 0;
72 for (i = 0; i < EDID_LENGTH; i++)
76 /* checksum passed, everything's good */
79 printk("EDID checksum failed, aborting\n");
84 static int edid_check_header(unsigned char *edid)
86 if ((edid[0] != 0x00) || (edid[1] != 0xff) || (edid[2] != 0xff) ||
87 (edid[3] != 0xff) || (edid[4] != 0xff) || (edid[5] != 0xff) ||
90 ("EDID header doesn't match EDID v1 header, aborting\n");
96 static void parse_vendor_block(unsigned char *block, struct fb_monspecs *specs)
98 specs->manufacturer[0] = ((block[0] & 0x7c) >> 2) + '@';
99 specs->manufacturer[1] = ((block[0] & 0x03) << 3) +
100 ((block[1] & 0xe0) >> 5) + '@';
101 specs->manufacturer[2] = (block[1] & 0x1f) + '@';
102 specs->manufacturer[3] = 0;
103 specs->model = block[2] + (block[3] << 8);
104 specs->serial = block[4] + (block[5] << 8) +
105 (block[6] << 16) + (block[7] << 24);
106 specs->year = block[9] + 1990;
107 specs->week = block[8];
108 DPRINTK(" Manufacturer: %s\n", specs->manufacturer);
109 DPRINTK(" Model: %x\n", specs->model);
110 DPRINTK(" Serial#: %u\n", specs->serial);
111 DPRINTK(" Year: %u Week %u\n", specs->year, specs->week);
114 static void get_dpms_capabilities(unsigned char flags,
115 struct fb_monspecs *specs)
118 if (flags & DPMS_ACTIVE_OFF)
119 specs->dpms |= FB_DPMS_ACTIVE_OFF;
120 if (flags & DPMS_SUSPEND)
121 specs->dpms |= FB_DPMS_SUSPEND;
122 if (flags & DPMS_STANDBY)
123 specs->dpms |= FB_DPMS_STANDBY;
124 DPRINTK(" DPMS: Active %s, Suspend %s, Standby %s\n",
125 (flags & DPMS_ACTIVE_OFF) ? "yes" : "no",
126 (flags & DPMS_SUSPEND) ? "yes" : "no",
127 (flags & DPMS_STANDBY) ? "yes" : "no");
130 static void get_chroma(unsigned char *block, struct fb_monspecs *specs)
134 DPRINTK(" Chroma\n");
135 /* Chromaticity data */
136 tmp = ((block[5] & (3 << 6)) >> 6) | (block[0x7] << 2);
139 specs->chroma.redx = tmp/1024;
140 DPRINTK(" RedX: 0.%03d ", specs->chroma.redx);
142 tmp = ((block[5] & (3 << 4)) >> 4) | (block[0x8] << 2);
145 specs->chroma.redy = tmp/1024;
146 DPRINTK("RedY: 0.%03d\n", specs->chroma.redy);
148 tmp = ((block[5] & (3 << 2)) >> 2) | (block[0x9] << 2);
151 specs->chroma.greenx = tmp/1024;
152 DPRINTK(" GreenX: 0.%03d ", specs->chroma.greenx);
154 tmp = (block[5] & 3) | (block[0xa] << 2);
157 specs->chroma.greeny = tmp/1024;
158 DPRINTK("GreenY: 0.%03d\n", specs->chroma.greeny);
160 tmp = ((block[6] & (3 << 6)) >> 6) | (block[0xb] << 2);
163 specs->chroma.bluex = tmp/1024;
164 DPRINTK(" BlueX: 0.%03d ", specs->chroma.bluex);
166 tmp = ((block[6] & (3 << 4)) >> 4) | (block[0xc] << 2);
169 specs->chroma.bluey = tmp/1024;
170 DPRINTK("BlueY: 0.%03d\n", specs->chroma.bluey);
172 tmp = ((block[6] & (3 << 2)) >> 2) | (block[0xd] << 2);
175 specs->chroma.whitex = tmp/1024;
176 DPRINTK(" WhiteX: 0.%03d ", specs->chroma.whitex);
178 tmp = (block[6] & 3) | (block[0xe] << 2);
181 specs->chroma.whitey = tmp/1024;
182 DPRINTK("WhiteY: 0.%03d\n", specs->chroma.whitey);
185 static int edid_is_serial_block(unsigned char *block)
187 if ((block[0] == 0x00) && (block[1] == 0x00) &&
188 (block[2] == 0x00) && (block[3] == 0xff) &&
195 static int edid_is_ascii_block(unsigned char *block)
197 if ((block[0] == 0x00) && (block[1] == 0x00) &&
198 (block[2] == 0x00) && (block[3] == 0xfe) &&
205 static int edid_is_limits_block(unsigned char *block)
207 if ((block[0] == 0x00) && (block[1] == 0x00) &&
208 (block[2] == 0x00) && (block[3] == 0xfd) &&
215 static int edid_is_monitor_block(unsigned char *block)
217 if ((block[0] == 0x00) && (block[1] == 0x00) &&
218 (block[2] == 0x00) && (block[3] == 0xfc) &&
225 static void calc_mode_timings(int xres, int yres, int refresh, struct fb_videomode *mode)
227 struct fb_var_screeninfo var;
232 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON,
233 refresh, &var, &info);
236 mode->pixclock = var.pixclock;
237 mode->refresh = refresh;
238 mode->left_margin = var.left_margin;
239 mode->right_margin = var.right_margin;
240 mode->upper_margin = var.upper_margin;
241 mode->lower_margin = var.lower_margin;
242 mode->hsync_len = var.hsync_len;
243 mode->vsync_len = var.vsync_len;
248 static int get_est_timing(unsigned char *block, struct fb_videomode *mode)
255 calc_mode_timings(720, 400, 70, &mode[num]);
256 mode[num++].flag = FB_MODE_IS_CALCULATED;
257 DPRINTK(" 720x400@70Hz\n");
260 calc_mode_timings(720, 400, 88, &mode[num]);
261 mode[num++].flag = FB_MODE_IS_CALCULATED;
262 DPRINTK(" 720x400@88Hz\n");
265 mode[num++] = vesa_modes[3];
266 DPRINTK(" 640x480@60Hz\n");
269 calc_mode_timings(640, 480, 67, &mode[num]);
270 mode[num++].flag = FB_MODE_IS_CALCULATED;
271 DPRINTK(" 640x480@67Hz\n");
274 mode[num++] = vesa_modes[4];
275 DPRINTK(" 640x480@72Hz\n");
278 mode[num++] = vesa_modes[5];
279 DPRINTK(" 640x480@75Hz\n");
282 mode[num++] = vesa_modes[7];
283 DPRINTK(" 800x600@56Hz\n");
286 mode[num++] = vesa_modes[8];
287 DPRINTK(" 800x600@60Hz\n");
292 mode[num++] = vesa_modes[9];
293 DPRINTK(" 800x600@72Hz\n");
296 mode[num++] = vesa_modes[10];
297 DPRINTK(" 800x600@75Hz\n");
300 calc_mode_timings(832, 624, 75, &mode[num]);
301 mode[num++].flag = FB_MODE_IS_CALCULATED;
302 DPRINTK(" 832x624@75Hz\n");
305 mode[num++] = vesa_modes[12];
306 DPRINTK(" 1024x768@87Hz Interlaced\n");
309 mode[num++] = vesa_modes[13];
310 DPRINTK(" 1024x768@60Hz\n");
313 mode[num++] = vesa_modes[14];
314 DPRINTK(" 1024x768@70Hz\n");
317 mode[num++] = vesa_modes[15];
318 DPRINTK(" 1024x768@75Hz\n");
321 mode[num++] = vesa_modes[21];
322 DPRINTK(" 1280x1024@75Hz\n");
326 mode[num++] = vesa_modes[17];
327 DPRINTK(" 1152x870@75Hz\n");
329 DPRINTK(" Manufacturer's mask: %x\n",c&0x7F);
333 static int get_std_timing(unsigned char *block, struct fb_videomode *mode)
335 int xres, yres = 0, refresh, ratio, i;
337 xres = (block[0] + 31) * 8;
341 ratio = (block[1] & 0xc0) >> 6;
353 yres = (xres * 9)/16;
356 refresh = (block[1] & 0x3f) + 60;
358 DPRINTK(" %dx%d@%dHz\n", xres, yres, refresh);
359 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
360 if (vesa_modes[i].xres == xres &&
361 vesa_modes[i].yres == yres &&
362 vesa_modes[i].refresh == refresh) {
363 *mode = vesa_modes[i];
364 mode->flag |= FB_MODE_IS_STANDARD;
368 calc_mode_timings(xres, yres, refresh, mode);
372 static int get_dst_timing(unsigned char *block,
373 struct fb_videomode *mode)
377 for (j = 0; j < 6; j++, block+= STD_TIMING_DESCRIPTION_SIZE)
378 num += get_std_timing(block, &mode[num]);
383 static void get_detailed_timing(unsigned char *block,
384 struct fb_videomode *mode)
386 mode->xres = H_ACTIVE;
387 mode->yres = V_ACTIVE;
388 mode->pixclock = PIXEL_CLOCK;
389 mode->pixclock /= 1000;
390 mode->pixclock = KHZ2PICOS(mode->pixclock);
391 mode->right_margin = H_SYNC_OFFSET;
392 mode->left_margin = (H_ACTIVE + H_BLANKING) -
393 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
394 mode->upper_margin = V_BLANKING - V_SYNC_OFFSET -
396 mode->lower_margin = V_SYNC_OFFSET;
397 mode->hsync_len = H_SYNC_WIDTH;
398 mode->vsync_len = V_SYNC_WIDTH;
400 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
402 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
403 mode->refresh = PIXEL_CLOCK/((H_ACTIVE + H_BLANKING) *
404 (V_ACTIVE + V_BLANKING));
406 mode->flag = FB_MODE_IS_DETAILED;
408 DPRINTK(" %d MHz ", PIXEL_CLOCK/1000000);
409 DPRINTK("%d %d %d %d ", H_ACTIVE, H_ACTIVE + H_SYNC_OFFSET,
410 H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH, H_ACTIVE + H_BLANKING);
411 DPRINTK("%d %d %d %d ", V_ACTIVE, V_ACTIVE + V_SYNC_OFFSET,
412 V_ACTIVE + V_SYNC_OFFSET + V_SYNC_WIDTH, V_ACTIVE + V_BLANKING);
413 DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE) ? "+" : "-",
414 (VSYNC_POSITIVE) ? "+" : "-");
418 * fb_create_modedb - create video mode database
420 * @dbsize: database size
422 * RETURNS: struct fb_videomode, @dbsize contains length of database
425 * This function builds a mode database using the contents of the EDID
428 struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize)
430 struct fb_videomode *mode, *m;
431 unsigned char *block;
434 mode = kmalloc(50 * sizeof(struct fb_videomode), GFP_KERNEL);
437 memset(mode, 0, 50 * sizeof(struct fb_videomode));
439 if (edid == NULL || !edid_checksum(edid) ||
440 !edid_check_header(edid)) {
447 DPRINTK(" Supported VESA Modes\n");
448 block = edid + ESTABLISHED_TIMING_1;
449 num += get_est_timing(block, &mode[num]);
451 DPRINTK(" Standard Timings\n");
452 block = edid + STD_TIMING_DESCRIPTIONS_START;
453 for (i = 0; i < STD_TIMING; i++, block += STD_TIMING_DESCRIPTION_SIZE)
454 num += get_std_timing(block, &mode[num]);
456 DPRINTK(" Detailed Timings\n");
457 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
458 for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) {
461 if (block[0] == 0x00 && block[1] == 0x00) {
462 if (block[3] == 0xfa) {
463 num += get_dst_timing(block + 5, &mode[num]);
466 get_detailed_timing(block, &mode[num]);
468 mode[num].flag |= FB_MODE_IS_FIRST;
475 /* Yikes, EDID data is totally useless */
482 m = kmalloc(num * sizeof(struct fb_videomode), GFP_KERNEL);
485 memmove(m, mode, num * sizeof(struct fb_videomode));
491 * fb_destroy_modedb - destroys mode database
492 * @modedb: mode database to destroy
495 * Destroy mode database created by fb_create_modedb
497 void fb_destroy_modedb(struct fb_videomode *modedb)
503 int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs)
506 unsigned char *block;
508 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
510 DPRINTK(" Monitor Operating Limits: ");
511 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
512 if (edid_is_limits_block(block)) {
513 specs->hfmin = H_MIN_RATE * 1000;
514 specs->hfmax = H_MAX_RATE * 1000;
515 specs->vfmin = V_MIN_RATE;
516 specs->vfmax = V_MAX_RATE;
517 specs->dclkmax = MAX_PIXEL_CLOCK * 1000000;
518 specs->gtf = (GTF_SUPPORT) ? 1 : 0;
520 DPRINTK("From EDID\n");
525 /* estimate monitor limits based on modes supported */
527 struct fb_videomode *modes;
528 int num_modes, i, hz, hscan, pixclock;
530 modes = fb_create_modedb(edid, &num_modes);
532 DPRINTK("None Available\n");
537 for (i = 0; i < num_modes; i++) {
538 hz = modes[i].refresh;
539 pixclock = PICOS2KHZ(modes[i].pixclock) * 1000;
540 hscan = (modes[i].yres * 105 * hz + 5000)/100;
542 if (specs->dclkmax == 0 || specs->dclkmax < pixclock)
543 specs->dclkmax = pixclock;
544 if (specs->dclkmin == 0 || specs->dclkmin > pixclock)
545 specs->dclkmin = pixclock;
546 if (specs->hfmax == 0 || specs->hfmax < hscan)
547 specs->hfmax = hscan;
548 if (specs->hfmin == 0 || specs->hfmin > hscan)
549 specs->hfmin = hscan;
550 if (specs->vfmax == 0 || specs->vfmax < hz)
552 if (specs->vfmin == 0 || specs->vfmin > hz)
555 DPRINTK("Extrapolated\n");
556 fb_destroy_modedb(modes);
558 DPRINTK(" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
559 specs->hfmin/1000, specs->hfmax/1000, specs->vfmin,
560 specs->vfmax, specs->dclkmax/1000000);
564 static void get_monspecs(unsigned char *edid, struct fb_monspecs *specs)
566 unsigned char c, *block;
568 block = edid + EDID_STRUCT_DISPLAY;
570 fb_get_monitor_limits(edid, specs);
572 c = (block[0] & 0x80) >> 7;
575 specs->input |= FB_DISP_DDI;
576 DPRINTK(" Digital Display Input");
578 DPRINTK(" Analog Display Input: Input Voltage - ");
579 switch ((block[0] & 0x60) >> 5) {
581 DPRINTK("0.700V/0.300V");
582 specs->input |= FB_DISP_ANA_700_300;
585 DPRINTK("0.714V/0.286V");
586 specs->input |= FB_DISP_ANA_714_286;
589 DPRINTK("1.000V/0.400V");
590 specs->input |= FB_DISP_ANA_1000_400;
593 DPRINTK("0.700V/0.000V");
594 specs->input |= FB_DISP_ANA_700_000;
598 specs->input |= FB_DISP_UNKNOWN;
601 DPRINTK("\n Sync: ");
602 c = (block[0] & 0x10) >> 4;
604 DPRINTK(" Configurable signal level\n");
608 DPRINTK("Blank to Blank ");
609 specs->signal |= FB_SIGNAL_BLANK_BLANK;
612 DPRINTK("Separate ");
613 specs->signal |= FB_SIGNAL_SEPARATE;
616 DPRINTK("Composite ");
617 specs->signal |= FB_SIGNAL_COMPOSITE;
620 DPRINTK("Sync on Green ");
621 specs->signal |= FB_SIGNAL_SYNC_ON_GREEN;
624 DPRINTK("Serration on ");
625 specs->signal |= FB_SIGNAL_SERRATION_ON;
628 specs->max_x = block[1];
629 specs->max_y = block[2];
630 DPRINTK(" Max H-size in cm: ");
632 DPRINTK("%d\n", specs->max_x);
634 DPRINTK("variable\n");
635 DPRINTK(" Max V-size in cm: ");
637 DPRINTK("%d\n", specs->max_y);
639 DPRINTK("variable\n");
642 specs->gamma = c+100;
644 DPRINTK("%d.%d\n", specs->gamma/100, specs->gamma % 100);
646 get_dpms_capabilities(block[4], specs);
648 switch ((block[4] & 0x18) >> 3) {
650 DPRINTK(" Monochrome/Grayscale\n");
651 specs->input |= FB_DISP_MONO;
654 DPRINTK(" RGB Color Display\n");
655 specs->input |= FB_DISP_RGB;
658 DPRINTK(" Non-RGB Multicolor Display\n");
659 specs->input |= FB_DISP_MULTI;
662 DPRINTK(" Unknown\n");
663 specs->input |= FB_DISP_UNKNOWN;
667 get_chroma(block, specs);
672 DPRINTK(" Default color format is primary\n");
673 specs->misc |= FB_MISC_PRIM_COLOR;
676 DPRINTK(" First DETAILED Timing is preferred\n");
677 specs->misc |= FB_MISC_1ST_DETAIL;
680 printk(" Display is GTF capable\n");
685 static int edid_is_timing_block(unsigned char *block)
687 if ((block[0] != 0x00) || (block[1] != 0x00) ||
688 (block[2] != 0x00) || (block[4] != 0x00))
694 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
697 unsigned char *block;
699 if (edid == NULL || var == NULL)
702 if (!(edid_checksum(edid)))
705 if (!(edid_check_header(edid)))
708 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
710 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
711 if (edid_is_timing_block(block)) {
712 var->xres = var->xres_virtual = H_ACTIVE;
713 var->yres = var->yres_virtual = V_ACTIVE;
714 var->height = var->width = -1;
715 var->right_margin = H_SYNC_OFFSET;
716 var->left_margin = (H_ACTIVE + H_BLANKING) -
717 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
718 var->upper_margin = V_BLANKING - V_SYNC_OFFSET -
720 var->lower_margin = V_SYNC_OFFSET;
721 var->hsync_len = H_SYNC_WIDTH;
722 var->vsync_len = V_SYNC_WIDTH;
723 var->pixclock = PIXEL_CLOCK;
724 var->pixclock /= 1000;
725 var->pixclock = KHZ2PICOS(var->pixclock);
728 var->sync |= FB_SYNC_HOR_HIGH_ACT;
730 var->sync |= FB_SYNC_VERT_HIGH_ACT;
737 void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
739 unsigned char *block;
745 if (!(edid_checksum(edid)))
748 if (!(edid_check_header(edid)))
751 memset(specs, 0, sizeof(struct fb_monspecs));
753 specs->version = edid[EDID_STRUCT_VERSION];
754 specs->revision = edid[EDID_STRUCT_REVISION];
756 DPRINTK("========================================\n");
757 DPRINTK("Display Information (EDID)\n");
758 DPRINTK("========================================\n");
759 DPRINTK(" EDID Version %d.%d\n", (int) specs->version,
760 (int) specs->revision);
762 parse_vendor_block(edid + ID_MANUFACTURER_NAME, specs);
764 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
765 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
766 if (edid_is_serial_block(block)) {
767 copy_string(block, specs->serial_no);
768 DPRINTK(" Serial Number: %s\n", specs->serial_no);
769 } else if (edid_is_ascii_block(block)) {
770 copy_string(block, specs->ascii);
771 DPRINTK(" ASCII Block: %s\n", specs->ascii);
772 } else if (edid_is_monitor_block(block)) {
773 copy_string(block, specs->monitor);
774 DPRINTK(" Monitor Name: %s\n", specs->monitor);
778 DPRINTK(" Display Characteristics:\n");
779 get_monspecs(edid, specs);
781 specs->modedb = fb_create_modedb(edid, &specs->modedb_len);
782 DPRINTK("========================================\n");
785 char *get_EDID_from_firmware(struct device *dev)
787 unsigned char *pedid = NULL;
789 #if defined(CONFIG_EDID_FIRMWARE) && defined(CONFIG_X86)
790 pedid = edid_info.dummy;
798 * VESA Generalized Timing Formula (GTF)
802 #define V_FRONTPORCH 1
804 #define H_SCALEFACTOR 20
805 #define H_BLANKSCALE 128
806 #define H_GRADIENT 600
810 struct __fb_timings {
823 * fb_get_vblank - get vertical blank time
824 * @hfreq: horizontal freq
827 * vblank = right_margin + vsync_len + left_margin
829 * given: right_margin = 1 (V_FRONTPORCH)
834 * left_margin = --------------- - vsync_len
837 static u32 fb_get_vblank(u32 hfreq)
841 vblank = (hfreq * FLYBACK)/1000;
842 vblank = (vblank + 500)/1000;
843 return (vblank + V_FRONTPORCH);
847 * fb_get_hblank_by_freq - get horizontal blank time given hfreq
848 * @hfreq: horizontal freq
849 * @xres: horizontal resolution in pixels
854 * hblank = ------------------
857 * duty cycle = percent of htotal assigned to inactive display
858 * duty cycle = C - (M/Hfreq)
860 * where: C = ((offset - scale factor) * blank_scale)
861 * -------------------------------------- + scale factor
863 * M = blank_scale * gradient
866 static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres)
868 u32 c_val, m_val, duty_cycle, hblank;
870 c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 +
871 H_SCALEFACTOR) * 1000;
872 m_val = (H_BLANKSCALE * H_GRADIENT)/256;
873 m_val = (m_val * 1000000)/hfreq;
874 duty_cycle = c_val - m_val;
875 hblank = (xres * duty_cycle)/(100000 - duty_cycle);
880 * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
881 * @dclk: pixelclock in Hz
882 * @xres: horizontal resolution in pixels
887 * hblank = ------------------
890 * duty cycle = percent of htotal assigned to inactive display
891 * duty cycle = C - (M * h_period)
893 * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
894 * -----------------------------------------------
900 static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres)
902 u32 duty_cycle, h_period, hblank;
905 h_period = 100 - C_VAL;
906 h_period *= h_period;
907 h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk);
910 h_period = int_sqrt(h_period);
911 h_period -= (100 - C_VAL) * 100;
913 h_period /= 2 * M_VAL;
915 duty_cycle = C_VAL * 1000 - (M_VAL * h_period)/100;
916 hblank = (xres * duty_cycle)/(100000 - duty_cycle) + 8;
922 * fb_get_hfreq - estimate hsync
923 * @vfreq: vertical refresh rate
924 * @yres: vertical resolution
928 * (yres + front_port) * vfreq * 1000000
929 * hfreq = -------------------------------------
930 * (1000000 - (vfreq * FLYBACK)
934 static u32 fb_get_hfreq(u32 vfreq, u32 yres)
938 divisor = (1000000 - (vfreq * FLYBACK))/1000;
939 hfreq = (yres + V_FRONTPORCH) * vfreq * 1000;
940 return (hfreq/divisor);
943 static void fb_timings_vfreq(struct __fb_timings *timings)
945 timings->hfreq = fb_get_hfreq(timings->vfreq, timings->vactive);
946 timings->vblank = fb_get_vblank(timings->hfreq);
947 timings->vtotal = timings->vactive + timings->vblank;
948 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
950 timings->htotal = timings->hactive + timings->hblank;
951 timings->dclk = timings->htotal * timings->hfreq;
954 static void fb_timings_hfreq(struct __fb_timings *timings)
956 timings->vblank = fb_get_vblank(timings->hfreq);
957 timings->vtotal = timings->vactive + timings->vblank;
958 timings->vfreq = timings->hfreq/timings->vtotal;
959 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
961 timings->htotal = timings->hactive + timings->hblank;
962 timings->dclk = timings->htotal * timings->hfreq;
965 static void fb_timings_dclk(struct __fb_timings *timings)
967 timings->hblank = fb_get_hblank_by_dclk(timings->dclk,
969 timings->htotal = timings->hactive + timings->hblank;
970 timings->hfreq = timings->dclk/timings->htotal;
971 timings->vblank = fb_get_vblank(timings->hfreq);
972 timings->vtotal = timings->vactive + timings->vblank;
973 timings->vfreq = timings->hfreq/timings->vtotal;
977 * fb_get_mode - calculates video mode using VESA GTF
978 * @flags: if: 0 - maximize vertical refresh rate
979 * 1 - vrefresh-driven calculation;
980 * 2 - hscan-driven calculation;
981 * 3 - pixelclock-driven calculation;
982 * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock
983 * @var: pointer to fb_var_screeninfo
984 * @info: pointer to fb_info
987 * Calculates video mode based on monitor specs using VESA GTF.
988 * The GTF is best for VESA GTF compliant monitors but is
989 * specifically formulated to work for older monitors as well.
991 * If @flag==0, the function will attempt to maximize the
992 * refresh rate. Otherwise, it will calculate timings based on
993 * the flag and accompanying value.
995 * If FB_IGNOREMON bit is set in @flags, monitor specs will be
996 * ignored and @var will be filled with the calculated timings.
998 * All calculations are based on the VESA GTF Spreadsheet
999 * available at VESA's public ftp (http://www.vesa.org).
1002 * The timings generated by the GTF will be different from VESA
1003 * DMT. It might be a good idea to keep a table of standard
1004 * VESA modes as well. The GTF may also not work for some displays,
1005 * such as, and especially, analog TV.
1008 * A valid info->monspecs, otherwise 'safe numbers' will be used.
1010 int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, struct fb_info *info)
1012 struct __fb_timings timings;
1013 u32 interlace = 1, dscan = 1;
1014 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
1017 * If monspecs are invalid, use values that are enough
1020 if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
1021 !info->monspecs.dclkmax ||
1022 info->monspecs.hfmax < info->monspecs.hfmin ||
1023 info->monspecs.vfmax < info->monspecs.vfmin ||
1024 info->monspecs.dclkmax < info->monspecs.dclkmin) {
1025 hfmin = 29000; hfmax = 30000;
1026 vfmin = 60; vfmax = 60;
1027 dclkmin = 0; dclkmax = 25000000;
1029 hfmin = info->monspecs.hfmin;
1030 hfmax = info->monspecs.hfmax;
1031 vfmin = info->monspecs.vfmin;
1032 vfmax = info->monspecs.vfmax;
1033 dclkmin = info->monspecs.dclkmin;
1034 dclkmax = info->monspecs.dclkmax;
1037 memset(&timings, 0, sizeof(struct __fb_timings));
1038 timings.hactive = var->xres;
1039 timings.vactive = var->yres;
1040 if (var->vmode & FB_VMODE_INTERLACED) {
1041 timings.vactive /= 2;
1044 if (var->vmode & FB_VMODE_DOUBLE) {
1045 timings.vactive *= 2;
1049 switch (flags & ~FB_IGNOREMON) {
1050 case FB_MAXTIMINGS: /* maximize refresh rate */
1051 timings.hfreq = hfmax;
1052 fb_timings_hfreq(&timings);
1053 if (timings.vfreq > vfmax) {
1054 timings.vfreq = vfmax;
1055 fb_timings_vfreq(&timings);
1057 if (timings.dclk > dclkmax) {
1058 timings.dclk = dclkmax;
1059 fb_timings_dclk(&timings);
1062 case FB_VSYNCTIMINGS: /* vrefresh driven */
1063 timings.vfreq = val;
1064 fb_timings_vfreq(&timings);
1066 case FB_HSYNCTIMINGS: /* hsync driven */
1067 timings.hfreq = val;
1068 fb_timings_hfreq(&timings);
1070 case FB_DCLKTIMINGS: /* pixelclock driven */
1071 timings.dclk = PICOS2KHZ(val) * 1000;
1072 fb_timings_dclk(&timings);
1079 if (!(flags & FB_IGNOREMON) &&
1080 (timings.vfreq < vfmin || timings.vfreq > vfmax ||
1081 timings.hfreq < hfmin || timings.hfreq > hfmax ||
1082 timings.dclk < dclkmin || timings.dclk > dclkmax))
1085 var->pixclock = KHZ2PICOS(timings.dclk/1000);
1086 var->hsync_len = (timings.htotal * 8)/100;
1087 var->right_margin = (timings.hblank/2) - var->hsync_len;
1088 var->left_margin = timings.hblank - var->right_margin - var->hsync_len;
1090 var->vsync_len = (3 * interlace)/dscan;
1091 var->lower_margin = (1 * interlace)/dscan;
1092 var->upper_margin = (timings.vblank * interlace)/dscan -
1093 (var->vsync_len + var->lower_margin);
1099 * fb_validate_mode - validates var against monitor capabilities
1100 * @var: pointer to fb_var_screeninfo
1101 * @info: pointer to fb_info
1104 * Validates video mode against monitor capabilities specified in
1108 * A valid info->monspecs.
1110 int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info)
1112 u32 hfreq, vfreq, htotal, vtotal, pixclock;
1113 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
1116 * If monspecs are invalid, use values that are enough
1119 if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
1120 !info->monspecs.dclkmax ||
1121 info->monspecs.hfmax < info->monspecs.hfmin ||
1122 info->monspecs.vfmax < info->monspecs.vfmin ||
1123 info->monspecs.dclkmax < info->monspecs.dclkmin) {
1124 hfmin = 29000; hfmax = 30000;
1125 vfmin = 60; vfmax = 60;
1126 dclkmin = 0; dclkmax = 25000000;
1128 hfmin = info->monspecs.hfmin;
1129 hfmax = info->monspecs.hfmax;
1130 vfmin = info->monspecs.vfmin;
1131 vfmax = info->monspecs.vfmax;
1132 dclkmin = info->monspecs.dclkmin;
1133 dclkmax = info->monspecs.dclkmax;
1138 pixclock = PICOS2KHZ(var->pixclock) * 1000;
1140 htotal = var->xres + var->right_margin + var->hsync_len +
1142 vtotal = var->yres + var->lower_margin + var->vsync_len +
1145 if (var->vmode & FB_VMODE_INTERLACED)
1147 if (var->vmode & FB_VMODE_DOUBLE)
1150 hfreq = pixclock/htotal;
1151 vfreq = hfreq/vtotal;
1153 return (vfreq < vfmin || vfreq > vfmax ||
1154 hfreq < hfmin || hfreq > hfmax ||
1155 pixclock < dclkmin || pixclock > dclkmax) ?
1159 EXPORT_SYMBOL(fb_parse_edid);
1160 EXPORT_SYMBOL(fb_edid_to_monspecs);
1161 EXPORT_SYMBOL(get_EDID_from_firmware);
1163 EXPORT_SYMBOL(fb_get_mode);
1164 EXPORT_SYMBOL(fb_validate_mode);
1165 EXPORT_SYMBOL(fb_create_modedb);
1166 EXPORT_SYMBOL(fb_destroy_modedb);
1167 EXPORT_SYMBOL(fb_get_monitor_limits);