Subversion Repositories shark

Rev

Rev 420 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 giacomo 1
/* ------------------------------------------------------------------------- */
2
/* i2c-algo-bit.c i2c driver algorithms for bit-shift adapters               */
3
/* ------------------------------------------------------------------------- */
4
/*   Copyright (C) 1995-2000 Simon G. Vogl
5
 
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
19
/* ------------------------------------------------------------------------- */
20
 
21
/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
22
   Frodo Looijaard <frodol@dds.nl> */
23
 
515 giacomo 24
/* $Id: i2c-algo-bit.c,v 1.2 2004-03-20 12:22:37 giacomo Exp $ */
420 giacomo 25
 
26
/* #define DEBUG 1 */
27
 
28
#include <linux/kernel.h>
29
#include <linux/module.h>
30
#include <linux/delay.h>
31
#include <linux/slab.h>
32
#include <linux/init.h>
33
#include <linux/errno.h>
34
#include <linux/sched.h>
35
#include <linux/i2c.h>
36
#include <linux/i2c-algo-bit.h>
37
 
38
 
39
/* ----- global defines ----------------------------------------------- */
40
#define DEB(x) if (i2c_debug>=1) x;
41
#define DEB2(x) if (i2c_debug>=2) x;
42
#define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/
43
#define DEBPROTO(x) if (i2c_debug>=9) { x; }
44
        /* debug the protocol by showing transferred bits */
45
 
46
 
47
/* ----- global variables --------------------------------------------- */
48
 
49
/* module parameters:
50
 */
51
static int i2c_debug;
52
static int bit_test;    /* see if the line-setting functions work       */
53
 
54
/* --- setting states on the bus with the right timing: --------------- */
55
 
56
#define setsda(adap,val) adap->setsda(adap->data, val)
57
#define setscl(adap,val) adap->setscl(adap->data, val)
58
#define getsda(adap) adap->getsda(adap->data)
59
#define getscl(adap) adap->getscl(adap->data)
60
 
61
static inline void sdalo(struct i2c_algo_bit_data *adap)
62
{
63
        setsda(adap,0);
64
        udelay(adap->udelay);
65
}
66
 
67
static inline void sdahi(struct i2c_algo_bit_data *adap)
68
{
69
        setsda(adap,1);
70
        udelay(adap->udelay);
71
}
72
 
73
static inline void scllo(struct i2c_algo_bit_data *adap)
74
{
75
        setscl(adap,0);
76
        udelay(adap->udelay);
77
}
78
 
79
/*
80
 * Raise scl line, and do checking for delays. This is necessary for slower
81
 * devices.
82
 */
83
static inline int sclhi(struct i2c_algo_bit_data *adap)
84
{
85
        unsigned long start;
86
 
87
        setscl(adap,1);
88
 
89
        /* Not all adapters have scl sense line... */
90
        if (adap->getscl == NULL )
91
                return 0;
92
 
515 giacomo 93
        start=jiffies26;
420 giacomo 94
        while (! getscl(adap) ) {      
95
                /* the hw knows how to read the clock line,
96
                 * so we wait until it actually gets high.
97
                 * This is safer as some chips may hold it low
98
                 * while they are processing data internally.
99
                 */
515 giacomo 100
                if (time_after_eq(jiffies26, start+adap->timeout)) {
420 giacomo 101
                        return -ETIMEDOUT;
102
                }
515 giacomo 103
                //cond_resched();
420 giacomo 104
        }
515 giacomo 105
        DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies26-start));
420 giacomo 106
        udelay(adap->udelay);
107
        return 0;
108
}
109
 
110
 
111
/* --- other auxiliary functions -------------------------------------- */
112
static void i2c_start(struct i2c_algo_bit_data *adap)
113
{
114
        /* assert: scl, sda are high */
115
        DEBPROTO(printk("S "));
116
        sdalo(adap);
117
        scllo(adap);
118
}
119
 
