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_params(struct snd_pcm_substream
*substream
,
42 struct snd_pcm_hw_params
*params
,
43 struct snd_soc_dai
*dai
);
44 static int hdac_hda_dai_hw_free(struct snd_pcm_substream
*substream
,
45 struct snd_soc_dai
*dai
);
46 static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
47 unsigned int tx_mask
, unsigned int rx_mask
,
48 int slots
, int slot_width
);
49 static struct hda_pcm
*snd_soc_find_pcm_from_dai(struct hdac_hda_priv
*hda_pvt
,
50 struct snd_soc_dai
*dai
);
52 static const struct snd_soc_dai_ops hdac_hda_dai_ops
= {
53 .startup
= hdac_hda_dai_open
,
54 .shutdown
= hdac_hda_dai_close
,
55 .prepare
= hdac_hda_dai_prepare
,
56 .hw_params
= hdac_hda_dai_hw_params
,
57 .hw_free
= hdac_hda_dai_hw_free
,
58 .set_tdm_slot
= hdac_hda_dai_set_tdm_slot
,
61 static struct snd_soc_dai_driver hdac_hda_dais
[] = {
63 .id
= HDAC_ANALOG_DAI_ID
,
64 .name
= "Analog Codec DAI",
65 .ops
= &hdac_hda_dai_ops
,
67 .stream_name
= "Analog Codec Playback",
70 .rates
= SNDRV_PCM_RATE_8000_192000
,
71 .formats
= STUB_FORMATS
,
75 .stream_name
= "Analog Codec Capture",
78 .rates
= SNDRV_PCM_RATE_8000_192000
,
79 .formats
= STUB_FORMATS
,
84 .id
= HDAC_DIGITAL_DAI_ID
,
85 .name
= "Digital Codec DAI",
86 .ops
= &hdac_hda_dai_ops
,
88 .stream_name
= "Digital Codec Playback",
91 .rates
= SNDRV_PCM_RATE_8000_192000
,
92 .formats
= STUB_FORMATS
,
96 .stream_name
= "Digital Codec Capture",
99 .rates
= SNDRV_PCM_RATE_8000_192000
,
100 .formats
= STUB_FORMATS
,
105 .id
= HDAC_ALT_ANALOG_DAI_ID
,
106 .name
= "Alt Analog Codec DAI",
107 .ops
= &hdac_hda_dai_ops
,
109 .stream_name
= "Alt Analog Codec Playback",
112 .rates
= SNDRV_PCM_RATE_8000_192000
,
113 .formats
= STUB_FORMATS
,
117 .stream_name
= "Alt Analog Codec Capture",
120 .rates
= SNDRV_PCM_RATE_8000_192000
,
121 .formats
= STUB_FORMATS
,
128 static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
129 unsigned int tx_mask
, unsigned int rx_mask
,
130 int slots
, int slot_width
)
132 struct snd_soc_component
*component
= dai
->component
;
133 struct hdac_hda_priv
*hda_pvt
;
134 struct hdac_hda_pcm
*pcm
;
136 hda_pvt
= snd_soc_component_get_drvdata(component
);
137 pcm
= &hda_pvt
->pcm
[dai
->id
];
139 pcm
[dai
->id
].stream_tag
[SNDRV_PCM_STREAM_PLAYBACK
] = tx_mask
;
141 pcm
[dai
->id
].stream_tag
[SNDRV_PCM_STREAM_CAPTURE
] = rx_mask
;
146 static int hdac_hda_dai_hw_params(struct snd_pcm_substream
*substream
,
147 struct snd_pcm_hw_params
*params
,
148 struct snd_soc_dai
*dai
)
150 struct snd_soc_component
*component
= dai
->component
;
151 struct hdac_hda_priv
*hda_pvt
;
152 unsigned int format_val
;
155 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
156 maxbps
= dai
->driver
->playback
.sig_bits
;
158 maxbps
= dai
->driver
->capture
.sig_bits
;
160 hda_pvt
= snd_soc_component_get_drvdata(component
);
161 format_val
= snd_hdac_calc_stream_format(params_rate(params
),
162 params_channels(params
),
163 params_format(params
),
168 "invalid format_val, rate=%d, ch=%d, format=%d, maxbps=%d\n",
169 params_rate(params
), params_channels(params
),
170 params_format(params
), maxbps
);
175 hda_pvt
->pcm
[dai
->id
].format_val
[substream
->stream
] = format_val
;
179 static int hdac_hda_dai_hw_free(struct snd_pcm_substream
*substream
,
180 struct snd_soc_dai
*dai
)
182 struct snd_soc_component
*component
= dai
->component
;
183 struct hdac_hda_priv
*hda_pvt
;
184 struct hda_pcm_stream
*hda_stream
;
187 hda_pvt
= snd_soc_component_get_drvdata(component
);
188 pcm
= snd_soc_find_pcm_from_dai(hda_pvt
, dai
);
192 hda_stream
= &pcm
->stream
[substream
->stream
];
193 snd_hda_codec_cleanup(&hda_pvt
->codec
, hda_stream
, substream
);
198 static int hdac_hda_dai_prepare(struct snd_pcm_substream
*substream
,
199 struct snd_soc_dai
*dai
)
201 struct snd_soc_component
*component
= dai
->component
;
202 struct hda_pcm_stream
*hda_stream
;
203 struct hdac_hda_priv
*hda_pvt
;
204 struct hdac_device
*hdev
;
205 unsigned int format_val
;
210 hda_pvt
= snd_soc_component_get_drvdata(component
);
211 hdev
= &hda_pvt
->codec
.core
;
212 pcm
= snd_soc_find_pcm_from_dai(hda_pvt
, dai
);
216 hda_stream
= &pcm
->stream
[substream
->stream
];
218 stream
= hda_pvt
->pcm
[dai
->id
].stream_tag
[substream
->stream
];
219 format_val
= hda_pvt
->pcm
[dai
->id
].format_val
[substream
->stream
];
221 ret
= snd_hda_codec_prepare(&hda_pvt
->codec
, hda_stream
,
222 stream
, format_val
, substream
);
224 dev_err(&hdev
->dev
, "codec prepare failed %d\n", ret
);
229 static int hdac_hda_dai_open(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
;
238 hda_pvt
= snd_soc_component_get_drvdata(component
);
239 pcm
= snd_soc_find_pcm_from_dai(hda_pvt
, dai
);
243 snd_hda_codec_pcm_get(pcm
);
245 hda_stream
= &pcm
->stream
[substream
->stream
];
247 ret
= hda_stream
->ops
.open(hda_stream
, &hda_pvt
->codec
, substream
);
249 snd_hda_codec_pcm_put(pcm
);
254 static void hdac_hda_dai_close(struct snd_pcm_substream
*substream
,
255 struct snd_soc_dai
*dai
)
257 struct snd_soc_component
*component
= dai
->component
;
258 struct hdac_hda_priv
*hda_pvt
;
259 struct hda_pcm_stream
*hda_stream
;
262 hda_pvt
= snd_soc_component_get_drvdata(component
);
263 pcm
= snd_soc_find_pcm_from_dai(hda_pvt
, dai
);
267 hda_stream
= &pcm
->stream
[substream
->stream
];
269 hda_stream
->ops
.close(hda_stream
, &hda_pvt
->codec
, substream
);
271 snd_hda_codec_pcm_put(pcm
);
274 static struct hda_pcm
*snd_soc_find_pcm_from_dai(struct hdac_hda_priv
*hda_pvt
,
275 struct snd_soc_dai
*dai
)
277 struct hda_codec
*hcodec
= &hda_pvt
->codec
;
278 struct hda_pcm
*cpcm
;
279 const char *pcm_name
;
282 case HDAC_ANALOG_DAI_ID
:
285 case HDAC_DIGITAL_DAI_ID
:
286 pcm_name
= "Digital";
288 case HDAC_ALT_ANALOG_DAI_ID
:
289 pcm_name
= "Alt Analog";
292 dev_err(&hcodec
->core
.dev
, "invalid dai id %d\n", dai
->id
);
296 list_for_each_entry(cpcm
, &hcodec
->pcm_list_head
, list
) {
297 if (strpbrk(cpcm
->name
, pcm_name
))
301 dev_err(&hcodec
->core
.dev
, "didn't find PCM for DAI %s\n", dai
->name
);
305 static int hdac_hda_codec_probe(struct snd_soc_component
*component
)
307 struct hdac_hda_priv
*hda_pvt
=
308 snd_soc_component_get_drvdata(component
);
309 struct snd_soc_dapm_context
*dapm
=
310 snd_soc_component_get_dapm(component
);
311 struct hdac_device
*hdev
= &hda_pvt
->codec
.core
;
312 struct hda_codec
*hcodec
= &hda_pvt
->codec
;
313 struct hdac_ext_link
*hlink
;
314 hda_codec_patch_t patch
;
317 hlink
= snd_hdac_ext_bus_get_link(hdev
->bus
, dev_name(&hdev
->dev
));
319 dev_err(&hdev
->dev
, "hdac link not found\n");
323 snd_hdac_ext_bus_link_get(hdev
->bus
, hlink
);
325 ret
= snd_hda_codec_device_new(hcodec
->bus
, component
->card
->snd_card
,
328 dev_err(&hdev
->dev
, "failed to create hda codec %d\n", ret
);
332 * Overwrite type to HDA_DEV_ASOC since it is a ASoC driver
333 * hda_codec.c will check this flag to determine if unregister
336 hdev
->type
= HDA_DEV_ASOC
;
339 * snd_hda_codec_device_new decrements the usage count so call get pm
340 * else the device will be powered off
342 pm_runtime_get_noresume(&hdev
->dev
);
344 hcodec
->bus
->card
= dapm
->card
->snd_card
;
346 ret
= snd_hda_codec_set_name(hcodec
, hcodec
->preset
->name
);
348 dev_err(&hdev
->dev
, "name failed %s\n", hcodec
->preset
->name
);
352 ret
= snd_hdac_regmap_init(&hcodec
->core
);
354 dev_err(&hdev
->dev
, "regmap init failed\n");
358 patch
= (hda_codec_patch_t
)hcodec
->preset
->driver_data
;
362 dev_err(&hdev
->dev
, "patch failed %d\n", ret
);
366 dev_dbg(&hdev
->dev
, "no patch file found\n");
369 ret
= snd_hda_codec_parse_pcms(hcodec
);
371 dev_err(&hdev
->dev
, "unable to map pcms to dai %d\n", ret
);
375 ret
= snd_hda_codec_build_controls(hcodec
);
377 dev_err(&hdev
->dev
, "unable to create controls %d\n", ret
);
381 hcodec
->core
.lazy_cache
= true;
384 * hdac_device core already sets the state to active and calls
385 * get_noresume. So enable runtime and set the device to suspend.
386 * pm_runtime_enable is also called during codec registeration
388 pm_runtime_put(&hdev
->dev
);
389 pm_runtime_suspend(&hdev
->dev
);
394 pm_runtime_put(&hdev
->dev
);
396 snd_hdac_ext_bus_link_put(hdev
->bus
, hlink
);
400 static void hdac_hda_codec_remove(struct snd_soc_component
*component
)
402 struct hdac_hda_priv
*hda_pvt
=
403 snd_soc_component_get_drvdata(component
);
404 struct hdac_device
*hdev
= &hda_pvt
->codec
.core
;
405 struct hdac_ext_link
*hlink
= NULL
;
407 hlink
= snd_hdac_ext_bus_get_link(hdev
->bus
, dev_name(&hdev
->dev
));
409 dev_err(&hdev
->dev
, "hdac link not found\n");
413 pm_runtime_disable(&hdev
->dev
);
414 snd_hdac_ext_bus_link_put(hdev
->bus
, hlink
);
417 static const struct snd_soc_dapm_route hdac_hda_dapm_routes
[] = {
418 {"AIF1TX", NULL
, "Codec Input Pin1"},
419 {"AIF2TX", NULL
, "Codec Input Pin2"},
420 {"AIF3TX", NULL
, "Codec Input Pin3"},
422 {"Codec Output Pin1", NULL
, "AIF1RX"},
423 {"Codec Output Pin2", NULL
, "AIF2RX"},
424 {"Codec Output Pin3", NULL
, "AIF3RX"},
427 static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets
[] = {
428 /* Audio Interface */
429 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0,
431 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0,
433 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0,
435 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0,
437 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0,
439 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0,
443 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
444 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
445 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
448 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
449 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
450 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
453 static const struct snd_soc_component_driver hdac_hda_codec
= {
454 .probe
= hdac_hda_codec_probe
,
455 .remove
= hdac_hda_codec_remove
,
456 .idle_bias_on
= false,
457 .dapm_widgets
= hdac_hda_dapm_widgets
,
458 .num_dapm_widgets
= ARRAY_SIZE(hdac_hda_dapm_widgets
),
459 .dapm_routes
= hdac_hda_dapm_routes
,
460 .num_dapm_routes
= ARRAY_SIZE(hdac_hda_dapm_routes
),
463 static int hdac_hda_dev_probe(struct hdac_device
*hdev
)
465 struct hdac_ext_link
*hlink
;
466 struct hdac_hda_priv
*hda_pvt
;
469 /* hold the ref while we probe */
470 hlink
= snd_hdac_ext_bus_get_link(hdev
->bus
, dev_name(&hdev
->dev
));
472 dev_err(&hdev
->dev
, "hdac link not found\n");
475 snd_hdac_ext_bus_link_get(hdev
->bus
, hlink
);
477 hda_pvt
= hdac_to_hda_priv(hdev
);
481 /* ASoC specific initialization */
482 ret
= devm_snd_soc_register_component(&hdev
->dev
,
483 &hdac_hda_codec
, hdac_hda_dais
,
484 ARRAY_SIZE(hdac_hda_dais
));
486 dev_err(&hdev
->dev
, "failed to register HDA codec %d\n", ret
);
490 dev_set_drvdata(&hdev
->dev
, hda_pvt
);
491 snd_hdac_ext_bus_link_put(hdev
->bus
, hlink
);
496 static int hdac_hda_dev_remove(struct hdac_device
*hdev
)
498 struct hdac_hda_priv
*hda_pvt
;
500 hda_pvt
= dev_get_drvdata(&hdev
->dev
);
501 cancel_delayed_work_sync(&hda_pvt
->codec
.jackpoll_work
);
505 static struct hdac_ext_bus_ops hdac_ops
= {
506 .hdev_attach
= hdac_hda_dev_probe
,
507 .hdev_detach
= hdac_hda_dev_remove
,
510 struct hdac_ext_bus_ops
*snd_soc_hdac_hda_get_ops(void)
514 EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops
);
516 MODULE_LICENSE("GPL v2");
517 MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers");
518 MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>");