Skip to contentSkip to conversation

i2c: ls2x: Add support for the Loongson-2K/LS7A I2C controller

PATCH Superseded Binbin Zhou 3 participants 8 messages Dec 19, 2022 – Dec 20, 2022

Subsystems (5)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Binbin Zhou

author maintainer
#0 lore raw

v7 cover i2c: ls2x: Add support for the Loongson-2K/LS7A I2C controller

Hi all:

This patch series adds support for the I2C module found on various Loongson systems with the Loongson-2K SoC or the Loongson LS7A bridge chip.

For now, the I2C driver is suitable for DT-based or ACPI-based systems.

I have tested on Loongson-3A5000LA+LS7A1000/LS7A2000, Loongson-2K1000LA and Loongson-2K0500.

Thanks.

Changes since V6:
- patch (1/4)
- Add Reviewed-by tag.
- patch (3/4)
- GPL-2.0 -> GPL-2.0-only;
- Add property.h;
- writew() should be used to operate I2C_LS2X_PRER, and drop the

    suffix of I2C_LS2X_PRER_LO/I2C_LS2X_PRER_HI;
  - Drop ls2x_i2c_priv->dev, for it can be completely replaced by
    'adapter.dev.parent';
  - Reasonable return value handling in ls2x_i2c_xfer_one();
  - Dropping the I2C_M_STOP flag judgment, the stop parameter of
    ls2x_i2c_xfer_one() represents the last msg;

- Add comments for subsys_initcall();
- Code formatting, such as dropping unnecessary blank lines.

Changes since V5:
- patch (1/4)
- Add property.h.
- patch (3/4)
- Put the ls2x_i2c_reginit() in front of irq requst;
- Refact ls2x_i2c_adjust_bus_speed: discard the magic value of the divider register and LS2X_I2C_FREQ_STD is used to calculate the frequency;
- Drop useless parameters: priv->suspended, and also disable I2C

    interrupts during suspend;
  - Drop ls2x_i2c_remove(), for the adapter will be auto deleted on
    driver detach;

- Drop MODULE_ALIAS;
- Code formatting, such as alignment.

Thanks Andy for your comments.

Changes since V4:
- patch (1/4)
- Drop unneeded headers: of.h;
- xxx_props -> xxx_properties.
- patch (2/4)
- Add interrupt headers to fix syntax error found by Rob.
- patch (3/4)
- Drop atmoic loop in ls2x_i2c_master_xfer(), I have tested it on the

    appropriate environment with no problems;
  - Define the corresponding bits in I2C_LS2X_CTR to avoid magic
    numbers;
  - dev_get_drvdata() is used to get ls2x_i2c_priv() in
    ls2x_i2c_suspend();

- i2c_add_adapter() -> devm_i2c_add_adapter();
- SET_SYSTEM_SLEEP_PM_OPS() -> DEFINE_RUNTIME_DEV_PM_OPS();
- Code formatting, such as alignment.

Details: https://lore.kernel.org/all/Y4e%[email protected]/

Changes since V3:
- Addressed all review comments from v3
- Change the changelog text to make it clearer (1/5);
- Fix some minor bugs, such as formatting issues (2/5);
- Fix some formatting issues (3/5);
- Deep refactoring of code for clarity (4/5).
Details: https://lore.kernel.org/all/[email protected]/

Thanks to all for their suggestions.

Changes since V2:
- Addressed all review comments from v2
- Drop of_match_ptr() in i2c-gpio to avoid potential unused warnings

    (1/5);
  - Introduce i2c_gpio_get_props() function as the generic interface
    to get i2c-gpio props from DT or ACPI table (2/5);

- Refact ls2x i2c code, similar to removing excessive goto tags (4/5).

Thanks to Andy and Mika for their suggestions.

Changes since V1:
- Remove the function of getting the static i2c bus number from ACPI "_UID";
- Fix build warning from kernel test robot.

