ada00e157edf3f3a8ded7b9016670c1b528382ac
[linux-3.10.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-2013, NVIDIA CORPORATION.  All rights reserved.
6  *
7  * Based on code copyright/by:
8  *
9  * Copyright (c) 2009-2013, NVIDIA CORPORATION.  All rights reserved.
10  * Scott Peterson <speterson@nvidia.com>
11  * Vijay Mali <vmali@nvidia.com>
12  * Manoj Gangwal <mgangwal@nvidia.com>
13  *
14  * Copyright (C) 2010 Google, Inc.
15  * Iliyan Malchev <malchev@google.com>
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * version 2 as published by the Free Software Foundation.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29  * 02110-1301 USA
30  *
31  */
32
33 #include <asm/mach-types.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/dmaengine_pcm.h>
42
43 #include "tegra_pcm.h"
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_S8 |
52                                   SNDRV_PCM_FMTBIT_S16_LE |
53                                   SNDRV_PCM_FMTBIT_S24_LE |
54                                   SNDRV_PCM_FMTBIT_S20_3LE |
55                                   SNDRV_PCM_FMTBIT_S32_LE,
56         .channels_min           = 1,
57         .channels_max           = 2,
58         .period_bytes_min       = 128,
59         .period_bytes_max       = PAGE_SIZE * 2,
60         .periods_min            = 1,
61         .periods_max            = 8,
62         .buffer_bytes_max       = PAGE_SIZE * 8,
63         .fifo_size              = 4,
64 };
65
66 static int tegra_pcm_open(struct snd_pcm_substream *substream)
67 {
68         struct snd_soc_pcm_runtime *rtd = substream->private_data;
69         struct device *dev = rtd->platform->dev;
70         struct tegra_runtime_data *prtd;
71         int ret;
72
73         prtd = kzalloc(sizeof(struct tegra_runtime_data), GFP_KERNEL);
74         if (prtd == NULL)
75                 return -ENOMEM;
76
77         /* Set HW params now that initialization is complete */
78         snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
79
80         /* Ensure period size is multiple of 8 */
81         ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
82                 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 0x8);
83         if (ret) {
84                 dev_err(dev, "failed to set constraint %d\n", ret);
85                 kfree(prtd);
86                 return ret;
87         }
88
89         ret = snd_dmaengine_pcm_open_request_chan(substream, NULL, NULL);
90         if (ret) {
91                 dev_err(dev, "dmaengine pcm open failed with err %d\n", ret);
92                 kfree(prtd);
93                 return ret;
94         }
95
96         return 0;
97 }
98
99 static int tegra_pcm_close(struct snd_pcm_substream *substream)
100 {
101         snd_dmaengine_pcm_close_release_chan(substream);
102         return 0;
103 }
104
105 int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
106                                 struct snd_pcm_hw_params *params)
107 {
108         struct snd_soc_pcm_runtime *rtd = substream->private_data;
109         struct device *dev = rtd->platform->dev;
110         struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
111         struct tegra_pcm_dma_params *dmap;
112         struct dma_slave_config slave_config;
113         int ret;
114
115         dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
116
117         ret = snd_hwparams_to_dma_slave_config(substream, params,
118                                                 &slave_config);
119         if (ret) {
120                 dev_err(dev, "hw params config failed with err %d\n", ret);
121                 return ret;
122         }
123
124         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
125                 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
126                 slave_config.dst_addr = dmap->addr;
127                 slave_config.dst_maxburst = 4;
128         } else {
129                 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
130                 slave_config.src_addr = dmap->addr;
131                 slave_config.src_maxburst = 4;
132         }
133         slave_config.slave_id = dmap->req_sel;
134
135         ret = dmaengine_slave_config(chan, &slave_config);
136         if (ret < 0) {
137                 dev_err(dev, "dma slave config failed with err %d\n", ret);
138                 return ret;
139         }
140
141         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
142         return 0;
143 }
144
145 int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
146 {
147         snd_pcm_set_runtime_buffer(substream, NULL);
148         return 0;
149 }
150
151 int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
152 {
153         struct snd_soc_pcm_runtime *rtd = substream->private_data;
154         struct tegra_pcm_dma_params * dmap;
155
156         dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
157
158         if (!dmap)
159                 return 0;
160
161         switch (cmd) {
162         case SNDRV_PCM_TRIGGER_START:
163         case SNDRV_PCM_TRIGGER_RESUME:
164         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
165
166                 return snd_dmaengine_pcm_trigger(substream,
167                                         SNDRV_PCM_TRIGGER_START);
168
169         case SNDRV_PCM_TRIGGER_STOP:
170         case SNDRV_PCM_TRIGGER_SUSPEND:
171         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
172
173                 return snd_dmaengine_pcm_trigger(substream,
174                                         SNDRV_PCM_TRIGGER_STOP);
175         default:
176                 return -EINVAL;
177         }
178         return 0;
179 }
180
181 static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
182                                 struct vm_area_struct *vma)
183 {
184         struct snd_pcm_runtime *runtime = substream->runtime;
185
186         return dma_mmap_writecombine(substream->pcm->card->dev, vma,
187                                         runtime->dma_area,
188                                         runtime->dma_addr,
189                                         runtime->dma_bytes);
190 }
191
192 static struct snd_pcm_ops tegra_pcm_ops = {
193         .open           = tegra_pcm_open,
194         .close          = tegra_pcm_close,
195         .ioctl          = snd_pcm_lib_ioctl,
196         .hw_params      = tegra_pcm_hw_params,
197         .hw_free        = tegra_pcm_hw_free,
198         .trigger        = tegra_pcm_trigger,
199         .pointer        = snd_dmaengine_pcm_pointer,
200         .mmap           = tegra_pcm_mmap,
201 };
202
203 static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
204                                 int stream , size_t size)
205 {
206         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
207         struct snd_dma_buffer *buf = &substream->dma_buffer;
208 #if TEGRA30_USE_SMMU
209         unsigned char *vaddr;
210         phys_addr_t paddr;
211         struct tegra_smmu_data *ptsd;
212
213         ptsd = kzalloc(sizeof(struct tegra_smmu_data), GFP_KERNEL);
214         ptsd->pcm_nvmap_client = nvmap_create_client(nvmap_dev, "Audio_SMMU");
215         ptsd->pcm_nvmap_handle = nvmap_alloc(ptsd->pcm_nvmap_client,
216                                  size,
217                                  32,
218                                  NVMAP_HANDLE_WRITE_COMBINE,
219                                  NVMAP_HEAP_IOVMM);
220
221         vaddr = (unsigned char *) nvmap_mmap(ptsd->pcm_nvmap_handle);
222         paddr = nvmap_pin(ptsd->pcm_nvmap_client, ptsd->pcm_nvmap_handle);
223         buf->area = vaddr;
224         buf->addr = paddr;
225         buf->private_data = ptsd;
226 #else
227         buf->area = dma_alloc_writecombine(pcm->card->dev, size,
228                                                 &buf->addr, GFP_KERNEL);
229         if (!buf->area)
230                 return -ENOMEM;
231         buf->private_data = NULL;
232 #endif
233         buf->dev.type = SNDRV_DMA_TYPE_DEV;
234         buf->dev.dev = pcm->card->dev;
235         buf->bytes = size;
236
237         return 0;
238 }
239
240 void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
241 {
242         struct snd_pcm_substream *substream;
243         struct snd_dma_buffer *buf;
244 #if TEGRA30_USE_SMMU
245         struct tegra_smmu_data *ptsd;
246 #endif
247
248         substream = pcm->streams[stream].substream;
249         if (!substream)
250                 return;
251
252         buf = &substream->dma_buffer;
253         if (!buf->area)
254                 return;
255
256 #if TEGRA30_USE_SMMU
257         if (!buf->private_data)
258                 return;
259         ptsd = (struct tegra_smmu_data *)buf->private_data;
260         nvmap_unpin(ptsd->pcm_nvmap_client, ptsd->pcm_nvmap_handle);
261         nvmap_munmap(ptsd->pcm_nvmap_handle, buf->area);
262         nvmap_free(ptsd->pcm_nvmap_client, ptsd->pcm_nvmap_handle);
263         kfree(ptsd);
264         buf->private_data = NULL;
265 #else
266         dma_free_writecombine(pcm->card->dev, buf->bytes,
267                                 buf->area, buf->addr);
268 #endif
269         buf->area = NULL;
270 }
271
272 static u64 tegra_dma_mask = DMA_BIT_MASK(32);
273
274 int tegra_pcm_dma_allocate(struct snd_soc_pcm_runtime *rtd, size_t size)
275 {
276         struct snd_card *card = rtd->card->snd_card;
277         struct snd_pcm *pcm = rtd->pcm;
278         int ret = 0;
279
280         if (!card->dev->dma_mask)
281                 card->dev->dma_mask = &tegra_dma_mask;
282         if (!card->dev->coherent_dma_mask)
283                 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
284
285         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
286                 ret = tegra_pcm_preallocate_dma_buffer(pcm,
287                                                 SNDRV_PCM_STREAM_PLAYBACK,
288                                                 size);
289                 if (ret)
290                         goto err;
291         }
292
293         if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
294                 ret = tegra_pcm_preallocate_dma_buffer(pcm,
295                                                 SNDRV_PCM_STREAM_CAPTURE,
296                                                 size);
297                 if (ret)
298                         goto err_free_play;
299         }
300
301         return 0;
302
303 err_free_play:
304         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
305 err:
306         return ret;
307 }
308
309 int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
310 {
311         return tegra_pcm_dma_allocate(rtd ,
312                                         tegra_pcm_hardware.buffer_bytes_max);
313 }
314
315 void tegra_pcm_free(struct snd_pcm *pcm)
316 {
317         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
318         tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
319 }
320
321 static int tegra_pcm_probe(struct snd_soc_platform *platform)
322 {
323         platform->dapm.idle_bias_off = 1;
324         return 0;
325 }
326
327 static struct snd_soc_platform_driver tegra_pcm_platform = {
328         .ops            = &tegra_pcm_ops,
329         .pcm_new        = tegra_pcm_new,
330         .pcm_free       = tegra_pcm_free,
331         .probe          = tegra_pcm_probe,
332 };
333
334 int tegra_pcm_platform_register(struct device *dev)
335 {
336         return snd_soc_register_platform(dev, &tegra_pcm_platform);
337 }
338 EXPORT_SYMBOL_GPL(tegra_pcm_platform_register);
339
340 void tegra_pcm_platform_unregister(struct device *dev)
341 {
342         snd_soc_unregister_platform(dev);
343 }
344 EXPORT_SYMBOL_GPL(tegra_pcm_platform_unregister);
345
346 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
347 MODULE_DESCRIPTION("Tegra PCM ASoC driver");
348 MODULE_LICENSE("GPL");