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