pxa2xx-pcm-lib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <asm/dma.h>
  13. #include <mach/pxa-regs.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)
  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. DCSR(prtd->dma_ch) &= ~DCSR_RUN;
  118. DCSR(prtd->dma_ch) = 0;
  119. DCMD(prtd->dma_ch) = 0;
  120. *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(__pxa2xx_pcm_prepare);
  124. void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
  125. {
  126. struct snd_pcm_substream *substream = dev_id;
  127. struct pxa2xx_runtime_data *rtd = substream->runtime->private_data;
  128. int dcsr;
  129. dcsr = DCSR(dma_ch);
  130. DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
  131. if (dcsr & DCSR_ENDINTR) {
  132. snd_pcm_period_elapsed(substream);
  133. } else {
  134. printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
  135. rtd->params->name, dma_ch, dcsr);
  136. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  137. }
  138. }
  139. EXPORT_SYMBOL(pxa2xx_pcm_dma_irq);
  140. int __pxa2xx_pcm_open(struct snd_pcm_substream *substream)
  141. {
  142. struct snd_pcm_runtime *runtime = substream->runtime;
  143. struct pxa2xx_runtime_data *rtd;
  144. int ret;
  145. runtime->hw = pxa2xx_pcm_hardware;
  146. /*
  147. * For mysterious reasons (and despite what the manual says)
  148. * playback samples are lost if the DMA count is not a multiple
  149. * of the DMA burst size. Let's add a rule to enforce that.
  150. */
  151. ret = snd_pcm_hw_constraint_step(runtime, 0,
  152. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  153. if (ret)
  154. goto out;
  155. ret = snd_pcm_hw_constraint_step(runtime, 0,
  156. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  157. if (ret)
  158. goto out;
  159. ret = snd_pcm_hw_constraint_integer(runtime,
  160. SNDRV_PCM_HW_PARAM_PERIODS);
  161. if (ret < 0)
  162. goto out;
  163. ret = -ENOMEM;
  164. rtd = kmalloc(sizeof(*rtd), GFP_KERNEL);
  165. if (!rtd)
  166. goto out;
  167. rtd->dma_desc_array =
  168. dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  169. &rtd->dma_desc_array_phys, GFP_KERNEL);
  170. if (!rtd->dma_desc_array)
  171. goto err1;
  172. runtime->private_data = rtd;
  173. return 0;
  174. err1:
  175. kfree(rtd);
  176. out:
  177. return ret;
  178. }
  179. EXPORT_SYMBOL(__pxa2xx_pcm_open);
  180. int __pxa2xx_pcm_close(struct snd_pcm_substream *substream)
  181. {
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. struct pxa2xx_runtime_data *rtd = runtime->private_data;
  184. dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  185. rtd->dma_desc_array, rtd->dma_desc_array_phys);
  186. kfree(rtd);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL(__pxa2xx_pcm_close);
  190. int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
  191. struct vm_area_struct *vma)
  192. {
  193. struct snd_pcm_runtime *runtime = substream->runtime;
  194. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  195. runtime->dma_area,
  196. runtime->dma_addr,
  197. runtime->dma_bytes);
  198. }
  199. EXPORT_SYMBOL(pxa2xx_pcm_mmap);
  200. int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  201. {
  202. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  203. struct snd_dma_buffer *buf = &substream->dma_buffer;
  204. size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
  205. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  206. buf->dev.dev = pcm->card->dev;
  207. buf->private_data = NULL;
  208. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  209. &buf->addr, GFP_KERNEL);
  210. if (!buf->area)
  211. return -ENOMEM;
  212. buf->bytes = size;
  213. return 0;
  214. }
  215. EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
  216. void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  217. {
  218. struct snd_pcm_substream *substream;
  219. struct snd_dma_buffer *buf;
  220. int stream;
  221. for (stream = 0; stream < 2; stream++) {
  222. substream = pcm->streams[stream].substream;
  223. if (!substream)
  224. continue;
  225. buf = &substream->dma_buffer;
  226. if (!buf->area)
  227. continue;
  228. dma_free_writecombine(pcm->card->dev, buf->bytes,
  229. buf->area, buf->addr);
  230. buf->area = NULL;
  231. }
  232. }
  233. EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
  234. MODULE_AUTHOR("Nicolas Pitre");
  235. MODULE_DESCRIPTION("Intel PXA2xx sound library");
  236. MODULE_LICENSE("GPL");