Skip to contentSkip to conversation

I2C controller support for KEBA PLCs

PATCH Superseded Gerhard Engleder 2 participants 5 messages Jul 31, 2024 – Aug 8, 2024 I2C subsystem host drivers

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover I2C controller support for KEBA PLCs

KEBA PLCs use a PCIe FPGA to implement several functions in a common way. This is a first step to include basic support for this FPGA.

Under drivers/misc the cp500 PCI driver is added. This drivers creates auxiliary devices for every function inside the FPGA. As a first step it only creates auxiliary devices for the I2C controllers.

The I2C controller driver is added under drivers/i2c/busses like all the other I2C controller drivers.

This patches enable access to the EEPROMs and to the hardware monitoring chip of KEBA PLCs.

v4:
- first patch for drivers/misc removed because it is already merged and part of 6.11-rc1

v3:
- first patch is already merged to char-misc-next by Greg KH
- add AUXILIARY_BUS Kconfig dependency
- don't declare int i within for loop (Andi Shyti)
- enable I2C controller as late as possible (Andi Shyti)
- use devm_i2c_add_adapter (Andi Shyti)
- add KI2C_CONTROL_DISABLE (Andi Shyti)

v2:
- use aux bus (Greg KH)
- add Documentation/ABI/ entry for version and keep_cfg sysfs files (Greg KH)
- use sysfs_emit (Greg KH)
- add some information about I2C controller in commit message (Andi Shyti)
- remove old copyright line (Andi Shyti)
- sort header alphabetically (Andi Shyti)
- rework register defines as suggested (Andi Shyti)
- fix block comment style multiple times (Andi Shyti)
- add comment that IN_USE bit is a semaphore (Andi Shyti)
- add comment that IN_USE bit polling sleeps (Andi Shyti)
- addr as first argument of ki2c_wait_for_bit() (Andi Shyti)
- avoid compact style for register flag check (Andi Shyti)
- move bus reset dev_err's to reset implementation (Andi Shyti)
- use dev_err_probe() (Andi Shyti)
- fix 'ki2c_devtype defined but not used' (kernel test robot)
- fix 'WARNING comparing pointer to 0', twice (kernel test robot)
- fix 'Using plain integer as NULL pointer' (kernel test robot)
- fix 'symbol cp035_devices was not declared. Should it be static?', 3 times (kernel test robot)

Gerhard Engleder (1):
i2c: keba: Add KEBA I2C controller support

 drivers/i2c/busses/Kconfig    |  11 +
 drivers/i2c/busses/Makefile   |   1 +
 drivers/i2c/busses/i2c-keba.c | 593 ++++++++++++++++++++++++++++++++++
 3 files changed, 605 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-keba.c
Signature · 1 line
2.39.2
#1 lore raw

PATCH v4 1/1 i2c: keba: Add KEBA I2C controller support

From: Gerhard Engleder <[email protected]>

The KEBA I2C controller is found in the system FPGA of KEBA PLC devices. It is used to connect EEPROMs and hardware monitoring chips. The

It is a simple I2C controller with a fixed bus speed of 100 kbit/s. The whole message transmission is executed by the driver. The driver triggers all steps over control, status and data register. There are no FIFOs or interrupts.

---
 drivers/i2c/busses/Kconfig    |  11 +
 drivers/i2c/busses/Makefile   |   1 +
 drivers/i2c/busses/i2c-keba.c | 593 ++++++++++++++++++++++++++++++++++
 3 files changed, 605 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-keba.c
drivers/i2c/busses/Kconfig +11 -0
index a22f9125322a..3d2215acc9b4 100644
@@ -772,6 +772,17 @@ config I2C_JZ4780
 
 	 If you don't know what to do here, say N.
 
+config I2C_KEBA
+	tristate "KEBA I2C controller support"
+	depends on HAS_IOMEM
+	select AUXILIARY_BUS
+	help
+	  This driver supports the I2C controller found in KEBA system FPGA
+	  devices.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called i2c-keba.
+
 config I2C_KEMPLD
 	tristate "Kontron COM I2C Controller"
 	depends on MFD_KEMPLD
drivers/i2c/busses/Makefile +1 -0
index 78d0561339e5..ecc07c50f2a0 100644
@@ -76,6 +76,7 @@ obj-$(CONFIG_I2C_IMX)		+= i2c-imx.o
 obj-$(CONFIG_I2C_IMX_LPI2C)	+= i2c-imx-lpi2c.o
 obj-$(CONFIG_I2C_IOP3XX)	+= i2c-iop3xx.o
 obj-$(CONFIG_I2C_JZ4780)	+= i2c-jz4780.o
