1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Tests for the driver model regulator API 4 * 5 * Copyright (c) 2015 Samsung Electronics 6 * Przemyslaw Marczak <p.marczak@samsung.com> 7 */ 8 9 #include <common.h> 10 #include <errno.h> 11 #include <dm.h> 12 #include <fdtdec.h> 13 #include <malloc.h> 14 #include <dm/device-internal.h> 15 #include <dm/root.h> 16 #include <dm/util.h> 17 #include <dm/test.h> 18 #include <dm/uclass-internal.h> 19 #include <power/pmic.h> 20 #include <power/regulator.h> 21 #include <power/sandbox_pmic.h> 22 #include <test/ut.h> 23 24 enum { 25 BUCK1, 26 BUCK2, 27 BUCK3, 28 LDO1, 29 LDO2, 30 OUTPUT_COUNT, 31 }; 32 33 enum { 34 DEVNAME = 0, 35 PLATNAME, 36 OUTPUT_NAME_COUNT, 37 }; 38 39 static const char *regulator_names[OUTPUT_COUNT][OUTPUT_NAME_COUNT] = { 40 /* devname, platname */ 41 { SANDBOX_BUCK1_DEVNAME, SANDBOX_BUCK1_PLATNAME }, 42 { SANDBOX_BUCK2_DEVNAME, SANDBOX_BUCK2_PLATNAME }, 43 { SANDBOX_BUCK3_DEVNAME, SANDBOX_BUCK3_PLATNAME }, 44 { SANDBOX_LDO1_DEVNAME, SANDBOX_LDO1_PLATNAME}, 45 { SANDBOX_LDO2_DEVNAME, SANDBOX_LDO2_PLATNAME}, 46 }; 47 48 /* Test regulator get method */ 49 static int dm_test_power_regulator_get(struct unit_test_state *uts) 50 { 51 struct dm_regulator_uclass_platdata *uc_pdata; 52 struct udevice *dev_by_devname; 53 struct udevice *dev_by_platname; 54 const char *devname; 55 const char *platname; 56 int i; 57 58 for (i = 0; i < OUTPUT_COUNT; i++) { 59 /* 60 * Do the test for each regulator's devname and platname, 61 * which are related to a single device. 62 */ 63 devname = regulator_names[i][DEVNAME]; 64 platname = regulator_names[i][PLATNAME]; 65 66 /* 67 * Check, that regulator_get_by_devname() function, returns 68 * a device with the name equal to the requested one. 69 */ 70 ut_assertok(regulator_get_by_devname(devname, &dev_by_devname)); 71 ut_asserteq_str(devname, dev_by_devname->name); 72 73 /* 74 * Check, that regulator_get_by_platname() function, returns 75 * a device with the name equal to the requested one. 76 */ 77 ut_assertok(regulator_get_by_platname(platname, &dev_by_platname)); 78 uc_pdata = dev_get_uclass_platdata(dev_by_platname); 79 ut_assert(uc_pdata); 80 ut_asserteq_str(platname, uc_pdata->name); 81 82 /* 83 * Check, that the pointers returned by both get functions, 84 * points to the same regulator device. 85 */ 86 ut_asserteq_ptr(dev_by_devname, dev_by_platname); 87 } 88 89 return 0; 90 } 91 DM_TEST(dm_test_power_regulator_get, DM_TESTF_SCAN_FDT); 92 93 /* Test regulator set and get Voltage method */ 94 static int dm_test_power_regulator_set_get_voltage(struct unit_test_state *uts) 95 { 96 struct dm_regulator_uclass_platdata *uc_pdata; 97 struct udevice *dev; 98 const char *platname; 99 int val_set, val_get; 100 101 /* Set and get Voltage of BUCK1 - set to 'min' constraint */ 102 platname = regulator_names[BUCK1][PLATNAME]; 103 ut_assertok(regulator_get_by_platname(platname, &dev)); 104 105 uc_pdata = dev_get_uclass_platdata(dev); 106 ut_assert(uc_pdata); 107 108 val_set = uc_pdata->min_uV; 109 ut_assertok(regulator_set_value(dev, val_set)); 110 111 val_get = regulator_get_value(dev); 112 ut_assert(val_get >= 0); 113 114 ut_asserteq(val_set, val_get); 115 116 return 0; 117 } 118 DM_TEST(dm_test_power_regulator_set_get_voltage, DM_TESTF_SCAN_FDT); 119 120 /* Test regulator set and get Current method */ 121 static int dm_test_power_regulator_set_get_current(struct unit_test_state *uts) 122 { 123 struct dm_regulator_uclass_platdata *uc_pdata; 124 struct udevice *dev; 125 const char *platname; 126 int val_set, val_get; 127 128 /* Set and get the Current of LDO1 - set to 'min' constraint */ 129 platname = regulator_names[LDO1][PLATNAME]; 130 ut_assertok(regulator_get_by_platname(platname, &dev)); 131 132 uc_pdata = dev_get_uclass_platdata(dev); 133 ut_assert(uc_pdata); 134 135 val_set = uc_pdata->min_uA; 136 ut_assertok(regulator_set_current(dev, val_set)); 137 138 val_get = regulator_get_current(dev); 139 ut_assert(val_get >= 0); 140 141 ut_asserteq(val_set, val_get); 142 143 /* Check LDO2 current limit constraints - should be -ENODATA */ 144 platname = regulator_names[LDO2][PLATNAME]; 145 ut_assertok(regulator_get_by_platname(platname, &dev)); 146 147 uc_pdata = dev_get_uclass_platdata(dev); 148 ut_assert(uc_pdata); 149 ut_asserteq(-ENODATA, uc_pdata->min_uA); 150 ut_asserteq(-ENODATA, uc_pdata->max_uA); 151 152 /* Try set the Current of LDO2 - should return -ENOSYS */ 153 ut_asserteq(-ENOSYS, regulator_set_current(dev, 0)); 154 155 return 0; 156 } 157 DM_TEST(dm_test_power_regulator_set_get_current, DM_TESTF_SCAN_FDT); 158 159 /* Test regulator set and get Enable method */ 160 static int dm_test_power_regulator_set_get_enable(struct unit_test_state *uts) 161 { 162 const char *platname; 163 struct udevice *dev; 164 bool val_set = true; 165 166 /* Set the Enable of LDO1 - default is disabled */ 167 platname = regulator_names[LDO1][PLATNAME]; 168 ut_assertok(regulator_get_by_platname(platname, &dev)); 169 ut_assertok(regulator_set_enable(dev, val_set)); 170 171 /* Get the Enable state of LDO1 and compare it with the requested one */ 172 ut_asserteq(regulator_get_enable(dev), val_set); 173 174 return 0; 175 } 176 DM_TEST(dm_test_power_regulator_set_get_enable, DM_TESTF_SCAN_FDT); 177 178 /* Test regulator set and get mode method */ 179 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts) 180 { 181 const char *platname; 182 struct udevice *dev; 183 int val_set = LDO_OM_SLEEP; 184 185 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */ 186 platname = regulator_names[LDO1][PLATNAME]; 187 ut_assertok(regulator_get_by_platname(platname, &dev)); 188 ut_assertok(regulator_set_mode(dev, val_set)); 189 190 /* Get the mode id of LDO1 and compare it with the requested one */ 191 ut_asserteq(regulator_get_mode(dev), val_set); 192 193 return 0; 194 } 195 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT); 196 197 /* Test regulator autoset method */ 198 static int dm_test_power_regulator_autoset(struct unit_test_state *uts) 199 { 200 const char *platname; 201 struct udevice *dev, *dev_autoset; 202 203 /* 204 * Test the BUCK1 with fdt properties 205 * - min-microvolt = max-microvolt = 1200000 206 * - min-microamp = max-microamp = 200000 207 * - always-on = set 208 * - boot-on = not set 209 * Expected output state: uV=1200000; uA=200000; output enabled 210 */ 211 platname = regulator_names[BUCK1][PLATNAME]; 212 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset)); 213 214 /* Check, that the returned device is proper */ 215 ut_assertok(regulator_get_by_platname(platname, &dev)); 216 ut_asserteq_ptr(dev, dev_autoset); 217 218 /* Check the setup after autoset */ 219 ut_asserteq(regulator_get_value(dev), 220 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV); 221 ut_asserteq(regulator_get_current(dev), 222 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA); 223 ut_asserteq(regulator_get_enable(dev), 224 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE); 225 226 return 0; 227 } 228 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT); 229 230 /* 231 * Struct setting: to keep the expected output settings. 232 * @voltage: Voltage value [uV] 233 * @current: Current value [uA] 234 * @enable: output enable state: true/false 235 */ 236 struct setting { 237 int voltage; 238 int current; 239 bool enable; 240 }; 241 242 /* 243 * platname_list: an array of regulator platform names. 244 * For testing regulator_list_autoset() for outputs: 245 * - LDO1 246 * - LDO2 247 */ 248 static const char *platname_list[] = { 249 SANDBOX_LDO1_PLATNAME, 250 SANDBOX_LDO2_PLATNAME, 251 NULL, 252 }; 253 254 /* 255 * expected_setting_list: an array of regulator output setting, expected after 256 * call of the regulator_list_autoset() for the "platname_list" array. 257 * For testing results of regulator_list_autoset() for outputs: 258 * - LDO1 259 * - LDO2 260 * The settings are defined in: include/power/sandbox_pmic.h 261 */ 262 static const struct setting expected_setting_list[] = { 263 [0] = { /* LDO1 */ 264 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV, 265 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA, 266 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE, 267 }, 268 [1] = { /* LDO2 */ 269 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV, 270 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA, 271 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE, 272 }, 273 }; 274 275 static int list_count = ARRAY_SIZE(expected_setting_list); 276 277 /* Test regulator list autoset method */ 278 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts) 279 { 280 struct udevice *dev_list[2], *dev; 281 int i; 282 283 /* 284 * Test the settings of the regulator list: 285 * LDO1 with fdt properties: 286 * - min-microvolt = max-microvolt = 1800000 287 * - min-microamp = max-microamp = 100000 288 * - always-on = not set 289 * - boot-on = set 290 * Expected output state: uV=1800000; uA=100000; output enabled 291 * 292 * LDO2 with fdt properties: 293 * - min-microvolt = max-microvolt = 3300000 294 * - always-on = not set 295 * - boot-on = not set 296 * Expected output state: uV=300000(default); output disabled(default) 297 * The expected settings are defined in: include/power/sandbox_pmic.h. 298 */ 299 ut_assertok(regulator_list_autoset(platname_list, dev_list, false)); 300 301 for (i = 0; i < list_count; i++) { 302 /* Check, that the returned device is non-NULL */ 303 ut_assert(dev_list[i]); 304 305 /* Check, that the returned device is proper */ 306 ut_assertok(regulator_get_by_platname(platname_list[i], &dev)); 307 ut_asserteq_ptr(dev_list[i], dev); 308 309 /* Check, that regulator output Voltage value is as expected */ 310 ut_asserteq(regulator_get_value(dev_list[i]), 311 expected_setting_list[i].voltage); 312 313 /* Check, that regulator output Current value is as expected */ 314 ut_asserteq(regulator_get_current(dev_list[i]), 315 expected_setting_list[i].current); 316 317 /* Check, that regulator output Enable state is as expected */ 318 ut_asserteq(regulator_get_enable(dev_list[i]), 319 expected_setting_list[i].enable); 320 } 321 322 return 0; 323 } 324 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT); 325