[PATCH] spi: dw: configure CPOL/CPHA for each message before sending
From: Jisheng Zhang
Date: Sat Nov 07 2026 - 02:27:39 EST
From: Michael Drobot <michael.drobot@xxxxxxxxxx>
Configure CPOL and CPHA in CTRLR0 before starting the message sending
procedure. Without this preconfiguration step CPOL and CPHA will be set
in dw_spi_update_config(), which runs after CS is asserted when using
GPIO-controlled CS. This causes framing errors on any SPI mode other
than 0.
Signed-off-by: Michael Drobot <michael.drobot@xxxxxxxxxx>
Co-authored-by: Jisheng Zhang <jszhang@xxxxxxxxxx>
Signed-off-by: Jisheng Zhang <jszhang@xxxxxxxxxx>
---
drivers/spi/spi-dw-core.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/drivers/spi/spi-dw-core.c b/drivers/spi/spi-dw-core.c
index 90af2cc9e17d..2bd4ad0f13ad 100644
--- a/drivers/spi/spi-dw-core.c
+++ b/drivers/spi/spi-dw-core.c
@@ -507,6 +507,33 @@ static int dw_spi_poll_transfer(struct dw_spi *dws,
return 0;
}
+static int dw_spi_prepare_message(struct spi_controller *ctlr,
+ struct spi_message *msg)
+{
+ struct dw_spi *dws = spi_controller_get_devdata(ctlr);
+ struct spi_device *spi = msg->spi;
+ struct dw_spi_cfg cfg = {
+ .tmode = DW_SPI_CTRLR0_TMOD_TR,
+ .dfs = spi->bits_per_word,
+ .freq = spi->max_speed_hz,
+ .spi_frf = DW_SPI_CTRLR0_SPI_FRF_STD_SPI,
+ };
+
+ /*
+ * For GPIO CS case, set CPOL/CPHA before the SPI core asserts GPIO CS.
+ * transfer_one() will configure any per-transfer word size or speed
+ * overrides.
+ */
+ if (spi_is_csgpiod(spi)) {
+ dw_spi_enable_chip(dws, 0);
+ dw_spi_update_config(dws, spi, &cfg, NULL);
+ dw_spi_mask_intr(dws, 0xff);
+ dw_spi_enable_chip(dws, 1);
+ }
+
+ return 0;
+}
+
static int dw_spi_transfer_one(struct spi_controller *ctlr,
struct spi_device *spi,
struct spi_transfer *transfer)
@@ -1382,6 +1409,7 @@ int dw_spi_add_controller(struct device *dev, struct dw_spi *dws)
if (!target) {
ctlr->use_gpio_descriptors = true;
+ ctlr->prepare_message = dw_spi_prepare_message;
ctlr->mode_bits |= SPI_LOOP;
if (dws->set_cs)
ctlr->set_cs = dws->set_cs;
--
2.53.0