i2c: ma35d1: Add support for MA35D1 I2C controller
Subsystems (4)
v5 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 v5:
- Patch 2 (driver):
- Add spinlock protection for MA35_CTL0 register access.
- Add NULL pointer check for i2c->msg in the ISR.
- Use devres-managed cleanup (devm_add_action_or_reset) for runtime PM.
- Use pm_runtime_force_suspend/resume() for Controller mode system sleep.
- Control I2CEN bit enabling/disabling via runtime PM.
- Refactor arbitration lost handling in state machines.
- Remove unsupported I2C_FUNC_PROTOCOL_MANGLING flag.
- Clean up readl_poll_timeout() parameters, retry loop, and unused macros.
v4: https://lore.kernel.org/r/[email protected]/
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 | 803 ++++++++++++++++++ 6 files changed, 972 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
PATCH v5 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
@@ -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
PATCH v5 2/3 i2c: ma35d1: Add Nuvoton MA35D1 I2C driver support
In reply to Zi-Yu ChenAdd 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 | 803 ++++++++++++++++++++++++++++++++ 3 files changed, 817 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ma35d1.c
drivers/i2c/busses/Kconfig +13 -0
@@ -1602,4 +1602,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
@@ -131,6 +131,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 +803 -0
803 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 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] Dangling `i2c->msg` pointer and lack of IRQ synchronization causes Use-After-Free / Memory…
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [High] Dangling i2c->msg pointer and lack of IRQ synchronization causes Use-After-Free / Memory Corruption on timeout.
- [High] Incomplete target unregistration and lack of IRQ sync causes NULL pointer dereference panic and bus freeze.
- [High] Zero-message transfer triggers a START condition followed by a ZERO_SIZE_PTR dereference panic.
- [Medium] Missing pm_runtime_mark_last_busy() breaks PM autosuspend delay logic.
- [Medium] Unlocked Read-Modify-Write of MA35_CTL0 in system suspend path races with the IRQ handler.
- [Medium] Incorrect handling of I2C_M_IGNORE_NAK aborts the transaction while reporting success.
- [Medium] A malformed or unusually large clock-frequency device tree property causes division by zero during probe.
- [Low] Comment incorrectly describes receiving data as transmitting data.
--
commit 469758a7eb28d07aab76dce6fd3f09b0e77b5e45
Author: Zi-Yu Chen <[email protected]>
Subject: 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
[ ... ]
drivers/i2c/busses/i2c-ma35d1.c static void ma35d1_i2c_irq_controller_trx(struct ma35d1_i2c *i2c, · line 352
Quoted 9 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0@@ -345,13 +345,11 @@ static void ma35d1_i2c_irq_controller_trx(struct ma35d1_i2c *i2c,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 handling I2C_M_IGNORE_NAK by stopping the transfer and returning 0 correctly implement the ignore flag?
Typically, ignoring a NACK implies treating it as an ACK and continuing to the data phase rather than aborting the transaction entirely.
Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0dev_dbg (i2c->dev,"\n i2c: ack was not received\n" );ma35d1_i2c_stop (i2c, -ENXIO ); }break ;
[ ... ]
drivers/i2c/busses/i2c-ma35d1.c
Quoted 3 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0case MA35_M_RECE_DATA_ACK :case MA35_M_RECE_DATA_NACK :/* DATA has been transmitted and ACK has been received */
[Severity: Low]
Is this comment accurate? Since this is the receive data case, shouldn't it state that data has been received instead of transmitted?
Quoted 2 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0byte =readb (i2c->regs +MA35_DAT ); i2c->msg->buf[i2c->msg_ptr++] = byte;
[ ... ]
Quoted 7 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0@@ -415,22 +415,19 @@ static int ma35d1_i2c_doxfer(struct ma35d1_i2c *i2c, struct i2c_msg *msgs, int num) {unsigned long timeout;unsigned int val;int ret, err;
drivers/i2c/busses/i2c-ma35d1.c static int ma35d1_i2c_doxfer(struct ma35d1_i2c *i2c, struct i2c_msg *msgs, · line 430
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0i2c->msg = msgs; i2c->msg_num = num; i2c->msg_ptr =0 ; i2c->msg_idx =0 ; i2c->err =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 );
[Severity: High]
What happens if ma35d1_i2c_doxfer() is called with num == 0 (e.g., via the I2C_RDWR ioctl)? The hardware is issued a START condition, but the i2c->msg_num == 0 condition in wait_event_timeout() evaluates to true instantly.
When the hardware START interrupt fires, will ma35d1_i2c_irq_controller_trx() then call ma35d1_i2c_set_addr() and dereference a ZERO_SIZE_PTR for i2c->msg?
drivers/i2c/busses/i2c-ma35d1.c static int ma35d1_i2c_doxfer(struct ma35d1_i2c *i2c, struct i2c_msg *msgs,
Quoted 8 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0ma35d1_i2c_disable_irq (i2c); ret = i2c->msg_idx;if (timeout ==0 ) {dev_dbg (i2c->dev,"xfer timeout\n" ); ret = -ETIMEDOUT ; goto reset; }
[Severity: High]
Does this timeout path leave a dangling pointer in i2c->msg? If ma35d1_i2c_disable_irq() runs but an interrupt handler is already executing or fires slightly late, the handler will bypass the unlikely(!i2c->msg) check and operate on memory that has already been freed by the caller.
Could this cause memory corruption when the handler writes to i2c->msg->buf[i2c->msg_ptr++], or a hardware external abort when ma35d1_i2c_reset() runs concurrently?
Quoted 2 lines
err = readl_poll_timeout(i2c->regs +
MA35_CTL0, val,
[ ... ]
Quoted 7 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0@@ -445,15 +442,13 @@ static int ma35d1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) {struct ma35d1_i2c *i2c =i2c_get_adapdata (adap);int ret; ret =pm_runtime_resume_and_get (i2c->dev);
drivers/i2c/busses/i2c-ma35d1.c static int ma35d1_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0if (ret)return ret; ret =ma35d1_i2c_doxfer (i2c, msgs, num);if (i2c->target) {ma35d1_i2c_write_ctl (i2c,MA35_CTL_SI_AA );ma35d1_i2c_enable_irq (i2c); }pm_runtime_put_autosuspend (i2c->dev);
[Severity: Medium]
Does dropping the PM reference here without calling pm_runtime_mark_last_busy() break the autosuspend delay logic?
Since the last busy timestamp isn't updated, won't subsequent transfers instantly suspend the controller once the initial 5000ms delay elapses, instead of keeping it active?
Quoted 3 lines
return ret;
}
[ ... ]
Quoted 4 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0@@ -490,12 +485,11 @@ static int ma35d1_unreg_target(struct i2c_client *target) {struct ma35d1_i2c *i2c =i2c_get_adapdata (target->adapter);int ret;
drivers/i2c/busses/i2c-ma35d1.c static int ma35d1_unreg_target(struct i2c_client *target)
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0if (!i2c->target)return -EINVAL ;/* Disable I2C interrupt */ ma35d1_i2c_disable_irq (i2c); i2c->target =NULL ; ret =pm_runtime_put_sync (i2c->dev);
[Severity: High]
Can setting i2c->target to NULL without calling synchronize_irq() lead to a NULL pointer dereference in ma35d1_i2c_irq_target_trx() if a concurrent target interrupt fires?
Additionally, since ma35d1_i2c_disable_irq() does not clear the hardware target address (MA35_ADDR0) or the AA bit in MA35_CTL0, does the I2C controller remain active on the bus? Would it continue to ACK its slave address without generating interrupts, permanently holding SCL low and freezing the bus until PM fully suspends it?
Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0if (ret <0 )dev_err (i2c->dev,"failed to suspend i2c controller" );return 0 ; }
[ ... ]
drivers/i2c/busses/i2c-ma35d1.c static int ma35d1_i2c_suspend(struct device *dev) · line 531
Quoted 10 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0@@ -529,15 +523,12 @@ 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 );
[Severity: Medium]
Does this unlocked read-modify-write on MA35_CTL0 race with ma35d1_i2c_write_ctl() in the interrupt handler?
Because the device is in target mode, it can still receive transactions and generate interrupts concurrently. Can this race corrupt the control register state, such as losing the MA35_CTL_SI flag or inadvertently clearing I2CEN?
Quoted 3 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0/* Setup wake-up control */ writel (0x1 , i2c->regs +MA35_WKCTL );
[ ... ]
drivers/i2c/busses/i2c-ma35d1.c static int ma35d1_i2c_probe(struct platform_device *pdev) · line 638
Quoted 7 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0@@ -642,12 +633,11 @@ static int ma35d1_i2c_probe(struct platform_device *pdev)/* Default to 100kHz if not specified in DT */ busfreq =100000 ;device_property_read_u32 (dev,"clock-frequency" , &busfreq);/* Calculate divider based on the current peripheral clock rate */ clkdiv =DIV_ROUND_CLOSEST (clk_get_rate (i2c->clk), busfreq *4 ) -1 ;
[Severity: Medium]
Can this division crash the kernel during boot if the device tree contains a malformed clock-frequency?
If busfreq is 0, or if it is an excessively large value that causes busfreq * 4 to overflow a 32-bit integer back to 0, does this result in a division by zero macro expansion?
Quoted 5 lines
drivers/i2c/busses/i2c-ma35d1.c new file +0
-0if (clkdiv <0 || clkdiv >0xffff )return dev_err_probe (dev, -EINVAL ,"invalid clkdiv value: %d\n" , clkdiv); i2c->irq =platform_get_irq (pdev,0 );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v5 3/3 arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC
In reply to Zi-Yu ChenAdd 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
@@ -57,6 +57,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 {
@@ -100,6 +112,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
[email protected]
AI reviewRe: [PATCH v5 3/3] arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The devicetree node for the NAU8822 audio codec uses a model-specific name rather than a generic…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The devicetree node for the NAU8822 audio codec uses a model-specific name rather than a generic functional name. --
commit 4b9e8074772f80384175f231ae4886c8f9599142
Author: Zi-Yu Chen <[email protected]>
arm64: dts: nuvoton: Add I2C nodes for MA35D1 SoC
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.
Quoted 5 lines
arch/arm64/boot/dts/nuvoton/ma35d1-som-256m.dts +0
-0
arch/arm64/boot/dts/nuvoton/ma35d1-som-256m.dts &clk { · line 63
Quoted 9 lines
arch/arm64/boot/dts/nuvoton/ma35d1-som-256m.dts +6
-0@@ -55,6 +55,18 @@ &clk { "integer"; }; +&i2c2 { + status = "okay"; + pinctrl-names = "default"; + pinctrl-0 = <&pinctrl_i2c2>; + + nau8822: nau8822@1a {
[Severity: Low]
This isn't a bug, but should this devicetree node use a generic functional name like audio-codec@1a instead of the model-specific nau8822@1a?
The Devicetree Specification (Section 2.2.2 Generic Names Recommendation) requires node names to describe the general class of device, and using specific names can lead to dt-schema validation warnings.
Quoted 6 lines
arch/arm64/boot/dts/nuvoton/ma35d1-som-256m.dts +6
-0+ compatible = "nuvoton,nau8822"; + reg = <0x1a> ; + nuvoton,spk-btl; + }; +}; +
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3