patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / cris / arch-v10 / drivers / i2c.c
1 /*!***************************************************************************
2 *!
3 *! FILE NAME  : i2c.c
4 *!
5 *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
6 *!              kernel modules (i2c_writereg/readreg) and from userspace using
7 *!              ioctl()'s
8 *!
9 *! Nov 30 1998  Torbjorn Eliasson  Initial version.
10 *!              Bjorn Wesen        Elinux kernel version.
11 *! Jan 14 2000  Johan Adolfsson    Fixed PB shadow register stuff - 
12 *!                                 don't use PB_I2C if DS1302 uses same bits,
13 *!                                 use PB.
14 *! $Log: i2c.c,v $
15 *! Revision 1.7  2004/05/28 09:26:59  starvik
16 *! Modified I2C initialization to work in 2.6.
17 *!
18 *! Revision 1.6  2004/05/14 07:58:03  starvik
19 *! Merge of changes from 2.4
20 *!
21 *! Revision 1.4  2002/12/11 13:13:57  starvik
22 *! Added arch/ to v10 specific includes
23 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
24 *!
25 *! Revision 1.3  2002/11/20 11:56:11  starvik
26 *! Merge of Linux 2.5.48
27 *!
28 *! Revision 1.2  2002/11/18 13:16:06  starvik
29 *! Linux 2.5 port of latest 2.4 drivers
30 *!
31 *! Revision 1.9  2002/10/31 15:32:26  starvik
32 *! Update Port B register and shadow even when running with hardware support
33 *!   to avoid glitches when reading bits
34 *! Never set direction to out in i2c_inbyte
35 *! Removed incorrect clock togling at end of i2c_inbyte
36 *!
37 *! Revision 1.8  2002/08/13 06:31:53  starvik
38 *! Made SDA and SCL line configurable
39 *! Modified i2c_inbyte to work with PCF8563
40 *!
41 *! Revision 1.7  2001/04/04 13:11:36  markusl
42 *! Updated according to review remarks
43 *!
44 *! Revision 1.6  2001/03/19 12:43:00  markusl
45 *! Made some symbols unstatic (used by the eeprom driver)
46 *!
47 *! Revision 1.5  2001/02/27 13:52:48  bjornw
48 *! malloc.h -> slab.h
49 *!
50 *! Revision 1.4  2001/02/15 07:17:40  starvik
51 *! Corrected usage if port_pb_i2c_shadow
52 *!
53 *! Revision 1.3  2001/01/26 17:55:13  bjornw
54 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
55 *!   magically. Config.in needs to set it for the options that need it, like
56 *!   Dallas 1302 support. Actually, it should be default since it screws up
57 *!   the PB bits even if you don't use I2C..
58 *! * Include linux/config.h to get the above
59 *!
60 *! Revision 1.2  2001/01/18 15:49:30  bjornw
61 *! 2.4 port of I2C including some cleanups (untested of course)
62 *!
63 *! Revision 1.1  2001/01/18 15:35:25  bjornw
64 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
65 *!
66 *!
67 *! ---------------------------------------------------------------------------
68 *!
69 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
70 *!
71 *!***************************************************************************/
72 /* $Id: i2c.c,v 1.7 2004/05/28 09:26:59 starvik Exp $ */
73
74 /****************** INCLUDE FILES SECTION ***********************************/
75
76 #include <linux/module.h>
77 #include <linux/sched.h>
78 #include <linux/slab.h>
79 #include <linux/errno.h>
80 #include <linux/kernel.h>
81 #include <linux/fs.h>
82 #include <linux/string.h>
83 #include <linux/init.h>
84 #include <linux/config.h>
85
86 #include <asm/etraxi2c.h>
87
88 #include <asm/system.h>
89 #include <asm/arch/svinto.h>
90 #include <asm/io.h>
91 #include <asm/delay.h>
92
93 #include "i2c.h"
94
95 /****************** I2C DEFINITION SECTION *************************/
96
97 #define D(x)
98
99 #define I2C_MAJOR 123  /* LOCAL/EXPERIMENTAL */
100 static const char i2c_name[] = "i2c";
101
102 #define CLOCK_LOW_TIME            8
103 #define CLOCK_HIGH_TIME           8
104 #define START_CONDITION_HOLD_TIME 8
105 #define STOP_CONDITION_HOLD_TIME  8
106 #define ENABLE_OUTPUT 0x01
107 #define ENABLE_INPUT 0x00
108 #define I2C_CLOCK_HIGH 1
109 #define I2C_CLOCK_LOW 0
110 #define I2C_DATA_HIGH 1
111 #define I2C_DATA_LOW 0
112
113 #if 0
114 /* TODO: fix this so the CONFIG_ETRAX_I2C_USES... is set in Config.in instead */
115 #if defined(CONFIG_DS1302) && (CONFIG_DS1302_SDABIT==0) && \
116            (CONFIG_DS1302_SCLBIT == 1)
117 #define CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
118 #endif
119 #endif
120
121 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
122 /* Use PB and not PB_I2C */
123 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
124 #define CONFIG_ETRAX_I2C_DATA_PORT 0
125 #endif
126 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
127 #define CONFIG_ETRAX_I2C_CLK_PORT 1
128 #endif
129
130 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
131 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
132 #define i2c_enable() 
133 #define i2c_disable() 
134
135 /* enable or disable output-enable, to select output or input on the i2c bus */
136
137 #define i2c_dir_out() \
138   REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
139 #define i2c_dir_in()  \
140   REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
141
142 /* control the i2c clock and data signals */
143
144 #define i2c_clk(x) \
145   REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
146 #define i2c_data(x) \
147   REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
148
149 /* read a bit from the i2c interface */
150
151 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
152
153 #else
154 /* enable or disable the i2c interface */
155
156 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
157 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
158
159 /* enable or disable output-enable, to select output or input on the i2c bus */
160
161 #define i2c_dir_out() \
162         *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
163         REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1); 
164 #define i2c_dir_in() \
165         *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
166         REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
167
168 /* control the i2c clock and data signals */
169
170 #define i2c_clk(x) \
171         *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
172        ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
173        REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
174
175 #define i2c_data(x) \
176         *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
177            ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
178         REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
179
180 /* read a bit from the i2c interface */
181
182 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
183 #endif
184
185 /* use the kernels delay routine */
186
187 #define i2c_delay(usecs) udelay(usecs)
188
189
190 /****************** FUNCTION DEFINITION SECTION *************************/
191
192
193 /* generate i2c start condition */
194
195 void
196 i2c_start(void)
197 {
198         /*
199          * SCL=1 SDA=1
200          */
201         i2c_dir_out();
202         i2c_delay(CLOCK_HIGH_TIME/6);
203         i2c_data(I2C_DATA_HIGH);
204         i2c_clk(I2C_CLOCK_HIGH);
205         i2c_delay(CLOCK_HIGH_TIME);
206         /*
207          * SCL=1 SDA=0
208          */
209         i2c_data(I2C_DATA_LOW);
210         i2c_delay(START_CONDITION_HOLD_TIME);
211         /*
212          * SCL=0 SDA=0
213          */
214         i2c_clk(I2C_CLOCK_LOW);
215         i2c_delay(CLOCK_LOW_TIME);
216 }
217
218 /* generate i2c stop condition */
219
220 void
221 i2c_stop(void)
222 {
223         i2c_dir_out();
224
225         /*
226          * SCL=0 SDA=0
227          */
228         i2c_clk(I2C_CLOCK_LOW);
229         i2c_data(I2C_DATA_LOW);
230         i2c_delay(CLOCK_LOW_TIME*2);
231         /*
232          * SCL=1 SDA=0
233          */
234         i2c_clk(I2C_CLOCK_HIGH);
235         i2c_delay(CLOCK_HIGH_TIME*2);
236         /*
237          * SCL=1 SDA=1
238          */
239         i2c_data(I2C_DATA_HIGH);
240         i2c_delay(STOP_CONDITION_HOLD_TIME);
241
242         i2c_dir_in();
243 }
244
245 /* write a byte to the i2c interface */
246
247 void
248 i2c_outbyte(unsigned char x)
249 {
250         int i;
251
252         i2c_dir_out();
253
254         for (i = 0; i < 8; i++) {
255                 if (x & 0x80) {
256                         i2c_data(I2C_DATA_HIGH);
257                 } else {
258                         i2c_data(I2C_DATA_LOW);
259                 }
260                 
261                 i2c_delay(CLOCK_LOW_TIME/2);
262                 i2c_clk(I2C_CLOCK_HIGH);
263                 i2c_delay(CLOCK_HIGH_TIME);
264                 i2c_clk(I2C_CLOCK_LOW);
265                 i2c_delay(CLOCK_LOW_TIME/2);
266                 x <<= 1;
267         }
268         i2c_data(I2C_DATA_LOW);
269         i2c_delay(CLOCK_LOW_TIME/2);
270
271         /*
272          * enable input
273          */
274         i2c_dir_in();
275 }
276
277 /* read a byte from the i2c interface */
278
279 unsigned char
280 i2c_inbyte(void)
281 {
282         unsigned char aBitByte = 0;
283         int i;
284
285         /* Switch off I2C to get bit */
286         i2c_disable();
287         i2c_dir_in();
288         i2c_delay(CLOCK_HIGH_TIME/2);
289
290         /* Get bit */
291         aBitByte |= i2c_getbit();
292
293         /* Enable I2C */
294         i2c_enable();
295         i2c_delay(CLOCK_LOW_TIME/2);
296
297         for (i = 1; i < 8; i++) {
298                 aBitByte <<= 1;
299                 /* Clock pulse */
300                 i2c_clk(I2C_CLOCK_HIGH);
301                 i2c_delay(CLOCK_HIGH_TIME);
302                 i2c_clk(I2C_CLOCK_LOW);
303                 i2c_delay(CLOCK_LOW_TIME);
304
305                 /* Switch off I2C to get bit */
306                 i2c_disable();
307                 i2c_dir_in();
308                 i2c_delay(CLOCK_HIGH_TIME/2);
309
310                 /* Get bit */
311                 aBitByte |= i2c_getbit();
312
313                 /* Enable I2C */
314                 i2c_enable();
315                 i2c_delay(CLOCK_LOW_TIME/2);
316         }
317         i2c_clk(I2C_CLOCK_HIGH);
318         i2c_delay(CLOCK_HIGH_TIME);
319
320         /*
321          * we leave the clock low, getbyte is usually followed
322          * by sendack/nack, they assume the clock to be low
323          */
324         i2c_clk(I2C_CLOCK_LOW);
325         return aBitByte;
326 }
327
328 /*#---------------------------------------------------------------------------
329 *#
330 *# FUNCTION NAME: i2c_getack
331 *#
332 *# DESCRIPTION  : checks if ack was received from ic2
333 *#
334 *#--------------------------------------------------------------------------*/
335
336 int
337 i2c_getack(void)
338 {
339         int ack = 1;
340         /*
341          * enable output
342          */
343         i2c_dir_out();
344         /*
345          * Release data bus by setting
346          * data high
347          */
348         i2c_data(I2C_DATA_HIGH);
349         /*
350          * enable input
351          */
352         i2c_dir_in();
353         i2c_delay(CLOCK_HIGH_TIME/4);
354         /*
355          * generate ACK clock pulse
356          */
357         i2c_clk(I2C_CLOCK_HIGH);
358         /*
359          * Use PORT PB instead of I2C
360          * for input. (I2C not working)
361          */
362         i2c_clk(1);
363         i2c_data(1);
364         /*
365          * switch off I2C
366          */
367         i2c_data(1);
368         i2c_disable();
369         i2c_dir_in();
370         /*
371          * now wait for ack
372          */
373         i2c_delay(CLOCK_HIGH_TIME/2);
374         /*
375          * check for ack
376          */
377         if(i2c_getbit())
378                 ack = 0;
379         i2c_delay(CLOCK_HIGH_TIME/2);
380         if(!ack){
381                 if(!i2c_getbit()) /* receiver pulld SDA low */
382                         ack = 1;
383                 i2c_delay(CLOCK_HIGH_TIME/2);
384         }
385
386         /*
387          * our clock is high now, make sure data is low
388          * before we enable our output. If we keep data high
389          * and enable output, we would generate a stop condition.
390          */
391         i2c_data(I2C_DATA_LOW);
392
393         /*
394          * end clock pulse
395          */
396         i2c_enable();
397         i2c_dir_out();
398         i2c_clk(I2C_CLOCK_LOW);
399         i2c_delay(CLOCK_HIGH_TIME/4);
400         /*
401          * enable output
402          */
403         i2c_dir_out();
404         /*
405          * remove ACK clock pulse
406          */
407         i2c_data(I2C_DATA_HIGH);
408         i2c_delay(CLOCK_LOW_TIME/2);
409         return ack;
410 }
411
412 /*#---------------------------------------------------------------------------
413 *#
414 *# FUNCTION NAME: I2C::sendAck
415 *#
416 *# DESCRIPTION  : Send ACK on received data
417 *#
418 *#--------------------------------------------------------------------------*/
419 void
420 i2c_sendack(void)
421 {
422         /*
423          * enable output
424          */
425         i2c_delay(CLOCK_LOW_TIME);
426         i2c_dir_out();
427         /*
428          * set ack pulse high
429          */
430         i2c_data(I2C_DATA_LOW);
431         /*
432          * generate clock pulse
433          */
434         i2c_delay(CLOCK_HIGH_TIME/6);
435         i2c_clk(I2C_CLOCK_HIGH);
436         i2c_delay(CLOCK_HIGH_TIME);
437         i2c_clk(I2C_CLOCK_LOW);
438         i2c_delay(CLOCK_LOW_TIME/6);
439         /*
440          * reset data out
441          */
442         i2c_data(I2C_DATA_HIGH);
443         i2c_delay(CLOCK_LOW_TIME);
444         
445         i2c_dir_in();
446 }
447
448 /*#---------------------------------------------------------------------------
449 *#
450 *# FUNCTION NAME: i2c_sendnack
451 *#
452 *# DESCRIPTION  : Sends NACK on received data
453 *#
454 *#--------------------------------------------------------------------------*/
455 void
456 i2c_sendnack(void)
457 {
458         /*
459          * enable output
460          */
461         i2c_delay(CLOCK_LOW_TIME);
462         i2c_dir_out();
463         /*
464          * set data high
465          */
466         i2c_data(I2C_DATA_HIGH);
467         /*
468          * generate clock pulse
469          */
470         i2c_delay(CLOCK_HIGH_TIME/6);
471         i2c_clk(I2C_CLOCK_HIGH);
472         i2c_delay(CLOCK_HIGH_TIME);
473         i2c_clk(I2C_CLOCK_LOW);
474         i2c_delay(CLOCK_LOW_TIME);
475
476         i2c_dir_in();
477 }
478
479 /*#---------------------------------------------------------------------------
480 *#
481 *# FUNCTION NAME: i2c_writereg
482 *#
483 *# DESCRIPTION  : Writes a value to an I2C device
484 *#
485 *#--------------------------------------------------------------------------*/
486 int
487 i2c_writereg(unsigned char theSlave, unsigned char theReg, 
488              unsigned char theValue)
489 {
490         int error, cntr = 3;
491         unsigned long flags;
492
493         do {
494                 error = 0;
495                 /*
496                  * we don't like to be interrupted
497                  */
498                 local_irq_save(flags);
499                 local_irq_disable();
500
501                 i2c_start();
502                 /*
503                  * send slave address
504                  */
505                 i2c_outbyte((theSlave & 0xfe));
506                 /*
507                  * wait for ack
508                  */
509                 if(!i2c_getack())
510                         error = 1;
511                 /*
512                  * now select register
513                  */
514                 i2c_dir_out();
515                 i2c_outbyte(theReg);
516                 /*
517                  * now it's time to wait for ack
518                  */
519                 if(!i2c_getack())
520                         error |= 2;
521                 /*
522                  * send register register data
523                  */
524                 i2c_outbyte(theValue);
525                 /*
526                  * now it's time to wait for ack
527                  */
528                 if(!i2c_getack())
529                         error |= 4;
530                 /*
531                  * end byte stream
532                  */
533                 i2c_stop();
534                 /*
535                  * enable interrupt again
536                  */
537                 local_irq_restore(flags);
538                 
539         } while(error && cntr--);
540
541         i2c_delay(CLOCK_LOW_TIME);
542
543         return -error;
544 }
545
546 /*#---------------------------------------------------------------------------
547 *#
548 *# FUNCTION NAME: i2c_readreg
549 *#
550 *# DESCRIPTION  : Reads a value from the decoder registers.
551 *#
552 *#--------------------------------------------------------------------------*/
553 unsigned char
554 i2c_readreg(unsigned char theSlave, unsigned char theReg)
555 {
556         unsigned char b = 0;
557         int error, cntr = 3;
558         unsigned long flags;
559
560         do {
561                 error = 0;
562                 /*
563                  * we don't like to be interrupted
564                  */
565                 local_irq_save(flags);
566                 local_irq_disable();
567                 /*
568                  * generate start condition
569                  */
570                 i2c_start();
571     
572                 /*
573                  * send slave address
574                  */
575                 i2c_outbyte((theSlave & 0xfe));
576                 /*
577                  * wait for ack
578                  */
579                 if(!i2c_getack())
580                         error = 1;
581                 /*
582                  * now select register
583                  */
584                 i2c_dir_out();
585                 i2c_outbyte(theReg);
586                 /*
587                  * now it's time to wait for ack
588                  */
589                 if(!i2c_getack())
590                         error = 1;
591                 /*
592                  * repeat start condition
593                  */
594                 i2c_delay(CLOCK_LOW_TIME);
595                 i2c_start();
596                 /*
597                  * send slave address
598                  */
599                 i2c_outbyte(theSlave | 0x01);
600                 /*
601                  * wait for ack
602                  */
603                 if(!i2c_getack())
604                         error = 1;
605                 /*
606                  * fetch register
607                  */
608                 b = i2c_inbyte();
609                 /*
610                  * last received byte needs to be nacked
611                  * instead of acked
612                  */
613                 i2c_sendack();
614                 /*
615                  * end sequence
616                  */
617                 i2c_stop();
618                 /*
619                  * enable interrupt again
620                  */
621                 local_irq_restore(flags);
622                 
623         } while(error && cntr--);
624
625         return b;
626 }
627
628 static int
629 i2c_open(struct inode *inode, struct file *filp)
630 {
631         return 0;
632 }
633
634 static int
635 i2c_release(struct inode *inode, struct file *filp)
636 {
637         return 0;
638 }
639
640 /* Main device API. ioctl's to write or read to/from i2c registers.
641  */
642
643 static int
644 i2c_ioctl(struct inode *inode, struct file *file,
645           unsigned int cmd, unsigned long arg)
646 {
647         if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
648                 return -EINVAL;
649         }
650
651         switch (_IOC_NR(cmd)) {
652                 case I2C_WRITEREG:
653                         /* write to an i2c slave */
654                         D(printk("i2cw %d %d %d\n", 
655                                  I2C_ARGSLAVE(arg),
656                                  I2C_ARGREG(arg),
657                                  I2C_ARGVALUE(arg)));
658
659                         return i2c_writereg(I2C_ARGSLAVE(arg),
660                                             I2C_ARGREG(arg),
661                                             I2C_ARGVALUE(arg));
662                 case I2C_READREG:
663                 {
664                         unsigned char val;
665                         /* read from an i2c slave */
666                         D(printk("i2cr %d %d ", 
667                                 I2C_ARGSLAVE(arg),
668                                 I2C_ARGREG(arg)));
669                         val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
670                         D(printk("= %d\n", val));
671                         return val;
672                 }                                           
673                 default:
674                         return -EINVAL;
675
676         }
677         
678         return 0;
679 }
680
681 static struct file_operations i2c_fops = {
682         .owner    = THIS_MODULE,
683         .ioctl    = i2c_ioctl,
684         .open     = i2c_open,
685         .release  = i2c_release,
686 };
687
688 int __init
689 i2c_init(void)
690 {
691         /* Setup and enable the Port B I2C interface */
692
693 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
694         *R_PORT_PB_I2C = port_pb_i2c_shadow |= 
695                 IO_STATE(R_PORT_PB_I2C, i2c_en,  on) |
696                 IO_FIELD(R_PORT_PB_I2C, i2c_d,   1)  |
697                 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1)  |
698                 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
699 #endif
700
701         port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
702         port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
703
704         *R_PORT_PB_DIR = (port_pb_dir_shadow |=
705                           IO_STATE(R_PORT_PB_DIR, dir0, input)  |
706                           IO_STATE(R_PORT_PB_DIR, dir1, output));
707
708         return 0;
709 }
710
711 static int __init
712 i2c_register(void)
713 {
714         int res;
715
716         i2c_init();
717         res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
718         if(res < 0) {
719                 printk(KERN_ERR "i2c: couldn't get a major number.\n");
720                 return res;
721         }
722
723         printk(KERN_INFO "I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
724         
725         return 0;
726 }
727
728 /* this makes sure that i2c_register is called during boot */
729
730 module_init(i2c_register);
731
732 /****************** END OF FILE i2c.c ********************************/