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_codec.h>
18 #include <sound/hda_register.h>
21 #define HDAC_ANALOG_DAI_ID 0
22 #define HDAC_DIGITAL_DAI_ID 1
23 #define HDAC_ALT_ANALOG_DAI_ID 2
25 #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
26 SNDRV_PCM_FMTBIT_U8 | \
27 SNDRV_PCM_FMTBIT_S16_LE | \
28 SNDRV_PCM_FMTBIT_U16_LE | \
29 SNDRV_PCM_FMTBIT_S24_LE | \
30 SNDRV_PCM_FMTBIT_U24_LE | \
31 SNDRV_PCM_FMTBIT_S32_LE | \
32 SNDRV_PCM_FMTBIT_U32_LE | \
33 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
35 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
36 struct snd_soc_dai *dai);
37 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
38 struct snd_soc_dai *dai);
39 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
40 struct snd_soc_dai *dai);
41 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
42 struct snd_soc_dai *dai);
43 static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai,
44 unsigned int tx_mask, unsigned int rx_mask,
45 int slots, int slot_width);
46 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
47 struct snd_soc_dai *dai);
49 static struct snd_soc_dai_ops hdac_hda_dai_ops = {
50 .startup = hdac_hda_dai_open,
51 .shutdown = hdac_hda_dai_close,
52 .prepare = hdac_hda_dai_prepare,
53 .hw_free = hdac_hda_dai_hw_free,
54 .set_tdm_slot = hdac_hda_dai_set_tdm_slot,
57 static struct snd_soc_dai_driver hdac_hda_dais[] = {
59 .id = HDAC_ANALOG_DAI_ID,
60 .name = "Analog Codec DAI",
61 .ops = &hdac_hda_dai_ops,
63 .stream_name = "Analog Codec Playback",
66 .rates = SNDRV_PCM_RATE_8000_192000,
67 .formats = STUB_FORMATS,
71 .stream_name = "Analog Codec Capture",
74 .rates = SNDRV_PCM_RATE_8000_192000,
75 .formats = STUB_FORMATS,
80 .id = HDAC_DIGITAL_DAI_ID,
81 .name = "Digital Codec DAI",
82 .ops = &hdac_hda_dai_ops,
84 .stream_name = "Digital Codec Playback",
87 .rates = SNDRV_PCM_RATE_8000_192000,
88 .formats = STUB_FORMATS,
92 .stream_name = "Digital Codec Capture",
95 .rates = SNDRV_PCM_RATE_8000_192000,
96 .formats = STUB_FORMATS,
101 .id = HDAC_ALT_ANALOG_DAI_ID,
102 .name = "Alt Analog Codec DAI",
103 .ops = &hdac_hda_dai_ops,
105 .stream_name = "Alt Analog Codec Playback",
108 .rates = SNDRV_PCM_RATE_8000_192000,
109 .formats = STUB_FORMATS,
113 .stream_name = "Alt Analog Codec Capture",
116 .rates = SNDRV_PCM_RATE_8000_192000,
117 .formats = STUB_FORMATS,
124 static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai,
125 unsigned int tx_mask, unsigned int rx_mask,
126 int slots, int slot_width)
128 struct snd_soc_component *component = dai->component;
129 struct hdac_hda_priv *hda_pvt;
130 struct hdac_hda_pcm *pcm;
132 hda_pvt = snd_soc_component_get_drvdata(component);
133 pcm = &hda_pvt->pcm[dai->id];
135 pcm[dai->id].stream_tag[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask;
137 pcm[dai->id].stream_tag[SNDRV_PCM_STREAM_CAPTURE] = rx_mask;
142 static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream,
143 struct snd_soc_dai *dai)
145 struct snd_soc_component *component = dai->component;
146 struct hdac_hda_priv *hda_pvt;
147 struct hda_pcm_stream *hda_stream;
150 hda_pvt = snd_soc_component_get_drvdata(component);
151 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
155 hda_stream = &pcm->stream[substream->stream];
156 snd_hda_codec_cleanup(&hda_pvt->codec, hda_stream, substream);
161 static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream,
162 struct snd_soc_dai *dai)
164 struct snd_soc_component *component = dai->component;
165 struct hdac_hda_priv *hda_pvt;
166 struct snd_pcm_runtime *runtime = substream->runtime;
167 struct hdac_device *hdev;
168 struct hda_pcm_stream *hda_stream;
169 unsigned int format_val;
174 hda_pvt = snd_soc_component_get_drvdata(component);
175 hdev = &hda_pvt->codec.core;
176 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
180 hda_stream = &pcm->stream[substream->stream];
182 format_val = snd_hdac_calc_stream_format(runtime->rate,
189 "invalid format_val, rate=%d, ch=%d, format=%d\n",
190 runtime->rate, runtime->channels, runtime->format);
194 stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream];
196 ret = snd_hda_codec_prepare(&hda_pvt->codec, hda_stream,
197 stream, format_val, substream);
199 dev_err(&hdev->dev, "codec prepare failed %d\n", ret);
204 static int hdac_hda_dai_open(struct snd_pcm_substream *substream,
205 struct snd_soc_dai *dai)
207 struct snd_soc_component *component = dai->component;
208 struct hdac_hda_priv *hda_pvt;
209 struct hda_pcm_stream *hda_stream;
213 hda_pvt = snd_soc_component_get_drvdata(component);
214 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
218 snd_hda_codec_pcm_get(pcm);
220 hda_stream = &pcm->stream[substream->stream];
222 ret = hda_stream->ops.open(hda_stream, &hda_pvt->codec, substream);
224 snd_hda_codec_pcm_put(pcm);
229 static void hdac_hda_dai_close(struct snd_pcm_substream *substream,
230 struct snd_soc_dai *dai)
232 struct snd_soc_component *component = dai->component;
233 struct hdac_hda_priv *hda_pvt;
234 struct hda_pcm_stream *hda_stream;
237 hda_pvt = snd_soc_component_get_drvdata(component);
238 pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai);
242 hda_stream = &pcm->stream[substream->stream];
244 hda_stream->ops.close(hda_stream, &hda_pvt->codec, substream);
246 snd_hda_codec_pcm_put(pcm);
249 static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt,
250 struct snd_soc_dai *dai)
252 struct hda_codec *hcodec = &hda_pvt->codec;
253 struct hda_pcm *cpcm;
254 const char *pcm_name;
257 case HDAC_ANALOG_DAI_ID:
260 case HDAC_DIGITAL_DAI_ID:
261 pcm_name = "Digital";
263 case HDAC_ALT_ANALOG_DAI_ID:
264 pcm_name = "Alt Analog";
267 dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id);
271 list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) {
272 if (strpbrk(cpcm->name, pcm_name))
276 dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name);
280 static int hdac_hda_codec_probe(struct snd_soc_component *component)
282 struct hdac_hda_priv *hda_pvt =
283 snd_soc_component_get_drvdata(component);
284 struct snd_soc_dapm_context *dapm =
285 snd_soc_component_get_dapm(component);
286 struct hdac_device *hdev = &hda_pvt->codec.core;
287 struct hda_codec *hcodec = &hda_pvt->codec;
288 struct hdac_ext_link *hlink;
289 hda_codec_patch_t patch;
292 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
294 dev_err(&hdev->dev, "hdac link not found\n");
298 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
300 ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card,
303 dev_err(&hdev->dev, "failed to create hda codec %d\n", ret);
308 * snd_hda_codec_device_new decrements the usage count so call get pm
309 * else the device will be powered off
311 pm_runtime_get_noresume(&hdev->dev);
313 hcodec->bus->card = dapm->card->snd_card;
315 ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name);
317 dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name);
321 ret = snd_hdac_regmap_init(&hcodec->core);
323 dev_err(&hdev->dev, "regmap init failed\n");
327 patch = (hda_codec_patch_t)hcodec->preset->driver_data;
331 dev_err(&hdev->dev, "patch failed %d\n", ret);
335 dev_dbg(&hdev->dev, "no patch file found\n");
338 ret = snd_hda_codec_parse_pcms(hcodec);
340 dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
344 ret = snd_hda_codec_build_controls(hcodec);
346 dev_err(&hdev->dev, "unable to create controls %d\n", ret);
350 hcodec->core.lazy_cache = true;
353 * hdac_device core already sets the state to active and calls
354 * get_noresume. So enable runtime and set the device to suspend.
355 * pm_runtime_enable is also called during codec registeration
357 pm_runtime_put(&hdev->dev);
358 pm_runtime_suspend(&hdev->dev);
363 pm_runtime_put(&hdev->dev);
365 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
369 static void hdac_hda_codec_remove(struct snd_soc_component *component)
371 struct hdac_hda_priv *hda_pvt =
372 snd_soc_component_get_drvdata(component);
373 struct hdac_device *hdev = &hda_pvt->codec.core;
374 struct hdac_ext_link *hlink = NULL;
376 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
378 dev_err(&hdev->dev, "hdac link not found\n");
382 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
383 pm_runtime_disable(&hdev->dev);
386 static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = {
387 {"AIF1TX", NULL, "Codec Input Pin1"},
388 {"AIF2TX", NULL, "Codec Input Pin2"},
389 {"AIF3TX", NULL, "Codec Input Pin3"},
391 {"Codec Output Pin1", NULL, "AIF1RX"},
392 {"Codec Output Pin2", NULL, "AIF2RX"},
393 {"Codec Output Pin3", NULL, "AIF3RX"},
396 static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = {
397 /* Audio Interface */
398 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0,
400 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0,
402 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0,
404 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0,
406 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0,
408 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0,
412 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
413 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
414 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
417 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
418 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
419 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
422 static const struct snd_soc_component_driver hdac_hda_codec = {
423 .probe = hdac_hda_codec_probe,
424 .remove = hdac_hda_codec_remove,
425 .idle_bias_on = false,
426 .dapm_widgets = hdac_hda_dapm_widgets,
427 .num_dapm_widgets = ARRAY_SIZE(hdac_hda_dapm_widgets),
428 .dapm_routes = hdac_hda_dapm_routes,
429 .num_dapm_routes = ARRAY_SIZE(hdac_hda_dapm_routes),
432 static int hdac_hda_dev_probe(struct hdac_device *hdev)
434 struct hdac_ext_link *hlink;
435 struct hdac_hda_priv *hda_pvt;
438 /* hold the ref while we probe */
439 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
441 dev_err(&hdev->dev, "hdac link not found\n");
444 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
446 hda_pvt = hdac_to_hda_priv(hdev);
450 /* ASoC specific initialization */
451 ret = devm_snd_soc_register_component(&hdev->dev,
452 &hdac_hda_codec, hdac_hda_dais,
453 ARRAY_SIZE(hdac_hda_dais));
455 dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret);
459 dev_set_drvdata(&hdev->dev, hda_pvt);
460 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
465 static int hdac_hda_dev_remove(struct hdac_device *hdev)
470 static struct hdac_ext_bus_ops hdac_ops = {
471 .hdev_attach = hdac_hda_dev_probe,
472 .hdev_detach = hdac_hda_dev_remove,
475 struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void)
479 EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops);
481 MODULE_LICENSE("GPL v2");
482 MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers");
483 MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>");