xref: /linux/sound/soc/intel/avs/boards/i2s_test.c (revision f898c16a0624e7f2dcb0b1cda6916c9be6489197)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/module.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dapm.h>
15 #include "../utils.h"
16 
17 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
18 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
19 {
20 	struct snd_soc_dai_link_component *platform;
21 	struct snd_soc_dai_link *dl;
22 
23 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
24 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
25 	if (!dl || !platform)
26 		return -ENOMEM;
27 
28 	platform->name = platform_name;
29 
30 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
31 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
32 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
33 	if (!dl->name || !dl->cpus)
34 		return -ENOMEM;
35 
36 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
37 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
38 	dl->codecs = &snd_soc_dummy_dlc;
39 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
40 		return -ENOMEM;
41 
42 	dl->num_cpus = 1;
43 	dl->num_codecs = 1;
44 	dl->platforms = platform;
45 	dl->num_platforms = 1;
46 	dl->id = 0;
47 	dl->nonatomic = 1;
48 	dl->no_pcm = 1;
49 	dl->dpcm_capture = 1;
50 	dl->dpcm_playback = 1;
51 
52 	*dai_link = dl;
53 
54 	return 0;
55 }
56 
57 static int avs_i2s_test_probe(struct platform_device *pdev)
58 {
59 	struct snd_soc_dai_link *dai_link;
60 	struct snd_soc_acpi_mach *mach;
61 	struct snd_soc_card *card;
62 	struct device *dev = &pdev->dev;
63 	const char *pname;
64 	int ssp_port, tdm_slot, ret;
65 
66 	mach = dev_get_platdata(dev);
67 	pname = mach->mach_params.platform;
68 
69 	if (!avs_mach_singular_ssp(mach)) {
70 		dev_err(dev, "Invalid SSP configuration\n");
71 		return -EINVAL;
72 	}
73 	ssp_port = avs_mach_ssp_port(mach);
74 
75 	if (!avs_mach_singular_tdm(mach, ssp_port)) {
76 		dev_err(dev, "Invalid TDM configuration\n");
77 		return -EINVAL;
78 	}
79 	tdm_slot = avs_mach_ssp_tdm(mach, ssp_port);
80 
81 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
82 	if (!card)
83 		return -ENOMEM;
84 
85 	card->name = devm_kasprintf(dev, GFP_KERNEL,
86 				    AVS_STRING_FMT("ssp", "-loopback", ssp_port, tdm_slot));
87 	if (!card->name)
88 		return -ENOMEM;
89 
90 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
91 	if (ret) {
92 		dev_err(dev, "Failed to create dai link: %d\n", ret);
93 		return ret;
94 	}
95 
96 	card->dev = dev;
97 	card->owner = THIS_MODULE;
98 	card->dai_link = dai_link;
99 	card->num_links = 1;
100 	card->fully_routed = true;
101 
102 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
103 	if (ret)
104 		return ret;
105 
106 	return devm_snd_soc_register_card(dev, card);
107 }
108 
109 static const struct platform_device_id avs_i2s_test_driver_ids[] = {
110 	{
111 		.name = "avs_i2s_test",
112 	},
113 	{},
114 };
115 MODULE_DEVICE_TABLE(platform, avs_i2s_test_driver_ids);
116 
117 static struct platform_driver avs_i2s_test_driver = {
118 	.probe = avs_i2s_test_probe,
119 	.driver = {
120 		.name = "avs_i2s_test",
121 		.pm = &snd_soc_pm_ops,
122 	},
123 	.id_table = avs_i2s_test_driver_ids,
124 };
125 
126 module_platform_driver(avs_i2s_test_driver);
127 
128 MODULE_DESCRIPTION("Intel i2s test machine driver");
129 MODULE_LICENSE("GPL");
130