pxa2xx-pcm.c 9.3 KB

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