xref: /openbmc/linux/sound/soc/intel/avs/boards/i2s_test.c (revision 2d594783)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
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 
16 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
17 			       struct snd_soc_dai_link **dai_link)
18 {
19 	struct snd_soc_dai_link_component *platform;
20 	struct snd_soc_dai_link *dl;
21 
22 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
23 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
24 	if (!dl || !platform)
25 		return -ENOMEM;
26 
27 	platform->name = platform_name;
28 
29 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
30 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
31 	if (!dl->name || !dl->cpus)
32 		return -ENOMEM;
33 
34 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
35 	dl->codecs = &asoc_dummy_dlc;
36 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
37 		return -ENOMEM;
38 
39 	dl->num_cpus = 1;
40 	dl->num_codecs = 1;
41 	dl->platforms = platform;
42 	dl->num_platforms = 1;
43 	dl->id = 0;
44 	dl->nonatomic = 1;
45 	dl->no_pcm = 1;
46 	dl->dpcm_capture = 1;
47 	dl->dpcm_playback = 1;
48 
49 	*dai_link = dl;
50 
51 	return 0;
52 }
53 
54 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
55 				  struct snd_soc_dapm_route **routes, int *num_routes)
56 {
57 	struct snd_soc_dapm_route *dr;
58 	const int num_dr = 2;
59 
60 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
61 	if (!dr)
62 		return -ENOMEM;
63 
64 	dr[0].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%dpb", ssp_port);
65 	dr[0].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
66 	if (!dr[0].sink || !dr[0].source)
67 		return -ENOMEM;
68 
69 	dr[1].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
70 	dr[1].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%dcp", ssp_port);
71 	if (!dr[1].sink || !dr[1].source)
72 		return -ENOMEM;
73 
74 	*routes = dr;
75 	*num_routes = num_dr;
76 
77 	return 0;
78 }
79 
80 static int avs_create_dapm_widgets(struct device *dev, int ssp_port,
81 				   struct snd_soc_dapm_widget **widgets, int *num_widgets)
82 {
83 	struct snd_soc_dapm_widget *dw;
84 	const int num_dw = 2;
85 
86 	dw = devm_kcalloc(dev, num_dw, sizeof(*dw), GFP_KERNEL);
87 	if (!dw)
88 		return -ENOMEM;
89 
90 	dw[0].id = snd_soc_dapm_hp;
91 	dw[0].reg = SND_SOC_NOPM;
92 	dw[0].name = devm_kasprintf(dev, GFP_KERNEL, "ssp%dpb", ssp_port);
93 	if (!dw[0].name)
94 		return -ENOMEM;
95 
96 	dw[1].id = snd_soc_dapm_mic;
97 	dw[1].reg = SND_SOC_NOPM;
98 	dw[1].name = devm_kasprintf(dev, GFP_KERNEL, "ssp%dcp", ssp_port);
99 	if (!dw[1].name)
100 		return -ENOMEM;
101 
102 	*widgets = dw;
103 	*num_widgets = num_dw;
104 
105 	return 0;
106 }
107 
108 static int avs_i2s_test_probe(struct platform_device *pdev)
109 {
110 	struct snd_soc_dapm_widget *widgets;
111 	struct snd_soc_dapm_route *routes;
112 	struct snd_soc_dai_link *dai_link;
113 	struct snd_soc_acpi_mach *mach;
114 	struct snd_soc_card *card;
115 	struct device *dev = &pdev->dev;
116 	const char *pname;
117 	int num_routes, num_widgets;
118 	int ssp_port, ret;
119 
120 	mach = dev_get_platdata(dev);
121 	pname = mach->mach_params.platform;
122 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
123 
124 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
125 	if (!card)
126 		return -ENOMEM;
127 
128 	card->name = devm_kasprintf(dev, GFP_KERNEL, "ssp%d-loopback", ssp_port);
129 	if (!card->name)
130 		return -ENOMEM;
131 
132 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
133 	if (ret) {
134 		dev_err(dev, "Failed to create dai link: %d\n", ret);
135 		return ret;
136 	}
137 
138 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
139 	if (ret) {
140 		dev_err(dev, "Failed to create dapm routes: %d\n", ret);
141 		return ret;
142 	}
143 
144 	ret = avs_create_dapm_widgets(dev, ssp_port, &widgets, &num_widgets);
145 	if (ret) {
146 		dev_err(dev, "Failed to create dapm widgets: %d\n", ret);
147 		return ret;
148 	}
149 
150 	card->dev = dev;
151 	card->owner = THIS_MODULE;
152 	card->dai_link = dai_link;
153 	card->num_links = 1;
154 	card->dapm_routes = routes;
155 	card->num_dapm_routes = num_routes;
156 	card->dapm_widgets = widgets;
157 	card->num_dapm_widgets = num_widgets;
158 	card->fully_routed = true;
159 
160 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
161 	if (ret)
162 		return ret;
163 
164 	return devm_snd_soc_register_card(dev, card);
165 }
166 
167 static struct platform_driver avs_i2s_test_driver = {
168 	.probe = avs_i2s_test_probe,
169 	.driver = {
170 		.name = "avs_i2s_test",
171 		.pm = &snd_soc_pm_ops,
172 	},
173 };
174 
175 module_platform_driver(avs_i2s_test_driver);
176 
177 MODULE_LICENSE("GPL");
178 MODULE_ALIAS("platform:avs_i2s_test");
179