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