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