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 | |
Patrick Delaunay | 17585e2 | 2018-07-27 16:37:09 +0200 | [diff] [blame] | 35 | /* configure the default state (auto-probe) */ |
| 36 | led_default_state(); |
| 37 | |
Patrick Bruenn | 274fb46 | 2018-04-11 11:16:29 +0200 | [diff] [blame] | 38 | /* Check that we handle the default-state property correctly. */ |
| 39 | ut_assertok(led_get_by_label("sandbox:default_on", &dev)); |
| 40 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
| 41 | |
| 42 | ut_assertok(led_get_by_label("sandbox:default_off", &dev)); |
| 43 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | DM_TEST(dm_test_led_default_state, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 48 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 49 | /* Test of the led uclass using the led_gpio driver */ |
| 50 | static int dm_test_led_gpio(struct unit_test_state *uts) |
| 51 | { |
| 52 | const int offset = 1; |
| 53 | struct udevice *dev, *gpio; |
| 54 | |
| 55 | /* |
| 56 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 57 | * bank gpio_a, offset 1. |
| 58 | */ |
| 59 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 60 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 61 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 62 | ut_assertok(led_set_state(dev, LEDST_ON)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 63 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 64 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 65 | |
| 66 | ut_assertok(led_set_state(dev, LEDST_OFF)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 67 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 68 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 73 | |
Simon Glass | 9413ad4 | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 74 | /* Test that we can toggle LEDs */ |
| 75 | static int dm_test_led_toggle(struct unit_test_state *uts) |
| 76 | { |
| 77 | const int offset = 1; |
| 78 | struct udevice *dev, *gpio; |
| 79 | |
| 80 | /* |
| 81 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 82 | * bank gpio_a, offset 1. |
| 83 | */ |
| 84 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 85 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 86 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 87 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); |
| 88 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
| 89 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
| 90 | |
| 91 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); |
| 92 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 93 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | DM_TEST(dm_test_led_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 98 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 99 | /* Test obtaining an LED by label */ |
| 100 | static int dm_test_led_label(struct unit_test_state *uts) |
| 101 | { |
| 102 | struct udevice *dev, *cmp; |
| 103 | |
| 104 | ut_assertok(led_get_by_label("sandbox:red", &dev)); |
| 105 | ut_asserteq(1, device_active(dev)); |
| 106 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp)); |
| 107 | ut_asserteq_ptr(dev, cmp); |
| 108 | |
| 109 | ut_assertok(led_get_by_label("sandbox:green", &dev)); |
| 110 | ut_asserteq(1, device_active(dev)); |
| 111 | ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp)); |
| 112 | ut_asserteq_ptr(dev, cmp); |
| 113 | |
| 114 | ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev)); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | 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] | 119 | |
| 120 | /* Test LED blinking */ |
| 121 | #ifdef CONFIG_LED_BLINK |
| 122 | static int dm_test_led_blink(struct unit_test_state *uts) |
| 123 | { |
| 124 | const int offset = 1; |
| 125 | struct udevice *dev, *gpio; |
| 126 | |
| 127 | /* |
| 128 | * Check that we get an error when trying to blink an LED, since it is |
| 129 | * not supported by the GPIO LED driver. |
| 130 | */ |
| 131 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 132 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 133 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 134 | ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK)); |
| 135 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 136 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 137 | ut_asserteq(-ENOSYS, led_set_period(dev, 100)); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | DM_TEST(dm_test_led_blink, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 142 | #endif |