fbmon.c 37 KB

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