Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

i2c: ocores: Use read_poll_timeout_atomic to avoid false poll timeouts

Replace the manual polling loop in ocores_wait() with the kernel helper
read_poll_timeout_atomic(). This simplifies the code and ensures robust
timeout handling.

In particular, the helper guarantees a condition check after the
delay, even if the delay exceeds the timeout, avoiding spurious
timeout errors under load or preemption.

Signed-off-by: Martin Aberer <martin.aberer@bachmann.info>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Andi Shyti <andi.shyti@kernel.org>
Link: https://lore.kernel.org/r/20260324140556.2249039-1-martin.aberer@bachmann.info

authored by

Martin Aberer and committed by
Andi Shyti
8461f5e3 3762e535

+9 -15
+9 -15
drivers/i2c/busses/i2c-ocores.c
··· 24 24 #include <linux/io.h> 25 25 #include <linux/log2.h> 26 26 #include <linux/spinlock.h> 27 + #include <linux/iopoll.h> 27 28 #include <linux/jiffies.h> 28 29 29 30 /* ··· 259 258 * @reg: register to query 260 259 * @mask: bitmask to apply on register value 261 260 * @val: expected result 262 - * @timeout: timeout in jiffies 261 + * @timeout_us: timeout in microseconds 263 262 * 264 263 * Timeout is necessary to avoid to stay here forever when the chip 265 264 * does not answer correctly. ··· 268 267 */ 269 268 static int ocores_wait(struct ocores_i2c *i2c, 270 269 int reg, u8 mask, u8 val, 271 - const unsigned long timeout) 270 + unsigned long timeout_us) 272 271 { 273 - unsigned long j; 272 + u8 status; 274 273 275 - j = jiffies + timeout; 276 - while (1) { 277 - u8 status = oc_getreg(i2c, reg); 278 - 279 - if ((status & mask) == val) 280 - break; 281 - 282 - if (time_after(jiffies, j)) 283 - return -ETIMEDOUT; 284 - } 285 - return 0; 274 + return read_poll_timeout_atomic(oc_getreg, status, 275 + (status & mask) == val, 276 + 0, timeout_us, false, 277 + i2c, reg); 286 278 } 287 279 288 280 /** ··· 308 314 * once we are here we expect to get the expected result immediately 309 315 * so if after 1ms we timeout then something is broken. 310 316 */ 311 - err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, msecs_to_jiffies(1)); 317 + err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, 1000); 312 318 if (err) 313 319 dev_warn(i2c->adap.dev.parent, 314 320 "%s: STATUS timeout, bit 0x%x did not clear in 1ms\n",