xburst: Add 3.10 support
[openwrt.git] / target / linux / xburst / patches-3.10 / 016-ASoC-jz4740-Use-the-generic-dmaengine-PCM-driver.patch
1 From a20f54fdd500e5bccc9bd1ca4ac9f150addf2e64 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 27 Apr 2013 21:26:30 +0200
4 Subject: [PATCH 16/16] ASoC: jz4740: Use the generic dmaengine PCM driver
5
6 Since there is a dmaengine driver for the jz4740 DMA controller now we can use
7 the generic dmaengine PCM driver instead of a custom one.
8
9 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
10 Acked-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 ---
12  sound/soc/jz4740/Kconfig      |    1 +
13  sound/soc/jz4740/jz4740-i2s.c |   48 +++----
14  sound/soc/jz4740/jz4740-pcm.c |  310 ++---------------------------------------
15  sound/soc/jz4740/jz4740-pcm.h |   20 ---
16  4 files changed, 27 insertions(+), 352 deletions(-)
17  delete mode 100644 sound/soc/jz4740/jz4740-pcm.h
18
19 diff --git a/sound/soc/jz4740/Kconfig b/sound/soc/jz4740/Kconfig
20 index 5351cba..29f76af 100644
21 --- a/sound/soc/jz4740/Kconfig
22 +++ b/sound/soc/jz4740/Kconfig
23 @@ -1,6 +1,7 @@
24  config SND_JZ4740_SOC
25         tristate "SoC Audio for Ingenic JZ4740 SoC"
26         depends on MACH_JZ4740 && SND_SOC
27 +       select SND_SOC_GENERIC_DMAENGINE_PCM
28         help
29           Say Y or M if you want to add support for codecs attached to
30           the JZ4740 I2S interface. You will also need to select the audio
31 diff --git a/sound/soc/jz4740/jz4740-i2s.c b/sound/soc/jz4740/jz4740-i2s.c
32 index 9a12644..5d04134 100644
33 --- a/sound/soc/jz4740/jz4740-i2s.c
34 +++ b/sound/soc/jz4740/jz4740-i2s.c
35 @@ -29,9 +29,12 @@
36  #include <sound/pcm_params.h>
37  #include <sound/soc.h>
38  #include <sound/initval.h>
39 +#include <sound/dmaengine_pcm.h>
40 +
41 +#include <asm/mach-jz4740/dma.h>
42  
43  #include "jz4740-i2s.h"
44 -#include "jz4740-pcm.h"
45 +
46  
47  #define JZ_REG_AIC_CONF                0x00
48  #define JZ_REG_AIC_CTRL                0x04
49 @@ -89,8 +92,8 @@ struct jz4740_i2s {
50         struct clk *clk_aic;
51         struct clk *clk_i2s;
52  
53 -       struct jz4740_pcm_config pcm_config_playback;
54 -       struct jz4740_pcm_config pcm_config_capture;
55 +       struct snd_dmaengine_dai_dma_data playback_dma_data;
56 +       struct snd_dmaengine_dai_dma_data capture_dma_data;
57  };
58  
59  static inline uint32_t jz4740_i2s_read(const struct jz4740_i2s *i2s,
60 @@ -233,8 +236,6 @@ static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
61         struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
62  {
63         struct jz4740_i2s *i2s = snd_soc_dai_get_drvdata(dai);
64 -       enum jz4740_dma_width dma_width;
65 -       struct jz4740_pcm_config *pcm_config;
66         unsigned int sample_size;
67         uint32_t ctrl;
68  
69 @@ -243,11 +244,9 @@ static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
70         switch (params_format(params)) {
71         case SNDRV_PCM_FORMAT_S8:
72                 sample_size = 0;
73 -               dma_width = JZ4740_DMA_WIDTH_8BIT;
74                 break;
75         case SNDRV_PCM_FORMAT_S16:
76                 sample_size = 1;
77 -               dma_width = JZ4740_DMA_WIDTH_16BIT;
78                 break;
79         default:
80                 return -EINVAL;
81 @@ -260,22 +259,13 @@ static int jz4740_i2s_hw_params(struct snd_pcm_substream *substream,
82                         ctrl |= JZ_AIC_CTRL_MONO_TO_STEREO;
83                 else
84                         ctrl &= ~JZ_AIC_CTRL_MONO_TO_STEREO;
85 -
86 -               pcm_config = &i2s->pcm_config_playback;
87 -               pcm_config->dma_config.dst_width = dma_width;
88 -
89         } else {
90                 ctrl &= ~JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_MASK;
91                 ctrl |= sample_size << JZ_AIC_CTRL_INPUT_SAMPLE_SIZE_OFFSET;
92 -
93 -               pcm_config = &i2s->pcm_config_capture;
94 -               pcm_config->dma_config.src_width = dma_width;
95         }
96  
97         jz4740_i2s_write(i2s, JZ_REG_AIC_CTRL, ctrl);
98  
99 -       snd_soc_dai_set_dma_data(dai, substream, pcm_config);
100 -
101         return 0;
102  }
103  
104 @@ -342,25 +332,19 @@ static int jz4740_i2s_resume(struct snd_soc_dai *dai)
105  
106  static void jz4740_i2c_init_pcm_config(struct jz4740_i2s *i2s)
107  {
108 -       struct jz4740_dma_config *dma_config;
109 +       struct snd_dmaengine_dai_dma_data *dma_data;
110  
111         /* Playback */
112 -       dma_config = &i2s->pcm_config_playback.dma_config;
113 -       dma_config->src_width = JZ4740_DMA_WIDTH_32BIT;
114 -       dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
115 -       dma_config->request_type = JZ4740_DMA_TYPE_AIC_TRANSMIT;
116 -       dma_config->flags = JZ4740_DMA_SRC_AUTOINC;
117 -       dma_config->mode = JZ4740_DMA_MODE_SINGLE;
118 -       i2s->pcm_config_playback.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
119 +       dma_data = &i2s->playback_dma_data;
120 +       dma_data->maxburst = 16;
121 +       dma_data->slave_id = JZ4740_DMA_TYPE_AIC_TRANSMIT;
122 +       dma_data->addr = i2s->phys_base + JZ_REG_AIC_FIFO;
123  
124         /* Capture */
125 -       dma_config = &i2s->pcm_config_capture.dma_config;
126 -       dma_config->dst_width = JZ4740_DMA_WIDTH_32BIT;
127 -       dma_config->transfer_size = JZ4740_DMA_TRANSFER_SIZE_16BYTE;
128 -       dma_config->request_type = JZ4740_DMA_TYPE_AIC_RECEIVE;
129 -       dma_config->flags = JZ4740_DMA_DST_AUTOINC;
130 -       dma_config->mode = JZ4740_DMA_MODE_SINGLE;
131 -       i2s->pcm_config_capture.fifo_addr = i2s->phys_base + JZ_REG_AIC_FIFO;
132 +       dma_data = &i2s->capture_dma_data;
133 +       dma_data->maxburst = 16;
134 +       dma_data->slave_id = JZ4740_DMA_TYPE_AIC_RECEIVE;
135 +       dma_data->addr = i2s->phys_base + JZ_REG_AIC_FIFO;
136  }
137  
138  static int jz4740_i2s_dai_probe(struct snd_soc_dai *dai)
139 @@ -371,6 +355,8 @@ static int jz4740_i2s_dai_probe(struct snd_soc_dai *dai)
140         clk_enable(i2s->clk_aic);
141  
142         jz4740_i2c_init_pcm_config(i2s);
143 +       dai->playback_dma_data = &i2s->playback_dma_data;
144 +       dai->capture_dma_data = &i2s->capture_dma_data;
145  
146         conf = (7 << JZ_AIC_CONF_FIFO_RX_THRESHOLD_OFFSET) |
147                 (8 << JZ_AIC_CONF_FIFO_TX_THRESHOLD_OFFSET) |
148 diff --git a/sound/soc/jz4740/jz4740-pcm.c b/sound/soc/jz4740/jz4740-pcm.c
149 index 7100592..79fcade 100644
150 --- a/sound/soc/jz4740/jz4740-pcm.c
151 +++ b/sound/soc/jz4740/jz4740-pcm.c
152 @@ -19,38 +19,14 @@
153  #include <linux/platform_device.h>
154  #include <linux/slab.h>
155  
156 -#include <linux/dma-mapping.h>
157 +#include <sound/dmaengine_pcm.h>
158  
159 -#include <sound/core.h>
160 -#include <sound/pcm.h>
161 -#include <sound/pcm_params.h>
162 -#include <sound/soc.h>
163 -
164 -#include <asm/mach-jz4740/dma.h>
165 -#include "jz4740-pcm.h"
166 -
167 -struct jz4740_runtime_data {
168 -       unsigned long dma_period;
169 -       dma_addr_t dma_start;
170 -       dma_addr_t dma_pos;
171 -       dma_addr_t dma_end;
172 -
173 -       struct jz4740_dma_chan *dma;
174 -
175 -       dma_addr_t fifo_addr;
176 -};
177 -
178 -/* identify hardware playback capabilities */
179  static const struct snd_pcm_hardware jz4740_pcm_hardware = {
180         .info = SNDRV_PCM_INFO_MMAP |
181                 SNDRV_PCM_INFO_MMAP_VALID |
182                 SNDRV_PCM_INFO_INTERLEAVED |
183                 SNDRV_PCM_INFO_BLOCK_TRANSFER,
184         .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
185 -
186 -       .rates                  = SNDRV_PCM_RATE_8000_48000,
187 -       .channels_min           = 1,
188 -       .channels_max           = 2,
189         .period_bytes_min       = 16,
190         .period_bytes_max       = 2 * PAGE_SIZE,
191         .periods_min            = 2,
192 @@ -59,290 +35,22 @@ static const struct snd_pcm_hardware jz4740_pcm_hardware = {
193         .fifo_size              = 32,
194  };
195  
196 -static void jz4740_pcm_start_transfer(struct jz4740_runtime_data *prtd,
197 -       struct snd_pcm_substream *substream)
198 -{
199 -       unsigned long count;
200 -
201 -       if (prtd->dma_pos == prtd->dma_end)
202 -               prtd->dma_pos = prtd->dma_start;
203 -
204 -       if (prtd->dma_pos + prtd->dma_period > prtd->dma_end)
205 -               count = prtd->dma_end - prtd->dma_pos;
206 -       else
207 -               count = prtd->dma_period;
208 -
209 -       jz4740_dma_disable(prtd->dma);
210 -
211 -       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
212 -               jz4740_dma_set_src_addr(prtd->dma, prtd->dma_pos);
213 -               jz4740_dma_set_dst_addr(prtd->dma, prtd->fifo_addr);
214 -       } else {
215 -               jz4740_dma_set_src_addr(prtd->dma, prtd->fifo_addr);
216 -               jz4740_dma_set_dst_addr(prtd->dma, prtd->dma_pos);
217 -       }
218 -
219 -       jz4740_dma_set_transfer_count(prtd->dma, count);
220 -
221 -       prtd->dma_pos += count;
222 -
223 -       jz4740_dma_enable(prtd->dma);
224 -}
225 -
226 -static void jz4740_pcm_dma_transfer_done(struct jz4740_dma_chan *dma, int err,
227 -       void *dev_id)
228 -{
229 -       struct snd_pcm_substream *substream = dev_id;
230 -       struct snd_pcm_runtime *runtime = substream->runtime;
231 -       struct jz4740_runtime_data *prtd = runtime->private_data;
232 -
233 -       snd_pcm_period_elapsed(substream);
234 -
235 -       jz4740_pcm_start_transfer(prtd, substream);
236 -}
237 -
238 -static int jz4740_pcm_hw_params(struct snd_pcm_substream *substream,
239 -       struct snd_pcm_hw_params *params)
240 -{
241 -       struct snd_pcm_runtime *runtime = substream->runtime;
242 -       struct jz4740_runtime_data *prtd = runtime->private_data;
243 -       struct snd_soc_pcm_runtime *rtd = substream->private_data;
244 -       struct jz4740_pcm_config *config;
245 -
246 -       config = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
247 -
248 -       if (!config)
249 -               return 0;
250 -
251 -       if (!prtd->dma) {
252 -               if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
253 -                       prtd->dma = jz4740_dma_request(substream, "PCM Capture");
254 -               else
255 -                       prtd->dma = jz4740_dma_request(substream, "PCM Playback");
256 -       }
257 -
258 -       if (!prtd->dma)
259 -               return -EBUSY;
260 -
261 -       jz4740_dma_configure(prtd->dma, &config->dma_config);
262 -       prtd->fifo_addr = config->fifo_addr;
263 -
264 -       jz4740_dma_set_complete_cb(prtd->dma, jz4740_pcm_dma_transfer_done);
265 -
266 -       snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
267 -       runtime->dma_bytes = params_buffer_bytes(params);
268 -
269 -       prtd->dma_period = params_period_bytes(params);
270 -       prtd->dma_start = runtime->dma_addr;
271 -       prtd->dma_pos = prtd->dma_start;
272 -       prtd->dma_end = prtd->dma_start + runtime->dma_bytes;
273 -
274 -       return 0;
275 -}
276 -
277 -static int jz4740_pcm_hw_free(struct snd_pcm_substream *substream)
278 -{
279 -       struct jz4740_runtime_data *prtd = substream->runtime->private_data;
280 -
281 -       snd_pcm_set_runtime_buffer(substream, NULL);
282 -       if (prtd->dma) {
283 -               jz4740_dma_free(prtd->dma);
284 -               prtd->dma = NULL;
285 -       }
286 -
287 -       return 0;
288 -}
289 -
290 -static int jz4740_pcm_prepare(struct snd_pcm_substream *substream)
291 -{
292 -       struct jz4740_runtime_data *prtd = substream->runtime->private_data;
293 -
294 -       if (!prtd->dma)
295 -               return -EBUSY;
296 -
297 -       prtd->dma_pos = prtd->dma_start;
298 -
299 -       return 0;
300 -}
301 -
302 -static int jz4740_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
303 -{
304 -       struct snd_pcm_runtime *runtime = substream->runtime;
305 -       struct jz4740_runtime_data *prtd = runtime->private_data;
306 -
307 -       switch (cmd) {
308 -       case SNDRV_PCM_TRIGGER_START:
309 -       case SNDRV_PCM_TRIGGER_RESUME:
310 -       case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
311 -               jz4740_pcm_start_transfer(prtd, substream);
312 -               break;
313 -       case SNDRV_PCM_TRIGGER_STOP:
314 -       case SNDRV_PCM_TRIGGER_SUSPEND:
315 -       case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
316 -               jz4740_dma_disable(prtd->dma);
317 -               break;
318 -       default:
319 -               break;
320 -       }
321 -
322 -       return 0;
323 -}
324 -
325 -static snd_pcm_uframes_t jz4740_pcm_pointer(struct snd_pcm_substream *substream)
326 -{
327 -       struct snd_pcm_runtime *runtime = substream->runtime;
328 -       struct jz4740_runtime_data *prtd = runtime->private_data;
329 -       unsigned long byte_offset;
330 -       snd_pcm_uframes_t offset;
331 -       struct jz4740_dma_chan *dma = prtd->dma;
332 -
333 -       /* prtd->dma_pos points to the end of the current transfer. So by
334 -        * subtracting prdt->dma_start we get the offset to the end of the
335 -        * current period in bytes. By subtracting the residue of the transfer
336 -        * we get the current offset in bytes. */
337 -       byte_offset = prtd->dma_pos - prtd->dma_start;
338 -       byte_offset -= jz4740_dma_get_residue(dma);
339 -
340 -       offset = bytes_to_frames(runtime, byte_offset);
341 -       if (offset >= runtime->buffer_size)
342 -               offset = 0;
343 -
344 -       return offset;
345 -}
346 -
347 -static int jz4740_pcm_open(struct snd_pcm_substream *substream)
348 -{
349 -       struct snd_pcm_runtime *runtime = substream->runtime;
350 -       struct jz4740_runtime_data *prtd;
351 -
352 -       prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
353 -       if (prtd == NULL)
354 -               return -ENOMEM;
355 -
356 -       snd_soc_set_runtime_hwparams(substream, &jz4740_pcm_hardware);
357 -
358 -       runtime->private_data = prtd;
359 -
360 -       return 0;
361 -}
362 -
363 -static int jz4740_pcm_close(struct snd_pcm_substream *substream)
364 -{
365 -       struct snd_pcm_runtime *runtime = substream->runtime;
366 -       struct jz4740_runtime_data *prtd = runtime->private_data;
367 -
368 -       kfree(prtd);
369 -
370 -       return 0;
371 -}
372 -
373 -static int jz4740_pcm_mmap(struct snd_pcm_substream *substream,
374 -       struct vm_area_struct *vma)
375 -{
376 -       return remap_pfn_range(vma, vma->vm_start,
377 -                       substream->dma_buffer.addr >> PAGE_SHIFT,
378 -                       vma->vm_end - vma->vm_start, vma->vm_page_prot);
379 -}
380 -
381 -static struct snd_pcm_ops jz4740_pcm_ops = {
382 -       .open           = jz4740_pcm_open,
383 -       .close          = jz4740_pcm_close,
384 -       .ioctl          = snd_pcm_lib_ioctl,
385 -       .hw_params      = jz4740_pcm_hw_params,
386 -       .hw_free        = jz4740_pcm_hw_free,
387 -       .prepare        = jz4740_pcm_prepare,
388 -       .trigger        = jz4740_pcm_trigger,
389 -       .pointer        = jz4740_pcm_pointer,
390 -       .mmap           = jz4740_pcm_mmap,
391 -};
392 -
393 -static int jz4740_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
394 -{
395 -       struct snd_pcm_substream *substream = pcm->streams[stream].substream;
396 -       struct snd_dma_buffer *buf = &substream->dma_buffer;
397 -       size_t size = jz4740_pcm_hardware.buffer_bytes_max;
398 -
399 -       buf->dev.type = SNDRV_DMA_TYPE_DEV;
400 -       buf->dev.dev = pcm->card->dev;
401 -       buf->private_data = NULL;
402 -
403 -       buf->area = dma_alloc_noncoherent(pcm->card->dev, size,
404 -                                         &buf->addr, GFP_KERNEL);
405 -       if (!buf->area)
406 -               return -ENOMEM;
407 -
408 -       buf->bytes = size;
409 -
410 -       return 0;
411 -}
412 -
413 -static void jz4740_pcm_free(struct snd_pcm *pcm)
414 -{
415 -       struct snd_pcm_substream *substream;
416 -       struct snd_dma_buffer *buf;
417 -       int stream;
418 -
419 -       for (stream = 0; stream < SNDRV_PCM_STREAM_LAST; ++stream) {
420 -               substream = pcm->streams[stream].substream;
421 -               if (!substream)
422 -                       continue;
423 -
424 -               buf = &substream->dma_buffer;
425 -               if (!buf->area)
426 -                       continue;
427 -
428 -               dma_free_noncoherent(pcm->card->dev, buf->bytes, buf->area,
429 -                               buf->addr);
430 -               buf->area = NULL;
431 -       }
432 -}
433 -
434 -static u64 jz4740_pcm_dmamask = DMA_BIT_MASK(32);
435 -
436 -static int jz4740_pcm_new(struct snd_soc_pcm_runtime *rtd)
437 -{
438 -       struct snd_card *card = rtd->card->snd_card;
439 -       struct snd_pcm *pcm = rtd->pcm;
440 -       int ret = 0;
441 -
442 -       if (!card->dev->dma_mask)
443 -               card->dev->dma_mask = &jz4740_pcm_dmamask;
444 -
445 -       if (!card->dev->coherent_dma_mask)
446 -               card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
447 -
448 -       if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
449 -               ret = jz4740_pcm_preallocate_dma_buffer(pcm,
450 -                       SNDRV_PCM_STREAM_PLAYBACK);
451 -               if (ret)
452 -                       goto err;
453 -       }
454 -
455 -       if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
456 -               ret = jz4740_pcm_preallocate_dma_buffer(pcm,
457 -                       SNDRV_PCM_STREAM_CAPTURE);
458 -               if (ret)
459 -                       goto err;
460 -       }
461 -
462 -err:
463 -       return ret;
464 -}
465 -
466 -static struct snd_soc_platform_driver jz4740_soc_platform = {
467 -               .ops            = &jz4740_pcm_ops,
468 -               .pcm_new        = jz4740_pcm_new,
469 -               .pcm_free       = jz4740_pcm_free,
470 +static const struct snd_dmaengine_pcm_config jz4740_dmaengine_pcm_config = {
471 +       .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
472 +       .pcm_hardware = &jz4740_pcm_hardware,
473 +       .prealloc_buffer_size = 256 * PAGE_SIZE,
474  };
475  
476  static int jz4740_pcm_probe(struct platform_device *pdev)
477  {
478 -       return snd_soc_register_platform(&pdev->dev, &jz4740_soc_platform);
479 +       return snd_dmaengine_pcm_register(&pdev->dev,
480 +               &jz4740_dmaengine_pcm_config,
481 +               SND_DMAENGINE_PCM_FLAG_COMPAT);
482  }
483  
484  static int jz4740_pcm_remove(struct platform_device *pdev)
485  {
486 -       snd_soc_unregister_platform(&pdev->dev);
487 +       snd_dmaengine_pcm_unregister(&pdev->dev);
488         return 0;
489  }
490  
491 diff --git a/sound/soc/jz4740/jz4740-pcm.h b/sound/soc/jz4740/jz4740-pcm.h
492 deleted file mode 100644
493 index 1220cbb..0000000
494 --- a/sound/soc/jz4740/jz4740-pcm.h
495 +++ /dev/null
496 @@ -1,20 +0,0 @@
497 -/*
498 - *
499 - * This program is free software; you can redistribute it and/or modify
500 - * it under the terms of the GNU General Public License version 2 as
501 - * published by the Free Software Foundation.
502 - */
503 -
504 -#ifndef _JZ4740_PCM_H
505 -#define _JZ4740_PCM_H
506 -
507 -#include <linux/dma-mapping.h>
508 -#include <asm/mach-jz4740/dma.h>
509 -
510 -
511 -struct jz4740_pcm_config {
512 -       struct jz4740_dma_config dma_config;
513 -       phys_addr_t fifo_addr;
514 -};
515 -
516 -#endif
517 -- 
518 1.7.10.4
519