dma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Au1000/Au1500/Au1100 Audio DMA support.
  4. *
  5. * (c) 2011 Manuel Lauss <manuel.lauss@googlemail.com>
  6. *
  7. * copied almost verbatim from the old ALSA driver, written by
  8. * Charles Eidsness <charles@cooper-street.com>
  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_obj(struct pcm_period);
  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_obj(struct pcm_period);
  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. struct snd_soc_component *component)
  157. {
  158. return snd_soc_component_get_drvdata(component);
  159. }
  160. static inline struct audio_stream *ss_to_as(struct snd_pcm_substream *ss,
  161. struct snd_soc_component *component)
  162. {
  163. struct alchemy_pcm_ctx *ctx = ss_to_ctx(ss, component);
  164. return &(ctx->stream[ss->stream]);
  165. }
  166. static int alchemy_pcm_open(struct snd_soc_component *component,
  167. struct snd_pcm_substream *substream)
  168. {
  169. struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
  170. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  171. int *dmaids, s = substream->stream;
  172. char *name;
  173. dmaids = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
  174. if (!dmaids)
  175. return -ENODEV; /* whoa, has ordering changed? */
  176. /* DMA setup */
  177. name = (s == SNDRV_PCM_STREAM_PLAYBACK) ? "audio-tx" : "audio-rx";
  178. ctx->stream[s].dma = request_au1000_dma(dmaids[s], name,
  179. au1000_dma_interrupt, 0,
  180. &ctx->stream[s]);
  181. set_dma_mode(ctx->stream[s].dma,
  182. get_dma_mode(ctx->stream[s].dma) & ~DMA_NC);
  183. ctx->stream[s].substream = substream;
  184. ctx->stream[s].buffer = NULL;
  185. snd_soc_set_runtime_hwparams(substream, &alchemy_pcm_hardware);
  186. return 0;
  187. }
  188. static int alchemy_pcm_close(struct snd_soc_component *component,
  189. struct snd_pcm_substream *substream)
  190. {
  191. struct alchemy_pcm_ctx *ctx = ss_to_ctx(substream, component);
  192. int stype = substream->stream;
  193. ctx->stream[stype].substream = NULL;
  194. free_au1000_dma(ctx->stream[stype].dma);
  195. return 0;
  196. }
  197. static int alchemy_pcm_hw_params(struct snd_soc_component *component,
  198. struct snd_pcm_substream *substream,
  199. struct snd_pcm_hw_params *hw_params)
  200. {
  201. struct audio_stream *stream = ss_to_as(substream, component);
  202. return au1000_setup_dma_link(stream,
  203. params_period_bytes(hw_params),
  204. params_periods(hw_params));
  205. }
  206. static int alchemy_pcm_hw_free(struct snd_soc_component *component,
  207. struct snd_pcm_substream *substream)
  208. {
  209. struct audio_stream *stream = ss_to_as(substream, component);
  210. au1000_release_dma_link(stream);
  211. return 0;
  212. }
  213. static int alchemy_pcm_trigger(struct snd_soc_component *component,
  214. struct snd_pcm_substream *substream, int cmd)
  215. {
  216. struct audio_stream *stream = ss_to_as(substream, component);
  217. int err = 0;
  218. switch (cmd) {
  219. case SNDRV_PCM_TRIGGER_START:
  220. au1000_dma_start(stream);
  221. break;
  222. case SNDRV_PCM_TRIGGER_STOP:
  223. au1000_dma_stop(stream);
  224. break;
  225. default:
  226. err = -EINVAL;
  227. break;
  228. }
  229. return err;
  230. }
  231. static snd_pcm_uframes_t alchemy_pcm_pointer(struct snd_soc_component *component,
  232. struct snd_pcm_substream *ss)
  233. {
  234. struct audio_stream *stream = ss_to_as(ss, component);
  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 int alchemy_pcm_new(struct snd_soc_component *component,
  243. struct snd_soc_pcm_runtime *rtd)
  244. {
  245. struct snd_pcm *pcm = rtd->pcm;
  246. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  247. NULL, 65536, (4096 * 1024) - 1);
  248. return 0;
  249. }
  250. static const struct snd_soc_component_driver alchemy_pcm_soc_component = {
  251. .name = DRV_NAME,
  252. .open = alchemy_pcm_open,
  253. .close = alchemy_pcm_close,
  254. .hw_params = alchemy_pcm_hw_params,
  255. .hw_free = alchemy_pcm_hw_free,
  256. .trigger = alchemy_pcm_trigger,
  257. .pointer = alchemy_pcm_pointer,
  258. .pcm_construct = alchemy_pcm_new,
  259. };
  260. static int alchemy_pcm_drvprobe(struct platform_device *pdev)
  261. {
  262. struct alchemy_pcm_ctx *ctx;
  263. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  264. if (!ctx)
  265. return -ENOMEM;
  266. platform_set_drvdata(pdev, ctx);
  267. return devm_snd_soc_register_component(&pdev->dev,
  268. &alchemy_pcm_soc_component, NULL, 0);
  269. }
  270. static struct platform_driver alchemy_pcmdma_driver = {
  271. .driver = {
  272. .name = "alchemy-pcm-dma",
  273. },
  274. .probe = alchemy_pcm_drvprobe,
  275. };
  276. module_platform_driver(alchemy_pcmdma_driver);
  277. MODULE_LICENSE("GPL");
  278. MODULE_DESCRIPTION("Au1000/Au1500/Au1100 Audio DMA driver");
  279. MODULE_AUTHOR("Manuel Lauss");