fbmon.c 33 KB

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