i2c.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef _I2C_H
  2. #define _I2C_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. struct i2c_adapter;
  7. /**
  8. * struct i2c_msg - an I2C transaction segment beginning with START
  9. * @addr: Slave address, either seven or ten bits. When this is a ten
  10. * bit address, I2C_M_TEN must be set in @flags and the adapter
  11. * must support I2C_FUNC_10BIT_ADDR.
  12. * @flags: I2C_M_RD is handled by all adapters. No other flags may be
  13. * provided unless the adapter exported the relevant I2C_FUNC_*
  14. * flags through i2c_check_functionality().
  15. * @len: Number of data bytes in @buf being read from or written to the
  16. * I2C slave address. For read transactions where I2C_M_RECV_LEN
  17. * is set, the caller guarantees that this buffer can hold up to
  18. * 32 bytes in addition to the initial length byte sent by the
  19. * slave (plus, if used, the SMBus PEC); and this value will be
  20. * incremented by the number of block data bytes received.
  21. * @buf: The buffer into which data is read, or from which it's written.
  22. *
  23. * An i2c_msg is the low level representation of one segment of an I2C
  24. * transaction. It is visible to drivers in the @i2c_transfer() procedure,
  25. * to userspace from i2c-dev, and to I2C adapter drivers through the
  26. * @i2c_adapter.@master_xfer() method.
  27. *
  28. * Except when I2C "protocol mangling" is used, all I2C adapters implement
  29. * the standard rules for I2C transactions. Each transaction begins with a
  30. * START. That is followed by the slave address, and a bit encoding read
  31. * versus write. Then follow all the data bytes, possibly including a byte
  32. * with SMBus PEC. The transfer terminates with a NAK, or when all those
  33. * bytes have been transferred and ACKed. If this is the last message in a
  34. * group, it is followed by a STOP. Otherwise it is followed by the next
  35. * @i2c_msg transaction segment, beginning with a (repeated) START.
  36. *
  37. * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then
  38. * passing certain @flags may have changed those standard protocol behaviors.
  39. * Those flags are only for use with broken/nonconforming slaves, and with
  40. * adapters which are known to support the specific mangling options they
  41. * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR).
  42. */
  43. struct i2c_msg {
  44. uint16_t addr; /* slave address */
  45. uint16_t flags;
  46. #define I2C_M_RD 0x0001 /* read data, from slave to master */
  47. /* I2C_M_RD is guaranteed to be 0x0001! */
  48. #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
  49. #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
  50. #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
  51. #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  52. #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  53. #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_NOSTART */
  54. #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  55. uint16_t len; /* msg length */
  56. uint8_t *buf; /* pointer to msg data */
  57. };
  58. /* To determine what functionality is present */
  59. #define I2C_FUNC_I2C 0x00000001
  60. #define I2C_FUNC_10BIT_ADDR 0x00000002
  61. #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_IGNORE_NAK etc. */
  62. #define I2C_FUNC_SMBUS_PEC 0x00000008
  63. #define I2C_FUNC_NOSTART 0x00000010 /* I2C_M_NOSTART */
  64. #define I2C_FUNC_SLAVE 0x00000020
  65. #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
  66. #define I2C_FUNC_SMBUS_QUICK 0x00010000
  67. #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
  68. #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
  69. #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
  70. #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
  71. #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
  72. #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
  73. #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
  74. #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
  75. #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
  76. #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
  77. #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
  78. #define I2C_FUNC_SMBUS_HOST_NOTIFY 0x10000000
  79. #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
  80. I2C_FUNC_SMBUS_WRITE_BYTE)
  81. #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
  82. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
  83. #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
  84. I2C_FUNC_SMBUS_WRITE_WORD_DATA)
  85. #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
  86. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
  87. #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
  88. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
  89. #define I2C_FUNC_SMBUS_EMUL (I2C_FUNC_SMBUS_QUICK | \
  90. I2C_FUNC_SMBUS_BYTE | \
  91. I2C_FUNC_SMBUS_BYTE_DATA | \
  92. I2C_FUNC_SMBUS_WORD_DATA | \
  93. I2C_FUNC_SMBUS_PROC_CALL | \
  94. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
  95. I2C_FUNC_SMBUS_I2C_BLOCK | \
  96. I2C_FUNC_SMBUS_PEC)
  97. /*
  98. * Data for SMBus Messages
  99. */
  100. #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
  101. union i2c_smbus_data {
  102. uint8_t byte;
  103. uint16_t word;
  104. uint8_t block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
  105. /* and one more for user-space compatibility */
  106. };
  107. /* i2c_smbus_xfer read or write markers */
  108. #define I2C_SMBUS_READ 1
  109. #define I2C_SMBUS_WRITE 0
  110. /* SMBus transaction types (size parameter in the above functions)
  111. Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
  112. #define I2C_SMBUS_QUICK 0
  113. #define I2C_SMBUS_BYTE 1
  114. #define I2C_SMBUS_BYTE_DATA 2
  115. #define I2C_SMBUS_WORD_DATA 3
  116. #define I2C_SMBUS_PROC_CALL 4
  117. #define I2C_SMBUS_BLOCK_DATA 5
  118. #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
  119. #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
  120. #define I2C_SMBUS_I2C_BLOCK_DATA 8
  121. /*flags for the client struct: */
  122. #define I2C_CLIENT_PEC 0x04 /* Use Packet Error Checking */
  123. #define I2C_CLIENT_TEN 0x10 /* we have a ten bit chip address */
  124. /* Must equal I2C_M_TEN below */
  125. #define I2C_CLIENT_SLAVE 0x20 /* we are the slave */
  126. #define I2C_CLIENT_HOST_NOTIFY 0x40 /* We want to use I2C host notify */
  127. #define I2C_CLIENT_WAKE 0x80 /* for board_info; true iff can wake */
  128. #define I2C_CLIENT_SCCB 0x9000 /* Use Omnivision SCCB protocol */
  129. /* Must match I2C_M_STOP|IGNORE_NAK */
  130. enum i2c_slave_event {
  131. I2C_SLAVE_READ_REQUESTED,
  132. I2C_SLAVE_WRITE_REQUESTED,
  133. I2C_SLAVE_READ_PROCESSED,
  134. I2C_SLAVE_WRITE_RECEIVED,
  135. I2C_SLAVE_STOP,
  136. };
  137. typedef int (*i2c_slave_cb_t)(struct i2c_adapter *, enum i2c_slave_event, u8 *);
  138. struct i2c_algorithm {
  139. /* master_xfer should return the number of messages successfully
  140. processed, or a negative value on error */
  141. int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
  142. int num);
  143. int (*reg_slave)(struct i2c_adapter *adap);
  144. int (*unreg_slave)(struct i2c_adapter *adap);
  145. };
  146. /*
  147. * i2c_adapter is the structure used to identify a physical i2c bus along
  148. * with the access algorithms necessary to access it.
  149. */
  150. struct i2c_adapter {
  151. const struct i2c_algorithm *algo; /* the algorithm to access the bus */
  152. void *algo_data;
  153. void *dw_dev;
  154. int retries;
  155. int timeout; /* in jiffies */
  156. SemaphoreHandle_t xMutex;
  157. i2c_slave_cb_t slave_cb; /* callback for slave mode */
  158. int open_count;
  159. unsigned short flags; /* div., see below */
  160. unsigned short addr; /* chip address - NOTE: 7bit */
  161. char name[16];
  162. };
  163. static __INLINE int i2c_slave_event(struct i2c_adapter *adap,
  164. enum i2c_slave_event event, u8 *val)
  165. {
  166. return adap->slave_cb(adap, event, val);
  167. }
  168. void i2c_init(void);
  169. int i2c_add_adapter(struct i2c_adapter *adap);
  170. struct i2c_adapter *i2c_open(const char *i2cdev);
  171. void i2c_close(struct i2c_adapter *adap);
  172. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
  173. int i2c_slave_register(struct i2c_adapter *adap, u8 addr, i2c_slave_cb_t slave_cb);
  174. int i2c_slave_unregister(struct i2c_adapter *adap);
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif