[PATCH 2/4] gpio: kunit: factor out common code for waiting for device probe

From: Bartosz Golaszewski

Date: Thu Oct 01 2026 - 08:46:59 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 09bbbbbde61694dd8b201ad699ed1943f6bfc48d..2dc000c6201d9983b16aec3f1368509756fe7800 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>
@@ -28,6 +29,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
@@ -178,7 +194,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,
@@ -205,11 +220,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);
@@ -222,7 +233,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
@@ -259,11 +269,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);
@@ -333,7 +339,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);
@@ -364,11 +369,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);
@@ -383,15 +384,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);
@@ -453,7 +447,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);
@@ -477,11 +470,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);
@@ -499,15 +488,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);
@@ -604,7 +586,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);
@@ -645,11 +626,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);

@@ -761,7 +738,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);
@@ -806,11 +782,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