i2c.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2016 - 2021, Broadcom
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef I2C_H
  7. #define I2C_H
  8. #include <stdint.h>
  9. #define I2C_SPEED_100KHz 100000
  10. #define I2C_SPEED_400KHz 400000
  11. #define I2C_SPEED_DEFAULT I2C_SPEED_100KHz
  12. /*
  13. * Function Name: i2c_probe
  14. *
  15. * Description:
  16. * This function probes the I2C bus for the existence of the specified
  17. * device.
  18. *
  19. * Parameters:
  20. * bus_id - I2C bus ID
  21. * devaddr - Device Address
  22. *
  23. * Return:
  24. * 0 on success, or -1 on failure.
  25. */
  26. int i2c_probe(uint32_t bus_id, uint8_t devaddr);
  27. /*
  28. * Function Name: i2c_init
  29. *
  30. * Description:
  31. * This function initializes the SMBUS.
  32. *
  33. * Parameters:
  34. * bus_id - I2C bus ID
  35. * speed - I2C bus speed in Hz
  36. *
  37. * Return:
  38. * 0 on success, or -1 on failure.
  39. */
  40. int i2c_init(uint32_t bus_id, int speed);
  41. /*
  42. * Function Name: i2c_set_bus_speed
  43. *
  44. * Description:
  45. * This function configures the SMBUS speed
  46. *
  47. * Parameters:
  48. * bus_id - I2C bus ID
  49. * speed - I2C bus speed in Hz
  50. *
  51. * Return:
  52. * 0 on success, or -1 on failure.
  53. */
  54. int i2c_set_bus_speed(uint32_t bus_id, uint32_t speed);
  55. /*
  56. * Function Name: i2c_get_bus_speed
  57. *
  58. * Description:
  59. * This function returns the SMBUS speed.
  60. *
  61. * Parameters:
  62. * bus_id - I2C bus ID
  63. *
  64. * Return:
  65. * Bus speed in Hz, 0 on failure
  66. */
  67. uint32_t i2c_get_bus_speed(uint32_t bus_id);
  68. /*
  69. * Function Name: i2c_recv_byte
  70. *
  71. * Description:
  72. * This function reads I2C data from a device without specifying
  73. * a command regsiter.
  74. *
  75. * Parameters:
  76. * bus_id - I2C bus ID
  77. * devaddr - Device Address
  78. * value - Data Read
  79. *
  80. * Return:
  81. * 0 on success, or -1 on failure.
  82. */
  83. int i2c_recv_byte(uint32_t bus_id, uint8_t devaddr, uint8_t *value);
  84. /*
  85. * Function Name: i2c_send_byte
  86. *
  87. * Description:
  88. * This function send I2C data to a device without specifying
  89. * a command regsiter.
  90. *
  91. * Parameters:
  92. * bus_id - I2C bus ID
  93. * devaddr - Device Address
  94. * value - Data Send
  95. *
  96. * Return:
  97. * 0 on success, or -1 on failure.
  98. */
  99. int i2c_send_byte(uint32_t bus_id, uint8_t devaddr, uint8_t value);
  100. /*
  101. * Function Name: i2c_read
  102. *
  103. * Description:
  104. * This function reads I2C data from a device with a designated
  105. * command register
  106. *
  107. * Parameters:
  108. * bus_id - I2C bus ID
  109. * devaddr - Device Address
  110. * addr - Register Offset
  111. * alen - Address Length, 1 for byte, 2 for word (not supported)
  112. * buffer - Data Buffer
  113. * len - Data Length in bytes
  114. *
  115. * Return:
  116. * 0 on success, or -1 on failure.
  117. */
  118. int i2c_read(uint32_t bus_id,
  119. uint8_t devaddr,
  120. uint32_t addr,
  121. int alen,
  122. uint8_t *buffer,
  123. int len);
  124. /*
  125. * Function Name: i2c_write
  126. *
  127. * Description:
  128. * This function write I2C data to a device with a designated
  129. * command register
  130. *
  131. * Parameters:
  132. * bus_id - I2C bus ID
  133. * devaddr - Device Address
  134. * addr - Register Offset
  135. * alen - Address Length, 1 for byte, 2 for word (not supported)
  136. * buffer - Data Buffer
  137. * len - Data Length in bytes
  138. *
  139. * Return:
  140. * 0 on success, or -1 on failure.
  141. */
  142. int i2c_write(uint32_t bus_id,
  143. uint8_t devaddr,
  144. uint32_t addr,
  145. int alen,
  146. uint8_t *buffer,
  147. int len);
  148. #endif /* I2C_H */