blob: 442a52d10913a59afc58ccf57ba2e8b81ea56e8f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/tty.h>
30#include <linux/fb.h>
31#include <linux/module.h>
Antonino A. Daplas5e518d72005-09-09 13:04:34 -070032#include <video/edid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#ifdef CONFIG_PPC_OF
34#include <linux/pci.h>
35#include <asm/prom.h>
36#include <asm/pci-bridge.h>
37#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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
55#ifdef CONFIG_FB_MODE_HELPERS
56struct broken_edid {
57 u8 manufacturer[4];
58 u32 model;
59 u32 fix;
60};
61
62static struct broken_edid brokendb[] = {
63 /* DEC FR-PCXAV-YZ */
64 {
65 .manufacturer = "DEC",
66 .model = 0x073a,
67 .fix = FBMON_FIX_HEADER,
68 },
69 /* ViewSonic PF775a */
70 {
71 .manufacturer = "VSC",
72 .model = 0x5a44,
73 .fix = FBMON_FIX_INPUT,
74 },
75};
76
77static const unsigned char edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0x00
79};
80
81static void copy_string(unsigned char *c, unsigned char *s)
82{
83 int i;
84 c = c + 5;
85 for (i = 0; (i < 13 && *c != 0x0A); i++)
86 *(s++) = *(c++);
87 *s = 0;
88 while (i-- && (*--s == 0x20)) *s = 0;
89}
90
91static int check_edid(unsigned char *edid)
92{
93 unsigned char *block = edid + ID_MANUFACTURER_NAME, manufacturer[4];
94 unsigned char *b;
95 u32 model;
96 int i, fix = 0, ret = 0;
97
98 manufacturer[0] = ((block[0] & 0x7c) >> 2) + '@';
99 manufacturer[1] = ((block[0] & 0x03) << 3) +
100 ((block[1] & 0xe0) >> 5) + '@';
101 manufacturer[2] = (block[1] & 0x1f) + '@';
102 manufacturer[3] = 0;
103 model = block[2] + (block[3] << 8);
104
105 for (i = 0; i < ARRAY_SIZE(brokendb); i++) {
106 if (!strncmp(manufacturer, brokendb[i].manufacturer, 4) &&
107 brokendb[i].model == model) {
108 printk("fbmon: The EDID Block of "
109 "Manufacturer: %s Model: 0x%x is known to "
110 "be broken,\n", manufacturer, model);
111 fix = brokendb[i].fix;
112 break;
113 }
114 }
115
116 switch (fix) {
117 case FBMON_FIX_HEADER:
118 for (i = 0; i < 8; i++) {
119 if (edid[i] != edid_v1_header[i])
120 ret = fix;
121 }
122 break;
123 case FBMON_FIX_INPUT:
124 b = edid + EDID_STRUCT_DISPLAY;
125 /* Only if display is GTF capable will
126 the input type be reset to analog */
127 if (b[4] & 0x01 && b[0] & 0x80)
128 ret = fix;
129 break;
130 }
131
132 return ret;
133}
134
135static void fix_edid(unsigned char *edid, int fix)
136{
137 unsigned char *b;
138
139 switch (fix) {
140 case FBMON_FIX_HEADER:
141 printk("fbmon: trying a header reconstruct\n");
142 memcpy(edid, edid_v1_header, 8);
143 break;
144 case FBMON_FIX_INPUT:
145 printk("fbmon: trying to fix input type\n");
146 b = edid + EDID_STRUCT_DISPLAY;
147 b[0] &= ~0x80;
148 edid[127] += 0x80;
149 }
150}
151
152static int edid_checksum(unsigned char *edid)
153{
154 unsigned char i, csum = 0, all_null = 0;
155 int err = 0, fix = check_edid(edid);
156
157 if (fix)
158 fix_edid(edid, fix);
159
160 for (i = 0; i < EDID_LENGTH; i++) {
161 csum += edid[i];
162 all_null |= edid[i];
163 }
164
165 if (csum == 0x00 && all_null) {
166 /* checksum passed, everything's good */
167 err = 1;
168 }
169
170 return err;
171}
172
173static int edid_check_header(unsigned char *edid)
174{
175 int i, err = 1, fix = check_edid(edid);
176
177 if (fix)
178 fix_edid(edid, fix);
179
180 for (i = 0; i < 8; i++) {
181 if (edid[i] != edid_v1_header[i])
182 err = 0;
183 }
184
185 return err;
186}
187
188static void parse_vendor_block(unsigned char *block, struct fb_monspecs *specs)
189{
190 specs->manufacturer[0] = ((block[0] & 0x7c) >> 2) + '@';
191 specs->manufacturer[1] = ((block[0] & 0x03) << 3) +
192 ((block[1] & 0xe0) >> 5) + '@';
193 specs->manufacturer[2] = (block[1] & 0x1f) + '@';
194 specs->manufacturer[3] = 0;
195 specs->model = block[2] + (block[3] << 8);
196 specs->serial = block[4] + (block[5] << 8) +
197 (block[6] << 16) + (block[7] << 24);
198 specs->year = block[9] + 1990;
199 specs->week = block[8];
200 DPRINTK(" Manufacturer: %s\n", specs->manufacturer);
201 DPRINTK(" Model: %x\n", specs->model);
202 DPRINTK(" Serial#: %u\n", specs->serial);
203 DPRINTK(" Year: %u Week %u\n", specs->year, specs->week);
204}
205
206static void get_dpms_capabilities(unsigned char flags,
207 struct fb_monspecs *specs)
208{
209 specs->dpms = 0;
210 if (flags & DPMS_ACTIVE_OFF)
211 specs->dpms |= FB_DPMS_ACTIVE_OFF;
212 if (flags & DPMS_SUSPEND)
213 specs->dpms |= FB_DPMS_SUSPEND;
214 if (flags & DPMS_STANDBY)
215 specs->dpms |= FB_DPMS_STANDBY;
216 DPRINTK(" DPMS: Active %s, Suspend %s, Standby %s\n",
217 (flags & DPMS_ACTIVE_OFF) ? "yes" : "no",
218 (flags & DPMS_SUSPEND) ? "yes" : "no",
219 (flags & DPMS_STANDBY) ? "yes" : "no");
220}
221
222static void get_chroma(unsigned char *block, struct fb_monspecs *specs)
223{
224 int tmp;
225
226 DPRINTK(" Chroma\n");
227 /* Chromaticity data */
228 tmp = ((block[5] & (3 << 6)) >> 6) | (block[0x7] << 2);
229 tmp *= 1000;
230 tmp += 512;
231 specs->chroma.redx = tmp/1024;
232 DPRINTK(" RedX: 0.%03d ", specs->chroma.redx);
233
234 tmp = ((block[5] & (3 << 4)) >> 4) | (block[0x8] << 2);
235 tmp *= 1000;
236 tmp += 512;
237 specs->chroma.redy = tmp/1024;
238 DPRINTK("RedY: 0.%03d\n", specs->chroma.redy);
239
240 tmp = ((block[5] & (3 << 2)) >> 2) | (block[0x9] << 2);
241 tmp *= 1000;
242 tmp += 512;
243 specs->chroma.greenx = tmp/1024;
244 DPRINTK(" GreenX: 0.%03d ", specs->chroma.greenx);
245
246 tmp = (block[5] & 3) | (block[0xa] << 2);
247 tmp *= 1000;
248 tmp += 512;
249 specs->chroma.greeny = tmp/1024;
250 DPRINTK("GreenY: 0.%03d\n", specs->chroma.greeny);
251
252 tmp = ((block[6] & (3 << 6)) >> 6) | (block[0xb] << 2);
253 tmp *= 1000;
254 tmp += 512;
255 specs->chroma.bluex = tmp/1024;
256 DPRINTK(" BlueX: 0.%03d ", specs->chroma.bluex);
257
258 tmp = ((block[6] & (3 << 4)) >> 4) | (block[0xc] << 2);
259 tmp *= 1000;
260 tmp += 512;
261 specs->chroma.bluey = tmp/1024;
262 DPRINTK("BlueY: 0.%03d\n", specs->chroma.bluey);
263
264 tmp = ((block[6] & (3 << 2)) >> 2) | (block[0xd] << 2);
265 tmp *= 1000;
266 tmp += 512;
267 specs->chroma.whitex = tmp/1024;
268 DPRINTK(" WhiteX: 0.%03d ", specs->chroma.whitex);
269
270 tmp = (block[6] & 3) | (block[0xe] << 2);
271 tmp *= 1000;
272 tmp += 512;
273 specs->chroma.whitey = tmp/1024;
274 DPRINTK("WhiteY: 0.%03d\n", specs->chroma.whitey);
275}
276
277static int edid_is_serial_block(unsigned char *block)
278{
279 if ((block[0] == 0x00) && (block[1] == 0x00) &&
280 (block[2] == 0x00) && (block[3] == 0xff) &&
281 (block[4] == 0x00))
282 return 1;
283 else
284 return 0;
285}
286
287static int edid_is_ascii_block(unsigned char *block)
288{
289 if ((block[0] == 0x00) && (block[1] == 0x00) &&
290 (block[2] == 0x00) && (block[3] == 0xfe) &&
291 (block[4] == 0x00))
292 return 1;
293 else
294 return 0;
295}
296
297static int edid_is_limits_block(unsigned char *block)
298{
299 if ((block[0] == 0x00) && (block[1] == 0x00) &&
300 (block[2] == 0x00) && (block[3] == 0xfd) &&
301 (block[4] == 0x00))
302 return 1;
303 else
304 return 0;
305}
306
307static int edid_is_monitor_block(unsigned char *block)
308{
309 if ((block[0] == 0x00) && (block[1] == 0x00) &&
310 (block[2] == 0x00) && (block[3] == 0xfc) &&
311 (block[4] == 0x00))
312 return 1;
313 else
314 return 0;
315}
316
Antonino A. Daplas61ab7902005-09-09 13:10:02 -0700317static void calc_mode_timings(int xres, int yres, int refresh,
318 struct fb_videomode *mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct fb_var_screeninfo var;
321 struct fb_info info;
322
Antonino A. Daplas61ab7902005-09-09 13:10:02 -0700323 memset(&var, 0, sizeof(struct fb_var_screeninfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 var.xres = xres;
325 var.yres = yres;
326 fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON,
327 refresh, &var, &info);
328 mode->xres = xres;
329 mode->yres = yres;
330 mode->pixclock = var.pixclock;
331 mode->refresh = refresh;
332 mode->left_margin = var.left_margin;
333 mode->right_margin = var.right_margin;
334 mode->upper_margin = var.upper_margin;
335 mode->lower_margin = var.lower_margin;
336 mode->hsync_len = var.hsync_len;
337 mode->vsync_len = var.vsync_len;
338 mode->vmode = 0;
339 mode->sync = 0;
340}
341
342static int get_est_timing(unsigned char *block, struct fb_videomode *mode)
343{
344 int num = 0;
345 unsigned char c;
346
347 c = block[0];
348 if (c&0x80) {
349 calc_mode_timings(720, 400, 70, &mode[num]);
350 mode[num++].flag = FB_MODE_IS_CALCULATED;
351 DPRINTK(" 720x400@70Hz\n");
352 }
353 if (c&0x40) {
354 calc_mode_timings(720, 400, 88, &mode[num]);
355 mode[num++].flag = FB_MODE_IS_CALCULATED;
356 DPRINTK(" 720x400@88Hz\n");
357 }
358 if (c&0x20) {
359 mode[num++] = vesa_modes[3];
360 DPRINTK(" 640x480@60Hz\n");
361 }
362 if (c&0x10) {
363 calc_mode_timings(640, 480, 67, &mode[num]);
364 mode[num++].flag = FB_MODE_IS_CALCULATED;
365 DPRINTK(" 640x480@67Hz\n");
366 }
367 if (c&0x08) {
368 mode[num++] = vesa_modes[4];
369 DPRINTK(" 640x480@72Hz\n");
370 }
371 if (c&0x04) {
372 mode[num++] = vesa_modes[5];
373 DPRINTK(" 640x480@75Hz\n");
374 }
375 if (c&0x02) {
376 mode[num++] = vesa_modes[7];
377 DPRINTK(" 800x600@56Hz\n");
378 }
379 if (c&0x01) {
380 mode[num++] = vesa_modes[8];
381 DPRINTK(" 800x600@60Hz\n");
382 }
383
384 c = block[1];
385 if (c&0x80) {
386 mode[num++] = vesa_modes[9];
387 DPRINTK(" 800x600@72Hz\n");
388 }
389 if (c&0x40) {
390 mode[num++] = vesa_modes[10];
391 DPRINTK(" 800x600@75Hz\n");
392 }
393 if (c&0x20) {
394 calc_mode_timings(832, 624, 75, &mode[num]);
395 mode[num++].flag = FB_MODE_IS_CALCULATED;
396 DPRINTK(" 832x624@75Hz\n");
397 }
398 if (c&0x10) {
399 mode[num++] = vesa_modes[12];
400 DPRINTK(" 1024x768@87Hz Interlaced\n");
401 }
402 if (c&0x08) {
403 mode[num++] = vesa_modes[13];
404 DPRINTK(" 1024x768@60Hz\n");
405 }
406 if (c&0x04) {
407 mode[num++] = vesa_modes[14];
408 DPRINTK(" 1024x768@70Hz\n");
409 }
410 if (c&0x02) {
411 mode[num++] = vesa_modes[15];
412 DPRINTK(" 1024x768@75Hz\n");
413 }
414 if (c&0x01) {
415 mode[num++] = vesa_modes[21];
416 DPRINTK(" 1280x1024@75Hz\n");
417 }
418 c = block[2];
419 if (c&0x80) {
420 mode[num++] = vesa_modes[17];
421 DPRINTK(" 1152x870@75Hz\n");
422 }
423 DPRINTK(" Manufacturer's mask: %x\n",c&0x7F);
424 return num;
425}
426
427static int get_std_timing(unsigned char *block, struct fb_videomode *mode)
428{
429 int xres, yres = 0, refresh, ratio, i;
430
431 xres = (block[0] + 31) * 8;
432 if (xres <= 256)
433 return 0;
434
435 ratio = (block[1] & 0xc0) >> 6;
436 switch (ratio) {
437 case 0:
438 yres = xres;
439 break;
440 case 1:
441 yres = (xres * 3)/4;
442 break;
443 case 2:
444 yres = (xres * 4)/5;
445 break;
446 case 3:
447 yres = (xres * 9)/16;
448 break;
449 }
450 refresh = (block[1] & 0x3f) + 60;
451
452 DPRINTK(" %dx%d@%dHz\n", xres, yres, refresh);
453 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
454 if (vesa_modes[i].xres == xres &&
455 vesa_modes[i].yres == yres &&
456 vesa_modes[i].refresh == refresh) {
457 *mode = vesa_modes[i];
458 mode->flag |= FB_MODE_IS_STANDARD;
459 return 1;
460 }
461 }
462 calc_mode_timings(xres, yres, refresh, mode);
463 return 1;
464}
465
466static int get_dst_timing(unsigned char *block,
467 struct fb_videomode *mode)
468{
469 int j, num = 0;
470
471 for (j = 0; j < 6; j++, block+= STD_TIMING_DESCRIPTION_SIZE)
472 num += get_std_timing(block, &mode[num]);
473
474 return num;
475}
476
477static void get_detailed_timing(unsigned char *block,
478 struct fb_videomode *mode)
479{
480 mode->xres = H_ACTIVE;
481 mode->yres = V_ACTIVE;
482 mode->pixclock = PIXEL_CLOCK;
483 mode->pixclock /= 1000;
484 mode->pixclock = KHZ2PICOS(mode->pixclock);
485 mode->right_margin = H_SYNC_OFFSET;
486 mode->left_margin = (H_ACTIVE + H_BLANKING) -
487 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
488 mode->upper_margin = V_BLANKING - V_SYNC_OFFSET -
489 V_SYNC_WIDTH;
490 mode->lower_margin = V_SYNC_OFFSET;
491 mode->hsync_len = H_SYNC_WIDTH;
492 mode->vsync_len = V_SYNC_WIDTH;
493 if (HSYNC_POSITIVE)
494 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
495 if (VSYNC_POSITIVE)
496 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
497 mode->refresh = PIXEL_CLOCK/((H_ACTIVE + H_BLANKING) *
498 (V_ACTIVE + V_BLANKING));
499 mode->vmode = 0;
500 mode->flag = FB_MODE_IS_DETAILED;
501
502 DPRINTK(" %d MHz ", PIXEL_CLOCK/1000000);
503 DPRINTK("%d %d %d %d ", H_ACTIVE, H_ACTIVE + H_SYNC_OFFSET,
504 H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH, H_ACTIVE + H_BLANKING);
505 DPRINTK("%d %d %d %d ", V_ACTIVE, V_ACTIVE + V_SYNC_OFFSET,
506 V_ACTIVE + V_SYNC_OFFSET + V_SYNC_WIDTH, V_ACTIVE + V_BLANKING);
507 DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE) ? "+" : "-",
508 (VSYNC_POSITIVE) ? "+" : "-");
509}
510
511/**
512 * fb_create_modedb - create video mode database
513 * @edid: EDID data
514 * @dbsize: database size
515 *
516 * RETURNS: struct fb_videomode, @dbsize contains length of database
517 *
518 * DESCRIPTION:
519 * This function builds a mode database using the contents of the EDID
520 * data
521 */
522static struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize)
523{
524 struct fb_videomode *mode, *m;
525 unsigned char *block;
526 int num = 0, i;
527
528 mode = kmalloc(50 * sizeof(struct fb_videomode), GFP_KERNEL);
529 if (mode == NULL)
530 return NULL;
531 memset(mode, 0, 50 * sizeof(struct fb_videomode));
532
533 if (edid == NULL || !edid_checksum(edid) ||
534 !edid_check_header(edid)) {
535 kfree(mode);
536 return NULL;
537 }
538
539 *dbsize = 0;
540
541 DPRINTK(" Supported VESA Modes\n");
542 block = edid + ESTABLISHED_TIMING_1;
543 num += get_est_timing(block, &mode[num]);
544
545 DPRINTK(" Standard Timings\n");
546 block = edid + STD_TIMING_DESCRIPTIONS_START;
547 for (i = 0; i < STD_TIMING; i++, block += STD_TIMING_DESCRIPTION_SIZE)
548 num += get_std_timing(block, &mode[num]);
549
550 DPRINTK(" Detailed Timings\n");
551 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
552 for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) {
553 int first = 1;
554
555 if (block[0] == 0x00 && block[1] == 0x00) {
556 if (block[3] == 0xfa) {
557 num += get_dst_timing(block + 5, &mode[num]);
558 }
559 } else {
560 get_detailed_timing(block, &mode[num]);
561 if (first) {
562 mode[num].flag |= FB_MODE_IS_FIRST;
563 first = 0;
564 }
565 num++;
566 }
567 }
568
569 /* Yikes, EDID data is totally useless */
570 if (!num) {
571 kfree(mode);
572 return NULL;
573 }
574
575 *dbsize = num;
576 m = kmalloc(num * sizeof(struct fb_videomode), GFP_KERNEL);
577 if (!m)
578 return mode;
579 memmove(m, mode, num * sizeof(struct fb_videomode));
580 kfree(mode);
581 return m;
582}
583
584/**
585 * fb_destroy_modedb - destroys mode database
586 * @modedb: mode database to destroy
587 *
588 * DESCRIPTION:
589 * Destroy mode database created by fb_create_modedb
590 */
591void fb_destroy_modedb(struct fb_videomode *modedb)
592{
593 kfree(modedb);
594}
595
596static int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs)
597{
598 int i, retval = 1;
599 unsigned char *block;
600
601 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
602
603 DPRINTK(" Monitor Operating Limits: ");
604 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
605 if (edid_is_limits_block(block)) {
606 specs->hfmin = H_MIN_RATE * 1000;
607 specs->hfmax = H_MAX_RATE * 1000;
608 specs->vfmin = V_MIN_RATE;
609 specs->vfmax = V_MAX_RATE;
610 specs->dclkmax = MAX_PIXEL_CLOCK * 1000000;
611 specs->gtf = (GTF_SUPPORT) ? 1 : 0;
612 retval = 0;
613 DPRINTK("From EDID\n");
614 break;
615 }
616 }
617
618 /* estimate monitor limits based on modes supported */
619 if (retval) {
620 struct fb_videomode *modes;
621 int num_modes, i, hz, hscan, pixclock;
622
623 modes = fb_create_modedb(edid, &num_modes);
624 if (!modes) {
625 DPRINTK("None Available\n");
626 return 1;
627 }
628
629 retval = 0;
630 for (i = 0; i < num_modes; i++) {
631 hz = modes[i].refresh;
632 pixclock = PICOS2KHZ(modes[i].pixclock) * 1000;
633 hscan = (modes[i].yres * 105 * hz + 5000)/100;
634
635 if (specs->dclkmax == 0 || specs->dclkmax < pixclock)
636 specs->dclkmax = pixclock;
637 if (specs->dclkmin == 0 || specs->dclkmin > pixclock)
638 specs->dclkmin = pixclock;
639 if (specs->hfmax == 0 || specs->hfmax < hscan)
640 specs->hfmax = hscan;
641 if (specs->hfmin == 0 || specs->hfmin > hscan)
642 specs->hfmin = hscan;
643 if (specs->vfmax == 0 || specs->vfmax < hz)
644 specs->vfmax = hz;
645 if (specs->vfmin == 0 || specs->vfmin > hz)
646 specs->vfmin = hz;
647 }
648 DPRINTK("Extrapolated\n");
649 fb_destroy_modedb(modes);
650 }
651 DPRINTK(" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n",
652 specs->hfmin/1000, specs->hfmax/1000, specs->vfmin,
653 specs->vfmax, specs->dclkmax/1000000);
654 return retval;
655}
656
657static void get_monspecs(unsigned char *edid, struct fb_monspecs *specs)
658{
659 unsigned char c, *block;
660
661 block = edid + EDID_STRUCT_DISPLAY;
662
663 fb_get_monitor_limits(edid, specs);
664
665 c = block[0] & 0x80;
666 specs->input = 0;
667 if (c) {
668 specs->input |= FB_DISP_DDI;
669 DPRINTK(" Digital Display Input");
670 } else {
671 DPRINTK(" Analog Display Input: Input Voltage - ");
672 switch ((block[0] & 0x60) >> 5) {
673 case 0:
674 DPRINTK("0.700V/0.300V");
675 specs->input |= FB_DISP_ANA_700_300;
676 break;
677 case 1:
678 DPRINTK("0.714V/0.286V");
679 specs->input |= FB_DISP_ANA_714_286;
680 break;
681 case 2:
682 DPRINTK("1.000V/0.400V");
683 specs->input |= FB_DISP_ANA_1000_400;
684 break;
685 case 3:
686 DPRINTK("0.700V/0.000V");
687 specs->input |= FB_DISP_ANA_700_000;
688 break;
689 }
690 }
691 DPRINTK("\n Sync: ");
692 c = block[0] & 0x10;
693 if (c)
694 DPRINTK(" Configurable signal level\n");
695 c = block[0] & 0x0f;
696 specs->signal = 0;
697 if (c & 0x10) {
698 DPRINTK("Blank to Blank ");
699 specs->signal |= FB_SIGNAL_BLANK_BLANK;
700 }
701 if (c & 0x08) {
702 DPRINTK("Separate ");
703 specs->signal |= FB_SIGNAL_SEPARATE;
704 }
705 if (c & 0x04) {
706 DPRINTK("Composite ");
707 specs->signal |= FB_SIGNAL_COMPOSITE;
708 }
709 if (c & 0x02) {
710 DPRINTK("Sync on Green ");
711 specs->signal |= FB_SIGNAL_SYNC_ON_GREEN;
712 }
713 if (c & 0x01) {
714 DPRINTK("Serration on ");
715 specs->signal |= FB_SIGNAL_SERRATION_ON;
716 }
717 DPRINTK("\n");
718 specs->max_x = block[1];
719 specs->max_y = block[2];
720 DPRINTK(" Max H-size in cm: ");
721 if (specs->max_x)
722 DPRINTK("%d\n", specs->max_x);
723 else
724 DPRINTK("variable\n");
725 DPRINTK(" Max V-size in cm: ");
726 if (specs->max_y)
727 DPRINTK("%d\n", specs->max_y);
728 else
729 DPRINTK("variable\n");
730
731 c = block[3];
732 specs->gamma = c+100;
733 DPRINTK(" Gamma: ");
734 DPRINTK("%d.%d\n", specs->gamma/100, specs->gamma % 100);
735
736 get_dpms_capabilities(block[4], specs);
737
738 switch ((block[4] & 0x18) >> 3) {
739 case 0:
740 DPRINTK(" Monochrome/Grayscale\n");
741 specs->input |= FB_DISP_MONO;
742 break;
743 case 1:
744 DPRINTK(" RGB Color Display\n");
745 specs->input |= FB_DISP_RGB;
746 break;
747 case 2:
748 DPRINTK(" Non-RGB Multicolor Display\n");
749 specs->input |= FB_DISP_MULTI;
750 break;
751 default:
752 DPRINTK(" Unknown\n");
753 specs->input |= FB_DISP_UNKNOWN;
754 break;
755 }
756
757 get_chroma(block, specs);
758
759 specs->misc = 0;
760 c = block[4] & 0x7;
761 if (c & 0x04) {
762 DPRINTK(" Default color format is primary\n");
763 specs->misc |= FB_MISC_PRIM_COLOR;
764 }
765 if (c & 0x02) {
766 DPRINTK(" First DETAILED Timing is preferred\n");
767 specs->misc |= FB_MISC_1ST_DETAIL;
768 }
769 if (c & 0x01) {
770 printk(" Display is GTF capable\n");
771 specs->gtf = 1;
772 }
773}
774
775static int edid_is_timing_block(unsigned char *block)
776{
777 if ((block[0] != 0x00) || (block[1] != 0x00) ||
778 (block[2] != 0x00) || (block[4] != 0x00))
779 return 1;
780 else
781 return 0;
782}
783
784int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
785{
786 int i;
787 unsigned char *block;
788
789 if (edid == NULL || var == NULL)
790 return 1;
791
792 if (!(edid_checksum(edid)))
793 return 1;
794
795 if (!(edid_check_header(edid)))
796 return 1;
797
798 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
799
800 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
801 if (edid_is_timing_block(block)) {
802 var->xres = var->xres_virtual = H_ACTIVE;
803 var->yres = var->yres_virtual = V_ACTIVE;
804 var->height = var->width = -1;
805 var->right_margin = H_SYNC_OFFSET;
806 var->left_margin = (H_ACTIVE + H_BLANKING) -
807 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
808 var->upper_margin = V_BLANKING - V_SYNC_OFFSET -
809 V_SYNC_WIDTH;
810 var->lower_margin = V_SYNC_OFFSET;
811 var->hsync_len = H_SYNC_WIDTH;
812 var->vsync_len = V_SYNC_WIDTH;
813 var->pixclock = PIXEL_CLOCK;
814 var->pixclock /= 1000;
815 var->pixclock = KHZ2PICOS(var->pixclock);
816
817 if (HSYNC_POSITIVE)
818 var->sync |= FB_SYNC_HOR_HIGH_ACT;
819 if (VSYNC_POSITIVE)
820 var->sync |= FB_SYNC_VERT_HIGH_ACT;
821 return 0;
822 }
823 }
824 return 1;
825}
826
827void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
828{
829 unsigned char *block;
Antonino A. Daplas475666d2005-11-07 01:00:46 -0800830 int i, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 if (edid == NULL)
833 return;
834
835 if (!(edid_checksum(edid)))
836 return;
837
838 if (!(edid_check_header(edid)))
839 return;
840
841 memset(specs, 0, sizeof(struct fb_monspecs));
842
843 specs->version = edid[EDID_STRUCT_VERSION];
844 specs->revision = edid[EDID_STRUCT_REVISION];
845
846 DPRINTK("========================================\n");
847 DPRINTK("Display Information (EDID)\n");
848 DPRINTK("========================================\n");
849 DPRINTK(" EDID Version %d.%d\n", (int) specs->version,
850 (int) specs->revision);
851
852 parse_vendor_block(edid + ID_MANUFACTURER_NAME, specs);
853
854 block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
855 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
856 if (edid_is_serial_block(block)) {
857 copy_string(block, specs->serial_no);
858 DPRINTK(" Serial Number: %s\n", specs->serial_no);
859 } else if (edid_is_ascii_block(block)) {
860 copy_string(block, specs->ascii);
861 DPRINTK(" ASCII Block: %s\n", specs->ascii);
862 } else if (edid_is_monitor_block(block)) {
863 copy_string(block, specs->monitor);
864 DPRINTK(" Monitor Name: %s\n", specs->monitor);
865 }
866 }
867
868 DPRINTK(" Display Characteristics:\n");
869 get_monspecs(edid, specs);
870
871 specs->modedb = fb_create_modedb(edid, &specs->modedb_len);
Antonino A. Daplas475666d2005-11-07 01:00:46 -0800872
873 /*
874 * Workaround for buggy EDIDs that sets that the first
875 * detailed timing is preferred but has not detailed
876 * timing specified
877 */
878 for (i = 0; i < specs->modedb_len; i++) {
879 if (specs->modedb[i].flag & FB_MODE_IS_DETAILED) {
880 found = 1;
881 break;
882 }
883 }
884
885 if (!found)
886 specs->misc &= ~FB_MISC_1ST_DETAIL;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 DPRINTK("========================================\n");
889}
890
891/*
892 * VESA Generalized Timing Formula (GTF)
893 */
894
895#define FLYBACK 550
896#define V_FRONTPORCH 1
897#define H_OFFSET 40
898#define H_SCALEFACTOR 20
899#define H_BLANKSCALE 128
900#define H_GRADIENT 600
901#define C_VAL 30
902#define M_VAL 300
903
904struct __fb_timings {
905 u32 dclk;
906 u32 hfreq;
907 u32 vfreq;
908 u32 hactive;
909 u32 vactive;
910 u32 hblank;
911 u32 vblank;
912 u32 htotal;
913 u32 vtotal;
914};
915
916/**
917 * fb_get_vblank - get vertical blank time
918 * @hfreq: horizontal freq
919 *
920 * DESCRIPTION:
921 * vblank = right_margin + vsync_len + left_margin
922 *
923 * given: right_margin = 1 (V_FRONTPORCH)
924 * vsync_len = 3
925 * flyback = 550
926 *
927 * flyback * hfreq
928 * left_margin = --------------- - vsync_len
929 * 1000000
930 */
931static u32 fb_get_vblank(u32 hfreq)
932{
933 u32 vblank;
934
935 vblank = (hfreq * FLYBACK)/1000;
936 vblank = (vblank + 500)/1000;
937 return (vblank + V_FRONTPORCH);
938}
939
940/**
941 * fb_get_hblank_by_freq - get horizontal blank time given hfreq
942 * @hfreq: horizontal freq
943 * @xres: horizontal resolution in pixels
944 *
945 * DESCRIPTION:
946 *
947 * xres * duty_cycle
948 * hblank = ------------------
949 * 100 - duty_cycle
950 *
951 * duty cycle = percent of htotal assigned to inactive display
952 * duty cycle = C - (M/Hfreq)
953 *
954 * where: C = ((offset - scale factor) * blank_scale)
955 * -------------------------------------- + scale factor
956 * 256
957 * M = blank_scale * gradient
958 *
959 */
960static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres)
961{
962 u32 c_val, m_val, duty_cycle, hblank;
963
964 c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 +
965 H_SCALEFACTOR) * 1000;
966 m_val = (H_BLANKSCALE * H_GRADIENT)/256;
967 m_val = (m_val * 1000000)/hfreq;
968 duty_cycle = c_val - m_val;
969 hblank = (xres * duty_cycle)/(100000 - duty_cycle);
970 return (hblank);
971}
972
973/**
974 * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
975 * @dclk: pixelclock in Hz
976 * @xres: horizontal resolution in pixels
977 *
978 * DESCRIPTION:
979 *
980 * xres * duty_cycle
981 * hblank = ------------------
982 * 100 - duty_cycle
983 *
984 * duty cycle = percent of htotal assigned to inactive display
985 * duty cycle = C - (M * h_period)
986 *
987 * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
988 * -----------------------------------------------
989 * 2 * M
990 * M = 300;
991 * C = 30;
992
993 */
994static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres)
995{
996 u32 duty_cycle, h_period, hblank;
997
998 dclk /= 1000;
999 h_period = 100 - C_VAL;
1000 h_period *= h_period;
1001 h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk);
1002 h_period *=10000;
1003
1004 h_period = int_sqrt(h_period);
1005 h_period -= (100 - C_VAL) * 100;
1006 h_period *= 1000;
1007 h_period /= 2 * M_VAL;
1008
1009 duty_cycle = C_VAL * 1000 - (M_VAL * h_period)/100;
1010 hblank = (xres * duty_cycle)/(100000 - duty_cycle) + 8;
1011 hblank &= ~15;
1012 return (hblank);
1013}
1014
1015/**
1016 * fb_get_hfreq - estimate hsync
1017 * @vfreq: vertical refresh rate
1018 * @yres: vertical resolution
1019 *
1020 * DESCRIPTION:
1021 *
1022 * (yres + front_port) * vfreq * 1000000
1023 * hfreq = -------------------------------------
1024 * (1000000 - (vfreq * FLYBACK)
1025 *
1026 */
1027
1028static u32 fb_get_hfreq(u32 vfreq, u32 yres)
1029{
1030 u32 divisor, hfreq;
1031
1032 divisor = (1000000 - (vfreq * FLYBACK))/1000;
1033 hfreq = (yres + V_FRONTPORCH) * vfreq * 1000;
1034 return (hfreq/divisor);
1035}
1036
1037static void fb_timings_vfreq(struct __fb_timings *timings)
1038{
1039 timings->hfreq = fb_get_hfreq(timings->vfreq, timings->vactive);
1040 timings->vblank = fb_get_vblank(timings->hfreq);
1041 timings->vtotal = timings->vactive + timings->vblank;
1042 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
1043 timings->hactive);
1044 timings->htotal = timings->hactive + timings->hblank;
1045 timings->dclk = timings->htotal * timings->hfreq;
1046}
1047
1048static void fb_timings_hfreq(struct __fb_timings *timings)
1049{
1050 timings->vblank = fb_get_vblank(timings->hfreq);
1051 timings->vtotal = timings->vactive + timings->vblank;
1052 timings->vfreq = timings->hfreq/timings->vtotal;
1053 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
1054 timings->hactive);
1055 timings->htotal = timings->hactive + timings->hblank;
1056 timings->dclk = timings->htotal * timings->hfreq;
1057}
1058
1059static void fb_timings_dclk(struct __fb_timings *timings)
1060{
1061 timings->hblank = fb_get_hblank_by_dclk(timings->dclk,
1062 timings->hactive);
1063 timings->htotal = timings->hactive + timings->hblank;
1064 timings->hfreq = timings->dclk/timings->htotal;
1065 timings->vblank = fb_get_vblank(timings->hfreq);
1066 timings->vtotal = timings->vactive + timings->vblank;
1067 timings->vfreq = timings->hfreq/timings->vtotal;
1068}
1069
1070/*
1071 * fb_get_mode - calculates video mode using VESA GTF
1072 * @flags: if: 0 - maximize vertical refresh rate
1073 * 1 - vrefresh-driven calculation;
1074 * 2 - hscan-driven calculation;
1075 * 3 - pixelclock-driven calculation;
1076 * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock
1077 * @var: pointer to fb_var_screeninfo
1078 * @info: pointer to fb_info
1079 *
1080 * DESCRIPTION:
1081 * Calculates video mode based on monitor specs using VESA GTF.
1082 * The GTF is best for VESA GTF compliant monitors but is
1083 * specifically formulated to work for older monitors as well.
1084 *
1085 * If @flag==0, the function will attempt to maximize the
1086 * refresh rate. Otherwise, it will calculate timings based on
1087 * the flag and accompanying value.
1088 *
1089 * If FB_IGNOREMON bit is set in @flags, monitor specs will be
1090 * ignored and @var will be filled with the calculated timings.
1091 *
1092 * All calculations are based on the VESA GTF Spreadsheet
1093 * available at VESA's public ftp (http://www.vesa.org).
1094 *
1095 * NOTES:
1096 * The timings generated by the GTF will be different from VESA
1097 * DMT. It might be a good idea to keep a table of standard
1098 * VESA modes as well. The GTF may also not work for some displays,
1099 * such as, and especially, analog TV.
1100 *
1101 * REQUIRES:
1102 * A valid info->monspecs, otherwise 'safe numbers' will be used.
1103 */
1104int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, struct fb_info *info)
1105{
1106 struct __fb_timings timings;
1107 u32 interlace = 1, dscan = 1;
1108 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
1109
1110 /*
1111 * If monspecs are invalid, use values that are enough
1112 * for 640x480@60
1113 */
1114 if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
1115 !info->monspecs.dclkmax ||
1116 info->monspecs.hfmax < info->monspecs.hfmin ||
1117 info->monspecs.vfmax < info->monspecs.vfmin ||
1118 info->monspecs.dclkmax < info->monspecs.dclkmin) {
1119 hfmin = 29000; hfmax = 30000;
1120 vfmin = 60; vfmax = 60;
1121 dclkmin = 0; dclkmax = 25000000;
1122 } else {
1123 hfmin = info->monspecs.hfmin;
1124 hfmax = info->monspecs.hfmax;
1125 vfmin = info->monspecs.vfmin;
1126 vfmax = info->monspecs.vfmax;
1127 dclkmin = info->monspecs.dclkmin;
1128 dclkmax = info->monspecs.dclkmax;
1129 }
1130
1131 memset(&timings, 0, sizeof(struct __fb_timings));
1132 timings.hactive = var->xres;
1133 timings.vactive = var->yres;
1134 if (var->vmode & FB_VMODE_INTERLACED) {
1135 timings.vactive /= 2;
1136 interlace = 2;
1137 }
1138 if (var->vmode & FB_VMODE_DOUBLE) {
1139 timings.vactive *= 2;
1140 dscan = 2;
1141 }
1142
1143 switch (flags & ~FB_IGNOREMON) {
1144 case FB_MAXTIMINGS: /* maximize refresh rate */
1145 timings.hfreq = hfmax;
1146 fb_timings_hfreq(&timings);
1147 if (timings.vfreq > vfmax) {
1148 timings.vfreq = vfmax;
1149 fb_timings_vfreq(&timings);
1150 }
1151 if (timings.dclk > dclkmax) {
1152 timings.dclk = dclkmax;
1153 fb_timings_dclk(&timings);
1154 }
1155 break;
1156 case FB_VSYNCTIMINGS: /* vrefresh driven */
1157 timings.vfreq = val;
1158 fb_timings_vfreq(&timings);
1159 break;
1160 case FB_HSYNCTIMINGS: /* hsync driven */
1161 timings.hfreq = val;
1162 fb_timings_hfreq(&timings);
1163 break;
1164 case FB_DCLKTIMINGS: /* pixelclock driven */
1165 timings.dclk = PICOS2KHZ(val) * 1000;
1166 fb_timings_dclk(&timings);
1167 break;
1168 default:
1169 return -EINVAL;
1170
1171 }
1172
1173 if (!(flags & FB_IGNOREMON) &&
1174 (timings.vfreq < vfmin || timings.vfreq > vfmax ||
1175 timings.hfreq < hfmin || timings.hfreq > hfmax ||
1176 timings.dclk < dclkmin || timings.dclk > dclkmax))
1177 return -EINVAL;
1178
1179 var->pixclock = KHZ2PICOS(timings.dclk/1000);
1180 var->hsync_len = (timings.htotal * 8)/100;
1181 var->right_margin = (timings.hblank/2) - var->hsync_len;
1182 var->left_margin = timings.hblank - var->right_margin - var->hsync_len;
1183
1184 var->vsync_len = (3 * interlace)/dscan;
1185 var->lower_margin = (1 * interlace)/dscan;
1186 var->upper_margin = (timings.vblank * interlace)/dscan -
1187 (var->vsync_len + var->lower_margin);
1188
1189 return 0;
1190}
1191#else
1192int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
1193{
1194 return 1;
1195}
1196void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
1197{
1198 specs = NULL;
1199}
1200void fb_destroy_modedb(struct fb_videomode *modedb)
1201{
1202}
1203int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var,
1204 struct fb_info *info)
1205{
1206 return -EINVAL;
1207}
1208#endif /* CONFIG_FB_MODE_HELPERS */
1209
1210/*
1211 * fb_validate_mode - validates var against monitor capabilities
1212 * @var: pointer to fb_var_screeninfo
1213 * @info: pointer to fb_info
1214 *
1215 * DESCRIPTION:
1216 * Validates video mode against monitor capabilities specified in
1217 * info->monspecs.
1218 *
1219 * REQUIRES:
1220 * A valid info->monspecs.
1221 */
1222int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info)
1223{
1224 u32 hfreq, vfreq, htotal, vtotal, pixclock;
1225 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
1226
1227 /*
1228 * If monspecs are invalid, use values that are enough
1229 * for 640x480@60
1230 */
1231 if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
1232 !info->monspecs.dclkmax ||
1233 info->monspecs.hfmax < info->monspecs.hfmin ||
1234 info->monspecs.vfmax < info->monspecs.vfmin ||
1235 info->monspecs.dclkmax < info->monspecs.dclkmin) {
1236 hfmin = 29000; hfmax = 30000;
1237 vfmin = 60; vfmax = 60;
1238 dclkmin = 0; dclkmax = 25000000;
1239 } else {
1240 hfmin = info->monspecs.hfmin;
1241 hfmax = info->monspecs.hfmax;
1242 vfmin = info->monspecs.vfmin;
1243 vfmax = info->monspecs.vfmax;
1244 dclkmin = info->monspecs.dclkmin;
1245 dclkmax = info->monspecs.dclkmax;
1246 }
1247
1248 if (!var->pixclock)
1249 return -EINVAL;
1250 pixclock = PICOS2KHZ(var->pixclock) * 1000;
1251
1252 htotal = var->xres + var->right_margin + var->hsync_len +
1253 var->left_margin;
1254 vtotal = var->yres + var->lower_margin + var->vsync_len +
1255 var->upper_margin;
1256
1257 if (var->vmode & FB_VMODE_INTERLACED)
1258 vtotal /= 2;
1259 if (var->vmode & FB_VMODE_DOUBLE)
1260 vtotal *= 2;
1261
1262 hfreq = pixclock/htotal;
Jon Smirl0a793b72005-07-27 11:46:03 -07001263 hfreq = (hfreq + 500) / 1000 * 1000;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 vfreq = hfreq/vtotal;
1266
1267 return (vfreq < vfmin || vfreq > vfmax ||
1268 hfreq < hfmin || hfreq > hfmax ||
1269 pixclock < dclkmin || pixclock > dclkmax) ?
1270 -EINVAL : 0;
1271}
1272
Antonino A. Daplas5e518d72005-09-09 13:04:34 -07001273#if defined(__i386__)
1274#include <linux/pci.h>
1275
1276/*
1277 * We need to ensure that the EDID block is only returned for
1278 * the primary graphics adapter.
1279 */
1280
1281const unsigned char *fb_firmware_edid(struct device *device)
1282{
1283 struct pci_dev *dev = NULL;
1284 struct resource *res = NULL;
1285 unsigned char *edid = NULL;
1286
1287 if (device)
1288 dev = to_pci_dev(device);
1289
1290 if (dev)
1291 res = &dev->resource[PCI_ROM_RESOURCE];
1292
1293 if (res && res->flags & IORESOURCE_ROM_SHADOW)
1294 edid = edid_info.dummy;
1295
1296 return edid;
1297}
1298#else
1299const unsigned char *fb_firmware_edid(struct device *device)
1300{
1301 return NULL;
1302}
1303#endif /* _i386_ */
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305EXPORT_SYMBOL(fb_parse_edid);
1306EXPORT_SYMBOL(fb_edid_to_monspecs);
Antonino A. Daplas5e518d72005-09-09 13:04:34 -07001307EXPORT_SYMBOL(fb_firmware_edid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308EXPORT_SYMBOL(fb_get_mode);
1309EXPORT_SYMBOL(fb_validate_mode);
1310EXPORT_SYMBOL(fb_destroy_modedb);