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