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)); |
| 22 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 3, &dev)); |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | DM_TEST(dm_test_led_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 27 | |
| 28 | /* Test of the led uclass using the led_gpio driver */ |
| 29 | static int dm_test_led_gpio(struct unit_test_state *uts) |
| 30 | { |
| 31 | const int offset = 1; |
| 32 | struct udevice *dev, *gpio; |
| 33 | |
| 34 | /* |
| 35 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 36 | * bank gpio_a, offset 1. |
| 37 | */ |
| 38 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 39 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 40 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 41 | ut_assertok(led_set_state(dev, LEDST_ON)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 42 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 43 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 44 | |
| 45 | ut_assertok(led_set_state(dev, LEDST_OFF)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 46 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 47 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 52 | |
Simon Glass | 9413ad4 | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 53 | /* Test that we can toggle LEDs */ |
| 54 | static int dm_test_led_toggle(struct unit_test_state *uts) |
| 55 | { |
| 56 | const int offset = 1; |
| 57 | struct udevice *dev, *gpio; |
| 58 | |
| 59 | /* |
| 60 | * Check that we can manipulate an LED. LED 1 is connected to GPIO |
| 61 | * bank gpio_a, offset 1. |
| 62 | */ |
| 63 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 64 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 65 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 66 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); |
| 67 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
| 68 | ut_asserteq(LEDST_ON, led_get_state(dev)); |
| 69 | |
| 70 | ut_assertok(led_set_state(dev, LEDST_TOGGLE)); |
| 71 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 72 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | DM_TEST(dm_test_led_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 77 | |
Simon Glass | 3c43fba | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 78 | /* Test obtaining an LED by label */ |
| 79 | static int dm_test_led_label(struct unit_test_state *uts) |
| 80 | { |
| 81 | struct udevice *dev, *cmp; |
| 82 | |
| 83 | ut_assertok(led_get_by_label("sandbox:red", &dev)); |
| 84 | ut_asserteq(1, device_active(dev)); |
| 85 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp)); |
| 86 | ut_asserteq_ptr(dev, cmp); |
| 87 | |
| 88 | ut_assertok(led_get_by_label("sandbox:green", &dev)); |
| 89 | ut_asserteq(1, device_active(dev)); |
| 90 | ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp)); |
| 91 | ut_asserteq_ptr(dev, cmp); |
| 92 | |
| 93 | ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev)); |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | 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] | 98 | |
| 99 | /* Test LED blinking */ |
| 100 | #ifdef CONFIG_LED_BLINK |
| 101 | static int dm_test_led_blink(struct unit_test_state *uts) |
| 102 | { |
| 103 | const int offset = 1; |
| 104 | struct udevice *dev, *gpio; |
| 105 | |
| 106 | /* |
| 107 | * Check that we get an error when trying to blink an LED, since it is |
| 108 | * not supported by the GPIO LED driver. |
| 109 | */ |
| 110 | ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev)); |
| 111 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 112 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 113 | ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK)); |
| 114 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 115 | ut_asserteq(LEDST_OFF, led_get_state(dev)); |
| 116 | ut_asserteq(-ENOSYS, led_set_period(dev, 100)); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | DM_TEST(dm_test_led_blink, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 121 | #endif |