xref: /linux/sound/soc/sof/intel/hda-dai.c (revision ee1e79b72e3cf5eac42ba9de827536f91d4c04e2)
1 // SPDX-License-Identifier: (GPL-2.0 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  */
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 *
56 	hda_link_stream_assign(struct hdac_bus *bus,
57 			       struct snd_pcm_substream *substream)
58 {
59 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
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 	list_for_each_entry(stream, &bus->stream_list, list) {
72 		struct hdac_ext_stream *hstream =
73 			stream_to_hdac_ext_stream(stream);
74 		if (stream->direction != substream->stream)
75 			continue;
76 
77 		hda_stream = hstream_to_sof_hda_stream(hstream);
78 
79 		/* check if link is available */
80 		if (!hstream->link_locked) {
81 			if (stream->opened) {
82 				/*
83 				 * check if the stream tag matches the stream
84 				 * tag of one of the connected FEs
85 				 */
86 				if (hda_check_fes(rtd, stream_dir,
87 						  stream->stream_tag)) {
88 					res = hstream;
89 					break;
90 				}
91 			} else {
92 				res = hstream;
93 
94 				/*
95 				 * This must be a hostless stream.
96 				 * So reserve the host DMA channel.
97 				 */
98 				hda_stream->host_reserved = 1;
99 				break;
100 			}
101 		}
102 	}
103 
104 	if (res) {
105 		/*
106 		 * Decouple host and link DMA. The decoupled flag
107 		 * is updated in snd_hdac_ext_stream_decouple().
108 		 */
109 		if (!res->decoupled)
110 			snd_hdac_ext_stream_decouple(bus, res, true);
111 		spin_lock_irq(&bus->reg_lock);
112 		res->link_locked = 1;
113 		res->link_substream = substream;
114 		spin_unlock_irq(&bus->reg_lock);
115 	}
116 
117 	return res;
118 }
119 
120 static int hda_link_dma_params(struct hdac_ext_stream *stream,
121 			       struct hda_pipe_params *params)
122 {
123 	struct hdac_stream *hstream = &stream->hstream;
124 	unsigned char stream_tag = hstream->stream_tag;
125 	struct hdac_bus *bus = hstream->bus;
126 	struct hdac_ext_link *link;
127 	unsigned int format_val;
128 
129 	snd_hdac_ext_stream_decouple(bus, stream, true);
130 	snd_hdac_ext_link_stream_reset(stream);
131 
132 	format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
133 						 params->format,
134 						 params->link_bps, 0);
135 
136 	dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
137 		format_val, params->s_freq, params->ch, params->format);
138 
139 	snd_hdac_ext_link_stream_setup(stream, format_val);
140 
141 	if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) {
142 		list_for_each_entry(link, &bus->hlink_list, list) {
143 			if (link->index == params->link_index)
144 				snd_hdac_ext_link_set_stream_id(link,
145 								stream_tag);
146 		}
147 	}
148 
149 	stream->link_prepared = 1;
150 
151 	return 0;
152 }
153 
154 /* Send DAI_CONFIG IPC to the DAI that matches the dai_name and direction */
155 static int hda_link_config_ipc(struct sof_intel_hda_stream *hda_stream,
156 			       const char *dai_name, int channel, int dir)
157 {
158 	struct sof_ipc_dai_config *config;
159 	struct snd_sof_dai *sof_dai;
160 	struct sof_ipc_reply reply;
161 	int ret = 0;
162 
163 	list_for_each_entry(sof_dai, &hda_stream->sdev->dai_list, list) {
164 		if (!sof_dai->cpu_dai_name)
165 			continue;
166 
167 		if (!strcmp(dai_name, sof_dai->cpu_dai_name) &&
168 		    dir == sof_dai->comp_dai.direction) {
169 			config = sof_dai->dai_config;
170 
171 			if (!config) {
172 				dev_err(hda_stream->sdev->dev,
173 					"error: no config for DAI %s\n",
174 					sof_dai->name);
175 				return -EINVAL;
176 			}
177 
178 			/* update config with stream tag */
179 			config->hda.link_dma_ch = channel;
180 
181 			/* send IPC */
182 			ret = sof_ipc_tx_message(hda_stream->sdev->ipc,
183 						 config->hdr.cmd,
184 						 config,
185 						 config->hdr.size,
186 						 &reply, sizeof(reply));
187 
188 			if (ret < 0)
189 				dev_err(hda_stream->sdev->dev,
190 					"error: failed to set dai config for %s\n",
191 					sof_dai->name);
192 			return ret;
193 		}
194 	}
195 
196 	return -EINVAL;
197 }
198 
199 static int hda_link_hw_params(struct snd_pcm_substream *substream,
200 			      struct snd_pcm_hw_params *params,
201 			      struct snd_soc_dai *dai)
202 {
203 	struct hdac_stream *hstream = substream->runtime->private_data;
204 	struct hdac_bus *bus = hstream->bus;
205 	struct hdac_ext_stream *link_dev;
206 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
207 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
208 	struct sof_intel_hda_stream *hda_stream;
209 	struct hda_pipe_params p_params = {0};
210 	struct hdac_ext_link *link;
211 	int stream_tag;
212 	int ret;
213 
214 	/* get stored dma data if resuming from system suspend */
215 	link_dev = snd_soc_dai_get_dma_data(dai, substream);
216 	if (!link_dev) {
217 		link_dev = hda_link_stream_assign(bus, substream);
218 		if (!link_dev)
219 			return -EBUSY;
220 	}
221 
222 	stream_tag = hdac_stream(link_dev)->stream_tag;
223 
224 	hda_stream = hstream_to_sof_hda_stream(link_dev);
225 
226 	/* update the DSP with the new tag */
227 	ret = hda_link_config_ipc(hda_stream, dai->name, stream_tag - 1,
228 				  substream->stream);
229 	if (ret < 0)
230 		return ret;
231 
232 	snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
233 
234 	link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name);
235 	if (!link)
236 		return -EINVAL;
237 
238 	/* set the stream tag in the codec dai dma params */
239 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
240 		snd_soc_dai_set_tdm_slot(codec_dai, stream_tag, 0, 0, 0);
241 	else
242 		snd_soc_dai_set_tdm_slot(codec_dai, 0, stream_tag, 0, 0);
243 
244 	p_params.s_fmt = snd_pcm_format_width(params_format(params));
245 	p_params.ch = params_channels(params);
246 	p_params.s_freq = params_rate(params);
247 	p_params.stream = substream->stream;
248 	p_params.link_dma_id = stream_tag - 1;
249 	p_params.link_index = link->index;
250 	p_params.format = params_format(params);
251 
252 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
253 		p_params.link_bps = codec_dai->driver->playback.sig_bits;
254 	else
255 		p_params.link_bps = codec_dai->driver->capture.sig_bits;
256 
257 	return hda_link_dma_params(link_dev, &p_params);
258 }
259 
260 static int hda_link_pcm_prepare(struct snd_pcm_substream *substream,
261 				struct snd_soc_dai *dai)
262 {
263 	struct hdac_ext_stream *link_dev =
264 				snd_soc_dai_get_dma_data(dai, substream);
265 	struct sof_intel_hda_stream *hda_stream;
266 	struct snd_sof_dev *sdev =
267 				snd_soc_component_get_drvdata(dai->component);
268 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
269 	int stream = substream->stream;
270 
271 	hda_stream = hstream_to_sof_hda_stream(link_dev);
272 
273 	if (link_dev->link_prepared)
274 		return 0;
275 
276 	dev_dbg(sdev->dev, "hda: prepare stream dir %d\n", substream->stream);
277 
278 	return hda_link_hw_params(substream, &rtd->dpcm[stream].hw_params,
279 				  dai);
280 }
281 
282 static int hda_link_pcm_trigger(struct snd_pcm_substream *substream,
283 				int cmd, struct snd_soc_dai *dai)
284 {
285 	struct hdac_ext_stream *link_dev =
286 				snd_soc_dai_get_dma_data(dai, substream);
287 	struct sof_intel_hda_stream *hda_stream;
288 	struct snd_soc_pcm_runtime *rtd;
289 	struct hdac_ext_link *link;
290 	struct hdac_stream *hstream;
291 	struct hdac_bus *bus;
292 	int stream_tag;
293 	int ret;
294 
295 	hstream = substream->runtime->private_data;
296 	bus = hstream->bus;
297 	rtd = snd_pcm_substream_chip(substream);
298 
299 	link = snd_hdac_ext_bus_get_link(bus, rtd->codec_dai->component->name);
300 	if (!link)
301 		return -EINVAL;
302 
303 	hda_stream = hstream_to_sof_hda_stream(link_dev);
304 
305 	dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
306 	switch (cmd) {
307 	case SNDRV_PCM_TRIGGER_RESUME:
308 		/* set up hw_params */
309 		ret = hda_link_pcm_prepare(substream, dai);
310 		if (ret < 0) {
311 			dev_err(dai->dev,
312 				"error: setting up hw_params during resume\n");
313 			return ret;
314 		}
315 
316 		/* fallthrough */
317 	case SNDRV_PCM_TRIGGER_START:
318 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
319 		snd_hdac_ext_link_stream_start(link_dev);
320 		break;
321 	case SNDRV_PCM_TRIGGER_SUSPEND:
322 	case SNDRV_PCM_TRIGGER_STOP:
323 		/*
324 		 * clear link DMA channel. It will be assigned when
325 		 * hw_params is set up again after resume.
326 		 */
327 		ret = hda_link_config_ipc(hda_stream, dai->name,
328 					  DMA_CHAN_INVALID, substream->stream);
329 		if (ret < 0)
330 			return ret;
331 
332 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
333 			stream_tag = hdac_stream(link_dev)->stream_tag;
334 			snd_hdac_ext_link_clear_stream_id(link, stream_tag);
335 		}
336 
337 		link_dev->link_prepared = 0;
338 
339 		/* fallthrough */
340 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
341 		snd_hdac_ext_link_stream_clear(link_dev);
342 		break;
343 	default:
344 		return -EINVAL;
345 	}
346 	return 0;
347 }
348 
349 static int hda_link_hw_free(struct snd_pcm_substream *substream,
350 			    struct snd_soc_dai *dai)
351 {
352 	unsigned int stream_tag;
353 	struct sof_intel_hda_stream *hda_stream;
354 	struct hdac_bus *bus;
355 	struct hdac_ext_link *link;
356 	struct hdac_stream *hstream;
357 	struct snd_soc_pcm_runtime *rtd;
358 	struct hdac_ext_stream *link_dev;
359 	int ret;
360 
361 	hstream = substream->runtime->private_data;
362 	bus = hstream->bus;
363 	rtd = snd_pcm_substream_chip(substream);
364 	link_dev = snd_soc_dai_get_dma_data(dai, substream);
365 	hda_stream = hstream_to_sof_hda_stream(link_dev);
366 
367 	/* free the link DMA channel in the FW */
368 	ret = hda_link_config_ipc(hda_stream, dai->name, DMA_CHAN_INVALID,
369 				  substream->stream);
370 	if (ret < 0)
371 		return ret;
372 
373 	link = snd_hdac_ext_bus_get_link(bus, rtd->codec_dai->component->name);
374 	if (!link)
375 		return -EINVAL;
376 
377 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
378 		stream_tag = hdac_stream(link_dev)->stream_tag;
379 		snd_hdac_ext_link_clear_stream_id(link, stream_tag);
380 	}
381 
382 	snd_soc_dai_set_dma_data(dai, substream, NULL);
383 	snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
384 	link_dev->link_prepared = 0;
385 
386 	/* free the host DMA channel reserved by hostless streams */
387 	hda_stream->host_reserved = 0;
388 
389 	return 0;
390 }
391 
392 static const struct snd_soc_dai_ops hda_link_dai_ops = {
393 	.hw_params = hda_link_hw_params,
394 	.hw_free = hda_link_hw_free,
395 	.trigger = hda_link_pcm_trigger,
396 	.prepare = hda_link_pcm_prepare,
397 };
398 #endif
399 
400 /*
401  * common dai driver for skl+ platforms.
402  * some products who use this DAI array only physically have a subset of
403  * the DAIs, but no harm is done here by adding the whole set.
404  */
405 struct snd_soc_dai_driver skl_dai[] = {
406 {
407 	.name = "SSP0 Pin",
408 },
409 {
410 	.name = "SSP1 Pin",
411 },
412 {
413 	.name = "SSP2 Pin",
414 },
415 {
416 	.name = "SSP3 Pin",
417 },
418 {
419 	.name = "SSP4 Pin",
420 },
421 {
422 	.name = "SSP5 Pin",
423 },
424 {
425 	.name = "DMIC01 Pin",
426 },
427 {
428 	.name = "DMIC16k Pin",
429 },
430 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
431 {
432 	.name = "iDisp1 Pin",
433 	.ops = &hda_link_dai_ops,
434 },
435 {
436 	.name = "iDisp2 Pin",
437 	.ops = &hda_link_dai_ops,
438 },
439 {
440 	.name = "iDisp3 Pin",
441 	.ops = &hda_link_dai_ops,
442 },
443 {
444 	.name = "Analog CPU DAI",
445 	.ops = &hda_link_dai_ops,
446 },
447 {
448 	.name = "Digital CPU DAI",
449 	.ops = &hda_link_dai_ops,
450 },
451 {
452 	.name = "Alt Analog CPU DAI",
453 	.ops = &hda_link_dai_ops,
454 },
455 #endif
456 };
457