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,
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/version.h>
35 #include <linux/init.h>
36 #include <asm/uaccess.h>
37 #include <linux/ioport.h>
38 #include <linux/errno.h>
39 #include <linux/sched.h>
41 #include <linux/i2c.h>
42 #include <linux/i2c-algo-pcf.h>
43 #include "i2c-pcf8584.h"
45 /* ----- global defines ----------------------------------------------- */
46 #define DEB(x) if (i2c_debug>=1) x
47 #define DEB2(x) if (i2c_debug>=2) x
48 #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
49 #define DEBPROTO(x) if (i2c_debug>=9) x;
50 /* debug the protocol by showing transferred bits */
51 #define DEF_TIMEOUT 16
55 static int i2c_debug=0;
56 static int pcf_scan=0; /* have a look at what's hanging 'round */
58 /* --- setting states on the bus with the right timing: --------------- */
60 #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
61 #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
62 #define get_own(adap) adap->getown(adap->data)
63 #define get_clock(adap) adap->getclock(adap->data)
64 #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
65 #define i2c_inb(adap) adap->getpcf(adap->data, 0)
67 /* --- other auxiliary functions -------------------------------------- */
69 static void i2c_start(struct i2c_algo_pcf_data *adap)
71 DEBPROTO(printk("S "));
72 set_pcf(adap, 1, I2C_PCF_START);
75 static void i2c_repstart(struct i2c_algo_pcf_data *adap)
77 DEBPROTO(printk(" Sr "));
78 set_pcf(adap, 1, I2C_PCF_REPSTART);
82 static void i2c_stop(struct i2c_algo_pcf_data *adap)
84 DEBPROTO(printk("P\n"));
85 set_pcf(adap, 1, I2C_PCF_STOP);
89 static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
91 int timeout = DEF_TIMEOUT;
94 status = get_pcf(adap, 1);
96 while (timeout-- && !(status & I2C_PCF_BB)) {
97 udelay(100); /* wait for 100 us */
98 status = get_pcf(adap, 1);
102 printk("Timeout waiting for Bus Busy\n");
109 static inline void pcf_sleep(unsigned long timeout)
111 schedule_timeout( timeout * HZ);
115 static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
117 int timeout = DEF_TIMEOUT;
119 *status = get_pcf(adap, 1);
121 while (timeout-- && (*status & I2C_PCF_PIN)) {
123 *status = get_pcf(adap, 1);
133 * This should perform the 'PCF8584 initialization sequence' as described
134 * in the Philips IC12 data book (1995, Aug 29).
135 * There should be a 30 clock cycle wait after reset, I assume this
136 * has been fulfilled.
137 * There should be a delay at the end equal to the longest I2C message
138 * to synchronize the BB-bit (in multimaster systems). How long is
139 * this? I assume 1 second is always long enough.
141 * vdovikin: added detect code for PCF8584
143 static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
147 DEB3(printk("i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
149 /* S1=0x80: S0 selected, serial interface off */
150 set_pcf(adap, 1, I2C_PCF_PIN);
151 /* check to see S1 now used as R/W ctrl -
152 PCF8584 does that when ESO is zero */
153 /* PCF also resets PIN bit */
154 if ((temp = get_pcf(adap, 1)) != (0)) {
155 DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
156 return -ENXIO; /* definetly not PCF8584 */
159 /* load own address in S0, effective address is (own << 1) */
160 i2c_outb(adap, get_own(adap));
161 /* check it's realy writen */
162 if ((temp = i2c_inb(adap)) != get_own(adap)) {
163 DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
167 /* S1=0xA0, next byte in S2 */
168 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
169 /* check to see S2 now selected */
170 if ((temp = get_pcf(adap, 1)) != I2C_PCF_ES1) {
171 DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
175 /* load clock register S2 */
176 i2c_outb(adap, get_clock(adap));
177 /* check it's realy writen, the only 5 lowest bits does matter */
178 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
179 DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
183 /* Enable serial interface, idle, S0 selected */
184 set_pcf(adap, 1, I2C_PCF_IDLE);
186 /* check to see PCF is realy idled and we can access status register */
187 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
188 DEB2(printk("i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
192 printk("i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
198 /* ----- Utility functions
201 static inline int try_address(struct i2c_algo_pcf_data *adap,
202 unsigned char addr, int retries)
204 int i, status, ret = -1;
205 for (i=0;i<retries;i++) {
206 i2c_outb(adap, addr);
208 status = get_pcf(adap, 1);
209 if (wait_for_pin(adap, &status) >= 0) {
210 if ((status & I2C_PCF_LRB) == 0) {
212 break; /* success! */
216 udelay(adap->udelay);
218 DEB2(if (i) printk("i2c-algo-pcf.o: needed %d retries for %d\n",i,
224 static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
227 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
228 int wrcount, status, timeout;
230 for (wrcount=0; wrcount<count; ++wrcount) {
231 DEB2(printk("i2c-algo-pcf.o: %s i2c_write: writing %2.2X\n",
232 i2c_adap->name, buf[wrcount]&0xff));
233 i2c_outb(adap, buf[wrcount]);
234 timeout = wait_for_pin(adap, &status);
237 printk("i2c-algo-pcf.o: %s i2c_write: "
238 "error - timeout.\n", i2c_adap->name);
239 return -EREMOTEIO; /* got a better one ?? */
242 if (status & I2C_PCF_LRB) {
244 printk("i2c-algo-pcf.o: %s i2c_write: "
245 "error - no ack.\n", i2c_adap->name);
246 return -EREMOTEIO; /* got a better one ?? */
261 static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
265 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
267 /* increment number of bytes to read by one -- read dummy byte */
268 for (i = 0; i <= count; i++) {
270 if (wait_for_pin(adap, &status)) {
272 printk("i2c-algo-pcf.o: pcf_readbytes timed out.\n");
277 if ((status & I2C_PCF_LRB) && (i != count)) {
279 printk("i2c-algo-pcf.o: i2c_read: i2c_inb, No ack.\n");
284 if (i == count - 1) {
285 set_pcf(adap, 1, I2C_PCF_ESO);
296 buf[i - 1] = i2c_inb(adap);
298 i2c_inb(adap); /* dummy read */
306 static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
307 struct i2c_msg *msg, int retries)
309 unsigned short flags = msg->flags;
312 if ( (flags & I2C_M_TEN) ) {
313 /* a ten bit address */
314 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
315 DEB2(printk("addr0: %d\n",addr));
316 /* try extended address code...*/
317 ret = try_address(adap, addr, retries);
319 printk("died at extended address code.\n");
322 /* the remaining 8 bit address */
323 i2c_outb(adap,msg->addr & 0x7f);
324 /* Status check comes here */
326 printk("died at 2nd address code.\n");
329 if ( flags & I2C_M_RD ) {
331 /* okay, now switch into reading mode */
333 ret = try_address(adap, addr, retries);
335 printk("died at extended address code.\n");
339 } else { /* normal 7bit address */
340 addr = ( msg->addr << 1 );
341 if (flags & I2C_M_RD )
343 if (flags & I2C_M_REV_DIR_ADDR )
345 i2c_outb(adap, addr);
350 static int pcf_xfer(struct i2c_adapter *i2c_adap,
351 struct i2c_msg msgs[],
354 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
355 struct i2c_msg *pmsg;
357 int ret=0, timeout, status;
360 /* Check for bus busy */
361 timeout = wait_for_bb(adap);
363 DEB2(printk("i2c-algo-pcf.o: "
364 "Timeout waiting for BB in pcf_xfer\n");)
368 for (i = 0;ret >= 0 && i < num; i++) {
371 DEB2(printk("i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
372 pmsg->flags & I2C_M_RD ? "read" : "write",
373 pmsg->len, pmsg->addr, i + 1, num);)
375 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
382 /* Wait for PIN (pending interrupt NOT) */
383 timeout = wait_for_pin(adap, &status);
386 DEB2(printk("i2c-algo-pcf.o: Timeout waiting "
387 "for PIN(1) in pcf_xfer\n");)
392 /* Check LRB (last rcvd bit - slave ack) */
393 if (status & I2C_PCF_LRB) {
395 DEB2(printk("i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
400 DEB3(printk("i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
401 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
404 if (pmsg->flags & I2C_M_RD) {
405 /* read bytes into buffer*/
406 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
409 if (ret != pmsg->len) {
410 DEB2(printk("i2c-algo-pcf.o: fail: "
411 "only read %d bytes.\n",ret));
413 DEB2(printk("i2c-algo-pcf.o: read %d bytes.\n",ret));
416 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
419 if (ret != pmsg->len) {
420 DEB2(printk("i2c-algo-pcf.o: fail: "
421 "only wrote %d bytes.\n",ret));
423 DEB2(printk("i2c-algo-pcf.o: wrote %d bytes.\n",ret));
431 static int algo_control(struct i2c_adapter *adapter,
432 unsigned int cmd, unsigned long arg)
437 static u32 pcf_func(struct i2c_adapter *adap)
439 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
440 I2C_FUNC_PROTOCOL_MANGLING;
443 /* -----exported algorithm data: ------------------------------------- */
445 static struct i2c_algorithm pcf_algo = {
450 NULL, /* slave_xmit */
451 NULL, /* slave_recv */
452 algo_control, /* ioctl */
453 pcf_func, /* functionality */
457 * registering functions to load algorithms at runtime
459 int i2c_pcf_add_bus(struct i2c_adapter *adap)
462 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
464 DEB2(printk("i2c-algo-pcf.o: hw routines for %s registered.\n",
467 /* register new adapter to i2c module... */
469 adap->id |= pcf_algo.id;
470 adap->algo = &pcf_algo;
472 adap->timeout = 100; /* default values, should */
473 adap->retries = 3; /* be replaced by defines */
475 if ((i = pcf_init_8584(pcf_adap))) {
483 i2c_add_adapter(adap);
487 printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s.\n",
489 for (i = 0x00; i < 0xff; i+=2) {
490 if (wait_for_bb(pcf_adap)) {
491 printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s - TIMEOUTed.\n",
495 i2c_outb(pcf_adap, i);
497 if ((wait_for_pin(pcf_adap, &status) >= 0) &&
498 ((status & I2C_PCF_LRB) == 0)) {
499 printk("(%02x)",i>>1);
504 udelay(pcf_adap->udelay);
512 int i2c_pcf_del_bus(struct i2c_adapter *adap)
515 if ((res = i2c_del_adapter(adap)) < 0)
517 DEB2(printk("i2c-algo-pcf.o: adapter unregistered: %s\n",adap->name));
525 int __init i2c_algo_pcf_init (void)
527 printk("i2c-algo-pcf.o: i2c pcf8584 algorithm module\n");
532 EXPORT_SYMBOL(i2c_pcf_add_bus);
533 EXPORT_SYMBOL(i2c_pcf_del_bus);
536 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
537 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
538 MODULE_LICENSE("GPL");
540 MODULE_PARM(pcf_scan, "i");
541 MODULE_PARM(i2c_debug,"i");
543 MODULE_PARM_DESC(pcf_scan, "Scan for active chips on the bus");
544 MODULE_PARM_DESC(i2c_debug,
545 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
548 int init_module(void)
550 return i2c_algo_pcf_init();
553 void cleanup_module(void)