Skip to contentSkip to conversation

omap24xx_i2c: Enable Repeated Start functionality

PATCH Superseded Aniket Limaye 3 participants 12 messages Mar 4, 2025 – Mar 6, 2025 I2C

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover omap24xx_i2c: Enable Repeated Start functionality

This series implements Repeated Start functionality for the DM_I2C xfer API (omap_i2c_xfer())

First, split out the actual implementation for each individual i2c msg transfer into a new function __omap24_i2c_xfer_msg() and use this to implement omap_i2c_xfer().

Then, add support for the Repeated Start condition in omap_i2c_xfer() through the CONFIG_I2C_REPEATED_START option.

Also add this config to Kconfig and enable it for ARCH_K3 platforms by default.

Test logs (On AM68-SK with additional debug prints):
https://gist.github.com/aniket-l/8c64c5bc81246751a02836a96510df14

Requesting reviews/testing on specifically the CONFIG_I2C_REPEATED_START addition:
- Is the Kconfig needed or should we simply always default to not sending Stop bit? Drivers can use i2c_msg.flags to request Stop bit for each msg, same as in the Linux Kernel i2c-omap driver[0].
- If yes, Should we re-use CONFIG_I2C_REPEATED_START from the legacy implementation of the driver or define a new config
- Default enable it for K3 devices?

[0]: https://github.com/torvalds/linux/blob/v6.14-rc5/drivers/i2c/busses/i2c-omap.c#L699

Aniket Limaye (3):
i2c: omap24xx_i2c: Use new function __omap24_i2c_xfer_msg()
i2c: omap24xx_i2c: support CONFIG_I2C_REPEATED_START in DM_I2C xfer
drivers: i2c: Kconfig: Add config for I2C_REPEATED_START

 drivers/i2c/Kconfig        |   9 +++
 drivers/i2c/omap24xx_i2c.c | 149 ++++++++++++++++++++++++++++++++-----
 2 files changed, 140 insertions(+), 18 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/3 i2c: omap24xx_i2c: Use new function __omap24_i2c_xfer_msg()

Remove __omap24_i2c_read/write() usage from omap_i2c_xfer() in favour of the more flexible __omap24_i2c_xfer_msg().
Consequently, these are also no longer needed when DM_I2C is enabled.

New function __omap24_i2c_xfer_msg() will take care of individual read OR write transfers with a target device. It goes through below sequence:
- Program the provided Target Chip address (OMAP_I2C_SA_REG)
- Program the provided Data len (OMAP_I2C_CNT_REG)
- Program the provided Control register flags (OMAP_I2C_CON_REG)
- Read from or Write to the provided Data buffer (OMAP_I2C_DATA_REG)

For a detailed programming guide, refer to the TRM[0] (12.1.3.4 I2C Programming Guide).

This patch by itself should be a transparent change. However this is needed for implementing a proper Repeated Start (Sr) functionality for i2c_msgs.

Previous implementation for omap_i2c_xfer called __omap24_i2c_read/write functions, with hardcoded addr=0 and alen=0 for each i2c_msg. Each of these calls would program the registers always with a Stop bit set, not allowing for a repeated start between i2c_msgs in the same xfer().

[0]: https://www.ti.com/lit/zip/spruj28 (TRM)

---
 drivers/i2c/omap24xx_i2c.c | 139 ++++++++++++++++++++++++++++++++-----
 1 file changed, 121 insertions(+), 18 deletions(-)
drivers/i2c/omap24xx_i2c.c +121 -18
index ebe472e20cd..0d044121d27 100644
@@ -535,6 +535,107 @@ pr_exit:
 	return res;
 }
 
