USB TCPM(Type-C Port Manager)的主要作用是管理 USB Type-C 端口的连接和电源传输协议(USB Power Delivery, PD),确保设备正确识别、协商和切换数据传输和电源供应的角色。TCPM 在 USB Type-C 连接中起到关键管理作用,主要职责包括:
-
管理 USB Type-C 插拔检测:检测设备的插入和拔出事件,识别连接器的方向(翻转)和线缆类型,并做出相应的配置调整。
-
协商电力传输:与 USB PD 控制器(如 TCPC)配合,通过 PD 协议与对端设备协商电力供应,确定电压和电流等级,并动态调整电力配置以匹配设备的需求。
-
角色管理:决定设备在 USB 连接中的角色(主机或设备、供电方或受电方),并根据连接情况和协议协商结果,动态切换这些角色。
-
配合 PD 控制器(TCPC):TCPM 通常与 TCPC(Type-C Port Controller)协同工作。TCPC 是具体实现 PD 协议硬件通信的控制器,而 TCPM 作为软件部分,负责控制 TCPC 和处理高层的协议逻辑。
-
安全和错误处理:监控连接状态,处理异常情况(如过流、短路、异常拔插),确保连接的安全性和稳定性。
通过 TCPM 的管理,可以实现 USB Type-C 设备之间的智能互操作,确保数据传输和电力传输的安全、灵活和高效。
tcpm的工作细节
在我们平时工作中adb使用typec口,来进行调试是常有的事情,那么在typec插上的一瞬间是就是通过tcpm来建立最初的联系,然后通知到dwc3和gadget来进行交互的,后面的工作就完全交给dwc3那边进行处理。
可以从/sys/kernel/debug/usb/tcpm-xxx下,抓取一份tcpm的log,这里抓取了一份RK平台,从开机到adb数据线设备插拔的过程log,来分析整个数据流程:
1. 一些重要参数的初始化值port->port_type = TYPEC_PORT_DRPport->prefer_role = TYPEC_SINKport->data_role = TYPEC_DEVICE;port->pwr_role = TYPEC_SINK;port->try_role = TYPEC_SINK;port->state = SNK_UNATTACHEDport->vbus_never_low = falseport->pd_capable = falsetcpm_rp_cc(port) = TYPEC_CC_RP_3_02. 一些重要参数的变动port->vbus_present = false -> _tcpm_pd_vbus_on() -> tureport->vbus_vsafe0v = true -> _tcpm_pd_vbus_on() -> false3. 状态机转变state的整个调用流程tcpm_set_state -> kthread_queue_work(port->wq, &port->state_machine) --> tcpm_state_machine_work -> run_state_machine4. 抓取到的整个数据流程包含tcpm初始化,插入/拔出adb数据线rk3568_s:/sys/kernel/debug/usb/tcpm-0-004e # cat log// 开机时启动后,初始阶段的log[ 3.169614] init extcon finished[ 3.173249] Setting usb_comm capable false[ 3.174716] Setting voltage/current limit 0 mV 0 mA[ 3.174728] polarity 0[ 3.176800] Requesting mux state 0, usb-role 0, orientation 0[ 3.178567] state change INVALID_STATE -> SNK_UNATTACHED [rev1 NONE_AMS][ 3.179503] CC1: 0 -> 0, CC2: 0 -> 0 [state SNK_UNATTACHED, polarity 0, disconnected][ 3.179510] state change SNK_UNATTACHED -> PORT_RESET [rev1 NONE_AMS][ 3.179520] 0-004e: registered[ 3.179530] Setting usb_comm capable false[ 3.181007] Setting voltage/current limit 0 mV 0 mA[ 3.181023] polarity 0[ 3.183097] Requesting mux state 0, usb-role 0, orientation 0[ 3.184351] cc:=0[ 3.185144] pending state change PORT_RESET -> PORT_RESET_WAIT_OFF @ 100 ms [rev1 NONE_AMS][ 3.185154] state change PORT_RESET -> PORT_RESET_WAIT_OFF [delayed 100 ms][ 3.185159] state change PORT_RESET_WAIT_OFF -> SNK_UNATTACHED [rev1 NONE_AMS][ 3.185163] Start toggling[ 3.185831] state change SNK_UNATTACHED -> TOGGLING [rev1 NONE_AMS]//插入typec后,tcpm的log,其中"======>" 这种箭头的打印是我本人加的[ 53.680304][ T152] =================> tcpci_irq -> status[0x02][ 53.681360] VBUS on[ 53.705497][ T152] =================> tcpci_irq -> status[0x01][ 53.706613] CC1: 0 -> 3, CC2: 0 -> 0 [state TOGGLING, polarity 0, connected][ 53.706627] state change TOGGLING -> SNK_ATTACH_WAIT [rev1 NONE_AMS][ 53.706644] pending state change SNK_ATTACH_WAIT -> SNK_DEBOUNCED @ 200 ms [rev1 NONE_AMS][ 53.906677] state change SNK_ATTACH_WAIT -> SNK_DEBOUNCED [delayed 200 ms][ 53.906691] state change SNK_DEBOUNCED -> SNK_ATTACHED [rev1 NONE_AMS][ 53.906695] polarity 0[ 53.908791][ T151] emdoor: tcpm_set_polarity typec positive[ 53.908862] Requesting mux state 1, usb-role 2, orientation 1[ 53.909477][ T151] husb311 0-004e: CC connected in CC1 as UFP[ 53.909744] state change SNK_ATTACHED -> SNK_STARTUP [rev1 NONE_AMS][ 53.909755] state change SNK_STARTUP -> SNK_DISCOVERY [rev3 NONE_AMS][ 53.909759] Setting voltage/current limit 5000 mV 0 mA[ 53.909775] vbus=0 charge:=1[ 53.910560] state change SNK_DISCOVERY -> SNK_WAIT_CAPABILITIES [rev3 NONE_AMS][ 53.910951] pending state change SNK_WAIT_CAPABILITIES -> HARD_RESET_SEND @ 310 ms [rev3 NONE_AMS][ 54.221059] state change SNK_WAIT_CAPABILITIES -> HARD_RESET_SEND [delayed 310 ms][ 54.221080] AMS HARD_RESET start[ 54.221083] PD TX, type: 0x5[ 54.223648][ T152] =================> tcpci_irq -> status[0x50][ 54.223737] PD TX complete, status: 0[ 54.223819] state change HARD_RESET_SEND -> HARD_RESET_START [rev3 HARD_RESET][ 54.225211] state change HARD_RESET_START -> SNK_HARD_RESET_SINK_OFF [rev3 HARD_RESET][ 54.225235] vconn:=0[ 54.225747] Requesting mux state 1, usb-role 2, orientation 1[ 54.226346] pending state change SNK_HARD_RESET_SINK_OFF -> SNK_HARD_RESET_SINK_ON @ 650 ms [rev3 HARD_RESET][ 54.876427] state change SNK_HARD_RESET_SINK_OFF -> SNK_HARD_RESET_SINK_ON [delayed 650 ms][ 54.876447] AMS HARD_RESET finished[ 54.876850] state change SNK_HARD_RESET_SINK_ON -> SNK_STARTUP [rev3 NONE_AMS][ 54.876871] state change SNK_STARTUP -> SNK_DISCOVERY [rev3 NONE_AMS][ 54.876875] Setting voltage/current limit 5000 mV 0 mA[ 54.876896] state change SNK_DISCOVERY -> SNK_WAIT_CAPABILITIES [rev3 NONE_AMS][ 54.877264] pending state change SNK_WAIT_CAPABILITIES -> HARD_RESET_SEND @ 310 ms [rev3 NONE_AMS][ 55.187319] state change SNK_WAIT_CAPABILITIES -> HARD_RESET_SEND [delayed 310 ms][ 55.187339] AMS HARD_RESET start[ 55.187343] PD TX, type: 0x5[ 55.189910][ T152] =================> tcpci_irq -> status[0x50][ 55.189982] PD TX complete, status: 0[ 55.190020] state change HARD_RESET_SEND -> HARD_RESET_START [rev3 HARD_RESET][ 55.191474] state change HARD_RESET_START -> SNK_HARD_RESET_SINK_OFF [rev3 HARD_RESET][ 55.191494] vconn:=0[ 55.192023] Requesting mux state 1, usb-role 2, orientation 1[ 55.192727] pending state change SNK_HARD_RESET_SINK_OFF -> SNK_HARD_RESET_SINK_ON @ 650 ms [rev3 HARD_RESET][ 55.842808] state change SNK_HARD_RESET_SINK_OFF -> SNK_HARD_RESET_SINK_ON [delayed 650 ms][ 55.842828] AMS HARD_RESET finished[ 55.843201] state change SNK_HARD_RESET_SINK_ON -> SNK_STARTUP [rev3 NONE_AMS][ 55.843214] state change SNK_STARTUP -> SNK_DISCOVERY [rev3 NONE_AMS][ 55.843217] Setting voltage/current limit 5000 mV 0 mA[ 55.843238] state change SNK_DISCOVERY -> SNK_WAIT_CAPABILITIES [rev3 NONE_AMS][ 55.843604] pending state change SNK_WAIT_CAPABILITIES -> SNK_READY @ 310 ms [rev3 NONE_AMS][ 56.153719] state change SNK_WAIT_CAPABILITIES -> SNK_READY [delayed 310 ms]// 拔出typec时,tcpm的打印[ 2087.027300][ T152] =================> tcpci_irq -> status[0x01][ 2087.028556] CC1: 3 -> 0, CC2: 0 -> 0 [state SNK_READY, polarity 0, disconnected][ 2087.028574] state change SNK_READY -> SNK_UNATTACHED [rev3 NONE_AMS][ 2087.028585] disable BIST MODE TESTDATA[ 2087.029540] Setting usb_comm capable false[ 2087.031079] Setting voltage/current limit 0 mV 0 mA[ 2087.031116] polarity 0[ 2087.033294][ T151] emdoor: tcpm_set_polarity typec positive[ 2087.033323] Requesting mux state 0, usb-role 0, orientation 0[ 2087.034933] Start toggling[ 2087.035701] state change SNK_UNATTACHED -> TOGGLING [rev3 NONE_AMS][ 2087.046906][ T152] =================> tcpci_irq -> status[0x02][ 2087.048083] VBUS off[ 2087.048095] VBUS VSAFE0V
从log内,进一步分析整个tcpm的运行流程,总结了出如下流程图:
同样关于插入的过程也总结了如下流程图: