1*4882a593Smuzhiyun============================================ 2*4882a593SmuzhiyunImplementing I2C device drivers in userspace 3*4882a593Smuzhiyun============================================ 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunUsually, I2C devices are controlled by a kernel driver. But it is also 6*4882a593Smuzhiyunpossible to access all devices on an adapter from userspace, through 7*4882a593Smuzhiyunthe /dev interface. You need to load module i2c-dev for this. 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunEach registered I2C adapter gets a number, counting from 0. You can 10*4882a593Smuzhiyunexamine /sys/class/i2c-dev/ to see what number corresponds to which adapter. 11*4882a593SmuzhiyunAlternatively, you can run "i2cdetect -l" to obtain a formatted list of all 12*4882a593SmuzhiyunI2C adapters present on your system at a given time. i2cdetect is part of 13*4882a593Smuzhiyunthe i2c-tools package. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunI2C device files are character device files with major device number 89 16*4882a593Smuzhiyunand a minor device number corresponding to the number assigned as 17*4882a593Smuzhiyunexplained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 18*4882a593Smuzhiyuni2c-10, ...). All 256 minor device numbers are reserved for I2C. 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunC example 22*4882a593Smuzhiyun========= 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunSo let's say you want to access an I2C adapter from a C program. 25*4882a593SmuzhiyunFirst, you need to include these two headers:: 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun #include <linux/i2c-dev.h> 28*4882a593Smuzhiyun #include <i2c/smbus.h> 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunNow, you have to decide which adapter you want to access. You should 31*4882a593Smuzhiyuninspect /sys/class/i2c-dev/ or run "i2cdetect -l" to decide this. 32*4882a593SmuzhiyunAdapter numbers are assigned somewhat dynamically, so you can not 33*4882a593Smuzhiyunassume much about them. They can even change from one boot to the next. 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunNext thing, open the device file, as follows:: 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun int file; 38*4882a593Smuzhiyun int adapter_nr = 2; /* probably dynamically determined */ 39*4882a593Smuzhiyun char filename[20]; 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun snprintf(filename, 19, "/dev/i2c-%d", adapter_nr); 42*4882a593Smuzhiyun file = open(filename, O_RDWR); 43*4882a593Smuzhiyun if (file < 0) { 44*4882a593Smuzhiyun /* ERROR HANDLING; you can check errno to see what went wrong */ 45*4882a593Smuzhiyun exit(1); 46*4882a593Smuzhiyun } 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunWhen you have opened the device, you must specify with what device 49*4882a593Smuzhiyunaddress you want to communicate:: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun int addr = 0x40; /* The I2C address */ 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun if (ioctl(file, I2C_SLAVE, addr) < 0) { 54*4882a593Smuzhiyun /* ERROR HANDLING; you can check errno to see what went wrong */ 55*4882a593Smuzhiyun exit(1); 56*4882a593Smuzhiyun } 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunWell, you are all set up now. You can now use SMBus commands or plain 59*4882a593SmuzhiyunI2C to communicate with your device. SMBus commands are preferred if 60*4882a593Smuzhiyunthe device supports them. Both are illustrated below:: 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun __u8 reg = 0x10; /* Device register to access */ 63*4882a593Smuzhiyun __s32 res; 64*4882a593Smuzhiyun char buf[10]; 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun /* Using SMBus commands */ 67*4882a593Smuzhiyun res = i2c_smbus_read_word_data(file, reg); 68*4882a593Smuzhiyun if (res < 0) { 69*4882a593Smuzhiyun /* ERROR HANDLING: I2C transaction failed */ 70*4882a593Smuzhiyun } else { 71*4882a593Smuzhiyun /* res contains the read word */ 72*4882a593Smuzhiyun } 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun /* 75*4882a593Smuzhiyun * Using I2C Write, equivalent of 76*4882a593Smuzhiyun * i2c_smbus_write_word_data(file, reg, 0x6543) 77*4882a593Smuzhiyun */ 78*4882a593Smuzhiyun buf[0] = reg; 79*4882a593Smuzhiyun buf[1] = 0x43; 80*4882a593Smuzhiyun buf[2] = 0x65; 81*4882a593Smuzhiyun if (write(file, buf, 3) != 3) { 82*4882a593Smuzhiyun /* ERROR HANDLING: I2C transaction failed */ 83*4882a593Smuzhiyun } 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */ 86*4882a593Smuzhiyun if (read(file, buf, 1) != 1) { 87*4882a593Smuzhiyun /* ERROR HANDLING: I2C transaction failed */ 88*4882a593Smuzhiyun } else { 89*4882a593Smuzhiyun /* buf[0] contains the read byte */ 90*4882a593Smuzhiyun } 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunNote that only a subset of the I2C and SMBus protocols can be achieved by 93*4882a593Smuzhiyunthe means of read() and write() calls. In particular, so-called combined 94*4882a593Smuzhiyuntransactions (mixing read and write messages in the same transaction) 95*4882a593Smuzhiyunaren't supported. For this reason, this interface is almost never used by 96*4882a593Smuzhiyunuser-space programs. 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunIMPORTANT: because of the use of inline functions, you *have* to use 99*4882a593Smuzhiyun'-O' or some variation when you compile your program! 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunFull interface description 103*4882a593Smuzhiyun========================== 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunThe following IOCTLs are defined: 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun``ioctl(file, I2C_SLAVE, long addr)`` 108*4882a593Smuzhiyun Change slave address. The address is passed in the 7 lower bits of the 109*4882a593Smuzhiyun argument (except for 10 bit addresses, passed in the 10 lower bits in this 110*4882a593Smuzhiyun case). 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun``ioctl(file, I2C_TENBIT, long select)`` 113*4882a593Smuzhiyun Selects ten bit addresses if select not equals 0, selects normal 7 bit 114*4882a593Smuzhiyun addresses if select equals 0. Default 0. This request is only valid 115*4882a593Smuzhiyun if the adapter has I2C_FUNC_10BIT_ADDR. 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun``ioctl(file, I2C_PEC, long select)`` 118*4882a593Smuzhiyun Selects SMBus PEC (packet error checking) generation and verification 119*4882a593Smuzhiyun if select not equals 0, disables if select equals 0. Default 0. 120*4882a593Smuzhiyun Used only for SMBus transactions. This request only has an effect if the 121*4882a593Smuzhiyun the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just 122*4882a593Smuzhiyun doesn't have any effect. 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun``ioctl(file, I2C_FUNCS, unsigned long *funcs)`` 125*4882a593Smuzhiyun Gets the adapter functionality and puts it in ``*funcs``. 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun``ioctl(file, I2C_RDWR, struct i2c_rdwr_ioctl_data *msgset)`` 128*4882a593Smuzhiyun Do combined read/write transaction without stop in between. 129*4882a593Smuzhiyun Only valid if the adapter has I2C_FUNC_I2C. The argument is 130*4882a593Smuzhiyun a pointer to a:: 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun struct i2c_rdwr_ioctl_data { 133*4882a593Smuzhiyun struct i2c_msg *msgs; /* ptr to array of simple messages */ 134*4882a593Smuzhiyun int nmsgs; /* number of messages to exchange */ 135*4882a593Smuzhiyun } 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun The msgs[] themselves contain further pointers into data buffers. 138*4882a593Smuzhiyun The function will write or read data to or from that buffers depending 139*4882a593Smuzhiyun on whether the I2C_M_RD flag is set in a particular message or not. 140*4882a593Smuzhiyun The slave address and whether to use ten bit address mode has to be 141*4882a593Smuzhiyun set in each message, overriding the values set with the above ioctl's. 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun``ioctl(file, I2C_SMBUS, struct i2c_smbus_ioctl_data *args)`` 144*4882a593Smuzhiyun If possible, use the provided ``i2c_smbus_*`` methods described below instead 145*4882a593Smuzhiyun of issuing direct ioctls. 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunYou can do plain I2C transactions by using read(2) and write(2) calls. 148*4882a593SmuzhiyunYou do not need to pass the address byte; instead, set it through 149*4882a593Smuzhiyunioctl I2C_SLAVE before you try to access the device. 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunYou can do SMBus level transactions (see documentation file smbus-protocol 152*4882a593Smuzhiyunfor details) through the following functions:: 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun __s32 i2c_smbus_write_quick(int file, __u8 value); 155*4882a593Smuzhiyun __s32 i2c_smbus_read_byte(int file); 156*4882a593Smuzhiyun __s32 i2c_smbus_write_byte(int file, __u8 value); 157*4882a593Smuzhiyun __s32 i2c_smbus_read_byte_data(int file, __u8 command); 158*4882a593Smuzhiyun __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); 159*4882a593Smuzhiyun __s32 i2c_smbus_read_word_data(int file, __u8 command); 160*4882a593Smuzhiyun __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); 161*4882a593Smuzhiyun __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); 162*4882a593Smuzhiyun __s32 i2c_smbus_block_process_call(int file, __u8 command, __u8 length, 163*4882a593Smuzhiyun __u8 *values); 164*4882a593Smuzhiyun __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); 165*4882a593Smuzhiyun __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, 166*4882a593Smuzhiyun __u8 *values); 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunAll these transactions return -1 on failure; you can read errno to see 169*4882a593Smuzhiyunwhat happened. The 'write' transactions return 0 on success; the 170*4882a593Smuzhiyun'read' transactions return the read value, except for read_block, which 171*4882a593Smuzhiyunreturns the number of values read. The block buffers need not be longer 172*4882a593Smuzhiyunthan 32 bytes. 173*4882a593Smuzhiyun 174*4882a593SmuzhiyunThe above functions are made available by linking against the libi2c library, 175*4882a593Smuzhiyunwhich is provided by the i2c-tools project. See: 176*4882a593Smuzhiyunhttps://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/. 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun 179*4882a593SmuzhiyunImplementation details 180*4882a593Smuzhiyun====================== 181*4882a593Smuzhiyun 182*4882a593SmuzhiyunFor the interested, here's the code flow which happens inside the kernel 183*4882a593Smuzhiyunwhen you use the /dev interface to I2C: 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun1) Your program opens /dev/i2c-N and calls ioctl() on it, as described in 186*4882a593Smuzhiyun section "C example" above. 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun2) These open() and ioctl() calls are handled by the i2c-dev kernel 189*4882a593Smuzhiyun driver: see i2c-dev.c:i2cdev_open() and i2c-dev.c:i2cdev_ioctl(), 190*4882a593Smuzhiyun respectively. You can think of i2c-dev as a generic I2C chip driver 191*4882a593Smuzhiyun that can be programmed from user-space. 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun3) Some ioctl() calls are for administrative tasks and are handled by 194*4882a593Smuzhiyun i2c-dev directly. Examples include I2C_SLAVE (set the address of the 195*4882a593Smuzhiyun device you want to access) and I2C_PEC (enable or disable SMBus error 196*4882a593Smuzhiyun checking on future transactions.) 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun4) Other ioctl() calls are converted to in-kernel function calls by 199*4882a593Smuzhiyun i2c-dev. Examples include I2C_FUNCS, which queries the I2C adapter 200*4882a593Smuzhiyun functionality using i2c.h:i2c_get_functionality(), and I2C_SMBUS, which 201*4882a593Smuzhiyun performs an SMBus transaction using i2c-core-smbus.c:i2c_smbus_xfer(). 202*4882a593Smuzhiyun 203*4882a593Smuzhiyun The i2c-dev driver is responsible for checking all the parameters that 204*4882a593Smuzhiyun come from user-space for validity. After this point, there is no 205*4882a593Smuzhiyun difference between these calls that came from user-space through i2c-dev 206*4882a593Smuzhiyun and calls that would have been performed by kernel I2C chip drivers 207*4882a593Smuzhiyun directly. This means that I2C bus drivers don't need to implement 208*4882a593Smuzhiyun anything special to support access from user-space. 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun5) These i2c.h functions are wrappers to the actual implementation of 211*4882a593Smuzhiyun your I2C bus driver. Each adapter must declare callback functions 212*4882a593Smuzhiyun implementing these standard calls. i2c.h:i2c_get_functionality() calls 213*4882a593Smuzhiyun i2c_adapter.algo->functionality(), while 214*4882a593Smuzhiyun i2c-core-smbus.c:i2c_smbus_xfer() calls either 215*4882a593Smuzhiyun adapter.algo->smbus_xfer() if it is implemented, or if not, 216*4882a593Smuzhiyun i2c-core-smbus.c:i2c_smbus_xfer_emulated() which in turn calls 217*4882a593Smuzhiyun i2c_adapter.algo->master_xfer(). 218*4882a593Smuzhiyun 219*4882a593SmuzhiyunAfter your I2C bus driver has processed these requests, execution runs 220*4882a593Smuzhiyunup the call chain, with almost no processing done, except by i2c-dev to 221*4882a593Smuzhiyunpackage the returned data, if any, in suitable format for the ioctl. 222