xref: /openbmc/u-boot/test/dm/spmi.c (revision d0dd7397)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
4  */
5 
6 #include <common.h>
7 #include <fdtdec.h>
8 #include <dm.h>
9 #include <dm/device.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/util.h>
13 #include <power/pmic.h>
14 #include <spmi/spmi.h>
15 #include <asm/gpio.h>
16 #include <test/ut.h>
17 
18 /* Test if bus childs got probed propperly*/
19 static int dm_test_spmi_probe(struct unit_test_state *uts)
20 {
21 	const char *name = "spmi@0";
22 	struct udevice *bus, *dev;
23 
24 	ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus));
25 
26 	/* Check bus name */
27 	ut_asserteq_str(name, bus->name);
28 
29 	/* Check that it has some devices */
30 	ut_asserteq(device_has_children(bus), true);
31 
32 	ut_assertok(device_find_first_child(bus, &dev));
33 
34 	/* There should be at least one child */
35 	ut_assertnonnull(dev);
36 
37 	/* Check that only PMICs are connected to the bus */
38 	while (dev) {
39 		ut_asserteq(device_get_uclass_id(dev), UCLASS_PMIC);
40 		device_find_next_child(&dev);
41 	}
42 
43 	return 0;
44 }
45 DM_TEST(dm_test_spmi_probe, DM_TESTF_SCAN_FDT);
46 
47 /* Test if it's possible to read bus directly and indirectly */
48 static int dm_test_spmi_access(struct unit_test_state *uts)
49 {
50 	const char *pmic_name = "pm8916@0";
51 	struct udevice *bus, *pmic;
52 
53 	ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus));
54 
55 	ut_assertok(device_get_child(bus, 0, &pmic));
56 
57 	/* Sanity check if it's proper PMIC */
58 	ut_asserteq_str(pmic_name, pmic->name);
59 
60 	/* Read PMIC ID reg using SPMI bus - it assumes it has slaveID == 0*/
61 	ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x4), 0x10);
62 	ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x5), 0x5);
63 
64 	/* Read ID reg via pmic interface */
65 	ut_asserteq(pmic_reg_read(pmic, 0xC004), 0x10);
66 	ut_asserteq(pmic_reg_read(pmic, 0xC005), 0x5);
67 
68 	return 0;
69 }
70 DM_TEST(dm_test_spmi_access, DM_TESTF_SCAN_FDT);
71 
72 
73 /* Test if it's possible to access GPIO that should be in pmic */
74 static int dm_test_spmi_access_peripheral(struct unit_test_state *uts)
75 {
76 	struct udevice *dev;
77 	unsigned int offset, gpio;
78 	const char *name;
79 	int offset_count;
80 
81 	/* Get second pin of PMIC GPIO */
82 	ut_assertok(gpio_lookup_name("spmi1", &dev, &offset, &gpio));
83 
84 	/* Check if PMIC is parent */
85 	ut_asserteq(device_get_uclass_id(dev->parent), UCLASS_PMIC);
86 
87 	/* This should be second gpio */
88 	ut_asserteq(1, offset);
89 
90 	name = gpio_get_bank_info(dev, &offset_count);
91 
92 	/* Check bank name */
93 	ut_asserteq_str("spmi", name);
94 	/* Check pin count */
95 	ut_asserteq(4, offset_count);
96 
97 	ut_assertok(gpio_request(gpio, "testing"));
98 
99 	/* Try to set/clear gpio */
100 	ut_assertok(gpio_direction_output(gpio, 0));
101 	ut_asserteq(gpio_get_value(gpio), 0);
102 	ut_assertok(gpio_direction_output(gpio, 1));
103 	ut_asserteq(gpio_get_value(gpio), 1);
104 	ut_assertok(gpio_direction_input(gpio));
105 	ut_asserteq(gpio_get_value(gpio), 1);
106 
107 	ut_assertok(gpio_free(gpio));
108 
109 	return 0;
110 }
111 DM_TEST(dm_test_spmi_access_peripheral, DM_TESTF_SCAN_FDT);
112