[PATCH v2 4/4] gpio: kunit: add a test case for platform lookup tables

From: Bartosz Golaszewski

Date: Mon Oct 12 2026 - 10:49:10 EST


Add a kunit test-case for retrieving GPIOs using platform lookup tables.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
---
drivers/gpio/gpiolib-kunit.c | 100 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 100 insertions(+)

diff --git a/drivers/gpio/gpiolib-kunit.c b/drivers/gpio/gpiolib-kunit.c
index b662be88a08711380f938852d84809f36a8d62d0..5d04b134d57746a78439fc54c6c78c1acde10a1d 100644
--- a/drivers/gpio/gpiolib-kunit.c
+++ b/drivers/gpio/gpiolib-kunit.c
@@ -18,6 +18,7 @@

#include <kunit/fwnode.h>
#include <kunit/platform_device.h>
+#include <kunit/resource.h>
#include <kunit/test.h>

#define GPIO_TEST_PROVIDER "gpio-test-provider"
@@ -828,11 +829,110 @@ static struct kunit_suite gpio_swnode_hog_test_suite = {
.init = gpio_test_register_drivers,
};

+KUNIT_DEFINE_ACTION_WRAPPER(gpiod_remove_lookup_table_wrapper,
+ gpiod_remove_lookup_table,
+ struct gpiod_lookup_table *);
+
+static int kunit_gpiod_add_lookup_table(struct kunit *test,
+ struct gpiod_lookup_table *table)
+{
+ gpiod_add_lookup_table(table);
+
+ return kunit_add_action_or_reset(test, gpiod_remove_lookup_table_wrapper, table);
+}
+
+static struct gpiod_lookup_table gpio_test_lookup_table_by_device = {
+ .dev_id = GPIO_BASE_TEST_CONSUMER,
+ .table = {
+ GPIO_LOOKUP(GPIO_TEST_PROVIDER, 0, "foo", GPIO_ACTIVE_HIGH),
+ { }
+ },
+};
+
+static void gpio_lookup_table_by_device(struct kunit *test)
+{
+ struct gpio_test_consumer_pdata *pdata;
+ struct platform_device_info pdevinfo;
+ struct platform_device *pdev;
+ int ret;
+
+ ret = kunit_gpiod_add_lookup_table(test, &gpio_test_lookup_table_by_device);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ pdevinfo = (struct platform_device_info){
+ .name = GPIO_TEST_PROVIDER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ pdevinfo = (struct platform_device_info){
+ .name = GPIO_BASE_TEST_CONSUMER,
+ .id = PLATFORM_DEVID_NONE,
+ .data = &gpio_test_pdata_template,
+ .size_data = sizeof(gpio_test_pdata_template),
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ gpio_test_device_check_bound(test, &pdev->dev, true);
+
+ pdata = dev_get_platdata(&pdev->dev);
+ KUNIT_ASSERT_TRUE(test, pdata->gpio_ok);
+}
+
+static struct gpiod_lookup_table gpio_test_lookup_table_global = {
+ .dev_id = NULL,
+ .table = {
+ GPIO_LOOKUP(GPIO_TEST_PROVIDER, 0, "foo", GPIO_ACTIVE_HIGH),
+ { }
+ },
+};
+
+static void gpio_lookup_table_global(struct kunit *test)
+{
+ struct platform_device_info pdevinfo;
+ struct platform_device *pdev;
+ struct gpio_desc *desc;
+ int ret;
+
+ ret = kunit_gpiod_add_lookup_table(test, &gpio_test_lookup_table_global);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+
+ pdevinfo = (struct platform_device_info){
+ .name = GPIO_TEST_PROVIDER,
+ .id = PLATFORM_DEVID_NONE,
+ };
+
+ pdev = kunit_platform_device_register_full(test, &pdevinfo);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
+
+ desc = gpiod_get_index(NULL, "foo", 0, GPIOD_OUT_HIGH);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc);
+
+ gpiod_put(desc);
+}
+
+static struct kunit_case gpio_lookup_table_tests[] = {
+ KUNIT_CASE(gpio_lookup_table_by_device),
+ KUNIT_CASE(gpio_lookup_table_global),
+ { }
+};
+
+static struct kunit_suite gpio_lookup_tables_suite = {
+ .name = "gpio-lookup-tables",
+ .test_cases = gpio_lookup_table_tests,
+ .init = gpio_test_register_drivers,
+};
+
kunit_test_suites(
&gpio_swnode_lookup_test_suite,
&gpio_swnode_probe_order_test_suite,
&gpio_unbind_with_consumers_test_suite,
&gpio_swnode_hog_test_suite,
+ &gpio_lookup_tables_suite,
);

MODULE_DESCRIPTION("Test module for the GPIO subsystem");

--
2.47.3