From: Heiko Stuebner <heiko@sntech.de>
To: broonie@kernel.org
Cc: Heiko Stuebner <heiko@sntech.de>,
linux-spi@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, quentin.schulz@cherry.de,
Lee Jones <lee@kernel.org>,
Sebastian Reichel <sebastian.reichel@collabora.com>,
Heiko Stuebner <heiko.stuebner@cherry.de>
Subject: [PATCH v2 3/6] spi: Add infrastructure for atomic sync transfers
Date: Fri, 13 Nov 2026 14:50:14 +0100 [thread overview]
Message-ID: <20261113135017.1047959-4-heiko@sntech.de> (raw)
In-Reply-To: <20261113135017.1047959-1-heiko@sntech.de>
From: Heiko Stuebner <heiko.stuebner@cherry.de>
There are cases where SPI transfers are needed during times without
interrupts like shutdown when a PMIC should turn off the system.
I2C already has atomic transfers for exactly that case, so introduce a
similar concept for SPI. Similar to I2C, this is and should stay a
corner-case, so is only allowed during the shutdown stages of the kernel,
when interrupts are already turned off.
So add an optional transfer_atomic callback, drivers can implement to do
immediate uninterrupted transfers and let spi_sync() determine if the
state is late enough to use it.
For drivers without a transfer_atomic callback defined, the previous
behaviour is kept for backwards compatiblity.
spi_async() does not require a sleeping context for itself, so while it
could in theory also be included, its caller waiting for the completion
can sleep, so would still cause the splat about it.
Signed-off-by: Heiko Stuebner <heiko.stuebner@cherry.de>
---
drivers/spi/spi.c | 58 ++++++++++++++++++++++++++++++++++++++---
include/linux/spi/spi.h | 11 ++++++++
2 files changed, 65 insertions(+), 4 deletions(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index b268357e933c..0b1e07d87562 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -5027,6 +5027,37 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message)
return status;
}
+static int __spi_sync_atomic(struct spi_device *spi, struct spi_message *message)
+{
+ struct spi_controller *ctlr = spi->controller;
+ unsigned long flags;
+ int status;
+
+ if (!ctlr->transfer_atomic) {
+ dev_err(&ctlr->dev, "No atomic transfer handler defined\n");
+ return -EOPNOTSUPP;
+ }
+
+ if (__spi_check_suspended(ctlr)) {
+ dev_warn_once(&spi->dev, "Attempted to sync while suspend\n");
+ return -ESHUTDOWN;
+ }
+
+ /* Not using spi_maybe_optimize_message, so just validate */
+ status = __spi_validate(spi, message);
+ if (status)
+ return status;
+
+ spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
+ status = ctlr->transfer_atomic(spi, message);
+ spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
+
+ if (status == 0)
+ status = message->status;
+
+ return status;
+}
+
/**
* spi_sync - blocking/synchronous SPI data transfers
* @spi: device with which data will be exchanged
@@ -5037,6 +5068,10 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message)
* is non-interruptible, and has no timeout. Low-overhead controller
* drivers may DMA directly into and out of the message buffers.
*
+ * During the time when the system is being shutdown and a transfer_atomic
+ * function is set, this call may also be used from a context that does
+ * not allow sleeping, i.e. with disabled interrupts.
+ *
* Note that the SPI device's chip select is active during the message,
* and then is normally disabled between messages. Drivers for some
* frequently-used devices may want to minimize costs of selecting a chip,
@@ -5050,11 +5085,17 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message)
*/
int spi_sync(struct spi_device *spi, struct spi_message *message)
{
+ struct spi_controller *ctlr = spi->controller;
int ret;
- mutex_lock(&spi->controller->bus_lock_mutex);
- ret = __spi_sync(spi, message);
- mutex_unlock(&spi->controller->bus_lock_mutex);
+ /* Keep old behaviour for clients that tolerate the warnings */
+ if (spi_in_atomic_mode() && ctlr->transfer_atomic) {
+ ret = __spi_sync_atomic(spi, message);
+ } else {
+ mutex_lock(&spi->controller->bus_lock_mutex);
+ ret = __spi_sync(spi, message);
+ mutex_unlock(&spi->controller->bus_lock_mutex);
+ }
return ret;
}
@@ -5070,6 +5111,10 @@ EXPORT_SYMBOL_GPL(spi_sync);
* is non-interruptible, and has no timeout. Low-overhead controller
* drivers may DMA directly into and out of the message buffers.
*
+ * During the time when the system is being shutdown and a transfer_atomic
+ * function is set, this call may also be used from a context that does
+ * not allow sleeping, i.e. with disabled interrupts.
+ *
* This call should be used by drivers that require exclusive access to the
* SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
* be released by a spi_bus_unlock call when the exclusive access is over.
@@ -5078,7 +5123,12 @@ EXPORT_SYMBOL_GPL(spi_sync);
*/
int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
{
- return __spi_sync(spi, message);
+ struct spi_controller *ctlr = spi->controller;
+
+ if (spi_in_atomic_mode() && ctlr->transfer_atomic)
+ return __spi_sync_atomic(spi, message);
+ else
+ return __spi_sync(spi, message);
}
EXPORT_SYMBOL_GPL(spi_sync_locked);
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 0bf31776c54c..f62b21af5d75 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -461,6 +461,8 @@ extern struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi,
* controller for configuring specific CS setup time, hold time and inactive
* delay in terms of clock counts
* @transfer: adds a message to the controller's transfer queue.
+ * @transfer_atomic: transfer a message immediately from a late-stage
+ * synchronous transfer
* @cleanup: frees controller-specific state
* @can_dma: determine whether this controller supports DMA
* @dma_map_dev: device which can be used for DMA mapping
@@ -721,6 +723,15 @@ struct spi_controller {
int (*transfer)(struct spi_device *spi,
struct spi_message *mesg);
+ /*
+ * Bidirectional bulk atomic transfers
+ *
+ * Special function that allows transfers in contexts where
+ * no interrupts nor DMA are available.
+ */
+ int (*transfer_atomic)(struct spi_device *spi,
+ struct spi_message *mesg);
+
/* Called on deregistration to free memory provided by spi_controller */
void (*cleanup)(struct spi_device *spi);
--
2.53.0
next prev parent reply other threads:[~2026-11-13 13:50 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` [PATCH v2 1/6] spi: Allow spi_delay_exec() to be used from atomic contexts Heiko Stuebner
2026-11-13 13:50 ` [PATCH v2 2/6] spi: Export spi_set_cs() function Heiko Stuebner
2026-11-13 13:56 ` Heiko Stübner
2026-11-13 13:50 ` Heiko Stuebner [this message]
2026-11-13 13:50 ` [PATCH v2 4/6] spi: rockchip: Fix runtime resume error handling 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 ` [PATCH v2 6/6] spi: rockchip: Add an atomic transfer function Heiko Stuebner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261113135017.1047959-4-heiko@sntech.de \
--to=heiko@sntech.de \
--cc=broonie@kernel.org \
--cc=heiko.stuebner@cherry.de \
--cc=lee@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-rockchip@lists.infradead.org \
--cc=linux-spi@vger.kernel.org \
--cc=quentin.schulz@cherry.de \
--cc=sebastian.reichel@collabora.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox