Skip to contentSkip to conversation

Add Qualcomm I2C target controller driver

PATCH Superseded Viken Dadhaniya 6 participants 15 messages Aug 13 – Sep 20

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add Qualcomm I2C target controller driver

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode and is not supported by the existing Qualcomm I2C master controller drivers (GENI, QUP).

This series adds DT binding and driver support for this IP. The driver uses the standard Linux I2C slave framework (reg_target/unreg_target, i2c_slave_event) so any slave backend (e.g. slave-24c02) can be attached via i2c_slave_register().

The series is structured as follows:

Patch 1: DT binding document
Patch 2: Driver implementation including Kconfig, Makefile and
MAINTAINERS entries

The driver has been tested on QDU1000 hardware.

---
Changes in v3:
- Rename file to match compatible (qcom,qdu1000-i2c-target.yaml)
- Remove interconnect-names property
- Remove allOf reference to i2c-controller.yaml
- Remove #address-cells and #size-cells from example
- Add minItems: 1 to pinctrl-names
- Move I2C_S_CORE_EN out of hw_init; enable core only in reg_slave
and hw_reset
- Add usleep_range(10, 20) after SW_RESET before reconfiguring hardware
- Fix RESTART_DETECTED dispatch order; handle before STRCH_RD and
Rx data phases
- Use test_and_set_bit in write_requested and handle_strch_rd
- Loop drain_rx_fifo until FIFO is empty to close post-STOP data race
- Disable core before clearing slave pointer in unreg_slave
- Clear target->status on resume
- Switch from devm_clk_get_enabled to devm_clk_get with manual
clk_prepare_enable
- Remove unbalanced disable_irq from remove
- Update struct kdoc to note I2C_LOCK_ROOT_ADAPTER serialises
reg/unreg_target
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Rename driver and binding from "slave" to "target" terminology
- Use SoC-specific compatible string (qcom,qdu1000-i2c-target) instead of
generic qcom,i2c-slave; add allOf/$ref to i2c-controller.yaml
- Replace SMBus layer (smbus_xfer, I2C_FUNC_SMBUS_*) with native Linux I2C
slave framework (reg_target/unreg_target, I2C_SLAVE_* events)
- Remove qcom,slave-addr DT property; slave address taken from the
registered i2c_client at reg_target() time
- Remove staging buffers and spinlock; bytes delivered directly to backend
via i2c_slave_event() per STRCH_RD/RX/STOP event
- Use SET_NOIRQ_SYSTEM_SLEEP_PM_OPS; PM core quiesces IRQs at noirq stage, removing need for manual disable_irq/enable_irq in suspend
- Simplify clock-names (xo/ahb) and interconnect-names (i2c)
- Replace icc_enable/icc_disable with icc_set_bw() vote/unvote
- Merge MAINTAINERS entry into the dt-bindings patch
- Link to v1: https://patch.msgid.link/[email protected]

---
Viken Dadhaniya (2):
dt-bindings: i2c: Add Qualcomm I2C target controller
i2c: qcom-target: Add driver for Qualcomm I2C target controller

 .../bindings/i2c/qcom,qdu1000-i2c-target.yaml      |  75 +++
 MAINTAINERS                                        |   9 +
 drivers/i2c/busses/Kconfig                         |  15 +
 drivers/i2c/busses/Makefile                        |   1 +
 drivers/i2c/busses/i2c-qcom-target.c               | 588 +++++++++++++++++++++
 5 files changed, 688 insertions(+)

---
base-commit: 415606a7be939835db9b0d6b711887586646346d
change-id: 20260628-i2c-qcom-slave-c382ff4e8691

Best regards,

Signature · 1 line
Viken Dadhaniya <[email protected]>
#1 lore raw

PATCH v3 1/2 dt-bindings: i2c: Add Qualcomm I2C target controller

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. It is a distinct IP from the Qualcomm I2C master controllers (GENI, QUP) with a different register interface, so it requires its own binding.

Document the MMIO region, interrupt, XO and AHB clocks, interconnect path, and optional pinctrl states for the controller.

---
 .../bindings/i2c/qcom,qdu1000-i2c-target.yaml      | 75 ++++++++++++++++++++++
 1 file changed, 75 insertions(+)
Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml new file +75 -0
new file mode 100644
index 000000000000..9a6b08f0ab0e
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/i2c/qcom,qdu1000-i2c-target.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm I2C Target Controller
+
+maintainers:
+  - Mukesh Kumar Savaliya <[email protected]>
+  - Viken Dadhaniya <[email protected]>
+
+description:
+  Dedicated hardware IP found on Qualcomm SoCs that operates exclusively
+  as an I2C target (slave) device on the bus. Supports FIFO (PIO) mode
+  for data transfer.
+
+properties:
+  compatible:
+    enum:
+      - qcom,qdu1000-i2c-target
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: XO reference clock
+      - description: AHB bus clock
+
+  clock-names:
+    items:
+      - const: xo
+      - const: ahb
+
+  interconnects:
+    maxItems: 1
+
+  pinctrl-0: true
+  pinctrl-1: true
+
+  pinctrl-names:
+    minItems: 1
+    items:
+      - const: default
+      - const: sleep
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+  - interconnects
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/qcom,qdu1000-gcc.h>
+    #include <dt-bindings/interconnect/qcom,qdu1000-rpmh.h>
+
+    i2c@88ca000 {
+        compatible = "qcom,qdu1000-i2c-target";
+        reg = <0x88ca000 0x64>;
+        clocks = <&gcc GCC_SM_BUS_XO_CLK>, <&gcc GCC_SM_BUS_AHB_CLK>;
+        clock-names = "xo", "ahb";
+        interrupts = <GIC_SPI 358 IRQ_TYPE_LEVEL_HIGH>;
+        interconnects = <&gem_noc MASTER_APPSS_PROC 0 &config_noc SLAVE_SMBUS_CFG 0>;
+    };
+...
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/2] dt-bindings: i2c: Add Qualcomm I2C target controller

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The I2C target controller binding prevents instantiation of I2C slave backend child nodes by…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The I2C target controller binding prevents instantiation of I2C slave backend child nodes by using additionalProperties: false and omitting #address-cells, #size-cells, and a reference to i2c-controller.yaml. --

--- Patch [1]: [PATCH v3 1/2] dt-bindings: i2c: Add Qualcomm I2C target controller --- commit 554673beb1cac58de736368fdfcb491254cb8342
Author: Viken Dadhaniya <[email protected]>

dt-bindings: i2c: Add Qualcomm I2C target controller

This commit adds a device tree binding for the dedicated I2C target controller found on QDU1000 and related Qualcomm SoCs. It documents the MMIO region, interrupts, clocks, interconnects, and pinctrl states.

Quoted 5 lines
Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml new file +0 -0
new file mode 100644
index 0000000000000..9a6b08f0ab0e7

[ ... ]

Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml line 59

Quoted 9 lines
Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml new file +9 -0
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+  - interconnects
+
+additionalProperties: false

[Severity: Medium]
Does this strict additionalProperties: false prevent the instantiation of I2C slave backend child nodes?

Because this rejects properties like #address-cells and #size-cells, any device tree that defines I2C target clients (such as slave-24c02) as child nodes to this controller will fail schema validation.

Should this schema reference i2c-controller.yaml and use
unevaluatedProperties: false instead to allow standard I2C slave backend instantiation?

Quoted 4 lines
Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml new file +4 -0
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] dt-bindings: i2c: Add Qualcomm I2C target controller

In reply to Viken Dadhaniya
Quoted 5 lines · Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. It is a distinct IP from the Qualcomm I2C master controllers (GENI, QUP) with a different register interface, so it requires its own binding.

Quoted 8 lines

Document the MMIO region, interrupt, XO and AHB clocks, interconnect path, and optional pinctrl states for the controller.

---
 .../bindings/i2c/qcom,qdu1000-i2c-target.yaml      | 75 ++++++++++++++++++++++
 1 file changed, 75 insertions(+)

I think comments of Sashiko are wrong.

Best regards,
Krzysztof

#4 lore raw

PATCH v3 2/2 i2c: qcom-target: Add driver for Qualcomm I2C target controller

In reply to Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. The existing Qualcomm I2C controller drivers (GENI, QUP) are master-only and cannot serve systems where the SoC must respond as an I2C target on the bus.

Register the controller with the Linux I2C slave framework via i2c_algorithm.reg_target and i2c_algorithm.unreg_target so that any standard slave backend (e.g. slave-24c02) can be attached at runtime via i2c_slave_register(). Handle IRQ events for RX FIFO service, clock stretching during read and write phases, STOP and repeated-start conditions, and error recovery with SW reset. Enable the required AHB and XO clocks, vote for interconnect bandwidth, and restore hardware state across suspend and resume using the noirq PM callbacks.

---
 MAINTAINERS                          |   9 +
 drivers/i2c/busses/Kconfig           |  15 +
 drivers/i2c/busses/Makefile          |   1 +
 drivers/i2c/busses/i2c-qcom-target.c | 588 +++++++++++++++++++++++++++++++++++
 4 files changed, 613 insertions(+)
MAINTAINERS +9 -0
index fe67f7bfa44c..7a2d66385e36 100644
@@ -22380,6 +22380,15 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/i2c/qcom,i2c-geni-qcom.yaml
 F:	drivers/i2c/busses/i2c-qcom-geni.c
 
+QUALCOMM I2C TARGET CONTROLLER DRIVER
+M:	Viken Dadhaniya <[email protected]>
+M:	Mukesh Kumar Savaliya <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml
+F:	drivers/i2c/busses/i2c-qcom-target.c
+
 QUALCOMM I2C CCI DRIVER
 M:	Loic Poulain <[email protected]>
 M:	Robert Foss <[email protected]>
drivers/i2c/busses/Kconfig +15 -0
index d7b89508311f..2e18238e0ae6 100644
@@ -1070,6 +1070,21 @@ config I2C_QCOM_GENI
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-qcom-geni.
 
+config I2C_QCOM_TARGET
+	tristate "Qualcomm I2C target controller"
+	depends on ARCH_QCOM || COMPILE_TEST
+	depends on COMMON_CLK
+	depends on INTERCONNECT
+	select I2C_SLAVE
+	help
+	  This driver supports I2C target mode on Qualcomm Technologies
+	  SoCs. If you say yes to this option, support will be included
+	  for the built-in I2C target controller on QDU1000 and other
+	  compatible Qualcomm SoCs.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-qcom-target.
+
 config I2C_QUP
 	tristate "Qualcomm QUP based I2C controller"
 	depends on ARCH_QCOM || COMPILE_TEST
drivers/i2c/busses/Makefile +1 -0
index 3755c54b3d82..ab3070cdb144 100644
@@ -101,6 +101,7 @@ obj-$(CONFIG_I2C_PXA)		+= i2c-pxa.o
 obj-$(CONFIG_I2C_PXA_PCI)	+= i2c-pxa-pci.o
 obj-$(CONFIG_I2C_QCOM_CCI)	+= i2c-qcom-cci.o
 obj-$(CONFIG_I2C_QCOM_GENI)	+= i2c-qcom-geni.o
+obj-$(CONFIG_I2C_QCOM_TARGET)	+= i2c-qcom-target.o
 obj-$(CONFIG_I2C_QUP)		+= i2c-qup.o
 obj-$(CONFIG_I2C_RIIC)		+= i2c-riic.o
 obj-$(CONFIG_I2C_RK3X)		+= i2c-rk3x.o
drivers/i2c/busses/i2c-qcom-target.c new file +588 -0

588 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Calling usleep_range() from the hardirq handler will cause a 'scheduling while atomic' kernel…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Calling usleep_range() from the hardirq handler will cause a 'scheduling while atomic' kernel panic.
- [High] Risk of synchronous external abort due to clocks being disabled before the IRQ is freed in the remove path.
- [High] The driver calls sleeping functions (clock APIs and icc_set_bw) from a noirq power management context.
- [High] Premature clock stretch release during a Repeated Start condition.
- [Medium] Missing cleanup of clocks and interconnect bandwidth vote on probe error paths.
--

