1 /*!***************************************************************************
6 *! ---------------------------------------------------------------------------
8 *! ( C ) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
10 *!***************************************************************************/
12 /******************** INCLUDE FILES SECTION ****************************/
14 #include <linux/module.h>
17 #ifdef CONFIG_ETRAX_I2C_DYN_ALLOC
18 #include <linux/types.h> /* for dev_t */
19 #include <linux/cdev.h> /* for struct cdev */
22 #include <linux/device.h>
26 #include "i2c_errno.h"
29 #include <asm/delay.h>
30 #include <asm/arch/io_interface_mux.h>
31 #include <asm/uaccess.h>
35 MODULE_DESCRIPTION( "I2C Device Driver - 2.3" );
37 /*!*********************************************************************
38 *!History I2C driver Geert Vancompernolle
39 *!---------------------------------------
42 *! First official version.
45 *! Changes to remove unwanted spikes at ACK/NACK time.
47 *!*********************************************************************/
49 MODULE_LICENSE( "GPL" );
51 /****************** MACRO's **********************/
55 #ifndef CONFIG_ETRAX_I2C_DYN_ALLOC
56 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
60 /* Following are abbreviations taken from Philips I2C standard */
61 /* Values are representing time in us and are rounded to next whole number, if relevant */
62 #define THDSTA 4 /* Hold delay time for (repeated) START condition */
63 #define TLOW 5 /* LOW period of the SCL clock */
64 #define THDDAT 1 /* Hold delay time for DATA: value of 0 is allowed but 1 taken to be sure */
65 #define TSUDAT 1 /* Set-up time for DATA */
66 #define THIGH 4 /* HIGH period of the SCL clock */
67 #define TSUSTA 5 /* Set-up time for a repeated START condition */
68 #define TSUSTO 4 /* Set-up time for STOP condition */
69 #define TBUF 5 /* Bus-free time between STOP and START condition */
71 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
72 #define MAXSCLRETRIES 100
75 #define MAXBUSFREERETRIES 5
77 #define WRITEADDRESS_MASK ( 0xFE )
78 #define READADDRESS_MASK ( 0x01 )
85 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
86 /* Use PB and not PB_I2C */
87 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
88 #define CONFIG_ETRAX_I2C_DATA_PORT 0
90 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
91 #define CONFIG_ETRAX_I2C_CLK_PORT 1
94 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
95 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
99 /* enable or disable output-enable, to select output or input on the i2c bus */
100 #define i2c_sda_dir_out() \
101 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1 )
102 #define i2c_sda_dir_in() \
103 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0 )
105 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
106 #define i2c_scl_dir_out() \
107 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SCLBIT, 1 )
108 #define i2c_scl_dir_in() \
109 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SCLBIT, 0 )
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 )
118 /* read status of SDA bit from the i2c interface */
119 #define i2c_sda_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SDABIT ) ) ) >> SDABIT )
121 /* read status of SCL bit from the i2c interface */
122 #define i2c_scl_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SCLBIT ) ) ) >> SCLBIT )
125 /* enable or disable the i2c interface */
126 #define i2c_enable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_en ) )
127 #define i2c_disable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_en ) )
129 /* enable or disable output-enable, to select output or input on the i2c bus */
130 #define i2c_sda_dir_out() \
131 *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
132 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1 );
133 #define i2c_sda_dir_in() \
134 *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
135 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0 );
137 /* control the i2c clock and data signals */
138 #define i2c_set_scl( x ) \
139 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
140 ~IO_MASK( R_PORT_PB_I2C, i2c_set_scl ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, ( x ) ) ); \
141 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 1, x );
143 #define i2c_set_sda( x ) \
144 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
145 ~IO_MASK( R_PORT_PB_I2C, i2c_d ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_d, ( x ) ) ); \
146 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 0, x );
148 /* read a bit from the i2c interface */
149 #define i2c_sda_is_high() ( *R_PORT_PB_READ & 0x1 )
152 /* use the kernels delay routine */
153 #define i2c_delay( usecs ) udelay( usecs )
156 /****************** TYPEDEF's **********************/
159 /****************** STATIC (file scope) VARIABLES **********************/
160 static DEFINE_SPINLOCK( i2c_lock ); /* Protect directions etc */
161 static const char i2c_name[] = "i2c";
165 /****************** PROTOTYPING SECTION *************************/
166 static int i2c_open( struct inode *inode, struct file *filp );
167 static int i2c_release( struct inode *inode, struct file *filp );
168 static int i2c_command( unsigned char slave
169 , unsigned char* wbuf
171 , unsigned char* rbuf
174 static int i2c_bus_free_check( unsigned char maxretries );
175 static void i2c_finalise( const char* text, unsigned long irqflags );
178 /************************************************************************/
179 /****************** AUXILIARIES *************************/
180 /************************************************************************/
182 /*#---------------------------------------------------------------------------
184 *# FUNCTION NAME: i2c_open
186 *# DESCRIPTION : opens an I2C device
188 *# PARAMETERS : *inode: reference to inode
189 *# *filp : reference to file pointer
191 *#---------------------------------------------------------------------------
193 static int i2c_open( struct inode *inode, struct file *filp )
199 /*#---------------------------------------------------------------------------
201 *# FUNCTION NAME: i2c_release
203 *# DESCRIPTION : Releases the I2C device
205 *# PARAMETERS : *inode: reference to inode
206 *# *filp : reference to file pointer
208 *#---------------------------------------------------------------------------
210 static int i2c_release( struct inode *inode, struct file *filp )
216 /*#---------------------------------------------------------------------------
218 *# FUNCTION NAME: i2c_ioctl
220 *# DESCRIPTION : Main device API: ioctl's to write/read
221 *# to/from i2c registers
223 *# PARAMETERS : *inode: reference to inode
224 *# *filp : reference to file pointer
225 *# cmd : command to be executed during the ioctl call
226 *# arg : pointer to a structure with the data???
228 *# RETURN : result of the ioctl call
230 *#---------------------------------------------------------------------------
232 static int i2c_ioctl( struct inode *inode
238 /* the acme ioctls */
240 int RetVal = EI2CNOERRORS;
242 if ( _IOC_TYPE( cmd ) != ETRAXI2C_IOCTYPE )
247 switch ( _IOC_NR( cmd ) )
250 /* write to an i2c slave */
251 RetVal = i2c_writereg( I2C_ARGSLAVE( arg )
253 , I2C_ARGVALUE( arg )
258 RetVal = i2c_readreg( I2C_ARGSLAVE( arg ), I2C_ARGREG( arg ) );
261 /* New functions added by GVC */
263 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
265 int RetryCntr = MAXRETRIES;
269 RetVal = i2c_command( i2cdata.slave
275 } while ( ( EI2CNOERRORS != RetVal )
279 copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
283 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
285 int RetryCntr = MAXRETRIES;
289 RetVal = i2c_command( i2cdata.slave
295 } while ( ( EI2CNOERRORS != RetVal )
302 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
304 int RetryCntr = MAXRETRIES;
308 RetVal = i2c_command( i2cdata.slave
314 } while ( ( EI2CNOERRORS != RetVal )
318 copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
329 /*#---------------------------------------------------------------------------
331 *# FUNCTION NAME: i2c_command
333 *# DESCRIPTION : general routine to read/write bytes from an I2C device
335 *# 'i2c_command()' sends wlen bytes to the I2c bus and receives
336 *# rlen bytes from the I2c bus.
337 *# The data to be send must be placed in wbuf[ 0 ] upto wbuf[ wlen - 1 ).
338 *# The data to be received is assembled in rbuf[ 0 ] upto rbuf[ rlen - 1 ].
340 *# If no data is to be sent or received, put appropriate buffer parameter
341 *# to "NULL" and appropriate length parameter to "0".
343 *# PARAMETERS : slave = slave address of the I2C device
344 *# wbuf = address of first element of write buffer (wbuf)
345 *# wlen = number of bytes to be written to slave
346 *# rbuf = address of first element of read buffer (rbuf)
347 *# rlen = number of bytes to be read from slave
350 *# EI2CNOERRORS: I2C communication went fine
351 *# EI2CBUSNFREE: I2C bus is not free
352 *# EI2CWADDRESS: I2C write address failed
353 *# EI2CRADDRESS: I2C read address failed
354 *# EI2CSENDDATA: I2C send data failed
355 *# EI2CRECVDATA: I2C receive data failed
356 *# EI2CSTRTCOND: I2C start condition failed
357 *# EI2CRSTACOND: I2C repeated start condition failed
358 *# EI2CSTOPCOND: I2C stop condition failed
359 *# EI2CNOSNDBYT: I2C no bytes to be sent
360 *# EI2CNOSNDBUF: I2C no send buffer defined
361 *# EI2CNORCVBYT: I2C no bytes to be received
362 *# EI2CNORCVBUF: I2C no receive buffer defined
363 *# EI2CNOACKNLD: I2C no acknowledge received
366 *# First, the send part is completed.
367 *# In the send routine, there is no stop generated. This is because maybe
368 *# a repeated start condition must be generated.
369 *# This happens when we want to receive some data from the I2c bus. If not,
370 *# at the end of the general I2c loop the stopcondition is generated.
371 *# If, on the contrary, there are a number of bytes to be received, a new
372 *# startcondition is generated in the 'if' part of the main I2c routine,
373 *# which controls the receiving part.
374 *# Only when the receiving of data is finished, a final stopcondition is
377 *#---------------------------------------------------------------------------
379 static int i2c_command( unsigned char slave
380 , unsigned char* wbuf
382 , unsigned char* rbuf
386 /* Check arguments and report error if relevant... */
387 if ( ( wlen > 0 ) && ( wbuf == NULL ) )
389 printk( KERN_DEBUG "I2C: EI2CNOSNDBUF\n" );
390 return ( EI2CNOSNDBUF );
392 else if ( ( wlen == 0 ) && ( wbuf != NULL ) )
394 printk( KERN_DEBUG "I2C: EI2CNOSNDBYT\n" );
395 return ( EI2CNOSNDBYT );
397 else if ( ( rlen > 0 ) && ( rbuf == NULL ) )
399 printk( KERN_DEBUG "I2C: EI2CNORCVBUF\n" );
400 return ( EI2CNORCVBUF );
402 else if ( ( rlen == 0 ) && ( rbuf != NULL ) )
404 printk( KERN_DEBUG "I2C: EI2CNORCVBYT\n" );
405 return ( EI2CNORCVBYT );
407 else if ( EI2CBUSNFREE == i2c_bus_free_check( MAXBUSFREERETRIES ) )
409 /* There's no need to try more, since we weren't even
410 * able to start the I2C communication.
411 * So, no IRQ flags are stored yet, no changes to any other
412 * stuff like START, STOP, SENDBYTES...
413 * Result, simply write down the error and return the correct error code.
415 printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
416 return ( EI2CBUSNFREE );
420 /* Finally... We made it... */
421 unsigned long irqflags = 0;
423 /* we don't like to be interrupted */
424 local_irq_save( irqflags );
426 /* Check if there are bytes to be send,
427 * or if you immediately want to receive data.
431 /* start I2C communication */
432 if ( EI2CNOERRORS != i2c_start() )
434 return ( i2c_finalise( "I2C: EI2CSTRTCOND\n", irqflags )
439 /* send slave address: xxxxxxx0B (last bit must be zero) */
440 if ( EI2CNOERRORS != i2c_outbyte( slave & WRITEADDRESS_MASK ) )
442 return ( i2c_finalise( "I2C: EI2CWADDRESS\n", irqflags )
449 /* send register data */
450 if ( EI2CNOERRORS != i2c_outbyte( *wbuf ) && wlen )
452 return ( i2c_finalise( "I2C: EI2CSENDDATA\n", irqflags )
464 * Receiving data from I2c_bus
465 * If there are bytes to be received, a new start condition is
466 * generated => Repeated Startcondition.
467 * A final stopcondition is generated at the end of the main I2c
473 * Generate start condition if wlen == 0
474 * or repeated start condition if wlen != 0...
476 if ( EI2CNOERRORS != i2c_start() )
478 return ( i2c_finalise( ( ( 0 < wlen )
479 ? "I2C: EI2CRSTACOND\n"
480 : "I2C: EI2CSTRTCOND\n"
484 , ( ( 0 < wlen ) ? EI2CRSTACOND : EI2CSTRTCOND )
488 /* Send ReadAddress: xxxxxxx1B (last bit must be one) */
489 if ( EI2CNOERRORS != i2c_outbyte( slave | READADDRESS_MASK ) )
491 return ( i2c_finalise( "I2C: EI2CRADDRESS\n", irqflags )
499 *rbuf = i2c_inbyte();
502 /* last received byte needs to be NACK-ed instead of ACK-ed */
514 /* Generate final stop condition */
515 if ( EI2CNOERRORS != i2c_stop() )
517 return ( i2c_finalise( "I2C CMD: EI2CSTOPCOND\n", irqflags )
522 /* enable interrupt again */
523 local_irq_restore( irqflags );
526 return ( EI2CNOERRORS );
530 /*#---------------------------------------------------------------------------
532 *# FUNCTION NAME: i2c_bus_free_check
534 *# DESCRIPTION : checks if the I2C bus is free before starting
535 *# an I2C communication
537 *# PARAMETERS : maxretries, the number of times we will try to release
540 *# RETURN : I2cStatus_I2cBusNotFreeError in case the bus is not free,
541 *# I2cStatus_I2cNoError otherwise
543 *#---------------------------------------------------------------------------
545 static int i2c_bus_free_check( unsigned char maxretries )
547 i2c_sda_dir_in(); /* Release SDA line */
548 i2c_set_scl( SCL_HIGH ); /* put SCL line high */
550 i2c_delay( WAITONEUS );
552 while ( ( !i2c_sda_is_high() || !i2c_scl_is_high() )
556 /* Try to release I2C bus by generating STOP conditions */
560 if ( 0 == maxretries )
562 printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
563 return ( EI2CBUSNFREE );
567 return ( EI2CNOERRORS );
569 } /* i2c_bus_free_check */
572 static void i2c_finalise( const char* errortxt
573 , unsigned long irqflags
576 printk( KERN_DEBUG "%s", errortxt );
577 local_irq_restore( irqflags );
578 /* The least we can do when things go terribly wrong,
579 * is to try to release the bus.
580 * If this fails, well, then I don't know
581 * what I can do more for the moment...
583 (void)i2c_bus_free_check( MAXBUSFREERETRIES );
587 static struct file_operations i2c_fops =
592 , .release = i2c_release
596 /***********************************************************************/
597 /************* EXTERNAL FUNCTION DEFINITION SECTION ********************/
598 /***********************************************************************/
600 /*#---------------------------------------------------------------------------
602 *# FUNCTION NAME: i2c_init
604 *# DESCRIPTION : initialises the I2C device driver
608 *#---------------------------------------------------------------------------
610 int __init i2c_init( void )
613 static int first = 1;
622 /* Setup and enable the Port B I2C interface */
624 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
625 /* here, we're using the dedicated I2C pins of FoxBoard */
626 if ( ( res = cris_request_io_interface( if_i2c, "I2C" ) ) )
628 printk( KERN_CRIT "i2c_init: Failed to get IO interface\n" );
632 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
633 IO_STATE( R_PORT_PB_I2C, i2c_en, on ) |
634 IO_FIELD( R_PORT_PB_I2C, i2c_d, 1 ) |
635 IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, 1 ) |
636 IO_STATE( R_PORT_PB_I2C, i2c_oe_, enable );
638 port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir0 );
639 port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir1 );
641 *R_PORT_PB_DIR = ( port_pb_dir_shadow |=
642 IO_STATE( R_PORT_PB_DIR, dir0, input ) |
643 IO_STATE( R_PORT_PB_DIR, dir1, output ) );
645 /* If everything goes fine, res = 0, meaning "if" fails =>
646 * will do the "else" too and as such initialise the clock port...
649 if ( ( res = cris_io_interface_allocate_pins( if_i2c
651 , CONFIG_ETRAX_I2C_DATA_PORT
652 , CONFIG_ETRAX_I2C_DATA_PORT
657 printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n" );
661 else if ( ( res = cris_io_interface_allocate_pins( if_i2c
663 , CONFIG_ETRAX_I2C_CLK_PORT
664 , CONFIG_ETRAX_I2C_CLK_PORT
669 cris_io_interface_free_pins( if_i2c
671 , CONFIG_ETRAX_I2C_DATA_PORT
672 , CONFIG_ETRAX_I2C_DATA_PORT
674 printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n" );
682 /*#---------------------------------------------------------------------------
684 *# FUNCTION NAME: i2c_register
686 *# DESCRIPTION : this registers the i2c driver as a character device
690 *#---------------------------------------------------------------------------
693 static struct class *i2c_class;
695 static int __init i2c_register( void )
698 #ifdef CONFIG_ETRAX_I2C_DYN_ALLOC
700 struct cdev *my_i2cdev = NULL;
710 #ifdef CONFIG_ETRAX_I2C_DYN_ALLOC
711 res = alloc_chrdev_region( &devt, 0, 1, i2c_name );
715 printk( KERN_DEBUG "I2C: EI2CNOMNUMBR\n" );
719 my_i2cdev = cdev_alloc();
720 my_i2cdev->ops = &i2c_fops;
721 my_i2cdev->owner = THIS_MODULE;
723 /* make device "alive" */
724 res = cdev_add( my_i2cdev, devt, 1 );
728 printk( KERN_DEBUG "I2C: EI2CDADDFAIL\n" );
732 int i2c_major = MAJOR( devt );
734 res = register_chrdev( I2C_MAJOR, i2c_name, &i2c_fops );
738 printk( KERN_ERR "i2c: couldn't get a major number.\n" );
742 int i2c_major = I2C_MAJOR;
745 printk( KERN_INFO "I2C: driver v2.3, (c) 1999-2004 Axis Communications AB\n" );
746 printk( KERN_INFO "I2C: Improvements by Geert Vancompernolle, Positive Going, BK srl\n" );
748 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
749 printk( KERN_INFO "I2C: with master/slave delay patch\n" );
752 i2c_class = class_create (THIS_MODULE, "i2c_etrax");
753 device_create (i2c_class, NULL,
754 MKDEV(i2c_major,0), NULL, i2c_name);
760 /*#---------------------------------------------------------------------------
762 *# FUNCTION NAME: i2c_start
764 *# DESCRIPTION : generate i2c start condition
768 *# RETURN : EI2CNOERRORS if OK, EI2CSTRTCOND otherwise
770 *#---------------------------------------------------------------------------
772 int i2c_start( void )
774 /* Set SCL=1, SDA=1 */
776 i2c_set_sda( SDA_HIGH );
777 i2c_delay( WAITONEUS );
778 i2c_set_scl( SCL_HIGH );
779 i2c_delay( WAITONEUS );
781 /* Set SCL=1, SDA=0 */
782 i2c_set_sda( SDA_LOW );
785 /* Set SCL=0, SDA=0 */
786 i2c_set_scl( SCL_LOW );
787 /* We can take 1 us less than defined in spec (5 us), since the next action
788 * will be to set the dataline high or low and this action is 1 us
789 * before the clock is put high, so that makes our 5 us.
791 i2c_delay( TLOW - WAITONEUS );
793 if ( i2c_sda_is_high() || i2c_scl_is_high() )
795 printk( KERN_DEBUG "I2C: EI2CSTRTCOND\n" );
796 return ( EI2CSTRTCOND );
799 return ( EI2CNOERRORS );
803 /*#---------------------------------------------------------------------------
805 *# FUNCTION NAME: i2c_stop
807 *# DESCRIPTION : generate i2c stop condition
813 *#---------------------------------------------------------------------------
817 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
822 /* Set SCL=0, SDA=0 */
823 /* Don't change order, otherwise you might generate a start condition! */
824 i2c_set_scl( SCL_LOW );
825 i2c_delay( WAITONEUS );
826 i2c_set_sda( SDA_LOW );
827 i2c_delay( WAITONEUS );
829 /* Set SCL=1, SDA=0 */
831 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
832 i2c_set_scl( SCL_HIGH );
836 if( i2c_scl_is_high() )
843 i2c_set_scl( SCL_HIGH );
847 /* Set SCL=1, SDA=1 */
848 i2c_set_sda( SDA_HIGH );
853 if ( !i2c_sda_is_high() || !i2c_scl_is_high() )
855 return ( EI2CSTOPCOND );
858 return ( EI2CNOERRORS );
862 /*#---------------------------------------------------------------------------
864 *# FUNCTION NAME: i2c_outbyte
866 *# DESCRIPTION : write a byte to the i2c interface
868 *# PARAMETERS : x: byte to be sent on the I2C bus
872 *#---------------------------------------------------------------------------
874 int i2c_outbyte( unsigned char x )
880 for ( i = 0; i < 8; i++ )
884 i2c_set_sda( SDA_HIGH );
888 i2c_set_sda( SDA_LOW );
892 i2c_set_scl( SCL_HIGH );
894 i2c_set_scl( SCL_LOW );
896 i2c_set_sda( SDA_LOW );
897 /* There should be only 5 us between falling edge and new rising
898 * edge of clock pulse.
899 * Since we spend already 1 us since clock edge was low, there are
900 * only ( TLOW - TSUDAT ) us left.
901 * Next to this, since the data line will be set up 1 us before the
902 * clock line is set up, we can reduce the delay with another us.
904 i2c_delay( TLOW - TSUDAT - WAITONEUS );
913 return( EI2CNOACKNLD );
916 return ( EI2CNOERRORS );
920 /*#---------------------------------------------------------------------------
922 *# FUNCTION NAME: i2c_inbyte
924 *# DESCRIPTION : read a byte from the i2c interface
928 *# RETURN : returns the byte read from the I2C device
930 *#---------------------------------------------------------------------------
932 unsigned char i2c_inbyte( void )
934 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
937 unsigned char aBitByte = 0;
938 unsigned char Mask = 0x80; /* !!! ATTENTION: do NOT use 'char', otherwise shifting is wrong!!! */
939 /* Must be UNSIGNED, not SIGNED! */
942 /* Switch off I2C to get bit */
948 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
952 if( i2c_scl_is_high() )
957 i2c_set_scl( SCL_HIGH );
960 i2c_set_scl( SCL_HIGH );
964 if ( i2c_sda_is_high() )
969 i2c_set_scl( SCL_LOW );
977 * we leave the clock low, getbyte is usually followed
978 * by sendack/nack, they assume the clock to be low
984 /*#---------------------------------------------------------------------------
986 *# FUNCTION NAME: i2c_getack
988 *# DESCRIPTION : checks if ack was received from ic2
992 *# RETURN : returns the ack state of the I2C device
994 *#---------------------------------------------------------------------------
996 int i2c_getack( void )
999 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1000 int n=MAXSCLRETRIES;
1003 /* generate ACK clock pulse */
1004 i2c_set_scl( SCL_HIGH );
1006 /* switch off I2C */
1009 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1011 i2c_set_scl( SCL_LOW );
1013 /* now wait for ack */
1016 /* set clock as input */
1019 /* wait for clock to rise (n=MAXSCLRETRIES) */
1022 if( i2c_scl_is_high() )
1027 i2c_set_scl( SCL_HIGH );
1033 /* now wait for ack */
1037 /* check for ack: if SDA is high, then NACK, else ACK */
1038 if ( i2c_sda_is_high() )
1047 /* end clock pulse */
1049 i2c_set_scl( SCL_LOW );
1051 i2c_set_sda( SDA_LOW );
1053 /* Since we "lost" already THDDAT time, we can subtract it here... */
1054 i2c_delay( TLOW - THDDAT );
1060 /*#---------------------------------------------------------------------------
1062 *# FUNCTION NAME: i2c_sendack
1064 *# DESCRIPTION : sends ACK on received data
1066 *# PARAMETERS : none
1070 *#---------------------------------------------------------------------------
1072 void i2c_sendack( void )
1074 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1075 int n=MAXSCLRETRIES;
1079 /* Clock has been set to TLOW already at end of i2c_inbyte()
1080 * and i2c_outbyte(), so no need to do it again.
1083 /* set ack pulse low */
1084 i2c_set_sda( SDA_LOW );
1085 /* generate clock pulse */
1086 i2c_delay( TSUDAT );
1088 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1090 /* wait for clock to rise (n=MAXSCLRETRIES) */
1093 if( i2c_scl_is_high() )
1098 i2c_set_scl( SCL_HIGH );
1102 i2c_set_scl( SCL_HIGH );
1106 i2c_set_scl( SCL_LOW );
1107 i2c_delay( THDDAT );
1108 /* reset data out */
1109 i2c_set_sda( SDA_HIGH );
1110 /* Subtract time spend already when waited to put SDA high */
1111 i2c_delay( TLOW - THDDAT );
1113 /* release the SDA line */
1118 /*#---------------------------------------------------------------------------
1120 *# FUNCTION NAME: i2c_sendnack
1122 *# DESCRIPTION : sends NACK on received data
1124 *# PARAMETERS : none
1128 *#---------------------------------------------------------------------------
1130 void i2c_sendnack( void )
1132 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1133 int n=MAXSCLRETRIES;
1136 /* make sure the SDA line is set high prior to activation of the output.
1137 * this way, you avoid an unnecessary peak to ground when a NACK has to
1141 i2c_set_sda( SDA_HIGH );
1145 /* generate clock pulse */
1146 i2c_delay( TSUDAT );
1148 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1150 /* wait for clock to rise (n=MAXSCLRETRIES) */
1153 if( i2c_scl_is_high() )
1158 i2c_set_scl( SCL_HIGH );
1162 i2c_set_scl( SCL_HIGH );
1166 i2c_set_scl( SCL_LOW );
1167 i2c_delay( TSUDAT );
1168 i2c_set_sda( SDA_LOW );
1169 i2c_delay( TLOW - TSUDAT );
1171 /* There's no need to change the direction of SDA to "in" again,
1172 * since a NACK is always followed by a stop condition.
1173 * A STOP condition will put the direction of SDA back to "out"
1174 * resulting in a useless SDA "dip" on the line...
1176 /* i2c_sda_dir_in(); */
1177 } /* i2c_sendnack */
1180 /*#---------------------------------------------------------------------------
1182 *# FUNCTION NAME: i2c_writereg
1184 *# DESCRIPTION : writes a value to a register of an I2C device
1186 *# PARAMETERS : theSlave = slave address of the I2C device
1187 *# theReg = register of the I2C device that needs to be written
1188 *# theValue = value to be written to the register
1190 *# RETURN : returns OR-ed result of the write action:
1196 *#---------------------------------------------------------------------------
1198 int i2c_writereg( unsigned char theSlave
1199 , unsigned char theReg
1200 , unsigned char theValue
1203 int error, cntr = 3;
1204 unsigned long flags;
1206 spin_lock( &i2c_lock );
1211 /* we don't like to be interrupted */
1212 local_irq_save( flags );
1215 /* send slave address */
1216 if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
1221 /* now select register */
1222 if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
1227 /* send register register data */
1228 if ( EI2CNOACKNLD == i2c_outbyte( theValue ) )
1233 /* end byte stream */
1235 /* enable interrupt again */
1236 local_irq_restore( flags );
1238 } while ( error && cntr-- );
1242 spin_unlock( &i2c_lock );
1245 } /* i2c_writereg */
1248 /*#---------------------------------------------------------------------------
1250 *# FUNCTION NAME: i2c_readreg
1252 *# DESCRIPTION : reads the value from a certain register of an I2C device.
1253 *# Function first writes the register that it wants to read
1256 *# PARAMETERS : theSlave = slave address of the I2C device
1257 *# theReg = register of the I2C device that needs to be written
1259 *# RETURN : returns OR-ed result of the write action:
1265 *#---------------------------------------------------------------------------
1267 unsigned char i2c_readreg( unsigned char theSlave
1268 , unsigned char theReg
1271 unsigned char b = 0;
1272 int error, cntr = 3;
1273 unsigned long flags;
1275 spin_lock( &i2c_lock );
1281 /* we don't like to be interrupted */
1282 local_irq_save( flags );
1284 /* generate start condition */
1287 /* send slave address */
1288 if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
1293 /* now select register */
1296 if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
1301 /* repeat start condition */
1305 /* send slave address */
1306 if ( EI2CNOACKNLD == i2c_outbyte( theSlave | 0x01 ) )
1311 /* fetch register */
1314 * last received byte needs to be nacked
1322 /* enable interrupt again */
1323 local_irq_restore( flags );
1325 } while ( error && cntr-- );
1327 spin_unlock( &i2c_lock );
1333 /*#---------------------------------------------------------------------------
1335 *# FUNCTION NAME: i2c_read
1343 *#---------------------------------------------------------------------------
1345 int i2c_read( unsigned char slave, unsigned char* rbuf, unsigned char rlen )
1347 return ( i2c_command( slave, NULL, 0, rbuf, rlen ) );
1351 /*#---------------------------------------------------------------------------
1353 *# FUNCTION NAME: i2c_write
1361 *#---------------------------------------------------------------------------
1363 int i2c_write( unsigned char slave, unsigned char* wbuf, unsigned char wlen )
1365 return ( i2c_command( slave, wbuf, wlen, NULL, 0 ) );
1369 /*#---------------------------------------------------------------------------
1371 *# FUNCTION NAME: i2c_writeread
1379 *#---------------------------------------------------------------------------
1381 int i2c_writeread( unsigned char slave
1382 , unsigned char* wbuf
1383 , unsigned char wlen
1384 , unsigned char* rbuf
1385 , unsigned char rlen
1388 return ( i2c_command( slave, wbuf, wlen, rbuf, rlen ) );
1389 } /* i2c_writeread */
1392 /*#---------------------------------------------------------------------------
1394 *# FUNCTION NAME: module_init
1396 *# DESCRIPTION : this makes sure that i2c_register is called during boot
1400 *#---------------------------------------------------------------------------
1402 module_init( i2c_register );
1404 /****************** END OF FILE i2c.c ********************************/