modded some stuff (new lines and restructuration for readabelidy)
[htc-msm-2-6-32:leviathan-incoming.git] / sound / soc / msm / msm7k-pcm.c
1 /* linux/sound/soc/msm/msm7k-pcm.c
2  *
3  * Copyright (c) 2008-2009, Code Aurora Forum. All rights reserved.
4  *
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 <sound/core.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dapm.h>
33 #include <sound/pcm.h>
34 #include <sound/initval.h>
35 #include <sound/control.h>
36 #include <asm/dma.h>
37 #include <linux/dma-mapping.h>
38
39 #include "msm-pcm.h"
40
41 #define SND_DRIVER        "snd_msm"
42 #define MAX_PCM_DEVICES SNDRV_CARDS
43 #define MAX_PCM_SUBSTREAMS 1
44
45 struct snd_msm {
46         struct snd_card *card;
47         struct snd_pcm *pcm;
48 };
49
50 int copy_count;
51
52 struct audio_locks the_locks;
53 EXPORT_SYMBOL(the_locks);
54 struct msm_volume msm_vol_ctl;
55 EXPORT_SYMBOL(msm_vol_ctl);
56
57
58 static unsigned convert_dsp_samp_index(unsigned index)
59 {
60         switch (index) {
61         case 48000:
62                 return AUDREC_CMD_SAMP_RATE_INDX_48000;
63         case 44100:
64                 return AUDREC_CMD_SAMP_RATE_INDX_44100;
65         case 32000:
66                 return AUDREC_CMD_SAMP_RATE_INDX_32000;
67         case 24000:
68                 return AUDREC_CMD_SAMP_RATE_INDX_24000;
69         case 22050:
70                 return AUDREC_CMD_SAMP_RATE_INDX_22050;
71         case 16000:
72                 return AUDREC_CMD_SAMP_RATE_INDX_16000;
73         case 12000:
74                 return AUDREC_CMD_SAMP_RATE_INDX_12000;
75         case 11025:
76                 return AUDREC_CMD_SAMP_RATE_INDX_11025;
77         case 8000:
78                 return AUDREC_CMD_SAMP_RATE_INDX_8000;
79         default:
80                 return AUDREC_CMD_SAMP_RATE_INDX_44100;
81         }
82 }
83
84 static unsigned convert_samp_rate(unsigned hz)
85 {
86         switch (hz) {
87         case 48000:
88                 return RPC_AUD_DEF_SAMPLE_RATE_48000;
89         case 44100:
90                 return RPC_AUD_DEF_SAMPLE_RATE_44100;
91         case 32000:
92                 return RPC_AUD_DEF_SAMPLE_RATE_32000;
93         case 24000:
94                 return RPC_AUD_DEF_SAMPLE_RATE_24000;
95         case 22050:
96                 return RPC_AUD_DEF_SAMPLE_RATE_22050;
97         case 16000:
98                 return RPC_AUD_DEF_SAMPLE_RATE_16000;
99         case 12000:
100                 return RPC_AUD_DEF_SAMPLE_RATE_12000;
101         case 11025:
102                 return RPC_AUD_DEF_SAMPLE_RATE_11025;
103         case 8000:
104                 return RPC_AUD_DEF_SAMPLE_RATE_8000;
105         default:
106                 return RPC_AUD_DEF_SAMPLE_RATE_44100;
107         }
108 }
109
110 static struct snd_pcm_hardware msm_pcm_playback_hardware = {
111         .info =                 SNDRV_PCM_INFO_INTERLEAVED,
112         .formats =              USE_FORMATS,
113         .rates =                USE_RATE,
114         .rate_min =             USE_RATE_MIN,
115         .rate_max =             USE_RATE_MAX,
116         .channels_min =         USE_CHANNELS_MIN,
117         .channels_max =         USE_CHANNELS_MAX,
118         .buffer_bytes_max =     MAX_BUFFER_PLAYBACK_SIZE,
119         .period_bytes_min =     64,
120         .period_bytes_max =     MAX_PERIOD_SIZE,
121         .periods_min =          USE_PERIODS_MIN,
122         .periods_max =          USE_PERIODS_MAX,
123         .fifo_size =            0,
124 };
125
126 static struct snd_pcm_hardware msm_pcm_capture_hardware = {
127         .info =                 SNDRV_PCM_INFO_INTERLEAVED,
128         .formats =              USE_FORMATS,
129         .rates =                USE_RATE,
130         .rate_min =             USE_RATE_MIN,
131         .rate_max =             USE_RATE_MAX,
132         .channels_min =         USE_CHANNELS_MIN,
133         .channels_max =         USE_CHANNELS_MAX,
134         .buffer_bytes_max =     MAX_BUFFER_CAPTURE_SIZE,
135         .period_bytes_min =     CAPTURE_SIZE,
136         .period_bytes_max =     CAPTURE_SIZE,
137         .periods_min =          USE_PERIODS_MIN,
138         .periods_max =          USE_PERIODS_MAX,
139         .fifo_size =            0,
140 };
141
142 /* Conventional and unconventional sample rate supported */
143 static unsigned int supported_sample_rates[] = {
144         8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
145 };
146
147 static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
148         .count = ARRAY_SIZE(supported_sample_rates),
149         .list = supported_sample_rates,
150         .mask = 0,
151 };
152
153 static void playback_event_handler(void *data)
154 {
155         struct msm_audio *prtd = data;
156         snd_pcm_period_elapsed(prtd->playback_substream);
157 }
158
159 static void capture_event_handler(void *data)
160 {
161         struct msm_audio *prtd = data;
162         snd_pcm_period_elapsed(prtd->capture_substream);
163 }
164
165 static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
166 {
167         struct snd_pcm_runtime *runtime = substream->runtime;
168         struct msm_audio *prtd = runtime->private_data;
169
170         prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
171         prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
172         prtd->pcm_irq_pos = 0;
173         prtd->pcm_buf_pos = 0;
174
175         /* rate and channels are sent to audio driver */
176         prtd->out_sample_rate = runtime->rate;
177         prtd->out_channel_mode = runtime->channels;
178
179         return 0;
180 }
181
182 static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
183 {
184         struct snd_pcm_runtime *runtime = substream->runtime;
185         struct msm_audio *prtd = runtime->private_data;
186         struct audmgr_config cfg;
187         int rc;
188
189         prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
190         prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
191         prtd->pcm_irq_pos = 0;
192         prtd->pcm_buf_pos = 0;
193
194         /* rate and channels are sent to audio driver */
195         prtd->samp_rate = convert_samp_rate(runtime->rate);
196         prtd->samp_rate_index = convert_dsp_samp_index(runtime->rate);
197         prtd->channel_mode = (runtime->channels - 1);
198         prtd->buffer_size = prtd->channel_mode ? STEREO_DATA_SIZE : \
199                                                         MONO_DATA_SIZE;
200
201         if (prtd->enabled == 1)
202                 return 0;
203
204         prtd->type = AUDREC_CMD_TYPE_0_INDEX_WAV;
205
206         cfg.tx_rate = convert_samp_rate(runtime->rate);
207         cfg.rx_rate = RPC_AUD_DEF_SAMPLE_RATE_NONE;
208         cfg.def_method = RPC_AUD_DEF_METHOD_RECORD;
209         cfg.codec = RPC_AUD_DEF_CODEC_PCM;
210         cfg.snd_method = RPC_SND_METHOD_MIDI;
211
212         rc = audmgr_enable(&prtd->audmgr, &cfg);
213         if (rc < 0)
214                 return rc;
215
216         if (msm_adsp_enable(prtd->audpre)) {
217                 audmgr_disable(&prtd->audmgr);
218                 return -ENODEV;
219         }
220         if (msm_adsp_enable(prtd->audrec)) {
221                 msm_adsp_disable(prtd->audpre);
222                 audmgr_disable(&prtd->audmgr);
223                 return -ENODEV;
224         }
225         prtd->enabled = 1;
226         alsa_rec_dsp_enable(prtd, 1);
227
228         return 0;
229 }
230
231 static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
232 {
233         int ret = 0;
234
235         switch (cmd) {
236         case SNDRV_PCM_TRIGGER_START:
237         case SNDRV_PCM_TRIGGER_RESUME:
238         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
239                 break;
240         case SNDRV_PCM_TRIGGER_STOP:
241         case SNDRV_PCM_TRIGGER_SUSPEND:
242         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
243                 break;
244         default:
245                 ret = -EINVAL;
246         }
247
248         return ret;
249 }
250
251 static snd_pcm_uframes_t
252 msm_pcm_playback_pointer(struct snd_pcm_substream *substream)
253 {
254         struct snd_pcm_runtime *runtime = substream->runtime;
255         struct msm_audio *prtd = runtime->private_data;
256
257         if (prtd->pcm_irq_pos == prtd->pcm_size)
258                 prtd->pcm_irq_pos = 0;
259         return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
260 }
261
262 static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
263                  int channel, snd_pcm_uframes_t hwoff, void __user *buf,
264                                                  snd_pcm_uframes_t frames)
265 {
266         int rc = 0, rc1 = 0, rc2 = 0;
267         int fbytes = 0;
268         struct snd_pcm_runtime *runtime = substream->runtime;
269         struct msm_audio *prtd = substream->runtime->private_data;
270
271         int monofbytes = 0;
272         char *bufferp = NULL;
273
274         fbytes = frames_to_bytes(runtime, frames);
275         monofbytes = fbytes / 2;
276         if (runtime->channels == 2) {
277                 rc = alsa_buffer_read(prtd, buf, fbytes, NULL);
278         } else {
279                 bufferp = buf;
280                 rc1 = alsa_buffer_read(prtd, bufferp, monofbytes, NULL);
281                 bufferp = buf + monofbytes ;
282                 rc2 = alsa_buffer_read(prtd, bufferp, monofbytes, NULL);
283                 rc = rc1 + rc2;
284         }
285         prtd->pcm_buf_pos += fbytes;
286         return rc;
287 }
288
289 static snd_pcm_uframes_t
290 msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
291 {
292         struct snd_pcm_runtime *runtime = substream->runtime;
293         struct msm_audio *prtd = runtime->private_data;
294
295         return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
296 }
297
298 static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
299 {
300         struct snd_pcm_runtime *runtime = substream->runtime;
301         struct msm_audio *prtd = runtime->private_data;
302
303         alsa_audrec_disable(prtd);
304         audmgr_close(&prtd->audmgr);
305         msm_adsp_put(prtd->audrec);
306         msm_adsp_put(prtd->audpre);
307         kfree(prtd);
308
309         return 0;
310 }
311
312 struct  msm_audio_event_callbacks snd_msm_audio_ops = {
313         .playback = playback_event_handler,
314         .capture = capture_event_handler,
315 };
316
317 static int msm_pcm_open(struct snd_pcm_substream *substream)
318 {
319         struct snd_pcm_runtime *runtime = substream->runtime;
320         struct msm_audio *prtd;
321         int ret = 0;
322
323         prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
324         if (prtd == NULL) {
325                 ret = -ENOMEM;
326                 return ret;
327         }
328         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
329                 msm_vol_ctl.update = 1; /* Update Volume, with Cached value */
330                 runtime->hw = msm_pcm_playback_hardware;
331                 prtd->dir = SNDRV_PCM_STREAM_PLAYBACK;
332                 prtd->playback_substream = substream;
333         } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
334                 runtime->hw = msm_pcm_capture_hardware;
335                 prtd->dir = SNDRV_PCM_STREAM_CAPTURE;
336                 prtd->capture_substream = substream;
337         }
338         ret = snd_pcm_hw_constraint_list(runtime, 0,
339                                                 SNDRV_PCM_HW_PARAM_RATE,
340                                                 &constraints_sample_rates);
341         if (ret < 0)
342                 goto out;
343         /* Ensure that buffer size is a multiple of period size */
344         ret = snd_pcm_hw_constraint_integer(runtime,
345                                             SNDRV_PCM_HW_PARAM_PERIODS);
346         if (ret < 0)
347                 goto out;
348
349         prtd->ops = &snd_msm_audio_ops;
350         prtd->out[0].used = BUF_INVALID_LEN;
351         prtd->out_head = 1; /* point to second buffer on startup */
352         runtime->private_data = prtd;
353
354         ret = alsa_adsp_configure(prtd);
355         if (ret)
356                 goto out;
357         copy_count = 0;
358         return 0;
359
360  out:
361         kfree(prtd);
362         return ret;
363 }
364
365 static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
366         snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
367 {
368         printk("running msm_pcm_playback_copy\n");
369         int rc = 1;
370         int fbytes = 0;
371
372         struct snd_pcm_runtime *runtime = substream->runtime;
373         struct msm_audio *prtd = runtime->private_data;
374
375         fbytes = frames_to_bytes(runtime, frames);
376         printk("msm_pcm_playback_copy: alsa_send_buffer\n");
377         rc = alsa_send_buffer(prtd, buf, fbytes, NULL);
378         ++copy_count;
379         prtd->pcm_buf_pos += fbytes;
380         if (copy_count == 1) {
381                 mutex_lock(&the_locks.lock);
382                 alsa_audio_configure(prtd);
383                 mutex_unlock(&the_locks.lock);
384         }
385         if ((prtd->running) && (msm_vol_ctl.update)) {
386                 rc = msm_audio_volume_update(PCMPLAYBACK_DECODERID, msm_vol_ctl.volume, msm_vol_ctl.pan);
387                 msm_vol_ctl.update = 0;
388         }
389         printk("finished msm_pcm_playback_copy\n");
390         return  rc;
391 }
392
393 static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
394 {
395         struct snd_pcm_runtime *runtime = substream->runtime;
396         struct msm_audio *prtd = runtime->private_data;
397
398         alsa_audio_disable(prtd);
399         audmgr_close(&prtd->audmgr);
400         kfree(prtd);
401
402         return 0;
403 }
404
405
406 static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
407          snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
408 {
409         int ret = 0;
410
411         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
412                 ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
413         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
414                 ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
415         return ret;
416 }
417
418 static int msm_pcm_close(struct snd_pcm_substream *substream)
419 {
420         int ret = 0;
421
422         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
423                 ret = msm_pcm_playback_close(substream);
424         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
425                 ret = msm_pcm_capture_close(substream);
426         return ret;
427 }
428 static int msm_pcm_prepare(struct snd_pcm_substream *substream)
429 {
430         int ret = 0;
431
432         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
433                 ret = msm_pcm_playback_prepare(substream);
434         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
435                 ret = msm_pcm_capture_prepare(substream);
436         return ret;
437 }
438
439 static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
440 {
441         snd_pcm_uframes_t ret = 0;
442
443         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
444                 ret = msm_pcm_playback_pointer(substream);
445         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
446                 ret = msm_pcm_capture_pointer(substream);
447         return ret;
448 }
449
450 int msm_pcm_hw_params(struct snd_pcm_substream *substream,
451                                 struct snd_pcm_hw_params *params)
452 {
453         struct snd_pcm_runtime *runtime = substream->runtime;
454
455         if (substream->pcm->device & 1) {
456                 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
457                 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
458         }
459         return 0;
460
461 }
462
463 static struct snd_pcm_ops msm_pcm_ops = {
464         .open           = msm_pcm_open,
465         .copy           = msm_pcm_copy,
466         .hw_params      = msm_pcm_hw_params,
467         .close          = msm_pcm_close,
468         .ioctl          = snd_pcm_lib_ioctl,
469         .prepare        = msm_pcm_prepare,
470         .trigger        = msm_pcm_trigger,
471         .pointer        = msm_pcm_pointer,
472 };
473
474
475
476 static int msm_pcm_remove(struct platform_device *devptr)
477 {
478         struct snd_soc_device *socdev = platform_get_drvdata(devptr);
479         snd_soc_free_pcms(socdev);
480         kfree(socdev->card->codec);
481         platform_set_drvdata(devptr, NULL);
482         return 0;
483 }
484
485 static int pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
486         int stream)
487 {
488         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
489         struct snd_dma_buffer *buf = &substream->dma_buffer;
490         size_t size;
491         if (!stream)
492                 size = PLAYBACK_DMASZ;
493         else
494                 size = CAPTURE_DMASZ;
495
496         buf->dev.type = SNDRV_DMA_TYPE_DEV;
497         buf->dev.dev = pcm->card->dev;
498         buf->private_data = NULL;
499         buf->area = dma_alloc_coherent(pcm->card->dev, size,
500                                            &buf->addr, GFP_KERNEL);
501         if (!buf->area)
502                 return -ENOMEM;
503
504         buf->bytes = size;
505         return 0;
506 }
507
508 static void msm_pcm_free_dma_buffers(struct snd_pcm *pcm)
509 {
510         struct snd_pcm_substream *substream;
511         struct snd_dma_buffer *buf;
512         int stream;
513
514         for (stream = 0; stream < 2; stream++) {
515                 substream = pcm->streams[stream].substream;
516                 if (!substream)
517                         continue;
518
519                 buf = &substream->dma_buffer;
520                 if (!buf->area)
521                         continue;
522
523                 dma_free_coherent(pcm->card->dev, buf->bytes,
524                                       buf->area, buf->addr);
525                 buf->area = NULL;
526         }
527 }
528
529 static int msm_pcm_new(struct snd_card *card,
530                         struct snd_soc_dai *codec_dai,
531                         struct snd_pcm *pcm)
532 {
533         int ret;
534         if (!card->dev->coherent_dma_mask)
535                 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
536
537         if (codec_dai->playback.channels_min) {
538                 ret = pcm_preallocate_dma_buffer(pcm,
539                         SNDRV_PCM_STREAM_PLAYBACK);
540                 if (ret)
541                         return ret;
542         }
543
544         if (codec_dai->capture.channels_min) {
545                 ret = pcm_preallocate_dma_buffer(pcm,
546                         SNDRV_PCM_STREAM_CAPTURE);
547                 if (ret)
548                         msm_pcm_free_dma_buffers(pcm);
549         }
550         return ret;
551 }
552
553 struct snd_soc_platform msm_soc_platform = {
554         .name           = "msm-audio",
555         .remove         = msm_pcm_remove,
556         .pcm_ops        = &msm_pcm_ops,
557         .pcm_new        = msm_pcm_new,
558         .pcm_free       = msm_pcm_free_dma_buffers,
559 };
560 EXPORT_SYMBOL(msm_soc_platform);
561
562 static int __init msm_soc_platform_init(void)
563 {
564         return snd_soc_register_platform(&msm_soc_platform);
565 }
566 module_init(msm_soc_platform_init);
567
568 static void __exit msm_soc_platform_exit(void)
569 {
570         snd_soc_unregister_platform(&msm_soc_platform);
571 }
572 module_exit(msm_soc_platform_exit);
573
574 MODULE_DESCRIPTION("PCM module platform driver");
575 MODULE_LICENSE("GPL v2");