Philippe Reynes | 1d31000 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com> |
| 4 | * |
| 5 | * Based on led.c |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <button.h> |
| 11 | #include <asm/gpio.h> |
| 12 | #include <dm/test.h> |
| 13 | #include <test/ut.h> |
| 14 | |
| 15 | /* Base test of the button uclass */ |
| 16 | static int dm_test_button_base(struct unit_test_state *uts) |
| 17 | { |
| 18 | struct udevice *dev; |
| 19 | |
| 20 | /* Get the top-level device */ |
| 21 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev)); |
| 22 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev)); |
| 23 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev)); |
| 24 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_BUTTON, 3, &dev)); |
| 25 | |
| 26 | return 0; |
| 27 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 28 | DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Philippe Reynes | 1d31000 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 29 | |
| 30 | /* Test of the button uclass using the button_gpio driver */ |
| 31 | static int dm_test_button_gpio(struct unit_test_state *uts) |
| 32 | { |
| 33 | const int offset = 3; |
| 34 | struct udevice *dev, *gpio; |
| 35 | |
| 36 | /* |
| 37 | * Check that we can manipulate an BUTTON. BUTTON 1 is connected to GPIO |
| 38 | * bank gpio_a, offset 3. |
| 39 | */ |
| 40 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev)); |
| 41 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 42 | |
| 43 | ut_asserteq(0, sandbox_gpio_set_value(gpio, offset, 0)); |
| 44 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 45 | ut_asserteq(BUTTON_OFF, button_get_state(dev)); |
| 46 | |
| 47 | ut_asserteq(0, sandbox_gpio_set_value(gpio, offset, 1)); |
| 48 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
| 49 | ut_asserteq(BUTTON_ON, button_get_state(dev)); |
| 50 | |
| 51 | return 0; |
| 52 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 53 | DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Philippe Reynes | 1d31000 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 54 | |
| 55 | /* Test obtaining an BUTTON by label */ |
| 56 | static int dm_test_button_label(struct unit_test_state *uts) |
| 57 | { |
| 58 | struct udevice *dev, *cmp; |
| 59 | |
Heinrich Schuchardt | 39916bb | 2020-09-14 12:50:54 +0200 | [diff] [blame] | 60 | ut_assertok(button_get_by_label("button1", &dev)); |
Philippe Reynes | 1d31000 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 61 | ut_asserteq(1, device_active(dev)); |
| 62 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp)); |
| 63 | ut_asserteq_ptr(dev, cmp); |
| 64 | |
Heinrich Schuchardt | 39916bb | 2020-09-14 12:50:54 +0200 | [diff] [blame] | 65 | ut_assertok(button_get_by_label("button2", &dev)); |
Philippe Reynes | 1d31000 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 66 | ut_asserteq(1, device_active(dev)); |
| 67 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp)); |
| 68 | ut_asserteq_ptr(dev, cmp); |
| 69 | |
Heinrich Schuchardt | 39916bb | 2020-09-14 12:50:54 +0200 | [diff] [blame] | 70 | ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev)); |
Philippe Reynes | 1d31000 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 74 | DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |