summaryrefslogtreecommitdiff
path: root/src/lx_output.c
blob: beb1634d702ae80a5a011083f6c4611b584041d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Copyright (c) 2008 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Neither the name of the Advanced Micro Devices, Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xf86.h"
#include "X11/Xatom.h"
#include "geode.h"
#include "xf86Modes.h"
#include "xf86Crtc.h"
#include "cim/cim_defs.h"
#include "cim/cim_regs.h"

#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)

typedef struct _LXOutputPrivateRec
{
    I2CBusPtr pDDCBus;
} LXOutputPrivateRec, *LXOutputPrivatePtr;

static Atom scale_atom;

static void
lx_create_resources(xf86OutputPtr output)
{
    int ret;
    char *s;
    ScrnInfoPtr pScrni = output->scrn;
    GeodeRec *pGeode = GEODEPTR(pScrni);

    /* Scaling is only used for panels */

    if (!(pGeode->Output & OUTPUT_PANEL))
	return;

    scale_atom = MAKE_ATOM("scale");
    ret = RRConfigureOutputProperty(output->randr_output,
	scale_atom, FALSE, FALSE, FALSE, 0, NULL);

    if (ret) {
	xf86DrvMsg(pScrni->scrnIndex, X_ERROR,
	    "RRConfigureOutputProperty error %d\n", ret);
    }

    s = "on";
    ret = RRChangeOutputProperty(output->randr_output, scale_atom,
	XA_STRING, 8, PropModeReplace, strlen(s), (pointer) s, FALSE, FALSE);

    if (ret) {
	xf86DrvMsg(pScrni->scrnIndex, X_ERROR,
	    "RRCharOutputProperty error %d\n", ret);
    }
}

static Bool
lx_output_set_property(xf86OutputPtr output, Atom property,
    RRPropertyValuePtr value)
{
    ScrnInfoPtr pScrni = output->scrn;
    GeodeRec *pGeode = GEODEPTR(pScrni);
    Bool scale = pGeode->Scale;
    char *s;
    int ret;

    if (property != scale_atom)
	return FALSE;

    if (value->type != XA_STRING || value->format != 8)
	return FALSE;

    s = (char *)value->data;

    if (value->size == 2 && !strncmp("on", s, 2))
	pGeode->Scale = TRUE;
    else if (value->size == 3 && !strncmp("off", s, 3))
	pGeode->Scale = FALSE;

    if (pGeode->Scale != scale && output->crtc) {
	xf86CrtcPtr crtc = output->crtc;

	if (crtc->enabled) {
	    ret = xf86CrtcSetMode(crtc, &crtc->desiredMode,
		crtc->desiredRotation, crtc->desiredX, crtc->desiredY);

	    if (!ret) {
		xf86DrvMsg(pScrni->scrnIndex, X_ERROR,
		    "Failed to set mode after property change!\n");

		pGeode->Scale = scale;
		return FALSE;
	    }
	}
    }

    return TRUE;
}

static void
lx_output_dpms(xf86OutputPtr output, int mode)
{
    /* DPMS is handled by the CRTC */
}

static void
lx_output_prepare(xf86OutputPtr output)
{
    /* Nothing to do */
}

static void
lx_output_commit(xf86OutputPtr output)
{
    /* Nothing to do */
}

static void
lx_output_save(xf86OutputPtr output)
{
    /* Nothing to do */
}

static void
lx_output_restore(xf86OutputPtr output)
{
    /* Nothing to do */
}

static int
lx_output_mode_valid(xf86OutputPtr output, DisplayModePtr pMode)
{
    ScrnInfoPtr pScrni = output->scrn;
    GeodeRec *pGeode = GEODEPTR(pScrni);

    /* DCON Panel specific resolution - OLPC's one */
    if (pGeode->Output & (OUTPUT_PANEL | OUTPUT_DCON)) {
        if (pGeode->panelMode->HDisplay == 1200 &&
            pGeode->panelMode->VDisplay == 900)
            return MODE_OK;
    }

    if (pGeode->Output & OUTPUT_PANEL &&
        gfx_is_panel_mode_supported(pGeode->panelMode->HDisplay,
                                    pGeode->panelMode->VDisplay,
                                    pMode->HDisplay,
                                    pMode->VDisplay,
                                    pScrni->bitsPerPixel) != -1) {

        return MODE_OK;
    }

    if (gfx_is_display_mode_supported(pMode->HDisplay,
                                      pMode->VDisplay,
                                      pScrni->bitsPerPixel,
                                      GeodeGetRefreshRate(pMode)) != -1) {
        return MODE_OK;
    }

    if (pMode->type & (M_T_DRIVER | M_T_PREFERRED))
        return MODE_OK;

    return MODE_BAD;
}

