summaryrefslogtreecommitdiff
path: root/test/dm/led.c
blob: 36652c2833a996877a833afc391f24878ab678ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015 Google, Inc
 */

#include <dm.h>
#include <led.h>
#include <asm/gpio.h>
#include <dm/test.h>
#include <test/test.h>
#include <test/ut.h>

/* Base test of the led uclass */
static int dm_test_led_base(struct unit_test_state *uts)
{
	struct udevice *dev;

	/* Get the top-level device */
	ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 2, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 4, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 5, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 6, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 7, &dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 8, &dev));
	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 9, &dev));

	return 0;
}
DM_TEST(dm_test_led_base, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test of the LED 'default-state' device tree property */
static int dm_test_led_default_state(struct unit_test_state *uts)
{
	struct udevice *dev;

	/* Check that we handle the default-state property correctly. */
	ut_assertok(led_get_by_label("sandbox:default_on", &dev));
	ut_asserteq(LEDST_ON, led_get_state(dev));

	/* Also tests default label behaviour */
	ut_assertok(led_get_by_label("default_off", &dev));
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_default_state, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test of the led uclass using the led_gpio driver */
static int dm_test_led_gpio(struct unit_test_state *uts)
{
	const int offset = 1;
	struct udevice *dev, *gpio;

	/*
	 * Check that we can manipulate an LED. LED 0 is connected to GPIO
	 * bank gpio_a, offset 1.
	 */
	ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
	ut_assertok(led_set_state(dev, LEDST_ON));
	ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
	ut_asserteq(LEDST_ON, led_get_state(dev));

	ut_assertok(led_set_state(dev, LEDST_OFF));
	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_gpio, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test that we can toggle LEDs */
static int dm_test_led_toggle(struct unit_test_state *uts)
{
	const int offset = 1;
	struct udevice *dev, *gpio;

	/*
	 * Check that we can manipulate an LED. LED 0 is connected to GPIO
	 * bank gpio_a, offset 1.
	 */
	ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
	ut_assertok(led_set_state(dev, LEDST_TOGGLE));
	ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
	ut_asserteq(LEDST_ON, led_get_state(dev));

	ut_assertok(led_set_state(dev, LEDST_TOGGLE));
	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_toggle, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test obtaining an LED by label */
static int dm_test_led_label(struct unit_test_state *uts)
{
	struct udevice *dev, *cmp;

	ut_assertok(led_get_by_label("sandbox:red", &dev));
	ut_asserteq(1, device_active(dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 0, &cmp));
	ut_asserteq_ptr(dev, cmp);

	ut_assertok(led_get_by_label("sandbox:green", &dev));
	ut_asserteq(1, device_active(dev));
	ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
	ut_asserteq_ptr(dev, cmp);

	ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));

	/* Test if function, color and function-enumerator naming works */
	ut_assertok(led_get_by_label("red:status-20", &dev));

	/* Test if function, color naming works */
	ut_assertok(led_get_by_label("green:status", &dev));

	/* Test if function, without color naming works */
	ut_assertok(led_get_by_label(":status", &dev));

	/* Test if color without function naming works */
	ut_assertok(led_get_by_label("green:", &dev));

	/* Test if function, color naming is ignored if label is found */
	ut_assertok(led_get_by_label("sandbox:function", &dev));

	return 0;
}
DM_TEST(dm_test_led_label, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test LED blinking */
#ifdef CONFIG_LED_BLINK
static int dm_test_led_blink(struct unit_test_state *uts)
{
	const int offset = 1;
	struct udevice *dev, *gpio;

	/*
	 * Check that we get an error when trying to blink an LED, since it is
	 * not supported by the GPIO LED driver.
	 */
	ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
	ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
	ut_asserteq(LEDST_OFF, led_get_state(dev));
	ut_asserteq(-ENOSYS, led_set_period(dev, 100));

	return 0;
}
DM_TEST(dm_test_led_blink, UTF_SCAN_PDATA | UTF_SCAN_FDT);
#endif

/* Test LED boot */
#ifdef CONFIG_LED_BOOT
static int dm_test_led_boot(struct unit_test_state *uts)
{
	struct udevice *dev

	/* options/u-boot/boot-led is set to phandle to "sandbox:green" */
	ut_assertok(led_get_by_label("sandbox:green", &dev));
	ut_asserteq(LEDST_OFF, led_get_state(dev));
	ut_assertok(led_boot_on());
	ut_asserteq(LEDST_ON, led_get_state(dev));
	ut_assertok(led_boot_off());
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_boot, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test LED boot blink fallback */
#ifndef CONFIG_LED_BLINK
static int dm_test_led_boot_blink(struct unit_test_state *uts)
{
	struct udevice *dev

	/* options/u-boot/boot-led is set to phandle to "sandbox:green" */
	ut_assertok(led_get_by_label("sandbox:green", &dev));
	ut_asserteq(LEDST_OFF, led_get_state(dev));
	ut_assertok(led_boot_blink());
	ut_asserteq(LEDST_ON, led_get_state(dev));
	ut_assertok(led_boot_off());
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_boot_blink, UTF_SCAN_PDATA | UTF_SCAN_FDT);
#endif
#endif

/* Test LED activity */
#ifdef CONFIG_LED_ACTIVITY
static int dm_test_led_activity(struct unit_test_state *uts)
{
	struct udevice *dev

	/* options/u-boot/activity-led is set to phandle to "sandbox:red" */
	ut_assertok(led_get_by_label("sandbox:red", &dev));
	ut_asserteq(LEDST_OFF, led_get_state(dev));
	ut_assertok(led_activity_on());
	ut_asserteq(LEDST_ON, led_get_state(dev));
	ut_assertok(led_activity_off());
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_activity, UTF_SCAN_PDATA | UTF_SCAN_FDT);

/* Test LED activity blink fallback */
#ifndef CONFIG_LED_BLINK
static int dm_test_led_activityt_blink(struct unit_test_state *uts)
{
	struct udevice *dev

	/* options/u-boot/activity-led is set to phandle to "sandbox:red" */
	ut_assertok(led_get_by_label("sandbox:red", &dev));
	ut_asserteq(LEDST_OFF, led_get_state(dev));
	ut_assertok(led_activity_blink());
	ut_asserteq(LEDST_ON, led_get_state(dev));
	ut_assertok(led_activity_off());
	ut_asserteq(LEDST_OFF, led_get_state(dev));

	return 0;
}
DM_TEST(dm_test_led_activityt_blink, UTF_SCAN_PDATA | UTF_SCAN_FDT);
#endif
#endif