From nobody Sun Feb 8 12:20:26 2026 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 721A43033EF for ; Mon, 26 Jan 2026 09:06:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769418387; cv=none; b=Jz4wEQx3Sc0uR3t9jxEJ3Qqnriwp59kC1l16J4QBWdsEiqB6laus/y4xyxbgrr4rnqhC2vr89aquiwh8j8GfsWEWooNLYNJfFylBOUonQnzBBfTyA9a0T8OYVQTVeQJQrO4FuCNGczAF/W2JbioWWtJ4nb73m3FEPbwWF2vt2nA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769418387; c=relaxed/simple; bh=KY83muG2ZW+6ROgcrhF3RmXusvN2d/Jnw7vj7y215is=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=t+dXp+4uEIOZXyscv9FN/A53bUbOQE8aKotnN61k/McYeRlyhp60bxCvRfeNM0Ye9Avm9atSrOxkZWyE1e68+D3gsMiqAGjaF/7kd21rOMrq7xwKg1nmNIVZjfVRsYqQpFGEEa8R+1Wj1FAhYGsIlNekoZKruqFtTVjCvWwL4NA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=DJkSEsGi; arc=none smtp.client-ip=209.85.208.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="DJkSEsGi" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-6581af9c94aso8389267a12.1 for ; Mon, 26 Jan 2026 01:06:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1769418384; x=1770023184; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=L28omD9h5A1Pa+2UFWswURo4v1rX609r7Db36NIyuBA=; b=DJkSEsGiBda5ljU3gfdVZhaYDXdn2OP9Ii22Z3KeXQVQYQ6eyPMU7Yf/+dTuazrJ63 Vr5UCUABDeTGZnn8JCvkqLQLd+MJMKRL9K0OysFY7aFOR2vkJp9DQxZQpuMUczoHd0qO d78QBjEzr/KM62C+7r6equnr6FbreROitDQTg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769418384; x=1770023184; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=L28omD9h5A1Pa+2UFWswURo4v1rX609r7Db36NIyuBA=; b=j4KMYK2vW8FidzeIueGyIceCi5Hx1aKasXiBAosAV+GHUBCszJCcg3SyresBRrx3HS kBmUFX3wzDlBtpYXMCrvjcT1HX/0rMnHTt6IeOVcGyO+MMgR/UOoXkDdscaBt8D/ILHh YWCODeRrY4tyd373gWpnsumsjBDLnp9CtiAFuCZa1PLyaCpzRlvX9PsodPMWCvaUDicv 7Eg8kCyL27jplNe1PaY2qyjj+y4+rzhLsEqzo2hoCVDjOW1eIcBu3aEN3SakhvYygCJX o68YviT8Ue4huUVclxU3FgJ6FJQ6gMbcSK/qfmcrEDealN1etjwYp9XAxl/0gFBTQEko KCww== X-Forwarded-Encrypted: i=1; AJvYcCUhF8Gc4UgSsS59a8TvPft6/5ILAmZorgod8Gas29y0rOXjiXQL0baGoP8n7xaxRWFR0dfXxCiFAjzz4A0=@vger.kernel.org X-Gm-Message-State: AOJu0YxPoC+W4m1p2hUpC82A5aTvDp9lM2h8zKT6YCZ1mUpeP0u0SfLO NGLWWu49wJ0vYZlNIo/skBs8E/+6h0ctepAE583/gjtRQinNLhJvOmERuVaoC93GpQ== X-Gm-Gg: AZuq6aL4GShOgmJO7WPD85Tkb4k+pgwYqBr5lpmdcaq2NxsDOnbwT+wzMO74Oiu76xc bBi3S56/Yh//uhi9NGSGXJevcz3yMKadn7cbNJBPOWd5iLJfGSie/aIY7zOKSvoaE1kC+wo0oks QvySuqsSZPcETIbhTB7NNsyZEwN0EkSEolEVyV/Rb2JiX50dneUx87SZhE7x4wMox4y5YKmh6Wz bJBG5jyT0+48QYnsjxBJeTuMUAbYsFrlqh4BRTxREhXrBn5Qe8ZrdSfL6YweV67peq1gGsYyNRg 0cFKainkYRghZYh/4VJbg3DqHwodOAUc/gmP2f+B80GQZcwBE7id65rgfZ6ZPvahgxcoz5Bqj8I a8Sb4VVc/TSC3obP3kUm4J/ICG8SbgtqowNMbkBCMHxaTsUdqXsoHpSnDZAcSgoVSOCgtdzqdEs h8dQXNjn3E3Zobq40EUL6wgkpjdt6G/U3K6pXp7C795XB9qBIcA/CI+rdrwUUYiPzkbOUVer6FJ 7lM4zig X-Received: by 2002:a17:907:9713:b0:b86:f81d:66b1 with SMTP id a640c23a62f3a-b8d0a73aeb1mr251837366b.2.1769418383663; Mon, 26 Jan 2026 01:06:23 -0800 (PST) Received: from akuchynski.c.googlers.com.com (150.230.32.34.bc.googleusercontent.com. [34.32.230.150]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b885b7bb0b0sm614579666b.67.2026.01.26.01.06.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jan 2026 01:06:22 -0800 (PST) From: Andrei Kuchynski To: Heikki Krogerus , Jameson Thies , Benson Leung , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Abhishek Pandit-Subedi , Greg Kroah-Hartman , Dmitry Baryshkov , Pooja Katiyar , Johan Hovold , Hsin-Te Yuan , Madhu M , Venkat Jayaraman , Pengyu Luo , Fedor Pchelkin , Andrei Kuchynski Subject: [PATCH] usb: typec: ucsi: Add Thunderbolt alternate mode support Date: Mon, 26 Jan 2026 09:06:16 +0000 Message-ID: <20260126090616.2224907-1-akuchynski@chromium.org> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- drivers/usb/typec/ucsi/Makefile | 4 + drivers/usb/typec/ucsi/thunderbolt.c | 205 +++++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.c | 18 ++- drivers/usb/typec/ucsi/ucsi.h | 20 +++ 4 files changed, 242 insertions(+), 5 deletions(-) create mode 100644 drivers/usb/typec/ucsi/thunderbolt.c diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makef= ile index dbc571763eff6..c7e38bf01350d 100644 --- a/drivers/usb/typec/ucsi/Makefile +++ b/drivers/usb/typec/ucsi/Makefile @@ -17,6 +17,10 @@ ifneq ($(CONFIG_TYPEC_DP_ALTMODE),) typec_ucsi-y +=3D displayport.o endif =20 +ifneq ($(CONFIG_TYPEC_TBT_ALTMODE),) + typec_ucsi-y +=3D thunderbolt.o +endif + obj-$(CONFIG_UCSI_ACPI) +=3D ucsi_acpi.o obj-$(CONFIG_UCSI_CCG) +=3D ucsi_ccg.o obj-$(CONFIG_UCSI_STM32G0) +=3D ucsi_stm32g0.o diff --git a/drivers/usb/typec/ucsi/thunderbolt.c b/drivers/usb/typec/ucsi/= thunderbolt.c new file mode 100644 index 0000000000000..e3232544ce4c5 --- /dev/null +++ b/drivers/usb/typec/ucsi/thunderbolt.c @@ -0,0 +1,205 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * UCSI Thunderbolt Alternate Mode Support + * + * Copyright 2025 Google LLC + */ + +#include +#include + +#include "ucsi.h" + +/** + * struct ucsi_tbt - Thunderbolt Alternate Mode private data structure + * @con: Pointer to UCSI connector structure + * @alt: Pointer to typec altmode structure + * @work: Work structure + * @cam: An offset into the list of alternate modes supported by the PPM + * @header: VDO header + */ +struct ucsi_tbt { + struct ucsi_connector *con; + struct typec_altmode *alt; + struct work_struct work; + int cam; + u32 header; +}; + +static void ucsi_thunderbolt_work(struct work_struct *work) +{ + struct ucsi_tbt *tbt =3D container_of(work, struct ucsi_tbt, work); + + if (typec_altmode_vdm(tbt->alt, tbt->header, NULL, 0)) + dev_err(&tbt->alt->dev, "VDM 0x%x failed\n", tbt->header); + + tbt->header =3D 0; +} + +static int ucsi_thunderbolt_set_altmode(struct ucsi_tbt *tbt, + bool enter, u32 vdo) +{ + int svdm_version; + int cmd; + int ret; + u64 command =3D UCSI_SET_NEW_CAM | + UCSI_CONNECTOR_NUMBER(tbt->con->num) | + UCSI_SET_NEW_CAM_SET_AM(tbt->cam) | + ((u64)vdo << 32); + + if (enter) + command |=3D (1 << 23); + + ret =3D ucsi_send_command(tbt->con->ucsi, command, NULL, 0); + if (ret < 0) + return ret; + + svdm_version =3D typec_altmode_get_svdm_version(tbt->alt); + if (svdm_version < 0) + return svdm_version; + + if (enter) + cmd =3D CMD_ENTER_MODE; + else + cmd =3D CMD_EXIT_MODE; + tbt->header =3D VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd); + tbt->header |=3D VDO_OPOS(TYPEC_TBT_MODE); + tbt->header |=3D VDO_CMDT(CMDT_RSP_ACK); + + schedule_work(&tbt->work); + + return 0; +} + +static int ucsi_thunderbolt_enter(struct typec_altmode *alt, u32 *vdo) +{ + struct ucsi_tbt *tbt =3D typec_altmode_get_drvdata(alt); + struct ucsi_connector *con =3D tbt->con; + u64 command; + u8 cur =3D 0; + int ret; + + if (!ucsi_con_mutex_lock(con)) + return -ENOTCONN; + + command =3D UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num); + ret =3D ucsi_send_command(con->ucsi, command, &cur, sizeof(cur)); + if (ret < 0) { + if (con->ucsi->version > 0x0100) + goto err_unlock; + cur =3D 0xff; + } + + if (cur !=3D 0xff) { + if (cur >=3D UCSI_MAX_ALTMODES || con->port_altmode[cur] !=3D alt) + ret =3D -EBUSY; + else + ret =3D 0; + goto err_unlock; + } + + ret =3D ucsi_thunderbolt_set_altmode(tbt, true, *vdo); + ucsi_altmode_update_active(tbt->con); + +err_unlock: + ucsi_con_mutex_unlock(con); + + return ret; +} + +static int ucsi_thunderbolt_exit(struct typec_altmode *alt) +{ + struct ucsi_tbt *tbt =3D typec_altmode_get_drvdata(alt); + int ret; + + if (!ucsi_con_mutex_lock(tbt->con)) + return -ENOTCONN; + + ret =3D ucsi_thunderbolt_set_altmode(tbt, false, 0); + + ucsi_con_mutex_unlock(tbt->con); + + return ret; +} + +static int ucsi_thunderbolt_vdm(struct typec_altmode *alt, + u32 header, const u32 *data, int count) +{ + struct ucsi_tbt *tbt =3D typec_altmode_get_drvdata(alt); + int cmd_type =3D PD_VDO_CMDT(header); + int cmd =3D PD_VDO_CMD(header); + int svdm_version; + + if (!ucsi_con_mutex_lock(tbt->con)) + return -ENOTCONN; + + svdm_version =3D typec_altmode_get_svdm_version(alt); + if (svdm_version < 0) { + ucsi_con_mutex_unlock(tbt->con); + return svdm_version; + } + + switch (cmd_type) { + case CMDT_INIT: + if (PD_VDO_SVDM_VER(header) < svdm_version) { + svdm_version =3D PD_VDO_SVDM_VER(header); + typec_partner_set_svdm_version(tbt->con->partner, svdm_version); + } + tbt->header =3D VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd); + tbt->header |=3D VDO_OPOS(TYPEC_TBT_MODE); + tbt->header |=3D VDO_CMDT(CMDT_RSP_ACK); + + schedule_work(&tbt->work); + break; + default: + break; + } + + ucsi_con_mutex_unlock(tbt->con); + + return 0; +} + +static const struct typec_altmode_ops ucsi_thunderbolt_ops =3D { + .enter =3D ucsi_thunderbolt_enter, + .exit =3D ucsi_thunderbolt_exit, + .vdm =3D ucsi_thunderbolt_vdm, +}; + +struct typec_altmode *ucsi_register_thunderbolt(struct ucsi_connector *con, + bool override, int offset, + struct typec_altmode_desc *desc) +{ + struct typec_altmode *alt; + struct ucsi_tbt *tbt; + + alt =3D typec_port_register_altmode(con->port, desc); + if (IS_ERR(alt) || !override) + return alt; + + tbt =3D devm_kzalloc(&alt->dev, sizeof(*tbt), GFP_KERNEL); + if (!tbt) { + typec_unregister_altmode(alt); + return ERR_PTR(-ENOMEM); + } + + tbt->cam =3D offset; + tbt->con =3D con; + tbt->alt =3D alt; + INIT_WORK(&tbt->work, ucsi_thunderbolt_work); + typec_altmode_set_drvdata(alt, tbt); + typec_altmode_set_ops(alt, &ucsi_thunderbolt_ops); + + return alt; +} + +void ucsi_thunderbolt_remove_partner(struct typec_altmode *alt) +{ + struct ucsi_tbt *tbt; + + if (alt) { + tbt =3D typec_altmode_get_drvdata(alt); + if (tbt) + cancel_work_sync(&tbt->work); + } +} diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 251990475faa7..91b6c71dd7396 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -13,6 +13,7 @@ #include #include #include +#include =20 #include "ucsi.h" #include "trace.h" @@ -417,6 +418,9 @@ static int ucsi_register_altmode(struct ucsi_connector = *con, alt =3D ucsi_register_displayport(con, override, i, desc); break; + case USB_TYPEC_TBT_SID: + alt =3D ucsi_register_thunderbolt(con, override, i, desc); + break; default: alt =3D typec_port_register_altmode(con->port, desc); break; @@ -647,12 +651,15 @@ static void ucsi_unregister_altmodes(struct ucsi_conn= ector *con, u8 recipient) } =20 while (adev[i]) { - if (recipient =3D=3D UCSI_RECIPIENT_SOP && - (adev[i]->svid =3D=3D USB_TYPEC_DP_SID || - (adev[i]->svid =3D=3D USB_TYPEC_NVIDIA_VLINK_SID && - adev[i]->vdo !=3D USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) { + if (recipient =3D=3D UCSI_RECIPIENT_SOP) { pdev =3D typec_altmode_get_partner(adev[i]); - ucsi_displayport_remove_partner((void *)pdev); + + if (adev[i]->svid =3D=3D USB_TYPEC_DP_SID || + (adev[i]->svid =3D=3D USB_TYPEC_NVIDIA_VLINK_SID && + adev[i]->vdo !=3D USB_TYPEC_NVIDIA_VLINK_DBG_VDO)) + ucsi_displayport_remove_partner((void *)pdev); + else if (adev[i]->svid =3D=3D USB_TYPEC_TBT_SID) + ucsi_thunderbolt_remove_partner((void *)pdev); } typec_unregister_altmode(adev[i]); adev[i++] =3D NULL; @@ -1318,6 +1325,7 @@ static void ucsi_handle_connector_change(struct work_= struct *work) =20 if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) { ucsi_partner_change(con); + ucsi_altmode_update_active(con); =20 /* Complete pending data role swap */ if (!completion_done(&con->complete)) diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 4797b4aa1e35b..43a0d01ade8ff 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -600,6 +600,26 @@ static inline void ucsi_displayport_remove_partner(struct typec_altmode *adev) { } #endif /* CONFIG_TYPEC_DP_ALTMODE */ =20 +#if IS_ENABLED(CONFIG_TYPEC_TBT_ALTMODE) +struct typec_altmode * +ucsi_register_thunderbolt(struct ucsi_connector *con, + bool override, int offset, + struct typec_altmode_desc *desc); + +void ucsi_thunderbolt_remove_partner(struct typec_altmode *adev); +#else +static inline struct typec_altmode * +ucsi_register_thunderbolt(struct ucsi_connector *con, + bool override, int offset, + struct typec_altmode_desc *desc) +{ + return typec_port_register_altmode(con->port, desc); +} + +static inline void +ucsi_thunderbolt_remove_partner(struct typec_altmode *adev) { } +#endif /* CONFIG_TYPEC_TBT_ALTMODE */ + #ifdef CONFIG_DEBUG_FS void ucsi_debugfs_init(void); void ucsi_debugfs_exit(void); --=20 2.52.0.457.g6b5491de43-goog