120
static void i2c_repstart(struct i2c_algo_bit_data *adap)
121
{
122
        /* scl, sda may not be high */
123
        DEBPROTO(printk(" Sr "));
124
        setsda(adap,1);
125
        sclhi(adap);
126
        udelay(adap->udelay);
127
 
128
        sdalo(adap);
129
        scllo(adap);
130
}
131
 
132
 
133
static void i2c_stop(struct i2c_algo_bit_data *adap)
134
{
135
        DEBPROTO(printk("P\n"));
136
        /* assert: scl is low */
137
        sdalo(adap);
138
        sclhi(adap);
139
        sdahi(adap);
140
}
141
 
142
 
143
 
144
/* send a byte without start cond., look for arbitration,
145
   check ackn. from slave */
146
/* returns:
147
 * 1 if the device acknowledged
148
 * 0 if the device did not ack
149
 * -ETIMEDOUT if an error occurred (while raising the scl line)
150
 */
151
static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
152
{
153
        int i;
154
        int sb;
155
        int ack;
156
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
157
 
158
        /* assert: scl is low */
159
        for ( i=7 ; i>=0 ; i-- ) {
160
                sb = c & ( 1 << i );
161
                setsda(adap,sb);
162
                udelay(adap->udelay);
163
                DEBPROTO(printk(KERN_DEBUG "%d",sb!=0));
164
                if (sclhi(adap)<0) { /* timed out */
165
                        sdahi(adap); /* we don't want to block the net */
166
                        DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at bit #%d\n", c&0xff, i));
167
                        return -ETIMEDOUT;
168
                };
169
                /* do arbitration here:
170
                 * if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
171
                 */
172
                setscl(adap, 0 );
173
                udelay(adap->udelay);
174
        }
175
        sdahi(adap);
176
        if (sclhi(adap)<0){ /* timeout */
177
            DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at ack\n", c&0xff));
178
            return -ETIMEDOUT;
179
        };
180
        /* read ack: SDA should be pulled down by slave */
181
        ack=getsda(adap);       /* ack: sda is pulled low ->success.     */
182
        DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x , getsda() = %d\n", c & 0xff, ack));
183
 
184
        DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) );
185
        DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") );
186
        scllo(adap);
187
        return 0==ack;          /* return 1 if device acked      */
188
        /* assert: scl is low (sda undef) */
189
}
190
 
191
 
192
static int i2c_inb(struct i2c_adapter *i2c_adap)
193
{
194
        /* read byte via i2c port, without start/stop sequence  */
195
        /* acknowledge is sent in i2c_read.                     */
196
        int i;
197
        unsigned char indata=0;
198
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
199
 
200
        /* assert: scl is low */
201
        sdahi(adap);
202
        for (i=0;i<8;i++) {
203
                if (sclhi(adap)<0) { /* timeout */
204
                        DEB2(printk(KERN_DEBUG " i2c_inb: timeout at bit #%d\n", 7-i));
205
                        return -ETIMEDOUT;
206
                };
207
                indata *= 2;
208
                if ( getsda(adap) )
209
                        indata |= 0x01;
210
                scllo(adap);
211
        }
212
        /* assert: scl is low */
213
        DEB2(printk(KERN_DEBUG "i2c_inb: 0x%02x\n", indata & 0xff));
214
 
215
        DEBPROTO(printk(KERN_DEBUG " 0x%02x", indata & 0xff));
216
        return (int) (indata & 0xff);
217
}
218
 
219
/*
220
 * Sanity check for the adapter hardware - check the reaction of
221
 * the bus lines only if it seems to be idle.
222
 */
