blob: 36a15dcadc5385ccf59665d5c1534157c966a4bf [file] [log] [blame]
Guenter Roeckf0690a22017-04-27 14:09:56 -07001/*
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
22enum 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
31enum typec_cc_polarity {
32 TYPEC_POLARITY_CC1,
33 TYPEC_POLARITY_CC2,
34};
35
36/* Time to wait for TCPC to complete transmit */
Guenter Roeck9adf9f92017-08-10 21:15:43 -070037#define PD_T_TCPC_TX_TIMEOUT 100 /* in ms */
38#define PD_ROLE_SWAP_TIMEOUT (MSEC_PER_SEC * 10)
Adam Thomson2eadc332018-04-23 15:10:56 +010039#define PD_PPS_CTRL_TIMEOUT (MSEC_PER_SEC * 10)
Guenter Roeckf0690a22017-04-27 14:09:56 -070040
41enum tcpm_transmit_status {
42 TCPC_TX_SUCCESS = 0,
43 TCPC_TX_DISCARDED = 1,
44 TCPC_TX_FAILED = 2,
45};
46
47enum tcpm_transmit_type {
48 TCPC_TX_SOP = 0,
49 TCPC_TX_SOP_PRIME = 1,
50 TCPC_TX_SOP_PRIME_PRIME = 2,
51 TCPC_TX_SOP_DEBUG_PRIME = 3,
52 TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
53 TCPC_TX_HARD_RESET = 5,
54 TCPC_TX_CABLE_RESET = 6,
55 TCPC_TX_BIST_MODE_2 = 7
56};
57
Guenter Roeck98076fa2017-09-11 20:32:05 -070058/**
59 * struct tcpc_config - Port configuration
60 * @src_pdo: PDO parameters sent to port partner as response to
61 * PD_CTRL_GET_SOURCE_CAP message
62 * @nr_src_pdo: Number of entries in @src_pdo
63 * @snk_pdo: PDO parameters sent to partner as response to
64 * PD_CTRL_GET_SINK_CAP message
65 * @nr_snk_pdo: Number of entries in @snk_pdo
Guenter Roeck98076fa2017-09-11 20:32:05 -070066 * @operating_snk_mw:
67 * Required operating sink power in mW
68 * @type: Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
69 * TYPEC_PORT_DRP)
70 * @default_role:
71 * Default port role (TYPEC_SINK or TYPEC_SOURCE).
72 * Set to TYPEC_NO_PREFERRED_ROLE if no default role.
73 * @try_role_hw:True if try.{Src,Snk} is implemented in hardware
74 * @alt_modes: List of supported alternate modes
75 */
Guenter Roeckf0690a22017-04-27 14:09:56 -070076struct tcpc_config {
77 const u32 *src_pdo;
78 unsigned int nr_src_pdo;
79
80 const u32 *snk_pdo;
81 unsigned int nr_snk_pdo;
82
Guenter Roeck193a6802017-05-09 09:04:58 -070083 const u32 *snk_vdo;
84 unsigned int nr_snk_vdo;
85
Guenter Roeckf0690a22017-04-27 14:09:56 -070086 unsigned int operating_snk_mw;
87
88 enum typec_port_type type;
Heikki Krogerusceeb1622018-03-20 15:57:05 +030089 enum typec_port_data data;
Guenter Roeckf0690a22017-04-27 14:09:56 -070090 enum typec_role default_role;
91 bool try_role_hw; /* try.{src,snk} implemented in hardware */
Badhri Jagan Sridharan23b5f732018-10-01 12:45:00 -070092 bool self_powered; /* port belongs to a self powered device */
Guenter Roeckf0690a22017-04-27 14:09:56 -070093
Guenter Roeck3c41dbd2017-08-10 21:15:39 -070094 const struct typec_altmode_desc *alt_modes;
Guenter Roeckf0690a22017-04-27 14:09:56 -070095};
96
Guenter Roeckf0690a22017-04-27 14:09:56 -070097/* Mux state attributes */
98#define TCPC_MUX_USB_ENABLED BIT(0) /* USB enabled */
99#define TCPC_MUX_DP_ENABLED BIT(1) /* DP enabled */
100#define TCPC_MUX_POLARITY_INVERTED BIT(2) /* Polarity inverted */
101
Guenter Roeck98076fa2017-09-11 20:32:05 -0700102/**
103 * struct tcpc_dev - Port configuration and callback functions
104 * @config: Pointer to port configuration
Li Jun5e85a042018-06-27 07:45:22 +0800105 * @fwnode: Pointer to port fwnode
Guenter Roeck98076fa2017-09-11 20:32:05 -0700106 * @get_vbus: Called to read current VBUS state
107 * @get_current_limit:
108 * Optional; called by the tcpm core when configured as a snk
109 * and cc=Rp-def. This allows the tcpm to provide a fallback
110 * current-limit detection method for the cc=Rp-def case.
111 * For example, some tcpcs may include BC1.2 charger detection
112 * and use that in this case.
113 * @set_cc: Called to set value of CC pins
114 * @get_cc: Called to read current CC pin values
115 * @set_polarity:
116 * Called to set polarity
117 * @set_vconn: Called to enable or disable VCONN
118 * @set_vbus: Called to enable or disable VBUS
119 * @set_current_limit:
120 * Optional; called to set current limit as negotiated
121 * with partner.
122 * @set_pd_rx: Called to enable or disable reception of PD messages
123 * @set_roles: Called to set power and data roles
Hans de Goede7893f9e2019-04-16 22:07:52 +0200124 * @start_toggling:
125 * Optional; if supported by hardware, called to start dual-role
126 * toggling or single-role connection detection. Toggling stops
127 * automatically if a connection is established.
Guenter Roeck98076fa2017-09-11 20:32:05 -0700128 * @try_role: Optional; called to set a preferred role
129 * @pd_transmit:Called to transmit PD message
130 * @mux: Pointer to multiplexer data
131 */
Guenter Roeckf0690a22017-04-27 14:09:56 -0700132struct tcpc_dev {
133 const struct tcpc_config *config;
Li Jun5e85a042018-06-27 07:45:22 +0800134 struct fwnode_handle *fwnode;
Guenter Roeckf0690a22017-04-27 14:09:56 -0700135
136 int (*init)(struct tcpc_dev *dev);
137 int (*get_vbus)(struct tcpc_dev *dev);
Hans de Goedeea62cfc2017-08-30 11:48:05 +0200138 int (*get_current_limit)(struct tcpc_dev *dev);
Guenter Roeckf0690a22017-04-27 14:09:56 -0700139 int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
140 int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
141 enum typec_cc_status *cc2);
142 int (*set_polarity)(struct tcpc_dev *dev,
143 enum typec_cc_polarity polarity);
144 int (*set_vconn)(struct tcpc_dev *dev, bool on);
145 int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
146 int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
147 int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
148 int (*set_roles)(struct tcpc_dev *dev, bool attached,
149 enum typec_role role, enum typec_data_role data);
Hans de Goede7893f9e2019-04-16 22:07:52 +0200150 int (*start_toggling)(struct tcpc_dev *dev,
151 enum typec_port_type port_type,
152 enum typec_cc_status cc);
Guenter Roeckf0690a22017-04-27 14:09:56 -0700153 int (*try_role)(struct tcpc_dev *dev, int role);
154 int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
155 const struct pd_message *msg);
Guenter Roeckf0690a22017-04-27 14:09:56 -0700156};
157
158struct tcpm_port;
159
160struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
161void tcpm_unregister_port(struct tcpm_port *port);
162
Guenter Roeckf0690a22017-04-27 14:09:56 -0700163void tcpm_vbus_change(struct tcpm_port *port);
164void tcpm_cc_change(struct tcpm_port *port);
165void tcpm_pd_receive(struct tcpm_port *port,
166 const struct pd_message *msg);
167void tcpm_pd_transmit_complete(struct tcpm_port *port,
168 enum tcpm_transmit_status status);
169void tcpm_pd_hard_reset(struct tcpm_port *port);
170void tcpm_tcpc_reset(struct tcpm_port *port);
171
172#endif /* __LINUX_USB_TCPM_H */