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