Merge series "ASoC: Intel: catpt: Offload fixes and code optimization" from Cezary...
[linux-2.6-microblaze.git] / sound / soc / soc-compress.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-compress.c  --  ALSA SoC Compress
4 //
5 // Copyright (C) 2012 Intel Corp.
6 //
7 // Authors: Namarta Kohli <namartax.kohli@intel.com>
8 //          Ramesh Babu K V <ramesh.babu@linux.intel.com>
9 //          Vinod Koul <vinod.koul@linux.intel.com>
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <sound/core.h>
17 #include <sound/compress_params.h>
18 #include <sound/compress_driver.h>
19 #include <sound/soc.h>
20 #include <sound/initval.h>
21 #include <sound/soc-dpcm.h>
22 #include <sound/soc-link.h>
23 #include <linux/pm_runtime.h>
24
25 static int soc_compr_components_open(struct snd_compr_stream *cstream,
26                                      struct snd_soc_component **last)
27 {
28         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
29         struct snd_soc_component *component;
30         int i, ret;
31
32         for_each_rtd_components(rtd, i, component) {
33                 if (!component->driver->compress_ops ||
34                     !component->driver->compress_ops->open)
35                         continue;
36
37                 ret = component->driver->compress_ops->open(component, cstream);
38                 if (ret < 0) {
39                         dev_err(component->dev,
40                                 "Compress ASoC: can't open platform %s: %d\n",
41                                 component->name, ret);
42
43                         *last = component;
44                         return ret;
45                 }
46         }
47
48         *last = NULL;
49         return 0;
50 }
51
52 static int soc_compr_components_free(struct snd_compr_stream *cstream,
53                                      struct snd_soc_component *last)
54 {
55         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
56         struct snd_soc_component *component;
57         int i;
58
59         for_each_rtd_components(rtd, i, component) {
60                 if (component == last)
61                         break;
62
63                 if (!component->driver->compress_ops ||
64                     !component->driver->compress_ops->free)
65                         continue;
66
67                 component->driver->compress_ops->free(component, cstream);
68         }
69
70         return 0;
71 }
72
73 static int soc_compr_open(struct snd_compr_stream *cstream)
74 {
75         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
76         struct snd_soc_component *component = NULL;
77         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
78         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
79         int ret;
80
81         ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
82         if (ret < 0)
83                 goto pm_err;
84
85         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
86
87         ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
88         if (ret < 0)
89                 goto out;
90
91         ret = soc_compr_components_open(cstream, &component);
92         if (ret < 0)
93                 goto machine_err;
94
95         ret = snd_soc_link_compr_startup(cstream);
96         if (ret < 0)
97                 goto machine_err;
98
99         snd_soc_runtime_activate(rtd, stream);
100
101         mutex_unlock(&rtd->card->pcm_mutex);
102
103         return 0;
104
105 machine_err:
106         soc_compr_components_free(cstream, component);
107
108         snd_soc_dai_compr_shutdown(cpu_dai, cstream);
109 out:
110         mutex_unlock(&rtd->card->pcm_mutex);
111 pm_err:
112         snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 1);
113
114         return ret;
115 }
116
117 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
118 {
119         struct snd_soc_pcm_runtime *fe = cstream->private_data;
120         struct snd_pcm_substream *fe_substream =
121                  fe->pcm->streams[cstream->direction].substream;
122         struct snd_soc_component *component;
123         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
124         struct snd_soc_dpcm *dpcm;
125         struct snd_soc_dapm_widget_list *list;
126         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
127         int ret;
128
129         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
130         fe->dpcm[stream].runtime = fe_substream->runtime;
131
132         ret = dpcm_path_get(fe, stream, &list);
133         if (ret < 0)
134                 goto be_err;
135         else if (ret == 0)
136                 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
137                         fe->dai_link->name, stream ? "capture" : "playback");
138         /* calculate valid and active FE <-> BE dpcms */
139         dpcm_process_paths(fe, stream, &list, 1);
140         fe->dpcm[stream].runtime = fe_substream->runtime;
141
142         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
143
144         ret = dpcm_be_dai_startup(fe, stream);
145         if (ret < 0) {
146                 /* clean up all links */
147                 for_each_dpcm_be(fe, stream, dpcm)
148                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
149
150                 dpcm_be_disconnect(fe, stream);
151                 fe->dpcm[stream].runtime = NULL;
152                 goto out;
153         }
154
155         ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
156         if (ret < 0)
157                 goto out;
158
159         ret = soc_compr_components_open(cstream, &component);
160         if (ret < 0)
161                 goto open_err;
162
163         ret = snd_soc_link_compr_startup(cstream);
164         if (ret < 0)
165                 goto machine_err;
166
167         dpcm_clear_pending_state(fe, stream);
168         dpcm_path_put(&list);
169
170         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
171         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
172
173         snd_soc_runtime_activate(fe, stream);
174
175         mutex_unlock(&fe->card->mutex);
176
177         return 0;
178
179 machine_err:
180         soc_compr_components_free(cstream, component);
181 open_err:
182         snd_soc_dai_compr_shutdown(cpu_dai, cstream);
183 out:
184         dpcm_path_put(&list);
185 be_err:
186         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
187         mutex_unlock(&fe->card->mutex);
188         return ret;
189 }
190
191 static int soc_compr_free(struct snd_compr_stream *cstream)
192 {
193         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
194         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
195         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
196         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
197
198         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
199
200         snd_soc_runtime_deactivate(rtd, stream);
201
202         snd_soc_dai_digital_mute(codec_dai, 1, stream);
203
204         if (!snd_soc_dai_active(cpu_dai))
205                 cpu_dai->rate = 0;
206
207         if (!snd_soc_dai_active(codec_dai))
208                 codec_dai->rate = 0;
209
210         snd_soc_link_compr_shutdown(cstream);
211
212         soc_compr_components_free(cstream, NULL);
213
214         snd_soc_dai_compr_shutdown(cpu_dai, cstream);
215
216         snd_soc_dapm_stream_stop(rtd, stream);
217
218         mutex_unlock(&rtd->card->pcm_mutex);
219
220         snd_soc_pcm_component_pm_runtime_put(rtd, cstream, 0);
221
222         return 0;
223 }
224
225 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
226 {
227         struct snd_soc_pcm_runtime *fe = cstream->private_data;
228         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
229         struct snd_soc_dpcm *dpcm;
230         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
231         int ret;
232
233         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
234
235         snd_soc_runtime_deactivate(fe, stream);
236
237         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
238
239         ret = dpcm_be_dai_hw_free(fe, stream);
240         if (ret < 0)
241                 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
242
243         ret = dpcm_be_dai_shutdown(fe, stream);
244
245         /* mark FE's links ready to prune */
246         for_each_dpcm_be(fe, stream, dpcm)
247                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
248
249         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
250
251         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
252         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
253
254         dpcm_be_disconnect(fe, stream);
255
256         fe->dpcm[stream].runtime = NULL;
257
258         snd_soc_link_compr_shutdown(cstream);
259
260         soc_compr_components_free(cstream, NULL);
261
262         snd_soc_dai_compr_shutdown(cpu_dai, cstream);
263
264         mutex_unlock(&fe->card->mutex);
265         return 0;
266 }
267
268 static int soc_compr_components_trigger(struct snd_compr_stream *cstream,
269                                         int cmd)
270 {
271         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
272         struct snd_soc_component *component;
273         int i, ret;
274
275         for_each_rtd_components(rtd, i, component) {
276                 if (!component->driver->compress_ops ||
277                     !component->driver->compress_ops->trigger)
278                         continue;
279
280                 ret = component->driver->compress_ops->trigger(
281                         component, cstream, cmd);
282                 if (ret < 0)
283                         return ret;
284         }
285
286         return 0;
287 }
288
289 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
290 {
291         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
292         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
293         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
294         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
295         int ret;
296
297         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
298
299         ret = soc_compr_components_trigger(cstream, cmd);
300         if (ret < 0)
301                 goto out;
302
303         ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
304         if (ret < 0)
305                 goto out;
306
307         switch (cmd) {
308         case SNDRV_PCM_TRIGGER_START:
309                 snd_soc_dai_digital_mute(codec_dai, 0, stream);
310                 break;
311         case SNDRV_PCM_TRIGGER_STOP:
312                 snd_soc_dai_digital_mute(codec_dai, 1, stream);
313                 break;
314         }
315
316 out:
317         mutex_unlock(&rtd->card->pcm_mutex);
318         return ret;
319 }
320
321 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
322 {
323         struct snd_soc_pcm_runtime *fe = cstream->private_data;
324         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
325         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
326         int ret;
327
328         if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
329             cmd == SND_COMPR_TRIGGER_DRAIN)
330                 return soc_compr_components_trigger(cstream, cmd);
331
332         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
333
334         ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
335         if (ret < 0)
336                 goto out;
337
338         ret = soc_compr_components_trigger(cstream, cmd);
339         if (ret < 0)
340                 goto out;
341
342         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
343
344         ret = dpcm_be_dai_trigger(fe, stream, cmd);
345
346         switch (cmd) {
347         case SNDRV_PCM_TRIGGER_START:
348         case SNDRV_PCM_TRIGGER_RESUME:
349         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
350                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
351                 break;
352         case SNDRV_PCM_TRIGGER_STOP:
353         case SNDRV_PCM_TRIGGER_SUSPEND:
354                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
355                 break;
356         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
357                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
358                 break;
359         }
360
361 out:
362         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
363         mutex_unlock(&fe->card->mutex);
364         return ret;
365 }
366
367 static int soc_compr_components_set_params(struct snd_compr_stream *cstream,
368                                            struct snd_compr_params *params)
369 {
370         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
371         struct snd_soc_component *component;
372         int i, ret;
373
374         for_each_rtd_components(rtd, i, component) {
375                 if (!component->driver->compress_ops ||
376                     !component->driver->compress_ops->set_params)
377                         continue;
378
379                 ret = component->driver->compress_ops->set_params(
380                         component, cstream, params);
381                 if (ret < 0)
382                         return ret;
383         }
384
385         return 0;
386 }
387
388 static int soc_compr_set_params(struct snd_compr_stream *cstream,
389                                 struct snd_compr_params *params)
390 {
391         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
392         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
393         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
394         int ret;
395
396         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
397
398         /*
399          * First we call set_params for the CPU DAI, then the component
400          * driver this should configure the SoC side. If the machine has
401          * compressed ops then we call that as well. The expectation is
402          * that these callbacks will configure everything for this compress
403          * path, like configuring a PCM port for a CODEC.
404          */
405         ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
406         if (ret < 0)
407                 goto err;
408
409         ret = soc_compr_components_set_params(cstream, params);
410         if (ret < 0)
411                 goto err;
412
413         ret = snd_soc_link_compr_set_params(cstream);
414         if (ret < 0)
415                 goto err;
416
417         snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
418
419         /* cancel any delayed stream shutdown that is pending */
420         rtd->pop_wait = 0;
421         mutex_unlock(&rtd->card->pcm_mutex);
422
423         cancel_delayed_work_sync(&rtd->delayed_work);
424
425         return 0;
426
427 err:
428         mutex_unlock(&rtd->card->pcm_mutex);
429         return ret;
430 }
431
432 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
433                                    struct snd_compr_params *params)
434 {
435         struct snd_soc_pcm_runtime *fe = cstream->private_data;
436         struct snd_pcm_substream *fe_substream =
437                  fe->pcm->streams[cstream->direction].substream;
438         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
439         int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
440         int ret;
441
442         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
443
444         /*
445          * Create an empty hw_params for the BE as the machine driver must
446          * fix this up to match DSP decoder and ASRC configuration.
447          * I.e. machine driver fixup for compressed BE is mandatory.
448          */
449         memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
450                 sizeof(struct snd_pcm_hw_params));
451
452         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
453
454         ret = dpcm_be_dai_hw_params(fe, stream);
455         if (ret < 0)
456                 goto out;
457
458         ret = dpcm_be_dai_prepare(fe, stream);
459         if (ret < 0)
460                 goto out;
461
462         ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
463         if (ret < 0)
464                 goto out;
465
466         ret = soc_compr_components_set_params(cstream, params);
467         if (ret < 0)
468                 goto out;
469
470         ret = snd_soc_link_compr_set_params(cstream);
471         if (ret < 0)
472                 goto out;
473
474         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
475         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
476
477 out:
478         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
479         mutex_unlock(&fe->card->mutex);
480         return ret;
481 }
482
483 static int soc_compr_get_params(struct snd_compr_stream *cstream,
484                                 struct snd_codec *params)
485 {
486         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
487         struct snd_soc_component *component;
488         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
489         int i, ret = 0;
490
491         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
492
493         ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
494         if (ret < 0)
495                 goto err;
496
497         for_each_rtd_components(rtd, i, component) {
498                 if (!component->driver->compress_ops ||
499                     !component->driver->compress_ops->get_params)
500                         continue;
501
502                 ret = component->driver->compress_ops->get_params(
503                         component, cstream, params);
504                 break;
505         }
506
507 err:
508         mutex_unlock(&rtd->card->pcm_mutex);
509         return ret;
510 }
511
512 static int soc_compr_get_caps(struct snd_compr_stream *cstream,
513                               struct snd_compr_caps *caps)
514 {
515         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
516         struct snd_soc_component *component;
517         int i, ret = 0;
518
519         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
520
521         for_each_rtd_components(rtd, i, component) {
522                 if (!component->driver->compress_ops ||
523                     !component->driver->compress_ops->get_caps)
524                         continue;
525
526                 ret = component->driver->compress_ops->get_caps(
527                         component, cstream, caps);
528                 break;
529         }
530
531         mutex_unlock(&rtd->card->pcm_mutex);
532         return ret;
533 }
534
535 static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
536                                     struct snd_compr_codec_caps *codec)
537 {
538         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
539         struct snd_soc_component *component;
540         int i, ret = 0;
541
542         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
543
544         for_each_rtd_components(rtd, i, component) {
545                 if (!component->driver->compress_ops ||
546                     !component->driver->compress_ops->get_codec_caps)
547                         continue;
548
549                 ret = component->driver->compress_ops->get_codec_caps(
550                         component, cstream, codec);
551                 break;
552         }
553
554         mutex_unlock(&rtd->card->pcm_mutex);
555         return ret;
556 }
557
558 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
559 {
560         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
561         struct snd_soc_component *component;
562         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
563         int i, ret = 0;
564
565         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
566
567         ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
568         if (ret < 0)
569                 goto err;
570
571         for_each_rtd_components(rtd, i, component) {
572                 if (!component->driver->compress_ops ||
573                     !component->driver->compress_ops->ack)
574                         continue;
575
576                 ret = component->driver->compress_ops->ack(
577                         component, cstream, bytes);
578                 if (ret < 0)
579                         goto err;
580         }
581
582 err:
583         mutex_unlock(&rtd->card->pcm_mutex);
584         return ret;
585 }
586
587 static int soc_compr_pointer(struct snd_compr_stream *cstream,
588                              struct snd_compr_tstamp *tstamp)
589 {
590         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
591         struct snd_soc_component *component;
592         int i, ret = 0;
593         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
594
595         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
596
597         ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
598         if (ret < 0)
599                 goto out;
600
601         for_each_rtd_components(rtd, i, component) {
602                 if (!component->driver->compress_ops ||
603                     !component->driver->compress_ops->pointer)
604                         continue;
605
606                 ret = component->driver->compress_ops->pointer(
607                         component, cstream, tstamp);
608                 break;
609         }
610 out:
611         mutex_unlock(&rtd->card->pcm_mutex);
612         return ret;
613 }
614
615 static int soc_compr_copy(struct snd_compr_stream *cstream,
616                           char __user *buf, size_t count)
617 {
618         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
619         struct snd_soc_component *component;
620         int i, ret = 0;
621
622         mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
623
624         for_each_rtd_components(rtd, i, component) {
625                 if (!component->driver->compress_ops ||
626                     !component->driver->compress_ops->copy)
627                         continue;
628
629                 ret = component->driver->compress_ops->copy(
630                         component, cstream, buf, count);
631                 break;
632         }
633
634         mutex_unlock(&rtd->card->pcm_mutex);
635         return ret;
636 }
637
638 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
639                                   struct snd_compr_metadata *metadata)
640 {
641         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
642         struct snd_soc_component *component;
643         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
644         int i, ret;
645
646         ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
647         if (ret < 0)
648                 return ret;
649
650         for_each_rtd_components(rtd, i, component) {
651                 if (!component->driver->compress_ops ||
652                     !component->driver->compress_ops->set_metadata)
653                         continue;
654
655                 ret = component->driver->compress_ops->set_metadata(
656                         component, cstream, metadata);
657                 if (ret < 0)
658                         return ret;
659         }
660
661         return 0;
662 }
663
664 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
665                                   struct snd_compr_metadata *metadata)
666 {
667         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
668         struct snd_soc_component *component;
669         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
670         int i, ret;
671
672         ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
673         if (ret < 0)
674                 return ret;
675
676         for_each_rtd_components(rtd, i, component) {
677                 if (!component->driver->compress_ops ||
678                     !component->driver->compress_ops->get_metadata)
679                         continue;
680
681                 return component->driver->compress_ops->get_metadata(
682                         component, cstream, metadata);
683         }
684
685         return 0;
686 }
687
688 /* ASoC Compress operations */
689 static struct snd_compr_ops soc_compr_ops = {
690         .open           = soc_compr_open,
691         .free           = soc_compr_free,
692         .set_params     = soc_compr_set_params,
693         .set_metadata   = soc_compr_set_metadata,
694         .get_metadata   = soc_compr_get_metadata,
695         .get_params     = soc_compr_get_params,
696         .trigger        = soc_compr_trigger,
697         .pointer        = soc_compr_pointer,
698         .ack            = soc_compr_ack,
699         .get_caps       = soc_compr_get_caps,
700         .get_codec_caps = soc_compr_get_codec_caps
701 };
702
703 /* ASoC Dynamic Compress operations */
704 static struct snd_compr_ops soc_compr_dyn_ops = {
705         .open           = soc_compr_open_fe,
706         .free           = soc_compr_free_fe,
707         .set_params     = soc_compr_set_params_fe,
708         .get_params     = soc_compr_get_params,
709         .set_metadata   = soc_compr_set_metadata,
710         .get_metadata   = soc_compr_get_metadata,
711         .trigger        = soc_compr_trigger_fe,
712         .pointer        = soc_compr_pointer,
713         .ack            = soc_compr_ack,
714         .get_caps       = soc_compr_get_caps,
715         .get_codec_caps = soc_compr_get_codec_caps
716 };
717
718 /**
719  * snd_soc_new_compress - create a new compress.
720  *
721  * @rtd: The runtime for which we will create compress
722  * @num: the device index number (zero based - shared with normal PCMs)
723  *
724  * Return: 0 for success, else error.
725  */
726 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
727 {
728         struct snd_soc_component *component;
729         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
730         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
731         struct snd_compr *compr;
732         struct snd_pcm *be_pcm;
733         char new_name[64];
734         int ret = 0, direction = 0;
735         int playback = 0, capture = 0;
736         int i;
737
738         /*
739          * make sure these are same value,
740          * and then use these as equally
741          */
742         BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
743         BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE  != (int)SND_COMPRESS_CAPTURE);
744
745         if (rtd->num_cpus > 1 ||
746             rtd->num_codecs > 1) {
747                 dev_err(rtd->card->dev,
748                         "Compress ASoC: Multi CPU/Codec not supported\n");
749                 return -EINVAL;
750         }
751
752         /* check client and interface hw capabilities */
753         if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
754             snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_PLAYBACK))
755                 playback = 1;
756         if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
757             snd_soc_dai_stream_valid(cpu_dai,   SNDRV_PCM_STREAM_CAPTURE))
758                 capture = 1;
759
760         /*
761          * Compress devices are unidirectional so only one of the directions
762          * should be set, check for that (xor)
763          */
764         if (playback + capture != 1) {
765                 dev_err(rtd->card->dev,
766                         "Compress ASoC: Invalid direction for P %d, C %d\n",
767                         playback, capture);
768                 return -EINVAL;
769         }
770
771         if (playback)
772                 direction = SND_COMPRESS_PLAYBACK;
773         else
774                 direction = SND_COMPRESS_CAPTURE;
775
776         compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
777         if (!compr)
778                 return -ENOMEM;
779
780         compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
781                                   GFP_KERNEL);
782         if (!compr->ops)
783                 return -ENOMEM;
784
785         if (rtd->dai_link->dynamic) {
786                 snprintf(new_name, sizeof(new_name), "(%s)",
787                         rtd->dai_link->stream_name);
788
789                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
790                                 rtd->dai_link->dpcm_playback,
791                                 rtd->dai_link->dpcm_capture, &be_pcm);
792                 if (ret < 0) {
793                         dev_err(rtd->card->dev,
794                                 "Compress ASoC: can't create compressed for %s: %d\n",
795                                 rtd->dai_link->name, ret);
796                         return ret;
797                 }
798
799                 rtd->pcm = be_pcm;
800                 rtd->fe_compr = 1;
801                 if (rtd->dai_link->dpcm_playback)
802                         be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
803                 else if (rtd->dai_link->dpcm_capture)
804                         be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
805                 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
806         } else {
807                 snprintf(new_name, sizeof(new_name), "%s %s-%d",
808                         rtd->dai_link->stream_name, codec_dai->name, num);
809
810                 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
811         }
812
813         for_each_rtd_components(rtd, i, component) {
814                 if (!component->driver->compress_ops ||
815                     !component->driver->compress_ops->copy)
816                         continue;
817
818                 compr->ops->copy = soc_compr_copy;
819                 break;
820         }
821
822         mutex_init(&compr->lock);
823         ret = snd_compress_new(rtd->card->snd_card, num, direction,
824                                 new_name, compr);
825         if (ret < 0) {
826                 component = asoc_rtd_to_codec(rtd, 0)->component;
827                 dev_err(component->dev,
828                         "Compress ASoC: can't create compress for codec %s: %d\n",
829                         component->name, ret);
830                 return ret;
831         }
832
833         /* DAPM dai link stream work */
834         rtd->close_delayed_work_func = snd_soc_close_delayed_work;
835
836         rtd->compr = compr;
837         compr->private_data = rtd;
838
839         dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
840                 codec_dai->name, cpu_dai->name);
841
842         return 0;
843 }
844 EXPORT_SYMBOL_GPL(snd_soc_new_compress);