Assorted small bios/randr12 bits and pieces
[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 #include "X11/Xatom.h"
45
46 #include "xf86Crtc.h"
47 #include "nv_include.h"
48
49 const char *OutputType[] = {
50     "None",
51     "VGA",
52     "DVI",
53     "LVDS",
54     "S-video",
55     "Composite",
56 };
57
58 const char *MonTypeName[7] = {
59     "AUTO",
60     "NONE",
61     "CRT",
62     "LVDS",
63     "TMDS",
64     "CTV",
65     "STV"
66 };
67
68 /* 
69  * TMDS registers are indirect 8 bit registers.
70  * Reading is straightforward, writing a bit odd.
71  * Reading: Write adress (+write protect bit, do not forget this), then read value.
72  * Writing: Write adress (+write protect bit), write value, write adress again and write it again (+write protect bit).
73  */
74
75 void NVWriteTMDS(NVPtr pNv, int ramdac, uint32_t tmds_reg, uint32_t val)
76 {
77         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, 
78                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
79
80         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_DATA, val & 0xff);
81
82         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, tmds_reg & 0xff);
83         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, 
84                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
85 }
86
87 uint8_t NVReadTMDS(NVPtr pNv, int ramdac, uint32_t tmds_reg)
88 {
89         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL, 
90                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
91
92         return (nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_DATA) & 0xff);
93 }
94
95 /* Two register sets exist, this one is only used for dual link dvi/lvds */
96
97 void NVWriteTMDS2(NVPtr pNv, int ramdac, uint32_t tmds_reg, uint32_t val)
98 {
99         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL_2, 
100                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_2_WRITE_DISABLE);
101
102         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_DATA_2, val & 0xff);
103
104         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL_2, tmds_reg & 0xff);
105         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL_2, 
106                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_2_WRITE_DISABLE);
107 }
108
109 uint8_t NVReadTMDS2(NVPtr pNv, int ramdac, uint32_t tmds_reg)
110 {
111         nvWriteRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_CONTROL_2, 
112                 (tmds_reg & 0xff) | NV_RAMDAC_FP_TMDS_CONTROL_2_WRITE_DISABLE);
113
114         return (nvReadRAMDAC(pNv, ramdac, NV_RAMDAC_FP_TMDS_DATA_2) & 0xff);
115 }
116
117 void NVOutputWriteTMDS(xf86OutputPtr output, uint32_t tmds_reg, uint32_t val)
118 {
119         NVOutputPrivatePtr nv_output = output->driver_private;
120         ScrnInfoPtr     pScrn = output->scrn;
121         NVPtr pNv = NVPTR(pScrn);
122
123         /* We must write to the "bus" of the output */
124         NVWriteTMDS(pNv, nv_output->preferred_output, tmds_reg, val);
125 }
126
127 uint8_t NVOutputReadTMDS(xf86OutputPtr output, uint32_t tmds_reg)
128 {
129         NVOutputPrivatePtr nv_output = output->driver_private;
130         ScrnInfoPtr     pScrn = output->scrn;
131         NVPtr pNv = NVPTR(pScrn);
132
133         /* We must read from the "bus" of the output */
134         return NVReadTMDS(pNv, nv_output->preferred_output, tmds_reg);
135 }
136
137 void NVOutputWriteTMDS2(xf86OutputPtr output, uint32_t tmds_reg, uint32_t val)
138 {
139         NVOutputPrivatePtr nv_output = output->driver_private;
140         ScrnInfoPtr     pScrn = output->scrn;
141         NVPtr pNv = NVPTR(pScrn);
142
143         /* We must write to the "bus" of the output */
144         NVWriteTMDS2(pNv, nv_output->preferred_output, tmds_reg, val);
145 }
146
147 uint8_t NVOutputReadTMDS2(xf86OutputPtr output, uint32_t tmds_reg)
148 {
149         NVOutputPrivatePtr nv_output = output->driver_private;
150         ScrnInfoPtr     pScrn = output->scrn;
151         NVPtr pNv = NVPTR(pScrn);
152
153         /* We must read from the "bus" of the output */
154         return NVReadTMDS2(pNv, nv_output->preferred_output, tmds_reg);
155 }
156
157 /* These functions now write into the output, instead of a specific ramdac */
158
159 void NVOutputWriteRAMDAC(xf86OutputPtr output, uint32_t ramdac_reg, uint32_t val)
160 {
161     NVOutputPrivatePtr nv_output = output->driver_private;
162     ScrnInfoPtr pScrn = output->scrn;
163     NVPtr pNv = NVPTR(pScrn);
164
165     nvWriteRAMDAC(pNv, nv_output->preferred_output, ramdac_reg, val);
166 }
167
168 uint32_t NVOutputReadRAMDAC(xf86OutputPtr output, uint32_t ramdac_reg)
169 {
170     NVOutputPrivatePtr nv_output = output->driver_private;
171     ScrnInfoPtr pScrn = output->scrn;
172     NVPtr pNv = NVPTR(pScrn);
173
174     return nvReadRAMDAC(pNv, nv_output->preferred_output, ramdac_reg);
175 }
176
177 static void dpms_update_output_ramdac(xf86OutputPtr output, int mode)
178 {
179         NVOutputPrivatePtr nv_output = output->driver_private;
180         ScrnInfoPtr pScrn = output->scrn;
181         NVPtr pNv = NVPTR(pScrn);
182         xf86CrtcPtr crtc = output->crtc;
183         if (!crtc)      /* we need nv_crtc, so give up */
184                 return;
185         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
186
187         /* We may be going for modesetting, so we must reset our output binding */
188         if (mode == DPMSModeOff) {
189                 NVWriteVGACR5758(pNv, nv_crtc->head, 0, 0x7f);
190                 NVWriteVGACR5758(pNv, nv_crtc->head, 2, 0);
191                 return;
192         }
193
194         /* The previous call was not a modeset, but a normal dpms call */
195         NVWriteVGACR5758(pNv, nv_crtc->head, 0, pNv->dcb_table.entry[nv_output->dcb_entry].type);
196         NVWriteVGACR5758(pNv, nv_crtc->head, 2, pNv->dcb_table.entry[nv_output->dcb_entry].or);
197 }
198
199 static void
200 nv_lvds_output_dpms(xf86OutputPtr output, int mode)
201 {
202         NVOutputPrivatePtr nv_output = output->driver_private;
203
204         ErrorF("nv_lvds_output_dpms is called with mode %d\n", mode);
205
206         if (nv_output->last_dpms == mode) /* Don't do unnecesary mode changes. */
207                 return;
208
209         nv_output->last_dpms = mode;
210
211         NVPtr pNv = NVPTR(output->scrn);
212         xf86CrtcPtr crtc = output->crtc;
213         if (!crtc)      /* we need nv_crtc, so give up */
214                 return;
215         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
216         int pclk = 0;
217
218         if (pNv->NVArch >= 0x17 && pNv->twoHeads)
219                 dpms_update_output_ramdac(output, mode);
220
221         if (!pNv->dcb_table.entry[nv_output->dcb_entry].lvdsconf.use_power_scripts)
222                 return;
223
224         /* only need to pass in pclk for BIT bioses */
225         if (pNv->VBIOS.major_version > 4)
226                 pclk = nv_calc_tmds_clock_from_pll(output);
227
228         switch (mode) {
229         case DPMSModeStandby:
230         case DPMSModeSuspend:
231                 call_lvds_script(output->scrn, nv_crtc->head, nv_output->dcb_entry, LVDS_BACKLIGHT_OFF, pclk);
232                 break;
233         case DPMSModeOff:
234                 call_lvds_script(output->scrn, nv_crtc->head, nv_output->dcb_entry, LVDS_PANEL_OFF, pclk);
235                 break;
236         case DPMSModeOn:
237                 call_lvds_script(output->scrn, nv_crtc->head, nv_output->dcb_entry, LVDS_PANEL_ON, pclk);
238         default:
239                 break;
240         }
241 }
242
243 static void
244 nv_analog_output_dpms(xf86OutputPtr output, int mode)
245 {
246         NVOutputPrivatePtr nv_output = output->driver_private;
247         ScrnInfoPtr pScrn = output->scrn;
248         NVPtr pNv = NVPTR(pScrn);
249
250         ErrorF("nv_analog_output_dpms is called with mode %d\n", mode);
251
252         if (nv_output->last_dpms == mode) /* Don't do unnecesary mode changes. */
253                 return;
254
255         nv_output->last_dpms = mode;
256
257         if (pNv->NVArch >= 0x17 && pNv->twoHeads)
258                 dpms_update_output_ramdac(output, mode);
259 }
260
261 static void
262 nv_tmds_output_dpms(xf86OutputPtr output, int mode)
263 {
264         NVOutputPrivatePtr nv_output = output->driver_private;
265
266         ErrorF("nv_tmds_output_dpms is called with mode %d\n", mode);
267
268         if (nv_output->last_dpms == mode) /* Don't do unnecesary mode changes. */
269                 return;
270
271         nv_output->last_dpms = mode;
272
273         xf86CrtcPtr crtc = output->crtc;
274         NVPtr pNv = NVPTR(output->scrn);
275
276         if (pNv->NVArch >= 0x17 && pNv->twoHeads)
277                 dpms_update_output_ramdac(output, mode);
278
279         /* Are we assigned a ramdac already?, else we will be activated during mode set */
280         if (crtc) {
281                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
282
283                 ErrorF("nv_tmds_output_dpms is called for CRTC %d with mode %d\n", nv_crtc->head, mode);
284
285                 uint32_t fpcontrol = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_CONTROL);
286                 switch(mode) {
287                         case DPMSModeStandby:
288                         case DPMSModeSuspend:
289                         case DPMSModeOff:
290                                 /* cut the TMDS output */           
291                                 fpcontrol |= 0x20000022;
292                                 break;
293                         case DPMSModeOn:
294                                 /* disable cutting the TMDS output */
295                                 fpcontrol &= ~0x20000022;
296                                 break;
297                 }
298                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_CONTROL, fpcontrol);
299         }
300 }
301
302 void nv_output_save_state_ext(xf86OutputPtr output, RIVA_HW_STATE *state)
303 {
304         NVOutputPrivatePtr nv_output = output->driver_private;
305         NVOutputRegPtr regp;
306         int i;
307
308         regp = &state->dac_reg[nv_output->output_resource];
309
310         regp->output = NVOutputReadRAMDAC(output, NV_RAMDAC_OUTPUT);
311
312         /* NV11's don't seem to like this, so let's restrict it to digital outputs only. */
313         if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS) {
314                 /* Store the registers in case we need them again for something (like data for VT restore) */
315                 for (i = 0; i < 0xFF; i++) {
316                         regp->TMDS[i] = NVOutputReadTMDS(output, i);
317                 }
318
319                 for (i = 0; i < 0xFF; i++) {
320                         regp->TMDS2[i] = NVOutputReadTMDS2(output, i);
321                 }
322         }
323 }
324
325 void nv_output_load_state_ext(xf86OutputPtr output, RIVA_HW_STATE *state, Bool override)
326 {
327         NVOutputPrivatePtr nv_output = output->driver_private;
328         NVOutputRegPtr regp;
329
330         regp = &state->dac_reg[nv_output->output_resource];
331
332         /* This exists purely for proper text mode restore */
333         if (override) NVOutputWriteRAMDAC(output, NV_RAMDAC_OUTPUT, regp->output);
334 }
335
336 /* NOTE: Don't rely on this data for anything other than restoring VT's */
337
338 static void
339 nv_output_save (xf86OutputPtr output)
340 {
341         ScrnInfoPtr     pScrn = output->scrn;
342         NVPtr pNv = NVPTR(pScrn);
343         RIVA_HW_STATE *state;
344
345         ErrorF("nv_output_save is called\n");
346         state = &pNv->SavedReg;
347
348         /* Due to strange mapping of outputs we could have swapped analog and digital */
349         /* So we force save all the registers */
350         nv_output_save_state_ext(output, state);
351 }
352
353 uint32_t nv_calc_tmds_clock_from_pll(xf86OutputPtr output)
354 {
355         ScrnInfoPtr pScrn = output->scrn;
356         NVPtr pNv = NVPTR(pScrn);
357         RIVA_HW_STATE *state;
358         NVOutputRegPtr regp;
359         NVOutputPrivatePtr nv_output = output->driver_private;
360
361         state = &pNv->SavedReg;
362         /* Registers are stored by their preferred ramdac */
363         /* So or = 3 still means it uses the "ramdac0" regs. */
364         regp = &state->dac_reg[nv_output->preferred_output];
365
366         /* Only do it once for a dvi-d/dvi-a pair */
367         Bool swapped_clock = FALSE;
368         Bool vpllb_disabled = FALSE;
369         /* Bit3 swaps crtc (clocks are bound to crtc) and output */
370         if (regp->TMDS[0x4] & (1 << 3)) {
371                 swapped_clock = TRUE;
372         }
373
374         uint8_t vpll_num = swapped_clock ^ nv_output->preferred_output;
375
376         uint32_t vplla = vpll_num ? state->vpll2_a : state->vpll1_a;
377         uint32_t vpllb = vpll_num ? state->vpll2_b : state->vpll1_b;
378
379         if (!pNv->twoStagePLL)
380                 vpllb_disabled = TRUE;
381
382         /* This is the dummy value nvidia sets when vpll is disabled */
383         if ((vpllb & 0xFFFF) == 0x11F)
384                 vpllb_disabled = TRUE;
385
386         uint8_t m1, m2, n1, n2, p;
387
388         m1 = vplla & 0xFF;
389         n1 = (vplla >> 8) & 0xFF;
390         p = (vplla >> 16) & 0x7;
391
392         if (vpllb_disabled) {
393                 m2 = 1;
394                 n2 = 1;
395         } else {
396                 m2 = vpllb & 0xFF;
397                 n2 = (vpllb >> 8) & 0xFF;
398         }
399
400         uint32_t clock = ((pNv->CrystalFreqKHz * n1 * n2)/(m1 * m2)) >> p;
401         ErrorF("The original bios clock seems to have been %d kHz\n", clock);
402         return clock;
403 }
404
405 void nv_set_tmds_registers(xf86OutputPtr output, uint32_t clock, Bool override, Bool crosswired)
406 {
407         ScrnInfoPtr pScrn = output->scrn;
408         NVOutputPrivatePtr nv_output = output->driver_private;
409         xf86CrtcPtr crtc = output->crtc;
410         /* We have no crtc, so what are we supposed to do now? */
411         /* This can only happen during VT restore */
412         if (crtc && !override) {
413                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
414                 /*
415                  * Resetting all registers is a bad idea, it seems to work fine without it.
416                  */
417                 if (nv_output->type == OUTPUT_TMDS)
418                         run_tmds_table(pScrn, nv_output->dcb_entry, nv_crtc->head, clock/10);
419                 else
420                         call_lvds_script(pScrn, nv_crtc->head, nv_output->dcb_entry, LVDS_RESET, clock / 10);
421         } else {
422                 /*
423                  * We have no crtc, but we do know what output we are and if we were crosswired.
424                  * We can determine our crtc from this.
425                  */
426                 if (nv_output->type == OUTPUT_TMDS)
427                         run_tmds_table(pScrn, nv_output->dcb_entry, nv_output->preferred_output ^ crosswired, clock/10);
428                 else {
429                         call_lvds_script(pScrn, nv_output->preferred_output ^ crosswired, nv_output->dcb_entry, LVDS_RESET, clock / 10);
430                         call_lvds_script(pScrn, nv_output->preferred_output ^ crosswired, nv_output->dcb_entry, LVDS_PANEL_ON, clock / 10);
431                 }
432         }
433 }
434
435 static void
436 nv_output_restore (xf86OutputPtr output)
437 {
438         ScrnInfoPtr pScrn = output->scrn;
439         NVPtr pNv = NVPTR(pScrn);
440         RIVA_HW_STATE *state;
441         NVOutputPrivatePtr nv_output = output->driver_private;
442         ErrorF("nv_output_restore is called\n");
443
444         state = &pNv->SavedReg;
445         /* Select the default output resource for consistent restore. */
446         if (ffs(pNv->dcb_table.entry[nv_output->dcb_entry].or) & OUTPUT_1) {
447                 nv_output->output_resource = 1;
448         } else {
449                 nv_output->output_resource = 0;
450         }
451
452         /* Due to strange mapping of outputs we could have swapped analog and digital */
453         /* So we force load all the registers */
454         nv_output_load_state_ext(output, state, TRUE);
455
456         nv_output->last_dpms = NV_DPMS_CLEARED;
457 }
458
459 static int
460 nv_output_mode_valid(xf86OutputPtr output, DisplayModePtr pMode)
461 {
462         if (pMode->Flags & V_DBLSCAN)
463                 return MODE_NO_DBLESCAN;
464
465         if (pMode->Clock > 400000 || pMode->Clock < 25000)
466                 return MODE_CLOCK_RANGE;
467
468         return MODE_OK;
469 }
470
471
472 static Bool
473 nv_output_mode_fixup(xf86OutputPtr output, DisplayModePtr mode,
474                      DisplayModePtr adjusted_mode)
475 {
476         NVOutputPrivatePtr nv_output = output->driver_private;
477         ErrorF("nv_output_mode_fixup is called\n");
478
479         /* For internal panels and gpu scaling on DVI we need the native mode */
480         if ((nv_output->type == OUTPUT_LVDS || (nv_output->type == OUTPUT_TMDS && nv_output->scaling_mode != SCALE_PANEL))) {
481                 adjusted_mode->HDisplay = nv_output->native_mode->HDisplay;
482                 adjusted_mode->HSkew = nv_output->native_mode->HSkew;
483                 adjusted_mode->HSyncStart = nv_output->native_mode->HSyncStart;
484                 adjusted_mode->HSyncEnd = nv_output->native_mode->HSyncEnd;
485                 adjusted_mode->HTotal = nv_output->native_mode->HTotal;
486                 adjusted_mode->VDisplay = nv_output->native_mode->VDisplay;
487                 adjusted_mode->VScan = nv_output->native_mode->VScan;
488                 adjusted_mode->VSyncStart = nv_output->native_mode->VSyncStart;
489                 adjusted_mode->VSyncEnd = nv_output->native_mode->VSyncEnd;
490                 adjusted_mode->VTotal = nv_output->native_mode->VTotal;
491                 adjusted_mode->Clock = nv_output->native_mode->Clock;
492
493                 xf86SetModeCrtc(adjusted_mode, INTERLACE_HALVE_V);
494         }
495
496         return TRUE;
497 }
498
499 static void
500 nv_output_mode_set_regs(xf86OutputPtr output, DisplayModePtr mode, DisplayModePtr adjusted_mode)
501 {
502         NVOutputPrivatePtr nv_output = output->driver_private;
503         ScrnInfoPtr pScrn = output->scrn;
504         //RIVA_HW_STATE *state;
505         //NVOutputRegPtr regp, savep;
506         Bool is_fp = FALSE;
507         xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
508         int i;
509
510         /* It's getting quiet here, not removing function just yet, we may still need it */
511
512         //state = &pNv->ModeReg;
513         //regp = &state->dac_reg[nv_output->output_resource];
514
515         if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)
516                 is_fp = TRUE;
517
518         if (output->crtc) {
519                 NVCrtcPrivatePtr nv_crtc = output->crtc->driver_private;
520                 int two_crt = FALSE;
521                 int two_mon = FALSE;
522
523                 for (i = 0; i < config->num_output; i++) {
524                         NVOutputPrivatePtr nv_output2 = config->output[i]->driver_private;
525
526                         /* is it this output ?? */
527                         if (config->output[i] == output)
528                                 continue;
529
530                         /* it the output connected */
531                         if (config->output[i]->crtc == NULL)
532                                 continue;
533
534                         two_mon = TRUE;
535                         if ((nv_output2->type == OUTPUT_ANALOG) && (nv_output->type == OUTPUT_ANALOG)) {
536                                 two_crt = TRUE;
537                         }
538                 }
539
540                 ErrorF("%d: crtc %d output %d twocrt %d twomon %d\n", is_fp, nv_crtc->head, nv_output->output_resource, two_crt, two_mon);
541         }
542 }
543
544 /* Only return if output is active (=have a crtc). */
545
546 static Bool 
547 nv_have_duallink(ScrnInfoPtr pScrn)
548 {
549         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
550         NVPtr pNv = NVPTR(pScrn);
551         int i;
552
553         for (i = 0; i < xf86_config->num_output; i++) {
554                 xf86OutputPtr output = xf86_config->output[i];
555                 NVOutputPrivatePtr nv_output = output->driver_private;
556                 if (pNv->dcb_table.entry[nv_output->dcb_entry].duallink_possible && 
557                         (pNv->dcb_table.entry[nv_output->dcb_entry].type == OUTPUT_LVDS || 
558                         pNv->dcb_table.entry[nv_output->dcb_entry].type == OUTPUT_TMDS) &&
559                         output->crtc) {
560
561                         return TRUE;
562                 }
563         }
564
565         return FALSE;
566 }
567
568 static void
569 nv_output_mode_set_routing(xf86OutputPtr output)
570 {
571         NVOutputPrivatePtr nv_output = output->driver_private;
572         xf86CrtcPtr crtc = output->crtc;
573         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
574         ScrnInfoPtr pScrn = output->scrn;
575         NVPtr pNv = NVPTR(pScrn);
576         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
577         Bool strange_mode = FALSE;
578         int i;
579
580         uint32_t output_reg[2] = {0, 0};
581
582         for (i = 0; i < xf86_config->num_output; i++) {
583                 xf86OutputPtr output2 = xf86_config->output[i];
584                 NVOutputPrivatePtr nv_output2 = output2->driver_private;
585                 if (output2->crtc) { /* enabled? */
586                         uint8_t ors = nv_output2->output_resource;
587                         if (nv_output2->type == OUTPUT_ANALOG)
588                                 output_reg[ors] = NV_RAMDAC_OUTPUT_DAC_ENABLE;
589                         if (ors != nv_output2->preferred_output)
590                                 if (pNv->Architecture == NV_ARCH_40)
591                                         strange_mode = TRUE;
592                 }
593         }
594
595         /* Some (most?) pre-NV30 cards have switchable crtc's. */
596         if (pNv->switchable_crtc) {
597                 uint8_t crtc0_index = nv_output->output_resource ^ nv_crtc->head;
598                 output_reg[~(crtc0_index) & 1] |= NV_RAMDAC_OUTPUT_SELECT_CRTC1;
599
600                 if (strange_mode)
601                         output_reg[crtc0_index] |= NV_RAMDAC_OUTPUT_SELECT_CRTC1;
602         }
603
604         ErrorF("output reg: 0x%X 0x%X\n", output_reg[0], output_reg[1]);
605
606         /* The registers can't be considered seperately on most cards */
607         nvWriteRAMDAC(pNv, 0, NV_RAMDAC_OUTPUT, output_reg[0]);
608         nvWriteRAMDAC(pNv, 1, NV_RAMDAC_OUTPUT, output_reg[1]);
609
610         /* This could use refinement for flatpanels, but it should work this way */
611         if (pNv->NVArch < 0x44) {
612                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_TEST_CONTROL, 0xf0000000);
613                 if (pNv->Architecture == NV_ARCH_40)
614                         nvWriteRAMDAC(pNv, 0, NV_RAMDAC_670, 0xf0000000);
615         } else {
616                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_TEST_CONTROL, 0x00100000);
617                 nvWriteRAMDAC(pNv, 0, NV_RAMDAC_670, 0x00100000);
618         }
619 }
620
621 static void
622 nv_output_mode_set(xf86OutputPtr output, DisplayModePtr mode,
623                    DisplayModePtr adjusted_mode)
624 {
625         ScrnInfoPtr pScrn = output->scrn;
626         NVPtr pNv = NVPTR(pScrn);
627         NVOutputPrivatePtr nv_output = output->driver_private;
628         RIVA_HW_STATE *state;
629
630         ErrorF("nv_output_mode_set is called\n");
631
632         state = &pNv->ModeReg;
633
634         nv_output_mode_set_regs(output, mode, adjusted_mode);
635         nv_output_load_state_ext(output, state, FALSE);
636         if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)
637                 nv_set_tmds_registers(output, adjusted_mode->Clock, FALSE, FALSE);
638
639         nv_output_mode_set_routing(output);
640 }
641
642 static xf86MonPtr
643 nv_get_edid(xf86OutputPtr output)
644 {
645         /* no use for shared DDC output */
646         NVOutputPrivatePtr nv_output = output->driver_private;
647         xf86MonPtr ddc_mon;
648
649         if (nv_output->pDDCBus == NULL)
650                 return NULL;
651
652         ddc_mon = xf86OutputGetEDID(output, nv_output->pDDCBus);
653         if (!ddc_mon)
654                 return NULL;
655
656         if (ddc_mon->features.input_type && (nv_output->type == OUTPUT_ANALOG))
657                 goto invalid;
658
659         if ((!ddc_mon->features.input_type) && (nv_output->type == OUTPUT_TMDS ||
660                                 nv_output->type == OUTPUT_LVDS))
661                 goto invalid;
662
663         return ddc_mon;
664
665 invalid:
666         xfree(ddc_mon);
667         return NULL;
668 }
669
670 static Bool
671 nv_ddc_detect(xf86OutputPtr output)
672 {
673         xf86MonPtr m = nv_get_edid(output);
674
675         if (m == NULL)
676                 return FALSE;
677
678         xfree(m);
679         return TRUE;
680 }
681
682 static Bool
683 nv_crt_load_detect(xf86OutputPtr output)
684 {
685         ScrnInfoPtr pScrn = output->scrn;
686         NVOutputPrivatePtr nv_output = output->driver_private;
687         NVPtr pNv = NVPTR(pScrn);
688         uint32_t reg_output, reg_test_ctrl, temp;
689         Bool present = FALSE;
690
691         /* For some reason we get false positives on output 1, maybe due tv-out? */
692         if (nv_output->preferred_output == 1) {
693                 return FALSE;
694         }
695
696         if (nv_output->pDDCBus != NULL) {
697                 xf86MonPtr ddc_mon = xf86OutputGetEDID(output, nv_output->pDDCBus);
698                 /* Is there a digital flatpanel on this channel? */
699                 if (ddc_mon && ddc_mon->features.input_type) {
700                         return FALSE;
701                 }
702         }
703
704         reg_output = nvReadRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_OUTPUT);
705         reg_test_ctrl = nvReadRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL);
706
707         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL, (reg_test_ctrl & ~0x00010000));
708
709         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_OUTPUT, (reg_output & 0x0000FEEE));
710         usleep(1000);
711
712         temp = nvReadRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_OUTPUT);
713         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_OUTPUT, temp | 1);
714
715         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_DATA, 0x94050140);
716         temp = nvReadRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL);
717         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL, temp | 0x1000);
718
719         usleep(1000);
720
721         present = (nvReadRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL) & (1 << 28)) ? TRUE : FALSE;
722
723         temp = NVOutputReadRAMDAC(output, NV_RAMDAC_TEST_CONTROL);
724         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL, temp & 0x000EFFF);
725
726         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_OUTPUT, reg_output);
727         nvWriteRAMDAC(pNv, nv_output->preferred_output, NV_RAMDAC_TEST_CONTROL, reg_test_ctrl);
728
729         if (present) {
730                 ErrorF("A crt was detected on output %d with no ddc support\n", nv_output->preferred_output);
731                 return TRUE;
732         }
733
734         return FALSE;
735 }
736
737 static xf86OutputStatus
738 nv_tmds_output_detect(xf86OutputPtr output)
739 {
740         ErrorF("nv_tmds_output_detect is called\n");
741
742         if (nv_ddc_detect(output))
743                 return XF86OutputStatusConnected;
744
745         return XF86OutputStatusDisconnected;
746 }
747
748
749 static xf86OutputStatus
750 nv_analog_output_detect(xf86OutputPtr output)
751 {
752         ErrorF("nv_analog_output_detect is called\n");
753
754         if (nv_ddc_detect(output))
755                 return XF86OutputStatusConnected;
756
757         //if (nv_crt_load_detect(output))
758         //      return XF86OutputStatusConnected;
759
760         return XF86OutputStatusDisconnected;
761 }
762
763 static DisplayModePtr
764 nv_output_get_modes(xf86OutputPtr output)
765 {
766         NVOutputPrivatePtr nv_output = output->driver_private;
767         xf86MonPtr ddc_mon;
768         DisplayModePtr ddc_modes;
769
770         ErrorF("nv_output_get_modes is called\n");
771
772         ddc_mon = nv_get_edid(output);
773
774         xf86OutputSetEDID(output, ddc_mon);
775
776         if (ddc_mon == NULL)
777                 return NULL;
778
779         ddc_modes = xf86OutputGetEDIDModes (output);
780
781         if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS) {
782                 int i;
783                 DisplayModePtr mode;
784
785                 for (i = 0; i < 4; i++) {
786                         /* We only look at detailed timings atm */
787                         if (ddc_mon->det_mon[i].type != DT)
788                                 continue;
789                         /* Selecting only based on width ok? */
790                         if (ddc_mon->det_mon[i].section.d_timings.h_active > nv_output->fpWidth) {
791                                 nv_output->fpWidth = ddc_mon->det_mon[i].section.d_timings.h_active;
792                                 nv_output->fpHeight = ddc_mon->det_mon[i].section.d_timings.v_active;
793                         }
794                 }
795
796                 if (nv_output->native_mode)
797                         xfree(nv_output->native_mode);
798                 nv_output->native_mode = NULL;
799                 if (nv_output->type == OUTPUT_TMDS) {
800                         DisplayModePtr cvtmode;
801                         /* Add a native resolution mode that is preferred */
802                         /* Reduced blanking should be fine on DVI monitor */
803                         cvtmode = xf86CVTMode(nv_output->fpWidth, nv_output->fpHeight, 60.0, TRUE, FALSE);
804                         cvtmode->type = M_T_DRIVER | M_T_PREFERRED;
805
806                         /* can xf86CVTMode generate invalid modes? */
807                         if (output->funcs->mode_valid(output, cvtmode) == MODE_OK) {
808                                 ddc_modes = xf86ModesAdd(ddc_modes, cvtmode);
809                                 nv_output->native_mode = xf86DuplicateMode(cvtmode);
810                         } else {
811                                 xf86DeleteMode(&cvtmode, cvtmode);
812                         }
813                 }
814
815                 if (!nv_output->native_mode)
816                         for (mode = ddc_modes; mode != NULL; mode = mode->next)
817                                 if (mode->HDisplay == nv_output->fpWidth &&
818                                     mode->VDisplay == nv_output->fpHeight) {
819                                         nv_output->native_mode = xf86DuplicateMode(mode);
820                                         break;
821                                 }
822                 if (!nv_output->native_mode) {
823                         ErrorF("Really bad stuff happening, CVT mode bad and no other native mode can be found.\n");
824                         ErrorF("Bailing out\n");
825                         return NULL;
826                 }
827
828                 /* We want the new mode to be the only preferred one */
829                 for (mode = ddc_modes; mode != NULL; mode = mode->next)
830                         if (mode->type & M_T_PREFERRED && !xf86ModesEqual(mode, nv_output->native_mode))
831                                 mode->type &= ~M_T_PREFERRED;
832         }
833
834         return ddc_modes;
835 }
836
837 static void
838 nv_output_destroy (xf86OutputPtr output)
839 {
840         ErrorF("nv_output_destroy is called\n");
841         NVOutputPrivatePtr nv_output = output->driver_private;
842
843         if (nv_output) {
844                 if (nv_output->native_mode)
845                         xfree(nv_output->native_mode);
846                 xfree(output->driver_private);
847         }
848 }
849
850 static void
851 nv_output_prepare(xf86OutputPtr output)
852 {
853         ErrorF("nv_output_prepare is called\n");
854         NVOutputPrivatePtr nv_output = output->driver_private;
855         ScrnInfoPtr pScrn = output->scrn;
856         xf86CrtcPtr crtc = output->crtc;
857         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
858         NVPtr pNv = NVPTR(pScrn);
859         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
860         int i;
861
862         output->funcs->dpms(output, DPMSModeOff);
863
864         /* Set our output type and output routing possibilities to the right registers */
865         NVWriteVGACR5758(pNv, nv_crtc->head, 0, pNv->dcb_table.entry[nv_output->dcb_entry].type);
866         NVWriteVGACR5758(pNv, nv_crtc->head, 2, pNv->dcb_table.entry[nv_output->dcb_entry].or);
867
868         /*
869          * Here we detect output resource conflicts.
870          * We do this based on connected monitors, since we need to catch this before something important happens.
871          */
872
873         uint8_t output_resource_mask = 0;
874         for (i = 0; i < xf86_config->num_output; i++) {
875                 xf86OutputPtr output2 = xf86_config->output[i];
876                 NVOutputPrivatePtr nv_output2 = output2->driver_private;
877
878                 /* I don't know how well this will deal with triple connected output situations. */
879                 if (output2 != output && output2->crtc) { /* output in use? */
880                         output_resource_mask |= (nv_output2->output_resource + 1); /* +1 to actually get a non-zero value */
881                 }
882         }
883
884         uint8_t or = pNv->dcb_table.entry[nv_output->dcb_entry].or;
885         /* Do we have a output resource conflict? */
886         if (output_resource_mask & (nv_output->output_resource + 1)) {
887                 if (or == ffs(or)) { /* we need this output resource */
888                         for (i = 0; i < xf86_config->num_output; i++) { /* let's find the other */
889                                 xf86OutputPtr output2 = xf86_config->output[i];
890                                 NVOutputPrivatePtr nv_output2 = output2->driver_private;
891
892                                 if (output2 != output && output2->status == XF86OutputStatusConnected) {
893                                         if (nv_output->output_resource == nv_output2->output_resource) {
894                                                 nv_output2->output_resource ^= 1;
895                                                 break; /* We don't deal with triple outputs yet */
896                                         }
897                                 }
898                         }
899                 } else { /* we have alternatives */
900                         nv_output->output_resource ^= 1;
901                 }
902         }
903 }
904
905 static void
906 nv_output_commit(xf86OutputPtr output)
907 {
908         ErrorF("nv_output_commit is called\n");
909
910         output->funcs->dpms(output, DPMSModeOn);
911 }
912
913 static const xf86OutputFuncsRec nv_analog_output_funcs = {
914     .dpms = nv_analog_output_dpms,
915     .save = nv_output_save,
916     .restore = nv_output_restore,
917     .mode_valid = nv_output_mode_valid,
918     .mode_fixup = nv_output_mode_fixup,
919     .mode_set = nv_output_mode_set,
920     .detect = nv_analog_output_detect,
921     .get_modes = nv_output_get_modes,
922     .destroy = nv_output_destroy,
923     .prepare = nv_output_prepare,
924     .commit = nv_output_commit,
925 };
926
927 #ifdef RANDR_12_INTERFACE
928 /*
929  * Several scaling modes exist, let the user choose.
930  */
931 #define SCALING_MODE_NAME "SCALING_MODE"
932 static const struct {
933         char *name;
934         enum scaling_modes mode;
935 } scaling_mode[] = {
936         { "panel", SCALE_PANEL },
937         { "fullscreen", SCALE_FULLSCREEN },
938         { "aspect", SCALE_ASPECT },
939         { "noscale", SCALE_NOSCALE },
940         { NULL, SCALE_INVALID}
941 };
942 static Atom scaling_mode_atom;
943
944 static int
945 nv_scaling_mode_lookup(char *name, int size)
946 {
947         int i;
948
949         /* for when name is zero terminated */
950         if (size < 0)
951                 size = strlen(name);
952
953         for (i = 0; scaling_mode[i].name; i++)
954                 /* We're getting non-terminated strings */
955                 if (strlen(scaling_mode[i].name) >= size &&
956                                 !strncasecmp(name, scaling_mode[i].name, size))
957                         break;
958
959         return scaling_mode[i].mode;
960 }
961
962 static void
963 nv_digital_output_create_resources(xf86OutputPtr output)
964 {
965         NVOutputPrivatePtr nv_output = output->driver_private;
966         ScrnInfoPtr pScrn = output->scrn;
967         int error, i;
968
969         /*
970          * Setup scaling mode property.
971          */
972         scaling_mode_atom = MakeAtom(SCALING_MODE_NAME, sizeof(SCALING_MODE_NAME) - 1, TRUE);
973
974         error = RRConfigureOutputProperty(output->randr_output,
975                                         scaling_mode_atom, TRUE, FALSE, FALSE,
976                                         0, NULL);
977
978         if (error != 0) {
979                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
980                         "RRConfigureOutputProperty error, %d\n", error);
981         }
982
983         char *existing_scale_name = NULL;
984         for (i = 0; scaling_mode[i].name; i++)
985                 if (scaling_mode[i].mode == nv_output->scaling_mode)
986                         existing_scale_name = scaling_mode[i].name;
987
988         error = RRChangeOutputProperty(output->randr_output, scaling_mode_atom,
989                                         XA_STRING, 8, PropModeReplace, 
990                                         strlen(existing_scale_name),
991                                         existing_scale_name, FALSE, TRUE);
992
993         if (error != 0) {
994                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
995                         "Failed to set scaling mode, %d\n", error);
996         }
997 }
998
999 static Bool
1000 nv_digital_output_set_property(xf86OutputPtr output, Atom property,
1001                                 RRPropertyValuePtr value)
1002 {
1003         NVOutputPrivatePtr nv_output = output->driver_private;
1004
1005         if (property == scaling_mode_atom) {
1006                 int32_t ret;
1007                 char *name = NULL;
1008
1009                 if (value->type != XA_STRING || value->format != 8)
1010                         return FALSE;
1011
1012                 name = (char *) value->data;
1013
1014                 /* Match a string to a scaling mode */
1015                 ret = nv_scaling_mode_lookup(name, value->size);
1016                 if (ret == SCALE_INVALID)
1017                         return FALSE;
1018
1019                 /* LVDS must always use gpu scaling. */
1020                 if (ret == SCALE_PANEL && nv_output->type == OUTPUT_LVDS)
1021                         return FALSE;
1022
1023                 nv_output->scaling_mode = ret;
1024                 return TRUE;
1025         }
1026
1027         return TRUE;
1028 }
1029
1030 #endif /* RANDR_12_INTERFACE */
1031
1032 static int 
1033 nv_tmds_output_mode_valid(xf86OutputPtr output, DisplayModePtr pMode)
1034 {
1035         ScrnInfoPtr pScrn = output->scrn;
1036         NVPtr pNv = NVPTR(pScrn);
1037         NVOutputPrivatePtr nv_output = output->driver_private;
1038
1039         /* We can't exceed the native mode.*/
1040         if (pMode->HDisplay > nv_output->fpWidth || pMode->VDisplay > nv_output->fpHeight)
1041                 return MODE_PANEL;
1042
1043         if (pNv->dcb_table.entry[nv_output->dcb_entry].duallink_possible) {
1044                 if (pMode->Clock > 330000) /* 2x165 MHz */
1045                         return MODE_CLOCK_RANGE;
1046         } else {
1047                 if (pMode->Clock > 165000) /* 165 MHz */
1048                         return MODE_CLOCK_RANGE;
1049         }
1050
1051         return nv_output_mode_valid(output, pMode);
1052 }
1053
1054 static const xf86OutputFuncsRec nv_tmds_output_funcs = {
1055         .dpms = nv_tmds_output_dpms,
1056         .save = nv_output_save,
1057         .restore = nv_output_restore,
1058         .mode_valid = nv_tmds_output_mode_valid,
1059         .mode_fixup = nv_output_mode_fixup,
1060         .mode_set = nv_output_mode_set,
1061         .detect = nv_tmds_output_detect,
1062         .get_modes = nv_output_get_modes,
1063         .destroy = nv_output_destroy,
1064         .prepare = nv_output_prepare,
1065         .commit = nv_output_commit,
1066 #ifdef RANDR_12_INTERFACE
1067         .create_resources = nv_digital_output_create_resources,
1068         .set_property = nv_digital_output_set_property,
1069 #endif /* RANDR_12_INTERFACE */
1070 };
1071
1072 static int nv_lvds_output_mode_valid
1073 (xf86OutputPtr output, DisplayModePtr pMode)
1074 {
1075         NVOutputPrivatePtr nv_output = output->driver_private;
1076
1077         /* No modes > panel's native res */
1078         if (pMode->HDisplay > nv_output->fpWidth || pMode->VDisplay > nv_output->fpHeight)
1079                 return MODE_PANEL;
1080
1081         return nv_output_mode_valid(output, pMode);
1082 }
1083
1084 static xf86OutputStatus
1085 nv_lvds_output_detect(xf86OutputPtr output)
1086 {
1087         ErrorF("nv_lvds_output_detect is called\n");
1088         ScrnInfoPtr pScrn = output->scrn;
1089         NVPtr pNv = NVPTR(pScrn);
1090         NVOutputPrivatePtr nv_output = output->driver_private;
1091
1092         if (pNv->dcb_table.entry[nv_output->dcb_entry].lvdsconf.use_straps_for_mode &&
1093             pNv->VBIOS.fp.native_mode)
1094                 return XF86OutputStatusConnected;
1095         if (nv_ddc_detect(output))
1096                 return XF86OutputStatusConnected;
1097
1098         return XF86OutputStatusDisconnected;
1099 }
1100
1101 static DisplayModePtr
1102 nv_lvds_output_get_modes(xf86OutputPtr output)
1103 {
1104         ErrorF("nv_lvds_output_get_modes is called\n");
1105         ScrnInfoPtr pScrn = output->scrn;
1106         NVPtr pNv = NVPTR(pScrn);
1107         NVOutputPrivatePtr nv_output = output->driver_private;
1108         DisplayModePtr modes;
1109
1110         if ((modes = nv_output_get_modes(output)))
1111                 return modes;
1112
1113         /* it might be possible to set up a mode from what we can read from the
1114          * RAMDAC registers, but if we can't read the BIOS table correctly
1115          * we might as well give up */
1116         if (!pNv->dcb_table.entry[nv_output->dcb_entry].lvdsconf.use_straps_for_mode ||
1117             (pNv->VBIOS.fp.native_mode == NULL))
1118                 return NULL;
1119
1120         nv_output->fpWidth = pNv->VBIOS.fp.native_mode->HDisplay;
1121         nv_output->fpHeight = pNv->VBIOS.fp.native_mode->VDisplay;
1122
1123         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Panel size is %u x %u\n",
1124                 nv_output->fpWidth, nv_output->fpHeight);
1125
1126         nv_output->native_mode = xf86DuplicateMode(pNv->VBIOS.fp.native_mode);
1127
1128         return xf86DuplicateMode(pNv->VBIOS.fp.native_mode);
1129 }
1130
1131 static const xf86OutputFuncsRec nv_lvds_output_funcs = {
1132         .dpms = nv_lvds_output_dpms,
1133         .save = nv_output_save,
1134         .restore = nv_output_restore,
1135         .mode_valid = nv_lvds_output_mode_valid,
1136         .mode_fixup = nv_output_mode_fixup,
1137         .mode_set = nv_output_mode_set,
1138         .detect = nv_lvds_output_detect,
1139         .get_modes = nv_lvds_output_get_modes,
1140         .destroy = nv_output_destroy,
1141         .prepare = nv_output_prepare,
1142         .commit = nv_output_commit,
1143 #ifdef RANDR_12_INTERFACE
1144         .create_resources = nv_digital_output_create_resources,
1145         .set_property = nv_digital_output_set_property,
1146 #endif /* RANDR_12_INTERFACE */
1147 };
1148
1149 static void nv_add_analog_output(ScrnInfoPtr pScrn, int dcb_entry, Bool dvi_pair)
1150 {
1151         NVPtr pNv = NVPTR(pScrn);
1152         xf86OutputPtr       output;
1153         NVOutputPrivatePtr    nv_output;
1154         char outputname[20];
1155         Bool create_output = TRUE;
1156         int i2c_index = pNv->dcb_table.entry[dcb_entry].i2c_index;
1157
1158         /* DVI have an analog connector and a digital one, differentiate between that and a normal vga */
1159         if (dvi_pair) {
1160                 sprintf(outputname, "DVI-A-%d", pNv->dvi_a_count);
1161                 pNv->dvi_a_count++;
1162         } else {
1163                 sprintf(outputname, "VGA-%d", pNv->vga_count);
1164                 pNv->vga_count++;
1165         }
1166
1167         nv_output = xnfcalloc (sizeof (NVOutputPrivateRec), 1);
1168         if (!nv_output) {
1169                 return;
1170         }
1171
1172         nv_output->dcb_entry = dcb_entry;
1173
1174         if (pNv->dcb_table.i2c_read[i2c_index] && pNv->pI2CBus[i2c_index] == NULL)
1175                 NV_I2CInit(pScrn, &pNv->pI2CBus[i2c_index], pNv->dcb_table.i2c_read[i2c_index], xstrdup(outputname));
1176
1177         nv_output->type = OUTPUT_ANALOG;
1178
1179         /* output route:
1180          * bit0: OUTPUT_0 valid
1181          * bit1: OUTPUT_1 valid
1182          * So lowest order has highest priority.
1183          * Below is guesswork:
1184          * bit2: All outputs valid
1185          */
1186         /* We choose the preferred output resource initially. */
1187         if (ffs(pNv->dcb_table.entry[dcb_entry].or) & OUTPUT_1) {
1188                 nv_output->preferred_output = 1;
1189                 nv_output->output_resource = 1;
1190         } else {
1191                 nv_output->preferred_output = 0;
1192                 nv_output->output_resource = 0;
1193         }
1194
1195         nv_output->bus = pNv->dcb_table.entry[dcb_entry].bus;
1196
1197         if (!create_output) {
1198                 xfree(nv_output);
1199                 return;
1200         }
1201
1202         /* Delay creation of output until we actually know we want it */
1203         output = xf86OutputCreate (pScrn, &nv_analog_output_funcs, outputname);
1204         if (!output)
1205                 return;
1206
1207         output->driver_private = nv_output;
1208
1209         nv_output->pDDCBus = pNv->pI2CBus[i2c_index];
1210
1211         if (pNv->switchable_crtc) {
1212                 output->possible_crtcs = pNv->dcb_table.entry[dcb_entry].heads;
1213         } else {
1214                 output->possible_crtcs = (1 << nv_output->preferred_output);
1215         }
1216
1217         nv_output->last_dpms = NV_DPMS_CLEARED;
1218
1219         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Adding output %s\n", outputname);
1220 }
1221
1222 static void nv_add_digital_output(ScrnInfoPtr pScrn, int dcb_entry, int lvds)
1223 {
1224         NVPtr pNv = NVPTR(pScrn);
1225         xf86OutputPtr       output;
1226         NVOutputPrivatePtr    nv_output;
1227         char outputname[20];
1228         Bool create_output = TRUE;
1229         int i2c_index = pNv->dcb_table.entry[dcb_entry].i2c_index;
1230
1231         if (lvds) {
1232                 sprintf(outputname, "LVDS-%d", pNv->lvds_count);
1233                 pNv->lvds_count++;
1234         } else {
1235                 sprintf(outputname, "DVI-D-%d", pNv->dvi_d_count);
1236                 pNv->dvi_d_count++;
1237         }
1238
1239         nv_output = xnfcalloc (sizeof (NVOutputPrivateRec), 1);
1240
1241         if (!nv_output) {
1242                 return;
1243         }
1244
1245         nv_output->dcb_entry = dcb_entry;
1246
1247         if (pNv->dcb_table.i2c_read[i2c_index] && pNv->pI2CBus[i2c_index] == NULL)
1248                 NV_I2CInit(pScrn, &pNv->pI2CBus[i2c_index], pNv->dcb_table.i2c_read[i2c_index], xstrdup(outputname));
1249
1250         nv_output->pDDCBus = pNv->pI2CBus[i2c_index];
1251
1252         /* output route:
1253          * bit0: OUTPUT_0 valid
1254          * bit1: OUTPUT_1 valid
1255          * So lowest order has highest priority.
1256          * Below is guesswork:
1257          * bit2: All outputs valid
1258          */
1259         /* We choose the preferred output resource initially. */
1260         if (ffs(pNv->dcb_table.entry[dcb_entry].or) & OUTPUT_1) {
1261                 nv_output->preferred_output = 1;
1262                 nv_output->output_resource = 1;
1263         } else {
1264                 nv_output->preferred_output = 0;
1265                 nv_output->output_resource = 0;
1266         }
1267
1268         nv_output->bus = pNv->dcb_table.entry[dcb_entry].bus;
1269
1270         if (lvds) {
1271                 nv_output->type = OUTPUT_LVDS;
1272                 /* comment below two lines to test LVDS under RandR12.
1273                  * If your screen "blooms" or "bleeds" (i.e. has a developing
1274                  * white / psychedelic pattern) then KILL X IMMEDIATELY
1275                  * (ctrl+alt+backspace) & if the effect continues reset power */
1276                 ErrorF("Output refused because we don't accept LVDS at the moment.\n");
1277                 create_output = FALSE;
1278         } else {
1279                 nv_output->type = OUTPUT_TMDS;
1280         }
1281
1282         if (!create_output) {
1283                 xfree(nv_output);
1284                 return;
1285         }
1286
1287         /* Delay creation of output until we are certain is desirable */
1288         if (lvds)
1289                 output = xf86OutputCreate (pScrn, &nv_lvds_output_funcs, outputname);
1290         else
1291                 output = xf86OutputCreate (pScrn, &nv_tmds_output_funcs, outputname);
1292         if (!output)
1293                 return;
1294
1295         output->driver_private = nv_output;
1296
1297         if (pNv->fpScaler) /* GPU Scaling */
1298                 nv_output->scaling_mode = SCALE_ASPECT;
1299         else /* Panel scaling */
1300                 nv_output->scaling_mode = SCALE_PANEL;
1301
1302 #ifdef RANDR_12_INTERFACE
1303         if (xf86GetOptValString(pNv->Options, OPTION_SCALING_MODE)) {
1304                 nv_output->scaling_mode = nv_scaling_mode_lookup(xf86GetOptValString(pNv->Options, OPTION_SCALING_MODE), -1);
1305                 if (nv_output->scaling_mode == SCALE_INVALID)
1306                         nv_output->scaling_mode = SCALE_ASPECT; /* default */
1307         }
1308 #endif /* RANDR_12_INTERFACE */
1309
1310         /* Due to serious problems we have to restrict the crtc's for certain types of outputs. */
1311         /* This is a result of problems with G70 cards that have a dvi with ffs(or) == 1 */
1312         /* Anyone know what the solution for this is? */
1313         /* This does not apply to NV31 LVDS with or == 3. */
1314         if (nv_output->preferred_output == 0 && pNv->Architecture == NV_ARCH_40) {
1315                 output->possible_crtcs = (1 << 0);
1316         } else {
1317                 if (pNv->switchable_crtc) {
1318                         output->possible_crtcs = pNv->dcb_table.entry[dcb_entry].heads;
1319                 } else {
1320                         output->possible_crtcs = (1 << nv_output->preferred_output);
1321                 }
1322         }
1323
1324         nv_output->last_dpms = NV_DPMS_CLEARED;
1325
1326         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Adding output %s\n", outputname);
1327 }
1328
1329 void NvDCBSetupOutputs(ScrnInfoPtr pScrn)
1330 {
1331         NVPtr pNv = NVPTR(pScrn);
1332         int i, type, i2c_count[0xf];
1333
1334         pNv->switchable_crtc = FALSE;
1335         /* I was wrong, again. */
1336         if (pNv->NVArch > 0x11 && pNv->twoHeads)
1337                 pNv->switchable_crtc = TRUE;
1338
1339         memset(i2c_count, 0, sizeof(i2c_count));
1340         for (i = 0 ; i < pNv->dcb_table.entries; i++)
1341                 i2c_count[pNv->dcb_table.entry[i].i2c_index]++;
1342
1343         /* we setup the outputs up from the BIOS table */
1344         for (i = 0 ; i < pNv->dcb_table.entries; i++) {
1345                 type = pNv->dcb_table.entry[i].type;
1346                 if (type > 3) {
1347                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "DCB type %d not known\n", type);
1348                         continue;
1349                 }
1350
1351                 xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "DCB entry %d: type: %d, i2c_index: %d, heads: %d, bus: %d, or: %d\n", i, type, pNv->dcb_table.entry[i].i2c_index, pNv->dcb_table.entry[i].heads, pNv->dcb_table.entry[i].bus, pNv->dcb_table.entry[i].or);
1352
1353                 switch(type) {
1354                 case OUTPUT_ANALOG:
1355                         nv_add_analog_output(pScrn, i, (i2c_count[pNv->dcb_table.entry[i].i2c_index] > 1));
1356                         break;
1357                 case OUTPUT_TMDS:
1358                         nv_add_digital_output(pScrn, i, 0);
1359                         break;
1360                 case OUTPUT_LVDS:
1361                         nv_add_digital_output(pScrn, i, 1);
1362                         break;
1363                 default:
1364                         break;
1365                 }
1366         }
1367 }
1368
1369 void NvSetupOutputs(ScrnInfoPtr pScrn)
1370 {
1371         NVPtr pNv = NVPTR(pScrn);
1372
1373         pNv->Television = FALSE;
1374
1375         memset(pNv->pI2CBus, 0, sizeof(pNv->pI2CBus));
1376         NvDCBSetupOutputs(pScrn);
1377 }
1378
1379 /*************************************************************************** \
1380 |*                                                                           *|
1381 |*       Copyright 1993-2003 NVIDIA, Corporation.  All rights reserved.      *|
1382 |*                                                                           *|
1383 |*     NOTICE TO USER:   The source code  is copyrighted under  U.S. and     *|
1384 |*     international laws.  Users and possessors of this source code are     *|
1385 |*     hereby granted a nonexclusive,  royalty-free copyright license to     *|
1386 |*     use this code in individual and commercial software.                  *|
1387 |*                                                                           *|
1388 |*     Any use of this source code must include,  in the user documenta-     *|
1389 |*     tion and  internal comments to the code,  notices to the end user     *|
1390 |*     as follows:                                                           *|
1391 |*                                                                           *|
1392 |*       Copyright 1993-1999 NVIDIA, Corporation.  All rights reserved.      *|
1393 |*                                                                           *|
1394 |*     NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY     *|
1395 |*     OF  THIS SOURCE  CODE  FOR ANY PURPOSE.  IT IS  PROVIDED  "AS IS"     *|
1396 |*     WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  NVIDIA, CORPOR-     *|
1397 |*     ATION DISCLAIMS ALL WARRANTIES  WITH REGARD  TO THIS SOURCE CODE,     *|
1398 |*     INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE-     *|
1399 |*     MENT,  AND FITNESS  FOR A PARTICULAR PURPOSE.   IN NO EVENT SHALL     *|
1400 |*     NVIDIA, CORPORATION  BE LIABLE FOR ANY SPECIAL,  INDIRECT,  INCI-     *|
1401 |*     DENTAL, OR CONSEQUENTIAL DAMAGES,  OR ANY DAMAGES  WHATSOEVER RE-     *|
1402 |*     SULTING FROM LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION     *|
1403 |*     OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF     *|
1404 |*     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE.     *|
1405 |*                                                                           *|
1406 |*     U.S. Government  End  Users.   This source code  is a "commercial     *|
1407 |*     item,"  as that  term is  defined at  48 C.F.R. 2.101 (OCT 1995),     *|
1408 |*     consisting  of "commercial  computer  software"  and  "commercial     *|
1409 |*     computer  software  documentation,"  as such  terms  are  used in     *|
1410 |*     48 C.F.R. 12.212 (SEPT 1995)  and is provided to the U.S. Govern-     *|
1411 |*     ment only as  a commercial end item.   Consistent with  48 C.F.R.     *|
1412 |*     12.212 and  48 C.F.R. 227.7202-1 through  227.7202-4 (JUNE 1995),     *|
1413 |*     all U.S. Government End Users  acquire the source code  with only     *|
1414 |*     those rights set forth herein.                                        *|
1415 |*                                                                           *|
1416  \***************************************************************************/