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

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