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