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