• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Authors: Keyon Jie <yang.jie@linux.intel.com>
9 //
10 
11 #include <sound/pcm_params.h>
12 #include <sound/hdaudio_ext.h>
13 #include "../sof-priv.h"
14 #include "../sof-audio.h"
15 #include "hda.h"
16 
17 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
18 
19 struct hda_pipe_params {
20 	u8 host_dma_id;
21 	u8 link_dma_id;
22 	u32 ch;
23 	u32 s_freq;
24 	u32 s_fmt;
25 	u8 linktype;
26 	snd_pcm_format_t format;
27 	int link_index;
28 	int stream;
29 	unsigned int host_bps;
30 	unsigned int link_bps;
31 };
32 
33 /*
34  * This function checks if the host dma channel corresponding
35  * to the link DMA stream_tag argument is assigned to one
36  * of the FEs connected to the BE DAI.
37  */
hda_check_fes(struct snd_soc_pcm_runtime * rtd,int dir,int stream_tag)38 static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd,
39 			  int dir, int stream_tag)
40 {
41 	struct snd_pcm_substream *fe_substream;
42 	struct hdac_stream *fe_hstream;
43 	struct snd_soc_dpcm *dpcm;
44 
45 	for_each_dpcm_fe(rtd, dir, dpcm) {
46 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, dir);
47 		fe_hstream = fe_substream->runtime->private_data;
48 		if (fe_hstream->stream_tag == stream_tag)
49 			return true;
50 	}
51 
52 	return false;
53 }
54 
55 static struct hdac_ext_stream *
hda_link_stream_assign(struct hdac_bus * bus,struct snd_pcm_substream * substream)56 	hda_link_stream_assign(struct hdac_bus *bus,
57 			       struct snd_pcm_substream *substream)
58 {
59 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
60 	struct sof_intel_hda_stream *hda_stream;
61 	struct hdac_ext_stream *res = NULL;
62 	struct hdac_stream *stream = NULL;
63 
64 	int stream_dir = substream->stream;
65 
66 	if (!bus->ppcap) {
67 		dev_err(bus->dev, "stream type not supported\n");
68 		return NULL;
69 	}
70 
71 	spin_lock_irq(&bus->reg_lock);
72 	list_for_each_entry(stream, &bus->stream_list, list) {
73 		struct hdac_ext_stream *hstream =
74 			stream_to_hdac_ext_stream(stream);
75 		if (stream->direction != substream->stream)
76 			continue;
77 
78 		hda_stream = hstream_to_sof_hda_stream(hstream);
79 
80 		/* check if link is available */
81 		if (!hstream->link_locked) {
82 			if (stream->opened) {
83 				/*
84 				 * check if the stream tag matches the stream
85 				 * tag of one of the connected FEs
86 				 */
87 				if (hda_check_fes(rtd, stream_dir,
88 						  stream->stream_tag)) {
89 					res = hstream;
90 					break;
91 				}
92 			} else {
93 				res = hstream;
94 
95 				/*
96 				 * This must be a hostless stream.
97 				 * So reserve the host DMA channel.
98 				 */
99 				hda_stream->host_reserved = 1;
100 				break;
101 			}
102 		}
103 	}
104 
105 	if (res) {
106 		/*
107 		 * Decouple host and link DMA. The decoupled flag
108 		 * is updated in snd_hdac_ext_stream_decouple().
109 		 */
110 		if (!res->decoupled)
111 			snd_hdac_ext_stream_decouple_locked(bus, res, true);
112 
113 		res->link_locked = 1;
114 		res->link_substream = substream;
115 	}
116 	spin_unlock_irq(&bus->reg_lock);
117 
118 	return res;
119 }
120 
hda_link_dma_params(struct hdac_ext_stream * stream,struct hda_pipe_params * params)121 static int hda_link_dma_params(struct hdac_ext_stream *stream,
122 			       struct hda_pipe_params *params)
123 {
124 	struct hdac_stream *hstream = &stream->hstream;
125 	unsigned char stream_tag = hstream->stream_tag;
126 	struct hdac_bus *bus = hstream->bus;
127 	struct hdac_ext_link *link;
128 	unsigned int format_val;
129 
130 	snd_hdac_ext_stream_decouple(bus, stream, true);
131 	snd_hdac_ext_link_stream_reset(stream);
132 
133 	format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
134 						 params->format,
135 						 params->link_bps, 0);
136 
137 	dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
138 		format_val, params->s_freq, params->ch, params->format);
139 
140 	snd_hdac_ext_link_stream_setup(stream, format_val);
141 
142 	if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) {
143 		list_for_each_entry(link, &bus->hlink_list, list) {
144 			if (link->index == params->link_index)
145 				snd_hdac_ext_link_set_stream_id(link,
146 								stream_tag);
147 		}
148 	}
149 
150 	stream->link_prepared = 1;
151 
152 	return 0;
153 }
154 
155 /* Send DAI_CONFIG IPC to the DAI that matches the dai_name and direction */
hda_link_config_ipc(struct sof_intel_hda_stream * hda_stream,const char * dai_name,int channel,int dir)156 static int hda_link_config_ipc(struct sof_intel_hda_stream *hda_stream,
157 			       const char *dai_name, int channel, int dir)
158 {
159 	struct sof_ipc_dai_config *config;
160 	struct snd_sof_dai *sof_dai;
161 	struct sof_ipc_reply reply;
162 	int ret = 0;
163 
164 	list_for_each_entry(sof_dai, &hda_stream->sdev->dai_list, list) {
165 		if (!sof_dai->cpu_dai_name)
166 			continue;
167 
168 		if (!strcmp(dai_name, sof_dai->cpu_dai_name) &&
169 		    dir == sof_dai->comp_dai.direction) {
170 			config = sof_dai->dai_config;
171 
172 			if (!config) {
173 				dev_err(hda_stream->sdev->dev,
174 					"error: no config for DAI %s\n",
175 					sof_dai->name);
176 				return -EINVAL;
177 			}
178 
179 			/* update config with stream tag */
180 			config->hda.link_dma_ch = channel;
181 
182 			/* send IPC */
183 			ret = sof_ipc_tx_message(hda_stream->sdev->ipc,
184 						 config->hdr.cmd,
185 						 config,
186 						 config->hdr.size,
187 						 &reply, sizeof(reply));
188 
189 			if (ret < 0)
190 				dev_err(hda_stream->sdev->dev,
191 					"error: failed to set dai config for %s\n",
192 					sof_dai->name);
193 			return ret;
194 		}
195 	}
196 
197 	return -EINVAL;
198 }
199 
hda_link_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)200 static int hda_link_hw_params(struct snd_pcm_substream *substream,
201 			      struct snd_pcm_hw_params *params,
202 			      struct snd_soc_dai *dai)
203 {
204 	struct hdac_stream *hstream = substream->runtime->private_data;
205 	struct hdac_bus *bus = hstream->bus;
206 	struct hdac_ext_stream *link_dev;
207 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
208 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
209 	struct sof_intel_hda_stream *hda_stream;
210 	struct hda_pipe_params p_params = {0};
211 	struct hdac_ext_link *link;
212 	int stream_tag;
213 	int ret;
214 
215 	link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name);
216 	if (!link)
217 		return -EINVAL;
218 
219 	/* get stored dma data if resuming from system suspend */
220 	link_dev = snd_soc_dai_get_dma_data(dai, substream);
221 	if (!link_dev) {
222 		link_dev = hda_link_stream_assign(bus, substream);
223 		if (!link_dev)
224 			return -EBUSY;
225 
226 		snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
227 	}
228 
229 	stream_tag = hdac_stream(link_dev)->stream_tag;
230 
231 	hda_stream = hstream_to_sof_hda_stream(link_dev);
232 
233 	/* update the DSP with the new tag */
234 	ret = hda_link_config_ipc(hda_stream, dai->name, stream_tag - 1,
235 				  substream->stream);
236 	if (ret < 0)
237 		return ret;
238 
239 	/* set the stream tag in the codec dai dma params */
240 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
241 		snd_soc_dai_set_tdm_slot(codec_dai, stream_tag, 0, 0, 0);
242 	else
243 		snd_soc_dai_set_tdm_slot(codec_dai, 0, stream_tag, 0, 0);
244 
245 	p_params.s_fmt = snd_pcm_format_width(params_format(params));
246 	p_params.ch = params_channels(params);
247 	p_params.s_freq = params_rate(params);
248 	p_params.stream = substream->stream;
249 	p_params.link_dma_id = stream_tag - 1;
250 	p_params.link_index = link->index;
251 	p_params.format = params_format(params);
252 
253 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
254 		p_params.link_bps = codec_dai->driver->playback.sig_bits;
255 	else
256 		p_params.link_bps = codec_dai->driver->capture.sig_bits;
257 
258 	return hda_link_dma_params(link_dev, &p_params);
259 }
260 
hda_link_pcm_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)261 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream,
262 				struct snd_soc_dai *dai)
263 {
264 	struct hdac_ext_stream *link_dev =
265 				snd_soc_dai_get_dma_data(dai, substream);
266 	struct snd_sof_dev *sdev =
267 				snd_soc_component_get_drvdata(dai->component);
268 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
269 	int stream = substream->stream;
270 
271 	if (link_dev->link_prepared)
272 		return 0;
273 
274 	dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream);
275 
276 	return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params,
277 				  dai);
278 }
279 
hda_link_pcm_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)280 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream,
281 				int cmd, struct snd_soc_dai *dai)
282 {
283 	struct hdac_ext_stream *link_dev =
284 				snd_soc_dai_get_dma_data(dai, substream);
285 	struct sof_intel_hda_stream *hda_stream;
286 	struct snd_soc_pcm_runtime *rtd;
287 	struct hdac_ext_link *link;
288 	struct hdac_stream *hstream;
289 	struct hdac_bus *bus;
290 	int stream_tag;
291 	int ret;
292 
293 	hstream = substream->runtime->private_data;
294 	bus = hstream->bus;
295 	rtd = asoc_substream_to_rtd(substream);
296 
297 	link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name);
298 	if (!link)
299 		return -EINVAL;
300 
301 	hda_stream = hstream_to_sof_hda_stream(link_dev);
302 
303 	dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
304 	switch (cmd) {
305 	case SNDRV_PCM_TRIGGER_RESUME:
306 		/* set up hw_params */
307 		ret = hda_link_pcm_prepare(substream, dai);
308 		if (ret < 0) {
309 			dev_err(dai->dev,
310 				"error: setting up hw_params during resume\n");
311 			return ret;
312 		}
313 
314 		fallthrough;
315 	case SNDRV_PCM_TRIGGER_START:
316 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
317 		snd_hdac_ext_link_stream_start(link_dev);
318 		break;
319 	case SNDRV_PCM_TRIGGER_SUSPEND:
320 	case SNDRV_PCM_TRIGGER_STOP:
321 		/*
322 		 * clear link DMA channel. It will be assigned when
323 		 * hw_params is set up again after resume.
324 		 */
325 		ret = hda_link_config_ipc(hda_stream, dai->name,
326 					  DMA_CHAN_INVALID, substream->stream);
327 		if (ret < 0)
328 			return ret;
329 
330 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
331 			stream_tag = hdac_stream(link_dev)->stream_tag;
332 			snd_hdac_ext_link_clear_stream_id(link, stream_tag);
333 		}
334 
335 		link_dev->link_prepared = 0;
336 
337 		fallthrough;
338 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
339 		snd_hdac_ext_link_stream_clear(link_dev);
340 		break;
341 	default:
342 		return -EINVAL;
343 	}
344 	return 0;
345 }
346 
hda_link_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)347 static int hda_link_hw_free(struct snd_pcm_substream *substream,
348 			    struct snd_soc_dai *dai)
349 {
350 	unsigned int stream_tag;
351 	struct sof_intel_hda_stream *hda_stream;
352 	struct hdac_bus *bus;
353 	struct hdac_ext_link *link;
354 	struct hdac_stream *hstream;
355 	struct snd_soc_pcm_runtime *rtd;
356 	struct hdac_ext_stream *link_dev;
357 	int ret;
358 
359 	hstream = substream->runtime->private_data;
360 	bus = hstream->bus;
361 	rtd = asoc_substream_to_rtd(substream);
362 	link_dev = snd_soc_dai_get_dma_data(dai, substream);
363 
364 	if (!link_dev) {
365 		dev_dbg(dai->dev,
366 			"%s: link_dev is not assigned\n", __func__);
367 		return -EINVAL;
368 	}
369 
370 	hda_stream = hstream_to_sof_hda_stream(link_dev);
371 
372 	/* free the link DMA channel in the FW */
373 	ret = hda_link_config_ipc(hda_stream, dai->name, DMA_CHAN_INVALID,
374 				  substream->stream);
375 	if (ret < 0)
376 		return ret;
377 
378 	link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name);
379 	if (!link)
380 		return -EINVAL;
381 
382 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
383 		stream_tag = hdac_stream(link_dev)->stream_tag;
384 		snd_hdac_ext_link_clear_stream_id(link, stream_tag);
385 	}
386 
387 	snd_soc_dai_set_dma_data(dai, substream, NULL);
388 	snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
389 	link_dev->link_prepared = 0;
390 
391 	/* free the host DMA channel reserved by hostless streams */
392 	hda_stream->host_reserved = 0;
393 
394 	return 0;
395 }
396 
397 static const struct snd_soc_dai_ops hda_link_dai_ops = {
398 	.hw_params = hda_link_hw_params,
399 	.hw_free = hda_link_hw_free,
400 	.trigger = hda_link_pcm_trigger,
401 	.prepare = hda_link_pcm_prepare,
402 };
403 
404 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_PROBES)
405 #include "../compress.h"
406 
407 static struct snd_soc_cdai_ops sof_probe_compr_ops = {
408 	.startup	= sof_probe_compr_open,
409 	.shutdown	= sof_probe_compr_free,
410 	.set_params	= sof_probe_compr_set_params,
411 	.trigger	= sof_probe_compr_trigger,
412 	.pointer	= sof_probe_compr_pointer,
413 };
414 
415 #endif
416 #endif
417 
418 /*
419  * common dai driver for skl+ platforms.
420  * some products who use this DAI array only physically have a subset of
421  * the DAIs, but no harm is done here by adding the whole set.
422  */
423 struct snd_soc_dai_driver skl_dai[] = {
424 {
425 	.name = "SSP0 Pin",
426 	.playback = {
427 		.channels_min = 1,
428 		.channels_max = 8,
429 	},
430 	.capture = {
431 		.channels_min = 1,
432 		.channels_max = 8,
433 	},
434 },
435 {
436 	.name = "SSP1 Pin",
437 	.playback = {
438 		.channels_min = 1,
439 		.channels_max = 8,
440 	},
441 	.capture = {
442 		.channels_min = 1,
443 		.channels_max = 8,
444 	},
445 },
446 {
447 	.name = "SSP2 Pin",
448 	.playback = {
449 		.channels_min = 1,
450 		.channels_max = 8,
451 	},
452 	.capture = {
453 		.channels_min = 1,
454 		.channels_max = 8,
455 	},
456 },
457 {
458 	.name = "SSP3 Pin",
459 	.playback = {
460 		.channels_min = 1,
461 		.channels_max = 8,
462 	},
463 	.capture = {
464 		.channels_min = 1,
465 		.channels_max = 8,
466 	},
467 },
468 {
469 	.name = "SSP4 Pin",
470 	.playback = {
471 		.channels_min = 1,
472 		.channels_max = 8,
473 	},
474 	.capture = {
475 		.channels_min = 1,
476 		.channels_max = 8,
477 	},
478 },
479 {
480 	.name = "SSP5 Pin",
481 	.playback = {
482 		.channels_min = 1,
483 		.channels_max = 8,
484 	},
485 	.capture = {
486 		.channels_min = 1,
487 		.channels_max = 8,
488 	},
489 },
490 {
491 	.name = "DMIC01 Pin",
492 	.capture = {
493 		.channels_min = 1,
494 		.channels_max = 4,
495 	},
496 },
497 {
498 	.name = "DMIC16k Pin",
499 	.capture = {
500 		.channels_min = 1,
501 		.channels_max = 4,
502 	},
503 },
504 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
505 {
506 	.name = "iDisp1 Pin",
507 	.ops = &hda_link_dai_ops,
508 	.playback = {
509 		.channels_min = 1,
510 		.channels_max = 8,
511 	},
512 },
513 {
514 	.name = "iDisp2 Pin",
515 	.ops = &hda_link_dai_ops,
516 	.playback = {
517 		.channels_min = 1,
518 		.channels_max = 8,
519 	},
520 },
521 {
522 	.name = "iDisp3 Pin",
523 	.ops = &hda_link_dai_ops,
524 	.playback = {
525 		.channels_min = 1,
526 		.channels_max = 8,
527 	},
528 },
529 {
530 	.name = "iDisp4 Pin",
531 	.ops = &hda_link_dai_ops,
532 	.playback = {
533 		.channels_min = 1,
534 		.channels_max = 8,
535 	},
536 },
537 {
538 	.name = "Analog CPU DAI",
539 	.ops = &hda_link_dai_ops,
540 	.playback = {
541 		.channels_min = 1,
542 		.channels_max = 16,
543 	},
544 	.capture = {
545 		.channels_min = 1,
546 		.channels_max = 16,
547 	},
548 },
549 {
550 	.name = "Digital CPU DAI",
551 	.ops = &hda_link_dai_ops,
552 	.playback = {
553 		.channels_min = 1,
554 		.channels_max = 16,
555 	},
556 	.capture = {
557 		.channels_min = 1,
558 		.channels_max = 16,
559 	},
560 },
561 {
562 	.name = "Alt Analog CPU DAI",
563 	.ops = &hda_link_dai_ops,
564 	.playback = {
565 		.channels_min = 1,
566 		.channels_max = 16,
567 	},
568 	.capture = {
569 		.channels_min = 1,
570 		.channels_max = 16,
571 	},
572 },
573 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_PROBES)
574 {
575 	.name = "Probe Extraction CPU DAI",
576 	.compress_new = snd_soc_new_compress,
577 	.cops = &sof_probe_compr_ops,
578 	.capture = {
579 		.stream_name = "Probe Extraction",
580 		.channels_min = 1,
581 		.channels_max = 8,
582 		.rates = SNDRV_PCM_RATE_48000,
583 		.rate_min = 48000,
584 		.rate_max = 48000,
585 	},
586 },
587 #endif
588 #endif
589 };
590