1Usually, i2c devices are controlled by a kernel driver. But it is also 2possible to access all devices on an adapter from userspace, through 3the /dev interface. You need to load module i2c-dev for this. 4 5Each registered i2c adapter gets a number, counting from 0. You can 6examine /proc/bus/i2c to see what number corresponds to which adapter. 7I2C device files are character device files with major device number 89 8and a minor device number corresponding to the number assigned as 9explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 10i2c-10, ...). All 256 minor device numbers are reserved for i2c. 11 12 13C example 14========= 15 16So let's say you want to access an i2c adapter from a C program. The 17first thing to do is `#include <linux/i2c.h>" and "#include <linux/i2c-dev.h>. 18Yes, I know, you should never include kernel header files, but until glibc 19knows about i2c, there is not much choice. 20 21Now, you have to decide which adapter you want to access. You should 22inspect /proc/bus/i2c to decide this. Adapter numbers are assigned 23somewhat dynamically, so you can not even assume /dev/i2c-0 is the 24first adapter. 25 26Next thing, open the device file, as follows: 27 int file; 28 int adapter_nr = 2; /* probably dynamically determined */ 29 char filename[20]; 30 31 sprintf(filename,"/dev/i2c-%d",adapter_nr); 32 if ((file = open(filename,O_RDWR)) < 0) { 33 /* ERROR HANDLING; you can check errno to see what went wrong */ 34 exit(1); 35 } 36 37When you have opened the device, you must specify with what device 38address you want to communicate: 39 int addr = 0x40; /* The I2C address */ 40 if (ioctl(file,I2C_SLAVE,addr) < 0) { 41 /* ERROR HANDLING; you can check errno to see what went wrong */ 42 exit(1); 43 } 44 45Well, you are all set up now. You can now use SMBus commands or plain 46I2C to communicate with your device. SMBus commands are preferred if 47the device supports them. Both are illustrated below. 48 __u8 register = 0x10; /* Device register to access */ 49 __s32 res; 50 char buf[10]; 51 /* Using SMBus commands */ 52 res = i2c_smbus_read_word_data(file,register); 53 if (res < 0) { 54 /* ERROR HANDLING: i2c transaction failed */ 55 } else { 56 /* res contains the read word */ 57 } 58 /* Using I2C Write, equivalent of 59 i2c_smbus_write_word_data(file,register,0x6543) */ 60 buf[0] = register; 61 buf[1] = 0x43; 62 buf[2] = 0x65; 63 if ( write(file,buf,3) != 3) { 64 /* ERROR HANDLING: i2c transaction failed */ 65 } 66 /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ 67 if (read(file,buf,1) != 1) { 68 /* ERROR HANDLING: i2c transaction failed */ 69 } else { 70 /* buf[0] contains the read byte */ 71 } 72 73IMPORTANT: because of the use of inline functions, you *have* to use 74'-O' or some variation when you compile your program! 75 76 77Full interface description 78========================== 79 80The following IOCTLs are defined and fully supported 81(see also i2c-dev.h and i2c.h): 82 83ioctl(file,I2C_SLAVE,long addr) 84 Change slave address. The address is passed in the 7 lower bits of the 85 argument (except for 10 bit addresses, passed in the 10 lower bits in this 86 case). 87 88ioctl(file,I2C_TENBIT,long select) 89 Selects ten bit addresses if select not equals 0, selects normal 7 bit 90 addresses if select equals 0. Default 0. 91 92ioctl(file,I2C_FUNCS,unsigned long *funcs) 93 Gets the adapter functionality and puts it in *funcs. 94 95ioctl(file,I2C_RDWR,struct i2c_rdwr_ioctl_data *msgset) 96 97 Do combined read/write transaction without stop in between. 98 The argument is a pointer to a struct i2c_rdwr_ioctl_data { 99 100 struct i2c_msg *msgs; /* ptr to array of simple messages */ 101 int nmsgs; /* number of messages to exchange */ 102 } 103 104 The msgs[] themselves contain further pointers into data buffers. 105 The function will write or read data to or from that buffers depending 106 on whether the I2C_M_RD flag is set in a particular message or not. 107 The slave address and whether to use ten bit address mode has to be 108 set in each message, overriding the values set with the above ioctl's. 109 110 111Other values are NOT supported at this moment, except for I2C_SMBUS, 112which you should never directly call; instead, use the access functions 113below. 114 115You can do plain i2c transactions by using read(2) and write(2) calls. 116You do not need to pass the address byte; instead, set it through 117ioctl I2C_SLAVE before you try to access the device. 118 119You can do SMBus level transactions (see documentation file smbus-protocol 120for details) through the following functions: 121 __s32 i2c_smbus_write_quick(int file, __u8 value); 122 __s32 i2c_smbus_read_byte(int file); 123 __s32 i2c_smbus_write_byte(int file, __u8 value); 124 __s32 i2c_smbus_read_byte_data(int file, __u8 command); 125 __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); 126 __s32 i2c_smbus_read_word_data(int file, __u8 command); 127 __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); 128 __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); 129 __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); 130 __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, 131 __u8 *values); 132All these transactions return -1 on failure; you can read errno to see 133what happened. The 'write' transactions return 0 on success; the 134'read' transactions return the read value, except for read_block, which 135returns the number of values read. The block buffers need not be longer 136than 32 bytes. 137 138The above functions are all macros, that resolve to calls to the 139i2c_smbus_access function, that on its turn calls a specific ioctl 140with the data in a specific format. Read the source code if you 141want to know what happens behind the screens. 142