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