blob: eed3f4654c568aba8c1d9abdf4ec03f066875605 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3c43fba2015-07-06 12:54:34 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glass3c43fba2015-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 Glass0e1fad42020-07-19 10:15:37 -060011#include <test/test.h>
Simon Glass3c43fba2015-07-06 12:54:34 -060012#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));
Patrick Bruenn274fb462018-04-11 11:16:29 +020023 ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev));
Marek Vasut876276f2022-04-25 18:33:50 +020024 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 4, &dev));
Simon Glass3c43fba2015-07-06 12:54:34 -060025
26 return 0;
27}
Simon Glasse180c2b2020-07-28 19:41:12 -060028DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3c43fba2015-07-06 12:54:34 -060029
Patrick Bruenn274fb462018-04-11 11:16:29 +020030/* Test of the LED 'default-state' device tree property */
31static int dm_test_led_default_state(struct unit_test_state *uts)
32{
33 struct udevice *dev;
34
35 /* Check that we handle the default-state property correctly. */
36 ut_assertok(led_get_by_label("sandbox:default_on", &dev));
37 ut_asserteq(LEDST_ON, led_get_state(dev));
38
Sean Anderson3e41c7b2020-09-14 11:02:03 -040039 /* Also tests default label behaviour */
40 ut_assertok(led_get_by_label("default_off", &dev));
Patrick Bruenn274fb462018-04-11 11:16:29 +020041 ut_asserteq(LEDST_OFF, led_get_state(dev));
42
43 return 0;
44}
Simon Glasse180c2b2020-07-28 19:41:12 -060045DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Patrick Bruenn274fb462018-04-11 11:16:29 +020046
Simon Glass3c43fba2015-07-06 12:54:34 -060047/* Test of the led uclass using the led_gpio driver */
48static int dm_test_led_gpio(struct unit_test_state *uts)
49{
50 const int offset = 1;
51 struct udevice *dev, *gpio;
52
53 /*
Marek Vasut876276f2022-04-25 18:33:50 +020054 * Check that we can manipulate an LED. LED 0 is connected to GPIO
Simon Glass3c43fba2015-07-06 12:54:34 -060055 * bank gpio_a, offset 1.
56 */
Marek Vasut876276f2022-04-25 18:33:50 +020057 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
Simon Glass3c43fba2015-07-06 12:54:34 -060058 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
59 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glassddae9fc2017-04-10 11:34:54 -060060 ut_assertok(led_set_state(dev, LEDST_ON));
Simon Glass3c43fba2015-07-06 12:54:34 -060061 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
Simon Glass8f4b6122017-04-10 11:34:55 -060062 ut_asserteq(LEDST_ON, led_get_state(dev));
Simon Glassddae9fc2017-04-10 11:34:54 -060063
64 ut_assertok(led_set_state(dev, LEDST_OFF));
Simon Glass3c43fba2015-07-06 12:54:34 -060065 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glass8f4b6122017-04-10 11:34:55 -060066 ut_asserteq(LEDST_OFF, led_get_state(dev));
Simon Glass3c43fba2015-07-06 12:54:34 -060067
68 return 0;
69}
Simon Glasse180c2b2020-07-28 19:41:12 -060070DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3c43fba2015-07-06 12:54:34 -060071
Simon Glass9413ad42017-04-10 11:34:56 -060072/* Test that we can toggle LEDs */
73static int dm_test_led_toggle(struct unit_test_state *uts)
74{
75 const int offset = 1;
76 struct udevice *dev, *gpio;
77
78 /*
Marek Vasut876276f2022-04-25 18:33:50 +020079 * Check that we can manipulate an LED. LED 0 is connected to GPIO
Simon Glass9413ad42017-04-10 11:34:56 -060080 * bank gpio_a, offset 1.
81 */
Marek Vasut876276f2022-04-25 18:33:50 +020082 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
Simon Glass9413ad42017-04-10 11:34:56 -060083 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
84 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
85 ut_assertok(led_set_state(dev, LEDST_TOGGLE));
86 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
87 ut_asserteq(LEDST_ON, led_get_state(dev));
88
89 ut_assertok(led_set_state(dev, LEDST_TOGGLE));
90 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
91 ut_asserteq(LEDST_OFF, led_get_state(dev));
92
93 return 0;
94}
Simon Glasse180c2b2020-07-28 19:41:12 -060095DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass9413ad42017-04-10 11:34:56 -060096
Simon Glass3c43fba2015-07-06 12:54:34 -060097/* Test obtaining an LED by label */
98static int dm_test_led_label(struct unit_test_state *uts)
99{
100 struct udevice *dev, *cmp;
101
102 ut_assertok(led_get_by_label("sandbox:red", &dev));
103 ut_asserteq(1, device_active(dev));
Marek Vasut876276f2022-04-25 18:33:50 +0200104 ut_assertok(uclass_get_device(UCLASS_LED, 0, &cmp));
Simon Glass3c43fba2015-07-06 12:54:34 -0600105 ut_asserteq_ptr(dev, cmp);
106
107 ut_assertok(led_get_by_label("sandbox:green", &dev));
108 ut_asserteq(1, device_active(dev));
Marek Vasut876276f2022-04-25 18:33:50 +0200109 ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
Simon Glass3c43fba2015-07-06 12:54:34 -0600110 ut_asserteq_ptr(dev, cmp);
111
112 ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
113
114 return 0;
115}
Simon Glasse180c2b2020-07-28 19:41:12 -0600116DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass53378da2017-04-10 11:34:57 -0600117
118/* Test LED blinking */
119#ifdef CONFIG_LED_BLINK
120static int dm_test_led_blink(struct unit_test_state *uts)
121{
122 const int offset = 1;
123 struct udevice *dev, *gpio;
124
125 /*
126 * Check that we get an error when trying to blink an LED, since it is
127 * not supported by the GPIO LED driver.
128 */
Marek Vasut876276f2022-04-25 18:33:50 +0200129 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
Simon Glass53378da2017-04-10 11:34:57 -0600130 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
131 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
132 ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
133 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
134 ut_asserteq(LEDST_OFF, led_get_state(dev));
135 ut_asserteq(-ENOSYS, led_set_period(dev, 100));
136
137 return 0;
138}
Simon Glasse180c2b2020-07-28 19:41:12 -0600139DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass53378da2017-04-10 11:34:57 -0600140#endif