summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2022-11-11 12:15:13 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-11-14 19:18:39 +0000
commitf9d615394061964989e92dc6ed0f5124a9f4d2f1 (patch)
treee7a4371c0cc0b19b159ef8f1357b3d643a3bf09c
parent5011f4a3fb0937fc393e28b0af30150b62adae78 (diff)
downloadchrome-ec-f9d615394061964989e92dc6ed0f5124a9f4d2f1.tar.gz
test: pi3usb9201 driver shim
Verify that the shim layer of the pi3usb9201 handles GPIO callbacks. BRANCH=none BUG=none TEST=twister Signed-off-by: Yuval Peress <peress@google.com> Change-Id: I19f7d48f3f2d7e066e952caa35b857d564535d47 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4018195 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Abe Levkoy <alevkoy@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org>
-rw-r--r--zephyr/test/drivers/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/Kconfig3
-rw-r--r--zephyr/test/drivers/bc12_pi3usb9201/CMakeLists.txt5
-rw-r--r--zephyr/test/drivers/bc12_pi3usb9201/src/main.c119
-rw-r--r--zephyr/test/drivers/boards/native_posix.overlay20
-rw-r--r--zephyr/test/drivers/testcase.yaml3
6 files changed, 150 insertions, 1 deletions
diff --git a/zephyr/test/drivers/CMakeLists.txt b/zephyr/test/drivers/CMakeLists.txt
index e5b39fde6b..dd8ab61b59 100644
--- a/zephyr/test/drivers/CMakeLists.txt
+++ b/zephyr/test/drivers/CMakeLists.txt
@@ -49,6 +49,7 @@ add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_LOCATE_CHIP_ALTS locate_chip)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_BUTTON button)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_CONSOLE console)
add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_HOST_CMD_THREAD host_command_thread)
+add_subdirectory_ifdef(CONFIG_LINK_TEST_SUITE_PI3USB9201 bc12_pi3usb9201)
get_target_property(TEST_SOURCES_NEW app SOURCES)
diff --git a/zephyr/test/drivers/Kconfig b/zephyr/test/drivers/Kconfig
index 21f18db9ef..496bfd2ac0 100644
--- a/zephyr/test/drivers/Kconfig
+++ b/zephyr/test/drivers/Kconfig
@@ -123,4 +123,7 @@ config LINK_TEST_SUITE_CONSOLE
config LINK_TEST_SUITE_HOST_CMD_THREAD
bool "Link and test the host command thread override tests"
+config LINK_TEST_SUITE_PI3USB9201
+ bool "Link and test the pi3usb9201 tests"
+
source "Kconfig.zephyr"
diff --git a/zephyr/test/drivers/bc12_pi3usb9201/CMakeLists.txt b/zephyr/test/drivers/bc12_pi3usb9201/CMakeLists.txt
new file mode 100644
index 0000000000..842a514e16
--- /dev/null
+++ b/zephyr/test/drivers/bc12_pi3usb9201/CMakeLists.txt
@@ -0,0 +1,5 @@
+# Copyright 2022 The ChromiumOS Authors
+#Use of this source code is governed by a BSD-style license that can be
+#found in the LICENSE file.
+
+target_sources(app PRIVATE src/main.c)
diff --git a/zephyr/test/drivers/bc12_pi3usb9201/src/main.c b/zephyr/test/drivers/bc12_pi3usb9201/src/main.c
new file mode 100644
index 0000000000..b2c69c712a
--- /dev/null
+++ b/zephyr/test/drivers/bc12_pi3usb9201/src/main.c
@@ -0,0 +1,119 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/device.h>
+#include <zephyr/drivers/gpio/gpio_emul.h>
+#include <zephyr/fff.h>
+#include <zephyr/ztest.h>
+
+#include "gpio_signal.h"
+#include "task.h"
+#include "test/drivers/test_state.h"
+#include "test/drivers/utils.h"
+#include "usb_charge.h"
+
+/* Get reference to externally linked handlers (defined in DTS) */
+#define USBC0_GPIO_PATH DT_PATH(named_gpios, usb_c0_bc12_int_l)
+#define USBC0_GPIO_DEV DEVICE_DT_GET(DT_GPIO_CTLR(USBC0_GPIO_PATH, gpios))
+#define USBC0_GPIO_PORT DT_GPIO_PIN(USBC0_GPIO_PATH, gpios)
+
+#define USBC1_GPIO_PATH DT_PATH(named_gpios, usb_c1_bc12_int_l)
+#define USBC1_GPIO_DEV DEVICE_DT_GET(DT_GPIO_CTLR(USBC1_GPIO_PATH, gpios))
+#define USBC1_GPIO_PORT DT_GPIO_PIN(USBC1_GPIO_PATH, gpios)
+
+static void toggle_gpio(const struct device *dev, gpio_pin_t pin)
+{
+ static const int values[] = { 1, 0, 1 };
+
+ for (int i = 0; i < ARRAY_SIZE(values); ++i) {
+ gpio_emul_input_set(dev, pin, values[i]);
+ }
+}
+
+FAKE_VOID_FUNC(usb_charger_task_event, int, uint32_t);
+
+struct pi3usb9201_fixture {
+ const struct bc12_drv *drv[2];
+ struct bc12_drv mock_drv;
+};
+
+static void *setup(void)
+{
+ static struct pi3usb9201_fixture fixture;
+
+ fixture.mock_drv.usb_charger_task_event = usb_charger_task_event;
+
+ return &fixture;
+}
+
+static void before(void *f)
+{
+ struct pi3usb9201_fixture *fixture = f;
+
+ fixture->drv[0] = bc12_ports[0].drv;
+ fixture->drv[1] = bc12_ports[1].drv;
+
+ RESET_FAKE(usb_charger_task_event);
+ test_set_chipset_to_s0();
+}
+
+static void after(void *f)
+{
+ struct pi3usb9201_fixture *fixture = f;
+
+ bc12_ports[0].drv = fixture->drv[0];
+ bc12_ports[1].drv = fixture->drv[1];
+}
+
+ZTEST_SUITE(pi3usb9201, drivers_predicate_post_main, setup, before, after,
+ NULL);
+
+ZTEST_F(pi3usb9201, test_usb0_evt)
+{
+ /* Set up the driver to use the mock */
+ bc12_ports[0].drv = &fixture->mock_drv;
+
+ /* Trigger the event and verify that port0 was added to the task event
+ * bitmap
+ */
+ toggle_gpio(USBC0_GPIO_DEV, USBC0_GPIO_PORT);
+ zassert_true(*task_get_event_bitmap(TASK_ID_USB_CHG) & BIT(0));
+
+ /* Give the task a bit of time to process the events */
+ task_wake(TASK_ID_USB_CHG);
+ k_msleep(500);
+
+ /* Ensure that the callback was made (it should be the first, but others
+ * may exist).
+ */
+ zassert_true(usb_charger_task_event_fake.call_count > 0);
+ zassert_equal(0, usb_charger_task_event_fake.arg0_history[0]);
+ zassert_equal(USB_CHG_EVENT_BC12,
+ usb_charger_task_event_fake.arg1_history[0]);
+}
+
+ZTEST_F(pi3usb9201, test_usb1_evt)
+{
+ /* Set up the driver to use the mock */
+ bc12_ports[1].drv = &fixture->mock_drv;
+
+ /* Trigger the event and verify that port1 was added to the task event
+ * bitmap
+ */
+ toggle_gpio(USBC1_GPIO_DEV, USBC1_GPIO_PORT);
+ zassert_true(*task_get_event_bitmap(TASK_ID_USB_CHG) & BIT(1));
+
+ /* Give the task a bit of time to process the events */
+ task_wake(TASK_ID_USB_CHG);
+ k_msleep(500);
+
+ /* Ensure that the callback was made (it should be the first, but others
+ * may exist).
+ */
+ zassert_true(usb_charger_task_event_fake.call_count > 0);
+ zassert_equal(1, usb_charger_task_event_fake.arg0_history[0]);
+ zassert_equal(USB_CHG_EVENT_BC12,
+ usb_charger_task_event_fake.arg1_history[0]);
+}
diff --git a/zephyr/test/drivers/boards/native_posix.overlay b/zephyr/test/drivers/boards/native_posix.overlay
index e244cb39f1..ddf493e35d 100644
--- a/zephyr/test/drivers/boards/native_posix.overlay
+++ b/zephyr/test/drivers/boards/native_posix.overlay
@@ -266,6 +266,12 @@
gpio_ec_cbi_wp: ec_cbi_wp {
gpios = <&gpio1 9 GPIO_OUTPUT>;
};
+ gpio_usb_c0_bc12_int_l: usb_c0_bc12_int_l {
+ gpios = <&gpio1 10 GPIO_INPUT>;
+ };
+ gpio_usb_c1_bc12_int_l: usb_c1_bc12_int_l {
+ gpios = <&gpio1 11 GPIO_INPUT>;
+ };
};
gpio1: gpio@101 {
@@ -278,7 +284,7 @@
low-level;
gpio-controller;
#gpio-cells = <2>;
- ngpios = <10>;
+ ngpios = <12>;
};
gpio-interrupts {
@@ -334,6 +340,16 @@
flags = <GPIO_INT_EDGE_BOTH>;
handler = "switch_interrupt";
};
+ int_usb_c0_bc12: usb_c0_bc12 {
+ irq-pin = <&gpio_usb_c0_bc12_int_l>;
+ flags = <GPIO_INT_EDGE_FALLING>;
+ handler = "usb0_evt";
+ };
+ int_usb_c1_bc12: usb_c1_bc12 {
+ irq-pin = <&gpio_usb_c1_bc12_int_l>;
+ flags = <GPIO_INT_EDGE_FALLING>;
+ handler = "usb1_evt";
+ };
};
named-i2c-ports {
@@ -810,6 +826,7 @@
pi3usb9201_emul0: pi3usb9201@5f {
compatible = "pericom,pi3usb9201";
reg = <0x5f>;
+ irq = <&int_usb_c0_bc12>;
};
sn5s330_emul: sn5s330@40 {
@@ -837,6 +854,7 @@
pi3usb9201_emul1: pi3usb9201@5d {
compatible = "pericom,pi3usb9201";
reg = <0x5d>;
+ irq = <&int_usb_c1_bc12>;
};
syv682x_emul: syv682x@41 {
diff --git a/zephyr/test/drivers/testcase.yaml b/zephyr/test/drivers/testcase.yaml
index d8a2dc838a..8a68a2af23 100644
--- a/zephyr/test/drivers/testcase.yaml
+++ b/zephyr/test/drivers/testcase.yaml
@@ -212,3 +212,6 @@ tests:
drivers.host_cmd_thread:
extra_configs:
- CONFIG_LINK_TEST_SUITE_HOST_CMD_THREAD=y
+ drivers.pi3usb9201:
+ extra_configs:
+ - CONFIG_LINK_TEST_SUITE_PI3USB9201=y