1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/drivers/regulator.h>
7 #include <zephyr/drivers/regulator/fake.h>
8 #include <zephyr/ztest.h>
9 
10 DEFINE_FFF_GLOBALS;
11 
12 static const struct device *const parent = DEVICE_DT_GET(DT_NODELABEL(regulator));
13 /* REG0: no Devicetree properties */
14 static const struct device *const reg0 = DEVICE_DT_GET(DT_NODELABEL(reg0));
15 /* REG1: regulator-always-on */
16 static const struct device *const reg1 = DEVICE_DT_GET(DT_NODELABEL(reg1));
17 /* REG2: regulator-boot-on */
18 static const struct device *const reg2 = DEVICE_DT_GET(DT_NODELABEL(reg2));
19 /* REG3: regulator-max/min-microvolt/microamp, regulator-allowed-modes */
20 static const struct device *const reg3 = DEVICE_DT_GET(DT_NODELABEL(reg3));
21 /* REG4: regulator-boot-off */
22 static const struct device *const reg4 = DEVICE_DT_GET(DT_NODELABEL(reg4));
23 /* REG5: regulator-boot-off and is_enabled */
24 static const struct device *const reg5 = DEVICE_DT_GET(DT_NODELABEL(reg5));
25 
26 static DEVICE_API(regulator, dummy_regulator_api);
27 static DEVICE_API(regulator_parent, dummy_regulator_parent_api);
28 
29 DEVICE_DEFINE(dummy_regulator, "dummy_regulator", NULL, NULL, NULL, NULL, POST_KERNEL,
30 	      CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &dummy_regulator_api);
31 DEVICE_DEFINE(dummy_regulator_parent, "dummy_regulator_parent", NULL, NULL, NULL, NULL, POST_KERNEL,
32 	      CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &dummy_regulator_parent_api);
33 
34 /* Dummy regulator: empty API */
35 static const struct device *const dummy_reg = DEVICE_GET(dummy_regulator);
36 /* Dummy parent regulator: empty API */
37 static const struct device *const dummy_parent = DEVICE_GET(dummy_regulator_parent);
38 
ZTEST(regulator_api,test_parent_dvs_state_set_not_implemented)39 ZTEST(regulator_api, test_parent_dvs_state_set_not_implemented)
40 {
41 	int ret = regulator_parent_dvs_state_set(dummy_parent, 0);
42 
43 	zassert_equal(ret, -ENOSYS);
44 }
45 
ZTEST(regulator_api,test_parent_dvs_state_set_ok)46 ZTEST(regulator_api, test_parent_dvs_state_set_ok)
47 {
48 	RESET_FAKE(regulator_parent_fake_dvs_state_set);
49 
50 	regulator_parent_fake_dvs_state_set_fake.return_val = 0;
51 
52 	zassert_equal(regulator_parent_dvs_state_set(parent, 0U), 0);
53 	zassert_equal(regulator_parent_fake_dvs_state_set_fake.arg0_val, parent);
54 	zassert_equal(regulator_parent_fake_dvs_state_set_fake.arg1_val, 0U);
55 	zassert_equal(regulator_parent_fake_dvs_state_set_fake.call_count, 1U);
56 }
57 
ZTEST(regulator_api,test_parent_dvs_state_set_fail)58 ZTEST(regulator_api, test_parent_dvs_state_set_fail)
59 {
60 	RESET_FAKE(regulator_parent_fake_dvs_state_set);
61 
62 	regulator_parent_fake_dvs_state_set_fake.return_val = -ENOTSUP;
63 
64 	zassert_equal(regulator_parent_dvs_state_set(parent, 0U), -ENOTSUP);
65 	zassert_equal(regulator_parent_fake_dvs_state_set_fake.arg0_val, parent);
66 	zassert_equal(regulator_parent_fake_dvs_state_set_fake.arg1_val, 0U);
67 	zassert_equal(regulator_parent_fake_dvs_state_set_fake.call_count, 1U);
68 }
69 
ZTEST(regulator_api,test_parent_ship_mode_not_implemented)70 ZTEST(regulator_api, test_parent_ship_mode_not_implemented)
71 {
72 	int ret = regulator_parent_ship_mode(dummy_parent);
73 
74 	zassert_equal(ret, -ENOSYS);
75 }
76 
ZTEST(regulator_api,test_parent_ship_mode_ok)77 ZTEST(regulator_api, test_parent_ship_mode_ok)
78 {
79 	RESET_FAKE(regulator_parent_fake_ship_mode);
80 
81 	regulator_parent_fake_ship_mode_fake.return_val = 0;
82 
83 	zassert_equal(regulator_parent_ship_mode(parent), 0);
84 	zassert_equal(regulator_parent_fake_ship_mode_fake.arg0_val, parent);
85 	zassert_equal(regulator_parent_fake_ship_mode_fake.call_count, 1U);
86 }
87 
ZTEST(regulator_api,test_parent_ship_mode_fail)88 ZTEST(regulator_api, test_parent_ship_mode_fail)
89 {
90 	RESET_FAKE(regulator_parent_fake_ship_mode);
91 
92 	regulator_parent_fake_ship_mode_fake.return_val = -ENOTSUP;
93 
94 	zassert_equal(regulator_parent_ship_mode(parent), -ENOTSUP);
95 	zassert_equal(regulator_parent_fake_ship_mode_fake.arg0_val, parent);
96 	zassert_equal(regulator_parent_fake_ship_mode_fake.call_count, 1U);
97 }
98 
ZTEST(regulator_api,test_common_config)99 ZTEST(regulator_api, test_common_config)
100 {
101 	const struct regulator_common_config *config;
102 
103 	/* reg0: all defaults */
104 	config = reg0->config;
105 	zassert_equal(config->min_uv, INT32_MIN);
106 	zassert_equal(config->max_uv, INT32_MAX);
107 	zassert_equal(config->min_ua, INT32_MIN);
108 	zassert_equal(config->max_ua, INT32_MAX);
109 	zassert_equal(config->allowed_modes_cnt, 0U);
110 	zassert_equal(config->initial_mode, REGULATOR_INITIAL_MODE_UNKNOWN);
111 	zassert_equal(REGULATOR_ACTIVE_DISCHARGE_GET_BITS(config->flags),
112 		      REGULATOR_ACTIVE_DISCHARGE_DEFAULT);
113 
114 	/* reg1: regulator-always-on */
115 	config = reg1->config;
116 	zassert_equal(config->flags & REGULATOR_ALWAYS_ON, REGULATOR_ALWAYS_ON);
117 
118 	/* reg2: regulator-boot-on */
119 	config = reg2->config;
120 	zassert_equal(config->flags & REGULATOR_BOOT_ON, REGULATOR_BOOT_ON);
121 
122 	/* reg3: regulator-min/max-microvolt/microamp */
123 	config = reg3->config;
124 	zassert_equal(config->min_uv, 100);
125 	zassert_equal(config->max_uv, 200);
126 	zassert_equal(config->min_ua, 100);
127 	zassert_equal(config->max_ua, 200);
128 	zassert_equal(config->allowed_modes[0], 1U);
129 	zassert_equal(config->allowed_modes[1], 10U);
130 	zassert_equal(config->allowed_modes_cnt, 2U);
131 	zassert_equal(REGULATOR_ACTIVE_DISCHARGE_GET_BITS(config->flags), 1U);
132 
133 	/* reg4: regulator-boot-off */
134 	config = reg4->config;
135 	zassert_equal(config->flags & REGULATOR_BOOT_OFF, REGULATOR_BOOT_OFF);
136 }
137 
ZTEST(regulator_api,test_common_is_init_enabled)138 ZTEST(regulator_api, test_common_is_init_enabled)
139 {
140 	zassert_false(regulator_common_is_init_enabled(reg0));
141 	zassert_true(regulator_common_is_init_enabled(reg1));
142 	zassert_true(regulator_common_is_init_enabled(reg2));
143 	zassert_false(regulator_common_is_init_enabled(reg3));
144 	zassert_false(regulator_common_is_init_enabled(reg4));
145 	zassert_false(regulator_common_is_init_enabled(reg5));
146 }
147 
ZTEST(regulator_api,test_enable_disable)148 ZTEST(regulator_api, test_enable_disable)
149 {
150 	RESET_FAKE(regulator_fake_enable);
151 	RESET_FAKE(regulator_fake_disable);
152 
153 	/* REG1 already enabled, not enabled again */
154 	zassert_equal(regulator_enable(reg1), 0);
155 	zassert_equal(regulator_fake_enable_fake.call_count, 0U);
156 
157 	/* REG1: can't be disabled */
158 	zassert_equal(regulator_disable(reg1), 0);
159 	zassert_equal(regulator_fake_disable_fake.call_count, 0U);
160 
161 	/* REG2: can be disabled */
162 	zassert_equal(regulator_disable(reg2), 0);
163 	zassert_equal(regulator_fake_disable_fake.arg0_val, reg2);
164 	zassert_equal(regulator_fake_disable_fake.call_count, 1U);
165 
166 	/* REG2: enable again */
167 	zassert_equal(regulator_enable(reg2), 0);
168 	zassert_equal(regulator_fake_enable_fake.arg0_val, reg2);
169 	zassert_equal(regulator_fake_enable_fake.call_count, 1U);
170 
171 	/* REG0: enable */
172 	zassert_equal(regulator_enable(reg0), 0);
173 	zassert_equal(regulator_fake_enable_fake.arg0_val, reg0);
174 	zassert_equal(regulator_fake_enable_fake.call_count, 2U);
175 
176 	/* REG0: disable */
177 	zassert_equal(regulator_disable(reg0), 0);
178 	zassert_equal(regulator_fake_disable_fake.arg0_val, reg0);
179 	zassert_equal(regulator_fake_disable_fake.call_count, 2U);
180 
181 	/* REG5: disabled at boot, can be enabled again */
182 	zassert_equal(regulator_enable(reg5), 0);
183 	zassert_equal(regulator_fake_enable_fake.call_count, 3U);
184 
185 	/* REG5: disable */
186 	zassert_equal(regulator_disable(reg5), 0);
187 	zassert_equal(regulator_fake_disable_fake.call_count, 3U);
188 }
189 
ZTEST(regulator_api,test_count_voltages_not_implemented)190 ZTEST(regulator_api, test_count_voltages_not_implemented)
191 {
192 	unsigned int count = regulator_count_voltages(dummy_reg);
193 
194 	zassert_equal(count, 0U);
195 }
196 
ZTEST(regulator_api,test_count_voltages)197 ZTEST(regulator_api, test_count_voltages)
198 {
199 	RESET_FAKE(regulator_fake_count_voltages);
200 
201 	regulator_fake_count_voltages_fake.return_val = 10U;
202 
203 	zassert_equal(regulator_count_voltages(reg0), 10U);
204 	zassert_equal(regulator_fake_count_voltages_fake.arg0_val, reg0);
205 	zassert_equal(regulator_fake_count_voltages_fake.call_count, 1U);
206 }
207 
ZTEST(regulator_api,test_list_voltage_not_implemented)208 ZTEST(regulator_api, test_list_voltage_not_implemented)
209 {
210 	int ret = regulator_list_voltage(dummy_reg, 0, NULL);
211 
212 	zassert_equal(ret, -EINVAL);
213 }
214 
list_voltage_ok(const struct device * dev,unsigned int idx,int32_t * volt_uv)215 static int list_voltage_ok(const struct device *dev, unsigned int idx, int32_t *volt_uv)
216 {
217 	ARG_UNUSED(dev);
218 	ARG_UNUSED(idx);
219 
220 	*volt_uv = 100;
221 
222 	return 0;
223 }
224 
ZTEST(regulator_api,test_list_voltage_valid)225 ZTEST(regulator_api, test_list_voltage_valid)
226 {
227 	int32_t volt_uv;
228 
229 	RESET_FAKE(regulator_fake_list_voltage);
230 
231 	regulator_fake_list_voltage_fake.custom_fake = list_voltage_ok;
232 
233 	zassert_equal(regulator_list_voltage(reg0, 0, &volt_uv), 0);
234 	zassert_equal(volt_uv, 100);
235 	zassert_equal(regulator_fake_list_voltage_fake.call_count, 1U);
236 	zassert_equal(regulator_fake_list_voltage_fake.arg0_val, reg0);
237 	zassert_equal(regulator_fake_list_voltage_fake.arg1_val, 0);
238 	zassert_equal(regulator_fake_list_voltage_fake.arg2_val, &volt_uv);
239 }
240 
list_voltage_invalid(const struct device * dev,unsigned int idx,int32_t * volt_uv)241 static int list_voltage_invalid(const struct device *dev, unsigned int idx, int32_t *volt_uv)
242 {
243 	ARG_UNUSED(dev);
244 	ARG_UNUSED(idx);
245 	ARG_UNUSED(volt_uv);
246 
247 	return -EINVAL;
248 }
249 
ZTEST(regulator_api,test_list_voltage_invalid)250 ZTEST(regulator_api, test_list_voltage_invalid)
251 {
252 	RESET_FAKE(regulator_fake_list_voltage);
253 
254 	regulator_fake_list_voltage_fake.custom_fake = list_voltage_invalid;
255 
256 	zassert_equal(regulator_list_voltage(reg0, 0, NULL), -EINVAL);
257 	zassert_equal(regulator_fake_list_voltage_fake.call_count, 1U);
258 	zassert_equal(regulator_fake_list_voltage_fake.arg0_val, reg0);
259 	zassert_equal(regulator_fake_list_voltage_fake.arg1_val, 0);
260 	zassert_equal(regulator_fake_list_voltage_fake.arg2_val, NULL);
261 }
262 
list_voltage(const struct device * dev,unsigned int idx,int32_t * volt_uv)263 static int list_voltage(const struct device *dev, unsigned int idx, int32_t *volt_uv)
264 {
265 	ARG_UNUSED(dev);
266 
267 	switch (idx) {
268 	case 0U:
269 		*volt_uv = 100;
270 		break;
271 	case 1U:
272 		*volt_uv = 200;
273 		break;
274 	case 2U:
275 		*volt_uv = 300;
276 		break;
277 	case 3U:
278 		*volt_uv = 400;
279 		break;
280 	default:
281 		return -EINVAL;
282 	}
283 
284 	return 0;
285 }
286 
ZTEST(regulator_api,test_is_supported_voltage)287 ZTEST(regulator_api, test_is_supported_voltage)
288 {
289 	RESET_FAKE(regulator_fake_count_voltages);
290 	RESET_FAKE(regulator_fake_list_voltage);
291 
292 	regulator_fake_count_voltages_fake.return_val = 4U;
293 	regulator_fake_list_voltage_fake.custom_fake = list_voltage;
294 
295 	zassert_false(regulator_is_supported_voltage(reg0, 0, 50));
296 	zassert_true(regulator_is_supported_voltage(reg0, 50, 100));
297 	zassert_true(regulator_is_supported_voltage(reg0, 100, 200));
298 	zassert_true(regulator_is_supported_voltage(reg0, 150, 200));
299 	zassert_true(regulator_is_supported_voltage(reg0, 200, 300));
300 	zassert_true(regulator_is_supported_voltage(reg0, 300, 400));
301 	zassert_true(regulator_is_supported_voltage(reg0, 400, 500));
302 	zassert_false(regulator_is_supported_voltage(reg0, 500, 600));
303 
304 	zassert_not_equal(regulator_fake_count_voltages_fake.call_count, 0U);
305 	zassert_not_equal(regulator_fake_list_voltage_fake.call_count, 0U);
306 }
307 
ZTEST(regulator_api,test_is_supported_voltage_dt_limit)308 ZTEST(regulator_api, test_is_supported_voltage_dt_limit)
309 {
310 	RESET_FAKE(regulator_fake_count_voltages);
311 	RESET_FAKE(regulator_fake_list_voltage);
312 
313 	regulator_fake_count_voltages_fake.return_val = 4U;
314 	regulator_fake_list_voltage_fake.custom_fake = list_voltage;
315 
316 	zassert_false(regulator_is_supported_voltage(reg3, 0, 50));
317 	zassert_true(regulator_is_supported_voltage(reg3, 50, 100));
318 	zassert_true(regulator_is_supported_voltage(reg3, 100, 200));
319 	zassert_true(regulator_is_supported_voltage(reg3, 150, 200));
320 	zassert_true(regulator_is_supported_voltage(reg3, 200, 300));
321 	zassert_false(regulator_is_supported_voltage(reg3, 300, 400));
322 	zassert_false(regulator_is_supported_voltage(reg3, 400, 500));
323 	zassert_false(regulator_is_supported_voltage(reg3, 500, 600));
324 
325 	zassert_not_equal(regulator_fake_count_voltages_fake.call_count, 0U);
326 	zassert_not_equal(regulator_fake_list_voltage_fake.call_count, 0U);
327 }
328 
ZTEST(regulator_api,test_set_voltage_not_implemented)329 ZTEST(regulator_api, test_set_voltage_not_implemented)
330 {
331 	int ret = regulator_set_voltage(dummy_reg, 0, 0);
332 
333 	zassert_equal(ret, -ENOSYS);
334 }
335 
ZTEST(regulator_api,test_set_voltage_ok)336 ZTEST(regulator_api, test_set_voltage_ok)
337 {
338 	RESET_FAKE(regulator_fake_set_voltage);
339 
340 	regulator_fake_set_voltage_fake.return_val = 0;
341 
342 	zassert_equal(regulator_set_voltage(reg0, 0, 0), 0);
343 	zassert_equal(regulator_fake_set_voltage_fake.arg0_val, reg0);
344 	zassert_equal(regulator_fake_set_voltage_fake.arg1_val, 0);
345 	zassert_equal(regulator_fake_set_voltage_fake.arg2_val, 0);
346 	zassert_equal(regulator_fake_set_voltage_fake.call_count, 1U);
347 }
348 
ZTEST(regulator_api,test_set_voltage_fail)349 ZTEST(regulator_api, test_set_voltage_fail)
350 {
351 	RESET_FAKE(regulator_fake_set_voltage);
352 
353 	regulator_fake_set_voltage_fake.return_val = -EINVAL;
354 
355 	zassert_equal(regulator_set_voltage(reg0, 0, 0), -EINVAL);
356 	zassert_equal(regulator_fake_set_voltage_fake.arg0_val, reg0);
357 	zassert_equal(regulator_fake_set_voltage_fake.arg1_val, 0);
358 	zassert_equal(regulator_fake_set_voltage_fake.arg2_val, 0);
359 	zassert_equal(regulator_fake_set_voltage_fake.call_count, 1U);
360 }
361 
ZTEST(regulator_api,test_set_voltage_dt_limit)362 ZTEST(regulator_api, test_set_voltage_dt_limit)
363 {
364 	RESET_FAKE(regulator_fake_set_voltage);
365 
366 	regulator_fake_set_voltage_fake.return_val = 0;
367 
368 	zassert_equal(regulator_set_voltage(reg3, 300, 400), -EINVAL);
369 	zassert_equal(regulator_fake_set_voltage_fake.call_count, 0U);
370 }
371 
ZTEST(regulator_api,test_get_voltage_not_implemented)372 ZTEST(regulator_api, test_get_voltage_not_implemented)
373 {
374 	int ret = regulator_get_voltage(dummy_reg, NULL);
375 
376 	zassert_equal(ret, -ENOSYS);
377 }
378 
get_voltage_ok(const struct device * dev,int32_t * volt_uv)379 static int get_voltage_ok(const struct device *dev, int32_t *volt_uv)
380 {
381 	ARG_UNUSED(dev);
382 
383 	*volt_uv = 100;
384 
385 	return 0;
386 }
387 
ZTEST(regulator_api,test_get_voltage_ok)388 ZTEST(regulator_api, test_get_voltage_ok)
389 {
390 	int32_t volt_uv;
391 
392 	RESET_FAKE(regulator_fake_get_voltage);
393 
394 	regulator_fake_get_voltage_fake.custom_fake = get_voltage_ok;
395 
396 	zassert_equal(regulator_get_voltage(reg0, &volt_uv), 0);
397 	zassert_equal(volt_uv, 100);
398 	zassert_equal(regulator_fake_get_voltage_fake.call_count, 1U);
399 	zassert_equal(regulator_fake_get_voltage_fake.arg0_val, reg0);
400 	zassert_equal(regulator_fake_get_voltage_fake.arg1_val, &volt_uv);
401 }
402 
get_voltage_fail(const struct device * dev,int32_t * volt_uv)403 static int get_voltage_fail(const struct device *dev, int32_t *volt_uv)
404 {
405 	ARG_UNUSED(dev);
406 	ARG_UNUSED(volt_uv);
407 
408 	return -EIO;
409 }
410 
ZTEST(regulator_api,test_get_voltage_error)411 ZTEST(regulator_api, test_get_voltage_error)
412 {
413 	RESET_FAKE(regulator_fake_get_voltage);
414 
415 	regulator_fake_get_voltage_fake.custom_fake = get_voltage_fail;
416 
417 	zassert_equal(regulator_get_voltage(reg0, NULL), -EIO);
418 	zassert_equal(regulator_fake_get_voltage_fake.call_count, 1U);
419 	zassert_equal(regulator_fake_get_voltage_fake.arg0_val, reg0);
420 	zassert_equal(regulator_fake_get_voltage_fake.arg1_val, NULL);
421 }
422 
ZTEST(regulator_api,test_set_current_limit_not_implemented)423 ZTEST(regulator_api, test_set_current_limit_not_implemented)
424 {
425 	int ret = regulator_set_current_limit(dummy_reg, 0, 0);
426 
427 	zassert_equal(ret, -ENOSYS);
428 }
429 
ZTEST(regulator_api,test_set_current_limit_ok)430 ZTEST(regulator_api, test_set_current_limit_ok)
431 {
432 	RESET_FAKE(regulator_fake_set_current_limit);
433 
434 	regulator_fake_set_current_limit_fake.return_val = 0;
435 
436 	zassert_equal(regulator_set_current_limit(reg0, 0, 0), 0);
437 	zassert_equal(regulator_fake_set_current_limit_fake.arg0_val, reg0);
438 	zassert_equal(regulator_fake_set_current_limit_fake.arg1_val, 0);
439 	zassert_equal(regulator_fake_set_current_limit_fake.arg2_val, 0);
440 	zassert_equal(regulator_fake_set_current_limit_fake.call_count, 1U);
441 }
442 
ZTEST(regulator_api,test_set_current_limit_fail)443 ZTEST(regulator_api, test_set_current_limit_fail)
444 {
445 	RESET_FAKE(regulator_fake_set_current_limit);
446 
447 	regulator_fake_set_current_limit_fake.return_val = -EINVAL;
448 
449 	zassert_equal(regulator_set_current_limit(reg0, 0, 0), -EINVAL);
450 	zassert_equal(regulator_fake_set_current_limit_fake.arg0_val, reg0);
451 	zassert_equal(regulator_fake_set_current_limit_fake.arg1_val, 0);
452 	zassert_equal(regulator_fake_set_current_limit_fake.arg2_val, 0);
453 	zassert_equal(regulator_fake_set_current_limit_fake.call_count, 1U);
454 }
455 
ZTEST(regulator_api,test_set_current_limit_dt_limit)456 ZTEST(regulator_api, test_set_current_limit_dt_limit)
457 {
458 	RESET_FAKE(regulator_fake_set_current_limit);
459 
460 	regulator_fake_set_current_limit_fake.return_val = 0;
461 
462 	zassert_equal(regulator_set_current_limit(reg3, 300, 400), -EINVAL);
463 	zassert_equal(regulator_fake_set_current_limit_fake.call_count, 0U);
464 }
465 
ZTEST(regulator_api,test_get_current_limit_not_implemented)466 ZTEST(regulator_api, test_get_current_limit_not_implemented)
467 {
468 	int ret = regulator_get_current_limit(dummy_reg, NULL);
469 
470 	zassert_equal(ret, -ENOSYS);
471 }
472 
get_current_limit_ok(const struct device * dev,int32_t * curr_ua)473 static int get_current_limit_ok(const struct device *dev, int32_t *curr_ua)
474 {
475 	ARG_UNUSED(dev);
476 
477 	*curr_ua = 100;
478 
479 	return 0;
480 }
481 
ZTEST(regulator_api,test_get_current_limit_ok)482 ZTEST(regulator_api, test_get_current_limit_ok)
483 {
484 	int32_t curr_ua;
485 
486 	RESET_FAKE(regulator_fake_get_current_limit);
487 
488 	regulator_fake_get_current_limit_fake.custom_fake = get_current_limit_ok;
489 
490 	zassert_equal(regulator_get_current_limit(reg0, &curr_ua), 0);
491 	zassert_equal(curr_ua, 100);
492 	zassert_equal(regulator_fake_get_current_limit_fake.call_count, 1U);
493 	zassert_equal(regulator_fake_get_current_limit_fake.arg0_val, reg0);
494 	zassert_equal(regulator_fake_get_current_limit_fake.arg1_val, &curr_ua);
495 }
496 
get_current_limit_fail(const struct device * dev,int32_t * curr_ua)497 static int get_current_limit_fail(const struct device *dev, int32_t *curr_ua)
498 {
499 	ARG_UNUSED(dev);
500 	ARG_UNUSED(curr_ua);
501 
502 	return -EIO;
503 }
504 
ZTEST(regulator_api,test_get_current_limit_error)505 ZTEST(regulator_api, test_get_current_limit_error)
506 {
507 	RESET_FAKE(regulator_fake_get_current_limit);
508 
509 	regulator_fake_get_current_limit_fake.custom_fake = get_current_limit_fail;
510 
511 	zassert_equal(regulator_get_current_limit(reg0, NULL), -EIO);
512 	zassert_equal(regulator_fake_get_current_limit_fake.call_count, 1U);
513 	zassert_equal(regulator_fake_get_current_limit_fake.arg0_val, reg0);
514 	zassert_equal(regulator_fake_get_current_limit_fake.arg1_val, NULL);
515 }
516 
ZTEST(regulator_api,test_set_mode_not_implemented)517 ZTEST(regulator_api, test_set_mode_not_implemented)
518 {
519 	int ret = regulator_set_mode(dummy_reg, 0);
520 
521 	zassert_equal(ret, -ENOSYS);
522 }
523 
ZTEST(regulator_api,test_set_mode_ok)524 ZTEST(regulator_api, test_set_mode_ok)
525 {
526 	RESET_FAKE(regulator_fake_set_mode);
527 
528 	regulator_fake_set_mode_fake.return_val = 0;
529 
530 	zassert_equal(regulator_set_mode(reg0, 0), 0);
531 	zassert_equal(regulator_set_mode(reg0, 1), 0);
532 	zassert_equal(regulator_set_mode(reg0, 10), 0);
533 	zassert_equal(regulator_fake_set_mode_fake.call_count, 3U);
534 }
535 
ZTEST(regulator_api,test_set_mode_fail)536 ZTEST(regulator_api, test_set_mode_fail)
537 {
538 	RESET_FAKE(regulator_fake_set_mode);
539 
540 	regulator_fake_set_mode_fake.return_val = -ENOTSUP;
541 
542 	zassert_equal(regulator_set_mode(reg0, 0), -ENOTSUP);
543 	zassert_equal(regulator_fake_set_mode_fake.arg0_val, reg0);
544 	zassert_equal(regulator_fake_set_mode_fake.arg1_val, 0);
545 	zassert_equal(regulator_fake_set_mode_fake.call_count, 1U);
546 }
547 
ZTEST(regulator_api,test_set_mode_dt_limit)548 ZTEST(regulator_api, test_set_mode_dt_limit)
549 {
550 	RESET_FAKE(regulator_fake_set_mode);
551 
552 	regulator_fake_set_mode_fake.return_val = 0;
553 
554 	zassert_equal(regulator_set_mode(reg3, 0), -ENOTSUP);
555 	zassert_equal(regulator_set_mode(reg3, 1), 0);
556 	zassert_equal(regulator_set_mode(reg3, 10), 0);
557 	zassert_equal(regulator_fake_set_mode_fake.call_count, 2U);
558 }
559 
ZTEST(regulator_api,test_get_mode_not_implemented)560 ZTEST(regulator_api, test_get_mode_not_implemented)
561 {
562 	int ret = regulator_get_mode(dummy_reg, NULL);
563 
564 	zassert_equal(ret, -ENOSYS);
565 }
566 
ZTEST(regulator_api,test_set_active_discharge_not_implemented)567 ZTEST(regulator_api, test_set_active_discharge_not_implemented)
568 {
569 	int ret = regulator_set_active_discharge(dummy_reg, false);
570 
571 	zassert_equal(ret, -ENOSYS);
572 }
573 
get_active_discharge_ok(const struct device * dev,bool * active_discharge)574 static int get_active_discharge_ok(const struct device *dev, bool *active_discharge)
575 {
576 	ARG_UNUSED(dev);
577 
578 	*active_discharge = true;
579 
580 	return 0;
581 }
582 
ZTEST(regulator_api,test_get_active_discharge_ok)583 ZTEST(regulator_api, test_get_active_discharge_ok)
584 {
585 	bool active_discharge;
586 
587 	RESET_FAKE(regulator_fake_get_active_discharge);
588 
589 	regulator_fake_get_active_discharge_fake.custom_fake = get_active_discharge_ok;
590 
591 	zassert_equal(regulator_get_active_discharge(reg0, &active_discharge), false);
592 	zassert_equal(active_discharge, true);
593 	zassert_equal(regulator_fake_get_active_discharge_fake.call_count, 1U);
594 	zassert_equal(regulator_fake_get_active_discharge_fake.arg0_val, reg0);
595 	zassert_equal(regulator_fake_get_active_discharge_fake.arg1_val, &active_discharge);
596 }
597 
ZTEST(regulator_api,test_get_active_discharge_not_implemented)598 ZTEST(regulator_api, test_get_active_discharge_not_implemented)
599 {
600 	int ret = regulator_get_active_discharge(dummy_reg, NULL);
601 
602 	zassert_equal(ret, -ENOSYS);
603 }
604 
get_mode_ok(const struct device * dev,regulator_mode_t * mode)605 static int get_mode_ok(const struct device *dev, regulator_mode_t *mode)
606 {
607 	ARG_UNUSED(dev);
608 
609 	*mode = 10U;
610 
611 	return 0;
612 }
613 
ZTEST(regulator_api,test_get_mode_ok)614 ZTEST(regulator_api, test_get_mode_ok)
615 {
616 	regulator_mode_t mode;
617 
618 	RESET_FAKE(regulator_fake_get_mode);
619 
620 	regulator_fake_get_mode_fake.custom_fake = get_mode_ok;
621 
622 	zassert_equal(regulator_get_mode(reg0, &mode), 0U);
623 	zassert_equal(mode, 10U);
624 	zassert_equal(regulator_fake_get_mode_fake.call_count, 1U);
625 	zassert_equal(regulator_fake_get_mode_fake.arg0_val, reg0);
626 	zassert_equal(regulator_fake_get_mode_fake.arg1_val, &mode);
627 }
628 
get_mode_fail(const struct device * dev,regulator_mode_t * mode)629 static int get_mode_fail(const struct device *dev, regulator_mode_t *mode)
630 {
631 	ARG_UNUSED(dev);
632 	ARG_UNUSED(mode);
633 
634 	return -EIO;
635 }
636 
ZTEST(regulator_api,test_get_mode_error)637 ZTEST(regulator_api, test_get_mode_error)
638 {
639 	RESET_FAKE(regulator_fake_get_mode);
640 
641 	regulator_fake_get_mode_fake.custom_fake = get_mode_fail;
642 
643 	zassert_equal(regulator_get_mode(reg0, NULL), -EIO);
644 	zassert_equal(regulator_fake_get_mode_fake.call_count, 1U);
645 	zassert_equal(regulator_fake_get_mode_fake.arg0_val, reg0);
646 	zassert_equal(regulator_fake_get_mode_fake.arg1_val, NULL);
647 }
648 
ZTEST(regulator_api,test_get_error_flags_not_implemented)649 ZTEST(regulator_api, test_get_error_flags_not_implemented)
650 {
651 	int ret = regulator_get_error_flags(dummy_reg, NULL);
652 
653 	zassert_equal(ret, -ENOSYS);
654 }
655 
get_error_flags_ok(const struct device * dev,regulator_error_flags_t * flags)656 static int get_error_flags_ok(const struct device *dev, regulator_error_flags_t *flags)
657 {
658 	ARG_UNUSED(dev);
659 
660 	*flags = REGULATOR_ERROR_OVER_CURRENT;
661 
662 	return 0;
663 }
664 
ZTEST(regulator_api,test_get_error_flags_ok)665 ZTEST(regulator_api, test_get_error_flags_ok)
666 {
667 	regulator_error_flags_t flags;
668 
669 	RESET_FAKE(regulator_fake_get_error_flags);
670 
671 	regulator_fake_get_error_flags_fake.custom_fake = get_error_flags_ok;
672 
673 	zassert_equal(regulator_get_error_flags(reg0, &flags), 0);
674 	zassert_equal(flags, REGULATOR_ERROR_OVER_CURRENT);
675 	zassert_equal(regulator_fake_get_error_flags_fake.call_count, 1U);
676 	zassert_equal(regulator_fake_get_error_flags_fake.arg0_val, reg0);
677 	zassert_equal(regulator_fake_get_error_flags_fake.arg1_val, &flags);
678 }
679 
get_error_flags_fail(const struct device * dev,regulator_error_flags_t * flags)680 static int get_error_flags_fail(const struct device *dev, regulator_error_flags_t *flags)
681 {
682 	ARG_UNUSED(dev);
683 	ARG_UNUSED(flags);
684 
685 	return -EIO;
686 }
687 
ZTEST(regulator_api,test_get_max_voltage)688 ZTEST(regulator_api, test_get_max_voltage)
689 {
690 	int32_t max_uv = 0;
691 	int err = 0;
692 
693 	err = regulator_common_get_max_voltage(reg0, &max_uv);
694 	zassert_equal(err, -ENOENT);
695 
696 	err = regulator_common_get_max_voltage(reg3, &max_uv);
697 	zassert_equal(err, 0);
698 	zassert_equal(max_uv, 200);
699 }
700 
ZTEST(regulator_api,test_get_min_voltage)701 ZTEST(regulator_api, test_get_min_voltage)
702 {
703 	int32_t min_uv = 0;
704 	int err = 0;
705 
706 	err = regulator_common_get_min_voltage(reg0, &min_uv);
707 	zassert_equal(err, -ENOENT);
708 
709 	err = regulator_common_get_min_voltage(reg3, &min_uv);
710 	zassert_equal(err, 0);
711 	zassert_equal(min_uv, 100);
712 }
713 
ZTEST(regulator_api,test_get_error_flags_error)714 ZTEST(regulator_api, test_get_error_flags_error)
715 {
716 	RESET_FAKE(regulator_fake_get_error_flags);
717 
718 	regulator_fake_get_error_flags_fake.custom_fake = get_error_flags_fail;
719 
720 	zassert_equal(regulator_get_error_flags(reg0, NULL), -EIO);
721 	zassert_equal(regulator_fake_get_error_flags_fake.call_count, 1U);
722 	zassert_equal(regulator_fake_get_error_flags_fake.arg0_val, reg0);
723 	zassert_equal(regulator_fake_get_error_flags_fake.arg1_val, NULL);
724 }
725 
setup(void)726 void *setup(void)
727 {
728 	zassert_true(device_is_ready(parent));
729 	zassert_true(device_is_ready(reg0));
730 	zassert_true(device_is_ready(reg1));
731 	zassert_true(device_is_ready(reg2));
732 	zassert_true(device_is_ready(reg3));
733 	zassert_true(device_is_ready(reg4));
734 	zassert_true(device_is_ready(reg5));
735 
736 	/* REG1, REG2 initialized at init time (always-on/boot-on) */
737 	zassert_equal(regulator_fake_enable_fake.call_count, 2U);
738 	zassert_true(regulator_is_enabled(reg1));
739 	zassert_true(regulator_is_enabled(reg2));
740 
741 	/* REG3 mode set at init time (initial-mode) */
742 	zassert_equal(regulator_fake_set_mode_fake.call_count, 1U);
743 
744 	/* REG4 already disabled at init time (boot-off) */
745 	zassert_false(regulator_is_enabled(reg4));
746 
747 	/* REG5 explicitly disabled at init time (boot-off) */
748 	zassert_equal(regulator_fake_disable_fake.call_count, 1U);
749 	zassert_false(regulator_is_enabled(reg5));
750 
751 	return NULL;
752 }
753 
754 ZTEST_SUITE(regulator_api, NULL, setup, NULL, NULL, NULL);
755