arm64: dts: qcom: sm8550: add TRNG node
[linux-modified.git] / sound / soc / intel / avs / pcm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8
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>
17 #include "avs.h"
18 #include "path.h"
19 #include "topology.h"
20
21 struct avs_dma_data {
22         struct avs_tplg_path_template *template;
23         struct avs_path *path;
24         /*
25          * link stream is stored within substream's runtime
26          * private_data to fulfill the needs of codec BE path
27          *
28          * host stream assigned
29          */
30         struct hdac_ext_stream *host_stream;
31
32         struct snd_pcm_substream *substream;
33 };
34
35 static struct avs_tplg_path_template *
36 avs_dai_find_path_template(struct snd_soc_dai *dai, bool is_fe, int direction)
37 {
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;
41
42         if (direction == SNDRV_PCM_STREAM_CAPTURE) {
43                 dir = is_fe ? SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN;
44         } else {
45                 dir = is_fe ? SND_SOC_DAPM_DIR_IN : SND_SOC_DAPM_DIR_OUT;
46         }
47
48         dp = list_first_entry_or_null(&dw->edges[dir], typeof(*dp), list_node[dir]);
49         if (!dp)
50                 return NULL;
51
52         /* Get the other widget, with actual path template data */
53         dw = (dp->source == dw) ? dp->sink : dp->source;
54
55         return dw->priv;
56 }
57
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)
60 {
61         struct snd_soc_pcm_runtime *rtd = snd_soc_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;
65
66         template = avs_dai_find_path_template(dai, is_fe, substream->stream);
67         if (!template) {
68                 dev_err(dai->dev, "no %s path for dai %s, invalid tplg?\n",
69                         snd_pcm_stream_str(substream), dai->name);
70                 return -EINVAL;
71         }
72
73         data = kzalloc(sizeof(*data), GFP_KERNEL);
74         if (!data)
75                 return -ENOMEM;
76
77         data->substream = substream;
78         data->template = template;
79         snd_soc_dai_set_dma_data(dai, substream, data);
80
81         if (rtd->dai_link->ignore_suspend)
82                 adev->num_lp_paths++;
83
84         return 0;
85 }
86
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,
90                              int dma_id)
91 {
92         struct avs_dma_data *data;
93         struct avs_path *path;
94         struct avs_dev *adev = to_avs_dev(dai->dev);
95         int ret;
96
97         data = snd_soc_dai_get_dma_data(dai, substream);
98
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));
104
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));
110
111         path = avs_path_create(adev, dma_id, data->template, fe_hw_params, be_hw_params);
112         if (IS_ERR(path)) {
113                 ret = PTR_ERR(path);
114                 dev_err(dai->dev, "create path failed: %d\n", ret);
115                 return ret;
116         }
117
118         data->path = path;
119         return 0;
120 }
121
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,
124                                 int dma_id)
125 {
126         struct snd_pcm_hw_params *fe_hw_params = NULL;
127         struct snd_soc_pcm_runtime *fe, *be;
128         struct snd_soc_dpcm *dpcm;
129
130         be = snd_soc_substream_to_rtd(substream);
131         for_each_dpcm_fe(be, substream->stream, dpcm) {
132                 fe = dpcm->fe;
133                 fe_hw_params = &fe->dpcm[substream->stream].hw_params;
134         }
135
136         return avs_dai_hw_params(substream, fe_hw_params, be_hw_params, dai, dma_id);
137 }
138
139 static int avs_dai_prepare(struct avs_dev *adev, struct snd_pcm_substream *substream,
140                            struct snd_soc_dai *dai)
141 {
142         struct avs_dma_data *data;
143         int ret;
144
145         data = snd_soc_dai_get_dma_data(dai, substream);
146         if (!data->path)
147                 return 0;
148
149         ret = avs_path_reset(data->path);
150         if (ret < 0) {
151                 dev_err(dai->dev, "reset path failed: %d\n", ret);
152                 return ret;
153         }
154
155         ret = avs_path_pause(data->path);
156         if (ret < 0)
157                 dev_err(dai->dev, "pause path failed: %d\n", ret);
158         return ret;
159 }
160
161 static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops;
162
163 static int avs_dai_nonhda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
164 {
165         return avs_dai_startup(substream, dai, false, &avs_dai_nonhda_be_ops);
166 }
167
168 static void avs_dai_nonhda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
169 {
170         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
171         struct avs_dev *adev = to_avs_dev(dai->dev);
172         struct avs_dma_data *data;
173
174         if (rtd->dai_link->ignore_suspend)
175                 adev->num_lp_paths--;
176
177         data = snd_soc_dai_get_dma_data(dai, substream);
178
179         snd_soc_dai_set_dma_data(dai, substream, NULL);
180         kfree(data);
181 }
182
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)
185 {
186         struct avs_dma_data *data;
187
188         data = snd_soc_dai_get_dma_data(dai, substream);
189         if (data->path)
190                 return 0;
191
192         /* Actual port-id comes from topology. */
193         return avs_dai_be_hw_params(substream, hw_params, dai, 0);
194 }
195
196 static int avs_dai_nonhda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
197 {
198         struct avs_dma_data *data;
199
200         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
201
202         data = snd_soc_dai_get_dma_data(dai, substream);
203         if (data->path) {
204                 avs_path_free(data->path);
205                 data->path = NULL;
206         }
207
208         return 0;
209 }
210
211 static int avs_dai_nonhda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
212 {
213         return avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
214 }
215
216 static int avs_dai_nonhda_be_trigger(struct snd_pcm_substream *substream, int cmd,
217                                      struct snd_soc_dai *dai)
218 {
219         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
220         struct avs_dma_data *data;
221         int ret = 0;
222
223         data = snd_soc_dai_get_dma_data(dai, substream);
224
225         switch (cmd) {
226         case SNDRV_PCM_TRIGGER_RESUME:
227                 if (rtd->dai_link->ignore_suspend)
228                         break;
229                 fallthrough;
230         case SNDRV_PCM_TRIGGER_START:
231         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
232                 ret = avs_path_pause(data->path);
233                 if (ret < 0) {
234                         dev_err(dai->dev, "pause BE path failed: %d\n", ret);
235                         break;
236                 }
237
238                 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
239                 if (ret < 0)
240                         dev_err(dai->dev, "run BE path failed: %d\n", ret);
241                 break;
242
243         case SNDRV_PCM_TRIGGER_SUSPEND:
244                 if (rtd->dai_link->ignore_suspend)
245                         break;
246                 fallthrough;
247         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
248         case SNDRV_PCM_TRIGGER_STOP:
249                 ret = avs_path_pause(data->path);
250                 if (ret < 0)
251                         dev_err(dai->dev, "pause BE path failed: %d\n", ret);
252
253                 ret = avs_path_reset(data->path);
254                 if (ret < 0)
255                         dev_err(dai->dev, "reset BE path failed: %d\n", ret);
256                 break;
257
258         default:
259                 ret = -EINVAL;
260                 break;
261         }
262
263         return ret;
264 }
265
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,
273 };
274
275 static const struct snd_soc_dai_ops avs_dai_hda_be_ops;
276
277 static int avs_dai_hda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
278 {
279         return avs_dai_startup(substream, dai, false, &avs_dai_hda_be_ops);
280 }
281
282 static void avs_dai_hda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
283 {
284         return avs_dai_nonhda_be_shutdown(substream, dai);
285 }
286
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)
289 {
290         struct avs_dma_data *data;
291         struct hdac_ext_stream *link_stream;
292
293         data = snd_soc_dai_get_dma_data(dai, substream);
294         if (data->path)
295                 return 0;
296
297         link_stream = substream->runtime->private_data;
298
299         return avs_dai_be_hw_params(substream, hw_params, dai,
300                                     hdac_stream(link_stream)->stream_tag - 1);
301 }
302
303 static int avs_dai_hda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
304 {
305         struct avs_dma_data *data;
306         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
307         struct hdac_ext_stream *link_stream;
308         struct hdac_ext_link *link;
309         struct hda_codec *codec;
310
311         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
312
313         data = snd_soc_dai_get_dma_data(dai, substream);
314         if (!data->path)
315                 return 0;
316
317         link_stream = substream->runtime->private_data;
318         link_stream->link_prepared = false;
319         avs_path_free(data->path);
320         data->path = NULL;
321
322         /* clear link <-> stream mapping */
323         codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev);
324         link = snd_hdac_ext_bus_get_hlink_by_addr(&codec->bus->core, codec->core.addr);
325         if (!link)
326                 return -EINVAL;
327
328         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
329                 snd_hdac_ext_bus_link_clear_stream_id(link, hdac_stream(link_stream)->stream_tag);
330
331         return 0;
332 }
333
334 static int avs_dai_hda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
335 {
336         struct snd_soc_pcm_runtime *rtd = snd_soc_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;
343         int ret;
344
345         if (link_stream->link_prepared)
346                 return 0;
347
348         codec = dev_to_hda_codec(snd_soc_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);
352
353         snd_hdac_ext_stream_reset(link_stream);
354         snd_hdac_ext_stream_setup(link_stream, format_val);
355
356         link = snd_hdac_ext_bus_get_hlink_by_addr(bus, codec->core.addr);
357         if (!link)
358                 return -EINVAL;
359
360         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
361                 snd_hdac_ext_bus_link_set_stream_id(link, hdac_stream(link_stream)->stream_tag);
362
363         ret = avs_dai_prepare(to_avs_dev(dai->dev), substream, dai);
364         if (ret)
365                 return ret;
366
367         link_stream->link_prepared = true;
368         return 0;
369 }
370
371 static int avs_dai_hda_be_trigger(struct snd_pcm_substream *substream, int cmd,
372                                   struct snd_soc_dai *dai)
373 {
374         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
375         struct hdac_ext_stream *link_stream;
376         struct avs_dma_data *data;
377         int ret = 0;
378
379         dev_dbg(dai->dev, "entry %s cmd=%d\n", __func__, cmd);
380
381         data = snd_soc_dai_get_dma_data(dai, substream);
382         link_stream = substream->runtime->private_data;
383
384         switch (cmd) {
385         case SNDRV_PCM_TRIGGER_RESUME:
386                 if (rtd->dai_link->ignore_suspend)
387                         break;
388                 fallthrough;
389         case SNDRV_PCM_TRIGGER_START:
390         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
391                 snd_hdac_ext_stream_start(link_stream);
392
393                 ret = avs_path_pause(data->path);
394                 if (ret < 0) {
395                         dev_err(dai->dev, "pause BE path failed: %d\n", ret);
396                         break;
397                 }
398
399                 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
400                 if (ret < 0)
401                         dev_err(dai->dev, "run BE path failed: %d\n", ret);
402                 break;
403
404         case SNDRV_PCM_TRIGGER_SUSPEND:
405                 if (rtd->dai_link->ignore_suspend)
406                         break;
407                 fallthrough;
408         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
409         case SNDRV_PCM_TRIGGER_STOP:
410                 ret = avs_path_pause(data->path);
411                 if (ret < 0)
412                         dev_err(dai->dev, "pause BE path failed: %d\n", ret);
413
414                 snd_hdac_ext_stream_clear(link_stream);
415
416                 ret = avs_path_reset(data->path);
417                 if (ret < 0)
418                         dev_err(dai->dev, "reset BE path failed: %d\n", ret);
419                 break;
420
421         default:
422                 ret = -EINVAL;
423                 break;
424         }
425
426         return ret;
427 }
428
429 static const struct snd_soc_dai_ops avs_dai_hda_be_ops = {
430         .startup = avs_dai_hda_be_startup,
431         .shutdown = avs_dai_hda_be_shutdown,
432         .hw_params = avs_dai_hda_be_hw_params,
433         .hw_free = avs_dai_hda_be_hw_free,
434         .prepare = avs_dai_hda_be_prepare,
435         .trigger = avs_dai_hda_be_trigger,
436 };
437
438 static const unsigned int rates[] = {
439         8000, 11025, 12000, 16000,
440         22050, 24000, 32000, 44100,
441         48000, 64000, 88200, 96000,
442         128000, 176400, 192000,
443 };
444
445 static const struct snd_pcm_hw_constraint_list hw_rates = {
446         .count = ARRAY_SIZE(rates),
447         .list = rates,
448         .mask = 0,
449 };
450
451 const struct snd_soc_dai_ops avs_dai_fe_ops;
452
453 static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
454 {
455         struct snd_pcm_runtime *runtime = substream->runtime;
456         struct avs_dma_data *data;
457         struct avs_dev *adev = to_avs_dev(dai->dev);
458         struct hdac_bus *bus = &adev->base.core;
459         struct hdac_ext_stream *host_stream;
460         int ret;
461
462         ret = avs_dai_startup(substream, dai, true, &avs_dai_fe_ops);
463         if (ret)
464                 return ret;
465
466         data = snd_soc_dai_get_dma_data(dai, substream);
467
468         host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST);
469         if (!host_stream) {
470                 ret = -EBUSY;
471                 goto err;
472         }
473
474         data->host_stream = host_stream;
475         ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
476         if (ret < 0)
477                 goto err;
478
479         /* avoid wrap-around with wall-clock */
480         ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000);
481         if (ret < 0)
482                 goto err;
483
484         ret = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_rates);
485         if (ret < 0)
486                 goto err;
487
488         snd_pcm_set_sync(substream);
489
490         dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p",
491                 __func__, hdac_stream(host_stream)->stream_tag, substream);
492
493         return 0;
494
495 err:
496         kfree(data);
497         return ret;
498 }
499
500 static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
501 {
502         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
503         struct avs_dev *adev = to_avs_dev(dai->dev);
504         struct avs_dma_data *data;
505
506         if (rtd->dai_link->ignore_suspend)
507                 adev->num_lp_paths--;
508
509         data = snd_soc_dai_get_dma_data(dai, substream);
510
511         snd_soc_dai_set_dma_data(dai, substream, NULL);
512         snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST);
513         kfree(data);
514 }
515
516 static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream,
517                                 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
518 {
519         struct snd_pcm_hw_params *be_hw_params = NULL;
520         struct snd_soc_pcm_runtime *fe, *be;
521         struct snd_soc_dpcm *dpcm;
522         struct avs_dma_data *data;
523         struct hdac_ext_stream *host_stream;
524         int ret;
525
526         data = snd_soc_dai_get_dma_data(dai, substream);
527         if (data->path)
528                 return 0;
529
530         host_stream = data->host_stream;
531
532         hdac_stream(host_stream)->bufsize = 0;
533         hdac_stream(host_stream)->period_bytes = 0;
534         hdac_stream(host_stream)->format_val = 0;
535
536         fe = snd_soc_substream_to_rtd(substream);
537         for_each_dpcm_be(fe, substream->stream, dpcm) {
538                 be = dpcm->be;
539                 be_hw_params = &be->dpcm[substream->stream].hw_params;
540         }
541
542         ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai,
543                                 hdac_stream(host_stream)->stream_tag - 1);
544         if (ret)
545                 goto create_err;
546
547         ret = avs_path_bind(data->path);
548         if (ret < 0) {
549                 dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret);
550                 goto bind_err;
551         }
552
553         return 0;
554
555 bind_err:
556         avs_path_free(data->path);
557         data->path = NULL;
558 create_err:
559         snd_pcm_lib_free_pages(substream);
560         return ret;
561 }
562
563 static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
564 {
565         struct avs_dma_data *data;
566         struct hdac_ext_stream *host_stream;
567         int ret;
568
569         dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p",
570                 __func__, substream, substream->runtime);
571
572         data = snd_soc_dai_get_dma_data(dai, substream);
573         if (!data->path)
574                 return 0;
575
576         host_stream = data->host_stream;
577
578         ret = avs_path_unbind(data->path);
579         if (ret < 0)
580                 dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret);
581
582         avs_path_free(data->path);
583         data->path = NULL;
584         snd_hdac_stream_cleanup(hdac_stream(host_stream));
585         hdac_stream(host_stream)->prepared = false;
586
587         return ret;
588 }
589
590 static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
591 {
592         int ret;
593
594         ret = __avs_dai_fe_hw_free(substream, dai);
595         snd_pcm_lib_free_pages(substream);
596
597         return ret;
598 }
599
600 static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
601 {
602         struct snd_pcm_runtime *runtime = substream->runtime;
603         struct avs_dma_data *data;
604         struct avs_dev *adev = to_avs_dev(dai->dev);
605         struct hdac_ext_stream *host_stream;
606         unsigned int format_val;
607         int ret;
608
609         data = snd_soc_dai_get_dma_data(dai, substream);
610         host_stream = data->host_stream;
611
612         if (hdac_stream(host_stream)->prepared)
613                 return 0;
614
615         snd_hdac_stream_reset(hdac_stream(host_stream));
616
617         format_val = snd_hdac_calc_stream_format(runtime->rate, runtime->channels, runtime->format,
618                                                  runtime->sample_bits, 0);
619
620         ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val);
621         if (ret < 0)
622                 return ret;
623
624         ret = snd_hdac_ext_host_stream_setup(host_stream, false);
625         if (ret < 0)
626                 return ret;
627
628         ret = avs_dai_prepare(adev, substream, dai);
629         if (ret)
630                 return ret;
631
632         hdac_stream(host_stream)->prepared = true;
633         return 0;
634 }
635
636 static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
637 {
638         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
639         struct avs_dma_data *data;
640         struct hdac_ext_stream *host_stream;
641         struct hdac_bus *bus;
642         unsigned long flags;
643         int ret = 0;
644
645         data = snd_soc_dai_get_dma_data(dai, substream);
646         host_stream = data->host_stream;
647         bus = hdac_stream(host_stream)->bus;
648
649         switch (cmd) {
650         case SNDRV_PCM_TRIGGER_RESUME:
651                 if (rtd->dai_link->ignore_suspend)
652                         break;
653                 fallthrough;
654         case SNDRV_PCM_TRIGGER_START:
655         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
656                 spin_lock_irqsave(&bus->reg_lock, flags);
657                 snd_hdac_stream_start(hdac_stream(host_stream));
658                 spin_unlock_irqrestore(&bus->reg_lock, flags);
659
660                 /* Timeout on DRSM poll shall not stop the resume so ignore the result. */
661                 if (cmd == SNDRV_PCM_TRIGGER_RESUME)
662                         snd_hdac_stream_wait_drsm(hdac_stream(host_stream));
663
664                 ret = avs_path_pause(data->path);
665                 if (ret < 0) {
666                         dev_err(dai->dev, "pause FE path failed: %d\n", ret);
667                         break;
668                 }
669
670                 ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
671                 if (ret < 0)
672                         dev_err(dai->dev, "run FE path failed: %d\n", ret);
673
674                 break;
675
676         case SNDRV_PCM_TRIGGER_SUSPEND:
677                 if (rtd->dai_link->ignore_suspend)
678                         break;
679                 fallthrough;
680         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
681         case SNDRV_PCM_TRIGGER_STOP:
682                 ret = avs_path_pause(data->path);
683                 if (ret < 0)
684                         dev_err(dai->dev, "pause FE path failed: %d\n", ret);
685
686                 spin_lock_irqsave(&bus->reg_lock, flags);
687                 snd_hdac_stream_stop(hdac_stream(host_stream));
688                 spin_unlock_irqrestore(&bus->reg_lock, flags);
689
690                 ret = avs_path_reset(data->path);
691                 if (ret < 0)
692                         dev_err(dai->dev, "reset FE path failed: %d\n", ret);
693                 break;
694
695         default:
696                 ret = -EINVAL;
697                 break;
698         }
699
700         return ret;
701 }
702
703 const struct snd_soc_dai_ops avs_dai_fe_ops = {
704         .startup = avs_dai_fe_startup,
705         .shutdown = avs_dai_fe_shutdown,
706         .hw_params = avs_dai_fe_hw_params,
707         .hw_free = avs_dai_fe_hw_free,
708         .prepare = avs_dai_fe_prepare,
709         .trigger = avs_dai_fe_trigger,
710 };
711
712 static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count,
713                                   loff_t *ppos)
714 {
715         struct snd_soc_component *component = file->private_data;
716         struct snd_soc_card *card = component->card;
717         struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
718         char buf[64];
719         size_t len;
720
721         len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix,
722                         mach->tplg_filename);
723
724         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
725 }
726
727 static const struct file_operations topology_name_fops = {
728         .open = simple_open,
729         .read = topology_name_read,
730         .llseek = default_llseek,
731 };
732
733 static int avs_component_load_libraries(struct avs_soc_component *acomp)
734 {
735         struct avs_tplg *tplg = acomp->tplg;
736         struct avs_dev *adev = to_avs_dev(acomp->base.dev);
737         int ret;
738
739         if (!tplg->num_libs)
740                 return 0;
741
742         /* Parent device may be asleep and library loading involves IPCs. */
743         ret = pm_runtime_resume_and_get(adev->dev);
744         if (ret < 0)
745                 return ret;
746
747         avs_hda_power_gating_enable(adev, false);
748         avs_hda_clock_gating_enable(adev, false);
749         avs_hda_l1sen_enable(adev, false);
750
751         ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
752
753         avs_hda_l1sen_enable(adev, true);
754         avs_hda_clock_gating_enable(adev, true);
755         avs_hda_power_gating_enable(adev, true);
756
757         if (!ret)
758                 ret = avs_module_info_init(adev, false);
759
760         pm_runtime_mark_last_busy(adev->dev);
761         pm_runtime_put_autosuspend(adev->dev);
762
763         return ret;
764 }
765
766 static int avs_component_probe(struct snd_soc_component *component)
767 {
768         struct snd_soc_card *card = component->card;
769         struct snd_soc_acpi_mach *mach;
770         struct avs_soc_component *acomp;
771         struct avs_dev *adev;
772         char *filename;
773         int ret;
774
775         dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name);
776         mach = dev_get_platdata(card->dev);
777         acomp = to_avs_soc_component(component);
778         adev = to_avs_dev(component->dev);
779
780         acomp->tplg = avs_tplg_new(component);
781         if (!acomp->tplg)
782                 return -ENOMEM;
783
784         if (!mach->tplg_filename)
785                 goto finalize;
786
787         /* Load specified topology and create debugfs for it. */
788         filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
789                              mach->tplg_filename);
790         if (!filename)
791                 return -ENOMEM;
792
793         ret = avs_load_topology(component, filename);
794         kfree(filename);
795         if (ret == -ENOENT && !strncmp(mach->tplg_filename, "hda-", 4)) {
796                 unsigned int vendor_id;
797
798                 if (sscanf(mach->tplg_filename, "/*(DEBLOBBED)*/", &vendor_id) != 1)
799                         return ret;
800
801                 if (((vendor_id >> 16) & 0xFFFF) == 0x8086)
802                         mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL,
803                                                              "/*(DEBLOBBED)*/");
804                 else
805                         mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL,
806                                                              "/*(DEBLOBBED)*/");
807
808                 filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
809                                      mach->tplg_filename);
810                 if (!filename)
811                         return -ENOMEM;
812
813                 dev_info(card->dev, "trying to load fallback topology %s\n", mach->tplg_filename);
814                 ret = avs_load_topology(component, filename);
815                 kfree(filename);
816         }
817         if (ret < 0)
818                 return ret;
819
820         ret = avs_component_load_libraries(acomp);
821         if (ret < 0) {
822                 dev_err(card->dev, "libraries loading failed: %d\n", ret);
823                 goto err_load_libs;
824         }
825
826 finalize:
827         debugfs_create_file("topology_name", 0444, component->debugfs_root, component,
828                             &topology_name_fops);
829
830         mutex_lock(&adev->comp_list_mutex);
831         list_add_tail(&acomp->node, &adev->comp_list);
832         mutex_unlock(&adev->comp_list_mutex);
833
834         return 0;
835
836 err_load_libs:
837         avs_remove_topology(component);
838         return ret;
839 }
840
841 static void avs_component_remove(struct snd_soc_component *component)
842 {
843         struct avs_soc_component *acomp = to_avs_soc_component(component);
844         struct snd_soc_acpi_mach *mach;
845         struct avs_dev *adev = to_avs_dev(component->dev);
846         int ret;
847
848         mach = dev_get_platdata(component->card->dev);
849
850         mutex_lock(&adev->comp_list_mutex);
851         list_del(&acomp->node);
852         mutex_unlock(&adev->comp_list_mutex);
853
854         if (mach->tplg_filename) {
855                 ret = avs_remove_topology(component);
856                 if (ret < 0)
857                         dev_err(component->dev, "unload topology failed: %d\n", ret);
858         }
859 }
860
861 static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data)
862 {
863         struct snd_pcm_substream *substream;
864         struct snd_soc_pcm_runtime *rtd;
865         int ret;
866
867         substream = data->substream;
868         rtd = snd_soc_substream_to_rtd(substream);
869
870         ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai);
871         if (ret)
872                 dev_err(dai->dev, "hw_params on resume failed: %d\n", ret);
873
874         return ret;
875 }
876
877 static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
878 {
879         struct hdac_ext_stream *host_stream;
880         struct hdac_stream *hstream;
881         struct hdac_bus *bus;
882         int ret;
883
884         host_stream = data->host_stream;
885         hstream = hdac_stream(host_stream);
886         bus = hdac_stream(host_stream)->bus;
887
888         /* Set DRSM before programming stream and position registers. */
889         snd_hdac_stream_drsm_enable(bus, true, hstream->index);
890
891         ret = dai->driver->ops->prepare(data->substream, dai);
892         if (ret) {
893                 dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret);
894                 return ret;
895         }
896
897         writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL);
898         writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU);
899         writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL);
900         writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU);
901
902         /* As per HW spec recommendation, program LPIB and DPIB to the same value. */
903         snd_hdac_stream_set_lpib(hstream, hstream->lpib);
904         snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib);
905
906         return 0;
907 }
908
909 static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
910 {
911         int ret;
912
913         ret = dai->driver->ops->prepare(data->substream, dai);
914         if (ret)
915                 dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret);
916
917         return ret;
918 }
919
920 static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
921 {
922         struct hdac_ext_stream *host_stream;
923         int ret;
924
925         host_stream = data->host_stream;
926
927         /* Store position addresses so we can resume from them later on. */
928         hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream));
929         host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL);
930         host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU);
931         host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL);
932         host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU);
933
934         ret = __avs_dai_fe_hw_free(data->substream, dai);
935         if (ret < 0)
936                 dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret);
937
938         return ret;
939 }
940
941 static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
942 {
943         int ret;
944
945         ret = dai->driver->ops->hw_free(data->substream, dai);
946         if (ret < 0)
947                 dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret);
948
949         return ret;
950 }
951
952 static int avs_component_pm_op(struct snd_soc_component *component, bool be,
953                                int (*op)(struct snd_soc_dai *, struct avs_dma_data *))
954 {
955         struct snd_soc_pcm_runtime *rtd;
956         struct avs_dma_data *data;
957         struct snd_soc_dai *dai;
958         int ret;
959
960         for_each_component_dais(component, dai) {
961                 data = snd_soc_dai_dma_data_get_playback(dai);
962                 if (data) {
963                         rtd = snd_soc_substream_to_rtd(data->substream);
964                         if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
965                                 ret = op(dai, data);
966                                 if (ret < 0) {
967                                         __snd_pcm_set_state(data->substream->runtime,
968                                                             SNDRV_PCM_STATE_DISCONNECTED);
969                                         return ret;
970                                 }
971                         }
972                 }
973
974                 data = snd_soc_dai_dma_data_get_capture(dai);
975                 if (data) {
976                         rtd = snd_soc_substream_to_rtd(data->substream);
977                         if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
978                                 ret = op(dai, data);
979                                 if (ret < 0) {
980                                         __snd_pcm_set_state(data->substream->runtime,
981                                                             SNDRV_PCM_STATE_DISCONNECTED);
982                                         return ret;
983                                 }
984                         }
985                 }
986         }
987
988         return 0;
989 }
990
991 static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be)
992 {
993         return avs_component_pm_op(component, be, &avs_dai_resume_hw_params);
994 }
995
996 static int avs_component_resume_prepare(struct snd_soc_component *component, bool be)
997 {
998         int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
999
1000         if (be)
1001                 prepare_cb = &avs_dai_resume_be_prepare;
1002         else
1003                 prepare_cb = &avs_dai_resume_fe_prepare;
1004
1005         return avs_component_pm_op(component, be, prepare_cb);
1006 }
1007
1008 static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be)
1009 {
1010         int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
1011
1012         if (be)
1013                 hw_free_cb = &avs_dai_suspend_be_hw_free;
1014         else
1015                 hw_free_cb = &avs_dai_suspend_fe_hw_free;
1016
1017         return avs_component_pm_op(component, be, hw_free_cb);
1018 }
1019
1020 static int avs_component_suspend(struct snd_soc_component *component)
1021 {
1022         int ret;
1023
1024         /*
1025          * When freeing paths, FEs need to be first as they perform
1026          * path unbinding.
1027          */
1028         ret = avs_component_suspend_hw_free(component, false);
1029         if (ret)
1030                 return ret;
1031
1032         return avs_component_suspend_hw_free(component, true);
1033 }
1034
1035 static int avs_component_resume(struct snd_soc_component *component)
1036 {
1037         int ret;
1038
1039         /*
1040          * When creating paths, FEs need to be last as they perform
1041          * path binding.
1042          */
1043         ret = avs_component_resume_hw_params(component, true);
1044         if (ret)
1045                 return ret;
1046
1047         ret = avs_component_resume_hw_params(component, false);
1048         if (ret)
1049                 return ret;
1050
1051         /* It is expected that the LINK stream is prepared first. */
1052         ret = avs_component_resume_prepare(component, true);
1053         if (ret)
1054                 return ret;
1055
1056         return avs_component_resume_prepare(component, false);
1057 }
1058
1059 static const struct snd_pcm_hardware avs_pcm_hardware = {
1060         .info                   = SNDRV_PCM_INFO_MMAP |
1061                                   SNDRV_PCM_INFO_MMAP_VALID |
1062                                   SNDRV_PCM_INFO_INTERLEAVED |
1063                                   SNDRV_PCM_INFO_PAUSE |
1064                                   SNDRV_PCM_INFO_RESUME |
1065                                   SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
1066         .formats                = SNDRV_PCM_FMTBIT_S16_LE |
1067                                   SNDRV_PCM_FMTBIT_S24_LE |
1068                                   SNDRV_PCM_FMTBIT_S32_LE,
1069         .buffer_bytes_max       = AZX_MAX_BUF_SIZE,
1070         .period_bytes_min       = 128,
1071         .period_bytes_max       = AZX_MAX_BUF_SIZE / 2,
1072         .periods_min            = 2,
1073         .periods_max            = AZX_MAX_FRAG,
1074         .fifo_size              = 0,
1075 };
1076
1077 static int avs_component_open(struct snd_soc_component *component,
1078                               struct snd_pcm_substream *substream)
1079 {
1080         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1081
1082         /* only FE DAI links are handled here */
1083         if (rtd->dai_link->no_pcm)
1084                 return 0;
1085
1086         return snd_soc_set_runtime_hwparams(substream, &avs_pcm_hardware);
1087 }
1088
1089 static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream)
1090 {
1091         return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
1092                      (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index));
1093 }
1094
1095 static snd_pcm_uframes_t
1096 avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream)
1097 {
1098         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1099         struct avs_dma_data *data;
1100         struct hdac_ext_stream *host_stream;
1101         unsigned int pos;
1102
1103         data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
1104         if (!data->host_stream)
1105                 return 0;
1106
1107         host_stream = data->host_stream;
1108         pos = avs_hda_stream_dpib_read(host_stream);
1109
1110         if (pos >= hdac_stream(host_stream)->bufsize)
1111                 pos = 0;
1112
1113         return bytes_to_frames(substream->runtime, pos);
1114 }
1115
1116 static int avs_component_mmap(struct snd_soc_component *component,
1117                               struct snd_pcm_substream *substream,
1118                               struct vm_area_struct *vma)
1119 {
1120         return snd_pcm_lib_default_mmap(substream, vma);
1121 }
1122
1123 #define MAX_PREALLOC_SIZE       (32 * 1024 * 1024)
1124
1125 static int avs_component_construct(struct snd_soc_component *component,
1126                                    struct snd_soc_pcm_runtime *rtd)
1127 {
1128         struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0);
1129         struct snd_pcm *pcm = rtd->pcm;
1130
1131         if (dai->driver->playback.channels_min)
1132                 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
1133                                            SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1134                                            MAX_PREALLOC_SIZE);
1135
1136         if (dai->driver->capture.channels_min)
1137                 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
1138                                            SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
1139                                            MAX_PREALLOC_SIZE);
1140
1141         return 0;
1142 }
1143
1144 static const struct snd_soc_component_driver avs_component_driver = {
1145         .name                   = "avs-pcm",
1146         .probe                  = avs_component_probe,
1147         .remove                 = avs_component_remove,
1148         .suspend                = avs_component_suspend,
1149         .resume                 = avs_component_resume,
1150         .open                   = avs_component_open,
1151         .pointer                = avs_component_pointer,
1152         .mmap                   = avs_component_mmap,
1153         .pcm_construct          = avs_component_construct,
1154         .module_get_upon_open   = 1, /* increment refcount when a pcm is opened */
1155         .topology_name_prefix   = "intel/avs",
1156 };
1157
1158 int avs_soc_component_register(struct device *dev, const char *name,
1159                                const struct snd_soc_component_driver *drv,
1160                                struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais)
1161 {
1162         struct avs_soc_component *acomp;
1163         int ret;
1164
1165         acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL);
1166         if (!acomp)
1167                 return -ENOMEM;
1168
1169         ret = snd_soc_component_initialize(&acomp->base, drv, dev);
1170         if (ret < 0)
1171                 return ret;
1172
1173         /* force name change after ASoC is done with its init */
1174         acomp->base.name = name;
1175         INIT_LIST_HEAD(&acomp->node);
1176
1177         return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais);
1178 }
1179
1180 static struct snd_soc_dai_driver dmic_cpu_dais[] = {
1181 {
1182         .name = "DMIC Pin",
1183         .ops = &avs_dai_nonhda_be_ops,
1184         .capture = {
1185                 .stream_name    = "DMIC Rx",
1186                 .channels_min   = 1,
1187                 .channels_max   = 4,
1188                 .rates          = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000,
1189                 .formats        = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
1190         },
1191 },
1192 {
1193         .name = "DMIC WoV Pin",
1194         .ops = &avs_dai_nonhda_be_ops,
1195         .capture = {
1196                 .stream_name    = "DMIC WoV Rx",
1197                 .channels_min   = 1,
1198                 .channels_max   = 4,
1199                 .rates          = SNDRV_PCM_RATE_16000,
1200                 .formats        = SNDRV_PCM_FMTBIT_S16_LE,
1201         },
1202 },
1203 };
1204
1205 int avs_dmic_platform_register(struct avs_dev *adev, const char *name)
1206 {
1207         return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais,
1208                                           ARRAY_SIZE(dmic_cpu_dais));
1209 }
1210
1211 static const struct snd_soc_dai_driver i2s_dai_template = {
1212         .ops = &avs_dai_nonhda_be_ops,
1213         .playback = {
1214                 .channels_min   = 1,
1215                 .channels_max   = 8,
1216                 .rates          = SNDRV_PCM_RATE_8000_192000 |
1217                                   SNDRV_PCM_RATE_KNOT,
1218                 .formats        = SNDRV_PCM_FMTBIT_S16_LE |
1219                                   SNDRV_PCM_FMTBIT_S24_LE |
1220                                   SNDRV_PCM_FMTBIT_S32_LE,
1221         },
1222         .capture = {
1223                 .channels_min   = 1,
1224                 .channels_max   = 8,
1225                 .rates          = SNDRV_PCM_RATE_8000_192000 |
1226                                   SNDRV_PCM_RATE_KNOT,
1227                 .formats        = SNDRV_PCM_FMTBIT_S16_LE |
1228                                   SNDRV_PCM_FMTBIT_S24_LE |
1229                                   SNDRV_PCM_FMTBIT_S32_LE,
1230         },
1231 };
1232
1233 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
1234                               unsigned long *tdms)
1235 {
1236         struct snd_soc_dai_driver *cpus, *dai;
1237         size_t ssp_count, cpu_count;
1238         int i, j;
1239
1240         ssp_count = adev->hw_cfg.i2s_caps.ctrl_count;
1241
1242         cpu_count = 0;
1243         for_each_set_bit(i, &port_mask, ssp_count)
1244                 if (!tdms || test_bit(0, &tdms[i]))
1245                         cpu_count++;
1246         if (tdms)
1247                 for_each_set_bit(i, &port_mask, ssp_count)
1248                         cpu_count += hweight_long(tdms[i]);
1249
1250         cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL);
1251         if (!cpus)
1252                 return -ENOMEM;
1253
1254         dai = cpus;
1255         for_each_set_bit(i, &port_mask, ssp_count) {
1256                 if (!tdms || test_bit(0, &tdms[i])) {
1257                         memcpy(dai, &i2s_dai_template, sizeof(*dai));
1258
1259                         dai->name =
1260                                 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i);
1261                         dai->playback.stream_name =
1262                                 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i);
1263                         dai->capture.stream_name =
1264                                 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i);
1265
1266                         if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1267                                 return -ENOMEM;
1268                         dai++;
1269                 }
1270         }
1271
1272         if (!tdms)
1273                 goto plat_register;
1274
1275         for_each_set_bit(i, &port_mask, ssp_count) {
1276                 for_each_set_bit(j, &tdms[i], ssp_count) {
1277                         memcpy(dai, &i2s_dai_template, sizeof(*dai));
1278
1279                         dai->name =
1280                                 devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j);
1281                         dai->playback.stream_name =
1282                                 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j);
1283                         dai->capture.stream_name =
1284                                 devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j);
1285
1286                         if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
1287                                 return -ENOMEM;
1288                         dai++;
1289                 }
1290         }
1291
1292 plat_register:
1293         return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count);
1294 }
1295
1296 /* HD-Audio CPU DAI template */
1297 static const struct snd_soc_dai_driver hda_cpu_dai = {
1298         .ops = &avs_dai_hda_be_ops,
1299         .playback = {
1300                 .channels_min   = 1,
1301                 .channels_max   = 8,
1302                 .rates          = SNDRV_PCM_RATE_8000_192000,
1303                 .formats        = SNDRV_PCM_FMTBIT_S16_LE |
1304                                   SNDRV_PCM_FMTBIT_S24_LE |
1305                                   SNDRV_PCM_FMTBIT_S32_LE,
1306         },
1307         .capture = {
1308                 .channels_min   = 1,
1309                 .channels_max   = 8,
1310                 .rates          = SNDRV_PCM_RATE_8000_192000,
1311                 .formats        = SNDRV_PCM_FMTBIT_S16_LE |
1312                                   SNDRV_PCM_FMTBIT_S24_LE |
1313                                   SNDRV_PCM_FMTBIT_S32_LE,
1314         },
1315 };
1316
1317 static void avs_component_hda_unregister_dais(struct snd_soc_component *component)
1318 {
1319         struct snd_soc_acpi_mach *mach;
1320         struct snd_soc_dai *dai, *save;
1321         struct hda_codec *codec;
1322         char name[32];
1323
1324         mach = dev_get_platdata(component->card->dev);
1325         codec = mach->pdata;
1326         sprintf(name, "%s-cpu", dev_name(&codec->core.dev));
1327
1328         for_each_component_dais_safe(component, dai, save) {
1329                 int stream;
1330
1331                 if (!strstr(dai->driver->name, name))
1332                         continue;
1333
1334                 for_each_pcm_streams(stream)
1335                         snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
1336
1337                 snd_soc_unregister_dai(dai);
1338         }
1339 }
1340
1341 static int avs_component_hda_probe(struct snd_soc_component *component)
1342 {
1343         struct snd_soc_dapm_context *dapm;
1344         struct snd_soc_dai_driver *dais;
1345         struct snd_soc_acpi_mach *mach;
1346         struct hda_codec *codec;
1347         struct hda_pcm *pcm;
1348         const char *cname;
1349         int pcm_count = 0, ret, i;
1350
1351         mach = dev_get_platdata(component->card->dev);
1352         if (!mach)
1353                 return -EINVAL;
1354
1355         codec = mach->pdata;
1356         if (list_empty(&codec->pcm_list_head))
1357                 return -EINVAL;
1358         list_for_each_entry(pcm, &codec->pcm_list_head, list)
1359                 pcm_count++;
1360
1361         dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais),
1362                             GFP_KERNEL);
1363         if (!dais)
1364                 return -ENOMEM;
1365
1366         cname = dev_name(&codec->core.dev);
1367         dapm = snd_soc_component_get_dapm(component);
1368         pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
1369
1370         for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
1371                 struct snd_soc_dai *dai;
1372
1373                 memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais));
1374                 dais[i].id = i;
1375                 dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL,
1376                                               "%s-cpu%d", cname, i);
1377                 if (!dais[i].name) {
1378                         ret = -ENOMEM;
1379                         goto exit;
1380                 }
1381
1382                 if (pcm->stream[0].substreams) {
1383                         dais[i].playback.stream_name =
1384                                 devm_kasprintf(component->dev, GFP_KERNEL,
1385                                                "%s-cpu%d Tx", cname, i);
1386                         if (!dais[i].playback.stream_name) {
1387                                 ret = -ENOMEM;
1388                                 goto exit;
1389                         }
1390                 }
1391
1392                 if (pcm->stream[1].substreams) {
1393                         dais[i].capture.stream_name =
1394                                 devm_kasprintf(component->dev, GFP_KERNEL,
1395                                                "%s-cpu%d Rx", cname, i);
1396                         if (!dais[i].capture.stream_name) {
1397                                 ret = -ENOMEM;
1398                                 goto exit;
1399                         }
1400                 }
1401
1402                 dai = snd_soc_register_dai(component, &dais[i], false);
1403                 if (!dai) {
1404                         dev_err(component->dev, "register dai for %s failed\n",
1405                                 pcm->name);
1406                         ret = -EINVAL;
1407                         goto exit;
1408                 }
1409
1410                 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1411                 if (ret < 0) {
1412                         dev_err(component->dev, "create widgets failed: %d\n",
1413                                 ret);
1414                         goto exit;
1415                 }
1416         }
1417
1418         ret = avs_component_probe(component);
1419 exit:
1420         if (ret)
1421                 avs_component_hda_unregister_dais(component);
1422
1423         return ret;
1424 }
1425
1426 static void avs_component_hda_remove(struct snd_soc_component *component)
1427 {
1428         avs_component_hda_unregister_dais(component);
1429         avs_component_remove(component);
1430 }
1431
1432 static int avs_component_hda_open(struct snd_soc_component *component,
1433                                   struct snd_pcm_substream *substream)
1434 {
1435         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1436         struct hdac_ext_stream *link_stream;
1437         struct hda_codec *codec;
1438
1439         if (!rtd->dai_link->no_pcm) {
1440                 struct snd_pcm_hardware hwparams = avs_pcm_hardware;
1441                 struct snd_soc_pcm_runtime *be;
1442                 struct snd_soc_dpcm *dpcm;
1443                 int dir = substream->stream;
1444
1445                 /*
1446                  * Support the DPCM reparenting while still fulfilling expectations of HDAudio
1447                  * common code - a valid stream pointer at substream->runtime->private_data -
1448                  * by having all FEs point to the same private data.
1449                  */
1450                 for_each_dpcm_be(rtd, dir, dpcm) {
1451                         struct snd_pcm_substream *be_substream;
1452
1453                         be = dpcm->be;
1454                         if (be->dpcm[dir].users == 1)
1455                                 break;
1456
1457                         be_substream = snd_soc_dpcm_get_substream(be, dir);
1458                         substream->runtime->private_data = be_substream->runtime->private_data;
1459                         break;
1460                 }
1461
1462                 /* RESUME unsupported for de-coupled HD-Audio capture. */
1463                 if (dir == SNDRV_PCM_STREAM_CAPTURE)
1464                         hwparams.info &= ~SNDRV_PCM_INFO_RESUME;
1465
1466                 return snd_soc_set_runtime_hwparams(substream, &hwparams);
1467         }
1468
1469         codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev);
1470         link_stream = snd_hdac_ext_stream_assign(&codec->bus->core, substream,
1471                                              HDAC_EXT_STREAM_TYPE_LINK);
1472         if (!link_stream)
1473                 return -EBUSY;
1474
1475         substream->runtime->private_data = link_stream;
1476         return 0;
1477 }
1478
1479 static int avs_component_hda_close(struct snd_soc_component *component,
1480                                    struct snd_pcm_substream *substream)
1481 {
1482         struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1483         struct hdac_ext_stream *link_stream;
1484
1485         /* only BE DAI links are handled here */
1486         if (!rtd->dai_link->no_pcm)
1487                 return 0;
1488
1489         link_stream = substream->runtime->private_data;
1490         snd_hdac_ext_stream_release(link_stream, HDAC_EXT_STREAM_TYPE_LINK);
1491         substream->runtime->private_data = NULL;
1492
1493         return 0;
1494 }
1495
1496 static const struct snd_soc_component_driver avs_hda_component_driver = {
1497         .name                   = "avs-hda-pcm",
1498         .probe                  = avs_component_hda_probe,
1499         .remove                 = avs_component_hda_remove,
1500         .suspend                = avs_component_suspend,
1501         .resume                 = avs_component_resume,
1502         .open                   = avs_component_hda_open,
1503         .close                  = avs_component_hda_close,
1504         .pointer                = avs_component_pointer,
1505         .mmap                   = avs_component_mmap,
1506         .pcm_construct          = avs_component_construct,
1507         /*
1508          * hda platform component's probe() is dependent on
1509          * codec->pcm_list_head, it needs to be initialized after codec
1510          * component. remove_order is here for completeness sake
1511          */
1512         .probe_order            = SND_SOC_COMP_ORDER_LATE,
1513         .remove_order           = SND_SOC_COMP_ORDER_EARLY,
1514         .module_get_upon_open   = 1,
1515         .topology_name_prefix   = "intel/avs",
1516 };
1517
1518 int avs_hda_platform_register(struct avs_dev *adev, const char *name)
1519 {
1520         return avs_soc_component_register(adev->dev, name,
1521                                           &avs_hda_component_driver, NULL, 0);
1522 }