1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2024 Cirrus Logic, Inc. and
3 // Cirrus Logic International Semiconductor Ltd.
5 #include <kunit/device.h>
6 #include <kunit/test.h>
7 #include <linux/module.h>
8 #include <sound/control.h>
10 #include <sound/soc-card.h>
12 struct soc_card_test_priv
{
13 struct device
*card_dev
;
14 struct snd_soc_card
*card
;
17 static const struct snd_kcontrol_new test_card_controls
[] = {
18 SOC_SINGLE("Fee", SND_SOC_NOPM
, 0, 1, 0),
19 SOC_SINGLE("Fi", SND_SOC_NOPM
, 1, 1, 0),
20 SOC_SINGLE("Fo", SND_SOC_NOPM
, 2, 1, 0),
21 SOC_SINGLE("Fum", SND_SOC_NOPM
, 3, 1, 0),
22 SOC_SINGLE("Left Fee", SND_SOC_NOPM
, 4, 1, 0),
23 SOC_SINGLE("Right Fee", SND_SOC_NOPM
, 5, 1, 0),
24 SOC_SINGLE("Left Fi", SND_SOC_NOPM
, 6, 1, 0),
25 SOC_SINGLE("Right Fi", SND_SOC_NOPM
, 7, 1, 0),
26 SOC_SINGLE("Left Fo", SND_SOC_NOPM
, 8, 1, 0),
27 SOC_SINGLE("Right Fo", SND_SOC_NOPM
, 9, 1, 0),
28 SOC_SINGLE("Left Fum", SND_SOC_NOPM
, 10, 1, 0),
29 SOC_SINGLE("Right Fum", SND_SOC_NOPM
, 11, 1, 0),
32 static void test_snd_soc_card_get_kcontrol(struct kunit
*test
)
34 struct soc_card_test_priv
*priv
= test
->priv
;
35 struct snd_soc_card
*card
= priv
->card
;
36 struct snd_kcontrol
*kc
;
37 struct soc_mixer_control
*mc
;
40 ret
= snd_soc_add_card_controls(card
, test_card_controls
, ARRAY_SIZE(test_card_controls
));
41 KUNIT_ASSERT_EQ(test
, ret
, 0);
43 /* Look up every control */
44 for (i
= 0; i
< ARRAY_SIZE(test_card_controls
); ++i
) {
45 kc
= snd_soc_card_get_kcontrol(card
, test_card_controls
[i
].name
);
46 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test
, kc
, "Failed to find '%s'\n",
47 test_card_controls
[i
].name
);
51 /* Test that it is the correct control */
52 mc
= (struct soc_mixer_control
*)kc
->private_value
;
53 KUNIT_EXPECT_EQ_MSG(test
, mc
->shift
, i
, "For '%s'\n", test_card_controls
[i
].name
);
56 /* Test some names that should not be found */
57 kc
= snd_soc_card_get_kcontrol(card
, "None");
58 KUNIT_EXPECT_NULL(test
, kc
);
60 kc
= snd_soc_card_get_kcontrol(card
, "Left None");
61 KUNIT_EXPECT_NULL(test
, kc
);
63 kc
= snd_soc_card_get_kcontrol(card
, "Left");
64 KUNIT_EXPECT_NULL(test
, kc
);
66 kc
= snd_soc_card_get_kcontrol(card
, NULL
);
67 KUNIT_EXPECT_NULL(test
, kc
);
70 static int soc_card_test_case_init(struct kunit
*test
)
72 struct soc_card_test_priv
*priv
;
75 priv
= kunit_kzalloc(test
, sizeof(*priv
), GFP_KERNEL
);
81 priv
->card
= kunit_kzalloc(test
, sizeof(*priv
->card
), GFP_KERNEL
);
85 priv
->card_dev
= kunit_device_register(test
, "sound-soc-card-test");
86 priv
->card_dev
= get_device(priv
->card_dev
);
90 priv
->card
->name
= "soc-card-test";
91 priv
->card
->dev
= priv
->card_dev
;
92 priv
->card
->owner
= THIS_MODULE
;
94 ret
= snd_soc_register_card(priv
->card
);
96 put_device(priv
->card_dev
);
103 static void soc_card_test_case_exit(struct kunit
*test
)
105 struct soc_card_test_priv
*priv
= test
->priv
;
108 snd_soc_unregister_card(priv
->card
);
111 put_device(priv
->card_dev
);
114 static struct kunit_case soc_card_test_cases
[] = {
115 KUNIT_CASE(test_snd_soc_card_get_kcontrol
),
119 static struct kunit_suite soc_card_test_suite
= {
121 .test_cases
= soc_card_test_cases
,
122 .init
= soc_card_test_case_init
,
123 .exit
= soc_card_test_case_exit
,
126 kunit_test_suites(&soc_card_test_suite
);
128 MODULE_DESCRIPTION("ASoC soc-card KUnit test");
129 MODULE_LICENSE("GPL");