Skip to contentSkip to conversation

drivers: mmc: Replace manual devm_clk_get/clk_prepare_enable with devm_clk_get_optional_enabled

PATCH In review ping.gao 2 participants 2 messages Jan 23 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20260123054113epcas5p26e6418eb49d2caa8e3ad048d14386189@epcas5p2.samsung.com

ping.gao

author
#1 lore raw

PATCH 1/1 drivers: mmc: Replace manual devm_clk_get/clk_prepare_enable with devm_clk_get_optional_enabled

By using , we:
- Automatically handle optional clocks (return NULL if not present).
- Ensure the clock is enabled upon acquisition.
This change reduces code complexity and improves reliability.

before biu_clk in dwmmc driver devm_clk_get fail, but it's ERR_PTR, not null,it will panic when call clk_prepare
log is below:

[  438.400868] [7:   binder:436_2: 4998] Unable to handle kernel paging request at virtual address fffffffffffffffe
[  438.400877] [7:   binder:436_2: 4998] Mem abort info:
[  438.400881] [7:   binder:436_2: 4998]   ESR = 0x0000000096000005
[  438.400887] [7:   binder:436_2: 4998]   EC = 0x25: DABT (current EL), IL = 32 bits
[  438.400894] [7:   binder:436_2: 4998]   SET = 0, FnV = 0
[  438.400899] [7:   binder:436_2: 4998]   EA = 0, S1PTW = 0
[  438.400904] [7:   binder:436_2: 4998]   FSC = 0x05: level 1 translation fault
...
[  438.409424] [7:   binder:436_2: 4998] Call trace:
[  438.409429] [7:   binder:436_2: 4998]  clk_prepare+0x10/0x24
[  438.409439] [7:   binder:436_2: 4998]  dw_mci_runtime_resume+0x50/0x2d8 [dw_mmc_samsung cd210e210975263404c28fc89778f369f8398f0c]
[  438.409471] [7:   binder:436_2: 4998]  dw_mci_exynos_runtime_resume+0x18/0x58 [dw_mmc_exynos_samsung 2735a594c7c9c9e8c65b0b87523fbf70dcaabfff]
[  438.409496] [7:   binder:436_2: 4998]  pm_generic_runtime_resume+0x40/0x58
[  438.409506] [7:   binder:436_2: 4998]  pm_runtime_force_resume+0x9c/0x134
[  438.409517] [7:   binder:436_2: 4998]  platform_pm_resume+0x40/0x8c
[  438.409529] [7:   binder:436_2: 4998]  dpm_run_callback+0x64/0x230
[  438.409540] [7:   binder:436_2: 4998]  __device_resume+0x1d8/0x394
[  438.409551] [7:   binder:436_2: 4998]  dpm_resume+0x110/0x2b8
[  438.409561] [7:   binder:436_2: 4998]  dpm_resume_end+0x1c/0x38
[  438.409570] [7:   binder:436_2: 4998]  suspend_devices_and_enter+0x828/0xab0
[  438.409582] [7:   binder:436_2: 4998]  pm_suspend+0x334/0x618
[  438.409592] [7:   binder:436_2: 4998]  state_store+0x104/0x144
[  438.409601] [7:   binder:436_2: 4998]  kobj_attr_store+0x30/0x48
[  438.409610] [7:   binder:436_2: 4998]  sysfs_kf_write+0x54/0x6c
[  438.409619] [7:   binder:436_2: 4998]  kernfs_fop_write_iter+0x104/0x1a8
[  438.409628] [7:   binder:436_2: 4998]  vfs_write+0x24c/0x2f4
[  438.409640] [7:   binder:436_2: 4998]  ksys_write+0x78/0xe8
[  438.409652] [7:   binder:436_2: 4998]  __arm64_sys_write+0x1c/0x2c
[  438.409664] [7:   binder:436_2: 4998]  invoke_syscall+0x58/0x114
[  438.409676] [7:   binder:436_2: 4998]  el0_svc_common+0xac/0xe0
[  438.409687] [7:   binder:436_2: 4998]  do_el0_svc+0x1c/0x28
[  438.409698] [7:   binder:436_2: 4998]  el0_svc+0x38/0x68
[  438.409705] [7:   binder:436_2: 4998]  el0t_64_sync_handler+0x68/0xbc
[  438.409712] [7:   binder:436_2: 4998]  el0t_64_sync+0x1a8/0x1ac
---
 drivers/mmc/host/dw_mmc.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/mmc/host/dw_mmc.c +4 -14
