1 // SPDX-License-Identifier: GPL-2.0-only
3 // KUnit test for the Cirrus side-codec library.
5 // Copyright (C) 2023 Cirrus Logic, Inc. and
6 // Cirrus Logic International Semiconductor Ltd.
8 #include <kunit/test.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
13 #include "cirrus_scodec.h"
15 struct cirrus_scodec_test_gpio {
16 unsigned int pin_state;
17 struct gpio_chip chip;
20 struct cirrus_scodec_test_priv {
21 struct platform_device amp_pdev;
22 struct platform_device *gpio_pdev;
23 struct cirrus_scodec_test_gpio *gpio_priv;
26 static int cirrus_scodec_test_gpio_get_direction(struct gpio_chip *chip,
29 return GPIO_LINE_DIRECTION_IN;
32 static int cirrus_scodec_test_gpio_direction_in(struct gpio_chip *chip,
38 static int cirrus_scodec_test_gpio_get(struct gpio_chip *chip, unsigned int offset)
40 struct cirrus_scodec_test_gpio *gpio_priv = gpiochip_get_data(chip);
42 return !!(gpio_priv->pin_state & BIT(offset));
45 static int cirrus_scodec_test_gpio_direction_out(struct gpio_chip *chip,
46 unsigned int offset, int value)
51 static void cirrus_scodec_test_gpio_set(struct gpio_chip *chip, unsigned int offset,
56 static int cirrus_scodec_test_gpio_set_config(struct gpio_chip *gc,
60 switch (pinconf_to_config_param(config)) {
61 case PIN_CONFIG_OUTPUT:
62 case PIN_CONFIG_OUTPUT_ENABLE:
69 static const struct gpio_chip cirrus_scodec_test_gpio_chip = {
70 .label = "cirrus_scodec_test_gpio",
72 .request = gpiochip_generic_request,
73 .free = gpiochip_generic_free,
74 .get_direction = cirrus_scodec_test_gpio_get_direction,
75 .direction_input = cirrus_scodec_test_gpio_direction_in,
76 .get = cirrus_scodec_test_gpio_get,
77 .direction_output = cirrus_scodec_test_gpio_direction_out,
78 .set = cirrus_scodec_test_gpio_set,
79 .set_config = cirrus_scodec_test_gpio_set_config,
84 static int cirrus_scodec_test_gpio_probe(struct platform_device *pdev)
86 struct cirrus_scodec_test_gpio *gpio_priv;
89 gpio_priv = devm_kzalloc(&pdev->dev, sizeof(*gpio_priv), GFP_KERNEL);
93 /* GPIO core modifies our struct gpio_chip so use a copy */
94 gpio_priv->chip = cirrus_scodec_test_gpio_chip;
95 ret = devm_gpiochip_add_data(&pdev->dev, &gpio_priv->chip, gpio_priv);
97 return dev_err_probe(&pdev->dev, ret, "Failed to add gpiochip\n");
99 dev_set_drvdata(&pdev->dev, gpio_priv);
104 static struct platform_driver cirrus_scodec_test_gpio_driver = {
105 .driver.name = "cirrus_scodec_test_gpio_drv",
106 .probe = cirrus_scodec_test_gpio_probe,
109 /* software_node referencing the gpio driver */
110 static const struct software_node cirrus_scodec_test_gpio_swnode = {
111 .name = "cirrus_scodec_test_gpio",
114 static int cirrus_scodec_test_create_gpio(struct kunit *test)
116 struct cirrus_scodec_test_priv *priv = test->priv;
119 priv->gpio_pdev = platform_device_alloc(cirrus_scodec_test_gpio_driver.driver.name, -1);
120 if (!priv->gpio_pdev)
123 ret = device_add_software_node(&priv->gpio_pdev->dev, &cirrus_scodec_test_gpio_swnode);
125 platform_device_put(priv->gpio_pdev);
126 KUNIT_FAIL(test, "Failed to add swnode to gpio: %d\n", ret);
130 ret = platform_device_add(priv->gpio_pdev);
132 platform_device_put(priv->gpio_pdev);
133 KUNIT_FAIL(test, "Failed to add gpio platform device: %d\n", ret);
137 priv->gpio_priv = dev_get_drvdata(&priv->gpio_pdev->dev);
138 if (!priv->gpio_priv) {
139 platform_device_put(priv->gpio_pdev);
140 KUNIT_FAIL(test, "Failed to get gpio private data\n");
147 static void cirrus_scodec_test_set_gpio_ref_arg(struct software_node_ref_args *arg,
150 struct software_node_ref_args template =
151 SOFTWARE_NODE_REFERENCE(&cirrus_scodec_test_gpio_swnode, gpio_num, 0);
156 static int cirrus_scodec_test_set_spkid_swnode(struct kunit *test,
158 struct software_node_ref_args *args,
161 const struct property_entry props_template[] = {
162 PROPERTY_ENTRY_REF_ARRAY_LEN("spk-id-gpios", args, num_args),
165 struct property_entry *props;
166 struct software_node *node;
168 node = kunit_kzalloc(test, sizeof(*node), GFP_KERNEL);
172 props = kunit_kzalloc(test, sizeof(props_template), GFP_KERNEL);
176 memcpy(props, props_template, sizeof(props_template));
177 node->properties = props;
179 return device_add_software_node(dev, node);
182 struct cirrus_scodec_test_spkid_param {
185 int num_amps_sharing;
188 static void cirrus_scodec_test_spkid_parse(struct kunit *test)
190 struct cirrus_scodec_test_priv *priv = test->priv;
191 const struct cirrus_scodec_test_spkid_param *param = test->param_value;
192 int num_spk_id_refs = param->num_amps * param->gpios_per_amp;
193 struct software_node_ref_args *refs;
194 struct device *dev = &priv->amp_pdev.dev;
198 refs = kunit_kcalloc(test, num_spk_id_refs, sizeof(*refs), GFP_KERNEL);
199 KUNIT_ASSERT_NOT_NULL(test, refs);
201 for (i = 0, v = 0; i < num_spk_id_refs; ) {
202 cirrus_scodec_test_set_gpio_ref_arg(&refs[i++], v++);
205 * If amps are sharing GPIOs repeat the last set of
206 * GPIOs until we've done that number of amps.
207 * We have done all GPIOs for an amp when i is a multiple
209 * We have done all amps sharing the same GPIOs when i is
210 * a multiple of (gpios_per_amp * num_amps_sharing).
212 if (!(i % param->gpios_per_amp) &&
213 (i % (param->gpios_per_amp * param->num_amps_sharing)))
214 v -= param->gpios_per_amp;
217 ret = cirrus_scodec_test_set_spkid_swnode(test, dev, refs, num_spk_id_refs);
218 KUNIT_EXPECT_EQ_MSG(test, ret, 0, "Failed to add swnode\n");
220 for (i = 0; i < param->num_amps; ++i) {
221 for (v = 0; v < (1 << param->gpios_per_amp); ++v) {
222 /* Set only the GPIO bits used by this amp */
223 priv->gpio_priv->pin_state =
224 v << (param->gpios_per_amp * (i / param->num_amps_sharing));
226 ret = cirrus_scodec_get_speaker_id(dev, i, param->num_amps, -1);
227 KUNIT_EXPECT_EQ_MSG(test, ret, v,
228 "get_speaker_id failed amp:%d pin_state:%#x\n",
229 i, priv->gpio_priv->pin_state);
234 static void cirrus_scodec_test_no_spkid(struct kunit *test)
236 struct cirrus_scodec_test_priv *priv = test->priv;
237 struct device *dev = &priv->amp_pdev.dev;
240 ret = cirrus_scodec_get_speaker_id(dev, 0, 4, -1);
241 KUNIT_EXPECT_EQ(test, ret, -ENOENT);
244 static void cirrus_scodec_test_dev_release(struct device *dev)
248 static int cirrus_scodec_test_case_init(struct kunit *test)
250 struct cirrus_scodec_test_priv *priv;
253 priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
259 /* Create dummy GPIO */
260 ret = cirrus_scodec_test_create_gpio(test);
264 /* Create dummy amp driver dev */
265 priv->amp_pdev.name = "cirrus_scodec_test_amp_drv";
266 priv->amp_pdev.id = -1;
267 priv->amp_pdev.dev.release = cirrus_scodec_test_dev_release;
268 ret = platform_device_register(&priv->amp_pdev);
269 KUNIT_ASSERT_GE_MSG(test, ret, 0, "Failed to register amp platform device\n");
274 static void cirrus_scodec_test_case_exit(struct kunit *test)
276 struct cirrus_scodec_test_priv *priv = test->priv;
278 if (priv->amp_pdev.name)
279 platform_device_unregister(&priv->amp_pdev);
281 if (priv->gpio_pdev) {
282 device_remove_software_node(&priv->gpio_pdev->dev);
283 platform_device_unregister(priv->gpio_pdev);
287 static int cirrus_scodec_test_suite_init(struct kunit_suite *suite)
291 /* Register mock GPIO driver */
292 ret = platform_driver_register(&cirrus_scodec_test_gpio_driver);
294 kunit_err(suite, "Failed to register gpio platform driver, %d\n", ret);
301 static void cirrus_scodec_test_suite_exit(struct kunit_suite *suite)
303 platform_driver_unregister(&cirrus_scodec_test_gpio_driver);
306 static const struct cirrus_scodec_test_spkid_param cirrus_scodec_test_spkid_param_cases[] = {
307 { .num_amps = 2, .gpios_per_amp = 1, .num_amps_sharing = 1 },
308 { .num_amps = 2, .gpios_per_amp = 2, .num_amps_sharing = 1 },
309 { .num_amps = 2, .gpios_per_amp = 3, .num_amps_sharing = 1 },
310 { .num_amps = 2, .gpios_per_amp = 4, .num_amps_sharing = 1 },
311 { .num_amps = 3, .gpios_per_amp = 1, .num_amps_sharing = 1 },
312 { .num_amps = 3, .gpios_per_amp = 2, .num_amps_sharing = 1 },
313 { .num_amps = 3, .gpios_per_amp = 3, .num_amps_sharing = 1 },
314 { .num_amps = 3, .gpios_per_amp = 4, .num_amps_sharing = 1 },
315 { .num_amps = 4, .gpios_per_amp = 1, .num_amps_sharing = 1 },
316 { .num_amps = 4, .gpios_per_amp = 2, .num_amps_sharing = 1 },
317 { .num_amps = 4, .gpios_per_amp = 3, .num_amps_sharing = 1 },
318 { .num_amps = 4, .gpios_per_amp = 4, .num_amps_sharing = 1 },
320 /* Same GPIO shared by all amps */
321 { .num_amps = 2, .gpios_per_amp = 1, .num_amps_sharing = 2 },
322 { .num_amps = 2, .gpios_per_amp = 2, .num_amps_sharing = 2 },
323 { .num_amps = 2, .gpios_per_amp = 3, .num_amps_sharing = 2 },
324 { .num_amps = 2, .gpios_per_amp = 4, .num_amps_sharing = 2 },
325 { .num_amps = 3, .gpios_per_amp = 1, .num_amps_sharing = 3 },
326 { .num_amps = 3, .gpios_per_amp = 2, .num_amps_sharing = 3 },
327 { .num_amps = 3, .gpios_per_amp = 3, .num_amps_sharing = 3 },
328 { .num_amps = 3, .gpios_per_amp = 4, .num_amps_sharing = 3 },
329 { .num_amps = 4, .gpios_per_amp = 1, .num_amps_sharing = 4 },
330 { .num_amps = 4, .gpios_per_amp = 2, .num_amps_sharing = 4 },
331 { .num_amps = 4, .gpios_per_amp = 3, .num_amps_sharing = 4 },
332 { .num_amps = 4, .gpios_per_amp = 4, .num_amps_sharing = 4 },
334 /* Two sets of shared GPIOs */
335 { .num_amps = 4, .gpios_per_amp = 1, .num_amps_sharing = 2 },
336 { .num_amps = 4, .gpios_per_amp = 2, .num_amps_sharing = 2 },
337 { .num_amps = 4, .gpios_per_amp = 3, .num_amps_sharing = 2 },
338 { .num_amps = 4, .gpios_per_amp = 4, .num_amps_sharing = 2 },
341 static void cirrus_scodec_test_spkid_param_desc(const struct cirrus_scodec_test_spkid_param *param,
344 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "amps:%d gpios_per_amp:%d num_amps_sharing:%d",
345 param->num_amps, param->gpios_per_amp, param->num_amps_sharing);
348 KUNIT_ARRAY_PARAM(cirrus_scodec_test_spkid, cirrus_scodec_test_spkid_param_cases,
349 cirrus_scodec_test_spkid_param_desc);
351 static struct kunit_case cirrus_scodec_test_cases[] = {
352 KUNIT_CASE_PARAM(cirrus_scodec_test_spkid_parse, cirrus_scodec_test_spkid_gen_params),
353 KUNIT_CASE(cirrus_scodec_test_no_spkid),
357 static struct kunit_suite cirrus_scodec_test_suite = {
358 .name = "snd-hda-scodec-cs35l56-test",
359 .suite_init = cirrus_scodec_test_suite_init,
360 .suite_exit = cirrus_scodec_test_suite_exit,
361 .init = cirrus_scodec_test_case_init,
362 .exit = cirrus_scodec_test_case_exit,
363 .test_cases = cirrus_scodec_test_cases,
366 kunit_test_suite(cirrus_scodec_test_suite);
368 MODULE_IMPORT_NS(SND_HDA_CIRRUS_SCODEC);
369 MODULE_DESCRIPTION("KUnit test for the Cirrus side-codec library");
370 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
371 MODULE_LICENSE("GPL");