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#include <linux/module.h>
29#include <linux/sched.h>
30#include <linux/slab.h>
31#include <linux/errno.h>
32#include <linux/kernel.h>
33#include <linux/fs.h>
34#include <linux/string.h>
35#include <linux/init.h>
36#include <linux/smp_lock.h>
37
38#include <asm/etraxi2c.h>
39
40#include <asm/system.h>
41#include <asm/io.h>
42#include <asm/delay.h>
43
44#include "i2c.h"
45
46
47
48#define D(x)
49
50#define I2C_MAJOR 123
51static const char i2c_name[] = "i2c";
52
53#define CLOCK_LOW_TIME 8
54#define CLOCK_HIGH_TIME 8
55#define START_CONDITION_HOLD_TIME 8
56#define STOP_CONDITION_HOLD_TIME 8
57#define ENABLE_OUTPUT 0x01
58#define ENABLE_INPUT 0x00
59#define I2C_CLOCK_HIGH 1
60#define I2C_CLOCK_LOW 0
61#define I2C_DATA_HIGH 1
62#define I2C_DATA_LOW 0
63
64#define i2c_enable()
65#define i2c_disable()
66
67
68
69#define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
70#define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
71
72
73
74#define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
75#define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
76
77
78
79#define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
80
81#define i2c_delay(usecs) udelay(usecs)
82
83static DEFINE_SPINLOCK(i2c_lock);
84
85
86
87static struct crisv32_iopin cris_i2c_clk;
88static struct crisv32_iopin cris_i2c_data;
89
90
91
92
93
94
95void
96i2c_start(void)
97{
98
99
100
101 i2c_dir_out();
102 i2c_delay(CLOCK_HIGH_TIME/6);
103 i2c_data(I2C_DATA_HIGH);
104 i2c_clk(I2C_CLOCK_HIGH);
105 i2c_delay(CLOCK_HIGH_TIME);
106
107
108
109 i2c_data(I2C_DATA_LOW);
110 i2c_delay(START_CONDITION_HOLD_TIME);
111
112
113
114 i2c_clk(I2C_CLOCK_LOW);
115 i2c_delay(CLOCK_LOW_TIME);
116}
117
118
119
120void
121i2c_stop(void)
122{
123 i2c_dir_out();
124
125
126
127
128 i2c_clk(I2C_CLOCK_LOW);
129 i2c_data(I2C_DATA_LOW);
130 i2c_delay(CLOCK_LOW_TIME*2);
131
132
133
134 i2c_clk(I2C_CLOCK_HIGH);
135 i2c_delay(CLOCK_HIGH_TIME*2);
136
137
138
139 i2c_data(I2C_DATA_HIGH);
140 i2c_delay(STOP_CONDITION_HOLD_TIME);
141
142 i2c_dir_in();
143}
144
145
146
147void
148i2c_outbyte(unsigned char x)
149{
150 int i;
151
152 i2c_dir_out();
153
154 for (i = 0; i < 8; i++) {
155 if (x & 0x80) {
156 i2c_data(I2C_DATA_HIGH);
157 } else {
158 i2c_data(I2C_DATA_LOW);
159 }
160
161 i2c_delay(CLOCK_LOW_TIME/2);
162 i2c_clk(I2C_CLOCK_HIGH);
163 i2c_delay(CLOCK_HIGH_TIME);
164 i2c_clk(I2C_CLOCK_LOW);
165 i2c_delay(CLOCK_LOW_TIME/2);
166 x <<= 1;
167 }
168 i2c_data(I2C_DATA_LOW);
169 i2c_delay(CLOCK_LOW_TIME/2);
170
171
172
173
174 i2c_dir_in();
175}
176
177
178
179unsigned char
180i2c_inbyte(void)
181{
182 unsigned char aBitByte = 0;
183 int i;
184
185
186 i2c_disable();
187 i2c_dir_in();
188 i2c_delay(CLOCK_HIGH_TIME/2);
189
190
191 aBitByte |= i2c_getbit();
192
193
194 i2c_enable();
195 i2c_delay(CLOCK_LOW_TIME/2);
196
197 for (i = 1; i < 8; i++) {
198 aBitByte <<= 1;
199
200 i2c_clk(I2C_CLOCK_HIGH);
201 i2c_delay(CLOCK_HIGH_TIME);
202 i2c_clk(I2C_CLOCK_LOW);
203 i2c_delay(CLOCK_LOW_TIME);
204
205
206 i2c_disable();
207 i2c_dir_in();
208 i2c_delay(CLOCK_HIGH_TIME/2);
209
210
211 aBitByte |= i2c_getbit();
212
213
214 i2c_enable();
215 i2c_delay(CLOCK_LOW_TIME/2);
216 }
217 i2c_clk(I2C_CLOCK_HIGH);
218 i2c_delay(CLOCK_HIGH_TIME);
219
220
221
222
223
224 i2c_clk(I2C_CLOCK_LOW);
225 return aBitByte;
226}
227
228
229
230
231
232
233
234
235
236int
237i2c_getack(void)
238{
239 int ack = 1;
240
241
242
243 i2c_dir_out();
244
245
246
247
248 i2c_data(I2C_DATA_HIGH);
249
250
251
252 i2c_dir_in();
253 i2c_delay(CLOCK_HIGH_TIME/4);
254
255
256
257 i2c_clk(I2C_CLOCK_HIGH);
258#if 0
259
260
261
262
263 i2c_clk(1);
264 i2c_data(1);
265
266
267
268 i2c_data(1);
269 i2c_disable();
270 i2c_dir_in();
271#endif
272
273
274
275
276 i2c_delay(CLOCK_HIGH_TIME/2);
277
278
279
280 if (i2c_getbit())
281 ack = 0;
282 i2c_delay(CLOCK_HIGH_TIME/2);
283 if (!ack) {
284 if (!i2c_getbit())
285 ack = 1;
286 i2c_delay(CLOCK_HIGH_TIME/2);
287 }
288
289
290
291
292
293
294#if 0
295 i2c_data(I2C_DATA_LOW);
296
297
298
299
300 i2c_enable();
301 i2c_dir_out();
302#endif
303 i2c_clk(I2C_CLOCK_LOW);
304 i2c_delay(CLOCK_HIGH_TIME/4);
305
306
307
308 i2c_dir_out();
309
310
311
312 i2c_data(I2C_DATA_HIGH);
313 i2c_delay(CLOCK_LOW_TIME/2);
314 return ack;
315}
316
317
318
319
320
321
322
323
324void
325i2c_sendack(void)
326{
327
328
329
330 i2c_delay(CLOCK_LOW_TIME);
331 i2c_dir_out();
332
333
334
335 i2c_data(I2C_DATA_LOW);
336
337
338
339 i2c_delay(CLOCK_HIGH_TIME/6);
340 i2c_clk(I2C_CLOCK_HIGH);
341 i2c_delay(CLOCK_HIGH_TIME);
342 i2c_clk(I2C_CLOCK_LOW);
343 i2c_delay(CLOCK_LOW_TIME/6);
344
345
346
347 i2c_data(I2C_DATA_HIGH);
348 i2c_delay(CLOCK_LOW_TIME);
349
350 i2c_dir_in();
351}
352
353
354
355
356
357
358
359
360void
361i2c_sendnack(void)
362{
363
364
365
366 i2c_delay(CLOCK_LOW_TIME);
367 i2c_dir_out();
368
369
370
371 i2c_data(I2C_DATA_HIGH);
372
373
374
375 i2c_delay(CLOCK_HIGH_TIME/6);
376 i2c_clk(I2C_CLOCK_HIGH);
377 i2c_delay(CLOCK_HIGH_TIME);
378 i2c_clk(I2C_CLOCK_LOW);
379 i2c_delay(CLOCK_LOW_TIME);
380
381 i2c_dir_in();
382}
383
384
385
386
387
388
389
390
391int
392i2c_write(unsigned char theSlave, void *data, size_t nbytes)
393{
394 int error, cntr = 3;
395 unsigned char bytes_wrote = 0;
396 unsigned char value;
397 unsigned long flags;
398
399 spin_lock_irqsave(&i2c_lock, flags);
400
401 do {
402 error = 0;
403
404 i2c_start();
405
406
407
408 i2c_outbyte((theSlave & 0xfe));
409
410
411
412 if (!i2c_getack())
413 error = 1;
414
415
416
417 for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {
418 memcpy(&value, data + bytes_wrote, sizeof value);
419 i2c_outbyte(value);
420
421
422
423 if (!i2c_getack())
424 error |= 4;
425 }
426
427
428
429 i2c_stop();
430
431 } while (error && cntr--);
432
433 i2c_delay(CLOCK_LOW_TIME);
434
435 spin_unlock_irqrestore(&i2c_lock, flags);
436
437 return -error;
438}
439
440
441
442
443
444
445
446
447int
448i2c_read(unsigned char theSlave, void *data, size_t nbytes)
449{
450 unsigned char b = 0;
451 unsigned char bytes_read = 0;
452 int error, cntr = 3;
453 unsigned long flags;
454
455 spin_lock_irqsave(&i2c_lock, flags);
456
457 do {
458 error = 0;
459 memset(data, 0, nbytes);
460
461
462
463 i2c_start();
464
465
466
467 i2c_outbyte((theSlave | 0x01));
468
469
470
471 if (!i2c_getack())
472 error = 1;
473
474
475
476 for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {
477 b = i2c_inbyte();
478 memcpy(data + bytes_read, &b, sizeof b);
479
480 if (bytes_read < (nbytes - 1))
481 i2c_sendack();
482 }
483
484
485
486
487 i2c_sendnack();
488
489
490
491 i2c_stop();
492 } while (error && cntr--);
493
494 spin_unlock_irqrestore(&i2c_lock, flags);
495
496 return -error;
497}
498
499
500
501
502
503
504
505
506int
507i2c_writereg(unsigned char theSlave, unsigned char theReg,
508 unsigned char theValue)
509{
510 int error, cntr = 3;
511 unsigned long flags;
512
513 spin_lock_irqsave(&i2c_lock, flags);
514
515 do {
516 error = 0;
517
518 i2c_start();
519
520
521
522 i2c_outbyte((theSlave & 0xfe));
523
524
525
526 if(!i2c_getack())
527 error = 1;
528
529
530
531 i2c_dir_out();
532 i2c_outbyte(theReg);
533
534
535
536 if(!i2c_getack())
537 error |= 2;
538
539
540
541 i2c_outbyte(theValue);
542
543
544
545 if(!i2c_getack())
546 error |= 4;
547
548
549
550 i2c_stop();
551 } while(error && cntr--);
552
553 i2c_delay(CLOCK_LOW_TIME);
554
555 spin_unlock_irqrestore(&i2c_lock, flags);
556
557 return -error;
558}
559
560
561
562
563
564
565
566
567unsigned char
568i2c_readreg(unsigned char theSlave, unsigned char theReg)
569{
570 unsigned char b = 0;
571 int error, cntr = 3;
572 unsigned long flags;
573
574 spin_lock_irqsave(&i2c_lock, flags);
575
576 do {
577 error = 0;
578
579
580
581 i2c_start();
582
583
584
585
586 i2c_outbyte((theSlave & 0xfe));
587
588
589
590 if(!i2c_getack())
591 error = 1;
592
593
594
595 i2c_dir_out();
596 i2c_outbyte(theReg);
597
598
599
600 if(!i2c_getack())
601 error |= 2;
602
603
604
605 i2c_delay(CLOCK_LOW_TIME);
606 i2c_start();
607
608
609
610 i2c_outbyte(theSlave | 0x01);
611
612
613
614 if(!i2c_getack())
615 error |= 4;
616
617
618
619 b = i2c_inbyte();
620
621
622
623
624 i2c_sendnack();
625
626
627
628 i2c_stop();
629
630 } while(error && cntr--);
631
632 spin_unlock_irqrestore(&i2c_lock, flags);
633
634 return b;
635}
636
637static int
638i2c_open(struct inode *inode, struct file *filp)
639{
640 cycle_kernel_lock();
641 return 0;
642}
643
644static int
645i2c_release(struct inode *inode, struct file *filp)
646{
647 return 0;
648}
649
650
651
652
653static int
654i2c_ioctl(struct inode *inode, struct file *file,
655 unsigned int cmd, unsigned long arg)
656{
657 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
658 return -ENOTTY;
659 }
660
661 switch (_IOC_NR(cmd)) {
662 case I2C_WRITEREG:
663
664 D(printk("i2cw %d %d %d\n",
665 I2C_ARGSLAVE(arg),
666 I2C_ARGREG(arg),
667 I2C_ARGVALUE(arg)));
668
669 return i2c_writereg(I2C_ARGSLAVE(arg),
670 I2C_ARGREG(arg),
671 I2C_ARGVALUE(arg));
672 case I2C_READREG:
673 {
674 unsigned char val;
675
676 D(printk("i2cr %d %d ",
677 I2C_ARGSLAVE(arg),
678 I2C_ARGREG(arg)));
679 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
680 D(printk("= %d\n", val));
681 return val;
682 }
683 default:
684 return -EINVAL;
685
686 }
687
688 return 0;
689}
690
691static const struct file_operations i2c_fops = {
692 .owner = THIS_MODULE,
693 .ioctl = i2c_ioctl,
694 .open = i2c_open,
695 .release = i2c_release,
696};
697
698static int __init i2c_init(void)
699{
700 static int res;
701 static int first = 1;
702
703 if (!first)
704 return res;
705
706 first = 0;
707
708
709
710 res = crisv32_io_get_name(&cris_i2c_data,
711 CONFIG_ETRAX_V32_I2C_DATA_PORT);
712 if (res < 0)
713 return res;
714
715 res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);
716 crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);
717
718 return res;
719}
720
721
722static int __init i2c_register(void)
723{
724 int res;
725
726 res = i2c_init();
727 if (res < 0)
728 return res;
729
730
731
732 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
733 if (res < 0) {
734 printk(KERN_ERR "i2c: couldn't get a major number.\n");
735 return res;
736 }
737
738 printk(KERN_INFO
739 "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
740
741 return 0;
742}
743
744module_init(i2c_register);
745
746
747