radeon_monitor.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 mon_types[4] = {MT_NONE, MT_NONE, MT_NONE, MT_NONE};
  433. int i;
  434. for (i = 0; i < 4; i++)
  435. mon_types[i] = radeon_probe_i2c_connector(rinfo,
  436. i+1, &EDIDs[i]);
  437. }
  438. #endif /* DEBUG */
  439. /*
  440. * Old single head cards
  441. */
  442. if (!rinfo->has_CRTC2) {
  443. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  444. if (rinfo->mon1_type == MT_NONE)
  445. rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
  446. &rinfo->mon1_EDID);
  447. #endif /* CONFIG_PPC || CONFIG_SPARC */
  448. #ifdef CONFIG_FB_RADEON_I2C
  449. if (rinfo->mon1_type == MT_NONE)
  450. rinfo->mon1_type =
  451. radeon_probe_i2c_connector(rinfo, ddc_dvi,
  452. &rinfo->mon1_EDID);
  453. if (rinfo->mon1_type == MT_NONE)
  454. rinfo->mon1_type =
  455. radeon_probe_i2c_connector(rinfo, ddc_vga,
  456. &rinfo->mon1_EDID);
  457. if (rinfo->mon1_type == MT_NONE)
  458. rinfo->mon1_type =
  459. radeon_probe_i2c_connector(rinfo, ddc_crt2,
  460. &rinfo->mon1_EDID);
  461. #endif /* CONFIG_FB_RADEON_I2C */
  462. if (rinfo->mon1_type == MT_NONE)
  463. rinfo->mon1_type = MT_CRT;
  464. goto bail;
  465. }
  466. /*
  467. * Check for cards with reversed DACs or TMDS controllers using BIOS
  468. */
  469. if (rinfo->bios_seg &&
  470. (tmp = BIOS_IN16(rinfo->fp_bios_start + 0x50))) {
  471. for (i = 1; i < 4; i++) {
  472. unsigned int tmp0;
  473. if (!BIOS_IN8(tmp + i*2) && i > 1)
  474. break;
  475. tmp0 = BIOS_IN16(tmp + i*2);
  476. if ((!(tmp0 & 0x01)) && (((tmp0 >> 8) & 0x0f) == ddc_dvi)) {
  477. rinfo->reversed_DAC = 1;
  478. printk(KERN_INFO "radeonfb: Reversed DACs detected\n");
  479. }
  480. if ((((tmp0 >> 8) & 0x0f) == ddc_dvi) && ((tmp0 >> 4) & 0x01)) {
  481. rinfo->reversed_TMDS = 1;
  482. printk(KERN_INFO "radeonfb: Reversed TMDS detected\n");
  483. }
  484. }
  485. }
  486. /*
  487. * Probe primary head (DVI or laptop internal panel)
  488. */
  489. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  490. if (rinfo->mon1_type == MT_NONE)
  491. rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
  492. &rinfo->mon1_EDID);
  493. #endif /* CONFIG_PPC || CONFIG_SPARC */
  494. #ifdef CONFIG_FB_RADEON_I2C
  495. if (rinfo->mon1_type == MT_NONE)
  496. rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
  497. &rinfo->mon1_EDID);
  498. if (rinfo->mon1_type == MT_NONE) {
  499. rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
  500. &rinfo->mon1_EDID);
  501. if (rinfo->mon1_type != MT_NONE)
  502. ddc_crt2_used = 1;
  503. }
  504. #endif /* CONFIG_FB_RADEON_I2C */
  505. if (rinfo->mon1_type == MT_NONE && rinfo->is_mobility &&
  506. ((rinfo->bios_seg && (INREG(BIOS_4_SCRATCH) & 4))
  507. || (INREG(LVDS_GEN_CNTL) & LVDS_ON))) {
  508. rinfo->mon1_type = MT_LCD;
  509. printk("Non-DDC laptop panel detected\n");
  510. }
  511. if (rinfo->mon1_type == MT_NONE)
  512. rinfo->mon1_type = radeon_crt_is_connected(rinfo, rinfo->reversed_DAC);
  513. /*
  514. * Probe secondary head (mostly VGA, can be DVI)
  515. */
  516. #if defined(CONFIG_PPC) || defined(CONFIG_SPARC)
  517. if (rinfo->mon2_type == MT_NONE)
  518. rinfo->mon2_type = radeon_probe_OF_head(rinfo, 1,
  519. &rinfo->mon2_EDID);
  520. #endif /* CONFIG_PPC || defined(CONFIG_SPARC) */
  521. #ifdef CONFIG_FB_RADEON_I2C
  522. if (rinfo->mon2_type == MT_NONE)
  523. rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_vga,
  524. &rinfo->mon2_EDID);
  525. if (rinfo->mon2_type == MT_NONE && !ddc_crt2_used)
  526. rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
  527. &rinfo->mon2_EDID);
  528. #endif /* CONFIG_FB_RADEON_I2C */
  529. if (rinfo->mon2_type == MT_NONE)
  530. rinfo->mon2_type = radeon_crt_is_connected(rinfo, !rinfo->reversed_DAC);
  531. /*
  532. * If we only detected port 2, we swap them, if none detected,
  533. * assume CRT (maybe fallback to old BIOS_SCRATCH stuff ? or look
  534. * at FP registers ?)
  535. */
  536. if (rinfo->mon1_type == MT_NONE) {
  537. if (rinfo->mon2_type != MT_NONE) {
  538. rinfo->mon1_type = rinfo->mon2_type;
  539. rinfo->mon1_EDID = rinfo->mon2_EDID;
  540. } else
  541. rinfo->mon1_type = MT_CRT;
  542. rinfo->mon2_type = MT_NONE;
  543. rinfo->mon2_EDID = NULL;
  544. }
  545. /*
  546. * Deal with reversed TMDS
  547. */
  548. if (rinfo->reversed_TMDS) {
  549. /* Always keep internal TMDS as primary head */
  550. if (rinfo->mon1_type == MT_DFP || rinfo->mon2_type == MT_DFP) {
  551. int tmp_type = rinfo->mon1_type;
  552. u8 *tmp_EDID = rinfo->mon1_EDID;
  553. rinfo->mon1_type = rinfo->mon2_type;
  554. rinfo->mon1_EDID = rinfo->mon2_EDID;
  555. rinfo->mon2_type = tmp_type;
  556. rinfo->mon2_EDID = tmp_EDID;
  557. if (rinfo->mon1_type == MT_CRT || rinfo->mon2_type == MT_CRT)
  558. rinfo->reversed_DAC ^= 1;
  559. }
  560. }
  561. }
  562. if (ignore_edid) {
  563. kfree(rinfo->mon1_EDID);
  564. rinfo->mon1_EDID = NULL;
  565. kfree(rinfo->mon2_EDID);
  566. rinfo->mon2_EDID = NULL;
  567. }
  568. bail:
  569. printk(KERN_INFO "radeonfb: Monitor 1 type %s found\n",
  570. radeon_get_mon_name(rinfo->mon1_type));
  571. if (rinfo->mon1_EDID)
  572. printk(KERN_INFO "radeonfb: EDID probed\n");
  573. if (!rinfo->has_CRTC2)
  574. return;
  575. printk(KERN_INFO "radeonfb: Monitor 2 type %s found\n",
  576. radeon_get_mon_name(rinfo->mon2_type));
  577. if (rinfo->mon2_EDID)
  578. printk(KERN_INFO "radeonfb: EDID probed\n");
  579. }
  580. /*
  581. * This function applies any arch/model/machine specific fixups
  582. * to the panel info. It may eventually alter EDID block as
  583. * well or whatever is specific to a given model and not probed
  584. * properly by the default code
  585. */
  586. static void radeon_fixup_panel_info(struct radeonfb_info *rinfo)
  587. {
  588. #ifdef CONFIG_PPC
  589. /*
  590. * LCD Flat panels should use fixed dividers, we enfore that on
  591. * PPC only for now...
  592. */
  593. if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type == MT_LCD
  594. && rinfo->is_mobility) {
  595. int ppll_div_sel;
  596. u32 ppll_divn;
  597. ppll_div_sel = INREG8(CLOCK_CNTL_INDEX + 1) & 0x3;
  598. radeon_pll_errata_after_index(rinfo);
  599. ppll_divn = INPLL(PPLL_DIV_0 + ppll_div_sel);
  600. rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
  601. rinfo->panel_info.fbk_divider = ppll_divn & 0x7ff;
  602. rinfo->panel_info.post_divider = (ppll_divn >> 16) & 0x7;
  603. rinfo->panel_info.use_bios_dividers = 1;
  604. printk(KERN_DEBUG "radeonfb: Using Firmware dividers 0x%08x "
  605. "from PPLL %d\n",
  606. rinfo->panel_info.fbk_divider |
  607. (rinfo->panel_info.post_divider << 16),
  608. ppll_div_sel);
  609. }
  610. #endif /* CONFIG_PPC */
  611. }
  612. /*
  613. * Fill up panel infos from a mode definition, either returned by the EDID
  614. * or from the default mode when we can't do any better
  615. */
  616. static void radeon_var_to_panel_info(struct radeonfb_info *rinfo, struct fb_var_screeninfo *var)
  617. {
  618. rinfo->panel_info.xres = var->xres;
  619. rinfo->panel_info.yres = var->yres;
  620. rinfo->panel_info.clock = 100000000 / var->pixclock;
  621. rinfo->panel_info.hOver_plus = var->right_margin;
  622. rinfo->panel_info.hSync_width = var->hsync_len;
  623. rinfo->panel_info.hblank = var->left_margin +
  624. (var->right_margin + var->hsync_len);
  625. rinfo->panel_info.vOver_plus = var->lower_margin;
  626. rinfo->panel_info.vSync_width = var->vsync_len;
  627. rinfo->panel_info.vblank = var->upper_margin +
  628. (var->lower_margin + var->vsync_len);
  629. rinfo->panel_info.hAct_high =
  630. (var->sync & FB_SYNC_HOR_HIGH_ACT) != 0;
  631. rinfo->panel_info.vAct_high =
  632. (var->sync & FB_SYNC_VERT_HIGH_ACT) != 0;
  633. rinfo->panel_info.valid = 1;
  634. /* We use a default of 200ms for the panel power delay,
  635. * I need to have a real schedule() instead of mdelay's in the panel code.
  636. * we might be possible to figure out a better power delay either from
  637. * MacOS OF tree or from the EDID block (proprietary extensions ?)
  638. */
  639. rinfo->panel_info.pwr_delay = 200;
  640. }
  641. static void radeon_videomode_to_var(struct fb_var_screeninfo *var,
  642. const struct fb_videomode *mode)
  643. {
  644. var->xres = mode->xres;
  645. var->yres = mode->yres;
  646. var->xres_virtual = mode->xres;
  647. var->yres_virtual = mode->yres;
  648. var->xoffset = 0;
  649. var->yoffset = 0;
  650. var->pixclock = mode->pixclock;
  651. var->left_margin = mode->left_margin;
  652. var->right_margin = mode->right_margin;
  653. var->upper_margin = mode->upper_margin;
  654. var->lower_margin = mode->lower_margin;
  655. var->hsync_len = mode->hsync_len;
  656. var->vsync_len = mode->vsync_len;
  657. var->sync = mode->sync;
  658. var->vmode = mode->vmode;
  659. }
  660. #ifdef CONFIG_PPC_PSERIES
  661. static int is_powerblade(const char *model)
  662. {
  663. struct device_node *root;
  664. const char* cp;
  665. int len, l, rc = 0;
  666. root = of_find_node_by_path("/");
  667. if (root && model) {
  668. l = strlen(model);
  669. cp = of_get_property(root, "model", &len);
  670. if (cp)
  671. rc = memcmp(model, cp, min(len, l)) == 0;
  672. of_node_put(root);
  673. }
  674. return rc;
  675. }
  676. #endif
  677. /*
  678. * Build the modedb for head 1 (head 2 will come later), check panel infos
  679. * from either BIOS or EDID, and pick up the default mode
  680. */
  681. void radeon_check_modes(struct radeonfb_info *rinfo, const char *mode_option)
  682. {
  683. struct fb_info * info = rinfo->info;
  684. int has_default_mode = 0;
  685. /*
  686. * Fill default var first
  687. */
  688. info->var = radeonfb_default_var;
  689. INIT_LIST_HEAD(&info->modelist);
  690. /*
  691. * First check out what BIOS has to say
  692. */
  693. if (rinfo->mon1_type == MT_LCD)
  694. radeon_get_panel_info_BIOS(rinfo);
  695. /*
  696. * Parse EDID detailed timings and deduce panel infos if any. Right now
  697. * we only deal with first entry returned by parse_EDID, we may do better
  698. * some day...
  699. */
  700. if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type != MT_CRT
  701. && rinfo->mon1_EDID) {
  702. struct fb_var_screeninfo var;
  703. pr_debug("Parsing EDID data for panel info\n");
  704. if (fb_parse_edid(rinfo->mon1_EDID, &var) == 0) {
  705. if (var.xres >= rinfo->panel_info.xres &&
  706. var.yres >= rinfo->panel_info.yres)
  707. radeon_var_to_panel_info(rinfo, &var);
  708. }
  709. }
  710. /*
  711. * Do any additional platform/arch fixups to the panel infos
  712. */
  713. radeon_fixup_panel_info(rinfo);
  714. /*
  715. * If we have some valid panel infos, we setup the default mode based on
  716. * those
  717. */
  718. if (rinfo->mon1_type != MT_CRT && rinfo->panel_info.valid) {
  719. struct fb_var_screeninfo *var = &info->var;
  720. pr_debug("Setting up default mode based on panel info\n");
  721. var->xres = rinfo->panel_info.xres;
  722. var->yres = rinfo->panel_info.yres;
  723. var->xres_virtual = rinfo->panel_info.xres;
  724. var->yres_virtual = rinfo->panel_info.yres;
  725. var->xoffset = var->yoffset = 0;
  726. var->bits_per_pixel = 8;
  727. var->pixclock = 100000000 / rinfo->panel_info.clock;
  728. var->left_margin = (rinfo->panel_info.hblank - rinfo->panel_info.hOver_plus
  729. - rinfo->panel_info.hSync_width);
  730. var->right_margin = rinfo->panel_info.hOver_plus;
  731. var->upper_margin = (rinfo->panel_info.vblank - rinfo->panel_info.vOver_plus
  732. - rinfo->panel_info.vSync_width);
  733. var->lower_margin = rinfo->panel_info.vOver_plus;
  734. var->hsync_len = rinfo->panel_info.hSync_width;
  735. var->vsync_len = rinfo->panel_info.vSync_width;
  736. var->sync = 0;
  737. if (rinfo->panel_info.hAct_high)
  738. var->sync |= FB_SYNC_HOR_HIGH_ACT;
  739. if (rinfo->panel_info.vAct_high)
  740. var->sync |= FB_SYNC_VERT_HIGH_ACT;
  741. var->vmode = 0;
  742. has_default_mode = 1;
  743. }
  744. /*
  745. * Now build modedb from EDID
  746. */
  747. if (rinfo->mon1_EDID) {
  748. fb_edid_to_monspecs(rinfo->mon1_EDID, &info->monspecs);
  749. fb_videomode_to_modelist(info->monspecs.modedb,
  750. info->monspecs.modedb_len,
  751. &info->modelist);
  752. rinfo->mon1_modedb = info->monspecs.modedb;
  753. rinfo->mon1_dbsize = info->monspecs.modedb_len;
  754. }
  755. /*
  756. * Finally, if we don't have panel infos we need to figure some (or
  757. * we try to read it from card), we try to pick a default mode
  758. * and create some panel infos. Whatever...
  759. */
  760. if (rinfo->mon1_type != MT_CRT && !rinfo->panel_info.valid) {
  761. struct fb_videomode *modedb;
  762. int dbsize;
  763. char modename[32];
  764. pr_debug("Guessing panel info...\n");
  765. if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
  766. u32 tmp = INREG(FP_HORZ_STRETCH) & HORZ_PANEL_SIZE;
  767. rinfo->panel_info.xres = ((tmp >> HORZ_PANEL_SHIFT) + 1) * 8;
  768. tmp = INREG(FP_VERT_STRETCH) & VERT_PANEL_SIZE;
  769. rinfo->panel_info.yres = (tmp >> VERT_PANEL_SHIFT) + 1;
  770. }
  771. if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
  772. printk(KERN_WARNING "radeonfb: Can't find panel size, going back to CRT\n");
  773. rinfo->mon1_type = MT_CRT;
  774. goto pickup_default;
  775. }
  776. printk(KERN_WARNING "radeonfb: Assuming panel size %dx%d\n",
  777. rinfo->panel_info.xres, rinfo->panel_info.yres);
  778. modedb = rinfo->mon1_modedb;
  779. dbsize = rinfo->mon1_dbsize;
  780. snprintf(modename, 31, "%dx%d", rinfo->panel_info.xres, rinfo->panel_info.yres);
  781. if (fb_find_mode(&info->var, info, modename,
  782. modedb, dbsize, NULL, 8) == 0) {
  783. printk(KERN_WARNING "radeonfb: Can't find mode for panel size, going back to CRT\n");
  784. rinfo->mon1_type = MT_CRT;
  785. goto pickup_default;
  786. }
  787. has_default_mode = 1;
  788. radeon_var_to_panel_info(rinfo, &info->var);
  789. }
  790. pickup_default:
  791. /*
  792. * Apply passed-in mode option if any
  793. */
  794. if (mode_option) {
  795. if (fb_find_mode(&info->var, info, mode_option,
  796. info->monspecs.modedb,
  797. info->monspecs.modedb_len, NULL, 8) != 0)
  798. has_default_mode = 1;
  799. }
  800. #ifdef CONFIG_PPC_PSERIES
  801. if (!has_default_mode && (
  802. is_powerblade("IBM,8842") || /* JS20 */
  803. is_powerblade("IBM,8844") || /* JS21 */
  804. is_powerblade("IBM,7998") || /* JS12/JS21/JS22 */
  805. is_powerblade("IBM,0792") || /* QS21 */
  806. is_powerblade("IBM,0793") /* QS22 */
  807. )) {
  808. printk("Falling back to 800x600 on JSxx hardware\n");
  809. if (fb_find_mode(&info->var, info, "800x600@60",
  810. info->monspecs.modedb,
  811. info->monspecs.modedb_len, NULL, 8) != 0)
  812. has_default_mode = 1;
  813. }
  814. #endif
  815. /*
  816. * Still no mode, let's pick up a default from the db
  817. */
  818. if (!has_default_mode && info->monspecs.modedb != NULL) {
  819. struct fb_monspecs *specs = &info->monspecs;
  820. struct fb_videomode *modedb = NULL;
  821. /* get preferred timing */
  822. if (specs->misc & FB_MISC_1ST_DETAIL) {
  823. int i;
  824. for (i = 0; i < specs->modedb_len; i++) {
  825. if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
  826. modedb = &specs->modedb[i];
  827. break;
  828. }
  829. }
  830. } else {
  831. /* otherwise, get first mode in database */
  832. modedb = &specs->modedb[0];
  833. }
  834. if (modedb != NULL) {
  835. info->var.bits_per_pixel = 8;
  836. radeon_videomode_to_var(&info->var, modedb);
  837. has_default_mode = 1;
  838. }
  839. }
  840. if (1) {
  841. struct fb_videomode mode;
  842. /* Make sure that whatever mode got selected is actually in the
  843. * modelist or the kernel may die
  844. */
  845. fb_var_to_videomode(&mode, &info->var);
  846. fb_add_videomode(&mode, &info->modelist);
  847. }
  848. }
  849. /*
  850. * The code below is used to pick up a mode in check_var and
  851. * set_var. It should be made generic
  852. */
  853. /*
  854. * This is used when looking for modes. We assign a "distance" value
  855. * to a mode in the modedb depending how "close" it is from what we
  856. * are looking for.
  857. * Currently, we don't compare that much, we could do better but
  858. * the current fbcon doesn't quite mind ;)
  859. */
  860. static int radeon_compare_modes(const struct fb_var_screeninfo *var,
  861. const struct fb_videomode *mode)
  862. {
  863. int distance = 0;
  864. distance = mode->yres - var->yres;
  865. distance += (mode->xres - var->xres)/2;
  866. return distance;
  867. }
  868. /*
  869. * This function is called by check_var, it gets the passed in mode parameter, and
  870. * outputs a valid mode matching the passed-in one as closely as possible.
  871. * We need something better ultimately. Things like fbcon basically pass us out
  872. * current mode with xres/yres hacked, while things like XFree will actually
  873. * produce a full timing that we should respect as much as possible.
  874. *
  875. * This is why I added the FB_ACTIVATE_FIND that is used by fbcon. Without this,
  876. * we do a simple spec match, that's all. With it, we actually look for a mode in
  877. * either our monitor modedb or the vesa one if none
  878. *
  879. */
  880. int radeon_match_mode(struct radeonfb_info *rinfo,
  881. struct fb_var_screeninfo *dest,
  882. const struct fb_var_screeninfo *src)
  883. {
  884. const struct fb_videomode *db = vesa_modes;
  885. int i, dbsize = 34;
  886. int has_rmx, native_db = 0;
  887. int distance = INT_MAX;
  888. const struct fb_videomode *candidate = NULL;
  889. /* Start with a copy of the requested mode */
  890. memcpy(dest, src, sizeof(struct fb_var_screeninfo));
  891. /* Check if we have a modedb built from EDID */
  892. if (rinfo->mon1_modedb) {
  893. db = rinfo->mon1_modedb;
  894. dbsize = rinfo->mon1_dbsize;
  895. native_db = 1;
  896. }
  897. /* Check if we have a scaler allowing any fancy mode */
  898. has_rmx = rinfo->mon1_type == MT_LCD || rinfo->mon1_type == MT_DFP;
  899. /* If we have a scaler and are passed FB_ACTIVATE_TEST or
  900. * FB_ACTIVATE_NOW, just do basic checking and return if the
  901. * mode match
  902. */
  903. if ((src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST ||
  904. (src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
  905. /* We don't have an RMX, validate timings. If we don't have
  906. * monspecs, we should be paranoid and not let use go above
  907. * 640x480-60, but I assume userland knows what it's doing here
  908. * (though I may be proven wrong...)
  909. */
  910. if (has_rmx == 0 && rinfo->mon1_modedb)
  911. if (fb_validate_mode((struct fb_var_screeninfo *)src, rinfo->info))
  912. return -EINVAL;
  913. return 0;
  914. }
  915. /* Now look for a mode in the database */
  916. while (db) {
  917. for (i = 0; i < dbsize; i++) {
  918. int d;
  919. if (db[i].yres < src->yres)
  920. continue;
  921. if (db[i].xres < src->xres)
  922. continue;
  923. d = radeon_compare_modes(src, &db[i]);
  924. /* If the new mode is at least as good as the previous one,
  925. * then it's our new candidate
  926. */
  927. if (d < distance) {
  928. candidate = &db[i];
  929. distance = d;
  930. }
  931. }
  932. db = NULL;
  933. /* If we have a scaler, we allow any mode from the database */
  934. if (native_db && has_rmx) {
  935. db = vesa_modes;
  936. dbsize = 34;
  937. native_db = 0;
  938. }
  939. }
  940. /* If we have found a match, return it */
  941. if (candidate != NULL) {
  942. radeon_videomode_to_var(dest, candidate);
  943. return 0;
  944. }
  945. /* If we haven't and don't have a scaler, fail */
  946. if (!has_rmx)
  947. return -EINVAL;
  948. return 0;
  949. }