a27f65f68325ac9d3bce4a537c6e1667a69c9833
[linux-2.6.git] / sound / soc / tegra / tegra_pcm.c
1 /*
2  * tegra_pcm.c - Tegra PCM driver
3  *
4  * Author: Stephen Warren <swarren@nvidia.com>
5  * Copyright (C) 2010 - NVIDIA, Inc.
6  *
7  * Based on code copyright/by:
8  *
9  * Copyright (c) 2009-2010, NVIDIA Corporation.
10  * Scott Peterson <speterson@nvidia.com>
11  * Vijay Mali <vmali@nvidia.com>
12  *
13  * Copyright (C) 2010 Google, Inc.
14  * Iliyan Malchev <malchev@google.com>
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * version 2 as published by the Free Software Foundation.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28  * 02110-1301 USA
29  *
30  */
31
32 #include <linux/module.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/slab.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39
40 #include "tegra_pcm.h"
41
42 #define DRV_NAME "tegra-pcm-audio"
43
44 static const struct snd_pcm_hardware tegra_pcm_hardware = {
45         .info                   = SNDRV_PCM_INFO_MMAP |
46                                   SNDRV_PCM_INFO_MMAP_VALID |
47                                   SNDRV_PCM_INFO_PAUSE |
48                                   SNDRV_PCM_INFO_RESUME |
49                                   SNDRV_PCM_INFO_INTERLEAVED,
50         .formats                = SNDRV_PCM_FMTBIT_S16_LE,
51         .channels_min           = 1,
52         .channels_max           = 2,
53         .period_bytes_min       = 128,
54         .period_bytes_max       = PAGE_SIZE,
55         .periods_min            = 2,
56         .periods_max            = 8,
57         .buffer_bytes_max       = PAGE_SIZE * 8,
58         .fifo_size              = 4,
59 };
60
61 static void tegra_pcm_queue_dma(struct tegra_runtime_data *prtd)
62 {
63         struct snd_pcm_substream *substream = prtd->substream;
64         struct snd_dma_buffer *buf = &substream->dma_buffer;
65         struct tegra_dma_req *dma_req;
66         unsigned long addr;
67
68         dma_req = &prtd->dma_req[prtd->dma_req_idx];
69         prtd->dma_req_idx = 1 - prtd->dma_req_idx;
70
71         addr = buf->addr + prtd->dma_pos;
72         prtd->dma_pos += dma_req->size;
73         if (prtd->dma_pos >= prtd->dma_pos_end)
74                 prtd->dma_pos = 0;
75
76         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
77                 dma_req->source_addr = addr;
78         else
79                 dma_req->dest_addr = addr;
80
81         tegra_dma_enqueue_req(prtd->dma_chan, dma_req);
82 }
83
84 static void dma_complete_callback(struct tegra_dma_req *req)
85 {
86         struct tegra_runtime_data *prtd = (struct tegra_runtime_data *)req->dev;
87         struct snd_pcm_substream *substream = prtd->substream;
88         struct snd_pcm_runtime *runtime = substream->runtime;
89
90         spin_lock(&prtd->lock);
91
92         if (!prtd->running) {
93                 spin_unlock(&prtd->lock);
94                 return;
95         }
96
97         if (++prtd->period_index >= runtime->periods)
98                 prtd->period_index = 0;
99
100         tegra_pcm_queue_dma(prtd);
101
102         spin_unlock(&prtd->lock);
103
104         snd_pcm_period_elapsed(substream);
105 }
106
107 static void setup_dma_tx_request(struct tegra_dma_req *req,
108                                         struct tegra_pcm_dma_params * dmap)
109 {
110         req->complete = dma_complete_callback;
111         req->to_memory = false;
112         req->dest_addr = dmap->addr;
113         req->dest_wrap = dmap->wrap;
114         req->source_bus_width = 32;
115         req->source_wrap = 0;
116         req->dest_bus_width = dmap->width;
117         req->req_sel = dmap->req_sel;
118 }
119
120 static void setup_dma_rx_request(struct tegra_dma_req *req,
121                                         struct tegra_pcm_dma_params * dmap)
122 {
123         req->complete = dma_complete_callback;
124         req->to_memory = true;
125         req->source_addr = dmap->addr;
126         req->dest_wrap = 0;
127         req->source_bus_width = dmap->width;
128         req->source_wrap = dmap->wrap;
129         req->dest_bus_width = 32;
130         req->req_sel = dmap->req_sel;
131 }
132
133 static int tegra_pcm_open(struct snd_pcm_substream *substream)
134 {
135         struct snd_pcm_runtime *runtime = substream->runtime;
136         struct tegra_runtime_data *prtd;
137         struct snd_soc_pcm_runtime *rtd = substream->private_data;
138         struct tegra_pcm_dma_params * dmap;
139         int ret = 0;
140
141         prtd = kzalloc(sizeof(struct tegra_runtime_data), GFP_KERNEL);
142         if (prtd == NULL)
143                 return -ENOMEM;
144
145         runtime->private_data = prtd;
146         prtd->substream = substream;
147
148         spin_lock_init(&prtd->lock);
149
150         dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
151
152         if (dmap) {
153                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
154                         setup_dma_tx_request(&prtd->dma_req[0], dmap);
155                         setup_dma_tx_request(&prtd->dma_req[1], dmap);
156                 } else {
157                         setup_dma_rx_request(&prtd->dma_req[0], dmap);
158                         setup_dma_rx_request(&prtd->dma_req[1], dmap);
159                 }
160
161                 prtd->dma_req[0].dev = prtd;
162                 prtd->dma_req[1].dev = prtd;
163
164                 prtd->dma_chan = tegra_dma_allocate_channel(
165                                         TEGRA_DMA_MODE_CONTINUOUS_SINGLE,
166                                         "pcm");
167                 if (prtd->dma_chan == NULL) {
168                         ret = -ENOMEM;
169                         goto err;
170                 }
171         }
172
173         /* Set HW params now that initialization is complete */
174         snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
175
176         /* Ensure period size is multiple of 8 */
177         ret = snd_pcm_hw_constraint_step(runtime, 0,
178                 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 0x8);
179         if (ret < 0)
180                 goto err;
181
182         /* Ensure that buffer size is a multiple of period size */
183         ret = snd_pcm_hw_constraint_integer(runtime,
184                                                 SNDRV_PCM_HW_PARAM_PERIODS);
185         if (ret < 0)
186                 goto err;
187
188 #ifdef CONFIG_HAS_WAKELOCK
189         snprintf(prtd->tegra_wake_lock_name, sizeof(prtd->tegra_wake_lock_name),
190                 "tegra-pcm-%s-%d",
191                 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? "out" : "in",
192                 substream->pcm->device);
193         wake_lock_init(&prtd->tegra_wake_lock, WAKE_LOCK_SUSPEND,
194                 prtd->tegra_wake_lock_name);
195 #endif
196
197         return 0;
198
199 err:
200         if (prtd->dma_chan) {
201                 tegra_dma_free_channel(prtd->dma_chan);
202         }
203
204         kfree(prtd);
205
206         return ret;
207 }
208
209 static int tegra_pcm_close(struct snd_pcm_substream *substream)
210 {
211         struct snd_pcm_runtime *runtime = substream->runtime;
212         struct tegra_runtime_data *prtd = runtime->private_data;
213
214 #ifdef CONFIG_HAS_WAKELOCK
215         wake_lock_destroy(&prtd->tegra_wake_lock);
216 #endif
217
218         if (prtd->dma_chan)
219                 tegra_dma_free_channel(prtd->dma_chan);
220
221         kfree(prtd);
222
223         return 0;
224 }
225
226 static int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
227                                 struct snd_pcm_hw_params *params)
228 {
229         struct snd_pcm_runtime *runtime = substream->runtime;
230         struct tegra_runtime_data *prtd = runtime->private_data;
231
232         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
233
234         prtd->dma_req[0].size = params_period_bytes(params);
235         prtd->dma_req[1].size = prtd->dma_req[0].size;
236
237         return 0;
238 }
239
240 static int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
241 {
242         snd_pcm_set_runtime_buffer(substream, NULL);
243
244         return 0;
245 }
246
247 static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
248 {
249         struct snd_pcm_runtime *runtime = substream->runtime;
250         struct tegra_runtime_data *prtd = runtime->private_data;
251         unsigned long flags;
252
253         switch (cmd) {
254         case SNDRV_PCM_TRIGGER_START:
255                 prtd->dma_pos = 0;
256                 prtd->dma_pos_end = frames_to_bytes(runtime, runtime->periods * runtime->period_size);
257                 prtd->period_index = 0;
258                 prtd->dma_req_idx = 0;
259                 /* Fall-through */
260         case SNDRV_PCM_TRIGGER_RESUME:
261         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
262 #ifdef CONFIG_HAS_WAKELOCK
263         wake_lock(&prtd->tegra_wake_lock);
264 #endif
265                 spin_lock_irqsave(&prtd->lock, flags);
266                 prtd->running = 1;
267                 spin_unlock_irqrestore(&prtd->lock, flags);
268                 tegra_pcm_queue_dma(prtd);
269                 tegra_pcm_queue_dma(prtd);
270                 break;
271         case SNDRV_PCM_TRIGGER_STOP:
272         case SNDRV_PCM_TRIGGER_SUSPEND:
273         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
274                 spin_lock_irqsave(&prtd->lock, flags);
275                 prtd->running = 0;
276                 spin_unlock_irqrestore(&prtd->lock, flags);
277                 tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[0]);
278                 tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[1]);
279
280 #ifdef CONFIG_HAS_WAKELOCK
281                 wake_unlock(&prtd->tegra_wake_lock);
282 #endif
283                 break;
284         default:
285                 return -EINVAL;
286         }
287
288         return 0;
289 }
290
291 static snd_pcm_uframes_t tegra_pcm_pointer(struct snd_pcm_substream *substream)
292 {
293         struct snd_pcm_runtime *runtime = substream->runtime;
294         struct tegra_runtime_data *prtd = runtime->private_data;
295         int dma_transfer_count;
296
297         dma_transfer_count = tegra_dma_get_transfer_count(prtd->dma_chan,
298                                         &prtd->dma_req[prtd->dma_req_idx],
299                                         false);
300
301         return prtd->period_index * runtime->period_size +
302                 bytes_to_frames(runtime, dma_transfer_count);
303 }
304
305 static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
306                                 struct vm_area_struct *vma)
307 {
308         struct snd_pcm_runtime *runtime = substream->runtime;
309
310         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
311                                         runtime->dma_area,
312                                         runtime->dma_addr,
313                                         runtime->dma_bytes);
314 }
315
316 static struct snd_pcm_ops tegra_pcm_ops = {
317         .open           = tegra_pcm_open,
318         .close          = tegra_pcm_close,
319         .ioctl          = snd_pcm_lib_ioctl,
320         .hw_params      = tegra_pcm_hw_params,
321         .hw_free        = tegra_pcm_hw_free,
322         .trigger        = tegra_pcm_trigger,
323         .pointer        = tegra_pcm_pointer,
324         .mmap           = tegra_pcm_mmap,
325 };
326
327 static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
328 {
329         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
330         struct snd_dma_buffer *buf = &substream->dma_buffer;
331         size_t size = tegra_pcm_hardware.buffer_bytes_max;
332
333         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
334                                                 &buf->addr, GFP_KERNEL);
335         if (!buf->area)
336                 return -ENOMEM;
337
338         buf->dev.type = SNDRV_DMA_TYPE_DEV;
339         buf->dev.dev = pcm->card->dev;
340         buf->private_data = NULL;
341         buf->bytes = size;
342
343         return 0;
344 }
345
346 static void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
347 {
348         struct snd_pcm_substream *substream;
349         struct snd_dma_buffer *buf;
350
351         substream = pcm->streams[stream].substream;
352         if (!substream)
353                 return;
354
355         buf = &substream->dma_buffer;
356         if (!buf->area)
357                 return;
358
359         dma_free_writecombine(pcm->card->dev, buf->bytes,
360                                 buf->area, buf->addr);
361         buf->area = NULL;
362 }
363
364 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
365
366 static int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
367 {
368         struct snd_card *card = rtd->card->snd_card;
369         struct snd_soc_dai *dai = rtd->cpu_dai;
370         struct snd_pcm *pcm = rtd->pcm;
371         int ret = 0;
372
373         if (!card->dev->dma_mask)
374                 card->dev->dma_mask = &tegra_dma_mask;
375         if (!card->dev->coherent_dma_mask)
376                 card->dev->coherent_dma_mask = 0xffffffff;
377
378         if (dai->driver->playback.channels_min) {
379                 ret = tegra_pcm_preallocate_dma_buffer(pcm,
380                                                 SNDRV_PCM_STREAM_PLAYBACK);
381                 if (ret)
382                         goto err;
383         }
384
385         if (dai->driver->capture.channels_min) {
386                 ret = tegra_pcm_preallocate_dma_buffer(pcm,
387                                                 SNDRV_PCM_STREAM_CAPTURE);
388                 if (ret)
389                         goto err_free_play;
390         }
391
392         return 0;
393
394 err_free_play:
395         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
396 err:
397         return ret;
398 }
399
400 static void tegra_pcm_free(struct snd_pcm *pcm)
401 {
402         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
403         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
404 }
405
406 struct snd_soc_platform_driver tegra_pcm_platform = {
407         .ops            = &tegra_pcm_ops,
408         .pcm_new        = tegra_pcm_new,
409         .pcm_free       = tegra_pcm_free,
410 };
411
412 static int __devinit tegra_pcm_platform_probe(struct platform_device *pdev)
413 {
414         return snd_soc_register_platform(&pdev->dev, &tegra_pcm_platform);
415 }
416
417 static int __devexit tegra_pcm_platform_remove(struct platform_device *pdev)
418 {
419         snd_soc_unregister_platform(&pdev->dev);
420         return 0;
421 }
422
423 static struct platform_driver tegra_pcm_driver = {
424         .driver = {
425                 .name = DRV_NAME,
426                 .owner = THIS_MODULE,
427         },
428         .probe = tegra_pcm_platform_probe,
429         .remove = __devexit_p(tegra_pcm_platform_remove),
430 };
431
432 static int __init snd_tegra_pcm_init(void)
433 {
434         return platform_driver_register(&tegra_pcm_driver);
435 }
436 module_init(snd_tegra_pcm_init);
437
438 static void __exit snd_tegra_pcm_exit(void)
439 {
440         platform_driver_unregister(&tegra_pcm_driver);
441 }
442 module_exit(snd_tegra_pcm_exit);
443
444 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
445 MODULE_DESCRIPTION("Tegra PCM ASoC driver");
446 MODULE_LICENSE("GPL");
447 MODULE_ALIAS("platform:" DRV_NAME);