pxa2xx-pcm-lib.c 7.3 KB

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