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 <linux/platform_device.h>
11 #include <sound/hda_codec.h>
12 #include <sound/hda_i915.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include "../../../codecs/hda.h"
16
avs_create_dai_links(struct device * dev,struct hda_codec * codec,int pcm_count,const char * platform_name,struct snd_soc_dai_link ** links)17 static int avs_create_dai_links(struct device *dev, struct hda_codec *codec, int pcm_count,
18 const char *platform_name, struct snd_soc_dai_link **links)
19 {
20 struct snd_soc_dai_link_component *platform;
21 struct snd_soc_dai_link *dl;
22 struct hda_pcm *pcm;
23 const char *cname = dev_name(&codec->core.dev);
24 int i;
25
26 dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
27 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
28 if (!dl || !platform)
29 return -ENOMEM;
30
31 platform->name = platform_name;
32 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
33
34 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
35 dl[i].name = devm_kasprintf(dev, GFP_KERNEL, "%s link%d", cname, i);
36 if (!dl[i].name)
37 return -ENOMEM;
38
39 dl[i].id = i;
40 dl[i].nonatomic = 1;
41 dl[i].no_pcm = 1;
42 dl[i].dpcm_playback = 1;
43 dl[i].dpcm_capture = 1;
44 dl[i].platforms = platform;
45 dl[i].num_platforms = 1;
46 dl[i].ignore_pmdown_time = 1;
47
48 dl[i].codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
49 dl[i].cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
50 if (!dl[i].codecs || !dl[i].cpus)
51 return -ENOMEM;
52
53 dl[i].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d", cname, i);
54 if (!dl[i].cpus->dai_name)
55 return -ENOMEM;
56
57 dl[i].codecs->name = devm_kstrdup(dev, cname, GFP_KERNEL);
58 dl[i].codecs->dai_name = pcm->name;
59 dl[i].num_codecs = 1;
60 dl[i].num_cpus = 1;
61 }
62
63 *links = dl;
64 return 0;
65 }
66
avs_create_dapm_routes(struct device * dev,struct hda_codec * codec,int pcm_count,struct snd_soc_dapm_route ** routes,int * num_routes)67 static int avs_create_dapm_routes(struct device *dev, struct hda_codec *codec, int pcm_count,
68 struct snd_soc_dapm_route **routes, int *num_routes)
69 {
70 struct snd_soc_dapm_route *dr;
71 struct hda_pcm *pcm;
72 const char *cname = dev_name(&codec->core.dev);
73 int i, n = 0;
74
75 /* at max twice the number of pcms */
76 dr = devm_kcalloc(dev, pcm_count * 2, sizeof(*dr), GFP_KERNEL);
77 if (!dr)
78 return -ENOMEM;
79
80 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
81
82 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
83 struct hda_pcm_stream *stream;
84 int dir;
85
86 dir = SNDRV_PCM_STREAM_PLAYBACK;
87 stream = &pcm->stream[dir];
88 if (!stream->substreams)
89 goto capture_routes;
90
91 dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
92 snd_pcm_direction_name(dir));
93 dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Tx", cname, i);
94 if (!dr[n].sink || !dr[n].source)
95 return -ENOMEM;
96 n++;
97
98 capture_routes:
99 dir = SNDRV_PCM_STREAM_CAPTURE;
100 stream = &pcm->stream[dir];
101 if (!stream->substreams)
102 continue;
103
104 dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Rx", cname, i);
105 dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
106 snd_pcm_direction_name(dir));
107 if (!dr[n].sink || !dr[n].source)
108 return -ENOMEM;
109 n++;
110 }
111
112 *routes = dr;
113 *num_routes = n;
114 return 0;
115 }
116
117 /* Should be aligned with SectionPCM's name from topology */
118 #define FEDAI_NAME_PREFIX "HDMI"
119
120 static struct snd_pcm *
avs_card_hdmi_pcm_at(struct snd_soc_card * card,int hdmi_idx)121 avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
122 {
123 struct snd_soc_pcm_runtime *rtd;
124 int dir = SNDRV_PCM_STREAM_PLAYBACK;
125
126 for_each_card_rtds(card, rtd) {
127 struct snd_pcm *spcm;
128 int ret, n;
129
130 spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
131 if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
132 continue;
133
134 ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
135 if (ret != 1)
136 continue;
137 if (n == hdmi_idx)
138 return rtd->pcm;
139 }
140
141 return NULL;
142 }
143
avs_card_late_probe(struct snd_soc_card * card)144 static int avs_card_late_probe(struct snd_soc_card *card)
145 {
146 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
147 struct hda_codec *codec = mach->pdata;
148 struct hda_pcm *hpcm;
149 /* Topology pcm indexing is 1-based */
150 int i = 1;
151
152 list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
153 struct snd_pcm *spcm;
154
155 spcm = avs_card_hdmi_pcm_at(card, i);
156 if (spcm) {
157 hpcm->pcm = spcm;
158 hpcm->device = spcm->device;
159 dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
160 __func__, i, hpcm->device, spcm);
161 } else {
162 hpcm->pcm = NULL;
163 hpcm->device = SNDRV_PCM_INVALID_DEVICE;
164 dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
165 __func__, i);
166 }
167 i++;
168 }
169
170 return hda_codec_probe_complete(codec);
171 }
172
avs_probing_link_init(struct snd_soc_pcm_runtime * rtm)173 static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
174 {
175 struct snd_soc_dapm_route *routes;
176 struct snd_soc_acpi_mach *mach;
177 struct snd_soc_dai_link *links = NULL;
178 struct snd_soc_card *card = rtm->card;
179 struct hda_codec *codec;
180 struct hda_pcm *pcm;
181 int ret, n, pcm_count = 0;
182
183 mach = dev_get_platdata(card->dev);
184 codec = mach->pdata;
185
186 if (list_empty(&codec->pcm_list_head))
187 return -EINVAL;
188 list_for_each_entry(pcm, &codec->pcm_list_head, list)
189 pcm_count++;
190
191 ret = avs_create_dai_links(card->dev, codec, pcm_count, mach->mach_params.platform, &links);
192 if (ret < 0) {
193 dev_err(card->dev, "create links failed: %d\n", ret);
194 return ret;
195 }
196
197 for (n = 0; n < pcm_count; n++) {
198 ret = snd_soc_add_pcm_runtime(card, &links[n]);
199 if (ret < 0) {
200 dev_err(card->dev, "add links failed: %d\n", ret);
201 return ret;
202 }
203 }
204
205 ret = avs_create_dapm_routes(card->dev, codec, pcm_count, &routes, &n);
206 if (ret < 0) {
207 dev_err(card->dev, "create routes failed: %d\n", ret);
208 return ret;
209 }
210
211 ret = snd_soc_dapm_add_routes(&card->dapm, routes, n);
212 if (ret < 0) {
213 dev_err(card->dev, "add routes failed: %d\n", ret);
214 return ret;
215 }
216
217 return 0;
218 }
219
220 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
221
222 static struct snd_soc_dai_link probing_link = {
223 .name = "probing-LINK",
224 .id = -1,
225 .nonatomic = 1,
226 .no_pcm = 1,
227 .dpcm_playback = 1,
228 .dpcm_capture = 1,
229 .cpus = dummy,
230 .num_cpus = ARRAY_SIZE(dummy),
231 .init = avs_probing_link_init,
232 };
233
avs_hdaudio_probe(struct platform_device * pdev)234 static int avs_hdaudio_probe(struct platform_device *pdev)
235 {
236 struct snd_soc_dai_link *binder;
237 struct snd_soc_acpi_mach *mach;
238 struct snd_soc_card *card;
239 struct device *dev = &pdev->dev;
240 struct hda_codec *codec;
241
242 mach = dev_get_platdata(dev);
243 codec = mach->pdata;
244
245 /* codec may be unloaded before card's probe() fires */
246 if (!device_is_registered(&codec->core.dev))
247 return -ENODEV;
248
249 binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
250 if (!binder)
251 return -ENOMEM;
252
253 binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
254 binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
255 if (!binder->platforms || !binder->codecs)
256 return -ENOMEM;
257
258 binder->codecs->name = devm_kstrdup(dev, dev_name(&codec->core.dev), GFP_KERNEL);
259 if (!binder->codecs->name)
260 return -ENOMEM;
261
262 binder->platforms->name = mach->mach_params.platform;
263 binder->num_platforms = 1;
264 binder->codecs->dai_name = "codec-probing-DAI";
265 binder->num_codecs = 1;
266
267 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
268 if (!card)
269 return -ENOMEM;
270
271 card->name = binder->codecs->name;
272 card->dev = dev;
273 card->owner = THIS_MODULE;
274 card->dai_link = binder;
275 card->num_links = 1;
276 card->fully_routed = true;
277 if (hda_codec_is_display(codec))
278 card->late_probe = avs_card_late_probe;
279
280 return devm_snd_soc_register_card(dev, card);
281 }
282
283 static struct platform_driver avs_hdaudio_driver = {
284 .probe = avs_hdaudio_probe,
285 .driver = {
286 .name = "avs_hdaudio",
287 .pm = &snd_soc_pm_ops,
288 },
289 };
290
291 module_platform_driver(avs_hdaudio_driver)
292
293 MODULE_DESCRIPTION("Intel HD-Audio machine driver");
294 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
295 MODULE_LICENSE("GPL");
296 MODULE_ALIAS("platform:avs_hdaudio");
297