1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright 2015-2017 Google, Inc
4  * Copyright 2024 Collabora
5  */
6 
7 #ifndef __LINUX_USB_TCPM_H
8 #define __LINUX_USB_TCPM_H
9 
10 #include <dm/of.h>
11 #include <linux/bitops.h>
12 #include "pd.h"
13 
14 enum typec_orientation {
15 	TYPEC_ORIENTATION_NONE,
16 	TYPEC_ORIENTATION_NORMAL,
17 	TYPEC_ORIENTATION_REVERSE,
18 };
19 
20 enum typec_cc_status {
21 	TYPEC_CC_OPEN,
22 	TYPEC_CC_RA,
23 	TYPEC_CC_RD,
24 	TYPEC_CC_RP_DEF,
25 	TYPEC_CC_RP_1_5,
26 	TYPEC_CC_RP_3_0,
27 };
28 
29 enum typec_cc_polarity {
30 	TYPEC_POLARITY_CC1,
31 	TYPEC_POLARITY_CC2,
32 };
33 
34 enum tcpm_transmit_status {
35 	TCPC_TX_SUCCESS = 0,
36 	TCPC_TX_DISCARDED = 1,
37 	TCPC_TX_FAILED = 2,
38 };
39 
40 enum tcpm_transmit_type {
41 	TCPC_TX_SOP = 0,
42 	TCPC_TX_SOP_PRIME = 1,
43 	TCPC_TX_SOP_PRIME_PRIME = 2,
44 	TCPC_TX_SOP_DEBUG_PRIME = 3,
45 	TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
46 	TCPC_TX_HARD_RESET = 5,
47 	TCPC_TX_CABLE_RESET = 6,
48 	TCPC_TX_BIST_MODE_2 = 7
49 };
50 
51 struct dm_tcpm_ops {
52 	int (*get_connector_node)(struct udevice *dev, ofnode *connector_node);
53 	int (*init)(struct udevice *dev);
54 	int (*get_vbus)(struct udevice *dev);
55 	int (*set_cc)(struct udevice *dev, enum typec_cc_status cc);
56 	int (*get_cc)(struct udevice *dev, enum typec_cc_status *cc1,
57 		      enum typec_cc_status *cc2);
58 	int (*set_polarity)(struct udevice *dev,
59 			    enum typec_cc_polarity polarity);
60 	int (*set_vconn)(struct udevice *dev, bool on);
61 	int (*set_vbus)(struct udevice *dev, bool on, bool charge);
62 	int (*set_pd_rx)(struct udevice *dev, bool on);
63 	int (*set_roles)(struct udevice *dev, bool attached,
64 			 enum typec_role role, enum typec_data_role data);
65 	int (*start_toggling)(struct udevice *dev,
66 			      enum typec_port_type port_type,
67 			      enum typec_cc_status cc);
68 	int (*pd_transmit)(struct udevice *dev, enum tcpm_transmit_type type,
69 			   const struct pd_message *msg, unsigned int negotiated_rev);
70 	void (*poll_event)(struct udevice *dev);
71 	int (*enter_low_power_mode)(struct udevice *dev, bool attached, bool pd_capable);
72 };
73 
74 /* API for drivers */
75 void tcpm_vbus_change(struct udevice *dev);
76 void tcpm_cc_change(struct udevice *dev);
77 void tcpm_pd_receive(struct udevice *dev, const struct pd_message *msg);
78 void tcpm_pd_transmit_complete(struct udevice *dev,
79 			       enum tcpm_transmit_status status);
80 void tcpm_pd_hard_reset(struct udevice *dev);
81 
82 /* API for boards */
83 extern const char * const typec_pd_rev_name[];
84 extern const char * const typec_orientation_name[];
85 extern const char * const typec_role_name[];
86 extern const char * const typec_data_role_name[];
87 extern const char * const typec_cc_status_name[];
88 
89 int tcpm_get(int index, struct udevice **devp);
90 int tcpm_get_pd_rev(struct udevice *dev);
91 int tcpm_get_current(struct udevice *dev);
92 int tcpm_get_voltage(struct udevice *dev);
93 enum typec_orientation tcpm_get_orientation(struct udevice *dev);
94 enum typec_role tcpm_get_pwr_role(struct udevice *dev);
95 enum typec_data_role tcpm_get_data_role(struct udevice *dev);
96 bool tcpm_is_connected(struct udevice *dev);
97 const char *tcpm_get_state(struct udevice *dev);
98 
99 #endif /* __LINUX_USB_TCPM_H */
100