From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796342195; b=awwKyIp3r4F82pqOdBf5HSiyl2kkASOg22NTd+WydCLH9a29SaEeVt+oC73lMBgFWAyN cXUaCZENb9Yqmytc6DaDiDA1NKwDMG1drarbHFZKmU+Kf34FeN/qAwqTAtSjdG9A1N3ad 17+rC3HOBVX5dIi+5LQkUmNSQK+SaFCVZB5Zz48lUtXvWbc42owgyhNemRBu2swURO0ad il9Cm7tymn7Vx1ukT6fqkRjMwVOXuU0GkMo9ue67n4JUnQKBM9w1jrMc29L4QhCvvxJ8X WVWrbtDeUlmInx1skuzDvfzCbV1jbAo2OMvUttKK+xB1YjbapBBbS5o91YnQskX1W9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796342195; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=FH3G+nk8WYMUHAQDs294WJMjaWFwWSoh2tPD8iBPLrc=; b=qk3kY0nJsDXteTMrBdQEk//gBm7ylAu+xAGoshVDUVIXZBpBf/lDDhaQ9l1yCCQgNsuY IBNllaO9EaErW8htXbpEgb9zILRBX4SVI4ETO8XGnyX8ScEb85RGGrzV1ZerfAU9rLdxK ivE4GWbJyZonAxlF5lzIfalmNvv9E5oTLKekcdvgiT290QG1zSt8mWasb4/LqvUb+wH7x WDK/TWHTBLnAVyW8jovJywD7uOJ+h1pgo47otGsFk3inHS8qKs0OnQJGR4IOkOQ6euE3o Mf0Ciy9fMyISIuEL6h6uJ0fPs5GSLtF6hYoyoZZ6S3PhrPBQr8CIUlnKiu6Z+bQlj3Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rDJ7FNbo; arc=none smtp.remote-ip=209.85.214.173 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 495FA606A32F for ; Thu, 3 Dec 2026 23:56:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 495FA606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=rDJ7FNbo Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2f04ad2c46dso2242035ad.2 for ; Thu, 03 Dec 2026 15:56:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796342194; x=1796946994; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FH3G+nk8WYMUHAQDs294WJMjaWFwWSoh2tPD8iBPLrc=; b=rDJ7FNboWBtCNeDKZYu2JDqagIeu4zG4WBASwkAxJcxJ32BvtTpLpC/OHsKLpsBtvO dACpBDSDDmZzuskf//722dlgMeRJ7CcHw49trWL3eTj0AsZLZiucr0TiFqVqE+rUbj42 Fmn4+/1yX1G1SgO7tlSIJpizjZtmM2UQMcL8MKSluOec8uNATkqFqwDPU1y9CBWSDJ6e GAWDwSkfYaGN8qP+kSwiy+Q6INwoFRFM6gvHHZc0F/Vjl+ym20P+daubxbGgwyg6vMM5 e0MOqCG7boW4zbOzGmfyNH6+2IRiFV/MINeUb8pXKKD6Xt6uwra2/1zQLgPsHpgC9oEy jRRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796342194; x=1796946994; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=FH3G+nk8WYMUHAQDs294WJMjaWFwWSoh2tPD8iBPLrc=; b=YkuRA9dyNfws6MrAkwDMNh8CP7q5E2q+WRlWYLZjYfvQvAgAMpUEIWBkZ7nVy0olc/ tbDf1vqE6LDeTaNnqNOIoit0io0uYazEjU+D8ccsutQ/ko2qijOqy2pjFkN7Hj77vdYB I3iYjFb8taA84Luolw2Bcw5J+ABNovWY7/DajeN9bFy41z9hgmZohFx0CgGNS+k+TPYQ JlRXae3GIYy72IA2JmQlBPM51tSYlqlYEVWQ6DiPQycCg6oHgNz1d30JoKf/d5bXxvAj H2EhauEOtBzBq7b8eCnvcexDhed469y0ayA3AEBSG1ga/PNtcyhBjPN4HW5Cpj20Gf/p dj3g== X-Gm-Message-State: APuU4Z/I7bHJAtUuKPIoGwianQvXai5cr9khygGqXeteqrdBDbMsccxc Wedv8yUxvApgjYoy3Df1i9r8CzpGyyCIrwZDhXt4M+AxMwRmoY8R+pXV X-Gm-Gg: AZFdTZdL+FJ+006ZhlBCBeszF1qZGpxto7oYN6NhdkI8oKbUHNvUVxztgTblCmfFkhO N91NqWdTwp2/SeIc8TZWZdqwLBZHNuaVc6JvgdbgQyqfr8QheziMdjVa0R6UnS5JobSuVfpqD2o Kmy9yU3IgzZQQGrD1r9STVhVG+pDnOeA2VIh/Bo87aNyWO7MdHrrpjt0IkrsapdSa19HBZ4c41Q scPZZTq47hu5Fpjw1oWskX3eFO8JWuTmiIyYSB8uNI73BCB7seaLrm95xdfz9XY5yxLHOI69cRh ZnksSVelYJi74Tpa8qDYY+77mCti4bOEi5oTxzcymXxBUtrq7EHy+g7g+qKI7j72rpzX7YxNs2i j3BR680rDFa35M0twHRQogv9eS8eI/6iti+De8ahI4dLz7tKSPpyO/DNLe21yM0fYRxYbGyCDON uo1NgAkVCdJpH1fCIloF/CuMj771m8Wtd+kQJranfeTv2Uh0hRlxvrUlnc1IuJodGUPV+OENaYc lSlpyVR8nDF3OkthClqr8rvk0W5MuwXGx3nfDwW3hImWivsn8tlO/k1xLd6/yhI0kAoDCyAyUhb zSU6jdoJMgPLRhqD/U+CMeFf6gHVYtBLgsDamjxANIZhPnA+o8xYrLCsHT5oVtcxox8AdxYytK1 WgqWBBWeEmlktEMo/w6oEdBc6ATo+kgSh5xEL02+Jj7sieIAK/ZAss6K+7++1lFP+rmk4COH32b 8grxgkWQkiVEmNT4pKGEoWbNWotTcPGjaNgq+2DrbYI2s7Nj7JI1ZRYQixuv3OA0KPzA== X-Received: by 2002:a17:902:ec92:b0:2f1:8f60:3b88 with SMTP id d9443c01a7336-2f1ba224ce7mr1689435ad.53.1796342194394; Thu, 03 Dec 2026 15:56:34 -0800 (PST) Received: from josephctchan-96c-1t.c.googlers.com.com (95.100.125.34.bc.googleusercontent.com. [34.125.100.95]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f1b7967a2bsm1660345ad.68.2026.12.03.15.56.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 15:56:33 -0800 (PST) From: Joseph CT Chan To: Mark Brown Cc: linux-spi@vger.kernel.org, David Lechner , Bart Van Assche , Joseph CT Chan Subject: [PATCH v3 4/5] spi: spi-mux: pass child CS and word delays to the parent Date: Thu, 3 Dec 2026 23:56:24 +0000 Message-ID: <20261203235626.1437609-5-jctchan@gmail.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog In-Reply-To: <20261203235626.1437609-1-jctchan@gmail.com> References: <20261203235626.1437609-1-jctchan@gmail.com> X-Mailing-List: linux-spi@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Joseph CT Chan spi_mux_select() copies only max_speed_hz, mode and bits_per_word from the child to the spi-mux device on the parent controller. The child's cs_setup, cs_hold, cs_inactive and word_delay are never passed on, so the parent controller uses the spi-mux device's own delays (usually zero) for every child. A child that needs a CS setup time or a delay between words, for example from spi-cs-setup-delay-ns in its device tree node, does not get it when it sits behind a mux. Save the spi-mux device's own delays at probe time, and when switching to a child, merge them with the child's in a new helper, spi_mux_merge_cs_delay(): use the longer CS delay of the two, so that the needs of the mux and of the child are both met; on a tie the spi-mux delay is kept. A delay given in SCK cycles cannot be converted to time without a transfer, so it cannot be compared: a child delay in SCK cycles is used when the spi-mux delay is zero, and otherwise the spi-mux delay is kept and a warning is printed once. If only the spi-mux delay is in SCK cycles, it is kept without a warning; device tree only gives delays in ns or us, so this case does not come from DT. The child's word_delay is used when it is set, else the spi-mux device's own. The delays are written before spi_setup() on the parent, so they reach both set_cs_timing() and the software CS delays in spi_set_cs(). spi_mux_select() overwrites the delays of the spi-mux device, so put its own delays back when the driver unbinds. Otherwise a later probe would save the last child's merged delays as the spi-mux device's own. Unlike a device directly on a controller, whose CS delays are read on every message, the delays are now copied when the mux switches to the child or after the child calls spi_setup(). A child that changes its delays must call spi_setup() for the change to take effect. This depends on patch 2 of this series ("spi: spi-mux: re-apply settings after child calls spi_setup()"), which makes the copy happen again after the child calls spi_setup(). Tested with KUnit (spi_mux: spi_mux_cs_delay_test and spi_mux_settings_lifecycle_test, added later in this series) on ARCH=um and x86_64; they fail without this patch. Builds with W=1 on ARCH=um and x86_64. Not tested on hardware. Developed with help from the Claude and Gemini AI assistants. Fixes: e9e40543ad5b ("spi: Add generic SPI multiplexer") Fixes: 8c33ebfeeb59 ("spi: move cs spi_delay to spi_device") Signed-off-by: Joseph CT Chan --- v3: Rebased on spi/for-7.5. v2: Updated the patch reference in the commit message after the split. drivers/spi/spi-mux.c | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/drivers/spi/spi-mux.c b/drivers/spi/spi-mux.c index 0e770137b5bb..45d8920727e0 100644 --- a/drivers/spi/spi-mux.c +++ b/drivers/spi/spi-mux.c @@ -34,6 +34,10 @@ * @child_msg_dev: Used to store the spi_device pointer to the child * @mux: mux_control structure used to provide chip selects for * downstream spi devices + * @mux_cs_setup: CS setup delay of the spi-mux device itself + * @mux_cs_hold: CS hold delay of the spi-mux device itself + * @mux_cs_inactive: CS inactive delay of the spi-mux device itself + * @mux_word_delay: word delay of the spi-mux device itself */ struct spi_mux_priv { struct spi_device *spi; @@ -43,8 +47,55 @@ struct spi_mux_priv { void *child_msg_context; struct spi_device *child_msg_dev; struct mux_control *mux; + + struct spi_delay mux_cs_setup; + struct spi_delay mux_cs_hold; + struct spi_delay mux_cs_inactive; + struct spi_delay mux_word_delay; }; +/* + * Pick the CS delay for the parent from the spi-mux device's own delay and + * the child's: the longer of the two, so that both are honored; on a tie the + * spi-mux delay is kept. A delay in SCK cycles cannot be converted to time + * without a transfer, so it cannot be compared: + * - a child delay in SCK cycles is used when the spi-mux delay is zero; + * otherwise the spi-mux delay is kept and a warning is printed once; + * - if only the spi-mux delay is in SCK cycles, it is kept. Device tree + * only gives delays in ns or us, so this case does not come from DT. + */ +static void spi_mux_merge_cs_delay(struct spi_mux_priv *priv, + struct spi_delay *dst, + struct spi_delay *mux, + struct spi_delay *child) +{ + int mux_ns, child_ns; + + if (!child->value) { + *dst = *mux; + return; + } + + mux_ns = spi_delay_to_ns(mux, NULL); + child_ns = spi_delay_to_ns(child, NULL); + + if (mux_ns >= 0 && child_ns >= 0) { + *dst = child_ns > mux_ns ? *child : *mux; + return; + } + + if (child_ns < 0) { + if (!mux->value) { + *dst = *child; + return; + } + dev_warn_once(&priv->spi->dev, + "child CS delay in SCK cycles ignored, using the mux delay\n"); + } + + *dst = *mux; +} + /* should not get called when the parent controller is doing a transfer */ static int spi_mux_select(struct spi_device *spi) { @@ -65,6 +116,14 @@ static int spi_mux_select(struct spi_device *spi) priv->spi->max_speed_hz = spi->max_speed_hz; priv->spi->mode = spi->mode; priv->spi->bits_per_word = spi->bits_per_word; + spi_mux_merge_cs_delay(priv, &priv->spi->cs_setup, &priv->mux_cs_setup, + &spi->cs_setup); + spi_mux_merge_cs_delay(priv, &priv->spi->cs_hold, &priv->mux_cs_hold, + &spi->cs_hold); + spi_mux_merge_cs_delay(priv, &priv->spi->cs_inactive, + &priv->mux_cs_inactive, &spi->cs_inactive); + priv->spi->word_delay = spi->word_delay.value ? spi->word_delay : + priv->mux_word_delay; ret = spi_setup(priv->spi); if (ret) { @@ -156,10 +215,26 @@ static int spi_mux_transfer_one_message(struct spi_controller *ctlr, return 0; } +/* + * spi_mux_select() overwrites the delays of the spi-mux device with the + * merged ones. Put its own delays back when the driver unbinds, so that a + * later probe does not take the last child's delays as its own. + */ +static void spi_mux_restore_delays(void *data) +{ + struct spi_mux_priv *priv = data; + + priv->spi->cs_setup = priv->mux_cs_setup; + priv->spi->cs_hold = priv->mux_cs_hold; + priv->spi->cs_inactive = priv->mux_cs_inactive; + priv->spi->word_delay = priv->mux_word_delay; +} + static int spi_mux_probe(struct spi_device *spi) { struct spi_controller *ctlr; struct spi_mux_priv *priv; + int ret; ctlr = devm_spi_alloc_host(&spi->dev, sizeof(*priv)); if (!ctlr) @@ -168,6 +243,14 @@ static int spi_mux_probe(struct spi_device *spi) spi_set_drvdata(spi, ctlr); priv = spi_controller_get_devdata(ctlr); priv->spi = spi; + priv->mux_cs_setup = spi->cs_setup; + priv->mux_cs_hold = spi->cs_hold; + priv->mux_cs_inactive = spi->cs_inactive; + priv->mux_word_delay = spi->word_delay; + + ret = devm_add_action_or_reset(&spi->dev, spi_mux_restore_delays, priv); + if (ret) + return ret; /* * Increase lockdep class as these lock are taken while the parent bus -- 2.56.1.915.g69d5ec7298-goog