Skip to contentSkip to conversation

i2c: viai2c: turn common code into a proper module

PATCH In review Arnd Bergmann 4 participants 6 messages May 28, 2024 – Jun 26, 2024 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: viai2c: turn common code into a proper module

From: Arnd Bergmann <[email protected]>

The i2c-viai2c-common.c file is used by two drivers, but is not a proper abstraction and can get linked into both modules in the same configuration, which results in a warning:

scripts/Makefile.build:236: drivers/i2c/busses/Makefile: i2c-viai2c-common.o is added to multiple modules: i2c-wmt i2c-zhaoxin

The other problems with this include the incorrect use of a __weak function when both are built-in, and the fact that the "common" module is sprinked with 'if (i2c->plat == ...)' checks that have knowledge about the differences between the drivers using it.

Avoid the link time warning by making the common driver a proper module with MODULE_LICENCE()/MODULE_AUTHOR() tags, and remove the __weak function by slightly rearranging the code.

This adds a little more duplication between the two main drivers, but those versions get more readable in the process.

Fixes a06b80e83011 ("i2c: add zhaoxin i2c controller driver")
---
 drivers/i2c/busses/Makefile             |   6 +-
 drivers/i2c/busses/i2c-viai2c-common.c  |  71 ++-------------
 drivers/i2c/busses/i2c-viai2c-common.h  |   2 +-
 drivers/i2c/busses/i2c-viai2c-wmt.c     |  37 ++++++++
 drivers/i2c/busses/i2c-viai2c-zhaoxin.c | 113 +++++++++++++++++++-----
 5 files changed, 140 insertions(+), 89 deletions(-)
drivers/i2c/busses/Makefile +2 -4
index 3d65934f5eb4..78d0561339e5 100644
@@ -29,8 +29,7 @@ obj-$(CONFIG_I2C_SIS630)	+= i2c-sis630.o
 obj-$(CONFIG_I2C_SIS96X)	+= i2c-sis96x.o
 obj-$(CONFIG_I2C_VIA)		+= i2c-via.o
 obj-$(CONFIG_I2C_VIAPRO)	+= i2c-viapro.o
-i2c-zhaoxin-objs := i2c-viai2c-zhaoxin.o i2c-viai2c-common.o
-obj-$(CONFIG_I2C_ZHAOXIN)	+= i2c-zhaoxin.o
+obj-$(CONFIG_I2C_ZHAOXIN)	+= i2c-viai2c-zhaoxin.o i2c-viai2c-common.o
 
 # Mac SMBus host controller drivers
 obj-$(CONFIG_I2C_HYDRA)		+= i2c-hydra.o
@@ -120,8 +119,7 @@ obj-$(CONFIG_I2C_TEGRA_BPMP)	+= i2c-tegra-bpmp.o
 obj-$(CONFIG_I2C_UNIPHIER)	+= i2c-uniphier.o
 obj-$(CONFIG_I2C_UNIPHIER_F)	+= i2c-uniphier-f.o
 obj-$(CONFIG_I2C_VERSATILE)	+= i2c-versatile.o
-i2c-wmt-objs := i2c-viai2c-wmt.o i2c-viai2c-common.o
-obj-$(CONFIG_I2C_WMT)		+= i2c-wmt.o
+obj-$(CONFIG_I2C_WMT)		+= i2c-viai2c-wmt.o i2c-viai2c-common.o
 i2c-octeon-objs := i2c-octeon-core.o i2c-octeon-platdrv.o
 obj-$(CONFIG_I2C_OCTEON)	+= i2c-octeon.o
 i2c-thunderx-objs := i2c-octeon-core.o i2c-thunderx-pcidrv.o
drivers/i2c/busses/i2c-viai2c-common.c +9 -62

110 lines not on this page · raw message

drivers/i2c/busses/i2c-viai2c-common.h +1 -1
index 81e827c54434..00f17733223c 100644
@@ -80,6 +80,6 @@ struct viai2c {
 int viai2c_wait_bus_not_busy(struct viai2c *i2c);
 int viai2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num);
 int viai2c_init(struct platform_device *pdev, struct viai2c **pi2c, int plat);
-int viai2c_fifo_irq_xfer(struct viai2c *i2c, bool irq);
+int viai2c_irq_xfer(struct viai2c *i2c);
 
 #endif
drivers/i2c/busses/i2c-viai2c-wmt.c +37 -0
index e1988f946026..b3ab9633015a 100644
@@ -72,6 +72,33 @@ static int wmt_i2c_reset_hardware(struct viai2c *i2c)
 	return 0;
 }
 