+obj-$(CONFIG_I2C_KEBA)		+= i2c-keba.o
 obj-$(CONFIG_I2C_KEMPLD)	+= i2c-kempld.o
 obj-$(CONFIG_I2C_LPC2K)		+= i2c-lpc2k.o
 obj-$(CONFIG_I2C_LS2X)		+= i2c-ls2x.o
drivers/i2c/busses/i2c-keba.c new file +593 -0

593 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#2 lore raw

Re: [PATCH v4 1/1] i2c: keba: Add KEBA I2C controller support

Hi Gerhard,

...

drivers/i2c/busses/i2c-keba.c line 80

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+	ret = readb_poll_timeout(ki2c->base + KI2C_STATUS_REG,
+				 sts, (sts & KI2C_STATUS_IN_USE) == 0,
+				 KI2C_INUSE_SLEEP_US, KI2C_INUSE_TIMEOUT_US);
+	if (ret != 0)

if (ret)

Quoted 1 line
drivers/i2c/busses/i2c-keba.c new file +1 -0
+		dev_warn(&ki2c->auxdev->auxdev.dev, "%s err!\n", __func__);

...

drivers/i2c/busses/i2c-keba.c line 180

Quoted 9 lines
drivers/i2c/busses/i2c-keba.c new file +9 -0
+/*
+ * Resetting bus bitwise is done by checking SDA and applying clock cycles as
+ * long as SDA is low. 9 clock cycles are applied at most.
+ *
+ * Clock cycles are generated and ndelay() determines the duration of clock
+ * cycles. Generated clock rate is 100 KHz and so duration of both clock levels
+ * is: delay in ns = (10^6 / 100) / 2
+ */
+#define KI2C_RECOVERY_CLK_CNT	9

we can have

   #define KI2C_RECOVERY_CLK_CNT	9 * 2

and...

Quoted 1 line

+#define KI2C_RECOVERY_NDELAY 5000

... use udelay()

   #define KI2C_RECOVERY_UDELAY	5

I think it's a bit easier to read later.

drivers/i2c/busses/i2c-keba.c line 194

