mxs-pcm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. struct snd_pcm_hw_params *params)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  79. struct snd_pcm_runtime *runtime = substream->runtime;
  80. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  81. dma_cap_mask_t mask;
  82. iprtd->dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  83. dma_cap_zero(mask);
  84. dma_cap_set(DMA_SLAVE, mask);
  85. iprtd->dma_data.chan_irq = iprtd->dma_params->chan_irq;
  86. iprtd->dma_chan = dma_request_channel(mask, filter, iprtd);
  87. if (!iprtd->dma_chan)
  88. return -EINVAL;
  89. return 0;
  90. }
  91. static int snd_mxs_pcm_hw_params(struct snd_pcm_substream *substream,
  92. struct snd_pcm_hw_params *params)
  93. {
  94. struct snd_pcm_runtime *runtime = substream->runtime;
  95. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  96. unsigned long dma_addr;
  97. struct dma_chan *chan;
  98. int ret;
  99. ret = mxs_dma_alloc(substream, params);
  100. if (ret)
  101. return ret;
  102. chan = iprtd->dma_chan;
  103. iprtd->size = params_buffer_bytes(params);
  104. iprtd->periods = params_periods(params);
  105. iprtd->period_bytes = params_period_bytes(params);
  106. iprtd->offset = 0;
  107. iprtd->period_time = HZ / (params_rate(params) /
  108. params_period_size(params));
  109. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  110. dma_addr = runtime->dma_addr;
  111. iprtd->buf = substream->dma_buffer.area;
  112. iprtd->desc = chan->device->device_prep_dma_cyclic(chan, dma_addr,
  113. iprtd->period_bytes * iprtd->periods,
  114. iprtd->period_bytes,
  115. substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  116. DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  117. if (!iprtd->desc) {
  118. dev_err(&chan->dev->device, "cannot prepare slave dma\n");
  119. return -EINVAL;
  120. }
  121. iprtd->desc->callback = audio_dma_irq;
  122. iprtd->desc->callback_param = substream;
  123. return 0;
  124. }
  125. static int snd_mxs_pcm_hw_free(struct snd_pcm_substream *substream)
  126. {
  127. struct snd_pcm_runtime *runtime = substream->runtime;
  128. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  129. if (iprtd->dma_chan) {
  130. dma_release_channel(iprtd->dma_chan);
  131. iprtd->dma_chan = NULL;
  132. }
  133. return 0;
  134. }
  135. static int snd_mxs_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  136. {
  137. struct snd_pcm_runtime *runtime = substream->runtime;
  138. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  139. switch (cmd) {
  140. case SNDRV_PCM_TRIGGER_START:
  141. case SNDRV_PCM_TRIGGER_RESUME:
  142. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  143. dmaengine_submit(iprtd->desc);
  144. break;
  145. case SNDRV_PCM_TRIGGER_STOP:
  146. case SNDRV_PCM_TRIGGER_SUSPEND:
  147. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  148. dmaengine_terminate_all(iprtd->dma_chan);
  149. break;
  150. default:
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. static snd_pcm_uframes_t snd_mxs_pcm_pointer(
  156. struct snd_pcm_substream *substream)
  157. {
  158. struct snd_pcm_runtime *runtime = substream->runtime;
  159. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  160. return bytes_to_frames(substream->runtime, iprtd->offset);
  161. }
  162. static int snd_mxs_open(struct snd_pcm_substream *substream)
  163. {
  164. struct snd_pcm_runtime *runtime = substream->runtime;
  165. struct mxs_pcm_runtime_data *iprtd;
  166. int ret;
  167. iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL);
  168. if (iprtd == NULL)
  169. return -ENOMEM;
  170. runtime->private_data = iprtd;
  171. ret = snd_pcm_hw_constraint_integer(substream->runtime,
  172. SNDRV_PCM_HW_PARAM_PERIODS);
  173. if (ret < 0) {
  174. kfree(iprtd);
  175. return ret;
  176. }
  177. snd_soc_set_runtime_hwparams(substream, &snd_mxs_hardware);
  178. return 0;
  179. }
  180. static int snd_mxs_close(struct snd_pcm_substream *substream)
  181. {
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. struct mxs_pcm_runtime_data *iprtd = runtime->private_data;
  184. kfree(iprtd);
  185. return 0;
  186. }
  187. static int snd_mxs_pcm_mmap(struct snd_pcm_substream *substream,
  188. struct vm_area_struct *vma)
  189. {
  190. struct snd_pcm_runtime *runtime = substream->runtime;
  191. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  192. runtime->dma_area,
  193. runtime->dma_addr,
  194. runtime->dma_bytes);
  195. }
  196. static struct snd_pcm_ops mxs_pcm_ops = {
  197. .open = snd_mxs_open,
  198. .close = snd_mxs_close,
  199. .ioctl = snd_pcm_lib_ioctl,
  200. .hw_params = snd_mxs_pcm_hw_params,
  201. .hw_free = snd_mxs_pcm_hw_free,
  202. .trigger = snd_mxs_pcm_trigger,
  203. .pointer = snd_mxs_pcm_pointer,
  204. .mmap = snd_mxs_pcm_mmap,
  205. };
  206. static int mxs_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  207. {
  208. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  209. struct snd_dma_buffer *buf = &substream->dma_buffer;
  210. size_t size = snd_mxs_hardware.buffer_bytes_max;
  211. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  212. buf->dev.dev = pcm->card->dev;
  213. buf->private_data = NULL;
  214. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  215. &buf->addr, GFP_KERNEL);
  216. if (!buf->area)
  217. return -ENOMEM;
  218. buf->bytes = size;
  219. return 0;
  220. }
  221. static u64 mxs_pcm_dmamask = DMA_BIT_MASK(32);
  222. static int mxs_pcm_new(struct snd_soc_pcm_runtime *rtd)
  223. {
  224. struct snd_card *card = rtd->card->snd_card;
  225. struct snd_pcm *pcm = rtd->pcm;
  226. int ret = 0;
  227. if (!card->dev->dma_mask)
  228. card->dev->dma_mask = &mxs_pcm_dmamask;
  229. if (!card->dev->coherent_dma_mask)
  230. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  231. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  232. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  233. SNDRV_PCM_STREAM_PLAYBACK);
  234. if (ret)
  235. goto out;
  236. }
  237. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  238. ret = mxs_pcm_preallocate_dma_buffer(pcm,
  239. SNDRV_PCM_STREAM_CAPTURE);
  240. if (ret)
  241. goto out;
  242. }
  243. out:
  244. return ret;
  245. }
  246. static void mxs_pcm_free(struct snd_pcm *pcm)
  247. {
  248. struct snd_pcm_substream *substream;
  249. struct snd_dma_buffer *buf;
  250. int stream;
  251. for (stream = 0; stream < 2; stream++) {
  252. substream = pcm->streams[stream].substream;
  253. if (!substream)
  254. continue;
  255. buf = &substream->dma_buffer;
  256. if (!buf->area)
  257. continue;
  258. dma_free_writecombine(pcm->card->dev, buf->bytes,
  259. buf->area, buf->addr);
  260. buf->area = NULL;
  261. }
  262. }
  263. static struct snd_soc_platform_driver mxs_soc_platform = {
  264. .ops = &mxs_pcm_ops,
  265. .pcm_new = mxs_pcm_new,
  266. .pcm_free = mxs_pcm_free,
  267. };
  268. static int __devinit mxs_soc_platform_probe(struct platform_device *pdev)
  269. {
  270. return snd_soc_register_platform(&pdev->dev, &mxs_soc_platform);
  271. }
  272. static int __devexit mxs_soc_platform_remove(struct platform_device *pdev)
  273. {
  274. snd_soc_unregister_platform(&pdev->dev);
  275. return 0;
  276. }
  277. static struct platform_driver mxs_pcm_driver = {
  278. .driver = {
  279. .name = "mxs-pcm-audio",
  280. .owner = THIS_MODULE,
  281. },
  282. .probe = mxs_soc_platform_probe,
  283. .remove = __devexit_p(mxs_soc_platform_remove),
  284. };
  285. module_platform_driver(mxs_pcm_driver);
  286. MODULE_LICENSE("GPL");
  287. MODULE_ALIAS("platform:mxs-pcm-audio");