Linux Audio

Check our new training course

Loading...
v5.9
  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// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9//
 10// PCM Layer, interface between ALSA and IPC.
 11//
 12
 13#include <linux/pm_runtime.h>
 14#include <sound/pcm_params.h>
 15#include <sound/sof.h>
 16#include "sof-priv.h"
 17#include "sof-audio.h"
 18#include "ops.h"
 19#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
 20#include "compress.h"
 21#endif
 22
 23/* Create DMA buffer page table for DSP */
 24static int create_page_table(struct snd_soc_component *component,
 25			     struct snd_pcm_substream *substream,
 26			     unsigned char *dma_area, size_t size)
 27{
 28	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 
 
 
 29	struct snd_sof_pcm *spcm;
 30	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
 31	int stream = substream->stream;
 32
 33	spcm = snd_sof_find_spcm_dai(component, rtd);
 34	if (!spcm)
 35		return -EINVAL;
 36
 37	return snd_sof_create_page_table(component->dev, dmab,
 38		spcm->stream[stream].page_table.area, size);
 39}
 40
 41static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
 42			      const struct sof_ipc_pcm_params_reply *reply)
 43{
 44	struct snd_soc_component *scomp = spcm->scomp;
 45	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 46
 47	/* validate offset */
 48	int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
 49
 50	if (ret < 0)
 51		dev_err(scomp->dev, "error: got wrong reply for PCM %d\n",
 52			spcm->pcm.pcm_id);
 53
 54	return ret;
 55}
 56
 57/*
 58 * sof pcm period elapse work
 59 */
 60void snd_sof_pcm_period_elapsed_work(struct work_struct *work)
 61{
 62	struct snd_sof_pcm_stream *sps =
 63		container_of(work, struct snd_sof_pcm_stream,
 64			     period_elapsed_work);
 65
 66	snd_pcm_period_elapsed(sps->substream);
 67}
 68
 69/*
 70 * sof pcm period elapse, this could be called at irq thread context.
 71 */
 72void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
 73{
 74	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 75	struct snd_soc_component *component =
 76		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
 
 77	struct snd_sof_pcm *spcm;
 78
 79	spcm = snd_sof_find_spcm_dai(component, rtd);
 80	if (!spcm) {
 81		dev_err(component->dev,
 82			"error: period elapsed for unknown stream!\n");
 83		return;
 84	}
 85
 86	/*
 87	 * snd_pcm_period_elapsed() can be called in interrupt context
 88	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
 89	 * when the PCM is done draining or xrun happened, a STOP IPC will
 90	 * then be sent and this IPC will hit IPC timeout.
 91	 * To avoid sending IPC before the previous IPC is handled, we
 92	 * schedule delayed work here to call the snd_pcm_period_elapsed().
 93	 */
 94	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
 95}
 96EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
 97
 98static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
 99				struct snd_sof_dev *sdev,
100				struct snd_sof_pcm *spcm)
101{
102	struct sof_ipc_stream stream;
103	struct sof_ipc_reply reply;
104	int ret;
105
106	stream.hdr.size = sizeof(stream);
107	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
108	stream.comp_id = spcm->stream[substream->stream].comp_id;
109
110	/* send IPC to the DSP */
111	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
112				 sizeof(stream), &reply, sizeof(reply));
113	if (!ret)
114		spcm->prepared[substream->stream] = false;
115
116	return ret;
117}
118
119static int sof_pcm_hw_params(struct snd_soc_component *component,
120			     struct snd_pcm_substream *substream,
121			     struct snd_pcm_hw_params *params)
122{
123	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
124	struct snd_pcm_runtime *runtime = substream->runtime;
 
 
125	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
126	struct snd_sof_pcm *spcm;
127	struct sof_ipc_pcm_params pcm;
128	struct sof_ipc_pcm_params_reply ipc_params_reply;
129	int ret;
130
131	/* nothing to do for BE */
132	if (rtd->dai_link->no_pcm)
133		return 0;
134
135	spcm = snd_sof_find_spcm_dai(component, rtd);
136	if (!spcm)
137		return -EINVAL;
138
139	/*
140	 * Handle repeated calls to hw_params() without free_pcm() in
141	 * between. At least ALSA OSS emulation depends on this.
142	 */
143	if (spcm->prepared[substream->stream]) {
144		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
145		if (ret < 0)
146			return ret;
147	}
148
149	dev_dbg(component->dev, "pcm: hw params stream %d dir %d\n",
150		spcm->pcm.pcm_id, substream->stream);
151
152	memset(&pcm, 0, sizeof(pcm));
153
154	/* create compressed page table for audio firmware */
155	if (runtime->buffer_changed) {
156		ret = create_page_table(component, substream, runtime->dma_area,
 
 
 
 
 
 
 
 
 
 
 
 
157					runtime->dma_bytes);
158		if (ret < 0)
159			return ret;
160	}
161
162	/* number of pages should be rounded up */
163	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
164
165	/* set IPC PCM parameters */
166	pcm.hdr.size = sizeof(pcm);
167	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
168	pcm.comp_id = spcm->stream[substream->stream].comp_id;
169	pcm.params.hdr.size = sizeof(pcm.params);
170	pcm.params.buffer.phy_addr =
171		spcm->stream[substream->stream].page_table.addr;
172	pcm.params.buffer.size = runtime->dma_bytes;
173	pcm.params.direction = substream->stream;
174	pcm.params.sample_valid_bytes = params_width(params) >> 3;
175	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
176	pcm.params.rate = params_rate(params);
177	pcm.params.channels = params_channels(params);
178	pcm.params.host_period_bytes = params_period_bytes(params);
179
180	/* container size */
181	ret = snd_pcm_format_physical_width(params_format(params));
182	if (ret < 0)
183		return ret;
184	pcm.params.sample_container_bytes = ret >> 3;
185
186	/* format */
187	switch (params_format(params)) {
188	case SNDRV_PCM_FORMAT_S16:
189		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
190		break;
191	case SNDRV_PCM_FORMAT_S24:
192		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
193		break;
194	case SNDRV_PCM_FORMAT_S32:
195		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
196		break;
197	case SNDRV_PCM_FORMAT_FLOAT:
198		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
199		break;
200	default:
201		return -EINVAL;
202	}
203
204	/* firmware already configured host stream */
205	ret = snd_sof_pcm_platform_hw_params(sdev,
206					     substream,
207					     params,
208					     &pcm.params);
209	if (ret < 0) {
210		dev_err(component->dev, "error: platform hw params failed\n");
211		return ret;
212	}
213
214	dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
215
216	/* send IPC to the DSP */
217	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
218				 &ipc_params_reply, sizeof(ipc_params_reply));
219	if (ret < 0) {
220		dev_err(component->dev, "error: hw params ipc failed for stream %d\n",
221			pcm.params.stream_tag);
222		return ret;
223	}
224
225	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
226	if (ret < 0)
227		return ret;
228
229	spcm->prepared[substream->stream] = true;
230
231	/* save pcm hw_params */
232	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
233
234	return ret;
235}
236
237static int sof_pcm_hw_free(struct snd_soc_component *component,
238			   struct snd_pcm_substream *substream)
 
