Philippe Reynes | 0785e20 | 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> |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 10 | #include <adc.h> |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 11 | #include <button.h> |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 12 | #include <power/regulator.h> |
| 13 | #include <power/sandbox_pmic.h> |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 14 | #include <asm/gpio.h> |
| 15 | #include <dm/test.h> |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 16 | #include <dt-bindings/input/input.h> |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 17 | #include <test/ut.h> |
| 18 | |
| 19 | /* Base test of the button uclass */ |
| 20 | static int dm_test_button_base(struct unit_test_state *uts) |
| 21 | { |
| 22 | struct udevice *dev; |
| 23 | |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 24 | /* Get the top-level gpio buttons device */ |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 25 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev)); |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 26 | /* Get the 2 gpio buttons */ |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 27 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev)); |
| 28 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev)); |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 29 | |
| 30 | /* Get the top-level adc buttons device */ |
| 31 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 3, &dev)); |
| 32 | /* Get the 3 adc buttons */ |
| 33 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 4, &dev)); |
| 34 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 5, &dev)); |
| 35 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 6, &dev)); |
| 36 | |
| 37 | ut_asserteq(-ENODEV, uclass_get_device(UCLASS_BUTTON, 7, &dev)); |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 38 | |
| 39 | return 0; |
| 40 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 41 | DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 42 | |
| 43 | /* Test of the button uclass using the button_gpio driver */ |
| 44 | static int dm_test_button_gpio(struct unit_test_state *uts) |
| 45 | { |
| 46 | const int offset = 3; |
| 47 | struct udevice *dev, *gpio; |
| 48 | |
| 49 | /* |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 50 | * Check that we can manipulate a BUTTON. BUTTON 1 is connected to GPIO |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 51 | * bank gpio_a, offset 3. |
| 52 | */ |
| 53 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev)); |
| 54 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio)); |
| 55 | |
| 56 | ut_asserteq(0, sandbox_gpio_set_value(gpio, offset, 0)); |
| 57 | ut_asserteq(0, sandbox_gpio_get_value(gpio, offset)); |
| 58 | ut_asserteq(BUTTON_OFF, button_get_state(dev)); |
| 59 | |
| 60 | ut_asserteq(0, sandbox_gpio_set_value(gpio, offset, 1)); |
| 61 | ut_asserteq(1, sandbox_gpio_get_value(gpio, offset)); |
| 62 | ut_asserteq(BUTTON_ON, button_get_state(dev)); |
| 63 | |
| 64 | return 0; |
| 65 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 66 | DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 67 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 68 | /* Test obtaining a BUTTON by label */ |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 69 | static int dm_test_button_label(struct unit_test_state *uts) |
| 70 | { |
| 71 | struct udevice *dev, *cmp; |
| 72 | |
Heinrich Schuchardt | 57c2fc6 | 2020-09-14 12:50:54 +0200 | [diff] [blame] | 73 | ut_assertok(button_get_by_label("button1", &dev)); |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 74 | ut_asserteq(1, device_active(dev)); |
| 75 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp)); |
| 76 | ut_asserteq_ptr(dev, cmp); |
| 77 | |
Heinrich Schuchardt | 57c2fc6 | 2020-09-14 12:50:54 +0200 | [diff] [blame] | 78 | ut_assertok(button_get_by_label("button2", &dev)); |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 79 | ut_asserteq(1, device_active(dev)); |
| 80 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp)); |
| 81 | ut_asserteq_ptr(dev, cmp); |
| 82 | |
Heinrich Schuchardt | 57c2fc6 | 2020-09-14 12:50:54 +0200 | [diff] [blame] | 83 | ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev)); |
Philippe Reynes | 0785e20 | 2020-07-24 18:19:52 +0200 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 87 | DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 88 | |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 89 | /* Test button has linux,code */ |
| 90 | static int dm_test_button_linux_code(struct unit_test_state *uts) |
| 91 | { |
| 92 | struct udevice *dev; |
| 93 | |
| 94 | ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev)); |
| 95 | ut_asserteq(BTN_1, button_get_code(dev)); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | DM_TEST(dm_test_button_linux_code, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 100 | |
Marek Szyprowski | ad39859 | 2021-02-18 11:33:18 +0100 | [diff] [blame] | 101 | /* Test adc-keys driver */ |
| 102 | static int dm_test_button_keys_adc(struct unit_test_state *uts) |
| 103 | { |
| 104 | struct udevice *supply; |
| 105 | struct udevice *dev; |
| 106 | int uV; |
| 107 | |
| 108 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
| 109 | |
| 110 | ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply)); |
| 111 | ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV)); |
| 112 | ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply)); |
| 113 | /* Update ADC plat and get new Vdd value */ |
| 114 | ut_assertok(adc_vdd_value(dev, &uV)); |
| 115 | ut_asserteq(SANDBOX_BUCK2_SET_UV, uV); |
| 116 | |
| 117 | /* |
| 118 | * sandbox-adc returns constant value on channel 3, is used by adc-keys: |
| 119 | * SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_SET_UV / SANDBOX_ADC_DATA_MASK = |
| 120 | * 0x3000 * 3300000 / 0xffff = 618759uV |
| 121 | * This means that button3 and button4 are released and button5 |
| 122 | * is pressed. |
| 123 | */ |
| 124 | ut_assertok(button_get_by_label("button3", &dev)); |
| 125 | ut_asserteq(BUTTON_OFF, button_get_state(dev)); |
| 126 | ut_assertok(button_get_by_label("button4", &dev)); |
| 127 | ut_asserteq(BUTTON_OFF, button_get_state(dev)); |
| 128 | ut_assertok(button_get_by_label("button5", &dev)); |
| 129 | ut_asserteq(BUTTON_ON, button_get_state(dev)); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |