Skip to contentSkip to conversation

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

PATCH Superseded Binbin Zhou 3 participants 12 messages Dec 6, 2022 – Dec 12, 2022

Subsystems (5)

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Binbin Zhou

author maintainer
#0 lore raw

v5 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 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(), The continuous I2C

    read/write test passes on the corresponding machine;
  - 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                 |  27 +-
 drivers/i2c/busses/i2c-ls2x.c                 | 379 ++++++++++++++++++
 6 files changed, 460 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 v5 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 | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-gpio.c +17 -10
index 0e4385a9bcf7..7bec473cd6b1 100644
@@ -13,7 +13,6 @@
 #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/slab.h>
@@ -300,22 +299,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 +373,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 +489,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
#5 lore raw

PATCH v5 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
#6 lore raw

PATCH v5 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 | 379 ++++++++++++++++++++++++++++++++++
 3 files changed, 391 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 +379 -0

379 lines not on this page · raw message

Signature · 1 line
2.31.1
author maintainer
#11 lore raw

PATCH v5 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 cd8e003d885e..017eb0e36738 100644
@@ -600,6 +600,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
#2 lore raw

Re: [PATCH V5 1/4] i2c: gpio: Add support on ACPI-based system

In reply to Binbin Zhou
Quoted 2 lines · Binbin Zhou

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

...

Quoted 1 line

-#include <linux/of.h>

Cool, but don't you need to add property.h?

Otherwise looks good to me.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

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

In reply to Binbin Zhou
Quoted 2 lines · Binbin Zhou

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

Much better, thanks!

...

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

Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c new file +8 -0
+/*
+ * The I2C controller has a fixed I2C bus frequency by default, but to
+ * be compatible with more client devices, we can obtain the set I2C
+ * bus frequency from ACPI or FDT.
+ */
+static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
+{
+	u16 val = 0x12c; /* Default value of I2C divider latch register */

Besides comment better to be placed on top of the commented line, the value is better to have its own definition where you place the comment and elaborate what it means in practice (The clock frequency is changed? Bus speed is different?)

Quoted 11 lines
drivers/i2c/busses/i2c-ls2x.c new file +11 -0
+	struct i2c_timings *t = &priv->i2c_t;
+	u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);
+
+	i2c_parse_fw_timings(priv->dev, t, false);
+
+	if (acpi_speed || t->bus_freq_hz)
+		val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1;
+
+	/* Set LS2X I2C frequency */
+	writel(val, priv->base + I2C_LS2X_PRER_LO);
+}

...

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

Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2 -0
+	writeb(data | LS2X_CTR_EN | LS2X_CTR_IEN | LS2X_CTR_MST,
+			priv->base + I2C_LS2X_CTR);

Wrong indentation.

...

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

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

You requested IRQ without filling all data structures. Is it fine? Have you checked that with CONFIG_DEBUG_SHIRQ being enabled?

...

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

Quoted 6 lines
drivers/i2c/busses/i2c-ls2x.c new file +6 -0
+	r = devm_i2c_add_adapter(dev, adap);
+	if (r)
+		return dev_err_probe(dev, r, "Failure adding adapter\n");
+
+	return 0;
+}