239{
240	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
242	struct snd_sof_pcm *spcm;
243	int ret, err = 0;
244
245	/* nothing to do for BE */
246	if (rtd->dai_link->no_pcm)
247		return 0;
248
249	spcm = snd_sof_find_spcm_dai(component, rtd);
250	if (!spcm)
251		return -EINVAL;
252
253	dev_dbg(component->dev, "pcm: free stream %d dir %d\n",
254		spcm->pcm.pcm_id, substream->stream);
255
256	if (spcm->prepared[substream->stream]) {
257		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
258		if (ret < 0)
259			err = ret;
260	}
261
 
 
262	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
263
264	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
265	if (ret < 0) {
266		dev_err(component->dev, "error: platform hw free failed\n");
267		err = ret;
268	}
269
270	return err;
271}
272
273static int sof_pcm_prepare(struct snd_soc_component *component,
274			   struct snd_pcm_substream *substream)
275{
276	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 
 
 
277	struct snd_sof_pcm *spcm;
278	int ret;
279
280	/* nothing to do for BE */
281	if (rtd->dai_link->no_pcm)
282		return 0;
283
284	spcm = snd_sof_find_spcm_dai(component, rtd);
285	if (!spcm)
286		return -EINVAL;
287
288	if (spcm->prepared[substream->stream])
289		return 0;
290
291	dev_dbg(component->dev, "pcm: prepare stream %d dir %d\n",
292		spcm->pcm.pcm_id, substream->stream);
293
294	/* set hw_params */
295	ret = sof_pcm_hw_params(component,
296				substream, &spcm->params[substream->stream]);
297	if (ret < 0) {
298		dev_err(component->dev,
299			"error: set pcm hw_params after resume\n");
300		return ret;
301	}
302
303	return 0;
304}
305
306/*
307 * FE dai link trigger actions are always executed in non-atomic context because
308 * they involve IPC's.
309 */
310static int sof_pcm_trigger(struct snd_soc_component *component,
311			   struct snd_pcm_substream *substream, int cmd)
312{
313	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 
 
314	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
315	struct snd_sof_pcm *spcm;
316	struct sof_ipc_stream stream;
317	struct sof_ipc_reply reply;
318	bool reset_hw_params = false;
319	bool ipc_first = false;
320	int ret;
321
322	/* nothing to do for BE */
323	if (rtd->dai_link->no_pcm)
324		return 0;
325
326	spcm = snd_sof_find_spcm_dai(component, rtd);
327	if (!spcm)
328		return -EINVAL;
329
330	dev_dbg(component->dev, "pcm: trigger stream %d dir %d cmd %d\n",
331		spcm->pcm.pcm_id, substream->stream, cmd);
332
333	stream.hdr.size = sizeof(stream);
334	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
335	stream.comp_id = spcm->stream[substream->stream].comp_id;
336
337	switch (cmd) {
338	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
339		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
340		ipc_first = true;
341		break;
342	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
343		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
344		break;
345	case SNDRV_PCM_TRIGGER_RESUME:
346		if (spcm->stream[substream->stream].suspend_ignored) {
347			/*
348			 * this case will be triggered when INFO_RESUME is
349			 * supported, no need to resume streams that remained
350			 * enabled in D0ix.
351			 */
352			spcm->stream[substream->stream].suspend_ignored = false;
353			return 0;
354		}
355
356		/* set up hw_params */
357		ret = sof_pcm_prepare(component, substream);
358		if (ret < 0) {
359			dev_err(component->dev,
360				"error: failed to set up hw_params upon resume\n");
361			return ret;
362		}
363
364		fallthrough;
365	case SNDRV_PCM_TRIGGER_START:
366		if (spcm->stream[substream->stream].suspend_ignored) {
367			/*
368			 * This case will be triggered when INFO_RESUME is
369			 * not supported, no need to re-start streams that
370			 * remained enabled in D0ix.
371			 */
372			spcm->stream[substream->stream].suspend_ignored = false;
373			return 0;
374		}
375		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
376		break;
377	case SNDRV_PCM_TRIGGER_SUSPEND:
378		if (sdev->system_suspend_target == SOF_SUSPEND_S0IX &&
379		    spcm->stream[substream->stream].d0i3_compatible) {
380			/*
381			 * trap the event, not sending trigger stop to
382			 * prevent the FW pipelines from being stopped,
383			 * and mark the flag to ignore the upcoming DAPM
384			 * PM events.
385			 */
386			spcm->stream[substream->stream].suspend_ignored = true;
387			return 0;
388		}
389		fallthrough;
390	case SNDRV_PCM_TRIGGER_STOP:
391		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
392		ipc_first = true;
393		reset_hw_params = true;
394		break;
395	default:
396		dev_err(component->dev, "error: unhandled trigger cmd %d\n",
397			cmd);
398		return -EINVAL;
399	}
400
401	/*
402	 * DMA and IPC sequence is different for start and stop. Need to send
403	 * STOP IPC before stop DMA
404	 */
405	if (!ipc_first)
406		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
407
408	/* send IPC to the DSP */
409	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
410				 sizeof(stream), &reply, sizeof(reply));
411
412	/* need to STOP DMA even if STOP IPC failed */
413	if (ipc_first)
414		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
415
416	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
417	if (!ret && reset_hw_params)
418		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
419
420	return ret;
421}
422
423static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component,
424					 struct snd_pcm_substream *substream)
425{
426	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 
 
427	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
428	struct snd_sof_pcm *spcm;
429	snd_pcm_uframes_t host, dai;
430
431	/* nothing to do for BE */
432	if (rtd->dai_link->no_pcm)
433		return 0;
434
435	/* use dsp ops pointer callback directly if set */
436	if (sof_ops(sdev)->pcm_pointer)
437		return sof_ops(sdev)->pcm_pointer(sdev, substream);
438
439	spcm = snd_sof_find_spcm_dai(component, rtd);
440	if (!spcm)
441		return -EINVAL;
442
443	/* read position from DSP */
444	host = bytes_to_frames(substream->runtime,
445			       spcm->stream[substream->stream].posn.host_posn);
446	dai = bytes_to_frames(substream->runtime,
447			      spcm->stream[substream->stream].posn.dai_posn);
448
449	dev_dbg(component->dev,
450		"PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
451		spcm->pcm.pcm_id, substream->stream, host, dai);
452
453	return host;
454}
455
456static int sof_pcm_open(struct snd_soc_component *component,
457			struct snd_pcm_substream *substream)
458{
459	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
460	struct snd_pcm_runtime *runtime = substream->runtime;
 
 
461	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
462	const struct snd_sof_dsp_ops *ops = sof_ops(sdev);
463	struct snd_sof_pcm *spcm;
464	struct snd_soc_tplg_stream_caps *caps;
465	int ret;
466
467	/* nothing to do for BE */
468	if (rtd->dai_link->no_pcm)
469		return 0;
470
471	spcm = snd_sof_find_spcm_dai(component, rtd);
472	if (!spcm)
473		return -EINVAL;
474
475	dev_dbg(component->dev, "pcm: open stream %d dir %d\n",
476		spcm->pcm.pcm_id, substream->stream);
477
 
 
478
479	caps = &spcm->pcm.caps[substream->stream];
480
481	/* set any runtime constraints based on topology */
482	snd_pcm_hw_constraint_step(substream->runtime, 0,
483				   SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
484				   le32_to_cpu(caps->period_size_min));
485	snd_pcm_hw_constraint_step(substream->runtime, 0,
486				   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
487				   le32_to_cpu(caps->period_size_min));
488
489	/* set runtime config */
490	runtime->hw.info = ops->hw_info; /* platform-specific */
491
 
 
 
492	runtime->hw.formats = le64_to_cpu(caps->formats);
493	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
494	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
495	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
496	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
497
498	/*
499	 * caps->buffer_size_min is not used since the
500	 * snd_pcm_hardware structure only defines buffer_bytes_max
501	 */
502	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
503
504	dev_dbg(component->dev, "period min %zd max %zd bytes\n",
505		runtime->hw.period_bytes_min,
506		runtime->hw.period_bytes_max);
507	dev_dbg(component->dev, "period count %d max %d\n",
508		runtime->hw.periods_min,
509		runtime->hw.periods_max);
510	dev_dbg(component->dev, "buffer max %zd bytes\n",
511		runtime->hw.buffer_bytes_max);
512
513	/* set wait time - TODO: come from topology */
514	substream->wait_time = 500;
515
516	spcm->stream[substream->stream].posn.host_posn = 0;
517	spcm->stream[substream->stream].posn.dai_posn = 0;
518	spcm->stream[substream->stream].substream = substream;
519	spcm->prepared[substream->stream] = false;
520
521	ret = snd_sof_pcm_platform_open(sdev, substream);
522	if (ret < 0)
523		dev_err(component->dev, "error: pcm open failed %d\n", ret);
524
525	return ret;
526}
527
528static int sof_pcm_close(struct snd_soc_component *component,
529			 struct snd_pcm_substream *substream)
530{
531	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
 
 
532	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
533	struct snd_sof_pcm *spcm;
534	int err;
535
536	/* nothing to do for BE */
537	if (rtd->dai_link->no_pcm)
538		return 0;
539
540	spcm = snd_sof_find_spcm_dai(component, rtd);
541	if (!spcm)
542		return -EINVAL;
543
544	dev_dbg(component->dev, "pcm: close stream %d dir %d\n",
545		spcm->pcm.pcm_id, substream->stream);
546
547	err = snd_sof_pcm_platform_close(sdev, substream);
548	if (err < 0) {
549		dev_err(component->dev, "error: pcm close failed %d\n",
550			err);
551		/*
552		 * keep going, no point in preventing the close
553		 * from happening
554		 */
555	}
556
557	return 0;
558}
559
 
 
 
 
 
 
 
 
 
 
 
 
560/*
561 * Pre-allocate playback/capture audio buffer pages.
562 * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
563 * snd_pcm_lib_preallocate_free_for_all() is called by the core.
564 */
565static int sof_pcm_new(struct snd_soc_component *component,
566		       struct snd_soc_pcm_runtime *rtd)
567{
 
 
568	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
569	struct snd_sof_pcm *spcm;
570	struct snd_pcm *pcm = rtd->pcm;
571	struct snd_soc_tplg_stream_caps *caps;
572	int stream = SNDRV_PCM_STREAM_PLAYBACK;
573
574	/* find SOF PCM for this RTD */
575	spcm = snd_sof_find_spcm_dai(component, rtd);
576	if (!spcm) {
577		dev_warn(component->dev, "warn: can't find PCM with DAI ID %d\n",
578			 rtd->dai_link->id);
579		return 0;
580	}
581
582	dev_dbg(component->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
583
584	/* do we need to pre-allocate playback audio buffer pages */
585	if (!spcm->pcm.playback)
586		goto capture;
587
588	caps = &spcm->pcm.caps[stream];
589
590	/* pre-allocate playback audio buffer pages */
591	dev_dbg(component->dev,
592		"spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
593		caps->name, caps->buffer_size_min, caps->buffer_size_max);
594
595	if (!pcm->streams[stream].substream) {
596		dev_err(component->dev, "error: NULL playback substream!\n");
597		return -EINVAL;
598	}
599
600	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
601				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
602				   0, le32_to_cpu(caps->buffer_size_max));
603capture:
604	stream = SNDRV_PCM_STREAM_CAPTURE;
605
606	/* do we need to pre-allocate capture audio buffer pages */
607	if (!spcm->pcm.capture)
608		return 0;
609
610	caps = &spcm->pcm.caps[stream];
611
612	/* pre-allocate capture audio buffer pages */
613	dev_dbg(component->dev,
614		"spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
615		caps->name, caps->buffer_size_min, caps->buffer_size_max);
616
617	if (!pcm->streams[stream].substream) {
618		dev_err(component->dev, "error: NULL capture substream!\n");
619		return -EINVAL;
620	}
621
622	snd_pcm_set_managed_buffer(pcm->streams[stream].substream,
623				   SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
624				   0, le32_to_cpu(caps->buffer_size_max));
625
626	return 0;
627}
628
629/* fixup the BE DAI link to match any values from topology */
630static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
631				  struct snd_pcm_hw_params *params)
632{
633	struct snd_interval *rate = hw_param_interval(params,
634			SNDRV_PCM_HW_PARAM_RATE);
635	struct snd_interval *channels = hw_param_interval(params,
636						SNDRV_PCM_HW_PARAM_CHANNELS);
637	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
638	struct snd_soc_component *component =
639		snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
 
640	struct snd_sof_dai *dai =
641		snd_sof_find_dai(component, (char *)rtd->dai_link->name);
642	struct snd_soc_dpcm *dpcm;
643
644	/* no topology exists for this BE, try a common configuration */
645	if (!dai) {
646		dev_warn(component->dev,
647			 "warning: no topology found for BE DAI %s config\n",
648			 rtd->dai_link->name);
649
650		/*  set 48k, stereo, 16bits by default */
651		rate->min = 48000;
652		rate->max = 48000;
653
654		channels->min = 2;
655		channels->max = 2;
656
657		snd_mask_none(fmt);
658		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
659
660		return 0;
661	}
662
663	/* read format from topology */
664	snd_mask_none(fmt);
665
666	switch (dai->comp_dai.config.frame_fmt) {
667	case SOF_IPC_FRAME_S16_LE:
668		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
669		break;
670	case SOF_IPC_FRAME_S24_4LE:
671		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
672		break;
673	case SOF_IPC_FRAME_S32_LE:
674		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
675		break;
676	default:
677		dev_err(component->dev, "error: No available DAI format!\n");
678		return -EINVAL;
679	}
680
681	/* read rate and channels from topology */
682	switch (dai->dai_config->type) {
683	case SOF_DAI_INTEL_SSP:
684		rate->min = dai->dai_config->ssp.fsync_rate;
685		rate->max = dai->dai_config->ssp.fsync_rate;
686		channels->min = dai->dai_config->ssp.tdm_slots;
687		channels->max = dai->dai_config->ssp.tdm_slots;
688
689		dev_dbg(component->dev,
690			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
691		dev_dbg(component->dev,
692			"channels_min: %d channels_max: %d\n",
693			channels->min, channels->max);
694
695		break;
696	case SOF_DAI_INTEL_DMIC:
697		/* DMIC only supports 16 or 32 bit formats */
698		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
699			dev_err(component->dev,
700				"error: invalid fmt %d for DAI type %d\n",
701				dai->comp_dai.config.frame_fmt,
702				dai->dai_config->type);
703		}
704		break;
705	case SOF_DAI_INTEL_HDA:
706		/*
707		 * HDaudio does not follow the default trigger
708		 * sequence due to firmware implementation
709		 */
710		for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
711			struct snd_soc_pcm_runtime *fe = dpcm->fe;
712
713			fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
714				SND_SOC_DPCM_TRIGGER_POST;
715		}
716		break;
717	case SOF_DAI_INTEL_ALH:
718		/* do nothing for ALH dai_link */
719		break;
720	case SOF_DAI_IMX_ESAI:
721		rate->min = dai->dai_config->esai.fsync_rate;
722		rate->max = dai->dai_config->esai.fsync_rate;
723		channels->min = dai->dai_config->esai.tdm_slots;
724		channels->max = dai->dai_config->esai.tdm_slots;
725
726		dev_dbg(component->dev,
727			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
728		dev_dbg(component->dev,
729			"channels_min: %d channels_max: %d\n",
730			channels->min, channels->max);
731		break;
732	case SOF_DAI_IMX_SAI:
733		rate->min = dai->dai_config->sai.fsync_rate;
734		rate->max = dai->dai_config->sai.fsync_rate;
735		channels->min = dai->dai_config->sai.tdm_slots;
736		channels->max = dai->dai_config->sai.tdm_slots;
737
738		dev_dbg(component->dev,
739			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
740		dev_dbg(component->dev,
741			"channels_min: %d channels_max: %d\n",
742			channels->min, channels->max);
743		break;
744	default:
745		dev_err(component->dev, "error: invalid DAI type %d\n",
746			dai->dai_config->type);
747		break;
748	}
749
750	return 0;
751}
752
753static int sof_pcm_probe(struct snd_soc_component *component)
754{
755	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
756	struct snd_sof_pdata *plat_data = sdev->pdata;
757	const char *tplg_filename;
758	int ret;
759
760	/* load the default topology */
761	sdev->component = component;
762
763	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
764				       "%s/%s",
765				       plat_data->tplg_filename_prefix,
766				       plat_data->tplg_filename);
767	if (!tplg_filename)
768		return -ENOMEM;
769
770	ret = snd_sof_load_topology(component, tplg_filename);
771	if (ret < 0) {
772		dev_err(component->dev, "error: failed to load DSP topology %d\n",
773			ret);
774		return ret;
775	}
776
 
 
 
 
 
 
 
 
777	return ret;
778}
779
780static void sof_pcm_remove(struct snd_soc_component *component)
781{
782	/* remove topology */
783	snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
784}
785
786void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
787{
788	struct snd_soc_component_driver *pd = &sdev->plat_drv;
789	struct snd_sof_pdata *plat_data = sdev->pdata;
790	const char *drv_name;
791
792	drv_name = plat_data->machine->drv_name;
793
794	pd->name = "sof-audio-component";
795	pd->probe = sof_pcm_probe;
796	pd->remove = sof_pcm_remove;
797	pd->open = sof_pcm_open;
798	pd->close = sof_pcm_close;
799	pd->hw_params = sof_pcm_hw_params;
800	pd->prepare = sof_pcm_prepare;
801	pd->hw_free = sof_pcm_hw_free;
802	pd->trigger = sof_pcm_trigger;
803	pd->pointer = sof_pcm_pointer;
804
805#if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
806	pd->compress_ops = &sof_compressed_ops;
807#endif
808#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
809	/* override cops when probe support is enabled */
810	pd->compress_ops = &sof_probe_compressed_ops;
811#endif
812	pd->pcm_construct = sof_pcm_new;
813	pd->ignore_machine = drv_name;
814	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
815	pd->be_pcm_base = SOF_BE_PCM_BASE;
816	pd->use_dai_pcm_id = true;
817	pd->topology_name_prefix = "sof";
818
819	 /* increment module refcount when a pcm is opened */
820	pd->module_get_upon_open = 1;
821}
v5.4
  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// Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9//
 10// PCM Layer, interface between ALSA and IPC.
 11//
 12
 13#include <linux/pm_runtime.h>
 14#include <sound/pcm_params.h>
 15#include <sound/sof.h>
 16#include "sof-priv.h"
 
 17#include "ops.h"
 18
 19#define DRV_NAME	"sof-audio-component"
 
 20
 21/* Create DMA buffer page table for DSP */
 22static int create_page_table(struct snd_pcm_substream *substream,
 
 23			     unsigned char *dma_area, size_t size)
 24{
 25	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 26	struct snd_soc_component *component =
 27		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 28	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
 29	struct snd_sof_pcm *spcm;
 30	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
 31	int stream = substream->stream;
 32
 33	spcm = snd_sof_find_spcm_dai(sdev, rtd);
 34	if (!spcm)
 35		return -EINVAL;
 36
 37	return snd_sof_create_page_table(sdev, dmab,
 38		spcm->stream[stream].page_table.area, size);
 39}
 40
 41static int sof_pcm_dsp_params(struct snd_sof_pcm *spcm, struct snd_pcm_substream *substream,
 42			      const struct sof_ipc_pcm_params_reply *reply)
 43{
 44	struct snd_sof_dev *sdev = spcm->sdev;
 
 
 45	/* validate offset */
 46	int ret = snd_sof_ipc_pcm_params(sdev, substream, reply);
 47
 48	if (ret < 0)
 49		dev_err(sdev->dev, "error: got wrong reply for PCM %d\n",
 50			spcm->pcm.pcm_id);
 51
 52	return ret;
 53}
 54
 55/*
 56 * sof pcm period elapse work
 57 */
 58static void sof_pcm_period_elapsed_work(struct work_struct *work)
 59{
 60	struct snd_sof_pcm_stream *sps =
 61		container_of(work, struct snd_sof_pcm_stream,
 62			     period_elapsed_work);
 63
 64	snd_pcm_period_elapsed(sps->substream);
 65}
 66
 67/*
 68 * sof pcm period elapse, this could be called at irq thread context.
 69 */
 70void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream)
 71{
 72	struct snd_soc_pcm_runtime *rtd = substream->private_data;
 73	struct snd_soc_component *component =
 74		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 75	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
 76	struct snd_sof_pcm *spcm;
 77
 78	spcm = snd_sof_find_spcm_dai(sdev, rtd);
 79	if (!spcm) {
 80		dev_err(sdev->dev,
 81			"error: period elapsed for unknown stream!\n");
 82		return;
 83	}
 84
 85	/*
 86	 * snd_pcm_period_elapsed() can be called in interrupt context
 87	 * before IRQ_HANDLED is returned. Inside snd_pcm_period_elapsed(),
 88	 * when the PCM is done draining or xrun happened, a STOP IPC will
 89	 * then be sent and this IPC will hit IPC timeout.
 90	 * To avoid sending IPC before the previous IPC is handled, we
 91	 * schedule delayed work here to call the snd_pcm_period_elapsed().
 92	 */
 93	schedule_work(&spcm->stream[substream->stream].period_elapsed_work);
 94}
 95EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
 96
 97/* this may get called several times by oss emulation */
 98static int sof_pcm_hw_params(struct snd_pcm_substream *substream,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 99			     struct snd_pcm_hw_params *params)
