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