ALSA: hda/cirrus_scodec_test: Modernize creation of dummy devices
authorRichard Fitzgerald <rf@opensource.cirrus.com>
Tue, 15 Apr 2025 10:54:14 +0000 (11:54 +0100)
committerTakashi Iwai <tiwai@suse.de>
Tue, 15 Apr 2025 15:12:54 +0000 (17:12 +0200)
Replace the old direct use of platform_device APIs with newer KUnit APIs
and the faux bus.

The dummy codec driver device doesn't need to be a platform device.
It can be a faux bus device.

The dummy GPIO driver still must be a platform_device so that a
software_node can be added to it before it probes. But use the new
KUnit-managed APIs to create the platform_device and platform_driver.
These will cleanup automatically when a test completes or fails.

Also use KUnit resource cleanup to destroy the faux bus driver and the GPIO
software node instead of doing this "manually" in test exit() functions.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Link: https://patch.msgid.link/20250415105414.471039-1-rf@opensource.cirrus.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/pci/hda/cirrus_scodec_test.c

index f5d6241daee47c7b095967f51f648900bd794b12..58ed3976283c47e751b4d1da58ff4dbcaf856028 100644 (file)
@@ -5,20 +5,30 @@
 // Copyright (C) 2023 Cirrus Logic, Inc. and
 //                    Cirrus Logic International Semiconductor Ltd.
 
+#include <kunit/platform_device.h>
+#include <kunit/resource.h>
 #include <kunit/test.h>
+#include <linux/device.h>
+#include <linux/device/faux.h>
 #include <linux/gpio/driver.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 
 #include "cirrus_scodec.h"
 
+KUNIT_DEFINE_ACTION_WRAPPER(faux_device_destroy_wrapper, faux_device_destroy,
+                           struct faux_device *)
+KUNIT_DEFINE_ACTION_WRAPPER(device_remove_software_node_wrapper,
+                           device_remove_software_node,
+                           struct device *)
+
 struct cirrus_scodec_test_gpio {
        unsigned int pin_state;
        struct gpio_chip chip;
 };
 
 struct cirrus_scodec_test_priv {
-       struct platform_device amp_pdev;
+       struct faux_device *amp_dev;
        struct platform_device *gpio_pdev;
        struct cirrus_scodec_test_gpio *gpio_priv;
 };
@@ -103,6 +113,7 @@ static int cirrus_scodec_test_gpio_probe(struct platform_device *pdev)
 
 static struct platform_driver cirrus_scodec_test_gpio_driver = {
        .driver.name    = "cirrus_scodec_test_gpio_drv",
+       .driver.owner   = THIS_MODULE,
        .probe          = cirrus_scodec_test_gpio_probe,
 };
 
@@ -111,37 +122,28 @@ static const struct software_node cirrus_scodec_test_gpio_swnode = {
        .name = "cirrus_scodec_test_gpio",
 };
 
-static int cirrus_scodec_test_create_gpio(struct kunit *test)
+static void cirrus_scodec_test_create_gpio(struct kunit *test)
 {
        struct cirrus_scodec_test_priv *priv = test->priv;
-       int ret;
 
-       priv->gpio_pdev = platform_device_alloc(cirrus_scodec_test_gpio_driver.driver.name, -1);
-       if (!priv->gpio_pdev)
-               return -ENOMEM;
+       KUNIT_ASSERT_EQ(test, 0,
+                       kunit_platform_driver_register(test, &cirrus_scodec_test_gpio_driver));
 
-       ret = device_add_software_node(&priv->gpio_pdev->dev, &cirrus_scodec_test_gpio_swnode);
-       if (ret) {
-               platform_device_put(priv->gpio_pdev);
-               KUNIT_FAIL(test, "Failed to add swnode to gpio: %d\n", ret);
-               return ret;
-       }
+       priv->gpio_pdev = kunit_platform_device_alloc(test,
+                                                     cirrus_scodec_test_gpio_driver.driver.name,
+                                                     PLATFORM_DEVID_NONE);
+       KUNIT_ASSERT_NOT_NULL(test, priv->gpio_pdev);
 
-       ret = platform_device_add(priv->gpio_pdev);
-       if (ret) {
-               platform_device_put(priv->gpio_pdev);
-               KUNIT_FAIL(test, "Failed to add gpio platform device: %d\n", ret);
-               return ret;
-       }
+       KUNIT_ASSERT_EQ(test, 0, device_add_software_node(&priv->gpio_pdev->dev,
+                                                         &cirrus_scodec_test_gpio_swnode));
+       KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test,
+                                                          device_remove_software_node_wrapper,
+                                                          &priv->gpio_pdev->dev));
 
