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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "led.h"
  22. #include "storage.h"
  23. #include "pd.h"
  24. #include "fusb302b.h"
  25. /* Whether or not the power supply is unconstrained */
  26. static bool dpm_unconstrained_power;
  27. /* The last explicitly or implicitly negotiated voltage in PDV */
  28. static int dpm_present_voltage = PD_MV2PDV(5000);
  29. /* The requested voltage */
  30. static int dpm_requested_voltage;
  31. bool pdb_dpm_evaluate_capability(const union pd_msg *capabilities, union pd_msg *request)
  32. {
  33. /* Get the current configuration */
  34. struct pdb_config *cfg = pdb_config_flash_read();
  35. /* Get the number of PDOs */
  36. uint8_t numobj = PD_NUMOBJ_GET(capabilities);
  37. /* Make the LED blink to indicate ongoing power negotiations */
  38. chEvtSignal(pdb_led_thread, PDB_EVT_LED_FAST_BLINK);
  39. /* Get whether or not the power supply is constrained */
  40. dpm_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
  41. /* Make sure we have configuration */
  42. if (cfg != NULL) {
  43. /* Look at the PDOs to see if one matches our desires */
  44. for (uint8_t i = 0; i < numobj; i++) {
  45. /* Fixed Supply PDOs come first, so when we see a PDO that isn't a
  46. * Fixed Supply, stop reading. */
  47. if ((capabilities->obj[i] & PD_PDO_TYPE) != PD_PDO_TYPE_FIXED) {
  48. break;
  49. }
  50. /* If the V from the PDO equals our desired V and the I is at least
  51. * our desired I */
  52. if (PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == cfg->v
  53. && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= cfg->i) {
  54. /* We got what we wanted, so build a request for that */
  55. request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
  56. PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  57. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(cfg->i)
  58. | PD_RDO_FV_CURRENT_SET(cfg->i)
  59. | PD_RDO_NO_USB_SUSPEND | PD_RDO_OBJPOS_SET(i + 1);
  60. /* Update requested voltage */
  61. dpm_requested_voltage = cfg->v;
  62. return true;
  63. }
  64. }
  65. }
  66. /* Nothing matched (or no configuration), so get 5 V */
  67. request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
  68. PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
  69. request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(10)
  70. | PD_RDO_FV_CURRENT_SET(10)
  71. | PD_RDO_NO_USB_SUSPEND | PD_RDO_CAP_MISMATCH
  72. | PD_RDO_OBJPOS_SET(1);
  73. /* Update requested voltage */
  74. dpm_requested_voltage = PD_MV2PDV(5000);
  75. return false;
  76. }
  77. void pdb_dpm_get_sink_capability(union pd_msg *cap)
  78. {
  79. /* Keep track of how many PDOs we've added */
  80. int numobj = 0;
  81. /* Get the current configuration */
  82. struct pdb_config *cfg = pdb_config_flash_read();
  83. /* If we have no configuration or want something other than 5 V, add a PDO
  84. * for vSafe5V */
  85. if (cfg == NULL || cfg->v != 100) {
  86. /* 100 mA, 5 V, and higher capability. */
  87. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  88. | PD_PDO_SNK_FIXED_VOLTAGE_SET(100)
  89. | PD_PDO_SNK_FIXED_CURRENT_SET(10);
  90. }
  91. /* Add a PDO for the desired power. */
  92. if (cfg != NULL) {
  93. cap->obj[numobj++] = PD_PDO_TYPE_FIXED
  94. | PD_PDO_SNK_FIXED_VOLTAGE_SET(cfg->v)
  95. | PD_PDO_SNK_FIXED_CURRENT_SET(cfg->i);
  96. /* If we want more than 5 V, set the Higher Capability flag */
  97. if (cfg->v != 100) {
  98. cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
  99. }
  100. }
  101. /* Set the unconstrained power flag. */
  102. if (dpm_unconstrained_power) {
  103. cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
  104. }
  105. /* Set the Sink_Capabilities message header */
  106. cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP | PD_SPECREV_2_0
  107. | PD_POWERROLE_SINK | PD_NUMOBJ(numobj);
  108. }
  109. bool pdb_dpm_giveback_enabled(void)
  110. {
  111. struct pdb_config *cfg = pdb_config_flash_read();
  112. return cfg->flags & PDB_CONFIG_FLAGS_GIVEBACK;
  113. }
  114. bool pdb_dpm_evaluate_typec_current(void)
  115. {
  116. static bool cfg_set = false;
  117. static struct pdb_config *cfg = NULL;
  118. /* Only get the configuration the first time this function runs, since its
  119. * location will never change without rebooting into setup mode. */
  120. if (!cfg_set) {
  121. cfg = pdb_config_flash_read();
  122. cfg_set = true;
  123. }
  124. /* We don't control the voltage anymore; it will always be 5 V. */
  125. dpm_requested_voltage = PD_MV2PDV(5000);
  126. /* If we have no configuration or don't want 5 V, Type-C Current can't
  127. * possibly satisfy our needs */
  128. if (cfg == NULL || cfg->v != 100) {
  129. return false;
  130. }
  131. /* Get the present Type-C Current advertisement */
  132. enum fusb_typec_current tcc = fusb_get_typec_current();
  133. /* If 1.5 A is available and we want no more than that, great. */
  134. if (tcc == OnePointFiveAmps && cfg->i <= 150) {
  135. return true;
  136. }
  137. /* If 3 A is available and we want no more than that, that's great too. */
  138. if (tcc == ThreePointZeroAmps && cfg->i <= 300) {
  139. return true;
  140. }
  141. /* We're overly cautious if USB default current is available, since that
  142. * could mean different things depending on the port we're connected to,
  143. * and since we're really supposed to enumerate in order to request more
  144. * than 100 mA. This could be changed in the future. */
  145. return false;
  146. }
  147. void pdb_dpm_pd_start(void)
  148. {
  149. chEvtSignal(pdb_led_thread, PDB_EVT_LED_FAST_BLINK);
  150. }
  151. void pdb_dpm_sink_standby(void)
  152. {
  153. /* If the voltage is changing, enter Sink Standby */
  154. if (dpm_requested_voltage != dpm_present_voltage) {
  155. /* For the PD Buddy Sink, entering Sink Standby is equivalent to
  156. * turning the output off. However, we don't want to change the LED
  157. * state for standby mode. */
  158. palClearLine(LINE_OUT_CTRL);
  159. }
  160. }
  161. void pdb_dpm_output_set(bool state)
  162. {
  163. /* Update the present voltage */
  164. dpm_present_voltage = dpm_requested_voltage;
  165. /* Set the power output */
  166. if (state) {
  167. /* Turn the output on */
  168. chEvtSignal(pdb_led_thread, PDB_EVT_LED_MEDIUM_BLINK_OFF);
  169. palSetLine(LINE_OUT_CTRL);
  170. } else {
  171. /* Turn the output off */
  172. chEvtSignal(pdb_led_thread, PDB_EVT_LED_ON);
  173. palClearLine(LINE_OUT_CTRL);
  174. }
  175. }
  176. void pdb_dpm_output_default(void)
  177. {
  178. /* Pretend we requested 5 V */
  179. dpm_requested_voltage = PD_MV2PDV(5000);
  180. /* Turn the output off */
  181. pdb_dpm_output_set(false);
  182. }