Skip to contentSkip to conversation

i2c: microchip: pci1xxxx: Add driver for I2C host controller in multifunction endpoint of pci1xxxx switch

PATCH Superseded Tharun Kumar P 2 participants 3 messages Sep 6, 2022 – Sep 7, 2022

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 i2c: microchip: pci1xxxx: Add driver for I2C host controller in multifunction endpoint of pci1xxxx switch

Microchip pci1xxxx is an unmanaged PCIe3.1a Switch for Consumer, Industrial and Automotive applications. This switch has multiple downstream ports. In one of the Switch's Downstream port, there is a multifunction endpoint for peripherals which includes an I2C host controller. The I2C function in the endpoint operates at 100KHz, 400KHz and 1 MHz and has buffer depth of 128 bytes.
This patch provides the I2C controller driver for the I2C function of the switch.

---
V2 -> V3:
1. Replaced SIMPLE_DEV_PM_OPS with DEFINE_SIMPLE_DEV_PM_OPS
2. Used devm_add_action API to avoid mixing devm and non-devm APIs
---
RFC -> V2:
1. Removed pci_free_irq_vectors API in code since pcim_enable_device is used
2. Added pci1xxxx_i2c_shutdown API in failure case of pci_alloc_irq_vectors and devm_request_irq
3. Used devm variant of i2c_add_adapter
4. Resolved name collision and fixed styling issues in comments
---

 MAINTAINERS                            |    8 +
 drivers/i2c/busses/Kconfig             |   10 +
 drivers/i2c/busses/Makefile            |    1 +
 drivers/i2c/busses/i2c-mchp-pci1xxxx.c | 1227 ++++++++++++++++++++++++
 4 files changed, 1246 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-mchp-pci1xxxx.c
