Add i2c driver (to etrax) improved by Geert Vancompernolle
[librecmc/librecmc.git] / target / linux / etrax / files / arch / cris / arch-v10 / drivers / i2c_gvc.c
1 /*!***************************************************************************
2 *!
3 *! FILE NAME  : i2c.c
4 *!
5 *!
6 *! ---------------------------------------------------------------------------
7 *!
8 *! ( C ) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
9 *!
10 *!***************************************************************************/
11 /* $Id: i2c.c,v 1.16 2005/09/29 13:33:35 bjarne Exp $ */
12
13 #define DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
14 //#undef  DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
15
16 /******************** INCLUDE FILES SECTION ****************************/
17
18 #include <linux/module.h>
19 #include <linux/fs.h>
20
21 /**GVC**/
22 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
23 #include <linux/types.h> /* for dev_t */
24 #include <linux/cdev.h>  /* for struct cdev */
25 #endif
26 /**END GVC**/
27
28 #include "etraxi2c.h"
29
30 /**GVC**/
31 #include "i2c_errno.h"
32 /**END GVC**/
33
34 #include <asm/io.h>
35 #include <asm/delay.h>
36 #include <asm/arch/io_interface_mux.h>
37 #include <asm/uaccess.h>
38
39 #include "i2c_gvc.h"
40
41 MODULE_DESCRIPTION( "I2C Device Driver - 1.1" );
42
43 /*!*********************************************************************
44  *!History I2C driver Geert Vancompernolle
45  *!---------------------------------------
46  *!
47  *! - v1.0:
48  *!     First official version.
49  *!
50  *! - v1.1:
51  *!     Changes to remove unwanted spikes at ACK/NACK time.
52  *!
53  *!*********************************************************************/
54  
55 MODULE_LICENSE( "GPL" );
56
57 /******************            MACRO's            **********************/
58
59 #define D( x )
60
61 /**GVC**/
62 #ifndef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
63 /**END GVC**/
64 #define I2C_MAJOR 123                           /* LOCAL/EXPERIMENTAL */
65 /**GVC**/
66 #endif
67 /**END GVC**/
68
69 /**GVC**/
70 #define WAITONEUS                 1
71 /* Following are abbreviations taken from Philips I2C standard */
72 /* Values are representing time in us and are rounded to next whole number, if relevant */ 
73 #define THDSTA                    4     /* Hold delay time for (repeated) START condition */
74 #define TLOW                      5     /* LOW period of the SCL clock */
75 #define THDDAT                    1     /* Hold delay time for DATA: value of 0 is allowed but 1 taken to be sure */
76 #define TSUDAT                    1     /* Set-up time for DATA */
77 #define THIGH                     4     /* HIGH period of the SCL clock */
78 #define TSUSTA                    5     /* Set-up time for a repeated START condition */
79 #define TSUSTO                    4     /* Set-up time for STOP condition */
80 #define TBUF                      5     /* Bus-free time between STOP and START condition */
81
82 #define MAXBUSFREERETRIES         5
83 #define MAXRETRIES                3
84 #define WRITEADDRESS_MASK         ( 0xFE )
85 #define READADDRESS_MASK          ( 0x01 )
86 /**END GVC**/
87
88 #define SCL_HIGH                  1
89 #define SCL_LOW                   0
90 #define SDA_HIGH                  1
91 #define SDA_LOW                   0
92
93 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
94 /* Use PB and not PB_I2C */
95 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
96 #define CONFIG_ETRAX_I2C_DATA_PORT 0
97 #endif
98 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
99 #define CONFIG_ETRAX_I2C_CLK_PORT 1
100 #endif
101
102 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
103 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
104 #define i2c_enable() 
105 #define i2c_disable() 
106
107 /* enable or disable output-enable, to select output or input on the i2c bus */
108 #define i2c_sda_dir_out() \
109   REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1 )
110 #define i2c_sda_dir_in()  \
111   REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0 )
112
113 /* control the i2c clock and data signals */
114 #define i2c_set_scl( x ) \
115   REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x )
116 #define i2c_set_sda( x ) \
117   REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x )
118
119 /* read status of SDA bit from the i2c interface */
120 #define i2c_sda_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SDABIT ) ) ) >> SDABIT )
121
122 /**GVC**/
123 /* read status of SCL bit from the i2c interface */
124 #define i2c_scl_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SCLBIT ) ) ) >> SCLBIT )
125 /**END GVC**/
126
127 #else
128 /* enable or disable the i2c interface */
129 #define i2c_enable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_en ) )
130 #define i2c_disable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_en ) )
131
132 /* enable or disable output-enable, to select output or input on the i2c bus */
133 #define i2c_sda_dir_out() \
134         *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
135         REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1 ); 
136 #define i2c_sda_dir_in() \
137         *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
138         REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0 );
139
140 /* control the i2c clock and data signals */
141 #define i2c_set_scl( x ) \
142         *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
143        ~IO_MASK( R_PORT_PB_I2C, i2c_set_scl ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, ( x ) ) ); \
144        REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 1, x );
145
146 #define i2c_set_sda( x ) \
147         *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
148            ~IO_MASK( R_PORT_PB_I2C, i2c_d ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_d, ( x ) ) ); \
149         REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 0, x );
150
151 /* read a bit from the i2c interface */
152 #define i2c_sda_is_high() ( *R_PORT_PB_READ & 0x1 )
153 #endif
154
155 /* use the kernels delay routine */
156 #define i2c_delay( usecs ) udelay( usecs )
157
158
159 /******************           TYPEDEF's           **********************/
160
161
162 /****************** STATIC (file scope) VARIABLES **********************/
163 static DEFINE_SPINLOCK( i2c_lock ); /* Protect directions etc */
164 /**GVC**/
165 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
166 static const char i2c_name[] = "i2cgvc";
167 #else
168 static const char i2c_name[] = "i2c";
169 #endif
170 /**END GVC**/
171
172
173 /******************     PROTOTYPING SECTION     *************************/
174 static int  i2c_open( struct inode *inode, struct file *filp );
175 static int  i2c_release( struct inode *inode, struct file *filp );
176 /**GVC**/
177 static int  i2c_command( unsigned char  slave
178                        , unsigned char* wbuf
179                        , unsigned char  wlen
180                        , unsigned char* rbuf
181                        , unsigned char  rlen
182                        );
183 static int  i2c_bus_free_check( unsigned char maxretries );
184 static void i2c_finalise( const char* text, unsigned long irqflags );
185 /**END GVC**/
186                            
187
188 /************************************************************************/
189 /******************         AUXILIARIES         *************************/
190 /************************************************************************/
191
192 /*#---------------------------------------------------------------------------
193  *#
194  *# FUNCTION NAME: i2c_open
195  *#
196  *# DESCRIPTION  : opens an I2C device
197  *#
198  *# PARAMETERS   : *inode: reference to inode
199  *#                *filp : reference to file pointer 
200  *#
201  *#---------------------------------------------------------------------------
202  */
203 static int i2c_open( struct inode *inode, struct file *filp )
204 {
205     return 0;
206 }   /* i2c_open */
207
208
209 /*#---------------------------------------------------------------------------
210  *#
211  *# FUNCTION NAME: i2c_release
212  *#
213  *# DESCRIPTION  : Releases the I2C device
214  *#
215  *# PARAMETERS   : *inode: reference to inode
216  *#                *filp : reference to file pointer 
217  *#
218  *#---------------------------------------------------------------------------
219  */
220 static int i2c_release( struct inode *inode, struct file *filp )
221 {
222     return 0;
223 }   /* i2c_release */
224
225
226 /*#---------------------------------------------------------------------------
227  *#
228  *# FUNCTION NAME: i2c_ioctl
229  *#
230  *# DESCRIPTION  : Main device API: ioctl's to write/read 
231  *#                to/from i2c registers
232  *#
233  *# PARAMETERS   : *inode: reference to inode
234  *#                *filp : reference to file pointer 
235  *#                cmd   : command to be executed during the ioctl call 
236  *#                arg   : pointer to a structure with the data??? 
237  *#
238  *# RETURN       : result of the ioctl call
239  *#
240  *#---------------------------------------------------------------------------
241  */
242 static int i2c_ioctl( struct inode *inode
243                     , struct file *file
244                     , unsigned int cmd
245                     , unsigned long arg
246                     )
247 {
248     /* the acme ioctls */
249     I2C_DATA i2cdata;
250     int RetVal = EI2CNOERRORS;
251     
252     if ( _IOC_TYPE( cmd ) != ETRAXI2C_IOCTYPE ) 
253     {
254         return ( -EINVAL );
255     }
256     
257     switch ( _IOC_NR( cmd ) ) 
258     {
259     case I2C_WRITEREG:
260         /* write to an i2c slave */
261         RetVal = i2c_writereg( I2C_ARGSLAVE( arg )
262                              , I2C_ARGREG( arg )
263                              , I2C_ARGVALUE( arg )
264                              );
265         break;                       
266
267     case I2C_READREG:
268         RetVal = i2c_readreg( I2C_ARGSLAVE( arg ), I2C_ARGREG( arg ) );
269         break;
270     
271 /**GVC**/
272     /* New functions added by GVC */    
273     case I2C_READ:
274         copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
275         {
276             int RetryCntr = MAXRETRIES;
277             
278             do
279             {
280                 RetVal = i2c_command( i2cdata.slave
281                                     , NULL
282                                     , 0 
283                                     , i2cdata.rbuf
284                                     , i2cdata.rlen
285                                     );
286              } while ( ( EI2CNOERRORS != RetVal )
287                      &&( --RetryCntr )
288                      );
289         }
290         copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
291         break;
292
293     case I2C_WRITE:
294         copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
295         {
296             int RetryCntr = MAXRETRIES;
297             
298             do
299             {
300                 RetVal = i2c_command( i2cdata.slave
301                                     , i2cdata.wbuf
302                                     , i2cdata.wlen
303                                     , NULL
304                                     , 0 
305                                     );
306              } while ( ( EI2CNOERRORS != RetVal )
307                      &&( --RetryCntr )
308                      );
309         }
310         break;
311           
312     case I2C_WRITEREAD:
313         copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
314         {
315             int RetryCntr = MAXRETRIES;
316             
317             do
318             {
319                 RetVal = i2c_command( i2cdata.slave
320                                     , i2cdata.wbuf
321                                     , i2cdata.wlen
322                                     , i2cdata.rbuf
323                                     , i2cdata.rlen 
324                                     );
325              } while ( ( EI2CNOERRORS != RetVal )
326                      &&( --RetryCntr )
327                      );
328         }
329         copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
330         break;
331 /**END GVC**/    
332     
333     default:
334         RetVal = -EINVAL;
335     }
336     
337     return ( -RetVal );
338 }   /* i2c_ioctl */
339
340
341 /*#---------------------------------------------------------------------------
342  *#
343  *# FUNCTION NAME: i2c_command
344  *#
345  *# DESCRIPTION  : general routine to read/write bytes from an I2C device
346  *#                
347  *#                'i2c_command()' sends wlen bytes to the I2c bus and receives
348  *#                rlen bytes from the I2c bus.  
349  *#                The data to be send must be placed in wbuf[ 0 ] upto wbuf[ wlen - 1 ).
350  *#                The data to be received is assembled in rbuf[ 0 ] upto rbuf[ rlen - 1 ].
351  *#                 
352  *#                If no data is to be sent or received, put appropriate buffer parameter
353  *#                to "NULL" and appropriate length parameter to "0".
354  *# 
355  *# PARAMETERS   : slave = slave address of the I2C device
356  *#                wbuf  = address of first element of write buffer (wbuf)
357  *#                wlen  = number of bytes to be written to slave
358  *#                rbuf  = address of first element of read buffer (rbuf)
359  *#                rlen  = number of bytes to be read from slave
360  *#
361  *# RETURN       : 
362  *#    EI2CNOERRORS: I2C communication went fine
363  *#    EI2CBUSNFREE: I2C bus is not free
364  *#    EI2CWADDRESS: I2C write address failed
365  *#    EI2CRADDRESS: I2C read address failed
366  *#    EI2CSENDDATA: I2C send data failed
367  *#    EI2CRECVDATA: I2C receive data failed
368  *#    EI2CSTRTCOND: I2C start condition failed
369  *#    EI2CRSTACOND: I2C repeated start condition failed
370  *#    EI2CSTOPCOND: I2C stop condition failed
371  *#    EI2CNOSNDBYT: I2C no bytes to be sent
372  *#    EI2CNOSNDBUF: I2C no send buffer defined
373  *#    EI2CNORCVBYT: I2C no bytes to be received
374  *#    EI2CNORCVBUF: I2C no receive buffer defined
375  *#    EI2CNOACKNLD: I2C no acknowledge received
376  *#
377  *# REMARK       :
378  *#   First, the send part is completed.  
379  *#   In the send routine, there is no stop generated.  This is because maybe
380  *#   a repeated start condition must be generated.
381  *#   This happens when we want to receive some data from the I2c bus.  If not,
382  *#   at the end of the general I2c loop the stopcondition is generated.
383  *#   If, on the contrary, there are a number of bytes to be received, a new
384  *#   startcondition is generated in the 'if' part of the main I2c routine, 
385  *#   which controls the receiving part.  
386  *#   Only when the receiving of data is finished, a final stopcondition is 
387  *#   generated.
388  *#
389  *#---------------------------------------------------------------------------
390  */
391 static int i2c_command( unsigned char  slave
392                       , unsigned char* wbuf
393                       , unsigned char  wlen
394                       , unsigned char* rbuf
395                       , unsigned char  rlen
396                       )
397 {
398     /* Check arguments and report error if relevant... */
399     if ( ( wlen > 0 ) && ( wbuf == NULL ) )
400     {
401         printk( KERN_DEBUG "I2C: EI2CNOSNDBUF\n" );
402         return ( EI2CNOSNDBUF );
403     }
404     else if ( ( wlen == 0 ) && ( wbuf != NULL ) )
405     {
406         printk( KERN_DEBUG "I2C: EI2CNOSNDBYT\n" );
407         return ( EI2CNOSNDBYT );
408     }
409     else if ( ( rlen > 0 ) && ( rbuf == NULL ) )
410     {
411         printk( KERN_DEBUG "I2C: EI2CNORCVBUF\n" );
412         return ( EI2CNORCVBUF );
413     }
414     else if ( ( rlen == 0 ) && ( rbuf != NULL ) )
415     {
416         printk( KERN_DEBUG "I2C: EI2CNORCVBYT\n" );
417         return ( EI2CNORCVBYT );
418     }
419     else if ( EI2CBUSNFREE == i2c_bus_free_check( MAXBUSFREERETRIES ) )
420     {
421         /* There's no need to try more, since we weren't even
422          * able to start the I2C communication.
423          * So, no IRQ flags are stored yet, no changes to any other
424          * stuff like START, STOP, SENDBYTES...
425          * Result, simply write down the error and return the correct error code.
426          */
427         printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
428         return ( EI2CBUSNFREE );
429     }
430     else
431     {
432         /* Finally... We made it... */
433         unsigned long irqflags = 0;
434
435         /* we don't like to be interrupted */
436         local_irq_save( irqflags );
437
438         /* Check if there are bytes to be send, 
439          * or if you immediately want to receive data.
440          */
441         if ( 0 < wlen )
442         {
443             /* start I2C communication */        
444             if ( EI2CNOERRORS != i2c_start() )
445             {
446                 return ( i2c_finalise( "I2C: EI2CSTRTCOND\n", irqflags  )
447                        , EI2CSTRTCOND 
448                        );
449             }
450
451             /* send slave address: xxxxxxx0B (last bit must be zero) */
452             if ( EI2CNOERRORS != i2c_outbyte( slave & WRITEADDRESS_MASK ) )
453             {
454                 return ( i2c_finalise( "I2C: EI2CWADDRESS\n", irqflags  )
455                        , EI2CWADDRESS 
456                        );
457             }
458
459             while ( wlen-- )
460             {   
461                 /* send register data */
462                 if ( EI2CNOERRORS != i2c_outbyte( *wbuf ) )
463                 {
464                     return ( i2c_finalise( "I2C: EI2CSENDDATA\n", irqflags  )
465                            , EI2CSENDDATA 
466                            );
467                 }
468                 
469                 wbuf++;
470             };
471                 
472             i2c_delay( TLOW );
473         }
474
475         /*
476          * Receiving data from I2c_bus
477          * If there are bytes to be received, a new start condition is
478          * generated => Repeated Startcondition.
479          * A final stopcondition is generated at the end of the main I2c
480          * routine.
481          */
482         if ( 0 < rlen )
483         {
484             /*
485              * Generate start condition if wlen == 0 
486              * or repeated start condition if wlen != 0...
487              */
488             if ( EI2CNOERRORS != i2c_start() )
489             {
490                 return ( i2c_finalise( ( ( 0 < wlen ) 
491                                        ? "I2C: EI2CRSTACOND\n"
492                                        : "I2C: EI2CSTRTCOND\n"
493                                        )
494                                      , irqflags
495                                      )
496                        , ( ( 0 < wlen ) ? EI2CRSTACOND : EI2CSTRTCOND )
497                        );
498             }
499
500             /* Send ReadAddress: xxxxxxx1B (last bit must be one) */
501             if ( EI2CNOERRORS != i2c_outbyte( slave | READADDRESS_MASK ) )
502             {
503                 return ( i2c_finalise( "I2C: EI2CRADDRESS\n", irqflags )
504                        , EI2CRADDRESS
505                        );
506             }
507             
508             while ( rlen-- )
509             {
510                 /* fetch register */
511                 *rbuf = i2c_inbyte();
512                 rbuf++;
513                 
514                 /* last received byte needs to be NACK-ed instead of ACK-ed */
515                 if ( rlen )
516                 {
517                     i2c_sendack();
518                 }
519                 else 
520                 {
521                     i2c_sendnack(); 
522                 }
523             };
524         }
525
526         /* Generate final stop condition */
527         if ( EI2CNOERRORS != i2c_stop() )
528         {
529             return ( i2c_finalise( "I2C: EI2CSTOPCOND\n", irqflags )
530                    , EI2CSTOPCOND
531                    );
532         } 
533     
534         /* enable interrupt again */
535         local_irq_restore( irqflags );
536     }
537     
538     return ( EI2CNOERRORS );
539 } /*  i2c_command */
540
541
542 /*#---------------------------------------------------------------------------
543  *#
544  *# FUNCTION NAME: i2c_bus_free_check
545  *#
546  *# DESCRIPTION  : checks if the I2C bus is free before starting
547  *#                an I2C communication
548  *# 
549  *# PARAMETERS   : maxretries, the number of times we will try to release
550  *#                the I2C bus  
551  *#
552  *# RETURN       : I2cStatus_I2cBusNotFreeError in case the bus is not free,
553  *#                I2cStatus_I2cNoError otherwise 
554  *#
555  *#---------------------------------------------------------------------------
556  */
557 static int i2c_bus_free_check( unsigned char maxretries )
558 {
559     i2c_sda_dir_in();        /* Release SDA line */
560     i2c_set_scl( SCL_HIGH ); /* put SCL line high */
561     
562     i2c_delay( WAITONEUS );
563         
564     while ( ( !i2c_sda_is_high() || !i2c_scl_is_high() )
565           &&( maxretries-- )
566           )
567     {
568         /* Try to release I2C bus by generating STOP conditions */
569         i2c_stop();
570     }
571     
572     if ( 0 == maxretries )
573     {
574         printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
575         return ( EI2CBUSNFREE );
576     }
577     else 
578     {
579         return ( EI2CNOERRORS );
580     }
581 } /* i2c_bus_free_check */
582
583
584 static void i2c_finalise( const char* errortxt
585                         , unsigned long irqflags
586                         )
587 {
588     printk( KERN_DEBUG "%s", errortxt );
589     local_irq_restore( irqflags );
590     /* The least we can do when things go terribly wrong,
591      * is to try to release the bus.
592      * If this fails, well, then I don't know
593      * what I can do more for the moment...
594      */
595     (void)i2c_bus_free_check( MAXBUSFREERETRIES );
596 }   /* i2c_finalise */                         
597
598
599 static struct file_operations i2c_fops = 
600 {
601     .owner    = THIS_MODULE
602 ,   .ioctl    = i2c_ioctl
603 ,   .open     = i2c_open
604 ,   .release  = i2c_release
605 };
606
607
608 /***********************************************************************/
609 /************* EXTERNAL FUNCTION DEFINITION SECTION ********************/
610 /***********************************************************************/
611
612 /*#---------------------------------------------------------------------------
613  *#
614  *# FUNCTION NAME: i2c_init
615  *#
616  *# DESCRIPTION  : initialises the I2C device driver
617  *#
618  *# PARAMETERS   :
619  *#
620  *#---------------------------------------------------------------------------
621  */
622 int __init i2c_init( void )
623 {
624     static int res = 0;
625     static int first = 1;
626
627     if ( !first ) 
628     {
629         return res;
630     }
631     
632     first = 0;
633
634     /* Setup and enable the Port B I2C interface */
635
636 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
637     /* here, we're using the dedicated I2C pins of FoxBoard */
638     if ( ( res = cris_request_io_interface( if_i2c, "I2C" ) ) ) 
639     {
640         printk( KERN_CRIT "i2c_init: Failed to get IO interface\n" );
641         return res;
642     }
643
644     *R_PORT_PB_I2C = port_pb_i2c_shadow |= 
645         IO_STATE( R_PORT_PB_I2C, i2c_en,  on ) |
646         IO_FIELD( R_PORT_PB_I2C, i2c_d,   1 )  |
647         IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, 1 )  |
648         IO_STATE( R_PORT_PB_I2C, i2c_oe_, enable );
649
650     port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir0 );
651     port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir1 );
652
653     *R_PORT_PB_DIR = ( port_pb_dir_shadow |=
654               IO_STATE( R_PORT_PB_DIR, dir0, input )  |
655               IO_STATE( R_PORT_PB_DIR, dir1, output ) );
656 #else
657         /* If everything goes fine, res = 0, meaning "if" fails => 
658          * will do the "else" too and as such initialise the clock port...
659          * Clever trick! 
660          */
661         if ( ( res = cris_io_interface_allocate_pins( if_i2c
662                                                     , 'b'
663                                                     , CONFIG_ETRAX_I2C_DATA_PORT
664                                                     , CONFIG_ETRAX_I2C_DATA_PORT 
665                                                     ) 
666              ) 
667            ) 
668         {
669             printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n" );
670             return ( res );
671         }
672         /* Same here...*/ 
673         else if ( ( res = cris_io_interface_allocate_pins( if_i2c
674                                                          , 'b'
675                                                          , CONFIG_ETRAX_I2C_CLK_PORT
676                                                          , CONFIG_ETRAX_I2C_CLK_PORT 
677                                                          ) 
678                   ) 
679                 ) 
680         {
681             cris_io_interface_free_pins( if_i2c
682                                        , 'b'
683                                        , CONFIG_ETRAX_I2C_DATA_PORT
684                                        , CONFIG_ETRAX_I2C_DATA_PORT 
685                                        );
686             printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n" );
687         }
688 #endif
689
690     return ( res );
691 }   /* i2c_init */
692
693
694 /*#---------------------------------------------------------------------------
695  *#
696  *# FUNCTION NAME: i2c_register
697  *#
698  *# DESCRIPTION  : this registers the i2c driver as a character device
699  *#
700  *# PARAMETERS   :
701  *#
702  *#---------------------------------------------------------------------------
703  */
704 static int __init i2c_register( void )
705 {
706     int res;
707 /**GVC**/
708 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
709     dev_t devt;
710     struct cdev *my_i2cdev = NULL;
711 #endif
712 /**END GVC**/
713
714     res = i2c_init();
715     
716     if ( res < 0 )
717     {
718         return res;
719     }
720     
721 /**GVC**/
722 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
723     res = alloc_chrdev_region( &devt, 0, 1, i2c_name );
724     
725     if ( res < 0 )
726     {
727         printk( KERN_DEBUG "I2C: EI2CNOMNUMBR\n" );
728         return ( res );
729     }
730     
731     my_i2cdev = cdev_alloc();
732     my_i2cdev->ops = &i2c_fops;
733     my_i2cdev->owner = THIS_MODULE;
734    
735     /* make device "alive" */ 
736     res = cdev_add( my_i2cdev, devt, 1 );
737     
738     if ( res < 0 )
739     { 
740         printk( KERN_DEBUG "I2C: EI2CDADDFAIL\n" );
741         return ( res );
742     }
743 #else
744 /**END GVC**/
745     res = register_chrdev( I2C_MAJOR, i2c_name, &i2c_fops );
746     
747     if ( res < 0 ) 
748     {
749         printk( KERN_ERR "i2c: couldn't get a major number.\n" );
750         return res;
751     }
752 /**GVC**/
753 #endif
754 /**END GVC**/
755
756     printk( KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n" );
757
758 /**GVC**/
759     printk( KERN_INFO "  ==> Improvements done by Geert Vancompernolle - December 2006\n" );
760
761 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
762     printk( KERN_INFO "I2C Major: %d / I2C Name: %s\n", MAJOR( devt ), i2c_name );
763 #else
764 /**END GVC**/
765     printk( KERN_INFO "I2C Major: %d / I2C Name: %s\n", I2C_MAJOR, i2c_name );
766 /**GVC**/
767 #endif    
768 /**END GVC**/
769     
770     return ( 0 );
771 }   /* i2c_register */
772
773
774 /*#---------------------------------------------------------------------------
775  *#
776  *# FUNCTION NAME: i2c_start
777  *#
778  *# DESCRIPTION  : generate i2c start condition
779  *#
780  *# PARAMETERS   : none
781  *#
782  *# RETURN       : EI2CNOERRORS if OK, EI2CSTRTCOND otherwise
783  *#
784  *#---------------------------------------------------------------------------
785  */
786 int i2c_start( void )
787 {
788     /* Set SCL=1, SDA=1 */
789     i2c_sda_dir_out();
790     i2c_set_sda( SDA_HIGH );
791     i2c_delay( WAITONEUS );
792     i2c_set_scl( SCL_HIGH );
793     i2c_delay( WAITONEUS );
794     
795     /* Set SCL=1, SDA=0 */
796     i2c_set_sda( SDA_LOW );
797     i2c_delay( THDSTA );
798     
799     /* Set SCL=0, SDA=0 */
800     i2c_set_scl( SCL_LOW );
801     /* We can take 1 us less than defined in spec (5 us), since the next action
802      * will be to set the dataline high or low and this action is 1 us
803      * before the clock is put high, so that makes our 5 us.
804      */
805     i2c_delay( TLOW - WAITONEUS );
806     
807     if ( i2c_sda_is_high() || i2c_scl_is_high() )
808     {
809         printk( KERN_DEBUG "I2C: EI2CSTRTCOND\n" );
810         return ( EI2CSTRTCOND );
811     }
812     
813     return ( EI2CNOERRORS );
814 }   /* i2c_start */
815
816
817 /*#---------------------------------------------------------------------------
818  *#
819  *# FUNCTION NAME: i2c_stop
820  *#
821  *# DESCRIPTION  : generate i2c stop condition
822  *#
823  *# PARAMETERS   : none
824  *#
825  *# RETURN       : none
826  *#
827  *#---------------------------------------------------------------------------
828  */
829 int i2c_stop( void )
830 {
831     i2c_sda_dir_out();
832
833     /* Set SCL=0, SDA=0 */
834     /* Don't change order, otherwise you might generate a start condition! */
835     i2c_set_scl( SCL_LOW );
836     i2c_delay( WAITONEUS );
837     i2c_set_sda( SDA_LOW );
838     i2c_delay( WAITONEUS );
839     
840     /* Set SCL=1, SDA=0 */
841     i2c_set_scl( SCL_HIGH );
842     i2c_delay( TSUSTO );
843     
844     /* Set SCL=1, SDA=1 */
845     i2c_set_sda( SDA_HIGH );
846     i2c_delay( TBUF );
847
848     i2c_sda_dir_in();
849     
850     if ( !i2c_sda_is_high() || !i2c_scl_is_high() )
851     {
852         printk( KERN_DEBUG "I2C: EI2CSTOPCOND\n" );
853         return ( EI2CSTOPCOND );
854     }
855     
856     return ( EI2CNOERRORS );
857 }   /* i2c_stop */
858
859
860 /*#---------------------------------------------------------------------------
861  *#
862  *# FUNCTION NAME: i2c_outbyte
863  *#
864  *# DESCRIPTION  : write a byte to the i2c interface
865  *#
866  *# PARAMETERS   : x: byte to be sent on the I2C bus
867  *#
868  *# RETURN       : none
869  *#
870  *#---------------------------------------------------------------------------
871  */
872 int i2c_outbyte( unsigned char x )
873 {
874     int i;
875
876     i2c_sda_dir_out();
877
878     for ( i = 0; i < 8; i++ ) 
879     {
880         if ( x & 0x80 ) 
881         {
882             i2c_set_sda( SDA_HIGH );
883         } 
884         else 
885         {
886             i2c_set_sda( SDA_LOW );
887         }
888         
889         i2c_delay( TSUDAT );
890         i2c_set_scl( SCL_HIGH );
891         i2c_delay( THIGH );
892         i2c_set_scl( SCL_LOW );
893         i2c_delay( TSUDAT );
894         i2c_set_sda( SDA_LOW );
895         /* There should be only 5 us between falling edge and new rising
896          * edge of clock pulse.
897          * Since we spend already 1 us since clock edge was low, there are
898          * only ( TLOW - TSUDAT ) us left.
899          * Next to this, since the data line will be set up 1 us before the
900          * clock line is set up, we can reduce the delay with another us.
901          */
902         i2c_delay( TLOW - TSUDAT - WAITONEUS );
903         x <<= 1;
904     }
905     
906     /* enable input */
907     i2c_sda_dir_in();
908     
909     if ( !i2c_getack() )
910     {
911         printk( KERN_DEBUG "I2C: EI2CNOACKNLD\n" );
912         return( EI2CNOACKNLD );
913     }
914     
915     return ( EI2CNOERRORS );
916 }   /* i2c_outbyte */
917
918
919 /*#---------------------------------------------------------------------------
920  *#
921  *# FUNCTION NAME: i2c_inbyte
922  *#
923  *# DESCRIPTION  : read a byte from the i2c interface
924  *#
925  *# PARAMETERS   : none
926  *#
927  *# RETURN       : returns the byte read from the I2C device
928  *#
929  *#---------------------------------------------------------------------------
930  */
931 unsigned char i2c_inbyte( void )
932 {
933     unsigned char aBitByte = 0;
934     unsigned char Mask     = 0x80;    /* !!! ATTENTION: do NOT use 'char', otherwise shifting is wrong!!! */
935                                       /* Must be UNSIGNED, not SIGNED! */
936
937
938     /* Switch off I2C to get bit */
939     i2c_disable();
940     i2c_sda_dir_in();
941
942     while ( Mask != 0 )
943     {
944         i2c_set_scl( SCL_HIGH );
945         i2c_delay( THIGH );
946
947         if ( i2c_sda_is_high() )
948         {
949             aBitByte |= Mask;
950         }
951
952         i2c_set_scl( SCL_LOW );
953
954         Mask >>= 1;
955
956         i2c_delay( TLOW );
957     }
958
959     /*
960      * we leave the clock low, getbyte is usually followed
961      * by sendack/nack, they assume the clock to be low
962      */
963     return ( aBitByte );
964 }   /* i2c_inbyte */
965
966
967 /*#---------------------------------------------------------------------------
968  *#
969  *# FUNCTION NAME: i2c_getack
970  *#
971  *# DESCRIPTION  : checks if ack was received from ic2
972  *#
973  *# PARAMETERS   : none
974  *#
975  *# RETURN       : returns the ack state of the I2C device
976  *#
977  *#---------------------------------------------------------------------------
978  */
979 int i2c_getack( void )
980 {
981     int ack = 1;
982
983     /* generate ACK clock pulse */
984     i2c_set_scl( SCL_HIGH );
985     
986     /* switch off I2C */
987     i2c_disable();
988
989     /* now wait for ack */
990     i2c_delay( THIGH );
991     /* check for ack: if SDA is high, then NACK, else ACK */
992     if ( i2c_sda_is_high() )
993     {
994         ack = 0;
995     }
996     else
997     {
998         ack = 1;
999     }
1000     
1001     /* end clock pulse */
1002     i2c_enable();
1003     i2c_set_scl( SCL_LOW );
1004     i2c_sda_dir_out();
1005     i2c_set_sda( SDA_LOW );
1006
1007     /* Since we "lost" already THDDAT time, we can subtract it here... */
1008     i2c_delay( TLOW  - THDDAT );
1009     
1010     return ( ack );
1011 }   /* i2c_getack */
1012
1013
1014 /*#---------------------------------------------------------------------------
1015  *#
1016  *# FUNCTION NAME: i2c_sendack
1017  *#
1018  *# DESCRIPTION  : sends ACK on received data
1019  *#
1020  *# PARAMETERS   : none
1021  *#
1022  *# RETURN       : none
1023  *#
1024  *#---------------------------------------------------------------------------
1025  */
1026 void i2c_sendack( void )
1027 {
1028     /* enable output */
1029     /* Clock has been set to TLOW already at end of i2c_inbyte()
1030      * and i2c_outbyte(), so no need to do it again.
1031      */
1032     i2c_sda_dir_out();
1033     /* set ack pulse low */
1034     i2c_set_sda( SDA_LOW );
1035     /* generate clock pulse */
1036     i2c_delay( TSUDAT );
1037     i2c_set_scl( SCL_HIGH );
1038     i2c_delay( THIGH );
1039     i2c_set_scl( SCL_LOW );
1040     i2c_delay( THDDAT );
1041     /* reset data out */
1042     i2c_set_sda( SDA_HIGH );
1043     /* Subtract time spend already when waited to put SDA high */
1044     i2c_delay( TLOW - THDDAT );
1045
1046     /* release the SDA line */
1047     i2c_sda_dir_in();
1048 }   /* i2c_sendack */
1049
1050
1051 /*#---------------------------------------------------------------------------
1052  *#
1053  *# FUNCTION NAME: i2c_sendnack
1054  *#
1055  *# DESCRIPTION  : sends NACK on received data
1056  *#
1057  *# PARAMETERS   : none
1058  *#
1059  *# RETURN       : none
1060  *#
1061  *#---------------------------------------------------------------------------
1062  */
1063 void i2c_sendnack( void )
1064 {
1065     /* make sure the SDA line is set high prior to activation of the output.
1066      * this way, you avoid an unnecessary peak to ground when a NACK has to
1067      * be created.
1068      */
1069     /* set data high */
1070     i2c_set_sda( SDA_HIGH );
1071     /* enable output */
1072     i2c_sda_dir_out();
1073
1074     /* generate clock pulse */
1075     i2c_delay( TSUDAT );
1076     i2c_set_scl( SCL_HIGH );
1077     i2c_delay( THIGH );
1078     i2c_set_scl( SCL_LOW );
1079     i2c_delay( TSUDAT );
1080     i2c_set_sda( SDA_LOW );
1081     i2c_delay( TLOW - TSUDAT );
1082     
1083     /* There's no need to change the direction of SDA to "in" again,
1084      * since a NACK is always followed by a stop condition.
1085      * A STOP condition will put the direction of SDA back to "out"
1086      * resulting in a useless SDA "dip" on the line...
1087      */
1088     /* i2c_sda_dir_in(); */
1089 }   /* i2c_sendnack */
1090
1091
1092 /*#---------------------------------------------------------------------------
1093  *#
1094  *# FUNCTION NAME: i2c_writereg
1095  *#
1096  *# DESCRIPTION  : writes a value to a register of an I2C device
1097  *#
1098  *# PARAMETERS   : theSlave = slave address of the I2C device
1099  *#                theReg   = register of the I2C device that needs to be written
1100  *#                theValue = value to be written to the register
1101  *#
1102  *# RETURN       : returns OR-ed result of the write action:
1103  *#                  0 = Ok
1104  *#                  1 = Slave_NoAck
1105  *#                  2 = Reg_NoAck
1106  *#                  4 = Val_NoAck
1107  *#
1108  *#---------------------------------------------------------------------------
1109  */
1110 int i2c_writereg( unsigned char theSlave
1111                 , unsigned char theReg
1112                 , unsigned char theValue 
1113                 )
1114 {
1115     int error, cntr = 3;
1116     unsigned long flags;
1117
1118     spin_lock( &i2c_lock );
1119
1120     do 
1121     {
1122         error = 0;
1123         /* we don't like to be interrupted */
1124         local_irq_save( flags );
1125
1126         i2c_start();
1127         /* send slave address */
1128         if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
1129         {
1130             error = 1;
1131         }
1132             
1133         /* now select register */
1134         if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
1135         {
1136             error |= 2;
1137         }
1138         
1139         /* send register register data */
1140         if ( EI2CNOACKNLD == i2c_outbyte( theValue ) )
1141         {
1142             error |= 4;
1143         }
1144              
1145         /* end byte stream */
1146         i2c_stop();
1147         /* enable interrupt again */
1148         local_irq_restore( flags );
1149         
1150     } while ( error && cntr-- );
1151
1152     i2c_delay( TLOW );
1153
1154     spin_unlock( &i2c_lock );
1155
1156     return ( -error );
1157 }   /* i2c_writereg */
1158
1159
1160 /*#---------------------------------------------------------------------------
1161  *#
1162  *# FUNCTION NAME: i2c_readreg
1163  *#
1164  *# DESCRIPTION  : reads the value from a certain register of an I2C device.
1165  *#                Function first writes the register that it wants to read
1166  *#                later on.
1167  *#  
1168  *# PARAMETERS   : theSlave = slave address of the I2C device
1169  *#                theReg   = register of the I2C device that needs to be written
1170  *#
1171  *# RETURN       : returns OR-ed result of the write action:
1172  *#                  0 = Ok
1173  *#                  1 = Slave_NoAck
1174  *#                  2 = Reg_NoAck
1175  *#                  4 = Val_NoAck
1176  *#
1177  *#---------------------------------------------------------------------------
1178  */
1179 unsigned char i2c_readreg( unsigned char theSlave
1180                          , unsigned char theReg 
1181                          )
1182 {
1183     unsigned char b = 0;
1184     int error, cntr = 3;
1185     unsigned long flags;
1186
1187     spin_lock( &i2c_lock );
1188
1189     do 
1190     {
1191         error = 0;
1192         
1193         /* we don't like to be interrupted */
1194         local_irq_save( flags );
1195         
1196         /* generate start condition */
1197         i2c_start();
1198     
1199         /* send slave address */
1200         if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
1201         {
1202             error = 1;
1203         }
1204
1205         /* now select register */
1206         i2c_sda_dir_out();
1207
1208         if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
1209         {
1210             error |= 2;
1211         }            
1212
1213         /* repeat start condition */
1214         i2c_delay( TLOW );
1215         i2c_start();
1216         
1217         /* send slave address */
1218         if ( EI2CNOACKNLD == i2c_outbyte( theSlave | 0x01 ) )
1219         {
1220             error |= 1;
1221         }
1222             
1223         /* fetch register */
1224         b = i2c_inbyte();
1225         /*
1226          * last received byte needs to be nacked
1227          * instead of acked
1228          */
1229         i2c_sendnack();
1230         
1231         /* end sequence */
1232         i2c_stop();
1233         
1234         /* enable interrupt again */
1235         local_irq_restore( flags );
1236         
1237     } while ( error && cntr-- );
1238
1239     spin_unlock( &i2c_lock );
1240
1241     return ( b );
1242 }   /* i2c_readreg */
1243
1244
1245 /*#---------------------------------------------------------------------------
1246  *#
1247  *# FUNCTION NAME: i2c_read
1248  *#
1249  *# DESCRIPTION  :
1250  *#  
1251  *# PARAMETERS   :
1252  *#
1253  *# RETURN       :
1254  *#
1255  *#---------------------------------------------------------------------------
1256  */
1257 int i2c_read( unsigned char slave, unsigned char* rbuf, unsigned char rlen )
1258 {
1259     return ( i2c_command( slave, NULL, 0, rbuf, rlen ) );
1260 }   /* i2c_read */
1261
1262
1263 /*#---------------------------------------------------------------------------
1264  *#
1265  *# FUNCTION NAME: i2c_write
1266  *#
1267  *# DESCRIPTION  :
1268  *#  
1269  *# PARAMETERS   :
1270  *#
1271  *# RETURN       :
1272  *#
1273  *#---------------------------------------------------------------------------
1274  */
1275 int i2c_write( unsigned char slave, unsigned char* wbuf, unsigned char wlen )
1276 {
1277     return ( i2c_command( slave, wbuf, wlen, NULL, 0 ) );
1278 }   /* i2c_write */
1279
1280
1281 /*#---------------------------------------------------------------------------
1282  *#
1283  *# FUNCTION NAME: i2c_writeread
1284  *#
1285  *# DESCRIPTION  :
1286  *#  
1287  *# PARAMETERS   :
1288  *#
1289  *# RETURN       :
1290  *#
1291  *#---------------------------------------------------------------------------
1292  */
1293 int i2c_writeread( unsigned char  slave
1294                  , unsigned char* wbuf
1295                  , unsigned char  wlen
1296                  , unsigned char* rbuf
1297                  , unsigned char  rlen
1298                  )
1299 {
1300     return ( i2c_command( slave, wbuf, wlen, rbuf, rlen ) );
1301 }   /* i2c_writeread */
1302
1303
1304 /*#---------------------------------------------------------------------------
1305  *#
1306  *# FUNCTION NAME: module_init
1307  *#
1308  *# DESCRIPTION  : this makes sure that i2c_register is called during boot
1309  *#
1310  *# PARAMETERS   :
1311  *#
1312  *#---------------------------------------------------------------------------
1313  */
1314 module_init( i2c_register );
1315
1316 /****************** END OF FILE i2c.c ********************************/