mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] spi: dw: configure CPOL/CPHA for each message before sending
@ 2026-11-07  7:07 Jisheng Zhang
  0 siblings, 0 replies; only message in thread
From: Jisheng Zhang @ 2026-11-07  7:07 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-spi, linux-kernel, Michael Drobot

From: Michael Drobot <michael.drobot@garmin.com>

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@garmin.com>
Co-authored-by: Jisheng Zhang <jszhang@kernel.org>
Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
---
 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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-07  7:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-07  7:07 [PATCH] spi: dw: configure CPOL/CPHA for each message before sending Jisheng Zhang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®