Linux Audio

Check our new training course

Loading...
v6.8
  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 <sound/hda-mlink.h>
 14#include <sound/hda_register.h>
 15#include <sound/intel-nhlt.h>
 16#include <sound/sof/ipc4/header.h>
 17#include <uapi/sound/sof/header.h>
 18#include "../ipc4-priv.h"
 19#include "../ipc4-topology.h"
 20#include "../sof-priv.h"
 21#include "../sof-audio.h"
 22#include "hda.h"
 23
 24/*
 25 * The default method is to fetch NHLT from BIOS. With this parameter set
 26 * it is possible to override that with NHLT in the SOF topology manifest.
 27 */
 28static bool hda_use_tplg_nhlt;
 29module_param_named(sof_use_tplg_nhlt, hda_use_tplg_nhlt, bool, 0444);
 30MODULE_PARM_DESC(sof_use_tplg_nhlt, "SOF topology nhlt override");
 31
 32static struct snd_sof_dev *widget_to_sdev(struct snd_soc_dapm_widget *w)
 33{
 34	struct snd_sof_widget *swidget = w->dobj.private;
 35	struct snd_soc_component *component = swidget->scomp;
 36
 37	return snd_soc_component_get_drvdata(component);
 38}
 
 
 
 
 
 39
 40int hda_dai_config(struct snd_soc_dapm_widget *w, unsigned int flags,
 41		   struct snd_sof_dai_config_data *data)
 
 
 
 
 
 42{
 43	struct snd_sof_widget *swidget = w->dobj.private;
 44	const struct sof_ipc_tplg_ops *tplg_ops;
 45	struct snd_sof_dev *sdev;
 46	int ret;
 47
 48	if (!swidget)
 49		return 0;
 50
 51	sdev = widget_to_sdev(w);
 52	tplg_ops = sof_ipc_get_ops(sdev, tplg);
 53
 54	if (tplg_ops && tplg_ops->dai_config) {
 55		ret = tplg_ops->dai_config(sdev, swidget, flags, data);
 56		if (ret < 0) {
 57			dev_err(sdev->dev, "DAI config with flags %x failed for widget %s\n",
 58				flags, w->name);
 59			return ret;
 60		}
 61	}
 62
 63	return 0;
 64}
 65
 66#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK)
 67
 68static struct snd_sof_dev *dai_to_sdev(struct snd_pcm_substream *substream,
 69				       struct snd_soc_dai *cpu_dai)
 70{
 71	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
 72
 73	return widget_to_sdev(w);
 74}
 75
 76static const struct hda_dai_widget_dma_ops *
 77hda_dai_get_ops(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai)
 78{
 79	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
 80	struct snd_sof_widget *swidget = w->dobj.private;
 81	struct snd_sof_dev *sdev;
 82	struct snd_sof_dai *sdai;
 
 83
 84	sdev = widget_to_sdev(w);
 85
 86	/*
 87	 * The swidget parameter of hda_select_dai_widget_ops() is ignored in
 88	 * case of DSPless mode
 89	 */
 90	if (sdev->dspless_mode_selected)
 91		return hda_select_dai_widget_ops(sdev, NULL);
 92
 93	sdai = swidget->private;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 94
 95	/* select and set the DAI widget ops if not set already */
 96	if (!sdai->platform_private) {
 97		const struct hda_dai_widget_dma_ops *ops =
 98			hda_select_dai_widget_ops(sdev, swidget);
 99		if (!ops)
100			return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
102		/* check if mandatory ops are set */
103		if (!ops || !ops->get_hext_stream)
104			return NULL;
105
106		sdai->platform_private = ops;
 
107	}
 
108
109	return sdai->platform_private;
110}
111
112int hda_link_dma_cleanup(struct snd_pcm_substream *substream, struct hdac_ext_stream *hext_stream,
113			 struct snd_soc_dai *cpu_dai)
 
 
 
114{
115	const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai);
 
116	struct sof_intel_hda_stream *hda_stream;
117	struct hdac_ext_link *hlink;
118	struct snd_sof_dev *sdev;
119	int stream_tag;
120
121	if (!ops) {
122		dev_err(cpu_dai->dev, "DAI widget ops not set\n");
123		return -EINVAL;
124	}
125
126	sdev = dai_to_sdev(substream, cpu_dai);
127
128	hlink = ops->get_hlink(sdev, substream);
129	if (!hlink)
130		return -EINVAL;
131
 
 
 
132	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
133		stream_tag = hdac_stream(hext_stream)->stream_tag;
134		snd_hdac_ext_bus_link_clear_stream_id(hlink, stream_tag);
135	}
136
137	if (ops->release_hext_stream)
138		ops->release_hext_stream(sdev, cpu_dai, substream);
139
140	hext_stream->link_prepared = 0;
141
142	/* free the host DMA channel reserved by hostless streams */
143	hda_stream = hstream_to_sof_hda_stream(hext_stream);
144	hda_stream->host_reserved = 0;
145
146	return 0;
147}
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149static int hda_link_dma_hw_params(struct snd_pcm_substream *substream,
150				  struct snd_pcm_hw_params *params, struct snd_soc_dai *cpu_dai)
151{
152	const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai);
 
 
 
153	struct hdac_ext_stream *hext_stream;
154	struct hdac_stream *hstream;
155	struct hdac_ext_link *hlink;
156	struct snd_sof_dev *sdev;
157	int stream_tag;
158
159	if (!ops) {
160		dev_err(cpu_dai->dev, "DAI widget ops not set\n");
161		return -EINVAL;
162	}
163
164	sdev = dai_to_sdev(substream, cpu_dai);
 
165
166	hlink = ops->get_hlink(sdev, substream);
167	if (!hlink)
168		return -EINVAL;
169
170	hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
171
172	if (!hext_stream) {
173		if (ops->assign_hext_stream)
174			hext_stream = ops->assign_hext_stream(sdev, cpu_dai, substream);
 
 
 
175	}
176
177	if (!hext_stream)
178		return -EBUSY;
179
180	hstream = &hext_stream->hstream;
181	stream_tag = hstream->stream_tag;
 
 
182
183	if (hext_stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK)
184		snd_hdac_ext_bus_link_set_stream_id(hlink, stream_tag);
 
 
185
186	/* set the hdac_stream in the codec dai */
187	if (ops->codec_dai_set_stream)
188		ops->codec_dai_set_stream(sdev, substream, hstream);
189
190	if (ops->reset_hext_stream)
191		ops->reset_hext_stream(sdev, hext_stream);
 
 
192
193	if (ops->calc_stream_format && ops->setup_hext_stream) {
194		unsigned int format_val = ops->calc_stream_format(sdev, substream, params);
195
196		ops->setup_hext_stream(sdev, hext_stream, format_val);
197	}
 
 
 
 
 
