Skip to content

Commit da87d45

Browse files
Andrei Kuchynskigregkh
authored andcommitted
usb: typec: ucsi: Add Thunderbolt alternate mode support
Introduce support for Thunderbolt (TBT) alternate mode to the UCSI driver. This allows the driver to manage the entry and exit of TBT altmode by providing the necessary typec_altmode_ops. ucsi_altmode_update_active() is invoked when the Connector Partner Changed bit is set in the GET_CONNECTOR_STATUS data. This ensures that the alternate mode's active state is synchronized with the current mode the connector is operating in. Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org> Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Link: https://patch.msgid.link/20260206115754.828954-1-akuchynski@chromium.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b3f9d6e commit da87d45

4 files changed

Lines changed: 249 additions & 5 deletions

File tree

drivers/usb/typec/ucsi/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ ifneq ($(CONFIG_TYPEC_DP_ALTMODE),)
1717
typec_ucsi-y += displayport.o
1818
endif
1919

20+
ifneq ($(CONFIG_TYPEC_TBT_ALTMODE),)
21+
typec_ucsi-y += thunderbolt.o
22+
endif
23+
2024
obj-$(CONFIG_UCSI_ACPI) += ucsi_acpi.o
2125
obj-$(CONFIG_UCSI_CCG) += ucsi_ccg.o
2226
obj-$(CONFIG_UCSI_STM32G0) += ucsi_stm32g0.o
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* UCSI Thunderbolt Alternate Mode Support
4+
*
5+
* Copyright 2026 Google LLC
6+
*/
7+
8+
#include <linux/usb/typec_tbt.h>
9+
#include <linux/usb/pd_vdo.h>
10+
#include <linux/err.h>
11+
#include <linux/dev_printk.h>
12+
#include <linux/device/devres.h>
13+
#include <linux/gfp_types.h>
14+
#include <linux/types.h>
15+
#include <linux/usb/typec_altmode.h>
16+
#include <linux/workqueue.h>
17+
18+
#include "ucsi.h"
19+
20+
/**
21+
* struct ucsi_tbt - Thunderbolt Alternate Mode private data structure
22+
* @con: Pointer to UCSI connector structure
23+
* @alt: Pointer to typec altmode structure
24+
* @work: Work structure
25+
* @cam: An offset into the list of alternate modes supported by the PPM
26+
* @header: VDO header
27+
*/
28+
struct ucsi_tbt {
29+
struct ucsi_connector *con;
30+
struct typec_altmode *alt;
31+
struct work_struct work;
32+
int cam;
33+
u32 header;
34+
};
35+
36+
static void ucsi_thunderbolt_work(struct work_struct *work)
37+
{
38+
struct ucsi_tbt *tbt = container_of(work, struct ucsi_tbt, work);
39+
40+
if (typec_altmode_vdm(tbt->alt, tbt->header, NULL, 0))
41+
dev_err(&tbt->alt->dev, "VDM 0x%x failed\n", tbt->header);
42+
43+
tbt->header = 0;
44+
}
45+
46+
static int ucsi_thunderbolt_set_altmode(struct ucsi_tbt *tbt,
47+
bool enter, u32 vdo)
48+
{
49+
int svdm_version;
50+
int cmd;
51+
int ret;
52+
u64 command = UCSI_SET_NEW_CAM |
53+
UCSI_CONNECTOR_NUMBER(tbt->con->num) |
54+
UCSI_SET_NEW_CAM_SET_AM(tbt->cam) |
55+
((u64)vdo << 32);
56+
57+
if (enter)
58+
command |= (1 << 23);
59+
60+
ret = ucsi_send_command(tbt->con->ucsi, command, NULL, 0);
61+
if (ret < 0)
62+
return ret;
63+
64+
svdm_version = typec_altmode_get_svdm_version(tbt->alt);
65+
if (svdm_version < 0)
66+
return svdm_version;
67+
68+
if (enter)
69+
cmd = CMD_ENTER_MODE;
70+
else
71+
cmd = CMD_EXIT_MODE;
72+
tbt->header = VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd);
73+
tbt->header |= VDO_OPOS(TYPEC_TBT_MODE);
74+
tbt->header |= VDO_CMDT(CMDT_RSP_ACK);
75+
76+
schedule_work(&tbt->work);
77+
78+
return 0;
79+
}
80+
81+
static int ucsi_thunderbolt_enter(struct typec_altmode *alt, u32 *vdo)
82+
{
83+
struct ucsi_tbt *tbt = typec_altmode_get_drvdata(alt);
84+
struct ucsi_connector *con = tbt->con;
85+
u64 command;
86+
u8 cur = 0;
87+
int ret;
88+
89+
if (!ucsi_con_mutex_lock(con))
90+
return -ENOTCONN;
91+
92+
command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
93+
ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
94+
if (ret < 0) {
95+
if (con->ucsi->version > 0x0100)
96+
goto err_unlock;
97+
cur = 0xff;
98+
}
99+
100+
if (cur != 0xff) {
101+
if (cur >= UCSI_MAX_ALTMODES || con->port_altmode[cur] != alt)
102+
ret = -EBUSY;
103+
else
104+
ret = 0;
105+
goto err_unlock;
106+
}
107+
108+
ret = ucsi_thunderbolt_set_altmode(tbt, true, *vdo);
109+
ucsi_altmode_update_active(tbt->con);
110+
111+
err_unlock:
112+
ucsi_con_mutex_unlock(con);
113+
114+
return ret;
115+
}
116+
117+
static int ucsi_thunderbolt_exit(struct typec_altmode *alt)
118+
{
119+
struct ucsi_tbt *tbt = typec_altmode_get_drvdata(alt);
120+
int ret;
121+
122+
if (!ucsi_con_mutex_lock(tbt->con))
123+
return -ENOTCONN;
124+
125+
ret = ucsi_thunderbolt_set_altmode(tbt, false, 0);
126+
127+
ucsi_con_mutex_unlock(tbt->con);
128+
129+
return ret;
130+
}
131+
132+
static int ucsi_thunderbolt_vdm(struct typec_altmode *alt,
133+
u32 header, const u32 *data, int count)
134+
{
135+
struct ucsi_tbt *tbt = typec_altmode_get_drvdata(alt);
136+
int cmd_type = PD_VDO_CMDT(header);
137+
int cmd = PD_VDO_CMD(header);
138+
int svdm_version;
139+
140+
if (!ucsi_con_mutex_lock(tbt->con))
141+
return -ENOTCONN;
142+
143+
svdm_version = typec_altmode_get_svdm_version(alt);
144+
if (svdm_version < 0) {
145+
ucsi_con_mutex_unlock(tbt->con);
146+
return svdm_version;
147+
}
148+
149+
switch (cmd_type) {
150+
case CMDT_INIT:
151+
if (PD_VDO_SVDM_VER(header) < svdm_version) {
152+
svdm_version = PD_VDO_SVDM_VER(header);
153+
typec_partner_set_svdm_version(tbt->con->partner, svdm_version);
154+
}
155+
tbt->header = VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd);
156+
tbt->header |= VDO_OPOS(TYPEC_TBT_MODE);
157+
tbt->header |= VDO_CMDT(CMDT_RSP_ACK);
158+
159+
schedule_work(&tbt->work);
160+
break;
161+
default:
162+
break;
163+
}
164+
165+
ucsi_con_mutex_unlock(tbt->con);
166+
167+
return 0;
168+
}
169+
170+
static const struct typec_altmode_ops ucsi_thunderbolt_ops = {
171+
.enter = ucsi_thunderbolt_enter,
172+
.exit = ucsi_thunderbolt_exit,
173+
.vdm = ucsi_thunderbolt_vdm,
174+
};
175+
176+
struct typec_altmode *ucsi_register_thunderbolt(struct ucsi_connector *con,
177+
bool override, int offset,
178+
struct typec_altmode_desc *desc)
179+
{
180+
struct typec_altmode *alt;
181+
struct ucsi_tbt *tbt;
182+
183+
alt = typec_port_register_altmode(con->port, desc);
184+
if (IS_ERR(alt) || !override)
185+
return alt;
186+
187+
tbt = devm_kzalloc(&alt->dev, sizeof(*tbt), GFP_KERNEL);
188+
if (!tbt) {
189+
typec_unregister_altmode(alt);
190+
return ERR_PTR(-ENOMEM);
191+
}
192+
193+
tbt->cam = offset;
194+
tbt->con = con;
195+
tbt->alt = alt;
196+
INIT_WORK(&tbt->work, ucsi_thunderbolt_work);
197+
typec_altmode_set_drvdata(alt, tbt);
198+
typec_altmode_set_ops(alt, &ucsi_thunderbolt_ops);
199+
200+
return alt;
201+
}
202+
203+
void ucsi_thunderbolt_remove_partner(struct typec_altmode *alt)
204+
{
205+
struct ucsi_tbt *tbt;
206+
207+
if (alt) {
208+
tbt = typec_altmode_get_drvdata(alt);
209+
if (tbt)
210+
cancel_work_sync(&tbt->work);
211+
}
212+
}

