Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * i2c-algo-pcf.c i2c driver algorithms for PCF8584 adapters
4 *
5 * Copyright (C) 1995-1997 Simon G. Vogl
6 * 1998-2000 Hans Berglund
7 *
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>
11 *
12 * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
13 * messages, proper stop/repstart signaling during receive, added detect code
14 */
15
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"
23
24
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
30
31/*
32 * module parameters:
33 */
34static int i2c_debug;
35
36/* setting states on the bus with the right timing: */
37
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)
44
45/* other auxiliary functions */
46
47static void i2c_start(struct i2c_algo_pcf_data *adap)
48{
49 DEBPROTO(printk(KERN_DEBUG "S "));
50 set_pcf(adap, 1, I2C_PCF_START);
51}
52
53static void i2c_repstart(struct i2c_algo_pcf_data *adap)
54{
55 DEBPROTO(printk(" Sr "));
56 set_pcf(adap, 1, I2C_PCF_REPSTART);
57}
58
59static void i2c_stop(struct i2c_algo_pcf_data *adap)
60{
61 DEBPROTO(printk("P\n"));
62 set_pcf(adap, 1, I2C_PCF_STOP);
63}
64
65static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
66{
67 DEB2(printk(KERN_INFO
68 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
69 *status));
70 /*
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.)
75 */
76 set_pcf(adap, 1, I2C_PCF_PIN);
77 set_pcf(adap, 1, I2C_PCF_ESO);
78 /*
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.
83 *
84 * REVISIT: should probably use msleep instead of mdelay if we
85 * know we can sleep.
86 */
87 if (adap->lab_mdelay)
88 mdelay(adap->lab_mdelay);
89
90 DEB2(printk(KERN_INFO
91 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
92 get_pcf(adap, 1)));
93}
94
95static int wait_for_bb(struct i2c_algo_pcf_data *adap)
96{
97
98 int timeout = DEF_TIMEOUT;
99 int status;
100
101 status = get_pcf(adap, 1);
102
103 while (!(status & I2C_PCF_BB) && --timeout) {
104 udelay(100); /* wait for 100 us */
105 status = get_pcf(adap, 1);
106 }
107
108 if (timeout == 0) {
109 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
110 return -ETIMEDOUT;
111 }
112
113 return 0;
114}
115
116static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
117{
118
119 int timeout = DEF_TIMEOUT;
120
121 *status = get_pcf(adap, 1);
122
123 while ((*status & I2C_PCF_PIN) && --timeout) {
124 adap->waitforpin(adap->data);
125 *status = get_pcf(adap, 1);
126 }
127 if (*status & I2C_PCF_LAB) {
128 handle_lab(adap, status);
129 return -EINTR;
130 }
131
132 if (timeout == 0)
133 return -ETIMEDOUT;
134
135 return 0;
136}
137
138/*
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.
146 *
147 * vdovikin: added detect code for PCF8584
148 */
149static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
150{
151 unsigned char temp;
152
153 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
154 get_pcf(adap, 1)));
155
156 /* S1=0x80: S0 selected, serial interface off */
157 set_pcf(adap, 1, I2C_PCF_PIN);
158 /*
159 * check to see S1 now used as R/W ctrl -
160 * PCF8584 does that when ESO is zero
161 */
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 */
165 }
166
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));
172 return -ENXIO;
173 }
174
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));
180 return -ENXIO;
181 }
182
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));
188 return -ENXIO;
189 }
190
191 /* Enable serial interface, idle, S0 selected */
192 set_pcf(adap, 1, I2C_PCF_IDLE);
193
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));
197 return -ENXIO;
198 }
199
200 printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
201
202 return 0;
203}
204
205static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
206 int count, int last)
207{
208 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
209 int wrcount, status, timeout;
210
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);
216 if (timeout) {
217 if (timeout == -EINTR)
218 return -EINTR; /* arbitration lost */
219
220 i2c_stop(adap);
221 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
222 return -EREMOTEIO; /* got a better one ?? */
223 }
224 if (status & I2C_PCF_LRB) {
225 i2c_stop(adap);
226 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
227 return -EREMOTEIO; /* got a better one ?? */
228 }
229 }
230 if (last)
231 i2c_stop(adap);
232 else
233 i2c_repstart(adap);
234
235 return wrcount;
236}
237
238static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
239 int count, int last)
240{
241 int i, status;
242 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
243 int wfp;
244
245 /* increment number of bytes to read by one -- read dummy byte */
246 for (i = 0; i <= count; i++) {
247
248 if ((wfp = wait_for_pin(adap, &status))) {
249 if (wfp == -EINTR)
250 return -EINTR; /* arbitration lost */
251
252 i2c_stop(adap);
253 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
254 return -1;
255 }
256
257 if ((status & I2C_PCF_LRB) && (i != count)) {
258 i2c_stop(adap);
259 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
260 return -1;
261 }
262
263 if (i == count - 1) {
264 set_pcf(adap, 1, I2C_PCF_ESO);
265 } else if (i == count) {
266 if (last)
267 i2c_stop(adap);
268 else
269 i2c_repstart(adap);
270 }
271
272 if (i)
273 buf[i - 1] = i2c_inb(adap);
274 else
275 i2c_inb(adap); /* dummy read */
276 }
277
278 return i - 1;
279}
280
281
282static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
283 struct i2c_msg *msg)
284{
285 unsigned char addr = i2c_8bit_addr_from_msg(msg);
286
287 if (msg->flags & I2C_M_REV_DIR_ADDR)
288 addr ^= 1;
289 i2c_outb(adap, addr);
290
291 return 0;
292}
293
294static int pcf_xfer(struct i2c_adapter *i2c_adap,
295 struct i2c_msg *msgs,
296 int num)
297{
298 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
299 struct i2c_msg *pmsg;
300 int i;
301 int ret=0, timeout, status;
302
303 if (adap->xfer_begin)
304 adap->xfer_begin(adap->data);
305
306 /* Check for bus busy */
307 timeout = wait_for_bb(adap);
308 if (timeout) {
309 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
310 "Timeout waiting for BB in pcf_xfer\n");)
311 i = -EIO;
312 goto out;
313 }
314
315 for (i = 0;ret >= 0 && i < num; i++) {
316 pmsg = &msgs[i];
317
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);)
321
322 ret = pcf_doAddress(adap, pmsg);
323
324 /* Send START */
325 if (i == 0)
326 i2c_start(adap);
327
328 /* Wait for PIN (pending interrupt NOT) */
329 timeout = wait_for_pin(adap, &status);
330 if (timeout) {
331 if (timeout == -EINTR) {
332 /* arbitration lost */
333 i = -EINTR;
334 goto out;
335 }
336 i2c_stop(adap);
337 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
338 "for PIN(1) in pcf_xfer\n");)
339 i = -EREMOTEIO;
340 goto out;
341 }
342
343 /* Check LRB (last rcvd bit - slave ack) */
344 if (status & I2C_PCF_LRB) {
345 i2c_stop(adap);
346 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
347 i = -EREMOTEIO;
348 goto out;
349 }
350
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);)
353
354 if (pmsg->flags & I2C_M_RD) {
355 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
356 (i + 1 == num));
357
358 if (ret != pmsg->len) {
359 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
360 "only read %d bytes.\n",ret));
361 } else {
362 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
363 }
364 } else {
365 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
366 (i + 1 == num));
367
368 if (ret != pmsg->len) {
369 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
370 "only wrote %d bytes.\n",ret));
371 } else {
372 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
373 }
374 }
375 }
376
377out:
378 if (adap->xfer_end)
379 adap->xfer_end(adap->data);
380 return i;
381}
382
383static u32 pcf_func(struct i2c_adapter *adap)
384{
385 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
386 I2C_FUNC_PROTOCOL_MANGLING;
387}
388
389/* exported algorithm data: */
390static const struct i2c_algorithm pcf_algo = {
391 .master_xfer = pcf_xfer,
392 .functionality = pcf_func,
393};
394
395/*
396 * registering functions to load algorithms at runtime
397 */
398int i2c_pcf_add_bus(struct i2c_adapter *adap)
399{
400 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
401 int rval;
402
403 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
404
405 /* register new adapter to i2c module... */
406 adap->algo = &pcf_algo;
407
408 if ((rval = pcf_init_8584(pcf_adap)))
409 return rval;
410
411 rval = i2c_add_adapter(adap);
412
413 return rval;
414}
415EXPORT_SYMBOL(i2c_pcf_add_bus);
416
417MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
418MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
419MODULE_LICENSE("GPL");
420
421module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
422MODULE_PARM_DESC(i2c_debug,
423 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
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
6 *
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.
11 *
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.
16 *
17 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
18 * Frodo Looijaard <frodol@dds.nl>, and also from Martin Bailey
19 * <mbailey@littlefeet-inc.com>
20 *
21 * Partially rewriten by Oleg I. Vdovikin <vdovikin@jscc.ru> to handle multiple
22 * messages, proper stop/repstart signaling during receive, added detect code
23 */
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/delay.h>
28#include <linux/errno.h>
29#include <linux/i2c.h>
30#include <linux/i2c-algo-pcf.h>
31#include "i2c-algo-pcf.h"
32
33
34#define DEB2(x) if (i2c_debug >= 2) x
35#define DEB3(x) if (i2c_debug >= 3) x /* print several statistical values */
36#define DEBPROTO(x) if (i2c_debug >= 9) x;
37 /* debug the protocol by showing transferred bits */
38#define DEF_TIMEOUT 16
39
40/*
41 * module parameters:
42 */
43static int i2c_debug;
44
45/* setting states on the bus with the right timing: */
46
47#define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
48#define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
49#define get_own(adap) adap->getown(adap->data)
50#define get_clock(adap) adap->getclock(adap->data)
51#define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
52#define i2c_inb(adap) adap->getpcf(adap->data, 0)
53
54/* other auxiliary functions */
55
56static void i2c_start(struct i2c_algo_pcf_data *adap)
57{
58 DEBPROTO(printk(KERN_DEBUG "S "));
59 set_pcf(adap, 1, I2C_PCF_START);
60}
61
62static void i2c_repstart(struct i2c_algo_pcf_data *adap)
63{
64 DEBPROTO(printk(" Sr "));
65 set_pcf(adap, 1, I2C_PCF_REPSTART);
66}
67
68static void i2c_stop(struct i2c_algo_pcf_data *adap)
69{
70 DEBPROTO(printk("P\n"));
71 set_pcf(adap, 1, I2C_PCF_STOP);
72}
73
74static void handle_lab(struct i2c_algo_pcf_data *adap, const int *status)
75{
76 DEB2(printk(KERN_INFO
77 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
78 *status));
79 /*
80 * Cleanup from LAB -- reset and enable ESO.
81 * This resets the PCF8584; since we've lost the bus, no
82 * further attempts should be made by callers to clean up
83 * (no i2c_stop() etc.)
84 */
85 set_pcf(adap, 1, I2C_PCF_PIN);
86 set_pcf(adap, 1, I2C_PCF_ESO);
87 /*
88 * We pause for a time period sufficient for any running
89 * I2C transaction to complete -- the arbitration logic won't
90 * work properly until the next START is seen.
91 * It is assumed the bus driver or client has set a proper value.
92 *
93 * REVISIT: should probably use msleep instead of mdelay if we
94 * know we can sleep.
95 */
96 if (adap->lab_mdelay)
97 mdelay(adap->lab_mdelay);
98
99 DEB2(printk(KERN_INFO
100 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
101 get_pcf(adap, 1)));
102}
103
104static int wait_for_bb(struct i2c_algo_pcf_data *adap)
105{
106
107 int timeout = DEF_TIMEOUT;
108 int status;
109
110 status = get_pcf(adap, 1);
111
112 while (!(status & I2C_PCF_BB) && --timeout) {
113 udelay(100); /* wait for 100 us */
114 status = get_pcf(adap, 1);
115 }
116
117 if (timeout == 0) {
118 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
119 return -ETIMEDOUT;
120 }
121
122 return 0;
123}
124
125static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status)
126{
127
128 int timeout = DEF_TIMEOUT;
129
130 *status = get_pcf(adap, 1);
131
132 while ((*status & I2C_PCF_PIN) && --timeout) {
133 adap->waitforpin(adap->data);
134 *status = get_pcf(adap, 1);
135 }
136 if (*status & I2C_PCF_LAB) {
137 handle_lab(adap, status);
138 return -EINTR;
139 }
140
141 if (timeout == 0)
142 return -ETIMEDOUT;
143
144 return 0;
145}
146
147/*
148 * This should perform the 'PCF8584 initialization sequence' as described
149 * in the Philips IC12 data book (1995, Aug 29).
150 * There should be a 30 clock cycle wait after reset, I assume this
151 * has been fulfilled.
152 * There should be a delay at the end equal to the longest I2C message
153 * to synchronize the BB-bit (in multimaster systems). How long is
154 * this? I assume 1 second is always long enough.
155 *
156 * vdovikin: added detect code for PCF8584
157 */
158static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
159{
160 unsigned char temp;
161
162 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: PCF state 0x%02x\n",
163 get_pcf(adap, 1)));
164
165 /* S1=0x80: S0 selected, serial interface off */
166 set_pcf(adap, 1, I2C_PCF_PIN);
167 /*
168 * check to see S1 now used as R/W ctrl -
169 * PCF8584 does that when ESO is zero
170 */
171 if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
172 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
173 return -ENXIO; /* definitely not PCF8584 */
174 }
175
176 /* load own address in S0, effective address is (own << 1) */
177 i2c_outb(adap, get_own(adap));
178 /* check it's really written */
179 if ((temp = i2c_inb(adap)) != get_own(adap)) {
180 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
181 return -ENXIO;
182 }
183
184 /* S1=0xA0, next byte in S2 */
185 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
186 /* check to see S2 now selected */
187 if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
188 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
189 return -ENXIO;
190 }
191
192 /* load clock register S2 */
193 i2c_outb(adap, get_clock(adap));
194 /* check it's really written, the only 5 lowest bits does matter */
195 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
196 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
197 return -ENXIO;
198 }
199
200 /* Enable serial interface, idle, S0 selected */
201 set_pcf(adap, 1, I2C_PCF_IDLE);
202
203 /* check to see PCF is really idled and we can access status register */
204 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
205 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
206 return -ENXIO;
207 }
208
209 printk(KERN_DEBUG "i2c-algo-pcf.o: detected and initialized PCF8584.\n");
210
211 return 0;
212}
213
214static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
215 int count, int last)
216{
217 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
218 int wrcount, status, timeout;
219
220 for (wrcount=0; wrcount<count; ++wrcount) {
221 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
222 buf[wrcount] & 0xff));
223 i2c_outb(adap, buf[wrcount]);
224 timeout = wait_for_pin(adap, &status);
225 if (timeout) {
226 if (timeout == -EINTR)
227 return -EINTR; /* arbitration lost */
228
229 i2c_stop(adap);
230 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
231 return -EREMOTEIO; /* got a better one ?? */
232 }
233 if (status & I2C_PCF_LRB) {
234 i2c_stop(adap);
235 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
236 return -EREMOTEIO; /* got a better one ?? */
237 }
238 }
239 if (last)
240 i2c_stop(adap);
241 else
242 i2c_repstart(adap);
243
244 return wrcount;
245}
246
247static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
248 int count, int last)
249{
250 int i, status;
251 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
252 int wfp;
253
254 /* increment number of bytes to read by one -- read dummy byte */
255 for (i = 0; i <= count; i++) {
256
257 if ((wfp = wait_for_pin(adap, &status))) {
258 if (wfp == -EINTR)
259 return -EINTR; /* arbitration lost */
260
261 i2c_stop(adap);
262 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
263 return -1;
264 }
265
266 if ((status & I2C_PCF_LRB) && (i != count)) {
267 i2c_stop(adap);
268 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
269 return -1;
270 }
271
272 if (i == count - 1) {
273 set_pcf(adap, 1, I2C_PCF_ESO);
274 } else if (i == count) {
275 if (last)
276 i2c_stop(adap);
277 else
278 i2c_repstart(adap);
279 }
280
281 if (i)
282 buf[i - 1] = i2c_inb(adap);
283 else
284 i2c_inb(adap); /* dummy read */
285 }
286
287 return i - 1;
288}
289
290
291static int pcf_doAddress(struct i2c_algo_pcf_data *adap,
292 struct i2c_msg *msg)
293{
294 unsigned short flags = msg->flags;
295 unsigned char addr;
296
297 addr = msg->addr << 1;
298 if (flags & I2C_M_RD)
299 addr |= 1;
300 if (flags & I2C_M_REV_DIR_ADDR)
301 addr ^= 1;
302 i2c_outb(adap, addr);
303
304 return 0;
305}
306
307static int pcf_xfer(struct i2c_adapter *i2c_adap,
308 struct i2c_msg *msgs,
309 int num)
310{
311 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
312 struct i2c_msg *pmsg;
313 int i;
314 int ret=0, timeout, status;
315
316 if (adap->xfer_begin)
317 adap->xfer_begin(adap->data);
318
319 /* Check for bus busy */
320 timeout = wait_for_bb(adap);
321 if (timeout) {
322 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
323 "Timeout waiting for BB in pcf_xfer\n");)
324 i = -EIO;
325 goto out;
326 }
327
328 for (i = 0;ret >= 0 && i < num; i++) {
329 pmsg = &msgs[i];
330
331 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
332 pmsg->flags & I2C_M_RD ? "read" : "write",
333 pmsg->len, pmsg->addr, i + 1, num);)
334
335 ret = pcf_doAddress(adap, pmsg);
336
337 /* Send START */
338 if (i == 0)
339 i2c_start(adap);
340
341 /* Wait for PIN (pending interrupt NOT) */
342 timeout = wait_for_pin(adap, &status);
343 if (timeout) {
344 if (timeout == -EINTR) {
345 /* arbitration lost */
346 i = -EINTR;
347 goto out;
348 }
349 i2c_stop(adap);
350 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
351 "for PIN(1) in pcf_xfer\n");)
352 i = -EREMOTEIO;
353 goto out;
354 }
355
356 /* Check LRB (last rcvd bit - slave ack) */
357 if (status & I2C_PCF_LRB) {
358 i2c_stop(adap);
359 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
360 i = -EREMOTEIO;
361 goto out;
362 }
363
364 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
365 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
366
367 if (pmsg->flags & I2C_M_RD) {
368 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
369 (i + 1 == num));
370
371 if (ret != pmsg->len) {
372 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
373 "only read %d bytes.\n",ret));
374 } else {
375 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
376 }
377 } else {
378 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
379 (i + 1 == num));
380
381 if (ret != pmsg->len) {
382 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
383 "only wrote %d bytes.\n",ret));
384 } else {
385 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
386 }
387 }
388 }
389
390out:
391 if (adap->xfer_end)
392 adap->xfer_end(adap->data);
393 return i;
394}
395
396static u32 pcf_func(struct i2c_adapter *adap)
397{
398 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
399 I2C_FUNC_PROTOCOL_MANGLING;
400}
401
402/* exported algorithm data: */
403static const struct i2c_algorithm pcf_algo = {
404 .master_xfer = pcf_xfer,
405 .functionality = pcf_func,
406};
407
408/*
409 * registering functions to load algorithms at runtime
410 */
411int i2c_pcf_add_bus(struct i2c_adapter *adap)
412{
413 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
414 int rval;
415
416 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
417
418 /* register new adapter to i2c module... */
419 adap->algo = &pcf_algo;
420
421 if ((rval = pcf_init_8584(pcf_adap)))
422 return rval;
423
424 rval = i2c_add_adapter(adap);
425
426 return rval;
427}
428EXPORT_SYMBOL(i2c_pcf_add_bus);
429
430MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
431MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
432MODULE_LICENSE("GPL");
433
434module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
435MODULE_PARM_DESC(i2c_debug,
436 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");