Commit 5ac79730 authored by Stephen Boyd's avatar Stephen Boyd
Browse files

platform: Add test managed platform_device/driver APIs



Introduce KUnit resource wrappers around platform_driver_register(),
platform_device_alloc(), and platform_device_add() so that test authors
can register platform drivers/devices from their tests and have the
drivers/devices automatically be unregistered when the test is done.

This makes test setup code simpler when a platform driver or platform
device is needed. Add a few test cases at the same time to make sure the
APIs work as intended.

Cc: Brendan Higgins <brendan.higgins@linux.dev>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Cc: Rae Moar <rmoar@google.com>
Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Signed-off-by: default avatarStephen Boyd <sboyd@kernel.org>
Link: https://lore.kernel.org/r/20240718210513.3801024-6-sboyd@kernel.org
parent 5c9dd72d
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ API Reference
	resource
	functionredirection
	of
	platformdevice


This page documents the KUnit kernel testing API. It is divided into the
@@ -36,3 +37,7 @@ Driver KUnit API
Documentation/dev-tools/kunit/api/of.rst

 - Documents the KUnit device tree (OF) API

Documentation/dev-tools/kunit/api/platformdevice.rst

 - Documents the KUnit platform device API
+10 −0
Original line number Diff line number Diff line
.. SPDX-License-Identifier: GPL-2.0

===================
Platform Device API
===================

The KUnit platform device API is used to test platform devices.

.. kernel-doc:: lib/kunit/platform.c
   :export:
+1 −0
Original line number Diff line number Diff line
@@ -393,6 +393,7 @@ bool device_is_bound(struct device *dev)
{
	return dev->p && klist_node_attached(&dev->p->knode_driver);
}
EXPORT_SYMBOL_GPL(device_is_bound);

static void driver_bound(struct device *dev)
{
+20 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _KUNIT_PLATFORM_DRIVER_H
#define _KUNIT_PLATFORM_DRIVER_H

struct kunit;
struct platform_device;
struct platform_driver;

struct platform_device *
kunit_platform_device_alloc(struct kunit *test, const char *name, int id);
int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev);

int kunit_platform_device_prepare_wait_for_probe(struct kunit *test,
						 struct platform_device *pdev,
						 struct completion *x);

int kunit_platform_driver_register(struct kunit *test,
				   struct platform_driver *drv);

#endif
+3 −1
Original line number Diff line number Diff line
@@ -9,7 +9,8 @@ kunit-objs += test.o \
					try-catch.o \
					executor.o \
					attributes.o \
					device.o
					device.o \
					platform.o

ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
kunit-objs +=				debugfs.o
@@ -19,6 +20,7 @@ endif
obj-y +=				hooks.o

obj-$(CONFIG_KUNIT_TEST) +=		kunit-test.o
obj-$(CONFIG_KUNIT_TEST) +=		platform-test.o

# string-stream-test compiles built-in only.
ifeq ($(CONFIG_KUNIT_TEST),y)
Loading