Skip to contentSkip to conversation

i2c: ma35d1: Add support for MA35D1 I2C controller

PATCH Superseded Zi-Yu Chen 2 participants 5 messages Jun 10

Subsystems (4)

v4 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Zi-Yu Chen

author
#0 lore raw

v4 cover i2c: ma35d1: Add support for MA35D1 I2C controller

This series adds support for the I2C controller found in the Nuvoton MA35D1 SoC. The driver supports controller and optional target mode and runtime power management.

The implementation has been tested on the Nuvoton MA35D1 SOM board.

Changes in v4:
- Patch 1 (dt-bindings):
- Update example interrupt number.
- Patch 2 (driver):
- Remove redundant spinlocks and fix 'irq' type to signed.
- Fix target address recovery in ma35d1_i2c_reset().
- Refactor IRQ handler for multi-msg reads and NACK/IGNORE_NAK.
- Fix timeout UAF via explicit IRQ disabling during recovery.
- Drop IRQF_SHARED and fix PM paths in probe/unregistration.
- Limit suspend/resume wakeup logic to target mode only.
- Patch 3 (dts):
- Add missing i2c0 node to dtsi.
- Switch SoM configuration from i2c1 to i2c2 with updated pinctrl.
- Add nau8822 audio codec node under i2c2 on the SoM board.
- Drop redundant i2c0 alias and clean up dtsi property ordering.
v3: https://lore.kernel.org/r/[email protected]/

Changes in v3:
- Fix minor DTS formatting issues (whitespace, missing newline)
v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Overall:
- Rebase on linux-i2c/i2c-next
- Switched terminology from "master/slave" to "controller/target".
- Patch 1 (dt-bindings):
- Simplified description and fixed 'reg' size in example.
- Patch 2 (driver):
- Modernized using devm_*, generic device properties, and FIELD_PREP/GENMASK.
- Optimized power management by moving clock control to runtime PM.
- Simplified code by removing redundant .remove(), .owner, and inlines.
- Added dev_err_probe() and default bus frequency handling.
- Patch 3 (dts):
- Moved i2c aliases to board dts and reordered nodes alphabetically.
v1: https://lore.kernel.org/r/[email protected]

Zi-Yu Chen (3):
dt-bindings: i2c: nuvoton,ma35d1-i2c: Add MA35D1 I2C controller
i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support
arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC

 .../bindings/i2c/nuvoton,ma35d1-i2c.yaml      |  63 ++
 .../boot/dts/nuvoton/ma35d1-som-256m.dts      |  20 +
 arch/arm64/boot/dts/nuvoton/ma35d1.dtsi       |  72 ++
 drivers/i2c/busses/Kconfig                    |  13 +
 drivers/i2c/busses/Makefile                   |   1 +
 drivers/i2c/busses/i2c-ma35d1.c               | 778 ++++++++++++++++++
 6 files changed, 947 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml
 create mode 100644 drivers/i2c/busses/i2c-ma35d1.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v4 1/3 dt-bindings: i2c: nuvoton,ma35d1-i2c: Add MA35D1 I2C controller

Add device tree binding documentation for the I2C controller found in the Nuvoton MA35D1 SoC.

---
 .../bindings/i2c/nuvoton,ma35d1-i2c.yaml      | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml
Documentation/devicetree/bindings/i2c/nuvoton,ma35d1-i2c.yaml new file +63 -0
new file mode 100644
index 000000000000..f2c004049d86
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/i2c/nuvoton,ma35d1-i2c.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 I2C Controller
+
+maintainers:
+  - Zi-Yu Chen <[email protected]>
+
+description:
+  The Nuvoton MA35D1 I2C controller supports controller and optional target mode.
+
+allOf:
+  - $ref: /schemas/i2c/i2c-controller.yaml#
+
+properties:
+  compatible:
+    const: nuvoton,ma35d1-i2c
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  clock-frequency:
+    description:
+      Desired I2C bus clock frequency in Hz. The absence of this property
+      indicates the default frequency 100 kHz.
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - resets
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+    #include <dt-bindings/reset/nuvoton,ma35d1-reset.h>
+
+    i2c0: i2c@40800000 {
+      compatible = "nuvoton,ma35d1-i2c";
+      reg = <0x40800000 0x1000>;
+      interrupts = <GIC_SPI 13 IRQ_TYPE_LEVEL_HIGH>;
+      clocks = <&clk I2C0_GATE>;
+      clock-frequency = <100000>;
+      resets = <&sys MA35D1_RESET_I2C0>;
+      #address-cells = <1>;
+      #size-cells = <0>;
+    };
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v4 2/3 i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

