Devicetree
 help / color / mirror / Atom feed
* Re: [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver
  2026-07-09 16:34 ` [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
@ 2026-07-09  8:49   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-07-09  8:49 UTC (permalink / raw)
  To: Amber Kao; +Cc: devicetree, robh, conor+dt

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Race condition during driver removal causing a use-after-free on the `ucsi->connector` array.
- [High] Infinite IRQ storm and CPU lockup due to silently ignored I2C write or memory allocation failures.
--

--- Patch [2]: [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver ---
commit a72e407ddecce41bb76245d04b0009fb6795ba01
Author: Amber Kao <amber.kao@ite.com.tw>

usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver

This commit adds core UCSI support for the ITE IT885x USB Type-C Power
Delivery controller over I2C. It provides port and partner registration,
deferring Alternate Mode support and command translation to future patches.

> diff --git a/drivers/usb/typec/ucsi/ucsi_itepd.c b/drivers/usb/typec/ucsi/ucsi_itepd.c
> new file mode 100644
> index 0000000000000..e364a3bd7df4e
> --- /dev/null
> +++ b/drivers/usb/typec/ucsi/ucsi_itepd.c

[ ... ]

> +static int itepd_write_reg(struct itepd *itepd, u8 reg, const void *data, u32 len)
> +{
> +	struct i2c_client *client = itepd->client;
> +	struct i2c_msg msg[1];
> +	u8 *buf;
> +	int ret;
> +
> +	buf = kzalloc(len + 1, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;

[ ... ]

> +static irqreturn_t itepd_irq_process(struct itepd *itepd)
> +{
> +	u8 msg_in[ITEPD_MSG_IN_MAX_LEN] = {};
> +	__le32 le_cci;
> +	u32 cci = 0;
> +	u8 event;
> +	u8 len;
> +	int ret;

[ ... ]

> +out_clear:
> +	itepd_write_reg(itepd, ITEPD_VENDOR_WC_INT, &event, sizeof(event));
> +
> +	if (!ret && (event & ITEPD_ALERT_UCSI_EVENT))
> +		ucsi_notify_common(itepd->ucsi, cci);
> +
> +	return IRQ_HANDLED;
> +}

[Severity: High]
Could a failure to clear the interrupt result in an infinite IRQ storm?

Looking at itepd_write_reg(), it dynamically allocates memory via kzalloc().
If this allocation fails with -ENOMEM, or if the underlying I2C transfer
fails, the hardware interrupt is not actually cleared on the device.

Because itepd_irq_process() ignores the return value of itepd_write_reg()
and unconditionally returns IRQ_HANDLED, the kernel IRQ subsystem will unmask
the line and immediately trigger the handler again, potentially causing a CPU
lockup under memory pressure or I2C bus errors.

[ ... ]

> +static void itepd_remove(struct i2c_client *client)
> +{
> +	struct itepd *itepd = i2c_get_clientdata(client);
> +
> +	ucsi_unregister(itepd->ucsi);
> +	free_irq(client->irq, itepd);
> +	ucsi_destroy(itepd->ucsi);
> +}

[Severity: High]
Is there a race condition during driver removal that could lead to a
use-after-free?

This teardown sequence calls ucsi_unregister() before free_irq().

When ucsi_unregister() is called, it frees the ucsi->connector array. If a
pending hardware interrupt is processed by the threaded IRQ handler
after ucsi_unregister() completes but before free_irq() is called,
itepd_irq_process() will call ucsi_notify_common().

This sequence would result in ucsi_notify_common() accessing the freed
connector array.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260710-ucsi-itepd-feature-v2-0-41943fd5df38@ite.com.tw?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 0/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver
@ 2026-07-09 16:34 Amber Kao
  2026-07-09 16:34 ` [PATCH v2 1/2] dt-bindings: usb: Add ITE IT885x support Amber Kao
  2026-07-09 16:34 ` [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
  0 siblings, 2 replies; 4+ messages in thread
From: Amber Kao @ 2026-07-09 16:34 UTC (permalink / raw)
  To: Jeson Yang, Yaode Fang, Bling Chiang, Doreen Lin, Eric Su,
	Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heikki Krogerus
  Cc: linux-usb, devicetree, linux-kernel, Amber Kao

This series adds Device Tree bindings and minimal UCSI driver
support for the ITE IT885x USB Type-C Power Delivery controller
over I2C.

Per Heikki's review of v1, this series has been split into smaller,
self-contained patches. This version drops the Alternate Mode
support, the UCSI command-translation hook, and the auxiliary-bus
core/client split entirely. The driver now does nothing more than
register the UCSI ports and partners. Alternate Mode support and
the auxiliary-bus split will follow in later series, once each
feature can be reviewed on its own.

Note: This driver has not been tested on physical hardware. Runtime
verification was performed by building the module out-of-tree
against the currently running kernel's headers and loading it on an
x86_64 virtual machine, using i2c-stub with manual device
instantiation to trigger probe().

Testing performed:
- checkpatch.pl --strict: no errors or warnings
- dt_binding_check / dtbs_check: no errors or warnings
- Sparse (C=2 CF="-D__CHECK_ENDIAN__"): no errors or warnings
- checkstack.pl: no functions exceed 512-byte stack limit on
  x86_64, arm64, arm32, and ppc64le
- Kconfig tristate: tested =m, =y, =n, allmodconfig, allnoconfig
- Cross-compilation: x86_64, arm64, arm32, ppc64le
- CONFIG_SMP and CONFIG_PREEMPT: both on and off
- Strict warning mode (W=1 EXTRA_CFLAGS=-W): no new warnings
- i2c-stub emulation with manual device instantiation: probe()
  correctly rejects devices with no IRQ resource (-ENODEV), no
  crash, module load/unload cycle clean
- Fault injection (CONFIG_FAILSLAB, fail-nth): each reachable kzalloc()/
  devm_kzalloc() in probe() was individually failed and returned -ENOMEM
  cleanly with correct teardown; the runtime IRQ/UCSI paths were not
  reachable via i2c-stub.

Note: big-endian (ppc64) cross-compilation was not tested, as
TYPEC_UCSI currently depends on !CPU_BIG_ENDIAN upstream. Byte-order
correctness was instead verified via sparse.

Signed-off-by: Amber Kao <amber.kao@ite.com.tw>
---
Amber Kao (2):
      dt-bindings: usb: Add ITE IT885x support
      usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver

 .../devicetree/bindings/usb/ite,itepd-it885x.yaml  | 105 +++++++
 MAINTAINERS                                        |  12 +
 drivers/usb/typec/ucsi/Kconfig                     |  10 +
 drivers/usb/typec/ucsi/Makefile                    |   1 +
 drivers/usb/typec/ucsi/ucsi_itepd.c                | 348 +++++++++++++++++++++
 5 files changed, 476 insertions(+)
---
base-commit: 8fde5d1d47f69db6082dfa34500c27f8485389a5
change-id: 20260605-ucsi-itepd-feature-95e6dcee4fc5

Best regards,
--  
Amber Kao <amber.kao@ite.com.tw>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] dt-bindings: usb: Add ITE IT885x support
  2026-07-09 16:34 [PATCH v2 0/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
@ 2026-07-09 16:34 ` Amber Kao
  2026-07-09 16:34 ` [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
  1 sibling, 0 replies; 4+ messages in thread
From: Amber Kao @ 2026-07-09 16:34 UTC (permalink / raw)
  To: Jeson Yang, Yaode Fang, Bling Chiang, Doreen Lin, Eric Su,
	Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heikki Krogerus
  Cc: linux-usb, devicetree, linux-kernel, Amber Kao

Add device tree binding documentation for the ITE IT885x. The ITE
IT885x is an I2C-based USB Type-C Power Delivery (PD) controller.

v1 -> v2:
- Per Conor/Sashiko: dropped the redundant `gpios` property, the
  interrupt line is already described via `interrupts`
- Per Conor: renamed example node from `itepd@40` to `typec@40`

Cc: Yaode Fang <Yaode.Fang@ite.com.tw>
Cc: Jeson Yang <jeson.yang@ite.com.tw>
Cc: Bling Chiang <Bling.Chiang@ite.com.tw>
Cc: Eric Su <Eric.Su@ite.com.tw>
Cc: Doreen Lin <doreen.lin@ite.com.tw>
Signed-off-by: Amber Kao <amber.kao@ite.com.tw>

---
v1: https://lore.kernel.org/all/20260615-ucsi-itepd-feature-v1-0-a826cfd0df6a@ite.com.tw/
---
 .../devicetree/bindings/usb/ite,itepd-it885x.yaml  | 105 +++++++++++++++++++++
 MAINTAINERS                                        |  11 +++
 2 files changed, 116 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/ite,itepd-it885x.yaml b/Documentation/devicetree/bindings/usb/ite,itepd-it885x.yaml
new file mode 100644
index 000000000000..0a292a8e72cd
--- /dev/null
+++ b/Documentation/devicetree/bindings/usb/ite,itepd-it885x.yaml
@@ -0,0 +1,105 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/usb/ite,itepd-it885x.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: ITE IT885x USB Type-C Power Delivery Controller
+
+maintainers:
+  - Jeson Yang <jeson.yang@ite.com.tw>
+
+description:
+  The ITE IT885x is an I2C-based USB Type-C Power Delivery (PD) controller.
+
+properties:
+  compatible:
+    const: ite,itepd-it885x
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  wakeup-source: true
+
+  pinctrl-names:
+    minItems: 1
+
+  pinctrl-0: true
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+patternProperties:
+  "^connector(@[0-9a-f]+)?$":
+    $ref: /schemas/connector/usb-connector.yaml#
+    unevaluatedProperties: false
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        typec@40 {
+            compatible = "ite,itepd-it885x";
+            reg = <0x40>;
+            interrupts-extended = <&tlmm 129 IRQ_TYPE_EDGE_FALLING>;
+            wakeup-source;
+
+            pinctrl-names = "default";
+            pinctrl-0 = <&itepd_int_default>;
+
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            connector@0 {
+                compatible = "usb-c-connector";
+                reg = <0>;
+                label = "USB-C";
+                power-role = "dual";
+                data-role = "dual";
+
+                ports {
+                    #address-cells = <1>;
+                    #size-cells = <0>;
+
+                    port@0 {
+                        reg = <0>;
+                        endpoint {
+                            remote-endpoint = <&eud_con>;
+                        };
+                    };
+
+                    port@1 {
+                        reg = <1>;
+                        endpoint {
+                            remote-endpoint = <&redriver_ss_out>;
+                        };
+                    };
+
+                    port@2 {
+                        reg = <2>;
+                        endpoint {
+                            remote-endpoint = <&fsa4480_sbu_mux>;
+                        };
+                    };
+                };
+            };
+        };
+    };
diff --git a/MAINTAINERS b/MAINTAINERS
index 461a3eed6129..1b03fa3aa060 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13720,6 +13720,17 @@ T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
 F:	Documentation/devicetree/bindings/display/bridge/ite,it66121.yaml
 F:	drivers/gpu/drm/bridge/ite-it66121.c
 
+ITE IT885x TYPE-C PD CONTROLLER DRIVER
+M:	Jeson Yang <jeson.yang@ite.com.tw>
+M:	Amber Kao <amber.kao@ite.com.tw>
+R:	Yaode Fang <Yaode.Fang@ite.com.tw>
+R:	Bling Chiang <Bling.Chiang@ite.com.tw>
+R:	Doreen Lin <doreen.lin@ite.com.tw>
+R:	Eric Su <Eric.Su@ite.com.tw>
+L:	linux-usb@vger.kernel.org
+S:	Maintained
+F:	Documentation/devicetree/bindings/usb/ite,itepd-it885x.yaml
+
 IVTV VIDEO4LINUX DRIVER
 M:	Andy Walls <awalls@md.metrocast.net>
 L:	linux-media@vger.kernel.org

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver
  2026-07-09 16:34 [PATCH v2 0/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
  2026-07-09 16:34 ` [PATCH v2 1/2] dt-bindings: usb: Add ITE IT885x support Amber Kao
@ 2026-07-09 16:34 ` Amber Kao
  2026-07-09  8:49   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Amber Kao @ 2026-07-09 16:34 UTC (permalink / raw)
  To: Jeson Yang, Yaode Fang, Bling Chiang, Doreen Lin, Eric Su,
	Greg Kroah-Hartman, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heikki Krogerus
  Cc: linux-usb, devicetree, linux-kernel, Amber Kao

Add core UCSI support for the ITE IT885x USB Type-C Power Delivery
controller over I2C.

Per Heikki's review of v1, this series has been split into smaller
patches. This patch provides the bare minimum: register the UCSI
ports and partners only, with no command translation and no
Alternate Mode support. The command-translation hook and Alternate
Mode child device from v1 have been dropped entirely for this patch,
and will be reintroduced one feature at a time in follow-up series.

v1 -> v2:
- Per Heikki: split into a minimal, single-file patch
  (ucsi_itepd.c only); the itepd.c/itepd.h core, the auxiliary-bus
  child-device split, and itepd_altmode.c are dropped from this
  patch and deferred to a follow-up series
- Per Heikki: removed the UCSI command-translation hook
  (ucsi_itepd_command_hook()) entirely for this minimal patch
- Use heap-allocated (kzalloc) I2C buffers instead of stack memory,
  since i2c_transfer() buffers must be DMA-safe
- Add explicit little-endian conversions for all multi-byte
  register fields
- Require a valid IRQ at probe time
- Fix probe()/remove() ordering so the IRQ thread can never observe
  a freed or not-yet-created ucsi instance
- Add an i2c_device_id table alongside of_device_id
- Dropped the AUXILIARY_BUS/DRM Kconfig dependencies, no longer
  needed without the altmode client

Cc: Yaode Fang <Yaode.Fang@ite.com.tw>
Cc: Jeson Yang <jeson.yang@ite.com.tw>
Cc: Bling Chiang <Bling.Chiang@ite.com.tw>
Cc: Eric Su <Eric.Su@ite.com.tw>
Cc: Doreen Lin <doreen.lin@ite.com.tw>
Signed-off-by: Amber Kao <amber.kao@ite.com.tw>
---
v1: https://lore.kernel.org/all/20260615-ucsi-itepd-feature-v1-0-a826cfd0df6a@ite.com.tw/
---
 MAINTAINERS                         |   1 +
 drivers/usb/typec/ucsi/Kconfig      |  10 ++
 drivers/usb/typec/ucsi/Makefile     |   1 +
 drivers/usb/typec/ucsi/ucsi_itepd.c | 348 ++++++++++++++++++++++++++++++++++++
 4 files changed, 360 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 1b03fa3aa060..22c0f386b25a 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -13730,6 +13730,7 @@ R:	Eric Su <Eric.Su@ite.com.tw>
 L:	linux-usb@vger.kernel.org
 S:	Maintained
 F:	Documentation/devicetree/bindings/usb/ite,itepd-it885x.yaml
+F:	drivers/usb/typec/ucsi/ucsi_itepd.c
 
 IVTV VIDEO4LINUX DRIVER
 M:	Andy Walls <awalls@md.metrocast.net>
diff --git a/drivers/usb/typec/ucsi/Kconfig b/drivers/usb/typec/ucsi/Kconfig
index 87dd992a4b9e..28442d2aebf8 100644
--- a/drivers/usb/typec/ucsi/Kconfig
+++ b/drivers/usb/typec/ucsi/Kconfig
@@ -104,4 +104,14 @@ config UCSI_HUAWEI_GAOKUN
 	  To compile the driver as a module, choose M here: the module will be
 	  called ucsi_huawei_gaokun.
 
+config UCSI_ITEPD
+	tristate "UCSI Interface Driver for ITE IT885x"
+	depends on I2C
+	help
+	  This driver enables UCSI support on platforms that expose an
+	  ITE IT885x Type-C Power Delivery controller over I2C interface
+
+	  To compile the driver as a module, choose M here: the module
+	  will be called ucsi_itepd.
+
 endif
diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makefile
index c7e38bf01350..0903a1fd486b 100644
--- a/drivers/usb/typec/ucsi/Makefile
+++ b/drivers/usb/typec/ucsi/Makefile
@@ -28,3 +28,4 @@ obj-$(CONFIG_UCSI_PMIC_GLINK)		+= ucsi_glink.o
 obj-$(CONFIG_CROS_EC_UCSI)		+= cros_ec_ucsi.o
 obj-$(CONFIG_UCSI_LENOVO_YOGA_C630)	+= ucsi_yoga_c630.o
 obj-$(CONFIG_UCSI_HUAWEI_GAOKUN)	+= ucsi_huawei_gaokun.o
+obj-$(CONFIG_UCSI_ITEPD)		+= ucsi_itepd.o
diff --git a/drivers/usb/typec/ucsi/ucsi_itepd.c b/drivers/usb/typec/ucsi/ucsi_itepd.c
new file mode 100644
index 000000000000..e364a3bd7df4
--- /dev/null
+++ b/drivers/usb/typec/ucsi/ucsi_itepd.c
@@ -0,0 +1,348 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2025-2026, ITE. All Rights Reserved
+ */
+#include <linux/bits.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/slab.h>
+#include <linux/unaligned.h>
+
+#include "ucsi.h"
+
+#define ITEPD_UCSI_VERSION_REG	0x80
+#define ITEPD_UCSI_CCI_REG	    0x84
+#define ITEPD_UCSI_MSG_IN_REG	0x88
+#define ITEPD_UCSI_CONTROL_REG	0x98
+
+#define ITEPD_VENDOR_WC_INT	    0xbc
+#define ITEPD_VENDOR_INT	    0xbd
+#define ITEPD_ALERT_VDM_EVENT	    BIT(0)
+#define ITEPD_ALERT_UCSI_EVENT	    BIT(1)
+
+#define ITEPD_MSG_IN_MAX_LEN	0x28
+
+struct itepd {
+	struct i2c_client *client;
+	struct ucsi *ucsi;
+	struct mutex i2c_lock;		/* Serializes I2C accesses */
+	struct mutex received_lock;	/* Protects cci and msg_in */
+	u8 msg_in[ITEPD_MSG_IN_MAX_LEN];
+	u32 cci;
+};
+
+static u8 ucsi_itepd_get_len(u32 cci)
+{
+	if (cci & UCSI_CCI_COMMAND_COMPLETE)
+		return UCSI_CCI_LENGTH(cci);
+	return 0;
+}
+
+static int itepd_read_reg(struct itepd *itepd, u8 reg, void *data, u32 len)
+{
+	struct i2c_client *client = itepd->client;
+	struct i2c_msg msg[2];
+	u8 *buf;
+	int ret;
+
+	/* I2C buffers must be DMA-safe, so no stack memory here. */
+	buf = kzalloc(len + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	buf[0] = reg;
+
+	msg[0].addr     = client->addr;
+	msg[0].flags    = 0;
+	msg[0].len      = 1;
+	msg[0].buf      = buf;
+
+	msg[1].addr     = client->addr;
+	msg[1].flags    = I2C_M_RD;
+	msg[1].len      = len;
+	msg[1].buf      = buf + 1;
+
+	mutex_lock(&itepd->i2c_lock);
+	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
+	mutex_unlock(&itepd->i2c_lock);
+	if (ret < 0) {
+		dev_err(&client->dev, "reg 0x%02x read failed: %d\n", reg, ret);
+		goto out_free;
+	}
+	if (ret != ARRAY_SIZE(msg)) {
+		ret = -EIO;
+		goto out_free;
+	}
+
+	memcpy(data, buf + 1, len);
+	ret = 0;
+
+out_free:
+	kfree(buf);
+	return ret;
+}
+
+static int itepd_write_reg(struct itepd *itepd, u8 reg, const void *data, u32 len)
+{
+	struct i2c_client *client = itepd->client;
+	struct i2c_msg msg[1];
+	u8 *buf;
+	int ret;
+
+	buf = kzalloc(len + 1, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	buf[0] = reg;
+	memcpy(buf + 1, data, len);
+
+	msg[0].addr = client->addr;
+	msg[0].flags = 0;
+	msg[0].len = len + 1;
+	msg[0].buf = buf;
+
+	mutex_lock(&itepd->i2c_lock);
+	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
+	mutex_unlock(&itepd->i2c_lock);
+
+	if (ret < 0) {
+		dev_err(&client->dev, "reg 0x%02x write failed: %d\n", reg, ret);
+		goto out_free;
+	}
+
+	if (ret != ARRAY_SIZE(msg)) {
+		ret = -EIO;
+		goto out_free;
+	}
+
+	ret = 0;
+
+out_free:
+	kfree(buf);
+	return ret;
+}
+
+static int ucsi_itepd_read_version(struct ucsi *ucsi, u16 *version)
+{
+	struct itepd *itepd = ucsi_get_drvdata(ucsi);
+	__le16 le_version;
+	int ret;
+
+	ret = itepd_read_reg(itepd, ITEPD_UCSI_VERSION_REG, &le_version,
+			     sizeof(le_version));
+
+	if (ret)
+		return ret;
+
+	*version = le16_to_cpu(le_version);
+
+	return 0;
+}
+
+static int ucsi_itepd_read_cci(struct ucsi *ucsi, u32 *cci)
+{
+	struct itepd *itepd = ucsi_get_drvdata(ucsi);
+
+	mutex_lock(&itepd->received_lock);
+	*cci = itepd->cci;
+	mutex_unlock(&itepd->received_lock);
+
+	return 0;
+}
+
+static int ucsi_itepd_poll_cci(struct ucsi *ucsi, u32 *cci)
+{
+	struct itepd *itepd = ucsi_get_drvdata(ucsi);
+	__le32 le_cci;
+	int ret;
+
+	ret = itepd_read_reg(itepd, ITEPD_UCSI_CCI_REG, &le_cci,
+			     sizeof(le_cci));
+
+	if (ret)
+		return ret;
+
+	*cci = le32_to_cpu(le_cci);
+
+	return 0;
+}
+
+static int ucsi_itepd_read_message_in(struct ucsi *ucsi, void *val, size_t val_len)
+{
+	struct itepd *itepd = ucsi_get_drvdata(ucsi);
+
+	mutex_lock(&itepd->received_lock);
+	memcpy(val, itepd->msg_in, min(val_len, sizeof(itepd->msg_in)));
+	mutex_unlock(&itepd->received_lock);
+
+	return 0;
+}
+
+static int ucsi_itepd_async_control(struct ucsi *ucsi, u64 command)
+{
+	struct itepd *itepd = ucsi_get_drvdata(ucsi);
+	__le64 le_cmd = cpu_to_le64(command);
+
+	return itepd_write_reg(itepd, ITEPD_UCSI_CONTROL_REG, &le_cmd,
+			       sizeof(le_cmd));
+}
+
+static const struct ucsi_operations ucsi_itepd_ops = {
+	.read_version		= ucsi_itepd_read_version,
+	.read_cci		= ucsi_itepd_read_cci,
+	.poll_cci		= ucsi_itepd_poll_cci,
+	.read_message_in	= ucsi_itepd_read_message_in,
+	.sync_control		= ucsi_sync_control_common,
+	.async_control		= ucsi_itepd_async_control,
+};
+
+static irqreturn_t itepd_irq_process(struct itepd *itepd)
+{
+	u8 msg_in[ITEPD_MSG_IN_MAX_LEN] = {};
+	__le32 le_cci;
+	u32 cci = 0;
+	u8 event;
+	u8 len;
+	int ret;
+
+	ret = itepd_read_reg(itepd, ITEPD_VENDOR_INT, &event, sizeof(event));
+
+	if (ret)
+		return IRQ_NONE;
+
+	event &= ITEPD_ALERT_VDM_EVENT | ITEPD_ALERT_UCSI_EVENT;
+
+	if (!event)
+		return IRQ_NONE;
+
+	if (event & ITEPD_ALERT_UCSI_EVENT) {
+		ret = itepd_read_reg(itepd, ITEPD_UCSI_CCI_REG, &le_cci, sizeof(le_cci));
+		if (ret)
+			goto out_clear;
+
+		cci = le32_to_cpu(le_cci);
+		len = min_t(u8, ucsi_itepd_get_len(cci), sizeof(msg_in));
+
+		if (len) {
+			ret = itepd_read_reg(itepd, ITEPD_UCSI_MSG_IN_REG,
+					     msg_in, len);
+			if (ret)
+				goto out_clear;
+		}
+
+		mutex_lock(&itepd->received_lock);
+		itepd->cci = cci;
+		memcpy(itepd->msg_in, msg_in, sizeof(msg_in));
+		mutex_unlock(&itepd->received_lock);
+	}
+
+out_clear:
+	itepd_write_reg(itepd, ITEPD_VENDOR_WC_INT, &event, sizeof(event));
+
+	if (!ret && (event & ITEPD_ALERT_UCSI_EVENT))
+		ucsi_notify_common(itepd->ucsi, cci);
+
+	return IRQ_HANDLED;
+}
+
+static irqreturn_t itepd_irq_thread_fn(int irq, void *data)
+{
+	struct itepd *itepd = data;
+
+	return itepd_irq_process(itepd);
+}
+
+static int itepd_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct itepd *itepd;
+	u8 event;
+	int ret;
+
+	if (client->irq <= 0)
+		return dev_err_probe(dev, -ENODEV, "no IRQ provided\n");
+
+	itepd = devm_kzalloc(dev, sizeof(*itepd), GFP_KERNEL);
+
+	if (!itepd)
+		return -ENOMEM;
+
+	itepd->client = client;
+	mutex_init(&itepd->i2c_lock);
+	mutex_init(&itepd->received_lock);
+	i2c_set_clientdata(client, itepd);
+
+	itepd->ucsi = ucsi_create(dev, &ucsi_itepd_ops);
+
+	if (IS_ERR(itepd->ucsi))
+		return PTR_ERR(itepd->ucsi);
+
+	ucsi_set_drvdata(itepd->ucsi, itepd);
+
+	event = ITEPD_ALERT_VDM_EVENT | ITEPD_ALERT_UCSI_EVENT;
+	ret = itepd_write_reg(itepd, ITEPD_VENDOR_WC_INT, &event, sizeof(event));
+
+	if (ret)
+		goto out_ucsi_destroy;
+
+	ret = request_threaded_irq(client->irq, NULL, itepd_irq_thread_fn, IRQF_ONESHOT,
+				   dev_name(dev), itepd);
+
+	if (ret) {
+		dev_err(dev, "request_threaded_irq failed - %d\n", ret);
+		goto out_ucsi_destroy;
+	}
+
+	ret = ucsi_register(itepd->ucsi);
+	if (ret) {
+		dev_err(dev, "failed to register UCSI: %d\n", ret);
+		goto out_free_irq;
+	}
+
+	return 0;
+
+out_free_irq:
+	free_irq(client->irq, itepd);
+out_ucsi_destroy:
+	ucsi_destroy(itepd->ucsi);
+	return ret;
+}
+
+static void itepd_remove(struct i2c_client *client)
+{
+	struct itepd *itepd = i2c_get_clientdata(client);
+
+	ucsi_unregister(itepd->ucsi);
+	free_irq(client->irq, itepd);
+	ucsi_destroy(itepd->ucsi);
+}
+
+static const struct of_device_id itepd_of_match_table[] = {
+	{ .compatible = "ite,itepd-it885x" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, itepd_of_match_table);
+
+static const struct i2c_device_id itepd_id_table[] = {
+	{ "ucsi_itepd", 0 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, itepd_id_table);
+
+static struct i2c_driver itepd_driver = {
+	.driver = {
+		.name = "ucsi_itepd",
+		.of_match_table = itepd_of_match_table,
+	},
+	.probe = itepd_probe,
+	.remove = itepd_remove,
+	.id_table = itepd_id_table,
+};
+module_i2c_driver(itepd_driver);
+
+MODULE_AUTHOR("Jeson Yang <jeson.yang@ite.com.tw>");
+MODULE_DESCRIPTION("UCSI driver for ITE IT885x Type-C PD controllers");
+MODULE_LICENSE("GPL");

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-07-09  8:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-09 16:34 [PATCH v2 0/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
2026-07-09 16:34 ` [PATCH v2 1/2] dt-bindings: usb: Add ITE IT885x support Amber Kao
2026-07-09 16:34 ` [PATCH v2 2/2] usb: typec: ucsi: Add ITE IT885x Type-C PD controller driver Amber Kao
2026-07-09  8:49   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox