Back to home page

OSCL-LXR

 
 

    


0001 =======================
0002 I2C/SMBus Functionality
0003 =======================
0004 
0005 INTRODUCTION
0006 ------------
0007 
0008 Because not every I2C or SMBus adapter implements everything in the
0009 I2C specifications, a client can not trust that everything it needs
0010 is implemented when it is given the option to attach to an adapter:
0011 the client needs some way to check whether an adapter has the needed
0012 functionality.
0013 
0014 
0015 FUNCTIONALITY CONSTANTS
0016 -----------------------
0017 
0018 For the most up-to-date list of functionality constants, please check
0019 <uapi/linux/i2c.h>!
0020 
0021   =============================== ==============================================
0022   I2C_FUNC_I2C                    Plain i2c-level commands (Pure SMBus
0023                                   adapters typically can not do these)
0024   I2C_FUNC_10BIT_ADDR             Handles the 10-bit address extensions
0025   I2C_FUNC_PROTOCOL_MANGLING      Knows about the I2C_M_IGNORE_NAK,
0026                                   I2C_M_REV_DIR_ADDR and I2C_M_NO_RD_ACK
0027                                   flags (which modify the I2C protocol!)
0028   I2C_FUNC_NOSTART                Can skip repeated start sequence
0029   I2C_FUNC_SMBUS_QUICK            Handles the SMBus write_quick command
0030   I2C_FUNC_SMBUS_READ_BYTE        Handles the SMBus read_byte command
0031   I2C_FUNC_SMBUS_WRITE_BYTE       Handles the SMBus write_byte command
0032   I2C_FUNC_SMBUS_READ_BYTE_DATA   Handles the SMBus read_byte_data command
0033   I2C_FUNC_SMBUS_WRITE_BYTE_DATA  Handles the SMBus write_byte_data command
0034   I2C_FUNC_SMBUS_READ_WORD_DATA   Handles the SMBus read_word_data command
0035   I2C_FUNC_SMBUS_WRITE_WORD_DATA  Handles the SMBus write_byte_data command
0036   I2C_FUNC_SMBUS_PROC_CALL        Handles the SMBus process_call command
0037   I2C_FUNC_SMBUS_READ_BLOCK_DATA  Handles the SMBus read_block_data command
0038   I2C_FUNC_SMBUS_WRITE_BLOCK_DATA Handles the SMBus write_block_data command
0039   I2C_FUNC_SMBUS_READ_I2C_BLOCK   Handles the SMBus read_i2c_block_data command
0040   I2C_FUNC_SMBUS_WRITE_I2C_BLOCK  Handles the SMBus write_i2c_block_data command
0041   =============================== ==============================================
0042 
0043 A few combinations of the above flags are also defined for your convenience:
0044 
0045   =========================       ======================================
0046   I2C_FUNC_SMBUS_BYTE             Handles the SMBus read_byte
0047                                   and write_byte commands
0048   I2C_FUNC_SMBUS_BYTE_DATA        Handles the SMBus read_byte_data
0049                                   and write_byte_data commands
0050   I2C_FUNC_SMBUS_WORD_DATA        Handles the SMBus read_word_data
0051                                   and write_word_data commands
0052   I2C_FUNC_SMBUS_BLOCK_DATA       Handles the SMBus read_block_data
0053                                   and write_block_data commands
0054   I2C_FUNC_SMBUS_I2C_BLOCK        Handles the SMBus read_i2c_block_data
0055                                   and write_i2c_block_data commands
0056   I2C_FUNC_SMBUS_EMUL             Handles all SMBus commands that can be
0057                                   emulated by a real I2C adapter (using
0058                                   the transparent emulation layer)
0059   =========================       ======================================
0060 
0061 In kernel versions prior to 3.5 I2C_FUNC_NOSTART was implemented as
0062 part of I2C_FUNC_PROTOCOL_MANGLING.
0063 
0064 
0065 ADAPTER IMPLEMENTATION
0066 ----------------------
0067 
0068 When you write a new adapter driver, you will have to implement a
0069 function callback ``functionality``. Typical implementations are given
0070 below.
0071 
0072 A typical SMBus-only adapter would list all the SMBus transactions it
0073 supports. This example comes from the i2c-piix4 driver::
0074 
0075   static u32 piix4_func(struct i2c_adapter *adapter)
0076   {
0077         return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
0078                I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
0079                I2C_FUNC_SMBUS_BLOCK_DATA;
0080   }
0081 
0082 A typical full-I2C adapter would use the following (from the i2c-pxa
0083 driver)::
0084 
0085   static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
0086   {
0087         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0088   }
0089 
0090 I2C_FUNC_SMBUS_EMUL includes all the SMBus transactions (with the
0091 addition of I2C block transactions) which i2c-core can emulate using
0092 I2C_FUNC_I2C without any help from the adapter driver. The idea is
0093 to let the client drivers check for the support of SMBus functions
0094 without having to care whether the said functions are implemented in
0095 hardware by the adapter, or emulated in software by i2c-core on top
0096 of an I2C adapter.
0097 
0098 
0099 CLIENT CHECKING
0100 ---------------
0101 
0102 Before a client tries to attach to an adapter, or even do tests to check
0103 whether one of the devices it supports is present on an adapter, it should
0104 check whether the needed functionality is present. The typical way to do
0105 this is (from the lm75 driver)::
0106 
0107   static int lm75_detect(...)
0108   {
0109         (...)
0110         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
0111                                      I2C_FUNC_SMBUS_WORD_DATA))
0112                 goto exit;
0113         (...)
0114   }
0115 
0116 Here, the lm75 driver checks if the adapter can do both SMBus byte data
0117 and SMBus word data transactions. If not, then the driver won't work on
0118 this adapter and there's no point in going on. If the check above is
0119 successful, then the driver knows that it can call the following
0120 functions: i2c_smbus_read_byte_data(), i2c_smbus_write_byte_data(),
0121 i2c_smbus_read_word_data() and i2c_smbus_write_word_data(). As a rule of
0122 thumb, the functionality constants you test for with
0123 i2c_check_functionality() should match exactly the i2c_smbus_* functions
0124 which you driver is calling.
0125 
0126 Note that the check above doesn't tell whether the functionalities are
0127 implemented in hardware by the underlying adapter or emulated in
0128 software by i2c-core. Client drivers don't have to care about this, as
0129 i2c-core will transparently implement SMBus transactions on top of I2C
0130 adapters.
0131 
0132 
0133 CHECKING THROUGH /DEV
0134 ---------------------
0135 
0136 If you try to access an adapter from a userspace program, you will have
0137 to use the /dev interface. You will still have to check whether the
0138 functionality you need is supported, of course. This is done using
0139 the I2C_FUNCS ioctl. An example, adapted from the i2cdetect program, is
0140 below::
0141 
0142   int file;
0143   if (file = open("/dev/i2c-0", O_RDWR) < 0) {
0144         /* Some kind of error handling */
0145         exit(1);
0146   }
0147   if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
0148         /* Some kind of error handling */
0149         exit(1);
0150   }
0151   if (!(funcs & I2C_FUNC_SMBUS_QUICK)) {
0152         /* Oops, the needed functionality (SMBus write_quick function) is
0153            not available! */
0154         exit(1);
0155   }
0156   /* Now it is safe to use the SMBus write_quick command */