tegra_pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 <sound/dmaengine_pcm.h>
  39. #include "tegra_pcm.h"
  40. static const struct snd_pcm_hardware tegra_pcm_hardware = {
  41. .info = SNDRV_PCM_INFO_MMAP |
  42. SNDRV_PCM_INFO_MMAP_VALID |
  43. SNDRV_PCM_INFO_PAUSE |
  44. SNDRV_PCM_INFO_RESUME |
  45. SNDRV_PCM_INFO_INTERLEAVED,
  46. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  47. .channels_min = 2,
  48. .channels_max = 2,
  49. .period_bytes_min = 1024,
  50. .period_bytes_max = PAGE_SIZE,
  51. .periods_min = 2,
  52. .periods_max = 8,
  53. .buffer_bytes_max = PAGE_SIZE * 8,
  54. .fifo_size = 4,
  55. };
  56. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  57. static void tegra_pcm_queue_dma(struct tegra_runtime_data *prtd)
  58. {
  59. struct snd_pcm_substream *substream = prtd->substream;
  60. struct snd_dma_buffer *buf = &substream->dma_buffer;
  61. struct tegra_dma_req *dma_req;
  62. unsigned long addr;
  63. dma_req = &prtd->dma_req[prtd->dma_req_idx];
  64. prtd->dma_req_idx = 1 - prtd->dma_req_idx;
  65. addr = buf->addr + prtd->dma_pos;
  66. prtd->dma_pos += dma_req->size;
  67. if (prtd->dma_pos >= prtd->dma_pos_end)
  68. prtd->dma_pos = 0;
  69. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  70. dma_req->source_addr = addr;
  71. else
  72. dma_req->dest_addr = addr;
  73. tegra_dma_enqueue_req(prtd->dma_chan, dma_req);
  74. }
  75. static void dma_complete_callback(struct tegra_dma_req *req)
  76. {
  77. struct tegra_runtime_data *prtd = (struct tegra_runtime_data *)req->dev;
  78. struct snd_pcm_substream *substream = prtd->substream;
  79. struct snd_pcm_runtime *runtime = substream->runtime;
  80. spin_lock(&prtd->lock);
  81. if (!prtd->running) {
  82. spin_unlock(&prtd->lock);
  83. return;
  84. }
  85. if (++prtd->period_index >= runtime->periods)
  86. prtd->period_index = 0;
  87. tegra_pcm_queue_dma(prtd);
  88. spin_unlock(&prtd->lock);
  89. snd_pcm_period_elapsed(substream);
  90. }
  91. static void setup_dma_tx_request(struct tegra_dma_req *req,
  92. struct tegra_pcm_dma_params * dmap)
  93. {
  94. req->complete = dma_complete_callback;
  95. req->to_memory = false;
  96. req->dest_addr = dmap->addr;
  97. req->dest_wrap = dmap->wrap;
  98. req->source_bus_width = 32;
  99. req->source_wrap = 0;
  100. req->dest_bus_width = dmap->width;
  101. req->req_sel = dmap->req_sel;
  102. }
  103. static void setup_dma_rx_request(struct tegra_dma_req *req,
  104. struct tegra_pcm_dma_params * dmap)
  105. {
  106. req->complete = dma_complete_callback;
  107. req->to_memory = true;
  108. req->source_addr = dmap->addr;
  109. req->dest_wrap = 0;
  110. req->source_bus_width = dmap->width;
  111. req->source_wrap = dmap->wrap;
  112. req->dest_bus_width = 32;
  113. req->req_sel = dmap->req_sel;
  114. }
  115. static int tegra_pcm_open(struct snd_pcm_substream *substream)
  116. {
  117. struct snd_pcm_runtime *runtime = substream->runtime;
  118. struct tegra_runtime_data *prtd;
  119. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  120. struct tegra_pcm_dma_params * dmap;
  121. int ret = 0;
  122. prtd = kzalloc(sizeof(struct tegra_runtime_data), GFP_KERNEL);
  123. if (prtd == NULL)
  124. return -ENOMEM;
  125. runtime->private_data = prtd;
  126. prtd->substream = substream;
  127. spin_lock_init(&prtd->lock);
  128. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  129. dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  130. setup_dma_tx_request(&prtd->dma_req[0], dmap);
  131. setup_dma_tx_request(&prtd->dma_req[1], dmap);
  132. } else {
  133. dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  134. setup_dma_rx_request(&prtd->dma_req[0], dmap);
  135. setup_dma_rx_request(&prtd->dma_req[1], dmap);
  136. }
  137. prtd->dma_req[0].dev = prtd;
  138. prtd->dma_req[1].dev = prtd;
  139. prtd->dma_chan = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
  140. if (prtd->dma_chan == NULL) {
  141. ret = -ENOMEM;
  142. goto err;
  143. }
  144. /* Set HW params now that initialization is complete */
  145. snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
  146. /* Ensure that buffer size is a multiple of period size */
  147. ret = snd_pcm_hw_constraint_integer(runtime,
  148. SNDRV_PCM_HW_PARAM_PERIODS);
  149. if (ret < 0)
  150. goto err;
  151. return 0;
  152. err:
  153. if (prtd->dma_chan) {
  154. tegra_dma_free_channel(prtd->dma_chan);
  155. }
  156. kfree(prtd);
  157. return ret;
  158. }
  159. static int tegra_pcm_close(struct snd_pcm_substream *substream)
  160. {
  161. struct snd_pcm_runtime *runtime = substream->runtime;
  162. struct tegra_runtime_data *prtd = runtime->private_data;
  163. tegra_dma_free_channel(prtd->dma_chan);
  164. kfree(prtd);
  165. return 0;
  166. }
  167. static int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
  168. struct snd_pcm_hw_params *params)
  169. {
  170. struct snd_pcm_runtime *runtime = substream->runtime;
  171. struct tegra_runtime_data *prtd = runtime->private_data;
  172. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  173. prtd->dma_req[0].size = params_period_bytes(params);
  174. prtd->dma_req[1].size = prtd->dma_req[0].size;
  175. return 0;
  176. }
  177. static int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
  178. {
  179. snd_pcm_set_runtime_buffer(substream, NULL);
  180. return 0;
  181. }
  182. static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  183. {
  184. struct snd_pcm_runtime *runtime = substream->runtime;
  185. struct tegra_runtime_data *prtd = runtime->private_data;
  186. unsigned long flags;
  187. switch (cmd) {
  188. case SNDRV_PCM_TRIGGER_START:
  189. prtd->dma_pos = 0;
  190. prtd->dma_pos_end = frames_to_bytes(runtime, runtime->periods * runtime->period_size);
  191. prtd->period_index = 0;
  192. prtd->dma_req_idx = 0;
  193. /* Fall-through */
  194. case SNDRV_PCM_TRIGGER_RESUME:
  195. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  196. spin_lock_irqsave(&prtd->lock, flags);
  197. prtd->running = 1;
  198. spin_unlock_irqrestore(&prtd->lock, flags);
  199. tegra_pcm_queue_dma(prtd);
  200. tegra_pcm_queue_dma(prtd);
  201. break;
  202. case SNDRV_PCM_TRIGGER_STOP:
  203. case SNDRV_PCM_TRIGGER_SUSPEND:
  204. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  205. spin_lock_irqsave(&prtd->lock, flags);
  206. prtd->running = 0;
  207. spin_unlock_irqrestore(&prtd->lock, flags);
  208. tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[0]);
  209. tegra_dma_dequeue_req(prtd->dma_chan, &prtd->dma_req[1]);
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. return 0;
  215. }
  216. static snd_pcm_uframes_t tegra_pcm_pointer(struct snd_pcm_substream *substream)
  217. {
  218. struct snd_pcm_runtime *runtime = substream->runtime;
  219. struct tegra_runtime_data *prtd = runtime->private_data;
  220. return prtd->period_index * runtime->period_size;
  221. }
  222. static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
  223. struct vm_area_struct *vma)
  224. {
  225. struct snd_pcm_runtime *runtime = substream->runtime;
  226. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  227. runtime->dma_area,
  228. runtime->dma_addr,
  229. runtime->dma_bytes);
  230. }
  231. static struct snd_pcm_ops tegra_pcm_ops = {
  232. .open = tegra_pcm_open,
  233. .close = tegra_pcm_close,
  234. .ioctl = snd_pcm_lib_ioctl,
  235. .hw_params = tegra_pcm_hw_params,
  236. .hw_free = tegra_pcm_hw_free,
  237. .trigger = tegra_pcm_trigger,
  238. .pointer = tegra_pcm_pointer,
  239. .mmap = tegra_pcm_mmap,
  240. };
  241. #else
  242. static int tegra_pcm_open(struct snd_pcm_substream *substream)
  243. {
  244. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  245. struct device *dev = rtd->platform->dev;
  246. int ret;
  247. /* Set HW params now that initialization is complete */
  248. snd_soc_set_runtime_hwparams(substream, &tegra_pcm_hardware);
  249. ret = snd_dmaengine_pcm_open(substream, NULL, NULL);
  250. if (ret) {
  251. dev_err(dev, "dmaengine pcm open failed with err %d\n", ret);
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. static int tegra_pcm_close(struct snd_pcm_substream *substream)
  257. {
  258. snd_dmaengine_pcm_close(substream);
  259. return 0;
  260. }
  261. static int tegra_pcm_hw_params(struct snd_pcm_substream *substream,
  262. struct snd_pcm_hw_params *params)
  263. {
  264. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  265. struct device *dev = rtd->platform->dev;
  266. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  267. struct tegra_pcm_dma_params *dmap;
  268. struct dma_slave_config slave_config;
  269. int ret;
  270. dmap = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  271. ret = snd_hwparams_to_dma_slave_config(substream, params,
  272. &slave_config);
  273. if (ret) {
  274. dev_err(dev, "hw params config failed with err %d\n", ret);
  275. return ret;
  276. }
  277. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  278. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  279. slave_config.dst_addr = dmap->addr;
  280. slave_config.src_maxburst = 0;
  281. } else {
  282. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  283. slave_config.src_addr = dmap->addr;
  284. slave_config.dst_maxburst = 0;
  285. }
  286. slave_config.slave_id = dmap->req_sel;
  287. ret = dmaengine_slave_config(chan, &slave_config);
  288. if (ret < 0) {
  289. dev_err(dev, "dma slave config failed with err %d\n", ret);
  290. return ret;
  291. }
  292. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  293. return 0;
  294. }
  295. static int tegra_pcm_hw_free(struct snd_pcm_substream *substream)
  296. {
  297. snd_pcm_set_runtime_buffer(substream, NULL);
  298. return 0;
  299. }
  300. static int tegra_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  301. {
  302. switch (cmd) {
  303. case SNDRV_PCM_TRIGGER_START:
  304. case SNDRV_PCM_TRIGGER_RESUME:
  305. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  306. return snd_dmaengine_pcm_trigger(substream,
  307. SNDRV_PCM_TRIGGER_START);
  308. case SNDRV_PCM_TRIGGER_STOP:
  309. case SNDRV_PCM_TRIGGER_SUSPEND:
  310. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  311. return snd_dmaengine_pcm_trigger(substream,
  312. SNDRV_PCM_TRIGGER_STOP);
  313. default:
  314. return -EINVAL;
  315. }
  316. return 0;
  317. }
  318. static int tegra_pcm_mmap(struct snd_pcm_substream *substream,
  319. struct vm_area_struct *vma)
  320. {
  321. struct snd_pcm_runtime *runtime = substream->runtime;
  322. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  323. runtime->dma_area,
  324. runtime->dma_addr,
  325. runtime->dma_bytes);
  326. }
  327. static struct snd_pcm_ops tegra_pcm_ops = {
  328. .open = tegra_pcm_open,
  329. .close = tegra_pcm_close,
  330. .ioctl = snd_pcm_lib_ioctl,
  331. .hw_params = tegra_pcm_hw_params,
  332. .hw_free = tegra_pcm_hw_free,
  333. .trigger = tegra_pcm_trigger,
  334. .pointer = snd_dmaengine_pcm_pointer,
  335. .mmap = tegra_pcm_mmap,
  336. };
  337. #endif
  338. static int tegra_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  339. {
  340. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  341. struct snd_dma_buffer *buf = &substream->dma_buffer;
  342. size_t size = tegra_pcm_hardware.buffer_bytes_max;
  343. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  344. &buf->addr, GFP_KERNEL);
  345. if (!buf->area)
  346. return -ENOMEM;
  347. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  348. buf->dev.dev = pcm->card->dev;
  349. buf->private_data = NULL;
  350. buf->bytes = size;
  351. return 0;
  352. }
  353. static void tegra_pcm_deallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  354. {
  355. struct snd_pcm_substream *substream;
  356. struct snd_dma_buffer *buf;
  357. substream = pcm->streams[stream].substream;
  358. if (!substream)
  359. return;
  360. buf = &substream->dma_buffer;
  361. if (!buf->area)
  362. return;
  363. dma_free_writecombine(pcm->card->dev, buf->bytes,
  364. buf->area, buf->addr);
  365. buf->area = NULL;
  366. }
  367. static u64 tegra_dma_mask = DMA_BIT_MASK(32);
  368. static int tegra_pcm_new(struct snd_soc_pcm_runtime *rtd)
  369. {
  370. struct snd_card *card = rtd->card->snd_card;
  371. struct snd_pcm *pcm = rtd->pcm;
  372. int ret = 0;
  373. if (!card->dev->dma_mask)
  374. card->dev->dma_mask = &tegra_dma_mask;
  375. if (!card->dev->coherent_dma_mask)
  376. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  377. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  378. ret = tegra_pcm_preallocate_dma_buffer(pcm,
  379. SNDRV_PCM_STREAM_PLAYBACK);
  380. if (ret)
  381. goto err;
  382. }
  383. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  384. ret = tegra_pcm_preallocate_dma_buffer(pcm,
  385. SNDRV_PCM_STREAM_CAPTURE);
  386. if (ret)
  387. goto err_free_play;
  388. }
  389. return 0;
  390. err_free_play:
  391. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
  392. err:
  393. return ret;
  394. }
  395. static void tegra_pcm_free(struct snd_pcm *pcm)
  396. {
  397. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_CAPTURE);
  398. tegra_pcm_deallocate_dma_buffer(pcm, SNDRV_PCM_STREAM_PLAYBACK);
  399. }
  400. static struct snd_soc_platform_driver tegra_pcm_platform = {
  401. .ops = &tegra_pcm_ops,
  402. .pcm_new = tegra_pcm_new,
  403. .pcm_free = tegra_pcm_free,
  404. };
  405. int __devinit tegra_pcm_platform_register(struct device *dev)
  406. {
  407. return snd_soc_register_platform(dev, &tegra_pcm_platform);
  408. }
  409. EXPORT_SYMBOL_GPL(tegra_pcm_platform_register);
  410. void __devexit tegra_pcm_platform_unregister(struct device *dev)
  411. {
  412. snd_soc_unregister_platform(dev);
  413. }
  414. EXPORT_SYMBOL_GPL(tegra_pcm_platform_unregister);
  415. MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
  416. MODULE_DESCRIPTION("Tegra PCM ASoC driver");
  417. MODULE_LICENSE("GPL");