PD Buddy Sink Firmware
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.

fusb302b.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef PDB_FUSB302B_H
  19. #define PDB_FUSB302B_H
  20. #include <stdint.h>
  21. #include "messages.h"
  22. /* I2C address of the FUSB302B */
  23. #define FUSB302B_ADDR 0x22
  24. /* Device ID register */
  25. #define FUSB_DEVICE_ID 0x01
  26. #define FUSB_DEVICE_ID_VERSION_ID_SHIFT 4
  27. #define FUSB_DEVICE_ID_VERSION_ID (0xF << FUSB_DEVICE_ID_VERSION_ID_SHIFT)
  28. #define FUSB_DEVICE_ID_PRODUCT_ID_SHIFT 2
  29. #define FUSB_DEVICE_ID_PRODUCT_ID (0x3 << FUSB_DEVICE_ID_PRODUCT_ID_SHIFT)
  30. #define FUSB_DEVICE_ID_REVISION_ID_SHIFT 0
  31. #define FUSB_DEVICE_ID_REVISION_ID (0x3 << FUSB_DEVICE_ID_REVISION_ID_SHIFT)
  32. /* Switches0 register */
  33. #define FUSB_SWITCHES0 0x02
  34. #define FUSB_SWITCHES0_PU_EN2 (1 << 7)
  35. #define FUSB_SWITCHES0_PU_EN1 (1 << 6)
  36. #define FUSB_SWITCHES0_VCONN_CC2 (1 << 5)
  37. #define FUSB_SWITCHES0_VCONN_CC1 (1 << 4)
  38. #define FUSB_SWITCHES0_MEAS_CC2 (1 << 3)
  39. #define FUSB_SWITCHES0_MEAS_CC1 (1 << 2)
  40. #define FUSB_SWITCHES0_PDWN_2 (1 << 1)
  41. #define FUSB_SWITCHES0_PDWN_1 1
  42. /* Switches1 register */
  43. #define FUSB_SWITCHES1 0x03
  44. #define FUSB_SWITCHES1_POWERROLE (1 << 7)
  45. #define FUSB_SWITCHES1_SPECREV_SHIFT 5
  46. #define FUSB_SWITCHES1_SPECREV (0x3 << FUSB_SWITCHES1_SPECREV_SHIFT)
  47. #define FUSB_SWITCHES1_DATAROLE (1 << 4)
  48. #define FUSB_SWITCHES1_AUTO_CRC (1 << 2)
  49. #define FUSB_SWITCHES1_TXCC2 (1 << 1)
  50. #define FUSB_SWITCHES1_TXCC1 1
  51. /* Measure register */
  52. #define FUSB_MEASURE 0x04
  53. #define FUSB_MEASURE_MEAS_VBUS (1 << 6)
  54. #define FUSB_MEASURE_MDAC_SHIFT 0
  55. #define FUSB_MEASURE_MDAC (0x3F << FUSB_MEASURE_MDAC_SHIFT)
  56. /* Slice register */
  57. #define FUSB_SLICE 0x05
  58. #define FUSB_SLICE_SDAC_HYS_SHIFT 6
  59. #define FUSB_SLICE_SDAC_HYS (0x3 << FUSB_SLICE_SDAC_HYS_SHIFT)
  60. #define FUSB_SLICE_SDAC_SHIFT 0
  61. #define FUSB_SLICE_SDAC (0x3F << FUSB_SLICE_SDAC_SHIFT)
  62. /* Control0 register */
  63. #define FUSB_CONTROL0 0x06
  64. #define FUSB_CONTROL0_TX_FLUSH (1 << 6)
  65. #define FUSB_CONTROL0_INT_MASK (1 << 5)
  66. #define FUSB_CONTROL0_HOST_CUR_SHIFT 2
  67. #define FUSB_CONTROL0_HOST_CUR (0x3 << FUSB_CONTROL0_HOST_CUR_SHIFT)
  68. #define FUSB_CONTROL0_AUTO_PRE (1 << 1)
  69. #define FUSB_CONTROL0_TX_START 1
  70. /* Control1 register */
  71. #define FUSB_CONTROL1 0x07
  72. #define FUSB_CONTROL1_ENSOP2DB (1 << 6)
  73. #define FUSB_CONTROL1_ENSOP1DB (1 << 5)
  74. #define FUSB_CONTROL1_BIST_MODE2 (1 << 4)
  75. #define FUSB_CONTROL1_RX_FLUSH (1 << 2)
  76. #define FUSB_CONTROL1_ENSOP2 (1 << 1)
  77. #define FUSB_CONTROL1_ENSOP1 1
  78. /* Control2 register */
  79. #define FUSB_CONTROL2 0x08
  80. #define FUSB_CONTROL2_TOG_SAVE_PWR_SHIFT 6
  81. #define FUSB_CONTROL2_TOG_SAVE_PWR (0x3 << FUSB_CONTROL2_TOG_SAVE_PWR)
  82. #define FUSB_CONTROL2_TOG_RD_ONLY (1 << 5)
  83. #define FUSB_CONTROL2_WAKE_EN (1 << 3)
  84. #define FUSB_CONTROL2_MODE_SHIFT 1
  85. #define FUSB_CONTROL2_MODE (0x3 << FUSB_CONTROL2_MODE_SHIFT)
  86. #define FUSB_CONTROL2_TOGGLE 1
  87. /* Control3 register */
  88. #define FUSB_CONTROL3 0x09
  89. #define FUSB_CONTROL3_SEND_HARD_RESET (1 << 6)
  90. #define FUSB_CONTROL3_BIST_TMODE (1 << 5)
  91. #define FUSB_CONTROL3_AUTO_HARDRESET (1 << 4)
  92. #define FUSB_CONTROL3_AUTO_SOFTRESET (1 << 3)
  93. #define FUSB_CONTROL3_N_RETRIES_SHIFT 1
  94. #define FUSB_CONTROL3_N_RETRIES (0x3 << FUSB_CONTROL3_N_RETRIES_SHIFT)
  95. #define FUSB_CONTROL3_AUTO_RETRY 1
  96. /* Mask1 register */
  97. #define FUSB_MASK1 0x0A
  98. #define FUSB_MASK1_M_VBUSOK (1 << 7)
  99. #define FUSB_MASK1_M_ACTIVITY (1 << 6)
  100. #define FUSB_MASK1_M_COMP_CHNG (1 << 5)
  101. #define FUSB_MASK1_M_CRC_CHK (1 << 4)
  102. #define FUSB_MASK1_M_ALERT (1 << 3)
  103. #define FUSB_MASK1_M_WAKE (1 << 2)
  104. #define FUSB_MASK1_M_COLLISION (1 << 1)
  105. #define FUSB_MASK1_M_BC_LVL (1 << 0)
  106. /* Power register */
  107. #define FUSB_POWER 0x0B
  108. #define FUSB_POWER_PWR3 (1 << 3)
  109. #define FUSB_POWER_PWR2 (1 << 2)
  110. #define FUSB_POWER_PWR1 (1 << 1)
  111. #define FUSB_POWER_PWR0 1
  112. /* Reset register */
  113. #define FUSB_RESET 0x0C
  114. #define FUSB_RESET_PD_RESET (1 << 1)
  115. #define FUSB_RESET_SW_RES 1
  116. /* OCPreg register */
  117. #define FUSB_OCPREG 0x0D
  118. #define FUSB_OCPREG_OCP_RANGE (1 << 3)
  119. #define FUSB_OCPREG_OCP_CUR_SHIFT 0
  120. #define FUSB_OCPREG_OCP_CUR (0x7 << FUSB_OCPREG_OCP_CUR_SHIFT)
  121. /* Maska register */
  122. #define FUSB_MASKA 0x0E
  123. #define FUSB_MASKA_M_OCP_TEMP (1 << 7)
  124. #define FUSB_MASKA_M_TOGDONE (1 << 6)
  125. #define FUSB_MASKA_M_SOFTFAIL (1 << 5)
  126. #define FUSB_MASKA_M_RETRYFAIL (1 << 4)
  127. #define FUSB_MASKA_M_HARDSENT (1 << 3)
  128. #define FUSB_MASKA_M_TXSENT (1 << 2)
  129. #define FUSB_MASKA_M_SOFTRST (1 << 1)
  130. #define FUSB_MASKA_M_HARDRST 1
  131. /* Maskb register */
  132. #define FUSB_MASKB 0x0F
  133. #define FUSB_MASKB_M_GCRCSENT 1
  134. /* Control4 register */
  135. #define FUSB_CONTROL4 0x10
  136. #define FUSB_CONTROL4_TOG_EXIT_AUD 1
  137. /* Status0a register */
  138. #define FUSB_STATUS0A 0x3C
  139. #define FUSB_STATUS0A_SOFTFAIL (1 << 5)
  140. #define FUSB_STATUS0A_RETRYFAIL (1 << 4)
  141. #define FUSB_STATUS0A_POWER3 (1 << 3)
  142. #define FUSB_STATUS0A_POWER2 (1 << 2)
  143. #define FUSB_STATUS0A_SOFTRST (1 << 1)
  144. #define FUSB_STATUS0A_HARDRST 1
  145. /* Status1a register */
  146. #define FUSB_STATUS1A 0x3D
  147. #define FUSB_STATUS1A_TOGSS_SHIFT 3
  148. #define FUSB_STATUS1A_TOGSS (0x7 << FUSB_STATUS1A_TOGSS_SHIFT)
  149. #define FUSB_STATUS1A_RXSOP2DB (1 << 2)
  150. #define FUSB_STATUS1A_RXSOP1DB (1 << 1)
  151. #define FUSB_STATUS1A_RXSOP 1
  152. /* Interrupta register */
  153. #define FUSB_INTERRUPTA 0x3E
  154. #define FUSB_INTERRUPTA_I_OCP_TEMP (1 << 7)
  155. #define FUSB_INTERRUPTA_I_TOGDONE (1 << 6)
  156. #define FUSB_INTERRUPTA_I_SOFTFAIL (1 << 5)
  157. #define FUSB_INTERRUPTA_I_RETRYFAIL (1 << 4)
  158. #define FUSB_INTERRUPTA_I_HARDSENT (1 << 3)
  159. #define FUSB_INTERRUPTA_I_TXSENT (1 << 2)
  160. #define FUSB_INTERRUPTA_I_SOFTRST (1 << 1)
  161. #define FUSB_INTERRUPTA_I_HARDRST 1
  162. /* Interruptb register */
  163. #define FUSB_INTERRUPTB 0x3F
  164. #define FUSB_INTERRUPTB_I_GCRCSENT 1
  165. /* Status0 register */
  166. #define FUSB_STATUS0 0x40
  167. #define FUSB_STATUS0_VBUSOK (1 << 7)
  168. #define FUSB_STATUS0_ACTIVITY (1 << 6)
  169. #define FUSB_STATUS0_COMP (1 << 5)
  170. #define FUSB_STATUS0_CRC_CHK (1 << 4)
  171. #define FUSB_STATUS0_ALERT (1 << 3)
  172. #define FUSB_STATUS0_WAKE (1 << 2)
  173. #define FUSB_STATUS0_BC_LVL_SHIFT 0
  174. #define FUSB_STATUS0_BC_LVL (0x3 << FUSB_STATUS0_BC_LVL_SHIFT)
  175. /* Status1 register */
  176. #define FUSB_STATUS1 0x41
  177. #define FUSB_STATUS1_RXSOP2 (1 << 7)
  178. #define FUSB_STATUS1_RXSOP1 (1 << 6)
  179. #define FUSB_STATUS1_RX_EMPTY (1 << 5)
  180. #define FUSB_STATUS1_RX_FULL (1 << 4)
  181. #define FUSB_STATUS1_TX_EMPTY (1 << 3)
  182. #define FUSB_STATUS1_TX_FULL (1 << 2)
  183. #define FUSB_STATUS1_OVRTEMP (1 << 1)
  184. #define FUSB_STATUS1_OCP 1
  185. /* Interrupt register */
  186. #define FUSB_INTERRUPT 0x42
  187. #define FUSB_INTERRUPT_I_VBUSOK (1 << 7)
  188. #define FUSB_INTERRUPT_I_ACTIVITY (1 << 6)
  189. #define FUSB_INTERRUPT_I_COMP_CHNG (1 << 5)
  190. #define FUSB_INTERRUPT_I_CRC_CHK (1 << 4)
  191. #define FUSB_INTERRUPT_I_ALERT (1 << 3)
  192. #define FUSB_INTERRUPT_I_WAKE (1 << 2)
  193. #define FUSB_INTERRUPT_I_COLLISION (1 << 1)
  194. #define FUSB_INTERRUPT_I_BC_LVL 1
  195. /* FIFOs register */
  196. #define FUSB_FIFOS 0x43
  197. #define FUSB_FIFO_RX_TOKEN_BITS 0xE0
  198. #define FUSB_FIFO_RX_SOP 0xE0
  199. #define FUSB_FIFO_RX_SOP1 0xC0
  200. #define FUSB_FIFO_RX_SOP2 0xA0
  201. #define FUSB_FIFO_RX_SOP1DB 0x80
  202. #define FUSB_FIFO_RX_SOP2DB 0x60
  203. /*
  204. * FUSB status union
  205. *
  206. * Provides a nicer structure than just an array of uint8_t for working with
  207. * the FUSB302B status and interrupt flags.
  208. */
  209. union fusb_status {
  210. uint8_t bytes[7];
  211. struct {
  212. uint8_t status0a;
  213. uint8_t status1a;
  214. uint8_t interrupta;
  215. uint8_t interruptb;
  216. uint8_t status0;
  217. uint8_t status1;
  218. uint8_t interrupt;
  219. };
  220. };
  221. /*
  222. * FUSB Type-C Current level enum
  223. */
  224. enum fusb_typec_current {
  225. None = 0,
  226. Default = 1,
  227. OnePointFiveAmps = 2,
  228. ThreePointZeroAmps = 3
  229. };
  230. /* FUSB functions */
  231. /*
  232. * Send a USB Power Delivery message to the FUSB302B
  233. */
  234. void fusb_send_message(const union pd_msg *msg);
  235. /*
  236. * Read a USB Power Delivery message from the FUSB302B
  237. */
  238. uint8_t fusb_read_message(union pd_msg *msg);
  239. /*
  240. * Tell the FUSB302B to send a hard reset signal
  241. */
  242. void fusb_send_hardrst(void);
  243. /*
  244. * Read the FUSB302B status and interrupt flags into *status
  245. */
  246. void fusb_get_status(union fusb_status *status);
  247. /*
  248. * Read the FUSB302B BC_LVL as an enum fusb_typec_current
  249. */
  250. enum fusb_typec_current fusb_get_typec_current(void);
  251. /*
  252. * Initialization routine for the FUSB302B
  253. */
  254. void fusb_setup(void);
  255. /*
  256. * Reset the FUSB302B
  257. */
  258. void fusb_reset(void);
  259. #endif /* PDB_FUSB302B_H */