1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
5 * Copyright (C) 1995-1997 Simon G. Vogl
6 * 1998-2000 Hans Berglund
8 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
9 * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
10 * <mbailey@littlefeet-inc.com>
12 * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
13 * messages, proper stop/repstart signaling during receive, added detect code
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/i2c.h>
21 #include <linux/i2c-algo-pcf.h>
22 #include "i2c-algo-pcf.h"
25 #define DEB2(x) if (i2c_debug >= 2) x
26 #define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */
27 #define DEBPROTO(x) if (i2c_debug >= 9) x;
28 /* debug the protocol by showing transferred bits */
29 #define DEF_TIMEOUT 16
36 /* setting states on the bus with the right timing: */
38 #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
39 #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
40 #define get_own(adap) adap->getown(adap->data)
41 #define get_clock(adap) adap->getclock(adap->data)
42 #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
43 #define i2c_inb(adap) adap->getpcf(adap->data, 0)
45 /* other auxiliary functions */
47 static void i2c_start(struct i2c_algo_pcf_data
*adap
)
49 DEBPROTO(printk(KERN_DEBUG
"S "));
50 set_pcf(adap
, 1, I2C_PCF_START
);
53 static void i2c_repstart(struct i2c_algo_pcf_data
*adap
)
55 DEBPROTO(printk(" Sr "));
56 set_pcf(adap
, 1, I2C_PCF_REPSTART
);
59 static void i2c_stop(struct i2c_algo_pcf_data
*adap
)
61 DEBPROTO(printk("P\n"));
62 set_pcf(adap
, 1, I2C_PCF_STOP
);
65 static void handle_lab(struct i2c_algo_pcf_data
*adap
, const int *status
)
68 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
71 * Cleanup from LAB -- reset and enable ESO.
72 * This resets the PCF8584; since we've lost the bus, no
73 * further attempts should be made by callers to clean up
74 * (no i2c_stop() etc.)
76 set_pcf(adap
, 1, I2C_PCF_PIN
);
77 set_pcf(adap
, 1, I2C_PCF_ESO
);
79 * We pause for a time period sufficient for any running
80 * I2C transaction to complete -- the arbitration logic won't
81 * work properly until the next START is seen.
82 * It is assumed the bus driver or client has set a proper value.
84 * REVISIT: should probably use msleep instead of mdelay if we
88 mdelay(adap
->lab_mdelay
);
91 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
95 static int wait_for_bb(struct i2c_algo_pcf_data
*adap
)
98 int timeout
= DEF_TIMEOUT
;
101 status
= get_pcf(adap
, 1);
103 while (!(status
& I2C_PCF_BB
) && --timeout
) {
104 udelay(100); /* wait for 100 us */
105 status
= get_pcf(adap
, 1);
109 printk(KERN_ERR
"Timeout waiting for Bus Busy\n");
116 static int wait_for_pin(struct i2c_algo_pcf_data
*adap
, int *status
)
119 int timeout
= DEF_TIMEOUT
;
121 *status
= get_pcf(adap
, 1);
123 while ((*status
& I2C_PCF_PIN
) && --timeout
) {
124 adap
->waitforpin(adap
->data
);
125 *status
= get_pcf(adap
, 1);
127 if (*status
& I2C_PCF_LAB
) {
128 handle_lab(adap
, status
);
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
)
153 DEB3(printk(KERN_DEBUG
"i2c-algo-pcf.o: PCF state 0x%02x\n",
156 /* S1=0x80: S0 selected, serial interface off */
157 set_pcf(adap
, 1, I2C_PCF_PIN
);
159 * check to see S1 now used as R/W ctrl -
160 * PCF8584 does that when ESO is zero
162 if (((temp
= get_pcf(adap
, 1)) & 0x7f) != (0)) {
163 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp
));
164 return -ENXIO
; /* definitely not PCF8584 */
167 /* load own address in S0, effective address is (own << 1) */
168 i2c_outb(adap
, get_own(adap
));
169 /* check it's really written */
170 if ((temp
= i2c_inb(adap
)) != get_own(adap
)) {
171 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp
));
175 /* S1=0xA0, next byte in S2 */
176 set_pcf(adap
, 1, I2C_PCF_PIN
| I2C_PCF_ES1
);
177 /* check to see S2 now selected */
178 if (((temp
= get_pcf(adap
, 1)) & 0x7f) != I2C_PCF_ES1
) {
179 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp
));
183 /* load clock register S2 */
184 i2c_outb(adap
, get_clock(adap
));
185 /* check it's really written, the only 5 lowest bits does matter */
186 if (((temp
= i2c_inb(adap
)) & 0x1f) != get_clock(adap
)) {
187 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp
));
191 /* Enable serial interface, idle, S0 selected */
192 set_pcf(adap
, 1, I2C_PCF_IDLE
);
194 /* check to see PCF is really idled and we can access status register */
195 if ((temp
= get_pcf(adap
, 1)) != (I2C_PCF_PIN
| I2C_PCF_BB
)) {
196 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp
));
200 printk(KERN_DEBUG
"i2c-algo-pcf.o: detected and initialized PCF8584.\n");
205 static int pcf_sendbytes(struct i2c_adapter
*i2c_adap
, const char *buf
,
208 struct i2c_algo_pcf_data
*adap
= i2c_adap
->algo_data
;
209 int wrcount
, status
, timeout
;
211 for (wrcount
=0; wrcount
<count
; ++wrcount
) {
212 DEB2(dev_dbg(&i2c_adap
->dev
, "i2c_write: writing %2.2X\n",
213 buf
[wrcount
] & 0xff));
214 i2c_outb(adap
, buf
[wrcount
]);
215 timeout
= wait_for_pin(adap
, &status
);
217 if (timeout
== -EINTR
)
218 return -EINTR
; /* arbitration lost */
221 dev_err(&i2c_adap
->dev
, "i2c_write: error - timeout.\n");
222 return -EREMOTEIO
; /* got a better one ?? */
224 if (status
& I2C_PCF_LRB
) {
226 dev_err(&i2c_adap
->dev
, "i2c_write: error - no ack.\n");
227 return -EREMOTEIO
; /* got a better one ?? */
238 static int pcf_readbytes(struct i2c_adapter
*i2c_adap
, char *buf
,
242 struct i2c_algo_pcf_data
*adap
= i2c_adap
->algo_data
;
245 /* increment number of bytes to read by one -- read dummy byte */
246 for (i
= 0; i
<= count
; i
++) {
248 if ((wfp
= wait_for_pin(adap
, &status
))) {
250 return -EINTR
; /* arbitration lost */
253 dev_err(&i2c_adap
->dev
, "pcf_readbytes timed out.\n");
257 if ((status
& I2C_PCF_LRB
) && (i
!= count
)) {
259 dev_err(&i2c_adap
->dev
, "i2c_read: i2c_inb, No ack.\n");
263 if (i
== count
- 1) {
264 set_pcf(adap
, 1, I2C_PCF_ESO
);
265 } else if (i
== count
) {
273 buf
[i
- 1] = i2c_inb(adap
);
275 i2c_inb(adap
); /* dummy read */
282 static int pcf_doAddress(struct i2c_algo_pcf_data
*adap
,
285 unsigned char addr
= i2c_8bit_addr_from_msg(msg
);
287 if (msg
->flags
& I2C_M_REV_DIR_ADDR
)
289 i2c_outb(adap
, addr
);
294 static int pcf_xfer(struct i2c_adapter
*i2c_adap
,
295 struct i2c_msg
*msgs
,
298 struct i2c_algo_pcf_data
*adap
= i2c_adap
->algo_data
;
299 struct i2c_msg
*pmsg
;
301 int ret
=0, timeout
, status
;
303 if (adap
->xfer_begin
)
304 adap
->xfer_begin(adap
->data
);
306 /* Check for bus busy */
307 timeout
= wait_for_bb(adap
);
309 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: "
310 "Timeout waiting for BB in pcf_xfer\n");)
315 for (i
= 0;ret
>= 0 && i
< num
; i
++) {
318 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
319 pmsg
->flags
& I2C_M_RD
? "read" : "write",
320 pmsg
->len
, pmsg
->addr
, i
+ 1, num
);)
322 ret
= pcf_doAddress(adap
, pmsg
);
328 /* Wait for PIN (pending interrupt NOT) */
329 timeout
= wait_for_pin(adap
, &status
);
331 if (timeout
== -EINTR
) {
332 /* arbitration lost */
337 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: Timeout waiting "
338 "for PIN(1) in pcf_xfer\n");)
343 /* Check LRB (last rcvd bit - slave ack) */
344 if (status
& I2C_PCF_LRB
) {
346 DEB2(printk(KERN_ERR
"i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
351 DEB3(printk(KERN_DEBUG
"i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
352 i
, msgs
[i
].addr
, msgs
[i
].flags
, msgs
[i
].len
);)
354 if (pmsg
->flags
& I2C_M_RD
) {
355 ret
= pcf_readbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
,
358 if (ret
!= pmsg
->len
) {
359 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: fail: "
360 "only read %d bytes.\n",ret
));
362 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: read %d bytes.\n",ret
));
365 ret
= pcf_sendbytes(i2c_adap
, pmsg
->buf
, pmsg
->len
,
368 if (ret
!= pmsg
->len
) {
369 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: fail: "
370 "only wrote %d bytes.\n",ret
));
372 DEB2(printk(KERN_DEBUG
"i2c-algo-pcf.o: wrote %d bytes.\n",ret
));
379 adap
->xfer_end(adap
->data
);
383 static u32
pcf_func(struct i2c_adapter
*adap
)
385 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
386 I2C_FUNC_PROTOCOL_MANGLING
;
389 /* exported algorithm data: */
390 static const struct i2c_algorithm pcf_algo
= {
391 .master_xfer
= pcf_xfer
,
392 .functionality
= pcf_func
,
396 * registering functions to load algorithms at runtime
398 int i2c_pcf_add_bus(struct i2c_adapter
*adap
)
400 struct i2c_algo_pcf_data
*pcf_adap
= adap
->algo_data
;
403 DEB2(dev_dbg(&adap
->dev
, "hw routines registered.\n"));
405 /* register new adapter to i2c module... */
406 adap
->algo
= &pcf_algo
;
408 if ((rval
= pcf_init_8584(pcf_adap
)))
411 rval
= i2c_add_adapter(adap
);
415 EXPORT_SYMBOL(i2c_pcf_add_bus
);
417 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
418 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
419 MODULE_LICENSE("GPL");
421 module_param(i2c_debug
, int, S_IRUGO
| S_IWUSR
);
422 MODULE_PARM_DESC(i2c_debug
,
423 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");