1 /*!***************************************************************************
6 *! ---------------------------------------------------------------------------
8 *! ( C ) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
10 *!***************************************************************************/
11 /* $Id: i2c.c,v 1.16 2005/09/29 13:33:35 bjarne Exp $ */
13 #define DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
14 //#undef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
16 /******************** INCLUDE FILES SECTION ****************************/
18 #include <linux/module.h>
22 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
23 #include <linux/types.h> /* for dev_t */
24 #include <linux/cdev.h> /* for struct cdev */
31 #include "i2c_errno.h"
35 #include <asm/delay.h>
36 #include <asm/arch/io_interface_mux.h>
37 #include <asm/uaccess.h>
41 MODULE_DESCRIPTION( "I2C Device Driver - 1.1" );
43 /*!*********************************************************************
44 *!History I2C driver Geert Vancompernolle
45 *!---------------------------------------
48 *! First official version.
51 *! Changes to remove unwanted spikes at ACK/NACK time.
53 *!*********************************************************************/
55 MODULE_LICENSE( "GPL" );
57 /****************** MACRO's **********************/
62 #ifndef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
64 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
71 /* Following are abbreviations taken from Philips I2C standard */
72 /* Values are representing time in us and are rounded to next whole number, if relevant */
73 #define THDSTA 4 /* Hold delay time for (repeated) START condition */
74 #define TLOW 5 /* LOW period of the SCL clock */
75 #define THDDAT 1 /* Hold delay time for DATA: value of 0 is allowed but 1 taken to be sure */
76 #define TSUDAT 1 /* Set-up time for DATA */
77 #define THIGH 4 /* HIGH period of the SCL clock */
78 #define TSUSTA 5 /* Set-up time for a repeated START condition */
79 #define TSUSTO 4 /* Set-up time for STOP condition */
80 #define TBUF 5 /* Bus-free time between STOP and START condition */
82 #define MAXBUSFREERETRIES 5
84 #define WRITEADDRESS_MASK ( 0xFE )
85 #define READADDRESS_MASK ( 0x01 )
93 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
94 /* Use PB and not PB_I2C */
95 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
96 #define CONFIG_ETRAX_I2C_DATA_PORT 0
98 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
99 #define CONFIG_ETRAX_I2C_CLK_PORT 1
102 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
103 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
105 #define i2c_disable()
107 /* enable or disable output-enable, to select output or input on the i2c bus */
108 #define i2c_sda_dir_out() \
109 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1 )
110 #define i2c_sda_dir_in() \
111 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0 )
113 /* control the i2c clock and data signals */
114 #define i2c_set_scl( x ) \
115 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x )
116 #define i2c_set_sda( x ) \
117 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x )
119 /* read status of SDA bit from the i2c interface */
120 #define i2c_sda_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SDABIT ) ) ) >> SDABIT )
123 /* read status of SCL bit from the i2c interface */
124 #define i2c_scl_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SCLBIT ) ) ) >> SCLBIT )
128 /* enable or disable the i2c interface */
129 #define i2c_enable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_en ) )
130 #define i2c_disable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_en ) )
132 /* enable or disable output-enable, to select output or input on the i2c bus */
133 #define i2c_sda_dir_out() \
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, 1 );
136 #define i2c_sda_dir_in() \
137 *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
138 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0 );
140 /* control the i2c clock and data signals */
141 #define i2c_set_scl( x ) \
142 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
143 ~IO_MASK( R_PORT_PB_I2C, i2c_set_scl ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, ( x ) ) ); \
144 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 1, x );
146 #define i2c_set_sda( x ) \
147 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
148 ~IO_MASK( R_PORT_PB_I2C, i2c_d ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_d, ( x ) ) ); \
149 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 0, x );
151 /* read a bit from the i2c interface */
152 #define i2c_sda_is_high() ( *R_PORT_PB_READ & 0x1 )
155 /* use the kernels delay routine */
156 #define i2c_delay( usecs ) udelay( usecs )
159 /****************** TYPEDEF's **********************/
162 /****************** STATIC (file scope) VARIABLES **********************/
163 static DEFINE_SPINLOCK( i2c_lock
); /* Protect directions etc */
165 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
166 static const char i2c_name
[] = "i2cgvc";
168 static const char i2c_name
[] = "i2c";
173 /****************** PROTOTYPING SECTION *************************/
174 static int i2c_open( struct inode
*inode
, struct file
*filp
);
175 static int i2c_release( struct inode
*inode
, struct file
*filp
);
177 static int i2c_command( unsigned char slave
178 , unsigned char* wbuf
180 , unsigned char* rbuf
183 static int i2c_bus_free_check( unsigned char maxretries
);
184 static void i2c_finalise( const char* text
, unsigned long irqflags
);
188 /************************************************************************/
189 /****************** AUXILIARIES *************************/
190 /************************************************************************/
192 /*#---------------------------------------------------------------------------
194 *# FUNCTION NAME: i2c_open
196 *# DESCRIPTION : opens an I2C device
198 *# PARAMETERS : *inode: reference to inode
199 *# *filp : reference to file pointer
201 *#---------------------------------------------------------------------------
203 static int i2c_open( struct inode
*inode
, struct file
*filp
)
209 /*#---------------------------------------------------------------------------
211 *# FUNCTION NAME: i2c_release
213 *# DESCRIPTION : Releases the I2C device
215 *# PARAMETERS : *inode: reference to inode
216 *# *filp : reference to file pointer
218 *#---------------------------------------------------------------------------
220 static int i2c_release( struct inode
*inode
, struct file
*filp
)
226 /*#---------------------------------------------------------------------------
228 *# FUNCTION NAME: i2c_ioctl
230 *# DESCRIPTION : Main device API: ioctl's to write/read
231 *# to/from i2c registers
233 *# PARAMETERS : *inode: reference to inode
234 *# *filp : reference to file pointer
235 *# cmd : command to be executed during the ioctl call
236 *# arg : pointer to a structure with the data???
238 *# RETURN : result of the ioctl call
240 *#---------------------------------------------------------------------------
242 static int i2c_ioctl( struct inode
*inode
248 /* the acme ioctls */
250 int RetVal
= EI2CNOERRORS
;
252 if ( _IOC_TYPE( cmd
) != ETRAXI2C_IOCTYPE
)
257 switch ( _IOC_NR( cmd
) )
260 /* write to an i2c slave */
261 RetVal
= i2c_writereg( I2C_ARGSLAVE( arg
)
263 , I2C_ARGVALUE( arg
)
268 RetVal
= i2c_readreg( I2C_ARGSLAVE( arg
), I2C_ARGREG( arg
) );
272 /* New functions added by GVC */
274 copy_from_user( (char*)&i2cdata
, (char*)arg
, sizeof( I2C_DATA
) );
276 int RetryCntr
= MAXRETRIES
;
280 RetVal
= i2c_command( i2cdata
.slave
286 } while ( ( EI2CNOERRORS
!= RetVal
)
290 copy_to_user( (char*)arg
, (char*)&i2cdata
, sizeof( I2C_DATA
) );
294 copy_from_user( (char*)&i2cdata
, (char*)arg
, sizeof( I2C_DATA
) );
296 int RetryCntr
= MAXRETRIES
;
300 RetVal
= i2c_command( i2cdata
.slave
306 } while ( ( EI2CNOERRORS
!= RetVal
)
313 copy_from_user( (char*)&i2cdata
, (char*)arg
, sizeof( I2C_DATA
) );
315 int RetryCntr
= MAXRETRIES
;
319 RetVal
= i2c_command( i2cdata
.slave
325 } while ( ( EI2CNOERRORS
!= RetVal
)
329 copy_to_user( (char*)arg
, (char*)&i2cdata
, sizeof( I2C_DATA
) );
341 /*#---------------------------------------------------------------------------
343 *# FUNCTION NAME: i2c_command
345 *# DESCRIPTION : general routine to read/write bytes from an I2C device
347 *# 'i2c_command()' sends wlen bytes to the I2c bus and receives
348 *# rlen bytes from the I2c bus.
349 *# The data to be send must be placed in wbuf[ 0 ] upto wbuf[ wlen - 1 ).
350 *# The data to be received is assembled in rbuf[ 0 ] upto rbuf[ rlen - 1 ].
352 *# If no data is to be sent or received, put appropriate buffer parameter
353 *# to "NULL" and appropriate length parameter to "0".
355 *# PARAMETERS : slave = slave address of the I2C device
356 *# wbuf = address of first element of write buffer (wbuf)
357 *# wlen = number of bytes to be written to slave
358 *# rbuf = address of first element of read buffer (rbuf)
359 *# rlen = number of bytes to be read from slave
362 *# EI2CNOERRORS: I2C communication went fine
363 *# EI2CBUSNFREE: I2C bus is not free
364 *# EI2CWADDRESS: I2C write address failed
365 *# EI2CRADDRESS: I2C read address failed
366 *# EI2CSENDDATA: I2C send data failed
367 *# EI2CRECVDATA: I2C receive data failed
368 *# EI2CSTRTCOND: I2C start condition failed
369 *# EI2CRSTACOND: I2C repeated start condition failed
370 *# EI2CSTOPCOND: I2C stop condition failed
371 *# EI2CNOSNDBYT: I2C no bytes to be sent
372 *# EI2CNOSNDBUF: I2C no send buffer defined
373 *# EI2CNORCVBYT: I2C no bytes to be received
374 *# EI2CNORCVBUF: I2C no receive buffer defined
375 *# EI2CNOACKNLD: I2C no acknowledge received
378 *# First, the send part is completed.
379 *# In the send routine, there is no stop generated. This is because maybe
380 *# a repeated start condition must be generated.
381 *# This happens when we want to receive some data from the I2c bus. If not,
382 *# at the end of the general I2c loop the stopcondition is generated.
383 *# If, on the contrary, there are a number of bytes to be received, a new
384 *# startcondition is generated in the 'if' part of the main I2c routine,
385 *# which controls the receiving part.
386 *# Only when the receiving of data is finished, a final stopcondition is
389 *#---------------------------------------------------------------------------
391 static int i2c_command( unsigned char slave
392 , unsigned char* wbuf
394 , unsigned char* rbuf
398 /* Check arguments and report error if relevant... */
399 if ( ( wlen
> 0 ) && ( wbuf
== NULL
) )
401 printk( KERN_DEBUG
"I2C: EI2CNOSNDBUF\n" );
402 return ( EI2CNOSNDBUF
);
404 else if ( ( wlen
== 0 ) && ( wbuf
!= NULL
) )
406 printk( KERN_DEBUG
"I2C: EI2CNOSNDBYT\n" );
407 return ( EI2CNOSNDBYT
);
409 else if ( ( rlen
> 0 ) && ( rbuf
== NULL
) )
411 printk( KERN_DEBUG
"I2C: EI2CNORCVBUF\n" );
412 return ( EI2CNORCVBUF
);
414 else if ( ( rlen
== 0 ) && ( rbuf
!= NULL
) )
416 printk( KERN_DEBUG
"I2C: EI2CNORCVBYT\n" );
417 return ( EI2CNORCVBYT
);
419 else if ( EI2CBUSNFREE
== i2c_bus_free_check( MAXBUSFREERETRIES
) )
421 /* There's no need to try more, since we weren't even
422 * able to start the I2C communication.
423 * So, no IRQ flags are stored yet, no changes to any other
424 * stuff like START, STOP, SENDBYTES...
425 * Result, simply write down the error and return the correct error code.
427 printk( KERN_DEBUG
"I2C: EI2CBUSNFREE\n" );
428 return ( EI2CBUSNFREE
);
432 /* Finally... We made it... */
433 unsigned long irqflags
= 0;
435 /* we don't like to be interrupted */
436 local_irq_save( irqflags
);
438 /* Check if there are bytes to be send,
439 * or if you immediately want to receive data.
443 /* start I2C communication */
444 if ( EI2CNOERRORS
!= i2c_start() )
446 return ( i2c_finalise( "I2C: EI2CSTRTCOND\n", irqflags
)
451 /* send slave address: xxxxxxx0B (last bit must be zero) */
452 if ( EI2CNOERRORS
!= i2c_outbyte( slave
& WRITEADDRESS_MASK
) )
454 return ( i2c_finalise( "I2C: EI2CWADDRESS\n", irqflags
)
461 /* send register data */
462 if ( EI2CNOERRORS
!= i2c_outbyte( *wbuf
) )
464 return ( i2c_finalise( "I2C: EI2CSENDDATA\n", irqflags
)
476 * Receiving data from I2c_bus
477 * If there are bytes to be received, a new start condition is
478 * generated => Repeated Startcondition.
479 * A final stopcondition is generated at the end of the main I2c
485 * Generate start condition if wlen == 0
486 * or repeated start condition if wlen != 0...
488 if ( EI2CNOERRORS
!= i2c_start() )
490 return ( i2c_finalise( ( ( 0 < wlen
)
491 ? "I2C: EI2CRSTACOND\n"
492 : "I2C: EI2CSTRTCOND\n"
496 , ( ( 0 < wlen
) ? EI2CRSTACOND
: EI2CSTRTCOND
)
500 /* Send ReadAddress: xxxxxxx1B (last bit must be one) */
501 if ( EI2CNOERRORS
!= i2c_outbyte( slave
| READADDRESS_MASK
) )
503 return ( i2c_finalise( "I2C: EI2CRADDRESS\n", irqflags
)
511 *rbuf
= i2c_inbyte();
514 /* last received byte needs to be NACK-ed instead of ACK-ed */
526 /* Generate final stop condition */
527 if ( EI2CNOERRORS
!= i2c_stop() )
529 return ( i2c_finalise( "I2C: EI2CSTOPCOND\n", irqflags
)
534 /* enable interrupt again */
535 local_irq_restore( irqflags
);
538 return ( EI2CNOERRORS
);
542 /*#---------------------------------------------------------------------------
544 *# FUNCTION NAME: i2c_bus_free_check
546 *# DESCRIPTION : checks if the I2C bus is free before starting
547 *# an I2C communication
549 *# PARAMETERS : maxretries, the number of times we will try to release
552 *# RETURN : I2cStatus_I2cBusNotFreeError in case the bus is not free,
553 *# I2cStatus_I2cNoError otherwise
555 *#---------------------------------------------------------------------------
557 static int i2c_bus_free_check( unsigned char maxretries
)
559 i2c_sda_dir_in(); /* Release SDA line */
560 i2c_set_scl( SCL_HIGH
); /* put SCL line high */
562 i2c_delay( WAITONEUS
);
564 while ( ( !i2c_sda_is_high() || !i2c_scl_is_high() )
568 /* Try to release I2C bus by generating STOP conditions */
572 if ( 0 == maxretries
)
574 printk( KERN_DEBUG
"I2C: EI2CBUSNFREE\n" );
575 return ( EI2CBUSNFREE
);
579 return ( EI2CNOERRORS
);
581 } /* i2c_bus_free_check */
584 static void i2c_finalise( const char* errortxt
585 , unsigned long irqflags
588 printk( KERN_DEBUG
"%s", errortxt
);
589 local_irq_restore( irqflags
);
590 /* The least we can do when things go terribly wrong,
591 * is to try to release the bus.
592 * If this fails, well, then I don't know
593 * what I can do more for the moment...
595 (void)i2c_bus_free_check( MAXBUSFREERETRIES
);
599 static struct file_operations i2c_fops
=
604 , .release
= i2c_release
608 /***********************************************************************/
609 /************* EXTERNAL FUNCTION DEFINITION SECTION ********************/
610 /***********************************************************************/
612 /*#---------------------------------------------------------------------------
614 *# FUNCTION NAME: i2c_init
616 *# DESCRIPTION : initialises the I2C device driver
620 *#---------------------------------------------------------------------------
622 int __init
i2c_init( void )
625 static int first
= 1;
634 /* Setup and enable the Port B I2C interface */
636 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
637 /* here, we're using the dedicated I2C pins of FoxBoard */
638 if ( ( res
= cris_request_io_interface( if_i2c
, "I2C" ) ) )
640 printk( KERN_CRIT
"i2c_init: Failed to get IO interface\n" );
644 *R_PORT_PB_I2C
= port_pb_i2c_shadow
|=
645 IO_STATE( R_PORT_PB_I2C
, i2c_en
, on
) |
646 IO_FIELD( R_PORT_PB_I2C
, i2c_d
, 1 ) |
647 IO_FIELD( R_PORT_PB_I2C
, i2c_set_scl
, 1 ) |
648 IO_STATE( R_PORT_PB_I2C
, i2c_oe_
, enable
);
650 port_pb_dir_shadow
&= ~IO_MASK( R_PORT_PB_DIR
, dir0
);
651 port_pb_dir_shadow
&= ~IO_MASK( R_PORT_PB_DIR
, dir1
);
653 *R_PORT_PB_DIR
= ( port_pb_dir_shadow
|=
654 IO_STATE( R_PORT_PB_DIR
, dir0
, input
) |
655 IO_STATE( R_PORT_PB_DIR
, dir1
, output
) );
657 /* If everything goes fine, res = 0, meaning "if" fails =>
658 * will do the "else" too and as such initialise the clock port...
661 if ( ( res
= cris_io_interface_allocate_pins( if_i2c
663 , CONFIG_ETRAX_I2C_DATA_PORT
664 , CONFIG_ETRAX_I2C_DATA_PORT
669 printk( KERN_WARNING
"i2c_init: Failed to get IO pin for I2C data port\n" );
673 else if ( ( res
= cris_io_interface_allocate_pins( if_i2c
675 , CONFIG_ETRAX_I2C_CLK_PORT
676 , CONFIG_ETRAX_I2C_CLK_PORT
681 cris_io_interface_free_pins( if_i2c
683 , CONFIG_ETRAX_I2C_DATA_PORT
684 , CONFIG_ETRAX_I2C_DATA_PORT
686 printk( KERN_WARNING
"i2c_init: Failed to get IO pin for I2C clk port\n" );
694 /*#---------------------------------------------------------------------------
696 *# FUNCTION NAME: i2c_register
698 *# DESCRIPTION : this registers the i2c driver as a character device
702 *#---------------------------------------------------------------------------
704 static int __init
i2c_register( void )
708 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
710 struct cdev
*my_i2cdev
= NULL
;
722 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
723 res
= alloc_chrdev_region( &devt
, 0, 1, i2c_name
);
727 printk( KERN_DEBUG
"I2C: EI2CNOMNUMBR\n" );
731 my_i2cdev
= cdev_alloc();
732 my_i2cdev
->ops
= &i2c_fops
;
733 my_i2cdev
->owner
= THIS_MODULE
;
735 /* make device "alive" */
736 res
= cdev_add( my_i2cdev
, devt
, 1 );
740 printk( KERN_DEBUG
"I2C: EI2CDADDFAIL\n" );
745 res
= register_chrdev( I2C_MAJOR
, i2c_name
, &i2c_fops
);
749 printk( KERN_ERR
"i2c: couldn't get a major number.\n" );
756 printk( KERN_INFO
"I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n" );
759 printk( KERN_INFO
" ==> Improvements done by Geert Vancompernolle - December 2006\n" );
761 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
762 printk( KERN_INFO
"I2C Major: %d / I2C Name: %s\n", MAJOR( devt
), i2c_name
);
765 printk( KERN_INFO
"I2C Major: %d / I2C Name: %s\n", I2C_MAJOR
, i2c_name
);
774 /*#---------------------------------------------------------------------------
776 *# FUNCTION NAME: i2c_start
778 *# DESCRIPTION : generate i2c start condition
782 *# RETURN : EI2CNOERRORS if OK, EI2CSTRTCOND otherwise
784 *#---------------------------------------------------------------------------
786 int i2c_start( void )
788 /* Set SCL=1, SDA=1 */
790 i2c_set_sda( SDA_HIGH
);
791 i2c_delay( WAITONEUS
);
792 i2c_set_scl( SCL_HIGH
);
793 i2c_delay( WAITONEUS
);
795 /* Set SCL=1, SDA=0 */
796 i2c_set_sda( SDA_LOW
);
799 /* Set SCL=0, SDA=0 */
800 i2c_set_scl( SCL_LOW
);
801 /* We can take 1 us less than defined in spec (5 us), since the next action
802 * will be to set the dataline high or low and this action is 1 us
803 * before the clock is put high, so that makes our 5 us.
805 i2c_delay( TLOW
- WAITONEUS
);
807 if ( i2c_sda_is_high() || i2c_scl_is_high() )
809 printk( KERN_DEBUG
"I2C: EI2CSTRTCOND\n" );
810 return ( EI2CSTRTCOND
);
813 return ( EI2CNOERRORS
);
817 /*#---------------------------------------------------------------------------
819 *# FUNCTION NAME: i2c_stop
821 *# DESCRIPTION : generate i2c stop condition
827 *#---------------------------------------------------------------------------
833 /* Set SCL=0, SDA=0 */
834 /* Don't change order, otherwise you might generate a start condition! */
835 i2c_set_scl( SCL_LOW
);
836 i2c_delay( WAITONEUS
);
837 i2c_set_sda( SDA_LOW
);
838 i2c_delay( WAITONEUS
);
840 /* Set SCL=1, SDA=0 */
841 i2c_set_scl( SCL_HIGH
);
844 /* Set SCL=1, SDA=1 */
845 i2c_set_sda( SDA_HIGH
);
850 if ( !i2c_sda_is_high() || !i2c_scl_is_high() )
852 printk( KERN_DEBUG
"I2C: EI2CSTOPCOND\n" );
853 return ( EI2CSTOPCOND
);
856 return ( EI2CNOERRORS
);
860 /*#---------------------------------------------------------------------------
862 *# FUNCTION NAME: i2c_outbyte
864 *# DESCRIPTION : write a byte to the i2c interface
866 *# PARAMETERS : x: byte to be sent on the I2C bus
870 *#---------------------------------------------------------------------------
872 int i2c_outbyte( unsigned char x
)
878 for ( i
= 0; i
< 8; i
++ )
882 i2c_set_sda( SDA_HIGH
);
886 i2c_set_sda( SDA_LOW
);
890 i2c_set_scl( SCL_HIGH
);
892 i2c_set_scl( SCL_LOW
);
894 i2c_set_sda( SDA_LOW
);
895 /* There should be only 5 us between falling edge and new rising
896 * edge of clock pulse.
897 * Since we spend already 1 us since clock edge was low, there are
898 * only ( TLOW - TSUDAT ) us left.
899 * Next to this, since the data line will be set up 1 us before the
900 * clock line is set up, we can reduce the delay with another us.
902 i2c_delay( TLOW
- TSUDAT
- WAITONEUS
);
911 printk( KERN_DEBUG
"I2C: EI2CNOACKNLD\n" );
912 return( EI2CNOACKNLD
);
915 return ( EI2CNOERRORS
);
919 /*#---------------------------------------------------------------------------
921 *# FUNCTION NAME: i2c_inbyte
923 *# DESCRIPTION : read a byte from the i2c interface
927 *# RETURN : returns the byte read from the I2C device
929 *#---------------------------------------------------------------------------
931 unsigned char i2c_inbyte( void )
933 unsigned char aBitByte
= 0;
934 unsigned char Mask
= 0x80; /* !!! ATTENTION: do NOT use 'char', otherwise shifting is wrong!!! */
935 /* Must be UNSIGNED, not SIGNED! */
938 /* Switch off I2C to get bit */
944 i2c_set_scl( SCL_HIGH
);
947 if ( i2c_sda_is_high() )
952 i2c_set_scl( SCL_LOW
);
960 * we leave the clock low, getbyte is usually followed
961 * by sendack/nack, they assume the clock to be low
967 /*#---------------------------------------------------------------------------
969 *# FUNCTION NAME: i2c_getack
971 *# DESCRIPTION : checks if ack was received from ic2
975 *# RETURN : returns the ack state of the I2C device
977 *#---------------------------------------------------------------------------
979 int i2c_getack( void )
983 /* generate ACK clock pulse */
984 i2c_set_scl( SCL_HIGH
);
989 /* now wait for ack */
991 /* check for ack: if SDA is high, then NACK, else ACK */
992 if ( i2c_sda_is_high() )
1001 /* end clock pulse */
1003 i2c_set_scl( SCL_LOW
);
1005 i2c_set_sda( SDA_LOW
);
1007 /* Since we "lost" already THDDAT time, we can subtract it here... */
1008 i2c_delay( TLOW
- THDDAT
);
1014 /*#---------------------------------------------------------------------------
1016 *# FUNCTION NAME: i2c_sendack
1018 *# DESCRIPTION : sends ACK on received data
1020 *# PARAMETERS : none
1024 *#---------------------------------------------------------------------------
1026 void i2c_sendack( void )
1029 /* Clock has been set to TLOW already at end of i2c_inbyte()
1030 * and i2c_outbyte(), so no need to do it again.
1033 /* set ack pulse low */
1034 i2c_set_sda( SDA_LOW
);
1035 /* generate clock pulse */
1036 i2c_delay( TSUDAT
);
1037 i2c_set_scl( SCL_HIGH
);
1039 i2c_set_scl( SCL_LOW
);
1040 i2c_delay( THDDAT
);
1041 /* reset data out */
1042 i2c_set_sda( SDA_HIGH
);
1043 /* Subtract time spend already when waited to put SDA high */
1044 i2c_delay( TLOW
- THDDAT
);
1046 /* release the SDA line */
1051 /*#---------------------------------------------------------------------------
1053 *# FUNCTION NAME: i2c_sendnack
1055 *# DESCRIPTION : sends NACK on received data
1057 *# PARAMETERS : none
1061 *#---------------------------------------------------------------------------
1063 void i2c_sendnack( void )
1065 /* make sure the SDA line is set high prior to activation of the output.
1066 * this way, you avoid an unnecessary peak to ground when a NACK has to
1070 i2c_set_sda( SDA_HIGH
);
1074 /* generate clock pulse */
1075 i2c_delay( TSUDAT
);
1076 i2c_set_scl( SCL_HIGH
);
1078 i2c_set_scl( SCL_LOW
);
1079 i2c_delay( TSUDAT
);
1080 i2c_set_sda( SDA_LOW
);
1081 i2c_delay( TLOW
- TSUDAT
);
1083 /* There's no need to change the direction of SDA to "in" again,
1084 * since a NACK is always followed by a stop condition.
1085 * A STOP condition will put the direction of SDA back to "out"
1086 * resulting in a useless SDA "dip" on the line...
1088 /* i2c_sda_dir_in(); */
1089 } /* i2c_sendnack */
1092 /*#---------------------------------------------------------------------------
1094 *# FUNCTION NAME: i2c_writereg
1096 *# DESCRIPTION : writes a value to a register of an I2C device
1098 *# PARAMETERS : theSlave = slave address of the I2C device
1099 *# theReg = register of the I2C device that needs to be written
1100 *# theValue = value to be written to the register
1102 *# RETURN : returns OR-ed result of the write action:
1108 *#---------------------------------------------------------------------------
1110 int i2c_writereg( unsigned char theSlave
1111 , unsigned char theReg
1112 , unsigned char theValue
1115 int error
, cntr
= 3;
1116 unsigned long flags
;
1118 spin_lock( &i2c_lock
);
1123 /* we don't like to be interrupted */
1124 local_irq_save( flags
);
1127 /* send slave address */
1128 if ( EI2CNOACKNLD
== i2c_outbyte( theSlave
& 0xfe ) )
1133 /* now select register */
1134 if ( EI2CNOACKNLD
== i2c_outbyte( theReg
) )
1139 /* send register register data */
1140 if ( EI2CNOACKNLD
== i2c_outbyte( theValue
) )
1145 /* end byte stream */
1147 /* enable interrupt again */
1148 local_irq_restore( flags
);
1150 } while ( error
&& cntr
-- );
1154 spin_unlock( &i2c_lock
);
1157 } /* i2c_writereg */
1160 /*#---------------------------------------------------------------------------
1162 *# FUNCTION NAME: i2c_readreg
1164 *# DESCRIPTION : reads the value from a certain register of an I2C device.
1165 *# Function first writes the register that it wants to read
1168 *# PARAMETERS : theSlave = slave address of the I2C device
1169 *# theReg = register of the I2C device that needs to be written
1171 *# RETURN : returns OR-ed result of the write action:
1177 *#---------------------------------------------------------------------------
1179 unsigned char i2c_readreg( unsigned char theSlave
1180 , unsigned char theReg
1183 unsigned char b
= 0;
1184 int error
, cntr
= 3;
1185 unsigned long flags
;
1187 spin_lock( &i2c_lock
);
1193 /* we don't like to be interrupted */
1194 local_irq_save( flags
);
1196 /* generate start condition */
1199 /* send slave address */
1200 if ( EI2CNOACKNLD
== i2c_outbyte( theSlave
& 0xfe ) )
1205 /* now select register */
1208 if ( EI2CNOACKNLD
== i2c_outbyte( theReg
) )
1213 /* repeat start condition */
1217 /* send slave address */
1218 if ( EI2CNOACKNLD
== i2c_outbyte( theSlave
| 0x01 ) )
1223 /* fetch register */
1226 * last received byte needs to be nacked
1234 /* enable interrupt again */
1235 local_irq_restore( flags
);
1237 } while ( error
&& cntr
-- );
1239 spin_unlock( &i2c_lock
);
1245 /*#---------------------------------------------------------------------------
1247 *# FUNCTION NAME: i2c_read
1255 *#---------------------------------------------------------------------------
1257 int i2c_read( unsigned char slave
, unsigned char* rbuf
, unsigned char rlen
)
1259 return ( i2c_command( slave
, NULL
, 0, rbuf
, rlen
) );
1263 /*#---------------------------------------------------------------------------
1265 *# FUNCTION NAME: i2c_write
1273 *#---------------------------------------------------------------------------
1275 int i2c_write( unsigned char slave
, unsigned char* wbuf
, unsigned char wlen
)
1277 return ( i2c_command( slave
, wbuf
, wlen
, NULL
, 0 ) );
1281 /*#---------------------------------------------------------------------------
1283 *# FUNCTION NAME: i2c_writeread
1291 *#---------------------------------------------------------------------------
1293 int i2c_writeread( unsigned char slave
1294 , unsigned char* wbuf
1295 , unsigned char wlen
1296 , unsigned char* rbuf
1297 , unsigned char rlen
1300 return ( i2c_command( slave
, wbuf
, wlen
, rbuf
, rlen
) );
1301 } /* i2c_writeread */
1304 /*#---------------------------------------------------------------------------
1306 *# FUNCTION NAME: module_init
1308 *# DESCRIPTION : this makes sure that i2c_register is called during boot
1312 *#---------------------------------------------------------------------------
1314 module_init( i2c_register
);
1316 /****************** END OF FILE i2c.c ********************************/