198
199	hext_stream->link_prepared = 1;
 
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201	return 0;
202}
203
204static int __maybe_unused hda_dai_hw_free(struct snd_pcm_substream *substream,
205					  struct snd_soc_dai *cpu_dai)
206{
207	const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai);
 
 
208	struct hdac_ext_stream *hext_stream;
209	struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai);
210
211	if (!ops) {
212		dev_err(cpu_dai->dev, "DAI widget ops not set\n");
213		return -EINVAL;
214	}
215
216	hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
217	if (!hext_stream)
218		return 0;
219
220	return hda_link_dma_cleanup(substream, hext_stream, cpu_dai);
221}
222
223static int __maybe_unused hda_dai_hw_params(struct snd_pcm_substream *substream,
224					    struct snd_pcm_hw_params *params,
225					    struct snd_soc_dai *dai)
226{
227	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, substream->stream);
228	const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, dai);
229	struct hdac_ext_stream *hext_stream;
230	struct snd_sof_dai_config_data data = { 0 };
231	unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
232	struct snd_sof_dev *sdev = widget_to_sdev(w);
233	int ret;
234
235	if (!ops) {
236		dev_err(sdev->dev, "DAI widget ops not set\n");
237		return -EINVAL;
238	}
239
240	hext_stream = ops->get_hext_stream(sdev, dai, substream);
241	if (hext_stream && hext_stream->link_prepared)
242		return 0;
243
244	ret = hda_link_dma_hw_params(substream, params, dai);
245	if (ret < 0)
246		return ret;
247
248	hext_stream = ops->get_hext_stream(sdev, dai, substream);
 
 
 
 
 
 
249
250	flags |= SOF_DAI_CONFIG_FLAGS_2_STEP_STOP << SOF_DAI_CONFIG_FLAGS_QUIRK_SHIFT;
251	data.dai_data = hdac_stream(hext_stream)->stream_tag - 1;
 
 
 
 
 
252
253	return hda_dai_config(w, flags, &data);
 
254}
255
256/*
257 * In contrast to IPC3, the dai trigger in IPC4 mixes pipeline state changes
258 * (over IPC channel) and DMA state change (direct host register changes).
259 */
260static int __maybe_unused hda_dai_trigger(struct snd_pcm_substream *substream, int cmd,
261					  struct snd_soc_dai *dai)
262{
263	const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, dai);
264	struct hdac_ext_stream *hext_stream;
265	struct snd_sof_dev *sdev;
266	int ret;
267
268	if (!ops) {
269		dev_err(dai->dev, "DAI widget ops not set\n");
270		return -EINVAL;
271	}
272
273	dev_dbg(dai->dev, "cmd=%d dai %s direction %d\n", cmd,
274		dai->name, substream->stream);
275
276	sdev = dai_to_sdev(substream, dai);
 
 
277
278	hext_stream = ops->get_hext_stream(sdev, dai, substream);
279	if (!hext_stream)
280		return -EINVAL;
281
282	if (ops->pre_trigger) {
283		ret = ops->pre_trigger(sdev, dai, substream, cmd);
284		if (ret < 0)
285			return ret;
286	}
287
288	if (ops->trigger) {
289		ret = ops->trigger(sdev, dai, substream, cmd);
290		if (ret < 0)
291			return ret;
292	}
 
 
293
294	if (ops->post_trigger) {
295		ret = ops->post_trigger(sdev, dai, substream, cmd);
296		if (ret < 0)
297			return ret;
298	}
299
300	switch (cmd) {
301	case SNDRV_PCM_TRIGGER_SUSPEND:
302		ret = hda_link_dma_cleanup(substream, hext_stream, dai);
303		if (ret < 0) {
304			dev_err(sdev->dev, "%s: failed to clean up link DMA\n", __func__);
305			return ret;
306		}
307		break;
308	default:
309		break;
310	}
311
312	return 0;
313}
314
315#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
316
317static int hda_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
318{
319	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
 
 
 
320	int stream = substream->stream;
 
321
322	return hda_dai_hw_params(substream, &rtd->dpcm[stream].hw_params, dai);
323}
324
325static const struct snd_soc_dai_ops hda_dai_ops = {
326	.hw_params = hda_dai_hw_params,
327	.hw_free = hda_dai_hw_free,
328	.trigger = hda_dai_trigger,
329	.prepare = hda_dai_prepare,
330};
331
332#endif
 
 
333
334static struct sof_ipc4_copier *widget_to_copier(struct snd_soc_dapm_widget *w)
 
 
 
 
335{
336	struct snd_sof_widget *swidget = w->dobj.private;
337	struct snd_sof_dai *sdai = swidget->private;
338	struct sof_ipc4_copier *ipc4_copier = (struct sof_ipc4_copier *)sdai->private;
339
340	return ipc4_copier;
 
 
 
341}
342
343static int non_hda_dai_hw_params(struct snd_pcm_substream *substream,
344				 struct snd_pcm_hw_params *params,
345				 struct snd_soc_dai *cpu_dai)
346{
347	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
348	struct sof_ipc4_dma_config_tlv *dma_config_tlv;
349	const struct hda_dai_widget_dma_ops *ops;
350	struct sof_ipc4_dma_config *dma_config;
351	struct sof_ipc4_copier *ipc4_copier;
352	struct hdac_ext_stream *hext_stream;
353	struct hdac_stream *hstream;
354	struct snd_sof_dev *sdev;
355	int stream_id;
356	int ret;
357
358	ops = hda_dai_get_ops(substream, cpu_dai);
359	if (!ops) {
360		dev_err(cpu_dai->dev, "DAI widget ops not set\n");
361		return -EINVAL;
362	}
363
364	/* use HDaudio stream handling */
365	ret = hda_dai_hw_params(substream, params, cpu_dai);
366	if (ret < 0) {
367		dev_err(cpu_dai->dev, "%s: hda_dai_hw_params failed: %d\n", __func__, ret);
368		return ret;
369	}
370
371	/* get stream_id */
372	sdev = widget_to_sdev(w);
373	hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
374
375	if (!hext_stream) {
376		dev_err(cpu_dai->dev, "%s: no hext_stream found\n", __func__);
377		return -ENODEV;
378	}
 
 
 
 
 
379
380	hstream = &hext_stream->hstream;
381	stream_id = hstream->stream_tag;
 
 
 
 
382
383	if (!stream_id) {
384		dev_err(cpu_dai->dev, "%s: no stream_id allocated\n", __func__);
385		return -ENODEV;
386	}
 
 
387
388	/* configure TLV */
389	ipc4_copier = widget_to_copier(w);
 
 
 
 
 
 
 
 
 
 
 
 
 
390
391	dma_config_tlv = &ipc4_copier->dma_config_tlv;
392	dma_config_tlv->type = SOF_IPC4_GTW_DMA_CONFIG_ID;
393	/* dma_config_priv_size is zero */
394	dma_config_tlv->length = sizeof(dma_config_tlv->dma_config);
395
396	dma_config = &dma_config_tlv->dma_config;
 
 
397
398	dma_config->dma_method = SOF_IPC4_DMA_METHOD_HDA;
399	dma_config->pre_allocated_by_host = 1;
400	dma_config->dma_channel_id = stream_id - 1;
401	dma_config->stream_id = stream_id;
402	dma_config->dma_stream_channel_map.device_count = 0; /* mapping not used */
403	dma_config->dma_priv_config_size = 0;
404
405	return 0;
406}
 
 
 
 
 
 
 
 
407
408static int non_hda_dai_prepare(struct snd_pcm_substream *substream,
409			       struct snd_soc_dai *cpu_dai)
410{
411	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
412	int stream = substream->stream;
413
414	return non_hda_dai_hw_params(substream, &rtd->dpcm[stream].hw_params, cpu_dai);
415}
416
417static const struct snd_soc_dai_ops ssp_dai_ops = {
418	.hw_params = non_hda_dai_hw_params,
419	.hw_free = hda_dai_hw_free,
420	.trigger = hda_dai_trigger,
421	.prepare = non_hda_dai_prepare,
422};
423
424static const struct snd_soc_dai_ops dmic_dai_ops = {
425	.hw_params = non_hda_dai_hw_params,
426	.hw_free = hda_dai_hw_free,
427	.trigger = hda_dai_trigger,
428	.prepare = non_hda_dai_prepare,
429};
430
431int sdw_hda_dai_hw_params(struct snd_pcm_substream *substream,
432			  struct snd_pcm_hw_params *params,
433			  struct snd_soc_dai *cpu_dai,
434			  int link_id)
435{
436	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
437	const struct hda_dai_widget_dma_ops *ops;
438	struct hdac_ext_stream *hext_stream;
439	struct snd_sof_dev *sdev;
440	int ret;
441
442	ret = non_hda_dai_hw_params(substream, params, cpu_dai);
443	if (ret < 0) {
444		dev_err(cpu_dai->dev, "%s: non_hda_dai_hw_params failed %d\n", __func__, ret);
445		return ret;
 
 
446	}
 
 
 
 
447
448	ops = hda_dai_get_ops(substream, cpu_dai);
449	sdev = widget_to_sdev(w);
450	hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
 
451
452	if (!hext_stream)
453		return -ENODEV;
454
455	/* in the case of SoundWire we need to program the PCMSyCM registers */
456	ret = hdac_bus_eml_sdw_map_stream_ch(sof_to_bus(sdev), link_id, cpu_dai->id,
457					     GENMASK(params_channels(params) - 1, 0),
458					     hdac_stream(hext_stream)->stream_tag,
459					     substream->stream);
460	if (ret < 0) {
461		dev_err(cpu_dai->dev, "%s:  hdac_bus_eml_sdw_map_stream_ch failed %d\n",
462			__func__, ret);
463		return ret;
464	}
465
466	return 0;
467}
468
469int sdw_hda_dai_hw_free(struct snd_pcm_substream *substream,
470			struct snd_soc_dai *cpu_dai,
471			int link_id)
472{
473	struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
474	struct snd_sof_dev *sdev;
475	int ret;
476
477	ret = hda_dai_hw_free(substream, cpu_dai);
478	if (ret < 0) {
479		dev_err(cpu_dai->dev, "%s: non_hda_dai_hw_free failed %d\n", __func__, ret);
480		return ret;
481	}
482
483	sdev = widget_to_sdev(w);
484
485	/* in the case of SoundWire we need to reset the PCMSyCM registers */
486	ret = hdac_bus_eml_sdw_map_stream_ch(sof_to_bus(sdev), link_id, cpu_dai->id,
487					     0, 0, substream->stream);
488	if (ret < 0) {
489		dev_err(cpu_dai->dev, "%s:  hdac_bus_eml_sdw_map_stream_ch failed %d\n",
490			__func__, ret);
491		return ret;
492	}
493
494	return 0;
495}
496
497int sdw_hda_dai_trigger(struct snd_pcm_substream *substream, int cmd,
498			struct snd_soc_dai *cpu_dai)
499{
500	return hda_dai_trigger(substream, cmd, cpu_dai);
501}
 
