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