Looking at the above...

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

Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+static int ls2x_i2c_remove(struct platform_device *pdev)
+{
+	struct ls2x_i2c_priv *priv = platform_get_drvdata(pdev);
+
+	i2c_del_adapter(&priv->adapter);

...are you sure this is correct?

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

...

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

Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +4 -0
+static int ls2x_i2c_suspend(struct device *dev)
+{
+	struct ls2x_i2c_priv *priv = dev_get_drvdata(dev);
 
+	priv->suspended = 1;

No protection needed?

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

Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c new file +7 -0
+	return 0;
+}
+
+static int ls2x_i2c_resume(struct device *dev)
+{
+	struct ls2x_i2c_priv *priv = dev_get_drvdata(dev);
 
+	priv->suspended = 0;

Ditto.

Quoted 3 lines
drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+	ls2x_i2c_reginit(priv);
+	return 0;
+}

...

Quoted 1 line

+MODULE_ALIAS("platform:ls2x-i2c");

Why is this required?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#3 lore raw

Re: [PATCH V5 1/4] i2c: gpio: Add support on ACPI-based system

In reply to Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 2 lines · Binbin Zhou

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

...

Quoted 1 line

-#include <linux/of.h>

Cool, but don't you need to add property.h?

Get it, I should have been more careful, maybe I'm still not very sensitive to header files.

Now I'm still basically judging based on whether I compile with errors or not.

Thanks.

Binbin

Quoted 3 lines

Otherwise looks good to me.

author maintainer
#8 lore raw

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

In reply to Andy Shevchenko

Hi Andy:

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

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

Much better, thanks!

...

Quoted 12 lines

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

Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c new file +8 -0
+/*
+ * The I2C controller has a fixed I2C bus frequency by default, but to
+ * be compatible with more client devices, we can obtain the set I2C
+ * bus frequency from ACPI or FDT.
+ */
+static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
+{
+	u16 val = 0x12c; /* Default value of I2C divider latch register */

Besides comment better to be placed on top of the commented line, the value is better to have its own definition where you place the comment and elaborate what it means in practice (The clock frequency is changed? Bus speed is different?)

Ok, I'll put this comment on a separate line.

The LS2X I2C supports STANDARD_MODE and FAST_MODE, so the maximum bus frequency is 400kHz. "0x12c" is our empirical value after experimentation and represents 33KHz.

Also, I think the better way is:

@@ -53,6 +53,15 @@
 #define LS2X_CTR_IEN           BIT(6) /* Enable i2c interrupt */ #define LS2X_CTR_MST           BIT(5) /* 0: Slave mode 1: Master mode */

+/* The PCLK clock frequency input from the LPB bus */
+#define LS2X_I2C_PCLK_FREQ     (50 * HZ_PER_MHZ)
+/*
+ * The LS2X I2C controller supports standard mode and fast mode,
+ * so the maximum bus frequency is 400kHz.
+ * The '33KHz' is our empirical value after experimentation.
+ */
+#define LS2X_I2C_FREQ_STD      (33 * HZ_PER_KHZ)
+
 struct ls2x_i2c_priv {
        struct i2c_adapter      adapter;
        struct device           *dev;
@@ -231,17 +240,19 @@ static irqreturn_t ls2x_i2c_irq_handler(int this_irq, void *dev_id) */
 static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
 {
-       u16 val = 0x12c; /* Default value of I2C divider latch register */ struct i2c_timings *t = &priv->i2c_t;
        u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);

        i2c_parse_fw_timings(priv->dev, t, false);

        if (acpi_speed || t->bus_freq_hz)
-               val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1; +               t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed); +       else
+               t->bus_freq_hz = LS2X_I2C_FREQ_STD;

-       /* Set LS2X I2C frequency */
-       writel(val, priv->base + I2C_LS2X_PRER_LO);
+       /* Calculate and set LS2X I2C frequency */
+       writel((LS2X_I2C_PCLK_FREQ / (5 * t->bus_freq_hz) - 1), +              priv->base + I2C_LS2X_PRER_LO);
 }

Quoted 17 lines
Quoted 11 lines
drivers/i2c/busses/i2c-ls2x.c new file +11 -0
+	struct i2c_timings *t = &priv->i2c_t;
+	u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);
+
+	i2c_parse_fw_timings(priv->dev, t, false);
+
+	if (acpi_speed || t->bus_freq_hz)
+		val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1;
+
+	/* Set LS2X I2C frequency */
+	writel(val, priv->base + I2C_LS2X_PRER_LO);
+}

...

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

Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2 -0
+	writeb(data | LS2X_CTR_EN | LS2X_CTR_IEN | LS2X_CTR_MST,
+			priv->base + I2C_LS2X_CTR);

