You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i2c.h 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef _GPXE_I2C_H
  2. #define _GPXE_I2C_H
  3. /** @file
  4. *
  5. * I2C interface
  6. *
  7. */
  8. #include <stdint.h>
  9. /** An I2C device
  10. *
  11. * An I2C device represents a specific slave device on an I2C bus. It
  12. * is accessed via an I2C interface.
  13. */
  14. struct i2c_device {
  15. /** Address of this device */
  16. unsigned int address;
  17. /** Flag indicating a ten-bit address format */
  18. int tenbit;
  19. };
  20. /** An I2C interface
  21. *
  22. * An I2C interface provides access to an I2C bus, via which I2C
  23. * devices may be reached.
  24. */
  25. struct i2c_interface {
  26. /**
  27. * Read data from I2C device
  28. *
  29. * @v i2c I2C interface
  30. * @v i2cdev I2C device
  31. * @v offset Starting offset within the device
  32. * @v data Data buffer
  33. * @v len Length of data buffer
  34. * @ret rc Return status code
  35. */
  36. int ( * read ) ( struct i2c_interface *i2c, struct i2c_device *i2cdev,
  37. unsigned int offset, uint8_t *data,
  38. unsigned int len );
  39. /**
  40. * Write data to I2C device
  41. *
  42. * @v i2c I2C interface
  43. * @v i2cdev I2C device
  44. * @v offset Starting offset within the device
  45. * @v data Data buffer
  46. * @v len Length of data buffer
  47. * @ret rc Return status code
  48. */
  49. int ( * write ) ( struct i2c_interface *i2c, struct i2c_device *i2cdev,
  50. unsigned int offset, const uint8_t *data,
  51. unsigned int len );
  52. };
  53. /** A bit-bashing I2C interface
  54. *
  55. * This provides a standardised way to construct I2C buses via a
  56. * bit-bashing interface.
  57. */
  58. struct i2c_bit_basher {
  59. /** I2C interface */
  60. struct i2c_interface i2c;
  61. /** Bit-bashing interface */
  62. struct bit_basher basher;
  63. };
  64. /** Ten-bit address marker
  65. *
  66. * This value is ORed with the I2C device address to indicate a
  67. * ten-bit address format on the bus.
  68. */
  69. #define I2C_TENBIT_ADDRESS 0x7800
  70. /** An I2C write command */
  71. #define I2C_WRITE 0
  72. /** An I2C read command */
  73. #define I2C_READ 1
  74. /** Bit indices used for I2C bit-bashing interface */
  75. enum {
  76. /** Serial clock */
  77. I2C_BIT_SCL = 0,
  78. /** Serial data */
  79. I2C_BIT_SDA,
  80. };
  81. /** Delay required for bit-bashing operation */
  82. #define I2C_UDELAY 5
  83. /**
  84. * Check presence of I2C device
  85. *
  86. * @v i2c I2C interface
  87. * @v i2cdev I2C device
  88. * @ret rc Return status code
  89. *
  90. * Checks for the presence of the device on the I2C bus by attempting
  91. * a zero-length write.
  92. */
  93. static inline int i2c_check_presence ( struct i2c_interface *i2c,
  94. struct i2c_device *i2cdev ) {
  95. return i2c->write ( i2c, i2cdev, 0, NULL, 0 );
  96. }
  97. extern void init_i2c_bit_basher ( struct i2c_bit_basher *i2cbit );
  98. #endif /* _GPXE_I2C_H */