502
503static int hda_dai_suspend(struct hdac_bus *bus)
504{
505	struct snd_soc_pcm_runtime *rtd;
506	struct hdac_ext_stream *hext_stream;
507	struct hdac_stream *s;
508	int ret;
509
510	/* set internal flag for BE */
511	list_for_each_entry(s, &bus->stream_list, list) {
512
513		hext_stream = stream_to_hdac_ext_stream(s);
514
515		/*
516		 * clear stream. This should already be taken care for running
517		 * streams when the SUSPEND trigger is called. But paused
518		 * streams do not get suspended, so this needs to be done
519		 * explicitly during suspend.
520		 */
521		if (hext_stream->link_substream) {
522			const struct hda_dai_widget_dma_ops *ops;
523			struct snd_sof_widget *swidget;
524			struct snd_soc_dapm_widget *w;
525			struct snd_soc_dai *cpu_dai;
526			struct snd_sof_dev *sdev;
527			struct snd_sof_dai *sdai;
528
529			rtd = snd_soc_substream_to_rtd(hext_stream->link_substream);
530			cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
531			w = snd_soc_dai_get_widget(cpu_dai, hdac_stream(hext_stream)->direction);
532			swidget = w->dobj.private;
533			sdev = widget_to_sdev(w);
534			sdai = swidget->private;
535			ops = sdai->platform_private;
536
537			ret = hda_link_dma_cleanup(hext_stream->link_substream,
538						   hext_stream,
539						   cpu_dai);
540			if (ret < 0)
541				return ret;
542
543			/* for consistency with TRIGGER_SUSPEND  */
544			if (ops->post_trigger) {
545				ret = ops->post_trigger(sdev, cpu_dai,
546							hext_stream->link_substream,
547							SNDRV_PCM_TRIGGER_SUSPEND);
548				if (ret < 0)
549					return ret;
550			}
551		}
552	}
553
554	return 0;
555}
556
557static void ssp_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558{
559	const struct sof_intel_dsp_desc *chip;
560	int i;
561
562	chip = get_chip_info(sdev->pdata);
563
564	if (chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
565		for (i = 0; i < ops->num_drv; i++) {
566			if (strstr(ops->drv[i].name, "SSP"))
567				ops->drv[i].ops = &ssp_dai_ops;
568		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
569	}
 
 
 
 
 
 
 
570}
571
572static void dmic_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
 
 
573{
574	const struct sof_intel_dsp_desc *chip;
575	int i;
 
576
577	chip = get_chip_info(sdev->pdata);
 
 
 
 
 
 
 
 
578
579	if (chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
580		for (i = 0; i < ops->num_drv; i++) {
581			if (strstr(ops->drv[i].name, "DMIC"))
582				ops->drv[i].ops = &dmic_dai_ops;
583		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584	}
 
 
585}
586
587#else
 
 
 
 
 
 
 
588
589static inline void ssp_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) {}
590static inline void dmic_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) {}
 
 
 
 
 
 
591
592#endif /* CONFIG_SND_SOC_SOF_HDA_LINK */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
594void hda_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
595{
596	int i;
597
598	for (i = 0; i < ops->num_drv; i++) {
 
 
 
 
 
 
599#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
600		if (strstr(ops->drv[i].name, "iDisp") ||
601		    strstr(ops->drv[i].name, "Analog") ||
602		    strstr(ops->drv[i].name, "Digital"))
603			ops->drv[i].ops = &hda_dai_ops;
604#endif
605	}
 
 
 
 
606
607	ssp_set_dai_drv_ops(sdev, ops);
608	dmic_set_dai_drv_ops(sdev, ops);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
610	if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4 && !hda_use_tplg_nhlt) {
611		struct sof_ipc4_fw_data *ipc4_data = sdev->private;
612
613		ipc4_data->nhlt = intel_nhlt_init(sdev->dev);
 
 
 
 
 
 
614	}
615}
616
617void hda_ops_free(struct snd_sof_dev *sdev)
618{
619	if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) {
620		struct sof_ipc4_fw_data *ipc4_data = sdev->private;
621
622		if (!hda_use_tplg_nhlt)
623			intel_nhlt_free(ipc4_data->nhlt);
624
625		kfree(sdev->private);
626		sdev->private = NULL;
627	}
628}
629EXPORT_SYMBOL_NS(hda_ops_free, SND_SOC_SOF_INTEL_HDA_COMMON);
630
631/*
632 * common dai driver for skl+ platforms.
633 * some products who use this DAI array only physically have a subset of
634 * the DAIs, but no harm is done here by adding the whole set.
635 */
636struct snd_soc_dai_driver skl_dai[] = {
637{
638	.name = "SSP0 Pin",
639	.playback = {
640		.channels_min = 1,
641		.channels_max = 8,
642	},
643	.capture = {
644		.channels_min = 1,
645		.channels_max = 8,
646	},
647},
648{
649	.name = "SSP1 Pin",
650	.playback = {
651		.channels_min = 1,
652		.channels_max = 8,
653	},
654	.capture = {
655		.channels_min = 1,
656		.channels_max = 8,
657	},
658},
659{
660	.name = "SSP2 Pin",
661	.playback = {
662		.channels_min = 1,
663		.channels_max = 8,
664	},
665	.capture = {
666		.channels_min = 1,
667		.channels_max = 8,
668	},
669},
670{
671	.name = "SSP3 Pin",
672	.playback = {
673		.channels_min = 1,
674		.channels_max = 8,
675	},
676	.capture = {
677		.channels_min = 1,
678		.channels_max = 8,
679	},
680},
681{
682	.name = "SSP4 Pin",
683	.playback = {
684		.channels_min = 1,
685		.channels_max = 8,
686	},
687	.capture = {
688		.channels_min = 1,
689		.channels_max = 8,
690	},
691},
692{
693	.name = "SSP5 Pin",
694	.playback = {
695		.channels_min = 1,
696		.channels_max = 8,
697	},
698	.capture = {
699		.channels_min = 1,
700		.channels_max = 8,
701	},
702},
703{
704	.name = "DMIC01 Pin",
705	.capture = {
706		.channels_min = 1,
707		.channels_max = 4,
708	},
709},
710{
711	.name = "DMIC16k Pin",
712	.capture = {
713		.channels_min = 1,
714		.channels_max = 4,
715	},
716},
717#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
718{
719	.name = "iDisp1 Pin",
720	.playback = {
721		.channels_min = 1,
722		.channels_max = 8,
723	},
724},
725{
726	.name = "iDisp2 Pin",
727	.playback = {
728		.channels_min = 1,
729		.channels_max = 8,
730	},
731},
732{
733	.name = "iDisp3 Pin",
734	.playback = {
735		.channels_min = 1,
736		.channels_max = 8,
737	},
738},
739{
740	.name = "iDisp4 Pin",
741	.playback = {
742		.channels_min = 1,
743		.channels_max = 8,
744	},
745},
746{
747	.name = "Analog CPU DAI",
748	.playback = {
749		.channels_min = 1,
750		.channels_max = 16,
751	},
752	.capture = {
753		.channels_min = 1,
754		.channels_max = 16,
755	},
756},
757{
758	.name = "Digital CPU DAI",
759	.playback = {
760		.channels_min = 1,
761		.channels_max = 16,
762	},
763	.capture = {
764		.channels_min = 1,
765		.channels_max = 16,
766	},
767},
768{
769	.name = "Alt Analog CPU DAI",
770	.playback = {
771		.channels_min = 1,
772		.channels_max = 16,
773	},
774	.capture = {
775		.channels_min = 1,
776		.channels_max = 16,
777	},
778},
779#endif
780};
781
782int hda_dsp_dais_suspend(struct snd_sof_dev *sdev)
783{
784	/*
785	 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
786	 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
787	 * Since the component suspend is called last, we can trap this corner case
788	 * and force the DAIs to release their resources.
789	 */
790#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK)
791	int ret;
792
793	ret = hda_dai_suspend(sof_to_bus(sdev));
794	if (ret < 0)
795		return ret;
796#endif
797
798	return 0;
799}
v6.2
  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 <sound/intel-nhlt.h>
 14#include <sound/sof/ipc4/header.h>
 15#include <uapi/sound/sof/header.h>
 16#include "../ipc4-priv.h"
 17#include "../ipc4-topology.h"
 18#include "../sof-priv.h"
 19#include "../sof-audio.h"
 20#include "hda.h"
 21
 22/*
 23 * The default method is to fetch NHLT from BIOS. With this parameter set
 24 * it is possible to override that with NHLT in the SOF topology manifest.
 25 */
 26static bool hda_use_tplg_nhlt;
 27module_param_named(sof_use_tplg_nhlt, hda_use_tplg_nhlt, bool, 0444);
 28MODULE_PARM_DESC(sof_use_tplg_nhlt, "SOF topology nhlt override");
 29
 30#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
 
 
 
 31
 32struct hda_pipe_params {
 33	u32 ch;
 34	u32 s_freq;
 35	snd_pcm_format_t format;
 36	int link_index;
 37	unsigned int link_bps;
 38};
 39
 40/*
 41 * This function checks if the host dma channel corresponding
 42 * to the link DMA stream_tag argument is assigned to one
 43 * of the FEs connected to the BE DAI.
 44 */
 45static bool hda_check_fes(struct snd_soc_pcm_runtime *rtd,
 46			  int dir, int stream_tag)
 47{
 48	struct snd_pcm_substream *fe_substream;
 49	struct hdac_stream *fe_hstream;
 50	struct snd_soc_dpcm *dpcm;
 
 51
 52	for_each_dpcm_fe(rtd, dir, dpcm) {
 53		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, dir);
 54		fe_hstream = fe_substream->runtime->private_data;
 55		if (fe_hstream->stream_tag == stream_tag)
 56			return true;
 
 
 
 
 
 
 
 
 57	}
 58
 59	return false;
 60}
 61
 62static struct hdac_ext_stream *
 63hda_link_stream_assign(struct hdac_bus *bus,
 64		       struct snd_pcm_substream *substream)
 
 65{
 66	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 67	struct sof_intel_hda_stream *hda_stream;
 68	const struct sof_intel_dsp_desc *chip;
 
 
 
 
 
 
 
 69	struct snd_sof_dev *sdev;
 70	struct hdac_ext_stream *res = NULL;
 71	struct hdac_stream *hstream = NULL;
 72
 73	int stream_dir = substream->stream;
 74
 75	if (!bus->ppcap) {
 76		dev_err(bus->dev, "stream type not supported\n");
 77		return NULL;
 78	}
 79
 80	spin_lock_irq(&bus->reg_lock);
 81	list_for_each_entry(hstream, &bus->stream_list, list) {
 82		struct hdac_ext_stream *hext_stream =
 83			stream_to_hdac_ext_stream(hstream);
 84		if (hstream->direction != substream->stream)
 85			continue;
 86
 87		hda_stream = hstream_to_sof_hda_stream(hext_stream);
 88		sdev = hda_stream->sdev;
 89		chip = get_chip_info(sdev->pdata);
 90
 91		/* check if link is available */
 92		if (!hext_stream->link_locked) {
 93			/*
 94			 * choose the first available link for platforms that do not have the
 95			 * PROCEN_FMT_QUIRK set.
 96			 */
 97			if (!(chip->quirks & SOF_INTEL_PROCEN_FMT_QUIRK)) {
 98				res = hext_stream;
 99				break;
100			}
101
102			if (hstream->opened) {
103				/*
104				 * check if the stream tag matches the stream
105				 * tag of one of the connected FEs
106				 */
107				if (hda_check_fes(rtd, stream_dir,
108						  hstream->stream_tag)) {
109					res = hext_stream;
110					break;
111				}
112			} else {
113				res = hext_stream;
114
115				/*
116				 * This must be a hostless stream.
117				 * So reserve the host DMA channel.
118				 */
119				hda_stream->host_reserved = 1;
120				break;
121			}
122		}
123	}
124
125	if (res) {
126		/* Make sure that host and link DMA is decoupled. */
127		snd_hdac_ext_stream_decouple_locked(bus, res, true);
128
129		res->link_locked = 1;
130		res->link_substream = substream;
131	}
132	spin_unlock_irq(&bus->reg_lock);
133
134	return res;
135}
136
137static int hda_link_dma_cleanup(struct snd_pcm_substream *substream,
138				struct hdac_ext_stream *hext_stream,
139				struct snd_soc_dai *cpu_dai,
140				struct snd_soc_dai *codec_dai,
141				bool trigger_suspend_stop)
142{
143	struct hdac_stream *hstream = &hext_stream->hstream;
144	struct hdac_bus *bus = hstream->bus;
145	struct sof_intel_hda_stream *hda_stream;
146	struct hdac_ext_link *hlink;
 
147	int stream_tag;
148
149	hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, codec_dai->component->name);
 
 
 
 
 
 
 
