1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/errno.h>
25#include <linux/sched.h>
26#include <linux/i2c.h>
27#include <linux/i2c-algo-bit.h>
28
29
30
31
32#ifdef DEBUG
33#define bit_dbg(level, dev, format, args...) \
34 do { \
35 if (i2c_debug >= level) \
36 dev_dbg(dev, format, ##args); \
37 } while (0)
38#else
39#define bit_dbg(level, dev, format, args...) \
40 do {} while (0)
41#endif
42
43
44
45static int bit_test;
46module_param(bit_test, int, S_IRUGO);
47MODULE_PARM_DESC(bit_test, "lines testing - 0 off; 1 report; 2 fail if stuck");
48
49#ifdef DEBUG
50static int i2c_debug = 1;
51module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(i2c_debug,
53 "debug level - 0 off; 1 normal; 2 verbose; 3 very verbose");
54#endif
55
56
57
58#define setsda(adap, val) adap->setsda(adap->data, val)
59#define setscl(adap, val) adap->setscl(adap->data, val)
60#define getsda(adap) adap->getsda(adap->data)
61#define getscl(adap) adap->getscl(adap->data)
62
63static inline void sdalo(struct i2c_algo_bit_data *adap)
64{
65 setsda(adap, 0);
66 udelay((adap->udelay + 1) / 2);
67}
68
69static inline void sdahi(struct i2c_algo_bit_data *adap)
70{
71 setsda(adap, 1);
72 udelay((adap->udelay + 1) / 2);
73}
74
75static inline void scllo(struct i2c_algo_bit_data *adap)
76{
77 setscl(adap, 0);
78 udelay(adap->udelay / 2);
79}
80
81
82
83
84
85static int sclhi(struct i2c_algo_bit_data *adap)
86{
87 unsigned long start;
88
89 setscl(adap, 1);
90
91
92 if (!adap->getscl)
93 goto done;
94
95 start = jiffies;
96 while (!getscl(adap)) {
97
98
99
100
101
102 if (time_after(jiffies, start + adap->timeout)) {
103
104
105
106 if (getscl(adap))
107 break;
108 return -ETIMEDOUT;
109 }
110 cpu_relax();
111 }
112#ifdef DEBUG
113 if (jiffies != start && i2c_debug >= 3)
114 pr_debug("i2c-algo-bit: needed %ld jiffies for SCL to go "
115 "high\n", jiffies - start);
116#endif
117
118done:
119 udelay(adap->udelay);
120 return 0;
121}
122
123
124
125static void i2c_start(struct i2c_algo_bit_data *adap)
126{
127
128 setsda(adap, 0);
129 udelay(adap->udelay);
130 scllo(adap);
131}
132
133static void i2c_repstart(struct i2c_algo_bit_data *adap)
134{
135
136 sdahi(adap);
137 sclhi(adap);
138 setsda(adap, 0);
139 udelay(adap->udelay);
140 scllo(adap);
141}
142
143
144static void i2c_stop(struct i2c_algo_bit_data *adap)
145{
146
147 sdalo(adap);
148 sclhi(adap);
149 setsda(adap, 1);
150 udelay(adap->udelay);
151}
152
153
154
155
156
157
158
159
160
161
162static int i2c_outb(struct i2c_adapter *i2c_adap, unsigned char c)
163{
164 int i;
165 int sb;
166 int ack;
167 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
168
169
170 for (i = 7; i >= 0; i--) {
171 sb = (c >> i) & 1;
172 setsda(adap, sb);
173 udelay((adap->udelay + 1) / 2);
174 if (sclhi(adap) < 0) {
175 bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
176 "timeout at bit #%d\n", (int)c, i);
177 return -ETIMEDOUT;
178 }
179
180
181
182
183
184
185 scllo(adap);
186 }
187 sdahi(adap);
188 if (sclhi(adap) < 0) {
189 bit_dbg(1, &i2c_adap->dev, "i2c_outb: 0x%02x, "
190 "timeout at ack\n", (int)c);
191 return -ETIMEDOUT;
192 }
193
194
195
196
197 ack = !getsda(adap);
198 bit_dbg(2, &i2c_adap->dev, "i2c_outb: 0x%02x %s\n", (int)c,
199 ack ? "A" : "NA");
200
201 scllo(adap);
202 return ack;
203
204}
205
206
207static int i2c_inb(struct i2c_adapter *i2c_adap)
208{
209
210
211 int i;
212 unsigned char indata = 0;
213 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
214
215
216 sdahi(adap);
217 for (i = 0; i < 8; i++) {
218 if (sclhi(adap) < 0) {
219 bit_dbg(1, &i2c_adap->dev, "i2c_inb: timeout at bit "
220 "#%d\n", 7 - i);
221 return -ETIMEDOUT;
222 }
223 indata *= 2;
224 if (getsda(adap))
225 indata |= 0x01;
226 setscl(adap, 0);
227 udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
228 }
229
230 return indata;
231}
232
233
234
235
236
237static int test_bus(struct i2c_adapter *i2c_adap)
238{
239 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
240 const char *name = i2c_adap->name;
241 int scl, sda, ret;
242
243 if (adap->pre_xfer) {
244 ret = adap->pre_xfer(i2c_adap);
245 if (ret < 0)
246 return -ENODEV;
247 }
248
249 if (adap->getscl == NULL)
250 pr_info("%s: Testing SDA only, SCL is not readable\n", name);
251
252 sda = getsda(adap);
253 scl = (adap->getscl == NULL) ? 1 : getscl(adap);
254 if (!scl || !sda) {
255 printk(KERN_WARNING
256 "%s: bus seems to be busy (scl=%d, sda=%d)\n",
257 name, scl, sda);
258 goto bailout;
259 }
260
261 sdalo(adap);
262 sda = getsda(adap);
263 scl = (adap->getscl == NULL) ? 1 : getscl(adap);
264 if (sda) {
265 printk(KERN_WARNING "%s: SDA stuck high!\n", name);
266 goto bailout;
267 }
268 if (!scl) {
269 printk(KERN_WARNING "%s: SCL unexpected low "
270 "while pulling SDA low!\n", name);
271 goto bailout;
272 }
273
274 sdahi(adap);
275 sda = getsda(adap);
276 scl = (adap->getscl == NULL) ? 1 : getscl(adap);
277 if (!sda) {
278 printk(KERN_WARNING "%s: SDA stuck low!\n", name);
279 goto bailout;
280 }
281 if (!scl) {
282 printk(KERN_WARNING "%s: SCL unexpected low "
283 "while pulling SDA high!\n", name);
284 goto bailout;
285 }
286
287 scllo(adap);
288 sda = getsda(adap);
289 scl = (adap->getscl == NULL) ? 0 : getscl(adap);
290 if (scl) {
291 printk(KERN_WARNING "%s: SCL stuck high!\n", name);
292 goto bailout;
293 }
294 if (!sda) {
295 printk(KERN_WARNING "%s: SDA unexpected low "
296 "while pulling SCL low!\n", name);
297 goto bailout;
298 }
299
300 sclhi(adap);
301 sda = getsda(adap);
302 scl = (adap->getscl == NULL) ? 1 : getscl(adap);
303 if (!scl) {
304 printk(KERN_WARNING "%s: SCL stuck low!\n", name);
305 goto bailout;
306 }
307 if (!sda) {
308 printk(KERN_WARNING "%s: SDA unexpected low "
309 "while pulling SCL high!\n", name);
310 goto bailout;
311 }
312
313 if (adap->post_xfer)
314 adap->post_xfer(i2c_adap);
315
316 pr_info("%s: Test OK\n", name);
317 return 0;
318bailout:
319 sdahi(adap);
320 sclhi(adap);
321
322 if (adap->post_xfer)
323 adap->post_xfer(i2c_adap);
324
325 return -ENODEV;
326}
327
328
329
330
331
332
333
334
335
336
337
338static int try_address(struct i2c_adapter *i2c_adap,
339 unsigned char addr, int retries)
340{
341 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
342 int i, ret = 0;
343
344 for (i = 0; i <= retries; i++) {
345 ret = i2c_outb(i2c_adap, addr);
346 if (ret == 1 || i == retries)
347 break;
348 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
349 i2c_stop(adap);
350 udelay(adap->udelay);
351 yield();
352 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
353 i2c_start(adap);
354 }
355 if (i && ret)
356 bit_dbg(1, &i2c_adap->dev, "Used %d tries to %s client at "
357 "0x%02x: %s\n", i + 1,
358 addr & 1 ? "read from" : "write to", addr >> 1,
359 ret == 1 ? "success" : "failed, timeout?");
360 return ret;
361}
362
363static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
364{
365 const unsigned char *temp = msg->buf;
366 int count = msg->len;
367 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
368 int retval;
369 int wrcount = 0;
370
371 while (count > 0) {
372 retval = i2c_outb(i2c_adap, *temp);
373
374
375 if ((retval > 0) || (nak_ok && (retval == 0))) {
376 count--;
377 temp++;
378 wrcount++;
379
380
381
382
383
384 } else if (retval == 0) {
385 dev_err(&i2c_adap->dev, "sendbytes: NAK bailout.\n");
386 return -EIO;
387
388
389
390
391
392
393
394
395 } else {
396 dev_err(&i2c_adap->dev, "sendbytes: error %d\n",
397 retval);
398 return retval;
399 }
400 }
401 return wrcount;
402}
403
404static int acknak(struct i2c_adapter *i2c_adap, int is_ack)
405{
406 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
407
408
409 if (is_ack)
410 setsda(adap, 0);
411 udelay((adap->udelay + 1) / 2);
412 if (sclhi(adap) < 0) {
413 dev_err(&i2c_adap->dev, "readbytes: ack/nak timeout\n");
414 return -ETIMEDOUT;
415 }
416 scllo(adap);
417 return 0;
418}
419
420static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
421{
422 int inval;
423 int rdcount = 0;
424 unsigned char *temp = msg->buf;
425 int count = msg->len;
426 const unsigned flags = msg->flags;
427
428 while (count > 0) {
429 inval = i2c_inb(i2c_adap);
430 if (inval >= 0) {
431 *temp = inval;
432 rdcount++;
433 } else {
434 break;
435 }
436
437 temp++;
438 count--;
439
440
441
442 if (rdcount == 1 && (flags & I2C_M_RECV_LEN)) {
443 if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
444 if (!(flags & I2C_M_NO_RD_ACK))
445 acknak(i2c_adap, 0);
446 dev_err(&i2c_adap->dev, "readbytes: invalid "
447 "block length (%d)\n", inval);
448 return -EPROTO;
449 }
450
451
452
453 count += inval;
454 msg->len += inval;
455 }
456
457 bit_dbg(2, &i2c_adap->dev, "readbytes: 0x%02x %s\n",
458 inval,
459 (flags & I2C_M_NO_RD_ACK)
460 ? "(no ack/nak)"
461 : (count ? "A" : "NA"));
462
463 if (!(flags & I2C_M_NO_RD_ACK)) {
464 inval = acknak(i2c_adap, count);
465 if (inval < 0)
466 return inval;
467 }
468 }
469 return rdcount;
470}
471
472
473
474
475
476
477
478
479
480static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
481{
482 unsigned short flags = msg->flags;
483 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
484 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
485
486 unsigned char addr;
487 int ret, retries;
488
489 retries = nak_ok ? 0 : i2c_adap->retries;
490
491 if (flags & I2C_M_TEN) {
492
493 addr = 0xf0 | ((msg->addr >> 7) & 0x06);
494 bit_dbg(2, &i2c_adap->dev, "addr0: %d\n", addr);
495
496 ret = try_address(i2c_adap, addr, retries);
497 if ((ret != 1) && !nak_ok) {
498 dev_err(&i2c_adap->dev,
499 "died at extended address code\n");
500 return -ENXIO;
501 }
502
503 ret = i2c_outb(i2c_adap, msg->addr & 0xff);
504 if ((ret != 1) && !nak_ok) {
505
506 dev_err(&i2c_adap->dev, "died at 2nd address code\n");
507 return -ENXIO;
508 }
509 if (flags & I2C_M_RD) {
510 bit_dbg(3, &i2c_adap->dev, "emitting repeated "
511 "start condition\n");
512 i2c_repstart(adap);
513
514 addr |= 0x01;
515 ret = try_address(i2c_adap, addr, retries);
516 if ((ret != 1) && !nak_ok) {
517 dev_err(&i2c_adap->dev,
518 "died at repeated address code\n");
519 return -EIO;
520 }
521 }
522 } else {
523 addr = msg->addr << 1;
524 if (flags & I2C_M_RD)
525 addr |= 1;
526 if (flags & I2C_M_REV_DIR_ADDR)
527 addr ^= 1;
528 ret = try_address(i2c_adap, addr, retries);
529 if ((ret != 1) && !nak_ok)
530 return -ENXIO;
531 }
532
533 return 0;
534}
535
536static int bit_xfer(struct i2c_adapter *i2c_adap,
537 struct i2c_msg msgs[], int num)
538{
539 struct i2c_msg *pmsg;
540 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
541 int i, ret;
542 unsigned short nak_ok;
543
544 if (adap->pre_xfer) {
545 ret = adap->pre_xfer(i2c_adap);
546 if (ret < 0)
547 return ret;
548 }
549
550 bit_dbg(3, &i2c_adap->dev, "emitting start condition\n");
551 i2c_start(adap);
552 for (i = 0; i < num; i++) {
553 pmsg = &msgs[i];
554 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
555 if (!(pmsg->flags & I2C_M_NOSTART)) {
556 if (i) {
557 bit_dbg(3, &i2c_adap->dev, "emitting "
558 "repeated start condition\n");
559 i2c_repstart(adap);
560 }
561 ret = bit_doAddress(i2c_adap, pmsg);
562 if ((ret != 0) && !nak_ok) {
563 bit_dbg(1, &i2c_adap->dev, "NAK from "
564 "device addr 0x%02x msg #%d\n",
565 msgs[i].addr, i);
566 goto bailout;
567 }
568 }
569 if (pmsg->flags & I2C_M_RD) {
570
571 ret = readbytes(i2c_adap, pmsg);
572 if (ret >= 1)
573 bit_dbg(2, &i2c_adap->dev, "read %d byte%s\n",
574 ret, ret == 1 ? "" : "s");
575 if (ret < pmsg->len) {
576 if (ret >= 0)
577 ret = -EIO;
578 goto bailout;
579 }
580 } else {
581
582 ret = sendbytes(i2c_adap, pmsg);
583 if (ret >= 1)
584 bit_dbg(2, &i2c_adap->dev, "wrote %d byte%s\n",
585 ret, ret == 1 ? "" : "s");
586 if (ret < pmsg->len) {
587 if (ret >= 0)
588 ret = -EIO;
589 goto bailout;
590 }
591 }
592 }
593 ret = i;
594
595bailout:
596 bit_dbg(3, &i2c_adap->dev, "emitting stop condition\n");
597 i2c_stop(adap);
598
599 if (adap->post_xfer)
600 adap->post_xfer(i2c_adap);
601 return ret;
602}
603
604static u32 bit_func(struct i2c_adapter *adap)
605{
606 return I2C_FUNC_I2C | I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_EMUL |
607 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
608 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
609 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
610}
611
612
613
614
615const struct i2c_algorithm i2c_bit_algo = {
616 .master_xfer = bit_xfer,
617 .functionality = bit_func,
618};
619EXPORT_SYMBOL(i2c_bit_algo);
620
621
622
623
624static int __i2c_bit_add_bus(struct i2c_adapter *adap,
625 int (*add_adapter)(struct i2c_adapter *))
626{
627 struct i2c_algo_bit_data *bit_adap = adap->algo_data;
628 int ret;
629
630 if (bit_test) {
631 ret = test_bus(adap);
632 if (bit_test >= 2 && ret < 0)
633 return -ENODEV;
634 }
635
636
637 adap->algo = &i2c_bit_algo;
638 adap->retries = 3;
639
640 ret = add_adapter(adap);
641 if (ret < 0)
642 return ret;
643
644
645 if (bit_adap->getscl == NULL) {
646 dev_warn(&adap->dev, "Not I2C compliant: can't read SCL\n");
647 dev_warn(&adap->dev, "Bus may be unreliable\n");
648 }
649 return 0;
650}
651
652int i2c_bit_add_bus(struct i2c_adapter *adap)
653{
654 return __i2c_bit_add_bus(adap, i2c_add_adapter);
655}
656EXPORT_SYMBOL(i2c_bit_add_bus);
657
658int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)
659{
660 return __i2c_bit_add_bus(adap, i2c_add_numbered_adapter);
661}
662EXPORT_SYMBOL(i2c_bit_add_numbered_bus);
663
664MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
665MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
666MODULE_LICENSE("GPL");
667