223
static int test_bus(struct i2c_algo_bit_data *adap, char* name) {
224
        int scl,sda;
225
        sda=getsda(adap);
226
        if (adap->getscl==NULL) {
227
                printk(KERN_WARNING "i2c-algo-bit.o: Warning: Adapter can't read from clock line - skipping test.\n");
228
                return 0;              
229
        }
230
        scl=getscl(adap);
231
        printk(KERN_INFO "i2c-algo-bit.o: Adapter: %s scl: %d  sda: %d -- testing...\n",
232
               name,getscl(adap),getsda(adap));
233
        if (!scl || !sda ) {
234
                printk(KERN_INFO " i2c-algo-bit.o: %s seems to be busy.\n",name);
235
                goto bailout;
236
        }
237
        sdalo(adap);
238
        printk(KERN_DEBUG "i2c-algo-bit.o:1 scl: %d  sda: %d \n",getscl(adap),
239
               getsda(adap));
240
        if ( 0 != getsda(adap) ) {
241
                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA stuck high!\n",name);
242
                sdahi(adap);
243
                goto bailout;
244
        }
245
        if ( 0 == getscl(adap) ) {
246
                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL unexpected low while pulling SDA low!\n",
247
                        name);
248
                goto bailout;
249
        }              
250
        sdahi(adap);
251
        printk(KERN_DEBUG "i2c-algo-bit.o:2 scl: %d  sda: %d \n",getscl(adap),
252
               getsda(adap));
253
        if ( 0 == getsda(adap) ) {
254
                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA stuck low!\n",name);
255
                sdahi(adap);
256
                goto bailout;
257
        }
258
        if ( 0 == getscl(adap) ) {
259
                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL unexpected low while SDA high!\n",
260
                       name);
261
        goto bailout;
262
        }
263
        scllo(adap);
264
        printk(KERN_DEBUG "i2c-algo-bit.o:3 scl: %d  sda: %d \n",getscl(adap),
265
               getsda(adap));
266
        if ( 0 != getscl(adap) ) {
267
                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL stuck high!\n",name);
268
                sclhi(adap);
269
                goto bailout;
270
        }
271
        if ( 0 == getsda(adap) ) {
272
                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA unexpected low while pulling SCL low!\n",
273
                        name);
274
                goto bailout;
275
        }
276
        sclhi(adap);
277
        printk(KERN_DEBUG "i2c-algo-bit.o:4 scl: %d  sda: %d \n",getscl(adap),
278
               getsda(adap));
279
        if ( 0 == getscl(adap) ) {
280
                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL stuck low!\n",name);
281
                sclhi(adap);
282
                goto bailout;
283
        }
284
        if ( 0 == getsda(adap) ) {
285
                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA unexpected low while SCL high!\n",
286
                        name);
287
                goto bailout;
288
        }
289
        printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name);
290
        return 0;
291
bailout:
292
        sdahi(adap);
293
        sclhi(adap);
294
        return -ENODEV;
295
}
296
 
297
/* ----- Utility functions
298
 */
299
 
300
/* try_address tries to contact a chip for a number of
301
 * times before it gives up.
302
 * return values:
303
 * 1 chip answered
304
 * 0 chip did not answer
305
 * -x transmission error
306
 */
307
static inline int try_address(struct i2c_adapter *i2c_adap,
308
                       unsigned char addr, int retries)
309
{
310
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
311
        int i,ret = -1;
312
        for (i=0;i<=retries;i++) {
313
                ret = i2c_outb(i2c_adap,addr);
314
                if (ret==1)
315
                        break;  /* success! */
316
                i2c_stop(adap);
317
                udelay(5/*adap->udelay*/);
318
                if (i==retries)  /* no success */
319
                        break;
320
                i2c_start(adap);
321
                udelay(adap->udelay);
322
        }
323
        DEB2(if (i)
324
             printk(KERN_DEBUG "i2c-algo-bit.o: Used %d tries to %s client at 0x%02x : %s\n",
325
                    i+1, addr & 1 ? "read" : "write", addr>>1,
326
                    ret==1 ? "success" : ret==0 ? "no ack" : "failed, timeout?" )
327
            );
328
        return ret;
329
}
330
 
