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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /* If we have a fixed PDO, its V equals our desired V, and its I is
  56. * at least our desired I */
  57. if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
  58. && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == PD_MV2PDV(scfg->v)
  59. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i) {
  60. /* We got what we wanted, so build a request for that */
  61. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
  62. | PD_NUMOBJ(1);
  63. if (scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK) {
  64. /* GiveBack enabled */
  65. request->obj[0] = PD_RDO_FV_MIN_CURRENT_SET(DPM_MIN_CURRENT)
  66. | PD_RDO_FV_CURRENT_SET(scfg->i)
  67. | PD_RDO_NO_USB_SUSPEND | PD_RDO_GIVEBACK
  68. | PD_RDO_OBJPOS_SET(i + 1);
  69. } else {
  70. /* GiveBack disabled */
  71. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(scfg->i)
  72. | PD_RDO_FV_CURRENT_SET(scfg->i)
  73. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  74. }
  75. if (dpm_data->usb_comms) {
  76. request->obj[0] |= PD_RDO_USB_COMMS;
  77. }
  78. /* Update requested voltage */
  79. dpm_data->_requested_voltage = PD_PDV2MV(PD_MV2PDV(scfg->v));
  80. dpm_data->_capability_match = true;
  81. return true;
  82. }
  83. /* If we have a PPS APDO, our desired V lies within its range, and
  84. * its I is at least our desired I */
  85. if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED
  86. && (capabilities->obj[i] & PD_APDO_TYPE) == PD_APDO_TYPE_PPS
  87. && PD_APDO_SRC_PPS_MAX_VOLTAGE_GET(capabilities, i) >= PD_MV2PAV(scfg->v)
  88. && PD_APDO_SRC_PPS_MIN_VOLTAGE_GET(capabilities, i) <= PD_MV2PAV(scfg->v)
  89. && PD_APDO_SRC_PPS_CURRENT_GET(capabilities, i) >= PD_CA2PAI(scfg->i)) {
  90. /* We got what we wanted, so build a request for that */
  91. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
  92. | PD_NUMOBJ(1);
  93. /* Build a request */
  94. request->obj[0] = PD_RDO_PROG_CURRENT_SET(PD_CA2PAI(scfg->i))
  95. | PD_RDO_PROG_VOLTAGE_SET(PD_MV2PRV(scfg->v))
  96. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  97. if (dpm_data->usb_comms) {
  98. request->obj[0] |= PD_RDO_USB_COMMS;
  99. }
  100. /* Update requested voltage */
  101. dpm_data->_requested_voltage = PD_PRV2MV(PD_MV2PRV(scfg->v));
  102. dpm_data->_capability_match = true;
  103. return true;
  104. }
  105. }
  106. }
  107. /* Nothing matched (or no configuration), so get 5 V at low current */
  108. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST | PD_NUMOBJ(1);
  109. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(DPM_MIN_CURRENT)
  110. | PD_RDO_FV_CURRENT_SET(DPM_MIN_CURRENT)
  111. | PD_RDO_NO_USB_SUSPEND
  112. | PD_RDO_OBJPOS_SET(1);
  113. /* If the output is enabled and we got here, it must be a capability
  114. * mismatch. */
  115. if (dpm_data->output_enabled) {
  116. request->obj[0] |= PD_RDO_CAP_MISMATCH;
  117. }
  118. /* If we can do USB communications, tell the power supply */
  119. if (dpm_data->usb_comms) {
  120. request->obj[0] |= PD_RDO_USB_COMMS;
  121. }
  122. /* Update requested voltage */
  123. dpm_data->_requested_voltage = 5000;
  124. /* At this point, we have a capability match iff the output is disabled */
  125. dpm_data->_capability_match = !dpm_data->output_enabled;
  126. return !dpm_data->output_enabled;
  127. }
  128. void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
  129. {
  130. /* Keep track of how many PDOs we've added */
  131. int numobj = 0;
  132. /* Get the current configuration */
  133. struct pdbs_config *scfg = pdbs_config_flash_read();
  134. /* Cast the dpm_data to the right type */
  135. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  136. /* If we have no configuration or want something other than 5 V, add a PDO
  137. * for vSafe5V */
  138. if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  139. /* Minimum current, 5 V, and higher capability. */
  140. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  141. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(5000))
  142. | PD_PDO_SNK_FIXED_CURRENT_SET(DPM_MIN_CURRENT);
  143. }
  144. /* Add a PDO for the desired power. */
  145. if (scfg != NULL) {
  146. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  147. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(scfg->v))
  148. | PD_PDO_SNK_FIXED_CURRENT_SET(scfg->i);
  149. /* If we want more than 5 V, set the Higher Capability flag */
  150. if (PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  151. cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
  152. }
  153. }
  154. /* Set the unconstrained power flag. */
  155. if (dpm_data->_unconstrained_power) {
  156. cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
  157. }
  158. /* Set the USB communications capable flag. */
  159. if (dpm_data->usb_comms) {
  160. cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
  161. }
  162. /* Set the Sink_Capabilities message header */
  163. cap->hdr = cfg->pe.hdr_template | PD_MSGTYPE_SINK_CAPABILITIES
  164. | PD_NUMOBJ(numobj);
  165. }
  166. bool pdbs_dpm_giveback_enabled(struct pdb_config *cfg)
  167. {
  168. struct pdbs_config *scfg = pdbs_config_flash_read();
  169. return scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK;
  170. }
  171. bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
  172. enum fusb_typec_current tcc)
  173. {
  174. struct pdbs_config *scfg = pdbs_config_flash_read();
  175. /* Cast the dpm_data to the right type */
  176. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  177. /* We don't control the voltage anymore; it will always be 5 V. */
  178. dpm_data->_requested_voltage = 5000;
  179. /* Make the present Type-C Current advertisement available to the rest of
  180. * the DPM */
  181. dpm_data->typec_current = tcc;
  182. /* If we have no configuration or don't want 5 V, Type-C Current can't
  183. * possibly satisfy our needs */
  184. if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  185. dpm_data->_capability_match = false;
  186. return false;
  187. }
  188. /* If 1.5 A is available and we want no more than that, great. */
  189. if (tcc == fusb_tcc_1_5 && scfg->i <= 150) {
  190. dpm_data->_capability_match = true;
  191. return true;
  192. }
  193. /* If 3 A is available and we want no more than that, that's great too. */
  194. if (tcc == fusb_tcc_3_0 && scfg->i <= 300) {
  195. dpm_data->_capability_match = true;
  196. return true;
  197. }
  198. /* We're overly cautious if USB default current is available, since that
  199. * could mean different things depending on the port we're connected to,
  200. * and since we're really supposed to enumerate in order to request more
  201. * than 100 mA. This could be changed in the future. */
  202. dpm_data->_capability_match = false;
  203. return false;
  204. }
  205. void pdbs_dpm_pd_start(struct pdb_config *cfg)
  206. {
  207. /* Cast the dpm_data to the right type */
  208. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  209. if (dpm_data->led_pd_status) {
  210. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
  211. }
  212. }
  213. /*
  214. * Set the output state, with LED indication.
  215. */
  216. static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
  217. {
  218. /* Update the present voltage */
  219. dpm_data->_present_voltage = dpm_data->_requested_voltage;
  220. /* Set the power output */
  221. if (state && dpm_data->output_enabled) {
  222. /* Turn the output on */
  223. if (dpm_data->led_pd_status) {
  224. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_ON);
  225. }
  226. palSetLine(LINE_OUT_CTRL);
  227. } else {
  228. /* Turn the output off */
  229. if (dpm_data->led_pd_status) {
  230. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_OFF);
  231. }
  232. palClearLine(LINE_OUT_CTRL);
  233. }
  234. }
  235. void pdbs_dpm_transition_default(struct pdb_config *cfg)
  236. {
  237. /* Cast the dpm_data to the right type */
  238. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  239. /* Pretend we requested 5 V */
  240. dpm_data->_requested_voltage = 5000;
  241. /* Turn the output off */
  242. dpm_output_set(cfg->dpm_data, false);
  243. }
  244. void pdbs_dpm_transition_min(struct pdb_config *cfg)
  245. {
  246. dpm_output_set(cfg->dpm_data, false);
  247. }
  248. void pdbs_dpm_transition_standby(struct pdb_config *cfg)
  249. {
  250. /* Cast the dpm_data to the right type */
  251. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  252. /* If the voltage is changing, enter Sink Standby */
  253. if (dpm_data->_requested_voltage != dpm_data->_present_voltage) {
  254. /* For the PD Buddy Sink, entering Sink Standby is equivalent to
  255. * turning the output off. However, we don't want to change the LED
  256. * state for standby mode. */
  257. palClearLine(LINE_OUT_CTRL);
  258. }
  259. }
  260. void pdbs_dpm_transition_requested(struct pdb_config *cfg)
  261. {
  262. /* Cast the dpm_data to the right type */
  263. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  264. dpm_output_set(cfg->dpm_data, dpm_data->_capability_match);
  265. }