Skip to content

Commit

Permalink
spi: stm32: fix stm32_spi_prepare_mbr() that halves spi clk for every…
Browse files Browse the repository at this point in the history
… run

When this driver is used with a driver that uses preallocated spi_transfer
structs. The speed_hz is halved by every run. This results in:

spi_stm32 44004000.spi: SPI transfer setup failed
ads7846 spi0.0: SPI transfer failed: -22

Example when running with DIV_ROUND_UP():
- First run; speed_hz = 1000000, spi->clk_rate 125000000
  div 125 -> mbrdiv = 7, cur_speed = 976562
- Second run; speed_hz = 976562
  div 128,00007 (roundup to 129) -> mbrdiv = 8, cur_speed = 488281
- Third run; speed_hz = 488281
  div 256,000131072067109 (roundup to 257) and then -EINVAL is returned.

Use DIV_ROUND_CLOSEST to allow to round down and allow us to keep the
set speed.

Signed-off-by: Sean Nyekjaer <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Mark Brown <[email protected]>
  • Loading branch information
sknsean authored and broonie committed Nov 3, 2022
1 parent 134af9a commit 62aa1a3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/spi/spi-stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
u32 div, mbrdiv;

/* Ensure spi->clk_rate is even */
div = DIV_ROUND_UP(spi->clk_rate & ~0x1, speed_hz);
div = DIV_ROUND_CLOSEST(spi->clk_rate & ~0x1, speed_hz);

/*
* SPI framework set xfer->speed_hz to master->max_speed_hz if
Expand Down

0 comments on commit 62aa1a3

Please sign in to comment.