ASoC: soc-compress: convert to SPDX identifiers
[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
23 static int soc_compr_components_open(struct snd_compr_stream *cstream,
24                                      struct snd_soc_component **last)
25 {
26         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
27         struct snd_soc_component *component;
28         struct snd_soc_rtdcom_list *rtdcom;
29         int ret;
30
31         for_each_rtdcom(rtd, rtdcom) {
32                 component = rtdcom->component;
33
34                 if (!component->driver->compr_ops ||
35                     !component->driver->compr_ops->open)
36                         continue;
37
38                 ret = component->driver->compr_ops->open(cstream);
39                 if (ret < 0) {
40                         dev_err(component->dev,
41                                 "Compress ASoC: can't open platform %s: %d\n",
42                                 component->name, ret);
43
44                         *last = component;
45                         return ret;
46                 }
47         }
48
49         *last = NULL;
50         return 0;
51 }
52
53 static int soc_compr_components_free(struct snd_compr_stream *cstream,
54                                      struct snd_soc_component *last)
55 {
56         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
57         struct snd_soc_component *component;
58         struct snd_soc_rtdcom_list *rtdcom;
59
60         for_each_rtdcom(rtd, rtdcom) {
61                 component = rtdcom->component;
62
63                 if (component == last)
64                         break;
65
66                 if (!component->driver->compr_ops ||
67                     !component->driver->compr_ops->free)
68                         continue;
69
70                 component->driver->compr_ops->free(cstream);
71         }
72
73         return 0;
74 }
75
76 static int soc_compr_open(struct snd_compr_stream *cstream)
77 {
78         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
79         struct snd_soc_component *component;
80         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
81         int ret;
82
83         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
84
85         if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
86                 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
87                 if (ret < 0) {
88                         dev_err(cpu_dai->dev,
89                                 "Compress ASoC: can't open interface %s: %d\n",
90                                 cpu_dai->name, ret);
91                         goto out;
92                 }
93         }
94
95         ret = soc_compr_components_open(cstream, &component);
96         if (ret < 0)
97                 goto machine_err;
98
99         if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
100                 ret = rtd->dai_link->compr_ops->startup(cstream);
101                 if (ret < 0) {
102                         dev_err(rtd->dev,
103                                 "Compress ASoC: %s startup failed: %d\n",
104                                 rtd->dai_link->name, ret);
105                         goto machine_err;
106                 }
107         }
108
109         snd_soc_runtime_activate(rtd, cstream->direction);
110
111         mutex_unlock(&rtd->pcm_mutex);
112
113         return 0;
114
115 machine_err:
116         soc_compr_components_free(cstream, component);
117
118         if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
119                 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
120 out:
121         mutex_unlock(&rtd->pcm_mutex);
122         return ret;
123 }
124
125 static int soc_compr_open_fe(struct snd_compr_stream *cstream)
126 {
127         struct snd_soc_pcm_runtime *fe = cstream->private_data;
128         struct snd_pcm_substream *fe_substream =
129                  fe->pcm->streams[cstream->direction].substream;
130         struct snd_soc_component *component;
131         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
132         struct snd_soc_dpcm *dpcm;
133         struct snd_soc_dapm_widget_list *list;
134         int stream;
135         int ret;
136
137         if (cstream->direction == SND_COMPRESS_PLAYBACK)
138                 stream = SNDRV_PCM_STREAM_PLAYBACK;
139         else
140                 stream = SNDRV_PCM_STREAM_CAPTURE;
141
142         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
143
144         if (cpu_dai->driver->cops && cpu_dai->driver->cops->startup) {
145                 ret = cpu_dai->driver->cops->startup(cstream, cpu_dai);
146                 if (ret < 0) {
147                         dev_err(cpu_dai->dev,
148                                 "Compress ASoC: can't open interface %s: %d\n",
149                                 cpu_dai->name, ret);
150                         goto out;
151                 }
152         }
153
154         ret = soc_compr_components_open(cstream, &component);
155         if (ret < 0)
156                 goto machine_err;
157
158         if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
159                 ret = fe->dai_link->compr_ops->startup(cstream);
160                 if (ret < 0) {
161                         pr_err("Compress ASoC: %s startup failed: %d\n",
162                                fe->dai_link->name, ret);
163                         goto machine_err;
164                 }
165         }
166
167         fe->dpcm[stream].runtime = fe_substream->runtime;
168
169         ret = dpcm_path_get(fe, stream, &list);
170         if (ret < 0)
171                 goto fe_err;
172         else if (ret == 0)
173                 dev_dbg(fe->dev, "Compress ASoC: %s no valid %s route\n",
174                         fe->dai_link->name, stream ? "capture" : "playback");
175
176         /* calculate valid and active FE <-> BE dpcms */
177         dpcm_process_paths(fe, stream, &list, 1);
178
179         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
180
181         ret = dpcm_be_dai_startup(fe, stream);
182         if (ret < 0) {
183                 /* clean up all links */
184                 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
185                         dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
186
187                 dpcm_be_disconnect(fe, stream);
188                 fe->dpcm[stream].runtime = NULL;
189                 goto path_err;
190         }
191
192         dpcm_clear_pending_state(fe, stream);
193         dpcm_path_put(&list);
194
195         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
196         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
197
198         snd_soc_runtime_activate(fe, stream);
199
200         mutex_unlock(&fe->card->mutex);
201
202         return 0;
203
204 path_err:
205         dpcm_path_put(&list);
206 fe_err:
207         if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
208                 fe->dai_link->compr_ops->shutdown(cstream);
209 machine_err:
210         soc_compr_components_free(cstream, component);
211
212         if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
213                 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
214 out:
215         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
216         mutex_unlock(&fe->card->mutex);
217         return ret;
218 }
219
220 /*
221  * Power down the audio subsystem pmdown_time msecs after close is called.
222  * This is to ensure there are no pops or clicks in between any music tracks
223  * due to DAPM power cycling.
224  */
225 static void close_delayed_work(struct work_struct *work)
226 {
227         struct snd_soc_pcm_runtime *rtd =
228                         container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
229         struct snd_soc_dai *codec_dai = rtd->codec_dai;
230
231         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
232
233         dev_dbg(rtd->dev,
234                 "Compress ASoC: pop wq checking: %s status: %s waiting: %s\n",
235                 codec_dai->driver->playback.stream_name,
236                 codec_dai->playback_active ? "active" : "inactive",
237                 rtd->pop_wait ? "yes" : "no");
238
239         /* are we waiting on this codec DAI stream */
240         if (rtd->pop_wait == 1) {
241                 rtd->pop_wait = 0;
242                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
243                                           SND_SOC_DAPM_STREAM_STOP);
244         }
245
246         mutex_unlock(&rtd->pcm_mutex);
247 }
248
249 static int soc_compr_free(struct snd_compr_stream *cstream)
250 {
251         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
252         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
253         struct snd_soc_dai *codec_dai = rtd->codec_dai;
254         int stream;
255
256         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
257
258         if (cstream->direction == SND_COMPRESS_PLAYBACK)
259                 stream = SNDRV_PCM_STREAM_PLAYBACK;
260         else
261                 stream = SNDRV_PCM_STREAM_CAPTURE;
262
263         snd_soc_runtime_deactivate(rtd, stream);
264
265         snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
266
267         if (!cpu_dai->active)
268                 cpu_dai->rate = 0;
269
270         if (!codec_dai->active)
271                 codec_dai->rate = 0;
272
273         if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
274                 rtd->dai_link->compr_ops->shutdown(cstream);
275
276         soc_compr_components_free(cstream, NULL);
277
278         if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
279                 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
280
281         if (cstream->direction == SND_COMPRESS_PLAYBACK) {
282                 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
283                         snd_soc_dapm_stream_event(rtd,
284                                                   SNDRV_PCM_STREAM_PLAYBACK,
285                                                   SND_SOC_DAPM_STREAM_STOP);
286                 } else {
287                         rtd->pop_wait = 1;
288                         queue_delayed_work(system_power_efficient_wq,
289                                            &rtd->delayed_work,
290                                            msecs_to_jiffies(rtd->pmdown_time));
291                 }
292         } else {
293                 /* capture streams can be powered down now */
294                 snd_soc_dapm_stream_event(rtd,
295                                           SNDRV_PCM_STREAM_CAPTURE,
296                                           SND_SOC_DAPM_STREAM_STOP);
297         }
298
299         mutex_unlock(&rtd->pcm_mutex);
300         return 0;
301 }
302
303 static int soc_compr_free_fe(struct snd_compr_stream *cstream)
304 {
305         struct snd_soc_pcm_runtime *fe = cstream->private_data;
306         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
307         struct snd_soc_dpcm *dpcm;
308         int stream, ret;
309
310         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
311
312         if (cstream->direction == SND_COMPRESS_PLAYBACK)
313                 stream = SNDRV_PCM_STREAM_PLAYBACK;
314         else
315                 stream = SNDRV_PCM_STREAM_CAPTURE;
316
317         snd_soc_runtime_deactivate(fe, stream);
318
319         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
320
321         ret = dpcm_be_dai_hw_free(fe, stream);
322         if (ret < 0)
323                 dev_err(fe->dev, "Compressed ASoC: hw_free failed: %d\n", ret);
324
325         ret = dpcm_be_dai_shutdown(fe, stream);
326
327         /* mark FE's links ready to prune */
328         list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
329                 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
330
331         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
332
333         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
334         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
335
336         dpcm_be_disconnect(fe, stream);
337
338         fe->dpcm[stream].runtime = NULL;
339
340         if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
341                 fe->dai_link->compr_ops->shutdown(cstream);
342
343         soc_compr_components_free(cstream, NULL);
344
345         if (cpu_dai->driver->cops && cpu_dai->driver->cops->shutdown)
346                 cpu_dai->driver->cops->shutdown(cstream, cpu_dai);
347
348         mutex_unlock(&fe->card->mutex);
349         return 0;
350 }
351
352 static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
353 {
354
355         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
356         struct snd_soc_component *component;
357         struct snd_soc_rtdcom_list *rtdcom;
358         struct snd_soc_dai *codec_dai = rtd->codec_dai;
359         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
360         int ret = 0, __ret;
361
362         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
363
364         for_each_rtdcom(rtd, rtdcom) {
365                 component = rtdcom->component;
366
367                 if (!component->driver->compr_ops ||
368                     !component->driver->compr_ops->trigger)
369                         continue;
370
371                 __ret = component->driver->compr_ops->trigger(cstream, cmd);
372                 if (__ret < 0)
373                         ret = __ret;
374         }
375         if (ret < 0)
376                 goto out;
377
378         if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger)
379                 cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
380
381         switch (cmd) {
382         case SNDRV_PCM_TRIGGER_START:
383                 snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
384                 break;
385         case SNDRV_PCM_TRIGGER_STOP:
386                 snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
387                 break;
388         }
389
390 out:
391         mutex_unlock(&rtd->pcm_mutex);
392         return ret;
393 }
394
395 static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
396 {
397         struct snd_soc_pcm_runtime *fe = cstream->private_data;
398         struct snd_soc_component *component;
399         struct snd_soc_rtdcom_list *rtdcom;
400         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
401         int ret = 0, __ret, stream;
402
403         if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
404                 cmd == SND_COMPR_TRIGGER_DRAIN) {
405
406                 for_each_rtdcom(fe, rtdcom) {
407                         component = rtdcom->component;
408
409                         if (!component->driver->compr_ops ||
410                             !component->driver->compr_ops->trigger)
411                                 continue;
412
413                         __ret = component->driver->compr_ops->trigger(cstream, cmd);
414                         if (__ret < 0)
415                                 ret = __ret;
416                 }
417                 return ret;
418         }
419
420         if (cstream->direction == SND_COMPRESS_PLAYBACK)
421                 stream = SNDRV_PCM_STREAM_PLAYBACK;
422         else
423                 stream = SNDRV_PCM_STREAM_CAPTURE;
424
425         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
426
427         if (cpu_dai->driver->cops && cpu_dai->driver->cops->trigger) {
428                 ret = cpu_dai->driver->cops->trigger(cstream, cmd, cpu_dai);
429                 if (ret < 0)
430                         goto out;
431         }
432
433         for_each_rtdcom(fe, rtdcom) {
434                 component = rtdcom->component;
435
436                 if (!component->driver->compr_ops ||
437                     !component->driver->compr_ops->trigger)
438                         continue;
439
440                 __ret = component->driver->compr_ops->trigger(cstream, cmd);
441                 if (__ret < 0)
442                         ret = __ret;
443         }
444         if (ret < 0)
445                 goto out;
446
447         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
448
449         ret = dpcm_be_dai_trigger(fe, stream, cmd);
450
451         switch (cmd) {
452         case SNDRV_PCM_TRIGGER_START:
453         case SNDRV_PCM_TRIGGER_RESUME:
454         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
455                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
456                 break;
457         case SNDRV_PCM_TRIGGER_STOP:
458         case SNDRV_PCM_TRIGGER_SUSPEND:
459                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
460                 break;
461         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
462                 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
463                 break;
464         }
465
466 out:
467         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
468         mutex_unlock(&fe->card->mutex);
469         return ret;
470 }
471
472 static int soc_compr_set_params(struct snd_compr_stream *cstream,
473                                         struct snd_compr_params *params)
474 {
475         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
476         struct snd_soc_component *component;
477         struct snd_soc_rtdcom_list *rtdcom;
478         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
479         int ret = 0, __ret;
480
481         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
482
483         /*
484          * First we call set_params for the CPU DAI, then the component
485          * driver this should configure the SoC side. If the machine has
486          * compressed ops then we call that as well. The expectation is
487          * that these callbacks will configure everything for this compress
488          * path, like configuring a PCM port for a CODEC.
489          */
490         if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
491                 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
492                 if (ret < 0)
493                         goto err;
494         }
495
496         for_each_rtdcom(rtd, rtdcom) {
497                 component = rtdcom->component;
498
499                 if (!component->driver->compr_ops ||
500                     !component->driver->compr_ops->set_params)
501                         continue;
502
503                 __ret = component->driver->compr_ops->set_params(cstream, params);
504                 if (__ret < 0)
505                         ret = __ret;
506         }
507         if (ret < 0)
508                 goto err;
509
510         if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
511                 ret = rtd->dai_link->compr_ops->set_params(cstream);
512                 if (ret < 0)
513                         goto err;
514         }
515
516         if (cstream->direction == SND_COMPRESS_PLAYBACK)
517                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
518                                           SND_SOC_DAPM_STREAM_START);
519         else
520                 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
521                                           SND_SOC_DAPM_STREAM_START);
522
523         /* cancel any delayed stream shutdown that is pending */
524         rtd->pop_wait = 0;
525         mutex_unlock(&rtd->pcm_mutex);
526
527         cancel_delayed_work_sync(&rtd->delayed_work);
528
529         return ret;
530
531 err:
532         mutex_unlock(&rtd->pcm_mutex);
533         return ret;
534 }
535
536 static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
537                                    struct snd_compr_params *params)
538 {
539         struct snd_soc_pcm_runtime *fe = cstream->private_data;
540         struct snd_pcm_substream *fe_substream =
541                  fe->pcm->streams[cstream->direction].substream;
542         struct snd_soc_component *component;
543         struct snd_soc_rtdcom_list *rtdcom;
544         struct snd_soc_dai *cpu_dai = fe->cpu_dai;
545         int ret = 0, __ret, stream;
546
547         if (cstream->direction == SND_COMPRESS_PLAYBACK)
548                 stream = SNDRV_PCM_STREAM_PLAYBACK;
549         else
550                 stream = SNDRV_PCM_STREAM_CAPTURE;
551
552         mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
553
554         if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_params) {
555                 ret = cpu_dai->driver->cops->set_params(cstream, params, cpu_dai);
556                 if (ret < 0)
557                         goto out;
558         }
559
560         for_each_rtdcom(fe, rtdcom) {
561                 component = rtdcom->component;
562
563                 if (!component->driver->compr_ops ||
564                     !component->driver->compr_ops->set_params)
565                         continue;
566
567                 __ret = component->driver->compr_ops->set_params(cstream, params);
568                 if (__ret < 0)
569                         ret = __ret;
570         }
571         if (ret < 0)
572                 goto out;
573
574         if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
575                 ret = fe->dai_link->compr_ops->set_params(cstream);
576                 if (ret < 0)
577                         goto out;
578         }
579
580         /*
581          * Create an empty hw_params for the BE as the machine driver must
582          * fix this up to match DSP decoder and ASRC configuration.
583          * I.e. machine driver fixup for compressed BE is mandatory.
584          */
585         memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
586                 sizeof(struct snd_pcm_hw_params));
587
588         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
589
590         ret = dpcm_be_dai_hw_params(fe, stream);
591         if (ret < 0)
592                 goto out;
593
594         ret = dpcm_be_dai_prepare(fe, stream);
595         if (ret < 0)
596                 goto out;
597
598         dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
599         fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
600
601 out:
602         fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
603         mutex_unlock(&fe->card->mutex);
604         return ret;
605 }
606
607 static int soc_compr_get_params(struct snd_compr_stream *cstream,
608                                 struct snd_codec *params)
609 {
610         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
611         struct snd_soc_component *component;
612         struct snd_soc_rtdcom_list *rtdcom;
613         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
614         int ret = 0, __ret;
615
616         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
617
618         if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_params) {
619                 ret = cpu_dai->driver->cops->get_params(cstream, params, cpu_dai);
620                 if (ret < 0)
621                         goto err;
622         }
623
624         for_each_rtdcom(rtd, rtdcom) {
625                 component = rtdcom->component;
626
627                 if (!component->driver->compr_ops ||
628                     !component->driver->compr_ops->get_params)
629                         continue;
630
631                 __ret = component->driver->compr_ops->get_params(cstream, params);
632                 if (__ret < 0)
633                         ret = __ret;
634         }
635
636 err:
637         mutex_unlock(&rtd->pcm_mutex);
638         return ret;
639 }
640
641 static int soc_compr_get_caps(struct snd_compr_stream *cstream,
642                               struct snd_compr_caps *caps)
643 {
644         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
645         struct snd_soc_component *component;
646         struct snd_soc_rtdcom_list *rtdcom;
647         int ret = 0, __ret;
648
649         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
650
651         for_each_rtdcom(rtd, rtdcom) {
652                 component = rtdcom->component;
653
654                 if (!component->driver->compr_ops ||
655                     !component->driver->compr_ops->get_caps)
656                         continue;
657
658                 __ret = component->driver->compr_ops->get_caps(cstream, caps);
659                 if (__ret < 0)
660                         ret = __ret;
661         }
662
663         mutex_unlock(&rtd->pcm_mutex);
664         return ret;
665 }
666
667 static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
668                                     struct snd_compr_codec_caps *codec)
669 {
670         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
671         struct snd_soc_component *component;
672         struct snd_soc_rtdcom_list *rtdcom;
673         int ret = 0, __ret;
674
675         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
676
677         for_each_rtdcom(rtd, rtdcom) {
678                 component = rtdcom->component;
679
680                 if (!component->driver->compr_ops ||
681                     !component->driver->compr_ops->get_codec_caps)
682                         continue;
683
684                 __ret = component->driver->compr_ops->get_codec_caps(cstream, codec);
685                 if (__ret < 0)
686                         ret = __ret;
687         }
688
689         mutex_unlock(&rtd->pcm_mutex);
690         return ret;
691 }
692
693 static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
694 {
695         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
696         struct snd_soc_component *component;
697         struct snd_soc_rtdcom_list *rtdcom;
698         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
699         int ret = 0, __ret;
700
701         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
702
703         if (cpu_dai->driver->cops && cpu_dai->driver->cops->ack) {
704                 ret = cpu_dai->driver->cops->ack(cstream, bytes, cpu_dai);
705                 if (ret < 0)
706                         goto err;
707         }
708
709         for_each_rtdcom(rtd, rtdcom) {
710                 component = rtdcom->component;
711
712                 if (!component->driver->compr_ops ||
713                     !component->driver->compr_ops->ack)
714                         continue;
715
716                 __ret = component->driver->compr_ops->ack(cstream, bytes);
717                 if (__ret < 0)
718                         ret = __ret;
719         }
720
721 err:
722         mutex_unlock(&rtd->pcm_mutex);
723         return ret;
724 }
725
726 static int soc_compr_pointer(struct snd_compr_stream *cstream,
727                              struct snd_compr_tstamp *tstamp)
728 {
729         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
730         struct snd_soc_component *component;
731         struct snd_soc_rtdcom_list *rtdcom;
732         int ret = 0, __ret;
733         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
734
735         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
736
737         if (cpu_dai->driver->cops && cpu_dai->driver->cops->pointer)
738                 cpu_dai->driver->cops->pointer(cstream, tstamp, cpu_dai);
739
740         for_each_rtdcom(rtd, rtdcom) {
741                 component = rtdcom->component;
742
743                 if (!component->driver->compr_ops ||
744                     !component->driver->compr_ops->pointer)
745                         continue;
746
747                 __ret = component->driver->compr_ops->pointer(cstream, tstamp);
748                 if (__ret < 0)
749                         ret = __ret;
750         }
751
752         mutex_unlock(&rtd->pcm_mutex);
753         return ret;
754 }
755
756 static int soc_compr_copy(struct snd_compr_stream *cstream,
757                           char __user *buf, size_t count)
758 {
759         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
760         struct snd_soc_component *component;
761         struct snd_soc_rtdcom_list *rtdcom;
762         int ret = 0;
763
764         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
765
766         for_each_rtdcom(rtd, rtdcom) {
767                 component = rtdcom->component;
768
769                 if (!component->driver->compr_ops ||
770                     !component->driver->compr_ops->copy)
771                         continue;
772
773                 ret = component->driver->compr_ops->copy(cstream, buf, count);
774                 break;
775         }
776
777         mutex_unlock(&rtd->pcm_mutex);
778         return ret;
779 }
780
781 static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
782                                   struct snd_compr_metadata *metadata)
783 {
784         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
785         struct snd_soc_component *component;
786         struct snd_soc_rtdcom_list *rtdcom;
787         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
788         int ret = 0, __ret;
789
790         if (cpu_dai->driver->cops && cpu_dai->driver->cops->set_metadata) {
791                 ret = cpu_dai->driver->cops->set_metadata(cstream, metadata, cpu_dai);
792                 if (ret < 0)
793                         return ret;
794         }
795
796         for_each_rtdcom(rtd, rtdcom) {
797                 component = rtdcom->component;
798
799                 if (!component->driver->compr_ops ||
800                     !component->driver->compr_ops->set_metadata)
801                         continue;
802
803                 __ret = component->driver->compr_ops->set_metadata(cstream, metadata);
804                 if (__ret < 0)
805                         ret = __ret;
806         }
807
808         return ret;
809 }
810
811 static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
812                                   struct snd_compr_metadata *metadata)
813 {
814         struct snd_soc_pcm_runtime *rtd = cstream->private_data;
815         struct snd_soc_component *component;
816         struct snd_soc_rtdcom_list *rtdcom;
817         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
818         int ret = 0, __ret;
819
820         if (cpu_dai->driver->cops && cpu_dai->driver->cops->get_metadata) {
821                 ret = cpu_dai->driver->cops->get_metadata(cstream, metadata, cpu_dai);
822                 if (ret < 0)
823                         return ret;
824         }
825
826         for_each_rtdcom(rtd, rtdcom) {
827                 component = rtdcom->component;
828
829                 if (!component->driver->compr_ops ||
830                     !component->driver->compr_ops->get_metadata)
831                         continue;
832
833                 __ret = component->driver->compr_ops->get_metadata(cstream, metadata);
834                 if (__ret < 0)
835                         ret = __ret;
836         }
837
838         return ret;
839 }
840
841 /* ASoC Compress operations */
842 static struct snd_compr_ops soc_compr_ops = {
843         .open           = soc_compr_open,
844         .free           = soc_compr_free,
845         .set_params     = soc_compr_set_params,
846         .set_metadata   = soc_compr_set_metadata,
847         .get_metadata   = soc_compr_get_metadata,
848         .get_params     = soc_compr_get_params,
849         .trigger        = soc_compr_trigger,
850         .pointer        = soc_compr_pointer,
851         .ack            = soc_compr_ack,
852         .get_caps       = soc_compr_get_caps,
853         .get_codec_caps = soc_compr_get_codec_caps
854 };
855
856 /* ASoC Dynamic Compress operations */
857 static struct snd_compr_ops soc_compr_dyn_ops = {
858         .open           = soc_compr_open_fe,
859         .free           = soc_compr_free_fe,
860         .set_params     = soc_compr_set_params_fe,
861         .get_params     = soc_compr_get_params,
862         .set_metadata   = soc_compr_set_metadata,
863         .get_metadata   = soc_compr_get_metadata,
864         .trigger        = soc_compr_trigger_fe,
865         .pointer        = soc_compr_pointer,
866         .ack            = soc_compr_ack,
867         .get_caps       = soc_compr_get_caps,
868         .get_codec_caps = soc_compr_get_codec_caps
869 };
870
871 /**
872  * snd_soc_new_compress - create a new compress.
873  *
874  * @rtd: The runtime for which we will create compress
875  * @num: the device index number (zero based - shared with normal PCMs)
876  *
877  * Return: 0 for success, else error.
878  */
879 int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
880 {
881         struct snd_soc_component *component;
882         struct snd_soc_rtdcom_list *rtdcom;
883         struct snd_soc_dai *codec_dai = rtd->codec_dai;
884         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
885         struct snd_compr *compr;
886         struct snd_pcm *be_pcm;
887         char new_name[64];
888         int ret = 0, direction = 0;
889         int playback = 0, capture = 0;
890
891         if (rtd->num_codecs > 1) {
892                 dev_err(rtd->card->dev,
893                         "Compress ASoC: Multicodec not supported\n");
894                 return -EINVAL;
895         }
896
897         /* check client and interface hw capabilities */
898         if (codec_dai->driver->playback.channels_min)
899                 playback = 1;
900         if (codec_dai->driver->capture.channels_min)
901                 capture = 1;
902
903         capture = capture && cpu_dai->driver->capture.channels_min;
904         playback = playback && cpu_dai->driver->playback.channels_min;
905
906         /*
907          * Compress devices are unidirectional so only one of the directions
908          * should be set, check for that (xor)
909          */
910         if (playback + capture != 1) {
911                 dev_err(rtd->card->dev,
912                         "Compress ASoC: Invalid direction for P %d, C %d\n",
913                         playback, capture);
914                 return -EINVAL;
915         }
916
917         if (playback)
918                 direction = SND_COMPRESS_PLAYBACK;
919         else
920                 direction = SND_COMPRESS_CAPTURE;
921
922         compr = kzalloc(sizeof(*compr), GFP_KERNEL);
923         if (!compr)
924                 return -ENOMEM;
925
926         compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
927                                   GFP_KERNEL);
928         if (!compr->ops) {
929                 ret = -ENOMEM;
930                 goto compr_err;
931         }
932
933         if (rtd->dai_link->dynamic) {
934                 snprintf(new_name, sizeof(new_name), "(%s)",
935                         rtd->dai_link->stream_name);
936
937                 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
938                                 rtd->dai_link->dpcm_playback,
939                                 rtd->dai_link->dpcm_capture, &be_pcm);
940                 if (ret < 0) {
941                         dev_err(rtd->card->dev,
942                                 "Compress ASoC: can't create compressed for %s: %d\n",
943                                 rtd->dai_link->name, ret);
944                         goto compr_err;
945                 }
946
947                 rtd->pcm = be_pcm;
948                 rtd->fe_compr = 1;
949                 if (rtd->dai_link->dpcm_playback)
950                         be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
951                 else if (rtd->dai_link->dpcm_capture)
952                         be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
953                 memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
954         } else {
955                 snprintf(new_name, sizeof(new_name), "%s %s-%d",
956                         rtd->dai_link->stream_name, codec_dai->name, num);
957
958                 memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
959         }
960
961         for_each_rtdcom(rtd, rtdcom) {
962                 component = rtdcom->component;
963
964                 if (!component->driver->compr_ops ||
965                     !component->driver->compr_ops->copy)
966                         continue;
967
968                 compr->ops->copy = soc_compr_copy;
969                 break;
970         }
971
972         mutex_init(&compr->lock);
973         ret = snd_compress_new(rtd->card->snd_card, num, direction,
974                                 new_name, compr);
975         if (ret < 0) {
976                 component = rtd->codec_dai->component;
977                 dev_err(component->dev,
978                         "Compress ASoC: can't create compress for codec %s: %d\n",
979                         component->name, ret);
980                 goto compr_err;
981         }
982
983         /* DAPM dai link stream work */
984         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
985
986         rtd->compr = compr;
987         compr->private_data = rtd;
988
989         dev_info(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
990                  codec_dai->name, cpu_dai->name);
991         return ret;
992
993 compr_err:
994         kfree(compr);
995         return ret;
996 }
997 EXPORT_SYMBOL_GPL(snd_soc_new_compress);