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;
39 struct snd_soc_dapm_path *dp;
40 enum snd_soc_dapm_direction dir;
42 if (direction == SNDRV_PCM_STREAM_CAPTURE) {
43 dw = dai->capture_widget;
44 dir = is_fe ? SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN;
46 dw = dai->playback_widget;
47 dir = is_fe ? SND_SOC_DAPM_DIR_IN : SND_SOC_DAPM_DIR_OUT;
50 dp = list_first_entry_or_null(&dw->edges[dir], typeof(*dp), list_node[dir]);
54 /* Get the other widget, with actual path template data */
55 dw = (dp->source == dw) ? dp->sink : dp->source;
60 static int avs_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai, bool is_fe,
61 const struct snd_soc_dai_ops *ops)
63 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
64 struct avs_dev *adev = to_avs_dev(dai->dev);
65 struct avs_tplg_path_template *template;
66 struct avs_dma_data *data;
68 template = avs_dai_find_path_template(dai, is_fe, substream->stream);
70 dev_err(dai->dev, "no %s path for dai %s, invalid tplg?\n",
71 snd_pcm_stream_str(substream), dai->name);
75 data = kzalloc(sizeof(*data), GFP_KERNEL);
79 data->substream = substream;
80 data->template = template;
81 snd_soc_dai_set_dma_data(dai, substream, data);
83 if (rtd->dai_link->ignore_suspend)
89 static int avs_dai_hw_params(struct snd_pcm_substream *substream,
90 struct snd_pcm_hw_params *fe_hw_params,
91 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
94 struct avs_dma_data *data;
95 struct avs_path *path;
96 struct avs_dev *adev = to_avs_dev(dai->dev);
99 data = snd_soc_dai_get_dma_data(dai, substream);
101 dev_dbg(dai->dev, "%s FE hw_params str %p rtd %p",
102 __func__, substream, substream->runtime);
103 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
104 params_rate(fe_hw_params), params_channels(fe_hw_params),
105 params_width(fe_hw_params), params_physical_width(fe_hw_params));
107 dev_dbg(dai->dev, "%s BE hw_params str %p rtd %p",
108 __func__, substream, substream->runtime);
109 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
110 params_rate(be_hw_params), params_channels(be_hw_params),
111 params_width(be_hw_params), params_physical_width(be_hw_params));
113 path = avs_path_create(adev, dma_id, data->template, fe_hw_params, be_hw_params);
116 dev_err(dai->dev, "create path failed: %d\n", ret);
124 static int avs_dai_be_hw_params(struct snd_pcm_substream *substream,
125 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
128 struct snd_pcm_hw_params *fe_hw_params = NULL;
129 struct snd_soc_pcm_runtime *fe, *be;
130 struct snd_soc_dpcm *dpcm;
132 be = asoc_substream_to_rtd(substream);
133 for_each_dpcm_fe(be, substream->stream, dpcm) {
135 fe_hw_params = &fe->dpcm[substream->stream].hw_params;
138 return avs_dai_hw_params(substream, fe_hw_params, be_hw_params, dai, dma_id);
141 static int avs_dai_prepare(struct avs_dev *adev, struct snd_pcm_substream *substream,
142 struct snd_soc_dai *dai)
144 struct avs_dma_data *data;
147 data = snd_soc_dai_get_dma_data(dai, substream);
151 ret = avs_path_reset(data->path);
153 dev_err(dai->dev, "reset path failed: %d\n", ret);
157 ret = avs_path_pause(data->path);
159 dev_err(dai->dev, "pause path failed: %d\n", ret);
163 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops;
165 static int avs_dai_nonhda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
167 return avs_dai_startup(substream, dai, false, &avs_dai_nonhda_be_ops);
170 static void avs_dai_nonhda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
172 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
173 struct avs_dev *adev = to_avs_dev(dai->dev);
174 struct avs_dma_data *data;
176 if (rtd->dai_link->ignore_suspend)
177 adev->num_lp_paths--;
179 data = snd_soc_dai_get_dma_data(dai, substream);
181 snd_soc_dai_set_dma_data(dai, substream, NULL);
185 static int avs_dai_nonhda_be_hw_params(struct snd_pcm_substream *substream,
186 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
188 struct avs_dma_data *data;
190 data = snd_soc_dai_get_dma_data(dai, substream);
194 /* Actual port-id comes from topology. */
195 return avs_dai_be_hw_params(substream, hw_params, dai, 0);
198 static int avs_dai_nonhda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
200 struct avs_dma_data *data;
202 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
204 data = snd_soc_dai_get_dma_data(dai, substream);
206 avs_path_free(data->path);
213 static int avs_dai_nonhda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
215 return avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
218 static int avs_dai_nonhda_be_trigger(struct snd_pcm_substream *substream, int cmd,
219 struct snd_soc_dai *dai)
221 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
222 struct avs_dma_data *data;
225 data = snd_soc_dai_get_dma_data(dai, substream);
228 case SNDRV_PCM_TRIGGER_RESUME:
229 if (rtd->dai_link->ignore_suspend)
232 case SNDRV_PCM_TRIGGER_START:
233 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
234 ret = avs_path_pause(data->path);
236 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
240 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
242 dev_err(dai->dev, "run BE path failed: %d\n", ret);
245 case SNDRV_PCM_TRIGGER_SUSPEND:
246 if (rtd->dai_link->ignore_suspend)
249 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
250 case SNDRV_PCM_TRIGGER_STOP:
251 ret = avs_path_pause(data->path);
253 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
255 ret = avs_path_reset(data->path);
257 dev_err(dai->dev, "reset BE path failed: %d\n", ret);
268 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops = {
269 .startup = avs_dai_nonhda_be_startup,
270 .shutdown = avs_dai_nonhda_be_shutdown,
271 .hw_params = avs_dai_nonhda_be_hw_params,
272 .hw_free = avs_dai_nonhda_be_hw_free,
273 .prepare = avs_dai_nonhda_be_prepare,
274 .trigger = avs_dai_nonhda_be_trigger,
277 static const struct snd_soc_dai_ops avs_dai_hda_be_ops;
279 static int avs_dai_hda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
281 return avs_dai_startup(substream, dai, false, &avs_dai_hda_be_ops);
284 static void avs_dai_hda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
286 return avs_dai_nonhda_be_shutdown(substream, dai);
289 static int avs_dai_hda_be_hw_params(struct snd_pcm_substream *substream,
290 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
292 struct avs_dma_data *data;
293 struct hdac_ext_stream *link_stream;
295 data = snd_soc_dai_get_dma_data(dai, substream);
299 link_stream = substream->runtime->private_data;
301 return avs_dai_be_hw_params(substream, hw_params, dai,
302 hdac_stream(link_stream)->stream_tag - 1);
305 static int avs_dai_hda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
307 struct avs_dma_data *data;
308 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
309 struct hdac_ext_stream *link_stream;
310 struct hdac_ext_link *link;
311 struct hda_codec *codec;
313 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
315 data = snd_soc_dai_get_dma_data(dai, substream);
319 link_stream = substream->runtime->private_data;
320 link_stream->link_prepared = false;
321 avs_path_free(data->path);
324 /* clear link <-> stream mapping */
325 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
326 link = snd_hdac_ext_bus_get_hlink_by_addr(&codec->bus->core, codec->core.addr);
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
331 snd_hdac_ext_bus_link_clear_stream_id(link, hdac_stream(link_stream)->stream_tag);
336 static int avs_dai_hda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
338 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
339 struct snd_pcm_runtime *runtime = substream->runtime;
340 struct hdac_ext_stream *link_stream = runtime->private_data;
341 struct hdac_ext_link *link;
342 struct hda_codec *codec;
343 struct hdac_bus *bus;
344 unsigned int format_val;
347 if (link_stream->link_prepared)
350 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
351 bus = &codec->bus->core;
352 format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
353 runtime->sample_bits, 0);
355 snd_hdac_ext_stream_decouple(bus, link_stream, true);
356 snd_hdac_ext_stream_reset(link_stream);
357 snd_hdac_ext_stream_setup(link_stream, format_val);
359 link = snd_hdac_ext_bus_get_hlink_by_addr(bus, codec->core.addr);
363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
364 snd_hdac_ext_bus_link_set_stream_id(link, hdac_stream(link_stream)->stream_tag);
366 ret = avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
370 link_stream->link_prepared = true;
374 static int avs_dai_hda_be_trigger(struct snd_pcm_substream *substream, int cmd,
375 struct snd_soc_dai *dai)
377 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
378 struct hdac_ext_stream *link_stream;
379 struct avs_dma_data *data;
382 dev_dbg(dai->dev, "entry %s cmd=%d\n", __func__, cmd);
384 data = snd_soc_dai_get_dma_data(dai, substream);
385 link_stream = substream->runtime->private_data;
388 case SNDRV_PCM_TRIGGER_RESUME:
389 if (rtd->dai_link->ignore_suspend)
392 case SNDRV_PCM_TRIGGER_START:
393 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
394 snd_hdac_ext_stream_start(link_stream);
396 ret = avs_path_pause(data->path);
398 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
402 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
404 dev_err(dai->dev, "run BE path failed: %d\n", ret);
407 case SNDRV_PCM_TRIGGER_SUSPEND:
408 if (rtd->dai_link->ignore_suspend)
411 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
412 case SNDRV_PCM_TRIGGER_STOP:
413 ret = avs_path_pause(data->path);
415 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
417 snd_hdac_ext_stream_clear(link_stream);
419 ret = avs_path_reset(data->path);
421 dev_err(dai->dev, "reset BE path failed: %d\n", ret);
432 static const struct snd_soc_dai_ops avs_dai_hda_be_ops = {
433 .startup = avs_dai_hda_be_startup,
434 .shutdown = avs_dai_hda_be_shutdown,
435 .hw_params = avs_dai_hda_be_hw_params,
436 .hw_free = avs_dai_hda_be_hw_free,
437 .prepare = avs_dai_hda_be_prepare,
438 .trigger = avs_dai_hda_be_trigger,
441 static const unsigned int rates[] = {
442 8000, 11025, 12000, 16000,
443 22050, 24000, 32000, 44100,
444 48000, 64000, 88200, 96000,
445 128000, 176400, 192000,
448 static const struct snd_pcm_hw_constraint_list hw_rates = {
449 .count = ARRAY_SIZE(rates),
454 const struct snd_soc_dai_ops avs_dai_fe_ops;
456 static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
458 struct snd_pcm_runtime *runtime = substream->runtime;
459 struct avs_dma_data *data;
460 struct avs_dev *adev = to_avs_dev(dai->dev);
461 struct hdac_bus *bus = &adev->base.core;
462 struct hdac_ext_stream *host_stream;
465 ret = avs_dai_startup(substream, dai, true, &avs_dai_fe_ops);
469 data = snd_soc_dai_get_dma_data(dai, substream);
471 host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST);
477 data->host_stream = host_stream;
478 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
479 /* avoid wrap-around with wall-clock */
480 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000);
481 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_rates);
482 snd_pcm_set_sync(substream);
484 dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p",
485 __func__, hdac_stream(host_stream)->stream_tag, substream);
490 static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
492 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
493 struct avs_dev *adev = to_avs_dev(dai->dev);
494 struct avs_dma_data *data;
496 if (rtd->dai_link->ignore_suspend)
497 adev->num_lp_paths--;
499 data = snd_soc_dai_get_dma_data(dai, substream);
501 snd_soc_dai_set_dma_data(dai, substream, NULL);
502 snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST);
506 static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
509 struct snd_pcm_hw_params *be_hw_params = NULL;
510 struct snd_soc_pcm_runtime *fe, *be;
511 struct snd_soc_dpcm *dpcm;
512 struct avs_dma_data *data;
513 struct hdac_ext_stream *host_stream;
516 data = snd_soc_dai_get_dma_data(dai, substream);
520 host_stream = data->host_stream;
522 hdac_stream(host_stream)->bufsize = 0;
523 hdac_stream(host_stream)->period_bytes = 0;
524 hdac_stream(host_stream)->format_val = 0;
526 fe = asoc_substream_to_rtd(substream);
527 for_each_dpcm_be(fe, substream->stream, dpcm) {
529 be_hw_params = &be->dpcm[substream->stream].hw_params;
532 ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai,
533 hdac_stream(host_stream)->stream_tag - 1);
537 ret = avs_path_bind(data->path);
539 dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret);
546 avs_path_free(data->path);
549 snd_pcm_lib_free_pages(substream);
553 static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
555 struct avs_dma_data *data;
556 struct hdac_ext_stream *host_stream;
559 dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p",
560 __func__, substream, substream->runtime);
562 data = snd_soc_dai_get_dma_data(dai, substream);
566 host_stream = data->host_stream;
568 ret = avs_path_unbind(data->path);
570 dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret);
572 avs_path_free(data->path);
574 snd_hdac_stream_cleanup(hdac_stream(host_stream));
575 hdac_stream(host_stream)->prepared = false;
580 static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
584 ret = __avs_dai_fe_hw_free(substream, dai);
585 snd_pcm_lib_free_pages(substream);
590 static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
592 struct snd_pcm_runtime *runtime = substream->runtime;
593 struct avs_dma_data *data;
594 struct avs_dev *adev = to_avs_dev(dai->dev);
595 struct hdac_ext_stream *host_stream;
596 struct hdac_bus *bus;
597 unsigned int format_val;
600 data = snd_soc_dai_get_dma_data(dai, substream);
601 host_stream = data->host_stream;
603 if (hdac_stream(host_stream)->prepared)
606 bus = hdac_stream(host_stream)->bus;
607 snd_hdac_ext_stream_decouple(bus, data->host_stream, true);
608 snd_hdac_stream_reset(hdac_stream(host_stream));
610 format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
611 runtime->sample_bits, 0);
613 ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val);
617 ret = snd_hdac_stream_setup(hdac_stream(host_stream));
621 ret = avs_dai_prepare(adev, substream, dai);
625 hdac_stream(host_stream)->prepared = true;
629 static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
631 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
632 struct avs_dma_data *data;
633 struct hdac_ext_stream *host_stream;
634 struct hdac_bus *bus;
638 data = snd_soc_dai_get_dma_data(dai, substream);
639 host_stream = data->host_stream;
640 bus = hdac_stream(host_stream)->bus;
643 case SNDRV_PCM_TRIGGER_RESUME:
644 if (rtd->dai_link->ignore_suspend)
647 case SNDRV_PCM_TRIGGER_START:
648 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
649 spin_lock_irqsave(&bus->reg_lock, flags);
650 snd_hdac_stream_start(hdac_stream(host_stream), true);
651 spin_unlock_irqrestore(&bus->reg_lock, flags);
653 /* Timeout on DRSM poll shall not stop the resume so ignore the result. */
654 if (cmd == SNDRV_PCM_TRIGGER_RESUME)
655 snd_hdac_stream_wait_drsm(hdac_stream(host_stream));
657 ret = avs_path_pause(data->path);
659 dev_err(dai->dev, "pause FE path failed: %d\n", ret);
663 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
665 dev_err(dai->dev, "run FE path failed: %d\n", ret);
669 case SNDRV_PCM_TRIGGER_SUSPEND:
670 if (rtd->dai_link->ignore_suspend)
673 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
674 case SNDRV_PCM_TRIGGER_STOP:
675 ret = avs_path_pause(data->path);
677 dev_err(dai->dev, "pause FE path failed: %d\n", ret);
679 spin_lock_irqsave(&bus->reg_lock, flags);
680 snd_hdac_stream_stop(hdac_stream(host_stream));
681 spin_unlock_irqrestore(&bus->reg_lock, flags);
683 ret = avs_path_reset(data->path);
685 dev_err(dai->dev, "reset FE path failed: %d\n", ret);
696 const struct snd_soc_dai_ops avs_dai_fe_ops = {
697 .startup = avs_dai_fe_startup,
698 .shutdown = avs_dai_fe_shutdown,
699 .hw_params = avs_dai_fe_hw_params,
700 .hw_free = avs_dai_fe_hw_free,
701 .prepare = avs_dai_fe_prepare,
702 .trigger = avs_dai_fe_trigger,
705 static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count,
708 struct snd_soc_component *component = file->private_data;
709 struct snd_soc_card *card = component->card;
710 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
714 len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix,
715 mach->tplg_filename);
717 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
720 static const struct file_operations topology_name_fops = {
722 .read = topology_name_read,
723 .llseek = default_llseek,
726 static int avs_component_load_libraries(struct avs_soc_component *acomp)
728 struct avs_tplg *tplg = acomp->tplg;
729 struct avs_dev *adev = to_avs_dev(acomp->base.dev);
735 /* Parent device may be asleep and library loading involves IPCs. */
736 ret = pm_runtime_resume_and_get(adev->dev);
740 avs_hda_power_gating_enable(adev, false);
741 avs_hda_clock_gating_enable(adev, false);
742 avs_hda_l1sen_enable(adev, false);
744 ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
746 avs_hda_l1sen_enable(adev, true);
747 avs_hda_clock_gating_enable(adev, true);
748 avs_hda_power_gating_enable(adev, true);
751 ret = avs_module_info_init(adev, false);
753 pm_runtime_mark_last_busy(adev->dev);
754 pm_runtime_put_autosuspend(adev->dev);
759 static int avs_component_probe(struct snd_soc_component *component)
761 struct snd_soc_card *card = component->card;
762 struct snd_soc_acpi_mach *mach;
763 struct avs_soc_component *acomp;
764 struct avs_dev *adev;
768 dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name);
769 mach = dev_get_platdata(card->dev);
770 acomp = to_avs_soc_component(component);
771 adev = to_avs_dev(component->dev);
773 acomp->tplg = avs_tplg_new(component);
777 if (!mach->tplg_filename)
780 /* Load specified topology and create debugfs for it. */
781 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
782 mach->tplg_filename);
786 ret = avs_load_topology(component, filename);
791 ret = avs_component_load_libraries(acomp);
793 dev_err(card->dev, "libraries loading failed: %d\n", ret);
798 debugfs_create_file("topology_name", 0444, component->debugfs_root, component,
799 &topology_name_fops);
801 mutex_lock(&adev->comp_list_mutex);
802 list_add_tail(&acomp->node, &adev->comp_list);
803 mutex_unlock(&adev->comp_list_mutex);
808 avs_remove_topology(component);
812 static void avs_component_remove(struct snd_soc_component *component)
814 struct avs_soc_component *acomp = to_avs_soc_component(component);
815 struct snd_soc_acpi_mach *mach;
816 struct avs_dev *adev = to_avs_dev(component->dev);
819 mach = dev_get_platdata(component->card->dev);
821 mutex_lock(&adev->comp_list_mutex);
822 list_del(&acomp->node);
823 mutex_unlock(&adev->comp_list_mutex);
825 if (mach->tplg_filename) {
826 ret = avs_remove_topology(component);
828 dev_err(component->dev, "unload topology failed: %d\n", ret);
832 static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data)
834 struct snd_pcm_substream *substream;
835 struct snd_soc_pcm_runtime *rtd;
838 substream = data->substream;
839 rtd = snd_pcm_substream_chip(substream);
841 ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai);
843 dev_err(dai->dev, "hw_params on resume failed: %d\n", ret);
848 static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
850 struct hdac_ext_stream *host_stream;
851 struct hdac_stream *hstream;
852 struct hdac_bus *bus;
855 host_stream = data->host_stream;
856 hstream = hdac_stream(host_stream);
857 bus = hdac_stream(host_stream)->bus;
859 /* Set DRSM before programming stream and position registers. */
860 snd_hdac_stream_drsm_enable(bus, true, hstream->index);
862 ret = dai->driver->ops->prepare(data->substream, dai);
864 dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret);
868 writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL);
869 writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU);
870 writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL);
871 writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU);
873 /* As per HW spec recommendation, program LPIB and DPIB to the same value. */
874 snd_hdac_stream_set_lpib(hstream, hstream->lpib);
875 snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib);
880 static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
884 ret = dai->driver->ops->prepare(data->substream, dai);
886 dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret);
891 static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
893 struct hdac_ext_stream *host_stream;
896 host_stream = data->host_stream;
898 /* Store position addresses so we can resume from them later on. */
899 hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream));
900 host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL);
901 host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU);
902 host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL);
903 host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU);
905 ret = __avs_dai_fe_hw_free(data->substream, dai);
907 dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret);
912 static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
916 ret = dai->driver->ops->hw_free(data->substream, dai);
918 dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret);
923 static int avs_component_pm_op(struct snd_soc_component *component, bool be,
924 int (*op)(struct snd_soc_dai *, struct avs_dma_data *))
926 struct snd_soc_pcm_runtime *rtd;
927 struct avs_dma_data *data;
928 struct snd_soc_dai *dai;
931 for_each_component_dais(component, dai) {
932 data = dai->playback_dma_data;
934 rtd = snd_pcm_substream_chip(data->substream);
935 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
938 __snd_pcm_set_state(data->substream->runtime,
939 SNDRV_PCM_STATE_DISCONNECTED);
945 data = dai->capture_dma_data;
947 rtd = snd_pcm_substream_chip(data->substream);
948 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
951 __snd_pcm_set_state(data->substream->runtime,
952 SNDRV_PCM_STATE_DISCONNECTED);
962 static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be)
964 return avs_component_pm_op(component, be, &avs_dai_resume_hw_params);
967 static int avs_component_resume_prepare(struct snd_soc_component *component, bool be)
969 int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
972 prepare_cb = &avs_dai_resume_be_prepare;
974 prepare_cb = &avs_dai_resume_fe_prepare;
976 return avs_component_pm_op(component, be, prepare_cb);
979 static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be)
981 int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
984 hw_free_cb = &avs_dai_suspend_be_hw_free;
986 hw_free_cb = &avs_dai_suspend_fe_hw_free;
988 return avs_component_pm_op(component, be, hw_free_cb);
991 static int avs_component_suspend(struct snd_soc_component *component)
996 * When freeing paths, FEs need to be first as they perform
999 ret = avs_component_suspend_hw_free(component, false);
1003 return avs_component_suspend_hw_free(component, true);
1006 static int avs_component_resume(struct snd_soc_component *component)
1011 * When creating paths, FEs need to be last as they perform
1014 ret = avs_component_resume_hw_params(component, true);
1018 ret = avs_component_resume_hw_params(component, false);
1022 /* It is expected that the LINK stream is prepared first. */
1023 ret = avs_component_resume_prepare(component, true);
1027 return avs_component_resume_prepare(component, false);
1030 static const struct snd_pcm_hardware avs_pcm_hardware = {
1031 .info = SNDRV_PCM_INFO_MMAP |
1032 SNDRV_PCM_INFO_MMAP_VALID |
1033 SNDRV_PCM_INFO_INTERLEAVED |
1034 SNDRV_PCM_INFO_PAUSE |
1035 SNDRV_PCM_INFO_RESUME |
1036 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
1037 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1038 SNDRV_PCM_FMTBIT_S24_LE |
1039 SNDRV_PCM_FMTBIT_S32_LE,
1040 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
1041 .period_bytes_min = 128,
1042 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
1044 .periods_max = AZX_MAX_FRAG,
1048 static int avs_component_open(struct snd_soc_component *component,
1049 struct snd_pcm_substream *substream)
1051 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1053 /* only FE DAI links are handled here */
1054 if (rtd->dai_link->no_pcm)
1057 return snd_soc_set_runtime_hwparams(substream, &avs_pcm_hardware);
1060 static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream)
1062 return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
1063 (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index));
1066 static snd_pcm_uframes_t
1067 avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream)
1069 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1070 struct avs_dma_data *data;
1071 struct hdac_ext_stream *host_stream;
1074 data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
1075 if (!data->host_stream)
1078 host_stream = data->host_stream;
1079 pos = avs_hda_stream_dpib_read(host_stream);
1081 if (pos >= hdac_stream(host_stream)->bufsize)
1084 return bytes_to_frames(substream->runtime, pos);
1087 static int avs_component_mmap(struct snd_soc_component *component,
1088 struct snd_pcm_substream *substream,
1089 struct vm_area_struct *vma)
1091 return snd_pcm_lib_default_mmap(substream, vma);
1094 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
1096 static int avs_component_construct(struct snd_soc_component *component,
1097 struct snd_soc_pcm_runtime *rtd)
1099 struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
1100 struct snd_pcm *pcm = rtd->pcm;
1102 if (dai->driver->playback.channels_min)
1103 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1104 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1107 if (dai->driver->capture.channels_min)
1108 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1109 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1115 static const struct snd_soc_component_driver avs_component_driver = {
1117 .probe = avs_component_probe,
1118 .remove = avs_component_remove,
1119 .suspend = avs_component_suspend,
1120 .resume = avs_component_resume,
1121 .open = avs_component_open,
1122 .pointer = avs_component_pointer,
1123 .mmap = avs_component_mmap,
1124 .pcm_construct = avs_component_construct,
1125 .module_get_upon_open = 1, /* increment refcount when a pcm is opened */
1126 .topology_name_prefix = "intel/avs",
1129 static int avs_soc_component_register(struct device *dev, const char *name,
1130 const struct snd_soc_component_driver *drv,
1131 struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais)
1133 struct avs_soc_component *acomp;
1136 acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL);
1140 ret = snd_soc_component_initialize(&acomp->base, drv, dev);
1144 /* force name change after ASoC is done with its init */
1145 acomp->base.name = name;
1146 INIT_LIST_HEAD(&acomp->node);
1148 return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais);
1151 static struct snd_soc_dai_driver dmic_cpu_dais[] = {
1154 .ops = &avs_dai_nonhda_be_ops,
1156 .stream_name = "DMIC Rx",
1159 .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000,
1160 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
1164 .name = "DMIC WoV Pin",
1165 .ops = &avs_dai_nonhda_be_ops,
1167 .stream_name = "DMIC WoV Rx",
1170 .rates = SNDRV_PCM_RATE_16000,
1171 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1176 int avs_dmic_platform_register(struct avs_dev *adev, const char *name)
1178 return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais,
1179 ARRAY_SIZE(dmic_cpu_dais));
1182 static const struct snd_soc_dai_driver i2s_dai_template = {
1183 .ops = &avs_dai_nonhda_be_ops,
1187 .rates = SNDRV_PCM_RATE_8000_192000 |
1188 SNDRV_PCM_RATE_KNOT,
1189 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1190 SNDRV_PCM_FMTBIT_S24_LE |
1191 SNDRV_PCM_FMTBIT_S32_LE,
1196 .rates = SNDRV_PCM_RATE_8000_192000 |
1197 SNDRV_PCM_RATE_KNOT,
1198 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1199 SNDRV_PCM_FMTBIT_S24_LE |
1200 SNDRV_PCM_FMTBIT_S32_LE,
1204 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
1205 unsigned long *tdms)
1207 struct snd_soc_dai_driver *cpus, *dai;
1208 size_t ssp_count, cpu_count;
1211 ssp_count = adev->hw_cfg.i2s_caps.ctrl_count;
1212 cpu_count = hweight_long(port_mask);
1214 for_each_set_bit(i, &port_mask, ssp_count)
1215 cpu_count += hweight_long(tdms[i]);
1217 cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL);
1222 for_each_set_bit(i, &port_mask, ssp_count) {
1223 memcpy(dai, &i2s_dai_template, sizeof(*dai));
1226 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i);
1227 dai->playback.stream_name =
1228 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i);
1229 dai->capture.stream_name =
1230 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i);
1232 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1240 for_each_set_bit(i, &port_mask, ssp_count) {
1241 for_each_set_bit(j, &tdms[i], ssp_count) {
1242 memcpy(dai, &i2s_dai_template, sizeof(*dai));
1245 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j);
1246 dai->playback.stream_name =
1247 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j);
1248 dai->capture.stream_name =
1249 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j);
1251 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1258 return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count);
1261 /* HD-Audio CPU DAI template */
1262 static const struct snd_soc_dai_driver hda_cpu_dai = {
1263 .ops = &avs_dai_hda_be_ops,
1267 .rates = SNDRV_PCM_RATE_8000_192000,
1268 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1269 SNDRV_PCM_FMTBIT_S24_LE |
1270 SNDRV_PCM_FMTBIT_S32_LE,
1275 .rates = SNDRV_PCM_RATE_8000_192000,
1276 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1277 SNDRV_PCM_FMTBIT_S24_LE |
1278 SNDRV_PCM_FMTBIT_S32_LE,
1282 static void avs_component_hda_unregister_dais(struct snd_soc_component *component)
1284 struct snd_soc_acpi_mach *mach;
1285 struct snd_soc_dai *dai, *save;
1286 struct hda_codec *codec;
1289 mach = dev_get_platdata(component->card->dev);
1290 codec = mach->pdata;
1291 sprintf(name, "%s-cpu", dev_name(&codec->core.dev));
1293 for_each_component_dais_safe(component, dai, save) {
1294 if (!strstr(dai->driver->name, name))
1297 snd_soc_dapm_free_widget(dai->playback_widget);
1298 snd_soc_dapm_free_widget(dai->capture_widget);
1299 snd_soc_unregister_dai(dai);
1303 static int avs_component_hda_probe(struct snd_soc_component *component)
1305 struct snd_soc_dapm_context *dapm;
1306 struct snd_soc_dai_driver *dais;
1307 struct snd_soc_acpi_mach *mach;
1308 struct hda_codec *codec;
1309 struct hda_pcm *pcm;
1311 int pcm_count = 0, ret, i;
1313 mach = dev_get_platdata(component->card->dev);
1317 codec = mach->pdata;
1318 if (list_empty(&codec->pcm_list_head))
1320 list_for_each_entry(pcm, &codec->pcm_list_head, list)
1323 dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais),
1328 cname = dev_name(&codec->core.dev);
1329 dapm = snd_soc_component_get_dapm(component);
1330 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
1332 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
1333 struct snd_soc_dai *dai;
1335 memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais));
1337 dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL,
1338 "%s-cpu%d", cname, i);
1339 if (!dais[i].name) {
1344 if (pcm->stream[0].substreams) {
1345 dais[i].playback.stream_name =
1346 devm_kasprintf(component->dev, GFP_KERNEL,
1347 "%s-cpu%d Tx", cname, i);
1348 if (!dais[i].playback.stream_name) {
1354 if (pcm->stream[1].substreams) {
1355 dais[i].capture.stream_name =
1356 devm_kasprintf(component->dev, GFP_KERNEL,
1357 "%s-cpu%d Rx", cname, i);
1358 if (!dais[i].capture.stream_name) {
1364 dai = snd_soc_register_dai(component, &dais[i], false);
1366 dev_err(component->dev, "register dai for %s failed\n",
1372 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1374 dev_err(component->dev, "create widgets failed: %d\n",
1380 ret = avs_component_probe(component);
1383 avs_component_hda_unregister_dais(component);
1388 static void avs_component_hda_remove(struct snd_soc_component *component)
1390 avs_component_hda_unregister_dais(component);
1391 avs_component_remove(component);
1394 static int avs_component_hda_open(struct snd_soc_component *component,
1395 struct snd_pcm_substream *substream)
1397 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1398 struct hdac_ext_stream *link_stream;
1399 struct hda_codec *codec;
1401 if (!rtd->dai_link->no_pcm) {
1402 struct snd_pcm_hardware hwparams = avs_pcm_hardware;
1403 struct snd_soc_pcm_runtime *be;
1404 struct snd_soc_dpcm *dpcm;
1405 int dir = substream->stream;
1408 * Support the DPCM reparenting while still fulfilling expectations of HDAudio
1409 * common code - a valid stream pointer at substream->runtime->private_data -
1410 * by having all FEs point to the same private data.
1412 for_each_dpcm_be(rtd, dir, dpcm) {
1413 struct snd_pcm_substream *be_substream;
1416 if (be->dpcm[dir].users == 1)
1419 be_substream = snd_soc_dpcm_get_substream(be, dir);
1420 substream->runtime->private_data = be_substream->runtime->private_data;
1424 /* RESUME unsupported for de-coupled HD-Audio capture. */
1425 if (dir == SNDRV_PCM_STREAM_CAPTURE)
1426 hwparams.info &= ~SNDRV_PCM_INFO_RESUME;
1428 return snd_soc_set_runtime_hwparams(substream, &hwparams);
1431 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
1432 link_stream = snd_hdac_ext_stream_assign(&codec->bus->core, substream,
1433 HDAC_EXT_STREAM_TYPE_LINK);
1437 substream->runtime->private_data = link_stream;
1441 static int avs_component_hda_close(struct snd_soc_component *component,
1442 struct snd_pcm_substream *substream)
1444 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1445 struct hdac_ext_stream *link_stream;
1447 /* only BE DAI links are handled here */
1448 if (!rtd->dai_link->no_pcm)
1451 link_stream = substream->runtime->private_data;
1452 snd_hdac_ext_stream_release(link_stream, HDAC_EXT_STREAM_TYPE_LINK);
1453 substream->runtime->private_data = NULL;
1458 static const struct snd_soc_component_driver avs_hda_component_driver = {
1459 .name = "avs-hda-pcm",
1460 .probe = avs_component_hda_probe,
1461 .remove = avs_component_hda_remove,
1462 .suspend = avs_component_suspend,
1463 .resume = avs_component_resume,
1464 .open = avs_component_hda_open,
1465 .close = avs_component_hda_close,
1466 .pointer = avs_component_pointer,
1467 .mmap = avs_component_mmap,
1468 .pcm_construct = avs_component_construct,
1470 * hda platform component's probe() is dependent on
1471 * codec->pcm_list_head, it needs to be initialized after codec
1472 * component. remove_order is here for completeness sake
1474 .probe_order = SND_SOC_COMP_ORDER_LATE,
1475 .remove_order = SND_SOC_COMP_ORDER_EARLY,
1476 .module_get_upon_open = 1,
1477 .topology_name_prefix = "intel/avs",
1480 int avs_hda_platform_register(struct avs_dev *adev, const char *name)
1482 return avs_soc_component_register(adev->dev, name,
1483 &avs_hda_component_driver, NULL, 0);