MAINTAINERS +8 -0
index 3cf9842d9233..204885ab5200 100644
@@ -13110,6 +13110,14 @@ S:	Supported
 F:	Documentation/devicetree/bindings/mtd/atmel-nand.txt
 F:	drivers/mtd/nand/raw/atmel/*
 
+MICROCHIP PCI1XXXX I2C DRIVER
+M:	Tharun Kumar P <[email protected]>
+M:	Kumaravel Thiagarajan <[email protected]>
+M:	Microchip Linux Driver Support <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/i2c/busses/i2c-mchp-pci1xxxx.c
+
 MICROCHIP PWM DRIVER
 M:	Claudiu Beznea <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
drivers/i2c/busses/Kconfig +10 -0
index a1bae59208e3..7e967d87dc04 100644
@@ -1244,6 +1244,16 @@ config I2C_PARPORT
 	  This support is also available as a module.  If so, the module
 	  will be called i2c-parport.
 
+config I2C_PCI1XXXX
+	tristate "PCI1XXXX I2C Host Adapter"
+	depends on PCI
+	help
+	  If you say yes to this option, support will be included for
+	  Microchip PCI1XXXX's I2C interface.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called i2c-mchp-pci1xxxx.
+
 config I2C_ROBOTFUZZ_OSIF
 	tristate "RobotFuzz Open Source InterFace USB adapter"
 	depends on USB
drivers/i2c/busses/Makefile +1 -0
index 479f60e4ee3d..2372ed91e7ad 100644
@@ -131,6 +131,7 @@ obj-$(CONFIG_I2C_DIOLAN_U2C)	+= i2c-diolan-u2c.o
 obj-$(CONFIG_I2C_DLN2)		+= i2c-dln2.o
 obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o
 obj-$(CONFIG_I2C_PARPORT)	+= i2c-parport.o
+obj-$(CONFIG_I2C_PCI1XXXX)	+= i2c-mchp-pci1xxxx.o
 obj-$(CONFIG_I2C_ROBOTFUZZ_OSIF)	+= i2c-robotfuzz-osif.o
 obj-$(CONFIG_I2C_TAOS_EVM)	+= i2c-taos-evm.o
 obj-$(CONFIG_I2C_TINY_USB)	+= i2c-tiny-usb.o
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +1227 -0

1,227 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw
Quoted 8 lines · Tharun Kumar P

Microchip pci1xxxx is an unmanaged PCIe3.1a Switch for Consumer, Industrial and Automotive applications. This switch has multiple downstream ports. In one of the Switch's Downstream port, there is a multifunction endpoint for peripherals which includes an I2C host controller. The I2C function in the endpoint operates at 100KHz, 400KHz and 1 MHz and has buffer depth of 128 bytes.
This patch provides the I2C controller driver for the I2C function of the switch.

No serious problems found. I think next version would be good enough.

...

+ bits.h

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 18

Quoted 7 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +7 -0
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/i2c-smbus.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pci.h>

+ types.h

...

Quoted 4 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +4 -0
+static void pci1xxxx_i2c_buffer_write(struct pci1xxxx_i2c *i2c, u8 slaveaddr,
+				      u8 transferlen, unsigned char *buf)
+{
+	void __iomem *bp;

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 433

Quoted 10 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +10 -0
+
+	bp = i2c->i2c_base;
+	if (slaveaddr) {
+		writeb(slaveaddr, bp + SMBUS_MST_BUF);
+		if (buf)
+			memcpy_toio(bp + SMBUS_MST_BUF + 1, buf, transferlen);
+	} else {
+		if (buf)
+			memcpy_toio(bp + SMBUS_MST_BUF, buf, transferlen);
+	}

So, basically it's like this:

	void __iomem *bp = i2c->i2c_base + SMBUS_MST_BUF;

	if (slaveaddr)
		writeb(slaveaddr, bp++);

	if (buf)
		memcpy_toio(bp, buf, transferlen);
Quoted 1 line

+}

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 448

Quoted 3 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +3 -0
+static void pci1xxxx_i2c_reset_counters(struct pci1xxxx_i2c *i2c)
+{
+	void __iomem *bp;

In all these helpers do like above:

	void __iomem *bp = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;

It will unload code from duplicating noise.

Quoted 7 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +7 -0
+	u8 regval;
+
+	bp = i2c->i2c_base;
+	regval = readb(bp + SMBUS_CONTROL_REG_OFF);
+	regval |= CTL_RESET_COUNTERS;
+	writeb(regval, bp + SMBUS_CONTROL_REG_OFF);
+}

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 534

Quoted 5 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +5 -0
+static irqreturn_t pci1xxxx_i2c_isr(int irq, void *dev)
+{
+	irqreturn_t intr_handled = IRQ_NONE;
+	struct pci1xxxx_i2c *i2c = dev;
+	void __iomem *bp;
	void __iomem *bp = i2c->i2c_base + SMBUS_GEN_INT_STAT_REG_OFF;
Quoted 28 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +28 -0
+	u16 reg1;
+	u8 reg3;
+
+	bp = i2c->i2c_base;
+
+	/*
+	 *  Read the SMBus interrupt status register to see if the
+	 *  DMA_TERM interrupt has caused this callback.
+	 */
+	reg1 = readw(bp + SMBUS_GEN_INT_STAT_REG_OFF);
+
+	if (reg1 & I2C_BUF_MSTR_INTR_MASK) {
+		reg3 = readb(bp + SMBUS_INTR_STAT_REG_OFF);
+		if (reg3 & INTR_STAT_DMA_TERM) {
+			complete(&i2c->i2c_xfer_done);
+			intr_handled = IRQ_HANDLED;
+			writeb(INTR_STAT_DMA_TERM, bp + SMBUS_INTR_STAT_REG_OFF);
+		}
+		pci1xxxx_ack_high_level_intr(i2c, I2C_BUF_MSTR_INTR_MASK);
+	}
+
+	if (reg1 & SMBALERT_INTR_MASK) {
+		intr_handled = IRQ_HANDLED;
+		pci1xxxx_ack_high_level_intr(i2c, SMBALERT_INTR_MASK);
+	}
+
+	return intr_handled;
+}

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 735