--- Patch [2]: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller --- commit 1029beb1e1d38e9f7984ad5331b9a78f18027431
Author: Viken Dadhaniya <[email protected]>
Subject: i2c: qcom-target: Add driver for Qualcomm I2C target controller

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. This patch registers the controller with the Linux I2C slave framework and handles IRQ events, clocks, and power management.

Quoted 5 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
new file mode 100644
index 0000000000000..277ea944eedb6

[ ... ]

drivers/i2c/busses/i2c-qcom-target.c static int qcom_i2c_target_drain_rx_fifo(struct qcom_i2c_target *target) · line 198

Quoted 13 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
@@ -187,15 +187,15 @@ static int qcom_i2c_target_drain_rx_fifo(struct qcom_i2c_target *target)
 static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
 {
 	/* Clear error bits before SW_RESET; the reset may not be instantaneous */
 	writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
 	       target->base + I2C_S_IRQ_CLR);
 	writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);
 	/*
 	 * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
 	 * Use a conservative delay to allow the reset to finish before
 	 * reconfiguring the controller.
 	 */
 	usleep_range(10, 20);

[Severity: High]
Does calling usleep_range() here cause a scheduling while atomic kernel panic?

qcom_i2c_target_hw_reset() is called from qcom_i2c_target_handle_error(), which is invoked directly from the hardirq handler qcom_i2c_target_irq() when an error condition or clock low timeout occurs.

Quoted 4 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
 	qcom_i2c_target_hw_init(target);
 	writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
 	writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);

}

[ ... ]

Quoted 6 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
@@ -279,15 +279,15 @@ static irqreturn_t qcom_i2c_target_irq(int irq, void *dev)
 	if (!READ_ONCE(target->slave)) {
 		writel(irq_stat, target->base + I2C_S_IRQ_CLR);
 		return IRQ_HANDLED;
 	}
 

drivers/i2c/busses/i2c-qcom-target.c static irqreturn_t qcom_i2c_target_irq(int irq, void *dev) · line 293

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
 	if (irq_stat & (BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT)))
 		return qcom_i2c_target_handle_error(target, irq_stat);
 
 	if (irq_stat & BIT(STOP_DETECTED))
 		return qcom_i2c_target_handle_stop(target, irq_stat);
 
 	if (irq_stat & BIT(RESTART_DETECTED)) {
 		dev_dbg(target->dev, "Repeated start bit detected\n");
 		target->status = 0;
 		writel(ACK_RESUME, target->base + I2C_S_CONTROL);

[Severity: High]
If a repeated start occurs and STRCH_RD is co-asserted, does writing ACK_RESUME here release the clock stretch prematurely?

Releasing it here would happen before the STRCH_RD handler below has a chance to write the required payload to I2C_S_TX_FIFO, potentially corrupting the data sent to the I2C master.

Quoted 5 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
 		writel(BIT(RESTART_DETECTED), target->base + I2C_S_IRQ_CLR);
 	}
 
 	if (irq_stat & BIT(STRCH_RD))
 		qcom_i2c_target_handle_strch_rd(target);

[ ... ]

Quoted 16 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
@@ -482,15 +482,15 @@ static int qcom_i2c_target_probe(struct platform_device *pdev)
 	target->ahb_clk = devm_clk_get(dev, "ahb");
 	if (IS_ERR(target->ahb_clk))
 		return dev_err_probe(dev, PTR_ERR(target->ahb_clk),
 				     "failed to get AHB clock\n");
 
 	ret = clk_prepare_enable(target->xo_clk);
 	if (ret)
 		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
 
 	ret = clk_prepare_enable(target->ahb_clk);
 	if (ret) {
 		clk_disable_unprepare(target->xo_clk);
 		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
 	}
 

drivers/i2c/busses/i2c-qcom-target.c

Quoted 3 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
 	target->irq = platform_get_irq(pdev, 0);
 	if (target->irq < 0)
 		return target->irq;

[Severity: Medium]
Are xo_clk and ahb_clk leaked here if platform_get_irq() fails?

It appears clk_disable_unprepare() is missing for both clocks in this error path, as well as in the subsequent qcom_i2c_target_icc_init() and devm_request_irq() error paths.

Quoted 4 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
 	ret = qcom_i2c_target_icc_init(target);
 	if (ret)
 		return ret;

[ ... ]

drivers/i2c/busses/i2c-qcom-target.c static void qcom_i2c_target_remove(struct platform_device *pdev) · line 518

Quoted 9 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
@@ -511,11 +511,11 @@ static void qcom_i2c_target_remove(struct platform_device *pdev)
 {
 	struct qcom_i2c_target *target = platform_get_drvdata(pdev);
 
 	writel(0, target->base + I2C_S_CONFIG);
 	i2c_del_adapter(&target->adap);
 	icc_set_bw(target->icc_path, 0, 0);
 	clk_disable_unprepare(target->xo_clk);
 	clk_disable_unprepare(target->ahb_clk);

[Severity: High]
Since the IRQ is devm-managed, can it fire after the clocks are disabled here?

If the interrupt fires during this window, the handler will read I2C_S_IRQ_STATUS while the clocks are off, which could cause a synchronous external abort.

Quoted 3 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
 }
 
 static int qcom_i2c_target_suspend(struct device *dev)

[ ... ]

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +0 -0
@@ -561,11 +561,11 @@ static int qcom_i2c_target_resume(struct device *dev)
 	return 0;
 
 err_disable_xo:
 	clk_disable_unprepare(target->xo_clk);
 err_disable_ahb:
 	clk_disable_unprepare(target->ahb_clk);
 	return ret;
 }
 
 static const struct dev_pm_ops qcom_i2c_target_pm_ops = {
 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_i2c_target_suspend,
Quoted 1 line

qcom_i2c_target_resume)

[Severity: High]
Does using the noirq PM macros here cause a scheduling while atomic panic during suspend and resume?

SET_NOIRQ_SYSTEM_SLEEP_PM_OPS binds the callbacks to the suspend_noirq and resume_noirq phases, which run with local interrupts disabled. However, qcom_i2c_target_suspend() and qcom_i2c_target_resume() both call clk_disable_unprepare() or clk_prepare_enable(), and icc_set_bw(), which can sleep.

Quoted 3 lines

};

static const struct of_device_id qcom_i2c_target_dt_match[] = {

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

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

In reply to Viken Dadhaniya
Quoted 4 lines · Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. The existing Qualcomm I2C controller drivers (GENI, QUP) are master-only and cannot serve systems where the SoC must respond as an I2C target on the bus.

[...]

drivers/i2c/busses/i2c-qcom-target.c line 471

Quoted 9 lines
drivers/i2c/busses/i2c-qcom-target.c new file +9 -0
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		clk_disable_unprepare(target->xo_clk);
+		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+	}

Does this need an OPP table? Presumably we need a power vote for each bus frequency

Konrad

#7 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Quoted 8 lines · Konrad Dybcio
Quoted 4 lines · Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. The existing Qualcomm I2C controller drivers (GENI, QUP) are master-only and cannot serve systems where the SoC must respond as an I2C target on the bus.

[...]

Quoted 12 lines

drivers/i2c/busses/i2c-qcom-target.c line 471

Quoted 9 lines
drivers/i2c/busses/i2c-qcom-target.c new file +9 -0
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		clk_disable_unprepare(target->xo_clk);
+		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+	}

Does this need an OPP table? Presumably we need a power vote for each bus frequency

The xo and ahb are simple gate clocks with no rate states — xo runs at a fixed 19.2 MHz and the ahb rate is fabric-managed. Since this is a target-only controller the master drives bus speed, so there is nothing for us to scale. OPP is not needed here

Quoted 2 lines

Konrad

#8 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Quoted 24 lines · Viken Dadhaniya
Quoted 8 lines · Konrad Dybcio
Quoted 4 lines · Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. The existing Qualcomm I2C controller drivers (GENI, QUP) are master-only and cannot serve systems where the SoC must respond as an I2C target on the bus.

[...]

Quoted 12 lines

drivers/i2c/busses/i2c-qcom-target.c line 471

Quoted 9 lines
drivers/i2c/busses/i2c-qcom-target.c new file +9 -0
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		clk_disable_unprepare(target->xo_clk);
+		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+	}

Does this need an OPP table? Presumably we need a power vote for each bus frequency

Quoted 4 lines

The xo and ahb are simple gate clocks with no rate states — xo runs at a fixed 19.2 MHz and the ahb rate is fabric-managed. Since this is a target-only controller the master drives bus speed, so there is nothing for us to scale. OPP is not needed here

OK, thanks for confirming.

Konrad

maintainer
#9 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

In reply to Viken Dadhaniya

Hi Viken,

just a quick look here.

...

drivers/i2c/busses/i2c-qcom-target.c line 197

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +12 -0
+static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
+{
+	/* Clear error bits before SW_RESET; the reset may not be instantaneous */
+	writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
+	       target->base + I2C_S_IRQ_CLR);
+	writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);
+	/*
+	 * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
+	 * Use a conservative delay to allow the reset to finish before
+	 * reconfiguring the controller.
+	 */
+	usleep_range(10, 20);

This is also called in atomic context from the irq handler. Please avoid using usleep_range(), perhaps you can put this in a thread.

Quoted 20 lines
drivers/i2c/busses/i2c-qcom-target.c new file +20 -0
+	qcom_i2c_target_hw_init(target);
+	writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
+	writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+}
+
+static irqreturn_t qcom_i2c_target_handle_error(struct qcom_i2c_target *target,
+						u32 irq_stat)
+{
+	u8 val = 0;
+
+	if (irq_stat & BIT(ERR_CONDITION))
+		dev_err(target->dev, "Error condition: unexpected Start/Stop bits\n");
+	else
+		dev_err(target->dev, "Clock low timeout\n");
+	qcom_i2c_target_dump_regs(target);
+	qcom_i2c_target_hw_reset(target);
+	i2c_slave_event(target->slave, I2C_SLAVE_STOP, &val);
+	target->status = 0;
+	return IRQ_HANDLED;
+}

...

Quoted 39 lines
drivers/i2c/busses/i2c-qcom-target.c new file +39 -0
+static irqreturn_t qcom_i2c_target_irq(int irq, void *dev)
+{
+	struct qcom_i2c_target *target = dev;
+	u32 irq_stat, rx_bits;
+
+	/*
+	 * Dispatch priority (highest first):
+	 *   ERR_CONDITION / CLOCK_LOW_TIMEOUT  — hardware error, triggers SW reset
+	 *   STOP_DETECTED                      — end of transaction, clears all state
+	 *   RESTART_DETECTED                   — repeated start, resets state before
+	 *                                        any data phase in the same snapshot
+	 *   STRCH_RD                           — read-phase data supply
+	 *   RX_FIFO_FULL / RX_DATA_AVAIL /
+	 *   STRCH_WR                           — write-phase Rx, coalesced into one drain
+	 */
+	irq_stat = readl_relaxed(target->base + I2C_S_IRQ_STATUS);
+	if (!irq_stat)
+		return IRQ_NONE;
+
+	dev_dbg(target->dev, "IRQ status: 0x%x\n", irq_stat);
+
+	/*
+	 * Load target->slave once. Both reg_slave() and unreg_slave() disable
+	 * the IRQ before writing the pointer, so it cannot change while this
+	 * handler runs. Sub-handlers may dereference target->slave directly.
+	 *
+	 * The core is enabled only in reg_slave() and disabled in unreg_slave(),
+	 * so no bus activity is expected here. Clear and discard any stale IRQ.
+	 */
+	if (!READ_ONCE(target->slave)) {
+		writel(irq_stat, target->base + I2C_S_IRQ_CLR);
+		return IRQ_HANDLED;
+	}
+
+	if (irq_stat & (BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT)))
+		return qcom_i2c_target_handle_error(target, irq_stat);
+
+	if (irq_stat & BIT(STOP_DETECTED))
+		return qcom_i2c_target_handle_stop(target, irq_stat);

