pxa2xx-pcm-lib.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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_stop_xrun(substream);
  173. }
  174. }
  175. EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
  176. int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
  177. {
  178. struct snd_pcm_runtime *runtime = substream->runtime;
  179. struct pxa2xx_runtime_data *rtd;
  180. int ret;
  181. runtime->hw = pxa2xx_pcm_hardware;
  182. /*
  183. * For mysterious reasons (and despite what the manual says)
  184. * playback samples are lost if the DMA count is not a multiple
  185. * of the DMA burst size. Let's add a rule to enforce that.
  186. */
  187. ret = snd_pcm_hw_constraint_step(runtime, 0,
  188. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  189. if (ret)
  190. goto out;
  191. ret = snd_pcm_hw_constraint_step(runtime, 0,
  192. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  193. if (ret)
  194. goto out;
  195. ret = snd_pcm_hw_constraint_integer(runtime,
  196. SNDRV_PCM_HW_PARAM_PERIODS);
  197. if (ret < 0)
  198. goto out;
  199. ret = -ENOMEM;
  200. rtd = kzalloc(sizeof(*rtd), GFP_KERNEL);
  201. if (!rtd)
  202. goto out;
  203. rtd->dma_desc_array =
  204. dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  205. &rtd->dma_desc_array_phys, GFP_KERNEL);
  206. if (!rtd->dma_desc_array)
  207. goto err1;
  208. rtd->dma_ch = -1;
  209. runtime->private_data = rtd;
  210. return 0;
  211. err1:
  212. kfree(rtd);
  213. out:
  214. return ret;
  215. }
  216. EXPORT_SYMBOL(__pxa2xx_pcm_open);
  217. int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
  218. {
  219. struct snd_pcm_runtime *runtime = substream->runtime;
  220. struct pxa2xx_runtime_data *rtd = runtime->private_data;
  221. dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  222. rtd->dma_desc_array, rtd->dma_desc_array_phys);
  223. kfree(rtd);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(__pxa2xx_pcm_close);
  227. int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
  228. struct vm_area_struct *vma)
  229. {
  230. struct snd_pcm_runtime *runtime = substream->runtime;
  231. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  232. runtime->dma_area,
  233. runtime->dma_addr,
  234. runtime->dma_bytes);
  235. }
  236. EXPORT_SYMBOL(pxa2xx_pcm_mmap);
  237. int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  238. {
  239. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  240. struct snd_dma_buffer *buf = &substream->dma_buffer;
  241. size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
  242. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  243. buf->dev.dev = pcm->card->dev;
  244. buf->private_data = NULL;
  245. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  246. &buf->addr, GFP_KERNEL);
  247. if (!buf->area)
  248. return -ENOMEM;
  249. buf->bytes = size;
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
  253. void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  254. {
  255. struct snd_pcm_substream *substream;
  256. struct snd_dma_buffer *buf;
  257. int stream;
  258. for (stream = 0; stream < 2; stream++) {
  259. substream = pcm->streams[stream].substream;
  260. if (!substream)
  261. continue;
  262. buf = &substream->dma_buffer;
  263. if (!buf->area)
  264. continue;
  265. dma_free_writecombine(pcm->card->dev, buf->bytes,
  266. buf->area, buf->addr);
  267. buf->area = NULL;
  268. }
  269. }
  270. EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
  271. MODULE_AUTHOR("Nicolas Pitre");
  272. MODULE_DESCRIPTION("Intel PXA2xx sound library");
  273. MODULE_LICENSE("GPL");