]>
Commit | Line | Data |
---|---|---|
1245b700 NK |
1 | /* |
2 | * soc-compress.c -- ALSA SoC Compress | |
3 | * | |
4 | * Copyright (C) 2012 Intel Corp. | |
5 | * | |
6 | * Authors: Namarta Kohli <namartax.kohli@intel.com> | |
7 | * Ramesh Babu K V <ramesh.babu@linux.intel.com> | |
8 | * Vinod Koul <vinod.koul@linux.intel.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | * | |
15 | */ | |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/delay.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/workqueue.h> | |
22 | #include <sound/core.h> | |
23 | #include <sound/compress_params.h> | |
24 | #include <sound/compress_driver.h> | |
25 | #include <sound/soc.h> | |
26 | #include <sound/initval.h> | |
2a99ef0f | 27 | #include <sound/soc-dpcm.h> |
1245b700 NK |
28 | |
29 | static int soc_compr_open(struct snd_compr_stream *cstream) | |
30 | { | |
31 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
32 | struct snd_soc_platform *platform = rtd->platform; | |
33 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
34 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
35 | int ret = 0; | |
36 | ||
15e2e619 CK |
37 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
38 | ||
1245b700 NK |
39 | if (platform->driver->compr_ops && platform->driver->compr_ops->open) { |
40 | ret = platform->driver->compr_ops->open(cstream); | |
41 | if (ret < 0) { | |
42 | pr_err("compress asoc: can't open platform %s\n", platform->name); | |
43 | goto out; | |
44 | } | |
45 | } | |
46 | ||
47 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) { | |
48 | ret = rtd->dai_link->compr_ops->startup(cstream); | |
49 | if (ret < 0) { | |
50 | pr_err("compress asoc: %s startup failed\n", rtd->dai_link->name); | |
51 | goto machine_err; | |
52 | } | |
53 | } | |
54 | ||
55 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { | |
56 | cpu_dai->playback_active++; | |
57 | codec_dai->playback_active++; | |
58 | } else { | |
59 | cpu_dai->capture_active++; | |
60 | codec_dai->capture_active++; | |
61 | } | |
62 | ||
63 | cpu_dai->active++; | |
64 | codec_dai->active++; | |
65 | rtd->codec->active++; | |
66 | ||
15e2e619 CK |
67 | mutex_unlock(&rtd->pcm_mutex); |
68 | ||
1245b700 NK |
69 | return 0; |
70 | ||
71 | machine_err: | |
72 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) | |
73 | platform->driver->compr_ops->free(cstream); | |
74 | out: | |
15e2e619 | 75 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
76 | return ret; |
77 | } | |
78 | ||
2a99ef0f LG |
79 | static int soc_compr_open_fe(struct snd_compr_stream *cstream) |
80 | { | |
81 | struct snd_soc_pcm_runtime *fe = cstream->private_data; | |
82 | struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream; | |
83 | struct snd_soc_platform *platform = fe->platform; | |
84 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; | |
85 | struct snd_soc_dai *codec_dai = fe->codec_dai; | |
86 | struct snd_soc_dpcm *dpcm; | |
87 | struct snd_soc_dapm_widget_list *list; | |
88 | int stream; | |
89 | int ret = 0; | |
90 | ||
91 | if (cstream->direction == SND_COMPRESS_PLAYBACK) | |
92 | stream = SNDRV_PCM_STREAM_PLAYBACK; | |
93 | else | |
94 | stream = SNDRV_PCM_STREAM_CAPTURE; | |
95 | ||
96 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
97 | ||
98 | if (platform->driver->compr_ops && platform->driver->compr_ops->open) { | |
99 | ret = platform->driver->compr_ops->open(cstream); | |
100 | if (ret < 0) { | |
101 | pr_err("compress asoc: can't open platform %s\n", platform->name); | |
102 | goto out; | |
103 | } | |
104 | } | |
105 | ||
106 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) { | |
107 | ret = fe->dai_link->compr_ops->startup(cstream); | |
108 | if (ret < 0) { | |
109 | pr_err("compress asoc: %s startup failed\n", fe->dai_link->name); | |
110 | goto machine_err; | |
111 | } | |
112 | } | |
113 | ||
114 | fe->dpcm[stream].runtime = fe_substream->runtime; | |
115 | ||
116 | if (dpcm_path_get(fe, stream, &list) <= 0) { | |
117 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", | |
118 | fe->dai_link->name, stream ? "capture" : "playback"); | |
119 | } | |
120 | ||
121 | /* calculate valid and active FE <-> BE dpcms */ | |
122 | dpcm_process_paths(fe, stream, &list, 1); | |
123 | ||
124 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
125 | ||
126 | ret = dpcm_be_dai_startup(fe, stream); | |
127 | if (ret < 0) { | |
128 | /* clean up all links */ | |
129 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) | |
130 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
131 | ||
132 | dpcm_be_disconnect(fe, stream); | |
133 | fe->dpcm[stream].runtime = NULL; | |
134 | goto fe_err; | |
135 | } | |
136 | ||
137 | dpcm_clear_pending_state(fe, stream); | |
138 | dpcm_path_put(&list); | |
139 | ||
140 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; | |
141 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
142 | ||
143 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { | |
144 | cpu_dai->playback_active++; | |
145 | codec_dai->playback_active++; | |
146 | } else { | |
147 | cpu_dai->capture_active++; | |
148 | codec_dai->capture_active++; | |
149 | } | |
150 | ||
151 | cpu_dai->active++; | |
152 | codec_dai->active++; | |
153 | fe->codec->active++; | |
154 | ||
155 | mutex_unlock(&fe->card->mutex); | |
156 | ||
157 | return 0; | |
158 | ||
159 | fe_err: | |
160 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown) | |
161 | fe->dai_link->compr_ops->shutdown(cstream); | |
162 | machine_err: | |
163 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) | |
164 | platform->driver->compr_ops->free(cstream); | |
165 | out: | |
166 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
167 | mutex_unlock(&fe->card->mutex); | |
168 | return ret; | |
169 | } | |
170 | ||
202c8f70 CK |
171 | /* |
172 | * Power down the audio subsystem pmdown_time msecs after close is called. | |
173 | * This is to ensure there are no pops or clicks in between any music tracks | |
174 | * due to DAPM power cycling. | |
175 | */ | |
176 | static void close_delayed_work(struct work_struct *work) | |
177 | { | |
178 | struct snd_soc_pcm_runtime *rtd = | |
179 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); | |
180 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
181 | ||
182 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); | |
183 | ||
184 | dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n", | |
185 | codec_dai->driver->playback.stream_name, | |
186 | codec_dai->playback_active ? "active" : "inactive", | |
187 | rtd->pop_wait ? "yes" : "no"); | |
188 | ||
189 | /* are we waiting on this codec DAI stream */ | |
190 | if (rtd->pop_wait == 1) { | |
191 | rtd->pop_wait = 0; | |
192 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, | |
193 | SND_SOC_DAPM_STREAM_STOP); | |
194 | } | |
195 | ||
196 | mutex_unlock(&rtd->pcm_mutex); | |
197 | } | |
198 | ||
1245b700 NK |
199 | static int soc_compr_free(struct snd_compr_stream *cstream) |
200 | { | |
201 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
202 | struct snd_soc_platform *platform = rtd->platform; | |
203 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
204 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
205 | struct snd_soc_codec *codec = rtd->codec; | |
206 | ||
15e2e619 CK |
207 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
208 | ||
1245b700 NK |
209 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
210 | cpu_dai->playback_active--; | |
211 | codec_dai->playback_active--; | |
212 | } else { | |
213 | cpu_dai->capture_active--; | |
214 | codec_dai->capture_active--; | |
215 | } | |
216 | ||
da18396f MB |
217 | snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction); |
218 | ||
1245b700 NK |
219 | cpu_dai->active--; |
220 | codec_dai->active--; | |
221 | codec->active--; | |
222 | ||
223 | if (!cpu_dai->active) | |
224 | cpu_dai->rate = 0; | |
225 | ||
226 | if (!codec_dai->active) | |
227 | codec_dai->rate = 0; | |
228 | ||
229 | ||
230 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown) | |
231 | rtd->dai_link->compr_ops->shutdown(cstream); | |
232 | ||
233 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) | |
234 | platform->driver->compr_ops->free(cstream); | |
9d2667a9 | 235 | cpu_dai->runtime = NULL; |
1245b700 NK |
236 | |
237 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { | |
208a1589 | 238 | if (snd_soc_runtime_ignore_pmdown_time(rtd)) { |
1245b700 NK |
239 | snd_soc_dapm_stream_event(rtd, |
240 | SNDRV_PCM_STREAM_PLAYBACK, | |
241 | SND_SOC_DAPM_STREAM_STOP); | |
8c3d2aa4 | 242 | } else { |
9bffb1fb | 243 | rtd->pop_wait = 1; |
3d24cfe4 MB |
244 | queue_delayed_work(system_power_efficient_wq, |
245 | &rtd->delayed_work, | |
246 | msecs_to_jiffies(rtd->pmdown_time)); | |
8c3d2aa4 | 247 | } |
1245b700 NK |
248 | } else { |
249 | /* capture streams can be powered down now */ | |
250 | snd_soc_dapm_stream_event(rtd, | |
251 | SNDRV_PCM_STREAM_CAPTURE, | |
252 | SND_SOC_DAPM_STREAM_STOP); | |
253 | } | |
254 | ||
15e2e619 | 255 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
256 | return 0; |
257 | } | |
258 | ||
2a99ef0f LG |
259 | static int soc_compr_free_fe(struct snd_compr_stream *cstream) |
260 | { | |
261 | struct snd_soc_pcm_runtime *fe = cstream->private_data; | |
262 | struct snd_soc_platform *platform = fe->platform; | |
263 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; | |
264 | struct snd_soc_dai *codec_dai = fe->codec_dai; | |
265 | struct snd_soc_dpcm *dpcm; | |
266 | int stream, ret; | |
267 | ||
268 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
269 | ||
270 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { | |
271 | stream = SNDRV_PCM_STREAM_PLAYBACK; | |
272 | cpu_dai->playback_active--; | |
273 | codec_dai->playback_active--; | |
274 | } else { | |
275 | stream = SNDRV_PCM_STREAM_CAPTURE; | |
276 | cpu_dai->capture_active--; | |
277 | codec_dai->capture_active--; | |
278 | } | |
279 | ||
280 | cpu_dai->active--; | |
281 | codec_dai->active--; | |
282 | fe->codec->active--; | |
283 | ||
284 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
285 | ||
286 | ret = dpcm_be_dai_hw_free(fe, stream); | |
287 | if (ret < 0) | |
288 | dev_err(fe->dev, "compressed hw_free failed %d\n", ret); | |
289 | ||
290 | ret = dpcm_be_dai_shutdown(fe, stream); | |
291 | ||
292 | /* mark FE's links ready to prune */ | |
293 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) | |
294 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; | |
295 | ||
296 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
297 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); | |
298 | else | |
299 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); | |
300 | ||
301 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; | |
302 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
303 | ||
304 | dpcm_be_disconnect(fe, stream); | |
305 | ||
306 | fe->dpcm[stream].runtime = NULL; | |
307 | ||
308 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown) | |
309 | fe->dai_link->compr_ops->shutdown(cstream); | |
310 | ||
311 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) | |
312 | platform->driver->compr_ops->free(cstream); | |
313 | ||
314 | mutex_unlock(&fe->card->mutex); | |
315 | return 0; | |
316 | } | |
317 | ||
1245b700 NK |
318 | static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd) |
319 | { | |
320 | ||
321 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
322 | struct snd_soc_platform *platform = rtd->platform; | |
323 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
324 | int ret = 0; | |
325 | ||
15e2e619 CK |
326 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
327 | ||
1245b700 NK |
328 | if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) { |
329 | ret = platform->driver->compr_ops->trigger(cstream, cmd); | |
330 | if (ret < 0) | |
15e2e619 | 331 | goto out; |
1245b700 NK |
332 | } |
333 | ||
da18396f MB |
334 | switch (cmd) { |
335 | case SNDRV_PCM_TRIGGER_START: | |
336 | snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction); | |
337 | break; | |
338 | case SNDRV_PCM_TRIGGER_STOP: | |
339 | snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction); | |
340 | break; | |
e38b9b74 | 341 | } |
1245b700 | 342 | |
15e2e619 CK |
343 | out: |
344 | mutex_unlock(&rtd->pcm_mutex); | |
1245b700 NK |
345 | return ret; |
346 | } | |
347 | ||
2a99ef0f LG |
348 | static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd) |
349 | { | |
350 | struct snd_soc_pcm_runtime *fe = cstream->private_data; | |
351 | struct snd_soc_platform *platform = fe->platform; | |
352 | int ret = 0, stream; | |
353 | ||
354 | if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN || | |
355 | cmd == SND_COMPR_TRIGGER_DRAIN) { | |
356 | ||
357 | if (platform->driver->compr_ops && | |
358 | platform->driver->compr_ops->trigger) | |
359 | return platform->driver->compr_ops->trigger(cstream, cmd); | |
360 | } | |
361 | ||
362 | if (cstream->direction == SND_COMPRESS_PLAYBACK) | |
363 | stream = SNDRV_PCM_STREAM_PLAYBACK; | |
364 | else | |
365 | stream = SNDRV_PCM_STREAM_CAPTURE; | |
366 | ||
367 | ||
368 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
369 | ||
370 | if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) { | |
371 | ret = platform->driver->compr_ops->trigger(cstream, cmd); | |
372 | if (ret < 0) | |
373 | goto out; | |
374 | } | |
375 | ||
376 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
377 | ||
378 | ret = dpcm_be_dai_trigger(fe, stream, cmd); | |
379 | ||
380 | switch (cmd) { | |
381 | case SNDRV_PCM_TRIGGER_START: | |
382 | case SNDRV_PCM_TRIGGER_RESUME: | |
383 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
384 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; | |
385 | break; | |
386 | case SNDRV_PCM_TRIGGER_STOP: | |
387 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
388 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; | |
389 | break; | |
390 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
391 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; | |
392 | break; | |
393 | } | |
394 | ||
395 | out: | |
396 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
397 | mutex_unlock(&fe->card->mutex); | |
398 | return ret; | |
399 | } | |
400 | ||
1245b700 NK |
401 | static int soc_compr_set_params(struct snd_compr_stream *cstream, |
402 | struct snd_compr_params *params) | |
403 | { | |
404 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
405 | struct snd_soc_platform *platform = rtd->platform; | |
1245b700 NK |
406 | int ret = 0; |
407 | ||
15e2e619 CK |
408 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
409 | ||
1245b700 NK |
410 | /* first we call set_params for the platform driver |
411 | * this should configure the soc side | |
412 | * if the machine has compressed ops then we call that as well | |
413 | * expectation is that platform and machine will configure everything | |
414 | * for this compress path, like configuring pcm port for codec | |
415 | */ | |
416 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) { | |
417 | ret = platform->driver->compr_ops->set_params(cstream, params); | |
418 | if (ret < 0) | |
fa40ef20 | 419 | goto err; |
1245b700 NK |
420 | } |
421 | ||
422 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) { | |
423 | ret = rtd->dai_link->compr_ops->set_params(cstream); | |
424 | if (ret < 0) | |
fa40ef20 | 425 | goto err; |
1245b700 NK |
426 | } |
427 | ||
2c071ed7 CK |
428 | if (cstream->direction == SND_COMPRESS_PLAYBACK) |
429 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, | |
430 | SND_SOC_DAPM_STREAM_START); | |
431 | else | |
432 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, | |
433 | SND_SOC_DAPM_STREAM_START); | |
1245b700 | 434 | |
fa40ef20 CK |
435 | /* cancel any delayed stream shutdown that is pending */ |
436 | rtd->pop_wait = 0; | |
437 | mutex_unlock(&rtd->pcm_mutex); | |
438 | ||
439 | cancel_delayed_work_sync(&rtd->delayed_work); | |
440 | ||
441 | return ret; | |
442 | ||
443 | err: | |
15e2e619 | 444 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
445 | return ret; |
446 | } | |
447 | ||
2a99ef0f LG |
448 | static int soc_compr_set_params_fe(struct snd_compr_stream *cstream, |
449 | struct snd_compr_params *params) | |
450 | { | |
451 | struct snd_soc_pcm_runtime *fe = cstream->private_data; | |
452 | struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream; | |
453 | struct snd_soc_platform *platform = fe->platform; | |
454 | int ret = 0, stream; | |
455 | ||
456 | if (cstream->direction == SND_COMPRESS_PLAYBACK) | |
457 | stream = SNDRV_PCM_STREAM_PLAYBACK; | |
458 | else | |
459 | stream = SNDRV_PCM_STREAM_CAPTURE; | |
460 | ||
461 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); | |
462 | ||
463 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) { | |
464 | ret = platform->driver->compr_ops->set_params(cstream, params); | |
465 | if (ret < 0) | |
466 | goto out; | |
467 | } | |
468 | ||
469 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) { | |
470 | ret = fe->dai_link->compr_ops->set_params(cstream); | |
471 | if (ret < 0) | |
472 | goto out; | |
473 | } | |
474 | ||
475 | /* | |
476 | * Create an empty hw_params for the BE as the machine driver must | |
477 | * fix this up to match DSP decoder and ASRC configuration. | |
478 | * I.e. machine driver fixup for compressed BE is mandatory. | |
479 | */ | |
480 | memset(&fe->dpcm[fe_substream->stream].hw_params, 0, | |
481 | sizeof(struct snd_pcm_hw_params)); | |
482 | ||
483 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; | |
484 | ||
485 | ret = dpcm_be_dai_hw_params(fe, stream); | |
486 | if (ret < 0) | |
487 | goto out; | |
488 | ||
489 | ret = dpcm_be_dai_prepare(fe, stream); | |
490 | if (ret < 0) | |
491 | goto out; | |
492 | ||
493 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
494 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); | |
495 | else | |
496 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); | |
497 | ||
498 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; | |
499 | ||
500 | out: | |
501 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; | |
502 | mutex_unlock(&fe->card->mutex); | |
503 | return ret; | |
504 | } | |
505 | ||
1245b700 NK |
506 | static int soc_compr_get_params(struct snd_compr_stream *cstream, |
507 | struct snd_codec *params) | |
508 | { | |
509 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
510 | struct snd_soc_platform *platform = rtd->platform; | |
511 | int ret = 0; | |
512 | ||
15e2e619 CK |
513 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
514 | ||
1245b700 NK |
515 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_params) |
516 | ret = platform->driver->compr_ops->get_params(cstream, params); | |
517 | ||
15e2e619 | 518 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
519 | return ret; |
520 | } | |
521 | ||
522 | static int soc_compr_get_caps(struct snd_compr_stream *cstream, | |
523 | struct snd_compr_caps *caps) | |
524 | { | |
525 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
526 | struct snd_soc_platform *platform = rtd->platform; | |
527 | int ret = 0; | |
528 | ||
15e2e619 CK |
529 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
530 | ||
1245b700 NK |
531 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps) |
532 | ret = platform->driver->compr_ops->get_caps(cstream, caps); | |
533 | ||
15e2e619 | 534 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
535 | return ret; |
536 | } | |
537 | ||
538 | static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream, | |
539 | struct snd_compr_codec_caps *codec) | |
540 | { | |
541 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
542 | struct snd_soc_platform *platform = rtd->platform; | |
543 | int ret = 0; | |
544 | ||
15e2e619 CK |
545 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
546 | ||
1245b700 NK |
547 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps) |
548 | ret = platform->driver->compr_ops->get_codec_caps(cstream, codec); | |
549 | ||
15e2e619 | 550 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
551 | return ret; |
552 | } | |
553 | ||
554 | static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes) | |
555 | { | |
556 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
557 | struct snd_soc_platform *platform = rtd->platform; | |
558 | int ret = 0; | |
559 | ||
15e2e619 CK |
560 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
561 | ||
1245b700 NK |
562 | if (platform->driver->compr_ops && platform->driver->compr_ops->ack) |
563 | ret = platform->driver->compr_ops->ack(cstream, bytes); | |
564 | ||
15e2e619 | 565 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
566 | return ret; |
567 | } | |
568 | ||
569 | static int soc_compr_pointer(struct snd_compr_stream *cstream, | |
570 | struct snd_compr_tstamp *tstamp) | |
571 | { | |
572 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
573 | struct snd_soc_platform *platform = rtd->platform; | |
574 | ||
15e2e619 CK |
575 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
576 | ||
1245b700 NK |
577 | if (platform->driver->compr_ops && platform->driver->compr_ops->pointer) |
578 | platform->driver->compr_ops->pointer(cstream, tstamp); | |
579 | ||
15e2e619 | 580 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
581 | return 0; |
582 | } | |
583 | ||
1f88eb0f | 584 | static int soc_compr_copy(struct snd_compr_stream *cstream, |
4daf891c | 585 | char __user *buf, size_t count) |
1f88eb0f CK |
586 | { |
587 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
588 | struct snd_soc_platform *platform = rtd->platform; | |
589 | int ret = 0; | |
590 | ||
591 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); | |
592 | ||
593 | if (platform->driver->compr_ops && platform->driver->compr_ops->copy) | |
594 | ret = platform->driver->compr_ops->copy(cstream, buf, count); | |
595 | ||
596 | mutex_unlock(&rtd->pcm_mutex); | |
597 | return ret; | |
598 | } | |
599 | ||
02bd90e8 | 600 | static int soc_compr_set_metadata(struct snd_compr_stream *cstream, |
36953d98 JK |
601 | struct snd_compr_metadata *metadata) |
602 | { | |
603 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
604 | struct snd_soc_platform *platform = rtd->platform; | |
605 | int ret = 0; | |
606 | ||
607 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata) | |
608 | ret = platform->driver->compr_ops->set_metadata(cstream, metadata); | |
609 | ||
610 | return ret; | |
611 | } | |
612 | ||
02bd90e8 | 613 | static int soc_compr_get_metadata(struct snd_compr_stream *cstream, |
36953d98 JK |
614 | struct snd_compr_metadata *metadata) |
615 | { | |
616 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
617 | struct snd_soc_platform *platform = rtd->platform; | |
618 | int ret = 0; | |
619 | ||
620 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata) | |
621 | ret = platform->driver->compr_ops->get_metadata(cstream, metadata); | |
622 | ||
623 | return ret; | |
624 | } | |
2a99ef0f | 625 | |
1245b700 NK |
626 | /* ASoC Compress operations */ |
627 | static struct snd_compr_ops soc_compr_ops = { | |
628 | .open = soc_compr_open, | |
629 | .free = soc_compr_free, | |
630 | .set_params = soc_compr_set_params, | |
02bd90e8 VK |
631 | .set_metadata = soc_compr_set_metadata, |
632 | .get_metadata = soc_compr_get_metadata, | |
1245b700 NK |
633 | .get_params = soc_compr_get_params, |
634 | .trigger = soc_compr_trigger, | |
635 | .pointer = soc_compr_pointer, | |
636 | .ack = soc_compr_ack, | |
637 | .get_caps = soc_compr_get_caps, | |
638 | .get_codec_caps = soc_compr_get_codec_caps | |
639 | }; | |
640 | ||
2a99ef0f LG |
641 | /* ASoC Dynamic Compress operations */ |
642 | static struct snd_compr_ops soc_compr_dyn_ops = { | |
643 | .open = soc_compr_open_fe, | |
644 | .free = soc_compr_free_fe, | |
645 | .set_params = soc_compr_set_params_fe, | |
646 | .get_params = soc_compr_get_params, | |
647 | .set_metadata = soc_compr_set_metadata, | |
648 | .get_metadata = soc_compr_get_metadata, | |
649 | .trigger = soc_compr_trigger_fe, | |
650 | .pointer = soc_compr_pointer, | |
651 | .ack = soc_compr_ack, | |
652 | .get_caps = soc_compr_get_caps, | |
653 | .get_codec_caps = soc_compr_get_codec_caps | |
654 | }; | |
655 | ||
1245b700 NK |
656 | /* create a new compress */ |
657 | int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num) | |
658 | { | |
659 | struct snd_soc_codec *codec = rtd->codec; | |
1f88eb0f | 660 | struct snd_soc_platform *platform = rtd->platform; |
1245b700 NK |
661 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
662 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
663 | struct snd_compr *compr; | |
2a99ef0f | 664 | struct snd_pcm *be_pcm; |
1245b700 NK |
665 | char new_name[64]; |
666 | int ret = 0, direction = 0; | |
667 | ||
668 | /* check client and interface hw capabilities */ | |
669 | snprintf(new_name, sizeof(new_name), "%s %s-%d", | |
670 | rtd->dai_link->stream_name, codec_dai->name, num); | |
daa2db59 CK |
671 | |
672 | if (codec_dai->driver->playback.channels_min) | |
673 | direction = SND_COMPRESS_PLAYBACK; | |
674 | else if (codec_dai->driver->capture.channels_min) | |
675 | direction = SND_COMPRESS_CAPTURE; | |
676 | else | |
677 | return -EINVAL; | |
678 | ||
1245b700 NK |
679 | compr = kzalloc(sizeof(*compr), GFP_KERNEL); |
680 | if (compr == NULL) { | |
681 | snd_printk(KERN_ERR "Cannot allocate compr\n"); | |
682 | return -ENOMEM; | |
683 | } | |
684 | ||
1f88eb0f CK |
685 | compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops), |
686 | GFP_KERNEL); | |
687 | if (compr->ops == NULL) { | |
688 | dev_err(rtd->card->dev, "Cannot allocate compressed ops\n"); | |
689 | ret = -ENOMEM; | |
690 | goto compr_err; | |
691 | } | |
2a99ef0f LG |
692 | |
693 | if (rtd->dai_link->dynamic) { | |
694 | snprintf(new_name, sizeof(new_name), "(%s)", | |
695 | rtd->dai_link->stream_name); | |
696 | ||
697 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, | |
698 | 1, 0, &be_pcm); | |
699 | if (ret < 0) { | |
700 | dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n", | |
701 | rtd->dai_link->name); | |
702 | goto compr_err; | |
703 | } | |
704 | ||
705 | rtd->pcm = be_pcm; | |
706 | rtd->fe_compr = 1; | |
707 | be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; | |
708 | be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; | |
709 | memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops)); | |
710 | } else | |
711 | memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops)); | |
1f88eb0f CK |
712 | |
713 | /* Add copy callback for not memory mapped DSPs */ | |
714 | if (platform->driver->compr_ops && platform->driver->compr_ops->copy) | |
715 | compr->ops->copy = soc_compr_copy; | |
716 | ||
1245b700 NK |
717 | mutex_init(&compr->lock); |
718 | ret = snd_compress_new(rtd->card->snd_card, num, direction, compr); | |
719 | if (ret < 0) { | |
720 | pr_err("compress asoc: can't create compress for codec %s\n", | |
721 | codec->name); | |
1f88eb0f | 722 | goto compr_err; |
1245b700 NK |
723 | } |
724 | ||
202c8f70 CK |
725 | /* DAPM dai link stream work */ |
726 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); | |
727 | ||
1245b700 NK |
728 | rtd->compr = compr; |
729 | compr->private_data = rtd; | |
730 | ||
731 | printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name, | |
732 | cpu_dai->name); | |
733 | return ret; | |
1f88eb0f CK |
734 | |
735 | compr_err: | |
736 | kfree(compr); | |
737 | return ret; | |
1245b700 | 738 | } |