331
static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
332
{
333
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
334
        char c;
335
        const char *temp = msg->buf;
336
        int count = msg->len;
337
        unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
338
        int retval;
339
        int wrcount=0;
340
 
341
        while (count > 0) {
342
                c = *temp;
343
                DEB2(dev_dbg(&i2c_adap->dev, "sendbytes: writing %2.2X\n", c&0xff));
344
                retval = i2c_outb(i2c_adap,c);
345
                if ((retval>0) || (nak_ok && (retval==0)))  { /* ok or ignored NAK */
346
                        count--;
347
                        temp++;
348
                        wrcount++;
349
                } else { /* arbitration or no acknowledge */
350
                        dev_err(&i2c_adap->dev, "sendbytes: error - bailout.\n");
351
                        i2c_stop(adap);
352
                        return (retval<0)? retval : -EFAULT;
353
                                /* got a better one ?? */
354
                }
355
#if 0
356
                /* from asm/delay.h */
357
                __delay(adap->mdelay * (loops_per_sec / 1000) );
358
#endif
359
        }
360
        return wrcount;
361
}
362
 
363
static inline int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
364
{
365
        int inval;
366
        int rdcount=0;          /* counts bytes read */
367
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
368
        char *temp = msg->buf;
369
        int count = msg->len;
370
 
371
        while (count > 0) {
372
                inval = i2c_inb(i2c_adap);
373
/*printk("%#02x ",inval); if ( ! (count % 16) ) printk("\n"); */
374
                if (inval>=0) {
375
                        *temp = inval;
376
                        rdcount++;
377
                } else {   /* read timed out */
378
                        printk(KERN_ERR "i2c-algo-bit.o: readbytes: i2c_inb timed out.\n");
379
                        break;
380
                }
381
 
382
                if ( count > 1 ) {              /* send ack */
383
                        sdalo(adap);
384
                        DEBPROTO(printk(" Am "));
385
                } else {
386
                        sdahi(adap);    /* neg. ack on last byte */
387
                        DEBPROTO(printk(" NAm "));
388
                }
389
                if (sclhi(adap)<0) {    /* timeout */
390
                        sdahi(adap);
391
                        printk(KERN_ERR "i2c-algo-bit.o: readbytes: Timeout at ack\n");
392
                        return -ETIMEDOUT;
393
                };
394
                scllo(adap);
395
                sdahi(adap);
396
                temp++;
397
                count--;
398
        }
399
        return rdcount;
400
}
401
 
402
/* doAddress initiates the transfer by generating the start condition (in
403
 * try_address) and transmits the address in the necessary format to handle
404
 * reads, writes as well as 10bit-addresses.
405
 * returns:
406
 *  0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
407
 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
408
 *      -ETIMEDOUT, for example if the lines are stuck...)
409
 */
410
static inline int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
411
{
412
        unsigned short flags = msg->flags;
413
        unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
414
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
415
 
416
        unsigned char addr;
417
        int ret, retries;
418
 
419
        retries = nak_ok ? 0 : i2c_adap->retries;
420
 
421
        if ( (flags & I2C_M_TEN)  ) {
422
                /* a ten bit address */
423
                addr = 0xf0 | (( msg->addr >> 7) & 0x03);
424
                DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
425
                /* try extended address code...*/
426
                ret = try_address(i2c_adap, addr, retries);
427
                if ((ret != 1) && !nak_ok)  {
428
                        printk(KERN_ERR "died at extended address code.\n");
429
                        return -EREMOTEIO;
430
                }
431
                /* the remaining 8 bit address */
432
                ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
433
                if ((ret != 1) && !nak_ok) {
434
                        /* the chip did not ack / xmission error occurred */
435
                        printk(KERN_ERR "died at 2nd address code.\n");
436
                        return -EREMOTEIO;
437
                }
438
                if ( flags & I2C_M_RD ) {
439
                        i2c_repstart(adap);
440
                        /* okay, now switch into reading mode */
441
                        addr |= 0x01;
442
                        ret = try_address(i2c_adap, addr, retries);
443
                        if ((ret!=1) && !nak_ok) {
444
                                printk(KERN_ERR "died at extended address code.\n");
445
                                return -EREMOTEIO;
446
                        }
447
                }
448
        } else {                /* normal 7bit address  */
449
                addr = ( msg->addr << 1 );
450
                if (flags & I2C_M_RD )
451
                        addr |= 1;
452
                if (flags & I2C_M_REV_DIR_ADDR )
453
                        addr ^= 1;
454
                ret = try_address(i2c_adap, addr, retries);
455
                if ((ret!=1) && !nak_ok)
456
                        return -EREMOTEIO;
457
        }
458
 
459
        return 0;
460
}
461
 
