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 const int limit = 165000;
38 NV50OutputWrite(output, 0x4300, (pclk > limit) ? 0x101 : 0);
42 NV50SorDPMSSet(xf86OutputPtr output, int mode)
46 while((NV50OutputRead(output, 0xc004) & 0x80000000));
48 tmp = NV50OutputRead(output, 0xc004);
51 if(mode == DPMSModeOn)
56 NV50OutputWrite(output, 0xc004, tmp);
57 while((NV50OutputRead(output, 0xc030) & 0x10000000));
61 NV50TMDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
63 // Disable dual-link modes until I can find a way to make them work
65 if (mode->Clock > 165000)
66 return MODE_CLOCK_HIGH;
68 return NV50OutputModeValid(output, mode);
72 NV50LVDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
74 NV50OutputPrivPtr nv_output = output->driver_private;
75 DisplayModePtr native = nv_output->nativeMode;
77 // Ignore modes larger than the native res.
78 if (mode->HDisplay > native->HDisplay || mode->VDisplay > native->VDisplay)
81 return NV50OutputModeValid(output, mode);
85 NV50SorModeSet(xf86OutputPtr output, DisplayModePtr mode,
86 DisplayModePtr adjusted_mode)
88 ScrnInfoPtr pScrn = output->scrn;
89 NV50OutputPrivPtr nv_output = output->driver_private;
90 const int sorOff = 0x40 * nv_output->or;
94 /* Disconnect the SOR */
95 NV50DisplayCommand(pScrn, 0x600 + sorOff, 0);
99 if (nv_output->panelType == LVDS) {
102 if (adjusted_mode->Clock > 165000) {
108 // This wouldn't be necessary, but the server is stupid and calls
109 // NV50SorDPMSSet after the output is disconnected, even though the hardware
110 // turns it off automatically.
111 NV50SorDPMSSet(output, DPMSModeOn);
113 NV50DisplayCommand(pScrn, 0x600 + sorOff,
114 (NV50CrtcGetHead(output->crtc) == HEAD0 ? 1 : 2) | type |
115 ((adjusted_mode->Flags & V_NHSYNC) ? 0x1000 : 0) |
116 ((adjusted_mode->Flags & V_NVSYNC) ? 0x2000 : 0));
118 NV50CrtcSetScale(output->crtc, adjusted_mode, nv_output->scale);
121 static xf86OutputStatus
122 NV50SorDetect(xf86OutputPtr output)
124 NV50OutputPrivPtr nv_output = output->driver_private;
126 /* Assume physical status isn't going to change before the BlockHandler */
127 if(nv_output->cached_status != XF86OutputStatusUnknown)
128 return nv_output->cached_status;
130 NV50OutputPartnersDetect(nv_output->partner, output, nv_output->i2c);
131 return nv_output->cached_status;
134 static xf86OutputStatus
135 NV50SorLVDSDetect(xf86OutputPtr output)
137 /* Assume LVDS is always connected */
138 return XF86OutputStatusConnected;
142 NV50SorDestroy(xf86OutputPtr output)
144 NV50OutputPrivPtr nv_output = output->driver_private;
146 NV50OutputDestroy(output);
148 xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
150 xfree(output->driver_private);
151 output->driver_private = NULL;
155 NV50SorSetModeBackend(DisplayModePtr dst, const DisplayModePtr src)
157 // Stash the backend mode timings from src into dst
158 dst->Clock = src->Clock;
159 dst->Flags = src->Flags;
160 dst->CrtcHDisplay = src->CrtcHDisplay;
161 dst->CrtcHBlankStart = src->CrtcHBlankStart;
162 dst->CrtcHSyncStart = src->CrtcHSyncStart;
163 dst->CrtcHSyncEnd = src->CrtcHSyncEnd;
164 dst->CrtcHBlankEnd = src->CrtcHBlankEnd;
165 dst->CrtcHTotal = src->CrtcHTotal;
166 dst->CrtcHSkew = src->CrtcHSkew;
167 dst->CrtcVDisplay = src->CrtcVDisplay;
168 dst->CrtcVBlankStart = src->CrtcVBlankStart;
169 dst->CrtcVSyncStart = src->CrtcVSyncStart;
170 dst->CrtcVSyncEnd = src->CrtcVSyncEnd;
171 dst->CrtcVBlankEnd = src->CrtcVBlankEnd;
172 dst->CrtcVTotal = src->CrtcVTotal;
173 dst->CrtcHAdjusted = src->CrtcHAdjusted;
174 dst->CrtcVAdjusted = src->CrtcVAdjusted;
178 NV50SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
179 DisplayModePtr adjusted_mode)
181 NV50OutputPrivPtr nv_output = output->driver_private;
182 DisplayModePtr native = nv_output->nativeMode;
184 if(native && nv_output->scale != NV50_SCALE_OFF) {
185 NV50SorSetModeBackend(adjusted_mode, native);
186 // This mode is already "fixed"
187 NV50CrtcSkipModeFixup(output->crtc);
194 NV50SorTMDSModeFixup(xf86OutputPtr output, DisplayModePtr mode,
195 DisplayModePtr adjusted_mode)
197 int scrnIndex = output->scrn->scrnIndex;
198 NV50OutputPrivPtr nv_output = output->driver_private;
199 DisplayModePtr modes = output->probed_modes;
201 xf86DeleteMode(&nv_output->nativeMode, nv_output->nativeMode);
204 // Find the preferred mode and use that as the "native" mode.
205 // If no preferred mode is available, use the first one.
208 // Find the preferred mode.
209 for(mode = modes; mode; mode = mode->next) {
210 if(mode->type & M_T_PREFERRED) {
211 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
212 "%s: preferred mode is %s\n",
213 output->name, mode->name);
218 // XXX: May not want to allow scaling if no preferred mode is found.
221 xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
222 "%s: no preferred mode found, using %s\n",
223 output->name, mode->name);
226 nv_output->nativeMode = xf86DuplicateMode(mode);
227 NV50CrtcDoModeFixup(nv_output->nativeMode, mode);
230 return NV50SorModeFixup(output, mode, adjusted_mode);
233 static DisplayModePtr
234 NV50SorGetLVDSModes(xf86OutputPtr output)
236 NV50OutputPrivPtr nv_output = output->driver_private;
237 return xf86DuplicateMode(nv_output->nativeMode);
240 #define MAKE_ATOM(a) MakeAtom((a), sizeof(a) - 1, TRUE);
248 struct property dither;
249 struct property scale;
253 NV50SorCreateResources(xf86OutputPtr output)
255 ScrnInfoPtr pScrn = output->scrn;
256 NVPtr pNv = NVPTR(pScrn);
260 /******** dithering ********/
261 properties.dither.atom = MAKE_ATOM("dither");
262 properties.dither.range[0] = 0;
263 properties.dither.range[1] = 1;
264 err = RRConfigureOutputProperty(output->randr_output,
265 properties.dither.atom, FALSE, TRUE, FALSE,
266 2, properties.dither.range);
268 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
269 "Failed to configure dithering property for %s: error %d\n",
272 // Set the default value
273 data = pNv->FPDither;
274 err = RRChangeOutputProperty(output->randr_output, properties.dither.atom,
275 XA_INTEGER, 32, PropModeReplace, 1, &data,
278 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
279 "Failed to set dithering property for %s: error %d\n",
282 /******** scaling ********/
283 properties.scale.atom = MAKE_ATOM("scale");
284 err = RRConfigureOutputProperty(output->randr_output,
285 properties.scale.atom, FALSE, FALSE,
288 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
289 "Failed to configure scaling property for %s: error %d\n",
292 // Set the default value
294 err = RRChangeOutputProperty(output->randr_output, properties.scale.atom,
295 XA_STRING, 8, PropModeReplace, strlen(s),
296 (pointer)s, FALSE, FALSE);
298 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
299 "Failed to set scaling property for %s: error %d\n",
304 NV50SorSetProperty(xf86OutputPtr output, Atom prop, RRPropertyValuePtr val)
306 NV50OutputPrivPtr nv_output = output->driver_private;
308 if(prop == properties.dither.atom) {
311 if (val->type != XA_INTEGER || val->format != 32 || val->size != 1)
314 i = *(INT32*)val->data;
315 if (i < properties.dither.range[0] || i > properties.dither.range[1])
318 NV50CrtcSetDither(output->crtc, i, TRUE);
320 } else if (prop == properties.scale.atom) {
322 enum NV50ScaleMode oldScale, scale;
326 enum NV50ScaleMode scale;
328 { "off", NV50_SCALE_OFF },
329 { "aspect", NV50_SCALE_ASPECT },
330 { "fill", NV50_SCALE_FILL },
331 { "center", NV50_SCALE_CENTER },
335 if (val->type != XA_STRING || val->format != 8)
337 s = (char*)val->data;
339 for (i = 0; modes[i].name; i++) {
340 const char *name = modes[i].name;
341 const int len = strlen(name);
343 if(val->size == len && !strncmp(name, s, len)) {
344 scale = modes[i].scale;
350 if (scale == NV50_SCALE_OFF && nv_output->panelType == LVDS)
351 // LVDS requires scaling
354 oldScale = nv_output->scale;
355 nv_output->scale = scale;
357 xf86CrtcPtr crtc = output->crtc;
359 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode, crtc->desiredRotation,
360 crtc->desiredX, crtc->desiredY)) {
361 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
362 "Failed to set scaling to %s for output %s\n",
363 modes[i].name, output->name);
365 // Restore old scale and try again.
366 nv_output->scale = oldScale;
367 if (!xf86CrtcSetMode(crtc, &crtc->desiredMode,
368 crtc->desiredRotation, crtc->desiredX,
370 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
371 "Failed to restore old scaling for output %s\n",
384 static const xf86OutputFuncsRec NV50SorTMDSOutputFuncs = {
385 .dpms = NV50SorDPMSSet,
388 .mode_valid = NV50TMDSModeValid,
389 .mode_fixup = NV50SorTMDSModeFixup,
390 .prepare = NV50OutputPrepare,
391 .commit = NV50OutputCommit,
392 .mode_set = NV50SorModeSet,
393 .detect = NV50SorDetect,
394 .get_modes = NV50OutputGetDDCModes,
395 .create_resources = NV50SorCreateResources,
396 .set_property = NV50SorSetProperty,
397 .destroy = NV50SorDestroy,
400 static const xf86OutputFuncsRec NV50SorLVDSOutputFuncs = {
401 .dpms = NV50SorDPMSSet,
404 .mode_valid = NV50LVDSModeValid,
405 .mode_fixup = NV50SorModeFixup,
406 .prepare = NV50OutputPrepare,
407 .commit = NV50OutputCommit,
408 .mode_set = NV50SorModeSet,
409 .detect = NV50SorLVDSDetect,
410 .get_modes = NV50SorGetLVDSModes,
411 .create_resources = NV50SorCreateResources,
412 .set_property = NV50SorSetProperty,
413 .destroy = NV50SorDestroy,
416 static DisplayModePtr
417 ReadLVDSNativeMode(ScrnInfoPtr pScrn, const int off)
419 DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
420 const CARD32 size = NV50DisplayRead(pScrn, 0xb4c + off);
421 const int width = size & 0x3fff;
422 const int height = (size >> 16) & 0x3fff;
424 mode->HDisplay = mode->CrtcHDisplay = width;
425 mode->VDisplay = mode->CrtcVDisplay = height;
426 mode->Clock = NV50DisplayRead(pScrn, 0xad4 + off) & 0x3fffff;
427 mode->CrtcHBlankStart = NV50DisplayRead(pScrn, 0xafc + off);
428 mode->CrtcHSyncEnd = NV50DisplayRead(pScrn, 0xb04 + off);
429 mode->CrtcHBlankEnd = NV50DisplayRead(pScrn, 0xae8 + off);
430 mode->CrtcHTotal = NV50DisplayRead(pScrn, 0xaf4 + off);
432 mode->next = mode->prev = NULL;
433 mode->status = MODE_OK;
434 mode->type = M_T_DRIVER | M_T_PREFERRED;
436 xf86SetModeDefaultName(mode);
441 static DisplayModePtr
442 GetLVDSNativeMode(ScrnInfoPtr pScrn)
444 CARD32 val = NV50DisplayRead(pScrn, 0x50);
446 if ((val & 0x3) == 0x2) {
447 return ReadLVDSNativeMode(pScrn, 0);
448 } else if ((val & 0x300) == 0x200) {
449 return ReadLVDSNativeMode(pScrn, 0x540);
456 NV50CreateSor(ScrnInfoPtr pScrn, ORNum or, PanelType panelType)
458 NV50OutputPrivPtr nv_output = xnfcalloc(sizeof(*nv_output), 1);
459 xf86OutputPtr output;
461 const xf86OutputFuncsRec *funcs;
466 if(panelType == LVDS) {
467 strcpy(orName, "LVDS");
468 funcs = &NV50SorLVDSOutputFuncs;
470 nv_output->nativeMode = GetLVDSNativeMode(pScrn);
472 if(!nv_output->nativeMode) {
473 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
474 "Failed to find LVDS native mode\n");
479 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s native size %dx%d\n",
480 orName, nv_output->nativeMode->HDisplay,
481 nv_output->nativeMode->VDisplay);
483 snprintf(orName, 5, "DVI%d", or);
484 funcs = &NV50SorTMDSOutputFuncs;
487 output = xf86OutputCreate(pScrn, funcs, orName);
489 nv_output->type = SOR;
491 nv_output->panelType = panelType;
492 nv_output->cached_status = XF86OutputStatusUnknown;
493 if (panelType == TMDS)
494 nv_output->set_pclk = NV50SorSetPClk;
495 output->driver_private = nv_output;
496 output->interlaceAllowed = TRUE;
497 output->doubleScanAllowed = TRUE;
499 if (panelType != LVDS) {
500 NV50OutputWrite(output, 0xc00c, 0x03010700);
501 NV50OutputWrite(output, 0xc010, 0x0000152f);
502 NV50OutputWrite(output, 0xc014, 0x00000000);
503 NV50OutputWrite(output, 0xc018, 0x00245af8);