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