blob: ecaa47cf5f355bbd0a8fc85ef33424be29012f88 [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>
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 */
16static 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 Glass974dccd2020-07-28 19:41:12 -060028DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes0785e202020-07-24 18:19:52 +020029
30/* Test of the button uclass using the button_gpio driver */
31static 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 Glass974dccd2020-07-28 19:41:12 -060053DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes0785e202020-07-24 18:19:52 +020054
55/* Test obtaining an BUTTON by label */
56static int dm_test_button_label(struct unit_test_state *uts)
57{
58 struct udevice *dev, *cmp;
59
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020060 ut_assertok(button_get_by_label("button1", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020061 ut_asserteq(1, device_active(dev));
62 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp));
63 ut_asserteq_ptr(dev, cmp);
64
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020065 ut_assertok(button_get_by_label("button2", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020066 ut_asserteq(1, device_active(dev));
67 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp));
68 ut_asserteq_ptr(dev, cmp);
69
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020070 ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020071
72 return 0;
73}
Simon Glass974dccd2020-07-28 19:41:12 -060074DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);