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

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