2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-07-06 18:54:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <led.h>
|
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <dm/test.h>
|
2020-07-19 16:15:37 +00:00
|
|
|
#include <test/test.h>
|
2015-07-06 18:54:34 +00:00
|
|
|
#include <test/ut.h>
|
|
|
|
|
|
|
|
/* Base test of the led uclass */
|
|
|
|
static int dm_test_led_base(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
/* Get the top-level device */
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
|
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 2, &dev));
|
2018-04-11 09:16:29 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 3, &dev));
|
2022-04-25 16:33:50 +00:00
|
|
|
ut_asserteq(-ENODEV, uclass_get_device(UCLASS_LED, 4, &dev));
|
2015-07-06 18:54:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_led_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2015-07-06 18:54:34 +00:00
|
|
|
|
2018-04-11 09:16:29 +00:00
|
|
|
/* Test of the LED 'default-state' device tree property */
|
|
|
|
static int dm_test_led_default_state(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
/* Check that we handle the default-state property correctly. */
|
|
|
|
ut_assertok(led_get_by_label("sandbox:default_on", &dev));
|
|
|
|
ut_asserteq(LEDST_ON, led_get_state(dev));
|
|
|
|
|
2020-09-14 15:02:03 +00:00
|
|
|
/* Also tests default label behaviour */
|
|
|
|
ut_assertok(led_get_by_label("default_off", &dev));
|
2018-04-11 09:16:29 +00:00
|
|
|
ut_asserteq(LEDST_OFF, led_get_state(dev));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_led_default_state, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2018-04-11 09:16:29 +00:00
|
|
|
|
2015-07-06 18:54:34 +00:00
|
|
|
/* Test of the led uclass using the led_gpio driver */
|
|
|
|
static int dm_test_led_gpio(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
const int offset = 1;
|
|
|
|
struct udevice *dev, *gpio;
|
|
|
|
|
|
|
|
/*
|
2022-04-25 16:33:50 +00:00
|
|
|
* Check that we can manipulate an LED. LED 0 is connected to GPIO
|
2015-07-06 18:54:34 +00:00
|
|
|
* bank gpio_a, offset 1.
|
|
|
|
*/
|
2022-04-25 16:33:50 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
|
2015-07-06 18:54:34 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
|
|
|
|
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
|
2017-04-10 17:34:54 +00:00
|
|
|
ut_assertok(led_set_state(dev, LEDST_ON));
|
2015-07-06 18:54:34 +00:00
|
|
|
ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
|
2017-04-10 17:34:55 +00:00
|
|
|
ut_asserteq(LEDST_ON, led_get_state(dev));
|
2017-04-10 17:34:54 +00:00
|
|
|
|
|
|
|
ut_assertok(led_set_state(dev, LEDST_OFF));
|
2015-07-06 18:54:34 +00:00
|
|
|
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
|
2017-04-10 17:34:55 +00:00
|
|
|
ut_asserteq(LEDST_OFF, led_get_state(dev));
|
2015-07-06 18:54:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_led_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2015-07-06 18:54:34 +00:00
|
|
|
|
2017-04-10 17:34:56 +00:00
|
|
|
/* Test that we can toggle LEDs */
|
|
|
|
static int dm_test_led_toggle(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
const int offset = 1;
|
|
|
|
struct udevice *dev, *gpio;
|
|
|
|
|
|
|
|
/*
|
2022-04-25 16:33:50 +00:00
|
|
|
* Check that we can manipulate an LED. LED 0 is connected to GPIO
|
2017-04-10 17:34:56 +00:00
|
|
|
* bank gpio_a, offset 1.
|
|
|
|
*/
|
2022-04-25 16:33:50 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
|
2017-04-10 17:34:56 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
|
|
|
|
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
|
|
|
|
ut_assertok(led_set_state(dev, LEDST_TOGGLE));
|
|
|
|
ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
|
|
|
|
ut_asserteq(LEDST_ON, led_get_state(dev));
|
|
|
|
|
|
|
|
ut_assertok(led_set_state(dev, LEDST_TOGGLE));
|
|
|
|
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
|
|
|
|
ut_asserteq(LEDST_OFF, led_get_state(dev));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_led_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2017-04-10 17:34:56 +00:00
|
|
|
|
2015-07-06 18:54:34 +00:00
|
|
|
/* Test obtaining an LED by label */
|
|
|
|
static int dm_test_led_label(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
struct udevice *dev, *cmp;
|
|
|
|
|
|
|
|
ut_assertok(led_get_by_label("sandbox:red", &dev));
|
|
|
|
ut_asserteq(1, device_active(dev));
|
2022-04-25 16:33:50 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 0, &cmp));
|
2015-07-06 18:54:34 +00:00
|
|
|
ut_asserteq_ptr(dev, cmp);
|
|
|
|
|
|
|
|
ut_assertok(led_get_by_label("sandbox:green", &dev));
|
|
|
|
ut_asserteq(1, device_active(dev));
|
2022-04-25 16:33:50 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 1, &cmp));
|
2015-07-06 18:54:34 +00:00
|
|
|
ut_asserteq_ptr(dev, cmp);
|
|
|
|
|
|
|
|
ut_asserteq(-ENODEV, led_get_by_label("sandbox:blue", &dev));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_led_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2017-04-10 17:34:57 +00:00
|
|
|
|
|
|
|
/* Test LED blinking */
|
|
|
|
#ifdef CONFIG_LED_BLINK
|
|
|
|
static int dm_test_led_blink(struct unit_test_state *uts)
|
|
|
|
{
|
|
|
|
const int offset = 1;
|
|
|
|
struct udevice *dev, *gpio;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we get an error when trying to blink an LED, since it is
|
|
|
|
* not supported by the GPIO LED driver.
|
|
|
|
*/
|
2022-04-25 16:33:50 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_LED, 0, &dev));
|
2017-04-10 17:34:57 +00:00
|
|
|
ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
|
|
|
|
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
|
|
|
|
ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
|
|
|
|
ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
|
|
|
|
ut_asserteq(LEDST_OFF, led_get_state(dev));
|
|
|
|
ut_asserteq(-ENOSYS, led_set_period(dev, 100));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-29 01:41:12 +00:00
|
|
|
DM_TEST(dm_test_led_blink, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
|
2017-04-10 17:34:57 +00:00
|
|
|
#endif
|