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