summaryrefslogtreecommitdiff
path: root/src/tfp410/tfp410.c
blob: c2c4fc3d51b768882d67ba026874035be0c3c5a8 (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
/* -*- c-basic-offset: 4 -*- */
/*
 * Copyright © 2007 Dave Mueller
 *
 * 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 (including the next
 * paragraph) 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.
 *
 * Authors:
 *    Dave Mueller <dave.mueller@gmx.ch>
 *
 */

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

#include <stdint.h>

#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86Resources.h"
#include "compiler.h"
#include "miscstruct.h"
#include "xf86i2c.h"
#include "xf86Crtc.h"
#define DPMS_SERVER
#include <X11/extensions/dpms.h>

#include "../i2c_vid.h"
#include "tfp410.h"
#include "tfp410_reg.h"

typedef struct _TFP410SaveRec {
    uint8_t ctl1;
    uint8_t ctl2;
} TFP410SaveRec;

typedef struct {
    I2CDevRec d;
    Bool quiet;

    TFP410SaveRec SavedReg;
    TFP410SaveRec ModeReg;
} TFP410Rec, *TFP410Ptr;

#define TFPPTR(d) ((TFP410Ptr)(d->DriverPrivate.ptr))

static Bool
tfp410ReadByte(TFP410Ptr tfp, int addr, uint8_t *ch)
{
    if (!xf86I2CReadByte(&(tfp->d), addr, ch)) {
	if (!tfp->quiet) {
	    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_ERROR,
		       "Unable to read from %s Slave %d.\n",
		       tfp->d.pI2CBus->BusName, tfp->d.SlaveAddr);
	}
	return FALSE;
    }
    return TRUE;
}

static Bool
tfp410WriteByte(TFP410Ptr tfp, int addr, uint8_t ch)
{
    if (!xf86I2CWriteByte(&(tfp->d), addr, ch)) {
	if (!tfp->quiet) {
	    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_ERROR,
		       "Unable to write to %s Slave %d.\n",
		       tfp->d.pI2CBus->BusName, tfp->d.SlaveAddr);
	}
	return FALSE;
    }
    return TRUE;
}

static int
tfp410GetID(TFP410Ptr tfp, int addr)
{
    unsigned char ch1, ch2;

    if (tfp410ReadByte(tfp, addr+0, &ch1) &&
        tfp410ReadByte(tfp, addr+1, &ch2)) {

	return ((ch2<<8) & 0xFF00) | (ch1 & 0x00FF);
    }
    return -1;
}

/* Ti TFP410 driver for chip on i2c bus */
static void *
tfp410_init(I2CBusPtr b, I2CSlaveAddr addr)
{
    /* this will detect the tfp410 chip on the specified i2c bus */
    TFP410Ptr tfp;
    int id;

    tfp = xcalloc(1, sizeof(TFP410Rec));
    if (tfp == NULL)
	return NULL;

    tfp->d.DevName = "TFP410 TMDS Controller";
    tfp->d.SlaveAddr = addr;
    tfp->d.pI2CBus = b;
    tfp->d.StartTimeout = b->StartTimeout;
    tfp->d.BitTimeout = b->BitTimeout;
    tfp->d.AcknTimeout = b->AcknTimeout;
    tfp->d.ByteTimeout = b->ByteTimeout;
    tfp->d.DriverPrivate.ptr = tfp;
    tfp->quiet = TRUE;

    if ((id = tfp410GetID(tfp, TFP410_VID_LO)) != TFP410_VID) {
	if (id != 0xffffffff) {
	    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_ERROR,
		       "tfp410 not detected got VID %X: from %s Slave %d.\n",
		       id, tfp->d.pI2CBus->BusName, tfp->d.SlaveAddr);
	}
	goto out;
    }

    if ((id = tfp410GetID(tfp, TFP410_DID_LO)) != TFP410_DID) {
	xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_ERROR,
		   "tfp410 not detected got DID %X: from %s Slave %d.\n",
		   id, tfp->d.pI2CBus->BusName, tfp->d.SlaveAddr);
	goto out;
    }
    tfp->quiet = FALSE;

    if (!xf86I2CDevInit(&(tfp->d))) {
	goto out;
    }

    return tfp;

out:
    xfree(tfp);
    return NULL;
}

