7f9838dceab50f05421f7d3a53064e54db6bbd56
[sfrench/cifs-2.6.git] / 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         u8 *pedid = NULL;
68         u8 *pmt = NULL;
69         u8 *tmp;
70         int i, mt = MT_NONE;  
71         
72         RTRACE("analyzing OF properties...\n");
73         pmt = (u8 *)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 = (u8 *)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                 char *pname;
128                 int len, second = 0;
129
130                 dp = dp->child;
131                 do {
132                         if (!dp)
133                                 return MT_NONE;
134                         pname = (char *)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         } while (*s++);
400         if (second)
401                 s2[i] = 0;
402         else {
403                 s1[i] = 0;
404                 s2[0] = 0;
405         }
406         if (strcmp(s1, "CRT") == 0)
407                 rinfo->mon1_type = MT_CRT;
408         else if (strcmp(s1, "TMDS") == 0)
409                 rinfo->mon1_type = MT_DFP;
410         else if (strcmp(s1, "LVDS") == 0)
411                 rinfo->mon1_type = MT_LCD;
412
413         if (strcmp(s2, "CRT") == 0)
414                 rinfo->mon2_type = MT_CRT;
415         else if (strcmp(s2, "TMDS") == 0)
416                 rinfo->mon2_type = MT_DFP;
417         else if (strcmp(s2, "LVDS") == 0)
418                 rinfo->mon2_type = MT_LCD;
419
420         return 1;
421 }
422
423 /*
424  * Probe display on both primary and secondary card's connector (if any)
425  * by various available techniques (i2c, OF device tree, BIOS, ...) and
426  * try to retrieve EDID. The algorithm here comes from XFree's radeon
427  * driver
428  */
429 void __devinit radeon_probe_screens(struct radeonfb_info *rinfo,
430                                     const char *monitor_layout, int ignore_edid)
431 {
432 #ifdef CONFIG_FB_RADEON_I2C
433         int ddc_crt2_used = 0;  
434 #endif
435         int tmp, i;
436
437         radeon_parse_connector_info(rinfo);
438
439         if (radeon_parse_monitor_layout(rinfo, monitor_layout)) {
440
441                 /*
442                  * If user specified a monitor_layout option, use it instead
443                  * of auto-detecting. Maybe we should only use this argument
444                  * on the first radeon card probed or provide a way to specify
445                  * a layout for each card ?
446                  */
447
448                 RTRACE("Using specified monitor layout: %s", monitor_layout);
449 #ifdef CONFIG_FB_RADEON_I2C
450                 if (!ignore_edid) {
451                         if (rinfo->mon1_type != MT_NONE)
452                                 if (!radeon_probe_i2c_connector(rinfo, ddc_dvi, &rinfo->mon1_EDID)) {
453                                         radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon1_EDID);
454                                         ddc_crt2_used = 1;
455                                 }
456                         if (rinfo->mon2_type != MT_NONE)
457                                 if (!radeon_probe_i2c_connector(rinfo, ddc_vga, &rinfo->mon2_EDID) &&
458                                     !ddc_crt2_used)
459                                         radeon_probe_i2c_connector(rinfo, ddc_crt2, &rinfo->mon2_EDID);
460                 }
461 #endif /* CONFIG_FB_RADEON_I2C */
462                 if (rinfo->mon1_type == MT_NONE) {
463                         if (rinfo->mon2_type != MT_NONE) {
464                                 rinfo->mon1_type = rinfo->mon2_type;
465                                 rinfo->mon1_EDID = rinfo->mon2_EDID;
466                         } else {
467                                 rinfo->mon1_type = MT_CRT;
468                                 printk(KERN_INFO "radeonfb: No valid monitor, assuming CRT on first port\n");
469                         }
470                         rinfo->mon2_type = MT_NONE;
471                         rinfo->mon2_EDID = NULL;
472                 }
473         } else {
474                 /*
475                  * Auto-detecting display type (well... trying to ...)
476                  */
477                 
478                 RTRACE("Starting monitor auto detection...\n");
479
480 #if DEBUG && defined(CONFIG_FB_RADEON_I2C)
481                 {
482                         u8 *EDIDs[4] = { NULL, NULL, NULL, NULL };
483                         int mon_types[4] = {MT_NONE, MT_NONE, MT_NONE, MT_NONE};
484                         int i;
485
486                         for (i = 0; i < 4; i++)
487                                 mon_types[i] = radeon_probe_i2c_connector(rinfo,
488                                                                           i+1, &EDIDs[i]);
489                 }
490 #endif /* DEBUG */
491                 /*
492                  * Old single head cards
493                  */
494                 if (!rinfo->has_CRTC2) {
495 #ifdef CONFIG_PPC_OF
496                         if (rinfo->mon1_type == MT_NONE)
497                                 rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
498                                                                         &rinfo->mon1_EDID);
499 #endif /* CONFIG_PPC_OF */
500 #ifdef CONFIG_FB_RADEON_I2C
501                         if (rinfo->mon1_type == MT_NONE)
502                                 rinfo->mon1_type =
503                                         radeon_probe_i2c_connector(rinfo, ddc_dvi,
504                                                                    &rinfo->mon1_EDID);
505                         if (rinfo->mon1_type == MT_NONE)
506                                 rinfo->mon1_type =
507                                         radeon_probe_i2c_connector(rinfo, ddc_vga,
508                                                                    &rinfo->mon1_EDID);
509                         if (rinfo->mon1_type == MT_NONE)
510                                 rinfo->mon1_type =
511                                         radeon_probe_i2c_connector(rinfo, ddc_crt2,
512                                                                    &rinfo->mon1_EDID);  
513 #endif /* CONFIG_FB_RADEON_I2C */
514                         if (rinfo->mon1_type == MT_NONE)
515                                 rinfo->mon1_type = MT_CRT;
516                         goto bail;
517                 }
518
519                 /*
520                  * Check for cards with reversed DACs or TMDS controllers using BIOS
521                  */
522                 if (rinfo->bios_seg &&
523                     (tmp = BIOS_IN16(rinfo->fp_bios_start + 0x50))) {
524                         for (i = 1; i < 4; i++) {
525                                 unsigned int tmp0;
526
527                                 if (!BIOS_IN8(tmp + i*2) && i > 1)
528                                         break;
529                                 tmp0 = BIOS_IN16(tmp + i*2);
530                                 if ((!(tmp0 & 0x01)) && (((tmp0 >> 8) & 0x0f) == ddc_dvi)) {
531                                         rinfo->reversed_DAC = 1;
532                                         printk(KERN_INFO "radeonfb: Reversed DACs detected\n");
533                                 }
534                                 if ((((tmp0 >> 8) & 0x0f) == ddc_dvi) && ((tmp0 >> 4) & 0x01)) {
535                                         rinfo->reversed_TMDS = 1;
536                                         printk(KERN_INFO "radeonfb: Reversed TMDS detected\n");
537                                 }
538                         }
539                 }
540
541                 /*
542                  * Probe primary head (DVI or laptop internal panel)
543                  */
544 #ifdef CONFIG_PPC_OF
545                 if (rinfo->mon1_type == MT_NONE)
546                         rinfo->mon1_type = radeon_probe_OF_head(rinfo, 0,
547                                                                 &rinfo->mon1_EDID);
548 #endif /* CONFIG_PPC_OF */
549 #ifdef CONFIG_FB_RADEON_I2C
550                 if (rinfo->mon1_type == MT_NONE)
551                         rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_dvi,
552                                                                       &rinfo->mon1_EDID);
553                 if (rinfo->mon1_type == MT_NONE) {
554                         rinfo->mon1_type = radeon_probe_i2c_connector(rinfo, ddc_crt2,
555                                                                       &rinfo->mon1_EDID);
556                         if (rinfo->mon1_type != MT_NONE)
557                                 ddc_crt2_used = 1;
558                 }
559 #endif /* CONFIG_FB_RADEON_I2C */
560                 if (rinfo->mon1_type == MT_NONE && rinfo->is_mobility &&
561                     ((rinfo->bios_seg && (INREG(BIOS_4_SCRATCH) & 4))
562                      || (INREG(LVDS_GEN_CNTL) & LVDS_ON))) {
563                         rinfo->mon1_type = MT_LCD;
564                         printk("Non-DDC laptop panel detected\n");
565                 }
566                 if (rinfo->mon1_type == MT_NONE)
567                         rinfo->mon1_type = radeon_crt_is_connected(rinfo, rinfo->reversed_DAC);
568
569                 /*
570                  * Probe secondary head (mostly VGA, can be DVI)
571                  */
572 #ifdef CONFIG_PPC_OF
573                 if (rinfo->mon2_type == MT_NONE)
574                         rinfo->mon2_type = radeon_probe_OF_head(rinfo, 1,
575                                                                 &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                 kfree(rinfo->mon1_EDID);
622                 rinfo->mon1_EDID = NULL;
623                 kfree(rinfo->mon2_EDID);
624                 rinfo->mon2_EDID = NULL;
625         }
626
627  bail:
628         printk(KERN_INFO "radeonfb: Monitor 1 type %s found\n",
629                radeon_get_mon_name(rinfo->mon1_type));
630         if (rinfo->mon1_EDID)
631                 printk(KERN_INFO "radeonfb: EDID probed\n");
632         if (!rinfo->has_CRTC2)
633                 return;
634         printk(KERN_INFO "radeonfb: Monitor 2 type %s found\n",
635                radeon_get_mon_name(rinfo->mon2_type));
636         if (rinfo->mon2_EDID)
637                 printk(KERN_INFO "radeonfb: EDID probed\n");
638 }
639
640
641 /*
642  * This functions applyes any arch/model/machine specific fixups
643  * to the panel info. It may eventually alter EDID block as
644  * well or whatever is specific to a given model and not probed
645  * properly by the default code
646  */
647 static void radeon_fixup_panel_info(struct radeonfb_info *rinfo)
648 {
649 #ifdef CONFIG_PPC_OF
650         /*
651          * LCD Flat panels should use fixed dividers, we enfore that on
652          * PPC only for now...
653          */
654         if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type == MT_LCD
655             && rinfo->is_mobility) {
656                 int ppll_div_sel;
657                 u32 ppll_divn;
658                 ppll_div_sel = INREG8(CLOCK_CNTL_INDEX + 1) & 0x3;
659                 radeon_pll_errata_after_index(rinfo);
660                 ppll_divn = INPLL(PPLL_DIV_0 + ppll_div_sel);
661                 rinfo->panel_info.ref_divider = rinfo->pll.ref_div;
662                 rinfo->panel_info.fbk_divider = ppll_divn & 0x7ff;
663                 rinfo->panel_info.post_divider = (ppll_divn >> 16) & 0x7;
664                 rinfo->panel_info.use_bios_dividers = 1;
665
666                 printk(KERN_DEBUG "radeonfb: Using Firmware dividers 0x%08x "
667                        "from PPLL %d\n",
668                        rinfo->panel_info.fbk_divider |
669                        (rinfo->panel_info.post_divider << 16),
670                        ppll_div_sel);
671         }
672 #endif /* CONFIG_PPC_OF */
673 }
674
675
676 /*
677  * Fill up panel infos from a mode definition, either returned by the EDID
678  * or from the default mode when we can't do any better
679  */
680 static void radeon_var_to_panel_info(struct radeonfb_info *rinfo, struct fb_var_screeninfo *var)
681 {
682         rinfo->panel_info.xres = var->xres;
683         rinfo->panel_info.yres = var->yres;
684         rinfo->panel_info.clock = 100000000 / var->pixclock;
685         rinfo->panel_info.hOver_plus = var->right_margin;
686         rinfo->panel_info.hSync_width = var->hsync_len;
687         rinfo->panel_info.hblank = var->left_margin +
688                 (var->right_margin + var->hsync_len);
689         rinfo->panel_info.vOver_plus = var->lower_margin;
690         rinfo->panel_info.vSync_width = var->vsync_len;
691         rinfo->panel_info.vblank = var->upper_margin +
692                 (var->lower_margin + var->vsync_len);
693         rinfo->panel_info.hAct_high =
694                 (var->sync & FB_SYNC_HOR_HIGH_ACT) != 0;
695         rinfo->panel_info.vAct_high =
696                 (var->sync & FB_SYNC_VERT_HIGH_ACT) != 0;
697         rinfo->panel_info.valid = 1;
698         /* We use a default of 200ms for the panel power delay, 
699          * I need to have a real schedule() instead of mdelay's in the panel code.
700          * we might be possible to figure out a better power delay either from
701          * MacOS OF tree or from the EDID block (proprietary extensions ?)
702          */
703         rinfo->panel_info.pwr_delay = 200;
704 }
705
706 static void radeon_videomode_to_var(struct fb_var_screeninfo *var,
707                                     const struct fb_videomode *mode)
708 {
709         var->xres = mode->xres;
710         var->yres = mode->yres;
711         var->xres_virtual = mode->xres;
712         var->yres_virtual = mode->yres;
713         var->xoffset = 0;
714         var->yoffset = 0;
715         var->pixclock = mode->pixclock;
716         var->left_margin = mode->left_margin;
717         var->right_margin = mode->right_margin;
718         var->upper_margin = mode->upper_margin;
719         var->lower_margin = mode->lower_margin;
720         var->hsync_len = mode->hsync_len;
721         var->vsync_len = mode->vsync_len;
722         var->sync = mode->sync;
723         var->vmode = mode->vmode;
724 }
725
726 /*
727  * Build the modedb for head 1 (head 2 will come later), check panel infos
728  * from either BIOS or EDID, and pick up the default mode
729  */
730 void __devinit radeon_check_modes(struct radeonfb_info *rinfo, const char *mode_option)
731 {
732         struct fb_info * info = rinfo->info;
733         int has_default_mode = 0;
734
735         /*
736          * Fill default var first
737          */
738         info->var = radeonfb_default_var;
739         INIT_LIST_HEAD(&info->modelist);
740
741         /*
742          * First check out what BIOS has to say
743          */
744         if (rinfo->mon1_type == MT_LCD)
745                 radeon_get_panel_info_BIOS(rinfo);
746
747         /*
748          * Parse EDID detailed timings and deduce panel infos if any. Right now
749          * we only deal with first entry returned by parse_EDID, we may do better
750          * some day...
751          */
752         if (!rinfo->panel_info.use_bios_dividers && rinfo->mon1_type != MT_CRT
753             && rinfo->mon1_EDID) {
754                 struct fb_var_screeninfo var;
755                 RTRACE("Parsing EDID data for panel info\n");
756                 if (fb_parse_edid(rinfo->mon1_EDID, &var) == 0) {
757                         if (var.xres >= rinfo->panel_info.xres &&
758                             var.yres >= rinfo->panel_info.yres)
759                                 radeon_var_to_panel_info(rinfo, &var);
760                 }
761         }
762
763         /*
764          * Do any additional platform/arch fixups to the panel infos
765          */
766         radeon_fixup_panel_info(rinfo);
767
768         /*
769          * If we have some valid panel infos, we setup the default mode based on
770          * those
771          */
772         if (rinfo->mon1_type != MT_CRT && rinfo->panel_info.valid) {
773                 struct fb_var_screeninfo *var = &info->var;
774
775                 RTRACE("Setting up default mode based on panel info\n");
776                 var->xres = rinfo->panel_info.xres;
777                 var->yres = rinfo->panel_info.yres;
778                 var->xres_virtual = rinfo->panel_info.xres;
779                 var->yres_virtual = rinfo->panel_info.yres;
780                 var->xoffset = var->yoffset = 0;
781                 var->bits_per_pixel = 8;
782                 var->pixclock = 100000000 / rinfo->panel_info.clock;
783                 var->left_margin = (rinfo->panel_info.hblank - rinfo->panel_info.hOver_plus
784                                     - rinfo->panel_info.hSync_width);
785                 var->right_margin = rinfo->panel_info.hOver_plus;
786                 var->upper_margin = (rinfo->panel_info.vblank - rinfo->panel_info.vOver_plus
787                                      - rinfo->panel_info.vSync_width);
788                 var->lower_margin = rinfo->panel_info.vOver_plus;
789                 var->hsync_len = rinfo->panel_info.hSync_width;
790                 var->vsync_len = rinfo->panel_info.vSync_width;
791                 var->sync = 0;
792                 if (rinfo->panel_info.hAct_high)
793                         var->sync |= FB_SYNC_HOR_HIGH_ACT;
794                 if (rinfo->panel_info.vAct_high)
795                         var->sync |= FB_SYNC_VERT_HIGH_ACT;
796                 var->vmode = 0;
797                 has_default_mode = 1;
798         }
799
800         /*
801          * Now build modedb from EDID
802          */
803         if (rinfo->mon1_EDID) {
804                 fb_edid_to_monspecs(rinfo->mon1_EDID, &info->monspecs);
805                 fb_videomode_to_modelist(info->monspecs.modedb,
806                                          info->monspecs.modedb_len,
807                                          &info->modelist);
808                 rinfo->mon1_modedb = info->monspecs.modedb;
809                 rinfo->mon1_dbsize = info->monspecs.modedb_len;
810         }
811
812         
813         /*
814          * Finally, if we don't have panel infos we need to figure some (or
815          * we try to read it from card), we try to pick a default mode
816          * and create some panel infos. Whatever...
817          */
818         if (rinfo->mon1_type != MT_CRT && !rinfo->panel_info.valid) {
819                 struct fb_videomode     *modedb;
820                 int                     dbsize;
821                 char                    modename[32];
822
823                 RTRACE("Guessing panel info...\n");
824                 if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
825                         u32 tmp = INREG(FP_HORZ_STRETCH) & HORZ_PANEL_SIZE;
826                         rinfo->panel_info.xres = ((tmp >> HORZ_PANEL_SHIFT) + 1) * 8;
827                         tmp = INREG(FP_VERT_STRETCH) & VERT_PANEL_SIZE;
828                         rinfo->panel_info.yres = (tmp >> VERT_PANEL_SHIFT) + 1;
829                 }
830                 if (rinfo->panel_info.xres == 0 || rinfo->panel_info.yres == 0) {
831                         printk(KERN_WARNING "radeonfb: Can't find panel size, going back to CRT\n");
832                         rinfo->mon1_type = MT_CRT;
833                         goto pickup_default;
834                 }
835                 printk(KERN_WARNING "radeonfb: Assuming panel size %dx%d\n",
836                        rinfo->panel_info.xres, rinfo->panel_info.yres);
837                 modedb = rinfo->mon1_modedb;
838                 dbsize = rinfo->mon1_dbsize;
839                 snprintf(modename, 31, "%dx%d", rinfo->panel_info.xres, rinfo->panel_info.yres);
840                 if (fb_find_mode(&info->var, info, modename,
841                                  modedb, dbsize, NULL, 8) == 0) {
842                         printk(KERN_WARNING "radeonfb: Can't find mode for panel size, going back to CRT\n");
843                         rinfo->mon1_type = MT_CRT;
844                         goto pickup_default;
845                 }
846                 has_default_mode = 1;
847                 radeon_var_to_panel_info(rinfo, &info->var);
848         }
849
850  pickup_default:
851         /*
852          * Apply passed-in mode option if any
853          */
854         if (mode_option) {
855                 if (fb_find_mode(&info->var, info, mode_option,
856                                  info->monspecs.modedb,
857                                  info->monspecs.modedb_len, NULL, 8) != 0)
858                         has_default_mode = 1;
859         }
860
861         /*
862          * Still no mode, let's pick up a default from the db
863          */
864         if (!has_default_mode && info->monspecs.modedb != NULL) {
865                 struct fb_monspecs *specs = &info->monspecs;
866                 struct fb_videomode *modedb = NULL;
867
868                 /* get preferred timing */
869                 if (specs->misc & FB_MISC_1ST_DETAIL) {
870                         int i;
871
872                         for (i = 0; i < specs->modedb_len; i++) {
873                                 if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
874                                         modedb = &specs->modedb[i];
875                                         break;
876                                 }
877                         }
878                 } else {
879                         /* otherwise, get first mode in database */
880                         modedb = &specs->modedb[0];
881                 }
882                 if (modedb != NULL) {
883                         info->var.bits_per_pixel = 8;
884                         radeon_videomode_to_var(&info->var, modedb);
885                         has_default_mode = 1;
886                 }
887         }
888         if (1) {
889                 struct fb_videomode mode;
890                 /* Make sure that whatever mode got selected is actually in the
891                  * modelist or the kernel may die
892                  */
893                 fb_var_to_videomode(&mode, &info->var);
894                 fb_add_videomode(&mode, &info->modelist);
895         }
896 }
897
898 /*
899  * The code below is used to pick up a mode in check_var and
900  * set_var. It should be made generic
901  */
902
903 /*
904  * This is used when looking for modes. We assign a "distance" value
905  * to a mode in the modedb depending how "close" it is from what we
906  * are looking for.
907  * Currently, we don't compare that much, we could do better but
908  * the current fbcon doesn't quite mind ;)
909  */
910 static int radeon_compare_modes(const struct fb_var_screeninfo *var,
911                                 const struct fb_videomode *mode)
912 {
913         int distance = 0;
914
915         distance = mode->yres - var->yres;
916         distance += (mode->xres - var->xres)/2;
917         return distance;
918 }
919
920 /*
921  * This function is called by check_var, it gets the passed in mode parameter, and
922  * outputs a valid mode matching the passed-in one as closely as possible.
923  * We need something better ultimately. Things like fbcon basically pass us out
924  * current mode with xres/yres hacked, while things like XFree will actually
925  * produce a full timing that we should respect as much as possible.
926  *
927  * This is why I added the FB_ACTIVATE_FIND that is used by fbcon. Without this,
928  * we do a simple spec match, that's all. With it, we actually look for a mode in
929  * either our monitor modedb or the vesa one if none
930  *
931  */
932 int  radeon_match_mode(struct radeonfb_info *rinfo,
933                        struct fb_var_screeninfo *dest,
934                        const struct fb_var_screeninfo *src)
935 {
936         const struct fb_videomode       *db = vesa_modes;
937         int                             i, dbsize = 34;
938         int                             has_rmx, native_db = 0;
939         int                             distance = INT_MAX;
940         const struct fb_videomode       *candidate = NULL;
941
942         /* Start with a copy of the requested mode */
943         memcpy(dest, src, sizeof(struct fb_var_screeninfo));
944
945         /* Check if we have a modedb built from EDID */
946         if (rinfo->mon1_modedb) {
947                 db = rinfo->mon1_modedb;
948                 dbsize = rinfo->mon1_dbsize;
949                 native_db = 1;
950         }
951
952         /* Check if we have a scaler allowing any fancy mode */
953         has_rmx = rinfo->mon1_type == MT_LCD || rinfo->mon1_type == MT_DFP;
954
955         /* If we have a scaler and are passed FB_ACTIVATE_TEST or
956          * FB_ACTIVATE_NOW, just do basic checking and return if the
957          * mode match
958          */
959         if ((src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST ||
960             (src->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
961                 /* We don't have an RMX, validate timings. If we don't have
962                  * monspecs, we should be paranoid and not let use go above
963                  * 640x480-60, but I assume userland knows what it's doing here
964                  * (though I may be proven wrong...)
965                  */
966                 if (has_rmx == 0 && rinfo->mon1_modedb)
967                         if (fb_validate_mode((struct fb_var_screeninfo *)src, rinfo->info))
968                                 return -EINVAL;
969                 return 0;
970         }
971
972         /* Now look for a mode in the database */
973         while (db) {
974                 for (i = 0; i < dbsize; i++) {
975                         int d;
976
977                         if (db[i].yres < src->yres)
978                                 continue;       
979                         if (db[i].xres < src->xres)
980                                 continue;
981                         d = radeon_compare_modes(src, &db[i]);
982                         /* If the new mode is at least as good as the previous one,
983                          * then it's our new candidate
984                          */
985                         if (d < distance) {
986                                 candidate = &db[i];
987                                 distance = d;
988                         }
989                 }
990                 db = NULL;
991                 /* If we have a scaler, we allow any mode from the database */
992                 if (native_db && has_rmx) {
993                         db = vesa_modes;
994                         dbsize = 34;
995                         native_db = 0;
996                 }
997         }
998
999         /* If we have found a match, return it */
1000         if (candidate != NULL) {
1001                 radeon_videomode_to_var(dest, candidate);
1002                 return 0;
1003         }
1004
1005         /* If we haven't and don't have a scaler, fail */
1006         if (!has_rmx)
1007                 return -EINVAL;
1008
1009         return 0;
1010 }