1 /*!***************************************************************************
6 *! ---------------------------------------------------------------------------
8 *! ( C ) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
10 *!***************************************************************************/
12 #define DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
13 //#undef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
15 /******************** INCLUDE FILES SECTION ****************************/
17 #include <linux/module.h>
21 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
22 #include <linux/types.h> /* for dev_t */
23 #include <linux/cdev.h> /* for struct cdev */
30 #include "i2c_errno.h"
34 #include <asm/delay.h>
35 #include <asm/arch/io_interface_mux.h>
36 #include <asm/uaccess.h>
40 MODULE_DESCRIPTION( "I2C Device Driver - 1.1" );
42 /*!*********************************************************************
43 *!History I2C driver Geert Vancompernolle
44 *!---------------------------------------
47 *! First official version.
50 *! Changes to remove unwanted spikes at ACK/NACK time.
52 *!*********************************************************************/
54 MODULE_LICENSE( "GPL" );
56 /****************** MACRO's **********************/
61 #ifndef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
63 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
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 */
81 #define MAXBUSFREERETRIES 5
83 #define WRITEADDRESS_MASK ( 0xFE )
84 #define READADDRESS_MASK ( 0x01 )
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
97 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
98 #define CONFIG_ETRAX_I2C_CLK_PORT 1
101 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
102 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
104 #define i2c_disable()
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 )
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 )
122 /* read status of SCL bit from the i2c interface */
123 #define i2c_scl_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SCLBIT ) ) ) >> SCLBIT )
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 ) )
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 );
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 );
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 );
150 /* read a bit from the i2c interface */
151 #define i2c_sda_is_high() ( *R_PORT_PB_READ & 0x1 )
154 /* use the kernels delay routine */
155 #define i2c_delay( usecs ) udelay( usecs )
158 /****************** TYPEDEF's **********************/
161 /****************** STATIC (file scope) VARIABLES **********************/
162 static DEFINE_SPINLOCK( i2c_lock
); /* Protect directions etc */
164 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
165 static const char i2c_name
[] = "i2cgvc";
167 static const char i2c_name
[] = "i2c";
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
);
176 static int i2c_command( unsigned char slave
177 , unsigned char* wbuf
179 , unsigned char* rbuf
182 static int i2c_bus_free_check( unsigned char maxretries
);
183 static void i2c_finalise( const char* text
, unsigned long irqflags
);
187 /************************************************************************/
188 /****************** AUXILIARIES *************************/
189 /************************************************************************/
191 /*#---------------------------------------------------------------------------
193 *# FUNCTION NAME: i2c_open
195 *# DESCRIPTION : opens an I2C device
197 *# PARAMETERS : *inode: reference to inode
198 *# *filp : reference to file pointer
200 *#---------------------------------------------------------------------------
202 static int i2c_open( struct inode
*inode
, struct file
*filp
)
208 /*#---------------------------------------------------------------------------
210 *# FUNCTION NAME: i2c_release
212 *# DESCRIPTION : Releases the I2C device
214 *# PARAMETERS : *inode: reference to inode
215 *# *filp : reference to file pointer
217 *#---------------------------------------------------------------------------
219 static int i2c_release( struct inode
*inode
, struct file
*filp
)
225 /*#---------------------------------------------------------------------------
227 *# FUNCTION NAME: i2c_ioctl
229 *# DESCRIPTION : Main device API: ioctl's to write/read
230 *# to/from i2c registers
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???
237 *# RETURN : result of the ioctl call
239 *#---------------------------------------------------------------------------
241 static int i2c_ioctl( struct inode
*inode
247 /* the acme ioctls */
249 int RetVal
= EI2CNOERRORS
;
251 if ( _IOC_TYPE( cmd
) != ETRAXI2C_IOCTYPE
)
256 switch ( _IOC_NR( cmd
) )
259 /* write to an i2c slave */
260 RetVal
= i2c_writereg( I2C_ARGSLAVE( arg
)
262 , I2C_ARGVALUE( arg
)
267 RetVal
= i2c_readreg( I2C_ARGSLAVE( arg
), I2C_ARGREG( arg
) );
271 /* New functions added by GVC */
273 copy_from_user( (char*)&i2cdata
, (char*)arg
, sizeof( I2C_DATA
) );
275 int RetryCntr
= MAXRETRIES
;
279 RetVal
= i2c_command( i2cdata
.slave
285 } while ( ( EI2CNOERRORS
!= RetVal
)
289 copy_to_user( (char*)arg
, (char*)&i2cdata
, sizeof( I2C_DATA
) );
293 copy_from_user( (char*)&i2cdata
, (char*)arg
, sizeof( I2C_DATA
) );
295 int RetryCntr
= MAXRETRIES
;
299 RetVal
= i2c_command( i2cdata
.slave
305 } while ( ( EI2CNOERRORS
!= RetVal
)
312 copy_from_user( (char*)&i2cdata
, (char*)arg
, sizeof( I2C_DATA
) );
314 int RetryCntr
= MAXRETRIES
;
318 RetVal
= i2c_command( i2cdata
.slave
324 } while ( ( EI2CNOERRORS
!= RetVal
)
328 copy_to_user( (char*)arg
, (char*)&i2cdata
, sizeof( I2C_DATA
) );
340 /*#---------------------------------------------------------------------------
342 *# FUNCTION NAME: i2c_command
344 *# DESCRIPTION : general routine to read/write bytes from an I2C device
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 ].
351 *# If no data is to be sent or received, put appropriate buffer parameter
352 *# to "NULL" and appropriate length parameter to "0".
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
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
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
388 *#---------------------------------------------------------------------------
390 static int i2c_command( unsigned char slave
391 , unsigned char* wbuf
393 , unsigned char* rbuf
397 /* Check arguments and report error if relevant... */
398 if ( ( wlen
> 0 ) && ( wbuf
== NULL
) )
400 printk( KERN_DEBUG
"I2C: EI2CNOSNDBUF\n" );
401 return ( EI2CNOSNDBUF
);
403 else if ( ( wlen
== 0 ) && ( wbuf
!= NULL
) )
405 printk( KERN_DEBUG
"I2C: EI2CNOSNDBYT\n" );
406 return ( EI2CNOSNDBYT
);
408 else if ( ( rlen
> 0 ) && ( rbuf
== NULL
) )
410 printk( KERN_DEBUG
"I2C: EI2CNORCVBUF\n" );
411 return ( EI2CNORCVBUF
);
413 else if ( ( rlen
== 0 ) && ( rbuf
!= NULL
) )
415 printk( KERN_DEBUG
"I2C: EI2CNORCVBYT\n" );
416 return ( EI2CNORCVBYT
);
418 else if ( EI2CBUSNFREE
== i2c_bus_free_check( MAXBUSFREERETRIES
) )
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.
426 printk( KERN_DEBUG
"I2C: EI2CBUSNFREE\n" );
427 return ( EI2CBUSNFREE
);
431 /* Finally... We made it... */
432 unsigned long irqflags
= 0;
434 /* we don't like to be interrupted */
435 local_irq_save( irqflags
);
437 /* Check if there are bytes to be send,
438 * or if you immediately want to receive data.
442 /* start I2C communication */
443 if ( EI2CNOERRORS
!= i2c_start() )
445 return ( i2c_finalise( "I2C: EI2CSTRTCOND\n", irqflags
)
450 /* send slave address: xxxxxxx0B (last bit must be zero) */
451 if ( EI2CNOERRORS
!= i2c_outbyte( slave
& WRITEADDRESS_MASK
) )
453 return ( i2c_finalise( "I2C: EI2CWADDRESS\n", irqflags
)
460 /* send register data */
461 if ( EI2CNOERRORS
!= i2c_outbyte( *wbuf
) )
463 return ( i2c_finalise( "I2C: EI2CSENDDATA\n", irqflags
)
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
484 * Generate start condition if wlen == 0
485 * or repeated start condition if wlen != 0...
487 if ( EI2CNOERRORS
!= i2c_start() )
489 return ( i2c_finalise( ( ( 0 < wlen
)
490 ? "I2C: EI2CRSTACOND\n"
491 : "I2C: EI2CSTRTCOND\n"
495 , ( ( 0 < wlen
) ? EI2CRSTACOND
: EI2CSTRTCOND
)
499 /* Send ReadAddress: xxxxxxx1B (last bit must be one) */
500 if ( EI2CNOERRORS
!= i2c_outbyte( slave
| READADDRESS_MASK
) )
502 return ( i2c_finalise( "I2C: EI2CRADDRESS\n", irqflags
)
510 *rbuf
= i2c_inbyte();
513 /* last received byte needs to be NACK-ed instead of ACK-ed */
525 /* Generate final stop condition */
526 if ( EI2CNOERRORS
!= i2c_stop() )
528 return ( i2c_finalise( "I2C: EI2CSTOPCOND\n", irqflags
)
533 /* enable interrupt again */
534 local_irq_restore( irqflags
);
537 return ( EI2CNOERRORS
);
541 /*#---------------------------------------------------------------------------
543 *# FUNCTION NAME: i2c_bus_free_check
545 *# DESCRIPTION : checks if the I2C bus is free before starting
546 *# an I2C communication
548 *# PARAMETERS : maxretries, the number of times we will try to release
551 *# RETURN : I2cStatus_I2cBusNotFreeError in case the bus is not free,
552 *# I2cStatus_I2cNoError otherwise
554 *#---------------------------------------------------------------------------
556 static int i2c_bus_free_check( unsigned char maxretries
)
558 i2c_sda_dir_in(); /* Release SDA line */
559 i2c_set_scl( SCL_HIGH
); /* put SCL line high */
561 i2c_delay( WAITONEUS
);
563 while ( ( !i2c_sda_is_high() || !i2c_scl_is_high() )
567 /* Try to release I2C bus by generating STOP conditions */
571 if ( 0 == maxretries
)
573 printk( KERN_DEBUG
"I2C: EI2CBUSNFREE\n" );
574 return ( EI2CBUSNFREE
);
578 return ( EI2CNOERRORS
);
580 } /* i2c_bus_free_check */
583 static void i2c_finalise( const char* errortxt
584 , unsigned long irqflags
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...
594 (void)i2c_bus_free_check( MAXBUSFREERETRIES
);
598 static struct file_operations i2c_fops
=
603 , .release
= i2c_release
607 /***********************************************************************/
608 /************* EXTERNAL FUNCTION DEFINITION SECTION ********************/
609 /***********************************************************************/
611 /*#---------------------------------------------------------------------------
613 *# FUNCTION NAME: i2c_init
615 *# DESCRIPTION : initialises the I2C device driver
619 *#---------------------------------------------------------------------------
621 int __init
i2c_init( void )
624 static int first
= 1;
633 /* Setup and enable the Port B I2C interface */
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" ) ) )
639 printk( KERN_CRIT
"i2c_init: Failed to get IO interface\n" );
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
);
649 port_pb_dir_shadow
&= ~IO_MASK( R_PORT_PB_DIR
, dir0
);
650 port_pb_dir_shadow
&= ~IO_MASK( R_PORT_PB_DIR
, dir1
);
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
) );
656 /* If everything goes fine, res = 0, meaning "if" fails =>
657 * will do the "else" too and as such initialise the clock port...
660 if ( ( res
= cris_io_interface_allocate_pins( if_i2c
662 , CONFIG_ETRAX_I2C_DATA_PORT
663 , CONFIG_ETRAX_I2C_DATA_PORT
668 printk( KERN_WARNING
"i2c_init: Failed to get IO pin for I2C data port\n" );
672 else if ( ( res
= cris_io_interface_allocate_pins( if_i2c
674 , CONFIG_ETRAX_I2C_CLK_PORT
675 , CONFIG_ETRAX_I2C_CLK_PORT
680 cris_io_interface_free_pins( if_i2c
682 , CONFIG_ETRAX_I2C_DATA_PORT
683 , CONFIG_ETRAX_I2C_DATA_PORT
685 printk( KERN_WARNING
"i2c_init: Failed to get IO pin for I2C clk port\n" );
693 /*#---------------------------------------------------------------------------
695 *# FUNCTION NAME: i2c_register
697 *# DESCRIPTION : this registers the i2c driver as a character device
701 *#---------------------------------------------------------------------------
703 static int __init
i2c_register( void )
707 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
709 struct cdev
*my_i2cdev
= NULL
;
721 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
722 res
= alloc_chrdev_region( &devt
, 0, 1, i2c_name
);
726 printk( KERN_DEBUG
"I2C: EI2CNOMNUMBR\n" );
730 my_i2cdev
= cdev_alloc();
731 my_i2cdev
->ops
= &i2c_fops
;
732 my_i2cdev
->owner
= THIS_MODULE
;
734 /* make device "alive" */
735 res
= cdev_add( my_i2cdev
, devt
, 1 );
739 printk( KERN_DEBUG
"I2C: EI2CDADDFAIL\n" );
744 res
= register_chrdev( I2C_MAJOR
, i2c_name
, &i2c_fops
);
748 printk( KERN_ERR
"i2c: couldn't get a major number.\n" );
755 printk( KERN_INFO
"I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n" );
758 printk( KERN_INFO
" ==> Improvements done by Geert Vancompernolle - December 2006\n" );
760 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
761 printk( KERN_INFO
"I2C Major: %d / I2C Name: %s\n", MAJOR( devt
), i2c_name
);
764 printk( KERN_INFO
"I2C Major: %d / I2C Name: %s\n", I2C_MAJOR
, i2c_name
);
773 /*#---------------------------------------------------------------------------
775 *# FUNCTION NAME: i2c_start
777 *# DESCRIPTION : generate i2c start condition
781 *# RETURN : EI2CNOERRORS if OK, EI2CSTRTCOND otherwise
783 *#---------------------------------------------------------------------------
785 int i2c_start( void )
787 /* Set SCL=1, SDA=1 */
789 i2c_set_sda( SDA_HIGH
);
790 i2c_delay( WAITONEUS
);
791 i2c_set_scl( SCL_HIGH
);
792 i2c_delay( WAITONEUS
);
794 /* Set SCL=1, SDA=0 */
795 i2c_set_sda( SDA_LOW
);
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.
804 i2c_delay( TLOW
- WAITONEUS
);
806 if ( i2c_sda_is_high() || i2c_scl_is_high() )
808 printk( KERN_DEBUG
"I2C: EI2CSTRTCOND\n" );
809 return ( EI2CSTRTCOND
);
812 return ( EI2CNOERRORS
);
816 /*#---------------------------------------------------------------------------
818 *# FUNCTION NAME: i2c_stop
820 *# DESCRIPTION : generate i2c stop condition
826 *#---------------------------------------------------------------------------
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
);
839 /* Set SCL=1, SDA=0 */
840 i2c_set_scl( SCL_HIGH
);
843 /* Set SCL=1, SDA=1 */
844 i2c_set_sda( SDA_HIGH
);
849 if ( !i2c_sda_is_high() || !i2c_scl_is_high() )
851 printk( KERN_DEBUG
"I2C: EI2CSTOPCOND\n" );
852 return ( EI2CSTOPCOND
);
855 return ( EI2CNOERRORS
);
859 /*#---------------------------------------------------------------------------
861 *# FUNCTION NAME: i2c_outbyte
863 *# DESCRIPTION : write a byte to the i2c interface
865 *# PARAMETERS : x: byte to be sent on the I2C bus
869 *#---------------------------------------------------------------------------
871 int i2c_outbyte( unsigned char x
)
877 for ( i
= 0; i
< 8; i
++ )
881 i2c_set_sda( SDA_HIGH
);
885 i2c_set_sda( SDA_LOW
);
889 i2c_set_scl( SCL_HIGH
);
891 i2c_set_scl( SCL_LOW
);
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.
901 i2c_delay( TLOW
- TSUDAT
- WAITONEUS
);
910 printk( KERN_DEBUG
"I2C: EI2CNOACKNLD\n" );
911 return( EI2CNOACKNLD
);
914 return ( EI2CNOERRORS
);
918 /*#---------------------------------------------------------------------------
920 *# FUNCTION NAME: i2c_inbyte
922 *# DESCRIPTION : read a byte from the i2c interface
926 *# RETURN : returns the byte read from the I2C device
928 *#---------------------------------------------------------------------------
930 unsigned char i2c_inbyte( void )
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! */
937 /* Switch off I2C to get bit */
943 i2c_set_scl( SCL_HIGH
);
946 if ( i2c_sda_is_high() )
951 i2c_set_scl( SCL_LOW
);
959 * we leave the clock low, getbyte is usually followed
960 * by sendack/nack, they assume the clock to be low
966 /*#---------------------------------------------------------------------------
968 *# FUNCTION NAME: i2c_getack
970 *# DESCRIPTION : checks if ack was received from ic2
974 *# RETURN : returns the ack state of the I2C device
976 *#---------------------------------------------------------------------------
978 int i2c_getack( void )
982 /* generate ACK clock pulse */
983 i2c_set_scl( SCL_HIGH
);
988 /* now wait for ack */
990 /* check for ack: if SDA is high, then NACK, else ACK */
991 if ( i2c_sda_is_high() )
1000 /* end clock pulse */
1002 i2c_set_scl( SCL_LOW
);
1004 i2c_set_sda( SDA_LOW
);
1006 /* Since we "lost" already THDDAT time, we can subtract it here... */
1007 i2c_delay( TLOW
- THDDAT
);
1013 /*#---------------------------------------------------------------------------
1015 *# FUNCTION NAME: i2c_sendack
1017 *# DESCRIPTION : sends ACK on received data
1019 *# PARAMETERS : none
1023 *#---------------------------------------------------------------------------
1025 void i2c_sendack( void )
1028 /* Clock has been set to TLOW already at end of i2c_inbyte()
1029 * and i2c_outbyte(), so no need to do it again.
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
);
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
);
1045 /* release the SDA line */
1050 /*#---------------------------------------------------------------------------
1052 *# FUNCTION NAME: i2c_sendnack
1054 *# DESCRIPTION : sends NACK on received data
1056 *# PARAMETERS : none
1060 *#---------------------------------------------------------------------------
1062 void i2c_sendnack( void )
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
1069 i2c_set_sda( SDA_HIGH
);
1073 /* generate clock pulse */
1074 i2c_delay( TSUDAT
);
1075 i2c_set_scl( SCL_HIGH
);
1077 i2c_set_scl( SCL_LOW
);
1078 i2c_delay( TSUDAT
);
1079 i2c_set_sda( SDA_LOW
);
1080 i2c_delay( TLOW
- TSUDAT
);
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...
1087 /* i2c_sda_dir_in(); */
1088 } /* i2c_sendnack */
1091 /*#---------------------------------------------------------------------------
1093 *# FUNCTION NAME: i2c_writereg
1095 *# DESCRIPTION : writes a value to a register of an I2C device
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
1101 *# RETURN : returns OR-ed result of the write action:
1107 *#---------------------------------------------------------------------------
1109 int i2c_writereg( unsigned char theSlave
1110 , unsigned char theReg
1111 , unsigned char theValue
1114 int error
, cntr
= 3;
1115 unsigned long flags
;
1117 spin_lock( &i2c_lock
);
1122 /* we don't like to be interrupted */
1123 local_irq_save( flags
);
1126 /* send slave address */
1127 if ( EI2CNOACKNLD
== i2c_outbyte( theSlave
& 0xfe ) )
1132 /* now select register */
1133 if ( EI2CNOACKNLD
== i2c_outbyte( theReg
) )
1138 /* send register register data */
1139 if ( EI2CNOACKNLD
== i2c_outbyte( theValue
) )
1144 /* end byte stream */
1146 /* enable interrupt again */
1147 local_irq_restore( flags
);
1149 } while ( error
&& cntr
-- );
1153 spin_unlock( &i2c_lock
);
1156 } /* i2c_writereg */
1159 /*#---------------------------------------------------------------------------
1161 *# FUNCTION NAME: i2c_readreg
1163 *# DESCRIPTION : reads the value from a certain register of an I2C device.
1164 *# Function first writes the register that it wants to read
1167 *# PARAMETERS : theSlave = slave address of the I2C device
1168 *# theReg = register of the I2C device that needs to be written
1170 *# RETURN : returns OR-ed result of the write action:
1176 *#---------------------------------------------------------------------------
1178 unsigned char i2c_readreg( unsigned char theSlave
1179 , unsigned char theReg
1182 unsigned char b
= 0;
1183 int error
, cntr
= 3;
1184 unsigned long flags
;
1186 spin_lock( &i2c_lock
);
1192 /* we don't like to be interrupted */
1193 local_irq_save( flags
);
1195 /* generate start condition */
1198 /* send slave address */
1199 if ( EI2CNOACKNLD
== i2c_outbyte( theSlave
& 0xfe ) )
1204 /* now select register */
1207 if ( EI2CNOACKNLD
== i2c_outbyte( theReg
) )
1212 /* repeat start condition */
1216 /* send slave address */
1217 if ( EI2CNOACKNLD
== i2c_outbyte( theSlave
| 0x01 ) )
1222 /* fetch register */
1225 * last received byte needs to be nacked
1233 /* enable interrupt again */
1234 local_irq_restore( flags
);
1236 } while ( error
&& cntr
-- );
1238 spin_unlock( &i2c_lock
);
1244 /*#---------------------------------------------------------------------------
1246 *# FUNCTION NAME: i2c_read
1254 *#---------------------------------------------------------------------------
1256 int i2c_read( unsigned char slave
, unsigned char* rbuf
, unsigned char rlen
)
1258 return ( i2c_command( slave
, NULL
, 0, rbuf
, rlen
) );
1262 /*#---------------------------------------------------------------------------
1264 *# FUNCTION NAME: i2c_write
1272 *#---------------------------------------------------------------------------
1274 int i2c_write( unsigned char slave
, unsigned char* wbuf
, unsigned char wlen
)
1276 return ( i2c_command( slave
, wbuf
, wlen
, NULL
, 0 ) );
1280 /*#---------------------------------------------------------------------------
1282 *# FUNCTION NAME: i2c_writeread
1290 *#---------------------------------------------------------------------------
1292 int i2c_writeread( unsigned char slave
1293 , unsigned char* wbuf
1294 , unsigned char wlen
1295 , unsigned char* rbuf
1296 , unsigned char rlen
1299 return ( i2c_command( slave
, wbuf
, wlen
, rbuf
, rlen
) );
1300 } /* i2c_writeread */
1303 /*#---------------------------------------------------------------------------
1305 *# FUNCTION NAME: module_init
1307 *# DESCRIPTION : this makes sure that i2c_register is called during boot
1311 *#---------------------------------------------------------------------------
1313 module_init( i2c_register
);
1315 /****************** END OF FILE i2c.c ********************************/