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