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