Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame^] | 6 | #include <common.h> |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <led.h> |
| 9 | #include <asm/gpio.h> |
| 10 | #include <dm/test.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 11 | #include <test/test.h> |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 12 | #include <test/ut.h> |
| 13 | |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 14 | /* Base test of the led uclass */ |
| 15 | static 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 Bruenn | b58adfe | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 23 | ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev)); |
Marek Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 24 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 4, &dev)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 25 | |
| 26 | return 0; |
| 27 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 28 | DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 29 | |
Patrick Bruenn | b58adfe | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 30 | /* Test of the LED 'default-state' device tree property */ |
| 31 | static 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 Anderson | fbf8d65 | 2020-09-14 11:02:03 -0400 | [diff] [blame] | 39 | /* Also tests default label behaviour */ |
| 40 | ut_assertok(led_get_by_label("default_off", &dev)); |
Patrick Bruenn | b58adfe | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 41 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 42 | |
| 43 | return 0; |
| 44 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 45 | DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Patrick Bruenn | b58adfe | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 46 | |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 47 | /* Test of the led uclass using the led_gpio driver */ |
| 48 | static int dm_test_led_gpio(struct unit_test_state *uts) |
| 49 | { |
| 50 | const int offset = 1; |
| 51 | struct udevice *dev, *gpio; |
| 52 | |
| 53 | /* |
Marek Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 54 | * Check that we can manipulate an LED. LED 0 is connected to GPIO |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 55 | * bank gpio_a, offset 1. |
| 56 | */ |
Marek Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 57 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 58 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 59 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 60 | ut_assertok(led_set_state(dev, LEDST_ON)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 61 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 62 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 63 | |
| 64 | ut_assertok(led_set_state(dev, LEDST_OFF)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 65 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 66 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 67 | |
| 68 | return 0; |
| 69 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 70 | DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 71 | |
Simon Glass | e445dfd | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 72 | /* Test that we can toggle LEDs */ |
| 73 | static int dm_test_led_toggle(struct unit_test_state *uts) |
| 74 | { |
| 75 | const int offset = 1; |
| 76 | struct udevice *dev, *gpio; |
| 77 | |
| 78 | /* |
Marek Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 79 | * Check that we can manipulate an LED. LED 0 is connected to GPIO |
Simon Glass | e445dfd | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 80 | * bank gpio_a, offset 1. |
| 81 | */ |
Marek Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 82 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev)); |
Simon Glass | e445dfd | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 83 | 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 95 | DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | e445dfd | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 96 | |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 97 | /* Test obtaining an LED by label */ |
| 98 | static 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 Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 104 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &cmp)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 105 | ut_asserteq_ptr(dev, cmp); |
| 106 | |
| 107 | ut_assertok(led_get_by_label("sandbox:green", &dev)); |
| 108 | ut_asserteq(1, device_active(dev)); |
Marek Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 109 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp)); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 110 | ut_asserteq_ptr(dev, cmp); |
| 111 | |
| 112 | ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev)); |
| 113 | |
| 114 | return 0; |
| 115 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 116 | DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 117 | |
| 118 | /* Test LED blinking */ |
| 119 | #ifdef CONFIG_LED_BLINK |
| 120 | static 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 Vasut | 83bbd0a | 2022-04-25 18:33:50 +0200 | [diff] [blame] | 129 | ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev)); |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 130 | 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 139 | DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 140 | #endif |