root/i2c/trunk/kernel/i2c-algo-biths.c @ 3750

Revision 3750, 19.3 KB (checked in by kmalkki, 10 years ago)

(Kyösti) Moved code sections from .h to .c as there was no reason to

keep them inlined.

Fix protocol byte dump. Compile bit-level protocol dump in by default.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* ------------------------------------------------------------------------- */
2/* i2c-algo-biths.c i2c driver algorithms for bit-shift adapters             */
3/* ------------------------------------------------------------------------- */
4/*   Copyright (C) 1995-2000 Simon G. Vogl
5     Copyright (C) 2002-2003 Kyösti Mälkki
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/* $Id$ */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/version.h>
29#include <linux/init.h>
30#include <asm/uaccess.h>
31#include <linux/ioport.h>
32#include <linux/errno.h>
33#include <linux/sched.h>
34#include "i2c.h"
35#include "i2c-algo-biths.h"
36
37/* ----- global defines ----------------------------------------------- */
38
39/* If non-zero, adapter code written for original i2c-algo-bit can be used unmodified.
40 * As this export same symbols, you should either remove i2c-algo-bit.o from depmod
41 * directories, or load this module manually.
42 */
43#ifndef ALGO_BIT_COMPATIBILITY
44#define ALGO_BIT_COMPATIBILITY 0
45#endif
46
47#define FATAL_BUS       0
48#define MODULE_STATUS   1
49#define FATAL_MSG       2
50#define FATAL_PROTOCOL  3
51#define ALL_MSG         4
52#define ALL_PROTOCOL    5
53#define BIT_LEVEL       9
54
55#define DEB1(x)         if (i2c_debug>=MODULE_STATUS) (x);
56static void proto_s(char *d, const char *s) { strcat(d, s); }
57static void proto_x(char *d, const char *x, unsigned char y) { while (*d) d++; sprintf(d, x, y); }
58#define PROTO_S(x)      if (adap->dstr) proto_s(adap->dstr, x)
59#define PROTO_B(x)      if ((adap->dstr) && (i2c_debug>=BIT_LEVEL)) proto_s(adap->dstr, x)
60#define PROTO_X(x,y)    if (adap->dstr) proto_x(adap->dstr, x, y)
61
62#define PROTO_MAX_DUMP 1024  // 50 x ".oooooooo [xx] .i[xx]"
63
64/* ----- global variables --------------------------------------------- */
65
66/* module parameters:
67 */
68static int i2c_debug; 
69static int bit_test;    /* see if the line-setting functions work       */
70
71/* Bus timing for 50/50 duty cycle :  T_setup + T_hold = T_scllo = T_sclhi */
72/* Run setscl/setsda with a special flag */
73#define T_min   0               /* after any SCL SDA change     */
74#define T_sclhi _HS_DBL_DT      /* SCL high                     */
75#define T_scllo _HS_DBL_DT      /* SCL low                      */
76#define T_setup 0               /* SDA change to SCL rise       */
77#define T_hold  0               /* SCL fall to SDA change       */
78
79#define _sf(a)          adap->ctrl|=(a)
80#define _cf(a)          adap->ctrl&=~(a)
81#define ___setscl(b)    if (b) _sf(_HS_SCL); else _cf(_HS_SCL); i2c_setscl(adap)
82#define ___setsda(b)    if (b) _sf(_HS_SDA); else _cf(_HS_SDA); i2c_setsda(adap)
83
84#define __setdt(x,dt)   if (dt) _sf(dt); x; if (dt) _cf(dt)
85#define __setscl(b,dt)  __setdt(___setscl(b),dt)
86#define __setsda(b,dt)  __setdt(___setsda(b),dt)
87#define __getscl()      i2c_getscl(adap)
88#define __getsda()      i2c_getsda(adap)
89
90#define RETURN_ON_FAILURE(x)            x; if (adap->errors) return
91#define TRY(x) RETURN_ON_FAILURE(x)
92
93#define _setscl(b,dt)   RETURN_ON_FAILURE(__setscl(b,dt))
94#define _setsda(b,dt)   RETURN_ON_FAILURE(__setsda(b,dt))
95#define _getscl         __getscl
96#define _getsda         __getsda
97
98#define _sclhi(dt)      _setscl(1,dt)
99#define _scllo(dt)      _setscl(0,dt)
100#define _sdahi(dt)      _setsda(1,dt)
101#define _sdalo(dt)      _setsda(0,dt)
102
103/* --- setting states on the bus with the right timing: --------------- */
104
105static int i2c_sda_set(struct i2c_algo_biths_data *adap, int rdcount)
106{
107    int sda;
108    /* allow some rise/fall time */
109    while ( rdcount-- ) {
110        sda = adap->getsda(adap->hw_data);
111        if (adap->ctrl & _HS_SDA) {
112            if (sda)
113                return 0;
114            if (!rdcount) {
115                adap->errors |= _HS_SDA_ARB;
116                return -1;
117            }
118        } else { /* !(adap->ctrl & _HS_SDA) */
119            if (!sda)
120                return 0;
121            if (!rdcount) {
122                adap->errors |= _HS_HW_FAIL;
123                return -1;
124            }
125        }
126    }
127    return 0;
128}
129
130static void i2c_setsda(struct i2c_algo_biths_data *adap)
131{
132    adap->setstate(adap); 
133    adap->setsda(adap->hw_data, adap->hw_state);
134    if ( !(adap->ctrl & _HS_SDA_FREE) && ! i2c_sda_set(adap, 10)) {
135        return;
136    }
137    adap->set_timer(adap);
138    adap->run_timer(adap);
139}
140
141static int i2c_getscl(struct i2c_algo_biths_data *adap)
142{
143    return adap->getscl(adap->hw_data);
144}
145
146static int i2c_getsda(struct i2c_algo_biths_data *adap)
147{
148    return adap->getsda(adap->hw_data);
149}
150/*
151 * Raise scl line, and do check for delays. This is necessary for slower
152 * devices.
153 */
154
155static void i2c_setscl(struct i2c_algo_biths_data *adap)
156{
157#ifndef HW_CANNOT_READ_SCL /* Not all adapters have scl sense line... */
158    int rdcount = 10;
159    adap->setstate(adap);
160    adap->setscl(adap->hw_data, adap->hw_state);
161    if (adap->ctrl & _HS_SCL) {
162        unsigned long start;
163        /* allow some rise time */
164        while (rdcount && !adap->getscl(adap->hw_data)) rdcount--;
165        /* else clock synchronisation, give more time */
166        start = jiffies;
167        while (!rdcount && !adap->getscl(adap->hw_data)) {
168            if ( time_after(jiffies, start+adap->timeout) ) {
169                adap->errors |= _HS_TIMEOUT; /* scl undef */
170                return;
171            }
172#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
173            if (current->need_resched)
174                schedule();
175#else
176            cond_resched();
177#endif
178        }
179        adap->set_timer(adap);
180        /* test for SDA arbitration when SCL is high */
181        if ( !(adap->ctrl & _HS_SDA_FREE) && ! i2c_sda_set(adap, 1)) {
182            return;
183        }
184    } else {
185        /* allow some fall time */
186        while (rdcount && adap->getscl(adap->hw_data)) rdcount--;
187        if ( !rdcount ) {
188            adap->errors |= _HS_HW_FAIL;
189            return;
190        }
191        adap->set_timer(adap);
192    }
193#else
194    adap->setstate(adap);
195    adap->setscl(adap->hw_data, adap->hw_state);
196    adap->set_timer(adap);
197#endif /* HW_CANNOT_READ_SCL */
198    adap->run_timer(adap);
199}
200
201
202/* start, repstart */
203static void i2c_start(struct i2c_algo_biths_data *adap)
204{
205        PROTO_S("S");
206        /* assert: scl, sda undef */
207        adap->errors = 0;
208        _sdahi(T_setup);
209        _sclhi(T_min);
210        _sdalo(T_min);
211        _scllo(T_hold);
212        /* assert: scl, sda low */
213}
214
215static void i2c_stop(struct i2c_algo_biths_data *adap)
216{
217        PROTO_S(" P");
218        /* scl undef after error, sda, scl freedom unknown */
219        adap->ctrl &= ~(_HS_SDA_FREE | _HS_DBL_DT);
220        if (adap->errors) {
221            adap->errors = 0;
222            _scllo(T_hold);
223        }
224        /* assert: scl low, sda undef */
225        _sdalo(T_setup);
226        _sclhi(T_min);
227        _sdahi(T_min);
228        /* assert: scl, sda high */
229}
230
231
232static void i2c_outbits(struct i2c_algo_biths_data *adap, int i) 
233{
234        /* assert: scl is low */
235        PROTO_B(".");
236        while  (i--) {
237            PROTO_B("o");
238            _setsda(adap->shiftreg & 0x80, T_setup);
239            _sclhi(T_sclhi);
240            _scllo(T_hold);
241            adap->shiftreg<<=1;
242        }
243        /* assert: scl is low */
244}
245
246static void i2c_inbits(struct i2c_algo_biths_data *adap, int i)
247{
248        /* assert: scl is low, sda undef */
249        adap->ctrl |= _HS_SDA_FREE;
250        PROTO_B(".");
251        while  (i--) {
252            PROTO_B("i");
253            _sdahi(T_setup); 
254            _sclhi(T_sclhi);   
255            adap->shiftreg<<=1;
256            if (_getsda())
257                adap->shiftreg |= 0x01;
258            _scllo(T_hold);
259        }
260        adap->ctrl &= ~_HS_SDA_FREE;
261        /* assert: scl is low */
262}
263
264static void i2c_outb(struct i2c_algo_biths_data *adap, unsigned short flags,
265                     char *buf, int *count) 
266{
267        while (*count) {
268            adap->shiftreg = *buf;
269            TRY(i2c_outbits(adap, 8));
270            PROTO_X(" %02X ", *buf);
271            buf++;
272           
273            /* read ack: SDA should be pulled down by slave */
274            TRY(i2c_inbits(adap, 1));
275           
276            if (! (adap->shiftreg & 0x01)) { 
277                PROTO_S("[A]");
278            } else if (flags & I2C_M_IGNORE_NAK) {
279                PROTO_S("[NA]");
280            } else {
281                PROTO_S("[NA]");
282                adap->errors |= _HS_NAK;
283            }
284            if (adap->errors) return;
285            (*count)--;
286        }
287}
288
289static void i2c_inb(struct i2c_algo_biths_data *adap, unsigned short flags,
290                    char *buf, int *count) 
291{
292        while (*count) {
293            TRY(i2c_inbits(adap, 8));
294            *buf = adap->shiftreg;
295            PROTO_X(" [%02X] ", *buf);
296            buf++;
297           
298            if (! (flags & I2C_M_NO_RD_ACK)) {
299                if (*count == 1) /* was last */
300                    adap->shiftreg = 0x80;
301                else
302                    adap->shiftreg = 0x00;
303                TRY(i2c_outbits(adap,1));
304
305                if (*count == 1) {
306                    PROTO_S("NA");
307                } else {
308                    PROTO_S("A");
309                }
310            }
311            (*count)--;
312        }
313}
314
315
316static void debug_protocol(struct i2c_algo_biths_data *adap, int retval)
317{
318    if (! adap->dstr) return;
319
320    if ( ((retval<0) && (i2c_debug>=FATAL_PROTOCOL)) || 
321         (i2c_debug>=ALL_PROTOCOL) ) {
322        printk(KERN_DEBUG "i2c-algo-biths.o: %s: %s\n", adap->name, adap->dstr); 
323    }
324    *adap->dstr = 0;
325}
326
327static const char * i2c_strerr(int retval)
328{ 
329    switch (retval) {
330        case 2:
331            return "ack";
332               
333        case 1:
334            return "no ack (ignored)";
335
336        case 0:
337            return "not reached";
338
339        case -EREMOTEIO:
340            return "no ack";
341
342        case -ETIMEDOUT:
343            return "SCL rise timeout";
344         
345        case -ECOMM:
346            return "SDA arbitration";
347
348        case -ENODEV:
349            return "SCL/SDA failure";
350
351        default:
352            return "unknown";
353    }
354}
355
356static int errflag(int flags)
357{
358    if (! flags)
359        return 2;
360    if (flags & _HS_HW_FAIL)
361        return -ENODEV;
362    if (flags & _HS_SDA_ARB)
363        return -ECOMM;
364    if (flags & _HS_TIMEOUT)
365        return -ETIMEDOUT;
366    if (flags & _HS_NAK)
367        return -EREMOTEIO;
368}
369
370static void debug_printout(struct i2c_adapter *i2c_adap, int num, int retval)
371{ 
372    if ( ((retval<0) && (i2c_debug>=FATAL_MSG)) ||
373         (i2c_debug>=ALL_MSG) ) {
374        printk(KERN_ERR "i2c-algo-biths.o: %s: msg #%d %s\n", i2c_adap->name, num, i2c_strerr(retval));
375    }
376}
377
378/*
379 * Sanity check for the adapter hardware
380 */
381static int test_bus(struct i2c_algo_biths_data *adap)
382{
383
384    int sscl, ssda, gscl, gsda, i=0;
385    int errors;
386    int test[][2] = {{1,1}, {0,1}, {1,1}, {1,0}, {1,1}, {-1,-1}}; // SDA, SCL pair
387
388    printk(KERN_INFO "i2c-algo-biths.o: %s bus test\n", adap->name);
389
390    while ( test[i][0]!=-1 ) {
391        ssda = test[i][0];
392        sscl = test[i][1];
393
394        adap->errors = 0;
395        __setsda(ssda, T_min);
396        gsda = __getsda() ? 1 : 0;
397        errors = adap->errors & (_HS_HW_FAIL | _HS_SDA_ARB);
398        if (errors)
399            printk(KERN_WARNING "i2c-algo-biths.o: %s %s\n", adap->name,
400                   i2c_strerr(errflag(errors)));
401           
402        __setscl(sscl, T_min);
403        gscl = __getscl() ? 1 : 0;
404        errors = adap->errors & (_HS_HW_FAIL | _HS_TIMEOUT);
405        if (errors)
406            printk(KERN_WARNING "i2c-algo-biths.o: %s %s\n", adap->name,
407                   i2c_strerr(errflag(errors)));
408
409        printk(KERN_DEBUG "i2c-algo-biths.o: %s SCL: %d  SDA: %d\n", adap->name, gscl, gsda);
410        if ( sscl!=gscl )
411            printk(KERN_WARNING "i2c-algo-biths.o: %s SCL set %d, got %d!\n", adap->name, sscl, gscl);
412        if ( ssda!=gsda )
413            printk(KERN_WARNING "i2c-algo-biths.o: %s SDA set %d, got %d!\n", adap->name, ssda, gsda);
414        if ( (adap->errors) || sscl!=gscl || ssda!=gsda )
415            break;
416        i++;
417    }
418   
419    __setsda(1,0);
420    __setscl(1,0);
421    adap->errors = 0;
422
423    if (test[i][0]==-1) {
424        printk(KERN_INFO "i2c-algo-biths.o: %s passed bus test.\n",adap->name);
425        return 0;
426    } else {
427        printk(KERN_INFO "i2c-algo-biths.o: %s failed bus test.\n",adap->name);
428        return -ENODEV;
429    }
430}
431
432
433/* doAddress transmits the address in the necessary format to handle
434 * reads, writes as well as 10bit-addresses.
435 */
436
437static void doAddress(struct i2c_algo_biths_data *adap, struct i2c_msg *msg) 
438{
439        unsigned char addr[2];
440        int count;
441
442        if ( msg->flags & I2C_M_TEN ) { 
443                /* a ten bit address */
444                count = 2;
445                addr[0] = 0xf0 | (( msg->addr >> 7) & 0x03);
446                addr[1] = msg->addr & 0x7f;
447
448                /* try extended address code ... and the remaining 8 bit address */
449                TRY(i2c_outb(adap, msg->flags, addr, &count));
450
451                if ( msg->flags & I2C_M_RD ) {
452                        TRY(i2c_start(adap));
453                       
454                        /* okay, now switch into reading mode */
455                        count = 1;
456                        addr[0] |= 0x01;
457                        TRY(i2c_outb(adap, msg->flags, addr, &count));
458                }
459        } else {                /* normal 7bit address  */
460                count = 1;
461                addr[0] = ( msg->addr << 1 );
462                if (msg->flags & I2C_M_RD )
463                        addr[0] |= 1;
464                if (msg->flags & I2C_M_REV_DIR_ADDR )
465                        addr[0] ^= 1;
466                TRY(i2c_outb(adap, msg->flags, addr, &count));
467        }
468}
469
470/*
471 * return values:
472 * 1 ACK
473 * 0 IGNORED client NAK
474 * -EREMOTEIO  client NAK
475 * -ETIMEDOUT  from sclhi()
476 */
477
478
479static int bit_xfer(struct i2c_adapter *i2c_adap,
480                    struct i2c_msg msgs[], int num)
481{
482        struct i2c_msg *msg = msgs;
483        struct i2c_algo_biths_data *adap = i2c_adap->algo_data;
484        int mn, j, state;
485        enum { MSG_INIT, MSG_START, MSG_ADDRESS, MSG_DATA, MSG_READY, MSG_STOP, MSG_EXIT };
486
487        state = MSG_INIT;
488        mn=0;
489
490        adap->dstr=NULL;
491        if (i2c_debug>=FATAL_PROTOCOL) {
492            adap->dstr = kmalloc(PROTO_MAX_DUMP, GFP_KERNEL);
493            if (adap->dstr) {
494                *adap->dstr = 0;
495            } else {
496                printk(KERN_DEBUG "i2c-algo-biths.o %s: missing protocol dump (-ENOMEM)\n", adap->name);
497            }
498        }
499
500        for (j=0; j<num; j++) msgs[j].err = 0; // unprocessed
501
502        do switch (state) {
503           
504            case MSG_INIT:
505                msg = &msgs[mn];
506                if ((msg->flags & I2C_M_NOSTART) && (mn)) {
507                    state = MSG_DATA;
508                    break;
509                }
510
511            case MSG_START:     
512                i2c_start(adap);
513                if (adap->errors) {
514                    state = MSG_STOP;   
515                    break;
516                }       
517                   
518            case MSG_ADDRESS:
519                doAddress(adap, msg);
520                if (adap->errors) {
521                    state = MSG_STOP;
522                    break;
523                }
524
525            case MSG_DATA:
526                j = msg->len;
527                if ( msg->flags & I2C_M_RD ) {
528                    i2c_inb(adap, msg->flags, msg->buf, &j);
529                } else {
530                    i2c_outb(adap, msg->flags, msg->buf, &j);
531                }
532                msg->done = msg->len - j;
533                if (adap->errors) {
534                    state = MSG_STOP;
535                    break;
536                }
537       
538            case MSG_READY:
539                mn++;
540                if (mn<num) {
541                    msg->err = errflag(adap->errors);
542                    debug_protocol(adap, msg->err);
543                    state = MSG_INIT;
544                    break;
545                }
546
547            case MSG_STOP:
548                msg->err = errflag(adap->errors);
549                i2c_stop(adap);
550                j = 0;
551                while (adap->errors) {
552                    if ( ++j > 10) {
553                        msg->err = -ENODEV;
554                        break;
555                    }
556                    i2c_stop(adap);
557                }
558                debug_protocol(adap, msg->err);
559                state = MSG_EXIT;
560                break;
561
562            default: /* not reached */
563                state = MSG_EXIT;
564                msg->err = -EINVAL;
565                break;
566                                 
567        } while (state != MSG_EXIT);
568       
569        if (adap->dstr) kfree(adap->dstr);
570
571        for (j=0; j<num; j++)
572            debug_printout(i2c_adap, j, msgs[j].err);
573
574        return (msg->err < 0) ? msg->err : mn;
575}
576
577static int algo_control(struct i2c_adapter *i2c_adap, 
578        unsigned int cmd, unsigned long arg)
579{
580        return 0;
581}
582
583static u32 bit_func(struct i2c_adapter *i2c_adap)
584{
585        return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | 
586               I2C_FUNC_PROTOCOL_MANGLING;
587}
588
589
590/* -----exported algorithm data: -------------------------------------  */
591
592static struct i2c_algorithm i2c_algo_biths = {
593        "Bit-shift algorithm",
594        I2C_ALGO_BIT,
595        bit_xfer,
596        NULL,
597        NULL,                           /* slave_xmit           */
598        NULL,                           /* slave_recv           */
599        algo_control,                   /* ioctl                */
600        bit_func,                       /* functionality        */
601};
602
603
604#if ALGO_BIT_COMPATIBILITY
605#include "i2c-algo-bit.h"
606
607static _HS_ATTR_ void _old_setscl(void *hw_data, int hw_state)
608{
609        struct i2c_algo_bit_data *old = hw_data;
610        old->setscl(old->data, hw_state & _HS_SCL);
611}
612static _HS_ATTR_ void _old_setsda(void *hw_data, int hw_state)
613{
614        struct i2c_algo_bit_data *old = hw_data;
615        old->setsda(old->data, hw_state & _HS_SDA);
616}
617static _HS_ATTR_ int _old_getscl(void *hw_data)
618{
619        struct i2c_algo_bit_data *old = hw_data;
620        return old->getscl(old->data);
621}
622static _HS_ATTR_ int _old_getsda(void *hw_data)
623{
624        struct i2c_algo_bit_data *old = hw_data;
625        return old->getsda(old->data);
626}
627
628static _HS_ATTR_ void _old_setstate(struct i2c_algo_biths_data *adap)
629{
630        adap->hw_state = adap->ctrl;
631}
632
633static struct i2c_algo_biths_data _old_template = {
634    setstate:  _old_setstate,
635    setsda:  _old_setsda,
636    setscl:  _old_setscl,
637    getsda:  _old_getsda,
638    getscl:  _old_getscl,
639};
640
641/*
642 * registering functions to load algorithms at runtime
643 */
644int i2c_bit_add_bus(struct i2c_adapter *i2c_adap)
645{
646        int i;
647        struct i2c_algo_bit_data *old_adap = i2c_adap->algo_data;
648        struct i2c_algo_biths_data *adap;
649
650        adap = kmalloc(sizeof(struct i2c_algo_biths_data), GFP_KERNEL);
651        if (adap==NULL)
652            return -ENOMEM;
653
654        memcpy(adap, &_old_template, sizeof(struct i2c_algo_biths_data));
655        adap->hw_data = old_adap;
656        adap->xloops = old_adap->udelay * 0x0863; /* 1/4 vs 1/2 cycle, 0x10c6 / 2 */
657        adap->timeout = old_adap->timeout;
658        i2c_adap->algo_data = adap;
659
660        return i2c_biths_add_bus(i2c_adap);
661}
662
663
664int i2c_bit_del_bus(struct i2c_adapter *i2c_adap)
665{
666        int res;
667
668        if ((res = i2c_biths_del_bus(i2c_adap)) < 0)
669            return res;
670
671        kfree(i2c_adap->algo_data);
672        return 0;
673}
674
675EXPORT_SYMBOL(i2c_bit_add_bus);
676EXPORT_SYMBOL(i2c_bit_del_bus);
677
678#endif
679
680#ifdef rdtscl
681/* TSC stuff from arch/i386/lib/delay.c */
682
683static _HS_ATTR_ void i2c_tsc_set(struct i2c_algo_biths_data *adap)
684{
685    rdtscl(adap->bclock);
686}
687
688static _HS_ATTR_ void i2c_tsc_run(struct i2c_algo_biths_data *adap)
689{
690    unsigned long now, loops, xloops;
691    int d0;
692    xloops = adap->xloops; 
693    __asm__("mull %0"
694            :"=d" (xloops), "=&a" (d0)
695            :"1" (xloops),"0" (current_cpu_data.loops_per_jiffy));
696    loops = xloops * HZ;
697    do
698    {
699        rep_nop();
700        rdtscl(now);
701    } while ( (now - adap->bclock) < loops );
702}
703
704#else
705
706static _HS_ATTR_ void i2c_udelay_set(struct i2c_algo_biths_data *adap) {}
707
708static _HS_ATTR_ void i2c_udelay_run(struct i2c_algo_biths_data *adap)
709{
710    int usecs;
711    /* adap->xloops = usecs * 0x000010c6 / 2; */
712    usecs = ((adap->xloops + 0x0863) >> 12); 
713    if (adap->ctrl & _HS_DBL_DT)
714        usecs<<=1;
715    udelay(usecs);
716}
717
718#endif /* rdtscl */
719
720
721/*
722 * registering functions to load algorithms at runtime
723 */
724int i2c_biths_add_bus(struct i2c_adapter *i2c_adap)
725{
726        int i;
727        struct i2c_algo_biths_data *adap = i2c_adap->algo_data;
728
729        /* get name */
730        adap->name = i2c_adap->name;
731        adap->dstr = 0; // no protocol dump buffer
732
733        if (adap->set_timer == NULL) {
734#ifdef rdtscl   /* if (x86_udelay_tsc) here instead ? */
735                adap->set_timer = i2c_tsc_set;
736                adap->run_timer = i2c_tsc_run;
737#else
738                adap->set_timer = i2c_udelay_set;
739                adap->run_timer = i2c_udelay_run;
740#endif
741        }
742
743        if (bit_test) {
744            int ret = test_bus(adap);
745            if (ret<0)
746                return ret;
747        }
748
749        DEB1(printk(KERN_DEBUG "i2c-algo-biths.o: hw routines for %s registered.\n",
750                    i2c_adap->name));
751       
752        /* register new adapter to i2c module... */
753
754        i2c_adap->id |= i2c_algo_biths.id;
755        i2c_adap->algo = &i2c_algo_biths;
756
757        i2c_adap->timeout = HZ; /* default values, should       */
758        i2c_adap->retries = 3;  /* be replaced by defines       */
759
760#ifdef MODULE
761        MOD_INC_USE_COUNT;
762#endif
763        i2c_add_adapter(i2c_adap);
764
765        return 0;
766}
767
768
769int i2c_biths_del_bus(struct i2c_adapter *i2c_adap)
770{
771        int res;
772
773        if ((res = i2c_del_adapter(i2c_adap)) < 0)
774                return res;
775
776        DEB1(printk(KERN_DEBUG "i2c-algo-biths.o: adapter unregistered: %s\n",i2c_adap->name));
777
778#ifdef MODULE
779        MOD_DEC_USE_COUNT;
780#endif
781        return 0;
782}
783
784int __init i2c_algo_biths_init (void)
785{
786        printk(KERN_INFO "i2c-algo-biths.o: i2c high-speed bit algorithm module version %s (%s)\n", I2C_VERSION, I2C_DATE);
787#ifdef rdtscl
788        DEB1(printk(KERN_DEBUG "i2c-algo-biths.o:  ... will use rdtscl() for bus clock\n"));
789#else
790        DEB1(printk(KERN_DEBUG "i2c-algo-biths.o:  ... will use udelay() for bus clock\n");
791#endif
792        return 0;
793}
794
795
796
797EXPORT_SYMBOL(i2c_biths_add_bus);
798EXPORT_SYMBOL(i2c_biths_del_bus);
799
800#ifdef MODULE
801MODULE_AUTHOR("Kyösti Mälkki <kmalkki@cc.hut.fi>");
802MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
803#ifdef MODULE_LICENSE
804MODULE_LICENSE("GPL");
805#endif
806
807MODULE_PARM(bit_test, "i");
808MODULE_PARM(i2c_debug,"i");
809
810MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
811MODULE_PARM_DESC(i2c_debug,
812            "debug level - 1 use; 2 fatal, 3 +proto; 4 all, 5 +proto");
813
814int init_module(void) 
815{
816        return i2c_algo_biths_init();
817}
818
819void cleanup_module(void) 
820{
821}
822#endif
Note: See TracBrowser for help on using the browser.