OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [i2c/] [i2c-algo-pcf.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* ------------------------------------------------------------------------- */
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
#include <linux/kernel.h>
31
#include <linux/module.h>
32
#include <linux/delay.h>
33
#include <linux/slab.h>
34
#include <linux/init.h>
35
#include <asm/uaccess.h>
36
#include <linux/ioport.h>
37
#include <linux/errno.h>
38
#include <linux/sched.h>
39
 
40
#include <linux/i2c.h>
41
#include <linux/i2c-algo-pcf.h>
42
#include "i2c-pcf8584.h"
43
 
44
/* ----- global defines ----------------------------------------------- */
45
#define DEB(x) if (i2c_debug>=1) x
46
#define DEB2(x) if (i2c_debug>=2) x
47
#define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
48
#define DEBPROTO(x) if (i2c_debug>=9) x;
49
        /* debug the protocol by showing transferred bits */
50
#define DEF_TIMEOUT 16
51
 
52
/* module parameters:
53
 */
54
static int i2c_debug=0;
55
static int pcf_scan=0;   /* have a look at what's hanging 'round         */
56
 
57
/* --- setting states on the bus with the right timing: --------------- */
58
 
59
#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
60
#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
61
#define get_own(adap) adap->getown(adap->data)
62
#define get_clock(adap) adap->getclock(adap->data)
63
#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
64
#define i2c_inb(adap) adap->getpcf(adap->data, 0)
65
 
66
/* --- other auxiliary functions -------------------------------------- */
67
 
68
static void i2c_start(struct i2c_algo_pcf_data *adap)
69
{
70
        DEBPROTO(printk("S "));
71
        set_pcf(adap, 1, I2C_PCF_START);
72
}
73
 
74
static void i2c_repstart(struct i2c_algo_pcf_data *adap)
75
{
76
        DEBPROTO(printk(" Sr "));
77
        set_pcf(adap, 1, I2C_PCF_REPSTART);
78
}
79
 
80
 
81
static void i2c_stop(struct i2c_algo_pcf_data *adap)
82
{
83
        DEBPROTO(printk("P\n"));
84
        set_pcf(adap, 1, I2C_PCF_STOP);
85
}
86
 
87
 
88
static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
89
 
90
        int timeout = DEF_TIMEOUT;
91
        int status;
92
 
93
        status = get_pcf(adap, 1);
94
#ifndef STUB_I2C
95
        while (timeout-- && !(status & I2C_PCF_BB)) {
96
                udelay(100); /* wait for 100 us */
97
                status = get_pcf(adap, 1);
98
        }
99
#endif
100
        if (timeout <= 0) {
101
                printk(KERN_ERR "Timeout waiting for Bus Busy\n");
102
        }
103
 
104
        return (timeout<=0);
105
}
106
 
107
 
108
static inline void pcf_sleep(unsigned long timeout)
109
{
110
        schedule_timeout( timeout * HZ);
111
}
112
 
113
 
114
static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
115
 
116
        int timeout = DEF_TIMEOUT;
117
 
118
        *status = get_pcf(adap, 1);
119
#ifndef STUB_I2C
120
        while (timeout-- && (*status & I2C_PCF_PIN)) {
121
                adap->waitforpin();
122
                *status = get_pcf(adap, 1);
123
        }
124
#endif
125
        if (timeout <= 0)
126
                return(-1);
127
        else
128
                return(0);
129
}
130
 
131
/*
132
 * This should perform the 'PCF8584 initialization sequence' as described
133
 * in the Philips IC12 data book (1995, Aug 29).
134
 * There should be a 30 clock cycle wait after reset, I assume this
135
 * has been fulfilled.
136
 * There should be a delay at the end equal to the longest I2C message
137
 * to synchronize the BB-bit (in multimaster systems). How long is
138
 * this? I assume 1 second is always long enough.
139
 *
140
 * vdovikin: added detect code for PCF8584
141
 */
