blob: c28fa044f459777fb371d2e1e5c951992b6e2064 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd783eb32015-07-06 12:54:34 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glassd783eb32015-07-06 12:54:34 -06004 */
5
Simon Glassd783eb32015-07-06 12:54:34 -06006#include <dm.h>
7#include <led.h>
8#include <asm/gpio.h>
9#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060010#include <test/test.h>
Simon Glassd783eb32015-07-06 12:54:34 -060011#include <test/ut.h>
12
Simon Glassd783eb32015-07-06 12:54:34 -060013/* Base test of the led uclass */
14static int dm_test_led_base(struct unit_test_state *uts)
15{
16 struct udevice *dev;
17
18 /* Get the top-level device */
19 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
20 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
21 ut_assertok(uclass_get_device(UCLASS_LED, 2, &dev));
Patrick Bruennb58adfe2018-04-11 11:16:29 +020022 ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev));
Marek Vasut83bbd0a2022-04-25 18:33:50 +020023 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 4, &dev));
Simon Glassd783eb32015-07-06 12:54:34 -060024
25 return 0;
26}
Simon Glass974dccd2020-07-28 19:41:12 -060027DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd783eb32015-07-06 12:54:34 -060028
Patrick Bruennb58adfe2018-04-11 11:16:29 +020029/* Test of the LED 'default-state' device tree property */
30static int dm_test_led_default_state(struct unit_test_state *uts)
31{
32 struct udevice *dev;
33
34 /* Check that we handle the default-state property correctly. */
35 ut_assertok(led_get_by_label("sandbox:default_on", &dev));
36 ut_asserteq(LEDST_ON, led_get_state(dev));
37
Sean Andersonfbf8d652020-09-14 11:02:03 -040038 /* Also tests default label behaviour */
39 ut_assertok(led_get_by_label("default_off", &dev));
Patrick Bruennb58adfe2018-04-11 11:16:29 +020040 ut_asserteq(LEDST_OFF, led_get_state(dev));
41
42 return 0;
43}
Simon Glass974dccd2020-07-28 19:41:12 -060044DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Patrick Bruennb58adfe2018-04-11 11:16:29 +020045
Simon Glassd783eb32015-07-06 12:54:34 -060046/* Test of the led uclass using the led_gpio driver */
47static int dm_test_led_gpio(struct unit_test_state *uts)
48{
49 const int offset = 1;
50 struct udevice *dev, *gpio;
51
52 /*
Marek Vasut83bbd0a2022-04-25 18:33:50 +020053 * Check that we can manipulate an LED. LED 0 is connected to GPIO
Simon Glassd783eb32015-07-06 12:54:34 -060054 * bank gpio_a, offset 1.
55 */
Marek Vasut83bbd0a2022-04-25 18:33:50 +020056 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
Simon Glassd783eb32015-07-06 12:54:34 -060057 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
58 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glass6ca19772017-04-10 11:34:54 -060059 ut_assertok(led_set_state(dev, LEDST_ON));
Simon Glassd783eb32015-07-06 12:54:34 -060060 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
Simon Glassdc531662017-04-10 11:34:55 -060061 ut_asserteq(LEDST_ON, led_get_state(dev));
Simon Glass6ca19772017-04-10 11:34:54 -060062
63 ut_assertok(led_set_state(dev, LEDST_OFF));
Simon Glassd783eb32015-07-06 12:54:34 -060064 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glassdc531662017-04-10 11:34:55 -060065 ut_asserteq(LEDST_OFF, led_get_state(dev));
Simon Glassd783eb32015-07-06 12:54:34 -060066
67 return 0;
68}
Simon Glass974dccd2020-07-28 19:41:12 -060069DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd783eb32015-07-06 12:54:34 -060070
Simon Glasse445dfd2017-04-10 11:34:56 -060071/* Test that we can toggle LEDs */
72static int dm_test_led_toggle(struct unit_test_state *uts)
73{
74 const int offset = 1;
75 struct udevice *dev, *gpio;
76
77 /*
Marek Vasut83bbd0a2022-04-25 18:33:50 +020078 * Check that we can manipulate an LED. LED 0 is connected to GPIO
Simon Glasse445dfd2017-04-10 11:34:56 -060079 * bank gpio_a, offset 1.
80 */
Marek Vasut83bbd0a2022-04-25 18:33:50 +020081 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
Simon Glasse445dfd2017-04-10 11:34:56 -060082 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
83 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
84 ut_assertok(led_set_state(dev, LEDST_TOGGLE));
85 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
86 ut_asserteq(LEDST_ON, led_get_state(dev));
87
88 ut_assertok(led_set_state(dev, LEDST_TOGGLE));
89 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
90 ut_asserteq(LEDST_OFF, led_get_state(dev));
91
92 return 0;
93}
Simon Glass974dccd2020-07-28 19:41:12 -060094DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse445dfd2017-04-10 11:34:56 -060095
Simon Glassd783eb32015-07-06 12:54:34 -060096/* Test obtaining an LED by label */
97static int dm_test_led_label(struct unit_test_state *uts)
98{
99 struct udevice *dev, *cmp;
100
101 ut_assertok(led_get_by_label("sandbox:red", &dev));
102 ut_asserteq(1, device_active(dev));
Marek Vasut83bbd0a2022-04-25 18:33:50 +0200103 ut_assertok(uclass_get_device(UCLASS_LED, 0, &cmp));
Simon Glassd783eb32015-07-06 12:54:34 -0600104 ut_asserteq_ptr(dev, cmp);
105
106 ut_assertok(led_get_by_label("sandbox:green", &dev));
107 ut_asserteq(1, device_active(dev));
Marek Vasut83bbd0a2022-04-25 18:33:50 +0200108 ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
Simon Glassd783eb32015-07-06 12:54:34 -0600109 ut_asserteq_ptr(dev, cmp);
110
111 ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
112
113 return 0;
114}
Simon Glass974dccd2020-07-28 19:41:12 -0600115DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3bd0c462017-04-10 11:34:57 -0600116
117/* Test LED blinking */
118#ifdef CONFIG_LED_BLINK
119static int dm_test_led_blink(struct unit_test_state *uts)
120{
121 const int offset = 1;
122 struct udevice *dev, *gpio;
123
124 /*
125 * Check that we get an error when trying to blink an LED, since it is
126 * not supported by the GPIO LED driver.
127 */
Marek Vasut83bbd0a2022-04-25 18:33:50 +0200128 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
Simon Glass3bd0c462017-04-10 11:34:57 -0600129 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
130 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
131 ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
132 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
133 ut_asserteq(LEDST_OFF, led_get_state(dev));
134 ut_asserteq(-ENOSYS, led_set_period(dev, 100));
135
136 return 0;
137}
Simon Glass974dccd2020-07-28 19:41:12 -0600138DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3bd0c462017-04-10 11:34:57 -0600139#endif