Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 giacomo 1
/* ------------------------------------------------------------------------- */
2
/* i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters                 */
3
/* ------------------------------------------------------------------------- */
4
/*   Copyright (C) 1995-1997 Simon G. Vogl
5
                   1998-2000 Hans Berglund
6
 
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 2 of the License, or
10
    (at your option) any later version.
11
 
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
 
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
20
/* ------------------------------------------------------------------------- */
21
 
22
/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
23
   Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
24
   <mbailey@littlefeet-inc.com> */
25
 
26
/* Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
27
   messages, proper stop/repstart signaling during receive,
28
   added detect code */
29
 
30
/* #define DEBUG 1 */           /* to pick up dev_dbg calls */
31
 
32
#include <linux/kernel.h>
33
#include <linux/module.h>
34
#include <linux/delay.h>
35
#include <linux/slab.h>
36
#include <linux/init.h>
37
#include <linux/errno.h>
38
#include <linux/i2c.h>
39
#include <linux/i2c-algo-pcf.h>
40
#include "i2c-algo-pcf.h"
41
 
42
 
43
/* ----- global defines ----------------------------------------------- */
44
#define DEB(x) if (i2c_debug>=1) x
45
#define DEB2(x) if (i2c_debug>=2) x
46
#define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
47
#define DEBPROTO(x) if (i2c_debug>=9) x;
48
        /* debug the protocol by showing transferred bits */
49
#define DEF_TIMEOUT 16
50
 
51
/* module parameters:
52
 */
53
static int i2c_debug=0;
54
 
55
/* --- setting states on the bus with the right timing: --------------- */
56
 
57
#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
58
#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
59
#define get_own(adap) adap->getown(adap->data)
60
#define get_clock(adap) adap->getclock(adap->data)
61
#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
62
#define i2c_inb(adap) adap->getpcf(adap->data, 0)
63
 
64
/* --- other auxiliary functions -------------------------------------- */
65
 
66
static void i2c_start(struct i2c_algo_pcf_data *adap)
67
{
68
        DEBPROTO(printk("S "));
69
        set_pcf(adap, 1, I2C_PCF_START);
70
}
71
 
72
static void i2c_repstart(struct i2c_algo_pcf_data *adap)
73
{
74
        DEBPROTO(printk(" Sr "));
75
        set_pcf(adap, 1, I2C_PCF_REPSTART);
76
}
77
 
78
 
79
static void i2c_stop(struct i2c_algo_pcf_data *adap)
80
{
81
        DEBPROTO(printk("P\n"));
82
        set_pcf(adap, 1, I2C_PCF_STOP);
83
}
84
 
85
 
86
static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
87
 
88
        int timeout = DEF_TIMEOUT;
89
        int status;
90
 
91
        status = get_pcf(adap, 1);
92
#ifndef STUB_I2C
93
        while (timeout-- && !(status & I2C_PCF_BB)) {
94
                udelay(100); /* wait for 100 us */
95
                status = get_pcf(adap, 1);
96
        }
97
#endif
98
        if (timeout <= 0) {
99
                printk(KERN_ERR "Timeout waiting for Bus Busy\n");
100
        }
101
 
102
        return (timeout<=0);
103
}
104
 
105
 
106
static inline void pcf_sleep(unsigned long timeout)
107
{
108
        schedule_timeout( timeout * HZ);
109
}
110
 
111
 
112
static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
113
 
114
        int timeout = DEF_TIMEOUT;
115
 
116
        *status = get_pcf(adap, 1);
117
#ifndef STUB_I2C
118
        while (timeout-- && (*status & I2C_PCF_PIN)) {
119
                adap->waitforpin();
120
                *status = get_pcf(adap, 1);
121
        }
122
#endif
123
        if (timeout <= 0)
124
                return(-1);
125
        else
126
                return(0);
127
}
128
 
129
/*
130
 * This should perform the 'PCF8584 initialization sequence' as described
131
 * in the Philips IC12 data book (1995, Aug 29).
132
 * There should be a 30 clock cycle wait after reset, I assume this
133
 * has been fulfilled.
134
 * There should be a delay at the end equal to the longest I2C message
135
 * to synchronize the BB-bit (in multimaster systems). How long is
136
 * this? I assume 1 second is always long enough.
137
 *
138
 * vdovikin: added detect code for PCF8584
139
 */
140
static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
141
{
142
        unsigned char temp;
143
 
144
        DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
145
 
146
        /* S1=0x80: S0 selected, serial interface off */
147
        set_pcf(adap, 1, I2C_PCF_PIN);
148
        /* check to see S1 now used as R/W ctrl -
149
           PCF8584 does that when ESO is zero */
150
        if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
151
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
152
                return -ENXIO; /* definetly not PCF8584 */
153
        }
154
 
155
        /* load own address in S0, effective address is (own << 1)      */