Wrong indentation.

Quoted 8 lines

...

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

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

You requested IRQ without filling all data structures. Is it fine? Have you checked that with CONFIG_DEBUG_SHIRQ being enabled?

Sorry, I don't quite understand what you mean by "without filling all data structures", I need call ls2x_i2c_reginit(priv) before it ?

I see that other i2c drivers request interrupts at about the same time as I do.

I tested it with CONFIG_DEBUG_SHIRQ and no exceptions were reported.

Quoted 10 lines

...

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

Quoted 6 lines
drivers/i2c/busses/i2c-ls2x.c new file +6 -0
+	r = devm_i2c_add_adapter(dev, adap);
+	if (r)
+		return dev_err_probe(dev, r, "Failure adding adapter\n");
+
+	return 0;
+}

Looking at the above...

Quoted 6 lines

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

Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+static int ls2x_i2c_remove(struct platform_device *pdev)
+{
+	struct ls2x_i2c_priv *priv = platform_get_drvdata(pdev);
+
+	i2c_del_adapter(&priv->adapter);

...are you sure this is correct?

When we use devm_i2c_add_adapter(), the adapter will be auto deleted on driver detach.

So I just drop the ls2x_i2c_remove() ?

Quoted 9 lines
Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}

...

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

Quoted 4 lines
drivers/i2c/busses/i2c-ls2x.c new file +4 -0
+static int ls2x_i2c_suspend(struct device *dev)
+{
+	struct ls2x_i2c_priv *priv = dev_get_drvdata(dev);
+	priv->suspended = 1;

No protection needed?

Actually this variable is not used elsewhere, maybe it is useless, I will try to remove it and add some necessary actions in the suspend/rusume callbacks, such as disable i2c interrupts, to ensure integrity.

Quoted 9 lines

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

Quoted 7 lines
drivers/i2c/busses/i2c-ls2x.c new file +7 -0
+	return 0;
+}
+
+static int ls2x_i2c_resume(struct device *dev)
+{
+	struct ls2x_i2c_priv *priv = dev_get_drvdata(dev);
+	priv->suspended = 0;

Ditto.

Quoted 7 lines
Quoted 3 lines
drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+	ls2x_i2c_reginit(priv);
+	return 0;
+}

...

Quoted 1 line

+MODULE_ALIAS("platform:ls2x-i2c");

Why is this required?

I just referred to other drivers before, and now the MODULE_DEVICE_TABLE already creates proper alias for platform driver.

I will drop it.

Thanks.

Binbin

#4 lore raw

Re: [PATCH V5 1/4] i2c: gpio: Add support on ACPI-based system

In reply to Binbin Zhou
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko

On Tue, Dec 06, 2022 at 11:16:54AM +0800, Binbin Zhou wrote:

...

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

-#include <linux/of.h>

Cool, but don't you need to add property.h?

Get it, I should have been more careful, maybe I'm still not very sensitive to header files.

Now I'm still basically judging based on whether I compile with errors or not.

You can generate the include tree by running make includecheck

The rule of thumb is to include what you are the direct user of. With a few exceptions where we have the guarantees that one header is always included by another (e.g. bits.h included by bitops.h).

Quoted 1 line
Quoted 1 line

Otherwise looks good to me.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

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

In reply to Binbin Zhou
Quoted 2 lines · Binbin Zhou
Quoted 1 line · Andy Shevchenko

On Tue, Dec 06, 2022 at 11:16:56AM +0800, Binbin Zhou wrote:

...

Quoted 49 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * The I2C controller has a fixed I2C bus frequency by default, but to
+ * be compatible with more client devices, we can obtain the set I2C
+ * bus frequency from ACPI or FDT.

Quoted 8 lines

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

Quoted 4 lines

+ */

drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
+{
+	u16 val = 0x12c; /* Default value of I2C divider latch register */

Besides comment better to be placed on top of the commented line, the value is better to have its own definition where you place the comment and elaborate what it means in practice (The clock frequency is changed? Bus speed is different?)

Ok, I'll put this comment on a separate line.

The LS2X I2C supports STANDARD_MODE and FAST_MODE, so the maximum bus frequency is 400kHz.
"0x12c" is our empirical value after experimentation and represents 33KHz.

Also, I think the better way is:

(quoted diff) +9 -1
@@ -53,6 +53,15 @@
  #define LS2X_CTR_IEN           BIT(6) /* Enable i2c interrupt */
  #define LS2X_CTR_MST           BIT(5) /* 0: Slave mode 1: Master mode */
 
+/* The PCLK clock frequency input from the LPB bus */
+#define LS2X_I2C_PCLK_FREQ     (50 * HZ_PER_MHZ)
+/*
+ * The LS2X I2C controller supports standard mode and fast mode,
+ * so the maximum bus frequency is 400kHz.
+ * The '33KHz' is our empirical value after experimentation.
+ */
+#define LS2X_I2C_FREQ_STD      (33 * HZ_PER_KHZ)
+
  struct ls2x_i2c_priv {
         struct i2c_adapter      adapter;
         struct device           *dev;
@@ -231,17 +240,19 @@ static irqreturn_t ls2x_i2c_irq_handler(int this_irq,
 void *dev_id)
   */
  static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
  {
-       u16 val = 0x12c; /* Default value of I2C divider latch register */
         struct i2c_timings *t = &priv->i2c_t;
         u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);
 
         i2c_parse_fw_timings(priv->dev, t, false);
 
         if (acpi_speed || t->bus_freq_hz)

Review static irqreturn_t ls2x_i2c_irq_handler(int this_irq, · line 256

Quoted 10 lines
(quoted diff) +5 -3
-               val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1;
+               t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
+       else
+               t->bus_freq_hz = LS2X_I2C_FREQ_STD;
 
-       /* Set LS2X I2C frequency */
-       writel(val, priv->base + I2C_LS2X_PRER_LO);
+       /* Calculate and set LS2X I2C frequency */
 
+       writel((LS2X_I2C_PCLK_FREQ / (5 * t->bus_freq_hz) - 1),

Fine with me, but drop unneeded parentheses.

Quoted 14 lines
(quoted diff) +1 -0
+              priv->base + I2C_LS2X_PRER_LO);
  }
Quoted 12 lines
Quoted 12 lines
drivers/i2c/busses/i2c-ls2x.c new file +11 -0
+	struct i2c_timings *t = &priv->i2c_t;
+	u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);
+
+	i2c_parse_fw_timings(priv->dev, t, false);
+
+	if (acpi_speed || t->bus_freq_hz)
+		val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1;
+
+	/* Set LS2X I2C frequency */
+	writel(val, priv->base + I2C_LS2X_PRER_LO);
+}

...

Quoted 9 lines
Quoted 6 lines

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

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

You requested IRQ without filling all data structures. Is it fine? Have you checked that with CONFIG_DEBUG_SHIRQ being enabled?

Sorry, I don't quite understand what you mean by "without filling all data structures", I need call ls2x_i2c_reginit(priv) before it ?

When you register an IRQ handler (which is that call) it needs to be prepared to handle interrupt immediately. Which means that your data structures has to be filled properly. If you can guarantee that with the current code, fine then.

Quoted 4 lines

I see that other i2c drivers request interrupts at about the same time as I do.

I tested it with CONFIG_DEBUG_SHIRQ and no exceptions were reported.

Good. And you removed and reinserted module?

At least this helps to detect some of the potential issues.

...

Quoted 15 lines
Quoted 8 lines

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

Quoted 6 lines
drivers/i2c/busses/i2c-ls2x.c new file +6 -0
+	r = devm_i2c_add_adapter(dev, adap);
+	if (r)
+		return dev_err_probe(dev, r, "Failure adding adapter\n");
+
+	return 0;
+}

Looking at the above...

