diff mbox series

[v2] mmc: sdhci-msm: Correctly set the load for the regulator

Message ID 20241127095029.3918290-1-quic_yuanjiey@quicinc.com
State New
Headers show
Series [v2] mmc: sdhci-msm: Correctly set the load for the regulator | expand

Commit Message

Yuanjie Yang Nov. 27, 2024, 9:50 a.m. UTC
Qualcomm regulator supports two power supply modes: HPM and LPM.
Currently, the sdhci-msm.c driver does not set the load to adjust
the current for eMMC and SD. Therefore, if the regulator set load
in LPM state, it will lead to the inability to properly initialize
eMMC and SD.

Set the correct regulator current for eMMC and SD to ensure that the
device can work normally even when the regulator is in LPM.

Signed-off-by: Yuanjie Yang <quic_yuanjiey@quicinc.com>
---
Changes in v2:
- Add enum msm_reg_type to optimize the code
- Delete redundant emmc type judgment
- Link to v1: https://lore.kernel.org/linux-arm-msm/20241122075048.2006894-1-quic_yuanjiey@quicinc.com/

---
 drivers/mmc/host/sdhci-msm.c | 92 +++++++++++++++++++++++++++++++++++-
 1 file changed, 90 insertions(+), 2 deletions(-)

Comments

Bjorn Andersson Nov. 28, 2024, 5:15 p.m. UTC | #1
On Wed, Nov 27, 2024 at 05:50:29PM +0800, Yuanjie Yang wrote:
> Qualcomm regulator supports two power supply modes: HPM and LPM.
> Currently, the sdhci-msm.c driver does not set the load to adjust
> the current for eMMC and SD. Therefore, if the regulator set load
> in LPM state, it will lead to the inability to properly initialize
> eMMC and SD.
> 
> Set the correct regulator current for eMMC and SD to ensure that the
> device can work normally even when the regulator is in LPM.
> 
> Signed-off-by: Yuanjie Yang <quic_yuanjiey@quicinc.com>
> ---
> Changes in v2:
> - Add enum msm_reg_type to optimize the code

Please re-optimize the code to make it easy to read and understand.

