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