power: first sketch at delivering wakeup reason to userland via /proc interface
[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 =     MIN_PERIOD_SIZE,
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                 prtd->eos_ack = 0;
334         } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
335                 runtime->hw = msm_pcm_capture_hardware;
336                 prtd->dir = SNDRV_PCM_STREAM_CAPTURE;
337                 prtd->capture_substream = substream;
338         }
339         ret = snd_pcm_hw_constraint_list(runtime, 0,
340                                                 SNDRV_PCM_HW_PARAM_RATE,
341                                                 &constraints_sample_rates);
342         if (ret < 0)
343                 goto out;
344         /* Ensure that buffer size is a multiple of period size */
345         ret = snd_pcm_hw_constraint_integer(runtime,
346                                             SNDRV_PCM_HW_PARAM_PERIODS);
347         if (ret < 0)
348                 goto out;
349
350         prtd->ops = &snd_msm_audio_ops;
351         prtd->out[0].used = BUF_INVALID_LEN;
352         prtd->out_head = 1; /* point to second buffer on startup */
353         runtime->private_data = prtd;
354
355         ret = alsa_adsp_configure(prtd);
356         if (ret)
357                 goto out;
358         copy_count = 0;
359         return 0;
360
361  out:
362         kfree(prtd);
363         return ret;
364 }
365
366 static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
367         snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
368 {
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         rc = alsa_send_buffer(prtd, buf, fbytes, NULL,copy_count);
377         ++copy_count;
378         prtd->pcm_buf_pos += fbytes;
379         if ((prtd->running) && (msm_vol_ctl.update)) {
380                 rc = msm_audio_volume_update(PCMPLAYBACK_DECODERID,
381                                 msm_vol_ctl.volume, msm_vol_ctl.pan);
382                 msm_vol_ctl.update = 0;
383         }
384
385         return  rc;
386 }
387
388 static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
389 {
390         struct snd_pcm_runtime *runtime = substream->runtime;
391         struct msm_audio *prtd = runtime->private_data;
392 #if 0
393         int rc = 0;
394
395         pr_debug("%s()\n", __func__);
396
397         /* pcm dmamiss message is sent continously
398          * when decoder is starved so no race
399          * condition concern
400          */
401         if (prtd->enabled)
402                 rc = wait_event_interruptible(the_locks.eos_wait,
403                                         prtd->eos_ack);
404 #endif
405         alsa_audio_disable(prtd);
406         audmgr_close(&prtd->audmgr);
407         kfree(prtd);
408
409         return 0;
410 }
411
412
413 static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
414          snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
415 {
416         int ret = 0;
417
418         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
419                 ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
420         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
421                 ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
422         return ret;
423 }
424
425 static int msm_pcm_silence(struct snd_pcm_substream *substream, 
426          int channels,snd_pcm_uframes_t pos, snd_pcm_uframes_t frames)
427 {
428         return 0;
429 }
430
431 static int msm_pcm_close(struct snd_pcm_substream *substream)
432 {
433         int ret = 0;
434
435         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
436                 ret = msm_pcm_playback_close(substream);
437         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
438                 ret = msm_pcm_capture_close(substream);
439         return ret;
440 }
441 static int msm_pcm_prepare(struct snd_pcm_substream *substream)
442 {
443         int ret = 0;
444
445         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
446                 ret = msm_pcm_playback_prepare(substream);
447         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
448                 ret = msm_pcm_capture_prepare(substream);
449         return ret;
450 }
451
452 static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
453 {
454         snd_pcm_uframes_t ret = 0;
455
456         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
457                 ret = msm_pcm_playback_pointer(substream);
458         else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
459                 ret = msm_pcm_capture_pointer(substream);
460         return ret;
461 }
462
463 int msm_pcm_hw_params(struct snd_pcm_substream *substream,
464                                 struct snd_pcm_hw_params *params)
465 {
466         struct snd_pcm_runtime *runtime = substream->runtime;
467
468         if (substream->pcm->device & 1) {
469                 runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED;
470                 runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED;
471         }
472         return 0;
473
474 }
475
476 static struct snd_pcm_ops msm_pcm_ops = {
477         .open           = msm_pcm_open,
478         .copy           = msm_pcm_copy,
479         .silence        = msm_pcm_silence,
480         .hw_params      = msm_pcm_hw_params,
481         .close          = msm_pcm_close,
482         .ioctl          = snd_pcm_lib_ioctl,
483         .prepare        = msm_pcm_prepare,
484         .trigger        = msm_pcm_trigger,
485         .pointer        = msm_pcm_pointer,
486 };
487
488
489
490 static int msm_pcm_remove(struct platform_device *devptr)
491 {
492 #if 1
493         struct snd_soc_device *socdev = platform_get_drvdata(devptr);
494         snd_soc_free_pcms(socdev);
495         kfree(socdev->card->codec);
496         platform_set_drvdata(devptr, NULL);
497         return 0;
498 #endif
499
500 #if 0
501         printk("DISABLED %s\n", __func__);
502         return -1;
503 #endif
504 }
505
506 static int pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
507         int stream)
508 {
509         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
510         struct snd_dma_buffer *buf = &substream->dma_buffer;
511         size_t size;
512         if (!stream)
513                 size = PLAYBACK_DMASZ;
514         else
515                 size = CAPTURE_DMASZ;
516
517         buf->dev.type = SNDRV_DMA_TYPE_DEV;
518         buf->dev.dev = pcm->card->dev;
519         buf->private_data = NULL;
520         buf->area = dma_alloc_coherent(pcm->card->dev, size,
521                                            &buf->addr, GFP_KERNEL);
522         if (!buf->area)
523                 return -ENOMEM;
524
525         buf->bytes = size;
526         return 0;
527 }
528
529 static void msm_pcm_free_dma_buffers(struct snd_pcm *pcm)
530 {
531         struct snd_pcm_substream *substream;
532         struct snd_dma_buffer *buf;
533         int stream;
534
535         for (stream = 0; stream < 2; stream++) {
536                 substream = pcm->streams[stream].substream;
537                 if (!substream)
538                         continue;
539
540                 buf = &substream->dma_buffer;
541                 if (!buf->area)
542                         continue;
543
544                 dma_free_coherent(pcm->card->dev, buf->bytes,
545                                       buf->area, buf->addr);
546                 buf->area = NULL;
547         }
548 }
549
550 static int msm_pcm_new(struct snd_card *card,
551                         struct snd_soc_dai *codec_dai,
552                         struct snd_pcm *pcm)
553 {
554         int ret;
555         if (!card->dev->coherent_dma_mask)
556                 card->dev->coherent_dma_mask = DMA_32BIT_MASK;
557
558         ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, 1);
559         if (ret)
560                 return ret;
561         ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
562         if (ret)
563                 return ret;
564         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &msm_pcm_ops);
565         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &msm_pcm_ops);
566
567         ret = pcm_preallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
568         if (ret)
569                 return ret;
570
571         ret = pcm_preallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
572         if (ret)
573                 msm_pcm_free_dma_buffers(pcm);
574         return ret;
575 }
576
577 struct snd_soc_platform msm_soc_platform = {
578         .name           = "msm-audio",
579         .remove         = msm_pcm_remove,
580         .pcm_ops        = &msm_pcm_ops,
581         .pcm_new        = msm_pcm_new,
582         .pcm_free       = msm_pcm_free_dma_buffers,
583 };
584 EXPORT_SYMBOL(msm_soc_platform);
585
586 static int __init msm_soc_platform_init(void)
587 {
588         return snd_soc_register_platform(&msm_soc_platform);
589 }
590 module_init(msm_soc_platform_init);
591
592 static void __exit msm_soc_platform_exit(void)
593 {
594         snd_soc_unregister_platform(&msm_soc_platform);
595 }
596 module_exit(msm_soc_platform_exit);
597
598 MODULE_DESCRIPTION("PCM module platform driver");
599 MODULE_LICENSE("GPL v2");