Skip to contentSkip to conversation

mmc: rtsx_usb_sdmmc: Improve sd_set_power_mode()

PATCH In review Ulf Hansson 3 participants 10 messages Jun 10, 2025 – Jun 19, 2025 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

cover mmc: rtsx_usb_sdmmc: Improve sd_set_power_mode()

The code in sd_set_power_mode() is a bit obfuscated and also has some minor issue in its error-path. This small series addresses these problems.

Ulf Hansson (4):
mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()
mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors
mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void
mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

 drivers/mmc/host/rtsx_usb_sdmmc.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()

In the error path of sd_set_power_mode() we don't update host->power_mode, which could lead to an imbalance of the runtime PM usage count. Fix this by always updating host->power_mode.

---
 drivers/mmc/host/rtsx_usb_sdmmc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/mmc/host/rtsx_usb_sdmmc.c +1 -3
index d229c2b83ea9..8c35cb85a9c0 100644
@@ -1029,9 +1029,7 @@ static int sd_set_power_mode(struct rtsx_usb_sdmmc *host,
 		err = sd_power_on(host);
 	}
 
-	if (!err)
-		host->power_mode = power_mode;
-
+	host->power_mode = power_mode;
 	return err;
 }
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors

In reply to Ulf Hansson

It should be useful to know when we fail to power-on/off a card. Let's therefore print debug-messages when this happens.

---
 drivers/mmc/host/rtsx_usb_sdmmc.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mmc/host/rtsx_usb_sdmmc.c +4 -0
index 8c35cb85a9c0..c204cdeaee3e 100644
@@ -1023,10 +1023,14 @@ static int sd_set_power_mode(struct rtsx_usb_sdmmc *host,
 
 	if (power_mode == MMC_POWER_OFF) {
 		err = sd_power_off(host);
+		if (err)
+			dev_dbg(sdmmc_dev(host), "power-off (err = %d)\n", err);
 		pm_runtime_put_noidle(sdmmc_dev(host));
 	} else {
 		pm_runtime_get_noresume(sdmmc_dev(host));
 		err = sd_power_on(host);
+		if (err)
+			dev_dbg(sdmmc_dev(host), "power-on (err = %d)\n", err);
 	}
 
 	host->power_mode = power_mode;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void

In reply to Ulf Hansson

The sdmmc_set_ios() is the only caller of sd_set_power_mode() and it ignores the return code. Let's therefore convert sd_set_power_mode() into a void function instead.

---
 drivers/mmc/host/rtsx_usb_sdmmc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/mmc/host/rtsx_usb_sdmmc.c +2 -3
index c204cdeaee3e..af45bac019d2 100644
@@ -1010,7 +1010,7 @@ static int sd_power_off(struct rtsx_usb_sdmmc *host)
 	return sd_pull_ctl_disable_qfn24(ucr);
 }
 