In reply to Zi-Yu Chen

Add I2C support for Nuvoton MA35D1 SoC.
The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

---
 drivers/i2c/busses/Kconfig      |  13 +
 drivers/i2c/busses/Makefile     |   1 +
 drivers/i2c/busses/i2c-ma35d1.c | 778 ++++++++++++++++++++++++++++++++
 3 files changed, 792 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-ma35d1.c
drivers/i2c/busses/Kconfig +13 -0
index 8c935f867a37..816c55814ed4 100644
@@ -1590,4 +1590,17 @@ config I2C_VIRTIO
           This driver can also be built as a module. If so, the module
           will be called i2c-virtio.
 
+config I2C_MA35D1
+	tristate "Nuvoton MA35D1 I2C driver"
+	depends on ARCH_MA35 || COMPILE_TEST
+	select I2C_SLAVE
+	help
+	  If you say yes to this option, support will be included for the
+	  I2C controller in the Nuvoton MA35D1 SoC. This driver
+	  supports the standard I2C bus protocols, including master and
+	  slave modes.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-ma35d1.
+
 endmenu
drivers/i2c/busses/Makefile +1 -0
index 547123ab351f..264f6f3f608d 100644
@@ -130,6 +130,7 @@ obj-$(CONFIG_I2C_XILINX)	+= i2c-xiic.o
 obj-$(CONFIG_I2C_XLP9XX)	+= i2c-xlp9xx.o
 obj-$(CONFIG_I2C_RCAR)		+= i2c-rcar.o
 obj-$(CONFIG_I2C_GXP)		+= i2c-gxp.o
+obj-$(CONFIG_I2C_MA35D1)	+= i2c-ma35d1.o
 
 # External I2C/SMBus adapter drivers
 obj-$(CONFIG_I2C_DIOLAN_U2C)	+= i2c-diolan-u2c.o
drivers/i2c/busses/i2c-ma35d1.c new file +778 -0

778 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v4 2/3] i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [High] Missing IRQ synchronization on teardown paths causes use-after-free and NULL pointer dereferences.…

Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [High] Missing IRQ synchronization on teardown paths causes use-after-free and NULL pointer dereferences.
- [High] Lockless register access and unsafe read-modify-write operations on MA35_CTL0 corrupt the hardware state machine.
- [High] Arbitration lost is handled incorrectly, violating I2C specifications and causing transfer timeouts.
- [High] Explicit pm_runtime_disable() in the probe error path causes a double disable, permanently breaking deferred probes.
- [High] Missing .remove callback leaves the I2C clock running and violates the clock API if the module is unloaded while runtime active.
- [High] Clearing MA35_CTL_I2CEN in ma35d1_unreg_target() breaks subsequent master transfers due to PM runtime state mismatch.
- [Medium] The driver falsely advertises I2C_FUNC_PROTOCOL_MANGLING but incorrectly handles I2C_M_IGNORE_NAK and ignores I2C_M_NOSTART.
- [Medium] The system suspend callback leaks power by leaving the I2C clock enabled if the device is active and target mode is inactive. --

commit bda32059fddeb5d34a52e14ab99c35478baf80a4
Author: Zi-Yu Chen <[email protected]>

i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support