...

Quoted 9 lines
drivers/i2c/busses/i2c-qcom-target.c new file +9 -0
+	target->xo_clk = devm_clk_get(dev, "xo");
+	if (IS_ERR(target->xo_clk))
+		return dev_err_probe(dev, PTR_ERR(target->xo_clk),
+				     "failed to get XO clock\n");
+
+	target->ahb_clk = devm_clk_get(dev, "ahb");
+	if (IS_ERR(target->ahb_clk))
+		return dev_err_probe(dev, PTR_ERR(target->ahb_clk),
+				     "failed to get AHB clock\n");

drivers/i2c/busses/i2c-qcom-target.c line 471

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		clk_disable_unprepare(target->xo_clk);
+		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+	}

The problem is that these clocks are not disabled in the returns below.

Thanks,
Andi

Quoted 25 lines
drivers/i2c/busses/i2c-qcom-target.c new file +25 -0
+
+	target->irq = platform_get_irq(pdev, 0);
+	if (target->irq < 0)
+		return target->irq;
+
+	ret = qcom_i2c_target_icc_init(target);
+	if (ret)
+		return ret;
+
+	ret = devm_request_irq(dev, target->irq, qcom_i2c_target_irq, 0,
+			       dev_name(dev), target);
+	if (ret)
+		return dev_err_probe(dev, ret, "request_irq failed for IRQ %d\n",
+				     target->irq);
+
+	qcom_i2c_target_hw_init(target);
+
+	platform_set_drvdata(pdev, target);
+
+	ret = qcom_i2c_target_adap_init(target);
+	if (ret)
+		return dev_err_probe(dev, ret, "i2c_add_adapter failed\n");
+
+	return 0;
+}
#10 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Quoted 6 lines · Andi Shyti

Hi Viken,

just a quick look here.

...

Quoted 17 lines

drivers/i2c/busses/i2c-qcom-target.c line 197

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +12 -0
+static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
+{
+	/* Clear error bits before SW_RESET; the reset may not be instantaneous */
+	writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
+	       target->base + I2C_S_IRQ_CLR);
+	writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);
+	/*
+	 * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
+	 * Use a conservative delay to allow the reset to finish before
+	 * reconfiguring the controller.
+	 */
+	usleep_range(10, 20);

This is also called in atomic context from the irq handler. Please avoid using usleep_range(), perhaps you can put this in a thread.

Thanks for the review. The delay is only 20 µs, so we were thinking of replacing usleep_range() with udelay() instead of converting to a threaded IRQ, to avoid the added complexity on all the normal fast paths.

Quoted 79 lines
Quoted 20 lines
drivers/i2c/busses/i2c-qcom-target.c new file +20 -0
+	qcom_i2c_target_hw_init(target);
+	writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
+	writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+}
+
+static irqreturn_t qcom_i2c_target_handle_error(struct qcom_i2c_target *target,
+						u32 irq_stat)
+{
+	u8 val = 0;
+
+	if (irq_stat & BIT(ERR_CONDITION))
+		dev_err(target->dev, "Error condition: unexpected Start/Stop bits\n");
+	else
+		dev_err(target->dev, "Clock low timeout\n");
+	qcom_i2c_target_dump_regs(target);
+	qcom_i2c_target_hw_reset(target);
+	i2c_slave_event(target->slave, I2C_SLAVE_STOP, &val);
+	target->status = 0;
+	return IRQ_HANDLED;
+}

...

Quoted 39 lines
drivers/i2c/busses/i2c-qcom-target.c new file +39 -0
+static irqreturn_t qcom_i2c_target_irq(int irq, void *dev)
+{
+	struct qcom_i2c_target *target = dev;
+	u32 irq_stat, rx_bits;
+
+	/*
+	 * Dispatch priority (highest first):
+	 *   ERR_CONDITION / CLOCK_LOW_TIMEOUT  — hardware error, triggers SW reset
+	 *   STOP_DETECTED                      — end of transaction, clears all state
+	 *   RESTART_DETECTED                   — repeated start, resets state before
+	 *                                        any data phase in the same snapshot
+	 *   STRCH_RD                           — read-phase data supply
+	 *   RX_FIFO_FULL / RX_DATA_AVAIL /
+	 *   STRCH_WR                           — write-phase Rx, coalesced into one drain
+	 */
+	irq_stat = readl_relaxed(target->base + I2C_S_IRQ_STATUS);
+	if (!irq_stat)
+		return IRQ_NONE;
+
+	dev_dbg(target->dev, "IRQ status: 0x%x\n", irq_stat);
+
+	/*
+	 * Load target->slave once. Both reg_slave() and unreg_slave() disable
+	 * the IRQ before writing the pointer, so it cannot change while this
+	 * handler runs. Sub-handlers may dereference target->slave directly.
+	 *
+	 * The core is enabled only in reg_slave() and disabled in unreg_slave(),
+	 * so no bus activity is expected here. Clear and discard any stale IRQ.
+	 */
+	if (!READ_ONCE(target->slave)) {
+		writel(irq_stat, target->base + I2C_S_IRQ_CLR);
+		return IRQ_HANDLED;
+	}
+
+	if (irq_stat & (BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT)))
+		return qcom_i2c_target_handle_error(target, irq_stat);
+
+	if (irq_stat & BIT(STOP_DETECTED))
+		return qcom_i2c_target_handle_stop(target, irq_stat);

...

Quoted 14 lines
drivers/i2c/busses/i2c-qcom-target.c new file +14 -0
+	target->xo_clk = devm_clk_get(dev, "xo");
+	if (IS_ERR(target->xo_clk))
+		return dev_err_probe(dev, PTR_ERR(target->xo_clk),
+				     "failed to get XO clock\n");
+
+	target->ahb_clk = devm_clk_get(dev, "ahb");
+	if (IS_ERR(target->ahb_clk))
+		return dev_err_probe(dev, PTR_ERR(target->ahb_clk),
+				     "failed to get AHB clock\n");
+
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
Quoted 8 lines

drivers/i2c/busses/i2c-qcom-target.c line 471

Quoted 5 lines
drivers/i2c/busses/i2c-qcom-target.c new file +5 -0
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		clk_disable_unprepare(target->xo_clk);
+		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+	}

The problem is that these clocks are not disabled in the returns below.

Sure, I will add the missing clock cleanup paths in the next revision.

Quoted 29 lines

Thanks,
Andi

Quoted 25 lines
drivers/i2c/busses/i2c-qcom-target.c new file +25 -0
+
+	target->irq = platform_get_irq(pdev, 0);
+	if (target->irq < 0)
+		return target->irq;
+
+	ret = qcom_i2c_target_icc_init(target);
+	if (ret)
+		return ret;
+
+	ret = devm_request_irq(dev, target->irq, qcom_i2c_target_irq, 0,
+			       dev_name(dev), target);
+	if (ret)
+		return dev_err_probe(dev, ret, "request_irq failed for IRQ %d\n",
+				     target->irq);
+
+	qcom_i2c_target_hw_init(target);
+
+	platform_set_drvdata(pdev, target);
+
+	ret = qcom_i2c_target_adap_init(target);
+	if (ret)
+		return dev_err_probe(dev, ret, "i2c_add_adapter failed\n");
+
+	return 0;
+}
maintainer
#11 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Hi Viken,

Quoted 21 lines · Viken Dadhaniya
Quoted 19 lines · Andi Shyti

...

drivers/i2c/busses/i2c-qcom-target.c line 197

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +12 -0
+static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
+{
+	/* Clear error bits before SW_RESET; the reset may not be instantaneous */
+	writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
+	       target->base + I2C_S_IRQ_CLR);
+	writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);
+	/*
+	 * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
+	 * Use a conservative delay to allow the reset to finish before
+	 * reconfiguring the controller.
+	 */
+	usleep_range(10, 20);

This is also called in atomic context from the irq handler. Please avoid using usleep_range(), perhaps you can put this in a thread.

Quoted 4 lines

Thanks for the review. The delay is only 20 µs, so we were thinking of replacing usleep_range() with udelay() instead of converting to a threaded IRQ, to avoid the added complexity on all the normal fast paths.

perhaps before sleeping you could check the context you are executing (with an explaining comment).

Thanks,
Andi

#12 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Quoted 24 lines · Andi Shyti

Hi Viken,

Quoted 21 lines · Viken Dadhaniya
Quoted 19 lines · Andi Shyti

...

drivers/i2c/busses/i2c-qcom-target.c line 197

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +12 -0
+static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
+{
+	/* Clear error bits before SW_RESET; the reset may not be instantaneous */
+	writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
+	       target->base + I2C_S_IRQ_CLR);
+	writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);
+	/*
+	 * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
+	 * Use a conservative delay to allow the reset to finish before
+	 * reconfiguring the controller.
+	 */
+	usleep_range(10, 20);

This is also called in atomic context from the irq handler. Please avoid using usleep_range(), perhaps you can put this in a thread.

Quoted 7 lines
Quoted 4 lines

Thanks for the review. The delay is only 20 µs, so we were thinking of replacing usleep_range() with udelay() instead of converting to a threaded IRQ, to avoid the added complexity on all the normal fast paths.

perhaps before sleeping you could check the context you are executing (with an explaining comment).

This helper is reached from the hard IRQ error handler, so it must remain atomic-safe regardless of the current context. Since the reset wait is only 20 µs, I will replace usleep_range() with udelay() and add a comment explaining why a non-sleeping delay is required.

Quoted 3 lines

Thanks,
Andi

#13 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

In reply to Viken Dadhaniya
Quoted 5 lines · Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. The existing Qualcomm I2C controller drivers (GENI, QUP) are master-only and cannot serve systems where the SoC must respond as an I2C target on the bus.

Quoted 8 lines

Register the controller with the Linux I2C slave framework via i2c_algorithm.reg_target and i2c_algorithm.unreg_target so that any standard slave backend (e.g. slave-24c02) can be attached at runtime via i2c_slave_register(). Handle IRQ events for RX FIFO service, clock stretching during read and write phases, STOP and repeated-start conditions, and error recovery with SW reset. Enable the required AHB and XO clocks, vote for interconnect bandwidth, and restore hardware state across suspend and resume using the noirq PM callbacks.

Neat:
Though second paragraph gives good information, but i would suggest to keep new lines to make it easier and simpler.>

Quoted 253 lines
---
  MAINTAINERS                          |   9 +
  drivers/i2c/busses/Kconfig           |  15 +
  drivers/i2c/busses/Makefile          |   1 +
  drivers/i2c/busses/i2c-qcom-target.c | 588 +++++++++++++++++++++++++++++++++++
  4 files changed, 613 insertions(+)
MAINTAINERS +9 -0
index fe67f7bfa44c..7a2d66385e36 100644
@@ -22380,6 +22380,15 @@ S:	Maintained
  F:	Documentation/devicetree/bindings/i2c/qcom,i2c-geni-qcom.yaml
  F:	drivers/i2c/busses/i2c-qcom-geni.c
 
+QUALCOMM I2C TARGET CONTROLLER DRIVER
+M:	Viken Dadhaniya <[email protected]>
+M:	Mukesh Kumar Savaliya <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml
+F:	drivers/i2c/busses/i2c-qcom-target.c
+
  QUALCOMM I2C CCI DRIVER
  M:	Loic Poulain <[email protected]>
  M:	Robert Foss <[email protected]>
drivers/i2c/busses/Kconfig +15 -0
index d7b89508311f..2e18238e0ae6 100644
@@ -1070,6 +1070,21 @@ config I2C_QCOM_GENI
  	  This driver can also be built as a module.  If so, the module
  	  will be called i2c-qcom-geni.
 
