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