static xf86OutputStatus
tfp410_detect(I2CDevPtr d)
{
    TFP410Ptr tfp = TFPPTR(d);
    xf86OutputStatus ret = XF86OutputStatusDisconnected;
    unsigned char ctl2;

    if (tfp410ReadByte(tfp, TFP410_CTL_2, &ctl2)) {
	if (ctl2 & TFP410_CTL_2_HTPLG)
	    ret = XF86OutputStatusConnected;
	else
	    ret = XF86OutputStatusDisconnected;
    }

    return ret;
}

static ModeStatus
tfp410_mode_valid(I2CDevPtr d, DisplayModePtr mode)
{
    return MODE_OK;
}

static void
tfp410_mode_set(I2CDevPtr d, DisplayModePtr mode, DisplayModePtr adjusted_mode)
{
    /* As long as the basics are set up, since we don't have clock dependencies
     * in the mode setup, we can just leave the registers alone and everything
     * will work fine.
     */
    /* don't do much */
    return;
}

/* set the tfp410 power state */
static void
tfp410_dpms(I2CDevPtr d, int mode)
{
    TFP410Ptr tfp = TFPPTR(d);
    unsigned char ctl1;

    if (!tfp410ReadByte(tfp, TFP410_CTL_1, &ctl1))
	return;

    if (mode == DPMSModeOn)
	ctl1 |= TFP410_CTL_1_PD;
    else
	ctl1 &= ~TFP410_CTL_1_PD;

    tfp410WriteByte(tfp, TFP410_CTL_1, ctl1);
}

static void
tfp410_dump_regs(I2CDevPtr d)
{
    TFP410Ptr tfp = TFPPTR(d);
    uint8_t val, val2;

    tfp410ReadByte(tfp, TFP410_REV, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_REV: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_CTL_1, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_CTL1: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_CTL_2, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_CTL2: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_CTL_3, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_CTL3: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_USERCFG, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_USERCFG: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_DE_DLY, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_DE_DLY: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_DE_CTL, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_DE_CTL: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_DE_TOP, &val);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_DE_TOP: 0x%02X\n", val);
    tfp410ReadByte(tfp, TFP410_DE_CNT_LO, &val);
    tfp410ReadByte(tfp, TFP410_DE_CNT_HI, &val2);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_DE_CNT: 0x%02X%02X\n", val2, val);
    tfp410ReadByte(tfp, TFP410_DE_LIN_LO, &val);
    tfp410ReadByte(tfp, TFP410_DE_LIN_HI, &val2);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_DE_LIN: 0x%02X%02X\n", val2, val);
    tfp410ReadByte(tfp, TFP410_H_RES_LO, &val);
    tfp410ReadByte(tfp, TFP410_H_RES_HI, &val2);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_H_RES: 0x%02X%02X\n", val2, val);
    tfp410ReadByte(tfp, TFP410_V_RES_LO, &val);
    tfp410ReadByte(tfp, TFP410_V_RES_HI, &val2);
    xf86DrvMsg(tfp->d.pI2CBus->scrnIndex, X_INFO,
	       "TFP410_V_RES: 0x%02X%02X\n", val2, val);
}

static void
tfp410_save(I2CDevPtr d)
{
    TFP410Ptr tfp = TFPPTR(d);

    if (!tfp410ReadByte(tfp, TFP410_CTL_1, &tfp->SavedReg.ctl1))
	return;

    if (!tfp410ReadByte(tfp, TFP410_CTL_2, &tfp->SavedReg.ctl2))
	return;
}

static void
tfp410_restore(I2CDevPtr d)
{
    TFP410Ptr tfp = TFPPTR(d);

    /* Restore it powered down initially */
    tfp410WriteByte(tfp, TFP410_CTL_1, tfp->SavedReg.ctl1 & ~0x1);

    tfp410WriteByte(tfp, TFP410_CTL_2, tfp->SavedReg.ctl2);
    tfp410WriteByte(tfp, TFP410_CTL_1, tfp->SavedReg.ctl1);
}

_X_EXPORT I830I2CVidOutputRec TFP410VidOutput = {
    .init = tfp410_init,
    .detect = tfp410_detect,
    .mode_valid = tfp410_mode_valid,
    .mode_set = tfp410_mode_set,
    .dpms = tfp410_dpms,
    .dump_regs = tfp410_dump_regs,
    .save = tfp410_save,
    .restore = tfp410_restore,
};