2
* Copyright 2003 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
4
* Permission to use, copy, modify, distribute, and sell this software and its
5
* documentation for any purpose is hereby granted without fee, provided that
6
* the above copyright notice appear in all copies and that both that copyright
7
* notice and this permission notice appear in supporting documentation, and
8
* that the name of Marc Aurele La France not be used in advertising or
9
* publicity pertaining to distribution of the software without specific,
10
* written prior permission. Marc Aurele La France makes no representations
11
* about the suitability of this software for any purpose. It is provided
12
* "as-is" without express or implied warranty.
14
* MARC AURELE LA FRANCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
16
* EVENT SHALL MARC AURELE LA FRANCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
* PERFORMANCE OF THIS SOFTWARE.
28
#include "atimach64i2c.h"
29
#include "atistruct.h"
33
/* This is derived from GATOS code, with a liberal sprinkling of bug fixes */
36
* Some local macros for use by the mid-level I2C functions.
40
(*pI2CBus->I2CUDelay)(pI2CBus, pI2CBus->HoldTime)
43
#define ATII2CSCLDirOff \
44
if (pATII2C->SCLDir != 0) \
45
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
46
pATII2C->I2CCur & ~pATII2C->SCLDir)
48
#define ATII2CSCLDirOn \
49
if (pATII2C->SCLDir != 0) \
50
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
51
pATII2C->I2CCur | pATII2C->SCLDir)
53
#define ATII2CSDADirOff \
54
if (pATII2C->SDADir != 0) \
55
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
56
pATII2C->I2CCur & ~pATII2C->SDADir)
58
#define ATII2CSDADirOn \
59
if (pATII2C->SDADir != 0) \
60
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
61
pATII2C->I2CCur | pATII2C->SDADir)
64
#define ATII2CSCLBitGet \
65
((*pATII2C->I2CGetBits)(pATI) & pATII2C->SCLGet)
67
#define ATII2CSCLBitOff \
70
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
71
pATII2C->I2CCur & ~pATII2C->SCLSet); \
75
#define ATII2CSCLBitOn \
78
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
79
pATII2C->I2CCur | pATII2C->SCLSet); \
80
do /* Wait until all devices have released SCL */ \
83
} while (ATII2CSCLBitGet == 0); \
87
#define ATII2CSDABitGet \
88
((*pATII2C->I2CGetBits)(pATI) & pATII2C->SDAGet)
90
#define ATII2CSDABitOff \
93
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
94
pATII2C->I2CCur & ~pATII2C->SDASet); \
98
#define ATII2CSDABitOn \
101
(*pATII2C->I2CSetBits)(pATII2C, pATI, \
102
pATII2C->I2CCur | pATII2C->SDASet); \
106
#define ATII2CSDABitSet(_flag) \
119
* This function puts a start signal on the I2C bus.
128
ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
129
ATIPtr pATI = pATII2C->pATI;
134
* Set I2C line directions to out-bound. SCL will remain out-bound until
141
* Send Start bit. This is a pull-down of the data line while the clock
155
* This function puts an 8-bit address on the I2C bus.
164
I2CBusPtr pI2CBus = pI2CDev->pI2CBus;
166
/* Send low byte of device address */
167
if ((*pI2CBus->I2CPutByte)(pI2CDev, (I2CByte)Address))
169
/* Send top byte of address, if appropriate */
170
if (((Address & 0x00F8U) != 0x00F0U) &&
171
((Address & 0x00FEU) != 0x0000U))
174
if ((*pI2CBus->I2CPutByte)(pI2CDev, (I2CByte)(Address >> 8)))
178
/* Kill I2C transaction on failure */
179
(*pI2CBus->I2CStop)(pI2CDev);
186
* This function puts a stop signal on the I2C bus.
194
I2CBusPtr pI2CBus = pI2CDev->pI2CBus;
195
ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
196
ATIPtr pATI = pATII2C->pATI;
198
ATII2CSDADirOn; /* Set data line direction to out-bound */
201
* Send Stop bit. This is a pull-up of the data line while the clock line
209
/* Reset I2C line directions to in-bound */
217
* This function puts an 8-bit value on the I2C bus, starting with its MSB.
226
I2CBusPtr pI2CBus = pI2CDev->pI2CBus;
227
ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
228
ATIPtr pATI = pATII2C->pATI;
232
ATII2CSDADirOn; /* Set data line direction to out-bound */
235
for (i = 0; i < 8; i++)
237
ATII2CSDABitSet(Data & 0x80U);
244
ATII2CSDABitOn; /* Release data line */
246
ATII2CSDADirOff; /* Set data line direction to in-bound */
248
ATII2CSCLBitOn; /* Start bit-read clock pulse */
256
ATII2CSCLBitOff; /* End clock pulse */
264
* This function retrieves an 8-bit value from the I2C bus.
274
I2CBusPtr pI2CBus = pI2CDev->pI2CBus;
275
ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
276
ATIPtr pATI = pATII2C->pATI;
277
unsigned long Value = 1;
281
ATII2CSCLBitOn; /* Start bit-read clock pulse */
283
/* Accumulate bit into byte value */
288
ATII2CSCLBitOff; /* End clock pulse */
289
} while (Value <= (unsigned long)((I2CByte)(-1)));
291
*pData = (I2CByte)Value;
293
ATII2CSDADirOn; /* Set data line direction to out-bound */
295
/* Send [N]ACK bit */
296
ATII2CSDABitSet(Last);
301
ATII2CSDABitOn; /* Release data line */
303
ATII2CSDADirOff; /* Set data line direction to in-bound */
309
* ATICreateI2CBusRec --
311
* This function is called to initialise an I2CBusRec.
322
ATII2CPtr pATII2C = xnfcalloc(1, SizeOf(ATII2CRec));
324
if (!(pI2CBus = xf86CreateI2CBusRec()))
326
xf86DrvMsg(iScreen, X_WARNING, "Unable to allocate I2C Bus record.\n");
331
/* Fill in generic structure fields */
332
pI2CBus->BusName = BusName;
333
pI2CBus->scrnIndex = iScreen;
335
pI2CBus->I2CAddress = ATII2CAddress;
336
pI2CBus->I2CStart = ATII2CStart;
337
pI2CBus->I2CStop = ATII2CStop;
338
pI2CBus->I2CPutByte = ATII2CPutByte;
339
pI2CBus->I2CGetByte = ATII2CGetByte;
341
pI2CBus->DriverPrivate.ptr = pATII2C;
343
pATII2C->pATI = pATI;
345
if (xf86I2CBusInit(pI2CBus))
348
xf86DrvMsg(iScreen, X_WARNING,
349
"I2C bus %s initialisation failure.\n", BusName);
350
xf86DestroyI2CBusRec(pI2CBus, TRUE, TRUE);
358
* This is called by ATIPreInit() to create I2C bus record(s) for the adapter.
363
ScrnInfoPtr pScreenInfo,
367
if (!xf86LoadSubModule(pScreenInfo, "i2c"))
370
ATIMach64I2CPreInit(pScreenInfo, pATI);
374
* ATII2CFreeScreen --
376
* This is called by ATIFreeScreen() to remove the driver's I2C interface.
384
I2CBusPtr pI2CBus, *ppI2CBus;
388
nI2CBus = xf86I2CGetScreenBuses(iScreen, &ppI2CBus);
389
while (--nI2CBus >= 0)
391
pI2CBus = ppI2CBus[nI2CBus];
392
pATII2C = pI2CBus->DriverPrivate.ptr;
394
xf86DestroyI2CBusRec(pI2CBus, TRUE, TRUE);