pxa2xx-pcm-lib.c 7.2 KB

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