150	if (!hlink)
151		return -EINVAL;
152
153	if (trigger_suspend_stop)
154		snd_hdac_ext_stream_clear(hext_stream);
155
156	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
157		stream_tag = hdac_stream(hext_stream)->stream_tag;
158		snd_hdac_ext_bus_link_clear_stream_id(hlink, stream_tag);
159	}
160	snd_soc_dai_set_dma_data(cpu_dai, substream, NULL);
161	snd_hdac_ext_stream_release(hext_stream, HDAC_EXT_STREAM_TYPE_LINK);
 
 
162	hext_stream->link_prepared = 0;
163
164	/* free the host DMA channel reserved by hostless streams */
165	hda_stream = hstream_to_sof_hda_stream(hext_stream);
166	hda_stream->host_reserved = 0;
167
168	return 0;
169}
170
171static int hda_link_dma_params(struct hdac_ext_stream *hext_stream,
172			       struct hda_pipe_params *params)
173{
174	struct hdac_stream *hstream = &hext_stream->hstream;
175	unsigned char stream_tag = hstream->stream_tag;
176	struct hdac_bus *bus = hstream->bus;
177	struct hdac_ext_link *hlink;
178	unsigned int format_val;
179
180	snd_hdac_ext_stream_reset(hext_stream);
181
182	format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
183						 params->format,
184						 params->link_bps, 0);
185
186	dev_dbg(bus->dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
187		format_val, params->s_freq, params->ch, params->format);
188
189	snd_hdac_ext_stream_setup(hext_stream, format_val);
190
191	if (hext_stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) {
192		list_for_each_entry(hlink, &bus->hlink_list, list) {
193			if (hlink->index == params->link_index)
194				snd_hdac_ext_bus_link_set_stream_id(hlink,
195								    stream_tag);
196		}
197	}
198
199	hext_stream->link_prepared = 1;
200
201	return 0;
202}
203
204static int hda_link_dma_hw_params(struct snd_pcm_substream *substream,
205				  struct snd_pcm_hw_params *params)
206{
207	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
208	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
209	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
210	struct hda_pipe_params p_params = {0};
211	struct hdac_ext_stream *hext_stream;
 
212	struct hdac_ext_link *hlink;
213	struct snd_sof_dev *sdev;
214	struct hdac_bus *bus;
 
 
 
 
 
215
216	sdev = snd_soc_component_get_drvdata(cpu_dai->component);
217	bus = sof_to_bus(sdev);
218
219	hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, codec_dai->component->name);
220	if (!hlink)
221		return -EINVAL;
222
223	hext_stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
 