Quoted 11 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +11 -0
+	ret = set_sys_lock(i2c);
+	if (ret != -EPERM) {
+		regval = readl(bp + SMB_GPR_REG);
+		release_sys_lock(i2c);
+	} else {
+		/*
+		 * Configure I2C Fast Mode as default frequency if unable
+		 * to acquire sys lock.
+		 */
+		regval = 0;
+	}

Why not positive conditional?

	if (ret == -EPERM) {
		...
	} else {
		...
	}

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/*
+	 * Enable Pullup for the SMB alert pin which is just used for

pull-up

Quoted 2 lines
(quoted diff) +2 -0
+	 * wakeup right now.
+	 */

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 827

Quoted 1 line
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +1 -0
+		transferlen = min_t(u16, remainingbytes, (u16)SMBUS_BUF_MAX_SIZE);

min_t (note 't' part) does type casting for you, no need to repeat this.

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 839

Quoted 2 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +2 -0
+		if ((count + transferlen >= total_len) &&
+				(i2c->flags & I2C_FLAGS_STOP)) {

Broken indentation (align ( under ( in the second line)

Quoted 5 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +5 -0
+			pci1xxxx_i2c_set_clear_FW_ACK(i2c, false);
+			pci1xxxx_i2c_send_start_stop(i2c, 0);
+		} else {
+			pci1xxxx_i2c_set_clear_FW_ACK(i2c, true);
+		}

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 876

Quoted 2 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +2 -0
+		time_left = wait_for_completion_timeout
+				(&i2c->i2c_xfer_done,

Why this on a separate line? Join them together.

Quoted 7 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +7 -0
+				msecs_to_jiffies(PCI1XXXX_I2C_TIMEOUT_MS));
+		if (time_left == 0) {
+			/* Reset the I2C core to release the bus lock. */
+			pci1xxxx_i2c_init(i2c);
+			retval = -ETIMEDOUT;
+			goto cleanup;
+		}

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 901

Quoted 7 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +7 -0
+		if (i2c->flags & I2C_FLAGS_SMB_BLK_READ) {
+			buf[0] = readb(bp + SMBUS_MST_BUF);
+			read_count = buf[0];
+			memcpy_fromio(&buf[1], bp + SMBUS_MST_BUF + 1, read_count);
+		} else {
+			memcpy_fromio(&buf[count], bp + SMBUS_MST_BUF, transferlen);
+		}

Hmm... count in the second case because we may not write all in one transfer?

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 825

Quoted 6 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +6 -0
+	for (count = 0; count < total_len; count += transferlen) {
+		/*
+		 * Before start of any transaction clear the existing
+		 * START/STOP conditions.
+		 */
+		writeb(0x00, bp + SMB_CORE_CMD_REG_OFF1);

0x00 --> 0

(below as well)

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 953

Quoted 10 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +10 -0
+		pci1xxxx_i2c_clear_flags(i2c);
+		remainingbytes = total_len - count;
+
+		/* If it is the starting of the transaction send START. */
+		if (count == 0) {
+			pci1xxxx_i2c_send_start_stop(i2c, 1);
+
+			/* -1 for the slave address. */
+			transferlen = min_t(u16, (u16)SMBUS_BUF_MAX_SIZE - 1,
+					  remainingbytes);

No castings.

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 963

Quoted 10 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +10 -0
+			pci1xxxx_i2c_buffer_write(i2c, slaveaddr,
+						  transferlen, &buf[count]);
+			/*
+			 * The actual number of bytes written on the I2C bus
+			 * is including the slave address.
+			 */
+			actualwritelen = transferlen + 1;
+		} else {
+			transferlen = min_t(u16, (u16)SMBUS_BUF_MAX_SIZE,
+					  remainingbytes);

Ditto.

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 975

Quoted 12 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +12 -0
+			pci1xxxx_i2c_buffer_write(i2c, 0x00, transferlen, &buf[count]);
+			actualwritelen = transferlen;
+		}
+
+		pci1xxxx_i2c_set_count(i2c, actualwritelen, actualwritelen, 0x00);
+
+		/*
+		 * Send STOP only when I2C_FLAGS_STOP bit is set in the flags and
+		 * only for the last transaction.
+		 */
+		if (remainingbytes <= transferlen &&
+				(i2c->flags & I2C_FLAGS_STOP))

Indentation needs to be fixed.

Quoted 11 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +11 -0
+			pci1xxxx_i2c_send_start_stop(i2c, 0);
+
+		pci1xxxx_i2c_start_DMA(i2c);
+
+		/*
+		 * Wait for the DMA_TERM interrupt.
+		 */
+		time_left = wait_for_completion_timeout(&i2c->i2c_xfer_done,
+				msecs_to_jiffies(PCI1XXXX_I2C_TIMEOUT_MS));
+		if (time_left == 0) {
+			/* Reset the I2C core to release the bus lock. */

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 995

Quoted 10 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +9 -0
+			pci1xxxx_i2c_init(i2c);
+			retval = -ETIMEDOUT;
+			goto cleanup;
+		}
+
+		regval = readb(bp + SMB_CORE_COMPLETION_REG_OFF3);
+		if (regval & COMPLETION_MNAKX) {
 
+			writeb(COMPLETION_MNAKX, i2c->i2c_base +
+						SMB_CORE_COMPLETION_REG_OFF3);
			writeb(COMPLETION_MNAKX,
			       i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3);

looks better.

Quoted 4 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +4 -0
+			retval = -ETIMEDOUT;
+			goto cleanup;
+		}
+	}

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 1075

Quoted 3 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +3 -0
+static const struct i2c_adapter pci1xxxx_i2c_ops = {
+	.owner	= THIS_MODULE,
+	.name	= "Pci1xxxx I2C Adapter",

Wouldn't PCI1xxxx look better?

Quoted 2 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +2 -0
+	.algo	= &pci1xxxx_i2c_algo,
+};

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 1108

Quoted 2 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +2 -0
+	device_set_wakeup_enable(dev, TRUE);
+	pci_wake_from_d3(pdev, TRUE);

What's TRUE? Why true can't be used?

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 1148

Quoted 5 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +5 -0
+	struct pci1xxxx_i2c *i2c;
+	struct device *dev;
+	int ret;
+
+	dev = &pdev->dev;

The idea is when we 100% know that the dereference is okay and can't fail, we move assignments like this to the definition block above, otherwise, when an additional check is needed, we keep the assignment closer to its first user (conditional).

Quoted 3 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +3 -0
+	i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
+	if (!i2c)
+		return -ENOMEM;

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 1168

Quoted 8 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +7 -0
+	/*
+	 * We are getting the base address of the SMB core. SMB core uses
+	 * BAR0 and size is 32K.
+	 */
+	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
+	if (ret < 0)
 
+		return -ENOMEM;

What's wrong with

		return ret;

?

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

+ bits.h
+ types.h

Okay. I will check and include these header files.

Quoted 11 lines

So, basically it's like this:

        void __iomem *bp = i2c->i2c_base + SMBUS_MST_BUF;

        if (slaveaddr)
                writeb(slaveaddr, bp++);

        if (buf)
                memcpy_toio(bp, buf, transferlen);

Quoted 1 line

+}

Okay. I will update code like this.

Quoted 11 lines

...

drivers/i2c/busses/i2c-mchp-pci1xxxx.c line 448

Quoted 3 lines
drivers/i2c/busses/i2c-mchp-pci1xxxx.c new file +3 -0
+static void pci1xxxx_i2c_reset_counters(struct pci1xxxx_i2c *i2c)
+{
+     void __iomem *bp;

In all these helpers do like above:

        void __iomem *bp = i2c->i2c_base + SMBUS_CONTROL_REG_OFF;

It will unload code from duplicating noise.

Okay

Quoted 9 lines

...
Why not positive conditional?

        if (ret == -EPERM) {
                ...
        } else {
                ...
        }

Okay, I will change code like this.

Quoted 6 lines

...

Quoted 2 lines

+     /*
+      * Enable Pullup for the SMB alert pin which is just used for

pull-up

Okay. I will modify like this.

Quoted 6 lines
Quoted 3 lines

+             transferlen = min_t(u16, remainingbytes, (u16)SMBUS_BUF_MAX_SIZE);

min_t (note 't' part) does type casting for you, no need to repeat this.

okay.

Quoted 6 lines

...

Quoted 2 lines

+             if ((count + transferlen >= total_len) && +                             (i2c->flags & I2C_FLAGS_STOP)) {

Broken indentation (align ( under ( in the second line)

Okay.

Quoted 7 lines

...

Quoted 2 lines

+             time_left = wait_for_completion_timeout +                             (&i2c->i2c_xfer_done,

Why this on a separate line? Join them together.

Okay. I will join.

Quoted 6 lines
Quoted 4 lines

+             } else {
+                     memcpy_fromio(&buf[count], bp + SMBUS_MST_BUF, transferlen);
+             }

Hmm... count in the second case because we may not write all in one transfer?

Yes. Harware supports transferring 128 bytes at a time.

Quoted 4 lines

...

Quoted 1 line

             writeb(0x00, bp + SMB_CORE_CMD_REG_OFF1);

0x00 --> 0

Okay.

Quoted 1 line

(below as well)

Okay.

Quoted 4 lines
Quoted 2 lines

+                     transferlen = min_t(u16, (u16)SMBUS_BUF_MAX_SIZE - 1, +                                       remainingbytes);

No castings.

Okay.

Quoted 4 lines
Quoted 2 lines

+                     transferlen = min_t(u16, (u16)SMBUS_BUF_MAX_SIZE, +                                       remainingbytes);

Ditto.

Okay.

Quoted 4 lines
Quoted 2 lines

+             if (remainingbytes <= transferlen && +                             (i2c->flags & I2C_FLAGS_STOP))

Indentation needs to be fixed.

Okay. I will fix indentation

Quoted 8 lines
Quoted 3 lines

+
+                     writeb(COMPLETION_MNAKX, i2c->i2c_base +
+                                             SMB_CORE_COMPLETION_REG_OFF3);

                        writeb(COMPLETION_MNAKX,
                               i2c->i2c_base + SMB_CORE_COMPLETION_REG_OFF3);

looks better.

Okay. I will change code as suggested.

Quoted 6 lines
Quoted 3 lines

+     .owner  = THIS_MODULE,
+     .name   = "Pci1xxxx I2C Adapter",

Wouldn't PCI1xxxx look better?

Yes. I will use PCI1xxxx.

Quoted 9 lines
Quoted 2 lines

+     .algo   = &pci1xxxx_i2c_algo,
+};

...

Quoted 2 lines

+     device_set_wakeup_enable(dev, TRUE);
+     pci_wake_from_d3(pdev, TRUE);

What's TRUE? Why true can't be used?

I will use true.

Quoted 8 lines

...

Quoted 5 lines

+     struct pci1xxxx_i2c *i2c;
+     struct device *dev;
+     int ret;
+
+     dev = &pdev->dev;

Quoted 4 lines

The idea is when we 100% know that the dereference is okay and can't fail, we move assignments like this to the definition block above, otherwise, when an additional check is needed, we keep the assignment closer to its first user (conditional).

Okay.

Quoted 9 lines
Quoted 4 lines

+     ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); +     if (ret < 0)

+             return -ENOMEM;

What's wrong with

                return ret;

Yes, return ret can be used. I will check and use it.

Thanks,
Tharun Kumar P