Binbin Zhou (4):
i2c: gpio: Add support on ACPI-based system
dt-bindings: i2c: add Loongson LS2X I2C controller
i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller
LoongArch: Enable LS2X I2C in loongson3_defconfig

 .../bindings/i2c/loongson,ls2x-i2c.yaml       |  51 +++
 arch/loongarch/configs/loongson3_defconfig    |   1 +
 drivers/i2c/busses/Kconfig                    |  11 +
 drivers/i2c/busses/Makefile                   |   1 +
 drivers/i2c/busses/i2c-gpio.c                 |  28 +-
 drivers/i2c/busses/i2c-ls2x.c                 | 369 ++++++++++++++++++
 6 files changed, 451 insertions(+), 10 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml
 create mode 100644 drivers/i2c/busses/i2c-ls2x.c
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v7 1/4 i2c: gpio: Add support on ACPI-based system

Add support for the ACPI-based device registration, so that the driver can be also enabled through ACPI table.

---
 drivers/i2c/busses/i2c-gpio.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-gpio.c +18 -10
index 0e4385a9bcf7..680936234ef8 100644
@@ -13,9 +13,9 @@
 #include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
-#include <linux/of.h>
 #include <linux/platform_data/i2c-gpio.h>
 #include <linux/platform_device.h>
+#include <linux/property.h>
 #include <linux/slab.h>
 
 struct i2c_gpio_private_data {
@@ -300,22 +300,23 @@ static inline void i2c_gpio_fault_injector_init(struct platform_device *pdev) {}
 static inline void i2c_gpio_fault_injector_exit(struct platform_device *pdev) {}
 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
 
-static void of_i2c_gpio_get_props(struct device_node *np,
-				  struct i2c_gpio_platform_data *pdata)
+/* Get i2c-gpio properties from DT or ACPI table */
+static void i2c_gpio_get_properties(struct device *dev,
+				     struct i2c_gpio_platform_data *pdata)
 {
 	u32 reg;
 
-	of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay);
+	device_property_read_u32(dev, "i2c-gpio,delay-us", &pdata->udelay);
 
-	if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", &reg))
+	if (!device_property_read_u32(dev, "i2c-gpio,timeout-ms", &reg))
 		pdata->timeout = msecs_to_jiffies(reg);
 
 	pdata->sda_is_open_drain =
-		of_property_read_bool(np, "i2c-gpio,sda-open-drain");
+		device_property_read_bool(dev, "i2c-gpio,sda-open-drain");
 	pdata->scl_is_open_drain =
-		of_property_read_bool(np, "i2c-gpio,scl-open-drain");
+		device_property_read_bool(dev, "i2c-gpio,scl-open-drain");
 	pdata->scl_is_output_only =
-		of_property_read_bool(np, "i2c-gpio,scl-output-only");
+		device_property_read_bool(dev, "i2c-gpio,scl-output-only");
 }
 
 static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
@@ -373,8 +374,8 @@ static int i2c_gpio_probe(struct platform_device *pdev)
 	bit_data = &priv->bit_data;
 	pdata = &priv->pdata;
 
