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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017-2018 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. /*
  27. * Find the index of the first PDO from capabilities in the voltage range,
  28. * using the desired order.
  29. *
  30. * If there is no such PDO, returns -1 instead.
  31. */
  32. static int8_t dpm_get_range_fixed_pdo_index(const union pd_msg *capabilities,
  33. struct pdbs_config *scfg)
  34. {
  35. /* Get the number of PDOs */
  36. uint8_t numobj = PD_NUMOBJ_GET(capabilities);
  37. /* Get ready to iterate over the PDOs */
  38. int8_t i;
  39. int8_t step;
  40. if (scfg->flags & PDBS_CONFIG_FLAGS_HV_PREFERRED) {
  41. i = numobj - 1;
  42. step = -1;
  43. } else {
  44. i = 0;
  45. step = 1;
  46. }
  47. /* Look at the PDOs to see if one falls in our voltage range. */
  48. while (0 <= i && i < numobj) {
  49. /* If we have a fixed PDO, its V is within our range, and its I is at
  50. * least our desired I */
  51. if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
  52. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i
  53. && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) >= PD_MV2PDV(scfg->vmin)
  54. && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) <= PD_MV2PDV(scfg->vmax)) {
  55. return i;
  56. }
  57. i += step;
  58. }
  59. return -1;
  60. }
  61. bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
  62. const union pd_msg *capabilities, union pd_msg *request)
  63. {
  64. /* Cast the dpm_data to the right type */
  65. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  66. /* Update the stored Source_Capabilities */
  67. if (capabilities != NULL) {
  68. if (dpm_data->capabilities != NULL) {
  69. chPoolFree(&pdb_msg_pool, (union pd_msg *) dpm_data->capabilities);
  70. }
  71. dpm_data->capabilities = capabilities;
  72. } else {
  73. /* No new capabilities; use a shorter name for the stored ones. */
  74. capabilities = dpm_data->capabilities;
  75. }
  76. /* Get the current configuration */
  77. struct pdbs_config *scfg = pdbs_config_flash_read();
  78. /* Get the number of PDOs */
  79. uint8_t numobj = PD_NUMOBJ_GET(capabilities);
  80. /* Make the LED blink to indicate ongoing power negotiations */
  81. if (dpm_data->led_pd_status) {
  82. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
  83. }
  84. /* Get whether or not the power supply is constrained */
  85. dpm_data->_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
  86. /* Make sure we have configuration */
  87. if (scfg != NULL && dpm_data->output_enabled) {
  88. /* Look at the PDOs to see if one matches our desires */
  89. for (uint8_t i = 0; i < numobj; i++) {
  90. /* If we have a fixed PDO, its V equals our desired V, and its I is
  91. * at least our desired I */
  92. if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED
  93. && PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == PD_MV2PDV(scfg->v)
  94. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i) {
  95. /* We got what we wanted, so build a request for that */
  96. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
  97. | PD_NUMOBJ(1);
  98. if (scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK) {
  99. /* GiveBack enabled */
  100. request->obj[0] = PD_RDO_FV_MIN_CURRENT_SET(DPM_MIN_CURRENT)
  101. | PD_RDO_FV_CURRENT_SET(scfg->i)
  102. | PD_RDO_NO_USB_SUSPEND | PD_RDO_GIVEBACK
  103. | PD_RDO_OBJPOS_SET(i + 1);
  104. } else {
  105. /* GiveBack disabled */
  106. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(scfg->i)
  107. | PD_RDO_FV_CURRENT_SET(scfg->i)
  108. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  109. }
  110. if (dpm_data->usb_comms) {
  111. request->obj[0] |= PD_RDO_USB_COMMS;
  112. }
  113. /* Update requested voltage */
  114. dpm_data->_requested_voltage = PD_PDV2MV(PD_MV2PDV(scfg->v));
  115. dpm_data->_capability_match = true;
  116. return true;
  117. }
  118. /* If we have a PPS APDO, our desired V lies within its range, and
  119. * its I is at least our desired I */
  120. if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED
  121. && (capabilities->obj[i] & PD_APDO_TYPE) == PD_APDO_TYPE_PPS
  122. && PD_APDO_SRC_PPS_MAX_VOLTAGE_GET(capabilities, i) >= PD_MV2PAV(scfg->v)
  123. && PD_APDO_SRC_PPS_MIN_VOLTAGE_GET(capabilities, i) <= PD_MV2PAV(scfg->v)
  124. && PD_APDO_SRC_PPS_CURRENT_GET(capabilities, i) >= PD_CA2PAI(scfg->i)) {
  125. /* We got what we wanted, so build a request for that */
  126. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
  127. | PD_NUMOBJ(1);
  128. /* Build a request */
  129. request->obj[0] = PD_RDO_PROG_CURRENT_SET(PD_CA2PAI(scfg->i))
  130. | PD_RDO_PROG_VOLTAGE_SET(PD_MV2PRV(scfg->v))
  131. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  132. if (dpm_data->usb_comms) {
  133. request->obj[0] |= PD_RDO_USB_COMMS;
  134. }
  135. /* Update requested voltage */
  136. dpm_data->_requested_voltage = PD_PRV2MV(PD_MV2PRV(scfg->v));
  137. dpm_data->_capability_match = true;
  138. return true;
  139. }
  140. }
  141. /* If there's a PDO in the voltage range, use it */
  142. int8_t i = dpm_get_range_fixed_pdo_index(capabilities, scfg);
  143. if (i >= 0) {
  144. /* We got what we wanted, so build a request for that */
  145. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
  146. | PD_NUMOBJ(1);
  147. if (scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK) {
  148. /* GiveBack enabled */
  149. request->obj[0] = PD_RDO_FV_MIN_CURRENT_SET(DPM_MIN_CURRENT)
  150. | PD_RDO_FV_CURRENT_SET(scfg->i)
  151. | PD_RDO_NO_USB_SUSPEND | PD_RDO_GIVEBACK
  152. | PD_RDO_OBJPOS_SET(i + 1);
  153. } else {
  154. /* GiveBack disabled */
  155. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(scfg->i)
  156. | PD_RDO_FV_CURRENT_SET(scfg->i)
  157. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  158. }
  159. if (dpm_data->usb_comms) {
  160. request->obj[0] |= PD_RDO_USB_COMMS;
  161. }
  162. /* Update requested voltage */
  163. dpm_data->_requested_voltage = PD_PDV2MV(PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i));
  164. dpm_data->_capability_match = true;
  165. return true;
  166. }
  167. }
  168. /* Nothing matched (or no configuration), so get 5 V at low current */
  169. request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST | PD_NUMOBJ(1);
  170. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(DPM_MIN_CURRENT)
  171. | PD_RDO_FV_CURRENT_SET(DPM_MIN_CURRENT)
  172. | PD_RDO_NO_USB_SUSPEND
  173. | PD_RDO_OBJPOS_SET(1);
  174. /* If the output is enabled and we got here, it must be a capability
  175. * mismatch. */
  176. if (dpm_data->output_enabled) {
  177. request->obj[0] |= PD_RDO_CAP_MISMATCH;
  178. }
  179. /* If we can do USB communications, tell the power supply */
  180. if (dpm_data->usb_comms) {
  181. request->obj[0] |= PD_RDO_USB_COMMS;
  182. }
  183. /* Update requested voltage */
  184. dpm_data->_requested_voltage = 5000;
  185. /* At this point, we have a capability match iff the output is disabled */
  186. dpm_data->_capability_match = !dpm_data->output_enabled;
  187. return !dpm_data->output_enabled;
  188. }
  189. void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
  190. {
  191. /* Keep track of how many PDOs we've added */
  192. int numobj = 0;
  193. /* Get the current configuration */
  194. struct pdbs_config *scfg = pdbs_config_flash_read();
  195. /* Cast the dpm_data to the right type */
  196. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  197. /* If we have no configuration or want something other than 5 V, add a PDO
  198. * for vSafe5V */
  199. if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  200. /* Minimum current, 5 V, and higher capability. */
  201. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  202. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(5000))
  203. | PD_PDO_SNK_FIXED_CURRENT_SET(DPM_MIN_CURRENT);
  204. }
  205. /* Add a PDO for the desired power. */
  206. if (scfg != NULL) {
  207. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  208. | PD_PDO_SNK_FIXED_VOLTAGE_SET(PD_MV2PDV(scfg->v))
  209. | PD_PDO_SNK_FIXED_CURRENT_SET(scfg->i);
  210. /* If we want more than 5 V, set the Higher Capability flag */
  211. if (PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  212. cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
  213. }
  214. }
  215. /* Set the unconstrained power flag. */
  216. if (dpm_data->_unconstrained_power) {
  217. cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
  218. }
  219. /* Set the USB communications capable flag. */
  220. if (dpm_data->usb_comms) {
  221. cap->obj[0] |= PD_PDO_SNK_FIXED_USB_COMMS;
  222. }
  223. /* Set the Sink_Capabilities message header */
  224. cap->hdr = cfg->pe.hdr_template | PD_MSGTYPE_SINK_CAPABILITIES
  225. | PD_NUMOBJ(numobj);
  226. }
  227. bool pdbs_dpm_giveback_enabled(struct pdb_config *cfg)
  228. {
  229. struct pdbs_config *scfg = pdbs_config_flash_read();
  230. return scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK;
  231. }
  232. bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
  233. enum fusb_typec_current tcc)
  234. {
  235. struct pdbs_config *scfg = pdbs_config_flash_read();
  236. /* Cast the dpm_data to the right type */
  237. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  238. /* We don't control the voltage anymore; it will always be 5 V. */
  239. dpm_data->_requested_voltage = 5000;
  240. /* Make the present Type-C Current advertisement available to the rest of
  241. * the DPM */
  242. dpm_data->typec_current = tcc;
  243. /* If we have no configuration or don't want 5 V, Type-C Current can't
  244. * possibly satisfy our needs */
  245. if (scfg == NULL || PD_MV2PDV(scfg->v) != PD_MV2PDV(5000)) {
  246. dpm_data->_capability_match = false;
  247. return false;
  248. }
  249. /* If 1.5 A is available and we want no more than that, great. */
  250. if (tcc == fusb_tcc_1_5 && scfg->i <= 150) {
  251. dpm_data->_capability_match = true;
  252. return true;
  253. }
  254. /* If 3 A is available and we want no more than that, that's great too. */
  255. if (tcc == fusb_tcc_3_0 && scfg->i <= 300) {
  256. dpm_data->_capability_match = true;
  257. return true;
  258. }
  259. /* We're overly cautious if USB default current is available, since that
  260. * could mean different things depending on the port we're connected to,
  261. * and since we're really supposed to enumerate in order to request more
  262. * than 100 mA. This could be changed in the future. */
  263. dpm_data->_capability_match = false;
  264. return false;
  265. }
  266. void pdbs_dpm_pd_start(struct pdb_config *cfg)
  267. {
  268. /* Cast the dpm_data to the right type */
  269. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  270. if (dpm_data->led_pd_status) {
  271. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_NEGOTIATING);
  272. }
  273. }
  274. /*
  275. * Set the output state, with LED indication.
  276. */
  277. static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
  278. {
  279. /* Update the present voltage */
  280. dpm_data->_present_voltage = dpm_data->_requested_voltage;
  281. /* Set the power output */
  282. if (state && dpm_data->output_enabled) {
  283. /* Turn the output on */
  284. if (dpm_data->led_pd_status) {
  285. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_ON);
  286. }
  287. palSetLine(LINE_OUT_CTRL);
  288. } else {
  289. /* Turn the output off */
  290. if (dpm_data->led_pd_status) {
  291. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_OUTPUT_OFF);
  292. }
  293. palClearLine(LINE_OUT_CTRL);
  294. }
  295. }
  296. void pdbs_dpm_transition_default(struct pdb_config *cfg)
  297. {
  298. /* Cast the dpm_data to the right type */
  299. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  300. /* Pretend we requested 5 V */
  301. dpm_data->_requested_voltage = 5000;
  302. /* Turn the output off */
  303. dpm_output_set(cfg->dpm_data, false);
  304. }
  305. void pdbs_dpm_transition_min(struct pdb_config *cfg)
  306. {
  307. dpm_output_set(cfg->dpm_data, false);
  308. }
  309. void pdbs_dpm_transition_standby(struct pdb_config *cfg)
  310. {
  311. /* Cast the dpm_data to the right type */
  312. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  313. /* If the voltage is changing, enter Sink Standby */
  314. if (dpm_data->_requested_voltage != dpm_data->_present_voltage) {
  315. /* For the PD Buddy Sink, entering Sink Standby is equivalent to
  316. * turning the output off. However, we don't want to change the LED
  317. * state for standby mode. */
  318. palClearLine(LINE_OUT_CTRL);
  319. }
  320. }
  321. void pdbs_dpm_transition_requested(struct pdb_config *cfg)
  322. {
  323. /* Cast the dpm_data to the right type */
  324. struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
  325. dpm_output_set(cfg->dpm_data, dpm_data->_capability_match);
  326. }