100{
101	struct snd_soc_pcm_runtime *rtd = substream->private_data;
102	struct snd_pcm_runtime *runtime = substream->runtime;
103	struct snd_soc_component *component =
104		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
105	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
106	struct snd_sof_pcm *spcm;
107	struct sof_ipc_pcm_params pcm;
108	struct sof_ipc_pcm_params_reply ipc_params_reply;
109	int ret;
110
111	/* nothing to do for BE */
112	if (rtd->dai_link->no_pcm)
113		return 0;
114
115	spcm = snd_sof_find_spcm_dai(sdev, rtd);
116	if (!spcm)
117		return -EINVAL;
118
119	dev_dbg(sdev->dev, "pcm: hw params stream %d dir %d\n",
 
 
 
 
 
 
 
 
 
 
120		spcm->pcm.pcm_id, substream->stream);
121
122	memset(&pcm, 0, sizeof(pcm));
123
124	/* allocate audio buffer pages */
125	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
126	if (ret < 0) {
127		dev_err(sdev->dev, "error: could not allocate %d bytes for PCM %d\n",
128			params_buffer_bytes(params), spcm->pcm.pcm_id);
129		return ret;
130	}
131	if (ret) {
132		/*
133		 * ret == 1 means the buffer is changed
134		 * create compressed page table for audio firmware
135		 * ret == 0 means the buffer is not changed
136		 * so no need to regenerate the page table
137		 */
138		ret = create_page_table(substream, runtime->dma_area,
139					runtime->dma_bytes);
140		if (ret < 0)
141			return ret;
142	}
143
144	/* number of pages should be rounded up */
145	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
146
147	/* set IPC PCM parameters */
148	pcm.hdr.size = sizeof(pcm);
149	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
150	pcm.comp_id = spcm->stream[substream->stream].comp_id;
151	pcm.params.hdr.size = sizeof(pcm.params);
152	pcm.params.buffer.phy_addr =
153		spcm->stream[substream->stream].page_table.addr;
154	pcm.params.buffer.size = runtime->dma_bytes;
155	pcm.params.direction = substream->stream;
156	pcm.params.sample_valid_bytes = params_width(params) >> 3;
157	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
158	pcm.params.rate = params_rate(params);
159	pcm.params.channels = params_channels(params);
160	pcm.params.host_period_bytes = params_period_bytes(params);
161
162	/* container size */
163	ret = snd_pcm_format_physical_width(params_format(params));
164	if (ret < 0)
165		return ret;
166	pcm.params.sample_container_bytes = ret >> 3;
167
168	/* format */
169	switch (params_format(params)) {
170	case SNDRV_PCM_FORMAT_S16:
171		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
172		break;
173	case SNDRV_PCM_FORMAT_S24:
174		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
175		break;
176	case SNDRV_PCM_FORMAT_S32:
177		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
178		break;
179	case SNDRV_PCM_FORMAT_FLOAT:
180		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
181		break;
182	default:
183		return -EINVAL;
184	}
185
186	/* firmware already configured host stream */
187	ret = snd_sof_pcm_platform_hw_params(sdev,
188					     substream,
189					     params,
190					     &pcm.params);
191	if (ret < 0) {
192		dev_err(sdev->dev, "error: platform hw params failed\n");
193		return ret;
194	}
195
196	dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);
197
198	/* send IPC to the DSP */
199	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
200				 &ipc_params_reply, sizeof(ipc_params_reply));
201	if (ret < 0) {
202		dev_err(sdev->dev, "error: hw params ipc failed for stream %d\n",
203			pcm.params.stream_tag);
204		return ret;
205	}
206
207	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
208	if (ret < 0)
209		return ret;
210
211	spcm->prepared[substream->stream] = true;
212
213	/* save pcm hw_params */
214	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
215
216	return ret;
217}
218
219static int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream,
220				struct snd_sof_dev *sdev,
221				struct snd_sof_pcm *spcm)
222{
223	struct sof_ipc_stream stream;
224	struct sof_ipc_reply reply;
225	int ret;
226
227	stream.hdr.size = sizeof(stream);
228	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
229	stream.comp_id = spcm->stream[substream->stream].comp_id;
230
231	/* send IPC to the DSP */
232	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
233				 sizeof(stream), &reply, sizeof(reply));
234	if (!ret)
235		spcm->prepared[substream->stream] = false;
236
237	return ret;
238}
239
240static int sof_pcm_hw_free(struct snd_pcm_substream *substream)
241{
242	struct snd_soc_pcm_runtime *rtd = substream->private_data;
243	struct snd_soc_component *component =
244		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
245	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
246	struct snd_sof_pcm *spcm;
247	int ret, err = 0;
248
249	/* nothing to do for BE */
250	if (rtd->dai_link->no_pcm)
251		return 0;
252
253	spcm = snd_sof_find_spcm_dai(sdev, rtd);
254	if (!spcm)
255		return -EINVAL;
256
257	dev_dbg(sdev->dev, "pcm: free stream %d dir %d\n", spcm->pcm.pcm_id,
258		substream->stream);
259
260	if (spcm->prepared[substream->stream]) {
261		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
262		if (ret < 0)
263			err = ret;
264	}
265
266	snd_pcm_lib_free_pages(substream);
267
268	cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
269
270	ret = snd_sof_pcm_platform_hw_free(sdev, substream);
271	if (ret < 0) {
272		dev_err(sdev->dev, "error: platform hw free failed\n");
273		err = ret;
274	}
275
276	return err;
277}
278
279static int sof_pcm_prepare(struct snd_pcm_substream *substream)
 
