dev-interface 8.4 KB

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