Skip to content

Commit d8e4ebf

Browse files
miquelraynalbroonie
authored andcommitted
spi: Create a helper to derive adaptive timeouts
Big transfers might take a bit of time, too constraining timeouts might lead to false positives. In order to simplify the drivers work and with the goal of factorizing code in mind, let's add a helper that can be used by any spi controller driver to derive a relevant per-transfer timeout value. The logic is simple: we know how much time it would take to transfer a byte, we can easily derive the total theoretical amount of time involved for each transfer. We multiply it by two to have a bit of margin and enforce a minimum of 500ms. Suggested-by: Mark Brown <[email protected]> Signed-off-by: Miquel Raynal <[email protected]> Link: https://lore.kernel.org/r/Message-Id: <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent e6afe03 commit d8e4ebf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

include/linux/spi/spi.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,23 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
12611261
return false;
12621262
}
12631263

1264+
/**
1265+
* spi_controller_xfer_timeout - Compute a suitable timeout value
1266+
* @ctlr: SPI device
1267+
* @xfer: Transfer descriptor
1268+
*
1269+
* Compute a relevant timeout value for the given transfer. We derive the time
1270+
* that it would take on a single data line and take twice this amount of time
1271+
* with a minimum of 500ms to avoid false positives on loaded systems.
1272+
*
1273+
* Returns: Transfer timeout value in milliseconds.
1274+
*/
1275+
static inline unsigned int spi_controller_xfer_timeout(struct spi_controller *ctlr,
1276+
struct spi_transfer *xfer)
1277+
{
1278+
return max(xfer->len * 8 * 2 / (xfer->speed_hz / 1000), 500U);
1279+
}
1280+
12641281
/*---------------------------------------------------------------------------*/
12651282

12661283
/* SPI transfer replacement methods which make use of spi_res */

0 commit comments

Comments
 (0)