Merge remote-tracking branches 'asoc/topic/omap', 'asoc/topic/pxa', 'asoc/topic/rockc...
[sfrench/cifs-2.6.git] / drivers / staging / typec / tcpm.h
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
38
39 enum tcpm_transmit_status {
40         TCPC_TX_SUCCESS = 0,
41         TCPC_TX_DISCARDED = 1,
42         TCPC_TX_FAILED = 2,
43 };
44
45 enum tcpm_transmit_type {
46         TCPC_TX_SOP = 0,
47         TCPC_TX_SOP_PRIME = 1,
48         TCPC_TX_SOP_PRIME_PRIME = 2,
49         TCPC_TX_SOP_DEBUG_PRIME = 3,
50         TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
51         TCPC_TX_HARD_RESET = 5,
52         TCPC_TX_CABLE_RESET = 6,
53         TCPC_TX_BIST_MODE_2 = 7
54 };
55
56 struct tcpc_config {
57         const u32 *src_pdo;
58         unsigned int nr_src_pdo;
59
60         const u32 *snk_pdo;
61         unsigned int nr_snk_pdo;
62
63         const u32 *snk_vdo;
64         unsigned int nr_snk_vdo;
65
66         unsigned int max_snk_mv;
67         unsigned int max_snk_ma;
68         unsigned int max_snk_mw;
69         unsigned int operating_snk_mw;
70
71         enum typec_port_type type;
72         enum typec_role default_role;
73         bool try_role_hw;       /* try.{src,snk} implemented in hardware */
74
75         struct typec_altmode_desc *alt_modes;
76 };
77
78 enum tcpc_usb_switch {
79         TCPC_USB_SWITCH_CONNECT,
80         TCPC_USB_SWITCH_DISCONNECT,
81         TCPC_USB_SWITCH_RESTORE,        /* TODO FIXME */
82 };
83
84 /* Mux state attributes */
85 #define TCPC_MUX_USB_ENABLED            BIT(0)  /* USB enabled */
86 #define TCPC_MUX_DP_ENABLED             BIT(1)  /* DP enabled */
87 #define TCPC_MUX_POLARITY_INVERTED      BIT(2)  /* Polarity inverted */
88
89 /* Mux modes, decoded to attributes */
90 enum tcpc_mux_mode {
91         TYPEC_MUX_NONE  = 0,                            /* Open switch */
92         TYPEC_MUX_USB   = TCPC_MUX_USB_ENABLED,         /* USB only */
93         TYPEC_MUX_DP    = TCPC_MUX_DP_ENABLED,          /* DP only */
94         TYPEC_MUX_DOCK  = TCPC_MUX_USB_ENABLED |        /* Both USB and DP */
95                           TCPC_MUX_DP_ENABLED,
96 };
97
98 struct tcpc_mux_dev {
99         int (*set)(struct tcpc_mux_dev *dev, enum tcpc_mux_mode mux_mode,
100                    enum tcpc_usb_switch usb_config,
101                    enum typec_cc_polarity polarity);
102         bool dfp_only;
103         void *priv_data;
104 };
105
106 struct tcpc_dev {
107         const struct tcpc_config *config;
108
109         int (*init)(struct tcpc_dev *dev);
110         int (*get_vbus)(struct tcpc_dev *dev);
111         int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
112         int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
113                       enum typec_cc_status *cc2);
114         int (*set_polarity)(struct tcpc_dev *dev,
115                             enum typec_cc_polarity polarity);
116         int (*set_vconn)(struct tcpc_dev *dev, bool on);
117         int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
118         int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
119         int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
120         int (*set_roles)(struct tcpc_dev *dev, bool attached,
121                          enum typec_role role, enum typec_data_role data);
122         int (*start_drp_toggling)(struct tcpc_dev *dev,
123                                   enum typec_cc_status cc);
124         int (*try_role)(struct tcpc_dev *dev, int role);
125         int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
126                            const struct pd_message *msg);
127         struct tcpc_mux_dev *mux;
128 };
129
130 struct tcpm_port;
131
132 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
133 void tcpm_unregister_port(struct tcpm_port *port);
134
135 void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
136                                      unsigned int nr_pdo);
137 void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
138                                    unsigned int nr_pdo,
139                                    unsigned int max_snk_mv,
140                                    unsigned int max_snk_ma,
141                                    unsigned int max_snk_mw,
142                                    unsigned int operating_snk_mw);
143
144 void tcpm_vbus_change(struct tcpm_port *port);
145 void tcpm_cc_change(struct tcpm_port *port);
146 void tcpm_pd_receive(struct tcpm_port *port,
147                      const struct pd_message *msg);
148 void tcpm_pd_transmit_complete(struct tcpm_port *port,
149                                enum tcpm_transmit_status status);
150 void tcpm_pd_hard_reset(struct tcpm_port *port);
151 void tcpm_tcpc_reset(struct tcpm_port *port);
152
153 #endif /* __LINUX_USB_TCPM_H */