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