pxa2xx-pcm.c 9.5 KB

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