* [PATCH v4 0/2] usb: typec: add CIX Type-C PD controller driver
@ 2026-11-13 12:53 Gary Yang
2026-11-13 12:53 ` [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Gary Yang @ 2026-11-13 12:53 UTC (permalink / raw)
To: Heikki Krogerus, Greg Kroah-Hartman, Rob Herring,
Krzysztof Kozlowski, Conor Dooley
Cc: linux-usb, devicetree, linux-kernel, cix-kernel-upstream,
Gary Yang
This series adds a Type-C PD controller driver for CIX platforms.
The first supported controller is the Realtek RTS5453H, a stand-alone
port controller: the CC and USB Power Delivery state machines run in
its firmware and are configured through vendor registers rather than
the TCPC register interface, so it cannot be driven by the tcpm
framework. The driver translates the controller status registers into
Type-C class notifications and drives the platform PHY's orientation
switch and mode mux.
The driver is named after the CIX platform integration rather than the
RTS5453H so that further register-compatible PD controllers can be
supported without renaming it.
Patch 1 adds the devicetree binding and patch 2 adds the driver and its
MAINTAINERS entry. The DTS patches from v1 are sent separately through
the SoC tree.
Changes in v4:
- Keep the interrupt disabled across system sleep and re-enable it in
->resume() only, after the parent I2C adapter has resumed.
- Return IRQ_NONE when the port status did not change, so that the
interrupt core can attribute the shared level-triggered line and
disable a line that stays asserted.
- Mark the DisplayPort alternate mode active only after a driver is
bound, so that the module reference taken by
typec_altmode_update_active() stays balanced.
- binding: drop the nodes below the documented device from the
example.
- Carry Reviewed-by from Krzysztof Kozlowski.
Changes in v3:
- Report the Type-C data role from the controller status for every
connection, including DisplayPort-only configurations where no USB
data role is available, instead of deriving it from the USB role
switch.
- Pass the port alternate mode in the mux state, matching other
firmware-driven port drivers.
- Force the USB role switch to be reprogrammed on system resume and
check the enable_irq_wake() return value.
- Acknowledge a pending controller interrupt during probe so that a
shared interrupt line is not left asserted.
- Do not drop the borrowed fwnode reference when parsing the connector
properties fails.
- Treat all fwnode_usb_role_switch_get() errors as fatal; a connector
without a role switch link returns NULL, not -ENODEV.
- Drop a redundant include.
Changes in v2:
- Drop the DTS patches from the series.
- binding: describe the single connector as a plain "connector" child
node instead of an addressed node, and stop requiring
"try-power-role", which only applies to dual-role ports.
- driver: add the missing USB_ROLE_SWITCH Kconfig dependency.
- driver: register the DisplayPort partner alternate mode only for
actual DP connections, with a valid VDO, call
typec_altmode_update_active() on attach/detach and set
no_mode_control since the firmware drives mode entry.
- driver: never pass a NULL alternate mode to typec_mux_set(), and
treat debug accessories as disconnected everywhere.
- driver: handle USB 2.0-only connections in orientation, role and
mux state.
- driver: only cache the USB role after the role switch accepted it,
and force a role refresh on resume.
- driver: use typec_get_fw_cap() instead of open-coding the connector
properties.
- driver: acquire the mux and role switch before registering the
port, use dev_err_probe() on probe failures and reset the mux to the
safe state on teardown.
- driver: remove the undocumented disable_pd_interrupt sysfs attribute
and the manual disable_irq()/IRQ_DISABLE_UNLAZY handling that could
leak the shared IRQ's disable depth. This also fixes the build error
reported by the kernel test robot on sparc64 and s390.
- driver: use a named i2c_device_id initializer and add the missing
hibernation PM callbacks.
Tested on a Radxa Orion O6 (Sky1): attach/detach, USB-only and DP-only
alt modes, cable orientation detection and HPD reporting all work.
Gary Yang (2):
dt-bindings: usb: add Realtek RTS5453H Type-C port controller
usb: typec: add CIX Type-C PD controller driver
.../bindings/usb/realtek,rts5453h.yaml | 70 ++
MAINTAINERS | 7 +
drivers/usb/typec/Kconfig | 13 +
drivers/usb/typec/Makefile | 1 +
drivers/usb/typec/cix_pd.c | 768 ++++++++++++++++++
5 files changed, 859 insertions(+)
create mode 100644 Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml
create mode 100644 drivers/usb/typec/cix_pd.c
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller 2026-11-13 12:53 [PATCH v4 0/2] usb: typec: add CIX Type-C PD controller driver Gary Yang @ 2026-11-13 12:53 ` Gary Yang 2026-11-13 14:15 ` sashiko-bot 2026-11-13 12:53 ` [PATCH v4 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2026-11-16 8:15 ` [PATCH v5 0/2] " Gary Yang 2 siblings, 1 reply; 10+ messages in thread From: Gary Yang @ 2026-11-13 12:53 UTC (permalink / raw) To: Heikki Krogerus, Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-kernel, cix-kernel-upstream, Gary Yang, Krzysztof Kozlowski Document the Realtek RTS5453H stand-alone USB Type-C port controller. The controller is connected over I2C and manages a single USB Type-C connector, described with the generic USB connector binding. The connector's OF graph links to the Type-C orientation switch and mode mux provided by the platform PHY. Signed-off-by: Gary Yang <gary.yang@cixtech.com> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com> --- .../bindings/usb/realtek,rts5453h.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml diff --git a/Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml b/Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml new file mode 100644 index 000000000000..f7c062ba1114 --- /dev/null +++ b/Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/usb/realtek,rts5453h.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Realtek RTS5453H USB Type-C port controller + +description: | + The RTS5453H is a stand-alone USB Type-C port controller with USB + Power Delivery support. It is controlled over I2C and reports + changes of the port state through an interrupt line. + + Orientation switching and USB/DisplayPort lane muxing are performed by + the platform PHY. The controller finds the Type-C switch and mux + through the OF graph of its connector node. + +maintainers: + - Gary Yang <gary.yang@cixtech.com> + +properties: + compatible: + const: realtek,rts5453h + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + connector: + type: object + $ref: /schemas/connector/usb-connector.yaml# + unevaluatedProperties: false + description: + The USB Type-C connector managed by this controller. + + required: + - data-role + - power-role + +required: + - compatible + - reg + - interrupts + - connector + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + typec@30 { + compatible = "realtek,rts5453h"; + reg = <0x30>; + interrupt-parent = <&gpio0>; + interrupts = <8 IRQ_TYPE_LEVEL_LOW>; + + connector { + compatible = "usb-c-connector"; + data-role = "host"; + power-role = "source"; + }; + }; + }; ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller 2026-11-13 12:53 ` [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang @ 2026-11-13 14:15 ` sashiko-bot 0 siblings, 0 replies; 10+ messages in thread From: sashiko-bot @ 2026-11-13 14:15 UTC (permalink / raw) To: Gary Yang; +Cc: linux-usb > Document the Realtek RTS5453H stand-alone USB Type-C port controller. > The controller is connected over I2C and manages a single USB Type-C > connector, described with the generic USB connector binding. > > The connector's OF graph links to the Type-C orientation switch and > mode mux provided by the platform PHY. > > Signed-off-by: Gary Yang <gary.yang@cixtech.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261113125304.1693911-2-gary.yang@cixtech.com?part=1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v4 2/2] usb: typec: add CIX Type-C PD controller driver 2026-11-13 12:53 [PATCH v4 0/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2026-11-13 12:53 ` [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang @ 2026-11-13 12:53 ` Gary Yang 2026-11-13 14:31 ` sashiko-bot 2026-11-16 8:15 ` [PATCH v5 0/2] " Gary Yang 2 siblings, 1 reply; 10+ messages in thread From: Gary Yang @ 2026-11-13 12:53 UTC (permalink / raw) To: Heikki Krogerus, Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-kernel, cix-kernel-upstream, Gary Yang Add a driver for the CIX Type-C PD controller, which is connected over I2C and reports power, data and plug orientation changes through a level-triggered interrupt. The Realtek RTS5453H is the first supported device. The controller runs the Type-C and PD state machines in firmware and exposes the current port status over I2C. The driver translates that into Type-C class notifications, driving the orientation switch and mode mux registered by the platform PHY for the DisplayPort alternate mode. It also drives the platform USB role switch so that the USB controller follows the negotiated data role. The platform wires several controller instances to one shared level-triggered interrupt line, and the controller has no per-instance pending bit. Each instance acknowledges its own interrupt and reports IRQ_NONE when the port status did not change since the last report, so the interrupt core can attribute the interrupt and can disable a line that a controller keeps asserted without ever clearing it. Reporting IRQ_HANDLED unconditionally would turn such a line into an uninterruptible interrupt storm. The interrupt handler accesses the controller over I2C, so the interrupt is disabled for the whole system sleep and re-enabled only after the I2C adapter has resumed. The core re-enables the line in resume_device_irqs() before the adapter is resumed, which would otherwise let the handler access a suspended bus. The DisplayPort alternate mode is entered by the controller firmware, so the driver reports it as active only once an alternate mode driver is bound to the mode device: typec_altmode_update_active() takes a module reference only when a driver is bound, and reporting the mode earlier would unbalance that reference when the mode is deactivated. The driver is named after the CIX platform integration rather than the RTS5453H so that further register-compatible PD controllers can be supported without renaming it. Signed-off-by: Gary Yang <gary.yang@cixtech.com> --- MAINTAINERS | 7 + drivers/usb/typec/Kconfig | 13 + drivers/usb/typec/Makefile | 1 + drivers/usb/typec/cix_pd.c | 768 +++++++++++++++++++++++++++++++++++++ 4 files changed, 789 insertions(+) create mode 100644 drivers/usb/typec/cix_pd.c diff --git a/MAINTAINERS b/MAINTAINERS index 93abbb315065..bd99313bf91d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6522,6 +6522,13 @@ M: Satish Kharat <satishkh@cisco.com> S: Supported F: drivers/infiniband/hw/usnic/ +CIX USB TYPE-C PD CONTROLLER +M: Gary Yang <gary.yang@cixtech.com> +L: linux-usb@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml +F: drivers/usb/typec/cix_pd.c + CLANG CONTEXT ANALYSIS M: Marco Elver <elver@google.com> R: Bart Van Assche <bvanassche@acm.org> diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig index 2f80c2792dbd..4d62f5deeeba 100644 --- a/drivers/usb/typec/Kconfig +++ b/drivers/usb/typec/Kconfig @@ -110,6 +110,19 @@ config TYPEC_WUSB3801 If you choose to build this driver as a dynamically linked module, the module will be called wusb3801.ko. +config TYPEC_CIX_PD + tristate "CIX Type-C PD controller driver" + depends on I2C + depends on USB_ROLE_SWITCH + depends on ARCH_CIX || COMPILE_TEST + select REGMAP_I2C + help + Say Y or M here if your system has a CIX Type-C PD controller. + This driver currently supports the Realtek RTS5453H controller. + + If you choose to build this driver as a dynamically linked module, the + module will be called cix_pd.ko. + source "drivers/usb/typec/mux/Kconfig" source "drivers/usb/typec/altmodes/Kconfig" diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile index 8a6a1c663eb6..bb3f3139fd7e 100644 --- a/drivers/usb/typec/Makefile +++ b/drivers/usb/typec/Makefile @@ -11,4 +11,5 @@ obj-$(CONFIG_TYPEC_HD3SS3220) += hd3ss3220.o obj-$(CONFIG_TYPEC_STUSB160X) += stusb160x.o obj-$(CONFIG_TYPEC_RT1719) += rt1719.o obj-$(CONFIG_TYPEC_WUSB3801) += wusb3801.o +obj-$(CONFIG_TYPEC_CIX_PD) += cix_pd.o obj-$(CONFIG_TYPEC) += mux/ diff --git a/drivers/usb/typec/cix_pd.c b/drivers/usb/typec/cix_pd.c new file mode 100644 index 000000000000..b413a125c7e6 --- /dev/null +++ b/drivers/usb/typec/cix_pd.c @@ -0,0 +1,768 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2025 Cix Technology Group Co., Ltd. + * + * Driver for the CIX Type-C PD controller. + */ + +#include <linux/bits.h> +#include <linux/device.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/regmap.h> +#include <linux/string.h> +#include <linux/usb/role.h> +#include <linux/usb/typec.h> +#include <linux/usb/typec_altmode.h> +#include <linux/usb/typec_dp.h> +#include <linux/usb/typec_mux.h> + +#define RTS_DATA_CONTROL 0x50 +#define I2C_INT_ACK_BIT BIT(2) +#define HPD_IRQ_ACK BIT(5) +#define RTS_DATA_STATUS 0x5F +/* byte 1 */ +#define CONNECTION_ORIENTATION BIT(1) +#define USB2_CONNECTION BIT(4) +#define USB3_2_CONNECTION BIT(5) +#define USB_DATA_ROLE BIT(7) +/* byte 2 */ +#define DP_CONNECTION BIT(0) +#define DP_SOURCE_SINK BIT(1) +#define DEBUG_ACCESSORY_ATTACHED BIT(4) +#define HPD_IRQ BIT(6) +#define HPD_STATE BIT(7) +/* byte 3 */ +#define POWER_SOURCE BIT(0) +#define POWER_SINK BIT(1) +#define POWER_MASK 3 + +#define DATA_STATUS_COUNT 6 + +struct cix_pd { + struct device *dev; + struct regmap *regmap; + struct mutex lock; /* device lock */ + struct mutex irq_lock; /* protects IRQ state transitions */ + int id; + int irq; + u8 data_status[DATA_STATUS_COUNT]; + u8 prev_status[DATA_STATUS_COUNT]; + bool status_valid; + struct typec_displayport_data dp_data; + struct typec_mux_state state; + struct typec_capability caps; + struct typec_mux *mux; + struct usb_role_switch *role_sw; + struct typec_altmode *port_altmode; + struct typec_altmode *partner_altmode; + struct typec_port *port; + enum usb_role role; + unsigned long mode; + u32 hpd_status; + struct typec_partner *partner; + struct typec_partner_desc desc; + bool wake_enabled; + bool irq_disabled; +}; + +static int cix_pd_block_read(struct cix_pd *rts, u8 reg, void *val, size_t len) +{ + return regmap_bulk_read(rts->regmap, reg, val, len); +} + +static int cix_pd_block_write(struct cix_pd *rts, u8 reg, + const void *val, size_t len) +{ + return regmap_bulk_write(rts->regmap, reg, val, len); +} + +static const struct regmap_config cix_pd_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0x7F, +}; + +static const char *const dp_only_mode = "Alt Mode:DP"; +static const char *const dp_usb_mode = "Alt Mode:DP + USB"; +static const char *const usb_mode = "Alt Mode:USB"; + +static const char *const power_source = "power source"; +static const char *const power_sink = "power sink"; + +static const char *const orientation_normal = "orientation normal"; +static const char *const orientation_reverse = "orientation reverse"; + +static const char *const data_host = "usb host"; +static const char *const data_device = "usb device"; + +static const char *cix_pd_pwr_role_str(const struct cix_pd *rts) +{ + switch (rts->data_status[3] & POWER_MASK) { + case POWER_SOURCE: + return power_source; + case POWER_SINK: + return power_sink; + default: + return "unknown"; + } +} + +/* + * The controller negotiates the DisplayPort alternate mode autonomously, + * so the capabilities below describe the fixed set supported by the + * platform DP output: a receptacle exposing DFP_D with pin assignments + * C, D and E. + */ +#define CIX_PD_DP_PIN_ASSIGN (BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D) | \ + BIT(DP_PIN_ASSIGN_E)) + +static const struct typec_altmode_desc cix_pd_dp_port_altmode = { + .svid = USB_TYPEC_DP_SID, + .mode = USB_TYPEC_DP_MODE, + .vdo = DP_CAP_DFP_D | DP_CAP_RECEPTACLE | + (CIX_PD_DP_PIN_ASSIGN << 8), +}; + +static const struct typec_altmode_desc cix_pd_dp_partner_altmode = { + .svid = USB_TYPEC_DP_SID, + .mode = USB_TYPEC_DP_MODE, + .vdo = DP_CAP_UFP_D | DP_CAP_RECEPTACLE | + (CIX_PD_DP_PIN_ASSIGN << 16), + .mode_selection = true, +}; + +static int cix_pd_typec_port_update(struct cix_pd *typec, bool force) +{ + struct typec_displayport_data *dp_data = &typec->dp_data; + enum typec_orientation orientation; + enum usb_role role; + bool usb2, usb3, dp, connected, dev_role; + int ret, err; + + usb2 = typec->data_status[1] & USB2_CONNECTION; + usb3 = typec->data_status[1] & USB3_2_CONNECTION; + dp = typec->data_status[2] & DP_CONNECTION; + + /* + * Debug accessories are not supported, so they are reported as + * disconnected instead of being passed on to the framework. + */ + if (typec->data_status[2] & DEBUG_ACCESSORY_ATTACHED) { + dev_err(typec->dev, + "debug accessory mode not supported, try another usb cable\n"); + connected = false; + } else { + connected = usb2 || usb3 || dp; + } + + if (!connected) + orientation = TYPEC_ORIENTATION_NONE; + else if (typec->data_status[1] & CONNECTION_ORIENTATION) + orientation = TYPEC_ORIENTATION_REVERSE; + else + orientation = TYPEC_ORIENTATION_NORMAL; + + ret = typec_set_orientation(typec->port, orientation); + if (ret) + return ret; + + /* + * The controller reports the data role whenever a cable is attached, + * including DisplayPort-only configurations where no USB data role is + * available. Keep the port's data role in sync independently of the + * USB role switch. + */ + dev_role = typec->data_status[1] & USB_DATA_ROLE; + if (connected) + typec_set_data_role(typec->port, + dev_role ? TYPEC_DEVICE : TYPEC_HOST); + + if (!connected || !(usb2 || usb3)) + role = USB_ROLE_NONE; + else if (dev_role) + role = USB_ROLE_DEVICE; + else + role = USB_ROLE_HOST; + + if (force || typec->role != role) { + err = 0; + if (typec->role_sw) { + err = usb_role_switch_set_role(typec->role_sw, role); + if (err) + dev_err(typec->dev, + "typec port(%d): failed to set USB role: %d\n", + typec->id, err); + } + + /* Only cache the role once the switch accepted it. */ + if (!err) + typec->role = role; + } + + if ((typec->data_status[3] & POWER_MASK) == POWER_SOURCE) { + typec_set_pwr_role(typec->port, TYPEC_SOURCE); + } else if ((typec->data_status[3] & POWER_MASK) == POWER_SINK) { + typec_set_pwr_role(typec->port, TYPEC_SINK); + } else { + dev_dbg(typec->dev, "typec port(%d): unknown power state %d\n", + typec->id, typec->data_status[3]); + } + + if (!connected) { + if (typec->partner_altmode) { + typec_altmode_update_active(typec->partner_altmode, + false); + typec_unregister_altmode(typec->partner_altmode); + typec->partner_altmode = NULL; + } + if (typec->partner) { + dev_info(typec->dev, "typec port(%d): unregister partner\n", + typec->id); + typec_unregister_partner(typec->partner); + typec->partner = NULL; + } + } else { + if (!typec->partner) { + dev_info(typec->dev, "typec port(%d): register partner\n", + typec->id); + typec->partner = typec_register_partner(typec->port, + &typec->desc); + if (IS_ERR(typec->partner)) { + dev_err(typec->dev, + "typec port(%d): failed to register partner: %ld\n", + typec->id, PTR_ERR(typec->partner)); + typec->partner = NULL; + } + } + + if (typec->partner_altmode && !dp) { + typec_altmode_update_active(typec->partner_altmode, + false); + typec_unregister_altmode(typec->partner_altmode); + typec->partner_altmode = NULL; + } + + if (typec->partner && dp && !typec->partner_altmode) { + typec->partner_altmode = + typec_partner_register_altmode(typec->partner, + &cix_pd_dp_partner_altmode); + if (IS_ERR(typec->partner_altmode)) { + dev_err(typec->dev, + "typec port(%d): failed to register DP altmode: %ld\n", + typec->id, + PTR_ERR(typec->partner_altmode)); + typec->partner_altmode = NULL; + } + } + + /* + * typec_altmode_update_active() takes a reference to the + * alternate mode driver's module only when a driver is bound + * to the mode device. Reporting the mode as active without a + * driver would leave that reference unbalanced, and the later + * deactivation would drop a reference that was never taken. + * Report the mode only once a driver is bound, and retry on + * the next status update so that a driver loaded after the + * cable was attached still learns about the active mode. + */ + if (typec->partner_altmode && + device_is_bound(&typec->partner_altmode->dev)) + typec_altmode_update_active(typec->partner_altmode, true); + } + + dp_data->status = 0; + typec->state.data = NULL; + typec->state.alt = NULL; + + /* + * Only pass a DisplayPort state to the mux when the partner's + * alternate mode was registered successfully. + */ + if (connected && dp && typec->partner_altmode) { + dp_data->status = DP_STATUS_ENABLED; + if (typec->data_status[2] & HPD_STATE) + dp_data->status |= DP_STATUS_HPD_STATE; + if (typec->data_status[2] & HPD_IRQ) + dp_data->status |= DP_STATUS_IRQ_HPD; + + typec->state.data = dp_data; + typec->state.alt = typec->port_altmode; + typec->state.mode = usb3 ? TYPEC_DP_STATE_D : + TYPEC_DP_STATE_E; + } else if (connected && (usb2 || usb3)) { + typec->state.mode = TYPEC_STATE_USB; + } else { + typec->state.mode = TYPEC_STATE_SAFE; + } + + ret = typec_mux_set(typec->mux, &typec->state); + if (ret) + return ret; + + if (typec->mode == typec->state.mode && + typec->hpd_status == dp_data->status) + return 0; + + if (typec->state.mode == TYPEC_DP_STATE_D) + dev_info(typec->dev, + "typec port(%d): alt mode(%s) orientation(%s), usb_data_role(%s), power role(%s), hpd_state(%d)\n", + typec->id, dp_usb_mode, + orientation == TYPEC_ORIENTATION_REVERSE ? + orientation_reverse : orientation_normal, + role == USB_ROLE_DEVICE ? data_device : data_host, + cix_pd_pwr_role_str(typec), + (int)(dp_data->status & DP_STATUS_HPD_STATE)); + else if (typec->state.mode == TYPEC_DP_STATE_E) + dev_info(typec->dev, + "typec port(%d): alt mode(%s) orientation(%s), power role(%s), hpd_state(%d)\n", + typec->id, dp_only_mode, + orientation == TYPEC_ORIENTATION_REVERSE ? + orientation_reverse : orientation_normal, + cix_pd_pwr_role_str(typec), + (int)(dp_data->status & DP_STATUS_HPD_STATE)); + else if (typec->state.mode == TYPEC_STATE_USB) + dev_info(typec->dev, + "typec port(%d): alt mode(%s) orientation(%s), power role(%s), usb_data_role(%s)\n", + typec->id, usb_mode, + orientation == TYPEC_ORIENTATION_REVERSE ? + orientation_reverse : orientation_normal, + cix_pd_pwr_role_str(typec), + role == USB_ROLE_DEVICE ? data_device : data_host); + else if (typec->state.mode == TYPEC_STATE_SAFE) + dev_info(typec->dev, "typec port(%d): disconnect state\n", + typec->id); + + typec->mode = typec->state.mode; + typec->hpd_status = dp_data->status; + + return 0; +} + +static void cix_pd_typec_init_state(struct cix_pd *typec) +{ + typec->state.alt = NULL; + typec->state.mode = TYPEC_STATE_SAFE; + typec->state.data = NULL; + typec->partner = NULL; + typec->desc.accessory = TYPEC_ACCESSORY_NONE; + /* + * The data status registers only report the source/sink state, not + * whether USB PD is being used, so the partner is not advertised as + * PD capable. + */ + typec->desc.usb_pd = 0; +} + +/* + * Returns 1 when the reported port state changed, 0 when it did not, and a + * negative error code when the controller could not be accessed. + */ +static int cix_pd_event_handler(struct cix_pd *rts, u8 clear_irq, bool force) +{ + int ret, err = 0; + bool changed; + u8 data_control[3] = {0}; + + mutex_lock(&rts->lock); + if (clear_irq) { + data_control[0] = 2; + data_control[1] = I2C_INT_ACK_BIT; + data_control[2] = 0; + + ret = cix_pd_block_write(rts, RTS_DATA_CONTROL, data_control, 3); + if (ret) + goto i2c_err; + } + + ret = cix_pd_block_read(rts, RTS_DATA_STATUS, rts->data_status, 6); + if (ret) + goto i2c_err; + + dev_dbg(rts->dev, + "typec port(%d): data status = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", + rts->id, rts->data_status[0], rts->data_status[1], + rts->data_status[2], rts->data_status[3], rts->data_status[4], + rts->data_status[5]); + + /* + * Only bytes 1..3 are decoded by this driver. Byte 0 and bytes 4..5 + * have no known meaning, so they are excluded from the comparison: a + * change in an undecoded bit must not be reported as a port event. + */ + changed = !rts->status_valid || + memcmp(&rts->data_status[1], &rts->prev_status[1], + DATA_STATUS_COUNT - 3); + memcpy(rts->prev_status, rts->data_status, DATA_STATUS_COUNT); + rts->status_valid = true; + + err = cix_pd_typec_port_update(rts, force); + if (err) + dev_err(rts->dev, "typec port(%d): failed to update port: %d\n", + rts->id, err); + + if (rts->data_status[2] & HPD_IRQ) { + data_control[0] = 2; + data_control[1] = 0; + data_control[2] = HPD_IRQ_ACK; + ret = cix_pd_block_write(rts, RTS_DATA_CONTROL, data_control, 3); + if (ret) + goto i2c_err; + } + + mutex_unlock(&rts->lock); + return changed ? 1 : 0; + +i2c_err: + /* + * Report the controller as unresponsive so the IRQ handler can tell + * the interrupt core that this instance did not handle the event. + */ + dev_err(rts->dev, "typec port(%d): i2c transfer error = %d\n", + rts->id, ret); + mutex_unlock(&rts->lock); + return -ESHUTDOWN; +} + +static int cix_pd_typec_parse_port_props(struct typec_capability *cap, + struct fwnode_handle *fwnode, + struct device *dev) +{ + int ret; + + memset(cap, 0, sizeof(*cap)); + ret = typec_get_fw_cap(cap, fwnode); + if (ret) { + /* + * typec_get_fw_cap() stores a borrowed reference without + * taking it, so it must not be dropped by the caller. + */ + cap->fwnode = NULL; + return dev_err_probe(dev, ret, + "failed to parse connector properties\n"); + } + + cap->fwnode = fwnode_handle_get(fwnode); + cap->orientation_aware = true; + /* Alternate modes are entered and exited by the controller firmware. */ + cap->no_mode_control = true; + + return 0; +} + +static int cix_pd_typec_get_handles(struct cix_pd *typec, + struct fwnode_handle *fwnode, + struct device *dev) +{ + int ret; + + typec->mux = fwnode_typec_mux_get(fwnode); + if (IS_ERR(typec->mux)) + return dev_err_probe(dev, PTR_ERR(typec->mux), + "failed to get mux\n"); + + typec->role_sw = fwnode_usb_role_switch_get(fwnode); + if (IS_ERR(typec->role_sw)) { + ret = PTR_ERR(typec->role_sw); + typec->role_sw = NULL; + typec_mux_put(typec->mux); + typec->mux = NULL; + return dev_err_probe(dev, ret, + "failed to get USB role switch\n"); + } + + if (!typec->mux) + dev_dbg(dev, "mux not found\n"); + + return 0; +} + +static void cix_pd_unregister_ports(struct cix_pd *typec) +{ + if (!IS_ERR_OR_NULL(typec->port)) { + typec->state.alt = NULL; + typec->state.mode = TYPEC_STATE_SAFE; + typec->state.data = NULL; + usb_role_switch_set_role(typec->role_sw, USB_ROLE_NONE); + typec_set_orientation(typec->port, TYPEC_ORIENTATION_NONE); + typec_mux_set(typec->mux, &typec->state); + + if (typec->partner_altmode) + typec_altmode_update_active(typec->partner_altmode, + false); + typec_unregister_altmode(typec->partner_altmode); + typec->partner_altmode = NULL; + typec_unregister_partner(typec->partner); + typec->partner = NULL; + typec_unregister_altmode(typec->port_altmode); + typec->port_altmode = NULL; + + typec_unregister_port(typec->port); + typec->port = NULL; + } + + usb_role_switch_put(typec->role_sw); + typec->role_sw = NULL; + typec_mux_put(typec->mux); + typec->mux = NULL; + fwnode_handle_put(typec->caps.fwnode); + typec->caps.fwnode = NULL; +} + +static irqreturn_t cix_pd_irq_handle(int irq, void *data) +{ + struct cix_pd *rts = data; + + /* + * The Type-C ports of this platform are wired to one shared, + * level-triggered interrupt line and the controller has no + * per-instance pending bit, so the interrupt is attributed by + * comparing the port status with the previously reported one. The + * controller interrupt is acknowledged in any case, but an unchanged + * status is reported as IRQ_NONE: if a sibling keeps the shared line + * asserted without ever clearing its interrupt, the spurious + * interrupt detector can then eventually disable the line instead of + * leaving the system in an interrupt storm. + */ + if (cix_pd_event_handler(rts, 1, false) <= 0) + return IRQ_NONE; + + return IRQ_HANDLED; +} + +static int cix_pd_init_ports(struct cix_pd *typec) +{ + struct device *dev = typec->dev; + struct typec_altmode_desc desc = cix_pd_dp_port_altmode; + struct fwnode_handle *fwnode; + int ret; + + fwnode = device_get_named_child_node(dev, "connector"); + if (!fwnode) + return dev_err_probe(dev, -ENODEV, "no connector node found\n"); + + ret = cix_pd_typec_get_handles(typec, fwnode, dev); + if (ret) + goto out_put_fwnode; + + ret = cix_pd_typec_parse_port_props(&typec->caps, fwnode, dev); + if (ret) + goto out_unregister_ports; + + typec->port = typec_register_port(dev, &typec->caps); + if (IS_ERR(typec->port)) { + ret = PTR_ERR(typec->port); + typec->port = NULL; + dev_err_probe(dev, ret, "failed to register port\n"); + goto out_unregister_ports; + } + + desc.roles = typec->caps.data; + typec->port_altmode = typec_port_register_altmode(typec->port, &desc); + if (IS_ERR(typec->port_altmode)) { + ret = PTR_ERR(typec->port_altmode); + typec->port_altmode = NULL; + dev_err_probe(dev, ret, "failed to register DP altmode\n"); + goto out_unregister_ports; + } + + fwnode_handle_put(fwnode); + + return 0; + +out_unregister_ports: + cix_pd_unregister_ports(typec); +out_put_fwnode: + fwnode_handle_put(fwnode); + return ret; +} + +static int cix_pd_probe(struct i2c_client *client) +{ + struct cix_pd *rts; + int ret; + + rts = devm_kzalloc(&client->dev, sizeof(*rts), GFP_KERNEL); + if (!rts) + return -ENOMEM; + + rts->dev = &client->dev; + rts->irq = client->irq; + mutex_init(&rts->lock); + mutex_init(&rts->irq_lock); + cix_pd_typec_init_state(rts); + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) + return dev_err_probe(&client->dev, -ENODEV, + "i2c check functionality failed\n"); + + rts->regmap = devm_regmap_init_i2c(client, &cix_pd_regmap_config); + if (IS_ERR(rts->regmap)) + return dev_err_probe(&client->dev, PTR_ERR(rts->regmap), + "failed to init regmap\n"); + + i2c_set_clientdata(client, rts); + rts->id = client->addr; + + ret = cix_pd_init_ports(rts); + if (ret) + return ret; + + /* + * Acknowledge any interrupt the controller raised before the IRQ was + * requested, so the shared line is not left asserted by this instance. + */ + ret = cix_pd_event_handler(rts, 1, false); + if (ret < 0) { + dev_err(&client->dev, "failed to read initial status: %d\n", ret); + if (ret == -ESHUTDOWN) + ret = -EIO; + goto err_unregister_ports; + } + + if (client->irq > 0) { + ret = request_threaded_irq(client->irq, NULL, cix_pd_irq_handle, + IRQF_SHARED | IRQF_ONESHOT, + dev_name(&client->dev), rts); + if (ret) { + dev_err(&client->dev, "failed to request irq: %d\n", ret); + goto err_unregister_ports; + } + } + + device_set_wakeup_capable(&client->dev, true); + + return 0; + +err_unregister_ports: + cix_pd_unregister_ports(rts); + return ret; +} + +static void cix_pd_remove(struct i2c_client *client) +{ + struct cix_pd *rts = i2c_get_clientdata(client); + + if (rts->irq > 0) { + if (rts->wake_enabled) { + disable_irq_wake(rts->irq); + rts->wake_enabled = false; + } + free_irq(rts->irq, rts); + } + + cix_pd_unregister_ports(rts); +} + +static const struct of_device_id cix_pd_of_match[] = { + { .compatible = "realtek,rts5453h", }, + {} +}; +MODULE_DEVICE_TABLE(of, cix_pd_of_match); + +static const struct i2c_device_id cix_pd_id[] = { + { .name = "rts5453h" }, + {} +}; +MODULE_DEVICE_TABLE(i2c, cix_pd_id); + +static int __maybe_unused cix_pd_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct cix_pd *rts = i2c_get_clientdata(client); + int ret; + + if (client->irq <= 0) + return 0; + + mutex_lock(&rts->irq_lock); + if (rts->irq_disabled) { + mutex_unlock(&rts->irq_lock); + return 0; + } + + /* + * The threaded handler talks to the controller over I2C, so it must + * not run once the adapter is suspended. The core re-enables the + * interrupt in resume_device_irqs(), which runs before the adapter is + * resumed, so the IRQ is kept disabled here and only re-enabled in + * ->resume(). + */ + disable_irq(client->irq); + rts->irq_disabled = true; + + if (device_may_wakeup(dev)) { + ret = enable_irq_wake(client->irq); + if (ret) { + dev_err(dev, "failed to enable IRQ wake: %d\n", ret); + enable_irq(client->irq); + rts->irq_disabled = false; + } else { + rts->wake_enabled = true; + } + } + mutex_unlock(&rts->irq_lock); + + return 0; +} + +static int __maybe_unused cix_pd_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct cix_pd *rts = i2c_get_clientdata(client); + int ret; + + if (client->irq > 0) { + mutex_lock(&rts->irq_lock); + if (rts->wake_enabled) { + disable_irq_wake(client->irq); + rts->wake_enabled = false; + } + if (rts->irq_disabled) { + enable_irq(client->irq); + rts->irq_disabled = false; + } + mutex_unlock(&rts->irq_lock); + } + + /* Force the USB role to be reprogrammed after system resume. */ + ret = cix_pd_event_handler(rts, 0, true); + if (ret < 0) { + dev_err(dev, "failed to refresh status: %d\n", ret); + if (ret == -ESHUTDOWN) + ret = -EIO; + return ret; + } + + return 0; +} + +static const struct dev_pm_ops cix_pd_pm_ops = { + .suspend = pm_sleep_ptr(cix_pd_suspend), + .resume = pm_sleep_ptr(cix_pd_resume), + .freeze = pm_sleep_ptr(cix_pd_suspend), + .thaw = pm_sleep_ptr(cix_pd_resume), + .poweroff = pm_sleep_ptr(cix_pd_suspend), + .restore = pm_sleep_ptr(cix_pd_resume), +}; + +static struct i2c_driver cix_pd_i2c_driver = { + .driver = { + .name = "rts5453h", + .of_match_table = cix_pd_of_match, + .pm = pm_ptr(&cix_pd_pm_ops), + }, + .id_table = cix_pd_id, + .probe = cix_pd_probe, + .remove = cix_pd_remove, +}; + +module_i2c_driver(cix_pd_i2c_driver); +MODULE_DESCRIPTION("CIX Type-C PD controller driver"); +MODULE_AUTHOR("Chao Zeng <Chao.Zeng@cixtech.com>"); +MODULE_AUTHOR("Gary Yang <gary.yang@cixtech.com>"); +MODULE_LICENSE("GPL"); ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v4 2/2] usb: typec: add CIX Type-C PD controller driver 2026-11-13 12:53 ` [PATCH v4 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang @ 2026-11-13 14:31 ` sashiko-bot 0 siblings, 0 replies; 10+ messages in thread From: sashiko-bot @ 2026-11-13 14:31 UTC (permalink / raw) To: Gary Yang; +Cc: linux-usb, devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] usb: typec: cix_pd: HPD_IRQ latched in prev_status in cix_pd_event_handler() - [Medium] usb: typec: cix_pd: unacknowledged IRQ on status refresh in cix_pd_resume() - [Medium] usb: typec: cix_pd: unconditional port update in cix_pd_event_handler() - [Medium] usb: typec: cix_pd: unlocked device_is_bound() in cix_pd_typec_port_update() - [Medium] usb: typec: cix_pd: IRQ re-enabled on wake error in cix_pd_suspend() -- commit df6c3014329332ed5d36e38c30e57956677e5273 Author: Gary Yang <gary.yang@cixtech.com> usb: typec: add CIX Type-C PD controller driver This commit adds an I2C driver for the CIX Type-C PD controller (Realtek RTS5453H), reporting orientation, USB data role, power role, and DisplayPort alternate mode state changes to the Type-C framework. > diff --git a/drivers/usb/typec/cix_pd.c b/drivers/usb/typec/cix_pd.c > new file mode 100644 > index 0000000000000..b413a125c7e6a > --- /dev/null > +++ b/drivers/usb/typec/cix_pd.c [ ... ] > +static int cix_pd_typec_port_update(struct cix_pd *typec, bool force) > +{ [ ... ] > + if (!connected) { > + if (typec->partner_altmode) { > + typec_altmode_update_active(typec->partner_altmode, > + false); > + typec_unregister_altmode(typec->partner_altmode); > + typec->partner_altmode = NULL; > + } > + if (typec->partner) { > + dev_info(typec->dev, "typec port(%d): unregister partner\n", > + typec->id); > + typec_unregister_partner(typec->partner); > + typec->partner = NULL; > + } > + } else { > + if (!typec->partner) { > + dev_info(typec->dev, "typec port(%d): register partner\n", > + typec->id); > + typec->partner = typec_register_partner(typec->port, > + &typec->desc); > + if (IS_ERR(typec->partner)) { > + dev_err(typec->dev, > + "typec port(%d): failed to register partner: %ld\n", > + typec->id, PTR_ERR(typec->partner)); > + typec->partner = NULL; > + } > + } > + > + if (typec->partner_altmode && !dp) { > + typec_altmode_update_active(typec->partner_altmode, > + false); > + typec_unregister_altmode(typec->partner_altmode); > + typec->partner_altmode = NULL; > + } > + > + if (typec->partner && dp && !typec->partner_altmode) { > + typec->partner_altmode = > + typec_partner_register_altmode(typec->partner, > + &cix_pd_dp_partner_altmode); > + if (IS_ERR(typec->partner_altmode)) { > + dev_err(typec->dev, > + "typec port(%d): failed to register DP altmode: %ld\n", > + typec->id, > + PTR_ERR(typec->partner_altmode)); > + typec->partner_altmode = NULL; > + } > + } > + > + /* > + * typec_altmode_update_active() takes a reference to the > + * alternate mode driver's module only when a driver is bound > + * to the mode device. Reporting the mode as active without a > + * driver would leave that reference unbalanced, and the later > + * deactivation would drop a reference that was never taken. > + * Report the mode only once a driver is bound, and retry on > + * the next status update so that a driver loaded after the > + * cable was attached still learns about the active mode. > + */ > + if (typec->partner_altmode && > + device_is_bound(&typec->partner_altmode->dev)) > + typec_altmode_update_active(typec->partner_altmode, true); > + } [Severity: Medium] Does calling device_is_bound(&typec->partner_altmode->dev) and typec_altmode_update_active() without holding device_lock(&typec->partner_altmode->dev) race with driver unbind? device_is_bound() requires the caller to hold device_lock(dev). During driver unbind, __device_release_driver() runs typec_remove() -> typec_altmode_update_active(adev, false) and device_unbind_cleanup() (which sets adev->dev.driver = NULL) before detaching knode_driver: CPU0 CPU1 cix_pd_typec_port_update() device_release_driver_internal() __device_release_driver(dev) typec_remove(dev) typec_altmode_update_active(..., false) device_is_bound(dev) == true typec_altmode_update_active(..., true) if (adev->dev.driver) device_unbind_cleanup(dev) dev->driver = NULL try_module_get(adev->dev.driver->owner) <-- NULL pointer dereference Depending on the exact timing against __device_release_driver(), this can also leak a module reference, set adev->active = true without taking a module reference, or double-put the module reference when typec_altmode_update_active(typec->partner_altmode, false) runs on disconnect or in cix_pd_unregister_ports(). In addition, when the DisplayPort alternate mode driver is built as a module and loaded asynchronously by udev after typec_partner_register_altmode(), device_is_bound() is false during the initial status update; if the hardware raises no further interrupt after the module binds, partner_altmode remains inactive in sysfs. [ ... ] > +static int cix_pd_event_handler(struct cix_pd *rts, u8 clear_irq, bool force) > +{ > + int ret, err = 0; > + bool changed; > + u8 data_control[3] = {0}; > + > + mutex_lock(&rts->lock); > + if (clear_irq) { > + data_control[0] = 2; > + data_control[1] = I2C_INT_ACK_BIT; > + data_control[2] = 0; > + > + ret = cix_pd_block_write(rts, RTS_DATA_CONTROL, data_control, 3); > + if (ret) > + goto i2c_err; > + } > + > + ret = cix_pd_block_read(rts, RTS_DATA_STATUS, rts->data_status, 6); > + if (ret) > + goto i2c_err; > + > + dev_dbg(rts->dev, > + "typec port(%d): data status = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", > + rts->id, rts->data_status[0], rts->data_status[1], > + rts->data_status[2], rts->data_status[3], rts->data_status[4], > + rts->data_status[5]); > + > + /* > + * Only bytes 1..3 are decoded by this driver. Byte 0 and bytes 4..5 > + * have no known meaning, so they are excluded from the comparison: a > + * change in an undecoded bit must not be reported as a port event. > + */ > + changed = !rts->status_valid || > + memcmp(&rts->data_status[1], &rts->prev_status[1], > + DATA_STATUS_COUNT - 3); > + memcpy(rts->prev_status, rts->data_status, DATA_STATUS_COUNT); > + rts->status_valid = true; > + > + err = cix_pd_typec_port_update(rts, force); [Severity: Medium] Should cix_pd_event_handler() skip cix_pd_typec_port_update() when neither changed nor force is true? Because all controller instances share one interrupt line, an interrupt on one port runs cix_pd_event_handler() on every sibling instance. Calling cix_pd_typec_port_update() unconditionally when !changed && !force causes every unchanged sibling port to call typec_set_orientation() and typec_mux_set(), which reprogram the orientation switch and mode mux and emit spurious KOBJ_CHANGE uevents and sysfs notifications on every unaffected port. > + if (err) > + dev_err(rts->dev, "typec port(%d): failed to update port: %d\n", > + rts->id, err); > + > + if (rts->data_status[2] & HPD_IRQ) { > + data_control[0] = 2; > + data_control[1] = 0; > + data_control[2] = HPD_IRQ_ACK; > + ret = cix_pd_block_write(rts, RTS_DATA_CONTROL, data_control, 3); > + if (ret) > + goto i2c_err; > + } [Severity: High] Does copying rts->data_status into rts->prev_status with HPD_IRQ still set break shared-interrupt attribution on subsequent interrupts? When a DisplayPort sink generates an HPD_IRQ pulse, rts->prev_status[2] saves HPD_IRQ = 1 (and cix_pd_typec_port_update() saves DP_STATUS_IRQ_HPD in typec->hpd_status), and then writing HPD_IRQ_ACK clears HPD_IRQ in the controller hardware. If the sink generates a second HPD_IRQ while the rest of data_status[1..3] is unchanged, rts->data_status[2] again has HPD_IRQ = 1, matching the stale HPD_IRQ = 1 in rts->prev_status[2]; memcmp() reports no change and cix_pd_irq_handle() returns IRQ_NONE for a valid interrupt it just acknowledged, which can cause the spurious interrupt detector to disable the shared IRQ line. Conversely, if a sibling port interrupts next while this port has HPD_IRQ = 0, this port sees a difference against its stale rts->prev_status[2], returns IRQ_HANDLED for the sibling's interrupt, and logs a duplicate mode message because dp_data->status no longer matches typec->hpd_status. [ ... ] > +static int __maybe_unused cix_pd_suspend(struct device *dev) > +{ [ ... ] > + disable_irq(client->irq); > + rts->irq_disabled = true; > + > + if (device_may_wakeup(dev)) { > + ret = enable_irq_wake(client->irq); > + if (ret) { > + dev_err(dev, "failed to enable IRQ wake: %d\n", ret); > + enable_irq(client->irq); > + rts->irq_disabled = false; > + } else { > + rts->wake_enabled = true; > + } > + } > + mutex_unlock(&rts->irq_lock); > + > + return 0; > +} [Severity: Medium] If enable_irq_wake() fails, doesn't re-enabling client->irq while returning 0 allow the interrupt handler to run before the I2C adapter resumes? Returning 0 lets system suspend proceed with the IRQ enabled, so resume_device_irqs() unmasks the line before the I2C adapter is resumed and cix_pd_irq_handle() can attempt I2C transfers on a suspended bus. Also, when multiple controller instances share client->irq, calling disable_irq() in each instance increments the IRQ disable depth to the number of instances, whereas suspend_device_irq() on IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND chips decrements the depth only once, leaving the shared wakeup interrupt masked during sleep. > + > +static int __maybe_unused cix_pd_resume(struct device *dev) > +{ > + struct i2c_client *client = to_i2c_client(dev); > + struct cix_pd *rts = i2c_get_clientdata(client); > + int ret; > + > + if (client->irq > 0) { > + mutex_lock(&rts->irq_lock); > + if (rts->wake_enabled) { > + disable_irq_wake(client->irq); > + rts->wake_enabled = false; > + } > + if (rts->irq_disabled) { > + enable_irq(client->irq); > + rts->irq_disabled = false; > + } > + mutex_unlock(&rts->irq_lock); > + } > + > + /* Force the USB role to be reprogrammed after system resume. */ > + ret = cix_pd_event_handler(rts, 0, true); [Severity: Medium] Does calling cix_pd_event_handler(rts, 0, true) after enable_irq() consume sleep-time status changes without acknowledging the controller interrupt? If a port status change occurred while the system was suspended, the controller keeps the shared level-triggered interrupt line asserted until I2C_INT_ACK_BIT is written. When multiple instances share client->irq, enable_irq() keeps the line masked until the last instance resumes, while each earlier instance's cix_pd_event_handler(rts, 0, true) updates rts->prev_status without clearing the hardware interrupt (and on the last or sole instance, enable_irq() races the IRQ thread against cix_pd_event_handler()). When cix_pd_irq_handle() then runs for the still-asserted line, rts->prev_status already matches rts->data_status, so every instance returns IRQ_NONE. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261113125304.1693911-2-gary.yang@cixtech.com?part=2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v5 0/2] usb: typec: add CIX Type-C PD controller driver 2026-11-13 12:53 [PATCH v4 0/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2026-11-13 12:53 ` [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang 2026-11-13 12:53 ` [PATCH v4 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang @ 2026-11-16 8:15 ` Gary Yang 2026-11-16 8:15 ` [PATCH v5 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang 2026-11-16 8:15 ` [PATCH v5 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2 siblings, 2 replies; 10+ messages in thread From: Gary Yang @ 2026-11-16 8:15 UTC (permalink / raw) To: Heikki Krogerus, Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-kernel, cix-kernel-upstream, gary.yang This series adds a Type-C PD controller driver for CIX platforms. The first supported controller is the Realtek RTS5453H, a stand-alone port controller: the CC and USB Power Delivery state machines run in its firmware and are configured through vendor registers rather than the TCPC register interface, so it cannot be driven by the tcpm framework. The driver translates the controller status registers into Type-C class notifications and drives the platform PHY's orientation switch and mode mux. The driver is named after the CIX platform integration rather than the RTS5453H so that further register-compatible PD controllers can be supported without renaming it. Patch 1 adds the devicetree binding and patch 2 adds the driver and its MAINTAINERS entry. The DTS patches from v1 are sent separately through the SoC tree. Changes in v5: - Exclude HPD_IRQ from the status comparison and handle it as an event of its own. The bit stays set until the interrupt is acknowledged, so caching it in the previous status made a repeated HPD interrupt look unchanged, and an acknowledged event look like a status change on the next interrupt of a sibling port. - Reprogram the port only when the decoded status changed or an HPD interrupt arrived, and skip the orientation write when the orientation did not change, so a shared interrupt aimed at a sibling port no longer re-reports unchanged state to the Type-C class. Invalidate the cached status when a port update fails, so the update is retried on the next interrupt instead of being lost with the acknowledged event. - Take the alternate mode device lock around device_is_bound() and typec_altmode_update_active(), so activation cannot race driver unbind. - Keep the shared interrupt line masked for the whole system sleep: ->suspend() disables the line and waits for an in-flight handler, and ->resume() reads the status back and acknowledges the controller before re-enabling it. An event during suspend can no longer loop on the level-triggered line. Wake-up through this line is not offered: the GPIO controller behind it has no irq_set_wake() callback. Changes in v4: - Keep the interrupt disabled across system sleep and re-enable it in ->resume() only, after the parent I2C adapter has resumed. - Return IRQ_NONE when the port status did not change, so that the interrupt core can attribute the shared level-triggered line and disable a line that stays asserted. - Mark the DisplayPort alternate mode active only after a driver is bound, so that the module reference taken by typec_altmode_update_active() stays balanced. - binding: drop the nodes below the documented device from the example. - Carry Reviewed-by from Krzysztof Kozlowski. Changes in v3: - Report the Type-C data role from the controller status for every connection, including DisplayPort-only configurations where no USB data role is available, instead of deriving it from the USB role switch. - Pass the port alternate mode in the mux state, matching other firmware-driven port drivers. - Force the USB role switch to be reprogrammed on system resume and check the enable_irq_wake() return value. - Acknowledge a pending controller interrupt during probe so that a shared interrupt line is not left asserted. - Do not drop the borrowed fwnode reference when parsing the connector properties fails. - Treat all fwnode_usb_role_switch_get() errors as fatal; a connector without a role switch link returns NULL, not -ENODEV. - Drop a redundant include. Changes in v2: - Drop the DTS patches from the series. - binding: describe the single connector as a plain "connector" child node instead of an addressed node, and stop requiring "try-power-role", which only applies to dual-role ports. - driver: add the missing USB_ROLE_SWITCH Kconfig dependency. - driver: register the DisplayPort partner alternate mode only for actual DP connections, with a valid VDO, call typec_altmode_update_active() on attach/detach and set no_mode_control since the firmware drives mode entry. - driver: never pass a NULL alternate mode to typec_mux_set(), and treat debug accessories as disconnected everywhere. - driver: handle USB 2.0-only connections in orientation, role and mux state. - driver: only cache the USB role after the role switch accepted it, and force a role refresh on resume. - driver: use typec_get_fw_cap() instead of open-coding the connector properties. - driver: acquire the mux and role switch before registering the port, use dev_err_probe() on probe failures and reset the mux to the safe state on teardown. - driver: remove the undocumented disable_pd_interrupt sysfs attribute and the manual disable_irq()/IRQ_DISABLE_UNLAZY handling that could leak the shared IRQ's disable depth. This also fixes the build error reported by the kernel test robot on sparc64 and s390. - driver: use a named i2c_device_id initializer and add the missing hibernation PM callbacks. Tested on a Radxa Orion O6 (Sky1): attach/detach, USB-only and DP-only alt modes, cable orientation detection and HPD reporting all work. Gary Yang (2): dt-bindings: usb: add Realtek RTS5453H Type-C port controller usb: typec: add CIX Type-C PD controller driver .../bindings/usb/realtek,rts5453h.yaml | 70 ++ MAINTAINERS | 7 + drivers/usb/typec/Kconfig | 13 + drivers/usb/typec/Makefile | 1 + drivers/usb/typec/cix_pd.c | 778 ++++++++++++++++++ 5 files changed, 869 insertions(+) create mode 100644 Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml create mode 100644 drivers/usb/typec/cix_pd.c base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 -- 2.50.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v5 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller 2026-11-16 8:15 ` [PATCH v5 0/2] " Gary Yang @ 2026-11-16 8:15 ` Gary Yang 2026-11-16 8:21 ` sashiko-bot 2026-11-16 8:15 ` [PATCH v5 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 1 sibling, 1 reply; 10+ messages in thread From: Gary Yang @ 2026-11-16 8:15 UTC (permalink / raw) To: Heikki Krogerus, Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-kernel, cix-kernel-upstream, gary.yang, Krzysztof Kozlowski Document the Realtek RTS5453H stand-alone USB Type-C port controller. The controller is connected over I2C and manages a single USB Type-C connector, described with the generic USB connector binding. The connector's OF graph links to the Type-C orientation switch and mode mux provided by the platform PHY. Signed-off-by: Gary Yang <gary.yang@cixtech.com> Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com> --- .../bindings/usb/realtek,rts5453h.yaml | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml diff --git a/Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml b/Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml new file mode 100644 index 000000000000..f7c062ba1114 --- /dev/null +++ b/Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/usb/realtek,rts5453h.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Realtek RTS5453H USB Type-C port controller + +description: | + The RTS5453H is a stand-alone USB Type-C port controller with USB + Power Delivery support. It is controlled over I2C and reports + changes of the port state through an interrupt line. + + Orientation switching and USB/DisplayPort lane muxing are performed by + the platform PHY. The controller finds the Type-C switch and mux + through the OF graph of its connector node. + +maintainers: + - Gary Yang <gary.yang@cixtech.com> + +properties: + compatible: + const: realtek,rts5453h + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + connector: + type: object + $ref: /schemas/connector/usb-connector.yaml# + unevaluatedProperties: false + description: + The USB Type-C connector managed by this controller. + + required: + - data-role + - power-role + +required: + - compatible + - reg + - interrupts + - connector + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + typec@30 { + compatible = "realtek,rts5453h"; + reg = <0x30>; + interrupt-parent = <&gpio0>; + interrupts = <8 IRQ_TYPE_LEVEL_LOW>; + + connector { + compatible = "usb-c-connector"; + data-role = "host"; + power-role = "source"; + }; + }; + }; -- 2.50.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v5 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller 2026-11-16 8:15 ` [PATCH v5 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang @ 2026-11-16 8:21 ` sashiko-bot 0 siblings, 0 replies; 10+ messages in thread From: sashiko-bot @ 2026-11-16 8:21 UTC (permalink / raw) To: Gary Yang; +Cc: linux-usb > Document the Realtek RTS5453H stand-alone USB Type-C port controller. > The controller is connected over I2C and manages a single USB Type-C > connector, described with the generic USB connector binding. > > The connector's OF graph links to the Type-C orientation switch and > mode mux provided by the platform PHY. > > Signed-off-by: Gary Yang <gary.yang@cixtech.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261116081555.2670878-1-gary.yang@cixtech.com?part=1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v5 2/2] usb: typec: add CIX Type-C PD controller driver 2026-11-16 8:15 ` [PATCH v5 0/2] " Gary Yang 2026-11-16 8:15 ` [PATCH v5 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang @ 2026-11-16 8:15 ` Gary Yang 2026-11-16 8:29 ` sashiko-bot 1 sibling, 1 reply; 10+ messages in thread From: Gary Yang @ 2026-11-16 8:15 UTC (permalink / raw) To: Heikki Krogerus, Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-kernel, cix-kernel-upstream, gary.yang Add a driver for the CIX Type-C PD controller, which is connected over I2C and reports power, data and plug orientation changes through a level-triggered interrupt. The Realtek RTS5453H is the first supported device. The controller runs the Type-C and PD state machines in firmware and exposes the current port status over I2C. The driver translates that into Type-C class notifications, driving the orientation switch and mode mux registered by the platform PHY for the DisplayPort alternate mode. It also drives the platform USB role switch so that the USB controller follows the negotiated data role. The platform wires several controller instances to one shared level-triggered interrupt line, and the controller has no per-instance pending bit. Each instance acknowledges its own interrupt and reports IRQ_NONE when nothing changed since the last report, so the interrupt core can attribute the interrupt and can disable a line that a controller keeps asserted without ever clearing it. Reporting IRQ_HANDLED unconditionally would turn such a line into an uninterruptible interrupt storm. The comparison covers only the decoded status bits and excludes the HPD interrupt bit, which is an event that stays set until the interrupt is acknowledged. The port is only reprogrammed when the decoded status changed or an HPD interrupt arrived, so an interrupt that was meant for a sibling port does not re-report an unchanged orientation and power role to the Type-C class; the state is programmed unconditionally once during probe and after system resume. The cached status is invalidated when a port update fails, so a transient switch or mux error is retried on the next interrupt instead of being lost with the acknowledged event. The interrupt handler accesses the controller over I2C, which is not possible once the parent I2C adapter is suspended, and leaving the shared level-triggered line enabled would loop in the handler while the controller cannot be acknowledged. ->suspend() therefore masks the line for the whole sleep and waits for an in-flight handler; the parent adapter is still alive at that point because children are suspended before their parent. ->resume() reads the status the controller kept during the sleep, acknowledges it and re-enables the line. The GPIO controller that provides the line has no irq_set_wake() callback, so wake-up through it is not supported. The DisplayPort alternate mode is entered by the controller firmware, so the driver reports it as active only once an alternate mode driver is bound to the mode device: typec_altmode_update_active() takes a module reference only while a driver is bound, and reporting the mode earlier would unbalance that reference when the mode is deactivated. Activation and deactivation are both done under the mode device lock, which also serializes them with driver unbinding and typec_remove(). The driver is named after the CIX platform integration rather than the RTS5453H so that further register-compatible PD controllers can be supported without renaming it. Signed-off-by: Gary Yang <gary.yang@cixtech.com> --- MAINTAINERS | 7 + drivers/usb/typec/Kconfig | 13 + drivers/usb/typec/Makefile | 1 + drivers/usb/typec/cix_pd.c | 778 +++++++++++++++++++++++++++++++++++++ 4 files changed, 799 insertions(+) create mode 100644 drivers/usb/typec/cix_pd.c diff --git a/MAINTAINERS b/MAINTAINERS index 93abbb315065..bd99313bf91d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6522,6 +6522,13 @@ M: Satish Kharat <satishkh@cisco.com> S: Supported F: drivers/infiniband/hw/usnic/ +CIX USB TYPE-C PD CONTROLLER +M: Gary Yang <gary.yang@cixtech.com> +L: linux-usb@vger.kernel.org +S: Maintained +F: Documentation/devicetree/bindings/usb/realtek,rts5453h.yaml +F: drivers/usb/typec/cix_pd.c + CLANG CONTEXT ANALYSIS M: Marco Elver <elver@google.com> R: Bart Van Assche <bvanassche@acm.org> diff --git a/drivers/usb/typec/Kconfig b/drivers/usb/typec/Kconfig index 2f80c2792dbd..4d62f5deeeba 100644 --- a/drivers/usb/typec/Kconfig +++ b/drivers/usb/typec/Kconfig @@ -110,6 +110,19 @@ config TYPEC_WUSB3801 If you choose to build this driver as a dynamically linked module, the module will be called wusb3801.ko. +config TYPEC_CIX_PD + tristate "CIX Type-C PD controller driver" + depends on I2C + depends on USB_ROLE_SWITCH + depends on ARCH_CIX || COMPILE_TEST + select REGMAP_I2C + help + Say Y or M here if your system has a CIX Type-C PD controller. + This driver currently supports the Realtek RTS5453H controller. + + If you choose to build this driver as a dynamically linked module, the + module will be called cix_pd.ko. + source "drivers/usb/typec/mux/Kconfig" source "drivers/usb/typec/altmodes/Kconfig" diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile index 8a6a1c663eb6..bb3f3139fd7e 100644 --- a/drivers/usb/typec/Makefile +++ b/drivers/usb/typec/Makefile @@ -11,4 +11,5 @@ obj-$(CONFIG_TYPEC_HD3SS3220) += hd3ss3220.o obj-$(CONFIG_TYPEC_STUSB160X) += stusb160x.o obj-$(CONFIG_TYPEC_RT1719) += rt1719.o obj-$(CONFIG_TYPEC_WUSB3801) += wusb3801.o +obj-$(CONFIG_TYPEC_CIX_PD) += cix_pd.o obj-$(CONFIG_TYPEC) += mux/ diff --git a/drivers/usb/typec/cix_pd.c b/drivers/usb/typec/cix_pd.c new file mode 100644 index 000000000000..7891becaa7bb --- /dev/null +++ b/drivers/usb/typec/cix_pd.c @@ -0,0 +1,778 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2025 Cix Technology Group Co., Ltd. + * + * Driver for the CIX Type-C PD controller. + */ + +#include <linux/bits.h> +#include <linux/device.h> +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/regmap.h> +#include <linux/string.h> +#include <linux/usb/role.h> +#include <linux/usb/typec.h> +#include <linux/usb/typec_altmode.h> +#include <linux/usb/typec_dp.h> +#include <linux/usb/typec_mux.h> + +#define RTS_DATA_CONTROL 0x50 +#define I2C_INT_ACK_BIT BIT(2) +#define HPD_IRQ_ACK BIT(5) +#define RTS_DATA_STATUS 0x5F +/* byte 1 */ +#define CONNECTION_ORIENTATION BIT(1) +#define USB2_CONNECTION BIT(4) +#define USB3_2_CONNECTION BIT(5) +#define USB_DATA_ROLE BIT(7) +/* byte 2 */ +#define DP_CONNECTION BIT(0) +#define DP_SOURCE_SINK BIT(1) +#define DEBUG_ACCESSORY_ATTACHED BIT(4) +#define HPD_IRQ BIT(6) +#define HPD_STATE BIT(7) +/* byte 3 */ +#define POWER_SOURCE BIT(0) +#define POWER_SINK BIT(1) +#define POWER_MASK 3 + +#define DATA_STATUS_COUNT 6 + +struct cix_pd { + struct device *dev; + struct regmap *regmap; + struct mutex lock; /* device lock */ + int id; + u8 data_status[DATA_STATUS_COUNT]; + u8 prev_status[DATA_STATUS_COUNT]; + bool status_valid; + struct typec_displayport_data dp_data; + struct typec_mux_state state; + struct typec_capability caps; + struct typec_mux *mux; + struct usb_role_switch *role_sw; + struct typec_altmode *port_altmode; + struct typec_altmode *partner_altmode; + struct typec_port *port; + enum usb_role role; + enum typec_orientation orientation; + unsigned long mode; + u32 hpd_status; + struct typec_partner *partner; + struct typec_partner_desc desc; +}; + +static int cix_pd_block_read(struct cix_pd *rts, u8 reg, void *val, size_t len) +{ + return regmap_bulk_read(rts->regmap, reg, val, len); +} + +static int cix_pd_block_write(struct cix_pd *rts, u8 reg, + const void *val, size_t len) +{ + return regmap_bulk_write(rts->regmap, reg, val, len); +} + +static const struct regmap_config cix_pd_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0x7F, +}; + +static const char *const dp_only_mode = "Alt Mode:DP"; +static const char *const dp_usb_mode = "Alt Mode:DP + USB"; +static const char *const usb_mode = "Alt Mode:USB"; + +static const char *const power_source = "power source"; +static const char *const power_sink = "power sink"; + +static const char *const orientation_normal = "orientation normal"; +static const char *const orientation_reverse = "orientation reverse"; + +static const char *const data_host = "usb host"; +static const char *const data_device = "usb device"; + +static const char *cix_pd_pwr_role_str(const struct cix_pd *rts) +{ + switch (rts->data_status[3] & POWER_MASK) { + case POWER_SOURCE: + return power_source; + case POWER_SINK: + return power_sink; + default: + return "unknown"; + } +} + +/* + * The controller negotiates the DisplayPort alternate mode autonomously, + * so the capabilities below describe the fixed set supported by the + * platform DP output: a receptacle exposing DFP_D with pin assignments + * C, D and E. + */ +#define CIX_PD_DP_PIN_ASSIGN (BIT(DP_PIN_ASSIGN_C) | BIT(DP_PIN_ASSIGN_D) | \ + BIT(DP_PIN_ASSIGN_E)) + +static const struct typec_altmode_desc cix_pd_dp_port_altmode = { + .svid = USB_TYPEC_DP_SID, + .mode = USB_TYPEC_DP_MODE, + .vdo = DP_CAP_DFP_D | DP_CAP_RECEPTACLE | + (CIX_PD_DP_PIN_ASSIGN << 8), +}; + +static const struct typec_altmode_desc cix_pd_dp_partner_altmode = { + .svid = USB_TYPEC_DP_SID, + .mode = USB_TYPEC_DP_MODE, + .vdo = DP_CAP_UFP_D | DP_CAP_RECEPTACLE | + (CIX_PD_DP_PIN_ASSIGN << 16), + .mode_selection = true, +}; + +/* + * typec_altmode_update_active() takes a reference to the alternate mode + * driver's module when the mode is activated and drops it when the mode is + * deactivated, but only while a driver is bound. Report the mode as active + * only after a driver has bound: activating it earlier would make the later + * deactivation drop a reference that was never taken. The port state, which + * is what drives the DisplayPort output on this platform, is programmed + * through the mux regardless. + * + * The mode device lock keeps the check consistent with driver binding and + * with typec_remove(), which reports the mode inactive when a driver + * unbinds. + */ +static void cix_pd_altmode_set_active(struct cix_pd *typec, bool active) +{ + struct typec_altmode *alt = typec->partner_altmode; + struct device *alt_dev; + + if (!alt) + return; + + alt_dev = &alt->dev; + device_lock(alt_dev); + if (!active || device_is_bound(alt_dev)) + typec_altmode_update_active(alt, active); + device_unlock(alt_dev); +} + +static int cix_pd_typec_port_update(struct cix_pd *typec, bool force) +{ + struct typec_displayport_data *dp_data = &typec->dp_data; + enum typec_orientation orientation; + enum usb_role role; + bool usb2, usb3, dp, connected, dev_role; + int ret, err; + + usb2 = typec->data_status[1] & USB2_CONNECTION; + usb3 = typec->data_status[1] & USB3_2_CONNECTION; + dp = typec->data_status[2] & DP_CONNECTION; + + /* + * Debug accessories are not supported, so they are reported as + * disconnected instead of being passed on to the framework. + */ + if (typec->data_status[2] & DEBUG_ACCESSORY_ATTACHED) { + dev_err(typec->dev, + "debug accessory mode not supported, try another usb cable\n"); + connected = false; + } else { + connected = usb2 || usb3 || dp; + } + + if (!connected) + orientation = TYPEC_ORIENTATION_NONE; + else if (typec->data_status[1] & CONNECTION_ORIENTATION) + orientation = TYPEC_ORIENTATION_REVERSE; + else + orientation = TYPEC_ORIENTATION_NORMAL; + + /* + * The controller keeps reporting the same orientation when a sibling + * port raises the shared interrupt, and the Type-C class emits an + * event for every call, so the switch is only programmed again when + * the orientation changed or after system resume. + */ + if (force || typec->orientation != orientation) { + ret = typec_set_orientation(typec->port, orientation); + if (ret) + return ret; + typec->orientation = orientation; + } + + /* + * The controller reports the data role whenever a cable is attached, + * including DisplayPort-only configurations where no USB data role is + * available. Keep the port's data role in sync independently of the + * USB role switch. + */ + dev_role = typec->data_status[1] & USB_DATA_ROLE; + if (connected) + typec_set_data_role(typec->port, + dev_role ? TYPEC_DEVICE : TYPEC_HOST); + + if (!connected || !(usb2 || usb3)) + role = USB_ROLE_NONE; + else if (dev_role) + role = USB_ROLE_DEVICE; + else + role = USB_ROLE_HOST; + + if (force || typec->role != role) { + err = 0; + if (typec->role_sw) { + err = usb_role_switch_set_role(typec->role_sw, role); + if (err) + dev_err(typec->dev, + "typec port(%d): failed to set USB role: %d\n", + typec->id, err); + } + + /* Only cache the role once the switch accepted it. */ + if (!err) + typec->role = role; + } + + if ((typec->data_status[3] & POWER_MASK) == POWER_SOURCE) { + typec_set_pwr_role(typec->port, TYPEC_SOURCE); + } else if ((typec->data_status[3] & POWER_MASK) == POWER_SINK) { + typec_set_pwr_role(typec->port, TYPEC_SINK); + } else { + dev_dbg(typec->dev, "typec port(%d): unknown power state %d\n", + typec->id, typec->data_status[3]); + } + + if (!connected) { + if (typec->partner_altmode) { + cix_pd_altmode_set_active(typec, false); + typec_unregister_altmode(typec->partner_altmode); + typec->partner_altmode = NULL; + } + if (typec->partner) { + dev_info(typec->dev, "typec port(%d): unregister partner\n", + typec->id); + typec_unregister_partner(typec->partner); + typec->partner = NULL; + } + } else { + if (!typec->partner) { + dev_info(typec->dev, "typec port(%d): register partner\n", + typec->id); + typec->partner = typec_register_partner(typec->port, + &typec->desc); + if (IS_ERR(typec->partner)) { + dev_err(typec->dev, + "typec port(%d): failed to register partner: %ld\n", + typec->id, PTR_ERR(typec->partner)); + typec->partner = NULL; + } + } + + if (typec->partner_altmode && !dp) { + cix_pd_altmode_set_active(typec, false); + typec_unregister_altmode(typec->partner_altmode); + typec->partner_altmode = NULL; + } + + if (typec->partner && dp && !typec->partner_altmode) { + typec->partner_altmode = + typec_partner_register_altmode(typec->partner, + &cix_pd_dp_partner_altmode); + if (IS_ERR(typec->partner_altmode)) { + dev_err(typec->dev, + "typec port(%d): failed to register DP altmode: %ld\n", + typec->id, + PTR_ERR(typec->partner_altmode)); + typec->partner_altmode = NULL; + } + } + + if (typec->partner_altmode) + cix_pd_altmode_set_active(typec, true); + } + + dp_data->status = 0; + typec->state.data = NULL; + typec->state.alt = NULL; + + /* + * Only pass a DisplayPort state to the mux when the partner's + * alternate mode was registered successfully. + */ + if (connected && dp && typec->partner_altmode) { + dp_data->status = DP_STATUS_ENABLED; + if (typec->data_status[2] & HPD_STATE) + dp_data->status |= DP_STATUS_HPD_STATE; + if (typec->data_status[2] & HPD_IRQ) + dp_data->status |= DP_STATUS_IRQ_HPD; + + typec->state.data = dp_data; + typec->state.alt = typec->port_altmode; + typec->state.mode = usb3 ? TYPEC_DP_STATE_D : + TYPEC_DP_STATE_E; + } else if (connected && (usb2 || usb3)) { + typec->state.mode = TYPEC_STATE_USB; + } else { + typec->state.mode = TYPEC_STATE_SAFE; + } + + ret = typec_mux_set(typec->mux, &typec->state); + if (ret) + return ret; + + if (typec->mode == typec->state.mode && + typec->hpd_status == dp_data->status) + return 0; + + if (typec->state.mode == TYPEC_DP_STATE_D) + dev_info(typec->dev, + "typec port(%d): alt mode(%s) orientation(%s), usb_data_role(%s), power role(%s), hpd_state(%d)\n", + typec->id, dp_usb_mode, + orientation == TYPEC_ORIENTATION_REVERSE ? + orientation_reverse : orientation_normal, + role == USB_ROLE_DEVICE ? data_device : data_host, + cix_pd_pwr_role_str(typec), + (int)(dp_data->status & DP_STATUS_HPD_STATE)); + else if (typec->state.mode == TYPEC_DP_STATE_E) + dev_info(typec->dev, + "typec port(%d): alt mode(%s) orientation(%s), power role(%s), hpd_state(%d)\n", + typec->id, dp_only_mode, + orientation == TYPEC_ORIENTATION_REVERSE ? + orientation_reverse : orientation_normal, + cix_pd_pwr_role_str(typec), + (int)(dp_data->status & DP_STATUS_HPD_STATE)); + else if (typec->state.mode == TYPEC_STATE_USB) + dev_info(typec->dev, + "typec port(%d): alt mode(%s) orientation(%s), power role(%s), usb_data_role(%s)\n", + typec->id, usb_mode, + orientation == TYPEC_ORIENTATION_REVERSE ? + orientation_reverse : orientation_normal, + cix_pd_pwr_role_str(typec), + role == USB_ROLE_DEVICE ? data_device : data_host); + else if (typec->state.mode == TYPEC_STATE_SAFE) + dev_info(typec->dev, "typec port(%d): disconnect state\n", + typec->id); + + typec->mode = typec->state.mode; + typec->hpd_status = dp_data->status; + + return 0; +} + +static void cix_pd_typec_init_state(struct cix_pd *typec) +{ + typec->state.alt = NULL; + typec->state.mode = TYPEC_STATE_SAFE; + typec->state.data = NULL; + typec->partner = NULL; + typec->desc.accessory = TYPEC_ACCESSORY_NONE; + /* + * The data status registers only report the source/sink state, not + * whether USB PD is being used, so the partner is not advertised as + * PD capable. + */ + typec->desc.usb_pd = 0; +} + +/* + * Returns 1 when the reported port state changed or an HPD interrupt was + * handled, 0 when there was nothing to report, and a negative error code when + * the controller could not be accessed. + */ +static int cix_pd_event_handler(struct cix_pd *rts, bool clear_irq, bool force) +{ + int ret, err = 0; + bool hpd_irq, changed; + u8 data_control[3] = {0}; + + mutex_lock(&rts->lock); + + if (clear_irq) { + data_control[0] = 2; + data_control[1] = I2C_INT_ACK_BIT; + data_control[2] = 0; + + ret = cix_pd_block_write(rts, RTS_DATA_CONTROL, data_control, 3); + if (ret) + goto i2c_err; + } + + ret = cix_pd_block_read(rts, RTS_DATA_STATUS, rts->data_status, 6); + if (ret) + goto i2c_err; + + dev_dbg(rts->dev, + "typec port(%d): data status = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", + rts->id, rts->data_status[0], rts->data_status[1], + rts->data_status[2], rts->data_status[3], rts->data_status[4], + rts->data_status[5]); + + hpd_irq = rts->data_status[2] & HPD_IRQ; + + /* + * HPD_IRQ is an event bit which stays set until it is acknowledged + * below, so it is excluded from the comparison: the acknowledged + * status would otherwise look different on the next read. Byte 0 and + * bytes 4..5 are not decoded, so a change in them is not a port + * event either. + */ + changed = !rts->status_valid || + rts->data_status[1] != rts->prev_status[1] || + (rts->data_status[2] & ~HPD_IRQ) != + (rts->prev_status[2] & ~HPD_IRQ) || + rts->data_status[3] != rts->prev_status[3]; + + memcpy(rts->prev_status, rts->data_status, DATA_STATUS_COUNT); + rts->status_valid = true; + + /* + * Reprogram the port only when one of the decoded bits changed, when + * an HPD interrupt arrived, or when the caller forces a refresh after + * system resume. The ports share one interrupt line, so a sibling's + * interrupt must not re-report an unchanged orientation and power + * role to the Type-C class. + */ + if (changed || hpd_irq || force) { + err = cix_pd_typec_port_update(rts, force); + if (err) { + /* + * Invalidate the cached status so that the update is + * retried on the next interrupt or on system resume + * instead of being lost with the acknowledged event. + */ + rts->status_valid = false; + dev_err(rts->dev, + "typec port(%d): failed to update port: %d\n", + rts->id, err); + } + } + + if (hpd_irq) { + data_control[0] = 2; + data_control[1] = 0; + data_control[2] = HPD_IRQ_ACK; + ret = cix_pd_block_write(rts, RTS_DATA_CONTROL, data_control, 3); + if (ret) + goto i2c_err; + } + + mutex_unlock(&rts->lock); + return (changed || hpd_irq) ? 1 : 0; + +i2c_err: + /* + * Report the controller as unresponsive so the IRQ handler can tell + * the interrupt core that this instance did not handle the event. + */ + dev_err(rts->dev, "typec port(%d): i2c transfer error = %d\n", + rts->id, ret); + mutex_unlock(&rts->lock); + return -ESHUTDOWN; +} + +static int cix_pd_typec_parse_port_props(struct typec_capability *cap, + struct fwnode_handle *fwnode, + struct device *dev) +{ + int ret; + + memset(cap, 0, sizeof(*cap)); + ret = typec_get_fw_cap(cap, fwnode); + if (ret) { + /* + * typec_get_fw_cap() stores a borrowed reference without + * taking it, so it must not be dropped by the caller. + */ + cap->fwnode = NULL; + return dev_err_probe(dev, ret, + "failed to parse connector properties\n"); + } + + cap->fwnode = fwnode_handle_get(fwnode); + cap->orientation_aware = true; + /* Alternate modes are entered and exited by the controller firmware. */ + cap->no_mode_control = true; + + return 0; +} + +static int cix_pd_typec_get_handles(struct cix_pd *typec, + struct fwnode_handle *fwnode, + struct device *dev) +{ + int ret; + + typec->mux = fwnode_typec_mux_get(fwnode); + if (IS_ERR(typec->mux)) + return dev_err_probe(dev, PTR_ERR(typec->mux), + "failed to get mux\n"); + + typec->role_sw = fwnode_usb_role_switch_get(fwnode); + if (IS_ERR(typec->role_sw)) { + ret = PTR_ERR(typec->role_sw); + typec->role_sw = NULL; + typec_mux_put(typec->mux); + typec->mux = NULL; + return dev_err_probe(dev, ret, + "failed to get USB role switch\n"); + } + + if (!typec->mux) + dev_dbg(dev, "mux not found\n"); + + return 0; +} + +static void cix_pd_unregister_ports(struct cix_pd *typec) +{ + if (!IS_ERR_OR_NULL(typec->port)) { + typec->state.alt = NULL; + typec->state.mode = TYPEC_STATE_SAFE; + typec->state.data = NULL; + usb_role_switch_set_role(typec->role_sw, USB_ROLE_NONE); + typec_set_orientation(typec->port, TYPEC_ORIENTATION_NONE); + typec_mux_set(typec->mux, &typec->state); + + if (typec->partner_altmode) + cix_pd_altmode_set_active(typec, false); + typec_unregister_altmode(typec->partner_altmode); + typec->partner_altmode = NULL; + typec_unregister_partner(typec->partner); + typec->partner = NULL; + typec_unregister_altmode(typec->port_altmode); + typec->port_altmode = NULL; + + typec_unregister_port(typec->port); + typec->port = NULL; + } + + usb_role_switch_put(typec->role_sw); + typec->role_sw = NULL; + typec_mux_put(typec->mux); + typec->mux = NULL; + fwnode_handle_put(typec->caps.fwnode); + typec->caps.fwnode = NULL; +} + +static irqreturn_t cix_pd_irq_handle(int irq, void *data) +{ + struct cix_pd *rts = data; + int ret; + + /* + * The Type-C ports of this platform are wired to one shared, + * level-triggered interrupt line and the controller has no + * per-instance pending bit, so the interrupt is attributed by + * comparing the port status with the previously reported one. The + * controller interrupt is acknowledged in any case, but an unchanged + * status is reported as IRQ_NONE: if a sibling keeps the shared line + * asserted without ever clearing its interrupt, the spurious + * interrupt detector can then eventually disable the line instead of + * leaving the system in an interrupt storm. + */ + ret = cix_pd_event_handler(rts, true, false); + return ret > 0 ? IRQ_HANDLED : IRQ_NONE; +} + +static int cix_pd_init_ports(struct cix_pd *typec) +{ + struct device *dev = typec->dev; + struct typec_altmode_desc desc = cix_pd_dp_port_altmode; + struct fwnode_handle *fwnode; + int ret; + + fwnode = device_get_named_child_node(dev, "connector"); + if (!fwnode) + return dev_err_probe(dev, -ENODEV, "no connector node found\n"); + + ret = cix_pd_typec_get_handles(typec, fwnode, dev); + if (ret) + goto out_put_fwnode; + + ret = cix_pd_typec_parse_port_props(&typec->caps, fwnode, dev); + if (ret) + goto out_unregister_ports; + + typec->port = typec_register_port(dev, &typec->caps); + if (IS_ERR(typec->port)) { + ret = PTR_ERR(typec->port); + typec->port = NULL; + dev_err_probe(dev, ret, "failed to register port\n"); + goto out_unregister_ports; + } + + desc.roles = typec->caps.data; + typec->port_altmode = typec_port_register_altmode(typec->port, &desc); + if (IS_ERR(typec->port_altmode)) { + ret = PTR_ERR(typec->port_altmode); + typec->port_altmode = NULL; + dev_err_probe(dev, ret, "failed to register DP altmode\n"); + goto out_unregister_ports; + } + + fwnode_handle_put(fwnode); + + return 0; + +out_unregister_ports: + cix_pd_unregister_ports(typec); +out_put_fwnode: + fwnode_handle_put(fwnode); + return ret; +} + +static int cix_pd_probe(struct i2c_client *client) +{ + struct cix_pd *rts; + int ret; + + rts = devm_kzalloc(&client->dev, sizeof(*rts), GFP_KERNEL); + if (!rts) + return -ENOMEM; + + rts->dev = &client->dev; + mutex_init(&rts->lock); + cix_pd_typec_init_state(rts); + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) + return dev_err_probe(&client->dev, -ENODEV, + "i2c check functionality failed\n"); + + rts->regmap = devm_regmap_init_i2c(client, &cix_pd_regmap_config); + if (IS_ERR(rts->regmap)) + return dev_err_probe(&client->dev, PTR_ERR(rts->regmap), + "failed to init regmap\n"); + + i2c_set_clientdata(client, rts); + rts->id = client->addr; + + ret = cix_pd_init_ports(rts); + if (ret) + return ret; + + /* + * Acknowledge any interrupt the controller raised before the IRQ was + * requested, so the shared line is not left asserted by this instance, + * and program the port state once so that the switch and the mux are + * in a known state even when no cable is attached. + */ + ret = cix_pd_event_handler(rts, true, true); + if (ret < 0) { + dev_err(&client->dev, "failed to read initial status: %d\n", ret); + if (ret == -ESHUTDOWN) + ret = -EIO; + goto err_unregister_ports; + } + + if (client->irq > 0) { + ret = request_threaded_irq(client->irq, NULL, cix_pd_irq_handle, + IRQF_SHARED | IRQF_ONESHOT, + dev_name(&client->dev), rts); + if (ret) { + dev_err(&client->dev, "failed to request irq: %d\n", ret); + goto err_unregister_ports; + } + } + + return 0; + +err_unregister_ports: + cix_pd_unregister_ports(rts); + return ret; +} + +static void cix_pd_remove(struct i2c_client *client) +{ + struct cix_pd *rts = i2c_get_clientdata(client); + + if (client->irq > 0) + free_irq(client->irq, rts); + + cix_pd_unregister_ports(rts); +} + +static const struct of_device_id cix_pd_of_match[] = { + { .compatible = "realtek,rts5453h", }, + {} +}; +MODULE_DEVICE_TABLE(of, cix_pd_of_match); + +static const struct i2c_device_id cix_pd_id[] = { + { .name = "rts5453h" }, + {} +}; +MODULE_DEVICE_TABLE(i2c, cix_pd_id); + +static int __maybe_unused cix_pd_suspend(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + + /* + * The controller is not accessible once the parent I2C adapter is + * suspended, so keep the interrupt masked for the whole sleep. The line + * is level-triggered and shared by the ports: left enabled, a cable + * event would loop in the handler, which could not acknowledge the + * controller before ->resume(). The parent adapter is still alive here + * because children are suspended first, so disable_irq() can safely + * wait for an in-flight handler to finish. + */ + if (client->irq > 0) + disable_irq(client->irq); + + return 0; +} + +static int __maybe_unused cix_pd_resume(struct device *dev) +{ + struct i2c_client *client = to_i2c_client(dev); + struct cix_pd *rts = i2c_get_clientdata(client); + int ret; + + /* + * Read the status the controller kept while the system was asleep and + * acknowledge it. The parent I2C adapter is always resumed before this + * device, and the interrupt is still masked. + */ + ret = cix_pd_event_handler(rts, true, true); + + if (client->irq > 0) + enable_irq(client->irq); + + if (ret < 0) { + dev_err(dev, "failed to refresh status: %d\n", ret); + return ret == -ESHUTDOWN ? -EIO : ret; + } + + return 0; +} + +static const struct dev_pm_ops cix_pd_pm_ops = { + .suspend = pm_sleep_ptr(cix_pd_suspend), + .resume = pm_sleep_ptr(cix_pd_resume), + .freeze = pm_sleep_ptr(cix_pd_suspend), + .thaw = pm_sleep_ptr(cix_pd_resume), + .poweroff = pm_sleep_ptr(cix_pd_suspend), + .restore = pm_sleep_ptr(cix_pd_resume), +}; + +static struct i2c_driver cix_pd_i2c_driver = { + .driver = { + .name = "rts5453h", + .of_match_table = cix_pd_of_match, + .pm = pm_ptr(&cix_pd_pm_ops), + }, + .id_table = cix_pd_id, + .probe = cix_pd_probe, + .remove = cix_pd_remove, +}; + +module_i2c_driver(cix_pd_i2c_driver); +MODULE_DESCRIPTION("CIX Type-C PD controller driver"); +MODULE_AUTHOR("Chao Zeng <Chao.Zeng@cixtech.com>"); +MODULE_AUTHOR("Gary Yang <gary.yang@cixtech.com>"); +MODULE_LICENSE("GPL"); -- 2.50.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v5 2/2] usb: typec: add CIX Type-C PD controller driver 2026-11-16 8:15 ` [PATCH v5 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang @ 2026-11-16 8:29 ` sashiko-bot 0 siblings, 0 replies; 10+ messages in thread From: sashiko-bot @ 2026-11-16 8:29 UTC (permalink / raw) To: Gary Yang; +Cc: linux-usb > Add a driver for the CIX Type-C PD controller, which is connected over > I2C and reports power, data and plug orientation changes through a > level-triggered interrupt. The Realtek RTS5453H is the first supported > device. > > The controller runs the Type-C and PD state machines in firmware and > exposes the current port status over I2C. The driver translates that > into Type-C class notifications, driving the orientation switch and > mode mux registered by the platform PHY for the DisplayPort alternate > mode. It also drives the platform USB role switch so that the USB > controller follows the negotiated data role. > > The platform wires several controller instances to one shared > level-triggered interrupt line, and the controller has no per-instance > pending bit. Each instance acknowledges its own interrupt and reports > [ ... ] > The driver is named after the CIX platform integration rather than the > RTS5453H so that further register-compatible PD controllers can be > supported without renaming it. > > Signed-off-by: Gary Yang <gary.yang@cixtech.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261116081555.2670878-1-gary.yang@cixtech.com?part=2 ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-16 8:29 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-13 12:53 [PATCH v4 0/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2026-11-13 12:53 ` [PATCH v4 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang 2026-11-13 14:15 ` sashiko-bot 2026-11-13 12:53 ` [PATCH v4 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2026-11-13 14:31 ` sashiko-bot 2026-11-16 8:15 ` [PATCH v5 0/2] " Gary Yang 2026-11-16 8:15 ` [PATCH v5 1/2] dt-bindings: usb: add Realtek RTS5453H Type-C port controller Gary Yang 2026-11-16 8:21 ` sashiko-bot 2026-11-16 8:15 ` [PATCH v5 2/2] usb: typec: add CIX Type-C PD controller driver Gary Yang 2026-11-16 8:29 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox