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