224	if (!hext_stream) {
225		hext_stream = hda_link_stream_assign(bus, substream);
226		if (!hext_stream)
227			return -EBUSY;
228
229		snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)hext_stream);
230	}
231
232	/* set the hdac_stream in the codec dai */
233	snd_soc_dai_set_stream(codec_dai, hdac_stream(hext_stream), substream->stream);
234
235	p_params.ch = params_channels(params);
236	p_params.s_freq = params_rate(params);
237	p_params.link_index = hlink->index;
238	p_params.format = params_format(params);
239
240	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
241		p_params.link_bps = codec_dai->driver->playback.sig_bits;
242	else
243		p_params.link_bps = codec_dai->driver->capture.sig_bits;
244
245	return hda_link_dma_params(hext_stream, &p_params);
246}
 
247
248static int hda_link_dma_prepare(struct snd_pcm_substream *substream)
249{
250	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
251	int stream = substream->stream;
252
253	return hda_link_dma_hw_params(substream, &rtd->dpcm[stream].hw_params);
254}
255
256static int hda_link_dma_trigger(struct snd_pcm_substream *substream, int cmd)
257{
258	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
259	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
260	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
261	struct hdac_ext_stream *hext_stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
262	int ret;
263
264	if (!hext_stream)
265		return 0;
266
267	switch (cmd) {
268	case SNDRV_PCM_TRIGGER_START:
269	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
270		snd_hdac_ext_stream_start(hext_stream);
271		break;
272	case SNDRV_PCM_TRIGGER_SUSPEND:
273	case SNDRV_PCM_TRIGGER_STOP:
274		ret = hda_link_dma_cleanup(substream, hext_stream, cpu_dai, codec_dai, true);
275		if (ret < 0)
276			return ret;
277
278		break;
279	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
280		snd_hdac_ext_stream_clear(hext_stream);
281
282		break;
283	default:
284		return -EINVAL;
285	}
286	return 0;
287}
288
289static int hda_link_dma_hw_free(struct snd_pcm_substream *substream)
 
290{
291	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
292	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
293	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
294	struct hdac_ext_stream *hext_stream;
 
 
 
 
 
 
295
296	hext_stream = snd_soc_dai_get_dma_data(cpu_dai, substream);
297	if (!hext_stream)
298		return 0;
299
300	return hda_link_dma_cleanup(substream, hext_stream, cpu_dai, codec_dai, false);
301}
302
303static int hda_dai_widget_update(struct snd_soc_dapm_widget *w,
304				 int channel, bool widget_setup)
 
305{
306	struct snd_sof_dai_config_data data;
 
 
 
 
 
 
307
308	data.dai_data = channel;
 
 
 
309
310	/* set up/free DAI widget and send DAI_CONFIG IPC */
311	if (widget_setup)
312		return hda_ctrl_dai_widget_setup(w, SOF_DAI_CONFIG_FLAGS_2_STEP_STOP, &data);
313
314	return hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE, &data);
315}
 