Quoted 13 lines
drivers/i2c/busses/i2c-keba.c new file +13 -0
+static int ki2c_reset_bus_bitwise(struct ki2c *ki2c)
+{
+	int count = 0;
+	int val = 1;
+	int ret = 0;
+
+	/* disable I2C controller (MEN = 0) to get direct access to SCL/SDA */
+	iowrite8(0, ki2c->base + KI2C_CONTROL_REG);
+
+	/* generate clock cycles */
+	ki2c_set_scl(ki2c, val);
+	ndelay(KI2C_RECOVERY_NDELAY);
+	while (count++ < KI2C_RECOVERY_CLK_CNT * 2) {

incrementing inside a while? sounds like a for :-)

for (count = 0; count++ < KI2C_RECOVERY_CLK_CNT * 2; count++)

Quoted 1 line
drivers/i2c/busses/i2c-keba.c new file +1 -0
+		if (val) {

...

drivers/i2c/busses/i2c-keba.c line 334

Quoted 7 lines
drivers/i2c/busses/i2c-keba.c new file +7 -0
+static int ki2c_repstart_addr(struct ki2c *ki2c, struct i2c_msg *m)
+{
+	int ret;
+
+	/* repeated start and write is not supported */
+	if ((m->flags & I2C_M_RD) == 0) {
+		dev_warn(&ki2c->auxdev->auxdev.dev,

you are returning an error but printing a warning. Should the print level be aligned with the returning behaviour? Otherwise, if this has a debugging purpose, just use dev_dbg().

Quoted 14 lines
drivers/i2c/busses/i2c-keba.c new file +14 -0
+			 "Repeated start not supported for writes\n");
+		return -EINVAL;
+	}
+
+	/* send repeated start */
+	iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_RSTA,
+		 ki2c->base + KI2C_CONTROL_REG);
+
+	ret = ki2c_wait_for_mcf(ki2c);
+	if (ret < 0) {
+		dev_warn(&ki2c->auxdev->auxdev.dev,
+			 "%s wait for MCF err at 0x%02x!\n", __func__, m->addr);
+		return ret;
+	}

...

Review

Quoted 1 line
(quoted diff) +1 -0
+	for (int i = 0; i < len; i++) {

please, do not declare inside the for(), it's strange that checkpatch doesn't warn about this.

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+		ret = ki2c_wait_for_data(ki2c);
+		if (ret < 0)
+			return ret;
+

drivers/i2c/busses/i2c-keba.c line 416

Quoted 10 lines
drivers/i2c/busses/i2c-keba.c new file +10 -0
+		/* send tx-nack after transfer of last byte */
+		if (i == len - 2)
+			iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_TXAK,
+				 ki2c->base + KI2C_CONTROL_REG);
+
+		/*
+		 * switch to TX on last byte, so that reading DATA-register
+		 * does not trigger another read transfer.
+		 */
+		if (i == len - 1)

else if

Quoted 1 line
drivers/i2c/busses/i2c-keba.c new file +1 -0
+			iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_MTX,

...

drivers/i2c/busses/i2c-keba.c line 458

Quoted 1 line
drivers/i2c/busses/i2c-keba.c new file +1 -0
+	return (ret < 0) ? ret : num; 

no need for parenthesis here.

Quoted 1 line

+}

...

drivers/i2c/busses/i2c-keba.c line 483

Quoted 5 lines
drivers/i2c/busses/i2c-keba.c new file +5 -0
+		unsigned short const addr_list[2] = { info[i].addr,
+						      I2C_CLIENT_END };
+
+		client = i2c_new_scanned_device(&ki2c->adapter, &info[i],
+						addr_list, NULL);

so this comes with a known client's list. Stupid question, have you checked the address range is OK for this use?

drivers/i2c/busses/i2c-keba.c line 486

Quoted 3 lines
drivers/i2c/busses/i2c-keba.c new file +3 -0
+		if (!IS_ERR(client)) {
+			ki2c->client[i] = client;
+		} else if (PTR_ERR(client) != -ENODEV) {

if you set here ki2c->client_size = i, you avoid unregistering devices that are not registered... just a micro optimization, not a binding comment.

Quoted 1 line
(quoted diff) +1 -0
+			ki2c_unregister_devices(ki2c);

...

drivers/i2c/busses/i2c-keba.c line 544

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+	/* reset bus before probing I2C devices */
+	ret = ki2c_reset_bus(ki2c);
+	if (ret)
+		goto out;

We can still have the enabling and the reset at the end, I don't seen any interaction with the hardware.

Quoted 18 lines
drivers/i2c/busses/i2c-keba.c new file +18 -0
+	ret = devm_i2c_add_adapter(dev, adap);
+	if (ret) {
+		dev_err(dev, "Failed to add adapter (%d)!\n", ret);
+		goto out;
+	}
+
+	ret = ki2c_register_devices(ki2c);
+	if (ret) {
+		dev_err(dev, "Failed to register devices (%d)!\n", ret);
+		goto out;
+	}
+
+	return 0;
+
+out:
+	iowrite8(KI2C_CONTROL_DISABLE, ki2c->base + KI2C_CONTROL_REG);
+	return ret;
+}

...

drivers/i2c/busses/i2c-keba.c line 579

Quoted 3 lines
drivers/i2c/busses/i2c-keba.c new file +3 -0
+static const struct auxiliary_device_id ki2c_devtype_aux[] = {
+	{ .name = "keba.i2c" },
+	{ },

Please, remove the comma here, there has been recently a patch from Wolfram doing it on all the i2c drivers[*]

Thanks,
Andi

[*] 11bfff4913e4 ("i2c: don't use ',' after delimiters")

#3 lore raw

Re: [PATCH v4 1/1] i2c: keba: Add KEBA I2C controller support

Quoted 1 line · Andi Shyti

Hi Gerhard,

Hi Andi,

Quoted 9 lines

...

drivers/i2c/busses/i2c-keba.c line 80

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+	ret = readb_poll_timeout(ki2c->base + KI2C_STATUS_REG,
+				 sts, (sts & KI2C_STATUS_IN_USE) == 0,
+				 KI2C_INUSE_SLEEP_US, KI2C_INUSE_TIMEOUT_US);
+	if (ret != 0)

if (ret)

Done.

Quoted 17 lines
Quoted 2 lines
drivers/i2c/busses/i2c-keba.c new file +1 -0
+		dev_warn(&ki2c->auxdev->auxdev.dev, "%s err!\n", __func__);

...

drivers/i2c/busses/i2c-keba.c line 180

Quoted 9 lines
drivers/i2c/busses/i2c-keba.c new file +9 -0
+/*
+ * Resetting bus bitwise is done by checking SDA and applying clock cycles as
+ * long as SDA is low. 9 clock cycles are applied at most.
+ *
+ * Clock cycles are generated and ndelay() determines the duration of clock
+ * cycles. Generated clock rate is 100 KHz and so duration of both clock levels
+ * is: delay in ns = (10^6 / 100) / 2
+ */
+#define KI2C_RECOVERY_CLK_CNT	9

we can have

Quoted 1 line
    #define KI2C_RECOVERY_CLK_CNT	9 * 2

Done.

Quoted 10 lines

and...

Quoted 1 line

+#define KI2C_RECOVERY_NDELAY 5000

... use udelay()

    #define KI2C_RECOVERY_UDELAY	5

I think it's a bit easier to read later.

Done.

Quoted 17 lines
Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +3 -0
+static int ki2c_reset_bus_bitwise(struct ki2c *ki2c)
+{
+	int count = 0;

drivers/i2c/busses/i2c-keba.c line 194

Quoted 10 lines
drivers/i2c/busses/i2c-keba.c new file +10 -0
+	int val = 1;
+	int ret = 0;
+
+	/* disable I2C controller (MEN = 0) to get direct access to SCL/SDA */
+	iowrite8(0, ki2c->base + KI2C_CONTROL_REG);
+
+	/* generate clock cycles */
+	ki2c_set_scl(ki2c, val);
+	ndelay(KI2C_RECOVERY_NDELAY);
+	while (count++ < KI2C_RECOVERY_CLK_CNT * 2) {

incrementing inside a while? sounds like a for :-)

Quoted 1 line

for (count = 0; count++ < KI2C_RECOVERY_CLK_CNT * 2; count++)

Done.

Quoted 5 lines
Quoted 2 lines
drivers/i2c/busses/i2c-keba.c new file +1 -0
+		if (val) {

...

Quoted 11 lines

drivers/i2c/busses/i2c-keba.c line 334

Quoted 7 lines
drivers/i2c/busses/i2c-keba.c new file +7 -0
+static int ki2c_repstart_addr(struct ki2c *ki2c, struct i2c_msg *m)
+{
+	int ret;
+
+	/* repeated start and write is not supported */
+	if ((m->flags & I2C_M_RD) == 0) {
+		dev_warn(&ki2c->auxdev->auxdev.dev,

you are returning an error but printing a warning. Should the print level be aligned with the returning behaviour? Otherwise, if this has a debugging purpose, just use dev_dbg().

You are right, changed all dev_warn() to dev_err().

Quoted 18 lines
Quoted 15 lines
drivers/i2c/busses/i2c-keba.c new file +14 -0
+			 "Repeated start not supported for writes\n");
+		return -EINVAL;
+	}
+
+	/* send repeated start */
+	iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_RSTA,
+		 ki2c->base + KI2C_CONTROL_REG);
+
+	ret = ki2c_wait_for_mcf(ki2c);
+	if (ret < 0) {
+		dev_warn(&ki2c->auxdev->auxdev.dev,
+			 "%s wait for MCF err at 0x%02x!\n", __func__, m->addr);
+		return ret;
+	}

...

Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	for (int i = 0; i < len; i++) {

please, do not declare inside the for(), it's strange that checkpatch doesn't warn about this.

Done for all for loops.

Quoted 17 lines
Quoted 5 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+		ret = ki2c_wait_for_data(ki2c);
+		if (ret < 0)
+			return ret;
+

drivers/i2c/busses/i2c-keba.c line 416

Quoted 10 lines
drivers/i2c/busses/i2c-keba.c new file +10 -0
+		/* send tx-nack after transfer of last byte */
+		if (i == len - 2)
+			iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_TXAK,
+				 ki2c->base + KI2C_CONTROL_REG);
+
+		/*
+		 * switch to TX on last byte, so that reading DATA-register
+		 * does not trigger another read transfer.
+		 */
+		if (i == len - 1)

else if

Done.

Quoted 8 lines
Quoted 2 lines
drivers/i2c/busses/i2c-keba.c new file +1 -0
+			iowrite8(KI2C_CONTROL_MEN | KI2C_CONTROL_MSTA | KI2C_CONTROL_MTX,

...

drivers/i2c/busses/i2c-keba.c line 458

Quoted 1 line
drivers/i2c/busses/i2c-keba.c new file +1 -0
+	return (ret < 0) ? ret : num;

no need for parenthesis here.

Done.

Quoted 5 lines
Quoted 2 lines

+}

...

Quoted 8 lines

drivers/i2c/busses/i2c-keba.c line 483

Quoted 5 lines
drivers/i2c/busses/i2c-keba.c new file +5 -0
+		unsigned short const addr_list[2] = { info[i].addr,
+						      I2C_CLIENT_END };
+
+		client = i2c_new_scanned_device(&ki2c->adapter, &info[i],
+						addr_list, NULL);

so this comes with a known client's list. Stupid question, have you checked the address range is OK for this use?

Yes, the addresses are 0x4c (EMC1403) and 0x50 to 0x56 (EEPROMs). All between 0x08 and 0x77. All devices are working/found.

Quoted 8 lines

drivers/i2c/busses/i2c-keba.c line 486

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +3 -0
+		if (!IS_ERR(client)) {
+			ki2c->client[i] = client;
+		} else if (PTR_ERR(client) != -ENODEV) {

if you set here ki2c->client_size = i, you avoid unregistering devices that are not registered... just a micro optimization, not a binding comment.

Done.

Quoted 5 lines
Quoted 2 lines
(quoted diff) +1 -0
+			ki2c_unregister_devices(ki2c);

...

Quoted 7 lines

drivers/i2c/busses/i2c-keba.c line 544

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+	/* reset bus before probing I2C devices */
+	ret = ki2c_reset_bus(ki2c);
+	if (ret)
+		goto out;

We can still have the enabling and the reset at the end, I don't seen any interaction with the hardware.

At least ki2c_register_device() does interact with the hardware. It uses i2c_new_scanned_device() which probes the devices.

IMHO the device shall be ready to operate when devm_i2c_add_adapter() is called. Because the user space can trigger I2C action as soon as devm_i2c_add_adapter() is done. So enabling and reset after devm_i2c_add_adapter() would be a potential race against user space I2C actions. Is that correct?

Quoted 22 lines
Quoted 19 lines
drivers/i2c/busses/i2c-keba.c new file +18 -0
+	ret = devm_i2c_add_adapter(dev, adap);
+	if (ret) {
+		dev_err(dev, "Failed to add adapter (%d)!\n", ret);
+		goto out;
+	}
+
+	ret = ki2c_register_devices(ki2c);
+	if (ret) {
+		dev_err(dev, "Failed to register devices (%d)!\n", ret);
+		goto out;
+	}
+
+	return 0;
+
+out:
+	iowrite8(KI2C_CONTROL_DISABLE, ki2c->base + KI2C_CONTROL_REG);
+	return ret;
+}

...

Quoted 6 lines

drivers/i2c/busses/i2c-keba.c line 579

Quoted 3 lines
drivers/i2c/busses/i2c-keba.c new file +3 -0
+static const struct auxiliary_device_id ki2c_devtype_aux[] = {
+	{ .name = "keba.i2c" },
+	{ },

Please, remove the comma here, there has been recently a patch from Wolfram doing it on all the i2c drivers[*]

Done.

Quoted 5 lines

Thanks,
Andi

[*] 11bfff4913e4 ("i2c: don't use ',' after delimiters")

Thank you for your detailed review!

Gerhard

maintainer
#4 lore raw

Re: [PATCH v4 1/1] i2c: keba: Add KEBA I2C controller support

Hi Gerhard,

...

Quoted 11 lines
Quoted 8 lines

drivers/i2c/busses/i2c-keba.c line 483

Quoted 5 lines
drivers/i2c/busses/i2c-keba.c new file +5 -0
+		unsigned short const addr_list[2] = { info[i].addr,
+						      I2C_CLIENT_END };
+
+		client = i2c_new_scanned_device(&ki2c->adapter, &info[i],
+						addr_list, NULL);

so this comes with a known client's list. Stupid question, have you checked the address range is OK for this use?

Yes, the addresses are 0x4c (EMC1403) and 0x50 to 0x56 (EEPROMs). All between 0x08 and 0x77. All devices are working/found.

just making sure :-)

Quoted 12 lines
Quoted 11 lines
Quoted 1 line
(quoted diff) +1 -0
+			ki2c_unregister_devices(ki2c);

...

drivers/i2c/busses/i2c-keba.c line 544

Quoted 4 lines
drivers/i2c/busses/i2c-keba.c new file +4 -0
+	/* reset bus before probing I2C devices */
+	ret = ki2c_reset_bus(ki2c);
+	if (ret)
+		goto out;

We can still have the enabling and the reset at the end, I don't seen any interaction with the hardware.

Quoted 8 lines

At least ki2c_register_device() does interact with the hardware. It uses i2c_new_scanned_device() which probes the devices.

IMHO the device shall be ready to operate when devm_i2c_add_adapter() is called. Because the user space can trigger I2C action as soon as devm_i2c_add_adapter() is done. So enabling and reset after devm_i2c_add_adapter() would be a potential race against user space I2C actions. Is that correct?

It's OK! Thanks.

Andi