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