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