xref: /openbmc/u-boot/test/dm/pmic.c (revision 23ff8633)
1 /*
2  * Tests for the driver model pmic API
3  *
4  * Copyright (c) 2015 Samsung Electronics
5  * Przemyslaw Marczak <p.marczak@samsung.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <errno.h>
12 #include <dm.h>
13 #include <fdtdec.h>
14 #include <malloc.h>
15 #include <dm/device-internal.h>
16 #include <dm/root.h>
17 #include <dm/util.h>
18 #include <dm/test.h>
19 #include <dm/uclass-internal.h>
20 #include <power/pmic.h>
21 #include <power/sandbox_pmic.h>
22 #include <test/ut.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
26 /* Test PMIC get method */
27 static int dm_test_power_pmic_get(struct unit_test_state *uts)
28 {
29 	const char *name = "sandbox_pmic";
30 	struct udevice *dev;
31 
32 	ut_assertok(pmic_get(name, &dev));
33 	ut_assertnonnull(dev);
34 
35 	/* Check PMIC's name */
36 	ut_asserteq_str(name, dev->name);
37 
38 	return 0;
39 }
40 DM_TEST(dm_test_power_pmic_get, DM_TESTF_SCAN_FDT);
41 
42 /* Test PMIC I/O */
43 static int dm_test_power_pmic_io(struct unit_test_state *uts)
44 {
45 	const char *name = "sandbox_pmic";
46 	uint8_t out_buffer, in_buffer;
47 	struct udevice *dev;
48 	int reg_count, i;
49 
50 	ut_assertok(pmic_get(name, &dev));
51 
52 	reg_count = pmic_reg_count(dev);
53 	ut_asserteq(reg_count, SANDBOX_PMIC_REG_COUNT);
54 
55 	/*
56 	 * Test PMIC I/O - write and read a loop counter.
57 	 * usually we can't write to all PMIC's registers in the real hardware,
58 	 * but we can to the sandbox pmic.
59 	 */
60 	for (i = 0; i < reg_count; i++) {
61 		out_buffer = i;
62 		ut_assertok(pmic_write(dev, i, &out_buffer, 1));
63 		ut_assertok(pmic_read(dev, i, &in_buffer, 1));
64 		ut_asserteq(out_buffer, in_buffer);
65 	}
66 
67 	return 0;
68 }
69 DM_TEST(dm_test_power_pmic_io, DM_TESTF_SCAN_FDT);
70