+config I2C_QCOM_TARGET
+	tristate "Qualcomm I2C target controller"
+	depends on ARCH_QCOM || COMPILE_TEST
+	depends on COMMON_CLK
+	depends on INTERCONNECT
+	select I2C_SLAVE
+	help
+	  This driver supports I2C target mode on Qualcomm Technologies
+	  SoCs. If you say yes to this option, support will be included
+	  for the built-in I2C target controller on QDU1000 and other
+	  compatible Qualcomm SoCs.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-qcom-target.
+
  config I2C_QUP
  	tristate "Qualcomm QUP based I2C controller"
  	depends on ARCH_QCOM || COMPILE_TEST
drivers/i2c/busses/Makefile +1 -0
index 3755c54b3d82..ab3070cdb144 100644
@@ -101,6 +101,7 @@ obj-$(CONFIG_I2C_PXA)		+= i2c-pxa.o
  obj-$(CONFIG_I2C_PXA_PCI)	+= i2c-pxa-pci.o
  obj-$(CONFIG_I2C_QCOM_CCI)	+= i2c-qcom-cci.o
  obj-$(CONFIG_I2C_QCOM_GENI)	+= i2c-qcom-geni.o
+obj-$(CONFIG_I2C_QCOM_TARGET)	+= i2c-qcom-target.o
  obj-$(CONFIG_I2C_QUP)		+= i2c-qup.o
  obj-$(CONFIG_I2C_RIIC)		+= i2c-riic.o
  obj-$(CONFIG_I2C_RK3X)		+= i2c-rk3x.o
drivers/i2c/busses/i2c-qcom-target.c new file +181 -0
new file mode 100644
index 000000000000..277ea944eedb
@@ -0,0 +1,588 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/interconnect.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+/* Register offsets */
+#define I2C_S_DEVICE_ADDR			0x00
+#define I2C_S_IRQ_STATUS			0x08
+#define I2C_S_IRQ_CLR				0x0C
+#define I2C_S_IRQ_EN				0x10
+#define I2C_S_CONFIG				0x18
+#define I2C_S_CONTROL				0x1C
+#define I2C_S_FIFOS_STATUS			0x20
+#define I2C_S_TX_FIFO				0x24
+#define I2C_S_RX_FIFO				0x28
+#define I2C_S_DEBUG_REG1			0x3C
+#define I2C_S_DEBUG_REG2			0x40
+#define I2C_S_SW_RESET_REG			0x4C
+#define I2C_S_CLK_LOW_TIMEOUT			0x50
+#define I2C_S_CLK_RELEASE_DELAY_CNT_VAL	0x54
+#define I2C_S_SDA_HOLD_CNT_VAL			0x58
+
+/* I2C_S_CONFIG register fields */
+#define I2C_S_CORE_EN				BIT(0)
+
+/* I2C_S_CONTROL register fields */
+#define CLEAR_RX_FIFO				BIT(0)
+#define CLEAR_TX_FIFO				BIT(1)
+#define NACK					BIT(2)
+#define ACK_RESUME				BIT(3)
+
+/* I2C_S_SW_RESET_REG register fields */
+#define SW_RESET				BIT(0)
+
+/* I2C_S_FIFOS_STATUS register fields */
+#define RX_FIFO_COUNT_MASK			GENMASK(31, 16)
+
+/* Interconnect bandwidth vote in bytes per second */
+#define APPS_PROC_TO_I2C_TARGET_VOTE		1190000
+
+/**
+ * enum qcom_i2c_target_irq - IRQ bit positions in I2C_S_IRQ_STATUS
+ * @STOP_DETECTED:	I2C stop condition detected on the bus
+ * @RX_FIFO_FULL:	receive FIFO has reached capacity
+ * @TX_FIFO_EMPTY:	transmit FIFO is empty
+ * @RX_DATA_AVAIL:	receive data is available in the RX FIFO
+ * @CLOCK_LOW_TIMEOUT:	SCL held low longer than the configured timeout
+ * @STRCH_WR:		clock stretching during a write (Rx) phase
+ * @STRCH_RD:		clock stretching during a read (Tx) phase
+ * @GCA_DETECTED:	general call address detected (not used)
+ * @ERR_CONDITION:	unexpected start or stop bit detected (error)
+ * @RESTART_DETECTED:	repeated start condition detected
+ */
+enum qcom_i2c_target_irq {
+	STOP_DETECTED,
+	RX_FIFO_FULL,
+	TX_FIFO_EMPTY,
+	RX_DATA_AVAIL,
+	CLOCK_LOW_TIMEOUT,
+	STRCH_WR,
+	STRCH_RD,
+	GCA_DETECTED,
+	ERR_CONDITION,
+	RESTART_DETECTED,
+};
+
+/*
+ * TX_FIFO_EMPTY is excluded: this driver fills the TX FIFO one byte at a
+ * time in response to STRCH_RD (clock-stretch during read phase), so
+ * TX_FIFO_EMPTY adds no value and would double the interrupt rate.
+ * GCA (general call address) is unsupported.
+ */
+#define QCOM_I2C_TARGET_ALL_IRQ	(GENMASK(RESTART_DETECTED, STOP_DETECTED) \
+				 & ~(BIT(GCA_DETECTED) | BIT(TX_FIFO_EMPTY)))
+
+/* Bit indices for the status bitmask, used with set_bit/test_bit/clear_bit */
+enum qcom_i2c_target_status {
+	READ_IN_PROGRESS,
+	WRITE_IN_PROGRESS,
+};
+
+/**
+ * struct qcom_i2c_target - Qualcomm I2C target controller private data
+ * @dev:		driver model device node
+ * @base:		base address of HW registers
+ * @adap:		I2C adapter (slave mode)
+ * @ahb_clk:		AHB bus clock
+ * @xo_clk:		XO reference clock
+ * @icc_path:		interconnect bandwidth path
+ * @slave:		currently registered slave backend client; written only
+ *			under disable_irq() in reg_slave()/unreg_slave() so the
+ *			ISR sees a stable pointer for its entire execution
+ * @status:		bitmask of enum qcom_i2c_target_status flags; must be
+ *			unsigned long for set_bit/test_bit/clear_bit; accessed
+ *			only from the ISR and from reg_slave()/unreg_slave()
+ *			under disable_irq(), so no additional lock is needed
+ * @irq:		interrupt line number
+ */
+struct qcom_i2c_target {
+	struct device		*dev;
+	void __iomem		*base;
+	struct i2c_adapter	adap;
+	struct clk		*ahb_clk;
+	struct clk		*xo_clk;
+	struct icc_path		*icc_path;
+	struct i2c_client	*slave;
+	unsigned long		status;
+	int			irq;
+};
+
+static void qcom_i2c_target_dump_regs(struct qcom_i2c_target *target)
+{
+	dev_dbg(target->dev, "I2C_S_DEVICE_ADDR:               0x%x\n",
+		readl_relaxed(target->base + I2C_S_DEVICE_ADDR));
+	dev_dbg(target->dev, "I2C_S_IRQ_STATUS:                0x%x\n",
+		readl_relaxed(target->base + I2C_S_IRQ_STATUS));
+	dev_dbg(target->dev, "I2C_S_CONFIG:                    0x%x\n",
+		readl_relaxed(target->base + I2C_S_CONFIG));
+	dev_dbg(target->dev, "I2C_S_IRQ_EN:                    0x%x\n",
+		readl_relaxed(target->base + I2C_S_IRQ_EN));
+	dev_dbg(target->dev, "I2C_S_FIFOS_STATUS:              0x%x\n",
+		readl_relaxed(target->base + I2C_S_FIFOS_STATUS));
+	dev_dbg(target->dev, "I2C_S_DEBUG_REG1:                0x%x\n",
+		readl_relaxed(target->base + I2C_S_DEBUG_REG1));
+	dev_dbg(target->dev, "I2C_S_DEBUG_REG2:                0x%x\n",
+		readl_relaxed(target->base + I2C_S_DEBUG_REG2));
+	dev_dbg(target->dev, "I2C_S_CLK_LOW_TIMEOUT:           0x%x\n",
+		readl_relaxed(target->base + I2C_S_CLK_LOW_TIMEOUT));
+	dev_dbg(target->dev, "I2C_S_CLK_RELEASE_DELAY_CNT_VAL: 0x%x\n",
+		readl_relaxed(target->base + I2C_S_CLK_RELEASE_DELAY_CNT_VAL));
+	dev_dbg(target->dev, "I2C_S_SDA_HOLD_CNT_VAL:          0x%x\n",
+		readl_relaxed(target->base + I2C_S_SDA_HOLD_CNT_VAL));
+}
+
+static void qcom_i2c_target_hw_init(struct qcom_i2c_target *target)
+{
+	dev_dbg(target->dev, "HW init: resetting FIFOs, enabling IRQs\n");
+	writel(CLEAR_TX_FIFO | CLEAR_RX_FIFO, target->base + I2C_S_CONTROL);
+	writel(QCOM_I2C_TARGET_ALL_IRQ, target->base + I2C_S_IRQ_EN);
+}
+
+static void qcom_i2c_target_write_requested(struct qcom_i2c_target *target)
+{
+	u8 val = 0;
+
+	if (!test_and_set_bit(WRITE_IN_PROGRESS, &target->status)) {
+		dev_dbg(target->dev, "Write phase started\n");
+		i2c_slave_event(target->slave, I2C_SLAVE_WRITE_REQUESTED, &val);
+	}
+}
+
+static int qcom_i2c_target_drain_rx_fifo(struct qcom_i2c_target *target)
+{
+	unsigned int rx_count;
+	int ret = 0;
+	u8 val;
+
+	while (!ret) {
+		rx_count = FIELD_GET(RX_FIFO_COUNT_MASK,
+				     readl_relaxed(target->base + I2C_S_FIFOS_STATUS));
+		if (!rx_count)
+			break;
+
+		while (rx_count--) {
+			val = (u8)readl_relaxed(target->base + I2C_S_RX_FIFO);
+			dev_dbg(target->dev, "Data from RX FIFO: 0x%x\n", val);
+			ret = i2c_slave_event(target->slave, I2C_SLAVE_WRITE_RECEIVED, &val);
+			if (ret)
+				break;
+		}
+	}

drivers/i2c/busses/i2c-qcom-target.c line 191

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+
+	return ret;
+}
+
+static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
+{
+	/* Clear error bits before SW_RESET; the reset may not be instantaneous */
+	writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
+	       target->base + I2C_S_IRQ_CLR);
+	writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);