316
317static int hda_dai_hw_params_update(struct snd_pcm_substream *substream,
318				    struct snd_pcm_hw_params *params,
319				    struct snd_soc_dai *dai)
320{
321	struct hdac_ext_stream *hext_stream;
322	struct snd_soc_dapm_widget *w;
323	int stream_tag;
324
325	hext_stream = snd_soc_dai_get_dma_data(dai, substream);
326	if (!hext_stream)
327		return -EINVAL;
328
329	stream_tag = hdac_stream(hext_stream)->stream_tag;
330
331	w = snd_soc_dai_get_widget(dai, substream->stream);
332
333	/* set up the DAI widget and send the DAI_CONFIG with the new tag */
334	return hda_dai_widget_update(w, stream_tag - 1, true);
335}
336
337static int hda_dai_hw_params(struct snd_pcm_substream *substream,
338			     struct snd_pcm_hw_params *params,
339			     struct snd_soc_dai *dai)
 
 
 
340{
341	struct hdac_ext_stream *hext_stream =
342				snd_soc_dai_get_dma_data(dai, substream);
 
343	int ret;
344
345	if (hext_stream && hext_stream->link_prepared)
346		return 0;
 
 
 
 
 
347
348	ret = hda_link_dma_hw_params(substream, params);
349	if (ret < 0)
350		return ret;
351
352	return hda_dai_hw_params_update(substream, params, dai);
353}
 
354
 
 
 
 
 
355
356static int hda_dai_config_pause_push_ipc(struct snd_soc_dapm_widget *w)
357{
358	struct snd_sof_widget *swidget = w->dobj.private;
359	struct snd_soc_component *component = swidget->scomp;
360	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
361	const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
362	int ret = 0;
363
364	if (tplg_ops->dai_config) {
365		ret = tplg_ops->dai_config(sdev, swidget, SOF_DAI_CONFIG_FLAGS_PAUSE, NULL);
366		if (ret < 0)
367			dev_err(sdev->dev, "%s: DAI config failed for widget %s\n", __func__,
368				w->name);
 
 
 
 
 
 
 
 
 
 
 
369	}
370
371	return ret;
372}
373
 
 
374static int hda_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
375{
376	struct hdac_ext_stream *hext_stream =
377				snd_soc_dai_get_dma_data(dai, substream);
378	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component);
379	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
380	int stream = substream->stream;
381	int ret;
382
383	if (hext_stream && hext_stream->link_prepared)
384		return 0;
385
386	dev_dbg(sdev->dev, "prepare stream dir %d\n", substream->stream);
 
 
 
 
 
387
388	ret = hda_link_dma_prepare(substream);
389	if (ret < 0)
390		return ret;
391
392	return hda_dai_hw_params_update(substream, &rtd->dpcm[stream].hw_params, dai);
393}
394
395static int hda_dai_hw_free_ipc(int stream, /* direction */
396			       struct snd_soc_dai *dai)
397{
398	struct snd_soc_dapm_widget *w;
 
 
399
400	w = snd_soc_dai_get_widget(dai, stream);
401
402	/* free the link DMA channel in the FW and the DAI widget */
403	return hda_dai_widget_update(w, DMA_CHAN_INVALID, false);
404}
405
406static int ipc3_hda_dai_trigger(struct snd_pcm_substream *substream,
407				int cmd, struct snd_soc_dai *dai)
 
408{
409	struct snd_soc_dapm_widget *w;
 
 
 
 
 
 
 
 
410	int ret;
411
412	dev_dbg(dai->dev, "cmd=%d dai %s direction %d\n", cmd,
413		dai->name, substream->stream);
 
 
 
414
415	ret = hda_link_dma_trigger(substream, cmd);
416	if (ret < 0)
 
 
417		return ret;
 
418
419	w = snd_soc_dai_get_widget(dai, substream->stream);
 
 
420
421	switch (cmd) {
422	case SNDRV_PCM_TRIGGER_SUSPEND:
423	case SNDRV_PCM_TRIGGER_STOP:
424		/*
425		 * free DAI widget during stop/suspend to keep widget use_count's balanced.
426		 */
427		ret = hda_dai_hw_free_ipc(substream->stream, dai);
428		if (ret < 0)
429			return ret;
430
431		break;
432	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
433		ret = hda_dai_config_pause_push_ipc(w);
434		if (ret < 0)
435			return ret;
436		break;
437
438	default:
439		break;
 
440	}
441	return 0;
442}
443
444/*
445 * In contrast to IPC3, the dai trigger in IPC4 mixes pipeline state changes
446 * (over IPC channel) and DMA state change (direct host register changes).
447 */
448static int ipc4_hda_dai_trigger(struct snd_pcm_substream *substream,
449				int cmd, struct snd_soc_dai *dai)
450{
451	struct hdac_ext_stream *hext_stream = snd_soc_dai_get_dma_data(dai, substream);
452	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(dai->component);
453	struct snd_soc_pcm_runtime *rtd;
454	struct snd_sof_widget *swidget;
455	struct snd_soc_dapm_widget *w;
456	struct snd_soc_dai *codec_dai;
457	struct snd_soc_dai *cpu_dai;
458	int ret;
459
460	dev_dbg(dai->dev, "cmd=%d dai %s direction %d\n", cmd,
461		dai->name, substream->stream);
 
 
462
463	rtd = asoc_substream_to_rtd(substream);
464	cpu_dai = asoc_rtd_to_cpu(rtd, 0);
465	codec_dai = asoc_rtd_to_codec(rtd, 0);
466
467	w = snd_soc_dai_get_widget(dai, substream->stream);
468	swidget = w->dobj.private;
 
 
 
 
469
470	switch (cmd) {
471	case SNDRV_PCM_TRIGGER_START:
472	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
473		snd_hdac_ext_stream_start(hext_stream);
474		break;
475	case SNDRV_PCM_TRIGGER_SUSPEND:
476	case SNDRV_PCM_TRIGGER_STOP:
477	{
478		struct snd_sof_widget *pipe_widget = swidget->pipe_widget;
479		struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
480
481		ret = sof_ipc4_set_pipeline_state(sdev, pipe_widget->instance_id,
482						  SOF_IPC4_PIPE_PAUSED);
483		if (ret < 0)
484			return ret;
 
485
486		pipeline->state = SOF_IPC4_PIPE_PAUSED;
 
487
488		snd_hdac_ext_stream_clear(hext_stream);
 
 
 
 
 
489
490		ret = sof_ipc4_set_pipeline_state(sdev, pipe_widget->instance_id,
491						  SOF_IPC4_PIPE_RESET);
492		if (ret < 0)
493			return ret;
 
 
494
495		pipeline->state = SOF_IPC4_PIPE_RESET;
 
 
 
 
 
 
 
 
 
496
497		ret = hda_link_dma_cleanup(substream, hext_stream, cpu_dai, codec_dai, false);
498		if (ret < 0) {
499			dev_err(sdev->dev, "%s: failed to clean up link DMA\n", __func__);
500			return ret;
501		}
502		break;
503	}
504	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
505	{
506		struct snd_sof_widget *pipe_widget = swidget->pipe_widget;
507		struct sof_ipc4_pipeline *pipeline = pipe_widget->private;
508
509		ret = sof_ipc4_set_pipeline_state(sdev, pipe_widget->instance_id,
510						  SOF_IPC4_PIPE_PAUSED);
511		if (ret < 0)
512			return ret;
513
514		pipeline->state = SOF_IPC4_PIPE_PAUSED;
 
515
516		snd_hdac_ext_stream_clear(hext_stream);
517		break;
518	}
519	default:
520		dev_err(sdev->dev, "%s: unknown trigger command %d\n", __func__, cmd);
521		return -EINVAL;
 
 
 
522	}
523
524	return 0;
525}
526
527static int hda_dai_hw_free(struct snd_pcm_substream *substream,
528			   struct snd_soc_dai *dai)
 
