[PATCH v2 2/4] gpio: kunit: factor out common code for waiting for device probe
From: Bartosz Golaszewski
Date: Mon Oct 12 2026 - 10:47:03 EST
We repeat the same sequence of function calls when waiting synchronously
for a device to probe. Put them into a dedicated helper function and
shrink the test-case code in the process.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
---
drivers/gpio/gpiolib-kunit.c | 80 ++++++++++++++------------------------------
1 file changed, 26 insertions(+), 54 deletions(-)
diff --git a/drivers/gpio/gpiolib-kunit.c b/drivers/gpio/gpiolib-kunit.c
index 66095c7a3d4277c6cf6c5a39ba292b67ecc990e3..f5f1e3627bf0084dc87f4a4dad8ac7e003c3945c 100644
--- a/drivers/gpio/gpiolib-kunit.c
+++ b/drivers/gpio/gpiolib-kunit.c
@@ -4,6 +4,7 @@
*/
#include <linux/cleanup.h>
+#include <linux/device.h>
#include <linux/err.h>
#include <linux/fwnode.h>
#include <linux/gpio/consumer.h>
@@ -27,6 +28,21 @@
#define GPIO_TEST_PROVIDER_NGPIO 4
+static void gpio_test_device_check_bound(struct kunit *test,
+ struct device *dev, bool expected)
+{
+ bool bound = false;
+
+ wait_for_device_probe();
+ scoped_guard(device, dev)
+ bound = device_is_bound(dev);
+
+ if (expected)
+ KUNIT_ASSERT_TRUE(test, bound);
+ else
+ KUNIT_ASSERT_FALSE(test, bound);
+}
+
/*
* The test provider tracks per-line direction and value so that lines can be
* driven as both inputs and outputs - this is needed to exercise input as well
@@ -177,7 +193,6 @@ static void gpio_swnode_lookup_by_primary(struct kunit *test)
struct platform_device_info pdevinfo;
struct property_entry properties[2];
struct platform_device *pdev;
- bool bound = false;
pdevinfo = (struct platform_device_info){
.name = GPIO_TEST_PROVIDER,
@@ -204,11 +219,7 @@ static void gpio_swnode_lookup_by_primary(struct kunit *test)
pdev = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
- wait_for_device_probe();
- scoped_guard(device, &pdev->dev)
- bound = device_is_bound(&pdev->dev);
-
- KUNIT_ASSERT_TRUE(test, bound);
+ gpio_test_device_check_bound(test, &pdev->dev, true);
pdata = dev_get_platdata(&pdev->dev);
KUNIT_ASSERT_TRUE(test, pdata->gpio_ok);
@@ -221,7 +232,6 @@ static void gpio_swnode_lookup_by_secondary(struct kunit *test)
struct property_entry properties[2];
struct fwnode_handle *primary;
struct platform_device *pdev;
- bool bound = false;
/*
* Can't live on the stack as it will still get referenced in cleanup
@@ -258,11 +268,7 @@ static void gpio_swnode_lookup_by_secondary(struct kunit *test)
pdev = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
- wait_for_device_probe();
- scoped_guard(device, &pdev->dev)
- bound = device_is_bound(&pdev->dev);
-
- KUNIT_ASSERT_TRUE(test, bound);
+ gpio_test_device_check_bound(test, &pdev->dev, true);
pdata = dev_get_platdata(&pdev->dev);
KUNIT_ASSERT_TRUE(test, pdata->gpio_ok);
@@ -332,7 +338,6 @@ static void gpio_swnode_probe_order(struct kunit *test)
struct gpio_probe_order_pdata *pdata;
struct platform_device_info pdevinfo;
struct platform_device *prvd, *cons;
- bool bound = false;
int ret;
ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
@@ -363,11 +368,7 @@ static void gpio_swnode_probe_order(struct kunit *test)
cons = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
- wait_for_device_probe();
- scoped_guard(device, &cons->dev)
- bound = device_is_bound(&cons->dev);
-
- KUNIT_ASSERT_FALSE(test, bound);
+ gpio_test_device_check_bound(test, &cons->dev, false);
pdata = dev_get_platdata(&cons->dev);
KUNIT_ASSERT_EQ(test, pdata->probe_count, 0);
@@ -382,15 +383,8 @@ static void gpio_swnode_probe_order(struct kunit *test)
prvd = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
- wait_for_device_probe();
-
- scoped_guard(device, &prvd->dev)
- bound = device_is_bound(&prvd->dev);
- KUNIT_ASSERT_TRUE(test, bound);
-
- scoped_guard(device, &cons->dev)
- bound = device_is_bound(&cons->dev);
- KUNIT_ASSERT_TRUE(test, bound);
+ gpio_test_device_check_bound(test, &prvd->dev, true);
+ gpio_test_device_check_bound(test, &cons->dev, true);
pdata = dev_get_platdata(&cons->dev);
KUNIT_ASSERT_EQ(test, pdata->probe_count, 1);
@@ -452,7 +446,6 @@ static void gpio_swnode_probe_defer_on_unregistered(struct kunit *test)
struct platform_device_info pdevinfo;
struct platform_device *prvd, *cons;
struct fwnode_handle *fwnode;
- bool bound = false;
int ret;
ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
@@ -476,11 +469,7 @@ static void gpio_swnode_probe_defer_on_unregistered(struct kunit *test)
cons = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cons);
- wait_for_device_probe();
- scoped_guard(device, &cons->dev)
- bound = device_is_bound(&cons->dev);
-
- KUNIT_ASSERT_FALSE(test, bound);
+ gpio_test_device_check_bound(test, &cons->dev, false);
pdata = dev_get_platdata(&cons->dev);
KUNIT_ASSERT_GT(test, pdata->probe_count, 0);
@@ -498,15 +487,8 @@ static void gpio_swnode_probe_defer_on_unregistered(struct kunit *test)
prvd = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, prvd);
- wait_for_device_probe();
-
- scoped_guard(device, &prvd->dev)
- bound = device_is_bound(&prvd->dev);
- KUNIT_ASSERT_TRUE(test, bound);
-
- scoped_guard(device, &cons->dev)
- bound = device_is_bound(&cons->dev);
- KUNIT_ASSERT_TRUE(test, bound);
+ gpio_test_device_check_bound(test, &cons->dev, true);
+ gpio_test_device_check_bound(test, &prvd->dev, true);
pdata = dev_get_platdata(&cons->dev);
KUNIT_ASSERT_EQ(test, pdata->gpio_err, 0);
@@ -603,7 +585,6 @@ static void gpio_unbind_with_consumers(struct kunit *test)
struct platform_device_info pdevinfo;
struct property_entry properties[2];
struct platform_device *prvd, *cons;
- bool bound = false;
int ret;
ret = kunit_platform_driver_register(test, &gpio_test_provider_driver);
@@ -644,11 +625,7 @@ static void gpio_unbind_with_consumers(struct kunit *test)
ret = kunit_platform_device_add(test, cons);
KUNIT_ASSERT_EQ(test, ret, 0);
- wait_for_device_probe();
- scoped_guard(device, &cons->dev)
- bound = device_is_bound(&cons->dev);
-
- KUNIT_ASSERT_TRUE(test, bound);
+ gpio_test_device_check_bound(test, &cons->dev, true);
kunit_platform_device_unregister(test, prvd);
@@ -760,7 +737,6 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
struct platform_device *pdev;
struct fwnode_handle *fwnode;
struct gpio_desc *desc;
- bool bound = true;
int ret;
fwnode = software_node_fwnode(&gpio_test_provider_swnode);
@@ -805,11 +781,7 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset,
pdev = kunit_platform_device_register_full(test, &pdevinfo);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
- wait_for_device_probe();
- scoped_guard(device, &pdev->dev)
- bound = device_is_bound(&pdev->dev);
-
- KUNIT_ASSERT_FALSE(test, bound);
+ gpio_test_device_check_bound(test, &pdev->dev, false);
pdata = dev_get_platdata(&pdev->dev);
KUNIT_ASSERT_FALSE(test, pdata->gpio_ok);
--
2.47.3