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