529{
 
 
530	int ret;
531
532	ret = hda_link_dma_hw_free(substream);
533	if (ret < 0)
 
 
 
 
 
 
 
 
 
 
 
 
534		return ret;
 
535
536	return hda_dai_hw_free_ipc(substream->stream, dai);
537}
538
539static const struct snd_soc_dai_ops ipc3_hda_dai_ops = {
540	.hw_params = hda_dai_hw_params,
541	.hw_free = hda_dai_hw_free,
542	.trigger = ipc3_hda_dai_trigger,
543	.prepare = hda_dai_prepare,
544};
545
546static int hda_dai_suspend(struct hdac_bus *bus)
547{
548	struct snd_soc_pcm_runtime *rtd;
549	struct hdac_ext_stream *hext_stream;
550	struct hdac_stream *s;
551	int ret;
552
553	/* set internal flag for BE */
554	list_for_each_entry(s, &bus->stream_list, list) {
555
556		hext_stream = stream_to_hdac_ext_stream(s);
557
558		/*
559		 * clear stream. This should already be taken care for running
560		 * streams when the SUSPEND trigger is called. But paused
561		 * streams do not get suspended, so this needs to be done
562		 * explicitly during suspend.
563		 */
564		if (hext_stream->link_substream) {
 
 
 
565			struct snd_soc_dai *cpu_dai;
566			struct snd_soc_dai *codec_dai;
 
567
568			rtd = asoc_substream_to_rtd(hext_stream->link_substream);
569			cpu_dai = asoc_rtd_to_cpu(rtd, 0);
570			codec_dai = asoc_rtd_to_codec(rtd, 0);
 
 
 
 
571
572			ret = hda_link_dma_cleanup(hext_stream->link_substream,
573						   hext_stream,
574						   cpu_dai, codec_dai, false);
575			if (ret < 0)
576				return ret;
577
578			/* for consistency with TRIGGER_SUSPEND we free DAI resources */
579			ret = hda_dai_hw_free_ipc(hdac_stream(hext_stream)->direction, cpu_dai);
580			if (ret < 0)
581				return ret;
 
 
 
 
582		}
583	}
584
585	return 0;
586}
587
588static const struct snd_soc_dai_ops ipc4_hda_dai_ops = {
589	.hw_params = hda_dai_hw_params,
590	.hw_free = hda_dai_hw_free,
591	.trigger = ipc4_hda_dai_trigger,
592	.prepare = hda_dai_prepare,
593};
594
595#endif
596
597/* only one flag used so far to harden hw_params/hw_free/trigger/prepare */
598struct ssp_dai_dma_data {
599	bool setup;
600};
601
602static int ssp_dai_setup_or_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai,
603				 bool setup)
604{
605	struct snd_soc_dapm_widget *w;
 
606
607	w = snd_soc_dai_get_widget(dai, substream->stream);
608
609	if (setup)
610		return hda_ctrl_dai_widget_setup(w, SOF_DAI_CONFIG_FLAGS_NONE, NULL);
611
612	return hda_ctrl_dai_widget_free(w, SOF_DAI_CONFIG_FLAGS_NONE, NULL);
613}
614
615static int ssp_dai_startup(struct snd_pcm_substream *substream,
616			   struct snd_soc_dai *dai)
617{
618	struct ssp_dai_dma_data *dma_data;
619
620	dma_data = kzalloc(sizeof(*dma_data), GFP_KERNEL);
621	if (!dma_data)
622		return -ENOMEM;
623
624	snd_soc_dai_set_dma_data(dai, substream, dma_data);
625
626	return 0;
627}
628
629static int ssp_dai_setup(struct snd_pcm_substream *substream,
630			 struct snd_soc_dai *dai,
631			 bool setup)
632{
633	struct ssp_dai_dma_data *dma_data;
634	int ret = 0;
635
636	dma_data = snd_soc_dai_get_dma_data(dai, substream);
637	if (!dma_data) {
638		dev_err(dai->dev, "%s: failed to get dma_data\n", __func__);
639		return -EIO;
640	}
641
642	if (dma_data->setup != setup) {
643		ret = ssp_dai_setup_or_free(substream, dai, setup);
644		if (!ret)
645			dma_data->setup = setup;
646	}
647	return ret;
648}
649
650static int ssp_dai_hw_params(struct snd_pcm_substream *substream,
651			     struct snd_pcm_hw_params *params,
652			     struct snd_soc_dai *dai)
653{
654	/* params are ignored for now */
655	return ssp_dai_setup(substream, dai, true);
656}
657
658static int ssp_dai_prepare(struct snd_pcm_substream *substream,
659			   struct snd_soc_dai *dai)
660{
661	/*
662	 * the SSP will only be reconfigured during resume operations and
663	 * not in case of xruns
664	 */
665	return ssp_dai_setup(substream, dai, true);
666}
667
668static int ipc3_ssp_dai_trigger(struct snd_pcm_substream *substream,
669				int cmd, struct snd_soc_dai *dai)
670{
671	if (cmd != SNDRV_PCM_TRIGGER_SUSPEND)
672		return 0;
673
674	return ssp_dai_setup(substream, dai, false);
675}
676
677static int ssp_dai_hw_free(struct snd_pcm_substream *substream,
678			   struct snd_soc_dai *dai)
679{
680	return ssp_dai_setup(substream, dai, false);
681}
682
683static void ssp_dai_shutdown(struct snd_pcm_substream *substream,
684			     struct snd_soc_dai *dai)
685{
686	struct ssp_dai_dma_data *dma_data;
687
688	dma_data = snd_soc_dai_get_dma_data(dai, substream);
689	if (!dma_data) {
690		dev_err(dai->dev, "%s: failed to get dma_data\n", __func__);
691		return;
692	}
693	snd_soc_dai_set_dma_data(dai, substream, NULL);
694	kfree(dma_data);
695}
696
697static const struct snd_soc_dai_ops ipc3_ssp_dai_ops = {
698	.startup = ssp_dai_startup,
699	.hw_params = ssp_dai_hw_params,
700	.prepare = ssp_dai_prepare,
701	.trigger = ipc3_ssp_dai_trigger,
702	.hw_free = ssp_dai_hw_free,
703	.shutdown = ssp_dai_shutdown,
704};
705
706static int ipc4_be_dai_common_trigger(struct snd_soc_dai *dai, int cmd, int stream)
707{
708	struct snd_sof_widget *pipe_widget;
709	struct sof_ipc4_pipeline *pipeline;
710	struct snd_sof_widget *swidget;
711	struct snd_soc_dapm_widget *w;
712	struct snd_sof_dev *sdev;
713	int ret;
714
715	w = snd_soc_dai_get_widget(dai, stream);
716	swidget = w->dobj.private;
717	pipe_widget = swidget->pipe_widget;
718	pipeline = pipe_widget->private;
719	sdev = snd_soc_component_get_drvdata(swidget->scomp);
720
721	switch (cmd) {
722	case SNDRV_PCM_TRIGGER_SUSPEND:
723	case SNDRV_PCM_TRIGGER_STOP:
724		ret = sof_ipc4_set_pipeline_state(sdev, pipe_widget->instance_id,
725						  SOF_IPC4_PIPE_PAUSED);
726		if (ret < 0)
727			return ret;
728		pipeline->state = SOF_IPC4_PIPE_PAUSED;
729
730		ret = sof_ipc4_set_pipeline_state(sdev, pipe_widget->instance_id,
731						  SOF_IPC4_PIPE_RESET);
732		if (ret < 0)
733			return ret;
734		pipeline->state = SOF_IPC4_PIPE_RESET;
735		break;
736	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
737		ret = sof_ipc4_set_pipeline_state(sdev, pipe_widget->instance_id,
738						  SOF_IPC4_PIPE_PAUSED);
739		if (ret < 0)
740			return ret;
741		pipeline->state = SOF_IPC4_PIPE_PAUSED;
742		break;
743	default:
744		break;
745	}
746
747	return 0;
748}
749
750static int ipc4_be_dai_trigger(struct snd_pcm_substream *substream,
751			       int cmd, struct snd_soc_dai *dai)
752{
753	return ipc4_be_dai_common_trigger(dai, cmd, substream->stream);
754}
755
756static const struct snd_soc_dai_ops ipc4_dmic_dai_ops = {
757	.trigger = ipc4_be_dai_trigger,
758};
759
760static const struct snd_soc_dai_ops ipc4_ssp_dai_ops = {
761	.trigger = ipc4_be_dai_trigger,
762};
763
764void hda_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
765{
766	int i;
767
768	switch (sdev->pdata->ipc_type) {
769	case SOF_IPC:
770		for (i = 0; i < ops->num_drv; i++) {
771			if (strstr(ops->drv[i].name, "SSP")) {
772				ops->drv[i].ops = &ipc3_ssp_dai_ops;
773				continue;
774			}
775#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
776			if (strstr(ops->drv[i].name, "iDisp") ||
777			    strstr(ops->drv[i].name, "Analog") ||
778			    strstr(ops->drv[i].name, "Digital"))
779				ops->drv[i].ops = &ipc3_hda_dai_ops;
780#endif
781		}
782		break;
783	case SOF_INTEL_IPC4:
784	{
785		struct sof_ipc4_fw_data *ipc4_data = sdev->private;
786
787		for (i = 0; i < ops->num_drv; i++) {
788			if (strstr(ops->drv[i].name, "DMIC")) {
789				ops->drv[i].ops = &ipc4_dmic_dai_ops;
790				continue;
791			}
792			if (strstr(ops->drv[i].name, "SSP")) {
793				ops->drv[i].ops = &ipc4_ssp_dai_ops;
794				continue;
795			}
796#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
797			if (strstr(ops->drv[i].name, "iDisp") ||
798			    strstr(ops->drv[i].name, "Analog") ||
799			    strstr(ops->drv[i].name, "Digital"))
800				ops->drv[i].ops = &ipc4_hda_dai_ops;
801#endif
802		}
803
804		if (!hda_use_tplg_nhlt)
805			ipc4_data->nhlt = intel_nhlt_init(sdev->dev);
806
807		if (IS_ENABLED(CONFIG_SND_SOC_SOF_INTEL_SOUNDWIRE))
808			sdw_callback.trigger = ipc4_be_dai_common_trigger;
809
810		break;
811	}
812	default:
813		break;
814	}
815}
816
817void hda_ops_free(struct snd_sof_dev *sdev)
818{
819	if (sdev->pdata->ipc_type == SOF_INTEL_IPC4) {
820		struct sof_ipc4_fw_data *ipc4_data = sdev->private;
821
822		if (!hda_use_tplg_nhlt)
823			intel_nhlt_free(ipc4_data->nhlt);
 
 
 
824	}
825}
826EXPORT_SYMBOL_NS(hda_ops_free, SND_SOC_SOF_INTEL_HDA_COMMON);
827
828/*
829 * common dai driver for skl+ platforms.
830 * some products who use this DAI array only physically have a subset of
831 * the DAIs, but no harm is done here by adding the whole set.
832 */
833struct snd_soc_dai_driver skl_dai[] = {
834{
835	.name = "SSP0 Pin",
836	.playback = {
837		.channels_min = 1,
838		.channels_max = 8,
839	},
840	.capture = {
841		.channels_min = 1,
842		.channels_max = 8,
843	},
844},
845{
846	.name = "SSP1 Pin",
847	.playback = {
848		.channels_min = 1,
849		.channels_max = 8,
850	},
851	.capture = {
852		.channels_min = 1,
853		.channels_max = 8,
854	},
855},
856{
857	.name = "SSP2 Pin",
858	.playback = {
859		.channels_min = 1,
860		.channels_max = 8,
861	},
862	.capture = {
863		.channels_min = 1,
864		.channels_max = 8,
865	},
866},
867{
868	.name = "SSP3 Pin",
869	.playback = {
870		.channels_min = 1,
871		.channels_max = 8,
872	},
873	.capture = {
874		.channels_min = 1,
875		.channels_max = 8,
876	},
877},
878{
879	.name = "SSP4 Pin",
880	.playback = {
881		.channels_min = 1,
882		.channels_max = 8,
883	},
884	.capture = {
885		.channels_min = 1,
886		.channels_max = 8,
887	},
888},
889{
890	.name = "SSP5 Pin",
891	.playback = {
892		.channels_min = 1,
893		.channels_max = 8,
894	},
895	.capture = {
896		.channels_min = 1,
897		.channels_max = 8,
898	},
899},
900{
901	.name = "DMIC01 Pin",
902	.capture = {
903		.channels_min = 1,
904		.channels_max = 4,
905	},
906},
907{
908	.name = "DMIC16k Pin",
909	.capture = {
910		.channels_min = 1,
911		.channels_max = 4,
912	},
913},
914#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
915{
916	.name = "iDisp1 Pin",
917	.playback = {
918		.channels_min = 1,
919		.channels_max = 8,
920	},
921},
922{
923	.name = "iDisp2 Pin",
924	.playback = {
925		.channels_min = 1,
926		.channels_max = 8,
927	},
928},
929{
930	.name = "iDisp3 Pin",
931	.playback = {
932		.channels_min = 1,
933		.channels_max = 8,
934	},
935},
936{
937	.name = "iDisp4 Pin",
938	.playback = {
939		.channels_min = 1,
940		.channels_max = 8,
941	},
942},
943{
944	.name = "Analog CPU DAI",
945	.playback = {
946		.channels_min = 1,
947		.channels_max = 16,
948	},
949	.capture = {
950		.channels_min = 1,
951		.channels_max = 16,
952	},
953},
954{
955	.name = "Digital CPU DAI",
956	.playback = {
957		.channels_min = 1,
958		.channels_max = 16,
959	},
960	.capture = {
961		.channels_min = 1,
962		.channels_max = 16,
963	},
964},
965{
966	.name = "Alt Analog CPU DAI",
967	.playback = {
968		.channels_min = 1,
969		.channels_max = 16,
970	},
971	.capture = {
972		.channels_min = 1,
973		.channels_max = 16,
974	},
975},
976#endif
977};
978
979int hda_dsp_dais_suspend(struct snd_sof_dev *sdev)
980{
981	/*
982	 * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
983	 * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
984	 * Since the component suspend is called last, we can trap this corner case
985	 * and force the DAIs to release their resources.
986	 */
987#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
988	int ret;
989
990	ret = hda_dai_suspend(sof_to_bus(sdev));
991	if (ret < 0)
992		return ret;
993#endif
994
995	return 0;
996}