index 61f6986f15ef..18981d010b62 100644
@@ -3375,20 +3375,10 @@ int dw_mci_probe(struct dw_mci *host)
 					     "platform data not available\n");
 	}
 
-	host->biu_clk = devm_clk_get(host->dev, "biu");
-	if (IS_ERR(host->biu_clk)) {
-		dev_dbg(host->dev, "biu clock not available\n");
-		ret = PTR_ERR(host->biu_clk);
-		if (ret == -EPROBE_DEFER)
-			return ret;
-
-	} else {
-		ret = clk_prepare_enable(host->biu_clk);
-		if (ret) {
-			dev_err(host->dev, "failed to enable biu clock\n");
-			return ret;
-		}
-	}
+	host->biu_clk = devm_clk_get_optional_enabled(host->dev, "biu");
+	ret = PTR_ERR_OR_ZERO(host->biu_clk);
+	if (ret)
+		return dev_err_probe(host->dev, ret, "failed to get biu clock\n");
 
 	host->ciu_clk = devm_clk_get(host->dev, "ciu");
 	if (IS_ERR(host->ciu_clk)) {
Signature · 1 line
2.50.1
maintainer
#2 lore raw
Quoted 5 lines · ping.gao

By using , we:
- Automatically handle optional clocks (return NULL if not present).
- Ensure the clock is enabled upon acquisition.
This change reduces code complexity and improves reliability.

The commit tile should be:

mmc: dw_mmc: Replace....

Quoted 3 lines

before biu_clk in dwmmc driver devm_clk_get fail, but it's ERR_PTR, not null,it will panic when call clk_prepare
log is below:

----8<----

Quoted 14 lines
drivers/mmc/host/dw_mmc.c +0 -5
index 61f6986f15ef..18981d010b62 100644
@@ -3375,20 +3375,10 @@ int dw_mci_probe(struct dw_mci *host)
  					     "platform data not available\n");
  	}
 
-	host->biu_clk = devm_clk_get(host->dev, "biu");
-	if (IS_ERR(host->biu_clk)) {
-		dev_dbg(host->dev, "biu clock not available\n");
-		ret = PTR_ERR(host->biu_clk);
-		if (ret == -EPROBE_DEFER)

drivers/mmc/host/dw_mmc.c int dw_mci_probe(struct dw_mci *host) · line 3378

Quoted 10 lines
drivers/mmc/host/dw_mmc.c +1 -9
-			return ret;
-
-	} else {
-		ret = clk_prepare_enable(host->biu_clk);
-		if (ret) {
-			dev_err(host->dev, "failed to enable biu clock\n");
-			return ret;
-		}
-	}
+	host->biu_clk = devm_clk_get_optional_enabled(host->dev, "biu");

This won't work, as biu_clk will automatically be disabled, unprepared and freed when dwmmc host is unbound from the bus. Given we need to control biu_clk in runtime, you could just use devm_clk_get_optional, it will return NULL which serves as a dummy clk, if biu isn't present. Then clk_prepare_enable/clk_disable_unprepare for biu_clk is safe to bail out internally.

Quoted 6 lines
drivers/mmc/host/dw_mmc.c +3 -0
+	ret = PTR_ERR_OR_ZERO(host->biu_clk);
+	if (ret)
+		return dev_err_probe(host->dev, ret, "failed to get biu clock\n");
 
  	host->ciu_clk = devm_clk_get(host->dev, "ciu");
  	if (IS_ERR(host->ciu_clk)) {