-static int sd_set_power_mode(struct rtsx_usb_sdmmc *host,
+static void sd_set_power_mode(struct rtsx_usb_sdmmc *host,
 		unsigned char power_mode)
 {
 	int err;
@@ -1019,7 +1019,7 @@ static int sd_set_power_mode(struct rtsx_usb_sdmmc *host,
 		power_mode = MMC_POWER_ON;
 
 	if (power_mode == host->power_mode)
-		return 0;
+		return;
 
 	if (power_mode == MMC_POWER_OFF) {
 		err = sd_power_off(host);
@@ -1034,7 +1034,6 @@ static int sd_set_power_mode(struct rtsx_usb_sdmmc *host,
 	}
 
 	host->power_mode = power_mode;
-	return err;
 }
 
 static int sd_set_timing(struct rtsx_usb_sdmmc *host,
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/4 mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

In reply to Ulf Hansson

It's only at MMC_POWER_OFF and at MMC_POWER_UP when some operations must be carried out in sd_set_power_mode(). The code is a bit obfuscated in this regards. Let's convert it into a switch-case-clause to make this clear.

---
 drivers/mmc/host/rtsx_usb_sdmmc.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/mmc/host/rtsx_usb_sdmmc.c +13 -5
index af45bac019d2..c1fdcc334c8f 100644
@@ -1015,22 +1015,30 @@ static void sd_set_power_mode(struct rtsx_usb_sdmmc *host,
 {
 	int err;
 
-	if (power_mode != MMC_POWER_OFF)
-		power_mode = MMC_POWER_ON;
-
 	if (power_mode == host->power_mode)
 		return;
 
-	if (power_mode == MMC_POWER_OFF) {
+	switch (power_mode) {
+	case MMC_POWER_OFF:
 		err = sd_power_off(host);
 		if (err)
 			dev_dbg(sdmmc_dev(host), "power-off (err = %d)\n", err);
 		pm_runtime_put_noidle(sdmmc_dev(host));
-	} else {
+		break;
+
+	case MMC_POWER_UP:
 		pm_runtime_get_noresume(sdmmc_dev(host));
 		err = sd_power_on(host);
 		if (err)
 			dev_dbg(sdmmc_dev(host), "power-on (err = %d)\n", err);
+		break;
+
+	case MMC_POWER_ON:
+	case MMC_POWER_UNDEFINED:
+		break;
+
+	default:
+		break;
 	}
 
 	host->power_mode = power_mode;
Signature · 1 line
2.43.0
Reviewed-by on the v1 series
#5 lore raw
In reply to Ulf Hansson
Quoted 10 lines

The code in sd_set_power_mode() is a bit obfuscated and also has some minor issue in its error-path. This small series addresses these problems.

Ulf Hansson (4):
mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()
mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors
mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void
mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

 drivers/mmc/host/rtsx_usb_sdmmc.c | 31 ++++++++++++++++++++---------

For the whole series:

Quoted 6 lines

--

 1 file changed, 20 insertions(+), 11 deletions(-)

--
2.43.0

#6 lore raw
In reply to Ulf Hansson
Quoted 10 lines · Ulf Hansson

The code in sd_set_power_mode() is a bit obfuscated and also has some minor issue in its error-path. This small series addresses these problems.

Ulf Hansson (4):
mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()
mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors
mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void
mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

Quoted 6 lines
 drivers/mmc/host/rtsx_usb_sdmmc.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

--
2.43.0

Ricky, I would appreciate your feedback on these too. Or at least an ack.

Kind regards
Uffe

#7 lore raw
Quoted 15 lines · Ulf Hansson
Quoted 14 lines

The code in sd_set_power_mode() is a bit obfuscated and also has some minor issue in its error-path. This small series addresses these problems.

Ulf Hansson (4):
mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()
mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors
mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void
mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

 drivers/mmc/host/rtsx_usb_sdmmc.c | 31
++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
Quoted 5 lines
Quoted 3 lines

--
2.43.0

Ricky, I would appreciate your feedback on these too. Or at least an ack.

Hi Ulf,
Sorry, I forgot to reply this patch,
This patch is fine for me, and this also work well Thank you

Quoted 3 lines

Kind regards
Uffe

Acked-by
#8 lore raw
In reply to Ulf Hansson
Quoted 30 lines

-----Original Message-----
From: Ricky WU
Sent: Wednesday, June 18, 2025 10:48 AM
To: 'Ulf Hansson' <[email protected]>; [email protected]
Cc: [email protected]
Subject: RE: [PATCH 0/4] mmc: rtsx_usb_sdmmc: Improve sd_set_power_mode()

Quoted 4 lines · Ulf Hansson
Quoted 3 lines

The code in sd_set_power_mode() is a bit obfuscated and also has some minor issue in its error-path. This small series addresses these

problems.

Quoted 15 lines
Quoted 14 lines

Ulf Hansson (4):
mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()
mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors
mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void
mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

 drivers/mmc/host/rtsx_usb_sdmmc.c | 31
++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

--
2.43.0

Quoted 6 lines
Quoted 1 line

Ricky, I would appreciate your feedback on these too. Or at least an ack.

Hi Ulf,
Sorry, I forgot to reply this patch,
This patch is fine for me, and this also work well Thank you

Quoted 4 lines
Quoted 4 lines

Kind regards
Uffe

#9 lore raw
In reply to Ulf Hansson
Quoted 4 lines · Ulf Hansson

The code in sd_set_power_mode() is a bit obfuscated and also has some minor issue in its error-path. This small series addresses these problems.

Quoted 9 lines

Ulf Hansson (4):
mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode()
mmc: rtsx_usb_sdmmc: Print debug-messages at power-on/off errors
mmc: rtsx_usb_sdmmc: Convert sd_set_power_mode() into void
mmc: rtsx_usb_sdmmc: Re-work the code in sd_set_power_mode()

 drivers/mmc/host/rtsx_usb_sdmmc.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

Applied for next!

Kind regards
Uffe