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 #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
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 void handle_lab(struct i2c_algo_pcf_data
*adap
, const int *status
)
84 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
87 /* Cleanup from LAB -- reset and enable ESO.
88 * This resets the PCF8584; since we've lost the bus, no
89 * further attempts should be made by callers to clean up
90 * (no i2c_stop() etc.)
92 set_pcf(adap
, 1, I2C_PCF_PIN
);
93 set_pcf(adap
, 1, I2C_PCF_ESO
);
95 /* We pause for a time period sufficient for any running
96 * I2C transaction to complete -- the arbitration logic won't
97 * work properly until the next START is seen.
98 * It is assumed the bus driver or client has set a proper value.
100 * REVISIT: should probably use msleep instead of mdelay if we
103 if (adap
->lab_mdelay
)
104 mdelay(adap
->lab_mdelay
);
106 DEB2(printk(KERN_INFO
107 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
111 static int wait_for_bb(struct i2c_algo_pcf_data
*adap
) {
113 int timeout
= DEF_TIMEOUT
;
116 status
= get_pcf(adap
, 1);
118 while (timeout
-- && !(status
& I2C_PCF_BB
)) {
119 udelay(100); /* wait for 100 us */
120 status
= get_pcf(adap
, 1);
124 printk(KERN_ERR
"Timeout waiting for Bus Busy\n");
131 static int wait_for_pin(struct i2c_algo_pcf_data
*adap
, int *status
) {
133 int timeout
= DEF_TIMEOUT
;
135 *status
= get_pcf(adap
, 1);
137 while (timeout
-- && (*status
& I2C_PCF_PIN
)) {
139 *status
= get_pcf(adap
, 1);
141 if (*status
& I2C_PCF_LAB
) {
142 handle_lab(adap
, status
);
153 * This should perform the 'PCF8584 initialization sequence' as described
154 * in the Philips IC12 data book (1995, Aug 29).
155 * There should be a 30 clock cycle wait after reset, I assume this
156 * has been fulfilled.
157 * There should be a delay at the end equal to the longest I2C message
158 * to synchronize the BB-bit (in multimaster systems). How long is
159 * this? I assume 1 second is always long enough.
161 * vdovikin: added detect code for PCF8584
163 static int pcf_init_8584 (struct i2c_algo_pcf_data
*adap
)
167 DEB3(printk(KERN_DEBUG
"i2c-algo-pcf.o: PCF state 0x%02x\n", get_pcf(adap
, 1)));
169 /* S1=0x80: S0 selected, serial interface off */
170 set_pcf(adap
, 1, I2C_PCF_PIN
);
171 /* check to see S1 now used as R/W ctrl -
172 PCF8584 does that when ESO is zero */
173 if (((temp
= get_pcf(adap
, 1)) & 0x7f) != (0)) {
174 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp
));
175 return -ENXIO
; /* definetly not PCF8584 */
178 /* load own address in S0, effective address is (own << 1) */
179 i2c_outb(adap
, get_own(adap
));
180 /* check it's really written */
181 if ((temp
= i2c_inb(adap
)) != get_own(adap
)) {
182 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp
));
186 /* S1=0xA0, next byte in S2 */
187 set_pcf(adap
, 1, I2C_PCF_PIN
| I2C_PCF_ES1
);
188 /* check to see S2 now selected */
189 if (((temp
= get_pcf(adap
, 1)) & 0x7f) != I2C_PCF_ES1
) {
190 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp
));
194 /* load clock register S2 */
195 i2c_outb(adap
, get_clock(adap
));
196 /* check it's really written, the only 5 lowest bits does matter */
197 if (((temp
= i2c_inb(adap
)) & 0x1f) != get_clock(adap
)) {
198 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp
));
202 /* Enable serial interface, idle, S0 selected */
203 set_pcf(adap
, 1, I2C_PCF_IDLE
);
205 /* check to see PCF is really idled and we can access status register */
206 if ((temp
= get_pcf(adap
, 1)) != (I2C_PCF_PIN
| I2C_PCF_BB
)) {
207 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp
));
211 printk(KERN_DEBUG
"i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
217 /* ----- Utility functions
220 static int pcf_sendbytes(struct i2c_adapter
*i2c_adap
, const char *buf
,
223 struct i2c_algo_pcf_data
*adap
= i2c_adap
->algo_data
;
224 int wrcount
, status
, timeout
;
226 for (wrcount
=0; wrcount
<count
; ++wrcount
) {
227 DEB2(dev_dbg(&i2c_adap
->dev
, "i2c_write: writing %2.2X\n",
229 i2c_outb(adap
, buf
[wrcount
]);
230 timeout
= wait_for_pin(adap
, &status
);
232 if (timeout
== -EINTR
) {
233 /* arbitration lost */
237 dev_err(&i2c_adap
->dev
, "i2c_write: error - timeout.\n");
238 return -EREMOTEIO
; /* got a better one ?? */
241 if (status
& I2C_PCF_LRB
) {
243 dev_err(&i2c_adap
->dev
, "i2c_write: error - no ack.\n");
244 return -EREMOTEIO
; /* got a better one ?? */
259 static int pcf_readbytes(struct i2c_adapter
*i2c_adap
, char *buf
,
263 struct i2c_algo_pcf_data
*adap
= i2c_adap
->algo_data
;
266 /* increment number of bytes to read by one -- read dummy byte */
267 for (i
= 0; i
<= count
; i
++) {
269 if ((wfp
= wait_for_pin(adap
, &status
))) {
271 /* arbitration lost */
275 dev_err(&i2c_adap
->dev
, "pcf_readbytes timed out.\n");
280 if ((status
& I2C_PCF_LRB
) && (i
!= count
)) {
282 dev_err(&i2c_adap
->dev
, "i2c_read: i2c_inb, No ack.\n");
287 if (i
== count
- 1) {
288 set_pcf(adap
, 1, I2C_PCF_ESO
);
299 buf
[i
- 1] = i2c_inb(adap
);
301 i2c_inb(adap
); /* dummy read */
309 static int pcf_doAddress(struct i2c_algo_pcf_data
*adap
,
312 unsigned short flags
= msg
->flags
;
315 addr
= msg
->addr
<< 1;
316 if (flags
& I2C_M_RD
)
318 if (flags
& I2C_M_REV_DIR_ADDR
)
320 i2c_outb(adap
, addr
);
325 static int pcf_xfer(struct i2c_adapter
*i2c_adap
,
326 struct i2c_msg
*msgs
,
329 struct i2c_algo_pcf_data
*adap
= i2c_adap
->algo_data
;
330 struct i2c_msg
*pmsg
;
332 int ret
=0, timeout
, status
;
335 /* Check for bus busy */
336 timeout
= wait_for_bb(adap
);
338 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: "
339 "Timeout waiting for BB in pcf_xfer\n");)
343 for (i
= 0;ret
>= 0 && i
< num
; i
++) {
346 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
347 pmsg
->flags
& I2C_M_RD
? "read" : "write",
348 pmsg
->len
, pmsg
->addr
, i
+ 1, num
);)
350 ret
= pcf_doAddress(adap
, pmsg
);
357 /* Wait for PIN (pending interrupt NOT) */
358 timeout
= wait_for_pin(adap
, &status
);
360 if (timeout
== -EINTR
) {
361 /* arbitration lost */
365 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: Timeout waiting "
366 "for PIN(1) in pcf_xfer\n");)
371 /* Check LRB (last rcvd bit - slave ack) */
372 if (status
& I2C_PCF_LRB
) {
374 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
379 DEB3(printk(KERN_DEBUG
"i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
380 i
, msgs
[i
].addr
, msgs
[i
].flags
, msgs
[i
].len
);)
383 if (pmsg
->flags
& I2C_M_RD
) {
384 /* read bytes into buffer*/
385 ret
= pcf_readbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
,
388 if (ret
!= pmsg
->len
) {
389 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: fail: "
390 "only read %d bytes.\n",ret
));
392 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: read %d bytes.\n",ret
));
395 ret
= pcf_sendbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
,
398 if (ret
!= pmsg
->len
) {
399 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: fail: "
400 "only wrote %d bytes.\n",ret
));
402 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: wrote %d bytes.\n",ret
));
410 static u32
pcf_func(struct i2c_adapter
*adap
)
412 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
413 I2C_FUNC_PROTOCOL_MANGLING
;
416 /* -----exported algorithm data: ------------------------------------- */
418 static const struct i2c_algorithm pcf_algo
= {
419 .master_xfer
= pcf_xfer
,
420 .functionality
= pcf_func
,
424 * registering functions to load algorithms at runtime
426 int i2c_pcf_add_bus(struct i2c_adapter
*adap
)
428 struct i2c_algo_pcf_data
*pcf_adap
= adap
->algo_data
;
431 DEB2(dev_dbg(&adap
->dev
, "hw routines registered.\n"));
433 /* register new adapter to i2c module... */
434 adap
->algo
= &pcf_algo
;
437 if ((rval
= pcf_init_8584(pcf_adap
)))
440 rval
= i2c_add_adapter(adap
);
444 EXPORT_SYMBOL(i2c_pcf_add_bus
);
446 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
447 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
448 MODULE_LICENSE("GPL");
450 module_param(i2c_debug
, int, S_IRUGO
| S_IWUSR
);
451 MODULE_PARM_DESC(i2c_debug
,
452 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");