dma.c 8.6 KB

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