1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright 2015-2017 Google, Inc
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 
22 enum typec_cc_polarity {
23 	TYPEC_POLARITY_CC1,
24 	TYPEC_POLARITY_CC2,
25 };
26 
27 /* Time to wait for TCPC to complete transmit */
28 #define PD_T_TCPC_TX_TIMEOUT	100		/* in ms	*/
29 #define PD_ROLE_SWAP_TIMEOUT	(MSEC_PER_SEC * 10)
30 #define PD_PPS_CTRL_TIMEOUT	(MSEC_PER_SEC * 10)
31 
32 enum tcpm_transmit_status {
33 	TCPC_TX_SUCCESS = 0,
34 	TCPC_TX_DISCARDED = 1,
35 	TCPC_TX_FAILED = 2,
36 };
37 
38 enum tcpm_transmit_type {
39 	TCPC_TX_SOP = 0,
40 	TCPC_TX_SOP_PRIME = 1,
41 	TCPC_TX_SOP_PRIME_PRIME = 2,
42 	TCPC_TX_SOP_DEBUG_PRIME = 3,
43 	TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
44 	TCPC_TX_HARD_RESET = 5,
45 	TCPC_TX_CABLE_RESET = 6,
46 	TCPC_TX_BIST_MODE_2 = 7
47 };
48 
49 /* Mux state attributes */
50 #define TCPC_MUX_USB_ENABLED		BIT(0)	/* USB enabled */
51 #define TCPC_MUX_DP_ENABLED		BIT(1)	/* DP enabled */
52 #define TCPC_MUX_POLARITY_INVERTED	BIT(2)	/* Polarity inverted */
53 
54 /**
55  * struct tcpc_dev - Port configuration and callback functions
56  * @fwnode:	Pointer to port fwnode
57  * @get_vbus:	Called to read current VBUS state
58  * @get_current_limit:
59  *		Optional; called by the tcpm core when configured as a snk
60  *		and cc=Rp-def. This allows the tcpm to provide a fallback
61  *		current-limit detection method for the cc=Rp-def case.
62  *		For example, some tcpcs may include BC1.2 charger detection
63  *		and use that in this case.
64  * @set_cc:	Called to set value of CC pins
65  * @get_cc:	Called to read current CC pin values
66  * @set_polarity:
67  *		Called to set polarity
68  * @set_vconn:	Called to enable or disable VCONN
69  * @set_vbus:	Called to enable or disable VBUS
70  * @set_current_limit:
71  *		Optional; called to set current limit as negotiated
72  *		with partner.
73  * @set_pd_rx:	Called to enable or disable reception of PD messages
74  * @set_roles:	Called to set power and data roles
75  * @start_toggling:
76  *		Optional; if supported by hardware, called to start dual-role
77  *		toggling or single-role connection detection. Toggling stops
78  *		automatically if a connection is established.
79  * @try_role:	Optional; called to set a preferred role
80  * @pd_transmit:Called to transmit PD message
81  * @set_bist_data: Turn on/off bist data mode for compliance testing
82  * @enable_frs:
83  *		Optional; Called to enable/disable PD 3.0 fast role swap.
84  *		Enabling frs is accessory dependent as not all PD3.0
85  *		accessories support fast role swap.
86  */
87 struct tcpc_dev {
88 	struct fwnode_handle *fwnode;
89 
90 	int (*init)(struct tcpc_dev *dev);
91 	int (*get_vbus)(struct tcpc_dev *dev);
92 	int (*get_current_limit)(struct tcpc_dev *dev);
93 	int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
94 	int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
95 		      enum typec_cc_status *cc2);
96 	int (*set_polarity)(struct tcpc_dev *dev,
97 			    enum typec_cc_polarity polarity);
98 	int (*set_vconn)(struct tcpc_dev *dev, bool on);
99 	int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
100 	int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
101 	int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
102 	int (*set_roles)(struct tcpc_dev *dev, bool attached,
103 			 enum typec_role role, enum typec_data_role data);
104 	int (*start_toggling)(struct tcpc_dev *dev,
105 			      enum typec_port_type port_type,
106 			      enum typec_cc_status cc);
107 	int (*try_role)(struct tcpc_dev *dev, int role);
108 	int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
109 			   const struct pd_message *msg);
110 	int (*set_bist_data)(struct tcpc_dev *dev, bool on);
111 	int (*enable_frs)(struct tcpc_dev *dev, bool enable);
112 };
113 
114 struct tcpm_port;
115 
116 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
117 void tcpm_unregister_port(struct tcpm_port *port);
118 
119 void tcpm_vbus_change(struct tcpm_port *port);
120 void tcpm_cc_change(struct tcpm_port *port);
121 void tcpm_sink_frs(struct tcpm_port *port);
122 void tcpm_sourcing_vbus(struct tcpm_port *port);
123 void tcpm_pd_receive(struct tcpm_port *port,
124 		     const struct pd_message *msg);
125 void tcpm_pd_transmit_complete(struct tcpm_port *port,
126 			       enum tcpm_transmit_status status);
127 void tcpm_pd_hard_reset(struct tcpm_port *port);
128 void tcpm_tcpc_reset(struct tcpm_port *port);
129 
130 #endif /* __LINUX_USB_TCPM_H */
131