+static irqreturn_t wmt_i2c_isr(int irq, void *data)
+{
+	struct viai2c *i2c = data;
+	u8 status;
+
+	/* save the status and write-clear it */
+	status = readw(i2c->base + VIAI2C_REG_ISR);
+	writew(status, i2c->base + VIAI2C_REG_ISR);
+
+	i2c->ret = 0;
+	if (status & VIAI2C_ISR_NACK_ADDR)
+		i2c->ret = -EIO;
+
+	if (status & VIAI2C_ISR_SCL_TIMEOUT)
+		i2c->ret = -ETIMEDOUT;
+
+	if (!i2c->ret)
+		i2c->ret = viai2c_irq_xfer(i2c);
+
+	/* All the data has been successfully transferred or error occurred */
+	if (i2c->ret)
+		complete(&i2c->complete);
+
+	return IRQ_HANDLED;
+}
+
+
 static int wmt_i2c_probe(struct platform_device *pdev)
 {
 	struct device_node *np = pdev->dev.of_node;
@@ -84,6 +111,16 @@ static int wmt_i2c_probe(struct platform_device *pdev)
 	if (err)
 		return err;
 
+	i2c->irq = platform_get_irq(pdev, 0);
+	if (i2c->irq < 0)
+		return i2c->irq;
+
+	err = devm_request_irq(&pdev->dev, i2c->irq, wmt_i2c_isr,
+			       0, pdev->name, i2c);
+	if (err)
+		return dev_err_probe(&pdev->dev, err,
+				"failed to request irq %i\n", i2c->irq);
+
 	i2c->clk = of_clk_get(np, 0);
 	if (IS_ERR(i2c->clk)) {
 		dev_err(&pdev->dev, "unable to request clock\n");
drivers/i2c/busses/i2c-viai2c-zhaoxin.c +91 -22

153 lines not on this page · raw message

Signature · 1 line
2.39.2
#2 lore raw

PATCH 1/1 i2c: viai2c: turn common code into a proper module

In reply to a message not in the archive

Hi Arnd,

Quoted 14 lines

From: Arnd Bergmann <[email protected]>

The i2c-viai2c-common.c file is used by two drivers, but is not a proper abstraction and can get linked into both modules in the same configuration, which results in a warning:

scripts/Makefile.build:236: drivers/i2c/busses/Makefile: i2c-viai2c-common.o is added to multiple modules: i2c-wmt i2c-zhaoxin

The other problems with this include the incorrect use of a __weak function when both are built-in, and the fact that the "common" module is sprinked with 'if (i2c->plat == ...)' checks that have knowledge about the differences between the drivers using it.

Avoid the link time warning by making the common driver a proper module with MODULE_LICENCE()/MODULE_AUTHOR() tags, and remove the __weak function by slightly rearranging the code.

This adds a little more duplication between the two main drivers, but those versions get more readable in the process.

Fixes a06b80e83011 ("i2c: add zhaoxin i2c controller driver")
Quoted 7 lines
---
  drivers/i2c/busses/Makefile             |   6 +-
  drivers/i2c/busses/i2c-viai2c-common.c  |  71 ++-------------
  drivers/i2c/busses/i2c-viai2c-common.h  |   2 +-
  drivers/i2c/busses/i2c-viai2c-wmt.c     |  37 ++++++++
  drivers/i2c/busses/i2c-viai2c-zhaoxin.c | 113 +++++++++++++++++++-----
  5 files changed, 140 insertions(+), 89 deletions(-)

I tested this patch and it works fine, so I am OK with it.

Hans

#3 lore raw
In reply to Arnd Bergmann
Quoted 17 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The i2c-viai2c-common.c file is used by two drivers, but is not a proper abstraction and can get linked into both modules in the same configuration, which results in a warning:

scripts/Makefile.build:236: drivers/i2c/busses/Makefile: i2c-viai2c-common.o is added to multiple modules: i2c-wmt i2c-zhaoxin

The other problems with this include the incorrect use of a __weak function when both are built-in, and the fact that the "common" module is sprinked with 'if (i2c->plat == ...)' checks that have knowledge about the differences between the drivers using it.

Avoid the link time warning by making the common driver a proper module with MODULE_LICENCE()/MODULE_AUTHOR() tags, and remove the __weak function by slightly rearranging the code.

Quoted 5 lines

This adds a little more duplication between the two main drivers, but those versions get more readable in the process.

Fixes a06b80e83011 ("i2c: add zhaoxin i2c controller driver")

Andi, I am tempted to include this in my for-current pull request this week. Are you okay with this or do you want to review it more closely?

#4 lore raw
Quoted 18 lines · Wolfram Sang
Quoted 17 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The i2c-viai2c-common.c file is used by two drivers, but is not a proper abstraction and can get linked into both modules in the same configuration, which results in a warning:

scripts/Makefile.build:236: drivers/i2c/busses/Makefile: i2c-viai2c-common.o is added to multiple modules: i2c-wmt i2c-zhaoxin

The other problems with this include the incorrect use of a __weak function when both are built-in, and the fact that the "common" module is sprinked with 'if (i2c->plat == ...)' checks that have knowledge about the differences between the drivers using it.

Avoid the link time warning by making the common driver a proper module with MODULE_LICENCE()/MODULE_AUTHOR() tags, and remove the __weak function by slightly rearranging the code.

Quoted 8 lines
Quoted 5 lines

This adds a little more duplication between the two main drivers, but those versions get more readable in the process.

Fixes a06b80e83011 ("i2c: add zhaoxin i2c controller driver")

Andi, I am tempted to include this in my for-current pull request this week. Are you okay with this or do you want to review it more closely?

Meh, I forgot about it. Andi, do you plan a PR for rc6?

maintainer
#5 lore raw
Quoted 22 lines · Wolfram Sang
Quoted 21 lines · Wolfram Sang
Quoted 20 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The i2c-viai2c-common.c file is used by two drivers, but is not a proper abstraction and can get linked into both modules in the same configuration, which results in a warning:

scripts/Makefile.build:236: drivers/i2c/busses/Makefile: i2c-viai2c-common.o is added to multiple modules: i2c-wmt i2c-zhaoxin

The other problems with this include the incorrect use of a __weak function when both are built-in, and the fact that the "common" module is sprinked with 'if (i2c->plat == ...)' checks that have knowledge about the differences between the drivers using it.

Avoid the link time warning by making the common driver a proper module with MODULE_LICENCE()/MODULE_AUTHOR() tags, and remove the __weak function by slightly rearranging the code.

This adds a little more duplication between the two main drivers, but those versions get more readable in the process.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Fixes a06b80e83011 ("i2c: add zhaoxin i2c controller driver")

Andi, I am tempted to include this in my for-current pull request this week. Are you okay with this or do you want to review it more closely?

Meh, I forgot about it. Andi, do you plan a PR for rc6?

yes, sorry, will take it.

Sorry for having missed it.

Andi

maintainer
#6 lore raw
In reply to Arnd Bergmann

Hi,

...

drivers/i2c/busses/i2c-viai2c-common.c int viai2c_init(struct platform_device *pdev, struct viai2c **pi2c, int plat) · line 202

Quoted 4 lines
drivers/i2c/busses/i2c-viai2c-common.c +4 -0
+EXPORT_SYMBOL_GPL(viai2c_init);
+
+MODULE_DESCRIPTION("Via/Wondermedia/Zhaoxin I2C master-mode bus adapter");
+MODULE_AUTHOR("Tony Prisk <[email protected]>");

Do we want to add also Hans here?

Quoted 1 line

+MODULE_LICENSE("GPL");

...

Quoted 2 lines
drivers/i2c/busses/i2c-viai2c-wmt.c +2 -0
+static irqreturn_t wmt_i2c_isr(int irq, void *data)
+{

...

drivers/i2c/busses/i2c-viai2c-wmt.c static int wmt_i2c_reset_hardware(struct viai2c *i2c) · line 101

Quoted 8 lines
drivers/i2c/busses/i2c-viai2c-wmt.c +8 -0
+	/* All the data has been successfully transferred or error occurred */
+	if (i2c->ret)
+		complete(&i2c->complete);
+
+	return IRQ_HANDLED;
+}
+
+

I took the freedom to remove this double blank line.

Quoted 3 lines

static int wmt_i2c_probe(struct platform_device *pdev) {
struct device_node *np = pdev->dev.of_node;

...

drivers/i2c/busses/i2c-viai2c-zhaoxin.c static int zxi2c_probe(struct platform_device *pdev) · line 306

Quoted 10 lines
drivers/i2c/busses/i2c-viai2c-zhaoxin.c +6 -0
@@ -239,6 +298,16 @@ static int zxi2c_probe(struct platform_device *pdev)
 	if (error)
 		return error;
 
+	i2c->irq = platform_get_irq(pdev, 0);
+	if (i2c->irq < 0)
+		return i2c->irq;
+
+	error = devm_request_irq(&pdev->dev, i2c->irq, zxi2c_isr,
+			       IRQF_SHARED, pdev->name, i2c);

I took the freedom of re-alligning here.

Queued to i2c/i2c-host-fixes with Tested-by Hans.

Thanks,
Andi