pxa2xx-pcm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * linux/sound/arm/pxa2xx-pcm.c -- ALSA PCM interface for the Intel PXA2xx chip
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: Nov 30, 2004
  6. * Copyright: (C) 2004 MontaVista Software, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/dma-mapping.h>
  17. #include <sound/driver.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <asm/dma.h>
  23. #include <asm/hardware.h>
  24. #include <asm/arch/pxa-regs.h>
  25. #include <asm/arch/audio.h>
  26. #include "pxa2xx-pcm.h"
  27. static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
  28. .info = SNDRV_PCM_INFO_MMAP |
  29. SNDRV_PCM_INFO_MMAP_VALID |
  30. SNDRV_PCM_INFO_INTERLEAVED |
  31. SNDRV_PCM_INFO_PAUSE |
  32. SNDRV_PCM_INFO_RESUME,
  33. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  34. SNDRV_PCM_FMTBIT_S24_LE |
  35. SNDRV_PCM_FMTBIT_S32_LE,
  36. .period_bytes_min = 32,
  37. .period_bytes_max = 8192 - 32,
  38. .periods_min = 1,
  39. .periods_max = PAGE_SIZE/sizeof(pxa_dma_desc),
  40. .buffer_bytes_max = 128 * 1024,
  41. .fifo_size = 32,
  42. };
  43. struct pxa2xx_runtime_data {
  44. int dma_ch;
  45. struct pxa2xx_pcm_dma_params *params;
  46. pxa_dma_desc *dma_desc_array;
  47. dma_addr_t dma_desc_array_phys;
  48. };
  49. static void pxa2xx_pcm_dma_irq(int dma_ch, void *dev_id)
  50. {
  51. struct snd_pcm_substream *substream = dev_id;
  52. struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
  53. int dcsr;
  54. dcsr = DCSR(dma_ch);
  55. DCSR(dma_ch) = dcsr & ~DCSR_STOPIRQEN;
  56. if (dcsr & DCSR_ENDINTR) {
  57. snd_pcm_period_elapsed(substream);
  58. } else {
  59. printk( KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
  60. prtd->params->name, dma_ch, dcsr );
  61. }
  62. }
  63. static int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
  64. struct snd_pcm_hw_params *params)
  65. {
  66. struct snd_pcm_runtime *runtime = substream->runtime;
  67. struct pxa2xx_runtime_data *prtd = runtime->private_data;
  68. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  69. struct pxa2xx_pcm_dma_params *dma = rtd->cpu_dai->dma_data;
  70. size_t totsize = params_buffer_bytes(params);
  71. size_t period = params_period_bytes(params);
  72. pxa_dma_desc *dma_desc;
  73. dma_addr_t dma_buff_phys, next_desc_phys;
  74. int ret;
  75. /* this may get called several times by oss emulation
  76. * with different params */
  77. if (prtd->params == NULL) {
  78. prtd->params = dma;
  79. ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
  80. pxa2xx_pcm_dma_irq, substream);
  81. if (ret < 0)
  82. return ret;
  83. prtd->dma_ch = ret;
  84. } else if (prtd->params != dma) {
  85. pxa_free_dma(prtd->dma_ch);
  86. prtd->params = dma;
  87. ret = pxa_request_dma(prtd->params->name, DMA_PRIO_LOW,
  88. pxa2xx_pcm_dma_irq, substream);
  89. if (ret < 0)
  90. return ret;
  91. prtd->dma_ch = ret;
  92. }
  93. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  94. runtime->dma_bytes = totsize;
  95. dma_desc = prtd->dma_desc_array;
  96. next_desc_phys = prtd->dma_desc_array_phys;
  97. dma_buff_phys = runtime->dma_addr;
  98. do {
  99. next_desc_phys += sizeof(pxa_dma_desc);
  100. dma_desc->ddadr = next_desc_phys;
  101. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  102. dma_desc->dsadr = dma_buff_phys;
  103. dma_desc->dtadr = prtd->params->dev_addr;
  104. } else {
  105. dma_desc->dsadr = prtd->params->dev_addr;
  106. dma_desc->dtadr = dma_buff_phys;
  107. }
  108. if (period > totsize)
  109. period = totsize;
  110. dma_desc->dcmd = prtd->params->dcmd | period | DCMD_ENDIRQEN;
  111. dma_desc++;
  112. dma_buff_phys += period;
  113. } while (totsize -= period);
  114. dma_desc[-1].ddadr = prtd->dma_desc_array_phys;
  115. return 0;
  116. }
  117. static int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
  118. {
  119. struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
  120. if (prtd && prtd->params)
  121. *prtd->params->drcmr = 0;
  122. if (prtd->dma_ch) {
  123. snd_pcm_set_runtime_buffer(substream, NULL);
  124. pxa_free_dma(prtd->dma_ch);
  125. prtd->dma_ch = 0;
  126. }
  127. return 0;
  128. }
  129. static int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
  130. {
  131. struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
  132. DCSR(prtd->dma_ch) &= ~DCSR_RUN;
  133. DCSR(prtd->dma_ch) = 0;
  134. DCMD(prtd->dma_ch) = 0;
  135. *prtd->params->drcmr = prtd->dma_ch | DRCMR_MAPVLD;
  136. return 0;
  137. }
  138. static int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  139. {
  140. struct pxa2xx_runtime_data *prtd = substream->runtime->private_data;
  141. int ret = 0;
  142. switch (cmd) {
  143. case SNDRV_PCM_TRIGGER_START:
  144. DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
  145. DCSR(prtd->dma_ch) = DCSR_RUN;
  146. break;
  147. case SNDRV_PCM_TRIGGER_STOP:
  148. case SNDRV_PCM_TRIGGER_SUSPEND:
  149. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  150. DCSR(prtd->dma_ch) &= ~DCSR_RUN;
  151. break;
  152. case SNDRV_PCM_TRIGGER_RESUME:
  153. DCSR(prtd->dma_ch) |= DCSR_RUN;
  154. break;
  155. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  156. DDADR(prtd->dma_ch) = prtd->dma_desc_array_phys;
  157. DCSR(prtd->dma_ch) |= DCSR_RUN;
  158. break;
  159. default:
  160. ret = -EINVAL;
  161. }
  162. return ret;
  163. }
  164. static snd_pcm_uframes_t
  165. pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
  166. {
  167. struct snd_pcm_runtime *runtime = substream->runtime;
  168. struct pxa2xx_runtime_data *prtd = runtime->private_data;
  169. dma_addr_t ptr = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ?
  170. DSADR(prtd->dma_ch) : DTADR(prtd->dma_ch);
  171. snd_pcm_uframes_t x = bytes_to_frames(runtime, ptr - runtime->dma_addr);
  172. if (x == runtime->buffer_size)
  173. x = 0;
  174. return x;
  175. }
  176. static int pxa2xx_pcm_open(struct snd_pcm_substream *substream)
  177. {
  178. struct snd_pcm_runtime *runtime = substream->runtime;
  179. struct pxa2xx_runtime_data *prtd;
  180. int ret;
  181. snd_soc_set_runtime_hwparams(substream, &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. prtd = kzalloc(sizeof(struct pxa2xx_runtime_data), GFP_KERNEL);
  196. if (prtd == NULL) {
  197. ret = -ENOMEM;
  198. goto out;
  199. }
  200. prtd->dma_desc_array =
  201. dma_alloc_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  202. &prtd->dma_desc_array_phys, GFP_KERNEL);
  203. if (!prtd->dma_desc_array) {
  204. ret = -ENOMEM;
  205. goto err1;
  206. }
  207. runtime->private_data = prtd;
  208. return 0;
  209. err1:
  210. kfree(prtd);
  211. out:
  212. return ret;
  213. }
  214. static int pxa2xx_pcm_close(struct snd_pcm_substream *substream)
  215. {
  216. struct snd_pcm_runtime *runtime = substream->runtime;
  217. struct pxa2xx_runtime_data *prtd = runtime->private_data;
  218. dma_free_writecombine(substream->pcm->card->dev, PAGE_SIZE,
  219. prtd->dma_desc_array, prtd->dma_desc_array_phys);
  220. kfree(prtd);
  221. return 0;
  222. }
  223. static int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
  224. struct vm_area_struct *vma)
  225. {
  226. struct snd_pcm_runtime *runtime = substream->runtime;
  227. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  228. runtime->dma_area,
  229. runtime->dma_addr,
  230. runtime->dma_bytes);
  231. }
  232. struct snd_pcm_ops pxa2xx_pcm_ops = {
  233. .open = pxa2xx_pcm_open,
  234. .close = pxa2xx_pcm_close,
  235. .ioctl = snd_pcm_lib_ioctl,
  236. .hw_params = pxa2xx_pcm_hw_params,
  237. .hw_free = pxa2xx_pcm_hw_free,
  238. .prepare = pxa2xx_pcm_prepare,
  239. .trigger = pxa2xx_pcm_trigger,
  240. .pointer = pxa2xx_pcm_pointer,
  241. .mmap = pxa2xx_pcm_mmap,
  242. };
  243. static int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  244. {
  245. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  246. struct snd_dma_buffer *buf = &substream->dma_buffer;
  247. size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
  248. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  249. buf->dev.dev = pcm->card->dev;
  250. buf->private_data = NULL;
  251. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  252. &buf->addr, GFP_KERNEL);
  253. if (!buf->area)
  254. return -ENOMEM;
  255. buf->bytes = size;
  256. return 0;
  257. }
  258. static void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  259. {
  260. struct snd_pcm_substream *substream;
  261. struct snd_dma_buffer *buf;
  262. int stream;
  263. for (stream = 0; stream < 2; stream++) {
  264. substream = pcm->streams[stream].substream;
  265. if (!substream)
  266. continue;
  267. buf = &substream->dma_buffer;
  268. if (!buf->area)
  269. continue;
  270. dma_free_writecombine(pcm->card->dev, buf->bytes,
  271. buf->area, buf->addr);
  272. buf->area = NULL;
  273. }
  274. }
  275. static u64 pxa2xx_pcm_dmamask = DMA_32BIT_MASK;
  276. int pxa2xx_pcm_new(struct snd_card *card, struct snd_soc_codec_dai *dai,
  277. struct snd_pcm *pcm)
  278. {
  279. int ret = 0;
  280. if (!card->dev->dma_mask)
  281. card->dev->dma_mask = &pxa2xx_pcm_dmamask;
  282. if (!card->dev->coherent_dma_mask)
  283. card->dev->coherent_dma_mask = DMA_32BIT_MASK;
  284. if (dai->playback.channels_min) {
  285. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
  286. SNDRV_PCM_STREAM_PLAYBACK);
  287. if (ret)
  288. goto out;
  289. }
  290. if (dai->capture.channels_min) {
  291. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
  292. SNDRV_PCM_STREAM_CAPTURE);
  293. if (ret)
  294. goto out;
  295. }
  296. out:
  297. return ret;
  298. }
  299. struct snd_soc_platform pxa2xx_soc_platform = {
  300. .name = "pxa2xx-audio",
  301. .pcm_ops = &pxa2xx_pcm_ops,
  302. .pcm_new = pxa2xx_pcm_new,
  303. .pcm_free = pxa2xx_pcm_free_dma_buffers,
  304. };
  305. EXPORT_SYMBOL_GPL(pxa2xx_soc_platform);
  306. MODULE_AUTHOR("Nicolas Pitre");
  307. MODULE_DESCRIPTION("Intel PXA2xx PCM DMA module");
  308. MODULE_LICENSE("GPL");