dma.c 8.4 KB

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