drivers/usb/typec/ucsi/ucsi.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/delay.h>
1414
#include <linux/slab.h>
1515
#include <linux/usb/typec_dp.h>
16+
#include <linux/usb/typec_tbt.h>
1617

1718
#include "ucsi.h"
1819
#include "trace.h"
@@ -417,6 +418,9 @@ static int ucsi_register_altmode(struct ucsi_connector *con,
417418
alt = ucsi_register_displayport(con, override,
418419
i, desc);
419420
break;
421+
case USB_TYPEC_TBT_SID:
422+
alt = ucsi_register_thunderbolt(con, override, i, desc);
423+
break;
420424
default:
421425
alt = typec_port_register_altmode(con->port, desc);
422426
break;
@@ -647,12 +651,15 @@ static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
647651
}
648652

649653
while (adev[i]) {
650-
if (recipient == UCSI_RECIPIENT_SOP &&
651-
(adev[i]->svid == USB_TYPEC_DP_SID ||
652-
(adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
653-
adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
654+
if (recipient == UCSI_RECIPIENT_SOP) {
654655
pdev = typec_altmode_get_partner(adev[i]);
655-
ucsi_displayport_remove_partner((void *)pdev);
656+
657+
if (adev[i]->svid == USB_TYPEC_DP_SID ||
658+
(adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
659+
adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))
660+
ucsi_displayport_remove_partner((void *)pdev);
661+
else if (adev[i]->svid == USB_TYPEC_TBT_SID)
662+
ucsi_thunderbolt_remove_partner((void *)pdev);
656663
}
657664
typec_unregister_altmode(adev[i]);
658665
adev[i++] = NULL;
@@ -1318,6 +1325,7 @@ static void ucsi_handle_connector_change(struct work_struct *work)
13181325

13191326
if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) {
13201327
ucsi_partner_change(con);
1328+
ucsi_altmode_update_active(con);
13211329

13221330
/* Complete pending data role swap */
13231331
if (!completion_done(&con->complete))

drivers/usb/typec/ucsi/ucsi.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,26 @@ static inline void
600600
ucsi_displayport_remove_partner(struct typec_altmode *adev) { }
601601
#endif /* CONFIG_TYPEC_DP_ALTMODE */
602602

603+
#if IS_ENABLED(CONFIG_TYPEC_TBT_ALTMODE)
604+
struct typec_altmode *
605+
ucsi_register_thunderbolt(struct ucsi_connector *con,
606+
bool override, int offset,
607+
struct typec_altmode_desc *desc);
608+
609+
void ucsi_thunderbolt_remove_partner(struct typec_altmode *adev);
610+
#else
611+
static inline struct typec_altmode *
612+
ucsi_register_thunderbolt(struct ucsi_connector *con,
613+
bool override, int offset,
614+
struct typec_altmode_desc *desc)
615+
{
616+
return typec_port_register_altmode(con->port, desc);
617+
}
618+
619+
static inline void
620+
ucsi_thunderbolt_remove_partner(struct typec_altmode *adev) { }
621+
#endif /* CONFIG_TYPEC_TBT_ALTMODE */
622+
603623
#ifdef CONFIG_DEBUG_FS
604624
void ucsi_debugfs_init(void);
605625
void ucsi_debugfs_exit(void);

0 commit comments

Comments
 (0)