pxa2xx-pcm.c 9.4 KB

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