blob: f8a7fab61d7ede31efc823adf851e525c1f0c179 [file] [log] [blame]
Philippe Reynes1d310002020-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 Szyprowski289d0ea2021-02-18 11:33:18 +010010#include <adc.h>
Philippe Reynes1d310002020-07-24 18:19:52 +020011#include <button.h>
Marek Szyprowski289d0ea2021-02-18 11:33:18 +010012#include <power/regulator.h>
13#include <power/sandbox_pmic.h>
Philippe Reynes1d310002020-07-24 18:19:52 +020014#include <asm/gpio.h>
15#include <dm/test.h>
16#include <test/ut.h>
17
18/* Base test of the button uclass */
19static int dm_test_button_base(struct unit_test_state *uts)
20{
21 struct udevice *dev;
22
Marek Szyprowski289d0ea2021-02-18 11:33:18 +010023 /* Get the top-level gpio buttons device */
Philippe Reynes1d310002020-07-24 18:19:52 +020024 ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev));
Marek Szyprowski289d0ea2021-02-18 11:33:18 +010025 /* Get the 2 gpio buttons */
Philippe Reynes1d310002020-07-24 18:19:52 +020026 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
27 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev));
Marek Szyprowski289d0ea2021-02-18 11:33:18 +010028
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 Reynes1d310002020-07-24 18:19:52 +020037
38 return 0;
39}
Simon Glasse180c2b2020-07-28 19:41:12 -060040DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes1d310002020-07-24 18:19:52 +020041
42/* Test of the button uclass using the button_gpio driver */
43static 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 Glasse180c2b2020-07-28 19:41:12 -060065DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes1d310002020-07-24 18:19:52 +020066
67/* Test obtaining an BUTTON by label */
68static int dm_test_button_label(struct unit_test_state *uts)
69{
70 struct udevice *dev, *cmp;
71
Heinrich Schuchardt39916bb2020-09-14 12:50:54 +020072 ut_assertok(button_get_by_label("button1", &dev));
Philippe Reynes1d310002020-07-24 18:19:52 +020073 ut_asserteq(1, device_active(dev));
74 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp));
75 ut_asserteq_ptr(dev, cmp);
76
Heinrich Schuchardt39916bb2020-09-14 12:50:54 +020077 ut_assertok(button_get_by_label("button2", &dev));
Philippe Reynes1d310002020-07-24 18:19:52 +020078 ut_asserteq(1, device_active(dev));
79 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp));
80 ut_asserteq_ptr(dev, cmp);
81
Heinrich Schuchardt39916bb2020-09-14 12:50:54 +020082 ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev));
Philippe Reynes1d310002020-07-24 18:19:52 +020083
84 return 0;
85}
Simon Glasse180c2b2020-07-28 19:41:12 -060086DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Szyprowski289d0ea2021-02-18 11:33:18 +010087
88/* Test adc-keys driver */
89static 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}
120DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);