1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <sound/hda_register.h>
12 #include <sound/hdaudio_ext.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc-acpi.h>
15 #include <sound/soc-acpi-intel-match.h>
16 #include <sound/soc-component.h>
22 struct avs_tplg_path_template *template;
23 struct avs_path *path;
25 * link stream is stored within substream's runtime
26 * private_data to fulfill the needs of codec BE path
28 * host stream assigned
30 struct hdac_ext_stream *host_stream;
32 struct snd_pcm_substream *substream;
35 static struct avs_tplg_path_template *
36 avs_dai_find_path_template(struct snd_soc_dai *dai, bool is_fe, int direction)
38 struct snd_soc_dapm_widget *dw = snd_soc_dai_get_widget(dai, direction);
39 struct snd_soc_dapm_path *dp;
40 enum snd_soc_dapm_direction dir;
42 if (direction == SNDRV_PCM_STREAM_CAPTURE) {
43 dir = is_fe ? SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN;
45 dir = is_fe ? SND_SOC_DAPM_DIR_IN : SND_SOC_DAPM_DIR_OUT;
48 dp = list_first_entry_or_null(&dw->edges[dir], typeof(*dp), list_node[dir]);
52 /* Get the other widget, with actual path template data */
53 dw = (dp->source == dw) ? dp->sink : dp->source;
58 static int avs_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai, bool is_fe,
59 const struct snd_soc_dai_ops *ops)
61 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
62 struct avs_dev *adev = to_avs_dev(dai->dev);
63 struct avs_tplg_path_template *template;
64 struct avs_dma_data *data;
66 template = avs_dai_find_path_template(dai, is_fe, substream->stream);
68 dev_err(dai->dev, "no %s path for dai %s, invalid tplg?\n",
69 snd_pcm_stream_str(substream), dai->name);
73 data = kzalloc(sizeof(*data), GFP_KERNEL);
77 data->substream = substream;
78 data->template = template;
79 snd_soc_dai_set_dma_data(dai, substream, data);
81 if (rtd->dai_link->ignore_suspend)
87 static int avs_dai_hw_params(struct snd_pcm_substream *substream,
88 struct snd_pcm_hw_params *fe_hw_params,
89 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
92 struct avs_dma_data *data;
93 struct avs_path *path;
94 struct avs_dev *adev = to_avs_dev(dai->dev);
97 data = snd_soc_dai_get_dma_data(dai, substream);
99 dev_dbg(dai->dev, "%s FE hw_params str %p rtd %p",
100 __func__, substream, substream->runtime);
101 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
102 params_rate(fe_hw_params), params_channels(fe_hw_params),
103 params_width(fe_hw_params), params_physical_width(fe_hw_params));
105 dev_dbg(dai->dev, "%s BE hw_params str %p rtd %p",
106 __func__, substream, substream->runtime);
107 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
108 params_rate(be_hw_params), params_channels(be_hw_params),
109 params_width(be_hw_params), params_physical_width(be_hw_params));
111 path = avs_path_create(adev, dma_id, data->template, fe_hw_params, be_hw_params);
114 dev_err(dai->dev, "create path failed: %d\n", ret);
122 static int avs_dai_be_hw_params(struct snd_pcm_substream *substream,
123 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
126 struct snd_pcm_hw_params *fe_hw_params = NULL;
127 struct snd_soc_pcm_runtime *fe, *be;
128 struct snd_soc_dpcm *dpcm;
130 be = asoc_substream_to_rtd(substream);
131 for_each_dpcm_fe(be, substream->stream, dpcm) {
133 fe_hw_params = &fe->dpcm[substream->stream].hw_params;
136 return avs_dai_hw_params(substream, fe_hw_params, be_hw_params, dai, dma_id);
139 static int avs_dai_prepare(struct avs_dev *adev, struct snd_pcm_substream *substream,
140 struct snd_soc_dai *dai)
142 struct avs_dma_data *data;
145 data = snd_soc_dai_get_dma_data(dai, substream);
149 ret = avs_path_reset(data->path);
151 dev_err(dai->dev, "reset path failed: %d\n", ret);
155 ret = avs_path_pause(data->path);
157 dev_err(dai->dev, "pause path failed: %d\n", ret);
161 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops;
163 static int avs_dai_nonhda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
165 return avs_dai_startup(substream, dai, false, &avs_dai_nonhda_be_ops);
168 static void avs_dai_nonhda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
170 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
171 struct avs_dev *adev = to_avs_dev(dai->dev);
172 struct avs_dma_data *data;
174 if (rtd->dai_link->ignore_suspend)
175 adev->num_lp_paths--;
177 data = snd_soc_dai_get_dma_data(dai, substream);
179 snd_soc_dai_set_dma_data(dai, substream, NULL);
183 static int avs_dai_nonhda_be_hw_params(struct snd_pcm_substream *substream,
184 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
186 struct avs_dma_data *data;
188 data = snd_soc_dai_get_dma_data(dai, substream);
192 /* Actual port-id comes from topology. */
193 return avs_dai_be_hw_params(substream, hw_params, dai, 0);
196 static int avs_dai_nonhda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
198 struct avs_dma_data *data;
200 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
202 data = snd_soc_dai_get_dma_data(dai, substream);
204 avs_path_free(data->path);
211 static int avs_dai_nonhda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
213 return avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
216 static int avs_dai_nonhda_be_trigger(struct snd_pcm_substream *substream, int cmd,
217 struct snd_soc_dai *dai)
219 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
220 struct avs_dma_data *data;
223 data = snd_soc_dai_get_dma_data(dai, substream);
226 case SNDRV_PCM_TRIGGER_RESUME:
227 if (rtd->dai_link->ignore_suspend)
230 case SNDRV_PCM_TRIGGER_START:
231 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
232 ret = avs_path_pause(data->path);
234 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
238 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
240 dev_err(dai->dev, "run BE path failed: %d\n", ret);
243 case SNDRV_PCM_TRIGGER_SUSPEND:
244 if (rtd->dai_link->ignore_suspend)
247 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
248 case SNDRV_PCM_TRIGGER_STOP:
249 ret = avs_path_pause(data->path);
251 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
253 ret = avs_path_reset(data->path);
255 dev_err(dai->dev, "reset BE path failed: %d\n", ret);
266 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops = {
267 .startup = avs_dai_nonhda_be_startup,
268 .shutdown = avs_dai_nonhda_be_shutdown,
269 .hw_params = avs_dai_nonhda_be_hw_params,
270 .hw_free = avs_dai_nonhda_be_hw_free,
271 .prepare = avs_dai_nonhda_be_prepare,
272 .trigger = avs_dai_nonhda_be_trigger,
275 static const struct snd_soc_dai_ops avs_dai_hda_be_ops;
277 static int avs_dai_hda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
279 return avs_dai_startup(substream, dai, false, &avs_dai_hda_be_ops);
282 static void avs_dai_hda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
284 return avs_dai_nonhda_be_shutdown(substream, dai);
287 static int avs_dai_hda_be_hw_params(struct snd_pcm_substream *substream,
288 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
290 struct avs_dma_data *data;
291 struct hdac_ext_stream *link_stream;
293 data = snd_soc_dai_get_dma_data(dai, substream);
297 link_stream = substream->runtime->private_data;
299 return avs_dai_be_hw_params(substream, hw_params, dai,
300 hdac_stream(link_stream)->stream_tag - 1);
303 static int avs_dai_hda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
305 struct avs_dma_data *data;
306 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
307 struct hdac_ext_stream *link_stream;
308 struct hdac_ext_link *link;
309 struct hda_codec *codec;
311 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
313 data = snd_soc_dai_get_dma_data(dai, substream);
317 link_stream = substream->runtime->private_data;
318 link_stream->link_prepared = false;
319 avs_path_free(data->path);
322 /* clear link <-> stream mapping */
323 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
324 link = snd_hdac_ext_bus_get_hlink_by_addr(&codec->bus->core, codec->core.addr);
328 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
329 snd_hdac_ext_bus_link_clear_stream_id(link, hdac_stream(link_stream)->stream_tag);
334 static int avs_dai_hda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
336 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
337 struct snd_pcm_runtime *runtime = substream->runtime;
338 struct hdac_ext_stream *link_stream = runtime->private_data;
339 struct hdac_ext_link *link;
340 struct hda_codec *codec;
341 struct hdac_bus *bus;
342 unsigned int format_val;
345 if (link_stream->link_prepared)
348 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
349 bus = &codec->bus->core;
350 format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
351 runtime->sample_bits, 0);
353 snd_hdac_ext_stream_decouple(bus, link_stream, true);
354 snd_hdac_ext_stream_reset(link_stream);
355 snd_hdac_ext_stream_setup(link_stream, format_val);
357 link = snd_hdac_ext_bus_get_hlink_by_addr(bus, codec->core.addr);
361 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
362 snd_hdac_ext_bus_link_set_stream_id(link, hdac_stream(link_stream)->stream_tag);
364 ret = avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
368 link_stream->link_prepared = true;
372 static int avs_dai_hda_be_trigger(struct snd_pcm_substream *substream, int cmd,
373 struct snd_soc_dai *dai)
375 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
376 struct hdac_ext_stream *link_stream;
377 struct avs_dma_data *data;
380 dev_dbg(dai->dev, "entry %s cmd=%d\n", __func__, cmd);
382 data = snd_soc_dai_get_dma_data(dai, substream);
383 link_stream = substream->runtime->private_data;
386 case SNDRV_PCM_TRIGGER_RESUME:
387 if (rtd->dai_link->ignore_suspend)
390 case SNDRV_PCM_TRIGGER_START:
391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
392 snd_hdac_ext_stream_start(link_stream);
394 ret = avs_path_pause(data->path);
396 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
400 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
402 dev_err(dai->dev, "run BE path failed: %d\n", ret);
405 case SNDRV_PCM_TRIGGER_SUSPEND:
406 if (rtd->dai_link->ignore_suspend)
409 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
410 case SNDRV_PCM_TRIGGER_STOP:
411 ret = avs_path_pause(data->path);
413 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
415 snd_hdac_ext_stream_clear(link_stream);
417 ret = avs_path_reset(data->path);
419 dev_err(dai->dev, "reset BE path failed: %d\n", ret);
430 static const struct snd_soc_dai_ops avs_dai_hda_be_ops = {
431 .startup = avs_dai_hda_be_startup,
432 .shutdown = avs_dai_hda_be_shutdown,
433 .hw_params = avs_dai_hda_be_hw_params,
434 .hw_free = avs_dai_hda_be_hw_free,
435 .prepare = avs_dai_hda_be_prepare,
436 .trigger = avs_dai_hda_be_trigger,
439 static const unsigned int rates[] = {
440 8000, 11025, 12000, 16000,
441 22050, 24000, 32000, 44100,
442 48000, 64000, 88200, 96000,
443 128000, 176400, 192000,
446 static const struct snd_pcm_hw_constraint_list hw_rates = {
447 .count = ARRAY_SIZE(rates),
452 const struct snd_soc_dai_ops avs_dai_fe_ops;
454 static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
456 struct snd_pcm_runtime *runtime = substream->runtime;
457 struct avs_dma_data *data;
458 struct avs_dev *adev = to_avs_dev(dai->dev);
459 struct hdac_bus *bus = &adev->base.core;
460 struct hdac_ext_stream *host_stream;
463 ret = avs_dai_startup(substream, dai, true, &avs_dai_fe_ops);
467 data = snd_soc_dai_get_dma_data(dai, substream);
469 host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST);
475 data->host_stream = host_stream;
476 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
480 /* avoid wrap-around with wall-clock */
481 ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000);
485 ret = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_rates);
489 snd_pcm_set_sync(substream);
491 dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p",
492 __func__, hdac_stream(host_stream)->stream_tag, substream);
501 static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
503 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
504 struct avs_dev *adev = to_avs_dev(dai->dev);
505 struct avs_dma_data *data;
507 if (rtd->dai_link->ignore_suspend)
508 adev->num_lp_paths--;
510 data = snd_soc_dai_get_dma_data(dai, substream);
512 snd_soc_dai_set_dma_data(dai, substream, NULL);
513 snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST);
517 static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream,
518 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
520 struct snd_pcm_hw_params *be_hw_params = NULL;
521 struct snd_soc_pcm_runtime *fe, *be;
522 struct snd_soc_dpcm *dpcm;
523 struct avs_dma_data *data;
524 struct hdac_ext_stream *host_stream;
527 data = snd_soc_dai_get_dma_data(dai, substream);
531 host_stream = data->host_stream;
533 hdac_stream(host_stream)->bufsize = 0;
534 hdac_stream(host_stream)->period_bytes = 0;
535 hdac_stream(host_stream)->format_val = 0;
537 fe = asoc_substream_to_rtd(substream);
538 for_each_dpcm_be(fe, substream->stream, dpcm) {
540 be_hw_params = &be->dpcm[substream->stream].hw_params;
543 ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai,
544 hdac_stream(host_stream)->stream_tag - 1);
548 ret = avs_path_bind(data->path);
550 dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret);
557 avs_path_free(data->path);
560 snd_pcm_lib_free_pages(substream);
564 static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
566 struct avs_dma_data *data;
567 struct hdac_ext_stream *host_stream;
570 dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p",
571 __func__, substream, substream->runtime);
573 data = snd_soc_dai_get_dma_data(dai, substream);
577 host_stream = data->host_stream;
579 ret = avs_path_unbind(data->path);
581 dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret);
583 avs_path_free(data->path);
585 snd_hdac_stream_cleanup(hdac_stream(host_stream));
586 hdac_stream(host_stream)->prepared = false;
591 static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
595 ret = __avs_dai_fe_hw_free(substream, dai);
596 snd_pcm_lib_free_pages(substream);
601 static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
603 struct snd_pcm_runtime *runtime = substream->runtime;
604 struct avs_dma_data *data;
605 struct avs_dev *adev = to_avs_dev(dai->dev);
606 struct hdac_ext_stream *host_stream;
607 struct hdac_bus *bus;
608 unsigned int format_val;
611 data = snd_soc_dai_get_dma_data(dai, substream);
612 host_stream = data->host_stream;
614 if (hdac_stream(host_stream)->prepared)
617 bus = hdac_stream(host_stream)->bus;
618 snd_hdac_ext_stream_decouple(bus, data->host_stream, true);
619 snd_hdac_stream_reset(hdac_stream(host_stream));
621 format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
622 runtime->sample_bits, 0);
624 ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val);
628 ret = snd_hdac_stream_setup(hdac_stream(host_stream));
632 ret = avs_dai_prepare(adev, substream, dai);
636 hdac_stream(host_stream)->prepared = true;
640 static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
642 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
643 struct avs_dma_data *data;
644 struct hdac_ext_stream *host_stream;
645 struct hdac_bus *bus;
649 data = snd_soc_dai_get_dma_data(dai, substream);
650 host_stream = data->host_stream;
651 bus = hdac_stream(host_stream)->bus;
654 case SNDRV_PCM_TRIGGER_RESUME:
655 if (rtd->dai_link->ignore_suspend)
658 case SNDRV_PCM_TRIGGER_START:
659 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
660 spin_lock_irqsave(&bus->reg_lock, flags);
661 snd_hdac_stream_start(hdac_stream(host_stream));
662 spin_unlock_irqrestore(&bus->reg_lock, flags);
664 /* Timeout on DRSM poll shall not stop the resume so ignore the result. */
665 if (cmd == SNDRV_PCM_TRIGGER_RESUME)
666 snd_hdac_stream_wait_drsm(hdac_stream(host_stream));
668 ret = avs_path_pause(data->path);
670 dev_err(dai->dev, "pause FE path failed: %d\n", ret);
674 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
676 dev_err(dai->dev, "run FE path failed: %d\n", ret);
680 case SNDRV_PCM_TRIGGER_SUSPEND:
681 if (rtd->dai_link->ignore_suspend)
684 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
685 case SNDRV_PCM_TRIGGER_STOP:
686 ret = avs_path_pause(data->path);
688 dev_err(dai->dev, "pause FE path failed: %d\n", ret);
690 spin_lock_irqsave(&bus->reg_lock, flags);
691 snd_hdac_stream_stop(hdac_stream(host_stream));
692 spin_unlock_irqrestore(&bus->reg_lock, flags);
694 ret = avs_path_reset(data->path);
696 dev_err(dai->dev, "reset FE path failed: %d\n", ret);
707 const struct snd_soc_dai_ops avs_dai_fe_ops = {
708 .startup = avs_dai_fe_startup,
709 .shutdown = avs_dai_fe_shutdown,
710 .hw_params = avs_dai_fe_hw_params,
711 .hw_free = avs_dai_fe_hw_free,
712 .prepare = avs_dai_fe_prepare,
713 .trigger = avs_dai_fe_trigger,
716 static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count,
719 struct snd_soc_component *component = file->private_data;
720 struct snd_soc_card *card = component->card;
721 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
725 len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix,
726 mach->tplg_filename);
728 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
731 static const struct file_operations topology_name_fops = {
733 .read = topology_name_read,
734 .llseek = default_llseek,
737 static int avs_component_load_libraries(struct avs_soc_component *acomp)
739 struct avs_tplg *tplg = acomp->tplg;
740 struct avs_dev *adev = to_avs_dev(acomp->base.dev);
746 /* Parent device may be asleep and library loading involves IPCs. */
747 ret = pm_runtime_resume_and_get(adev->dev);
751 avs_hda_power_gating_enable(adev, false);
752 avs_hda_clock_gating_enable(adev, false);
753 avs_hda_l1sen_enable(adev, false);
755 ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
757 avs_hda_l1sen_enable(adev, true);
758 avs_hda_clock_gating_enable(adev, true);
759 avs_hda_power_gating_enable(adev, true);
762 ret = avs_module_info_init(adev, false);
764 pm_runtime_mark_last_busy(adev->dev);
765 pm_runtime_put_autosuspend(adev->dev);
770 static int avs_component_probe(struct snd_soc_component *component)
772 struct snd_soc_card *card = component->card;
773 struct snd_soc_acpi_mach *mach;
774 struct avs_soc_component *acomp;
775 struct avs_dev *adev;
779 dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name);
780 mach = dev_get_platdata(card->dev);
781 acomp = to_avs_soc_component(component);
782 adev = to_avs_dev(component->dev);
784 acomp->tplg = avs_tplg_new(component);
788 if (!mach->tplg_filename)
791 /* Load specified topology and create debugfs for it. */
792 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
793 mach->tplg_filename);
797 ret = avs_load_topology(component, filename);
802 ret = avs_component_load_libraries(acomp);
804 dev_err(card->dev, "libraries loading failed: %d\n", ret);
809 debugfs_create_file("topology_name", 0444, component->debugfs_root, component,
810 &topology_name_fops);
812 mutex_lock(&adev->comp_list_mutex);
813 list_add_tail(&acomp->node, &adev->comp_list);
814 mutex_unlock(&adev->comp_list_mutex);
819 avs_remove_topology(component);
823 static void avs_component_remove(struct snd_soc_component *component)
825 struct avs_soc_component *acomp = to_avs_soc_component(component);
826 struct snd_soc_acpi_mach *mach;
827 struct avs_dev *adev = to_avs_dev(component->dev);
830 mach = dev_get_platdata(component->card->dev);
832 mutex_lock(&adev->comp_list_mutex);
833 list_del(&acomp->node);
834 mutex_unlock(&adev->comp_list_mutex);
836 if (mach->tplg_filename) {
837 ret = avs_remove_topology(component);
839 dev_err(component->dev, "unload topology failed: %d\n", ret);
843 static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data)
845 struct snd_pcm_substream *substream;
846 struct snd_soc_pcm_runtime *rtd;
849 substream = data->substream;
850 rtd = asoc_substream_to_rtd(substream);
852 ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai);
854 dev_err(dai->dev, "hw_params on resume failed: %d\n", ret);
859 static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
861 struct hdac_ext_stream *host_stream;
862 struct hdac_stream *hstream;
863 struct hdac_bus *bus;
866 host_stream = data->host_stream;
867 hstream = hdac_stream(host_stream);
868 bus = hdac_stream(host_stream)->bus;
870 /* Set DRSM before programming stream and position registers. */
871 snd_hdac_stream_drsm_enable(bus, true, hstream->index);
873 ret = dai->driver->ops->prepare(data->substream, dai);
875 dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret);
879 writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL);
880 writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU);
881 writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL);
882 writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU);
884 /* As per HW spec recommendation, program LPIB and DPIB to the same value. */
885 snd_hdac_stream_set_lpib(hstream, hstream->lpib);
886 snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib);
891 static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
895 ret = dai->driver->ops->prepare(data->substream, dai);
897 dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret);
902 static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
904 struct hdac_ext_stream *host_stream;
907 host_stream = data->host_stream;
909 /* Store position addresses so we can resume from them later on. */
910 hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream));
911 host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL);
912 host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU);
913 host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL);
914 host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU);
916 ret = __avs_dai_fe_hw_free(data->substream, dai);
918 dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret);
923 static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
927 ret = dai->driver->ops->hw_free(data->substream, dai);
929 dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret);
934 static int avs_component_pm_op(struct snd_soc_component *component, bool be,
935 int (*op)(struct snd_soc_dai *, struct avs_dma_data *))
937 struct snd_soc_pcm_runtime *rtd;
938 struct avs_dma_data *data;
939 struct snd_soc_dai *dai;
942 for_each_component_dais(component, dai) {
943 data = snd_soc_dai_dma_data_get_playback(dai);
945 rtd = asoc_substream_to_rtd(data->substream);
946 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
949 __snd_pcm_set_state(data->substream->runtime,
950 SNDRV_PCM_STATE_DISCONNECTED);
956 data = snd_soc_dai_dma_data_get_capture(dai);
958 rtd = asoc_substream_to_rtd(data->substream);
959 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
962 __snd_pcm_set_state(data->substream->runtime,
963 SNDRV_PCM_STATE_DISCONNECTED);
973 static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be)
975 return avs_component_pm_op(component, be, &avs_dai_resume_hw_params);
978 static int avs_component_resume_prepare(struct snd_soc_component *component, bool be)
980 int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
983 prepare_cb = &avs_dai_resume_be_prepare;
985 prepare_cb = &avs_dai_resume_fe_prepare;
987 return avs_component_pm_op(component, be, prepare_cb);
990 static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be)
992 int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
995 hw_free_cb = &avs_dai_suspend_be_hw_free;
997 hw_free_cb = &avs_dai_suspend_fe_hw_free;
999 return avs_component_pm_op(component, be, hw_free_cb);
1002 static int avs_component_suspend(struct snd_soc_component *component)
1007 * When freeing paths, FEs need to be first as they perform
1010 ret = avs_component_suspend_hw_free(component, false);
1014 return avs_component_suspend_hw_free(component, true);
1017 static int avs_component_resume(struct snd_soc_component *component)
1022 * When creating paths, FEs need to be last as they perform
1025 ret = avs_component_resume_hw_params(component, true);
1029 ret = avs_component_resume_hw_params(component, false);
1033 /* It is expected that the LINK stream is prepared first. */
1034 ret = avs_component_resume_prepare(component, true);
1038 return avs_component_resume_prepare(component, false);
1041 static const struct snd_pcm_hardware avs_pcm_hardware = {
1042 .info = SNDRV_PCM_INFO_MMAP |
1043 SNDRV_PCM_INFO_MMAP_VALID |
1044 SNDRV_PCM_INFO_INTERLEAVED |
1045 SNDRV_PCM_INFO_PAUSE |
1046 SNDRV_PCM_INFO_RESUME |
1047 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
1048 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1049 SNDRV_PCM_FMTBIT_S24_LE |
1050 SNDRV_PCM_FMTBIT_S32_LE,
1051 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1052 .period_bytes_min = 128,
1053 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1055 .periods_max = AZX_MAX_FRAG,
1059 static int avs_component_open(struct snd_soc_component *component,
1060 struct snd_pcm_substream *substream)
1062 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1064 /* only FE DAI links are handled here */
1065 if (rtd->dai_link->no_pcm)
1068 return snd_soc_set_runtime_hwparams(substream, &avs_pcm_hardware);
1071 static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream)
1073 return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
1074 (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index));
1077 static snd_pcm_uframes_t
1078 avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream)
1080 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1081 struct avs_dma_data *data;
1082 struct hdac_ext_stream *host_stream;
1085 data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
1086 if (!data->host_stream)
1089 host_stream = data->host_stream;
1090 pos = avs_hda_stream_dpib_read(host_stream);
1092 if (pos >= hdac_stream(host_stream)->bufsize)
1095 return bytes_to_frames(substream->runtime, pos);
1098 static int avs_component_mmap(struct snd_soc_component *component,
1099 struct snd_pcm_substream *substream,
1100 struct vm_area_struct *vma)
1102 return snd_pcm_lib_default_mmap(substream, vma);
1105 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
1107 static int avs_component_construct(struct snd_soc_component *component,
1108 struct snd_soc_pcm_runtime *rtd)
1110 struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
1111 struct snd_pcm *pcm = rtd->pcm;
1113 if (dai->driver->playback.channels_min)
1114 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1115 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1118 if (dai->driver->capture.channels_min)
1119 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1120 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1126 static const struct snd_soc_component_driver avs_component_driver = {
1128 .probe = avs_component_probe,
1129 .remove = avs_component_remove,
1130 .suspend = avs_component_suspend,
1131 .resume = avs_component_resume,
1132 .open = avs_component_open,
1133 .pointer = avs_component_pointer,
1134 .mmap = avs_component_mmap,
1135 .pcm_construct = avs_component_construct,
1136 .module_get_upon_open = 1, /* increment refcount when a pcm is opened */
1137 .topology_name_prefix = "intel/avs",
1140 int avs_soc_component_register(struct device *dev, const char *name,
1141 const struct snd_soc_component_driver *drv,
1142 struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais)
1144 struct avs_soc_component *acomp;
1147 acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL);
1151 ret = snd_soc_component_initialize(&acomp->base, drv, dev);
1155 /* force name change after ASoC is done with its init */
1156 acomp->base.name = name;
1157 INIT_LIST_HEAD(&acomp->node);
1159 return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais);
1162 static struct snd_soc_dai_driver dmic_cpu_dais[] = {
1165 .ops = &avs_dai_nonhda_be_ops,
1167 .stream_name = "DMIC Rx",
1170 .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000,
1171 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
1175 .name = "DMIC WoV Pin",
1176 .ops = &avs_dai_nonhda_be_ops,
1178 .stream_name = "DMIC WoV Rx",
1181 .rates = SNDRV_PCM_RATE_16000,
1182 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1187 int avs_dmic_platform_register(struct avs_dev *adev, const char *name)
1189 return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais,
1190 ARRAY_SIZE(dmic_cpu_dais));
1193 static const struct snd_soc_dai_driver i2s_dai_template = {
1194 .ops = &avs_dai_nonhda_be_ops,
1198 .rates = SNDRV_PCM_RATE_8000_192000 |
1199 SNDRV_PCM_RATE_KNOT,
1200 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1201 SNDRV_PCM_FMTBIT_S24_LE |
1202 SNDRV_PCM_FMTBIT_S32_LE,
1207 .rates = SNDRV_PCM_RATE_8000_192000 |
1208 SNDRV_PCM_RATE_KNOT,
1209 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1210 SNDRV_PCM_FMTBIT_S24_LE |
1211 SNDRV_PCM_FMTBIT_S32_LE,
1215 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
1216 unsigned long *tdms)
1218 struct snd_soc_dai_driver *cpus, *dai;
1219 size_t ssp_count, cpu_count;
1222 ssp_count = adev->hw_cfg.i2s_caps.ctrl_count;
1223 cpu_count = hweight_long(port_mask);
1225 for_each_set_bit(i, &port_mask, ssp_count)
1226 cpu_count += hweight_long(tdms[i]);
1228 cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL);
1233 for_each_set_bit(i, &port_mask, ssp_count) {
1234 memcpy(dai, &i2s_dai_template, sizeof(*dai));
1237 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i);
1238 dai->playback.stream_name =
1239 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i);
1240 dai->capture.stream_name =
1241 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i);
1243 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1251 for_each_set_bit(i, &port_mask, ssp_count) {
1252 for_each_set_bit(j, &tdms[i], ssp_count) {
1253 memcpy(dai, &i2s_dai_template, sizeof(*dai));
1256 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j);
1257 dai->playback.stream_name =
1258 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j);
1259 dai->capture.stream_name =
1260 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j);
1262 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1269 return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count);
1272 /* HD-Audio CPU DAI template */
1273 static const struct snd_soc_dai_driver hda_cpu_dai = {
1274 .ops = &avs_dai_hda_be_ops,
1278 .rates = SNDRV_PCM_RATE_8000_192000,
1279 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1280 SNDRV_PCM_FMTBIT_S24_LE |
1281 SNDRV_PCM_FMTBIT_S32_LE,
1286 .rates = SNDRV_PCM_RATE_8000_192000,
1287 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1288 SNDRV_PCM_FMTBIT_S24_LE |
1289 SNDRV_PCM_FMTBIT_S32_LE,
1293 static void avs_component_hda_unregister_dais(struct snd_soc_component *component)
1295 struct snd_soc_acpi_mach *mach;
1296 struct snd_soc_dai *dai, *save;
1297 struct hda_codec *codec;
1300 mach = dev_get_platdata(component->card->dev);
1301 codec = mach->pdata;
1302 sprintf(name, "%s-cpu", dev_name(&codec->core.dev));
1304 for_each_component_dais_safe(component, dai, save) {
1307 if (!strstr(dai->driver->name, name))
1310 for_each_pcm_streams(stream)
1311 snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
1313 snd_soc_unregister_dai(dai);
1317 static int avs_component_hda_probe(struct snd_soc_component *component)
1319 struct snd_soc_dapm_context *dapm;
1320 struct snd_soc_dai_driver *dais;
1321 struct snd_soc_acpi_mach *mach;
1322 struct hda_codec *codec;
1323 struct hda_pcm *pcm;
1325 int pcm_count = 0, ret, i;
1327 mach = dev_get_platdata(component->card->dev);
1331 codec = mach->pdata;
1332 if (list_empty(&codec->pcm_list_head))
1334 list_for_each_entry(pcm, &codec->pcm_list_head, list)
1337 dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais),
1342 cname = dev_name(&codec->core.dev);
1343 dapm = snd_soc_component_get_dapm(component);
1344 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
1346 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
1347 struct snd_soc_dai *dai;
1349 memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais));
1351 dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL,
1352 "%s-cpu%d", cname, i);
1353 if (!dais[i].name) {
1358 if (pcm->stream[0].substreams) {
1359 dais[i].playback.stream_name =
1360 devm_kasprintf(component->dev, GFP_KERNEL,
1361 "%s-cpu%d Tx", cname, i);
1362 if (!dais[i].playback.stream_name) {
1368 if (pcm->stream[1].substreams) {
1369 dais[i].capture.stream_name =
1370 devm_kasprintf(component->dev, GFP_KERNEL,
1371 "%s-cpu%d Rx", cname, i);
1372 if (!dais[i].capture.stream_name) {
1378 dai = snd_soc_register_dai(component, &dais[i], false);
1380 dev_err(component->dev, "register dai for %s failed\n",
1386 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1388 dev_err(component->dev, "create widgets failed: %d\n",
1394 ret = avs_component_probe(component);
1397 avs_component_hda_unregister_dais(component);
1402 static void avs_component_hda_remove(struct snd_soc_component *component)
1404 avs_component_hda_unregister_dais(component);
1405 avs_component_remove(component);
1408 static int avs_component_hda_open(struct snd_soc_component *component,
1409 struct snd_pcm_substream *substream)
1411 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1412 struct hdac_ext_stream *link_stream;
1413 struct hda_codec *codec;
1415 if (!rtd->dai_link->no_pcm) {
1416 struct snd_pcm_hardware hwparams = avs_pcm_hardware;
1417 struct snd_soc_pcm_runtime *be;
1418 struct snd_soc_dpcm *dpcm;
1419 int dir = substream->stream;
1422 * Support the DPCM reparenting while still fulfilling expectations of HDAudio
1423 * common code - a valid stream pointer at substream->runtime->private_data -
1424 * by having all FEs point to the same private data.
1426 for_each_dpcm_be(rtd, dir, dpcm) {
1427 struct snd_pcm_substream *be_substream;
1430 if (be->dpcm[dir].users == 1)
1433 be_substream = snd_soc_dpcm_get_substream(be, dir);
1434 substream->runtime->private_data = be_substream->runtime->private_data;
1438 /* RESUME unsupported for de-coupled HD-Audio capture. */
1439 if (dir == SNDRV_PCM_STREAM_CAPTURE)
1440 hwparams.info &= ~SNDRV_PCM_INFO_RESUME;
1442 return snd_soc_set_runtime_hwparams(substream, &hwparams);
1445 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
1446 link_stream = snd_hdac_ext_stream_assign(&codec->bus->core, substream,
1447 HDAC_EXT_STREAM_TYPE_LINK);
1451 substream->runtime->private_data = link_stream;
1455 static int avs_component_hda_close(struct snd_soc_component *component,
1456 struct snd_pcm_substream *substream)
1458 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1459 struct hdac_ext_stream *link_stream;
1461 /* only BE DAI links are handled here */
1462 if (!rtd->dai_link->no_pcm)
1465 link_stream = substream->runtime->private_data;
1466 snd_hdac_ext_stream_release(link_stream, HDAC_EXT_STREAM_TYPE_LINK);
1467 substream->runtime->private_data = NULL;
1472 static const struct snd_soc_component_driver avs_hda_component_driver = {
1473 .name = "avs-hda-pcm",
1474 .probe = avs_component_hda_probe,
1475 .remove = avs_component_hda_remove,
1476 .suspend = avs_component_suspend,
1477 .resume = avs_component_resume,
1478 .open = avs_component_hda_open,
1479 .close = avs_component_hda_close,
1480 .pointer = avs_component_pointer,
1481 .mmap = avs_component_mmap,
1482 .pcm_construct = avs_component_construct,
1484 * hda platform component's probe() is dependent on
1485 * codec->pcm_list_head, it needs to be initialized after codec
1486 * component. remove_order is here for completeness sake
1488 .probe_order = SND_SOC_COMP_ORDER_LATE,
1489 .remove_order = SND_SOC_COMP_ORDER_EARLY,
1490 .module_get_upon_open = 1,
1491 .topology_name_prefix = "intel/avs",
1494 int avs_hda_platform_register(struct avs_dev *adev, const char *name)
1496 return avs_soc_component_register(adev->dev, name,
1497 &avs_hda_component_driver, NULL, 0);