Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <led.h> |
| 9 | #include <asm/gpio.h> |
| 10 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 11 | #include <test/test.h> |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 12 | #include <test/ut.h> |
| 13 | |
Simon Glass | 3c43fba | 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 | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 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)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 26 | |
| 27 | return 0; |
| 28 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 29 | DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 30 | |
Patrick Bruenn | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 31 | /* Test of the LED 'default-state' device tree property */ |
| 32 | static int dm_test_led_default_state(struct unit_test_state *uts) |
| 33 | { |
| 34 | struct udevice *dev; |
| 35 | |
Patrick Delaunay | 17585e2 | 2018-07-27 16:37:09 +0200 | [diff] [blame] | 36 | /* configure the default state (auto-probe) */ |
| 37 | led_default_state(); |
| 38 | |
Patrick Bruenn | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 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)); |
| 42 | |
Sean Anderson | 3e41c7b | 2020-09-14 11:02:03 -0400 | [diff] [blame] | 43 | /* Also tests default label behaviour */ |
| 44 | ut_assertok(led_get_by_label("default_off", &dev)); |
Patrick Bruenn | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 45 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 46 | |
| 47 | return 0; |
| 48 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 49 | DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Patrick Bruenn | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 50 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 51 | /* Test of the led uclass using the led_gpio driver */ |
| 52 | static int dm_test_led_gpio(struct unit_test_state *uts) |
| 53 | { |
| 54 | const int offset = 1; |
| 55 | struct udevice *dev, *gpio; |
| 56 | |
| 57 | /* |
| 58 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 59 | * bank gpio_a, offset 1. |
| 60 | */ |
| 61 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 62 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 63 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 64 | ut_assertok(led_set_state(dev, LEDST_ON)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 65 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 66 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 67 | |
| 68 | ut_assertok(led_set_state(dev, LEDST_OFF)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 69 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 70 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 74 | DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 75 | |
Simon Glass | 9413ad4 | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 76 | /* Test that we can toggle LEDs */ |
| 77 | static int dm_test_led_toggle(struct unit_test_state *uts) |
| 78 | { |
| 79 | const int offset = 1; |
| 80 | struct udevice *dev, *gpio; |
| 81 | |
| 82 | /* |
| 83 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 84 | * bank gpio_a, offset 1. |
| 85 | */ |
| 86 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 87 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 88 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 89 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); |
| 90 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
| 91 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
| 92 | |
| 93 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); |
| 94 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 95 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 96 | |
| 97 | return 0; |
| 98 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 99 | DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 9413ad4 | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 100 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 101 | /* Test obtaining an LED by label */ |
| 102 | static int dm_test_led_label(struct unit_test_state *uts) |
| 103 | { |
| 104 | struct udevice *dev, *cmp; |
| 105 | |
| 106 | ut_assertok(led_get_by_label("sandbox:red", &dev)); |
| 107 | ut_asserteq(1, device_active(dev)); |
| 108 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp)); |
| 109 | ut_asserteq_ptr(dev, cmp); |
| 110 | |
| 111 | ut_assertok(led_get_by_label("sandbox:green", &dev)); |
| 112 | ut_asserteq(1, device_active(dev)); |
| 113 | ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp)); |
| 114 | ut_asserteq_ptr(dev, cmp); |
| 115 | |
| 116 | ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev)); |
| 117 | |
| 118 | return 0; |
| 119 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 120 | DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 53378da | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 121 | |
| 122 | /* Test LED blinking */ |
| 123 | #ifdef CONFIG_LED_BLINK |
| 124 | static int dm_test_led_blink(struct unit_test_state *uts) |
| 125 | { |
| 126 | const int offset = 1; |
| 127 | struct udevice *dev, *gpio; |
| 128 | |
| 129 | /* |
| 130 | * Check that we get an error when trying to blink an LED, since it is |
| 131 | * not supported by the GPIO LED driver. |
| 132 | */ |
| 133 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 134 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 135 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 136 | ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK)); |
| 137 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 138 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 139 | ut_asserteq(-ENOSYS, led_set_period(dev, 100)); |
| 140 | |
| 141 | return 0; |
| 142 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 143 | DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 53378da | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 144 | #endif |