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 enable if allowed method */ 179 static 180 int dm_test_power_regulator_set_enable_if_allowed(struct unit_test_state *uts) 181 { 182 const char *platname; 183 struct udevice *dev, *dev_autoset; 184 bool val_set = false; 185 186 /* Get BUCK1 - always on regulator */ 187 platname = regulator_names[BUCK1][PLATNAME]; 188 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset)); 189 ut_assertok(regulator_get_by_platname(platname, &dev)); 190 191 /* Try disabling always-on regulator */ 192 ut_assertok(regulator_set_enable_if_allowed(dev, val_set)); 193 ut_asserteq(regulator_get_enable(dev), !val_set); 194 195 return 0; 196 } 197 DM_TEST(dm_test_power_regulator_set_enable_if_allowed, DM_TESTF_SCAN_FDT); 198 199 /* Test regulator set and get mode method */ 200 static int dm_test_power_regulator_set_get_mode(struct unit_test_state *uts) 201 { 202 const char *platname; 203 struct udevice *dev; 204 int val_set = LDO_OM_SLEEP; 205 206 /* Set the mode id to LDO_OM_SLEEP of LDO1 - default is LDO_OM_OFF */ 207 platname = regulator_names[LDO1][PLATNAME]; 208 ut_assertok(regulator_get_by_platname(platname, &dev)); 209 ut_assertok(regulator_set_mode(dev, val_set)); 210 211 /* Get the mode id of LDO1 and compare it with the requested one */ 212 ut_asserteq(regulator_get_mode(dev), val_set); 213 214 return 0; 215 } 216 DM_TEST(dm_test_power_regulator_set_get_mode, DM_TESTF_SCAN_FDT); 217 218 /* Test regulator autoset method */ 219 static int dm_test_power_regulator_autoset(struct unit_test_state *uts) 220 { 221 const char *platname; 222 struct udevice *dev, *dev_autoset; 223 224 /* 225 * Test the BUCK1 with fdt properties 226 * - min-microvolt = max-microvolt = 1200000 227 * - min-microamp = max-microamp = 200000 228 * - always-on = set 229 * - boot-on = not set 230 * Expected output state: uV=1200000; uA=200000; output enabled 231 */ 232 platname = regulator_names[BUCK1][PLATNAME]; 233 ut_assertok(regulator_autoset_by_name(platname, &dev_autoset)); 234 235 /* Check, that the returned device is proper */ 236 ut_assertok(regulator_get_by_platname(platname, &dev)); 237 ut_asserteq_ptr(dev, dev_autoset); 238 239 /* Check the setup after autoset */ 240 ut_asserteq(regulator_get_value(dev), 241 SANDBOX_BUCK1_AUTOSET_EXPECTED_UV); 242 ut_asserteq(regulator_get_current(dev), 243 SANDBOX_BUCK1_AUTOSET_EXPECTED_UA); 244 ut_asserteq(regulator_get_enable(dev), 245 SANDBOX_BUCK1_AUTOSET_EXPECTED_ENABLE); 246 247 return 0; 248 } 249 DM_TEST(dm_test_power_regulator_autoset, DM_TESTF_SCAN_FDT); 250 251 /* 252 * Struct setting: to keep the expected output settings. 253 * @voltage: Voltage value [uV] 254 * @current: Current value [uA] 255 * @enable: output enable state: true/false 256 */ 257 struct setting { 258 int voltage; 259 int current; 260 bool enable; 261 }; 262 263 /* 264 * platname_list: an array of regulator platform names. 265 * For testing regulator_list_autoset() for outputs: 266 * - LDO1 267 * - LDO2 268 */ 269 static const char *platname_list[] = { 270 SANDBOX_LDO1_PLATNAME, 271 SANDBOX_LDO2_PLATNAME, 272 NULL, 273 }; 274 275 /* 276 * expected_setting_list: an array of regulator output setting, expected after 277 * call of the regulator_list_autoset() for the "platname_list" array. 278 * For testing results of regulator_list_autoset() for outputs: 279 * - LDO1 280 * - LDO2 281 * The settings are defined in: include/power/sandbox_pmic.h 282 */ 283 static const struct setting expected_setting_list[] = { 284 [0] = { /* LDO1 */ 285 .voltage = SANDBOX_LDO1_AUTOSET_EXPECTED_UV, 286 .current = SANDBOX_LDO1_AUTOSET_EXPECTED_UA, 287 .enable = SANDBOX_LDO1_AUTOSET_EXPECTED_ENABLE, 288 }, 289 [1] = { /* LDO2 */ 290 .voltage = SANDBOX_LDO2_AUTOSET_EXPECTED_UV, 291 .current = SANDBOX_LDO2_AUTOSET_EXPECTED_UA, 292 .enable = SANDBOX_LDO2_AUTOSET_EXPECTED_ENABLE, 293 }, 294 }; 295 296 static int list_count = ARRAY_SIZE(expected_setting_list); 297 298 /* Test regulator list autoset method */ 299 static int dm_test_power_regulator_autoset_list(struct unit_test_state *uts) 300 { 301 struct udevice *dev_list[2], *dev; 302 int i; 303 304 /* 305 * Test the settings of the regulator list: 306 * LDO1 with fdt properties: 307 * - min-microvolt = max-microvolt = 1800000 308 * - min-microamp = max-microamp = 100000 309 * - always-on = not set 310 * - boot-on = set 311 * Expected output state: uV=1800000; uA=100000; output enabled 312 * 313 * LDO2 with fdt properties: 314 * - min-microvolt = max-microvolt = 3300000 315 * - always-on = not set 316 * - boot-on = not set 317 * Expected output state: uV=300000(default); output disabled(default) 318 * The expected settings are defined in: include/power/sandbox_pmic.h. 319 */ 320 ut_assertok(regulator_list_autoset(platname_list, dev_list, false)); 321 322 for (i = 0; i < list_count; i++) { 323 /* Check, that the returned device is non-NULL */ 324 ut_assert(dev_list[i]); 325 326 /* Check, that the returned device is proper */ 327 ut_assertok(regulator_get_by_platname(platname_list[i], &dev)); 328 ut_asserteq_ptr(dev_list[i], dev); 329 330 /* Check, that regulator output Voltage value is as expected */ 331 ut_asserteq(regulator_get_value(dev_list[i]), 332 expected_setting_list[i].voltage); 333 334 /* Check, that regulator output Current value is as expected */ 335 ut_asserteq(regulator_get_current(dev_list[i]), 336 expected_setting_list[i].current); 337 338 /* Check, that regulator output Enable state is as expected */ 339 ut_asserteq(regulator_get_enable(dev_list[i]), 340 expected_setting_list[i].enable); 341 } 342 343 return 0; 344 } 345 DM_TEST(dm_test_power_regulator_autoset_list, DM_TESTF_SCAN_FDT); 346