-	if (np) {
-		of_i2c_gpio_get_props(np, pdata);
+	if (dev_fwnode(dev)) {
+		i2c_gpio_get_properties(dev, pdata);
 	} else {
 		/*
 		 * If all platform data settings are zero it is OK
@@ -489,10 +490,17 @@ static const struct of_device_id i2c_gpio_dt_ids[] = {
 
 MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
 
+static const struct acpi_device_id i2c_gpio_acpi_match[] = {
+	{ "LOON0005" }, /* LoongArch */
+	{ }
+};
+MODULE_DEVICE_TABLE(acpi, i2c_gpio_acpi_match);
+
 static struct platform_driver i2c_gpio_driver = {
 	.driver		= {
 		.name	= "i2c-gpio",
 		.of_match_table	= i2c_gpio_dt_ids,
+		.acpi_match_table = i2c_gpio_acpi_match,
 	},
 	.probe		= i2c_gpio_probe,
 	.remove		= i2c_gpio_remove,
Signature · 1 line
2.31.1
author maintainer
#2 lore raw

PATCH v7 2/4 dt-bindings: i2c: add Loongson LS2X I2C controller

In reply to Binbin Zhou

Add Loongson LS2X I2C controller binding with DT schema format using json-schema.

---
 .../bindings/i2c/loongson,ls2x-i2c.yaml       | 51 +++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml
Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml new file +51 -0
new file mode 100644
index 000000000000..67882ec6e06a
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/i2c/loongson,ls2x-i2c.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Loongson LS2X I2C Controller
+
+maintainers:
+  - Binbin Zhou <[email protected]>
+
+allOf:
+  - $ref: /schemas/i2c/i2c-controller.yaml#
+
+properties:
+  compatible:
+    enum:
+      - loongson,ls2k-i2c
+      - loongson,ls7a-i2c
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    i2c0: i2c@1fe21000 {
+        compatible = "loongson,ls2k-i2c";
+        reg = <0x1fe21000 0x8>;
+        interrupt-parent = <&extioiic>;
+        interrupts = <22 IRQ_TYPE_LEVEL_LOW>;
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        eeprom@57 {
+            compatible = "atmel,24c16";
+            reg = <0x57>;
+            pagesize = <16>;
+        };
+    };
Signature · 1 line
2.31.1
author maintainer
#3 lore raw

PATCH v7 3/4 i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller

In reply to Binbin Zhou

This I2C module is integrated into the Loongson-2K SoCs and Loongson LS7A bridge chip.

---
 drivers/i2c/busses/Kconfig    |  11 +
 drivers/i2c/busses/Makefile   |   1 +
 drivers/i2c/busses/i2c-ls2x.c | 369 ++++++++++++++++++++++++++++++++++
 3 files changed, 381 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-ls2x.c
drivers/i2c/busses/Kconfig +11 -0
index e50f9603d189..908096a762ca 100644
@@ -888,6 +888,17 @@ config I2C_OWL
 	  Say Y here if you want to use the I2C bus controller on
 	  the Actions Semiconductor Owl SoC's.
 
+config I2C_LS2X
+	tristate "Loongson LS2X I2C adapter"
+	depends on MACH_LOONGSON64 || COMPILE_TEST
+	help
+	  If you say yes to this option, support will be included for the
+	  I2C interface on the Loongson-2K SoCs and Loongson LS7A bridge
+	  chip.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-ls2x.
+
 config I2C_PASEMI
 	tristate "PA Semi SMBus interface"
 	depends on PPC_PASEMI && PCI
drivers/i2c/busses/Makefile +1 -0
index e73cdb1d2b5a..df332ec3c489 100644
@@ -86,6 +86,7 @@ obj-$(CONFIG_I2C_MV64XXX)	+= i2c-mv64xxx.o
 obj-$(CONFIG_I2C_MXS)		+= i2c-mxs.o
 obj-$(CONFIG_I2C_NOMADIK)	+= i2c-nomadik.o
 obj-$(CONFIG_I2C_NPCM)		+= i2c-npcm7xx.o
+obj-$(CONFIG_I2C_LS2X)		+= i2c-ls2x.o
 obj-$(CONFIG_I2C_OCORES)	+= i2c-ocores.o
 obj-$(CONFIG_I2C_OMAP)		+= i2c-omap.o
 obj-$(CONFIG_I2C_OWL)		+= i2c-owl.o
drivers/i2c/busses/i2c-ls2x.c new file +369 -0

369 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH V7 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller

Quoted 2 lines · Binbin Zhou

This I2C module is integrated into the Loongson-2K SoCs and Loongson LS7A bridge chip.

Almost there, see my comments below (note, you have ~1w of time before this can be applied anyway, so take you time for carefully addressing comments, (re-)testing, etc.).

...

drivers/i2c/busses/Kconfig config I2C_OWL · line 891

Quoted 5 lines
drivers/i2c/busses/Kconfig +1 -0
@@ -888,6 +888,17 @@ config I2C_OWL
 	  Say Y here if you want to use the I2C bus controller on
 	  the Actions Semiconductor Owl SoC's.
 
+config I2C_LS2X

I believe in Latin alphabet L goes before O...

Quoted 13 lines
drivers/i2c/busses/Kconfig +10 -0
+	tristate "Loongson LS2X I2C adapter"
+	depends on MACH_LOONGSON64 || COMPILE_TEST
+	help
+	  If you say yes to this option, support will be included for the
+	  I2C interface on the Loongson-2K SoCs and Loongson LS7A bridge
+	  chip.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-ls2x.
+
 config I2C_PASEMI
 	tristate "PA Semi SMBus interface"
 	depends on PPC_PASEMI && PCI

...

Quoted 4 lines

obj-$(CONFIG_I2C_MXS) += i2c-mxs.o
obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o
obj-$(CONFIG_I2C_NPCM) += i2c-npcm7xx.o
+obj-$(CONFIG_I2C_LS2X) += i2c-ls2x.o

...and even before n and m.

Quoted 3 lines

obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o
obj-$(CONFIG_I2C_OMAP) += i2c-omap.o
obj-$(CONFIG_I2C_OWL) += i2c-owl.o

...

Quoted 7 lines
drivers/i2c/busses/i2c-ls2x.c new file +7 -0
+static int ls2x_i2c_xfer_one(struct i2c_adapter *adap,
+			     struct i2c_msg *msg, bool stop)
+{
+	int ret;
+	bool is_read = msg->flags & I2C_M_RD;
+	struct ls2x_i2c_priv *priv = i2c_get_adapdata(adap);
+

drivers/i2c/busses/i2c-ls2x.c line 232

Quoted 10 lines
drivers/i2c/busses/i2c-ls2x.c new file +9 -0
+	/* Contains steps to send start condition and address */
+	ret = ls2x_i2c_start(adap, msg);
+	if (!ret) {
+		if (is_read)
+			ret = ls2x_i2c_rx(adap, msg->buf, msg->len);
+		else
+			ret = ls2x_i2c_tx(adap, msg->buf, msg->len);
 
+		if (!ret && stop)
+			ret = ls2x_i2c_stop(adap);

So, we will send stop here...

drivers/i2c/busses/i2c-ls2x.c line 236

Quoted 4 lines
drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+	}
 
+	if (ret == -ENXIO)
+		ls2x_i2c_stop(adap);

...and if it fails, we send it again here. Is it okay?

Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+	else if (ret < 0)
+		ls2x_i2c_init(priv);
+
+	return ret;
+}

...

drivers/i2c/busses/i2c-ls2x.c line 250

Quoted 1 line
drivers/i2c/busses/i2c-ls2x.c new file +1 -0
+		ret = ls2x_i2c_xfer_one(adap, msg, msg == (emsg - 1));

Too many parentheses, isn't it?

Quoted 2 lines
(quoted diff) +2 -0
+		if (ret)
+			return ret;

...

drivers/i2c/busses/i2c-ls2x.c line 304

Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2 -0
+	r = devm_request_irq(dev, irq, ls2x_i2c_isr, IRQF_SHARED, "ls2x-i2c",
+			     priv);

Everywhere else you use 'ret', why is 'r' here?

Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2 -0
+	if (r < 0)
+		return dev_err_probe(dev, r, "Unable to request irq %d\n", irq);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH V7 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller

Hi Andy:

Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou

This I2C module is integrated into the Loongson-2K SoCs and Loongson LS7A bridge chip.

Almost there, see my comments below (note, you have ~1w of time before this can be applied anyway, so take you time for carefully addressing comments, (re-)testing, etc.).

...

Quoted 7 lines

drivers/i2c/busses/Kconfig config I2C_OWL · line 891

Quoted 5 lines
drivers/i2c/busses/Kconfig +1 -0
@@ -888,6 +888,17 @@ config I2C_OWL
        Say Y here if you want to use the I2C bus controller on
        the Actions Semiconductor Owl SoC's.
 
+config I2C_LS2X

I believe in Latin alphabet L goes before O...

Sorry, I will fix it.

Quoted 48 lines
Quoted 14 lines
drivers/i2c/busses/Kconfig +10 -0
+     tristate "Loongson LS2X I2C adapter"
+     depends on MACH_LOONGSON64 || COMPILE_TEST
+     help
+       If you say yes to this option, support will be included for the
+       I2C interface on the Loongson-2K SoCs and Loongson LS7A bridge
+       chip.
+
+       This driver can also be built as a module. If so, the module
+       will be called i2c-ls2x.
+
 config I2C_PASEMI
      tristate "PA Semi SMBus interface"
      depends on PPC_PASEMI && PCI

...

Quoted 4 lines

obj-$(CONFIG_I2C_MXS) += i2c-mxs.o
obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o
obj-$(CONFIG_I2C_NPCM) += i2c-npcm7xx.o
+obj-$(CONFIG_I2C_LS2X) += i2c-ls2x.o

...and even before n and m.

Quoted 3 lines

obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o
obj-$(CONFIG_I2C_OMAP) += i2c-omap.o
obj-$(CONFIG_I2C_OWL) += i2c-owl.o

...

Quoted 7 lines
drivers/i2c/busses/i2c-ls2x.c new file +7 -0
+static int ls2x_i2c_xfer_one(struct i2c_adapter *adap,
+                          struct i2c_msg *msg, bool stop)
+{
+     int ret;
+     bool is_read = msg->flags & I2C_M_RD;
+     struct ls2x_i2c_priv *priv = i2c_get_adapdata(adap);
+

drivers/i2c/busses/i2c-ls2x.c line 232

Quoted 10 lines
drivers/i2c/busses/i2c-ls2x.c new file +9 -0
+     /* Contains steps to send start condition and address */
+     ret = ls2x_i2c_start(adap, msg);
+     if (!ret) {
+             if (is_read)
+                     ret = ls2x_i2c_rx(adap, msg->buf, msg->len);
+             else
+                     ret = ls2x_i2c_tx(adap, msg->buf, msg->len);
 
+             if (!ret && stop)
+                     ret = ls2x_i2c_stop(adap);
Quoted 8 lines

So, we will send stop here...

drivers/i2c/busses/i2c-ls2x.c line 236

Quoted 4 lines
drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+     }
 
+     if (ret == -ENXIO)
+             ls2x_i2c_stop(adap);

...and if it fails, we send it again here. Is it okay?

First of all, I think the logic here is fine:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.

The key point is that the current implementation of the stop function is probably imperfect, it directly ignores the NOACK. The more reasonable method would be to judge by LS2X_SR_BUSY(bus busy state). The basic idea: if the i2c bus is detected as idle within the timeout range of the stop command issued, the stop command is considered successful, otherwise -ETIMEDOUT is returned.

As follows:
static int ls2x_i2c_stop(struct i2c_adapter *adap) {

-       int ret = ls2x_i2c_send_byte(adap, LS2X_CR_STOP);
+       u8 value;
+       struct ls2x_i2c_priv *priv = i2c_get_adapdata(adap);
-       return (ret == -ENXIO) ? 0 : ret;
+       writeb(LS2X_CR_STOP, priv->base + I2C_LS2X_CR);
+       return readl_poll_timeout(priv->base + I2C_LS2X_SR,

+ value, !(value & LS2X_SR_BUSY),
+ 100, jiffies_to_usecs(adap->timeout));
}

Now, in the case you mentioned above, the last msg transmission is completed and the stop command is sent. If the stop command fails, it will only return -ETIMEDOUT and call ls2x_i2c_init().

Of course, I will use tools like i2c-tools to do more tests.

Thanks for the detailed review.

Binbin

Quoted 34 lines
Quoted 6 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+     else if (ret < 0)
+             ls2x_i2c_init(priv);
+
+     return ret;
+}

...

Quoted 1 line

+ ret = ls2x_i2c_xfer_one(adap, msg, msg == (emsg - 1));

Too many parentheses, isn't it?

Quoted 2 lines
(quoted diff) +2 -0
+             if (ret)
+                     return ret;

...

drivers/i2c/busses/i2c-ls2x.c line 304

Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2 -0
+     r = devm_request_irq(dev, irq, ls2x_i2c_isr, IRQF_SHARED, "ls2x-i2c",
+                          priv);

Everywhere else you use 'ret', why is 'r' here?

drivers/i2c/busses/i2c-ls2x.c line 306

Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2 -0
+     if (r < 0)
+             return dev_err_probe(dev, r, "Unable to request irq %d\n", irq);

--
With Best Regards,
Andy Shevchenko

#6 lore raw

Re: [PATCH V7 3/4] i2c: ls2x: Add driver for Loongson-2K/LS7A I2C controller

Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko

On Mon, Dec 19, 2022 at 08:28:33PM +0800, Binbin Zhou wrote:

...

Quoted 25 lines
Quoted 25 lines
Quoted 7 lines
drivers/i2c/busses/i2c-ls2x.c new file +7 -0
+static int ls2x_i2c_xfer_one(struct i2c_adapter *adap,
+                          struct i2c_msg *msg, bool stop)
+{
+     int ret;
+     bool is_read = msg->flags & I2C_M_RD;
+     struct ls2x_i2c_priv *priv = i2c_get_adapdata(adap);
+

drivers/i2c/busses/i2c-ls2x.c line 232

Quoted 10 lines
drivers/i2c/busses/i2c-ls2x.c new file +9 -0
+     /* Contains steps to send start condition and address */
+     ret = ls2x_i2c_start(adap, msg);
+     if (!ret) {
+             if (is_read)
+                     ret = ls2x_i2c_rx(adap, msg->buf, msg->len);
+             else
+                     ret = ls2x_i2c_tx(adap, msg->buf, msg->len);
 
+             if (!ret && stop)
+                     ret = ls2x_i2c_stop(adap);

So, we will send stop here...

Quoted 4 lines
drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+     }
 
+     if (ret == -ENXIO)
+             ls2x_i2c_stop(adap);
Quoted 8 lines
Quoted 1 line

...and if it fails, we send it again here. Is it okay?

First of all, I think the logic here is fine:
In the first case, stop is called when the last msg is transmitted successfully; In the second case, stop is called when there is a NOACK during the transmission;
In the third case, init is called when other errors occur during the transmission, such as TIMEOUT.

This is not obvious from the code above. Maybe the comment is missing, maybe as you said the function should be amended and the code is refactored accordingly.

Quoted 21 lines

The key point is that the current implementation of the stop function is probably imperfect, it directly ignores the NOACK. The more reasonable method would be to judge by LS2X_SR_BUSY(bus busy state). The basic idea: if the i2c bus is detected as idle within the timeout range of the stop command issued, the stop command is considered successful, otherwise -ETIMEDOUT is returned.

As follows:
static int ls2x_i2c_stop(struct i2c_adapter *adap) {

(quoted diff) +6 -2
-       int ret = ls2x_i2c_send_byte(adap, LS2X_CR_STOP);
+       u8 value;
+       struct ls2x_i2c_priv *priv = i2c_get_adapdata(adap);
 
-       return (ret == -ENXIO) ? 0 : ret;
+       writeb(LS2X_CR_STOP, priv->base + I2C_LS2X_CR);
+       return readl_poll_timeout(priv->base + I2C_LS2X_SR,
+                                 value, !(value & LS2X_SR_BUSY),
+                                 100, jiffies_to_usecs(adap->timeout));
 }
Quoted 7 lines

Now, in the case you mentioned above, the last msg transmission is completed and the stop command is sent. If the stop command fails, it will only return -ETIMEDOUT and call ls2x_i2c_init().

Of course, I will use tools like i2c-tools to do more tests.

Thanks for the detailed review.

You're welcome!

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+     else if (ret < 0)
+             ls2x_i2c_init(priv);
+
+     return ret;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#7 lore raw

PATCH v7 4/4 LoongArch: Enable LS2X I2C in loongson3_defconfig

In reply to Binbin Zhou

This is now supported, enable for Loongson-3 systems. Other systems are unaffected.

---
 arch/loongarch/configs/loongson3_defconfig | 1 +
 1 file changed, 1 insertion(+)
arch/loongarch/configs/loongson3_defconfig +1 -0
index cb52774c80e8..5677c4f8576e 100644
@@ -602,6 +602,7 @@ CONFIG_HW_RANDOM_VIRTIO=m
 CONFIG_I2C_CHARDEV=y
 CONFIG_I2C_PIIX4=y
 CONFIG_I2C_GPIO=y
+CONFIG_I2C_LS2X=y
 CONFIG_SPI=y
 CONFIG_GPIO_SYSFS=y
 CONFIG_GPIO_LOONGSON=y
Signature · 1 line
2.31.1