kirkwood-dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * kirkwood-dma.c
  3. *
  4. * (c) 2010 Arnaud Patard <apatard@mandriva.com>
  5. * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/mbus.h>
  20. #include <sound/soc.h>
  21. #include "kirkwood.h"
  22. #define KIRKWOOD_RATES \
  23. (SNDRV_PCM_RATE_44100 | \
  24. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000)
  25. #define KIRKWOOD_FORMATS \
  26. (SNDRV_PCM_FMTBIT_S16_LE | \
  27. SNDRV_PCM_FMTBIT_S24_LE | \
  28. SNDRV_PCM_FMTBIT_S32_LE)
  29. struct kirkwood_dma_priv {
  30. struct snd_pcm_substream *play_stream;
  31. struct snd_pcm_substream *rec_stream;
  32. struct kirkwood_dma_data *data;
  33. };
  34. static struct snd_pcm_hardware kirkwood_dma_snd_hw = {
  35. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  36. SNDRV_PCM_INFO_MMAP |
  37. SNDRV_PCM_INFO_MMAP_VALID |
  38. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  39. SNDRV_PCM_INFO_PAUSE),
  40. .formats = KIRKWOOD_FORMATS,
  41. .rates = KIRKWOOD_RATES,
  42. .rate_min = 44100,
  43. .rate_max = 96000,
  44. .channels_min = 1,
  45. .channels_max = 2,
  46. .buffer_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES * KIRKWOOD_SND_MAX_PERIODS,
  47. .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES,
  48. .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES,
  49. .periods_min = KIRKWOOD_SND_MIN_PERIODS,
  50. .periods_max = KIRKWOOD_SND_MAX_PERIODS,
  51. .fifo_size = 0,
  52. };
  53. static u64 kirkwood_dma_dmamask = DMA_BIT_MASK(32);
  54. static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
  55. {
  56. struct kirkwood_dma_priv *prdata = dev_id;
  57. struct kirkwood_dma_data *priv = prdata->data;
  58. unsigned long mask, status, cause;
  59. mask = readl(priv->io + KIRKWOOD_INT_MASK);
  60. status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask;
  61. cause = readl(priv->io + KIRKWOOD_ERR_CAUSE);
  62. if (unlikely(cause)) {
  63. printk(KERN_WARNING "%s: got err interrupt 0x%lx\n",
  64. __func__, cause);
  65. writel(cause, priv->io + KIRKWOOD_ERR_CAUSE);
  66. return IRQ_HANDLED;
  67. }
  68. /* we've enabled only bytes interrupts ... */
  69. if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \
  70. KIRKWOOD_INT_CAUSE_REC_BYTES)) {
  71. printk(KERN_WARNING "%s: unexpected interrupt %lx\n",
  72. __func__, status);
  73. return IRQ_NONE;
  74. }
  75. /* ack int */
  76. writel(status, priv->io + KIRKWOOD_INT_CAUSE);
  77. if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES)
  78. snd_pcm_period_elapsed(prdata->play_stream);
  79. if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
  80. snd_pcm_period_elapsed(prdata->rec_stream);
  81. return IRQ_HANDLED;
  82. }
  83. static void
  84. kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
  85. unsigned long dma,
  86. const struct mbus_dram_target_info *dram)
  87. {
  88. int i;
  89. /* First disable and clear windows */
  90. writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
  91. writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
  92. /* try to find matching cs for current dma address */
  93. for (i = 0; i < dram->num_cs; i++) {
  94. const struct mbus_dram_window *cs = dram->cs + i;
  95. if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
  96. writel(cs->base & 0xffff0000,
  97. base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
  98. writel(((cs->size - 1) & 0xffff0000) |
  99. (cs->mbus_attr << 8) |
  100. (dram->mbus_dram_target_id << 4) | 1,
  101. base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
  102. }
  103. }
  104. }
  105. static int kirkwood_dma_open(struct snd_pcm_substream *substream)
  106. {
  107. int err;
  108. struct snd_pcm_runtime *runtime = substream->runtime;
  109. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  110. struct snd_soc_platform *platform = soc_runtime->platform;
  111. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  112. struct kirkwood_dma_data *priv;
  113. struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
  114. const struct mbus_dram_target_info *dram;
  115. unsigned long addr;
  116. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  117. snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
  118. /* Ensure that all constraints linked to dma burst are fulfilled */
  119. err = snd_pcm_hw_constraint_minmax(runtime,
  120. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  121. priv->burst * 2,
  122. KIRKWOOD_AUDIO_BUF_MAX-1);
  123. if (err < 0)
  124. return err;
  125. err = snd_pcm_hw_constraint_step(runtime, 0,
  126. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  127. priv->burst);
  128. if (err < 0)
  129. return err;
  130. err = snd_pcm_hw_constraint_step(substream->runtime, 0,
  131. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  132. priv->burst);
  133. if (err < 0)
  134. return err;
  135. if (prdata == NULL) {
  136. prdata = kzalloc(sizeof(struct kirkwood_dma_priv), GFP_KERNEL);
  137. if (prdata == NULL)
  138. return -ENOMEM;
  139. prdata->data = priv;
  140. err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
  141. "kirkwood-i2s", prdata);
  142. if (err) {
  143. kfree(prdata);
  144. return -EBUSY;
  145. }
  146. snd_soc_platform_set_drvdata(platform, prdata);
  147. /*
  148. * Enable Error interrupts. We're only ack'ing them but
  149. * it's useful for diagnostics
  150. */
  151. writel((unsigned long)-1, priv->io + KIRKWOOD_ERR_MASK);
  152. }
  153. dram = mv_mbus_dram_info();
  154. addr = virt_to_phys(substream->dma_buffer.area);
  155. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  156. prdata->play_stream = substream;
  157. kirkwood_dma_conf_mbus_windows(priv->io,
  158. KIRKWOOD_PLAYBACK_WIN, addr, dram);
  159. } else {
  160. prdata->rec_stream = substream;
  161. kirkwood_dma_conf_mbus_windows(priv->io,
  162. KIRKWOOD_RECORD_WIN, addr, dram);
  163. }
  164. return 0;
  165. }
  166. static int kirkwood_dma_close(struct snd_pcm_substream *substream)
  167. {
  168. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  169. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  170. struct snd_soc_platform *platform = soc_runtime->platform;
  171. struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
  172. struct kirkwood_dma_data *priv;
  173. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  174. if (!prdata || !priv)
  175. return 0;
  176. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  177. prdata->play_stream = NULL;
  178. else
  179. prdata->rec_stream = NULL;
  180. if (!prdata->play_stream && !prdata->rec_stream) {
  181. writel(0, priv->io + KIRKWOOD_ERR_MASK);
  182. free_irq(priv->irq, prdata);
  183. kfree(prdata);
  184. snd_soc_platform_set_drvdata(platform, NULL);
  185. }
  186. return 0;
  187. }
  188. static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream,
  189. struct snd_pcm_hw_params *params)
  190. {
  191. struct snd_pcm_runtime *runtime = substream->runtime;
  192. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  193. runtime->dma_bytes = params_buffer_bytes(params);
  194. return 0;
  195. }
  196. static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream)
  197. {
  198. snd_pcm_set_runtime_buffer(substream, NULL);
  199. return 0;
  200. }
  201. static int kirkwood_dma_prepare(struct snd_pcm_substream *substream)
  202. {
  203. struct snd_pcm_runtime *runtime = substream->runtime;
  204. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  205. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  206. struct kirkwood_dma_data *priv;
  207. unsigned long size, count;
  208. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  209. /* compute buffer size in term of "words" as requested in specs */
  210. size = frames_to_bytes(runtime, runtime->buffer_size);
  211. size = (size>>2)-1;
  212. count = snd_pcm_lib_period_bytes(substream);
  213. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  214. writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
  215. writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
  216. writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
  217. } else {
  218. writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
  219. writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
  220. writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
  221. }
  222. return 0;
  223. }
  224. static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream
  225. *substream)
  226. {
  227. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  228. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  229. struct kirkwood_dma_data *priv;
  230. snd_pcm_uframes_t count;
  231. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  232. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  233. count = bytes_to_frames(substream->runtime,
  234. readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
  235. else
  236. count = bytes_to_frames(substream->runtime,
  237. readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
  238. return count;
  239. }
  240. struct snd_pcm_ops kirkwood_dma_ops = {
  241. .open = kirkwood_dma_open,
  242. .close = kirkwood_dma_close,
  243. .ioctl = snd_pcm_lib_ioctl,
  244. .hw_params = kirkwood_dma_hw_params,
  245. .hw_free = kirkwood_dma_hw_free,
  246. .prepare = kirkwood_dma_prepare,
  247. .pointer = kirkwood_dma_pointer,
  248. };
  249. static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
  250. int stream)
  251. {
  252. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  253. struct snd_dma_buffer *buf = &substream->dma_buffer;
  254. size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
  255. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  256. buf->dev.dev = pcm->card->dev;
  257. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  258. &buf->addr, GFP_KERNEL);
  259. if (!buf->area)
  260. return -ENOMEM;
  261. buf->bytes = size;
  262. buf->private_data = NULL;
  263. return 0;
  264. }
  265. static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd)
  266. {
  267. struct snd_card *card = rtd->card->snd_card;
  268. struct snd_pcm *pcm = rtd->pcm;
  269. int ret;
  270. if (!card->dev->dma_mask)
  271. card->dev->dma_mask = &kirkwood_dma_dmamask;
  272. if (!card->dev->coherent_dma_mask)
  273. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  274. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  275. ret = kirkwood_dma_preallocate_dma_buffer(pcm,
  276. SNDRV_PCM_STREAM_PLAYBACK);
  277. if (ret)
  278. return ret;
  279. }
  280. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  281. ret = kirkwood_dma_preallocate_dma_buffer(pcm,
  282. SNDRV_PCM_STREAM_CAPTURE);
  283. if (ret)
  284. return ret;
  285. }
  286. return 0;
  287. }
  288. static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm)
  289. {
  290. struct snd_pcm_substream *substream;
  291. struct snd_dma_buffer *buf;
  292. int stream;
  293. for (stream = 0; stream < 2; stream++) {
  294. substream = pcm->streams[stream].substream;
  295. if (!substream)
  296. continue;
  297. buf = &substream->dma_buffer;
  298. if (!buf->area)
  299. continue;
  300. dma_free_coherent(pcm->card->dev, buf->bytes,
  301. buf->area, buf->addr);
  302. buf->area = NULL;
  303. }
  304. }
  305. static struct snd_soc_platform_driver kirkwood_soc_platform = {
  306. .ops = &kirkwood_dma_ops,
  307. .pcm_new = kirkwood_dma_new,
  308. .pcm_free = kirkwood_dma_free_dma_buffers,
  309. };
  310. static int __devinit kirkwood_soc_platform_probe(struct platform_device *pdev)
  311. {
  312. return snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform);
  313. }
  314. static int __devexit kirkwood_soc_platform_remove(struct platform_device *pdev)
  315. {
  316. snd_soc_unregister_platform(&pdev->dev);
  317. return 0;
  318. }
  319. static struct platform_driver kirkwood_pcm_driver = {
  320. .driver = {
  321. .name = "kirkwood-pcm-audio",
  322. .owner = THIS_MODULE,
  323. },
  324. .probe = kirkwood_soc_platform_probe,
  325. .remove = __devexit_p(kirkwood_soc_platform_remove),
  326. };
  327. module_platform_driver(kirkwood_pcm_driver);
  328. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  329. MODULE_DESCRIPTION("Marvell Kirkwood Audio DMA module");
  330. MODULE_LICENSE("GPL");
  331. MODULE_ALIAS("platform:kirkwood-pcm-audio");