]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3c43fba3 SG |
2 | /* |
3 | * Copyright (C) 2015 Google, Inc | |
3c43fba3 SG |
4 | */ |
5 | ||
d678a59d | 6 | #include <common.h> |
3c43fba3 SG |
7 | #include <dm.h> |
8 | #include <led.h> | |
9 | #include <asm/gpio.h> | |
10 | #include <dm/test.h> | |
0e1fad43 | 11 | #include <test/test.h> |
3c43fba3 SG |
12 | #include <test/ut.h> |
13 | ||
3c43fba3 SG |
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)); | |
274fb461 | 23 | ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev)); |
876276f5 | 24 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 4, &dev)); |
3c43fba3 SG |
25 | |
26 | return 0; | |
27 | } | |
e180c2b1 | 28 | DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
3c43fba3 | 29 | |
274fb461 PB |
30 | /* Test of the LED 'default-state' device tree property */ |
31 | static int dm_test_led_default_state(struct unit_test_state *uts) | |
32 | { | |
33 | struct udevice *dev; | |
34 | ||
35 | /* Check that we handle the default-state property correctly. */ | |
36 | ut_assertok(led_get_by_label("sandbox:default_on", &dev)); | |
37 | ut_asserteq(LEDST_ON, led_get_state(dev)); | |
38 | ||
3e41c7b2 SA |
39 | /* Also tests default label behaviour */ |
40 | ut_assertok(led_get_by_label("default_off", &dev)); | |
274fb461 PB |
41 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
42 | ||
43 | return 0; | |
44 | } | |
e180c2b1 | 45 | DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
274fb461 | 46 | |
3c43fba3 SG |
47 | /* Test of the led uclass using the led_gpio driver */ |
48 | static int dm_test_led_gpio(struct unit_test_state *uts) | |
49 | { | |
50 | const int offset = 1; | |
51 | struct udevice *dev, *gpio; | |
52 | ||
53 | /* | |
876276f5 | 54 | * Check that we can manipulate an LED. LED 0 is connected to GPIO |
3c43fba3 SG |
55 | * bank gpio_a, offset 1. |
56 | */ | |
876276f5 | 57 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev)); |
3c43fba3 SG |
58 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
59 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); | |
ddae9fcd | 60 | ut_assertok(led_set_state(dev, LEDST_ON)); |
3c43fba3 | 61 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
8f4b6123 | 62 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
ddae9fcd SG |
63 | |
64 | ut_assertok(led_set_state(dev, LEDST_OFF)); | |
3c43fba3 | 65 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
8f4b6123 | 66 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
3c43fba3 SG |
67 | |
68 | return 0; | |
69 | } | |
e180c2b1 | 70 | DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
3c43fba3 | 71 | |
9413ad4f SG |
72 | /* Test that we can toggle LEDs */ |
73 | static int dm_test_led_toggle(struct unit_test_state *uts) | |
74 | { | |
75 | const int offset = 1; | |
76 | struct udevice *dev, *gpio; | |
77 | ||
78 | /* | |
876276f5 | 79 | * Check that we can manipulate an LED. LED 0 is connected to GPIO |
9413ad4f SG |
80 | * bank gpio_a, offset 1. |
81 | */ | |
876276f5 | 82 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev)); |
9413ad4f SG |
83 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
84 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); | |
85 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); | |
86 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); | |
87 | ut_asserteq(LEDST_ON, led_get_state(dev)); | |
88 | ||
89 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); | |
90 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); | |
91 | ut_asserteq(LEDST_OFF, led_get_state(dev)); | |
92 | ||
93 | return 0; | |
94 | } | |
e180c2b1 | 95 | DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
9413ad4f | 96 | |
3c43fba3 SG |
97 | /* Test obtaining an LED by label */ |
98 | static int dm_test_led_label(struct unit_test_state *uts) | |
99 | { | |
100 | struct udevice *dev, *cmp; | |
101 | ||
102 | ut_assertok(led_get_by_label("sandbox:red", &dev)); | |
103 | ut_asserteq(1, device_active(dev)); | |
876276f5 | 104 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &cmp)); |
3c43fba3 SG |
105 | ut_asserteq_ptr(dev, cmp); |
106 | ||
107 | ut_assertok(led_get_by_label("sandbox:green", &dev)); | |
108 | ut_asserteq(1, device_active(dev)); | |
876276f5 | 109 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp)); |
3c43fba3 SG |
110 | ut_asserteq_ptr(dev, cmp); |
111 | ||
112 | ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev)); | |
113 | ||
114 | return 0; | |
115 | } | |
e180c2b1 | 116 | DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
53378dac SG |
117 | |
118 | /* Test LED blinking */ | |
119 | #ifdef CONFIG_LED_BLINK | |
120 | static int dm_test_led_blink(struct unit_test_state *uts) | |
121 | { | |
122 | const int offset = 1; | |
123 | struct udevice *dev, *gpio; | |
124 | ||
125 | /* | |
126 | * Check that we get an error when trying to blink an LED, since it is | |
127 | * not supported by the GPIO LED driver. | |
128 | */ | |
876276f5 | 129 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev)); |
53378dac SG |
130 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
131 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); | |
132 | ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK)); | |
133 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); | |
134 | ut_asserteq(LEDST_OFF, led_get_state(dev)); | |
135 | ut_asserteq(-ENOSYS, led_set_period(dev, 100)); | |
136 | ||
137 | return 0; | |
138 | } | |
e180c2b1 | 139 | DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
53378dac | 140 | #endif |