static Bool
lx_output_mode_fixup(xf86OutputPtr output, DisplayModePtr mode,
    DisplayModePtr adjusted_mode)
{
    return TRUE;
}

static void
lx_output_mode_set(xf86OutputPtr output, DisplayModePtr mode,
    DisplayModePtr adjusted_mode)
{
    ScrnInfoPtr pScrni = output->scrn;
    GeodeRec *pGeode = GEODEPTR(pScrni);

    /* Configure the output path */

    if (pGeode->Output & OUTPUT_PANEL)
	df_set_output_path((pGeode->Output & OUTPUT_CRT) ?
	    DF_DISPLAY_CRT_FP : DF_DISPLAY_FP);
    else
	df_set_output_path(DF_DISPLAY_CRT);
}

static xf86OutputStatus
lx_output_detect(xf86OutputPtr output)
{
    /* We assume that there is always something
     * out there */

    return XF86OutputStatusConnected;
}

static DisplayModePtr
lx_output_get_modes(xf86OutputPtr output)
{
    ScrnInfoPtr pScrni = output->scrn;
    GeodeRec *pGeode = GEODEPTR(pScrni);
    LXOutputPrivatePtr lx_output = output->driver_private;

    xf86MonPtr mon;
    DisplayModePtr modes;

    if (!(pGeode->Output & OUTPUT_PANEL)) {
	mon = xf86OutputGetEDID(output, lx_output->pDDCBus);
	xf86OutputSetEDID(output, mon);
	modes = xf86OutputGetEDIDModes(output);
    } else {
	modes = xf86DuplicateMode(pGeode->panelMode);
    }

    return modes;
}

static void
lx_output_destroy(xf86OutputPtr output)
{
    if (output->driver_private)
	xfree(output->driver_private);

    output->driver_private = NULL;
}

static const xf86OutputFuncsRec lx_output_funcs = {
    .create_resources = lx_create_resources,
    .dpms = lx_output_dpms,
    .save = lx_output_save,
    .restore = lx_output_restore,
    .mode_valid = lx_output_mode_valid,
    .mode_fixup = lx_output_mode_fixup,
    .prepare = lx_output_prepare,
    .mode_set = lx_output_mode_set,
    .commit = lx_output_commit,
    .detect = lx_output_detect,
    .get_modes = lx_output_get_modes,
    .set_property = lx_output_set_property,
    .destroy = lx_output_destroy,
};

void
LXSetupOutput(ScrnInfoPtr pScrni)
{
    xf86OutputPtr output;
    LXOutputPrivatePtr lxpriv;
    GeodePtr pGeode = GEODEPTR(pScrni);

    output = xf86OutputCreate(pScrni, &lx_output_funcs, "default");

    lxpriv = xnfcalloc(1, sizeof(LXOutputPrivateRec));

    if (!lxpriv) {
	xf86OutputDestroy(output);
	return;
    }

    output->driver_private = lxpriv;
    output->interlaceAllowed = TRUE;
    output->doubleScanAllowed = TRUE;

    /* Set up the DDC bus */

    GeodeI2CInit(pScrni, &lxpriv->pDDCBus, "CS5536 DDC");

    if (pScrni->monitor->widthmm && pScrni->monitor->heightmm) {
	/* prioritize the admin's screen size */
	output->mm_width = pScrni->monitor->widthmm;
	output->mm_height = pScrni->monitor->heightmm;
    } else if (pGeode->mm_width && pGeode->mm_height) {
	/* if we have a panel that we're certain of the size of, set it */
	output->mm_width = pScrni->monitor->widthmm = pGeode->mm_width;
	output->mm_height = pScrni->monitor->heightmm = pGeode->mm_height;
    }

    /* We only have one CRTC, and this output is tied to it */
    output->possible_crtcs = 1;
}