1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright(c) 2021-2022 Intel Corporation
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <sound/soc.h>
11 #include <sound/hdaudio_ext.h>
12 #include <sound/hda_i915.h>
13 #include <sound/hda_codec.h>
14 #include "hda.h"
15
hda_codec_create_dais(struct hda_codec * codec,int pcm_count,struct snd_soc_dai_driver ** drivers)16 static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
17 struct snd_soc_dai_driver **drivers)
18 {
19 struct device *dev = &codec->core.dev;
20 struct snd_soc_dai_driver *drvs;
21 struct hda_pcm *pcm;
22 int i;
23
24 drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
25 if (!drvs)
26 return -ENOMEM;
27
28 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
29
30 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
31 struct snd_soc_pcm_stream *stream;
32 int dir;
33
34 dev_info(dev, "creating for %s %d\n", pcm->name, i);
35 drvs[i].id = i;
36 drvs[i].name = pcm->name;
37 drvs[i].ops = &snd_soc_hda_codec_dai_ops;
38
39 dir = SNDRV_PCM_STREAM_PLAYBACK;
40 stream = &drvs[i].playback;
41 if (!pcm->stream[dir].substreams) {
42 dev_info(dev, "skipping playback dai for %s\n", pcm->name);
43 goto capture_dais;
44 }
45
46 stream->stream_name =
47 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
48 snd_pcm_direction_name(dir));
49 if (!stream->stream_name)
50 return -ENOMEM;
51 stream->channels_min = pcm->stream[dir].channels_min;
52 stream->channels_max = pcm->stream[dir].channels_max;
53 stream->rates = pcm->stream[dir].rates;
54 stream->formats = pcm->stream[dir].formats;
55 stream->subformats = pcm->stream[dir].subformats;
56 stream->sig_bits = pcm->stream[dir].maxbps;
57
58 capture_dais:
59 dir = SNDRV_PCM_STREAM_CAPTURE;
60 stream = &drvs[i].capture;
61 if (!pcm->stream[dir].substreams) {
62 dev_info(dev, "skipping capture dai for %s\n", pcm->name);
63 continue;
64 }
65
66 stream->stream_name =
67 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
68 snd_pcm_direction_name(dir));
69 if (!stream->stream_name)
70 return -ENOMEM;
71 stream->channels_min = pcm->stream[dir].channels_min;
72 stream->channels_max = pcm->stream[dir].channels_max;
73 stream->rates = pcm->stream[dir].rates;
74 stream->formats = pcm->stream[dir].formats;
75 stream->subformats = pcm->stream[dir].subformats;
76 stream->sig_bits = pcm->stream[dir].maxbps;
77 }
78
79 *drivers = drvs;
80 return 0;
81 }
82
hda_codec_register_dais(struct hda_codec * codec,struct snd_soc_component * component)83 static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
84 {
85 struct snd_soc_dai_driver *drvs = NULL;
86 struct snd_soc_dapm_context *dapm;
87 struct hda_pcm *pcm;
88 int ret, pcm_count = 0;
89
90 if (list_empty(&codec->pcm_list_head))
91 return -EINVAL;
92 list_for_each_entry(pcm, &codec->pcm_list_head, list)
93 pcm_count++;
94
95 ret = hda_codec_create_dais(codec, pcm_count, &drvs);
96 if (ret < 0)
97 return ret;
98
99 dapm = snd_soc_component_get_dapm(component);
100
101 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
102 struct snd_soc_dai *dai;
103
104 dai = snd_soc_register_dai(component, drvs, false);
105 if (!dai) {
106 dev_err(component->dev, "register dai for %s failed\n", pcm->name);
107 return -EINVAL;
108 }
109
110 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
111 if (ret < 0) {
112 dev_err(component->dev, "create widgets failed: %d\n", ret);
113 snd_soc_unregister_dai(dai);
114 return ret;
115 }
116
117 snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
118 drvs++;
119 }
120
121 return 0;
122 }
123
hda_codec_unregister_dais(struct hda_codec * codec,struct snd_soc_component * component)124 static void hda_codec_unregister_dais(struct hda_codec *codec,
125 struct snd_soc_component *component)
126 {
127 struct snd_soc_dai *dai, *save;
128 struct hda_pcm *pcm;
129
130 for_each_component_dais_safe(component, dai, save) {
131 int stream;
132
133 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
134 if (strcmp(dai->driver->name, pcm->name))
135 continue;
136
137 for_each_pcm_streams(stream)
138 snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
139
140 snd_soc_unregister_dai(dai);
141 break;
142 }
143 }
144 }
145
hda_codec_probe_complete(struct hda_codec * codec)146 int hda_codec_probe_complete(struct hda_codec *codec)
147 {
148 struct hdac_device *hdev = &codec->core;
149 struct hdac_bus *bus = hdev->bus;
150 int ret;
151
152 ret = snd_hda_codec_build_controls(codec);
153 if (ret < 0) {
154 dev_err(&hdev->dev, "unable to create controls %d\n", ret);
155 return ret;
156 }
157
158 /* Bus suspended codecs as it does not manage their pm */
159 pm_runtime_set_active(&hdev->dev);
160 /* rpm was forbidden in snd_hda_codec_device_new() */
161 snd_hda_codec_set_power_save(codec, 2000);
162 snd_hda_codec_register(codec);
163
164 /* Complement pm_runtime_get_sync(bus) in probe */
165 pm_runtime_put_autosuspend(bus->dev);
166
167 return ret;
168 }
169 EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
170
171 /* Expects codec with usage_count=1 and status=suspended */
hda_codec_probe(struct snd_soc_component * component)172 static int hda_codec_probe(struct snd_soc_component *component)
173 {
174 struct hda_codec *codec = dev_to_hda_codec(component->dev);
175 struct hda_codec_driver *driver = hda_codec_to_driver(codec);
176 struct hdac_device *hdev = &codec->core;
177 struct hdac_bus *bus = hdev->bus;
178 struct hdac_ext_link *hlink;
179 int ret;
180
181 #ifdef CONFIG_PM
182 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
183 !pm_runtime_status_suspended(&hdev->dev));
184 #endif
185
186 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
187 if (!hlink) {
188 dev_err(&hdev->dev, "hdac link not found\n");
189 return -EIO;
190 }
191
192 pm_runtime_get_sync(bus->dev);
193 if (hda_codec_is_display(codec))
194 snd_hdac_display_power(bus, hdev->addr, true);
195 snd_hdac_ext_bus_link_get(bus, hlink);
196
197 ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
198 false);
199 if (ret < 0) {
200 dev_err(&hdev->dev, "codec create failed: %d\n", ret);
201 goto device_new_err;
202 }
203
204 ret = snd_hda_codec_set_name(codec, codec->preset->name);
205 if (ret < 0) {
206 dev_err(&hdev->dev, "set name: %s failed: %d\n", codec->preset->name, ret);
207 goto err;
208 }
209
210 ret = snd_hdac_regmap_init(&codec->core);
211 if (ret < 0) {
212 dev_err(&hdev->dev, "regmap init failed: %d\n", ret);
213 goto err;
214 }
215
216 if (WARN_ON(!(driver->ops && driver->ops->probe))) {
217 ret = -EINVAL;
218 goto err;
219 }
220
221 ret = driver->ops->probe(codec, codec->preset);
222 if (ret < 0) {
223 dev_err(&hdev->dev, "codec init failed: %d\n", ret);
224 goto err;
225 }
226
227 ret = snd_hda_codec_parse_pcms(codec);
228 if (ret < 0) {
229 dev_err(&hdev->dev, "unable to map pcms to dai: %d\n", ret);
230 goto parse_pcms_err;
231 }
232
233 ret = hda_codec_register_dais(codec, component);
234 if (ret < 0) {
235 dev_err(&hdev->dev, "update dais failed: %d\n", ret);
236 goto parse_pcms_err;
237 }
238
239 if (!hda_codec_is_display(codec)) {
240 ret = hda_codec_probe_complete(codec);
241 if (ret < 0)
242 goto complete_err;
243 }
244
245 codec->core.lazy_cache = true;
246
247 return 0;
248
249 complete_err:
250 hda_codec_unregister_dais(codec, component);
251 parse_pcms_err:
252 if (driver->ops->remove)
253 driver->ops->remove(codec);
254 err:
255 snd_hda_codec_cleanup_for_unbind(codec);
256 device_new_err:
257 if (hda_codec_is_display(codec))
258 snd_hdac_display_power(bus, hdev->addr, false);
259
260 snd_hdac_ext_bus_link_put(bus, hlink);
261
262 pm_runtime_put_autosuspend(bus->dev);
263 return ret;
264 }
265
266 /* Leaves codec with usage_count=1 and status=suspended */
hda_codec_remove(struct snd_soc_component * component)267 static void hda_codec_remove(struct snd_soc_component *component)
268 {
269 struct hda_codec *codec = dev_to_hda_codec(component->dev);
270 struct hda_codec_driver *driver = hda_codec_to_driver(codec);
271 struct hdac_device *hdev = &codec->core;
272 struct hdac_bus *bus = hdev->bus;
273 struct hdac_ext_link *hlink;
274 bool was_registered = codec->core.registered;
275
276 /* Don't allow any more runtime suspends */
277 pm_runtime_forbid(&hdev->dev);
278
279 hda_codec_unregister_dais(codec, component);
280
281 if (driver->ops->remove)
282 driver->ops->remove(codec);
283
284 snd_hda_codec_cleanup_for_unbind(codec);
285 pm_runtime_put_noidle(&hdev->dev);
286 /* snd_hdac_device_exit() is only called on bus remove */
287 pm_runtime_set_suspended(&hdev->dev);
288
289 if (hda_codec_is_display(codec))
290 snd_hdac_display_power(bus, hdev->addr, false);
291
292 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
293 if (hlink)
294 snd_hdac_ext_bus_link_put(bus, hlink);
295 /*
296 * HDMI card's hda_codec_probe_complete() (see late_probe()) may
297 * not be called due to early error, leaving bus uc unbalanced
298 */
299 if (!was_registered) {
300 pm_runtime_put_autosuspend(bus->dev);
301 }
302
303 #ifdef CONFIG_PM
304 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
305 !pm_runtime_status_suspended(&hdev->dev));
306 #endif
307 }
308
309 static const struct snd_soc_dapm_route hda_dapm_routes[] = {
310 {"AIF1TX", NULL, "Codec Input Pin1"},
311 {"AIF2TX", NULL, "Codec Input Pin2"},
312 {"AIF3TX", NULL, "Codec Input Pin3"},
313
314 {"Codec Output Pin1", NULL, "AIF1RX"},
315 {"Codec Output Pin2", NULL, "AIF2RX"},
316 {"Codec Output Pin3", NULL, "AIF3RX"},
317 };
318
319 static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
320 /* Audio Interface */
321 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
322 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
323 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
324 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
325 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
326 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
327
328 /* Input Pins */
329 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
330 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
331 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
332
333 /* Output Pins */
334 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
335 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
336 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
337 };
338
339 static struct snd_soc_dai_driver card_binder_dai = {
340 .id = -1,
341 .name = "codec-probing-DAI",
342 };
343
hda_hdev_attach(struct hdac_device * hdev)344 static int hda_hdev_attach(struct hdac_device *hdev)
345 {
346 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
347 struct snd_soc_component_driver *comp_drv;
348
349 if (hda_codec_is_display(codec) && !hdev->bus->audio_component) {
350 dev_dbg(&hdev->dev, "no i915, skip registration for 0x%08x\n", hdev->vendor_id);
351 return -ENODEV;
352 }
353
354 comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
355 if (!comp_drv)
356 return -ENOMEM;
357
358 /*
359 * It's save to rely on dev_name() rather than a copy as component
360 * driver's lifetime is directly tied to hda codec one
361 */
362 comp_drv->name = dev_name(&hdev->dev);
363 comp_drv->probe = hda_codec_probe;
364 comp_drv->remove = hda_codec_remove;
365 comp_drv->idle_bias_on = false;
366 if (!hda_codec_is_display(codec)) {
367 comp_drv->dapm_widgets = hda_dapm_widgets;
368 comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
369 comp_drv->dapm_routes = hda_dapm_routes;
370 comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
371 }
372
373 return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
374 }
375
hda_hdev_detach(struct hdac_device * hdev)376 static int hda_hdev_detach(struct hdac_device *hdev)
377 {
378 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
379
380 if (codec->core.registered)
381 cancel_delayed_work_sync(&codec->jackpoll_work);
382
383 snd_soc_unregister_component(&hdev->dev);
384
385 return 0;
386 }
387
388 const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
389 .hdev_attach = hda_hdev_attach,
390 .hdev_detach = hda_hdev_detach,
391 };
392 EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
393
394 MODULE_DESCRIPTION("HD-Audio codec driver");
395 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
396 MODULE_LICENSE("GPL");
397