pxa2xx-pcm-lib.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/dmaengine.h>
  10. #include <sound/core.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/pxa2xx-lib.h>
  14. #include <sound/dmaengine_pcm.h>
  15. #include <mach/dma.h>
  16. #include "pxa2xx-pcm.h"
  17. static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
  18. .info = SNDRV_PCM_INFO_MMAP |
  19. SNDRV_PCM_INFO_MMAP_VALID |
  20. SNDRV_PCM_INFO_INTERLEAVED |
  21. SNDRV_PCM_INFO_PAUSE |
  22. SNDRV_PCM_INFO_RESUME,
  23. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  24. SNDRV_PCM_FMTBIT_S24_LE |
  25. SNDRV_PCM_FMTBIT_S32_LE,
  26. .period_bytes_min = 32,
  27. .period_bytes_max = 8192 - 32,
  28. .periods_min = 1,
  29. .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
  30. .buffer_bytes_max = 128 * 1024,
  31. .fifo_size = 32,
  32. };
  33. int __pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
  34. struct snd_pcm_hw_params *params)
  35. {
  36. struct snd_pcm_runtime *runtime = substream->runtime;
  37. struct pxa2xx_runtime_data *rtd = runtime->private_data;
  38. size_t totsize = params_buffer_bytes(params);
  39. size_t period = params_period_bytes(params);
  40. pxa_dma_desc *dma_desc;
  41. dma_addr_t dma_buff_phys, next_desc_phys;
  42. u32 dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
  43. /* temporary transition hack */
  44. switch (rtd->params->addr_width) {
  45. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  46. dcmd |= DCMD_WIDTH1;
  47. break;
  48. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  49. dcmd |= DCMD_WIDTH2;
  50. break;
  51. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  52. dcmd |= DCMD_WIDTH4;
  53. break;
  54. default:
  55. /* can't happen */
  56. break;
  57. }
  58. switch (rtd->params->maxburst) {
  59. case 8:
  60. dcmd |= DCMD_BURST8;
  61. break;
  62. case 16:
  63. dcmd |= DCMD_BURST16;
  64. break;
  65. case 32:
  66. dcmd |= DCMD_BURST32;
  67. break;
  68. }
  69. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  70. runtime->dma_bytes = totsize;
  71. dma_desc = rtd->dma_desc_array;
  72. next_desc_phys = rtd->dma_desc_array_phys;
  73. dma_buff_phys = runtime->dma_addr;
  74. do {
  75. next_desc_phys += sizeof(pxa_dma_desc);
  76. dma_desc->ddadr = next_desc_phys;
  77. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  78. dma_desc->dsadr = dma_buff_phys;
  79. dma_desc->dtadr = rtd->params->addr;
  80. } else {
  81. dma_desc->dsadr = rtd->params->addr;
  82. dma_desc->dtadr = dma_buff_phys;
  83. }
  84. if (period > totsize)
  85. period = totsize;
  86. dma_desc->dcmd = dcmd | period | DCMD_ENDIRQEN;
  87. dma_desc++;
  88. dma_buff_phys += period;
  89. } while (totsize -= period);
  90. dma_desc[-1].ddadr = rtd->dma_desc_array_phys;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(__pxa2xx_pcm_hw_params);
  94. int __pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
  95. {
  96. struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
  97. if (rtd && rtd->params && rtd->params->filter_data) {
  98. unsigned long req = *(unsigned long *) rtd->params->filter_data;
  99. DRCMR(req) = 0;
  100. }
  101. snd_pcm_set_runtime_buffer(substream, NULL);
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(__pxa2xx_pcm_hw_free);
  105. int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  106. {
  107. struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
  108. int ret = 0;
  109. switch (cmd) {
  110. case SNDRV_PCM_TRIGGER_START:
  111. DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
  112. DCSR(prtd->dma_ch) = DCSR_RUN;
  113. break;
  114. case SNDRV_PCM_TRIGGER_STOP:
  115. case SNDRV_PCM_TRIGGER_SUSPEND:
  116. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  117. DCSR(prtd->dma_ch) &= ~DCSR_RUN;
  118. break;
  119. case SNDRV_PCM_TRIGGER_RESUME:
  120. DCSR(prtd->dma_ch) |= DCSR_RUN;
  121. break;
  122. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  123. DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
  124. DCSR(prtd->dma_ch) |= DCSR_RUN;
  125. break;
  126. default:
  127. ret = -EINVAL;
  128. }
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(pxa2xx_pcm_trigger);
  132. snd_pcm_uframes_t
  133. pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
  134. {
  135. struct snd_pcm_runtime *runtime = substream->runtime;
  136. struct pxa2xx_runtime_data *prtd = runtime->private_data;
  137. dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  138. DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch);
  139. snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
  140. if (x == runtime->buffer_size)
  141. x = 0;
  142. return x;
  143. }
  144. EXPORT_SYMBOL(pxa2xx_pcm_pointer);
  145. int __pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
  146. {
  147. struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
  148. unsigned long req;
  149. if (!prtd || !prtd->params)
  150. return 0;
  151. if (prtd->dma_ch == -1)
  152. return -EINVAL;
  153. DCSR(prtd->dma_ch) &= ~DCSR_RUN;
  154. DCSR(prtd->dma_ch) = 0;
  155. DCMD(prtd->dma_ch) = 0;
  156. req = *(unsigned long *) prtd->params->filter_data;
  157. DRCMR(req) = prtd->dma_ch | DRCMR_MAPVLD;
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
  161. void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
  162. {
  163. struct snd_pcm_substream *substream = dev_id;
  164. int dcsr;
  165. dcsr = DCSR(dma_ch);
  166. DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
  167. if (dcsr & DCSR_ENDINTR) {
  168. snd_pcm_period_elapsed(substream);
  169. } else {
  170. printk(KERN_ERR "DMA error on channel %d (DCSR=%#x)\n",
  171. dma_ch, dcsr);
  172. snd_pcm_stream_lock(substream);
  173. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  174. snd_pcm_stream_unlock(substream);
  175. }
  176. }
  177. EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
  178. int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
  179. {
  180. struct snd_pcm_runtime *runtime = substream->runtime;
  181. struct pxa2xx_runtime_data *rtd;
  182. int ret;
  183. runtime->hw = pxa2xx_pcm_hardware;
  184. /*
  185. * For mysterious reasons (and despite what the manual says)
  186. * playback samples are lost if the DMA count is not a multiple
  187. * of the DMA burst size. Let's add a rule to enforce that.
  188. */
  189. ret = snd_pcm_hw_constraint_step(runtime, 0,
  190. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  191. if (ret)
  192. goto out;
  193. ret = snd_pcm_hw_constraint_step(runtime, 0,
  194. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  195. if (ret)
  196. goto out;
  197. ret = snd_pcm_hw_constraint_integer(runtime,
  198. SNDRV_PCM_HW_PARAM_PERIODS);
  199. if (ret < 0)
  200. goto out;
  201. ret = -ENOMEM;
  202. rtd = kzalloc(sizeof(*rtd), GFP_KERNEL);
  203. if (!rtd)
  204. goto out;
  205. rtd->dma_desc_array =
  206. dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  207. &rtd->dma_desc_array_phys, GFP_KERNEL);
  208. if (!rtd->dma_desc_array)
  209. goto err1;
  210. rtd->dma_ch = -1;
  211. runtime->private_data = rtd;
  212. return 0;
  213. err1:
  214. kfree(rtd);
  215. out:
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(__pxa2xx_pcm_open);
  219. int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
  220. {
  221. struct snd_pcm_runtime *runtime = substream->runtime;
  222. struct pxa2xx_runtime_data *rtd = runtime->private_data;
  223. dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  224. rtd->dma_desc_array, rtd->dma_desc_array_phys);
  225. kfree(rtd);
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(__pxa2xx_pcm_close);
  229. int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
  230. struct vm_area_struct *vma)
  231. {
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  234. runtime->dma_area,
  235. runtime->dma_addr,
  236. runtime->dma_bytes);
  237. }
  238. EXPORT_SYMBOL(pxa2xx_pcm_mmap);
  239. int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  240. {
  241. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  242. struct snd_dma_buffer *buf = &substream->dma_buffer;
  243. size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
  244. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  245. buf->dev.dev = pcm->card->dev;
  246. buf->private_data = NULL;
  247. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  248. &buf->addr, GFP_KERNEL);
  249. if (!buf->area)
  250. return -ENOMEM;
  251. buf->bytes = size;
  252. return 0;
  253. }
  254. EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
  255. void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  256. {
  257. struct snd_pcm_substream *substream;
  258. struct snd_dma_buffer *buf;
  259. int stream;
  260. for (stream = 0; stream < 2; stream++) {
  261. substream = pcm->streams[stream].substream;
  262. if (!substream)
  263. continue;
  264. buf = &substream->dma_buffer;
  265. if (!buf->area)
  266. continue;
  267. dma_free_writecombine(pcm->card->dev, buf->bytes,
  268. buf->area, buf->addr);
  269. buf->area = NULL;
  270. }
  271. }
  272. EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
  273. MODULE_AUTHOR("Nicolas Pitre");
  274. MODULE_DESCRIPTION("Intel PXA2xx sound library");
  275. MODULE_LICENSE("GPL");