dma.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Au1000/Au1500/Au1100 Audio DMA support.
  3. *
  4. * (c) 2011 Manuel Lauss <manuel.lauss@googlemail.com>
  5. *
  6. * copied almost verbatim from the old ALSA driver, written by
  7. * Charles Eidsness <charles@cooper-street.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/dma-mapping.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <asm/mach-au1x00/au1000.h>
  19. #include <asm/mach-au1x00/au1000_dma.h>
  20. #include "psc.h"
  21. #define ALCHEMY_PCM_FMTS \
  22. (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
  23. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  24. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE | \
  25. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | \
  26. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE | \
  27. 0)
  28. struct pcm_period {
  29. u32 start;
  30. u32 relative_end; /* relative to start of buffer */
  31. struct pcm_period *next;
  32. };
  33. struct audio_stream {
  34. struct snd_pcm_substream *substream;
  35. int dma;
  36. struct pcm_period *buffer;
  37. unsigned int period_size;
  38. unsigned int periods;
  39. };
  40. struct alchemy_pcm_ctx {
  41. struct audio_stream stream[2]; /* playback & capture */
  42. };
  43. static void au1000_release_dma_link(struct audio_stream *stream)
  44. {
  45. struct pcm_period *pointer;
  46. struct pcm_period *pointer_next;
  47. stream->period_size = 0;
  48. stream->periods = 0;
  49. pointer = stream->buffer;
  50. if (!pointer)
  51. return;
  52. do {
  53. pointer_next = pointer->next;
  54. kfree(pointer);
  55. pointer = pointer_next;
  56. } while (pointer != stream->buffer);
  57. stream->buffer = NULL;
  58. }
  59. static int au1000_setup_dma_link(struct audio_stream *stream,
  60. unsigned int period_bytes,
  61. unsigned int periods)
  62. {
  63. struct snd_pcm_substream *substream = stream->substream;
  64. struct snd_pcm_runtime *runtime = substream->runtime;
  65. struct pcm_period *pointer;
  66. unsigned long dma_start;
  67. int i;
  68. dma_start = virt_to_phys(runtime->dma_area);
  69. if (stream->period_size == period_bytes &&
  70. stream->periods == periods)
  71. return 0; /* not changed */
  72. au1000_release_dma_link(stream);
  73. stream->period_size = period_bytes;
  74. stream->periods = periods;
  75. stream->buffer = kmalloc(sizeof(struct pcm_period), GFP_KERNEL);
  76. if (!stream->buffer)
  77. return -ENOMEM;
  78. pointer = stream->buffer;
  79. for (i = 0; i < periods; i++) {
  80. pointer->start = (u32)(dma_start + (i * period_bytes));
  81. pointer->relative_end = (u32) (((i+1) * period_bytes) - 0x1);
  82. if (i < periods - 1) {
  83. pointer->next = kmalloc(sizeof(struct pcm_period),
  84. GFP_KERNEL);
  85. if (!pointer->next) {
  86. au1000_release_dma_link(stream);
  87. return -ENOMEM;
  88. }
  89. pointer = pointer->next;
  90. }
  91. }
  92. pointer->next = stream->buffer;
  93. return 0;
  94. }
  95. static void au1000_dma_stop(struct audio_stream *stream)
  96. {
  97. if (stream->buffer)
  98. disable_dma(stream->dma);
  99. }
  100. static void au1000_dma_start(struct audio_stream *stream)
  101. {
  102. if (!stream->buffer)
  103. return;
  104. init_dma(stream->dma);
  105. if (get_dma_active_buffer(stream->dma) == 0) {
  106. clear_dma_done0(stream->dma);
  107. set_dma_addr0(stream->dma, stream->buffer->start);
  108. set_dma_count0(stream->dma, stream->period_size >> 1);
  109. set_dma_addr1(stream->dma, stream->buffer->next->start);
  110. set_dma_count1(stream->dma, stream->period_size >> 1);
  111. } else {
  112. clear_dma_done1(stream->dma);
  113. set_dma_addr1(stream->dma, stream->buffer->start);
  114. set_dma_count1(stream->dma, stream->period_size >> 1);
  115. set_dma_addr0(stream->dma, stream->buffer->next->start);
  116. set_dma_count0(stream->dma, stream->period_size >> 1);
  117. }
  118. enable_dma_buffers(stream->dma);
  119. start_dma(stream->dma);
  120. }
  121. static irqreturn_t au1000_dma_interrupt(int irq, void *ptr)
  122. {
  123. struct audio_stream *stream = (struct audio_stream *)ptr;
  124. struct snd_pcm_substream *substream = stream->substream;
  125. switch (get_dma_buffer_done(stream->dma)) {
  126. case DMA_D0:
  127. stream->buffer = stream->buffer->next;
  128. clear_dma_done0(stream->dma);
  129. set_dma_addr0(stream->dma, stream->buffer->next->start);
  130. set_dma_count0(stream->dma, stream->period_size >> 1);
  131. enable_dma_buffer0(stream->dma);
  132. break;
  133. case DMA_D1:
  134. stream->buffer = stream->buffer->next;
  135. clear_dma_done1(stream->dma);
  136. set_dma_addr1(stream->dma, stream->buffer->next->start);
  137. set_dma_count1(stream->dma, stream->period_size >> 1);
  138. enable_dma_buffer1(stream->dma);
  139. break;
  140. case (DMA_D0 | DMA_D1):
  141. pr_debug("DMA %d missed interrupt.\n", stream->dma);
  142. au1000_dma_stop(stream);
  143. au1000_dma_start(stream);
  144. break;
  145. case (~DMA_D0 & ~DMA_D1):
  146. pr_debug("DMA %d empty irq.\n", stream->dma);
  147. }
  148. snd_pcm_period_elapsed(substream);
  149. return IRQ_HANDLED;
  150. }
  151. static const struct snd_pcm_hardware alchemy_pcm_hardware = {
  152. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  153. SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
  154. .formats = ALCHEMY_PCM_FMTS,
  155. .rates = SNDRV_PCM_RATE_8000_192000,
  156. .rate_min = SNDRV_PCM_RATE_8000,
  157. .rate_max = SNDRV_PCM_RATE_192000,
  158. .channels_min = 2,
  159. .channels_max = 2,
  160. .period_bytes_min = 1024,
  161. .period_bytes_max = 16 * 1024 - 1,
  162. .periods_min = 4,
  163. .periods_max = 255,
  164. .buffer_bytes_max = 128 * 1024,
  165. .fifo_size = 16,
  166. };
  167. static inline struct alchemy_pcm_ctx *ss_to_ctx(struct snd_pcm_substream *ss)
  168. {
  169. struct snd_soc_pcm_runtime *rtd = ss->private_data;
  170. return snd_soc_platform_get_drvdata(rtd->platform);
  171. }
  172. static inline struct audio_stream *ss_to_as(struct snd_pcm_substream *ss)
  173. {
  174. struct alchemy_pcm_ctx *ctx = ss_to_ctx(ss);
  175. return &(ctx->stream[ss->stream]);
  176. }
  177. static int alchemy_pcm_open(struct snd_pcm_substream *substream)
  178. {
  179. struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream);
  180. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  181. int *dmaids, s = substream->stream;
  182. char *name;
  183. dmaids = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  184. if (!dmaids)
  185. return -ENODEV; /* whoa, has ordering changed? */
  186. /* DMA setup */
  187. name = (s == SNDRV_PCM_STREAM_PLAYBACK) ? "audio-tx" : "audio-rx";
  188. ctx->stream[s].dma = request_au1000_dma(dmaids[s], name,
  189. au1000_dma_interrupt, 0,
  190. &ctx->stream[s]);
  191. set_dma_mode(ctx->stream[s].dma,
  192. get_dma_mode(ctx->stream[s].dma) & ~DMA_NC);
  193. ctx->stream[s].substream = substream;
  194. ctx->stream[s].buffer = NULL;
  195. snd_soc_set_runtime_hwparams(substream, &alchemy_pcm_hardware);
  196. return 0;
  197. }
  198. static int alchemy_pcm_close(struct snd_pcm_substream *substream)
  199. {
  200. struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream);
  201. int stype = substream->stream;
  202. ctx->stream[stype].substream = NULL;
  203. free_au1000_dma(ctx->stream[stype].dma);
  204. return 0;
  205. }
  206. static int alchemy_pcm_hw_params(struct snd_pcm_substream *substream,
  207. struct snd_pcm_hw_params *hw_params)
  208. {
  209. struct audio_stream *stream = ss_to_as(substream);
  210. int err;
  211. err = snd_pcm_lib_malloc_pages(substream,
  212. params_buffer_bytes(hw_params));
  213. if (err < 0)
  214. return err;
  215. err = au1000_setup_dma_link(stream,
  216. params_period_bytes(hw_params),
  217. params_periods(hw_params));
  218. if (err)
  219. snd_pcm_lib_free_pages(substream);
  220. return err;
  221. }
  222. static int alchemy_pcm_hw_free(struct snd_pcm_substream *substream)
  223. {
  224. struct audio_stream *stream = ss_to_as(substream);
  225. au1000_release_dma_link(stream);
  226. return snd_pcm_lib_free_pages(substream);
  227. }
  228. static int alchemy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  229. {
  230. struct audio_stream *stream = ss_to_as(substream);
  231. int err = 0;
  232. switch (cmd) {
  233. case SNDRV_PCM_TRIGGER_START:
  234. au1000_dma_start(stream);
  235. break;
  236. case SNDRV_PCM_TRIGGER_STOP:
  237. au1000_dma_stop(stream);
  238. break;
  239. default:
  240. err = -EINVAL;
  241. break;
  242. }
  243. return err;
  244. }
  245. static snd_pcm_uframes_t alchemy_pcm_pointer(struct snd_pcm_substream *ss)
  246. {
  247. struct audio_stream *stream = ss_to_as(ss);
  248. long location;
  249. location = get_dma_residue(stream->dma);
  250. location = stream->buffer->relative_end - location;
  251. if (location == -1)
  252. location = 0;
  253. return bytes_to_frames(ss->runtime, location);
  254. }
  255. static struct snd_pcm_ops alchemy_pcm_ops = {
  256. .open = alchemy_pcm_open,
  257. .close = alchemy_pcm_close,
  258. .ioctl = snd_pcm_lib_ioctl,
  259. .hw_params = alchemy_pcm_hw_params,
  260. .hw_free = alchemy_pcm_hw_free,
  261. .trigger = alchemy_pcm_trigger,
  262. .pointer = alchemy_pcm_pointer,
  263. };
  264. static void alchemy_pcm_free_dma_buffers(struct snd_pcm *pcm)
  265. {
  266. snd_pcm_lib_preallocate_free_for_all(pcm);
  267. }
  268. static int alchemy_pcm_new(struct snd_soc_pcm_runtime *rtd)
  269. {
  270. struct snd_pcm *pcm = rtd->pcm;
  271. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  272. snd_dma_continuous_data(GFP_KERNEL), 65536, (4096 * 1024) - 1);
  273. return 0;
  274. }
  275. static struct snd_soc_platform_driver alchemy_pcm_soc_platform = {
  276. .ops = &alchemy_pcm_ops,
  277. .pcm_new = alchemy_pcm_new,
  278. .pcm_free = alchemy_pcm_free_dma_buffers,
  279. };
  280. static int __devinit alchemy_pcm_drvprobe(struct platform_device *pdev)
  281. {
  282. struct alchemy_pcm_ctx *ctx;
  283. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  284. if (!ctx)
  285. return -ENOMEM;
  286. platform_set_drvdata(pdev, ctx);
  287. return snd_soc_register_platform(&pdev->dev, &alchemy_pcm_soc_platform);
  288. }
  289. static int __devexit alchemy_pcm_drvremove(struct platform_device *pdev)
  290. {
  291. snd_soc_unregister_platform(&pdev->dev);
  292. return 0;
  293. }
  294. static struct platform_driver alchemy_pcmdma_driver = {
  295. .driver = {
  296. .name = "alchemy-pcm-dma",
  297. .owner = THIS_MODULE,
  298. },
  299. .probe = alchemy_pcm_drvprobe,
  300. .remove = __devexit_p(alchemy_pcm_drvremove),
  301. };
  302. module_platform_driver(alchemy_pcmdma_driver);
  303. MODULE_LICENSE("GPL");
  304. MODULE_DESCRIPTION("Au1000/Au1500/Au1100 Audio DMA driver");
  305. MODULE_AUTHOR("Manuel Lauss");