summaryrefslogtreecommitdiff
path: root/include/usb_tc_sm.h
blob: ec6473edfd0590d48108ee21642c7a194004010a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Copyright 2019 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* USB Type-C module */

#ifndef __CROS_EC_USB_TC_H
#define __CROS_EC_USB_TC_H

#include "usb_sm.h"
#include "usb_pd.h"
#include "usb_pd_tcpm.h"

enum try_src_override_t {
	TRY_SRC_OVERRIDE_OFF,
	TRY_SRC_OVERRIDE_ON,
	TRY_SRC_NO_OVERRIDE
};

/*
 * Type C supply voltage (mV)
 *
 * This is the maximum voltage a sink can request
 * while charging.
 */
#define TYPE_C_VOLTAGE 5000 /* mV */

/*
 * Type C default sink current (mA)
 *
 * This is the maximum current a sink can draw if charging
 * while in the Audio Accessory State.
 */
#define TYPE_C_AUDIO_ACC_CURRENT 500 /* mA */

/**
 * Returns true if TypeC State machine is in attached source state.
 *
 * @param port USB-C port number
 * @return 1 if in attached source state, else 0
 */
int tc_is_attached_src(int port);

/**
 * Returns true if TypeC State machine is in attached sink state.
 *
 * @param port USB-C port number
 * @return 1 if in attached source state, else 0
 */
int tc_is_attached_snk(int port);

/**
 * Get cable plug setting. This should be constant per build. This replaces
 * the power role bit in PD header for SOP' and SOP" packets.
 *
 * @param port USB-C port number
 * @return PD cable plug setting
 */
enum pd_cable_plug tc_get_cable_plug(int port);

/**
 * Get current polarity
 *
 * @param port USB-C port number
 * @return 0 for CC1 as primary, 1 for CC2 as primary
 */
uint8_t tc_get_polarity(int port);

/**
 * Get Power Deliever communication state. If disabled, both protocol and policy
 * engine are disabled and should not run.
 *
 * @param port USB-C port number
 * @return 0 if pd is disabled, 1 is pd is enabled
 */
uint8_t tc_get_pd_enabled(int port);

/**
 * Set the power role
 *
 * @param port USB-C port number
 * @param role power role
 */
void tc_set_power_role(int port, enum pd_power_role role);

/**
 * Set the data role
 *
 * @param port USB-C port number
 * @param role data role
 */
void tc_set_data_role(int port, enum pd_data_role role);

/**
 * Sets the USB Mux depending on current data role
 *   Mux is connected except when:
 *     1) PD is disconnected
 *     2) Current data role is UFP and we only support DFP
 *
 *  @param port USB-C port number
 */
void set_usb_mux_with_current_data_role(int port);

/**
 * Policy Engine informs the Type-C state machine if the port partner
 * is dualrole power.
 *
 * @param port USB_C port number
 * @param en   1 if port partner is dualrole power, else 0
 */
void tc_partner_dr_power(int port, int en);

/**
 * Policy Engine informs the Type-C state machine if the port partner
 * has unconstrained power
 *
 * @param port USB_C port number
 * @param en   1 if port partner has unconstrained power, else 0
 */
void tc_partner_unconstrainedpower(int port, int en);

/**
 * Policy Engine informs the Type-C state machine if the port partner
 * is USB comms.
 *
 * @param port USB_C port number
 * @param en   1 if port partner is USB comms, else 0
 */
void tc_partner_usb_comm(int port, int en);

/**
 * Policy Engine informs the Type-C state machine if the port partner
 * is dualrole data.
 *
 * @param port USB_C port number
 * @param en   1 if port partner is dualrole data, else 0
 */
void tc_partner_dr_data(int port, int en);

/**
 * Policy Engine informs the Type-C state machine if the port partner
 * had a previous pd connection
 *
 * @param port USB_C port number
 * @param en   1 if port partner had a previous pd connection, else 0
 */
void tc_pd_connection(int port, int en);

/**
 * Initiates a Power Role Swap from Attached.SRC to Attached.SNK. This function
 * has no effect if the current Type-C state is not Attached.SRC.
 *
 * @param port USB_C port number
 */
void tc_prs_src_snk_assert_rd(int port);

/**
 * Initiates a Power Role Swap from Attached.SNK to Attached.SRC. This function
 * has no effect if the current Type-C state is not Attached.SNK.
 *
 * @param port USB_C port number
 */
void tc_prs_snk_src_assert_rp(int port);

/**
 * Informs the Type-C State Machine that a Power Role Swap is starting.
 * This function is called from the Policy Engine.
 *
 * @parm port USB_C port number
 */
void tc_request_power_swap(int port);

/**
 * Informs the Type-C State Machine that a Power Role Swap is complete.
 * This function is called from the Policy Engine.
 *
 * @param port USB_C port number
 * @param success swap completed normally
 */
void tc_pr_swap_complete(int port, bool success);

/**
 * The Type-C state machine updates the SLEEP_MASK_USB_PD mask for the
 * case that TCPC wants to set/clear SLEEP_MASK_USB_PD mask only by
 * itself, ex. TCPC embedded in EC.
 *
 * @param port USB_C port number
 */
__override_proto void tc_update_pd_sleep_mask(int port);

/**
 * Instructs the Attached.SNK to stop drawing power. This function is called
 * from the Policy Engine and only has effect if the current Type-C state
 * Attached.SNK.
 *
 * @param port USB_C port number
 */
