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> | |
27 | ||
28 | static int soc_compr_open(struct snd_compr_stream *cstream) | |
29 | { | |
30 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
31 | struct snd_soc_platform *platform = rtd->platform; | |
32 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
33 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
34 | int ret = 0; | |
35 | ||
15e2e619 CK |
36 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
37 | ||
1245b700 NK |
38 | if (platform->driver->compr_ops && platform->driver->compr_ops->open) { |
39 | ret = platform->driver->compr_ops->open(cstream); | |
40 | if (ret < 0) { | |
41 | pr_err("compress asoc: can't open platform %s\n", platform->name); | |
42 | goto out; | |
43 | } | |
44 | } | |
45 | ||
46 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) { | |
47 | ret = rtd->dai_link->compr_ops->startup(cstream); | |
48 | if (ret < 0) { | |
49 | pr_err("compress asoc: %s startup failed\n", rtd->dai_link->name); | |
50 | goto machine_err; | |
51 | } | |
52 | } | |
53 | ||
54 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { | |
55 | cpu_dai->playback_active++; | |
56 | codec_dai->playback_active++; | |
57 | } else { | |
58 | cpu_dai->capture_active++; | |
59 | codec_dai->capture_active++; | |
60 | } | |
61 | ||
62 | cpu_dai->active++; | |
63 | codec_dai->active++; | |
64 | rtd->codec->active++; | |
65 | ||
15e2e619 CK |
66 | mutex_unlock(&rtd->pcm_mutex); |
67 | ||
1245b700 NK |
68 | return 0; |
69 | ||
70 | machine_err: | |
71 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) | |
72 | platform->driver->compr_ops->free(cstream); | |
73 | out: | |
15e2e619 | 74 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
75 | return ret; |
76 | } | |
77 | ||
202c8f70 CK |
78 | /* |
79 | * Power down the audio subsystem pmdown_time msecs after close is called. | |
80 | * This is to ensure there are no pops or clicks in between any music tracks | |
81 | * due to DAPM power cycling. | |
82 | */ | |
83 | static void close_delayed_work(struct work_struct *work) | |
84 | { | |
85 | struct snd_soc_pcm_runtime *rtd = | |
86 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); | |
87 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
88 | ||
89 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); | |
90 | ||
91 | dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n", | |
92 | codec_dai->driver->playback.stream_name, | |
93 | codec_dai->playback_active ? "active" : "inactive", | |
94 | rtd->pop_wait ? "yes" : "no"); | |
95 | ||
96 | /* are we waiting on this codec DAI stream */ | |
97 | if (rtd->pop_wait == 1) { | |
98 | rtd->pop_wait = 0; | |
99 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, | |
100 | SND_SOC_DAPM_STREAM_STOP); | |
101 | } | |
102 | ||
103 | mutex_unlock(&rtd->pcm_mutex); | |
104 | } | |
105 | ||
1245b700 NK |
106 | static int soc_compr_free(struct snd_compr_stream *cstream) |
107 | { | |
108 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
109 | struct snd_soc_platform *platform = rtd->platform; | |
110 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
111 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
112 | struct snd_soc_codec *codec = rtd->codec; | |
113 | ||
15e2e619 CK |
114 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
115 | ||
1245b700 NK |
116 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
117 | cpu_dai->playback_active--; | |
118 | codec_dai->playback_active--; | |
e38b9b74 | 119 | snd_soc_dai_digital_mute(codec_dai, 1); |
1245b700 NK |
120 | } else { |
121 | cpu_dai->capture_active--; | |
122 | codec_dai->capture_active--; | |
123 | } | |
124 | ||
1245b700 NK |
125 | cpu_dai->active--; |
126 | codec_dai->active--; | |
127 | codec->active--; | |
128 | ||
129 | if (!cpu_dai->active) | |
130 | cpu_dai->rate = 0; | |
131 | ||
132 | if (!codec_dai->active) | |
133 | codec_dai->rate = 0; | |
134 | ||
135 | ||
136 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown) | |
137 | rtd->dai_link->compr_ops->shutdown(cstream); | |
138 | ||
139 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) | |
140 | platform->driver->compr_ops->free(cstream); | |
9d2667a9 | 141 | cpu_dai->runtime = NULL; |
1245b700 NK |
142 | |
143 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { | |
144 | if (!rtd->pmdown_time || codec->ignore_pmdown_time || | |
145 | rtd->dai_link->ignore_pmdown_time) { | |
146 | snd_soc_dapm_stream_event(rtd, | |
147 | SNDRV_PCM_STREAM_PLAYBACK, | |
148 | SND_SOC_DAPM_STREAM_STOP); | |
8c3d2aa4 | 149 | } else { |
9bffb1fb | 150 | rtd->pop_wait = 1; |
1245b700 NK |
151 | schedule_delayed_work(&rtd->delayed_work, |
152 | msecs_to_jiffies(rtd->pmdown_time)); | |
8c3d2aa4 | 153 | } |
1245b700 NK |
154 | } else { |
155 | /* capture streams can be powered down now */ | |
156 | snd_soc_dapm_stream_event(rtd, | |
157 | SNDRV_PCM_STREAM_CAPTURE, | |
158 | SND_SOC_DAPM_STREAM_STOP); | |
159 | } | |
160 | ||
15e2e619 | 161 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
162 | return 0; |
163 | } | |
164 | ||
165 | static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd) | |
166 | { | |
167 | ||
168 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
169 | struct snd_soc_platform *platform = rtd->platform; | |
170 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
171 | int ret = 0; | |
172 | ||
15e2e619 CK |
173 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
174 | ||
1245b700 NK |
175 | if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) { |
176 | ret = platform->driver->compr_ops->trigger(cstream, cmd); | |
177 | if (ret < 0) | |
15e2e619 | 178 | goto out; |
1245b700 NK |
179 | } |
180 | ||
e38b9b74 MB |
181 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
182 | switch (cmd) { | |
183 | case SNDRV_PCM_TRIGGER_START: | |
184 | snd_soc_dai_digital_mute(codec_dai, 0); | |
185 | break; | |
186 | case SNDRV_PCM_TRIGGER_STOP: | |
187 | snd_soc_dai_digital_mute(codec_dai, 1); | |
188 | break; | |
189 | } | |
190 | } | |
1245b700 | 191 | |
15e2e619 CK |
192 | out: |
193 | mutex_unlock(&rtd->pcm_mutex); | |
1245b700 NK |
194 | return ret; |
195 | } | |
196 | ||
197 | static int soc_compr_set_params(struct snd_compr_stream *cstream, | |
198 | struct snd_compr_params *params) | |
199 | { | |
200 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
201 | struct snd_soc_platform *platform = rtd->platform; | |
1245b700 NK |
202 | int ret = 0; |
203 | ||
15e2e619 CK |
204 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
205 | ||
1245b700 NK |
206 | /* first we call set_params for the platform driver |
207 | * this should configure the soc side | |
208 | * if the machine has compressed ops then we call that as well | |
209 | * expectation is that platform and machine will configure everything | |
210 | * for this compress path, like configuring pcm port for codec | |
211 | */ | |
212 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) { | |
213 | ret = platform->driver->compr_ops->set_params(cstream, params); | |
214 | if (ret < 0) | |
15e2e619 | 215 | goto out; |
1245b700 NK |
216 | } |
217 | ||
218 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) { | |
219 | ret = rtd->dai_link->compr_ops->set_params(cstream); | |
220 | if (ret < 0) | |
15e2e619 | 221 | goto out; |
1245b700 NK |
222 | } |
223 | ||
224 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, | |
225 | SND_SOC_DAPM_STREAM_START); | |
226 | ||
15e2e619 CK |
227 | out: |
228 | mutex_unlock(&rtd->pcm_mutex); | |
1245b700 NK |
229 | return ret; |
230 | } | |
231 | ||
232 | static int soc_compr_get_params(struct snd_compr_stream *cstream, | |
233 | struct snd_codec *params) | |
234 | { | |
235 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
236 | struct snd_soc_platform *platform = rtd->platform; | |
237 | int ret = 0; | |
238 | ||
15e2e619 CK |
239 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
240 | ||
1245b700 NK |
241 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_params) |
242 | ret = platform->driver->compr_ops->get_params(cstream, params); | |
243 | ||
15e2e619 | 244 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
245 | return ret; |
246 | } | |
247 | ||
248 | static int soc_compr_get_caps(struct snd_compr_stream *cstream, | |
249 | struct snd_compr_caps *caps) | |
250 | { | |
251 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
252 | struct snd_soc_platform *platform = rtd->platform; | |
253 | int ret = 0; | |
254 | ||
15e2e619 CK |
255 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
256 | ||
1245b700 NK |
257 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps) |
258 | ret = platform->driver->compr_ops->get_caps(cstream, caps); | |
259 | ||
15e2e619 | 260 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
261 | return ret; |
262 | } | |
263 | ||
264 | static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream, | |
265 | struct snd_compr_codec_caps *codec) | |
266 | { | |
267 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
268 | struct snd_soc_platform *platform = rtd->platform; | |
269 | int ret = 0; | |
270 | ||
15e2e619 CK |
271 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
272 | ||
1245b700 NK |
273 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps) |
274 | ret = platform->driver->compr_ops->get_codec_caps(cstream, codec); | |
275 | ||
15e2e619 | 276 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
277 | return ret; |
278 | } | |
279 | ||
280 | static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes) | |
281 | { | |
282 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
283 | struct snd_soc_platform *platform = rtd->platform; | |
284 | int ret = 0; | |
285 | ||
15e2e619 CK |
286 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
287 | ||
1245b700 NK |
288 | if (platform->driver->compr_ops && platform->driver->compr_ops->ack) |
289 | ret = platform->driver->compr_ops->ack(cstream, bytes); | |
290 | ||
15e2e619 | 291 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
292 | return ret; |
293 | } | |
294 | ||
295 | static int soc_compr_pointer(struct snd_compr_stream *cstream, | |
296 | struct snd_compr_tstamp *tstamp) | |
297 | { | |
298 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
299 | struct snd_soc_platform *platform = rtd->platform; | |
300 | ||
15e2e619 CK |
301 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
302 | ||
1245b700 NK |
303 | if (platform->driver->compr_ops && platform->driver->compr_ops->pointer) |
304 | platform->driver->compr_ops->pointer(cstream, tstamp); | |
305 | ||
15e2e619 | 306 | mutex_unlock(&rtd->pcm_mutex); |
1245b700 NK |
307 | return 0; |
308 | } | |
309 | ||
1f88eb0f CK |
310 | static int soc_compr_copy(struct snd_compr_stream *cstream, |
311 | const char __user *buf, size_t count) | |
312 | { | |
313 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; | |
314 | struct snd_soc_platform *platform = rtd->platform; | |
315 | int ret = 0; | |
316 | ||
317 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); | |
318 | ||
319 | if (platform->driver->compr_ops && platform->driver->compr_ops->copy) | |
320 | ret = platform->driver->compr_ops->copy(cstream, buf, count); | |
321 | ||
322 | mutex_unlock(&rtd->pcm_mutex); | |
323 | return ret; | |
324 | } | |
325 | ||
1245b700 NK |
326 | /* ASoC Compress operations */ |
327 | static struct snd_compr_ops soc_compr_ops = { | |
328 | .open = soc_compr_open, | |
329 | .free = soc_compr_free, | |
330 | .set_params = soc_compr_set_params, | |
331 | .get_params = soc_compr_get_params, | |
332 | .trigger = soc_compr_trigger, | |
333 | .pointer = soc_compr_pointer, | |
334 | .ack = soc_compr_ack, | |
335 | .get_caps = soc_compr_get_caps, | |
336 | .get_codec_caps = soc_compr_get_codec_caps | |
337 | }; | |
338 | ||
339 | /* create a new compress */ | |
340 | int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num) | |
341 | { | |
342 | struct snd_soc_codec *codec = rtd->codec; | |
1f88eb0f | 343 | struct snd_soc_platform *platform = rtd->platform; |
1245b700 NK |
344 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
345 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
346 | struct snd_compr *compr; | |
347 | char new_name[64]; | |
348 | int ret = 0, direction = 0; | |
349 | ||
350 | /* check client and interface hw capabilities */ | |
351 | snprintf(new_name, sizeof(new_name), "%s %s-%d", | |
352 | rtd->dai_link->stream_name, codec_dai->name, num); | |
353 | direction = SND_COMPRESS_PLAYBACK; | |
354 | compr = kzalloc(sizeof(*compr), GFP_KERNEL); | |
355 | if (compr == NULL) { | |
356 | snd_printk(KERN_ERR "Cannot allocate compr\n"); | |
357 | return -ENOMEM; | |
358 | } | |
359 | ||
1f88eb0f CK |
360 | compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops), |
361 | GFP_KERNEL); | |
362 | if (compr->ops == NULL) { | |
363 | dev_err(rtd->card->dev, "Cannot allocate compressed ops\n"); | |
364 | ret = -ENOMEM; | |
365 | goto compr_err; | |
366 | } | |
367 | memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops)); | |
368 | ||
369 | /* Add copy callback for not memory mapped DSPs */ | |
370 | if (platform->driver->compr_ops && platform->driver->compr_ops->copy) | |
371 | compr->ops->copy = soc_compr_copy; | |
372 | ||
1245b700 NK |
373 | mutex_init(&compr->lock); |
374 | ret = snd_compress_new(rtd->card->snd_card, num, direction, compr); | |
375 | if (ret < 0) { | |
376 | pr_err("compress asoc: can't create compress for codec %s\n", | |
377 | codec->name); | |
1f88eb0f | 378 | goto compr_err; |
1245b700 NK |
379 | } |
380 | ||
202c8f70 CK |
381 | /* DAPM dai link stream work */ |
382 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); | |
383 | ||
1245b700 NK |
384 | rtd->compr = compr; |
385 | compr->private_data = rtd; | |
386 | ||
387 | printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name, | |
388 | cpu_dai->name); | |
389 | return ret; | |
1f88eb0f CK |
390 | |
391 | compr_err: | |
392 | kfree(compr); | |
393 | return ret; | |
1245b700 | 394 | } |