1
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 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* ------------------------------------------------------------------------- */
22
23 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
24 Frodo Looijaard <frodol@dds.nl> ,and also from Martin Bailey
25 <mbailey@littlefeet-inc.com> */
26
27 /* $Id: i2c-algo-pcf.c,v 1.25 2000/11/10 13:43:32 frodo Exp $ */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/malloc.h>
33 #include <linux/version.h>
34 #include <linux/init.h>
35 #include <asm/uaccess.h>
36 #include <linux/ioport.h>
37 #include <linux/errno.h>
38 #include <linux/sched.h>
39
40 #include <linux/i2c.h>
41 #include <linux/i2c-algo-pcf.h>
42 #include "i2c-pcf8584.h"
43
44 /* ----- global defines ----------------------------------------------- */
45 #define DEB(x) if (i2c_debug>=1) x
46 #define DEB2(x) if (i2c_debug>=2) x
47 #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values*/
48 #define DEBPROTO(x) if (i2c_debug>=9) x;
49 /* debug the protocol by showing transferred bits */
50 #define DEF_TIMEOUT 16
51
52 /* debugging - slow down transfer to have a look at the data .. */
53 /* I use this with two leds&resistors, each one connected to sda,scl */
54 /* respectively. This makes sure that the algorithm works. Some chips */
55 /* might not like this, as they have an internal timeout of some mils */
56 /*
57 #define SLO_IO jif=jiffies;while(jiffies<=jif+i2c_table[minor].veryslow)\
58 if (need_resched) schedule();
59 */
60
61
62 /* ----- global variables --------------------------------------------- */
63
64 #ifdef SLO_IO
65 int jif;
66 #endif
67
68 /* module parameters:
69 */
70 static int i2c_debug=1;
71 static int pcf_test=0; /* see if the line-setting functions work */
72 static int pcf_scan=0; /* have a look at what's hanging 'round */
73
74 /* --- setting states on the bus with the right timing: --------------- */
75
76 #define set_pcf(adap, ctl, val) adap->setpcf(adap->data, ctl, val)
77 #define get_pcf(adap, ctl) adap->getpcf(adap->data, ctl)
78 #define get_own(adap) adap->getown(adap->data)
79 #define get_clock(adap) adap->getclock(adap->data)
80 #define i2c_outb(adap, val) adap->setpcf(adap->data, 0, val)
81 #define i2c_inb(adap) adap->getpcf(adap->data, 0)
82
83
84 /* --- other auxiliary functions -------------------------------------- */
85
86 static void i2c_start(struct i2c_algo_pcf_data *adap)
87 {
88 DEBPROTO(printk("S "));
89 set_pcf(adap, 1, I2C_PCF_START);
90 }
91
92 static void i2c_repstart(struct i2c_algo_pcf_data *adap)
93 {
94 DEBPROTO(printk(" Sr "));
95 set_pcf(adap, 1, I2C_PCF_REPSTART);
96 }
97
98
99 static void i2c_stop(struct i2c_algo_pcf_data *adap)
100 {
101 DEBPROTO(printk("P\n"));
102 set_pcf(adap, 1, I2C_PCF_STOP);
103 }
104
105
106 static int wait_for_bb(struct i2c_algo_pcf_data *adap) {
107
108 int timeout = DEF_TIMEOUT;
109 int status;
110
111 status = get_pcf(adap, 1);
112 #ifndef STUB_I2C
113 while (timeout-- && !(status & I2C_PCF_BB)) {
114 udelay(1000); /* How much is this? */
115 status = get_pcf(adap, 1);
116 }
117 #endif
118 if (timeout<=0)
119 printk("Timeout waiting for Bus Busy\n");
120 /*
121 set_pcf(adap, 1, I2C_PCF_STOP);
122 */
123 return(timeout<=0);
124 }
125
126
127 static inline void pcf_sleep(unsigned long timeout)
128 {
129 schedule_timeout( timeout * HZ);
130 }
131
132
133 static int wait_for_pin(struct i2c_algo_pcf_data *adap, int *status) {
134
135 int timeout = DEF_TIMEOUT;
136
137 *status = get_pcf(adap, 1);
138 #ifndef STUB_I2C
139 while (timeout-- && (*status & I2C_PCF_PIN)) {
140 adap->waitforpin();
141 *status = get_pcf(adap, 1);
142 }
143 #endif
144 if (timeout <= 0)
145 return(-1);
146 else
147 return(0);
148 }
149
150
151 /*
152 * This should perform the 'PCF8584 initialization sequence' as described
153 * in the Philips IC12 data book (1995, Aug 29).
154 * There should be a 30 clock cycle wait after reset, I assume this
155 * has been fulfilled.
156 * There should be a delay at the end equal to the longest I2C message
157 * to synchronize the BB-bit (in multimaster systems). How long is
158 * this? I assume 1 second is always long enough.
159 */
160 static int pcf_init_8584 (struct i2c_algo_pcf_data *adap)
161 {
162
163 /* S1=0x80: S0 selected, serial interface off */
164 set_pcf(adap, 1, I2C_PCF_PIN);
165
166 /* load own address in S0, effective address is (own << 1) */
167 i2c_outb(adap, get_own(adap));
168
169 /* S1=0xA0, next byte in S2 */
170 set_pcf(adap, 1, I2C_PCF_PIN | I2C_PCF_ES1);
171
172 /* load clock register S2 */
173 i2c_outb(adap, get_clock(adap));
174
175 /* Enable serial interface, idle, S0 selected */
176 set_pcf(adap, 1, I2C_PCF_IDLE);
177
178 DEB2(printk("i2c-algo-pcf.o: irq: Initialized 8584.\n"));
179 return 0;
180 }
181
182
183 /*
184 * Sanity check for the adapter hardware - check the reaction of
185 * the bus lines only if it seems to be idle.
186 */
187 static int test_bus(struct i2c_algo_pcf_data *adap, char *name) {
188 #if 0
189 int scl,sda;
190 sda=getsda(adap);
191 if (adap->getscl==NULL) {
192 printk("i2c-algo-pcf.o: Warning: Adapter can't read from clock line - skipping test.\n");
193 return 0;
194 }
195 scl=getscl(adap);
196 printk("i2c-algo-pcf.o: Adapter: %s scl: %d sda: %d -- testing...\n",
197 name,getscl(adap),getsda(adap));
198 if (!scl || !sda ) {
199 printk("i2c-algo-pcf.o: %s seems to be busy.\n",adap->name);
200 goto bailout;
201 }
202 sdalo(adap);
203 printk("i2c-algo-pcf.o:1 scl: %d sda: %d \n",getscl(adap),
204 getsda(adap));
205 if ( 0 != getsda(adap) ) {
206 printk("i2c-algo-pcf.o: %s SDA stuck high!\n",name);
207 sdahi(adap);
208 goto bailout;
209 }
210 if ( 0 == getscl(adap) ) {
211 printk("i2c-algo-pcf.o: %s SCL unexpected low while pulling SDA low!\n",
212 name);
213 goto bailout;
214 }
215 sdahi(adap);
216 printk("i2c-algo-pcf.o:2 scl: %d sda: %d \n",getscl(adap),
217 getsda(adap));
218 if ( 0 == getsda(adap) ) {
219 printk("i2c-algo-pcf.o: %s SDA stuck low!\n",name);
220 sdahi(adap);
221 goto bailout;
222 }
223 if ( 0 == getscl(adap) ) {
224 printk("i2c-algo-pcf.o: %s SCL unexpected low while SDA high!\n",
225 adap->name);
226 goto bailout;
227 }
228 scllo(adap);
229 printk("i2c-algo-pcf.o:3 scl: %d sda: %d \n",getscl(adap),
230 getsda(adap));
231 if ( 0 != getscl(adap) ) {
232 printk("i2c-algo-pcf.o: %s SCL stuck high!\n",name);
233 sclhi(adap);
234 goto bailout;
235 }
236 if ( 0 == getsda(adap) ) {
237 printk("i2c-algo-pcf.o: %s SDA unexpected low while pulling SCL low!\n",
238 name);
239 goto bailout;
240 }
241 sclhi(adap);
242 printk("i2c-algo-pcf.o:4 scl: %d sda: %d \n",getscl(adap),
243 getsda(adap));
244 if ( 0 == getscl(adap) ) {
245 printk("i2c-algo-pcf.o: %s SCL stuck low!\n",name);
246 sclhi(adap);
247 goto bailout;
248 }
249 if ( 0 == getsda(adap) ) {
250 printk("i2c-algo-pcf.o: %s SDA unexpected low while SCL high!\n",
251 name);
252 goto bailout;
253 }
254 printk("i2c-algo-pcf.o: %s passed test.\n",name);
255 return 0;
256 bailout:
257 sdahi(adap);
258 sclhi(adap);
259 return -ENODEV;
260 #endif
261 return (0);
262 }
263
264 /* ----- Utility functions
265 */
266
267 static inline int try_address(struct i2c_algo_pcf_data *adap,
268 unsigned char addr, int retries)
269 {
270 int i, status, ret = -1;
271 for (i=0;i<retries;i++) {
272 i2c_outb(adap, addr);
273 i2c_start(adap);
274 status = get_pcf(adap, 1);
275 if (wait_for_pin(adap, &status) >= 0) {
276 if ((status & I2C_PCF_LRB) == 0) {
277 i2c_stop(adap);
278 break; /* success! */
279 }
280 }
281 i2c_stop(adap);
282 udelay(adap->udelay);
283 }
284 DEB2(if (i) printk("i2c-algo-pcf.o: needed %d retries for %d\n",i,
285 addr));
286 return ret;
287 }
288
289
290 static int pcf_sendbytes(struct i2c_adapter *i2c_adap,const char *buf,
291 int count)
292 {
293 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
294 int wrcount, status, timeout;
295
296 for (wrcount=0; wrcount<count; ++wrcount) {
297 DEB2(printk("i2c-algo-pcf.o: %s i2c_write: writing %2.2X\n",
298 i2c_adap->name, buf[wrcount]&0xff));
299 i2c_outb(adap, buf[wrcount]);
300 timeout = wait_for_pin(adap, &status);
301 if (timeout) {
302 i2c_stop(adap);
303 printk("i2c-algo-pcf.o: %s i2c_write: "
304 "error - timeout.\n", i2c_adap->name);
305 return -EREMOTEIO; /* got a better one ?? */
306 }
307 #ifndef STUB_I2C
308 if (status & I2C_PCF_LRB) {
309 i2c_stop(adap);
310 printk("i2c-algo-pcf.o: %s i2c_write: "
311 "error - no ack.\n", i2c_adap->name);
312 return -EREMOTEIO; /* got a better one ?? */
313 }
314 #endif
315 }
316 i2c_stop(adap);
317 return (wrcount);
318 }
319
320
321 static int pcf_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
322 {
323 int rdcount=0, i, status, timeout, dummy=1;
324 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
325
326 for (i=0; i<count; ++i) {
327 buf[rdcount] = i2c_inb(adap);
328 if (dummy) {
329 dummy = 0;
330 } else {
331 rdcount++;
332 }
333 timeout = wait_for_pin(adap, &status);
334 if (timeout) {
335 i2c_stop(adap);
336 printk("i2c-algo-pcf.o: i2c_read: "
337 "i2c_inb timed out.\n");
338 return (-1);
339 }
340 #ifndef STUB_I2C
341 if (status & I2C_PCF_LRB) {
342 i2c_stop(adap);
343 printk("i2c-algo-pcf.o: i2c_read: i2c_inb, No ack.\n");
344 return (-1);
345 }
346 #endif
347 }
348 set_pcf(adap, 1, I2C_PCF_ESO);
349 buf[rdcount] = i2c_inb(adap);
350 if (dummy) {
351 dummy = 0;
352 } else {
353 rdcount++;
354 }
355 timeout = wait_for_pin(adap, &status);
356 if (timeout) {
357 i2c_stop(adap);
358 printk("i2c-algo-pcf.o: i2c_read: i2c_inb timed out.\n");
359 return (-1);
360 }
361
362 i2c_stop(adap);
363
364 /* Read final byte from S0 register */
365 buf[rdcount++] = i2c_inb(adap);
366
367 return (rdcount);
368 }
369
370
371 static inline int pcf_doAddress(struct i2c_algo_pcf_data *adap,
372 struct i2c_msg *msg, int retries)
373 {
374 unsigned short flags = msg->flags;
375 unsigned char addr;
376 int ret;
377 if ( (flags & I2C_M_TEN) ) {
378 /* a ten bit address */
379 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
380 DEB2(printk("addr0: %d\n",addr));
381 /* try extended address code...*/
382 ret = try_address(adap, addr, retries);
383 if (ret!=1) {
384 printk("died at extended address code.\n");
385 return -EREMOTEIO;
386 }
387 /* the remaining 8 bit address */
388 i2c_outb(adap,msg->addr & 0x7f);
389 /* Status check comes here */
390 if (ret != 1) {
391 printk("died at 2nd address code.\n");
392 return -EREMOTEIO;
393 }
394 if ( flags & I2C_M_RD ) {
395 i2c_repstart(adap);
396 /* okay, now switch into reading mode */
397 addr |= 0x01;
398 ret = try_address(adap, addr, retries);
399 if (ret!=1) {
400 printk("died at extended address code.\n");
401 return -EREMOTEIO;
402 }
403 }
404 } else { /* normal 7bit address */
405 addr = ( msg->addr << 1 );
406 if (flags & I2C_M_RD )
407 addr |= 1;
408 if (flags & I2C_M_REV_DIR_ADDR )
409 addr ^= 1;
410 i2c_outb(adap, addr);
411 }
412 return 0;
413 }
414
415 static int pcf_xfer(struct i2c_adapter *i2c_adap,
416 struct i2c_msg msgs[],
417 int num)
418 {
419 struct i2c_algo_pcf_data *adap = i2c_adap->algo_data;
420 struct i2c_msg *pmsg;
421 int i = 0;
422 int ret, timeout, status;
423
424 pmsg = &msgs[i];
425
426 /* Send address here if Read */
427 if (pmsg->flags & I2C_M_RD) {
428 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
429 }
430
431 /* Check for bus busy */
432 timeout = wait_for_bb(adap);
433 if (timeout) {
434 DEB2(printk("i2c-algo-pcf.o: "
435 "Timeout waiting for BB in pcf_xfer\n");)
436 return -EIO;
437 }
438
439 /* Send address here if Write */
440 if (!(pmsg->flags & I2C_M_RD)) {
441 ret = pcf_doAddress(adap, pmsg, i2c_adap->retries);
442 }
443 /* Send START */
444 i2c_start(adap);
445
446 /* Wait for PIN (pending interrupt NOT) */
447 timeout = wait_for_pin(adap, &status);
448 if (timeout) {
449 i2c_stop(adap);
450 DEB2(printk("i2c-algo-pcf.o: Timeout waiting "
451 "for PIN(1) in pcf_xfer\n");)
452 return (-EREMOTEIO);
453 }
454
455 #ifndef STUB_I2C
456 /* Check LRB (last rcvd bit - slave ack) */
457 if (status & I2C_PCF_LRB) {
458 i2c_stop(adap);
459 DEB2(printk("i2c-algo-pcf.o: No LRB(1) in pcf_xfer\n");)
460 return (-EREMOTEIO);
461 }
462 #endif
463
464 DEB3(printk("i2c-algo-pcf.o: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",
465 i, msgs[i].addr, msgs[i].flags, msgs[i].len);)
466
467 /* Read */
468 if (pmsg->flags & I2C_M_RD) {
469
470 /* read bytes into buffer*/
471 ret = pcf_readbytes(i2c_adap, pmsg->buf, pmsg->len);
472
473 if (ret != pmsg->len) {
474 DEB2(printk("i2c-algo-pcf.o: fail: "
475 "only read %d bytes.\n",ret));
476 } else {
477 DEB2(printk("i2c-algo-pcf.o: read %d bytes.\n",ret));
478 }
479 } else { /* Write */
480
481 /* Write bytes from buffer */
482 ret = pcf_sendbytes(i2c_adap, pmsg->buf, pmsg->len);
483
484 if (ret != pmsg->len) {
485 DEB2(printk("i2c-algo-pcf.o: fail: "
486 "only wrote %d bytes.\n",ret));
487 } else {
488 DEB2(printk("i2c-algo-pcf.o: wrote %d bytes.\n",ret));
489 }
490 }
491 return (num);
492 }
493
494 static int algo_control(struct i2c_adapter *adapter,
495 unsigned int cmd, unsigned long arg)
496 {
497 return 0;
498 }
499
500 static u32 pcf_func(struct i2c_adapter *adap)
501 {
502 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
503 I2C_FUNC_PROTOCOL_MANGLING;
504 }
505
506 /* -----exported algorithm data: ------------------------------------- */
507
508 static struct i2c_algorithm pcf_algo = {
509 "PCF8584 algorithm",
510 I2C_ALGO_PCF,
511 pcf_xfer,
512 NULL,
513 NULL, /* slave_xmit */
514 NULL, /* slave_recv */
515 algo_control, /* ioctl */
516 pcf_func, /* functionality */
517 };
518
519 /*
520 * registering functions to load algorithms at runtime
521 */
522 int i2c_pcf_add_bus(struct i2c_adapter *adap)
523 {
524 int i, status;
525 struct i2c_algo_pcf_data *pcf_adap = adap->algo_data;
526
527 if (pcf_test) {
528 int ret = test_bus(pcf_adap, adap->name);
529 if (ret<0)
530 return -ENODEV;
531 }
532
533 DEB2(printk("i2c-algo-pcf.o: hw routines for %s registered.\n",
534 adap->name));
535
536 /* register new adapter to i2c module... */
537
538 adap->id |= pcf_algo.id;
539 adap->algo = &pcf_algo;
540
541 adap->timeout = 100; /* default values, should */
542 adap->retries = 3; /* be replaced by defines */
543
544 #ifdef MODULE
545 MOD_INC_USE_COUNT;
546 #endif
547
548 i2c_add_adapter(adap);
549 pcf_init_8584(pcf_adap);
550
551 /* scan bus */
552 if (pcf_scan) {
553 printk(KERN_INFO " i2c-algo-pcf.o: scanning bus %s.\n",
554 adap->name);
555 for (i = 0x00; i < 0xff; i+=2) {
556 i2c_outb(pcf_adap, i);
557 i2c_start(pcf_adap);
558 if ((wait_for_pin(pcf_adap, &status) >= 0) &&
559 ((status & I2C_PCF_LRB) == 0)) {
560 printk("(%02x)",i>>1);
561 } else {
562 printk(".");
563 }
564 i2c_stop(pcf_adap);
565 udelay(pcf_adap->udelay);
566 }
567 printk("\n");
568 }
569 return 0;
570 }
571
572
573 int i2c_pcf_del_bus(struct i2c_adapter *adap)
574 {
575 int res;
576 if ((res = i2c_del_adapter(adap)) < 0)
577 return res;
578 DEB2(printk("i2c-algo-pcf.o: adapter unregistered: %s\n",adap->name));
579
580 #ifdef MODULE
581 MOD_DEC_USE_COUNT;
582 #endif
583 return 0;
584 }
585
586 int __init i2c_algo_pcf_init (void)
587 {
588 printk("i2c-algo-pcf.o: i2c pcf8584 algorithm module\n");
589 return 0;
590 }
591
592
593 EXPORT_SYMBOL(i2c_pcf_add_bus);
594 EXPORT_SYMBOL(i2c_pcf_del_bus);
595
596 #ifdef MODULE
597 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
598 MODULE_DESCRIPTION("I2C-Bus PCF8584 algorithm");
599
600 MODULE_PARM(pcf_test, "i");
601 MODULE_PARM(pcf_scan, "i");
602 MODULE_PARM(i2c_debug,"i");
603
604 MODULE_PARM_DESC(pcf_test, "Test if the I2C bus is available");
605 MODULE_PARM_DESC(pcf_scan, "Scan for active chips on the bus");
606 MODULE_PARM_DESC(i2c_debug,
607 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 pcf-protocol");
608
609
610 int init_module(void)
611 {
612 return i2c_algo_pcf_init();
613 }
614
615 void cleanup_module(void)
616 {
617 }
618 #endif
619
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.