Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / sound / soc / sof / pcm.c
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 /*
56  * sof pcm period elapse work
57  */
58 static 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  */
70 void 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 }
95 EXPORT_SYMBOL(snd_sof_pcm_period_elapsed);
96
97 /* this may get called several times by oss emulation */
98 static 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
219 static int sof_pcm_hw_free(struct snd_pcm_substream *substream)
220 {
221         struct snd_soc_pcm_runtime *rtd = substream->private_data;
222         struct snd_soc_component *component =
223                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
224         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
225         struct snd_sof_pcm *spcm;
226         struct sof_ipc_stream stream;
227         struct sof_ipc_reply reply;
228         int ret;
229
230         /* nothing to do for BE */
231         if (rtd->dai_link->no_pcm)
232                 return 0;
233
234         spcm = snd_sof_find_spcm_dai(sdev, rtd);
235         if (!spcm)
236                 return -EINVAL;
237
238         if (!spcm->prepared[substream->stream])
239                 return 0;
240
241         dev_dbg(sdev->dev, "pcm: free stream %d dir %d\n", spcm->pcm.pcm_id,
242                 substream->stream);
243
244         stream.hdr.size = sizeof(stream);
245         stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
246         stream.comp_id = spcm->stream[substream->stream].comp_id;
247
248         /* send IPC to the DSP */
249         ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
250                                  sizeof(stream), &reply, sizeof(reply));
251
252         snd_pcm_lib_free_pages(substream);
253
254         cancel_work_sync(&spcm->stream[substream->stream].period_elapsed_work);
255
256         if (ret < 0)
257                 return ret;
258
259         ret = snd_sof_pcm_platform_hw_free(sdev, substream);
260         if (ret < 0)
261                 dev_err(sdev->dev, "error: platform hw free failed\n");
262
263         spcm->prepared[substream->stream] = false;
264
265         return ret;
266 }
267
268 static int sof_pcm_prepare(struct snd_pcm_substream *substream)
269 {
270         struct snd_soc_pcm_runtime *rtd = substream->private_data;
271         struct snd_soc_component *component =
272                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
273         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
274         struct snd_sof_pcm *spcm;
275         int ret;
276
277         /* nothing to do for BE */
278         if (rtd->dai_link->no_pcm)
279                 return 0;
280
281         spcm = snd_sof_find_spcm_dai(sdev, rtd);
282         if (!spcm)
283                 return -EINVAL;
284
285         if (spcm->prepared[substream->stream])
286                 return 0;
287
288         dev_dbg(sdev->dev, "pcm: prepare stream %d dir %d\n", spcm->pcm.pcm_id,
289                 substream->stream);
290
291         /* set hw_params */
292         ret = sof_pcm_hw_params(substream, &spcm->params[substream->stream]);
293         if (ret < 0) {
294                 dev_err(sdev->dev, "error: set pcm hw_params after resume\n");
295                 return ret;
296         }
297
298         return 0;
299 }
300
301 /*
302  * FE dai link trigger actions are always executed in non-atomic context because
303  * they involve IPC's.
304  */
305 static int sof_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
306 {
307         struct snd_soc_pcm_runtime *rtd = substream->private_data;
308         struct snd_soc_component *component =
309                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
310         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
311         struct snd_sof_pcm *spcm;
312         struct sof_ipc_stream stream;
313         struct sof_ipc_reply reply;
314         bool reset_hw_params = false;
315         int ret;
316
317         /* nothing to do for BE */
318         if (rtd->dai_link->no_pcm)
319                 return 0;
320
321         spcm = snd_sof_find_spcm_dai(sdev, rtd);
322         if (!spcm)
323                 return -EINVAL;
324
325         dev_dbg(sdev->dev, "pcm: trigger stream %d dir %d cmd %d\n",
326                 spcm->pcm.pcm_id, substream->stream, cmd);
327
328         stream.hdr.size = sizeof(stream);
329         stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
330         stream.comp_id = spcm->stream[substream->stream].comp_id;
331
332         switch (cmd) {
333         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
334                 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
335                 break;
336         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
337                 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
338                 break;
339         case SNDRV_PCM_TRIGGER_RESUME:
340                 /* set up hw_params */
341                 ret = sof_pcm_prepare(substream);
342                 if (ret < 0) {
343                         dev_err(sdev->dev,
344                                 "error: failed to set up hw_params upon resume\n");
345                         return ret;
346                 }
347
348                 /* fallthrough */
349         case SNDRV_PCM_TRIGGER_START:
350                 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
351                 break;
352         case SNDRV_PCM_TRIGGER_SUSPEND:
353         case SNDRV_PCM_TRIGGER_STOP:
354                 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
355                 reset_hw_params = true;
356                 break;
357         default:
358                 dev_err(sdev->dev, "error: unhandled trigger cmd %d\n", cmd);
359                 return -EINVAL;
360         }
361
362         snd_sof_pcm_platform_trigger(sdev, substream, cmd);
363
364         /* send IPC to the DSP */
365         ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
366                                  sizeof(stream), &reply, sizeof(reply));
367
368         if (ret < 0 || !reset_hw_params)
369                 return ret;
370
371         /*
372          * In case of stream is stopped, DSP must be reprogrammed upon
373          * restart, so free PCM here.
374          */
375         stream.hdr.size = sizeof(stream);
376         stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
377         stream.comp_id = spcm->stream[substream->stream].comp_id;
378         spcm->prepared[substream->stream] = false;
379
380         /* send IPC to the DSP */
381         return sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
382                                   sizeof(stream), &reply, sizeof(reply));
383 }
384
385 static snd_pcm_uframes_t sof_pcm_pointer(struct snd_pcm_substream *substream)
386 {
387         struct snd_soc_pcm_runtime *rtd = substream->private_data;
388         struct snd_soc_component *component =
389                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
390         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
391         struct snd_sof_pcm *spcm;
392         snd_pcm_uframes_t host, dai;
393
394         /* nothing to do for BE */
395         if (rtd->dai_link->no_pcm)
396                 return 0;
397
398         /* use dsp ops pointer callback directly if set */
399         if (sof_ops(sdev)->pcm_pointer)
400                 return sof_ops(sdev)->pcm_pointer(sdev, substream);
401
402         spcm = snd_sof_find_spcm_dai(sdev, rtd);
403         if (!spcm)
404                 return -EINVAL;
405
406         /* read position from DSP */
407         host = bytes_to_frames(substream->runtime,
408                                spcm->stream[substream->stream].posn.host_posn);
409         dai = bytes_to_frames(substream->runtime,
410                               spcm->stream[substream->stream].posn.dai_posn);
411
412         dev_dbg(sdev->dev, "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
413                 spcm->pcm.pcm_id, substream->stream, host, dai);
414
415         return host;
416 }
417
418 static int sof_pcm_open(struct snd_pcm_substream *substream)
419 {
420         struct snd_soc_pcm_runtime *rtd = substream->private_data;
421         struct snd_pcm_runtime *runtime = substream->runtime;
422         struct snd_soc_component *component =
423                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
424         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
425         struct snd_sof_pcm *spcm;
426         struct snd_soc_tplg_stream_caps *caps;
427         int ret;
428
429         /* nothing to do for BE */
430         if (rtd->dai_link->no_pcm)
431                 return 0;
432
433         spcm = snd_sof_find_spcm_dai(sdev, rtd);
434         if (!spcm)
435                 return -EINVAL;
436
437         dev_dbg(sdev->dev, "pcm: open stream %d dir %d\n", spcm->pcm.pcm_id,
438                 substream->stream);
439
440         INIT_WORK(&spcm->stream[substream->stream].period_elapsed_work,
441                   sof_pcm_period_elapsed_work);
442
443         caps = &spcm->pcm.caps[substream->stream];
444
445         /* set any runtime constraints based on topology */
446         snd_pcm_hw_constraint_step(substream->runtime, 0,
447                                    SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
448                                    le32_to_cpu(caps->period_size_min));
449         snd_pcm_hw_constraint_step(substream->runtime, 0,
450                                    SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
451                                    le32_to_cpu(caps->period_size_min));
452
453         /* set runtime config */
454         runtime->hw.info = SNDRV_PCM_INFO_MMAP |
455                           SNDRV_PCM_INFO_MMAP_VALID |
456                           SNDRV_PCM_INFO_INTERLEAVED |
457                           SNDRV_PCM_INFO_PAUSE |
458                           SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
459         runtime->hw.formats = le64_to_cpu(caps->formats);
460         runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
461         runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
462         runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
463         runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
464
465         /*
466          * caps->buffer_size_min is not used since the
467          * snd_pcm_hardware structure only defines buffer_bytes_max
468          */
469         runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
470
471         dev_dbg(sdev->dev, "period min %zd max %zd bytes\n",
472                 runtime->hw.period_bytes_min,
473                 runtime->hw.period_bytes_max);
474         dev_dbg(sdev->dev, "period count %d max %d\n",
475                 runtime->hw.periods_min,
476                 runtime->hw.periods_max);
477         dev_dbg(sdev->dev, "buffer max %zd bytes\n",
478                 runtime->hw.buffer_bytes_max);
479
480         /* set wait time - TODO: come from topology */
481         substream->wait_time = 500;
482
483         spcm->stream[substream->stream].posn.host_posn = 0;
484         spcm->stream[substream->stream].posn.dai_posn = 0;
485         spcm->stream[substream->stream].substream = substream;
486         spcm->prepared[substream->stream] = false;
487
488         ret = snd_sof_pcm_platform_open(sdev, substream);
489         if (ret < 0)
490                 dev_err(sdev->dev, "error: pcm open failed %d\n", ret);
491
492         return ret;
493 }
494
495 static int sof_pcm_close(struct snd_pcm_substream *substream)
496 {
497         struct snd_soc_pcm_runtime *rtd = substream->private_data;
498         struct snd_soc_component *component =
499                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
500         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
501         struct snd_sof_pcm *spcm;
502         int err;
503
504         /* nothing to do for BE */
505         if (rtd->dai_link->no_pcm)
506                 return 0;
507
508         spcm = snd_sof_find_spcm_dai(sdev, rtd);
509         if (!spcm)
510                 return -EINVAL;
511
512         dev_dbg(sdev->dev, "pcm: close stream %d dir %d\n", spcm->pcm.pcm_id,
513                 substream->stream);
514
515         err = snd_sof_pcm_platform_close(sdev, substream);
516         if (err < 0) {
517                 dev_err(sdev->dev, "error: pcm close failed %d\n",
518                         err);
519                 /*
520                  * keep going, no point in preventing the close
521                  * from happening
522                  */
523         }
524
525         return 0;
526 }
527
528 static struct snd_pcm_ops sof_pcm_ops = {
529         .open           = sof_pcm_open,
530         .close          = sof_pcm_close,
531         .ioctl          = snd_pcm_lib_ioctl,
532         .hw_params      = sof_pcm_hw_params,
533         .prepare        = sof_pcm_prepare,
534         .hw_free        = sof_pcm_hw_free,
535         .trigger        = sof_pcm_trigger,
536         .pointer        = sof_pcm_pointer,
537         .page           = snd_pcm_sgbuf_ops_page,
538 };
539
540 /*
541  * Pre-allocate playback/capture audio buffer pages.
542  * no need to explicitly release memory preallocated by sof_pcm_new in pcm_free
543  * snd_pcm_lib_preallocate_free_for_all() is called by the core.
544  */
545 static int sof_pcm_new(struct snd_soc_pcm_runtime *rtd)
546 {
547         struct snd_soc_component *component =
548                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
549         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
550         struct snd_sof_pcm *spcm;
551         struct snd_pcm *pcm = rtd->pcm;
552         struct snd_soc_tplg_stream_caps *caps;
553         int stream = SNDRV_PCM_STREAM_PLAYBACK;
554
555         /* find SOF PCM for this RTD */
556         spcm = snd_sof_find_spcm_dai(sdev, rtd);
557         if (!spcm) {
558                 dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
559                          rtd->dai_link->id);
560                 return 0;
561         }
562
563         dev_dbg(sdev->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
564
565         /* do we need to pre-allocate playback audio buffer pages */
566         if (!spcm->pcm.playback)
567                 goto capture;
568
569         caps = &spcm->pcm.caps[stream];
570
571         /* pre-allocate playback audio buffer pages */
572         dev_dbg(sdev->dev, "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
573                 caps->name, caps->buffer_size_min, caps->buffer_size_max);
574
575         snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
576                                       SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
577                                       le32_to_cpu(caps->buffer_size_min),
578                                       le32_to_cpu(caps->buffer_size_max));
579 capture:
580         stream = SNDRV_PCM_STREAM_CAPTURE;
581
582         /* do we need to pre-allocate capture audio buffer pages */
583         if (!spcm->pcm.capture)
584                 return 0;
585
586         caps = &spcm->pcm.caps[stream];
587
588         /* pre-allocate capture audio buffer pages */
589         dev_dbg(sdev->dev, "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
590                 caps->name, caps->buffer_size_min, caps->buffer_size_max);
591
592         snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
593                                       SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
594                                       le32_to_cpu(caps->buffer_size_min),
595                                       le32_to_cpu(caps->buffer_size_max));
596
597         return 0;
598 }
599
600 /* fixup the BE DAI link to match any values from topology */
601 static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
602                                   struct snd_pcm_hw_params *params)
603 {
604         struct snd_interval *rate = hw_param_interval(params,
605                         SNDRV_PCM_HW_PARAM_RATE);
606         struct snd_interval *channels = hw_param_interval(params,
607                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
608         struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
609         struct snd_soc_component *component =
610                 snd_soc_rtdcom_lookup(rtd, DRV_NAME);
611         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
612         struct snd_sof_dai *dai =
613                 snd_sof_find_dai(sdev, (char *)rtd->dai_link->name);
614
615         /* no topology exists for this BE, try a common configuration */
616         if (!dai) {
617                 dev_warn(sdev->dev, "warning: no topology found for BE DAI %s config\n",
618                          rtd->dai_link->name);
619
620                 /*  set 48k, stereo, 16bits by default */
621                 rate->min = 48000;
622                 rate->max = 48000;
623
624                 channels->min = 2;
625                 channels->max = 2;
626
627                 snd_mask_none(fmt);
628                 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
629
630                 return 0;
631         }
632
633         /* read format from topology */
634         snd_mask_none(fmt);
635
636         switch (dai->comp_dai.config.frame_fmt) {
637         case SOF_IPC_FRAME_S16_LE:
638                 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
639                 break;
640         case SOF_IPC_FRAME_S24_4LE:
641                 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
642                 break;
643         case SOF_IPC_FRAME_S32_LE:
644                 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
645                 break;
646         default:
647                 dev_err(sdev->dev, "error: No available DAI format!\n");
648                 return -EINVAL;
649         }
650
651         /* read rate and channels from topology */
652         switch (dai->dai_config->type) {
653         case SOF_DAI_INTEL_SSP:
654                 rate->min = dai->dai_config->ssp.fsync_rate;
655                 rate->max = dai->dai_config->ssp.fsync_rate;
656                 channels->min = dai->dai_config->ssp.tdm_slots;
657                 channels->max = dai->dai_config->ssp.tdm_slots;
658
659                 dev_dbg(sdev->dev,
660                         "rate_min: %d rate_max: %d\n", rate->min, rate->max);
661                 dev_dbg(sdev->dev,
662                         "channels_min: %d channels_max: %d\n",
663                         channels->min, channels->max);
664
665                 break;
666         case SOF_DAI_INTEL_DMIC:
667                 /* DMIC only supports 16 or 32 bit formats */
668                 if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
669                         dev_err(sdev->dev,
670                                 "error: invalid fmt %d for DAI type %d\n",
671                                 dai->comp_dai.config.frame_fmt,
672                                 dai->dai_config->type);
673                 }
674                 break;
675         case SOF_DAI_INTEL_HDA:
676                 /* do nothing for HDA dai_link */
677                 break;
678         default:
679                 dev_err(sdev->dev, "error: invalid DAI type %d\n",
680                         dai->dai_config->type);
681                 break;
682         }
683
684         return 0;
685 }
686
687 static int sof_pcm_probe(struct snd_soc_component *component)
688 {
689         struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
690         struct snd_sof_pdata *plat_data = sdev->pdata;
691         const char *tplg_filename;
692         int ret;
693
694         /* load the default topology */
695         sdev->component = component;
696
697         tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
698                                        "%s/%s",
699                                        plat_data->tplg_filename_prefix,
700                                        plat_data->tplg_filename);
701         if (!tplg_filename)
702                 return -ENOMEM;
703
704         ret = snd_sof_load_topology(sdev, tplg_filename);
705         if (ret < 0) {
706                 dev_err(sdev->dev, "error: failed to load DSP topology %d\n",
707                         ret);
708                 return ret;
709         }
710
711         /*
712          * Some platforms in SOF, ex: BYT, may not have their platform PM
713          * callbacks set. Increment the usage count so as to
714          * prevent the device from entering runtime suspend.
715          */
716         if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
717                 pm_runtime_get_noresume(sdev->dev);
718
719         return ret;
720 }
721
722 static void sof_pcm_remove(struct snd_soc_component *component)
723 {
724         /* remove topology */
725         snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL);
726 }
727
728 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
729 {
730         struct snd_soc_component_driver *pd = &sdev->plat_drv;
731         struct snd_sof_pdata *plat_data = sdev->pdata;
732         const char *drv_name;
733
734         drv_name = plat_data->machine->drv_name;
735
736         pd->name = "sof-audio-component";
737         pd->probe = sof_pcm_probe;
738         pd->remove = sof_pcm_remove;
739         pd->ops = &sof_pcm_ops;
740 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
741         pd->compr_ops = &sof_compressed_ops;
742 #endif
743         pd->pcm_new = sof_pcm_new;
744         pd->ignore_machine = drv_name;
745         pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
746         pd->be_pcm_base = SOF_BE_PCM_BASE;
747         pd->use_dai_pcm_id = true;
748         pd->topology_name_prefix = "sof";
749
750          /* increment module refcount when a pcm is opened */
751         pd->module_get_upon_open = 1;
752 }