test: dm: regulator: Add regulator_set_value_clamp() tests

Add 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.

Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
Reviewed-by: Peng Fan <peng.fan@nxp.com>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
This commit is contained in:
Jonas Karlman
2026-07-12 16:18:32 +08:00
committed by Peng Fan
parent 0663b4f553
commit 10acd5bc9a
5 changed files with 65 additions and 8 deletions
+6
View File
@@ -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 {
+2 -1
View File
@@ -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
+6 -5
View File
@@ -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,
+13 -2
View File
@@ -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:
+38
View File
@@ -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)
{