summaryrefslogtreecommitdiff
path: root/src/atii2c.c
blob: a13d647718251ac5e28db120d0b2554c09a37c91 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright 2003 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of Marc Aurele La France not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  Marc Aurele La France makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as-is" without express or implied warranty.
 *
 * MARC AURELE LA FRANCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO
 * EVENT SHALL MARC AURELE LA FRANCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

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

#include "atii2c.h"
#include "atimach64i2c.h"
#include "atistruct.h"

#include "xf86.h"

/* This is derived from GATOS code, with a liberal sprinkling of bug fixes */

/*
 * Some local macros for use by the mid-level I2C functions.
 */

#define ATII2CDelay                                            \
    (*pI2CBus->I2CUDelay)(pI2CBus, pI2CBus->HoldTime)


#define ATII2CSCLDirOff                                        \
    if (pATII2C->SCLDir != 0)                                  \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur & ~pATII2C->SCLDir)

#define ATII2CSCLDirOn                                         \
    if (pATII2C->SCLDir != 0)                                  \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur | pATII2C->SCLDir)

#define ATII2CSDADirOff                                        \
    if (pATII2C->SDADir != 0)                                  \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur & ~pATII2C->SDADir)

#define ATII2CSDADirOn                                         \
    if (pATII2C->SDADir != 0)                                  \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur | pATII2C->SDADir)


#define ATII2CSCLBitGet                                        \
    ((*pATII2C->I2CGetBits)(pATI) & pATII2C->SCLGet)

#define ATII2CSCLBitOff                                        \
    do                                                         \
    {                                                          \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur & ~pATII2C->SCLSet);               \
        ATII2CDelay;                                           \
    } while (0)

#define ATII2CSCLBitOn                                         \
    do                                                         \
    {                                                          \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur | pATII2C->SCLSet);                \
        do      /* Wait until all devices have released SCL */ \
        {                                                      \
            ATII2CDelay;                                       \
        } while (ATII2CSCLBitGet == 0);                        \
    } while (0)


#define ATII2CSDABitGet                                        \
    ((*pATII2C->I2CGetBits)(pATI) & pATII2C->SDAGet)

#define ATII2CSDABitOff                                        \
    do                                                         \
    {                                                          \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur & ~pATII2C->SDASet);               \
        ATII2CDelay;                                           \
    } while (0)

#define ATII2CSDABitOn                                         \
    do                                                         \
    {                                                          \
        (*pATII2C->I2CSetBits)(pATII2C, pATI,                  \
            pATII2C->I2CCur | pATII2C->SDASet);                \
        ATII2CDelay;                                           \
    } while (0)

#define ATII2CSDABitSet(_flag)                                 \
    do                                                         \
    {                                                          \
        if (_flag)                                             \
            ATII2CSDABitOn;                                    \
        else                                                   \
            ATII2CSDABitOff;                                   \
    } while (0)


/*
 * ATII2CStart --
 *
 * This function puts a start signal on the I2C bus.
 */
static Bool
ATII2CStart
(
    I2CBusPtr pI2CBus,
    int       timeout
)
{
    ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
    ATIPtr    pATI    = pATII2C->pATI;

    (void)timeout;

    /*
     * Set I2C line directions to out-bound.  SCL will remain out-bound until
     * next I2C Stop.
     */
    ATII2CSCLDirOn;
    ATII2CSDADirOn;

    /*
     * Send Start bit.  This is a pull-down of the data line while the clock
     * line is pulled up.
     */
    ATII2CSDABitOn;
    ATII2CSCLBitOn;
    ATII2CSDABitOff;
    ATII2CSCLBitOff;

    return TRUE;
}

/*
 * ATII2CAddress --
 *
 * This function puts an 8-bit address on the I2C bus.
 */
static Bool
ATII2CAddress
(
    I2CDevPtr    pI2CDev,
    I2CSlaveAddr Address
)
{
    I2CBusPtr pI2CBus = pI2CDev->pI2CBus;

    /* Send low byte of device address */
    if ((*pI2CBus->I2CPutByte)(pI2CDev, (I2CByte)Address))
    {
        /* Send top byte of address, if appropriate */
        if (((Address & 0x00F8U) != 0x00F0U) &&
            ((Address & 0x00FEU) != 0x0000U))
            return TRUE;

        if ((*pI2CBus->I2CPutByte)(pI2CDev, (I2CByte)(Address >> 8)))
            return TRUE;
    }

    /* Kill I2C transaction on failure */
    (*pI2CBus->I2CStop)(pI2CDev);
    return FALSE;
}

/*
 * ATII2CStop --
 *
 * This function puts a stop signal on the I2C bus.
 */
static void
ATII2CStop
(
    I2CDevPtr pI2CDev
)
{
    I2CBusPtr pI2CBus = pI2CDev->pI2CBus;
    ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
    ATIPtr    pATI    = pATII2C->pATI;

    ATII2CSDADirOn;             /* Set data line direction to out-bound */

    /*
     * Send Stop bit.  This is a pull-up of the data line while the clock line
     * is pulled up.
     */
    ATII2CSDABitOff;
    ATII2CSCLBitOn;
    ATII2CSDABitOn;
    ATII2CSCLBitOff;

    /* Reset I2C line directions to in-bound */
    ATII2CSCLDirOff;
    ATII2CSDADirOff;
}

