dev-interface 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. I2C device files are character device files with major device number 89
  7. and a minor device number corresponding to the number assigned as
  8. explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ...,
  9. i2c-10, ...). All 256 minor device numbers are reserved for i2c.
  10. C example
  11. =========
  12. So let's say you want to access an i2c adapter from a C program. The
  13. first thing to do is `#include <linux/i2c.h>" and "#include <linux/i2c-dev.h>.
  14. Yes, I know, you should never include kernel header files, but until glibc
  15. knows about i2c, there is not much choice.
  16. Now, you have to decide which adapter you want to access. You should
  17. inspect /sys/class/i2c-dev/ to decide this. Adapter numbers are assigned
  18. somewhat dynamically, so you can not even assume /dev/i2c-0 is the
  19. first adapter.
  20. Next thing, open the device file, as follows:
  21. int file;
  22. int adapter_nr = 2; /* probably dynamically determined */
  23. char filename[20];
  24. sprintf(filename,"/dev/i2c-%d",adapter_nr);
  25. if ((file = open(filename,O_RDWR)) < 0) {
  26. /* ERROR HANDLING; you can check errno to see what went wrong */
  27. exit(1);
  28. }
  29. When you have opened the device, you must specify with what device
  30. address you want to communicate:
  31. int addr = 0x40; /* The I2C address */
  32. if (ioctl(file,I2C_SLAVE,addr) < 0) {
  33. /* ERROR HANDLING; you can check errno to see what went wrong */
  34. exit(1);
  35. }
  36. Well, you are all set up now. You can now use SMBus commands or plain
  37. I2C to communicate with your device. SMBus commands are preferred if
  38. the device supports them. Both are illustrated below.
  39. __u8 register = 0x10; /* Device register to access */
  40. __s32 res;
  41. char buf[10];
  42. /* Using SMBus commands */
  43. res = i2c_smbus_read_word_data(file,register);
  44. if (res < 0) {
  45. /* ERROR HANDLING: i2c transaction failed */
  46. } else {
  47. /* res contains the read word */
  48. }
  49. /* Using I2C Write, equivalent of
  50. i2c_smbus_write_word_data(file,register,0x6543) */
  51. buf[0] = register;
  52. buf[1] = 0x43;
  53. buf[2] = 0x65;
  54. if ( write(file,buf,3) != 3) {
  55. /* ERROR HANDLING: i2c transaction failed */
  56. }
  57. /* Using I2C Read, equivalent of i2c_smbus_read_byte(file) */
  58. if (read(file,buf,1) != 1) {
  59. /* ERROR HANDLING: i2c transaction failed */
  60. } else {
  61. /* buf[0] contains the read byte */
  62. }
  63. IMPORTANT: because of the use of inline functions, you *have* to use
  64. '-O' or some variation when you compile your program!
  65. Full interface description
  66. ==========================
  67. The following IOCTLs are defined and fully supported
  68. (see also i2c-dev.h and i2c.h):
  69. ioctl(file,I2C_SLAVE,long addr)
  70. Change slave address. The address is passed in the 7 lower bits of the
  71. argument (except for 10 bit addresses, passed in the 10 lower bits in this
  72. case).
  73. ioctl(file,I2C_TENBIT,long select)
  74. Selects ten bit addresses if select not equals 0, selects normal 7 bit
  75. addresses if select equals 0. Default 0.
  76. ioctl(file,I2C_PEC,long select)
  77. Selects SMBus PEC (packet error checking) generation and verification
  78. if select not equals 0, disables if select equals 0. Default 0.
  79. Used only for SMBus transactions.
  80. ioctl(file,I2C_FUNCS,unsigned long *funcs)
  81. Gets the adapter functionality and puts it in *funcs.
  82. ioctl(file,I2C_RDWR,struct i2c_ioctl_rdwr_data *msgset)
  83. Do combined read/write transaction without stop in between.
  84. The argument is a pointer to a struct i2c_ioctl_rdwr_data {
  85. struct i2c_msg *msgs; /* ptr to array of simple messages */
  86. int nmsgs; /* number of messages to exchange */
  87. }
  88. The msgs[] themselves contain further pointers into data buffers.
  89. The function will write or read data to or from that buffers depending
  90. on whether the I2C_M_RD flag is set in a particular message or not.
  91. The slave address and whether to use ten bit address mode has to be
  92. set in each message, overriding the values set with the above ioctl's.
  93. Other values are NOT supported at this moment, except for I2C_SMBUS,
  94. which you should never directly call; instead, use the access functions
  95. below.
  96. You can do plain i2c transactions by using read(2) and write(2) calls.
  97. You do not need to pass the address byte; instead, set it through
  98. ioctl I2C_SLAVE before you try to access the device.
  99. You can do SMBus level transactions (see documentation file smbus-protocol
  100. for details) through the following functions:
  101. __s32 i2c_smbus_write_quick(int file, __u8 value);
  102. __s32 i2c_smbus_read_byte(int file);
  103. __s32 i2c_smbus_write_byte(int file, __u8 value);
  104. __s32 i2c_smbus_read_byte_data(int file, __u8 command);
  105. __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
  106. __s32 i2c_smbus_read_word_data(int file, __u8 command);
  107. __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value);
  108. __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value);
  109. __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values);
  110. __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length,
  111. __u8 *values);
  112. All these transactions return -1 on failure; you can read errno to see
  113. what happened. The 'write' transactions return 0 on success; the
  114. 'read' transactions return the read value, except for read_block, which
  115. returns the number of values read. The block buffers need not be longer
  116. than 32 bytes.
  117. The above functions are all macros, that resolve to calls to the
  118. i2c_smbus_access function, that on its turn calls a specific ioctl
  119. with the data in a specific format. Read the source code if you
  120. want to know what happens behind the screens.