blob: 3318668df25abc8b6c6b4a91da0d02fd915657c1 [file] [log] [blame]
Philippe Reynes0785e202020-07-24 18:19:52 +02001// 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 Szyprowskiad398592021-02-18 11:33:18 +010010#include <adc.h>
Philippe Reynes0785e202020-07-24 18:19:52 +020011#include <button.h>
Marek Szyprowskiad398592021-02-18 11:33:18 +010012#include <power/regulator.h>
13#include <power/sandbox_pmic.h>
Philippe Reynes0785e202020-07-24 18:19:52 +020014#include <asm/gpio.h>
15#include <dm/test.h>
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030016#include <dt-bindings/input/input.h>
Philippe Reynes0785e202020-07-24 18:19:52 +020017#include <test/ut.h>
18
19/* Base test of the button uclass */
20static int dm_test_button_base(struct unit_test_state *uts)
21{
22 struct udevice *dev;
23
Marek Szyprowskiad398592021-02-18 11:33:18 +010024 /* Get the top-level gpio buttons device */
Philippe Reynes0785e202020-07-24 18:19:52 +020025 ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev));
Marek Szyprowskiad398592021-02-18 11:33:18 +010026 /* Get the 2 gpio buttons */
Philippe Reynes0785e202020-07-24 18:19:52 +020027 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
28 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev));
Marek Szyprowskiad398592021-02-18 11:33:18 +010029
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 Reynes0785e202020-07-24 18:19:52 +020038
39 return 0;
40}
Simon Glass974dccd2020-07-28 19:41:12 -060041DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes0785e202020-07-24 18:19:52 +020042
43/* Test of the button uclass using the button_gpio driver */
44static int dm_test_button_gpio(struct unit_test_state *uts)
45{
46 const int offset = 3;
47 struct udevice *dev, *gpio;
48
49 /*
Yuepeng Xing71cc0212022-12-02 14:23:07 +080050 * Check that we can manipulate a BUTTON. BUTTON 1 is connected to GPIO
Philippe Reynes0785e202020-07-24 18:19:52 +020051 * 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 Glass974dccd2020-07-28 19:41:12 -060066DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes0785e202020-07-24 18:19:52 +020067
Yuepeng Xing71cc0212022-12-02 14:23:07 +080068/* Test obtaining a BUTTON by label */
Philippe Reynes0785e202020-07-24 18:19:52 +020069static int dm_test_button_label(struct unit_test_state *uts)
70{
71 struct udevice *dev, *cmp;
72
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020073 ut_assertok(button_get_by_label("button1", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020074 ut_asserteq(1, device_active(dev));
75 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp));
76 ut_asserteq_ptr(dev, cmp);
77
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020078 ut_assertok(button_get_by_label("button2", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020079 ut_asserteq(1, device_active(dev));
80 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp));
81 ut_asserteq_ptr(dev, cmp);
82
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020083 ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020084
85 return 0;
86}
Simon Glass974dccd2020-07-28 19:41:12 -060087DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Szyprowskiad398592021-02-18 11:33:18 +010088
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030089/* Test button has linux,code */
90static 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}
99DM_TEST(dm_test_button_linux_code, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
100
Marek Szyprowskiad398592021-02-18 11:33:18 +0100101/* Test adc-keys driver */
102static 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}
133DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);