142
static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
143
{
144
        unsigned char temp;
145
 
146
        DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
147
 
148
        /* S1=0x80: S0 selected, serial interface off */
149
        set_pcf(adap, 1, I2C_PCF_PIN);
150
        /* check to see S1 now used as R/W ctrl -
151
           PCF8584 does that when ESO is zero */
152
        /* PCF also resets PIN bit */
153
        if ((temp = get_pcf(adap, 1)) != (0)) {
154
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
155
                return -ENXIO; /* definetly not PCF8584 */
156
        }
157
 
158
        /* load own address in S0, effective address is (own << 1)      */
159
        i2c_outb(adap, get_own(adap));
160
        /* check it's realy writen */
161
        if ((temp = i2c_inb(adap)) != get_own(adap)) {
162
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
163
                return -ENXIO;
164
        }
165
 
166
        /* S1=0xA0, next byte in S2                                     */
167
        set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
168
        /* check to see S2 now selected */
169
        if ((temp = get_pcf(adap, 1)) != I2C_PCF_ES1) {
170
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
171
                return -ENXIO;
172
        }
173
 
174
        /* load clock register S2                                       */
175
        i2c_outb(adap, get_clock(adap));
176
        /* check it's realy writen, the only 5 lowest bits does matter */
177
        if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
178
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
179
                return -ENXIO;
180
        }
181
 
182
        /* Enable serial interface, idle, S0 selected                   */
183
        set_pcf(adap, 1, I2C_PCF_IDLE);
184
 
185
        /* check to see PCF is realy idled and we can access status register */
186
        if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
187
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
188
                return -ENXIO;
189
        }
190
 
191
        printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
192
 
193
        return 0;
194
}
195
 
196
 
197
/* ----- Utility functions
198
 */
199
 
200
static inline int try_address(struct i2c_algo_pcf_data *adap,
201
                       unsigned char addr, int retries)
202
{
203
        int i, status, ret = -1;
204
        for (i=0;i<retries;i++) {
205
                i2c_outb(adap, addr);
206
                i2c_start(adap);
207
                status = get_pcf(adap, 1);
208
                if (wait_for_pin(adap, &status) >= 0) {
209
                        if ((status & I2C_PCF_LRB) == 0) {
210
                                i2c_stop(adap);
211
                                break;  /* success! */
212
                        }
213
                }
214
                i2c_stop(adap);
215
                udelay(adap->udelay);
216
        }
217
        DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
218
                           addr));
219
        return ret;
220
}
221
 
222
 
223
static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
224
                         int count, int last)
225
{
226
        struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
227
        int wrcount, status, timeout;
228
 
229
        for (wrcount=0; wrcount<count; ++wrcount) {
230
                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: %s i2c_write: writing %2.2X\n",
231
                      i2c_adap->name, buf[wrcount]&0xff));
232
                i2c_outb(adap, buf[wrcount]);
233
                timeout = wait_for_pin(adap, &status);
234
                if (timeout) {
235
                        i2c_stop(adap);
236
                        printk(KERN_ERR "i2c-algo-pcf.o: %s i2c_write: "
237
                               "error - timeout.\n", i2c_adap->name);
238
                        return -EREMOTEIO; /* got a better one ?? */
239
                }
240
#ifndef STUB_I2C
241
                if (status & I2C_PCF_LRB) {
242
                        i2c_stop(adap);
243
                        printk(KERN_ERR "i2c-algo-pcf.o: %s i2c_write: "
244
                               "error - no ack.\n", i2c_adap->name);
245
                        return -EREMOTEIO; /* got a better one ?? */
246
                }
247
#endif
248
        }
249
        if (last) {
250
                i2c_stop(adap);
251
        }
252
        else {
253
                i2c_repstart(adap);
254
        }
255
 
256
        return (wrcount);
257
}
258
 
259
 
260
static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
261
                         int count, int last)
262
{
263
        int i, status;
264
        struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
265
 
266
        /* increment number of bytes to read by one -- read dummy byte */
267
        for (i = 0; i <= count; i++) {
268
 
269
                if (wait_for_pin(adap, &status)) {
270
                        i2c_stop(adap);
271
                        printk(KERN_ERR "i2c-algo-pcf.o: pcf_readbytes timed out.\n");
272
                        return (-1);
273
                }
274
 
275
#ifndef STUB_I2C
276
                if ((status & I2C_PCF_LRB) && (i != count)) {
277
                        i2c_stop(adap);
278
                        printk(KERN_ERR "i2c-algo-pcf.o: i2c_read: i2c_inb, No ack.\n");
279
                        return (-1);
280
                }
281
#endif
282
 
283
                if (i == count - 1) {
284
                        set_pcf(adap, 1, I2C_PCF_ESO);
285
                } else
286
                if (i == count) {
287
                        if (last) {
288
                                i2c_stop(adap);
289
                        } else {
290
                                i2c_repstart(adap);
291
                        }
292
                };
293
 
294
                if (i) {
295
                        buf[i - 1] = i2c_inb(adap);
296
                } else {
297
                        i2c_inb(adap); /* dummy read */
298
                }
299
        }
300
 
301
        return (i - 1);
302
}
303
 
