dev-interface 8.7 KB

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