mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jisheng Zhang <jszhang@kernel.org>
To: Mark Brown <broonie@kernel.org>
Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org,
	Michael Drobot <michael.drobot@garmin.com>
Subject: [PATCH] spi: dw: configure CPOL/CPHA for each message before sending
Date: Sat,  7 Nov 2026 15:07:18 +0800	[thread overview]
Message-ID: <20261107070718.17395-1-jszhang@kernel.org> (raw)

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


                 reply	other threads:[~2026-11-07  7:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261107070718.17395-1-jszhang@kernel.org \
    --to=jszhang@kernel.org \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=michael.drobot@garmin.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

all inboxes | Powered by JetHome®