304
 
305
static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
306
                                struct i2c_msg *msg, int retries)
307
{
308
        unsigned short flags = msg->flags;
309
        unsigned char addr;
310
        int ret;
311
        if ( (flags & I2C_M_TEN)  ) {
312
                /* a ten bit address */
313
                addr = 0xf0 | (( msg->addr >> 7) & 0x03);
314
                DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
315
                /* try extended address code...*/
316
                ret = try_address(adap, addr, retries);
317
                if (ret!=1) {
318
                        printk(KERN_ERR "died at extended address code.\n");
319
                        return -EREMOTEIO;
320
                }
321
                /* the remaining 8 bit address */
322
                i2c_outb(adap,msg->addr & 0x7f);
323
/* Status check comes here */
324
                if (ret != 1) {
325
                        printk(KERN_ERR "died at 2nd address code.\n");
326
                        return -EREMOTEIO;
327
                }
328
                if ( flags & I2C_M_RD ) {
329
                        i2c_repstart(adap);
330
                        /* okay, now switch into reading mode */
331
                        addr |= 0x01;
332
                        ret = try_address(adap, addr, retries);
333
                        if (ret!=1) {
334
                                printk(KERN_ERR "died at extended address code.\n");
335
                                return -EREMOTEIO;
336
                        }
337
                }
338
        } else {                /* normal 7bit address  */
339
                addr = ( msg->addr << 1 );
340
                if (flags & I2C_M_RD )
341
                        addr |= 1;
342
                if (flags & I2C_M_REV_DIR_ADDR )
343
                        addr ^= 1;
344
                i2c_outb(adap, addr);
345
        }
346
        return 0;
347
}
348
 
349
static int pcf_xfer(struct i2c_adapter *i2c_adap,
350
                    struct i2c_msg msgs[],
351
                    int num)
352
{
353
        struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
354
        struct i2c_msg *pmsg;
355
        int i;
356
        int ret=0, timeout, status;
357
 
358
 
359
        /* Check for bus busy */
360
        timeout = wait_for_bb(adap);
361
        if (timeout) {
362
                DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
363
                            "Timeout waiting for BB in pcf_xfer\n");)
364
                return -EIO;
365
        }
366
 
367
        for (i = 0;ret >= 0 && i < num; i++) {
368
                pmsg = &msgs[i];
369
 
370
                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
371
                     pmsg->flags & I2C_M_RD ? "read" : "write",
372
                     pmsg->len, pmsg->addr, i + 1, num);)
373
 
374
                ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
375
 
376
                /* Send START */
377
                if (i == 0) {
378
                        i2c_start(adap);
379
                }
380
 
381
                /* Wait for PIN (pending interrupt NOT) */
382
                timeout = wait_for_pin(adap, &status);
383
                if (timeout) {
384
                        i2c_stop(adap);
385
                        DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
386
                                    "for PIN(1) in pcf_xfer\n");)
387
                        return (-EREMOTEIO);
388
                }
389
 
390
#ifndef STUB_I2C
391
                /* Check LRB (last rcvd bit - slave ack) */
392
                if (status & I2C_PCF_LRB) {
393
                        i2c_stop(adap);
394
                        DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
395
                        return (-EREMOTEIO);
396
                }
397
#endif
398
 
399
                DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
400
                            i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
401
 
402
                /* Read */
403
                if (pmsg->flags & I2C_M_RD) {
404
                        /* read bytes into buffer*/
405
                        ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
406
                                            (i + 1 == num));
407
 
408
                        if (ret != pmsg->len) {
409
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
410
                                            "only read %d bytes.\n",ret));
411
                        } else {
412
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
413
                        }