280{
281	struct snd_soc_pcm_runtime *rtd = substream->private_data;
282	struct snd_soc_component *component =
283		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
284	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
285	struct snd_sof_pcm *spcm;
286	int ret;
287
288	/* nothing to do for BE */
289	if (rtd->dai_link->no_pcm)
290		return 0;
291
292	spcm = snd_sof_find_spcm_dai(sdev, rtd);
293	if (!spcm)
294		return -EINVAL;
295
296	if (spcm->prepared[substream->stream])
297		return 0;
298
299	dev_dbg(sdev->dev, "pcm: prepare stream %d dir %d\n", spcm->pcm.pcm_id,
300		substream->stream);
301
302	/* set hw_params */
303	ret = sof_pcm_hw_params(substream, &spcm->params[substream->stream]);
 
304	if (ret < 0) {
305		dev_err(sdev->dev, "error: set pcm hw_params after resume\n");
 
306		return ret;
307	}
308
309	return 0;
310}
311
312/*
313 * FE dai link trigger actions are always executed in non-atomic context because
314 * they involve IPC's.
315 */
316static int sof_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 
317{
318	struct snd_soc_pcm_runtime *rtd = substream->private_data;
319	struct snd_soc_component *component =
320		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
321	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
322	struct snd_sof_pcm *spcm;
323	struct sof_ipc_stream stream;
324	struct sof_ipc_reply reply;
325	bool reset_hw_params = false;
326	bool ipc_first = false;
327	int ret;
328
329	/* nothing to do for BE */
330	if (rtd->dai_link->no_pcm)
331		return 0;
332
333	spcm = snd_sof_find_spcm_dai(sdev, rtd);
334	if (!spcm)
335		return -EINVAL;
336
337	dev_dbg(sdev->dev, "pcm: trigger stream %d dir %d cmd %d\n",
338		spcm->pcm.pcm_id, substream->stream, cmd);
339
340	stream.hdr.size = sizeof(stream);
341	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
342	stream.comp_id = spcm->stream[substream->stream].comp_id;
343
344	switch (cmd) {
345	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
346		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
347		ipc_first = true;
348		break;
349	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
350		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
351		break;
352	case SNDRV_PCM_TRIGGER_RESUME:
 
 
 
 
 
 
 
 
 
 
353		/* set up hw_params */
354		ret = sof_pcm_prepare(substream);
355		if (ret < 0) {
356			dev_err(sdev->dev,
357				"error: failed to set up hw_params upon resume\n");
358			return ret;
359		}
360
361		/* fallthrough */
362	case SNDRV_PCM_TRIGGER_START:
 
 
 
 
 
 
 
 
 
363		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
364		break;
365	case SNDRV_PCM_TRIGGER_SUSPEND:
 
 
 
 
 
 
 
 
 
 
 
 
366	case SNDRV_PCM_TRIGGER_STOP:
367		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
368		ipc_first = true;
369		reset_hw_params = true;
370		break;
371	default:
372		dev_err(sdev->dev, "error: unhandled trigger cmd %d\n", cmd);
 
373		return -EINVAL;
374	}
375
376	/*
377	 * DMA and IPC sequence is different for start and stop. Need to send
378	 * STOP IPC before stop DMA
379	 */
380	if (!ipc_first)
381		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
382
383	/* send IPC to the DSP */
384	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
385				 sizeof(stream), &reply, sizeof(reply));
386
387	/* need to STOP DMA even if STOP IPC failed */
388	if (ipc_first)
389		snd_sof_pcm_platform_trigger(sdev, substream, cmd);
390
391	/* free PCM if reset_hw_params is set and the STOP IPC is successful */
392	if (!ret && reset_hw_params)
393		ret = sof_pcm_dsp_pcm_free(substream, sdev, spcm);
394
395	return ret;
396}
397
398static snd_pcm_uframes_t sof_pcm_pointer(struct snd_pcm_substream *substream)
 