line space > + /*

Quoted 13 lines
drivers/i2c/busses/i2c-qcom-target.c new file +13 -0
+	 * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
+	 * Use a conservative delay to allow the reset to finish before
+	 * reconfiguring the controller.
+	 */
+	usleep_range(10, 20);
+	qcom_i2c_target_hw_init(target);
+	writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
+	writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+}
+
+static irqreturn_t qcom_i2c_target_handle_error(struct qcom_i2c_target *target,
+						u32 irq_stat)
+{

drivers/i2c/busses/i2c-qcom-target.c line 215

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+	u8 val = 0;
+
+	if (irq_stat & BIT(ERR_CONDITION))
+		dev_err(target->dev, "Error condition: unexpected Start/Stop bits\n");
+	else
+		dev_err(target->dev, "Clock low timeout\n");
+	qcom_i2c_target_dump_regs(target);
+	qcom_i2c_target_hw_reset(target);
+	i2c_slave_event(target->slave, I2C_SLAVE_STOP, &val);
+	target->status = 0;

line space before return> + return IRQ_HANDLED;

Quoted 14 lines
drivers/i2c/busses/i2c-qcom-target.c new file +14 -0
+}
+
+static irqreturn_t qcom_i2c_target_handle_stop(struct qcom_i2c_target *target,
+					       u32 irq_stat)
+{
+	u8 val = 0;
+
+	dev_dbg(target->dev, "Stop bit detected\n");
+	/*
+	 * Short-write corner case: WRITE_IN_PROGRESS was never set because
+	 * no RX_DATA_AVAIL or STRCH_WR fired before STOP. Do not call
+	 * qcom_i2c_target_write_requested() here — STOP ends the transaction
+	 * so setting WRITE_IN_PROGRESS now would leave a stale bit after
+	 * target->status is cleared below.

drivers/i2c/busses/i2c-qcom-target.c line 240

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+	 */
+	if (!test_bit(WRITE_IN_PROGRESS, &target->status)) {
+		unsigned int rx_count;
+
+		rx_count = FIELD_GET(RX_FIFO_COUNT_MASK,
+				     readl_relaxed(target->base + I2C_S_FIFOS_STATUS));
+		if (rx_count)
+			i2c_slave_event(target->slave, I2C_SLAVE_WRITE_REQUESTED,
+					&val);
+	}

minor as cleanup: give a line space > + qcom_i2c_target_drain_rx_fifo(target);

drivers/i2c/busses/i2c-qcom-target.c line 254

Quoted 13 lines
drivers/i2c/busses/i2c-qcom-target.c new file +13 -0
+	i2c_slave_event(target->slave, I2C_SLAVE_STOP, &val);
+	target->status = 0;
+	writel(CLEAR_RX_FIFO, target->base + I2C_S_CONTROL);
+	/*
+	 * STOP terminates the transaction, so any other Rx or clock
+	 * stretch bits latched in this same status read have already
+	 * been serviced by the drain above or are now stale. Ack the
+	 * whole word rather than just STOP_DETECTED; clearing only STOP
+	 * would leave those bits set and immediately re-enter the
+	 * handler, which for a co-asserted STRCH_RD would push a stale
+	 * byte into the TX FIFO.
+	 */
+	writel(irq_stat, target->base + I2C_S_IRQ_CLR);

give a line space before returning.> + return IRQ_HANDLED;

drivers/i2c/busses/i2c-qcom-target.c line 267

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +12 -0
+}
+
+static void qcom_i2c_target_handle_rx_data(struct qcom_i2c_target *target,
+					   u32 rx_irq_bits)
+{
+	int ret;
+
+	dev_dbg(target->dev, "Rx data event (rx_irq_bits=0x%x)\n", rx_irq_bits);
+	qcom_i2c_target_write_requested(target);
+	ret = qcom_i2c_target_drain_rx_fifo(target);
+	if (ret)
+		dev_dbg(target->dev, "Backend requested NACK\n");

line space for neat> + writel(ret ? NACK | CLEAR_RX_FIFO : ACK_RESUME,

Quoted 5 lines
drivers/i2c/busses/i2c-qcom-target.c new file +5 -0
+	       target->base + I2C_S_CONTROL);
+	writel(rx_irq_bits, target->base + I2C_S_IRQ_CLR);
+}
+
+static void qcom_i2c_target_handle_strch_rd(struct qcom_i2c_target *target)

drivers/i2c/busses/i2c-qcom-target.c line 283

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+{
+	enum i2c_slave_event event = I2C_SLAVE_READ_PROCESSED;
+	u8 val = 0;
+
+	dev_dbg(target->dev, "Clock stretching during read (Tx) phase\n");
+	if (!test_and_set_bit(READ_IN_PROGRESS, &target->status)) {
+		/* Repeated-start: master switched direction from write to read */
+		clear_bit(WRITE_IN_PROGRESS, &target->status);
+		event = I2C_SLAVE_READ_REQUESTED;
+	}

line space> + i2c_slave_event(target->slave, event, &val);

Quoted 85 lines
drivers/i2c/busses/i2c-qcom-target.c new file +85 -0
+	dev_dbg(target->dev, "Data to TX FIFO: 0x%x\n", val);
+	writel(val, target->base + I2C_S_TX_FIFO);
+	writel(ACK_RESUME, target->base + I2C_S_CONTROL);
+	writel(BIT(STRCH_RD), target->base + I2C_S_IRQ_CLR);
+}
+
+static irqreturn_t qcom_i2c_target_irq(int irq, void *dev)
+{
+	struct qcom_i2c_target *target = dev;
+	u32 irq_stat, rx_bits;
+
+	/*
+	 * Dispatch priority (highest first):
+	 *   ERR_CONDITION / CLOCK_LOW_TIMEOUT  — hardware error, triggers SW reset
+	 *   STOP_DETECTED                      — end of transaction, clears all state
+	 *   RESTART_DETECTED                   — repeated start, resets state before
+	 *                                        any data phase in the same snapshot
+	 *   STRCH_RD                           — read-phase data supply
+	 *   RX_FIFO_FULL / RX_DATA_AVAIL /
+	 *   STRCH_WR                           — write-phase Rx, coalesced into one drain
+	 */
+	irq_stat = readl_relaxed(target->base + I2C_S_IRQ_STATUS);
+	if (!irq_stat)
+		return IRQ_NONE;
+
+	dev_dbg(target->dev, "IRQ status: 0x%x\n", irq_stat);
+
+	/*
+	 * Load target->slave once. Both reg_slave() and unreg_slave() disable
+	 * the IRQ before writing the pointer, so it cannot change while this
+	 * handler runs. Sub-handlers may dereference target->slave directly.
+	 *
+	 * The core is enabled only in reg_slave() and disabled in unreg_slave(),
+	 * so no bus activity is expected here. Clear and discard any stale IRQ.
+	 */
+	if (!READ_ONCE(target->slave)) {
+		writel(irq_stat, target->base + I2C_S_IRQ_CLR);
+		return IRQ_HANDLED;
+	}
+
+	if (irq_stat & (BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT)))
+		return qcom_i2c_target_handle_error(target, irq_stat);
+
+	if (irq_stat & BIT(STOP_DETECTED))
+		return qcom_i2c_target_handle_stop(target, irq_stat);
+
+	if (irq_stat & BIT(RESTART_DETECTED)) {
+		dev_dbg(target->dev, "Repeated start bit detected\n");
+		target->status = 0;
+		writel(ACK_RESUME, target->base + I2C_S_CONTROL);
+		writel(BIT(RESTART_DETECTED), target->base + I2C_S_IRQ_CLR);
+	}
+
+	if (irq_stat & BIT(STRCH_RD))
+		qcom_i2c_target_handle_strch_rd(target);
+
+	/*
+	 * Coalesce all write-phase Rx bits into a single drain+ACK. When the
+	 * RX FIFO fills at threshold, RX_FIFO_FULL, RX_DATA_AVAIL and STRCH_WR
+	 * can all assert in the same irq_stat snapshot. Pass the combined mask
+	 * so ACK_RESUME is written exactly once and all bits are cleared together.
+	 */
+	rx_bits = irq_stat & (BIT(RX_FIFO_FULL) | BIT(RX_DATA_AVAIL) |
+			      BIT(STRCH_WR));
+	if (rx_bits)
+		qcom_i2c_target_handle_rx_data(target, rx_bits);
+
+	return IRQ_HANDLED;
+}
+
+static int qcom_i2c_target_reg_slave(struct i2c_client *slave)
+{
+	struct qcom_i2c_target *target = i2c_get_adapdata(slave->adapter);
+
+	if (target->slave)
+		return -EBUSY;
+
+	if (slave->flags & I2C_CLIENT_TEN)
+		return -EAFNOSUPPORT;
+
+	disable_irq(target->irq);
+	WRITE_ONCE(target->slave, slave);
+	writel(slave->addr, target->base + I2C_S_DEVICE_ADDR);
+	writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+	enable_irq(target->irq);

drivers/i2c/busses/i2c-qcom-target.c line 379

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+
+	return 0;
+}
+
+static int qcom_i2c_target_unreg_slave(struct i2c_client *slave)
+{
+	struct qcom_i2c_target *target = i2c_get_adapdata(slave->adapter);
+
+	if (!target->slave)
+		return -EINVAL;

minor comment: line space > + disable_irq(target->irq);

Quoted 159 lines
drivers/i2c/busses/i2c-qcom-target.c new file +159 -0
+	writel(0, target->base + I2C_S_CONFIG);
+	WRITE_ONCE(target->slave, NULL);
+	target->status = 0;
+	writel(0, target->base + I2C_S_DEVICE_ADDR);
+	enable_irq(target->irq);
+
+	return 0;
+}
+
+static int qcom_i2c_target_icc_init(struct qcom_i2c_target *target)
+{
+	int ret;
+
+	target->icc_path = devm_of_icc_get(target->dev, NULL);
+	if (IS_ERR(target->icc_path))
+		return dev_err_probe(target->dev, PTR_ERR(target->icc_path),
+				     "failed to get ICC path\n");
+
+	/*
+	 * The controller only needs interconnect bandwidth while it is
+	 * powered. The vote is placed here and across resume with
+	 * icc_set_bw(), and dropped with icc_set_bw(path, 0, 0) on suspend
+	 * and remove, so the vote and unvote are always symmetric.
+	 */
+	ret = icc_set_bw(target->icc_path, APPS_PROC_TO_I2C_TARGET_VOTE,
+			 APPS_PROC_TO_I2C_TARGET_VOTE);
+	if (ret)
+		return dev_err_probe(target->dev, ret, "icc_set_bw failed\n");
+
+	return 0;
+}
+
+static u32 qcom_i2c_target_func(struct i2c_adapter *adap)
+{
+	return I2C_FUNC_SLAVE;
+}
+
+static const struct i2c_algorithm qcom_i2c_target_algo = {
+	.reg_target	= qcom_i2c_target_reg_slave,
+	.unreg_target	= qcom_i2c_target_unreg_slave,
+	.functionality	= qcom_i2c_target_func,
+};
+
+static int qcom_i2c_target_adap_init(struct qcom_i2c_target *target)
+{
+	target->adap.algo = &qcom_i2c_target_algo;
+	target->adap.dev.parent = target->dev;
+	target->adap.dev.of_node = target->dev->of_node;
+	strscpy(target->adap.name, "qcom-i2c-target",
+		sizeof(target->adap.name));
+	i2c_set_adapdata(&target->adap, target);
+
+	return i2c_add_adapter(&target->adap);
+}
+
+static int qcom_i2c_target_probe(struct platform_device *pdev)
+{
+	struct qcom_i2c_target *target;
+	struct device *dev = &pdev->dev;
+	int ret;
+
+	target = devm_kzalloc(dev, sizeof(*target), GFP_KERNEL);
+	if (!target)
+		return -ENOMEM;
+
+	target->dev = dev;
+
+	target->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(target->base))
+		return dev_err_probe(dev, PTR_ERR(target->base),
+				     "failed to map registers\n");
+
+	target->xo_clk = devm_clk_get(dev, "xo");
+	if (IS_ERR(target->xo_clk))
+		return dev_err_probe(dev, PTR_ERR(target->xo_clk),
+				     "failed to get XO clock\n");
+
+	target->ahb_clk = devm_clk_get(dev, "ahb");
+	if (IS_ERR(target->ahb_clk))
+		return dev_err_probe(dev, PTR_ERR(target->ahb_clk),
+				     "failed to get AHB clock\n");
+
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		clk_disable_unprepare(target->xo_clk);
+		return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+	}
+
+	target->irq = platform_get_irq(pdev, 0);
+	if (target->irq < 0)
+		return target->irq;
+
+	ret = qcom_i2c_target_icc_init(target);
+	if (ret)
+		return ret;
+
+	ret = devm_request_irq(dev, target->irq, qcom_i2c_target_irq, 0,
+			       dev_name(dev), target);
+	if (ret)
+		return dev_err_probe(dev, ret, "request_irq failed for IRQ %d\n",
+				     target->irq);
+
+	qcom_i2c_target_hw_init(target);
+
+	platform_set_drvdata(pdev, target);
+
+	ret = qcom_i2c_target_adap_init(target);
+	if (ret)
+		return dev_err_probe(dev, ret, "i2c_add_adapter failed\n");
+
+	return 0;
+}
+
+static void qcom_i2c_target_remove(struct platform_device *pdev)
+{
+	struct qcom_i2c_target *target = platform_get_drvdata(pdev);
+
+	writel(0, target->base + I2C_S_CONFIG);
+	i2c_del_adapter(&target->adap);
+	icc_set_bw(target->icc_path, 0, 0);
+	clk_disable_unprepare(target->xo_clk);
+	clk_disable_unprepare(target->ahb_clk);
+}
+
+static int qcom_i2c_target_suspend(struct device *dev)
+{
+	struct qcom_i2c_target *target = dev_get_drvdata(dev);
+	int ret;
+
+	ret = icc_set_bw(target->icc_path, 0, 0);
+	if (ret)
+		dev_err(dev, "icc_set_bw failed on suspend: %d\n", ret);
+
+	clk_disable_unprepare(target->xo_clk);
+	clk_disable_unprepare(target->ahb_clk);
+
+	return 0;
+}
+
+static int qcom_i2c_target_resume(struct device *dev)
+{
+	struct qcom_i2c_target *target = dev_get_drvdata(dev);
+	int ret;
+
+	ret = clk_prepare_enable(target->ahb_clk);
+	if (ret) {
+		dev_err(dev, "failed to enable AHB clock\n");
+		return ret;
+	}
+
+	ret = clk_prepare_enable(target->xo_clk);
+	if (ret) {
+		dev_err(dev, "failed to enable XO clock\n");
+		goto err_disable_ahb;
+	}

drivers/i2c/busses/i2c-qcom-target.c line 549

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+
+	ret = icc_set_bw(target->icc_path, APPS_PROC_TO_I2C_TARGET_VOTE,
+			 APPS_PROC_TO_I2C_TARGET_VOTE);
+	if (ret) {
+		dev_err(dev, "icc_set_bw failed\n");
+		goto err_disable_xo;
+	}
+
+	qcom_i2c_target_hw_init(target);
+	target->status = 0;

if the status is 0 here after target init, what about other places ? does it need to change status ? OR is it inherent to framework ?

Any comment can be help while changing status ?> + if (target->slave) {

drivers/i2c/busses/i2c-qcom-target.c line 560

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+		writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
+		writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+	}
+
+	return 0;
+
+err_disable_xo:
+	clk_disable_unprepare(target->xo_clk);
+err_disable_ahb:
+	clk_disable_unprepare(target->ahb_clk);

line space before return> + return ret;

drivers/i2c/busses/i2c-qcom-target.c line 568

Quoted 7 lines
drivers/i2c/busses/i2c-qcom-target.c new file +7 -0
+}
+
+static const struct dev_pm_ops qcom_i2c_target_pm_ops = {
+	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_i2c_target_suspend,
+				      qcom_i2c_target_resume)
+};
+

