linux-mmc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: cuu <dexterkidd@gmail.com>
To: ulf.hansson@linaro.org
Cc: linux-mmc@vger.kernel.org, cuu <dexterkidd@gmail.com>
Subject: [PATCH] mmc_spi: Fix cd-gpios for SPI SD card insertion/removal detection
Date: Sun, 24 Aug 2025 16:47:36 +0800	[thread overview]
Message-ID: <20250824084736.172222-1-dexterkidd@gmail.com> (raw)

Previously, SD cards using pure SPI protocol with mmc_spi could not
reliably detect card removal via cd-gpios. This patch fixes the issue
by extending the card alive check in _mmc_detect_card_removed() to
include cd-gpios detection.

Additionally, mmc_rescan() now includes a status probing function
specific to SPI SD cards to ensure the card state is correctly updated
on insertion and removal events.

Signed-off-by: cuu <dexterkidd@gmail.com>
---
 drivers/mmc/core/core.c | 15 ++++++++++++---
 drivers/mmc/core/core.h |  1 +
 drivers/mmc/core/mmc.c  |  8 ++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 874c6fe92855..e028ec8fbf46 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -2123,6 +2123,11 @@ int _mmc_detect_card_removed(struct mmc_host *host)
 
 	ret = host->bus_ops->alive(host);
 
+        if(mmc_host_is_spi(host)) {
+        	if(host->ops->get_cd){
+       			ret = !host->ops->get_cd(host);
+                }
+        }
 	/*
 	 * Card detect status and alive check may be out of sync if card is
 	 * removed slowly, when card detect switch changes while card/slot
@@ -2238,9 +2243,13 @@ void mmc_rescan(struct work_struct *work)
 	}
 
 	/* Verify a registered card to be functional, else remove it. */
-	if (host->bus_ops)
-		host->bus_ops->detect(host);
-
+	if (host->bus_ops){
+		if (mmc_host_is_spi(host)){
+			mmc_detect_mmc(host);
+		}else{
+			host->bus_ops->detect(host);
+		}
+	}
 	host->detect_change = 0;
 
 	/* if there still is a card present, stop here */
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index 622085cd766f..5b48c50b5bb5 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -78,6 +78,7 @@ void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
 int _mmc_detect_card_removed(struct mmc_host *host);
 int mmc_detect_card_removed(struct mmc_host *host);
 
+int mmc_detect_mmc(struct mmc_host *host);
 int mmc_attach_mmc(struct mmc_host *host);
 int mmc_attach_sd(struct mmc_host *host);
 int mmc_attach_sdio(struct mmc_host *host);
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 5be9b42d5057..e73de34e072b 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -2316,6 +2316,14 @@ static const struct mmc_bus_ops mmc_ops = {
 	.flush_cache = _mmc_flush_cache,
 };
 
+/*
+ * Export function for mmc detection 
+ */
+int mmc_detect_mmc(struct mmc_host *host){
+	mmc_detect(host);
+	return 0;
+}
+
 /*
  * Starting point for MMC card init.
  */
-- 
2.34.1


                 reply	other threads:[~2025-08-24  8:50 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=20250824084736.172222-1-dexterkidd@gmail.com \
    --to=dexterkidd@gmail.com \
    --cc=linux-mmc@vger.kernel.org \
    --cc=ulf.hansson@linaro.org \
    /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;
as well as URLs for NNTP newsgroup(s).