> - Delete redundant emmc type judgment
> - Link to v1: https://lore.kernel.org/linux-arm-msm/20241122075048.2006894-1-quic_yuanjiey@quicinc.com/
> 
> ---
>  drivers/mmc/host/sdhci-msm.c | 92 +++++++++++++++++++++++++++++++++++-
>  1 file changed, 90 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mmc/host/sdhci-msm.c b/drivers/mmc/host/sdhci-msm.c
> index e00208535bd1..fc13ef60ab61 100644
> --- a/drivers/mmc/host/sdhci-msm.c
> +++ b/drivers/mmc/host/sdhci-msm.c
> @@ -134,9 +134,22 @@
>  /* Timeout value to avoid infinite waiting for pwr_irq */
>  #define MSM_PWR_IRQ_TIMEOUT_MS 5000
>  
> +/* Max load for eMMC Vdd supply */
> +#define MMC_VMMC_MAX_LOAD_UA	570000
> +
>  /* Max load for eMMC Vdd-io supply */
>  #define MMC_VQMMC_MAX_LOAD_UA	325000
>  
> +/* Max load for SD Vdd supply */
> +#define SD_VMMC_MAX_LOAD_UA	800000
> +
> +/* Max load for SD Vdd-io supply */
> +#define SD_VQMMC_MAX_LOAD_UA	22000
> +
> +#define MAX_MMC_SD_VMMC_LOAD_UA  max(MMC_VMMC_MAX_LOAD_UA, SD_VMMC_MAX_LOAD_UA)
> +
> +#define MAX_MMC_SD_VQMMC_LOAD_UA max(MMC_VQMMC_MAX_LOAD_UA, SD_VQMMC_MAX_LOAD_UA)
> +
>  #define msm_host_readl(msm_host, host, offset) \
>  	msm_host->var_ops->msm_readl_relaxed(host, offset)
>  
> @@ -147,6 +160,11 @@
>  #define CQHCI_VENDOR_CFG1	0xA00
>  #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN	(0x3 << 13)
>  
> +enum msm_reg_type {
> +	VMMC_REGULATOR,
> +	VQMMC_REGULATOR,
> +};
> +
>  struct sdhci_msm_offset {
>  	u32 core_hc_mode;
>  	u32 core_mci_data_cnt;
> @@ -1403,11 +1421,71 @@ static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
>  	return ret;
>  }
>  
> -static int sdhci_msm_set_vmmc(struct mmc_host *mmc)
> +static int sdhci_msm_get_regulator_load(struct mmc_host *mmc, int max_current,
> +					enum msm_reg_type type)
> +{
> +	int load = 0;
> +
> +	/*
> +	 * When eMMC and SD are powered on for the first time, select a higher
> +	 * current value from the corresponding current for eMMC and SD to
> +	 * ensure that the eMMC and SD cards start up properly and complete
> +	 * initialization. After the initialization process is finished, use
> +	 * the corresponding current to set the eMMC and SD to ensure the
> +	 * normal work of the device.
> +	 */
> +
> +	if (!mmc->card)
> +		return max_current;

max_current is type == VMMC_REGULATOR ? MAX_MMC_SD_VMMC_LOAD_UA :
MAX_MMC_SD_VQMMC_LOAD_UA;

Try to rewrite the patch so that you don't have the decisions spread
across multiple levels in the callstack.

> +
> +	if (mmc_card_mmc(mmc->card))
> +		load = (type == VMMC_REGULATOR) ? MMC_VMMC_MAX_LOAD_UA : MMC_VQMMC_MAX_LOAD_UA;
> +	else if (mmc_card_sd(mmc->card))
> +		load = (type == VMMC_REGULATOR) ? SD_VMMC_MAX_LOAD_UA : SD_VQMMC_MAX_LOAD_UA;
> +
> +	return load;
> +}
> +
> +static int msm_config_regulator_load(struct sdhci_msm_host *msm_host, struct mmc_host *mmc,
> +				     bool hpm, int max_current, enum msm_reg_type type)
> +{
> +	int ret;
> +	int load = 0;
> +
> +	/*
> +	 * After the initialization process is finished, Once the type of card
> +	 * is determined, only set the corresponding current for SD and eMMC.
> +	 */
> +
> +	if (mmc->card && !(mmc_card_mmc(mmc->card) || mmc_card_sd(mmc->card)))
> +		return 0;
> +
> +	if (hpm)
> +		load = sdhci_msm_get_regulator_load(mmc, max_current, type);

Does !hpm happen when regulators are enabled or always together with a
regulator_disable? (The regulator framework skips the load of disabled
regulators when aggregating)

> +
> +	if (type == VMMC_REGULATOR)
> +		ret = regulator_set_load(mmc->supply.vmmc, load);
> +	else
> +		ret = regulator_set_load(mmc->supply.vqmmc, load);
> +	if (ret)
> +		dev_err(mmc_dev(mmc), "%s: set load failed: %d\n",
> +			mmc_hostname(mmc), ret);
> +	return ret;
> +}
> +
> +static int sdhci_msm_set_vmmc(struct sdhci_msm_host *msm_host,
> +			      struct mmc_host *mmc, bool hpm)
>  {
> +	int ret;
> +
>  	if (IS_ERR(mmc->supply.vmmc))
>  		return 0;
>  
> +	ret = msm_config_regulator_load(msm_host, mmc, hpm,
> +					MAX_MMC_SD_VMMC_LOAD_UA, VMMC_REGULATOR);

msm_config_regulator_load() is mostly 2 different functions with
multiple levels of conditional code paths depending on this last
parameter. Please try to refactor this to avoid overloading the
functions like that.

Regards,
Bjorn

> +	if (ret)
> +		return ret;
> +
>  	return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
>  }
>  
> @@ -1435,6 +1513,15 @@ static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
>  				goto out;
>  			}
>  		}
> +
> +		ret = msm_config_regulator_load(msm_host, mmc, level,
> +						MAX_MMC_SD_VQMMC_LOAD_UA, VQMMC_REGULATOR);
> +		if (ret < 0) {
> +			dev_err(mmc_dev(mmc), "%s: vqmmc set regulator load failed: %d\n",
> +				mmc_hostname(mmc), ret);
> +			goto out;
> +		}
> +
>  		ret = regulator_enable(mmc->supply.vqmmc);
>  	} else {
>  		ret = regulator_disable(mmc->supply.vqmmc);
> @@ -1642,7 +1729,8 @@ static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
>  	}
>  
>  	if (pwr_state) {
> -		ret = sdhci_msm_set_vmmc(mmc);
> +		ret = sdhci_msm_set_vmmc(msm_host, mmc,
> +					 pwr_state & REQ_BUS_ON);
>  		if (!ret)
>  			ret = sdhci_msm_set_vqmmc(msm_host, mmc,
>  					pwr_state & REQ_BUS_ON);
> -- 
> 2.34.1
> 
>
Dmitry Baryshkov Nov. 29, 2024, 6:44 p.m. UTC | #2
On Wed, Nov 27, 2024 at 05:50:29PM +0800, Yuanjie Yang wrote:
> Qualcomm regulator supports two power supply modes: HPM and LPM.
> Currently, the sdhci-msm.c driver does not set the load to adjust
> the current for eMMC and SD. Therefore, if the regulator set load
> in LPM state, it will lead to the inability to properly initialize
> eMMC and SD.
> 
> Set the correct regulator current for eMMC and SD to ensure that the
> device can work normally even when the regulator is in LPM.
> 
> Signed-off-by: Yuanjie Yang <quic_yuanjiey@quicinc.com>
> ---
> Changes in v2:
> - Add enum msm_reg_type to optimize the code
> - Delete redundant emmc type judgment
> - Link to v1: https://lore.kernel.org/linux-arm-msm/20241122075048.2006894-1-quic_yuanjiey@quicinc.com/
> 
> ---
>  drivers/mmc/host/sdhci-msm.c | 92 +++++++++++++++++++++++++++++++++++-
>  1 file changed, 90 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mmc/host/sdhci-msm.c b/drivers/mmc/host/sdhci-msm.c
> index e00208535bd1..fc13ef60ab61 100644
> --- a/drivers/mmc/host/sdhci-msm.c
> +++ b/drivers/mmc/host/sdhci-msm.c
> @@ -134,9 +134,22 @@
>  /* Timeout value to avoid infinite waiting for pwr_irq */
>  #define MSM_PWR_IRQ_TIMEOUT_MS 5000
>  
> +/* Max load for eMMC Vdd supply */
> +#define MMC_VMMC_MAX_LOAD_UA	570000
> +
>  /* Max load for eMMC Vdd-io supply */
>  #define MMC_VQMMC_MAX_LOAD_UA	325000
>  
> +/* Max load for SD Vdd supply */
> +#define SD_VMMC_MAX_LOAD_UA	800000
> +
> +/* Max load for SD Vdd-io supply */
> +#define SD_VQMMC_MAX_LOAD_UA	22000
> +
> +#define MAX_MMC_SD_VMMC_LOAD_UA  max(MMC_VMMC_MAX_LOAD_UA, SD_VMMC_MAX_LOAD_UA)
> +
> +#define MAX_MMC_SD_VQMMC_LOAD_UA max(MMC_VQMMC_MAX_LOAD_UA, SD_VQMMC_MAX_LOAD_UA)
> +
>  #define msm_host_readl(msm_host, host, offset) \
>  	msm_host->var_ops->msm_readl_relaxed(host, offset)
>  
> @@ -147,6 +160,11 @@
>  #define CQHCI_VENDOR_CFG1	0xA00
>  #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN	(0x3 << 13)
>  
> +enum msm_reg_type {
> +	VMMC_REGULATOR,
> +	VQMMC_REGULATOR,

Please drop enum completely, then...

> +};
> +
>  struct sdhci_msm_offset {
>  	u32 core_hc_mode;
>  	u32 core_mci_data_cnt;
> @@ -1403,11 +1421,71 @@ static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
>  	return ret;
>  }
>  
> -static int sdhci_msm_set_vmmc(struct mmc_host *mmc)
> +static int sdhci_msm_get_regulator_load(struct mmc_host *mmc, int max_current,
> +					enum msm_reg_type type)
> +{
> +	int load = 0;
> +
> +	/*
> +	 * When eMMC and SD are powered on for the first time, select a higher
> +	 * current value from the corresponding current for eMMC and SD to
> +	 * ensure that the eMMC and SD cards start up properly and complete
> +	 * initialization. After the initialization process is finished, use
> +	 * the corresponding current to set the eMMC and SD to ensure the
> +	 * normal work of the device.
> +	 */
> +
> +	if (!mmc->card)
> +		return max_current;
> +
> +	if (mmc_card_mmc(mmc->card))
> +		load = (type == VMMC_REGULATOR) ? MMC_VMMC_MAX_LOAD_UA : MMC_VQMMC_MAX_LOAD_UA;
> +	else if (mmc_card_sd(mmc->card))
> +		load = (type == VMMC_REGULATOR) ? SD_VMMC_MAX_LOAD_UA : SD_VQMMC_MAX_LOAD_UA;

... split this into two functions, one for vmmc and another one for
vqmmc...

> +
> +	return load;
> +}
> +
> +static int msm_config_regulator_load(struct sdhci_msm_host *msm_host, struct mmc_host *mmc,
> +				     bool hpm, int max_current, enum msm_reg_type type)

Then this becomes two functions too, each of those can be inlined in the
proper place.

> +{
> +	int ret;
> +	int load = 0;
> +
> +	/*
> +	 * After the initialization process is finished, Once the type of card
> +	 * is determined, only set the corresponding current for SD and eMMC.
> +	 */
> +
> +	if (mmc->card && !(mmc_card_mmc(mmc->card) || mmc_card_sd(mmc->card)))
> +		return 0;

This goes into sdhci_msm_get_regulator_load().

> +
> +	if (hpm)
> +		load = sdhci_msm_get_regulator_load(mmc, max_current, type);
> +
> +	if (type == VMMC_REGULATOR)
> +		ret = regulator_set_load(mmc->supply.vmmc, load);
> +	else
> +		ret = regulator_set_load(mmc->supply.vqmmc, load);
> +	if (ret)
> +		dev_err(mmc_dev(mmc), "%s: set load failed: %d\n",
> +			mmc_hostname(mmc), ret);
> +	return ret;
> +}
> +
> +static int sdhci_msm_set_vmmc(struct sdhci_msm_host *msm_host,
> +			      struct mmc_host *mmc, bool hpm)
>  {
> +	int ret;
> +
>  	if (IS_ERR(mmc->supply.vmmc))
>  		return 0;
>  
> +	ret = msm_config_regulator_load(msm_host, mmc, hpm,
> +					MAX_MMC_SD_VMMC_LOAD_UA, VMMC_REGULATOR);
> +	if (ret)
> +		return ret;
> +
>  	return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
>  }
>  
> @@ -1435,6 +1513,15 @@ static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
>  				goto out;
>  			}
>  		}
> +
> +		ret = msm_config_regulator_load(msm_host, mmc, level,
> +						MAX_MMC_SD_VQMMC_LOAD_UA, VQMMC_REGULATOR);
> +		if (ret < 0) {
> +			dev_err(mmc_dev(mmc), "%s: vqmmc set regulator load failed: %d\n",
> +				mmc_hostname(mmc), ret);
> +			goto out;
> +		}
> +
>  		ret = regulator_enable(mmc->supply.vqmmc);
>  	} else {
>  		ret = regulator_disable(mmc->supply.vqmmc);
> @@ -1642,7 +1729,8 @@ static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
>  	}
>  
>  	if (pwr_state) {
> -		ret = sdhci_msm_set_vmmc(mmc);
> +		ret = sdhci_msm_set_vmmc(msm_host, mmc,
> +					 pwr_state & REQ_BUS_ON);
>  		if (!ret)
>  			ret = sdhci_msm_set_vqmmc(msm_host, mmc,
>  					pwr_state & REQ_BUS_ON);
> -- 
> 2.34.1
>
Yuanjie Yang Dec. 6, 2024, 2:53 a.m. UTC | #3
On Fri, Nov 29, 2024 at 08:44:37PM +0200, Dmitry Baryshkov wrote:
> On Wed, Nov 27, 2024 at 05:50:29PM +0800, Yuanjie Yang wrote:
> > Qualcomm regulator supports two power supply modes: HPM and LPM.
> > Currently, the sdhci-msm.c driver does not set the load to adjust
> > the current for eMMC and SD. Therefore, if the regulator set load
> > in LPM state, it will lead to the inability to properly initialize
> > eMMC and SD.
> > 
> > Set the correct regulator current for eMMC and SD to ensure that the
> > device can work normally even when the regulator is in LPM.
> > 
> > Signed-off-by: Yuanjie Yang <quic_yuanjiey@quicinc.com>
> > ---
> > Changes in v2:
> > - Add enum msm_reg_type to optimize the code
> > - Delete redundant emmc type judgment
> > - Link to v1: https://lore.kernel.org/linux-arm-msm/20241122075048.2006894-1-quic_yuanjiey@quicinc.com/
> > 
> > ---
> >  drivers/mmc/host/sdhci-msm.c | 92 +++++++++++++++++++++++++++++++++++-
> >  1 file changed, 90 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/mmc/host/sdhci-msm.c b/drivers/mmc/host/sdhci-msm.c
> > index e00208535bd1..fc13ef60ab61 100644
> > --- a/drivers/mmc/host/sdhci-msm.c
> > +++ b/drivers/mmc/host/sdhci-msm.c
> > @@ -134,9 +134,22 @@
> >  /* Timeout value to avoid infinite waiting for pwr_irq */
> >  #define MSM_PWR_IRQ_TIMEOUT_MS 5000
> >  
> > +/* Max load for eMMC Vdd supply */
> > +#define MMC_VMMC_MAX_LOAD_UA	570000
> > +
> >  /* Max load for eMMC Vdd-io supply */
> >  #define MMC_VQMMC_MAX_LOAD_UA	325000
> >  
> > +/* Max load for SD Vdd supply */
> > +#define SD_VMMC_MAX_LOAD_UA	800000
> > +
> > +/* Max load for SD Vdd-io supply */
> > +#define SD_VQMMC_MAX_LOAD_UA	22000
> > +
> > +#define MAX_MMC_SD_VMMC_LOAD_UA  max(MMC_VMMC_MAX_LOAD_UA, SD_VMMC_MAX_LOAD_UA)
> > +
> > +#define MAX_MMC_SD_VQMMC_LOAD_UA max(MMC_VQMMC_MAX_LOAD_UA, SD_VQMMC_MAX_LOAD_UA)
> > +
> >  #define msm_host_readl(msm_host, host, offset) \
> >  	msm_host->var_ops->msm_readl_relaxed(host, offset)
> >  
> > @@ -147,6 +160,11 @@
> >  #define CQHCI_VENDOR_CFG1	0xA00
> >  #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN	(0x3 << 13)
> >  
> > +enum msm_reg_type {
> > +	VMMC_REGULATOR,
> > +	VQMMC_REGULATOR,
> 
> Please drop enum completely, then...
Thanks, I will drop enum in next version.

> > +};
> > +
> >  struct sdhci_msm_offset {
> >  	u32 core_hc_mode;
> >  	u32 core_mci_data_cnt;
> > @@ -1403,11 +1421,71 @@ static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
> >  	return ret;
> >  }
> >  
> > -static int sdhci_msm_set_vmmc(struct mmc_host *mmc)
> > +static int sdhci_msm_get_regulator_load(struct mmc_host *mmc, int max_current,
> > +					enum msm_reg_type type)
> > +{
> > +	int load = 0;
> > +
> > +	/*
> > +	 * When eMMC and SD are powered on for the first time, select a higher
> > +	 * current value from the corresponding current for eMMC and SD to
> > +	 * ensure that the eMMC and SD cards start up properly and complete
> > +	 * initialization. After the initialization process is finished, use
> > +	 * the corresponding current to set the eMMC and SD to ensure the
> > +	 * normal work of the device.
> > +	 */
> > +
> > +	if (!mmc->card)
> > +		return max_current;
> > +
> > +	if (mmc_card_mmc(mmc->card))
> > +		load = (type == VMMC_REGULATOR) ? MMC_VMMC_MAX_LOAD_UA : MMC_VQMMC_MAX_LOAD_UA;
> > +	else if (mmc_card_sd(mmc->card))
> > +		load = (type == VMMC_REGULATOR) ? SD_VMMC_MAX_LOAD_UA : SD_VQMMC_MAX_LOAD_UA;
> 
> ... split this into two functions, one for vmmc and another one for
> vqmmc...
Thanks, I will split this funcution into two function in next version.

> > +
> > +	return load;
> > +}
> > +
> > +static int msm_config_regulator_load(struct sdhci_msm_host *msm_host, struct mmc_host *mmc,
> > +				     bool hpm, int max_current, enum msm_reg_type type)
> 
> Then this becomes two functions too, each of those can be inlined in the
> proper place.
Thanks, I will fix it in next version.

> 
> > +{
> > +	int ret;
> > +	int load = 0;
> > +
> > +	/*
> > +	 * After the initialization process is finished, Once the type of card
> > +	 * is determined, only set the corresponding current for SD and eMMC.
> > +	 */
> > +
> > +	if (mmc->card && !(mmc_card_mmc(mmc->card) || mmc_card_sd(mmc->card)))
> > +		return 0;
> 
> This goes into sdhci_msm_get_regulator_load().
Thanks, I will optimize this code in next patch.

> > +
> > +	if (hpm)
> > +		load = sdhci_msm_get_regulator_load(mmc, max_current, type);
> > +
> > +	if (type == VMMC_REGULATOR)
> > +		ret = regulator_set_load(mmc->supply.vmmc, load);
> > +	else
> > +		ret = regulator_set_load(mmc->supply.vqmmc, load);
> > +	if (ret)
> > +		dev_err(mmc_dev(mmc), "%s: set load failed: %d\n",
> > +			mmc_hostname(mmc), ret);
> > +	return ret;
> > +}
> > +
> > +static int sdhci_msm_set_vmmc(struct sdhci_msm_host *msm_host,
> > +			      struct mmc_host *mmc, bool hpm)
> >  {
> > +	int ret;
> > +
> >  	if (IS_ERR(mmc->supply.vmmc))
> >  		return 0;
> >  
> > +	ret = msm_config_regulator_load(msm_host, mmc, hpm,
> > +					MAX_MMC_SD_VMMC_LOAD_UA, VMMC_REGULATOR);
> > +	if (ret)
> > +		return ret;
> > +
> >  	return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
> >  }
> >  
> > @@ -1435,6 +1513,15 @@ static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
> >  				goto out;
> >  			}
> >  		}
> > +
> > +		ret = msm_config_regulator_load(msm_host, mmc, level,
> > +						MAX_MMC_SD_VQMMC_LOAD_UA, VQMMC_REGULATOR);
> > +		if (ret < 0) {
> > +			dev_err(mmc_dev(mmc), "%s: vqmmc set regulator load failed: %d\n",
> > +				mmc_hostname(mmc), ret);
> > +			goto out;
> > +		}
> > +
> >  		ret = regulator_enable(mmc->supply.vqmmc);
> >  	} else {
> >  		ret = regulator_disable(mmc->supply.vqmmc);
> > @@ -1642,7 +1729,8 @@ static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
> >  	}
> >  
> >  	if (pwr_state) {
> > -		ret = sdhci_msm_set_vmmc(mmc);
> > +		ret = sdhci_msm_set_vmmc(msm_host, mmc,
> > +					 pwr_state & REQ_BUS_ON);
> >  		if (!ret)
> >  			ret = sdhci_msm_set_vqmmc(msm_host, mmc,
> >  					pwr_state & REQ_BUS_ON);
> > -- 
> > 2.34.1
> > 
> 
> -- 
> With best wishes
> Dmitry

Thanks,
Yuanjie
Yuanjie Yang Dec. 6, 2024, 3:25 a.m. UTC | #4
On Thu, Nov 28, 2024 at 11:15:54AM -0600, Bjorn Andersson wrote:
> On Wed, Nov 27, 2024 at 05:50:29PM +0800, Yuanjie Yang wrote:
> > Qualcomm regulator supports two power supply modes: HPM and LPM.
> > Currently, the sdhci-msm.c driver does not set the load to adjust
> > the current for eMMC and SD. Therefore, if the regulator set load
> > in LPM state, it will lead to the inability to properly initialize
> > eMMC and SD.
> > 
> > Set the correct regulator current for eMMC and SD to ensure that the
> > device can work normally even when the regulator is in LPM.
> > 
> > Signed-off-by: Yuanjie Yang <quic_yuanjiey@quicinc.com>
> > ---
> > Changes in v2:
> > - Add enum msm_reg_type to optimize the code
> 
> Please re-optimize the code to make it easy to read and understand.
Thanks, I will re-optimize my code.

> > - Delete redundant emmc type judgment
> > - Link to v1: https://lore.kernel.org/linux-arm-msm/20241122075048.2006894-1-quic_yuanjiey@quicinc.com/
> > 
> > ---
> >  drivers/mmc/host/sdhci-msm.c | 92 +++++++++++++++++++++++++++++++++++-
> >  1 file changed, 90 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/mmc/host/sdhci-msm.c b/drivers/mmc/host/sdhci-msm.c
> > index e00208535bd1..fc13ef60ab61 100644
> > --- a/drivers/mmc/host/sdhci-msm.c
> > +++ b/drivers/mmc/host/sdhci-msm.c
> > @@ -134,9 +134,22 @@
> >  /* Timeout value to avoid infinite waiting for pwr_irq */
> >  #define MSM_PWR_IRQ_TIMEOUT_MS 5000
> >  
> > +/* Max load for eMMC Vdd supply */
> > +#define MMC_VMMC_MAX_LOAD_UA	570000
> > +
> >  /* Max load for eMMC Vdd-io supply */
> >  #define MMC_VQMMC_MAX_LOAD_UA	325000
> >  
> > +/* Max load for SD Vdd supply */
> > +#define SD_VMMC_MAX_LOAD_UA	800000
> > +
> > +/* Max load for SD Vdd-io supply */
> > +#define SD_VQMMC_MAX_LOAD_UA	22000
> > +
> > +#define MAX_MMC_SD_VMMC_LOAD_UA  max(MMC_VMMC_MAX_LOAD_UA, SD_VMMC_MAX_LOAD_UA)
> > +
> > +#define MAX_MMC_SD_VQMMC_LOAD_UA max(MMC_VQMMC_MAX_LOAD_UA, SD_VQMMC_MAX_LOAD_UA)
> > +
> >  #define msm_host_readl(msm_host, host, offset) \
> >  	msm_host->var_ops->msm_readl_relaxed(host, offset)
> >  
> > @@ -147,6 +160,11 @@
> >  #define CQHCI_VENDOR_CFG1	0xA00
> >  #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN	(0x3 << 13)
> >  
> > +enum msm_reg_type {
> > +	VMMC_REGULATOR,
> > +	VQMMC_REGULATOR,
> > +};
> > +
> >  struct sdhci_msm_offset {
> >  	u32 core_hc_mode;
> >  	u32 core_mci_data_cnt;
> > @@ -1403,11 +1421,71 @@ static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
> >  	return ret;
> >  }
> >  
> > -static int sdhci_msm_set_vmmc(struct mmc_host *mmc)
> > +static int sdhci_msm_get_regulator_load(struct mmc_host *mmc, int max_current,
> > +					enum msm_reg_type type)
> > +{
> > +	int load = 0;
> > +
> > +	/*
> > +	 * When eMMC and SD are powered on for the first time, select a higher
> > +	 * current value from the corresponding current for eMMC and SD to
> > +	 * ensure that the eMMC and SD cards start up properly and complete
> > +	 * initialization. After the initialization process is finished, use
> > +	 * the corresponding current to set the eMMC and SD to ensure the
> > +	 * normal work of the device.
> > +	 */
> > +
> > +	if (!mmc->card)
> > +		return max_current;
> 
> max_current is type == VMMC_REGULATOR ? MAX_MMC_SD_VMMC_LOAD_UA :
> MAX_MMC_SD_VQMMC_LOAD_UA;
> 
> Try to rewrite the patch so that you don't have the decisions spread
> across multiple levels in the callstack.
Thanks, I will optimzie this funcition in next version.

> > +
> > +	if (mmc_card_mmc(mmc->card))
> > +		load = (type == VMMC_REGULATOR) ? MMC_VMMC_MAX_LOAD_UA : MMC_VQMMC_MAX_LOAD_UA;
> > +	else if (mmc_card_sd(mmc->card))
> > +		load = (type == VMMC_REGULATOR) ? SD_VMMC_MAX_LOAD_UA : SD_VQMMC_MAX_LOAD_UA;
> > +
> > +	return load;
> > +}
> > +
> > +static int msm_config_regulator_load(struct sdhci_msm_host *msm_host, struct mmc_host *mmc,
> > +				     bool hpm, int max_current, enum msm_reg_type type)
> > +{
> > +	int ret;
> > +	int load = 0;
> > +
> > +	/*
> > +	 * After the initialization process is finished, Once the type of card
> > +	 * is determined, only set the corresponding current for SD and eMMC.
> > +	 */
> > +
> > +	if (mmc->card && !(mmc_card_mmc(mmc->card) || mmc_card_sd(mmc->card)))
> > +		return 0;
> > +
> > +	if (hpm)
> > +		load = sdhci_msm_get_regulator_load(mmc, max_current, type);
> 
> Does !hpm happen when regulators are enabled or always together with a
> regulator_disable? (The regulator framework skips the load of disabled
> regulators when aggregating)
> 
Thanks.
When two or more consumer use the same regulator as eMMC/SD.
When !hpm happen, the regulator state can be enabled or disabled.

When the regulotor only used by eMMC/SD.
When !hpm happen, the regulator state is enabled, set load 0 means to
set let regulator enter LPM state.

Recently our team are discussing this issue, when two or more consumer
use the same regulator, one consumer set load can affect other consumer.

We are trying to do some fix on DTS, just to delete "regulator-allow-set-load"
and set regulator-allowed-modes = <RPMH_REGULATOR_MODE_HPM>.
We are doing some experiment.

> > +
> > +	if (type == VMMC_REGULATOR)
> > +		ret = regulator_set_load(mmc->supply.vmmc, load);
> > +	else
> > +		ret = regulator_set_load(mmc->supply.vqmmc, load);
> > +	if (ret)
> > +		dev_err(mmc_dev(mmc), "%s: set load failed: %d\n",
> > +			mmc_hostname(mmc), ret);
> > +	return ret;
> > +}
> > +
> > +static int sdhci_msm_set_vmmc(struct sdhci_msm_host *msm_host,
> > +			      struct mmc_host *mmc, bool hpm)
> >  {
> > +	int ret;
> > +
> >  	if (IS_ERR(mmc->supply.vmmc))
> >  		return 0;
> >  
> > +	ret = msm_config_regulator_load(msm_host, mmc, hpm,
> > +					MAX_MMC_SD_VMMC_LOAD_UA, VMMC_REGULATOR);
> 
> msm_config_regulator_load() is mostly 2 different functions with
> multiple levels of conditional code paths depending on this last
> parameter. Please try to refactor this to avoid overloading the
> functions like that.
Thanks, I will optimize my code in next version.

> 
> Regards,
> Bjorn
> 
> > +	if (ret)
> > +		return ret;
> > +
> >  	return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
> >  }
> >  
> > @@ -1435,6 +1513,15 @@ static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
> >  				goto out;
> >  			}
> >  		}
> > +
> > +		ret = msm_config_regulator_load(msm_host, mmc, level,
> > +						MAX_MMC_SD_VQMMC_LOAD_UA, VQMMC_REGULATOR);
> > +		if (ret < 0) {
> > +			dev_err(mmc_dev(mmc), "%s: vqmmc set regulator load failed: %d\n",
> > +				mmc_hostname(mmc), ret);
> > +			goto out;
> > +		}
> > +
> >  		ret = regulator_enable(mmc->supply.vqmmc);
> >  	} else {
> >  		ret = regulator_disable(mmc->supply.vqmmc);
> > @@ -1642,7 +1729,8 @@ static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
> >  	}
> >  
> >  	if (pwr_state) {
> > -		ret = sdhci_msm_set_vmmc(mmc);
> > +		ret = sdhci_msm_set_vmmc(msm_host, mmc,
> > +					 pwr_state & REQ_BUS_ON);
> >  		if (!ret)
> >  			ret = sdhci_msm_set_vqmmc(msm_host, mmc,
> >  					pwr_state & REQ_BUS_ON);
> > -- 
> > 2.34.1
> > 
> >

Thanks,
Yuanjie
diff mbox series

Patch

diff --git a/drivers/mmc/host/sdhci-msm.c b/drivers/mmc/host/sdhci-msm.c
index e00208535bd1..fc13ef60ab61 100644
--- a/drivers/mmc/host/sdhci-msm.c
+++ b/drivers/mmc/host/sdhci-msm.c
@@ -134,9 +134,22 @@ 
 /* Timeout value to avoid infinite waiting for pwr_irq */
 #define MSM_PWR_IRQ_TIMEOUT_MS 5000
 
+/* Max load for eMMC Vdd supply */
+#define MMC_VMMC_MAX_LOAD_UA	570000
+
 /* Max load for eMMC Vdd-io supply */
 #define MMC_VQMMC_MAX_LOAD_UA	325000
 
+/* Max load for SD Vdd supply */
+#define SD_VMMC_MAX_LOAD_UA	800000
+
+/* Max load for SD Vdd-io supply */
+#define SD_VQMMC_MAX_LOAD_UA	22000
+
+#define MAX_MMC_SD_VMMC_LOAD_UA  max(MMC_VMMC_MAX_LOAD_UA, SD_VMMC_MAX_LOAD_UA)
+
+#define MAX_MMC_SD_VQMMC_LOAD_UA max(MMC_VQMMC_MAX_LOAD_UA, SD_VQMMC_MAX_LOAD_UA)
+
 #define msm_host_readl(msm_host, host, offset) \
 	msm_host->var_ops->msm_readl_relaxed(host, offset)
 
@@ -147,6 +160,11 @@ 
 #define CQHCI_VENDOR_CFG1	0xA00
 #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN	(0x3 << 13)
 
+enum msm_reg_type {
+	VMMC_REGULATOR,
+	VQMMC_REGULATOR,
+};
+
 struct sdhci_msm_offset {
 	u32 core_hc_mode;
 	u32 core_mci_data_cnt;
@@ -1403,11 +1421,71 @@  static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
 	return ret;
 }
 
-static int sdhci_msm_set_vmmc(struct mmc_host *mmc)
+static int sdhci_msm_get_regulator_load(struct mmc_host *mmc, int max_current,
+					enum msm_reg_type type)
+{
+	int load = 0;
+
+	/*
+	 * When eMMC and SD are powered on for the first time, select a higher
+	 * current value from the corresponding current for eMMC and SD to
+	 * ensure that the eMMC and SD cards start up properly and complete
+	 * initialization. After the initialization process is finished, use
+	 * the corresponding current to set the eMMC and SD to ensure the
+	 * normal work of the device.
+	 */
+
+	if (!mmc->card)
+		return max_current;
+
+	if (mmc_card_mmc(mmc->card))
+		load = (type == VMMC_REGULATOR) ? MMC_VMMC_MAX_LOAD_UA : MMC_VQMMC_MAX_LOAD_UA;
+	else if (mmc_card_sd(mmc->card))
+		load = (type == VMMC_REGULATOR) ? SD_VMMC_MAX_LOAD_UA : SD_VQMMC_MAX_LOAD_UA;
+
+	return load;
+}
+
+static int msm_config_regulator_load(struct sdhci_msm_host *msm_host, struct mmc_host *mmc,
+				     bool hpm, int max_current, enum msm_reg_type type)
+{
+	int ret;
+	int load = 0;
+
+	/*
+	 * After the initialization process is finished, Once the type of card
+	 * is determined, only set the corresponding current for SD and eMMC.
+	 */
+
+	if (mmc->card && !(mmc_card_mmc(mmc->card) || mmc_card_sd(mmc->card)))
+		return 0;
+
+	if (hpm)
+		load = sdhci_msm_get_regulator_load(mmc, max_current, type);
+
+	if (type == VMMC_REGULATOR)
+		ret = regulator_set_load(mmc->supply.vmmc, load);
+	else
+		ret = regulator_set_load(mmc->supply.vqmmc, load);
+	if (ret)
+		dev_err(mmc_dev(mmc), "%s: set load failed: %d\n",
+			mmc_hostname(mmc), ret);
+	return ret;
+}
+
+static int sdhci_msm_set_vmmc(struct sdhci_msm_host *msm_host,
+			      struct mmc_host *mmc, bool hpm)
 {
+	int ret;
+
 	if (IS_ERR(mmc->supply.vmmc))
 		return 0;
 
+	ret = msm_config_regulator_load(msm_host, mmc, hpm,
+					MAX_MMC_SD_VMMC_LOAD_UA, VMMC_REGULATOR);
+	if (ret)
+		return ret;
+
 	return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
 }
 
@@ -1435,6 +1513,15 @@  static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
 				goto out;
 			}
 		}
+
+		ret = msm_config_regulator_load(msm_host, mmc, level,
+						MAX_MMC_SD_VQMMC_LOAD_UA, VQMMC_REGULATOR);
+		if (ret < 0) {
+			dev_err(mmc_dev(mmc), "%s: vqmmc set regulator load failed: %d\n",
+				mmc_hostname(mmc), ret);
+			goto out;
+		}
+
 		ret = regulator_enable(mmc->supply.vqmmc);
 	} else {
 		ret = regulator_disable(mmc->supply.vqmmc);
@@ -1642,7 +1729,8 @@  static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
 	}
 
 	if (pwr_state) {
-		ret = sdhci_msm_set_vmmc(mmc);
+		ret = sdhci_msm_set_vmmc(msm_host, mmc,
+					 pwr_state & REQ_BUS_ON);
 		if (!ret)
 			ret = sdhci_msm_set_vqmmc(msm_host, mmc,
 					pwr_state & REQ_BUS_ON);