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