root/i2c/trunk/kernel/i2c-algo-bit.c @ 3653

Revision 3653, 16.2 KB (checked in by mds, 11 years ago)

(mds)

So I should check this change in, right?

yep - should do no harm to do it the right way ;)

Simon

DI Simon Vogl wrote:

Mark D. Studebaker wrote:

Dori Eldar of Intel reports:

---------------
Speaking of i2c-algo-bit.c there's a bug in line 147: instead of
setscl(1) you need also to wait for the slave
to raise his CLK line so instead you need to cal sclhi(), which does
just
that.

---------------

Is this a bug?


It seems so, indeed. I have not come across a situation where this has played
a role, but this should be the clean way to do it - for devices that don't hold
down the scl line, it makes no difference.

Simon

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
24/* $Id$ */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/delay.h>
29#include <linux/slab.h>
30#include <linux/version.h>
31#include <linux/init.h>
32#include <asm/uaccess.h>
33#include <linux/ioport.h>
34#include <linux/errno.h>
35#include <linux/sched.h>
36#include "i2c.h"
37#include "i2c-algo-bit.h"
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/* debugging - slow down transfer to have a look at the data ..         */
47/* I use this with two leds&resistors, each one connected to sda,scl    */
48/* respectively. This makes sure that the algorithm works. Some chips   */
49/* might not like this, as they have an internal timeout of some mils   */
50/*
51#define SLO_IO      jif=jiffies;while(jiffies<=jif+i2c_table[minor].veryslow)\
52                        if (need_resched) schedule();
53*/
54
55
56/* ----- global variables --------------------------------------------- */
57
58#ifdef SLO_IO
59        int jif;
60#endif
61
62/* module parameters:
63 */
64static int i2c_debug;
65static int bit_test;    /* see if the line-setting functions work       */
66static int bit_scan;    /* have a look at what's hanging 'round         */
67
68/* --- setting states on the bus with the right timing: --------------- */
69
70#define setsda(adap,val) adap->setsda(adap->data, val)
71#define setscl(adap,val) adap->setscl(adap->data, val)
72#define getsda(adap) adap->getsda(adap->data)
73#define getscl(adap) adap->getscl(adap->data)
74
75static inline void sdalo(struct i2c_algo_bit_data *adap)
76{
77        setsda(adap,0);
78        udelay(adap->udelay);
79}
80
81static inline void sdahi(struct i2c_algo_bit_data *adap)
82{
83        setsda(adap,1);
84        udelay(adap->udelay);
85}
86
87static inline void scllo(struct i2c_algo_bit_data *adap)
88{
89        setscl(adap,0);
90        udelay(adap->udelay);
91#ifdef SLO_IO
92        SLO_IO
93#endif
94}
95
96/*
97 * Raise scl line, and do checking for delays. This is necessary for slower
98 * devices.
99 */
100static inline int sclhi(struct i2c_algo_bit_data *adap)
101{
102        int start=jiffies;
103
104        setscl(adap,1);
105
106        udelay(adap->udelay);
107
108        /* Not all adapters have scl sense line... */
109        if (adap->getscl == NULL )
110                return 0;
111
112        while (! getscl(adap) ) {       
113                /* the hw knows how to read the clock line,
114                 * so we wait until it actually gets high.
115                 * This is safer as some chips may hold it low
116                 * while they are processing data internally.
117                 */
118                setscl(adap,1);
119                if (start+adap->timeout <= jiffies) {
120                        return -ETIMEDOUT;
121                }
122                if (current->need_resched)
123                        schedule();
124        }
125        DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies-start));
126#ifdef SLO_IO
127        SLO_IO
128#endif
129        return 0;
130} 
131
132
133/* --- other auxiliary functions -------------------------------------- */
134static void i2c_start(struct i2c_algo_bit_data *adap) 
135{
136        /* assert: scl, sda are high */
137        DEBPROTO(printk("S "));
138        sdalo(adap);
139        scllo(adap);
140}
141
142static void i2c_repstart(struct i2c_algo_bit_data *adap) 
143{
144        /* scl, sda may not be high */
145        DEBPROTO(printk(" Sr "));
146        setsda(adap,1);
147        sclhi(adap);
148        udelay(adap->udelay);
149       
150        sdalo(adap);
151        scllo(adap);
152}
153
154
155static void i2c_stop(struct i2c_algo_bit_data *adap) 
156{
157        DEBPROTO(printk("P\n"));
158        /* assert: scl is low */
159        sdalo(adap);
160        sclhi(adap); 
161        sdahi(adap);
162}
163
164
165
166/* send a byte without start cond., look for arbitration,
167   check ackn. from slave */
168/* returns:
169 * 1 if the device acknowledged
170 * 0 if the device did not ack
171 * -ETIMEDOUT if an error occurred (while raising the scl line)
172 */
173static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
174{
175        int i;
176        int sb;
177        int ack;
178        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
179
180        /* assert: scl is low */
181        DEB2(printk(KERN_DEBUG " i2c_outb:%2.2X\n",c&0xff));
182        for ( i=7 ; i>=0 ; i-- ) {
183                sb = c & ( 1 << i );
184                setsda(adap,sb);
185                udelay(adap->udelay);
186                DEBPROTO(printk(KERN_DEBUG "%d",sb!=0));
187                if (sclhi(adap)<0) { /* timed out */
188                        sdahi(adap); /* we don't want to block the net */
189                        return -ETIMEDOUT;
190                };
191                /* do arbitration here:
192                 * if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
193                 */
194                setscl(adap, 0 );
195                udelay(adap->udelay);
196        }
197        sdahi(adap);
198        if (sclhi(adap)<0){ /* timeout */
199                return -ETIMEDOUT;
200        };
201        /* read ack: SDA should be pulled down by slave */
202        ack=getsda(adap);       /* ack: sda is pulled low ->success.     */
203        DEB2(printk(KERN_DEBUG " i2c_outb: getsda() =  0x%2.2x\n", ~ack ));
204
205        DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) );
206        DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") );
207        scllo(adap);
208        return 0==ack;          /* return 1 if device acked      */
209        /* assert: scl is low (sda undef) */
210}
211
212
213static int i2c_inb(struct i2c_adapter *i2c_adap) 
214{
215        /* read byte via i2c port, without start/stop sequence  */
216        /* acknowledge is sent in i2c_read.                     */
217        int i;
218        unsigned char indata=0;
219        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
220
221        /* assert: scl is low */
222        DEB2(printk(KERN_DEBUG "i2c_inb.\n"));
223
224        sdahi(adap);
225        for (i=0;i<8;i++) {
226                if (sclhi(adap)<0) { /* timeout */
227                        return -ETIMEDOUT;
228                };
229                indata *= 2;
230                if ( getsda(adap) ) 
231                        indata |= 0x01;
232                scllo(adap);
233        }
234        /* assert: scl is low */
235        DEBPROTO(printk(KERN_DEBUG " %2.2x", indata & 0xff));
236        return (int) (indata & 0xff);
237}
238
239/*
240 * Sanity check for the adapter hardware - check the reaction of
241 * the bus lines only if it seems to be idle.
242 */
243static int test_bus(struct i2c_algo_bit_data *adap, char* name) {
244        int scl,sda;
245        sda=getsda(adap);
246        if (adap->getscl==NULL) {
247                printk(KERN_WARNING "i2c-algo-bit.o: Warning: Adapter can't read from clock line - skipping test.\n");
248                return 0;               
249        }
250        scl=getscl(adap);
251        printk(KERN_INFO "i2c-algo-bit.o: Adapter: %s scl: %d  sda: %d -- testing...\n",
252               name,getscl(adap),getsda(adap));
253        if (!scl || !sda ) {
254                printk(KERN_INFO " i2c-algo-bit.o: %s seems to be busy.\n",name);
255                goto bailout;
256        }
257        sdalo(adap);
258        printk(KERN_DEBUG "i2c-algo-bit.o:1 scl: %d  sda: %d \n",getscl(adap),
259               getsda(adap));
260        if ( 0 != getsda(adap) ) {
261                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA stuck high!\n",name);
262                sdahi(adap);
263                goto bailout;
264        }
265        if ( 0 == getscl(adap) ) {
266                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL unexpected low while pulling SDA low!\n",
267                        name);
268                goto bailout;
269        }               
270        sdahi(adap);
271        printk(KERN_DEBUG "i2c-algo-bit.o:2 scl: %d  sda: %d \n",getscl(adap),
272               getsda(adap));
273        if ( 0 == getsda(adap) ) {
274                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA stuck low!\n",name);
275                sdahi(adap);
276                goto bailout;
277        }
278        if ( 0 == getscl(adap) ) {
279                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL unexpected low while SDA high!\n",
280                       name);
281        goto bailout;
282        }
283        scllo(adap);
284        printk(KERN_DEBUG "i2c-algo-bit.o:3 scl: %d  sda: %d \n",getscl(adap),
285               getsda(adap));
286        if ( 0 != getscl(adap) ) {
287                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL stuck high!\n",name);
288                sclhi(adap);
289                goto bailout;
290        }
291        if ( 0 == getsda(adap) ) {
292                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA unexpected low while pulling SCL low!\n",
293                        name);
294                goto bailout;
295        }
296        sclhi(adap);
297        printk(KERN_DEBUG "i2c-algo-bit.o:4 scl: %d  sda: %d \n",getscl(adap),
298               getsda(adap));
299        if ( 0 == getscl(adap) ) {
300                printk(KERN_WARNING "i2c-algo-bit.o: %s SCL stuck low!\n",name);
301                sclhi(adap);
302                goto bailout;
303        }
304        if ( 0 == getsda(adap) ) {
305                printk(KERN_WARNING "i2c-algo-bit.o: %s SDA unexpected low while SCL high!\n",
306                        name);
307                goto bailout;
308        }
309        printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name);
310        return 0;
311bailout:
312        sdahi(adap);
313        sclhi(adap);
314        return -ENODEV;
315}
316
317/* ----- Utility functions
318 */
319
320/* try_address tries to contact a chip for a number of
321 * times before it gives up.
322 * return values:
323 * 1 chip answered
324 * 0 chip did not answer
325 * -x transmission error
326 */
327static inline int try_address(struct i2c_adapter *i2c_adap,
328                       unsigned char addr, int retries)
329{
330        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
331        int i,ret = -1;
332        for (i=0;i<=retries;i++) {
333                ret = i2c_outb(i2c_adap,addr);
334                if (ret==1)
335                        break;  /* success! */
336                i2c_stop(adap);
337                udelay(5/*adap->udelay*/);
338                if (i==retries)  /* no success */
339                        break;
340                i2c_start(adap);
341                udelay(adap->udelay);
342        }
343        DEB2(if (i) printk(KERN_DEBUG "i2c-algo-bit.o: needed %d retries for %d\n",
344                           i,addr));
345        return ret;
346}
347
348static int sendbytes(struct i2c_adapter *i2c_adap,const char *buf, int count)
349{
350        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
351        char c;
352        const char *temp = buf;
353        int retval;
354        int wrcount=0;
355
356        while (count > 0) {
357                c = *temp;
358                DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: %s i2c_write: writing %2.2X\n",
359                            i2c_adap->name, c&0xff));
360                retval = i2c_outb(i2c_adap,c);
361                if (retval>0) {
362                        count--; 
363                        temp++;
364                        wrcount++;
365                } else { /* arbitration or no acknowledge */
366                        printk(KERN_ERR "i2c-algo-bit.o: %s i2c_write: error - bailout.\n",
367                               i2c_adap->name);
368                        i2c_stop(adap);
369                        return (retval<0)? retval : -EFAULT;
370                                /* got a better one ?? */
371                }
372#if 0
373                /* from asm/delay.h */
374                __delay(adap->mdelay * (loops_per_sec / 1000) );
375#endif
376        }
377        return wrcount;
378}
379
380static inline int readbytes(struct i2c_adapter *i2c_adap,char *buf,int count)
381{
382        char *temp = buf;
383        int inval;
384        int rdcount=0;          /* counts bytes read */
385        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
386
387        while (count > 0) {
388                inval = i2c_inb(i2c_adap);
389/*printk("%#02x ",inval); if ( ! (count % 16) ) printk("\n"); */
390                if (inval>=0) {
391                        *temp = inval;
392                        rdcount++;
393                } else {   /* read timed out */
394                        printk(KERN_ERR "i2c-algo-bit.o: i2c_read: i2c_inb timed out.\n");
395                        break;
396                }
397
398                if ( count > 1 ) {              /* send ack */
399                        sdalo(adap);
400                        DEBPROTO(printk(" Am "));
401                } else {
402                        sdahi(adap);    /* neg. ack on last byte */
403                        DEBPROTO(printk(" NAm "));
404                }
405                if (sclhi(adap)<0) {    /* timeout */
406                        sdahi(adap);
407                        printk(KERN_ERR "i2c-algo-bit.o: i2c_read: Timeout at ack\n");
408                        return -ETIMEDOUT;
409                };
410                scllo(adap);
411                sdahi(adap);
412                temp++;
413                count--;
414        }
415        return rdcount;
416}
417
418/* doAddress initiates the transfer by generating the start condition (in
419 * try_address) and transmits the address in the necessary format to handle
420 * reads, writes as well as 10bit-addresses.
421 * returns:
422 *  0 everything went okay, the chip ack'ed
423 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
424 *      -ETIMEDOUT, for example if the lines are stuck...)
425 */
426static inline int bit_doAddress(struct i2c_adapter *i2c_adap,
427                                struct i2c_msg *msg, int retries) 
428{
429        unsigned short flags = msg->flags;
430        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
431
432        unsigned char addr;
433        int ret;
434        if ( (flags & I2C_M_TEN)  ) { 
435                /* a ten bit address */
436                addr = 0xf0 | (( msg->addr >> 7) & 0x03);
437                DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
438                /* try extended address code...*/
439                ret = try_address(i2c_adap, addr, retries);
440                if (ret!=1) {
441                        printk(KERN_ERR "died at extended address code.\n");
442                        return -EREMOTEIO;
443                }
444                /* the remaining 8 bit address */
445                ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
446                if (ret != 1) {
447                        /* the chip did not ack / xmission error occurred */
448                        printk(KERN_ERR "died at 2nd address code.\n");
449                        return -EREMOTEIO;
450                }
451                if ( flags & I2C_M_RD ) {
452                        i2c_repstart(adap);
453                        /* okay, now switch into reading mode */
454                        addr |= 0x01;
455                        ret = try_address(i2c_adap, addr, retries);
456                        if (ret!=1) {
457                                printk(KERN_ERR "died at extended address code.\n");
458                                return -EREMOTEIO;
459                        }
460                }
461        } else {                /* normal 7bit address  */
462                addr = ( msg->addr << 1 );
463                if (flags & I2C_M_RD )
464                        addr |= 1;
465                if (flags & I2C_M_REV_DIR_ADDR )
466                        addr ^= 1;
467                ret = try_address(i2c_adap, addr, retries);
468                if (ret!=1) {
469                        return -EREMOTEIO;
470                }
471        }
472        return 0;
473}
474
475static int bit_xfer(struct i2c_adapter *i2c_adap,
476                    struct i2c_msg msgs[], int num)
477{
478        struct i2c_msg *pmsg;
479        struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
480       
481        int i,ret;
482
483        i2c_start(adap);
484        for (i=0;i<num;i++) {
485                pmsg = &msgs[i];
486                if (!(pmsg->flags & I2C_M_NOSTART)) {
487                        if (i) {
488                                i2c_repstart(adap);
489                        }
490                        ret = bit_doAddress(i2c_adap,pmsg,i2c_adap->retries);
491                        if (ret != 0) {
492                                DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device adr %#2x msg #%d\n"
493                                       ,msgs[i].addr,i));
494                                return (ret<0) ? ret : -EREMOTEIO;
495                        }
496                }
497                if (pmsg->flags & I2C_M_RD ) {
498                        /* read bytes into buffer*/
499                        ret = readbytes(i2c_adap,pmsg->buf,pmsg->len);
500                        DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret));
501                        if (ret < pmsg->len ) {
502                                return (ret<0)? ret : -EREMOTEIO;
503                        }
504                } else {
505                        /* write bytes from buffer */
506                        ret = sendbytes(i2c_adap,pmsg->buf,pmsg->len);
507                        DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret));
508                        if (ret < pmsg->len ) {
509                                return (ret<0) ? ret : -EREMOTEIO;
510                        }
511                }
512        }
513        i2c_stop(adap);
514        return num;
515}
516
517static int algo_control(struct i2c_adapter *adapter, 
518        unsigned int cmd, unsigned long arg)
519{
520        return 0;
521}
522
523static u32 bit_func(struct i2c_adapter *adap)
524{
525        return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | 
526               I2C_FUNC_PROTOCOL_MANGLING;
527}
528
529
530/* -----exported algorithm data: -------------------------------------  */
531
532static struct i2c_algorithm i2c_bit_algo = {
533        "Bit-shift algorithm",
534        I2C_ALGO_BIT,
535        bit_xfer,
536        NULL,
537        NULL,                           /* slave_xmit           */
538        NULL,                           /* slave_recv           */
539        algo_control,                   /* ioctl                */
540        bit_func,                       /* functionality        */
541};
542
543/*
544 * registering functions to load algorithms at runtime
545 */
546int i2c_bit_add_bus(struct i2c_adapter *adap)
547{
548        int i;
549        struct i2c_algo_bit_data *bit_adap = adap->algo_data;
550
551        if (bit_test) {
552                int ret = test_bus(bit_adap, adap->name);
553                if (ret<0)
554                        return -ENODEV;
555        }
556
557        DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: hw routines for %s registered.\n",
558                    adap->name));
559
560        /* register new adapter to i2c module... */
561
562        adap->id |= i2c_bit_algo.id;
563        adap->algo = &i2c_bit_algo;
564
565        adap->timeout = 100;    /* default values, should       */
566        adap->retries = 3;      /* be replaced by defines       */
567
568        /* scan bus */
569        if (bit_scan) {
570                int ack;
571                printk(KERN_INFO " i2c-algo-bit.o: scanning bus %s.\n",
572                       adap->name);
573                for (i = 0x00; i < 0xff; i+=2) {
574                        i2c_start(bit_adap);
575                        ack = i2c_outb(adap,i);
576                        i2c_stop(bit_adap);
577                        if (ack>0) {
578                                printk("(%02x)",i>>1); 
579                        } else 
580                                printk("."); 
581                }
582                printk("\n");
583        }
584
585#ifdef MODULE
586        MOD_INC_USE_COUNT;
587#endif
588        i2c_add_adapter(adap);
589
590        return 0;
591}
592
593
594int i2c_bit_del_bus(struct i2c_adapter *adap)
595{
596        int res;
597
598        if ((res = i2c_del_adapter(adap)) < 0)
599                return res;
600
601        DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: adapter unregistered: %s\n",adap->name));
602
603#ifdef MODULE
604        MOD_DEC_USE_COUNT;
605#endif
606        return 0;
607}
608
609int __init i2c_algo_bit_init (void)
610{
611        printk(KERN_INFO "i2c-algo-bit.o: i2c bit algorithm module version %s (%s)\n", I2C_VERSION, I2C_DATE);
612        return 0;
613}
614
615
616
617EXPORT_SYMBOL(i2c_bit_add_bus);
618EXPORT_SYMBOL(i2c_bit_del_bus);
619
620#ifdef MODULE
621MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
622MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
623#ifdef MODULE_LICENSE
624MODULE_LICENSE("GPL");
625#endif
626
627MODULE_PARM(bit_test, "i");
628MODULE_PARM(bit_scan, "i");
629MODULE_PARM(i2c_debug,"i");
630
631MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
632MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus");
633MODULE_PARM_DESC(i2c_debug,
634            "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");
635
636int init_module(void) 
637{
638        return i2c_algo_bit_init();
639}
640
641void cleanup_module(void) 
642{
643}
644#endif
Note: See TracBrowser for help on using the browser.