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 8.8KB

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