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/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"
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
52 static int i2c_debug=0;
53 static int pcf_scan=0; /* have a look at what's hanging 'round */
55 /* --- setting states on the bus with the right timing: --------------- */
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)
64 /* --- other auxiliary functions -------------------------------------- */
66 static void i2c_start(struct i2c_algo_pcf_data *adap)
68 DEBPROTO(printk("S "));
69 set_pcf(adap, 1, I2C_PCF_START);
72 static void i2c_repstart(struct i2c_algo_pcf_data *adap)
74 DEBPROTO(printk(" Sr "));
75 set_pcf(adap, 1, I2C_PCF_REPSTART);
79 static void i2c_stop(struct i2c_algo_pcf_data *adap)
81 DEBPROTO(printk("P\n"));
82 set_pcf(adap, 1, I2C_PCF_STOP);
86 static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
88 int timeout = DEF_TIMEOUT;
91 status = get_pcf(adap, 1);
93 while (timeout-- && !(status & I2C_PCF_BB)) {
94 udelay(100); /* wait for 100 us */
95 status = get_pcf(adap, 1);
99 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
106 static inline void pcf_sleep(unsigned long timeout)
108 schedule_timeout( timeout * HZ);
112 static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
114 int timeout = DEF_TIMEOUT;
116 *status = get_pcf(adap, 1);
118 while (timeout-- && (*status & I2C_PCF_PIN)) {
120 *status = get_pcf(adap, 1);
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.
138 * vdovikin: added detect code for PCF8584
140 static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
144 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
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 */
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));
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));
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));
180 /* Enable serial interface, idle, S0 selected */
181 set_pcf(adap, 1, I2C_PCF_IDLE);
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));
189 printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
195 /* ----- Utility functions
198 static inline int try_address(struct i2c_algo_pcf_data *adap,
199 unsigned char addr, int retries)
201 int i, status, ret = -1;
202 for (i=0;i<retries;i++) {
203 i2c_outb(adap, addr);
205 status = get_pcf(adap, 1);
206 if (wait_for_pin(adap, &status) >= 0) {
207 if ((status & I2C_PCF_LRB) == 0) {
209 break; /* success! */
213 udelay(adap->udelay);
215 DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
221 static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
224 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
225 int wrcount, status, timeout;
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);
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 ?? */
239 if (status & I2C_PCF_LRB) {
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 ?? */
258 static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
262 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
264 /* increment number of bytes to read by one -- read dummy byte */
265 for (i = 0; i <= count; i++) {
267 if (wait_for_pin(adap, &status)) {
269 printk(KERN_ERR "i2c-algo-pcf.o: pcf_readbytes timed out.\n");
274 if ((status & I2C_PCF_LRB) && (i != count)) {
276 printk(KERN_ERR "i2c-algo-pcf.o: i2c_read: i2c_inb, No ack.\n");
281 if (i == count - 1) {
282 set_pcf(adap, 1, I2C_PCF_ESO);
293 buf[i - 1] = i2c_inb(adap);
295 i2c_inb(adap); /* dummy read */
303 static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
304 struct i2c_msg *msg, int retries)
306 unsigned short flags = msg->flags;
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);
316 printk(KERN_ERR "died at extended address code.\n");
319 /* the remaining 8 bit address */
320 i2c_outb(adap,msg->addr & 0x7f);
321 /* Status check comes here */
323 printk(KERN_ERR "died at 2nd address code.\n");
326 if ( flags & I2C_M_RD ) {
328 /* okay, now switch into reading mode */
330 ret = try_address(adap, addr, retries);
332 printk(KERN_ERR "died at extended address code.\n");
336 } else { /* normal 7bit address */
337 addr = ( msg->addr << 1 );
338 if (flags & I2C_M_RD )
340 if (flags & I2C_M_REV_DIR_ADDR )
342 i2c_outb(adap, addr);
347 static int pcf_xfer(struct i2c_adapter *i2c_adap,
348 struct i2c_msg msgs[],
351 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
352 struct i2c_msg *pmsg;
354 int ret=0, timeout, status;
357 /* Check for bus busy */
358 timeout = wait_for_bb(adap);
360 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
361 "Timeout waiting for BB in pcf_xfer\n");)
365 for (i = 0;ret >= 0 && i < num; i++) {
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);)
372 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
379 /* Wait for PIN (pending interrupt NOT) */
380 timeout = wait_for_pin(adap, &status);
383 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
384 "for PIN(1) in pcf_xfer\n");)
389 /* Check LRB (last rcvd bit - slave ack) */
390 if (status & I2C_PCF_LRB) {
392 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
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);)
401 if (pmsg->flags & I2C_M_RD) {
402 /* read bytes into buffer*/
403 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
406 if (ret != pmsg->len) {
407 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
408 "only read %d bytes.\n",ret));
410 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
413 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
416 if (ret != pmsg->len) {
417 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
418 "only wrote %d bytes.\n",ret));
420 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
428 static int algo_control(struct i2c_adapter *adapter,
429 unsigned int cmd, unsigned long arg)
434 static u32 pcf_func(struct i2c_adapter *adap)
436 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
437 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
440 /* -----exported algorithm data: ------------------------------------- */
442 static struct i2c_algorithm pcf_algo = {
443 .name = "PCF8584 algorithm",
445 .master_xfer = pcf_xfer,
446 .algo_control = algo_control,
447 .functionality = pcf_func,
451 * registering functions to load algorithms at runtime
453 int i2c_pcf_add_bus(struct i2c_adapter *adap)
456 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
458 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: hw routines for %s registered.\n",
461 /* register new adapter to i2c module... */
463 adap->id |= pcf_algo.id;
464 adap->algo = &pcf_algo;
466 adap->timeout = 100; /* default values, should */
467 adap->retries = 3; /* be replaced by defines */
469 if ((i = pcf_init_8584(pcf_adap))) {
479 printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s.\n",
481 for (i = 0x00; i < 0xff; i+=2) {
482 if (wait_for_bb(pcf_adap)) {
483 printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s - TIMEOUTed.\n",
487 i2c_outb(pcf_adap, i);
489 if ((wait_for_pin(pcf_adap, &status) >= 0) &&
490 ((status & I2C_PCF_LRB) == 0)) {
491 printk("(%02x)",i>>1);
496 udelay(pcf_adap->udelay);
501 return i2c_add_adapter(adap);
505 int i2c_pcf_del_bus(struct i2c_adapter *adap)
508 if ((res = i2c_del_adapter(adap)) < 0)
510 DEB2(printk("i2c-algo-pcf.o: adapter unregistered: %s\n",adap->name));
518 int __init i2c_algo_pcf_init (void)
520 printk("i2c-algo-pcf.o: i2c pcf8584 algorithm module\n");
524 EXPORT_SYMBOL(i2c_pcf_add_bus);
525 EXPORT_SYMBOL(i2c_pcf_del_bus);
528 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
529 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
530 MODULE_LICENSE("GPL");
532 MODULE_PARM(pcf_scan, "i");
533 MODULE_PARM(i2c_debug,"i");
535 MODULE_PARM_DESC(pcf_scan, "Scan for active chips on the bus");
536 MODULE_PARM_DESC(i2c_debug,
537 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
540 int init_module(void)
542 return i2c_algo_pcf_init();
545 void cleanup_module(void)