linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: stm32: fix stm32_spi_prepare_mbr() that halves spi clk for every run
@ 2022-11-03  8:00 Sean Nyekjaer
  2022-11-03 15:08 ` Mark Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Sean Nyekjaer @ 2022-11-03  8:00 UTC (permalink / raw)
  To: Alain Volmat, Mark Brown, Maxime Coquelin, Alexandre Torgue
  Cc: Sean Nyekjaer, linux-spi, linux-stm32, linux-arm-kernel,
	linux-kernel

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 <sean@geanix.com>
---
 drivers/spi/spi-stm32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/spi/spi-stm32.c b/drivers/spi/spi-stm32.c
index 6fe617b445a5..d6833361409d 100644
--- a/drivers/spi/spi-stm32.c
+++ b/drivers/spi/spi-stm32.c
@@ -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
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] spi: stm32: fix stm32_spi_prepare_mbr() that halves spi clk for every run
  2022-11-03  8:00 [PATCH] spi: stm32: fix stm32_spi_prepare_mbr() that halves spi clk for every run Sean Nyekjaer
@ 2022-11-03 15:08 ` Mark Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2022-11-03 15:08 UTC (permalink / raw)
  To: Maxime Coquelin, Sean Nyekjaer, Alexandre Torgue, Alain Volmat
  Cc: linux-spi, linux-arm-kernel, linux-stm32, linux-kernel

On Thu, 3 Nov 2022 09:00:42 +0100, Sean Nyekjaer wrote:
> 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.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/1] spi: stm32: fix stm32_spi_prepare_mbr() that halves spi clk for every run
      commit: 62aa1a344b0904549f6de7af958e8a1136fd5228

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-03 15:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-03  8:00 [PATCH] spi: stm32: fix stm32_spi_prepare_mbr() that halves spi clk for every run Sean Nyekjaer
2022-11-03 15:08 ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).