c71c5b48b555e42fe531a4c5bf8e3c5fc5542398
[librecmc/librecmc.git] / target / linux / xburst / files-2.6.32 / sound / soc / jz4740 / jz4740-pcm.c
1 /*
2  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3  *
4  *  This program is free software; you can redistribute  it and/or modify it
5  *  under  the terms of  the GNU General  Public License as published by the
6  *  Free Software Foundation;  either version 2 of the  License, or (at your
7  *  option) any later version.
8  *
9  *  You should have received a copy of the  GNU General Public License along
10  *  with this program; if not, write  to the Free Software Foundation, Inc.,
11  *  675 Mass Ave, Cambridge, MA 02139, USA.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/dma-mapping.h>
20
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25
26 #include <asm/mach-jz4740/dma.h>
27 #include "jz4740-pcm.h"
28
29 struct jz4740_runtime_data {
30         unsigned int dma_period;
31         dma_addr_t dma_start;
32         dma_addr_t dma_pos;
33         dma_addr_t dma_end;
34
35         struct jz4740_dma_chan *dma;
36
37         dma_addr_t fifo_addr;
38 };
39
40 /* identify hardware playback capabilities */
41 static const struct snd_pcm_hardware jz4740_pcm_hardware = {
42         .info = SNDRV_PCM_INFO_MMAP |
43                 SNDRV_PCM_INFO_MMAP_VALID |
44                 SNDRV_PCM_INFO_INTERLEAVED |
45                 SNDRV_PCM_INFO_BLOCK_TRANSFER,
46         .formats = SNDRV_PCM_FMTBIT_S16_LE |
47                    SNDRV_PCM_FMTBIT_S8,
48         .rates                  = SNDRV_PCM_RATE_8000_48000,
49         .channels_min           = 1,
50         .channels_max           = 2,
51         .period_bytes_min       = 32,
52         .period_bytes_max       = 2 * PAGE_SIZE,
53         .periods_min            = 2,
54         .periods_max            = 128,
55         .buffer_bytes_max       = 128 * 2 * PAGE_SIZE,
56         .fifo_size                      = 32,
57 };
58
59 static void jz4740_pcm_start_transfer(struct jz4740_runtime_data *prtd, int stream)
60 {
61         unsigned int count;
62
63         if (prtd->dma_pos + prtd->dma_period > prtd->dma_end)
64                 count = prtd->dma_end - prtd->dma_pos;
65         else
66                 count = prtd->dma_period;
67
68         jz4740_dma_disable(prtd->dma);
69
70         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
71                 jz4740_dma_set_src_addr(prtd->dma, prtd->dma_pos);
72                 jz4740_dma_set_dst_addr(prtd->dma, prtd->fifo_addr);
73         } else {
74                 jz4740_dma_set_src_addr(prtd->dma, prtd->fifo_addr);
75                 jz4740_dma_set_dst_addr(prtd->dma, prtd->dma_pos);
76         }
77
78         jz4740_dma_set_transfer_count(prtd->dma, count);
79
80         jz4740_dma_enable(prtd->dma);
81
82         prtd->dma_pos += prtd->dma_period;
83         if (prtd->dma_pos >= prtd->dma_end)
84                 prtd->dma_pos = prtd->dma_start;
85 }
86
87 static void jz4740_pcm_dma_transfer_done(struct jz4740_dma_chan *dma, int err,
88                                          void *dev_id)
89 {
90         struct snd_pcm_substream *substream = dev_id;
91         struct snd_pcm_runtime *runtime = substream->runtime;
92         struct jz4740_runtime_data *prtd = runtime->private_data;
93
94         snd_pcm_period_elapsed(substream);
95
96         jz4740_pcm_start_transfer(prtd, substream->stream);
97 }
98
99 static int jz4740_pcm_hw_params(struct snd_pcm_substream *substream,
100         struct snd_pcm_hw_params *params)
101 {
102         struct snd_pcm_runtime *runtime = substream->runtime;
103         struct jz4740_runtime_data *prtd = runtime->private_data;
104         struct snd_soc_pcm_runtime *rtd = substream->private_data;
105         struct jz4740_pcm_config *config;
106
107         config = rtd->dai->cpu_dai->dma_data;
108         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
109                 prtd->dma = jz4740_dma_request(substream, "PCM Playback");
110         } else {
111                 prtd->dma = jz4740_dma_request(substream, "PCM Capture");
112         }
113
114         if (!prtd->dma)
115                 return -EBUSY;
116
117         jz4740_dma_configure(prtd->dma, config->dma_config);
118         prtd->fifo_addr = config->fifo_addr;
119
120         jz4740_dma_set_complete_cb(prtd->dma, jz4740_pcm_dma_transfer_done);
121
122         snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
123         runtime->dma_bytes = params_buffer_bytes(params);
124
125         prtd->dma_period = params_period_bytes(params);
126         prtd->dma_start = runtime->dma_addr;
127         prtd->dma_pos = prtd->dma_start;
128         prtd->dma_end = prtd->dma_start + runtime->dma_bytes;
129
130         return 0;
131 }
132
133 static int jz4740_pcm_hw_free(struct snd_pcm_substream *substream)
134 {
135         struct jz4740_runtime_data *prtd = substream->runtime->private_data;
136
137         snd_pcm_set_runtime_buffer(substream, NULL);
138         if (prtd->dma)
139                 jz4740_dma_free(prtd->dma);
140
141         return 0;
142 }
143
144 static int jz4740_pcm_prepare(struct snd_pcm_substream *substream)
145 {
146         struct jz4740_runtime_data *prtd = substream->runtime->private_data;
147         int ret = 0;
148
149         if (!prtd->dma)
150                 return 0;
151
152         prtd->dma_pos = prtd->dma_start;
153
154         return ret;
155 }
156
157 static int jz4740_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
158 {
159         struct snd_pcm_runtime *runtime = substream->runtime;
160         struct jz4740_runtime_data *prtd = runtime->private_data;
161
162         int ret = 0;
163
164         switch (cmd) {
165         case SNDRV_PCM_TRIGGER_START:
166         case SNDRV_PCM_TRIGGER_RESUME:
167         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
168                 jz4740_pcm_start_transfer(prtd, substream->stream);
169                 break;
170         case SNDRV_PCM_TRIGGER_STOP:
171         case SNDRV_PCM_TRIGGER_SUSPEND:
172         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
173                 jz4740_dma_disable(prtd->dma);
174                 break;
175         default:
176                 ret = -EINVAL;
177         }
178
179         return ret;
180 }
181
182 static snd_pcm_uframes_t jz4740_pcm_pointer(struct snd_pcm_substream *substream)
183 {
184         struct snd_pcm_runtime *runtime = substream->runtime;
185         struct jz4740_runtime_data *prtd = runtime->private_data;
186         unsigned long count, pos;
187         snd_pcm_uframes_t offset;
188         struct jz4740_dma_chan *dma = prtd->dma;
189
190         count = jz4740_dma_get_residue(dma);
191         if (prtd->dma_pos == prtd->dma_start)
192                 pos = prtd->dma_end - prtd->dma_start - count;
193         else
194                 pos = prtd->dma_pos - prtd->dma_start - count;
195
196         offset = bytes_to_frames(runtime, pos);
197         if (offset >= runtime->buffer_size)
198                 offset = 0;
199
200         return offset;
201 }
202
203 static int jz4740_pcm_open(struct snd_pcm_substream *substream)
204 {
205         struct snd_pcm_runtime *runtime = substream->runtime;
206         struct jz4740_runtime_data *prtd;
207
208         snd_soc_set_runtime_hwparams(substream, &jz4740_pcm_hardware);
209         prtd = kzalloc(sizeof(struct jz4740_runtime_data), GFP_KERNEL);
210
211         if (prtd == NULL)
212                 return -ENOMEM;
213
214         runtime->private_data = prtd;
215         return 0;
216 }
217
218 static int jz4740_pcm_close(struct snd_pcm_substream *substream)
219 {
220         struct snd_pcm_runtime *runtime = substream->runtime;
221         struct jz4740_runtime_data *prtd = runtime->private_data;
222
223         kfree(prtd);
224
225         return 0;
226 }
227
228 static int jz4740_pcm_mmap(struct snd_pcm_substream *substream,
229                            struct vm_area_struct *vma)
230 {
231         return remap_pfn_range(vma, vma->vm_start,
232                        substream->dma_buffer.addr >> PAGE_SHIFT,
233                        vma->vm_end - vma->vm_start, vma->vm_page_prot);
234 }
235
236 static struct snd_pcm_ops jz4740_pcm_ops = {
237         .open           = jz4740_pcm_open,
238         .close          = jz4740_pcm_close,
239         .ioctl          = snd_pcm_lib_ioctl,
240         .hw_params      = jz4740_pcm_hw_params,
241         .hw_free        = jz4740_pcm_hw_free,
242         .prepare        = jz4740_pcm_prepare,
243         .trigger        = jz4740_pcm_trigger,
244         .pointer        = jz4740_pcm_pointer,
245         .mmap           = jz4740_pcm_mmap,
246 };
247
248 static int jz4740_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
249 {
250         struct snd_pcm_substream *substream = pcm->streams[stream].substream;
251         struct snd_dma_buffer *buf = &substream->dma_buffer;
252         size_t size = jz4740_pcm_hardware.buffer_bytes_max;
253
254         buf->dev.type = SNDRV_DMA_TYPE_DEV;
255         buf->dev.dev = pcm->card->dev;
256         buf->private_data = NULL;
257
258         buf->area = dma_alloc_noncoherent(pcm->card->dev, size,
259                                           &buf->addr, GFP_KERNEL);
260         if (!buf->area)
261                 return -ENOMEM;
262
263         buf->bytes = size;
264
265         return 0;
266 }
267
268 static void jz4740_pcm_free(struct snd_pcm *pcm)
269 {
270         struct snd_pcm_substream *substream;
271         struct snd_dma_buffer *buf;
272         int stream;
273
274         for (stream = 0; stream < 2; stream++) {
275                 substream = pcm->streams[stream].substream;
276                 if (!substream)
277                         continue;
278
279                 buf = &substream->dma_buffer;
280                 if (!buf->area)
281                         continue;
282
283                 dma_free_noncoherent(pcm->card->dev, buf->bytes,
284                   buf->area, buf->addr);
285                 buf->area = NULL;
286         }
287 }
288
289 static u64 jz4740_pcm_dmamask = DMA_BIT_MASK(32);
290
291 int jz4740_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
292         struct snd_pcm *pcm)
293 {
294         int ret = 0;
295
296         if (!card->dev->dma_mask)
297                 card->dev->dma_mask = &jz4740_pcm_dmamask;
298
299         if (!card->dev->coherent_dma_mask)
300                 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
301
302         if (dai->playback.channels_min) {
303                 ret = jz4740_pcm_preallocate_dma_buffer(pcm,
304                         SNDRV_PCM_STREAM_PLAYBACK);
305                 if (ret)
306                         goto err;
307         }
308
309         if (dai->capture.channels_min) {
310                 ret = jz4740_pcm_preallocate_dma_buffer(pcm,
311                         SNDRV_PCM_STREAM_CAPTURE);
312                 if (ret)
313                         goto err;
314         }
315
316 err:
317         return ret;
318 }
319
320 struct snd_soc_platform jz4740_soc_platform = {
321         .name           = "jz4740-pcm",
322         .pcm_ops        = &jz4740_pcm_ops,
323         .pcm_new        = jz4740_pcm_new,
324         .pcm_free       = jz4740_pcm_free,
325 };
326 EXPORT_SYMBOL_GPL(jz4740_soc_platform);
327
328 static int __init jz4740_soc_platform_init(void)
329 {
330     return snd_soc_register_platform(&jz4740_soc_platform);
331 }
332 module_init(jz4740_soc_platform_init);
333
334 static void __exit jz4740_soc_platform_exit(void)
335 {
336     snd_soc_unregister_platform(&jz4740_soc_platform);
337 }
338 module_exit(jz4740_soc_platform_exit);
339
340 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
341 MODULE_DESCRIPTION("Ingenic SoC JZ4740 PCM driver");
342 MODULE_LICENSE("GPL");