asoc: tegra: pcm: Add support for setting bias level
[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-2012 - 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 <asm/mach-types.h>
33 #include <linux/module.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/slab.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40
41 #include "tegra_pcm.h"
42
43 #define DRV_NAME "tegra-pcm-audio"
44
45 static const struct snd_pcm_hardware tegra_pcm_hardware = {
46         .info                   = SNDRV_PCM_INFO_MMAP |
47                                   SNDRV_PCM_INFO_MMAP_VALID |
48                                   SNDRV_PCM_INFO_PAUSE |
49                                   SNDRV_PCM_INFO_RESUME |
50                                   SNDRV_PCM_INFO_INTERLEAVED,
51         .formats                = SNDRV_PCM_FMTBIT_S16_LE,
52         .channels_min           = 1,
53         .channels_max           = 2,
54         .period_bytes_min       = 128,
55         .period_bytes_max       = PAGE_SIZE,
56         .periods_min            = 2,
57         .periods_max            = 8,
58         .buffer_bytes_max       = PAGE_SIZE * 8,
59         .fifo_size              = 4,
60 };
61
62 static void tegra_pcm_queue_dma(struct tegra_runtime_data *prtd)
63 {
64         struct snd_pcm_substream *substream = prtd->substream;
65         struct snd_dma_buffer *buf = &substream->dma_buffer;
66         struct tegra_dma_req *dma_req;
67         unsigned long addr;
68
69         dma_req = &prtd->dma_req[prtd->dma_req_idx];
70         if (++prtd->dma_req_idx >= prtd->dma_req_count)
71                 prtd->dma_req_idx -= prtd->dma_req_count;
72
73         addr = buf->addr + prtd->dma_pos;
74         prtd->dma_pos += dma_req->size;
75         if (prtd->dma_pos >= prtd->dma_pos_end)
76                 prtd->dma_pos = 0;
77
78         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
79                 dma_req->source_addr = addr;
80         else
81                 dma_req->dest_addr = addr;
82
83         tegra_dma_enqueue_req(prtd->dma_chan, dma_req);
84 }
85
86 static void dma_complete_callback(struct tegra_dma_req *req)
87 {
88         struct tegra_runtime_data *prtd = (struct tegra_runtime_data *)req->dev;
89         struct snd_pcm_substream *substream = prtd->substream;
90         struct snd_pcm_runtime *runtime = substream->runtime;
91
92         spin_lock(&prtd->lock);
93
94         if (!prtd->running) {
95                 spin_unlock(&prtd->lock);
96                 return;
97         }
98
99         if (++prtd->period_index >= runtime->periods)
100                 prtd->period_index = 0;
101
102         tegra_pcm_queue_dma(prtd);
103
104         spin_unlock(&prtd->lock);
105
106         snd_pcm_period_elapsed(substream);
107 }
108
109 static void setup_dma_tx_request(struct tegra_dma_req *req,
110                                         struct tegra_pcm_dma_params * dmap)
111 {
112         req->complete = dma_complete_callback;
113         req->to_memory = false;
114         req->dest_addr = dmap->addr;
115         req->dest_wrap = dmap->wrap;
116         req->source_bus_width = 32;
117         req->source_wrap = 0;
118         req->dest_bus_width = dmap->width;
119         req->req_sel = dmap->req_sel;
120 }
121
122 static void setup_dma_rx_request(struct tegra_dma_req *req,
123                                         struct tegra_pcm_dma_params * dmap)
124 {
125         req->complete = dma_complete_callback;
126         req->to_memory = true;
127         req->source_addr = dmap->addr;
128         req->dest_wrap = 0;
129         req->source_bus_width = dmap->width;
130         req->source_wrap = dmap->wrap;
131         req->dest_bus_width = 32;
132         req->req_sel = dmap->req_sel;
133 }
134
135 int tegra_pcm_allocate(struct snd_pcm_substream *substream,
136                                         int dma_mode,
137                                         const struct snd_pcm_hardware *pcm_hardware)
138 {
139         struct snd_pcm_runtime *runtime = substream->runtime;
140         struct tegra_runtime_data *prtd;
141         struct snd_soc_pcm_runtime *rtd = substream->private_data;
142         struct tegra_pcm_dma_params * dmap;
143         int ret = 0;
144         int i = 0;
145
146         prtd = kzalloc(sizeof(struct tegra_runtime_data), GFP_KERNEL);
147         if (prtd == NULL)
148                 return -ENOMEM;
149
150         runtime->private_data = prtd;
151         prtd->substream = substream;
152
153         spin_lock_init(&prtd->lock);
154
155         dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
156         prtd->dma_req_count = MAX_DMA_REQ_COUNT;
157
158         if (dmap) {
159                 for (i = 0; i < prtd->dma_req_count; i++)
160                         prtd->dma_req[i].dev = prtd;
161
162                 prtd->dma_chan = tegra_dma_allocate_channel(
163                                         dma_mode,
164                                         "pcm");
165                 if (prtd->dma_chan == NULL) {
166                         ret = -ENOMEM;
167                         goto err;
168                 }
169         }
170
171         /* Set HW params now that initialization is complete */
172         snd_soc_set_runtime_hwparams(substream, pcm_hardware);
173
174         /* Ensure period size is multiple of 8 */
175         ret = snd_pcm_hw_constraint_step(runtime, 0,
176                 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 0x8);
177         if (ret < 0)
178                 goto err;
179
180         /* Ensure that buffer size is a multiple of period size */
181         ret = snd_pcm_hw_constraint_integer(runtime,
182                                                 SNDRV_PCM_HW_PARAM_PERIODS);
183         if (ret < 0)
184                 goto err;
185
186         return 0;
187
188 err:
189         if (prtd->dma_chan) {
190                 tegra_dma_free_channel(prtd->dma_chan);
191         }
192
193         kfree(prtd);
194
195         return ret;
196 }
197
198 static int tegra_pcm_open(struct snd_pcm_substream *substream)
199 {
200         return tegra_pcm_allocate(substream,
201                                         TEGRA_DMA_MODE_CONTINUOUS_SINGLE,
202                                         &tegra_pcm_hardware);
203
204 }
205
206 int tegra_pcm_close(struct snd_pcm_substream *substream)
207 {
208         struct snd_pcm_runtime *runtime = substream->runtime;
209         struct tegra_runtime_data *prtd = runtime->private_data;
210
211         if (prtd->dma_chan)
212                 tegra_dma_free_channel(prtd->dma_chan);
213
214         kfree(prtd);
215
216         return 0;
217 }
218
219 int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
220                                 struct snd_pcm_hw_params *params)
221 {
222         struct snd_pcm_runtime *runtime = substream->runtime;
223         struct tegra_runtime_data *prtd = runtime->private_data;
224         struct snd_soc_pcm_runtime *rtd = substream->private_data;
225         struct tegra_pcm_dma_params * dmap;
226         int i;
227
228         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
229
230         /* Limit dma_req_count to period count */
231         if (prtd->dma_req_count > params_periods(params))
232                 prtd->dma_req_count = params_periods(params);
233         dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
234         if (dmap) {
235                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
236                         for (i = 0; i < prtd->dma_req_count; i++)
237                                 setup_dma_tx_request(&prtd->dma_req[i], dmap);
238                 } else {
239                         for (i = 0; i < prtd->dma_req_count; i++)
240                                 setup_dma_rx_request(&prtd->dma_req[i], dmap);
241                 }
242         }
243         for (i = 0; i < prtd->dma_req_count; i++)
244                 prtd->dma_req[i].size = params_period_bytes(params);
245
246         return 0;
247 }
248
249 int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
250 {
251         snd_pcm_set_runtime_buffer(substream, NULL);
252
253         return 0;
254 }
255
256 int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
257 {
258         struct snd_pcm_runtime *runtime = substream->runtime;
259         struct tegra_runtime_data *prtd = runtime->private_data;
260         struct snd_soc_pcm_runtime *rtd = substream->private_data;
261         struct tegra_pcm_dma_params * dmap;
262         unsigned long flags;
263         int i;
264
265         dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
266         if (!dmap)
267                 return 0;
268
269         switch (cmd) {
270         case SNDRV_PCM_TRIGGER_START:
271                 prtd->dma_pos = 0;
272                 prtd->dma_pos_end = frames_to_bytes(runtime, runtime->periods * runtime->period_size);
273                 prtd->period_index = 0;
274                 prtd->dma_req_idx = 0;
275                 /* Fall-through */
276         case SNDRV_PCM_TRIGGER_RESUME:
277         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
278                 spin_lock_irqsave(&prtd->lock, flags);
279                 prtd->running = 1;
280                 spin_unlock_irqrestore(&prtd->lock, flags);
281                 for (i = 0; i < prtd->dma_req_count; i++)
282                         tegra_pcm_queue_dma(prtd);
283                 break;
284         case SNDRV_PCM_TRIGGER_STOP:
285         case SNDRV_PCM_TRIGGER_SUSPEND:
286         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
287                 spin_lock_irqsave(&prtd->lock, flags);
288                 prtd->running = 0;
289                 spin_unlock_irqrestore(&prtd->lock, flags);
290                 tegra_dma_cancel(prtd->dma_chan);
291                 for (i = 0; i < prtd->dma_req_count; i++) {
292                         if (prtd->dma_req[i].status ==
293                                 -TEGRA_DMA_REQ_ERROR_ABORTED)
294                                 prtd->dma_req[i].complete(&prtd->dma_req[i]);
295                 }
296                 break;
297         default:
298                 return -EINVAL;
299         }
300
301         return 0;
302 }
303
304 snd_pcm_uframes_t tegra_pcm_pointer(struct snd_pcm_substream *substream)
305 {
306         struct snd_pcm_runtime *runtime = substream->runtime;
307         struct tegra_runtime_data *prtd = runtime->private_data;
308         int dma_transfer_count;
309
310         dma_transfer_count = tegra_dma_get_transfer_count(prtd->dma_chan,
311                                         &prtd->dma_req[prtd->dma_req_idx]);
312
313         return prtd->period_index * runtime->period_size +
314                 bytes_to_frames(runtime, dma_transfer_count);
315 }
316
317 int tegra_pcm_mmap(struct snd_pcm_substream *substream,
318                                 struct vm_area_struct *vma)
319 {
320         struct snd_pcm_runtime *runtime = substream->runtime;
321
322         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
323                                         runtime->dma_area,
324                                         runtime->dma_addr,
325                                         runtime->dma_bytes);
326 }
327
328 static struct snd_pcm_ops tegra_pcm_ops = {
329         .open           = tegra_pcm_open,
330         .close          = tegra_pcm_close,
331         .ioctl          = snd_pcm_lib_ioctl,
332         .hw_params      = tegra_pcm_hw_params,
333         .hw_free        = tegra_pcm_hw_free,
334         .trigger        = tegra_pcm_trigger,
335         .pointer        = tegra_pcm_pointer,
336         .mmap           = tegra_pcm_mmap,
337 };
338
339 static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
340                                 int stream , size_t size)
341 {
342         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
343         struct snd_dma_buffer *buf = &substream->dma_buffer;
344
345         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
346                                                 &buf->addr, GFP_KERNEL);
347         if (!buf->area)
348                 return -ENOMEM;
349
350         buf->dev.type = SNDRV_DMA_TYPE_DEV;
351         buf->dev.dev = pcm->card->dev;
352         buf->private_data = NULL;
353         buf->bytes = size;
354
355         return 0;
356 }
357
358 void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
359 {
360         struct snd_pcm_substream *substream;
361         struct snd_dma_buffer *buf;
362
363         substream = pcm->streams[stream].substream;
364         if (!substream)
365                 return;
366
367         buf = &substream->dma_buffer;
368         if (!buf->area)
369                 return;
370
371         dma_free_writecombine(pcm->card->dev, buf->bytes,
372                                 buf->area, buf->addr);
373         buf->area = NULL;
374 }
375
376 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
377
378 int tegra_pcm_dma_allocate(struct snd_soc_pcm_runtime *rtd, size_t size)
379 {
380         struct snd_card *card = rtd->card->snd_card;
381         struct snd_pcm *pcm = rtd->pcm;
382         int ret = 0;
383
384         if (!card->dev->dma_mask)
385                 card->dev->dma_mask = &tegra_dma_mask;
386         if (!card->dev->coherent_dma_mask)
387                 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
388
389         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
390                 ret = tegra_pcm_preallocate_dma_buffer(pcm,
391                                                 SNDRV_PCM_STREAM_PLAYBACK,
392                                                 size);
393                 if (ret)
394                         goto err;
395         }
396
397         if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
398                 ret = tegra_pcm_preallocate_dma_buffer(pcm,
399                                                 SNDRV_PCM_STREAM_CAPTURE,
400                                                 size);
401                 if (ret)
402                         goto err_free_play;
403         }
404
405         return 0;
406
407 err_free_play:
408         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
409 err:
410         return ret;
411 }
412
413 int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
414 {
415         return tegra_pcm_dma_allocate(rtd ,
416                                         tegra_pcm_hardware.buffer_bytes_max);
417 }
418
419 void tegra_pcm_free(struct snd_pcm *pcm)
420 {
421         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
422         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
423 }
424
425 static int tegra_pcm_probe(struct snd_soc_platform *platform)
426 {
427         if(machine_is_kai() || machine_is_tegra_enterprise())
428                 platform->dapm.idle_bias_off = 1;
429
430         return 0;
431 }
432
433 static struct snd_soc_platform_driver tegra_pcm_platform = {
434         .ops            = &tegra_pcm_ops,
435         .pcm_new        = tegra_pcm_new,
436         .pcm_free       = tegra_pcm_free,
437         .probe          = tegra_pcm_probe,
438 };
439
440 static int __devinit tegra_pcm_platform_probe(struct platform_device *pdev)
441 {
442         return snd_soc_register_platform(&pdev->dev, &tegra_pcm_platform);
443 }
444
445 static int __devexit tegra_pcm_platform_remove(struct platform_device *pdev)
446 {
447         snd_soc_unregister_platform(&pdev->dev);
448         return 0;
449 }
450
451 static struct platform_driver tegra_pcm_driver = {
452         .driver = {
453                 .name = DRV_NAME,
454                 .owner = THIS_MODULE,
455         },
456         .probe = tegra_pcm_platform_probe,
457         .remove = __devexit_p(tegra_pcm_platform_remove),
458 };
459 module_platform_driver(tegra_pcm_driver);
460
461 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
462 MODULE_DESCRIPTION("Tegra PCM ASoC driver");
463 MODULE_LICENSE("GPL");
464 MODULE_ALIAS("platform:" DRV_NAME);