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.

device_policy_manager.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include "device_policy_manager.h"
  19. #include <stdint.h>
  20. #include <hal.h>
  21. #include "led.h"
  22. #include "config.h"
  23. #include "pd.h"
  24. bool pdb_dpm_output_enabled = true;
  25. bool pdb_dpm_led_pd_status = true;
  26. bool pdb_dpm_usb_comms = false;
  27. const union pd_msg *pdb_dpm_capabilities = NULL;
  28. enum fusb_typec_current pdb_dpm_typec_current = None;
  29. /* The current draw when the output is disabled */
  30. #define DPM_MIN_CURRENT PD_MA2PDI(100)
  31. /* Whether or not the power supply is unconstrained */
  32. static bool dpm_unconstrained_power;
  33. /* The last explicitly or implicitly negotiated voltage in PDV */
  34. static int dpm_present_voltage = PD_MV2PDV(5000);
  35. /* The requested voltage */
  36. static int dpm_requested_voltage;
  37. /* Whether our capabilities matched or not */
  38. static bool dpm_capability_match;
  39. bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
  40. const union pd_msg *capabilities, union pd_msg *request)
  41. {
  42. /* Update the stored Source_Capabilities */
  43. if (capabilities != NULL) {
  44. if (pdb_dpm_capabilities != NULL) {
  45. chPoolFree(&pdb_msg_pool, (union pd_msg *) pdb_dpm_capabilities);
  46. }
  47. pdb_dpm_capabilities = capabilities;
  48. } else {
  49. /* No new capabilities; use a shorter name for the stored ones. */
  50. capabilities = pdb_dpm_capabilities;
  51. }
  52. /* Get the current configuration */
  53. struct pdbs_config *scfg = pdbs_config_flash_read();
  54. /* Get the number of PDOs */
  55. uint8_t numobj = PD_NUMOBJ_GET(capabilities);
  56. /* Make the LED blink to indicate ongoing power negotiations */
  57. if (pdb_dpm_led_pd_status) {
  58. chEvtSignal(pdb_led_thread, PDB_EVT_LED_NEGOTIATING);
  59. }
  60. /* Get whether or not the power supply is constrained */
  61. dpm_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
  62. /* Make sure we have configuration */
  63. if (scfg != NULL && pdb_dpm_output_enabled) {
  64. /* Look at the PDOs to see if one matches our desires */
  65. for (uint8_t i = 0; i < numobj; i++) {
  66. /* Fixed Supply PDOs come first, so when we see a PDO that isn't a
  67. * Fixed Supply, stop reading. */
  68. if ((capabilities->obj[i] & PD_PDO_TYPE) != PD_PDO_TYPE_FIXED) {
  69. break;
  70. }
  71. /* If the V from the PDO equals our desired V and the I is at least
  72. * our desired I */
  73. if (PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == scfg->v
  74. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i) {
  75. /* We got what we wanted, so build a request for that */
  76. request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
  77. PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  78. if (scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK) {
  79. /* GiveBack enabled */
  80. request->obj[0] = PD_RDO_FV_MIN_CURRENT_SET(DPM_MIN_CURRENT)
  81. | PD_RDO_FV_CURRENT_SET(scfg->i)
  82. | PD_RDO_NO_USB_SUSPEND | PD_RDO_GIVEBACK
  83. | PD_RDO_OBJPOS_SET(i + 1);
  84. } else {
  85. /* GiveBack disabled */
  86. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(scfg->i)
  87. | PD_RDO_FV_CURRENT_SET(scfg->i)
  88. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  89. }
  90. if (pdb_dpm_usb_comms) {
  91. request->obj[0] |= PD_RDO_USB_COMMS;
  92. }
  93. /* Update requested voltage */
  94. dpm_requested_voltage = scfg->v;
  95. dpm_capability_match = true;
  96. return true;
  97. }
  98. }
  99. }
  100. /* Nothing matched (or no configuration), so get 5 V at low current */
  101. request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
  102. PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  103. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(DPM_MIN_CURRENT)
  104. | PD_RDO_FV_CURRENT_SET(DPM_MIN_CURRENT)
  105. | PD_RDO_NO_USB_SUSPEND
  106. | PD_RDO_OBJPOS_SET(1);
  107. /* If the output is enabled and we got here, it must be a capability
  108. * mismatch. */
  109. if (pdb_dpm_output_enabled) {
  110. request->obj[0] |= PD_RDO_CAP_MISMATCH;
  111. }
  112. /* If we can do USB communications, tell the power supply */
  113. if (pdb_dpm_usb_comms) {
  114. request->obj[0] |= PD_RDO_USB_COMMS;
  115. }
  116. /* Update requested voltage */
  117. dpm_requested_voltage = PD_MV2PDV(5000);
  118. /* At this point, we have a capability match iff the output is disabled */
  119. dpm_capability_match = !pdb_dpm_output_enabled;
  120. return !pdb_dpm_output_enabled;
  121. }
  122. void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
  123. {
  124. /* Keep track of how many PDOs we've added */
  125. int numobj = 0;
  126. /* Get the current configuration */
  127. struct pdbs_config *scfg = pdbs_config_flash_read();
  128. /* If we have no configuration or want something other than 5 V, add a PDO
  129. * for vSafe5V */
  130. if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
  131. /* Minimum current, 5 V, and higher capability. */
  132. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  133. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(5000))
  134. | PD_PDO_SNK_FIXED_CURRENT_SET(DPM_MIN_CURRENT);
  135. }
  136. /* Add a PDO for the desired power. */
  137. if (scfg != NULL) {
  138. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  139. | PD_PDO_SNK_FIXED_VOLTAGE_SET(scfg->v)
  140. | PD_PDO_SNK_FIXED_CURRENT_SET(scfg->i);
  141. /* If we want more than 5 V, set the Higher Capability flag */
  142. if (scfg->v != PD_MV2PDV(5000)) {
  143. cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
  144. }
  145. }
  146. /* Set the unconstrained power flag. */
  147. if (dpm_unconstrained_power) {
  148. cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
  149. }
  150. /* Set the USB communications capable flag. */
  151. if (pdb_dpm_usb_comms) {
  152. cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
  153. }
  154. /* Set the Sink_Capabilities message header */
  155. cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP | PD_SPECREV_2_0
  156. | PD_POWERROLE_SINK | PD_NUMOBJ(numobj);
  157. }
  158. bool pdbs_dpm_giveback_enabled(struct pdb_config *cfg)
  159. {
  160. struct pdbs_config *scfg = pdbs_config_flash_read();
  161. return scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK;
  162. }
  163. bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
  164. enum fusb_typec_current tcc)
  165. {
  166. struct pdbs_config *scfg = pdbs_config_flash_read();
  167. /* We don't control the voltage anymore; it will always be 5 V. */
  168. dpm_requested_voltage = PD_MV2PDV(5000);
  169. /* Make the present Type-C Current advertisement available to the rest of
  170. * the DPM */
  171. pdb_dpm_typec_current = tcc;
  172. /* If we have no configuration or don't want 5 V, Type-C Current can't
  173. * possibly satisfy our needs */
  174. if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
  175. dpm_capability_match = false;
  176. return false;
  177. }
  178. /* If 1.5 A is available and we want no more than that, great. */
  179. if (tcc == OnePointFiveAmps && scfg->i <= 150) {
  180. dpm_capability_match = true;
  181. return true;
  182. }
  183. /* If 3 A is available and we want no more than that, that's great too. */
  184. if (tcc == ThreePointZeroAmps && scfg->i <= 300) {
  185. dpm_capability_match = true;
  186. return true;
  187. }
  188. /* We're overly cautious if USB default current is available, since that
  189. * could mean different things depending on the port we're connected to,
  190. * and since we're really supposed to enumerate in order to request more
  191. * than 100 mA. This could be changed in the future. */
  192. dpm_capability_match = false;
  193. return false;
  194. }
  195. void pdbs_dpm_pd_start(struct pdb_config *cfg)
  196. {
  197. if (pdb_dpm_led_pd_status) {
  198. chEvtSignal(pdb_led_thread, PDB_EVT_LED_NEGOTIATING);
  199. }
  200. }
  201. /*
  202. * Set the output state, with LED indication.
  203. */
  204. static void dpm_output_set(bool state)
  205. {
  206. /* Update the present voltage */
  207. dpm_present_voltage = dpm_requested_voltage;
  208. /* Set the power output */
  209. if (state && pdb_dpm_output_enabled) {
  210. /* Turn the output on */
  211. if (pdb_dpm_led_pd_status) {
  212. chEvtSignal(pdb_led_thread, PDB_EVT_LED_OUTPUT_ON);
  213. }
  214. palSetLine(LINE_OUT_CTRL);
  215. } else {
  216. /* Turn the output off */
  217. if (pdb_dpm_led_pd_status) {
  218. chEvtSignal(pdb_led_thread, PDB_EVT_LED_OUTPUT_OFF);
  219. }
  220. palClearLine(LINE_OUT_CTRL);
  221. }
  222. }
  223. void pdbs_dpm_transition_default(struct pdb_config *cfg)
  224. {
  225. /* Pretend we requested 5 V */
  226. dpm_requested_voltage = PD_MV2PDV(5000);
  227. /* Turn the output off */
  228. dpm_output_set(false);
  229. }
  230. void pdbs_dpm_transition_min(struct pdb_config *cfg)
  231. {
  232. dpm_output_set(false);
  233. }
  234. void pdbs_dpm_transition_standby(struct pdb_config *cfg)
  235. {
  236. /* If the voltage is changing, enter Sink Standby */
  237. if (dpm_requested_voltage != dpm_present_voltage) {
  238. /* For the PD Buddy Sink, entering Sink Standby is equivalent to
  239. * turning the output off. However, we don't want to change the LED
  240. * state for standby mode. */
  241. palClearLine(LINE_OUT_CTRL);
  242. }
  243. }
  244. void pdbs_dpm_transition_requested(struct pdb_config *cfg)
  245. {
  246. dpm_output_set(dpm_capability_match);
  247. }