xref: /openbmc/u-boot/test/dm/led.c (revision 7ffce4f1)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <led.h>
10 #include <asm/gpio.h>
11 #include <dm/test.h>
12 #include <test/ut.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 /* Base test of the led uclass */
17 static int dm_test_led_base(struct unit_test_state *uts)
18 {
19 	struct udevice *dev;
20 
21 	/* Get the top-level device */
22 	ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
23 	ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
24 	ut_assertok(uclass_get_device(UCLASS_LED, 2, &dev));
25 	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 3, &dev));
26 
27 	return 0;
28 }
29 DM_TEST(dm_test_led_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
30 
31 /* Test of the led uclass using the led_gpio driver */
32 static int dm_test_led_gpio(struct unit_test_state *uts)
33 {
34 	const int offset = 1;
35 	struct udevice *dev, *gpio;
36 
37 	/*
38 	 * Check that we can manipulate an LED. LED 1 is connected to GPIO
39 	 * bank gpio_a, offset 1.
40 	 */
41 	ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
42 	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
43 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
44 	ut_assertok(led_set_state(dev, LEDST_ON));
45 	ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
46 	ut_asserteq(LEDST_ON, led_get_state(dev));
47 
48 	ut_assertok(led_set_state(dev, LEDST_OFF));
49 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
50 	ut_asserteq(LEDST_OFF, led_get_state(dev));
51 
52 	return 0;
53 }
54 DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
55 
56 /* Test that we can toggle LEDs */
57 static int dm_test_led_toggle(struct unit_test_state *uts)
58 {
59 	const int offset = 1;
60 	struct udevice *dev, *gpio;
61 
62 	/*
63 	 * Check that we can manipulate an LED. LED 1 is connected to GPIO
64 	 * bank gpio_a, offset 1.
65 	 */
66 	ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
67 	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
68 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
69 	ut_assertok(led_set_state(dev, LEDST_TOGGLE));
70 	ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
71 	ut_asserteq(LEDST_ON, led_get_state(dev));
72 
73 	ut_assertok(led_set_state(dev, LEDST_TOGGLE));
74 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
75 	ut_asserteq(LEDST_OFF, led_get_state(dev));
76 
77 	return 0;
78 }
79 DM_TEST(dm_test_led_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
80 
81 /* Test obtaining an LED by label */
82 static int dm_test_led_label(struct unit_test_state *uts)
83 {
84 	struct udevice *dev, *cmp;
85 
86 	ut_assertok(led_get_by_label("sandbox:red", &dev));
87 	ut_asserteq(1, device_active(dev));
88 	ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
89 	ut_asserteq_ptr(dev, cmp);
90 
91 	ut_assertok(led_get_by_label("sandbox:green", &dev));
92 	ut_asserteq(1, device_active(dev));
93 	ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp));
94 	ut_asserteq_ptr(dev, cmp);
95 
96 	ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
97 
98 	return 0;
99 }
100 DM_TEST(dm_test_led_label, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
101 
102 /* Test LED blinking */
103 #ifdef CONFIG_LED_BLINK
104 static int dm_test_led_blink(struct unit_test_state *uts)
105 {
106 	const int offset = 1;
107 	struct udevice *dev, *gpio;
108 
109 	/*
110 	 * Check that we get an error when trying to blink an LED, since it is
111 	 * not supported by the GPIO LED driver.
112 	 */
113 	ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
114 	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
115 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
116 	ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
117 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
118 	ut_asserteq(LEDST_OFF, led_get_state(dev));
119 	ut_asserteq(-ENOSYS, led_set_period(dev, 100));
120 
121 	return 0;
122 }
123 DM_TEST(dm_test_led_blink, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
124 #endif
125