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;
223 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
224 maxbps = dai->driver->playback.sig_bits;
226 maxbps = dai->driver->capture.sig_bits;
227 bits = snd_hdac_stream_format_bits(params_format(params), SNDRV_PCM_SUBFORMAT_STD, maxbps);
229 hda_pvt = snd_soc_component_get_drvdata(component);
230 format_val = snd_hdac_stream_format(params_channels(params), bits, params_rate(params));
233 "invalid format_val, rate=%d, ch=%d, format=%d, maxbps=%d\n",
234 params_rate(params), params_channels(params),
235 params_format(params), maxbps);
240 hda_pvt->pcm[dai->id].format_val[substream->stream] = format_val;
244 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
245 struct snd_soc_dai *dai)
247 struct snd_soc_component *component = dai->component;
248 struct hdac_hda_priv *hda_pvt;
249 struct hda_pcm_stream *hda_stream;
252 hda_pvt = snd_soc_component_get_drvdata(component);
253 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
257 hda_stream = &pcm->stream[substream->stream];
258 snd_hda_codec_cleanup(hda_pvt->codec, hda_stream, substream);
263 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
264 struct snd_soc_dai *dai)
266 struct snd_soc_component *component = dai->component;
267 struct hda_pcm_stream *hda_stream;
268 struct hdac_hda_priv *hda_pvt;
269 struct hdac_device *hdev;
270 unsigned int format_val;
275 hda_pvt = snd_soc_component_get_drvdata(component);
276 hdev = &hda_pvt->codec->core;
277 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
281 hda_stream = &pcm->stream[substream->stream];
283 stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream];
284 format_val = hda_pvt->pcm[dai->id].format_val[substream->stream];
286 ret = snd_hda_codec_prepare(hda_pvt->codec, hda_stream,
287 stream, format_val, substream);
289 dev_err(&hdev->dev, "codec prepare failed %d\n", ret);
294 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
295 struct snd_soc_dai *dai)
297 struct snd_soc_component *component = dai->component;
298 struct hdac_hda_priv *hda_pvt;
299 struct hda_pcm_stream *hda_stream;
302 hda_pvt = snd_soc_component_get_drvdata(component);
303 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
307 snd_hda_codec_pcm_get(pcm);
309 hda_stream = &pcm->stream[substream->stream];
311 return hda_stream->ops.open(hda_stream, hda_pvt->codec, substream);
314 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
315 struct snd_soc_dai *dai)
317 struct snd_soc_component *component = dai->component;
318 struct hdac_hda_priv *hda_pvt;
319 struct hda_pcm_stream *hda_stream;
322 hda_pvt = snd_soc_component_get_drvdata(component);
323 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
327 hda_stream = &pcm->stream[substream->stream];
329 hda_stream->ops.close(hda_stream, hda_pvt->codec, substream);
331 snd_hda_codec_pcm_put(pcm);
334 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
335 struct snd_soc_dai *dai)
337 struct hda_codec *hcodec = hda_pvt->codec;
338 struct hda_pcm *cpcm;
339 const char *pcm_name;
342 * map DAI ID to the closest matching PCM name, using the naming
343 * scheme used by hda-codec snd_hda_gen_build_pcms() and for
344 * HDMI in hda_codec patch_hdmi.c)
348 case HDAC_ANALOG_DAI_ID:
351 case HDAC_DIGITAL_DAI_ID:
352 pcm_name = "Digital";
354 case HDAC_ALT_ANALOG_DAI_ID:
355 pcm_name = "Alt Analog";
357 case HDAC_HDMI_0_DAI_ID:
360 case HDAC_HDMI_1_DAI_ID:
363 case HDAC_HDMI_2_DAI_ID:
366 case HDAC_HDMI_3_DAI_ID:
370 dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id);
374 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
375 if (strstr(cpcm->name, pcm_name)) {
376 if (strcmp(pcm_name, "Analog") == 0) {
377 if (strstr(cpcm->name, "Alt Analog"))
384 dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name);
388 static bool is_hdmi_codec(struct hda_codec *hcodec)
390 struct hda_pcm *cpcm;
392 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
393 if (cpcm->pcm_type == HDA_PCM_TYPE_HDMI)
400 static int hdac_hda_codec_probe(struct snd_soc_component *component)
402 struct hdac_hda_priv *hda_pvt =
403 snd_soc_component_get_drvdata(component);
404 struct snd_soc_dapm_context *dapm =
405 snd_soc_component_get_dapm(component);
406 struct hdac_device *hdev = &hda_pvt->codec->core;
407 struct hda_codec *hcodec = hda_pvt->codec;
408 struct hdac_ext_link *hlink;
409 hda_codec_patch_t patch;
412 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
414 dev_err(&hdev->dev, "hdac link not found\n");
418 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
421 * Ensure any HDA display is powered at codec probe.
422 * After snd_hda_codec_device_new(), display power is
423 * managed by runtime PM.
425 if (hda_pvt->need_display_power)
426 snd_hdac_display_power(hdev->bus,
427 HDA_CODEC_IDX_CONTROLLER, true);
429 ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card,
430 hdev->addr, hcodec, true);
432 dev_err(&hdev->dev, "failed to create hda codec %d\n", ret);
436 #ifdef CONFIG_SND_HDA_PATCH_LOADER
437 if (loadable_patch[hda_pvt->dev_index] && *loadable_patch[hda_pvt->dev_index]) {
438 const struct firmware *fw;
440 dev_info(&hdev->dev, "Applying patch firmware '%s'\n",
441 loadable_patch[hda_pvt->dev_index]);
442 ret = request_firmware(&fw, loadable_patch[hda_pvt->dev_index],
447 ret = snd_hda_load_patch(hcodec->bus, fw->size, fw->data);
449 dev_err(&hdev->dev, "failed to load hda patch %d\n", ret);
452 release_firmware(fw);
457 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
458 * hda_codec.c will check this flag to determine if unregister
461 hdev->type = HDA_DEV_ASOC;
464 * snd_hda_codec_device_new decrements the usage count so call get pm
465 * else the device will be powered off
467 pm_runtime_get_noresume(&hdev->dev);
469 hcodec->bus->card = dapm->card->snd_card;
471 ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name);
473 dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name);
477 ret = snd_hdac_regmap_init(&hcodec->core);
479 dev_err(&hdev->dev, "regmap init failed\n");
483 patch = (hda_codec_patch_t)hcodec->preset->driver_data;
487 dev_err(&hdev->dev, "patch failed %d\n", ret);
491 dev_dbg(&hdev->dev, "no patch file found\n");
494 ret = snd_hda_codec_parse_pcms(hcodec);
496 dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
500 /* HDMI controls need to be created in machine drivers */
501 if (!is_hdmi_codec(hcodec)) {
502 ret = snd_hda_codec_build_controls(hcodec);
504 dev_err(&hdev->dev, "unable to create controls %d\n",
510 hcodec->core.lazy_cache = true;
512 if (hda_pvt->need_display_power)
513 snd_hdac_display_power(hdev->bus,
514 HDA_CODEC_IDX_CONTROLLER, false);
516 /* match for forbid call in snd_hda_codec_device_new() */
517 pm_runtime_allow(&hdev->dev);
520 * hdac_device core already sets the state to active and calls
521 * get_noresume. So enable runtime and set the device to suspend.
522 * pm_runtime_enable is also called during codec registeration
524 pm_runtime_put(&hdev->dev);
525 pm_runtime_suspend(&hdev->dev);
530 if (hcodec->patch_ops.free)
531 hcodec->patch_ops.free(hcodec);
533 snd_hdac_regmap_exit(hdev);
535 pm_runtime_put(&hdev->dev);
537 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
541 static void hdac_hda_codec_remove(struct snd_soc_component *component)
543 struct hdac_hda_priv *hda_pvt =
544 snd_soc_component_get_drvdata(component);
545 struct hdac_device *hdev = &hda_pvt->codec->core;
546 struct hda_codec *codec = hda_pvt->codec;
547 struct hdac_ext_link *hlink = NULL;
549 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
551 dev_err(&hdev->dev, "hdac link not found\n");
555 pm_runtime_disable(&hdev->dev);
556 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
558 if (codec->patch_ops.free)
559 codec->patch_ops.free(codec);
561 snd_hda_codec_cleanup_for_unbind(codec);
564 static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = {
565 {"AIF1TX", NULL, "Codec Input Pin1"},
566 {"AIF2TX", NULL, "Codec Input Pin2"},
567 {"AIF3TX", NULL, "Codec Input Pin3"},
569 {"Codec Output Pin1", NULL, "AIF1RX"},
570 {"Codec Output Pin2", NULL, "AIF2RX"},
571 {"Codec Output Pin3", NULL, "AIF3RX"},
574 static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = {
575 /* Audio Interface */
576 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0,
578 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0,
580 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0,
582 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0,
584 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0,
586 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0,
590 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
591 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
592 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
595 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
596 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
597 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
600 static const struct snd_soc_component_driver hdac_hda_codec = {
601 .probe = hdac_hda_codec_probe,
602 .remove = hdac_hda_codec_remove,
603 .dapm_widgets = hdac_hda_dapm_widgets,
604 .num_dapm_widgets = ARRAY_SIZE(hdac_hda_dapm_widgets),
605 .dapm_routes = hdac_hda_dapm_routes,
606 .num_dapm_routes = ARRAY_SIZE(hdac_hda_dapm_routes),
607 .idle_bias_on = false,
611 static const struct snd_soc_component_driver hdac_hda_hdmi_codec = {
612 .probe = hdac_hda_codec_probe,
613 .remove = hdac_hda_codec_remove,
614 .idle_bias_on = false,
618 static int hdac_hda_dev_probe(struct hdac_device *hdev)
620 struct hdac_hda_priv *hda_pvt = dev_get_drvdata(&hdev->dev);
621 struct hdac_ext_link *hlink;
624 /* hold the ref while we probe */
625 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
627 dev_err(&hdev->dev, "hdac link not found\n");
630 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
632 /* ASoC specific initialization */
633 if (hda_pvt->need_display_power)
634 ret = devm_snd_soc_register_component(&hdev->dev,
635 &hdac_hda_hdmi_codec, hdac_hda_hdmi_dais,
636 ARRAY_SIZE(hdac_hda_hdmi_dais));
638 ret = devm_snd_soc_register_component(&hdev->dev,
639 &hdac_hda_codec, hdac_hda_dais,
640 ARRAY_SIZE(hdac_hda_dais));
643 dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret);
647 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
652 static int hdac_hda_dev_remove(struct hdac_device *hdev)
655 * Resources are freed in hdac_hda_codec_remove(). This
656 * function is kept to keep hda_codec_driver_remove() happy.
661 static struct hdac_ext_bus_ops hdac_ops = {
662 .hdev_attach = hdac_hda_dev_probe,
663 .hdev_detach = hdac_hda_dev_remove,
666 struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void)
670 EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops);
672 MODULE_LICENSE("GPL v2");
673 MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers");
674 MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>");