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/i2c.h>
37 #include <linux/i2c-algo-pcf.h>
38 #include "i2c-algo-pcf.h"
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
51 static int i2c_debug=0;
53 /* --- setting states on the bus with the right timing: --------------- */
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)
62 /* --- other auxiliary functions -------------------------------------- */
64 static void i2c_start(struct i2c_algo_pcf_data *adap)
66 DEBPROTO(printk("S "));
67 set_pcf(adap, 1, I2C_PCF_START);
70 static void i2c_repstart(struct i2c_algo_pcf_data *adap)
72 DEBPROTO(printk(" Sr "));
73 set_pcf(adap, 1, I2C_PCF_REPSTART);
77 static void i2c_stop(struct i2c_algo_pcf_data *adap)
79 DEBPROTO(printk("P\n"));
80 set_pcf(adap, 1, I2C_PCF_STOP);
84 static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
86 int timeout = DEF_TIMEOUT;
89 status = get_pcf(adap, 1);
91 while (timeout-- && !(status & I2C_PCF_BB)) {
92 udelay(100); /* wait for 100 us */
93 status = get_pcf(adap, 1);
97 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
104 static inline void pcf_sleep(unsigned long timeout)
106 schedule_timeout( timeout * HZ);
110 static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
112 int timeout = DEF_TIMEOUT;
114 *status = get_pcf(adap, 1);
116 while (timeout-- && (*status & I2C_PCF_PIN)) {
118 *status = get_pcf(adap, 1);
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.
136 * vdovikin: added detect code for PCF8584
138 static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
142 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap, 1)));
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 */
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));
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));
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));
177 /* Enable serial interface, idle, S0 selected */
178 set_pcf(adap, 1, I2C_PCF_IDLE);
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));
186 printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
192 /* ----- Utility functions
195 static inline int try_address(struct i2c_algo_pcf_data *adap,
196 unsigned char addr, int retries)
198 int i, status, ret = -1;
199 for (i=0;i<retries;i++) {
200 i2c_outb(adap, addr);
202 status = get_pcf(adap, 1);
203 if (wait_for_pin(adap, &status) >= 0) {
204 if ((status & I2C_PCF_LRB) == 0) {
206 break; /* success! */
210 udelay(adap->udelay);
212 DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
218 static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
221 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
222 int wrcount, status, timeout;
224 for (wrcount=0; wrcount<count; ++wrcount) {
225 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
227 i2c_outb(adap, buf[wrcount]);
228 timeout = wait_for_pin(adap, &status);
231 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
232 return -EREMOTEIO; /* got a better one ?? */
235 if (status & I2C_PCF_LRB) {
237 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
238 return -EREMOTEIO; /* got a better one ?? */
253 static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
257 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
259 /* increment number of bytes to read by one -- read dummy byte */
260 for (i = 0; i <= count; i++) {
262 if (wait_for_pin(adap, &status)) {
264 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
269 if ((status & I2C_PCF_LRB) && (i != count)) {
271 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
276 if (i == count - 1) {
277 set_pcf(adap, 1, I2C_PCF_ESO);
288 buf[i - 1] = i2c_inb(adap);
290 i2c_inb(adap); /* dummy read */
298 static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
299 struct i2c_msg *msg, int retries)
301 unsigned short flags = msg->flags;
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);
311 printk(KERN_ERR "died at extended address code.\n");
314 /* the remaining 8 bit address */
315 i2c_outb(adap,msg->addr & 0x7f);
316 /* Status check comes here */
318 printk(KERN_ERR "died at 2nd address code.\n");
321 if ( flags & I2C_M_RD ) {
323 /* okay, now switch into reading mode */
325 ret = try_address(adap, addr, retries);
327 printk(KERN_ERR "died at extended address code.\n");
331 } else { /* normal 7bit address */
332 addr = ( msg->addr << 1 );
333 if (flags & I2C_M_RD )
335 if (flags & I2C_M_REV_DIR_ADDR )
337 i2c_outb(adap, addr);
342 static int pcf_xfer(struct i2c_adapter *i2c_adap,
343 struct i2c_msg msgs[],
346 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
347 struct i2c_msg *pmsg;
349 int ret=0, timeout, status;
352 /* Check for bus busy */
353 timeout = wait_for_bb(adap);
355 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
356 "Timeout waiting for BB in pcf_xfer\n");)
360 for (i = 0;ret >= 0 && i < num; i++) {
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);)
367 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
374 /* Wait for PIN (pending interrupt NOT) */
375 timeout = wait_for_pin(adap, &status);
378 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
379 "for PIN(1) in pcf_xfer\n");)
384 /* Check LRB (last rcvd bit - slave ack) */
385 if (status & I2C_PCF_LRB) {
387 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
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);)
396 if (pmsg->flags & I2C_M_RD) {
397 /* read bytes into buffer*/
398 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
401 if (ret != pmsg->len) {
402 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
403 "only read %d bytes.\n",ret));
405 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
408 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
411 if (ret != pmsg->len) {
412 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
413 "only wrote %d bytes.\n",ret));
415 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
423 static u32 pcf_func(struct i2c_adapter *adap)
425 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
426 I2C_FUNC_PROTOCOL_MANGLING;
429 /* -----exported algorithm data: ------------------------------------- */
431 static struct i2c_algorithm pcf_algo = {
432 .name = "PCF8584 algorithm",
434 .master_xfer = pcf_xfer,
435 .functionality = pcf_func,
439 * registering functions to load algorithms at runtime
441 int i2c_pcf_add_bus(struct i2c_adapter *adap)
443 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
446 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
448 /* register new adapter to i2c module... */
450 adap->id |= pcf_algo.id;
451 adap->algo = &pcf_algo;
453 adap->timeout = 100; /* default values, should */
454 adap->retries = 3; /* be replaced by defines */
456 rval = pcf_init_8584(pcf_adap);
458 i2c_add_adapter(adap);
463 int i2c_pcf_del_bus(struct i2c_adapter *adap)
465 return i2c_del_adapter(adap);
468 EXPORT_SYMBOL(i2c_pcf_add_bus);
469 EXPORT_SYMBOL(i2c_pcf_del_bus);
471 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
472 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
473 MODULE_LICENSE("GPL");
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");