xref: /openbmc/linux/sound/soc/sof/pcm.c (revision e657c18a)
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 */
22 static 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 
41 static 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 /* this may get called several times by oss emulation */
56 static int sof_pcm_hw_params(struct snd_pcm_substream *substream,
57 			     struct snd_pcm_hw_params *params)
58 {
59 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
60 	struct snd_pcm_runtime *runtime = substream->runtime;
61 	struct snd_soc_component *component =
62 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
63 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
64 	struct snd_sof_pcm *spcm;
65 	struct sof_ipc_pcm_params pcm;
66 	struct sof_ipc_pcm_params_reply ipc_params_reply;
67 	int ret;
68 
69 	/* nothing to do for BE */
70 	if (rtd->dai_link->no_pcm)
71 		return 0;
72 
73 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
74 	if (!spcm)
75 		return -EINVAL;
76 
77 	dev_dbg(sdev->dev, "pcm: hw params stream %d dir %d\n",
78 		spcm->pcm.pcm_id, substream->stream);
79 
80 	memset(&pcm, 0, sizeof(pcm));
81 
82 	/* allocate audio buffer pages */
83 	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
84 	if (ret < 0) {
85 		dev_err(sdev->dev, "error: could not allocate %d bytes for PCM %d\n",
86 			params_buffer_bytes(params), spcm->pcm.pcm_id);
87 		return ret;
88 	}
89 	if (ret) {
90 		/*
91 		 * ret == 1 means the buffer is changed
92 		 * create compressed page table for audio firmware
93 		 * ret == 0 means the buffer is not changed
94 		 * so no need to regenerate the page table
95 		 */
96 		ret = create_page_table(substream, runtime->dma_area,
97 					runtime->dma_bytes);
98 		if (ret < 0)
99 			return ret;
100 	}
101 
102 	/* number of pages should be rounded up */
103 	pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
104 
105 	/* set IPC PCM parameters */
106 	pcm.hdr.size = sizeof(pcm);
107 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
108 	pcm.comp_id = spcm->stream[substream->stream].comp_id;
109 	pcm.params.hdr.size = sizeof(pcm.params);
110 	pcm.params.buffer.phy_addr =
111 		spcm->stream[substream->stream].page_table.addr;
112 	pcm.params.buffer.size = runtime->dma_bytes;
113 	pcm.params.direction = substream->stream;
114 	pcm.params.sample_valid_bytes = params_width(params) >> 3;
115 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
116 	pcm.params.rate = params_rate(params);
117 	pcm.params.channels = params_channels(params);
118 	pcm.params.host_period_bytes = params_period_bytes(params);
119 
120 	/* container size */
121 	ret = snd_pcm_format_physical_width(params_format(params));
122 	if (ret < 0)
123 		return ret;
124 	pcm.params.sample_container_bytes = ret >> 3;
125 
126 	/* format */
127 	switch (params_format(params)) {
128 	case SNDRV_PCM_FORMAT_S16:
129 		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
130 		break;
131 	case SNDRV_PCM_FORMAT_S24:
132 		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
133 		break;
134 	case SNDRV_PCM_FORMAT_S32:
135 		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
136 		break;
137 	case SNDRV_PCM_FORMAT_FLOAT:
138 		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
139 		break;
140 	default:
141 		return -EINVAL;
142 	}
143 
144 	/* firmware already configured host stream */
145 	ret = snd_sof_pcm_platform_hw_params(sdev,
146 					     substream,
147 					     params,
148 					     &pcm.params);
149 	if (ret < 0) {
150 		dev_err(sdev->dev, "error: platform hw params failed\n");
151 		return ret;
152 	}
153 
154 	dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);
155 
156 	/* send IPC to the DSP */
157 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
158 				 &ipc_params_reply, sizeof(ipc_params_reply));
159 	if (ret < 0) {
160 		dev_err(sdev->dev, "error: hw params ipc failed for stream %d\n",
161 			pcm.params.stream_tag);
162 		return ret;
163 	}
164 
165 	ret = sof_pcm_dsp_params(spcm, substream, &ipc_params_reply);
166 	if (ret < 0)
167 		return ret;
168 
169 	/* save pcm hw_params */
170 	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
171 
172 	return ret;
173 }
174 
175 static int sof_pcm_hw_free(struct snd_pcm_substream *substream)
176 {
177 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 	struct snd_soc_component *component =
179 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
180 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
181 	struct snd_sof_pcm *spcm;
182 	struct sof_ipc_stream stream;
183 	struct sof_ipc_reply reply;
184 	int ret;
185 
186 	/* nothing to do for BE */
187 	if (rtd->dai_link->no_pcm)
188 		return 0;
189 
190 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
191 	if (!spcm)
192 		return -EINVAL;
193 
194 	dev_dbg(sdev->dev, "pcm: free stream %d dir %d\n", spcm->pcm.pcm_id,
195 		substream->stream);
196 
197 	stream.hdr.size = sizeof(stream);
198 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
199 	stream.comp_id = spcm->stream[substream->stream].comp_id;
200 
201 	/* send IPC to the DSP */
202 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
203 				 sizeof(stream), &reply, sizeof(reply));
204 
205 	snd_pcm_lib_free_pages(substream);
206 	return ret;
207 }
208 
209 static int sof_pcm_prepare(struct snd_pcm_substream *substream)
210 {
211 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
212 	struct snd_soc_component *component =
213 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
214 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
215 	struct snd_sof_pcm *spcm;
216 	int ret;
217 
218 	/* nothing to do for BE */
219 	if (rtd->dai_link->no_pcm)
220 		return 0;
221 
222 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
223 	if (!spcm)
224 		return -EINVAL;
225 
226 	/*
227 	 * check if hw_params needs to be set-up again.
228 	 * This is only needed when resuming from system sleep.
229 	 */
230 	if (!spcm->hw_params_upon_resume[substream->stream])
231 		return 0;
232 
233 	dev_dbg(sdev->dev, "pcm: prepare stream %d dir %d\n", spcm->pcm.pcm_id,
234 		substream->stream);
235 
236 	/* set hw_params */
237 	ret = sof_pcm_hw_params(substream, &spcm->params[substream->stream]);
238 	if (ret < 0) {
239 		dev_err(sdev->dev, "error: set pcm hw_params after resume\n");
240 		return ret;
241 	}
242 
243 	return 0;
244 }
245 
246 /*
247  * FE dai link trigger actions are always executed in non-atomic context because
248  * they involve IPC's.
249  */
250 static int sof_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
251 {
252 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
253 	struct snd_soc_component *component =
254 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
255 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
256 	struct snd_sof_pcm *spcm;
257 	struct sof_ipc_stream stream;
258 	struct sof_ipc_reply reply;
259 	int ret;
260 
261 	/* nothing to do for BE */
262 	if (rtd->dai_link->no_pcm)
263 		return 0;
264 
265 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
266 	if (!spcm)
267 		return -EINVAL;
268 
269 	dev_dbg(sdev->dev, "pcm: trigger stream %d dir %d cmd %d\n",
270 		spcm->pcm.pcm_id, substream->stream, cmd);
271 
272 	stream.hdr.size = sizeof(stream);
273 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
274 	stream.comp_id = spcm->stream[substream->stream].comp_id;
275 
276 	switch (cmd) {
277 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
278 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
279 		break;
280 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
281 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
282 		break;
283 	case SNDRV_PCM_TRIGGER_RESUME:
284 		/* set up hw_params */
285 		ret = sof_pcm_prepare(substream);
286 		if (ret < 0) {
287 			dev_err(sdev->dev,
288 				"error: failed to set up hw_params upon resume\n");
289 			return ret;
290 		}
291 
292 		/* fallthrough */
293 	case SNDRV_PCM_TRIGGER_START:
294 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
295 		break;
296 	case SNDRV_PCM_TRIGGER_SUSPEND:
297 	case SNDRV_PCM_TRIGGER_STOP:
298 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
299 		break;
300 	default:
301 		dev_err(sdev->dev, "error: unhandled trigger cmd %d\n", cmd);
302 		return -EINVAL;
303 	}
304 
305 	snd_sof_pcm_platform_trigger(sdev, substream, cmd);
306 
307 	/* send IPC to the DSP */
308 	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
309 				 sizeof(stream), &reply, sizeof(reply));
310 
311 	if (ret < 0 || cmd != SNDRV_PCM_TRIGGER_SUSPEND)
312 		return ret;
313 
314 	/*
315 	 * The hw_free op is usually called when the pcm stream is closed.
316 	 * Since the stream is not closed during suspend, the DSP needs to be
317 	 * notified explicitly to free pcm to prevent errors upon resume.
318 	 */
319 	stream.hdr.size = sizeof(stream);
320 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
321 	stream.comp_id = spcm->stream[substream->stream].comp_id;
322 
323 	/* send IPC to the DSP */
324 	return sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
325 				  sizeof(stream), &reply, sizeof(reply));
326 }
327 
328 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_pcm_substream *substream)
329 {
330 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
331 	struct snd_soc_component *component =
332 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
333 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
334 	struct snd_sof_pcm *spcm;
335 	snd_pcm_uframes_t host, dai;
336 
337 	/* nothing to do for BE */
338 	if (rtd->dai_link->no_pcm)
339 		return 0;
340 
341 	/* use dsp ops pointer callback directly if set */
342 	if (sof_ops(sdev)->pcm_pointer)
343 		return sof_ops(sdev)->pcm_pointer(sdev, substream);
344 
345 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
346 	if (!spcm)
347 		return -EINVAL;
348 
349 	/* read position from DSP */
350 	host = bytes_to_frames(substream->runtime,
351 			       spcm->stream[substream->stream].posn.host_posn);
352 	dai = bytes_to_frames(substream->runtime,
353 			      spcm->stream[substream->stream].posn.dai_posn);
354 
355 	dev_dbg(sdev->dev, "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
356 		spcm->pcm.pcm_id, substream->stream, host, dai);
357 
358 	return host;
359 }
360 
361 static int sof_pcm_open(struct snd_pcm_substream *substream)
362 {
363 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
364 	struct snd_pcm_runtime *runtime = substream->runtime;
365 	struct snd_soc_component *component =
366 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
367 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
368 	struct snd_sof_pcm *spcm;
369 	struct snd_soc_tplg_stream_caps *caps;
370 	int ret;
371 	int err;
372 
373 	/* nothing to do for BE */
374 	if (rtd->dai_link->no_pcm)
375 		return 0;
376 
377 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
378 	if (!spcm)
379 		return -EINVAL;
380 
381 	dev_dbg(sdev->dev, "pcm: open stream %d dir %d\n", spcm->pcm.pcm_id,
382 		substream->stream);
383 
384 	/* clear hw_params_upon_resume flag */
385 	spcm->hw_params_upon_resume[substream->stream] = 0;
386 
387 	caps = &spcm->pcm.caps[substream->stream];
388 
389 	ret = pm_runtime_get_sync(sdev->dev);
390 	if (ret < 0) {
391 		dev_err(sdev->dev, "error: pcm open failed to resume %d\n",
392 			ret);
393 		pm_runtime_put_noidle(sdev->dev);
394 		return ret;
395 	}
396 
397 	/* set any runtime constraints based on topology */
398 	snd_pcm_hw_constraint_step(substream->runtime, 0,
399 				   SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
400 				   le32_to_cpu(caps->period_size_min));
401 	snd_pcm_hw_constraint_step(substream->runtime, 0,
402 				   SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
403 				   le32_to_cpu(caps->period_size_min));
404 
405 	/* set runtime config */
406 	runtime->hw.info = SNDRV_PCM_INFO_MMAP |
407 			  SNDRV_PCM_INFO_MMAP_VALID |
408 			  SNDRV_PCM_INFO_INTERLEAVED |
409 			  SNDRV_PCM_INFO_PAUSE |
410 			  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
411 	runtime->hw.formats = le64_to_cpu(caps->formats);
412 	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
413 	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
414 	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
415 	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
416 
417 	/*
418 	 * caps->buffer_size_min is not used since the
419 	 * snd_pcm_hardware structure only defines buffer_bytes_max
420 	 */
421 	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
422 
423 	dev_dbg(sdev->dev, "period min %zd max %zd bytes\n",
424 		runtime->hw.period_bytes_min,
425 		runtime->hw.period_bytes_max);
426 	dev_dbg(sdev->dev, "period count %d max %d\n",
427 		runtime->hw.periods_min,
428 		runtime->hw.periods_max);
429 	dev_dbg(sdev->dev, "buffer max %zd bytes\n",
430 		runtime->hw.buffer_bytes_max);
431 
432 	/* set wait time - TODO: come from topology */
433 	substream->wait_time = 500;
434 
435 	spcm->stream[substream->stream].posn.host_posn = 0;
436 	spcm->stream[substream->stream].posn.dai_posn = 0;
437 	spcm->stream[substream->stream].substream = substream;
438 
439 	ret = snd_sof_pcm_platform_open(sdev, substream);
440 	if (ret < 0) {
441 		dev_err(sdev->dev, "error: pcm open failed %d\n",
442 			ret);
443 
444 		pm_runtime_mark_last_busy(sdev->dev);
445 
446 		err = pm_runtime_put_autosuspend(sdev->dev);
447 		if (err < 0)
448 			dev_err(sdev->dev, "error: pcm close failed to idle %d\n",
449 				err);
450 	}
451 
452 	return ret;
453 }
454 
455 static int sof_pcm_close(struct snd_pcm_substream *substream)
456 {
457 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
458 	struct snd_soc_component *component =
459 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
460 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
461 	struct snd_sof_pcm *spcm;
462 	int err;
463 
464 	/* nothing to do for BE */
465 	if (rtd->dai_link->no_pcm)
466 		return 0;
467 
468 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
469 	if (!spcm)
470 		return -EINVAL;
471 
472 	dev_dbg(sdev->dev, "pcm: close stream %d dir %d\n", spcm->pcm.pcm_id,
473 		substream->stream);
474 
475 	err = snd_sof_pcm_platform_close(sdev, substream);
476 	if (err < 0) {
477 		dev_err(sdev->dev, "error: pcm close failed %d\n",
478 			err);
479 		/*
480 		 * keep going, no point in preventing the close
481 		 * from happening
482 		 */
483 	}
484 
485 	pm_runtime_mark_last_busy(sdev->dev);
486 
487 	err = pm_runtime_put_autosuspend(sdev->dev);
488 	if (err < 0)
489 		dev_err(sdev->dev, "error: pcm close failed to idle %d\n",
490 			err);
491 
492 	return 0;
493 }
494 
495 static struct snd_pcm_ops sof_pcm_ops = {
496 	.open		= sof_pcm_open,
497 	.close		= sof_pcm_close,
498 	.ioctl		= snd_pcm_lib_ioctl,
499 	.hw_params	= sof_pcm_hw_params,
500 	.prepare	= sof_pcm_prepare,
501 	.hw_free	= sof_pcm_hw_free,
502 	.trigger	= sof_pcm_trigger,
503 	.pointer	= sof_pcm_pointer,
504 	.page		= snd_pcm_sgbuf_ops_page,
505 };
506 
507 /*
508  * Pre-allocate playback/capture audio buffer pages.
509  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
510  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
511  */
512 static int sof_pcm_new(struct snd_soc_pcm_runtime *rtd)
513 {
514 	struct snd_soc_component *component =
515 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
516 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
517 	struct snd_sof_pcm *spcm;
518 	struct snd_pcm *pcm = rtd->pcm;
519 	struct snd_soc_tplg_stream_caps *caps;
520 	int stream = SNDRV_PCM_STREAM_PLAYBACK;
521 
522 	/* find SOF PCM for this RTD */
523 	spcm = snd_sof_find_spcm_dai(sdev, rtd);
524 	if (!spcm) {
525 		dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
526 			 rtd->dai_link->id);
527 		return 0;
528 	}
529 
530 	dev_dbg(sdev->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
531 
532 	/* do we need to pre-allocate playback audio buffer pages */
533 	if (!spcm->pcm.playback)
534 		goto capture;
535 
536 	caps = &spcm->pcm.caps[stream];
537 
538 	/* pre-allocate playback audio buffer pages */
539 	dev_dbg(sdev->dev, "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
540 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
541 
542 	snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
543 				      SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
544 				      le32_to_cpu(caps->buffer_size_min),
545 				      le32_to_cpu(caps->buffer_size_max));
546 capture:
547 	stream = SNDRV_PCM_STREAM_CAPTURE;
548 
549 	/* do we need to pre-allocate capture audio buffer pages */
550 	if (!spcm->pcm.capture)
551 		return 0;
552 
553 	caps = &spcm->pcm.caps[stream];
554 
555 	/* pre-allocate capture audio buffer pages */
556 	dev_dbg(sdev->dev, "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
557 		caps->name, caps->buffer_size_min, caps->buffer_size_max);
558 
559 	snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
560 				      SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
561 				      le32_to_cpu(caps->buffer_size_min),
562 				      le32_to_cpu(caps->buffer_size_max));
563 
564 	return 0;
565 }
566 
567 /* fixup the BE DAI link to match any values from topology */
568 static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
569 				  struct snd_pcm_hw_params *params)
570 {
571 	struct snd_interval *rate = hw_param_interval(params,
572 			SNDRV_PCM_HW_PARAM_RATE);
573 	struct snd_interval *channels = hw_param_interval(params,
574 						SNDRV_PCM_HW_PARAM_CHANNELS);
575 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
576 	struct snd_soc_component *component =
577 		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
578 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
579 	struct snd_sof_dai *dai =
580 		snd_sof_find_dai(sdev, (char *)rtd->dai_link->name);
581 
582 	/* no topology exists for this BE, try a common configuration */
583 	if (!dai) {
584 		dev_warn(sdev->dev, "warning: no topology found for BE DAI %s config\n",
585 			 rtd->dai_link->name);
586 
587 		/*  set 48k, stereo, 16bits by default */
588 		rate->min = 48000;
589 		rate->max = 48000;
590 
591 		channels->min = 2;
592 		channels->max = 2;
593 
594 		snd_mask_none(fmt);
595 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
596 
597 		return 0;
598 	}
599 
600 	/* read format from topology */
601 	snd_mask_none(fmt);
602 
603 	switch (dai->comp_dai.config.frame_fmt) {
604 	case SOF_IPC_FRAME_S16_LE:
605 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
606 		break;
607 	case SOF_IPC_FRAME_S24_4LE:
608 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
609 		break;
610 	case SOF_IPC_FRAME_S32_LE:
611 		snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
612 		break;
613 	default:
614 		dev_err(sdev->dev, "error: No available DAI format!\n");
615 		return -EINVAL;
616 	}
617 
618 	/* read rate and channels from topology */
619 	switch (dai->dai_config->type) {
620 	case SOF_DAI_INTEL_SSP:
621 		rate->min = dai->dai_config->ssp.fsync_rate;
622 		rate->max = dai->dai_config->ssp.fsync_rate;
623 		channels->min = dai->dai_config->ssp.tdm_slots;
624 		channels->max = dai->dai_config->ssp.tdm_slots;
625 
626 		dev_dbg(sdev->dev,
627 			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
628 		dev_dbg(sdev->dev,
629 			"channels_min: %d channels_max: %d\n",
630 			channels->min, channels->max);
631 
632 		break;
633 	case SOF_DAI_INTEL_DMIC:
634 		/* DMIC only supports 16 or 32 bit formats */
635 		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
636 			dev_err(sdev->dev,
637 				"error: invalid fmt %d for DAI type %d\n",
638 				dai->comp_dai.config.frame_fmt,
639 				dai->dai_config->type);
640 		}
641 		break;
642 	case SOF_DAI_INTEL_HDA:
643 		/* do nothing for HDA dai_link */
644 		break;
645 	default:
646 		dev_err(sdev->dev, "error: invalid DAI type %d\n",
647 			dai->dai_config->type);
648 		break;
649 	}
650 
651 	return 0;
652 }
653 
654 static int sof_pcm_probe(struct snd_soc_component *component)
655 {
656 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
657 	struct snd_sof_pdata *plat_data = sdev->pdata;
658 	const char *tplg_filename;
659 	int ret;
660 
661 	/* load the default topology */
662 	sdev->component = component;
663 
664 	tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
665 				       "%s/%s",
666 				       plat_data->tplg_filename_prefix,
667 				       plat_data->tplg_filename);
668 	if (!tplg_filename)
669 		return -ENOMEM;
670 
671 	ret = snd_sof_load_topology(sdev, tplg_filename);
672 	if (ret < 0) {
673 		dev_err(sdev->dev, "error: failed to load DSP topology %d\n",
674 			ret);
675 		return ret;
676 	}
677 
678 	/*
679 	 * Some platforms in SOF, ex: BYT, may not have their platform PM
680 	 * callbacks set. Increment the usage count so as to
681 	 * prevent the device from entering runtime suspend.
682 	 */
683 	if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
684 		pm_runtime_get_noresume(sdev->dev);
685 
686 	return ret;
687 }
688 
689 static void sof_pcm_remove(struct snd_soc_component *component)
690 {
691 	/* remove topology */
692 	snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
693 }
694 
695 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
696 {
697 	struct snd_soc_component_driver *pd = &sdev->plat_drv;
698 	struct snd_sof_pdata *plat_data = sdev->pdata;
699 	const char *drv_name;
700 
701 	drv_name = plat_data->machine->drv_name;
702 
703 	pd->name = "sof-audio-component";
704 	pd->probe = sof_pcm_probe;
705 	pd->remove = sof_pcm_remove;
706 	pd->ops	= &sof_pcm_ops;
707 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
708 	pd->compr_ops = &sof_compressed_ops;
709 #endif
710 	pd->pcm_new = sof_pcm_new;
711 	pd->ignore_machine = drv_name;
712 	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
713 	pd->be_pcm_base = SOF_BE_PCM_BASE;
714 	pd->use_dai_pcm_id = true;
715 	pd->topology_name_prefix = "sof";
716 
717 	 /* increment module refcount when a pcm is opened */
718 	pd->module_get_upon_open = 1;
719 }
720