blob: 8b587d0a22a8b098dfd5f182a17cb0c5b37f2075 [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
6#include <common.h>
7#include <dm.h>
8#include <led.h>
9#include <asm/gpio.h>
10#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060011#include <test/test.h>
Simon Glassd783eb32015-07-06 12:54:34 -060012#include <test/ut.h>
13
Simon Glassd783eb32015-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));
Patrick Bruennb58adfe2018-04-11 11:16:29 +020023 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));
Simon Glassd783eb32015-07-06 12:54:34 -060026
27 return 0;
28}
Simon Glass974dccd2020-07-28 19:41:12 -060029DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd783eb32015-07-06 12:54:34 -060030
Patrick Bruennb58adfe2018-04-11 11:16:29 +020031/* Test of the LED 'default-state' device tree property */
32static int dm_test_led_default_state(struct unit_test_state *uts)
33{
34 struct udevice *dev;
35
Patrick Delaunay29bf6032018-07-27 16:37:09 +020036 /* configure the default state (auto-probe) */
37 led_default_state();
38
Patrick Bruennb58adfe2018-04-11 11:16:29 +020039 /* 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));
42
43 ut_assertok(led_get_by_label("sandbox:default_off", &dev));
44 ut_asserteq(LEDST_OFF, led_get_state(dev));
45
46 return 0;
47}
Simon Glass974dccd2020-07-28 19:41:12 -060048DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Patrick Bruennb58adfe2018-04-11 11:16:29 +020049
Simon Glassd783eb32015-07-06 12:54:34 -060050/* Test of the led uclass using the led_gpio driver */
51static int dm_test_led_gpio(struct unit_test_state *uts)
52{
53 const int offset = 1;
54 struct udevice *dev, *gpio;
55
56 /*
57 * Check that we can manipulate an LED. LED 1 is connected to GPIO
58 * bank gpio_a, offset 1.
59 */
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));
Simon Glass6ca19772017-04-10 11:34:54 -060063 ut_assertok(led_set_state(dev, LEDST_ON));
Simon Glassd783eb32015-07-06 12:54:34 -060064 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
Simon Glassdc531662017-04-10 11:34:55 -060065 ut_asserteq(LEDST_ON, led_get_state(dev));
Simon Glass6ca19772017-04-10 11:34:54 -060066
67 ut_assertok(led_set_state(dev, LEDST_OFF));
Simon Glassd783eb32015-07-06 12:54:34 -060068 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glassdc531662017-04-10 11:34:55 -060069 ut_asserteq(LEDST_OFF, led_get_state(dev));
Simon Glassd783eb32015-07-06 12:54:34 -060070
71 return 0;
72}
Simon Glass974dccd2020-07-28 19:41:12 -060073DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd783eb32015-07-06 12:54:34 -060074
Simon Glasse445dfd2017-04-10 11:34:56 -060075/* Test that we can toggle LEDs */
76static int dm_test_led_toggle(struct unit_test_state *uts)
77{
78 const int offset = 1;
79 struct udevice *dev, *gpio;
80
81 /*
82 * Check that we can manipulate an LED. LED 1 is connected to GPIO
83 * bank gpio_a, offset 1.
84 */
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));
91
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));
95
96 return 0;
97}
Simon Glass974dccd2020-07-28 19:41:12 -060098DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse445dfd2017-04-10 11:34:56 -060099
Simon Glassd783eb32015-07-06 12:54:34 -0600100/* Test obtaining an LED by label */
101static int dm_test_led_label(struct unit_test_state *uts)
102{
103 struct udevice *dev, *cmp;
104
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);
109
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);
114
115 ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
116
117 return 0;
118}
Simon Glass974dccd2020-07-28 19:41:12 -0600119DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3bd0c462017-04-10 11:34:57 -0600120
121/* Test LED blinking */
122#ifdef CONFIG_LED_BLINK
123static int dm_test_led_blink(struct unit_test_state *uts)
124{
125 const int offset = 1;
126 struct udevice *dev, *gpio;
127
128 /*
129 * Check that we get an error when trying to blink an LED, since it is
130 * not supported by the GPIO LED driver.
131 */
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));
139
140 return 0;
141}
Simon Glass974dccd2020-07-28 19:41:12 -0600142DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3bd0c462017-04-10 11:34:57 -0600143#endif