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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /* Look at the PDOs a second time to see if one falls in our voltage
  107. * range. */
  108. for (uint8_t i = 0; i < numobj; i++) {
  109. /* If we have a fixed PDO, its V equals our desired V, and its I is
  110. * at least our desired I */
  111. if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
  112. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i
  113. && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) >= PD_MV2PDV(scfg->vmin)
  114. && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) <= PD_MV2PDV(scfg->vmax)) {
  115. /* We got what we wanted, so build a request for that */
  116. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
  117. | PD_NUMOBJ(1);
  118. if (scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK) {
  119. /* GiveBack enabled */
  120. request->obj[0] = PD_RDO_FV_MIN_CURRENT_SET(DPM_MIN_CURRENT)
  121. | PD_RDO_FV_CURRENT_SET(scfg->i)
  122. | PD_RDO_NO_USB_SUSPEND | PD_RDO_GIVEBACK
  123. | PD_RDO_OBJPOS_SET(i + 1);
  124. } else {
  125. /* GiveBack disabled */
  126. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(scfg->i)
  127. | PD_RDO_FV_CURRENT_SET(scfg->i)
  128. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  129. }
  130. if (dpm_data->usb_comms) {
  131. request->obj[0] |= PD_RDO_USB_COMMS;
  132. }
  133. /* Update requested voltage */
  134. dpm_data->_requested_voltage = PD_PDV2MV(PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i));
  135. dpm_data->_capability_match = true;
  136. return true;
  137. }
  138. }
  139. }
  140. /* Nothing matched (or no configuration), so get 5 V at low current */
  141. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST | PD_NUMOBJ(1);
  142. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(DPM_MIN_CURRENT)
  143. | PD_RDO_FV_CURRENT_SET(DPM_MIN_CURRENT)
  144. | PD_RDO_NO_USB_SUSPEND
  145. | PD_RDO_OBJPOS_SET(1);
  146. /* If the output is enabled and we got here, it must be a capability
  147. * mismatch. */
  148. if (dpm_data->output_enabled) {
  149. request->obj[0] |= PD_RDO_CAP_MISMATCH;
  150. }
  151. /* If we can do USB communications, tell the power supply */
  152. if (dpm_data->usb_comms) {
  153. request->obj[0] |= PD_RDO_USB_COMMS;
  154. }
  155. /* Update requested voltage */
  156. dpm_data->_requested_voltage = 5000;
  157. /* At this point, we have a capability match iff the output is disabled */
  158. dpm_data->_capability_match = !dpm_data->output_enabled;
  159. return !dpm_data->output_enabled;
  160. }
  161. void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
  162. {
  163. /* Keep track of how many PDOs we've added */
  164. int numobj = 0;
  165. /* Get the current configuration */
  166. struct pdbs_config *scfg = pdbs_config_flash_read();
  167. /* Cast the dpm_data to the right type */
  168. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  169. /* If we have no configuration or want something other than 5 V, add a PDO
  170. * for vSafe5V */
  171. if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  172. /* Minimum current, 5 V, and higher capability. */
  173. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  174. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(5000))
  175. | PD_PDO_SNK_FIXED_CURRENT_SET(DPM_MIN_CURRENT);
  176. }
  177. /* Add a PDO for the desired power. */
  178. if (scfg != NULL) {
  179. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  180. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(scfg->v))
  181. | PD_PDO_SNK_FIXED_CURRENT_SET(scfg->i);
  182. /* If we want more than 5 V, set the Higher Capability flag */
  183. if (PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  184. cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
  185. }
  186. }
  187. /* Set the unconstrained power flag. */
  188. if (dpm_data->_unconstrained_power) {
  189. cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
  190. }
  191. /* Set the USB communications capable flag. */
  192. if (dpm_data->usb_comms) {
  193. cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
  194. }
  195. /* Set the Sink_Capabilities message header */
  196. cap->hdr = cfg->pe.hdr_template | PD_MSGTYPE_SINK_CAPABILITIES
  197. | PD_NUMOBJ(numobj);
  198. }
  199. bool pdbs_dpm_giveback_enabled(struct pdb_config *cfg)
  200. {
  201. struct pdbs_config *scfg = pdbs_config_flash_read();
  202. return scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK;
  203. }
  204. bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
  205. enum fusb_typec_current tcc)
  206. {
  207. struct pdbs_config *scfg = pdbs_config_flash_read();
  208. /* Cast the dpm_data to the right type */
  209. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  210. /* We don't control the voltage anymore; it will always be 5 V. */
  211. dpm_data->_requested_voltage = 5000;
  212. /* Make the present Type-C Current advertisement available to the rest of
  213. * the DPM */
  214. dpm_data->typec_current = tcc;
  215. /* If we have no configuration or don't want 5 V, Type-C Current can't
  216. * possibly satisfy our needs */
  217. if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  218. dpm_data->_capability_match = false;
  219. return false;
  220. }
  221. /* If 1.5 A is available and we want no more than that, great. */
  222. if (tcc == fusb_tcc_1_5 && scfg->i <= 150) {
  223. dpm_data->_capability_match = true;
  224. return true;
  225. }
  226. /* If 3 A is available and we want no more than that, that's great too. */
  227. if (tcc == fusb_tcc_3_0 && scfg->i <= 300) {
  228. dpm_data->_capability_match = true;
  229. return true;
  230. }
  231. /* We're overly cautious if USB default current is available, since that
  232. * could mean different things depending on the port we're connected to,
  233. * and since we're really supposed to enumerate in order to request more
  234. * than 100 mA. This could be changed in the future. */
  235. dpm_data->_capability_match = false;
  236. return false;
  237. }
  238. void pdbs_dpm_pd_start(struct pdb_config *cfg)
  239. {
  240. /* Cast the dpm_data to the right type */
  241. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  242. if (dpm_data->led_pd_status) {
  243. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
  244. }
  245. }
  246. /*
  247. * Set the output state, with LED indication.
  248. */
  249. static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
  250. {
  251. /* Update the present voltage */
  252. dpm_data->_present_voltage = dpm_data->_requested_voltage;
  253. /* Set the power output */
  254. if (state && dpm_data->output_enabled) {
  255. /* Turn the output on */
  256. if (dpm_data->led_pd_status) {
  257. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_ON);
  258. }
  259. palSetLine(LINE_OUT_CTRL);
  260. } else {
  261. /* Turn the output off */
  262. if (dpm_data->led_pd_status) {
  263. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_OFF);
  264. }
  265. palClearLine(LINE_OUT_CTRL);
  266. }
  267. }
  268. void pdbs_dpm_transition_default(struct pdb_config *cfg)
  269. {
  270. /* Cast the dpm_data to the right type */
  271. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  272. /* Pretend we requested 5 V */
  273. dpm_data->_requested_voltage = 5000;
  274. /* Turn the output off */
  275. dpm_output_set(cfg->dpm_data, false);
  276. }
  277. void pdbs_dpm_transition_min(struct pdb_config *cfg)
  278. {
  279. dpm_output_set(cfg->dpm_data, false);
  280. }
  281. void pdbs_dpm_transition_standby(struct pdb_config *cfg)
  282. {
  283. /* Cast the dpm_data to the right type */
  284. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  285. /* If the voltage is changing, enter Sink Standby */
  286. if (dpm_data->_requested_voltage != dpm_data->_present_voltage) {
  287. /* For the PD Buddy Sink, entering Sink Standby is equivalent to
  288. * turning the output off. However, we don't want to change the LED
  289. * state for standby mode. */
  290. palClearLine(LINE_OUT_CTRL);
  291. }
  292. }
  293. void pdbs_dpm_transition_requested(struct pdb_config *cfg)
  294. {
  295. /* Cast the dpm_data to the right type */
  296. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  297. dpm_output_set(cfg->dpm_data, dpm_data->_capability_match);
  298. }