randr12: Improve TMDS and LVDS registers (and enable for LVDS).
[nouveau] / src / nv_output.c
1 /*
2  * Copyright 2006 Dave Airlie
3  * Copyright 2007 Maarten Maathuis
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 /*
25  * this code uses ideas taken from the NVIDIA nv driver - the nvidia license
26  * decleration is at the bottom of this file as it is rather ugly 
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "xf86.h"
34 #include "os.h"
35 #include "mibank.h"
36 #include "globals.h"
37 #include "xf86.h"
38 #include "xf86Priv.h"
39 #include "xf86DDC.h"
40 #include "mipointer.h"
41 #include "windowstr.h"
42 #include <randrstr.h>
43 #include <X11/extensions/render.h>
44
45 #include "xf86Crtc.h"
46 #include "nv_include.h"
47
48 const char *OutputType[] = {
49     "None",
50     "VGA",
51     "DVI",
52     "LVDS",
53     "S-video",
54     "Composite",
55 };
56
57 const char *MonTypeName[7] = {
58     "AUTO",
59     "NONE",
60     "CRT",
61     "LVDS",
62     "TMDS",
63     "CTV",
64     "STV"
65 };
66
67 /* 
68  * TMDS registers are indirect 8 bit registers.
69  * Reading is straightforward, writing a bit odd.
70  * Reading: Write adress (+write protect bit, do not forget this), then read value.
71  * Writing: Write adress (+write protect bit), write value, write adress again and write it again (+write protect bit).
72  */
73
74 void NVWriteTMDS(NVPtr pNv, int ramdac, CARD32 tmds_reg, CARD32 val)
75 {
76         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, 
77                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
78
79         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_DATA, val & 0xff);
80
81         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, tmds_reg & 0xff);
82         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, 
83                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
84 }
85
86 CARD8 NVReadTMDS(NVPtr pNv, int ramdac, CARD32 tmds_reg)
87 {
88         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, 
89                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
90
91         return (nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_DATA) & 0xff);
92 }
93
94 void NVOutputWriteTMDS(xf86OutputPtr output, CARD32 tmds_reg, CARD32 val)
95 {
96         NVOutputPrivatePtr nv_output = output->driver_private;
97         ScrnInfoPtr     pScrn = output->scrn;
98         NVPtr pNv = NVPTR(pScrn);
99         int ramdac;
100
101         /* Is TMDS programmed on a different output? */
102         /* Always choose the prefered ramdac, since that one contains the tmds stuff */
103         /* Assumption: there is always once output that can only run of the primary ramdac */
104         if (nv_output->valid_ramdac & RAMDAC_1) {
105                 ramdac = 1;
106         } else {
107                 ramdac = 0;
108         }
109
110         NVWriteTMDS(pNv, ramdac, tmds_reg, val);
111 }
112
113 CARD8 NVOutputReadTMDS(xf86OutputPtr output, CARD32 tmds_reg)
114 {
115         NVOutputPrivatePtr nv_output = output->driver_private;
116         ScrnInfoPtr     pScrn = output->scrn;
117         NVPtr pNv = NVPTR(pScrn);
118         int ramdac;
119
120         /* Is TMDS programmed on a different output? */
121         /* Always choose the prefered ramdac, since that one contains the tmds stuff */
122         /* Assumption: there is always once output that can only run of the primary ramdac */
123         if (nv_output->valid_ramdac & RAMDAC_1) {
124                 ramdac = 1;
125         } else {
126                 ramdac = 0;
127         }
128
129         return NVReadTMDS(pNv, ramdac, tmds_reg);
130 }
131
132 void NVOutputWriteRAMDAC(xf86OutputPtr output, CARD32 ramdac_reg, CARD32 val)
133 {
134     NVOutputPrivatePtr nv_output = output->driver_private;
135     ScrnInfoPtr pScrn = output->scrn;
136     NVPtr pNv = NVPTR(pScrn);
137
138     nvWriteRAMDAC(pNv, nv_output->ramdac, ramdac_reg, val);
139 }
140
141 CARD32 NVOutputReadRAMDAC(xf86OutputPtr output, CARD32 ramdac_reg)
142 {
143     NVOutputPrivatePtr nv_output = output->driver_private;
144     ScrnInfoPtr pScrn = output->scrn;
145     NVPtr pNv = NVPTR(pScrn);
146
147     return nvReadRAMDAC(pNv, nv_output->ramdac, ramdac_reg);
148 }
149
150 static void nv_output_backlight_enable(xf86OutputPtr output,  Bool on)
151 {
152         ScrnInfoPtr pScrn = output->scrn;
153         NVPtr pNv = NVPTR(pScrn);
154
155         ErrorF("nv_output_backlight_enable is called for output %s to turn %s\n", output->name, on ? "on" : "off");
156
157         /* This is done differently on each laptop.  Here we
158          * define the ones we know for sure. */
159
160 #if defined(__powerpc__)
161         if ((pNv->Chipset == 0x10DE0179) ||
162             (pNv->Chipset == 0x10DE0189) ||
163             (pNv->Chipset == 0x10DE0329)) {
164                 /* NV17,18,34 Apple iMac, iBook, PowerBook */
165                 CARD32 tmp_pmc, tmp_pcrt;
166                 tmp_pmc = nvReadMC(pNv, 0x10F0) & 0x7FFFFFFF;
167                 tmp_pcrt = nvReadCRTC0(pNv, NV_CRTC_081C) & 0xFFFFFFFC;
168                 if (on) {
169                         tmp_pmc |= (1 << 31);
170                         tmp_pcrt |= 0x1;
171                 }
172                 nvWriteMC(pNv, 0x10F0, tmp_pmc);
173                 nvWriteCRTC0(pNv, NV_CRTC_081C, tmp_pcrt);
174         }
175 #endif
176
177         if(pNv->twoHeads && ((pNv->Chipset & 0x0ff0) != CHIPSET_NV11))
178                 nvWriteMC(pNv, 0x130C, on ? 3 : 7);
179 }
180
181 static void
182 nv_lvds_output_dpms(xf86OutputPtr output, int mode)
183 {
184         NVOutputPrivatePtr nv_output = output->driver_private;
185         NVPtr pNv = NVPTR(output->scrn);
186
187         if (nv_output->ramdac != -1 && mode != DPMSModeOff) {
188                 /* This was not a modeset, but a normal dpms call */
189                 pNv->ramdac_active[nv_output->ramdac] = TRUE;
190                 ErrorF("Activating ramdac %d\n", nv_output->ramdac);
191                 nv_output->ramdac_assigned = TRUE;
192         }
193
194         switch (mode) {
195         case DPMSModeStandby:
196         case DPMSModeSuspend:
197         case DPMSModeOff:
198                 nv_output_backlight_enable(output, 0);
199                 break;
200         case DPMSModeOn:
201                 nv_output_backlight_enable(output, 1);
202         default:
203                 break;
204         }
205
206         /* We may be going for modesetting, so we must reset the ramdacs */
207         if (nv_output->ramdac != -1 && mode == DPMSModeOff) {
208                 pNv->ramdac_active[nv_output->ramdac] = FALSE;
209                 ErrorF("Deactivating ramdac %d\n", nv_output->ramdac);
210                 nv_output->ramdac_assigned = FALSE;
211         }
212 }
213
214 static void
215 nv_analog_output_dpms(xf86OutputPtr output, int mode)
216 {
217         xf86CrtcPtr crtc = output->crtc;
218         NVOutputPrivatePtr nv_output = output->driver_private;
219         NVPtr pNv = NVPTR(output->scrn);
220
221         ErrorF("nv_analog_output_dpms is called with mode %d\n", mode);
222
223         if (nv_output->ramdac != -1) {
224                 /* We may be going for modesetting, so we must reset the ramdacs */
225                 if (mode == DPMSModeOff) {
226                         pNv->ramdac_active[nv_output->ramdac] = FALSE;
227                         ErrorF("Deactivating ramdac %d\n", nv_output->ramdac);
228                         nv_output->ramdac_assigned = FALSE;
229                         /* This was not a modeset, but a normal dpms call */
230                 } else {
231                         pNv->ramdac_active[nv_output->ramdac] = TRUE;
232                         ErrorF("Activating ramdac %d\n", nv_output->ramdac);
233                         nv_output->ramdac_assigned = TRUE;
234                 }
235         }
236
237         if (crtc) {
238                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
239
240                 ErrorF("nv_analog_output_dpms is called for CRTC %d with mode %d\n", nv_crtc->crtc, mode);
241         }
242 }
243
244 static void
245 nv_tmds_output_dpms(xf86OutputPtr output, int mode)
246 {
247         xf86CrtcPtr crtc = output->crtc;
248         NVOutputPrivatePtr nv_output = output->driver_private;
249         NVPtr pNv = NVPTR(output->scrn);
250
251         ErrorF("nv_tmds_output_dpms is called with mode %d\n", mode);
252
253         /* We just woke up again from an actual monitor dpms and not a modeset prepare */
254         /* Put here since we actually need our ramdac to wake up again ;-) */
255         if (nv_output->ramdac != -1 && mode != DPMSModeOff) {
256                 pNv->ramdac_active[nv_output->ramdac] = TRUE;
257                 nv_output->ramdac_assigned = TRUE;
258                 ErrorF("Activating ramdac %d\n", nv_output->ramdac);
259         }
260
261         /* Are we assigned a ramdac already?, else we will be activated during mode set */
262         if (crtc && nv_output->ramdac != -1) {
263                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
264
265                 ErrorF("nv_tmds_output_dpms is called for CRTC %d with mode %d\n", nv_crtc->crtc, mode);
266
267                 CARD32 fpcontrol = nvReadRAMDAC(pNv, nv_output->ramdac, NV_RAMDAC_FP_CONTROL);
268                 switch(mode) {
269                         case DPMSModeStandby:
270                         case DPMSModeSuspend:
271                         case DPMSModeOff:
272                                 /* cut the TMDS output */           
273                                 fpcontrol |= 0x20000022;
274                                 break;
275                         case DPMSModeOn:
276                                 /* disable cutting the TMDS output */
277                                 fpcontrol &= ~0x20000022;
278                                 break;
279                 }
280                 nvWriteRAMDAC(pNv, nv_output->ramdac, NV_RAMDAC_FP_CONTROL, fpcontrol);
281         }
282
283         /* We may be going for modesetting, so we must reset the ramdacs */
284         if (nv_output->ramdac != -1 && mode == DPMSModeOff) {
285                 pNv->ramdac_active[nv_output->ramdac] = FALSE;
286                 nv_output->ramdac_assigned = FALSE;
287                 ErrorF("Deactivating ramdac %d\n", nv_output->ramdac);
288         }
289 }
290
291 /* This sequence is an optimized/shortened version of what the blob does */
292 uint32_t tmds_regs_nv40[] = { 0x04, 0x05, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x40, 0x43, 0x00, 0x01, 0x02, 0x2e, 0x2f, 0x3a };
293 uint32_t tmds_regs_nv30[] = { 0x04, 0x05, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x29, 0x2a, 0x00, 0x01, 0x02, 0x2e, 0x2f, 0x3a };
294
295 #define TMDS_REGS(index) ( tmds_regs(pNv, index) )
296
297 uint32_t tmds_regs(NVPtr pNv, int i)
298 {
299         if (pNv->Architecture == NV_ARCH_40) {
300                 return tmds_regs_nv40[i];
301         } else {
302                 return tmds_regs_nv30[i];
303         }
304 }
305
306 #define TMDS_SIZE ( tmds_size(pNv) )
307
308 uint32_t tmds_size(NVPtr pNv)
309 {
310         if (pNv->Architecture == NV_ARCH_40) {
311                 return(sizeof(tmds_regs_nv40)/sizeof(tmds_regs_nv40[0]));
312         } else {
313                 return(sizeof(tmds_regs_nv30)/sizeof(tmds_regs_nv30[0]));
314         }
315 }
316
317 void nv_output_save_state_ext(xf86OutputPtr output, RIVA_HW_STATE *state, Bool override)
318 {
319         NVOutputPrivatePtr nv_output = output->driver_private;
320         ScrnInfoPtr pScrn = output->scrn;
321         NVPtr pNv = NVPTR(pScrn);
322         NVOutputRegPtr regp;
323         int i;
324
325         regp = &state->dac_reg[nv_output->ramdac];
326         regp->test_control      = NVOutputReadRAMDAC(output, NV_RAMDAC_TEST_CONTROL);
327         regp->unk_670   = NVOutputReadRAMDAC(output, NV_RAMDAC_670);
328         state->config       = nvReadFB(pNv, NV_PFB_CFG0);
329
330         //regp->unk_900         = NVOutputReadRAMDAC(output, NV_RAMDAC_900);
331
332         /* This exists purely for proper text mode restore */
333         if (override) regp->output = NVOutputReadRAMDAC(output, NV_RAMDAC_OUTPUT);
334
335         for (i = 0; i < TMDS_SIZE; i++) {
336                 regp->TMDS[TMDS_REGS(i)] = NVOutputReadTMDS(output, TMDS_REGS(i));
337         }
338 }
339
340 void nv_output_load_state_ext(xf86OutputPtr output, RIVA_HW_STATE *state, Bool override)
341 {
342         NVOutputPrivatePtr nv_output = output->driver_private;
343         NVOutputRegPtr regp;
344         ScrnInfoPtr pScrn = output->scrn;
345         NVPtr pNv = NVPTR(pScrn);
346         int i;
347
348         regp = &state->dac_reg[nv_output->ramdac];
349
350         /* This exists purely for proper text mode restore */
351         if (override) NVOutputWriteRAMDAC(output, NV_RAMDAC_OUTPUT, regp->output);
352
353         //NVOutputWriteRAMDAC(output, NV_RAMDAC_900, regp->unk_900);
354
355         NVOutputWriteRAMDAC(output, NV_RAMDAC_TEST_CONTROL, regp->test_control);
356         NVOutputWriteRAMDAC(output, NV_RAMDAC_670, regp->unk_670);
357
358         for (i = 0; i < TMDS_SIZE; i++) {
359                 NVOutputWriteTMDS(output, TMDS_REGS(i), regp->TMDS[TMDS_REGS(i)]);
360         }
361 }
362
363 /* NOTE: Don't rely on this data for anything other than restoring VT's */
364
365 static void
366 nv_output_save (xf86OutputPtr output)
367 {
368         ScrnInfoPtr     pScrn = output->scrn;
369         NVPtr pNv = NVPTR(pScrn);
370         RIVA_HW_STATE *state;
371         NVOutputPrivatePtr nv_output = output->driver_private;
372         int ramdac_backup = nv_output->ramdac;
373
374         ErrorF("nv_output_save is called\n");
375
376         /* This is early init and we have not yet been assigned a ramdac */
377         /* Always choose the prefered ramdac, for consistentcy */
378         /* Assumption: there is always once output that can only run of the primary ramdac */
379         if (nv_output->valid_ramdac & RAMDAC_1) {
380                 nv_output->ramdac = 1;
381         } else {
382                 nv_output->ramdac = 0;
383         }
384
385         state = &pNv->SavedReg;
386
387         /* Due to strange mapping of outputs we could have swapped analog and digital */
388         /* So we force save all the registers */
389         nv_output_save_state_ext(output, state, TRUE);
390
391         /* restore previous state */
392         nv_output->ramdac = ramdac_backup;
393 }
394
395 static void
396 nv_output_restore (xf86OutputPtr output)
397 {
398         ScrnInfoPtr pScrn = output->scrn;
399         NVPtr pNv = NVPTR(pScrn);
400         RIVA_HW_STATE *state;
401         NVOutputPrivatePtr nv_output = output->driver_private;
402         int ramdac_backup = nv_output->ramdac;
403
404         ErrorF("nv_output_restore is called\n");
405
406         /* We want consistent mode restoring and the ramdac entry is variable */
407         /* Always choose the prefered ramdac, for consistentcy */
408         /* Assumption: there is always once output that can only run of the primary ramdac */
409         if (nv_output->valid_ramdac & RAMDAC_1) {
410                 nv_output->ramdac = 1;
411         } else {
412                 nv_output->ramdac = 0;
413         }
414
415         state = &pNv->SavedReg;
416
417         /* Due to strange mapping of outputs we could have swapped analog and digital */
418         /* So we force load all the registers */
419         nv_output_load_state_ext(output, state, TRUE);
420
421         /* restore previous state */
422         nv_output->ramdac = ramdac_backup;
423 }
424
425 static int
426 nv_output_mode_valid(xf86OutputPtr output, DisplayModePtr pMode)
427 {
428         if (pMode->Flags & V_DBLSCAN)
429                 return MODE_NO_DBLESCAN;
430
431         if (pMode->Clock > 400000 || pMode->Clock < 25000)
432                 return MODE_CLOCK_RANGE;
433
434         return MODE_OK;
435 }
436
437
438 static Bool
439 nv_output_mode_fixup(xf86OutputPtr output, DisplayModePtr mode,
440                      DisplayModePtr adjusted_mode)
441 {
442         ErrorF("nv_output_mode_fixup is called\n");
443
444         return TRUE;
445 }
446
447 static void
448 nv_output_mode_set_regs(xf86OutputPtr output, DisplayModePtr mode, DisplayModePtr adjusted_mode)
449 {
450         NVOutputPrivatePtr nv_output = output->driver_private;
451         ScrnInfoPtr pScrn = output->scrn;
452         NVPtr pNv = NVPTR(pScrn);
453         RIVA_HW_STATE *state, *sv_state;
454         Bool is_fp = FALSE;
455         Bool is_lvds = FALSE;
456         NVOutputRegPtr regp, regp2, savep;
457         xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
458         int i;
459
460         xf86CrtcPtr crtc = output->crtc;
461         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
462
463         state = &pNv->ModeReg;
464         regp = &state->dac_reg[nv_output->ramdac];
465         /* The other ramdac */
466         regp2 = &state->dac_reg[(~(nv_output->ramdac)) & 1];
467
468         sv_state = &pNv->SavedReg;
469         savep = &sv_state->dac_reg[nv_output->ramdac];
470
471         if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS)) {
472                 is_fp = TRUE;
473                 if (nv_output->type == OUTPUT_LVDS) {
474                         is_lvds = TRUE;
475                 }
476         }
477
478         /* This is just a guess, there are probably more registers which need setting */
479         /* But we must start somewhere ;-) */
480         if (is_fp) {
481                 regp->TMDS[0x4] = 0x80;
482                 /* Enable crosswired mode */
483                 /* This will upset the monitor, trust me, i know it :-( */
484                 /* Now allowed for non-bios inited systems */
485                 if (nv_crtc->head != nv_output->preferred_crtc) {
486                         regp->TMDS[0x4] |= (1 << 3);
487                 }
488
489                 if (is_lvds) {
490                         regp->TMDS[0x4] |= (1 << 0);
491                 }
492         }
493
494         /* The TMDS game begins */
495         /* A few registers are also programmed on non-tmds monitors */
496         /* At the moment i can't give rationale for these values */
497         if (!is_fp) {
498                 regp->TMDS[0x2e] = 0x80;
499                 regp->TMDS[0x2f] = 0xff;
500                 regp->TMDS[0x33] = 0xfe;
501         } else {
502                 NVCrtcPrivatePtr nv_crtc = output->crtc->driver_private;
503                 uint32_t pll_setup_control = nvReadRAMDAC(pNv, 0, NV_RAMDAC_PLL_SETUP_CONTROL);
504                 regp->TMDS[0x2b] = 0x7d;
505                 regp->TMDS[0x2c] = 0x0;
506                 /* Various combinations exist for lvds, 0x08, 0x48, 0xc8, 0x88 */
507                 /* 0x88 seems most popular and (maybe) the end setting */
508                 if (is_lvds) {
509                         if (pNv->Architecture == NV_ARCH_40) {
510                                 regp->TMDS[0x2e] = 0x88;
511                         } else {
512                                 /* observed on nv31m */
513                                 regp->TMDS[0x2e] = 0x0;
514                         }
515                 } else {
516                         if (nv_crtc->head == 1) {
517                                 regp->TMDS[0x2e] = 0x81;
518                         } else {
519                                 regp->TMDS[0x2e] = 0x85;
520                         }
521                 }
522                 /* 0x08 is also seen for lvds */
523                 regp->TMDS[0x2f] = 0x21;
524                 regp->TMDS[0x30] = 0x0;
525                 regp->TMDS[0x31] = 0x0;
526                 regp->TMDS[0x32] = 0x0;
527                 regp->TMDS[0x33] = 0xf0;
528                 /* 0x00 is also seen for lvds */
529                 regp->TMDS[0x3a] = 0x80;
530
531                 /* Here starts the registers that may cause problems for some */
532                 /* This an educated guess */
533                 if (pNv->Architecture == NV_ARCH_40 && pNv->misc_info.reg_c040 & (1 << 10)) {
534                         regp->TMDS[0x5] = 0x68;
535                 } else {
536                         regp->TMDS[0x5] = 0x6e;
537                 }
538
539                 if (is_lvds) {
540                         if (pNv->Architecture == NV_ARCH_40) {
541                                 regp->TMDS[0x0] = 0x61;
542                         } else {
543                                 /* observed on a nv31m */
544                                 regp->TMDS[0x0] = 0x71;
545                         }
546                 } else {
547                         /* This seems to be related to PLL_SETUP_CONTROL */
548                         /* When PLL_SETUP_CONTROL ends with 0x1c, then this value is 0xc1 */
549                         /* Otherwise 0xf1 */
550                         if ((pll_setup_control & 0xff) == 0x1c) {
551                                 regp->TMDS[0x0] = 0xc1;
552                         } else {
553                                 regp->TMDS[0x0] = 0xf1;
554                         }
555                 }
556
557                 /* This is also related to PLL_SETUP_CONTROL, exactly how is unknown */
558                 if (pll_setup_control == 0) {
559                         regp->TMDS[0x1] = 0x0;
560                 } else {
561                         if (nvReadRAMDAC(pNv, 0, NV_RAMDAC_SEL_CLK) & (1<<12)) {
562                                 regp->TMDS[0x1] = 0x41;
563                         } else {
564                                 regp->TMDS[0x1] = 0x42;
565                         }
566                 }
567
568                 if (is_lvds) {
569                         regp->TMDS[0x2] = 0x0;
570                 } else {
571                         if (pll_setup_control == 0x0) {
572                                 regp->TMDS[0x2] = 0x90;
573                         } else {
574                                 regp->TMDS[0x2] = 0x89;
575                         }
576                 }
577
578                 /* I assume they are zero for !is_lvds */
579                 if (is_lvds && pNv->Architecture == NV_ARCH_40) {
580                         /* Observed values are 0x11 and 0x14, TODO: this needs refinement */
581                         regp->TMDS[0x40] = 0x14;
582                         regp->TMDS[0x43] = 0xb0;
583                 }
584         }
585
586         /* Put test control into what seems to be the neutral position */
587         if (pNv->NVArch < 0x44) {
588                 regp->test_control = 0x00000000;
589         } else {
590                 regp->test_control = 0x00100000;
591         }
592
593         /* This is a similar register to test control */
594         regp->unk_670 = regp->test_control;
595
596         /* This may be causing problems */
597         //regp->unk_900 = 0x10000;
598
599         if (output->crtc) {
600                 NVCrtcPrivatePtr nv_crtc = output->crtc->driver_private;
601                 int two_crt = FALSE;
602                 int two_mon = FALSE;
603
604                 for (i = 0; i < config->num_output; i++) {
605                         NVOutputPrivatePtr nv_output2 = config->output[i]->driver_private;
606
607                         /* is it this output ?? */
608                         if (config->output[i] == output)
609                                 continue;
610
611                         /* it the output connected */
612                         if (config->output[i]->crtc == NULL)
613                                 continue;
614
615                         two_mon = TRUE;
616                         if ((nv_output2->type == OUTPUT_ANALOG) && (nv_output->type == OUTPUT_ANALOG)) {
617                                 two_crt = TRUE;
618                         }
619                 }
620
621                 ErrorF("%d: crtc %d ramdac %d twocrt %d twomon %d\n", is_fp, nv_crtc->crtc, nv_output->ramdac, two_crt, two_mon);
622         }
623 }
624
625 static void
626 nv_output_mode_set_routing(xf86OutputPtr output)
627 {
628         NVOutputPrivatePtr nv_output = output->driver_private;
629         xf86CrtcPtr crtc = output->crtc;
630         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
631         ScrnInfoPtr     pScrn = output->scrn;
632         NVPtr pNv = NVPTR(pScrn);
633         Bool is_fp = FALSE;
634         int other_ramdac = 0;
635
636         uint32_t output_reg = nvReadRAMDAC(pNv, nv_output->ramdac, NV_RAMDAC_OUTPUT);
637
638         if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS)) {
639                 is_fp = TRUE;
640         }
641
642         if (is_fp) {
643                 output_reg = 0x0;
644         } else { 
645                 output_reg = NV_RAMDAC_OUTPUT_DAC_ENABLE;
646         }
647
648         if (nv_crtc->head == 1) {
649                 output_reg |= NV_RAMDAC_OUTPUT_SELECT_VPLL2;
650         } else {
651                 output_reg &= ~NV_RAMDAC_OUTPUT_SELECT_VPLL2;
652         }
653
654         if (nv_output->ramdac == 1) {
655                 other_ramdac = 0;
656         } else {
657                 other_ramdac = 1;
658         }
659
660         uint32_t output2_reg = nvReadRAMDAC(pNv, other_ramdac, NV_RAMDAC_OUTPUT);
661         
662         if (nv_crtc->head == 1) {
663                 output2_reg &= ~NV_RAMDAC_OUTPUT_SELECT_VPLL2;
664         } else {
665                 output2_reg |= NV_RAMDAC_OUTPUT_SELECT_VPLL2;
666         }
667
668         nvWriteRAMDAC(pNv, nv_output->ramdac, NV_RAMDAC_OUTPUT, output_reg);
669         nvWriteRAMDAC(pNv, other_ramdac, NV_RAMDAC_OUTPUT, output2_reg);
670 }
671
672 static void
673 nv_output_mode_set(xf86OutputPtr output, DisplayModePtr mode,
674                    DisplayModePtr adjusted_mode)
675 {
676     ScrnInfoPtr pScrn = output->scrn;
677     NVPtr pNv = NVPTR(pScrn);
678     RIVA_HW_STATE *state;
679
680         ErrorF("nv_output_mode_set is called\n");
681
682     state = &pNv->ModeReg;
683
684     nv_output_mode_set_regs(output, mode, adjusted_mode);
685     nv_output_load_state_ext(output, state, FALSE);
686         nv_output_mode_set_routing(output);
687 }
688
689 static xf86MonPtr
690 nv_get_edid(xf86OutputPtr output)
691 {
692         /* no use for shared DDC output */
693         NVOutputPrivatePtr nv_output = output->driver_private;
694         xf86MonPtr ddc_mon;
695
696         if (nv_output->pDDCBus == NULL)
697                 return NULL;
698
699         ddc_mon = xf86OutputGetEDID(output, nv_output->pDDCBus);
700         if (!ddc_mon)
701                 return NULL;
702
703         if (ddc_mon->features.input_type && (nv_output->type == OUTPUT_ANALOG))
704                 goto invalid;
705
706         if ((!ddc_mon->features.input_type) && (nv_output->type == OUTPUT_TMDS ||
707                                 nv_output->type == OUTPUT_LVDS))
708                 goto invalid;
709
710         return ddc_mon;
711
712 invalid:
713         xfree(ddc_mon);
714         return NULL;
715 }
716
717 static Bool
718 nv_ddc_detect(xf86OutputPtr output)
719 {
720         xf86MonPtr m = nv_get_edid(output);
721
722         if (m == NULL)
723                 return FALSE;
724
725         xfree(m);
726         return TRUE;
727 }
728
729 static Bool
730 nv_crt_load_detect(xf86OutputPtr output)
731 {
732         ScrnInfoPtr pScrn = output->scrn;
733         NVOutputPrivatePtr nv_output = output->driver_private;
734         NVPtr pNv = NVPTR(pScrn);
735         CARD32 reg_output, reg_test_ctrl, temp;
736         Bool present = FALSE;
737         int ramdac;
738
739         /* Usually these outputs are native to ramdac 1 */
740         if (nv_output->valid_ramdac & RAMDAC_0 && nv_output->valid_ramdac & RAMDAC_1) {
741                 ramdac = 1;
742         } else if (nv_output->valid_ramdac & RAMDAC_1) {
743                 ramdac = 1;
744         } else if (nv_output->valid_ramdac & RAMDAC_0) {
745                 ramdac = 0;
746         } else {
747                 return FALSE;
748         }
749
750         /* For some reason we get false positives on ramdac 1, maybe due tv-out? */
751         if (ramdac == 1) {
752                 return FALSE;
753         }
754
755         if (nv_output->pDDCBus != NULL) {
756                 xf86MonPtr ddc_mon = xf86OutputGetEDID(output, nv_output->pDDCBus);
757                 /* Is there a digital flatpanel on this channel? */
758                 if (ddc_mon && ddc_mon->features.input_type) {
759                         return FALSE;
760                 }
761         }
762
763         reg_output = nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_OUTPUT);
764         reg_test_ctrl = nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL);
765
766         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL, (reg_test_ctrl & ~0x00010000));
767
768         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_OUTPUT, (reg_output & 0x0000FEEE));
769         usleep(1000);
770
771         temp = nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_OUTPUT);
772         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_OUTPUT, temp | 1);
773
774         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_DATA, 0x94050140);
775         temp = nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL);
776         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL, temp | 0x1000);
777
778         usleep(1000);
779
780         present = (nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL) & (1 << 28)) ? TRUE : FALSE;
781
782         temp = NVOutputReadRAMDAC(output, NV_RAMDAC_TEST_CONTROL);
783         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL, temp & 0x000EFFF);
784
785         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_OUTPUT, reg_output);
786         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_TEST_CONTROL, reg_test_ctrl);
787
788         if (present) {
789                 ErrorF("A crt was detected on ramdac %d with no ddc support\n", ramdac);
790                 return TRUE;
791         }
792
793         return FALSE;
794 }
795
796 static xf86OutputStatus
797 nv_tmds_output_detect(xf86OutputPtr output)
798 {
799         ErrorF("nv_tmds_output_detect is called\n");
800
801         if (nv_ddc_detect(output))
802                 return XF86OutputStatusConnected;
803
804         return XF86OutputStatusDisconnected;
805 }
806
807
808 static xf86OutputStatus
809 nv_analog_output_detect(xf86OutputPtr output)
810 {
811         ErrorF("nv_analog_output_detect is called\n");
812
813         if (nv_ddc_detect(output))
814                 return XF86OutputStatusConnected;
815
816         if (nv_crt_load_detect(output))
817                 return XF86OutputStatusConnected;
818
819         return XF86OutputStatusDisconnected;
820 }
821
822 static DisplayModePtr
823 nv_output_get_modes(xf86OutputPtr output)
824 {
825         NVOutputPrivatePtr nv_output = output->driver_private;
826         xf86MonPtr ddc_mon;
827         DisplayModePtr ddc_modes;
828
829         ErrorF("nv_output_get_modes is called\n");
830
831         ddc_mon = nv_get_edid(output);
832
833         xf86OutputSetEDID(output, ddc_mon);
834
835         if (ddc_mon == NULL)
836                 return NULL;
837
838         ddc_modes = xf86OutputGetEDIDModes (output);
839
840         if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS) {
841                 int i;
842                 DisplayModePtr mode;
843
844                 for (i = 0; i < 4; i++) {
845                         /* We only look at detailed timings atm */
846                         if (ddc_mon->det_mon[i].type != DT)
847                                 continue;
848                         /* Selecting only based on width ok? */
849                         if (ddc_mon->det_mon[i].section.d_timings.h_active > nv_output->fpWidth) {
850                                 nv_output->fpWidth = ddc_mon->det_mon[i].section.d_timings.h_active;
851                                 nv_output->fpHeight = ddc_mon->det_mon[i].section.d_timings.v_active;
852                         }
853                 }
854
855                 /* Add a native resolution mode that is preferred */
856                 /* Reduced blanking should be fine on DVI monitor */
857                 nv_output->native_mode = xf86CVTMode(nv_output->fpWidth, nv_output->fpHeight, 60.0, TRUE, FALSE);
858                 nv_output->native_mode->type = M_T_DRIVER | M_T_PREFERRED;
859                 /* We want the new mode to be preferred */
860                 for (mode = ddc_modes; mode != NULL; mode = mode->next) {
861                         if (mode->type & M_T_PREFERRED) {
862                                 mode->type &= ~M_T_PREFERRED;
863                         }
864                 }
865                 ddc_modes = xf86ModesAdd(ddc_modes, nv_output->native_mode);
866         }
867
868         return ddc_modes;
869 }
870
871 static void
872 nv_output_destroy (xf86OutputPtr output)
873 {
874         ErrorF("nv_output_destroy is called\n");
875         if (output->driver_private)
876                 xfree (output->driver_private);
877 }
878
879 static void
880 nv_clear_ramdac_from_outputs(xf86OutputPtr output, int ramdac)
881 {
882         int i;
883         ScrnInfoPtr pScrn = output->scrn;
884         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
885         xf86OutputPtr output2;
886         NVOutputPrivatePtr nv_output2;
887         for (i = 0; i < xf86_config->num_output; i++) {
888                 output2 = xf86_config->output[i];
889                 nv_output2 = output2->driver_private;
890                 if (nv_output2->ramdac == ramdac && output != output2) {
891                         nv_output2->ramdac = -1;
892                         nv_output2->ramdac_assigned = FALSE;
893                         break;
894                 }
895         }
896 }
897
898 static void
899 nv_output_prepare(xf86OutputPtr output)
900 {
901         ErrorF("nv_output_prepare is called\n");
902         NVOutputPrivatePtr nv_output = output->driver_private;
903         ScrnInfoPtr     pScrn = output->scrn;
904         NVPtr pNv = NVPTR(pScrn);
905         Bool stole_ramdac = FALSE;
906         xf86OutputPtr output2 = NULL;
907
908         output->funcs->dpms(output, DPMSModeOff);
909
910         if (nv_output->ramdac_assigned) {
911                 ErrorF("We already have a ramdac.\n");
912                 return;
913         }
914
915         /* We need ramdac 0, so let's steal it */
916         if (!(nv_output->valid_ramdac & RAMDAC_1) && pNv->ramdac_active[0]) {
917                 ErrorF("Stealing ramdac0 ;-)\n");
918                 int i;
919                 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
920                 NVOutputPrivatePtr nv_output2;
921                 for (i = 0; i < xf86_config->num_output; i++) {
922                         output2 = xf86_config->output[i];
923                         nv_output2 = output2->driver_private;
924                         if (nv_output2->ramdac == 0 && output != output2) {
925                                 nv_output2->ramdac = -1;
926                                 nv_output2->ramdac_assigned = FALSE;
927                                 break;
928                         }
929                 }
930                 pNv->ramdac_active[0] = FALSE;
931                 stole_ramdac = TRUE;
932         }
933
934         /* We need ramdac 1, so let's steal it */
935         if (!(nv_output->valid_ramdac & RAMDAC_0) && pNv->ramdac_active[1]) {
936                 ErrorF("Stealing ramdac1 ;-)\n");
937                 int i;
938                 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
939                 NVOutputPrivatePtr nv_output2;
940                 for (i = 0; i < xf86_config->num_output; i++) {
941                         output2 = xf86_config->output[i];
942                         nv_output2 = output2->driver_private;
943                         if (nv_output2->ramdac == 1 && output != output2) {
944                                 nv_output2->ramdac = -1;
945                                 nv_output2->ramdac_assigned = FALSE;
946                                 break;
947                         }
948                 }
949                 pNv->ramdac_active[1] = FALSE;
950                 stole_ramdac = TRUE;
951         }
952
953         /* TODO: figure out what ramdac 2 is and how it is identified */
954
955         /* At this point we already stole ramdac 0 or 1 if we need it */
956         if (!pNv->ramdac_active[0] && (nv_output->valid_ramdac & RAMDAC_0)) {
957                 ErrorF("Activating ramdac %d\n", 0);
958                 pNv->ramdac_active[0] = TRUE;
959                 nv_output->ramdac = 0;
960         } else {
961                 ErrorF("Activating ramdac %d\n", 1);
962                 pNv->ramdac_active[1] = TRUE;
963                 nv_output->ramdac = 1;
964         }
965
966         if (nv_output->ramdac != -1) {
967                 nv_output->ramdac_assigned = TRUE;
968                 nv_clear_ramdac_from_outputs(output, nv_output->ramdac);
969         }
970
971         if (stole_ramdac) {
972                 ErrorF("Resetting the stolen ramdac\n");
973                 output2->funcs->prepare(output2);
974                 output2->funcs->mode_set(output2, &(output2->crtc->desiredMode), &(output2->crtc->desiredMode));
975         }
976 }
977
978 static void
979 nv_output_commit(xf86OutputPtr output)
980 {
981         ErrorF("nv_output_commit is called\n");
982
983         output->funcs->dpms(output, DPMSModeOn);
984 }
985
986 static const xf86OutputFuncsRec nv_analog_output_funcs = {
987     .dpms = nv_analog_output_dpms,
988     .save = nv_output_save,
989     .restore = nv_output_restore,
990     .mode_valid = nv_output_mode_valid,
991     .mode_fixup = nv_output_mode_fixup,
992     .mode_set = nv_output_mode_set,
993     .detect = nv_analog_output_detect,
994     .get_modes = nv_output_get_modes,
995     .destroy = nv_output_destroy,
996     .prepare = nv_output_prepare,
997     .commit = nv_output_commit,
998 };
999
1000 static const xf86OutputFuncsRec nv_tmds_output_funcs = {
1001     .dpms = nv_tmds_output_dpms,
1002     .save = nv_output_save,
1003     .restore = nv_output_restore,
1004     .mode_valid = nv_output_mode_valid,
1005     .mode_fixup = nv_output_mode_fixup,
1006     .mode_set = nv_output_mode_set,
1007     .detect = nv_tmds_output_detect,
1008     .get_modes = nv_output_get_modes,
1009     .destroy = nv_output_destroy,
1010     .prepare = nv_output_prepare,
1011     .commit = nv_output_commit,
1012 };
1013
1014 static int nv_lvds_output_mode_valid
1015 (xf86OutputPtr output, DisplayModePtr pMode)
1016 {
1017         NVOutputPrivatePtr nv_output = output->driver_private;
1018
1019         /* No modes > panel's native res */
1020         if (pMode->HDisplay > nv_output->fpWidth || pMode->VDisplay > nv_output->fpHeight)
1021                 return MODE_PANEL;
1022
1023         return nv_output_mode_valid(output, pMode);
1024 }
1025
1026 static xf86OutputStatus
1027 nv_lvds_output_detect(xf86OutputPtr output)
1028 {
1029         ScrnInfoPtr pScrn = output->scrn;
1030         NVPtr pNv = NVPTR(pScrn);
1031
1032         if (pNv->fp_native_mode || nv_ddc_detect(output))
1033                 return XF86OutputStatusConnected;
1034
1035         return XF86OutputStatusDisconnected;
1036 }
1037
1038 static DisplayModePtr
1039 nv_lvds_output_get_modes(xf86OutputPtr output)
1040 {
1041         ScrnInfoPtr pScrn = output->scrn;
1042         NVPtr pNv = NVPTR(pScrn);
1043         NVOutputPrivatePtr nv_output = output->driver_private;
1044         DisplayModePtr modes;
1045
1046         if ((modes = nv_output_get_modes(output)))
1047                 return modes;
1048
1049         /* it is possible to set up a mode from what we can read from the
1050          * RAMDAC registers, but if we can't read the BIOS table correctly
1051          * we might as well give up */
1052         if (pNv->fp_native_mode == NULL)
1053                 return NULL;
1054
1055         nv_output->fpWidth = NVOutputReadRAMDAC(output, NV_RAMDAC_FP_HDISP_END) + 1;
1056         nv_output->fpHeight = NVOutputReadRAMDAC(output, NV_RAMDAC_FP_VDISP_END) + 1;
1057         nv_output->fpSyncs = NVOutputReadRAMDAC(output, NV_RAMDAC_FP_CONTROL) & 0x30000033;
1058
1059         if (pNv->fp_native_mode->HDisplay != nv_output->fpWidth ||
1060                 pNv->fp_native_mode->VDisplay != nv_output->fpHeight) {
1061                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1062                         "Panel size mismatch; ignoring RAMDAC\n");
1063                 nv_output->fpWidth = pNv->fp_native_mode->HDisplay;
1064                 nv_output->fpHeight = pNv->fp_native_mode->VDisplay;
1065         }
1066
1067         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Panel size is %u x %u\n",
1068                 nv_output->fpWidth, nv_output->fpHeight);
1069
1070         nv_output->native_mode = xf86DuplicateMode(pNv->fp_native_mode);
1071
1072         return xf86DuplicateMode(pNv->fp_native_mode);
1073 }
1074
1075 static const xf86OutputFuncsRec nv_lvds_output_funcs = {
1076         .dpms = nv_lvds_output_dpms,
1077         .save = nv_output_save,
1078         .restore = nv_output_restore,
1079         .mode_valid = nv_lvds_output_mode_valid,
1080         .mode_fixup = nv_output_mode_fixup,
1081         .mode_set = nv_output_mode_set,
1082         .detect = nv_lvds_output_detect,
1083         .get_modes = nv_lvds_output_get_modes,
1084         .destroy = nv_output_destroy,
1085         .prepare = nv_output_prepare,
1086         .commit = nv_output_commit,
1087 };
1088
1089 static void nv_add_analog_output(ScrnInfoPtr pScrn, int heads, int order, int i2c_index, Bool dvi_pair)
1090 {
1091         NVPtr pNv = NVPTR(pScrn);
1092         xf86OutputPtr       output;
1093         NVOutputPrivatePtr    nv_output;
1094         char outputname[20];
1095         Bool create_output = TRUE;
1096
1097         /* DVI have an analog connector and a digital one, differentiate between that and a normal vga */
1098         if (dvi_pair) {
1099                 sprintf(outputname, "DVI-A-%d", pNv->dvi_a_count);
1100                 pNv->dvi_a_count++;
1101         } else {
1102                 sprintf(outputname, "VGA-%d", pNv->vga_count);
1103                 pNv->vga_count++;
1104         }
1105
1106         nv_output = xnfcalloc (sizeof (NVOutputPrivateRec), 1);
1107         if (!nv_output) {
1108                 return;
1109         }
1110
1111         if (pNv->dcb_table.i2c_read[i2c_index] && pNv->pI2CBus[i2c_index] == NULL)
1112                 NV_I2CInit(pScrn, &pNv->pI2CBus[i2c_index], pNv->dcb_table.i2c_read[i2c_index], xstrdup(outputname));
1113
1114         nv_output->type = OUTPUT_ANALOG;
1115
1116         /* order:
1117          * bit0: RAMDAC_0 valid
1118          * bit1: RAMDAC_1 valid
1119          * So lowest order has highest priority.
1120          * Below is guesswork:
1121          * bit2: All ramdac's valid?
1122          * FIXME: this probably wrong
1123          */
1124         nv_output->valid_ramdac = order;
1125
1126         if (!create_output) {
1127                 xfree(nv_output);
1128                 return;
1129         }
1130
1131         /* Delay creation of output until we actually know we want it */
1132         output = xf86OutputCreate (pScrn, &nv_analog_output_funcs, outputname);
1133         if (!output)
1134                 return;
1135
1136         output->driver_private = nv_output;
1137
1138         nv_output->pDDCBus = pNv->pI2CBus[i2c_index];
1139
1140         nv_output->ramdac = -1;
1141
1142         /* This is only to facilitate proper output routing for dvi */
1143         /* See sel_clk assignment in nv_crtc.c */
1144         if (order & RAMDAC_1) {
1145                 nv_output->preferred_crtc = 1;
1146         } else {
1147                 nv_output->preferred_crtc = 0;
1148         }
1149
1150         output->possible_crtcs = heads;
1151         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Adding output %s\n", outputname);
1152 }
1153
1154 static void nv_add_digital_output(ScrnInfoPtr pScrn, int heads, int order, int i2c_index, int lvds)
1155 {
1156         NVPtr pNv = NVPTR(pScrn);
1157         xf86OutputPtr       output;
1158         NVOutputPrivatePtr    nv_output;
1159         char outputname[20];
1160         Bool create_output = TRUE;
1161
1162         if (lvds) {
1163                 sprintf(outputname, "LVDS-%d", pNv->lvds_count);
1164                 pNv->lvds_count++;
1165         } else {
1166                 sprintf(outputname, "DVI-D-%d", pNv->dvi_d_count);
1167                 pNv->dvi_d_count++;
1168         }
1169
1170         nv_output = xnfcalloc (sizeof (NVOutputPrivateRec), 1);
1171
1172         if (!nv_output) {
1173                 return;
1174         }
1175
1176         if (pNv->dcb_table.i2c_read[i2c_index] && pNv->pI2CBus[i2c_index] == NULL)
1177                 NV_I2CInit(pScrn, &pNv->pI2CBus[i2c_index], pNv->dcb_table.i2c_read[i2c_index], xstrdup(outputname));
1178
1179         nv_output->pDDCBus = pNv->pI2CBus[i2c_index];
1180
1181         /* order:
1182          * bit0: RAMDAC_0 valid
1183          * bit1: RAMDAC_1 valid
1184          * So lowest order has highest priority.
1185          * Below is guesswork:
1186          * bit2: All ramdac's valid?
1187          * FIXME: this probably wrong
1188          */
1189         nv_output->valid_ramdac = order;
1190
1191         if (lvds) {
1192                 nv_output->type = OUTPUT_LVDS;
1193                 /* comment below two lines to test LVDS under RandR12.
1194                  * If your screen "blooms" or "bleeds" (i.e. has a developing
1195                  * white / psychedelic pattern) then KILL X IMMEDIATELY
1196                  * (ctrl+alt+backspace) & if the effect continues reset power */
1197                 ErrorF("Output refused because we don't accept LVDS at the moment.\n");
1198                 create_output = FALSE;
1199         } else {
1200                 nv_output->type = OUTPUT_TMDS;
1201         }
1202
1203         if (!create_output) {
1204                 xfree(nv_output);
1205                 return;
1206         }
1207
1208         /* Delay creation of output until we are certain is desirable */
1209         if (lvds)
1210                 output = xf86OutputCreate (pScrn, &nv_lvds_output_funcs, outputname);
1211         else
1212                 output = xf86OutputCreate (pScrn, &nv_tmds_output_funcs, outputname);
1213         if (!output)
1214                 return;
1215
1216         output->driver_private = nv_output;
1217
1218         nv_output->ramdac = -1;
1219
1220         /* This is a theory: */
1221         /* DVI outputs are in no way bound to ramdac's, but exist purely on a crtc level */
1222         /* Don't ask why this relation seems valid, ask those weirdos at nvidia */
1223         if (order & RAMDAC_1) {
1224                 nv_output->preferred_crtc = 1;
1225         } else {
1226                 nv_output->preferred_crtc = 0;
1227         }
1228
1229         output->possible_crtcs = heads;
1230         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Adding output %s\n", outputname);
1231 }
1232
1233 void NvDCBSetupOutputs(ScrnInfoPtr pScrn)
1234 {
1235         unsigned char type, i2c_index, or, heads, bus;
1236         NVPtr pNv = NVPTR(pScrn);
1237         int i, bus_count[0xf];
1238
1239         memset(bus_count, 0, sizeof(bus_count));
1240         for (i = 0 ; i < pNv->dcb_table.entries; i++)
1241                 bus_count[pNv->dcb_table.entry[i].bus]++;
1242
1243         /* we setup the outputs up from the BIOS table */
1244         for (i = 0 ; i < pNv->dcb_table.entries; i++) {
1245                 type = pNv->dcb_table.entry[i].type;
1246                 i2c_index = pNv->dcb_table.entry[i].i2c_index;
1247                 or = ffs(pNv->dcb_table.entry[i].or);
1248                 heads = pNv->dcb_table.entry[i].head;
1249                 bus = pNv->dcb_table.entry[i].bus;
1250
1251                 if (type > 3) {
1252                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "DCB type %d not known\n", type);
1253                         continue;
1254                 }
1255
1256                 xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "DCB entry %d: type: %d, i2c_index: %d, head: %d, bus: %d, or: %d\n", i, type, i2c_index, heads, bus, or);
1257
1258                 switch(type) {
1259                 case OUTPUT_ANALOG:
1260                         nv_add_analog_output(pScrn, heads, or, i2c_index, (bus_count[bus] > 1));
1261                         break;
1262                 case OUTPUT_TMDS:
1263                         nv_add_digital_output(pScrn, heads, or, i2c_index, 0);
1264                         break;
1265                 case OUTPUT_LVDS:
1266                         nv_add_digital_output(pScrn, heads, or, i2c_index, 1);
1267                         break;
1268                 default:
1269                         break;
1270                 }
1271         }
1272 }
1273
1274 void NvSetupOutputs(ScrnInfoPtr pScrn)
1275 {
1276         NVPtr pNv = NVPTR(pScrn);
1277
1278         pNv->Television = FALSE;
1279
1280         memset(pNv->pI2CBus, 0, sizeof(pNv->pI2CBus));
1281         NvDCBSetupOutputs(pScrn);
1282 }
1283
1284 /*************************************************************************** \
1285 |*                                                                           *|
1286 |*       Copyright 1993-2003 NVIDIA, Corporation.  All rights reserved.      *|
1287 |*                                                                           *|
1288 |*     NOTICE TO USER:   The source code  is copyrighted under  U.S. and     *|
1289 |*     international laws.  Users and possessors of this source code are     *|
1290 |*     hereby granted a nonexclusive,  royalty-free copyright license to     *|
1291 |*     use this code in individual and commercial software.                  *|
1292 |*                                                                           *|
1293 |*     Any use of this source code must include,  in the user documenta-     *|
1294 |*     tion and  internal comments to the code,  notices to the end user     *|
1295 |*     as follows:                                                           *|
1296 |*                                                                           *|
1297 |*       Copyright 1993-1999 NVIDIA, Corporation.  All rights reserved.      *|
1298 |*                                                                           *|
1299 |*     NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY     *|
1300 |*     OF  THIS SOURCE  CODE  FOR ANY PURPOSE.  IT IS  PROVIDED  "AS IS"     *|
1301 |*     WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  NVIDIA, CORPOR-     *|
1302 |*     ATION DISCLAIMS ALL WARRANTIES  WITH REGARD  TO THIS SOURCE CODE,     *|
1303 |*     INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE-     *|
1304 |*     MENT,  AND FITNESS  FOR A PARTICULAR PURPOSE.   IN NO EVENT SHALL     *|
1305 |*     NVIDIA, CORPORATION  BE LIABLE FOR ANY SPECIAL,  INDIRECT,  INCI-     *|
1306 |*     DENTAL, OR CONSEQUENTIAL DAMAGES,  OR ANY DAMAGES  WHATSOEVER RE-     *|
1307 |*     SULTING FROM LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION     *|
1308 |*     OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF     *|
1309 |*     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE.     *|
1310 |*                                                                           *|
1311 |*     U.S. Government  End  Users.   This source code  is a "commercial     *|
1312 |*     item,"  as that  term is  defined at  48 C.F.R. 2.101 (OCT 1995),     *|
1313 |*     consisting  of "commercial  computer  software"  and  "commercial     *|
1314 |*     computer  software  documentation,"  as such  terms  are  used in     *|
1315 |*     48 C.F.R. 12.212 (SEPT 1995)  and is provided to the U.S. Govern-     *|
1316 |*     ment only as  a commercial end item.   Consistent with  48 C.F.R.     *|
1317 |*     12.212 and  48 C.F.R. 227.7202-1 through  227.7202-4 (JUNE 1995),     *|
1318 |*     all U.S. Government End Users  acquire the source code  with only     *|
1319 |*     those rights set forth herein.                                        *|
1320 |*                                                                           *|
1321  \***************************************************************************/