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/init.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 #include <sound/hdaudio_ext.h>
17 #include <sound/hda_i915.h>
18 #include <sound/hda_codec.h>
19 #include <sound/hda_register.h>
23 #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
24 SNDRV_PCM_FMTBIT_U8 | \
25 SNDRV_PCM_FMTBIT_S16_LE | \
26 SNDRV_PCM_FMTBIT_U16_LE | \
27 SNDRV_PCM_FMTBIT_S24_LE | \
28 SNDRV_PCM_FMTBIT_U24_LE | \
29 SNDRV_PCM_FMTBIT_S32_LE | \
30 SNDRV_PCM_FMTBIT_U32_LE | \
31 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
33 #define STUB_HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
34 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
35 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
36 SNDRV_PCM_RATE_192000)
38 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
39 struct snd_soc_dai *dai);
40 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
41 struct snd_soc_dai *dai);
42 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
43 struct snd_soc_dai *dai);
44 static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream,
45 struct snd_pcm_hw_params *params,
46 struct snd_soc_dai *dai);
47 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
48 struct snd_soc_dai *dai);
49 static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai,
50 unsigned int tx_mask, unsigned int rx_mask,
51 int slots, int slot_width);
52 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
53 struct snd_soc_dai *dai);
55 static const struct snd_soc_dai_ops hdac_hda_dai_ops = {
56 .startup = hdac_hda_dai_open,
57 .shutdown = hdac_hda_dai_close,
58 .prepare = hdac_hda_dai_prepare,
59 .hw_params = hdac_hda_dai_hw_params,
60 .hw_free = hdac_hda_dai_hw_free,
61 .set_tdm_slot = hdac_hda_dai_set_tdm_slot,
64 static struct snd_soc_dai_driver hdac_hda_dais[] = {
66 .id = HDAC_ANALOG_DAI_ID,
67 .name = "Analog Codec DAI",
68 .ops = &hdac_hda_dai_ops,
70 .stream_name = "Analog Codec Playback",
73 .rates = SNDRV_PCM_RATE_8000_192000,
74 .formats = STUB_FORMATS,
78 .stream_name = "Analog Codec Capture",
81 .rates = SNDRV_PCM_RATE_8000_192000,
82 .formats = STUB_FORMATS,
87 .id = HDAC_DIGITAL_DAI_ID,
88 .name = "Digital Codec DAI",
89 .ops = &hdac_hda_dai_ops,
91 .stream_name = "Digital Codec Playback",
94 .rates = SNDRV_PCM_RATE_8000_192000,
95 .formats = STUB_FORMATS,
99 .stream_name = "Digital Codec Capture",
102 .rates = SNDRV_PCM_RATE_8000_192000,
103 .formats = STUB_FORMATS,
108 .id = HDAC_ALT_ANALOG_DAI_ID,
109 .name = "Alt Analog Codec DAI",
110 .ops = &hdac_hda_dai_ops,
112 .stream_name = "Alt Analog Codec Playback",
115 .rates = SNDRV_PCM_RATE_8000_192000,
116 .formats = STUB_FORMATS,
120 .stream_name = "Alt Analog Codec Capture",
123 .rates = SNDRV_PCM_RATE_8000_192000,
124 .formats = STUB_FORMATS,
129 .id = HDAC_HDMI_0_DAI_ID,
130 .name = "intel-hdmi-hifi1",
131 .ops = &hdac_hda_dai_ops,
133 .stream_name = "hifi1",
136 .rates = STUB_HDMI_RATES,
137 .formats = STUB_FORMATS,
142 .id = HDAC_HDMI_1_DAI_ID,
143 .name = "intel-hdmi-hifi2",
144 .ops = &hdac_hda_dai_ops,
146 .stream_name = "hifi2",
149 .rates = STUB_HDMI_RATES,
150 .formats = STUB_FORMATS,
155 .id = HDAC_HDMI_2_DAI_ID,
156 .name = "intel-hdmi-hifi3",
157 .ops = &hdac_hda_dai_ops,
159 .stream_name = "hifi3",
162 .rates = STUB_HDMI_RATES,
163 .formats = STUB_FORMATS,
168 .id = HDAC_HDMI_3_DAI_ID,
169 .name = "intel-hdmi-hifi4",
170 .ops = &hdac_hda_dai_ops,
172 .stream_name = "hifi4",
175 .rates = STUB_HDMI_RATES,
176 .formats = STUB_FORMATS,
183 static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai,
184 unsigned int tx_mask, unsigned int rx_mask,
185 int slots, int slot_width)
187 struct snd_soc_component *component = dai->component;
188 struct hdac_hda_priv *hda_pvt;
189 struct hdac_hda_pcm *pcm;
191 hda_pvt = snd_soc_component_get_drvdata(component);
192 pcm = &hda_pvt->pcm[dai->id];
195 pcm->stream_tag[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
197 pcm->stream_tag[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
202 static int hdac_hda_dai_hw_params(struct snd_pcm_substream *substream,
203 struct snd_pcm_hw_params *params,
204 struct snd_soc_dai *dai)
206 struct snd_soc_component *component = dai->component;
207 struct hdac_hda_priv *hda_pvt;
208 unsigned int format_val;
211 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
212 maxbps = dai->driver->playback.sig_bits;
214 maxbps = dai->driver->capture.sig_bits;
216 hda_pvt = snd_soc_component_get_drvdata(component);
217 format_val = snd_hdac_calc_stream_format(params_rate(params),
218 params_channels(params),
219 params_format(params),
224 "invalid format_val, rate=%d, ch=%d, format=%d, maxbps=%d\n",
225 params_rate(params), params_channels(params),
226 params_format(params), maxbps);
231 hda_pvt->pcm[dai->id].format_val[substream->stream] = format_val;
235 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
236 struct snd_soc_dai *dai)
238 struct snd_soc_component *component = dai->component;
239 struct hdac_hda_priv *hda_pvt;
240 struct hda_pcm_stream *hda_stream;
243 hda_pvt = snd_soc_component_get_drvdata(component);
244 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
248 hda_stream = &pcm->stream[substream->stream];
249 snd_hda_codec_cleanup(&hda_pvt->codec, hda_stream, substream);
254 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
255 struct snd_soc_dai *dai)
257 struct snd_soc_component *component = dai->component;
258 struct hda_pcm_stream *hda_stream;
259 struct hdac_hda_priv *hda_pvt;
260 struct hdac_device *hdev;
261 unsigned int format_val;
266 hda_pvt = snd_soc_component_get_drvdata(component);
267 hdev = &hda_pvt->codec.core;
268 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
272 hda_stream = &pcm->stream[substream->stream];
274 stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream];
275 format_val = hda_pvt->pcm[dai->id].format_val[substream->stream];
277 ret = snd_hda_codec_prepare(&hda_pvt->codec, hda_stream,
278 stream, format_val, substream);
280 dev_err(&hdev->dev, "codec prepare failed %d\n", ret);
285 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
286 struct snd_soc_dai *dai)
288 struct snd_soc_component *component = dai->component;
289 struct hdac_hda_priv *hda_pvt;
290 struct hda_pcm_stream *hda_stream;
294 hda_pvt = snd_soc_component_get_drvdata(component);
295 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
299 snd_hda_codec_pcm_get(pcm);
301 hda_stream = &pcm->stream[substream->stream];
303 ret = hda_stream->ops.open(hda_stream, &hda_pvt->codec, substream);
305 snd_hda_codec_pcm_put(pcm);
310 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
311 struct snd_soc_dai *dai)
313 struct snd_soc_component *component = dai->component;
314 struct hdac_hda_priv *hda_pvt;
315 struct hda_pcm_stream *hda_stream;
318 hda_pvt = snd_soc_component_get_drvdata(component);
319 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
323 hda_stream = &pcm->stream[substream->stream];
325 hda_stream->ops.close(hda_stream, &hda_pvt->codec, substream);
327 snd_hda_codec_pcm_put(pcm);
330 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
331 struct snd_soc_dai *dai)
333 struct hda_codec *hcodec = &hda_pvt->codec;
334 struct hda_pcm *cpcm;
335 const char *pcm_name;
338 * map DAI ID to the closest matching PCM name, using the naming
339 * scheme used by hda-codec snd_hda_gen_build_pcms() and for
340 * HDMI in hda_codec patch_hdmi.c)
344 case HDAC_ANALOG_DAI_ID:
347 case HDAC_DIGITAL_DAI_ID:
348 pcm_name = "Digital";
350 case HDAC_ALT_ANALOG_DAI_ID:
351 pcm_name = "Alt Analog";
353 case HDAC_HDMI_0_DAI_ID:
356 case HDAC_HDMI_1_DAI_ID:
359 case HDAC_HDMI_2_DAI_ID:
362 case HDAC_HDMI_3_DAI_ID:
366 dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id);
370 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
371 if (strstr(cpcm->name, pcm_name))
375 dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name);
379 static bool is_hdmi_codec(struct hda_codec *hcodec)
381 struct hda_pcm *cpcm;
383 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
384 if (cpcm->pcm_type == HDA_PCM_TYPE_HDMI)
391 static int hdac_hda_codec_probe(struct snd_soc_component *component)
393 struct hdac_hda_priv *hda_pvt =
394 snd_soc_component_get_drvdata(component);
395 struct snd_soc_dapm_context *dapm =
396 snd_soc_component_get_dapm(component);
397 struct hdac_device *hdev = &hda_pvt->codec.core;
398 struct hda_codec *hcodec = &hda_pvt->codec;
399 struct hdac_ext_link *hlink;
400 hda_codec_patch_t patch;
403 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
405 dev_err(&hdev->dev, "hdac link not found\n");
409 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
412 * Ensure any HDA display is powered at codec probe.
413 * After snd_hda_codec_device_new(), display power is
414 * managed by runtime PM.
416 if (hda_pvt->need_display_power)
417 snd_hdac_display_power(hdev->bus,
418 HDA_CODEC_IDX_CONTROLLER, true);
420 ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card,
423 dev_err(&hdev->dev, "failed to create hda codec %d\n", ret);
427 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
428 * hda_codec.c will check this flag to determine if unregister
431 hdev->type = HDA_DEV_ASOC;
434 * snd_hda_codec_device_new decrements the usage count so call get pm
435 * else the device will be powered off
437 pm_runtime_get_noresume(&hdev->dev);
439 hcodec->bus->card = dapm->card->snd_card;
441 ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name);
443 dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name);
447 ret = snd_hdac_regmap_init(&hcodec->core);
449 dev_err(&hdev->dev, "regmap init failed\n");
453 patch = (hda_codec_patch_t)hcodec->preset->driver_data;
457 dev_err(&hdev->dev, "patch failed %d\n", ret);
461 dev_dbg(&hdev->dev, "no patch file found\n");
464 /* configure codec for 1:1 PCM:DAI mapping */
465 hcodec->mst_no_extra_pcms = 1;
467 ret = snd_hda_codec_parse_pcms(hcodec);
469 dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
473 /* HDMI controls need to be created in machine drivers */
474 if (!is_hdmi_codec(hcodec)) {
475 ret = snd_hda_codec_build_controls(hcodec);
477 dev_err(&hdev->dev, "unable to create controls %d\n",
483 hcodec->core.lazy_cache = true;
485 if (hda_pvt->need_display_power)
486 snd_hdac_display_power(hdev->bus,
487 HDA_CODEC_IDX_CONTROLLER, false);
490 * hdac_device core already sets the state to active and calls
491 * get_noresume. So enable runtime and set the device to suspend.
492 * pm_runtime_enable is also called during codec registeration
494 pm_runtime_put(&hdev->dev);
495 pm_runtime_suspend(&hdev->dev);
500 if (hcodec->patch_ops.free)
501 hcodec->patch_ops.free(hcodec);
503 snd_hdac_regmap_exit(hdev);
505 pm_runtime_put(&hdev->dev);
507 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
511 static void hdac_hda_codec_remove(struct snd_soc_component *component)
513 struct hdac_hda_priv *hda_pvt =
514 snd_soc_component_get_drvdata(component);
515 struct hdac_device *hdev = &hda_pvt->codec.core;
516 struct hdac_ext_link *hlink = NULL;
518 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
520 dev_err(&hdev->dev, "hdac link not found\n");
524 pm_runtime_disable(&hdev->dev);
525 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
527 snd_hdac_regmap_exit(hdev);
530 static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = {
531 {"AIF1TX", NULL, "Codec Input Pin1"},
532 {"AIF2TX", NULL, "Codec Input Pin2"},
533 {"AIF3TX", NULL, "Codec Input Pin3"},
535 {"Codec Output Pin1", NULL, "AIF1RX"},
536 {"Codec Output Pin2", NULL, "AIF2RX"},
537 {"Codec Output Pin3", NULL, "AIF3RX"},
540 static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = {
541 /* Audio Interface */
542 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0,
544 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0,
546 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0,
548 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0,
550 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0,
552 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0,
556 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
557 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
558 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
561 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
562 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
563 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
566 static const struct snd_soc_component_driver hdac_hda_codec = {
567 .probe = hdac_hda_codec_probe,
568 .remove = hdac_hda_codec_remove,
569 .idle_bias_on = false,
570 .dapm_widgets = hdac_hda_dapm_widgets,
571 .num_dapm_widgets = ARRAY_SIZE(hdac_hda_dapm_widgets),
572 .dapm_routes = hdac_hda_dapm_routes,
573 .num_dapm_routes = ARRAY_SIZE(hdac_hda_dapm_routes),
576 static int hdac_hda_dev_probe(struct hdac_device *hdev)
578 struct hdac_ext_link *hlink;
579 struct hdac_hda_priv *hda_pvt;
582 /* hold the ref while we probe */
583 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
585 dev_err(&hdev->dev, "hdac link not found\n");
588 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
590 hda_pvt = hdac_to_hda_priv(hdev);
594 /* ASoC specific initialization */
595 ret = devm_snd_soc_register_component(&hdev->dev,
596 &hdac_hda_codec, hdac_hda_dais,
597 ARRAY_SIZE(hdac_hda_dais));
599 dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret);
603 dev_set_drvdata(&hdev->dev, hda_pvt);
604 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
609 static int hdac_hda_dev_remove(struct hdac_device *hdev)
611 struct hdac_hda_priv *hda_pvt;
613 hda_pvt = dev_get_drvdata(&hdev->dev);
614 if (hda_pvt && hda_pvt->codec.registered)
615 cancel_delayed_work_sync(&hda_pvt->codec.jackpoll_work);
620 static struct hdac_ext_bus_ops hdac_ops = {
621 .hdev_attach = hdac_hda_dev_probe,
622 .hdev_detach = hdac_hda_dev_remove,
625 struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void)
629 EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops);
631 MODULE_LICENSE("GPL v2");
632 MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers");
633 MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>");