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> |
| 11 | #include <test/ut.h> |
| 12 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 13 | /* Base test of the led uclass */ |
| 14 | static 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 Bruenn | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 22 | ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev)); |
| 23 | ut_assertok(uclass_get_device(UCLASS_LED, 4, &dev)); |
| 24 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 5, &dev)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 25 | |
| 26 | return 0; |
| 27 | } |
| 28 | DM_TEST(dm_test_led_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 29 | |
Patrick Bruenn | 274fb46 | 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 | |
| 39 | ut_assertok(led_get_by_label("sandbox:default_off", &dev)); |
| 40 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | DM_TEST(dm_test_led_default_state, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 45 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 46 | /* Test of the led uclass using the led_gpio driver */ |
| 47 | static int dm_test_led_gpio(struct unit_test_state *uts) |
| 48 | { |
| 49 | const int offset = 1; |
| 50 | struct udevice *dev, *gpio; |
| 51 | |
| 52 | /* |
| 53 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 54 | * bank gpio_a, offset 1. |
| 55 | */ |
| 56 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 57 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 58 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 59 | ut_assertok(led_set_state(dev, LEDST_ON)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 60 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 61 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 62 | |
| 63 | ut_assertok(led_set_state(dev, LEDST_OFF)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 64 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 65 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 70 | |
Simon Glass | 9413ad4 | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 71 | /* Test that we can toggle LEDs */ |
| 72 | static int dm_test_led_toggle(struct unit_test_state *uts) |
| 73 | { |
| 74 | const int offset = 1; |
| 75 | struct udevice *dev, *gpio; |
| 76 | |
| 77 | /* |
| 78 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 79 | * bank gpio_a, offset 1. |
| 80 | */ |
| 81 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 82 | 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 | } |
| 94 | DM_TEST(dm_test_led_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 95 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 96 | /* Test obtaining an LED by label */ |
| 97 | static 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)); |
| 103 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp)); |
| 104 | ut_asserteq_ptr(dev, cmp); |
| 105 | |
| 106 | ut_assertok(led_get_by_label("sandbox:green", &dev)); |
| 107 | ut_asserteq(1, device_active(dev)); |
| 108 | ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp)); |
| 109 | ut_asserteq_ptr(dev, cmp); |
| 110 | |
| 111 | ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev)); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | DM_TEST(dm_test_led_label, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 53378da | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 116 | |
| 117 | /* Test LED blinking */ |
| 118 | #ifdef CONFIG_LED_BLINK |
| 119 | static 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 | */ |
| 128 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 129 | 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 | } |
| 138 | DM_TEST(dm_test_led_blink, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 139 | #endif |