2 * Copyright (c) 2007 NVIDIA, Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include <X11/extensions/dpms.h>
26 #include <X11/Xatom.h>
28 #include "nv_include.h"
29 #include "nv50_type.h"
30 #include "nv50_display.h"
31 #include "nv50_output.h"
34 NV50SorSetPClk(xf86OutputPtr output, int pclk)
36 NV50OutputPrivPtr nv_output = output->driver_private;
37 ScrnInfoPtr pScrn = output->scrn;
38 NVPtr pNv = NVPTR(pScrn);
39 const int limit = 165000;
41 NVWrite(pNv, 0x00614300 + nv_output->or * 0x800, (pclk > limit) ? 0x101 : 0);
45 NV50SorDPMSSet(xf86OutputPtr output, int mode)
47 NV50OutputPrivPtr nv_output = output->driver_private;
48 ScrnInfoPtr pScrn = output->scrn;
49 NVPtr pNv = NVPTR(pScrn);
52 while((NVRead(pNv, 0x0061c004 + nv_output->or * 0x800) & 0x80000000));
54 tmp = NVRead(pNv, 0x0061c004 + nv_output->or * 0x800);
57 if(mode == DPMSModeOn)
62 NVWrite(pNv, 0x0061c004 + nv_output->or * 0x800, tmp);
63 while((NVRead(pNv, 0x0061c030 + nv_output->or * 0x800) & 0x10000000));
67 NV50TMDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
69 // Disable dual-link modes until I can find a way to make them work
71 if (mode->Clock > 165000)
72 return MODE_CLOCK_HIGH;
74 return NV50OutputModeValid(output, mode);
78 NV50LVDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
80 NV50OutputPrivPtr nv_output = output->driver_private;
81 DisplayModePtr native = nv_output->nativeMode;
83 // Ignore modes larger than the native res.
84 if (mode->HDisplay > native->HDisplay || mode->VDisplay > native->VDisplay)
87 return NV50OutputModeValid(output, mode);
91 NV50SorModeSet(xf86OutputPtr output, DisplayModePtr mode,
92 DisplayModePtr adjusted_mode)
94 ScrnInfoPtr pScrn = output->scrn;
95 NV50OutputPrivPtr nv_output = output->driver_private;
96 const int sorOff = 0x40 * nv_output->or;
100 /* Disconnect the SOR */
101 NV50DisplayCommand(pScrn, 0x600 + sorOff, 0);
105 if (nv_output->type == OUTPUT_LVDS) {
108 if (adjusted_mode->Clock > 165000) {
114 // This wouldn't be necessary, but the server is stupid and calls
115 // NV50SorDPMSSet after the output is disconnected, even though the hardware
116 // turns it off automatically.
117 NV50SorDPMSSet(output, DPMSModeOn);
119 NV50DisplayCommand(pScrn, 0x600 + sorOff,
120 (NV50CrtcGetHead(output->crtc) == HEAD0 ? 1 : 2) | type |
121 ((adjusted_mode->Flags & V_NHSYNC) ? 0x1000 : 0) |
122 ((adjusted_mode->Flags & V_NVSYNC) ? 0x2000 : 0));
124 NV50CrtcSetScale(output->crtc, adjusted_mode, nv_output->scale);
127 static xf86OutputStatus
128 NV50SorDetect(xf86OutputPtr output)
130 NV50OutputPrivPtr nv_output = output->driver_private;
133 if (nv_output->pDDCBus == NULL)
134 return XF86OutputStatusDisconnected;
136 ddc_mon = xf86OutputGetEDID(output, nv_output->pDDCBus);
138 return XF86OutputStatusDisconnected;
140 if (!ddc_mon->features.input_type) /* Analog? */
141 return XF86OutputStatusDisconnected;
143 return XF86OutputStatusConnected;
146 static xf86OutputStatus
147 NV50SorLVDSDetect(xf86OutputPtr output)
149 /* Assume LVDS is always connected */
150 return XF86OutputStatusConnected;
154 NV50SorDestroy(xf86OutputPtr output)
156 NV50OutputPrivPtr nv_output = output->driver_private;
158 NV50OutputDestroy(output);
160 xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
162 xfree(output->driver_private);
163 output->driver_private = NULL;
167 NV50SorSetModeBackend(DisplayModePtr dst, const DisplayModePtr src)
169 // Stash the backend mode timings from src into dst
170 dst->Clock = src->Clock;
171 dst->Flags = src->Flags;
172 dst->CrtcHDisplay = src->CrtcHDisplay;
173 dst->CrtcHBlankStart = src->CrtcHBlankStart;
174 dst->CrtcHSyncStart = src->CrtcHSyncStart;
175 dst->CrtcHSyncEnd = src->CrtcHSyncEnd;
176 dst->CrtcHBlankEnd = src->CrtcHBlankEnd;
177 dst->CrtcHTotal = src->CrtcHTotal;
178 dst->CrtcHSkew = src->CrtcHSkew;
179 dst->CrtcVDisplay = src->CrtcVDisplay;
180 dst->CrtcVBlankStart = src->CrtcVBlankStart;
181 dst->CrtcVSyncStart = src->CrtcVSyncStart;
182 dst->CrtcVSyncEnd = src->CrtcVSyncEnd;
183 dst->CrtcVBlankEnd = src->CrtcVBlankEnd;
184 dst->CrtcVTotal = src->CrtcVTotal;
185 dst->CrtcHAdjusted = src->CrtcHAdjusted;
186 dst->CrtcVAdjusted = src->CrtcVAdjusted;
190 NV50SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
191 DisplayModePtr adjusted_mode)
193 NV50OutputPrivPtr nv_output = output->driver_private;
194 DisplayModePtr native = nv_output->nativeMode;
196 if(native && nv_output->scale != SCALE_PANEL) {
197 NV50SorSetModeBackend(adjusted_mode, native);
198 // This mode is already "fixed"
199 NV50CrtcSkipModeFixup(output->crtc);
206 NV50SorTMDSModeFixup(xf86OutputPtr output, DisplayModePtr mode,
207 DisplayModePtr adjusted_mode)
209 int scrnIndex = output->scrn->scrnIndex;
210 NV50OutputPrivPtr nv_output = output->driver_private;
211 DisplayModePtr modes = output->probed_modes;
213 xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
216 // Find the preferred mode and use that as the "native" mode.
217 // If no preferred mode is available, use the first one.
220 // Find the preferred mode.
221 for(mode = modes; mode; mode = mode->next) {
222 if(mode->type & M_T_PREFERRED) {
223 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
224 "%s: preferred mode is %s\n",
225 output->name, mode->name);
230 // XXX: May not want to allow scaling if no preferred mode is found.
233 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
234 "%s: no preferred mode found, using %s\n",
235 output->name, mode->name);
238 nv_output->nativeMode = xf86DuplicateMode(mode);
239 NV50CrtcDoModeFixup(nv_output->nativeMode, mode);
242 return NV50SorModeFixup(output, mode, adjusted_mode);
245 static DisplayModePtr
246 NV50SorGetLVDSModes(xf86OutputPtr output)
248 NV50OutputPrivPtr nv_output = output->driver_private;
249 return xf86DuplicateMode(nv_output->nativeMode);
252 #define MAKE_ATOM(a) MakeAtom((a), sizeof(a) - 1, TRUE);
260 struct property dither;
261 struct property scale;
265 NV50SorCreateResources(xf86OutputPtr output)
267 ScrnInfoPtr pScrn = output->scrn;
268 NVPtr pNv = NVPTR(pScrn);
272 /******** dithering ********/
273 properties.dither.atom = MAKE_ATOM("dither");
274 properties.dither.range[0] = 0;
275 properties.dither.range[1] = 1;
276 err = RRConfigureOutputProperty(output->randr_output,
277 properties.dither.atom, FALSE, TRUE, FALSE,
278 2, properties.dither.range);
280 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
281 "Failed to configure dithering property for %s: error %d\n",
284 // Set the default value
285 data = pNv->FPDither;
286 err = RRChangeOutputProperty(output->randr_output, properties.dither.atom,
287 XA_INTEGER, 32, PropModeReplace, 1, &data,
290 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
291 "Failed to set dithering property for %s: error %d\n",
294 /******** scaling ********/
295 properties.scale.atom = MAKE_ATOM("scale");
296 err = RRConfigureOutputProperty(output->randr_output,
297 properties.scale.atom, FALSE, FALSE,
300 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
301 "Failed to configure scaling property for %s: error %d\n",
304 // Set the default value
306 err = RRChangeOutputProperty(output->randr_output, properties.scale.atom,
307 XA_STRING, 8, PropModeReplace, strlen(s),
308 (pointer)s, FALSE, FALSE);
310 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
311 "Failed to set scaling property for %s: error %d\n",
316 NV50SorSetProperty(xf86OutputPtr output, Atom prop, RRPropertyValuePtr val)
318 NV50OutputPrivPtr nv_output = output->driver_private;
320 if(prop == properties.dither.atom) {
323 if (val->type != XA_INTEGER || val->format != 32 || val->size != 1)
326 i = *(INT32*)val->data;
327 if (i < properties.dither.range[0] || i > properties.dither.range[1])
330 NV50CrtcSetDither(output->crtc, i, TRUE);
332 } else if (prop == properties.scale.atom) {
334 enum scaling_modes oldScale, scale;
338 enum scaling_modes scale;
340 { "panel", SCALE_PANEL },
341 { "aspect", SCALE_ASPECT },
342 { "fullscreen", SCALE_FULLSCREEN },
343 { "noscale", SCALE_NOSCALE },
347 if (val->type != XA_STRING || val->format != 8)
349 s = (char*)val->data;
351 for (i = 0; modes[i].name; i++) {
352 const char *name = modes[i].name;
353 const int len = strlen(name);
355 if(val->size == len && !strncmp(name, s, len)) {
356 scale = modes[i].scale;
362 if (scale == SCALE_PANEL && nv_output->type == OUTPUT_LVDS)
363 // LVDS requires scaling
366 oldScale = nv_output->scale;
367 nv_output->scale = scale;
369 xf86CrtcPtr crtc = output->crtc;
371 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode, crtc->desiredRotation,
372 crtc->desiredX, crtc->desiredY)) {
373 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
374 "Failed to set scaling to %s for output %s\n",
375 modes[i].name, output->name);
377 // Restore old scale and try again.
378 nv_output->scale = oldScale;
379 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode,
380 crtc->desiredRotation, crtc->desiredX,
382 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
383 "Failed to restore old scaling for output %s\n",
396 static const xf86OutputFuncsRec NV50SorTMDSOutputFuncs = {
397 .dpms = NV50SorDPMSSet,
400 .mode_valid = NV50TMDSModeValid,
401 .mode_fixup = NV50SorTMDSModeFixup,
402 .prepare = NV50OutputPrepare,
403 .commit = NV50OutputCommit,
404 .mode_set = NV50SorModeSet,
405 .detect = NV50SorDetect,
406 .get_modes = NV50OutputGetDDCModes,
407 .create_resources = NV50SorCreateResources,
408 .set_property = NV50SorSetProperty,
409 .destroy = NV50SorDestroy,
412 static const xf86OutputFuncsRec NV50SorLVDSOutputFuncs = {
413 .dpms = NV50SorDPMSSet,
416 .mode_valid = NV50LVDSModeValid,
417 .mode_fixup = NV50SorModeFixup,
418 .prepare = NV50OutputPrepare,
419 .commit = NV50OutputCommit,
420 .mode_set = NV50SorModeSet,
421 .detect = NV50SorLVDSDetect,
422 .get_modes = NV50SorGetLVDSModes,
423 .create_resources = NV50SorCreateResources,
424 .set_property = NV50SorSetProperty,
425 .destroy = NV50SorDestroy,
428 static DisplayModePtr
429 ReadLVDSNativeMode(ScrnInfoPtr pScrn, const int off)
431 NVPtr pNv = NVPTR(pScrn);
432 DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
433 const CARD32 size = NVRead(pNv, 0x00610b4c + off);
434 const int width = size & 0x3fff;
435 const int height = (size >> 16) & 0x3fff;
437 mode->HDisplay = mode->CrtcHDisplay = width;
438 mode->VDisplay = mode->CrtcVDisplay = height;
439 mode->Clock = NVRead(pNv, 0x00610ad4 + off) & 0x3fffff;
440 mode->CrtcHBlankStart = NVRead(pNv, 0x00610afc + off);
441 mode->CrtcHSyncEnd = NVRead(pNv, 0x00610b04 + off);
442 mode->CrtcHBlankEnd = NVRead(pNv, 0x00610ae8 + off);
443 mode->CrtcHTotal = NVRead(pNv, 0x00610af4 + off);
445 mode->next = mode->prev = NULL;
446 mode->status = MODE_OK;
447 mode->type = M_T_DRIVER | M_T_PREFERRED;
449 xf86SetModeDefaultName(mode);
454 static DisplayModePtr
455 GetLVDSNativeMode(ScrnInfoPtr pScrn)
457 NVPtr pNv = NVPTR(pScrn);
458 CARD32 val = NVRead(pNv, 0x00610050);
460 if ((val & 0x3) == 0x2) {
461 return ReadLVDSNativeMode(pScrn, 0);
462 } else if ((val & 0x300) == 0x200) {
463 return ReadLVDSNativeMode(pScrn, 0x540);
470 NV50CreateSor(ScrnInfoPtr pScrn, ORNum or, NVOutputType type)
472 NV50OutputPrivPtr nv_output = xnfcalloc(sizeof(*nv_output), 1);
473 NVPtr pNv = NVPTR(pScrn);
474 xf86OutputPtr output;
476 const xf86OutputFuncsRec *funcs;
481 if(type == OUTPUT_LVDS) {
482 strcpy(orName, "LVDS");
483 funcs = &NV50SorLVDSOutputFuncs;
485 nv_output->nativeMode = GetLVDSNativeMode(pScrn);
487 if(!nv_output->nativeMode) {
488 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
489 "Failed to find LVDS native mode\n");
494 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s native size %dx%d\n",
495 orName, nv_output->nativeMode->HDisplay,
496 nv_output->nativeMode->VDisplay);
498 snprintf(orName, 5, "DVI%d", or);
499 funcs = &NV50SorTMDSOutputFuncs;
502 output = xf86OutputCreate(pScrn, funcs, orName);
505 nv_output->type = type;
506 output->driver_private = nv_output;
507 output->interlaceAllowed = TRUE;
508 output->doubleScanAllowed = TRUE;
510 if (type != OUTPUT_LVDS) {
511 NVWrite(pNv, 0x0061c00c + nv_output->or * 0x800, 0x03010700);
512 NVWrite(pNv, 0x0061c010 + nv_output->or * 0x800, 0x0000152f);
513 NVWrite(pNv, 0x0061c014 + nv_output->or * 0x800, 0x00000000);
514 NVWrite(pNv, 0x0061c018 + nv_output->or * 0x800, 0x00245af8);