/*
 * ATII2CPutByte --
 *
 * This function puts an 8-bit value on the I2C bus, starting with its MSB.
 */
static Bool
ATII2CPutByte
(
    I2CDevPtr pI2CDev,
    I2CByte   Data
)
{
    I2CBusPtr pI2CBus = pI2CDev->pI2CBus;
    ATII2CPtr pATII2C = pI2CBus->DriverPrivate.ptr;
    ATIPtr    pATI    = pATII2C->pATI;
    int       i;
    Bool      Result;

    ATII2CSDADirOn;             /* Set data line direction to out-bound */

    /* Send data byte */
    for (i = 0;  i < 8;  i++)
    {
        ATII2CSDABitSet(Data & 0x80U);
        ATII2CSCLBitOn;
        ATII2CSCLBitOff;

        Data <<= 1;
    }

    ATII2CSDABitOn;             /* Release data line */

    ATII2CSDADirOff;            /* Set data line direction to in-bound */

    ATII2CSCLBitOn;             /* Start bit-read clock pulse */

    /* Get [N]ACK bit */
    if (ATII2CSDABitGet)
        Result = FALSE;
    else
        Result = TRUE;

    ATII2CSCLBitOff;            /* End clock pulse */

    return Result;
}

/*
 * ATII2CGetByte --
 *
 * This function retrieves an 8-bit value from the I2C bus.
 */
static Bool
ATII2CGetByte
(
    I2CDevPtr pI2CDev,
    I2CByte   *pData,
    Bool      Last
)
{
    I2CBusPtr     pI2CBus = pI2CDev->pI2CBus;
    ATII2CPtr     pATII2C = pI2CBus->DriverPrivate.ptr;
    ATIPtr        pATI    = pATII2C->pATI;
    unsigned long Value   = 1;

    do
    {
        ATII2CSCLBitOn;         /* Start bit-read clock pulse */

        /* Accumulate bit into byte value */
        Value <<= 1;
        if (ATII2CSDABitGet)
            Value++;

        ATII2CSCLBitOff;        /* End clock pulse */
    } while (Value <= (unsigned long)((I2CByte)(-1)));

    *pData = (I2CByte)Value;

    ATII2CSDADirOn;             /* Set data line direction to out-bound */

    /* Send [N]ACK bit */
    ATII2CSDABitSet(Last);
    ATII2CSCLBitOn;
    ATII2CSCLBitOff;

    if (!Last)
        ATII2CSDABitOn;         /* Release data line */

    ATII2CSDADirOff;            /* Set data line direction to in-bound */

    return TRUE;
}

/*
 * ATICreateI2CBusRec --
 *
 * This function is called to initialise an I2CBusRec.
 */
I2CBusPtr
ATICreateI2CBusRec
(
    int    iScreen,
    ATIPtr pATI,
    char   *BusName
)
{
    I2CBusPtr pI2CBus;
    ATII2CPtr pATII2C = xnfcalloc(1, SizeOf(ATII2CRec));

    if (!(pI2CBus = xf86CreateI2CBusRec()))
    {
        xf86DrvMsg(iScreen, X_WARNING, "Unable to allocate I2C Bus record.\n");
        xfree(pATII2C);
        return NULL;
    }

    /* Fill in generic structure fields */
    pI2CBus->BusName           = BusName;
    pI2CBus->scrnIndex         = iScreen;

    pI2CBus->I2CAddress        = ATII2CAddress;
    pI2CBus->I2CStart          = ATII2CStart;
    pI2CBus->I2CStop           = ATII2CStop;
    pI2CBus->I2CPutByte        = ATII2CPutByte;
    pI2CBus->I2CGetByte        = ATII2CGetByte;

    pI2CBus->DriverPrivate.ptr = pATII2C;

    pATII2C->pATI              = pATI;

    if (xf86I2CBusInit(pI2CBus))
        return pI2CBus;

    xf86DrvMsg(iScreen, X_WARNING,
        "I2C bus %s initialisation failure.\n", BusName);
    xf86DestroyI2CBusRec(pI2CBus, TRUE, TRUE);
    xfree(pATII2C);
    return NULL;
}

/*
 * ATII2CPreInit --
 *
 * This is called by ATIPreInit() to create I2C bus record(s) for the adapter.
 */
void
ATII2CPreInit
(
    ScrnInfoPtr pScreenInfo,
    ATIPtr      pATI
)
{
            if (!xf86LoadSubModule(pScreenInfo, "i2c"))
                return;

            ATIMach64I2CPreInit(pScreenInfo, pATI);
}

/*
 * ATII2CFreeScreen --
 *
 * This is called by ATIFreeScreen() to remove the driver's I2C interface.
 */
void
ATII2CFreeScreen
(
    int iScreen
)
{
    I2CBusPtr pI2CBus, *ppI2CBus;
    ATII2CPtr pATII2C;
    int nI2CBus;

    nI2CBus = xf86I2CGetScreenBuses(iScreen, &ppI2CBus);
    while (--nI2CBus >= 0)
    {
        pI2CBus = ppI2CBus[nI2CBus];
        pATII2C = pI2CBus->DriverPrivate.ptr;

        xf86DestroyI2CBusRec(pI2CBus, TRUE, TRUE);
        xfree(pATII2C);
    }

    xfree(ppI2CBus);
}