summaryrefslogtreecommitdiff
path: root/src/sil164/sil164.c
blob: 453ed044bd4ab4907078061871bffa75eb13b9e3 (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
/* -*- c-basic-offset: 4 -*- */
/**************************************************************************

Copyright © 2006 Dave Airlie

All Rights Reserved.

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, sub license, 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 NON-INFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR 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.

**************************************************************************/

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

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

#include "../i2c_vid.h"
#include "sil164.h"
#include "sil164_reg.h"

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

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

/* Silicon Image 164 driver for chip on i2c bus */
static void *
sil164_init(I2CBusPtr b, I2CSlaveAddr addr)
{
    /* this will detect the SIL164 chip on the specified i2c bus */
    SIL164Ptr sil;
    unsigned char ch;

    xf86DrvMsg(b->scrnIndex, X_ERROR, "detecting sil164\n");

    sil = xcalloc(1, sizeof(SIL164Rec));
    if (sil == NULL)
	return NULL;

    sil->d.DevName = "SIL164 TMDS Controller";
    sil->d.SlaveAddr = addr;
    sil->d.pI2CBus = b;
    sil->d.StartTimeout = b->StartTimeout;
    sil->d.BitTimeout = b->BitTimeout;
    sil->d.AcknTimeout = b->AcknTimeout;
    sil->d.ByteTimeout = b->ByteTimeout;
    sil->d.DriverPrivate.ptr = sil;

    if (!sil164ReadByte(sil, SIL164_VID_LO, &ch))
	goto out;

    if (ch!=(SIL164_VID & 0xFF)) {
	xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_ERROR,
		   "sil164 not detected got %d: from %s Slave %d.\n",
		   ch, sil->d.pI2CBus->BusName, sil->d.SlaveAddr);
	goto out;
    }

    if (!sil164ReadByte(sil, SIL164_DID_LO, &ch))
	goto out;

    if (ch!=(SIL164_DID & 0xFF)) {
	xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_ERROR,
		   "sil164 not detected got %d: from %s Slave %d.\n",
		   ch, sil->d.pI2CBus->BusName, sil->d.SlaveAddr);
	goto out;
    }

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

    return sil;

out:
    xfree(sil);
    return NULL;
}

static xf86OutputStatus
sil164_detect(I2CDevPtr d)
{
    SIL164Ptr sil = SILPTR(d);
    CARD8 reg9;

    sil164ReadByte(sil, SIL164_REG9, &reg9);

    if (reg9 & SIL164_9_HTPLG)
	return XF86OutputStatusConnected;
    else
	return XF86OutputStatusDisconnected;
}

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

static void
sil164_mode_set(I2CDevPtr d, DisplayModePtr 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.
     */
    /* recommended programming sequence from doc */
    /*sil164WriteByte(sil, 0x08, 0x30);
      sil164WriteByte(sil, 0x09, 0x00);
      sil164WriteByte(sil, 0x0a, 0x90);
      sil164WriteByte(sil, 0x0c, 0x89);
      sil164WriteByte(sil, 0x08, 0x31);*/
    /* don't do much */
    return;
}

/* set the SIL164 power state */
static void
sil164_dpms(I2CDevPtr d, int mode)
{
    SIL164Ptr sil = SILPTR(d);
    int ret;
    unsigned char ch;

    ret = sil164ReadByte(sil, SIL164_REG8, &ch);
    if (ret == FALSE)
	return;

    if (mode == DPMSModeOn)
	ch |= SIL164_8_PD;
    else
	ch &= ~SIL164_8_PD;

    sil164WriteByte(sil, SIL164_REG8, ch);

    return;
}

static void
sil164_dump_regs(I2CDevPtr d)
{
    SIL164Ptr sil = SILPTR(d);
    CARD8 val;

    sil164ReadByte(sil, SIL164_FREQ_LO, &val);
    xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_INFO, "SIL164_FREQ_LO: 0x%02x\n",
	       val);
    sil164ReadByte(sil, SIL164_FREQ_HI, &val);
    xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_INFO, "SIL164_FREQ_HI: 0x%02x\n",
	       val);
    sil164ReadByte(sil, SIL164_REG8, &val);
    xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_INFO, "SIL164_REG8: 0x%02x\n", val);
    sil164ReadByte(sil, SIL164_REG9, &val);
    xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_INFO, "SIL164_REG9: 0x%02x\n", val);
    sil164ReadByte(sil, SIL164_REGC, &val);
    xf86DrvMsg(sil->d.pI2CBus->scrnIndex, X_INFO, "SIL164_REGC: 0x%02x\n", val);
}

static void
sil164_save(I2CDevPtr d)
{
    SIL164Ptr sil = SILPTR(d);

    if (!sil164ReadByte(sil, SIL164_REG8, &sil->SavedReg.reg8))
	return;

    if (!sil164ReadByte(sil, SIL164_REG9, &sil->SavedReg.reg9))
	return;

    if (!sil164ReadByte(sil, SIL164_REGC, &sil->SavedReg.regc))
	return;

    return;
}

static void
sil164_restore(I2CDevPtr d)
{
    SIL164Ptr sil = SILPTR(d);

    /* Restore it powered down initially */
    sil164WriteByte(sil, SIL164_REG8, sil->SavedReg.reg8 & ~0x1);

    sil164WriteByte(sil, SIL164_REG9, sil->SavedReg.reg9);
    sil164WriteByte(sil, SIL164_REGC, sil->SavedReg.regc);
    sil164WriteByte(sil, SIL164_REG8, sil->SavedReg.reg8);
}


I830I2CVidOutputRec SIL164VidOutput = {
    .init = sil164_init,
    .detect = sil164_detect,
    .mode_valid = sil164_mode_valid,
    .mode_set = sil164_mode_set,
    .dpms = sil164_dpms,
    .dump_regs = sil164_dump_regs,
    .save = sil164_save,
    .restore = sil164_restore,
};