dma.c 8.6 KB

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