can we also add reason here to add NOIRQ callbacks ? My generic question would be why not system PM or Runtime PM callbacks ? This will help later stage for any futurstic changes.

Quoted 21 lines
drivers/i2c/busses/i2c-qcom-target.c new file +20 -0
+static const struct of_device_id qcom_i2c_target_dt_match[] = {
+	{ .compatible = "qcom,qdu1000-i2c-target" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, qcom_i2c_target_dt_match);
+
+static struct platform_driver qcom_i2c_target_driver = {
+	.driver = {
+		.name		= "qcom-i2c-target",
+		.pm		= &qcom_i2c_target_pm_ops,
+		.of_match_table	= qcom_i2c_target_dt_match,
+	},
+	.probe	= qcom_i2c_target_probe,
+	.remove	= qcom_i2c_target_remove,
+};
+module_platform_driver(qcom_i2c_target_driver);
+
+MODULE_DESCRIPTION("Qualcomm I2C target controller driver");
+MODULE_AUTHOR("Viken Dadhaniya <[email protected]>");
+MODULE_LICENSE("GPL");
#14 lore raw

Re: [PATCH v3 2/2] i2c: qcom-target: Add driver for Qualcomm I2C target controller

Quoted 16 lines · Mukesh Savaliya
Quoted 5 lines · Viken Dadhaniya

QDU1000 and related Qualcomm SoCs include a dedicated I2C target controller that operates exclusively in target mode. The existing Qualcomm I2C controller drivers (GENI, QUP) are master-only and cannot serve systems where the SoC must respond as an I2C target on the bus.

Quoted 8 lines

Register the controller with the Linux I2C slave framework via i2c_algorithm.reg_target and i2c_algorithm.unreg_target so that any standard slave backend (e.g. slave-24c02) can be attached at runtime via i2c_slave_register(). Handle IRQ events for RX FIFO service, clock stretching during read and write phases, STOP and repeated-start conditions, and error recovery with SW reset. Enable the required AHB and XO clocks, vote for interconnect bandwidth, and restore hardware state across suspend and resume using the noirq PM callbacks.

Quoted 2 lines

Neat:
Though second paragraph gives good information, but i would suggest to keep  new lines to make it easier and simpler.>

Updated in v4.

Quoted 255 lines
Quoted 255 lines

---
  MAINTAINERS                          |   9 + drivers/i2c/busses/Kconfig           |  15 + drivers/i2c/busses/Makefile          |   1 + drivers/i2c/busses/i2c-qcom-target.c | 588 +++++++++++++++++++++++++++++++++++

  4 files changed, 613 insertions(+)
MAINTAINERS +0 -0
index fe67f7bfa44c..7a2d66385e36 100644
@@ -22380,6 +22380,15 @@ S:    Maintained

  F:    Documentation/devicetree/bindings/i2c/qcom,i2c-geni-qcom.yaml F:    drivers/i2c/busses/i2c-qcom-geni.c
  +QUALCOMM I2C TARGET CONTROLLER DRIVER

MAINTAINERS +8 -0
+M:    Viken Dadhaniya <[email protected]>
+M:    Mukesh Kumar Savaliya <[email protected]>
+L:    [email protected]
+L:    [email protected]
+S:    Maintained
+F:    Documentation/devicetree/bindings/i2c/qcom,qdu1000-i2c-target.yaml
+F:    drivers/i2c/busses/i2c-qcom-target.c
+

  QUALCOMM I2C CCI DRIVER
  M:    Loic Poulain <[email protected]> M:    Robert Foss <[email protected]>

drivers/i2c/busses/Kconfig +0 -0
index d7b89508311f..2e18238e0ae6 100644
@@ -1070,6 +1070,21 @@ config I2C_QCOM_GENI

        This driver can also be built as a module.  If so, the module will be called i2c-qcom-geni.
  +config I2C_QCOM_TARGET
+    tristate "Qualcomm I2C target controller"
+    depends on ARCH_QCOM || COMPILE_TEST
+    depends on COMMON_CLK
+    depends on INTERCONNECT
+    select I2C_SLAVE
+    help
+      This driver supports I2C target mode on Qualcomm Technologies +      SoCs. If you say yes to this option, support will be included +      for the built-in I2C target controller on QDU1000 and other +      compatible Qualcomm SoCs.

drivers/i2c/busses/Kconfig +4 -0
+
+      This driver can also be built as a module. If so, the module
+      will be called i2c-qcom-target.
+

  config I2C_QUP
      tristate "Qualcomm QUP based I2C controller" depends on ARCH_QCOM || COMPILE_TEST

drivers/i2c/busses/Makefile +0 -0
index 3755c54b3d82..ab3070cdb144 100644
@@ -101,6 +101,7 @@ obj-$(CONFIG_I2C_PXA)        += i2c-pxa.o

  obj-$(CONFIG_I2C_PXA_PCI)    += i2c-pxa-pci.o obj-$(CONFIG_I2C_QCOM_CCI)    += i2c-qcom-cci.o obj-$(CONFIG_I2C_QCOM_GENI)    += i2c-qcom-geni.o +obj-$(CONFIG_I2C_QCOM_TARGET)    += i2c-qcom-target.o obj-$(CONFIG_I2C_QUP)        += i2c-qup.o obj-$(CONFIG_I2C_RIIC)        += i2c-riic.o obj-$(CONFIG_I2C_RK3X)        += i2c-rk3x.o

drivers/i2c/busses/i2c-qcom-target.c new file +185 -0
new file mode 100644
index 000000000000..277ea944eedb
@@ -0,0 +1,588 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/interconnect.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+/* Register offsets */
+#define I2C_S_DEVICE_ADDR            0x00
+#define I2C_S_IRQ_STATUS            0x08
+#define I2C_S_IRQ_CLR                0x0C
+#define I2C_S_IRQ_EN                0x10
+#define I2C_S_CONFIG                0x18
+#define I2C_S_CONTROL                0x1C
+#define I2C_S_FIFOS_STATUS            0x20
+#define I2C_S_TX_FIFO                0x24
+#define I2C_S_RX_FIFO                0x28
+#define I2C_S_DEBUG_REG1            0x3C
+#define I2C_S_DEBUG_REG2            0x40
+#define I2C_S_SW_RESET_REG            0x4C
+#define I2C_S_CLK_LOW_TIMEOUT            0x50
+#define I2C_S_CLK_RELEASE_DELAY_CNT_VAL    0x54
+#define I2C_S_SDA_HOLD_CNT_VAL            0x58
+
+/* I2C_S_CONFIG register fields */
+#define I2C_S_CORE_EN                BIT(0)
+
+/* I2C_S_CONTROL register fields */
+#define CLEAR_RX_FIFO                BIT(0)
+#define CLEAR_TX_FIFO                BIT(1)
+#define NACK                    BIT(2)
+#define ACK_RESUME                BIT(3)
+
+/* I2C_S_SW_RESET_REG register fields */
+#define SW_RESET                BIT(0)
+
+/* I2C_S_FIFOS_STATUS register fields */
+#define RX_FIFO_COUNT_MASK            GENMASK(31, 16)
+
+/* Interconnect bandwidth vote in bytes per second */
+#define APPS_PROC_TO_I2C_TARGET_VOTE        1190000
+
+/**
+ * enum qcom_i2c_target_irq - IRQ bit positions in I2C_S_IRQ_STATUS
+ * @STOP_DETECTED:    I2C stop condition detected on the bus
+ * @RX_FIFO_FULL:    receive FIFO has reached capacity
+ * @TX_FIFO_EMPTY:    transmit FIFO is empty
+ * @RX_DATA_AVAIL:    receive data is available in the RX FIFO
+ * @CLOCK_LOW_TIMEOUT:    SCL held low longer than the configured timeout
+ * @STRCH_WR:        clock stretching during a write (Rx) phase
+ * @STRCH_RD:        clock stretching during a read (Tx) phase
+ * @GCA_DETECTED:    general call address detected (not used)
+ * @ERR_CONDITION:    unexpected start or stop bit detected (error)
+ * @RESTART_DETECTED:    repeated start condition detected
+ */
+enum qcom_i2c_target_irq {
+    STOP_DETECTED,
+    RX_FIFO_FULL,
+    TX_FIFO_EMPTY,
+    RX_DATA_AVAIL,
+    CLOCK_LOW_TIMEOUT,
+    STRCH_WR,
+    STRCH_RD,
+    GCA_DETECTED,
+    ERR_CONDITION,
+    RESTART_DETECTED,
+};
+
+/*
+ * TX_FIFO_EMPTY is excluded: this driver fills the TX FIFO one byte at a
+ * time in response to STRCH_RD (clock-stretch during read phase), so
+ * TX_FIFO_EMPTY adds no value and would double the interrupt rate.
+ * GCA (general call address) is unsupported.
+ */
+#define QCOM_I2C_TARGET_ALL_IRQ    (GENMASK(RESTART_DETECTED, STOP_DETECTED) \
+                 & ~(BIT(GCA_DETECTED) | BIT(TX_FIFO_EMPTY)))
+
+/* Bit indices for the status bitmask, used with set_bit/test_bit/clear_bit */
+enum qcom_i2c_target_status {
+    READ_IN_PROGRESS,
+    WRITE_IN_PROGRESS,
+};
+
+/**
+ * struct qcom_i2c_target - Qualcomm I2C target controller private data
+ * @dev:        driver model device node
+ * @base:        base address of HW registers
+ * @adap:        I2C adapter (slave mode)
+ * @ahb_clk:        AHB bus clock
+ * @xo_clk:        XO reference clock
+ * @icc_path:        interconnect bandwidth path
+ * @slave:        currently registered slave backend client; written only
+ *            under disable_irq() in reg_slave()/unreg_slave() so the
+ *            ISR sees a stable pointer for its entire execution
+ * @status:        bitmask of enum qcom_i2c_target_status flags; must be
+ *            unsigned long for set_bit/test_bit/clear_bit; accessed
+ *            only from the ISR and from reg_slave()/unreg_slave()
+ *            under disable_irq(), so no additional lock is needed
+ * @irq:        interrupt line number
+ */
+struct qcom_i2c_target {
+    struct device        *dev;
+    void __iomem        *base;
+    struct i2c_adapter    adap;
+    struct clk        *ahb_clk;
+    struct clk        *xo_clk;
+    struct icc_path        *icc_path;
+    struct i2c_client    *slave;
+    unsigned long        status;
+    int            irq;
+};
+
+static void qcom_i2c_target_dump_regs(struct qcom_i2c_target *target)
+{
+    dev_dbg(target->dev, "I2C_S_DEVICE_ADDR:               0x%x\n",
+        readl_relaxed(target->base + I2C_S_DEVICE_ADDR));
+    dev_dbg(target->dev, "I2C_S_IRQ_STATUS:                0x%x\n",
+        readl_relaxed(target->base + I2C_S_IRQ_STATUS));
+    dev_dbg(target->dev, "I2C_S_CONFIG:                    0x%x\n",
+        readl_relaxed(target->base + I2C_S_CONFIG));
+    dev_dbg(target->dev, "I2C_S_IRQ_EN:                    0x%x\n",
+        readl_relaxed(target->base + I2C_S_IRQ_EN));
+    dev_dbg(target->dev, "I2C_S_FIFOS_STATUS:              0x%x\n",
+        readl_relaxed(target->base + I2C_S_FIFOS_STATUS));
+    dev_dbg(target->dev, "I2C_S_DEBUG_REG1:                0x%x\n",
+        readl_relaxed(target->base + I2C_S_DEBUG_REG1));
+    dev_dbg(target->dev, "I2C_S_DEBUG_REG2:                0x%x\n",
+        readl_relaxed(target->base + I2C_S_DEBUG_REG2));
+    dev_dbg(target->dev, "I2C_S_CLK_LOW_TIMEOUT:           0x%x\n",
+        readl_relaxed(target->base + I2C_S_CLK_LOW_TIMEOUT));
+    dev_dbg(target->dev, "I2C_S_CLK_RELEASE_DELAY_CNT_VAL: 0x%x\n",
+        readl_relaxed(target->base + I2C_S_CLK_RELEASE_DELAY_CNT_VAL));
+    dev_dbg(target->dev, "I2C_S_SDA_HOLD_CNT_VAL:          0x%x\n",
+        readl_relaxed(target->base + I2C_S_SDA_HOLD_CNT_VAL));
+}
+
+static void qcom_i2c_target_hw_init(struct qcom_i2c_target *target)
+{
+    dev_dbg(target->dev, "HW init: resetting FIFOs, enabling IRQs\n");
+    writel(CLEAR_TX_FIFO | CLEAR_RX_FIFO, target->base + I2C_S_CONTROL);
+    writel(QCOM_I2C_TARGET_ALL_IRQ, target->base + I2C_S_IRQ_EN);
+}
+
+static void qcom_i2c_target_write_requested(struct qcom_i2c_target *target)
+{
+    u8 val = 0;
+
+    if (!test_and_set_bit(WRITE_IN_PROGRESS, &target->status)) {
+        dev_dbg(target->dev, "Write phase started\n");
+        i2c_slave_event(target->slave, I2C_SLAVE_WRITE_REQUESTED, &val);
+    }
+}
+
+static int qcom_i2c_target_drain_rx_fifo(struct qcom_i2c_target *target)
+{
+    unsigned int rx_count;
+    int ret = 0;
+    u8 val;
+
+    while (!ret) {
+        rx_count = FIELD_GET(RX_FIFO_COUNT_MASK,
+                     readl_relaxed(target->base + I2C_S_FIFOS_STATUS));
+        if (!rx_count)
+            break;
+
+        while (rx_count--) {
+            val = (u8)readl_relaxed(target->base + I2C_S_RX_FIFO);
+            dev_dbg(target->dev, "Data from RX FIFO: 0x%x\n", val);
+            ret = i2c_slave_event(target->slave, I2C_SLAVE_WRITE_RECEIVED, &val);
+            if (ret)
+                break;
+        }
+    }
+
+    return ret;
+}
+
Quoted 8 lines