399{
400	struct snd_soc_pcm_runtime *rtd = substream->private_data;
401	struct snd_soc_component *component =
402		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
403	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
404	struct snd_sof_pcm *spcm;
405	snd_pcm_uframes_t host, dai;
406
407	/* nothing to do for BE */
408	if (rtd->dai_link->no_pcm)
409		return 0;
410
411	/* use dsp ops pointer callback directly if set */
412	if (sof_ops(sdev)->pcm_pointer)
413		return sof_ops(sdev)->pcm_pointer(sdev, substream);
414
415	spcm = snd_sof_find_spcm_dai(sdev, rtd);
416	if (!spcm)
417		return -EINVAL;
418
419	/* read position from DSP */
420	host = bytes_to_frames(substream->runtime,
421			       spcm->stream[substream->stream].posn.host_posn);
422	dai = bytes_to_frames(substream->runtime,
423			      spcm->stream[substream->stream].posn.dai_posn);
424
425	dev_dbg(sdev->dev, "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
 
426		spcm->pcm.pcm_id, substream->stream, host, dai);
427
428	return host;
429}
430
431static int sof_pcm_open(struct snd_pcm_substream *substream)
 
432{
433	struct snd_soc_pcm_runtime *rtd = substream->private_data;
434	struct snd_pcm_runtime *runtime = substream->runtime;
435	struct snd_soc_component *component =
436		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
437	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
 
438	struct snd_sof_pcm *spcm;
439	struct snd_soc_tplg_stream_caps *caps;
440	int ret;
441
442	/* nothing to do for BE */
443	if (rtd->dai_link->no_pcm)
444		return 0;
445
446	spcm = snd_sof_find_spcm_dai(sdev, rtd);
447	if (!spcm)
448		return -EINVAL;
449
450	dev_dbg(sdev->dev, "pcm: open stream %d dir %d\n", spcm->pcm.pcm_id,
451		substream->stream);
452
453	INIT_WORK(&spcm->stream[substream->stream].period_elapsed_work,
454		  sof_pcm_period_elapsed_work);
455
456	caps = &spcm->pcm.caps[substream->stream];
457
458	/* set any runtime constraints based on topology */
459	snd_pcm_hw_constraint_step(substream->runtime, 0,
460				   SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
461				   le32_to_cpu(caps->period_size_min));
462	snd_pcm_hw_constraint_step(substream->runtime, 0,
463				   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
464				   le32_to_cpu(caps->period_size_min));
465
466	/* set runtime config */
467	runtime->hw.info = SNDRV_PCM_INFO_MMAP |
468			  SNDRV_PCM_INFO_MMAP_VALID |
469			  SNDRV_PCM_INFO_INTERLEAVED |
470			  SNDRV_PCM_INFO_PAUSE |
471			  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
472	runtime->hw.formats = le64_to_cpu(caps->formats);
473	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
474	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
475	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
476	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
477
478	/*
479	 * caps->buffer_size_min is not used since the
480	 * snd_pcm_hardware structure only defines buffer_bytes_max
481	 */
482	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
483
484	dev_dbg(sdev->dev, "period min %zd max %zd bytes\n",
485		runtime->hw.period_bytes_min,
486		runtime->hw.period_bytes_max);
487	dev_dbg(sdev->dev, "period count %d max %d\n",
488		runtime->hw.periods_min,
489		runtime->hw.periods_max);
490	dev_dbg(sdev->dev, "buffer max %zd bytes\n",
491		runtime->hw.buffer_bytes_max);
492
493	/* set wait time - TODO: come from topology */
494	substream->wait_time = 500;
495
496	spcm->stream[substream->stream].posn.host_posn = 0;
497	spcm->stream[substream->stream].posn.dai_posn = 0;
498	spcm->stream[substream->stream].substream = substream;
499	spcm->prepared[substream->stream] = false;
500
501	ret = snd_sof_pcm_platform_open(sdev, substream);
502	if (ret < 0)
503		dev_err(sdev->dev, "error: pcm open failed %d\n", ret);
504
505	return ret;
506}
507
508static int sof_pcm_close(struct snd_pcm_substream *substream)
 