Add I2C support for Nuvoton MA35D1 SoC. The controller supports standard, fast and fast-plus modes, and provides controller/target functionality.

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0 -0
new file mode 100644
index 0000000000000..7b6b20c0996d2

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 147

Quoted 9 lines
drivers/i2c/busses/i2c-ma35d1.c new file +9 -0
+static void ma35d1_i2c_write_CTL(struct ma35d1_i2c *i2c, unsigned int ctl)
+{
+	unsigned int val;
+
+	val = readl(i2c->regs + MA35_CTL0);
+	val &= ~(MA35_CTL_STA_SI_AA | MA35_CTL_STO);
+	val |= ctl;
+	writel(val, i2c->regs + MA35_CTL0);
+}

[Severity: High]
Is there a missing lock here? The driver performs unprotected read-modify-write operations on the shared MA35_CTL0 register. A timeout or unbind process calling ma35d1_i2c_disable_irq() could race with the interrupt handler updating state via ma35d1_i2c_write_CTL(), clobbering the STO or SI bits and locking up the hardware.

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 174

Quoted 7 lines
drivers/i2c/busses/i2c-ma35d1.c new file +7 -0
+static void ma35d1_i2c_disable_irq(struct ma35d1_i2c *i2c)
+{
+	unsigned long tmp;
+
+	tmp = readl(i2c->regs + MA35_CTL0);
+	writel(tmp & ~MA35_CTL_INTEN, i2c->regs + MA35_CTL0);
+}

[Severity: High]
Can this read-modify-write race with the interrupt handler? See the comment on ma35d1_i2c_write_CTL() above.

[ ... ]

Quoted 3 lines
drivers/i2c/busses/i2c-ma35d1.c new file +3 -0
+static void i2c_ma35d1_irq_target_trx(struct ma35d1_i2c *i2c,
+				      unsigned long i2c_status)
+{

[ ... ]

Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +4 -0
+	case MA35_S_REPEAT_START_STOP:
+	/* A STOP or repeated START has been received
+	 *  while still addressed as Target/Receiver
+	 */

drivers/i2c/busses/i2c-ma35d1.c line 287

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+		i2c_slave_event(i2c->target, I2C_SLAVE_STOP, &byte);
+		break;
+
+	default:
+		dev_err(i2c->dev, "Status 0x%02lx is NOT processed\n",
+			i2c_status);
+		break;
+	}
+	ma35d1_i2c_write_CTL(i2c, MA35_CTL_SI_AA);
+}

[Severity: High]
Does this fail to handle target arbitration lost states (0x68, 0xb0)? If execution falls to the default case, the target backend is never notified of the transfer start, potentially desynchronizing the target state machine.

[ ... ]