void tc_snk_power_off(int port);

/**
 * Instructs the Attached.SRC to stop supplying power. The function has
 * no effect if the current Type-C state is not Attached.SRC.
 *
 * @param port USB_C port number
 */
void tc_src_power_off(int port);

/**
 * Instructs the Attached.SRC to start supplying power. The function has
 * no effect if the current Type-C state is not Attached.SRC.
 *
 * @param port USB_C port number
 */
int tc_src_power_on(int port);

/**
 * Tests if a VCONN Swap is possible.
 *
 * @param port USB_C port number
 * @return 1 if vconn swap is possible, else 0
 */
int tc_check_vconn_swap(int port);

/**
 * Checks if VCONN is being sourced.
 *
 * @param port USB_C port number
 * @return 1 if vconn is being sourced, 0 if it's not.
 */
int tc_is_vconn_src(int port);

/**
 * Instructs the Attached.SRC or Attached.SNK to start sourcing VCONN.
 * This function is called from the Policy Engine and only has effect
 * if the current Type-C state Attached.SRC or Attached.SNK.
 *
 * @param port USB_C port number
 */
void pd_request_vconn_swap_on(int port);

/**
 * Instructs the Attached.SRC or Attached.SNK to stop sourcing VCONN.
 * This function is called from the Policy Engine and only has effect
 * if the current Type-C state Attached.SRC or Attached.SNK.
 *
 * @param port USB_C port number
 */
void pd_request_vconn_swap_off(int port);

/**
 * Returns the polarity of a Sink.
 *
 * @param cc1 value of CC1 set by tcpm_get_cc
 * @param cc2 value of CC2 set by tcpm_get_cc
 * @return 0 if cc1 is connected, else 1 for cc2
 */
enum tcpc_cc_polarity get_snk_polarity(enum tcpc_cc_voltage_status cc1,
				       enum tcpc_cc_voltage_status cc2);

/**
 * Called by the state machine framework to initialize the
 * TypeC state machine
 *
 * @param port USB-C port number
 */
void tc_state_init(int port);

/**
 * Called by the state machine framework to handle events
 * that affect the state machine as a whole
 *
 * @param port USB-C port number
 * @param evt event
 */
void tc_event_check(int port, int evt);

/**
 * Runs the TypeC layer statemachine
 *
 * @param port USB-C port number
 */
void tc_run(const int port);

/**
 * Sets the debug level for the TC layer
 *
 * @param level debug level
 */
void tc_set_debug_level(enum debug_level level);

/**
 * Start error recovery
 *
 * @param port USB-C port number
 */
void tc_start_error_recovery(int port);

/**
 * Hard Reset the TypeC port
 *
 * @param port USB-C port number
 */
void tc_hard_reset_request(int port);

/**
 * Hard Reset is complete for the TypeC port
 *
 * @param port USB-C port number
 */
void tc_hard_reset_complete(int port);

/**
 * Start the state machine event loop
 *
 * @param port USB-C port number
 */
void tc_start_event_loop(int port);

/**
 * Pauses the state machine event loop
 *
 * @param port USB-C port number
 */
void tc_pause_event_loop(int port);

/**
 * Determine if the state machine event loop is paused
 *
 * @param port USB-C port number
 * @return true if paused, else false
 */
bool tc_event_loop_is_paused(int port);

/**
 * Allow system to override the control of TrySrc
 *
 * @param en	TRY_SRC_OVERRIDE_OFF - Force TrySrc OFF
 *		TRY_SRC_OVERRIDE_ON - Force TrySrc ON
 *		TRY_SRC_NO_OVERRIDE - Allow state machine to control TrySrc
 */
void tc_try_src_override(enum try_src_override_t ov);

/**
 * Get state of try_src_override
 *
 * @return	TRY_SRC_OVERRIDE_OFF - TrySrc is forced OFF
 *		TRY_SRC_OVERRIDE_ON - TrySrc is forced ON
 *		TRY_SRC_NO_OVERRIDE - TypeC state machine controls TrySrc
 */
enum try_src_override_t tc_get_try_src_override(void);

/**
 * Returns the name of the current typeC state
 *
 * @param port USB-C port number
 * @return name of current typeC state
 */
const char *tc_get_current_state(int port);

/**
 * Returns the flag mask of the typeC state machine
 *
 * @param port USB-C port number
 * @return flag mask of the typeC state machine
 */
uint32_t tc_get_flags(int port);

/**
 * USB retimer firmware update set run flag
 * Setting this flag indicates firmware update operations can be
 * processed unconditionally.
 *
 * @param port USB-C port number
 */
void tc_usb_firmware_fw_update_run(int port);

/**
 * USB retimer firmware update set limited run flag
 * Setting this flag indicates firmware update operations can be
 * processed under limitation: PD task has to be suspended.
 *
 * @param port USB-C port number
 */
void tc_usb_firmware_fw_update_limited_run(int port);

#ifdef CONFIG_USB_CTVPD

/**
 * Resets the charge-through support timer. This can be
 * called many times but the support timer will only
 * reset once, while in the Attached.SNK state.
 *
 * @param port USB-C port number
 */
void tc_reset_support_timer(int port);

#else

/**
 *
 */
void tc_ctvpd_detected(int port);
#endif /* CONFIG_USB_CTVPD */
#endif /* __CROS_EC_USB_TC_H */