tcpm.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright 2015-2017 Google, Inc
  4. */
  5. #ifndef __LINUX_USB_TCPM_H
  6. #define __LINUX_USB_TCPM_H
  7. #include <linux/bitops.h>
  8. #include <linux/usb/typec.h>
  9. #include "pd.h"
  10. enum typec_cc_status {
  11. TYPEC_CC_OPEN,
  12. TYPEC_CC_RA,
  13. TYPEC_CC_RD,
  14. TYPEC_CC_RP_DEF,
  15. TYPEC_CC_RP_1_5,
  16. TYPEC_CC_RP_3_0,
  17. };
  18. /* Collision Avoidance */
  19. #define SINK_TX_NG TYPEC_CC_RP_1_5
  20. #define SINK_TX_OK TYPEC_CC_RP_3_0
  21. enum typec_cc_polarity {
  22. TYPEC_POLARITY_CC1,
  23. TYPEC_POLARITY_CC2,
  24. };
  25. /* Time to wait for TCPC to complete transmit */
  26. #define PD_T_TCPC_TX_TIMEOUT 100 /* in ms */
  27. #define PD_ROLE_SWAP_TIMEOUT (MSEC_PER_SEC * 10)
  28. #define PD_PPS_CTRL_TIMEOUT (MSEC_PER_SEC * 10)
  29. enum tcpm_transmit_status {
  30. TCPC_TX_SUCCESS = 0,
  31. TCPC_TX_DISCARDED = 1,
  32. TCPC_TX_FAILED = 2,
  33. };
  34. enum tcpm_transmit_type {
  35. TCPC_TX_SOP = 0,
  36. TCPC_TX_SOP_PRIME = 1,
  37. TCPC_TX_SOP_PRIME_PRIME = 2,
  38. TCPC_TX_SOP_DEBUG_PRIME = 3,
  39. TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
  40. TCPC_TX_HARD_RESET = 5,
  41. TCPC_TX_CABLE_RESET = 6,
  42. TCPC_TX_BIST_MODE_2 = 7
  43. };
  44. /* Mux state attributes */
  45. #define TCPC_MUX_USB_ENABLED BIT(0) /* USB enabled */
  46. #define TCPC_MUX_DP_ENABLED BIT(1) /* DP enabled */
  47. #define TCPC_MUX_POLARITY_INVERTED BIT(2) /* Polarity inverted */
  48. /**
  49. * struct tcpc_dev - Port configuration and callback functions
  50. * @fwnode: Pointer to port fwnode
  51. * @get_vbus: Called to read current VBUS state
  52. * @get_current_limit:
  53. * Optional; called by the tcpm core when configured as a snk
  54. * and cc=Rp-def. This allows the tcpm to provide a fallback
  55. * current-limit detection method for the cc=Rp-def case.
  56. * For example, some tcpcs may include BC1.2 charger detection
  57. * and use that in this case.
  58. * @set_cc: Called to set value of CC pins
  59. * @apply_rc: Optional; Needed to move TCPCI based chipset to APPLY_RC state
  60. * as stated by the TCPCI specification.
  61. * @get_cc: Called to read current CC pin values
  62. * @set_polarity:
  63. * Called to set polarity
  64. * @set_vconn: Called to enable or disable VCONN
  65. * @set_vbus: Called to enable or disable VBUS
  66. * @set_current_limit:
  67. * Optional; called to set current limit as negotiated
  68. * with partner.
  69. * @set_pd_rx: Called to enable or disable reception of PD messages
  70. * @set_roles: Called to set power and data roles
  71. * @start_toggling:
  72. * Optional; if supported by hardware, called to start dual-role
  73. * toggling or single-role connection detection. Toggling stops
  74. * automatically if a connection is established.
  75. * @try_role: Optional; called to set a preferred role
  76. * @pd_transmit:Called to transmit PD message
  77. * @set_bist_data: Turn on/off bist data mode for compliance testing
  78. * @enable_frs:
  79. * Optional; Called to enable/disable PD 3.0 fast role swap.
  80. * Enabling frs is accessory dependent as not all PD3.0
  81. * accessories support fast role swap.
  82. * @frs_sourcing_vbus:
  83. * Optional; Called to notify that vbus is now being sourced.
  84. * Low level drivers can perform chip specific operations, if any.
  85. * @enable_auto_vbus_discharge:
  86. * Optional; TCPCI spec based TCPC implementations can optionally
  87. * support hardware to autonomously dischrge vbus upon disconnecting
  88. * as sink or source. TCPM signals TCPC to enable the mechanism upon
  89. * entering connected state and signals disabling upon disconnect.
  90. * @set_auto_vbus_discharge_threshold:
  91. * Mandatory when enable_auto_vbus_discharge is implemented. TCPM
  92. * calls this function to allow lower levels drivers to program the
  93. * vbus threshold voltage below which the vbus discharge circuit
  94. * will be turned on. requested_vbus_voltage is set to 0 when vbus
  95. * is going to disappear knowingly i.e. during PR_SWAP and
  96. * HARD_RESET etc.
  97. * @is_vbus_vsafe0v:
  98. * Optional; TCPCI spec based TCPC implementations are expected to
  99. * detect VSAFE0V voltage level at vbus. When detection of VSAFE0V
  100. * is supported by TCPC, set this callback for TCPM to query
  101. * whether vbus is at VSAFE0V when needed.
  102. * Returns true when vbus is at VSAFE0V, false otherwise.
  103. * @set_partner_usb_comm_capable:
  104. * Optional; The USB Communications Capable bit indicates if port
  105. * partner is capable of communication over the USB data lines
  106. * (e.g. D+/- or SS Tx/Rx). Called to notify the status of the bit.
  107. * @check_contaminant:
  108. * Optional; The callback is called when CC pins report open status
  109. * at the end of the deboumce period or when the port is still
  110. * toggling. Chip level drivers are expected to check for contaminant
  111. * and call tcpm_clean_port when the port is clean.
  112. */
  113. struct tcpc_dev {
  114. struct fwnode_handle *fwnode;
  115. int (*init)(struct tcpc_dev *dev);
  116. int (*get_vbus)(struct tcpc_dev *dev);
  117. int (*get_current_limit)(struct tcpc_dev *dev);
  118. int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
  119. int (*apply_rc)(struct tcpc_dev *dev, enum typec_cc_status cc,
  120. enum typec_cc_polarity polarity);
  121. int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
  122. enum typec_cc_status *cc2);
  123. int (*set_polarity)(struct tcpc_dev *dev,
  124. enum typec_cc_polarity polarity);
  125. int (*set_vconn)(struct tcpc_dev *dev, bool on);
  126. int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
  127. int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
  128. int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
  129. int (*set_roles)(struct tcpc_dev *dev, bool attached,
  130. enum typec_role role, enum typec_data_role data);
  131. int (*start_toggling)(struct tcpc_dev *dev,
  132. enum typec_port_type port_type,
  133. enum typec_cc_status cc);
  134. int (*try_role)(struct tcpc_dev *dev, int role);
  135. int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
  136. const struct pd_message *msg, unsigned int negotiated_rev);
  137. int (*set_bist_data)(struct tcpc_dev *dev, bool on);
  138. int (*enable_frs)(struct tcpc_dev *dev, bool enable);
  139. void (*frs_sourcing_vbus)(struct tcpc_dev *dev);
  140. int (*enable_auto_vbus_discharge)(struct tcpc_dev *dev, bool enable);
  141. int (*set_auto_vbus_discharge_threshold)(struct tcpc_dev *dev, enum typec_pwr_opmode mode,
  142. bool pps_active, u32 requested_vbus_voltage);
  143. bool (*is_vbus_vsafe0v)(struct tcpc_dev *dev);
  144. void (*set_partner_usb_comm_capable)(struct tcpc_dev *dev, bool enable);
  145. void (*check_contaminant)(struct tcpc_dev *dev);
  146. };
  147. struct tcpm_port;
  148. struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
  149. void tcpm_unregister_port(struct tcpm_port *port);
  150. void tcpm_vbus_change(struct tcpm_port *port);
  151. void tcpm_cc_change(struct tcpm_port *port);
  152. void tcpm_sink_frs(struct tcpm_port *port);
  153. void tcpm_sourcing_vbus(struct tcpm_port *port);
  154. void tcpm_pd_receive(struct tcpm_port *port,
  155. const struct pd_message *msg);
  156. void tcpm_pd_transmit_complete(struct tcpm_port *port,
  157. enum tcpm_transmit_status status);
  158. void tcpm_pd_hard_reset(struct tcpm_port *port);
  159. void tcpm_tcpc_reset(struct tcpm_port *port);
  160. void tcpm_port_clean(struct tcpm_port *port);
  161. bool tcpm_port_is_toggling(struct tcpm_port *port);
  162. #endif /* __LINUX_USB_TCPM_H */