i2c.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
  2. /*
  3. * i2c.h - definitions for the I2C bus interface
  4. *
  5. * Copyright (C) 1995-2000 Simon G. Vogl
  6. * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and
  7. * Frodo Looijaard <frodol@dds.nl>
  8. */
  9. #ifndef _LINUX_I2C_H
  10. #define _LINUX_I2C_H
  11. #include <linux/types.h>
  12. /**
  13. * struct i2c_msg - an I2C transaction segment beginning with START
  14. *
  15. * @addr: Slave address, either 7 or 10 bits. When this is a 10 bit address,
  16. * %I2C_M_TEN must be set in @flags and the adapter must support
  17. * %I2C_FUNC_10BIT_ADDR.
  18. *
  19. * @flags:
  20. * Supported by all adapters:
  21. * %I2C_M_RD: read data (from slave to master). Guaranteed to be 0x0001!
  22. *
  23. * Optional:
  24. * %I2C_M_DMA_SAFE: the buffer of this message is DMA safe. Makes only sense
  25. * in kernelspace, because userspace buffers are copied anyway
  26. *
  27. * Only if I2C_FUNC_10BIT_ADDR is set:
  28. * %I2C_M_TEN: this is a 10 bit chip address
  29. *
  30. * Only if I2C_FUNC_SMBUS_READ_BLOCK_DATA is set:
  31. * %I2C_M_RECV_LEN: message length will be first received byte
  32. *
  33. * Only if I2C_FUNC_NOSTART is set:
  34. * %I2C_M_NOSTART: skip repeated start sequence
  35. * Only if I2C_FUNC_PROTOCOL_MANGLING is set:
  36. * %I2C_M_NO_RD_ACK: in a read message, master ACK/NACK bit is skipped
  37. * %I2C_M_IGNORE_NAK: treat NACK from client as ACK
  38. * %I2C_M_REV_DIR_ADDR: toggles the Rd/Wr bit
  39. * %I2C_M_STOP: force a STOP condition after the message
  40. *
  41. * @len: Number of data bytes in @buf being read from or written to the I2C
  42. * slave address. For read transactions where %I2C_M_RECV_LEN is set, the
  43. * caller guarantees that this buffer can hold up to %I2C_SMBUS_BLOCK_MAX
  44. * bytes in addition to the initial length byte sent by the slave (plus,
  45. * if used, the SMBus PEC); and this value will be incremented by the number
  46. * of block data bytes received.
  47. *
  48. * @buf: The buffer into which data is read, or from which it's written.
  49. *
  50. * An i2c_msg is the low level representation of one segment of an I2C
  51. * transaction. It is visible to drivers in the @i2c_transfer() procedure,
  52. * to userspace from i2c-dev, and to I2C adapter drivers through the
  53. * @i2c_adapter.@master_xfer() method.
  54. *
  55. * Except when I2C "protocol mangling" is used, all I2C adapters implement
  56. * the standard rules for I2C transactions. Each transaction begins with a
  57. * START. That is followed by the slave address, and a bit encoding read
  58. * versus write. Then follow all the data bytes, possibly including a byte
  59. * with SMBus PEC. The transfer terminates with a NAK, or when all those
  60. * bytes have been transferred and ACKed. If this is the last message in a
  61. * group, it is followed by a STOP. Otherwise it is followed by the next
  62. * @i2c_msg transaction segment, beginning with a (repeated) START.
  63. *
  64. * Alternatively, when the adapter supports %I2C_FUNC_PROTOCOL_MANGLING then
  65. * passing certain @flags may have changed those standard protocol behaviors.
  66. * Those flags are only for use with broken/nonconforming slaves, and with
  67. * adapters which are known to support the specific mangling options they need.
  68. */
  69. struct i2c_msg {
  70. __u16 addr;
  71. __u16 flags;
  72. #define I2C_M_RD 0x0001 /* guaranteed to be 0x0001! */
  73. #define I2C_M_TEN 0x0010 /* use only if I2C_FUNC_10BIT_ADDR */
  74. #define I2C_M_DMA_SAFE 0x0200 /* use only in kernel space */
  75. #define I2C_M_RECV_LEN 0x0400 /* use only if I2C_FUNC_SMBUS_READ_BLOCK_DATA */
  76. #define I2C_M_NO_RD_ACK 0x0800 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
  77. #define I2C_M_IGNORE_NAK 0x1000 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
  78. #define I2C_M_REV_DIR_ADDR 0x2000 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
  79. #define I2C_M_NOSTART 0x4000 /* use only if I2C_FUNC_NOSTART */
  80. #define I2C_M_STOP 0x8000 /* use only if I2C_FUNC_PROTOCOL_MANGLING */
  81. __u16 len;
  82. __u8 *buf;
  83. };
  84. /* To determine what functionality is present */
  85. #define I2C_FUNC_I2C 0x00000001
  86. #define I2C_FUNC_10BIT_ADDR 0x00000002 /* required for I2C_M_TEN */
  87. #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* required for I2C_M_IGNORE_NAK etc. */
  88. #define I2C_FUNC_SMBUS_PEC 0x00000008
  89. #define I2C_FUNC_NOSTART 0x00000010 /* required for I2C_M_NOSTART */
  90. #define I2C_FUNC_SLAVE 0x00000020
  91. #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 or later */
  92. #define I2C_FUNC_SMBUS_QUICK 0x00010000
  93. #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
  94. #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
  95. #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
  96. #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
  97. #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
  98. #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
  99. #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
  100. #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 /* required for I2C_M_RECV_LEN */
  101. #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
  102. #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
  103. #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
  104. #define I2C_FUNC_SMBUS_HOST_NOTIFY 0x10000000 /* SMBus 2.0 or later */
  105. #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
  106. I2C_FUNC_SMBUS_WRITE_BYTE)
  107. #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
  108. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
  109. #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
  110. I2C_FUNC_SMBUS_WRITE_WORD_DATA)
  111. #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
  112. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
  113. #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
  114. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
  115. #define I2C_FUNC_SMBUS_EMUL (I2C_FUNC_SMBUS_QUICK | \
  116. I2C_FUNC_SMBUS_BYTE | \
  117. I2C_FUNC_SMBUS_BYTE_DATA | \
  118. I2C_FUNC_SMBUS_WORD_DATA | \
  119. I2C_FUNC_SMBUS_PROC_CALL | \
  120. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
  121. I2C_FUNC_SMBUS_I2C_BLOCK | \
  122. I2C_FUNC_SMBUS_PEC)
  123. /* if I2C_M_RECV_LEN is also supported */
  124. #define I2C_FUNC_SMBUS_EMUL_ALL (I2C_FUNC_SMBUS_EMUL | \
  125. I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
  126. I2C_FUNC_SMBUS_BLOCK_PROC_CALL)
  127. /*
  128. * Data for SMBus Messages
  129. */
  130. #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
  131. union i2c_smbus_data {
  132. __u8 byte;
  133. __u16 word;
  134. __u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
  135. /* and one more for user-space compatibility */
  136. };
  137. /* i2c_smbus_xfer read or write markers */
  138. #define I2C_SMBUS_READ 1
  139. #define I2C_SMBUS_WRITE 0
  140. /* SMBus transaction types (size parameter in the above functions)
  141. Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
  142. #define I2C_SMBUS_QUICK 0
  143. #define I2C_SMBUS_BYTE 1
  144. #define I2C_SMBUS_BYTE_DATA 2
  145. #define I2C_SMBUS_WORD_DATA 3
  146. #define I2C_SMBUS_PROC_CALL 4
  147. #define I2C_SMBUS_BLOCK_DATA 5
  148. #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
  149. #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
  150. #define I2C_SMBUS_I2C_BLOCK_DATA 8
  151. #endif /* _LINUX_I2C_H */