Quoted 3 lines
drivers/i2c/busses/i2c-ma35d1.c new file +3 -0
+static void i2c_ma35d1_irq_controller_trx(struct ma35d1_i2c *i2c,
+					  unsigned long i2c_status)
+{

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 344

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+	case MA35_M_TRAN_ADDR_NACK:
+	case MA35_M_RECE_ADDR_NACK:
+		/* Controller Transmit Address NACK */
+		/* 0x20: SLA+W has been transmitted and NACK has been received */
+		/* 0x48: SLA+R has been transmitted and NACK has been received */
+		if (i2c->msg->flags & I2C_M_IGNORE_NAK) {
+			ma35d1_i2c_stop(i2c, 0);
+		} else {

[Severity: Medium]
Does this correctly implement I2C_M_IGNORE_NAK? NACKs are expected to be ignored to proceed with transmitting the payload as if an ACK occurred, but this terminates the transfer completely.

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 416

Quoted 12 lines
drivers/i2c/busses/i2c-ma35d1.c new file +12 -0
+static irqreturn_t ma35d1_i2c_irq(int irqno, void *dev_id)
+{
+	struct ma35d1_i2c *i2c = dev_id;
+	unsigned long status;
+
+	status = readl(i2c->regs + MA35_STATUS0);
+
+	if (status == MA35_M_ARB_LOST) {
+		dev_err(i2c->dev, "Arbitration lost\n");
+		ma35d1_i2c_stop(i2c, -EAGAIN);
+		goto out;
+	}

[Severity: High]
Should this issue a STOP condition when arbitration is lost? Generating a STOP while another master is actively driving the bus violates the I2C protocol, which can disrupt the winning master's transfer.

[ ... ]

Quoted 3 lines
drivers/i2c/busses/i2c-ma35d1.c new file +3 -0
+static int ma35d1_i2c_doxfer(struct ma35d1_i2c *i2c, struct i2c_msg *msgs,
+			     int num)
+{

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 456

Quoted 11 lines
drivers/i2c/busses/i2c-ma35d1.c new file +11 -0
+	ma35d1_i2c_enable_irq(i2c);
+	ma35d1_i2c_write_CTL(i2c, MA35_CTL_STA_SI);
+	timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
+	ma35d1_i2c_disable_irq(i2c);
+	ret = i2c->msg_idx;
+
+	if (timeout == 0) {
+		dev_dbg(i2c->dev, "xfer timeout\n");
+		ret = -ETIMEDOUT;
+		goto reset;
+	}

[Severity: High]
Are we missing synchronization with inflight ISRs here? Disabling the IRQ at the register level doesn't stop an already executing handler. Could the ISR blindly proceed to dereference the message buffer after it has been freed? This might need synchronize_irq() after ma35d1_i2c_disable_irq().

[ ... ]

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+static int ma35d1_unreg_target(struct i2c_client *target)
+{
+	struct ma35d1_i2c *i2c = i2c_get_adapdata(target->adapter);
+	unsigned int val;
+	int ret;
+
+	if (!i2c->target)
+		return -EINVAL;

drivers/i2c/busses/i2c-ma35d1.c line 560

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+
+	/* Disable I2C interrupt */
+	ma35d1_i2c_disable_irq(i2c);
+
+	/* Disable I2C */
+	val = readl(i2c->regs + MA35_CTL0);
+	val &= ~MA35_CTL_I2CEN;
+	writel(val, i2c->regs + MA35_CTL0);
+
+	i2c->target = NULL;

[Severity: High]
Similar to ma35d1_i2c_doxfer(), does this need synchronize_irq() before setting i2c->target to NULL? Setting it to NULL while an ISR is concurrently executing on another CPU could cause a NULL pointer dereference.

drivers/i2c/busses/i2c-ma35d1.c line 562

Quoted 2 lines
drivers/i2c/busses/i2c-ma35d1.c new file +2 -0
+
+	ret = pm_runtime_put_sync(i2c->dev);

[Severity: High]
Can clearing MA35_CTL_I2CEN above break subsequent master transfers? If the autosuspend timer is active, pm_runtime_put_sync() leaves the device in the RPM_ACTIVE state. A subsequent master transfer could observe RPM_ACTIVE and skip runtime_resume(), resulting in an -EBUSY timeout because the hardware was disabled.

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 573

Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +5 -0
+/* Declare Our I2C Functionality */
+static u32 ma35d1_i2c_func(struct i2c_adapter *adap)
+{
+	return I2C_FUNC_I2C | I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_SMBUS_EMUL;
+}

[Severity: Medium]
Should this driver advertise I2C_FUNC_PROTOCOL_MANGLING? It unconditionally issues a START condition (ignoring I2C_M_NOSTART) and handles I2C_M_IGNORE_NAK incorrectly.

[ ... ]

Quoted 2 lines
drivers/i2c/busses/i2c-ma35d1.c new file +2 -0
+static int ma35d1_i2c_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 673

Quoted 6 lines
drivers/i2c/busses/i2c-ma35d1.c new file +6 -0
+rpm_put:
+	pm_runtime_dont_use_autosuspend(dev);
+	pm_runtime_put_sync(dev);
+	pm_runtime_disable(dev);
+	return ret;
+}

[Severity: High]
Can this cause a double disable of runtime PM? The driver uses devm_pm_runtime_enable() which attaches a devres cleanup action. Calling pm_runtime_disable() manually increments the disable_depth, and the subsequent devres cleanup increments it again, preventing the device from powering on during deferred probes.

Quoted 15 lines
drivers/i2c/busses/i2c-ma35d1.c new file +15 -0
+static int ma35d1_i2c_suspend(struct device *dev)
+{
+	struct ma35d1_i2c *i2c = dev_get_drvdata(dev);
+	unsigned int val;
+
+	/* Prepare for wake-up from I2C events if target mode is active */
+	if (i2c->target) {
+		val = readl(i2c->regs + MA35_CTL0);
+		val |= (MA35_CTL_SI | MA35_CTL_AA);
+		writel(val, i2c->regs + MA35_CTL0);
+
+		/* Setup wake-up control */
+		writel(0x1, i2c->regs + MA35_WKCTL);
+
+		/* Clear pending wake-up flags */

drivers/i2c/busses/i2c-ma35d1.c line 699

Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +10 -0
+		val = readl(i2c->regs + MA35_WKSTS);
+		writel(val, i2c->regs + MA35_WKSTS);
+
+		enable_irq_wake(i2c->irq);
+
+		ma35d1_i2c_enable_irq(i2c);
+	}
+
+	return 0;
+}

[Severity: Medium]
Could this leak power by leaving the I2C clock enabled if the device is active and target mode is inactive? If the device happened to be in the RPM_ACTIVE state when the system goes to sleep, the clock remains fully enabled for the duration of the suspend.

[ ... ]

drivers/i2c/busses/i2c-ma35d1.c line 773

Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +8 -0
+static struct platform_driver ma35d1_i2c_driver = {
+	.probe      = ma35d1_i2c_probe,
+	.driver     = {
+		.name   = "ma35d1-i2c",
+		.of_match_table = ma35d1_i2c_of_match,
+		.pm = pm_ptr(&ma35d1_i2c_pmops),
+	},
+};

[Severity: High]
Is a .remove callback needed here? If the module is unloaded while the device is in the RPM_ACTIVE state (e.g., within the 5000ms autosuspend window), the clock remains enabled. During devres unbinding, executing clk_unprepare() on an enabled clock violates the clock framework constraints.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#4 lore raw

PATCH v4 3/3 arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC

In reply to Zi-Yu Chen

Add I2C controller nodes to the MA35D1 SoC dtsi file. Also, enable the I2C2 interface on the MA35D1 SOM board and configure its pinctrl to allow communication with the onboard NAU8822 audio codec

---
 .../boot/dts/nuvoton/ma35d1-som-256m.dts      | 20 ++++++
 arch/arm64/boot/dts/nuvoton/ma35d1.dtsi       | 72 +++++++++++++++++++
 2 files changed, 92 insertions(+)
arch/arm64/boot/dts/nuvoton/ma35d1-som-256m.dts +20 -0
index f6f20a17e501..4b9ff6d00631 100644
@@ -55,6 +55,18 @@ &clk {
 			   "integer";
 };
 
+&i2c2 {
+	status = "okay";
+	pinctrl-names = "default";
+	pinctrl-0 = <&pinctrl_i2c2>;
+
+	nau8822: nau8822@1a {
+		compatible = "nuvoton,nau8822";
+		reg = <0x1a> ;
+		nuvoton,spk-btl;
+	};
+};
+
 &pinctrl {
 	uart-grp {
 		pinctrl_uart0: uart0-pins {
@@ -98,6 +110,14 @@ pinctrl_uart16: uart16-pins {
 			power-source = <1>;
 		};
 	};
+
+	i2c-grp {
+		pinctrl_i2c2: i2c2-pins {
+			nuvoton,pins = <1 8 4>,
+				       <1 9 4>;
+			bias-disable;
+		};
+	};
 };
 
 &uart0 {
arch/arm64/boot/dts/nuvoton/ma35d1.dtsi +72 -0

78 lines not on this page · raw message

Signature · 1 line
2.34.1