kirkwood-dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 = 0xFFFFFFFFUL;
  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 kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
  84. unsigned long dma,
  85. struct mbus_dram_target_info *dram)
  86. {
  87. int i;
  88. /* First disable and clear windows */
  89. writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
  90. writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
  91. /* try to find matching cs for current dma address */
  92. for (i = 0; i < dram->num_cs; i++) {
  93. struct mbus_dram_window *cs = dram->cs + i;
  94. if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
  95. writel(cs->base & 0xffff0000,
  96. base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
  97. writel(((cs->size - 1) & 0xffff0000) |
  98. (cs->mbus_attr << 8) |
  99. (dram->mbus_dram_target_id << 4) | 1,
  100. base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
  101. }
  102. }
  103. }
  104. static int kirkwood_dma_open(struct snd_pcm_substream *substream)
  105. {
  106. int err;
  107. struct snd_pcm_runtime *runtime = substream->runtime;
  108. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  109. struct snd_soc_platform *platform = soc_runtime->platform;
  110. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  111. struct kirkwood_dma_data *priv;
  112. struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
  113. unsigned long addr;
  114. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  115. snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
  116. /* Ensure that all constraints linked to dma burst are fulfilled */
  117. err = snd_pcm_hw_constraint_minmax(runtime,
  118. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  119. priv->burst * 2,
  120. KIRKWOOD_AUDIO_BUF_MAX-1);
  121. if (err < 0)
  122. return err;
  123. err = snd_pcm_hw_constraint_step(runtime, 0,
  124. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  125. priv->burst);
  126. if (err < 0)
  127. return err;
  128. err = snd_pcm_hw_constraint_step(substream->runtime, 0,
  129. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  130. priv->burst);
  131. if (err < 0)
  132. return err;
  133. if (prdata == NULL) {
  134. prdata = kzalloc(sizeof(struct kirkwood_dma_priv), GFP_KERNEL);
  135. if (prdata == NULL)
  136. return -ENOMEM;
  137. prdata->data = priv;
  138. err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
  139. "kirkwood-i2s", prdata);
  140. if (err) {
  141. kfree(prdata);
  142. return -EBUSY;
  143. }
  144. snd_soc_platform_set_drvdata(platform, prdata);
  145. /*
  146. * Enable Error interrupts. We're only ack'ing them but
  147. * it's useful for diagnostics
  148. */
  149. writel((unsigned long)-1, priv->io + KIRKWOOD_ERR_MASK);
  150. }
  151. addr = virt_to_phys(substream->dma_buffer.area);
  152. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  153. prdata->play_stream = substream;
  154. kirkwood_dma_conf_mbus_windows(priv->io,
  155. KIRKWOOD_PLAYBACK_WIN, addr, priv->dram);
  156. } else {
  157. prdata->rec_stream = substream;
  158. kirkwood_dma_conf_mbus_windows(priv->io,
  159. KIRKWOOD_RECORD_WIN, addr, priv->dram);
  160. }
  161. return 0;
  162. }
  163. static int kirkwood_dma_close(struct snd_pcm_substream *substream)
  164. {
  165. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  166. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  167. struct snd_soc_platform *platform = soc_runtime->platform;
  168. struct kirkwood_dma_priv *prdata = snd_soc_platform_get_drvdata(platform);
  169. struct kirkwood_dma_data *priv;
  170. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  171. if (!prdata || !priv)
  172. return 0;
  173. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  174. prdata->play_stream = NULL;
  175. else
  176. prdata->rec_stream = NULL;
  177. if (!prdata->play_stream && !prdata->rec_stream) {
  178. writel(0, priv->io + KIRKWOOD_ERR_MASK);
  179. free_irq(priv->irq, prdata);
  180. kfree(prdata);
  181. snd_soc_platform_set_drvdata(platform, NULL);
  182. }
  183. return 0;
  184. }
  185. static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream,
  186. struct snd_pcm_hw_params *params)
  187. {
  188. struct snd_pcm_runtime *runtime = substream->runtime;
  189. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  190. runtime->dma_bytes = params_buffer_bytes(params);
  191. return 0;
  192. }
  193. static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream)
  194. {
  195. snd_pcm_set_runtime_buffer(substream, NULL);
  196. return 0;
  197. }
  198. static int kirkwood_dma_prepare(struct snd_pcm_substream *substream)
  199. {
  200. struct snd_pcm_runtime *runtime = substream->runtime;
  201. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  202. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  203. struct kirkwood_dma_data *priv;
  204. unsigned long size, count;
  205. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  206. /* compute buffer size in term of "words" as requested in specs */
  207. size = frames_to_bytes(runtime, runtime->buffer_size);
  208. size = (size>>2)-1;
  209. count = snd_pcm_lib_period_bytes(substream);
  210. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  211. writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
  212. writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
  213. writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
  214. } else {
  215. writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
  216. writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
  217. writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
  218. }
  219. return 0;
  220. }
  221. static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream
  222. *substream)
  223. {
  224. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  225. struct snd_soc_dai *cpu_dai = soc_runtime->cpu_dai;
  226. struct kirkwood_dma_data *priv;
  227. snd_pcm_uframes_t count;
  228. priv = snd_soc_dai_get_dma_data(cpu_dai, substream);
  229. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  230. count = bytes_to_frames(substream->runtime,
  231. readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
  232. else
  233. count = bytes_to_frames(substream->runtime,
  234. readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
  235. return count;
  236. }
  237. struct snd_pcm_ops kirkwood_dma_ops = {
  238. .open = kirkwood_dma_open,
  239. .close = kirkwood_dma_close,
  240. .ioctl = snd_pcm_lib_ioctl,
  241. .hw_params = kirkwood_dma_hw_params,
  242. .hw_free = kirkwood_dma_hw_free,
  243. .prepare = kirkwood_dma_prepare,
  244. .pointer = kirkwood_dma_pointer,
  245. };
  246. static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
  247. int stream)
  248. {
  249. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  250. struct snd_dma_buffer *buf = &substream->dma_buffer;
  251. size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
  252. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  253. buf->dev.dev = pcm->card->dev;
  254. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  255. &buf->addr, GFP_KERNEL);
  256. if (!buf->area)
  257. return -ENOMEM;
  258. buf->bytes = size;
  259. buf->private_data = NULL;
  260. return 0;
  261. }
  262. static int kirkwood_dma_new(struct snd_card *card,
  263. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  264. {
  265. int ret;
  266. if (!card->dev->dma_mask)
  267. card->dev->dma_mask = &kirkwood_dma_dmamask;
  268. if (!card->dev->coherent_dma_mask)
  269. card->dev->coherent_dma_mask = 0xffffffff;
  270. if (dai->driver->playback.channels_min) {
  271. ret = kirkwood_dma_preallocate_dma_buffer(pcm,
  272. SNDRV_PCM_STREAM_PLAYBACK);
  273. if (ret)
  274. return ret;
  275. }
  276. if (dai->driver->capture.channels_min) {
  277. ret = kirkwood_dma_preallocate_dma_buffer(pcm,
  278. SNDRV_PCM_STREAM_CAPTURE);
  279. if (ret)
  280. return ret;
  281. }
  282. return 0;
  283. }
  284. static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm)
  285. {
  286. struct snd_pcm_substream *substream;
  287. struct snd_dma_buffer *buf;
  288. int stream;
  289. for (stream = 0; stream < 2; stream++) {
  290. substream = pcm->streams[stream].substream;
  291. if (!substream)
  292. continue;
  293. buf = &substream->dma_buffer;
  294. if (!buf->area)
  295. continue;
  296. dma_free_coherent(pcm->card->dev, buf->bytes,
  297. buf->area, buf->addr);
  298. buf->area = NULL;
  299. }
  300. }
  301. static struct snd_soc_platform_driver kirkwood_soc_platform = {
  302. .ops = &kirkwood_dma_ops,
  303. .pcm_new = kirkwood_dma_new,
  304. .pcm_free = kirkwood_dma_free_dma_buffers,
  305. };
  306. static int __devinit kirkwood_soc_platform_probe(struct platform_device *pdev)
  307. {
  308. return snd_soc_register_platform(&pdev->dev, &kirkwood_soc_platform);
  309. }
  310. static int __devexit kirkwood_soc_platform_remove(struct platform_device *pdev)
  311. {
  312. snd_soc_unregister_platform(&pdev->dev);
  313. return 0;
  314. }
  315. static struct platform_driver kirkwood_pcm_driver = {
  316. .driver = {
  317. .name = "kirkwood-pcm-audio",
  318. .owner = THIS_MODULE,
  319. },
  320. .probe = kirkwood_soc_platform_probe,
  321. .remove = __devexit_p(kirkwood_soc_platform_remove),
  322. };
  323. static int __init kirkwood_pcm_init(void)
  324. {
  325. return platform_driver_register(&kirkwood_pcm_driver);
  326. }
  327. module_init(kirkwood_pcm_init);
  328. static void __exit kirkwood_pcm_exit(void)
  329. {
  330. platform_driver_unregister(&kirkwood_pcm_driver);
  331. }
  332. module_exit(kirkwood_pcm_exit);
  333. MODULE_AUTHOR("Arnaud Patard <arnaud.patard@rtp-net.org>");
  334. MODULE_DESCRIPTION("Marvell Kirkwood Audio DMA module");
  335. MODULE_LICENSE("GPL");
  336. MODULE_ALIAS("platform:kirkwood-pcm-audio");