414
                } else { /* Write */
415
                        ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
416
                                            (i + 1 == num));
417
 
418
                        if (ret != pmsg->len) {
419
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
420
                                            "only wrote %d bytes.\n",ret));
421
                        } else {
422
                                DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
423
                        }
424
                }
425
        }
426
 
427
        return (i);
428
}
429
 
430
static int algo_control(struct i2c_adapter *adapter,
431
        unsigned int cmd, unsigned long arg)
432
{
433
        return 0;
434
}
435
 
436
static u32 pcf_func(struct i2c_adapter *adap)
437
{
438
        return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
439
               I2C_FUNC_PROTOCOL_MANGLING;
440
}
441
 
442
/* -----exported algorithm data: -------------------------------------  */
443
 
444
static struct i2c_algorithm pcf_algo = {
445
        "PCF8584 algorithm",
446
        I2C_ALGO_PCF,
447
        pcf_xfer,
448
        NULL,
449
        NULL,                           /* slave_xmit           */
450
        NULL,                           /* slave_recv           */
451
        algo_control,                   /* ioctl                */
452
        pcf_func,                       /* functionality        */
453
};
454
 
455
/*
456
 * registering functions to load algorithms at runtime
457
 */
458
int i2c_pcf_add_bus(struct i2c_adapter *adap)
459
{
460
        int i, status;
461
        struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
462
 
463
        DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: hw routines for %s registered.\n",
464
                    adap->name));
465
 
466
        /* register new adapter to i2c module... */
467
 
468
        adap->id |= pcf_algo.id;
469
        adap->algo = &pcf_algo;
470
 
471
        adap->timeout = 100;            /* default values, should       */
472
        adap->retries = 3;              /* be replaced by defines       */
473
 
474
        if ((i = pcf_init_8584(pcf_adap))) {
475
                return i;
476
        }
477
 
478
#ifdef MODULE
479
        MOD_INC_USE_COUNT;
480
#endif
481
 
482
        i2c_add_adapter(adap);
483
 
484
        /* scan bus */
485
        if (pcf_scan) {
486
                printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s.\n",
487
                       adap->name);
488
                for (i = 0x00; i < 0xff; i+=2) {
489
                        if (wait_for_bb(pcf_adap)) {
490
                        printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s - TIMEOUTed.\n",
491
                           adap->name);
492
                            break;
493
                        }
494
                        i2c_outb(pcf_adap, i);
495
                        i2c_start(pcf_adap);
496
                        if ((wait_for_pin(pcf_adap, &status) >= 0) &&
497
                            ((status & I2C_PCF_LRB) == 0)) {
498
                                printk("(%02x)",i>>1);
499
                        } else {
500
                                printk(".");
501
                        }
502
                        i2c_stop(pcf_adap);
503
                        udelay(pcf_adap->udelay);
504
                }
505
                printk("\n");
506
        }
507
        return 0;
508
}
509
 
510
 
511
int i2c_pcf_del_bus(struct i2c_adapter *adap)
512
{
513
        int res;
514
        if ((res = i2c_del_adapter(adap)) < 0)
515
                return res;
516
        DEB2(printk("i2c-algo-pcf.o: adapter unregistered: %s\n",adap->name));
517
 
518
#ifdef MODULE
519
        MOD_DEC_USE_COUNT;
520
#endif
521
        return 0;
522
}
523
 
524
int __init i2c_algo_pcf_init (void)
525
{
526
        printk("i2c-algo-pcf.o: i2c pcf8584 algorithm module\n");
527
        return 0;
528
}
529
 
530
 
531
EXPORT_SYMBOL(i2c_pcf_add_bus);
532
EXPORT_SYMBOL(i2c_pcf_del_bus);
533
 
534
#ifdef MODULE
535
MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
536
MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
537
MODULE_LICENSE("GPL");
538
 
539
MODULE_PARM(pcf_scan, "i");
540
MODULE_PARM(i2c_debug,"i");
541
 
542
MODULE_PARM_DESC(pcf_scan, "Scan for active chips on the bus");
543
MODULE_PARM_DESC(i2c_debug,
544
        "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
545
 
546
 
547
int init_module(void)
548
{
549
        return i2c_algo_pcf_init();
550
}
551
 
552
void cleanup_module(void)
553
{
554
}
555
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.