mxs-pcm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * Based on sound/soc/imx/imx-pcm-dma-mx2.c
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/dmaengine.h>
  30. #include <sound/core.h>
  31. #include <sound/initval.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <mach/dma.h>
  36. #include "mxs-pcm.h"
  37. static struct snd_pcm_hardware snd_mxs_hardware = {
  38. .info = SNDRV_PCM_INFO_MMAP |
  39. SNDRV_PCM_INFO_MMAP_VALID |
  40. SNDRV_PCM_INFO_PAUSE |
  41. SNDRV_PCM_INFO_RESUME |
  42. SNDRV_PCM_INFO_INTERLEAVED,
  43. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  44. SNDRV_PCM_FMTBIT_S20_3LE |
  45. SNDRV_PCM_FMTBIT_S24_LE,
  46. .channels_min = 2,
  47. .channels_max = 2,
  48. .period_bytes_min = 32,
  49. .period_bytes_max = 8192,
  50. .periods_min = 1,
  51. .periods_max = 52,
  52. .buffer_bytes_max = 64 * 1024,
  53. .fifo_size = 32,
  54. };
  55. static void audio_dma_irq(void *data)
  56. {
  57. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
  58. struct snd_pcm_runtime *runtime = substream->runtime;
  59. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  60. iprtd->offset += iprtd->period_bytes;
  61. iprtd->offset %= iprtd->period_bytes * iprtd->periods;
  62. snd_pcm_period_elapsed(substream);
  63. }
  64. static bool filter(struct dma_chan *chan, void *param)
  65. {
  66. struct mxs_pcm_runtime_data *iprtd = param;
  67. struct mxs_pcm_dma_params *dma_params = iprtd->dma_params;
  68. if (!mxs_dma_is_apbx(chan))
  69. return false;
  70. if (chan->chan_id != dma_params->chan_num)
  71. return false;
  72. chan->private = &iprtd->dma_data;
  73. return true;
  74. }
  75. static int mxs_dma_alloc(struct snd_pcm_substream *substream)
  76. {
  77. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  78. struct snd_pcm_runtime *runtime = substream->runtime;
  79. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  80. dma_cap_mask_t mask;
  81. iprtd->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  82. dma_cap_zero(mask);
  83. dma_cap_set(DMA_SLAVE, mask);
  84. iprtd->dma_data.chan_irq = iprtd->dma_params->chan_irq;
  85. iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
  86. if (!iprtd->dma_chan)
  87. return -EINVAL;
  88. return 0;
  89. }
  90. static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
  91. struct snd_pcm_hw_params *params)
  92. {
  93. struct snd_pcm_runtime *runtime = substream->runtime;
  94. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  95. unsigned long dma_addr;
  96. struct dma_chan *chan;
  97. chan = iprtd->dma_chan;
  98. iprtd->periods = params_periods(params);
  99. iprtd->period_bytes = params_period_bytes(params);
  100. iprtd->offset = 0;
  101. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  102. dma_addr = runtime->dma_addr;
  103. iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
  104. iprtd->period_bytes * iprtd->periods,
  105. iprtd->period_bytes,
  106. substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  107. DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  108. if (!iprtd->desc) {
  109. dev_err(&chan->dev->device, "cannot prepare slave dma\n");
  110. return -EINVAL;
  111. }
  112. iprtd->desc->callback = audio_dma_irq;
  113. iprtd->desc->callback_param = substream;
  114. return 0;
  115. }
  116. static int snd_mxs_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  117. {
  118. struct snd_pcm_runtime *runtime = substream->runtime;
  119. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  120. switch (cmd) {
  121. case SNDRV_PCM_TRIGGER_START:
  122. case SNDRV_PCM_TRIGGER_RESUME:
  123. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  124. dmaengine_submit(iprtd->desc);
  125. break;
  126. case SNDRV_PCM_TRIGGER_STOP:
  127. case SNDRV_PCM_TRIGGER_SUSPEND:
  128. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  129. dmaengine_terminate_all(iprtd->dma_chan);
  130. break;
  131. default:
  132. return -EINVAL;
  133. }
  134. return 0;
  135. }
  136. static snd_pcm_uframes_t snd_mxs_pcm_pointer(
  137. struct snd_pcm_substream *substream)
  138. {
  139. struct snd_pcm_runtime *runtime = substream->runtime;
  140. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  141. return bytes_to_frames(substream->runtime, iprtd->offset);
  142. }
  143. static int snd_mxs_open(struct snd_pcm_substream *substream)
  144. {
  145. struct snd_pcm_runtime *runtime = substream->runtime;
  146. struct mxs_pcm_runtime_data *iprtd;
  147. int ret;
  148. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  149. if (iprtd == NULL)
  150. return -ENOMEM;
  151. runtime->private_data = iprtd;
  152. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  153. SNDRV_PCM_HW_PARAM_PERIODS);
  154. if (ret < 0) {
  155. kfree(iprtd);
  156. return ret;
  157. }
  158. ret = mxs_dma_alloc(substream);
  159. if (ret) {
  160. kfree(iprtd);
  161. return ret;
  162. }
  163. snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
  164. return 0;
  165. }
  166. static int snd_mxs_close(struct snd_pcm_substream *substream)
  167. {
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  170. dma_release_channel(iprtd->dma_chan);
  171. kfree(iprtd);
  172. return 0;
  173. }
  174. static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
  175. struct vm_area_struct *vma)
  176. {
  177. struct snd_pcm_runtime *runtime = substream->runtime;
  178. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  179. runtime->dma_area,
  180. runtime->dma_addr,
  181. runtime->dma_bytes);
  182. }
  183. static struct snd_pcm_ops mxs_pcm_ops = {
  184. .open = snd_mxs_open,
  185. .close = snd_mxs_close,
  186. .ioctl = snd_pcm_lib_ioctl,
  187. .hw_params = snd_mxs_pcm_hw_params,
  188. .trigger = snd_mxs_pcm_trigger,
  189. .pointer = snd_mxs_pcm_pointer,
  190. .mmap = snd_mxs_pcm_mmap,
  191. };
  192. static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  193. {
  194. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  195. struct snd_dma_buffer *buf = &substream->dma_buffer;
  196. size_t size = snd_mxs_hardware.buffer_bytes_max;
  197. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  198. buf->dev.dev = pcm->card->dev;
  199. buf->private_data = NULL;
  200. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  201. &buf->addr, GFP_KERNEL);
  202. if (!buf->area)
  203. return -ENOMEM;
  204. buf->bytes = size;
  205. return 0;
  206. }
  207. static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
  208. static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
  209. {
  210. struct snd_card *card = rtd->card->snd_card;
  211. struct snd_pcm *pcm = rtd->pcm;
  212. int ret = 0;
  213. if (!card->dev->dma_mask)
  214. card->dev->dma_mask = &mxs_pcm_dmamask;
  215. if (!card->dev->coherent_dma_mask)
  216. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  217. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  218. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  219. SNDRV_PCM_STREAM_PLAYBACK);
  220. if (ret)
  221. goto out;
  222. }
  223. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  224. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  225. SNDRV_PCM_STREAM_CAPTURE);
  226. if (ret)
  227. goto out;
  228. }
  229. out:
  230. return ret;
  231. }
  232. static void mxs_pcm_free(struct snd_pcm *pcm)
  233. {
  234. struct snd_pcm_substream *substream;
  235. struct snd_dma_buffer *buf;
  236. int stream;
  237. for (stream = 0; stream < 2; stream++) {
  238. substream = pcm->streams[stream].substream;
  239. if (!substream)
  240. continue;
  241. buf = &substream->dma_buffer;
  242. if (!buf->area)
  243. continue;
  244. dma_free_writecombine(pcm->card->dev, buf->bytes,
  245. buf->area, buf->addr);
  246. buf->area = NULL;
  247. }
  248. }
  249. static struct snd_soc_platform_driver mxs_soc_platform = {
  250. .ops = &mxs_pcm_ops,
  251. .pcm_new = mxs_pcm_new,
  252. .pcm_free = mxs_pcm_free,
  253. };
  254. static int __devinit mxs_soc_platform_probe(struct platform_device *pdev)
  255. {
  256. return snd_soc_register_platform(&pdev->dev, &mxs_soc_platform);
  257. }
  258. static int __devexit mxs_soc_platform_remove(struct platform_device *pdev)
  259. {
  260. snd_soc_unregister_platform(&pdev->dev);
  261. return 0;
  262. }
  263. static struct platform_driver mxs_pcm_driver = {
  264. .driver = {
  265. .name = "mxs-pcm-audio",
  266. .owner = THIS_MODULE,
  267. },
  268. .probe = mxs_soc_platform_probe,
  269. .remove = __devexit_p(mxs_soc_platform_remove),
  270. };
  271. module_platform_driver(mxs_pcm_driver);
  272. MODULE_LICENSE("GPL");
  273. MODULE_ALIAS("platform:mxs-pcm-audio");