1/*
2 * Copyright (c) 2007-2008 NVIDIA, Corporation
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
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.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#ifdef HAVE_XEXTPROTO_71
29#include <X11/extensions/dpmsconst.h>
30#else
31#define DPMS_SERVER
32#include <X11/extensions/dpms.h>
33#endif
34
35#include <X11/Xatom.h>
36
37#include "g80_type.h"
38#include "g80_display.h"
39#include "g80_output.h"
40
41static void
42G80SorSetPClk(xf86OutputPtr output, int pclk)
43{
44    G80Ptr pNv = G80PTR(output->scrn);
45    G80OutputPrivPtr pPriv = output->driver_private;
46    const int orOff = 0x800 * pPriv->or;
47    const int limit = 165000;
48
49    pNv->reg[(0x00614300+orOff)/4] = 0x70000 | (pclk > limit ? 0x101 : 0);
50}
51
52static void
53G80SorDPMSSet(xf86OutputPtr output, int mode)
54{
55    G80Ptr pNv = G80PTR(output->scrn);
56    G80OutputPrivPtr pPriv = output->driver_private;
57    const int off = 0x800 * pPriv->or;
58    CARD32 tmp;
59
60    while(pNv->reg[(0x0061C004+off)/4] & 0x80000000);
61
62    tmp = pNv->reg[(0x0061C004+off)/4];
63    tmp |= 0x80000000;
64
65    if(mode == DPMSModeOn)
66        tmp |= 1;
67    else
68        tmp &= ~1;
69
70    pNv->reg[(0x0061C004+off)/4] = tmp;
71    while((pNv->reg[(0x61C030+off)/4] & 0x10000000));
72}
73
74static int
75G80TMDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
76{
77    G80Ptr pNv = G80PTR(output->scrn);
78
79    // Disable dual-link modes unless enabled in the config file.
80    if (mode->Clock > 165000 && !pNv->AllowDualLink)
81        return MODE_CLOCK_HIGH;
82
83    return G80OutputModeValid(output, mode);
84}
85
86static int
87G80LVDSModeValid(xf86OutputPtr output, DisplayModePtr mode)
88{
89    G80OutputPrivPtr pPriv = output->driver_private;
90    DisplayModePtr native = pPriv->nativeMode;
91
92    // Ignore modes larger than the native res.
93    if (mode->HDisplay > native->HDisplay || mode->VDisplay > native->VDisplay)
94        return MODE_PANEL;
95
96    return G80OutputModeValid(output, mode);
97}
98
99static void
100G80SorModeSet(xf86OutputPtr output, DisplayModePtr mode,
101              DisplayModePtr adjusted_mode)
102{
103    ScrnInfoPtr pScrn = output->scrn;
104    G80OutputPrivPtr pPriv = output->driver_private;
105    const int sorOff = 0x40 * pPriv->or;
106    CARD32 type;
107
108    if(!adjusted_mode) {
109        /* Disconnect the SOR */
110        C(0x00000600 + sorOff, 0);
111        return;
112    }
113
114    if(pPriv->panelType == LVDS)
115        type = 0;
116    else if(adjusted_mode->Clock > 165000)
117        type = 0x500;
118    else
119        type = 0x100;
120
121    // This wouldn't be necessary, but the server is stupid and calls
122    // G80SorDPMSSet after the output is disconnected, even though the hardware
123    // turns it off automatically.
124    G80SorDPMSSet(output, DPMSModeOn);
125
126    C(0x00000600 + sorOff,
127        (G80CrtcGetHead(output->crtc) == HEAD0 ? 1 : 2) |
128        type |
129        ((adjusted_mode->Flags & V_NHSYNC) ? 0x1000 : 0) |
130        ((adjusted_mode->Flags & V_NVSYNC) ? 0x2000 : 0));
131
132    G80CrtcSetScale(output->crtc, adjusted_mode, pPriv->scale);
133}
134
135static xf86OutputStatus
136G80SorDetect(xf86OutputPtr output)
137{
138    G80OutputPrivPtr pPriv = output->driver_private;
139
140    /* Assume physical status isn't going to change before the BlockHandler */
141    if(pPriv->cached_status != XF86OutputStatusUnknown)
142        return pPriv->cached_status;
143
144    G80OutputPartnersDetect(pPriv->partner, output, pPriv->i2c);
145    return pPriv->cached_status;
146}
147
148static xf86OutputStatus
149G80SorLVDSDetect(xf86OutputPtr output)
150{
151    G80OutputPrivPtr pPriv = output->driver_private;
152
153    if(pPriv->i2c) {
154        /* If LVDS has an I2C port, use the normal probe routine to get the
155         * EDID, if possible. */
156        G80SorDetect(output);
157    }
158
159    /* Ignore G80SorDetect and assume LVDS is always connected */
160    return XF86OutputStatusConnected;
161}
162
163static void
164G80SorDestroy(xf86OutputPtr output)
165{
166    G80OutputPrivPtr pPriv = output->driver_private;
167
168    G80OutputDestroy(output);
169
170    xf86DeleteMode(&pPriv->nativeMode, pPriv->nativeMode);
171
172    free(output->driver_private);
173    output->driver_private = NULL;
174}
175
176static void G80SorSetModeBackend(DisplayModePtr dst, const DisplayModePtr src)
177{
178    // Stash the backend mode timings from src into dst
179    dst->Clock           = src->Clock;
180    dst->Flags           = src->Flags;
181    dst->CrtcHDisplay    = src->CrtcHDisplay;
182    dst->CrtcHBlankStart = src->CrtcHBlankStart;
183    dst->CrtcHSyncStart  = src->CrtcHSyncStart;
184    dst->CrtcHSyncEnd    = src->CrtcHSyncEnd;
185    dst->CrtcHBlankEnd   = src->CrtcHBlankEnd;
186    dst->CrtcHTotal      = src->CrtcHTotal;
187    dst->CrtcHSkew       = src->CrtcHSkew;
188    dst->CrtcVDisplay    = src->CrtcVDisplay;
189    dst->CrtcVBlankStart = src->CrtcVBlankStart;
190    dst->CrtcVSyncStart  = src->CrtcVSyncStart;
191    dst->CrtcVSyncEnd    = src->CrtcVSyncEnd;
192    dst->CrtcVBlankEnd   = src->CrtcVBlankEnd;
193    dst->CrtcVTotal      = src->CrtcVTotal;
194    dst->CrtcHAdjusted   = src->CrtcHAdjusted;
195    dst->CrtcVAdjusted   = src->CrtcVAdjusted;
196}
197
198static Bool
199G80SorModeFixup(xf86OutputPtr output, DisplayModePtr mode,
200                DisplayModePtr adjusted_mode)
201{
202    G80OutputPrivPtr pPriv = output->driver_private;
203    DisplayModePtr native = pPriv->nativeMode;
204
205    if(native && pPriv->scale != G80_SCALE_OFF) {
206        G80SorSetModeBackend(adjusted_mode, native);
207        // This mode is already "fixed"
208        G80CrtcSkipModeFixup(output->crtc);
209    }
210
211    return TRUE;
212}
213
214static Bool
215G80SorTMDSModeFixup(xf86OutputPtr output, DisplayModePtr mode,
216                    DisplayModePtr adjusted_mode)
217{
218    int scrnIndex = output->scrn->scrnIndex;
219    G80OutputPrivPtr pPriv = output->driver_private;
220    DisplayModePtr modes = output->probed_modes;
221
222    xf86DeleteMode(&pPriv->nativeMode, pPriv->nativeMode);
223
224    if(modes) {
225        // Find the preferred mode and use that as the "native" mode.
226        // If no preferred mode is available, use the first one.
227        DisplayModePtr mode;
228
229        // Find the preferred mode.
230        for(mode = modes; mode; mode = mode->next) {
231            if(mode->type & M_T_PREFERRED) {
232                xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
233                               "%s: preferred mode is %s\n",
234                               output->name, mode->name);
235                break;
236            }
237        }
238
239        // XXX: May not want to allow scaling if no preferred mode is found.
240        if(!mode) {
241            mode = modes;
242            xf86DrvMsgVerb(scrnIndex, X_INFO, 5,
243                    "%s: no preferred mode found, using %s\n",
244                    output->name, mode->name);
245        }
246
247        pPriv->nativeMode = xf86DuplicateMode(mode);
248        G80CrtcDoModeFixup(pPriv->nativeMode, mode);
249    }
250
251    return G80SorModeFixup(output, mode, adjusted_mode);
252}
253
254static DisplayModePtr
255G80SorGetLVDSModes(xf86OutputPtr output)
256{
257    G80OutputPrivPtr pPriv = output->driver_private;
258
259    /* If an EDID was read during detection, use the modes from that. */
260    DisplayModePtr modes = G80OutputGetDDCModes(output);
261    if(modes)
262        return modes;
263
264    /* Otherwise, feed in the mode we read during initialization. */
265    return xf86DuplicateMode(pPriv->nativeMode);
266}
267
268#define MAKE_ATOM(a) MakeAtom((a), sizeof(a) - 1, TRUE);
269
270struct property {
271    Atom atom;
272    INT32 range[2];
273};
274
275static struct {
276    struct property dither;
277    struct property scale;
278} properties;
279
280static void
281G80SorCreateResources(xf86OutputPtr output)
282{
283    ScrnInfoPtr pScrn = output->scrn;
284    G80Ptr pNv = G80PTR(pScrn);
285    int data, err;
286    const char *s;
287
288    /******** dithering ********/
289    properties.dither.atom = MAKE_ATOM("dither");
290    properties.dither.range[0] = 0;
291    properties.dither.range[1] = 1;
292    err = RRConfigureOutputProperty(output->randr_output,
293                                    properties.dither.atom, FALSE, TRUE, FALSE,
294                                    2, properties.dither.range);
295    if(err)
296        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
297                   "Failed to configure dithering property for %s: error %d\n",
298                   output->name, err);
299
300    // Set the default value
301    data = pNv->Dither;
302    err = RRChangeOutputProperty(output->randr_output, properties.dither.atom,
303                                 XA_INTEGER, 32, PropModeReplace, 1, &data,
304                                 FALSE, FALSE);
305    if(err)
306        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
307                   "Failed to set dithering property for %s: error %d\n",
308                   output->name, err);
309
310    /******** scaling ********/
311    properties.scale.atom = MAKE_ATOM("scale");
312    err = RRConfigureOutputProperty(output->randr_output,
313                                    properties.scale.atom, FALSE, FALSE,
314                                    FALSE, 0, NULL);
315    if(err)
316        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
317                   "Failed to configure scaling property for %s: error %d\n",
318                   output->name, err);
319
320    // Set the default value
321    s = "aspect";
322    err = RRChangeOutputProperty(output->randr_output, properties.scale.atom,
323                                 XA_STRING, 8, PropModeReplace, strlen(s),
324                                 (pointer)s, FALSE, FALSE);
325    if(err)
326        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
327                   "Failed to set scaling property for %s: error %d\n",
328                   output->name, err);
329}
330
331static Bool
332G80SorSetProperty(xf86OutputPtr output, Atom prop, RRPropertyValuePtr val)
333{
334    G80OutputPrivPtr pPriv = output->driver_private;
335
336    if(prop == properties.dither.atom) {
337        INT32 i;
338
339        if(val->type != XA_INTEGER || val->format != 32 || val->size != 1)
340            return FALSE;
341
342        i = *(INT32*)val->data;
343        if(i < properties.dither.range[0] || i > properties.dither.range[1])
344            return FALSE;
345
346        G80CrtcSetDither(output->crtc, i, TRUE);
347    } else if(prop == properties.scale.atom) {
348        const char *s;
349        enum G80ScaleMode oldScale, scale;
350        int i;
351        const struct {
352            const char *name;
353            enum G80ScaleMode scale;
354        } modes[] = {
355            { "off",    G80_SCALE_OFF },
356            { "aspect", G80_SCALE_ASPECT },
357            { "fill",   G80_SCALE_FILL },
358            { "center", G80_SCALE_CENTER },
359            { NULL,     0 },
360        };
361
362        if(val->type != XA_STRING || val->format != 8)
363            return FALSE;
364        s = (char*)val->data;
365
366        for(i = 0; modes[i].name; i++) {
367            const char *name = modes[i].name;
368            const int len = strlen(name);
369
370            if(val->size == len && !strncmp(name, s, len)) {
371                scale = modes[i].scale;
372                break;
373            }
374        }
375        if(!modes[i].name)
376            return FALSE;
377        if(scale == G80_SCALE_OFF && pPriv->panelType == LVDS)
378            // LVDS requires scaling
379            return FALSE;
380
381        oldScale = pPriv->scale;
382        pPriv->scale = scale;
383        if(output->crtc) {
384            xf86CrtcPtr crtc = output->crtc;
385
386            if(!xf86CrtcSetMode(crtc, &crtc->desiredMode, crtc->desiredRotation,
387                                crtc->desiredX, crtc->desiredY)) {
388                xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
389                           "Failed to set scaling to %s for output %s\n",
390                           modes[i].name, output->name);
391
392                // Restore old scale and try again.
393                pPriv->scale = oldScale;
394                if(!xf86CrtcSetMode(crtc, &crtc->desiredMode,
395                                    crtc->desiredRotation, crtc->desiredX,
396                                    crtc->desiredY)) {
397                    xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
398                               "Failed to restore old scaling for output %s\n",
399                               output->name);
400                }
401
402                return FALSE;
403            }
404        }
405    }
406
407    return TRUE;
408}
409
410static const xf86OutputFuncsRec G80SorTMDSOutputFuncs = {
411    .dpms = G80SorDPMSSet,
412    .save = NULL,
413    .restore = NULL,
414    .mode_valid = G80TMDSModeValid,
415    .mode_fixup = G80SorTMDSModeFixup,
416    .prepare = G80OutputPrepare,
417    .commit = G80OutputCommit,
418    .mode_set = G80SorModeSet,
419    .detect = G80SorDetect,
420    .get_modes = G80OutputGetDDCModes,
421    .create_resources = G80SorCreateResources,
422    .set_property = G80SorSetProperty,
423    .destroy = G80SorDestroy,
424};
425
426static const xf86OutputFuncsRec G80SorLVDSOutputFuncs = {
427    .dpms = G80SorDPMSSet,
428    .save = NULL,
429    .restore = NULL,
430    .mode_valid = G80LVDSModeValid,
431    .mode_fixup = G80SorModeFixup,
432    .prepare = G80OutputPrepare,
433    .commit = G80OutputCommit,
434    .mode_set = G80SorModeSet,
435    .detect = G80SorLVDSDetect,
436    .get_modes = G80SorGetLVDSModes,
437    .create_resources = G80SorCreateResources,
438    .set_property = G80SorSetProperty,
439    .destroy = G80SorDestroy,
440};
441
442static DisplayModePtr
443ReadLVDSNativeMode(G80Ptr pNv, const int off)
444{
445    DisplayModePtr mode = xnfcalloc(1, sizeof(DisplayModeRec));
446    const CARD32 size = pNv->reg[(0x00610B4C+off)/4];
447    const int width = size & 0x3fff;
448    const int height = (size >> 16) & 0x3fff;
449
450    mode->HDisplay = mode->CrtcHDisplay = width;
451    mode->VDisplay = mode->CrtcVDisplay = height;
452    mode->Clock           = pNv->reg[(0x610AD4+off)/4] & 0x3fffff;
453    mode->CrtcHBlankStart = pNv->reg[(0x610AFC+off)/4];
454    mode->CrtcHSyncEnd    = pNv->reg[(0x610B04+off)/4];
455    mode->CrtcHBlankEnd   = pNv->reg[(0x610AE8+off)/4];
456    mode->CrtcHTotal      = pNv->reg[(0x610AF4+off)/4];
457
458    mode->next = mode->prev = NULL;
459    mode->status = MODE_OK;
460    mode->type = M_T_DRIVER | M_T_PREFERRED;
461
462    xf86SetModeDefaultName(mode);
463
464    return mode;
465}
466
467static DisplayModePtr
468GetLVDSNativeMode(G80Ptr pNv)
469{
470    CARD32 val = pNv->reg[0x00610050/4];
471
472    if((val & 3) == 2)
473        return ReadLVDSNativeMode(pNv, 0);
474    else if((val & 0x300) == 0x200)
475        return ReadLVDSNativeMode(pNv, 0x540);
476
477    return NULL;
478}
479
480xf86OutputPtr
481G80CreateSor(ScrnInfoPtr pScrn, ORNum or, PanelType panelType)
482{
483    G80Ptr pNv = G80PTR(pScrn);
484    G80OutputPrivPtr pPriv = xnfcalloc(sizeof(*pPriv), 1);
485    const int off = 0x800 * or;
486    xf86OutputPtr output;
487    char orName[5];
488    const xf86OutputFuncsRec *funcs;
489
490    if(!pPriv)
491        return NULL;
492
493    if(panelType == LVDS) {
494        strcpy(orName, "LVDS");
495        funcs = &G80SorLVDSOutputFuncs;
496
497        pPriv->nativeMode = GetLVDSNativeMode(pNv);
498
499        if(!pPriv->nativeMode) {
500            xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
501                       "Failed to find LVDS native mode\n");
502            free(pPriv);
503            return NULL;
504        }
505
506        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "%s native size %dx%d\n",
507                   orName, pPriv->nativeMode->HDisplay,
508                   pPriv->nativeMode->VDisplay);
509    } else {
510        snprintf(orName, 5, "DVI%d", or);
511        pNv->reg[(0x61C00C+off)/4] = 0x03010700;
512        pNv->reg[(0x61C010+off)/4] = 0x0000152f;
513        pNv->reg[(0x61C014+off)/4] = 0x00000000;
514        pNv->reg[(0x61C018+off)/4] = 0x00245af8;
515        funcs = &G80SorTMDSOutputFuncs;
516    }
517
518    output = xf86OutputCreate(pScrn, funcs, orName);
519
520    pPriv->type = SOR;
521    pPriv->or = or;
522    pPriv->panelType = panelType;
523    pPriv->cached_status = XF86OutputStatusUnknown;
524    if(panelType == TMDS)
525        pPriv->set_pclk = G80SorSetPClk;
526    output->driver_private = pPriv;
527    output->interlaceAllowed = TRUE;
528    output->doubleScanAllowed = TRUE;
529
530    return output;
531}
532