156
        i2c_outb(adap, get_own(adap));
157
        /* check it's really written */
158
        if ((temp = i2c_inb(adap)) != get_own(adap)) {
159
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
160
                return -ENXIO;
161
        }
162
 
163
        /* S1=0xA0, next byte in S2                                     */
164
        set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
165
        /* check to see S2 now selected */
166
        if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
167
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
168
                return -ENXIO;
169
        }
170
 
171
        /* load clock register S2                                       */
172
        i2c_outb(adap, get_clock(adap));
173
        /* check it's really written, the only 5 lowest bits does matter */
174
        if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
175
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
176
                return -ENXIO;
177
        }
178
 
179
        /* Enable serial interface, idle, S0 selected                   */
180
        set_pcf(adap, 1, I2C_PCF_IDLE);
181
 
182
        /* check to see PCF is really idled and we can access status register */
183
        if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
184
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
185
                return -ENXIO;
186
        }
187
 
188
        printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
189
 
190
        return 0;
191
}
192
 
193
 
194
/* ----- Utility functions
195
 */
196
 
197
static inline int try_address(struct i2c_algo_pcf_data *adap,
198
                       unsigned char addr, int retries)
199
{
200
        int i, status, ret = -1;
201
        for (i=0;i<retries;i++) {
202
                i2c_outb(adap, addr);
203
                i2c_start(adap);
204
                status = get_pcf(adap, 1);
205
                if (wait_for_pin(adap, &status) >= 0) {
206
                        if ((status & I2C_PCF_LRB) == 0) {
207
                                i2c_stop(adap);
208
                                break;  /* success! */
209
                        }
210
                }
211
                i2c_stop(adap);
212
                udelay(adap->udelay);
213
        }
214
        DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
215
                           addr));
216
        return ret;
217
}
218
 
219
 
220
static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
221
                         int count, int last)
222
{
223
        struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
224
        int wrcount, status, timeout;
225
 
226
        for (wrcount=0; wrcount<count; ++wrcount) {
227
                DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
228
                                buf[wrcount]&0xff));
229
                i2c_outb(adap, buf[wrcount]);
230
                timeout = wait_for_pin(adap, &status);
231
                if (timeout) {
232
                        i2c_stop(adap);
233
                        dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
234
                        return -EREMOTEIO; /* got a better one ?? */
235
                }
236
#ifndef STUB_I2C
237
                if (status & I2C_PCF_LRB) {
238
                        i2c_stop(adap);
239
                        dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
240
                        return -EREMOTEIO; /* got a better one ?? */
241
                }
242
#endif
243
        }
244
        if (last) {
245
                i2c_stop(adap);
246
        }
247
        else {
248
                i2c_repstart(adap);
249
        }
250
 
251
        return (wrcount);
252
}
253
 
254
 
255
static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
256
                         int count, int last)
257
{
258
        int i, status;
259
        struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
260
 
261
        /* increment number of bytes to read by one -- read dummy byte */
262
        for (i = 0; i <= count; i++) {
263
 
264
                if (wait_for_pin(adap, &status)) {
265
                        i2c_stop(adap);
266
                        dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
267
                        return (-1);
268
                }
269
 
270
#ifndef STUB_I2C
271
                if ((status & I2C_PCF_LRB) && (i != count)) {
272
                        i2c_stop(adap);
273
                        dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
274
                        return (-1);
275
                }
276
#endif
277
 
278
                if (i == count - 1) {
279
                        set_pcf(adap, 1, I2C_PCF_ESO);
280
                } else
281
                if (i == count) {
282
                        if (last) {
283
                                i2c_stop(adap);
284
                        } else {
285
                                i2c_repstart(adap);
286
                        }
287
                };
288
 
289
                if (i) {
290
                        buf[i - 1] = i2c_inb(adap);
291
                } else {
292
                        i2c_inb(adap); /* dummy read */
293
                }
294
        }
295
 
296
        return (i - 1);
297
}
298
 
299
 
300
static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
301
                                struct i2c_msg *msg, int retries)
