upgrade to linux 2.6.10-1.12_FC2
[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         .xres           = 640,
11         .yres           = 480,
12         .xres_virtual   = 640,
13         .yres_virtual   = 480,
14         .bits_per_pixel = 8,
15         .red            = { .length = 8 },
16         .green          = { .length = 8 },
17         .blue           = { .length = 8 },
18         .activate       = FB_ACTIVATE_NOW,
19         .height         = -1,
20         .width          = -1,
21         .pixclock       = 39721,
22         .left_margin    = 40,
23         .right_margin   = 24,
24         .upper_margin   = 32,
25         .lower_margin   = 11,
26         .hsync_len      = 96,
27         .vsync_len      = 2,
28         .vmode          = FB_VMODE_NONINTERLACED
29 };
30
31 static char *radeon_get_mon_name(int type)
32 {
33         char *pret = NULL;
34
35         switch (type) {
36                 case MT_NONE:
37                         pret = "no";
38                         break;
39                 case MT_CRT:
40                         pret = "CRT";
41                         break;
42                 case MT_DFP:
43                         pret = "DFP";
44                         break;
45                 case MT_LCD:
46                         pret = "LCD";
47                         break;
48                 case MT_CTV:
49                         pret = "CTV";
50                         break;
51                 case MT_STV:
52                         pret = "STV";
53                         break;
54         }
55
56         return pret;
57 }
58
59
60 #ifdef CONFIG_PPC_OF
61 /*
62  * Try to find monitor informations & EDID data out of the Open Firmware
63  * device-tree. This also contains some "hacks" to work around a few machine
64  * models with broken OF probing by hard-coding known EDIDs for some Mac
65  * laptops internal LVDS panel. (XXX: not done yet)
66  */
67 static int __devinit radeon_parse_montype_prop(struct device_node *dp, u8 **out_EDID, int hdno)
68 {
69         static char *propnames[] = { "DFP,EDID", "LCD,EDID", "EDID", "EDID1", "EDID2",  NULL };
70         u8 *pedid = NULL;
71         u8 *pmt = NULL;
72         u8 *tmp;
73         int i, mt = MT_NONE;  
74         
75         RTRACE("analyzing OF properties...\n");
76         pmt = (u8 *)get_property(dp, "display-type", NULL);
77         if (!pmt)
78                 return MT_NONE;
79         RTRACE("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 = (u8 *)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 = get_property(dp->parent, (hdno == 0) ? "EDID1" : "EDID2", NULL);
105         if (pedid == NULL && dp->parent && (hdno == 0))
106                 pedid = get_property(dp->parent, "EDID", NULL);
107         if (pedid == NULL)
108                 return mt;
109
110         tmp = (u8 *)kmalloc(EDID_LENGTH, GFP_KERNEL);
111         if (!tmp)
112                 return mt;
113         memcpy(tmp, pedid, EDID_LENGTH);
114         *out_EDID = tmp;
115         return mt;
116 }
117
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         RTRACE("radeon_probe_OF_head\n");
124
125         dp = pci_device_to_OF_node(rinfo->pdev);
126         while (dp == NULL)
127                 return MT_NONE;
128
129         if (rinfo->has_CRTC2) {
130                 char *pname;
131                 int len, second = 0;
132
133                 dp = dp->child;
134                 do {
135                         if (!dp)
136                                 return MT_NONE;
137                         pname = (char *)get_property(dp, "name", NULL);
138                         if (!pname)
139                                 return MT_NONE;
140                         len = strlen(pname);
141                         RTRACE("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 */
164
165
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         RTRACE("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                 RTRACE("ref_divider = %x\n", rinfo->panel_info.ref_divider);
206                 RTRACE("post_divider = %x\n", rinfo->panel_info.post_divider);
207                 RTRACE("fbk_divider = %x\n", rinfo->panel_info.fbk_divider);
208         }
209         RTRACE("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                 RTRACE(" %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                         RTRACE("Found panel in BIOS table:\n");
234                         RTRACE("  hblank: %d\n", rinfo->panel_info.hblank);
235                         RTRACE("  hOver_plus: %d\n", rinfo->panel_info.hOver_plus);
236                         RTRACE("  hSync_width: %d\n", rinfo->panel_info.hSync_width);
237                         RTRACE("  vblank: %d\n", rinfo->panel_info.vblank);
238                         RTRACE("  vOver_plus: %d\n", rinfo->panel_info.vOver_plus);
239                         RTRACE("  vSync_width: %d\n", rinfo->panel_info.vSync_width);
240                         RTRACE("  clock: %d\n", rinfo->panel_info.clock);
241                                 
242                         return 1;
243                 }
244         }
245         RTRACE("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  */
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         RTRACE("%d chips in connector info\n", chips);
278         for (i = 0; i < chips; i++) {
279                 tmp = BIOS_IN8(offset++);
280                 connectors = tmp & 0x0f;
281                 RTRACE(" - 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                         RTRACE("  * 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  */
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  */
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         } while (*s++);
403         if (second)
404                 s2[i] = 0;
405         else {
406                 s1[i] = 0;
407                 s2[0] = 0;
408         }
409         if (strcmp(s1, "CRT") == 0)
410                 rinfo->mon1_type = MT_CRT;
411         else if (strcmp(s1, "TMDS") == 0)
412                 rinfo->mon1_type = MT_DFP;
413         else if (strcmp(s1, "LVDS") == 0)
414                 rinfo->mon1_type = MT_LCD;
415
416         if (strcmp(s2, "CRT") == 0)
417                 rinfo->mon2_type = MT_CRT;
418         else if (strcmp(s2, "TMDS") == 0)
419                 rinfo->mon2_type = MT_DFP;
420         else if (strcmp(s2, "LVDS") == 0)
421                 rinfo->mon2_type = MT_LCD;
422
423         return 1;
424 }
425
426 /*
427  * Probe display on both primary and secondary card's connector (if any)
428  * by various available techniques (i2c, OF device tree, BIOS, ...) and
429  * try to retreive EDID. The algorithm here comes from XFree's radeon
430  * driver
431  */
432 void __devinit radeon_probe_screens(struct radeonfb_info *rinfo,
433                                     const char *monitor_layout, int ignore_edid)
434 {
435 #ifdef CONFIG_FB_RADEON_I2C
436         int ddc_crt2_used = 0;  
437 #endif
438         int tmp, i;
439
440         radeon_parse_connector_info(rinfo);
441
442         if (radeon_parse_monitor_layout(rinfo, monitor_layout)) {
443
444                 /*
445                  * If user specified a monitor_layout option, use it instead
446                  * of auto-detecting. Maybe we should only use this argument
447                  * on the first radeon card probed or provide a way to specify
448                  * a layout for each card ?
449                  */
450
451                 RTRACE("Using specified monitor layout: %s", monitor_layout);
452 #ifdef CONFIG_FB_RADEON_I2C
453                 if (!ignore_edid) {
454                         if (rinfo->mon1_type != MT_NONE)
455                                 if (!radeon_probe_i2c_connector(rinfo, ddc_dvi, &rinfo->mon1_EDID)) {
456                                         radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon1_EDID);
457                                         ddc_crt2_used = 1;
458                                 }
459                         if (rinfo->mon2_type != MT_NONE)
460                                 if (!radeon_probe_i2c_connector(rinfo, ddc_vga, &rinfo->mon2_EDID) &&
461                                     !ddc_crt2_used)
462                                         radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon2_EDID);
463                 }
464 #endif /* CONFIG_FB_RADEON_I2C */
465                 if (rinfo->mon1_type == MT_NONE) {
466                         if (rinfo->mon2_type != MT_NONE) {
467                                 rinfo->mon1_type = rinfo->mon2_type;
468                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
469                         } else {
470                                 rinfo->mon1_type = MT_CRT;
471                                 printk(KERN_INFO "radeonfb: No valid monitor, assuming CRT on first port\n");
472                         }
473                         rinfo->mon2_type = MT_NONE;
474                         rinfo->mon2_EDID = NULL;
475                 }
476         } else {
477                 /*
478                  * Auto-detecting display type (well... trying to ...)
479                  */
480                 
481                 RTRACE("Starting monitor auto detection...\n");
482
483 #if DEBUG && defined(CONFIG_FB_RADEON_I2C)
484                 {
485                         u8 *EDIDs[4] = { NULL, NULL, NULL, NULL };
486                         int mon_types[4] = {MT_NONE, MT_NONE, MT_NONE, MT_NONE};
487                         int i;
488
489                         for (i = 0; i < 4; i++)
490                                 mon_types[i] = radeon_probe_i2c_connector(rinfo,
491                                                                           i+1, &EDIDs[i]);
492                 }
493 #endif /* DEBUG */
494                 /*
495                  * Old single head cards
496                  */
497                 if (!rinfo->has_CRTC2) {
498 #ifdef CONFIG_PPC_OF
499                         if (rinfo->mon1_type == MT_NONE)
500                                 rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
501                                                                         &rinfo->mon1_EDID);
502 #endif /* CONFIG_PPC_OF */
503 #ifdef CONFIG_FB_RADEON_I2C
504                         if (rinfo->mon1_type == MT_NONE)
505                                 rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
506                                                                       &rinfo->mon1_EDID);
507                         if (rinfo->mon1_type == MT_NONE)
508                                 rinfo->mon1_type =
509                                         radeon_probe_i2c_connector(rinfo, ddc_vga,
510                                                                    &rinfo->mon1_EDID);
511                         if (rinfo->mon1_type == MT_NONE)
512                                 rinfo->mon1_type =
513                                         radeon_probe_i2c_connector(rinfo, ddc_crt2,
514                                                                    &rinfo->mon1_EDID);  
515 #endif /* CONFIG_FB_RADEON_I2C */
516                         if (rinfo->mon1_type == MT_NONE)
517                                 rinfo->mon1_type = MT_CRT;
518                         goto bail;
519                 }
520
521                 /*
522                  * Check for cards with reversed DACs or TMDS controllers using BIOS
523                  */
524                 if (rinfo->bios_seg &&
525                     (tmp = BIOS_IN16(rinfo->fp_bios_start + 0x50))) {
526                         for (i = 1; i < 4; i++) {
527                                 unsigned int tmp0;
528
529                                 if (!BIOS_IN8(tmp + i*2) && i > 1)
530                                         break;
531                                 tmp0 = BIOS_IN16(tmp + i*2);
532                                 if ((!(tmp0 & 0x01)) && (((tmp0 >> 8) & 0x0f) == ddc_dvi)) {
533                                         rinfo->reversed_DAC = 1;
534                                         printk(KERN_INFO "radeonfb: Reversed DACs detected\n");
535                                 }
536                                 if ((((tmp0 >> 8) & 0x0f) == ddc_dvi) && ((tmp0 >> 4) & 0x01)) {
537                                         rinfo->reversed_TMDS = 1;
538                                         printk(KERN_INFO "radeonfb: Reversed TMDS detected\n");
539                                 }
540                         }
541                 }
542
543                 /*
544                  * Probe primary head (DVI or laptop internal panel)
545                  */
546 #ifdef CONFIG_PPC_OF
547                 if (rinfo->mon1_type == MT_NONE)
548                         rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0, &rinfo->mon1_EDID);
549 #endif /* CONFIG_PPC_OF */
550 #ifdef CONFIG_FB_RADEON_I2C
551                 if (rinfo->mon1_type == MT_NONE)
552                         rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
553                                                                       &rinfo->mon1_EDID);
554                 if (rinfo->mon1_type == MT_NONE) {
555                         rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
556                                                                       &rinfo->mon1_EDID);
557                         if (rinfo->mon1_type != MT_NONE)
558                                 ddc_crt2_used = 1;
559                 }
560 #endif /* CONFIG_FB_RADEON_I2C */
561                 if (rinfo->mon1_type == MT_NONE && rinfo->is_mobility &&
562                     ((rinfo->bios_seg && (INREG(BIOS_4_SCRATCH) & 4))
563                      || (INREG(LVDS_GEN_CNTL) & LVDS_ON))) {
564                         rinfo->mon1_type = MT_LCD;
565                         printk("Non-DDC laptop panel detected\n");
566                 }
567                 if (rinfo->mon1_type == MT_NONE)
568                         rinfo->mon1_type = radeon_crt_is_connected(rinfo, rinfo->reversed_DAC);
569
570                 /*
571                  * Probe secondary head (mostly VGA, can be DVI)
572                  */
573 #ifdef CONFIG_PPC_OF
574                 if (rinfo->mon2_type == MT_NONE)
575                         rinfo->mon2_type = radeon_probe_OF_head(rinfo, 1, &rinfo->mon2_EDID);
576 #endif /* CONFIG_PPC_OF */
577 #ifdef CONFIG_FB_RADEON_I2C
578                 if (rinfo->mon2_type == MT_NONE)
579                         rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_vga,
580                                                                       &rinfo->mon2_EDID);
581                 if (rinfo->mon2_type == MT_NONE && !ddc_crt2_used)
582                         rinfo->mon2_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
583                                                                       &rinfo->mon2_EDID);
584 #endif /* CONFIG_FB_RADEON_I2C */
585                 if (rinfo->mon2_type == MT_NONE)
586                         rinfo->mon2_type = radeon_crt_is_connected(rinfo, !rinfo->reversed_DAC);
587
588                 /*
589                  * If we only detected port 2, we swap them, if none detected,
590                  * assume CRT (maybe fallback to old BIOS_SCRATCH stuff ? or look
591                  * at FP registers ?)
592                  */
593                 if (rinfo->mon1_type == MT_NONE) {
594                         if (rinfo->mon2_type != MT_NONE) {
595                                 rinfo->mon1_type = rinfo->mon2_type;
596                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
597                         } else
598                                 rinfo->mon1_type = MT_CRT;
599                         rinfo->mon2_type = MT_NONE;
600                         rinfo->mon2_EDID = NULL;
601                 }
602
603                 /*
604                  * Deal with reversed TMDS
605                  */
606                 if (rinfo->reversed_TMDS) {
607                         /* Always keep internal TMDS as primary head */
608                         if (rinfo->mon1_type == MT_DFP || rinfo->mon2_type == MT_DFP) {
609                                 int tmp_type = rinfo->mon1_type;
610                                 u8 *tmp_EDID = rinfo->mon1_EDID;
611                                 rinfo->mon1_type = rinfo->mon2_type;
612                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
613                                 rinfo->mon2_type = tmp_type;
614                                 rinfo->mon2_EDID = tmp_EDID;
615                                 if (rinfo->mon1_type == MT_CRT || rinfo->mon2_type == MT_CRT)
616                                         rinfo->reversed_DAC ^= 1;
617                         }
618                 }
619         }
620         if (ignore_edid) {
621                 if (rinfo->mon1_EDID)
622                         kfree(rinfo->mon1_EDID);
623                 rinfo->mon1_EDID = NULL;
624                 if (rinfo->mon2_EDID)
625                         kfree(rinfo->mon2_EDID);
626                 rinfo->mon2_EDID = NULL;
627         }
628
629  bail:
630         printk(KERN_INFO "radeonfb: Monitor 1 type %s found\n",
631                radeon_get_mon_name(rinfo->mon1_type));
632         if (rinfo->mon1_EDID)
633                 printk(KERN_INFO "radeonfb: EDID probed\n");
634         if (!rinfo->has_CRTC2)
635                 return;
636         printk(KERN_INFO "radeonfb: Monitor 2 type %s found\n",
637                radeon_get_mon_name(rinfo->mon2_type));
638         if (rinfo->mon2_EDID)
639                 printk(KERN_INFO "radeonfb: EDID probed\n");
640 }
641
642
643 /*
644  * This functions applyes any arch/model/machine specific fixups
645  * to the panel info. It may eventually alter EDID block as
646  * well or whatever is specific to a given model and not probed
647  * properly by the default code
648  */
649 static void radeon_fixup_panel_info(struct radeonfb_info *rinfo)
650 {
651  #ifdef CONFIG_PPC_OF
652         /*
653          * LCD Flat panels should use fixed dividers, we enfore that on
654          * PowerMac only for now...
655          */
656         if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type == MT_LCD
657             && rinfo->is_mobility) {
658                 int ppll_div_sel = INREG8(CLOCK_CNTL_INDEX + 1) & 0x3;
659                 u32 ppll_divn = INPLL(PPLL_DIV_0 + ppll_div_sel);
660                 rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
661                 rinfo->panel_info.fbk_divider = ppll_divn & 0x7ff;
662                 rinfo->panel_info.post_divider = (ppll_divn >> 16) & 0x7;
663                 rinfo->panel_info.use_bios_dividers = 1;
664
665                 printk(KERN_DEBUG "radeonfb: Using Firmware dividers 0x%08x "
666                        "from PPLL %d\n",
667                        rinfo->panel_info.fbk_divider |
668                        (rinfo->panel_info.post_divider << 16),
669                        ppll_div_sel);
670         }
671 #endif /* CONFIG_PPC_OF */
672 }
673
674
675 /*
676  * Fill up panel infos from a mode definition, either returned by the EDID
677  * or from the default mode when we can't do any better
678  */
679 static void radeon_var_to_panel_info(struct radeonfb_info *rinfo, struct fb_var_screeninfo *var)
680 {
681         rinfo->panel_info.xres = var->xres;
682         rinfo->panel_info.yres = var->yres;
683         rinfo->panel_info.clock = 100000000 / var->pixclock;
684         rinfo->panel_info.hOver_plus = var->right_margin;
685         rinfo->panel_info.hSync_width = var->hsync_len;
686         rinfo->panel_info.hblank = var->left_margin +
687                 (var->right_margin + var->hsync_len);
688         rinfo->panel_info.vOver_plus = var->lower_margin;
689         rinfo->panel_info.vSync_width = var->vsync_len;
690         rinfo->panel_info.vblank = var->upper_margin +
691                 (var->lower_margin + var->vsync_len);
692         rinfo->panel_info.hAct_high =
693                 (var->sync & FB_SYNC_HOR_HIGH_ACT) != 0;
694         rinfo->panel_info.vAct_high =
695                 (var->sync & FB_SYNC_VERT_HIGH_ACT) != 0;
696         rinfo->panel_info.valid = 1;
697         /* We use a default of 200ms for the panel power delay, 
698          * I need to have a real schedule() instead of mdelay's in the panel code.
699          * we might be possible to figure out a better power delay either from
700          * MacOS OF tree or from the EDID block (proprietary extensions ?)
701          */
702         rinfo->panel_info.pwr_delay = 200;
703 }
704
705 static void radeon_var_to_videomode(struct fb_videomode *mode,
706                                     const struct fb_var_screeninfo *var)
707 {
708     mode->xres = var->xres;
709     mode->yres = var->yres;
710     mode->pixclock = var->pixclock;
711     mode->left_margin = var->left_margin;
712     mode->right_margin = var->right_margin;
713     mode->upper_margin = var->upper_margin;
714     mode->lower_margin = var->lower_margin;
715     mode->hsync_len = var->hsync_len;
716     mode->vsync_len = var->vsync_len;
717     mode->sync = var->sync;
718     mode->vmode = var->vmode;
719 }
720
721 static void radeon_videomode_to_var(struct fb_var_screeninfo *var,
722                                     const struct fb_videomode *mode)
723 {
724     var->xres = mode->xres;
725     var->yres = mode->yres;
726     var->xres_virtual = mode->xres;
727     var->yres_virtual = mode->yres;
728     var->xoffset = 0;
729     var->yoffset = 0;
730     var->pixclock = mode->pixclock;
731     var->left_margin = mode->left_margin;
732     var->right_margin = mode->right_margin;
733     var->upper_margin = mode->upper_margin;
734     var->lower_margin = mode->lower_margin;
735     var->hsync_len = mode->hsync_len;
736     var->vsync_len = mode->vsync_len;
737     var->sync = mode->sync;
738     var->vmode = mode->vmode;
739 }
740
741 /*
742  * Build the modedb for head 1 (head 2 will come later), check panel infos
743  * from either BIOS or EDID, and pick up the default mode
744  */
745 void __devinit radeon_check_modes(struct radeonfb_info *rinfo, const char *mode_option)
746 {
747         int has_default_mode = 0;
748
749         /*
750          * Fill default var first
751          */
752         rinfo->info->var = radeonfb_default_var;
753
754         /*
755          * First check out what BIOS has to say
756          */
757         if (rinfo->mon1_type == MT_LCD)
758                 radeon_get_panel_info_BIOS(rinfo);
759
760         /*
761          * Parse EDID detailed timings and deduce panel infos if any. Right now
762          * we only deal with first entry returned by parse_EDID, we may do better
763          * some day...
764          */
765         if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type != MT_CRT
766             && rinfo->mon1_EDID) {
767                 struct fb_var_screeninfo var;
768                 RTRACE("Parsing EDID data for panel info\n");
769                 if (fb_parse_edid(rinfo->mon1_EDID, &var) == 0) {
770                         if (var.xres >= rinfo->panel_info.xres &&
771                             var.yres >= rinfo->panel_info.yres)
772                                 radeon_var_to_panel_info(rinfo, &var);
773                 }
774         }
775
776         /*
777          * Do any additional platform/arch fixups to the panel infos
778          */
779         radeon_fixup_panel_info(rinfo);
780
781         /*
782          * If we have some valid panel infos, we setup the default mode based on
783          * those
784          */
785         if (rinfo->mon1_type != MT_CRT && rinfo->panel_info.valid) {
786                 struct fb_var_screeninfo *var = &rinfo->info->var;
787
788                 RTRACE("Setting up default mode based on panel info\n");
789                 var->xres = rinfo->panel_info.xres;
790                 var->yres = rinfo->panel_info.yres;
791                 var->xres_virtual = rinfo->panel_info.xres;
792                 var->yres_virtual = rinfo->panel_info.yres;
793                 var->xoffset = var->yoffset = 0;
794                 var->bits_per_pixel = 8;
795                 var->pixclock = 100000000 / rinfo->panel_info.clock;
796                 var->left_margin = (rinfo->panel_info.hblank - rinfo->panel_info.hOver_plus
797                                     - rinfo->panel_info.hSync_width);
798                 var->right_margin = rinfo->panel_info.hOver_plus;
799                 var->upper_margin = (rinfo->panel_info.vblank - rinfo->panel_info.vOver_plus
800                                      - rinfo->panel_info.vSync_width);
801                 var->lower_margin = rinfo->panel_info.vOver_plus;
802                 var->hsync_len = rinfo->panel_info.hSync_width;
803                 var->vsync_len = rinfo->panel_info.vSync_width;
804                 var->sync = 0;
805                 if (rinfo->panel_info.hAct_high)
806                         var->sync |= FB_SYNC_HOR_HIGH_ACT;
807                 if (rinfo->panel_info.vAct_high)
808                         var->sync |= FB_SYNC_VERT_HIGH_ACT;
809                 var->vmode = 0;
810                 has_default_mode = 1;
811         }
812
813         /*
814          * Now build modedb from EDID
815          */
816         if (rinfo->mon1_EDID) {
817                 rinfo->mon1_modedb = fb_create_modedb(rinfo->mon1_EDID,
818                                                       &rinfo->mon1_dbsize);
819                 fb_get_monitor_limits(rinfo->mon1_EDID, &rinfo->info->monspecs);
820         }
821
822         
823         /*
824          * Finally, if we don't have panel infos we need to figure some (or
825          * we try to read it from card), we try to pick a default mode
826          * and create some panel infos. Whatever...
827          */
828         if (rinfo->mon1_type != MT_CRT && !rinfo->panel_info.valid) {
829                 struct fb_videomode     *modedb;
830                 int                     dbsize;
831                 char                    modename[32];
832
833                 RTRACE("Guessing panel info...\n");
834                 if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
835                         u32 tmp = INREG(FP_HORZ_STRETCH) & HORZ_PANEL_SIZE;
836                         rinfo->panel_info.xres = ((tmp >> HORZ_PANEL_SHIFT) + 1) * 8;
837                         tmp = INREG(FP_VERT_STRETCH) & VERT_PANEL_SIZE;
838                         rinfo->panel_info.yres = (tmp >> VERT_PANEL_SHIFT) + 1;
839                 }
840                 if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
841                         printk(KERN_WARNING "radeonfb: Can't find panel size, going back to CRT\n");
842                         rinfo->mon1_type = MT_CRT;
843                         goto pickup_default;
844                 }
845                 printk(KERN_WARNING "radeonfb: Assuming panel size %dx%d\n",
846                        rinfo->panel_info.xres, rinfo->panel_info.yres);
847                 modedb = rinfo->mon1_modedb;
848                 dbsize = rinfo->mon1_dbsize;
849                 snprintf(modename, 31, "%dx%d", rinfo->panel_info.xres, rinfo->panel_info.yres);
850                 if (fb_find_mode(&rinfo->info->var, rinfo->info, modename,
851                                  modedb, dbsize, NULL, 8) == 0) {
852                         printk(KERN_WARNING "radeonfb: Can't find mode for panel size, going back to CRT\n");
853                         rinfo->mon1_type = MT_CRT;
854                         goto pickup_default;
855                 }
856                 has_default_mode = 1;
857                 radeon_var_to_panel_info(rinfo, &rinfo->info->var);
858         }
859
860  pickup_default:
861         /*
862          * Pick up a random default mode
863          */
864         if (!has_default_mode || mode_option) {
865                 struct fb_videomode default_mode;
866                 if (has_default_mode)
867                         radeon_var_to_videomode(&default_mode, &rinfo->info->var);
868                 else
869                         radeon_var_to_videomode(&default_mode, &radeonfb_default_var);
870                 if (fb_find_mode(&rinfo->info->var, rinfo->info, mode_option,
871                                  rinfo->mon1_modedb, rinfo->mon1_dbsize, &default_mode, 8) == 0)
872                         rinfo->info->var = radeonfb_default_var;
873         }
874
875 }
876
877 /*
878  * The code below is used to pick up a mode in check_var and
879  * set_var. It should be made generic
880  */
881
882 /*
883  * This is used when looking for modes. We assign a "goodness" value
884  * to a mode in the modedb depending how "close" it is from what we
885  * are looking for.
886  * Currently, we don't compare that much, we could do better but
887  * the current fbcon doesn't quite mind ;)
888  */
889 static int radeon_compare_modes(const struct fb_var_screeninfo *var,
890                                 const struct fb_videomode *mode)
891 {
892         int goodness = 0;
893
894         if (var->yres == mode->yres)
895                 goodness += 10;
896         if (var->xres == mode->xres)
897                 goodness += 9;
898         return goodness;
899 }
900
901 /*
902  * This function is called by check_var, it gets the passed in mode parameter, and
903  * outputs a valid mode matching the passed-in one as closely as possible.
904  * We need something better ultimately. Things like fbcon basically pass us out
905  * current mode with xres/yres hacked, while things like XFree will actually
906  * produce a full timing that we should respect as much as possible.
907  *
908  * This is why I added the FB_ACTIVATE_FIND that is used by fbcon. Without this,
909  * we do a simple spec match, that's all. With it, we actually look for a mode in
910  * either our monitor modedb or the vesa one if none
911  *
912  */
913 int  radeon_match_mode(struct radeonfb_info *rinfo,
914                        struct fb_var_screeninfo *dest,
915                        const struct fb_var_screeninfo *src)
916 {
917         const struct fb_videomode       *db = vesa_modes;
918         int                             i, dbsize = 34;
919         int                             has_rmx, native_db = 0;
920         int                             goodness = 0;
921         const struct fb_videomode       *candidate = NULL;
922
923         /* Start with a copy of the requested mode */
924         memcpy(dest, src, sizeof(struct fb_var_screeninfo));
925
926         /* Check if we have a modedb built from EDID */
927         if (rinfo->mon1_modedb) {
928                 db = rinfo->mon1_modedb;
929                 dbsize = rinfo->mon1_dbsize;
930                 native_db = 1;
931         }
932
933         /* Check if we have a scaler allowing any fancy mode */
934         has_rmx = rinfo->mon1_type == MT_LCD || rinfo->mon1_type == MT_DFP;
935
936         /* If we have a scaler and are passed FB_ACTIVATE_TEST or
937          * FB_ACTIVATE_NOW, just do basic checking and return if the
938          * mode match
939          */
940         if ((src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST ||
941             (src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
942                 /* We don't have an RMX, validate timings. If we don't have
943                  * monspecs, we should be paranoid and not let use go above
944                  * 640x480-60, but I assume userland knows what it's doing here
945                  * (though I may be proven wrong...)
946                  */
947                 if (has_rmx == 0 && rinfo->mon1_modedb)
948                         if (fb_validate_mode((struct fb_var_screeninfo *)src, rinfo->info))
949                                 return -EINVAL;
950                 return 0;
951         }
952
953         /* Now look for a mode in the database */
954         while (db) {
955                 for (i = 0; i < dbsize; i++) {
956                         int g;
957
958                         if (db[i].yres < src->yres)
959                                 continue;       
960                         if (db[i].xres < src->xres)
961                                 continue;
962                         g = radeon_compare_modes(src, &db[i]);
963                         /* If the new mode is at least as good as the previous one,
964                          * then it's our new candidate
965                          */
966                         if (g >= goodness) {
967                                 candidate = &db[i];
968                                 goodness = g;
969                         }
970                 }
971                 db = NULL;
972                 /* If we have a scaler, we allow any mode from the database */
973                 if (native_db && has_rmx) {
974                         db = vesa_modes;
975                         dbsize = 34;
976                         native_db = 0;
977                 }
978         }
979
980         /* If we have found a match, return it */
981         if (candidate != NULL) {
982                 radeon_videomode_to_var(dest, candidate);
983                 return 0;
984         }
985
986         /* If we haven't and don't have a scaler, fail */
987         if (!has_rmx)
988                 return -EINVAL;
989
990         return 0;
991 }