509{
510	struct snd_soc_pcm_runtime *rtd = substream->private_data;
511	struct snd_soc_component *component =
512		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
513	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
514	struct snd_sof_pcm *spcm;
515	int err;
516
517	/* nothing to do for BE */
518	if (rtd->dai_link->no_pcm)
519		return 0;
520
521	spcm = snd_sof_find_spcm_dai(sdev, rtd);
522	if (!spcm)
523		return -EINVAL;
524
525	dev_dbg(sdev->dev, "pcm: close stream %d dir %d\n", spcm->pcm.pcm_id,
526		substream->stream);
527
528	err = snd_sof_pcm_platform_close(sdev, substream);
529	if (err < 0) {
530		dev_err(sdev->dev, "error: pcm close failed %d\n",
531			err);
532		/*
533		 * keep going, no point in preventing the close
534		 * from happening
535		 */
536	}
537
538	return 0;
539}
540
541static struct snd_pcm_ops sof_pcm_ops = {
542	.open		= sof_pcm_open,
543	.close		= sof_pcm_close,
544	.ioctl		= snd_pcm_lib_ioctl,
545	.hw_params	= sof_pcm_hw_params,
546	.prepare	= sof_pcm_prepare,
547	.hw_free	= sof_pcm_hw_free,
548	.trigger	= sof_pcm_trigger,
549	.pointer	= sof_pcm_pointer,
550	.page		= snd_pcm_sgbuf_ops_page,
551};
552
553/*
554 * Pre-allocate playback/capture audio buffer pages.
555 * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
556 * snd_pcm_lib_preallocate_free_for_all() is called by the core.
557 */
558static int sof_pcm_new(struct snd_soc_pcm_runtime *rtd)
 
