1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
11 #include <test/test.h>
14 /* Base test of the led uclass */
15 static int dm_test_led_base(struct unit_test_state *uts)
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_assertok(uclass_get_device(UCLASS_LED, 3, &dev));
24 ut_assertok(uclass_get_device(UCLASS_LED, 4, &dev));
25 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 5, &dev));
29 DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
31 /* Test of the LED 'default-state' device tree property */
32 static int dm_test_led_default_state(struct unit_test_state *uts)
36 /* configure the default state (auto-probe) */
39 /* Check that we handle the default-state property correctly. */
40 ut_assertok(led_get_by_label("sandbox:default_on", &dev));
41 ut_asserteq(LEDST_ON, led_get_state(dev));
43 ut_assertok(led_get_by_label("sandbox:default_off", &dev));
44 ut_asserteq(LEDST_OFF, led_get_state(dev));
48 DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
50 /* Test of the led uclass using the led_gpio driver */
51 static int dm_test_led_gpio(struct unit_test_state *uts)
54 struct udevice *dev, *gpio;
57 * Check that we can manipulate an LED. LED 1 is connected to GPIO
58 * bank gpio_a, offset 1.
60 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
61 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
62 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
63 ut_assertok(led_set_state(dev, LEDST_ON));
64 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
65 ut_asserteq(LEDST_ON, led_get_state(dev));
67 ut_assertok(led_set_state(dev, LEDST_OFF));
68 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
69 ut_asserteq(LEDST_OFF, led_get_state(dev));
73 DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
75 /* Test that we can toggle LEDs */
76 static int dm_test_led_toggle(struct unit_test_state *uts)
79 struct udevice *dev, *gpio;
82 * Check that we can manipulate an LED. LED 1 is connected to GPIO
83 * bank gpio_a, offset 1.
85 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
86 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
87 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
88 ut_assertok(led_set_state(dev, LEDST_TOGGLE));
89 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
90 ut_asserteq(LEDST_ON, led_get_state(dev));
92 ut_assertok(led_set_state(dev, LEDST_TOGGLE));
93 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
94 ut_asserteq(LEDST_OFF, led_get_state(dev));
98 DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
100 /* Test obtaining an LED by label */
101 static int dm_test_led_label(struct unit_test_state *uts)
103 struct udevice *dev, *cmp;
105 ut_assertok(led_get_by_label("sandbox:red", &dev));
106 ut_asserteq(1, device_active(dev));
107 ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
108 ut_asserteq_ptr(dev, cmp);
110 ut_assertok(led_get_by_label("sandbox:green", &dev));
111 ut_asserteq(1, device_active(dev));
112 ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp));
113 ut_asserteq_ptr(dev, cmp);
115 ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
119 DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
121 /* Test LED blinking */
122 #ifdef CONFIG_LED_BLINK
123 static int dm_test_led_blink(struct unit_test_state *uts)
125 const int offset = 1;
126 struct udevice *dev, *gpio;
129 * Check that we get an error when trying to blink an LED, since it is
130 * not supported by the GPIO LED driver.
132 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
133 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
134 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
135 ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
136 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
137 ut_asserteq(LEDST_OFF, led_get_state(dev));
138 ut_asserteq(-ENOSYS, led_set_period(dev, 100));
142 DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);