blob: ebb9b46584865cacc9e72cbf1b5843ef7958753a [file] [log] [blame]
Simon Glass3c43fba2015-07-06 12:54:34 -06001/*
2 * Copyright (C) 2015 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <led.h>
10#include <asm/gpio.h>
11#include <dm/test.h>
12#include <test/ut.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16/* Base test of the led uclass */
17static int dm_test_led_base(struct unit_test_state *uts)
18{
19 struct udevice *dev;
20
21 /* Get the top-level device */
22 ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
23 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
24 ut_assertok(uclass_get_device(UCLASS_LED, 2, &dev));
25 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 3, &dev));
26
27 return 0;
28}
29DM_TEST(dm_test_led_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
30
31/* Test of the led uclass using the led_gpio driver */
32static int dm_test_led_gpio(struct unit_test_state *uts)
33{
34 const int offset = 1;
35 struct udevice *dev, *gpio;
36
37 /*
38 * Check that we can manipulate an LED. LED 1 is connected to GPIO
39 * bank gpio_a, offset 1.
40 */
41 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
42 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
43 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
Simon Glassddae9fc2017-04-10 11:34:54 -060044 ut_assertok(led_set_state(dev, LEDST_ON));
Simon Glass3c43fba2015-07-06 12:54:34 -060045 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
Simon Glassddae9fc2017-04-10 11:34:54 -060046
47 ut_assertok(led_set_state(dev, LEDST_OFF));
Simon Glass3c43fba2015-07-06 12:54:34 -060048 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
49
50 return 0;
51}
52DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
53
54/* Test obtaining an LED by label */
55static int dm_test_led_label(struct unit_test_state *uts)
56{
57 struct udevice *dev, *cmp;
58
59 ut_assertok(led_get_by_label("sandbox:red", &dev));
60 ut_asserteq(1, device_active(dev));
61 ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
62 ut_asserteq_ptr(dev, cmp);
63
64 ut_assertok(led_get_by_label("sandbox:green", &dev));
65 ut_asserteq(1, device_active(dev));
66 ut_assertok(uclass_get_device(UCLASS_LED, 2, &cmp));
67 ut_asserteq_ptr(dev, cmp);
68
69 ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
70
71 return 0;
72}
73DM_TEST(dm_test_led_label, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);