559{
560	struct snd_soc_component *component =
561		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
562	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
563	struct snd_sof_pcm *spcm;
564	struct snd_pcm *pcm = rtd->pcm;
565	struct snd_soc_tplg_stream_caps *caps;
566	int stream = SNDRV_PCM_STREAM_PLAYBACK;
567
568	/* find SOF PCM for this RTD */
569	spcm = snd_sof_find_spcm_dai(sdev, rtd);
570	if (!spcm) {
571		dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
572			 rtd->dai_link->id);
573		return 0;
574	}
575
576	dev_dbg(sdev->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
577
578	/* do we need to pre-allocate playback audio buffer pages */
579	if (!spcm->pcm.playback)
580		goto capture;
581
582	caps = &spcm->pcm.caps[stream];
583
584	/* pre-allocate playback audio buffer pages */
585	dev_dbg(sdev->dev, "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
 
586		caps->name, caps->buffer_size_min, caps->buffer_size_max);
587
588	snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
589				      SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
590				      le32_to_cpu(caps->buffer_size_min),
591				      le32_to_cpu(caps->buffer_size_max));
 
 
 
 
592capture:
593	stream = SNDRV_PCM_STREAM_CAPTURE;
594
595	/* do we need to pre-allocate capture audio buffer pages */
596	if (!spcm->pcm.capture)
597		return 0;
598
599	caps = &spcm->pcm.caps[stream];
600
601	/* pre-allocate capture audio buffer pages */
602	dev_dbg(sdev->dev, "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
 
603		caps->name, caps->buffer_size_min, caps->buffer_size_max);
604
605	snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
606				      SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
607				      le32_to_cpu(caps->buffer_size_min),
608				      le32_to_cpu(caps->buffer_size_max));
 
 
 
 
609
610	return 0;
611}
612
613/* fixup the BE DAI link to match any values from topology */
614static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
615				  struct snd_pcm_hw_params *params)
616{
617	struct snd_interval *rate = hw_param_interval(params,
618			SNDRV_PCM_HW_PARAM_RATE);
619	struct snd_interval *channels = hw_param_interval(params,
620						SNDRV_PCM_HW_PARAM_CHANNELS);
621	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
622	struct snd_soc_component *component =
623		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
624	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
625	struct snd_sof_dai *dai =
626		snd_sof_find_dai(sdev, (char *)rtd->dai_link->name);
 
627
628	/* no topology exists for this BE, try a common configuration */
629	if (!dai) {
630		dev_warn(sdev->dev, "warning: no topology found for BE DAI %s config\n",
 
631			 rtd->dai_link->name);
632
633		/*  set 48k, stereo, 16bits by default */
634		rate->min = 48000;
635		rate->max = 48000;
636
637		channels->min = 2;
638		channels->max = 2;
639
640		snd_mask_none(fmt);
641		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
642
643		return 0;
644	}
645
646	/* read format from topology */
647	snd_mask_none(fmt);
648
649	switch (dai->comp_dai.config.frame_fmt) {
650	case SOF_IPC_FRAME_S16_LE:
651		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
652		break;
653	case SOF_IPC_FRAME_S24_4LE:
654		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
655		break;
656	case SOF_IPC_FRAME_S32_LE:
657		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
658		break;
659	default:
660		dev_err(sdev->dev, "error: No available DAI format!\n");
661		return -EINVAL;
662	}
663
664	/* read rate and channels from topology */
665	switch (dai->dai_config->type) {
666	case SOF_DAI_INTEL_SSP:
667		rate->min = dai->dai_config->ssp.fsync_rate;
668		rate->max = dai->dai_config->ssp.fsync_rate;
669		channels->min = dai->dai_config->ssp.tdm_slots;
670		channels->max = dai->dai_config->ssp.tdm_slots;
671
672		dev_dbg(sdev->dev,
673			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
674		dev_dbg(sdev->dev,
675			"channels_min: %d channels_max: %d\n",
676			channels->min, channels->max);
677
678		break;
679	case SOF_DAI_INTEL_DMIC:
680		/* DMIC only supports 16 or 32 bit formats */
681		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
682			dev_err(sdev->dev,
683				"error: invalid fmt %d for DAI type %d\n",
684				dai->comp_dai.config.frame_fmt,
685				dai->dai_config->type);
686		}
687		break;
688	case SOF_DAI_INTEL_HDA:
689		/* do nothing for HDA dai_link */
 
 
 
 
 
 
 
 
 
690		break;
691	case SOF_DAI_INTEL_ALH:
692		/* do nothing for ALH dai_link */
693		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
694	default:
695		dev_err(sdev->dev, "error: invalid DAI type %d\n",
696			dai->dai_config->type);
697		break;
698	}
699
700	return 0;
701}
702
703static int sof_pcm_probe(struct snd_soc_component *component)
704{
705	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
706	struct snd_sof_pdata *plat_data = sdev->pdata;
707	const char *tplg_filename;
708	int ret;
709
710	/* load the default topology */
711	sdev->component = component;
712
713	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
714				       "%s/%s",
715				       plat_data->tplg_filename_prefix,
716				       plat_data->tplg_filename);
717	if (!tplg_filename)
718		return -ENOMEM;
719
720	ret = snd_sof_load_topology(sdev, tplg_filename);
721	if (ret < 0) {
722		dev_err(sdev->dev, "error: failed to load DSP topology %d\n",
723			ret);
724		return ret;
725	}
726
727	/*
728	 * Some platforms in SOF, ex: BYT, may not have their platform PM
729	 * callbacks set. Increment the usage count so as to
730	 * prevent the device from entering runtime suspend.
731	 */
732	if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
733		pm_runtime_get_noresume(sdev->dev);
734
735	return ret;
736}
737
738static void sof_pcm_remove(struct snd_soc_component *component)
739{
740	/* remove topology */
741	snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
742}
743
744void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
745{
746	struct snd_soc_component_driver *pd = &sdev->plat_drv;
747	struct snd_sof_pdata *plat_data = sdev->pdata;
748	const char *drv_name;
749
750	drv_name = plat_data->machine->drv_name;
751
752	pd->name = "sof-audio-component";
753	pd->probe = sof_pcm_probe;
754	pd->remove = sof_pcm_remove;
755	pd->ops	= &sof_pcm_ops;
 
 
 
 
 
 
 
756#if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
757	pd->compr_ops = &sof_compressed_ops;
 
 
 
 
758#endif
759	pd->pcm_new = sof_pcm_new;
760	pd->ignore_machine = drv_name;
761	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
762	pd->be_pcm_base = SOF_BE_PCM_BASE;
763	pd->use_dai_pcm_id = true;
764	pd->topology_name_prefix = "sof";
765
766	 /* increment module refcount when a pcm is opened */
767	pd->module_get_upon_open = 1;
768}