drivers/i2c/busses/i2c-qcom-target.c line 191

Quoted 6 lines
drivers/i2c/busses/i2c-qcom-target.c new file +6 -0
+static void qcom_i2c_target_hw_reset(struct qcom_i2c_target *target)
+{
+    /* Clear error bits before SW_RESET; the reset may not be instantaneous */
+    writel(BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT),
+           target->base + I2C_S_IRQ_CLR);
+    writel(SW_RESET, target->base + I2C_S_SW_RESET_REG);

line space > +    /*

Added in v4.

Quoted 16 lines
Quoted 16 lines
drivers/i2c/busses/i2c-qcom-target.c new file +16 -0
+     * I2C_S_SW_RESET_REG is write-only so completion cannot be polled.
+     * Use a conservative delay to allow the reset to finish before
+     * reconfiguring the controller.
+     */
+    usleep_range(10, 20);
+    qcom_i2c_target_hw_init(target);
+    writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
+    writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+}
+
+static irqreturn_t qcom_i2c_target_handle_error(struct qcom_i2c_target *target,
+                        u32 irq_stat)
+{
+    u8 val = 0;
+
+    if (irq_stat & BIT(ERR_CONDITION))
Quoted 8 lines

drivers/i2c/busses/i2c-qcom-target.c line 215

Quoted 7 lines
drivers/i2c/busses/i2c-qcom-target.c new file +7 -0
+        dev_err(target->dev, "Error condition: unexpected Start/Stop bits\n");
+    else
+        dev_err(target->dev, "Clock low timeout\n");
+    qcom_i2c_target_dump_regs(target);
+    qcom_i2c_target_hw_reset(target);
+    i2c_slave_event(target->slave, I2C_SLAVE_STOP, &val);
+    target->status = 0;

line space before return> +    return IRQ_HANDLED;

Added in v4.

Quoted 17 lines
Quoted 17 lines
drivers/i2c/busses/i2c-qcom-target.c new file +17 -0
+}
+
+static irqreturn_t qcom_i2c_target_handle_stop(struct qcom_i2c_target *target,
+                           u32 irq_stat)
+{
+    u8 val = 0;
+
+    dev_dbg(target->dev, "Stop bit detected\n");
+    /*
+     * Short-write corner case: WRITE_IN_PROGRESS was never set because
+     * no RX_DATA_AVAIL or STRCH_WR fired before STOP. Do not call
+     * qcom_i2c_target_write_requested() here — STOP ends the transaction
+     * so setting WRITE_IN_PROGRESS now would leave a stale bit after
+     * target->status is cleared below.
+     */
+    if (!test_bit(WRITE_IN_PROGRESS, &target->status)) {
+        unsigned int rx_count;
Quoted 8 lines

drivers/i2c/busses/i2c-qcom-target.c line 240

Quoted 7 lines
drivers/i2c/busses/i2c-qcom-target.c new file +7 -0
+
+        rx_count = FIELD_GET(RX_FIFO_COUNT_MASK,
+                     readl_relaxed(target->base + I2C_S_FIFOS_STATUS));
+        if (rx_count)
+            i2c_slave_event(target->slave, I2C_SLAVE_WRITE_REQUESTED,
+                    &val);
+    }

minor as cleanup: give a line space > + qcom_i2c_target_drain_rx_fifo(target);

Added in v4.

Quoted 14 lines

drivers/i2c/busses/i2c-qcom-target.c line 254

Quoted 13 lines
drivers/i2c/busses/i2c-qcom-target.c new file +13 -0
+    i2c_slave_event(target->slave, I2C_SLAVE_STOP, &val);
+    target->status = 0;
+    writel(CLEAR_RX_FIFO, target->base + I2C_S_CONTROL);
+    /*
+     * STOP terminates the transaction, so any other Rx or clock
+     * stretch bits latched in this same status read have already
+     * been serviced by the drain above or are now stale. Ack the
+     * whole word rather than just STOP_DETECTED; clearing only STOP
+     * would leave those bits set and immediately re-enter the
+     * handler, which for a co-asserted STRCH_RD would push a stale
+     * byte into the TX FIFO.
+     */
+    writel(irq_stat, target->base + I2C_S_IRQ_CLR);
Quoted 1 line

give a line space before returning.> +    return IRQ_HANDLED;

Added in v4.

Quoted 13 lines

drivers/i2c/busses/i2c-qcom-target.c line 267

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-target.c new file +12 -0
+}
+
+static void qcom_i2c_target_handle_rx_data(struct qcom_i2c_target *target,
+                       u32 rx_irq_bits)
+{
+    int ret;
+
+    dev_dbg(target->dev, "Rx data event (rx_irq_bits=0x%x)\n", rx_irq_bits);
+    qcom_i2c_target_write_requested(target);
+    ret = qcom_i2c_target_drain_rx_fifo(target);
+    if (ret)
+        dev_dbg(target->dev, "Backend requested NACK\n");

line space for neat> +    writel(ret ? NACK | CLEAR_RX_FIFO : ACK_RESUME,

Quoted 16 lines
Quoted 5 lines
drivers/i2c/busses/i2c-qcom-target.c new file +5 -0
+           target->base + I2C_S_CONTROL);
+    writel(rx_irq_bits, target->base + I2C_S_IRQ_CLR);
+}
+
+static void qcom_i2c_target_handle_strch_rd(struct qcom_i2c_target *target)

