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 <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 #include "../utils.h"
17
avs_link_startup(struct snd_pcm_substream * substream)18 static int avs_link_startup(struct snd_pcm_substream *substream)
19 {
20 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
21 const struct snd_soc_pcm_stream *stream_info;
22 struct snd_soc_dai *codec_dai;
23
24 codec_dai = snd_soc_rtd_to_codec(rtd, 0);
25 stream_info = snd_soc_dai_get_pcm_stream(codec_dai, substream->stream);
26
27 return snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, stream_info->sig_bits);
28 }
29
30 static const struct snd_soc_ops avs_link_ops = {
31 .startup = avs_link_startup,
32 };
33
avs_create_dai_links(struct device * dev,struct hda_codec * codec,int pcm_count,struct snd_soc_dai_link ** links)34 static int avs_create_dai_links(struct device *dev, struct hda_codec *codec, int pcm_count,
35 struct snd_soc_dai_link **links)
36 {
37 struct snd_soc_dai_link_component *platform;
38 struct snd_soc_dai_link *dl;
39 struct hda_pcm *pcm;
40 const char *cname = dev_name(&codec->core.dev);
41 int i;
42
43 dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
44 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
45 if (!dl || !platform)
46 return -ENOMEM;
47
48 platform->name = dev_name(dev);
49 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
50
51 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
52 dl[i].name = devm_kasprintf(dev, GFP_KERNEL, "%s link%d", cname, i);
53 if (!dl[i].name)
54 return -ENOMEM;
55
56 dl[i].id = i;
57 dl[i].nonatomic = 1;
58 dl[i].no_pcm = 1;
59 dl[i].platforms = platform;
60 dl[i].num_platforms = 1;
61 dl[i].ignore_pmdown_time = 1;
62 dl[i].ops = &avs_link_ops;
63
64 dl[i].codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
65 dl[i].cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
66 if (!dl[i].codecs || !dl[i].cpus)
67 return -ENOMEM;
68
69 dl[i].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d", cname, i);
70 if (!dl[i].cpus->dai_name)
71 return -ENOMEM;
72
73 dl[i].codecs->name = devm_kstrdup_const(dev, cname, GFP_KERNEL);
74 if (!dl[i].codecs->name)
75 return -ENOMEM;
76
77 dl[i].codecs->dai_name = pcm->name;
78 dl[i].num_codecs = 1;
79 dl[i].num_cpus = 1;
80 }
81
82 *links = dl;
83 return 0;
84 }
85
86 /* Should be aligned with SectionPCM's name from topology */
87 #define FEDAI_NAME_PREFIX "HDMI"
88
89 static struct snd_pcm *
avs_card_hdmi_pcm_at(struct snd_soc_card * card,int hdmi_idx)90 avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
91 {
92 struct snd_soc_pcm_runtime *rtd;
93 int dir = SNDRV_PCM_STREAM_PLAYBACK;
94
95 for_each_card_rtds(card, rtd) {
96 struct snd_pcm *spcm;
97 int ret, n;
98
99 spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
100 if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
101 continue;
102
103 ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
104 if (ret != 1)
105 continue;
106 if (n == hdmi_idx)
107 return rtd->pcm;
108 }
109
110 return NULL;
111 }
112
avs_card_late_probe(struct snd_soc_card * card)113 static int avs_card_late_probe(struct snd_soc_card *card)
114 {
115 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
116 struct avs_mach_pdata *pdata = mach->pdata;
117 struct hda_codec *codec = pdata->codec;
118 struct hda_pcm *hpcm;
119 /* Topology pcm indexing is 1-based */
120 int i = 1;
121
122 list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
123 struct snd_pcm *spcm;
124
125 spcm = avs_card_hdmi_pcm_at(card, i);
126 if (spcm) {
127 hpcm->pcm = spcm;
128 hpcm->device = spcm->device;
129 dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
130 __func__, i, hpcm->device, spcm);
131 } else {
132 hpcm->pcm = NULL;
133 hpcm->device = SNDRV_PCM_INVALID_DEVICE;
134 dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
135 __func__, i);
136 }
137 i++;
138 }
139
140 return hda_codec_probe_complete(codec);
141 }
142
avs_probing_link_init(struct snd_soc_pcm_runtime * rtm)143 static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
144 {
145 struct snd_soc_acpi_mach *mach;
146 struct avs_mach_pdata *pdata;
147 struct snd_soc_dai_link *links = NULL;
148 struct snd_soc_card *card = rtm->card;
149 struct hda_codec *codec;
150 struct hda_pcm *pcm;
151 int ret, pcm_count = 0;
152
153 mach = dev_get_platdata(card->dev);
154 pdata = mach->pdata;
155 codec = pdata->codec;
156
157 if (list_empty(&codec->pcm_list_head))
158 return -EINVAL;
159 list_for_each_entry(pcm, &codec->pcm_list_head, list)
160 pcm_count++;
161
162 ret = avs_create_dai_links(card->dev, codec, pcm_count, &links);
163 if (ret < 0) {
164 dev_err(card->dev, "create links failed: %d\n", ret);
165 return ret;
166 }
167
168 ret = snd_soc_add_pcm_runtimes(card, links, pcm_count);
169 if (ret < 0) {
170 dev_err(card->dev, "add links failed: %d\n", ret);
171 return ret;
172 }
173
174 return 0;
175 }
176
177 static const struct snd_soc_dai_link probing_link = {
178 .name = "probing-LINK",
179 .id = -1,
180 .nonatomic = 1,
181 .no_pcm = 1,
182 .cpus = &snd_soc_dummy_dlc,
183 .num_cpus = 1,
184 .init = avs_probing_link_init,
185 };
186
avs_hdaudio_probe(struct platform_device * pdev)187 static int avs_hdaudio_probe(struct platform_device *pdev)
188 {
189 struct snd_soc_dai_link *binder;
190 struct snd_soc_acpi_mach *mach;
191 struct avs_mach_pdata *pdata;
192 struct snd_soc_card *card;
193 struct device *dev = &pdev->dev;
194 struct hda_codec *codec;
195
196 mach = dev_get_platdata(dev);
197 pdata = mach->pdata;
198 codec = pdata->codec;
199
200 /* codec may be unloaded before card's probe() fires */
201 if (!device_is_registered(&codec->core.dev))
202 return -ENODEV;
203
204 binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
205 if (!binder)
206 return -ENOMEM;
207
208 binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
209 binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
210 if (!binder->platforms || !binder->codecs)
211 return -ENOMEM;
212
213 binder->codecs->name = devm_kstrdup_const(dev, dev_name(&codec->core.dev), GFP_KERNEL);
214 if (!binder->codecs->name)
215 return -ENOMEM;
216
217 binder->platforms->name = dev_name(dev);
218 binder->num_platforms = 1;
219 binder->codecs->dai_name = "codec-probing-DAI";
220 binder->num_codecs = 1;
221
222 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
223 if (!card)
224 return -ENOMEM;
225
226 if (pdata->obsolete_card_names) {
227 card->name = devm_kasprintf(dev, GFP_KERNEL, "hdaudioB%dD%d", codec->bus->core.idx,
228 codec->core.addr);
229 if (!card->name)
230 return -ENOMEM;
231 } else {
232 card->driver_name = "avs_hdaudio";
233 if (hda_codec_is_display(codec))
234 card->long_name = card->name = "AVS HDMI";
235 else
236 card->long_name = card->name = "AVS HD-Audio";
237 }
238
239 card->dev = dev;
240 card->owner = THIS_MODULE;
241 card->dai_link = binder;
242 card->num_links = 1;
243 card->fully_routed = true;
244 if (hda_codec_is_display(codec))
245 card->late_probe = avs_card_late_probe;
246
247 return devm_snd_soc_register_deferrable_card(dev, card);
248 }
249
250 static const struct platform_device_id avs_hdaudio_driver_ids[] = {
251 { .name = "avs_hdaudio" },
252 { }
253 };
254 MODULE_DEVICE_TABLE(platform, avs_hdaudio_driver_ids);
255
256 static struct platform_driver avs_hdaudio_driver = {
257 .probe = avs_hdaudio_probe,
258 .driver = {
259 .name = "avs_hdaudio",
260 .pm = &snd_soc_pm_ops,
261 },
262 .id_table = avs_hdaudio_driver_ids,
263 };
264
265 module_platform_driver(avs_hdaudio_driver)
266
267 MODULE_DESCRIPTION("Intel HD-Audio machine driver");
268 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
269 MODULE_LICENSE("GPL");
270