OSDN Git Service

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