]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mmc: dw_mmc: Improve dw_mci_get_cd()
authorShawn Lin <shawn.lin@rock-chips.com>
Tue, 6 Jan 2026 02:17:04 +0000 (10:17 +0800)
committerUlf Hansson <ulf.hansson@linaro.org>
Mon, 23 Feb 2026 11:06:54 +0000 (12:06 +0100)
The current dw_mci_get_cd() implementation maintains a DW_MMC_CARD_PRESENT
flag primarily for logging purposes, which adds unnecessary complexity.
Additionally, the if-else-elif control flow does not align with the Linux
kernel coding style.

This commit simplifies the function by:
- Removing the redundant card presence flag
- Replacing the conditional chain with a cleaner implementation
- Improving code readability while maintaining functionality

The change reduces code complexity without affecting the actual card
detection behavior.

Signed-off-by: Shawn Lin <shawn.lin@rock-chips.com>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
drivers/mmc/host/dw_mmc.c
drivers/mmc/host/dw_mmc.h

index e563998b846cdfcab43811ea9ff2041a581b8b26..1d085b41920eda2f5c730625aa4e98f7ade69c83 100644 (file)
@@ -881,42 +881,21 @@ static void dw_mci_post_req(struct mmc_host *mmc,
 
 static int dw_mci_get_cd(struct mmc_host *mmc)
 {
-       int present;
        struct dw_mci *host = mmc_priv(mmc);
        int gpio_cd = mmc_gpio_get_cd(mmc);
 
-       /* Use platform get_cd function, else try onboard card detect */
-       if (((mmc->caps & MMC_CAP_NEEDS_POLL)
-                               || !mmc_card_is_removable(mmc))) {
-               present = 1;
+       if (mmc->caps & MMC_CAP_NEEDS_POLL)
+               return 1;
 
-               if (!test_bit(DW_MMC_CARD_PRESENT, &host->flags)) {
-                       if (mmc->caps & MMC_CAP_NEEDS_POLL) {
-                               dev_info(&mmc->class_dev,
-                                       "card is polling.\n");
-                       } else {
-                               dev_info(&mmc->class_dev,
-                                       "card is non-removable.\n");
-                       }
-                       set_bit(DW_MMC_CARD_PRESENT, &host->flags);
-               }
-
-               return present;
-       } else if (gpio_cd >= 0)
-               present = gpio_cd;
-       else
-               present = (mci_readl(host, CDETECT) & BIT(0))
-                       == 0 ? 1 : 0;
+       if (!mmc_card_is_removable(mmc))
+               return 1;
 
-       spin_lock_bh(&host->lock);
-       if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &host->flags))
-               dev_dbg(&mmc->class_dev, "card is present\n");
-       else if (!present &&
-                       !test_and_clear_bit(DW_MMC_CARD_PRESENT, &host->flags))
-               dev_dbg(&mmc->class_dev, "card is not present\n");
-       spin_unlock_bh(&host->lock);
+       /* Try slot gpio detection */
+       if (gpio_cd >= 0)
+               return !!gpio_cd;
 
-       return present;
+       /* Host native card detect */
+       return !(mci_readl(host, CDETECT) & BIT(0));
 }
 
 static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
@@ -2918,6 +2897,11 @@ static int dw_mci_init_host(struct dw_mci *host)
                mmc->max_seg_size = mmc->max_req_size;
        }
 
+       if (mmc->caps & MMC_CAP_NEEDS_POLL)
+               dev_info(&mmc->class_dev, "card is polling.\n");
+       else if (!mmc_card_is_removable(mmc))
+               dev_info(&mmc->class_dev, "card is non-removable.\n");
+
        dw_mci_get_cd(mmc);
 
        ret = mmc_add_host(mmc);
index 5c17bcc85bf5912b516eef19a9acd96880feecb4..6800e7ce1d7f12c788b44364e985f6de18d15e6d 100644 (file)
@@ -240,11 +240,10 @@ struct dw_mci {
 #endif
        struct mmc_host         *mmc;
        unsigned long           flags;
-#define DW_MMC_CARD_PRESENT    0
-#define DW_MMC_CARD_NEED_INIT  1
-#define DW_MMC_CARD_NO_LOW_PWR 2
-#define DW_MMC_CARD_NO_USE_HOLD 3
-#define DW_MMC_CARD_NEEDS_POLL 4
+#define DW_MMC_CARD_NEED_INIT  0
+#define DW_MMC_CARD_NO_LOW_PWR 1
+#define DW_MMC_CARD_NO_USE_HOLD 2
+#define DW_MMC_CARD_NEEDS_POLL 3
        u32                     ctype;
        unsigned int            clock;
        unsigned int            clk_old;