pxa2xx-pcm.c 9.4 KB

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