blob: 6ef924f37a9e35c4389bea335d0244699b568daa [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* linux/sound/soc/msm/msm7k-pcm.c
2 *
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +05303 * Copyright (c) 2008-2009, 2012 Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 *
5 * All source code in this file is licensed under the following license except
6 * where indicated.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you can find it at http://www.fsf.org.
19 */
20
21
22
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/time.h>
28#include <linux/wait.h>
29#include <linux/platform_device.h>
30#include <linux/slab.h>
31#include <sound/core.h>
32#include <sound/soc.h>
33#include <sound/soc-dapm.h>
34#include <sound/pcm.h>
35#include <sound/initval.h>
36#include <sound/control.h>
37#include <asm/dma.h>
38#include <linux/dma-mapping.h>
39
40#include "msm-pcm.h"
41
42#define SND_DRIVER "snd_msm"
43#define MAX_PCM_DEVICES SNDRV_CARDS
44#define MAX_PCM_SUBSTREAMS 1
45
46struct snd_msm {
47 struct snd_card *card;
48 struct snd_pcm *pcm;
49};
50
51int copy_count;
52
53struct audio_locks the_locks;
54EXPORT_SYMBOL(the_locks);
55struct msm_volume msm_vol_ctl;
56EXPORT_SYMBOL(msm_vol_ctl);
57
58
59static unsigned convert_dsp_samp_index(unsigned index)
60{
61 switch (index) {
62 case 48000:
63 return AUDREC_CMD_SAMP_RATE_INDX_48000;
64 case 44100:
65 return AUDREC_CMD_SAMP_RATE_INDX_44100;
66 case 32000:
67 return AUDREC_CMD_SAMP_RATE_INDX_32000;
68 case 24000:
69 return AUDREC_CMD_SAMP_RATE_INDX_24000;
70 case 22050:
71 return AUDREC_CMD_SAMP_RATE_INDX_22050;
72 case 16000:
73 return AUDREC_CMD_SAMP_RATE_INDX_16000;
74 case 12000:
75 return AUDREC_CMD_SAMP_RATE_INDX_12000;
76 case 11025:
77 return AUDREC_CMD_SAMP_RATE_INDX_11025;
78 case 8000:
79 return AUDREC_CMD_SAMP_RATE_INDX_8000;
80 default:
81 return AUDREC_CMD_SAMP_RATE_INDX_44100;
82 }
83}
84
85static unsigned convert_samp_rate(unsigned hz)
86{
87 switch (hz) {
88 case 48000:
89 return RPC_AUD_DEF_SAMPLE_RATE_48000;
90 case 44100:
91 return RPC_AUD_DEF_SAMPLE_RATE_44100;
92 case 32000:
93 return RPC_AUD_DEF_SAMPLE_RATE_32000;
94 case 24000:
95 return RPC_AUD_DEF_SAMPLE_RATE_24000;
96 case 22050:
97 return RPC_AUD_DEF_SAMPLE_RATE_22050;
98 case 16000:
99 return RPC_AUD_DEF_SAMPLE_RATE_16000;
100 case 12000:
101 return RPC_AUD_DEF_SAMPLE_RATE_12000;
102 case 11025:
103 return RPC_AUD_DEF_SAMPLE_RATE_11025;
104 case 8000:
105 return RPC_AUD_DEF_SAMPLE_RATE_8000;
106 default:
107 return RPC_AUD_DEF_SAMPLE_RATE_44100;
108 }
109}
110
111static struct snd_pcm_hardware msm_pcm_playback_hardware = {
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530112 .info = SNDRV_PCM_INFO_MMAP |
113 SNDRV_PCM_INFO_MMAP_VALID |
114 SNDRV_PCM_INFO_INTERLEAVED,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 .formats = USE_FORMATS,
116 .rates = USE_RATE,
117 .rate_min = USE_RATE_MIN,
118 .rate_max = USE_RATE_MAX,
119 .channels_min = USE_CHANNELS_MIN,
120 .channels_max = USE_CHANNELS_MAX,
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530121 .buffer_bytes_max = 4800 * 2,
122 .period_bytes_min = 4800,
123 .period_bytes_max = 4800,
124 .periods_min = 2,
125 .periods_max = 2,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 .fifo_size = 0,
127};
128
129static struct snd_pcm_hardware msm_pcm_capture_hardware = {
130 .info = SNDRV_PCM_INFO_INTERLEAVED,
131 .formats = USE_FORMATS,
132 .rates = USE_RATE,
133 .rate_min = USE_RATE_MIN,
134 .rate_max = USE_RATE_MAX,
135 .channels_min = USE_CHANNELS_MIN,
136 .channels_max = USE_CHANNELS_MAX,
137 .buffer_bytes_max = MAX_BUFFER_CAPTURE_SIZE,
138 .period_bytes_min = CAPTURE_SIZE,
139 .period_bytes_max = CAPTURE_SIZE,
140 .periods_min = USE_PERIODS_MIN,
141 .periods_max = USE_PERIODS_MAX,
142 .fifo_size = 0,
143};
144
145/* Conventional and unconventional sample rate supported */
146static unsigned int supported_sample_rates[] = {
147 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
148};
149
150static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
151 .count = ARRAY_SIZE(supported_sample_rates),
152 .list = supported_sample_rates,
153 .mask = 0,
154};
155
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530156static void msm_pcm_enqueue_data(struct snd_pcm_substream *substream)
157{
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 struct msm_audio *prtd = runtime->private_data;
160 unsigned int period_size;
161
162 pr_debug("prtd->out_tail =%d mmap_flag=%d\n",
163 prtd->out_tail, prtd->mmap_flag);
164 period_size = snd_pcm_lib_period_bytes(substream);
165 alsa_dsp_send_buffer(prtd, prtd->out_tail, period_size);
166 prtd->out_tail ^= 1;
167 ++copy_count;
168 prtd->period++;
169 if (unlikely(prtd->period >= runtime->periods))
170 prtd->period = 0;
171
172}
173
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174static void playback_event_handler(void *data)
175{
176 struct msm_audio *prtd = data;
177 snd_pcm_period_elapsed(prtd->playback_substream);
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530178 if (prtd->mmap_flag) {
179 if (prtd->dir == SNDRV_PCM_STREAM_CAPTURE)
180 return;
181 if (!prtd->stopped)
182 msm_pcm_enqueue_data(prtd->playback_substream);
183 else
184 prtd->out_needed++;
185 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186}
187
188static void capture_event_handler(void *data)
189{
190 struct msm_audio *prtd = data;
191 snd_pcm_period_elapsed(prtd->capture_substream);
192}
193
194static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
195{
196 struct snd_pcm_runtime *runtime = substream->runtime;
197 struct msm_audio *prtd = runtime->private_data;
198
199 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
200 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
201 prtd->pcm_irq_pos = 0;
202 prtd->pcm_buf_pos = 0;
203
204 /* rate and channels are sent to audio driver */
205 prtd->out_sample_rate = runtime->rate;
206 prtd->out_channel_mode = runtime->channels;
207
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530208 if (prtd->enabled | !(prtd->mmap_flag))
209 return 0;
210
211 prtd->data = substream->dma_buffer.area;
212 prtd->phys = substream->dma_buffer.addr;
213 prtd->out[0].data = prtd->data + 0;
214 prtd->out[0].addr = prtd->phys + 0;
215 prtd->out[0].size = BUFSZ;
216 prtd->out[1].data = prtd->data + BUFSZ;
217 prtd->out[1].addr = prtd->phys + BUFSZ;
218 prtd->out[1].size = BUFSZ;
219
220 prtd->out[0].used = prtd->pcm_count;
221 prtd->out[1].used = prtd->pcm_count;
222
223 mutex_lock(&the_locks.lock);
224 alsa_audio_configure(prtd);
225 mutex_unlock(&the_locks.lock);
226
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 return 0;
229}
230
231static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
232{
233 struct snd_pcm_runtime *runtime = substream->runtime;
234 struct msm_audio *prtd = runtime->private_data;
235 struct audmgr_config cfg;
236 int rc;
237
238 prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
239 prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
240 prtd->pcm_irq_pos = 0;
241 prtd->pcm_buf_pos = 0;
242
243 /* rate and channels are sent to audio driver */
244 prtd->samp_rate = convert_samp_rate(runtime->rate);
245 prtd->samp_rate_index = convert_dsp_samp_index(runtime->rate);
246 prtd->channel_mode = (runtime->channels - 1);
247 prtd->buffer_size = prtd->channel_mode ? STEREO_DATA_SIZE : \
248 MONO_DATA_SIZE;
249
250 if (prtd->enabled == 1)
251 return 0;
252
253 prtd->type = AUDREC_CMD_TYPE_0_INDEX_WAV;
254
255 cfg.tx_rate = convert_samp_rate(runtime->rate);
256 cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
257 cfg.def_method = RPC_AUD_DEF_METHOD_RECORD;
258 cfg.codec = RPC_AUD_DEF_CODEC_PCM;
259 cfg.snd_method = RPC_SND_METHOD_MIDI;
260
261 rc = audmgr_enable(&prtd->audmgr, &cfg);
262 if (rc < 0)
263 return rc;
264
265 if (msm_adsp_enable(prtd->audpre)) {
266 audmgr_disable(&prtd->audmgr);
267 return -ENODEV;
268 }
269 if (msm_adsp_enable(prtd->audrec)) {
270 msm_adsp_disable(prtd->audpre);
271 audmgr_disable(&prtd->audmgr);
272 return -ENODEV;
273 }
274 prtd->enabled = 1;
275 alsa_rec_dsp_enable(prtd, 1);
276
277 return 0;
278}
279
280static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
281{
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530282 struct snd_pcm_runtime *runtime = substream->runtime;
283 struct msm_audio *prtd = runtime->private_data;
284 unsigned long flag = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285 int ret = 0;
286
287 switch (cmd) {
288 case SNDRV_PCM_TRIGGER_START:
289 case SNDRV_PCM_TRIGGER_RESUME:
290 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530291 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE)
292 || !prtd->mmap_flag)
293 break;
294 if (!prtd->out_needed) {
295 prtd->stopped = 0;
296 break;
297 }
298 spin_lock_irqsave(&the_locks.write_dsp_lock, flag);
299 if (prtd->running == 1) {
300 if (prtd->stopped == 1) {
301 prtd->stopped = 0;
302 prtd->period = 0;
303 if (prtd->pcm_irq_pos == 0) {
304 prtd->out_tail = 0;
305 msm_pcm_enqueue_data(
306 prtd->playback_substream);
307 prtd->out_needed--;
308 } else {
309 prtd->out_tail = 1;
310 msm_pcm_enqueue_data(
311 prtd->playback_substream);
312 prtd->out_needed--;
313 }
314 if (prtd->out_needed) {
315 prtd->out_tail ^= 1;
316 msm_pcm_enqueue_data(
317 prtd->playback_substream);
318 prtd->out_needed--;
319 }
320 }
321 }
322 spin_unlock_irqrestore(&the_locks.write_dsp_lock, flag);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323 break;
324 case SNDRV_PCM_TRIGGER_STOP:
325 case SNDRV_PCM_TRIGGER_SUSPEND:
326 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530327 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE)
328 || !prtd->mmap_flag)
329 break;
330 prtd->stopped = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331 break;
332 default:
333 ret = -EINVAL;
334 }
335
336 return ret;
337}
338
339static snd_pcm_uframes_t
340msm_pcm_playback_pointer(struct snd_pcm_substream *substream)
341{
342 struct snd_pcm_runtime *runtime = substream->runtime;
343 struct msm_audio *prtd = runtime->private_data;
344
345 if (prtd->pcm_irq_pos == prtd->pcm_size)
346 prtd->pcm_irq_pos = 0;
347 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
348}
349
350static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
351 int channel, snd_pcm_uframes_t hwoff, void __user *buf,
352 snd_pcm_uframes_t frames)
353{
354 int rc = 0, rc1 = 0, rc2 = 0;
355 int fbytes = 0;
356 struct snd_pcm_runtime *runtime = substream->runtime;
357 struct msm_audio *prtd = substream->runtime->private_data;
358
359 int monofbytes = 0;
360 char *bufferp = NULL;
361
362 fbytes = frames_to_bytes(runtime, frames);
363 monofbytes = fbytes / 2;
364 if (runtime->channels == 2) {
365 rc = alsa_buffer_read(prtd, buf, fbytes, NULL);
366 } else {
367 bufferp = buf;
368 rc1 = alsa_buffer_read(prtd, bufferp, monofbytes, NULL);
369 bufferp = buf + monofbytes ;
370 rc2 = alsa_buffer_read(prtd, bufferp, monofbytes, NULL);
371 rc = rc1 + rc2;
372 }
373 prtd->pcm_buf_pos += fbytes;
374 return rc;
375}
376
377static snd_pcm_uframes_t
378msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
379{
380 struct snd_pcm_runtime *runtime = substream->runtime;
381 struct msm_audio *prtd = runtime->private_data;
382
383 return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
384}
385
386static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
387{
388 struct snd_pcm_runtime *runtime = substream->runtime;
389 struct msm_audio *prtd = runtime->private_data;
390
391 alsa_audrec_disable(prtd);
392 audmgr_close(&prtd->audmgr);
393 msm_adsp_put(prtd->audrec);
394 msm_adsp_put(prtd->audpre);
395 kfree(prtd);
396
397 return 0;
398}
399
400struct msm_audio_event_callbacks snd_msm_audio_ops = {
401 .playback = playback_event_handler,
402 .capture = capture_event_handler,
403};
404
405static int msm_pcm_open(struct snd_pcm_substream *substream)
406{
407 struct snd_pcm_runtime *runtime = substream->runtime;
408 struct msm_audio *prtd;
409 int ret = 0;
410
411 prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
412 if (prtd == NULL) {
413 ret = -ENOMEM;
414 return ret;
415 }
416 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
417 msm_vol_ctl.update = 1; /* Update Volume, with Cached value */
418 runtime->hw = msm_pcm_playback_hardware;
419 prtd->dir = SNDRV_PCM_STREAM_PLAYBACK;
420 prtd->playback_substream = substream;
421 prtd->eos_ack = 0;
422 } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
423 runtime->hw = msm_pcm_capture_hardware;
424 prtd->dir = SNDRV_PCM_STREAM_CAPTURE;
425 prtd->capture_substream = substream;
426 }
427 ret = snd_pcm_hw_constraint_list(runtime, 0,
428 SNDRV_PCM_HW_PARAM_RATE,
429 &constraints_sample_rates);
430 if (ret < 0)
431 goto out;
432 /* Ensure that buffer size is a multiple of period size */
433 ret = snd_pcm_hw_constraint_integer(runtime,
434 SNDRV_PCM_HW_PARAM_PERIODS);
435 if (ret < 0)
436 goto out;
437
438 prtd->ops = &snd_msm_audio_ops;
439 prtd->out[0].used = BUF_INVALID_LEN;
440 prtd->out_head = 1; /* point to second buffer on startup */
441 runtime->private_data = prtd;
442
443 ret = alsa_adsp_configure(prtd);
444 if (ret)
445 goto out;
446 copy_count = 0;
447 return 0;
448
449 out:
450 kfree(prtd);
451 return ret;
452}
453
454static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
455 snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
456{
457 int rc = 1;
458 int fbytes = 0;
459
460 struct snd_pcm_runtime *runtime = substream->runtime;
461 struct msm_audio *prtd = runtime->private_data;
462
463 fbytes = frames_to_bytes(runtime, frames);
464 rc = alsa_send_buffer(prtd, buf, fbytes, NULL);
465 ++copy_count;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 if (copy_count == 1) {
467 mutex_lock(&the_locks.lock);
468 alsa_audio_configure(prtd);
469 mutex_unlock(&the_locks.lock);
470 }
471 if ((prtd->running) && (msm_vol_ctl.update)) {
472 rc = msm_audio_volume_update(PCMPLAYBACK_DECODERID,
473 msm_vol_ctl.volume, msm_vol_ctl.pan);
474 msm_vol_ctl.update = 0;
475 }
476
477 return rc;
478}
479
480static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
481{
482 struct snd_pcm_runtime *runtime = substream->runtime;
483 struct msm_audio *prtd = runtime->private_data;
484
485 int rc = 0;
486
487 pr_debug("%s()\n", __func__);
488
489 /* pcm dmamiss message is sent continously
490 * when decoder is starved so no race
491 * condition concern
492 */
493 if (prtd->enabled)
494 rc = wait_event_interruptible(the_locks.eos_wait,
495 prtd->eos_ack);
496
497 alsa_audio_disable(prtd);
498 audmgr_close(&prtd->audmgr);
499 kfree(prtd);
500
501 return 0;
502}
503
504
505static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
506 snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
507{
508 int ret = 0;
509
510 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
511 ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
512 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
513 ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
514 return ret;
515}
516
517static int msm_pcm_close(struct snd_pcm_substream *substream)
518{
519 int ret = 0;
520
521 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
522 ret = msm_pcm_playback_close(substream);
523 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
524 ret = msm_pcm_capture_close(substream);
525 return ret;
526}
527static int msm_pcm_prepare(struct snd_pcm_substream *substream)
528{
529 int ret = 0;
530
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
532 ret = msm_pcm_playback_prepare(substream);
533 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
534 ret = msm_pcm_capture_prepare(substream);
535 return ret;
536}
537
538static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
539{
540 snd_pcm_uframes_t ret = 0;
541
542 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
543 ret = msm_pcm_playback_pointer(substream);
544 else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
545 ret = msm_pcm_capture_pointer(substream);
546 return ret;
547}
548
549int msm_pcm_hw_params(struct snd_pcm_substream *substream,
550 struct snd_pcm_hw_params *params)
551{
552 struct snd_pcm_runtime *runtime = substream->runtime;
553
554 if (substream->pcm->device & 1) {
555 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
556 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
557 }
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530558 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 return 0;
560
561}
562
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530563int msm_pcm_mmap(struct snd_pcm_substream *substream,
564 struct vm_area_struct *vma)
565{
566 struct snd_pcm_runtime *runtime = substream->runtime;
567 struct msm_audio *prtd = runtime->private_data;
568
569 prtd->out_head = 0; /* point to First buffer on startup */
570 prtd->mmap_flag = 1;
571 runtime->dma_bytes = snd_pcm_lib_period_bytes(substream)*2;
572 dma_mmap_coherent(substream->pcm->card->dev, vma,
573 runtime->dma_area,
574 runtime->dma_addr,
575 runtime->dma_bytes);
576 return 0;
577}
578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579static struct snd_pcm_ops msm_pcm_ops = {
580 .open = msm_pcm_open,
581 .copy = msm_pcm_copy,
582 .hw_params = msm_pcm_hw_params,
583 .close = msm_pcm_close,
584 .ioctl = snd_pcm_lib_ioctl,
585 .prepare = msm_pcm_prepare,
586 .trigger = msm_pcm_trigger,
587 .pointer = msm_pcm_pointer,
Asish Bhattacharyac1c701f2012-02-22 10:55:59 +0530588 .mmap = msm_pcm_mmap,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589};
590
591static int pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
592 int stream)
593{
594 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
595 struct snd_dma_buffer *buf = &substream->dma_buffer;
596 size_t size;
597 if (!stream)
598 size = PLAYBACK_DMASZ;
599 else
600 size = CAPTURE_DMASZ;
601
602 buf->dev.type = SNDRV_DMA_TYPE_DEV;
603 buf->dev.dev = pcm->card->dev;
604 buf->private_data = NULL;
605 buf->area = dma_alloc_coherent(pcm->card->dev, size,
606 &buf->addr, GFP_KERNEL);
607 if (!buf->area)
608 return -ENOMEM;
609
610 buf->bytes = size;
611 return 0;
612}
613
614static void msm_pcm_free_dma_buffers(struct snd_pcm *pcm)
615{
616 struct snd_pcm_substream *substream;
617 struct snd_dma_buffer *buf;
618 int stream;
619
620 for (stream = 0; stream < 2; stream++) {
621 substream = pcm->streams[stream].substream;
622 if (!substream)
623 continue;
624
625 buf = &substream->dma_buffer;
626 if (!buf->area)
627 continue;
628
629 dma_free_coherent(pcm->card->dev, buf->bytes,
630 buf->area, buf->addr);
631 buf->area = NULL;
632 }
633}
634
635static int msm_pcm_new(struct snd_soc_pcm_runtime *rtd)
636{
637 int ret;
638 struct snd_card *card = rtd->card->snd_card;
639 struct snd_pcm *pcm = rtd->pcm;
640
641 if (!card->dev->coherent_dma_mask)
642 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
643
644 ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
645 if (ret)
646 return ret;
647 ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
648 if (ret)
649 return ret;
650 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &msm_pcm_ops);
651 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &msm_pcm_ops);
652
653 ret = pcm_preallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
654 if (ret)
655 return ret;
656
657 ret = pcm_preallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
658 if (ret)
659 msm_pcm_free_dma_buffers(pcm);
660 return ret;
661}
662
663struct snd_soc_platform_driver msm_soc_platform = {
664 .ops = &msm_pcm_ops,
665 .pcm_new = msm_pcm_new,
666 .pcm_free = msm_pcm_free_dma_buffers,
667};
668EXPORT_SYMBOL(msm_soc_platform);
669
670static __devinit int msm_pcm_probe(struct platform_device *pdev)
671{
672 return snd_soc_register_platform(&pdev->dev,
673 &msm_soc_platform);
674}
675
676static int msm_pcm_remove(struct platform_device *pdev)
677{
678 snd_soc_unregister_platform(&pdev->dev);
679 return 0;
680}
681
682static struct platform_driver msm_pcm_driver = {
683 .driver = {
684 .name = "msm-dsp-audio",
685 .owner = THIS_MODULE,
686 },
687 .probe = msm_pcm_probe,
688 .remove = __devexit_p(msm_pcm_remove),
689};
690
691static int __init msm_soc_platform_init(void)
692{
693 return platform_driver_register(&msm_pcm_driver);
694}
695module_init(msm_soc_platform_init);
696
697static void __exit msm_soc_platform_exit(void)
698{
699 platform_driver_unregister(&msm_pcm_driver);
700}
701module_exit(msm_soc_platform_exit);
702
703MODULE_DESCRIPTION("PCM module platform driver");
704MODULE_LICENSE("GPL v2");