radeon_monitor.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "radeonfb.h"
  3. #include <linux/slab.h>
  4. #include "../edid.h"
  5. static const struct fb_var_screeninfo radeonfb_default_var = {
  6. .xres = 640,
  7. .yres = 480,
  8. .xres_virtual = 640,
  9. .yres_virtual = 480,
  10. .bits_per_pixel = 8,
  11. .red = { .length = 8 },
  12. .green = { .length = 8 },
  13. .blue = { .length = 8 },
  14. .activate = FB_ACTIVATE_NOW,
  15. .height = -1,
  16. .width = -1,
  17. .pixclock = 39721,
  18. .left_margin = 40,
  19. .right_margin = 24,
  20. .upper_margin = 32,
  21. .lower_margin = 11,
  22. .hsync_len = 96,
  23. .vsync_len = 2,
  24. .vmode = FB_VMODE_NONINTERLACED
  25. };
  26. static char *radeon_get_mon_name(int type)
  27. {
  28. char *pret = NULL;
  29. switch (type) {
  30. case MT_NONE:
  31. pret = "no";
  32. break;
  33. case MT_CRT:
  34. pret = "CRT";
  35. break;
  36. case MT_DFP:
  37. pret = "DFP";
  38. break;
  39. case MT_LCD:
  40. pret = "LCD";
  41. break;
  42. case MT_CTV:
  43. pret = "CTV";
  44. break;
  45. case MT_STV:
  46. pret = "STV";
  47. break;
  48. }
  49. return pret;
  50. }
  51. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  52. /*
  53. * Try to find monitor informations & EDID data out of the Open Firmware
  54. * device-tree. This also contains some "hacks" to work around a few machine
  55. * models with broken OF probing by hard-coding known EDIDs for some Mac
  56. * laptops internal LVDS panel. (XXX: not done yet)
  57. */
  58. static int radeon_parse_montype_prop(struct device_node *dp, u8 **out_EDID,
  59. int hdno)
  60. {
  61. static char *propnames[] = { "DFP,EDID", "LCD,EDID", "EDID",
  62. "EDID1", "EDID2", NULL };
  63. const u8 *pedid = NULL;
  64. const u8 *pmt = NULL;
  65. u8 *tmp;
  66. int i, mt = MT_NONE;
  67. pr_debug("analyzing OF properties...\n");
  68. pmt = of_get_property(dp, "display-type", NULL);
  69. if (!pmt)
  70. return MT_NONE;
  71. pr_debug("display-type: %s\n", pmt);
  72. /* OF says "LCD" for DFP as well, we discriminate from the caller of this
  73. * function
  74. */
  75. if (!strcmp(pmt, "LCD") || !strcmp(pmt, "DFP"))
  76. mt = MT_DFP;
  77. else if (!strcmp(pmt, "CRT"))
  78. mt = MT_CRT;
  79. else {
  80. if (strcmp(pmt, "NONE") != 0)
  81. printk(KERN_WARNING "radeonfb: Unknown OF display-type: %s\n",
  82. pmt);
  83. return MT_NONE;
  84. }
  85. for (i = 0; propnames[i] != NULL; ++i) {
  86. pedid = of_get_property(dp, propnames[i], NULL);
  87. if (pedid != NULL)
  88. break;
  89. }
  90. /* We didn't find the EDID in the leaf node, some cards will actually
  91. * put EDID1/EDID2 in the parent, look for these (typically M6 tipb).
  92. * single-head cards have hdno == -1 and skip this step
  93. */
  94. if (pedid == NULL && dp->parent && (hdno != -1))
  95. pedid = of_get_property(dp->parent,
  96. (hdno == 0) ? "EDID1" : "EDID2", NULL);
  97. if (pedid == NULL && dp->parent && (hdno == 0))
  98. pedid = of_get_property(dp->parent, "EDID", NULL);
  99. if (pedid == NULL)
  100. return mt;
  101. tmp = kmemdup(pedid, EDID_LENGTH, GFP_KERNEL);
  102. if (!tmp)
  103. return mt;
  104. *out_EDID = tmp;
  105. return mt;
  106. }
  107. static int radeon_probe_OF_head(struct radeonfb_info *rinfo, int head_no,
  108. u8 **out_EDID)
  109. {
  110. struct device_node *dp;
  111. pr_debug("radeon_probe_OF_head\n");
  112. dp = rinfo->of_node;
  113. while (dp == NULL)
  114. return MT_NONE;
  115. if (rinfo->has_CRTC2) {
  116. const char *pname;
  117. int len, second = 0;
  118. dp = dp->child;
  119. do {
  120. if (!dp)
  121. return MT_NONE;
  122. pname = of_get_property(dp, "name", NULL);
  123. if (!pname)
  124. return MT_NONE;
  125. len = strlen(pname);
  126. pr_debug("head: %s (letter: %c, head_no: %d)\n",
  127. pname, pname[len-1], head_no);
  128. if (pname[len-1] == 'A' && head_no == 0) {
  129. int mt = radeon_parse_montype_prop(dp, out_EDID, 0);
  130. /* Maybe check for LVDS_GEN_CNTL here ? I need to check out
  131. * what OF does when booting with lid closed
  132. */
  133. if (mt == MT_DFP && rinfo->is_mobility)
  134. mt = MT_LCD;
  135. return mt;
  136. } else if (pname[len-1] == 'B' && head_no == 1)
  137. return radeon_parse_montype_prop(dp, out_EDID, 1);
  138. second = 1;
  139. dp = dp->sibling;
  140. } while(!second);
  141. } else {
  142. if (head_no > 0)
  143. return MT_NONE;
  144. return radeon_parse_montype_prop(dp, out_EDID, -1);
  145. }
  146. return MT_NONE;
  147. }
  148. #endif /* CONFIG_PPC || CONFIG_SPARC */
  149. static int radeon_get_panel_info_BIOS(struct radeonfb_info *rinfo)
  150. {
  151. unsigned long tmp, tmp0;
  152. char stmp[30];
  153. int i;
  154. if (!rinfo->bios_seg)
  155. return 0;
  156. if (!(tmp = BIOS_IN16(rinfo->fp_bios_start + 0x40))) {
  157. printk(KERN_ERR "radeonfb: Failed to detect DFP panel info using BIOS\n");
  158. rinfo->panel_info.pwr_delay = 200;
  159. return 0;
  160. }
  161. for(i=0; i<24; i++)
  162. stmp[i] = BIOS_IN8(tmp+i+1);
  163. stmp[24] = 0;
  164. printk("radeonfb: panel ID string: %s\n", stmp);
  165. rinfo->panel_info.xres = BIOS_IN16(tmp + 25);
  166. rinfo->panel_info.yres = BIOS_IN16(tmp + 27);
  167. printk("radeonfb: detected LVDS panel size from BIOS: %dx%d\n",
  168. rinfo->panel_info.xres, rinfo->panel_info.yres);
  169. rinfo->panel_info.pwr_delay = BIOS_IN16(tmp + 44);
  170. pr_debug("BIOS provided panel power delay: %d\n", rinfo->panel_info.pwr_delay);
  171. if (rinfo->panel_info.pwr_delay > 2000 || rinfo->panel_info.pwr_delay <= 0)
  172. rinfo->panel_info.pwr_delay = 2000;
  173. /*
  174. * Some panels only work properly with some divider combinations
  175. */
  176. rinfo->panel_info.ref_divider = BIOS_IN16(tmp + 46);
  177. rinfo->panel_info.post_divider = BIOS_IN8(tmp + 48);
  178. rinfo->panel_info.fbk_divider = BIOS_IN16(tmp + 49);
  179. if (rinfo->panel_info.ref_divider != 0 &&
  180. rinfo->panel_info.fbk_divider > 3) {
  181. rinfo->panel_info.use_bios_dividers = 1;
  182. printk(KERN_INFO "radeondb: BIOS provided dividers will be used\n");
  183. pr_debug("ref_divider = %x\n", rinfo->panel_info.ref_divider);
  184. pr_debug("post_divider = %x\n", rinfo->panel_info.post_divider);
  185. pr_debug("fbk_divider = %x\n", rinfo->panel_info.fbk_divider);
  186. }
  187. pr_debug("Scanning BIOS table ...\n");
  188. for(i=0; i<32; i++) {
  189. tmp0 = BIOS_IN16(tmp+64+i*2);
  190. if (tmp0 == 0)
  191. break;
  192. pr_debug(" %d x %d\n", BIOS_IN16(tmp0), BIOS_IN16(tmp0+2));
  193. if ((BIOS_IN16(tmp0) == rinfo->panel_info.xres) &&
  194. (BIOS_IN16(tmp0+2) == rinfo->panel_info.yres)) {
  195. rinfo->panel_info.hblank = (BIOS_IN16(tmp0+17) - BIOS_IN16(tmp0+19)) * 8;
  196. rinfo->panel_info.hOver_plus = ((BIOS_IN16(tmp0+21) -
  197. BIOS_IN16(tmp0+19) -1) * 8) & 0x7fff;
  198. rinfo->panel_info.hSync_width = BIOS_IN8(tmp0+23) * 8;
  199. rinfo->panel_info.vblank = BIOS_IN16(tmp0+24) - BIOS_IN16(tmp0+26);
  200. rinfo->panel_info.vOver_plus = (BIOS_IN16(tmp0+28) & 0x7ff) - BIOS_IN16(tmp0+26);
  201. rinfo->panel_info.vSync_width = (BIOS_IN16(tmp0+28) & 0xf800) >> 11;
  202. rinfo->panel_info.clock = BIOS_IN16(tmp0+9);
  203. /* Assume high active syncs for now until ATI tells me more... maybe we
  204. * can probe register values here ?
  205. */
  206. rinfo->panel_info.hAct_high = 1;
  207. rinfo->panel_info.vAct_high = 1;
  208. /* Mark panel infos valid */
  209. rinfo->panel_info.valid = 1;
  210. pr_debug("Found panel in BIOS table:\n");
  211. pr_debug(" hblank: %d\n", rinfo->panel_info.hblank);
  212. pr_debug(" hOver_plus: %d\n", rinfo->panel_info.hOver_plus);
  213. pr_debug(" hSync_width: %d\n", rinfo->panel_info.hSync_width);
  214. pr_debug(" vblank: %d\n", rinfo->panel_info.vblank);
  215. pr_debug(" vOver_plus: %d\n", rinfo->panel_info.vOver_plus);
  216. pr_debug(" vSync_width: %d\n", rinfo->panel_info.vSync_width);
  217. pr_debug(" clock: %d\n", rinfo->panel_info.clock);
  218. return 1;
  219. }
  220. }
  221. pr_debug("Didn't find panel in BIOS table !\n");
  222. return 0;
  223. }
  224. /* Try to extract the connector informations from the BIOS. This
  225. * doesn't quite work yet, but it's output is still useful for
  226. * debugging
  227. */
  228. static void radeon_parse_connector_info(struct radeonfb_info *rinfo)
  229. {
  230. int offset, chips, connectors, tmp, i, conn, type;
  231. static char* __conn_type_table[16] = {
  232. "NONE", "Proprietary", "CRT", "DVI-I", "DVI-D", "Unknown", "Unknown",
  233. "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown",
  234. "Unknown", "Unknown", "Unknown"
  235. };
  236. if (!rinfo->bios_seg)
  237. return;
  238. offset = BIOS_IN16(rinfo->fp_bios_start + 0x50);
  239. if (offset == 0) {
  240. printk(KERN_WARNING "radeonfb: No connector info table detected\n");
  241. return;
  242. }
  243. /* Don't do much more at this point but displaying the data if
  244. * DEBUG is enabled
  245. */
  246. chips = BIOS_IN8(offset++) >> 4;
  247. pr_debug("%d chips in connector info\n", chips);
  248. for (i = 0; i < chips; i++) {
  249. tmp = BIOS_IN8(offset++);
  250. connectors = tmp & 0x0f;
  251. pr_debug(" - chip %d has %d connectors\n", tmp >> 4, connectors);
  252. for (conn = 0; ; conn++) {
  253. tmp = BIOS_IN16(offset);
  254. if (tmp == 0)
  255. break;
  256. offset += 2;
  257. type = (tmp >> 12) & 0x0f;
  258. pr_debug(" * connector %d of type %d (%s) : %04x\n",
  259. conn, type, __conn_type_table[type], tmp);
  260. }
  261. }
  262. }
  263. /*
  264. * Probe physical connection of a CRT. This code comes from XFree
  265. * as well and currently is only implemented for the CRT DAC, the
  266. * code for the TVDAC is commented out in XFree as "non working"
  267. */
  268. static int radeon_crt_is_connected(struct radeonfb_info *rinfo, int is_crt_dac)
  269. {
  270. int connected = 0;
  271. /* the monitor either wasn't connected or it is a non-DDC CRT.
  272. * try to probe it
  273. */
  274. if (is_crt_dac) {
  275. unsigned long ulOrigVCLK_ECP_CNTL;
  276. unsigned long ulOrigDAC_CNTL;
  277. unsigned long ulOrigDAC_EXT_CNTL;
  278. unsigned long ulOrigCRTC_EXT_CNTL;
  279. unsigned long ulData;
  280. unsigned long ulMask;
  281. ulOrigVCLK_ECP_CNTL = INPLL(VCLK_ECP_CNTL);
  282. ulData = ulOrigVCLK_ECP_CNTL;
  283. ulData &= ~(PIXCLK_ALWAYS_ONb
  284. | PIXCLK_DAC_ALWAYS_ONb);
  285. ulMask = ~(PIXCLK_ALWAYS_ONb
  286. | PIXCLK_DAC_ALWAYS_ONb);
  287. OUTPLLP(VCLK_ECP_CNTL, ulData, ulMask);
  288. ulOrigCRTC_EXT_CNTL = INREG(CRTC_EXT_CNTL);
  289. ulData = ulOrigCRTC_EXT_CNTL;
  290. ulData |= CRTC_CRT_ON;
  291. OUTREG(CRTC_EXT_CNTL, ulData);
  292. ulOrigDAC_EXT_CNTL = INREG(DAC_EXT_CNTL);
  293. ulData = ulOrigDAC_EXT_CNTL;
  294. ulData &= ~DAC_FORCE_DATA_MASK;
  295. ulData |= (DAC_FORCE_BLANK_OFF_EN
  296. |DAC_FORCE_DATA_EN
  297. |DAC_FORCE_DATA_SEL_MASK);
  298. if ((rinfo->family == CHIP_FAMILY_RV250) ||
  299. (rinfo->family == CHIP_FAMILY_RV280))
  300. ulData |= (0x01b6 << DAC_FORCE_DATA_SHIFT);
  301. else
  302. ulData |= (0x01ac << DAC_FORCE_DATA_SHIFT);
  303. OUTREG(DAC_EXT_CNTL, ulData);
  304. ulOrigDAC_CNTL = INREG(DAC_CNTL);
  305. ulData = ulOrigDAC_CNTL;
  306. ulData |= DAC_CMP_EN;
  307. ulData &= ~(DAC_RANGE_CNTL_MASK
  308. | DAC_PDWN);
  309. ulData |= 0x2;
  310. OUTREG(DAC_CNTL, ulData);
  311. mdelay(1);
  312. ulData = INREG(DAC_CNTL);
  313. connected = (DAC_CMP_OUTPUT & ulData) ? 1 : 0;
  314. ulData = ulOrigVCLK_ECP_CNTL;
  315. ulMask = 0xFFFFFFFFL;
  316. OUTPLLP(VCLK_ECP_CNTL, ulData, ulMask);
  317. OUTREG(DAC_CNTL, ulOrigDAC_CNTL );
  318. OUTREG(DAC_EXT_CNTL, ulOrigDAC_EXT_CNTL );
  319. OUTREG(CRTC_EXT_CNTL, ulOrigCRTC_EXT_CNTL);
  320. }
  321. return connected ? MT_CRT : MT_NONE;
  322. }
  323. /*
  324. * Parse the "monitor_layout" string if any. This code is mostly
  325. * copied from XFree's radeon driver
  326. */
  327. static int radeon_parse_monitor_layout(struct radeonfb_info *rinfo,
  328. const char *monitor_layout)
  329. {
  330. char s1[5], s2[5];
  331. int i = 0, second = 0;
  332. const char *s;
  333. if (!monitor_layout)
  334. return 0;
  335. s = monitor_layout;
  336. do {
  337. switch(*s) {
  338. case ',':
  339. s1[i] = '\0';
  340. i = 0;
  341. second = 1;
  342. break;
  343. case ' ':
  344. case '\0':
  345. break;
  346. default:
  347. if (i > 4)
  348. break;
  349. if (second)
  350. s2[i] = *s;
  351. else
  352. s1[i] = *s;
  353. i++;
  354. }
  355. if (i > 4)
  356. i = 4;
  357. } while (*s++);
  358. if (second)
  359. s2[i] = 0;
  360. else {
  361. s1[i] = 0;
  362. s2[0] = 0;
  363. }
  364. if (strcmp(s1, "CRT") == 0)
  365. rinfo->mon1_type = MT_CRT;
  366. else if (strcmp(s1, "TMDS") == 0)
  367. rinfo->mon1_type = MT_DFP;
  368. else if (strcmp(s1, "LVDS") == 0)
  369. rinfo->mon1_type = MT_LCD;
  370. if (strcmp(s2, "CRT") == 0)
  371. rinfo->mon2_type = MT_CRT;
  372. else if (strcmp(s2, "TMDS") == 0)
  373. rinfo->mon2_type = MT_DFP;
  374. else if (strcmp(s2, "LVDS") == 0)
  375. rinfo->mon2_type = MT_LCD;
  376. return 1;
  377. }
  378. /*
  379. * Probe display on both primary and secondary card's connector (if any)
  380. * by various available techniques (i2c, OF device tree, BIOS, ...) and
  381. * try to retrieve EDID. The algorithm here comes from XFree's radeon
  382. * driver
  383. */
  384. void radeon_probe_screens(struct radeonfb_info *rinfo,
  385. const char *monitor_layout, int ignore_edid)
  386. {
  387. #ifdef CONFIG_FB_RADEON_I2C
  388. int ddc_crt2_used = 0;
  389. #endif
  390. int tmp, i;
  391. radeon_parse_connector_info(rinfo);
  392. if (radeon_parse_monitor_layout(rinfo, monitor_layout)) {
  393. /*
  394. * If user specified a monitor_layout option, use it instead
  395. * of auto-detecting. Maybe we should only use this argument
  396. * on the first radeon card probed or provide a way to specify
  397. * a layout for each card ?
  398. */
  399. pr_debug("Using specified monitor layout: %s", monitor_layout);
  400. #ifdef CONFIG_FB_RADEON_I2C
  401. if (!ignore_edid) {
  402. if (rinfo->mon1_type != MT_NONE)
  403. if (!radeon_probe_i2c_connector(rinfo, ddc_dvi, &rinfo->mon1_EDID)) {
  404. radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon1_EDID);
  405. ddc_crt2_used = 1;
  406. }
  407. if (rinfo->mon2_type != MT_NONE)
  408. if (!radeon_probe_i2c_connector(rinfo, ddc_vga, &rinfo->mon2_EDID) &&
  409. !ddc_crt2_used)
  410. radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon2_EDID);
  411. }
  412. #endif /* CONFIG_FB_RADEON_I2C */
  413. if (rinfo->mon1_type == MT_NONE) {
  414. if (rinfo->mon2_type != MT_NONE) {
  415. rinfo->mon1_type = rinfo->mon2_type;
  416. rinfo->mon1_EDID = rinfo->mon2_EDID;
  417. } else {
  418. rinfo->mon1_type = MT_CRT;
  419. printk(KERN_INFO "radeonfb: No valid monitor, assuming CRT on first port\n");
  420. }
  421. rinfo->mon2_type = MT_NONE;
  422. rinfo->mon2_EDID = NULL;
  423. }
  424. } else {
  425. /*
  426. * Auto-detecting display type (well... trying to ...)
  427. */
  428. pr_debug("Starting monitor auto detection...\n");
  429. #if defined(DEBUG) && defined(CONFIG_FB_RADEON_I2C)
  430. {
  431. u8 *EDIDs[4] = { NULL, NULL, NULL, NULL };
  432. int i;
  433. for (i = 0; i < 4; i++)
  434. radeon_probe_i2c_connector(rinfo, i + 1, &EDIDs[i]);
  435. }
  436. #endif /* DEBUG */
  437. /*
  438. * Old single head cards
  439. */
  440. if (!rinfo->has_CRTC2) {
  441. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  442. if (rinfo->mon1_type == MT_NONE)
  443. rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
  444. &rinfo->mon1_EDID);
  445. #endif /* CONFIG_PPC || CONFIG_SPARC */
  446. #ifdef CONFIG_FB_RADEON_I2C
  447. if (rinfo->mon1_type == MT_NONE)
  448. rinfo->mon1_type =
  449. radeon_probe_i2c_connector(rinfo, ddc_dvi,
  450. &rinfo->mon1_EDID);
  451. if (rinfo->mon1_type == MT_NONE)
  452. rinfo->mon1_type =
  453. radeon_probe_i2c_connector(rinfo, ddc_vga,
  454. &rinfo->mon1_EDID);
  455. if (rinfo->mon1_type == MT_NONE)
  456. rinfo->mon1_type =
  457. radeon_probe_i2c_connector(rinfo, ddc_crt2,
  458. &rinfo->mon1_EDID);
  459. #endif /* CONFIG_FB_RADEON_I2C */
  460. if (rinfo->mon1_type == MT_NONE)
  461. rinfo->mon1_type = MT_CRT;
  462. goto bail;
  463. }
  464. /*
  465. * Check for cards with reversed DACs or TMDS controllers using BIOS
  466. */
  467. if (rinfo->bios_seg &&
  468. (tmp = BIOS_IN16(rinfo->fp_bios_start + 0x50))) {
  469. for (i = 1; i < 4; i++) {
  470. unsigned int tmp0;
  471. if (!BIOS_IN8(tmp + i*2) && i > 1)
  472. break;
  473. tmp0 = BIOS_IN16(tmp + i*2);
  474. if ((!(tmp0 & 0x01)) && (((tmp0 >> 8) & 0x0f) == ddc_dvi)) {
  475. rinfo->reversed_DAC = 1;
  476. printk(KERN_INFO "radeonfb: Reversed DACs detected\n");
  477. }
  478. if ((((tmp0 >> 8) & 0x0f) == ddc_dvi) && ((tmp0 >> 4) & 0x01)) {
  479. rinfo->reversed_TMDS = 1;
  480. printk(KERN_INFO "radeonfb: Reversed TMDS detected\n");
  481. }
  482. }
  483. }
  484. /*
  485. * Probe primary head (DVI or laptop internal panel)
  486. */
  487. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  488. if (rinfo->mon1_type == MT_NONE)
  489. rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
  490. &rinfo->mon1_EDID);
  491. #endif /* CONFIG_PPC || CONFIG_SPARC */
  492. #ifdef CONFIG_FB_RADEON_I2C
  493. if (rinfo->mon1_type == MT_NONE)
  494. rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
  495. &rinfo->mon1_EDID);
  496. if (rinfo->mon1_type == MT_NONE) {
  497. rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
  498. &rinfo->mon1_EDID);
  499. if (rinfo->mon1_type != MT_NONE)
  500. ddc_crt2_used = 1;
  501. }
  502. #endif /* CONFIG_FB_RADEON_I2C */
  503. if (rinfo->mon1_type == MT_NONE && rinfo->is_mobility &&
  504. ((rinfo->bios_seg && (INREG(BIOS_4_SCRATCH) & 4))
  505. || (INREG(LVDS_GEN_CNTL) & LVDS_ON))) {
  506. rinfo->mon1_type = MT_LCD;
  507. printk("Non-DDC laptop panel detected\n");
  508. }
  509. if (rinfo->mon1_type == MT_NONE)
  510. rinfo->mon1_type = radeon_crt_is_connected(rinfo, rinfo->reversed_DAC);
  511. /*
  512. * Probe secondary head (mostly VGA, can be DVI)
  513. */
  514. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  515. if (rinfo->mon2_type == MT_NONE)
  516. rinfo->mon2_type = radeon_probe_OF_head(rinfo, 1,
  517. &rinfo->mon2_EDID);
  518. #endif /* CONFIG_PPC || defined(CONFIG_SPARC) */
  519. #ifdef CONFIG_FB_RADEON_I2C
  520. if (rinfo->mon2_type == MT_NONE)
  521. rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_vga,
  522. &rinfo->mon2_EDID);
  523. if (rinfo->mon2_type == MT_NONE && !ddc_crt2_used)
  524. rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
  525. &rinfo->mon2_EDID);
  526. #endif /* CONFIG_FB_RADEON_I2C */
  527. if (rinfo->mon2_type == MT_NONE)
  528. rinfo->mon2_type = radeon_crt_is_connected(rinfo, !rinfo->reversed_DAC);
  529. /*
  530. * If we only detected port 2, we swap them, if none detected,
  531. * assume CRT (maybe fallback to old BIOS_SCRATCH stuff ? or look
  532. * at FP registers ?)
  533. */
  534. if (rinfo->mon1_type == MT_NONE) {
  535. if (rinfo->mon2_type != MT_NONE) {
  536. rinfo->mon1_type = rinfo->mon2_type;
  537. rinfo->mon1_EDID = rinfo->mon2_EDID;
  538. } else
  539. rinfo->mon1_type = MT_CRT;
  540. rinfo->mon2_type = MT_NONE;
  541. rinfo->mon2_EDID = NULL;
  542. }
  543. /*
  544. * Deal with reversed TMDS
  545. */
  546. if (rinfo->reversed_TMDS) {
  547. /* Always keep internal TMDS as primary head */
  548. if (rinfo->mon1_type == MT_DFP || rinfo->mon2_type == MT_DFP) {
  549. int tmp_type = rinfo->mon1_type;
  550. u8 *tmp_EDID = rinfo->mon1_EDID;
  551. rinfo->mon1_type = rinfo->mon2_type;
  552. rinfo->mon1_EDID = rinfo->mon2_EDID;
  553. rinfo->mon2_type = tmp_type;
  554. rinfo->mon2_EDID = tmp_EDID;
  555. if (rinfo->mon1_type == MT_CRT || rinfo->mon2_type == MT_CRT)
  556. rinfo->reversed_DAC ^= 1;
  557. }
  558. }
  559. }
  560. if (ignore_edid) {
  561. kfree(rinfo->mon1_EDID);
  562. rinfo->mon1_EDID = NULL;
  563. kfree(rinfo->mon2_EDID);
  564. rinfo->mon2_EDID = NULL;
  565. }
  566. bail:
  567. printk(KERN_INFO "radeonfb: Monitor 1 type %s found\n",
  568. radeon_get_mon_name(rinfo->mon1_type));
  569. if (rinfo->mon1_EDID)
  570. printk(KERN_INFO "radeonfb: EDID probed\n");
  571. if (!rinfo->has_CRTC2)
  572. return;
  573. printk(KERN_INFO "radeonfb: Monitor 2 type %s found\n",
  574. radeon_get_mon_name(rinfo->mon2_type));
  575. if (rinfo->mon2_EDID)
  576. printk(KERN_INFO "radeonfb: EDID probed\n");
  577. }
  578. /*
  579. * This function applies any arch/model/machine specific fixups
  580. * to the panel info. It may eventually alter EDID block as
  581. * well or whatever is specific to a given model and not probed
  582. * properly by the default code
  583. */
  584. static void radeon_fixup_panel_info(struct radeonfb_info *rinfo)
  585. {
  586. #ifdef CONFIG_PPC
  587. /*
  588. * LCD Flat panels should use fixed dividers, we enfore that on
  589. * PPC only for now...
  590. */
  591. if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type == MT_LCD
  592. && rinfo->is_mobility) {
  593. int ppll_div_sel;
  594. u32 ppll_divn;
  595. ppll_div_sel = INREG8(CLOCK_CNTL_INDEX + 1) & 0x3;
  596. radeon_pll_errata_after_index(rinfo);
  597. ppll_divn = INPLL(PPLL_DIV_0 + ppll_div_sel);
  598. rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
  599. rinfo->panel_info.fbk_divider = ppll_divn & 0x7ff;
  600. rinfo->panel_info.post_divider = (ppll_divn >> 16) & 0x7;
  601. rinfo->panel_info.use_bios_dividers = 1;
  602. printk(KERN_DEBUG "radeonfb: Using Firmware dividers 0x%08x "
  603. "from PPLL %d\n",
  604. rinfo->panel_info.fbk_divider |
  605. (rinfo->panel_info.post_divider << 16),
  606. ppll_div_sel);
  607. }
  608. #endif /* CONFIG_PPC */
  609. }
  610. /*
  611. * Fill up panel infos from a mode definition, either returned by the EDID
  612. * or from the default mode when we can't do any better
  613. */
  614. static void radeon_var_to_panel_info(struct radeonfb_info *rinfo, struct fb_var_screeninfo *var)
  615. {
  616. rinfo->panel_info.xres = var->xres;
  617. rinfo->panel_info.yres = var->yres;
  618. rinfo->panel_info.clock = 100000000 / var->pixclock;
  619. rinfo->panel_info.hOver_plus = var->right_margin;
  620. rinfo->panel_info.hSync_width = var->hsync_len;
  621. rinfo->panel_info.hblank = var->left_margin +
  622. (var->right_margin + var->hsync_len);
  623. rinfo->panel_info.vOver_plus = var->lower_margin;
  624. rinfo->panel_info.vSync_width = var->vsync_len;
  625. rinfo->panel_info.vblank = var->upper_margin +
  626. (var->lower_margin + var->vsync_len);
  627. rinfo->panel_info.hAct_high =
  628. (var->sync & FB_SYNC_HOR_HIGH_ACT) != 0;
  629. rinfo->panel_info.vAct_high =
  630. (var->sync & FB_SYNC_VERT_HIGH_ACT) != 0;
  631. rinfo->panel_info.valid = 1;
  632. /* We use a default of 200ms for the panel power delay,
  633. * I need to have a real schedule() instead of mdelay's in the panel code.
  634. * we might be possible to figure out a better power delay either from
  635. * MacOS OF tree or from the EDID block (proprietary extensions ?)
  636. */
  637. rinfo->panel_info.pwr_delay = 200;
  638. }
  639. static void radeon_videomode_to_var(struct fb_var_screeninfo *var,
  640. const struct fb_videomode *mode)
  641. {
  642. var->xres = mode->xres;
  643. var->yres = mode->yres;
  644. var->xres_virtual = mode->xres;
  645. var->yres_virtual = mode->yres;
  646. var->xoffset = 0;
  647. var->yoffset = 0;
  648. var->pixclock = mode->pixclock;
  649. var->left_margin = mode->left_margin;
  650. var->right_margin = mode->right_margin;
  651. var->upper_margin = mode->upper_margin;
  652. var->lower_margin = mode->lower_margin;
  653. var->hsync_len = mode->hsync_len;
  654. var->vsync_len = mode->vsync_len;
  655. var->sync = mode->sync;
  656. var->vmode = mode->vmode;
  657. }
  658. #ifdef CONFIG_PPC_PSERIES
  659. static int is_powerblade(const char *model)
  660. {
  661. struct device_node *root;
  662. const char* cp;
  663. int len, l, rc = 0;
  664. root = of_find_node_by_path("/");
  665. if (root && model) {
  666. l = strlen(model);
  667. cp = of_get_property(root, "model", &len);
  668. if (cp)
  669. rc = memcmp(model, cp, min(len, l)) == 0;
  670. of_node_put(root);
  671. }
  672. return rc;
  673. }
  674. #endif
  675. /*
  676. * Build the modedb for head 1 (head 2 will come later), check panel infos
  677. * from either BIOS or EDID, and pick up the default mode
  678. */
  679. void radeon_check_modes(struct radeonfb_info *rinfo, const char *mode_option)
  680. {
  681. struct fb_info * info = rinfo->info;
  682. int has_default_mode = 0;
  683. /*
  684. * Fill default var first
  685. */
  686. info->var = radeonfb_default_var;
  687. INIT_LIST_HEAD(&info->modelist);
  688. /*
  689. * First check out what BIOS has to say
  690. */
  691. if (rinfo->mon1_type == MT_LCD)
  692. radeon_get_panel_info_BIOS(rinfo);
  693. /*
  694. * Parse EDID detailed timings and deduce panel infos if any. Right now
  695. * we only deal with first entry returned by parse_EDID, we may do better
  696. * some day...
  697. */
  698. if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type != MT_CRT
  699. && rinfo->mon1_EDID) {
  700. struct fb_var_screeninfo var;
  701. pr_debug("Parsing EDID data for panel info\n");
  702. if (fb_parse_edid(rinfo->mon1_EDID, &var) == 0) {
  703. if (var.xres >= rinfo->panel_info.xres &&
  704. var.yres >= rinfo->panel_info.yres)
  705. radeon_var_to_panel_info(rinfo, &var);
  706. }
  707. }
  708. /*
  709. * Do any additional platform/arch fixups to the panel infos
  710. */
  711. radeon_fixup_panel_info(rinfo);
  712. /*
  713. * If we have some valid panel infos, we setup the default mode based on
  714. * those
  715. */
  716. if (rinfo->mon1_type != MT_CRT && rinfo->panel_info.valid) {
  717. struct fb_var_screeninfo *var = &info->var;
  718. pr_debug("Setting up default mode based on panel info\n");
  719. var->xres = rinfo->panel_info.xres;
  720. var->yres = rinfo->panel_info.yres;
  721. var->xres_virtual = rinfo->panel_info.xres;
  722. var->yres_virtual = rinfo->panel_info.yres;
  723. var->xoffset = var->yoffset = 0;
  724. var->bits_per_pixel = 8;
  725. var->pixclock = 100000000 / rinfo->panel_info.clock;
  726. var->left_margin = (rinfo->panel_info.hblank - rinfo->panel_info.hOver_plus
  727. - rinfo->panel_info.hSync_width);
  728. var->right_margin = rinfo->panel_info.hOver_plus;
  729. var->upper_margin = (rinfo->panel_info.vblank - rinfo->panel_info.vOver_plus
  730. - rinfo->panel_info.vSync_width);
  731. var->lower_margin = rinfo->panel_info.vOver_plus;
  732. var->hsync_len = rinfo->panel_info.hSync_width;
  733. var->vsync_len = rinfo->panel_info.vSync_width;
  734. var->sync = 0;
  735. if (rinfo->panel_info.hAct_high)
  736. var->sync |= FB_SYNC_HOR_HIGH_ACT;
  737. if (rinfo->panel_info.vAct_high)
  738. var->sync |= FB_SYNC_VERT_HIGH_ACT;
  739. var->vmode = 0;
  740. has_default_mode = 1;
  741. }
  742. /*
  743. * Now build modedb from EDID
  744. */
  745. if (rinfo->mon1_EDID) {
  746. fb_edid_to_monspecs(rinfo->mon1_EDID, &info->monspecs);
  747. fb_videomode_to_modelist(info->monspecs.modedb,
  748. info->monspecs.modedb_len,
  749. &info->modelist);
  750. rinfo->mon1_modedb = info->monspecs.modedb;
  751. rinfo->mon1_dbsize = info->monspecs.modedb_len;
  752. }
  753. /*
  754. * Finally, if we don't have panel infos we need to figure some (or
  755. * we try to read it from card), we try to pick a default mode
  756. * and create some panel infos. Whatever...
  757. */
  758. if (rinfo->mon1_type != MT_CRT && !rinfo->panel_info.valid) {
  759. struct fb_videomode *modedb;
  760. int dbsize;
  761. char modename[32];
  762. pr_debug("Guessing panel info...\n");
  763. if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
  764. u32 tmp = INREG(FP_HORZ_STRETCH) & HORZ_PANEL_SIZE;
  765. rinfo->panel_info.xres = ((tmp >> HORZ_PANEL_SHIFT) + 1) * 8;
  766. tmp = INREG(FP_VERT_STRETCH) & VERT_PANEL_SIZE;
  767. rinfo->panel_info.yres = (tmp >> VERT_PANEL_SHIFT) + 1;
  768. }
  769. if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
  770. printk(KERN_WARNING "radeonfb: Can't find panel size, going back to CRT\n");
  771. rinfo->mon1_type = MT_CRT;
  772. goto pickup_default;
  773. }
  774. printk(KERN_WARNING "radeonfb: Assuming panel size %dx%d\n",
  775. rinfo->panel_info.xres, rinfo->panel_info.yres);
  776. modedb = rinfo->mon1_modedb;
  777. dbsize = rinfo->mon1_dbsize;
  778. snprintf(modename, 31, "%dx%d", rinfo->panel_info.xres, rinfo->panel_info.yres);
  779. if (fb_find_mode(&info->var, info, modename,
  780. modedb, dbsize, NULL, 8) == 0) {
  781. printk(KERN_WARNING "radeonfb: Can't find mode for panel size, going back to CRT\n");
  782. rinfo->mon1_type = MT_CRT;
  783. goto pickup_default;
  784. }
  785. has_default_mode = 1;
  786. radeon_var_to_panel_info(rinfo, &info->var);
  787. }
  788. pickup_default:
  789. /*
  790. * Apply passed-in mode option if any
  791. */
  792. if (mode_option) {
  793. if (fb_find_mode(&info->var, info, mode_option,
  794. info->monspecs.modedb,
  795. info->monspecs.modedb_len, NULL, 8) != 0)
  796. has_default_mode = 1;
  797. }
  798. #ifdef CONFIG_PPC_PSERIES
  799. if (!has_default_mode && (
  800. is_powerblade("IBM,8842") || /* JS20 */
  801. is_powerblade("IBM,8844") || /* JS21 */
  802. is_powerblade("IBM,7998") || /* JS12/JS21/JS22 */
  803. is_powerblade("IBM,0792") || /* QS21 */
  804. is_powerblade("IBM,0793") /* QS22 */
  805. )) {
  806. printk("Falling back to 800x600 on JSxx hardware\n");
  807. if (fb_find_mode(&info->var, info, "800x600@60",
  808. info->monspecs.modedb,
  809. info->monspecs.modedb_len, NULL, 8) != 0)
  810. has_default_mode = 1;
  811. }
  812. #endif
  813. /*
  814. * Still no mode, let's pick up a default from the db
  815. */
  816. if (!has_default_mode && info->monspecs.modedb != NULL) {
  817. struct fb_monspecs *specs = &info->monspecs;
  818. struct fb_videomode *modedb = NULL;
  819. /* get preferred timing */
  820. if (specs->misc & FB_MISC_1ST_DETAIL) {
  821. int i;
  822. for (i = 0; i < specs->modedb_len; i++) {
  823. if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
  824. modedb = &specs->modedb[i];
  825. break;
  826. }
  827. }
  828. } else {
  829. /* otherwise, get first mode in database */
  830. modedb = &specs->modedb[0];
  831. }
  832. if (modedb != NULL) {
  833. info->var.bits_per_pixel = 8;
  834. radeon_videomode_to_var(&info->var, modedb);
  835. has_default_mode = 1;
  836. }
  837. }
  838. if (1) {
  839. struct fb_videomode mode;
  840. /* Make sure that whatever mode got selected is actually in the
  841. * modelist or the kernel may die
  842. */
  843. fb_var_to_videomode(&mode, &info->var);
  844. fb_add_videomode(&mode, &info->modelist);
  845. }
  846. }
  847. /*
  848. * The code below is used to pick up a mode in check_var and
  849. * set_var. It should be made generic
  850. */
  851. /*
  852. * This is used when looking for modes. We assign a "distance" value
  853. * to a mode in the modedb depending how "close" it is from what we
  854. * are looking for.
  855. * Currently, we don't compare that much, we could do better but
  856. * the current fbcon doesn't quite mind ;)
  857. */
  858. static int radeon_compare_modes(const struct fb_var_screeninfo *var,
  859. const struct fb_videomode *mode)
  860. {
  861. int distance = 0;
  862. distance = mode->yres - var->yres;
  863. distance += (mode->xres - var->xres)/2;
  864. return distance;
  865. }
  866. /*
  867. * This function is called by check_var, it gets the passed in mode parameter, and
  868. * outputs a valid mode matching the passed-in one as closely as possible.
  869. * We need something better ultimately. Things like fbcon basically pass us out
  870. * current mode with xres/yres hacked, while things like XFree will actually
  871. * produce a full timing that we should respect as much as possible.
  872. *
  873. * This is why I added the FB_ACTIVATE_FIND that is used by fbcon. Without this,
  874. * we do a simple spec match, that's all. With it, we actually look for a mode in
  875. * either our monitor modedb or the vesa one if none
  876. *
  877. */
  878. int radeon_match_mode(struct radeonfb_info *rinfo,
  879. struct fb_var_screeninfo *dest,
  880. const struct fb_var_screeninfo *src)
  881. {
  882. const struct fb_videomode *db = vesa_modes;
  883. int i, dbsize = 34;
  884. int has_rmx, native_db = 0;
  885. int distance = INT_MAX;
  886. const struct fb_videomode *candidate = NULL;
  887. /* Start with a copy of the requested mode */
  888. memcpy(dest, src, sizeof(struct fb_var_screeninfo));
  889. /* Check if we have a modedb built from EDID */
  890. if (rinfo->mon1_modedb) {
  891. db = rinfo->mon1_modedb;
  892. dbsize = rinfo->mon1_dbsize;
  893. native_db = 1;
  894. }
  895. /* Check if we have a scaler allowing any fancy mode */
  896. has_rmx = rinfo->mon1_type == MT_LCD || rinfo->mon1_type == MT_DFP;
  897. /* If we have a scaler and are passed FB_ACTIVATE_TEST or
  898. * FB_ACTIVATE_NOW, just do basic checking and return if the
  899. * mode match
  900. */
  901. if ((src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST ||
  902. (src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
  903. /* We don't have an RMX, validate timings. If we don't have
  904. * monspecs, we should be paranoid and not let use go above
  905. * 640x480-60, but I assume userland knows what it's doing here
  906. * (though I may be proven wrong...)
  907. */
  908. if (has_rmx == 0 && rinfo->mon1_modedb)
  909. if (fb_validate_mode((struct fb_var_screeninfo *)src, rinfo->info))
  910. return -EINVAL;
  911. return 0;
  912. }
  913. /* Now look for a mode in the database */
  914. while (db) {
  915. for (i = 0; i < dbsize; i++) {
  916. int d;
  917. if (db[i].yres < src->yres)
  918. continue;
  919. if (db[i].xres < src->xres)
  920. continue;
  921. d = radeon_compare_modes(src, &db[i]);
  922. /* If the new mode is at least as good as the previous one,
  923. * then it's our new candidate
  924. */
  925. if (d < distance) {
  926. candidate = &db[i];
  927. distance = d;
  928. }
  929. }
  930. db = NULL;
  931. /* If we have a scaler, we allow any mode from the database */
  932. if (native_db && has_rmx) {
  933. db = vesa_modes;
  934. dbsize = 34;
  935. native_db = 0;
  936. }
  937. }
  938. /* If we have found a match, return it */
  939. if (candidate != NULL) {
  940. radeon_videomode_to_var(dest, candidate);
  941. return 0;
  942. }
  943. /* If we haven't and don't have a scaler, fail */
  944. if (!has_rmx)
  945. return -EINVAL;
  946. return 0;
  947. }