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