162306a36Sopenharmony_ci======================= 262306a36Sopenharmony_ciI2C/SMBus Functionality 362306a36Sopenharmony_ci======================= 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciINTRODUCTION 662306a36Sopenharmony_ci------------ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ciBecause not every I2C or SMBus adapter implements everything in the 962306a36Sopenharmony_ciI2C specifications, a client can not trust that everything it needs 1062306a36Sopenharmony_ciis implemented when it is given the option to attach to an adapter: 1162306a36Sopenharmony_cithe client needs some way to check whether an adapter has the needed 1262306a36Sopenharmony_cifunctionality. 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciFUNCTIONALITY CONSTANTS 1662306a36Sopenharmony_ci----------------------- 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ciFor the most up-to-date list of functionality constants, please check 1962306a36Sopenharmony_ci<uapi/linux/i2c.h>! 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci =============================== ============================================== 2262306a36Sopenharmony_ci I2C_FUNC_I2C Plain i2c-level commands (Pure SMBus 2362306a36Sopenharmony_ci adapters typically can not do these) 2462306a36Sopenharmony_ci I2C_FUNC_10BIT_ADDR Handles the 10-bit address extensions 2562306a36Sopenharmony_ci I2C_FUNC_PROTOCOL_MANGLING Knows about the I2C_M_IGNORE_NAK, 2662306a36Sopenharmony_ci I2C_M_REV_DIR_ADDR and I2C_M_NO_RD_ACK 2762306a36Sopenharmony_ci flags (which modify the I2C protocol!) 2862306a36Sopenharmony_ci I2C_FUNC_NOSTART Can skip repeated start sequence 2962306a36Sopenharmony_ci I2C_FUNC_SMBUS_QUICK Handles the SMBus write_quick command 3062306a36Sopenharmony_ci I2C_FUNC_SMBUS_READ_BYTE Handles the SMBus read_byte command 3162306a36Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_BYTE Handles the SMBus write_byte command 3262306a36Sopenharmony_ci I2C_FUNC_SMBUS_READ_BYTE_DATA Handles the SMBus read_byte_data command 3362306a36Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_BYTE_DATA Handles the SMBus write_byte_data command 3462306a36Sopenharmony_ci I2C_FUNC_SMBUS_READ_WORD_DATA Handles the SMBus read_word_data command 3562306a36Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_WORD_DATA Handles the SMBus write_byte_data command 3662306a36Sopenharmony_ci I2C_FUNC_SMBUS_PROC_CALL Handles the SMBus process_call command 3762306a36Sopenharmony_ci I2C_FUNC_SMBUS_READ_BLOCK_DATA Handles the SMBus read_block_data command 3862306a36Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_BLOCK_DATA Handles the SMBus write_block_data command 3962306a36Sopenharmony_ci I2C_FUNC_SMBUS_READ_I2C_BLOCK Handles the SMBus read_i2c_block_data command 4062306a36Sopenharmony_ci I2C_FUNC_SMBUS_WRITE_I2C_BLOCK Handles the SMBus write_i2c_block_data command 4162306a36Sopenharmony_ci =============================== ============================================== 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ciA few combinations of the above flags are also defined for your convenience: 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci ========================= ====================================== 4662306a36Sopenharmony_ci I2C_FUNC_SMBUS_BYTE Handles the SMBus read_byte 4762306a36Sopenharmony_ci and write_byte commands 4862306a36Sopenharmony_ci I2C_FUNC_SMBUS_BYTE_DATA Handles the SMBus read_byte_data 4962306a36Sopenharmony_ci and write_byte_data commands 5062306a36Sopenharmony_ci I2C_FUNC_SMBUS_WORD_DATA Handles the SMBus read_word_data 5162306a36Sopenharmony_ci and write_word_data commands 5262306a36Sopenharmony_ci I2C_FUNC_SMBUS_BLOCK_DATA Handles the SMBus read_block_data 5362306a36Sopenharmony_ci and write_block_data commands 5462306a36Sopenharmony_ci I2C_FUNC_SMBUS_I2C_BLOCK Handles the SMBus read_i2c_block_data 5562306a36Sopenharmony_ci and write_i2c_block_data commands 5662306a36Sopenharmony_ci I2C_FUNC_SMBUS_EMUL Handles all SMBus commands that can be 5762306a36Sopenharmony_ci emulated by a real I2C adapter (using 5862306a36Sopenharmony_ci the transparent emulation layer) 5962306a36Sopenharmony_ci ========================= ====================================== 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ciIn kernel versions prior to 3.5 I2C_FUNC_NOSTART was implemented as 6262306a36Sopenharmony_cipart of I2C_FUNC_PROTOCOL_MANGLING. 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ciADAPTER IMPLEMENTATION 6662306a36Sopenharmony_ci---------------------- 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ciWhen you write a new adapter driver, you will have to implement a 6962306a36Sopenharmony_cifunction callback ``functionality``. Typical implementations are given 7062306a36Sopenharmony_cibelow. 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ciA typical SMBus-only adapter would list all the SMBus transactions it 7362306a36Sopenharmony_cisupports. This example comes from the i2c-piix4 driver:: 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci static u32 piix4_func(struct i2c_adapter *adapter) 7662306a36Sopenharmony_ci { 7762306a36Sopenharmony_ci return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | 7862306a36Sopenharmony_ci I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | 7962306a36Sopenharmony_ci I2C_FUNC_SMBUS_BLOCK_DATA; 8062306a36Sopenharmony_ci } 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ciA typical full-I2C adapter would use the following (from the i2c-pxa 8362306a36Sopenharmony_cidriver):: 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci static u32 i2c_pxa_functionality(struct i2c_adapter *adap) 8662306a36Sopenharmony_ci { 8762306a36Sopenharmony_ci return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 8862306a36Sopenharmony_ci } 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciI2C_FUNC_SMBUS_EMUL includes all the SMBus transactions (with the 9162306a36Sopenharmony_ciaddition of I2C block transactions) which i2c-core can emulate using 9262306a36Sopenharmony_ciI2C_FUNC_I2C without any help from the adapter driver. The idea is 9362306a36Sopenharmony_cito let the client drivers check for the support of SMBus functions 9462306a36Sopenharmony_ciwithout having to care whether the said functions are implemented in 9562306a36Sopenharmony_cihardware by the adapter, or emulated in software by i2c-core on top 9662306a36Sopenharmony_ciof an I2C adapter. 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ciCLIENT CHECKING 10062306a36Sopenharmony_ci--------------- 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ciBefore a client tries to attach to an adapter, or even do tests to check 10362306a36Sopenharmony_ciwhether one of the devices it supports is present on an adapter, it should 10462306a36Sopenharmony_cicheck whether the needed functionality is present. The typical way to do 10562306a36Sopenharmony_cithis is (from the lm75 driver):: 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci static int lm75_detect(...) 10862306a36Sopenharmony_ci { 10962306a36Sopenharmony_ci (...) 11062306a36Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 11162306a36Sopenharmony_ci I2C_FUNC_SMBUS_WORD_DATA)) 11262306a36Sopenharmony_ci goto exit; 11362306a36Sopenharmony_ci (...) 11462306a36Sopenharmony_ci } 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ciHere, the lm75 driver checks if the adapter can do both SMBus byte data 11762306a36Sopenharmony_ciand SMBus word data transactions. If not, then the driver won't work on 11862306a36Sopenharmony_cithis adapter and there's no point in going on. If the check above is 11962306a36Sopenharmony_cisuccessful, then the driver knows that it can call the following 12062306a36Sopenharmony_cifunctions: i2c_smbus_read_byte_data(), i2c_smbus_write_byte_data(), 12162306a36Sopenharmony_cii2c_smbus_read_word_data() and i2c_smbus_write_word_data(). As a rule of 12262306a36Sopenharmony_cithumb, the functionality constants you test for with 12362306a36Sopenharmony_cii2c_check_functionality() should match exactly the i2c_smbus_* functions 12462306a36Sopenharmony_ciwhich you driver is calling. 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ciNote that the check above doesn't tell whether the functionalities are 12762306a36Sopenharmony_ciimplemented in hardware by the underlying adapter or emulated in 12862306a36Sopenharmony_cisoftware by i2c-core. Client drivers don't have to care about this, as 12962306a36Sopenharmony_cii2c-core will transparently implement SMBus transactions on top of I2C 13062306a36Sopenharmony_ciadapters. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ciCHECKING THROUGH /DEV 13462306a36Sopenharmony_ci--------------------- 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ciIf you try to access an adapter from a userspace program, you will have 13762306a36Sopenharmony_cito use the /dev interface. You will still have to check whether the 13862306a36Sopenharmony_cifunctionality you need is supported, of course. This is done using 13962306a36Sopenharmony_cithe I2C_FUNCS ioctl. An example, adapted from the i2cdetect program, is 14062306a36Sopenharmony_cibelow:: 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci int file; 14362306a36Sopenharmony_ci if (file = open("/dev/i2c-0", O_RDWR) < 0) { 14462306a36Sopenharmony_ci /* Some kind of error handling */ 14562306a36Sopenharmony_ci exit(1); 14662306a36Sopenharmony_ci } 14762306a36Sopenharmony_ci if (ioctl(file, I2C_FUNCS, &funcs) < 0) { 14862306a36Sopenharmony_ci /* Some kind of error handling */ 14962306a36Sopenharmony_ci exit(1); 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci if (!(funcs & I2C_FUNC_SMBUS_QUICK)) { 15262306a36Sopenharmony_ci /* Oops, the needed functionality (SMBus write_quick function) is 15362306a36Sopenharmony_ci not available! */ 15462306a36Sopenharmony_ci exit(1); 15562306a36Sopenharmony_ci } 15662306a36Sopenharmony_ci /* Now it is safe to use the SMBus write_quick command */ 157