blob: e46b41674d90392f17ca56ed7e7f4a9ea919bb19 [file] [log] [blame]
Simon Glass3c43fba2015-07-06 12:54:34 -06001/*
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
Simon Glass3c43fba2015-07-06 12:54:34 -060014/* Base test of the led uclass */
15static 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}
27DM_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 */
30static 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));
Simon Glassddae9fc2017-04-10 11:34:54 -060042 ut_assertok(led_set_state(dev, LEDST_ON));
Simon Glass3c43fba2015-07-06 12:54:34 -060043 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
Simon Glass8f4b6122017-04-10 11:34:55 -060044 ut_asserteq(LEDST_ON, led_get_state(dev));
Simon Glassddae9fc2017-04-10 11:34:54 -060045
46 ut_assertok(led_set_state(dev, LEDST_OFF));
Simon Glass3c43fba2015-07-06 12:54:34 -060047 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glass8f4b6122017-04-10 11:34:55 -060048 ut_asserteq(LEDST_OFF, led_get_state(dev));
Simon Glass3c43fba2015-07-06 12:54:34 -060049
50 return 0;
51}
52DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
53
Simon Glass9413ad42017-04-10 11:34:56 -060054/* Test that we can toggle LEDs */
55static 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}
77DM_TEST(dm_test_led_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
78
Simon Glass3c43fba2015-07-06 12:54:34 -060079/* Test obtaining an LED by label */
80static 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}
98DM_TEST(dm_test_led_label, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass53378da2017-04-10 11:34:57 -060099
100/* Test LED blinking */
101#ifdef CONFIG_LED_BLINK
102static 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}
121DM_TEST(dm_test_led_blink, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
122#endif