blob: f516955a0cf44ddf6e1e2d8ae59a522f624e5ee0 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Guenter Roeckf0690a22017-04-27 14:09:56 -07002/*
3 * Copyright 2015-2017 Google, Inc
Guenter Roeckf0690a22017-04-27 14:09:56 -07004 */
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
13enum 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
22enum typec_cc_polarity {
23 TYPEC_POLARITY_CC1,
24 TYPEC_POLARITY_CC2,
25};
26
27/* Time to wait for TCPC to complete transmit */
Guenter Roeck9adf9f92017-08-10 21:15:43 -070028#define PD_T_TCPC_TX_TIMEOUT 100 /* in ms */
29#define PD_ROLE_SWAP_TIMEOUT (MSEC_PER_SEC * 10)
Adam Thomson2eadc332018-04-23 15:10:56 +010030#define PD_PPS_CTRL_TIMEOUT (MSEC_PER_SEC * 10)
Guenter Roeckf0690a22017-04-27 14:09:56 -070031
32enum tcpm_transmit_status {
33 TCPC_TX_SUCCESS = 0,
34 TCPC_TX_DISCARDED = 1,
35 TCPC_TX_FAILED = 2,
36};
37
38enum 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
Guenter Roeck98076fa2017-09-11 20:32:05 -070049/**
50 * struct tcpc_config - Port configuration
51 * @src_pdo: PDO parameters sent to port partner as response to
52 * PD_CTRL_GET_SOURCE_CAP message
53 * @nr_src_pdo: Number of entries in @src_pdo
54 * @snk_pdo: PDO parameters sent to partner as response to
55 * PD_CTRL_GET_SINK_CAP message
56 * @nr_snk_pdo: Number of entries in @snk_pdo
Guenter Roeck98076fa2017-09-11 20:32:05 -070057 * @operating_snk_mw:
58 * Required operating sink power in mW
59 * @type: Port type (TYPEC_PORT_DFP, TYPEC_PORT_UFP, or
60 * TYPEC_PORT_DRP)
61 * @default_role:
62 * Default port role (TYPEC_SINK or TYPEC_SOURCE).
63 * Set to TYPEC_NO_PREFERRED_ROLE if no default role.
64 * @try_role_hw:True if try.{Src,Snk} is implemented in hardware
65 * @alt_modes: List of supported alternate modes
66 */
Guenter Roeckf0690a22017-04-27 14:09:56 -070067struct tcpc_config {
68 const u32 *src_pdo;
69 unsigned int nr_src_pdo;
70
71 const u32 *snk_pdo;
72 unsigned int nr_snk_pdo;
73
Guenter Roeck193a6802017-05-09 09:04:58 -070074 const u32 *snk_vdo;
75 unsigned int nr_snk_vdo;
76
Guenter Roeckf0690a22017-04-27 14:09:56 -070077 unsigned int operating_snk_mw;
78
79 enum typec_port_type type;
Heikki Krogerusceeb1622018-03-20 15:57:05 +030080 enum typec_port_data data;
Guenter Roeckf0690a22017-04-27 14:09:56 -070081 enum typec_role default_role;
82 bool try_role_hw; /* try.{src,snk} implemented in hardware */
Badhri Jagan Sridharan23b5f732018-10-01 12:45:00 -070083 bool self_powered; /* port belongs to a self powered device */
Guenter Roeckf0690a22017-04-27 14:09:56 -070084
Guenter Roeck3c41dbd2017-08-10 21:15:39 -070085 const struct typec_altmode_desc *alt_modes;
Guenter Roeckf0690a22017-04-27 14:09:56 -070086};
87
Guenter Roeckf0690a22017-04-27 14:09:56 -070088/* Mux state attributes */
89#define TCPC_MUX_USB_ENABLED BIT(0) /* USB enabled */
90#define TCPC_MUX_DP_ENABLED BIT(1) /* DP enabled */
91#define TCPC_MUX_POLARITY_INVERTED BIT(2) /* Polarity inverted */
92
Guenter Roeck98076fa2017-09-11 20:32:05 -070093/**
94 * struct tcpc_dev - Port configuration and callback functions
95 * @config: Pointer to port configuration
Li Jun5e85a042018-06-27 07:45:22 +080096 * @fwnode: Pointer to port fwnode
Guenter Roeck98076fa2017-09-11 20:32:05 -070097 * @get_vbus: Called to read current VBUS state
98 * @get_current_limit:
99 * Optional; called by the tcpm core when configured as a snk
100 * and cc=Rp-def. This allows the tcpm to provide a fallback
101 * current-limit detection method for the cc=Rp-def case.
102 * For example, some tcpcs may include BC1.2 charger detection
103 * and use that in this case.
104 * @set_cc: Called to set value of CC pins
105 * @get_cc: Called to read current CC pin values
106 * @set_polarity:
107 * Called to set polarity
108 * @set_vconn: Called to enable or disable VCONN
109 * @set_vbus: Called to enable or disable VBUS
110 * @set_current_limit:
111 * Optional; called to set current limit as negotiated
112 * with partner.
113 * @set_pd_rx: Called to enable or disable reception of PD messages
114 * @set_roles: Called to set power and data roles
Hans de Goede7893f9e2019-04-16 22:07:52 +0200115 * @start_toggling:
116 * Optional; if supported by hardware, called to start dual-role
117 * toggling or single-role connection detection. Toggling stops
118 * automatically if a connection is established.
Guenter Roeck98076fa2017-09-11 20:32:05 -0700119 * @try_role: Optional; called to set a preferred role
120 * @pd_transmit:Called to transmit PD message
121 * @mux: Pointer to multiplexer data
122 */
Guenter Roeckf0690a22017-04-27 14:09:56 -0700123struct tcpc_dev {
124 const struct tcpc_config *config;
Li Jun5e85a042018-06-27 07:45:22 +0800125 struct fwnode_handle *fwnode;
Guenter Roeckf0690a22017-04-27 14:09:56 -0700126
127 int (*init)(struct tcpc_dev *dev);
128 int (*get_vbus)(struct tcpc_dev *dev);
Hans de Goedeea62cfc2017-08-30 11:48:05 +0200129 int (*get_current_limit)(struct tcpc_dev *dev);
Guenter Roeckf0690a22017-04-27 14:09:56 -0700130 int (*set_cc)(struct tcpc_dev *dev, enum typec_cc_status cc);
131 int (*get_cc)(struct tcpc_dev *dev, enum typec_cc_status *cc1,
132 enum typec_cc_status *cc2);
133 int (*set_polarity)(struct tcpc_dev *dev,
134 enum typec_cc_polarity polarity);
135 int (*set_vconn)(struct tcpc_dev *dev, bool on);
136 int (*set_vbus)(struct tcpc_dev *dev, bool on, bool charge);
137 int (*set_current_limit)(struct tcpc_dev *dev, u32 max_ma, u32 mv);
138 int (*set_pd_rx)(struct tcpc_dev *dev, bool on);
139 int (*set_roles)(struct tcpc_dev *dev, bool attached,
140 enum typec_role role, enum typec_data_role data);
Hans de Goede7893f9e2019-04-16 22:07:52 +0200141 int (*start_toggling)(struct tcpc_dev *dev,
142 enum typec_port_type port_type,
143 enum typec_cc_status cc);
Guenter Roeckf0690a22017-04-27 14:09:56 -0700144 int (*try_role)(struct tcpc_dev *dev, int role);
145 int (*pd_transmit)(struct tcpc_dev *dev, enum tcpm_transmit_type type,
146 const struct pd_message *msg);
Guenter Roeckf0690a22017-04-27 14:09:56 -0700147};
148
149struct tcpm_port;
150
151struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc);
152void tcpm_unregister_port(struct tcpm_port *port);
153
Guenter Roeckf0690a22017-04-27 14:09:56 -0700154void tcpm_vbus_change(struct tcpm_port *port);
155void tcpm_cc_change(struct tcpm_port *port);
156void tcpm_pd_receive(struct tcpm_port *port,
157 const struct pd_message *msg);
158void tcpm_pd_transmit_complete(struct tcpm_port *port,
159 enum tcpm_transmit_status status);
160void tcpm_pd_hard_reset(struct tcpm_port *port);
161void tcpm_tcpc_reset(struct tcpm_port *port);
162
163#endif /* __LINUX_USB_TCPM_H */