fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / i2c / algos / i2c-algo-pcf.c
blobecb2c2d7d540241745e27137f45da6b1cf52c081
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
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.
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.
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 /* ------------------------------------------------------------------------- */
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> */
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 */
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"
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
47 /* module parameters:
49 static int i2c_debug;
51 /* --- setting states on the bus with the right timing: --------------- */
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)
60 /* --- other auxiliary functions -------------------------------------- */
62 static void i2c_start(struct i2c_algo_pcf_data *adap)
64 DEBPROTO(printk("S "));
65 set_pcf(adap, 1, I2C_PCF_START);
68 static void i2c_repstart(struct i2c_algo_pcf_data *adap)
70 DEBPROTO(printk(" Sr "));
71 set_pcf(adap, 1, I2C_PCF_REPSTART);
75 static void i2c_stop(struct i2c_algo_pcf_data *adap)
77 DEBPROTO(printk("P\n"));
78 set_pcf(adap, 1, I2C_PCF_STOP);
81 static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
83 int timeout = DEF_TIMEOUT;
84 int status;
86 status = get_pcf(adap, 1);
87 #ifndef STUB_I2C
88 while (timeout-- && !(status & I2C_PCF_BB)) {
89 udelay(100); /* wait for 100 us */
90 status = get_pcf(adap, 1);
92 #endif
93 if (timeout <= 0) {
94 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
97 return (timeout<=0);
101 static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
103 int timeout = DEF_TIMEOUT;
105 *status = get_pcf(adap, 1);
106 #ifndef STUB_I2C
107 while (timeout-- && (*status & I2C_PCF_PIN)) {
108 adap->waitforpin();
109 *status = get_pcf(adap, 1);
111 if (*status & I2C_PCF_LAB) {
112 DEB2(printk(KERN_INFO
113 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
114 *status));
115 /* Cleanup from LAB-- reset and enable ESO.
116 * This resets the PCF8584; since we've lost the bus, no
117 * further attempts should be made by callers to clean up
118 * (no i2c_stop() etc.)
120 set_pcf(adap, 1, I2C_PCF_PIN);
121 set_pcf(adap, 1, I2C_PCF_ESO);
122 /* TODO: we should pause for a time period sufficient for any
123 * running I2C transaction to complete-- the arbitration
124 * logic won't work properly until the next START is seen.
126 DEB2(printk(KERN_INFO
127 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
128 get_pcf(adap,1)));
129 return(-EINTR);
131 #endif
132 if (timeout <= 0)
133 return(-1);
134 else
135 return(0);
139 * This should perform the 'PCF8584 initialization sequence' as described
140 * in the Philips IC12 data book (1995, Aug 29).
141 * There should be a 30 clock cycle wait after reset, I assume this
142 * has been fulfilled.
143 * There should be a delay at the end equal to the longest I2C message
144 * to synchronize the BB-bit (in multimaster systems). How long is
145 * this? I assume 1 second is always long enough.
147 * vdovikin: added detect code for PCF8584
149 static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
151 unsigned char temp;
153 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
155 /* S1=0x80: S0 selected, serial interface off */
156 set_pcf(adap, 1, I2C_PCF_PIN);
157 /* check to see S1 now used as R/W ctrl -
158 PCF8584 does that when ESO is zero */
159 if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
160 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
161 return -ENXIO; /* definetly not PCF8584 */
164 /* load own address in S0, effective address is (own << 1) */
165 i2c_outb(adap, get_own(adap));
166 /* check it's really written */
167 if ((temp = i2c_inb(adap)) != get_own(adap)) {
168 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
169 return -ENXIO;
172 /* S1=0xA0, next byte in S2 */
173 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
174 /* check to see S2 now selected */
175 if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
176 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
177 return -ENXIO;
180 /* load clock register S2 */
181 i2c_outb(adap, get_clock(adap));
182 /* check it's really written, the only 5 lowest bits does matter */
183 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
184 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
185 return -ENXIO;
188 /* Enable serial interface, idle, S0 selected */
189 set_pcf(adap, 1, I2C_PCF_IDLE);
191 /* check to see PCF is really idled and we can access status register */
192 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
193 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
194 return -ENXIO;
197 printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
199 return 0;
203 /* ----- Utility functions
206 static inline int try_address(struct i2c_algo_pcf_data *adap,
207 unsigned char addr, int retries)
209 int i, status, ret = -1;
210 int wfp;
211 for (i=0;i<retries;i++) {
212 i2c_outb(adap, addr);
213 i2c_start(adap);
214 status = get_pcf(adap, 1);
215 if ((wfp = wait_for_pin(adap, &status)) >= 0) {
216 if ((status & I2C_PCF_LRB) == 0) {
217 i2c_stop(adap);
218 break; /* success! */
221 if (wfp == -EINTR) {
222 /* arbitration lost */
223 udelay(adap->udelay);
224 return -EINTR;
226 i2c_stop(adap);
227 udelay(adap->udelay);
229 DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
230 addr));
231 return ret;
235 static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
236 int count, int last)
238 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
239 int wrcount, status, timeout;
241 for (wrcount=0; wrcount<count; ++wrcount) {
242 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
243 buf[wrcount]&0xff));
244 i2c_outb(adap, buf[wrcount]);
245 timeout = wait_for_pin(adap, &status);
246 if (timeout) {
247 if (timeout == -EINTR) {
248 /* arbitration lost */
249 return -EINTR;
251 i2c_stop(adap);
252 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
253 return -EREMOTEIO; /* got a better one ?? */
255 #ifndef STUB_I2C
256 if (status & I2C_PCF_LRB) {
257 i2c_stop(adap);
258 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
259 return -EREMOTEIO; /* got a better one ?? */
261 #endif
263 if (last) {
264 i2c_stop(adap);
266 else {
267 i2c_repstart(adap);
270 return (wrcount);
274 static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
275 int count, int last)
277 int i, status;
278 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
279 int wfp;
281 /* increment number of bytes to read by one -- read dummy byte */
282 for (i = 0; i <= count; i++) {
284 if ((wfp = wait_for_pin(adap, &status))) {
285 if (wfp == -EINTR) {
286 /* arbitration lost */
287 return -EINTR;
289 i2c_stop(adap);
290 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
291 return (-1);
294 #ifndef STUB_I2C
295 if ((status & I2C_PCF_LRB) && (i != count)) {
296 i2c_stop(adap);
297 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
298 return (-1);
300 #endif
302 if (i == count - 1) {
303 set_pcf(adap, 1, I2C_PCF_ESO);
304 } else
305 if (i == count) {
306 if (last) {
307 i2c_stop(adap);
308 } else {
309 i2c_repstart(adap);
313 if (i) {
314 buf[i - 1] = i2c_inb(adap);
315 } else {
316 i2c_inb(adap); /* dummy read */
320 return (i - 1);
324 static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
325 struct i2c_msg *msg, int retries)
327 unsigned short flags = msg->flags;
328 unsigned char addr;
329 int ret;
330 if ( (flags & I2C_M_TEN) ) {
331 /* a ten bit address */
332 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
333 DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
334 /* try extended address code...*/
335 ret = try_address(adap, addr, retries);
336 if (ret!=1) {
337 printk(KERN_ERR "died at extended address code.\n");
338 return -EREMOTEIO;
340 /* the remaining 8 bit address */
341 i2c_outb(adap,msg->addr & 0x7f);
342 /* Status check comes here */
343 if (ret != 1) {
344 printk(KERN_ERR "died at 2nd address code.\n");
345 return -EREMOTEIO;
347 if ( flags & I2C_M_RD ) {
348 i2c_repstart(adap);
349 /* okay, now switch into reading mode */
350 addr |= 0x01;
351 ret = try_address(adap, addr, retries);
352 if (ret!=1) {
353 printk(KERN_ERR "died at extended address code.\n");
354 return -EREMOTEIO;
357 } else { /* normal 7bit address */
358 addr = ( msg->addr << 1 );
359 if (flags & I2C_M_RD )
360 addr |= 1;
361 if (flags & I2C_M_REV_DIR_ADDR )
362 addr ^= 1;
363 i2c_outb(adap, addr);
365 return 0;
368 static int pcf_xfer(struct i2c_adapter *i2c_adap,
369 struct i2c_msg *msgs,
370 int num)
372 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
373 struct i2c_msg *pmsg;
374 int i;
375 int ret=0, timeout, status;
378 /* Check for bus busy */
379 timeout = wait_for_bb(adap);
380 if (timeout) {
381 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
382 "Timeout waiting for BB in pcf_xfer\n");)
383 return -EIO;
386 for (i = 0;ret >= 0 && i < num; i++) {
387 pmsg = &msgs[i];
389 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
390 pmsg->flags & I2C_M_RD ? "read" : "write",
391 pmsg->len, pmsg->addr, i + 1, num);)
393 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
395 /* Send START */
396 if (i == 0) {
397 i2c_start(adap);
400 /* Wait for PIN (pending interrupt NOT) */
401 timeout = wait_for_pin(adap, &status);
402 if (timeout) {
403 if (timeout == -EINTR) {
404 /* arbitration lost */
405 return (-EINTR);
407 i2c_stop(adap);
408 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
409 "for PIN(1) in pcf_xfer\n");)
410 return (-EREMOTEIO);
413 #ifndef STUB_I2C
414 /* Check LRB (last rcvd bit - slave ack) */
415 if (status & I2C_PCF_LRB) {
416 i2c_stop(adap);
417 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
418 return (-EREMOTEIO);
420 #endif
422 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
423 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
425 /* Read */
426 if (pmsg->flags & I2C_M_RD) {
427 /* read bytes into buffer*/
428 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
429 (i + 1 == num));
431 if (ret != pmsg->len) {
432 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
433 "only read %d bytes.\n",ret));
434 } else {
435 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
437 } else { /* Write */
438 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
439 (i + 1 == num));
441 if (ret != pmsg->len) {
442 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
443 "only wrote %d bytes.\n",ret));
444 } else {
445 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
450 return (i);
453 static u32 pcf_func(struct i2c_adapter *adap)
455 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
456 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
459 /* -----exported algorithm data: ------------------------------------- */
461 static const struct i2c_algorithm pcf_algo = {
462 .master_xfer = pcf_xfer,
463 .functionality = pcf_func,
467 * registering functions to load algorithms at runtime
469 int i2c_pcf_add_bus(struct i2c_adapter *adap)
471 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
472 int rval;
474 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
476 /* register new adapter to i2c module... */
477 adap->algo = &pcf_algo;
479 adap->timeout = 100; /* default values, should */
480 adap->retries = 3; /* be replaced by defines */
482 if ((rval = pcf_init_8584(pcf_adap)))
483 return rval;
485 rval = i2c_add_adapter(adap);
487 return rval;
489 EXPORT_SYMBOL(i2c_pcf_add_bus);
491 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
492 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
493 MODULE_LICENSE("GPL");
495 module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
496 MODULE_PARM_DESC(i2c_debug,
497 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");