1 /*!***************************************************************************
\r
6 *! ---------------------------------------------------------------------------
\r
8 *! ( C ) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
\r
10 *!***************************************************************************/
\r
12 #define DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
13 //#undef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
15 /******************** INCLUDE FILES SECTION ****************************/
\r
17 #include <linux/module.h>
\r
18 #include <linux/fs.h>
\r
21 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
22 #include <linux/types.h> /* for dev_t */
\r
23 #include <linux/cdev.h> /* for struct cdev */
\r
27 #include "etraxi2c.h"
\r
30 #include "i2c_errno.h"
\r
34 #include <asm/delay.h>
\r
35 #include <asm/arch/io_interface_mux.h>
\r
36 #include <asm/uaccess.h>
\r
38 #include "i2c_gvc.h"
\r
40 MODULE_DESCRIPTION( "I2C Device Driver - 1.1" );
\r
42 /*!*********************************************************************
\r
43 *!History I2C driver Geert Vancompernolle
\r
44 *!---------------------------------------
\r
47 *! First official version.
\r
50 *! Changes to remove unwanted spikes at ACK/NACK time.
\r
52 *!*********************************************************************/
\r
54 MODULE_LICENSE( "GPL" );
\r
56 /****************** MACRO's **********************/
\r
61 #ifndef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
63 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
\r
70 /* Following are abbreviations taken from Philips I2C standard */
\r
71 /* Values are representing time in us and are rounded to next whole number, if relevant */
\r
72 #define THDSTA 4 /* Hold delay time for (repeated) START condition */
\r
73 #define TLOW 5 /* LOW period of the SCL clock */
\r
74 #define THDDAT 1 /* Hold delay time for DATA: value of 0 is allowed but 1 taken to be sure */
\r
75 #define TSUDAT 1 /* Set-up time for DATA */
\r
76 #define THIGH 4 /* HIGH period of the SCL clock */
\r
77 #define TSUSTA 5 /* Set-up time for a repeated START condition */
\r
78 #define TSUSTO 4 /* Set-up time for STOP condition */
\r
79 #define TBUF 5 /* Bus-free time between STOP and START condition */
\r
81 #define MAXBUSFREERETRIES 5
\r
82 #define MAXRETRIES 3
\r
83 #define WRITEADDRESS_MASK ( 0xFE )
\r
84 #define READADDRESS_MASK ( 0x01 )
\r
92 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
\r
93 /* Use PB and not PB_I2C */
\r
94 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
\r
95 #define CONFIG_ETRAX_I2C_DATA_PORT 0
\r
97 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
\r
98 #define CONFIG_ETRAX_I2C_CLK_PORT 1
\r
101 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
\r
102 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
\r
103 #define i2c_enable()
\r
104 #define i2c_disable()
\r
106 /* enable or disable output-enable, to select output or input on the i2c bus */
\r
107 #define i2c_sda_dir_out() \
\r
108 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1 )
\r
109 #define i2c_sda_dir_in() \
\r
110 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0 )
\r
112 /* control the i2c clock and data signals */
\r
113 #define i2c_set_scl( x ) \
\r
114 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x )
\r
115 #define i2c_set_sda( x ) \
\r
116 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x )
\r
118 /* read status of SDA bit from the i2c interface */
\r
119 #define i2c_sda_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SDABIT ) ) ) >> SDABIT )
\r
122 /* read status of SCL bit from the i2c interface */
\r
123 #define i2c_scl_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SCLBIT ) ) ) >> SCLBIT )
\r
127 /* enable or disable the i2c interface */
\r
128 #define i2c_enable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_en ) )
\r
129 #define i2c_disable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_en ) )
\r
131 /* enable or disable output-enable, to select output or input on the i2c bus */
\r
132 #define i2c_sda_dir_out() \
\r
133 *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
\r
134 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1 );
\r
135 #define i2c_sda_dir_in() \
\r
136 *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
\r
137 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0 );
\r
139 /* control the i2c clock and data signals */
\r
140 #define i2c_set_scl( x ) \
\r
141 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
\r
142 ~IO_MASK( R_PORT_PB_I2C, i2c_set_scl ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, ( x ) ) ); \
\r
143 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 1, x );
\r
145 #define i2c_set_sda( x ) \
\r
146 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
\r
147 ~IO_MASK( R_PORT_PB_I2C, i2c_d ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_d, ( x ) ) ); \
\r
148 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 0, x );
\r
150 /* read a bit from the i2c interface */
\r
151 #define i2c_sda_is_high() ( *R_PORT_PB_READ & 0x1 )
\r
154 /* use the kernels delay routine */
\r
155 #define i2c_delay( usecs ) udelay( usecs )
\r
158 /****************** TYPEDEF's **********************/
\r
161 /****************** STATIC (file scope) VARIABLES **********************/
\r
162 static DEFINE_SPINLOCK( i2c_lock ); /* Protect directions etc */
\r
164 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
165 static const char i2c_name[] = "i2cgvc";
\r
167 static const char i2c_name[] = "i2c";
\r
172 /****************** PROTOTYPING SECTION *************************/
\r
173 static int i2c_open( struct inode *inode, struct file *filp );
\r
174 static int i2c_release( struct inode *inode, struct file *filp );
\r
176 static int i2c_command( unsigned char slave
\r
177 , unsigned char* wbuf
\r
178 , unsigned char wlen
\r
179 , unsigned char* rbuf
\r
180 , unsigned char rlen
\r
182 static int i2c_bus_free_check( unsigned char maxretries );
\r
183 static void i2c_finalise( const char* text, unsigned long irqflags );
\r
187 /************************************************************************/
\r
188 /****************** AUXILIARIES *************************/
\r
189 /************************************************************************/
\r
191 /*#---------------------------------------------------------------------------
\r
193 *# FUNCTION NAME: i2c_open
\r
195 *# DESCRIPTION : opens an I2C device
\r
197 *# PARAMETERS : *inode: reference to inode
\r
198 *# *filp : reference to file pointer
\r
200 *#---------------------------------------------------------------------------
\r
202 static int i2c_open( struct inode *inode, struct file *filp )
\r
208 /*#---------------------------------------------------------------------------
\r
210 *# FUNCTION NAME: i2c_release
\r
212 *# DESCRIPTION : Releases the I2C device
\r
214 *# PARAMETERS : *inode: reference to inode
\r
215 *# *filp : reference to file pointer
\r
217 *#---------------------------------------------------------------------------
\r
219 static int i2c_release( struct inode *inode, struct file *filp )
\r
222 } /* i2c_release */
\r
225 /*#---------------------------------------------------------------------------
\r
227 *# FUNCTION NAME: i2c_ioctl
\r
229 *# DESCRIPTION : Main device API: ioctl's to write/read
\r
230 *# to/from i2c registers
\r
232 *# PARAMETERS : *inode: reference to inode
\r
233 *# *filp : reference to file pointer
\r
234 *# cmd : command to be executed during the ioctl call
\r
235 *# arg : pointer to a structure with the data???
\r
237 *# RETURN : result of the ioctl call
\r
239 *#---------------------------------------------------------------------------
\r
241 static int i2c_ioctl( struct inode *inode
\r
242 , struct file *file
\r
244 , unsigned long arg
\r
247 /* the acme ioctls */
\r
249 int RetVal = EI2CNOERRORS;
\r
251 if ( _IOC_TYPE( cmd ) != ETRAXI2C_IOCTYPE )
\r
253 return ( -EINVAL );
\r
256 switch ( _IOC_NR( cmd ) )
\r
259 /* write to an i2c slave */
\r
260 RetVal = i2c_writereg( I2C_ARGSLAVE( arg )
\r
261 , I2C_ARGREG( arg )
\r
262 , I2C_ARGVALUE( arg )
\r
267 RetVal = i2c_readreg( I2C_ARGSLAVE( arg ), I2C_ARGREG( arg ) );
\r
271 /* New functions added by GVC */
\r
273 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
\r
275 int RetryCntr = MAXRETRIES;
\r
279 RetVal = i2c_command( i2cdata.slave
\r
285 } while ( ( EI2CNOERRORS != RetVal )
\r
289 copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
\r
293 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
\r
295 int RetryCntr = MAXRETRIES;
\r
299 RetVal = i2c_command( i2cdata.slave
\r
305 } while ( ( EI2CNOERRORS != RetVal )
\r
311 case I2C_WRITEREAD:
\r
312 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
\r
314 int RetryCntr = MAXRETRIES;
\r
318 RetVal = i2c_command( i2cdata.slave
\r
324 } while ( ( EI2CNOERRORS != RetVal )
\r
328 copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
\r
336 return ( -RetVal );
\r
340 /*#---------------------------------------------------------------------------
\r
342 *# FUNCTION NAME: i2c_command
\r
344 *# DESCRIPTION : general routine to read/write bytes from an I2C device
\r
346 *# 'i2c_command()' sends wlen bytes to the I2c bus and receives
\r
347 *# rlen bytes from the I2c bus.
\r
348 *# The data to be send must be placed in wbuf[ 0 ] upto wbuf[ wlen - 1 ).
\r
349 *# The data to be received is assembled in rbuf[ 0 ] upto rbuf[ rlen - 1 ].
\r
351 *# If no data is to be sent or received, put appropriate buffer parameter
\r
352 *# to "NULL" and appropriate length parameter to "0".
\r
354 *# PARAMETERS : slave = slave address of the I2C device
\r
355 *# wbuf = address of first element of write buffer (wbuf)
\r
356 *# wlen = number of bytes to be written to slave
\r
357 *# rbuf = address of first element of read buffer (rbuf)
\r
358 *# rlen = number of bytes to be read from slave
\r
361 *# EI2CNOERRORS: I2C communication went fine
\r
362 *# EI2CBUSNFREE: I2C bus is not free
\r
363 *# EI2CWADDRESS: I2C write address failed
\r
364 *# EI2CRADDRESS: I2C read address failed
\r
365 *# EI2CSENDDATA: I2C send data failed
\r
366 *# EI2CRECVDATA: I2C receive data failed
\r
367 *# EI2CSTRTCOND: I2C start condition failed
\r
368 *# EI2CRSTACOND: I2C repeated start condition failed
\r
369 *# EI2CSTOPCOND: I2C stop condition failed
\r
370 *# EI2CNOSNDBYT: I2C no bytes to be sent
\r
371 *# EI2CNOSNDBUF: I2C no send buffer defined
\r
372 *# EI2CNORCVBYT: I2C no bytes to be received
\r
373 *# EI2CNORCVBUF: I2C no receive buffer defined
\r
374 *# EI2CNOACKNLD: I2C no acknowledge received
\r
377 *# First, the send part is completed.
\r
378 *# In the send routine, there is no stop generated. This is because maybe
\r
379 *# a repeated start condition must be generated.
\r
380 *# This happens when we want to receive some data from the I2c bus. If not,
\r
381 *# at the end of the general I2c loop the stopcondition is generated.
\r
382 *# If, on the contrary, there are a number of bytes to be received, a new
\r
383 *# startcondition is generated in the 'if' part of the main I2c routine,
\r
384 *# which controls the receiving part.
\r
385 *# Only when the receiving of data is finished, a final stopcondition is
\r
388 *#---------------------------------------------------------------------------
\r
390 static int i2c_command( unsigned char slave
\r
391 , unsigned char* wbuf
\r
392 , unsigned char wlen
\r
393 , unsigned char* rbuf
\r
394 , unsigned char rlen
\r
397 /* Check arguments and report error if relevant... */
\r
398 if ( ( wlen > 0 ) && ( wbuf == NULL ) )
\r
400 printk( KERN_DEBUG "I2C: EI2CNOSNDBUF\n" );
\r
401 return ( EI2CNOSNDBUF );
\r
403 else if ( ( wlen == 0 ) && ( wbuf != NULL ) )
\r
405 printk( KERN_DEBUG "I2C: EI2CNOSNDBYT\n" );
\r
406 return ( EI2CNOSNDBYT );
\r
408 else if ( ( rlen > 0 ) && ( rbuf == NULL ) )
\r
410 printk( KERN_DEBUG "I2C: EI2CNORCVBUF\n" );
\r
411 return ( EI2CNORCVBUF );
\r
413 else if ( ( rlen == 0 ) && ( rbuf != NULL ) )
\r
415 printk( KERN_DEBUG "I2C: EI2CNORCVBYT\n" );
\r
416 return ( EI2CNORCVBYT );
\r
418 else if ( EI2CBUSNFREE == i2c_bus_free_check( MAXBUSFREERETRIES ) )
\r
420 /* There's no need to try more, since we weren't even
\r
421 * able to start the I2C communication.
\r
422 * So, no IRQ flags are stored yet, no changes to any other
\r
423 * stuff like START, STOP, SENDBYTES...
\r
424 * Result, simply write down the error and return the correct error code.
\r
426 printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
\r
427 return ( EI2CBUSNFREE );
\r
431 /* Finally... We made it... */
\r
432 unsigned long irqflags = 0;
\r
434 /* we don't like to be interrupted */
\r
435 local_irq_save( irqflags );
\r
437 /* Check if there are bytes to be send,
\r
438 * or if you immediately want to receive data.
\r
442 /* start I2C communication */
\r
443 if ( EI2CNOERRORS != i2c_start() )
\r
445 return ( i2c_finalise( "I2C: EI2CSTRTCOND\n", irqflags )
\r
450 /* send slave address: xxxxxxx0B (last bit must be zero) */
\r
451 if ( EI2CNOERRORS != i2c_outbyte( slave & WRITEADDRESS_MASK ) )
\r
453 return ( i2c_finalise( "I2C: EI2CWADDRESS\n", irqflags )
\r
460 /* send register data */
\r
461 if ( EI2CNOERRORS != i2c_outbyte( *wbuf ) )
\r
463 return ( i2c_finalise( "I2C: EI2CSENDDATA\n", irqflags )
\r
475 * Receiving data from I2c_bus
\r
476 * If there are bytes to be received, a new start condition is
\r
477 * generated => Repeated Startcondition.
\r
478 * A final stopcondition is generated at the end of the main I2c
\r
484 * Generate start condition if wlen == 0
\r
485 * or repeated start condition if wlen != 0...
\r
487 if ( EI2CNOERRORS != i2c_start() )
\r
489 return ( i2c_finalise( ( ( 0 < wlen )
\r
490 ? "I2C: EI2CRSTACOND\n"
\r
491 : "I2C: EI2CSTRTCOND\n"
\r
495 , ( ( 0 < wlen ) ? EI2CRSTACOND : EI2CSTRTCOND )
\r
499 /* Send ReadAddress: xxxxxxx1B (last bit must be one) */
\r
500 if ( EI2CNOERRORS != i2c_outbyte( slave | READADDRESS_MASK ) )
\r
502 return ( i2c_finalise( "I2C: EI2CRADDRESS\n", irqflags )
\r
509 /* fetch register */
\r
510 *rbuf = i2c_inbyte();
\r
513 /* last received byte needs to be NACK-ed instead of ACK-ed */
\r
525 /* Generate final stop condition */
\r
526 if ( EI2CNOERRORS != i2c_stop() )
\r
528 return ( i2c_finalise( "I2C: EI2CSTOPCOND\n", irqflags )
\r
533 /* enable interrupt again */
\r
534 local_irq_restore( irqflags );
\r
537 return ( EI2CNOERRORS );
\r
538 } /* i2c_command */
\r
541 /*#---------------------------------------------------------------------------
\r
543 *# FUNCTION NAME: i2c_bus_free_check
\r
545 *# DESCRIPTION : checks if the I2C bus is free before starting
\r
546 *# an I2C communication
\r
548 *# PARAMETERS : maxretries, the number of times we will try to release
\r
551 *# RETURN : I2cStatus_I2cBusNotFreeError in case the bus is not free,
\r
552 *# I2cStatus_I2cNoError otherwise
\r
554 *#---------------------------------------------------------------------------
\r
556 static int i2c_bus_free_check( unsigned char maxretries )
\r
558 i2c_sda_dir_in(); /* Release SDA line */
\r
559 i2c_set_scl( SCL_HIGH ); /* put SCL line high */
\r
561 i2c_delay( WAITONEUS );
\r
563 while ( ( !i2c_sda_is_high() || !i2c_scl_is_high() )
\r
567 /* Try to release I2C bus by generating STOP conditions */
\r
571 if ( 0 == maxretries )
\r
573 printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
\r
574 return ( EI2CBUSNFREE );
\r
578 return ( EI2CNOERRORS );
\r
580 } /* i2c_bus_free_check */
\r
583 static void i2c_finalise( const char* errortxt
\r
584 , unsigned long irqflags
\r
587 printk( KERN_DEBUG "%s", errortxt );
\r
588 local_irq_restore( irqflags );
\r
589 /* The least we can do when things go terribly wrong,
\r
590 * is to try to release the bus.
\r
591 * If this fails, well, then I don't know
\r
592 * what I can do more for the moment...
\r
594 (void)i2c_bus_free_check( MAXBUSFREERETRIES );
\r
595 } /* i2c_finalise */
\r
598 static struct file_operations i2c_fops =
\r
600 .owner = THIS_MODULE
\r
601 , .ioctl = i2c_ioctl
\r
603 , .release = i2c_release
\r
607 /***********************************************************************/
\r
608 /************* EXTERNAL FUNCTION DEFINITION SECTION ********************/
\r
609 /***********************************************************************/
\r
611 /*#---------------------------------------------------------------------------
\r
613 *# FUNCTION NAME: i2c_init
\r
615 *# DESCRIPTION : initialises the I2C device driver
\r
619 *#---------------------------------------------------------------------------
\r
621 int __init i2c_init( void )
\r
623 static int res = 0;
\r
624 static int first = 1;
\r
633 /* Setup and enable the Port B I2C interface */
\r
635 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
\r
636 /* here, we're using the dedicated I2C pins of FoxBoard */
\r
637 if ( ( res = cris_request_io_interface( if_i2c, "I2C" ) ) )
\r
639 printk( KERN_CRIT "i2c_init: Failed to get IO interface\n" );
\r
643 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
\r
644 IO_STATE( R_PORT_PB_I2C, i2c_en, on ) |
\r
645 IO_FIELD( R_PORT_PB_I2C, i2c_d, 1 ) |
\r
646 IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, 1 ) |
\r
647 IO_STATE( R_PORT_PB_I2C, i2c_oe_, enable );
\r
649 port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir0 );
\r
650 port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir1 );
\r
652 *R_PORT_PB_DIR = ( port_pb_dir_shadow |=
\r
653 IO_STATE( R_PORT_PB_DIR, dir0, input ) |
\r
654 IO_STATE( R_PORT_PB_DIR, dir1, output ) );
\r
656 /* If everything goes fine, res = 0, meaning "if" fails =>
\r
657 * will do the "else" too and as such initialise the clock port...
\r
660 if ( ( res = cris_io_interface_allocate_pins( if_i2c
\r
662 , CONFIG_ETRAX_I2C_DATA_PORT
\r
663 , CONFIG_ETRAX_I2C_DATA_PORT
\r
668 printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n" );
\r
672 else if ( ( res = cris_io_interface_allocate_pins( if_i2c
\r
674 , CONFIG_ETRAX_I2C_CLK_PORT
\r
675 , CONFIG_ETRAX_I2C_CLK_PORT
\r
680 cris_io_interface_free_pins( if_i2c
\r
682 , CONFIG_ETRAX_I2C_DATA_PORT
\r
683 , CONFIG_ETRAX_I2C_DATA_PORT
\r
685 printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n" );
\r
693 /*#---------------------------------------------------------------------------
\r
695 *# FUNCTION NAME: i2c_register
\r
697 *# DESCRIPTION : this registers the i2c driver as a character device
\r
701 *#---------------------------------------------------------------------------
\r
703 static int __init i2c_register( void )
\r
707 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
709 struct cdev *my_i2cdev = NULL;
\r
721 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
722 res = alloc_chrdev_region( &devt, 0, 1, i2c_name );
\r
726 printk( KERN_DEBUG "I2C: EI2CNOMNUMBR\n" );
\r
730 my_i2cdev = cdev_alloc();
\r
731 my_i2cdev->ops = &i2c_fops;
\r
732 my_i2cdev->owner = THIS_MODULE;
\r
734 /* make device "alive" */
\r
735 res = cdev_add( my_i2cdev, devt, 1 );
\r
739 printk( KERN_DEBUG "I2C: EI2CDADDFAIL\n" );
\r
744 res = register_chrdev( I2C_MAJOR, i2c_name, &i2c_fops );
\r
748 printk( KERN_ERR "i2c: couldn't get a major number.\n" );
\r
755 printk( KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n" );
\r
758 printk( KERN_INFO " ==> Improvements done by Geert Vancompernolle - December 2006\n" );
\r
760 #ifdef DYNAMIC_MAJOR_I2CDEV_NUMBER_ALLOC
\r
761 printk( KERN_INFO "I2C Major: %d / I2C Name: %s\n", MAJOR( devt ), i2c_name );
\r
764 printk( KERN_INFO "I2C Major: %d / I2C Name: %s\n", I2C_MAJOR, i2c_name );
\r
770 } /* i2c_register */
\r
773 /*#---------------------------------------------------------------------------
\r
775 *# FUNCTION NAME: i2c_start
\r
777 *# DESCRIPTION : generate i2c start condition
\r
779 *# PARAMETERS : none
\r
781 *# RETURN : EI2CNOERRORS if OK, EI2CSTRTCOND otherwise
\r
783 *#---------------------------------------------------------------------------
\r
785 int i2c_start( void )
\r
787 /* Set SCL=1, SDA=1 */
\r
789 i2c_set_sda( SDA_HIGH );
\r
790 i2c_delay( WAITONEUS );
\r
791 i2c_set_scl( SCL_HIGH );
\r
792 i2c_delay( WAITONEUS );
\r
794 /* Set SCL=1, SDA=0 */
\r
795 i2c_set_sda( SDA_LOW );
\r
796 i2c_delay( THDSTA );
\r
798 /* Set SCL=0, SDA=0 */
\r
799 i2c_set_scl( SCL_LOW );
\r
800 /* We can take 1 us less than defined in spec (5 us), since the next action
\r
801 * will be to set the dataline high or low and this action is 1 us
\r
802 * before the clock is put high, so that makes our 5 us.
\r
804 i2c_delay( TLOW - WAITONEUS );
\r
806 if ( i2c_sda_is_high() || i2c_scl_is_high() )
\r
808 printk( KERN_DEBUG "I2C: EI2CSTRTCOND\n" );
\r
809 return ( EI2CSTRTCOND );
\r
812 return ( EI2CNOERRORS );
\r
816 /*#---------------------------------------------------------------------------
\r
818 *# FUNCTION NAME: i2c_stop
\r
820 *# DESCRIPTION : generate i2c stop condition
\r
822 *# PARAMETERS : none
\r
826 *#---------------------------------------------------------------------------
\r
828 int i2c_stop( void )
\r
832 /* Set SCL=0, SDA=0 */
\r
833 /* Don't change order, otherwise you might generate a start condition! */
\r
834 i2c_set_scl( SCL_LOW );
\r
835 i2c_delay( WAITONEUS );
\r
836 i2c_set_sda( SDA_LOW );
\r
837 i2c_delay( WAITONEUS );
\r
839 /* Set SCL=1, SDA=0 */
\r
840 i2c_set_scl( SCL_HIGH );
\r
841 i2c_delay( TSUSTO );
\r
843 /* Set SCL=1, SDA=1 */
\r
844 i2c_set_sda( SDA_HIGH );
\r
849 if ( !i2c_sda_is_high() || !i2c_scl_is_high() )
\r
851 printk( KERN_DEBUG "I2C: EI2CSTOPCOND\n" );
\r
852 return ( EI2CSTOPCOND );
\r
855 return ( EI2CNOERRORS );
\r
859 /*#---------------------------------------------------------------------------
\r
861 *# FUNCTION NAME: i2c_outbyte
\r
863 *# DESCRIPTION : write a byte to the i2c interface
\r
865 *# PARAMETERS : x: byte to be sent on the I2C bus
\r
869 *#---------------------------------------------------------------------------
\r
871 int i2c_outbyte( unsigned char x )
\r
877 for ( i = 0; i < 8; i++ )
\r
881 i2c_set_sda( SDA_HIGH );
\r
885 i2c_set_sda( SDA_LOW );
\r
888 i2c_delay( TSUDAT );
\r
889 i2c_set_scl( SCL_HIGH );
\r
890 i2c_delay( THIGH );
\r
891 i2c_set_scl( SCL_LOW );
\r
892 i2c_delay( TSUDAT );
\r
893 i2c_set_sda( SDA_LOW );
\r
894 /* There should be only 5 us between falling edge and new rising
\r
895 * edge of clock pulse.
\r
896 * Since we spend already 1 us since clock edge was low, there are
\r
897 * only ( TLOW - TSUDAT ) us left.
\r
898 * Next to this, since the data line will be set up 1 us before the
\r
899 * clock line is set up, we can reduce the delay with another us.
\r
901 i2c_delay( TLOW - TSUDAT - WAITONEUS );
\r
908 if ( !i2c_getack() )
\r
910 printk( KERN_DEBUG "I2C: EI2CNOACKNLD\n" );
\r
911 return( EI2CNOACKNLD );
\r
914 return ( EI2CNOERRORS );
\r
915 } /* i2c_outbyte */
\r
918 /*#---------------------------------------------------------------------------
\r
920 *# FUNCTION NAME: i2c_inbyte
\r
922 *# DESCRIPTION : read a byte from the i2c interface
\r
924 *# PARAMETERS : none
\r
926 *# RETURN : returns the byte read from the I2C device
\r
928 *#---------------------------------------------------------------------------
\r
930 unsigned char i2c_inbyte( void )
\r
932 unsigned char aBitByte = 0;
\r
933 unsigned char Mask = 0x80; /* !!! ATTENTION: do NOT use 'char', otherwise shifting is wrong!!! */
\r
934 /* Must be UNSIGNED, not SIGNED! */
\r
937 /* Switch off I2C to get bit */
\r
941 while ( Mask != 0 )
\r
943 i2c_set_scl( SCL_HIGH );
\r
944 i2c_delay( THIGH );
\r
946 if ( i2c_sda_is_high() )
\r
951 i2c_set_scl( SCL_LOW );
\r
959 * we leave the clock low, getbyte is usually followed
\r
960 * by sendack/nack, they assume the clock to be low
\r
962 return ( aBitByte );
\r
966 /*#---------------------------------------------------------------------------
\r
968 *# FUNCTION NAME: i2c_getack
\r
970 *# DESCRIPTION : checks if ack was received from ic2
\r
972 *# PARAMETERS : none
\r
974 *# RETURN : returns the ack state of the I2C device
\r
976 *#---------------------------------------------------------------------------
\r
978 int i2c_getack( void )
\r
982 /* generate ACK clock pulse */
\r
983 i2c_set_scl( SCL_HIGH );
\r
985 /* switch off I2C */
\r
988 /* now wait for ack */
\r
989 i2c_delay( THIGH );
\r
990 /* check for ack: if SDA is high, then NACK, else ACK */
\r
991 if ( i2c_sda_is_high() )
\r
1000 /* end clock pulse */
\r
1002 i2c_set_scl( SCL_LOW );
\r
1003 i2c_sda_dir_out();
\r
1004 i2c_set_sda( SDA_LOW );
\r
1006 /* Since we "lost" already THDDAT time, we can subtract it here... */
\r
1007 i2c_delay( TLOW - THDDAT );
\r
1010 } /* i2c_getack */
\r
1013 /*#---------------------------------------------------------------------------
\r
1015 *# FUNCTION NAME: i2c_sendack
\r
1017 *# DESCRIPTION : sends ACK on received data
\r
1019 *# PARAMETERS : none
\r
1023 *#---------------------------------------------------------------------------
\r
1025 void i2c_sendack( void )
\r
1027 /* enable output */
\r
1028 /* Clock has been set to TLOW already at end of i2c_inbyte()
\r
1029 * and i2c_outbyte(), so no need to do it again.
\r
1031 i2c_sda_dir_out();
\r
1032 /* set ack pulse low */
\r
1033 i2c_set_sda( SDA_LOW );
\r
1034 /* generate clock pulse */
\r
1035 i2c_delay( TSUDAT );
\r
1036 i2c_set_scl( SCL_HIGH );
\r
1037 i2c_delay( THIGH );
\r
1038 i2c_set_scl( SCL_LOW );
\r
1039 i2c_delay( THDDAT );
\r
1040 /* reset data out */
\r
1041 i2c_set_sda( SDA_HIGH );
\r
1042 /* Subtract time spend already when waited to put SDA high */
\r
1043 i2c_delay( TLOW - THDDAT );
\r
1045 /* release the SDA line */
\r
1047 } /* i2c_sendack */
\r
1050 /*#---------------------------------------------------------------------------
\r
1052 *# FUNCTION NAME: i2c_sendnack
\r
1054 *# DESCRIPTION : sends NACK on received data
\r
1056 *# PARAMETERS : none
\r
1060 *#---------------------------------------------------------------------------
\r
1062 void i2c_sendnack( void )
\r
1064 /* make sure the SDA line is set high prior to activation of the output.
\r
1065 * this way, you avoid an unnecessary peak to ground when a NACK has to
\r
1068 /* set data high */
\r
1069 i2c_set_sda( SDA_HIGH );
\r
1070 /* enable output */
\r
1071 i2c_sda_dir_out();
\r
1073 /* generate clock pulse */
\r
1074 i2c_delay( TSUDAT );
\r
1075 i2c_set_scl( SCL_HIGH );
\r
1076 i2c_delay( THIGH );
\r
1077 i2c_set_scl( SCL_LOW );
\r
1078 i2c_delay( TSUDAT );
\r
1079 i2c_set_sda( SDA_LOW );
\r
1080 i2c_delay( TLOW - TSUDAT );
\r
1082 /* There's no need to change the direction of SDA to "in" again,
\r
1083 * since a NACK is always followed by a stop condition.
\r
1084 * A STOP condition will put the direction of SDA back to "out"
\r
1085 * resulting in a useless SDA "dip" on the line...
\r
1087 /* i2c_sda_dir_in(); */
\r
1088 } /* i2c_sendnack */
\r
1091 /*#---------------------------------------------------------------------------
\r
1093 *# FUNCTION NAME: i2c_writereg
\r
1095 *# DESCRIPTION : writes a value to a register of an I2C device
\r
1097 *# PARAMETERS : theSlave = slave address of the I2C device
\r
1098 *# theReg = register of the I2C device that needs to be written
\r
1099 *# theValue = value to be written to the register
\r
1101 *# RETURN : returns OR-ed result of the write action:
\r
1103 *# 1 = Slave_NoAck
\r
1107 *#---------------------------------------------------------------------------
\r
1109 int i2c_writereg( unsigned char theSlave
\r
1110 , unsigned char theReg
\r
1111 , unsigned char theValue
\r
1114 int error, cntr = 3;
\r
1115 unsigned long flags;
\r
1117 spin_lock( &i2c_lock );
\r
1122 /* we don't like to be interrupted */
\r
1123 local_irq_save( flags );
\r
1126 /* send slave address */
\r
1127 if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
\r
1132 /* now select register */
\r
1133 if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
\r
1138 /* send register register data */
\r
1139 if ( EI2CNOACKNLD == i2c_outbyte( theValue ) )
\r
1144 /* end byte stream */
\r
1146 /* enable interrupt again */
\r
1147 local_irq_restore( flags );
\r
1149 } while ( error && cntr-- );
\r
1151 i2c_delay( TLOW );
\r
1153 spin_unlock( &i2c_lock );
\r
1155 return ( -error );
\r
1156 } /* i2c_writereg */
\r
1159 /*#---------------------------------------------------------------------------
\r
1161 *# FUNCTION NAME: i2c_readreg
\r
1163 *# DESCRIPTION : reads the value from a certain register of an I2C device.
\r
1164 *# Function first writes the register that it wants to read
\r
1167 *# PARAMETERS : theSlave = slave address of the I2C device
\r
1168 *# theReg = register of the I2C device that needs to be written
\r
1170 *# RETURN : returns OR-ed result of the write action:
\r
1172 *# 1 = Slave_NoAck
\r
1176 *#---------------------------------------------------------------------------
\r
1178 unsigned char i2c_readreg( unsigned char theSlave
\r
1179 , unsigned char theReg
\r
1182 unsigned char b = 0;
\r
1183 int error, cntr = 3;
\r
1184 unsigned long flags;
\r
1186 spin_lock( &i2c_lock );
\r
1192 /* we don't like to be interrupted */
\r
1193 local_irq_save( flags );
\r
1195 /* generate start condition */
\r
1198 /* send slave address */
\r
1199 if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
\r
1204 /* now select register */
\r
1205 i2c_sda_dir_out();
\r
1207 if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
\r
1212 /* repeat start condition */
\r
1213 i2c_delay( TLOW );
\r
1216 /* send slave address */
\r
1217 if ( EI2CNOACKNLD == i2c_outbyte( theSlave | 0x01 ) )
\r
1222 /* fetch register */
\r
1225 * last received byte needs to be nacked
\r
1226 * instead of acked
\r
1230 /* end sequence */
\r
1233 /* enable interrupt again */
\r
1234 local_irq_restore( flags );
\r
1236 } while ( error && cntr-- );
\r
1238 spin_unlock( &i2c_lock );
\r
1241 } /* i2c_readreg */
\r
1244 /*#---------------------------------------------------------------------------
\r
1246 *# FUNCTION NAME: i2c_read
\r
1254 *#---------------------------------------------------------------------------
\r
1256 int i2c_read( unsigned char slave, unsigned char* rbuf, unsigned char rlen )
\r
1258 return ( i2c_command( slave, NULL, 0, rbuf, rlen ) );
\r
1262 /*#---------------------------------------------------------------------------
\r
1264 *# FUNCTION NAME: i2c_write
\r
1272 *#---------------------------------------------------------------------------
\r
1274 int i2c_write( unsigned char slave, unsigned char* wbuf, unsigned char wlen )
\r
1276 return ( i2c_command( slave, wbuf, wlen, NULL, 0 ) );
\r
1280 /*#---------------------------------------------------------------------------
\r
1282 *# FUNCTION NAME: i2c_writeread
\r
1290 *#---------------------------------------------------------------------------
\r
1292 int i2c_writeread( unsigned char slave
\r
1293 , unsigned char* wbuf
\r
1294 , unsigned char wlen
\r
1295 , unsigned char* rbuf
\r
1296 , unsigned char rlen
\r
1299 return ( i2c_command( slave, wbuf, wlen, rbuf, rlen ) );
\r
1300 } /* i2c_writeread */
\r
1303 /*#---------------------------------------------------------------------------
\r
1305 *# FUNCTION NAME: module_init
\r
1307 *# DESCRIPTION : this makes sure that i2c_register is called during boot
\r
1311 *#---------------------------------------------------------------------------
\r
1313 module_init( i2c_register );
\r
1315 /****************** END OF FILE i2c.c ********************************/
\r