[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <51a72ceca0154d7be85c3cc67722e7dd0b364a2e.1760000254.git.matthias.schiffer@ew.tq-group.com>
Date: Thu, 9 Oct 2025 11:19:49 +0200
From: Matthias Schiffer <matthias.schiffer@...tq-group.com>
To: Peter Korsgaard <peter@...sgaard.com>,
Andrew Lunn <andrew@...n.ch>,
Andi Shyti <andi.shyti@...nel.org>
Cc: linux-i2c@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux@...tq-group.com,
Matthias Schiffer <matthias.schiffer@...tq-group.com>
Subject: [PATCH v2 1/2] i2c: ocores: increase poll timeout to total transfer timeout
When a target makes use of clock stretching, a timeout of 1ms may not be
enough. One extreme example is the NXP PTN3460 eDP to LVDS bridge, which
takes ~320ms to send its ACK after a flash command has been
submitted.
The behavior in the regular case is unchanged, spinning for up to 1ms,
but the open-coded poll loop is replaced with read_poll_timeout_atomic()
as suggested by Andrew Lunn. In cases where 1ms is not sufficient,
read_poll_timeout() is used, allowing a total transfer time up to the
timeout set in struct i2c_adapter (defaulting to 1s, configurable through
the I2C_TIMEOUT ioctl).
Signed-off-by: Matthias Schiffer <matthias.schiffer@...tq-group.com>
---
v2: avoid spinning for a whole second, switch to
read_poll_timeout[_atomic]()
drivers/i2c/busses/i2c-ocores.c | 44 ++++++++++++++++-----------------
1 file changed, 22 insertions(+), 22 deletions(-)
diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c
index 482b37c8a1297..c4587194d46be 100644
--- a/drivers/i2c/busses/i2c-ocores.c
+++ b/drivers/i2c/busses/i2c-ocores.c
@@ -18,6 +18,7 @@
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
+#include <linux/iopoll.h>
#include <linux/wait.h>
#include <linux/platform_data/i2c-ocores.h>
#include <linux/slab.h>
@@ -258,7 +259,7 @@ static void ocores_process_timeout(struct ocores_i2c *i2c)
* @reg: register to query
* @mask: bitmask to apply on register value
* @val: expected result
- * @timeout: timeout in jiffies
+ * @timeout: absolute timeout in jiffies
*
* Timeout is necessary to avoid to stay here forever when the chip
* does not answer correctly.
@@ -269,30 +270,32 @@ static int ocores_wait(struct ocores_i2c *i2c,
int reg, u8 mask, u8 val,
const unsigned long timeout)
{
- unsigned long j;
-
- j = jiffies + timeout;
- while (1) {
- u8 status = oc_getreg(i2c, reg);
+ unsigned long max_wait;
+ u8 status;
+ int ret;
- if ((status & mask) == val)
- break;
+ /* In most cases the wait is short, so we spin for up to 1ms. */
+ ret = read_poll_timeout_atomic(oc_getreg, status,
+ (status & mask) == val,
+ 0, 1000, false, i2c, reg);
+ if (ret != -ETIMEDOUT)
+ return ret;
- if (time_after(jiffies, j))
- return -ETIMEDOUT;
- }
- return 0;
+ max_wait = jiffies_to_usecs(max(0L, timeout - jiffies)) + 1;
+ return read_poll_timeout(oc_getreg, status, (status & mask) == val,
+ 10000, max_wait, false, i2c, reg);
}
/**
* ocores_poll_wait() - Wait until is possible to process some data
* @i2c: ocores I2C device instance
+ * @timeout: absolute timeout in jiffies
*
* Used when the device is in polling mode (interrupts disabled).
*
* Return: 0 on success, -ETIMEDOUT on timeout
*/
-static int ocores_poll_wait(struct ocores_i2c *i2c)
+static int ocores_poll_wait(struct ocores_i2c *i2c, unsigned long timeout)
{
u8 mask;
int err;
@@ -310,15 +313,11 @@ static int ocores_poll_wait(struct ocores_i2c *i2c)
udelay((8 * 1000) / i2c->bus_clock_khz);
}
- /*
- * once we are here we expect to get the expected result immediately
- * so if after 1ms we timeout then something is broken.
- */
- err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1));
+ err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, timeout);
if (err)
- dev_warn(i2c->adap.dev.parent,
- "%s: STATUS timeout, bit 0x%x did not clear in 1ms\n",
- __func__, mask);
+ dev_dbg(i2c->adap.dev.parent,
+ "%s: STATUS timeout, bit 0x%x did not clear\n",
+ __func__, mask);
return err;
}
@@ -336,11 +335,12 @@ static int ocores_poll_wait(struct ocores_i2c *i2c)
*/
static int ocores_process_polling(struct ocores_i2c *i2c)
{
+ unsigned long timeout = jiffies + i2c->adap.timeout;
irqreturn_t ret;
int err = 0;
while (1) {
- err = ocores_poll_wait(i2c);
+ err = ocores_poll_wait(i2c, timeout);
if (err)
break; /* timeout */
--
TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany
Amtsgericht München, HRB 105018
Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider
https://www.tq-group.com/
Powered by blists - more mailing lists