-       priv->gpio_priv = dev_get_drvdata(&priv->gpio_pdev->dev);
-       if (!priv->gpio_priv) {
-               platform_device_put(priv->gpio_pdev);
-               KUNIT_FAIL(test, "Failed to get gpio private data\n");
-               return -EINVAL;
-       }
+       KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, priv->gpio_pdev));
 
-       return 0;
+       priv->gpio_priv = dev_get_drvdata(&priv->gpio_pdev->dev);
+       KUNIT_ASSERT_NOT_NULL(test, priv->gpio_priv);
 }
 
 static void cirrus_scodec_test_set_gpio_ref_arg(struct software_node_ref_args *arg,
@@ -191,7 +193,7 @@ static void cirrus_scodec_test_spkid_parse(struct kunit *test)
        const struct cirrus_scodec_test_spkid_param *param = test->param_value;
        int num_spk_id_refs = param->num_amps * param->gpios_per_amp;
        struct software_node_ref_args *refs;
-       struct device *dev = &priv->amp_pdev.dev;
+       struct device *dev = &priv->amp_dev->dev;
        unsigned int v;
        int i, ret;
 
@@ -234,21 +236,16 @@ static void cirrus_scodec_test_spkid_parse(struct kunit *test)
 static void cirrus_scodec_test_no_spkid(struct kunit *test)
 {
        struct cirrus_scodec_test_priv *priv = test->priv;
-       struct device *dev = &priv->amp_pdev.dev;
+       struct device *dev = &priv->amp_dev->dev;
        int ret;
 
        ret = cirrus_scodec_get_speaker_id(dev, 0, 4, -1);
        KUNIT_EXPECT_EQ(test, ret, -ENOENT);
 }
 
-static void cirrus_scodec_test_dev_release(struct device *dev)
-{
-}
-
 static int cirrus_scodec_test_case_init(struct kunit *test)
 {
        struct cirrus_scodec_test_priv *priv;
-       int ret;
 
        priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
        if (!priv)
@@ -257,52 +254,18 @@ static int cirrus_scodec_test_case_init(struct kunit *test)
        test->priv = priv;
 
        /* Create dummy GPIO */
-       ret = cirrus_scodec_test_create_gpio(test);
-       if (ret < 0)
-               return ret;
+       cirrus_scodec_test_create_gpio(test);
 
        /* Create dummy amp driver dev */
-       priv->amp_pdev.name = "cirrus_scodec_test_amp_drv";
-       priv->amp_pdev.id = -1;
-       priv->amp_pdev.dev.release = cirrus_scodec_test_dev_release;
-       ret = platform_device_register(&priv->amp_pdev);
-       KUNIT_ASSERT_GE_MSG(test, ret, 0, "Failed to register amp platform device\n");
+       priv->amp_dev = faux_device_create("cirrus_scodec_test_amp_drv", NULL, NULL);
+       KUNIT_ASSERT_NOT_NULL(test, priv->amp_dev);
+       KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test,
+                                                          faux_device_destroy_wrapper,
+                                                          priv->amp_dev));
 
        return 0;
 }
 
-static void cirrus_scodec_test_case_exit(struct kunit *test)
-{
-       struct cirrus_scodec_test_priv *priv = test->priv;
-
-       if (priv->amp_pdev.name)
-               platform_device_unregister(&priv->amp_pdev);
-
-       if (priv->gpio_pdev) {
-               device_remove_software_node(&priv->gpio_pdev->dev);
-               platform_device_unregister(priv->gpio_pdev);
-       }
-}
-
-static int cirrus_scodec_test_suite_init(struct kunit_suite *suite)
-{
-       int ret;
-
-       /* Register mock GPIO driver */
-       ret = platform_driver_register(&cirrus_scodec_test_gpio_driver);
-       if (ret < 0) {
-               kunit_err(suite, "Failed to register gpio platform driver, %d\n", ret);
-               return ret;
-       }
-
-       return 0;
-}
-
-static void cirrus_scodec_test_suite_exit(struct kunit_suite *suite)
-{
-       platform_driver_unregister(&cirrus_scodec_test_gpio_driver);
-}
-
 static const struct cirrus_scodec_test_spkid_param cirrus_scodec_test_spkid_param_cases[] = {
        { .num_amps = 2, .gpios_per_amp = 1, .num_amps_sharing = 1 },
        { .num_amps = 2, .gpios_per_amp = 2, .num_amps_sharing = 1 },
@@ -356,10 +319,7 @@ static struct kunit_case cirrus_scodec_test_cases[] = {
 
 static struct kunit_suite cirrus_scodec_test_suite = {
        .name = "snd-hda-scodec-cs35l56-test",
-       .suite_init = cirrus_scodec_test_suite_init,
-       .suite_exit = cirrus_scodec_test_suite_exit,
        .init = cirrus_scodec_test_case_init,
-       .exit = cirrus_scodec_test_case_exit,
        .test_cases = cirrus_scodec_test_cases,
 };