functionality 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. INTRODUCTION
  2. ------------
  3. Because not every I2C or SMBus adapter implements everything in the
  4. I2C specifications, a client can not trust that everything it needs
  5. is implemented when it is given the option to attach to an adapter:
  6. the client needs some way to check whether an adapter has the needed
  7. functionality.
  8. FUNCTIONALITY CONSTANTS
  9. -----------------------
  10. For the most up-to-date list of functionality constants, please check
  11. <linux/i2c.h>!
  12. I2C_FUNC_I2C Plain i2c-level commands (Pure SMBus
  13. adapters typically can not do these)
  14. I2C_FUNC_10BIT_ADDR Handles the 10-bit address extensions
  15. I2C_FUNC_PROTOCOL_MANGLING Knows about the I2C_M_IGNORE_NAK,
  16. I2C_M_REV_DIR_ADDR, I2C_M_NOSTART and
  17. I2C_M_NO_RD_ACK flags (which modify the
  18. I2C protocol!)
  19. I2C_FUNC_SMBUS_QUICK Handles the SMBus write_quick command
  20. I2C_FUNC_SMBUS_READ_BYTE Handles the SMBus read_byte command
  21. I2C_FUNC_SMBUS_WRITE_BYTE Handles the SMBus write_byte command
  22. I2C_FUNC_SMBUS_READ_BYTE_DATA Handles the SMBus read_byte_data command
  23. I2C_FUNC_SMBUS_WRITE_BYTE_DATA Handles the SMBus write_byte_data command
  24. I2C_FUNC_SMBUS_READ_WORD_DATA Handles the SMBus read_word_data command
  25. I2C_FUNC_SMBUS_WRITE_WORD_DATA Handles the SMBus write_byte_data command
  26. I2C_FUNC_SMBUS_PROC_CALL Handles the SMBus process_call command
  27. I2C_FUNC_SMBUS_READ_BLOCK_DATA Handles the SMBus read_block_data command
  28. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA Handles the SMBus write_block_data command
  29. I2C_FUNC_SMBUS_READ_I2C_BLOCK Handles the SMBus read_i2c_block_data command
  30. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK Handles the SMBus write_i2c_block_data command
  31. A few combinations of the above flags are also defined for your convenience:
  32. I2C_FUNC_SMBUS_BYTE Handles the SMBus read_byte
  33. and write_byte commands
  34. I2C_FUNC_SMBUS_BYTE_DATA Handles the SMBus read_byte_data
  35. and write_byte_data commands
  36. I2C_FUNC_SMBUS_WORD_DATA Handles the SMBus read_word_data
  37. and write_word_data commands
  38. I2C_FUNC_SMBUS_BLOCK_DATA Handles the SMBus read_block_data
  39. and write_block_data commands
  40. I2C_FUNC_SMBUS_I2C_BLOCK Handles the SMBus read_i2c_block_data
  41. and write_i2c_block_data commands
  42. I2C_FUNC_SMBUS_EMUL Handles all SMBus commands than can be
  43. emulated by a real I2C adapter (using
  44. the transparent emulation layer)
  45. ADAPTER IMPLEMENTATION
  46. ----------------------
  47. When you write a new adapter driver, you will have to implement a
  48. function callback `functionality'. Typical implementations are given
  49. below.
  50. A typical SMBus-only adapter would list all the SMBus transactions it
  51. supports. This example comes from the i2c-piix4 driver:
  52. static u32 piix4_func(struct i2c_adapter *adapter)
  53. {
  54. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  55. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  56. I2C_FUNC_SMBUS_BLOCK_DATA;
  57. }
  58. A typical full-I2C adapter would use the following (from the i2c-pxa
  59. driver):
  60. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  61. {
  62. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  63. }
  64. I2C_FUNC_SMBUS_EMUL includes all the SMBus transactions (with the
  65. addition of I2C block transactions) which i2c-core can emulate using
  66. I2C_FUNC_I2C without any help from the adapter driver. The idea is
  67. to let the client drivers check for the support of SMBus functions
  68. without having to care whether the said functions are implemented in
  69. hardware by the adapter, or emulated in software by i2c-core on top
  70. of an I2C adapter.
  71. CLIENT CHECKING
  72. ---------------
  73. Before a client tries to attach to an adapter, or even do tests to check
  74. whether one of the devices it supports is present on an adapter, it should
  75. check whether the needed functionality is present. The typical way to do
  76. this is (from the lm75 driver):
  77. static int lm75_detect(...)
  78. {
  79. (...)
  80. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  81. I2C_FUNC_SMBUS_WORD_DATA))
  82. goto exit;
  83. (...)
  84. }
  85. Here, the lm75 driver checks if the adapter can do both SMBus byte data
  86. and SMBus word data transactions. If not, then the driver won't work on
  87. this adapter and there's no point in going on. If the check above is
  88. successful, then the driver knows that it can call the following
  89. functions: i2c_smbus_read_byte_data(), i2c_smbus_write_byte_data(),
  90. i2c_smbus_read_word_data() and i2c_smbus_write_word_data(). As a rule of
  91. thumb, the functionality constants you test for with
  92. i2c_check_functionality() should match exactly the i2c_smbus_* functions
  93. which you driver is calling.
  94. Note that the check above doesn't tell whether the functionalities are
  95. implemented in hardware by the underlying adapter or emulated in
  96. software by i2c-core. Client drivers don't have to care about this, as
  97. i2c-core will transparently implement SMBus transactions on top of I2C
  98. adapters.
  99. CHECKING THROUGH /DEV
  100. ---------------------
  101. If you try to access an adapter from a userspace program, you will have
  102. to use the /dev interface. You will still have to check whether the
  103. functionality you need is supported, of course. This is done using
  104. the I2C_FUNCS ioctl. An example, adapted from the i2cdetect program, is
  105. below:
  106. int file;
  107. if (file = open("/dev/i2c-0", O_RDWR) < 0) {
  108. /* Some kind of error handling */
  109. exit(1);
  110. }
  111. if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
  112. /* Some kind of error handling */
  113. exit(1);
  114. }
  115. if (!(funcs & I2C_FUNC_SMBUS_QUICK)) {
  116. /* Oops, the needed functionality (SMBus write_quick function) is
  117. not available! */
  118. exit(1);
  119. }
  120. /* Now it is safe to use the SMBus write_quick command */