Quoted 6 lines

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

Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+static int ls2x_i2c_remove(struct platform_device *pdev)
+{
+	struct ls2x_i2c_priv *priv = platform_get_drvdata(pdev);
+
+	i2c_del_adapter(&priv->adapter);

...are you sure this is correct?

Quoted 4 lines

When we use devm_i2c_add_adapter(), the adapter will be auto deleted on driver detach.

So I just drop the ls2x_i2c_remove() ?

Correct.

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

...

Quoted 9 lines
Quoted 5 lines

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

Quoted 4 lines
drivers/i2c/busses/i2c-ls2x.c new file +4 -0
+static int ls2x_i2c_suspend(struct device *dev)
+{
+	struct ls2x_i2c_priv *priv = dev_get_drvdata(dev);
+	priv->suspended = 1;

No protection needed?

Actually this variable is not used elsewhere, maybe it is useless, I will try to remove it and add some necessary actions in the suspend/rusume callbacks, such as disable i2c interrupts, to ensure integrity.

Is your interrupt a wake source? It might be that you will need a special handling of it.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

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

Hi Andy:

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

On Tue, Dec 06, 2022 at 11:16:56AM +0800, Binbin Zhou wrote:

...

Quoted 49 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * The I2C controller has a fixed I2C bus frequency by default, but to
+ * be compatible with more client devices, we can obtain the set I2C
+ * bus frequency from ACPI or FDT.

Quoted 8 lines

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

Quoted 4 lines

+ */

drivers/i2c/busses/i2c-ls2x.c new file +3 -0
+static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
+{
+ u16 val = 0x12c; /* Default value of I2C divider latch register */

Besides comment better to be placed on top of the commented line, the value is better to have its own definition where you place the comment and elaborate what it means in practice (The clock frequency is changed? Bus speed is different?)

Ok, I'll put this comment on a separate line.

The LS2X I2C supports STANDARD_MODE and FAST_MODE, so the maximum bus frequency is 400kHz.
"0x12c" is our empirical value after experimentation and represents 33KHz.

Also, I think the better way is:

(quoted diff) +9 -1
@@ -53,6 +53,15 @@
 #define LS2X_CTR_IEN           BIT(6) /* Enable i2c interrupt */
 #define LS2X_CTR_MST           BIT(5) /* 0: Slave mode 1: Master mode */
 
+/* The PCLK clock frequency input from the LPB bus */
+#define LS2X_I2C_PCLK_FREQ     (50 * HZ_PER_MHZ)
+/*
+ * The LS2X I2C controller supports standard mode and fast mode,
+ * so the maximum bus frequency is 400kHz.
+ * The '33KHz' is our empirical value after experimentation.
+ */
+#define LS2X_I2C_FREQ_STD      (33 * HZ_PER_KHZ)
+
 struct ls2x_i2c_priv {
        struct i2c_adapter      adapter;
        struct device           *dev;
@@ -231,17 +240,19 @@ static irqreturn_t ls2x_i2c_irq_handler(int this_irq,
 void *dev_id)
  */
 static void ls2x_i2c_adjust_bus_speed(struct ls2x_i2c_priv *priv)
 {
-       u16 val = 0x12c; /* Default value of I2C divider latch register */
        struct i2c_timings *t = &priv->i2c_t;
        u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);
 
        i2c_parse_fw_timings(priv->dev, t, false);
 
        if (acpi_speed || t->bus_freq_hz)

Review static irqreturn_t ls2x_i2c_irq_handler(int this_irq, · line 256

Quoted 10 lines
(quoted diff) +5 -3
-               val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1;
+               t->bus_freq_hz = max(t->bus_freq_hz, acpi_speed);
+       else
+               t->bus_freq_hz = LS2X_I2C_FREQ_STD;
 
-       /* Set LS2X I2C frequency */
-       writel(val, priv->base + I2C_LS2X_PRER_LO);
+       /* Calculate and set LS2X I2C frequency */
 
+       writel((LS2X_I2C_PCLK_FREQ / (5 * t->bus_freq_hz) - 1),

Fine with me, but drop unneeded parentheses.

Quoted 14 lines
(quoted diff) +1 -0
+              priv->base + I2C_LS2X_PRER_LO);
 }
Quoted 12 lines
Quoted 12 lines

+ struct i2c_timings *t = &priv->i2c_t;
+ u32 acpi_speed = i2c_acpi_find_bus_speed(priv->dev);

drivers/i2c/busses/i2c-ls2x.c new file +9 -0
+
+ i2c_parse_fw_timings(priv->dev, t, false);
+
+ if (acpi_speed || t->bus_freq_hz)
+         val = 10 * HZ_PER_MHZ / max(t->bus_freq_hz, acpi_speed) - 1;
+
+ /* Set LS2X I2C frequency */
+ writel(val, priv->base + I2C_LS2X_PRER_LO);
+}

...

Quoted 7 lines
Quoted 6 lines

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

Quoted 4 lines

+ r = devm_request_irq(dev, irq, ls2x_i2c_irq_handler,

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

You requested IRQ without filling all data structures. Is it fine? Have you checked that with CONFIG_DEBUG_SHIRQ being enabled?

Quoted 6 lines
Quoted 2 lines

Sorry, I don't quite understand what you mean by "without filling all data structures", I need call ls2x_i2c_reginit(priv) before it ?

When you register an IRQ handler (which is that call) it needs to be prepared to handle interrupt immediately. Which means that your data structures has to be filled properly. If you can guarantee that with the current code, fine then.

Emm. If so, I think it might be safer to put ls2x_i2c_reginit() in front of it, since the bus frequency needs to be set correctly.

Quoted 9 lines
Quoted 5 lines

I see that other i2c drivers request interrupts at about the same time as I do.

I tested it with CONFIG_DEBUG_SHIRQ and no exceptions were reported.

Good. And you removed and reinserted module?

At least this helps to detect some of the potential issues.

Yes, all of these seem normal.

Quoted 21 lines

...

Quoted 18 lines
Quoted 8 lines

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

Quoted 6 lines

+ r = devm_i2c_add_adapter(dev, adap);
+ if (r)

drivers/i2c/busses/i2c-ls2x.c new file +4 -0
+         return dev_err_probe(dev, r, "Failure adding adapter\n");
+
+ return 0;
+}

Looking at the above...

Quoted 6 lines

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

Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5 -0
+static int ls2x_i2c_remove(struct platform_device *pdev)
+{
+ struct ls2x_i2c_priv *priv = platform_get_drvdata(pdev);
+
+ i2c_del_adapter(&priv->adapter);

...are you sure this is correct?

When we use devm_i2c_add_adapter(), the adapter will be auto deleted on driver detach.

Quoted 14 lines
Quoted 1 line

So I just drop the ls2x_i2c_remove() ?

Correct.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ return 0;
+}

...

Quoted 5 lines
Quoted 5 lines

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

Quoted 4 lines
drivers/i2c/busses/i2c-ls2x.c new file +4 -0
+static int ls2x_i2c_suspend(struct device *dev)
+{
+ struct ls2x_i2c_priv *priv = dev_get_drvdata(dev);
+ priv->suspended = 1;

No protection needed?

.> >

Quoted 6 lines
Quoted 3 lines

Actually this variable is not used elsewhere, maybe it is useless, I will try to remove it and add some necessary actions in the suspend/rusume callbacks, such as disable i2c interrupts, to ensure integrity.

Is your interrupt a wake source? It might be that you will need a special handling of it.

No. It isn't a wake source.
I think it is enough to clear the interrupt enable bit in the control register. As follows:
writeb(readb(priv->base + I2C_LS2X_CTR) & ~LS2X_CTR_IEN,

               priv->base + I2C_LS2X_CTR);

Thanks.
Binbin

Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+ return 0;
+}

--
With Best Regards,
Andy Shevchenko