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