i2c: tegra: Add stub runtime power management
[linux-2.6.git] / drivers / video / fbmon.c
1 /*
2  * linux/drivers/video/fbmon.c
3  *
4  * Copyright (C) 2002 James Simmons <jsimmons@users.sf.net>
5  *
6  * Credits:
7  *
8  * The EDID Parser is a conglomeration from the following sources:
9  *
10  *   1. SciTech SNAP Graphics Architecture
11  *      Copyright (C) 1991-2002 SciTech Software, Inc. All rights reserved.
12  *
13  *   2. XFree86 4.3.0, interpret_edid.c
14  *      Copyright 1998 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
15  *
16  *   3. John Fremlin <vii@users.sourceforge.net> and
17  *      Ani Joshi <ajoshi@unixbox.com>
18  *
19  * Generalized Timing Formula is derived from:
20  *
21  *      GTF Spreadsheet by Andy Morrish (1/5/97)
22  *      available at http://www.vesa.org
23  *
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
26  * for more details.
27  *
28  */
29 #include <linux/fb.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/slab.h>
33 #include <video/edid.h>
34 #ifdef CONFIG_PPC_OF
35 #include <asm/prom.h>
36 #include <asm/pci-bridge.h>
37 #endif
38 #include "edid.h"
39
40 /*
41  * EDID parser
42  */
43
44 #undef DEBUG  /* define this for verbose EDID parsing output */
45
46 #ifdef DEBUG
47 #define DPRINTK(fmt, args...) printk(fmt,## args)
48 #else
49 #define DPRINTK(fmt, args...)
50 #endif
51
52 #define FBMON_FIX_HEADER  1
53 #define FBMON_FIX_INPUT   2
54 #define FBMON_FIX_TIMINGS 3
55
56 #ifdef CONFIG_FB_MODE_HELPERS
57 struct broken_edid {
58         u8  manufacturer[4];
59         u32 model;
60         u32 fix;
61 };
62
63 static const struct broken_edid brokendb[] = {
64         /* DEC FR-PCXAV-YZ */
65         {
66                 .manufacturer = "DEC",
67                 .model        = 0x073a,
68                 .fix          = FBMON_FIX_HEADER,
69         },
70         /* ViewSonic PF775a */
71         {
72                 .manufacturer = "VSC",
73                 .model        = 0x5a44,
74                 .fix          = FBMON_FIX_INPUT,
75         },
76         /* Sharp UXGA? */
77         {
78                 .manufacturer = "SHP",
79                 .model        = 0x138e,
80                 .fix          = FBMON_FIX_TIMINGS,
81         },
82 };
83
84 static const unsigned char edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
85         0xff, 0xff, 0xff, 0x00
86 };
87
88 static void copy_string(unsigned char *c, unsigned char *s)
89 {
90   int i;
91   c = c + 5;
92   for (i = 0; (i < 13 && *c != 0x0A); i++)
93     *(s++) = *(c++);
94   *s = 0;
95   while (i-- && (*--s == 0x20)) *s = 0;
96 }
97
98 static int edid_is_serial_block(unsigned char *block)
99 {
100         if ((block[0] == 0x00) && (block[1] == 0x00) &&
101             (block[2] == 0x00) && (block[3] == 0xff) &&
102             (block[4] == 0x00))
103                 return 1;
104         else
105                 return 0;
106 }
107
108 static int edid_is_ascii_block(unsigned char *block)
109 {
110         if ((block[0] == 0x00) && (block[1] == 0x00) &&
111             (block[2] == 0x00) && (block[3] == 0xfe) &&
112             (block[4] == 0x00))
113                 return 1;
114         else
115                 return 0;
116 }
117
118 static int edid_is_limits_block(unsigned char *block)
119 {
120         if ((block[0] == 0x00) && (block[1] == 0x00) &&
121             (block[2] == 0x00) && (block[3] == 0xfd) &&
122             (block[4] == 0x00))
123                 return 1;
124         else
125                 return 0;
126 }
127
128 static int edid_is_monitor_block(unsigned char *block)
129 {
130         if ((block[0] == 0x00) && (block[1] == 0x00) &&
131             (block[2] == 0x00) && (block[3] == 0xfc) &&
132             (block[4] == 0x00))
133                 return 1;
134         else
135                 return 0;
136 }
137
138 static int edid_is_timing_block(unsigned char *block)
139 {
140         if ((block[0] != 0x00) || (block[1] != 0x00) ||
141             (block[2] != 0x00) || (block[4] != 0x00))
142                 return 1;
143         else
144                 return 0;
145 }
146
147 static int check_edid(unsigned char *edid)
148 {
149         unsigned char *block = edid + ID_MANUFACTURER_NAME, manufacturer[4];
150         unsigned char *b;
151         u32 model;
152         int i, fix = 0, ret = 0;
153
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) + '@';
158         manufacturer[3] = 0;
159         model = block[2] + (block[3] << 8);
160
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;
165                         break;
166                 }
167         }
168
169         switch (fix) {
170         case FBMON_FIX_HEADER:
171                 for (i = 0; i < 8; i++) {
172                         if (edid[i] != edid_v1_header[i]) {
173                                 ret = fix;
174                                 break;
175                         }
176                 }
177                 break;
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)
183                         ret = fix;
184                 break;
185         case FBMON_FIX_TIMINGS:
186                 b = edid + DETAILED_TIMING_DESCRIPTIONS_START;
187                 ret = fix;
188
189                 for (i = 0; i < 4; i++) {
190                         if (edid_is_limits_block(b)) {
191                                 ret = 0;
192                                 break;
193                         }
194
195                         b += DETAILED_TIMING_DESCRIPTION_SIZE;
196                 }
197
198                 break;
199         }
200
201         if (ret)
202                 printk("fbmon: The EDID Block of "
203                        "Manufacturer: %s Model: 0x%x is known to "
204                        "be broken,\n",  manufacturer, model);
205
206         return ret;
207 }
208
209 static void fix_edid(unsigned char *edid, int fix)
210 {
211         int i;
212         unsigned char *b, csum = 0;
213
214         switch (fix) {
215         case FBMON_FIX_HEADER:
216                 printk("fbmon: trying a header reconstruct\n");
217                 memcpy(edid, edid_v1_header, 8);
218                 break;
219         case FBMON_FIX_INPUT:
220                 printk("fbmon: trying to fix input type\n");
221                 b = edid + EDID_STRUCT_DISPLAY;
222                 b[0] &= ~0x80;
223                 edid[127] += 0x80;
224                 break;
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))) {
233                                 b[0] = 0x00;
234                                 b[1] = 0x00;
235                                 b[2] = 0x00;
236                                 b[3] = 0xfd;
237                                 b[4] = 0x00;
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*/
243                                 b[10] = 0;   /* GTF */
244                                 break;
245                         }
246
247                         b += DETAILED_TIMING_DESCRIPTION_SIZE;
248                 }
249
250                 for (i = 0; i < EDID_LENGTH - 1; i++)
251                         csum += edid[i];
252
253                 edid[127] = 256 - csum;
254                 break;
255         }
256 }
257
258 static int edid_checksum(unsigned char *edid)
259 {
260         unsigned char csum = 0, all_null = 0;
261         int i, err = 0, fix = check_edid(edid);
262
263         if (fix)
264                 fix_edid(edid, fix);
265
266         for (i = 0; i < EDID_LENGTH; i++) {
267                 csum += edid[i];
268                 all_null |= edid[i];
269         }
270
271         if (csum == 0x00 && all_null) {
272                 /* checksum passed, everything's good */
273                 err = 1;
274         }
275
276         return err;
277 }
278
279 static int edid_check_header(unsigned char *edid)
280 {
281         int i, err = 1, fix = check_edid(edid);
282
283         if (fix)
284                 fix_edid(edid, fix);
285
286         for (i = 0; i < 8; i++) {
287                 if (edid[i] != edid_v1_header[i])
288                         err = 0;
289         }
290
291         return err;
292 }
293
294 static void parse_vendor_block(unsigned char *block, struct fb_monspecs *specs)
295 {
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);
310 }
311
312 static void get_dpms_capabilities(unsigned char flags,
313                                   struct fb_monspecs *specs)
314 {
315         specs->dpms = 0;
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");
326 }
327
328 static void get_chroma(unsigned char *block, struct fb_monspecs *specs)
329 {
330         int tmp;
331
332         DPRINTK("      Chroma\n");
333         /* Chromaticity data */
334         tmp = ((block[5] & (3 << 6)) >> 6) | (block[0x7] << 2);
335         tmp *= 1000;
336         tmp += 512;
337         specs->chroma.redx = tmp/1024;
338         DPRINTK("         RedX:     0.%03d ", specs->chroma.redx);
339
340         tmp = ((block[5] & (3 << 4)) >> 4) | (block[0x8] << 2);
341         tmp *= 1000;
342         tmp += 512;
343         specs->chroma.redy = tmp/1024;
344         DPRINTK("RedY:     0.%03d\n", specs->chroma.redy);
345
346         tmp = ((block[5] & (3 << 2)) >> 2) | (block[0x9] << 2);
347         tmp *= 1000;
348         tmp += 512;
349         specs->chroma.greenx = tmp/1024;
350         DPRINTK("         GreenX:   0.%03d ", specs->chroma.greenx);
351
352         tmp = (block[5] & 3) | (block[0xa] << 2);
353         tmp *= 1000;
354         tmp += 512;
355         specs->chroma.greeny = tmp/1024;
356         DPRINTK("GreenY:   0.%03d\n", specs->chroma.greeny);
357
358         tmp = ((block[6] & (3 << 6)) >> 6) | (block[0xb] << 2);
359         tmp *= 1000;
360         tmp += 512;
361         specs->chroma.bluex = tmp/1024;
362         DPRINTK("         BlueX:    0.%03d ", specs->chroma.bluex);
363
364         tmp = ((block[6] & (3 << 4)) >> 4) | (block[0xc] << 2);
365         tmp *= 1000;
366         tmp += 512;
367         specs->chroma.bluey = tmp/1024;
368         DPRINTK("BlueY:    0.%03d\n", specs->chroma.bluey);
369
370         tmp = ((block[6] & (3 << 2)) >> 2) | (block[0xd] << 2);
371         tmp *= 1000;
372         tmp += 512;
373         specs->chroma.whitex = tmp/1024;
374         DPRINTK("         WhiteX:   0.%03d ", specs->chroma.whitex);
375
376         tmp = (block[6] & 3) | (block[0xe] << 2);
377         tmp *= 1000;
378         tmp += 512;
379         specs->chroma.whitey = tmp/1024;
380         DPRINTK("WhiteY:   0.%03d\n", specs->chroma.whitey);
381 }
382
383 static void calc_mode_timings(int xres, int yres, int refresh,
384                               struct fb_videomode *mode)
385 {
386         struct fb_var_screeninfo *var;
387
388         var = kzalloc(sizeof(struct fb_var_screeninfo), GFP_KERNEL);
389
390         if (var) {
391                 var->xres = xres;
392                 var->yres = yres;
393                 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON,
394                             refresh, var, NULL);
395                 mode->xres = xres;
396                 mode->yres = yres;
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;
405                 mode->vmode = 0;
406                 mode->sync = 0;
407                 kfree(var);
408         }
409 }
410
411 static int get_est_timing(unsigned char *block, struct fb_videomode *mode)
412 {
413         int num = 0;
414         unsigned char c;
415
416         c = block[0];
417         if (c&0x80) {
418                 calc_mode_timings(720, 400, 70, &mode[num]);
419                 mode[num++].flag = FB_MODE_IS_CALCULATED;
420                 DPRINTK("      720x400@70Hz\n");
421         }
422         if (c&0x40) {
423                 calc_mode_timings(720, 400, 88, &mode[num]);
424                 mode[num++].flag = FB_MODE_IS_CALCULATED;
425                 DPRINTK("      720x400@88Hz\n");
426         }
427         if (c&0x20) {
428                 mode[num++] = vesa_modes[3];
429                 DPRINTK("      640x480@60Hz\n");
430         }
431         if (c&0x10) {
432                 calc_mode_timings(640, 480, 67, &mode[num]);
433                 mode[num++].flag = FB_MODE_IS_CALCULATED;
434                 DPRINTK("      640x480@67Hz\n");
435         }
436         if (c&0x08) {
437                 mode[num++] = vesa_modes[4];
438                 DPRINTK("      640x480@72Hz\n");
439         }
440         if (c&0x04) {
441                 mode[num++] = vesa_modes[5];
442                 DPRINTK("      640x480@75Hz\n");
443         }
444         if (c&0x02) {
445                 mode[num++] = vesa_modes[7];
446                 DPRINTK("      800x600@56Hz\n");
447         }
448         if (c&0x01) {
449                 mode[num++] = vesa_modes[8];
450                 DPRINTK("      800x600@60Hz\n");
451         }
452
453         c = block[1];
454         if (c&0x80) {
455                 mode[num++] = vesa_modes[9];
456                 DPRINTK("      800x600@72Hz\n");
457         }
458         if (c&0x40) {
459                 mode[num++] = vesa_modes[10];
460                 DPRINTK("      800x600@75Hz\n");
461         }
462         if (c&0x20) {
463                 calc_mode_timings(832, 624, 75, &mode[num]);
464                 mode[num++].flag = FB_MODE_IS_CALCULATED;
465                 DPRINTK("      832x624@75Hz\n");
466         }
467         if (c&0x10) {
468                 mode[num++] = vesa_modes[12];
469                 DPRINTK("      1024x768@87Hz Interlaced\n");
470         }
471         if (c&0x08) {
472                 mode[num++] = vesa_modes[13];
473                 DPRINTK("      1024x768@60Hz\n");
474         }
475         if (c&0x04) {
476                 mode[num++] = vesa_modes[14];
477                 DPRINTK("      1024x768@70Hz\n");
478         }
479         if (c&0x02) {
480                 mode[num++] = vesa_modes[15];
481                 DPRINTK("      1024x768@75Hz\n");
482         }
483         if (c&0x01) {
484                 mode[num++] = vesa_modes[21];
485                 DPRINTK("      1280x1024@75Hz\n");
486         }
487         c = block[2];
488         if (c&0x80) {
489                 mode[num++] = vesa_modes[17];
490                 DPRINTK("      1152x870@75Hz\n");
491         }
492         DPRINTK("      Manufacturer's mask: %x\n",c&0x7F);
493         return num;
494 }
495
496 static int get_std_timing(unsigned char *block, struct fb_videomode *mode)
497 {
498         int xres, yres = 0, refresh, ratio, i;
499
500         xres = (block[0] + 31) * 8;
501         if (xres <= 256)
502                 return 0;
503
504         ratio = (block[1] & 0xc0) >> 6;
505         switch (ratio) {
506         case 0:
507                 yres = xres;
508                 break;
509         case 1:
510                 yres = (xres * 3)/4;
511                 break;
512         case 2:
513                 yres = (xres * 4)/5;
514                 break;
515         case 3:
516                 yres = (xres * 9)/16;
517                 break;
518         }
519         refresh = (block[1] & 0x3f) + 60;
520
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;
528                         return 1;
529                 }
530         }
531         calc_mode_timings(xres, yres, refresh, mode);
532         return 1;
533 }
534
535 static int get_dst_timing(unsigned char *block,
536                           struct fb_videomode *mode)
537 {
538         int j, num = 0;
539
540         for (j = 0; j < 6; j++, block += STD_TIMING_DESCRIPTION_SIZE)
541                 num += get_std_timing(block, &mode[num]);
542
543         return num;
544 }
545
546 static void get_detailed_timing(unsigned char *block,
547                                 struct fb_videomode *mode)
548 {
549         int v_size = V_SIZE;
550         int h_size = H_SIZE;
551
552         mode->xres = H_ACTIVE;
553         mode->yres = V_ACTIVE;
554         mode->pixclock = PIXEL_CLOCK;
555         mode->pixclock /= 1000;
556         mode->pixclock = KHZ2PICOS(mode->pixclock);
557         mode->right_margin = H_SYNC_OFFSET;
558         mode->left_margin = (H_ACTIVE + H_BLANKING) -
559                 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
560         mode->upper_margin = V_BLANKING - V_SYNC_OFFSET -
561                 V_SYNC_WIDTH;
562         mode->lower_margin = V_SYNC_OFFSET;
563         mode->hsync_len = H_SYNC_WIDTH;
564         mode->vsync_len = V_SYNC_WIDTH;
565         if (HSYNC_POSITIVE)
566                 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
567         if (VSYNC_POSITIVE)
568                 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
569         mode->refresh = PIXEL_CLOCK/((H_ACTIVE + H_BLANKING) *
570                                      (V_ACTIVE + V_BLANKING));
571         if (INTERLACED) {
572                 mode->yres *= 2;
573                 mode->upper_margin *= 2;
574                 mode->lower_margin *= 2;
575                 mode->vsync_len *= 2;
576                 mode->vmode |= FB_VMODE_INTERLACED;
577         }
578         mode->flag = FB_MODE_IS_DETAILED;
579
580         /* get aspect ratio */
581         if (h_size * 18 > v_size * 31 && h_size * 18 < v_size * 33)
582                 mode->flag |= FB_FLAG_RATIO_16_9;
583         if (h_size * 18 > v_size * 23 && h_size * 18 < v_size * 25)
584                 mode->flag |= FB_FLAG_RATIO_4_3;
585
586         DPRINTK("      %d MHz ",  PIXEL_CLOCK/1000000);
587         DPRINTK("%d %d %d %d ", H_ACTIVE, H_ACTIVE + H_SYNC_OFFSET,
588                H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH, H_ACTIVE + H_BLANKING);
589         DPRINTK("%d %d %d %d ", V_ACTIVE, V_ACTIVE + V_SYNC_OFFSET,
590                V_ACTIVE + V_SYNC_OFFSET + V_SYNC_WIDTH, V_ACTIVE + V_BLANKING);
591         DPRINTK("%dmm %dmm ", H_SIZE, V_SIZE);
592         DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE) ? "+" : "-",
593                (VSYNC_POSITIVE) ? "+" : "-");
594 }
595
596 /**
597  * fb_create_modedb - create video mode database
598  * @edid: EDID data
599  * @dbsize: database size
600  *
601  * RETURNS: struct fb_videomode, @dbsize contains length of database
602  *
603  * DESCRIPTION:
604  * This function builds a mode database using the contents of the EDID
605  * data
606  */
607 static struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize)
608 {
609         struct fb_videomode *mode, *m;
610         unsigned char *block;
611         int num = 0, i, first = 1;
612
613         mode = kzalloc(50 * sizeof(struct fb_videomode), GFP_KERNEL);
614         if (mode == NULL)
615                 return NULL;
616
617         if (edid == NULL || !edid_checksum(edid) ||
618             !edid_check_header(edid)) {
619                 kfree(mode);
620                 return NULL;
621         }
622
623         *dbsize = 0;
624
625         DPRINTK("   Detailed Timings\n");
626         block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
627         for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) {
628                 if (!(block[0] == 0x00 && block[1] == 0x00)) {
629                         get_detailed_timing(block, &mode[num]);
630                         if (first) {
631                                 mode[num].flag |= FB_MODE_IS_FIRST;
632                                 first = 0;
633                         }
634                         num++;
635                 }
636         }
637
638         DPRINTK("   Supported VESA Modes\n");
639         block = edid + ESTABLISHED_TIMING_1;
640         num += get_est_timing(block, &mode[num]);
641
642         DPRINTK("   Standard Timings\n");
643         block = edid + STD_TIMING_DESCRIPTIONS_START;
644         for (i = 0; i < STD_TIMING; i++, block += STD_TIMING_DESCRIPTION_SIZE)
645                 num += get_std_timing(block, &mode[num]);
646
647         block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
648         for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) {
649                 if (block[0] == 0x00 && block[1] == 0x00 && block[3] == 0xfa)
650                         num += get_dst_timing(block + 5, &mode[num]);
651         }
652
653         /* Yikes, EDID data is totally useless */
654         if (!num) {
655                 kfree(mode);
656                 return NULL;
657         }
658
659         *dbsize = num;
660         m = kmalloc(num * sizeof(struct fb_videomode), GFP_KERNEL);
661         if (!m)
662                 return mode;
663         memmove(m, mode, num * sizeof(struct fb_videomode));
664         kfree(mode);
665         return m;
666 }
667
668 /**
669  * fb_destroy_modedb - destroys mode database
670  * @modedb: mode database to destroy
671  *
672  * DESCRIPTION:
673  * Destroy mode database created by fb_create_modedb
674  */
675 void fb_destroy_modedb(struct fb_videomode *modedb)
676 {
677         kfree(modedb);
678 }
679
680 static int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs)
681 {
682         int i, retval = 1;
683         unsigned char *block;
684
685         block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
686
687         DPRINTK("      Monitor Operating Limits: ");
688
689         for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
690                 if (edid_is_limits_block(block)) {
691                         specs->hfmin = H_MIN_RATE * 1000;
692                         specs->hfmax = H_MAX_RATE * 1000;
693                         specs->vfmin = V_MIN_RATE;
694                         specs->vfmax = V_MAX_RATE;
695                         specs->dclkmax = MAX_PIXEL_CLOCK * 1000000;
696                         specs->gtf = (GTF_SUPPORT) ? 1 : 0;
697                         retval = 0;
698                         DPRINTK("From EDID\n");
699                         break;
700                 }
701         }
702
703         /* estimate monitor limits based on modes supported */
704         if (retval) {
705                 struct fb_videomode *modes, *mode;
706                 int num_modes, hz, hscan, pixclock;
707                 int vtotal, htotal;
708
709                 modes = fb_create_modedb(edid, &num_modes);
710                 if (!modes) {
711                         DPRINTK("None Available\n");
712                         return 1;
713                 }
714
715                 retval = 0;
716                 for (i = 0; i < num_modes; i++) {
717                         mode = &modes[i];
718                         pixclock = PICOS2KHZ(modes[i].pixclock) * 1000;
719                         htotal = mode->xres + mode->right_margin + mode->hsync_len
720                                 + mode->left_margin;
721                         vtotal = mode->yres + mode->lower_margin + mode->vsync_len
722                                 + mode->upper_margin;
723
724                         if (mode->vmode & FB_VMODE_INTERLACED)
725                                 vtotal /= 2;
726
727                         if (mode->vmode & FB_VMODE_DOUBLE)
728                                 vtotal *= 2;
729
730                         hscan = (pixclock + htotal / 2) / htotal;
731                         hscan = (hscan + 500) / 1000 * 1000;
732                         hz = (hscan + vtotal / 2) / vtotal;
733
734                         if (specs->dclkmax == 0 || specs->dclkmax < pixclock)
735                                 specs->dclkmax = pixclock;
736
737                         if (specs->dclkmin == 0 || specs->dclkmin > pixclock)
738                                 specs->dclkmin = pixclock;
739
740                         if (specs->hfmax == 0 || specs->hfmax < hscan)
741                                 specs->hfmax = hscan;
742
743                         if (specs->hfmin == 0 || specs->hfmin > hscan)
744                                 specs->hfmin = hscan;
745
746                         if (specs->vfmax == 0 || specs->vfmax < hz)
747                                 specs->vfmax = hz;
748
749                         if (specs->vfmin == 0 || specs->vfmin > hz)
750                                 specs->vfmin = hz;
751                 }
752                 DPRINTK("Extrapolated\n");
753                 fb_destroy_modedb(modes);
754         }
755         DPRINTK("           H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
756                 specs->hfmin/1000, specs->hfmax/1000, specs->vfmin,
757                 specs->vfmax, specs->dclkmax/1000000);
758         return retval;
759 }
760
761 static void get_monspecs(unsigned char *edid, struct fb_monspecs *specs)
762 {
763         unsigned char c, *block;
764
765         block = edid + EDID_STRUCT_DISPLAY;
766
767         fb_get_monitor_limits(edid, specs);
768
769         c = block[0] & 0x80;
770         specs->input = 0;
771         if (c) {
772                 specs->input |= FB_DISP_DDI;
773                 DPRINTK("      Digital Display Input");
774         } else {
775                 DPRINTK("      Analog Display Input: Input Voltage - ");
776                 switch ((block[0] & 0x60) >> 5) {
777                 case 0:
778                         DPRINTK("0.700V/0.300V");
779                         specs->input |= FB_DISP_ANA_700_300;
780                         break;
781                 case 1:
782                         DPRINTK("0.714V/0.286V");
783                         specs->input |= FB_DISP_ANA_714_286;
784                         break;
785                 case 2:
786                         DPRINTK("1.000V/0.400V");
787                         specs->input |= FB_DISP_ANA_1000_400;
788                         break;
789                 case 3:
790                         DPRINTK("0.700V/0.000V");
791                         specs->input |= FB_DISP_ANA_700_000;
792                         break;
793                 }
794         }
795         DPRINTK("\n      Sync: ");
796         c = block[0] & 0x10;
797         if (c)
798                 DPRINTK("      Configurable signal level\n");
799         c = block[0] & 0x0f;
800         specs->signal = 0;
801         if (c & 0x10) {
802                 DPRINTK("Blank to Blank ");
803                 specs->signal |= FB_SIGNAL_BLANK_BLANK;
804         }
805         if (c & 0x08) {
806                 DPRINTK("Separate ");
807                 specs->signal |= FB_SIGNAL_SEPARATE;
808         }
809         if (c & 0x04) {
810                 DPRINTK("Composite ");
811                 specs->signal |= FB_SIGNAL_COMPOSITE;
812         }
813         if (c & 0x02) {
814                 DPRINTK("Sync on Green ");
815                 specs->signal |= FB_SIGNAL_SYNC_ON_GREEN;
816         }
817         if (c & 0x01) {
818                 DPRINTK("Serration on ");
819                 specs->signal |= FB_SIGNAL_SERRATION_ON;
820         }
821         DPRINTK("\n");
822         specs->max_x = block[1];
823         specs->max_y = block[2];
824         DPRINTK("      Max H-size in cm: ");
825         if (specs->max_x)
826                 DPRINTK("%d\n", specs->max_x);
827         else
828                 DPRINTK("variable\n");
829         DPRINTK("      Max V-size in cm: ");
830         if (specs->max_y)
831                 DPRINTK("%d\n", specs->max_y);
832         else
833                 DPRINTK("variable\n");
834
835         c = block[3];
836         specs->gamma = c+100;
837         DPRINTK("      Gamma: ");
838         DPRINTK("%d.%d\n", specs->gamma/100, specs->gamma % 100);
839
840         get_dpms_capabilities(block[4], specs);
841
842         switch ((block[4] & 0x18) >> 3) {
843         case 0:
844                 DPRINTK("      Monochrome/Grayscale\n");
845                 specs->input |= FB_DISP_MONO;
846                 break;
847         case 1:
848                 DPRINTK("      RGB Color Display\n");
849                 specs->input |= FB_DISP_RGB;
850                 break;
851         case 2:
852                 DPRINTK("      Non-RGB Multicolor Display\n");
853                 specs->input |= FB_DISP_MULTI;
854                 break;
855         default:
856                 DPRINTK("      Unknown\n");
857                 specs->input |= FB_DISP_UNKNOWN;
858                 break;
859         }
860
861         get_chroma(block, specs);
862
863         specs->misc = 0;
864         c = block[4] & 0x7;
865         if (c & 0x04) {
866                 DPRINTK("      Default color format is primary\n");
867                 specs->misc |= FB_MISC_PRIM_COLOR;
868         }
869         if (c & 0x02) {
870                 DPRINTK("      First DETAILED Timing is preferred\n");
871                 specs->misc |= FB_MISC_1ST_DETAIL;
872         }
873         if (c & 0x01) {
874                 printk("      Display is GTF capable\n");
875                 specs->gtf = 1;
876         }
877 }
878
879 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
880 {
881         int i;
882         unsigned char *block;
883
884         if (edid == NULL || var == NULL)
885                 return 1;
886
887         if (!(edid_checksum(edid)))
888                 return 1;
889
890         if (!(edid_check_header(edid)))
891                 return 1;
892
893         block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
894
895         for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
896                 if (edid_is_timing_block(block)) {
897                         var->xres = var->xres_virtual = H_ACTIVE;
898                         var->yres = var->yres_virtual = V_ACTIVE;
899                         var->height = var->width = 0;
900                         var->right_margin = H_SYNC_OFFSET;
901                         var->left_margin = (H_ACTIVE + H_BLANKING) -
902                                 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
903                         var->upper_margin = V_BLANKING - V_SYNC_OFFSET -
904                                 V_SYNC_WIDTH;
905                         var->lower_margin = V_SYNC_OFFSET;
906                         var->hsync_len = H_SYNC_WIDTH;
907                         var->vsync_len = V_SYNC_WIDTH;
908                         var->pixclock = PIXEL_CLOCK;
909                         var->pixclock /= 1000;
910                         var->pixclock = KHZ2PICOS(var->pixclock);
911
912                         if (HSYNC_POSITIVE)
913                                 var->sync |= FB_SYNC_HOR_HIGH_ACT;
914                         if (VSYNC_POSITIVE)
915                                 var->sync |= FB_SYNC_VERT_HIGH_ACT;
916                         return 0;
917                 }
918         }
919         return 1;
920 }
921
922 void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
923 {
924         unsigned char *block;
925         int i, found = 0;
926
927         if (edid == NULL)
928                 return;
929
930         if (!(edid_checksum(edid)))
931                 return;
932
933         if (!(edid_check_header(edid)))
934                 return;
935
936         memset(specs, 0, sizeof(struct fb_monspecs));
937
938         specs->version = edid[EDID_STRUCT_VERSION];
939         specs->revision = edid[EDID_STRUCT_REVISION];
940
941         DPRINTK("========================================\n");
942         DPRINTK("Display Information (EDID)\n");
943         DPRINTK("========================================\n");
944         DPRINTK("   EDID Version %d.%d\n", (int) specs->version,
945                (int) specs->revision);
946
947         parse_vendor_block(edid + ID_MANUFACTURER_NAME, specs);
948
949         block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
950         for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
951                 if (edid_is_serial_block(block)) {
952                         copy_string(block, specs->serial_no);
953                         DPRINTK("   Serial Number: %s\n", specs->serial_no);
954                 } else if (edid_is_ascii_block(block)) {
955                         copy_string(block, specs->ascii);
956                         DPRINTK("   ASCII Block: %s\n", specs->ascii);
957                 } else if (edid_is_monitor_block(block)) {
958                         copy_string(block, specs->monitor);
959                         DPRINTK("   Monitor Name: %s\n", specs->monitor);
960                 }
961         }
962
963         DPRINTK("   Display Characteristics:\n");
964         get_monspecs(edid, specs);
965
966         specs->modedb = fb_create_modedb(edid, &specs->modedb_len);
967
968         /*
969          * Workaround for buggy EDIDs that sets that the first
970          * detailed timing is preferred but has not detailed
971          * timing specified
972          */
973         for (i = 0; i < specs->modedb_len; i++) {
974                 if (specs->modedb[i].flag & FB_MODE_IS_DETAILED) {
975                         found = 1;
976                         break;
977                 }
978         }
979
980         if (!found)
981                 specs->misc &= ~FB_MISC_1ST_DETAIL;
982
983         DPRINTK("========================================\n");
984 }
985
986 /**
987  * fb_edid_add_monspecs() - add monitor video modes from E-EDID data
988  * @edid:       128 byte array with an E-EDID block
989  * @specs:      monitor specs to be extended
990  */
991 void fb_edid_add_monspecs(unsigned char *edid, struct fb_monspecs *specs)
992 {
993         unsigned char *block;
994         struct fb_videomode *m;
995         int num = 0, i;
996         u8 svd[64], edt[(128 - 4) / DETAILED_TIMING_DESCRIPTION_SIZE];
997         u8 pos = 4, svd_n = 0;
998
999         if (!edid)
1000                 return;
1001
1002         if (!edid_checksum(edid))
1003                 return;
1004
1005         if (edid[0] != 0x2 ||
1006             edid[2] < 4 || edid[2] > 128)
1007                 return;
1008
1009         DPRINTK("  Short Video Descriptors\n");
1010
1011         while (pos < edid[2]) {
1012                 u8 len = edid[pos] & 0x1f, type = (edid[pos] >> 5) & 7;
1013                 pr_debug("Data block %u of %u bytes\n", type, len);
1014
1015                 pos++;
1016                 if (type == 2) {
1017                         for (i = pos; i < pos + len; i++) {
1018                                 u8 idx = edid[i] & 0x7f;
1019                                 svd[svd_n++] = idx;
1020                                 pr_debug("N%sative mode #%d\n",
1021                                          edid[i] & 0x80 ? "" : "on-n", idx);
1022                         }
1023                 } else if (type == 3 && len >= 3) {
1024                         u32 ieee_reg = edid[pos] | (edid[pos + 1] << 8) |
1025                                 (edid[pos + 2] << 16);
1026                         if (ieee_reg == 0x000c03)
1027                                 specs->misc |= FB_MISC_HDMI;
1028                 }
1029
1030                 pos += len;
1031         }
1032
1033         block = edid + edid[2];
1034
1035         DPRINTK("  Extended Detailed Timings\n");
1036
1037         for (i = 0; i < (128 - edid[2]) / DETAILED_TIMING_DESCRIPTION_SIZE;
1038              i++, block += DETAILED_TIMING_DESCRIPTION_SIZE)
1039                 if (PIXEL_CLOCK)
1040                         edt[num++] = block - edid;
1041
1042         /* Yikes, EDID data is totally useless */
1043         if (!(num + svd_n))
1044                 return;
1045
1046         m = kzalloc((specs->modedb_len + num + svd_n) *
1047                        sizeof(struct fb_videomode), GFP_KERNEL);
1048
1049         if (!m)
1050                 return;
1051
1052         memcpy(m, specs->modedb, specs->modedb_len * sizeof(struct fb_videomode));
1053
1054         for (i = specs->modedb_len; i < specs->modedb_len + num; i++) {
1055                 get_detailed_timing(edid + edt[i - specs->modedb_len], &m[i]);
1056                 if (i == specs->modedb_len)
1057                         m[i].flag |= FB_MODE_IS_FIRST;
1058                 pr_debug("Adding %ux%u@%u\n", m[i].xres, m[i].yres, m[i].refresh);
1059         }
1060
1061         for (i = specs->modedb_len + num; i < specs->modedb_len + num + svd_n; i++) {
1062                 int idx = svd[i - specs->modedb_len - num];
1063                 if (!idx || idx > (CEA_MODEDB_SIZE - 1)) {
1064                         pr_warning("Reserved SVD code %d\n", idx);
1065                 } else {
1066                         memcpy(&m[i], cea_modes + idx, sizeof(m[i]));
1067                         pr_debug("Adding SVD #%d: %ux%u@%u\n", idx,
1068                                  m[i].xres, m[i].yres, m[i].refresh);
1069                 }
1070         }
1071
1072         kfree(specs->modedb);
1073         specs->modedb = m;
1074         specs->modedb_len = specs->modedb_len + num + svd_n;
1075 }
1076
1077 /*
1078  * VESA Generalized Timing Formula (GTF)
1079  */
1080
1081 #define FLYBACK                     550
1082 #define V_FRONTPORCH                1
1083 #define H_OFFSET                    40
1084 #define H_SCALEFACTOR               20
1085 #define H_BLANKSCALE                128
1086 #define H_GRADIENT                  600
1087 #define C_VAL                       30
1088 #define M_VAL                       300
1089
1090 struct __fb_timings {
1091         u32 dclk;
1092         u32 hfreq;
1093         u32 vfreq;
1094         u32 hactive;
1095         u32 vactive;
1096         u32 hblank;
1097         u32 vblank;
1098         u32 htotal;
1099         u32 vtotal;
1100 };
1101
1102 /**
1103  * fb_get_vblank - get vertical blank time
1104  * @hfreq: horizontal freq
1105  *
1106  * DESCRIPTION:
1107  * vblank = right_margin + vsync_len + left_margin
1108  *
1109  *    given: right_margin = 1 (V_FRONTPORCH)
1110  *           vsync_len    = 3
1111  *           flyback      = 550
1112  *
1113  *                          flyback * hfreq
1114  *           left_margin  = --------------- - vsync_len
1115  *                           1000000
1116  */
1117 static u32 fb_get_vblank(u32 hfreq)
1118 {
1119         u32 vblank;
1120
1121         vblank = (hfreq * FLYBACK)/1000;
1122         vblank = (vblank + 500)/1000;
1123         return (vblank + V_FRONTPORCH);
1124 }
1125
1126 /**
1127  * fb_get_hblank_by_freq - get horizontal blank time given hfreq
1128  * @hfreq: horizontal freq
1129  * @xres: horizontal resolution in pixels
1130  *
1131  * DESCRIPTION:
1132  *
1133  *           xres * duty_cycle
1134  * hblank = ------------------
1135  *           100 - duty_cycle
1136  *
1137  * duty cycle = percent of htotal assigned to inactive display
1138  * duty cycle = C - (M/Hfreq)
1139  *
1140  * where: C = ((offset - scale factor) * blank_scale)
1141  *            -------------------------------------- + scale factor
1142  *                        256
1143  *        M = blank_scale * gradient
1144  *
1145  */
1146 static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres)
1147 {
1148         u32 c_val, m_val, duty_cycle, hblank;
1149
1150         c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 +
1151                  H_SCALEFACTOR) * 1000;
1152         m_val = (H_BLANKSCALE * H_GRADIENT)/256;
1153         m_val = (m_val * 1000000)/hfreq;
1154         duty_cycle = c_val - m_val;
1155         hblank = (xres * duty_cycle)/(100000 - duty_cycle);
1156         return (hblank);
1157 }
1158
1159 /**
1160  * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
1161  * @dclk: pixelclock in Hz
1162  * @xres: horizontal resolution in pixels
1163  *
1164  * DESCRIPTION:
1165  *
1166  *           xres * duty_cycle
1167  * hblank = ------------------
1168  *           100 - duty_cycle
1169  *
1170  * duty cycle = percent of htotal assigned to inactive display
1171  * duty cycle = C - (M * h_period)
1172  *
1173  * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
1174  *                   -----------------------------------------------
1175  *                                    2 * M
1176  *        M = 300;
1177  *        C = 30;
1178
1179  */
1180 static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres)
1181 {
1182         u32 duty_cycle, h_period, hblank;
1183
1184         dclk /= 1000;
1185         h_period = 100 - C_VAL;
1186         h_period *= h_period;
1187         h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk);
1188         h_period *= 10000;
1189
1190         h_period = int_sqrt(h_period);
1191         h_period -= (100 - C_VAL) * 100;
1192         h_period *= 1000;
1193         h_period /= 2 * M_VAL;
1194
1195         duty_cycle = C_VAL * 1000 - (M_VAL * h_period)/100;
1196         hblank = (xres * duty_cycle)/(100000 - duty_cycle) + 8;
1197         hblank &= ~15;
1198         return (hblank);
1199 }
1200
1201 /**
1202  * fb_get_hfreq - estimate hsync
1203  * @vfreq: vertical refresh rate
1204  * @yres: vertical resolution
1205  *
1206  * DESCRIPTION:
1207  *
1208  *          (yres + front_port) * vfreq * 1000000
1209  * hfreq = -------------------------------------
1210  *          (1000000 - (vfreq * FLYBACK)
1211  *
1212  */
1213
1214 static u32 fb_get_hfreq(u32 vfreq, u32 yres)
1215 {
1216         u32 divisor, hfreq;
1217
1218         divisor = (1000000 - (vfreq * FLYBACK))/1000;
1219         hfreq = (yres + V_FRONTPORCH) * vfreq  * 1000;
1220         return (hfreq/divisor);
1221 }
1222
1223 static void fb_timings_vfreq(struct __fb_timings *timings)
1224 {
1225         timings->hfreq = fb_get_hfreq(timings->vfreq, timings->vactive);
1226         timings->vblank = fb_get_vblank(timings->hfreq);
1227         timings->vtotal = timings->vactive + timings->vblank;
1228         timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
1229                                                  timings->hactive);
1230         timings->htotal = timings->hactive + timings->hblank;
1231         timings->dclk = timings->htotal * timings->hfreq;
1232 }
1233
1234 static void fb_timings_hfreq(struct __fb_timings *timings)
1235 {
1236         timings->vblank = fb_get_vblank(timings->hfreq);
1237         timings->vtotal = timings->vactive + timings->vblank;
1238         timings->vfreq = timings->hfreq/timings->vtotal;
1239         timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
1240                                                  timings->hactive);
1241         timings->htotal = timings->hactive + timings->hblank;
1242         timings->dclk = timings->htotal * timings->hfreq;
1243 }
1244
1245 static void fb_timings_dclk(struct __fb_timings *timings)
1246 {
1247         timings->hblank = fb_get_hblank_by_dclk(timings->dclk,
1248                                                 timings->hactive);
1249         timings->htotal = timings->hactive + timings->hblank;
1250         timings->hfreq = timings->dclk/timings->htotal;
1251         timings->vblank = fb_get_vblank(timings->hfreq);
1252         timings->vtotal = timings->vactive + timings->vblank;
1253         timings->vfreq = timings->hfreq/timings->vtotal;
1254 }
1255
1256 /*
1257  * fb_get_mode - calculates video mode using VESA GTF
1258  * @flags: if: 0 - maximize vertical refresh rate
1259  *             1 - vrefresh-driven calculation;
1260  *             2 - hscan-driven calculation;
1261  *             3 - pixelclock-driven calculation;
1262  * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock
1263  * @var: pointer to fb_var_screeninfo
1264  * @info: pointer to fb_info
1265  *
1266  * DESCRIPTION:
1267  * Calculates video mode based on monitor specs using VESA GTF.
1268  * The GTF is best for VESA GTF compliant monitors but is
1269  * specifically formulated to work for older monitors as well.
1270  *
1271  * If @flag==0, the function will attempt to maximize the
1272  * refresh rate.  Otherwise, it will calculate timings based on
1273  * the flag and accompanying value.
1274  *
1275  * If FB_IGNOREMON bit is set in @flags, monitor specs will be
1276  * ignored and @var will be filled with the calculated timings.
1277  *
1278  * All calculations are based on the VESA GTF Spreadsheet
1279  * available at VESA's public ftp (http://www.vesa.org).
1280  *
1281  * NOTES:
1282  * The timings generated by the GTF will be different from VESA
1283  * DMT.  It might be a good idea to keep a table of standard
1284  * VESA modes as well.  The GTF may also not work for some displays,
1285  * such as, and especially, analog TV.
1286  *
1287  * REQUIRES:
1288  * A valid info->monspecs, otherwise 'safe numbers' will be used.
1289  */
1290 int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, struct fb_info *info)
1291 {
1292         struct __fb_timings *timings;
1293         u32 interlace = 1, dscan = 1;
1294         u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax, err = 0;
1295
1296
1297         timings = kzalloc(sizeof(struct __fb_timings), GFP_KERNEL);
1298
1299         if (!timings)
1300                 return -ENOMEM;
1301
1302         /*
1303          * If monspecs are invalid, use values that are enough
1304          * for 640x480@60
1305          */
1306         if (!info || !info->monspecs.hfmax || !info->monspecs.vfmax ||
1307             !info->monspecs.dclkmax ||
1308             info->monspecs.hfmax < info->monspecs.hfmin ||
1309             info->monspecs.vfmax < info->monspecs.vfmin ||
1310             info->monspecs.dclkmax < info->monspecs.dclkmin) {
1311                 hfmin = 29000; hfmax = 30000;
1312                 vfmin = 60; vfmax = 60;
1313                 dclkmin = 0; dclkmax = 25000000;
1314         } else {
1315                 hfmin = info->monspecs.hfmin;
1316                 hfmax = info->monspecs.hfmax;
1317                 vfmin = info->monspecs.vfmin;
1318                 vfmax = info->monspecs.vfmax;
1319                 dclkmin = info->monspecs.dclkmin;
1320                 dclkmax = info->monspecs.dclkmax;
1321         }
1322
1323         timings->hactive = var->xres;
1324         timings->vactive = var->yres;
1325         if (var->vmode & FB_VMODE_INTERLACED) {
1326                 timings->vactive /= 2;
1327                 interlace = 2;
1328         }
1329         if (var->vmode & FB_VMODE_DOUBLE) {
1330                 timings->vactive *= 2;
1331                 dscan = 2;
1332         }
1333
1334         switch (flags & ~FB_IGNOREMON) {
1335         case FB_MAXTIMINGS: /* maximize refresh rate */
1336                 timings->hfreq = hfmax;
1337                 fb_timings_hfreq(timings);
1338                 if (timings->vfreq > vfmax) {
1339                         timings->vfreq = vfmax;
1340                         fb_timings_vfreq(timings);
1341                 }
1342                 if (timings->dclk > dclkmax) {
1343                         timings->dclk = dclkmax;
1344                         fb_timings_dclk(timings);
1345                 }
1346                 break;
1347         case FB_VSYNCTIMINGS: /* vrefresh driven */
1348                 timings->vfreq = val;
1349                 fb_timings_vfreq(timings);
1350                 break;
1351         case FB_HSYNCTIMINGS: /* hsync driven */
1352                 timings->hfreq = val;
1353                 fb_timings_hfreq(timings);
1354                 break;
1355         case FB_DCLKTIMINGS: /* pixelclock driven */
1356                 timings->dclk = PICOS2KHZ(val) * 1000;
1357                 fb_timings_dclk(timings);
1358                 break;
1359         default:
1360                 err = -EINVAL;
1361
1362         }
1363
1364         if (err || (!(flags & FB_IGNOREMON) &&
1365             (timings->vfreq < vfmin || timings->vfreq > vfmax ||
1366              timings->hfreq < hfmin || timings->hfreq > hfmax ||
1367              timings->dclk < dclkmin || timings->dclk > dclkmax))) {
1368                 err = -EINVAL;
1369         } else {
1370                 var->pixclock = KHZ2PICOS(timings->dclk/1000);
1371                 var->hsync_len = (timings->htotal * 8)/100;
1372                 var->right_margin = (timings->hblank/2) - var->hsync_len;
1373                 var->left_margin = timings->hblank - var->right_margin -
1374                         var->hsync_len;
1375                 var->vsync_len = (3 * interlace)/dscan;
1376                 var->lower_margin = (1 * interlace)/dscan;
1377                 var->upper_margin = (timings->vblank * interlace)/dscan -
1378                         (var->vsync_len + var->lower_margin);
1379         }
1380
1381         kfree(timings);
1382         return err;
1383 }
1384 #else
1385 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
1386 {
1387         return 1;
1388 }
1389 void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
1390 {
1391         specs = NULL;
1392 }
1393 void fb_edid_add_monspecs(unsigned char *edid, struct fb_monspecs *specs)
1394 {
1395 }
1396 void fb_destroy_modedb(struct fb_videomode *modedb)
1397 {
1398 }
1399 int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var,
1400                 struct fb_info *info)
1401 {
1402         return -EINVAL;
1403 }
1404 #endif /* CONFIG_FB_MODE_HELPERS */
1405
1406 /*
1407  * fb_validate_mode - validates var against monitor capabilities
1408  * @var: pointer to fb_var_screeninfo
1409  * @info: pointer to fb_info
1410  *
1411  * DESCRIPTION:
1412  * Validates video mode against monitor capabilities specified in
1413  * info->monspecs.
1414  *
1415  * REQUIRES:
1416  * A valid info->monspecs.
1417  */
1418 int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info)
1419 {
1420         u32 hfreq, vfreq, htotal, vtotal, pixclock;
1421         u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
1422
1423         /*
1424          * If monspecs are invalid, use values that are enough
1425          * for 640x480@60
1426          */
1427         if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
1428             !info->monspecs.dclkmax ||
1429             info->monspecs.hfmax < info->monspecs.hfmin ||
1430             info->monspecs.vfmax < info->monspecs.vfmin ||
1431             info->monspecs.dclkmax < info->monspecs.dclkmin) {
1432                 hfmin = 29000; hfmax = 30000;
1433                 vfmin = 60; vfmax = 60;
1434                 dclkmin = 0; dclkmax = 25000000;
1435         } else {
1436                 hfmin = info->monspecs.hfmin;
1437                 hfmax = info->monspecs.hfmax;
1438                 vfmin = info->monspecs.vfmin;
1439                 vfmax = info->monspecs.vfmax;
1440                 dclkmin = info->monspecs.dclkmin;
1441                 dclkmax = info->monspecs.dclkmax;
1442         }
1443
1444         if (!var->pixclock)
1445                 return -EINVAL;
1446         pixclock = PICOS2KHZ(var->pixclock) * 1000;
1447
1448         htotal = var->xres + var->right_margin + var->hsync_len +
1449                 var->left_margin;
1450         vtotal = var->yres + var->lower_margin + var->vsync_len +
1451                 var->upper_margin;
1452
1453         if (var->vmode & FB_VMODE_INTERLACED)
1454                 vtotal /= 2;
1455         if (var->vmode & FB_VMODE_DOUBLE)
1456                 vtotal *= 2;
1457
1458         hfreq = pixclock/htotal;
1459         hfreq = (hfreq + 500) / 1000 * 1000;
1460
1461         vfreq = hfreq/vtotal;
1462
1463         return (vfreq < vfmin || vfreq > vfmax ||
1464                 hfreq < hfmin || hfreq > hfmax ||
1465                 pixclock < dclkmin || pixclock > dclkmax) ?
1466                 -EINVAL : 0;
1467 }
1468
1469 #if defined(CONFIG_FIRMWARE_EDID) && defined(CONFIG_X86)
1470
1471 /*
1472  * We need to ensure that the EDID block is only returned for
1473  * the primary graphics adapter.
1474  */
1475
1476 const unsigned char *fb_firmware_edid(struct device *device)
1477 {
1478         struct pci_dev *dev = NULL;
1479         struct resource *res = NULL;
1480         unsigned char *edid = NULL;
1481
1482         if (device)
1483                 dev = to_pci_dev(device);
1484
1485         if (dev)
1486                 res = &dev->resource[PCI_ROM_RESOURCE];
1487
1488         if (res && res->flags & IORESOURCE_ROM_SHADOW)
1489                 edid = edid_info.dummy;
1490
1491         return edid;
1492 }
1493 #else
1494 const unsigned char *fb_firmware_edid(struct device *device)
1495 {
1496         return NULL;
1497 }
1498 #endif
1499 EXPORT_SYMBOL(fb_firmware_edid);
1500
1501 EXPORT_SYMBOL(fb_parse_edid);
1502 EXPORT_SYMBOL(fb_edid_to_monspecs);
1503 EXPORT_SYMBOL(fb_edid_add_monspecs);
1504 EXPORT_SYMBOL(fb_get_mode);
1505 EXPORT_SYMBOL(fb_validate_mode);
1506 EXPORT_SYMBOL(fb_destroy_modedb);