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