1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2015-18 Intel Corporation.
5 * hdac_hda.c - ASoC extensions to reuse the legacy HDA codec drivers
6 * with ASoC platform drivers. These APIs are called by the legacy HDA
7 * codec drivers using hdac_ext_bus_ops ops.
10 #include <linux/firmware.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17 #include <sound/hdaudio_ext.h>
18 #include <sound/hda_i915.h>
19 #include <sound/hda_codec.h>
20 #include <sound/hda_register.h>
24 #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
25 SNDRV_PCM_FMTBIT_U8 | \
26 SNDRV_PCM_FMTBIT_S16_LE | \
27 SNDRV_PCM_FMTBIT_U16_LE | \
28 SNDRV_PCM_FMTBIT_S24_LE | \
29 SNDRV_PCM_FMTBIT_U24_LE | \
30 SNDRV_PCM_FMTBIT_S32_LE | \
31 SNDRV_PCM_FMTBIT_U32_LE | \
32 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
34 #define STUB_HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
35 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
36 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
37 SNDRV_PCM_RATE_192000)
39 #ifdef CONFIG_SND_HDA_PATCH_LOADER
40 static char *loadable_patch[HDA_MAX_CODECS];
42 module_param_array_named(patch, loadable_patch, charp, NULL, 0444);
43 MODULE_PARM_DESC(patch, "Patch file array for Intel HD audio interface. The array index is the codec address.");
46 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
47 struct snd_soc_dai *dai);
48 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
49 struct snd_soc_dai *dai);
50 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
51 struct snd_soc_dai *dai);
52 static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params,
54 struct snd_soc_dai *dai);
55 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
56 struct snd_soc_dai *dai);
57 static int hdac_hda_dai_set_stream(struct snd_soc_dai *dai, void *stream,
59 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
60 struct snd_soc_dai *dai);
62 static const struct snd_soc_dai_ops hdac_hda_dai_ops = {
63 .startup = hdac_hda_dai_open,
64 .shutdown = hdac_hda_dai_close,
65 .prepare = hdac_hda_dai_prepare,
66 .hw_params = hdac_hda_dai_hw_params,
67 .hw_free = hdac_hda_dai_hw_free,
68 .set_stream = hdac_hda_dai_set_stream,
71 static struct snd_soc_dai_driver hdac_hda_dais[] = {
73 .id = HDAC_ANALOG_DAI_ID,
74 .name = "Analog Codec DAI",
75 .ops = &hdac_hda_dai_ops,
77 .stream_name = "Analog Codec Playback",
80 .rates = SNDRV_PCM_RATE_8000_192000,
81 .formats = STUB_FORMATS,
85 .stream_name = "Analog Codec Capture",
88 .rates = SNDRV_PCM_RATE_8000_192000,
89 .formats = STUB_FORMATS,
94 .id = HDAC_DIGITAL_DAI_ID,
95 .name = "Digital Codec DAI",
96 .ops = &hdac_hda_dai_ops,
98 .stream_name = "Digital Codec Playback",
101 .rates = SNDRV_PCM_RATE_8000_192000,
102 .formats = STUB_FORMATS,
106 .stream_name = "Digital Codec Capture",
109 .rates = SNDRV_PCM_RATE_8000_192000,
110 .formats = STUB_FORMATS,
115 .id = HDAC_ALT_ANALOG_DAI_ID,
116 .name = "Alt Analog Codec DAI",
117 .ops = &hdac_hda_dai_ops,
119 .stream_name = "Alt Analog Codec Playback",
122 .rates = SNDRV_PCM_RATE_8000_192000,
123 .formats = STUB_FORMATS,
127 .stream_name = "Alt Analog Codec Capture",
130 .rates = SNDRV_PCM_RATE_8000_192000,
131 .formats = STUB_FORMATS,
137 static struct snd_soc_dai_driver hdac_hda_hdmi_dais[] = {
139 .id = HDAC_HDMI_0_DAI_ID,
140 .name = "intel-hdmi-hifi1",
141 .ops = &hdac_hda_dai_ops,
143 .stream_name = "hifi1",
146 .rates = STUB_HDMI_RATES,
147 .formats = STUB_FORMATS,
152 .id = HDAC_HDMI_1_DAI_ID,
153 .name = "intel-hdmi-hifi2",
154 .ops = &hdac_hda_dai_ops,
156 .stream_name = "hifi2",
159 .rates = STUB_HDMI_RATES,
160 .formats = STUB_FORMATS,
165 .id = HDAC_HDMI_2_DAI_ID,
166 .name = "intel-hdmi-hifi3",
167 .ops = &hdac_hda_dai_ops,
169 .stream_name = "hifi3",
172 .rates = STUB_HDMI_RATES,
173 .formats = STUB_FORMATS,
178 .id = HDAC_HDMI_3_DAI_ID,
179 .name = "intel-hdmi-hifi4",
180 .ops = &hdac_hda_dai_ops,
182 .stream_name = "hifi4",
185 .rates = STUB_HDMI_RATES,
186 .formats = STUB_FORMATS,
193 static int hdac_hda_dai_set_stream(struct snd_soc_dai *dai,
194 void *stream, int direction)
196 struct snd_soc_component *component = dai->component;
197 struct hdac_hda_priv *hda_pvt;
198 struct hdac_hda_pcm *pcm;
199 struct hdac_stream *hstream;
204 hda_pvt = snd_soc_component_get_drvdata(component);
205 pcm = &hda_pvt->pcm[dai->id];
206 hstream = (struct hdac_stream *)stream;
208 pcm->stream_tag[direction] = hstream->stream_tag;
213 static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream,
214 struct snd_pcm_hw_params *params,
215 struct snd_soc_dai *dai)
217 struct snd_soc_component *component = dai->component;
218 struct hdac_hda_priv *hda_pvt;
219 unsigned int format_val;
222 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
223 maxbps = dai->driver->playback.sig_bits;
225 maxbps = dai->driver->capture.sig_bits;
227 hda_pvt = snd_soc_component_get_drvdata(component);
228 format_val = snd_hdac_calc_stream_format(params_rate(params),
229 params_channels(params),
230 params_format(params),
235 "invalid format_val, rate=%d, ch=%d, format=%d, maxbps=%d\n",
236 params_rate(params), params_channels(params),
237 params_format(params), maxbps);
242 hda_pvt->pcm[dai->id].format_val[substream->stream] = format_val;
246 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
247 struct snd_soc_dai *dai)
249 struct snd_soc_component *component = dai->component;
250 struct hdac_hda_priv *hda_pvt;
251 struct hda_pcm_stream *hda_stream;
254 hda_pvt = snd_soc_component_get_drvdata(component);
255 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
259 hda_stream = &pcm->stream[substream->stream];
260 snd_hda_codec_cleanup(hda_pvt->codec, hda_stream, substream);
265 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
266 struct snd_soc_dai *dai)
268 struct snd_soc_component *component = dai->component;
269 struct hda_pcm_stream *hda_stream;
270 struct hdac_hda_priv *hda_pvt;
271 struct hdac_device *hdev;
272 unsigned int format_val;
277 hda_pvt = snd_soc_component_get_drvdata(component);
278 hdev = &hda_pvt->codec->core;
279 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
283 hda_stream = &pcm->stream[substream->stream];
285 stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream];
286 format_val = hda_pvt->pcm[dai->id].format_val[substream->stream];
288 ret = snd_hda_codec_prepare(hda_pvt->codec, hda_stream,
289 stream, format_val, substream);
291 dev_err(&hdev->dev, "codec prepare failed %d\n", ret);
296 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
297 struct snd_soc_dai *dai)
299 struct snd_soc_component *component = dai->component;
300 struct hdac_hda_priv *hda_pvt;
301 struct hda_pcm_stream *hda_stream;
304 hda_pvt = snd_soc_component_get_drvdata(component);
305 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
309 snd_hda_codec_pcm_get(pcm);
311 hda_stream = &pcm->stream[substream->stream];
313 return hda_stream->ops.open(hda_stream, hda_pvt->codec, substream);
316 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
317 struct snd_soc_dai *dai)
319 struct snd_soc_component *component = dai->component;
320 struct hdac_hda_priv *hda_pvt;
321 struct hda_pcm_stream *hda_stream;
324 hda_pvt = snd_soc_component_get_drvdata(component);
325 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
329 hda_stream = &pcm->stream[substream->stream];
331 hda_stream->ops.close(hda_stream, hda_pvt->codec, substream);
333 snd_hda_codec_pcm_put(pcm);
336 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
337 struct snd_soc_dai *dai)
339 struct hda_codec *hcodec = hda_pvt->codec;
340 struct hda_pcm *cpcm;
341 const char *pcm_name;
344 * map DAI ID to the closest matching PCM name, using the naming
345 * scheme used by hda-codec snd_hda_gen_build_pcms() and for
346 * HDMI in hda_codec patch_hdmi.c)
350 case HDAC_ANALOG_DAI_ID:
353 case HDAC_DIGITAL_DAI_ID:
354 pcm_name = "Digital";
356 case HDAC_ALT_ANALOG_DAI_ID:
357 pcm_name = "Alt Analog";
359 case HDAC_HDMI_0_DAI_ID:
362 case HDAC_HDMI_1_DAI_ID:
365 case HDAC_HDMI_2_DAI_ID:
368 case HDAC_HDMI_3_DAI_ID:
372 dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id);
376 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
377 if (strstr(cpcm->name, pcm_name)) {
378 if (strcmp(pcm_name, "Analog") == 0) {
379 if (strstr(cpcm->name, "Alt Analog"))
386 dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name);
390 static bool is_hdmi_codec(struct hda_codec *hcodec)
392 struct hda_pcm *cpcm;
394 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
395 if (cpcm->pcm_type == HDA_PCM_TYPE_HDMI)
402 static int hdac_hda_codec_probe(struct snd_soc_component *component)
404 struct hdac_hda_priv *hda_pvt =
405 snd_soc_component_get_drvdata(component);
406 struct snd_soc_dapm_context *dapm =
407 snd_soc_component_get_dapm(component);
408 struct hdac_device *hdev = &hda_pvt->codec->core;
409 struct hda_codec *hcodec = hda_pvt->codec;
410 struct hdac_ext_link *hlink;
411 hda_codec_patch_t patch;
414 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
416 dev_err(&hdev->dev, "hdac link not found\n");
420 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
423 * Ensure any HDA display is powered at codec probe.
424 * After snd_hda_codec_device_new(), display power is
425 * managed by runtime PM.
427 if (hda_pvt->need_display_power)
428 snd_hdac_display_power(hdev->bus,
429 HDA_CODEC_IDX_CONTROLLER, true);
431 ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card,
432 hdev->addr, hcodec, true);
434 dev_err(&hdev->dev, "failed to create hda codec %d\n", ret);
438 #ifdef CONFIG_SND_HDA_PATCH_LOADER
439 if (loadable_patch[hda_pvt->dev_index] && *loadable_patch[hda_pvt->dev_index]) {
440 const struct firmware *fw;
442 dev_info(&hdev->dev, "Applying patch firmware '%s'\n",
443 loadable_patch[hda_pvt->dev_index]);
444 ret = request_firmware(&fw, loadable_patch[hda_pvt->dev_index],
449 ret = snd_hda_load_patch(hcodec->bus, fw->size, fw->data);
451 dev_err(&hdev->dev, "failed to load hda patch %d\n", ret);
454 release_firmware(fw);
459 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
460 * hda_codec.c will check this flag to determine if unregister
463 hdev->type = HDA_DEV_ASOC;
466 * snd_hda_codec_device_new decrements the usage count so call get pm
467 * else the device will be powered off
469 pm_runtime_get_noresume(&hdev->dev);
471 hcodec->bus->card = dapm->card->snd_card;
473 ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name);
475 dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name);
479 ret = snd_hdac_regmap_init(&hcodec->core);
481 dev_err(&hdev->dev, "regmap init failed\n");
485 patch = (hda_codec_patch_t)hcodec->preset->driver_data;
489 dev_err(&hdev->dev, "patch failed %d\n", ret);
493 dev_dbg(&hdev->dev, "no patch file found\n");
496 ret = snd_hda_codec_parse_pcms(hcodec);
498 dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
502 /* HDMI controls need to be created in machine drivers */
503 if (!is_hdmi_codec(hcodec)) {
504 ret = snd_hda_codec_build_controls(hcodec);
506 dev_err(&hdev->dev, "unable to create controls %d\n",
512 hcodec->core.lazy_cache = true;
514 if (hda_pvt->need_display_power)
515 snd_hdac_display_power(hdev->bus,
516 HDA_CODEC_IDX_CONTROLLER, false);
518 /* match for forbid call in snd_hda_codec_device_new() */
519 pm_runtime_allow(&hdev->dev);
522 * hdac_device core already sets the state to active and calls
523 * get_noresume. So enable runtime and set the device to suspend.
524 * pm_runtime_enable is also called during codec registeration
526 pm_runtime_put(&hdev->dev);
527 pm_runtime_suspend(&hdev->dev);
532 if (hcodec->patch_ops.free)
533 hcodec->patch_ops.free(hcodec);
535 snd_hdac_regmap_exit(hdev);
537 pm_runtime_put(&hdev->dev);
539 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
543 static void hdac_hda_codec_remove(struct snd_soc_component *component)
545 struct hdac_hda_priv *hda_pvt =
546 snd_soc_component_get_drvdata(component);
547 struct hdac_device *hdev = &hda_pvt->codec->core;
548 struct hda_codec *codec = hda_pvt->codec;
549 struct hdac_ext_link *hlink = NULL;
551 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
553 dev_err(&hdev->dev, "hdac link not found\n");
557 pm_runtime_disable(&hdev->dev);
558 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
560 if (codec->patch_ops.free)
561 codec->patch_ops.free(codec);
563 snd_hda_codec_cleanup_for_unbind(codec);
566 static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = {
567 {"AIF1TX", NULL, "Codec Input Pin1"},
568 {"AIF2TX", NULL, "Codec Input Pin2"},
569 {"AIF3TX", NULL, "Codec Input Pin3"},
571 {"Codec Output Pin1", NULL, "AIF1RX"},
572 {"Codec Output Pin2", NULL, "AIF2RX"},
573 {"Codec Output Pin3", NULL, "AIF3RX"},
576 static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = {
577 /* Audio Interface */
578 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0,
580 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0,
582 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0,
584 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0,
586 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0,
588 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0,
592 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
593 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
594 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
597 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
598 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
599 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
602 static const struct snd_soc_component_driver hdac_hda_codec = {
603 .probe = hdac_hda_codec_probe,
604 .remove = hdac_hda_codec_remove,
605 .dapm_widgets = hdac_hda_dapm_widgets,
606 .num_dapm_widgets = ARRAY_SIZE(hdac_hda_dapm_widgets),
607 .dapm_routes = hdac_hda_dapm_routes,
608 .num_dapm_routes = ARRAY_SIZE(hdac_hda_dapm_routes),
609 .idle_bias_on = false,
613 static const struct snd_soc_component_driver hdac_hda_hdmi_codec = {
614 .probe = hdac_hda_codec_probe,
615 .remove = hdac_hda_codec_remove,
616 .idle_bias_on = false,
620 static int hdac_hda_dev_probe(struct hdac_device *hdev)
622 struct hdac_hda_priv *hda_pvt = dev_get_drvdata(&hdev->dev);
623 struct hdac_ext_link *hlink;
626 /* hold the ref while we probe */
627 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
629 dev_err(&hdev->dev, "hdac link not found\n");
632 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
634 /* ASoC specific initialization */
635 if (hda_pvt->need_display_power)
636 ret = devm_snd_soc_register_component(&hdev->dev,
637 &hdac_hda_hdmi_codec, hdac_hda_hdmi_dais,
638 ARRAY_SIZE(hdac_hda_hdmi_dais));
640 ret = devm_snd_soc_register_component(&hdev->dev,
641 &hdac_hda_codec, hdac_hda_dais,
642 ARRAY_SIZE(hdac_hda_dais));
645 dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret);
649 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
654 static int hdac_hda_dev_remove(struct hdac_device *hdev)
657 * Resources are freed in hdac_hda_codec_remove(). This
658 * function is kept to keep hda_codec_driver_remove() happy.
663 static struct hdac_ext_bus_ops hdac_ops = {
664 .hdev_attach = hdac_hda_dev_probe,
665 .hdev_detach = hdac_hda_dev_remove,
668 struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void)
672 EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops);
674 MODULE_LICENSE("GPL v2");
675 MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers");
676 MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>");