+static int __omap24_i2c_xfer_msg(void __iomem *i2c_base, int ip_rev, int waitdelay,
+				 uchar chip, uchar *buffer, int len, u16 i2c_con_reg)
+{
+	int i;
+	u16 status;
+	int i2c_error = 0;
+	int timeout = I2C_TIMEOUT;
+
+	if (len < 0) {
+		puts("I2C xfer: data len < 0\n");
+		return 1;
+	}
+
+	if (!buffer) {
+		puts("I2C xfer: NULL pointer passed\n");
+		return 1;
+	}
+
+	if (!(i2c_con_reg & I2C_CON_EN)) {
+		puts("I2C xfer: I2C_CON_EN not set\n");
+		return 1;
+	}
+
+	/* Set slave address */
+	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
+	/* Read/Write len bytes data */
+	omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
+	/* Configure the I2C_CON register */
+	omap_i2c_write_reg(i2c_base, ip_rev, i2c_con_reg, OMAP_I2C_CON_REG);
+
+	/* read/write data bytewise */
+	for (i = 0; i < len; i++) {
+		status = wait_for_event(i2c_base, ip_rev, waitdelay);
+		/* Ignore I2C_STAT_RRDY in transmitter mode */
+		if (i2c_con_reg & I2C_CON_TRX)
+			status &= ~I2C_STAT_RRDY;
+		else
+			status &= ~I2C_STAT_XRDY;
+
+		/* Try to identify bus that is not padconf'd for I2C */
+		if (status == I2C_STAT_XRDY) {
+			i2c_error = 2;
+			printf("I2C xfer: pads on bus probably not configured (status=0x%x)\n",
+			       status);
+			goto xfer_exit;
+		}
+		if (status == 0 || (status & I2C_STAT_NACK)) {
+			i2c_error = 1;
+			printf("I2C xfer: error waiting for ACK (status=0x%x)\n",
+			       status);
+			goto xfer_exit;
+		}
+		if (status & I2C_STAT_XRDY) {
+			/* Transmit data */
+			omap_i2c_write_reg(i2c_base, ip_rev,
+					   buffer[i], OMAP_I2C_DATA_REG);
+			omap_i2c_write_reg(i2c_base, ip_rev,
+					   I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
+		}
+		if (status & I2C_STAT_RRDY) {
+			/* Receive data */
+			*buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
+							OMAP_I2C_DATA_REG);
+			omap_i2c_write_reg(i2c_base, ip_rev,
+					   I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
+		}
+	}
+
+	/*
+	 * poll ARDY bit for making sure that last byte really has been
+	 * transferred on the bus.
+	 */
+	do {
+		status = wait_for_event(i2c_base, ip_rev, waitdelay);
+	} while (!(status & I2C_STAT_ARDY) && timeout--);
+	if (timeout <= 0) {
+		puts("I2C xfer: timed out on last byte!\n");
+		i2c_error = 1;
+		goto xfer_exit;
+	} else {
+		omap_i2c_write_reg(i2c_base, ip_rev, I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
+	}
+
+	/* If Stop bit set, flush FIFO. */
+	if (i2c_con_reg & I2C_CON_STP)
+		goto xfer_exit;
+
+	return 0;
+
+xfer_exit:
+	flush_fifo(i2c_base, ip_rev);
+	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
+	return i2c_error;
+}
+
+#if !CONFIG_IS_ENABLED(DM_I2C)
+/*
+ * The legacy I2C functions. These need to get removed once
+ * all users of this driver are converted to DM.
+ */
+
 /*
  * i2c_read: Function now uses a single I2C read transaction with bulk transfer
  *           of the requested number of bytes (note that the 'i2c md' command
@@ -836,11 +937,6 @@ wr_exit:
 	return i2c_error;
 }
 
-#if !CONFIG_IS_ENABLED(DM_I2C)
-/*
- * The legacy I2C functions. These need to get removed once
- * all users of this driver are converted to DM.
- */
 static void __iomem *omap24_get_base(struct i2c_adapter *adap)
 {
 	switch (adap->hwadapnr) {
@@ -975,23 +1071,30 @@ static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
 {
 	struct omap_i2c *priv = dev_get_priv(bus);
 	int ret;
+	u16 i2c_con_reg = 0;
 
 	debug("i2c_xfer: %d messages\n", nmsgs);
 	for (; nmsgs > 0; nmsgs--, msg++) {
-		debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
-		if (msg->flags & I2C_M_RD) {
-			ret = __omap24_i2c_read(priv->regs, priv->ip_rev,
-						priv->waitdelay,
-						msg->addr, 0, 0, msg->buf,
-						msg->len);
-		} else {
-			ret = __omap24_i2c_write(priv->regs, priv->ip_rev,
-						 priv->waitdelay,
-						 msg->addr, 0, 0, msg->buf,
-						 msg->len);
-		}
+		debug("i2c_xfer: chip=0x%x, len=0x%x, read=0x%x\n",
+		      msg->addr, msg->len, (msg->flags & I2C_M_RD));
+
+		/* Wait until bus not busy */
+		if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
+			return -EREMOTEIO;
+
+		/* Set Controller mode with Start and Stop bit */
+		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
+		/* Set Transmitter/Receiver mode if it is a write/read msg */
+		if (msg->flags & I2C_M_RD)
+			i2c_con_reg &= ~I2C_CON_TRX;
+		else
+			i2c_con_reg |= I2C_CON_TRX;
+
+		ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, priv->waitdelay,
+					    msg->addr, msg->buf, msg->len,
+					    i2c_con_reg);
 		if (ret) {
-			debug("i2c_write: error sending\n");
+			debug("i2c_xfer: error sending\n");
 			return -EREMOTEIO;
 		}
 	}
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH RFC/RFT 1/3] i2c: omap24xx_i2c: Use new function __omap24_i2c_xfer_msg()

Hello Aniket,

Quoted 50 lines · Aniket Limaye

Remove __omap24_i2c_read/write() usage from omap_i2c_xfer() in favour of the more flexible __omap24_i2c_xfer_msg().
Consequently, these are also no longer needed when DM_I2C is enabled.

New function __omap24_i2c_xfer_msg() will take care of individual read OR write transfers with a target device. It goes through below sequence:
- Program the provided Target Chip address (OMAP_I2C_SA_REG)
- Program the provided Data len (OMAP_I2C_CNT_REG)
- Program the provided Control register flags (OMAP_I2C_CON_REG)
- Read from or Write to the provided Data buffer (OMAP_I2C_DATA_REG)

For a detailed programming guide, refer to the TRM[0] (12.1.3.4 I2C Programming Guide).

This patch by itself should be a transparent change. However this is needed for implementing a proper Repeated Start (Sr) functionality for i2c_msgs.

Previous implementation for omap_i2c_xfer called __omap24_i2c_read/write functions, with hardcoded addr=0 and alen=0 for each i2c_msg. Each of these calls would program the registers always with a Stop bit set, not allowing for a repeated start between i2c_msgs in the same xfer().

[0]: https://www.ti.com/lit/zip/spruj28 (TRM)

---
  drivers/i2c/omap24xx_i2c.c | 139 ++++++++++++++++++++++++++++++++-----
  1 file changed, 121 insertions(+), 18 deletions(-)
drivers/i2c/omap24xx_i2c.c +12 -0
index ebe472e20cd..0d044121d27 100644
@@ -535,6 +535,107 @@ pr_exit:
  	return res;
  }
 
+static int __omap24_i2c_xfer_msg(void __iomem *i2c_base, int ip_rev, int waitdelay,
+				 uchar chip, uchar *buffer, int len, u16 i2c_con_reg)
+{
+	int i;
+	u16 status;
+	int i2c_error = 0;
+	int timeout = I2C_TIMEOUT;
+
+	if (len < 0) {
+		puts("I2C xfer: data len < 0\n");
+		return 1;
+	}

drivers/i2c/omap24xx_i2c.c pr_exit: · line 559

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +10 -0
+
+	if (!buffer) {
+		puts("I2C xfer: NULL pointer passed\n");
+		return 1;
+	}
+
+	if (!(i2c_con_reg & I2C_CON_EN)) {
+		puts("I2C xfer: I2C_CON_EN not set\n");
+		return 1;
+	}

Can we use here (and above) a negative error code? Later you only check in omap_i2c_xfer() if returncode is ! 0 and return "-EREMOTEIO" may, you can return here better codes and simply return than this error codes from omap_i2c_xfer() ?

And may you change the strings from the puts call to a more driver specifc string ... so its clear from which i2c driver the error message comes... please fix this globally in your patchset.

Quoted 129 lines
drivers/i2c/omap24xx_i2c.c +94 -17
+
+	/* Set slave address */
+	omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
+	/* Read/Write len bytes data */
+	omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
+	/* Configure the I2C_CON register */
+	omap_i2c_write_reg(i2c_base, ip_rev, i2c_con_reg, OMAP_I2C_CON_REG);
+
+	/* read/write data bytewise */
+	for (i = 0; i < len; i++) {
+		status = wait_for_event(i2c_base, ip_rev, waitdelay);
+		/* Ignore I2C_STAT_RRDY in transmitter mode */
+		if (i2c_con_reg & I2C_CON_TRX)
+			status &= ~I2C_STAT_RRDY;
+		else
+			status &= ~I2C_STAT_XRDY;
+
+		/* Try to identify bus that is not padconf'd for I2C */
+		if (status == I2C_STAT_XRDY) {
+			i2c_error = 2;
+			printf("I2C xfer: pads on bus probably not configured (status=0x%x)\n",
+			       status);
+			goto xfer_exit;
+		}
+		if (status == 0 || (status & I2C_STAT_NACK)) {
+			i2c_error = 1;
+			printf("I2C xfer: error waiting for ACK (status=0x%x)\n",
+			       status);
+			goto xfer_exit;
+		}
+		if (status & I2C_STAT_XRDY) {
+			/* Transmit data */
+			omap_i2c_write_reg(i2c_base, ip_rev,
+					   buffer[i], OMAP_I2C_DATA_REG);
+			omap_i2c_write_reg(i2c_base, ip_rev,
+					   I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
+		}
+		if (status & I2C_STAT_RRDY) {
+			/* Receive data */
+			*buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
+							OMAP_I2C_DATA_REG);
+			omap_i2c_write_reg(i2c_base, ip_rev,
+					   I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
+		}
+	}
+
+	/*
+	 * poll ARDY bit for making sure that last byte really has been
+	 * transferred on the bus.
+	 */
+	do {
+		status = wait_for_event(i2c_base, ip_rev, waitdelay);
+	} while (!(status & I2C_STAT_ARDY) && timeout--);
+	if (timeout <= 0) {
+		puts("I2C xfer: timed out on last byte!\n");
+		i2c_error = 1;
+		goto xfer_exit;
+	} else {
+		omap_i2c_write_reg(i2c_base, ip_rev, I2C_STAT_ARDY, OMAP_I2C_STAT_REG);
+	}
+
+	/* If Stop bit set, flush FIFO. */
+	if (i2c_con_reg & I2C_CON_STP)
+		goto xfer_exit;
+
+	return 0;
+
+xfer_exit:
+	flush_fifo(i2c_base, ip_rev);
+	omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
+	return i2c_error;
+}
+
+#if !CONFIG_IS_ENABLED(DM_I2C)
+/*
+ * The legacy I2C functions. These need to get removed once
+ * all users of this driver are converted to DM.
+ */
+
  /*
   * i2c_read: Function now uses a single I2C read transaction with bulk transfer
   *           of the requested number of bytes (note that the 'i2c md' command
@@ -836,11 +937,6 @@ wr_exit:
  	return i2c_error;
  }
 
-#if !CONFIG_IS_ENABLED(DM_I2C)
-/*
- * The legacy I2C functions. These need to get removed once
- * all users of this driver are converted to DM.
- */
  static void __iomem *omap24_get_base(struct i2c_adapter *adap)
  {
  	switch (adap->hwadapnr) {
@@ -975,23 +1071,30 @@ static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  {
  	struct omap_i2c *priv = dev_get_priv(bus);
  	int ret;
+	u16 i2c_con_reg = 0;
 
  	debug("i2c_xfer: %d messages\n", nmsgs);
  	for (; nmsgs > 0; nmsgs--, msg++) {
-		debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
-		if (msg->flags & I2C_M_RD) {
-			ret = __omap24_i2c_read(priv->regs, priv->ip_rev,
-						priv->waitdelay,
-						msg->addr, 0, 0, msg->buf,
-						msg->len);
-		} else {
-			ret = __omap24_i2c_write(priv->regs, priv->ip_rev,
-						 priv->waitdelay,
-						 msg->addr, 0, 0, msg->buf,
-						 msg->len);
-		}
+		debug("i2c_xfer: chip=0x%x, len=0x%x, read=0x%x\n",
+		      msg->addr, msg->len, (msg->flags & I2C_M_RD));
+
+		/* Wait until bus not busy */
+		if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
+			return -EREMOTEIO;
+
+		/* Set Controller mode with Start and Stop bit */
+		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
+		/* Set Transmitter/Receiver mode if it is a write/read msg */
+		if (msg->flags & I2C_M_RD)
+			i2c_con_reg &= ~I2C_CON_TRX;
+		else
+			i2c_con_reg |= I2C_CON_TRX;

drivers/i2c/omap24xx_i2c.c static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) · line 1100

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +5 -1
+
+		ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, priv->waitdelay,
+					    msg->addr, msg->buf, msg->len,
+					    i2c_con_reg);
  		if (ret) {
-			debug("i2c_write: error sending\n");
+			debug("i2c_xfer: error sending\n");
  			return -EREMOTEIO;
  		}
  	}

Thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#3 lore raw

Re: [PATCH RFC/RFT 1/3] i2c: omap24xx_i2c: Use new function __omap24_i2c_xfer_msg()

Hello Heiko,

Quoted 64 lines · Heiko Schocher

Hello Aniket,

Quoted 50 lines · Aniket Limaye

Remove __omap24_i2c_read/write() usage from omap_i2c_xfer() in favour of the more flexible __omap24_i2c_xfer_msg().
Consequently, these are also no longer needed when DM_I2C is enabled.

New function __omap24_i2c_xfer_msg() will take care of individual read OR write transfers with a target device. It goes through below sequence:
- Program the provided Target Chip address (OMAP_I2C_SA_REG)
- Program the provided Data len (OMAP_I2C_CNT_REG)
- Program the provided Control register flags (OMAP_I2C_CON_REG)
- Read from or Write to the provided Data buffer (OMAP_I2C_DATA_REG)

For a detailed programming guide, refer to the TRM[0] (12.1.3.4 I2C Programming Guide).

This patch by itself should be a transparent change. However this is needed for implementing a proper Repeated Start (Sr) functionality for i2c_msgs.

Previous implementation for omap_i2c_xfer called __omap24_i2c_read/write functions, with hardcoded addr=0 and alen=0 for each i2c_msg. Each of these calls would program the registers always with a Stop bit set, not allowing for a repeated start between i2c_msgs in the same xfer().

[0]: https://www.ti.com/lit/zip/spruj28 (TRM)

---
  drivers/i2c/omap24xx_i2c.c | 139 ++++++++++++++++++++++++++++++++-----

  1 file changed, 121 insertions(+), 18 deletions(-)
drivers/i2c/omap24xx_i2c.c +12 -0
index ebe472e20cd..0d044121d27 100644
@@ -535,6 +535,107 @@ pr_exit:
       return res;
   }
+static int __omap24_i2c_xfer_msg(void __iomem *i2c_base, int ip_rev, 
 int waitdelay,
+                 uchar chip, uchar *buffer, int len, u16 i2c_con_reg)
+{
+    int i;
+    u16 status;
+    int i2c_error = 0;
+    int timeout = I2C_TIMEOUT;
+
+    if (len < 0) {
+        puts("I2C xfer: data len < 0\n");
+        return 1;
+    }

drivers/i2c/omap24xx_i2c.c pr_exit: · line 559

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +10 -0
+
+    if (!buffer) {
+        puts("I2C xfer: NULL pointer passed\n");
+        return 1;
+    }
+
+    if (!(i2c_con_reg & I2C_CON_EN)) {
+        puts("I2C xfer: I2C_CON_EN not set\n");
+        return 1;
+    }
Quoted 8 lines

Can we use here (and above) a negative error code? Later you only check in omap_i2c_xfer() if returncode is ! 0 and return "-EREMOTEIO" may, you can return here better codes and simply return than this error codes from omap_i2c_xfer() >
And may you change the strings from the puts call to a more driver specifc string ... so its clear from which i2c driver the error message comes... please fix this globally in your patchset.

yeah that makes sense, will update the function with proper error codes and error prints.

Quoted 145 lines
Quoted 139 lines
drivers/i2c/omap24xx_i2c.c +79 -0
+
+    /* Set slave address */
+    omap_i2c_write_reg(i2c_base, ip_rev, chip, OMAP_I2C_SA_REG);
+    /* Read/Write len bytes data */
+    omap_i2c_write_reg(i2c_base, ip_rev, len, OMAP_I2C_CNT_REG);
+    /* Configure the I2C_CON register */
+    omap_i2c_write_reg(i2c_base, ip_rev, i2c_con_reg, OMAP_I2C_CON_REG);
+
+    /* read/write data bytewise */
+    for (i = 0; i < len; i++) {
+        status = wait_for_event(i2c_base, ip_rev, waitdelay);
+        /* Ignore I2C_STAT_RRDY in transmitter mode */
+        if (i2c_con_reg & I2C_CON_TRX)
+            status &= ~I2C_STAT_RRDY;
+        else
+            status &= ~I2C_STAT_XRDY;
+
+        /* Try to identify bus that is not padconf'd for I2C */
+        if (status == I2C_STAT_XRDY) {
+            i2c_error = 2;
+            printf("I2C xfer: pads on bus probably not configured 
 (status=0x%x)\n",
+                   status);
+            goto xfer_exit;
+        }
+        if (status == 0 || (status & I2C_STAT_NACK)) {
+            i2c_error = 1;
+            printf("I2C xfer: error waiting for ACK (status=0x%x)\n",
+                   status);
+            goto xfer_exit;
+        }
+        if (status & I2C_STAT_XRDY) {
+            /* Transmit data */
+            omap_i2c_write_reg(i2c_base, ip_rev,
+                       buffer[i], OMAP_I2C_DATA_REG);
+            omap_i2c_write_reg(i2c_base, ip_rev,
+                       I2C_STAT_XRDY, OMAP_I2C_STAT_REG);
+        }
+        if (status & I2C_STAT_RRDY) {
+            /* Receive data */
+            *buffer++ = omap_i2c_read_reg(i2c_base, ip_rev,
+                            OMAP_I2C_DATA_REG);
+            omap_i2c_write_reg(i2c_base, ip_rev,
+                       I2C_STAT_RRDY, OMAP_I2C_STAT_REG);
+        }
+    }
+
+    /*
+     * poll ARDY bit for making sure that last byte really has been
+     * transferred on the bus.
+     */
+    do {
+        status = wait_for_event(i2c_base, ip_rev, waitdelay);
+    } while (!(status & I2C_STAT_ARDY) && timeout--);
+    if (timeout <= 0) {
+        puts("I2C xfer: timed out on last byte!\n");
+        i2c_error = 1;
+        goto xfer_exit;
+    } else {
+        omap_i2c_write_reg(i2c_base, ip_rev, I2C_STAT_ARDY, 
 OMAP_I2C_STAT_REG);
+    }
+
+    /* If Stop bit set, flush FIFO. */
+    if (i2c_con_reg & I2C_CON_STP)
+        goto xfer_exit;
+
+    return 0;
+
+xfer_exit:
+    flush_fifo(i2c_base, ip_rev);
+    omap_i2c_write_reg(i2c_base, ip_rev, 0xFFFF, OMAP_I2C_STAT_REG);
+    return i2c_error;
+}
+
+#if !CONFIG_IS_ENABLED(DM_I2C)
+/*
+ * The legacy I2C functions. These need to get removed once
+ * all users of this driver are converted to DM.
+ */
+
   /*

   * i2c_read: Function now uses a single I2C read transaction with bulk transfer *           of the requested number of bytes (note that the 'i2c md' command

drivers/i2c/omap24xx_i2c.c +20 -18
@@ -836,11 +937,6 @@ wr_exit:
       return i2c_error;
   }
-#if !CONFIG_IS_ENABLED(DM_I2C)
-/*
- * The legacy I2C functions. These need to get removed once
- * all users of this driver are converted to DM.
- */
   static void __iomem *omap24_get_base(struct i2c_adapter *adap)
   {
       switch (adap->hwadapnr) {
@@ -975,23 +1071,30 @@ static int omap_i2c_xfer(struct udevice *bus, 
 struct i2c_msg *msg, int nmsgs)
   {
       struct omap_i2c *priv = dev_get_priv(bus);
       int ret;
+    u16 i2c_con_reg = 0;
       debug("i2c_xfer: %d messages\n", nmsgs);
       for (; nmsgs > 0; nmsgs--, msg++) {
-        debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
-        if (msg->flags & I2C_M_RD) {
-            ret = __omap24_i2c_read(priv->regs, priv->ip_rev,
-                        priv->waitdelay,
-                        msg->addr, 0, 0, msg->buf,
-                        msg->len);
-        } else {
-            ret = __omap24_i2c_write(priv->regs, priv->ip_rev,
-                         priv->waitdelay,
-                         msg->addr, 0, 0, msg->buf,
-                         msg->len);
-        }
+        debug("i2c_xfer: chip=0x%x, len=0x%x, read=0x%x\n",
+              msg->addr, msg->len, (msg->flags & I2C_M_RD));
+
+        /* Wait until bus not busy */
+        if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
+            return -EREMOTEIO;
+
+        /* Set Controller mode with Start and Stop bit */
+        i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | 
 I2C_CON_STP;
+        /* Set Transmitter/Receiver mode if it is a write/read msg */
+        if (msg->flags & I2C_M_RD)
+            i2c_con_reg &= ~I2C_CON_TRX;
+        else
+            i2c_con_reg |= I2C_CON_TRX;
+
+        ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, 
 priv->waitdelay,
+                        msg->addr, msg->buf, msg->len,
+                        i2c_con_reg);
           if (ret) {
-            debug("i2c_write: error sending\n");
+            debug("i2c_xfer: error sending\n");
               return -EREMOTEIO;
Quoted 3 lines

          }
      }

Thanks!

Quoted 2 lines

bye,
Heiko

Thanks,
Aniket

#4 lore raw

PATCH 2/3 i2c: omap24xx_i2c: support CONFIG_I2C_REPEATED_START in DM_I2C xfer

In reply to Aniket Limaye

Repeated Start Condition (Sr) can be used to transfer multiple i2c msgs without sending a Stop condition (P). So far, the driver default was to always send a Stop condition after every i2c msg.

Add support for a config option (CONFIG_I2C_REPEATED_START) to disable sending the Stop condition by default. If this config is enabled, Stop condition will be sent only if explicitly requested in the msg flags OR if it is the last msg in the transfer.

Consequently, handle the Repeated Start condition (Sr) in the next msg by not calling the wait_for_bb() check since it will simply timeout in the absence of a stop condition (BB will be 1 until Stop is programmed)

---
 drivers/i2c/omap24xx_i2c.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/i2c/omap24xx_i2c.c +16 -6
index 0d044121d27..d3256ab666d 100644
@@ -1074,21 +1074,31 @@ static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
 	u16 i2c_con_reg = 0;
 
 	debug("i2c_xfer: %d messages\n", nmsgs);
-	for (; nmsgs > 0; nmsgs--, msg++) {
+	for (int i = 0; i < nmsgs; i++, msg++) {
 		debug("i2c_xfer: chip=0x%x, len=0x%x, read=0x%x\n",
 		      msg->addr, msg->len, (msg->flags & I2C_M_RD));
 
-		/* Wait until bus not busy */
-		if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
-			return -EREMOTEIO;
+		/* If previous msg sent a Stop or if this is the first msg
+		 * Wait until bus not busy
+		 */
+		if ((i2c_con_reg & I2C_CON_STP) || (i == 0)) {
+			if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
+				return -EREMOTEIO;
+		}
 
-		/* Set Controller mode with Start and Stop bit */
-		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
+		/* Set Controller mode with Start bit */
+		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT;
 		/* Set Transmitter/Receiver mode if it is a write/read msg */
 		if (msg->flags & I2C_M_RD)
 			i2c_con_reg &= ~I2C_CON_TRX;
 		else
 			i2c_con_reg |= I2C_CON_TRX;
+		/* Send Stop condition (P) by default */
+		if (!IS_ENABLED(CONFIG_I2C_REPEATED_START))
+			i2c_con_reg |= I2C_CON_STP;
+		/* Send Stop if explicitly requested or if this is the last msg */
+		if ((msg->flags & I2C_M_STOP) || (i == nmsgs - 1))
+			i2c_con_reg |= I2C_CON_STP;
 
 		ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, priv->waitdelay,
 					    msg->addr, msg->buf, msg->len,
Signature · 1 line
2.48.1
#5 lore raw

Re: [PATCH RFC/RFT 2/3] i2c: omap24xx_i2c: support CONFIG_I2C_REPEATED_START in DM_I2C xfer

Hello Aniket,

Quoted 27 lines · Aniket Limaye

Repeated Start Condition (Sr) can be used to transfer multiple i2c msgs without sending a Stop condition (P). So far, the driver default was to always send a Stop condition after every i2c msg.

Add support for a config option (CONFIG_I2C_REPEATED_START) to disable sending the Stop condition by default. If this config is enabled, Stop condition will be sent only if explicitly requested in the msg flags OR if it is the last msg in the transfer.

Consequently, handle the Repeated Start condition (Sr) in the next msg by not calling the wait_for_bb() check since it will simply timeout in the absence of a stop condition (BB will be 1 until Stop is programmed)

---
  drivers/i2c/omap24xx_i2c.c | 22 ++++++++++++++++------
  1 file changed, 16 insertions(+), 6 deletions(-)
drivers/i2c/omap24xx_i2c.c +0 -1
index 0d044121d27..d3256ab666d 100644
@@ -1074,21 +1074,31 @@ static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  	u16 i2c_con_reg = 0;
 
  	debug("i2c_xfer: %d messages\n", nmsgs);
-	for (; nmsgs > 0; nmsgs--, msg++) {

drivers/i2c/omap24xx_i2c.c static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) · line 1083

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +4 -3
+	for (int i = 0; i < nmsgs; i++, msg++) {
  		debug("i2c_xfer: chip=0x%x, len=0x%x, read=0x%x\n",
  		      msg->addr, msg->len, (msg->flags & I2C_M_RD));
 
-		/* Wait until bus not busy */
-		if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
-			return -EREMOTEIO;
+		/* If previous msg sent a Stop or if this is the first msg
+		 * Wait until bus not busy
+		 */

Nitpick: I would like more

		/*
		 * If previous msg sent a Stop or if this is the first msg

but unsure if we have here a strict rule...

drivers/i2c/omap24xx_i2c.c static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) · line 1087

Quoted 4 lines
drivers/i2c/omap24xx_i2c.c +4 -0
+		if ((i2c_con_reg & I2C_CON_STP) || (i == 0)) {
+			if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
+				return -EREMOTEIO;
+		}

You do not need the {} brackets here

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +2 -2
-		/* Set Controller mode with Start and Stop bit */
-		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
+		/* Set Controller mode with Start bit */
+		i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT;
  		/* Set Transmitter/Receiver mode if it is a write/read msg */
  		if (msg->flags & I2C_M_RD)
  			i2c_con_reg &= ~I2C_CON_TRX;
  		else
  			i2c_con_reg |= I2C_CON_TRX;

drivers/i2c/omap24xx_i2c.c static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +6 -0
+		/* Send Stop condition (P) by default */
+		if (!IS_ENABLED(CONFIG_I2C_REPEATED_START))
+			i2c_con_reg |= I2C_CON_STP;
+		/* Send Stop if explicitly requested or if this is the last msg */
+		if ((msg->flags & I2C_M_STOP) || (i == nmsgs - 1))
+			i2c_con_reg |= I2C_CON_STP;
 
  		ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, priv->waitdelay,
  					    msg->addr, msg->buf, msg->len,
 

Thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#6 lore raw

Re: [PATCH RFC/RFT 2/3] i2c: omap24xx_i2c: support CONFIG_I2C_REPEATED_START in DM_I2C xfer

Quoted 42 lines · Heiko Schocher

Hello Aniket,

Quoted 26 lines · Aniket Limaye

Repeated Start Condition (Sr) can be used to transfer multiple i2c msgs without sending a Stop condition (P). So far, the driver default was to always send a Stop condition after every i2c msg.

Add support for a config option (CONFIG_I2C_REPEATED_START) to disable sending the Stop condition by default. If this config is enabled, Stop condition will be sent only if explicitly requested in the msg flags OR if it is the last msg in the transfer.

Consequently, handle the Repeated Start condition (Sr) in the next msg by not calling the wait_for_bb() check since it will simply timeout in the absence of a stop condition (BB will be 1 until Stop is programmed)

---
  drivers/i2c/omap24xx_i2c.c | 22 ++++++++++++++++------

  1 file changed, 16 insertions(+), 6 deletions(-)
drivers/i2c/omap24xx_i2c.c +0 -0
index 0d044121d27..d3256ab666d 100644
@@ -1074,21 +1074,31 @@ static int omap_i2c_xfer(struct udevice *bus, 
 struct i2c_msg *msg, int nmsgs)
       u16 i2c_con_reg = 0;
       debug("i2c_xfer: %d messages\n", nmsgs);

drivers/i2c/omap24xx_i2c.c static int omap_i2c_xfer(struct udevice *bus, · line 1082

Quoted 10 lines
drivers/i2c/omap24xx_i2c.c +4 -4
-    for (; nmsgs > 0; nmsgs--, msg++) {
+    for (int i = 0; i < nmsgs; i++, msg++) {
           debug("i2c_xfer: chip=0x%x, len=0x%x, read=0x%x\n",
                 msg->addr, msg->len, (msg->flags & I2C_M_RD));
-        /* Wait until bus not busy */
-        if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
-            return -EREMOTEIO;
+        /* If previous msg sent a Stop or if this is the first msg
+         * Wait until bus not busy
+         */

Nitpick: I would like more

Quoted 5 lines

        /*
         * If previous msg sent a Stop or if this is the first msg

but unsure if we have here a strict rule...

Sure, will do.

Quoted 7 lines

drivers/i2c/omap24xx_i2c.c static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) · line 1087

Quoted 4 lines
drivers/i2c/omap24xx_i2c.c +4 -0
+        if ((i2c_con_reg & I2C_CON_STP) || (i == 0)) {
+            if (wait_for_bb(priv->regs, priv->ip_rev, priv->waitdelay))
+                return -EREMOTEIO;
+        }

You do not need the {} brackets here

Right... didn't catch that
Will update in the patch series

Quoted 25 lines
Quoted 11 lines
drivers/i2c/omap24xx_i2c.c +2 -2
-        /* Set Controller mode with Start and Stop bit */
-        i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | 
 I2C_CON_STP;
+        /* Set Controller mode with Start bit */
+        i2c_con_reg = I2C_CON_EN | I2C_CON_MST | I2C_CON_STT;
           /* Set Transmitter/Receiver mode if it is a write/read msg */
           if (msg->flags & I2C_M_RD)
               i2c_con_reg &= ~I2C_CON_TRX;

          else
              i2c_con_reg |= I2C_CON_TRX;
+        /* Send Stop condition (P) by default */

Quoted 9 lines

+        if (!IS_ENABLED(CONFIG_I2C_REPEATED_START))
+            i2c_con_reg |= I2C_CON_STP;
+        /* Send Stop if explicitly requested or if this is the last msg */
+        if ((msg->flags & I2C_M_STOP) || (i == nmsgs - 1))
+            i2c_con_reg |= I2C_CON_STP;
          ret = __omap24_i2c_xfer_msg(priv->regs, priv->ip_rev, priv->waitdelay, msg->addr, msg->buf, msg->len,

Thanks!

bye,
Heiko

#7 lore raw

PATCH 3/3 drivers: i2c: Kconfig: Add config for I2C_REPEATED_START

In reply to Aniket Limaye

Add a Kconfig option to disable sending Stop conditions between multiple i2c_msgs within a single xfer. Enable it by default for ARCH_K3 platforms.

The namespace for the config is not respecting the CONFIG_SYS_I2C convention since the legacy API has already been using CONFIG_I2C_REPEATED_START

---
 drivers/i2c/Kconfig | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/i2c/Kconfig +9 -0
index cdae6825736..ade86a71fb0 100644
@@ -503,6 +503,15 @@ config SYS_I2C_OMAP24XX
 	help
 	  Add support for the OMAP2+ I2C driver.
 
+config I2C_REPEATED_START
+	bool "Enable I2C repeated start"
+	depends on SYS_I2C_OMAP24XX
+	default y if ARCH_K3
+	help
+	  Enable support for repeated start. Updates driver defaults to not
+	  send a Stop condition and issue Repeated Start (Sr) for subsequent
+	  i2c msgs.
+
 config SYS_I2C_RCAR_I2C
 	bool "Renesas R-Car I2C driver"
 	depends on (RCAR_GEN2 || RCAR_64) && DM_I2C
Signature · 1 line
2.48.1
#8 lore raw
In reply to Aniket Limaye
Quoted 13 lines · Aniket Limaye

This series implements Repeated Start functionality for the DM_I2C xfer API (omap_i2c_xfer())

First, split out the actual implementation for each individual i2c msg transfer into a new function __omap24_i2c_xfer_msg() and use this to implement omap_i2c_xfer().

Then, add support for the Repeated Start condition in omap_i2c_xfer() through the CONFIG_I2C_REPEATED_START option.

Also add this config to Kconfig and enable it for ARCH_K3 platforms by default.

Quoted 8 lines

Test logs (On AM68-SK with additional debug prints):
https://gist.github.com/aniket-l/8c64c5bc81246751a02836a96510df14

Requesting reviews/testing on specifically the CONFIG_I2C_REPEATED_START addition:
- Is the Kconfig needed or should we simply always default to not sending Stop bit? Drivers can use i2c_msg.flags to request Stop bit for each msg, same as in the Linux Kernel i2c-omap driver[0].

Yes, a Kconfig is needed as this driver works (as implied by the name) on so much old and differently buggy IP.

Quoted 2 lines

- If yes, Should we re-use CONFIG_I2C_REPEATED_START from the legacy implementation of the driver or define a new config

Lets pick a new option, under the SYS_I2C_OMAP24XX namespace (and remove the legacy and unused variant).

Quoted 1 line

- Default enable it for K3 devices?

I'm fine with that.

Signature · 1 line
Tom
#9 lore raw

Hello Tom, Aniket,

Quoted 29 lines · Tom Rini
Quoted 13 lines · Aniket Limaye

This series implements Repeated Start functionality for the DM_I2C xfer API (omap_i2c_xfer())

First, split out the actual implementation for each individual i2c msg transfer into a new function __omap24_i2c_xfer_msg() and use this to implement omap_i2c_xfer().

Then, add support for the Repeated Start condition in omap_i2c_xfer() through the CONFIG_I2C_REPEATED_START option.

Also add this config to Kconfig and enable it for ARCH_K3 platforms by default.

Quoted 8 lines

Test logs (On AM68-SK with additional debug prints):
https://gist.github.com/aniket-l/8c64c5bc81246751a02836a96510df14

Requesting reviews/testing on specifically the CONFIG_I2C_REPEATED_START addition:
- Is the Kconfig needed or should we simply always default to not sending Stop bit? Drivers can use i2c_msg.flags to request Stop bit for each msg, same as in the Linux Kernel i2c-omap driver[0].

Yes, a Kconfig is needed as this driver works (as implied by the name) on so much old and differently buggy IP.

Quoted 2 lines

- If yes, Should we re-use CONFIG_I2C_REPEATED_START from the legacy implementation of the driver or define a new config

Quoted 7 lines

Lets pick a new option, under the SYS_I2C_OMAP24XX namespace (and remove the legacy and unused variant).

Quoted 1 line

- Default enable it for K3 devices?

I'm fine with that.

Yep, fully aggree, we need the Kconfig symbol for backward compatibility.

Thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: [email protected]
#10 lore raw

Hello Tom, Heiko,

Quoted 35 lines · Heiko Schocher

Hello Tom, Aniket,

Quoted 32 lines · Tom Rini
Quoted 13 lines · Aniket Limaye

This series implements Repeated Start functionality for the DM_I2C xfer API (omap_i2c_xfer())

First, split out the actual implementation for each individual i2c msg transfer into a new function __omap24_i2c_xfer_msg() and use this to implement omap_i2c_xfer().

Then, add support for the Repeated Start condition in omap_i2c_xfer() through the CONFIG_I2C_REPEATED_START option.

Also add this config to Kconfig and enable it for ARCH_K3 platforms by default.

Quoted 8 lines

Test logs (On AM68-SK with additional debug prints):
https://gist.github.com/aniket-l/8c64c5bc81246751a02836a96510df14

Requesting reviews/testing on specifically the CONFIG_I2C_REPEATED_START addition:
- Is the Kconfig needed or should we simply always default to not sending Stop bit? Drivers can use i2c_msg.flags to request Stop bit for each msg, same as in the Linux Kernel i2c-omap driver[0].

Yes, a Kconfig is needed as this driver works (as implied by the name) on so much old and differently buggy IP.

Quoted 2 lines

- If yes, Should we re-use CONFIG_I2C_REPEATED_START from the legacy implementation of the driver or define a new config

Lets pick a new option, under the SYS_I2C_OMAP24XX namespace (and remove the legacy and unused variant).

I will update the patch with the proper namespace for this config.

The legacy one (CONFIG_I2C_REPEATED_START) was never added to Kconfig at all... So will just leave it be ( unless you meant remove support for it from the legacy driver code as well and use this new one instead? )

Quoted 11 lines
Quoted 4 lines
Quoted 1 line

- Default enable it for K3 devices?

I'm fine with that.

Yep, fully aggree, we need the Kconfig symbol for backward compatibility.

Thanks!

bye,
Heiko

Thanks for your reviews!

Regards,
Aniket

#11 lore raw
Quoted 39 lines · Aniket Limaye

Hello Tom, Heiko,

Quoted 35 lines · Heiko Schocher

Hello Tom, Aniket,

Quoted 32 lines · Tom Rini
Quoted 13 lines · Aniket Limaye

This series implements Repeated Start functionality for the DM_I2C xfer API (omap_i2c_xfer())

First, split out the actual implementation for each individual i2c msg transfer into a new function __omap24_i2c_xfer_msg() and use this to implement omap_i2c_xfer().

Then, add support for the Repeated Start condition in omap_i2c_xfer() through the CONFIG_I2C_REPEATED_START option.

Also add this config to Kconfig and enable it for ARCH_K3 platforms by default.

Quoted 8 lines

Test logs (On AM68-SK with additional debug prints):
https://gist.github.com/aniket-l/8c64c5bc81246751a02836a96510df14

Requesting reviews/testing on specifically the CONFIG_I2C_REPEATED_START addition:
- Is the Kconfig needed or should we simply always default to not sending Stop bit? Drivers can use i2c_msg.flags to request Stop bit for each msg, same as in the Linux Kernel i2c-omap driver[0].

Yes, a Kconfig is needed as this driver works (as implied by the name) on so much old and differently buggy IP.

Quoted 2 lines

- If yes, Should we re-use CONFIG_I2C_REPEATED_START from the legacy implementation of the driver or define a new config

Lets pick a new option, under the SYS_I2C_OMAP24XX namespace (and remove the legacy and unused variant).

Quoted 5 lines

I will update the patch with the proper namespace for this config.

The legacy one (CONFIG_I2C_REPEATED_START) was never added to Kconfig at all... So will just leave it be ( unless you meant remove support for it from the legacy driver code as well and use this new one instead? )

Yes, please remove the useless legacy code in an earlier patch to the series, thanks.

Signature · 1 line
Tom