drivers/i2c/busses/i2c-qcom-target.c line 283

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+{
+    enum i2c_slave_event event = I2C_SLAVE_READ_PROCESSED;
+    u8 val = 0;
+
+    dev_dbg(target->dev, "Clock stretching during read (Tx) phase\n");
+    if (!test_and_set_bit(READ_IN_PROGRESS, &target->status)) {
+        /* Repeated-start: master switched direction from write to read */
+        clear_bit(WRITE_IN_PROGRESS, &target->status);
+        event = I2C_SLAVE_READ_REQUESTED;
+    }

line space> +    i2c_slave_event(target->slave, event, &val);

Added in v4.

Quoted 88 lines
Quoted 88 lines
drivers/i2c/busses/i2c-qcom-target.c new file +88 -0
+    dev_dbg(target->dev, "Data to TX FIFO: 0x%x\n", val);
+    writel(val, target->base + I2C_S_TX_FIFO);
+    writel(ACK_RESUME, target->base + I2C_S_CONTROL);
+    writel(BIT(STRCH_RD), target->base + I2C_S_IRQ_CLR);
+}
+
+static irqreturn_t qcom_i2c_target_irq(int irq, void *dev)
+{
+    struct qcom_i2c_target *target = dev;
+    u32 irq_stat, rx_bits;
+
+    /*
+     * Dispatch priority (highest first):
+     *   ERR_CONDITION / CLOCK_LOW_TIMEOUT  — hardware error, triggers SW reset
+     *   STOP_DETECTED                      — end of transaction, clears all state
+     *   RESTART_DETECTED                   — repeated start, resets state before
+     *                                        any data phase in the same snapshot
+     *   STRCH_RD                           — read-phase data supply
+     *   RX_FIFO_FULL / RX_DATA_AVAIL /
+     *   STRCH_WR                           — write-phase Rx, coalesced into one drain
+     */
+    irq_stat = readl_relaxed(target->base + I2C_S_IRQ_STATUS);
+    if (!irq_stat)
+        return IRQ_NONE;
+
+    dev_dbg(target->dev, "IRQ status: 0x%x\n", irq_stat);
+
+    /*
+     * Load target->slave once. Both reg_slave() and unreg_slave() disable
+     * the IRQ before writing the pointer, so it cannot change while this
+     * handler runs. Sub-handlers may dereference target->slave directly.
+     *
+     * The core is enabled only in reg_slave() and disabled in unreg_slave(),
+     * so no bus activity is expected here. Clear and discard any stale IRQ.
+     */
+    if (!READ_ONCE(target->slave)) {
+        writel(irq_stat, target->base + I2C_S_IRQ_CLR);
+        return IRQ_HANDLED;
+    }
+
+    if (irq_stat & (BIT(ERR_CONDITION) | BIT(CLOCK_LOW_TIMEOUT)))
+        return qcom_i2c_target_handle_error(target, irq_stat);
+
+    if (irq_stat & BIT(STOP_DETECTED))
+        return qcom_i2c_target_handle_stop(target, irq_stat);
+
+    if (irq_stat & BIT(RESTART_DETECTED)) {
+        dev_dbg(target->dev, "Repeated start bit detected\n");
+        target->status = 0;
+        writel(ACK_RESUME, target->base + I2C_S_CONTROL);
+        writel(BIT(RESTART_DETECTED), target->base + I2C_S_IRQ_CLR);
+    }
+
+    if (irq_stat & BIT(STRCH_RD))
+        qcom_i2c_target_handle_strch_rd(target);
+
+    /*
+     * Coalesce all write-phase Rx bits into a single drain+ACK. When the
+     * RX FIFO fills at threshold, RX_FIFO_FULL, RX_DATA_AVAIL and STRCH_WR
+     * can all assert in the same irq_stat snapshot. Pass the combined mask
+     * so ACK_RESUME is written exactly once and all bits are cleared together.
+     */
+    rx_bits = irq_stat & (BIT(RX_FIFO_FULL) | BIT(RX_DATA_AVAIL) |
+                  BIT(STRCH_WR));
+    if (rx_bits)
+        qcom_i2c_target_handle_rx_data(target, rx_bits);
+
+    return IRQ_HANDLED;
+}
+
+static int qcom_i2c_target_reg_slave(struct i2c_client *slave)
+{
+    struct qcom_i2c_target *target = i2c_get_adapdata(slave->adapter);
+
+    if (target->slave)
+        return -EBUSY;
+
+    if (slave->flags & I2C_CLIENT_TEN)
+        return -EAFNOSUPPORT;
+
+    disable_irq(target->irq);
+    WRITE_ONCE(target->slave, slave);
+    writel(slave->addr, target->base + I2C_S_DEVICE_ADDR);
+    writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+    enable_irq(target->irq);
+
+    return 0;
+}
Quoted 8 lines

drivers/i2c/busses/i2c-qcom-target.c line 379

Quoted 7 lines
drivers/i2c/busses/i2c-qcom-target.c new file +7 -0
+
+static int qcom_i2c_target_unreg_slave(struct i2c_client *slave)
+{
+    struct qcom_i2c_target *target = i2c_get_adapdata(slave->adapter);
+
+    if (!target->slave)
+        return -EINVAL;

minor comment: line space > +    disable_irq(target->irq);

Added in v4.

Quoted 171 lines
Quoted 159 lines
drivers/i2c/busses/i2c-qcom-target.c new file +159 -0
+    writel(0, target->base + I2C_S_CONFIG);
+    WRITE_ONCE(target->slave, NULL);
+    target->status = 0;
+    writel(0, target->base + I2C_S_DEVICE_ADDR);
+    enable_irq(target->irq);
+
+    return 0;
+}
+
+static int qcom_i2c_target_icc_init(struct qcom_i2c_target *target)
+{
+    int ret;
+
+    target->icc_path = devm_of_icc_get(target->dev, NULL);
+    if (IS_ERR(target->icc_path))
+        return dev_err_probe(target->dev, PTR_ERR(target->icc_path),
+                     "failed to get ICC path\n");
+
+    /*
+     * The controller only needs interconnect bandwidth while it is
+     * powered. The vote is placed here and across resume with
+     * icc_set_bw(), and dropped with icc_set_bw(path, 0, 0) on suspend
+     * and remove, so the vote and unvote are always symmetric.
+     */
+    ret = icc_set_bw(target->icc_path, APPS_PROC_TO_I2C_TARGET_VOTE,
+             APPS_PROC_TO_I2C_TARGET_VOTE);
+    if (ret)
+        return dev_err_probe(target->dev, ret, "icc_set_bw failed\n");
+
+    return 0;
+}
+
+static u32 qcom_i2c_target_func(struct i2c_adapter *adap)
+{
+    return I2C_FUNC_SLAVE;
+}
+
+static const struct i2c_algorithm qcom_i2c_target_algo = {
+    .reg_target    = qcom_i2c_target_reg_slave,
+    .unreg_target    = qcom_i2c_target_unreg_slave,
+    .functionality    = qcom_i2c_target_func,
+};
+
+static int qcom_i2c_target_adap_init(struct qcom_i2c_target *target)
+{
+    target->adap.algo = &qcom_i2c_target_algo;
+    target->adap.dev.parent = target->dev;
+    target->adap.dev.of_node = target->dev->of_node;
+    strscpy(target->adap.name, "qcom-i2c-target",
+        sizeof(target->adap.name));
+    i2c_set_adapdata(&target->adap, target);
+
+    return i2c_add_adapter(&target->adap);
+}
+
+static int qcom_i2c_target_probe(struct platform_device *pdev)
+{
+    struct qcom_i2c_target *target;
+    struct device *dev = &pdev->dev;
+    int ret;
+
+    target = devm_kzalloc(dev, sizeof(*target), GFP_KERNEL);
+    if (!target)
+        return -ENOMEM;
+
+    target->dev = dev;
+
+    target->base = devm_platform_ioremap_resource(pdev, 0);
+    if (IS_ERR(target->base))
+        return dev_err_probe(dev, PTR_ERR(target->base),
+                     "failed to map registers\n");
+
+    target->xo_clk = devm_clk_get(dev, "xo");
+    if (IS_ERR(target->xo_clk))
+        return dev_err_probe(dev, PTR_ERR(target->xo_clk),
+                     "failed to get XO clock\n");
+
+    target->ahb_clk = devm_clk_get(dev, "ahb");
+    if (IS_ERR(target->ahb_clk))
+        return dev_err_probe(dev, PTR_ERR(target->ahb_clk),
+                     "failed to get AHB clock\n");
+
+    ret = clk_prepare_enable(target->xo_clk);
+    if (ret)
+        return dev_err_probe(dev, ret, "failed to enable XO clock\n");
+
+    ret = clk_prepare_enable(target->ahb_clk);
+    if (ret) {
+        clk_disable_unprepare(target->xo_clk);
+        return dev_err_probe(dev, ret, "failed to enable AHB clock\n");
+    }
+
+    target->irq = platform_get_irq(pdev, 0);
+    if (target->irq < 0)
+        return target->irq;
+
+    ret = qcom_i2c_target_icc_init(target);
+    if (ret)
+        return ret;
+
+    ret = devm_request_irq(dev, target->irq, qcom_i2c_target_irq, 0,
+                   dev_name(dev), target);
+    if (ret)
+        return dev_err_probe(dev, ret, "request_irq failed for IRQ %d\n",
+                     target->irq);
+
+    qcom_i2c_target_hw_init(target);
+
+    platform_set_drvdata(pdev, target);
+
+    ret = qcom_i2c_target_adap_init(target);
+    if (ret)
+        return dev_err_probe(dev, ret, "i2c_add_adapter failed\n");
+
+    return 0;
+}
+
+static void qcom_i2c_target_remove(struct platform_device *pdev)
+{
+    struct qcom_i2c_target *target = platform_get_drvdata(pdev);
+
+    writel(0, target->base + I2C_S_CONFIG);
+    i2c_del_adapter(&target->adap);
+    icc_set_bw(target->icc_path, 0, 0);
+    clk_disable_unprepare(target->xo_clk);
+    clk_disable_unprepare(target->ahb_clk);
+}
+
+static int qcom_i2c_target_suspend(struct device *dev)
+{
+    struct qcom_i2c_target *target = dev_get_drvdata(dev);
+    int ret;
+
+    ret = icc_set_bw(target->icc_path, 0, 0);
+    if (ret)
+        dev_err(dev, "icc_set_bw failed on suspend: %d\n", ret);
+
+    clk_disable_unprepare(target->xo_clk);
+    clk_disable_unprepare(target->ahb_clk);
+
+    return 0;
+}
+
+static int qcom_i2c_target_resume(struct device *dev)
+{
+    struct qcom_i2c_target *target = dev_get_drvdata(dev);
+    int ret;
+
+    ret = clk_prepare_enable(target->ahb_clk);
+    if (ret) {
+        dev_err(dev, "failed to enable AHB clock\n");
+        return ret;
+    }
+
+    ret = clk_prepare_enable(target->xo_clk);
+    if (ret) {
+        dev_err(dev, "failed to enable XO clock\n");
+        goto err_disable_ahb;
+    }

drivers/i2c/busses/i2c-qcom-target.c line 549

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+
+    ret = icc_set_bw(target->icc_path, APPS_PROC_TO_I2C_TARGET_VOTE,
+             APPS_PROC_TO_I2C_TARGET_VOTE);
+    if (ret) {
+        dev_err(dev, "icc_set_bw failed\n");
+        goto err_disable_xo;
+    }
+
+    qcom_i2c_target_hw_init(target);
+    target->status = 0;

if the status is 0 here after target init, what about other places ? does it need to change status ? OR is it inherent to framework ?

Quoted 1 line

Any comment can be help while changing status ?> +    if (target->slave) {

Status is private transaction state owned by this driver; it is not managed by the I2C slave framework. It is cleared on STOP, error reset, repeated START, slave unregister, and resume. Resume clears it because the controller/FIFOs are reinitialized and any transaction state from before suspend is no longer valid. The initial probe value is already zeroed by devm_kzalloc().

Quoted 11 lines

drivers/i2c/busses/i2c-qcom-target.c line 560

Quoted 10 lines
drivers/i2c/busses/i2c-qcom-target.c new file +10 -0
+        writel(target->slave->addr, target->base + I2C_S_DEVICE_ADDR);
+        writel(I2C_S_CORE_EN, target->base + I2C_S_CONFIG);
+    }
+
+    return 0;
+
+err_disable_xo:
+    clk_disable_unprepare(target->xo_clk);
+err_disable_ahb:
+    clk_disable_unprepare(target->ahb_clk);

line space before return> +    return ret;

Quoted 10 lines

drivers/i2c/busses/i2c-qcom-target.c line 568

Quoted 7 lines
drivers/i2c/busses/i2c-qcom-target.c new file +7 -0
+}
+
+static const struct dev_pm_ops qcom_i2c_target_pm_ops = {
+    SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_i2c_target_suspend,
+                      qcom_i2c_target_resume)
+};
+

can we also add reason here to add NOIRQ callbacks ? My generic question would be why not system PM or Runtime PM callbacks ? This will help later stage for any futurstic changes.

Updated in v4.

Quoted 23 lines
Quoted 22 lines
drivers/i2c/busses/i2c-qcom-target.c new file +20 -0
+static const struct of_device_id qcom_i2c_target_dt_match[] = {
+    { .compatible = "qcom,qdu1000-i2c-target" },
+    {}
+};
+MODULE_DEVICE_TABLE(of, qcom_i2c_target_dt_match);
+
+static struct platform_driver qcom_i2c_target_driver = {
+    .driver = {
+        .name        = "qcom-i2c-target",
+        .pm        = &qcom_i2c_target_pm_ops,
+        .of_match_table    = qcom_i2c_target_dt_match,
+    },
+    .probe    = qcom_i2c_target_probe,
+    .remove    = qcom_i2c_target_remove,
+};
+module_platform_driver(qcom_i2c_target_driver);
+
+MODULE_DESCRIPTION("Qualcomm I2C target controller driver");
+MODULE_AUTHOR("Viken Dadhaniya <[email protected]>");
+MODULE_LICENSE("GPL");