PD Buddy Sink Firmware
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pd.h 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_PD_H
  19. #define PDB_PD_H
  20. #include <ch.h>
  21. /*
  22. * Macros for working with USB Power Delivery messages.
  23. *
  24. * This file is mostly written from the PD Rev. 2.0 spec, but the header is
  25. * written from the Rev. 3.0 spec.
  26. */
  27. /*
  28. * PD Header
  29. */
  30. #define PD_HDR_MSGTYPE_SHIFT 0
  31. #define PD_HDR_MSGTYPE (0x1F << PD_HDR_MSGTYPE_SHIFT)
  32. #define PD_HDR_DATAROLE_SHIFT 5
  33. #define PD_HDR_DATAROLE (0x1 << PD_HDR_DATAROLE_SHIFT)
  34. #define PD_HDR_SPECREV_SHIFT 6
  35. #define PD_HDR_SPECREV (0x3 << PD_HDR_SPECREV_SHIFT)
  36. #define PD_HDR_POWERROLE_SHIFT 8
  37. #define PD_HDR_POWERROLE (1 << PD_HDR_POWERROLE_SHIFT)
  38. #define PD_HDR_MESSAGEID_SHIFT 9
  39. #define PD_HDR_MESSAGEID (0x7 << PD_HDR_MESSAGEID_SHIFT)
  40. #define PD_HDR_NUMOBJ_SHIFT 12
  41. #define PD_HDR_NUMOBJ (0x7 << PD_HDR_NUMOBJ_SHIFT)
  42. #define PD_HDR_EXT (1 << 15)
  43. /* Message types */
  44. #define PD_MSGTYPE_GET(msg) (((msg)->hdr & PD_HDR_MSGTYPE) >> PD_HDR_MSGTYPE_SHIFT)
  45. /* Control Message */
  46. #define PD_MSGTYPE_GOODCRC 0x01
  47. #define PD_MSGTYPE_GOTOMIN 0x02
  48. #define PD_MSGTYPE_ACCEPT 0x03
  49. #define PD_MSGTYPE_REJECT 0x04
  50. #define PD_MSGTYPE_PING 0x05
  51. #define PD_MSGTYPE_PS_RDY 0x06
  52. #define PD_MSGTYPE_GET_SOURCE_CAP 0x07
  53. #define PD_MSGTYPE_GET_SINK_CAP 0x08
  54. #define PD_MSGTYPE_DR_SWAP 0x09
  55. #define PD_MSGTYPE_PR_SWAP 0x0A
  56. #define PD_MSGTYPE_VCONN_SWAP 0x0B
  57. #define PD_MSGTYPE_WAIT 0x0C
  58. #define PD_MSGTYPE_SOFT_RESET 0x0D
  59. #define PD_MSGTYPE_NOT_SUPPORTED 0x10
  60. #define PD_MSGTYPE_GET_SOURCE_CAP_EXTENDED 0x11
  61. #define PD_MSGTYPE_GET_STATUS 0x12
  62. #define PD_MSGTYPE_FR_SWAP 0x13
  63. #define PD_MSGTYPE_GET_PPS_STATUS 0x14
  64. #define PD_MSGTYPE_GET_COUNTRY_CODES 0x15
  65. /* Data Message */
  66. #define PD_MSGTYPE_SOURCE_CAPABILITIES 0x01
  67. #define PD_MSGTYPE_REQUEST 0x02
  68. #define PD_MSGTYPE_BIST 0x03
  69. #define PD_MSGTYPE_SINK_CAPABILITIES 0x04
  70. #define PD_MSGTYPE_BATTERY_STATUS 0x05
  71. #define PD_MSGTYPE_ALERT 0x06
  72. #define PD_MSGTYPE_GET_COUNTRY_INFO 0x07
  73. #define PD_MSGTYPE_VENDOR_DEFINED 0x0F
  74. /* Extended Message */
  75. #define PD_MSGTYPE_SOURCE_CAPABILITIES_EXTENDED 0x01
  76. #define PD_MSGTYPE_STATUS 0x02
  77. #define PD_MSGTYPE_GET_BATTERY_CAP 0x03
  78. #define PD_MSGTYPE_GET_BATTERY_STATUS 0x04
  79. #define PD_MSGTYPE_BATTERY_CAPABILITIES 0x05
  80. #define PD_MSGTYPE_GET_MANUFACTURER_INFO 0x06
  81. #define PD_MSGTYPE_MANUFACTURER_INFO 0x07
  82. #define PD_MSGTYPE_SECURITY_REQUEST 0x08
  83. #define PD_MSGTYPE_SECURITY_RESPONSE 0x09
  84. #define PD_MSGTYPE_FIRMWARE_UPDATE_REQUEST 0x0A
  85. #define PD_MSGTYPE_FIRMWARE_UPDATE_RESPONSE 0x0B
  86. #define PD_MSGTYPE_PPS_STATUS 0x0C
  87. #define PD_MSGTYPE_COUNTRY_INFO 0x0D
  88. #define PD_MSGTYPE_COUNTRY_CODES 0x0E
  89. /* Data roles */
  90. #define PD_DATAROLE_UFP (0x0 << PD_HDR_DATAROLE_SHIFT)
  91. #define PD_DATAROLE_DFP (0x1 << PD_HDR_DATAROLE_SHIFT)
  92. /* Specification revisions */
  93. #define PD_SPECREV_1_0 (0x0 << PD_HDR_SPECREV_SHIFT)
  94. #define PD_SPECREV_2_0 (0x1 << PD_HDR_SPECREV_SHIFT)
  95. #define PD_SPECREV_3_0 (0x2 << PD_HDR_SPECREV_SHIFT)
  96. /* Port power roles */
  97. #define PD_POWERROLE_SINK (0x0 << PD_HDR_POWERROLE_SHIFT)
  98. #define PD_POWERROLE_SOURCE (0x1 << PD_HDR_POWERROLE_SHIFT)
  99. /* Message ID */
  100. #define PD_MESSAGEID_GET(msg) (((msg)->hdr & PD_HDR_MESSAGEID) >> PD_HDR_MESSAGEID_SHIFT)
  101. /* Number of data objects */
  102. #define PD_NUMOBJ(n) (((n) << PD_HDR_NUMOBJ_SHIFT) & PD_HDR_NUMOBJ)
  103. #define PD_NUMOBJ_GET(msg) (((msg)->hdr & PD_HDR_NUMOBJ) >> PD_HDR_NUMOBJ_SHIFT)
  104. /*
  105. * PD Power Data Object
  106. */
  107. #define PD_PDO_TYPE_SHIFT 30
  108. #define PD_PDO_TYPE (0x3 << PD_PDO_TYPE_SHIFT)
  109. /* PDO types */
  110. #define PD_PDO_TYPE_FIXED (0x0 << PD_PDO_TYPE_SHIFT)
  111. #define PD_PDO_TYPE_BATTERY (0x1 << PD_PDO_TYPE_SHIFT)
  112. #define PD_PDO_TYPE_VARIABLE (0x2 << PD_PDO_TYPE_SHIFT)
  113. /* PD Source Fixed PDO */
  114. #define PD_PDO_SRC_FIXED_DUAL_ROLE_PWR_SHIFT 29
  115. #define PD_PDO_SRC_FIXED_DUAL_ROLE_PWR (1 << PD_PDO_SRC_FIXED_DUAL_ROLE_PWR_SHIFT)
  116. #define PD_PDO_SRC_FIXED_USB_SUSPEND_SHIFT 28
  117. #define PD_PDO_SRC_FIXED_USB_SUSPEND (1 << PD_PDO_SRC_FIXED_USB_SUSPEND_SHIFT)
  118. #define PD_PDO_SRC_FIXED_UNCONSTRAINED_SHIFT 27
  119. #define PD_PDO_SRC_FIXED_UNCONSTRAINED (1 << PD_PDO_SRC_FIXED_UNCONSTRAINED_SHIFT)
  120. #define PD_PDO_SRC_FIXED_USB_COMMS_SHIFT 26
  121. #define PD_PDO_SRC_FIXED_USB_COMMS (1 << PD_PDO_SRC_FIXED_USB_COMMS_SHIFT)
  122. #define PD_PDO_SRC_FIXED_DUAL_ROLE_DATA_SHIFT 25
  123. #define PD_PDO_SRC_FIXED_DUAL_ROLE_DATA (1 << PD_PDO_SRC_FIXED_DUAL_ROLE_DATA_SHIFT)
  124. #define PD_PDO_SRC_FIXED_PEAK_CURRENT_SHIFT 20
  125. #define PD_PDO_SRC_FIXED_PEAK_CURRENT (0x3 << PD_PDO_SRC_FIXED_PEAK_CURRENT_SHIFT)
  126. #define PD_PDO_SRC_FIXED_VOLTAGE_SHIFT 10
  127. #define PD_PDO_SRC_FIXED_VOLTAGE (0x3FF << PD_PDO_SRC_FIXED_VOLTAGE_SHIFT)
  128. #define PD_PDO_SRC_FIXED_CURRENT_SHIFT 0
  129. #define PD_PDO_SRC_FIXED_CURRENT (0x3FF << PD_PDO_SRC_FIXED_CURRENT_SHIFT)
  130. /* PD Source Fixed PDO current */
  131. #define PD_PDO_SRC_FIXED_CURRENT_GET(msg, i) (((msg)->obj[(i)] & PD_PDO_SRC_FIXED_CURRENT) >> PD_PDO_SRC_FIXED_CURRENT_SHIFT)
  132. /* PD Source Fixed PDO voltage */
  133. #define PD_PDO_SRC_FIXED_VOLTAGE_GET(msg, i) (((msg)->obj[(i)] & PD_PDO_SRC_FIXED_VOLTAGE) >> PD_PDO_SRC_FIXED_VOLTAGE_SHIFT)
  134. /* TODO: other types of source PDO */
  135. /* PD Sink Fixed PDO */
  136. #define PD_PDO_SNK_FIXED_DUAL_ROLE_PWR_SHIFT 29
  137. #define PD_PDO_SNK_FIXED_DUAL_ROLE_PWR (1 << PD_PDO_SNK_FIXED_DUAL_ROLE_PWR_SHIFT)
  138. #define PD_PDO_SNK_FIXED_HIGHER_CAP_SHIFT 28
  139. #define PD_PDO_SNK_FIXED_HIGHER_CAP (1 << PD_PDO_SNK_FIXED_HIGHER_CAP_SHIFT)
  140. #define PD_PDO_SNK_FIXED_UNCONSTRAINED_SHIFT 27
  141. #define PD_PDO_SNK_FIXED_UNCONSTRAINED (1 << PD_PDO_SNK_FIXED_UNCONSTRAINED_SHIFT)
  142. #define PD_PDO_SNK_FIXED_USB_COMMS_SHIFT 26
  143. #define PD_PDO_SNK_FIXED_USB_COMMS (1 << PD_PDO_SNK_FIXED_USB_COMMS_SHIFT)
  144. #define PD_PDO_SNK_FIXED_DUAL_ROLE_DATA_SHIFT 25
  145. #define PD_PDO_SNK_FIXED_DUAL_ROLE_DATA (1 << PD_PDO_SNK_FIXED_DUAL_ROLE_DATA_SHIFT)
  146. #define PD_PDO_SNK_FIXED_VOLTAGE_SHIFT 10
  147. #define PD_PDO_SNK_FIXED_VOLTAGE (0x3FF << PD_PDO_SNK_FIXED_VOLTAGE_SHIFT)
  148. #define PD_PDO_SNK_FIXED_CURRENT_SHIFT 0
  149. #define PD_PDO_SNK_FIXED_CURRENT (0x3FF << PD_PDO_SNK_FIXED_CURRENT_SHIFT)
  150. /* PD Sink Fixed PDO current */
  151. #define PD_PDO_SNK_FIXED_CURRENT_SET(i) (((i) << PD_PDO_SNK_FIXED_CURRENT_SHIFT) & PD_PDO_SNK_FIXED_CURRENT)
  152. /* PD Sink Fixed PDO voltage */
  153. #define PD_PDO_SNK_FIXED_VOLTAGE_SET(v) (((v) << PD_PDO_SNK_FIXED_VOLTAGE_SHIFT) & PD_PDO_SNK_FIXED_VOLTAGE)
  154. /* TODO: other types of sink PDO */
  155. /*
  156. * PD Request Data Object
  157. */
  158. #define PD_RDO_OBJPOS_SHIFT 28
  159. #define PD_RDO_OBJPOS (0x7 << PD_RDO_OBJPOS_SHIFT)
  160. #define PD_RDO_GIVEBACK_SHIFT 27
  161. #define PD_RDO_GIVEBACK (1 << PD_RDO_GIVEBACK_SHIFT)
  162. #define PD_RDO_CAP_MISMATCH_SHIFT 26
  163. #define PD_RDO_CAP_MISMATCH (1 << PD_RDO_CAP_MISMATCH_SHIFT)
  164. #define PD_RDO_USB_COMMS_SHIFT 25
  165. #define PD_RDO_USB_COMMS (1 << PD_RDO_USB_COMMS_SHIFT)
  166. #define PD_RDO_NO_USB_SUSPEND_SHIFT 24
  167. #define PD_RDO_NO_USB_SUSPEND (1 << PD_RDO_NO_USB_SUSPEND_SHIFT)
  168. #define PD_RDO_OBJPOS_SET(i) (((i) << PD_RDO_OBJPOS_SHIFT) & PD_RDO_OBJPOS)
  169. /* Fixed and Variable RDO, no GiveBack support */
  170. #define PD_RDO_FV_CURRENT_SHIFT 10
  171. #define PD_RDO_FV_CURRENT (0x3FF << PD_RDO_FV_CURRENT_SHIFT)
  172. #define PD_RDO_FV_MAX_CURRENT_SHIFT 0
  173. #define PD_RDO_FV_MAX_CURRENT (0x3FF << PD_RDO_FV_MAX_CURRENT_SHIFT)
  174. #define PD_RDO_FV_CURRENT_SET(i) (((i) << PD_RDO_FV_CURRENT_SHIFT) & PD_RDO_FV_CURRENT)
  175. #define PD_RDO_FV_MAX_CURRENT_SET(i) (((i) << PD_RDO_FV_MAX_CURRENT_SHIFT) & PD_RDO_FV_MAX_CURRENT)
  176. /* Fixed and Variable RDO with GiveBack support */
  177. #define PD_RDO_FV_MIN_CURRENT_SHIFT 0
  178. #define PD_RDO_FV_MIN_CURRENT (0x3FF << PD_RDO_FV_MIN_CURRENT_SHIFT)
  179. #define PD_RDO_FV_MIN_CURRENT_SET(i) (((i) << PD_RDO_FV_MIN_CURRENT_SHIFT) & PD_RDO_FV_MIN_CURRENT)
  180. /* TODO: Battery RDOs */
  181. /*
  182. * Time values
  183. *
  184. * Where a range is specified, the middle of the range (rounded down to the
  185. * nearest millisecond) is used.
  186. */
  187. #define PD_T_HARD_RESET_COMPLETE MS2ST(4)
  188. #define PD_T_PS_TRANSITION MS2ST(500)
  189. #define PD_T_SENDER_RESPONSE MS2ST(27)
  190. #define PD_T_SINK_REQUEST MS2ST(100)
  191. #define PD_T_TYPEC_SINK_WAIT_CAP MS2ST(465)
  192. /* This is actually from Type-C, not Power Delivery, but who cares? */
  193. #define PD_T_PD_DEBOUNCE MS2ST(15)
  194. /*
  195. * Counter maximums
  196. */
  197. #define PD_N_HARD_RESET_COUNT 2
  198. /*
  199. * Unit conversions
  200. *
  201. * V: volt
  202. * CV: centivolt
  203. * MV: millivolt
  204. * PDV: Power Delivery voltage unit (50 mV)
  205. * A: ampere
  206. * CA: centiampere
  207. * MA: milliampere
  208. * PDI: Power Delivery current unit (10 mA)
  209. */
  210. #define PD_MV2PDV(mv) ((mv) / 50)
  211. #define PD_PDV2MV(pdv) ((pdv) * 50)
  212. #define PD_MA2PDI(ma) ((ma) / 10)
  213. #define PD_PDI2MA(pdi) ((pdi) * 10)
  214. /* Get portions of a PD voltage in more normal units */
  215. #define PD_PDV_V(pdv) ((pdv) / 20)
  216. #define PD_PDV_CV(pdv) (5 * ((pdv) % 20))
  217. /* Get portions of a PD current in more normal units */
  218. #define PD_PDI_A(pdv) ((pdv) / 100)
  219. #define PD_PDI_CA(pdv) ((pdv) % 100)
  220. /*
  221. * Unit constants
  222. */
  223. #define PD_MV_MIN 0
  224. #define PD_MV_MAX 20000
  225. #define PD_PDV_MIN PD_MV2PDV(PD_MV_MIN)
  226. #define PD_PDV_MAX PD_MV2PDV(PD_MV_MAX)
  227. #define PD_MA_MIN 0
  228. #define PD_MA_MAX 5000
  229. #define PD_PDI_MIN PD_MA2PDI(PD_MA_MIN)
  230. #define PD_PDI_MAX PD_MA2PDI(PD_MA_MAX)
  231. #endif /* PDB_PD_H */