1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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"
39
40
41#define DEB2(x) if (i2c_debug>=2) x
42#define DEB3(x) if (i2c_debug>=3) x
43#define DEBPROTO(x) if (i2c_debug>=9) x;
44
45#define DEF_TIMEOUT 16
46
47
48
49static int i2c_debug;
50
51
52
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)
59
60
61
62static void i2c_start(struct i2c_algo_pcf_data *adap)
63{
64 DEBPROTO(printk("S "));
65 set_pcf(adap, 1, I2C_PCF_START);
66}
67
68static void i2c_repstart(struct i2c_algo_pcf_data *adap)
69{
70 DEBPROTO(printk(" Sr "));
71 set_pcf(adap, 1, I2C_PCF_REPSTART);
72}
73
74
75static void i2c_stop(struct i2c_algo_pcf_data *adap)
76{
77 DEBPROTO(printk("P\n"));
78 set_pcf(adap, 1, I2C_PCF_STOP);
79}
80
81static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
82
83 int timeout = DEF_TIMEOUT;
84 int status;
85
86 status = get_pcf(adap, 1);
87#ifndef STUB_I2C
88 while (timeout-- && !(status & I2C_PCF_BB)) {
89 udelay(100);
90 status = get_pcf(adap, 1);
91 }
92#endif
93 if (timeout <= 0) {
94 printk(KERN_ERR "Timeout waiting for Bus Busy\n");
95 }
96
97 return (timeout<=0);
98}
99
100
101static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
102
103 int timeout = DEF_TIMEOUT;
104
105 *status = get_pcf(adap, 1);
106#ifndef STUB_I2C
107 while (timeout-- && (*status & I2C_PCF_PIN)) {
108 adap->waitforpin();
109 *status = get_pcf(adap, 1);
110 }
111 if (*status & I2C_PCF_LAB) {
112 DEB2(printk(KERN_INFO
113 "i2c-algo-pcf.o: lost arbitration (CSR 0x%02x)\n",
114 *status));
115
116
117
118
119
120 set_pcf(adap, 1, I2C_PCF_PIN);
121 set_pcf(adap, 1, I2C_PCF_ESO);
122
123
124
125
126 DEB2(printk(KERN_INFO
127 "i2c-algo-pcf.o: reset LAB condition (CSR 0x%02x)\n",
128 get_pcf(adap,1)));
129 return(-EINTR);
130 }
131#endif
132 if (timeout <= 0)
133 return(-1);
134 else
135 return(0);
136}
137
138
139
140
141
142
143
144
145
146
147
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", get_pcf(adap, 1)));
154
155
156 set_pcf(adap, 1, I2C_PCF_PIN);
157
158
159 if (((temp = get_pcf(adap, 1)) & 0x7f) != (0)) {
160 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S0 (0x%02x).\n", temp));
161 return -ENXIO;
162 }
163
164
165 i2c_outb(adap, get_own(adap));
166
167 if ((temp = i2c_inb(adap)) != get_own(adap)) {
168 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S0 (0x%02x).\n", temp));
169 return -ENXIO;
170 }
171
172
173 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
174
175 if (((temp = get_pcf(adap, 1)) & 0x7f) != I2C_PCF_ES1) {
176 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S2 (0x%02x).\n", temp));
177 return -ENXIO;
178 }
179
180
181 i2c_outb(adap, get_clock(adap));
182
183 if (((temp = i2c_inb(adap)) & 0x1f) != get_clock(adap)) {
184 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't set S2 (0x%02x).\n", temp));
185 return -ENXIO;
186 }
187
188
189 set_pcf(adap, 1, I2C_PCF_IDLE);
190
191
192 if ((temp = get_pcf(adap, 1)) != (I2C_PCF_PIN | I2C_PCF_BB)) {
193 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: PCF detection failed -- can't select S1` (0x%02x).\n", temp));
194 return -ENXIO;
195 }
196
197 printk(KERN_DEBUG "i2c-algo-pcf.o: deteted and initialized PCF8584.\n");
198
199 return 0;
200}
201
202
203
204
205
206static inline int try_address(struct i2c_algo_pcf_data *adap,
207 unsigned char addr, int retries)
208{
209 int i, status, ret = -1;
210 int wfp;
211 for (i=0;i<retries;i++) {
212 i2c_outb(adap, addr);
213 i2c_start(adap);
214 status = get_pcf(adap, 1);
215 if ((wfp = wait_for_pin(adap, &status)) >= 0) {
216 if ((status & I2C_PCF_LRB) == 0) {
217 i2c_stop(adap);
218 break;
219 }
220 }
221 if (wfp == -EINTR) {
222
223 udelay(adap->udelay);
224 return -EINTR;
225 }
226 i2c_stop(adap);
227 udelay(adap->udelay);
228 }
229 DEB2(if (i) printk(KERN_DEBUG "i2c-algo-pcf.o: needed %d retries for %d\n",i,
230 addr));
231 return ret;
232}
233
234
235static int pcf_sendbytes(struct i2c_adapter *i2c_adap, const char *buf,
236 int count, int last)
237{
238 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
239 int wrcount, status, timeout;
240
241 for (wrcount=0; wrcount<count; ++wrcount) {
242 DEB2(dev_dbg(&i2c_adap->dev, "i2c_write: writing %2.2X\n",
243 buf[wrcount]&0xff));
244 i2c_outb(adap, buf[wrcount]);
245 timeout = wait_for_pin(adap, &status);
246 if (timeout) {
247 if (timeout == -EINTR) {
248
249 return -EINTR;
250 }
251 i2c_stop(adap);
252 dev_err(&i2c_adap->dev, "i2c_write: error - timeout.\n");
253 return -EREMOTEIO;
254 }
255#ifndef STUB_I2C
256 if (status & I2C_PCF_LRB) {
257 i2c_stop(adap);
258 dev_err(&i2c_adap->dev, "i2c_write: error - no ack.\n");
259 return -EREMOTEIO;
260 }
261#endif
262 }
263 if (last) {
264 i2c_stop(adap);
265 }
266 else {
267 i2c_repstart(adap);
268 }
269
270 return (wrcount);
271}
272
273
274static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf,
275 int count, int last)
276{
277 int i, status;
278 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
279 int wfp;
280
281
282 for (i = 0; i <= count; i++) {
283
284 if ((wfp = wait_for_pin(adap, &status))) {
285 if (wfp == -EINTR) {
286
287 return -EINTR;
288 }
289 i2c_stop(adap);
290 dev_err(&i2c_adap->dev, "pcf_readbytes timed out.\n");
291 return (-1);
292 }
293
294#ifndef STUB_I2C
295 if ((status & I2C_PCF_LRB) && (i != count)) {
296 i2c_stop(adap);
297 dev_err(&i2c_adap->dev, "i2c_read: i2c_inb, No ack.\n");
298 return (-1);
299 }
300#endif
301
302 if (i == count - 1) {
303 set_pcf(adap, 1, I2C_PCF_ESO);
304 } else
305 if (i == count) {
306 if (last) {
307 i2c_stop(adap);
308 } else {
309 i2c_repstart(adap);
310 }
311 };
312
313 if (i) {
314 buf[i - 1] = i2c_inb(adap);
315 } else {
316 i2c_inb(adap);
317 }
318 }
319
320 return (i - 1);
321}
322
323
324static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
325 struct i2c_msg *msg, int retries)
326{
327 unsigned short flags = msg->flags;
328 unsigned char addr;
329 int ret;
330 if ( (flags & I2C_M_TEN) ) {
331
332 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
333 DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
334
335 ret = try_address(adap, addr, retries);
336 if (ret!=1) {
337 printk(KERN_ERR "died at extended address code.\n");
338 return -EREMOTEIO;
339 }
340
341 i2c_outb(adap,msg->addr & 0x7f);
342
343 if (ret != 1) {
344 printk(KERN_ERR "died at 2nd address code.\n");
345 return -EREMOTEIO;
346 }
347 if ( flags & I2C_M_RD ) {
348 i2c_repstart(adap);
349
350 addr |= 0x01;
351 ret = try_address(adap, addr, retries);
352 if (ret!=1) {
353 printk(KERN_ERR "died at extended address code.\n");
354 return -EREMOTEIO;
355 }
356 }
357 } else {
358 addr = ( msg->addr << 1 );
359 if (flags & I2C_M_RD )
360 addr |= 1;
361 if (flags & I2C_M_REV_DIR_ADDR )
362 addr ^= 1;
363 i2c_outb(adap, addr);
364 }
365 return 0;
366}
367
368static int pcf_xfer(struct i2c_adapter *i2c_adap,
369 struct i2c_msg *msgs,
370 int num)
371{
372 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
373 struct i2c_msg *pmsg;
374 int i;
375 int ret=0, timeout, status;
376
377
378
379 timeout = wait_for_bb(adap);
380 if (timeout) {
381 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: "
382 "Timeout waiting for BB in pcf_xfer\n");)
383 return -EIO;
384 }
385
386 for (i = 0;ret >= 0 && i < num; i++) {
387 pmsg = &msgs[i];
388
389 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
390 pmsg->flags & I2C_M_RD ? "read" : "write",
391 pmsg->len, pmsg->addr, i + 1, num);)
392
393 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
394
395
396 if (i == 0) {
397 i2c_start(adap);
398 }
399
400
401 timeout = wait_for_pin(adap, &status);
402 if (timeout) {
403 if (timeout == -EINTR) {
404
405 return (-EINTR);
406 }
407 i2c_stop(adap);
408 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: Timeout waiting "
409 "for PIN(1) in pcf_xfer\n");)
410 return (-EREMOTEIO);
411 }
412
413#ifndef STUB_I2C
414
415 if (status & I2C_PCF_LRB) {
416 i2c_stop(adap);
417 DEB2(printk(KERN_ERR "i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
418 return (-EREMOTEIO);
419 }
420#endif
421
422 DEB3(printk(KERN_DEBUG "i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
423 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
424
425
426 if (pmsg->flags & I2C_M_RD) {
427
428 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len,
429 (i + 1 == num));
430
431 if (ret != pmsg->len) {
432 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
433 "only read %d bytes.\n",ret));
434 } else {
435 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: read %d bytes.\n",ret));
436 }
437 } else {
438 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
439 (i + 1 == num));
440
441 if (ret != pmsg->len) {
442 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: fail: "
443 "only wrote %d bytes.\n",ret));
444 } else {
445 DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: wrote %d bytes.\n",ret));
446 }
447 }
448 }
449
450 return (i);
451}
452
453static u32 pcf_func(struct i2c_adapter *adap)
454{
455 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
456 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
457}
458
459
460
461static const struct i2c_algorithm pcf_algo = {
462 .master_xfer = pcf_xfer,
463 .functionality = pcf_func,
464};
465
466
467
468
469int i2c_pcf_add_bus(struct i2c_adapter *adap)
470{
471 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
472 int rval;
473
474 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
475
476
477 adap->algo = &pcf_algo;
478
479 adap->timeout = 100;
480 adap->retries = 3;
481
482 if ((rval = pcf_init_8584(pcf_adap)))
483 return rval;
484
485 rval = i2c_add_adapter(adap);
486
487 return rval;
488}
489EXPORT_SYMBOL(i2c_pcf_add_bus);
490
491MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
492MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
493MODULE_LICENSE("GPL");
494
495module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
496MODULE_PARM_DESC(i2c_debug,
497 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
498