tegra_pcm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * tegra_pcm.c - Tegra PCM driver
  3. *
  4. * Author: Stephen Warren <swarren@nvidia.com>
  5. * Copyright (C) 2010,2012 - NVIDIA, Inc.
  6. *
  7. * Based on code copyright/by:
  8. *
  9. * Copyright (c) 2009-2010, NVIDIA Corporation.
  10. * Scott Peterson <speterson@nvidia.com>
  11. * Vijay Mali <vmali@nvidia.com>
  12. *
  13. * Copyright (C) 2010 Google, Inc.
  14. * Iliyan Malchev <malchev@google.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * version 2 as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  28. * 02110-1301 USA
  29. *
  30. */
  31. #include <linux/dma-mapping.h>
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include "tegra_pcm.h"
  39. static const struct snd_pcm_hardware tegra_pcm_hardware = {
  40. .info = SNDRV_PCM_INFO_MMAP |
  41. SNDRV_PCM_INFO_MMAP_VALID |
  42. SNDRV_PCM_INFO_PAUSE |
  43. SNDRV_PCM_INFO_RESUME |
  44. SNDRV_PCM_INFO_INTERLEAVED,
  45. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  46. .channels_min = 2,
  47. .channels_max = 2,
  48. .period_bytes_min = 1024,
  49. .period_bytes_max = PAGE_SIZE,
  50. .periods_min = 2,
  51. .periods_max = 8,
  52. .buffer_bytes_max = PAGE_SIZE * 8,
  53. .fifo_size = 4,
  54. };
  55. static void tegra_pcm_queue_dma(struct tegra_runtime_data *prtd)
  56. {
  57. struct snd_pcm_substream *substream = prtd->substream;
  58. struct snd_dma_buffer *buf = &substream->dma_buffer;
  59. struct tegra_dma_req *dma_req;
  60. unsigned long addr;
  61. dma_req = &prtd->dma_req[prtd->dma_req_idx];
  62. prtd->dma_req_idx = 1 - prtd->dma_req_idx;
  63. addr = buf->addr + prtd->dma_pos;
  64. prtd->dma_pos += dma_req->size;
  65. if (prtd->dma_pos >= prtd->dma_pos_end)
  66. prtd->dma_pos = 0;
  67. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  68. dma_req->source_addr = addr;
  69. else
  70. dma_req->dest_addr = addr;
  71. tegra_dma_enqueue_req(prtd->dma_chan, dma_req);
  72. }
  73. static void dma_complete_callback(struct tegra_dma_req *req)
  74. {
  75. struct tegra_runtime_data *prtd = (struct tegra_runtime_data *)req->dev;
  76. struct snd_pcm_substream *substream = prtd->substream;
  77. struct snd_pcm_runtime *runtime = substream->runtime;
  78. spin_lock(&prtd->lock);
  79. if (!prtd->running) {
  80. spin_unlock(&prtd->lock);
  81. return;
  82. }
  83. if (++prtd->period_index >= runtime->periods)
  84. prtd->period_index = 0;
  85. tegra_pcm_queue_dma(prtd);
  86. spin_unlock(&prtd->lock);
  87. snd_pcm_period_elapsed(substream);
  88. }
  89. static void setup_dma_tx_request(struct tegra_dma_req *req,
  90. struct tegra_pcm_dma_params * dmap)
  91. {
  92. req->complete = dma_complete_callback;
  93. req->to_memory = false;
  94. req->dest_addr = dmap->addr;
  95. req->dest_wrap = dmap->wrap;
  96. req->source_bus_width = 32;
  97. req->source_wrap = 0;
  98. req->dest_bus_width = dmap->width;
  99. req->req_sel = dmap->req_sel;
  100. }
  101. static void setup_dma_rx_request(struct tegra_dma_req *req,
  102. struct tegra_pcm_dma_params * dmap)
  103. {
  104. req->complete = dma_complete_callback;
  105. req->to_memory = true;
  106. req->source_addr = dmap->addr;
  107. req->dest_wrap = 0;
  108. req->source_bus_width = dmap->width;
  109. req->source_wrap = dmap->wrap;
  110. req->dest_bus_width = 32;
  111. req->req_sel = dmap->req_sel;
  112. }
  113. static int tegra_pcm_open(struct snd_pcm_substream *substream)
  114. {
  115. struct snd_pcm_runtime *runtime = substream->runtime;
  116. struct tegra_runtime_data *prtd;
  117. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. struct tegra_pcm_dma_params * dmap;
  119. int ret = 0;
  120. prtd = kzalloc(sizeof(struct tegra_runtime_data), GFP_KERNEL);
  121. if (prtd == NULL)
  122. return -ENOMEM;
  123. runtime->private_data = prtd;
  124. prtd->substream = substream;
  125. spin_lock_init(&prtd->lock);
  126. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  127. dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  128. setup_dma_tx_request(&prtd->dma_req[0], dmap);
  129. setup_dma_tx_request(&prtd->dma_req[1], dmap);
  130. } else {
  131. dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  132. setup_dma_rx_request(&prtd->dma_req[0], dmap);
  133. setup_dma_rx_request(&prtd->dma_req[1], dmap);
  134. }
  135. prtd->dma_req[0].dev = prtd;
  136. prtd->dma_req[1].dev = prtd;
  137. prtd->dma_chan = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
  138. if (prtd->dma_chan == NULL) {
  139. ret = -ENOMEM;
  140. goto err;
  141. }
  142. /* Set HW params now that initialization is complete */
  143. snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
  144. /* Ensure that buffer size is a multiple of period size */
  145. ret = snd_pcm_hw_constraint_integer(runtime,
  146. SNDRV_PCM_HW_PARAM_PERIODS);
  147. if (ret < 0)
  148. goto err;
  149. return 0;
  150. err:
  151. if (prtd->dma_chan) {
  152. tegra_dma_free_channel(prtd->dma_chan);
  153. }
  154. kfree(prtd);
  155. return ret;
  156. }
  157. static int tegra_pcm_close(struct snd_pcm_substream *substream)
  158. {
  159. struct snd_pcm_runtime *runtime = substream->runtime;
  160. struct tegra_runtime_data *prtd = runtime->private_data;
  161. tegra_dma_free_channel(prtd->dma_chan);
  162. kfree(prtd);
  163. return 0;
  164. }
  165. static int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
  166. struct snd_pcm_hw_params *params)
  167. {
  168. struct snd_pcm_runtime *runtime = substream->runtime;
  169. struct tegra_runtime_data *prtd = runtime->private_data;
  170. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  171. prtd->dma_req[0].size = params_period_bytes(params);
  172. prtd->dma_req[1].size = prtd->dma_req[0].size;
  173. return 0;
  174. }
  175. static int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
  176. {
  177. snd_pcm_set_runtime_buffer(substream, NULL);
  178. return 0;
  179. }
  180. static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  181. {
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. struct tegra_runtime_data *prtd = runtime->private_data;
  184. unsigned long flags;
  185. switch (cmd) {
  186. case SNDRV_PCM_TRIGGER_START:
  187. prtd->dma_pos = 0;
  188. prtd->dma_pos_end = frames_to_bytes(runtime, runtime->periods * runtime->period_size);
  189. prtd->period_index = 0;
  190. prtd->dma_req_idx = 0;
  191. /* Fall-through */
  192. case SNDRV_PCM_TRIGGER_RESUME:
  193. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  194. spin_lock_irqsave(&prtd->lock, flags);
  195. prtd->running = 1;
  196. spin_unlock_irqrestore(&prtd->lock, flags);
  197. tegra_pcm_queue_dma(prtd);
  198. tegra_pcm_queue_dma(prtd);
  199. break;
  200. case SNDRV_PCM_TRIGGER_STOP:
  201. case SNDRV_PCM_TRIGGER_SUSPEND:
  202. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  203. spin_lock_irqsave(&prtd->lock, flags);
  204. prtd->running = 0;
  205. spin_unlock_irqrestore(&prtd->lock, flags);
  206. tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[0]);
  207. tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[1]);
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. return 0;
  213. }
  214. static snd_pcm_uframes_t tegra_pcm_pointer(struct snd_pcm_substream *substream)
  215. {
  216. struct snd_pcm_runtime *runtime = substream->runtime;
  217. struct tegra_runtime_data *prtd = runtime->private_data;
  218. return prtd->period_index * runtime->period_size;
  219. }
  220. static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
  221. struct vm_area_struct *vma)
  222. {
  223. struct snd_pcm_runtime *runtime = substream->runtime;
  224. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  225. runtime->dma_area,
  226. runtime->dma_addr,
  227. runtime->dma_bytes);
  228. }
  229. static struct snd_pcm_ops tegra_pcm_ops = {
  230. .open = tegra_pcm_open,
  231. .close = tegra_pcm_close,
  232. .ioctl = snd_pcm_lib_ioctl,
  233. .hw_params = tegra_pcm_hw_params,
  234. .hw_free = tegra_pcm_hw_free,
  235. .trigger = tegra_pcm_trigger,
  236. .pointer = tegra_pcm_pointer,
  237. .mmap = tegra_pcm_mmap,
  238. };
  239. static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  240. {
  241. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  242. struct snd_dma_buffer *buf = &substream->dma_buffer;
  243. size_t size = tegra_pcm_hardware.buffer_bytes_max;
  244. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  245. &buf->addr, GFP_KERNEL);
  246. if (!buf->area)
  247. return -ENOMEM;
  248. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  249. buf->dev.dev = pcm->card->dev;
  250. buf->private_data = NULL;
  251. buf->bytes = size;
  252. return 0;
  253. }
  254. static void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  255. {
  256. struct snd_pcm_substream *substream;
  257. struct snd_dma_buffer *buf;
  258. substream = pcm->streams[stream].substream;
  259. if (!substream)
  260. return;
  261. buf = &substream->dma_buffer;
  262. if (!buf->area)
  263. return;
  264. dma_free_writecombine(pcm->card->dev, buf->bytes,
  265. buf->area, buf->addr);
  266. buf->area = NULL;
  267. }
  268. static u64 tegra_dma_mask = DMA_BIT_MASK(32);
  269. static int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
  270. {
  271. struct snd_card *card = rtd->card->snd_card;
  272. struct snd_pcm *pcm = rtd->pcm;
  273. int ret = 0;
  274. if (!card->dev->dma_mask)
  275. card->dev->dma_mask = &tegra_dma_mask;
  276. if (!card->dev->coherent_dma_mask)
  277. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  278. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  279. ret = tegra_pcm_preallocate_dma_buffer(pcm,
  280. SNDRV_PCM_STREAM_PLAYBACK);
  281. if (ret)
  282. goto err;
  283. }
  284. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  285. ret = tegra_pcm_preallocate_dma_buffer(pcm,
  286. SNDRV_PCM_STREAM_CAPTURE);
  287. if (ret)
  288. goto err_free_play;
  289. }
  290. return 0;
  291. err_free_play:
  292. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
  293. err:
  294. return ret;
  295. }
  296. static void tegra_pcm_free(struct snd_pcm *pcm)
  297. {
  298. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
  299. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
  300. }
  301. static struct snd_soc_platform_driver tegra_pcm_platform = {
  302. .ops = &tegra_pcm_ops,
  303. .pcm_new = tegra_pcm_new,
  304. .pcm_free = tegra_pcm_free,
  305. };
  306. int __devinit tegra_pcm_platform_register(struct device *dev)
  307. {
  308. return snd_soc_register_platform(dev, &tegra_pcm_platform);
  309. }
  310. EXPORT_SYMBOL_GPL(tegra_pcm_platform_register);
  311. void __devexit tegra_pcm_platform_unregister(struct device *dev)
  312. {
  313. snd_soc_unregister_platform(dev);
  314. }
  315. EXPORT_SYMBOL_GPL(tegra_pcm_platform_unregister);
  316. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  317. MODULE_DESCRIPTION("Tegra PCM ASoC driver");
  318. MODULE_LICENSE("GPL");