tcpm.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2015-2017 Google, Inc
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef __LINUX_USB_TCPM_H
  15. #define __LINUX_USB_TCPM_H
  16. #include <linux/bitops.h>
  17. #include <linux/usb/typec.h>
  18. #include "pd.h"
  19. enum typec_cc_status {
  20. TYPEC_CC_OPEN,
  21. TYPEC_CC_RA,
  22. TYPEC_CC_RD,
  23. TYPEC_CC_RP_DEF,
  24. TYPEC_CC_RP_1_5,
  25. TYPEC_CC_RP_3_0,
  26. };
  27. enum typec_cc_polarity {
  28. TYPEC_POLARITY_CC1,
  29. TYPEC_POLARITY_CC2,
  30. };
  31. /* Time to wait for TCPC to complete transmit */
  32. #define PD_T_TCPC_TX_TIMEOUT 100 /* in ms */
  33. #define PD_ROLE_SWAP_TIMEOUT (MSEC_PER_SEC * 10)
  34. enum tcpm_transmit_status {
  35. TCPC_TX_SUCCESS = 0,
  36. TCPC_TX_DISCARDED = 1,
  37. TCPC_TX_FAILED = 2,
  38. };
  39. enum tcpm_transmit_type {
  40. TCPC_TX_SOP = 0,
  41. TCPC_TX_SOP_PRIME = 1,
  42. TCPC_TX_SOP_PRIME_PRIME = 2,
  43. TCPC_TX_SOP_DEBUG_PRIME = 3,
  44. TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
  45. TCPC_TX_HARD_RESET = 5,
  46. TCPC_TX_CABLE_RESET = 6,
  47. TCPC_TX_BIST_MODE_2 = 7
  48. };
  49. /**
  50. * struct tcpc_config - Port configuration
  51. * @src_pdo: PDO parameters sent to port partner as response to
  52. * PD_CTRL_GET_SOURCE_CAP message
  53. * @nr_src_pdo: Number of entries in @src_pdo
  54. * @snk_pdo: PDO parameters sent to partner as response to
  55. * PD_CTRL_GET_SINK_CAP message
  56. * @nr_snk_pdo: Number of entries in @snk_pdo
  57. * @max_snk_mv: Maximum acceptable sink voltage in mV
  58. * @max_snk_ma: Maximum sink current in mA
  59. * @max_snk_mw: Maximum required sink power in mW
  60. * @operating_snk_mw:
  61. * Required operating sink power in mW
  62. * @type: Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
  63. * TYPEC_PORT_DRP)
  64. * @default_role:
  65. * Default port role (TYPEC_SINK or TYPEC_SOURCE).
  66. * Set to TYPEC_NO_PREFERRED_ROLE if no default role.
  67. * @try_role_hw:True if try.{Src,Snk} is implemented in hardware
  68. * @alt_modes: List of supported alternate modes
  69. */
  70. struct tcpc_config {
  71. const u32 *src_pdo;
  72. unsigned int nr_src_pdo;
  73. const u32 *snk_pdo;
  74. unsigned int nr_snk_pdo;
  75. const u32 *snk_vdo;
  76. unsigned int nr_snk_vdo;
  77. unsigned int max_snk_mv;
  78. unsigned int max_snk_ma;
  79. unsigned int max_snk_mw;
  80. unsigned int operating_snk_mw;
  81. enum typec_port_type type;
  82. enum typec_role default_role;
  83. bool try_role_hw; /* try.{src,snk} implemented in hardware */
  84. const struct typec_altmode_desc *alt_modes;
  85. };
  86. enum tcpc_usb_switch {
  87. TCPC_USB_SWITCH_CONNECT,
  88. TCPC_USB_SWITCH_DISCONNECT,
  89. };
  90. /* Mux state attributes */
  91. #define TCPC_MUX_USB_ENABLED BIT(0) /* USB enabled */
  92. #define TCPC_MUX_DP_ENABLED BIT(1) /* DP enabled */
  93. #define TCPC_MUX_POLARITY_INVERTED BIT(2) /* Polarity inverted */
  94. /* Mux modes, decoded to attributes */
  95. enum tcpc_mux_mode {
  96. TYPEC_MUX_NONE = 0, /* Open switch */
  97. TYPEC_MUX_USB = TCPC_MUX_USB_ENABLED, /* USB only */
  98. TYPEC_MUX_DP = TCPC_MUX_DP_ENABLED, /* DP only */
  99. TYPEC_MUX_DOCK = TCPC_MUX_USB_ENABLED | /* Both USB and DP */
  100. TCPC_MUX_DP_ENABLED,
  101. };
  102. struct tcpc_mux_dev {
  103. int (*set)(struct tcpc_mux_dev *dev, enum tcpc_mux_mode mux_mode,
  104. enum tcpc_usb_switch usb_config,
  105. enum typec_cc_polarity polarity);
  106. bool dfp_only;
  107. void *priv_data;
  108. };
  109. /**
  110. * struct tcpc_dev - Port configuration and callback functions
  111. * @config: Pointer to port configuration
  112. * @get_vbus: Called to read current VBUS state
  113. * @get_current_limit:
  114. * Optional; called by the tcpm core when configured as a snk
  115. * and cc=Rp-def. This allows the tcpm to provide a fallback
  116. * current-limit detection method for the cc=Rp-def case.
  117. * For example, some tcpcs may include BC1.2 charger detection
  118. * and use that in this case.
  119. * @set_cc: Called to set value of CC pins
  120. * @get_cc: Called to read current CC pin values
  121. * @set_polarity:
  122. * Called to set polarity
  123. * @set_vconn: Called to enable or disable VCONN
  124. * @set_vbus: Called to enable or disable VBUS
  125. * @set_current_limit:
  126. * Optional; called to set current limit as negotiated
  127. * with partner.
  128. * @set_pd_rx: Called to enable or disable reception of PD messages
  129. * @set_roles: Called to set power and data roles
  130. * @start_drp_toggling:
  131. * Optional; if supported by hardware, called to start DRP
  132. * toggling. DRP toggling is stopped automatically if
  133. * a connection is established.
  134. * @try_role: Optional; called to set a preferred role
  135. * @pd_transmit:Called to transmit PD message
  136. * @mux: Pointer to multiplexer data
  137. */
  138. struct tcpc_dev {
  139. const struct tcpc_config *config;
  140. int (*init)(struct tcpc_dev *dev);
  141. int (*get_vbus)(struct tcpc_dev *dev);
  142. int (*get_current_limit)(struct tcpc_dev *dev);
  143. int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
  144. int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
  145. enum typec_cc_status *cc2);
  146. int (*set_polarity)(struct tcpc_dev *dev,
  147. enum typec_cc_polarity polarity);
  148. int (*set_vconn)(struct tcpc_dev *dev, bool on);
  149. int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
  150. int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
  151. int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
  152. int (*set_roles)(struct tcpc_dev *dev, bool attached,
  153. enum typec_role role, enum typec_data_role data);
  154. int (*start_drp_toggling)(struct tcpc_dev *dev,
  155. enum typec_cc_status cc);
  156. int (*try_role)(struct tcpc_dev *dev, int role);
  157. int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
  158. const struct pd_message *msg);
  159. struct tcpc_mux_dev *mux;
  160. };
  161. struct tcpm_port;
  162. struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
  163. void tcpm_unregister_port(struct tcpm_port *port);
  164. void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
  165. unsigned int nr_pdo);
  166. void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
  167. unsigned int nr_pdo,
  168. unsigned int max_snk_mv,
  169. unsigned int max_snk_ma,
  170. unsigned int max_snk_mw,
  171. unsigned int operating_snk_mw);
  172. void tcpm_vbus_change(struct tcpm_port *port);
  173. void tcpm_cc_change(struct tcpm_port *port);
  174. void tcpm_pd_receive(struct tcpm_port *port,
  175. const struct pd_message *msg);
  176. void tcpm_pd_transmit_complete(struct tcpm_port *port,
  177. enum tcpm_transmit_status status);
  178. void tcpm_pd_hard_reset(struct tcpm_port *port);
  179. void tcpm_tcpc_reset(struct tcpm_port *port);
  180. #endif /* __LINUX_USB_TCPM_H */