All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/6] spi: Implement a atomic transfer as a last resort
@ 2026-11-13 13:50 ` Heiko Stuebner
  0 siblings, 0 replies; 20+ messages in thread
From: Heiko Stuebner @ 2026-11-13 13:50 UTC (permalink / raw)
  To: broonie
  Cc: Heiko Stuebner, linux-spi, linux-arm-kernel, linux-rockchip,
	quentin.schulz, Lee Jones, Sebastian Reichel

SPI devices at times are used as PMICs including a power-controller
functionality that should turn off a system.

This happens in an atomic context very late during the shutdown, where
no interrupts are available anymore and conflicts with the asynchronous
nature of the SPI subsystem.


====== History ======
For the RK806 spi-connected PMIC this has already caused interesting
artifacts.

A first change, tried to "fix" the issue by using the
SYS_OFF_MODE_POWER_OFF_PREPARE step of poweroff. While this hides the
issue, on some devices this now turns the pmic off too early before
other important steps are done.

To mitigate that issue for "some" devices, a change [0] tried to use
different poweroff phases for i2c and spi devices, where Lee rightfully
said, that with this no proper solution would be done - and Lee was right,
as that issue now is open since 2024.

The newest change is a simple revert back the old way [1] because of
those issues caused on other devices, but that now breaks the SPI side
again and also did not take into account that long history.


====== Now =======
The I2C framework has an atomic transfer callback for exactly this late
stage transfers since
commit 63b96983a5dd ("i2c: core: introduce callbacks for atomic transfers")
and during the discussion it was suggested implementing something like
this for SPI too.


As this did not seem to have happened yet, I thought I'd try my luck.

Similar to I2C this is meant only for that latest stage so I stole the
gating from there too.

I assume I did not take some SPI specifics into account, but I guess
having some code to discuss is a starting point at least :-)


===== Testing =====
Adding the below diff allows the atomic transfer to also work as main
transfer function for testing purposes. As SPI's ->transfer() call
already lives inside a spinlock_irqsafe context, this allows to find
any sleeping elements easily.

@@ -953,6 +953,9 @@
 
        /* Only register atomic transfer in host-mode */
        if (!target_mode) {
+               pm_runtime_resume_and_get(rs->dev);
+               ctlr->transfer = rockchip_spi_transfer_atomic;
+
                ctlr->transfer_atomic = rockchip_spi_transfer_atomic;
 
                ret = devm_register_sys_off_handler(&pdev->dev,


===== New =====
changes in v2:
address review comments from Mark Brown
- core: preserve old behaviour for drivers not declaring an atomic
  transfer function
- core: don't use message optimization - safer, but still validate the
  message to get bits_per_word defaults etc
- core: make spi_delay_exec usable from atomic contexts
- rockchip: only register atomic transfer in host-mode
- rockchip: provide a wait_for_idle_atomic function, to
  not require (non-working) jiffies for timeout
- rockchip: register additional restart handler to enable runtime pm
  before reboots too
- rockchip: drop runtime-pm from atomic transfer - would sleep anyway
- rockchip: don't prevent power_off, if runtime-resume fails
- rockchip: use core spi's spi_set_cs to also handle gpio-cs


[0] https://lore.kernel.org/lkml/20240730180903.81688-1-sebastian.reichel@collabora.com/
[1] https://lore.kernel.org/all/20260908-veyron-shutdown-v1-1-066ecb80f5f2@gmail.com/


Cc: Heiko Stuebner <heiko@sntech.de>
Cc: linux-spi@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-rockchip@lists.infradead.org
Cc: quentin.schulz@cherry.de
Cc: Lee Jones <lee@kernel.org>
Cc: Sebastian Reichel <sebastian.reichel@collabora.com>


Heiko Stuebner (6):
  spi: Allow spi_delay_exec() to be used from atomic contexts
  spi: Export spi_set_cs() function
  spi: Add infrastructure for atomic sync transfers
  spi: rockchip: Fix runtime resume error handling
  spi: rockchip: Move to pm_runtime_resume_and_get
  spi: rockchip: Add an atomic transfer function

 drivers/spi/spi-rockchip.c | 126 +++++++++++++++++++++++++++++++++++--
 drivers/spi/spi.c          |  77 ++++++++++++++++++++---
 include/linux/spi/spi.h    |  13 ++++
 3 files changed, 204 insertions(+), 12 deletions(-)

-- 
2.53.0


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

end of thread, other threads:[~2026-11-13 19:04 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-13 13:50 [PATCH v2 0/6] spi: Implement a atomic transfer as a last resort Heiko Stuebner
2026-11-13 13:50 ` Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 1/6] spi: Allow spi_delay_exec() to be used from atomic contexts Heiko Stuebner
2026-11-13 13:50   ` Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 2/6] spi: Export spi_set_cs() function Heiko Stuebner
2026-11-13 13:50   ` Heiko Stuebner
2026-11-13 13:56   ` Heiko Stübner
2026-11-13 13:56     ` Heiko Stübner
2026-11-13 18:30     ` Heiko Stuebner
2026-11-13 18:30       ` Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 3/6] spi: Add infrastructure for atomic sync transfers Heiko Stuebner
2026-11-13 13:50   ` Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 4/6] spi: rockchip: Fix runtime resume error handling Heiko Stuebner
2026-11-13 13:50   ` Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 5/6] spi: rockchip: Move to pm_runtime_resume_and_get Heiko Stuebner
2026-11-13 13:50   ` Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 6/6] spi: rockchip: Add an atomic transfer function Heiko Stuebner
2026-11-13 13:50   ` Heiko Stuebner
2026-11-13 19:04   ` Sebastian Reichel
2026-11-13 19:04     ` Sebastian Reichel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.