302
{
303
        unsigned short flags = msg->flags;
304
        unsigned char addr;
305
        int ret;
306
        if ( (flags & I2C_M_TEN)  ) {
307
                /* a ten bit address */
308
                addr = 0xf0 | (( msg->addr >> 7) & 0x03);
309
                DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
310
                /* try extended address code...*/
311
                ret = try_address(adap, addr, retries);
312
                if (ret!=1) {
313
                        printk(KERN_ERR "died at extended address code.\n");
314
                        return -EREMOTEIO;
315
                }
316
                /* the remaining 8 bit address */
317
                i2c_outb(adap,msg->addr & 0x7f);
318
/* Status check comes here */
319
                if (ret != 1) {
320
                        printk(KERN_ERR "died at 2nd address code.\n");
321
                        return -EREMOTEIO;
322
                }
323
                if ( flags & I2C_M_RD ) {
324
                        i2c_repstart(adap);
325
                        /* okay, now switch into reading mode */
326
                        addr |= 0x01;
327
                        ret = try_address(adap, addr, retries);
328
                        if (ret!=1) {
329
                                printk(KERN_ERR "died at extended address code.\n");
330
                                return -EREMOTEIO;
331
                        }
332
                }
333
        } else {                /* normal 7bit address  */
334
                addr = ( msg->addr << 1 );
335
                if (flags & I2C_M_RD )
336
                        addr |= 1;
337
                if (flags & I2C_M_REV_DIR_ADDR )
338
                        addr ^= 1;
339
                i2c_outb(adap, addr);
340
        }
341
        return 0;
342
}
343
 
344
static int pcf_xfer(struct i2c_adapter *i2c_adap,
345
                    struct i2c_msg msgs[],
346
                    int num)
347
{
348
        struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
349
        struct i2c_msg *pmsg;
350
        int i;
351
        int ret=0, timeout, status;
352
 
353
 
354
        /* Check for bus busy */
355
        timeout = wait_for_bb(adap);
356
        if (timeout) {
357
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
358
                            "Timeout waiting for BB in pcf_xfer\n");)
359
                return -EIO;
360
        }
361
 
362
        for (i = 0;ret >= 0 && i < num; i++) {
363
                pmsg = &msgs[i];
364
 
365
                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
366
                     pmsg->flags & I2C_M_RD ? "read" : "write",
367
                     pmsg->len, pmsg->addr, i + 1, num);)
368
 
369
                ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
370
 
371
                /* Send START */
372
                if (i == 0) {
373
                        i2c_start(adap);
374
                }
375
 
376
                /* Wait for PIN (pending interrupt NOT) */
377
                timeout = wait_for_pin(adap, &status);
378
                if (timeout) {
379
                        i2c_stop(adap);
380
                        DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
381
                                    "for PIN(1) in pcf_xfer\n");)
382
                        return (-EREMOTEIO);
383
                }
384
 
385
#ifndef STUB_I2C
386
                /* Check LRB (last rcvd bit - slave ack) */
387
                if (status & I2C_PCF_LRB) {
388
                        i2c_stop(adap);
389
                        DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
390
                        return (-EREMOTEIO);
391
                }
392
#endif
393
 
394
                DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
395
                            i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
396
 
397
                /* Read */
398
                if (pmsg->flags & I2C_M_RD) {
399
                        /* read bytes into buffer*/
400
                        ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
401
                                            (i + 1 == num));
402
 
403
                        if (ret != pmsg->len) {
404
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
405
                                            "only read %d bytes.\n",ret));
406
                        } else {
407
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
408
                        }
409
                } else { /* Write */
410
                        ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
411
                                            (i + 1 == num));
412
 
413
                        if (ret != pmsg->len) {
414
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
415
                                            "only wrote %d bytes.\n",ret));
416
                        } else {
417
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
418
                        }
419
                }
420
        }
421
 
422
        return (i);
423
}
424
 
425
static u32 pcf_func(struct i2c_adapter *adap)
426
{
427
        return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
428
               I2C_FUNC_PROTOCOL_MANGLING;
429
}
430
 
431
/* -----exported algorithm data: -------------------------------------  */
432
 
433
static struct i2c_algorithm pcf_algo = {
434
        .name           = "PCF8584 algorithm",
435
        .id             = I2C_ALGO_PCF,
436
        .master_xfer    = pcf_xfer,
437
        .functionality  = pcf_func,
438
};
439
 
440
/*
441
 * registering functions to load algorithms at runtime
442
 */
443
int i2c_pcf_add_bus(struct i2c_adapter *adap)
444
{
445
        struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
446
        int rval;
447
 
448
        DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
449
 
450
        /* register new adapter to i2c module... */
451
 
452
        adap->id |= pcf_algo.id;
453
        adap->algo = &pcf_algo;
454
 
455
        adap->timeout = 100;            /* default values, should       */
456
        adap->retries = 3;              /* be replaced by defines       */
457
 
458
        rval = pcf_init_8584(pcf_adap);
459
        if (!rval)
460
                i2c_add_adapter(adap);
461
        return rval;
462
}
463
 
464
 
465
int i2c_pcf_del_bus(struct i2c_adapter *adap)
466
{
467
        return i2c_del_adapter(adap);
468
}
469
 
470
EXPORT_SYMBOL(i2c_pcf_add_bus);
471
EXPORT_SYMBOL(i2c_pcf_del_bus);
472
 
473
MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
474
MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
475
MODULE_LICENSE("GPL");
476
 
477
MODULE_PARM(i2c_debug,"i");
478
MODULE_PARM_DESC(i2c_debug,
479
        "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");