| // SPDX-License-Identifier: GPL-2.0+ |
| /* |
| * Tests for the driver model regulator API |
| * |
| * Copyright (c) 2015 Samsung Electronics |
| * Przemyslaw Marczak <p.marczak@samsung.com> |
| */ |
| |
| #include <errno.h> |
| #include <dm.h> |
| #include <fdtdec.h> |
| #include <log.h> |
| #include <malloc.h> |
| #include <dm/device-internal.h> |
| #include <dm/root.h> |
| #include <dm/util.h> |
| #include <dm/test.h> |
| #include <dm/uclass-internal.h> |
| #include <power/pmic.h> |
| #include <power/regulator.h> |
| #include <power/sandbox_pmic.h> |
| #include <test/test.h> |
| #include <test/ut.h> |
| |
| enum { |
| BUCK1, |
| BUCK2, |
| BUCK3, |
| LDO1, |
| LDO2, |
| OUTPUT_COUNT, |
| }; |
| |
| enum { |
| DEVNAME = 0, |
| PLATNAME, |
| OUTPUT_NAME_COUNT, |
| }; |
| |
| static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = { |
| /* devname, platname */ |
| { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME }, |
| { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME }, |
| { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME }, |
| { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME}, |
| { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME}, |
| }; |
| |
| /* Test regulator get method */ |
| static int dm_test_power_regulator_get(struct unit_test_state *uts) |
| { |
| struct dm_regulator_uclass_plat *uc_pdata; |
| struct udevice *dev_by_devname; |
| struct udevice *dev_by_platname; |
| const char *devname; |
| const char *platname; |
| int i; |
| |
| for (i = 0; i < OUTPUT_COUNT; i++) { |
| /* |
| * Do the test for each regulator's devname and platname, |
| * which are related to a single device. |
| */ |
| devname = regulator_names[i][DEVNAME]; |
| platname = regulator_names[i][PLATNAME]; |
| |
| /* |
| * Check, that regulator_get_by_devname() function, returns |
| * a device with the name equal to the requested one. |
| */ |
| ut_assertok(regulator_get_by_devname(devname, &dev_by_devname)); |
| ut_asserteq_str(devname, dev_by_devname->name); |
| |
| /* |
| * Check, that regulator_get_by_platname() function, returns |
| * a device with the name equal to the requested one. |
| */ |
| ut_assertok(regulator_get_by_platname(platname, &dev_by_platname)); |
| uc_pdata = dev_get_uclass_plat(dev_by_platname); |
| ut_assert(uc_pdata); |
| ut_asserteq_str(platname, uc_pdata->name); |
| |
| /* |
| * Check, that the pointers returned by both get functions, |
| * points to the same regulator device. |
| */ |
| ut_asserteq_ptr(dev_by_devname, dev_by_platname); |
| } |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_get, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get Voltage method */ |
| static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts) |
| { |
| struct dm_regulator_uclass_plat *uc_pdata; |
| struct udevice *dev; |
| const char *platname; |
| int val_set, val_get; |
| |
| /* Set and get Voltage of BUCK1 - set to 'min' constraint */ |
| platname = regulator_names[BUCK1][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| |
| uc_pdata = dev_get_uclass_plat(dev); |
| ut_assert(uc_pdata); |
| |
| val_set = uc_pdata->min_uV; |
| ut_assertok(regulator_set_value(dev, val_set)); |
| |
| val_get = regulator_get_value(dev); |
| ut_assert(val_get >= 0); |
| |
| ut_asserteq(val_set, val_get); |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_get_voltage, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get Current method */ |
| static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts) |
| { |
| struct dm_regulator_uclass_plat *uc_pdata; |
| struct udevice *dev; |
| const char *platname; |
| int val_set, val_get; |
| |
| /* Set and get the Current of LDO1 - set to 'min' constraint */ |
| platname = regulator_names[LDO1][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| |
| uc_pdata = dev_get_uclass_plat(dev); |
| ut_assert(uc_pdata); |
| |
| val_set = uc_pdata->min_uA; |
| ut_assertok(regulator_set_current(dev, val_set)); |
| |
| val_get = regulator_get_current(dev); |
| ut_assert(val_get >= 0); |
| |
| ut_asserteq(val_set, val_get); |
| |
| /* Check LDO2 current limit constraints - should be -ENODATA */ |
| platname = regulator_names[LDO2][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| |
| uc_pdata = dev_get_uclass_plat(dev); |
| ut_assert(uc_pdata); |
| ut_asserteq(-ENODATA, uc_pdata->min_uA); |
| ut_asserteq(-ENODATA, uc_pdata->max_uA); |
| |
| /* Try set the Current of LDO2 - should return -ENOSYS */ |
| ut_asserteq(-ENOSYS, regulator_set_current(dev, 0)); |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_get_current, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get Enable method */ |
| static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts) |
| { |
| const char *platname; |
| struct udevice *dev; |
| bool val_set = true; |
| |
| /* Set the Enable of LDO1 - default is disabled */ |
| platname = regulator_names[LDO1][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| ut_assertok(regulator_set_enable(dev, val_set)); |
| |
| /* Get the Enable state of LDO1 and compare it with the requested one */ |
| ut_asserteq(regulator_get_enable(dev), val_set); |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_get_enable, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get enable if allowed method */ |
| static |
| int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts) |
| { |
| const char *platname; |
| struct udevice *dev, *dev_autoset; |
| bool val_set = false; |
| |
| /* Get BUCK1 - always on regulator */ |
| platname = regulator_names[BUCK1][PLATNAME]; |
| ut_asserteq(-EALREADY, regulator_autoset_by_name(platname, &dev_autoset)); |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| |
| /* Try disabling always-on regulator */ |
| ut_assertok(regulator_set_enable_if_allowed(dev, val_set)); |
| ut_asserteq(regulator_get_enable(dev), !val_set); |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_enable_if_allowed, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get mode method */ |
| static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts) |
| { |
| const char *platname; |
| struct udevice *dev; |
| int val_set = LDO_OM_SLEEP; |
| |
| /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */ |
| platname = regulator_names[LDO1][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| ut_assertok(regulator_set_mode(dev, val_set)); |
| |
| /* Get the mode id of LDO1 and compare it with the requested one */ |
| ut_asserteq(regulator_get_mode(dev), val_set); |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_get_mode, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get suspend Voltage method */ |
| static int dm_test_power_regulator_set_get_suspend_voltage(struct unit_test_state *uts) |
| { |
| struct dm_regulator_uclass_plat *uc_pdata; |
| const struct dm_regulator_ops *ops; |
| struct udevice *dev; |
| const char *platname; |
| int val_set, val_get; |
| |
| /* Set and get Voltage of BUCK1 - set to 'min' constraint */ |
| platname = regulator_names[BUCK1][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| |
| uc_pdata = dev_get_uclass_plat(dev); |
| ut_assert(uc_pdata); |
| |
| ops = dev_get_driver_ops(dev); |
| |
| if (ops->set_suspend_value && ops->get_suspend_value) { |
| val_set = uc_pdata->suspend_uV; |
| ut_assertok(regulator_set_suspend_value(dev, val_set)); |
| val_get = regulator_get_suspend_value(dev); |
| ut_assert(val_get >= 0); |
| |
| ut_asserteq(val_set, val_get); |
| } |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_get_suspend_voltage, UTF_SCAN_FDT); |
| |
| /* Test regulator set and get suspend Enable method */ |
| static int dm_test_power_regulator_set_get_suspend_enable(struct unit_test_state *uts) |
| { |
| const struct dm_regulator_ops *ops; |
| const char *platname; |
| struct udevice *dev; |
| bool val_set = true; |
| |
| /* Set the Enable of LDO1 - default is disabled */ |
| platname = regulator_names[LDO1][PLATNAME]; |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| |
| ops = dev_get_driver_ops(dev); |
| |
| if (ops->set_suspend_enable && ops->get_suspend_enable) { |
| ut_assertok(regulator_set_suspend_enable(dev, val_set)); |
| |
| /* |
| * Get the Enable state of LDO1 and |
| * compare it with the requested one |
| */ |
| ut_asserteq(regulator_get_suspend_enable(dev), val_set); |
| } |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_set_get_suspend_enable, UTF_SCAN_FDT); |
| |
| /* Test regulator autoset method */ |
| static int dm_test_power_regulator_autoset(struct unit_test_state *uts) |
| { |
| const char *platname; |
| struct udevice *dev, *dev_autoset; |
| |
| /* |
| * Test the BUCK1 with fdt properties |
| * - min-microvolt = max-microvolt = 1200000 |
| * - min-microamp = max-microamp = 200000 |
| * - always-on = set |
| * - boot-on = not set |
| * Expected output state: uV=1200000; uA=200000; output enabled |
| */ |
| platname = regulator_names[BUCK1][PLATNAME]; |
| ut_asserteq(-EALREADY, regulator_autoset_by_name(platname, &dev_autoset)); |
| |
| /* Check, that the returned device is proper */ |
| ut_assertok(regulator_get_by_platname(platname, &dev)); |
| ut_asserteq_ptr(dev, dev_autoset); |
| |
| /* Check the setup after autoset */ |
| ut_asserteq(regulator_get_value(dev), |
| SANDBOX_BUCK1_AUTOSET_EXPECTED_UV); |
| ut_asserteq(regulator_get_current(dev), |
| SANDBOX_BUCK1_AUTOSET_EXPECTED_UA); |
| ut_asserteq(regulator_get_enable(dev), |
| SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE); |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_autoset, UTF_SCAN_FDT); |
| |
| /* |
| * Struct setting: to keep the expected output settings. |
| * @voltage: Voltage value [uV] |
| * @current: Current value [uA] |
| * @enable: output enable state: true/false |
| */ |
| struct setting { |
| int voltage; |
| int current; |
| bool enable; |
| }; |
| |
| /* |
| * platname_list: an array of regulator platform names. |
| * For testing regulator_list_autoset() for outputs: |
| * - LDO1 |
| * - LDO2 |
| */ |
| static const char *platname_list[] = { |
| SANDBOX_LDO1_PLATNAME, |
| SANDBOX_LDO2_PLATNAME, |
| NULL, |
| }; |
| |
| /* |
| * expected_setting_list: an array of regulator output setting, expected after |
| * call of the regulator_list_autoset() for the "platname_list" array. |
| * For testing results of regulator_list_autoset() for outputs: |
| * - LDO1 |
| * - LDO2 |
| * The settings are defined in: include/power/sandbox_pmic.h |
| */ |
| static const struct setting expected_setting_list[] = { |
| [0] = { /* LDO1 */ |
| .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV, |
| .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA, |
| .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE, |
| }, |
| [1] = { /* LDO2 */ |
| .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV, |
| .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA, |
| .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE, |
| }, |
| }; |
| |
| static int list_count = ARRAY_SIZE(expected_setting_list); |
| |
| /* Test regulator list autoset method */ |
| static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts) |
| { |
| struct udevice *dev_list[2], *dev; |
| int i; |
| |
| /* |
| * Test the settings of the regulator list: |
| * LDO1 with fdt properties: |
| * - min-microvolt = max-microvolt = 1800000 |
| * - min-microamp = max-microamp = 100000 |
| * - always-on = not set |
| * - boot-on = set |
| * Expected output state: uV=1800000; uA=100000; output enabled |
| * |
| * LDO2 with fdt properties: |
| * - min-microvolt = max-microvolt = 3300000 |
| * - always-on = not set |
| * - boot-on = not set |
| * Expected output state: uV=300000(default); output disabled(default) |
| * The expected settings are defined in: include/power/sandbox_pmic.h. |
| */ |
| ut_assertok(regulator_list_autoset(platname_list, dev_list, false)); |
| |
| for (i = 0; i < list_count; i++) { |
| /* Check, that the returned device is non-NULL */ |
| ut_assert(dev_list[i]); |
| |
| /* Check, that the returned device is proper */ |
| ut_assertok(regulator_get_by_platname(platname_list[i], &dev)); |
| ut_asserteq_ptr(dev_list[i], dev); |
| |
| /* Check, that regulator output Voltage value is as expected */ |
| ut_asserteq(regulator_get_value(dev_list[i]), |
| expected_setting_list[i].voltage); |
| |
| /* Check, that regulator output Current value is as expected */ |
| ut_asserteq(regulator_get_current(dev_list[i]), |
| expected_setting_list[i].current); |
| |
| /* Check, that regulator output Enable state is as expected */ |
| ut_asserteq(regulator_get_enable(dev_list[i]), |
| expected_setting_list[i].enable); |
| } |
| |
| return 0; |
| } |
| DM_TEST(dm_test_power_regulator_autoset_list, UTF_SCAN_FDT); |