mmc: dw_mmc: Allow use of in-spec voltage range for vqmmc-supply
v2 cover mmc: dw_mmc: Allow use of in-spec voltage range for vqmmc-supply
The Rockchip RK3399 SoC SDMMC IO domain supports 1.8V and 3.0V mode, so vqmmc-supply for SD-cards is typically limited to max 3.0V to avoid damage to the SoC.
The SD Standards allow for an operating voltage range of 1.70V-1.95V and 2.7V-3.6V, meaning the 3.0V mode follows spec.
The commit 0b75109b6aaf ("mmc: dw_mmc: return error for invalid voltage setting") help enforce strict 1.8V and 3.3V when setting vqmmc-supply making mmc_set_signal_voltage() possible fail for MMC_SIGNAL_VOLTAGE_330 and can lead to an improper switch to MMC_SIGNAL_VOLTAGE_180.
This series adds a regulator_set_value_clamp() helper set voltage value within a requested voltage range instead of an exact value, and changes dw_mmc to use this helper to set a voltage within in-spec range to fix use of SD-cards with 3.3V signal voltage on Rockchip RK3399 boards using MMC_IO_VOLTAGE=y.
Changes in v2:
- Add static inline variant to #else block
- Collect r-b tags
Jonas Karlman (4):
power: regulator: Add helper to set voltage within an acceptable range
test: dm: regulator: Add regulator_set_value_clamp() tests
mmc: dw_mmc: Allow use of in-spec voltage range for vqmmc-supply
mmc: sdhci: Use CONFIG_IS_ENABLED for MMC_IO_VOLTAGE
arch/sandbox/dts/sandbox_pmic.dtsi | 6 ++++ doc/usage/cmd/dm.rst | 3 +- drivers/mmc/dw_mmc.c | 6 ++-- drivers/mmc/sdhci.c | 2 +- drivers/power/regulator/regulator-uclass.c | 27 +++++++++++++++ drivers/power/regulator/sandbox.c | 11 ++++--- include/power/regulator.h | 18 ++++++++++ include/power/sandbox_pmic.h | 15 +++++++-- test/dm/regulator.c | 38 ++++++++++++++++++++++ 9 files changed, 115 insertions(+), 11 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 1/4 power: regulator: Add helper to set voltage within an acceptable range
Add regulator_set_value_clamp() that clamps a requested target voltage to both caller-provided limits and the regulator constraints before setting the regulator voltage value.
Return -EINVAL when the caller limits cannot be satisfied by the regulator constraints or when the requested range is invalid.
This helper will initially be used to set vqmmc-supply voltage within an acceptable range according to SD Standards, i.e. 1.70V-1.95V and 2.7V-3.6V.
---
v2: Add static inline variant to #else block
--- drivers/power/regulator/regulator-uclass.c | 27 ++++++++++++++++++++++ include/power/regulator.h | 18 +++++++++++++++ 2 files changed, 45 insertions(+)
drivers/power/regulator/regulator-uclass.c +27 -0
@@ -111,6 +111,33 @@ int regulator_get_suspend_value(struct udevice *dev)return ops->get_suspend_value (dev); } +int regulator_set_value_clamp (struct udevice *dev, +int min_uV,int target_uV,int max_uV) +{ +const struct dm_regulator_ops *ops =dev_get_driver_ops (dev); +struct dm_regulator_uclass_plat *uc_pdata; +int uV; + +if (!ops || !ops->set_value) +return -ENOSYS ; + + uc_pdata =dev_get_uclass_plat (dev); +if (uc_pdata->min_uV != -ENODATA && max_uV < uc_pdata->min_uV) +return -EINVAL ; +if (uc_pdata->max_uV != -ENODATA && min_uV > uc_pdata->max_uV) +return -EINVAL ; +if (min_uV > max_uV) +return -EINVAL ; + +if (uc_pdata->min_uV != -ENODATA ) + min_uV =max (min_uV, uc_pdata->min_uV); +if (uc_pdata->max_uV != -ENODATA ) + max_uV =min (max_uV, uc_pdata->max_uV); + uV =clamp (target_uV, min_uV, max_uV); + +return regulator_set_value (dev, uV); +} +/* * To be called with at most caution as there is no check * before setting the actual voltage value.
include/power/regulator.h +18 -0
@@ -315,6 +315,18 @@ int regulator_set_suspend_value(struct udevice *dev, int uV);*/ int regulator_get_suspend_value (struct udevice *dev); +/** +* regulator_set_value_clamp: set clamped microvoltage value of a given regulator +* +* @dev - pointer to the regulator device +* @min_uV - the minimum output value to set [micro Volts] +* @target_uV - the target output value to set [micro Volts] +* @max_uV - the maximum output value to set [micro Volts] +* Return: - 0 on success or -errno val if fails +*/ +int regulator_set_value_clamp (struct udevice *dev, +int min_uV,int target_uV,int max_uV); +/** * regulator_set_value_force: set the microvoltage value of a given regulator * without any min-,max condition check
@@ -550,6 +562,12 @@ static inline int regulator_get_suspend_value(struct udevice *dev)return -ENOSYS ; } +static inline int regulator_set_value_clamp (struct udevice *dev, +int min_uV,int target_uV,int max_uV) +{ +return -ENOSYS ; +} +static inline int regulator_set_value_force (struct udevice *dev,int uV) {return -ENOSYS ;
Signature · 1 line
2.54.0
PATCH v2 2/4 test: dm: regulator: Add regulator_set_value_clamp() tests
In reply to Jonas KarlmanAdd a sandbox LDO3 with a configurable 1.8V to 3.3V range and use it to test regulator_set_value_clamp().
Test in-range requests, clamping against the regulator limits, invalid ranges outside the regulator limits and a min value higher than max.
---
v2: Collect r-b tag
--- arch/sandbox/dts/sandbox_pmic.dtsi | 6 +++++ doc/usage/cmd/dm.rst | 3 ++- drivers/power/regulator/sandbox.c | 11 +++++---- include/power/sandbox_pmic.h | 15 ++++++++++-- test/dm/regulator.c | 38 ++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-)
arch/sandbox/dts/sandbox_pmic.dtsi +6 -0
@@ -47,6 +47,12 @@ regulator-min-microvolt = <1500000>; regulator-max-microvolt = <1500000>; }; + + ldo3 { + regulator-name = "SUPPLY_1.8_3.3V"; + regulator-min-microvolt = <1800000>; + regulator-max-microvolt = <3300000>; + }; }; &mc34708 {
doc/usage/cmd/dm.rst +2 -1
@@ -409,7 +409,8 @@ This example shows the abridged sandbox output:: regulator 1 [ ] sandbox_buck | | |-- buck2 regulator 2 [ ] sandbox_ldo | | |-- ldo1 regulator 3 [ ] sandbox_ldo | | |-- ldo2 - regulator 4 [ ] sandbox_buck | | `-- no_match_by_nodename + regulator 4 [ ] sandbox_buck | | |-- no_match_by_nodename + regulator 5 [ ] sandbox_ldo | | `-- ldo3 pmic 1 [ ] mc34708_pmic | `-- pmic@41 bootcount 0 [ + ] bootcount-rtc |-- bootcount@0 bootcount 1 [ ] bootcount-i2c-eeprom |-- bootcount
drivers/power/regulator/sandbox.c +6 -5
@@ -56,10 +56,11 @@ static struct dm_regulator_mode sandbox_buck_modes[] = { MODE(BUCK_OM_PWM, OM2REG(BUCK_OM_PWM), "PWM"), }; -/* LDO: 1,2 - voltage range */ +/* LDO: 1,2,3 - voltage range */ static struct output_range ldo_voltage_range[] = {RANGE (OUT_LDO1_UV_MIN ,OUT_LDO1_UV_MAX ,OUT_LDO1_UV_STEP ),RANGE (OUT_LDO2_UV_MIN ,OUT_LDO2_UV_MAX ,OUT_LDO2_UV_STEP ), +RANGE (OUT_LDO3_UV_MIN ,OUT_LDO3_UV_MAX ,OUT_LDO3_UV_STEP ), };/* LDO: 1 - current range */
@@ -288,8 +289,8 @@ static int ldo_set_voltage(struct udevice *dev, int uV)static int ldo_get_current (struct udevice *dev) { -/* LDO2 - unsupported */ -if (dev->driver_data ==2 ) +/* LDO: 2,3 - unsupported */ +if (dev->driver_data >=2 )return -ENOSYS ; return out_get_value(dev, SANDBOX_LDO_COUNT, OUT_REG_UA,
@@ -298,8 +299,8 @@ static int ldo_get_current(struct udevice *dev)static int ldo_set_current (struct udevice *dev,int uA) { -/* LDO2 - unsupported */ -if (dev->driver_data ==2 ) +/* LDO: 2,3 - unsupported */ +if (dev->driver_data >=2 )return -ENOSYS ; return out_set_value(dev, SANDBOX_LDO_COUNT, OUT_REG_UA,
include/power/sandbox_pmic.h +13 -2
@@ -13,10 +13,10 @@#define SANDBOX_OF_BUCK_PREFIX "buck"#define SANDBOX_BUCK_COUNT 3 -#define SANDBOX_LDO_COUNT 2 +#define SANDBOX_LDO_COUNT 3/* * Sandbox PMIC registers: -* We have only 12 significant registers, but we alloc 16 for padding. +* We have only 15 significant registers, but we alloc 16 for padding. */ enum {SANDBOX_PMIC_REG_BUCK1_UV =0 ,
@@ -36,6 +36,10 @@ enum {SANDBOX_PMIC_REG_LDO2_UA ,SANDBOX_PMIC_REG_LDO2_OM , +SANDBOX_PMIC_REG_LDO3_UV , +SANDBOX_PMIC_REG_LDO3_UA , +SANDBOX_PMIC_REG_LDO3_OM , + SANDBOX_PMIC_REG_COUNT = 16, };
@@ -94,6 +98,11 @@ enum {#define OUT_LDO2_UV_MAX 3950000#define OUT_LDO2_UV_STEP 50000 +/* LDO3 Voltage: min: 0.75V, step: 50mV, max 3.95V */ +#define OUT_LDO3_UV_MIN 750000 +#define OUT_LDO3_UV_MAX 3950000 +#define OUT_LDO3_UV_STEP 50000 +/* register <-> value conversion */ #define REG2VAL (min, step, reg) ((min) + ((step) * (reg)))#define VAL2REG (min, step, val) (((val) - (min)) / (step))
@@ -116,6 +125,8 @@ enum {#define SANDBOX_LDO1_PLATNAME "VDD_EMMC_1.8V"#define SANDBOX_LDO2_DEVNAME "ldo2"#define SANDBOX_LDO2_PLATNAME "VDD_LCD_3.3V" +#define SANDBOX_LDO3_DEVNAME "ldo3" +#define SANDBOX_LDO3_PLATNAME "SUPPLY_1.8_3.3V"/* * Expected regulators setup after call of:
test/dm/regulator.c +38 -0
@@ -28,6 +28,7 @@ enum {BUCK3 ,LDO1 ,LDO2 , +LDO3 , OUTPUT_COUNT, };
@@ -44,6 +45,7 @@ static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = { { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME }, { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME}, { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME}, + { SANDBOX_LDO3_DEVNAME, SANDBOX_LDO3_PLATNAME}, };/* Test regulator get method */
@@ -118,6 +120,42 @@ static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts) }DM_TEST (dm_test_power_regulator_set_get_voltage,UTF_SCAN_FDT ); +/* Test regulator set Voltage clamp method */ +static int dm_test_power_regulator_set_value_clamp (struct unit_test_state *uts) +{ +struct udevice *dev; +const char *platname; + +/* LDO3 have 'min' 1.8V and 'max' 3.3V */ + platname = regulator_names[LDO3 ][PLATNAME ]; +ut_assertok (regulator_get_by_platname (platname, &dev)); + +/* 'target' in 'min'/'max' range - should not clamp voltage */ +ut_assertok (regulator_set_value_clamp (dev,1700000 ,1800000 ,1950000 )); +ut_asserteq (1800000 ,regulator_get_value (dev)); +ut_assertok (regulator_set_value_clamp (dev,2700000 ,3300000 ,3600000 )); +ut_asserteq (3300000 ,regulator_get_value (dev)); + +/* 'target' out of 'min'/'max' range - should clamp voltage */ +ut_assertok (regulator_set_value_clamp (dev,1700000 ,1700000 ,1950000 )); +ut_asserteq (1800000 ,regulator_get_value (dev)); +ut_assertok (regulator_set_value_clamp (dev,2700000 ,3400000 ,3600000 )); +ut_asserteq (3300000 ,regulator_get_value (dev)); + +/* 'min'/'max' out of range - should return -EINVAL */ +ut_asserteq (-EINVAL , +regulator_set_value_clamp (dev,1200000 ,1500000 ,1700000 )); +ut_asserteq (-EINVAL , +regulator_set_value_clamp (dev,3500000 ,4000000 ,5000000 )); + +/* 'min' higher than 'max' - should return -EINVAL */ +ut_asserteq (-EINVAL , +regulator_set_value_clamp (dev,3100000 ,3000000 ,2900000 )); + +return 0 ; +} +DM_TEST (dm_test_power_regulator_set_value_clamp,UTF_SCAN_FDT ); +/* Test regulator set and get Current method */ static int dm_test_power_regulator_set_get_current (struct unit_test_state *uts) {
Signature · 1 line
2.54.0
PATCH v2 3/4 mmc: dw_mmc: Allow use of in-spec voltage range for vqmmc-supply
In reply to Jonas KarlmanThe Rockchip RK3399 SoC SDMMC IO domain supports 1.8V and 3.0V mode, and the 3.0V mode is within SD Standards allowed 2.7V-3.6V range. However, the commit 0b75109b6aaf ("mmc: dw_mmc: return error for invalid voltage setting") help enforce strict 1.8V and 3.3V when setting vqmmc-supply making mmc_set_signal_voltage() now fail for MMC_SIGNAL_VOLTAGE_330 and leading to an improper switch to MMC_SIGNAL_VOLTAGE_180.
Use regulator_set_value_clamp() to set an SD Standards voltage range, 1.70V-1.95V and 2.7V-3.6V, I/O voltage instead of requiring an exact regulator value to closer match Linux and to fix use of 3.3V signal voltage on Rockchip RK3399 boards using MMC_IO_VOLTAGE=y.
---
v2: Collect r-b tag
--- drivers/mmc/dw_mmc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/mmc/dw_mmc.c +4 -2
@@ -645,9 +645,11 @@ static int dwmci_set_ios(struct mmc *mmc)int ret;if (mmc->signal_voltage ==MMC_SIGNAL_VOLTAGE_180 ) - ret =regulator_set_value (mmc->vqmmc_supply,1800000 ); + ret =regulator_set_value_clamp (mmc->vqmmc_supply, +1700000 ,1800000 ,1950000 );else - ret =regulator_set_value (mmc->vqmmc_supply,3300000 ); + ret =regulator_set_value_clamp (mmc->vqmmc_supply, +2700000 ,3300000 ,3600000 );if (ret && ret != -ENOSYS )return ret; }
Signature · 1 line
2.54.0
PATCH v2 4/4 mmc: sdhci: Use CONFIG_IS_ENABLED for MMC_IO_VOLTAGE
In reply to Jonas KarlmanUse CONFIG_IS_ENABLED() instead of IS_ENABLED() when checking the MMC_IO_VOLTAGE symbol so the Kconfig option is evaluated correctly for xPL builds.
---
v2: Collect r-b tag
--- drivers/mmc/sdhci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/sdhci.c +1 -1
@@ -551,7 +551,7 @@ void sdhci_set_uhs_timing(struct sdhci_host *host)void sdhci_set_voltage (struct sdhci_host *host) { -if (IS_ENABLED (CONFIG_MMC_IO_VOLTAGE )) { +if (CONFIG_IS_ENABLED (MMC_IO_VOLTAGE )) {struct mmc *mmc = (struct mmc *)host->mmc;u32 ctrl;
Signature · 1 line
2.54.0