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, const struct fb_monspecs *specs) 500 { 501 int i; 502 503 for (i = 0; i < DMT_SIZE; i++) { 504 u32 std_2byte_code = block[0] << 8 | block[1]; 505 if (std_2byte_code == dmt_modes[i].std_2byte_code) 506 break; 507 } 508 509 if (i < DMT_SIZE && dmt_modes[i].mode) { 510 /* DMT mode found */ 511 *mode = *dmt_modes[i].mode; 512 mode->flag |= FB_MODE_IS_STANDARD; 513 DPRINTK(" DMT id=%d\n", dmt_modes[i].dmt_id); 514 515 } else { 516 int xres, yres = 0, refresh, ratio; 517 518 xres = (block[0] + 31) * 8; 519 if (xres <= 256) 520 return 0; 521 522 ratio = (block[1] & 0xc0) >> 6; 523 switch (ratio) { 524 case 0: 525 /* in EDID 1.3 the meaning of 0 changed to 16:10 (prior 1:1) */ 526 if (ver < 1 || (ver == 1 && rev < 3)) 527 yres = xres; 528 else 529 yres = (xres * 10)/16; 530 break; 531 case 1: 532 yres = (xres * 3)/4; 533 break; 534 case 2: 535 yres = (xres * 4)/5; 536 break; 537 case 3: 538 yres = (xres * 9)/16; 539 break; 540 } 541 refresh = (block[1] & 0x3f) + 60; 542 DPRINTK(" %dx%d@%dHz\n", xres, yres, refresh); 543 544 calc_mode_timings(xres, yres, refresh, mode); 545 } 546 547 /* Check the mode we got is within valid spec of the monitor */ 548 if (specs && specs->dclkmax 549 && PICOS2KHZ(mode->pixclock) * 1000 > specs->dclkmax) { 550 DPRINTK(" mode exceed max DCLK\n"); 551 return 0; 552 } 553 554 return 1; 555 } 556 557 static int get_dst_timing(unsigned char *block, struct fb_videomode *mode, 558 int ver, int rev, const struct fb_monspecs *specs) 559 { 560 int j, num = 0; 561 562 for (j = 0; j < 6; j++, block += STD_TIMING_DESCRIPTION_SIZE) 563 num += get_std_timing(block, &mode[num], ver, rev, specs); 564 565 return num; 566 } 567 568 static void get_detailed_timing(unsigned char *block, 569 struct fb_videomode *mode) 570 { 571 mode->xres = H_ACTIVE; 572 mode->yres = V_ACTIVE; 573 mode->pixclock = PIXEL_CLOCK; 574 mode->pixclock /= 1000; 575 mode->pixclock = KHZ2PICOS(mode->pixclock); 576 mode->right_margin = H_SYNC_OFFSET; 577 mode->left_margin = (H_ACTIVE + H_BLANKING) - 578 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH); 579 mode->upper_margin = V_BLANKING - V_SYNC_OFFSET - 580 V_SYNC_WIDTH; 581 mode->lower_margin = V_SYNC_OFFSET; 582 mode->hsync_len = H_SYNC_WIDTH; 583 mode->vsync_len = V_SYNC_WIDTH; 584 if (HSYNC_POSITIVE) 585 mode->sync |= FB_SYNC_HOR_HIGH_ACT; 586 if (VSYNC_POSITIVE) 587 mode->sync |= FB_SYNC_VERT_HIGH_ACT; 588 mode->refresh = PIXEL_CLOCK/((H_ACTIVE + H_BLANKING) * 589 (V_ACTIVE + V_BLANKING)); 590 if (INTERLACED) { 591 mode->yres *= 2; 592 mode->upper_margin *= 2; 593 mode->lower_margin *= 2; 594 mode->vsync_len *= 2; 595 mode->vmode |= FB_VMODE_INTERLACED; 596 } 597 mode->flag = FB_MODE_IS_DETAILED; 598 599 DPRINTK(" %d MHz ", PIXEL_CLOCK/1000000); 600 DPRINTK("%d %d %d %d ", H_ACTIVE, H_ACTIVE + H_SYNC_OFFSET, 601 H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH, H_ACTIVE + H_BLANKING); 602 DPRINTK("%d %d %d %d ", V_ACTIVE, V_ACTIVE + V_SYNC_OFFSET, 603 V_ACTIVE + V_SYNC_OFFSET + V_SYNC_WIDTH, V_ACTIVE + V_BLANKING); 604 DPRINTK("%sHSync %sVSync\n\n", (HSYNC_POSITIVE) ? "+" : "-", 605 (VSYNC_POSITIVE) ? "+" : "-"); 606 } 607 608 /** 609 * fb_create_modedb - create video mode database 610 * @edid: EDID data 611 * @dbsize: database size 612 * 613 * RETURNS: struct fb_videomode, @dbsize contains length of database 614 * 615 * DESCRIPTION: 616 * This function builds a mode database using the contents of the EDID 617 * data 618 */ 619 static struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize, 620 const struct fb_monspecs *specs) 621 { 622 struct fb_videomode *mode, *m; 623 unsigned char *block; 624 int num = 0, i, first = 1; 625 int ver, rev; 626 627 mode = kzalloc(50 * sizeof(struct fb_videomode), GFP_KERNEL); 628 if (mode == NULL) 629 return NULL; 630 631 if (edid == NULL || !edid_checksum(edid) || 632 !edid_check_header(edid)) { 633 kfree(mode); 634 return NULL; 635 } 636 637 ver = edid[EDID_STRUCT_VERSION]; 638 rev = edid[EDID_STRUCT_REVISION]; 639 640 *dbsize = 0; 641 642 DPRINTK(" Detailed Timings\n"); 643 block = edid + DETAILED_TIMING_DESCRIPTIONS_START; 644 for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) { 645 if (!(block[0] == 0x00 && block[1] == 0x00)) { 646 get_detailed_timing(block, &mode[num]); 647 if (first) { 648 mode[num].flag |= FB_MODE_IS_FIRST; 649 first = 0; 650 } 651 num++; 652 } 653 } 654 655 DPRINTK(" Supported VESA Modes\n"); 656 block = edid + ESTABLISHED_TIMING_1; 657 num += get_est_timing(block, &mode[num]); 658 659 DPRINTK(" Standard Timings\n"); 660 block = edid + STD_TIMING_DESCRIPTIONS_START; 661 for (i = 0; i < STD_TIMING; i++, block += STD_TIMING_DESCRIPTION_SIZE) 662 num += get_std_timing(block, &mode[num], ver, rev, specs); 663 664 block = edid + DETAILED_TIMING_DESCRIPTIONS_START; 665 for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) { 666 if (block[0] == 0x00 && block[1] == 0x00 && block[3] == 0xfa) 667 num += get_dst_timing(block + 5, &mode[num], 668 ver, rev, specs); 669 } 670 671 /* Yikes, EDID data is totally useless */ 672 if (!num) { 673 kfree(mode); 674 return NULL; 675 } 676 677 *dbsize = num; 678 m = kmalloc(num * sizeof(struct fb_videomode), GFP_KERNEL); 679 if (!m) 680 return mode; 681 memmove(m, mode, num * sizeof(struct fb_videomode)); 682 kfree(mode); 683 return m; 684 } 685 686 /** 687 * fb_destroy_modedb - destroys mode database 688 * @modedb: mode database to destroy 689 * 690 * DESCRIPTION: 691 * Destroy mode database created by fb_create_modedb 692 */ 693 void fb_destroy_modedb(struct fb_videomode *modedb) 694 { 695 kfree(modedb); 696 } 697 698 static int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs) 699 { 700 int i, retval = 1; 701 unsigned char *block; 702 703 block = edid + DETAILED_TIMING_DESCRIPTIONS_START; 704 705 DPRINTK(" Monitor Operating Limits: "); 706 707 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) { 708 if (edid_is_limits_block(block)) { 709 specs->hfmin = H_MIN_RATE * 1000; 710 specs->hfmax = H_MAX_RATE * 1000; 711 specs->vfmin = V_MIN_RATE; 712 specs->vfmax = V_MAX_RATE; 713 specs->dclkmax = MAX_PIXEL_CLOCK * 1000000; 714 specs->gtf = (GTF_SUPPORT) ? 1 : 0; 715 retval = 0; 716 DPRINTK("From EDID\n"); 717 break; 718 } 719 } 720 721 /* estimate monitor limits based on modes supported */ 722 if (retval) { 723 struct fb_videomode *modes, *mode; 724 int num_modes, hz, hscan, pixclock; 725 int vtotal, htotal; 726 727 modes = fb_create_modedb(edid, &num_modes, specs); 728 if (!modes) { 729 DPRINTK("None Available\n"); 730 return 1; 731 } 732 733 retval = 0; 734 for (i = 0; i < num_modes; i++) { 735 mode = &modes[i]; 736 pixclock = PICOS2KHZ(modes[i].pixclock) * 1000; 737 htotal = mode->xres + mode->right_margin + mode->hsync_len 738 + mode->left_margin; 739 vtotal = mode->yres + mode->lower_margin + mode->vsync_len 740 + mode->upper_margin; 741 742 if (mode->vmode & FB_VMODE_INTERLACED) 743 vtotal /= 2; 744 745 if (mode->vmode & FB_VMODE_DOUBLE) 746 vtotal *= 2; 747 748 hscan = (pixclock + htotal / 2) / htotal; 749 hscan = (hscan + 500) / 1000 * 1000; 750 hz = (hscan + vtotal / 2) / vtotal; 751 752 if (specs->dclkmax == 0 || specs->dclkmax < pixclock) 753 specs->dclkmax = pixclock; 754 755 if (specs->dclkmin == 0 || specs->dclkmin > pixclock) 756 specs->dclkmin = pixclock; 757 758 if (specs->hfmax == 0 || specs->hfmax < hscan) 759 specs->hfmax = hscan; 760 761 if (specs->hfmin == 0 || specs->hfmin > hscan) 762 specs->hfmin = hscan; 763 764 if (specs->vfmax == 0 || specs->vfmax < hz) 765 specs->vfmax = hz; 766 767 if (specs->vfmin == 0 || specs->vfmin > hz) 768 specs->vfmin = hz; 769 } 770 DPRINTK("Extrapolated\n"); 771 fb_destroy_modedb(modes); 772 } 773 DPRINTK(" H: %d-%dKHz V: %d-%dHz DCLK: %dMHz\n", 774 specs->hfmin/1000, specs->hfmax/1000, specs->vfmin, 775 specs->vfmax, specs->dclkmax/1000000); 776 return retval; 777 } 778 779 static void get_monspecs(unsigned char *edid, struct fb_monspecs *specs) 780 { 781 unsigned char c, *block; 782 783 block = edid + EDID_STRUCT_DISPLAY; 784 785 fb_get_monitor_limits(edid, specs); 786 787 c = block[0] & 0x80; 788 specs->input = 0; 789 if (c) { 790 specs->input |= FB_DISP_DDI; 791 DPRINTK(" Digital Display Input"); 792 } else { 793 DPRINTK(" Analog Display Input: Input Voltage - "); 794 switch ((block[0] & 0x60) >> 5) { 795 case 0: 796 DPRINTK("0.700V/0.300V"); 797 specs->input |= FB_DISP_ANA_700_300; 798 break; 799 case 1: 800 DPRINTK("0.714V/0.286V"); 801 specs->input |= FB_DISP_ANA_714_286; 802 break; 803 case 2: 804 DPRINTK("1.000V/0.400V"); 805 specs->input |= FB_DISP_ANA_1000_400; 806 break; 807 case 3: 808 DPRINTK("0.700V/0.000V"); 809 specs->input |= FB_DISP_ANA_700_000; 810 break; 811 } 812 } 813 DPRINTK("\n Sync: "); 814 c = block[0] & 0x10; 815 if (c) 816 DPRINTK(" Configurable signal level\n"); 817 c = block[0] & 0x0f; 818 specs->signal = 0; 819 if (c & 0x10) { 820 DPRINTK("Blank to Blank "); 821 specs->signal |= FB_SIGNAL_BLANK_BLANK; 822 } 823 if (c & 0x08) { 824 DPRINTK("Separate "); 825 specs->signal |= FB_SIGNAL_SEPARATE; 826 } 827 if (c & 0x04) { 828 DPRINTK("Composite "); 829 specs->signal |= FB_SIGNAL_COMPOSITE; 830 } 831 if (c & 0x02) { 832 DPRINTK("Sync on Green "); 833 specs->signal |= FB_SIGNAL_SYNC_ON_GREEN; 834 } 835 if (c & 0x01) { 836 DPRINTK("Serration on "); 837 specs->signal |= FB_SIGNAL_SERRATION_ON; 838 } 839 DPRINTK("\n"); 840 specs->max_x = block[1]; 841 specs->max_y = block[2]; 842 DPRINTK(" Max H-size in cm: "); 843 if (specs->max_x) 844 DPRINTK("%d\n", specs->max_x); 845 else 846 DPRINTK("variable\n"); 847 DPRINTK(" Max V-size in cm: "); 848 if (specs->max_y) 849 DPRINTK("%d\n", specs->max_y); 850 else 851 DPRINTK("variable\n"); 852 853 c = block[3]; 854 specs->gamma = c+100; 855 DPRINTK(" Gamma: "); 856 DPRINTK("%d.%d\n", specs->gamma/100, specs->gamma % 100); 857 858 get_dpms_capabilities(block[4], specs); 859 860 switch ((block[4] & 0x18) >> 3) { 861 case 0: 862 DPRINTK(" Monochrome/Grayscale\n"); 863 specs->input |= FB_DISP_MONO; 864 break; 865 case 1: 866 DPRINTK(" RGB Color Display\n"); 867 specs->input |= FB_DISP_RGB; 868 break; 869 case 2: 870 DPRINTK(" Non-RGB Multicolor Display\n"); 871 specs->input |= FB_DISP_MULTI; 872 break; 873 default: 874 DPRINTK(" Unknown\n"); 875 specs->input |= FB_DISP_UNKNOWN; 876 break; 877 } 878 879 get_chroma(block, specs); 880 881 specs->misc = 0; 882 c = block[4] & 0x7; 883 if (c & 0x04) { 884 DPRINTK(" Default color format is primary\n"); 885 specs->misc |= FB_MISC_PRIM_COLOR; 886 } 887 if (c & 0x02) { 888 DPRINTK(" First DETAILED Timing is preferred\n"); 889 specs->misc |= FB_MISC_1ST_DETAIL; 890 } 891 if (c & 0x01) { 892 printk(" Display is GTF capable\n"); 893 specs->gtf = 1; 894 } 895 } 896 897 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var) 898 { 899 int i; 900 unsigned char *block; 901 902 if (edid == NULL || var == NULL) 903 return 1; 904 905 if (!(edid_checksum(edid))) 906 return 1; 907 908 if (!(edid_check_header(edid))) 909 return 1; 910 911 block = edid + DETAILED_TIMING_DESCRIPTIONS_START; 912 913 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) { 914 if (edid_is_timing_block(block)) { 915 var->xres = var->xres_virtual = H_ACTIVE; 916 var->yres = var->yres_virtual = V_ACTIVE; 917 var->height = var->width = 0; 918 var->right_margin = H_SYNC_OFFSET; 919 var->left_margin = (H_ACTIVE + H_BLANKING) - 920 (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH); 921 var->upper_margin = V_BLANKING - V_SYNC_OFFSET - 922 V_SYNC_WIDTH; 923 var->lower_margin = V_SYNC_OFFSET; 924 var->hsync_len = H_SYNC_WIDTH; 925 var->vsync_len = V_SYNC_WIDTH; 926 var->pixclock = PIXEL_CLOCK; 927 var->pixclock /= 1000; 928 var->pixclock = KHZ2PICOS(var->pixclock); 929 930 if (HSYNC_POSITIVE) 931 var->sync |= FB_SYNC_HOR_HIGH_ACT; 932 if (VSYNC_POSITIVE) 933 var->sync |= FB_SYNC_VERT_HIGH_ACT; 934 return 0; 935 } 936 } 937 return 1; 938 } 939 940 void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs) 941 { 942 unsigned char *block; 943 int i, found = 0; 944 945 if (edid == NULL) 946 return; 947 948 if (!(edid_checksum(edid))) 949 return; 950 951 if (!(edid_check_header(edid))) 952 return; 953 954 memset(specs, 0, sizeof(struct fb_monspecs)); 955 956 specs->version = edid[EDID_STRUCT_VERSION]; 957 specs->revision = edid[EDID_STRUCT_REVISION]; 958 959 DPRINTK("========================================\n"); 960 DPRINTK("Display Information (EDID)\n"); 961 DPRINTK("========================================\n"); 962 DPRINTK(" EDID Version %d.%d\n", (int) specs->version, 963 (int) specs->revision); 964 965 parse_vendor_block(edid + ID_MANUFACTURER_NAME, specs); 966 967 block = edid + DETAILED_TIMING_DESCRIPTIONS_START; 968 for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) { 969 if (edid_is_serial_block(block)) { 970 copy_string(block, specs->serial_no); 971 DPRINTK(" Serial Number: %s\n", specs->serial_no); 972 } else if (edid_is_ascii_block(block)) { 973 copy_string(block, specs->ascii); 974 DPRINTK(" ASCII Block: %s\n", specs->ascii); 975 } else if (edid_is_monitor_block(block)) { 976 copy_string(block, specs->monitor); 977 DPRINTK(" Monitor Name: %s\n", specs->monitor); 978 } 979 } 980 981 DPRINTK(" Display Characteristics:\n"); 982 get_monspecs(edid, specs); 983 984 specs->modedb = fb_create_modedb(edid, &specs->modedb_len, specs); 985 986 /* 987 * Workaround for buggy EDIDs that sets that the first 988 * detailed timing is preferred but has not detailed 989 * timing specified 990 */ 991 for (i = 0; i < specs->modedb_len; i++) { 992 if (specs->modedb[i].flag & FB_MODE_IS_DETAILED) { 993 found = 1; 994 break; 995 } 996 } 997 998 if (!found) 999 specs->misc &= ~FB_MISC_1ST_DETAIL; 1000 1001 DPRINTK("========================================\n"); 1002 } 1003 1004 /** 1005 * fb_edid_add_monspecs() - add monitor video modes from E-EDID data 1006 * @edid: 128 byte array with an E-EDID block 1007 * @spacs: monitor specs to be extended 1008 */ 1009 void fb_edid_add_monspecs(unsigned char *edid, struct fb_monspecs *specs) 1010 { 1011 unsigned char *block; 1012 struct fb_videomode *m; 1013 int num = 0, i; 1014 u8 svd[64], edt[(128 - 4) / DETAILED_TIMING_DESCRIPTION_SIZE]; 1015 u8 pos = 4, svd_n = 0; 1016 1017 if (!edid) 1018 return; 1019 1020 if (!edid_checksum(edid)) 1021 return; 1022 1023 if (edid[0] != 0x2 || 1024 edid[2] < 4 || edid[2] > 128 - DETAILED_TIMING_DESCRIPTION_SIZE) 1025 return; 1026 1027 DPRINTK(" Short Video Descriptors\n"); 1028 1029 while (pos < edid[2]) { 1030 u8 len = edid[pos] & 0x1f, type = (edid[pos] >> 5) & 7; 1031 pr_debug("Data block %u of %u bytes\n", type, len); 1032 if (type == 2) { 1033 for (i = pos; i < pos + len; i++) { 1034 u8 idx = edid[pos + i] & 0x7f; 1035 svd[svd_n++] = idx; 1036 pr_debug("N%sative mode #%d\n", 1037 edid[pos + i] & 0x80 ? "" : "on-n", idx); 1038 } 1039 } else if (type == 3 && len >= 3) { 1040 /* Check Vendor Specific Data Block. For HDMI, 1041 it is always 00-0C-03 for HDMI Licensing, LLC. */ 1042 if (edid[pos + 1] == 3 && edid[pos + 2] == 0xc && 1043 edid[pos + 3] == 0) 1044 specs->misc |= FB_MISC_HDMI; 1045 } 1046 pos += len + 1; 1047 } 1048 1049 block = edid + edid[2]; 1050 1051 DPRINTK(" Extended Detailed Timings\n"); 1052 1053 for (i = 0; i < (128 - edid[2]) / DETAILED_TIMING_DESCRIPTION_SIZE; 1054 i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) 1055 if (PIXEL_CLOCK) 1056 edt[num++] = block - edid; 1057 1058 /* Yikes, EDID data is totally useless */ 1059 if (!(num + svd_n)) 1060 return; 1061 1062 m = kzalloc((specs->modedb_len + num + svd_n) * 1063 sizeof(struct fb_videomode), GFP_KERNEL); 1064 1065 if (!m) 1066 return; 1067 1068 memcpy(m, specs->modedb, specs->modedb_len * sizeof(struct fb_videomode)); 1069 1070 for (i = specs->modedb_len; i < specs->modedb_len + num; i++) { 1071 get_detailed_timing(edid + edt[i - specs->modedb_len], &m[i]); 1072 if (i == specs->modedb_len) 1073 m[i].flag |= FB_MODE_IS_FIRST; 1074 pr_debug("Adding %ux%u@%u\n", m[i].xres, m[i].yres, m[i].refresh); 1075 } 1076 1077 for (i = specs->modedb_len + num; i < specs->modedb_len + num + svd_n; i++) { 1078 int idx = svd[i - specs->modedb_len - num]; 1079 if (!idx || idx > 63) { 1080 pr_warning("Reserved SVD code %d\n", idx); 1081 } else if (idx > ARRAY_SIZE(cea_modes) || !cea_modes[idx].xres) { 1082 pr_warning("Unimplemented SVD code %d\n", idx); 1083 } else { 1084 memcpy(&m[i], cea_modes + idx, sizeof(m[i])); 1085 pr_debug("Adding SVD #%d: %ux%u@%u\n", idx, 1086 m[i].xres, m[i].yres, m[i].refresh); 1087 } 1088 } 1089 1090 kfree(specs->modedb); 1091 specs->modedb = m; 1092 specs->modedb_len = specs->modedb_len + num + svd_n; 1093 } 1094 1095 /* 1096 * VESA Generalized Timing Formula (GTF) 1097 */ 1098 1099 #define FLYBACK 550 1100 #define V_FRONTPORCH 1 1101 #define H_OFFSET 40 1102 #define H_SCALEFACTOR 20 1103 #define H_BLANKSCALE 128 1104 #define H_GRADIENT 600 1105 #define C_VAL 30 1106 #define M_VAL 300 1107 1108 struct __fb_timings { 1109 u32 dclk; 1110 u32 hfreq; 1111 u32 vfreq; 1112 u32 hactive; 1113 u32 vactive; 1114 u32 hblank; 1115 u32 vblank; 1116 u32 htotal; 1117 u32 vtotal; 1118 }; 1119 1120 /** 1121 * fb_get_vblank - get vertical blank time 1122 * @hfreq: horizontal freq 1123 * 1124 * DESCRIPTION: 1125 * vblank = right_margin + vsync_len + left_margin 1126 * 1127 * given: right_margin = 1 (V_FRONTPORCH) 1128 * vsync_len = 3 1129 * flyback = 550 1130 * 1131 * flyback * hfreq 1132 * left_margin = --------------- - vsync_len 1133 * 1000000 1134 */ 1135 static u32 fb_get_vblank(u32 hfreq) 1136 { 1137 u32 vblank; 1138 1139 vblank = (hfreq * FLYBACK)/1000; 1140 vblank = (vblank + 500)/1000; 1141 return (vblank + V_FRONTPORCH); 1142 } 1143 1144 /** 1145 * fb_get_hblank_by_freq - get horizontal blank time given hfreq 1146 * @hfreq: horizontal freq 1147 * @xres: horizontal resolution in pixels 1148 * 1149 * DESCRIPTION: 1150 * 1151 * xres * duty_cycle 1152 * hblank = ------------------ 1153 * 100 - duty_cycle 1154 * 1155 * duty cycle = percent of htotal assigned to inactive display 1156 * duty cycle = C - (M/Hfreq) 1157 * 1158 * where: C = ((offset - scale factor) * blank_scale) 1159 * -------------------------------------- + scale factor 1160 * 256 1161 * M = blank_scale * gradient 1162 * 1163 */ 1164 static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres) 1165 { 1166 u32 c_val, m_val, duty_cycle, hblank; 1167 1168 c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 + 1169 H_SCALEFACTOR) * 1000; 1170 m_val = (H_BLANKSCALE * H_GRADIENT)/256; 1171 m_val = (m_val * 1000000)/hfreq; 1172 duty_cycle = c_val - m_val; 1173 hblank = (xres * duty_cycle)/(100000 - duty_cycle); 1174 return (hblank); 1175 } 1176 1177 /** 1178 * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock 1179 * @dclk: pixelclock in Hz 1180 * @xres: horizontal resolution in pixels 1181 * 1182 * DESCRIPTION: 1183 * 1184 * xres * duty_cycle 1185 * hblank = ------------------ 1186 * 100 - duty_cycle 1187 * 1188 * duty cycle = percent of htotal assigned to inactive display 1189 * duty cycle = C - (M * h_period) 1190 * 1191 * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100 1192 * ----------------------------------------------- 1193 * 2 * M 1194 * M = 300; 1195 * C = 30; 1196 1197 */ 1198 static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres) 1199 { 1200 u32 duty_cycle, h_period, hblank; 1201 1202 dclk /= 1000; 1203 h_period = 100 - C_VAL; 1204 h_period *= h_period; 1205 h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk); 1206 h_period *= 10000; 1207 1208 h_period = int_sqrt(h_period); 1209 h_period -= (100 - C_VAL) * 100; 1210 h_period *= 1000; 1211 h_period /= 2 * M_VAL; 1212 1213 duty_cycle = C_VAL * 1000 - (M_VAL * h_period)/100; 1214 hblank = (xres * duty_cycle)/(100000 - duty_cycle) + 8; 1215 hblank &= ~15; 1216 return (hblank); 1217 } 1218 1219 /** 1220 * fb_get_hfreq - estimate hsync 1221 * @vfreq: vertical refresh rate 1222 * @yres: vertical resolution 1223 * 1224 * DESCRIPTION: 1225 * 1226 * (yres + front_port) * vfreq * 1000000 1227 * hfreq = ------------------------------------- 1228 * (1000000 - (vfreq * FLYBACK) 1229 * 1230 */ 1231 1232 static u32 fb_get_hfreq(u32 vfreq, u32 yres) 1233 { 1234 u32 divisor, hfreq; 1235 1236 divisor = (1000000 - (vfreq * FLYBACK))/1000; 1237 hfreq = (yres + V_FRONTPORCH) * vfreq * 1000; 1238 return (hfreq/divisor); 1239 } 1240 1241 static void fb_timings_vfreq(struct __fb_timings *timings) 1242 { 1243 timings->hfreq = fb_get_hfreq(timings->vfreq, timings->vactive); 1244 timings->vblank = fb_get_vblank(timings->hfreq); 1245 timings->vtotal = timings->vactive + timings->vblank; 1246 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq, 1247 timings->hactive); 1248 timings->htotal = timings->hactive + timings->hblank; 1249 timings->dclk = timings->htotal * timings->hfreq; 1250 } 1251 1252 static void fb_timings_hfreq(struct __fb_timings *timings) 1253 { 1254 timings->vblank = fb_get_vblank(timings->hfreq); 1255 timings->vtotal = timings->vactive + timings->vblank; 1256 timings->vfreq = timings->hfreq/timings->vtotal; 1257 timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq, 1258 timings->hactive); 1259 timings->htotal = timings->hactive + timings->hblank; 1260 timings->dclk = timings->htotal * timings->hfreq; 1261 } 1262 1263 static void fb_timings_dclk(struct __fb_timings *timings) 1264 { 1265 timings->hblank = fb_get_hblank_by_dclk(timings->dclk, 1266 timings->hactive); 1267 timings->htotal = timings->hactive + timings->hblank; 1268 timings->hfreq = timings->dclk/timings->htotal; 1269 timings->vblank = fb_get_vblank(timings->hfreq); 1270 timings->vtotal = timings->vactive + timings->vblank; 1271 timings->vfreq = timings->hfreq/timings->vtotal; 1272 } 1273 1274 /* 1275 * fb_get_mode - calculates video mode using VESA GTF 1276 * @flags: if: 0 - maximize vertical refresh rate 1277 * 1 - vrefresh-driven calculation; 1278 * 2 - hscan-driven calculation; 1279 * 3 - pixelclock-driven calculation; 1280 * @val: depending on @flags, ignored, vrefresh, hsync or pixelclock 1281 * @var: pointer to fb_var_screeninfo 1282 * @info: pointer to fb_info 1283 * 1284 * DESCRIPTION: 1285 * Calculates video mode based on monitor specs using VESA GTF. 1286 * The GTF is best for VESA GTF compliant monitors but is 1287 * specifically formulated to work for older monitors as well. 1288 * 1289 * If @flag==0, the function will attempt to maximize the 1290 * refresh rate. Otherwise, it will calculate timings based on 1291 * the flag and accompanying value. 1292 * 1293 * If FB_IGNOREMON bit is set in @flags, monitor specs will be 1294 * ignored and @var will be filled with the calculated timings. 1295 * 1296 * All calculations are based on the VESA GTF Spreadsheet 1297 * available at VESA's public ftp (http://www.vesa.org). 1298 * 1299 * NOTES: 1300 * The timings generated by the GTF will be different from VESA 1301 * DMT. It might be a good idea to keep a table of standard 1302 * VESA modes as well. The GTF may also not work for some displays, 1303 * such as, and especially, analog TV. 1304 * 1305 * REQUIRES: 1306 * A valid info->monspecs, otherwise 'safe numbers' will be used. 1307 */ 1308 int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, struct fb_info *info) 1309 { 1310 struct __fb_timings *timings; 1311 u32 interlace = 1, dscan = 1; 1312 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax, err = 0; 1313 1314 1315 timings = kzalloc(sizeof(struct __fb_timings), GFP_KERNEL); 1316 1317 if (!timings) 1318 return -ENOMEM; 1319 1320 /* 1321 * If monspecs are invalid, use values that are enough 1322 * for 640x480@60 1323 */ 1324 if (!info || !info->monspecs.hfmax || !info->monspecs.vfmax || 1325 !info->monspecs.dclkmax || 1326 info->monspecs.hfmax < info->monspecs.hfmin || 1327 info->monspecs.vfmax < info->monspecs.vfmin || 1328 info->monspecs.dclkmax < info->monspecs.dclkmin) { 1329 hfmin = 29000; hfmax = 30000; 1330 vfmin = 60; vfmax = 60; 1331 dclkmin = 0; dclkmax = 25000000; 1332 } else { 1333 hfmin = info->monspecs.hfmin; 1334 hfmax = info->monspecs.hfmax; 1335 vfmin = info->monspecs.vfmin; 1336 vfmax = info->monspecs.vfmax; 1337 dclkmin = info->monspecs.dclkmin; 1338 dclkmax = info->monspecs.dclkmax; 1339 } 1340 1341 timings->hactive = var->xres; 1342 timings->vactive = var->yres; 1343 if (var->vmode & FB_VMODE_INTERLACED) { 1344 timings->vactive /= 2; 1345 interlace = 2; 1346 } 1347 if (var->vmode & FB_VMODE_DOUBLE) { 1348 timings->vactive *= 2; 1349 dscan = 2; 1350 } 1351 1352 switch (flags & ~FB_IGNOREMON) { 1353 case FB_MAXTIMINGS: /* maximize refresh rate */ 1354 timings->hfreq = hfmax; 1355 fb_timings_hfreq(timings); 1356 if (timings->vfreq > vfmax) { 1357 timings->vfreq = vfmax; 1358 fb_timings_vfreq(timings); 1359 } 1360 if (timings->dclk > dclkmax) { 1361 timings->dclk = dclkmax; 1362 fb_timings_dclk(timings); 1363 } 1364 break; 1365 case FB_VSYNCTIMINGS: /* vrefresh driven */ 1366 timings->vfreq = val; 1367 fb_timings_vfreq(timings); 1368 break; 1369 case FB_HSYNCTIMINGS: /* hsync driven */ 1370 timings->hfreq = val; 1371 fb_timings_hfreq(timings); 1372 break; 1373 case FB_DCLKTIMINGS: /* pixelclock driven */ 1374 timings->dclk = PICOS2KHZ(val) * 1000; 1375 fb_timings_dclk(timings); 1376 break; 1377 default: 1378 err = -EINVAL; 1379 1380 } 1381 1382 if (err || (!(flags & FB_IGNOREMON) && 1383 (timings->vfreq < vfmin || timings->vfreq > vfmax || 1384 timings->hfreq < hfmin || timings->hfreq > hfmax || 1385 timings->dclk < dclkmin || timings->dclk > dclkmax))) { 1386 err = -EINVAL; 1387 } else { 1388 var->pixclock = KHZ2PICOS(timings->dclk/1000); 1389 var->hsync_len = (timings->htotal * 8)/100; 1390 var->right_margin = (timings->hblank/2) - var->hsync_len; 1391 var->left_margin = timings->hblank - var->right_margin - 1392 var->hsync_len; 1393 var->vsync_len = (3 * interlace)/dscan; 1394 var->lower_margin = (1 * interlace)/dscan; 1395 var->upper_margin = (timings->vblank * interlace)/dscan - 1396 (var->vsync_len + var->lower_margin); 1397 } 1398 1399 kfree(timings); 1400 return err; 1401 } 1402 1403 #ifdef CONFIG_VIDEOMODE_HELPERS 1404 int fb_videomode_from_videomode(const struct videomode *vm, 1405 struct fb_videomode *fbmode) 1406 { 1407 unsigned int htotal, vtotal; 1408 1409 fbmode->xres = vm->hactive; 1410 fbmode->left_margin = vm->hback_porch; 1411 fbmode->right_margin = vm->hfront_porch; 1412 fbmode->hsync_len = vm->hsync_len; 1413 1414 fbmode->yres = vm->vactive; 1415 fbmode->upper_margin = vm->vback_porch; 1416 fbmode->lower_margin = vm->vfront_porch; 1417 fbmode->vsync_len = vm->vsync_len; 1418 1419 /* prevent division by zero in KHZ2PICOS macro */ 1420 fbmode->pixclock = vm->pixelclock ? 1421 KHZ2PICOS(vm->pixelclock / 1000) : 0; 1422 1423 fbmode->sync = 0; 1424 fbmode->vmode = 0; 1425 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH) 1426 fbmode->sync |= FB_SYNC_HOR_HIGH_ACT; 1427 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH) 1428 fbmode->sync |= FB_SYNC_VERT_HIGH_ACT; 1429 if (vm->flags & DISPLAY_FLAGS_INTERLACED) 1430 fbmode->vmode |= FB_VMODE_INTERLACED; 1431 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN) 1432 fbmode->vmode |= FB_VMODE_DOUBLE; 1433 fbmode->flag = 0; 1434 1435 htotal = vm->hactive + vm->hfront_porch + vm->hback_porch + 1436 vm->hsync_len; 1437 vtotal = vm->vactive + vm->vfront_porch + vm->vback_porch + 1438 vm->vsync_len; 1439 /* prevent division by zero */ 1440 if (htotal && vtotal) { 1441 fbmode->refresh = vm->pixelclock / (htotal * vtotal); 1442 /* a mode must have htotal and vtotal != 0 or it is invalid */ 1443 } else { 1444 fbmode->refresh = 0; 1445 return -EINVAL; 1446 } 1447 1448 return 0; 1449 } 1450 EXPORT_SYMBOL_GPL(fb_videomode_from_videomode); 1451 1452 #ifdef CONFIG_OF 1453 static inline void dump_fb_videomode(const struct fb_videomode *m) 1454 { 1455 pr_debug("fb_videomode = %ux%u@%uHz (%ukHz) %u %u %u %u %u %u %u %u %u\n", 1456 m->xres, m->yres, m->refresh, m->pixclock, m->left_margin, 1457 m->right_margin, m->upper_margin, m->lower_margin, 1458 m->hsync_len, m->vsync_len, m->sync, m->vmode, m->flag); 1459 } 1460 1461 /** 1462 * of_get_fb_videomode - get a fb_videomode from devicetree 1463 * @np: device_node with the timing specification 1464 * @fb: will be set to the return value 1465 * @index: index into the list of display timings in devicetree 1466 * 1467 * DESCRIPTION: 1468 * This function is expensive and should only be used, if only one mode is to be 1469 * read from DT. To get multiple modes start with of_get_display_timings ond 1470 * work with that instead. 1471 */ 1472 int of_get_fb_videomode(struct device_node *np, struct fb_videomode *fb, 1473 int index) 1474 { 1475 struct videomode vm; 1476 int ret; 1477 1478 ret = of_get_videomode(np, &vm, index); 1479 if (ret) 1480 return ret; 1481 1482 fb_videomode_from_videomode(&vm, fb); 1483 1484 pr_debug("%s: got %dx%d display mode from %s\n", 1485 of_node_full_name(np), vm.hactive, vm.vactive, np->name); 1486 dump_fb_videomode(fb); 1487 1488 return 0; 1489 } 1490 EXPORT_SYMBOL_GPL(of_get_fb_videomode); 1491 #endif /* CONFIG_OF */ 1492 #endif /* CONFIG_VIDEOMODE_HELPERS */ 1493 1494 #else 1495 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var) 1496 { 1497 return 1; 1498 } 1499 void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs) 1500 { 1501 specs = NULL; 1502 } 1503 void fb_edid_add_monspecs(unsigned char *edid, struct fb_monspecs *specs) 1504 { 1505 } 1506 void fb_destroy_modedb(struct fb_videomode *modedb) 1507 { 1508 } 1509 int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, 1510 struct fb_info *info) 1511 { 1512 return -EINVAL; 1513 } 1514 #endif /* CONFIG_FB_MODE_HELPERS */ 1515 1516 /* 1517 * fb_validate_mode - validates var against monitor capabilities 1518 * @var: pointer to fb_var_screeninfo 1519 * @info: pointer to fb_info 1520 * 1521 * DESCRIPTION: 1522 * Validates video mode against monitor capabilities specified in 1523 * info->monspecs. 1524 * 1525 * REQUIRES: 1526 * A valid info->monspecs. 1527 */ 1528 int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info) 1529 { 1530 u32 hfreq, vfreq, htotal, vtotal, pixclock; 1531 u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax; 1532 1533 /* 1534 * If monspecs are invalid, use values that are enough 1535 * for 640x480@60 1536 */ 1537 if (!info->monspecs.hfmax || !info->monspecs.vfmax || 1538 !info->monspecs.dclkmax || 1539 info->monspecs.hfmax < info->monspecs.hfmin || 1540 info->monspecs.vfmax < info->monspecs.vfmin || 1541 info->monspecs.dclkmax < info->monspecs.dclkmin) { 1542 hfmin = 29000; hfmax = 30000; 1543 vfmin = 60; vfmax = 60; 1544 dclkmin = 0; dclkmax = 25000000; 1545 } else { 1546 hfmin = info->monspecs.hfmin; 1547 hfmax = info->monspecs.hfmax; 1548 vfmin = info->monspecs.vfmin; 1549 vfmax = info->monspecs.vfmax; 1550 dclkmin = info->monspecs.dclkmin; 1551 dclkmax = info->monspecs.dclkmax; 1552 } 1553 1554 if (!var->pixclock) 1555 return -EINVAL; 1556 pixclock = PICOS2KHZ(var->pixclock) * 1000; 1557 1558 htotal = var->xres + var->right_margin + var->hsync_len + 1559 var->left_margin; 1560 vtotal = var->yres + var->lower_margin + var->vsync_len + 1561 var->upper_margin; 1562 1563 if (var->vmode & FB_VMODE_INTERLACED) 1564 vtotal /= 2; 1565 if (var->vmode & FB_VMODE_DOUBLE) 1566 vtotal *= 2; 1567 1568 hfreq = pixclock/htotal; 1569 hfreq = (hfreq + 500) / 1000 * 1000; 1570 1571 vfreq = hfreq/vtotal; 1572 1573 return (vfreq < vfmin || vfreq > vfmax || 1574 hfreq < hfmin || hfreq > hfmax || 1575 pixclock < dclkmin || pixclock > dclkmax) ? 1576 -EINVAL : 0; 1577 } 1578 1579 #if defined(CONFIG_FIRMWARE_EDID) && defined(CONFIG_X86) 1580 1581 /* 1582 * We need to ensure that the EDID block is only returned for 1583 * the primary graphics adapter. 1584 */ 1585 1586 const unsigned char *fb_firmware_edid(struct device *device) 1587 { 1588 struct pci_dev *dev = NULL; 1589 struct resource *res = NULL; 1590 unsigned char *edid = NULL; 1591 1592 if (device) 1593 dev = to_pci_dev(device); 1594 1595 if (dev) 1596 res = &dev->resource[PCI_ROM_RESOURCE]; 1597 1598 if (res && res->flags & IORESOURCE_ROM_SHADOW) 1599 edid = edid_info.dummy; 1600 1601 return edid; 1602 } 1603 #else 1604 const unsigned char *fb_firmware_edid(struct device *device) 1605 { 1606 return NULL; 1607 } 1608 #endif 1609 EXPORT_SYMBOL(fb_firmware_edid); 1610 1611 EXPORT_SYMBOL(fb_parse_edid); 1612 EXPORT_SYMBOL(fb_edid_to_monspecs); 1613 EXPORT_SYMBOL(fb_edid_add_monspecs); 1614 EXPORT_SYMBOL(fb_get_mode); 1615 EXPORT_SYMBOL(fb_validate_mode); 1616 EXPORT_SYMBOL(fb_destroy_modedb); 1617