462
static int bit_xfer(struct i2c_adapter *i2c_adap,
463
                    struct i2c_msg msgs[], int num)
464
{
465
        struct i2c_msg *pmsg;
466
        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
467
 
468
        int i,ret;
469
        unsigned short nak_ok;
470
 
471
        i2c_start(adap);
472
        for (i=0;i<num;i++) {
473
                pmsg = &msgs[i];
474
                nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
475
                if (!(pmsg->flags & I2C_M_NOSTART)) {
476
                        if (i) {
477
                                i2c_repstart(adap);
478
                        }
479
                        ret = bit_doAddress(i2c_adap, pmsg);
480
                        if ((ret != 0) && !nak_ok) {
481
                            DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device addr %2.2x msg #%d\n"
482
                                        ,msgs[i].addr,i));
483
                            return (ret<0) ? ret : -EREMOTEIO;
484
                        }
485
                }
486
                if (pmsg->flags & I2C_M_RD ) {
487
                        /* read bytes into buffer*/
488
                        ret = readbytes(i2c_adap, pmsg);
489
                        DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret));
490
                        if (ret < pmsg->len ) {
491
                                return (ret<0)? ret : -EREMOTEIO;
492
                        }
493
                } else {
494
                        /* write bytes from buffer */
495
                        ret = sendbytes(i2c_adap, pmsg);
496
                        DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret));
497
                        if (ret < pmsg->len ) {
498
                                return (ret<0) ? ret : -EREMOTEIO;
499
                        }
500
                }
501
        }
502
        i2c_stop(adap);
503
        return num;
504
}
505
 
506
static u32 bit_func(struct i2c_adapter *adap)
507
{
508
        return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
509
               I2C_FUNC_PROTOCOL_MANGLING;
510
}
511
 
512
 
513
/* -----exported algorithm data: -------------------------------------  */
514
 
515
static struct i2c_algorithm i2c_bit_algo = {
516
        .name           = "Bit-shift algorithm",
517
        .id             = I2C_ALGO_BIT,
518
        .master_xfer    = bit_xfer,
519
        .functionality  = bit_func,
520
};
521
 
522
/*
523
 * registering functions to load algorithms at runtime
524
 */
525
int i2c_bit_add_bus(struct i2c_adapter *adap)
526
{
527
        struct i2c_algo_bit_data *bit_adap = adap->algo_data;
528
 
529
        if (bit_test) {
530
                int ret = test_bus(bit_adap, adap->name);
531
                if (ret<0)
532
                        return -ENODEV;
533
        }
534
 
535
        DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
536
 
537
        /* register new adapter to i2c module... */
538
 
539
        adap->id |= i2c_bit_algo.id;
540
        adap->algo = &i2c_bit_algo;
541
 
542
        adap->timeout = 100;    /* default values, should       */
543
        adap->retries = 3;      /* be replaced by defines       */
544
 
545
        i2c_add_adapter(adap);
546
        return 0;
547
}
548
 
549
 
550
int i2c_bit_del_bus(struct i2c_adapter *adap)
551
{
552
        return i2c_del_adapter(adap);
553
}
554
 
555
EXPORT_SYMBOL(i2c_bit_add_bus);
556
EXPORT_SYMBOL(i2c_bit_del_bus);
557
 
558
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
559
MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
560
MODULE_LICENSE("GPL");
561
 
562
MODULE_PARM(bit_test, "i");
563
MODULE_PARM(i2c_debug,"i");
564
 
565
MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
566
MODULE_PARM_DESC(i2c_debug,
567
                 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");