1*4882a593Smuzhiyun======================= 2*4882a593SmuzhiyunI2C/SMBus Functionality 3*4882a593Smuzhiyun======================= 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunINTRODUCTION 6*4882a593Smuzhiyun------------ 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunBecause not every I2C or SMBus adapter implements everything in the 9*4882a593SmuzhiyunI2C specifications, a client can not trust that everything it needs 10*4882a593Smuzhiyunis implemented when it is given the option to attach to an adapter: 11*4882a593Smuzhiyunthe client needs some way to check whether an adapter has the needed 12*4882a593Smuzhiyunfunctionality. 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunFUNCTIONALITY CONSTANTS 16*4882a593Smuzhiyun----------------------- 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunFor the most up-to-date list of functionality constants, please check 19*4882a593Smuzhiyun<uapi/linux/i2c.h>! 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun =============================== ============================================== 22*4882a593Smuzhiyun I2C_FUNC_I2C Plain i2c-level commands (Pure SMBus 23*4882a593Smuzhiyun adapters typically can not do these) 24*4882a593Smuzhiyun I2C_FUNC_10BIT_ADDR Handles the 10-bit address extensions 25*4882a593Smuzhiyun I2C_FUNC_PROTOCOL_MANGLING Knows about the I2C_M_IGNORE_NAK, 26*4882a593Smuzhiyun I2C_M_REV_DIR_ADDR and I2C_M_NO_RD_ACK 27*4882a593Smuzhiyun flags (which modify the I2C protocol!) 28*4882a593Smuzhiyun I2C_FUNC_NOSTART Can skip repeated start sequence 29*4882a593Smuzhiyun I2C_FUNC_SMBUS_QUICK Handles the SMBus write_quick command 30*4882a593Smuzhiyun I2C_FUNC_SMBUS_READ_BYTE Handles the SMBus read_byte command 31*4882a593Smuzhiyun I2C_FUNC_SMBUS_WRITE_BYTE Handles the SMBus write_byte command 32*4882a593Smuzhiyun I2C_FUNC_SMBUS_READ_BYTE_DATA Handles the SMBus read_byte_data command 33*4882a593Smuzhiyun I2C_FUNC_SMBUS_WRITE_BYTE_DATA Handles the SMBus write_byte_data command 34*4882a593Smuzhiyun I2C_FUNC_SMBUS_READ_WORD_DATA Handles the SMBus read_word_data command 35*4882a593Smuzhiyun I2C_FUNC_SMBUS_WRITE_WORD_DATA Handles the SMBus write_byte_data command 36*4882a593Smuzhiyun I2C_FUNC_SMBUS_PROC_CALL Handles the SMBus process_call command 37*4882a593Smuzhiyun I2C_FUNC_SMBUS_READ_BLOCK_DATA Handles the SMBus read_block_data command 38*4882a593Smuzhiyun I2C_FUNC_SMBUS_WRITE_BLOCK_DATA Handles the SMBus write_block_data command 39*4882a593Smuzhiyun I2C_FUNC_SMBUS_READ_I2C_BLOCK Handles the SMBus read_i2c_block_data command 40*4882a593Smuzhiyun I2C_FUNC_SMBUS_WRITE_I2C_BLOCK Handles the SMBus write_i2c_block_data command 41*4882a593Smuzhiyun =============================== ============================================== 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunA few combinations of the above flags are also defined for your convenience: 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun ========================= ====================================== 46*4882a593Smuzhiyun I2C_FUNC_SMBUS_BYTE Handles the SMBus read_byte 47*4882a593Smuzhiyun and write_byte commands 48*4882a593Smuzhiyun I2C_FUNC_SMBUS_BYTE_DATA Handles the SMBus read_byte_data 49*4882a593Smuzhiyun and write_byte_data commands 50*4882a593Smuzhiyun I2C_FUNC_SMBUS_WORD_DATA Handles the SMBus read_word_data 51*4882a593Smuzhiyun and write_word_data commands 52*4882a593Smuzhiyun I2C_FUNC_SMBUS_BLOCK_DATA Handles the SMBus read_block_data 53*4882a593Smuzhiyun and write_block_data commands 54*4882a593Smuzhiyun I2C_FUNC_SMBUS_I2C_BLOCK Handles the SMBus read_i2c_block_data 55*4882a593Smuzhiyun and write_i2c_block_data commands 56*4882a593Smuzhiyun I2C_FUNC_SMBUS_EMUL Handles all SMBus commands that can be 57*4882a593Smuzhiyun emulated by a real I2C adapter (using 58*4882a593Smuzhiyun the transparent emulation layer) 59*4882a593Smuzhiyun ========================= ====================================== 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunIn kernel versions prior to 3.5 I2C_FUNC_NOSTART was implemented as 62*4882a593Smuzhiyunpart of I2C_FUNC_PROTOCOL_MANGLING. 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunADAPTER IMPLEMENTATION 66*4882a593Smuzhiyun---------------------- 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunWhen you write a new adapter driver, you will have to implement a 69*4882a593Smuzhiyunfunction callback ``functionality``. Typical implementations are given 70*4882a593Smuzhiyunbelow. 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunA typical SMBus-only adapter would list all the SMBus transactions it 73*4882a593Smuzhiyunsupports. This example comes from the i2c-piix4 driver:: 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun static u32 piix4_func(struct i2c_adapter *adapter) 76*4882a593Smuzhiyun { 77*4882a593Smuzhiyun return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 78*4882a593Smuzhiyun I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | 79*4882a593Smuzhiyun I2C_FUNC_SMBUS_BLOCK_DATA; 80*4882a593Smuzhiyun } 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunA typical full-I2C adapter would use the following (from the i2c-pxa 83*4882a593Smuzhiyundriver):: 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun static u32 i2c_pxa_functionality(struct i2c_adapter *adap) 86*4882a593Smuzhiyun { 87*4882a593Smuzhiyun return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 88*4882a593Smuzhiyun } 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunI2C_FUNC_SMBUS_EMUL includes all the SMBus transactions (with the 91*4882a593Smuzhiyunaddition of I2C block transactions) which i2c-core can emulate using 92*4882a593SmuzhiyunI2C_FUNC_I2C without any help from the adapter driver. The idea is 93*4882a593Smuzhiyunto let the client drivers check for the support of SMBus functions 94*4882a593Smuzhiyunwithout having to care whether the said functions are implemented in 95*4882a593Smuzhiyunhardware by the adapter, or emulated in software by i2c-core on top 96*4882a593Smuzhiyunof an I2C adapter. 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunCLIENT CHECKING 100*4882a593Smuzhiyun--------------- 101*4882a593Smuzhiyun 102*4882a593SmuzhiyunBefore a client tries to attach to an adapter, or even do tests to check 103*4882a593Smuzhiyunwhether one of the devices it supports is present on an adapter, it should 104*4882a593Smuzhiyuncheck whether the needed functionality is present. The typical way to do 105*4882a593Smuzhiyunthis is (from the lm75 driver):: 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun static int lm75_detect(...) 108*4882a593Smuzhiyun { 109*4882a593Smuzhiyun (...) 110*4882a593Smuzhiyun if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 111*4882a593Smuzhiyun I2C_FUNC_SMBUS_WORD_DATA)) 112*4882a593Smuzhiyun goto exit; 113*4882a593Smuzhiyun (...) 114*4882a593Smuzhiyun } 115*4882a593Smuzhiyun 116*4882a593SmuzhiyunHere, the lm75 driver checks if the adapter can do both SMBus byte data 117*4882a593Smuzhiyunand SMBus word data transactions. If not, then the driver won't work on 118*4882a593Smuzhiyunthis adapter and there's no point in going on. If the check above is 119*4882a593Smuzhiyunsuccessful, then the driver knows that it can call the following 120*4882a593Smuzhiyunfunctions: i2c_smbus_read_byte_data(), i2c_smbus_write_byte_data(), 121*4882a593Smuzhiyuni2c_smbus_read_word_data() and i2c_smbus_write_word_data(). As a rule of 122*4882a593Smuzhiyunthumb, the functionality constants you test for with 123*4882a593Smuzhiyuni2c_check_functionality() should match exactly the i2c_smbus_* functions 124*4882a593Smuzhiyunwhich you driver is calling. 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunNote that the check above doesn't tell whether the functionalities are 127*4882a593Smuzhiyunimplemented in hardware by the underlying adapter or emulated in 128*4882a593Smuzhiyunsoftware by i2c-core. Client drivers don't have to care about this, as 129*4882a593Smuzhiyuni2c-core will transparently implement SMBus transactions on top of I2C 130*4882a593Smuzhiyunadapters. 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunCHECKING THROUGH /DEV 134*4882a593Smuzhiyun--------------------- 135*4882a593Smuzhiyun 136*4882a593SmuzhiyunIf you try to access an adapter from a userspace program, you will have 137*4882a593Smuzhiyunto use the /dev interface. You will still have to check whether the 138*4882a593Smuzhiyunfunctionality you need is supported, of course. This is done using 139*4882a593Smuzhiyunthe I2C_FUNCS ioctl. An example, adapted from the i2cdetect program, is 140*4882a593Smuzhiyunbelow:: 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun int file; 143*4882a593Smuzhiyun if (file = open("/dev/i2c-0", O_RDWR) < 0) { 144*4882a593Smuzhiyun /* Some kind of error handling */ 145*4882a593Smuzhiyun exit(1); 146*4882a593Smuzhiyun } 147*4882a593Smuzhiyun if (ioctl(file, I2C_FUNCS, &funcs) < 0) { 148*4882a593Smuzhiyun /* Some kind of error handling */ 149*4882a593Smuzhiyun exit(1); 150*4882a593Smuzhiyun } 151*4882a593Smuzhiyun if (!(funcs & I2C_FUNC_SMBUS_QUICK)) { 152*4882a593Smuzhiyun /* Oops, the needed functionality (SMBus write_quick function) is 153*4882a593Smuzhiyun not available! */ 154*4882a593Smuzhiyun exit(1); 155*4882a593Smuzhiyun } 156*4882a593Smuzhiyun /* Now it is safe to use the SMBus write_quick command */ 157