Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
usb_pd_tcpm.h
1/* Copyright 2015 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6/* USB Power delivery port management */
7
8#ifndef __CROS_EC_USB_PD_TCPM_H
9#define __CROS_EC_USB_PD_TCPM_H
10
11#include "../config.h"
12#include <assert.h>
13#include <stdint.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19 /* List of common error codes that can be returned */
20 enum ec_error_list
21 {
22 /* Success - no error */
23 EC_SUCCESS = 0,
24 /* Unknown error */
25 EC_ERROR_UNKNOWN = 1,
26 /* Function not implemented yet */
27 EC_ERROR_UNIMPLEMENTED = 2,
28 /* Overflow error; too much input provided. */
29 EC_ERROR_OVERFLOW = 3,
30 /* Timeout */
31 EC_ERROR_TIMEOUT = 4,
32 /* Invalid argument */
33 EC_ERROR_INVAL = 5,
34 /* Already in use, or not ready yet */
35 EC_ERROR_BUSY = 6,
36 /* Access denied */
37 EC_ERROR_ACCESS_DENIED = 7,
38 /* Failed because component does not have power */
39 EC_ERROR_NOT_POWERED = 8,
40 /* Failed because component is not calibrated */
41 EC_ERROR_NOT_CALIBRATED = 9,
42 /* Failed because CRC error */
43 EC_ERROR_CRC = 10,
44 /* Invalid console command param (PARAMn means parameter n is bad) */
45 EC_ERROR_PARAM1 = 11,
46 EC_ERROR_PARAM2 = 12,
47 EC_ERROR_PARAM3 = 13,
48 EC_ERROR_PARAM4 = 14,
49 EC_ERROR_PARAM5 = 15,
50 EC_ERROR_PARAM6 = 16,
51 EC_ERROR_PARAM7 = 17,
52 EC_ERROR_PARAM8 = 18,
53 EC_ERROR_PARAM9 = 19,
54 /* Wrong number of params */
55 EC_ERROR_PARAM_COUNT = 20,
56 /* Interrupt event not handled */
57 EC_ERROR_NOT_HANDLED = 21,
58 /* Data has not changed */
59 EC_ERROR_UNCHANGED = 22,
60 /* Memory allocation */
61 EC_ERROR_MEMORY_ALLOCATION = 23,
62
63 /* Verified boot errors */
64 EC_ERROR_VBOOT_SIGNATURE = 0x1000, /* 4096 */
65 EC_ERROR_VBOOT_SIG_MAGIC = 0x1001,
66 EC_ERROR_VBOOT_SIG_SIZE = 0x1002,
67 EC_ERROR_VBOOT_SIG_ALGORITHM = 0x1003,
68 EC_ERROR_VBOOT_HASH_ALGORITHM = 0x1004,
69 EC_ERROR_VBOOT_SIG_OFFSET = 0x1005,
70 EC_ERROR_VBOOT_DATA_SIZE = 0x1006,
71
72 /* Verified boot key errors */
73 EC_ERROR_VBOOT_KEY = 0x1100,
74 EC_ERROR_VBOOT_KEY_MAGIC = 0x1101,
75 EC_ERROR_VBOOT_KEY_SIZE = 0x1102,
76
77 /* Verified boot data errors */
78 EC_ERROR_VBOOT_DATA = 0x1200,
79 EC_ERROR_VBOOT_DATA_VERIFY = 0x1201,
80
81 /* Module-internal error codes may use this range. */
82 EC_ERROR_INTERNAL_FIRST = 0x10000,
83 EC_ERROR_INTERNAL_LAST = 0x1FFFF
84 };
85
87 {
88 uint16_t vendor_id;
89 uint16_t product_id;
90 uint16_t device_id;
91 union
92 {
93 uint8_t fw_version_string[8];
94 uint64_t fw_version_number;
95 } __packed;
96 };
97
98/* Default retry count for transmitting */
99#define PD_RETRY_COUNT 3
100
101/* Time to wait for TCPC to complete transmit */
102#define PD_T_TCPC_TX_TIMEOUT (100 * MSEC_US)
103
104 enum usbpd_cc_pin
105 {
106 USBPD_CC_PIN_1,
107 USBPD_CC_PIN_2,
108 };
109
110 /* Detected resistor values of port partner */
111 enum tcpc_cc_voltage_status
112 {
113 TYPEC_CC_VOLT_OPEN = 0,
114 TYPEC_CC_VOLT_RA = 1, /* Port partner is applying Ra */
115 TYPEC_CC_VOLT_RD = 2, /* Port partner is applying Rd */
116 TYPEC_CC_VOLT_RP_DEF = 5, /* Port partner is applying Rp (0.5A) */
117 TYPEC_CC_VOLT_RP_1_5 = 6, /* Port partner is applying Rp (1.5A) */
118 TYPEC_CC_VOLT_RP_3_0 = 7, /* Port partner is applying Rp (3.0A) */
119 };
120
121 /* Resistor types we apply on our side of the CC lines */
122 enum tcpc_cc_pull
123 {
124 TYPEC_CC_RA = 0,
125 TYPEC_CC_RP = 1,
126 TYPEC_CC_RD = 2,
127 TYPEC_CC_OPEN = 3,
128 TYPEC_CC_RA_RD = 4, /* Powered cable with Sink */
129 };
130
131 /* Pull-up values we apply as a SRC to advertise different current limits */
132 enum tcpc_rp_value
133 {
134 TYPEC_RP_USB = 0,
135 TYPEC_RP_1A5 = 1,
136 TYPEC_RP_3A0 = 2,
137 TYPEC_RP_RESERVED = 3,
138 };
139
140 /* Possible port partner connections based on CC line states */
141 enum pd_cc_states
142 {
143 PD_CC_NONE = 0, /* No port partner attached */
144
145 /* From DFP perspective */
146 PD_CC_UFP_NONE = 1, /* No UFP accessory connected */
147 PD_CC_UFP_AUDIO_ACC = 2, /* UFP Audio accessory connected */
148 PD_CC_UFP_DEBUG_ACC = 3, /* UFP Debug accessory connected */
149 PD_CC_UFP_ATTACHED = 4, /* Plain UFP attached */
150
151 /* From UFP perspective */
152 PD_CC_DFP_ATTACHED = 5, /* Plain DFP attached */
153 PD_CC_DFP_DEBUG_ACC = 6, /* DFP debug accessory connected */
154 };
155
156 /* DRP (dual-role-power) setting */
157 enum tcpc_drp
158 {
159 TYPEC_NO_DRP = 0,
160 TYPEC_DRP = 1,
161 };
162
163 /*
164 * Power role.
165 *
166 * Note this is also used for PD header creation, and values align to those in
167 * the Power Delivery Specification Revision 3.0 (See
168 * 6.2.1.1.4 Port Power Role).
169 */
170 enum pd_power_role
171 {
172 PD_ROLE_SINK = 0,
173 PD_ROLE_SOURCE = 1
174 };
175
176 /*
177 * Data role.
178 *
179 * Note this is also used for PD header creation, and the first two values
180 * align to those in the Power Delivery Specification Revision 3.0 (See
181 * 6.2.1.1.6 Port Data Role).
182 */
183 enum pd_data_role
184 {
185 PD_ROLE_UFP = 0,
186 PD_ROLE_DFP = 1,
187 PD_ROLE_DISCONNECTED = 2,
188 };
189
190 enum pd_vconn_role
191 {
192 PD_ROLE_VCONN_OFF = 0,
193 PD_ROLE_VCONN_SRC = 1,
194 };
195
196 /*
197 * Note: BIT(0) may be used to determine whether the polarity is CC1 or CC2,
198 * regardless of whether a debug accessory is connected.
199 */
200 enum tcpc_cc_polarity
201 {
202 /*
203 * _CCx: is used to indicate the polarity while not connected to
204 * a Debug Accessory. Only one CC line will assert a resistor and
205 * the other will be open.
206 */
207 POLARITY_CC1 = 0,
208 POLARITY_CC2 = 1,
209
210 /*
211 * _CCx_DTS is used to indicate the polarity while connected to a
212 * SRC Debug Accessory. Assert resistors on both lines.
213 */
214 POLARITY_CC1_DTS = 2,
215 POLARITY_CC2_DTS = 3,
216
217 /*
218 * The current TCPC code relies on these specific POLARITY values.
219 * Adding in a check to verify if the list grows for any reason
220 * that this will give a hint that other places need to be
221 * adjusted.
222 */
223 POLARITY_COUNT
224 };
225
229 static inline enum tcpc_cc_polarity polarity_rm_dts(enum tcpc_cc_polarity polarity)
230 {
231 static_assert(POLARITY_COUNT == 4);
232 return (enum tcpc_cc_polarity)(polarity & (1 << 0));
233 }
234
235 enum tcpm_transmit_type
236 {
237 TCPC_TX_SOP = 0,
238 TCPC_TX_SOP_PRIME = 1,
239 TCPC_TX_SOP_PRIME_PRIME = 2,
240 TCPC_TX_SOP_DEBUG_PRIME = 3,
241 TCPC_TX_SOP_DEBUG_PRIME_PRIME = 4,
242 TCPC_TX_HARD_RESET = 5,
243 TCPC_TX_CABLE_RESET = 6,
244 TCPC_TX_BIST_MODE_2 = 7
245 };
246
247 enum tcpc_transmit_complete
248 {
249 TCPC_TX_UNSET = -1,
250 TCPC_TX_COMPLETE_SUCCESS = 0,
251 TCPC_TX_COMPLETE_DISCARDED = 1,
252 TCPC_TX_COMPLETE_FAILED = 2,
253 };
254
255 /*
256 * USB-C PD Vbus levels
257 *
258 * Return true on Vbus check if Vbus is...
259 */
260 enum vbus_level
261 {
262 VBUS_SAFE0V, /* less than vSafe0V max */
263 VBUS_PRESENT, /* at least vSafe5V min */
264 VBUS_REMOVED, /* less than vSinkDisconnect max */
265 };
266
267 struct tcpm_drv
268 {
276 int (*init)();
277
284 int (*release)();
285
294 int (*get_cc)(enum tcpc_cc_voltage_status* cc1, enum tcpc_cc_voltage_status* cc2);
295
303 int (*get_vbus_level)(enum vbus_level level);
304
313 int (*get_vbus_voltage)(int* vbus);
314
322 int (*select_rp_value)(int rp);
323
331 int (*set_cc)(int pull);
332
340 int (*set_polarity)(enum tcpc_cc_polarity polarity);
341
342#ifdef CONFIG_USB_PD_DECODE_SOP
353 int (*sop_prime_enable)(int enable);
354#endif
355
363 int (*set_vconn)(int enable);
364
373 int (*set_msg_header)(int power_role, int data_role);
374
380 int (*set_rx_enable)(int enable);
381
391 int (*get_message)(uint32_t* payload, uint32_t* head);
392
403 int (*transmit)(enum tcpm_transmit_type type, uint16_t header, const uint32_t* data);
404
409 void (*tcpc_alert)();
410
416 void (*tcpc_discharge_vbus)(int enable);
417
424
430 int (*debug_accessory)(int enable);
431
437 int (*debug_detach)();
438
439#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
446 int (*drp_toggle)();
447#endif
448
457 int (*get_chip_info)(int live, struct ec_response_pd_chip_info** info);
458
459#ifdef CONFIG_USBC_PPC
468 int (*get_snk_ctrl)(int* sinking);
469
477 int (*set_snk_ctrl)(int enable);
478
487 int (*get_src_ctrl)(int* sourcing);
488
496 int (*set_src_ctrl)(int enable);
497#endif
498
499#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
510#endif
511
512#ifdef CONFIG_USB_PD_FRS_TCPC
520 int (*set_frs_enable)(int enable);
521#endif
522
531 int (*handle_fault)(int fault);
532
533#ifdef CONFIG_CMD_TCPC_DUMP
537 void (*dump_registers)();
538#endif /* defined(CONFIG_CMD_TCPC_DUMP) */
539 };
540
541 enum tcpc_alert_polarity
542 {
543 TCPC_ALERT_ACTIVE_LOW,
544 TCPC_ALERT_ACTIVE_HIGH,
545 };
546
548 {
549 int i2c_host_port;
550 int i2c_slave_addr;
551 const struct tcpm_drv* drv;
552 enum tcpc_alert_polarity pol;
553 };
554
558 static inline int cc_is_rp(enum tcpc_cc_voltage_status cc)
559 {
560 return (cc == TYPEC_CC_VOLT_RP_DEF) || (cc == TYPEC_CC_VOLT_RP_1_5) || (cc == TYPEC_CC_VOLT_RP_3_0);
561 }
562
566 static inline int cc_is_open(enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2)
567 {
568 return cc1 == TYPEC_CC_VOLT_OPEN && cc2 == TYPEC_CC_VOLT_OPEN;
569 }
570
574 static inline int cc_is_snk_dbg_acc(enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2)
575 {
576 return cc1 == TYPEC_CC_VOLT_RD && cc2 == TYPEC_CC_VOLT_RD;
577 }
578
579 static inline uint8_t board_get_src_dts_polarity()
580 {
581 /*
582 * If the port in SRC DTS, the polarity is determined by the board,
583 * i.e. what Rp impedance the CC lines are pulled. If this function
584 * is not overridden, assume CC1 is primary.
585 */
586 return 0;
587 }
588
592 static inline int cc_is_src_dbg_acc(enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2)
593 {
594 return cc_is_rp(cc1) && cc_is_rp(cc2);
595 }
596
600 static inline int cc_is_audio_acc(enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2)
601 {
602 return cc1 == TYPEC_CC_VOLT_RA && cc2 == TYPEC_CC_VOLT_RA;
603 }
604
608 static inline int cc_is_at_least_one_rd(enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2)
609 {
610 return cc1 == TYPEC_CC_VOLT_RD || cc2 == TYPEC_CC_VOLT_RD;
611 }
612
616 static inline int cc_is_only_one_rd(enum tcpc_cc_voltage_status cc1, enum tcpc_cc_voltage_status cc2)
617 {
618 return cc_is_at_least_one_rd(cc1, cc2) && cc1 != cc2;
619 }
620
627 uint16_t tcpc_get_alert_status(void);
628
635 void board_set_tcpc_power_mode(int mode) __attribute__((weak));
636
642 void tcpc_init();
643
649 void tcpc_alert_clear();
650
658 int tcpc_run(int evt);
659
667 int board_tcpc_post_init() __attribute__((weak));
668
669#ifdef __cplusplus
670}
671#endif
672
673#endif /* __CROS_EC_USB_PD_TCPM_H */
Define assertions helpers.
Definition: usb_pd_tcpm.h:87
Definition: usb_pd_tcpm.h:548
Definition: usb_pd_tcpm.h:268
int(* select_rp_value)(int rp)
Definition: usb_pd_tcpm.h:322
int(* get_chip_info)(int live, struct ec_response_pd_chip_info **info)
Definition: usb_pd_tcpm.h:457
int(* set_vconn)(int enable)
Definition: usb_pd_tcpm.h:363
int(* set_rx_enable)(int enable)
Definition: usb_pd_tcpm.h:380
int(* get_vbus_level)(enum vbus_level level)
Definition: usb_pd_tcpm.h:303
int(* get_snk_ctrl)(int *sinking)
Definition: usb_pd_tcpm.h:468
int(* set_snk_ctrl)(int enable)
Definition: usb_pd_tcpm.h:477
void(* dump_registers)()
Definition: usb_pd_tcpm.h:537
int(* set_msg_header)(int power_role, int data_role)
Definition: usb_pd_tcpm.h:373
int(* init)()
Definition: usb_pd_tcpm.h:276
int(* debug_accessory)(int enable)
Definition: usb_pd_tcpm.h:430
int(* transmit)(enum tcpm_transmit_type type, uint16_t header, const uint32_t *data)
Definition: usb_pd_tcpm.h:403
int(* set_src_ctrl)(int enable)
Definition: usb_pd_tcpm.h:496
void(* tcpc_enable_auto_discharge_disconnect)(int enable)
Definition: usb_pd_tcpm.h:423
void(* tcpc_alert)()
Definition: usb_pd_tcpm.h:409
int(* sop_prime_enable)(int enable)
Definition: usb_pd_tcpm.h:353
void(* tcpc_discharge_vbus)(int enable)
Definition: usb_pd_tcpm.h:416
int(* get_cc)(enum tcpc_cc_voltage_status *cc1, enum tcpc_cc_voltage_status *cc2)
Definition: usb_pd_tcpm.h:294
int(* enter_low_power_mode)()
Definition: usb_pd_tcpm.h:509
int(* get_vbus_voltage)(int *vbus)
Definition: usb_pd_tcpm.h:313
int(* release)()
Definition: usb_pd_tcpm.h:284
int(* get_message)(uint32_t *payload, uint32_t *head)
Definition: usb_pd_tcpm.h:391
int(* set_cc)(int pull)
Definition: usb_pd_tcpm.h:331
int(* get_src_ctrl)(int *sourcing)
Definition: usb_pd_tcpm.h:487
int(* set_polarity)(enum tcpc_cc_polarity polarity)
Definition: usb_pd_tcpm.h:340
int(* debug_detach)()
Definition: usb_pd_tcpm.h:437
int(* drp_toggle)()
Definition: usb_pd_tcpm.h:446
int(* handle_fault)(int fault)
Definition: usb_pd_tcpm.h:531
int(* set_frs_enable)(int enable)
Definition: usb_pd_tcpm.h:520