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 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);
84 static int avs_dai_hw_params(struct snd_pcm_substream *substream,
85 struct snd_pcm_hw_params *fe_hw_params,
86 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
89 struct avs_dma_data *data;
90 struct avs_path *path;
91 struct avs_dev *adev = to_avs_dev(dai->dev);
94 data = snd_soc_dai_get_dma_data(dai, substream);
96 dev_dbg(dai->dev, "%s FE hw_params str %p rtd %p",
97 __func__, substream, substream->runtime);
98 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
99 params_rate(fe_hw_params), params_channels(fe_hw_params),
100 params_width(fe_hw_params), params_physical_width(fe_hw_params));
102 dev_dbg(dai->dev, "%s BE hw_params str %p rtd %p",
103 __func__, substream, substream->runtime);
104 dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
105 params_rate(be_hw_params), params_channels(be_hw_params),
106 params_width(be_hw_params), params_physical_width(be_hw_params));
108 path = avs_path_create(adev, dma_id, data->template, fe_hw_params, be_hw_params);
111 dev_err(dai->dev, "create path failed: %d\n", ret);
119 static int avs_dai_be_hw_params(struct snd_pcm_substream *substream,
120 struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
123 struct snd_pcm_hw_params *fe_hw_params = NULL;
124 struct snd_soc_pcm_runtime *fe, *be;
125 struct snd_soc_dpcm *dpcm;
127 be = asoc_substream_to_rtd(substream);
128 for_each_dpcm_fe(be, substream->stream, dpcm) {
130 fe_hw_params = &fe->dpcm[substream->stream].hw_params;
133 return avs_dai_hw_params(substream, fe_hw_params, be_hw_params, dai, dma_id);
136 static int avs_dai_prepare(struct avs_dev *adev, struct snd_pcm_substream *substream,
137 struct snd_soc_dai *dai)
139 struct avs_dma_data *data;
142 data = snd_soc_dai_get_dma_data(dai, substream);
146 ret = avs_path_reset(data->path);
148 dev_err(dai->dev, "reset path failed: %d\n", ret);
152 ret = avs_path_pause(data->path);
154 dev_err(dai->dev, "pause path failed: %d\n", ret);
158 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops;
160 static int avs_dai_nonhda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
162 return avs_dai_startup(substream, dai, false, &avs_dai_nonhda_be_ops);
165 static void avs_dai_nonhda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
167 struct avs_dma_data *data;
169 data = snd_soc_dai_get_dma_data(dai, substream);
171 snd_soc_dai_set_dma_data(dai, substream, NULL);
175 static int avs_dai_nonhda_be_hw_params(struct snd_pcm_substream *substream,
176 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
178 struct avs_dma_data *data;
180 data = snd_soc_dai_get_dma_data(dai, substream);
184 /* Actual port-id comes from topology. */
185 return avs_dai_be_hw_params(substream, hw_params, dai, 0);
188 static int avs_dai_nonhda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
190 struct avs_dma_data *data;
192 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
194 data = snd_soc_dai_get_dma_data(dai, substream);
196 avs_path_free(data->path);
203 static int avs_dai_nonhda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
205 return avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
208 static int avs_dai_nonhda_be_trigger(struct snd_pcm_substream *substream, int cmd,
209 struct snd_soc_dai *dai)
211 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
212 struct avs_dma_data *data;
215 data = snd_soc_dai_get_dma_data(dai, substream);
218 case SNDRV_PCM_TRIGGER_RESUME:
219 if (rtd->dai_link->ignore_suspend)
222 case SNDRV_PCM_TRIGGER_START:
223 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
224 ret = avs_path_pause(data->path);
226 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
230 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
232 dev_err(dai->dev, "run BE path failed: %d\n", ret);
235 case SNDRV_PCM_TRIGGER_SUSPEND:
236 if (rtd->dai_link->ignore_suspend)
239 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
240 case SNDRV_PCM_TRIGGER_STOP:
241 ret = avs_path_pause(data->path);
243 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
245 ret = avs_path_reset(data->path);
247 dev_err(dai->dev, "reset BE path failed: %d\n", ret);
258 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops = {
259 .startup = avs_dai_nonhda_be_startup,
260 .shutdown = avs_dai_nonhda_be_shutdown,
261 .hw_params = avs_dai_nonhda_be_hw_params,
262 .hw_free = avs_dai_nonhda_be_hw_free,
263 .prepare = avs_dai_nonhda_be_prepare,
264 .trigger = avs_dai_nonhda_be_trigger,
267 static const struct snd_soc_dai_ops avs_dai_hda_be_ops;
269 static int avs_dai_hda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
271 return avs_dai_startup(substream, dai, false, &avs_dai_hda_be_ops);
274 static void avs_dai_hda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
276 return avs_dai_nonhda_be_shutdown(substream, dai);
279 static int avs_dai_hda_be_hw_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
282 struct avs_dma_data *data;
283 struct hdac_ext_stream *link_stream;
285 data = snd_soc_dai_get_dma_data(dai, substream);
289 link_stream = substream->runtime->private_data;
291 return avs_dai_be_hw_params(substream, hw_params, dai,
292 hdac_stream(link_stream)->stream_tag - 1);
295 static int avs_dai_hda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
297 struct avs_dma_data *data;
298 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
299 struct hdac_ext_stream *link_stream;
300 struct hdac_ext_link *link;
301 struct hda_codec *codec;
303 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
305 data = snd_soc_dai_get_dma_data(dai, substream);
309 link_stream = substream->runtime->private_data;
310 link_stream->link_prepared = false;
311 avs_path_free(data->path);
314 /* clear link <-> stream mapping */
315 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
316 link = snd_hdac_ext_bus_get_hlink_by_addr(&codec->bus->core, codec->core.addr);
320 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
321 snd_hdac_ext_bus_link_clear_stream_id(link, hdac_stream(link_stream)->stream_tag);
326 static int avs_dai_hda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
328 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
329 struct snd_pcm_runtime *runtime = substream->runtime;
330 struct hdac_ext_stream *link_stream = runtime->private_data;
331 struct hdac_ext_link *link;
332 struct hda_codec *codec;
333 struct hdac_bus *bus;
334 unsigned int format_val;
337 if (link_stream->link_prepared)
340 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
341 bus = &codec->bus->core;
342 format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
343 runtime->sample_bits, 0);
345 snd_hdac_ext_stream_decouple(bus, link_stream, true);
346 snd_hdac_ext_stream_reset(link_stream);
347 snd_hdac_ext_stream_setup(link_stream, format_val);
349 link = snd_hdac_ext_bus_get_hlink_by_addr(bus, codec->core.addr);
353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
354 snd_hdac_ext_bus_link_set_stream_id(link, hdac_stream(link_stream)->stream_tag);
356 ret = avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
360 link_stream->link_prepared = true;
364 static int avs_dai_hda_be_trigger(struct snd_pcm_substream *substream, int cmd,
365 struct snd_soc_dai *dai)
367 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
368 struct hdac_ext_stream *link_stream;
369 struct avs_dma_data *data;
372 dev_dbg(dai->dev, "entry %s cmd=%d\n", __func__, cmd);
374 data = snd_soc_dai_get_dma_data(dai, substream);
375 link_stream = substream->runtime->private_data;
378 case SNDRV_PCM_TRIGGER_RESUME:
379 if (rtd->dai_link->ignore_suspend)
382 case SNDRV_PCM_TRIGGER_START:
383 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
384 snd_hdac_ext_stream_start(link_stream);
386 ret = avs_path_pause(data->path);
388 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
392 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
394 dev_err(dai->dev, "run BE path failed: %d\n", ret);
397 case SNDRV_PCM_TRIGGER_SUSPEND:
398 if (rtd->dai_link->ignore_suspend)
401 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
402 case SNDRV_PCM_TRIGGER_STOP:
403 ret = avs_path_pause(data->path);
405 dev_err(dai->dev, "pause BE path failed: %d\n", ret);
407 snd_hdac_ext_stream_clear(link_stream);
409 ret = avs_path_reset(data->path);
411 dev_err(dai->dev, "reset BE path failed: %d\n", ret);
422 static const struct snd_soc_dai_ops avs_dai_hda_be_ops = {
423 .startup = avs_dai_hda_be_startup,
424 .shutdown = avs_dai_hda_be_shutdown,
425 .hw_params = avs_dai_hda_be_hw_params,
426 .hw_free = avs_dai_hda_be_hw_free,
427 .prepare = avs_dai_hda_be_prepare,
428 .trigger = avs_dai_hda_be_trigger,
431 static const unsigned int rates[] = {
432 8000, 11025, 12000, 16000,
433 22050, 24000, 32000, 44100,
434 48000, 64000, 88200, 96000,
435 128000, 176400, 192000,
438 static const struct snd_pcm_hw_constraint_list hw_rates = {
439 .count = ARRAY_SIZE(rates),
444 const struct snd_soc_dai_ops avs_dai_fe_ops;
446 static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
448 struct snd_pcm_runtime *runtime = substream->runtime;
449 struct avs_dma_data *data;
450 struct avs_dev *adev = to_avs_dev(dai->dev);
451 struct hdac_bus *bus = &adev->base.core;
452 struct hdac_ext_stream *host_stream;
455 ret = avs_dai_startup(substream, dai, true, &avs_dai_fe_ops);
459 data = snd_soc_dai_get_dma_data(dai, substream);
461 host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST);
467 data->host_stream = host_stream;
468 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
469 /* avoid wrap-around with wall-clock */
470 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000);
471 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_rates);
472 snd_pcm_set_sync(substream);
474 dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p",
475 __func__, hdac_stream(host_stream)->stream_tag, substream);
480 static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
482 struct avs_dma_data *data;
484 data = snd_soc_dai_get_dma_data(dai, substream);
486 snd_soc_dai_set_dma_data(dai, substream, NULL);
487 snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST);
491 static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream,
492 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
494 struct snd_pcm_hw_params *be_hw_params = NULL;
495 struct snd_soc_pcm_runtime *fe, *be;
496 struct snd_soc_dpcm *dpcm;
497 struct avs_dma_data *data;
498 struct hdac_ext_stream *host_stream;
501 data = snd_soc_dai_get_dma_data(dai, substream);
505 host_stream = data->host_stream;
507 hdac_stream(host_stream)->bufsize = 0;
508 hdac_stream(host_stream)->period_bytes = 0;
509 hdac_stream(host_stream)->format_val = 0;
511 fe = asoc_substream_to_rtd(substream);
512 for_each_dpcm_be(fe, substream->stream, dpcm) {
514 be_hw_params = &be->dpcm[substream->stream].hw_params;
517 ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai,
518 hdac_stream(host_stream)->stream_tag - 1);
522 ret = avs_path_bind(data->path);
524 dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret);
531 avs_path_free(data->path);
534 snd_pcm_lib_free_pages(substream);
538 static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
540 struct avs_dma_data *data;
541 struct hdac_ext_stream *host_stream;
544 dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p",
545 __func__, substream, substream->runtime);
547 data = snd_soc_dai_get_dma_data(dai, substream);
551 host_stream = data->host_stream;
553 ret = avs_path_unbind(data->path);
555 dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret);
557 avs_path_free(data->path);
559 snd_hdac_stream_cleanup(hdac_stream(host_stream));
560 hdac_stream(host_stream)->prepared = false;
565 static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
569 ret = __avs_dai_fe_hw_free(substream, dai);
570 snd_pcm_lib_free_pages(substream);
575 static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
577 struct snd_pcm_runtime *runtime = substream->runtime;
578 struct avs_dma_data *data;
579 struct avs_dev *adev = to_avs_dev(dai->dev);
580 struct hdac_ext_stream *host_stream;
581 struct hdac_bus *bus;
582 unsigned int format_val;
585 data = snd_soc_dai_get_dma_data(dai, substream);
586 host_stream = data->host_stream;
588 if (hdac_stream(host_stream)->prepared)
591 bus = hdac_stream(host_stream)->bus;
592 snd_hdac_ext_stream_decouple(bus, data->host_stream, true);
593 snd_hdac_stream_reset(hdac_stream(host_stream));
595 format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
596 runtime->sample_bits, 0);
598 ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val);
602 ret = snd_hdac_stream_setup(hdac_stream(host_stream));
606 ret = avs_dai_prepare(adev, substream, dai);
610 hdac_stream(host_stream)->prepared = true;
614 static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
616 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
617 struct avs_dma_data *data;
618 struct hdac_ext_stream *host_stream;
619 struct hdac_bus *bus;
623 data = snd_soc_dai_get_dma_data(dai, substream);
624 host_stream = data->host_stream;
625 bus = hdac_stream(host_stream)->bus;
628 case SNDRV_PCM_TRIGGER_RESUME:
629 if (rtd->dai_link->ignore_suspend)
632 case SNDRV_PCM_TRIGGER_START:
633 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
634 spin_lock_irqsave(&bus->reg_lock, flags);
635 snd_hdac_stream_start(hdac_stream(host_stream), true);
636 spin_unlock_irqrestore(&bus->reg_lock, flags);
638 /* Timeout on DRSM poll shall not stop the resume so ignore the result. */
639 if (cmd == SNDRV_PCM_TRIGGER_RESUME)
640 snd_hdac_stream_wait_drsm(hdac_stream(host_stream));
642 ret = avs_path_pause(data->path);
644 dev_err(dai->dev, "pause FE path failed: %d\n", ret);
648 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
650 dev_err(dai->dev, "run FE path failed: %d\n", ret);
654 case SNDRV_PCM_TRIGGER_SUSPEND:
655 if (rtd->dai_link->ignore_suspend)
658 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
659 case SNDRV_PCM_TRIGGER_STOP:
660 ret = avs_path_pause(data->path);
662 dev_err(dai->dev, "pause FE path failed: %d\n", ret);
664 spin_lock_irqsave(&bus->reg_lock, flags);
665 snd_hdac_stream_stop(hdac_stream(host_stream));
666 spin_unlock_irqrestore(&bus->reg_lock, flags);
668 ret = avs_path_reset(data->path);
670 dev_err(dai->dev, "reset FE path failed: %d\n", ret);
681 const struct snd_soc_dai_ops avs_dai_fe_ops = {
682 .startup = avs_dai_fe_startup,
683 .shutdown = avs_dai_fe_shutdown,
684 .hw_params = avs_dai_fe_hw_params,
685 .hw_free = avs_dai_fe_hw_free,
686 .prepare = avs_dai_fe_prepare,
687 .trigger = avs_dai_fe_trigger,
690 static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count,
693 struct snd_soc_component *component = file->private_data;
694 struct snd_soc_card *card = component->card;
695 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
699 len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix,
700 mach->tplg_filename);
702 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
705 static const struct file_operations topology_name_fops = {
707 .read = topology_name_read,
708 .llseek = default_llseek,
711 static int avs_component_load_libraries(struct avs_soc_component *acomp)
713 struct avs_tplg *tplg = acomp->tplg;
714 struct avs_dev *adev = to_avs_dev(acomp->base.dev);
720 /* Parent device may be asleep and library loading involves IPCs. */
721 ret = pm_runtime_resume_and_get(adev->dev);
725 avs_hda_clock_gating_enable(adev, false);
726 avs_hda_l1sen_enable(adev, false);
728 ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
730 avs_hda_l1sen_enable(adev, true);
731 avs_hda_clock_gating_enable(adev, true);
734 ret = avs_module_info_init(adev, false);
736 pm_runtime_mark_last_busy(adev->dev);
737 pm_runtime_put_autosuspend(adev->dev);
742 static int avs_component_probe(struct snd_soc_component *component)
744 struct snd_soc_card *card = component->card;
745 struct snd_soc_acpi_mach *mach;
746 struct avs_soc_component *acomp;
747 struct avs_dev *adev;
751 dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name);
752 mach = dev_get_platdata(card->dev);
753 acomp = to_avs_soc_component(component);
754 adev = to_avs_dev(component->dev);
756 acomp->tplg = avs_tplg_new(component);
760 if (!mach->tplg_filename)
763 /* Load specified topology and create debugfs for it. */
764 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
765 mach->tplg_filename);
769 ret = avs_load_topology(component, filename);
774 ret = avs_component_load_libraries(acomp);
776 dev_err(card->dev, "libraries loading failed: %d\n", ret);
781 debugfs_create_file("topology_name", 0444, component->debugfs_root, component,
782 &topology_name_fops);
784 mutex_lock(&adev->comp_list_mutex);
785 list_add_tail(&acomp->node, &adev->comp_list);
786 mutex_unlock(&adev->comp_list_mutex);
791 avs_remove_topology(component);
795 static void avs_component_remove(struct snd_soc_component *component)
797 struct avs_soc_component *acomp = to_avs_soc_component(component);
798 struct snd_soc_acpi_mach *mach;
799 struct avs_dev *adev = to_avs_dev(component->dev);
802 mach = dev_get_platdata(component->card->dev);
804 mutex_lock(&adev->comp_list_mutex);
805 list_del(&acomp->node);
806 mutex_unlock(&adev->comp_list_mutex);
808 if (mach->tplg_filename) {
809 ret = avs_remove_topology(component);
811 dev_err(component->dev, "unload topology failed: %d\n", ret);
815 static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data)
817 struct snd_pcm_substream *substream;
818 struct snd_soc_pcm_runtime *rtd;
821 substream = data->substream;
822 rtd = snd_pcm_substream_chip(substream);
824 ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai);
826 dev_err(dai->dev, "hw_params on resume failed: %d\n", ret);
831 static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
833 struct hdac_ext_stream *host_stream;
834 struct hdac_stream *hstream;
835 struct hdac_bus *bus;
838 host_stream = data->host_stream;
839 hstream = hdac_stream(host_stream);
840 bus = hdac_stream(host_stream)->bus;
842 /* Set DRSM before programming stream and position registers. */
843 snd_hdac_stream_drsm_enable(bus, true, hstream->index);
845 ret = dai->driver->ops->prepare(data->substream, dai);
847 dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret);
851 writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL);
852 writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU);
853 writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL);
854 writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU);
856 /* As per HW spec recommendation, program LPIB and DPIB to the same value. */
857 snd_hdac_stream_set_lpib(hstream, hstream->lpib);
858 snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib);
863 static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
867 ret = dai->driver->ops->prepare(data->substream, dai);
869 dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret);
874 static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
876 struct hdac_ext_stream *host_stream;
879 host_stream = data->host_stream;
881 /* Store position addresses so we can resume from them later on. */
882 hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream));
883 host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL);
884 host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU);
885 host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL);
886 host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU);
888 ret = __avs_dai_fe_hw_free(data->substream, dai);
890 dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret);
895 static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
899 ret = dai->driver->ops->hw_free(data->substream, dai);
901 dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret);
906 static int avs_component_pm_op(struct snd_soc_component *component, bool be,
907 int (*op)(struct snd_soc_dai *, struct avs_dma_data *))
909 struct snd_soc_pcm_runtime *rtd;
910 struct avs_dma_data *data;
911 struct snd_soc_dai *dai;
914 for_each_component_dais(component, dai) {
915 data = dai->playback_dma_data;
917 rtd = snd_pcm_substream_chip(data->substream);
918 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
925 data = dai->capture_dma_data;
927 rtd = snd_pcm_substream_chip(data->substream);
928 if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
939 static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be)
941 return avs_component_pm_op(component, be, &avs_dai_resume_hw_params);
944 static int avs_component_resume_prepare(struct snd_soc_component *component, bool be)
946 int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
949 prepare_cb = &avs_dai_resume_be_prepare;
951 prepare_cb = &avs_dai_resume_fe_prepare;
953 return avs_component_pm_op(component, be, prepare_cb);
956 static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be)
958 int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
961 hw_free_cb = &avs_dai_suspend_be_hw_free;
963 hw_free_cb = &avs_dai_suspend_fe_hw_free;
965 return avs_component_pm_op(component, be, hw_free_cb);
968 static int avs_component_suspend(struct snd_soc_component *component)
973 * When freeing paths, FEs need to be first as they perform
976 ret = avs_component_suspend_hw_free(component, false);
980 return avs_component_suspend_hw_free(component, true);
983 static int avs_component_resume(struct snd_soc_component *component)
988 * When creating paths, FEs need to be last as they perform
991 ret = avs_component_resume_hw_params(component, true);
995 ret = avs_component_resume_hw_params(component, false);
999 /* It is expected that the LINK stream is prepared first. */
1000 ret = avs_component_resume_prepare(component, true);
1004 return avs_component_resume_prepare(component, false);
1007 static int avs_component_open(struct snd_soc_component *component,
1008 struct snd_pcm_substream *substream)
1010 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1011 struct snd_pcm_hardware hwparams;
1013 /* only FE DAI links are handled here */
1014 if (rtd->dai_link->no_pcm)
1017 hwparams.info = SNDRV_PCM_INFO_MMAP |
1018 SNDRV_PCM_INFO_MMAP_VALID |
1019 SNDRV_PCM_INFO_INTERLEAVED |
1020 SNDRV_PCM_INFO_PAUSE |
1021 SNDRV_PCM_INFO_RESUME |
1022 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
1024 hwparams.formats = SNDRV_PCM_FMTBIT_S16_LE |
1025 SNDRV_PCM_FMTBIT_S24_LE |
1026 SNDRV_PCM_FMTBIT_S32_LE;
1027 hwparams.period_bytes_min = 128;
1028 hwparams.period_bytes_max = AZX_MAX_BUF_SIZE / 2;
1029 hwparams.periods_min = 2;
1030 hwparams.periods_max = AZX_MAX_FRAG;
1031 hwparams.buffer_bytes_max = AZX_MAX_BUF_SIZE;
1032 hwparams.fifo_size = 0;
1034 return snd_soc_set_runtime_hwparams(substream, &hwparams);
1037 static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream)
1039 return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
1040 (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index));
1043 static snd_pcm_uframes_t
1044 avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream)
1046 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1047 struct avs_dma_data *data;
1048 struct hdac_ext_stream *host_stream;
1051 data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream);
1052 if (!data->host_stream)
1055 host_stream = data->host_stream;
1056 pos = avs_hda_stream_dpib_read(host_stream);
1058 if (pos >= hdac_stream(host_stream)->bufsize)
1061 return bytes_to_frames(substream->runtime, pos);
1064 static int avs_component_mmap(struct snd_soc_component *component,
1065 struct snd_pcm_substream *substream,
1066 struct vm_area_struct *vma)
1068 return snd_pcm_lib_default_mmap(substream, vma);
1071 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
1073 static int avs_component_construct(struct snd_soc_component *component,
1074 struct snd_soc_pcm_runtime *rtd)
1076 struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
1077 struct snd_pcm *pcm = rtd->pcm;
1079 if (dai->driver->playback.channels_min)
1080 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1081 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1084 if (dai->driver->capture.channels_min)
1085 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1086 SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1092 static const struct snd_soc_component_driver avs_component_driver = {
1094 .probe = avs_component_probe,
1095 .remove = avs_component_remove,
1096 .suspend = avs_component_suspend,
1097 .resume = avs_component_resume,
1098 .open = avs_component_open,
1099 .pointer = avs_component_pointer,
1100 .mmap = avs_component_mmap,
1101 .pcm_construct = avs_component_construct,
1102 .module_get_upon_open = 1, /* increment refcount when a pcm is opened */
1103 .topology_name_prefix = "intel/avs",
1106 static int avs_soc_component_register(struct device *dev, const char *name,
1107 const struct snd_soc_component_driver *drv,
1108 struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais)
1110 struct avs_soc_component *acomp;
1113 acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL);
1117 ret = snd_soc_component_initialize(&acomp->base, drv, dev);
1121 /* force name change after ASoC is done with its init */
1122 acomp->base.name = name;
1123 INIT_LIST_HEAD(&acomp->node);
1125 return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais);
1128 static struct snd_soc_dai_driver dmic_cpu_dais[] = {
1131 .ops = &avs_dai_nonhda_be_ops,
1133 .stream_name = "DMIC Rx",
1136 .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000,
1137 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
1141 .name = "DMIC WoV Pin",
1142 .ops = &avs_dai_nonhda_be_ops,
1144 .stream_name = "DMIC WoV Rx",
1147 .rates = SNDRV_PCM_RATE_16000,
1148 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1153 int avs_dmic_platform_register(struct avs_dev *adev, const char *name)
1155 return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais,
1156 ARRAY_SIZE(dmic_cpu_dais));
1159 static const struct snd_soc_dai_driver i2s_dai_template = {
1160 .ops = &avs_dai_nonhda_be_ops,
1164 .rates = SNDRV_PCM_RATE_8000_192000 |
1165 SNDRV_PCM_RATE_KNOT,
1166 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1167 SNDRV_PCM_FMTBIT_S24_LE |
1168 SNDRV_PCM_FMTBIT_S32_LE,
1173 .rates = SNDRV_PCM_RATE_8000_192000 |
1174 SNDRV_PCM_RATE_KNOT,
1175 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1176 SNDRV_PCM_FMTBIT_S24_LE |
1177 SNDRV_PCM_FMTBIT_S32_LE,
1181 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
1182 unsigned long *tdms)
1184 struct snd_soc_dai_driver *cpus, *dai;
1185 size_t ssp_count, cpu_count;
1188 ssp_count = adev->hw_cfg.i2s_caps.ctrl_count;
1189 cpu_count = hweight_long(port_mask);
1191 for_each_set_bit(i, &port_mask, ssp_count)
1192 cpu_count += hweight_long(tdms[i]);
1194 cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL);
1199 for_each_set_bit(i, &port_mask, ssp_count) {
1200 memcpy(dai, &i2s_dai_template, sizeof(*dai));
1203 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i);
1204 dai->playback.stream_name =
1205 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i);
1206 dai->capture.stream_name =
1207 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i);
1209 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1217 for_each_set_bit(i, &port_mask, ssp_count) {
1218 for_each_set_bit(j, &tdms[i], ssp_count) {
1219 memcpy(dai, &i2s_dai_template, sizeof(*dai));
1222 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j);
1223 dai->playback.stream_name =
1224 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j);
1225 dai->capture.stream_name =
1226 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j);
1228 if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1235 return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count);
1238 /* HD-Audio CPU DAI template */
1239 static const struct snd_soc_dai_driver hda_cpu_dai = {
1240 .ops = &avs_dai_hda_be_ops,
1244 .rates = SNDRV_PCM_RATE_8000_192000,
1245 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1246 SNDRV_PCM_FMTBIT_S24_LE |
1247 SNDRV_PCM_FMTBIT_S32_LE,
1252 .rates = SNDRV_PCM_RATE_8000_192000,
1253 .formats = SNDRV_PCM_FMTBIT_S16_LE |
1254 SNDRV_PCM_FMTBIT_S24_LE |
1255 SNDRV_PCM_FMTBIT_S32_LE,
1259 static void avs_component_hda_unregister_dais(struct snd_soc_component *component)
1261 struct snd_soc_acpi_mach *mach;
1262 struct snd_soc_dai *dai, *save;
1263 struct hda_codec *codec;
1266 mach = dev_get_platdata(component->card->dev);
1267 codec = mach->pdata;
1268 sprintf(name, "%s-cpu", dev_name(&codec->core.dev));
1270 for_each_component_dais_safe(component, dai, save) {
1271 if (!strstr(dai->driver->name, name))
1274 snd_soc_dapm_free_widget(dai->playback_widget);
1275 snd_soc_dapm_free_widget(dai->capture_widget);
1276 snd_soc_unregister_dai(dai);
1280 static int avs_component_hda_probe(struct snd_soc_component *component)
1282 struct snd_soc_dapm_context *dapm;
1283 struct snd_soc_dai_driver *dais;
1284 struct snd_soc_acpi_mach *mach;
1285 struct hda_codec *codec;
1286 struct hda_pcm *pcm;
1288 int pcm_count = 0, ret, i;
1290 mach = dev_get_platdata(component->card->dev);
1294 codec = mach->pdata;
1295 if (list_empty(&codec->pcm_list_head))
1297 list_for_each_entry(pcm, &codec->pcm_list_head, list)
1300 dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais),
1305 cname = dev_name(&codec->core.dev);
1306 dapm = snd_soc_component_get_dapm(component);
1307 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
1309 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
1310 struct snd_soc_dai *dai;
1312 memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais));
1314 dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL,
1315 "%s-cpu%d", cname, i);
1316 if (!dais[i].name) {
1321 if (pcm->stream[0].substreams) {
1322 dais[i].playback.stream_name =
1323 devm_kasprintf(component->dev, GFP_KERNEL,
1324 "%s-cpu%d Tx", cname, i);
1325 if (!dais[i].playback.stream_name) {
1331 if (pcm->stream[1].substreams) {
1332 dais[i].capture.stream_name =
1333 devm_kasprintf(component->dev, GFP_KERNEL,
1334 "%s-cpu%d Rx", cname, i);
1335 if (!dais[i].capture.stream_name) {
1341 dai = snd_soc_register_dai(component, &dais[i], false);
1343 dev_err(component->dev, "register dai for %s failed\n",
1349 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1351 dev_err(component->dev, "create widgets failed: %d\n",
1357 ret = avs_component_probe(component);
1360 avs_component_hda_unregister_dais(component);
1365 static void avs_component_hda_remove(struct snd_soc_component *component)
1367 avs_component_hda_unregister_dais(component);
1368 avs_component_remove(component);
1371 static int avs_component_hda_open(struct snd_soc_component *component,
1372 struct snd_pcm_substream *substream)
1374 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1375 struct hdac_ext_stream *link_stream;
1376 struct hda_codec *codec;
1378 /* only BE DAI links are handled here */
1379 if (!rtd->dai_link->no_pcm)
1380 return avs_component_open(component, substream);
1382 codec = dev_to_hda_codec(asoc_rtd_to_codec(rtd, 0)->dev);
1383 link_stream = snd_hdac_ext_stream_assign(&codec->bus->core, substream,
1384 HDAC_EXT_STREAM_TYPE_LINK);
1388 substream->runtime->private_data = link_stream;
1392 static int avs_component_hda_close(struct snd_soc_component *component,
1393 struct snd_pcm_substream *substream)
1395 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1396 struct hdac_ext_stream *link_stream;
1398 /* only BE DAI links are handled here */
1399 if (!rtd->dai_link->no_pcm)
1402 link_stream = substream->runtime->private_data;
1403 snd_hdac_ext_stream_release(link_stream, HDAC_EXT_STREAM_TYPE_LINK);
1404 substream->runtime->private_data = NULL;
1409 static const struct snd_soc_component_driver avs_hda_component_driver = {
1410 .name = "avs-hda-pcm",
1411 .probe = avs_component_hda_probe,
1412 .remove = avs_component_hda_remove,
1413 .suspend = avs_component_suspend,
1414 .resume = avs_component_resume,
1415 .open = avs_component_hda_open,
1416 .close = avs_component_hda_close,
1417 .pointer = avs_component_pointer,
1418 .mmap = avs_component_mmap,
1419 .pcm_construct = avs_component_construct,
1421 * hda platform component's probe() is dependent on
1422 * codec->pcm_list_head, it needs to be initialized after codec
1423 * component. remove_order is here for completeness sake
1425 .probe_order = SND_SOC_COMP_ORDER_LATE,
1426 .remove_order = SND_SOC_COMP_ORDER_EARLY,
1427 .module_get_upon_open = 1,
1428 .topology_name_prefix = "intel/avs",
1431 int avs_hda_platform_register(struct avs_dev *adev, const char *name)
1433 return avs_soc_component_register(adev->dev, name,
1434 &avs_hda_component_driver, NULL, 0);